1 ;;; mm-util.el --- Utility functions for MIME things
2 ;; Copyright (C) 1998, 1999, 2000 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 (eval-when-compile (require 'cl))
30 (defvar mm-mime-mule-charset-alist
32 (iso-8859-1 latin-iso8859-1)
33 (iso-8859-2 latin-iso8859-2)
34 (iso-8859-3 latin-iso8859-3)
35 (iso-8859-4 latin-iso8859-4)
36 (iso-8859-5 cyrillic-iso8859-5)
37 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
38 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
39 ;; charset is koi8-r, not iso-8859-5.
40 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
41 (iso-8859-6 arabic-iso8859-6)
42 (iso-8859-7 greek-iso8859-7)
43 (iso-8859-8 hebrew-iso8859-8)
44 (iso-8859-9 latin-iso8859-9)
45 (viscii vietnamese-viscii-lower)
46 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
47 (euc-kr korean-ksc5601)
48 (cn-gb-2312 chinese-gb2312)
49 (cn-big5 chinese-big5-1 chinese-big5-2)
51 (thai-tis620 thai-tis620)
52 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
53 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
54 latin-jisx0201 japanese-jisx0208-1978
55 chinese-gb2312 japanese-jisx0208
56 korean-ksc5601 japanese-jisx0212
58 (iso-2022-int-1 latin-iso8859-1 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 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
64 cyrillic-iso8859-5 greek-iso8859-7
65 latin-jisx0201 japanese-jisx0208-1978
66 chinese-gb2312 japanese-jisx0208
67 korean-ksc5601 japanese-jisx0212
68 chinese-cns11643-1 chinese-cns11643-2
69 chinese-cns11643-3 chinese-cns11643-4
70 chinese-cns11643-5 chinese-cns11643-6
72 (utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
73 "Alist of MIME-charset/MULE-charsets.")
78 (let ((nfunc (intern (format "mm-%s" (car elem)))))
79 (if (fboundp (car elem))
80 (defalias nfunc (car elem))
81 (defalias nfunc (cdr elem)))))
82 '((decode-coding-string . (lambda (s a) s))
83 (encode-coding-string . (lambda (s a) s))
84 (encode-coding-region . ignore)
85 (coding-system-list . ignore)
86 (decode-coding-region . ignore)
88 (device-type . ignore)
89 (coding-system-equal . equal)
90 (annotationp . ignore)
91 (set-buffer-file-coding-system . ignore)
93 . (lambda (charset int)
97 "Prompt the user for a coding system."
99 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
100 mm-mime-mule-charset-alist))))
107 (mapcar (lambda (e) (list (symbol-name (car e))))
108 mm-mime-mule-charset-alist)
112 (defalias 'mm-char-or-char-int-p
114 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
115 ((fboundp 'char-valid-p) 'char-valid-p)
118 (defvar mm-coding-system-list nil)
119 (defun mm-get-coding-system-list ()
120 "Get the coding system list."
121 (or mm-coding-system-list
122 (setq mm-coding-system-list (mm-coding-system-list))))
124 (defun mm-coding-system-p (sym)
125 "Return non-nil if SYM is a coding system."
126 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
127 (memq sym (mm-get-coding-system-list))))
129 (defvar mm-charset-synonym-alist
131 (gb2312 . cn-gb-2312)
132 ;; Windows-1252 is actually a superset of Latin-1. See also
133 ;; `gnus-article-dumbquotes-map'.
134 ,(unless (mm-coding-system-p 'windows-1252) ; should be defined eventually
135 '(windows-1252 . iso-8859-1))
137 "A mapping from invalid charset names to the real charset names.")
139 (defvar mm-binary-coding-system
141 ((mm-coding-system-p 'binary) 'binary)
142 ((mm-coding-system-p 'no-conversion) 'no-conversion)
144 "100% binary coding system.")
146 (defvar mm-text-coding-system
147 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
148 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
149 (and (mm-coding-system-p 'raw-text) 'raw-text))
150 mm-binary-coding-system)
151 "Text-safe coding system (For removing ^M).")
153 (defvar mm-text-coding-system-for-write nil
154 "Text coding system for write.")
156 (defvar mm-auto-save-coding-system
158 ((mm-coding-system-p 'emacs-mule)
159 (if (memq system-type '(windows-nt ms-dos ms-windows))
160 (if (mm-coding-system-p 'emacs-mule-dos)
161 'emacs-mule-dos mm-binary-coding-system)
163 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
164 (t mm-binary-coding-system))
165 "Coding system of auto save file.")
167 ;;; Internal variables:
171 (defun mm-mule-charset-to-mime-charset (charset)
172 "Return the MIME charset corresponding to the given Mule CHARSET."
173 (let ((alist mm-mime-mule-charset-alist)
176 (when (memq charset (cdar alist))
177 (setq out (caar alist)
182 (defun mm-charset-to-coding-system (charset &optional lbt)
183 "Return coding-system corresponding to CHARSET.
184 CHARSET is a symbol naming a MIME charset.
185 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
186 used as the line break code type of the coding system."
187 (when (stringp charset)
188 (setq charset (intern (downcase charset))))
190 (or (cdr (assq charset mm-charset-synonym-alist))
193 (setq charset (intern (format "%s-%s" charset lbt))))
195 ;; Running in a non-MULE environment.
196 ((null (mm-get-coding-system-list))
199 ((eq charset 'us-ascii)
201 ;; Check to see whether we can handle this charset. (This depends
202 ;; on there being some coding system matching each `mime-charset'
203 ;; coding sysytem property defined, as there should be.)
204 ((memq charset (mm-get-coding-system-list))
210 (if (fboundp 'subst-char-in-string)
211 (defsubst mm-replace-chars-in-string (string from to)
212 (subst-char-in-string from to string))
213 (defun mm-replace-chars-in-string (string from to)
214 "Replace characters in STRING from FROM to TO."
215 (let ((string (substring string 0)) ;Copy string.
216 (len (length string))
218 ;; Replace all occurrences of FROM with TO.
220 (when (= (aref string idx) from)
221 (aset string idx to))
225 (defsubst mm-enable-multibyte ()
226 "Set the multibyte flag of the current buffer.
227 Only do this if the default value of `enable-multibyte-characters' is
228 non-nil. This is a no-op in XEmacs."
229 (when (and (fboundp 'set-buffer-multibyte)
230 (boundp 'enable-multibyte-characters)
231 (default-value 'enable-multibyte-characters))
232 (set-buffer-multibyte t)))
234 (defsubst mm-disable-multibyte ()
235 "Unset the multibyte flag of in the current buffer.
236 This is a no-op in XEmacs."
237 (when (fboundp 'set-buffer-multibyte)
238 (set-buffer-multibyte nil)))
240 (defsubst mm-enable-multibyte-mule4 ()
241 "Enable multibyte in the current buffer.
242 Only used in Emacs Mule 4."
243 (when (and (fboundp 'set-buffer-multibyte)
244 (boundp 'enable-multibyte-characters)
245 (default-value 'enable-multibyte-characters)
246 (not (charsetp 'eight-bit-control)))
247 (set-buffer-multibyte t)))
249 (defsubst mm-disable-multibyte-mule4 ()
250 "Disable multibyte in the current buffer.
251 Only used in Emacs Mule 4."
252 (when (and (fboundp 'set-buffer-multibyte)
253 (not (charsetp 'eight-bit-control)))
254 (set-buffer-multibyte nil)))
256 (defun mm-preferred-coding-system (charset)
257 ;; A typo in some Emacs versions.
258 (or (get-charset-property charset 'prefered-coding-system)
259 (get-charset-property charset 'preferred-coding-system)))
261 (defun mm-charset-after (&optional pos)
262 "Return charset of a character in current buffer at position POS.
263 If POS is nil, it defauls to the current point.
264 If POS is out of range, the value is nil.
265 If the charset is `composition', return the actual one."
266 (let ((char (char-after pos)) charset)
267 (if (< (mm-char-int char) 128)
268 (setq charset 'ascii)
269 ;; charset-after is fake in some Emacsen.
270 (setq charset (and (fboundp 'char-charset) (char-charset char)))
271 (if (eq charset 'composition)
272 (let ((p (or pos (point))))
273 (cadr (find-charset-region p (1+ p))))
274 (if (and charset (not (memq charset '(ascii eight-bit-control
275 eight-bit-graphic))))
278 mail-parse-mule-charset ;; cached mule-charset
280 (setq mail-parse-mule-charset
281 (and (boundp 'current-language-environment)
284 (assoc current-language-environment
285 language-info-alist))))))
286 (if (or (not mail-parse-mule-charset)
287 (eq mail-parse-mule-charset 'ascii))
288 (setq mail-parse-mule-charset
289 (or (car (last (assq mail-parse-charset
290 mm-mime-mule-charset-alist)))
292 mail-parse-mule-charset)))))))
294 (defun mm-mime-charset (charset)
295 "Return the MIME charset corresponding to the given Mule CHARSET."
296 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
297 ;; This exists in Emacs 20.
299 (and (mm-preferred-coding-system charset)
301 (mm-preferred-coding-system charset) 'mime-charset))
302 (and (eq charset 'ascii)
304 (mm-mule-charset-to-mime-charset charset))
305 ;; This is for XEmacs.
306 (mm-mule-charset-to-mime-charset charset)))
308 (defun mm-delete-duplicates (list)
309 "Simple substitute for CL `delete-duplicates', testing with `equal'."
312 (setq head (car list))
313 (setq list (delete head list))
314 (setq result (cons head result)))
317 (defun mm-find-mime-charset-region (b e)
318 "Return the MIME charsets needed to encode the region between B and E."
319 (let ((charsets (mapcar 'mm-mime-charset
321 (mm-find-charset-region b e)))))
322 (when (memq 'iso-2022-jp-2 charsets)
323 (setq charsets (delq 'iso-2022-jp charsets)))
324 (setq charsets (mm-delete-duplicates charsets))
325 (if (and (> (length charsets) 1)
326 (fboundp 'find-coding-systems-region)
327 (memq 'utf-8 (find-coding-systems-region b e)))
331 (defsubst mm-multibyte-p ()
332 "Say whether multibyte is enabled."
333 (if (and (not (featurep 'xemacs))
334 (boundp 'enable-multibyte-characters))
335 enable-multibyte-characters
338 (defmacro mm-with-unibyte-buffer (&rest forms)
339 "Create a temporary buffer, and evaluate FORMS there like `progn'.
340 Use unibyte mode for this."
341 `(let (default-enable-multibyte-characters)
342 (with-temp-buffer ,@forms)))
343 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
344 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
346 (defmacro mm-with-unibyte-current-buffer (&rest forms)
347 "Evaluate FORMS with current current buffer temporarily made unibyte.
348 Also bind `default-enable-multibyte-characters' to nil.
349 Equivalent to `progn' in XEmacs"
350 (let ((multibyte (make-symbol "multibyte")))
351 `(if (fboundp 'set-buffer-multibyte)
352 (let ((,multibyte enable-multibyte-characters))
354 (let (default-enable-multibyte-characters)
355 (set-buffer-multibyte nil)
357 (set-buffer-multibyte ,multibyte)))
360 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
361 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
363 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
364 "Evaluate FORMS there like `progn' in current buffer.
366 (let ((multibyte (make-symbol "multibyte")))
367 `(if (or (featurep 'xemacs)
368 (not (fboundp 'set-buffer-multibyte))
369 (charsetp 'eight-bit-control)) ;; For Emacs Mule 4 only.
372 (let ((,multibyte (default-value 'enable-multibyte-characters)))
374 (let ((buffer-file-coding-system mm-binary-coding-system)
375 (coding-system-for-read mm-binary-coding-system)
376 (coding-system-for-write mm-binary-coding-system))
377 (set-buffer-multibyte nil)
378 (setq-default enable-multibyte-characters nil)
380 (setq-default enable-multibyte-characters ,multibyte)
381 (set-buffer-multibyte ,multibyte))))))
382 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
383 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
385 (defmacro mm-with-unibyte (&rest forms)
386 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
387 `(let (default-enable-multibyte-characters)
389 (put 'mm-with-unibyte 'lisp-indent-function 0)
390 (put 'mm-with-unibyte 'edebug-form-spec '(body))
392 (defun mm-find-charset-region (b e)
393 "Return a list of Emacs charsets in the region B to E."
395 ((and (mm-multibyte-p)
396 (fboundp 'find-charset-region))
397 ;; Remove composition since the base charsets have been included.
398 (delq 'composition (find-charset-region b e)))
400 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
403 (narrow-to-region b e)
404 (goto-char (point-min))
405 (skip-chars-forward "\0-\177")
410 (and (boundp 'current-language-environment)
411 (car (last (assq 'charset
412 (assoc current-language-environment
413 language-info-alist))))))
414 (if (eq charset 'ascii) (setq charset nil))
417 (car (last (assq mail-parse-charset
418 mm-mime-mule-charset-alist)))))
419 (list 'ascii (or charset 'latin-iso8859-1)))))))))
421 (if (fboundp 'shell-quote-argument)
422 (defalias 'mm-quote-arg 'shell-quote-argument)
423 (defun mm-quote-arg (arg)
424 "Return a version of ARG that is safe to evaluate in a shell."
425 (let ((pos 0) new-pos accum)
426 ;; *** bug: we don't handle newline characters properly
427 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
428 (push (substring arg pos new-pos) accum)
430 (push (list (aref arg new-pos)) accum)
431 (setq pos (1+ new-pos)))
434 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
436 (defun mm-auto-mode-alist ()
437 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
438 (let ((alist auto-mode-alist)
441 (when (listp (cdar alist))
442 (push (car alist) out))
446 (defvar mm-inhibit-file-name-handlers
448 "A list of handlers doing (un)compression (etc) thingies.")
450 (defun mm-insert-file-contents (filename &optional visit beg end replace
452 "Like `insert-file-contents', q.v., but only reads in the file.
453 A buffer may be modified in several ways after reading into the buffer due
454 to advanced Emacs features, such as file-name-handlers, format decoding,
455 find-file-hooks, etc.
456 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
457 This function ensures that none of these modifications will take place."
458 (let ((format-alist nil)
459 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
460 (default-major-mode 'fundamental-mode)
461 (enable-local-variables nil)
462 (after-insert-file-functions nil)
463 (enable-local-eval nil)
464 (find-file-hooks nil)
465 (inhibit-file-name-operation (if inhibit
466 'insert-file-contents
467 inhibit-file-name-operation))
468 (inhibit-file-name-handlers
470 (append mm-inhibit-file-name-handlers
471 inhibit-file-name-handlers)
472 inhibit-file-name-handlers)))
473 (insert-file-contents filename visit beg end replace)))
475 (defun mm-append-to-file (start end filename &optional codesys inhibit)
476 "Append the contents of the region to the end of file FILENAME.
477 When called from a function, expects three arguments,
478 START, END and FILENAME. START and END are buffer positions
479 saying what text to write.
480 Optional fourth argument specifies the coding system to use when
482 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
483 (let ((coding-system-for-write
484 (or codesys mm-text-coding-system-for-write
485 mm-text-coding-system))
486 (inhibit-file-name-operation (if inhibit
488 inhibit-file-name-operation))
489 (inhibit-file-name-handlers
491 (append mm-inhibit-file-name-handlers
492 inhibit-file-name-handlers)
493 inhibit-file-name-handlers)))
494 (append-to-file start end filename)))
496 (defun mm-write-region (start end filename &optional append visit lockname
497 coding-system inhibit)
499 "Like `write-region'.
500 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
501 (let ((coding-system-for-write
502 (or coding-system mm-text-coding-system-for-write
503 mm-text-coding-system))
504 (inhibit-file-name-operation (if inhibit
506 inhibit-file-name-operation))
507 (inhibit-file-name-handlers
509 (append mm-inhibit-file-name-handlers
510 inhibit-file-name-handlers)
511 inhibit-file-name-handlers)))
512 (write-region start end filename append visit lockname)))
516 ;;; mm-util.el ends here