1 ;;; mm-util.el --- Utility functions for Mule and low level things
2 ;; Copyright (C) 1998, 1999, 2000, 2001 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 (iso-8859-14 latin-iso8859-14)
46 (iso-8859-15 latin-iso8859-15)
47 (viscii vietnamese-viscii-lower)
48 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
49 (euc-kr korean-ksc5601)
50 (gb2312 chinese-gb2312)
51 (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 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
75 (not (fboundp 'coding-system-p))
77 (not (coding-system-p 'mule-utf-8)))
78 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
79 ;; If we have utf-8 we're in Mule 5+.
82 (coding-system-get 'mule-utf-8 'safe-charsets)))))
83 "Alist of MIME-charset/MULE-charsets.")
88 (let ((nfunc (intern (format "mm-%s" (car elem)))))
89 (if (fboundp (car elem))
90 (defalias nfunc (car elem))
91 (defalias nfunc (cdr elem)))))
92 '((decode-coding-string . (lambda (s a) s))
93 (encode-coding-string . (lambda (s a) s))
94 (encode-coding-region . ignore)
95 (coding-system-list . ignore)
96 (decode-coding-region . ignore)
98 (device-type . ignore)
99 (coding-system-equal . equal)
100 (annotationp . ignore)
101 (set-buffer-file-coding-system . ignore)
103 . (lambda (charset int)
111 (mapcar (lambda (e) (list (symbol-name (car e))))
112 mm-mime-mule-charset-alist)
114 (subst-char-in-string
115 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
116 "Replace characters in STRING from FROM to TO."
117 (let ((string (substring string 0)) ;Copy string.
118 (len (length string))
120 ;; Replace all occurrences of FROM with TO.
122 (when (= (aref string idx) from)
123 (aset string idx to))
126 (string-as-unibyte . identity)
127 (string-as-multibyte . identity)
128 (multibyte-string-p . ignore))))
131 (defalias 'mm-char-or-char-int-p
133 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
134 ((fboundp 'char-valid-p) 'char-valid-p)
138 (defalias 'mm-read-coding-system
140 ((fboundp 'read-coding-system)
141 (if (and (featurep 'xemacs)
142 (<= (string-to-number emacs-version) 21.1))
143 (lambda (prompt &optional default-coding-system)
144 (read-coding-system prompt))
145 'read-coding-system))
146 (t (lambda (prompt &optional default-coding-system)
147 "Prompt the user for a coding system."
149 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
150 mm-mime-mule-charset-alist)))))))
152 (defvar mm-coding-system-list nil)
153 (defun mm-get-coding-system-list ()
154 "Get the coding system list."
155 (or mm-coding-system-list
156 (setq mm-coding-system-list (mm-coding-system-list))))
158 (defun mm-coding-system-p (sym)
159 "Return non-nil if SYM is a coding system."
160 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
161 (memq sym (mm-get-coding-system-list))))
163 (defvar mm-charset-synonym-alist
165 (gb2312 . cn-gb-2312)
167 ;; Windows-1252 is actually a superset of Latin-1. See also
168 ;; `gnus-article-dumbquotes-map'.
169 ,(unless (mm-coding-system-p 'windows-1252) ; should be defined eventually
170 '(windows-1252 . iso-8859-1))
171 ;; ISO-8859-15 is very similar to ISO-8859-1.
172 ,(unless (mm-coding-system-p 'iso-8859-15) ; Emacs 21 defines it.
173 '(iso-8859-15 . iso-8859-1))
174 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
175 ;; Outlook users in Czech republic. Use this to allow reading of their
176 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
177 ,(unless (mm-coding-system-p 'windows-1250) ; should be defined eventually
178 '(windows-1250 . cp1250))
180 "A mapping from invalid charset names to the real charset names.")
182 (defvar mm-binary-coding-system
184 ((mm-coding-system-p 'binary) 'binary)
185 ((mm-coding-system-p 'no-conversion) 'no-conversion)
187 "100% binary coding system.")
189 (defvar mm-text-coding-system
190 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
191 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
192 (and (mm-coding-system-p 'raw-text) 'raw-text))
193 mm-binary-coding-system)
194 "Text-safe coding system (For removing ^M).")
196 (defvar mm-text-coding-system-for-write nil
197 "Text coding system for write.")
199 (defvar mm-auto-save-coding-system
201 ((mm-coding-system-p 'emacs-mule)
202 (if (memq system-type '(windows-nt ms-dos ms-windows))
203 (if (mm-coding-system-p 'emacs-mule-dos)
204 'emacs-mule-dos mm-binary-coding-system)
206 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
207 (t mm-binary-coding-system))
208 "Coding system of auto save file.")
210 (defvar mm-universal-coding-system mm-auto-save-coding-system
211 "The universal Coding system.")
213 ;;; Internal variables:
217 (defun mm-mule-charset-to-mime-charset (charset)
218 "Return the MIME charset corresponding to the given Mule CHARSET."
219 (let ((alist mm-mime-mule-charset-alist)
222 (when (memq charset (cdar alist))
223 (setq out (caar alist)
228 (defun mm-charset-to-coding-system (charset &optional lbt)
229 "Return coding-system corresponding to CHARSET.
230 CHARSET is a symbol naming a MIME charset.
231 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
232 used as the line break code type of the coding system."
233 (when (stringp charset)
234 (setq charset (intern (downcase charset))))
236 (or (cdr (assq charset mm-charset-synonym-alist))
239 (setq charset (intern (format "%s-%s" charset lbt))))
241 ;; Running in a non-MULE environment.
242 ((null (mm-get-coding-system-list))
245 ((eq charset 'us-ascii)
247 ;; Check to see whether we can handle this charset. (This depends
248 ;; on there being some coding system matching each `mime-charset'
249 ;; coding sysytem property defined, as there should be.)
250 ((memq charset (mm-get-coding-system-list))
256 (defsubst mm-replace-chars-in-string (string from to)
257 (mm-subst-char-in-string from to string))
259 (defsubst mm-enable-multibyte ()
260 "Set the multibyte flag of the current buffer.
261 Only do this if the default value of `enable-multibyte-characters' is
262 non-nil. This is a no-op in XEmacs."
263 (when (and (not (featurep 'xemacs))
264 (boundp 'default-enable-multibyte-characters)
265 default-enable-multibyte-characters
266 (fboundp 'set-buffer-multibyte))
267 (set-buffer-multibyte t)))
269 (defsubst mm-disable-multibyte ()
270 "Unset the multibyte flag of in the current buffer.
271 This is a no-op in XEmacs."
272 (when (and (not (featurep 'xemacs))
273 (fboundp 'set-buffer-multibyte))
274 (set-buffer-multibyte nil)))
276 (defsubst mm-enable-multibyte-mule4 ()
277 "Enable multibyte in the current buffer.
278 Only used in Emacs Mule 4."
279 (when (and (not (featurep 'xemacs))
280 (boundp 'default-enable-multibyte-characters)
281 default-enable-multibyte-characters
282 (fboundp 'set-buffer-multibyte)
284 (not (charsetp 'eight-bit-control)))
285 (set-buffer-multibyte t)))
287 (defsubst mm-disable-multibyte-mule4 ()
288 "Disable multibyte in the current buffer.
289 Only used in Emacs Mule 4."
290 (when (and (not (featurep 'xemacs))
291 (fboundp 'set-buffer-multibyte)
293 (not (charsetp 'eight-bit-control)))
294 (set-buffer-multibyte nil)))
296 (defun mm-preferred-coding-system (charset)
297 ;; A typo in some Emacs versions.
298 (or (get-charset-property charset 'prefered-coding-system)
299 (get-charset-property charset 'preferred-coding-system)))
301 (defun mm-charset-after (&optional pos)
302 "Return charset of a character in current buffer at position POS.
303 If POS is nil, it defauls to the current point.
304 If POS is out of range, the value is nil.
305 If the charset is `composition', return the actual one."
306 (let ((char (char-after pos)) charset)
307 (if (< (mm-char-int char) 128)
308 (setq charset 'ascii)
309 ;; charset-after is fake in some Emacsen.
310 (setq charset (and (fboundp 'char-charset) (char-charset char)))
311 (if (eq charset 'composition)
312 (let ((p (or pos (point))))
313 (cadr (find-charset-region p (1+ p))))
314 (if (and charset (not (memq charset '(ascii eight-bit-control
315 eight-bit-graphic))))
318 mail-parse-mule-charset ;; cached mule-charset
320 (setq mail-parse-mule-charset
321 (and (boundp 'current-language-environment)
324 (assoc current-language-environment
325 language-info-alist))))))
326 (if (or (not mail-parse-mule-charset)
327 (eq mail-parse-mule-charset 'ascii))
328 (setq mail-parse-mule-charset
329 (or (car (last (assq mail-parse-charset
330 mm-mime-mule-charset-alist)))
331 ;; Fixme: don't fix that!
333 mail-parse-mule-charset)))))))
335 (defun mm-mime-charset (charset)
336 "Return the MIME charset corresponding to the MULE CHARSET."
337 (if (eq charset 'unknown)
338 (error "The message contains non-printable characters, please use attachment"))
339 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
340 ;; This exists in Emacs 20.
342 (and (mm-preferred-coding-system charset)
344 (mm-preferred-coding-system charset) 'mime-charset))
345 (and (eq charset 'ascii)
347 (mm-preferred-coding-system charset)
348 (mm-mule-charset-to-mime-charset charset))
349 ;; This is for XEmacs.
350 (mm-mule-charset-to-mime-charset charset)))
352 (defun mm-delete-duplicates (list)
353 "Simple substitute for CL `delete-duplicates', testing with `equal'."
356 (setq head (car list))
357 (setq list (delete head list))
358 (setq result (cons head result)))
361 (defun mm-find-mime-charset-region (b e)
362 "Return the MIME charsets needed to encode the region between B and E."
363 (let ((charsets (mapcar 'mm-mime-charset
365 (mm-find-charset-region b e)))))
366 (when (memq 'iso-2022-jp-2 charsets)
367 (setq charsets (delq 'iso-2022-jp charsets)))
368 (setq charsets (mm-delete-duplicates charsets))
369 (if (and (> (length charsets) 1)
370 (fboundp 'find-coding-systems-region)
371 (let ((cs (find-coding-systems-region b e)))
372 (or (memq 'utf-8 cs) (memq 'mule-utf-8 cs))))
376 (defsubst mm-multibyte-p ()
377 "Say whether multibyte is enabled."
378 (if (and (not (featurep 'xemacs))
379 (boundp 'enable-multibyte-characters))
380 enable-multibyte-characters
383 (defmacro mm-with-unibyte-buffer (&rest forms)
384 "Create a temporary buffer, and evaluate FORMS there like `progn'.
385 Use unibyte mode for this."
386 `(let (default-enable-multibyte-characters)
387 (with-temp-buffer ,@forms)))
388 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
389 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
391 (defmacro mm-with-unibyte-current-buffer (&rest forms)
392 "Evaluate FORMS with current current buffer temporarily made unibyte.
393 Also bind `default-enable-multibyte-characters' to nil.
394 Equivalent to `progn' in XEmacs"
395 (let ((buffer (make-symbol "buffer")))
396 `(if (and (not (featurep 'xemacs))
397 (boundp 'enable-multibyte-characters)
398 enable-multibyte-characters
399 (fboundp 'set-buffer-multibyte))
400 (let ((,buffer (current-buffer)))
402 (let (default-enable-multibyte-characters)
403 (set-buffer-multibyte nil)
406 (set-buffer-multibyte t)))
407 (let (default-enable-multibyte-characters)
409 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
410 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
412 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
413 "Evaluate FORMS there like `progn' in current buffer.
415 (let ((buffer (make-symbol "buffer")))
416 `(if (and (not (featurep 'xemacs))
417 (boundp 'enable-multibyte-characters)
418 enable-multibyte-characters
419 (fboundp 'set-buffer-multibyte)
421 (not (charsetp 'eight-bit-control))) ;; For Emacs Mule 4 only.
422 (let ((,buffer (current-buffer)))
424 (let (default-enable-multibyte-characters)
425 (set-buffer-multibyte nil)
428 (set-buffer-multibyte t)))
429 (let (default-enable-multibyte-characters)
431 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
432 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
434 (defmacro mm-with-unibyte (&rest forms)
435 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
436 `(let (default-enable-multibyte-characters)
438 (put 'mm-with-unibyte 'lisp-indent-function 0)
439 (put 'mm-with-unibyte 'edebug-form-spec '(body))
441 (defun mm-find-charset-region (b e)
442 "Return a list of Emacs charsets in the region B to E."
444 ((and (mm-multibyte-p)
445 (fboundp 'find-charset-region))
446 ;; Remove composition since the base charsets have been included.
447 ;; Remove eight-bit-*, treat them as ascii.
448 (let ((css (find-charset-region b e)))
449 (mapcar (lambda (cs) (setq css (delq cs css)))
450 '(composition eight-bit-control eight-bit-graphic
454 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
457 (narrow-to-region b e)
458 (goto-char (point-min))
459 (skip-chars-forward "\0-\177")
464 (and (boundp 'current-language-environment)
465 (car (last (assq 'charset
466 (assoc current-language-environment
467 language-info-alist))))))
468 (if (eq charset 'ascii) (setq charset nil))
471 (car (last (assq mail-parse-charset
472 mm-mime-mule-charset-alist)))))
473 (list 'ascii (or charset 'latin-iso8859-1)))))))))
475 (if (fboundp 'shell-quote-argument)
476 (defalias 'mm-quote-arg 'shell-quote-argument)
477 (defun mm-quote-arg (arg)
478 "Return a version of ARG that is safe to evaluate in a shell."
479 (let ((pos 0) new-pos accum)
480 ;; *** bug: we don't handle newline characters properly
481 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
482 (push (substring arg pos new-pos) accum)
484 (push (list (aref arg new-pos)) accum)
485 (setq pos (1+ new-pos)))
488 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
490 (defun mm-auto-mode-alist ()
491 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
492 (let ((alist auto-mode-alist)
495 (when (listp (cdar alist))
496 (push (car alist) out))
500 (defvar mm-inhibit-file-name-handlers
501 '(jka-compr-handler image-file-handler)
502 "A list of handlers doing (un)compression (etc) thingies.")
504 (defun mm-insert-file-contents (filename &optional visit beg end replace
506 "Like `insert-file-contents', q.v., but only reads in the file.
507 A buffer may be modified in several ways after reading into the buffer due
508 to advanced Emacs features, such as file-name-handlers, format decoding,
509 find-file-hooks, etc.
510 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
511 This function ensures that none of these modifications will take place."
512 (let ((format-alist nil)
513 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
514 (default-major-mode 'fundamental-mode)
515 (enable-local-variables nil)
516 (after-insert-file-functions nil)
517 (enable-local-eval nil)
518 (find-file-hooks nil)
519 (inhibit-file-name-operation (if inhibit
520 'insert-file-contents
521 inhibit-file-name-operation))
522 (inhibit-file-name-handlers
524 (append mm-inhibit-file-name-handlers
525 inhibit-file-name-handlers)
526 inhibit-file-name-handlers)))
527 (insert-file-contents filename visit beg end replace)))
529 (defun mm-append-to-file (start end filename &optional codesys inhibit)
530 "Append the contents of the region to the end of file FILENAME.
531 When called from a function, expects three arguments,
532 START, END and FILENAME. START and END are buffer positions
533 saying what text to write.
534 Optional fourth argument specifies the coding system to use when
536 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
537 (let ((coding-system-for-write
538 (or codesys mm-text-coding-system-for-write
539 mm-text-coding-system))
540 (inhibit-file-name-operation (if inhibit
542 inhibit-file-name-operation))
543 (inhibit-file-name-handlers
545 (append mm-inhibit-file-name-handlers
546 inhibit-file-name-handlers)
547 inhibit-file-name-handlers)))
548 (append-to-file start end filename)))
550 (defun mm-write-region (start end filename &optional append visit lockname
551 coding-system inhibit)
553 "Like `write-region'.
554 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
555 (let ((coding-system-for-write
556 (or coding-system mm-text-coding-system-for-write
557 mm-text-coding-system))
558 (inhibit-file-name-operation (if inhibit
560 inhibit-file-name-operation))
561 (inhibit-file-name-handlers
563 (append mm-inhibit-file-name-handlers
564 inhibit-file-name-handlers)
565 inhibit-file-name-handlers)))
566 (write-region start end filename append visit lockname)))
568 (defun mm-image-load-path (&optional package)
570 (dolist (path load-path (nreverse result))
571 (if (file-directory-p
572 (setq dir (concat (file-name-directory
573 (directory-file-name path))
574 "etc/" (or package "gnus/"))))
576 (push path result))))
580 ;;; mm-util.el ends here