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