]> code.delx.au - gnu-emacs/blob - lisp/gnus/mm-util.el
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-83
[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 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 23
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 (defcustom mm-coding-system-priorities
354 (if (boundp 'current-language-environment)
355 (let ((lang (symbol-value 'current-language-environment)))
356 (cond ((string= lang "Japanese")
357 ;; Japanese users prefer iso-2022-jp to euc-japan or
358 ;; shift_jis, however iso-8859-1 should be used when
359 ;; there are only ASCII text and Latin-1 characters.
360 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
361 "Preferred coding systems for encoding outgoing messages.
362
363 More than one suitable coding system may be found for some text.
364 By default, the coding system with the highest priority is used
365 to encode outgoing messages (see `sort-coding-systems'). If this
366 variable is set, it overrides the default priority."
367 :version "21.2"
368 :type '(repeat (symbol :tag "Coding system"))
369 :group 'mime)
370
371 ;; ??
372 (defvar mm-use-find-coding-systems-region
373 (fboundp 'find-coding-systems-region)
374 "Use `find-coding-systems-region' to find proper coding systems.
375
376 Setting it to nil is useful on Emacsen supporting Unicode if sending
377 mail with multiple parts is preferred to sending a Unicode one.")
378
379 ;;; Internal variables:
380
381 ;;; Functions:
382
383 (defun mm-mule-charset-to-mime-charset (charset)
384 "Return the MIME charset corresponding to the given Mule CHARSET."
385 (if (and (fboundp 'find-coding-systems-for-charsets)
386 (fboundp 'sort-coding-systems))
387 (let ((css (sort (sort-coding-systems
388 (find-coding-systems-for-charsets (list charset)))
389 'mm-sort-coding-systems-predicate))
390 cs mime)
391 (while (and (not mime)
392 css)
393 (when (setq cs (pop css))
394 (setq mime (or (coding-system-get cs :mime-charset)
395 (coding-system-get cs 'mime-charset)))))
396 mime)
397 (let ((alist (mapcar (lambda (cs)
398 (assq cs mm-mime-mule-charset-alist))
399 (sort (mapcar 'car mm-mime-mule-charset-alist)
400 'mm-sort-coding-systems-predicate)))
401 out)
402 (while alist
403 (when (memq charset (cdar alist))
404 (setq out (caar alist)
405 alist nil))
406 (pop alist))
407 out)))
408
409 (defun mm-charset-to-coding-system (charset &optional lbt)
410 "Return coding-system corresponding to CHARSET.
411 CHARSET is a symbol naming a MIME charset.
412 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
413 used as the line break code type of the coding system."
414 (when (stringp charset)
415 (setq charset (intern (downcase charset))))
416 (when lbt
417 (setq charset (intern (format "%s-%s" charset lbt))))
418 (cond
419 ((null charset)
420 charset)
421 ;; Running in a non-MULE environment.
422 ((or (null (mm-get-coding-system-list))
423 (not (fboundp 'coding-system-get)))
424 charset)
425 ;; ascii
426 ((eq charset 'us-ascii)
427 'ascii)
428 ;; Check to see whether we can handle this charset. (This depends
429 ;; on there being some coding system matching each `mime-charset'
430 ;; property defined, as there should be.)
431 ((and (mm-coding-system-p charset)
432 ;;; Doing this would potentially weed out incorrect charsets.
433 ;;; charset
434 ;;; (eq charset (coding-system-get charset 'mime-charset))
435 )
436 charset)
437 ;; Translate invalid charsets.
438 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
439 (and cs (mm-coding-system-p cs) cs)))
440 ;; Last resort: search the coding system list for entries which
441 ;; have the right mime-charset in case the canonical name isn't
442 ;; defined (though it should be).
443 ((let (cs)
444 ;; mm-get-coding-system-list returns a list of cs without lbt.
445 ;; Do we need -lbt?
446 (dolist (c (mm-get-coding-system-list))
447 (if (and (null cs)
448 (eq charset (or (coding-system-get c :mime-charset)
449 (coding-system-get c 'mime-charset))))
450 (setq cs c)))
451 cs))))
452
453 (defsubst mm-replace-chars-in-string (string from to)
454 (mm-subst-char-in-string from to string))
455
456 (eval-and-compile
457 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
458 (boundp 'default-enable-multibyte-characters)
459 default-enable-multibyte-characters
460 (fboundp 'set-buffer-multibyte))
461 "True in Emacs with Mule.")
462
463 (if mm-emacs-mule
464 (defun mm-enable-multibyte ()
465 "Set the multibyte flag of the current buffer.
466 Only do this if the default value of `enable-multibyte-characters' is
467 non-nil. This is a no-op in XEmacs."
468 (set-buffer-multibyte 'to))
469 (defalias 'mm-enable-multibyte 'ignore))
470
471 (if mm-emacs-mule
472 (defun mm-disable-multibyte ()
473 "Unset the multibyte flag of in the current buffer.
474 This is a no-op in XEmacs."
475 (set-buffer-multibyte nil))
476 (defalias 'mm-disable-multibyte 'ignore)))
477
478 (defun mm-preferred-coding-system (charset)
479 ;; A typo in some Emacs versions.
480 (or (get-charset-property charset 'preferred-coding-system)
481 (get-charset-property charset 'prefered-coding-system)))
482
483 ;; Mule charsets shouldn't be used.
484 (defsubst mm-guess-charset ()
485 "Guess Mule charset from the language environment."
486 (or
487 mail-parse-mule-charset ;; cached mule-charset
488 (progn
489 (setq mail-parse-mule-charset
490 (and (boundp 'current-language-environment)
491 (car (last
492 (assq 'charset
493 (assoc current-language-environment
494 language-info-alist))))))
495 (if (or (not mail-parse-mule-charset)
496 (eq mail-parse-mule-charset 'ascii))
497 (setq mail-parse-mule-charset
498 (or (car (last (assq mail-parse-charset
499 mm-mime-mule-charset-alist)))
500 ;; default
501 'latin-iso8859-1)))
502 mail-parse-mule-charset)))
503
504 (defun mm-charset-after (&optional pos)
505 "Return charset of a character in current buffer at position POS.
506 If POS is nil, it defauls to the current point.
507 If POS is out of range, the value is nil.
508 If the charset is `composition', return the actual one."
509 (let ((char (char-after pos)) charset)
510 (if (< (mm-char-int char) 128)
511 (setq charset 'ascii)
512 ;; charset-after is fake in some Emacsen.
513 (setq charset (and (fboundp 'char-charset) (char-charset char)))
514 (if (eq charset 'composition) ; Mule 4
515 (let ((p (or pos (point))))
516 (cadr (find-charset-region p (1+ p))))
517 (if (and charset (not (memq charset '(ascii eight-bit-control
518 eight-bit-graphic))))
519 charset
520 (mm-guess-charset))))))
521
522 (defun mm-mime-charset (charset)
523 "Return the MIME charset corresponding to the given Mule CHARSET."
524 (if (eq charset 'unknown)
525 (error "The message contains non-printable characters, please use attachment"))
526 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
527 ;; This exists in Emacs 20.
528 (or
529 (and (mm-preferred-coding-system charset)
530 (or (coding-system-get
531 (mm-preferred-coding-system charset) :mime-charset)
532 (coding-system-get
533 (mm-preferred-coding-system charset) 'mime-charset)))
534 (and (eq charset 'ascii)
535 'us-ascii)
536 (mm-preferred-coding-system charset)
537 (mm-mule-charset-to-mime-charset charset))
538 ;; This is for XEmacs.
539 (mm-mule-charset-to-mime-charset charset)))
540
541 (defun mm-delete-duplicates (list)
542 "Simple substitute for CL `delete-duplicates', testing with `equal'."
543 (let (result head)
544 (while list
545 (setq head (car list))
546 (setq list (delete head list))
547 (setq result (cons head result)))
548 (nreverse result)))
549
550 ;; Fixme: This is used in places when it should be testing the
551 ;; default multibyteness. See mm-default-multibyte-p.
552 (eval-and-compile
553 (if (and (not (featurep 'xemacs))
554 (boundp 'enable-multibyte-characters))
555 (defun mm-multibyte-p ()
556 "Non-nil if multibyte is enabled in the current buffer."
557 enable-multibyte-characters)
558 (defun mm-multibyte-p () (featurep 'mule))))
559
560 (defun mm-default-multibyte-p ()
561 "Return non-nil if the session is multibyte.
562 This affects whether coding conversion should be attempted generally."
563 (if (featurep 'mule)
564 (if (boundp 'default-enable-multibyte-characters)
565 default-enable-multibyte-characters
566 t)))
567
568 (defun mm-sort-coding-systems-predicate (a b)
569 (let ((priorities
570 (mapcar (lambda (cs)
571 ;; Note: invalid entries are dropped silently
572 (and (setq cs (mm-coding-system-p cs))
573 (coding-system-base cs)))
574 mm-coding-system-priorities)))
575 (and (setq a (mm-coding-system-p a))
576 (if (setq b (mm-coding-system-p b))
577 (> (length (memq (coding-system-base a) priorities))
578 (length (memq (coding-system-base b) priorities)))
579 t))))
580
581 (eval-when-compile
582 (autoload 'latin-unity-massage-name "latin-unity")
583 (autoload 'latin-unity-maybe-remap "latin-unity")
584 (autoload 'latin-unity-representations-feasible-region "latin-unity")
585 (autoload 'latin-unity-representations-present-region "latin-unity")
586 (defvar latin-unity-coding-systems)
587 (defvar latin-unity-ucs-list))
588
589 (defun mm-xemacs-find-mime-charset-1 (begin end)
590 "Determine which MIME charset to use to send region as message.
591 This uses the XEmacs-specific latin-unity package to better handle the
592 case where identical characters from diverse ISO-8859-? character sets
593 can be encoded using a single one of the corresponding coding systems.
594
595 It treats `mm-coding-system-priorities' as the list of preferred
596 coding systems; a useful example setting for this list in Western
597 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
598 to the very standard Latin 1 coding system, and only move to coding
599 systems that are less supported as is necessary to encode the
600 characters that exist in the buffer.
601
602 Latin Unity doesn't know about those non-ASCII Roman characters that
603 are available in various East Asian character sets. As such, its
604 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
605 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
606 But this is very much a corner case, so don't worry about it."
607 (let ((systems mm-coding-system-priorities) csets psets curset)
608
609 ;; Load the Latin Unity library, if available.
610 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
611 (ignore-errors (require 'latin-unity)))
612
613 ;; Now, can we use it?
614 (if (featurep 'latin-unity)
615 (progn
616 (setq csets (latin-unity-representations-feasible-region begin end)
617 psets (latin-unity-representations-present-region begin end))
618
619 (catch 'done
620
621 ;; Pass back the first coding system in the preferred list
622 ;; that can encode the whole region.
623 (dolist (curset systems)
624 (setq curset (latin-unity-massage-name 'buffer-default curset))
625
626 ;; If the coding system is a universal coding system, then
627 ;; it can certainly encode all the characters in the region.
628 (if (memq curset latin-unity-ucs-list)
629 (throw 'done (list curset)))
630
631 ;; If a coding system isn't universal, and isn't in
632 ;; the list that latin unity knows about, we can't
633 ;; decide whether to use it here. Leave that until later
634 ;; in `mm-find-mime-charset-region' function, whence we
635 ;; have been called.
636 (unless (memq curset latin-unity-coding-systems)
637 (throw 'done nil))
638
639 ;; Right, we know about this coding system, and it may
640 ;; conceivably be able to encode all the characters in
641 ;; the region.
642 (if (latin-unity-maybe-remap begin end curset csets psets t)
643 (throw 'done (list curset))))
644
645 ;; Can't encode using anything from the
646 ;; `mm-coding-system-priorities' list.
647 ;; Leave `mm-find-mime-charset' to do most of the work.
648 nil))
649
650 ;; Right, latin unity isn't available; let `mm-find-charset-region'
651 ;; take its default action, which equally applies to GNU Emacs.
652 nil)))
653
654 (defmacro mm-xemacs-find-mime-charset (begin end)
655 (when (featurep 'xemacs)
656 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
657
658 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
659 "Return the MIME charsets needed to encode the region between B and E.
660 nil means ASCII, a single-element list represents an appropriate MIME
661 charset, and a longer list means no appropriate charset."
662 (let (charsets)
663 ;; The return possibilities of this function are a mess...
664 (or (and (mm-multibyte-p)
665 mm-use-find-coding-systems-region
666 ;; Find the mime-charset of the most preferred coding
667 ;; system that has one.
668 (let ((systems (find-coding-systems-region b e)))
669 (when mm-coding-system-priorities
670 (setq systems
671 (sort systems 'mm-sort-coding-systems-predicate)))
672 (setq systems (delq 'compound-text systems))
673 (unless (equal systems '(undecided))
674 (while systems
675 (let* ((head (pop systems))
676 (cs (or (coding-system-get head :mime-charset)
677 (coding-system-get head 'mime-charset))))
678 ;; The mime-charset (`x-ctext') of
679 ;; `compound-text' is not in the IANA list. We
680 ;; shouldn't normally use anything here with a
681 ;; mime-charset having an `x-' prefix.
682 ;; Fixme: Allow this to be overridden, since
683 ;; there is existing use of x-ctext.
684 ;; Also people apparently need the coding system
685 ;; `iso-2022-jp-3' (which Mule-UCS defines with
686 ;; mime-charset, though it's not valid).
687 (if (and cs
688 (not (string-match "^[Xx]-" (symbol-name cs)))
689 ;; UTF-16 of any variety is invalid for
690 ;; text parts and, unfortunately, has
691 ;; mime-charset defined both in Mule-UCS
692 ;; and versions of Emacs. (The name
693 ;; might be `mule-utf-16...' or
694 ;; `utf-16...'.)
695 (not (string-match "utf-16" (symbol-name cs))))
696 (setq systems nil
697 charsets (list cs))))))
698 charsets))
699 ;; If we're XEmacs, and some coding system is appropriate,
700 ;; mm-xemacs-find-mime-charset will return an appropriate list.
701 ;; Otherwise, we'll get nil, and the next setq will get invoked.
702 (setq charsets (mm-xemacs-find-mime-charset b e))
703
704 ;; Fixme: won't work for unibyte Emacs 23:
705
706 ;; We're not multibyte, or a single coding system won't cover it.
707 (setq charsets
708 (mm-delete-duplicates
709 (mapcar 'mm-mime-charset
710 (delq 'ascii
711 (mm-find-charset-region b e))))))
712 charsets))
713
714 (defmacro mm-with-unibyte-buffer (&rest forms)
715 "Create a temporary buffer, and evaluate FORMS there like `progn'.
716 Use unibyte mode for this."
717 `(let (default-enable-multibyte-characters)
718 (with-temp-buffer ,@forms)))
719 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
720 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
721
722 (defmacro mm-with-multibyte-buffer (&rest forms)
723 "Create a temporary buffer, and evaluate FORMS there like `progn'.
724 Use multibyte mode for this."
725 `(let ((default-enable-multibyte-characters t))
726 (with-temp-buffer ,@forms)))
727 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
728 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
729
730 (defmacro mm-with-unibyte-current-buffer (&rest forms)
731 "Evaluate FORMS with current buffer temporarily made unibyte.
732 Also bind `default-enable-multibyte-characters' to nil.
733 Equivalent to `progn' in XEmacs"
734 (let ((multibyte (make-symbol "multibyte"))
735 (buffer (make-symbol "buffer")))
736 `(if mm-emacs-mule
737 (let ((,multibyte enable-multibyte-characters)
738 (,buffer (current-buffer)))
739 (unwind-protect
740 (let (default-enable-multibyte-characters)
741 (set-buffer-multibyte nil)
742 ,@forms)
743 (set-buffer ,buffer)
744 (set-buffer-multibyte ,multibyte)))
745 (let (default-enable-multibyte-characters)
746 ,@forms))))
747 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
748 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
749
750 (defmacro mm-with-unibyte (&rest forms)
751 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
752 `(let (default-enable-multibyte-characters)
753 ,@forms))
754 (put 'mm-with-unibyte 'lisp-indent-function 0)
755 (put 'mm-with-unibyte 'edebug-form-spec '(body))
756
757 (defmacro mm-with-multibyte (&rest forms)
758 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
759 `(let ((default-enable-multibyte-characters t))
760 ,@forms))
761 (put 'mm-with-multibyte 'lisp-indent-function 0)
762 (put 'mm-with-multibyte 'edebug-form-spec '(body))
763
764 (defun mm-find-charset-region (b e)
765 "Return a list of Emacs charsets in the region B to E."
766 (cond
767 ((and (mm-multibyte-p)
768 (fboundp 'find-charset-region))
769 ;; Remove composition since the base charsets have been included.
770 ;; Remove eight-bit-*, treat them as ascii.
771 (let ((css (find-charset-region b e)))
772 (mapcar (lambda (cs) (setq css (delq cs css)))
773 '(composition eight-bit-control eight-bit-graphic
774 control-1))
775 css))
776 (t
777 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
778 (save-excursion
779 (save-restriction
780 (narrow-to-region b e)
781 (goto-char (point-min))
782 (skip-chars-forward "\0-\177")
783 (if (eobp)
784 '(ascii)
785 (let (charset)
786 (setq charset
787 (and (boundp 'current-language-environment)
788 (car (last (assq 'charset
789 (assoc current-language-environment
790 language-info-alist))))))
791 (if (eq charset 'ascii) (setq charset nil))
792 (or charset
793 (setq charset
794 (car (last (assq mail-parse-charset
795 mm-mime-mule-charset-alist)))))
796 (list 'ascii (or charset 'latin-iso8859-1)))))))))
797
798 (if (fboundp 'shell-quote-argument)
799 (defalias 'mm-quote-arg 'shell-quote-argument)
800 (defun mm-quote-arg (arg)
801 "Return a version of ARG that is safe to evaluate in a shell."
802 (let ((pos 0) new-pos accum)
803 ;; *** bug: we don't handle newline characters properly
804 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
805 (push (substring arg pos new-pos) accum)
806 (push "\\" accum)
807 (push (list (aref arg new-pos)) accum)
808 (setq pos (1+ new-pos)))
809 (if (= pos 0)
810 arg
811 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
812
813 (defun mm-auto-mode-alist ()
814 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
815 (let ((alist auto-mode-alist)
816 out)
817 (while alist
818 (when (listp (cdar alist))
819 (push (car alist) out))
820 (pop alist))
821 (nreverse out)))
822
823 (defvar mm-inhibit-file-name-handlers
824 '(jka-compr-handler image-file-handler)
825 "A list of handlers doing (un)compression (etc) thingies.")
826
827 (defun mm-insert-file-contents (filename &optional visit beg end replace
828 inhibit)
829 "Like `insert-file-contents', but only reads in the file.
830 A buffer may be modified in several ways after reading into the buffer due
831 to advanced Emacs features, such as file-name-handlers, format decoding,
832 `find-file-hooks', etc.
833 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
834 This function ensures that none of these modifications will take place."
835 (let* ((format-alist nil)
836 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
837 (default-major-mode 'fundamental-mode)
838 (enable-local-variables nil)
839 (after-insert-file-functions nil)
840 (enable-local-eval nil)
841 (inhibit-file-name-operation (if inhibit
842 'insert-file-contents
843 inhibit-file-name-operation))
844 (inhibit-file-name-handlers
845 (if inhibit
846 (append mm-inhibit-file-name-handlers
847 inhibit-file-name-handlers)
848 inhibit-file-name-handlers))
849 (ffh (if (boundp 'find-file-hook)
850 'find-file-hook
851 'find-file-hooks))
852 (val (symbol-value ffh)))
853 (set ffh nil)
854 (unwind-protect
855 (insert-file-contents filename visit beg end replace)
856 (set ffh val))))
857
858 (defun mm-append-to-file (start end filename &optional codesys inhibit)
859 "Append the contents of the region to the end of file FILENAME.
860 When called from a function, expects three arguments,
861 START, END and FILENAME. START and END are buffer positions
862 saying what text to write.
863 Optional fourth argument specifies the coding system to use when
864 encoding the file.
865 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
866 (let ((coding-system-for-write
867 (or codesys mm-text-coding-system-for-write
868 mm-text-coding-system))
869 (inhibit-file-name-operation (if inhibit
870 'append-to-file
871 inhibit-file-name-operation))
872 (inhibit-file-name-handlers
873 (if inhibit
874 (append mm-inhibit-file-name-handlers
875 inhibit-file-name-handlers)
876 inhibit-file-name-handlers)))
877 (write-region start end filename t 'no-message)
878 (message "Appended to %s" filename)))
879
880 (defun mm-write-region (start end filename &optional append visit lockname
881 coding-system inhibit)
882
883 "Like `write-region'.
884 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
885 (let ((coding-system-for-write
886 (or coding-system mm-text-coding-system-for-write
887 mm-text-coding-system))
888 (inhibit-file-name-operation (if inhibit
889 'write-region
890 inhibit-file-name-operation))
891 (inhibit-file-name-handlers
892 (if inhibit
893 (append mm-inhibit-file-name-handlers
894 inhibit-file-name-handlers)
895 inhibit-file-name-handlers)))
896 (write-region start end filename append visit lockname)))
897
898 (defun mm-image-load-path (&optional package)
899 (let (dir result)
900 (dolist (path load-path (nreverse result))
901 (when (and path
902 (file-directory-p
903 (setq dir (concat (file-name-directory
904 (directory-file-name path))
905 "etc/images/" (or package "gnus/")))))
906 (push dir result))
907 (push path result))))
908
909 ;; Fixme: This doesn't look useful where it's used.
910 (if (fboundp 'detect-coding-region)
911 (defun mm-detect-coding-region (start end)
912 "Like `detect-coding-region' except returning the best one."
913 (let ((coding-systems
914 (detect-coding-region start end)))
915 (or (car-safe coding-systems)
916 coding-systems)))
917 (defun mm-detect-coding-region (start end)
918 (let ((point (point)))
919 (goto-char start)
920 (skip-chars-forward "\0-\177" end)
921 (prog1
922 (if (eq (point) end) 'ascii (mm-guess-charset))
923 (goto-char point)))))
924
925 (if (fboundp 'coding-system-get)
926 (defun mm-detect-mime-charset-region (start end)
927 "Detect MIME charset of the text in the region between START and END."
928 (let ((cs (mm-detect-coding-region start end)))
929 (coding-system-get cs 'mime-charset)))
930 (defun mm-detect-mime-charset-region (start end)
931 "Detect MIME charset of the text in the region between START and END."
932 (let ((cs (mm-detect-coding-region start end)))
933 cs)))
934
935
936 (provide 'mm-util)
937
938 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
939 ;;; mm-util.el ends here