1 ;;; mm-util.el --- Utility functions for MIME things
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
27 (defvar mm-mime-mule-charset-alist
29 (iso-8859-1 latin-iso8859-1)
30 (iso-8859-2 latin-iso8859-2)
31 (iso-8859-3 latin-iso8859-3)
32 (iso-8859-4 latin-iso8859-4)
33 (iso-8859-5 cyrillic-iso8859-5)
34 (koi8-r cyrillic-iso8859-5)
35 (iso-8859-6 arabic-iso8859-6)
36 (iso-8859-7 greek-iso8859-7)
37 (iso-8859-8 hebrew-iso8859-8)
38 (iso-8859-9 latin-iso8859-9)
39 (viscii vietnamese-viscii-lower)
40 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
41 (euc-kr korean-ksc5601)
42 (cn-gb-2312 chinese-gb2312)
43 (cn-big5 chinese-big5-1 chinese-big5-2)
45 (thai-tis620 thai-tis620)
46 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
47 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
48 latin-jisx0201 japanese-jisx0208-1978
49 chinese-gb2312 japanese-jisx0208
50 korean-ksc5601 japanese-jisx0212
52 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
53 latin-jisx0201 japanese-jisx0208-1978
54 chinese-gb2312 japanese-jisx0208
55 korean-ksc5601 japanese-jisx0212
56 chinese-cns11643-1 chinese-cns11643-2)
57 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
58 cyrillic-iso8859-5 greek-iso8859-7
59 latin-jisx0201 japanese-jisx0208-1978
60 chinese-gb2312 japanese-jisx0208
61 korean-ksc5601 japanese-jisx0212
62 chinese-cns11643-1 chinese-cns11643-2
63 chinese-cns11643-3 chinese-cns11643-4
64 chinese-cns11643-5 chinese-cns11643-6
66 "Alist of MIME-charset/MULE-charsets.")
72 (let ((nfunc (intern (format "mm-%s" (car elem)))))
73 (if (fboundp (car elem))
74 (defalias nfunc (car elem))
75 (defalias nfunc (cdr elem)))))
76 '((decode-coding-string . (lambda (s a) s))
77 (encode-coding-string . (lambda (s a) s))
78 (encode-coding-region . ignore)
79 (coding-system-list . ignore)
80 (decode-coding-region . ignore)
82 (device-type . ignore)
83 (coding-system-equal . equal)
84 (annotationp . ignore)
85 (set-buffer-file-coding-system . ignore)
87 . (lambda (charset int)
91 "Prompt the user for a coding system."
93 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
94 mm-mime-mule-charset-alist)))))))
96 (defvar mm-coding-system-list nil)
97 (defun mm-get-coding-system-list ()
98 "Get the coding system list."
99 (or mm-coding-system-list
100 (setq mm-coding-system-list (mm-coding-system-list))))
102 (defvar mm-charset-synonym-alist
104 (gb2312 . cn-gb-2312)
106 "A mapping from invalid charset names to the real charset names.")
108 (defun mm-coding-system-p (sym)
109 "Return non-nil if SYM is a coding system."
110 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
111 (memq sym (mm-get-coding-system-list))))
113 (defvar mm-binary-coding-system
115 ((mm-coding-system-p 'binary) 'binary)
116 ((mm-coding-system-p 'no-conversion) 'no-conversion)
118 "100% binary coding system.")
120 (defvar mm-text-coding-system
121 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
122 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
123 (and (mm-coding-system-p 'raw-text) 'raw-text))
124 mm-binary-coding-system)
125 "Text-safe coding system (For removing ^M).")
127 (defvar mm-text-coding-system-for-write nil
128 "Text coding system for write.")
130 (defvar mm-auto-save-coding-system
132 ((mm-coding-system-p 'emacs-mule)
133 (if (memq system-type '(windows-nt ms-dos ms-windows))
134 (if (mm-coding-system-p 'emacs-mule-dos)
135 'emacs-mule-dos mm-binary-coding-system)
137 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
138 (t mm-binary-coding-system))
139 "Coding system of auto save file.")
141 ;;; Internal variables:
145 (defun mm-mule-charset-to-mime-charset (charset)
146 "Return the MIME charset corresponding to MULE CHARSET."
147 (let ((alist mm-mime-mule-charset-alist)
150 (when (memq charset (cdar alist))
151 (setq out (caar alist)
156 (defun mm-charset-to-coding-system (charset &optional lbt)
157 "Return coding-system corresponding to CHARSET.
158 CHARSET is a symbol naming a MIME charset.
159 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
160 used as the line break code type of the coding system."
161 (when (stringp charset)
162 (setq charset (intern (downcase charset))))
164 (or (cdr (assq charset mm-charset-synonym-alist))
167 (setq charset (intern (format "%s-%s" charset lbt))))
169 ;; Running in a non-MULE environment.
170 ((null (mm-get-coding-system-list))
173 ((eq charset 'us-ascii)
175 ;; Check to see whether we can handle this charset.
176 ((memq charset (mm-get-coding-system-list))
182 (defun mm-replace-chars-in-string (string from to)
183 "Replace characters in STRING from FROM to TO."
184 (let ((string (substring string 0)) ;Copy string.
185 (len (length string))
187 ;; Replace all occurrences of FROM with TO.
189 (when (= (aref string idx) from)
190 (aset string idx to))
194 (defsubst mm-enable-multibyte ()
195 "Enable multibyte in the current buffer."
196 (when (and (fboundp 'set-buffer-multibyte)
197 (boundp 'enable-multibyte-characters)
198 (default-value 'enable-multibyte-characters))
199 (set-buffer-multibyte t)))
201 (defsubst mm-disable-multibyte ()
202 "Disable multibyte in the current buffer."
203 (when (fboundp 'set-buffer-multibyte)
204 (set-buffer-multibyte nil)))
206 (defun mm-preferred-coding-system (charset)
207 ;; A typo in some Emacs versions.
208 (or (get-charset-property charset 'prefered-coding-system)
209 (get-charset-property charset 'preferred-coding-system)))
211 (defun mm-charset-after (&optional pos)
212 "Return charset of a character in current buffer at position POS.
213 If POS is nil, it defauls to the current point.
214 If POS is out of range, the value is nil.
215 If the charset is `composition', return the actual one."
216 (let ((charset (if (fboundp 'charset-after)
218 (char-charset (char-after pos)))))
219 (if (eq charset 'composition)
220 (let ((p (or pos (point))))
221 (cadr (find-charset-region p (1+ p))))
224 (defun mm-mime-charset (charset)
225 "Return the MIME charset corresponding to the MULE CHARSET."
226 (if (fboundp 'coding-system-get)
227 ;; This exists in Emacs 20.
229 (and (mm-preferred-coding-system charset)
231 (mm-preferred-coding-system charset) 'mime-charset))
232 (and (eq charset 'ascii)
234 (mm-preferred-coding-system charset)
235 (mm-mule-charset-to-mime-charset charset))
236 ;; This is for XEmacs.
237 (mm-mule-charset-to-mime-charset charset)))
239 (defun mm-delete-duplicates (list)
240 "Simple substitute for CL `delete-duplicates', testing with `equal'."
243 (setq head (car list))
244 (setq list (delete head list))
245 (setq result (cons head result)))
248 (defun mm-find-mime-charset-region (b e)
249 "Return the MIME charsets needed to encode the region between B and E."
250 (let ((charsets (mapcar 'mm-mime-charset
252 (mm-find-charset-region b e)))))
253 (when (memq 'iso-2022-jp-2 charsets)
254 (setq charsets (delq 'iso-2022-jp charsets)))
255 (setq charsets (mm-delete-duplicates charsets))
256 (if (and (> (length charsets) 1)
257 (fboundp 'find-coding-systems-region)
258 (memq 'utf-8 (find-coding-systems-region b e)))
262 (defsubst mm-multibyte-p ()
263 "Say whether multibyte is enabled."
264 (or (string-match "XEmacs\\|Lucid" emacs-version)
265 (and (boundp 'enable-multibyte-characters)
266 enable-multibyte-characters)))
268 (defmacro mm-with-unibyte-buffer (&rest forms)
269 "Create a temporary buffer, and evaluate FORMS there like `progn'.
270 See also `with-temp-file' and `with-output-to-string'."
271 (let ((temp-buffer (make-symbol "temp-buffer"))
272 (multibyte (make-symbol "multibyte")))
273 `(if (or (string-match "XEmacs\\|Lucid" emacs-version)
274 (not (boundp 'enable-multibyte-characters)))
275 (with-temp-buffer ,@forms)
276 (let ((,multibyte (default-value 'enable-multibyte-characters))
280 (setq-default enable-multibyte-characters nil)
282 (get-buffer-create (generate-new-buffer-name " *temp*")))
284 (with-current-buffer ,temp-buffer
285 (let ((buffer-file-coding-system mm-binary-coding-system)
286 (coding-system-for-read mm-binary-coding-system)
287 (coding-system-for-write mm-binary-coding-system))
289 (and (buffer-name ,temp-buffer)
290 (kill-buffer ,temp-buffer))))
291 (setq-default enable-multibyte-characters ,multibyte))))))
292 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
293 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
295 (defmacro mm-with-unibyte-current-buffer (&rest forms)
296 "Evaluate FORMS there like `progn' in current buffer."
297 (let ((multibyte (make-symbol "multibyte")))
298 `(if (or (string-match "XEmacs\\|Lucid" emacs-version)
299 (not (fboundp 'set-buffer-multibyte)))
302 (let ((,multibyte (default-value 'enable-multibyte-characters)))
304 (let ((buffer-file-coding-system mm-binary-coding-system)
305 (coding-system-for-read mm-binary-coding-system)
306 (coding-system-for-write mm-binary-coding-system))
307 (set-buffer-multibyte nil)
309 (set-buffer-multibyte ,multibyte))))))
310 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
311 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
313 (defmacro mm-with-unibyte (&rest forms)
314 "Set default `enable-multibyte-characters' to `nil', eval the FORMS."
315 (let ((multibyte (make-symbol "multibyte")))
316 `(if (or (string-match "XEmacs\\|Lucid" emacs-version)
317 (not (boundp 'enable-multibyte-characters)))
319 (let ((,multibyte (default-value 'enable-multibyte-characters)))
322 (setq-default enable-multibyte-characters nil)
324 (setq-default enable-multibyte-characters ,multibyte))))))
325 (put 'mm-with-unibyte 'lisp-indent-function 0)
326 (put 'mm-with-unibyte 'edebug-form-spec '(body))
328 (defun mm-find-charset-region (b e)
329 "Return a list of charsets in the region."
331 ((and (mm-multibyte-p)
332 (fboundp 'find-charset-region))
333 ;; Remove composition since the base charsets have been included.
334 (delq 'composition (find-charset-region b e)))
335 ((not (boundp 'current-language-environment))
338 (narrow-to-region b e)
339 (goto-char (point-min))
340 (skip-chars-forward "\0-\177")
343 (delq nil (list 'ascii
344 (or (car (last (assq mail-parse-charset
345 mm-mime-mule-charset-alist)))
346 'latin-iso8859-1)))))))
348 ;; We are in a unibyte buffer, so we futz around a bit.
351 (narrow-to-region b e)
352 (goto-char (point-min))
353 (let ((entry (assoc current-language-environment
354 language-info-alist)))
355 (skip-chars-forward "\0-\177")
358 (delq nil (list 'ascii
359 (or (car (last (assq 'charset entry)))
360 'latin-iso8859-1))))))))))
362 (defun mm-read-charset (prompt)
367 (mapcar (lambda (e) (list (symbol-name (car e))))
368 mm-mime-mule-charset-alist)
371 (defun mm-quote-arg (arg)
372 "Return a version of ARG that is safe to evaluate in a shell."
373 (let ((pos 0) new-pos accum)
374 ;; *** bug: we don't handle newline characters properly
375 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
376 (push (substring arg pos new-pos) accum)
378 (push (list (aref arg new-pos)) accum)
379 (setq pos (1+ new-pos)))
382 (apply 'concat (nconc (nreverse accum) (list (substring arg pos)))))))
384 (defun mm-auto-mode-alist ()
385 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
386 (let ((alist auto-mode-alist)
389 (when (listp (cdar alist))
390 (push (car alist) out))
394 (defvar mm-inhibit-file-name-handlers
396 "A list of handlers doing (un)compression (etc) thingies.")
398 (defun mm-insert-file-contents (filename &optional visit beg end replace
400 "Like `insert-file-contents', q.v., but only reads in the file.
401 A buffer may be modified in several ways after reading into the buffer due
402 to advanced Emacs features, such as file-name-handlers, format decoding,
403 find-file-hooks, etc.
404 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
405 This function ensures that none of these modifications will take place."
406 (let ((format-alist nil)
407 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
408 (default-major-mode 'fundamental-mode)
409 (enable-local-variables nil)
410 (after-insert-file-functions nil)
411 (enable-local-eval nil)
412 (find-file-hooks nil)
413 (inhibit-file-name-operation (if inhibit
414 'insert-file-contents
415 inhibit-file-name-operation))
416 (inhibit-file-name-handlers
418 (append mm-inhibit-file-name-handlers
419 inhibit-file-name-handlers)
420 inhibit-file-name-handlers)))
421 (insert-file-contents filename visit beg end replace)))
423 (defun mm-append-to-file (start end filename &optional codesys inhibit)
424 "Append the contents of the region to the end of file FILENAME.
425 When called from a function, expects three arguments,
426 START, END and FILENAME. START and END are buffer positions
427 saying what text to write.
428 Optional fourth argument specifies the coding system to use when
430 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
431 (let ((coding-system-for-write
432 (or codesys mm-text-coding-system-for-write
433 mm-text-coding-system))
434 (inhibit-file-name-operation (if inhibit
436 inhibit-file-name-operation))
437 (inhibit-file-name-handlers
439 (append mm-inhibit-file-name-handlers
440 inhibit-file-name-handlers)
441 inhibit-file-name-handlers)))
442 (append-to-file start end filename)))
444 (defun mm-write-region (start end filename &optional append visit lockname
445 coding-system inhibit)
447 "Like `write-region'.
448 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
449 (let ((coding-system-for-write
450 (or coding-system mm-text-coding-system-for-write
451 mm-text-coding-system))
452 (inhibit-file-name-operation (if inhibit
454 inhibit-file-name-operation))
455 (inhibit-file-name-handlers
457 (append mm-inhibit-file-name-handlers
458 inhibit-file-name-handlers)
459 inhibit-file-name-handlers)))
460 (write-region start end filename append visit lockname)))
464 ;;; mm-util.el ends here