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