(MAKEIT.BAT): Modify for apel-ja@lists.chise.org.
[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,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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 'custom)
33 (eval-when-compile (require 'wid-edit))
34
35 (if (featurep 'xemacs)
36     (require 'mcs-xm)
37   (require 'mcs-e20))
38
39
40 ;;; @ MIME charset
41 ;;;
42
43 (defcustom mime-charset-coding-system-alist
44   (let ((rest
45          '((us-ascii      . raw-text)
46            (gb2312        . cn-gb-2312)
47            (cn-gb         . cn-gb-2312)
48            (iso-2022-jp-2 . iso-2022-7bit-ss2)
49            (iso-2022-jp-3 . iso-2022-7bit-ss2)
50            (tis-620       . tis620)
51            (windows-874   . tis-620)
52            (cp874         . tis-620)
53            (x-ctext       . ctext)
54            (unknown       . undecided)
55            (x-unknown     . undecided)
56            ))
57         dest)
58     (while rest
59       (let ((pair (car rest)))
60         (or (find-coding-system (car pair))
61             (setq dest (cons pair dest))
62             ))
63       (setq rest (cdr rest))
64       )
65     dest)
66   "Alist MIME CHARSET vs CODING-SYSTEM.
67 MIME CHARSET and CODING-SYSTEM must be symbol."
68   :group 'i18n
69   :type '(repeat (cons symbol coding-system)))
70
71 (defcustom mime-charset-to-coding-system-default-method
72   nil
73   "Function called when suitable coding-system is not found from MIME-charset.
74 It must be nil or function.
75 If it is a function, interface must be (CHARSET LBT CODING-SYSTEM)."
76   :group 'i18n
77   :type '(choice function (const nil)))
78
79 (defun mime-charset-to-coding-system (charset &optional lbt)
80   "Return coding-system corresponding with CHARSET.
81 CHARSET is a symbol whose name is MIME charset.
82 If optional argument LBT (`CRLF', `LF', `CR', `unix', `dos' or `mac')
83 is specified, it is used as line break code type of coding-system."
84   (if (stringp charset)
85       (setq charset (intern (downcase charset)))
86     )
87   (let ((cs (assq charset mime-charset-coding-system-alist)))
88     (setq cs
89           (if cs
90               (cdr cs)
91             charset))
92     (if lbt
93         (setq cs (intern (format "%s-%s" cs
94                                  (cond ((eq lbt 'CRLF) 'dos)
95                                        ((eq lbt 'LF) 'unix)
96                                        ((eq lbt 'CR) 'mac)
97                                        (t lbt)))))
98       )
99     (if (find-coding-system cs)
100         cs
101       (if mime-charset-to-coding-system-default-method
102           (funcall mime-charset-to-coding-system-default-method
103                    charset lbt cs)
104         ))))
105
106 (defalias 'mime-charset-p 'mime-charset-to-coding-system)
107
108 (defvar widget-mime-charset-prompt-value-history nil
109   "History of input to `widget-mime-charset-prompt-value'.")
110
111 (define-widget 'mime-charset 'coding-system
112   "A mime-charset."
113   :format "%{%t%}: %v"
114   :tag "MIME-charset"
115   :prompt-history 'widget-mime-charset-prompt-value-history
116   :prompt-value 'widget-mime-charset-prompt-value
117   :action 'widget-mime-charset-action)
118
119 (defun widget-mime-charset-prompt-value (widget prompt value unbound)
120   ;; Read mime-charset from minibuffer.
121   (intern
122    (completing-read (format "%s (default %s) " prompt value)
123                     (mapcar (function
124                              (lambda (sym)
125                                (list (symbol-name sym))))
126                             (mime-charset-list)))))
127
128 (defun widget-mime-charset-action (widget &optional event)
129   ;; Read a mime-charset from the minibuffer.
130   (let ((answer
131          (widget-mime-charset-prompt-value
132           widget
133           (widget-apply widget :menu-tag-get)
134           (widget-value widget)
135           t)))
136     (widget-value-set widget answer)
137     (widget-apply widget :notify widget event)
138     (widget-setup)))
139
140 (defcustom default-mime-charset 'x-unknown
141   "Default value of MIME-charset.
142 It is used when MIME-charset is not specified.
143 It must be symbol."
144   :group 'i18n
145   :type 'mime-charset)
146
147 (cond ((featurep 'utf-2000)
148 ;; for CHISE Architecture
149 (defun mcs-region-repertoire-p (start end charsets &optional buffer)
150   (save-excursion
151     (if buffer
152         (set-buffer buffer))
153     (save-restriction
154       (narrow-to-region start end)
155       (goto-char (point-min))
156       (catch 'tag
157         (let (ch)
158           (while (not (eobp))
159             (setq ch (char-after (point)))
160             (unless (some (lambda (ccs)
161                             (encode-char ch ccs))
162                           charsets)
163               (throw 'tag nil))
164             (forward-char)))
165         t))))
166
167 (defun mcs-string-repertoire-p (string charsets &optional start end)
168   (let ((i (if start
169                (if (< start 0)
170                    (error 'args-out-of-range string start end)
171                  start)
172              0))
173         ch)
174     (if end
175         (if (> end (length string))
176             (error 'args-out-of-range string start end))
177       (setq end (length string)))
178     (catch 'tag
179       (while (< i end)
180         (setq ch (aref string i))
181         (unless (some (lambda (ccs)
182                         (encode-char ch ccs))
183                       charsets)
184           (throw 'tag nil))
185         (setq i (1+ i)))
186       t)))
187
188 (defun detect-mime-charset-region (start end)
189   "Return MIME charset for region between START and END."
190   (let ((rest charsets-mime-charset-alist)
191         cell)
192     (catch 'tag
193       (while rest
194         (setq cell (car rest))
195         (if (mcs-region-repertoire-p start end (car cell))
196             (throw 'tag (cdr cell)))
197         (setq rest (cdr rest)))
198       default-mime-charset-for-write)))
199
200 (defun detect-mime-charset-string (string)
201   "Return MIME charset for STRING."
202   (let ((rest charsets-mime-charset-alist)
203         cell)
204     (catch 'tag
205       (while rest
206         (setq cell (car rest))
207         (if (mcs-string-repertoire-p string (car cell))
208             (throw 'tag (cdr cell)))
209         (setq rest (cdr rest)))
210       default-mime-charset-for-write)))
211 )
212 (t
213 ;; for legacy Mule
214 (defun detect-mime-charset-region (start end)
215   "Return MIME charset for region between START and END."
216   (find-mime-charset-by-charsets (find-charset-region start end)
217                                  'region start end))
218 ))
219
220 (defun write-region-as-mime-charset (charset start end filename
221                                              &optional append visit lockname)
222   "Like `write-region', q.v., but encode by MIME CHARSET."
223   (let ((coding-system-for-write
224          (or (mime-charset-to-coding-system charset)
225              'binary)))
226     (write-region start end filename append visit lockname)))
227
228
229 ;;; @ end
230 ;;;
231
232 (require 'product)
233 (product-provide (provide 'mcs-20) (require 'apel-ver))
234
235 ;;; mcs-20.el ends here