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))
33 (let ((nfunc (intern (format "mm-%s" (car elem)))))
34 (if (fboundp (car elem))
35 (defalias nfunc (car elem))
36 (defalias nfunc (cdr elem)))))
37 '((decode-coding-string . (lambda (s a) s))
38 (encode-coding-string . (lambda (s a) s))
39 (encode-coding-region . ignore)
40 (coding-system-list . ignore)
41 (decode-coding-region . ignore)
43 (device-type . ignore)
44 (coding-system-equal . equal)
45 (annotationp . ignore)
46 (set-buffer-file-coding-system . ignore)
48 . (lambda (charset int)
56 (mapcar (lambda (e) (list (symbol-name (car e))))
57 mm-mime-mule-charset-alist)
60 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
61 "Replace characters in STRING from FROM to TO."
62 (let ((string (substring string 0)) ;Copy string.
65 ;; Replace all occurrences of FROM with TO.
67 (when (= (aref string idx) from)
71 (string-as-unibyte . identity)
72 (string-as-multibyte . identity)
73 (multibyte-string-p . ignore))))
76 (defalias 'mm-char-or-char-int-p
78 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
79 ((fboundp 'char-valid-p) 'char-valid-p)
83 (defalias 'mm-read-coding-system
85 ((fboundp 'read-coding-system)
86 (if (and (featurep 'xemacs)
87 (<= (string-to-number emacs-version) 21.1))
88 (lambda (prompt &optional default-coding-system)
89 (read-coding-system prompt))
91 (t (lambda (prompt &optional default-coding-system)
92 "Prompt the user for a coding system."
94 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
95 mm-mime-mule-charset-alist)))))))
97 (defvar mm-coding-system-list nil)
98 (defun mm-get-coding-system-list ()
99 "Get the coding system list."
100 (or mm-coding-system-list
101 (setq mm-coding-system-list (mm-coding-system-list))))
103 (defun mm-coding-system-p (sym)
104 "Return non-nil if SYM is a coding system."
105 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
106 (memq sym (mm-get-coding-system-list))))
108 (defvar mm-charset-synonym-alist
110 ;; Perfectly fine? A valid MIME name, anyhow.
111 ,(unless (mm-coding-system-p 'big5)
113 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
114 ,(unless (mm-coding-system-p 'x-ctext)
116 ;; Apparently not defined in Emacs 20, but is a valid MIME name.
117 ,(unless (mm-coding-system-p 'gb2312)
118 '(gb2312 . cn-gb-2312))
119 ;; Windows-1252 is actually a superset of Latin-1. See also
120 ;; `gnus-article-dumbquotes-map'.
121 ;;,(unless (mm-coding-system-p 'windows-1252)
122 ; should be defined eventually
123 ;; '(windows-1252 . iso-8859-1))
124 ;; ISO-8859-15 is very similar to ISO-8859-1.
125 ;;,(unless (mm-coding-system-p 'iso-8859-15) ; Emacs 21 defines it.
126 ;; '(iso-8859-15 . iso-8859-1))
127 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
128 ;; Outlook users in Czech republic. Use this to allow reading of their
129 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
130 ;;,(unless (mm-coding-system-p 'windows-1250)
131 ; should be defined eventually
132 ;; '(windows-1250 . cp1250))
134 "A mapping from invalid charset names to the real charset names.")
136 (defvar mm-binary-coding-system
138 ((mm-coding-system-p 'binary) 'binary)
139 ((mm-coding-system-p 'no-conversion) 'no-conversion)
141 "100% binary coding system.")
143 (defvar mm-text-coding-system
144 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
145 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
146 (and (mm-coding-system-p 'raw-text) 'raw-text))
147 mm-binary-coding-system)
148 "Text-safe coding system (For removing ^M).")
150 (defvar mm-text-coding-system-for-write nil
151 "Text coding system for write.")
153 (defvar mm-auto-save-coding-system
155 ((mm-coding-system-p 'emacs-mule)
156 (if (memq system-type '(windows-nt ms-dos ms-windows))
157 (if (mm-coding-system-p 'emacs-mule-dos)
158 'emacs-mule-dos mm-binary-coding-system)
160 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
161 (t mm-binary-coding-system))
162 "Coding system of auto save file.")
164 (defvar mm-universal-coding-system mm-auto-save-coding-system
165 "The universal Coding system.")
167 ;; Fixme: some of the cars here aren't valid MIME charsets. That
168 ;; should only matter with XEmacs, though.
169 (defvar mm-mime-mule-charset-alist
171 (iso-8859-1 latin-iso8859-1)
172 (iso-8859-2 latin-iso8859-2)
173 (iso-8859-3 latin-iso8859-3)
174 (iso-8859-4 latin-iso8859-4)
175 (iso-8859-5 cyrillic-iso8859-5)
176 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
177 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
178 ;; charset is koi8-r, not iso-8859-5.
179 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
180 (iso-8859-6 arabic-iso8859-6)
181 (iso-8859-7 greek-iso8859-7)
182 (iso-8859-8 hebrew-iso8859-8)
183 (iso-8859-9 latin-iso8859-9)
184 (iso-8859-14 latin-iso8859-14)
185 (iso-8859-15 latin-iso8859-15)
186 (viscii vietnamese-viscii-lower)
187 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
188 (euc-kr korean-ksc5601)
189 (gb2312 chinese-gb2312)
190 (big5 chinese-big5-1 chinese-big5-2)
192 (thai-tis620 thai-tis620)
193 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
194 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
195 latin-jisx0201 japanese-jisx0208-1978
196 chinese-gb2312 japanese-jisx0208
197 korean-ksc5601 japanese-jisx0212
199 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
200 latin-jisx0201 japanese-jisx0208-1978
201 chinese-gb2312 japanese-jisx0208
202 korean-ksc5601 japanese-jisx0212
203 chinese-cns11643-1 chinese-cns11643-2)
204 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
205 cyrillic-iso8859-5 greek-iso8859-7
206 latin-jisx0201 japanese-jisx0208-1978
207 chinese-gb2312 japanese-jisx0208
208 korean-ksc5601 japanese-jisx0212
209 chinese-cns11643-1 chinese-cns11643-2
210 chinese-cns11643-3 chinese-cns11643-4
211 chinese-cns11643-5 chinese-cns11643-6
213 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
214 (charsetp 'unicode-a)
215 (not (mm-coding-system-p 'mule-utf-8)))
216 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
217 ;; If we have utf-8 we're in Mule 5+.
220 (coding-system-get 'mule-utf-8 'safe-charsets)))))
221 "Alist of MIME-charset/MULE-charsets.")
223 ;; Correct by construction, but should be unnecessary:
225 (when (and (not (featurep 'xemacs))
226 (fboundp 'coding-system-list)
227 (fboundp 'sort-coding-systems))
228 (setq mm-mime-mule-charset-alist
233 (when (and (coding-system-get cs 'mime-charset)
234 (not (eq t (coding-system-get cs 'safe-charsets))))
235 (list (cons (coding-system-get cs 'mime-charset)
237 (coding-system-get cs 'safe-charsets))))))
238 (sort-coding-systems (coding-system-list 'base-only))))))
240 ;;; Internal variables:
244 (defun mm-mule-charset-to-mime-charset (charset)
245 "Return the MIME charset corresponding to the given Mule CHARSET."
246 (if (fboundp 'find-coding-systems-for-charsets)
248 (dolist (cs (find-coding-systems-for-charsets (list charset)))
251 (setq mime (coding-system-get cs 'mime-charset)))))
253 (let ((alist mm-mime-mule-charset-alist)
256 (when (memq charset (cdar alist))
257 (setq out (caar alist)
262 (defun mm-charset-to-coding-system (charset &optional lbt)
263 "Return coding-system corresponding to CHARSET.
264 CHARSET is a symbol naming a MIME charset.
265 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
266 used as the line break code type of the coding system."
267 (when (stringp charset)
268 (setq charset (intern (downcase charset))))
270 (setq charset (intern (format "%s-%s" charset lbt))))
272 ;; Running in a non-MULE environment.
273 ((null (mm-get-coding-system-list))
276 ((eq charset 'us-ascii)
278 ;; Check to see whether we can handle this charset. (This depends
279 ;; on there being some coding system matching each `mime-charset'
280 ;; property defined, as there should be.)
281 ((and (mm-coding-system-p charset)
282 ;;; Doing this would potentially weed out incorrect charsets.
284 ;;; (eq charset (coding-system-get charset 'mime-charset))
287 ;; Translate invalid charsets.
288 ((mm-coding-system-p (setq charset
290 mm-charset-synonym-alist))))
292 ;; Last resort: search the coding system list for entries which
293 ;; have the right mime-charset in case the canonical name isn't
294 ;; defined (though it should be).
296 ;; mm-get-coding-system-list returns a list of cs without lbt.
298 (dolist (c (mm-get-coding-system-list))
300 (eq charset (coding-system-get c 'mime-charset)))
304 (defsubst mm-replace-chars-in-string (string from to)
305 (mm-subst-char-in-string from to string))
308 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
309 (boundp 'default-enable-multibyte-characters)
310 default-enable-multibyte-characters
311 (fboundp 'set-buffer-multibyte))
314 (defvar mm-mule4-p (and mm-emacs-mule
316 (not (charsetp 'eight-bit-control)))
320 (defun mm-enable-multibyte ()
321 "Set the multibyte flag of the current buffer.
322 Only do this if the default value of `enable-multibyte-characters' is
323 non-nil. This is a no-op in XEmacs."
324 (set-buffer-multibyte t))
325 (defalias 'mm-enable-multibyte 'ignore))
328 (defun mm-disable-multibyte ()
329 "Unset the multibyte flag of in the current buffer.
330 This is a no-op in XEmacs."
331 (set-buffer-multibyte nil))
332 (defalias 'mm-disable-multibyte 'ignore))
335 (defun mm-enable-multibyte-mule4 ()
336 "Enable multibyte in the current buffer.
337 Only used in Emacs Mule 4."
338 (set-buffer-multibyte t))
339 (defalias 'mm-enable-multibyte-mule4 'ignore))
342 (defun mm-disable-multibyte-mule4 ()
343 "Disable multibyte in the current buffer.
344 Only used in Emacs Mule 4."
345 (set-buffer-multibyte nil))
346 (defalias 'mm-disable-multibyte-mule4 'ignore)))
348 (defun mm-preferred-coding-system (charset)
349 ;; A typo in some Emacs versions.
350 (or (get-charset-property charset 'prefered-coding-system)
351 (get-charset-property charset 'preferred-coding-system)))
353 (defun mm-charset-after (&optional pos)
354 "Return charset of a character in current buffer at position POS.
355 If POS is nil, it defauls to the current point.
356 If POS is out of range, the value is nil.
357 If the charset is `composition', return the actual one."
358 (let ((char (char-after pos)) charset)
359 (if (< (mm-char-int char) 128)
360 (setq charset 'ascii)
361 ;; charset-after is fake in some Emacsen.
362 (setq charset (and (fboundp 'char-charset) (char-charset char)))
363 (if (eq charset 'composition)
364 (let ((p (or pos (point))))
365 (cadr (find-charset-region p (1+ p))))
366 (if (and charset (not (memq charset '(ascii eight-bit-control
367 eight-bit-graphic))))
370 mail-parse-mule-charset ;; cached mule-charset
372 (setq mail-parse-mule-charset
373 (and (boundp 'current-language-environment)
376 (assoc current-language-environment
377 language-info-alist))))))
378 (if (or (not mail-parse-mule-charset)
379 (eq mail-parse-mule-charset 'ascii))
380 (setq mail-parse-mule-charset
381 (or (car (last (assq mail-parse-charset
382 mm-mime-mule-charset-alist)))
383 ;; Fixme: don't fix that!
385 mail-parse-mule-charset)))))))
387 (defun mm-mime-charset (charset)
388 "Return the MIME charset corresponding to the given Mule CHARSET."
389 (if (eq charset 'unknown)
390 (error "The message contains non-printable characters, please use attachment"))
391 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
392 ;; This exists in Emacs 20.
394 (and (mm-preferred-coding-system charset)
396 (mm-preferred-coding-system charset) 'mime-charset))
397 (and (eq charset 'ascii)
399 (mm-preferred-coding-system charset)
400 (mm-mule-charset-to-mime-charset charset))
401 ;; This is for XEmacs.
402 (mm-mule-charset-to-mime-charset charset)))
404 (defun mm-delete-duplicates (list)
405 "Simple substitute for CL `delete-duplicates', testing with `equal'."
408 (setq head (car list))
409 (setq list (delete head list))
410 (setq result (cons head result)))
413 ;; It's not clear whether this is supposed to mean the global or local
414 ;; setting. I think it's used inconsistently. -- fx
415 (defsubst mm-multibyte-p ()
416 "Say whether multibyte is enabled."
417 (if (and (not (featurep 'xemacs))
418 (boundp 'enable-multibyte-characters))
419 enable-multibyte-characters
422 (defun mm-find-mime-charset-region (b e)
423 "Return the MIME charsets needed to encode the region between B and E.
424 Nil means ASCII, a single-element list represents an appropriate MIME
425 charset, and a longer list means no appropriate charset."
426 ;; The return possibilities of this function are a mess...
429 (fboundp 'find-coding-systems-region)
430 ;; Find the mime-charset of the most preferred coding
431 ;; system that has one.
432 (let ((systems (find-coding-systems-region b e))
434 ;; Fixme: The `mime-charset' (`x-ctext') of `compound-text'
435 ;; is not in the IANA list.
436 (setq systems (delq 'compound-text systems))
437 (unless (equal systems '(undecided))
439 (let ((cs (coding-system-get (pop systems) 'mime-charset)))
442 result (list cs))))))
444 ;; Otherwise we're not multibyte, XEmacs or a single coding
445 ;; system won't cover it.
447 (mm-delete-duplicates
448 (mapcar 'mm-mime-charset
450 (mm-find-charset-region b e))))))
451 (if (memq 'iso-2022-jp-2 charsets)
452 (delq 'iso-2022-jp charsets)
455 (defmacro mm-with-unibyte-buffer (&rest forms)
456 "Create a temporary buffer, and evaluate FORMS there like `progn'.
457 Use unibyte mode for this."
458 `(let (default-enable-multibyte-characters)
459 (with-temp-buffer ,@forms)))
460 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
461 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
463 (defmacro mm-with-unibyte-current-buffer (&rest forms)
464 "Evaluate FORMS with current current buffer temporarily made unibyte.
465 Also bind `default-enable-multibyte-characters' to nil.
466 Equivalent to `progn' in XEmacs"
467 (let ((multibyte (make-symbol "multibyte"))
468 (buffer (make-symbol "buffer")))
470 (let ((,multibyte enable-multibyte-characters)
471 (,buffer (current-buffer)))
473 (let (default-enable-multibyte-characters)
474 (set-buffer-multibyte nil)
477 (set-buffer-multibyte ,multibyte)))
478 (let (default-enable-multibyte-characters)
480 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
481 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
483 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
484 "Evaluate FORMS there like `progn' in current buffer.
486 (let ((multibyte (make-symbol "multibyte"))
487 (buffer (make-symbol "buffer")))
489 (let ((,multibyte enable-multibyte-characters)
490 (,buffer (current-buffer)))
492 (let (default-enable-multibyte-characters)
493 (set-buffer-multibyte nil)
496 (set-buffer-multibyte ,multibyte)))
497 (let (default-enable-multibyte-characters)
499 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
500 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
502 (defmacro mm-with-unibyte (&rest forms)
503 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
504 `(let (default-enable-multibyte-characters)
506 (put 'mm-with-unibyte 'lisp-indent-function 0)
507 (put 'mm-with-unibyte 'edebug-form-spec '(body))
509 (defun mm-find-charset-region (b e)
510 "Return a list of Emacs charsets in the region B to E."
512 ((and (mm-multibyte-p)
513 (fboundp 'find-charset-region))
514 ;; Remove composition since the base charsets have been included.
515 ;; Remove eight-bit-*, treat them as ascii.
516 (let ((css (find-charset-region b e)))
517 (mapcar (lambda (cs) (setq css (delq cs css)))
518 '(composition eight-bit-control eight-bit-graphic
522 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
525 (narrow-to-region b e)
526 (goto-char (point-min))
527 (skip-chars-forward "\0-\177")
532 (and (boundp 'current-language-environment)
533 (car (last (assq 'charset
534 (assoc current-language-environment
535 language-info-alist))))))
536 (if (eq charset 'ascii) (setq charset nil))
539 (car (last (assq mail-parse-charset
540 mm-mime-mule-charset-alist)))))
541 (list 'ascii (or charset 'latin-iso8859-1)))))))))
543 (if (fboundp 'shell-quote-argument)
544 (defalias 'mm-quote-arg 'shell-quote-argument)
545 (defun mm-quote-arg (arg)
546 "Return a version of ARG that is safe to evaluate in a shell."
547 (let ((pos 0) new-pos accum)
548 ;; *** bug: we don't handle newline characters properly
549 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
550 (push (substring arg pos new-pos) accum)
552 (push (list (aref arg new-pos)) accum)
553 (setq pos (1+ new-pos)))
556 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
558 (defun mm-auto-mode-alist ()
559 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
560 (let ((alist auto-mode-alist)
563 (when (listp (cdar alist))
564 (push (car alist) out))
568 (defvar mm-inhibit-file-name-handlers
569 '(jka-compr-handler image-file-handler)
570 "A list of handlers doing (un)compression (etc) thingies.")
572 (defun mm-insert-file-contents (filename &optional visit beg end replace
574 "Like `insert-file-contents', q.v., but only reads in the file.
575 A buffer may be modified in several ways after reading into the buffer due
576 to advanced Emacs features, such as file-name-handlers, format decoding,
577 find-file-hooks, etc.
578 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
579 This function ensures that none of these modifications will take place."
580 (let ((format-alist nil)
581 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
582 (default-major-mode 'fundamental-mode)
583 (enable-local-variables nil)
584 (after-insert-file-functions nil)
585 (enable-local-eval nil)
586 (find-file-hooks nil)
587 (inhibit-file-name-operation (if inhibit
588 'insert-file-contents
589 inhibit-file-name-operation))
590 (inhibit-file-name-handlers
592 (append mm-inhibit-file-name-handlers
593 inhibit-file-name-handlers)
594 inhibit-file-name-handlers)))
595 (insert-file-contents filename visit beg end replace)))
597 (defun mm-append-to-file (start end filename &optional codesys inhibit)
598 "Append the contents of the region to the end of file FILENAME.
599 When called from a function, expects three arguments,
600 START, END and FILENAME. START and END are buffer positions
601 saying what text to write.
602 Optional fourth argument specifies the coding system to use when
604 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
605 (let ((coding-system-for-write
606 (or codesys mm-text-coding-system-for-write
607 mm-text-coding-system))
608 (inhibit-file-name-operation (if inhibit
610 inhibit-file-name-operation))
611 (inhibit-file-name-handlers
613 (append mm-inhibit-file-name-handlers
614 inhibit-file-name-handlers)
615 inhibit-file-name-handlers)))
616 (append-to-file start end filename)))
618 (defun mm-write-region (start end filename &optional append visit lockname
619 coding-system inhibit)
621 "Like `write-region'.
622 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
623 (let ((coding-system-for-write
624 (or coding-system mm-text-coding-system-for-write
625 mm-text-coding-system))
626 (inhibit-file-name-operation (if inhibit
628 inhibit-file-name-operation))
629 (inhibit-file-name-handlers
631 (append mm-inhibit-file-name-handlers
632 inhibit-file-name-handlers)
633 inhibit-file-name-handlers)))
634 (write-region start end filename append visit lockname)))
636 (defun mm-image-load-path (&optional package)
638 (dolist (path load-path (nreverse result))
639 (if (file-directory-p
640 (setq dir (concat (file-name-directory
641 (directory-file-name path))
642 "etc/" (or package "gnus/"))))
644 (push path result))))
648 ;;; mm-util.el ends here