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.
38 (let ((nfunc (intern (format "mm-%s" (car elem)))))
39 (if (fboundp (car elem))
40 (defalias nfunc (car elem))
41 (defalias nfunc (cdr elem)))))
42 '((decode-coding-string . (lambda (s a) s))
43 (encode-coding-string . (lambda (s a) s))
44 (encode-coding-region . ignore)
45 (coding-system-list . ignore)
46 (decode-coding-region . ignore)
48 (coding-system-equal . equal)
49 (annotationp . ignore)
50 (set-buffer-file-coding-system . ignore)
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)
75 . (lambda (string regexp rep &optional literal)
76 "See `replace-regexp-in-string', only the order of args differs."
77 (replace-regexp-in-string regexp rep string nil literal)))
78 (string-as-unibyte . identity)
79 (string-make-unibyte . identity)
80 ;; string-as-multibyte often doesn't really do what you think it does.
82 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
83 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
84 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
85 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
87 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
88 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
89 ;; Better use string-to-multibyte or encode-coding-string.
90 ;; If you really need string-as-multibyte somewhere it's usually
91 ;; because you're using the internal emacs-mule representation (maybe
92 ;; because you're using string-as-unibyte somewhere), which is
93 ;; generally a problem in itself.
94 ;; Here is an approximate equivalence table to help think about it:
95 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
96 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
97 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
98 (string-as-multibyte . identity)
101 "Return a multibyte string with the same individual chars as string."
103 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
105 (multibyte-string-p . ignore)
106 ;; It is not a MIME function, but some MIME functions use it.
107 (make-temp-file . (lambda (prefix &optional dir-flag)
108 (let ((file (expand-file-name
109 (make-temp-name prefix)
110 (if (fboundp 'temp-directory)
112 temporary-file-directory))))
114 (make-directory file))
116 (insert-byte . insert-char)
117 (multibyte-char-to-unibyte . identity))))
120 (defalias 'mm-char-or-char-int-p
122 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
123 ((fboundp 'char-valid-p) 'char-valid-p)
126 ;; Fixme: This seems always to be used to read a MIME charset, so it
127 ;; should be re-named and fixed (in Emacs) to offer completion only on
128 ;; proper charset names (base coding systems which have a
129 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
131 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
132 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
133 ;; Actually, there should be an `mm-coding-system-mime-charset'.
135 (defalias 'mm-read-coding-system
137 ((fboundp 'read-coding-system)
138 (if (and (featurep 'xemacs)
139 (<= (string-to-number emacs-version) 21.1))
140 (lambda (prompt &optional default-coding-system)
141 (read-coding-system prompt))
142 'read-coding-system))
143 (t (lambda (prompt &optional default-coding-system)
144 "Prompt the user for a coding system."
146 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
147 mm-mime-mule-charset-alist)))))))
149 (defvar mm-coding-system-list nil)
150 (defun mm-get-coding-system-list ()
151 "Get the coding system list."
152 (or mm-coding-system-list
153 (setq mm-coding-system-list (mm-coding-system-list))))
155 (defun mm-coding-system-p (cs)
156 "Return non-nil if CS is a symbol naming a coding system.
157 In XEmacs, also return non-nil if CS is a coding system object.
158 If CS is available, return CS itself in Emacs, and return a coding
159 system object in XEmacs."
160 (if (fboundp 'find-coding-system)
161 (and cs (find-coding-system cs))
162 (if (fboundp 'coding-system-p)
163 (when (coding-system-p cs)
165 ;; Is this branch ever actually useful?
166 (car (memq cs (mm-get-coding-system-list))))))
168 (defvar mm-charset-synonym-alist
170 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
171 ,@(unless (mm-coding-system-p 'x-ctext)
172 '((x-ctext . ctext)))
173 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
174 ,@(unless (mm-coding-system-p 'iso-8859-15)
175 '((iso-8859-15 . iso-8859-1)))
176 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
177 ,@(unless (mm-coding-system-p 'big5-hkscs)
178 '((big5-hkscs . big5)))
179 ;; Windows-1252 is actually a superset of Latin-1. See also
180 ;; `gnus-article-dumbquotes-map'.
181 ,@(unless (mm-coding-system-p 'windows-1252)
182 (if (mm-coding-system-p 'cp1252)
183 '((windows-1252 . cp1252))
184 '((windows-1252 . iso-8859-1))))
185 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
186 ;; Outlook users in Czech republic. Use this to allow reading of their
187 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
188 ,@(if (and (not (mm-coding-system-p 'windows-1250))
189 (mm-coding-system-p 'cp1250))
190 '((windows-1250 . cp1250)))
191 ;; A Microsoft misunderstanding.
192 ,@(if (and (not (mm-coding-system-p 'unicode))
193 (mm-coding-system-p 'utf-16-le))
194 '((unicode . utf-16-le)))
195 ;; A Microsoft misunderstanding.
196 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
197 (if (mm-coding-system-p 'cp949)
198 '((ks_c_5601-1987 . cp949))
199 '((ks_c_5601-1987 . euc-kr))))
201 "A mapping from invalid charset names to the real charset names.")
203 (defvar mm-binary-coding-system
205 ((mm-coding-system-p 'binary) 'binary)
206 ((mm-coding-system-p 'no-conversion) 'no-conversion)
208 "100% binary coding system.")
210 (defvar mm-text-coding-system
211 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
212 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
213 (and (mm-coding-system-p 'raw-text) 'raw-text))
214 mm-binary-coding-system)
215 "Text-safe coding system (For removing ^M).")
217 (defvar mm-text-coding-system-for-write nil
218 "Text coding system for write.")
220 (defvar mm-auto-save-coding-system
222 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
223 (if (memq system-type '(windows-nt ms-dos ms-windows))
224 (if (mm-coding-system-p 'utf-8-emacs-dos)
225 'utf-8-emacs-dos mm-binary-coding-system)
227 ((mm-coding-system-p 'emacs-mule)
228 (if (memq system-type '(windows-nt ms-dos ms-windows))
229 (if (mm-coding-system-p 'emacs-mule-dos)
230 'emacs-mule-dos mm-binary-coding-system)
232 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
233 (t mm-binary-coding-system))
234 "Coding system of auto save file.")
236 (defvar mm-universal-coding-system mm-auto-save-coding-system
237 "The universal coding system.")
239 ;; Fixme: some of the cars here aren't valid MIME charsets. That
240 ;; should only matter with XEmacs, though.
241 (defvar mm-mime-mule-charset-alist
243 (iso-8859-1 latin-iso8859-1)
244 (iso-8859-2 latin-iso8859-2)
245 (iso-8859-3 latin-iso8859-3)
246 (iso-8859-4 latin-iso8859-4)
247 (iso-8859-5 cyrillic-iso8859-5)
248 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
249 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
250 ;; charset is koi8-r, not iso-8859-5.
251 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
252 (iso-8859-6 arabic-iso8859-6)
253 (iso-8859-7 greek-iso8859-7)
254 (iso-8859-8 hebrew-iso8859-8)
255 (iso-8859-9 latin-iso8859-9)
256 (iso-8859-14 latin-iso8859-14)
257 (iso-8859-15 latin-iso8859-15)
258 (viscii vietnamese-viscii-lower)
259 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
260 (euc-kr korean-ksc5601)
261 (gb2312 chinese-gb2312)
262 (big5 chinese-big5-1 chinese-big5-2)
264 (thai-tis620 thai-tis620)
265 (windows-1251 cyrillic-iso8859-5)
266 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
267 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
268 latin-jisx0201 japanese-jisx0208-1978
269 chinese-gb2312 japanese-jisx0208
270 korean-ksc5601 japanese-jisx0212)
271 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
272 latin-jisx0201 japanese-jisx0208-1978
273 chinese-gb2312 japanese-jisx0208
274 korean-ksc5601 japanese-jisx0212
275 chinese-cns11643-1 chinese-cns11643-2)
276 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
277 cyrillic-iso8859-5 greek-iso8859-7
278 latin-jisx0201 japanese-jisx0208-1978
279 chinese-gb2312 japanese-jisx0208
280 korean-ksc5601 japanese-jisx0212
281 chinese-cns11643-1 chinese-cns11643-2
282 chinese-cns11643-3 chinese-cns11643-4
283 chinese-cns11643-5 chinese-cns11643-6
285 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
286 japanese-jisx0213-1 japanese-jisx0213-2)
287 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
288 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
289 (charsetp 'unicode-a)
290 (not (mm-coding-system-p 'mule-utf-8)))
291 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
292 ;; If we have utf-8 we're in Mule 5+.
295 (coding-system-get 'mule-utf-8 'safe-charsets)))))
296 "Alist of MIME-charset/MULE-charsets.")
298 (defun mm-enrich-utf-8-by-mule-ucs ()
299 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
300 This function will run when the `un-define' module is loaded under
301 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
302 with Mule charsets. It is completely useless for Emacs."
303 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
304 (assoc "un-define" after-load-alist)))
305 (setq after-load-alist
306 (delete '("un-define") after-load-alist)))
307 (when (boundp 'unicode-basic-translation-charset-order-list)
313 'unicode-basic-translation-charset-order-list))))
314 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
317 (setq mm-mime-mule-charset-alist
318 (nconc mm-mime-mule-charset-alist
319 (list (cons 'utf-8 val))))))
322 ;; Correct by construction, but should be unnecessary for Emacs:
323 (if (featurep 'xemacs)
324 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
325 (when (and (fboundp 'coding-system-list)
326 (fboundp 'sort-coding-systems))
327 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
331 mime (or (coding-system-get cs :mime-charset) ; Emacs 22
332 (coding-system-get cs 'mime-charset)))
334 (not (eq t (setq mule
335 (coding-system-get cs 'safe-charsets))))
336 (not (assq mime alist)))
337 (push (cons mime (delq 'ascii mule)) alist)))
338 (setq mm-mime-mule-charset-alist (nreverse alist)))))
340 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
341 "A list of special charsets.
342 Valid elements include:
343 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
344 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
347 (defvar mm-iso-8859-15-compatible
348 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
349 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
350 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
352 (defvar mm-iso-8859-x-to-15-table
353 (and (fboundp 'coding-system-p)
354 (mm-coding-system-p 'iso-8859-15)
357 (if (mm-coding-system-p (car cs))
358 (let ((c (string-to-char
359 (decode-coding-string "\341" (car cs)))))
360 (cons (char-charset c)
363 (decode-coding-string "\341" 'iso-8859-15)) c)
364 (string-to-list (decode-coding-string (car (cdr cs))
367 mm-iso-8859-15-compatible))
368 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
370 (defcustom mm-coding-system-priorities
371 (if (boundp 'current-language-environment)
372 (let ((lang (symbol-value 'current-language-environment)))
373 (cond ((string= lang "Japanese")
374 ;; Japanese users prefer iso-2022-jp to euc-japan or
375 ;; shift_jis, however iso-8859-1 should be used when
376 ;; there are only ASCII text and Latin-1 characters.
377 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
378 "Preferred coding systems for encoding outgoing messages.
380 More than one suitable coding system may be found for some text.
381 By default, the coding system with the highest priority is used
382 to encode outgoing messages (see `sort-coding-systems'). If this
383 variable is set, it overrides the default priority."
385 :type '(repeat (symbol :tag "Coding system"))
389 (defvar mm-use-find-coding-systems-region
390 (fboundp 'find-coding-systems-region)
391 "Use `find-coding-systems-region' to find proper coding systems.
393 Setting it to nil is useful on Emacsen supporting Unicode if sending
394 mail with multiple parts is preferred to sending a Unicode one.")
396 ;;; Internal variables:
400 (defun mm-mule-charset-to-mime-charset (charset)
401 "Return the MIME charset corresponding to the given Mule CHARSET."
402 (if (and (fboundp 'find-coding-systems-for-charsets)
403 (fboundp 'sort-coding-systems))
404 (let ((css (sort (sort-coding-systems
405 (find-coding-systems-for-charsets (list charset)))
406 'mm-sort-coding-systems-predicate))
408 (while (and (not mime)
410 (when (setq cs (pop css))
411 (setq mime (or (coding-system-get cs :mime-charset)
412 (coding-system-get cs 'mime-charset)))))
414 (let ((alist (mapcar (lambda (cs)
415 (assq cs mm-mime-mule-charset-alist))
416 (sort (mapcar 'car mm-mime-mule-charset-alist)
417 'mm-sort-coding-systems-predicate)))
420 (when (memq charset (cdar alist))
421 (setq out (caar alist)
426 (defun mm-charset-to-coding-system (charset &optional lbt)
427 "Return coding-system corresponding to CHARSET.
428 CHARSET is a symbol naming a MIME charset.
429 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
430 used as the line break code type of the coding system."
431 (when (stringp charset)
432 (setq charset (intern (downcase charset))))
434 (setq charset (intern (format "%s-%s" charset lbt))))
438 ;; Running in a non-MULE environment.
439 ((or (null (mm-get-coding-system-list))
440 (not (fboundp 'coding-system-get)))
443 ((eq charset 'us-ascii)
445 ;; Check to see whether we can handle this charset. (This depends
446 ;; on there being some coding system matching each `mime-charset'
447 ;; property defined, as there should be.)
448 ((and (mm-coding-system-p charset)
449 ;;; Doing this would potentially weed out incorrect charsets.
451 ;;; (eq charset (coding-system-get charset 'mime-charset))
454 ;; Translate invalid charsets.
455 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
456 (and cs (mm-coding-system-p cs) cs)))
457 ;; Last resort: search the coding system list for entries which
458 ;; have the right mime-charset in case the canonical name isn't
459 ;; defined (though it should be).
461 ;; mm-get-coding-system-list returns a list of cs without lbt.
463 (dolist (c (mm-get-coding-system-list))
465 (eq charset (or (coding-system-get c :mime-charset)
466 (coding-system-get c 'mime-charset))))
471 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
472 (boundp 'default-enable-multibyte-characters)
473 default-enable-multibyte-characters
474 (fboundp 'set-buffer-multibyte))
475 "True in Emacs with Mule.")
478 (defun mm-enable-multibyte ()
479 "Set the multibyte flag of the current buffer.
480 Only do this if the default value of `enable-multibyte-characters' is
481 non-nil. This is a no-op in XEmacs."
482 (set-buffer-multibyte 'to))
483 (defalias 'mm-enable-multibyte 'ignore))
486 (defun mm-disable-multibyte ()
487 "Unset the multibyte flag of in the current buffer.
488 This is a no-op in XEmacs."
489 (set-buffer-multibyte nil))
490 (defalias 'mm-disable-multibyte 'ignore)))
492 (defun mm-preferred-coding-system (charset)
493 ;; A typo in some Emacs versions.
494 (or (get-charset-property charset 'preferred-coding-system)
495 (get-charset-property charset 'prefered-coding-system)))
497 ;; Mule charsets shouldn't be used.
498 (defsubst mm-guess-charset ()
499 "Guess Mule charset from the language environment."
501 mail-parse-mule-charset ;; cached mule-charset
503 (setq mail-parse-mule-charset
504 (and (boundp 'current-language-environment)
507 (assoc current-language-environment
508 language-info-alist))))))
509 (if (or (not mail-parse-mule-charset)
510 (eq mail-parse-mule-charset 'ascii))
511 (setq mail-parse-mule-charset
512 (or (car (last (assq mail-parse-charset
513 mm-mime-mule-charset-alist)))
516 mail-parse-mule-charset)))
518 (defun mm-charset-after (&optional pos)
519 "Return charset of a character in current buffer at position POS.
520 If POS is nil, it defauls to the current point.
521 If POS is out of range, the value is nil.
522 If the charset is `composition', return the actual one."
523 (let ((char (char-after pos)) charset)
524 (if (< (mm-char-int char) 128)
525 (setq charset 'ascii)
526 ;; charset-after is fake in some Emacsen.
527 (setq charset (and (fboundp 'char-charset) (char-charset char)))
528 (if (eq charset 'composition) ; Mule 4
529 (let ((p (or pos (point))))
530 (cadr (find-charset-region p (1+ p))))
531 (if (and charset (not (memq charset '(ascii eight-bit-control
532 eight-bit-graphic))))
534 (mm-guess-charset))))))
536 (defun mm-mime-charset (charset)
537 "Return the MIME charset corresponding to the given Mule CHARSET."
538 (if (eq charset 'unknown)
539 (error "The message contains non-printable characters, please use attachment"))
540 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
541 ;; This exists in Emacs 20.
543 (and (mm-preferred-coding-system charset)
544 (or (coding-system-get
545 (mm-preferred-coding-system charset) :mime-charset)
547 (mm-preferred-coding-system charset) 'mime-charset)))
548 (and (eq charset 'ascii)
550 (mm-preferred-coding-system charset)
551 (mm-mule-charset-to-mime-charset charset))
552 ;; This is for XEmacs.
553 (mm-mule-charset-to-mime-charset charset)))
555 (if (fboundp 'delete-dups)
556 (defalias 'mm-delete-duplicates 'delete-dups)
557 (defun mm-delete-duplicates (list)
558 "Destructively remove `equal' duplicates from LIST.
559 Store the result in LIST and return it. LIST must be a proper list.
560 Of several `equal' occurrences of an element in LIST, the first
563 This is a compatibility function for Emacsen without `delete-dups'."
564 ;; Code from `subr.el' in Emacs 22:
567 (setcdr tail (delete (car tail) (cdr tail)))
568 (setq tail (cdr tail))))
571 ;; Fixme: This is used in places when it should be testing the
572 ;; default multibyteness. See mm-default-multibyte-p.
574 (if (and (not (featurep 'xemacs))
575 (boundp 'enable-multibyte-characters))
576 (defun mm-multibyte-p ()
577 "Non-nil if multibyte is enabled in the current buffer."
578 enable-multibyte-characters)
579 (defun mm-multibyte-p () (featurep 'mule))))
581 (defun mm-default-multibyte-p ()
582 "Return non-nil if the session is multibyte.
583 This affects whether coding conversion should be attempted generally."
585 (if (boundp 'default-enable-multibyte-characters)
586 default-enable-multibyte-characters
589 (defun mm-iso-8859-x-to-15-region (&optional b e)
590 (if (fboundp 'char-charset)
591 (let (charset item c inconvertible)
593 (if e (narrow-to-region b e))
594 (goto-char (point-min))
595 (skip-chars-forward "\0-\177")
598 ((not (setq item (assq (char-charset (setq c (char-after)))
599 mm-iso-8859-x-to-15-table)))
601 ((memq c (cdr (cdr item)))
602 (setq inconvertible t)
605 (insert-before-markers (prog1 (+ c (car (cdr item)))
607 (skip-chars-forward "\0-\177")))
608 (not inconvertible))))
610 (defun mm-sort-coding-systems-predicate (a b)
613 ;; Note: invalid entries are dropped silently
614 (and (setq cs (mm-coding-system-p cs))
615 (coding-system-base cs)))
616 mm-coding-system-priorities)))
617 (and (setq a (mm-coding-system-p a))
618 (if (setq b (mm-coding-system-p b))
619 (> (length (memq (coding-system-base a) priorities))
620 (length (memq (coding-system-base b) priorities)))
624 (autoload 'latin-unity-massage-name "latin-unity")
625 (autoload 'latin-unity-maybe-remap "latin-unity")
626 (autoload 'latin-unity-representations-feasible-region "latin-unity")
627 (autoload 'latin-unity-representations-present-region "latin-unity")
628 (defvar latin-unity-coding-systems)
629 (defvar latin-unity-ucs-list))
631 (defun mm-xemacs-find-mime-charset-1 (begin end)
632 "Determine which MIME charset to use to send region as message.
633 This uses the XEmacs-specific latin-unity package to better handle the
634 case where identical characters from diverse ISO-8859-? character sets
635 can be encoded using a single one of the corresponding coding systems.
637 It treats `mm-coding-system-priorities' as the list of preferred
638 coding systems; a useful example setting for this list in Western
639 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
640 to the very standard Latin 1 coding system, and only move to coding
641 systems that are less supported as is necessary to encode the
642 characters that exist in the buffer.
644 Latin Unity doesn't know about those non-ASCII Roman characters that
645 are available in various East Asian character sets. As such, its
646 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
647 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
648 But this is very much a corner case, so don't worry about it."
649 (let ((systems mm-coding-system-priorities) csets psets curset)
651 ;; Load the Latin Unity library, if available.
652 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
653 (require 'latin-unity))
655 ;; Now, can we use it?
656 (if (featurep 'latin-unity)
658 (setq csets (latin-unity-representations-feasible-region begin end)
659 psets (latin-unity-representations-present-region begin end))
663 ;; Pass back the first coding system in the preferred list
664 ;; that can encode the whole region.
665 (dolist (curset systems)
666 (setq curset (latin-unity-massage-name 'buffer-default curset))
668 ;; If the coding system is a universal coding system, then
669 ;; it can certainly encode all the characters in the region.
670 (if (memq curset latin-unity-ucs-list)
671 (throw 'done (list curset)))
673 ;; If a coding system isn't universal, and isn't in
674 ;; the list that latin unity knows about, we can't
675 ;; decide whether to use it here. Leave that until later
676 ;; in `mm-find-mime-charset-region' function, whence we
678 (unless (memq curset latin-unity-coding-systems)
681 ;; Right, we know about this coding system, and it may
682 ;; conceivably be able to encode all the characters in
684 (if (latin-unity-maybe-remap begin end curset csets psets t)
685 (throw 'done (list curset))))
687 ;; Can't encode using anything from the
688 ;; `mm-coding-system-priorities' list.
689 ;; Leave `mm-find-mime-charset' to do most of the work.
692 ;; Right, latin unity isn't available; let `mm-find-charset-region'
693 ;; take its default action, which equally applies to GNU Emacs.
696 (defmacro mm-xemacs-find-mime-charset (begin end)
697 (when (featurep 'xemacs)
698 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
700 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
701 "Return the MIME charsets needed to encode the region between B and E.
702 nil means ASCII, a single-element list represents an appropriate MIME
703 charset, and a longer list means no appropriate charset."
705 ;; The return possibilities of this function are a mess...
706 (or (and (mm-multibyte-p)
707 mm-use-find-coding-systems-region
708 ;; Find the mime-charset of the most preferred coding
709 ;; system that has one.
710 (let ((systems (find-coding-systems-region b e)))
711 (when mm-coding-system-priorities
713 (sort systems 'mm-sort-coding-systems-predicate)))
714 (setq systems (delq 'compound-text systems))
715 (unless (equal systems '(undecided))
717 (let* ((head (pop systems))
718 (cs (or (coding-system-get head :mime-charset)
719 (coding-system-get head 'mime-charset))))
720 ;; The mime-charset (`x-ctext') of
721 ;; `compound-text' is not in the IANA list. We
722 ;; shouldn't normally use anything here with a
723 ;; mime-charset having an `x-' prefix.
724 ;; Fixme: Allow this to be overridden, since
725 ;; there is existing use of x-ctext.
726 ;; Also people apparently need the coding system
727 ;; `iso-2022-jp-3' (which Mule-UCS defines with
728 ;; mime-charset, though it's not valid).
730 (not (string-match "^[Xx]-" (symbol-name cs)))
731 ;; UTF-16 of any variety is invalid for
732 ;; text parts and, unfortunately, has
733 ;; mime-charset defined both in Mule-UCS
734 ;; and versions of Emacs. (The name
735 ;; might be `mule-utf-16...' or
737 (not (string-match "utf-16" (symbol-name cs))))
739 charsets (list cs))))))
741 ;; If we're XEmacs, and some coding system is appropriate,
742 ;; mm-xemacs-find-mime-charset will return an appropriate list.
743 ;; Otherwise, we'll get nil, and the next setq will get invoked.
744 (setq charsets (mm-xemacs-find-mime-charset b e))
746 ;; We're not multibyte, or a single coding system won't cover it.
748 (mm-delete-duplicates
749 (mapcar 'mm-mime-charset
751 (mm-find-charset-region b e))))))
752 (if (and (> (length charsets) 1)
753 (memq 'iso-8859-15 charsets)
754 (memq 'iso-8859-15 hack-charsets)
755 (save-excursion (mm-iso-8859-x-to-15-region b e)))
756 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
757 mm-iso-8859-15-compatible))
758 (if (and (memq 'iso-2022-jp-2 charsets)
759 (memq 'iso-2022-jp-2 hack-charsets))
760 (setq charsets (delq 'iso-2022-jp charsets)))
761 ;; Attempt to reduce the number of charsets if utf-8 is available.
762 (if (and (featurep 'xemacs)
763 (> (length charsets) 1)
764 (mm-coding-system-p 'utf-8))
765 (let ((mm-coding-system-priorities
766 (cons 'utf-8 mm-coding-system-priorities)))
768 (mm-delete-duplicates
769 (mapcar 'mm-mime-charset
771 (mm-find-charset-region b e)))))))
774 (defmacro mm-with-unibyte-buffer (&rest forms)
775 "Create a temporary buffer, and evaluate FORMS there like `progn'.
776 Use unibyte mode for this."
777 `(let (default-enable-multibyte-characters)
778 (with-temp-buffer ,@forms)))
779 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
780 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
782 (defmacro mm-with-multibyte-buffer (&rest forms)
783 "Create a temporary buffer, and evaluate FORMS there like `progn'.
784 Use multibyte mode for this."
785 `(let ((default-enable-multibyte-characters t))
786 (with-temp-buffer ,@forms)))
787 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
788 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
790 (defmacro mm-with-unibyte-current-buffer (&rest forms)
791 "Evaluate FORMS with current buffer temporarily made unibyte.
792 Also bind `default-enable-multibyte-characters' to nil.
793 Equivalent to `progn' in XEmacs"
794 (let ((multibyte (make-symbol "multibyte"))
795 (buffer (make-symbol "buffer")))
797 (let ((,multibyte enable-multibyte-characters)
798 (,buffer (current-buffer)))
800 (let (default-enable-multibyte-characters)
801 (set-buffer-multibyte nil)
804 (set-buffer-multibyte ,multibyte)))
805 (let (default-enable-multibyte-characters)
807 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
808 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
810 (defmacro mm-with-unibyte (&rest forms)
811 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
812 `(let (default-enable-multibyte-characters)
814 (put 'mm-with-unibyte 'lisp-indent-function 0)
815 (put 'mm-with-unibyte 'edebug-form-spec '(body))
817 (defmacro mm-with-multibyte (&rest forms)
818 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
819 `(let ((default-enable-multibyte-characters t))
821 (put 'mm-with-multibyte 'lisp-indent-function 0)
822 (put 'mm-with-multibyte 'edebug-form-spec '(body))
824 (defun mm-find-charset-region (b e)
825 "Return a list of Emacs charsets in the region B to E."
827 ((and (mm-multibyte-p)
828 (fboundp 'find-charset-region))
829 ;; Remove composition since the base charsets have been included.
830 ;; Remove eight-bit-*, treat them as ascii.
831 (let ((css (find-charset-region b e)))
832 (mapcar (lambda (cs) (setq css (delq cs css)))
833 '(composition eight-bit-control eight-bit-graphic
837 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
840 (narrow-to-region b e)
841 (goto-char (point-min))
842 (skip-chars-forward "\0-\177")
847 (and (boundp 'current-language-environment)
848 (car (last (assq 'charset
849 (assoc current-language-environment
850 language-info-alist))))))
851 (if (eq charset 'ascii) (setq charset nil))
854 (car (last (assq mail-parse-charset
855 mm-mime-mule-charset-alist)))))
856 (list 'ascii (or charset 'latin-iso8859-1)))))))))
858 (defun mm-auto-mode-alist ()
859 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
860 (let ((alist auto-mode-alist)
863 (when (listp (cdar alist))
864 (push (car alist) out))
868 (defvar mm-inhibit-file-name-handlers
869 '(jka-compr-handler image-file-handler)
870 "A list of handlers doing (un)compression (etc) thingies.")
872 (defun mm-insert-file-contents (filename &optional visit beg end replace
874 "Like `insert-file-contents', but only reads in the file.
875 A buffer may be modified in several ways after reading into the buffer due
876 to advanced Emacs features, such as file-name-handlers, format decoding,
877 `find-file-hooks', etc.
878 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
879 This function ensures that none of these modifications will take place."
880 (let* ((format-alist nil)
881 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
882 (default-major-mode 'fundamental-mode)
883 (enable-local-variables nil)
884 (after-insert-file-functions nil)
885 (enable-local-eval nil)
886 (inhibit-file-name-operation (if inhibit
887 'insert-file-contents
888 inhibit-file-name-operation))
889 (inhibit-file-name-handlers
891 (append mm-inhibit-file-name-handlers
892 inhibit-file-name-handlers)
893 inhibit-file-name-handlers))
894 (ffh (if (boundp 'find-file-hook)
897 (val (symbol-value ffh)))
900 (insert-file-contents filename visit beg end replace)
903 (defun mm-append-to-file (start end filename &optional codesys inhibit)
904 "Append the contents of the region to the end of file FILENAME.
905 When called from a function, expects three arguments,
906 START, END and FILENAME. START and END are buffer positions
907 saying what text to write.
908 Optional fourth argument specifies the coding system to use when
910 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
911 (let ((coding-system-for-write
912 (or codesys mm-text-coding-system-for-write
913 mm-text-coding-system))
914 (inhibit-file-name-operation (if inhibit
916 inhibit-file-name-operation))
917 (inhibit-file-name-handlers
919 (append mm-inhibit-file-name-handlers
920 inhibit-file-name-handlers)
921 inhibit-file-name-handlers)))
922 (write-region start end filename t 'no-message)
923 (message "Appended to %s" filename)))
925 (defun mm-write-region (start end filename &optional append visit lockname
926 coding-system inhibit)
928 "Like `write-region'.
929 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
930 (let ((coding-system-for-write
931 (or coding-system mm-text-coding-system-for-write
932 mm-text-coding-system))
933 (inhibit-file-name-operation (if inhibit
935 inhibit-file-name-operation))
936 (inhibit-file-name-handlers
938 (append mm-inhibit-file-name-handlers
939 inhibit-file-name-handlers)
940 inhibit-file-name-handlers)))
941 (write-region start end filename append visit lockname)))
943 (defun mm-image-load-path (&optional package)
945 (dolist (path load-path (nreverse result))
948 (setq dir (concat (file-name-directory
949 (directory-file-name path))
950 "etc/images/" (or package "gnus/")))))
952 (push path result))))
954 ;; Fixme: This doesn't look useful where it's used.
955 (if (fboundp 'detect-coding-region)
956 (defun mm-detect-coding-region (start end)
957 "Like `detect-coding-region' except returning the best one."
958 (let ((coding-systems
959 (detect-coding-region start end)))
960 (or (car-safe coding-systems)
962 (defun mm-detect-coding-region (start end)
963 (let ((point (point)))
965 (skip-chars-forward "\0-\177" end)
967 (if (eq (point) end) 'ascii (mm-guess-charset))
968 (goto-char point)))))
970 (if (fboundp 'coding-system-get)
971 (defun mm-detect-mime-charset-region (start end)
972 "Detect MIME charset of the text in the region between START and END."
973 (let ((cs (mm-detect-coding-region start end)))
974 (or (coding-system-get cs :mime-charset)
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)))
982 (unless (fboundp 'coding-system-to-mime-charset)
983 (defalias 'coding-system-to-mime-charset 'ignore)))
985 (defun mm-coding-system-to-mime-charset (coding-system)
986 "Return the MIME charset corresponding to CODING-SYSTEM.
987 To make this function work with XEmacs, the APEL package is required."
989 (or (and (fboundp 'coding-system-get)
990 (or (coding-system-get coding-system :mime-charset)
991 (coding-system-get coding-system 'mime-charset)))
992 (and (featurep 'xemacs)
993 (or (and (fboundp 'coding-system-to-mime-charset)
994 (not (eq (symbol-function 'coding-system-to-mime-charset)
996 (and (condition-case nil
999 (fboundp 'coding-system-to-mime-charset)))
1000 (coding-system-to-mime-charset coding-system)))))
1003 (require 'jka-compr))
1005 (defun mm-decompress-buffer (filename &optional inplace force)
1006 "Decompress buffer's contents, depending on jka-compr.
1007 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
1008 agrees with `jka-compr-compression-info-list', decompression is done.
1009 Signal an error if FORCE is neither nil nor t and compressed data are
1010 not decompressed because `auto-compression-mode' is disabled.
1011 If INPLACE is nil, return decompressed data or nil without modifying
1012 the buffer. Otherwise, replace the buffer's contents with the
1013 decompressed data. The buffer's multibyteness must be turned off."
1016 (prog1 t (require 'jka-compr))
1017 (and (fboundp 'jka-compr-installed-p)
1018 (jka-compr-installed-p))))
1019 (let ((info (jka-compr-get-compression-info filename)))
1021 (unless (or (memq force (list nil t))
1022 (jka-compr-installed-p))
1024 (let ((prog (jka-compr-info-uncompress-program info))
1025 (args (jka-compr-info-uncompress-args info))
1026 (msg (format "%s %s..."
1027 (jka-compr-info-uncompress-message info)
1029 (err-file (jka-compr-make-temp-name))
1030 (cur (current-buffer))
1031 (coding-system-for-read mm-binary-coding-system)
1032 (coding-system-for-write mm-binary-coding-system)
1036 (insert-buffer-substring cur)
1039 (unless (memq (apply 'call-process-region
1040 (point-min) (point-max)
1041 prog t (list t err-file) nil args)
1042 jka-compr-acceptable-retval-list)
1046 (delete "" (split-string
1048 (insert-file-contents err-file)
1054 (format "Error while executing \"%s %s < %s\""
1055 prog (mapconcat 'identity args " ")
1057 (setq retval (buffer-string)))
1059 (setq err-msg (error-message-string err)))))
1060 (when (file-exists-p err-file)
1061 (ignore-errors (jka-compr-delete-temp-file err-file)))
1064 (delete-region (point-min) (point-max))
1067 (message "%s" (or err-msg (concat msg "done")))
1071 (unless (fboundp 'coding-system-name)
1072 (defalias 'coding-system-name 'ignore))
1073 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1074 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1075 (unless (fboundp 'find-operation-coding-system)
1076 (defalias 'find-operation-coding-system 'ignore)))
1078 (defun mm-find-buffer-file-coding-system (&optional filename)
1079 "Find coding system used to decode the contents of the current buffer.
1080 This function looks for the coding system magic cookie or examines the
1081 coding system specified by `file-coding-system-alist' being associated
1082 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1083 gzip, bzip2, etc. are allowed."
1085 (setq filename buffer-file-name))
1087 (let ((decomp (unless ;; No worth to examine charset of tar files.
1090 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1092 (mm-decompress-buffer filename nil t))))
1094 (set-buffer (let (default-enable-multibyte-characters)
1095 (generate-new-buffer " *temp*")))
1097 (setq filename (file-name-sans-extension filename)))
1098 (goto-char (point-min))
1101 ((boundp 'set-auto-coding-function) ;; Emacs
1103 (or (funcall (symbol-value 'set-auto-coding-function)
1104 filename (- (point-max) (point-min)))
1105 (car (find-operation-coding-system 'insert-file-contents
1107 (let (auto-coding-alist)
1109 (funcall (symbol-value 'set-auto-coding-function)
1110 nil (- (point-max) (point-min)))
1112 ((featurep 'file-coding) ;; XEmacs
1113 (let ((case-fold-search t)
1114 (end (point-at-eol))
1117 (and (re-search-forward "-\\*-+[\t ]*" end t)
1119 (setq start (match-end 0))
1120 (re-search-forward "[\t ]*-+\\*-" end t))
1122 (setq end (match-beginning 0))
1124 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1126 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1128 (find-coding-system (setq codesys
1129 (intern (match-string 1))))
1131 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1134 (setq start (match-end 0))
1135 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1137 (setq end (match-beginning 0))
1140 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1142 (find-coding-system (setq codesys
1143 (intern (match-string 1))))
1146 (goto-char (point-min))
1147 (setq case-fold-search nil)
1148 (re-search-forward "^;;;coding system: "
1149 ;;(+ (point-min) 3000) t))
1151 (looking-at "[^\t\n\r ]+")
1153 (setq codesys (intern (match-string 0))))
1157 (find-file-coding-system-for-read-from-filename
1159 (coding-system-name (coding-system-base codesys)))))))
1161 (kill-buffer (current-buffer)))))))
1165 ;;; mm-util.el ends here