1 ;;; mm-util.el --- Utility functions for Mule and low level things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
37 (let ((nfunc (intern (format "mm-%s" (car elem)))))
38 (if (fboundp (car elem))
39 (defalias nfunc (car elem))
40 (defalias nfunc (cdr elem)))))
41 '((decode-coding-string . (lambda (s a) s))
42 (encode-coding-string . (lambda (s a) s))
43 (encode-coding-region . ignore)
44 (coding-system-list . ignore)
45 (decode-coding-region . ignore)
47 (coding-system-equal . equal)
48 (annotationp . ignore)
49 (set-buffer-file-coding-system . ignore)
56 (mapcar (lambda (e) (list (symbol-name (car e))))
57 mm-mime-mule-charset-alist)
60 . (lambda (from to string &optional inplace) ;; stolen (and renamed) from nnheader.el
61 "Replace characters in STRING from FROM to TO.
62 Unless optional argument INPLACE is non-nil, return a new string."
63 (let ((string (if inplace string (copy-sequence string)))
66 ;; Replace all occurrences of FROM with TO.
68 (when (= (aref string idx) from)
72 (string-as-unibyte . identity)
73 (string-make-unibyte . identity)
74 (string-as-multibyte . identity)
75 (multibyte-string-p . ignore)
76 ;; It is not a MIME function, but some MIME functions use it.
77 (make-temp-file . (lambda (prefix &optional dir-flag)
78 (let ((file (expand-file-name
79 (make-temp-name prefix)
80 (if (fboundp 'temp-directory)
82 temporary-file-directory))))
84 (make-directory file))
86 (insert-byte . insert-char)
87 (multibyte-char-to-unibyte . identity))))
90 (defalias 'mm-char-or-char-int-p
92 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
93 ((fboundp 'char-valid-p) 'char-valid-p)
96 ;; Fixme: This seems always to be used to read a MIME charset, so it
97 ;; should be re-named and fixed (in Emacs) to offer completion only on
98 ;; proper charset names (base coding systems which have a
99 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
101 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
102 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
103 ;; Actually, there should be an `mm-coding-system-mime-charset'.
105 (defalias 'mm-read-coding-system
107 ((fboundp 'read-coding-system)
108 (if (and (featurep 'xemacs)
109 (<= (string-to-number emacs-version) 21.1))
110 (lambda (prompt &optional default-coding-system)
111 (read-coding-system prompt))
112 'read-coding-system))
113 (t (lambda (prompt &optional default-coding-system)
114 "Prompt the user for a coding system."
116 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
117 mm-mime-mule-charset-alist)))))))
119 (defvar mm-coding-system-list nil)
120 (defun mm-get-coding-system-list ()
121 "Get the coding system list."
122 (or mm-coding-system-list
123 (setq mm-coding-system-list (mm-coding-system-list))))
125 (defun mm-coding-system-p (cs)
126 "Return non-nil if CS is a symbol naming a coding system.
127 In XEmacs, also return non-nil if CS is a coding system object."
128 (if (fboundp 'find-coding-system)
129 (find-coding-system cs)
130 (if (fboundp 'coding-system-p)
132 ;; Is this branch ever actually useful?
133 (memq cs (mm-get-coding-system-list)))))
135 (defvar mm-charset-synonym-alist
137 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
138 ,@(unless (mm-coding-system-p 'x-ctext)
139 '((x-ctext . ctext)))
140 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
141 ,@(unless (mm-coding-system-p 'iso-8859-15)
142 '((iso-8859-15 . iso-8859-1)))
143 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
144 ,@(unless (mm-coding-system-p 'big5-hkscs)
145 '((big5-hkscs . big5)))
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)
149 (if (mm-coding-system-p 'cp1252)
150 '((windows-1252 . cp1252))
151 '((windows-1252 . iso-8859-1))))
152 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
153 ;; Outlook users in Czech republic. Use this to allow reading of their
154 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
155 ,@(if (and (not (mm-coding-system-p 'windows-1250))
156 (mm-coding-system-p 'cp1250))
157 '((windows-1250 . cp1250)))
158 ;; A Microsoft misunderstanding.
159 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
160 (if (mm-coding-system-p 'cp949)
161 '((ks_c_5601-1987 . cp949))
162 '((ks_c_5601-1987 . euc-kr))))
164 "A mapping from invalid charset names to the real charset names.")
166 (defvar mm-binary-coding-system
168 ((mm-coding-system-p 'binary) 'binary)
169 ((mm-coding-system-p 'no-conversion) 'no-conversion)
171 "100% binary coding system.")
173 (defvar mm-text-coding-system
174 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
175 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
176 (and (mm-coding-system-p 'raw-text) 'raw-text))
177 mm-binary-coding-system)
178 "Text-safe coding system (For removing ^M).")
180 (defvar mm-text-coding-system-for-write nil
181 "Text coding system for write.")
183 (defvar mm-auto-save-coding-system
185 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
186 (if (memq system-type '(windows-nt ms-dos ms-windows))
187 (if (mm-coding-system-p 'utf-8-emacs-dos)
188 'utf-8-emacs-dos mm-binary-coding-system)
190 ((mm-coding-system-p 'emacs-mule)
191 (if (memq system-type '(windows-nt ms-dos ms-windows))
192 (if (mm-coding-system-p 'emacs-mule-dos)
193 'emacs-mule-dos mm-binary-coding-system)
195 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
196 (t mm-binary-coding-system))
197 "Coding system of auto save file.")
199 (defvar mm-universal-coding-system mm-auto-save-coding-system
200 "The universal coding system.")
202 ;; Fixme: some of the cars here aren't valid MIME charsets. That
203 ;; should only matter with XEmacs, though.
204 (defvar mm-mime-mule-charset-alist
206 (iso-8859-1 latin-iso8859-1)
207 (iso-8859-2 latin-iso8859-2)
208 (iso-8859-3 latin-iso8859-3)
209 (iso-8859-4 latin-iso8859-4)
210 (iso-8859-5 cyrillic-iso8859-5)
211 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
212 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
213 ;; charset is koi8-r, not iso-8859-5.
214 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
215 (iso-8859-6 arabic-iso8859-6)
216 (iso-8859-7 greek-iso8859-7)
217 (iso-8859-8 hebrew-iso8859-8)
218 (iso-8859-9 latin-iso8859-9)
219 (iso-8859-14 latin-iso8859-14)
220 (iso-8859-15 latin-iso8859-15)
221 (viscii vietnamese-viscii-lower)
222 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
223 (euc-kr korean-ksc5601)
224 (gb2312 chinese-gb2312)
225 (big5 chinese-big5-1 chinese-big5-2)
227 (thai-tis620 thai-tis620)
228 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
229 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
230 latin-jisx0201 japanese-jisx0208-1978
231 chinese-gb2312 japanese-jisx0208
232 korean-ksc5601 japanese-jisx0212
234 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
235 latin-jisx0201 japanese-jisx0208-1978
236 chinese-gb2312 japanese-jisx0208
237 korean-ksc5601 japanese-jisx0212
238 chinese-cns11643-1 chinese-cns11643-2)
239 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
240 cyrillic-iso8859-5 greek-iso8859-7
241 latin-jisx0201 japanese-jisx0208-1978
242 chinese-gb2312 japanese-jisx0208
243 korean-ksc5601 japanese-jisx0212
244 chinese-cns11643-1 chinese-cns11643-2
245 chinese-cns11643-3 chinese-cns11643-4
246 chinese-cns11643-5 chinese-cns11643-6
248 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
249 (charsetp 'unicode-a)
250 (not (mm-coding-system-p 'mule-utf-8)))
251 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
252 ;; If we have utf-8 we're in Mule 5+.
255 (coding-system-get 'mule-utf-8 'safe-charsets)))))
256 "Alist of MIME-charset/MULE-charsets.")
258 ;; Correct by construction, but should be unnecessary:
260 (when (and (not (featurep 'xemacs))
261 (fboundp 'coding-system-list)
262 (fboundp 'sort-coding-systems))
263 (setq mm-mime-mule-charset-alist
268 (when (and (or (coding-system-get cs :mime-charset) ; Emacs 22
269 (coding-system-get cs 'mime-charset))
270 (not (eq t (coding-system-get cs 'safe-charsets))))
271 (list (cons (or (coding-system-get cs :mime-charset)
272 (coding-system-get cs 'mime-charset))
274 (coding-system-get cs 'safe-charsets))))))
275 (sort-coding-systems (coding-system-list 'base-only))))))
277 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
278 "A list of special charsets.
279 Valid elements include:
280 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
281 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
284 (defvar mm-iso-8859-15-compatible
285 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
286 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
287 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
289 (defvar mm-iso-8859-x-to-15-table
290 (and (fboundp 'coding-system-p)
291 (mm-coding-system-p 'iso-8859-15)
294 (if (mm-coding-system-p (car cs))
295 (let ((c (string-to-char
296 (decode-coding-string "\341" (car cs)))))
297 (cons (char-charset c)
300 (decode-coding-string "\341" 'iso-8859-15)) c)
301 (string-to-list (decode-coding-string (car (cdr cs))
304 mm-iso-8859-15-compatible))
305 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
307 (defcustom mm-coding-system-priorities
308 (if (boundp 'current-language-environment)
309 (let ((lang (symbol-value 'current-language-environment)))
310 (cond ((string= lang "Japanese")
311 ;; Japanese users may prefer iso-2022-jp to shift-jis.
312 '(iso-2022-jp iso-2022-jp-2 japanese-shift-jis
313 iso-latin-1 utf-8)))))
314 "Preferred coding systems for encoding outgoing messages.
316 More than one suitable coding system may be found for some text.
317 By default, the coding system with the highest priority is used
318 to encode outgoing messages (see `sort-coding-systems'). If this
319 variable is set, it overrides the default priority."
320 :type '(repeat (symbol :tag "Coding system"))
324 (defvar mm-use-find-coding-systems-region
325 (fboundp 'find-coding-systems-region)
326 "Use `find-coding-systems-region' to find proper coding systems.
328 Setting it to nil is useful on Emacsen supporting Unicode if sending
329 mail with multiple parts is preferred to sending a Unicode one.")
331 ;;; Internal variables:
335 (defun mm-mule-charset-to-mime-charset (charset)
336 "Return the MIME charset corresponding to the given Mule CHARSET."
337 (if (and (fboundp 'find-coding-systems-for-charsets)
338 (fboundp 'sort-coding-systems))
340 (dolist (cs (sort-coding-systems
342 (find-coding-systems-for-charsets (list charset)))))
345 (setq mime (or (coding-system-get cs :mime-charset)
346 (coding-system-get cs 'mime-charset))))))
348 (let ((alist mm-mime-mule-charset-alist)
351 (when (memq charset (cdar alist))
352 (setq out (caar alist)
357 (defun mm-charset-to-coding-system (charset &optional lbt)
358 "Return coding-system corresponding to CHARSET.
359 CHARSET is a symbol naming a MIME charset.
360 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
361 used as the line break code type of the coding system."
362 (when (stringp charset)
363 (setq charset (intern (downcase charset))))
365 (setq charset (intern (format "%s-%s" charset lbt))))
369 ;; Running in a non-MULE environment.
370 ((or (null (mm-get-coding-system-list))
371 (not (fboundp 'coding-system-get)))
374 ((eq charset 'us-ascii)
376 ;; Check to see whether we can handle this charset. (This depends
377 ;; on there being some coding system matching each `mime-charset'
378 ;; property defined, as there should be.)
379 ((and (mm-coding-system-p charset)
380 ;;; Doing this would potentially weed out incorrect charsets.
382 ;;; (eq charset (coding-system-get charset 'mime-charset))
385 ;; Translate invalid charsets.
386 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
387 (and cs (mm-coding-system-p cs) cs)))
388 ;; Last resort: search the coding system list for entries which
389 ;; have the right mime-charset in case the canonical name isn't
390 ;; defined (though it should be).
392 ;; mm-get-coding-system-list returns a list of cs without lbt.
394 (dolist (c (mm-get-coding-system-list))
396 (eq charset (or (coding-system-get c :mime-charset)
397 (coding-system-get c 'mime-charset))))
402 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
403 (boundp 'default-enable-multibyte-characters)
404 default-enable-multibyte-characters
405 (fboundp 'set-buffer-multibyte))
406 "True in Emacs with Mule.")
409 (defun mm-enable-multibyte ()
410 "Set the multibyte flag of the current buffer.
411 Only do this if the default value of `enable-multibyte-characters' is
412 non-nil. This is a no-op in XEmacs."
413 (set-buffer-multibyte 'to))
414 (defalias 'mm-enable-multibyte 'ignore))
417 (defun mm-disable-multibyte ()
418 "Unset the multibyte flag of in the current buffer.
419 This is a no-op in XEmacs."
420 (set-buffer-multibyte nil))
421 (defalias 'mm-disable-multibyte 'ignore)))
423 (defun mm-preferred-coding-system (charset)
424 ;; A typo in some Emacs versions.
425 (or (get-charset-property charset 'preferred-coding-system)
426 (get-charset-property charset 'prefered-coding-system)))
428 ;; Mule charsets shouldn't be used.
429 (defsubst mm-guess-charset ()
430 "Guess Mule charset from the language environment."
432 mail-parse-mule-charset ;; cached mule-charset
434 (setq mail-parse-mule-charset
435 (and (boundp 'current-language-environment)
438 (assoc current-language-environment
439 language-info-alist))))))
440 (if (or (not mail-parse-mule-charset)
441 (eq mail-parse-mule-charset 'ascii))
442 (setq mail-parse-mule-charset
443 (or (car (last (assq mail-parse-charset
444 mm-mime-mule-charset-alist)))
447 mail-parse-mule-charset)))
449 (defun mm-charset-after (&optional pos)
450 "Return charset of a character in current buffer at position POS.
451 If POS is nil, it defauls to the current point.
452 If POS is out of range, the value is nil.
453 If the charset is `composition', return the actual one."
454 (let ((char (char-after pos)) charset)
455 (if (< (mm-char-int char) 128)
456 (setq charset 'ascii)
457 ;; charset-after is fake in some Emacsen.
458 (setq charset (and (fboundp 'char-charset) (char-charset char)))
459 (if (eq charset 'composition) ; Mule 4
460 (let ((p (or pos (point))))
461 (cadr (find-charset-region p (1+ p))))
462 (if (and charset (not (memq charset '(ascii eight-bit-control
463 eight-bit-graphic))))
465 (mm-guess-charset))))))
467 (defun mm-mime-charset (charset)
468 "Return the MIME charset corresponding to the given Mule CHARSET."
469 (if (eq charset 'unknown)
470 (error "The message contains non-printable characters, please use attachment"))
471 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
472 ;; This exists in Emacs 20.
474 (and (mm-preferred-coding-system charset)
475 (or (coding-system-get
476 (mm-preferred-coding-system charset) :mime-charset)
478 (mm-preferred-coding-system charset) 'mime-charset)))
479 (and (eq charset 'ascii)
481 (mm-preferred-coding-system charset)
482 (mm-mule-charset-to-mime-charset charset))
483 ;; This is for XEmacs.
484 (mm-mule-charset-to-mime-charset charset)))
486 (defun mm-delete-duplicates (list)
487 "Simple substitute for CL `delete-duplicates', testing with `equal'."
490 (setq head (car list))
491 (setq list (delete head list))
492 (setq result (cons head result)))
495 ;; Fixme: This is used in places when it should be testing the
496 ;; default multibyteness. See mm-default-multibyte-p.
498 (if (and (not (featurep 'xemacs))
499 (boundp 'enable-multibyte-characters))
500 (defun mm-multibyte-p ()
501 "Non-nil if multibyte is enabled in the current buffer."
502 enable-multibyte-characters)
503 (defun mm-multibyte-p () (featurep 'mule))))
505 (defun mm-default-multibyte-p ()
506 "Return non-nil if the session is multibyte.
507 This affects whether coding conversion should be attempted generally."
509 (if (boundp 'default-enable-multibyte-characters)
510 default-enable-multibyte-characters
513 (defun mm-iso-8859-x-to-15-region (&optional b e)
514 (if (fboundp 'char-charset)
515 (let (charset item c inconvertible)
517 (if e (narrow-to-region b e))
518 (goto-char (point-min))
519 (skip-chars-forward "\0-\177")
522 ((not (setq item (assq (char-charset (setq c (char-after)))
523 mm-iso-8859-x-to-15-table)))
525 ((memq c (cdr (cdr item)))
526 (setq inconvertible t)
529 (insert-before-markers (prog1 (+ c (car (cdr item)))
531 (skip-chars-forward "\0-\177")))
532 (not inconvertible))))
534 (defun mm-sort-coding-systems-predicate (a b)
537 ;; Note: invalid entries are dropped silently
538 (and (coding-system-p cs)
539 (coding-system-base cs)))
540 mm-coding-system-priorities)))
541 (> (length (memq a priorities))
542 (length (memq b priorities)))))
544 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
545 "Return the MIME charsets needed to encode the region between B and E.
546 nil means ASCII, a single-element list represents an appropriate MIME
547 charset, and a longer list means no appropriate charset."
549 ;; The return possibilities of this function are a mess...
550 (or (and (mm-multibyte-p)
551 mm-use-find-coding-systems-region
552 ;; Find the mime-charset of the most preferred coding
553 ;; system that has one.
554 (let ((systems (find-coding-systems-region b e)))
555 (when mm-coding-system-priorities
557 (sort systems 'mm-sort-coding-systems-predicate)))
558 (setq systems (delq 'compound-text systems))
559 (unless (equal systems '(undecided))
561 (let* ((head (pop systems))
562 (cs (or (coding-system-get head :mime-charset)
563 (coding-system-get head 'mime-charset))))
564 ;; The mime-charset (`x-ctext') of
565 ;; `compound-text' is not in the IANA list. We
566 ;; shouldn't normally use anything here with a
567 ;; mime-charset having an `x-' prefix.
568 ;; Fixme: Allow this to be overridden, since
569 ;; there is existing use of x-ctext.
570 ;; Also people apparently need the coding system
571 ;; `iso-2022-jp-3' (which Mule-UCS defines with
572 ;; mime-charset, though it's not valid).
574 (not (string-match "^[Xx]-" (symbol-name cs)))
575 ;; UTF-16 of any variety is invalid for
576 ;; text parts and, unfortunately, has
577 ;; mime-charset defined both in Mule-UCS
578 ;; and versions of Emacs. (The name
579 ;; might be `mule-utf-16...' or
581 (not (string-match "utf-16" (symbol-name cs))))
583 charsets (list cs))))))
585 ;; Otherwise we're not multibyte, we're XEmacs, or a single
586 ;; coding system won't cover it.
588 (mm-delete-duplicates
589 (mapcar 'mm-mime-charset
591 (mm-find-charset-region b e))))))
592 (if (and (> (length charsets) 1)
593 (memq 'iso-8859-15 charsets)
594 (memq 'iso-8859-15 hack-charsets)
595 (save-excursion (mm-iso-8859-x-to-15-region b e)))
596 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
597 mm-iso-8859-15-compatible))
598 (if (and (memq 'iso-2022-jp-2 charsets)
599 (memq 'iso-2022-jp-2 hack-charsets))
600 (setq charsets (delq 'iso-2022-jp charsets)))
603 (defmacro mm-with-unibyte-buffer (&rest forms)
604 "Create a temporary buffer, and evaluate FORMS there like `progn'.
605 Use unibyte mode for this."
606 `(let (default-enable-multibyte-characters)
607 (with-temp-buffer ,@forms)))
608 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
609 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
611 (defmacro mm-with-unibyte-current-buffer (&rest forms)
612 "Evaluate FORMS with current buffer temporarily made unibyte.
613 Also bind `default-enable-multibyte-characters' to nil.
614 Equivalent to `progn' in XEmacs"
615 (let ((multibyte (make-symbol "multibyte"))
616 (buffer (make-symbol "buffer")))
618 (let ((,multibyte enable-multibyte-characters)
619 (,buffer (current-buffer)))
621 (let (default-enable-multibyte-characters)
622 (set-buffer-multibyte nil)
625 (set-buffer-multibyte ,multibyte)))
626 (let (default-enable-multibyte-characters)
628 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
629 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
631 (defmacro mm-with-unibyte (&rest forms)
632 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
633 `(let (default-enable-multibyte-characters)
635 (put 'mm-with-unibyte 'lisp-indent-function 0)
636 (put 'mm-with-unibyte 'edebug-form-spec '(body))
638 (defmacro mm-with-multibyte (&rest forms)
639 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
640 `(let ((default-enable-multibyte-characters t))
642 (put 'mm-with-multibyte 'lisp-indent-function 0)
643 (put 'mm-with-multibyte 'edebug-form-spec '(body))
645 (defun mm-find-charset-region (b e)
646 "Return a list of Emacs charsets in the region B to E."
648 ((and (mm-multibyte-p)
649 (fboundp 'find-charset-region))
650 ;; Remove composition since the base charsets have been included.
651 ;; Remove eight-bit-*, treat them as ascii.
652 (let ((css (find-charset-region b e)))
653 (mapcar (lambda (cs) (setq css (delq cs css)))
654 '(composition eight-bit-control eight-bit-graphic
658 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
661 (narrow-to-region b e)
662 (goto-char (point-min))
663 (skip-chars-forward "\0-\177")
668 (and (boundp 'current-language-environment)
669 (car (last (assq 'charset
670 (assoc current-language-environment
671 language-info-alist))))))
672 (if (eq charset 'ascii) (setq charset nil))
675 (car (last (assq mail-parse-charset
676 mm-mime-mule-charset-alist)))))
677 (list 'ascii (or charset 'latin-iso8859-1)))))))))
679 (defun mm-auto-mode-alist ()
680 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
681 (let ((alist auto-mode-alist)
684 (when (listp (cdar alist))
685 (push (car alist) out))
689 (defvar mm-inhibit-file-name-handlers
690 '(jka-compr-handler image-file-handler)
691 "A list of handlers doing (un)compression (etc) thingies.")
693 (defun mm-insert-file-contents (filename &optional visit beg end replace
695 "Like `insert-file-contents', but only reads in the file.
696 A buffer may be modified in several ways after reading into the buffer due
697 to advanced Emacs features, such as file-name-handlers, format decoding,
698 `find-file-hooks', etc.
699 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
700 This function ensures that none of these modifications will take place."
701 (let ((format-alist nil)
702 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
703 (default-major-mode 'fundamental-mode)
704 (enable-local-variables nil)
705 (after-insert-file-functions nil)
706 (enable-local-eval nil)
707 (find-file-hooks nil)
708 (inhibit-file-name-operation (if inhibit
709 'insert-file-contents
710 inhibit-file-name-operation))
711 (inhibit-file-name-handlers
713 (append mm-inhibit-file-name-handlers
714 inhibit-file-name-handlers)
715 inhibit-file-name-handlers)))
716 (insert-file-contents filename visit beg end replace)))
718 (defun mm-append-to-file (start end filename &optional codesys inhibit)
719 "Append the contents of the region to the end of file FILENAME.
720 When called from a function, expects three arguments,
721 START, END and FILENAME. START and END are buffer positions
722 saying what text to write.
723 Optional fourth argument specifies the coding system to use when
725 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
726 (let ((coding-system-for-write
727 (or codesys mm-text-coding-system-for-write
728 mm-text-coding-system))
729 (inhibit-file-name-operation (if inhibit
731 inhibit-file-name-operation))
732 (inhibit-file-name-handlers
734 (append mm-inhibit-file-name-handlers
735 inhibit-file-name-handlers)
736 inhibit-file-name-handlers)))
737 (write-region start end filename t 'no-message)
738 (message "Appended to %s" filename)))
740 (defun mm-write-region (start end filename &optional append visit lockname
741 coding-system inhibit)
743 "Like `write-region'.
744 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
745 (let ((coding-system-for-write
746 (or coding-system mm-text-coding-system-for-write
747 mm-text-coding-system))
748 (inhibit-file-name-operation (if inhibit
750 inhibit-file-name-operation))
751 (inhibit-file-name-handlers
753 (append mm-inhibit-file-name-handlers
754 inhibit-file-name-handlers)
755 inhibit-file-name-handlers)))
756 (write-region start end filename append visit lockname)))
758 (defun mm-image-load-path (&optional package)
760 (dolist (path load-path (nreverse result))
763 (setq dir (concat (file-name-directory
764 (directory-file-name path))
765 "etc/" (or package "gnus/")))))
767 (push path result))))
769 ;; Fixme: This doesn't look useful where it's used.
770 (if (fboundp 'detect-coding-region)
771 (defun mm-detect-coding-region (start end)
772 "Like `detect-coding-region' except returning the best one."
773 (let ((coding-systems
774 (detect-coding-region (point) (point-max))))
775 (or (car-safe coding-systems)
777 (defun mm-detect-coding-region (start end)
778 (let ((point (point)))
780 (skip-chars-forward "\0-\177" end)
782 (if (eq (point) end) 'ascii (mm-guess-charset))
783 (goto-char point)))))
785 (if (fboundp 'coding-system-get)
786 (defun mm-detect-mime-charset-region (start end)
787 "Detect MIME charset of the text in the region between START and END."
788 (let ((cs (mm-detect-coding-region start end)))
789 (coding-system-get cs 'mime-charset)))
790 (defun mm-detect-mime-charset-region (start end)
791 "Detect MIME charset of the text in the region between START and END."
792 (let ((cs (mm-detect-coding-region start end)))
798 ;;; mm-util.el ends here