1 ;;; mm-util.el --- Utility functions for Mule and low level things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 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 (coding-system-equal . equal)
47 (annotationp . ignore)
48 (set-buffer-file-coding-system . ignore)
50 . (lambda (charset int)
58 (mapcar (lambda (e) (list (symbol-name (car e))))
59 mm-mime-mule-charset-alist)
62 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
63 "Replace characters in STRING from FROM to TO."
64 (let ((string (substring string 0)) ;Copy string.
67 ;; Replace all occurrences of FROM with TO.
69 (when (= (aref string idx) from)
73 (string-as-unibyte . identity)
74 (string-as-multibyte . identity)
75 (multibyte-string-p . ignore))))
78 (defalias 'mm-char-or-char-int-p
80 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
81 ((fboundp 'char-valid-p) 'char-valid-p)
85 (defalias 'mm-read-coding-system
87 ((fboundp 'read-coding-system)
88 (if (and (featurep 'xemacs)
89 (<= (string-to-number emacs-version) 21.1))
90 (lambda (prompt &optional default-coding-system)
91 (read-coding-system prompt))
93 (t (lambda (prompt &optional default-coding-system)
94 "Prompt the user for a coding system."
96 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
97 mm-mime-mule-charset-alist)))))))
99 (defvar mm-coding-system-list nil)
100 (defun mm-get-coding-system-list ()
101 "Get the coding system list."
102 (or mm-coding-system-list
103 (setq mm-coding-system-list (mm-coding-system-list))))
105 (defun mm-coding-system-p (sym)
106 "Return non-nil if SYM is a coding system."
107 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
108 (memq sym (mm-get-coding-system-list))))
110 (defvar mm-charset-synonym-alist
112 ;; Perfectly fine? A valid MIME name, anyhow.
113 ,@(unless (mm-coding-system-p 'big5)
115 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
116 ,@(unless (mm-coding-system-p 'x-ctext)
117 '((x-ctext . ctext)))
118 ;; Apparently not defined in Emacs 20, but is a valid MIME name.
119 ,@(unless (mm-coding-system-p 'gb2312)
120 '((gb2312 . cn-gb-2312)))
121 ;; ISO-8859-15 is very similar to ISO-8859-1.
122 ,@(unless (mm-coding-system-p 'iso-8859-15) ; Emacs 21 defines it.
123 '((iso-8859-15 . iso-8859-1)))
124 ;; Windows-1252 is actually a superset of Latin-1. See also
125 ;; `gnus-article-dumbquotes-map'.
126 ,@(unless (mm-coding-system-p 'windows-1252)
127 (if (mm-coding-system-p 'cp1252)
128 '((windows-1252 . cp1252))
129 '((windows-1252 . 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 ,@(if (and (not (mm-coding-system-p 'windows-1250))
134 (mm-coding-system-p 'cp1250))
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 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
244 "A list of special charsets.
245 Valid elements include:
246 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
247 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
250 (defvar mm-iso-8859-15-compatible
251 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
252 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
253 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
255 (defvar mm-iso-8859-x-to-15-table
256 (and (fboundp 'coding-system-p)
257 (mm-coding-system-p 'iso-8859-15)
260 (if (mm-coding-system-p (car cs))
261 (let ((c (string-to-char
262 (decode-coding-string "\341" (car cs)))))
263 (cons (char-charset c)
266 (decode-coding-string "\341" 'iso-8859-15)) c)
267 (string-to-list (decode-coding-string (car (cdr cs))
270 mm-iso-8859-15-compatible))
271 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
273 (defcustom mm-coding-system-priorities nil
274 "Preferred coding systems for encoding outgoing mails.
276 More than one suitable coding systems may be found for some texts. By
277 default, a coding system with the highest priority is used to encode
278 outgoing mails (see `sort-coding-systems'). If this variable is set,
279 it overrides the default priority. For example, Japanese users may
280 prefer iso-2022-jp to japanese-shift-jis:
282 \(setq mm-coding-system-priorities
283 '(iso-2022-jp iso-2022-jp-2 japanese-shift-jis iso-latin-1 utf-8))
285 :type '(repeat (symbol :tag "Coding system"))
288 (defvar mm-use-find-coding-systems-region
289 (fboundp 'find-coding-systems-region)
290 "Use `find-coding-systems-region' to find proper coding systems.
292 Setting it to nil is useful on Emacsen supporting Unicode if sending
293 mail with multiple parts is preferred to sending a Unicode one.")
295 ;;; Internal variables:
299 (defun mm-mule-charset-to-mime-charset (charset)
300 "Return the MIME charset corresponding to the given Mule CHARSET."
301 (if (fboundp 'find-coding-systems-for-charsets)
303 (dolist (cs (find-coding-systems-for-charsets (list charset)))
306 (setq mime (coding-system-get cs 'mime-charset)))))
308 (let ((alist mm-mime-mule-charset-alist)
311 (when (memq charset (cdar alist))
312 (setq out (caar alist)
317 (defun mm-charset-to-coding-system (charset &optional lbt)
318 "Return coding-system corresponding to CHARSET.
319 CHARSET is a symbol naming a MIME charset.
320 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
321 used as the line break code type of the coding system."
322 (when (stringp charset)
323 (setq charset (intern (downcase charset))))
325 (setq charset (intern (format "%s-%s" charset lbt))))
329 ;; Running in a non-MULE environment.
330 ((null (mm-get-coding-system-list))
333 ((eq charset 'us-ascii)
335 ;; Check to see whether we can handle this charset. (This depends
336 ;; on there being some coding system matching each `mime-charset'
337 ;; property defined, as there should be.)
338 ((and (mm-coding-system-p charset)
339 ;;; Doing this would potentially weed out incorrect charsets.
341 ;;; (eq charset (coding-system-get charset 'mime-charset))
344 ;; Translate invalid charsets.
345 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
346 (and cs (mm-coding-system-p cs) cs)))
347 ;; Last resort: search the coding system list for entries which
348 ;; have the right mime-charset in case the canonical name isn't
349 ;; defined (though it should be).
351 ;; mm-get-coding-system-list returns a list of cs without lbt.
353 (dolist (c (mm-get-coding-system-list))
355 (eq charset (coding-system-get c 'mime-charset)))
359 (defsubst mm-replace-chars-in-string (string from to)
360 (mm-subst-char-in-string from to string))
363 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
364 (boundp 'default-enable-multibyte-characters)
365 default-enable-multibyte-characters
366 (fboundp 'set-buffer-multibyte))
369 (defvar mm-mule4-p (and mm-emacs-mule
371 (not (charsetp 'eight-bit-control)))
375 (defun mm-enable-multibyte ()
376 "Set the multibyte flag of the current buffer.
377 Only do this if the default value of `enable-multibyte-characters' is
378 non-nil. This is a no-op in XEmacs."
379 (set-buffer-multibyte t))
380 (defalias 'mm-enable-multibyte 'ignore))
383 (defun mm-disable-multibyte ()
384 "Unset the multibyte flag of in the current buffer.
385 This is a no-op in XEmacs."
386 (set-buffer-multibyte nil))
387 (defalias 'mm-disable-multibyte 'ignore))
390 (defun mm-enable-multibyte-mule4 ()
391 "Enable multibyte in the current buffer.
392 Only used in Emacs Mule 4."
393 (set-buffer-multibyte t))
394 (defalias 'mm-enable-multibyte-mule4 'ignore))
397 (defun mm-disable-multibyte-mule4 ()
398 "Disable multibyte in the current buffer.
399 Only used in Emacs Mule 4."
400 (set-buffer-multibyte nil))
401 (defalias 'mm-disable-multibyte-mule4 'ignore)))
403 (defun mm-preferred-coding-system (charset)
404 ;; A typo in some Emacs versions.
405 (or (get-charset-property charset 'preferred-coding-system)
406 (get-charset-property charset 'prefered-coding-system)))
408 (defsubst mm-guess-charset ()
409 "Guess Mule charset from the language environment."
411 mail-parse-mule-charset ;; cached mule-charset
413 (setq mail-parse-mule-charset
414 (and (boundp 'current-language-environment)
417 (assoc current-language-environment
418 language-info-alist))))))
419 (if (or (not mail-parse-mule-charset)
420 (eq mail-parse-mule-charset 'ascii))
421 (setq mail-parse-mule-charset
422 (or (car (last (assq mail-parse-charset
423 mm-mime-mule-charset-alist)))
426 mail-parse-mule-charset)))
428 (defun mm-charset-after (&optional pos)
429 "Return charset of a character in current buffer at position POS.
430 If POS is nil, it defauls to the current point.
431 If POS is out of range, the value is nil.
432 If the charset is `composition', return the actual one."
433 (let ((char (char-after pos)) charset)
434 (if (< (mm-char-int char) 128)
435 (setq charset 'ascii)
436 ;; charset-after is fake in some Emacsen.
437 (setq charset (and (fboundp 'char-charset) (char-charset char)))
438 (if (eq charset 'composition)
439 (let ((p (or pos (point))))
440 (cadr (find-charset-region p (1+ p))))
441 (if (and charset (not (memq charset '(ascii eight-bit-control
442 eight-bit-graphic))))
444 (mm-guess-charset))))))
446 (defun mm-mime-charset (charset)
447 "Return the MIME charset corresponding to the given Mule CHARSET."
448 (if (eq charset 'unknown)
449 (error "The message contains non-printable characters, please use attachment"))
450 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
451 ;; This exists in Emacs 20.
453 (and (mm-preferred-coding-system charset)
455 (mm-preferred-coding-system charset) 'mime-charset))
456 (and (eq charset 'ascii)
458 (mm-preferred-coding-system charset)
459 (mm-mule-charset-to-mime-charset charset))
460 ;; This is for XEmacs.
461 (mm-mule-charset-to-mime-charset charset)))
463 (defun mm-delete-duplicates (list)
464 "Simple substitute for CL `delete-duplicates', testing with `equal'."
467 (setq head (car list))
468 (setq list (delete head list))
469 (setq result (cons head result)))
472 (if (and (not (featurep 'xemacs))
473 (boundp 'enable-multibyte-characters))
474 (defalias 'mm-multibyte-p
476 "Say whether multibyte is enabled in the current buffer."
477 enable-multibyte-characters))
478 (defalias 'mm-multibyte-p (lambda () (featurep 'mule))))
480 (defun mm-iso-8859-x-to-15-region (&optional b e)
481 (if (fboundp 'char-charset)
482 (let (charset item c inconvertible)
484 (if e (narrow-to-region b e))
485 (goto-char (point-min))
486 (skip-chars-forward "\0-\177")
489 ((not (setq item (assq (char-charset (setq c (char-after)))
490 mm-iso-8859-x-to-15-table)))
492 ((memq c (cdr (cdr item)))
493 (setq inconvertible t)
496 (insert-before-markers (prog1 (+ c (car (cdr item)))
498 (skip-chars-forward "\0-\177"))))
499 (not inconvertible))))
501 (defun mm-sort-coding-systems-predicate (a b)
502 (> (length (memq a mm-coding-system-priorities))
503 (length (memq b mm-coding-system-priorities))))
505 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
506 "Return the MIME charsets needed to encode the region between B and E.
507 nil means ASCII, a single-element list represents an appropriate MIME
508 charset, and a longer list means no appropriate charset."
510 ;; The return possibilities of this function are a mess...
511 (or (and (mm-multibyte-p)
512 mm-use-find-coding-systems-region
513 ;; Find the mime-charset of the most preferred coding
514 ;; system that has one.
515 (let ((systems (find-coding-systems-region b e)))
516 (when mm-coding-system-priorities
518 (sort systems 'mm-sort-coding-systems-predicate)))
519 ;; Fixme: The `mime-charset' (`x-ctext') of `compound-text'
520 ;; is not in the IANA list.
521 (setq systems (delq 'compound-text systems))
522 (unless (equal systems '(undecided))
524 (let ((cs (coding-system-get (pop systems) 'mime-charset)))
527 charsets (list cs))))))
529 ;; Otherwise we're not multibyte, XEmacs or a single coding
530 ;; system won't cover it.
532 (mm-delete-duplicates
533 (mapcar 'mm-mime-charset
535 (mm-find-charset-region b e))))))
536 (if (and (memq 'iso-8859-15 charsets)
537 (memq 'iso-8859-15 hack-charsets)
538 (save-excursion (mm-iso-8859-x-to-15-region b e)))
539 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
540 mm-iso-8859-15-compatible))
541 (if (and (memq 'iso-2022-jp-2 charsets)
542 (memq 'iso-2022-jp-2 hack-charsets))
543 (setq charsets (delq 'iso-2022-jp charsets)))
546 (defmacro mm-with-unibyte-buffer (&rest forms)
547 "Create a temporary buffer, and evaluate FORMS there like `progn'.
548 Use unibyte mode for this."
549 `(let (default-enable-multibyte-characters)
550 (with-temp-buffer ,@forms)))
551 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
552 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
554 (defmacro mm-with-unibyte-current-buffer (&rest forms)
555 "Evaluate FORMS with current current buffer temporarily made unibyte.
556 Also bind `default-enable-multibyte-characters' to nil.
557 Equivalent to `progn' in XEmacs"
558 (let ((multibyte (make-symbol "multibyte"))
559 (buffer (make-symbol "buffer")))
561 (let ((,multibyte enable-multibyte-characters)
562 (,buffer (current-buffer)))
564 (let (default-enable-multibyte-characters)
565 (set-buffer-multibyte nil)
568 (set-buffer-multibyte ,multibyte)))
569 (let (default-enable-multibyte-characters)
571 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
572 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
574 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
575 "Evaluate FORMS there like `progn' in current buffer.
577 (let ((multibyte (make-symbol "multibyte"))
578 (buffer (make-symbol "buffer")))
580 (let ((,multibyte enable-multibyte-characters)
581 (,buffer (current-buffer)))
583 (let (default-enable-multibyte-characters)
584 (set-buffer-multibyte nil)
587 (set-buffer-multibyte ,multibyte)))
588 (let (default-enable-multibyte-characters)
590 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
591 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
593 (defmacro mm-with-unibyte (&rest forms)
594 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
595 `(let (default-enable-multibyte-characters)
597 (put 'mm-with-unibyte 'lisp-indent-function 0)
598 (put 'mm-with-unibyte 'edebug-form-spec '(body))
600 (defun mm-find-charset-region (b e)
601 "Return a list of Emacs charsets in the region B to E."
603 ((and (mm-multibyte-p)
604 (fboundp 'find-charset-region))
605 ;; Remove composition since the base charsets have been included.
606 ;; Remove eight-bit-*, treat them as ascii.
607 (let ((css (find-charset-region b e)))
608 (mapcar (lambda (cs) (setq css (delq cs css)))
609 '(composition eight-bit-control eight-bit-graphic
613 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
616 (narrow-to-region b e)
617 (goto-char (point-min))
618 (skip-chars-forward "\0-\177")
623 (and (boundp 'current-language-environment)
624 (car (last (assq 'charset
625 (assoc current-language-environment
626 language-info-alist))))))
627 (if (eq charset 'ascii) (setq charset nil))
630 (car (last (assq mail-parse-charset
631 mm-mime-mule-charset-alist)))))
632 (list 'ascii (or charset 'latin-iso8859-1)))))))))
634 (static-if (fboundp 'shell-quote-argument)
635 (defalias 'mm-quote-arg 'shell-quote-argument)
636 (defun mm-quote-arg (arg)
637 "Return a version of ARG that is safe to evaluate in a shell."
638 (let ((pos 0) new-pos accum)
639 ;; *** bug: we don't handle newline characters properly
640 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
641 (push (substring arg pos new-pos) accum)
643 (push (list (aref arg new-pos)) accum)
644 (setq pos (1+ new-pos)))
647 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
649 (defun mm-auto-mode-alist ()
650 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
651 (let ((alist auto-mode-alist)
654 (when (listp (cdar alist))
655 (push (car alist) out))
659 (defvar mm-inhibit-file-name-handlers
660 '(jka-compr-handler image-file-handler)
661 "A list of handlers doing (un)compression (etc) thingies.")
663 (defun mm-insert-file-contents (filename &optional visit beg end replace
665 "Like `insert-file-contents', q.v., but only reads in the file.
666 A buffer may be modified in several ways after reading into the buffer due
667 to advanced Emacs features, such as file-name-handlers, format decoding,
668 find-file-hooks, etc.
669 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
670 This function ensures that none of these modifications will take place."
671 (let ((format-alist nil)
672 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
673 (default-major-mode 'fundamental-mode)
674 (enable-local-variables nil)
675 (after-insert-file-functions nil)
676 (enable-local-eval nil)
677 (find-file-hooks nil)
678 (inhibit-file-name-operation (if inhibit
679 'insert-file-contents
680 inhibit-file-name-operation))
681 (inhibit-file-name-handlers
683 (append mm-inhibit-file-name-handlers
684 inhibit-file-name-handlers)
685 inhibit-file-name-handlers)))
686 (insert-file-contents filename visit beg end replace)))
688 (defun mm-append-to-file (start end filename &optional codesys inhibit)
689 "Append the contents of the region to the end of file FILENAME.
690 When called from a function, expects three arguments,
691 START, END and FILENAME. START and END are buffer positions
692 saying what text to write.
693 Optional fourth argument specifies the coding system to use when
695 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
696 (let ((coding-system-for-write
697 (or codesys mm-text-coding-system-for-write
698 mm-text-coding-system))
699 (inhibit-file-name-operation (if inhibit
701 inhibit-file-name-operation))
702 (inhibit-file-name-handlers
704 (append mm-inhibit-file-name-handlers
705 inhibit-file-name-handlers)
706 inhibit-file-name-handlers)))
707 (append-to-file start end filename)))
709 (defun mm-write-region (start end filename &optional append visit lockname
710 coding-system inhibit)
712 "Like `write-region'.
713 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
714 (let ((coding-system-for-write
715 (or coding-system mm-text-coding-system-for-write
716 mm-text-coding-system))
717 (inhibit-file-name-operation (if inhibit
719 inhibit-file-name-operation))
720 (inhibit-file-name-handlers
722 (append mm-inhibit-file-name-handlers
723 inhibit-file-name-handlers)
724 inhibit-file-name-handlers)))
725 (write-region start end filename append visit lockname)))
727 (defun mm-image-load-path (&optional package)
729 (dolist (path load-path (nreverse result))
730 (if (file-directory-p
731 (setq dir (concat (file-name-directory
732 (directory-file-name path))
733 "etc/" (or package "gnus/"))))
735 (push path result))))
737 (if (fboundp 'detect-coding-region)
738 (defun mm-detect-coding-region (start end)
739 "Like 'detect-coding-region' except returning the best one."
740 (let ((coding-systems
741 (detect-coding-region (point) (point-max))))
742 (or (car-safe coding-systems)
744 (defun mm-detect-coding-region (start end)
745 (let ((point (point)))
747 (skip-chars-forward "\0-\177" end)
749 (if (eq (point) end) 'ascii (mm-guess-charset))
750 (goto-char point)))))
752 (if (fboundp 'coding-system-get)
753 (defun mm-detect-mime-charset-region (start end)
754 "Detect MIME charset of the text in the region between START and END."
755 (let ((cs (mm-detect-coding-region start end)))
756 (coding-system-get cs 'mime-charset)))
757 (defun mm-detect-mime-charset-region (start end)
758 "Detect MIME charset of the text in the region between START and END."
759 (let ((cs (mm-detect-coding-region start end)))
762 (defun mm-guess-mime-charset ()
763 "Guess the default MIME charset from the language environment."
765 (and (boundp 'current-language-environment)
766 (assoc current-language-environment
767 language-info-alist)))
770 ((null language-info)
774 (or (assq 'coding-priority language-info)
775 (assq 'coding-system language-info))))
776 (if (fboundp 'coding-system-get)
777 (or (coding-system-get item 'mime-charset)
780 ((setq item (car (last (assq 'charset language-info))))
783 (mm-mime-charset item)))
787 ;; It is not a MIME function, but some MIME functions use it.
788 (defalias 'mm-make-temp-file
789 (if (fboundp 'make-temp-file)
791 (lambda (prefix &optional dir-flag)
792 (let ((file (expand-file-name
793 (make-temp-name prefix)
794 (if (fboundp 'temp-directory)
796 temporary-file-directory))))
798 (make-directory file))
803 ;;; mm-util.el ends here