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