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)
110 (subst-char-in-string
111 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
112 "Replace characters in STRING from FROM to TO."
113 (let ((string (substring string 0)) ;Copy string.
114 (len (length string))
116 ;; Replace all occurrences of FROM with TO.
118 (when (= (aref string idx) from)
119 (aset string idx to))
125 (defalias 'mm-char-or-char-int-p
127 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
128 ((fboundp 'char-valid-p) 'char-valid-p)
131 (defvar mm-coding-system-list nil)
132 (defun mm-get-coding-system-list ()
133 "Get the coding system list."
134 (or mm-coding-system-list
135 (setq mm-coding-system-list (mm-coding-system-list))))
137 (defun mm-coding-system-p (sym)
138 "Return non-nil if SYM is a coding system."
139 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
140 (memq sym (mm-get-coding-system-list))))
142 (defvar mm-charset-synonym-alist
144 (gb2312 . cn-gb-2312)
146 ;; Windows-1252 is actually a superset of Latin-1. See also
147 ;; `gnus-article-dumbquotes-map'.
148 ,(unless (mm-coding-system-p 'windows-1252) ; should be defined eventually
149 '(windows-1252 . iso-8859-1))
151 "A mapping from invalid charset names to the real charset names.")
153 (defvar mm-binary-coding-system
155 ((mm-coding-system-p 'binary) 'binary)
156 ((mm-coding-system-p 'no-conversion) 'no-conversion)
158 "100% binary coding system.")
160 (defvar mm-text-coding-system
161 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
162 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
163 (and (mm-coding-system-p 'raw-text) 'raw-text))
164 mm-binary-coding-system)
165 "Text-safe coding system (For removing ^M).")
167 (defvar mm-text-coding-system-for-write nil
168 "Text coding system for write.")
170 (defvar mm-auto-save-coding-system
172 ((mm-coding-system-p 'emacs-mule)
173 (if (memq system-type '(windows-nt ms-dos ms-windows))
174 (if (mm-coding-system-p 'emacs-mule-dos)
175 'emacs-mule-dos mm-binary-coding-system)
177 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
178 (t mm-binary-coding-system))
179 "Coding system of auto save file.")
181 ;;; Internal variables:
185 (defun mm-mule-charset-to-mime-charset (charset)
186 "Return the MIME charset corresponding to the given Mule CHARSET."
187 (let ((alist mm-mime-mule-charset-alist)
190 (when (memq charset (cdar alist))
191 (setq out (caar alist)
196 (defun mm-charset-to-coding-system (charset &optional lbt)
197 "Return coding-system corresponding to CHARSET.
198 CHARSET is a symbol naming a MIME charset.
199 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
200 used as the line break code type of the coding system."
201 (when (stringp charset)
202 (setq charset (intern (downcase charset))))
204 (or (cdr (assq charset mm-charset-synonym-alist))
207 (setq charset (intern (format "%s-%s" charset lbt))))
209 ;; Running in a non-MULE environment.
210 ((null (mm-get-coding-system-list))
213 ((eq charset 'us-ascii)
215 ;; Check to see whether we can handle this charset. (This depends
216 ;; on there being some coding system matching each `mime-charset'
217 ;; coding sysytem property defined, as there should be.)
218 ((memq charset (mm-get-coding-system-list))
224 (defsubst mm-replace-chars-in-string (string from to)
225 (mm-subst-char-in-string from to string))
227 (defsubst mm-enable-multibyte ()
228 "Set the multibyte flag of the current buffer.
229 Only do this if the default value of `enable-multibyte-characters' is
230 non-nil. This is a no-op in XEmacs."
231 (when (and (fboundp 'set-buffer-multibyte)
232 (boundp 'enable-multibyte-characters)
233 (default-value 'enable-multibyte-characters))
234 (set-buffer-multibyte t)))
236 (defsubst mm-disable-multibyte ()
237 "Unset the multibyte flag of in the current buffer.
238 This is a no-op in XEmacs."
239 (when (fboundp 'set-buffer-multibyte)
240 (set-buffer-multibyte nil)))
242 (defsubst mm-enable-multibyte-mule4 ()
243 "Enable multibyte in the current buffer.
244 Only used in Emacs Mule 4."
245 (when (and (fboundp 'set-buffer-multibyte)
246 (boundp 'enable-multibyte-characters)
247 (default-value 'enable-multibyte-characters)
249 (not (charsetp 'eight-bit-control)))
250 (set-buffer-multibyte t)))
252 (defsubst mm-disable-multibyte-mule4 ()
253 "Disable multibyte in the current buffer.
254 Only used in Emacs Mule 4."
255 (when (and (fboundp 'set-buffer-multibyte)
257 (not (charsetp 'eight-bit-control)))
258 (set-buffer-multibyte nil)))
260 (defun mm-preferred-coding-system (charset)
261 ;; A typo in some Emacs versions.
262 (or (get-charset-property charset 'prefered-coding-system)
263 (get-charset-property charset 'preferred-coding-system)))
265 (defun mm-charset-after (&optional pos)
266 "Return charset of a character in current buffer at position POS.
267 If POS is nil, it defauls to the current point.
268 If POS is out of range, the value is nil.
269 If the charset is `composition', return the actual one."
270 (let ((char (char-after pos)) charset)
271 (if (< (mm-char-int char) 128)
272 (setq charset 'ascii)
273 ;; charset-after is fake in some Emacsen.
274 (setq charset (and (fboundp 'char-charset) (char-charset char)))
275 (if (eq charset 'composition)
276 (let ((p (or pos (point))))
277 (cadr (find-charset-region p (1+ p))))
278 (if (and charset (not (memq charset '(ascii eight-bit-control
279 eight-bit-graphic))))
282 mail-parse-mule-charset ;; cached mule-charset
284 (setq mail-parse-mule-charset
285 (and (boundp 'current-language-environment)
288 (assoc current-language-environment
289 language-info-alist))))))
290 (if (or (not mail-parse-mule-charset)
291 (eq mail-parse-mule-charset 'ascii))
292 (setq mail-parse-mule-charset
293 (or (car (last (assq mail-parse-charset
294 mm-mime-mule-charset-alist)))
296 mail-parse-mule-charset)))))))
298 (defun mm-mime-charset (charset)
299 "Return the MIME charset corresponding to the MULE CHARSET."
300 (if (eq charset 'unknown)
301 (error "8-bit characters are found in the message, please specify charset."))
302 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
303 ;; This exists in Emacs 20.
305 (and (mm-preferred-coding-system charset)
307 (mm-preferred-coding-system charset) 'mime-charset))
308 (and (eq charset 'ascii)
310 (mm-preferred-coding-system charset)
311 (mm-mule-charset-to-mime-charset charset))
312 ;; This is for XEmacs.
313 (mm-mule-charset-to-mime-charset charset)))
315 (defun mm-delete-duplicates (list)
316 "Simple substitute for CL `delete-duplicates', testing with `equal'."
319 (setq head (car list))
320 (setq list (delete head list))
321 (setq result (cons head result)))
324 (defun mm-find-mime-charset-region (b e)
325 "Return the MIME charsets needed to encode the region between B and E."
326 (let ((charsets (mapcar 'mm-mime-charset
328 (mm-find-charset-region b e)))))
329 (when (memq 'iso-2022-jp-2 charsets)
330 (setq charsets (delq 'iso-2022-jp charsets)))
331 (setq charsets (mm-delete-duplicates charsets))
332 (if (and (> (length charsets) 1)
333 (fboundp 'find-coding-systems-region)
334 (memq 'utf-8 (find-coding-systems-region b e)))
338 (defsubst mm-multibyte-p ()
339 "Say whether multibyte is enabled."
340 (if (and (not (featurep 'xemacs))
341 (boundp 'enable-multibyte-characters))
342 enable-multibyte-characters
345 (defmacro mm-with-unibyte-buffer (&rest forms)
346 "Create a temporary buffer, and evaluate FORMS there like `progn'.
347 Use unibyte mode for this."
348 `(let (default-enable-multibyte-characters)
349 (with-temp-buffer ,@forms)))
350 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
351 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
353 (defmacro mm-with-unibyte-current-buffer (&rest forms)
354 "Evaluate FORMS with current current buffer temporarily made unibyte.
355 Also bind `default-enable-multibyte-characters' to nil.
356 Equivalent to `progn' in XEmacs"
357 (let ((multibyte (make-symbol "multibyte")))
358 `(if (fboundp 'set-buffer-multibyte)
359 (let ((,multibyte enable-multibyte-characters))
361 (let (default-enable-multibyte-characters)
362 (set-buffer-multibyte nil)
364 (set-buffer-multibyte ,multibyte)))
367 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
368 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
370 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
371 "Evaluate FORMS there like `progn' in current buffer.
373 (let ((multibyte (make-symbol "multibyte")))
374 `(if (or (featurep 'xemacs)
375 (not (fboundp 'set-buffer-multibyte))
376 (not (fboundp 'charsetp))
377 (charsetp 'eight-bit-control)) ;; For Emacs Mule 4 only.
380 (let ((,multibyte (default-value 'enable-multibyte-characters)))
382 (let ((buffer-file-coding-system mm-binary-coding-system)
383 (coding-system-for-read mm-binary-coding-system)
384 (coding-system-for-write mm-binary-coding-system))
385 (set-buffer-multibyte nil)
386 (setq-default enable-multibyte-characters nil)
388 (setq-default enable-multibyte-characters ,multibyte)
389 (set-buffer-multibyte ,multibyte))))))
390 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
391 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
393 (defmacro mm-with-unibyte (&rest forms)
394 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
395 `(let (default-enable-multibyte-characters)
397 (put 'mm-with-unibyte 'lisp-indent-function 0)
398 (put 'mm-with-unibyte 'edebug-form-spec '(body))
400 (defun mm-find-charset-region (b e)
401 "Return a list of Emacs charsets in the region B to E."
403 ((and (mm-multibyte-p)
404 (fboundp 'find-charset-region))
405 ;; Remove composition since the base charsets have been included.
406 ;; Remove eight-bit-*, treat them as ascii.
407 (let ((css (find-charset-region b e)))
408 (mapcar (lambda (cs) (setq css (delq cs css)))
409 '(composition eight-bit-control eight-bit-graphic))
412 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
415 (narrow-to-region b e)
416 (goto-char (point-min))
417 (skip-chars-forward "\0-\177")
422 (and (boundp 'current-language-environment)
423 (car (last (assq 'charset
424 (assoc current-language-environment
425 language-info-alist))))))
426 (if (eq charset 'ascii) (setq charset nil))
429 (car (last (assq mail-parse-charset
430 mm-mime-mule-charset-alist)))))
431 (list 'ascii (or charset 'latin-iso8859-1)))))))))
433 (if (fboundp 'shell-quote-argument)
434 (defalias 'mm-quote-arg 'shell-quote-argument)
435 (defun mm-quote-arg (arg)
436 "Return a version of ARG that is safe to evaluate in a shell."
437 (let ((pos 0) new-pos accum)
438 ;; *** bug: we don't handle newline characters properly
439 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
440 (push (substring arg pos new-pos) accum)
442 (push (list (aref arg new-pos)) accum)
443 (setq pos (1+ new-pos)))
446 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
448 (defun mm-auto-mode-alist ()
449 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
450 (let ((alist auto-mode-alist)
453 (when (listp (cdar alist))
454 (push (car alist) out))
458 (defvar mm-inhibit-file-name-handlers
460 "A list of handlers doing (un)compression (etc) thingies.")
462 (defun mm-insert-file-contents (filename &optional visit beg end replace
464 "Like `insert-file-contents', q.v., but only reads in the file.
465 A buffer may be modified in several ways after reading into the buffer due
466 to advanced Emacs features, such as file-name-handlers, format decoding,
467 find-file-hooks, etc.
468 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
469 This function ensures that none of these modifications will take place."
470 (let ((format-alist nil)
471 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
472 (default-major-mode 'fundamental-mode)
473 (enable-local-variables nil)
474 (after-insert-file-functions nil)
475 (enable-local-eval nil)
476 (find-file-hooks nil)
477 (inhibit-file-name-operation (if inhibit
478 'insert-file-contents
479 inhibit-file-name-operation))
480 (inhibit-file-name-handlers
482 (append mm-inhibit-file-name-handlers
483 inhibit-file-name-handlers)
484 inhibit-file-name-handlers)))
485 (insert-file-contents filename visit beg end replace)))
487 (defun mm-append-to-file (start end filename &optional codesys inhibit)
488 "Append the contents of the region to the end of file FILENAME.
489 When called from a function, expects three arguments,
490 START, END and FILENAME. START and END are buffer positions
491 saying what text to write.
492 Optional fourth argument specifies the coding system to use when
494 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
495 (let ((coding-system-for-write
496 (or codesys mm-text-coding-system-for-write
497 mm-text-coding-system))
498 (inhibit-file-name-operation (if inhibit
500 inhibit-file-name-operation))
501 (inhibit-file-name-handlers
503 (append mm-inhibit-file-name-handlers
504 inhibit-file-name-handlers)
505 inhibit-file-name-handlers)))
506 (append-to-file start end filename)))
508 (defun mm-write-region (start end filename &optional append visit lockname
509 coding-system inhibit)
511 "Like `write-region'.
512 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
513 (let ((coding-system-for-write
514 (or coding-system mm-text-coding-system-for-write
515 mm-text-coding-system))
516 (inhibit-file-name-operation (if inhibit
518 inhibit-file-name-operation))
519 (inhibit-file-name-handlers
521 (append mm-inhibit-file-name-handlers
522 inhibit-file-name-handlers)
523 inhibit-file-name-handlers)))
524 (write-region start end filename append visit lockname)))
528 ;;; mm-util.el ends here