Add comments to the mm- emulating functions.
[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, 2006, 2007, 2008 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 3 of the License, or
13 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs < 22.2.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30
31 (eval-when-compile (require 'cl))
32 (require 'mail-prsvr)
33
34 (eval-and-compile
35   (if (featurep 'xemacs)
36       (unless (ignore-errors
37                 (require 'timer-funcs))
38         (require 'timer))
39     (require 'timer)))
40
41 (defvar mm-mime-mule-charset-alist )
42
43 ;; Emulate functions that are not available in every (X)Emacs version.
44 ;; The name of a function is prefixed with mm-, like `mm-char-int' for
45 ;; `char-int' that is a native XEmacs function, not available in Emacs.
46 ;; Gnus programs all should use mm- functions, not the original ones.
47 (eval-and-compile
48   (mapc
49    (lambda (elem)
50      (let ((nfunc (intern (format "mm-%s" (car elem)))))
51        (if (fboundp (car elem))
52            (defalias nfunc (car elem))
53          (defalias nfunc (cdr elem)))))
54    `(;; `coding-system-list' is not available in XEmacs 21.4 built
55      ;; without the `file-coding' feature.
56      (coding-system-list . ignore)
57      ;; `char-int' is an XEmacs function, not available in Emacs.
58      (char-int . identity)
59      ;; `coding-system-equal' is an Emacs function, not available in XEmacs.
60      (coding-system-equal . equal)
61      ;; `annotationp' is an XEmacs function, not available in Emacs.
62      (annotationp . ignore)
63      ;; `set-buffer-file-coding-system' is not available in XEmacs 21.4
64      ;; built without the `file-coding' feature.
65      (set-buffer-file-coding-system . ignore)
66      ;; `read-charset' is an Emacs function, not available in XEmacs.
67      (read-charset
68       . ,(lambda (prompt)
69            "Return a charset."
70            (intern
71             (completing-read
72              prompt
73              (mapcar (lambda (e) (list (symbol-name (car e))))
74                      mm-mime-mule-charset-alist)
75              nil t))))
76      ;; `subst-char-in-string' is not available in XEmacs 21.4.
77      (subst-char-in-string
78       . ,(lambda (from to string &optional inplace)
79            ;; stolen (and renamed) from nnheader.el
80            "Replace characters in STRING from FROM to TO.
81           Unless optional argument INPLACE is non-nil, return a new string."
82            (let ((string (if inplace string (copy-sequence string)))
83                  (len (length string))
84                  (idx 0))
85              ;; Replace all occurrences of FROM with TO.
86              (while (< idx len)
87                (when (= (aref string idx) from)
88                  (aset string idx to))
89                (setq idx (1+ idx)))
90              string)))
91      ;; `replace-in-string' is an XEmacs function, not available in Emacs.
92      (replace-in-string
93       . ,(lambda (string regexp rep &optional literal)
94            "See `replace-regexp-in-string', only the order of args differs."
95            (replace-regexp-in-string regexp rep string nil literal)))
96      ;; `string-as-unibyte' is an Emacs function, not available in XEmacs.
97      (string-as-unibyte . identity)
98      ;; `string-make-unibyte' is an Emacs function, not available in XEmacs.
99      (string-make-unibyte . identity)
100      ;; string-as-multibyte often doesn't really do what you think it does.
101      ;; Example:
102      ;;    (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
103      ;;    (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
104      ;;    (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
105      ;;    (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
106      ;; but
107      ;;    (aref (string-as-multibyte "\201\300") 0) -> 2240
108      ;;    (aref (string-as-multibyte "\201\300") 1) -> <error>
109      ;; Better use string-to-multibyte or encode-coding-string.
110      ;; If you really need string-as-multibyte somewhere it's usually
111      ;; because you're using the internal emacs-mule representation (maybe
112      ;; because you're using string-as-unibyte somewhere), which is
113      ;; generally a problem in itself.
114      ;; Here is an approximate equivalence table to help think about it:
115      ;; (string-as-multibyte s)   ~= (decode-coding-string s 'emacs-mule)
116      ;; (string-to-multibyte s)   ~= (decode-coding-string s 'binary)
117      ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
118      ;; `string-as-multibyte' is an Emacs function, not available in XEmacs.
119      (string-as-multibyte . identity)
120      ;; `multibyte-string-p' is an Emacs function, not available in XEmacs.
121      (multibyte-string-p . ignore)
122      ;; `insert-byte' is available only in Emacs 23.1 or greater.
123      (insert-byte . insert-char)
124      ;; `multibyte-char-to-unibyte' is an Emacs function, not available
125      ;; in XEmacs.
126      (multibyte-char-to-unibyte . identity)
127      ;; `set-buffer-multibyte' is an Emacs function, not available in XEmacs.
128      (set-buffer-multibyte . ignore)
129      ;; `special-display-p' is an Emacs function, not available in XEmacs.
130      (special-display-p
131       . ,(lambda (buffer-name)
132            "Returns non-nil if a buffer named BUFFER-NAME gets a special frame."
133            (and special-display-function
134                 (or (and (member buffer-name special-display-buffer-names) t)
135                     (cdr (assoc buffer-name special-display-buffer-names))
136                     (catch 'return
137                       (dolist (elem special-display-regexps)
138                         (and (stringp elem)
139                              (string-match elem buffer-name)
140                              (throw 'return t))
141                         (and (consp elem)
142                              (stringp (car elem))
143                              (string-match (car elem) buffer-name)
144                              (throw 'return (cdr elem)))))))))
145      ;; `substring-no-properties' is available only in Emacs 22.1 or greater.
146      (substring-no-properties
147       . ,(lambda (string &optional from to)
148            "Return a substring of STRING, without text properties.
149 It starts at index FROM and ending before TO.
150 TO may be nil or omitted; then the substring runs to the end of STRING.
151 If FROM is nil or omitted, the substring starts at the beginning of STRING.
152 If FROM or TO is negative, it counts from the end.
153
154 With one argument, just copy STRING without its properties."
155            (setq string (substring string (or from 0) to))
156            (set-text-properties 0 (length string) nil string)
157            string)))))
158
159 ;; `decode-coding-string', `encode-coding-string', `decode-coding-region'
160 ;; and `encode-coding-region' are available in Emacs and XEmacs built with
161 ;; the `file-coding' feature, but the XEmacs versions treat nil, that is
162 ;; given as the `coding-system' argument, as the `binary' coding system.
163 (eval-and-compile
164   (if (featurep 'xemacs)
165       (if (featurep 'file-coding)
166           (progn
167             (defun mm-decode-coding-string (str coding-system)
168               (if coding-system
169                   (decode-coding-string str coding-system)
170                 str))
171             (defun mm-encode-coding-string (str coding-system)
172               (if coding-system
173                   (encode-coding-string str coding-system)
174                 str))
175             (defun mm-decode-coding-region (start end coding-system)
176               (if coding-system
177                   (decode-coding-region start end coding-system)))
178             (defun mm-encode-coding-region (start end coding-system)
179               (if coding-system
180                   (encode-coding-region start end coding-system))))
181         (defun mm-decode-coding-string (str coding-system) str)
182         (defun mm-encode-coding-string (str coding-system) str)
183         (defalias 'mm-decode-coding-region 'ignore)
184         (defalias 'mm-encode-coding-region 'ignore))
185     (defalias 'mm-decode-coding-string 'decode-coding-string)
186     (defalias 'mm-encode-coding-string 'encode-coding-string)
187     (defalias 'mm-decode-coding-region 'decode-coding-region)
188     (defalias 'mm-encode-coding-region 'encode-coding-region)))
189
190 ;; `string-to-multibyte' is available only in Emacs 22.1 or greater.
191 (defalias 'mm-string-to-multibyte
192   (cond
193    ((featurep 'xemacs)
194     'identity)
195    ((fboundp 'string-to-multibyte)
196     'string-to-multibyte)
197    (t
198     (lambda (string)
199       "Return a multibyte string with the same individual chars as STRING."
200       (mapconcat
201        (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
202        string "")))))
203
204 ;; `char-or-char-int-p' is an XEmacs function, not available in Emacs.
205 (eval-and-compile
206   (defalias 'mm-char-or-char-int-p
207     (cond
208      ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
209      ((fboundp 'char-valid-p) 'char-valid-p)
210      (t 'identity))))
211
212 ;; Fixme:  This seems always to be used to read a MIME charset, so it
213 ;; should be re-named and fixed (in Emacs) to offer completion only on
214 ;; proper charset names (base coding systems which have a
215 ;; mime-charset defined).  XEmacs doesn't believe in mime-charset;
216 ;; test with
217 ;;   `(or (coding-system-get 'iso-8859-1 'mime-charset)
218 ;;        (coding-system-get 'iso-8859-1 :mime-charset))'
219 ;; Actually, there should be an `mm-coding-system-mime-charset'.
220 (eval-and-compile
221   (defalias 'mm-read-coding-system
222     (cond
223      ((fboundp 'read-coding-system)
224       (if (and (featurep 'xemacs)
225                (<= (string-to-number emacs-version) 21.1))
226           (lambda (prompt &optional default-coding-system)
227             (read-coding-system prompt))
228         'read-coding-system))
229      (t (lambda (prompt &optional default-coding-system)
230           "Prompt the user for a coding system."
231           (completing-read
232            prompt (mapcar (lambda (s) (list (symbol-name (car s))))
233                           mm-mime-mule-charset-alist)))))))
234
235 (defvar mm-coding-system-list nil)
236 (defun mm-get-coding-system-list ()
237   "Get the coding system list."
238   (or mm-coding-system-list
239       (setq mm-coding-system-list (mm-coding-system-list))))
240
241 (defun mm-coding-system-p (cs)
242   "Return non-nil if CS is a symbol naming a coding system.
243 In XEmacs, also return non-nil if CS is a coding system object.
244 If CS is available, return CS itself in Emacs, and return a coding
245 system object in XEmacs."
246   (if (fboundp 'find-coding-system)
247       (and cs (find-coding-system cs))
248     (if (fboundp 'coding-system-p)
249         (when (coding-system-p cs)
250           cs)
251       ;; no-MULE XEmacs:
252       (car (memq cs (mm-get-coding-system-list))))))
253
254 (defun mm-codepage-setup (number &optional alias)
255   "Create a coding system cpNUMBER.
256 The coding system is created using `codepage-setup'.  If ALIAS is
257 non-nil, an alias is created and added to
258 `mm-charset-synonym-alist'.  If ALIAS is a string, it's used as
259 the alias.  Else windows-NUMBER is used."
260   (interactive
261    (let ((completion-ignore-case t)
262          (candidates (if (fboundp 'cp-supported-codepages)
263                          (cp-supported-codepages)
264                        ;; Removed in Emacs 23 (unicode), so signal an error:
265                        (error "`codepage-setup' not present in this Emacs version."))))
266      (list (completing-read "Setup DOS Codepage: (default 437) " candidates
267                             nil t nil nil "437"))))
268   (when alias
269     (setq alias (if (stringp alias)
270                     (intern alias)
271                   (intern (format "windows-%s" number)))))
272   (let* ((cp (intern (format "cp%s" number))))
273     (unless (mm-coding-system-p cp)
274       (if (fboundp 'codepage-setup)     ; silence compiler
275           (codepage-setup number)
276         (error "`codepage-setup' not present in this Emacs version.")))
277     (when (and alias
278                ;; Don't add alias if setup of cp failed.
279                (mm-coding-system-p cp))
280       (add-to-list 'mm-charset-synonym-alist (cons alias cp)))))
281
282 (defvar mm-charset-synonym-alist
283   `(
284     ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
285     ,@(unless (mm-coding-system-p 'x-ctext)
286         '((x-ctext . ctext)))
287     ;; ISO-8859-15 is very similar to ISO-8859-1.  But it's _different_ in 8
288     ;; positions!
289     ,@(unless (mm-coding-system-p 'iso-8859-15)
290         '((iso-8859-15 . iso-8859-1)))
291     ;; BIG-5HKSCS is similar to, but different than, BIG-5.
292     ,@(unless (mm-coding-system-p 'big5-hkscs)
293         '((big5-hkscs . big5)))
294     ;; A Microsoft misunderstanding.
295     ,@(when (and (not (mm-coding-system-p 'unicode))
296                  (mm-coding-system-p 'utf-16-le))
297         '((unicode . utf-16-le)))
298     ;; A Microsoft misunderstanding.
299     ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
300         (if (mm-coding-system-p 'cp949)
301             '((ks_c_5601-1987 . cp949))
302           '((ks_c_5601-1987 . euc-kr))))
303     ;; Windows-31J is Windows Codepage 932.
304     ,@(when (and (not (mm-coding-system-p 'windows-31j))
305                  (mm-coding-system-p 'cp932))
306         '((windows-31j . cp932)))
307     ;; Charset name: GBK, Charset aliases: CP936, MS936, windows-936
308     ;; http://www.iana.org/assignments/charset-reg/GBK
309     ;; Emacs 22.1 has cp936, but not gbk, so we alias it:
310     ,@(when (and (not (mm-coding-system-p 'gbk))
311                  (mm-coding-system-p 'cp936))
312         '((gbk . cp936)))
313     ;; UTF8 is a bogus name for UTF-8
314     ,@(when (and (not (mm-coding-system-p 'utf8))
315                  (mm-coding-system-p 'utf-8))
316         '((utf8 . utf-8)))
317     ;; ISO8859-1 is a bogus name for ISO-8859-1
318     ,@(when (and (not (mm-coding-system-p 'iso8859-1))
319                  (mm-coding-system-p 'iso-8859-1))
320         '((iso8859-1 . iso-8859-1)))
321     ;; ISO_8859-1 is a bogus name for ISO-8859-1
322     ,@(when (and (not (mm-coding-system-p 'iso_8859-1))
323                  (mm-coding-system-p 'iso-8859-1))
324         '((iso_8859-1 . iso-8859-1)))
325     )
326   "A mapping from unknown or invalid charset names to the real charset names.
327
328 See `mm-codepage-iso-8859-list' and `mm-codepage-ibm-list'.")
329
330 (defcustom mm-codepage-iso-8859-list
331   (list 1250 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
332         ;; Outlook users in Czech republic.  Use this to allow reading of
333         ;; their e-mails.  cp1250 should be defined by M-x codepage-setup
334         ;; (Emacs 21).
335         '(1252 . 1) ;; Windows-1252 is a superset of iso-8859-1 (West
336                     ;; Europe).  See also `gnus-article-dumbquotes-map'.
337         '(1254 . 9) ;; Windows-1254 is a superset of iso-8859-9 (Turkish).
338         '(1255 . 8));; Windows-1255 is a superset of iso-8859-8 (Hebrew).
339   "A list of Windows codepage numbers and iso-8859 charset numbers.
340
341 If an element is a number corresponding to a supported windows
342 codepage, appropriate entries to `mm-charset-synonym-alist' are
343 added by `mm-setup-codepage-iso-8859'.  An element may also be a
344 cons cell where the car is a codepage number and the cdr is the
345 corresponding number of an iso-8859 charset."
346   :type '(list (set :inline t
347                     (const 1250 :tag "Central and East European")
348                     (const (1252 . 1) :tag "West European")
349                     (const (1254 . 9) :tag "Turkish")
350                     (const (1255 . 8) :tag "Hebrew"))
351                (repeat :inline t
352                        :tag "Other options"
353                        (choice
354                         (integer :tag "Windows codepage number")
355                         (cons (integer :tag "Windows codepage number")
356                               (integer :tag "iso-8859 charset  number")))))
357   :version "22.1" ;; Gnus 5.10.9
358   :group 'mime)
359
360 (defcustom mm-codepage-ibm-list
361   (list 437 ;; (US etc.)
362         860 ;; (Portugal)
363         861 ;; (Iceland)
364         862 ;; (Israel)
365         863 ;; (Canadian French)
366         865 ;; (Nordic)
367         852 ;;
368         850 ;; (Latin 1)
369         855 ;; (Cyrillic)
370         866 ;; (Cyrillic - Russian)
371         857 ;; (Turkish)
372         864 ;; (Arabic)
373         869 ;; (Greek)
374         874);; (Thai)
375   ;; In Emacs 23 (unicode), cp... and ibm... are aliases.
376   ;; Cf. http://thread.gmane.org/v9lkng5nwy.fsf@marauder.physik.uni-ulm.de
377   "List of IBM codepage numbers.
378
379 The codepage mappings slighly differ between IBM and other vendors.
380 See \"ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/IBM/README.TXT\".
381
382 If an element is a number corresponding to a supported windows
383 codepage, appropriate entries to `mm-charset-synonym-alist' are
384 added by `mm-setup-codepage-ibm'."
385   :type '(list (set :inline t
386                     (const 437 :tag "US etc.")
387                     (const 860 :tag "Portugal")
388                     (const 861 :tag "Iceland")
389                     (const 862 :tag "Israel")
390                     (const 863 :tag "Canadian French")
391                     (const 865 :tag "Nordic")
392                     (const 852)
393                     (const 850 :tag "Latin 1")
394                     (const 855 :tag "Cyrillic")
395                     (const 866 :tag "Cyrillic - Russian")
396                     (const 857 :tag "Turkish")
397                     (const 864 :tag "Arabic")
398                     (const 869 :tag "Greek")
399                     (const 874 :tag "Thai"))
400                (repeat :inline t
401                        :tag "Other options"
402                        (integer :tag "Codepage number")))
403   :version "22.1" ;; Gnus 5.10.9
404   :group 'mime)
405
406 (defun mm-setup-codepage-iso-8859 (&optional list)
407   "Add appropriate entries to `mm-charset-synonym-alist'.
408 Unless LIST is given, `mm-codepage-iso-8859-list' is used."
409   (unless list
410     (setq list mm-codepage-iso-8859-list))
411   (dolist (i list)
412     (let (cp windows iso)
413       (if (consp i)
414           (setq cp (intern (format "cp%d" (car i)))
415                 windows (intern (format "windows-%d" (car i)))
416                 iso (intern (format "iso-8859-%d" (cdr i))))
417         (setq cp (intern (format "cp%d" i))
418               windows (intern (format "windows-%d" i))))
419       (unless (mm-coding-system-p windows)
420         (if (mm-coding-system-p cp)
421             (add-to-list 'mm-charset-synonym-alist (cons windows cp))
422           (add-to-list 'mm-charset-synonym-alist (cons windows iso)))))))
423
424 (defun mm-setup-codepage-ibm (&optional list)
425   "Add appropriate entries to `mm-charset-synonym-alist'.
426 Unless LIST is given, `mm-codepage-ibm-list' is used."
427   (unless list
428     (setq list mm-codepage-ibm-list))
429   (dolist (number list)
430     (let ((ibm (intern (format "ibm%d" number)))
431           (cp  (intern (format "cp%d" number))))
432       (when (and (not (mm-coding-system-p ibm))
433                  (mm-coding-system-p cp))
434         (add-to-list 'mm-charset-synonym-alist (cons ibm cp))))))
435
436 ;; Initialize:
437 (mm-setup-codepage-iso-8859)
438 (mm-setup-codepage-ibm)
439
440 ;; Note: this has to be defined before `mm-charset-to-coding-system'.
441 (defcustom mm-charset-eval-alist
442   (if (featurep 'xemacs)
443       nil ;; I don't know what would be useful for XEmacs.
444     '(;; Emacs 21 offers 1250 1251 1253 1257.  Emacs 22 provides autoloads for
445       ;; 1250-1258 (i.e. `mm-codepage-setup' does nothing).
446       (windows-1250 . (mm-codepage-setup 1250 t))
447       (windows-1251 . (mm-codepage-setup 1251 t))
448       (windows-1253 . (mm-codepage-setup 1253 t))
449       (windows-1257 . (mm-codepage-setup 1257 t))))
450   "An alist of (CHARSET . FORM) pairs.
451 If an article is encoded in an unknown CHARSET, FORM is
452 evaluated.  This allows to load additional libraries providing
453 charsets on demand.  If supported by your Emacs version, you
454 could use `autoload-coding-system' here."
455   :version "22.1" ;; Gnus 5.10.9
456   :type '(list (set :inline t
457                     (const (windows-1250 . (mm-codepage-setup 1250 t)))
458                     (const (windows-1251 . (mm-codepage-setup 1251 t)))
459                     (const (windows-1253 . (mm-codepage-setup 1253 t)))
460                     (const (windows-1257 . (mm-codepage-setup 1257 t)))
461                     (const (cp850 . (mm-codepage-setup 850 nil))))
462                (repeat :inline t
463                        :tag "Other options"
464                        (cons (symbol :tag "charset")
465                              (symbol :tag "form"))))
466   :group 'mime)
467 (put 'mm-charset-eval-alist 'risky-local-variable t)
468
469 (defvar mm-charset-override-alist)
470
471 ;; Note: this function has to be defined before `mm-charset-override-alist'
472 ;; since it will use this function in order to determine its default value
473 ;; when loading mm-util.elc.
474 (defun mm-charset-to-coding-system (charset &optional lbt
475                                             allow-override silent)
476   "Return coding-system corresponding to CHARSET.
477 CHARSET is a symbol naming a MIME charset.
478 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
479 used as the line break code type of the coding system.
480
481 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
482 map undesired charset names to their replacement.  This should
483 only be used for decoding, not for encoding.
484
485 A non-nil value of SILENT means don't issue a warning even if CHARSET
486 is not available."
487   ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
488   (when (stringp charset)
489     (setq charset (intern (downcase charset))))
490   (when lbt
491     (setq charset (intern (format "%s-%s" charset lbt))))
492   (cond
493    ((null charset)
494     charset)
495    ;; Running in a non-MULE environment.
496    ((or (null (mm-get-coding-system-list))
497         (not (fboundp 'coding-system-get)))
498     charset)
499    ;; Check override list quite early.  Should only used for decoding, not for
500    ;; encoding!
501    ((and allow-override
502          (let ((cs (cdr (assq charset mm-charset-override-alist))))
503            (and cs (mm-coding-system-p cs) cs))))
504    ;; ascii
505    ((eq charset 'us-ascii)
506     'ascii)
507    ;; Check to see whether we can handle this charset.  (This depends
508    ;; on there being some coding system matching each `mime-charset'
509    ;; property defined, as there should be.)
510    ((and (mm-coding-system-p charset)
511 ;;; Doing this would potentially weed out incorrect charsets.
512 ;;;      charset
513 ;;;      (eq charset (coding-system-get charset 'mime-charset))
514          )
515     charset)
516    ;; Eval expressions from `mm-charset-eval-alist'
517    ((let* ((el (assq charset mm-charset-eval-alist))
518            (cs (car el))
519            (form (cdr el)))
520       (and cs
521            form
522            (prog2
523                ;; Avoid errors...
524                (condition-case nil (eval form) (error nil))
525                ;; (message "Failed to eval `%s'" form))
526                (mm-coding-system-p cs)
527              (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
528            cs)))
529    ;; Translate invalid charsets.
530    ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
531       (and cs
532            (mm-coding-system-p cs)
533            ;; (message
534            ;;  "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
535            ;;  cs charset)
536            cs)))
537    ;; Last resort: search the coding system list for entries which
538    ;; have the right mime-charset in case the canonical name isn't
539    ;; defined (though it should be).
540    ((let (cs)
541       ;; mm-get-coding-system-list returns a list of cs without lbt.
542       ;; Do we need -lbt?
543       (dolist (c (mm-get-coding-system-list))
544         (if (and (null cs)
545                  (eq charset (or (coding-system-get c :mime-charset)
546                                  (coding-system-get c 'mime-charset))))
547             (setq cs c)))
548       (unless (or silent cs)
549         ;; Warn the user about unknown charset:
550         (if (fboundp 'gnus-message)
551             (gnus-message 7 "Unknown charset: %s" charset)
552           (message "Unknown charset: %s" charset)))
553       cs))))
554
555 ;; Note: `mm-charset-to-coding-system' has to be defined before this.
556 (defcustom mm-charset-override-alist
557   ;; Note: pairs that cannot be used in the Emacs version currently running
558   ;; will be removed.
559   '((gb2312 . gbk)
560     (iso-8859-1 . windows-1252)
561     (iso-8859-8 . windows-1255)
562     (iso-8859-9 . windows-1254))
563   "A mapping from undesired charset names to their replacement.
564
565 You may add pairs like (iso-8859-1 . windows-1252) here,
566 i.e. treat iso-8859-1 as windows-1252.  windows-1252 is a
567 superset of iso-8859-1."
568   :type
569   '(list
570     :convert-widget
571     (lambda (widget)
572       (let ((defaults
573               (delq nil
574                     (mapcar (lambda (pair)
575                               (if (mm-charset-to-coding-system (cdr pair)
576                                                                nil nil t)
577                                   pair))
578                             '((gb2312 . gbk)
579                               (iso-8859-1 . windows-1252)
580                               (iso-8859-8 . windows-1255)
581                               (iso-8859-9 . windows-1254)
582                               (undecided  . windows-1252)))))
583             (val (copy-sequence (default-value 'mm-charset-override-alist)))
584             pair rest)
585         (while val
586           (push (if (and (prog1
587                              (setq pair (assq (caar val) defaults))
588                            (setq defaults (delq pair defaults)))
589                          (equal (car val) pair))
590                     `(const ,pair)
591                   `(cons :format "%v"
592                          (const :format "(%v" ,(caar val))
593                          (symbol :size 3 :format " . %v)\n" ,(cdar val))))
594                 rest)
595           (setq val (cdr val)))
596         (while defaults
597           (push `(const ,(pop defaults)) rest))
598         (widget-convert
599          'list
600          `(set :inline t :format "%v" ,@(nreverse rest))
601          `(repeat :inline t :tag "Other options"
602                   (cons :format "%v"
603                         (symbol :size 3 :format "(%v")
604                         (symbol :size 3 :format " . %v)\n")))))))
605   ;; Remove pairs that cannot be used in the Emacs version currently
606   ;; running.  Note that this section will be evaluated when loading
607   ;; mm-util.elc.
608   :set (lambda (symbol value)
609          (custom-set-default
610           symbol (delq nil
611                        (mapcar (lambda (pair)
612                                  (if (mm-charset-to-coding-system (cdr pair)
613                                                                   nil nil t)
614                                      pair))
615                                value))))
616   :version "22.1" ;; Gnus 5.10.9
617   :group 'mime)
618
619 (defvar mm-binary-coding-system
620   (cond
621    ((mm-coding-system-p 'binary) 'binary)
622    ((mm-coding-system-p 'no-conversion) 'no-conversion)
623    (t nil))
624   "100% binary coding system.")
625
626 (defvar mm-text-coding-system
627   (or (if (memq system-type '(windows-nt ms-dos ms-windows))
628           (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
629         (and (mm-coding-system-p 'raw-text) 'raw-text))
630       mm-binary-coding-system)
631   "Text-safe coding system (For removing ^M).")
632
633 (defvar mm-text-coding-system-for-write nil
634   "Text coding system for write.")
635
636 (defvar mm-auto-save-coding-system
637   (cond
638    ((mm-coding-system-p 'utf-8-emacs)   ; Mule 7
639     (if (memq system-type '(windows-nt ms-dos ms-windows))
640         (if (mm-coding-system-p 'utf-8-emacs-dos)
641             'utf-8-emacs-dos mm-binary-coding-system)
642       'utf-8-emacs))
643    ((mm-coding-system-p 'emacs-mule)
644     (if (memq system-type '(windows-nt ms-dos ms-windows))
645         (if (mm-coding-system-p 'emacs-mule-dos)
646             'emacs-mule-dos mm-binary-coding-system)
647       'emacs-mule))
648    ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
649    (t mm-binary-coding-system))
650   "Coding system of auto save file.")
651
652 (defvar mm-universal-coding-system mm-auto-save-coding-system
653   "The universal coding system.")
654
655 ;; Fixme: some of the cars here aren't valid MIME charsets.  That
656 ;; should only matter with XEmacs, though.
657 (defvar mm-mime-mule-charset-alist
658   `((us-ascii ascii)
659     (iso-8859-1 latin-iso8859-1)
660     (iso-8859-2 latin-iso8859-2)
661     (iso-8859-3 latin-iso8859-3)
662     (iso-8859-4 latin-iso8859-4)
663     (iso-8859-5 cyrillic-iso8859-5)
664     ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
665     ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default