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