update keywords.
[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 'custom)
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            (tis-620       . tis620)
47            (windows-874   . tis620)
48            (x-ctext       . ctext)
49            (unknown       . undecided)
50            (x-unknown     . undecided)
51            ))
52         dest)
53     (while rest
54       (let ((pair (car rest)))
55         (or (find-coding-system (car pair))
56             (setq dest (cons pair dest))
57             ))
58       (setq rest (cdr rest))
59       )
60     dest)
61   "Alist MIME CHARSET vs CODING-SYSTEM.
62 MIME CHARSET and CODING-SYSTEM must be symbol."
63   :group 'i18n
64   :type '(repeat (cons symbol coding-system)))
65
66 (defcustom mime-charset-to-coding-system-default-method
67   nil
68   "Function called when suitable coding-system is not found from MIME-charset.
69 It must be nil or function.
70 If it is a function, interface must be (CHARSET LBT CODING-SYSTEM)."
71   :group 'i18n
72   :type '(choice function (const nil)))
73
74 (defsubst mime-charset-to-coding-system (charset &optional lbt)
75   "Return coding-system corresponding with CHARSET.
76 CHARSET is a symbol whose name is MIME charset.
77 If optional argument LBT (`CRLF', `LF', `CR', `unix', `dos' or `mac')
78 is specified, it is used as line break code type of coding-system."
79   (if (stringp charset)
80       (setq charset (intern (downcase charset)))
81     )
82   (let ((cs (assq charset mime-charset-coding-system-alist)))
83     (setq cs
84           (if cs
85               (cdr cs)
86             charset))
87     (if lbt
88         (setq cs (intern (format "%s-%s" cs
89                                  (cond ((eq lbt 'CRLF) 'dos)
90                                        ((eq lbt 'LF) 'unix)
91                                        ((eq lbt 'CR) 'mac)
92                                        (t lbt)))))
93       )
94     (if (find-coding-system cs)
95         cs
96       (if mime-charset-to-coding-system-default-method
97           (funcall mime-charset-to-coding-system-default-method
98                    charset lbt cs)
99         ))))
100
101 (defvar widget-mime-charset-prompt-value-history nil
102   "History of input to `widget-mime-charset-prompt-value'.")
103
104 (define-widget 'mime-charset 'coding-system
105   "A mime-charset."
106   :format "%{%t%}: %v"
107   :tag "MIME-charset"
108   :prompt-history 'widget-mime-charset-prompt-value-history
109   :prompt-value 'widget-mime-charset-prompt-value
110   :action 'widget-mime-charset-action)
111
112 (defun widget-mime-charset-prompt-value (widget prompt value unbound)
113   ;; Read mime-charset from minibuffer.
114   (intern
115    (completing-read (format "%s (default %s) " prompt value)
116                     (mapcar (function
117                              (lambda (sym)
118                                (list (symbol-name sym))))
119                             (mime-charset-list)))))
120
121 (defun widget-mime-charset-action (widget &optional event)
122   ;; Read a mime-charset from the minibuffer.
123   (let ((answer
124          (widget-mime-charset-prompt-value
125           widget
126           (widget-apply widget :menu-tag-get)
127           (widget-value widget)
128           t)))
129     (widget-value-set widget answer)
130     (widget-apply widget :notify widget event)
131     (widget-setup)))
132
133 (defcustom default-mime-charset 'x-ctext
134   "Default value of MIME-charset.
135 It is used when MIME-charset is not specified.
136 It must be symbol."
137   :group 'i18n
138   :type 'mime-charset)
139
140 (defcustom default-mime-charset-for-write
141   (if (find-coding-system 'utf-8)
142       'utf-8
143     default-mime-charset)
144   "Default value of MIME-charset for encoding.
145 It may be used when suitable MIME-charset is not found.
146 It must be symbol."
147   :group 'i18n
148   :type 'mime-charset)
149
150 (defcustom default-mime-charset-detect-method-for-write
151   nil
152   "Function called when suitable MIME-charset is not found to encode.
153 It must be nil or function.
154 If it is nil, variable `default-mime-charset-for-write' is used.
155 If it is a function, interface must be (TYPE CHARSETS &rest ARGS).
156 CHARSETS is list of charset.
157 If TYPE is 'region, ARGS has START and END."
158   :group 'i18n
159   :type '(choice function (const nil)))
160
161 (defun detect-mime-charset-region (start end)
162   "Return MIME charset for region between START and END."
163   (let ((charsets (find-charset-region start end)))
164     (or (charsets-to-mime-charset charsets)
165         (if default-mime-charset-detect-method-for-write
166             (funcall default-mime-charset-detect-method-for-write
167                      'region charsets start end)
168           default-mime-charset-for-write)
169         )))
170
171 (defun write-region-as-mime-charset (charset start end filename
172                                              &optional append visit lockname)
173   "Like `write-region', q.v., but encode by MIME CHARSET."
174   (let ((coding-system-for-write
175          (or (mime-charset-to-coding-system charset)
176              'binary)))
177     (write-region start end filename append visit lockname)))
178
179
180 ;;; @ end
181 ;;;
182
183 (provide 'mcs-20)
184
185 ;;; mcs-20.el ends here