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