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))
28 (eval-when-compile (require 'gnus-clfns))
29 (eval-when-compile (require 'static))
36 (let ((nfunc (intern (format "mm-%s" (car elem)))))
37 (if (fboundp (car elem))
38 (defalias nfunc (car elem))
39 (defalias nfunc (cdr elem)))))
40 '((decode-coding-string . (lambda (s a) s))
41 (encode-coding-string . (lambda (s a) s))
42 (encode-coding-region . ignore)
43 (coding-system-list . ignore)
44 (decode-coding-region . ignore)
46 (device-type . ignore)
47 (coding-system-equal . equal)
48 (annotationp . ignore)
49 (set-buffer-file-coding-system . ignore)
51 . (lambda (charset int)
59 (mapcar (lambda (e) (list (symbol-name (car e))))
60 mm-mime-mule-charset-alist)
63 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
64 "Replace characters in STRING from FROM to TO."
65 (let ((string (substring string 0)) ;Copy string.
68 ;; Replace all occurrences of FROM with TO.
70 (when (= (aref string idx) from)
74 (string-as-unibyte . identity)
75 (string-as-multibyte . identity)
76 (multibyte-string-p . ignore))))
79 (defalias 'mm-char-or-char-int-p
81 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
82 ((fboundp 'char-valid-p) 'char-valid-p)
86 (defalias 'mm-read-coding-system
88 ((fboundp 'read-coding-system)
89 (if (and (featurep 'xemacs)
90 (<= (string-to-number emacs-version) 21.1))
91 (lambda (prompt &optional default-coding-system)
92 (read-coding-system prompt))
94 (t (lambda (prompt &optional default-coding-system)
95 "Prompt the user for a coding system."
97 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
98 mm-mime-mule-charset-alist)))))))
100 (defvar mm-coding-system-list nil)
101 (defun mm-get-coding-system-list ()
102 "Get the coding system list."
103 (or mm-coding-system-list
104 (setq mm-coding-system-list (mm-coding-system-list))))
106 (defun mm-coding-system-p (sym)
107 "Return non-nil if SYM is a coding system."
108 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
109 (memq sym (mm-get-coding-system-list))))
111 (defvar mm-charset-synonym-alist
113 ;; Perfectly fine? A valid MIME name, anyhow.
114 ,(unless (mm-coding-system-p 'big5)
116 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
117 ,(unless (mm-coding-system-p 'x-ctext)
119 ;; Apparently not defined in Emacs 20, but is a valid MIME name.
120 ,(unless (mm-coding-system-p 'gb2312)
121 '(gb2312 . cn-gb-2312))
122 ;; Windows-1252 is actually a superset of Latin-1. See also
123 ;; `gnus-article-dumbquotes-map'.
124 ;;,(unless (mm-coding-system-p 'windows-1252)
125 ; should be defined eventually
126 ;; '(windows-1252 . iso-8859-1))
127 ;; ISO-8859-15 is very similar to ISO-8859-1.
128 ;;,(unless (mm-coding-system-p 'iso-8859-15) ; Emacs 21 defines it.
129 ;; '(iso-8859-15 . iso-8859-1))
130 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
131 ;; Outlook users in Czech republic. Use this to allow reading of their
132 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
133 ;;,(unless (mm-coding-system-p 'windows-1250)
134 ; should be defined eventually
135 ;; '(windows-1250 . cp1250))
137 "A mapping from invalid charset names to the real charset names.")
139 (defvar mm-binary-coding-system
141 ((mm-coding-system-p 'binary) 'binary)
142 ((mm-coding-system-p 'no-conversion) 'no-conversion)
144 "100% binary coding system.")
146 (defvar mm-text-coding-system
147 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
148 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
149 (and (mm-coding-system-p 'raw-text) 'raw-text))
150 mm-binary-coding-system)
151 "Text-safe coding system (For removing ^M).")
153 (defvar mm-text-coding-system-for-write nil
154 "Text coding system for write.")
156 (defvar mm-auto-save-coding-system
158 ((mm-coding-system-p 'emacs-mule)
159 (if (memq system-type '(windows-nt ms-dos ms-windows))
160 (if (mm-coding-system-p 'emacs-mule-dos)
161 'emacs-mule-dos mm-binary-coding-system)
163 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
164 (t mm-binary-coding-system))
165 "Coding system of auto save file.")
167 (defvar mm-universal-coding-system mm-auto-save-coding-system
168 "The universal Coding system.")
170 ;; Fixme: some of the cars here aren't valid MIME charsets. That
171 ;; should only matter with XEmacs, though.
172 (defvar mm-mime-mule-charset-alist
174 (iso-8859-1 latin-iso8859-1)
175 (iso-8859-2 latin-iso8859-2)
176 (iso-8859-3 latin-iso8859-3)
177 (iso-8859-4 latin-iso8859-4)
178 (iso-8859-5 cyrillic-iso8859-5)
179 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
180 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
181 ;; charset is koi8-r, not iso-8859-5.
182 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
183 (iso-8859-6 arabic-iso8859-6)
184 (iso-8859-7 greek-iso8859-7)
185 (iso-8859-8 hebrew-iso8859-8)
186 (iso-8859-9 latin-iso8859-9)
187 (iso-8859-14 latin-iso8859-14)
188 (iso-8859-15 latin-iso8859-15)
189 (viscii vietnamese-viscii-lower)
190 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
191 (euc-kr korean-ksc5601)
192 (gb2312 chinese-gb2312)
193 (big5 chinese-big5-1 chinese-big5-2)
195 (thai-tis620 thai-tis620)
196 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
197 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
198 latin-jisx0201 japanese-jisx0208-1978
199 chinese-gb2312 japanese-jisx0208
200 korean-ksc5601 japanese-jisx0212
202 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
203 latin-jisx0201 japanese-jisx0208-1978
204 chinese-gb2312 japanese-jisx0208
205 korean-ksc5601 japanese-jisx0212
206 chinese-cns11643-1 chinese-cns11643-2)
207 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
208 cyrillic-iso8859-5 greek-iso8859-7
209 latin-jisx0201 japanese-jisx0208-1978
210 chinese-gb2312 japanese-jisx0208
211 korean-ksc5601 japanese-jisx0212
212 chinese-cns11643-1 chinese-cns11643-2
213 chinese-cns11643-3 chinese-cns11643-4
214 chinese-cns11643-5 chinese-cns11643-6
216 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
217 (charsetp 'unicode-a)
218 (not (mm-coding-system-p 'mule-utf-8)))
219 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
220 ;; If we have utf-8 we're in Mule 5+.
223 (coding-system-get 'mule-utf-8 'safe-charsets)))))
224 "Alist of MIME-charset/MULE-charsets.")
226 ;; Correct by construction, but should be unnecessary:
228 (when (and (not (featurep 'xemacs))
229 (fboundp 'coding-system-list)
230 (fboundp 'sort-coding-systems))
231 (setq mm-mime-mule-charset-alist
236 (when (and (coding-system-get cs 'mime-charset)
237 (not (eq t (coding-system-get cs 'safe-charsets))))
238 (list (cons (coding-system-get cs 'mime-charset)
240 (coding-system-get cs 'safe-charsets))))))
241 (sort-coding-systems (coding-system-list 'base-only))))))
243 ;;; Internal variables:
247 (defun mm-mule-charset-to-mime-charset (charset)
248 "Return the MIME charset corresponding to the given Mule CHARSET."
249 (if (fboundp 'find-coding-systems-for-charsets)
251 (dolist (cs (find-coding-systems-for-charsets (list charset)))
254 (setq mime (coding-system-get cs 'mime-charset)))))
256 (let ((alist mm-mime-mule-charset-alist)
259 (when (memq charset (cdar alist))
260 (setq out (caar alist)
265 (defun mm-charset-to-coding-system (charset &optional lbt)
266 "Return coding-system corresponding to CHARSET.
267 CHARSET is a symbol naming a MIME charset.
268 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
269 used as the line break code type of the coding system."
270 (when (stringp charset)
271 (setq charset (intern (downcase charset))))
273 (setq charset (intern (format "%s-%s" charset lbt))))
275 ;; Running in a non-MULE environment.
276 ((null (mm-get-coding-system-list))
279 ((eq charset 'us-ascii)
281 ;; Check to see whether we can handle this charset. (This depends
282 ;; on there being some coding system matching each `mime-charset'
283 ;; property defined, as there should be.)
284 ((and (mm-coding-system-p charset)
285 ;;; Doing this would potentially weed out incorrect charsets.
287 ;;; (eq charset (coding-system-get charset 'mime-charset))
290 ;; Translate invalid charsets.
291 ((mm-coding-system-p (setq charset
293 mm-charset-synonym-alist))))
295 ;; Last resort: search the coding system list for entries which
296 ;; have the right mime-charset in case the canonical name isn't
297 ;; defined (though it should be).
299 ;; mm-get-coding-system-list returns a list of cs without lbt.
301 (dolist (c (mm-get-coding-system-list))
303 (eq charset (coding-system-get c 'mime-charset)))
307 (defsubst mm-replace-chars-in-string (string from to)
308 (mm-subst-char-in-string from to string))
311 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
312 (boundp 'default-enable-multibyte-characters)
313 default-enable-multibyte-characters
314 (fboundp 'set-buffer-multibyte))
317 (defvar mm-mule4-p (and mm-emacs-mule
319 (not (charsetp 'eight-bit-control)))
323 (defun mm-enable-multibyte ()
324 "Set the multibyte flag of the current buffer.
325 Only do this if the default value of `enable-multibyte-characters' is
326 non-nil. This is a no-op in XEmacs."
327 (set-buffer-multibyte t))
328 (defalias 'mm-enable-multibyte 'ignore))
331 (defun mm-disable-multibyte ()
332 "Unset the multibyte flag of in the current buffer.
333 This is a no-op in XEmacs."
334 (set-buffer-multibyte nil))
335 (defalias 'mm-disable-multibyte 'ignore))
338 (defun mm-enable-multibyte-mule4 ()
339 "Enable multibyte in the current buffer.
340 Only used in Emacs Mule 4."
341 (set-buffer-multibyte t))
342 (defalias 'mm-enable-multibyte-mule4 'ignore))
345 (defun mm-disable-multibyte-mule4 ()
346 "Disable multibyte in the current buffer.
347 Only used in Emacs Mule 4."
348 (set-buffer-multibyte nil))
349 (defalias 'mm-disable-multibyte-mule4 'ignore)))
351 (defun mm-preferred-coding-system (charset)
352 ;; A typo in some Emacs versions.
353 (or (get-charset-property charset 'prefered-coding-system)
354 (get-charset-property charset 'preferred-coding-system)))
356 (defun mm-charset-after (&optional pos)
357 "Return charset of a character in current buffer at position POS.
358 If POS is nil, it defauls to the current point.
359 If POS is out of range, the value is nil.
360 If the charset is `composition', return the actual one."
361 (let ((char (char-after pos)) charset)
362 (if (< (mm-char-int char) 128)
363 (setq charset 'ascii)
364 ;; charset-after is fake in some Emacsen.
365 (setq charset (and (fboundp 'char-charset) (char-charset char)))
366 (if (eq charset 'composition)
367 (let ((p (or pos (point))))
368 (cadr (find-charset-region p (1+ p))))
369 (if (and charset (not (memq charset '(ascii eight-bit-control
370 eight-bit-graphic))))
373 mail-parse-mule-charset ;; cached mule-charset
375 (setq mail-parse-mule-charset
376 (and (boundp 'current-language-environment)
379 (assoc current-language-environment
380 language-info-alist))))))
381 (if (or (not mail-parse-mule-charset)
382 (eq mail-parse-mule-charset 'ascii))
383 (setq mail-parse-mule-charset
384 (or (car (last (assq mail-parse-charset
385 mm-mime-mule-charset-alist)))
386 ;; Fixme: don't fix that!
388 mail-parse-mule-charset)))))))
390 (defun mm-mime-charset (charset)
391 "Return the MIME charset corresponding to the given Mule CHARSET."
392 (if (eq charset 'unknown)
393 (error "The message contains non-printable characters, please use attachment"))
394 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
395 ;; This exists in Emacs 20.
397 (and (mm-preferred-coding-system charset)
399 (mm-preferred-coding-system charset) 'mime-charset))
400 (and (eq charset 'ascii)
402 (mm-preferred-coding-system charset)
403 (mm-mule-charset-to-mime-charset charset))
404 ;; This is for XEmacs.
405 (mm-mule-charset-to-mime-charset charset)))
407 (defun mm-delete-duplicates (list)
408 "Simple substitute for CL `delete-duplicates', testing with `equal'."
411 (setq head (car list))
412 (setq list (delete head list))
413 (setq result (cons head result)))
416 ;; It's not clear whether this is supposed to mean the global or local
417 ;; setting. I think it's used inconsistently. -- fx
418 (defsubst mm-multibyte-p ()
419 "Say whether multibyte is enabled."
420 (if (and (not (featurep 'xemacs))
421 (boundp 'enable-multibyte-characters))
422 enable-multibyte-characters
425 (defun mm-find-mime-charset-region (b e)
426 "Return the MIME charsets needed to encode the region between B and E.
427 Nil means ASCII, a single-element list represents an appropriate MIME
428 charset, and a longer list means no appropriate charset."
429 ;; The return possibilities of this function are a mess...
432 (fboundp 'find-coding-systems-region)
433 ;; Find the mime-charset of the most preferred coding
434 ;; system that has one.
435 (let ((systems (find-coding-systems-region b e))
437 ;; Fixme: The `mime-charset' (`x-ctext') of `compound-text'
438 ;; is not in the IANA list.
439 (setq systems (delq 'compound-text systems))
440 (unless (equal systems '(undecided))
442 (let ((cs (coding-system-get (pop systems) 'mime-charset)))
445 result (list cs))))))
447 ;; Otherwise we're not multibyte, XEmacs or a single coding
448 ;; system won't cover it.
450 (mm-delete-duplicates
451 (mapcar 'mm-mime-charset
453 (mm-find-charset-region b e))))))
454 (if (memq 'iso-2022-jp-2 charsets)
455 (delq 'iso-2022-jp charsets)
458 (defmacro mm-with-unibyte-buffer (&rest forms)
459 "Create a temporary buffer, and evaluate FORMS there like `progn'.
460 Use unibyte mode for this."
461 `(let (default-enable-multibyte-characters)
462 (with-temp-buffer ,@forms)))
463 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
464 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
466 (defmacro mm-with-unibyte-current-buffer (&rest forms)
467 "Evaluate FORMS with current current buffer temporarily made unibyte.
468 Also bind `default-enable-multibyte-characters' to nil.
469 Equivalent to `progn' in XEmacs"
470 (let ((multibyte (make-symbol "multibyte"))
471 (buffer (make-symbol "buffer")))
473 (let ((,multibyte enable-multibyte-characters)
474 (,buffer (current-buffer)))
476 (let (default-enable-multibyte-characters)
477 (set-buffer-multibyte nil)
480 (set-buffer-multibyte ,multibyte)))
481 (let (default-enable-multibyte-characters)
483 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
484 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
486 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
487 "Evaluate FORMS there like `progn' in current buffer.
489 (let ((multibyte (make-symbol "multibyte"))
490 (buffer (make-symbol "buffer")))
492 (let ((,multibyte enable-multibyte-characters)
493 (,buffer (current-buffer)))
495 (let (default-enable-multibyte-characters)
496 (set-buffer-multibyte nil)
499 (set-buffer-multibyte ,multibyte)))
500 (let (default-enable-multibyte-characters)
502 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
503 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
505 (defmacro mm-with-unibyte (&rest forms)
506 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
507 `(let (default-enable-multibyte-characters)
509 (put 'mm-with-unibyte 'lisp-indent-function 0)
510 (put 'mm-with-unibyte 'edebug-form-spec '(body))
512 (defun mm-find-charset-region (b e)
513 "Return a list of Emacs charsets in the region B to E."
515 ((and (mm-multibyte-p)
516 (fboundp 'find-charset-region))
517 ;; Remove composition since the base charsets have been included.
518 ;; Remove eight-bit-*, treat them as ascii.
519 (let ((css (find-charset-region b e)))
520 (mapcar (lambda (cs) (setq css (delq cs css)))
521 '(composition eight-bit-control eight-bit-graphic
525 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
528 (narrow-to-region b e)
529 (goto-char (point-min))
530 (skip-chars-forward "\0-\177")
535 (and (boundp 'current-language-environment)
536 (car (last (assq 'charset
537 (assoc current-language-environment
538 language-info-alist))))))
539 (if (eq charset 'ascii) (setq charset nil))
542 (car (last (assq mail-parse-charset
543 mm-mime-mule-charset-alist)))))
544 (list 'ascii (or charset 'latin-iso8859-1)))))))))
546 (static-if (fboundp 'shell-quote-argument)
547 (defalias 'mm-quote-arg 'shell-quote-argument)
548 (defun mm-quote-arg (arg)
549 "Return a version of ARG that is safe to evaluate in a shell."
550 (let ((pos 0) new-pos accum)
551 ;; *** bug: we don't handle newline characters properly
552 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
553 (push (substring arg pos new-pos) accum)
555 (push (list (aref arg new-pos)) accum)
556 (setq pos (1+ new-pos)))
559 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
561 (defun mm-auto-mode-alist ()
562 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
563 (let ((alist auto-mode-alist)
566 (when (listp (cdar alist))
567 (push (car alist) out))
571 (defvar mm-inhibit-file-name-handlers
572 '(jka-compr-handler image-file-handler)
573 "A list of handlers doing (un)compression (etc) thingies.")
575 (defun mm-insert-file-contents (filename &optional visit beg end replace
577 "Like `insert-file-contents', q.v., but only reads in the file.
578 A buffer may be modified in several ways after reading into the buffer due
579 to advanced Emacs features, such as file-name-handlers, format decoding,
580 find-file-hooks, etc.
581 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
582 This function ensures that none of these modifications will take place."
583 (let ((format-alist nil)
584 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
585 (default-major-mode 'fundamental-mode)
586 (enable-local-variables nil)
587 (after-insert-file-functions nil)
588 (enable-local-eval nil)
589 (find-file-hooks nil)
590 (inhibit-file-name-operation (if inhibit
591 'insert-file-contents
592 inhibit-file-name-operation))
593 (inhibit-file-name-handlers
595 (append mm-inhibit-file-name-handlers
596 inhibit-file-name-handlers)
597 inhibit-file-name-handlers)))
598 (insert-file-contents filename visit beg end replace)))
600 (defun mm-append-to-file (start end filename &optional codesys inhibit)
601 "Append the contents of the region to the end of file FILENAME.
602 When called from a function, expects three arguments,
603 START, END and FILENAME. START and END are buffer positions
604 saying what text to write.
605 Optional fourth argument specifies the coding system to use when
607 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
608 (let ((coding-system-for-write
609 (or codesys mm-text-coding-system-for-write
610 mm-text-coding-system))
611 (inhibit-file-name-operation (if inhibit
613 inhibit-file-name-operation))
614 (inhibit-file-name-handlers
616 (append mm-inhibit-file-name-handlers
617 inhibit-file-name-handlers)
618 inhibit-file-name-handlers)))
619 (append-to-file start end filename)))
621 (defun mm-write-region (start end filename &optional append visit lockname
622 coding-system inhibit)
624 "Like `write-region'.
625 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
626 (let ((coding-system-for-write
627 (or coding-system mm-text-coding-system-for-write
628 mm-text-coding-system))
629 (inhibit-file-name-operation (if inhibit
631 inhibit-file-name-operation))
632 (inhibit-file-name-handlers
634 (append mm-inhibit-file-name-handlers
635 inhibit-file-name-handlers)
636 inhibit-file-name-handlers)))
637 (write-region start end filename append visit lockname)))
639 (defun mm-image-load-path (&optional package)
641 (dolist (path load-path (nreverse result))
642 (if (file-directory-p
643 (setq dir (concat (file-name-directory
644 (directory-file-name path))
645 "etc/" (or package "gnus/"))))
647 (push path result))))
651 ;;; mm-util.el ends here