Revision: miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-95
[bpt/emacs.git] / lisp / gnus / mm-util.el
CommitLineData
95fa1ff7 1;;; mm-util.el --- Utility functions for Mule and low level things
23f87bed
MB
2;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3;; Free Software Foundation, Inc.
c113de23
GM
4
5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7;; This file is part of GNU Emacs.
8
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.
13
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.
18
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.
23
24;;; Commentary:
25
26;;; Code:
27
23f87bed 28(eval-when-compile (require 'cl))
c113de23
GM
29(require 'mail-prsvr)
30
f53b2875
DL
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)
f53b2875
DL
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)))
f53b2875
DL
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))))
95fa1ff7
SZ
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)))
f53b2875 71 (string-as-unibyte . identity)
23f87bed 72 (string-make-unibyte . identity)
95fa1ff7 73 (string-as-multibyte . identity)
56e09c09 74 (multibyte-string-p . ignore)
23f87bed
MB
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)))
56e09c09
DL
85 (insert-byte . insert-char)
86 (multibyte-char-to-unibyte . identity))))
f53b2875 87
c113de23
GM
88(eval-and-compile
89 (defalias 'mm-char-or-char-int-p
95fa1ff7 90 (cond
c113de23 91 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
95fa1ff7 92 ((fboundp 'char-valid-p) 'char-valid-p)
c113de23
GM
93 (t 'identity))))
94
23f87bed
MB
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'.
95fa1ff7
SZ
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)))))))
117
c113de23
GM
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))))
123
23f87bed
MB
124(defun mm-coding-system-p (cs)
125 "Return non-nil if CS is a symbol naming a coding system.
0683d241
MB
126In XEmacs, also return non-nil if CS is a coding system object.
127If CS is available, return CS itself in Emacs, and return a coding
128system object in XEmacs."
23f87bed
MB
129 (if (fboundp 'find-coding-system)
130 (find-coding-system cs)
131 (if (fboundp 'coding-system-p)
0683d241
MB
132 (when (coding-system-p cs)
133 cs)
23f87bed 134 ;; Is this branch ever actually useful?
0683d241 135 (car (memq cs (mm-get-coding-system-list))))))
95fa1ff7 136
c113de23 137(defvar mm-charset-synonym-alist
95fa1ff7 138 `(
95fa1ff7 139 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
72eb5fc7
SZ
140 ,@(unless (mm-coding-system-p 'x-ctext)
141 '((x-ctext . ctext)))
23f87bed
MB
142 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
143 ,@(unless (mm-coding-system-p 'iso-8859-15)
72eb5fc7 144 '((iso-8859-15 . iso-8859-1)))
23f87bed
MB
145 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
146 ,@(unless (mm-coding-system-p 'big5-hkscs)
147 '((big5-hkscs . big5)))
d1a7bc93
DL
148 ;; Windows-1252 is actually a superset of Latin-1. See also
149 ;; `gnus-article-dumbquotes-map'.
a1506d29 150 ,@(unless (mm-coding-system-p 'windows-1252)
72eb5fc7
SZ
151 (if (mm-coding-system-p 'cp1252)
152 '((windows-1252 . cp1252))
153 '((windows-1252 . iso-8859-1))))
20c381cf
GM
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.
72eb5fc7
SZ
157 ,@(if (and (not (mm-coding-system-p 'windows-1250))
158 (mm-coding-system-p 'cp1250))
159 '((windows-1250 . cp1250)))
95fa1ff7 160 )
c113de23
GM
161 "A mapping from invalid charset names to the real charset names.")
162
c113de23 163(defvar mm-binary-coding-system
95fa1ff7 164 (cond
c113de23
GM
165 ((mm-coding-system-p 'binary) 'binary)
166 ((mm-coding-system-p 'no-conversion) 'no-conversion)
167 (t nil))
168 "100% binary coding system.")
169
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).")
176
177(defvar mm-text-coding-system-for-write nil
178 "Text coding system for write.")
179
180(defvar mm-auto-save-coding-system
95fa1ff7 181 (cond
23f87bed 182 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
56e09c09
DL
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))
c113de23
GM
187 ((mm-coding-system-p 'emacs-mule)
188 (if (memq system-type '(windows-nt ms-dos ms-windows))
95fa1ff7 189 (if (mm-coding-system-p 'emacs-mule-dos)
c113de23
GM
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.")
195
95fa1ff7 196(defvar mm-universal-coding-system mm-auto-save-coding-system
47b63dfa 197 "The universal coding system.")
95fa1ff7
SZ
198
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)
0683d241 225 (windows-1251 cyrillic-iso8859-5)
95fa1ff7
SZ
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
0683d241 230 korean-ksc5601 japanese-jisx0212)
95fa1ff7
SZ
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)
0683d241
MB
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)
95fa1ff7
SZ
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.")
257
0683d241
MB
258(defun mm-enrich-utf-8-by-mule-ucs ()
259 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
260This function will run when the `un-define' module is loaded under
261XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
262with 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))))
281
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)))))
95fa1ff7 299
47b63dfa
SZ
300(defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
301 "A list of special charsets.
302Valid 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."
305)
306
a1506d29 307(defvar mm-iso-8859-15-compatible
47b63dfa
SZ
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.")
311
312(defvar mm-iso-8859-x-to-15-table
313 (and (fboundp 'coding-system-p)
314 (mm-coding-system-p 'iso-8859-15)
a1506d29 315 (mapcar
47b63dfa
SZ
316 (lambda (cs)
317 (if (mm-coding-system-p (car cs))
a1506d29 318 (let ((c (string-to-char
47b63dfa
SZ
319 (decode-coding-string "\341" (car cs)))))
320 (cons (char-charset c)
321 (cons
a1506d29 322 (- (string-to-char
47b63dfa 323 (decode-coding-string "\341" 'iso-8859-15)) c)
a1506d29 324 (string-to-list (decode-coding-string (car (cdr cs))
47b63dfa
SZ
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.")
329
23f87bed
MB
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")
5153a47a
MB
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)))))
23f87bed
MB
338 "Preferred coding systems for encoding outgoing messages.
339
340More than one suitable coding system may be found for some text.
341By default, the coding system with the highest priority is used
342to encode outgoing messages (see `sort-coding-systems'). If this
343variable is set, it overrides the default priority."
a08b59c9 344 :version "21.2"
23f87bed
MB
345 :type '(repeat (symbol :tag "Coding system"))
346 :group 'mime)
347
348;; ??
1f7d2e14
SZ
349(defvar mm-use-find-coding-systems-region
350 (fboundp 'find-coding-systems-region)
23f87bed
MB
351 "Use `find-coding-systems-region' to find proper coding systems.
352
353Setting it to nil is useful on Emacsen supporting Unicode if sending
354mail with multiple parts is preferred to sending a Unicode one.")
1f7d2e14 355
c113de23
GM
356;;; Internal variables:
357
358;;; Functions:
359
360(defun mm-mule-charset-to-mime-charset (charset)
1c57d870 361 "Return the MIME charset corresponding to the given Mule CHARSET."
23f87bed
MB
362 (if (and (fboundp 'find-coding-systems-for-charsets)
363 (fboundp 'sort-coding-systems))
0683d241
MB
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)))))
95fa1ff7 373 mime)
0683d241
MB
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)))
95fa1ff7
SZ
378 out)
379 (while alist
380 (when (memq charset (cdar alist))
381 (setq out (caar alist)
382 alist nil))
383 (pop alist))
384 out)))
c113de23
GM
385
386(defun mm-charset-to-coding-system (charset &optional lbt)
387 "Return coding-system corresponding to CHARSET.
388CHARSET is a symbol naming a MIME charset.
389If optional argument LBT (`unix', `dos' or `mac') is specified, it is
390used as the line break code type of the coding system."
391 (when (stringp charset)
392 (setq charset (intern (downcase charset))))
c113de23
GM
393 (when lbt
394 (setq charset (intern (format "%s-%s" charset lbt))))
395 (cond
47b63dfa
SZ
396 ((null charset)
397 charset)
c113de23 398 ;; Running in a non-MULE environment.
23f87bed
MB
399 ((or (null (mm-get-coding-system-list))
400 (not (fboundp 'coding-system-get)))
c113de23
GM
401 charset)
402 ;; ascii
403 ((eq charset 'us-ascii)
404 'ascii)
1c57d870
DL
405 ;; Check to see whether we can handle this charset. (This depends
406 ;; on there being some coding system matching each `mime-charset'
95fa1ff7
SZ
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))
412 )
c113de23 413 charset)
95fa1ff7 414 ;; Translate invalid charsets.
d62d49df 415 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
23f87bed 416 (and cs (mm-coding-system-p cs) cs)))
95fa1ff7
SZ
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)
56e09c09
DL
425 (eq charset (or (coding-system-get c :mime-charset)
426 (coding-system-get c 'mime-charset))))
95fa1ff7
SZ
427 (setq cs c)))
428 cs))))
429
430(defsubst mm-replace-chars-in-string (string from to)
431 (mm-subst-char-in-string from to string))
432
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))
56e09c09 438 "True in Emacs with Mule.")
95fa1ff7
SZ
439
440 (if mm-emacs-mule
441 (defun mm-enable-multibyte ()
442 "Set the multibyte flag of the current buffer.
1c57d870
DL
443Only do this if the default value of `enable-multibyte-characters' is
444non-nil. This is a no-op in XEmacs."
23f87bed 445 (set-buffer-multibyte 'to))
95fa1ff7 446 (defalias 'mm-enable-multibyte 'ignore))
c113de23 447
95fa1ff7
SZ
448 (if mm-emacs-mule
449 (defun mm-disable-multibyte ()
450 "Unset the multibyte flag of in the current buffer.
1c57d870 451This is a no-op in XEmacs."
95fa1ff7 452 (set-buffer-multibyte nil))
56e09c09 453 (defalias 'mm-disable-multibyte 'ignore)))
052802c1 454
c113de23
GM
455(defun mm-preferred-coding-system (charset)
456 ;; A typo in some Emacs versions.
47b63dfa
SZ
457 (or (get-charset-property charset 'preferred-coding-system)
458 (get-charset-property charset 'prefered-coding-system)))
c113de23 459
23f87bed
MB
460;; Mule charsets shouldn't be used.
461(defsubst mm-guess-charset ()
462 "Guess Mule charset from the language environment."
463 (or
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)))
480
c113de23
GM
481(defun mm-charset-after (&optional pos)
482 "Return charset of a character in current buffer at position POS.
483If POS is nil, it defauls to the current point.
484If POS is out of range, the value is nil.
485If the charset is `composition', return the actual one."
052802c1
DL
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)))
56e09c09 491 (if (eq charset 'composition) ; Mule 4
052802c1
DL
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
23f87bed 497 (mm-guess-charset))))))
c113de23
GM
498
499(defun mm-mime-charset (charset)
1c57d870 500 "Return the MIME charset corresponding to the given Mule CHARSET."
95fa1ff7
SZ
501 (if (eq charset 'unknown)
502 (error "The message contains non-printable characters, please use attachment"))
052802c1 503 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
c113de23
GM
504 ;; This exists in Emacs 20.
505 (or
506 (and (mm-preferred-coding-system charset)
56e09c09
DL
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)))
c113de23
GM
511 (and (eq charset 'ascii)
512 'us-ascii)
95fa1ff7 513 (mm-preferred-coding-system charset)
c113de23
GM
514 (mm-mule-charset-to-mime-charset charset))
515 ;; This is for XEmacs.
516 (mm-mule-charset-to-mime-charset charset)))
517
518(defun mm-delete-duplicates (list)
68a38c23 519 "Simple substitute for CL `delete-duplicates', testing with `equal'."
c113de23
GM
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)))
526
23f87bed
MB
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
052802c1
DL
530 (if (and (not (featurep 'xemacs))
531 (boundp 'enable-multibyte-characters))
23f87bed
MB
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))))
536
537(defun mm-default-multibyte-p ()
538 "Return non-nil if the session is multibyte.
539This 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)))
c113de23 544
47b63dfa
SZ
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))
a1506d29
JB
553 (cond
554 ((not (setq item (assq (char-charset (setq c (char-after)))
47b63dfa
SZ
555 mm-iso-8859-x-to-15-table)))
556 (forward-char))
557 ((memq c (cdr (cdr item)))
558 (setq inconvertible t)
559 (forward-char))
560 (t
23f87bed
MB
561 (insert-before-markers (prog1 (+ c (car (cdr item)))
562 (delete-char 1)))))
563 (skip-chars-forward "\0-\177")))
47b63dfa
SZ
564 (not inconvertible))))
565
566(defun mm-sort-coding-systems-predicate (a b)
23f87bed
MB
567 (let ((priorities
568 (mapcar (lambda (cs)
569 ;; Note: invalid entries are dropped silently
0683d241 570 (and (setq cs (mm-coding-system-p cs))
23f87bed
MB
571 (coding-system-base cs)))
572 mm-coding-system-priorities)))
0683d241
MB
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))))
47b63dfa 578
aa0a8561
MB
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))
586
587(defun mm-xemacs-find-mime-charset-1 (begin end)
588 "Determine which MIME charset to use to send region as message.
589This uses the XEmacs-specific latin-unity package to better handle the
590case where identical characters from diverse ISO-8859-? character sets
591can be encoded using a single one of the corresponding coding systems.
592
593It treats `mm-coding-system-priorities' as the list of preferred
594coding systems; a useful example setting for this list in Western
595Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
596to the very standard Latin 1 coding system, and only move to coding
597systems that are less supported as is necessary to encode the
598characters that exist in the buffer.
599
600Latin Unity doesn't know about those non-ASCII Roman characters that
601are available in various East Asian character sets. As such, its
602behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
603buffer and it can otherwise be encoded as Latin 1, won't be ideal.
604But this is very much a corner case, so don't worry about it."
605 (let ((systems mm-coding-system-priorities) csets psets curset)
606
607 ;; Load the Latin Unity library, if available.
608 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
609 (require 'latin-unity))
610
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))
616
617 (catch 'done
618
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))
623
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)))
628
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))
636
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))))
642
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))
647
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)))
651
652(defmacro mm-xemacs-find-mime-charset (begin end)
653 (when (featurep 'xemacs)
654 `(mm-xemacs-find-mime-charset-1 ,begin ,end)))
655
47b63dfa 656(defun mm-find-mime-charset-region (b e &optional hack-charsets)
95fa1ff7 657 "Return the MIME charsets needed to encode the region between B and E.
f0529b5b 658nil means ASCII, a single-element list represents an appropriate MIME
95fa1ff7 659charset, and a longer list means no appropriate charset."
47b63dfa
SZ
660 (let (charsets)
661 ;; The return possibilities of this function are a mess...
662 (or (and (mm-multibyte-p)
1f7d2e14 663 mm-use-find-coding-systems-region
47b63dfa
SZ
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
a1506d29 668 (setq systems
47b63dfa 669 (sort systems 'mm-sort-coding-systems-predicate)))
47b63dfa
SZ
670 (setq systems (delq 'compound-text systems))
671 (unless (equal systems '(undecided))
672 (while systems
56e09c09
DL
673 (let* ((head (pop systems))
674 (cs (or (coding-system-get head :mime-charset)
675 (coding-system-get head 'mime-charset))))
23f87bed
MB
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))))
47b63dfa
SZ
694 (setq systems nil
695 charsets (list cs))))))
696 charsets))
aa0a8561
MB
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))
701
702 ;; We're not multibyte, or a single coding system won't cover it.
a1506d29 703 (setq charsets
47b63dfa
SZ
704 (mm-delete-duplicates
705 (mapcar 'mm-mime-charset
706 (delq 'ascii
707 (mm-find-charset-region b e))))))
23f87bed
MB
708 (if (and (> (length charsets) 1)
709 (memq 'iso-8859-15 charsets)
47b63dfa
SZ
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))
95fa1ff7 718
c113de23
GM
719(defmacro mm-with-unibyte-buffer (&rest forms)
720 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1c57d870
DL
721Use unibyte mode for this."
722 `(let (default-enable-multibyte-characters)
723 (with-temp-buffer ,@forms)))
c113de23
GM
724(put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
725(put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
726
23f87bed
MB
727(defmacro mm-with-multibyte-buffer (&rest forms)
728 "Create a temporary buffer, and evaluate FORMS there like `progn'.
729Use 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))
734
c113de23 735(defmacro mm-with-unibyte-current-buffer (&rest forms)
56e09c09 736 "Evaluate FORMS with current buffer temporarily made unibyte.
1c57d870
DL
737Also bind `default-enable-multibyte-characters' to nil.
738Equivalent to `progn' in XEmacs"
95fa1ff7
SZ
739 (let ((multibyte (make-symbol "multibyte"))
740 (buffer (make-symbol "buffer")))
a1506d29 741 `(if mm-emacs-mule
95fa1ff7
SZ
742 (let ((,multibyte enable-multibyte-characters)
743 (,buffer (current-buffer)))
1c57d870
DL
744 (unwind-protect
745 (let (default-enable-multibyte-characters)
746 (set-buffer-multibyte nil)
747 ,@forms)
95fa1ff7 748 (set-buffer ,buffer)
1c57d870 749 (set-buffer-multibyte ,multibyte)))
95fa1ff7 750 (let (default-enable-multibyte-characters)
1c57d870 751 ,@forms))))
c113de23
GM
752(put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
753(put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
754
755(defmacro mm-with-unibyte (&rest forms)
23f87bed 756 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
1c57d870
DL
757 `(let (default-enable-multibyte-characters)
758 ,@forms))
c113de23
GM
759(put 'mm-with-unibyte 'lisp-indent-function 0)
760(put 'mm-with-unibyte 'edebug-form-spec '(body))
761
23f87bed
MB
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))
768
c113de23 769(defun mm-find-charset-region (b e)
1c57d870 770 "Return a list of Emacs charsets in the region B to E."
c113de23
GM
771 (cond
772 ((and (mm-multibyte-p)
95fa1ff7 773 (fboundp 'find-charset-region))
c113de23 774 ;; Remove composition since the base charsets have been included.
95fa1ff7
SZ
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))
052802c1
DL
781 (t
782 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
c113de23
GM
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)
052802c1
DL
790 (let (charset)
791 (setq charset
792 (and (boundp 'current-language-environment)
95fa1ff7
SZ
793 (car (last (assq 'charset
794 (assoc current-language-environment
052802c1
DL
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)))))))))
c113de23
GM
802
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)
815 arg
816 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
817
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)))
827
828(defvar mm-inhibit-file-name-handlers
244d58ba 829 '(jka-compr-handler image-file-handler)
c113de23
GM
830 "A list of handlers doing (un)compression (etc) thingies.")
831
832(defun mm-insert-file-contents (filename &optional visit beg end replace
833 inhibit)
23f87bed 834 "Like `insert-file-contents', but only reads in the file.
c113de23
GM
835A buffer may be modified in several ways after reading into the buffer due
836to advanced Emacs features, such as file-name-handlers, format decoding,
23f87bed 837`find-file-hooks', etc.
56e09c09 838If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
c113de23
GM
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)
95fa1ff7 844 (after-insert-file-functions nil)
c113de23
GM
845 (enable-local-eval nil)
846 (find-file-hooks nil)
95fa1ff7 847 (inhibit-file-name-operation (if inhibit
c113de23
GM
848 'insert-file-contents
849 inhibit-file-name-operation))
850 (inhibit-file-name-handlers
851 (if inhibit
95fa1ff7 852 (append mm-inhibit-file-name-handlers
c113de23
GM
853 inhibit-file-name-handlers)
854 inhibit-file-name-handlers)))
855 (insert-file-contents filename visit beg end replace)))
856
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.
859When called from a function, expects three arguments,
860START, END and FILENAME. START and END are buffer positions
861saying what text to write.
862Optional fourth argument specifies the coding system to use when
863encoding the file.
23f87bed 864If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
95fa1ff7
SZ
865 (let ((coding-system-for-write
866 (or codesys mm-text-coding-system-for-write
c113de23 867 mm-text-coding-system))
95fa1ff7 868 (inhibit-file-name-operation (if inhibit
c113de23
GM
869 'append-to-file
870 inhibit-file-name-operation))
871 (inhibit-file-name-handlers
872 (if inhibit
95fa1ff7 873 (append mm-inhibit-file-name-handlers
c113de23
GM
874 inhibit-file-name-handlers)
875 inhibit-file-name-handlers)))
23f87bed
MB
876 (write-region start end filename t 'no-message)
877 (message "Appended to %s" filename)))
c113de23 878
95fa1ff7 879(defun mm-write-region (start end filename &optional append visit lockname
c113de23
GM
880 coding-system inhibit)
881
882 "Like `write-region'.
23f87bed 883If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
95fa1ff7
SZ
884 (let ((coding-system-for-write
885 (or coding-system mm-text-coding-system-for-write
c113de23 886 mm-text-coding-system))
95fa1ff7 887 (inhibit-file-name-operation (if inhibit
c113de23
GM
888 'write-region
889 inhibit-file-name-operation))
890 (inhibit-file-name-handlers
891 (if inhibit
95fa1ff7 892 (append mm-inhibit-file-name-handlers
c113de23
GM
893 inhibit-file-name-handlers)
894 inhibit-file-name-handlers)))
895 (write-region start end filename append visit lockname)))
896
95fa1ff7
SZ
897(defun mm-image-load-path (&optional package)
898 (let (dir result)
899 (dolist (path load-path (nreverse result))
f4dd4ae8
MB
900 (when (and path
901 (file-directory-p
902 (setq dir (concat (file-name-directory
903 (directory-file-name path))
904 "etc/" (or package "gnus/")))))
905 (push dir result))
95fa1ff7
SZ
906 (push path result))))
907
23f87bed
MB
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)))))
923
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)))
933
3efe5554 934
c113de23
GM
935(provide 'mm-util)
936
ab5796a9 937;;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
c113de23 938;;; mm-util.el ends here