2001-11-01 08:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / mm-util.el
1 ;;; mm-util.el --- Utility functions for Mule and low level things
2 ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
7
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)
11 ;; any later version.
12
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.
17
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.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (require 'mail-prsvr)
29
30 (eval-and-compile
31   (mapcar
32    (lambda (elem)
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)
42      (char-int . identity)
43      (device-type . ignore)
44      (coding-system-equal . equal)
45      (annotationp . ignore)
46      (set-buffer-file-coding-system . ignore)
47      (make-char
48       . (lambda (charset int)
49           (int-to-char int)))
50      (read-charset
51       . (lambda (prompt)
52           "Return a charset."
53           (intern
54            (completing-read
55             prompt
56             (mapcar (lambda (e) (list (symbol-name (car e))))
57                     mm-mime-mule-charset-alist)
58             nil t))))
59      (subst-char-in-string
60       . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
61           "Replace characters in STRING from FROM to TO."
62           (let ((string (substring string 0)) ;Copy string.
63                 (len (length string))
64                 (idx 0))
65             ;; Replace all occurrences of FROM with TO.
66             (while (< idx len)
67               (when (= (aref string idx) from)
68                 (aset string idx to))
69               (setq idx (1+ idx)))
70             string)))
71      (string-as-unibyte . identity)
72      (string-as-multibyte . identity)
73      (multibyte-string-p . ignore))))
74
75 (eval-and-compile
76   (defalias 'mm-char-or-char-int-p
77     (cond
78      ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
79      ((fboundp 'char-valid-p) 'char-valid-p)
80      (t 'identity))))
81
82 (eval-and-compile
83   (defalias 'mm-read-coding-system
84     (cond
85      ((fboundp 'read-coding-system)
86       (if (and (featurep 'xemacs)
87                (<= (string-to-number emacs-version) 21.1))
88           (lambda (prompt &optional default-coding-system)
89             (read-coding-system prompt))
90         'read-coding-system))
91      (t (lambda (prompt &optional default-coding-system)
92           "Prompt the user for a coding system."
93           (completing-read
94            prompt (mapcar (lambda (s) (list (symbol-name (car s))))
95                           mm-mime-mule-charset-alist)))))))
96
97 (defvar mm-coding-system-list nil)
98 (defun mm-get-coding-system-list ()
99   "Get the coding system list."
100   (or mm-coding-system-list
101       (setq mm-coding-system-list (mm-coding-system-list))))
102
103 (defun mm-coding-system-p (sym)
104   "Return non-nil if SYM is a coding system."
105   (or (and (fboundp 'coding-system-p) (coding-system-p sym))
106       (memq sym (mm-get-coding-system-list))))
107
108 (defvar mm-charset-synonym-alist
109   `(
110     ;; Perfectly fine?  A valid MIME name, anyhow.
111     ,(unless (mm-coding-system-p 'big5)
112        '(big5 . cn-big5))
113     ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
114     ,(unless (mm-coding-system-p 'x-ctext)
115        '(x-ctext . ctext))
116     ;; Apparently not defined in Emacs 20, but is a valid MIME name.
117     ,(unless (mm-coding-system-p 'gb2312)
118        '(gb2312 . cn-gb-2312))
119     ;; Windows-1252 is actually a superset of Latin-1.  See also
120     ;; `gnus-article-dumbquotes-map'.
121     ;;,(unless (mm-coding-system-p 'windows-1252)       
122                                         ; should be defined eventually
123     ;;  '(windows-1252 . iso-8859-1))
124     ;; ISO-8859-15 is very similar to ISO-8859-1.
125     ;;,(unless (mm-coding-system-p 'iso-8859-15) ; Emacs 21 defines it.
126     ;;   '(iso-8859-15 . iso-8859-1))
127     ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
128     ;; Outlook users in Czech republic. Use this to allow reading of their
129     ;; e-mails. cp1250 should be defined by M-x codepage-setup.
130     ;;,(unless (mm-coding-system-p 'windows-1250)       
131                                         ; should be defined eventually
132     ;;  '(windows-1250 . cp1250))
133     )
134   "A mapping from invalid charset names to the real charset names.")
135
136 (defvar mm-binary-coding-system
137   (cond
138    ((mm-coding-system-p 'binary) 'binary)
139    ((mm-coding-system-p 'no-conversion) 'no-conversion)
140    (t nil))
141   "100% binary coding system.")
142
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).")
149
150 (defvar mm-text-coding-system-for-write nil
151   "Text coding system for write.")
152
153 (defvar mm-auto-save-coding-system
154   (cond
155    ((mm-coding-system-p 'emacs-mule)
156     (if (memq system-type '(windows-nt ms-dos ms-windows))
157         (if (mm-coding-system-p 'emacs-mule-dos)
158             'emacs-mule-dos mm-binary-coding-system)
159       'emacs-mule))
160    ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
161    (t mm-binary-coding-system))
162   "Coding system of auto save file.")
163
164 (defvar mm-universal-coding-system mm-auto-save-coding-system
165   "The universal Coding system.")
166
167 ;; Fixme: some of the cars here aren't valid MIME charsets.  That
168 ;; should only matter with XEmacs, though.
169 (defvar mm-mime-mule-charset-alist
170   `((us-ascii ascii)
171     (iso-8859-1 latin-iso8859-1)
172     (iso-8859-2 latin-iso8859-2)
173     (iso-8859-3 latin-iso8859-3)
174     (iso-8859-4 latin-iso8859-4)
175     (iso-8859-5 cyrillic-iso8859-5)
176     ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
177     ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
178     ;; charset is koi8-r, not iso-8859-5.
179     (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
180     (iso-8859-6 arabic-iso8859-6)
181     (iso-8859-7 greek-iso8859-7)
182     (iso-8859-8 hebrew-iso8859-8)
183     (iso-8859-9 latin-iso8859-9)
184     (iso-8859-14 latin-iso8859-14)
185     (iso-8859-15 latin-iso8859-15)
186     (viscii vietnamese-viscii-lower)
187     (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
188     (euc-kr korean-ksc5601)
189     (gb2312 chinese-gb2312)
190     (big5 chinese-big5-1 chinese-big5-2)
191     (tibetan tibetan)
192     (thai-tis620 thai-tis620)
193     (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
194     (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
195                    latin-jisx0201 japanese-jisx0208-1978
196                    chinese-gb2312 japanese-jisx0208
197                    korean-ksc5601 japanese-jisx0212
198                    katakana-jisx0201)
199     (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
200                     latin-jisx0201 japanese-jisx0208-1978
201                     chinese-gb2312 japanese-jisx0208
202                     korean-ksc5601 japanese-jisx0212
203                     chinese-cns11643-1 chinese-cns11643-2)
204     (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
205                     cyrillic-iso8859-5 greek-iso8859-7
206                     latin-jisx0201 japanese-jisx0208-1978
207                     chinese-gb2312 japanese-jisx0208
208                     korean-ksc5601 japanese-jisx0212
209                     chinese-cns11643-1 chinese-cns11643-2
210                     chinese-cns11643-3 chinese-cns11643-4
211                     chinese-cns11643-5 chinese-cns11643-6
212                     chinese-cns11643-7)
213     ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
214              (charsetp 'unicode-a)
215              (not (mm-coding-system-p 'mule-utf-8)))
216          '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
217        ;; If we have utf-8 we're in Mule 5+.
218        (append '(utf-8)
219                (delete 'ascii
220                        (coding-system-get 'mule-utf-8 'safe-charsets)))))
221   "Alist of MIME-charset/MULE-charsets.")
222
223 ;; Correct by construction, but should be unnecessary:
224 ;; XEmacs hates it.
225 (when (and (not (featurep 'xemacs))
226            (fboundp 'coding-system-list)
227            (fboundp 'sort-coding-systems))
228   (setq mm-mime-mule-charset-alist
229         (apply
230          'nconc
231          (mapcar
232           (lambda (cs)
233             (when (and (coding-system-get cs 'mime-charset)
234                        (not (eq t (coding-system-get cs 'safe-charsets))))
235               (list (cons (coding-system-get cs 'mime-charset)
236                           (delq 'ascii
237                                 (coding-system-get cs 'safe-charsets))))))
238           (sort-coding-systems (coding-system-list 'base-only))))))
239
240 ;;; Internal variables:
241
242 ;;; Functions:
243
244 (defun mm-mule-charset-to-mime-charset (charset)
245   "Return the MIME charset corresponding to the given Mule CHARSET."
246   (if (fboundp 'find-coding-systems-for-charsets)
247       (let (mime)
248         (dolist (cs (find-coding-systems-for-charsets (list charset)))
249           (unless mime
250             (when cs
251               (setq mime (coding-system-get cs 'mime-charset)))))
252         mime)
253     (let ((alist mm-mime-mule-charset-alist)
254           out)
255       (while alist
256         (when (memq charset (cdar alist))
257           (setq out (caar alist)
258                 alist nil))
259         (pop alist))
260       out)))
261
262 (defun mm-charset-to-coding-system (charset &optional lbt)
263   "Return coding-system corresponding to CHARSET.
264 CHARSET is a symbol naming a MIME charset.
265 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
266 used as the line break code type of the coding system."
267   (when (stringp charset)
268     (setq charset (intern (downcase charset))))
269   (when lbt
270     (setq charset (intern (format "%s-%s" charset lbt))))
271   (cond
272    ((null charset)
273     charset)
274    ;; Running in a non-MULE environment.
275    ((null (mm-get-coding-system-list))
276     charset)
277    ;; ascii
278    ((eq charset 'us-ascii)
279     'ascii)
280    ;; Check to see whether we can handle this charset.  (This depends
281    ;; on there being some coding system matching each `mime-charset'
282    ;; property defined, as there should be.)
283    ((and (mm-coding-system-p charset)
284 ;;; Doing this would potentially weed out incorrect charsets.
285 ;;;      charset
286 ;;;      (eq charset (coding-system-get charset 'mime-charset))
287          )
288     charset)
289    ;; Translate invalid charsets.
290    ((mm-coding-system-p (setq charset
291                            (cdr (assq charset
292                                       mm-charset-synonym-alist))))
293     charset)
294    ;; Last resort: search the coding system list for entries which
295    ;; have the right mime-charset in case the canonical name isn't
296    ;; defined (though it should be).
297    ((let (cs)
298       ;; mm-get-coding-system-list returns a list of cs without lbt.
299       ;; Do we need -lbt?
300       (dolist (c (mm-get-coding-system-list))
301         (if (and (null cs)
302                  (eq charset (coding-system-get c 'mime-charset)))
303             (setq cs c)))
304       cs))))
305
306 (defsubst mm-replace-chars-in-string (string from to)
307   (mm-subst-char-in-string from to string))
308
309 (eval-and-compile
310   (defvar mm-emacs-mule (and (not (featurep 'xemacs))
311                              (boundp 'default-enable-multibyte-characters)
312                              default-enable-multibyte-characters
313                              (fboundp 'set-buffer-multibyte))
314     "Emacs mule.")
315   
316   (defvar mm-mule4-p (and mm-emacs-mule
317                           (fboundp 'charsetp)
318                           (not (charsetp 'eight-bit-control)))
319     "Mule version 4.")
320
321   (if mm-emacs-mule
322       (defun mm-enable-multibyte ()
323         "Set the multibyte flag of the current buffer.
324 Only do this if the default value of `enable-multibyte-characters' is
325 non-nil.  This is a no-op in XEmacs."
326         (set-buffer-multibyte t))
327     (defalias 'mm-enable-multibyte 'ignore))
328
329   (if mm-emacs-mule
330       (defun mm-disable-multibyte ()
331         "Unset the multibyte flag of in the current buffer.
332 This is a no-op in XEmacs."
333         (set-buffer-multibyte nil))
334     (defalias 'mm-disable-multibyte 'ignore))
335
336   (if mm-mule4-p
337       (defun mm-enable-multibyte-mule4  ()
338         "Enable multibyte in the current buffer.
339 Only used in Emacs Mule 4."
340         (set-buffer-multibyte t))
341     (defalias 'mm-enable-multibyte-mule4 'ignore))
342   
343   (if mm-mule4-p
344       (defun mm-disable-multibyte-mule4 ()
345         "Disable multibyte in the current buffer.
346 Only used in Emacs Mule 4."
347         (set-buffer-multibyte nil))
348     (defalias 'mm-disable-multibyte-mule4 'ignore)))
349
350 (defun mm-preferred-coding-system (charset)
351   ;; A typo in some Emacs versions.
352   (or (get-charset-property charset 'prefered-coding-system)
353       (get-charset-property charset 'preferred-coding-system)))
354
355 (defun mm-charset-after (&optional pos)
356   "Return charset of a character in current buffer at position POS.
357 If POS is nil, it defauls to the current point.
358 If POS is out of range, the value is nil.
359 If the charset is `composition', return the actual one."
360   (let ((char (char-after pos)) charset)
361     (if (< (mm-char-int char) 128)
362         (setq charset 'ascii)
363       ;; charset-after is fake in some Emacsen.
364       (setq charset (and (fboundp 'char-charset) (char-charset char)))
365       (if (eq charset 'composition)
366           (let ((p (or pos (point))))
367             (cadr (find-charset-region p (1+ p))))
368         (if (and charset (not (memq charset '(ascii eight-bit-control
369                                                     eight-bit-graphic))))
370             charset
371           (or
372            mail-parse-mule-charset ;; cached mule-charset
373            (progn
374              (setq mail-parse-mule-charset
375                    (and (boundp 'current-language-environment)
376                         (car (last
377                               (assq 'charset
378                                     (assoc current-language-environment
379                                            language-info-alist))))))
380              (if (or (not mail-parse-mule-charset)
381                      (eq mail-parse-mule-charset 'ascii))
382                  (setq mail-parse-mule-charset
383                        (or (car (last (assq mail-parse-charset
384                                             mm-mime-mule-charset-alist)))
385                            ;; Fixme: don't fix that!
386                            'latin-iso8859-1)))
387              mail-parse-mule-charset)))))))
388
389 (defun mm-mime-charset (charset)
390   "Return the MIME charset corresponding to the given Mule CHARSET."
391   (if (eq charset 'unknown)
392       (error "The message contains non-printable characters, please use attachment"))
393   (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
394       ;; This exists in Emacs 20.
395       (or
396        (and (mm-preferred-coding-system charset)
397             (coding-system-get
398              (mm-preferred-coding-system charset) 'mime-charset))
399        (and (eq charset 'ascii)
400             'us-ascii)
401        (mm-preferred-coding-system charset)
402        (mm-mule-charset-to-mime-charset charset))
403     ;; This is for XEmacs.
404     (mm-mule-charset-to-mime-charset charset)))
405
406 (defun mm-delete-duplicates (list)
407   "Simple  substitute for CL `delete-duplicates', testing with `equal'."
408   (let (result head)
409     (while list
410       (setq head (car list))
411       (setq list (delete head list))
412       (setq result (cons head result)))
413     (nreverse result)))
414
415 ;; It's not clear whether this is supposed to mean the global or local
416 ;; setting.  I think it's used inconsistently.  -- fx
417 (defsubst mm-multibyte-p ()
418   "Say whether multibyte is enabled."
419   (if (and (not (featurep 'xemacs))
420            (boundp 'enable-multibyte-characters))
421       enable-multibyte-characters
422     (featurep 'mule)))
423
424 (defun mm-find-mime-charset-region (b e)
425   "Return the MIME charsets needed to encode the region between B and E.
426 Nil means ASCII, a single-element list represents an appropriate MIME
427 charset, and a longer list means no appropriate charset."
428   ;; The return possibilities of this function are a mess...
429   (or (and
430        (mm-multibyte-p)
431        (fboundp 'find-coding-systems-region)
432        ;; Find the mime-charset of the most preferred coding
433        ;; system that has one.
434        (let ((systems (find-coding-systems-region b e))
435              result)
436          ;; Fixme: The `mime-charset' (`x-ctext') of `compound-text'
437          ;; is not in the IANA list.
438          (setq systems (delq 'compound-text systems))
439          (unless (equal systems '(undecided))
440            (while systems
441              (let ((cs (coding-system-get (pop systems) 'mime-charset)))
442                (if cs
443                    (setq systems nil
444                          result (list cs))))))
445          result))
446       ;; Otherwise we're not multibyte, XEmacs or a single coding
447       ;; system won't cover it.
448       (let ((charsets 
449              (mm-delete-duplicates
450               (mapcar 'mm-mime-charset
451                       (delq 'ascii
452                             (mm-find-charset-region b e))))))
453         (if (memq 'iso-2022-jp-2 charsets)
454             (delq 'iso-2022-jp charsets)
455           charsets))))
456
457 (defmacro mm-with-unibyte-buffer (&rest forms)
458   "Create a temporary buffer, and evaluate FORMS there like `progn'.
459 Use unibyte mode for this."
460   `(let (default-enable-multibyte-characters)
461      (with-temp-buffer ,@forms)))
462 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
463 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
464
465 (defmacro mm-with-unibyte-current-buffer (&rest forms)
466   "Evaluate FORMS with current current buffer temporarily made unibyte.
467 Also bind `default-enable-multibyte-characters' to nil.
468 Equivalent to `progn' in XEmacs"
469   (let ((multibyte (make-symbol "multibyte"))
470         (buffer (make-symbol "buffer")))
471     `(if mm-emacs-mule 
472          (let ((,multibyte enable-multibyte-characters)
473                (,buffer (current-buffer)))
474            (unwind-protect
475                (let (default-enable-multibyte-characters)
476                  (set-buffer-multibyte nil)
477                  ,@forms)
478              (set-buffer ,buffer)
479              (set-buffer-multibyte ,multibyte)))
480        (let (default-enable-multibyte-characters)
481          ,@forms))))
482 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
483 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
484
485 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
486   "Evaluate FORMS there like `progn' in current buffer.
487 Mule4 only."
488   (let ((multibyte (make-symbol "multibyte"))
489         (buffer (make-symbol "buffer")))
490     `(if mm-mule4-p
491          (let ((,multibyte enable-multibyte-characters)
492                (,buffer (current-buffer)))
493            (unwind-protect
494                (let (default-enable-multibyte-characters)
495                  (set-buffer-multibyte nil)
496                  ,@forms)
497              (set-buffer ,buffer)
498              (set-buffer-multibyte ,multibyte)))
499        (let (default-enable-multibyte-characters)
500          ,@forms))))
501 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
502 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
503
504 (defmacro mm-with-unibyte (&rest forms)
505   "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
506   `(let (default-enable-multibyte-characters)
507      ,@forms))
508 (put 'mm-with-unibyte 'lisp-indent-function 0)
509 (put 'mm-with-unibyte 'edebug-form-spec '(body))
510
511 (defun mm-find-charset-region (b e)
512   "Return a list of Emacs charsets in the region B to E."
513   (cond
514    ((and (mm-multibyte-p)
515          (fboundp 'find-charset-region))
516     ;; Remove composition since the base charsets have been included.
517     ;; Remove eight-bit-*, treat them as ascii.
518     (let ((css (find-charset-region b e)))
519       (mapcar (lambda (cs) (setq css (delq cs css)))
520               '(composition eight-bit-control eight-bit-graphic
521                             control-1))
522       css))
523    (t
524     ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
525     (save-excursion
526       (save-restriction
527         (narrow-to-region b e)
528         (goto-char (point-min))
529         (skip-chars-forward "\0-\177")
530         (if (eobp)
531             '(ascii)
532           (let (charset)
533             (setq charset
534                   (and (boundp 'current-language-environment)
535                        (car (last (assq 'charset
536                                         (assoc current-language-environment
537                                                language-info-alist))))))
538             (if (eq charset 'ascii) (setq charset nil))
539             (or charset
540                 (setq charset
541                       (car (last (assq mail-parse-charset
542                                        mm-mime-mule-charset-alist)))))
543             (list 'ascii (or charset 'latin-iso8859-1)))))))))
544
545 (if (fboundp 'shell-quote-argument)
546     (defalias 'mm-quote-arg 'shell-quote-argument)
547   (defun mm-quote-arg (arg)
548     "Return a version of ARG that is safe to evaluate in a shell."
549     (let ((pos 0) new-pos accum)
550       ;; *** bug: we don't handle newline characters properly
551       (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
552         (push (substring arg pos new-pos) accum)
553         (push "\\" accum)
554         (push (list (aref arg new-pos)) accum)
555         (setq pos (1+ new-pos)))
556       (if (= pos 0)
557           arg
558         (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
559
560 (defun mm-auto-mode-alist ()
561   "Return an `auto-mode-alist' with only the .gz (etc) thingies."
562   (let ((alist auto-mode-alist)
563         out)
564     (while alist
565       (when (listp (cdar alist))
566         (push (car alist) out))
567       (pop alist))
568     (nreverse out)))
569
570 (defvar mm-inhibit-file-name-handlers
571   '(jka-compr-handler image-file-handler)
572   "A list of handlers doing (un)compression (etc) thingies.")
573
574 (defun mm-insert-file-contents (filename &optional visit beg end replace
575                                          inhibit)
576   "Like `insert-file-contents', q.v., but only reads in the file.
577 A buffer may be modified in several ways after reading into the buffer due
578 to advanced Emacs features, such as file-name-handlers, format decoding,
579 find-file-hooks, etc.
580 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
581   This function ensures that none of these modifications will take place."
582   (let ((format-alist nil)
583         (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
584         (default-major-mode 'fundamental-mode)
585         (enable-local-variables nil)
586         (after-insert-file-functions nil)
587         (enable-local-eval nil)
588         (find-file-hooks nil)
589         (inhibit-file-name-operation (if inhibit
590                                          'insert-file-contents
591                                        inhibit-file-name-operation))
592         (inhibit-file-name-handlers
593          (if inhibit
594              (append mm-inhibit-file-name-handlers
595                      inhibit-file-name-handlers)
596            inhibit-file-name-handlers)))
597     (insert-file-contents filename visit beg end replace)))
598
599 (defun mm-append-to-file (start end filename &optional codesys inhibit)
600   "Append the contents of the region to the end of file FILENAME.
601 When called from a function, expects three arguments,
602 START, END and FILENAME.  START and END are buffer positions
603 saying what text to write.
604 Optional fourth argument specifies the coding system to use when
605 encoding the file.
606 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
607   (let ((coding-system-for-write
608          (or codesys mm-text-coding-system-for-write
609              mm-text-coding-system))
610         (inhibit-file-name-operation (if inhibit
611                                          'append-to-file
612                                        inhibit-file-name-operation))
613         (inhibit-file-name-handlers
614          (if inhibit
615              (append mm-inhibit-file-name-handlers
616                      inhibit-file-name-handlers)
617            inhibit-file-name-handlers)))
618     (append-to-file start end filename)))
619
620 (defun mm-write-region (start end filename &optional append visit lockname
621                               coding-system inhibit)
622
623   "Like `write-region'.
624 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
625   (let ((coding-system-for-write
626          (or coding-system mm-text-coding-system-for-write
627              mm-text-coding-system))
628         (inhibit-file-name-operation (if inhibit
629                                          'write-region
630                                        inhibit-file-name-operation))
631         (inhibit-file-name-handlers
632          (if inhibit
633              (append mm-inhibit-file-name-handlers
634                      inhibit-file-name-handlers)
635            inhibit-file-name-handlers)))
636     (write-region start end filename append visit lockname)))
637
638 (defun mm-image-load-path (&optional package)
639   (let (dir result)
640     (dolist (path load-path (nreverse result))
641       (if (file-directory-p
642            (setq dir (concat (file-name-directory
643                               (directory-file-name path))
644                              "etc/" (or package "gnus/"))))
645           (push dir result))
646       (push path result))))
647
648 (provide 'mm-util)
649
650 ;;; mm-util.el ends here