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