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