]> code.delx.au - gnu-emacs/blob - lisp/gnus/mm-util.el
Switch license to GPLv3 or later.
[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 )
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 (big5 chinese-big5-1 chinese-big5-2)
478 (tibetan tibetan)
479 (thai-tis620 thai-tis620)
480 (windows-1251 cyrillic-iso8859-5)
481 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
482 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
483 latin-jisx0201 japanese-jisx0208-1978
484 chinese-gb2312 japanese-jisx0208
485 korean-ksc5601 japanese-jisx0212)
486 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
487 latin-jisx0201 japanese-jisx0208-1978
488 chinese-gb2312 japanese-jisx0208
489 korean-ksc5601 japanese-jisx0212
490 chinese-cns11643-1 chinese-cns11643-2)
491 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
492 cyrillic-iso8859-5 greek-iso8859-7
493 latin-jisx0201 japanese-jisx0208-1978
494 chinese-gb2312 japanese-jisx0208
495 korean-ksc5601 japanese-jisx0212
496 chinese-cns11643-1 chinese-cns11643-2
497 chinese-cns11643-3 chinese-cns11643-4
498 chinese-cns11643-5 chinese-cns11643-6
499 chinese-cns11643-7)
500 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
501 japanese-jisx0213-1 japanese-jisx0213-2)
502 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
503 ,(cond ((fboundp 'unicode-precedence-list)
504 (cons 'utf-8 (delq 'ascii (mapcar 'charset-name
505 (unicode-precedence-list)))))
506 ((or (not (fboundp 'charsetp)) ;; non-Mule case
507 (charsetp 'unicode-a)
508 (not (mm-coding-system-p 'mule-utf-8)))
509 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
510 (t ;; If we have utf-8 we're in Mule 5+.
511 (append '(utf-8)
512 (delete 'ascii
513 (coding-system-get 'mule-utf-8 'safe-charsets))))))
514 "Alist of MIME-charset/MULE-charsets.")
515
516 (defun mm-enrich-utf-8-by-mule-ucs ()
517 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
518 This function will run when the `un-define' module is loaded under
519 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
520 with Mule charsets. It is completely useless for Emacs."
521 (when (boundp 'unicode-basic-translation-charset-order-list)
522 (condition-case nil
523 (let ((val (delq
524 'ascii
525 (copy-sequence
526 (symbol-value
527 'unicode-basic-translation-charset-order-list))))
528 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
529 (if elem
530 (setcdr elem val)
531 (setq mm-mime-mule-charset-alist
532 (nconc mm-mime-mule-charset-alist
533 (list (cons 'utf-8 val))))))
534 (error))))
535
536 ;; Correct by construction, but should be unnecessary for Emacs:
537 (if (featurep 'xemacs)
538 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
539 (when (and (fboundp 'coding-system-list)
540 (fboundp 'sort-coding-systems))
541 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
542 cs mime mule alist)
543 (while css
544 (setq cs (pop css)
545 mime (or (coding-system-get cs :mime-charset) ; Emacs 23 (unicode)
546 (coding-system-get cs 'mime-charset)))
547 (when (and mime
548 (not (eq t (setq mule
549 (coding-system-get cs 'safe-charsets))))
550 (not (assq mime alist)))
551 (push (cons mime (delq 'ascii mule)) alist)))
552 (setq mm-mime-mule-charset-alist (nreverse alist)))))
553
554 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
555 "A list of special charsets.
556 Valid elements include:
557 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
558 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
559 )
560
561 (defvar mm-iso-8859-15-compatible
562 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
563 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
564 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
565
566 (defvar mm-iso-8859-x-to-15-table
567 (and (fboundp 'coding-system-p)
568 (mm-coding-system-p 'iso-8859-15)
569 (mapcar
570 (lambda (cs)
571 (if (mm-coding-system-p (car cs))
572 (let ((c (string-to-char
573 (decode-coding-string "\341" (car cs)))))
574 (cons (char-charset c)
575 (cons
576 (- (string-to-char
577 (decode-coding-string "\341" 'iso-8859-15)) c)
578 (string-to-list (decode-coding-string (car (cdr cs))
579 (car cs))))))
580 '(gnus-charset 0)))
581 mm-iso-8859-15-compatible))
582 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
583
584 (defcustom mm-coding-system-priorities
585 (if (boundp 'current-language-environment)
586 (let ((lang (symbol-value 'current-language-environment)))
587 (cond ((string= lang "Japanese")
588 ;; Japanese users prefer iso-2022-jp to euc-japan or
589 ;; shift_jis, however iso-8859-1 should be used when
590 ;; there are only ASCII text and Latin-1 characters.
591 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
592 "Preferred coding systems for encoding outgoing messages.
593
594 More than one suitable coding system may be found for some text.
595 By default, the coding system with the highest priority is used
596 to encode outgoing messages (see `sort-coding-systems'). If this
597 variable is set, it overrides the default priority."
598 :version "21.2"
599 :type '(repeat (symbol :tag "Coding system"))
600 :group 'mime)
601
602 ;; ??
603 (defvar mm-use-find-coding-systems-region
604 (fboundp 'find-coding-systems-region)
605 "Use `find-coding-systems-region' to find proper coding systems.
606
607 Setting it to nil is useful on Emacsen supporting Unicode if sending
608 mail with multiple parts is preferred to sending a Unicode one.")
609
610 ;;; Internal variables:
611
612 ;;; Functions:
613
614 (defun mm-mule-charset-to-mime-charset (charset)
615 "Return the MIME charset corresponding to the given Mule CHARSET."
616 (if (and (fboundp 'find-coding-systems-for-charsets)
617 (fboundp 'sort-coding-systems))
618 (let ((css (sort (sort-coding-systems
619 (find-coding-systems-for-charsets (list charset)))
620 'mm-sort-coding-systems-predicate))
621 cs mime)
622 (while (and (not mime)
623 css)
624 (when (setq cs (pop css))
625 (setq mime (or (coding-system-get cs :mime-charset)
626 (coding-system-get cs 'mime-charset)))))
627 mime)
628 (let ((alist (mapcar (lambda (cs)
629 (assq cs mm-mime-mule-charset-alist))
630 (sort (mapcar 'car mm-mime-mule-charset-alist)
631 'mm-sort-coding-systems-predicate)))
632 out)
633 (while alist
634 (when (memq charset (cdar alist))
635 (setq out (caar alist)
636 alist nil))
637 (pop alist))
638 out)))
639
640 (defun mm-charset-to-coding-system (charset &optional lbt
641 allow-override)
642 "Return coding-system corresponding to CHARSET.
643 CHARSET is a symbol naming a MIME charset.
644 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
645 used as the line break code type of the coding system.
646
647 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
648 map undesired charset names to their replacement. This should
649 only be used for decoding, not for encoding."
650 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
651 (when (stringp charset)
652 (setq charset (intern (downcase charset))))
653 (when lbt
654 (setq charset (intern (format "%s-%s" charset lbt))))
655 (cond
656 ((null charset)
657 charset)
658 ;; Running in a non-MULE environment.
659 ((or (null (mm-get-coding-system-list))
660 (not (fboundp 'coding-system-get)))
661 charset)
662 ;; Check override list quite early. Should only used for decoding, not for
663 ;; encoding!
664 ((and allow-override
665 (let ((cs (cdr (assq charset mm-charset-override-alist))))
666 (and cs (mm-coding-system-p cs) cs))))
667 ;; ascii
668 ((eq charset 'us-ascii)
669 'ascii)
670 ;; Check to see whether we can handle this charset. (This depends
671 ;; on there being some coding system matching each `mime-charset'
672 ;; property defined, as there should be.)
673 ((and (mm-coding-system-p charset)
674 ;;; Doing this would potentially weed out incorrect charsets.
675 ;;; charset
676 ;;; (eq charset (coding-system-get charset 'mime-charset))
677 )
678 charset)
679 ;; Eval expressions from `mm-charset-eval-alist'
680 ((let* ((el (assq charset mm-charset-eval-alist))
681 (cs (car el))
682 (form (cdr el)))
683 (and cs
684 form
685 (prog2
686 ;; Avoid errors...
687 (condition-case nil (eval form) (error nil))
688 ;; (message "Failed to eval `%s'" form))
689 (mm-coding-system-p cs)
690 (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
691 cs)))
692 ;; Translate invalid charsets.
693 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
694 (and cs
695 (mm-coding-system-p cs)
696 ;; (message
697 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
698 ;; cs charset)
699 cs)))
700 ;; Last resort: search the coding system list for entries which
701 ;; have the right mime-charset in case the canonical name isn't
702 ;; defined (though it should be).
703 ((let (cs)
704 ;; mm-get-coding-system-list returns a list of cs without lbt.
705 ;; Do we need -lbt?
706 (dolist (c (mm-get-coding-system-list))
707 (if (and (null cs)
708 (eq charset (or (coding-system-get c :mime-charset)
709 (coding-system-get c 'mime-charset))))
710 (setq cs c)))
711 (unless cs
712 ;; Warn the user about unknown charset:
713 (if (fboundp 'gnus-message)
714 (gnus-message 7 "Unknown charset: %s" charset)
715 (message "Unknown charset: %s" charset)))
716 cs))))
717
718 (defsubst mm-replace-chars-in-string (string from to)
719 (mm-subst-char-in-string from to string))
720
721 (eval-and-compile
722 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
723 (boundp 'default-enable-multibyte-characters)
724 default-enable-multibyte-characters
725 (fboundp 'set-buffer-multibyte))
726 "True in Emacs with Mule.")
727
728 (if mm-emacs-mule
729 (defun mm-enable-multibyte ()
730 "Set the multibyte flag of the current buffer.
731 Only do this if the default value of `enable-multibyte-characters' is
732 non-nil. This is a no-op in XEmacs."
733 (set-buffer-multibyte 'to))
734 (defalias 'mm-enable-multibyte 'ignore))
735
736 (if mm-emacs-mule
737 (defun mm-disable-multibyte ()
738 "Unset the multibyte flag of in the current buffer.
739 This is a no-op in XEmacs."
740 (set-buffer-multibyte nil))
741 (defalias 'mm-disable-multibyte 'ignore)))
742
743 (defun mm-preferred-coding-system (charset)
744 ;; A typo in some Emacs versions.
745 (or (get-charset-property charset 'preferred-coding-system)
746 (get-charset-property charset 'prefered-coding-system)))
747
748 ;; Mule charsets shouldn't be used.
749 (defsubst mm-guess-charset ()
750 "Guess Mule charset from the language environment."
751 (or
752 mail-parse-mule-charset ;; cached mule-charset
753 (progn
754 (setq mail-parse-mule-charset
755 (and (boundp 'current-language-environment)
756 (car (last
757 (assq 'charset
758 (assoc current-language-environment
759 language-info-alist))))))
760 (if (or (not mail-parse-mule-charset)
761 (eq mail-parse-mule-charset 'ascii))
762 (setq mail-parse-mule-charset
763 (or (car (last (assq mail-parse-charset
764 mm-mime-mule-charset-alist)))
765 ;; default
766 'latin-iso8859-1)))
767 mail-parse-mule-charset)))
768
769 (defun mm-charset-after (&optional pos)
770 "Return charset of a character in current buffer at position POS.
771 If POS is nil, it defauls to the current point.
772 If POS is out of range, the value is nil.
773 If the charset is `composition', return the actual one."
774 (let ((char (char-after pos)) charset)
775 (if (< (mm-char-int char) 128)
776 (setq charset 'ascii)
777 ;; charset-after is fake in some Emacsen.
778 (setq charset (and (fboundp 'char-charset) (char-charset char)))
779 (if (eq charset 'composition) ; Mule 4
780 (let ((p (or pos (point))))
781 (cadr (find-charset-region p (1+ p))))
782 (if (and charset (not (memq charset '(ascii eight-bit-control
783 eight-bit-graphic))))
784 charset
785 (mm-guess-charset))))))
786
787 (defun mm-mime-charset (charset)
788 "Return the MIME charset corresponding to the given Mule CHARSET."
789 (if (eq charset 'unknown)
790 (error "The message contains non-printable characters, please use attachment"))
791 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
792 ;; This exists in Emacs 20.
793 (or
794 (and (mm-preferred-coding-system charset)
795 (or (coding-system-get
796 (mm-preferred-coding-system charset) :mime-charset)
797 (coding-system-get
798 (mm-preferred-coding-system charset) 'mime-charset)))
799 (and (eq charset 'ascii)
800 'us-ascii)
801 (mm-preferred-coding-system charset)
802 (mm-mule-charset-to-mime-charset charset))
803 ;; This is for XEmacs.
804 (mm-mule-charset-to-mime-charset charset)))
805
806 (if (fboundp 'delete-dups)
807 (defalias 'mm-delete-duplicates 'delete-dups)
808 (defun mm-delete-duplicates (list)
809 "Destructively remove `equal' duplicates from LIST.
810 Store the result in LIST and return it. LIST must be a proper list.
811 Of several `equal' occurrences of an element in LIST, the first
812 one is kept.
813
814 This is a compatibility function for Emacsen without `delete-dups'."
815 ;; Code from `subr.el' in Emacs 22:
816 (let ((tail list))
817 (while tail
818 (setcdr tail (delete (car tail) (cdr tail)))
819 (setq tail (cdr tail))))
820 list))
821
822 ;; Fixme: This is used in places when it should be testing the
823 ;; default multibyteness. See mm-default-multibyte-p.
824 (eval-and-compile
825 (if (and (not (featurep 'xemacs))
826 (boundp 'enable-multibyte-characters))
827 (defun mm-multibyte-p ()
828 "Non-nil if multibyte is enabled in the current buffer."
829 enable-multibyte-characters)
830 (defun mm-multibyte-p () (featurep 'mule))))
831
832 (defun mm-default-multibyte-p ()
833 "Return non-nil if the session is multibyte.
834 This affects whether coding conversion should be attempted generally."
835 (if (featurep 'mule)
836 (if (boundp 'default-enable-multibyte-characters)
837 default-enable-multibyte-characters
838 t)))
839
840 (defun mm-iso-8859-x-to-15-region (&optional b e)
841 (if (fboundp 'char-charset)
842 (let (charset item c inconvertible)
843 (save-restriction
844 (if e (narrow-to-region b e))
845 (goto-char (point-min))
846 (skip-chars-forward "\0-\177")
847 (while (not (eobp))
848 (cond
849 ((not (setq item (assq (char-charset (setq c (char-after)))
850 mm-iso-8859-x-to-15-table)))
851 (forward-char))
852 ((memq c (cdr (cdr item)))
853 (setq inconvertible t)
854 (forward-char))
855 (t
856 (insert-before-markers (prog1 (+ c (car (cdr item)))
857 (delete-char 1)))))
858 (skip-chars-forward "\0-\177")))
859 (not inconvertible))))
860
861 (defun mm-sort-coding-systems-predicate (a b)
862 (let ((priorities
863 (mapcar (lambda (cs)
864 ;; Note: invalid entries are dropped silently
865 (and (setq cs (mm-coding-system-p cs))
866 (coding-system-base cs)))
867 mm-coding-system-priorities)))
868 (and (setq a (mm-coding-system-p a))
869 (if (setq b (mm-coding-system-p b))
870 (> (length (memq (coding-system-base a) priorities))
871 (length (memq (coding-system-base b) priorities)))
872 t))))
873
874 (eval-when-compile
875 (autoload 'latin-unity-massage-name "latin-unity")
876 (autoload 'latin-unity-maybe-remap "latin-unity")
877 (autoload 'latin-unity-representations-feasible-region "latin-unity")
878 (autoload 'latin-unity-representations-present-region "latin-unity")
879 (defvar latin-unity-coding-systems)
880 (defvar latin-unity-ucs-list))
881
882 (defun mm-xemacs-find-mime-charset-1 (begin end)
883 "Determine which MIME charset to use to send region as message.
884 This uses the XEmacs-specific latin-unity package to better handle the
885 case where identical characters from diverse ISO-8859-? character sets
886 can be encoded using a single one of the corresponding coding systems.
887
888 It treats `mm-coding-system-priorities' as the list of preferred
889 coding systems; a useful example setting for this list in Western
890 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
891 to the very standard Latin 1 coding system, and only move to coding
892 systems that are less supported as is necessary to encode the
893 characters that exist in the buffer.
894
895 Latin Unity doesn't know about those non-ASCII Roman characters that
896 are available in various East Asian character sets. As such, its
897 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
898 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
899 But this is very much a corner case, so don't worry about it."
900 (let ((systems mm-coding-system-priorities) csets psets curset)
901
902 ;; Load the Latin Unity library, if available.
903 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
904 (ignore-errors (require 'latin-unity)))
905
906 ;; Now, can we use it?
907 (if (featurep 'latin-unity)
908 (progn
909 (setq csets (latin-unity-representations-feasible-region begin end)
910 psets (latin-unity-representations-present-region begin end))
911
912 (catch 'done
913
914 ;; Pass back the first coding system in the preferred list
915 ;; that can encode the whole region.
916 (dolist (curset systems)
917 (setq curset (latin-unity-massage-name 'buffer-default curset))
918
919 ;; If the coding system is a universal coding system, then
920 ;; it can certainly encode all the characters in the region.
921 (if (memq curset latin-unity-ucs-list)
922 (throw 'done (list curset)))
923
924 ;; If a coding system isn't universal, and isn't in
925 ;; the list that latin unity knows about, we can't
926 ;; decide whether to use it here. Leave that until later
927 ;; in `mm-find-mime-charset-region' function, whence we
928 ;; have been called.
929 (unless (memq curset latin-unity-coding-systems)
930 (throw 'done nil))
931
932 ;; Right, we know about this coding system, and it may
933 ;; conceivably be able to encode all the characters in
934 ;; the region.
935 (if (latin-unity-maybe-remap begin end curset csets psets t)
936 (throw 'done (list curset))))
937
938 ;; Can't encode using anything from the
939 ;; `mm-coding-system-priorities' list.
940 ;; Leave `mm-find-mime-charset' to do most of the work.
941 nil))
942
943 ;; Right, latin unity isn't available; let `mm-find-charset-region'
944 ;; take its default action, which equally applies to GNU Emacs.
945 nil)))
946
947 (defmacro mm-xemacs-find-mime-charset (begin end)
948 (when (featurep 'xemacs)
949 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
950
951 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
952 "Return the MIME charsets needed to encode the region between B and E.
953 nil means ASCII, a single-element list represents an appropriate MIME
954 charset, and a longer list means no appropriate charset."
955 (let (charsets)
956 ;; The return possibilities of this function are a mess...
957 (or (and (mm-multibyte-p)
958 mm-use-find-coding-systems-region
959 ;; Find the mime-charset of the most preferred coding
960 ;; system that has one.
961 (let ((systems (find-coding-systems-region b e)))
962 (when mm-coding-system-priorities
963 (setq systems
964 (sort systems 'mm-sort-coding-systems-predicate)))
965 (setq systems (delq 'compound-text systems))
966 (unless (equal systems '(undecided))
967 (while systems
968 (let* ((head (pop systems))
969 (cs (or (coding-system-get head :mime-charset)
970 (coding-system-get head 'mime-charset))))
971 ;; The mime-charset (`x-ctext') of
972 ;; `compound-text' is not in the IANA list. We
973 ;; shouldn't normally use anything here with a
974 ;; mime-charset having an `x-' prefix.
975 ;; Fixme: Allow this to be overridden, since
976 ;; there is existing use of x-ctext.
977 ;; Also people apparently need the coding system
978 ;; `iso-2022-jp-3' (which Mule-UCS defines with
979 ;; mime-charset, though it's not valid).
980 (if (and cs
981 (not (string-match "^[Xx]-" (symbol-name cs)))
982 ;; UTF-16 of any variety is invalid for
983 ;; text parts and, unfortunately, has
984 ;; mime-charset defined both in Mule-UCS
985 ;; and versions of Emacs. (The name
986 ;; might be `mule-utf-16...' or
987 ;; `utf-16...'.)
988 (not (string-match "utf-16" (symbol-name cs))))
989 (setq systems nil
990 charsets (list cs))))))
991 charsets))
992 ;; If we're XEmacs, and some coding system is appropriate,
993 ;; mm-xemacs-find-mime-charset will return an appropriate list.
994 ;; Otherwise, we'll get nil, and the next setq will get invoked.
995 (setq charsets (mm-xemacs-find-mime-charset b e))
996
997 ;; We're not multibyte, or a single coding system won't cover it.
998 (setq charsets
999 (mm-delete-duplicates
1000 (mapcar 'mm-mime-charset
1001 (delq 'ascii
1002 (mm-find-charset-region b e))))))
1003 (if (and (> (length charsets) 1)
1004 (memq 'iso-8859-15 charsets)
1005 (memq 'iso-8859-15 hack-charsets)
1006 (save-excursion (mm-iso-8859-x-to-15-region b e)))
1007 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
1008 mm-iso-8859-15-compatible))
1009 (if (and (memq 'iso-2022-jp-2 charsets)
1010 (memq 'iso-2022-jp-2 hack-charsets))
1011 (setq charsets (delq 'iso-2022-jp charsets)))
1012 ;; Attempt to reduce the number of charsets if utf-8 is available.
1013 (if (and (featurep 'xemacs)
1014 (> (length charsets) 1)
1015 (mm-coding-system-p 'utf-8))
1016 (let ((mm-coding-system-priorities
1017 (cons 'utf-8 mm-coding-system-priorities)))
1018 (setq charsets
1019 (mm-delete-duplicates
1020 (mapcar 'mm-mime-charset
1021 (delq 'ascii
1022 (mm-find-charset-region b e)))))))
1023 charsets))
1024
1025 (defmacro mm-with-unibyte-buffer (&rest forms)
1026 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1027 Use unibyte mode for this."
1028 `(let (default-enable-multibyte-characters)
1029 (with-temp-buffer ,@forms)))
1030 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
1031 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
1032
1033 (defmacro mm-with-multibyte-buffer (&rest forms)
1034 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1035 Use multibyte mode for this."
1036 `(let ((default-enable-multibyte-characters t))
1037 (with-temp-buffer ,@forms)))
1038 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
1039 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
1040
1041 (defmacro mm-with-unibyte-current-buffer (&rest forms)
1042 "Evaluate FORMS with current buffer temporarily made unibyte.
1043 Also bind `default-enable-multibyte-characters' to nil.
1044 Equivalent to `progn' in XEmacs
1045
1046 NOTE: Use this macro with caution in multibyte buffers (it is not
1047 worth using this macro in unibyte buffers of course). Use of
1048 `(set-buffer-multibyte t)', which is run finally, is generally
1049 harmful since it is likely to modify existing data in the buffer.
1050 For instance, it converts \"\\300\\255\" into \"\\255\" in
1051 Emacs 23 (unicode)."
1052 (let ((multibyte (make-symbol "multibyte"))
1053 (buffer (make-symbol "buffer")))
1054 `(if mm-emacs-mule
1055 (let ((,multibyte enable-multibyte-characters)
1056 (,buffer (current-buffer)))
1057 (unwind-protect
1058 (let (default-enable-multibyte-characters)
1059 (set-buffer-multibyte nil)
1060 ,@forms)
1061 (set-buffer ,buffer)
1062 (set-buffer-multibyte ,multibyte)))
1063 (let (default-enable-multibyte-characters)
1064 ,@forms))))
1065 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
1066 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
1067
1068 (defmacro mm-with-unibyte (&rest forms)
1069 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
1070 `(let (default-enable-multibyte-characters)
1071 ,@forms))
1072 (put 'mm-with-unibyte 'lisp-indent-function 0)
1073 (put 'mm-with-unibyte 'edebug-form-spec '(body))
1074
1075 (defmacro mm-with-multibyte (&rest forms)
1076 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
1077 `(let ((default-enable-multibyte-characters t))
1078 ,@forms))
1079 (put 'mm-with-multibyte 'lisp-indent-function 0)
1080 (put 'mm-with-multibyte 'edebug-form-spec '(body))
1081
1082 (defun mm-find-charset-region (b e)
1083 "Return a list of Emacs charsets in the region B to E."
1084 (cond
1085 ((and (mm-multibyte-p)
1086 (fboundp 'find-charset-region))
1087 ;; Remove composition since the base charsets have been included.
1088 ;; Remove eight-bit-*, treat them as ascii.
1089 (let ((css (find-charset-region b e)))
1090 (mapcar (lambda (cs) (setq css (delq cs css)))
1091 '(composition eight-bit-control eight-bit-graphic
1092 control-1))
1093 css))
1094 (t
1095 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
1096 (save-excursion
1097 (save-restriction
1098 (narrow-to-region b e)
1099 (goto-char (point-min))
1100 (skip-chars-forward "\0-\177")
1101 (if (eobp)
1102 '(ascii)
1103 (let (charset)
1104 (setq charset
1105 (and (boundp 'current-language-environment)
1106 (car (last (assq 'charset
1107 (assoc current-language-environment
1108 language-info-alist))))))
1109 (if (eq charset 'ascii) (setq charset nil))
1110 (or charset
1111 (setq charset
1112 (car (last (assq mail-parse-charset
1113 mm-mime-mule-charset-alist)))))
1114 (list 'ascii (or charset 'latin-iso8859-1)))))))))
1115
1116 (if (fboundp 'shell-quote-argument)
1117 (defalias 'mm-quote-arg 'shell-quote-argument)
1118 (defun mm-quote-arg (arg)
1119 "Return a version of ARG that is safe to evaluate in a shell."
1120 (let ((pos 0) new-pos accum)
1121 ;; *** bug: we don't handle newline characters properly
1122 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
1123 (push (substring arg pos new-pos) accum)
1124 (push "\\" accum)
1125 (push (list (aref arg new-pos)) accum)
1126 (setq pos (1+ new-pos)))
1127 (if (= pos 0)
1128 arg
1129 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
1130
1131 (defun mm-auto-mode-alist ()
1132 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1133 (let ((alist auto-mode-alist)
1134 out)
1135 (while alist
1136 (when (listp (cdar alist))
1137 (push (car alist) out))
1138 (pop alist))
1139 (nreverse out)))
1140
1141 (defvar mm-inhibit-file-name-handlers
1142 '(jka-compr-handler image-file-handler)
1143 "A list of handlers doing (un)compression (etc) thingies.")
1144
1145 (defun mm-insert-file-contents (filename &optional visit beg end replace
1146 inhibit)
1147 "Like `insert-file-contents', but only reads in the file.
1148 A buffer may be modified in several ways after reading into the buffer due
1149 to advanced Emacs features, such as file-name-handlers, format decoding,
1150 `find-file-hooks', etc.
1151 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
1152 This function ensures that none of these modifications will take place."
1153 (let* ((format-alist nil)
1154 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
1155 (default-major-mode 'fundamental-mode)
1156 (enable-local-variables nil)
1157 (after-insert-file-functions nil)
1158 (enable-local-eval nil)
1159 (inhibit-file-name-operation (if inhibit
1160 'insert-file-contents
1161 inhibit-file-name-operation))
1162 (inhibit-file-name-handlers
1163 (if inhibit
1164 (append mm-inhibit-file-name-handlers
1165 inhibit-file-name-handlers)
1166 inhibit-file-name-handlers))
1167 (ffh (if (boundp 'find-file-hook)
1168 'find-file-hook
1169 'find-file-hooks))
1170 (val (symbol-value ffh)))
1171 (set ffh nil)
1172 (unwind-protect
1173 (insert-file-contents filename visit beg end replace)
1174 (set ffh val))))
1175
1176 (defun mm-append-to-file (start end filename &optional codesys inhibit)
1177 "Append the contents of the region to the end of file FILENAME.
1178 When called from a function, expects three arguments,
1179 START, END and FILENAME. START and END are buffer positions
1180 saying what text to write.
1181 Optional fourth argument specifies the coding system to use when
1182 encoding the file.
1183 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1184 (let ((coding-system-for-write
1185 (or codesys mm-text-coding-system-for-write
1186 mm-text-coding-system))
1187 (inhibit-file-name-operation (if inhibit
1188 'append-to-file
1189 inhibit-file-name-operation))
1190 (inhibit-file-name-handlers
1191 (if inhibit
1192 (append mm-inhibit-file-name-handlers
1193 inhibit-file-name-handlers)
1194 inhibit-file-name-handlers)))
1195 (write-region start end filename t 'no-message)
1196 (message "Appended to %s" filename)))
1197
1198 (defun mm-write-region (start end filename &optional append visit lockname
1199 coding-system inhibit)
1200
1201 "Like `write-region'.
1202 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1203 (let ((coding-system-for-write
1204 (or coding-system mm-text-coding-system-for-write
1205 mm-text-coding-system))
1206 (inhibit-file-name-operation (if inhibit
1207 'write-region
1208 inhibit-file-name-operation))
1209 (inhibit-file-name-handlers
1210 (if inhibit
1211 (append mm-inhibit-file-name-handlers
1212 inhibit-file-name-handlers)
1213 inhibit-file-name-handlers)))
1214 (write-region start end filename append visit lockname)))
1215
1216 ;; It is not a MIME function, but some MIME functions use it.
1217 (if (and (fboundp 'make-temp-file)
1218 (ignore-errors
1219 (let ((def (symbol-function 'make-temp-file)))
1220 (and (byte-code-function-p def)
1221 (setq def (if (fboundp 'compiled-function-arglist)
1222 ;; XEmacs
1223 (eval (list 'compiled-function-arglist def))
1224 (aref def 0)))
1225 (>= (length def) 4)
1226 (eq (nth 3 def) 'suffix)))))
1227 (defalias 'mm-make-temp-file 'make-temp-file)
1228 ;; Stolen (and modified for Emacs 20 and XEmacs) from Emacs 22.
1229 (defun mm-make-temp-file (prefix &optional dir-flag suffix)
1230 "Create a temporary file.
1231 The returned file name (created by appending some random characters at the end
1232 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1233 is guaranteed to point to a newly created empty file.
1234 You can then use `write-region' to write new data into the file.
1235
1236 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1237
1238 If SUFFIX is non-nil, add that at the end of the file name."
1239 (let ((umask (default-file-modes))
1240 file)
1241 (unwind-protect
1242 (progn
1243 ;; Create temp files with strict access rights. It's easy to
1244 ;; loosen them later, whereas it's impossible to close the
1245 ;; time-window of loose permissions otherwise.
1246 (set-default-file-modes 448)
1247 (while (condition-case err
1248 (progn
1249 (setq file
1250 (make-temp-name
1251 (expand-file-name
1252 prefix
1253 (if (fboundp 'temp-directory)
1254 ;; XEmacs
1255 (temp-directory)
1256 temporary-file-directory))))
1257 (if suffix
1258 (setq file (concat file suffix)))
1259 (if dir-flag
1260 (make-directory file)
1261 ;; NOTE: This is unsafe if Emacs 20
1262 ;; users and XEmacs users don't use
1263 ;; a secure temp directory.
1264 (gmm-write-region "" nil file nil 'silent
1265 nil 'excl))
1266 nil)
1267 (file-already-exists t)
1268 ;; The Emacs 20 and XEmacs versions of
1269 ;; `make-directory' issue `file-error'.
1270 (file-error (or (and (or (featurep 'xemacs)
1271 (= emacs-major-version 20))
1272 (file-exists-p file))
1273 (signal (car err) (cdr err)))))
1274 ;; the file was somehow created by someone else between
1275 ;; `make-temp-name' and `write-region', let's try again.
1276 nil)
1277 file)
1278 ;; Reset the umask.
1279 (set-default-file-modes umask)))))
1280
1281 (defun mm-image-load-path (&optional package)
1282 (let (dir result)
1283 (dolist (path load-path (nreverse result))
1284 (when (and path
1285 (file-directory-p
1286 (setq dir (concat (file-name-directory
1287 (directory-file-name path))
1288 "etc/images/" (or package "gnus/")))))
1289 (push dir result))
1290 (push path result))))
1291
1292 ;; Fixme: This doesn't look useful where it's used.
1293 (if (fboundp 'detect-coding-region)
1294 (defun mm-detect-coding-region (start end)
1295 "Like `detect-coding-region' except returning the best one."
1296 (let ((coding-systems
1297 (detect-coding-region start end)))
1298 (or (car-safe coding-systems)
1299 coding-systems)))
1300 (defun mm-detect-coding-region (start end)
1301 (let ((point (point)))
1302 (goto-char start)
1303 (skip-chars-forward "\0-\177" end)
1304 (prog1
1305 (if (eq (point) end) 'ascii (mm-guess-charset))
1306 (goto-char point)))))
1307
1308 (if (fboundp 'coding-system-get)
1309 (defun mm-detect-mime-charset-region (start end)
1310 "Detect MIME charset of the text in the region between START and END."
1311 (let ((cs (mm-detect-coding-region start end)))
1312 (or (coding-system-get cs :mime-charset)
1313 (coding-system-get cs 'mime-charset))))
1314 (defun mm-detect-mime-charset-region (start end)
1315 "Detect MIME charset of the text in the region between START and END."
1316 (let ((cs (mm-detect-coding-region start end)))
1317 cs)))
1318
1319
1320 (provide 'mm-util)
1321
1322 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1323 ;;; mm-util.el ends here