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 'static))
29 (eval-when-compile (require 'cl))
32 (defvar mm-mime-mule-charset-alist
34 (iso-8859-1 latin-iso8859-1)
35 (iso-8859-2 latin-iso8859-2)
36 (iso-8859-3 latin-iso8859-3)
37 (iso-8859-4 latin-iso8859-4)
38 (iso-8859-5 cyrillic-iso8859-5)
39 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
40 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
41 ;; charset is koi8-r, not iso-8859-5.
42 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
43 (iso-8859-6 arabic-iso8859-6)
44 (iso-8859-7 greek-iso8859-7)
45 (iso-8859-8 hebrew-iso8859-8)
46 (iso-8859-9 latin-iso8859-9)
47 (viscii vietnamese-viscii-lower)
48 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
49 (euc-kr korean-ksc5601)
50 (cn-gb-2312 chinese-gb2312)
51 (cn-big5 chinese-big5-1 chinese-big5-2)
53 (thai-tis620 thai-tis620)
54 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
55 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
56 latin-jisx0201 japanese-jisx0208-1978
57 chinese-gb2312 japanese-jisx0208
58 korean-ksc5601 japanese-jisx0212
60 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
61 latin-jisx0201 japanese-jisx0208-1978
62 chinese-gb2312 japanese-jisx0208
63 korean-ksc5601 japanese-jisx0212
64 chinese-cns11643-1 chinese-cns11643-2)
65 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
66 cyrillic-iso8859-5 greek-iso8859-7
67 latin-jisx0201 japanese-jisx0208-1978
68 chinese-gb2312 japanese-jisx0208
69 korean-ksc5601 japanese-jisx0212
70 chinese-cns11643-1 chinese-cns11643-2
71 chinese-cns11643-3 chinese-cns11643-4
72 chinese-cns11643-5 chinese-cns11643-6
74 (utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
75 "Alist of MIME-charset/MULE-charsets.")
80 (let ((nfunc (intern (format "mm-%s" (car elem)))))
81 (if (fboundp (car elem))
82 (defalias nfunc (car elem))
83 (defalias nfunc (cdr elem)))))
84 '((decode-coding-string . (lambda (s a) s))
85 (encode-coding-string . (lambda (s a) s))
86 (encode-coding-region . ignore)
87 (coding-system-list . ignore)
88 (decode-coding-region . ignore)
90 (device-type . ignore)
91 (coding-system-equal . equal)
92 (annotationp . ignore)
93 (set-buffer-file-coding-system . ignore)
95 . (lambda (charset int)
99 "Prompt the user for a coding system."
101 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
102 mm-mime-mule-charset-alist))))
109 (mapcar (lambda (e) (list (symbol-name (car e))))
110 mm-mime-mule-charset-alist)
112 (subst-char-in-string
113 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
114 "Replace characters in STRING from FROM to TO."
115 (let ((string (substring string 0)) ;Copy string.
116 (len (length string))
118 ;; Replace all occurrences of FROM with TO.
120 (when (= (aref string idx) from)
121 (aset string idx to))
127 (defalias 'mm-char-or-char-int-p
129 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
130 ((fboundp 'char-valid-p) 'char-valid-p)
133 (defvar mm-coding-system-list nil)
134 (defun mm-get-coding-system-list ()
135 "Get the coding system list."
136 (or mm-coding-system-list
137 (setq mm-coding-system-list (mm-coding-system-list))))
139 (defvar mm-charset-synonym-alist
141 (gb2312 . cn-gb-2312)
144 "A mapping from invalid charset names to the real charset names.")
146 (defun mm-coding-system-p (sym)
147 "Return non-nil if SYM is a coding system."
148 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
149 (memq sym (mm-get-coding-system-list))))
151 (defvar mm-binary-coding-system
153 ((mm-coding-system-p 'binary) 'binary)
154 ((mm-coding-system-p 'no-conversion) 'no-conversion)
156 "100% binary coding system.")
158 (defvar mm-text-coding-system
159 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
160 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
161 (and (mm-coding-system-p 'raw-text) 'raw-text))
162 mm-binary-coding-system)
163 "Text-safe coding system (For removing ^M).")
165 (defvar mm-text-coding-system-for-write nil
166 "Text coding system for write.")
168 (defvar mm-auto-save-coding-system
170 ((mm-coding-system-p 'emacs-mule)
171 (if (memq system-type '(windows-nt ms-dos ms-windows))
172 (if (mm-coding-system-p 'emacs-mule-dos)
173 'emacs-mule-dos mm-binary-coding-system)
175 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
176 (t mm-binary-coding-system))
177 "Coding system of auto save file.")
179 ;;; Internal variables:
183 (defun mm-mule-charset-to-mime-charset (charset)
184 "Return the MIME charset corresponding to MULE CHARSET."
185 (let ((alist mm-mime-mule-charset-alist)
188 (when (memq charset (cdar alist))
189 (setq out (caar alist)
194 (defun mm-charset-to-coding-system (charset &optional lbt)
195 "Return coding-system corresponding to CHARSET.
196 CHARSET is a symbol naming a MIME charset.
197 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
198 used as the line break code type of the coding system."
199 (when (stringp charset)
200 (setq charset (intern (downcase charset))))
202 (or (cdr (assq charset mm-charset-synonym-alist))
205 (setq charset (intern (format "%s-%s" charset lbt))))
207 ;; Running in a non-MULE environment.
208 ((null (mm-get-coding-system-list))
211 ((eq charset 'us-ascii)
213 ;; Check to see whether we can handle this charset.
214 ((memq charset (mm-get-coding-system-list))
220 (defsubst mm-replace-chars-in-string (string from to)
221 (mm-subst-char-in-string from to string))
223 (defsubst mm-enable-multibyte ()
224 "Enable multibyte in the current buffer."
225 (when (and (fboundp 'set-buffer-multibyte)
226 (boundp 'enable-multibyte-characters)
227 (default-value 'enable-multibyte-characters))
228 (set-buffer-multibyte t)))
230 (defsubst mm-disable-multibyte ()
231 "Disable multibyte in the current buffer."
232 (when (fboundp 'set-buffer-multibyte)
233 (set-buffer-multibyte nil)))
235 (defsubst mm-enable-multibyte-mule4 ()
236 "Enable multibyte in the current buffer.
237 Only used in Emacs Mule 4."
238 (when (and (fboundp 'set-buffer-multibyte)
239 (boundp 'enable-multibyte-characters)
240 (default-value 'enable-multibyte-characters)
241 (not (charsetp 'eight-bit-control)))
242 (set-buffer-multibyte t)))
244 (defsubst mm-disable-multibyte-mule4 ()
245 "Disable multibyte in the current buffer.
246 Only used in Emacs Mule 4."
247 (when (and (fboundp 'set-buffer-multibyte)
248 (not (charsetp 'eight-bit-control)))
249 (set-buffer-multibyte nil)))
251 (defun mm-preferred-coding-system (charset)
252 ;; A typo in some Emacs versions.
253 (or (get-charset-property charset 'prefered-coding-system)
254 (get-charset-property charset 'preferred-coding-system)))
256 (defun mm-charset-after (&optional pos)
257 "Return charset of a character in current buffer at position POS.
258 If POS is nil, it defauls to the current point.
259 If POS is out of range, the value is nil.
260 If the charset is `composition', return the actual one."
261 (let ((char (char-after pos)) charset)
262 (if (< (mm-char-int char) 128)
263 (setq charset 'ascii)
264 ;; charset-after is fake in some Emacsen.
265 (setq charset (and (fboundp 'char-charset) (char-charset char)))
266 (if (eq charset 'composition)
267 (let ((p (or pos (point))))
268 (cadr (find-charset-region p (1+ p))))
269 (if (and charset (not (memq charset '(ascii eight-bit-control
270 eight-bit-graphic))))
273 mail-parse-mule-charset ;; cached mule-charset
275 (setq mail-parse-mule-charset
276 (and (boundp 'current-language-environment)
279 (assoc current-language-environment
280 language-info-alist))))))
281 (if (or (not mail-parse-mule-charset)
282 (eq mail-parse-mule-charset 'ascii))
283 (setq mail-parse-mule-charset
284 (or (car (last (assq mail-parse-charset
285 mm-mime-mule-charset-alist)))
287 mail-parse-mule-charset)))))))
289 (defun mm-mime-charset (charset)
290 "Return the MIME charset corresponding to the MULE CHARSET."
291 (if (eq charset 'unknown)
292 (error "8-bit characters are found in the message, please specify charset."))
293 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
294 ;; This exists in Emacs 20.
296 (and (mm-preferred-coding-system charset)
298 (mm-preferred-coding-system charset) 'mime-charset))
299 (and (eq charset 'ascii)
301 (mm-preferred-coding-system charset)
302 (mm-mule-charset-to-mime-charset charset))
303 ;; This is for XEmacs.
304 (mm-mule-charset-to-mime-charset charset)))
306 (defun mm-delete-duplicates (list)
307 "Simple substitute for CL `delete-duplicates', testing with `equal'."
310 (setq head (car list))
311 (setq list (delete head list))
312 (setq result (cons head result)))
315 (defun mm-find-mime-charset-region (b e)
316 "Return the MIME charsets needed to encode the region between B and E."
317 (let ((charsets (mapcar 'mm-mime-charset
319 (mm-find-charset-region b e)))))
320 (when (memq 'iso-2022-jp-2 charsets)
321 (setq charsets (delq 'iso-2022-jp charsets)))
322 (setq charsets (mm-delete-duplicates charsets))
323 (if (and (> (length charsets) 1)
324 (fboundp 'find-coding-systems-region)
325 (memq 'utf-8 (find-coding-systems-region b e)))
329 (defsubst mm-multibyte-p ()
330 "Say whether multibyte is enabled."
331 (if (and (not (featurep 'xemacs))
332 (boundp 'enable-multibyte-characters))
333 enable-multibyte-characters
336 (defmacro mm-with-unibyte-buffer (&rest forms)
337 "Create a temporary buffer, and evaluate FORMS there like `progn'.
338 See also `with-temp-file' and `with-output-to-string'."
339 (let ((temp-buffer (make-symbol "temp-buffer"))
340 (multibyte (make-symbol "multibyte")))
341 `(if (or (featurep 'xemacs)
342 (not (boundp 'enable-multibyte-characters)))
343 (with-temp-buffer ,@forms)
344 (let ((,multibyte (default-value 'enable-multibyte-characters))
348 (setq-default enable-multibyte-characters nil)
350 (get-buffer-create (generate-new-buffer-name " *temp*")))
352 (with-current-buffer ,temp-buffer
353 (let ((buffer-file-coding-system mm-binary-coding-system)
354 (coding-system-for-read mm-binary-coding-system)
355 (coding-system-for-write mm-binary-coding-system))
357 (and (buffer-name ,temp-buffer)
358 (kill-buffer ,temp-buffer))))
359 (setq-default enable-multibyte-characters ,multibyte))))))
360 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
361 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
363 (defmacro mm-with-unibyte-current-buffer (&rest forms)
364 "Evaluate FORMS there like `progn' in current buffer."
365 (let ((multibyte (make-symbol "multibyte")))
366 `(if (or (featurep 'xemacs)
367 (not (fboundp 'set-buffer-multibyte)))
370 (let ((,multibyte (default-value 'enable-multibyte-characters)))
372 (let ((buffer-file-coding-system mm-binary-coding-system)
373 (coding-system-for-read mm-binary-coding-system)
374 (coding-system-for-write mm-binary-coding-system))
375 (set-buffer-multibyte nil)
376 (setq-default enable-multibyte-characters nil)
378 (setq-default enable-multibyte-characters ,multibyte)
379 (set-buffer-multibyte ,multibyte))))))
380 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
381 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
383 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
384 "Evaluate FORMS there like `progn' in current buffer.
386 (let ((multibyte (make-symbol "multibyte")))
387 `(if (or (featurep 'xemacs)
388 (not (fboundp 'set-buffer-multibyte))
389 (charsetp 'eight-bit-control)) ;; For Emacs Mule 4 only.
392 (let ((,multibyte (default-value 'enable-multibyte-characters)))
394 (let ((buffer-file-coding-system mm-binary-coding-system)
395 (coding-system-for-read mm-binary-coding-system)
396 (coding-system-for-write mm-binary-coding-system))
397 (set-buffer-multibyte nil)
398 (setq-default enable-multibyte-characters nil)
400 (setq-default enable-multibyte-characters ,multibyte)
401 (set-buffer-multibyte ,multibyte))))))
402 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
403 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
405 (defmacro mm-with-unibyte (&rest forms)
406 "Set default `enable-multibyte-characters' to `nil', eval the FORMS."
407 (let ((multibyte (make-symbol "multibyte")))
408 `(if (or (featurep 'xemacs)
409 (not (boundp 'enable-multibyte-characters)))
411 (let ((,multibyte (default-value 'enable-multibyte-characters)))
414 (setq-default enable-multibyte-characters nil)
416 (setq-default enable-multibyte-characters ,multibyte))))))
417 (put 'mm-with-unibyte 'lisp-indent-function 0)
418 (put 'mm-with-unibyte 'edebug-form-spec '(body))
420 (defun mm-find-charset-region (b e)
421 "Return a list of charsets in the region."
423 ((and (mm-multibyte-p)
424 (fboundp 'find-charset-region))
425 ;; Remove composition since the base charsets have been included.
426 ;; Remove eight-bit-*, treat them as ascii.
427 (let ((css (find-charset-region b e)))
428 (mapcar (lambda (cs) (setq css (delq cs css)))
429 '(composition eight-bit-control eight-bit-graphic))
432 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
435 (narrow-to-region b e)
436 (goto-char (point-min))
437 (skip-chars-forward "\0-\177")
442 (and (boundp 'current-language-environment)
443 (car (last (assq 'charset
444 (assoc current-language-environment
445 language-info-alist))))))
446 (if (eq charset 'ascii) (setq charset nil))
449 (car (last (assq mail-parse-charset
450 mm-mime-mule-charset-alist)))))
451 (list 'ascii (or charset 'latin-iso8859-1)))))))))
453 (static-if (fboundp 'shell-quote-argument)
454 (defalias 'mm-quote-arg 'shell-quote-argument)
455 (defun mm-quote-arg (arg)
456 "Return a version of ARG that is safe to evaluate in a shell."
457 (let ((pos 0) new-pos accum)
458 ;; *** bug: we don't handle newline characters properly
459 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
460 (push (substring arg pos new-pos) accum)
462 (push (list (aref arg new-pos)) accum)
463 (setq pos (1+ new-pos)))
466 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
468 (defun mm-auto-mode-alist ()
469 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
470 (let ((alist auto-mode-alist)
473 (when (listp (cdar alist))
474 (push (car alist) out))
478 (defvar mm-inhibit-file-name-handlers
480 "A list of handlers doing (un)compression (etc) thingies.")
482 (defun mm-insert-file-contents (filename &optional visit beg end replace
484 "Like `insert-file-contents', q.v., but only reads in the file.
485 A buffer may be modified in several ways after reading into the buffer due
486 to advanced Emacs features, such as file-name-handlers, format decoding,
487 find-file-hooks, etc.
488 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
489 This function ensures that none of these modifications will take place."
490 (let ((format-alist nil)
491 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
492 (default-major-mode 'fundamental-mode)
493 (enable-local-variables nil)
494 (after-insert-file-functions nil)
495 (enable-local-eval nil)
496 (find-file-hooks nil)
497 (inhibit-file-name-operation (if inhibit
498 'insert-file-contents
499 inhibit-file-name-operation))
500 (inhibit-file-name-handlers
502 (append mm-inhibit-file-name-handlers
503 inhibit-file-name-handlers)
504 inhibit-file-name-handlers)))
505 (insert-file-contents filename visit beg end replace)))
507 (defun mm-append-to-file (start end filename &optional codesys inhibit)
508 "Append the contents of the region to the end of file FILENAME.
509 When called from a function, expects three arguments,
510 START, END and FILENAME. START and END are buffer positions
511 saying what text to write.
512 Optional fourth argument specifies the coding system to use when
514 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
515 (let ((coding-system-for-write
516 (or codesys mm-text-coding-system-for-write
517 mm-text-coding-system))
518 (inhibit-file-name-operation (if inhibit
520 inhibit-file-name-operation))
521 (inhibit-file-name-handlers
523 (append mm-inhibit-file-name-handlers
524 inhibit-file-name-handlers)
525 inhibit-file-name-handlers)))
526 (append-to-file start end filename)))
528 (defun mm-write-region (start end filename &optional append visit lockname
529 coding-system inhibit)
531 "Like `write-region'.
532 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
533 (let ((coding-system-for-write
534 (or coding-system mm-text-coding-system-for-write
535 mm-text-coding-system))
536 (inhibit-file-name-operation (if inhibit
538 inhibit-file-name-operation))
539 (inhibit-file-name-handlers
541 (append mm-inhibit-file-name-handlers
542 inhibit-file-name-handlers)
543 inhibit-file-name-handlers)))
544 (write-region start end filename append visit lockname)))
548 ;;; mm-util.el ends here