7003d62e1949141524ed54fa1afc7887b9016495
[elisp/apel.git] / emu-20.el
1 ;;; emu-20.el --- emu API 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 emu.
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 'custom)
33 (eval-when-compile (require 'wid-edit))
34
35
36 ;;; @ without code-conversion
37 ;;;
38
39 (defmacro as-binary-process (&rest body)
40   `(let (selective-display      ; Disable ^M to nl translation.
41          (coding-system-for-read  'binary)
42          (coding-system-for-write 'binary))
43      ,@body))
44
45 (defmacro as-binary-input-file (&rest body)
46   `(let ((coding-system-for-read 'binary))
47      ,@body))
48
49 (defmacro as-binary-output-file (&rest body)
50   `(let ((coding-system-for-write 'binary))
51      ,@body))
52
53 (defun write-region-as-binary (start end filename
54                                      &optional append visit lockname)
55   "Like `write-region', q.v., but don't encode."
56   (let ((coding-system-for-write 'binary))
57     (write-region start end filename append visit lockname)
58     ))
59
60 (defun insert-file-contents-as-binary (filename
61                                        &optional visit beg end replace)
62   "Like `insert-file-contents', q.v., but don't code and format conversion.
63 Like `insert-file-contents-literary', but it allows find-file-hooks,
64 automatic uncompression, etc.
65
66 Namely this function ensures that only format decoding and character
67 code conversion will not take place."
68   (let ((coding-system-for-read 'binary)
69         format-alist)
70     (insert-file-contents filename visit beg end replace)
71     ))
72
73 (defun insert-file-contents-as-raw-text (filename
74                                          &optional visit beg end replace)
75   "Like `insert-file-contents', q.v., but don't code and format conversion.
76 Like `insert-file-contents-literary', but it allows find-file-hooks,
77 automatic uncompression, etc.
78 Like `insert-file-contents-as-binary', but it converts line-break
79 code."
80   (let ((coding-system-for-read 'raw-text)
81         format-alist)
82     (insert-file-contents filename visit beg end replace)
83     ))
84
85 (defun write-region-as-raw-text-CRLF (start end filename
86                                             &optional append visit lockname)
87   "Like `write-region', q.v., but write as network representation."
88   (let ((coding-system-for-write 'raw-text-dos))
89     (write-region start end filename append visit lockname)
90     ))
91
92
93 ;;; @@ Mule emulating aliases
94 ;;;
95 ;;; You should not use it.
96
97 (defconst *noconv* 'binary
98   "Coding-system for binary.
99 This constant is defined to emulate old MULE anything older than MULE
100 2.3.  It is obsolete, so don't use it.")
101
102
103 ;;; @ MIME charset
104 ;;;
105
106 (defcustom mime-charset-coding-system-alist
107   `,(let ((rest
108            '((us-ascii      . raw-text)
109              (gb2312        . cn-gb-2312)
110              (iso-2022-jp-2 . iso-2022-7bit-ss2)
111              (x-ctext       . ctext)
112              (unknown       . undecided)
113              (x-unknown     . undecided)
114              ))
115           dest)
116       (while rest
117         (let ((pair (car rest)))
118           (or (find-coding-system (car pair))
119               (setq dest (cons pair dest))
120               ))
121         (setq rest (cdr rest))
122         )
123       dest)
124   "Alist MIME CHARSET vs CODING-SYSTEM.
125 MIME CHARSET and CODING-SYSTEM must be symbol."
126   :group 'i18n
127   :type '(repeat (cons symbol coding-system)))
128
129 (defsubst mime-charset-to-coding-system (charset &optional lbt)
130   "Return coding-system corresponding with CHARSET.
131 CHARSET is a symbol whose name is MIME charset.
132 If optional argument LBT (`CRLF', `LF', `CR', `unix', `dos' or `mac')
133 is specified, it is used as line break code type of coding-system."
134   (if (stringp charset)
135       (setq charset (intern (downcase charset)))
136     )
137   (let ((ret (assq charset mime-charset-coding-system-alist)))
138     (if ret
139         (setq charset (cdr ret))
140       ))
141   (if lbt
142       (setq charset (intern (format "%s-%s" charset
143                                     (cond ((eq lbt 'CRLF) 'dos)
144                                           ((eq lbt 'LF) 'unix)
145                                           ((eq lbt 'CR) 'mac)
146                                           (t lbt)))))
147     )
148   (if (find-coding-system charset)
149       charset))
150
151 (defsubst mime-charset-list ()
152   "Return a list of all existing MIME-charset."
153   (nconc (mapcar (function car) mime-charset-coding-system-alist)
154          (coding-system-list)))
155
156
157 (defvar widget-mime-charset-prompt-value-history nil
158   "History of input to `widget-mime-charset-prompt-value'.")
159
160 (define-widget 'mime-charset 'coding-system
161   "A mime-charset."
162   :format "%{%t%}: %v"
163   :tag "MIME-charset"
164   :prompt-history 'widget-mime-charset-prompt-value-history
165   :prompt-value 'widget-mime-charset-prompt-value
166   :action 'widget-mime-charset-action)
167
168 (defun widget-mime-charset-prompt-value (widget prompt value unbound)
169   ;; Read mime-charset from minibuffer.
170   (intern
171    (completing-read (format "%s (default %s) " prompt value)
172                     (mapcar (function
173                              (lambda (sym)
174                                (list (symbol-name sym))
175                                ))
176                             (mime-charset-list)))))
177
178 (defun widget-mime-charset-action (widget &optional event)
179   ;; Read a mime-charset from the minibuffer.
180   (let ((answer
181          (widget-mime-charset-prompt-value
182           widget
183           (widget-apply widget :menu-tag-get)
184           (widget-value widget)
185           t)))
186     (widget-value-set widget answer)
187     (widget-apply widget :notify widget event)
188     (widget-setup)))
189
190 (defcustom default-mime-charset 'x-ctext
191   "Default value of MIME-charset.
192 It is used when MIME-charset is not specified.
193 It must be symbol."
194   :group 'i18n
195   :type 'mime-charset)
196
197 (defsubst detect-mime-charset-region (start end)
198   "Return MIME charset for region between START and END."
199   (charsets-to-mime-charset (find-charset-region start end)))
200
201 (defun write-region-as-mime-charset (charset start end filename
202                                              &optional append visit lockname)
203   "Like `write-region', q.v., but encode by MIME CHARSET."
204   (let ((coding-system-for-write
205          (or (mime-charset-to-coding-system charset)
206              'binary)))
207     (write-region start end filename append visit lockname)
208     ))
209
210
211 ;;; @ end
212 ;;;
213
214 (provide 'emu-20)
215
216 ;;; emu-20.el ends here