1 ;;; mm-util.el --- Utility functions for Mule and low level things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
29 (eval-when-compile (require 'cl))
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)
45 (coding-system-equal . equal)
46 (annotationp . ignore)
47 (set-buffer-file-coding-system . ignore)
49 . (lambda (charset int)
57 (mapcar (lambda (e) (list (symbol-name (car e))))
58 mm-mime-mule-charset-alist)
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)))
68 ;; Replace all occurrences of FROM with TO.
70 (when (= (aref string idx) from)
74 (string-as-unibyte . identity)
75 (string-make-unibyte . identity)
76 ;; string-as-multibyte often doesn't really do what you think it does.
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)
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)
97 "Return a multibyte string with the same individual chars as string."
99 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
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)
108 temporary-file-directory))))
110 (make-directory file))
112 (insert-byte . insert-char)
113 (multibyte-char-to-unibyte . identity))))
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.
125 This is a compatibility function for different Emacsen."
126 (replace-regexp-in-string regexp newtext string nil literal)))
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.
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))))
142 (defalias 'mm-char-or-char-int-p
144 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
145 ((fboundp 'char-valid-p) 'char-valid-p)
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;
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'.
157 (defalias 'mm-read-coding-system
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."
168 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
169 mm-mime-mule-charset-alist)))))))
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))))
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)
187 ;; Is this branch ever actually useful?
188 (car (memq cs (mm-get-coding-system-list))))))
190 (defvar mm-charset-synonym-alist
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)))
214 "A mapping from invalid charset names to the real charset names.")
216 (defvar mm-binary-coding-system
218 ((mm-coding-system-p 'binary) 'binary)
219 ((mm-coding-system-p 'no-conversion) 'no-conversion)
221 "100% binary coding system.")
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).")
230 (defvar mm-text-coding-system-for-write nil
231 "Text coding system for write.")
233 (defvar mm-auto-save-coding-system
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)
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)
245 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
246 (t mm-binary-coding-system))
247 "Coding system of auto save file.")
249 (defvar mm-universal-coding-system mm-auto-save-coding-system
250 "The universal coding system.")
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
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)
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
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+.
308 (coding-system-get 'mule-utf-8 'safe-charsets)))))
309 "Alist of MIME-charset/MULE-charsets.")
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)
326 'unicode-basic-translation-charset-order-list))))
327 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
330 (setq mm-mime-mule-charset-alist
331 (nconc mm-mime-mule-charset-alist
332 (list (cons 'utf-8 val))))))
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)))
344 mime (or (coding-system-get cs :mime-charset) ; Emacs 22
345 (coding-system-get cs 'mime-charset)))
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)))))
353 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
354 "A list of special charsets.
355 Valid elements include:
356 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
357 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
360 (defvar mm-iso-8859-15-compatible
361 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
362 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
363 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
365 (defvar mm-iso-8859-x-to-15-table
366 (and (fboundp 'coding-system-p)
367 (mm-coding-system-p 'iso-8859-15)
370 (if (mm-coding-system-p (car cs))
371 (let ((c (string-to-char
372 (decode-coding-string "\341" (car cs)))))
373 (cons (char-charset c)
376 (decode-coding-string "\341" 'iso-8859-15)) c)
377 (string-to-list (decode-coding-string (car (cdr cs))
380 mm-iso-8859-15-compatible))
381 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
383 (defcustom mm-coding-system-priorities
384 (if (boundp 'current-language-environment)
385 (let ((lang (symbol-value 'current-language-environment)))
386 (cond ((string= lang "Japanese")
387 ;; Japanese users prefer iso-2022-jp to euc-japan or
388 ;; shift_jis, however iso-8859-1 should be used when
389 ;; there are only ASCII text and Latin-1 characters.
390 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
391 "Preferred coding systems for encoding outgoing messages.
393 More than one suitable coding system may be found for some text.
394 By default, the coding system with the highest priority is used
395 to encode outgoing messages (see `sort-coding-systems'). If this
396 variable is set, it overrides the default priority."
398 :type '(repeat (symbol :tag "Coding system"))
402 (defvar mm-use-find-coding-systems-region
403 (fboundp 'find-coding-systems-region)
404 "Use `find-coding-systems-region' to find proper coding systems.
406 Setting it to nil is useful on Emacsen supporting Unicode if sending
407 mail with multiple parts is preferred to sending a Unicode one.")
409 ;;; Internal variables:
413 (defun mm-mule-charset-to-mime-charset (charset)
414 "Return the MIME charset corresponding to the given Mule CHARSET."
415 (if (and (fboundp 'find-coding-systems-for-charsets)
416 (fboundp 'sort-coding-systems))
417 (let ((css (sort (sort-coding-systems
418 (find-coding-systems-for-charsets (list charset)))
419 'mm-sort-coding-systems-predicate))
421 (while (and (not mime)
423 (when (setq cs (pop css))
424 (setq mime (or (coding-system-get cs :mime-charset)
425 (coding-system-get cs 'mime-charset)))))
427 (let ((alist (mapcar (lambda (cs)
428 (assq cs mm-mime-mule-charset-alist))
429 (sort (mapcar 'car mm-mime-mule-charset-alist)
430 'mm-sort-coding-systems-predicate)))
433 (when (memq charset (cdar alist))
434 (setq out (caar alist)
439 (defun mm-charset-to-coding-system (charset &optional lbt)
440 "Return coding-system corresponding to CHARSET.
441 CHARSET is a symbol naming a MIME charset.
442 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
443 used as the line break code type of the coding system."
444 (when (stringp charset)
445 (setq charset (intern (downcase charset))))
447 (setq charset (intern (format "%s-%s" charset lbt))))
451 ;; Running in a non-MULE environment.
452 ((or (null (mm-get-coding-system-list))
453 (not (fboundp 'coding-system-get)))
456 ((eq charset 'us-ascii)
458 ;; Check to see whether we can handle this charset. (This depends
459 ;; on there being some coding system matching each `mime-charset'
460 ;; property defined, as there should be.)
461 ((and (mm-coding-system-p charset)
462 ;;; Doing this would potentially weed out incorrect charsets.
464 ;;; (eq charset (coding-system-get charset 'mime-charset))
467 ;; Translate invalid charsets.
468 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
469 (and cs (mm-coding-system-p cs) cs)))
470 ;; Last resort: search the coding system list for entries which
471 ;; have the right mime-charset in case the canonical name isn't
472 ;; defined (though it should be).
474 ;; mm-get-coding-system-list returns a list of cs without lbt.
476 (dolist (c (mm-get-coding-system-list))
478 (eq charset (or (coding-system-get c :mime-charset)
479 (coding-system-get c 'mime-charset))))
483 (defsubst mm-replace-chars-in-string (string from to)
484 (mm-subst-char-in-string from to string))
487 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
488 (boundp 'default-enable-multibyte-characters)
489 default-enable-multibyte-characters
490 (fboundp 'set-buffer-multibyte))
491 "True in Emacs with Mule.")
494 (defun mm-enable-multibyte ()
495 "Set the multibyte flag of the current buffer.
496 Only do this if the default value of `enable-multibyte-characters' is
497 non-nil. This is a no-op in XEmacs."
498 (set-buffer-multibyte 'to))
499 (defalias 'mm-enable-multibyte 'ignore))
502 (defun mm-disable-multibyte ()
503 "Unset the multibyte flag of in the current buffer.
504 This is a no-op in XEmacs."
505 (set-buffer-multibyte nil))
506 (defalias 'mm-disable-multibyte 'ignore)))
508 (defun mm-preferred-coding-system (charset)
509 ;; A typo in some Emacs versions.
510 (or (get-charset-property charset 'preferred-coding-system)
511 (get-charset-property charset 'prefered-coding-system)))
513 ;; Mule charsets shouldn't be used.
514 (defsubst mm-guess-charset ()
515 "Guess Mule charset from the language environment."
517 mail-parse-mule-charset ;; cached mule-charset
519 (setq mail-parse-mule-charset
520 (and (boundp 'current-language-environment)
523 (assoc current-language-environment
524 language-info-alist))))))
525 (if (or (not mail-parse-mule-charset)
526 (eq mail-parse-mule-charset 'ascii))
527 (setq mail-parse-mule-charset
528 (or (car (last (assq mail-parse-charset
529 mm-mime-mule-charset-alist)))
532 mail-parse-mule-charset)))
534 (defun mm-charset-after (&optional pos)
535 "Return charset of a character in current buffer at position POS.
536 If POS is nil, it defauls to the current point.
537 If POS is out of range, the value is nil.
538 If the charset is `composition', return the actual one."
539 (let ((char (char-after pos)) charset)
540 (if (< (mm-char-int char) 128)
541 (setq charset 'ascii)
542 ;; charset-after is fake in some Emacsen.
543 (setq charset (and (fboundp 'char-charset) (char-charset char)))
544 (if (eq charset 'composition) ; Mule 4
545 (let ((p (or pos (point))))
546 (cadr (find-charset-region p (1+ p))))
547 (if (and charset (not (memq charset '(ascii eight-bit-control
548 eight-bit-graphic))))
550 (mm-guess-charset))))))
552 (defun mm-mime-charset (charset)
553 "Return the MIME charset corresponding to the given Mule CHARSET."
554 (if (eq charset 'unknown)
555 (error "The message contains non-printable characters, please use attachment"))
556 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
557 ;; This exists in Emacs 20.
559 (and (mm-preferred-coding-system charset)
560 (or (coding-system-get
561 (mm-preferred-coding-system charset) :mime-charset)
563 (mm-preferred-coding-system charset) 'mime-charset)))
564 (and (eq charset 'ascii)
566 (mm-preferred-coding-system charset)
567 (mm-mule-charset-to-mime-charset charset))
568 ;; This is for XEmacs.
569 (mm-mule-charset-to-mime-charset charset)))
571 (defun mm-delete-duplicates (list)
572 "Simple substitute for CL `delete-duplicates', testing with `equal'."
575 (setq head (car list))
576 (setq list (delete head list))
577 (setq result (cons head result)))
580 ;; Fixme: This is used in places when it should be testing the
581 ;; default multibyteness. See mm-default-multibyte-p.
583 (if (and (not (featurep 'xemacs))
584 (boundp 'enable-multibyte-characters))
585 (defun mm-multibyte-p ()
586 "Non-nil if multibyte is enabled in the current buffer."
587 enable-multibyte-characters)
588 (defun mm-multibyte-p () (featurep 'mule))))
590 (defun mm-default-multibyte-p ()
591 "Return non-nil if the session is multibyte.
592 This affects whether coding conversion should be attempted generally."
594 (if (boundp 'default-enable-multibyte-characters)
595 default-enable-multibyte-characters
598 (defun mm-iso-8859-x-to-15-region (&optional b e)
599 (if (fboundp 'char-charset)
600 (let (charset item c inconvertible)
602 (if e (narrow-to-region b e))
603 (goto-char (point-min))
604 (skip-chars-forward "\0-\177")
607 ((not (setq item (assq (char-charset (setq c (char-after)))
608 mm-iso-8859-x-to-15-table)))
610 ((memq c (cdr (cdr item)))
611 (setq inconvertible t)
614 (insert-before-markers (prog1 (+ c (car (cdr item)))
616 (skip-chars-forward "\0-\177")))
617 (not inconvertible))))
619 (defun mm-sort-coding-systems-predicate (a b)
622 ;; Note: invalid entries are dropped silently
623 (and (setq cs (mm-coding-system-p cs))
624 (coding-system-base cs)))
625 mm-coding-system-priorities)))
626 (and (setq a (mm-coding-system-p a))
627 (if (setq b (mm-coding-system-p b))
628 (> (length (memq (coding-system-base a) priorities))
629 (length (memq (coding-system-base b) priorities)))
633 (autoload 'latin-unity-massage-name "latin-unity")
634 (autoload 'latin-unity-maybe-remap "latin-unity")
635 (autoload 'latin-unity-representations-feasible-region "latin-unity")
636 (autoload 'latin-unity-representations-present-region "latin-unity")
637 (defvar latin-unity-coding-systems)
638 (defvar latin-unity-ucs-list))
640 (defun mm-xemacs-find-mime-charset-1 (begin end)
641 "Determine which MIME charset to use to send region as message.
642 This uses the XEmacs-specific latin-unity package to better handle the
643 case where identical characters from diverse ISO-8859-? character sets
644 can be encoded using a single one of the corresponding coding systems.
646 It treats `mm-coding-system-priorities' as the list of preferred
647 coding systems; a useful example setting for this list in Western
648 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
649 to the very standard Latin 1 coding system, and only move to coding
650 systems that are less supported as is necessary to encode the
651 characters that exist in the buffer.
653 Latin Unity doesn't know about those non-ASCII Roman characters that
654 are available in various East Asian character sets. As such, its
655 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
656 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
657 But this is very much a corner case, so don't worry about it."
658 (let ((systems mm-coding-system-priorities) csets psets curset)
660 ;; Load the Latin Unity library, if available.
661 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
662 (ignore-errors (require 'latin-unity)))
664 ;; Now, can we use it?
665 (if (featurep 'latin-unity)
667 (setq csets (latin-unity-representations-feasible-region begin end)
668 psets (latin-unity-representations-present-region begin end))
672 ;; Pass back the first coding system in the preferred list
673 ;; that can encode the whole region.
674 (dolist (curset systems)
675 (setq curset (latin-unity-massage-name 'buffer-default curset))
677 ;; If the coding system is a universal coding system, then
678 ;; it can certainly encode all the characters in the region.
679 (if (memq curset latin-unity-ucs-list)
680 (throw 'done (list curset)))
682 ;; If a coding system isn't universal, and isn't in
683 ;; the list that latin unity knows about, we can't
684 ;; decide whether to use it here. Leave that until later
685 ;; in `mm-find-mime-charset-region' function, whence we
687 (unless (memq curset latin-unity-coding-systems)
690 ;; Right, we know about this coding system, and it may
691 ;; conceivably be able to encode all the characters in
693 (if (latin-unity-maybe-remap begin end curset csets psets t)
694 (throw 'done (list curset))))
696 ;; Can't encode using anything from the
697 ;; `mm-coding-system-priorities' list.
698 ;; Leave `mm-find-mime-charset' to do most of the work.
701 ;; Right, latin unity isn't available; let `mm-find-charset-region'
702 ;; take its default action, which equally applies to GNU Emacs.
705 (defmacro mm-xemacs-find-mime-charset (begin end)
706 (when (featurep 'xemacs)
707 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
709 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
710 "Return the MIME charsets needed to encode the region between B and E.
711 nil means ASCII, a single-element list represents an appropriate MIME
712 charset, and a longer list means no appropriate charset."
714 ;; The return possibilities of this function are a mess...
715 (or (and (mm-multibyte-p)
716 mm-use-find-coding-systems-region
717 ;; Find the mime-charset of the most preferred coding
718 ;; system that has one.
719 (let ((systems (find-coding-systems-region b e)))
720 (when mm-coding-system-priorities
722 (sort systems 'mm-sort-coding-systems-predicate)))
723 (setq systems (delq 'compound-text systems))
724 (unless (equal systems '(undecided))
726 (let* ((head (pop systems))
727 (cs (or (coding-system-get head :mime-charset)
728 (coding-system-get head 'mime-charset))))
729 ;; The mime-charset (`x-ctext') of
730 ;; `compound-text' is not in the IANA list. We
731 ;; shouldn't normally use anything here with a
732 ;; mime-charset having an `x-' prefix.
733 ;; Fixme: Allow this to be overridden, since
734 ;; there is existing use of x-ctext.
735 ;; Also people apparently need the coding system
736 ;; `iso-2022-jp-3' (which Mule-UCS defines with
737 ;; mime-charset, though it's not valid).
739 (not (string-match "^[Xx]-" (symbol-name cs)))
740 ;; UTF-16 of any variety is invalid for
741 ;; text parts and, unfortunately, has
742 ;; mime-charset defined both in Mule-UCS
743 ;; and versions of Emacs. (The name
744 ;; might be `mule-utf-16...' or
746 (not (string-match "utf-16" (symbol-name cs))))
748 charsets (list cs))))))
750 ;; If we're XEmacs, and some coding system is appropriate,
751 ;; mm-xemacs-find-mime-charset will return an appropriate list.
752 ;; Otherwise, we'll get nil, and the next setq will get invoked.
753 (setq charsets (mm-xemacs-find-mime-charset b e))
755 ;; We're not multibyte, or a single coding system won't cover it.
757 (mm-delete-duplicates
758 (mapcar 'mm-mime-charset
760 (mm-find-charset-region b e))))))
761 (if (and (> (length charsets) 1)
762 (memq 'iso-8859-15 charsets)
763 (memq 'iso-8859-15 hack-charsets)
764 (save-excursion (mm-iso-8859-x-to-15-region b e)))
765 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
766 mm-iso-8859-15-compatible))
767 (if (and (memq 'iso-2022-jp-2 charsets)
768 (memq 'iso-2022-jp-2 hack-charsets))
769 (setq charsets (delq 'iso-2022-jp charsets)))
770 ;; Attempt to reduce the number of charsets if utf-8 is available.
771 (if (and (featurep 'xemacs)
772 (> (length charsets) 1)
773 (mm-coding-system-p 'utf-8))
774 (let ((mm-coding-system-priorities
775 (cons 'utf-8 mm-coding-system-priorities)))
777 (mm-delete-duplicates
778 (mapcar 'mm-mime-charset
780 (mm-find-charset-region b e)))))))
783 (defmacro mm-with-unibyte-buffer (&rest forms)
784 "Create a temporary buffer, and evaluate FORMS there like `progn'.
785 Use unibyte mode for this."
786 `(let (default-enable-multibyte-characters)
787 (with-temp-buffer ,@forms)))
788 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
789 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
791 (defmacro mm-with-multibyte-buffer (&rest forms)
792 "Create a temporary buffer, and evaluate FORMS there like `progn'.
793 Use multibyte mode for this."
794 `(let ((default-enable-multibyte-characters t))
795 (with-temp-buffer ,@forms)))
796 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
797 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
799 (defmacro mm-with-unibyte-current-buffer (&rest forms)
800 "Evaluate FORMS with current buffer temporarily made unibyte.
801 Also bind `default-enable-multibyte-characters' to nil.
802 Equivalent to `progn' in XEmacs"
803 (let ((multibyte (make-symbol "multibyte"))
804 (buffer (make-symbol "buffer")))
806 (let ((,multibyte enable-multibyte-characters)
807 (,buffer (current-buffer)))
809 (let (default-enable-multibyte-characters)
810 (set-buffer-multibyte nil)
813 (set-buffer-multibyte ,multibyte)))
814 (let (default-enable-multibyte-characters)
816 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
817 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
819 (defmacro mm-with-unibyte (&rest forms)
820 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
821 `(let (default-enable-multibyte-characters)
823 (put 'mm-with-unibyte 'lisp-indent-function 0)
824 (put 'mm-with-unibyte 'edebug-form-spec '(body))
826 (defmacro mm-with-multibyte (&rest forms)
827 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
828 `(let ((default-enable-multibyte-characters t))
830 (put 'mm-with-multibyte 'lisp-indent-function 0)
831 (put 'mm-with-multibyte 'edebug-form-spec '(body))
833 (defun mm-find-charset-region (b e)
834 "Return a list of Emacs charsets in the region B to E."
836 ((and (mm-multibyte-p)
837 (fboundp 'find-charset-region))
838 ;; Remove composition since the base charsets have been included.
839 ;; Remove eight-bit-*, treat them as ascii.
840 (let ((css (find-charset-region b e)))
841 (mapcar (lambda (cs) (setq css (delq cs css)))
842 '(composition eight-bit-control eight-bit-graphic
846 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
849 (narrow-to-region b e)
850 (goto-char (point-min))
851 (skip-chars-forward "\0-\177")
856 (and (boundp 'current-language-environment)
857 (car (last (assq 'charset
858 (assoc current-language-environment
859 language-info-alist))))))
860 (if (eq charset 'ascii) (setq charset nil))
863 (car (last (assq mail-parse-charset
864 mm-mime-mule-charset-alist)))))
865 (list 'ascii (or charset 'latin-iso8859-1)))))))))
867 (if (fboundp 'shell-quote-argument)
868 (defalias 'mm-quote-arg 'shell-quote-argument)
869 (defun mm-quote-arg (arg)
870 "Return a version of ARG that is safe to evaluate in a shell."
871 (let ((pos 0) new-pos accum)
872 ;; *** bug: we don't handle newline characters properly
873 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
874 (push (substring arg pos new-pos) accum)
876 (push (list (aref arg new-pos)) accum)
877 (setq pos (1+ new-pos)))
880 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
882 (defun mm-auto-mode-alist ()
883 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
884 (let ((alist auto-mode-alist)
887 (when (listp (cdar alist))
888 (push (car alist) out))
892 (defvar mm-inhibit-file-name-handlers
893 '(jka-compr-handler image-file-handler)
894 "A list of handlers doing (un)compression (etc) thingies.")
896 (defun mm-insert-file-contents (filename &optional visit beg end replace
898 "Like `insert-file-contents', but only reads in the file.
899 A buffer may be modified in several ways after reading into the buffer due
900 to advanced Emacs features, such as file-name-handlers, format decoding,
901 `find-file-hooks', etc.
902 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
903 This function ensures that none of these modifications will take place."
904 (let* ((format-alist nil)
905 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
906 (default-major-mode 'fundamental-mode)
907 (enable-local-variables nil)
908 (after-insert-file-functions nil)
909 (enable-local-eval nil)
910 (inhibit-file-name-operation (if inhibit
911 'insert-file-contents
912 inhibit-file-name-operation))
913 (inhibit-file-name-handlers
915 (append mm-inhibit-file-name-handlers
916 inhibit-file-name-handlers)
917 inhibit-file-name-handlers))
918 (ffh (if (boundp 'find-file-hook)
921 (val (symbol-value ffh)))
924 (insert-file-contents filename visit beg end replace)
927 (defun mm-append-to-file (start end filename &optional codesys inhibit)
928 "Append the contents of the region to the end of file FILENAME.
929 When called from a function, expects three arguments,
930 START, END and FILENAME. START and END are buffer positions
931 saying what text to write.
932 Optional fourth argument specifies the coding system to use when
934 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
935 (let ((coding-system-for-write
936 (or codesys mm-text-coding-system-for-write
937 mm-text-coding-system))
938 (inhibit-file-name-operation (if inhibit
940 inhibit-file-name-operation))
941 (inhibit-file-name-handlers
943 (append mm-inhibit-file-name-handlers
944 inhibit-file-name-handlers)
945 inhibit-file-name-handlers)))
946 (write-region start end filename t 'no-message)
947 (message "Appended to %s" filename)))
949 (defun mm-write-region (start end filename &optional append visit lockname
950 coding-system inhibit)
952 "Like `write-region'.
953 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
954 (let ((coding-system-for-write
955 (or coding-system mm-text-coding-system-for-write
956 mm-text-coding-system))
957 (inhibit-file-name-operation (if inhibit
959 inhibit-file-name-operation))
960 (inhibit-file-name-handlers
962 (append mm-inhibit-file-name-handlers
963 inhibit-file-name-handlers)
964 inhibit-file-name-handlers)))
965 (write-region start end filename append visit lockname)))
967 (defun mm-image-load-path (&optional package)
969 (dolist (path load-path (nreverse result))
972 (setq dir (concat (file-name-directory
973 (directory-file-name path))
974 "etc/images/" (or package "gnus/")))))
976 (push path result))))
978 ;; Fixme: This doesn't look useful where it's used.
979 (if (fboundp 'detect-coding-region)
980 (defun mm-detect-coding-region (start end)
981 "Like `detect-coding-region' except returning the best one."
982 (let ((coding-systems
983 (detect-coding-region start end)))
984 (or (car-safe coding-systems)
986 (defun mm-detect-coding-region (start end)
987 (let ((point (point)))
989 (skip-chars-forward "\0-\177" end)
991 (if (eq (point) end) 'ascii (mm-guess-charset))
992 (goto-char point)))))
994 (if (fboundp 'coding-system-get)
995 (defun mm-detect-mime-charset-region (start end)
996 "Detect MIME charset of the text in the region between START and END."
997 (let ((cs (mm-detect-coding-region start end)))
998 (coding-system-get cs 'mime-charset)))
999 (defun mm-detect-mime-charset-region (start end)
1000 "Detect MIME charset of the text in the region between START and END."
1001 (let ((cs (mm-detect-coding-region start end)))
1007 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1008 ;;; mm-util.el ends here