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