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 (defun mm-delete-duplicates (list)
556 "Simple substitute for CL `delete-duplicates', testing with `equal'."
559 (setq head (car list))
560 (setq list (delete head list))
561 (setq result (cons head result)))
564 ;; Fixme: This is used in places when it should be testing the
565 ;; default multibyteness. See mm-default-multibyte-p.
567 (if (and (not (featurep 'xemacs))
568 (boundp 'enable-multibyte-characters))
569 (defun mm-multibyte-p ()
570 "Non-nil if multibyte is enabled in the current buffer."
571 enable-multibyte-characters)
572 (defun mm-multibyte-p () (featurep 'mule))))
574 (defun mm-default-multibyte-p ()
575 "Return non-nil if the session is multibyte.
576 This affects whether coding conversion should be attempted generally."
578 (if (boundp 'default-enable-multibyte-characters)
579 default-enable-multibyte-characters
582 (defun mm-iso-8859-x-to-15-region (&optional b e)
583 (if (fboundp 'char-charset)
584 (let (charset item c inconvertible)
586 (if e (narrow-to-region b e))
587 (goto-char (point-min))
588 (skip-chars-forward "\0-\177")
591 ((not (setq item (assq (char-charset (setq c (char-after)))
592 mm-iso-8859-x-to-15-table)))
594 ((memq c (cdr (cdr item)))
595 (setq inconvertible t)
598 (insert-before-markers (prog1 (+ c (car (cdr item)))
600 (skip-chars-forward "\0-\177")))
601 (not inconvertible))))
603 (defun mm-sort-coding-systems-predicate (a b)
606 ;; Note: invalid entries are dropped silently
607 (and (setq cs (mm-coding-system-p cs))
608 (coding-system-base cs)))
609 mm-coding-system-priorities)))
610 (and (setq a (mm-coding-system-p a))
611 (if (setq b (mm-coding-system-p b))
612 (> (length (memq (coding-system-base a) priorities))
613 (length (memq (coding-system-base b) priorities)))
617 (autoload 'latin-unity-massage-name "latin-unity")
618 (autoload 'latin-unity-maybe-remap "latin-unity")
619 (autoload 'latin-unity-representations-feasible-region "latin-unity")
620 (autoload 'latin-unity-representations-present-region "latin-unity")
621 (defvar latin-unity-coding-systems)
622 (defvar latin-unity-ucs-list))
624 (defun mm-xemacs-find-mime-charset-1 (begin end)
625 "Determine which MIME charset to use to send region as message.
626 This uses the XEmacs-specific latin-unity package to better handle the
627 case where identical characters from diverse ISO-8859-? character sets
628 can be encoded using a single one of the corresponding coding systems.
630 It treats `mm-coding-system-priorities' as the list of preferred
631 coding systems; a useful example setting for this list in Western
632 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
633 to the very standard Latin 1 coding system, and only move to coding
634 systems that are less supported as is necessary to encode the
635 characters that exist in the buffer.
637 Latin Unity doesn't know about those non-ASCII Roman characters that
638 are available in various East Asian character sets. As such, its
639 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
640 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
641 But this is very much a corner case, so don't worry about it."
642 (let ((systems mm-coding-system-priorities) csets psets curset)
644 ;; Load the Latin Unity library, if available.
645 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
646 (require 'latin-unity))
648 ;; Now, can we use it?
649 (if (featurep 'latin-unity)
651 (setq csets (latin-unity-representations-feasible-region begin end)
652 psets (latin-unity-representations-present-region begin end))
656 ;; Pass back the first coding system in the preferred list
657 ;; that can encode the whole region.
658 (dolist (curset systems)
659 (setq curset (latin-unity-massage-name 'buffer-default curset))
661 ;; If the coding system is a universal coding system, then
662 ;; it can certainly encode all the characters in the region.
663 (if (memq curset latin-unity-ucs-list)
664 (throw 'done (list curset)))
666 ;; If a coding system isn't universal, and isn't in
667 ;; the list that latin unity knows about, we can't
668 ;; decide whether to use it here. Leave that until later
669 ;; in `mm-find-mime-charset-region' function, whence we
671 (unless (memq curset latin-unity-coding-systems)
674 ;; Right, we know about this coding system, and it may
675 ;; conceivably be able to encode all the characters in
677 (if (latin-unity-maybe-remap begin end curset csets psets t)
678 (throw 'done (list curset))))
680 ;; Can't encode using anything from the
681 ;; `mm-coding-system-priorities' list.
682 ;; Leave `mm-find-mime-charset' to do most of the work.
685 ;; Right, latin unity isn't available; let `mm-find-charset-region'
686 ;; take its default action, which equally applies to GNU Emacs.
689 (defmacro mm-xemacs-find-mime-charset (begin end)
690 (when (featurep 'xemacs)
691 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
693 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
694 "Return the MIME charsets needed to encode the region between B and E.
695 nil means ASCII, a single-element list represents an appropriate MIME
696 charset, and a longer list means no appropriate charset."
698 ;; The return possibilities of this function are a mess...
699 (or (and (mm-multibyte-p)
700 mm-use-find-coding-systems-region
701 ;; Find the mime-charset of the most preferred coding
702 ;; system that has one.
703 (let ((systems (find-coding-systems-region b e)))
704 (when mm-coding-system-priorities
706 (sort systems 'mm-sort-coding-systems-predicate)))
707 (setq systems (delq 'compound-text systems))
708 (unless (equal systems '(undecided))
710 (let* ((head (pop systems))
711 (cs (or (coding-system-get head :mime-charset)
712 (coding-system-get head 'mime-charset))))
713 ;; The mime-charset (`x-ctext') of
714 ;; `compound-text' is not in the IANA list. We
715 ;; shouldn't normally use anything here with a
716 ;; mime-charset having an `x-' prefix.
717 ;; Fixme: Allow this to be overridden, since
718 ;; there is existing use of x-ctext.
719 ;; Also people apparently need the coding system
720 ;; `iso-2022-jp-3' (which Mule-UCS defines with
721 ;; mime-charset, though it's not valid).
723 (not (string-match "^[Xx]-" (symbol-name cs)))
724 ;; UTF-16 of any variety is invalid for
725 ;; text parts and, unfortunately, has
726 ;; mime-charset defined both in Mule-UCS
727 ;; and versions of Emacs. (The name
728 ;; might be `mule-utf-16...' or
730 (not (string-match "utf-16" (symbol-name cs))))
732 charsets (list cs))))))
734 ;; If we're XEmacs, and some coding system is appropriate,
735 ;; mm-xemacs-find-mime-charset will return an appropriate list.
736 ;; Otherwise, we'll get nil, and the next setq will get invoked.
737 (setq charsets (mm-xemacs-find-mime-charset b e))
739 ;; We're not multibyte, or a single coding system won't cover it.
741 (mm-delete-duplicates
742 (mapcar 'mm-mime-charset
744 (mm-find-charset-region b e))))))
745 (if (and (> (length charsets) 1)
746 (memq 'iso-8859-15 charsets)
747 (memq 'iso-8859-15 hack-charsets)
748 (save-excursion (mm-iso-8859-x-to-15-region b e)))
749 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
750 mm-iso-8859-15-compatible))
751 (if (and (memq 'iso-2022-jp-2 charsets)
752 (memq 'iso-2022-jp-2 hack-charsets))
753 (setq charsets (delq 'iso-2022-jp charsets)))
754 ;; Attempt to reduce the number of charsets if utf-8 is available.
755 (if (and (featurep 'xemacs)
756 (> (length charsets) 1)
757 (mm-coding-system-p 'utf-8))
758 (let ((mm-coding-system-priorities
759 (cons 'utf-8 mm-coding-system-priorities)))
761 (mm-delete-duplicates
762 (mapcar 'mm-mime-charset
764 (mm-find-charset-region b e)))))))
767 (defmacro mm-with-unibyte-buffer (&rest forms)
768 "Create a temporary buffer, and evaluate FORMS there like `progn'.
769 Use unibyte mode for this."
770 `(let (default-enable-multibyte-characters)
771 (with-temp-buffer ,@forms)))
772 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
773 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
775 (defmacro mm-with-multibyte-buffer (&rest forms)
776 "Create a temporary buffer, and evaluate FORMS there like `progn'.
777 Use multibyte mode for this."
778 `(let ((default-enable-multibyte-characters t))
779 (with-temp-buffer ,@forms)))
780 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
781 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
783 (defmacro mm-with-unibyte-current-buffer (&rest forms)
784 "Evaluate FORMS with current buffer temporarily made unibyte.
785 Also bind `default-enable-multibyte-characters' to nil.
786 Equivalent to `progn' in XEmacs"
787 (let ((multibyte (make-symbol "multibyte"))
788 (buffer (make-symbol "buffer")))
790 (let ((,multibyte enable-multibyte-characters)
791 (,buffer (current-buffer)))
793 (let (default-enable-multibyte-characters)
794 (set-buffer-multibyte nil)
797 (set-buffer-multibyte ,multibyte)))
798 (let (default-enable-multibyte-characters)
800 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
801 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
803 (defmacro mm-with-unibyte (&rest forms)
804 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
805 `(let (default-enable-multibyte-characters)
807 (put 'mm-with-unibyte 'lisp-indent-function 0)
808 (put 'mm-with-unibyte 'edebug-form-spec '(body))
810 (defmacro mm-with-multibyte (&rest forms)
811 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
812 `(let ((default-enable-multibyte-characters t))
814 (put 'mm-with-multibyte 'lisp-indent-function 0)
815 (put 'mm-with-multibyte 'edebug-form-spec '(body))
817 (defun mm-find-charset-region (b e)
818 "Return a list of Emacs charsets in the region B to E."
820 ((and (mm-multibyte-p)
821 (fboundp 'find-charset-region))
822 ;; Remove composition since the base charsets have been included.
823 ;; Remove eight-bit-*, treat them as ascii.
824 (let ((css (find-charset-region b e)))
825 (mapcar (lambda (cs) (setq css (delq cs css)))
826 '(composition eight-bit-control eight-bit-graphic
830 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
833 (narrow-to-region b e)
834 (goto-char (point-min))
835 (skip-chars-forward "\0-\177")
840 (and (boundp 'current-language-environment)
841 (car (last (assq 'charset
842 (assoc current-language-environment
843 language-info-alist))))))
844 (if (eq charset 'ascii) (setq charset nil))
847 (car (last (assq mail-parse-charset
848 mm-mime-mule-charset-alist)))))
849 (list 'ascii (or charset 'latin-iso8859-1)))))))))
851 (defun mm-auto-mode-alist ()
852 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
853 (let ((alist auto-mode-alist)
856 (when (listp (cdar alist))
857 (push (car alist) out))
861 (defvar mm-inhibit-file-name-handlers
862 '(jka-compr-handler image-file-handler)
863 "A list of handlers doing (un)compression (etc) thingies.")
865 (defun mm-insert-file-contents (filename &optional visit beg end replace
867 "Like `insert-file-contents', but only reads in the file.
868 A buffer may be modified in several ways after reading into the buffer due
869 to advanced Emacs features, such as file-name-handlers, format decoding,
870 `find-file-hooks', etc.
871 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
872 This function ensures that none of these modifications will take place."
873 (let* ((format-alist nil)
874 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
875 (default-major-mode 'fundamental-mode)
876 (enable-local-variables nil)
877 (after-insert-file-functions nil)
878 (enable-local-eval nil)
879 (inhibit-file-name-operation (if inhibit
880 'insert-file-contents
881 inhibit-file-name-operation))
882 (inhibit-file-name-handlers
884 (append mm-inhibit-file-name-handlers
885 inhibit-file-name-handlers)
886 inhibit-file-name-handlers))
887 (ffh (if (boundp 'find-file-hook)
890 (val (symbol-value ffh)))
893 (insert-file-contents filename visit beg end replace)
896 (defun mm-append-to-file (start end filename &optional codesys inhibit)
897 "Append the contents of the region to the end of file FILENAME.
898 When called from a function, expects three arguments,
899 START, END and FILENAME. START and END are buffer positions
900 saying what text to write.
901 Optional fourth argument specifies the coding system to use when
903 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
904 (let ((coding-system-for-write
905 (or codesys mm-text-coding-system-for-write
906 mm-text-coding-system))
907 (inhibit-file-name-operation (if inhibit
909 inhibit-file-name-operation))
910 (inhibit-file-name-handlers
912 (append mm-inhibit-file-name-handlers
913 inhibit-file-name-handlers)
914 inhibit-file-name-handlers)))
915 (write-region start end filename t 'no-message)
916 (message "Appended to %s" filename)))
918 (defun mm-write-region (start end filename &optional append visit lockname
919 coding-system inhibit)
921 "Like `write-region'.
922 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
923 (let ((coding-system-for-write
924 (or coding-system 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 append visit lockname)))
936 (defun mm-image-load-path (&optional package)
938 (dolist (path load-path (nreverse result))
941 (setq dir (concat (file-name-directory
942 (directory-file-name path))
943 "etc/images/" (or package "gnus/")))))
945 (push path result))))
947 ;; Fixme: This doesn't look useful where it's used.
948 (if (fboundp 'detect-coding-region)
949 (defun mm-detect-coding-region (start end)
950 "Like `detect-coding-region' except returning the best one."
951 (let ((coding-systems
952 (detect-coding-region start end)))
953 (or (car-safe coding-systems)
955 (defun mm-detect-coding-region (start end)
956 (let ((point (point)))
958 (skip-chars-forward "\0-\177" end)
960 (if (eq (point) end) 'ascii (mm-guess-charset))
961 (goto-char point)))))
963 (if (fboundp 'coding-system-get)
964 (defun mm-detect-mime-charset-region (start end)
965 "Detect MIME charset of the text in the region between START and END."
966 (let ((cs (mm-detect-coding-region start end)))
967 (or (coding-system-get cs :mime-charset)
968 (coding-system-get cs 'mime-charset))))
969 (defun mm-detect-mime-charset-region (start end)
970 "Detect MIME charset of the text in the region between START and END."
971 (let ((cs (mm-detect-coding-region start end)))
975 (unless (fboundp 'coding-system-to-mime-charset)
976 (defalias 'coding-system-to-mime-charset 'ignore)))
978 (defun mm-coding-system-to-mime-charset (coding-system)
979 "Return the MIME charset corresponding to CODING-SYSTEM.
980 To make this function work with XEmacs, the APEL package is required."
982 (or (and (fboundp 'coding-system-get)
983 (or (coding-system-get coding-system :mime-charset)
984 (coding-system-get coding-system 'mime-charset)))
985 (and (featurep 'xemacs)
986 (or (and (fboundp 'coding-system-to-mime-charset)
987 (not (eq (symbol-function 'coding-system-to-mime-charset)
989 (and (condition-case nil
992 (fboundp 'coding-system-to-mime-charset)))
993 (coding-system-to-mime-charset coding-system)))))
996 (require 'jka-compr))
998 (defun mm-decompress-buffer (filename &optional inplace force)
999 "Decompress buffer's contents, depending on jka-compr.
1000 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
1001 agrees with `jka-compr-compression-info-list', decompression is done.
1002 Signal an error if FORCE is neither nil nor t and compressed data are
1003 not decompressed because `auto-compression-mode' is disabled.
1004 If INPLACE is nil, return decompressed data or nil without modifying
1005 the buffer. Otherwise, replace the buffer's contents with the
1006 decompressed data. The buffer's multibyteness must be turned off."
1009 (prog1 t (require 'jka-compr))
1010 (and (fboundp 'jka-compr-installed-p)
1011 (jka-compr-installed-p))))
1012 (let ((info (jka-compr-get-compression-info filename)))
1014 (unless (or (memq force (list nil t))
1015 (jka-compr-installed-p))
1017 (let ((prog (jka-compr-info-uncompress-program info))
1018 (args (jka-compr-info-uncompress-args info))
1019 (msg (format "%s %s..."
1020 (jka-compr-info-uncompress-message info)
1022 (err-file (jka-compr-make-temp-name))
1023 (cur (current-buffer))
1024 (coding-system-for-read mm-binary-coding-system)
1025 (coding-system-for-write mm-binary-coding-system)
1029 (insert-buffer-substring cur)
1032 (unless (memq (apply 'call-process-region
1033 (point-min) (point-max)
1034 prog t (list t err-file) nil args)
1035 jka-compr-acceptable-retval-list)
1039 (delete "" (split-string
1041 (insert-file-contents err-file)
1047 (format "Error while executing \"%s %s < %s\""
1048 prog (mapconcat 'identity args " ")
1050 (setq retval (buffer-string)))
1052 (setq err-msg (error-message-string err)))))
1053 (when (file-exists-p err-file)
1054 (ignore-errors (jka-compr-delete-temp-file err-file)))
1057 (delete-region (point-min) (point-max))
1060 (message "%s" (or err-msg (concat msg "done")))
1064 (unless (fboundp 'coding-system-name)
1065 (defalias 'coding-system-name 'ignore))
1066 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1067 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1068 (unless (fboundp 'find-operation-coding-system)
1069 (defalias 'find-operation-coding-system 'ignore)))
1071 (defun mm-find-buffer-file-coding-system (&optional filename)
1072 "Find coding system used to decode the contents of the current buffer.
1073 This function looks for the coding system magic cookie or examines the
1074 coding system specified by `file-coding-system-alist' being associated
1075 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1076 gzip, bzip2, etc. are allowed."
1078 (setq filename buffer-file-name))
1080 (let ((decomp (unless ;; No worth to examine charset of tar files.
1083 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1085 (mm-decompress-buffer filename nil t))))
1087 (set-buffer (let (default-enable-multibyte-characters)
1088 (generate-new-buffer " *temp*")))
1090 (setq filename (file-name-sans-extension filename)))
1091 (goto-char (point-min))
1094 ((boundp 'set-auto-coding-function) ;; Emacs
1096 (or (funcall (symbol-value 'set-auto-coding-function)
1097 filename (- (point-max) (point-min)))
1098 (car (find-operation-coding-system 'insert-file-contents
1100 (let (auto-coding-alist)
1102 (funcall (symbol-value 'set-auto-coding-function)
1103 nil (- (point-max) (point-min)))
1105 ((featurep 'file-coding) ;; XEmacs
1106 (let ((case-fold-search t)
1107 (end (point-at-eol))
1110 (and (re-search-forward "-\\*-+[\t ]*" end t)
1112 (setq start (match-end 0))
1113 (re-search-forward "[\t ]*-+\\*-" end t))
1115 (setq end (match-beginning 0))
1117 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1119 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1121 (find-coding-system (setq codesys
1122 (intern (match-string 1))))
1124 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1127 (setq start (match-end 0))
1128 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1130 (setq end (match-beginning 0))
1133 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1135 (find-coding-system (setq codesys
1136 (intern (match-string 1))))
1139 (goto-char (point-min))
1140 (setq case-fold-search nil)
1141 (re-search-forward "^;;;coding system: "
1142 ;;(+ (point-min) 3000) t))
1144 (looking-at "[^\t\n\r ]+")
1146 (setq codesys (intern (match-string 0))))
1150 (find-file-coding-system-for-read-from-filename
1152 (coding-system-name (coding-system-base codesys)))))))
1154 (kill-buffer (current-buffer)))))))
1158 ;;; mm-util.el ends here