fc1edc7e8a5606b33aba5baed131d8b937b61f43
[gnus] / lisp / mm-util.el
1 ;;; mm-util.el --- Utility functions for Mule and low level things
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30 (require 'mail-prsvr)
31
32 (eval-and-compile
33   (mapcar
34    (lambda (elem)
35      (let ((nfunc (intern (format "mm-%s" (car elem)))))
36        (if (fboundp (car elem))
37            (defalias nfunc (car elem))
38          (defalias nfunc (cdr elem)))))
39    '((decode-coding-string . (lambda (s a) s))
40      (encode-coding-string . (lambda (s a) s))
41      (encode-coding-region . ignore)
42      (coding-system-list . ignore)
43      (decode-coding-region . ignore)
44      (char-int . identity)
45      (coding-system-equal . equal)
46      (annotationp . ignore)
47      (set-buffer-file-coding-system . ignore)
48      (read-charset
49       . (lambda (prompt)
50           "Return a charset."
51           (intern
52            (completing-read
53             prompt
54             (mapcar (lambda (e) (list (symbol-name (car e))))
55                     mm-mime-mule-charset-alist)
56             nil t))))
57      (subst-char-in-string
58       . (lambda (from to string &optional inplace)
59           ;; stolen (and renamed) from nnheader.el
60           "Replace characters in STRING from FROM to TO.
61           Unless optional argument INPLACE is non-nil, return a new string."
62           (let ((string (if inplace string (copy-sequence 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      (replace-in-string
72       . (lambda (string regexp rep &optional literal)
73           "See `replace-regexp-in-string', only the order of args differs."
74           (replace-regexp-in-string regexp rep string nil literal)))
75      (string-as-unibyte . identity)
76      (string-make-unibyte . identity)
77      ;; string-as-multibyte often doesn't really do what you think it does.
78      ;; Example:
79      ;;    (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
80      ;;    (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
81      ;;    (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
82      ;;    (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
83      ;; but
84      ;;    (aref (string-as-multibyte "\201\300") 0) -> 2240
85      ;;    (aref (string-as-multibyte "\201\300") 1) -> <error>
86      ;; Better use string-to-multibyte or encode-coding-string.
87      ;; If you really need string-as-multibyte somewhere it's usually
88      ;; because you're using the internal emacs-mule representation (maybe
89      ;; because you're using string-as-unibyte somewhere), which is
90      ;; generally a problem in itself.
91      ;; Here is an approximate equivalence table to help think about it:
92      ;; (string-as-multibyte s)   ~= (decode-coding-string s 'emacs-mule)
93      ;; (string-to-multibyte s)   ~= (decode-coding-string s 'binary)
94      ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
95      (string-as-multibyte . identity)
96      (string-to-multibyte
97       . (lambda (string)
98           "Return a multibyte string with the same individual chars as string."
99           (mapconcat
100            (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
101            string "")))
102      (multibyte-string-p . ignore)
103      ;; It is not a MIME function, but some MIME functions use it.
104      (make-temp-file . (lambda (prefix &optional dir-flag)
105                          (let ((file (expand-file-name
106                                       (make-temp-name prefix)
107                                       (if (fboundp 'temp-directory)
108                                           (temp-directory)
109                                         temporary-file-directory))))
110                            (if dir-flag
111                                (make-directory file))
112                            file)))
113      (insert-byte . insert-char)
114      (multibyte-char-to-unibyte . identity))))
115
116 (eval-and-compile
117   (defalias 'mm-char-or-char-int-p
118     (cond
119      ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
120      ((fboundp 'char-valid-p) 'char-valid-p)
121      (t 'identity))))
122
123 ;; Fixme:  This seems always to be used to read a MIME charset, so it
124 ;; should be re-named and fixed (in Emacs) to offer completion only on
125 ;; proper charset names (base coding systems which have a
126 ;; mime-charset defined).  XEmacs doesn't believe in mime-charset;
127 ;; test with
128 ;;   `(or (coding-system-get 'iso-8859-1 'mime-charset)
129 ;;        (coding-system-get 'iso-8859-1 :mime-charset))'
130 ;; Actually, there should be an `mm-coding-system-mime-charset'.
131 (eval-and-compile
132   (defalias 'mm-read-coding-system
133     (cond
134      ((fboundp 'read-coding-system)
135       (if (and (featurep 'xemacs)
136                (<= (string-to-number emacs-version) 21.1))
137           (lambda (prompt &optional default-coding-system)
138             (read-coding-system prompt))
139         'read-coding-system))
140      (t (lambda (prompt &optional default-coding-system)
141           "Prompt the user for a coding system."
142           (completing-read
143            prompt (mapcar (lambda (s) (list (symbol-name (car s))))
144                           mm-mime-mule-charset-alist)))))))
145
146 (defvar mm-coding-system-list nil)
147 (defun mm-get-coding-system-list ()
148   "Get the coding system list."
149   (or mm-coding-system-list
150       (setq mm-coding-system-list (mm-coding-system-list))))
151
152 (defun mm-coding-system-p (cs)
153   "Return non-nil if CS is a symbol naming a coding system.
154 In XEmacs, also return non-nil if CS is a coding system object.
155 If CS is available, return CS itself in Emacs, and return a coding
156 system object in XEmacs."
157   (if (fboundp 'find-coding-system)
158       (and cs (find-coding-system cs))
159     (if (fboundp 'coding-system-p)
160         (when (coding-system-p cs)
161           cs)
162       ;; Is this branch ever actually useful?
163       (car (memq cs (mm-get-coding-system-list))))))
164
165 (defvar mm-charset-synonym-alist
166   `(
167     ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
168     ,@(unless (mm-coding-system-p 'x-ctext)
169        '((x-ctext . ctext)))
170     ;; ISO-8859-15 is very similar to ISO-8859-1.  But it's _different_!
171     ,@(unless (mm-coding-system-p 'iso-8859-15)
172        '((iso-8859-15 . iso-8859-1)))
173     ;; BIG-5HKSCS is similar to, but different than, BIG-5.
174     ,@(unless (mm-coding-system-p 'big5-hkscs)
175         '((big5-hkscs . big5)))
176     ;; Windows-1252 is actually a superset of Latin-1.  See also
177     ;; `gnus-article-dumbquotes-map'.
178     ,@(unless (mm-coding-system-p 'windows-1252)
179        (if (mm-coding-system-p 'cp1252)
180            '((windows-1252 . cp1252))
181          '((windows-1252 . iso-8859-1))))
182     ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
183     ;; Outlook users in Czech republic. Use this to allow reading of their
184     ;; e-mails. cp1250 should be defined by M-x codepage-setup.
185     ,@(if (and (not (mm-coding-system-p 'windows-1250))
186                (mm-coding-system-p 'cp1250))
187           '((windows-1250 . cp1250)))
188     ;; A Microsoft misunderstanding.
189     ,@(if (and (not (mm-coding-system-p 'unicode))
190                (mm-coding-system-p 'utf-16-le))
191           '((unicode . utf-16-le)))
192     ;; A Microsoft misunderstanding.
193     ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
194         (if (mm-coding-system-p 'cp949)
195             '((ks_c_5601-1987 . cp949))
196           '((ks_c_5601-1987 . euc-kr))))
197     )
198   "A mapping from invalid charset names to the real charset names.")
199
200 (defvar mm-binary-coding-system
201   (cond
202    ((mm-coding-system-p 'binary) 'binary)
203    ((mm-coding-system-p 'no-conversion) 'no-conversion)
204    (t nil))
205   "100% binary coding system.")
206
207 (defvar mm-text-coding-system
208   (or (if (memq system-type '(windows-nt ms-dos ms-windows))
209           (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
210         (and (mm-coding-system-p 'raw-text) 'raw-text))
211       mm-binary-coding-system)
212   "Text-safe coding system (For removing ^M).")
213
214 (defvar mm-text-coding-system-for-write nil
215   "Text coding system for write.")
216
217 (defvar mm-auto-save-coding-system
218   (cond
219    ((mm-coding-system-p 'utf-8-emacs)   ; Mule 7
220     (if (memq system-type '(windows-nt ms-dos ms-windows))
221         (if (mm-coding-system-p 'utf-8-emacs-dos)
222             'utf-8-emacs-dos mm-binary-coding-system)
223       'utf-8-emacs))
224    ((mm-coding-system-p 'emacs-mule)
225     (if (memq system-type '(windows-nt ms-dos ms-windows))
226         (if (mm-coding-system-p 'emacs-mule-dos)
227             'emacs-mule-dos mm-binary-coding-system)
228       'emacs-mule))
229    ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
230    (t mm-binary-coding-system))
231   "Coding system of auto save file.")
232
233 (defvar mm-universal-coding-system mm-auto-save-coding-system
234   "The universal coding system.")
235
236 ;; Fixme: some of the cars here aren't valid MIME charsets.  That
237 ;; should only matter with XEmacs, though.
238 (defvar mm-mime-mule-charset-alist
239   `((us-ascii ascii)
240     (iso-8859-1 latin-iso8859-1)
241     (iso-8859-2 latin-iso8859-2)
242     (iso-8859-3 latin-iso8859-3)
243     (iso-8859-4 latin-iso8859-4)
244     (iso-8859-5 cyrillic-iso8859-5)
245     ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
246     ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
247     ;; charset is koi8-r, not iso-8859-5.
248     (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
249     (iso-8859-6 arabic-iso8859-6)
250     (iso-8859-7 greek-iso8859-7)
251     (iso-8859-8 hebrew-iso8859-8)
252     (iso-8859-9 latin-iso8859-9)
253     (iso-8859-14 latin-iso8859-14)
254     (iso-8859-15 latin-iso8859-15)
255     (viscii vietnamese-viscii-lower)
256     (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
257     (euc-kr korean-ksc5601)
258     (gb2312 chinese-gb2312)
259     (big5 chinese-big5-1 chinese-big5-2)
260     (tibetan tibetan)
261     (thai-tis620 thai-tis620)
262     (windows-1251 cyrillic-iso8859-5)
263     (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
264     (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
265                    latin-jisx0201 japanese-jisx0208-1978
266                    chinese-gb2312 japanese-jisx0208
267                    korean-ksc5601 japanese-jisx0212)
268     (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
269                     latin-jisx0201 japanese-jisx0208-1978
270                     chinese-gb2312 japanese-jisx0208
271                     korean-ksc5601 japanese-jisx0212
272                     chinese-cns11643-1 chinese-cns11643-2)
273     (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
274                     cyrillic-iso8859-5 greek-iso8859-7
275                     latin-jisx0201 japanese-jisx0208-1978
276                     chinese-gb2312 japanese-jisx0208
277                     korean-ksc5601 japanese-jisx0212
278                     chinese-cns11643-1 chinese-cns11643-2
279                     chinese-cns11643-3 chinese-cns11643-4
280                     chinese-cns11643-5 chinese-cns11643-6
281                     chinese-cns11643-7)
282     (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
283                    japanese-jisx0213-1 japanese-jisx0213-2)
284     (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
285     ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
286              (charsetp 'unicode-a)
287              (not (mm-coding-system-p 'mule-utf-8)))
288          '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
289        ;; If we have utf-8 we're in Mule 5+.
290        (append '(utf-8)
291                (delete 'ascii
292                        (coding-system-get 'mule-utf-8 'safe-charsets)))))
293   "Alist of MIME-charset/MULE-charsets.")
294
295 (defun mm-enrich-utf-8-by-mule-ucs ()
296   "Make the `utf-8' MIME charset usable by the Mule-UCS package.
297 This function will run when the `un-define' module is loaded under
298 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
299 with Mule charsets.  It is completely useless for Emacs."
300   (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
301                        (assoc "un-define" after-load-alist)))
302     (setq after-load-alist
303           (delete '("un-define") after-load-alist)))
304   (when (boundp 'unicode-basic-translation-charset-order-list)
305     (condition-case nil
306         (let ((val (delq
307                     'ascii
308                     (copy-sequence
309                      (symbol-value
310                       'unicode-basic-translation-charset-order-list))))
311               (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
312           (if elem
313               (setcdr elem val)
314             (setq mm-mime-mule-charset-alist
315                   (nconc mm-mime-mule-charset-alist
316                          (list (cons 'utf-8 val))))))
317       (error))))
318
319 ;; Correct by construction, but should be unnecessary for Emacs:
320 (if (featurep 'xemacs)
321     (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
322   (when (and (fboundp 'coding-system-list)
323              (fboundp 'sort-coding-systems))
324     (let ((css (sort-coding-systems (coding-system-list 'base-only)))
325           cs mime mule alist)
326       (while css
327         (setq cs (pop css)
328               mime (or (coding-system-get cs :mime-charset) ; Emacs 22
329                        (coding-system-get cs 'mime-charset)))
330         (when (and mime
331                    (not (eq t (setq mule
332                                     (coding-system-get cs 'safe-charsets))))
333                    (not (assq mime alist)))
334           (push (cons mime (delq 'ascii mule)) alist)))
335       (setq mm-mime-mule-charset-alist (nreverse alist)))))
336
337 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
338   "A list of special charsets.
339 Valid elements include:
340 `iso-8859-15'    convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
341 `iso-2022-jp-2'  convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
342 )
343
344 (defvar mm-iso-8859-15-compatible
345   '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
346     (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
347   "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
348
349 (defvar mm-iso-8859-x-to-15-table
350   (and (fboundp 'coding-system-p)
351        (mm-coding-system-p 'iso-8859-15)
352        (mapcar
353         (lambda (cs)
354           (if (mm-coding-system-p (car cs))
355               (let ((c (string-to-char
356                         (decode-coding-string "\341" (car cs)))))
357                 (cons (char-charset c)
358                       (cons
359                        (- (string-to-char
360                            (decode-coding-string "\341" 'iso-8859-15)) c)
361                        (string-to-list (decode-coding-string (car (cdr cs))
362                                                              (car cs))))))
363             '(gnus-charset 0)))
364         mm-iso-8859-15-compatible))
365   "A table of the difference character between ISO-8859-X and ISO-8859-15.")
366
367 (defcustom mm-coding-system-priorities
368   (if (boundp 'current-language-environment)
369       (let ((lang (symbol-value 'current-language-environment)))
370         (cond ((string= lang "Japanese")
371                ;; Japanese users prefer iso-2022-jp to euc-japan or
372                ;; shift_jis, however iso-8859-1 should be used when
373                ;; there are only ASCII text and Latin-1 characters.
374                '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
375   "Preferred coding systems for encoding outgoing messages.
376
377 More than one suitable coding system may be found for some text.
378 By default, the coding system with the highest priority is used
379 to encode outgoing messages (see `sort-coding-systems').  If this
380 variable is set, it overrides the default priority."
381   :version "21.2"
382   :type '(repeat (symbol :tag "Coding system"))
383   :group 'mime)
384
385 ;; ??
386 (defvar mm-use-find-coding-systems-region
387   (fboundp 'find-coding-systems-region)
388   "Use `find-coding-systems-region' to find proper coding systems.
389
390 Setting it to nil is useful on Emacsen supporting Unicode if sending
391 mail with multiple parts is preferred to sending a Unicode one.")
392
393 ;;; Internal variables:
394
395 ;;; Functions:
396
397 (defun mm-mule-charset-to-mime-charset (charset)
398   "Return the MIME charset corresponding to the given Mule CHARSET."
399   (if (and (fboundp 'find-coding-systems-for-charsets)
400            (fboundp 'sort-coding-systems))
401       (let ((css (sort (sort-coding-systems
402                         (find-coding-systems-for-charsets (list charset)))
403                        'mm-sort-coding-systems-predicate))
404             cs mime)
405         (while (and (not mime)
406                     css)
407           (when (setq cs (pop css))
408             (setq mime (or (coding-system-get cs :mime-charset)
409                            (coding-system-get cs 'mime-charset)))))
410         mime)
411     (let ((alist (mapcar (lambda (cs)
412                            (assq cs mm-mime-mule-charset-alist))
413                          (sort (mapcar 'car mm-mime-mule-charset-alist)
414                                'mm-sort-coding-systems-predicate)))
415           out)
416       (while alist
417         (when (memq charset (cdar alist))
418           (setq out (caar alist)
419                 alist nil))
420         (pop alist))
421       out)))
422
423 (defun mm-charset-to-coding-system (charset &optional lbt)
424   "Return coding-system corresponding to CHARSET.
425 CHARSET is a symbol naming a MIME charset.
426 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
427 used as the line break code type of the coding system."
428   (when (stringp charset)
429     (setq charset (intern (downcase charset))))
430   (when lbt
431     (setq charset (intern (format "%s-%s" charset lbt))))
432   (cond
433    ((null charset)
434     charset)
435    ;; Running in a non-MULE environment.
436    ((or (null (mm-get-coding-system-list))
437         (not (fboundp 'coding-system-get)))
438     charset)
439    ;; ascii
440    ((eq charset 'us-ascii)
441     'ascii)
442    ;; Check to see whether we can handle this charset.  (This depends
443    ;; on there being some coding system matching each `mime-charset'
444    ;; property defined, as there should be.)
445    ((and (mm-coding-system-p charset)
446 ;;; Doing this would potentially weed out incorrect charsets.
447 ;;;      charset
448 ;;;      (eq charset (coding-system-get charset 'mime-charset))
449          )
450     charset)
451    ;; Translate invalid charsets.
452    ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
453       (and cs (mm-coding-system-p cs) cs)))
454    ;; Last resort: search the coding system list for entries which
455    ;; have the right mime-charset in case the canonical name isn't
456    ;; defined (though it should be).
457    ((let (cs)
458       ;; mm-get-coding-system-list returns a list of cs without lbt.
459       ;; Do we need -lbt?
460       (dolist (c (mm-get-coding-system-list))
461         (if (and (null cs)
462                  (eq charset (or (coding-system-get c :mime-charset)
463                                  (coding-system-get c 'mime-charset))))
464             (setq cs c)))
465       cs))))
466
467 (eval-and-compile
468   (defvar mm-emacs-mule (and (not (featurep 'xemacs))
469                              (boundp 'default-enable-multibyte-characters)
470                              default-enable-multibyte-characters
471                              (fboundp 'set-buffer-multibyte))
472     "True in Emacs with Mule.")
473
474   (if mm-emacs-mule
475       (defun mm-enable-multibyte ()
476         "Set the multibyte flag of the current buffer.
477 Only do this if the default value of `enable-multibyte-characters' is
478 non-nil.  This is a no-op in XEmacs."
479         (set-buffer-multibyte 'to))
480     (defalias 'mm-enable-multibyte 'ignore))
481
482   (if mm-emacs-mule
483       (defun mm-disable-multibyte ()
484         "Unset the multibyte flag of in the current buffer.
485 This is a no-op in XEmacs."
486         (set-buffer-multibyte nil))
487     (defalias 'mm-disable-multibyte 'ignore)))
488
489 (defun mm-preferred-coding-system (charset)
490   ;; A typo in some Emacs versions.
491   (or (get-charset-property charset 'preferred-coding-system)
492       (get-charset-property charset 'prefered-coding-system)))
493
494 ;; Mule charsets shouldn't be used.
495 (defsubst mm-guess-charset ()
496   "Guess Mule charset from the language environment."
497   (or
498    mail-parse-mule-charset ;; cached mule-charset
499    (progn
500      (setq mail-parse-mule-charset
501            (and (boundp 'current-language-environment)
502                 (car (last
503                       (assq 'charset
504                             (assoc current-language-environment
505                                    language-info-alist))))))
506      (if (or (not mail-parse-mule-charset)
507              (eq mail-parse-mule-charset 'ascii))
508          (setq mail-parse-mule-charset
509                (or (car (last (assq mail-parse-charset
510                                     mm-mime-mule-charset-alist)))
511                    ;; default
512                    'latin-iso8859-1)))
513      mail-parse-mule-charset)))
514
515 (defun mm-charset-after (&optional pos)
516   "Return charset of a character in current buffer at position POS.
517 If POS is nil, it defauls to the current point.
518 If POS is out of range, the value is nil.
519 If the charset is `composition', return the actual one."
520   (let ((char (char-after pos)) charset)
521     (if (< (mm-char-int char) 128)
522         (setq charset 'ascii)
523       ;; charset-after is fake in some Emacsen.
524       (setq charset (and (fboundp 'char-charset) (char-charset char)))
525       (if (eq charset 'composition)     ; Mule 4
526           (let ((p (or pos (point))))
527             (cadr (find-charset-region p (1+ p))))
528         (if (and charset (not (memq charset '(ascii eight-bit-control
529                                                     eight-bit-graphic))))
530             charset
531           (mm-guess-charset))))))
532
533 (defun mm-mime-charset (charset)
534   "Return the MIME charset corresponding to the given Mule CHARSET."
535   (if (eq charset 'unknown)
536       (error "The message contains non-printable characters, please use attachment"))
537   (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
538       ;; This exists in Emacs 20.
539       (or
540        (and (mm-preferred-coding-system charset)
541             (or (coding-system-get
542                  (mm-preferred-coding-system charset) :mime-charset)
543                 (coding-system-get
544                  (mm-preferred-coding-system charset) 'mime-charset)))
545        (and (eq charset 'ascii)
546             'us-ascii)
547        (mm-preferred-coding-system charset)
548        (mm-mule-charset-to-mime-charset charset))
549     ;; This is for XEmacs.
550     (mm-mule-charset-to-mime-charset charset)))
551
552 (if (fboundp 'delete-dups)
553     (defalias 'mm-delete-duplicates 'delete-dups)
554   (defun mm-delete-duplicates (list)
555     "Destructively remove `equal' duplicates from LIST.
556 Store the result in LIST and return it.  LIST must be a proper list.
557 Of several `equal' occurrences of an element in LIST, the first
558 one is kept.
559
560 This is a compatibility function for Emacsen without `delete-dups'."
561     ;; Code from `subr.el' in Emacs 22:
562     (let ((tail list))
563       (while tail
564         (setcdr tail (delete (car tail) (cdr tail)))
565         (setq tail (cdr tail))))
566     list))
567
568 ;; Fixme:  This is used in places when it should be testing the
569 ;; default multibyteness.  See mm-default-multibyte-p.
570 (eval-and-compile
571   (if (and (not (featurep 'xemacs))
572            (boundp 'enable-multibyte-characters))
573       (defun mm-multibyte-p ()
574         "Non-nil if multibyte is enabled in the current buffer."
575         enable-multibyte-characters)
576     (defun mm-multibyte-p () (featurep 'mule))))
577
578 (defun mm-default-multibyte-p ()
579   "Return non-nil if the session is multibyte.
580 This affects whether coding conversion should be attempted generally."
581   (if (featurep 'mule)
582       (if (boundp 'default-enable-multibyte-characters)
583           default-enable-multibyte-characters
584         t)))
585
586 (defun mm-iso-8859-x-to-15-region (&optional b e)
587   (if (fboundp 'char-charset)
588       (let (charset item c inconvertible)
589         (save-restriction
590           (if e (narrow-to-region b e))
591           (goto-char (point-min))
592           (skip-chars-forward "\0-\177")
593           (while (not (eobp))
594             (cond
595              ((not (setq item (assq (char-charset (setq c (char-after)))
596                                     mm-iso-8859-x-to-15-table)))
597               (forward-char))
598              ((memq c (cdr (cdr item)))
599               (setq inconvertible t)
600               (forward-char))
601              (t
602               (insert-before-markers (prog1 (+ c (car (cdr item)))
603                                        (delete-char 1)))))
604             (skip-chars-forward "\0-\177")))
605         (not inconvertible))))
606
607 (defun mm-sort-coding-systems-predicate (a b)
608   (let ((priorities
609          (mapcar (lambda (cs)
610                    ;; Note: invalid entries are dropped silently
611                    (and (setq cs (mm-coding-system-p cs))
612                         (coding-system-base cs)))
613                  mm-coding-system-priorities)))
614     (and (setq a (mm-coding-system-p a))
615          (if (setq b (mm-coding-system-p b))
616              (> (length (memq (coding-system-base a) priorities))
617                 (length (memq (coding-system-base b) priorities)))
618            t))))
619
620 (eval-when-compile
621   (autoload 'latin-unity-massage-name "latin-unity")
622   (autoload 'latin-unity-maybe-remap "latin-unity")
623   (autoload 'latin-unity-representations-feasible-region "latin-unity")
624   (autoload 'latin-unity-representations-present-region "latin-unity")
625   (defvar latin-unity-coding-systems)
626   (defvar latin-unity-ucs-list))
627
628 (defun mm-xemacs-find-mime-charset-1 (begin end)
629   "Determine which MIME charset to use to send region as message.
630 This uses the XEmacs-specific latin-unity package to better handle the
631 case where identical characters from diverse ISO-8859-? character sets
632 can be encoded using a single one of the corresponding coding systems.
633
634 It treats `mm-coding-system-priorities' as the list of preferred
635 coding systems; a useful example setting for this list in Western
636 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
637 to the very standard Latin 1 coding system, and only move to coding
638 systems that are less supported as is necessary to encode the
639 characters that exist in the buffer.
640
641 Latin Unity doesn't know about those non-ASCII Roman characters that
642 are available in various East Asian character sets.  As such, its
643 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
644 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
645 But this is very much a corner case, so don't worry about it."
646   (let ((systems mm-coding-system-priorities) csets psets curset)
647
648     ;; Load the Latin Unity library, if available.
649     (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
650       (require 'latin-unity))
651
652     ;; Now, can we use it?
653     (if (featurep 'latin-unity)
654         (progn
655           (setq csets (latin-unity-representations-feasible-region begin end)
656                 psets (latin-unity-representations-present-region begin end))
657
658           (catch 'done
659
660             ;; Pass back the first coding system in the preferred list
661             ;; that can encode the whole region.
662             (dolist (curset systems)
663               (setq curset (latin-unity-massage-name 'buffer-default curset))
664
665               ;; If the coding system is a universal coding system, then
666               ;; it can certainly encode all the characters in the region.
667               (if (memq curset latin-unity-ucs-list)
668                   (throw 'done (list curset)))
669
670               ;; If a coding system isn't universal, and isn't in
671               ;; the list that latin unity knows about, we can't
672               ;; decide whether to use it here. Leave that until later
673               ;; in `mm-find-mime-charset-region' function, whence we
674               ;; have been called.
675               (unless (memq curset latin-unity-coding-systems)
676                 (throw 'done nil))
677
678               ;; Right, we know about this coding system, and it may
679               ;; conceivably be able to encode all the characters in
680               ;; the region.
681               (if (latin-unity-maybe-remap begin end curset csets psets t)
682                   (throw 'done (list curset))))
683
684             ;; Can't encode using anything from the
685             ;; `mm-coding-system-priorities' list.
686             ;; Leave `mm-find-mime-charset' to do most of the work.
687             nil))
688
689       ;; Right, latin unity isn't available; let `mm-find-charset-region'
690       ;; take its default action, which equally applies to GNU Emacs.
691       nil)))
692
693 (defmacro mm-xemacs-find-mime-charset (begin end)
694   (when (featurep 'xemacs)
695     `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
696
697 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
698   "Return the MIME charsets needed to encode the region between B and E.
699 nil means ASCII, a single-element list represents an appropriate MIME
700 charset, and a longer list means no appropriate charset."
701   (let (charsets)
702     ;; The return possibilities of this function are a mess...
703     (or (and (mm-multibyte-p)
704              mm-use-find-coding-systems-region
705              ;; Find the mime-charset of the most preferred coding
706              ;; system that has one.
707              (let ((systems (find-coding-systems-region b e)))
708                (when mm-coding-system-priorities
709                  (setq systems
710                        (sort systems 'mm-sort-coding-systems-predicate)))
711                (setq systems (delq 'compound-text systems))
712                (unless (equal systems '(undecided))
713                  (while systems
714                    (let* ((head (pop systems))
715                           (cs (or (coding-system-get head :mime-charset)
716                                   (coding-system-get head 'mime-charset))))
717                      ;; The mime-charset (`x-ctext') of
718                      ;; `compound-text' is not in the IANA list.  We
719                      ;; shouldn't normally use anything here with a
720                      ;; mime-charset having an `x-' prefix.
721                      ;; Fixme:  Allow this to be overridden, since
722                      ;; there is existing use of x-ctext.
723                      ;; Also people apparently need the coding system
724                      ;; `iso-2022-jp-3' (which Mule-UCS defines with
725                      ;; mime-charset, though it's not valid).
726                      (if (and cs
727                               (not (string-match "^[Xx]-" (symbol-name cs)))
728                               ;; UTF-16 of any variety is invalid for
729                               ;; text parts and, unfortunately, has
730                               ;; mime-charset defined both in Mule-UCS
731                               ;; and versions of Emacs.  (The name
732                               ;; might be `mule-utf-16...'  or
733                               ;; `utf-16...'.)
734                               (not (string-match "utf-16" (symbol-name cs))))
735                          (setq systems nil
736                                charsets (list cs))))))
737                charsets))
738         ;; If we're XEmacs, and some coding system is appropriate,
739         ;; mm-xemacs-find-mime-charset will return an appropriate list.
740         ;; Otherwise, we'll get nil, and the next setq will get invoked.
741         (setq charsets (mm-xemacs-find-mime-charset b e))
742
743         ;; We're not multibyte, or a single coding system won't cover it.
744         (setq charsets
745               (mm-delete-duplicates
746                (mapcar 'mm-mime-charset
747                        (delq 'ascii
748                              (mm-find-charset-region b e))))))
749     (if (and (> (length charsets) 1)
750              (memq 'iso-8859-15 charsets)
751              (memq 'iso-8859-15 hack-charsets)
752              (save-excursion (mm-iso-8859-x-to-15-region b e)))
753         (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
754                 mm-iso-8859-15-compatible))
755     (if (and (memq 'iso-2022-jp-2 charsets)
756              (memq 'iso-2022-jp-2 hack-charsets))
757         (setq charsets (delq 'iso-2022-jp charsets)))
758     ;; Attempt to reduce the number of charsets if utf-8 is available.
759     (if (and (featurep 'xemacs)
760              (> (length charsets) 1)
761              (mm-coding-system-p 'utf-8))
762         (let ((mm-coding-system-priorities
763                (cons 'utf-8 mm-coding-system-priorities)))
764           (setq charsets
765                 (mm-delete-duplicates
766                  (mapcar 'mm-mime-charset
767                          (delq 'ascii
768                                (mm-find-charset-region b e)))))))
769     charsets))
770
771 (defmacro mm-with-unibyte-buffer (&rest forms)
772   "Create a temporary buffer, and evaluate FORMS there like `progn'.
773 Use unibyte mode for this."
774   `(let (default-enable-multibyte-characters)
775      (with-temp-buffer ,@forms)))
776 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
777 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
778
779 (defmacro mm-with-multibyte-buffer (&rest forms)
780   "Create a temporary buffer, and evaluate FORMS there like `progn'.
781 Use multibyte mode for this."
782   `(let ((default-enable-multibyte-characters t))
783      (with-temp-buffer ,@forms)))
784 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
785 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
786
787 (defmacro mm-with-unibyte-current-buffer (&rest forms)
788   "Evaluate FORMS with current buffer temporarily made unibyte.
789 Also bind `default-enable-multibyte-characters' to nil.
790 Equivalent to `progn' in XEmacs"
791   (let ((multibyte (make-symbol "multibyte"))
792         (buffer (make-symbol "buffer")))
793     `(if mm-emacs-mule
794          (let ((,multibyte enable-multibyte-characters)
795                (,buffer (current-buffer)))
796            (unwind-protect
797                (let (default-enable-multibyte-characters)
798                  (set-buffer-multibyte nil)
799                  ,@forms)
800              (set-buffer ,buffer)
801              (set-buffer-multibyte ,multibyte)))
802        (let (default-enable-multibyte-characters)
803          ,@forms))))
804 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
805 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
806
807 (defmacro mm-with-unibyte (&rest forms)
808   "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
809   `(let (default-enable-multibyte-characters)
810      ,@forms))
811 (put 'mm-with-unibyte 'lisp-indent-function 0)
812 (put 'mm-with-unibyte 'edebug-form-spec '(body))
813
814 (defmacro mm-with-multibyte (&rest forms)
815   "Eval the FORMS with the default value of `enable-multibyte-characters' t."
816   `(let ((default-enable-multibyte-characters t))
817      ,@forms))
818 (put 'mm-with-multibyte 'lisp-indent-function 0)
819 (put 'mm-with-multibyte 'edebug-form-spec '(body))
820
821 (defun mm-find-charset-region (b e)
822   "Return a list of Emacs charsets in the region B to E."
823   (cond
824    ((and (mm-multibyte-p)
825          (fboundp 'find-charset-region))
826     ;; Remove composition since the base charsets have been included.
827     ;; Remove eight-bit-*, treat them as ascii.
828     (let ((css (find-charset-region b e)))
829       (mapcar (lambda (cs) (setq css (delq cs css)))
830               '(composition eight-bit-control eight-bit-graphic
831                             control-1))
832       css))
833    (t
834     ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
835     (save-excursion
836       (save-restriction
837         (narrow-to-region b e)
838         (goto-char (point-min))
839         (skip-chars-forward "\0-\177")
840         (if (eobp)
841             '(ascii)
842           (let (charset)
843             (setq charset
844                   (and (boundp 'current-language-environment)
845                        (car (last (assq 'charset
846                                         (assoc current-language-environment
847                                                language-info-alist))))))
848             (if (eq charset 'ascii) (setq charset nil))
849             (or charset
850                 (setq charset
851                       (car (last (assq mail-parse-charset
852                                        mm-mime-mule-charset-alist)))))
853             (list 'ascii (or charset 'latin-iso8859-1)))))))))
854
855 (defun mm-auto-mode-alist ()
856   "Return an `auto-mode-alist' with only the .gz (etc) thingies."
857   (let ((alist auto-mode-alist)
858         out)
859     (while alist
860       (when (listp (cdar alist))
861         (push (car alist) out))
862       (pop alist))
863     (nreverse out)))
864
865 (defvar mm-inhibit-file-name-handlers
866   '(jka-compr-handler image-file-handler)
867   "A list of handlers doing (un)compression (etc) thingies.")
868
869 (defun mm-insert-file-contents (filename &optional visit beg end replace
870                                          inhibit)
871   "Like `insert-file-contents', but only reads in the file.
872 A buffer may be modified in several ways after reading into the buffer due
873 to advanced Emacs features, such as file-name-handlers, format decoding,
874 `find-file-hooks', etc.
875 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
876   This function ensures that none of these modifications will take place."
877   (let* ((format-alist nil)
878          (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
879          (default-major-mode 'fundamental-mode)
880          (enable-local-variables nil)
881          (after-insert-file-functions nil)
882          (enable-local-eval nil)
883          (inhibit-file-name-operation (if inhibit
884                                           'insert-file-contents
885                                         inhibit-file-name-operation))
886          (inhibit-file-name-handlers
887           (if inhibit
888               (append mm-inhibit-file-name-handlers
889                       inhibit-file-name-handlers)
890             inhibit-file-name-handlers))
891          (ffh (if (boundp 'find-file-hook)
892                   'find-file-hook
893                 'find-file-hooks))
894          (val (symbol-value ffh)))
895     (set ffh nil)
896     (unwind-protect
897         (insert-file-contents filename visit beg end replace)
898       (set ffh val))))
899
900 (defun mm-append-to-file (start end filename &optional codesys inhibit)
901   "Append the contents of the region to the end of file FILENAME.
902 When called from a function, expects three arguments,
903 START, END and FILENAME.  START and END are buffer positions
904 saying what text to write.
905 Optional fourth argument specifies the coding system to use when
906 encoding the file.
907 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
908   (let ((coding-system-for-write
909          (or codesys mm-text-coding-system-for-write
910              mm-text-coding-system))
911         (inhibit-file-name-operation (if inhibit
912                                          'append-to-file
913                                        inhibit-file-name-operation))
914         (inhibit-file-name-handlers
915          (if inhibit
916              (append mm-inhibit-file-name-handlers
917                      inhibit-file-name-handlers)
918            inhibit-file-name-handlers)))
919     (write-region start end filename t 'no-message)
920     (message "Appended to %s" filename)))
921
922 (defun mm-write-region (start end filename &optional append visit lockname
923                               coding-system inhibit)
924
925   "Like `write-region'.
926 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
927   (let ((coding-system-for-write
928          (or coding-system mm-text-coding-system-for-write
929              mm-text-coding-system))
930         (inhibit-file-name-operation (if inhibit
931                                          'write-region
932                                        inhibit-file-name-operation))
933         (inhibit-file-name-handlers
934          (if inhibit
935              (append mm-inhibit-file-name-handlers
936                      inhibit-file-name-handlers)
937            inhibit-file-name-handlers)))
938     (write-region start end filename append visit lockname)))
939
940 (defun mm-image-load-path (&optional package)
941   (let (dir result)
942     (dolist (path load-path (nreverse result))
943       (when (and path
944                  (file-directory-p
945                   (setq dir (concat (file-name-directory
946                                      (directory-file-name path))
947                                     "etc/images/" (or package "gnus/")))))
948         (push dir result))
949       (push path result))))
950
951 ;; Fixme: This doesn't look useful where it's used.
952 (if (fboundp 'detect-coding-region)
953     (defun mm-detect-coding-region (start end)
954       "Like `detect-coding-region' except returning the best one."
955       (let ((coding-systems
956              (detect-coding-region start end)))
957         (or (car-safe coding-systems)
958             coding-systems)))
959   (defun mm-detect-coding-region (start end)
960     (let ((point (point)))
961       (goto-char start)
962       (skip-chars-forward "\0-\177" end)
963       (prog1
964           (if (eq (point) end) 'ascii (mm-guess-charset))
965         (goto-char point)))))
966
967 (if (fboundp 'coding-system-get)
968     (defun mm-detect-mime-charset-region (start end)
969       "Detect MIME charset of the text in the region between START and END."
970       (let ((cs (mm-detect-coding-region start end)))
971         (or (coding-system-get cs :mime-charset)
972             (coding-system-get cs 'mime-charset))))
973   (defun mm-detect-mime-charset-region (start end)
974     "Detect MIME charset of the text in the region between START and END."
975     (let ((cs (mm-detect-coding-region start end)))
976       cs)))
977
978 (eval-when-compile
979   (unless (fboundp 'coding-system-to-mime-charset)
980     (defalias 'coding-system-to-mime-charset 'ignore)))
981
982 (defun mm-coding-system-to-mime-charset (coding-system)
983   "Return the MIME charset corresponding to CODING-SYSTEM.
984 To make this function work with XEmacs, the APEL package is required."
985   (when coding-system
986     (or (and (fboundp 'coding-system-get)
987              (or (coding-system-get coding-system :mime-charset)
988                  (coding-system-get coding-system 'mime-charset)))
989         (and (featurep 'xemacs)
990              (or (and (fboundp 'coding-system-to-mime-charset)
991                       (not (eq (symbol-function 'coding-system-to-mime-charset)
992                                'ignore)))
993                  (and (condition-case nil
994                           (require 'mcharset)
995                         (error nil))
996                       (fboundp 'coding-system-to-mime-charset)))
997              (coding-system-to-mime-charset coding-system)))))
998
999 (eval-when-compile
1000   (require 'jka-compr))
1001
1002 (defun mm-decompress-buffer (filename &optional inplace force)
1003   "Decompress buffer's contents, depending on jka-compr.
1004 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
1005 agrees with `jka-compr-compression-info-list', decompression is done.
1006 Signal an error if FORCE is neither nil nor t and compressed data are
1007 not decompressed because `auto-compression-mode' is disabled.
1008 If INPLACE is nil, return decompressed data or nil without modifying
1009 the buffer.  Otherwise, replace the buffer's contents with the
1010 decompressed data.  The buffer's multibyteness must be turned off."
1011   (when (and filename
1012              (if force
1013                  (prog1 t (require 'jka-compr))
1014                (and (fboundp 'jka-compr-installed-p)
1015                     (jka-compr-installed-p))))
1016     (let ((info (jka-compr-get-compression-info filename)))
1017       (when info
1018         (unless (or (memq force (list nil t))
1019                     (jka-compr-installed-p))
1020           (error ""))
1021         (let ((prog (jka-compr-info-uncompress-program info))
1022               (args (jka-compr-info-uncompress-args info))
1023               (msg (format "%s %s..."
1024                            (jka-compr-info-uncompress-message info)
1025                            filename))
1026               (err-file (jka-compr-make-temp-name))
1027               (cur (current-buffer))
1028               (coding-system-for-read mm-binary-coding-system)
1029               (coding-system-for-write mm-binary-coding-system)
1030               retval err-msg)
1031           (message "%s" msg)
1032           (with-temp-buffer
1033             (insert-buffer-substring cur)
1034             (condition-case err
1035                 (progn
1036                   (unless (memq (apply 'call-process-region
1037                                        (point-min) (point-max)
1038                                        prog t (list t err-file) nil args)
1039                                 jka-compr-acceptable-retval-list)
1040                     (erase-buffer)
1041                     (insert (mapconcat
1042                              'identity
1043                              (delete "" (split-string
1044                                          (prog2
1045                                              (insert-file-contents err-file)
1046                                              (buffer-string)
1047                                            (erase-buffer))))
1048                              " ")
1049                             "\n")
1050                     (setq err-msg
1051                           (format "Error while executing \"%s %s < %s\""
1052                                   prog (mapconcat 'identity args " ")
1053                                   filename)))
1054                   (setq retval (buffer-string)))
1055               (error
1056                (setq err-msg (error-message-string err)))))
1057           (when (file-exists-p err-file)
1058             (ignore-errors (jka-compr-delete-temp-file err-file)))
1059           (when inplace
1060             (unless err-msg
1061               (delete-region (point-min) (point-max))
1062               (insert retval))
1063             (setq retval nil))
1064           (message "%s" (or err-msg (concat msg "done")))
1065           retval)))))
1066
1067 (eval-when-compile
1068   (unless (fboundp 'coding-system-name)
1069     (defalias 'coding-system-name 'ignore))
1070   (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1071     (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1072   (unless (fboundp 'find-operation-coding-system)
1073     (defalias 'find-operation-coding-system 'ignore)))
1074
1075 (defun mm-find-buffer-file-coding-system (&optional filename)
1076   "Find coding system used to decode the contents of the current buffer.
1077 This function looks for the coding system magic cookie or examines the
1078 coding system specified by `file-coding-system-alist' being associated
1079 with FILENAME which defaults to `buffer-file-name'.  Data compressed by
1080 gzip, bzip2, etc. are allowed."
1081   (unless filename
1082     (setq filename buffer-file-name))
1083   (save-excursion
1084     (let ((decomp (unless ;; No worth to examine charset of tar files.
1085                       (and filename
1086                            (string-match
1087                             "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1088                             filename))
1089                     (mm-decompress-buffer filename nil t))))
1090       (when decomp
1091         (set-buffer (let (default-enable-multibyte-characters)
1092                       (generate-new-buffer " *temp*")))
1093         (insert decomp)
1094         (setq filename (file-name-sans-extension filename)))
1095       (goto-char (point-min))
1096       (prog1
1097           (cond
1098            ((boundp 'set-auto-coding-function) ;; Emacs
1099             (if filename
1100                 (or (funcall (symbol-value 'set-auto-coding-function)
1101                              filename (- (point-max) (point-min)))
1102                     (car (find-operation-coding-system 'insert-file-contents
1103                                                        filename)))
1104               (let (auto-coding-alist)
1105                 (condition-case nil
1106                     (funcall (symbol-value 'set-auto-coding-function)
1107                              nil (- (point-max) (point-min)))
1108                   (error nil)))))
1109            ((featurep 'file-coding) ;; XEmacs
1110             (let ((case-fold-search t)
1111                   (end (point-at-eol))
1112                   codesys start)
1113               (or
1114                (and (re-search-forward "-\\*-+[\t ]*" end t)
1115                     (progn
1116                       (setq start (match-end 0))
1117                       (re-search-forward "[\t ]*-+\\*-" end t))
1118                     (progn
1119                       (setq end (match-beginning 0))
1120                       (goto-char start)
1121                       (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1122                           (re-search-forward
1123                            "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1124                            end t)))
1125                     (find-coding-system (setq codesys
1126                                               (intern (match-string 1))))
1127                     codesys)
1128                (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1129                                        nil t)
1130                     (progn
1131                       (setq start (match-end 0))
1132                       (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1133                     (progn
1134                       (setq end (match-beginning 0))
1135                       (goto-char start)
1136                       (re-search-forward
1137                        "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1138                        end t))
1139                     (find-coding-system (setq codesys
1140                                               (intern (match-string 1))))
1141                     codesys)
1142                (and (progn
1143                       (goto-char (point-min))
1144                       (setq case-fold-search nil)
1145                       (re-search-forward "^;;;coding system: "
1146                                          ;;(+ (point-min) 3000) t))
1147                                          nil t))
1148                     (looking-at "[^\t\n\r ]+")
1149                     (find-coding-system
1150                      (setq codesys (intern (match-string 0))))
1151                     codesys)
1152                (and filename
1153                     (setq codesys
1154                           (find-file-coding-system-for-read-from-filename
1155                            filename))
1156                     (coding-system-name (coding-system-base codesys)))))))
1157         (when decomp
1158           (kill-buffer (current-buffer)))))))
1159
1160 (provide 'mm-util)
1161
1162 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1163 ;;; mm-util.el ends here