update.
[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 ;;; @ binary access
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 code conversion."
56   (let ((coding-system-for-read 'binary))
57     (write-region start end filename append visit lockname)
58     ))
59
60
61 ;;; @@ Mule emulating aliases
62 ;;;
63 ;;; You should not use it.
64
65 (defconst *noconv* 'binary
66   "Coding-system for binary.
67 This constant is defined to emulate old MULE anything older than MULE
68 2.3.  It is obsolete, so don't use it.")
69
70
71 ;;; @ MIME charset
72 ;;;
73
74 (defcustom mime-charset-coding-system-alist
75   `,(let ((rest
76            '((us-ascii      . iso-8859-1)
77              (gb2312        . cn-gb-2312)
78              (iso-2022-jp-2 . iso-2022-7bit-ss2)
79              (x-ctext       . ctext)
80              ))
81           dest)
82       (while rest
83         (let ((pair (car rest)))
84           (or (find-coding-system (car pair))
85               (setq dest (cons pair dest))
86               ))
87         (setq rest (cdr rest))
88         )
89       dest)
90   "Alist MIME CHARSET vs CODING-SYSTEM.
91 MIME CHARSET and CODING-SYSTEM must be symbol."
92   :group 'i18n
93   :type '(repeat (cons symbol coding-system)))
94
95 (defsubst mime-charset-to-coding-system (charset &optional lbt)
96   "Return coding-system corresponding with CHARSET.
97 CHARSET is a symbol whose name is MIME charset.
98 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
99 used as line break code type of coding-system."
100   (if (stringp charset)
101       (setq charset (intern (downcase charset)))
102     )
103   (let ((ret (assq charset mime-charset-coding-system-alist)))
104     (if ret
105         (setq charset (cdr ret))
106       ))
107   (if lbt
108       (setq charset (intern (format "%s-%s" charset lbt)))
109     )
110   (if (find-coding-system charset)
111       charset))
112
113 (defsubst mime-charset-list ()
114   "Return a list of all existing MIME-charset."
115   (nconc (mapcar (function car) mime-charset-coding-system-alist)
116          (coding-system-list)))
117
118
119 (defvar widget-mime-charset-prompt-value-history nil
120   "History of input to `widget-mime-charset-prompt-value'.")
121
122 (define-widget 'mime-charset 'coding-system
123   "A mime-charset."
124   :format "%{%t%}: %v"
125   :tag "MIME-charset"
126   :prompt-history 'widget-mime-charset-prompt-value-history
127   :prompt-value 'widget-mime-charset-prompt-value
128   :action 'widget-mime-charset-action)
129
130 (defun widget-mime-charset-prompt-value (widget prompt value unbound)
131   ;; Read mime-charset from minibuffer.
132   (intern
133    (completing-read (format "%s (default %s) " prompt value)
134                     (mapcar (function
135                              (lambda (sym)
136                                (list (symbol-name sym))
137                                ))
138                             (mime-charset-list)))))
139
140 (defun widget-mime-charset-action (widget &optional event)
141   ;; Read a mime-charset from the minibuffer.
142   (let ((answer
143          (widget-mime-charset-prompt-value
144           widget
145           (widget-apply widget :menu-tag-get)
146           (widget-value widget)
147           t)))
148     (widget-value-set widget answer)
149     (widget-apply widget :notify widget event)
150     (widget-setup)))
151
152 (defcustom default-mime-charset 'x-ctext
153   "Default value of MIME-charset.
154 It is used when MIME-charset is not specified.
155 It must be symbol."
156   :group 'i18n
157   :type 'mime-charset)
158
159 (defsubst detect-mime-charset-region (start end)
160   "Return MIME charset for region between START and END."
161   (charsets-to-mime-charset (find-charset-region start end)))
162
163
164 ;;; @ end
165 ;;;
166
167 (provide 'emu-20)
168
169 ;;; emu-20.el ends here