1 ;;; mm-util.el --- Utility functions for Mule and low level things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
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)
44 (coding-system-equal . equal)
45 (annotationp . ignore)
46 (set-buffer-file-coding-system . ignore)
48 . (lambda (charset int)
56 (mapcar (lambda (e) (list (symbol-name (car e))))
57 mm-mime-mule-charset-alist)
60 . (lambda (from to string &optional inplace)
61 ;; stolen (and renamed) from nnheader.el
62 "Replace characters in STRING from FROM to TO.
63 Unless optional argument INPLACE is non-nil, return a new string."
64 (let ((string (if inplace string (copy-sequence string)))
67 ;; Replace all occurrences of FROM with TO.
69 (when (= (aref string idx) from)
73 (string-as-unibyte . identity)
74 (string-make-unibyte . identity)
75 ;; string-as-multibyte often doesn't really do what you think it does.
77 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
78 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
79 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
80 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
82 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
83 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
84 ;; Better use string-to-multibyte or encode-coding-string.
85 ;; If you really need string-as-multibyte somewhere it's usually
86 ;; because you're using the internal emacs-mule representation (maybe
87 ;; because you're using string-as-unibyte somewhere), which is
88 ;; generally a problem in itself.
89 ;; Here is an approximate equivalence table to help think about it:
90 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
91 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
92 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
93 (string-as-multibyte . identity)
96 "Return a multibyte string with the same individual chars as string."
98 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
100 (multibyte-string-p . ignore)
101 ;; It is not a MIME function, but some MIME functions use it.
102 (make-temp-file . (lambda (prefix &optional dir-flag)
103 (let ((file (expand-file-name
104 (make-temp-name prefix)
105 (if (fboundp 'temp-directory)
107 temporary-file-directory))))
109 (make-directory file))
111 (insert-byte . insert-char)
112 (multibyte-char-to-unibyte . identity))))
116 ((fboundp 'replace-in-string)
117 (defalias 'mm-replace-in-string 'replace-in-string))
118 ((fboundp 'replace-regexp-in-string)
119 (defun mm-replace-in-string (string regexp newtext &optional literal)
120 "Replace all matches for REGEXP with NEWTEXT in STRING.
121 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
122 string containing the replacements.
124 This is a compatibility function for different Emacsen."
125 (replace-regexp-in-string regexp newtext string nil literal)))
127 (defun mm-replace-in-string (string regexp newtext &optional literal)
128 "Replace all matches for REGEXP with NEWTEXT in STRING.
129 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
130 string containing the replacements.
132 This is a compatibility function for different Emacsen."
133 (let ((start 0) tail)
134 (while (string-match regexp string start)
135 (setq tail (- (length string) (match-end 0)))
136 (setq string (replace-match newtext nil literal string))
137 (setq start (- (length string) tail))))
141 (defalias 'mm-char-or-char-int-p
143 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
144 ((fboundp 'char-valid-p) 'char-valid-p)
147 ;; Fixme: This seems always to be used to read a MIME charset, so it
148 ;; should be re-named and fixed (in Emacs) to offer completion only on
149 ;; proper charset names (base coding systems which have a
150 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
152 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
153 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
154 ;; Actually, there should be an `mm-coding-system-mime-charset'.
156 (defalias 'mm-read-coding-system
158 ((fboundp 'read-coding-system)
159 (if (and (featurep 'xemacs)
160 (<= (string-to-number emacs-version) 21.1))
161 (lambda (prompt &optional default-coding-system)
162 (read-coding-system prompt))
163 'read-coding-system))
164 (t (lambda (prompt &optional default-coding-system)
165 "Prompt the user for a coding system."
167 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
168 mm-mime-mule-charset-alist)))))))
170 (defvar mm-coding-system-list nil)
171 (defun mm-get-coding-system-list ()
172 "Get the coding system list."
173 (or mm-coding-system-list
174 (setq mm-coding-system-list (mm-coding-system-list))))
176 (defun mm-coding-system-p (cs)
177 "Return non-nil if CS is a symbol naming a coding system.
178 In XEmacs, also return non-nil if CS is a coding system object.
179 If CS is available, return CS itself in Emacs, and return a coding
180 system object in XEmacs."
181 (if (fboundp 'find-coding-system)
182 (and cs (find-coding-system cs))
183 (if (fboundp 'coding-system-p)
184 (when (coding-system-p cs)
186 ;; Is this branch ever actually useful?
187 (car (memq cs (mm-get-coding-system-list))))))
189 (defvar mm-charset-synonym-alist
191 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
192 ,@(unless (mm-coding-system-p 'x-ctext)
193 '((x-ctext . ctext)))
194 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
195 ,@(unless (mm-coding-system-p 'iso-8859-15)
196 '((iso-8859-15 . iso-8859-1)))
197 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
198 ,@(unless (mm-coding-system-p 'big5-hkscs)
199 '((big5-hkscs . big5)))
200 ;; Windows-1252 is actually a superset of Latin-1. See also
201 ;; `gnus-article-dumbquotes-map'.
202 ,@(unless (mm-coding-system-p 'windows-1252)
203 (if (mm-coding-system-p 'cp1252)
204 '((windows-1252 . cp1252))
205 '((windows-1252 . iso-8859-1))))
206 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
207 ;; Outlook users in Czech republic. Use this to allow reading of their
208 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
209 ,@(if (and (not (mm-coding-system-p 'windows-1250))
210 (mm-coding-system-p 'cp1250))
211 '((windows-1250 . cp1250)))
213 "A mapping from invalid charset names to the real charset names.")
215 (defvar mm-binary-coding-system
217 ((mm-coding-system-p 'binary) 'binary)
218 ((mm-coding-system-p 'no-conversion) 'no-conversion)
220 "100% binary coding system.")
222 (defvar mm-text-coding-system
223 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
224 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
225 (and (mm-coding-system-p 'raw-text) 'raw-text))
226 mm-binary-coding-system)
227 "Text-safe coding system (For removing ^M).")
229 (defvar mm-text-coding-system-for-write nil
230 "Text coding system for write.")
232 (defvar mm-auto-save-coding-system
234 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
235 (if (memq system-type '(windows-nt ms-dos ms-windows))
236 (if (mm-coding-system-p 'utf-8-emacs-dos)
237 'utf-8-emacs-dos mm-binary-coding-system)
239 ((mm-coding-system-p 'emacs-mule)
240 (if (memq system-type '(windows-nt ms-dos ms-windows))
241 (if (mm-coding-system-p 'emacs-mule-dos)
242 'emacs-mule-dos mm-binary-coding-system)
244 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
245 (t mm-binary-coding-system))
246 "Coding system of auto save file.")
248 (defvar mm-universal-coding-system mm-auto-save-coding-system
249 "The universal coding system.")
251 ;; Fixme: some of the cars here aren't valid MIME charsets. That
252 ;; should only matter with XEmacs, though.
253 (defvar mm-mime-mule-charset-alist
255 (iso-8859-1 latin-iso8859-1)
256 (iso-8859-2 latin-iso8859-2)
257 (iso-8859-3 latin-iso8859-3)
258 (iso-8859-4 latin-iso8859-4)
259 (iso-8859-5 cyrillic-iso8859-5)
260 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
261 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
262 ;; charset is koi8-r, not iso-8859-5.
263 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
264 (iso-8859-6 arabic-iso8859-6)
265 (iso-8859-7 greek-iso8859-7)
266 (iso-8859-8 hebrew-iso8859-8)
267 (iso-8859-9 latin-iso8859-9)
268 (iso-8859-14 latin-iso8859-14)
269 (iso-8859-15 latin-iso8859-15)
270 (viscii vietnamese-viscii-lower)
271 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
272 (euc-kr korean-ksc5601)
273 (gb2312 chinese-gb2312)
274 (big5 chinese-big5-1 chinese-big5-2)
276 (thai-tis620 thai-tis620)
277 (windows-1251 cyrillic-iso8859-5)
278 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
279 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
280 latin-jisx0201 japanese-jisx0208-1978
281 chinese-gb2312 japanese-jisx0208
282 korean-ksc5601 japanese-jisx0212)
283 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
284 latin-jisx0201 japanese-jisx0208-1978
285 chinese-gb2312 japanese-jisx0208
286 korean-ksc5601 japanese-jisx0212
287 chinese-cns11643-1 chinese-cns11643-2)
288 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
289 cyrillic-iso8859-5 greek-iso8859-7
290 latin-jisx0201 japanese-jisx0208-1978
291 chinese-gb2312 japanese-jisx0208
292 korean-ksc5601 japanese-jisx0212
293 chinese-cns11643-1 chinese-cns11643-2
294 chinese-cns11643-3 chinese-cns11643-4
295 chinese-cns11643-5 chinese-cns11643-6
297 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
298 japanese-jisx0213-1 japanese-jisx0213-2)
299 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
300 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
301 (charsetp 'unicode-a)
302 (not (mm-coding-system-p 'mule-utf-8)))
303 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
304 ;; If we have utf-8 we're in Mule 5+.
307 (coding-system-get 'mule-utf-8 'safe-charsets)))))
308 "Alist of MIME-charset/MULE-charsets.")
310 (defun mm-enrich-utf-8-by-mule-ucs ()
311 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
312 This function will run when the `un-define' module is loaded under
313 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
314 with Mule charsets. It is completely useless for Emacs."
315 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
316 (assoc "un-define" after-load-alist)))
317 (setq after-load-alist
318 (delete '("un-define") after-load-alist)))
319 (when (boundp 'unicode-basic-translation-charset-order-list)
325 'unicode-basic-translation-charset-order-list))))
326 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
329 (setq mm-mime-mule-charset-alist
330 (nconc mm-mime-mule-charset-alist
331 (list (cons 'utf-8 val))))))
334 ;; Correct by construction, but should be unnecessary for Emacs:
335 (if (featurep 'xemacs)
336 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
337 (when (and (fboundp 'coding-system-list)
338 (fboundp 'sort-coding-systems))
339 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
343 mime (or (coding-system-get cs :mime-charset) ; Emacs 22
344 (coding-system-get cs 'mime-charset)))
346 (not (eq t (setq mule
347 (coding-system-get cs 'safe-charsets))))
348 (not (assq mime alist)))
349 (push (cons mime (delq 'ascii mule)) alist)))
350 (setq mm-mime-mule-charset-alist (nreverse alist)))))
352 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
353 "A list of special charsets.
354 Valid elements include:
355 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
356 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
359 (defvar mm-iso-8859-15-compatible
360 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
361 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
362 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
364 (defvar mm-iso-8859-x-to-15-table
365 (and (fboundp 'coding-system-p)
366 (mm-coding-system-p 'iso-8859-15)
369 (if (mm-coding-system-p (car cs))
370 (let ((c (string-to-char
371 (decode-coding-string "\341" (car cs)))))
372 (cons (char-charset c)
375 (decode-coding-string "\341" 'iso-8859-15)) c)
376 (string-to-list (decode-coding-string (car (cdr cs))
379 mm-iso-8859-15-compatible))
380 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
382 (defcustom mm-coding-system-priorities
383 (if (boundp 'current-language-environment)
384 (let ((lang (symbol-value 'current-language-environment)))
385 (cond ((string= lang "Japanese")
386 ;; Japanese users prefer iso-2022-jp to euc-japan or
387 ;; shift_jis, however iso-8859-1 should be used when
388 ;; there are only ASCII text and Latin-1 characters.
389 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
390 "Preferred coding systems for encoding outgoing messages.
392 More than one suitable coding system may be found for some text.
393 By default, the coding system with the highest priority is used
394 to encode outgoing messages (see `sort-coding-systems'). If this
395 variable is set, it overrides the default priority."
397 :type '(repeat (symbol :tag "Coding system"))
401 (defvar mm-use-find-coding-systems-region
402 (fboundp 'find-coding-systems-region)
403 "Use `find-coding-systems-region' to find proper coding systems.
405 Setting it to nil is useful on Emacsen supporting Unicode if sending
406 mail with multiple parts is preferred to sending a Unicode one.")
408 ;;; Internal variables:
412 (defun mm-mule-charset-to-mime-charset (charset)
413 "Return the MIME charset corresponding to the given Mule CHARSET."
414 (if (and (fboundp 'find-coding-systems-for-charsets)
415 (fboundp 'sort-coding-systems))
416 (let ((css (sort (sort-coding-systems
417 (find-coding-systems-for-charsets (list charset)))
418 'mm-sort-coding-systems-predicate))
420 (while (and (not mime)
422 (when (setq cs (pop css))
423 (setq mime (or (coding-system-get cs :mime-charset)
424 (coding-system-get cs 'mime-charset)))))
426 (let ((alist (mapcar (lambda (cs)
427 (assq cs mm-mime-mule-charset-alist))
428 (sort (mapcar 'car mm-mime-mule-charset-alist)
429 'mm-sort-coding-systems-predicate)))
432 (when (memq charset (cdar alist))
433 (setq out (caar alist)
438 (defun mm-charset-to-coding-system (charset &optional lbt)
439 "Return coding-system corresponding to CHARSET.
440 CHARSET is a symbol naming a MIME charset.
441 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
442 used as the line break code type of the coding system."
443 (when (stringp charset)
444 (setq charset (intern (downcase charset))))
446 (setq charset (intern (format "%s-%s" charset lbt))))
450 ;; Running in a non-MULE environment.
451 ((or (null (mm-get-coding-system-list))
452 (not (fboundp 'coding-system-get)))
455 ((eq charset 'us-ascii)
457 ;; Check to see whether we can handle this charset. (This depends
458 ;; on there being some coding system matching each `mime-charset'
459 ;; property defined, as there should be.)
460 ((and (mm-coding-system-p charset)
461 ;;; Doing this would potentially weed out incorrect charsets.
463 ;;; (eq charset (coding-system-get charset 'mime-charset))
466 ;; Translate invalid charsets.
467 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
468 (and cs (mm-coding-system-p cs) cs)))
469 ;; Last resort: search the coding system list for entries which
470 ;; have the right mime-charset in case the canonical name isn't
471 ;; defined (though it should be).
473 ;; mm-get-coding-system-list returns a list of cs without lbt.
475 (dolist (c (mm-get-coding-system-list))
477 (eq charset (or (coding-system-get c :mime-charset)
478 (coding-system-get c 'mime-charset))))
482 (defsubst mm-replace-chars-in-string (string from to)
483 (mm-subst-char-in-string from to string))
486 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
487 (boundp 'default-enable-multibyte-characters)
488 default-enable-multibyte-characters
489 (fboundp 'set-buffer-multibyte))
490 "True in Emacs with Mule.")
493 (defun mm-enable-multibyte ()
494 "Set the multibyte flag of the current buffer.
495 Only do this if the default value of `enable-multibyte-characters' is
496 non-nil. This is a no-op in XEmacs."
497 (set-buffer-multibyte 'to))
498 (defalias 'mm-enable-multibyte 'ignore))
501 (defun mm-disable-multibyte ()
502 "Unset the multibyte flag of in the current buffer.
503 This is a no-op in XEmacs."
504 (set-buffer-multibyte nil))
505 (defalias 'mm-disable-multibyte 'ignore)))
507 (defun mm-preferred-coding-system (charset)
508 ;; A typo in some Emacs versions.
509 (or (get-charset-property charset 'preferred-coding-system)
510 (get-charset-property charset 'prefered-coding-system)))
512 ;; Mule charsets shouldn't be used.
513 (defsubst mm-guess-charset ()
514 "Guess Mule charset from the language environment."
516 mail-parse-mule-charset ;; cached mule-charset
518 (setq mail-parse-mule-charset
519 (and (boundp 'current-language-environment)
522 (assoc current-language-environment
523 language-info-alist))))))
524 (if (or (not mail-parse-mule-charset)
525 (eq mail-parse-mule-charset 'ascii))
526 (setq mail-parse-mule-charset
527 (or (car (last (assq mail-parse-charset
528 mm-mime-mule-charset-alist)))
531 mail-parse-mule-charset)))
533 (defun mm-charset-after (&optional pos)
534 "Return charset of a character in current buffer at position POS.
535 If POS is nil, it defauls to the current point.
536 If POS is out of range, the value is nil.
537 If the charset is `composition', return the actual one."
538 (let ((char (char-after pos)) charset)
539 (if (< (mm-char-int char) 128)
540 (setq charset 'ascii)
541 ;; charset-after is fake in some Emacsen.
542 (setq charset (and (fboundp 'char-charset) (char-charset char)))
543 (if (eq charset 'composition) ; Mule 4
544 (let ((p (or pos (point))))
545 (cadr (find-charset-region p (1+ p))))
546 (if (and charset (not (memq charset '(ascii eight-bit-control
547 eight-bit-graphic))))
549 (mm-guess-charset))))))
551 (defun mm-mime-charset (charset)
552 "Return the MIME charset corresponding to the given Mule CHARSET."
553 (if (eq charset 'unknown)
554 (error "The message contains non-printable characters, please use attachment"))
555 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
556 ;; This exists in Emacs 20.
558 (and (mm-preferred-coding-system charset)
559 (or (coding-system-get
560 (mm-preferred-coding-system charset) :mime-charset)
562 (mm-preferred-coding-system charset) 'mime-charset)))
563 (and (eq charset 'ascii)
565 (mm-preferred-coding-system charset)
566 (mm-mule-charset-to-mime-charset charset))
567 ;; This is for XEmacs.
568 (mm-mule-charset-to-mime-charset charset)))
570 (defun mm-delete-duplicates (list)
571 "Simple substitute for CL `delete-duplicates', testing with `equal'."
574 (setq head (car list))
575 (setq list (delete head list))
576 (setq result (cons head result)))
579 ;; Fixme: This is used in places when it should be testing the
580 ;; default multibyteness. See mm-default-multibyte-p.
582 (if (and (not (featurep 'xemacs))
583 (boundp 'enable-multibyte-characters))
584 (defun mm-multibyte-p ()
585 "Non-nil if multibyte is enabled in the current buffer."
586 enable-multibyte-characters)
587 (defun mm-multibyte-p () (featurep 'mule))))
589 (defun mm-default-multibyte-p ()
590 "Return non-nil if the session is multibyte.
591 This affects whether coding conversion should be attempted generally."
593 (if (boundp 'default-enable-multibyte-characters)
594 default-enable-multibyte-characters
597 (defun mm-iso-8859-x-to-15-region (&optional b e)
598 (if (fboundp 'char-charset)
599 (let (charset item c inconvertible)
601 (if e (narrow-to-region b e))
602 (goto-char (point-min))
603 (skip-chars-forward "\0-\177")
606 ((not (setq item (assq (char-charset (setq c (char-after)))
607 mm-iso-8859-x-to-15-table)))
609 ((memq c (cdr (cdr item)))
610 (setq inconvertible t)
613 (insert-before-markers (prog1 (+ c (car (cdr item)))
615 (skip-chars-forward "\0-\177")))
616 (not inconvertible))))
618 (defun mm-sort-coding-systems-predicate (a b)
621 ;; Note: invalid entries are dropped silently
622 (and (setq cs (mm-coding-system-p cs))
623 (coding-system-base cs)))
624 mm-coding-system-priorities)))
625 (and (setq a (mm-coding-system-p a))
626 (if (setq b (mm-coding-system-p b))
627 (> (length (memq (coding-system-base a) priorities))
628 (length (memq (coding-system-base b) priorities)))
632 (autoload 'latin-unity-massage-name "latin-unity")
633 (autoload 'latin-unity-maybe-remap "latin-unity")
634 (autoload 'latin-unity-representations-feasible-region "latin-unity")
635 (autoload 'latin-unity-representations-present-region "latin-unity")
636 (defvar latin-unity-coding-systems)
637 (defvar latin-unity-ucs-list))
639 (defun mm-xemacs-find-mime-charset-1 (begin end)
640 "Determine which MIME charset to use to send region as message.
641 This uses the XEmacs-specific latin-unity package to better handle the
642 case where identical characters from diverse ISO-8859-? character sets
643 can be encoded using a single one of the corresponding coding systems.
645 It treats `mm-coding-system-priorities' as the list of preferred
646 coding systems; a useful example setting for this list in Western
647 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
648 to the very standard Latin 1 coding system, and only move to coding
649 systems that are less supported as is necessary to encode the
650 characters that exist in the buffer.
652 Latin Unity doesn't know about those non-ASCII Roman characters that
653 are available in various East Asian character sets. As such, its
654 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
655 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
656 But this is very much a corner case, so don't worry about it."
657 (let ((systems mm-coding-system-priorities) csets psets curset)
659 ;; Load the Latin Unity library, if available.
660 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
661 (ignore-errors (require 'latin-unity)))
663 ;; Now, can we use it?
664 (if (featurep 'latin-unity)
666 (setq csets (latin-unity-representations-feasible-region begin end)
667 psets (latin-unity-representations-present-region begin end))
671 ;; Pass back the first coding system in the preferred list
672 ;; that can encode the whole region.
673 (dolist (curset systems)
674 (setq curset (latin-unity-massage-name 'buffer-default curset))
676 ;; If the coding system is a universal coding system, then
677 ;; it can certainly encode all the characters in the region.
678 (if (memq curset latin-unity-ucs-list)
679 (throw 'done (list curset)))
681 ;; If a coding system isn't universal, and isn't in
682 ;; the list that latin unity knows about, we can't
683 ;; decide whether to use it here. Leave that until later
684 ;; in `mm-find-mime-charset-region' function, whence we
686 (unless (memq curset latin-unity-coding-systems)
689 ;; Right, we know about this coding system, and it may
690 ;; conceivably be able to encode all the characters in
692 (if (latin-unity-maybe-remap begin end curset csets psets t)
693 (throw 'done (list curset))))
695 ;; Can't encode using anything from the
696 ;; `mm-coding-system-priorities' list.
697 ;; Leave `mm-find-mime-charset' to do most of the work.
700 ;; Right, latin unity isn't available; let `mm-find-charset-region'
701 ;; take its default action, which equally applies to GNU Emacs.
704 (defmacro mm-xemacs-find-mime-charset (begin end)
705 (when (featurep 'xemacs)
706 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
708 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
709 "Return the MIME charsets needed to encode the region between B and E.
710 nil means ASCII, a single-element list represents an appropriate MIME
711 charset, and a longer list means no appropriate charset."
713 ;; The return possibilities of this function are a mess...
714 (or (and (mm-multibyte-p)
715 mm-use-find-coding-systems-region
716 ;; Find the mime-charset of the most preferred coding
717 ;; system that has one.
718 (let ((systems (find-coding-systems-region b e)))
719 (when mm-coding-system-priorities
721 (sort systems 'mm-sort-coding-systems-predicate)))
722 (setq systems (delq 'compound-text systems))
723 (unless (equal systems '(undecided))
725 (let* ((head (pop systems))
726 (cs (or (coding-system-get head :mime-charset)
727 (coding-system-get head 'mime-charset))))
728 ;; The mime-charset (`x-ctext') of
729 ;; `compound-text' is not in the IANA list. We
730 ;; shouldn't normally use anything here with a
731 ;; mime-charset having an `x-' prefix.
732 ;; Fixme: Allow this to be overridden, since
733 ;; there is existing use of x-ctext.
734 ;; Also people apparently need the coding system
735 ;; `iso-2022-jp-3' (which Mule-UCS defines with
736 ;; mime-charset, though it's not valid).
738 (not (string-match "^[Xx]-" (symbol-name cs)))
739 ;; UTF-16 of any variety is invalid for
740 ;; text parts and, unfortunately, has
741 ;; mime-charset defined both in Mule-UCS
742 ;; and versions of Emacs. (The name
743 ;; might be `mule-utf-16...' or
745 (not (string-match "utf-16" (symbol-name cs))))
747 charsets (list cs))))))
749 ;; If we're XEmacs, and some coding system is appropriate,
750 ;; mm-xemacs-find-mime-charset will return an appropriate list.
751 ;; Otherwise, we'll get nil, and the next setq will get invoked.
752 (setq charsets (mm-xemacs-find-mime-charset b e))
754 ;; We're not multibyte, or a single coding system won't cover it.
756 (mm-delete-duplicates
757 (mapcar 'mm-mime-charset
759 (mm-find-charset-region b e))))))
760 (if (and (> (length charsets) 1)
761 (memq 'iso-8859-15 charsets)
762 (memq 'iso-8859-15 hack-charsets)
763 (save-excursion (mm-iso-8859-x-to-15-region b e)))
764 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
765 mm-iso-8859-15-compatible))
766 (if (and (memq 'iso-2022-jp-2 charsets)
767 (memq 'iso-2022-jp-2 hack-charsets))
768 (setq charsets (delq 'iso-2022-jp charsets)))
771 (defmacro mm-with-unibyte-buffer (&rest forms)
772 "Create a temporary buffer, and evaluate FORMS there like `progn'.
773 Use unibyte mode for this."
774 `(let (default-enable-multibyte-characters)
775 (with-temp-buffer ,@forms)))
776 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
777 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
779 (defmacro mm-with-multibyte-buffer (&rest forms)
780 "Create a temporary buffer, and evaluate FORMS there like `progn'.
781 Use multibyte mode for this."
782 `(let ((default-enable-multibyte-characters t))
783 (with-temp-buffer ,@forms)))
784 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
785 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
787 (defmacro mm-with-unibyte-current-buffer (&rest forms)
788 "Evaluate FORMS with current buffer temporarily made unibyte.
789 Also bind `default-enable-multibyte-characters' to nil.
790 Equivalent to `progn' in XEmacs"
791 (let ((multibyte (make-symbol "multibyte"))
792 (buffer (make-symbol "buffer")))
794 (let ((,multibyte enable-multibyte-characters)
795 (,buffer (current-buffer)))
797 (let (default-enable-multibyte-characters)
798 (set-buffer-multibyte nil)
801 (set-buffer-multibyte ,multibyte)))
802 (let (default-enable-multibyte-characters)
804 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
805 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
807 (defmacro mm-with-unibyte (&rest forms)
808 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
809 `(let (default-enable-multibyte-characters)
811 (put 'mm-with-unibyte 'lisp-indent-function 0)
812 (put 'mm-with-unibyte 'edebug-form-spec '(body))
814 (defmacro mm-with-multibyte (&rest forms)
815 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
816 `(let ((default-enable-multibyte-characters t))
818 (put 'mm-with-multibyte 'lisp-indent-function 0)
819 (put 'mm-with-multibyte 'edebug-form-spec '(body))
821 (defun mm-find-charset-region (b e)
822 "Return a list of Emacs charsets in the region B to E."
824 ((and (mm-multibyte-p)
825 (fboundp 'find-charset-region))
826 ;; Remove composition since the base charsets have been included.
827 ;; Remove eight-bit-*, treat them as ascii.
828 (let ((css (find-charset-region b e)))
829 (mapcar (lambda (cs) (setq css (delq cs css)))
830 '(composition eight-bit-control eight-bit-graphic
834 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
837 (narrow-to-region b e)
838 (goto-char (point-min))
839 (skip-chars-forward "\0-\177")
844 (and (boundp 'current-language-environment)
845 (car (last (assq 'charset
846 (assoc current-language-environment
847 language-info-alist))))))
848 (if (eq charset 'ascii) (setq charset nil))
851 (car (last (assq mail-parse-charset
852 mm-mime-mule-charset-alist)))))
853 (list 'ascii (or charset 'latin-iso8859-1)))))))))
855 (if (fboundp 'shell-quote-argument)
856 (defalias 'mm-quote-arg 'shell-quote-argument)
857 (defun mm-quote-arg (arg)
858 "Return a version of ARG that is safe to evaluate in a shell."
859 (let ((pos 0) new-pos accum)
860 ;; *** bug: we don't handle newline characters properly
861 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
862 (push (substring arg pos new-pos) accum)
864 (push (list (aref arg new-pos)) accum)
865 (setq pos (1+ new-pos)))
868 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
870 (defun mm-auto-mode-alist ()
871 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
872 (let ((alist auto-mode-alist)
875 (when (listp (cdar alist))
876 (push (car alist) out))
880 (defvar mm-inhibit-file-name-handlers
881 '(jka-compr-handler image-file-handler)
882 "A list of handlers doing (un)compression (etc) thingies.")
884 (defun mm-insert-file-contents (filename &optional visit beg end replace
886 "Like `insert-file-contents', but only reads in the file.
887 A buffer may be modified in several ways after reading into the buffer due
888 to advanced Emacs features, such as file-name-handlers, format decoding,
889 `find-file-hooks', etc.
890 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
891 This function ensures that none of these modifications will take place."
892 (let* ((format-alist nil)
893 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
894 (default-major-mode 'fundamental-mode)
895 (enable-local-variables nil)
896 (after-insert-file-functions nil)
897 (enable-local-eval nil)
898 (inhibit-file-name-operation (if inhibit
899 'insert-file-contents
900 inhibit-file-name-operation))
901 (inhibit-file-name-handlers
903 (append mm-inhibit-file-name-handlers
904 inhibit-file-name-handlers)
905 inhibit-file-name-handlers))
906 (ffh (if (boundp 'find-file-hook)
909 (val (symbol-value ffh)))
912 (insert-file-contents filename visit beg end replace)
915 (defun mm-append-to-file (start end filename &optional codesys inhibit)
916 "Append the contents of the region to the end of file FILENAME.
917 When called from a function, expects three arguments,
918 START, END and FILENAME. START and END are buffer positions
919 saying what text to write.
920 Optional fourth argument specifies the coding system to use when
922 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
923 (let ((coding-system-for-write
924 (or codesys mm-text-coding-system-for-write
925 mm-text-coding-system))
926 (inhibit-file-name-operation (if inhibit
928 inhibit-file-name-operation))
929 (inhibit-file-name-handlers
931 (append mm-inhibit-file-name-handlers
932 inhibit-file-name-handlers)
933 inhibit-file-name-handlers)))
934 (write-region start end filename t 'no-message)
935 (message "Appended to %s" filename)))
937 (defun mm-write-region (start end filename &optional append visit lockname
938 coding-system inhibit)
940 "Like `write-region'.
941 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
942 (let ((coding-system-for-write
943 (or coding-system mm-text-coding-system-for-write
944 mm-text-coding-system))
945 (inhibit-file-name-operation (if inhibit
947 inhibit-file-name-operation))
948 (inhibit-file-name-handlers
950 (append mm-inhibit-file-name-handlers
951 inhibit-file-name-handlers)
952 inhibit-file-name-handlers)))
953 (write-region start end filename append visit lockname)))
955 (defun mm-image-load-path (&optional package)
957 (dolist (path load-path (nreverse result))
960 (setq dir (concat (file-name-directory
961 (directory-file-name path))
962 "etc/images/" (or package "gnus/")))))
964 (push path result))))
966 ;; Fixme: This doesn't look useful where it's used.
967 (if (fboundp 'detect-coding-region)
968 (defun mm-detect-coding-region (start end)
969 "Like `detect-coding-region' except returning the best one."
970 (let ((coding-systems
971 (detect-coding-region start end)))
972 (or (car-safe coding-systems)
974 (defun mm-detect-coding-region (start end)
975 (let ((point (point)))
977 (skip-chars-forward "\0-\177" end)
979 (if (eq (point) end) 'ascii (mm-guess-charset))
980 (goto-char point)))))
982 (if (fboundp 'coding-system-get)
983 (defun mm-detect-mime-charset-region (start end)
984 "Detect MIME charset of the text in the region between START and END."
985 (let ((cs (mm-detect-coding-region start end)))
986 (coding-system-get cs 'mime-charset)))
987 (defun mm-detect-mime-charset-region (start end)
988 "Detect MIME charset of the text in the region between START and END."
989 (let ((cs (mm-detect-coding-region start end)))
995 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
996 ;;; mm-util.el ends here