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.
29 (defvar mm-mime-mule-charset-alist
31 (iso-8859-1 latin-iso8859-1)
32 (iso-8859-2 latin-iso8859-2)
33 (iso-8859-3 latin-iso8859-3)
34 (iso-8859-4 latin-iso8859-4)
35 (iso-8859-5 cyrillic-iso8859-5)
36 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
37 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
38 ;; charset is koi8-r, not iso-8859-5.
39 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
40 (iso-8859-6 arabic-iso8859-6)
41 (iso-8859-7 greek-iso8859-7)
42 (iso-8859-8 hebrew-iso8859-8)
43 (iso-8859-9 latin-iso8859-9)
44 (viscii vietnamese-viscii-lower)
45 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
46 (euc-kr korean-ksc5601)
47 (cn-gb-2312 chinese-gb2312)
48 (cn-big5 chinese-big5-1 chinese-big5-2)
50 (thai-tis620 thai-tis620)
51 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
52 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
53 latin-jisx0201 japanese-jisx0208-1978
54 chinese-gb2312 japanese-jisx0208
55 korean-ksc5601 japanese-jisx0212
57 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
58 latin-jisx0201 japanese-jisx0208-1978
59 chinese-gb2312 japanese-jisx0208
60 korean-ksc5601 japanese-jisx0212
61 chinese-cns11643-1 chinese-cns11643-2)
62 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
63 cyrillic-iso8859-5 greek-iso8859-7
64 latin-jisx0201 japanese-jisx0208-1978
65 chinese-gb2312 japanese-jisx0208
66 korean-ksc5601 japanese-jisx0212
67 chinese-cns11643-1 chinese-cns11643-2
68 chinese-cns11643-3 chinese-cns11643-4
69 chinese-cns11643-5 chinese-cns11643-6
71 (utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
72 "Alist of MIME-charset/MULE-charsets.")
77 (let ((nfunc (intern (format "mm-%s" (car elem)))))
78 (if (fboundp (car elem))
79 (defalias nfunc (car elem))
80 (defalias nfunc (cdr elem)))))
81 '((decode-coding-string . (lambda (s a) s))
82 (encode-coding-string . (lambda (s a) s))
83 (encode-coding-region . ignore)
84 (coding-system-list . ignore)
85 (decode-coding-region . ignore)
87 (device-type . ignore)
88 (coding-system-equal . equal)
89 (annotationp . ignore)
90 (set-buffer-file-coding-system . ignore)
92 . (lambda (charset int)
96 "Prompt the user for a coding system."
98 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
99 mm-mime-mule-charset-alist))))
106 (mapcar (lambda (e) (list (symbol-name (car e))))
107 mm-mime-mule-charset-alist)
111 (defalias 'mm-char-or-char-int-p
113 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
114 ((fboundp 'char-valid-p) 'char-valid-p)
117 (defvar mm-coding-system-list nil)
118 (defun mm-get-coding-system-list ()
119 "Get the coding system list."
120 (or mm-coding-system-list
121 (setq mm-coding-system-list (mm-coding-system-list))))
123 (defvar mm-charset-synonym-alist
125 (gb2312 . cn-gb-2312)
127 "A mapping from invalid charset names to the real charset names.")
129 (defun mm-coding-system-p (sym)
130 "Return non-nil if SYM is a coding system."
131 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
132 (memq sym (mm-get-coding-system-list))))
134 (defvar mm-binary-coding-system
136 ((mm-coding-system-p 'binary) 'binary)
137 ((mm-coding-system-p 'no-conversion) 'no-conversion)
139 "100% binary coding system.")
141 (defvar mm-text-coding-system
142 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
143 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
144 (and (mm-coding-system-p 'raw-text) 'raw-text))
145 mm-binary-coding-system)
146 "Text-safe coding system (For removing ^M).")
148 (defvar mm-text-coding-system-for-write nil
149 "Text coding system for write.")
151 (defvar mm-auto-save-coding-system
153 ((mm-coding-system-p 'emacs-mule)
154 (if (memq system-type '(windows-nt ms-dos ms-windows))
155 (if (mm-coding-system-p 'emacs-mule-dos)
156 'emacs-mule-dos mm-binary-coding-system)
158 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
159 (t mm-binary-coding-system))
160 "Coding system of auto save file.")
162 ;;; Internal variables:
166 (defun mm-mule-charset-to-mime-charset (charset)
167 "Return the MIME charset corresponding to MULE CHARSET."
168 (let ((alist mm-mime-mule-charset-alist)
171 (when (memq charset (cdar alist))
172 (setq out (caar alist)
177 (defun mm-charset-to-coding-system (charset &optional lbt)
178 "Return coding-system corresponding to CHARSET.
179 CHARSET is a symbol naming a MIME charset.
180 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
181 used as the line break code type of the coding system."
182 (when (stringp charset)
183 (setq charset (intern (downcase charset))))
185 (or (cdr (assq charset mm-charset-synonym-alist))
188 (setq charset (intern (format "%s-%s" charset lbt))))
190 ;; Running in a non-MULE environment.
191 ((null (mm-get-coding-system-list))
194 ((eq charset 'us-ascii)
196 ;; Check to see whether we can handle this charset.
197 ((memq charset (mm-get-coding-system-list))
203 (if (fboundp 'subst-char-in-string)
204 (defsubst mm-replace-chars-in-string (string from to)
205 (subst-char-in-string from to string))
206 (defun mm-replace-chars-in-string (string from to)
207 "Replace characters in STRING from FROM to TO."
208 (let ((string (substring string 0)) ;Copy string.
209 (len (length string))
211 ;; Replace all occurrences of FROM with TO.
213 (when (= (aref string idx) from)
214 (aset string idx to))
218 (defsubst mm-enable-multibyte ()
219 "Enable multibyte in the current buffer."
220 (when (and (fboundp 'set-buffer-multibyte)
221 (boundp 'enable-multibyte-characters)
222 (default-value 'enable-multibyte-characters))
223 (set-buffer-multibyte t)))
225 (defsubst mm-disable-multibyte ()
226 "Disable multibyte in the current buffer."
227 (when (fboundp 'set-buffer-multibyte)
228 (set-buffer-multibyte nil)))
230 (defsubst mm-enable-multibyte-mule4 ()
231 "Enable multibyte in the current buffer.
232 Only used in Emacs Mule 4."
233 (when (and (fboundp 'set-buffer-multibyte)
234 (boundp 'enable-multibyte-characters)
235 (default-value 'enable-multibyte-characters)
236 (not (charsetp 'eight-bit-control)))
237 (set-buffer-multibyte t)))
239 (defsubst mm-disable-multibyte-mule4 ()
240 "Disable multibyte in the current buffer.
241 Only used in Emacs Mule 4."
242 (when (and (fboundp 'set-buffer-multibyte)
243 (not (charsetp 'eight-bit-control)))
244 (set-buffer-multibyte nil)))
246 (defun mm-preferred-coding-system (charset)
247 ;; A typo in some Emacs versions.
248 (or (get-charset-property charset 'prefered-coding-system)
249 (get-charset-property charset 'preferred-coding-system)))
251 (defun mm-charset-after (&optional pos)
252 "Return charset of a character in current buffer at position POS.
253 If POS is nil, it defauls to the current point.
254 If POS is out of range, the value is nil.
255 If the charset is `composition', return the actual one."
256 (let ((char (char-after pos)) charset)
257 (if (< (mm-char-int char) 128)
258 (setq charset 'ascii)
259 ;; charset-after is fake in some Emacsen.
260 (setq charset (and (fboundp 'char-charset) (char-charset char)))
261 (if (eq charset 'composition)
262 (let ((p (or pos (point))))
263 (cadr (find-charset-region p (1+ p))))
264 (if (and charset (not (memq charset '(ascii eight-bit-control
265 eight-bit-graphic))))
268 mail-parse-mule-charset ;; cached mule-charset
270 (setq mail-parse-mule-charset
271 (and (boundp 'current-language-environment)
274 (assoc current-language-environment
275 language-info-alist))))))
276 (if (or (not mail-parse-mule-charset)
277 (eq mail-parse-mule-charset 'ascii))
278 (setq mail-parse-mule-charset
279 (or (car (last (assq mail-parse-charset
280 mm-mime-mule-charset-alist)))
282 mail-parse-mule-charset)))))))
284 (defun mm-mime-charset (charset)
285 "Return the MIME charset corresponding to the MULE CHARSET."
286 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
287 ;; This exists in Emacs 20.
289 (and (mm-preferred-coding-system charset)
291 (mm-preferred-coding-system charset) 'mime-charset))
292 (and (eq charset 'ascii)
294 (mm-preferred-coding-system charset)
295 (mm-mule-charset-to-mime-charset charset))
296 ;; This is for XEmacs.
297 (mm-mule-charset-to-mime-charset charset)))
299 (defun mm-delete-duplicates (list)
300 "Simple substitute for CL `delete-duplicates', testing with `equal'."
303 (setq head (car list))
304 (setq list (delete head list))
305 (setq result (cons head result)))
308 (defun mm-find-mime-charset-region (b e)
309 "Return the MIME charsets needed to encode the region between B and E."
310 (let ((charsets (mapcar 'mm-mime-charset
312 (mm-find-charset-region b e)))))
313 (when (memq 'iso-2022-jp-2 charsets)
314 (setq charsets (delq 'iso-2022-jp charsets)))
315 (setq charsets (mm-delete-duplicates charsets))
316 (if (and (> (length charsets) 1)
317 (fboundp 'find-coding-systems-region)
318 (memq 'utf-8 (find-coding-systems-region b e)))
322 (defsubst mm-multibyte-p ()
323 "Say whether multibyte is enabled."
324 (if (boundp 'enable-multibyte-characters)
325 enable-multibyte-characters
328 (defmacro mm-with-unibyte-buffer (&rest forms)
329 "Create a temporary buffer, and evaluate FORMS there like `progn'.
330 See also `with-temp-file' and `with-output-to-string'."
331 (let ((temp-buffer (make-symbol "temp-buffer"))
332 (multibyte (make-symbol "multibyte")))
333 `(if (or (featurep 'xemacs)
334 (not (boundp 'enable-multibyte-characters)))
335 (with-temp-buffer ,@forms)
336 (let ((,multibyte (default-value 'enable-multibyte-characters))
340 (setq-default enable-multibyte-characters nil)
342 (get-buffer-create (generate-new-buffer-name " *temp*")))
344 (with-current-buffer ,temp-buffer
345 (let ((buffer-file-coding-system mm-binary-coding-system)
346 (coding-system-for-read mm-binary-coding-system)
347 (coding-system-for-write mm-binary-coding-system))
349 (and (buffer-name ,temp-buffer)
350 (kill-buffer ,temp-buffer))))
351 (setq-default enable-multibyte-characters ,multibyte))))))
352 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
353 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
355 (defmacro mm-with-unibyte-current-buffer (&rest forms)
356 "Evaluate FORMS there like `progn' in current buffer."
357 (let ((multibyte (make-symbol "multibyte")))
358 `(if (or (featurep 'xemacs)
359 (not (fboundp 'set-buffer-multibyte))
360 (charsetp 'eight-bit-control)) ;; For Emacs Mule 4 only.
363 (let ((,multibyte (default-value 'enable-multibyte-characters)))
365 (let ((buffer-file-coding-system mm-binary-coding-system)
366 (coding-system-for-read mm-binary-coding-system)
367 (coding-system-for-write mm-binary-coding-system))
368 (set-buffer-multibyte nil)
369 (setq-default enable-multibyte-characters nil)
371 (setq-default enable-multibyte-characters ,multibyte)
372 (set-buffer-multibyte ,multibyte))))))
373 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
374 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
376 (defmacro mm-with-unibyte (&rest forms)
377 "Set default `enable-multibyte-characters' to `nil', eval the FORMS."
378 (let ((multibyte (make-symbol "multibyte")))
379 `(if (or (featurep 'xemacs)
380 (not (boundp 'enable-multibyte-characters)))
382 (let ((,multibyte (default-value 'enable-multibyte-characters)))
385 (setq-default enable-multibyte-characters nil)
387 (setq-default enable-multibyte-characters ,multibyte))))))
388 (put 'mm-with-unibyte 'lisp-indent-function 0)
389 (put 'mm-with-unibyte 'edebug-form-spec '(body))
391 (defun mm-find-charset-region (b e)
392 "Return a list of charsets in the region."
394 ((and (mm-multibyte-p)
395 (fboundp 'find-charset-region))
396 ;; Remove composition since the base charsets have been included.
397 (delq 'composition (find-charset-region b e)))
399 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
402 (narrow-to-region b e)
403 (goto-char (point-min))
404 (skip-chars-forward "\0-\177")
409 (and (boundp 'current-language-environment)
410 (car (last (assq 'charset
411 (assoc current-language-environment
412 language-info-alist))))))
413 (if (eq charset 'ascii) (setq charset nil))
416 (car (last (assq mail-parse-charset
417 mm-mime-mule-charset-alist)))))
418 (list 'ascii (or charset 'latin-iso8859-1)))))))))
420 (if (fboundp 'shell-quote-argument)
421 (defalias 'mm-quote-arg 'shell-quote-argument)
422 (defun mm-quote-arg (arg)
423 "Return a version of ARG that is safe to evaluate in a shell."
424 (let ((pos 0) new-pos accum)
425 ;; *** bug: we don't handle newline characters properly
426 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
427 (push (substring arg pos new-pos) accum)
429 (push (list (aref arg new-pos)) accum)
430 (setq pos (1+ new-pos)))
433 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
435 (defun mm-auto-mode-alist ()
436 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
437 (let ((alist auto-mode-alist)
440 (when (listp (cdar alist))
441 (push (car alist) out))
445 (defvar mm-inhibit-file-name-handlers
447 "A list of handlers doing (un)compression (etc) thingies.")
449 (defun mm-insert-file-contents (filename &optional visit beg end replace
451 "Like `insert-file-contents', q.v., but only reads in the file.
452 A buffer may be modified in several ways after reading into the buffer due
453 to advanced Emacs features, such as file-name-handlers, format decoding,
454 find-file-hooks, etc.
455 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
456 This function ensures that none of these modifications will take place."
457 (let ((format-alist nil)
458 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
459 (default-major-mode 'fundamental-mode)
460 (enable-local-variables nil)
461 (after-insert-file-functions nil)
462 (enable-local-eval nil)
463 (find-file-hooks nil)
464 (inhibit-file-name-operation (if inhibit
465 'insert-file-contents
466 inhibit-file-name-operation))
467 (inhibit-file-name-handlers
469 (append mm-inhibit-file-name-handlers
470 inhibit-file-name-handlers)
471 inhibit-file-name-handlers)))
472 (insert-file-contents filename visit beg end replace)))
474 (defun mm-append-to-file (start end filename &optional codesys inhibit)
475 "Append the contents of the region to the end of file FILENAME.
476 When called from a function, expects three arguments,
477 START, END and FILENAME. START and END are buffer positions
478 saying what text to write.
479 Optional fourth argument specifies the coding system to use when
481 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
482 (let ((coding-system-for-write
483 (or codesys mm-text-coding-system-for-write
484 mm-text-coding-system))
485 (inhibit-file-name-operation (if inhibit
487 inhibit-file-name-operation))
488 (inhibit-file-name-handlers
490 (append mm-inhibit-file-name-handlers
491 inhibit-file-name-handlers)
492 inhibit-file-name-handlers)))
493 (append-to-file start end filename)))
495 (defun mm-write-region (start end filename &optional append visit lockname
496 coding-system inhibit)
498 "Like `write-region'.
499 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
500 (let ((coding-system-for-write
501 (or coding-system mm-text-coding-system-for-write
502 mm-text-coding-system))
503 (inhibit-file-name-operation (if inhibit
505 inhibit-file-name-operation))
506 (inhibit-file-name-handlers
508 (append mm-inhibit-file-name-handlers
509 inhibit-file-name-handlers)
510 inhibit-file-name-handlers)))
511 (write-region start end filename append visit lockname)))
515 ;;; mm-util.el ends here