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)
94 (string-to-multibyte . mm-string-as-multibyte)
95 (multibyte-string-p . ignore)
96 ;; It is not a MIME function, but some MIME functions use it.
97 (make-temp-file . (lambda (prefix &optional dir-flag)
98 (let ((file (expand-file-name
99 (make-temp-name prefix)
100 (if (fboundp 'temp-directory)
102 temporary-file-directory))))
104 (make-directory file))
106 (insert-byte . insert-char)
107 (multibyte-char-to-unibyte . identity))))
111 ((fboundp 'replace-in-string)
112 (defalias 'mm-replace-in-string 'replace-in-string))
113 ((fboundp 'replace-regexp-in-string)
114 (defun mm-replace-in-string (string regexp newtext &optional literal)
115 "Replace all matches for REGEXP with NEWTEXT in STRING.
116 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
117 string containing the replacements.
119 This is a compatibility function for different Emacsen."
120 (replace-regexp-in-string regexp newtext string nil literal)))
122 (defun mm-replace-in-string (string regexp newtext &optional literal)
123 "Replace all matches for REGEXP with NEWTEXT in STRING.
124 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
125 string containing the replacements.
127 This is a compatibility function for different Emacsen."
128 (let ((start 0) tail)
129 (while (string-match regexp string start)
130 (setq tail (- (length string) (match-end 0)))
131 (setq string (replace-match newtext nil literal string))
132 (setq start (- (length string) tail))))
136 (defalias 'mm-char-or-char-int-p
138 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
139 ((fboundp 'char-valid-p) 'char-valid-p)
142 ;; Fixme: This seems always to be used to read a MIME charset, so it
143 ;; should be re-named and fixed (in Emacs) to offer completion only on
144 ;; proper charset names (base coding systems which have a
145 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
147 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
148 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
149 ;; Actually, there should be an `mm-coding-system-mime-charset'.
151 (defalias 'mm-read-coding-system
153 ((fboundp 'read-coding-system)
154 (if (and (featurep 'xemacs)
155 (<= (string-to-number emacs-version) 21.1))
156 (lambda (prompt &optional default-coding-system)
157 (read-coding-system prompt))
158 'read-coding-system))
159 (t (lambda (prompt &optional default-coding-system)
160 "Prompt the user for a coding system."
162 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
163 mm-mime-mule-charset-alist)))))))
165 (defvar mm-coding-system-list nil)
166 (defun mm-get-coding-system-list ()
167 "Get the coding system list."
168 (or mm-coding-system-list
169 (setq mm-coding-system-list (mm-coding-system-list))))
171 (defun mm-coding-system-p (cs)
172 "Return non-nil if CS is a symbol naming a coding system.
173 In XEmacs, also return non-nil if CS is a coding system object.
174 If CS is available, return CS itself in Emacs, and return a coding
175 system object in XEmacs."
176 (if (fboundp 'find-coding-system)
177 (and cs (find-coding-system cs))
178 (if (fboundp 'coding-system-p)
179 (when (coding-system-p cs)
181 ;; Is this branch ever actually useful?
182 (car (memq cs (mm-get-coding-system-list))))))
184 (defvar mm-charset-synonym-alist
186 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
187 ,@(unless (mm-coding-system-p 'x-ctext)
188 '((x-ctext . ctext)))
189 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
190 ,@(unless (mm-coding-system-p 'iso-8859-15)
191 '((iso-8859-15 . iso-8859-1)))
192 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
193 ,@(unless (mm-coding-system-p 'big5-hkscs)
194 '((big5-hkscs . big5)))
195 ;; Windows-1252 is actually a superset of Latin-1. See also
196 ;; `gnus-article-dumbquotes-map'.
197 ,@(unless (mm-coding-system-p 'windows-1252)
198 (if (mm-coding-system-p 'cp1252)
199 '((windows-1252 . cp1252))
200 '((windows-1252 . iso-8859-1))))
201 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
202 ;; Outlook users in Czech republic. Use this to allow reading of their
203 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
204 ,@(if (and (not (mm-coding-system-p 'windows-1250))
205 (mm-coding-system-p 'cp1250))
206 '((windows-1250 . cp1250)))
208 "A mapping from invalid charset names to the real charset names.")
210 (defvar mm-binary-coding-system
212 ((mm-coding-system-p 'binary) 'binary)
213 ((mm-coding-system-p 'no-conversion) 'no-conversion)
215 "100% binary coding system.")
217 (defvar mm-text-coding-system
218 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
219 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
220 (and (mm-coding-system-p 'raw-text) 'raw-text))
221 mm-binary-coding-system)
222 "Text-safe coding system (For removing ^M).")
224 (defvar mm-text-coding-system-for-write nil
225 "Text coding system for write.")
227 (defvar mm-auto-save-coding-system
229 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
230 (if (memq system-type '(windows-nt ms-dos ms-windows))
231 (if (mm-coding-system-p 'utf-8-emacs-dos)
232 'utf-8-emacs-dos mm-binary-coding-system)
234 ((mm-coding-system-p 'emacs-mule)
235 (if (memq system-type '(windows-nt ms-dos ms-windows))
236 (if (mm-coding-system-p 'emacs-mule-dos)
237 'emacs-mule-dos mm-binary-coding-system)
239 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
240 (t mm-binary-coding-system))
241 "Coding system of auto save file.")
243 (defvar mm-universal-coding-system mm-auto-save-coding-system
244 "The universal coding system.")
246 ;; Fixme: some of the cars here aren't valid MIME charsets. That
247 ;; should only matter with XEmacs, though.
248 (defvar mm-mime-mule-charset-alist
250 (iso-8859-1 latin-iso8859-1)
251 (iso-8859-2 latin-iso8859-2)
252 (iso-8859-3 latin-iso8859-3)
253 (iso-8859-4 latin-iso8859-4)
254 (iso-8859-5 cyrillic-iso8859-5)
255 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
256 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
257 ;; charset is koi8-r, not iso-8859-5.
258 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
259 (iso-8859-6 arabic-iso8859-6)
260 (iso-8859-7 greek-iso8859-7)
261 (iso-8859-8 hebrew-iso8859-8)
262 (iso-8859-9 latin-iso8859-9)
263 (iso-8859-14 latin-iso8859-14)
264 (iso-8859-15 latin-iso8859-15)
265 (viscii vietnamese-viscii-lower)
266 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
267 (euc-kr korean-ksc5601)
268 (gb2312 chinese-gb2312)
269 (big5 chinese-big5-1 chinese-big5-2)
271 (thai-tis620 thai-tis620)
272 (windows-1251 cyrillic-iso8859-5)
273 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
274 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
275 latin-jisx0201 japanese-jisx0208-1978
276 chinese-gb2312 japanese-jisx0208
277 korean-ksc5601 japanese-jisx0212)
278 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
279 latin-jisx0201 japanese-jisx0208-1978
280 chinese-gb2312 japanese-jisx0208
281 korean-ksc5601 japanese-jisx0212
282 chinese-cns11643-1 chinese-cns11643-2)
283 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
284 cyrillic-iso8859-5 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 chinese-cns11643-3 chinese-cns11643-4
290 chinese-cns11643-5 chinese-cns11643-6
292 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
293 japanese-jisx0213-1 japanese-jisx0213-2)
294 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
295 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
296 (charsetp 'unicode-a)
297 (not (mm-coding-system-p 'mule-utf-8)))
298 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
299 ;; If we have utf-8 we're in Mule 5+.
302 (coding-system-get 'mule-utf-8 'safe-charsets)))))
303 "Alist of MIME-charset/MULE-charsets.")
305 (defun mm-enrich-utf-8-by-mule-ucs ()
306 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
307 This function will run when the `un-define' module is loaded under
308 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
309 with Mule charsets. It is completely useless for Emacs."
310 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
311 (assoc "un-define" after-load-alist)))
312 (setq after-load-alist
313 (delete '("un-define") after-load-alist)))
314 (when (boundp 'unicode-basic-translation-charset-order-list)
320 'unicode-basic-translation-charset-order-list))))
321 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
324 (setq mm-mime-mule-charset-alist
325 (nconc mm-mime-mule-charset-alist
326 (list (cons 'utf-8 val))))))
329 ;; Correct by construction, but should be unnecessary for Emacs:
330 (if (featurep 'xemacs)
331 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
332 (when (and (fboundp 'coding-system-list)
333 (fboundp 'sort-coding-systems))
334 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
338 mime (or (coding-system-get cs :mime-charset) ; Emacs 22
339 (coding-system-get cs 'mime-charset)))
341 (not (eq t (setq mule
342 (coding-system-get cs 'safe-charsets))))
343 (not (assq mime alist)))
344 (push (cons mime (delq 'ascii mule)) alist)))
345 (setq mm-mime-mule-charset-alist (nreverse alist)))))
347 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
348 "A list of special charsets.
349 Valid elements include:
350 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
351 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
354 (defvar mm-iso-8859-15-compatible
355 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
356 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
357 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
359 (defvar mm-iso-8859-x-to-15-table
360 (and (fboundp 'coding-system-p)
361 (mm-coding-system-p 'iso-8859-15)
364 (if (mm-coding-system-p (car cs))
365 (let ((c (string-to-char
366 (decode-coding-string "\341" (car cs)))))
367 (cons (char-charset c)
370 (decode-coding-string "\341" 'iso-8859-15)) c)
371 (string-to-list (decode-coding-string (car (cdr cs))
374 mm-iso-8859-15-compatible))
375 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
377 (defcustom mm-coding-system-priorities
378 (if (boundp 'current-language-environment)
379 (let ((lang (symbol-value 'current-language-environment)))
380 (cond ((string= lang "Japanese")
381 ;; Japanese users prefer iso-2022-jp to euc-japan or
382 ;; shift_jis, however iso-8859-1 should be used when
383 ;; there are only ASCII text and Latin-1 characters.
384 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
385 "Preferred coding systems for encoding outgoing messages.
387 More than one suitable coding system may be found for some text.
388 By default, the coding system with the highest priority is used
389 to encode outgoing messages (see `sort-coding-systems'). If this
390 variable is set, it overrides the default priority."
392 :type '(repeat (symbol :tag "Coding system"))
396 (defvar mm-use-find-coding-systems-region
397 (fboundp 'find-coding-systems-region)
398 "Use `find-coding-systems-region' to find proper coding systems.
400 Setting it to nil is useful on Emacsen supporting Unicode if sending
401 mail with multiple parts is preferred to sending a Unicode one.")
403 ;;; Internal variables:
407 (defun mm-mule-charset-to-mime-charset (charset)
408 "Return the MIME charset corresponding to the given Mule CHARSET."
409 (if (and (fboundp 'find-coding-systems-for-charsets)
410 (fboundp 'sort-coding-systems))
411 (let ((css (sort (sort-coding-systems
412 (find-coding-systems-for-charsets (list charset)))
413 'mm-sort-coding-systems-predicate))
415 (while (and (not mime)
417 (when (setq cs (pop css))
418 (setq mime (or (coding-system-get cs :mime-charset)
419 (coding-system-get cs 'mime-charset)))))
421 (let ((alist (mapcar (lambda (cs)
422 (assq cs mm-mime-mule-charset-alist))
423 (sort (mapcar 'car mm-mime-mule-charset-alist)
424 'mm-sort-coding-systems-predicate)))
427 (when (memq charset (cdar alist))
428 (setq out (caar alist)
433 (defun mm-charset-to-coding-system (charset &optional lbt)
434 "Return coding-system corresponding to CHARSET.
435 CHARSET is a symbol naming a MIME charset.
436 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
437 used as the line break code type of the coding system."
438 (when (stringp charset)
439 (setq charset (intern (downcase charset))))
441 (setq charset (intern (format "%s-%s" charset lbt))))
445 ;; Running in a non-MULE environment.
446 ((or (null (mm-get-coding-system-list))
447 (not (fboundp 'coding-system-get)))
450 ((eq charset 'us-ascii)
452 ;; Check to see whether we can handle this charset. (This depends
453 ;; on there being some coding system matching each `mime-charset'
454 ;; property defined, as there should be.)
455 ((and (mm-coding-system-p charset)
456 ;;; Doing this would potentially weed out incorrect charsets.
458 ;;; (eq charset (coding-system-get charset 'mime-charset))
461 ;; Translate invalid charsets.
462 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
463 (and cs (mm-coding-system-p cs) cs)))
464 ;; Last resort: search the coding system list for entries which
465 ;; have the right mime-charset in case the canonical name isn't
466 ;; defined (though it should be).
468 ;; mm-get-coding-system-list returns a list of cs without lbt.
470 (dolist (c (mm-get-coding-system-list))
472 (eq charset (or (coding-system-get c :mime-charset)
473 (coding-system-get c 'mime-charset))))
477 (defsubst mm-replace-chars-in-string (string from to)
478 (mm-subst-char-in-string from to string))
481 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
482 (boundp 'default-enable-multibyte-characters)
483 default-enable-multibyte-characters
484 (fboundp 'set-buffer-multibyte))
485 "True in Emacs with Mule.")
488 (defun mm-enable-multibyte ()
489 "Set the multibyte flag of the current buffer.
490 Only do this if the default value of `enable-multibyte-characters' is
491 non-nil. This is a no-op in XEmacs."
492 (set-buffer-multibyte 'to))
493 (defalias 'mm-enable-multibyte 'ignore))
496 (defun mm-disable-multibyte ()
497 "Unset the multibyte flag of in the current buffer.
498 This is a no-op in XEmacs."
499 (set-buffer-multibyte nil))
500 (defalias 'mm-disable-multibyte 'ignore)))
502 (defun mm-preferred-coding-system (charset)
503 ;; A typo in some Emacs versions.
504 (or (get-charset-property charset 'preferred-coding-system)
505 (get-charset-property charset 'prefered-coding-system)))
507 ;; Mule charsets shouldn't be used.
508 (defsubst mm-guess-charset ()
509 "Guess Mule charset from the language environment."
511 mail-parse-mule-charset ;; cached mule-charset
513 (setq mail-parse-mule-charset
514 (and (boundp 'current-language-environment)
517 (assoc current-language-environment
518 language-info-alist))))))
519 (if (or (not mail-parse-mule-charset)
520 (eq mail-parse-mule-charset 'ascii))
521 (setq mail-parse-mule-charset
522 (or (car (last (assq mail-parse-charset
523 mm-mime-mule-charset-alist)))
526 mail-parse-mule-charset)))
528 (defun mm-charset-after (&optional pos)
529 "Return charset of a character in current buffer at position POS.
530 If POS is nil, it defauls to the current point.
531 If POS is out of range, the value is nil.
532 If the charset is `composition', return the actual one."
533 (let ((char (char-after pos)) charset)
534 (if (< (mm-char-int char) 128)
535 (setq charset 'ascii)
536 ;; charset-after is fake in some Emacsen.
537 (setq charset (and (fboundp 'char-charset) (char-charset char)))
538 (if (eq charset 'composition) ; Mule 4
539 (let ((p (or pos (point))))
540 (cadr (find-charset-region p (1+ p))))
541 (if (and charset (not (memq charset '(ascii eight-bit-control
542 eight-bit-graphic))))
544 (mm-guess-charset))))))
546 (defun mm-mime-charset (charset)
547 "Return the MIME charset corresponding to the given Mule CHARSET."
548 (if (eq charset 'unknown)
549 (error "The message contains non-printable characters, please use attachment"))
550 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
551 ;; This exists in Emacs 20.
553 (and (mm-preferred-coding-system charset)
554 (or (coding-system-get
555 (mm-preferred-coding-system charset) :mime-charset)
557 (mm-preferred-coding-system charset) 'mime-charset)))
558 (and (eq charset 'ascii)
560 (mm-preferred-coding-system charset)
561 (mm-mule-charset-to-mime-charset charset))
562 ;; This is for XEmacs.
563 (mm-mule-charset-to-mime-charset charset)))
565 (defun mm-delete-duplicates (list)
566 "Simple substitute for CL `delete-duplicates', testing with `equal'."
569 (setq head (car list))
570 (setq list (delete head list))
571 (setq result (cons head result)))
574 ;; Fixme: This is used in places when it should be testing the
575 ;; default multibyteness. See mm-default-multibyte-p.
577 (if (and (not (featurep 'xemacs))
578 (boundp 'enable-multibyte-characters))
579 (defun mm-multibyte-p ()
580 "Non-nil if multibyte is enabled in the current buffer."
581 enable-multibyte-characters)
582 (defun mm-multibyte-p () (featurep 'mule))))
584 (defun mm-default-multibyte-p ()
585 "Return non-nil if the session is multibyte.
586 This affects whether coding conversion should be attempted generally."
588 (if (boundp 'default-enable-multibyte-characters)
589 default-enable-multibyte-characters
592 (defun mm-iso-8859-x-to-15-region (&optional b e)
593 (if (fboundp 'char-charset)
594 (let (charset item c inconvertible)
596 (if e (narrow-to-region b e))
597 (goto-char (point-min))
598 (skip-chars-forward "\0-\177")
601 ((not (setq item (assq (char-charset (setq c (char-after)))
602 mm-iso-8859-x-to-15-table)))
604 ((memq c (cdr (cdr item)))
605 (setq inconvertible t)
608 (insert-before-markers (prog1 (+ c (car (cdr item)))
610 (skip-chars-forward "\0-\177")))
611 (not inconvertible))))
613 (defun mm-sort-coding-systems-predicate (a b)
616 ;; Note: invalid entries are dropped silently
617 (and (setq cs (mm-coding-system-p cs))
618 (coding-system-base cs)))
619 mm-coding-system-priorities)))
620 (and (setq a (mm-coding-system-p a))
621 (if (setq b (mm-coding-system-p b))
622 (> (length (memq (coding-system-base a) priorities))
623 (length (memq (coding-system-base b) priorities)))
627 (autoload 'latin-unity-massage-name "latin-unity")
628 (autoload 'latin-unity-maybe-remap "latin-unity")
629 (autoload 'latin-unity-representations-feasible-region "latin-unity")
630 (autoload 'latin-unity-representations-present-region "latin-unity")
631 (defvar latin-unity-coding-systems)
632 (defvar latin-unity-ucs-list))
634 (defun mm-xemacs-find-mime-charset-1 (begin end)
635 "Determine which MIME charset to use to send region as message.
636 This uses the XEmacs-specific latin-unity package to better handle the
637 case where identical characters from diverse ISO-8859-? character sets
638 can be encoded using a single one of the corresponding coding systems.
640 It treats `mm-coding-system-priorities' as the list of preferred
641 coding systems; a useful example setting for this list in Western
642 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
643 to the very standard Latin 1 coding system, and only move to coding
644 systems that are less supported as is necessary to encode the
645 characters that exist in the buffer.
647 Latin Unity doesn't know about those non-ASCII Roman characters that
648 are available in various East Asian character sets. As such, its
649 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
650 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
651 But this is very much a corner case, so don't worry about it."
652 (let ((systems mm-coding-system-priorities) csets psets curset)
654 ;; Load the Latin Unity library, if available.
655 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
656 (ignore-errors (require 'latin-unity)))
658 ;; Now, can we use it?
659 (if (featurep 'latin-unity)
661 (setq csets (latin-unity-representations-feasible-region begin end)
662 psets (latin-unity-representations-present-region begin end))
666 ;; Pass back the first coding system in the preferred list
667 ;; that can encode the whole region.
668 (dolist (curset systems)
669 (setq curset (latin-unity-massage-name 'buffer-default curset))
671 ;; If the coding system is a universal coding system, then
672 ;; it can certainly encode all the characters in the region.
673 (if (memq curset latin-unity-ucs-list)
674 (throw 'done (list curset)))
676 ;; If a coding system isn't universal, and isn't in
677 ;; the list that latin unity knows about, we can't
678 ;; decide whether to use it here. Leave that until later
679 ;; in `mm-find-mime-charset-region' function, whence we
681 (unless (memq curset latin-unity-coding-systems)
684 ;; Right, we know about this coding system, and it may
685 ;; conceivably be able to encode all the characters in
687 (if (latin-unity-maybe-remap begin end curset csets psets t)
688 (throw 'done (list curset))))
690 ;; Can't encode using anything from the
691 ;; `mm-coding-system-priorities' list.
692 ;; Leave `mm-find-mime-charset' to do most of the work.
695 ;; Right, latin unity isn't available; let `mm-find-charset-region'
696 ;; take its default action, which equally applies to GNU Emacs.
699 (defmacro mm-xemacs-find-mime-charset (begin end)
700 (when (featurep 'xemacs)
701 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
703 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
704 "Return the MIME charsets needed to encode the region between B and E.
705 nil means ASCII, a single-element list represents an appropriate MIME
706 charset, and a longer list means no appropriate charset."
708 ;; The return possibilities of this function are a mess...
709 (or (and (mm-multibyte-p)
710 mm-use-find-coding-systems-region
711 ;; Find the mime-charset of the most preferred coding
712 ;; system that has one.
713 (let ((systems (find-coding-systems-region b e)))
714 (when mm-coding-system-priorities
716 (sort systems 'mm-sort-coding-systems-predicate)))
717 (setq systems (delq 'compound-text systems))
718 (unless (equal systems '(undecided))
720 (let* ((head (pop systems))
721 (cs (or (coding-system-get head :mime-charset)
722 (coding-system-get head 'mime-charset))))
723 ;; The mime-charset (`x-ctext') of
724 ;; `compound-text' is not in the IANA list. We
725 ;; shouldn't normally use anything here with a
726 ;; mime-charset having an `x-' prefix.
727 ;; Fixme: Allow this to be overridden, since
728 ;; there is existing use of x-ctext.
729 ;; Also people apparently need the coding system
730 ;; `iso-2022-jp-3' (which Mule-UCS defines with
731 ;; mime-charset, though it's not valid).
733 (not (string-match "^[Xx]-" (symbol-name cs)))
734 ;; UTF-16 of any variety is invalid for
735 ;; text parts and, unfortunately, has
736 ;; mime-charset defined both in Mule-UCS
737 ;; and versions of Emacs. (The name
738 ;; might be `mule-utf-16...' or
740 (not (string-match "utf-16" (symbol-name cs))))
742 charsets (list cs))))))
744 ;; If we're XEmacs, and some coding system is appropriate,
745 ;; mm-xemacs-find-mime-charset will return an appropriate list.
746 ;; Otherwise, we'll get nil, and the next setq will get invoked.
747 (setq charsets (mm-xemacs-find-mime-charset b e))
749 ;; We're not multibyte, or a single coding system won't cover it.
751 (mm-delete-duplicates
752 (mapcar 'mm-mime-charset
754 (mm-find-charset-region b e))))))
755 (if (and (> (length charsets) 1)
756 (memq 'iso-8859-15 charsets)
757 (memq 'iso-8859-15 hack-charsets)
758 (save-excursion (mm-iso-8859-x-to-15-region b e)))
759 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
760 mm-iso-8859-15-compatible))
761 (if (and (memq 'iso-2022-jp-2 charsets)
762 (memq 'iso-2022-jp-2 hack-charsets))
763 (setq charsets (delq 'iso-2022-jp charsets)))
766 (defmacro mm-with-unibyte-buffer (&rest forms)
767 "Create a temporary buffer, and evaluate FORMS there like `progn'.
768 Use unibyte mode for this."
769 `(let (default-enable-multibyte-characters)
770 (with-temp-buffer ,@forms)))
771 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
772 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
774 (defmacro mm-with-multibyte-buffer (&rest forms)
775 "Create a temporary buffer, and evaluate FORMS there like `progn'.
776 Use multibyte mode for this."
777 `(let ((default-enable-multibyte-characters t))
778 (with-temp-buffer ,@forms)))
779 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
780 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
782 (defmacro mm-with-unibyte-current-buffer (&rest forms)
783 "Evaluate FORMS with current buffer temporarily made unibyte.
784 Also bind `default-enable-multibyte-characters' to nil.
785 Equivalent to `progn' in XEmacs"
786 (let ((multibyte (make-symbol "multibyte"))
787 (buffer (make-symbol "buffer")))
789 (let ((,multibyte enable-multibyte-characters)
790 (,buffer (current-buffer)))
792 (let (default-enable-multibyte-characters)
793 (set-buffer-multibyte nil)
796 (set-buffer-multibyte ,multibyte)))
797 (let (default-enable-multibyte-characters)
799 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
800 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
802 (defmacro mm-with-unibyte (&rest forms)
803 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
804 `(let (default-enable-multibyte-characters)
806 (put 'mm-with-unibyte 'lisp-indent-function 0)
807 (put 'mm-with-unibyte 'edebug-form-spec '(body))
809 (defmacro mm-with-multibyte (&rest forms)
810 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
811 `(let ((default-enable-multibyte-characters t))
813 (put 'mm-with-multibyte 'lisp-indent-function 0)
814 (put 'mm-with-multibyte 'edebug-form-spec '(body))
816 (defun mm-find-charset-region (b e)
817 "Return a list of Emacs charsets in the region B to E."
819 ((and (mm-multibyte-p)
820 (fboundp 'find-charset-region))
821 ;; Remove composition since the base charsets have been included.
822 ;; Remove eight-bit-*, treat them as ascii.
823 (let ((css (find-charset-region b e)))
824 (mapcar (lambda (cs) (setq css (delq cs css)))
825 '(composition eight-bit-control eight-bit-graphic
829 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
832 (narrow-to-region b e)
833 (goto-char (point-min))
834 (skip-chars-forward "\0-\177")
839 (and (boundp 'current-language-environment)
840 (car (last (assq 'charset
841 (assoc current-language-environment
842 language-info-alist))))))
843 (if (eq charset 'ascii) (setq charset nil))
846 (car (last (assq mail-parse-charset
847 mm-mime-mule-charset-alist)))))
848 (list 'ascii (or charset 'latin-iso8859-1)))))))))
850 (if (fboundp 'shell-quote-argument)
851 (defalias 'mm-quote-arg 'shell-quote-argument)
852 (defun mm-quote-arg (arg)
853 "Return a version of ARG that is safe to evaluate in a shell."
854 (let ((pos 0) new-pos accum)
855 ;; *** bug: we don't handle newline characters properly
856 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
857 (push (substring arg pos new-pos) accum)
859 (push (list (aref arg new-pos)) accum)
860 (setq pos (1+ new-pos)))
863 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
865 (defun mm-auto-mode-alist ()
866 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
867 (let ((alist auto-mode-alist)
870 (when (listp (cdar alist))
871 (push (car alist) out))
875 (defvar mm-inhibit-file-name-handlers
876 '(jka-compr-handler image-file-handler)
877 "A list of handlers doing (un)compression (etc) thingies.")
879 (defun mm-insert-file-contents (filename &optional visit beg end replace
881 "Like `insert-file-contents', but only reads in the file.
882 A buffer may be modified in several ways after reading into the buffer due
883 to advanced Emacs features, such as file-name-handlers, format decoding,
884 `find-file-hooks', etc.
885 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
886 This function ensures that none of these modifications will take place."
887 (let ((format-alist nil)
888 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
889 (default-major-mode 'fundamental-mode)
890 (enable-local-variables nil)
891 (after-insert-file-functions nil)
892 (enable-local-eval nil)
893 (find-file-hooks nil)
894 (inhibit-file-name-operation (if inhibit
895 'insert-file-contents
896 inhibit-file-name-operation))
897 (inhibit-file-name-handlers
899 (append mm-inhibit-file-name-handlers
900 inhibit-file-name-handlers)
901 inhibit-file-name-handlers)))
902 (insert-file-contents filename visit beg end replace)))
904 (defun mm-append-to-file (start end filename &optional codesys inhibit)
905 "Append the contents of the region to the end of file FILENAME.
906 When called from a function, expects three arguments,
907 START, END and FILENAME. START and END are buffer positions
908 saying what text to write.
909 Optional fourth argument specifies the coding system to use when
911 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
912 (let ((coding-system-for-write
913 (or codesys mm-text-coding-system-for-write
914 mm-text-coding-system))
915 (inhibit-file-name-operation (if inhibit
917 inhibit-file-name-operation))
918 (inhibit-file-name-handlers
920 (append mm-inhibit-file-name-handlers
921 inhibit-file-name-handlers)
922 inhibit-file-name-handlers)))
923 (write-region start end filename t 'no-message)
924 (message "Appended to %s" filename)))
926 (defun mm-write-region (start end filename &optional append visit lockname
927 coding-system inhibit)
929 "Like `write-region'.
930 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
931 (let ((coding-system-for-write
932 (or coding-system mm-text-coding-system-for-write
933 mm-text-coding-system))
934 (inhibit-file-name-operation (if inhibit
936 inhibit-file-name-operation))
937 (inhibit-file-name-handlers
939 (append mm-inhibit-file-name-handlers
940 inhibit-file-name-handlers)
941 inhibit-file-name-handlers)))
942 (write-region start end filename append visit lockname)))
944 (defun mm-image-load-path (&optional package)
946 (dolist (path load-path (nreverse result))
949 (setq dir (concat (file-name-directory
950 (directory-file-name path))
951 "etc/images/" (or package "gnus/")))))
953 (push path result))))
955 ;; Fixme: This doesn't look useful where it's used.
956 (if (fboundp 'detect-coding-region)
957 (defun mm-detect-coding-region (start end)
958 "Like `detect-coding-region' except returning the best one."
959 (let ((coding-systems
960 (detect-coding-region start end)))
961 (or (car-safe coding-systems)
963 (defun mm-detect-coding-region (start end)
964 (let ((point (point)))
966 (skip-chars-forward "\0-\177" end)
968 (if (eq (point) end) 'ascii (mm-guess-charset))
969 (goto-char point)))))
971 (if (fboundp 'coding-system-get)
972 (defun mm-detect-mime-charset-region (start end)
973 "Detect MIME charset of the text in the region between START and END."
974 (let ((cs (mm-detect-coding-region start end)))
975 (coding-system-get cs 'mime-charset)))
976 (defun mm-detect-mime-charset-region (start end)
977 "Detect MIME charset of the text in the region between START and END."
978 (let ((cs (mm-detect-coding-region start end)))
984 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
985 ;;; mm-util.el ends here