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