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