* shr.el (shr-generic): The text nodes should be text, not :text.
[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, 2005, 2006,
4 ;;   2007, 2008, 2009, 2010  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 and XEmacs.
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             (gnus-completing-read
72              prompt
73              (mapcar (lambda (e) (symbol-name (car e)))
74                      mm-mime-mule-charset-alist)
75              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      ;; `line-number-at-pos' is available only in Emacs 22.1 or greater
159      ;; and XEmacs 21.5.
160      (line-number-at-pos
161       . ,(lambda (&optional pos)
162            "Return (narrowed) buffer line number at position POS.
163 If POS is nil, use current buffer location.
164 Counting starts at (point-min), so the value refers
165 to the contents of the accessible portion of the buffer."
166            (let ((opoint (or pos (point))) start)
167              (save-excursion
168                (goto-char (point-min))
169                (setq start (point))
170                (goto-char opoint)
171                (forward-line 0)
172                (1+ (count-lines start (point))))))))))
173
174 ;; `decode-coding-string', `encode-coding-string', `decode-coding-region'
175 ;; and `encode-coding-region' are available in Emacs and XEmacs built with
176 ;; the `file-coding' feature, but the XEmacs versions treat nil, that is
177 ;; given as the `coding-system' argument, as the `binary' coding system.
178 (eval-and-compile
179   (if (featurep 'xemacs)
180       (if (featurep 'file-coding)
181           (progn
182             (defun mm-decode-coding-string (str coding-system)
183               (if coding-system
184                   (decode-coding-string str coding-system)
185                 str))
186             (defun mm-encode-coding-string (str coding-system)
187               (if coding-system
188                   (encode-coding-string str coding-system)
189                 str))
190             (defun mm-decode-coding-region (start end coding-system)
191               (if coding-system
192                   (decode-coding-region start end coding-system)))
193             (defun mm-encode-coding-region (start end coding-system)
194               (if coding-system
195                   (encode-coding-region start end coding-system))))
196         (defun mm-decode-coding-string (str coding-system) str)
197         (defun mm-encode-coding-string (str coding-system) str)
198         (defalias 'mm-decode-coding-region 'ignore)
199         (defalias 'mm-encode-coding-region 'ignore))
200     (defalias 'mm-decode-coding-string 'decode-coding-string)
201     (defalias 'mm-encode-coding-string 'encode-coding-string)
202     (defalias 'mm-decode-coding-region 'decode-coding-region)
203     (defalias 'mm-encode-coding-region 'encode-coding-region)))
204
205 ;; `string-to-multibyte' is available only in Emacs 22.1 or greater.
206 (defalias 'mm-string-to-multibyte
207   (cond
208    ((featurep 'xemacs)
209     'identity)
210    ((fboundp 'string-to-multibyte)
211     'string-to-multibyte)
212    (t
213     (lambda (string)
214       "Return a multibyte string with the same individual chars as STRING."
215       (mapconcat
216        (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
217        string "")))))
218
219 ;; `char-or-char-int-p' is an XEmacs function, not available in Emacs.
220 (eval-and-compile
221   (defalias 'mm-char-or-char-int-p
222     (cond
223      ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
224      ((fboundp 'char-valid-p) 'char-valid-p)
225      (t 'identity))))
226
227 ;; `ucs-to-char' is a function that Mule-UCS provides.
228 (if (featurep 'xemacs)
229     (cond ((and (fboundp 'unicode-to-char) ;; XEmacs 21.5.
230                 (subrp (symbol-function 'unicode-to-char)))
231            (if (featurep 'mule)
232                (defalias 'mm-ucs-to-char 'unicode-to-char)
233              (defun mm-ucs-to-char (codepoint)
234                "Convert Unicode codepoint to character."
235                (or (unicode-to-char codepoint) ?#))))
236           ((featurep 'mule)
237            (defun mm-ucs-to-char (codepoint)
238              "Convert Unicode codepoint to character."
239              (if (fboundp 'ucs-to-char) ;; Mule-UCS is loaded.
240                  (progn
241                    (defalias 'mm-ucs-to-char
242                      (lambda (codepoint)
243                        "Convert Unicode codepoint to character."
244                        (condition-case nil
245                            (or (ucs-to-char codepoint) ?#)
246                          (error ?#))))
247                    (mm-ucs-to-char codepoint))
248                (condition-case nil
249                    (or (int-to-char codepoint) ?#)
250                  (error ?#)))))
251           (t
252            (defun mm-ucs-to-char (codepoint)
253              "Convert Unicode codepoint to character."
254              (condition-case nil
255                  (or (int-to-char codepoint) ?#)
256                (error ?#)))))
257   (if (let ((char (make-char 'japanese-jisx0208 36 34)))
258         (eq char (decode-char 'ucs char)))
259       ;; Emacs 23.
260       (defalias 'mm-ucs-to-char 'identity)
261     (defun mm-ucs-to-char (codepoint)
262       "Convert Unicode codepoint to character."
263       (or (decode-char 'ucs codepoint) ?#))))
264
265 ;; Fixme:  This seems always to be used to read a MIME charset, so it
266 ;; should be re-named and fixed (in Emacs) to offer completion only on
267 ;; proper charset names (base coding systems which have a
268 ;; mime-charset defined).  XEmacs doesn't believe in mime-charset;
269 ;; test with
270 ;;   `(or (coding-system-get 'iso-8859-1 'mime-charset)
271 ;;        (coding-system-get 'iso-8859-1 :mime-charset))'
272 ;; Actually, there should be an `mm-coding-system-mime-charset'.
273 (eval-and-compile
274   (defalias 'mm-read-coding-system
275     (cond
276      ((fboundp 'read-coding-system)
277       (if (and (featurep 'xemacs)
278                (<= (string-to-number emacs-version) 21.1))
279           (lambda (prompt &optional default-coding-system)
280             (read-coding-system prompt))
281         'read-coding-system))
282      (t (lambda (prompt &optional default-coding-system)
283           "Prompt the user for a coding system."
284           (gnus-completing-read
285            prompt (mapcar (lambda (s) (symbol-name (car s)))
286                           mm-mime-mule-charset-alist)))))))
287
288 (defvar mm-coding-system-list nil)
289 (defun mm-get-coding-system-list ()
290   "Get the coding system list."
291   (or mm-coding-system-list
292       (setq mm-coding-system-list (mm-coding-system-list))))
293
294 (defun mm-coding-system-p (cs)
295   "Return non-nil if CS is a symbol naming a coding system.
296 In XEmacs, also return non-nil if CS is a coding system object.
297 If CS is available, return CS itself in Emacs, and return a coding
298 system object in XEmacs."
299   (if (fboundp 'find-coding-system)
300       (and cs (find-coding-system cs))
301     (if (fboundp 'coding-system-p)
302         (when (coding-system-p cs)
303           cs)
304       ;; no-MULE XEmacs:
305       (car (memq cs (mm-get-coding-system-list))))))
306
307 (defun mm-codepage-setup (number &optional alias)
308   "Create a coding system cpNUMBER.
309 The coding system is created using `codepage-setup'.  If ALIAS is
310 non-nil, an alias is created and added to
311 `mm-charset-synonym-alist'.  If ALIAS is a string, it's used as
312 the alias.  Else windows-NUMBER is used."
313   (interactive
314    (let ((completion-ignore-case t)
315          (candidates (if (fboundp 'cp-supported-codepages)
316                          (cp-supported-codepages)
317                        ;; Removed in Emacs 23 (unicode), so signal an error:
318                        (error "`codepage-setup' not present in this Emacs version"))))
319      (list (gnus-completing-read "Setup DOS Codepage" candidates
320                                  t nil nil "437"))))
321   (when alias
322     (setq alias (if (stringp alias)
323                     (intern alias)
324                   (intern (format "windows-%s" number)))))
325   (let* ((cp (intern (format "cp%s" number))))
326     (unless (mm-coding-system-p cp)
327       (if (fboundp 'codepage-setup)     ; silence compiler
328           (codepage-setup number)
329         (error "`codepage-setup' not present in this Emacs version")))
330     (when (and alias
331                ;; Don't add alias if setup of cp failed.
332                (mm-coding-system-p cp))
333       (add-to-list 'mm-charset-synonym-alist (cons alias cp)))))
334
335 (defvar mm-charset-synonym-alist
336   `(
337     ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
338     ,@(unless (mm-coding-system-p 'x-ctext)
339         '((x-ctext . ctext)))
340     ;; ISO-8859-15 is very similar to ISO-8859-1.  But it's _different_ in 8
341     ;; positions!
342     ,@(unless (mm-coding-system-p 'iso-8859-15)
343         '((iso-8859-15 . iso-8859-1)))
344     ;; BIG-5HKSCS is similar to, but different than, BIG-5.
345     ,@(unless (mm-coding-system-p 'big5-hkscs)
346         '((big5-hkscs . big5)))
347     ;; A Microsoft misunderstanding.
348     ,@(when (and (not (mm-coding-system-p 'unicode))
349                  (mm-coding-system-p 'utf-16-le))
350         '((unicode . utf-16-le)))
351     ;; A Microsoft misunderstanding.
352     ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
353         (if (mm-coding-system-p 'cp949)
354             '((ks_c_5601-1987 . cp949))
355           '((ks_c_5601-1987 . euc-kr))))
356     ;; Windows-31J is Windows Codepage 932.
357     ,@(when (and (not (mm-coding-system-p 'windows-31j))
358                  (mm-coding-system-p 'cp932))
359         '((windows-31j . cp932)))
360     ;; Charset name: GBK, Charset aliases: CP936, MS936, windows-936
361     ;; http://www.iana.org/assignments/charset-reg/GBK
362     ;; Emacs 22.1 has cp936, but not gbk, so we alias it:
363     ,@(when (and (not (mm-coding-system-p 'gbk))
364                  (mm-coding-system-p 'cp936))
365         '((gbk . cp936)))
366     ;; UTF8 is a bogus name for UTF-8
367     ,@(when (and (not (mm-coding-system-p 'utf8))
368                  (mm-coding-system-p 'utf-8))
369         '((utf8 . utf-8)))
370     ;; ISO8859-1 is a bogus name for ISO-8859-1
371     ,@(when (and (not (mm-coding-system-p 'iso8859-1))
372                  (mm-coding-system-p 'iso-8859-1))
373         '((iso8859-1 . iso-8859-1)))
374     ;; ISO_8859-1 is a bogus name for ISO-8859-1
375     ,@(when (and (not (mm-coding-system-p 'iso_8859-1))
376                  (mm-coding-system-p 'iso-8859-1))
377         '((iso_8859-1 . iso-8859-1)))
378     )
379   "A mapping from unknown or invalid charset names to the real charset names.
380
381 See `mm-codepage-iso-8859-list' and `mm-codepage-ibm-list'.")
382
383 (defcustom mm-codepage-iso-8859-list
384   (list 1250 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
385         ;; Outlook users in Czech republic.  Use this to allow reading of
386         ;; their e-mails.
387         '(1252 . 1) ;; Windows-1252 is a superset of iso-8859-1 (West
388                     ;; Europe).  See also `gnus-article-dumbquotes-map'.
389         '(1254 . 9) ;; Windows-1254 is a superset of iso-8859-9 (Turkish).
390         '(1255 . 8));; Windows-1255 is a superset of iso-8859-8 (Hebrew).
391   "A list of Windows codepage numbers and iso-8859 charset numbers.
392
393 If an element is a number corresponding to a supported windows
394 codepage, appropriate entries to `mm-charset-synonym-alist' are
395 added by `mm-setup-codepage-iso-8859'.  An element may also be a
396 cons cell where the car is a codepage number and the cdr is the
397 corresponding number of an iso-8859 charset."
398   :type '(list (set :inline t
399                     (const 1250 :tag "Central and East European")
400                     (const (1252 . 1) :tag "West European")
401                     (const (1254 . 9) :tag "Turkish")
402                     (const (1255 . 8) :tag "Hebrew"))
403                (repeat :inline t
404                        :tag "Other options"
405                        (choice
406                         (integer :tag "Windows codepage number")
407                         (cons (integer :tag "Windows codepage number")
408                               (integer :tag "iso-8859 charset  number")))))
409   :version "22.1" ;; Gnus 5.10.9
410   :group 'mime)
411
412 (defcustom mm-codepage-ibm-list
413   (list 437 ;; (US etc.)
414         860 ;; (Portugal)
415         861 ;; (Iceland)
416         862 ;; (Israel)
417         863 ;; (Canadian French)
418         865 ;; (Nordic)
419         852 ;;
420         850 ;; (Latin 1)
421         855 ;; (Cyrillic)
422         866 ;; (Cyrillic - Russian)
423         857 ;; (Turkish)
424         864 ;; (Arabic)
425         869 ;; (Greek)
426         874);; (Thai)
427   ;; In Emacs 23 (unicode), cp... and ibm... are aliases.
428   ;; Cf. http://thread.gmane.org/v9lkng5nwy.fsf@marauder.physik.uni-ulm.de
429   "List of IBM codepage numbers.
430
431 The codepage mappings slighly differ between IBM and other vendors.
432 See \"ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/IBM/README.TXT\".
433
434 If an element is a number corresponding to a supported windows
435 codepage, appropriate entries to `mm-charset-synonym-alist' are
436 added by `mm-setup-codepage-ibm'."
437   :type '(list (set :inline t
438                     (const 437 :tag "US etc.")
439                     (const 860 :tag "Portugal")
440                     (const 861 :tag "Iceland")
441                     (const 862 :tag "Israel")
442                     (const 863 :tag "Canadian French")
443                     (const 865 :tag "Nordic")
444                     (const 852)
445                     (const 850 :tag "Latin 1")
446                     (const 855 :tag "Cyrillic")
447                     (const 866 :tag "Cyrillic - Russian")
448                     (const 857 :tag "Turkish")
449                     (const 864 :tag "Arabic")
450                     (const 869 :tag "Greek")
451                     (const 874 :tag "Thai"))
452                (repeat :inline t
453                        :tag "Other options"
454                        (integer :tag "Codepage number")))
455   :version "22.1" ;; Gnus 5.10.9
456   :group 'mime)
457
458 (defun mm-setup-codepage-iso-8859 (&optional list)
459   "Add appropriate entries to `mm-charset-synonym-alist'.
460 Unless LIST is given, `mm-codepage-iso-8859-list' is used."
461   (unless list
462     (setq list mm-codepage-iso-8859-list))
463   (dolist (i list)
464     (let (cp windows iso)
465       (if (consp i)
466           (setq cp (intern (format "cp%d" (car i)))
467                 windows (intern (format "windows-%d" (car i)))
468                 iso (intern (format "iso-8859-%d" (cdr i))))
469         (setq cp (intern (format "cp%d" i))
470               windows (intern (format "windows-%d" i))))
471       (unless (mm-coding-system-p windows)
472         (if (mm-coding-system-p cp)
473             (add-to-list 'mm-charset-synonym-alist (cons windows cp))
474           (add-to-list 'mm-charset-synonym-alist (cons windows iso)))))))
475
476 (defun mm-setup-codepage-ibm (&optional list)
477   "Add appropriate entries to `mm-charset-synonym-alist'.
478 Unless LIST is given, `mm-codepage-ibm-list' is used."
479   (unless list
480     (setq list mm-codepage-ibm-list))
481   (dolist (number list)
482     (let ((ibm (intern (format "ibm%d" number)))
483           (cp  (intern (format "cp%d" number))))
484       (when (and (not (mm-coding-system-p ibm))
485                  (mm-coding-system-p cp))
486         (add-to-list 'mm-charset-synonym-alist (cons ibm cp))))))
487
488 ;; Initialize:
489 (mm-setup-codepage-iso-8859)
490 (mm-setup-codepage-ibm)
491
492 ;; Note: this has to be defined before `mm-charset-to-coding-system'.
493 (defcustom mm-charset-eval-alist
494   (if (featurep 'xemacs)
495       nil ;; I don't know what would be useful for XEmacs.