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