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