ChangeLog.2 (2003-03-20): Fix typo.
[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              &nb