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))
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 (coding-system-equal . equal)
44 (annotationp . ignore)
45 (set-buffer-file-coding-system . ignore)
47 . (lambda (charset int)
55 (mapcar (lambda (e) (list (symbol-name (car e))))
56 mm-mime-mule-charset-alist)
59 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
60 "Replace characters in STRING from FROM to TO."
61 (let ((string (substring string 0)) ;Copy string.
64 ;; Replace all occurrences of FROM with TO.
66 (when (= (aref string idx) from)
70 (string-as-unibyte . identity)
71 (string-as-multibyte . identity)
72 (multibyte-string-p . ignore))))
75 (defalias 'mm-char-or-char-int-p
77 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
78 ((fboundp 'char-valid-p) 'char-valid-p)
82 (defalias 'mm-read-coding-system
84 ((fboundp 'read-coding-system)
85 (if (and (featurep 'xemacs)
86 (<= (string-to-number emacs-version) 21.1))
87 (lambda (prompt &optional default-coding-system)
88 (read-coding-system prompt))
90 (t (lambda (prompt &optional default-coding-system)
91 "Prompt the user for a coding system."
93 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
94 mm-mime-mule-charset-alist)))))))
96 (defvar mm-coding-system-list nil)
97 (defun mm-get-coding-system-list ()
98 "Get the coding system list."
99 (or mm-coding-system-list
100 (setq mm-coding-system-list (mm-coding-system-list))))
102 (defun mm-coding-system-p (sym)
103 "Return non-nil if SYM is a coding system."
104 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
105 (memq sym (mm-get-coding-system-list))))
107 (defvar mm-charset-synonym-alist
109 ;; Perfectly fine? A valid MIME name, anyhow.
110 ,@(unless (mm-coding-system-p 'big5)
112 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
113 ,@(unless (mm-coding-system-p 'x-ctext)
114 '((x-ctext . ctext)))
115 ;; Apparently not defined in Emacs 20, but is a valid MIME name.
116 ,@(unless (mm-coding-system-p 'gb2312)
117 '((gb2312 . cn-gb-2312)))
118 ;; ISO-8859-15 is very similar to ISO-8859-1.
119 ,@(unless (mm-coding-system-p 'iso-8859-15) ; Emacs 21 defines it.
120 '((iso-8859-15 . iso-8859-1)))
121 ;; Windows-1252 is actually a superset of Latin-1. See also
122 ;; `gnus-article-dumbquotes-map'.
123 ,@(unless (mm-coding-system-p 'windows-1252)
124 (if (mm-coding-system-p 'cp1252)
125 '((windows-1252 . cp1252))
126 '((windows-1252 . 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 ,@(if (and (not (mm-coding-system-p 'windows-1250))
131 (mm-coding-system-p 'cp1250))
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
154 (if (mm-coding-system-p 'iso-2022-7bit)
156 "Coding system of auto save file.
157 Note that the default value for this variable was emacs-mule for Emacs
158 or escape-quoted for XEmacs-MULE in the past.")
160 (defvar mm-universal-coding-system mm-auto-save-coding-system
161 "The universal coding system.")
163 ;; Fixme: some of the cars here aren't valid MIME charsets. That
164 ;; should only matter with XEmacs, though.
165 (defvar mm-mime-mule-charset-alist
167 (iso-8859-1 latin-iso8859-1)
168 (iso-8859-2 latin-iso8859-2)
169 (iso-8859-3 latin-iso8859-3)
170 (iso-8859-4 latin-iso8859-4)
171 (iso-8859-5 cyrillic-iso8859-5)
172 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
173 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
174 ;; charset is koi8-r, not iso-8859-5.
175 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
176 (iso-8859-6 arabic-iso8859-6)
177 (iso-8859-7 greek-iso8859-7)
178 (iso-8859-8 hebrew-iso8859-8)
179 (iso-8859-9 latin-iso8859-9)
180 (iso-8859-14 latin-iso8859-14)
181 (iso-8859-15 latin-iso8859-15)
182 (viscii vietnamese-viscii-lower)
183 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
184 (euc-kr korean-ksc5601)
185 (gb2312 chinese-gb2312)
186 (big5 chinese-big5-1 chinese-big5-2)
188 (thai-tis620 thai-tis620)
189 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
190 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
191 latin-jisx0201 japanese-jisx0208-1978
192 chinese-gb2312 japanese-jisx0208
193 korean-ksc5601 japanese-jisx0212
195 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
196 latin-jisx0201 japanese-jisx0208-1978
197 chinese-gb2312 japanese-jisx0208
198 korean-ksc5601 japanese-jisx0212
199 chinese-cns11643-1 chinese-cns11643-2)
200 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
201 cyrillic-iso8859-5 greek-iso8859-7
202 latin-jisx0201 japanese-jisx0208-1978
203 chinese-gb2312 japanese-jisx0208
204 korean-ksc5601 japanese-jisx0212
205 chinese-cns11643-1 chinese-cns11643-2
206 chinese-cns11643-3 chinese-cns11643-4
207 chinese-cns11643-5 chinese-cns11643-6
209 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
210 (charsetp 'unicode-a)
211 (not (mm-coding-system-p 'mule-utf-8)))
212 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
213 ;; If we have utf-8 we're in Mule 5+.
216 (coding-system-get 'mule-utf-8 'safe-charsets)))))
217 "Alist of MIME-charset/MULE-charsets.")
219 ;; Correct by construction, but should be unnecessary:
221 (when (and (not (featurep 'xemacs))
222 (fboundp 'coding-system-list)
223 (fboundp 'sort-coding-systems))
224 (setq mm-mime-mule-charset-alist
229 (when (and (coding-system-get cs 'mime-charset)
230 (not (eq t (coding-system-get cs 'safe-charsets))))
231 (list (cons (coding-system-get cs 'mime-charset)
233 (coding-system-get cs 'safe-charsets))))))
234 (sort-coding-systems (coding-system-list 'base-only))))))
236 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
237 "A list of special charsets.
238 Valid elements include:
239 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
240 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
243 (defvar mm-iso-8859-15-compatible
244 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
245 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
246 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
248 (defvar mm-iso-8859-x-to-15-table
249 (and (fboundp 'coding-system-p)
250 (mm-coding-system-p 'iso-8859-15)
253 (if (mm-coding-system-p (car cs))
254 (let ((c (string-to-char
255 (decode-coding-string "\341" (car cs)))))
256 (cons (char-charset c)
259 (decode-coding-string "\341" 'iso-8859-15)) c)
260 (string-to-list (decode-coding-string (car (cdr cs))
263 mm-iso-8859-15-compatible))
264 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
266 (defcustom mm-coding-system-priorities
267 (if (boundp 'current-language-environment)
268 (let ((lang (symbol-value 'current-language-environment)))
269 (cond ((string= lang "Japanese")
270 ;; Japanese users may prefer iso-2022-jp to shift-jis.
271 '(iso-2022-jp iso-2022-jp-2 japanese-shift-jis
272 iso-latin-1 utf-8)))))
273 "Preferred coding systems for encoding outgoing mails.
275 More than one suitable coding systems may be found for some texts. By
276 default, a coding system with the highest priority is used to encode
277 outgoing mails (see `sort-coding-systems'). If this variable is set,
278 it overrides the default priority."
279 :type '(repeat (symbol :tag "Coding system"))
282 (defvar mm-use-find-coding-systems-region
283 (fboundp 'find-coding-systems-region)
284 "Use `find-coding-systems-region' to find proper coding systems.
286 Setting it to nil is useful on Emacsen supporting Unicode if sending
287 mail with multiple parts is preferred to sending a Unicode one.")
289 ;;; Internal variables:
293 (defun mm-mule-charset-to-mime-charset (charset)
294 "Return the MIME charset corresponding to the given Mule CHARSET."
295 (if (fboundp 'find-coding-systems-for-charsets)
297 (dolist (cs (find-coding-systems-for-charsets (list charset)))
300 (setq mime (coding-system-get cs 'mime-charset)))))
302 (let ((alist mm-mime-mule-charset-alist)
305 (when (memq charset (cdar alist))
306 (setq out (caar alist)
311 (defun mm-charset-to-coding-system (charset &optional lbt)
312 "Return coding-system corresponding to CHARSET.
313 CHARSET is a symbol naming a MIME charset.
314 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
315 used as the line break code type of the coding system."
316 (when (stringp charset)
317 (setq charset (intern (downcase charset))))
319 (setq charset (intern (format "%s-%s" charset lbt))))
323 ;; Running in a non-MULE environment.
324 ((null (mm-get-coding-system-list))
327 ((eq charset 'us-ascii)
329 ;; Check to see whether we can handle this charset. (This depends
330 ;; on there being some coding system matching each `mime-charset'
331 ;; property defined, as there should be.)
332 ((and (mm-coding-system-p charset)
333 ;;; Doing this would potentially weed out incorrect charsets.
335 ;;; (eq charset (coding-system-get charset 'mime-charset))
338 ;; Translate invalid charsets.
339 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
340 (and cs (mm-coding-system-p cs) cs)))
341 ;; Last resort: search the coding system list for entries which
342 ;; have the right mime-charset in case the canonical name isn't
343 ;; defined (though it should be).
345 ;; mm-get-coding-system-list returns a list of cs without lbt.
347 (dolist (c (mm-get-coding-system-list))
349 (eq charset (coding-system-get c 'mime-charset)))
353 (defsubst mm-replace-chars-in-string (string from to)
354 (mm-subst-char-in-string from to string))
357 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
358 (boundp 'default-enable-multibyte-characters)
359 default-enable-multibyte-characters
360 (fboundp 'set-buffer-multibyte))
363 (defvar mm-mule4-p (and mm-emacs-mule
365 (not (charsetp 'eight-bit-control)))
369 (defun mm-enable-multibyte ()
370 "Set the multibyte flag of the current buffer.
371 Only do this if the default value of `enable-multibyte-characters' is
372 non-nil. This is a no-op in XEmacs."
373 (set-buffer-multibyte t))
374 (defalias 'mm-enable-multibyte 'ignore))
377 (defun mm-disable-multibyte ()
378 "Unset the multibyte flag of in the current buffer.
379 This is a no-op in XEmacs."
380 (set-buffer-multibyte nil))
381 (defalias 'mm-disable-multibyte 'ignore))
384 (defun mm-enable-multibyte-mule4 ()
385 "Enable multibyte in the current buffer.
386 Only used in Emacs Mule 4."
387 (set-buffer-multibyte t))
388 (defalias 'mm-enable-multibyte-mule4 'ignore))
391 (defun mm-disable-multibyte-mule4 ()
392 "Disable multibyte in the current buffer.
393 Only used in Emacs Mule 4."
394 (set-buffer-multibyte nil))
395 (defalias 'mm-disable-multibyte-mule4 'ignore)))
397 (defun mm-preferred-coding-system (charset)
398 ;; A typo in some Emacs versions.
399 (or (get-charset-property charset 'preferred-coding-system)
400 (get-charset-property charset 'prefered-coding-system)))
402 (defsubst mm-guess-charset ()
403 "Guess Mule charset from the language environment."
405 mail-parse-mule-charset ;; cached mule-charset
407 (setq mail-parse-mule-charset
408 (and (boundp 'current-language-environment)
411 (assoc current-language-environment
412 language-info-alist))))))
413 (if (or (not mail-parse-mule-charset)
414 (eq mail-parse-mule-charset 'ascii))
415 (setq mail-parse-mule-charset
416 (or (car (last (assq mail-parse-charset
417 mm-mime-mule-charset-alist)))
420 mail-parse-mule-charset)))
422 (defun mm-charset-after (&optional pos)
423 "Return charset of a character in current buffer at position POS.
424 If POS is nil, it defauls to the current point.
425 If POS is out of range, the value is nil.
426 If the charset is `composition', return the actual one."
427 (let ((char (char-after pos)) charset)
428 (if (< (mm-char-int char) 128)
429 (setq charset 'ascii)
430 ;; charset-after is fake in some Emacsen.
431 (setq charset (and (fboundp 'char-charset) (char-charset char)))
432 (if (eq charset 'composition)
433 (let ((p (or pos (point))))
434 (cadr (find-charset-region p (1+ p))))
435 (if (and charset (not (memq charset '(ascii eight-bit-control
436 eight-bit-graphic))))
438 (mm-guess-charset))))))
440 (defun mm-mime-charset (charset)
441 "Return the MIME charset corresponding to the given Mule CHARSET."
442 (if (eq charset 'unknown)
443 (error "The message contains non-printable characters, please use attachment"))
444 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
445 ;; This exists in Emacs 20.
447 (and (mm-preferred-coding-system charset)
449 (mm-preferred-coding-system charset) 'mime-charset))
450 (and (eq charset 'ascii)
452 (mm-preferred-coding-system charset)
453 (mm-mule-charset-to-mime-charset charset))
454 ;; This is for XEmacs.
455 (mm-mule-charset-to-mime-charset charset)))
457 (defun mm-delete-duplicates (list)
458 "Simple substitute for CL `delete-duplicates', testing with `equal'."
461 (setq head (car list))
462 (setq list (delete head list))
463 (setq result (cons head result)))
466 (if (and (not (featurep 'xemacs))
467 (boundp 'enable-multibyte-characters))
468 (defalias 'mm-multibyte-p
470 "Say whether multibyte is enabled in the current buffer."
471 enable-multibyte-characters))
472 (defalias 'mm-multibyte-p (lambda () (featurep 'mule))))
474 (defun mm-iso-8859-x-to-15-region (&optional b e)
475 (if (fboundp 'char-charset)
476 (let (charset item c inconvertible)
478 (if e (narrow-to-region b e))
479 (goto-char (point-min))
480 (skip-chars-forward "\0-\177")
483 ((not (setq item (assq (char-charset (setq c (char-after)))
484 mm-iso-8859-x-to-15-table)))
486 ((memq c (cdr (cdr item)))
487 (setq inconvertible t)
490 (insert-before-markers (prog1 (+ c (car (cdr item)))
492 (skip-chars-forward "\0-\177"))))
493 (not inconvertible))))
495 (defun mm-sort-coding-systems-predicate (a b)
496 (> (length (memq a mm-coding-system-priorities))
497 (length (memq b mm-coding-system-priorities))))
499 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
500 "Return the MIME charsets needed to encode the region between B and E.
501 nil means ASCII, a single-element list represents an appropriate MIME
502 charset, and a longer list means no appropriate charset."
504 ;; The return possibilities of this function are a mess...
505 (or (and (mm-multibyte-p)
506 mm-use-find-coding-systems-region
507 ;; Find the mime-charset of the most preferred coding
508 ;; system that has one.
509 (let ((systems (find-coding-systems-region b e)))
510 (when mm-coding-system-priorities
512 (sort systems 'mm-sort-coding-systems-predicate)))
513 ;; Fixme: The `mime-charset' (`x-ctext') of `compound-text'
514 ;; is not in the IANA list.
515 (setq systems (delq 'compound-text systems))
516 (unless (equal systems '(undecided))
518 (let ((cs (coding-system-get (pop systems) 'mime-charset)))
521 charsets (list cs))))))
523 ;; Otherwise we're not multibyte, XEmacs or a single coding
524 ;; system won't cover it.
526 (mm-delete-duplicates
527 (mapcar 'mm-mime-charset
529 (mm-find-charset-region b e))))))
530 (if (and (memq 'iso-8859-15 charsets)
531 (memq 'iso-8859-15 hack-charsets)
532 (save-excursion (mm-iso-8859-x-to-15-region b e)))
533 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
534 mm-iso-8859-15-compatible))
535 (if (and (memq 'iso-2022-jp-2 charsets)
536 (memq 'iso-2022-jp-2 hack-charsets))
537 (setq charsets (delq 'iso-2022-jp charsets)))
540 (defmacro mm-with-unibyte-buffer (&rest forms)
541 "Create a temporary buffer, and evaluate FORMS there like `progn'.
542 Use unibyte mode for this."
543 `(let (default-enable-multibyte-characters)
544 (with-temp-buffer ,@forms)))
545 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
546 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
548 (defmacro mm-with-unibyte-current-buffer (&rest forms)
549 "Evaluate FORMS with current current buffer temporarily made unibyte.
550 Also bind `default-enable-multibyte-characters' to nil.
551 Equivalent to `progn' in XEmacs"
552 (let ((multibyte (make-symbol "multibyte"))
553 (buffer (make-symbol "buffer")))
555 (let ((,multibyte enable-multibyte-characters)
556 (,buffer (current-buffer)))
558 (let (default-enable-multibyte-characters)
559 (set-buffer-multibyte nil)
562 (set-buffer-multibyte ,multibyte)))
563 (let (default-enable-multibyte-characters)
565 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
566 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
568 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
569 "Evaluate FORMS there like `progn' in current buffer.
571 (let ((multibyte (make-symbol "multibyte"))
572 (buffer (make-symbol "buffer")))
574 (let ((,multibyte enable-multibyte-characters)
575 (,buffer (current-buffer)))
577 (let (default-enable-multibyte-characters)
578 (set-buffer-multibyte nil)
581 (set-buffer-multibyte ,multibyte)))
582 (let (default-enable-multibyte-characters)
584 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
585 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
587 (defmacro mm-with-unibyte (&rest forms)
588 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
589 `(let (default-enable-multibyte-characters)
591 (put 'mm-with-unibyte 'lisp-indent-function 0)
592 (put 'mm-with-unibyte 'edebug-form-spec '(body))
594 (defun mm-find-charset-region (b e)
595 "Return a list of Emacs charsets in the region B to E."
597 ((and (mm-multibyte-p)
598 (fboundp 'find-charset-region))
599 ;; Remove composition since the base charsets have been included.
600 ;; Remove eight-bit-*, treat them as ascii.
601 (let ((css (find-charset-region b e)))
602 (mapcar (lambda (cs) (setq css (delq cs css)))
603 '(composition eight-bit-control eight-bit-graphic
607 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
610 (narrow-to-region b e)
611 (goto-char (point-min))
612 (skip-chars-forward "\0-\177")
617 (and (boundp 'current-language-environment)
618 (car (last (assq 'charset
619 (assoc current-language-environment
620 language-info-alist))))))
621 (if (eq charset 'ascii) (setq charset nil))
624 (car (last (assq mail-parse-charset
625 mm-mime-mule-charset-alist)))))
626 (list 'ascii (or charset 'latin-iso8859-1)))))))))
628 (if (fboundp 'shell-quote-argument)
629 (defalias 'mm-quote-arg 'shell-quote-argument)
630 (defun mm-quote-arg (arg)
631 "Return a version of ARG that is safe to evaluate in a shell."
632 (let ((pos 0) new-pos accum)
633 ;; *** bug: we don't handle newline characters properly
634 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
635 (push (substring arg pos new-pos) accum)
637 (push (list (aref arg new-pos)) accum)
638 (setq pos (1+ new-pos)))
641 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
643 (defun mm-auto-mode-alist ()
644 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
645 (let ((alist auto-mode-alist)
648 (when (listp (cdar alist))
649 (push (car alist) out))
653 (defvar mm-inhibit-file-name-handlers
654 '(jka-compr-handler image-file-handler)
655 "A list of handlers doing (un)compression (etc) thingies.")
657 (defun mm-insert-file-contents (filename &optional visit beg end replace
659 "Like `insert-file-contents', q.v., but only reads in the file.
660 A buffer may be modified in several ways after reading into the buffer due
661 to advanced Emacs features, such as file-name-handlers, format decoding,
662 find-file-hooks, etc.
663 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
664 This function ensures that none of these modifications will take place."
665 (let ((format-alist nil)
666 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
667 (default-major-mode 'fundamental-mode)
668 (enable-local-variables nil)
669 (after-insert-file-functions nil)
670 (enable-local-eval nil)
671 (find-file-hooks nil)
672 (inhibit-file-name-operation (if inhibit
673 'insert-file-contents
674 inhibit-file-name-operation))
675 (inhibit-file-name-handlers
677 (append mm-inhibit-file-name-handlers
678 inhibit-file-name-handlers)
679 inhibit-file-name-handlers)))
680 (insert-file-contents filename visit beg end replace)))
682 (defun mm-append-to-file (start end filename &optional codesys inhibit)
683 "Append the contents of the region to the end of file FILENAME.
684 When called from a function, expects three arguments,
685 START, END and FILENAME. START and END are buffer positions
686 saying what text to write.
687 Optional fourth argument specifies the coding system to use when
689 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
690 (let ((coding-system-for-write
691 (or codesys mm-text-coding-system-for-write
692 mm-text-coding-system))
693 (inhibit-file-name-operation (if inhibit
695 inhibit-file-name-operation))
696 (inhibit-file-name-handlers
698 (append mm-inhibit-file-name-handlers
699 inhibit-file-name-handlers)
700 inhibit-file-name-handlers)))
701 (append-to-file start end filename)))
703 (defun mm-write-region (start end filename &optional append visit lockname
704 coding-system inhibit)
706 "Like `write-region'.
707 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
708 (let ((coding-system-for-write
709 (or coding-system mm-text-coding-system-for-write
710 mm-text-coding-system))
711 (inhibit-file-name-operation (if inhibit
713 inhibit-file-name-operation))
714 (inhibit-file-name-handlers
716 (append mm-inhibit-file-name-handlers
717 inhibit-file-name-handlers)
718 inhibit-file-name-handlers)))
719 (write-region start end filename append visit lockname)))
721 (defun mm-image-load-path (&optional package)
723 (dolist (path load-path (nreverse result))
724 (if (file-directory-p
725 (setq dir (concat (file-name-directory
726 (directory-file-name path))
727 "etc/" (or package "gnus/"))))
729 (push path result))))
731 (if (fboundp 'detect-coding-region)
732 (defun mm-detect-coding-region (start end)
733 "Like 'detect-coding-region' except returning the best one."
734 (let ((coding-systems
735 (detect-coding-region (point) (point-max))))
736 (or (car-safe coding-systems)
738 (defun mm-detect-coding-region (start end)
739 (let ((point (point)))
741 (skip-chars-forward "\0-\177" end)
743 (if (eq (point) end) 'ascii (mm-guess-charset))
744 (goto-char point)))))
746 (if (fboundp 'coding-system-get)
747 (defun mm-detect-mime-charset-region (start end)
748 "Detect MIME charset of the text in the region between START and END."
749 (let ((cs (mm-detect-coding-region start end)))
750 (coding-system-get cs 'mime-charset)))
751 (defun mm-detect-mime-charset-region (start end)
752 "Detect MIME charset of the text in the region between START and END."
753 (let ((cs (mm-detect-coding-region start end)))
756 (defun mm-guess-mime-charset ()
757 "Guess the default MIME charset from the language environment."
759 (and (boundp 'current-language-environment)
760 (assoc current-language-environment
761 language-info-alist)))
764 ((null language-info)
768 (or (assq 'coding-priority language-info)
769 (assq 'coding-system language-info))))
770 (if (fboundp 'coding-system-get)
771 (or (coding-system-get item 'mime-charset)
774 ((setq item (car (last (assq 'charset language-info))))
777 (mm-mime-charset item)))
781 ;; It is not a MIME function, but some MIME functions use it.
782 (defalias 'mm-make-temp-file
783 (if (fboundp 'make-temp-file)
785 (lambda (prefix &optional dir-flag)
786 (let ((file (expand-file-name
787 (make-temp-name prefix)
788 (if (fboundp 'temp-directory)
790 temporary-file-directory))))
792 (make-directory file))
797 ;;; mm-util.el ends here