(no commit message)
[bpt/emacs.git] / lisp / gnus / mm-util.el
CommitLineData
95fa1ff7 1;;; mm-util.el --- Utility functions for Mule and low level things
e84b4b86 2
ba318903 3;; Copyright (C) 1998-2014 Free Software Foundation, Inc.
c113de23
GM
4
5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7;; This file is part of GNU Emacs.
8
5e809f55 9;; GNU Emacs is free software: you can redistribute it and/or modify
c113de23 10;; it under the terms of the GNU General Public License as published by
5e809f55
GM
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
c113de23
GM
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
5e809f55 16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c113de23
GM
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
5e809f55 20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c113de23
GM
21
22;;; Commentary:
23
24;;; Code:
25
23f87bed 26(eval-when-compile (require 'cl))
c113de23
GM
27(require 'mail-prsvr)
28
f53b2875 29(eval-and-compile
01c52d31
MB
30 (if (featurep 'xemacs)
31 (unless (ignore-errors
32 (require 'timer-funcs))
33 (require 'timer))
34 (require 'timer)))
35
9efa445f 36(defvar mm-mime-mule-charset-alist )
4c188c5a
GM
37;; Note this is not presently used on Emacs >= 23, which is good,
38;; since it means standalone message-mode (which requires mml and
39;; hence mml-util) does not load gnus-util.
40(autoload 'gnus-completing-read "gnus-util")
9efa445f 41
e3e955fe
MB
42;; Emulate functions that are not available in every (X)Emacs version.
43;; The name of a function is prefixed with mm-, like `mm-char-int' for
44;; `char-int' that is a native XEmacs function, not available in Emacs.
45;; Gnus programs all should use mm- functions, not the original ones.
01c52d31
MB
46(eval-and-compile
47 (mapc
f53b2875
DL
48 (lambda (elem)
49 (let ((nfunc (intern (format "mm-%s" (car elem)))))
50 (if (fboundp (car elem))
51 (defalias nfunc (car elem))
52 (defalias nfunc (cdr elem)))))
e3e955fe
MB
53 `(;; `coding-system-list' is not available in XEmacs 21.4 built
54 ;; without the `file-coding' feature.
55 (coding-system-list . ignore)
56 ;; `char-int' is an XEmacs function, not available in Emacs.
f53b2875 57 (char-int . identity)
e3e955fe 58 ;; `coding-system-equal' is an Emacs function, not available in XEmacs.
f53b2875 59 (coding-system-equal . equal)
e3e955fe 60 ;; `annotationp' is an XEmacs function, not available in Emacs.
f53b2875 61 (annotationp . ignore)
e3e955fe
MB
62 ;; `set-buffer-file-coding-system' is not available in XEmacs 21.4
63 ;; built without the `file-coding' feature.
f53b2875 64 (set-buffer-file-coding-system . ignore)
e3e955fe 65 ;; `read-charset' is an Emacs function, not available in XEmacs.
f53b2875 66 (read-charset
c7948b5f
MB
67 . ,(lambda (prompt)
68 "Return a charset."
69 (intern
229b59da 70 (gnus-completing-read
c7948b5f 71 prompt
229b59da 72 (mapcar (lambda (e) (symbol-name (car e)))
c7948b5f 73 mm-mime-mule-charset-alist)
229b59da 74 t))))
e3e955fe 75 ;; `subst-char-in-string' is not available in XEmacs 21.4.
95fa1ff7 76 (subst-char-in-string
c7948b5f
MB
77 . ,(lambda (from to string &optional inplace)
78 ;; stolen (and renamed) from nnheader.el
79 "Replace characters in STRING from FROM to TO.
91472578 80 Unless optional argument INPLACE is non-nil, return a new string."
c7948b5f
MB
81 (let ((string (if inplace string (copy-sequence string)))
82 (len (length string))
83 (idx 0))
84 ;; Replace all occurrences of FROM with TO.
85 (while (< idx len)
86 (when (= (aref string idx) from)
87 (aset string idx to))
88 (setq idx (1+ idx)))
89 string)))
e3e955fe 90 ;; `replace-in-string' is an XEmacs function, not available in Emacs.
01c52d31 91 (replace-in-string
c7948b5f
MB
92 . ,(lambda (string regexp rep &optional literal)
93 "See `replace-regexp-in-string', only the order of args differs."
94 (replace-regexp-in-string regexp rep string nil literal)))
e3e955fe 95 ;; `string-as-unibyte' is an Emacs function, not available in XEmacs.
f53b2875 96 (string-as-unibyte . identity)
e3e955fe 97 ;; `string-make-unibyte' is an Emacs function, not available in XEmacs.
23f87bed 98 (string-make-unibyte . identity)
9d9b0de9
SM
99 ;; string-as-multibyte often doesn't really do what you think it does.
100 ;; Example:
101 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
102 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
103 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
104 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
105 ;; but
106 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
107 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
108 ;; Better use string-to-multibyte or encode-coding-string.
109 ;; If you really need string-as-multibyte somewhere it's usually
110 ;; because you're using the internal emacs-mule representation (maybe
111 ;; because you're using string-as-unibyte somewhere), which is
112 ;; generally a problem in itself.
113 ;; Here is an approximate equivalence table to help think about it:
114 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
115 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
116 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
e3e955fe 117 ;; `string-as-multibyte' is an Emacs function, not available in XEmacs.
95fa1ff7 118 (string-as-multibyte . identity)
e3e955fe 119 ;; `multibyte-string-p' is an Emacs function, not available in XEmacs.
56e09c09 120 (multibyte-string-p . ignore)
e3e955fe 121 ;; `insert-byte' is available only in Emacs 23.1 or greater.
56e09c09 122 (insert-byte . insert-char)
e3e955fe
MB
123 ;; `multibyte-char-to-unibyte' is an Emacs function, not available
124 ;; in XEmacs.
01c52d31 125 (multibyte-char-to-unibyte . identity)
e3e955fe 126 ;; `set-buffer-multibyte' is an Emacs function, not available in XEmacs.
df06dd59 127 (set-buffer-multibyte . ignore)
e3e955fe 128 ;; `substring-no-properties' is available only in Emacs 22.1 or greater.
c7948b5f
MB
129 (substring-no-properties
130 . ,(lambda (string &optional from to)
131 "Return a substring of STRING, without text properties.
132It starts at index FROM and ending before TO.
133TO may be nil or omitted; then the substring runs to the end of STRING.
134If FROM is nil or omitted, the substring starts at the beginning of STRING.
135If FROM or TO is negative, it counts from the end.
136
137With one argument, just copy STRING without its properties."
138 (setq string (substring string (or from 0) to))
139 (set-text-properties 0 (length string) nil string)
e3e955fe
MB
140 string))
141 ;; `line-number-at-pos' is available only in Emacs 22.1 or greater
142 ;; and XEmacs 21.5.
143 (line-number-at-pos
144 . ,(lambda (&optional pos)
145 "Return (narrowed) buffer line number at position POS.
146If POS is nil, use current buffer location.
147Counting starts at (point-min), so the value refers
148to the contents of the accessible portion of the buffer."
149 (let ((opoint (or pos (point))) start)
150 (save-excursion
151 (goto-char (point-min))
152 (setq start (point))
153 (goto-char opoint)
154 (forward-line 0)
155 (1+ (count-lines start (point))))))))))
f53b2875 156
9ab16aab
KY
157;; `special-display-p' is an Emacs function, not available in XEmacs.
158(defalias 'mm-special-display-p
159 (if (featurep 'emacs)
160 'special-display-p
161 (lambda (buffer-name)
162 "Returns non-nil if a buffer named BUFFER-NAME gets a special frame."
163 (and special-display-function
164 (or (and (member buffer-name special-display-buffer-names) t)
165 (cdr (assoc buffer-name special-display-buffer-names))
166 (catch 'return
167 (dolist (elem special-display-regexps)
168 (and (stringp elem)
169 (string-match elem buffer-name)
170 (throw 'return t))
171 (and (consp elem)
172 (stringp (car elem))
173 (string-match (car elem) buffer-name)
174 (throw 'return (cdr elem))))))))))
175
e3e955fe
MB
176;; `decode-coding-string', `encode-coding-string', `decode-coding-region'
177;; and `encode-coding-region' are available in Emacs and XEmacs built with
178;; the `file-coding' feature, but the XEmacs versions treat nil, that is
179;; given as the `coding-system' argument, as the `binary' coding system.
82fe1aed
MB
180(eval-and-compile
181 (if (featurep 'xemacs)
182 (if (featurep 'file-coding)
82fe1aed
MB
183 (progn
184 (defun mm-decode-coding-string (str coding-system)
185 (if coding-system
186 (decode-coding-string str coding-system)
187 str))
188 (defun mm-encode-coding-string (str coding-system)
189 (if coding-system
190 (encode-coding-string str coding-system)
191 str))
192 (defun mm-decode-coding-region (start end coding-system)
193 (if coding-system
194 (decode-coding-region start end coding-system)))
195 (defun mm-encode-coding-region (start end coding-system)
196 (if coding-system
197 (encode-coding-region start end coding-system))))
198 (defun mm-decode-coding-string (str coding-system) str)
199 (defun mm-encode-coding-string (str coding-system) str)
200 (defalias 'mm-decode-coding-region 'ignore)
201 (defalias 'mm-encode-coding-region 'ignore))
202 (defalias 'mm-decode-coding-string 'decode-coding-string)
203 (defalias 'mm-encode-coding-string 'encode-coding-string)
204 (defalias 'mm-decode-coding-region 'decode-coding-region)
205 (defalias 'mm-encode-coding-region 'encode-coding-region)))
206
ed1d182d
GM
207;; `string-to-multibyte' is available only in Emacs.
208(defalias 'mm-string-to-multibyte (if (featurep 'xemacs)
209 'identity
210 'string-to-multibyte))
e8f0f70d 211
e3e955fe 212;; `char-or-char-int-p' is an XEmacs function, not available in Emacs.
c113de23
GM
213(eval-and-compile
214 (defalias 'mm-char-or-char-int-p
95fa1ff7 215 (cond
c113de23 216 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
95fa1ff7 217 ((fboundp 'char-valid-p) 'char-valid-p)
c113de23
GM
218 (t 'identity))))
219
e3e955fe 220;; `ucs-to-char' is a function that Mule-UCS provides.
99139556
KY
221(eval-and-compile
222 (if (featurep 'xemacs)
223 (cond ((and (fboundp 'unicode-to-char) ;; XEmacs 21.5.
224 (subrp (symbol-function 'unicode-to-char)))
225 (if (featurep 'mule)
226 (defalias 'mm-ucs-to-char 'unicode-to-char)
227 (defun mm-ucs-to-char (codepoint)
228 "Convert Unicode codepoint to character."
229 (or (unicode-to-char codepoint) ?#))))
230 ((featurep 'mule)
231 (defun mm-ucs-to-char (codepoint)
232 "Convert Unicode codepoint to character."
233 (if (fboundp 'ucs-to-char) ;; Mule-UCS is loaded.
234 (progn
235 (defalias 'mm-ucs-to-char
236 (lambda (codepoint)
237 "Convert Unicode codepoint to character."
238 (condition-case nil
239 (or (ucs-to-char codepoint) ?#)
240 (error ?#))))
241 (mm-ucs-to-char codepoint))
242 (condition-case nil
243 (or (int-to-char codepoint) ?#)
244 (error ?#)))))
245 (t
e3e955fe
MB
246 (defun mm-ucs-to-char (codepoint)
247 "Convert Unicode codepoint to character."
e3e955fe
MB
248 (condition-case nil
249 (or (int-to-char codepoint) ?#)
250 (error ?#)))))
99139556
KY
251 (if (let ((char (make-char 'japanese-jisx0208 36 34)))
252 (eq char (decode-char 'ucs char)))
253 ;; Emacs 23.
254 (defalias 'mm-ucs-to-char 'identity)
255 (defun mm-ucs-to-char (codepoint)
256 "Convert Unicode codepoint to character."
257 (or (decode-char 'ucs codepoint) ?#)))))
e3e955fe 258
23f87bed
MB
259;; Fixme: This seems always to be used to read a MIME charset, so it
260;; should be re-named and fixed (in Emacs) to offer completion only on
261;; proper charset names (base coding systems which have a
262;; mime-charset defined). XEmacs doesn't believe in mime-charset;
263;; test with
264;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
265;; (coding-system-get 'iso-8859-1 :mime-charset))'
266;; Actually, there should be an `mm-coding-system-mime-charset'.
95fa1ff7
SZ
267(eval-and-compile
268 (defalias 'mm-read-coding-system
4c188c5a
GM
269 (if (featurep 'emacs) 'read-coding-system
270 (cond
271 ((fboundp 'read-coding-system)
272 (if (and (featurep 'xemacs)
273 (<= (string-to-number emacs-version) 21.1))
274 (lambda (prompt &optional default-coding-system)
275 (read-coding-system prompt))
276 'read-coding-system))
277 (t (lambda (prompt &optional default-coding-system)
278 "Prompt the user for a coding system."
279 (gnus-completing-read
280 prompt (mapcar (lambda (s) (symbol-name (car s)))
281 mm-mime-mule-charset-alist))))))))
95fa1ff7 282
c113de23
GM
283(defvar mm-coding-system-list nil)
284(defun mm-get-coding-system-list ()
285 "Get the coding system list."
286 (or mm-coding-system-list
287 (setq mm-coding-system-list (mm-coding-system-list))))
288
23f87bed
MB
289(defun mm-coding-system-p (cs)
290 "Return non-nil if CS is a symbol naming a coding system.
0683d241
MB
291In XEmacs, also return non-nil if CS is a coding system object.
292If CS is available, return CS itself in Emacs, and return a coding
293system object in XEmacs."
23f87bed 294 (if (fboundp 'find-coding-system)
91472578 295 (and cs (find-coding-system cs))
23f87bed 296 (if (fboundp 'coding-system-p)
0683d241
MB
297 (when (coding-system-p cs)
298 cs)
5f4264e5 299 ;; no-MULE XEmacs:
0683d241 300 (car (memq cs (mm-get-coding-system-list))))))
95fa1ff7 301
c113de23 302(defvar mm-charset-synonym-alist
95fa1ff7 303 `(
95fa1ff7 304 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
72eb5fc7 305 ,@(unless (mm-coding-system-p 'x-ctext)
b44409c9 306 '((x-ctext . ctext)))
ab785936
MB
307 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_ in 8
308 ;; positions!
23f87bed 309 ,@(unless (mm-coding-system-p 'iso-8859-15)
b44409c9 310 '((iso-8859-15 . iso-8859-1)))
23f87bed
MB
311 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
312 ,@(unless (mm-coding-system-p 'big5-hkscs)
313 '((big5-hkscs . big5)))
bd29ba20 314 ;; A Microsoft misunderstanding.
ab785936
MB
315 ,@(when (and (not (mm-coding-system-p 'unicode))
316 (mm-coding-system-p 'utf-16-le))
317 '((unicode . utf-16-le)))
bd29ba20
RS
318 ;; A Microsoft misunderstanding.
319 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
320 (if (mm-coding-system-p 'cp949)
321 '((ks_c_5601-1987 . cp949))
322 '((ks_c_5601-1987 . euc-kr))))
b44409c9 323 ;; Windows-31J is Windows Codepage 932.
ab785936
MB
324 ,@(when (and (not (mm-coding-system-p 'windows-31j))
325 (mm-coding-system-p 'cp932))
326 '((windows-31j . cp932)))
4b70e299
MB
327 ;; Charset name: GBK, Charset aliases: CP936, MS936, windows-936
328 ;; http://www.iana.org/assignments/charset-reg/GBK
329 ;; Emacs 22.1 has cp936, but not gbk, so we alias it:
330 ,@(when (and (not (mm-coding-system-p 'gbk))
331 (mm-coding-system-p 'cp936))
332 '((gbk . cp936)))
bf46b4d4
MB
333 ;; UTF8 is a bogus name for UTF-8
334 ,@(when (and (not (mm-coding-system-p 'utf8))
335 (mm-coding-system-p 'utf-8))
336 '((utf8 . utf-8)))
01c52d31
MB
337 ;; ISO8859-1 is a bogus name for ISO-8859-1
338 ,@(when (and (not (mm-coding-system-p 'iso8859-1))
339 (mm-coding-system-p 'iso-8859-1))
340 '((iso8859-1 . iso-8859-1)))
bf46b4d4
MB
341 ;; ISO_8859-1 is a bogus name for ISO-8859-1
342 ,@(when (and (not (mm-coding-system-p 'iso_8859-1))
343 (mm-coding-system-p 'iso-8859-1))
344 '((iso_8859-1 . iso-8859-1)))
95fa1ff7 345 )
ab785936
MB
346 "A mapping from unknown or invalid charset names to the real charset names.
347
348See `mm-codepage-iso-8859-list' and `mm-codepage-ibm-list'.")
349
ddf6fd30
GM
350(defun mm-codepage-setup (number &optional alias)
351 "Create a coding system cpNUMBER.
352The coding system is created using `codepage-setup'. If ALIAS is
353non-nil, an alias is created and added to
354`mm-charset-synonym-alist'. If ALIAS is a string, it's used as
355the alias. Else windows-NUMBER is used."
356 (interactive
357 (let ((completion-ignore-case t)
358 (candidates (if (fboundp 'cp-supported-codepages)
359 (cp-supported-codepages)
360 ;; Removed in Emacs 23 (unicode), so signal an error:
361 (error "`codepage-setup' not present in this Emacs version"))))
362 (list (gnus-completing-read "Setup DOS Codepage" candidates
363 t nil nil "437"))))
364 (when alias
365 (setq alias (if (stringp alias)
366 (intern alias)
367 (intern (format "windows-%s" number)))))
368 (let* ((cp (intern (format "cp%s" number))))
369 (unless (mm-coding-system-p cp)
370 (if (fboundp 'codepage-setup) ; silence compiler
371 (codepage-setup number)
372 (error "`codepage-setup' not present in this Emacs version")))
373 (when (and alias
374 ;; Don't add alias if setup of cp failed.
375 (mm-coding-system-p cp))
376 (add-to-list 'mm-charset-synonym-alist (cons alias cp)))))
377
ab785936
MB
378(defcustom mm-codepage-iso-8859-list
379 (list 1250 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
380 ;; Outlook users in Czech republic. Use this to allow reading of
0c43b6f8 381 ;; their e-mails.
ab785936
MB
382 '(1252 . 1) ;; Windows-1252 is a superset of iso-8859-1 (West
383 ;; Europe). See also `gnus-article-dumbquotes-map'.
384 '(1254 . 9) ;; Windows-1254 is a superset of iso-8859-9 (Turkish).
385 '(1255 . 8));; Windows-1255 is a superset of iso-8859-8 (Hebrew).
386 "A list of Windows codepage numbers and iso-8859 charset numbers.
387
388If an element is a number corresponding to a supported windows
389codepage, appropriate entries to `mm-charset-synonym-alist' are
390added by `mm-setup-codepage-iso-8859'. An element may also be a
391cons cell where the car is a codepage number and the cdr is the
392corresponding number of an iso-8859 charset."
393 :type '(list (set :inline t
394 (const 1250 :tag "Central and East European")
395 (const (1252 . 1) :tag "West European")
396 (const (1254 . 9) :tag "Turkish")
397 (const (1255 . 8) :tag "Hebrew"))
398 (repeat :inline t
399 :tag "Other options"
400 (choice
401 (integer :tag "Windows codepage number")
402 (cons (integer :tag "Windows codepage number")
403 (integer :tag "iso-8859 charset number")))))
404 :version "22.1" ;; Gnus 5.10.9
405 :group 'mime)
406
407(defcustom mm-codepage-ibm-list
408 (list 437 ;; (US etc.)
409 860 ;; (Portugal)
410 861 ;; (Iceland)
411 862 ;; (Israel)
412 863 ;; (Canadian French)
413 865 ;; (Nordic)
414 852 ;;
415 850 ;; (Latin 1)
416 855 ;; (Cyrillic)
417 866 ;; (Cyrillic - Russian)
418 857 ;; (Turkish)
419 864 ;; (Arabic)
420 869 ;; (Greek)
421 874);; (Thai)
422 ;; In Emacs 23 (unicode), cp... and ibm... are aliases.
423 ;; Cf. http://thread.gmane.org/v9lkng5nwy.fsf@marauder.physik.uni-ulm.de
424 "List of IBM codepage numbers.
425
9858f6c3 426The codepage mappings slightly differ between IBM and other vendors.
ab785936
MB
427See \"ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/IBM/README.TXT\".
428
429If an element is a number corresponding to a supported windows
430codepage, appropriate entries to `mm-charset-synonym-alist' are
431added by `mm-setup-codepage-ibm'."
432 :type '(list (set :inline t
433 (const 437 :tag "US etc.")
434 (const 860 :tag "Portugal")
435 (const 861 :tag "Iceland")
436 (const 862 :tag "Israel")
437 (const 863 :tag "Canadian French")
438 (const 865 :tag "Nordic")
439 (const 852)
440 (const 850 :tag "Latin 1")
441 (const 855 :tag "Cyrillic")
442 (const 866 :tag "Cyrillic - Russian")
443 (const 857 :tag "Turkish")
444 (const 864 :tag "Arabic")
445 (const 869 :tag "Greek")
446 (const 874 :tag "Thai"))
447 (repeat :inline t
448 :tag "Other options"
449 (integer :tag "Codepage number")))
450 :version "22.1" ;; Gnus 5.10.9
451 :group 'mime)
452
453(defun mm-setup-codepage-iso-8859 (&optional list)
454 "Add appropriate entries to `mm-charset-synonym-alist'.
455Unless LIST is given, `mm-codepage-iso-8859-list' is used."
456 (unless list
457 (setq list mm-codepage-iso-8859-list))
458 (dolist (i list)
459 (let (cp windows iso)
460 (if (consp i)
461 (setq cp (intern (format "cp%d" (car i)))
462 windows (intern (format "windows-%d" (car i)))
463 iso (intern (format "iso-8859-%d" (cdr i))))
464 (setq cp (intern (format "cp%d" i))
465 windows (intern (format "windows-%d" i))))
466 (unless (mm-coding-system-p windows)
467 (if (mm-coding-system-p cp)
468 (add-to-list 'mm-charset-synonym-alist (cons windows cp))
469 (add-to-list 'mm-charset-synonym-alist (cons windows iso)))))))
470
471(defun mm-setup-codepage-ibm (&optional list)
472 "Add appropriate entries to `mm-charset-synonym-alist'.
473Unless LIST is given, `mm-codepage-ibm-list' is used."
474 (unless list
475 (setq list mm-codepage-ibm-list))
476 (dolist (number list)
477 (let ((ibm (intern (format "ibm%d" number)))
478 (cp (intern (format "cp%d" number))))
479 (when (and (not (mm-coding-system-p ibm))
480 (mm-coding-system-p cp))
481 (add-to-list 'mm-charset-synonym-alist (cons ibm cp))))))
482
483;; Initialize:
484(mm-setup-codepage-iso-8859)
485(mm-setup-codepage-ibm)
bd29ba20 486
96a22201
KY
487;; Note: this has to be defined before `mm-charset-to-coding-system'.
488(defcustom mm-charset-eval-alist
489 (if (featurep 'xemacs)
490 nil ;; I don't know what would be useful for XEmacs.
0c43b6f8
KY
491 '(;; Emacs 22 provides autoloads for 1250-1258
492 ;; (i.e. `mm-codepage-setup' does nothing).
96a22201
KY
493 (windows-1250 . (mm-codepage-setup 1250 t))
494 (windows-1251 . (mm-codepage-setup 1251 t))
495 (windows-1253 . (mm-codepage-setup 1253 t))
496 (windows-1257 . (mm-codepage-setup 1257 t))))
497 "An alist of (CHARSET . FORM) pairs.
498If an article is encoded in an unknown CHARSET, FORM is
499evaluated. This allows to load additional libraries providing
500charsets on demand. If supported by your Emacs version, you
501could use `autoload-coding-system' here."
502 :version "22.1" ;; Gnus 5.10.9
503 :type '(list (set :inline t
504 (const (windows-1250 . (mm-codepage-setup 1250 t)))
505 (const (windows-1251 . (mm-codepage-setup 1251 t)))
506 (const (windows-1253 . (mm-codepage-setup 1253 t)))
507 (const (windows-1257 . (mm-codepage-setup 1257 t)))
508 (const (cp850 . (mm-codepage-setup 850 nil))))
509 (repeat :inline t
510 :tag "Other options"
511 (cons (symbol :tag "charset")
512 (symbol :tag "form"))))
513 :group 'mime)
514(put 'mm-charset-eval-alist 'risky-local-variable t)
515
58d8c5cd
GM
516(defvar mm-charset-override-alist)
517
96a22201
KY
518;; Note: this function has to be defined before `mm-charset-override-alist'
519;; since it will use this function in order to determine its default value
520;; when loading mm-util.elc.
521(defun mm-charset-to-coding-system (charset &optional lbt
522 allow-override silent)
523 "Return coding-system corresponding to CHARSET.
524CHARSET is a symbol naming a MIME charset.
525If optional argument LBT (`unix', `dos' or `mac') is specified, it is
526used as the line break code type of the coding system.
527
528If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
529map undesired charset names to their replacement. This should
530only be used for decoding, not for encoding.
531
532A non-nil value of SILENT means don't issue a warning even if CHARSET
533is not available."
534 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
535 (when (stringp charset)
536 (setq charset (intern (downcase charset))))
537 (when lbt
538 (setq charset (intern (format "%s-%s" charset lbt))))
539 (cond
540 ((null charset)
541 charset)
542 ;; Running in a non-MULE environment.
543 ((or (null (mm-get-coding-system-list))
544 (not (fboundp 'coding-system-get)))
545 charset)
546 ;; Check override list quite early. Should only used for decoding, not for
547 ;; encoding!
548 ((and allow-override
549 (let ((cs (cdr (assq charset mm-charset-override-alist))))
550 (and cs (mm-coding-system-p cs) cs))))
551 ;; ascii
faf3b348
LMI
552 ((or (eq charset 'us-ascii)
553 (string-match "ansi.x3.4" (symbol-name charset)))
96a22201
KY
554 'ascii)
555 ;; Check to see whether we can handle this charset. (This depends
556 ;; on there being some coding system matching each `mime-charset'
557 ;; property defined, as there should be.)
558 ((and (mm-coding-system-p charset)
559;;; Doing this would potentially weed out incorrect charsets.
560;;; charset
561;;; (eq charset (coding-system-get charset 'mime-charset))
562 )
563 charset)
ddf5d974
KY
564 ;; Use coding system Emacs knows.
565 ((and (fboundp 'coding-system-from-name)
566 (coding-system-from-name charset)))
96a22201
KY
567 ;; Eval expressions from `mm-charset-eval-alist'
568 ((let* ((el (assq charset mm-charset-eval-alist))
569 (cs (car el))
570 (form (cdr el)))
571 (and cs
572 form
573 (prog2
574 ;; Avoid errors...
575 (condition-case nil (eval form) (error nil))
576 ;; (message "Failed to eval `%s'" form))
577 (mm-coding-system-p cs)
578 (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
579 cs)))
580 ;; Translate invalid charsets.
581 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
582 (and cs
583 (mm-coding-system-p cs)
584 ;; (message
585 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
586 ;; cs charset)
587 cs)))
588 ;; Last resort: search the coding system list for entries which
589 ;; have the right mime-charset in case the canonical name isn't
590 ;; defined (though it should be).
591 ((let (cs)
592 ;; mm-get-coding-system-list returns a list of cs without lbt.
593 ;; Do we need -lbt?
594 (dolist (c (mm-get-coding-system-list))
595 (if (and (null cs)
596 (eq charset (or (coding-system-get c :mime-charset)
597 (coding-system-get c 'mime-charset))))
598 (setq cs c)))
599 (unless (or silent cs)
600 ;; Warn the user about unknown charset:
601 (if (fboundp 'gnus-message)
602 (gnus-message 7 "Unknown charset: %s" charset)
603 (message "Unknown charset: %s" charset)))
604 cs))))
605
606;; Note: `mm-charset-to-coding-system' has to be defined before this.
bd29ba20 607(defcustom mm-charset-override-alist
96a22201
KY
608 ;; Note: pairs that cannot be used in the Emacs version currently running
609 ;; will be removed.
610 '((gb2312 . gbk)
611 (iso-8859-1 . windows-1252)
01c52d31
MB
612 (iso-8859-8 . windows-1255)
613 (iso-8859-9 . windows-1254))
bd29ba20
RS
614 "A mapping from undesired charset names to their replacement.
615
616You may add pairs like (iso-8859-1 . windows-1252) here,
617i.e. treat iso-8859-1 as windows-1252. windows-1252 is a
618superset of iso-8859-1."
b6b8f5fd
KY
619 :type
620 '(list
621 :convert-widget
622 (lambda (widget)
623 (let ((defaults
624 (delq nil
625 (mapcar (lambda (pair)
96a22201
KY
626 (if (mm-charset-to-coding-system (cdr pair)
627 nil nil t)
b6b8f5fd
KY
628 pair))
629 '((gb2312 . gbk)
630 (iso-8859-1 . windows-1252)
631 (iso-8859-8 . windows-1255)
632 (iso-8859-9 . windows-1254)
633 (undecided . windows-1252)))))
634 (val (copy-sequence (default-value 'mm-charset-override-alist)))
635 pair rest)
636 (while val
637 (push (if (and (prog1
638 (setq pair (assq (caar val) defaults))
639 (setq defaults (delq pair defaults)))
640 (equal (car val) pair))
641 `(const ,pair)
642 `(cons :format "%v"
643 (const :format "(%v" ,(caar val))
644 (symbol :size 3 :format " . %v)\n" ,(cdar val))))
645 rest)
646 (setq val (cdr val)))
647 (while defaults
648 (push `(const ,(pop defaults)) rest))
649 (widget-convert
650 'list
651 `(set :inline t :format "%v" ,@(nreverse rest))
652 `(repeat :inline t :tag "Other options"
653 (cons :format "%v"
654 (symbol :size 3 :format "(%v")
655 (symbol :size 3 :format " . %v)\n")))))))
96a22201
KY
656 ;; Remove pairs that cannot be used in the Emacs version currently
657 ;; running. Note that this section will be evaluated when loading
658 ;; mm-util.elc.
659 :set (lambda (symbol value)
660 (custom-set-default
661 symbol (delq nil
662 (mapcar (lambda (pair)
663 (if (mm-charset-to-coding-system (cdr pair)
664 nil nil t)
665 pair))
666 value))))
67099291 667 :version "22.1" ;; Gnus 5.10.9
bd29ba20
RS
668 :group 'mime)
669
c113de23 670(defvar mm-binary-coding-system
95fa1ff7 671 (cond
c113de23
GM
672 ((mm-coding-system-p 'binary) 'binary)
673 ((mm-coding-system-p 'no-conversion) 'no-conversion)
674 (t nil))
675 "100% binary coding system.")
676
677(defvar mm-text-coding-system
f5ec697d 678 (or (if (memq system-type '(windows-nt ms-dos))
c113de23
GM
679 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
680 (and (mm-coding-system-p 'raw-text) 'raw-text))
681 mm-binary-coding-system)
682 "Text-safe coding system (For removing ^M).")
683
684(defvar mm-text-coding-system-for-write nil
685 "Text coding system for write.")
686
687(defvar mm-auto-save-coding-system
95fa1ff7 688 (cond
23f87bed 689 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
f5ec697d 690 (if (memq system-type '(windows-nt ms-dos))
56e09c09
DL
691 (if (mm-coding-system-p 'utf-8-emacs-dos)
692 'utf-8-emacs-dos mm-binary-coding-system)
693 'utf-8-emacs))
c113de23 694 ((mm-coding-system-p 'emacs-mule)
f5ec697d 695 (if (memq system-type '(windows-nt ms-dos))
95fa1ff7 696 (if (mm-coding-system-p 'emacs-mule-dos)
c113de23
GM
697 'emacs-mule-dos mm-binary-coding-system)
698 'emacs-mule))
699 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
700 (t mm-binary-coding-system))
701 "Coding system of auto save file.")
702
95fa1ff7 703(defvar mm-universal-coding-system mm-auto-save-coding-system
47b63dfa 704 "The universal coding system.")
95fa1ff7
SZ
705
706;; Fixme: some of the cars here aren't valid MIME charsets. That
707;; should only matter with XEmacs, though.
708(defvar mm-mime-mule-charset-alist
709 `((us-ascii ascii)
710 (iso-8859-1 latin-iso8859-1)
711 (iso-8859-2 latin-iso8859-2)
712 (iso-8859-3 latin-iso8859-3)
713 (iso-8859-4 latin-iso8859-4)
714 (iso-8859-5 cyrillic-iso8859-5)
715 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
716 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
717 ;; charset is koi8-r, not iso-8859-5.
718 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
719 (iso-8859-6 arabic-iso8859-6)
720 (iso-8859-7 greek-iso8859-7)
721 (iso-8859-8 hebrew-iso8859-8)
722 (iso-8859-9 latin-iso8859-9)
723 (iso-8859-14 latin-iso8859-14)
724 (iso-8859-15 latin-iso8859-15)
725 (viscii vietnamese-viscii-lower)
726 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
727 (euc-kr korean-ksc5601)
728 (gb2312 chinese-gb2312)
13287a2a
KH
729 (gbk chinese-gbk)
730 (gb18030 gb18030-2-byte
731 gb18030-4-byte-bmp gb18030-4-byte-smp
732 gb18030-4-byte-ext-1 gb18030-4-byte-ext-2)
95fa1ff7
SZ
733 (big5 chinese-big5-1 chinese-big5-2)
734 (tibetan tibetan)
735 (thai-tis620 thai-tis620)
0683d241 736 (windows-1251 cyrillic-iso8859-5)
95fa1ff7
SZ
737 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
738 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
739 latin-jisx0201 japanese-jisx0208-1978
740 chinese-gb2312 japanese-jisx0208
0683d241 741 korean-ksc5601 japanese-jisx0212)
95fa1ff7
SZ
742 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
743 latin-jisx0201 japanese-jisx0208-1978
744 chinese-gb2312 japanese-jisx0208
745 korean-ksc5601 japanese-jisx0212
746 chinese-cns11643-1 chinese-cns11643-2)
747 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
748 cyrillic-iso8859-5 greek-iso8859-7
749 latin-jisx0201 japanese-jisx0208-1978
750 chinese-gb2312 japanese-jisx0208
751 korean-ksc5601 japanese-jisx0212
752 chinese-cns11643-1 chinese-cns11643-2
753 chinese-cns11643-3 chinese-cns11643-4
754 chinese-cns11643-5 chinese-cns11643-6
755 chinese-cns11643-7)
0683d241
MB
756 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
757 japanese-jisx0213-1 japanese-jisx0213-2)
758 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
26c9afc3
MB
759 ,(cond ((fboundp 'unicode-precedence-list)
760 (cons 'utf-8 (delq 'ascii (mapcar 'charset-name
761 (unicode-precedence-list)))))
762 ((or (not (fboundp 'charsetp)) ;; non-Mule case
763 (charsetp 'unicode-a)
764 (not (mm-coding-system-p 'mule-utf-8)))
765 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
766 (t ;; If we have utf-8 we're in Mule 5+.
767 (append '(utf-8)
768 (delete 'ascii
769 (coding-system-get 'mule-utf-8 'safe-charsets))))))
95fa1ff7
SZ
770 "Alist of MIME-charset/MULE-charsets.")
771
0683d241
MB
772(defun mm-enrich-utf-8-by-mule-ucs ()
773 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
774This function will run when the `un-define' module is loaded under
775XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
776with Mule charsets. It is completely useless for Emacs."
0683d241
MB
777 (when (boundp 'unicode-basic-translation-charset-order-list)
778 (condition-case nil
779 (let ((val (delq
780 'ascii
781 (copy-sequence
782 (symbol-value
783 'unicode-basic-translation-charset-order-list))))
784 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
785 (if elem
786 (setcdr elem val)
787 (setq mm-mime-mule-charset-alist
788 (nconc mm-mime-mule-charset-alist
789 (list (cons 'utf-8 val))))))
790 (error))))
791
792;; Correct by construction, but should be unnecessary for Emacs:
793(if (featurep 'xemacs)
794 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
795 (when (and (fboundp 'coding-system-list)
796 (fboundp 'sort-coding-systems))
797 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
798 cs mime mule alist)
799 (while css
800 (setq cs (pop css)
5432dcf9 801 mime (or (coding-system-get cs :mime-charset); Emacs 23 (unicode)
0683d241
MB
802 (coding-system-get cs 'mime-charset)))
803 (when (and mime
804 (not (eq t (setq mule
805 (coding-system-get cs 'safe-charsets))))
806 (not (assq mime alist)))
807 (push (cons mime (delq 'ascii mule)) alist)))
808 (setq mm-mime-mule-charset-alist (nreverse alist)))))
95fa1ff7 809
f5490ddc
MB
810(defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
811 "A list of special charsets.
812Valid elements include:
813`iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
814`iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
815)
816
817(defvar mm-iso-8859-15-compatible
818 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
819 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
820 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
821
822(defvar mm-iso-8859-x-to-15-table
823 (and (fboundp 'coding-system-p)
824 (mm-coding-system-p 'iso-8859-15)
825 (mapcar
826 (lambda (cs)
827 (if (mm-coding-system-p (car cs))
828 (let ((c (string-to-char
829 (decode-coding-string "\341" (car cs)))))
830 (cons (char-charset c)
831 (cons
832 (- (string-to-char
833 (decode-coding-string "\341" 'iso-8859-15)) c)
834 (string-to-list (decode-coding-string (car (cdr cs))
835 (car cs))))))
836 '(gnus-charset 0)))
837 mm-iso-8859-15-compatible))
838 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
839
23f87bed 840(defcustom mm-coding-system-priorities
548f737d
MB
841 (let ((lang (if (boundp 'current-language-environment)
842 (symbol-value 'current-language-environment))))
843 (cond (;; XEmacs without Mule but with `file-coding'.
844 (not lang) nil)
845 ;; In XEmacs 21.5 it may be the one like "Japanese (UTF-8)".
846 ((string-match "\\`Japanese" lang)
dab3a8d5
KY
847 ;; Japanese users prefer iso-2022-jp to others usually used
848 ;; for `buffer-file-coding-system', however iso-8859-1 should
849 ;; be used when there are only ASCII and Latin-1 characters.
850 '(iso-8859-1 iso-2022-jp utf-8))))
23f87bed
MB
851 "Preferred coding systems for encoding outgoing messages.
852
853More than one suitable coding system may be found for some text.
854By default, the coding system with the highest priority is used
855to encode outgoing messages (see `sort-coding-systems'). If this
856variable is set, it overrides the default priority."
dab3a8d5 857 :version "24.4"
23f87bed
MB
858 :type '(repeat (symbol :tag "Coding system"))
859 :group 'mime)
860
861;; ??
1f7d2e14
SZ
862(defvar mm-use-find-coding-systems-region
863 (fboundp 'find-coding-systems-region)
23f87bed
MB
864 "Use `find-coding-systems-region' to find proper coding systems.
865
866Setting it to nil is useful on Emacsen supporting Unicode if sending
867mail with multiple parts is preferred to sending a Unicode one.")
1f7d2e14 868
c912b478
KY
869(defvar mm-extra-numeric-entities
870 (mapcar
871 (lambda (item)
872 (cons (car item) (mm-ucs-to-char (cdr item))))
873 '((#x80 . #x20AC) (#x82 . #x201A) (#x83 . #x0192) (#x84 . #x201E)
874 (#x85 . #x2026) (#x86 . #x2020) (#x87 . #x2021) (#x88 . #x02C6)
875 (#x89 . #x2030) (#x8A . #x0160) (#x8B . #x2039) (#x8C . #x0152)
876 (#x8E . #x017D) (#x91 . #x2018) (#x92 . #x2019) (#x93 . #x201C)
877 (#x94 . #x201D) (#x95 . #x2022) (#x96 . #x2013) (#x97 . #x2014)
878 (#x98 . #x02DC) (#x99 . #x2122) (#x9A . #x0161) (#x9B . #x203A)
879 (#x9C . #x0153) (#x9E . #x017E) (#x9F . #x0178)))
880 "*Alist of extra numeric entities and characters other than ISO 10646.
881This table is used for decoding extra numeric entities to characters,
882like \"&#128;\" to the euro sign, mainly in html messages.")
883
c113de23
GM
884;;; Internal variables:
885
886;;; Functions:
887
888(defun mm-mule-charset-to-mime-charset (charset)
1c57d870 889 "Return the MIME charset corresponding to the given Mule CHARSET."
23f87bed
MB
890 (if (and (fboundp 'find-coding-systems-for-charsets)
891 (fboundp 'sort-coding-systems))
0683d241
MB
892 (let ((css (sort (sort-coding-systems
893 (find-coding-systems-for-charsets (list charset)))
894 'mm-sort-coding-systems-predicate))
895 cs mime)
896 (while (and (not mime)
897 css)
898 (when (setq cs (pop css))
899 (setq mime (or (coding-system-get cs :mime-charset)
900 (coding-system-get cs 'mime-charset)))))
95fa1ff7 901 mime)
0683d241
MB
902 (let ((alist (mapcar (lambda (cs)
903 (assq cs mm-mime-mule-charset-alist))
904 (sort (mapcar 'car mm-mime-mule-charset-alist)
905 'mm-sort-coding-systems-predicate)))
95fa1ff7
SZ
906 out)
907 (while alist
908 (when (memq charset (cdar alist))
909 (setq out (caar alist)
910 alist nil))
911 (pop alist))
912 out)))
c113de23 913
95fa1ff7 914(eval-and-compile
765d4319
KY
915 (if (featurep 'xemacs)
916 (defalias 'mm-enable-multibyte 'ignore)
917 (defun mm-enable-multibyte ()
918 "Set the multibyte flag of the current buffer.
1c57d870
DL
919Only do this if the default value of `enable-multibyte-characters' is
920non-nil. This is a no-op in XEmacs."
144b7b5c 921 (set-buffer-multibyte 'to)))
c113de23 922
765d4319
KY
923 (if (featurep 'xemacs)
924 (defalias 'mm-disable-multibyte 'ignore)
925 (defun mm-disable-multibyte ()
926 "Unset the multibyte flag of in the current buffer.
1c57d870 927This is a no-op in XEmacs."
765d4319 928 (set-buffer-multibyte nil))))
052802c1 929
c113de23
GM
930(defun mm-preferred-coding-system (charset)
931 ;; A typo in some Emacs versions.
47b63dfa
SZ
932 (or (get-charset-property charset 'preferred-coding-system)
933 (get-charset-property charset 'prefered-coding-system)))
c113de23 934
23f87bed
MB
935;; Mule charsets shouldn't be used.
936(defsubst mm-guess-charset ()
937 "Guess Mule charset from the language environment."
938 (or
939 mail-parse-mule-charset ;; cached mule-charset
940 (progn
941 (setq mail-parse-mule-charset
942 (and (boundp 'current-language-environment)
943 (car (last
944 (assq 'charset
945 (assoc current-language-environment
946 language-info-alist))))))
947 (if (or (not mail-parse-mule-charset)
948 (eq mail-parse-mule-charset 'ascii))
949 (setq mail-parse-mule-charset
950 (or (car (last (assq mail-parse-charset
951 mm-mime-mule-charset-alist)))
952 ;; default
953 'latin-iso8859-1)))
954 mail-parse-mule-charset)))
955
c113de23
GM
956(defun mm-charset-after (&optional pos)
957 "Return charset of a character in current buffer at position POS.
cd1181db 958If POS is nil, it defaults to the current point.
c113de23
GM
959If POS is out of range, the value is nil.
960If the charset is `composition', return the actual one."
052802c1
DL
961 (let ((char (char-after pos)) charset)
962 (if (< (mm-char-int char) 128)
963 (setq charset 'ascii)
964 ;; charset-after is fake in some Emacsen.
965 (setq charset (and (fboundp 'char-charset) (char-charset char)))
56e09c09 966 (if (eq charset 'composition) ; Mule 4
052802c1
DL
967 (let ((p (or pos (point))))
968 (cadr (find-charset-region p (1+ p))))
969 (if (and charset (not (memq charset '(ascii eight-bit-control
970 eight-bit-graphic))))
971 charset
23f87bed 972 (mm-guess-charset))))))
c113de23
GM
973
974(defun mm-mime-charset (charset)
1c57d870 975 "Return the MIME charset corresponding to the given Mule CHARSET."
95fa1ff7
SZ
976 (if (eq charset 'unknown)
977 (error "The message contains non-printable characters, please use attachment"))
052802c1 978 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
c113de23
GM
979 (or
980 (and (mm-preferred-coding-system charset)
56e09c09
DL
981 (or (coding-system-get
982 (mm-preferred-coding-system charset) :mime-charset)
983 (coding-system-get
984 (mm-preferred-coding-system charset) 'mime-charset)))
c113de23
GM
985 (and (eq charset 'ascii)
986 'us-ascii)
95fa1ff7 987 (mm-preferred-coding-system charset)
c113de23
GM
988 (mm-mule-charset-to-mime-charset charset))
989 ;; This is for XEmacs.
990 (mm-mule-charset-to-mime-charset charset)))
991
ed797193 992;; `delete-dups' is not available in XEmacs 21.4.
8753ddee
MB
993(if (fboundp 'delete-dups)
994 (defalias 'mm-delete-duplicates 'delete-dups)
995 (defun mm-delete-duplicates (list)
996 "Destructively remove `equal' duplicates from LIST.
997Store the result in LIST and return it. LIST must be a proper list.
998Of several `equal' occurrences of an element in LIST, the first
999one is kept.
1000
1001This is a compatibility function for Emacsen without `delete-dups'."
1002 ;; Code from `subr.el' in Emacs 22:
1003 (let ((tail list))
1004 (while tail
1005 (setcdr tail (delete (car tail) (cdr tail)))
1006 (setq tail (cdr tail))))
1007 list))
c113de23 1008
23f87bed
MB
1009;; Fixme: This is used in places when it should be testing the
1010;; default multibyteness. See mm-default-multibyte-p.
1011(eval-and-compile
052802c1
DL
1012 (if (and (not (featurep 'xemacs))
1013 (boundp 'enable-multibyte-characters))
23f87bed
MB
1014 (defun mm-multibyte-p ()
1015 "Non-nil if multibyte is enabled in the current buffer."
1016 enable-multibyte-characters)
1017 (defun mm-multibyte-p () (featurep 'mule))))
1018
1019(defun mm-default-multibyte-p ()
1020 "Return non-nil if the session is multibyte.
1021This affects whether coding conversion should be attempted generally."
1022 (if (featurep 'mule)
2e62b574
GM
1023 (if (boundp 'enable-multibyte-characters)
1024 (default-value 'enable-multibyte-characters)
23f87bed 1025 t)))
c113de23 1026
f5490ddc
MB
1027(defun mm-iso-8859-x-to-15-region (&optional b e)
1028 (if (fboundp 'char-charset)
1029 (let (charset item c inconvertible)
1030 (save-restriction
1031 (if e (narrow-to-region b e))
1032 (goto-char (point-min))
1033 (skip-chars-forward "\0-\177")
1034 (while (not (eobp))
1035 (cond
1036 ((not (setq item (assq (char-charset (setq c (char-after)))
1037 mm-iso-8859-x-to-15-table)))
1038 (forward-char))
1039 ((memq c (cdr (cdr item)))
1040 (setq inconvertible t)
1041 (forward-char))
1042 (t
1043 (insert-before-markers (prog1 (+ c (car (cdr item)))
1044 (delete-char 1)))))
1045 (skip-chars-forward "\0-\177")))
1046 (not inconvertible))))
1047
47b63dfa 1048(defun mm-sort-coding-systems-predicate (a b)
23f87bed
MB
1049 (let ((priorities
1050 (mapcar (lambda (cs)
1051 ;; Note: invalid entries are dropped silently
0683d241 1052 (and (setq cs (mm-coding-system-p cs))
23f87bed
MB
1053 (coding-system-base cs)))
1054 mm-coding-system-priorities)))
0683d241
MB
1055 (and (setq a (mm-coding-system-p a))
1056 (if (setq b (mm-coding-system-p b))
1057 (> (length (memq (coding-system-base a) priorities))
1058 (length (memq (coding-system-base b) priorities)))
1059 t))))
47b63dfa 1060
aa0a8561
MB
1061(eval-when-compile
1062 (autoload 'latin-unity-massage-name "latin-unity")
1063 (autoload 'latin-unity-maybe-remap "latin-unity")
1064 (autoload 'latin-unity-representations-feasible-region "latin-unity")
9efa445f
DN
1065 (autoload 'latin-unity-representations-present-region "latin-unity"))
1066
1067(defvar latin-unity-coding-systems)
1068(defvar latin-unity-ucs-list)
aa0a8561
MB
1069
1070(defun mm-xemacs-find-mime-charset-1 (begin end)
1071 "Determine which MIME charset to use to send region as message.
1072This uses the XEmacs-specific latin-unity package to better handle the
1073case where identical characters from diverse ISO-8859-? character sets
1074can be encoded using a single one of the corresponding coding systems.
1075
1076It treats `mm-coding-system-priorities' as the list of preferred
1077coding systems; a useful example setting for this list in Western
1078Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
1079to the very standard Latin 1 coding system, and only move to coding
1080systems that are less supported as is necessary to encode the
1081characters that exist in the buffer.
1082
1083Latin Unity doesn't know about those non-ASCII Roman characters that
1084are available in various East Asian character sets. As such, its
1085behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
1086buffer and it can otherwise be encoded as Latin 1, won't be ideal.
1087But this is very much a corner case, so don't worry about it."
1088 (let ((systems mm-coding-system-priorities) csets psets curset)
1089
1090 ;; Load the Latin Unity library, if available.
1091 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
01c52d31 1092 (require 'latin-unity))
aa0a8561
MB
1093
1094 ;; Now, can we use it?
1095 (if (featurep 'latin-unity)
1096 (progn
1097 (setq csets (latin-unity-representations-feasible-region begin end)
1098 psets (latin-unity-representations-present-region begin end))
1099
1100 (catch 'done
1101
1102 ;; Pass back the first coding system in the preferred list
1103 ;; that can encode the whole region.
1104 (dolist (curset systems)
1105 (setq curset (latin-unity-massage-name 'buffer-default curset))
1106
1107 ;; If the coding system is a universal coding system, then
1108 ;; it can certainly encode all the characters in the region.
1109 (if (memq curset latin-unity-ucs-list)
1110 (throw 'done (list curset)))
1111
1112 ;; If a coding system isn't universal, and isn't in
1113 ;; the list that latin unity knows about, we can't
1114 ;; decide whether to use it here. Leave that until later
1115 ;; in `mm-find-mime-charset-region' function, whence we
1116 ;; have been called.
1117 (unless (memq curset latin-unity-coding-systems)
1118 (throw 'done nil))
1119
1120 ;; Right, we know about this coding system, and it may
1121 ;; conceivably be able to encode all the characters in
1122 ;; the region.
1123 (if (latin-unity-maybe-remap begin end curset csets psets t)
1124 (throw 'done (list curset))))
1125
1126 ;; Can't encode using anything from the
1127 ;; `mm-coding-system-priorities' list.
1128 ;; Leave `mm-find-mime-charset' to do most of the work.
1129 nil))
1130
1131 ;; Right, latin unity isn't available; let `mm-find-charset-region'
1132 ;; take its default action, which equally applies to GNU Emacs.
1133 nil)))
1134
1135(defmacro mm-xemacs-find-mime-charset (begin end)
1136 (when (featurep 'xemacs)
10ace8ea 1137 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
aa0a8561 1138
b5000590
GM
1139(declare-function mm-delete-duplicates "mm-util" (list))
1140
47b63dfa 1141(defun mm-find-mime-charset-region (b e &optional hack-charsets)
95fa1ff7 1142 "Return the MIME charsets needed to encode the region between B and E.
f0529b5b 1143nil means ASCII, a single-element list represents an appropriate MIME
95fa1ff7 1144charset, and a longer list means no appropriate charset."
47b63dfa
SZ
1145 (let (charsets)
1146 ;; The return possibilities of this function are a mess...
1147 (or (and (mm-multibyte-p)
1f7d2e14 1148 mm-use-find-coding-systems-region
47b63dfa
SZ
1149 ;; Find the mime-charset of the most preferred coding
1150 ;; system that has one.
1151 (let ((systems (find-coding-systems-region b e)))
1152 (when mm-coding-system-priorities
a1506d29 1153 (setq systems
47b63dfa 1154 (sort systems 'mm-sort-coding-systems-predicate)))
47b63dfa
SZ
1155 (setq systems (delq 'compound-text systems))
1156 (unless (equal systems '(undecided))
1157 (while systems
56e09c09
DL
1158 (let* ((head (pop systems))
1159 (cs (or (coding-system-get head :mime-charset)
1160 (coding-system-get head 'mime-charset))))
23f87bed
MB
1161 ;; The mime-charset (`x-ctext') of
1162 ;; `compound-text' is not in the IANA list. We
1163 ;; shouldn't normally use anything here with a
1164 ;; mime-charset having an `x-' prefix.
1165 ;; Fixme: Allow this to be overridden, since
1166 ;; there is existing use of x-ctext.
1167 ;; Also people apparently need the coding system
1168 ;; `iso-2022-jp-3' (which Mule-UCS defines with
1169 ;; mime-charset, though it's not valid).
1170 (if (and cs
1171 (not (string-match "^[Xx]-" (symbol-name cs)))
1172 ;; UTF-16 of any variety is invalid for
1173 ;; text parts and, unfortunately, has
1174 ;; mime-charset defined both in Mule-UCS
1175 ;; and versions of Emacs. (The name
1176 ;; might be `mule-utf-16...' or
1177 ;; `utf-16...'.)
1178 (not (string-match "utf-16" (symbol-name cs))))
47b63dfa
SZ
1179 (setq systems nil
1180 charsets (list cs))))))
1181 charsets))
aa0a8561
MB
1182 ;; If we're XEmacs, and some coding system is appropriate,
1183 ;; mm-xemacs-find-mime-charset will return an appropriate list.
1184 ;; Otherwise, we'll get nil, and the next setq will get invoked.
1185 (setq charsets (mm-xemacs-find-mime-charset b e))
1186
8589dc17 1187 ;; Fixme: won't work for unibyte Emacs 23:
0c129bca 1188
aa0a8561 1189 ;; We're not multibyte, or a single coding system won't cover it.
a1506d29 1190 (setq charsets
47b63dfa
SZ
1191 (mm-delete-duplicates
1192 (mapcar 'mm-mime-charset
1193 (delq 'ascii
1194 (mm-find-charset-region b e))))))
f5490ddc
MB
1195 (if (and (> (length charsets) 1)
1196 (memq 'iso-8859-15 charsets)
1197 (memq 'iso-8859-15 hack-charsets)
1198 (save-excursion (mm-iso-8859-x-to-15-region b e)))
1199 (dolist (x mm-iso-8859-15-compatible)
1200 (setq charsets (delq (car x) charsets))))
1201 (if (and (memq 'iso-2022-jp-2 charsets)
1202 (memq 'iso-2022-jp-2 hack-charsets))
1203 (setq charsets (delq 'iso-2022-jp charsets)))
1204 ;; Attempt to reduce the number of charsets if utf-8 is available.
1205 (if (and (featurep 'xemacs)
1206 (> (length charsets) 1)
1207 (mm-coding-system-p 'utf-8))
1208 (let ((mm-coding-system-priorities
1209 (cons 'utf-8 mm-coding-system-priorities)))
1210 (setq charsets
1211 (mm-delete-duplicates
1212 (mapcar 'mm-mime-charset
1213 (delq 'ascii
1214 (mm-find-charset-region b e)))))))
47b63dfa 1215 charsets))
95fa1ff7 1216
c113de23
GM
1217(defmacro mm-with-unibyte-buffer (&rest forms)
1218 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1c57d870 1219Use unibyte mode for this."
d37ded9e
SM
1220 `(with-temp-buffer
1221 (mm-disable-multibyte)
1222 ,@forms))
c113de23
GM
1223(put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
1224(put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
1225
23f87bed
MB
1226(defmacro mm-with-multibyte-buffer (&rest forms)
1227 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1228Use multibyte mode for this."
d37ded9e
SM
1229 `(with-temp-buffer
1230 (mm-enable-multibyte)
1231 ,@forms))
23f87bed
MB
1232(put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
1233(put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
1234
c113de23 1235(defmacro mm-with-unibyte-current-buffer (&rest forms)
56e09c09 1236 "Evaluate FORMS with current buffer temporarily made unibyte.
72e841ce
KY
1237Equivalent to `progn' in XEmacs.
1238
1239Note: We recommend not using this macro any more; there should be
1240better ways to do a similar thing. The previous version of this macro
1241bound the default value of `enable-multibyte-characters' to nil while
1242evaluating FORMS but it is no longer done. So, some programs assuming
1243it if any may malfunction."
765d4319
KY
1244 (if (featurep 'xemacs)
1245 `(progn ,@forms)
72e841ce
KY
1246 (let ((multibyte (make-symbol "multibyte")))
1247 `(let ((,multibyte enable-multibyte-characters))
1248 (when ,multibyte
1249 (set-buffer-multibyte nil))
1250 (prog1
1251 (progn ,@forms)
1252 (when ,multibyte
1253 (set-buffer-multibyte t)))))))
c113de23
GM
1254(put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
1255(put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
1256
c113de23 1257(defun mm-find-charset-region (b e)
1c57d870 1258 "Return a list of Emacs charsets in the region B to E."
c113de23
GM
1259 (cond
1260 ((and (mm-multibyte-p)
95fa1ff7 1261 (fboundp 'find-charset-region))
c113de23 1262 ;; Remove composition since the base charsets have been included.
95fa1ff7
SZ
1263 ;; Remove eight-bit-*, treat them as ascii.
1264 (let ((css (find-charset-region b e)))
01c52d31
MB
1265 (dolist (cs
1266 '(composition eight-bit-control eight-bit-graphic control-1)
1267 css)
1268 (setq css (delq cs css)))))
052802c1
DL
1269 (t
1270 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
c113de23
GM
1271 (save-excursion
1272 (save-restriction
1273 (narrow-to-region b e)
1274 (goto-char (point-min))
1275 (skip-chars-forward "\0-\177")
1276 (if (eobp)
1277 '(ascii)
052802c1
DL
1278 (let (charset)
1279 (setq charset
1280 (and (boundp 'current-language-environment)
95fa1ff7
SZ
1281 (car (last (assq 'charset
1282 (assoc current-language-environment
052802c1
DL
1283 language-info-alist))))))
1284 (if (eq charset 'ascii) (setq charset nil))
1285 (or charset
1286 (setq charset
1287 (car (last (assq mail-parse-charset
1288 mm-mime-mule-charset-alist)))))
1289 (list 'ascii (or charset 'latin-iso8859-1)))))))))
c113de23 1290
c113de23
GM
1291(defun mm-auto-mode-alist ()
1292 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1293 (let ((alist auto-mode-alist)
1294 out)
1295 (while alist
1296 (when (listp (cdar alist))
1297 (push (car alist) out))
1298 (pop alist))
1299 (nreverse out)))
1300
1301(defvar mm-inhibit-file-name-handlers
01c52d31 1302 '(jka-compr-handler image-file-handler epa-file-handler)
c113de23
GM
1303 "A list of handlers doing (un)compression (etc) thingies.")
1304
1305(defun mm-insert-file-contents (filename &optional visit beg end replace
1306 inhibit)
23f87bed 1307 "Like `insert-file-contents', but only reads in the file.
c113de23
GM
1308A buffer may be modified in several ways after reading into the buffer due
1309to advanced Emacs features, such as file-name-handlers, format decoding,
23f87bed 1310`find-file-hooks', etc.
56e09c09 1311If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
c113de23 1312 This function ensures that none of these modifications will take place."
14acf2f5
SM
1313 (letf* ((format-alist nil)
1314 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
1315 ((default-value 'major-mode) 'fundamental-mode)
1316 (enable-local-variables nil)
1317 (after-insert-file-functions nil)
1318 (enable-local-eval nil)
1319 (inhibit-file-name-operation (if inhibit
1320 'insert-file-contents
1321 inhibit-file-name-operation))
1322 (inhibit-file-name-handlers
1323 (if inhibit
1324 (append mm-inhibit-file-name-handlers
1325 inhibit-file-name-handlers)
1326 inhibit-file-name-handlers))
1327 (ffh (if (boundp 'find-file-hook)
1328 'find-file-hook
1329 'find-file-hooks))
1330 (val (symbol-value ffh)))
4a43ee9b
MB
1331 (set ffh nil)
1332 (unwind-protect
1333 (insert-file-contents filename visit beg end replace)
1334 (set ffh val))))
c113de23
GM
1335
1336(defun mm-append-to-file (start end filename &optional codesys inhibit)
1337 "Append the contents of the region to the end of file FILENAME.
1338When called from a function, expects three arguments,
1339START, END and FILENAME. START and END are buffer positions
1340saying what text to write.
1341Optional fourth argument specifies the coding system to use when
1342encoding the file.
23f87bed 1343If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
95fa1ff7
SZ
1344 (let ((coding-system-for-write
1345 (or codesys mm-text-coding-system-for-write
c113de23 1346 mm-text-coding-system))
95fa1ff7 1347 (inhibit-file-name-operation (if inhibit
c113de23
GM
1348 'append-to-file
1349 inhibit-file-name-operation))
1350 (inhibit-file-name-handlers
1351 (if inhibit
95fa1ff7 1352 (append mm-inhibit-file-name-handlers
c113de23
GM
1353 inhibit-file-name-handlers)
1354 inhibit-file-name-handlers)))
23f87bed
MB
1355 (write-region start end filename t 'no-message)
1356 (message "Appended to %s" filename)))
c113de23 1357
95fa1ff7 1358(defun mm-write-region (start end filename &optional append visit lockname
c113de23
GM
1359 coding-system inhibit)
1360
1361 "Like `write-region'.
23f87bed 1362If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
95fa1ff7
SZ
1363 (let ((coding-system-for-write
1364 (or coding-system mm-text-coding-system-for-write
c113de23 1365 mm-text-coding-system))
95fa1ff7 1366 (inhibit-file-name-operation (if inhibit
c113de23
GM
1367 'write-region
1368 inhibit-file-name-operation))
1369 (inhibit-file-name-handlers
1370 (if inhibit
95fa1ff7 1371 (append mm-inhibit-file-name-handlers
c113de23
GM
1372 inhibit-file-name-handlers)
1373 inhibit-file-name-handlers)))
1374 (write-region start end filename append visit lockname)))
1375
b5000590 1376(autoload 'gmm-write-region "gmm-utils")
d1e12aef
KY
1377(declare-function help-function-arglist "help-fns"
1378 (def &optional preserve-names))
b5000590 1379
cf5a5c38
MB
1380;; It is not a MIME function, but some MIME functions use it.
1381(if (and (fboundp 'make-temp-file)
1382 (ignore-errors
cb8b0736
KY
1383 (let ((def (if (fboundp 'compiled-function-arglist) ;; XEmacs
1384 (eval (list 'compiled-function-arglist
1385 (symbol-function 'make-temp-file)))
1386 (require 'help-fns)
1387 (help-function-arglist 'make-temp-file t))))
1388 (and (>= (length def) 4)
cf5a5c38
MB
1389 (eq (nth 3 def) 'suffix)))))
1390 (defalias 'mm-make-temp-file 'make-temp-file)
01c52d31 1391 ;; Stolen (and modified for XEmacs) from Emacs 22.
cf5a5c38
MB
1392 (defun mm-make-temp-file (prefix &optional dir-flag suffix)
1393 "Create a temporary file.
1394The returned file name (created by appending some random characters at the end
1395of PREFIX, and expanding against `temporary-file-directory' if necessary),
1396is guaranteed to point to a newly created empty file.
1397You can then use `write-region' to write new data into the file.
1398
1399If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1400
1401If SUFFIX is non-nil, add that at the end of the file name."
1402 (let ((umask (default-file-modes))
1403 file)
1404 (unwind-protect
1405 (progn
1406 ;; Create temp files with strict access rights. It's easy to
1407 ;; loosen them later, whereas it's impossible to close the
1408 ;; time-window of loose permissions otherwise.
1409 (set-default-file-modes 448)
1410 (while (condition-case err
1411 (progn
1412 (setq file
1413 (make-temp-name
1414 (expand-file-name
1415 prefix
1416 (if (fboundp 'temp-directory)
1417 ;; XEmacs
1418 (temp-directory)
1419 temporary-file-directory))))
1420 (if suffix
1421 (setq file (concat file suffix)))
1422 (if dir-flag
1423 (make-directory file)
92edaeed
MB
1424 ;; NOTE: This is unsafe if Emacs 20
1425 ;; users and XEmacs users don't use
1426 ;; a secure temp directory.
1427 (gmm-write-region "" nil file nil 'silent
1428 nil 'excl))
cf5a5c38
MB
1429 nil)
1430 (file-already-exists t)
01c52d31
MB
1431 ;; The XEmacs version of `make-directory' issues
1432 ;; `file-error'.
1433 (file-error (or (and (featurep 'xemacs)
cf5a5c38
MB
1434 (file-exists-p file))
1435 (signal (car err) (cdr err)))))
1436 ;; the file was somehow created by someone else between
1437 ;; `make-temp-name' and `write-region', let's try again.
1438 nil)
1439 file)
1440 ;; Reset the umask.
1441 (set-default-file-modes umask)))))
1442
eecdcaf5
LMI
1443(defvar mm-image-load-path-cache nil)
1444
95fa1ff7 1445(defun mm-image-load-path (&optional package)
eecdcaf5
LMI
1446 (if (and mm-image-load-path-cache
1447 (equal load-path (car mm-image-load-path-cache)))
1448 (cdr mm-image-load-path-cache)
1449 (let (dir result)
1450 (dolist (path load-path)
1451 (when (and path
1452 (file-directory-p
1453 (setq dir (concat (file-name-directory
1454 (directory-file-name path))
1455 "etc/images/" (or package "gnus/")))))
1456 (push dir result)))
1457 (setq result (nreverse result)
1458 mm-image-load-path-cache (cons load-path result))
1459 result)))
95fa1ff7 1460
23f87bed
MB
1461;; Fixme: This doesn't look useful where it's used.
1462(if (fboundp 'detect-coding-region)
1463 (defun mm-detect-coding-region (start end)
1464 "Like `detect-coding-region' except returning the best one."
1465 (let ((coding-systems
9d9b0de9 1466 (detect-coding-region start end)))
23f87bed
MB
1467 (or (car-safe coding-systems)
1468 coding-systems)))
1469 (defun mm-detect-coding-region (start end)
1470 (let ((point (point)))
1471 (goto-char start)
1472 (skip-chars-forward "\0-\177" end)
1473 (prog1
1474 (if (eq (point) end) 'ascii (mm-guess-charset))
1475 (goto-char point)))))
1476
b5000590
GM
1477(declare-function mm-detect-coding-region "mm-util" (start end))
1478
23f87bed
MB
1479(if (fboundp 'coding-system-get)
1480 (defun mm-detect-mime-charset-region (start end)
1481 "Detect MIME charset of the text in the region between START and END."
1482 (let ((cs (mm-detect-coding-region start end)))
bd29ba20
RS
1483 (or (coding-system-get cs :mime-charset)
1484 (coding-system-get cs 'mime-charset))))
23f87bed
MB
1485 (defun mm-detect-mime-charset-region (start end)
1486 "Detect MIME charset of the text in the region between START and END."
1487 (let ((cs (mm-detect-coding-region start end)))
1488 cs)))
1489
01c52d31
MB
1490(eval-when-compile
1491 (unless (fboundp 'coding-system-to-mime-charset)
1492 (defalias 'coding-system-to-mime-charset 'ignore)))
1493
1494(defun mm-coding-system-to-mime-charset (coding-system)
1495 "Return the MIME charset corresponding to CODING-SYSTEM.
1496To make this function work with XEmacs, the APEL package is required."
1497 (when coding-system
1498 (or (and (fboundp 'coding-system-get)
1499 (or (coding-system-get coding-system :mime-charset)
1500 (coding-system-get coding-system 'mime-charset)))
1501 (and (featurep 'xemacs)
1502 (or (and (fboundp 'coding-system-to-mime-charset)
1503 (not (eq (symbol-function 'coding-system-to-mime-charset)
1504 'ignore)))
1505 (and (condition-case nil
1506 (require 'mcharset)
1507 (error nil))
1508 (fboundp 'coding-system-to-mime-charset)))
1509 (coding-system-to-mime-charset coding-system)))))
1510
c7641e3c
GM
1511(defvar jka-compr-acceptable-retval-list)
1512(declare-function jka-compr-make-temp-name "jka-compr" (&optional local))
01c52d31
MB
1513
1514(defun mm-decompress-buffer (filename &optional inplace force)
1515 "Decompress buffer's contents, depending on jka-compr.
1516Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
1517agrees with `jka-compr-compression-info-list', decompression is done.
1518Signal an error if FORCE is neither nil nor t and compressed data are
1519not decompressed because `auto-compression-mode' is disabled.
1520If INPLACE is nil, return decompressed data or nil without modifying
1521the buffer. Otherwise, replace the buffer's contents with the
1522decompressed data. The buffer's multibyteness must be turned off."
1523 (when (and filename
1524 (if force
1525 (prog1 t (require 'jka-compr))
1526 (and (fboundp 'jka-compr-installed-p)
1527 (jka-compr-installed-p))))
1528 (let ((info (jka-compr-get-compression-info filename)))
1529 (when info
1530 (unless (or (memq force (list nil t))
1531 (jka-compr-installed-p))
1532 (error ""))
1533 (let ((prog (jka-compr-info-uncompress-program info))
1534 (args (jka-compr-info-uncompress-args info))
1535 (msg (format "%s %s..."
1536 (jka-compr-info-uncompress-message info)
1537 filename))
1538 (err-file (jka-compr-make-temp-name))
1539 (cur (current-buffer))
1540 (coding-system-for-read mm-binary-coding-system)
1541 (coding-system-for-write mm-binary-coding-system)
1542 retval err-msg)
1543 (message "%s" msg)
1544 (mm-with-unibyte-buffer
1545 (insert-buffer-substring cur)
1546 (condition-case err
1547 (progn
1548 (unless (memq (apply 'call-process-region
1549 (point-min) (point-max)
1550 prog t (list t err-file) nil args)
1551 jka-compr-acceptable-retval-list)
1552 (erase-buffer)
4def29e7
KY
1553 (insert (mapconcat 'identity
1554 (split-string
1555 (prog2
1556 (insert-file-contents err-file)
1557 (buffer-string)
1558 (erase-buffer)) t)
1559 " ")
01c52d31
MB
1560 "\n")
1561 (setq err-msg
1562 (format "Error while executing \"%s %s < %s\""
1563 prog (mapconcat 'identity args " ")
1564 filename)))
1565 (setq retval (buffer-string)))
1566 (error
1567 (setq err-msg (error-message-string err)))))
1568 (when (file-exists-p err-file)
61a9da25 1569 (ignore-errors (delete-file err-file)))
01c52d31
MB
1570 (when inplace
1571 (unless err-msg
1572 (delete-region (point-min) (point-max))
1573 (insert retval))
1574 (setq retval nil))
1575 (message "%s" (or err-msg (concat msg "done")))
1576 retval)))))
1577
1578(eval-when-compile
1579 (unless (fboundp 'coding-system-name)
1580 (defalias 'coding-system-name 'ignore))
1581 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1582 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1583 (unless (fboundp 'find-operation-coding-system)
1584 (defalias 'find-operation-coding-system 'ignore)))
1585
1586(defun mm-find-buffer-file-coding-system (&optional filename)
1587 "Find coding system used to decode the contents of the current buffer.
1588This function looks for the coding system magic cookie or examines the
1589coding system specified by `file-coding-system-alist' being associated
1590with FILENAME which defaults to `buffer-file-name'. Data compressed by
1591gzip, bzip2, etc. are allowed."
1592 (unless filename
1593 (setq filename buffer-file-name))
1594 (save-excursion
89b163db 1595 (let ((decomp (unless ;; Not worth it to examine charset of tar files.
01c52d31
MB
1596 (and filename
1597 (string-match
1598 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1599 filename))
1600 (mm-decompress-buffer filename nil t))))
1601 (when decomp
bd486b03
SM
1602 (set-buffer (generate-new-buffer " *temp*"))
1603 (mm-disable-multibyte)
01c52d31
MB
1604 (insert decomp)
1605 (setq filename (file-name-sans-extension filename)))
1606 (goto-char (point-min))
8dabbfd6 1607 (unwind-protect
01c52d31
MB
1608 (cond
1609 ((boundp 'set-auto-coding-function) ;; Emacs
1610 (if filename
1611 (or (funcall (symbol-value 'set-auto-coding-function)
1612 filename (- (point-max) (point-min)))
1613 (car (find-operation-coding-system 'insert-file-contents
1614 filename)))
1615 (let (auto-coding-alist)
1616 (condition-case nil
1617 (funcall (symbol-value 'set-auto-coding-function)
1618 nil (- (point-max) (point-min)))
1619 (error nil)))))
9efa445f 1620 ((and (featurep 'xemacs) (featurep 'file-coding)) ;; XEmacs
01c52d31
MB
1621 (let ((case-fold-search t)
1622 (end (point-at-eol))
1623 codesys start)
1624 (or
1625 (and (re-search-forward "-\\*-+[\t ]*" end t)
1626 (progn
1627 (setq start (match-end 0))
1628 (re-search-forward "[\t ]*-+\\*-" end t))
1629 (progn
1630 (setq end (match-beginning 0))
1631 (goto-char start)
1632 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1633 (re-search-forward
1634 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1635 end t)))
1636 (find-coding-system (setq codesys
1637 (intern (match-string 1))))
1638 codesys)
1639 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1640 nil t)
1641 (progn
1642 (setq start (match-end 0))
1643 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1644 (progn
1645 (setq end (match-beginning 0))
1646 (goto-char start)
1647 (re-search-forward
1648 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1649 end t))
1650 (find-coding-system (setq codesys
1651 (intern (match-string 1))))
1652 codesys)
1653 (and (progn
1654 (goto-char (point-min))
1655 (setq case-fold-search nil)
1656 (re-search-forward "^;;;coding system: "
1657 ;;(+ (point-min) 3000) t))
1658 nil t))
1659 (looking-at "[^\t\n\r ]+")
1660 (find-coding-system
1661 (setq codesys (intern (match-string 0))))
1662 codesys)
1663 (and filename
1664 (setq codesys
1665 (find-file-coding-system-for-read-from-filename
1666 filename))
1667 (coding-system-name (coding-system-base codesys)))))))
1668 (when decomp
1669 (kill-buffer (current-buffer)))))))
3efe5554 1670
c113de23
GM
1671(provide 'mm-util)
1672
1673;;; mm-util.el ends here