]> code.delx.au - gnu-emacs/blob - lisp/gnus/mm-util.el
Update copyright notices of all files in the gnus directory.
[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 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 '((decode-coding-string . (lambda (s a) s))
40 (encode-coding-string . (lambda (s a) s))
41 (encode-coding-region . ignore)
42 (coding-system-list . ignore)
43 (decode-coding-region . ignore)
44 (char-int . identity)
45 (coding-system-equal . equal)
46 (annotationp . ignore)
47 (set-buffer-file-coding-system . ignore)
48 (make-char
49 . (lambda (charset int)
50 (int-to-char int)))
51 (read-charset
52 . (lambda (prompt)
53 "Return a charset."
54 (intern
55 (completing-read
56 prompt
57 (mapcar (lambda (e) (list (symbol-name (car e))))
58 mm-mime-mule-charset-alist)
59 nil t))))
60 (subst-char-in-string
61 . (lambda (from to string &optional inplace)
62 ;; stolen (and renamed) from nnheader.el
63 "Replace characters in STRING from FROM to TO.
64 Unless optional argument INPLACE is non-nil, return a new string."
65 (let ((string (if inplace string (copy-sequence string)))
66 (len (length string))
67 (idx 0))
68 ;; Replace all occurrences of FROM with TO.
69 (while (< idx len)
70 (when (= (aref string idx) from)
71 (aset string idx to))
72 (setq idx (1+ idx)))
73 string)))
74 (string-as-unibyte . identity)
75 (string-make-unibyte . identity)
76 ;; string-as-multibyte often doesn't really do what you think it does.
77 ;; Example:
78 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
79 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
80 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
81 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
82 ;; but
83 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
84 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
85 ;; Better use string-to-multibyte or encode-coding-string.
86 ;; If you really need string-as-multibyte somewhere it's usually
87 ;; because you're using the internal emacs-mule representation (maybe
88 ;; because you're using string-as-unibyte somewhere), which is
89 ;; generally a problem in itself.
90 ;; Here is an approximate equivalence table to help think about it:
91 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
92 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
93 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
94 (string-as-multibyte . identity)
95 (string-to-multibyte
96 . (lambda (string)
97 "Return a multibyte string with the same individual chars as string."
98 (mapconcat
99 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
100 string "")))
101 (multibyte-string-p . ignore)
102 ;; It is not a MIME function, but some MIME functions use it.
103 (make-temp-file . (lambda (prefix &optional dir-flag)
104 (let ((file (expand-file-name
105 (make-temp-name prefix)
106 (if (fboundp 'temp-directory)
107 (temp-directory)
108 temporary-file-directory))))
109 (if dir-flag
110 (make-directory file))
111 file)))
112 (insert-byte . insert-char)
113 (multibyte-char-to-unibyte . identity))))
114
115 (eval-and-compile
116 (cond
117 ((fboundp 'replace-in-string)
118 (defalias 'mm-replace-in-string 'replace-in-string))
119 ((fboundp 'replace-regexp-in-string)
120 (defun mm-replace-in-string (string regexp newtext &optional literal)
121 "Replace all matches for REGEXP with NEWTEXT in STRING.
122 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
123 string containing the replacements.
124
125 This is a compatibility function for different Emacsen."
126 (replace-regexp-in-string regexp newtext string nil literal)))
127 (t
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 (let ((start 0) tail)
135 (while (string-match regexp string start)
136 (setq tail (- (length string) (match-end 0)))
137 (setq string (replace-match newtext nil literal string))
138 (setq start (- (length string) tail))))
139 string))))
140
141 (eval-and-compile
142 (defalias 'mm-char-or-char-int-p
143 (cond
144 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
145 ((fboundp 'char-valid-p) 'char-valid-p)
146 (t 'identity))))
147
148 ;; Fixme: This seems always to be used to read a MIME charset, so it
149 ;; should be re-named and fixed (in Emacs) to offer completion only on
150 ;; proper charset names (base coding systems which have a
151 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
152 ;; test with
153 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
154 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
155 ;; Actually, there should be an `mm-coding-system-mime-charset'.
156 (eval-and-compile
157 (defalias 'mm-read-coding-system
158 (cond
159 ((fboundp 'read-coding-system)
160 (if (and (featurep 'xemacs)
161 (<= (string-to-number emacs-version) 21.1))
162 (lambda (prompt &optional default-coding-system)
163 (read-coding-system prompt))
164 'read-coding-system))
165 (t (lambda (prompt &optional default-coding-system)
166 "Prompt the user for a coding system."
167 (completing-read
168 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
169 mm-mime-mule-charset-alist)))))))
170
171 (defvar mm-coding-system-list nil)
172 (defun mm-get-coding-system-list ()
173 "Get the coding system list."
174 (or mm-coding-system-list
175 (setq mm-coding-system-list (mm-coding-system-list))))
176
177 (defun mm-coding-system-p (cs)
178 "Return non-nil if CS is a symbol naming a coding system.
179 In XEmacs, also return non-nil if CS is a coding system object.
180 If CS is available, return CS itself in Emacs, and return a coding
181 system object in XEmacs."
182 (if (fboundp 'find-coding-system)
183 (and cs (find-coding-system cs))
184 (if (fboundp 'coding-system-p)
185 (when (coding-system-p cs)
186 cs)
187 ;; Is this branch ever actually useful?
188 (car (memq cs (mm-get-coding-system-list))))))
189
190 (defvar mm-charset-synonym-alist
191 `(
192 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
193 ,@(unless (mm-coding-system-p 'x-ctext)
194 '((x-ctext . ctext)))
195 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
196 ,@(unless (mm-coding-system-p 'iso-8859-15)
197 '((iso-8859-15 . iso-8859-1)))
198 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
199 ,@(unless (mm-coding-system-p 'big5-hkscs)
200 '((big5-hkscs . big5)))
201 ;; Windows-1252 is actually a superset of Latin-1. See also
202 ;; `gnus-article-dumbquotes-map'.
203 ,@(unless (mm-coding-system-p 'windows-1252)
204 (if (mm-coding-system-p 'cp1252)
205 '((windows-1252 . cp1252))
206 '((windows-1252 . iso-8859-1))))
207 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
208 ;; Outlook users in Czech republic. Use this to allow reading of their
209 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
210 ,@(if (and (not (mm-coding-system-p 'windows-1250))
211 (mm-coding-system-p 'cp1250))
212 '((windows-1250 . cp1250)))
213 )
214 "A mapping from invalid charset names to the real charset names.")
215
216 (defvar mm-binary-coding-system
217 (cond
218 ((mm-coding-system-p 'binary) 'binary)
219 ((mm-coding-system-p 'no-conversion) 'no-conversion)
220 (t nil))
221 "100% binary coding system.")
222
223 (defvar mm-text-coding-system
224 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
225 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
226 (and (mm-coding-system-p 'raw-text) 'raw-text))
227 mm-binary-coding-system)
228 "Text-safe coding system (For removing ^M).")
229
230 (defvar mm-text-coding-system-for-write nil
231 "Text coding system for write.")
232
233 (defvar mm-auto-save-coding-system
234 (cond
235 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
236 (if (memq system-type '(windows-nt ms-dos ms-windows))
237 (if (mm-coding-system-p 'utf-8-emacs-dos)
238 'utf-8-emacs-dos mm-binary-coding-system)
239 'utf-8-emacs))
240 ((mm-coding-system-p 'emacs-mule)
241 (if (memq system-type '(windows-nt ms-dos ms-windows))
242 (if (mm-coding-system-p 'emacs-mule-dos)
243 'emacs-mule-dos mm-binary-coding-system)
244 'emacs-mule))
245 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
246 (t mm-binary-coding-system))
247 "Coding system of auto save file.")
248
249 (defvar mm-universal-coding-system mm-auto-save-coding-system
250 "The universal coding system.")
251
252 ;; Fixme: some of the cars here aren't valid MIME charsets. That
253 ;; should only matter with XEmacs, though.
254 (defvar mm-mime-mule-charset-alist
255 `((us-ascii ascii)
256 (iso-8859-1 latin-iso8859-1)
257 (iso-8859-2 latin-iso8859-2)
258 (iso-8859-3 latin-iso8859-3)
259 (iso-8859-4 latin-iso8859-4)
260 (iso-8859-5 cyrillic-iso8859-5)
261 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
262 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
263 ;; charset is koi8-r, not iso-8859-5.
264 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
265 (iso-8859-6 arabic-iso8859-6)
266 (iso-8859-7 greek-iso8859-7)
267 (iso-8859-8 hebrew-iso8859-8)
268 (iso-8859-9 latin-iso8859-9)
269 (iso-8859-14 latin-iso8859-14)
270 (iso-8859-15 latin-iso8859-15)
271 (viscii vietnamese-viscii-lower)
272 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
273 (euc-kr korean-ksc5601)
274 (gb2312 chinese-gb2312)
275 (big5 chinese-big5-1 chinese-big5-2)
276 (tibetan tibetan)
277 (thai-tis620 thai-tis620)
278 (windows-1251 cyrillic-iso8859-5)
279 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
280 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
281 latin-jisx0201 japanese-jisx0208-1978
282 chinese-gb2312 japanese-jisx0208
283 korean-ksc5601 japanese-jisx0212)
284 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
285 latin-jisx0201 japanese-jisx0208-1978
286 chinese-gb2312 japanese-jisx0208
287 korean-ksc5601 japanese-jisx0212
288 chinese-cns11643-1 chinese-cns11643-2)
289 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
290 cyrillic-iso8859-5 greek-iso8859-7
291 latin-jisx0201 japanese-jisx0208-1978
292 chinese-gb2312 japanese-jisx0208
293 korean-ksc5601 japanese-jisx0212
294 chinese-cns11643-1 chinese-cns11643-2
295 chinese-cns11643-3 chinese-cns11643-4
296 chinese-cns11643-5 chinese-cns11643-6
297 chinese-cns11643-7)
298 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
299 japanese-jisx0213-1 japanese-jisx0213-2)
300 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
301 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
302 (charsetp 'unicode-a)
303 (not (mm-coding-system-p 'mule-utf-8)))
304 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
305 ;; If we have utf-8 we're in Mule 5+.
306 (append '(utf-8)
307 (delete 'ascii
308 (coding-system-get 'mule-utf-8 'safe-charsets)))))
309 "Alist of MIME-charset/MULE-charsets.")
310
311 (defun mm-enrich-utf-8-by-mule-ucs ()
312 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
313 This function will run when the `un-define' module is loaded under
314 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
315 with Mule charsets. It is completely useless for Emacs."
316 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
317 (assoc "un-define" after-load-alist)))
318 (setq after-load-alist
319 (delete '("un-define") after-load-alist)))
320 (when (boundp 'unicode-basic-translation-charset-order-list)
321 (condition-case nil
322 (let ((val (delq
323 'ascii
324 (copy-sequence
325 (symbol-value
326 'unicode-basic-translation-charset-order-list))))
327 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
328 (if elem
329 (setcdr elem val)
330 (setq mm-mime-mule-charset-alist
331 (nconc mm-mime-mule-charset-alist
332 (list (cons 'utf-8 val))))))
333 (error))))
334
335 ;; Correct by construction, but should be unnecessary for Emacs:
336 (if (featurep 'xemacs)
337 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
338 (when (and (fboundp 'coding-system-list)
339 (fboundp 'sort-coding-systems))
340 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
341 cs mime mule alist)
342 (while css
343 (setq cs (pop css)
344 mime (or (coding-system-get cs :mime-charset) ; Emacs 22
345 (coding-system-get cs 'mime-charset)))
346 (when (and mime
347 (not (eq t (setq mule
348 (coding-system-get cs 'safe-charsets))))
349 (not (assq mime alist)))
350 (push (cons mime (delq 'ascii mule)) alist)))
351 (setq mm-mime-mule-charset-alist (nreverse alist)))))
352
353 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
354 "A list of special charsets.
355 Valid elements include:
356 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
357 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
358 )
359
360 (defvar mm-iso-8859-15-compatible
361 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
362 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
363 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
364
365 (defvar mm-iso-8859-x-to-15-table
366 (and (fboundp 'coding-system-p)
367 (mm-coding-system-p 'iso-8859-15)
368 (mapcar
369 (lambda (cs)
370 (if (mm-coding-system-p (car cs))
371 (let ((c (string-to-char
372 (decode-coding-string "\341" (car cs)))))
373 (cons (char-charset c)
374 (cons
375 (- (string-to-char
376 (decode-coding-string "\341" 'iso-8859-15)) c)
377 (string-to-list (decode-coding-string (car (cdr cs))
378 (car cs))))))
379 '(gnus-charset 0)))
380 mm-iso-8859-15-compatible))
381 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
382
383 (defcustom mm-coding-system-priorities
384 (if (boundp 'current-language-environment)
385 (let ((lang (symbol-value 'current-language-environment)))
386 (cond ((string= lang "Japanese")
387 ;; Japanese users prefer iso-2022-jp to euc-japan or
388 ;; shift_jis, however iso-8859-1 should be used when
389 ;; there are only ASCII text and Latin-1 characters.
390 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
391 "Preferred coding systems for encoding outgoing messages.
392
393 More than one suitable coding system may be found for some text.
394 By default, the coding system with the highest priority is used
395 to encode outgoing messages (see `sort-coding-systems'). If this
396 variable is set, it overrides the default priority."
397 :version "21.2"
398 :type '(repeat (symbol :tag "Coding system"))
399 :group 'mime)
400
401 ;; ??
402 (defvar mm-use-find-coding-systems-region
403 (fboundp 'find-coding-systems-region)
404 "Use `find-coding-systems-region' to find proper coding systems.
405
406 Setting it to nil is useful on Emacsen supporting Unicode if sending
407 mail with multiple parts is preferred to sending a Unicode one.")
408
409 ;;; Internal variables:
410
411 ;;; Functions:
412
413 (defun mm-mule-charset-to-mime-charset (charset)
414 "Return the MIME charset corresponding to the given Mule CHARSET."
415 (if (and (fboundp 'find-coding-systems-for-charsets)
416 (fboundp 'sort-coding-systems))
417 (let ((css (sort (sort-coding-systems
418 (find-coding-systems-for-charsets (list charset)))
419 'mm-sort-coding-systems-predicate))
420 cs mime)
421 (while (and (not mime)
422 css)
423 (when (setq cs (pop css))
424 (setq mime (or (coding-system-get cs :mime-charset)
425 (coding-system-get cs 'mime-charset)))))
426 mime)
427 (let ((alist (mapcar (lambda (cs)
428 (assq cs mm-mime-mule-charset-alist))
429 (sort (mapcar 'car mm-mime-mule-charset-alist)
430 'mm-sort-coding-systems-predicate)))
431 out)
432 (while alist
433 (when (memq charset (cdar alist))
434 (setq out (caar alist)
435 alist nil))
436 (pop alist))
437 out)))
438
439 (defun mm-charset-to-coding-system (charset &optional lbt)
440 "Return coding-system corresponding to CHARSET.
441 CHARSET is a symbol naming a MIME charset.
442 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
443 used as the line break code type of the coding system."
444 (when (stringp charset)
445 (setq charset (intern (downcase charset))))
446 (when lbt
447 (setq charset (intern (format "%s-%s" charset lbt))))
448 (cond
449 ((null charset)
450 charset)
451 ;; Running in a non-MULE environment.
452 ((or (null (mm-get-coding-system-list))
453 (not (fboundp 'coding-system-get)))
454 charset)
455 ;; ascii
456 ((eq charset 'us-ascii)
457 'ascii)
458 ;; Check to see whether we can handle this charset. (This depends
459 ;; on there being some coding system matching each `mime-charset'
460 ;; property defined, as there should be.)
461 ((and (mm-coding-system-p charset)
462 ;;; Doing this would potentially weed out incorrect charsets.
463 ;;; charset
464 ;;; (eq charset (coding-system-get charset 'mime-charset))
465 )
466 charset)
467 ;; Translate invalid charsets.
468 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
469 (and cs (mm-coding-system-p cs) cs)))
470 ;; Last resort: search the coding system list for entries which
471 ;; have the right mime-charset in case the canonical name isn't
472 ;; defined (though it should be).
473 ((let (cs)
474 ;; mm-get-coding-system-list returns a list of cs without lbt.
475 ;; Do we need -lbt?
476 (dolist (c (mm-get-coding-system-list))
477 (if (and (null cs)
478 (eq charset (or (coding-system-get c :mime-charset)
479 (coding-system-get c 'mime-charset))))
480 (setq cs c)))
481 cs))))
482
483 (defsubst mm-replace-chars-in-string (string from to)
484 (mm-subst-char-in-string from to string))
485
486 (eval-and-compile
487 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
488 (boundp 'default-enable-multibyte-characters)
489 default-enable-multibyte-characters
490 (fboundp 'set-buffer-multibyte))
491 "True in Emacs with Mule.")
492
493 (if mm-emacs-mule
494 (defun mm-enable-multibyte ()
495 "Set the multibyte flag of the current buffer.
496 Only do this if the default value of `enable-multibyte-characters' is
497 non-nil. This is a no-op in XEmacs."
498 (set-buffer-multibyte 'to))
499 (defalias 'mm-enable-multibyte 'ignore))
500
501 (if mm-emacs-mule
502 (defun mm-disable-multibyte ()
503 "Unset the multibyte flag of in the current buffer.
504 This is a no-op in XEmacs."
505 (set-buffer-multibyte nil))
506 (defalias 'mm-disable-multibyte 'ignore)))
507
508 (defun mm-preferred-coding-system (charset)
509 ;; A typo in some Emacs versions.
510 (or (get-charset-property charset 'preferred-coding-system)
511 (get-charset-property charset 'prefered-coding-system)))
512
513 ;; Mule charsets shouldn't be used.
514 (defsubst mm-guess-charset ()
515 "Guess Mule charset from the language environment."
516 (or
517 mail-parse-mule-charset ;; cached mule-charset
518 (progn
519 (setq mail-parse-mule-charset
520 (and (boundp 'current-language-environment)
521 (car (last
522 (assq 'charset
523 (assoc current-language-environment
524 language-info-alist))))))
525 (if (or (not mail-parse-mule-charset)
526 (eq mail-parse-mule-charset 'ascii))
527 (setq mail-parse-mule-charset
528 (or (car (last (assq mail-parse-charset
529 mm-mime-mule-charset-alist)))
530 ;; default
531 'latin-iso8859-1)))
532 mail-parse-mule-charset)))
533
534 (defun mm-charset-after (&optional pos)
535 "Return charset of a character in current buffer at position POS.
536 If POS is nil, it defauls to the current point.
537 If POS is out of range, the value is nil.
538 If the charset is `composition', return the actual one."
539 (let ((char (char-after pos)) charset)
540 (if (< (mm-char-int char) 128)
541 (setq charset 'ascii)
542 ;; charset-after is fake in some Emacsen.
543 (setq charset (and (fboundp 'char-charset) (char-charset char)))
544 (if (eq charset 'composition) ; Mule 4
545 (let ((p (or pos (point))))
546 (cadr (find-charset-region p (1+ p))))
547 (if (and charset (not (memq charset '(ascii eight-bit-control
548 eight-bit-graphic))))
549 charset
550 (mm-guess-charset))))))
551
552 (defun mm-mime-charset (charset)
553 "Return the MIME charset corresponding to the given Mule CHARSET."
554 (if (eq charset 'unknown)
555 (error "The message contains non-printable characters, please use attachment"))
556 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
557 ;; This exists in Emacs 20.
558 (or
559 (and (mm-preferred-coding-system charset)
560 (or (coding-system-get
561 (mm-preferred-coding-system charset) :mime-charset)
562 (coding-system-get
563 (mm-preferred-coding-system charset) 'mime-charset)))
564 (and (eq charset 'ascii)
565 'us-ascii)
566 (mm-preferred-coding-system charset)
567 (mm-mule-charset-to-mime-charset charset))
568 ;; This is for XEmacs.
569 (mm-mule-charset-to-mime-charset charset)))
570
571 (if (fboundp 'delete-dups)
572 (defalias 'mm-delete-duplicates 'delete-dups)
573 (defun mm-delete-duplicates (list)
574 "Destructively remove `equal' duplicates from LIST.
575 Store the result in LIST and return it. LIST must be a proper list.
576 Of several `equal' occurrences of an element in LIST, the first
577 one is kept.
578
579 This is a compatibility function for Emacsen without `delete-dups'."
580 ;; Code from `subr.el' in Emacs 22:
581 (let ((tail list))
582 (while tail
583 (setcdr tail (delete (car tail) (cdr tail)))
584 (setq tail (cdr tail))))
585 list))
586
587 ;; Fixme: This is used in places when it should be testing the
588 ;; default multibyteness. See mm-default-multibyte-p.
589 (eval-and-compile
590 (if (and (not (featurep 'xemacs))
591 (boundp 'enable-multibyte-characters))
592 (defun mm-multibyte-p ()
593 "Non-nil if multibyte is enabled in the current buffer."
594 enable-multibyte-characters)
595 (defun mm-multibyte-p () (featurep 'mule))))
596
597 (defun mm-default-multibyte-p ()
598 "Return non-nil if the session is multibyte.
599 This affects whether coding conversion should be attempted generally."
600 (if (featurep 'mule)
601 (if (boundp 'default-enable-multibyte-characters)
602 default-enable-multibyte-characters
603 t)))
604
605 (defun mm-iso-8859-x-to-15-region (&optional b e)
606 (if (fboundp 'char-charset)
607 (let (charset item c inconvertible)
608 (save-restriction
609 (if e (narrow-to-region b e))
610 (goto-char (point-min))
611 (skip-chars-forward "\0-\177")
612 (while (not (eobp))
613 (cond
614 ((not (setq item (assq (char-charset (setq c (char-after)))
615 mm-iso-8859-x-to-15-table)))
616 (forward-char))
617 ((memq c (cdr (cdr item)))
618 (setq inconvertible t)
619 (forward-char))
620 (t
621 (insert-before-markers (prog1 (+ c (car (cdr item)))
622 (delete-char 1)))))
623 (skip-chars-forward "\0-\177")))
624 (not inconvertible))))
625
626 (defun mm-sort-coding-systems-predicate (a b)
627 (let ((priorities
628 (mapcar (lambda (cs)
629 ;; Note: invalid entries are dropped silently
630 (and (setq cs (mm-coding-system-p cs))
631 (coding-system-base cs)))
632 mm-coding-system-priorities)))
633 (and (setq a (mm-coding-system-p a))
634 (if (setq b (mm-coding-system-p b))
635 (> (length (memq (coding-system-base a) priorities))
636 (length (memq (coding-system-base b) priorities)))
637 t))))
638
639 (eval-when-compile
640 (autoload 'latin-unity-massage-name "latin-unity")
641 (autoload 'latin-unity-maybe-remap "latin-unity")
642 (autoload 'latin-unity-representations-feasible-region "latin-unity")
643 (autoload 'latin-unity-representations-present-region "latin-unity")
644 (defvar latin-unity-coding-systems)
645 (defvar latin-unity-ucs-list))
646
647 (defun mm-xemacs-find-mime-charset-1 (begin end)
648 "Determine which MIME charset to use to send region as message.
649 This uses the XEmacs-specific latin-unity package to better handle the
650 case where identical characters from diverse ISO-8859-? character sets
651 can be encoded using a single one of the corresponding coding systems.
652
653 It treats `mm-coding-system-priorities' as the list of preferred
654 coding systems; a useful example setting for this list in Western
655 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
656 to the very standard Latin 1 coding system, and only move to coding
657 systems that are less supported as is necessary to encode the
658 characters that exist in the buffer.
659
660 Latin Unity doesn't know about those non-ASCII Roman characters that
661 are available in various East Asian character sets. As such, its
662 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
663 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
664 But this is very much a corner case, so don't worry about it."
665 (let ((systems mm-coding-system-priorities) csets psets curset)
666
667 ;; Load the Latin Unity library, if available.
668 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
669 (ignore-errors (require 'latin-unity)))
670
671 ;; Now, can we use it?
672 (if (featurep 'latin-unity)
673 (progn
674 (setq csets (latin-unity-representations-feasible-region begin end)
675 psets (latin-unity-representations-present-region begin end))
676
677 (catch 'done
678
679 ;; Pass back the first coding system in the preferred list
680 ;; that can encode the whole region.
681 (dolist (curset systems)
682 (setq curset (latin-unity-massage-name 'buffer-default curset))
683
684 ;; If the coding system is a universal coding system, then
685 ;; it can certainly encode all the characters in the region.
686 (if (memq curset latin-unity-ucs-list)
687 (throw 'done (list curset)))
688
689 ;; If a coding system isn't universal, and isn't in
690 ;; the list that latin unity knows about, we can't
691 ;; decide whether to use it here. Leave that until later
692 ;; in `mm-find-mime-charset-region' function, whence we
693 ;; have been called.
694 (unless (memq curset latin-unity-coding-systems)
695 (throw 'done nil))
696
697 ;; Right, we know about this coding system, and it may
698 ;; conceivably be able to encode all the characters in
699 ;; the region.
700 (if (latin-unity-maybe-remap begin end curset csets psets t)
701 (throw 'done (list curset))))
702
703 ;; Can't encode using anything from the
704 ;; `mm-coding-system-priorities' list.
705 ;; Leave `mm-find-mime-charset' to do most of the work.
706 nil))
707
708 ;; Right, latin unity isn't available; let `mm-find-charset-region'
709 ;; take its default action, which equally applies to GNU Emacs.
710 nil)))
711
712 (defmacro mm-xemacs-find-mime-charset (begin end)
713 (when (featurep 'xemacs)
714 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
715
716 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
717 "Return the MIME charsets needed to encode the region between B and E.
718 nil means ASCII, a single-element list represents an appropriate MIME
719 charset, and a longer list means no appropriate charset."
720 (let (charsets)
721 ;; The return possibilities of this function are a mess...
722 (or (and (mm-multibyte-p)
723 mm-use-find-coding-systems-region
724 ;; Find the mime-charset of the most preferred coding
725 ;; system that has one.
726 (let ((systems (find-coding-systems-region b e)))
727 (when mm-coding-system-priorities
728 (setq systems
729 (sort systems 'mm-sort-coding-systems-predicate)))
730 (setq systems (delq 'compound-text systems))
731 (unless (equal systems '(undecided))
732 (while systems
733 (let* ((head (pop systems))
734 (cs (or (coding-system-get head :mime-charset)
735 (coding-system-get head 'mime-charset))))
736 ;; The mime-charset (`x-ctext') of
737 ;; `compound-text' is not in the IANA list. We
738 ;; shouldn't normally use anything here with a
739 ;; mime-charset having an `x-' prefix.
740 ;; Fixme: Allow this to be overridden, since
741 ;; there is existing use of x-ctext.
742 ;; Also people apparently need the coding system
743 ;; `iso-2022-jp-3' (which Mule-UCS defines with
744 ;; mime-charset, though it's not valid).
745 (if (and cs
746 (not (string-match "^[Xx]-" (symbol-name cs)))
747 ;; UTF-16 of any variety is invalid for
748 ;; text parts and, unfortunately, has
749 ;; mime-charset defined both in Mule-UCS
750 ;; and versions of Emacs. (The name
751 ;; might be `mule-utf-16...' or
752 ;; `utf-16...'.)
753 (not (string-match "utf-16" (symbol-name cs))))
754 (setq systems nil
755 charsets (list cs))))))
756 charsets))
757 ;; If we're XEmacs, and some coding system is appropriate,
758 ;; mm-xemacs-find-mime-charset will return an appropriate list.
759 ;; Otherwise, we'll get nil, and the next setq will get invoked.
760 (setq charsets (mm-xemacs-find-mime-charset b e))
761
762 ;; We're not multibyte, or a single coding system won't cover it.
763 (setq charsets
764 (mm-delete-duplicates
765 (mapcar 'mm-mime-charset
766 (delq 'ascii
767 (mm-find-charset-region b e))))))
768 (if (and (> (length charsets) 1)
769 (memq 'iso-8859-15 charsets)
770 (memq 'iso-8859-15 hack-charsets)
771 (save-excursion (mm-iso-8859-x-to-15-region b e)))
772 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
773 mm-iso-8859-15-compatible))
774 (if (and (memq 'iso-2022-jp-2 charsets)
775 (memq 'iso-2022-jp-2 hack-charsets))
776 (setq charsets (delq 'iso-2022-jp charsets)))
777 ;; Attempt to reduce the number of charsets if utf-8 is available.
778 (if (and (featurep 'xemacs)
779 (> (length charsets) 1)
780 (mm-coding-system-p 'utf-8))
781 (let ((mm-coding-system-priorities
782 (cons 'utf-8 mm-coding-system-priorities)))
783 (setq charsets
784 (mm-delete-duplicates
785 (mapcar 'mm-mime-charset
786 (delq 'ascii
787 (mm-find-charset-region b e)))))))
788 charsets))
789
790 (defmacro mm-with-unibyte-buffer (&rest forms)
791 "Create a temporary buffer, and evaluate FORMS there like `progn'.
792 Use unibyte mode for this."
793 `(let (default-enable-multibyte-characters)
794 (with-temp-buffer ,@forms)))
795 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
796 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
797
798 (defmacro mm-with-multibyte-buffer (&rest forms)
799 "Create a temporary buffer, and evaluate FORMS there like `progn'.
800 Use multibyte mode for this."
801 `(let ((default-enable-multibyte-characters t))
802 (with-temp-buffer ,@forms)))
803 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
804 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
805
806 (defmacro mm-with-unibyte-current-buffer (&rest forms)
807 "Evaluate FORMS with current buffer temporarily made unibyte.
808 Also bind `default-enable-multibyte-characters' to nil.
809 Equivalent to `progn' in XEmacs"
810 (let ((multibyte (make-symbol "multibyte"))
811 (buffer (make-symbol "buffer")))
812 `(if mm-emacs-mule
813 (let ((,multibyte enable-multibyte-characters)
814 (,buffer (current-buffer)))
815 (unwind-protect
816 (let (default-enable-multibyte-characters)
817 (set-buffer-multibyte nil)
818 ,@forms)
819 (set-buffer ,buffer)
820 (set-buffer-multibyte ,multibyte)))
821 (let (default-enable-multibyte-characters)
822 ,@forms))))
823 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
824 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
825
826 (defmacro mm-with-unibyte (&rest forms)
827 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
828 `(let (default-enable-multibyte-characters)
829 ,@forms))
830 (put 'mm-with-unibyte 'lisp-indent-function 0)
831 (put 'mm-with-unibyte 'edebug-form-spec '(body))
832
833 (defmacro mm-with-multibyte (&rest forms)
834 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
835 `(let ((default-enable-multibyte-characters t))
836 ,@forms))
837 (put 'mm-with-multibyte 'lisp-indent-function 0)
838 (put 'mm-with-multibyte 'edebug-form-spec '(body))
839
840 (defun mm-find-charset-region (b e)
841 "Return a list of Emacs charsets in the region B to E."
842 (cond
843 ((and (mm-multibyte-p)
844 (fboundp 'find-charset-region))
845 ;; Remove composition since the base charsets have been included.
846 ;; Remove eight-bit-*, treat them as ascii.
847 (let ((css (find-charset-region b e)))
848 (mapcar (lambda (cs) (setq css (delq cs css)))
849 '(composition eight-bit-control eight-bit-graphic
850 control-1))
851 css))
852 (t
853 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
854 (save-excursion
855 (save-restriction
856 (narrow-to-region b e)
857 (goto-char (point-min))
858 (skip-chars-forward "\0-\177")
859 (if (eobp)
860 '(ascii)
861 (let (charset)
862 (setq charset
863 (and (boundp 'current-language-environment)
864 (car (last (assq 'charset
865 (assoc current-language-environment
866 language-info-alist))))))
867 (if (eq charset 'ascii) (setq charset nil))
868 (or charset
869 (setq charset
870 (car (last (assq mail-parse-charset
871 mm-mime-mule-charset-alist)))))
872 (list 'ascii (or charset 'latin-iso8859-1)))))))))
873
874 (if (fboundp 'shell-quote-argument)
875 (defalias 'mm-quote-arg 'shell-quote-argument)
876 (defun mm-quote-arg (arg)
877 "Return a version of ARG that is safe to evaluate in a shell."
878 (let ((pos 0) new-pos accum)
879 ;; *** bug: we don't handle newline characters properly
880 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
881 (push (substring arg pos new-pos) accum)
882 (push "\\" accum)
883 (push (list (aref arg new-pos)) accum)
884 (setq pos (1+ new-pos)))
885 (if (= pos 0)
886 arg
887 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
888
889 (defun mm-auto-mode-alist ()
890 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
891 (let ((alist auto-mode-alist)
892 out)
893 (while alist
894 (when (listp (cdar alist))
895 (push (car alist) out))
896 (pop alist))
897 (nreverse out)))
898
899 (defvar mm-inhibit-file-name-handlers
900 '(jka-compr-handler image-file-handler)
901 "A list of handlers doing (un)compression (etc) thingies.")
902
903 (defun mm-insert-file-contents (filename &optional visit beg end replace
904 inhibit)
905 "Like `insert-file-contents', but only reads in the file.
906 A buffer may be modified in several ways after reading into the buffer due
907 to advanced Emacs features, such as file-name-handlers, format decoding,
908 `find-file-hooks', etc.
909 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
910 This function ensures that none of these modifications will take place."
911 (let* ((format-alist nil)
912 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
913 (default-major-mode 'fundamental-mode)
914 (enable-local-variables nil)
915 (after-insert-file-functions nil)
916 (enable-local-eval nil)
917 (inhibit-file-name-operation (if inhibit
918 'insert-file-contents
919 inhibit-file-name-operation))
920 (inhibit-file-name-handlers
921 (if inhibit
922 (append mm-inhibit-file-name-handlers
923 inhibit-file-name-handlers)
924 inhibit-file-name-handlers))
925 (ffh (if (boundp 'find-file-hook)
926 'find-file-hook
927 'find-file-hooks))
928 (val (symbol-value ffh)))
929 (set ffh nil)
930 (unwind-protect
931 (insert-file-contents filename visit beg end replace)
932 (set ffh val))))
933
934 (defun mm-append-to-file (start end filename &optional codesys inhibit)
935 "Append the contents of the region to the end of file FILENAME.
936 When called from a function, expects three arguments,
937 START, END and FILENAME. START and END are buffer positions
938 saying what text to write.
939 Optional fourth argument specifies the coding system to use when
940 encoding the file.
941 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
942 (let ((coding-system-for-write
943 (or codesys mm-text-coding-system-for-write
944 mm-text-coding-system))
945 (inhibit-file-name-operation (if inhibit
946 'append-to-file
947 inhibit-file-name-operation))
948 (inhibit-file-name-handlers
949 (if inhibit
950 (append mm-inhibit-file-name-handlers
951 inhibit-file-name-handlers)
952 inhibit-file-name-handlers)))
953 (write-region start end filename t 'no-message)
954 (message "Appended to %s" filename)))
955
956 (defun mm-write-region (start end filename &optional append visit lockname
957 coding-system inhibit)
958
959 "Like `write-region'.
960 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
961 (let ((coding-system-for-write
962 (or coding-system mm-text-coding-system-for-write
963 mm-text-coding-system))
964 (inhibit-file-name-operation (if inhibit
965 'write-region
966 inhibit-file-name-operation))
967 (inhibit-file-name-handlers
968 (if inhibit
969 (append mm-inhibit-file-name-handlers
970 inhibit-file-name-handlers)
971 inhibit-file-name-handlers)))
972 (write-region start end filename append visit lockname)))
973
974 (defun mm-image-load-path (&optional package)
975 (let (dir result)
976 (dolist (path load-path (nreverse result))
977 (when (and path
978 (file-directory-p
979 (setq dir (concat (file-name-directory
980 (directory-file-name path))
981 "etc/images/" (or package "gnus/")))))
982 (push dir result))
983 (push path result))))
984
985 ;; Fixme: This doesn't look useful where it's used.
986 (if (fboundp 'detect-coding-region)
987 (defun mm-detect-coding-region (start end)
988 "Like `detect-coding-region' except returning the best one."
989 (let ((coding-systems
990 (detect-coding-region start end)))
991 (or (car-safe coding-systems)
992 coding-systems)))
993 (defun mm-detect-coding-region (start end)
994 (let ((point (point)))
995 (goto-char start)
996 (skip-chars-forward "\0-\177" end)
997 (prog1
998 (if (eq (point) end) 'ascii (mm-guess-charset))
999 (goto-char point)))))
1000
1001 (if (fboundp 'coding-system-get)
1002 (defun mm-detect-mime-charset-region (start end)
1003 "Detect MIME charset of the text in the region between START and END."
1004 (let ((cs (mm-detect-coding-region start end)))
1005 (coding-system-get cs 'mime-charset)))
1006 (defun mm-detect-mime-charset-region (start end)
1007 "Detect MIME charset of the text in the region between START and END."
1008 (let ((cs (mm-detect-coding-region start end)))
1009 cs)))
1010
1011
1012 (provide 'mm-util)
1013
1014 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1015 ;;; mm-util.el ends here