(enriched-encode): Allow the 3rd argument ORIG-BUF for old Emacsen.
[elisp/apel.git] / mcs-20.el
1 ;;; mcs-20.el --- MIME charset implementation for Emacs 20 and XEmacs/mule
2
3 ;; Copyright (C) 1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, Mule
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;    This module requires Emacs 20.0.93, XEmacs 20.3-b5 (with mule)
28 ;;    or later.
29
30 ;;; Code:
31
32 (require 'poem)
33 (require 'pcustom)
34 (eval-when-compile (require 'wid-edit))
35
36
37 ;;; @ MIME charset
38 ;;;
39
40 (defcustom mime-charset-coding-system-alist
41   (let ((rest
42          '((us-ascii      . raw-text)
43            (gb2312        . cn-gb-2312)
44            (cn-gb         . cn-gb-2312)
45            (iso-2022-jp-2 . iso-2022-7bit-ss2)
46            (iso-2022-jp-3 . iso-2022-7bit-ss2)
47            (tis-620       . tis620)
48            (windows-874   . tis-620)
49            (cp874         . tis-620)
50            (x-ctext       . ctext)
51            (unknown       . undecided)
52            (x-unknown     . undecided)
53            ))
54         dest)
55     (while rest
56       (let ((pair (car rest)))
57         (or (find-coding-system (car pair))
58             (setq dest (cons pair dest))
59             ))
60       (setq rest (cdr rest))
61       )
62     dest)
63   "Alist MIME CHARSET vs CODING-SYSTEM.
64 MIME CHARSET and CODING-SYSTEM must be symbol."
65   :group 'i18n
66   :type '(repeat (cons symbol coding-system)))
67
68 (defcustom mime-charset-to-coding-system-default-method
69   nil
70   "Function called when suitable coding-system is not found from MIME-charset.
71 It must be nil or function.
72 If it is a function, interface must be (CHARSET LBT CODING-SYSTEM)."
73   :group 'i18n
74   :type '(choice function (const nil)))
75
76 (defun mime-charset-to-coding-system (charset &optional lbt)
77   "Return coding-system corresponding with CHARSET.
78 CHARSET is a symbol whose name is MIME charset.
79 If optional argument LBT (`CRLF', `LF', `CR', `unix', `dos' or `mac')
80 is specified, it is used as line break code type of coding-system."
81   (if (stringp charset)
82       (setq charset (intern (downcase charset)))
83     )
84   (let ((cs (assq charset mime-charset-coding-system-alist)))
85     (setq cs
86           (if cs
87               (cdr cs)
88             charset))
89     (if lbt
90         (setq cs (intern (format "%s-%s" cs
91                                  (cond ((eq lbt 'CRLF) 'dos)
92                                        ((eq lbt 'LF) 'unix)
93                                        ((eq lbt 'CR) 'mac)
94                                        (t lbt)))))
95       )
96     (if (find-coding-system cs)
97         cs
98       (if mime-charset-to-coding-system-default-method
99           (funcall mime-charset-to-coding-system-default-method
100                    charset lbt cs)
101         ))))
102
103 (defvar widget-mime-charset-prompt-value-history nil
104   "History of input to `widget-mime-charset-prompt-value'.")
105
106 (define-widget 'mime-charset 'coding-system
107   "A mime-charset."
108   :format "%{%t%}: %v"
109   :tag "MIME-charset"
110   :prompt-history 'widget-mime-charset-prompt-value-history
111   :prompt-value 'widget-mime-charset-prompt-value
112   :action 'widget-mime-charset-action)
113
114 (defun widget-mime-charset-prompt-value (widget prompt value unbound)
115   ;; Read mime-charset from minibuffer.
116   (intern
117    (completing-read (format "%s (default %s) " prompt value)
118                     (mapcar (function
119                              (lambda (sym)
120                                (list (symbol-name sym))))
121                             (mime-charset-list)))))
122
123 (defun widget-mime-charset-action (widget &optional event)
124   ;; Read a mime-charset from the minibuffer.
125   (let ((answer
126          (widget-mime-charset-prompt-value
127           widget
128           (widget-apply widget :menu-tag-get)
129           (widget-value widget)
130           t)))
131     (widget-value-set widget answer)
132     (widget-apply widget :notify widget event)
133     (widget-setup)))
134
135 (defcustom default-mime-charset 'x-ctext
136   "Default value of MIME-charset.
137 It is used when MIME-charset is not specified.
138 It must be symbol."
139   :group 'i18n
140   :type 'mime-charset)
141
142 (defun detect-mime-charset-region (start end)
143   "Return MIME charset for region between START and END."
144   (find-mime-charset-by-charsets (find-charset-region start end)
145                                  'region start end))
146
147 (defun write-region-as-mime-charset (charset start end filename
148                                              &optional append visit lockname)
149   "Like `write-region', q.v., but encode by MIME CHARSET."
150   (let ((coding-system-for-write
151          (or (mime-charset-to-coding-system charset)
152              'binary)))
153     (write-region start end filename append visit lockname)))
154
155
156 ;;; @ end
157 ;;;
158
159 (require 'product)
160 (product-provide (provide 'mcs-20) (require 'apel-ver))
161
162 ;;; mcs-20.el ends here