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