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.
37 (let ((nfunc (intern (format "mm-%s" (car elem)))))
38 (if (fboundp (car elem))
39 (defalias nfunc (car elem))
40 (defalias nfunc (cdr elem)))))
41 '((decode-coding-string . (lambda (s a) s))
42 (encode-coding-string . (lambda (s a) s))
43 (encode-coding-region . ignore)
44 (coding-system-list . ignore)
45 (decode-coding-region . ignore)
47 (coding-system-equal . equal)
48 (annotationp . ignore)
49 (set-buffer-file-coding-system . ignore)
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)
74 . (lambda (string regexp rep &optional literal)
75 "See `replace-regexp-in-string', only the order of args differs."
76 (replace-regexp-in-string regexp rep string nil literal)))
77 (string-as-unibyte . identity)
78 (string-make-unibyte . identity)
79 ;; string-as-multibyte often doesn't really do what you think it does.
81 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
82 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
83 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
84 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
86 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
87 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
88 ;; Better use string-to-multibyte or encode-coding-string.
89 ;; If you really need string-as-multibyte somewhere it's usually
90 ;; because you're using the internal emacs-mule representation (maybe
91 ;; because you're using string-as-unibyte somewhere), which is
92 ;; generally a problem in itself.
93 ;; Here is an approximate equivalence table to help think about it:
94 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
95 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
96 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
97 (string-as-multibyte . identity)
100 "Return a multibyte string with the same individual chars as string."
102 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
104 (multibyte-string-p . ignore)
105 ;; It is not a MIME function, but some MIME functions use it.
106 (make-temp-file . (lambda (prefix &optional dir-flag)
107 (let ((file (expand-file-name
108 (make-temp-name prefix)
109 (if (fboundp 'temp-directory)
111 temporary-file-directory))))
113 (make-directory file))
115 (insert-byte . insert-char)
116 (multibyte-char-to-unibyte . identity))))
119 (defalias 'mm-char-or-char-int-p
121 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
122 ((fboundp 'char-valid-p) 'char-valid-p)
125 ;; Fixme: This seems always to be used to read a MIME charset, so it
126 ;; should be re-named and fixed (in Emacs) to offer completion only on
127 ;; proper charset names (base coding systems which have a
128 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
130 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
131 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
132 ;; Actually, there should be an `mm-coding-system-mime-charset'.
134 (defalias 'mm-read-coding-system
136 ((fboundp 'read-coding-system)
137 (if (and (featurep 'xemacs)
138 (<= (string-to-number emacs-version) 21.1))
139 (lambda (prompt &optional default-coding-system)
140 (read-coding-system prompt))
141 'read-coding-system))
142 (t (lambda (prompt &optional default-coding-system)
143 "Prompt the user for a coding system."
145 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
146 mm-mime-mule-charset-alist)))))))
148 (defvar mm-coding-system-list nil)
149 (defun mm-get-coding-system-list ()
150 "Get the coding system list."
151 (or mm-coding-system-list
152 (setq mm-coding-system-list (mm-coding-system-list))))
154 (defun mm-coding-system-p (cs)
155 "Return non-nil if CS is a symbol naming a coding system.
156 In XEmacs, also return non-nil if CS is a coding system object.
157 If CS is available, return CS itself in Emacs, and return a coding
158 system object in XEmacs."
159 (if (fboundp 'find-coding-system)
160 (and cs (find-coding-system cs))
161 (if (fboundp 'coding-system-p)
162 (when (coding-system-p cs)
164 ;; Is this branch ever actually useful?
165 (car (memq cs (mm-get-coding-system-list))))))
167 (defvar mm-charset-synonym-alist
169 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
170 ,@(unless (mm-coding-system-p 'x-ctext)
171 '((x-ctext . ctext)))
172 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
173 ,@(unless (mm-coding-system-p 'iso-8859-15)
174 '((iso-8859-15 . iso-8859-1)))
175 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
176 ,@(unless (mm-coding-system-p 'big5-hkscs)
177 '((big5-hkscs . big5)))
178 ;; Windows-1252 is actually a superset of Latin-1. See also
179 ;; `gnus-article-dumbquotes-map'.
180 ,@(unless (mm-coding-system-p 'windows-1252)
181 (if (mm-coding-system-p 'cp1252)
182 '((windows-1252 . cp1252))
183 '((windows-1252 . iso-8859-1))))
184 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
185 ;; Outlook users in Czech republic. Use this to allow reading of their
186 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
187 ,@(if (and (not (mm-coding-system-p 'windows-1250))
188 (mm-coding-system-p 'cp1250))
189 '((windows-1250 . cp1250)))
190 ;; A Microsoft misunderstanding.
191 ,@(if (and (not (mm-coding-system-p 'unicode))
192 (mm-coding-system-p 'utf-16-le))
193 '((unicode . utf-16-le)))
194 ;; A Microsoft misunderstanding.
195 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
196 (if (mm-coding-system-p 'cp949)
197 '((ks_c_5601-1987 . cp949))
198 '((ks_c_5601-1987 . euc-kr))))
200 "A mapping from invalid charset names to the real charset names.")
202 (defvar mm-binary-coding-system
204 ((mm-coding-system-p 'binary) 'binary)
205 ((mm-coding-system-p 'no-conversion) 'no-conversion)
207 "100% binary coding system.")
209 (defvar mm-text-coding-system
210 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
211 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
212 (and (mm-coding-system-p 'raw-text) 'raw-text))
213 mm-binary-coding-system)
214 "Text-safe coding system (For removing ^M).")
216 (defvar mm-text-coding-system-for-write nil
217 "Text coding system for write.")
219 (defvar mm-auto-save-coding-system
221 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
222 (if (memq system-type '(windows-nt ms-dos ms-windows))
223 (if (mm-coding-system-p 'utf-8-emacs-dos)
224 'utf-8-emacs-dos mm-binary-coding-system)
226 ((mm-coding-system-p 'emacs-mule)
227 (if (memq system-type '(windows-nt ms-dos ms-windows))
228 (if (mm-coding-system-p 'emacs-mule-dos)
229 'emacs-mule-dos mm-binary-coding-system)
231 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
232 (t mm-binary-coding-system))
233 "Coding system of auto save file.")
235 (defvar mm-universal-coding-system mm-auto-save-coding-system
236 "The universal coding system.")
238 ;; Fixme: some of the cars here aren't valid MIME charsets. That
239 ;; should only matter with XEmacs, though.
240 (defvar mm-mime-mule-charset-alist
242 (iso-8859-1 latin-iso8859-1)
243 (iso-8859-2 latin-iso8859-2)
244 (iso-8859-3 latin-iso8859-3)
245 (iso-8859-4 latin-iso8859-4)
246 (iso-8859-5 cyrillic-iso8859-5)
247 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
248 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
249 ;; charset is koi8-r, not iso-8859-5.
250 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
251 (iso-8859-6 arabic-iso8859-6)
252 (iso-8859-7 greek-iso8859-7)
253 (iso-8859-8 hebrew-iso8859-8)
254 (iso-8859-9 latin-iso8859-9)
255 (iso-8859-14 latin-iso8859-14)
256 (iso-8859-15 latin-iso8859-15)
257 (viscii vietnamese-viscii-lower)
258 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
259 (euc-kr korean-ksc5601)
260 (gb2312 chinese-gb2312)
261 (big5 chinese-big5-1 chinese-big5-2)
263 (thai-tis620 thai-tis620)
264 (windows-1251 cyrillic-iso8859-5)
265 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
266 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
267 latin-jisx0201 japanese-jisx0208-1978
268 chinese-gb2312 japanese-jisx0208
269 korean-ksc5601 japanese-jisx0212)
270 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
271 latin-jisx0201 japanese-jisx0208-1978
272 chinese-gb2312 japanese-jisx0208
273 korean-ksc5601 japanese-jisx0212
274 chinese-cns11643-1 chinese-cns11643-2)
275 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
276 cyrillic-iso8859-5 greek-iso8859-7
277 latin-jisx0201 japanese-jisx0208-1978
278 chinese-gb2312 japanese-jisx0208
279 korean-ksc5601 japanese-jisx0212
280 chinese-cns11643-1 chinese-cns11643-2
281 chinese-cns11643-3 chinese-cns11643-4
282 chinese-cns11643-5 chinese-cns11643-6
284 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
285 japanese-jisx0213-1 japanese-jisx0213-2)
286 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
287 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
288 (charsetp 'unicode-a)
289 (not (mm-coding-system-p 'mule-utf-8)))
290 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
291 ;; If we have utf-8 we're in Mule 5+.
294 (coding-system-get 'mule-utf-8 'safe-charsets)))))
295 "Alist of MIME-charset/MULE-charsets.")
297 (defun mm-enrich-utf-8-by-mule-ucs ()
298 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
299 This function will run when the `un-define' module is loaded under
300 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
301 with Mule charsets. It is completely useless for Emacs."
302 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
303 (assoc "un-define" after-load-alist)))
304 (setq after-load-alist
305 (delete '("un-define") after-load-alist)))
306 (when (boundp 'unicode-basic-translation-charset-order-list)
312 'unicode-basic-translation-charset-order-list))))
313 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
316 (setq mm-mime-mule-charset-alist
317 (nconc mm-mime-mule-charset-alist
318 (list (cons 'utf-8 val))))))
321 ;; Correct by construction, but should be unnecessary for Emacs:
322 (if (featurep 'xemacs)
323 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
324 (when (and (fboundp 'coding-system-list)
325 (fboundp 'sort-coding-systems))
326 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
330 mime (or (coding-system-get cs :mime-charset) ; Emacs 22
331 (coding-system-get cs 'mime-charset)))
333 (not (eq t (setq mule
334 (coding-system-get cs 'safe-charsets))))
335 (not (assq mime alist)))
336 (push (cons mime (delq 'ascii mule)) alist)))
337 (setq mm-mime-mule-charset-alist (nreverse alist)))))
339 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
340 "A list of special charsets.
341 Valid elements include:
342 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
343 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
346 (defvar mm-iso-8859-15-compatible
347 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
348 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
349 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
351 (defvar mm-iso-8859-x-to-15-table
352 (and (fboundp 'coding-system-p)
353 (mm-coding-system-p 'iso-8859-15)
356 (if (mm-coding-system-p (car cs))
357 (let ((c (string-to-char
358 (decode-coding-string "\341" (car cs)))))
359 (cons (char-charset c)
362 (decode-coding-string "\341" 'iso-8859-15)) c)
363 (string-to-list (decode-coding-string (car (cdr cs))
366 mm-iso-8859-15-compatible))
367 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
369 (defcustom mm-coding-system-priorities
370 (if (boundp 'current-language-environment)
371 (let ((lang (symbol-value 'current-language-environment)))
372 (cond ((string= lang "Japanese")
373 ;; Japanese users prefer iso-2022-jp to euc-japan or
374 ;; shift_jis, however iso-8859-1 should be used when
375 ;; there are only ASCII text and Latin-1 characters.
376 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
377 "Preferred coding systems for encoding outgoing messages.
379 More than one suitable coding system may be found for some text.
380 By default, the coding system with the highest priority is used
381 to encode outgoing messages (see `sort-coding-systems'). If this
382 variable is set, it overrides the default priority."
384 :type '(repeat (symbol :tag "Coding system"))
388 (defvar mm-use-find-coding-systems-region
389 (fboundp 'find-coding-systems-region)
390 "Use `find-coding-systems-region' to find proper coding systems.
392 Setting it to nil is useful on Emacsen supporting Unicode if sending
393 mail with multiple parts is preferred to sending a Unicode one.")
395 ;;; Internal variables:
399 (defun mm-mule-charset-to-mime-charset (charset)
400 "Return the MIME charset corresponding to the given Mule CHARSET."
401 (if (and (fboundp 'find-coding-systems-for-charsets)
402 (fboundp 'sort-coding-systems))
403 (let ((css (sort (sort-coding-systems
404 (find-coding-systems-for-charsets (list charset)))
405 'mm-sort-coding-systems-predicate))
407 (while (and (not mime)
409 (when (setq cs (pop css))
410 (setq mime (or (coding-system-get cs :mime-charset)
411 (coding-system-get cs 'mime-charset)))))
413 (let ((alist (mapcar (lambda (cs)
414 (assq cs mm-mime-mule-charset-alist))
415 (sort (mapcar 'car mm-mime-mule-charset-alist)
416 'mm-sort-coding-systems-predicate)))
419 (when (memq charset (cdar alist))
420 (setq out (caar alist)
425 (defun mm-charset-to-coding-system (charset &optional lbt)
426 "Return coding-system corresponding to CHARSET.
427 CHARSET is a symbol naming a MIME charset.
428 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
429 used as the line break code type of the coding system."
430 (when (stringp charset)
431 (setq charset (intern (downcase charset))))
433 (setq charset (intern (format "%s-%s" charset lbt))))
437 ;; Running in a non-MULE environment.
438 ((or (null (mm-get-coding-system-list))
439 (not (fboundp 'coding-system-get)))
442 ((eq charset 'us-ascii)
444 ;; Check to see whether we can handle this charset. (This depends
445 ;; on there being some coding system matching each `mime-charset'
446 ;; property defined, as there should be.)
447 ((and (mm-coding-system-p charset)
448 ;;; Doing this would potentially weed out incorrect charsets.
450 ;;; (eq charset (coding-system-get charset 'mime-charset))
453 ;; Translate invalid charsets.
454 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
455 (and cs (mm-coding-system-p cs) cs)))
456 ;; Last resort: search the coding system list for entries which
457 ;; have the right mime-charset in case the canonical name isn't
458 ;; defined (though it should be).
460 ;; mm-get-coding-system-list returns a list of cs without lbt.
462 (dolist (c (mm-get-coding-system-list))
464 (eq charset (or (coding-system-get c :mime-charset)
465 (coding-system-get c 'mime-charset))))
470 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
471 (boundp 'default-enable-multibyte-characters)
472 default-enable-multibyte-characters
473 (fboundp 'set-buffer-multibyte))
474 "True in Emacs with Mule.")
477 (defun mm-enable-multibyte ()
478 "Set the multibyte flag of the current buffer.
479 Only do this if the default value of `enable-multibyte-characters' is
480 non-nil. This is a no-op in XEmacs."
481 (set-buffer-multibyte 'to))
482 (defalias 'mm-enable-multibyte 'ignore))
485 (defun mm-disable-multibyte ()
486 "Unset the multibyte flag of in the current buffer.
487 This is a no-op in XEmacs."
488 (set-buffer-multibyte nil))
489 (defalias 'mm-disable-multibyte 'ignore)))
491 (defun mm-preferred-coding-system (charset)
492 ;; A typo in some Emacs versions.
493 (or (get-charset-property charset 'preferred-coding-system)
494 (get-charset-property charset 'prefered-coding-system)))
496 ;; Mule charsets shouldn't be used.
497 (defsubst mm-guess-charset ()
498 "Guess Mule charset from the language environment."
500 mail-parse-mule-charset ;; cached mule-charset
502 (setq mail-parse-mule-charset
503 (and (boundp 'current-language-environment)
506 (assoc current-language-environment
507 language-info-alist))))))
508 (if (or (not mail-parse-mule-charset)
509 (eq mail-parse-mule-charset 'ascii))
510 (setq mail-parse-mule-charset
511 (or (car (last (assq mail-parse-charset
512 mm-mime-mule-charset-alist)))
515 mail-parse-mule-charset)))
517 (defun mm-charset-after (&optional pos)
518 "Return charset of a character in current buffer at position POS.
519 If POS is nil, it defauls to the current point.
520 If POS is out of range, the value is nil.
521 If the charset is `composition', return the actual one."
522 (let ((char (char-after pos)) charset)
523 (if (< (mm-char-int char) 128)
524 (setq charset 'ascii)
525 ;; charset-after is fake in some Emacsen.
526 (setq charset (and (fboundp 'char-charset) (char-charset char)))
527 (if (eq charset 'composition) ; Mule 4
528 (let ((p (or pos (point))))
529 (cadr (find-charset-region p (1+ p))))
530 (if (and charset (not (memq charset '(ascii eight-bit-control
531 eight-bit-graphic))))
533 (mm-guess-charset))))))
535 (defun mm-mime-charset (charset)
536 "Return the MIME charset corresponding to the given Mule CHARSET."
537 (if (eq charset 'unknown)
538 (error "The message contains non-printable characters, please use attachment"))
539 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
540 ;; This exists in Emacs 20.
542 (and (mm-preferred-coding-system charset)
543 (or (coding-system-get
544 (mm-preferred-coding-system charset) :mime-charset)
546 (mm-preferred-coding-system charset) 'mime-charset)))
547 (and (eq charset 'ascii)
549 (mm-preferred-coding-system charset)
550 (mm-mule-charset-to-mime-charset charset))
551 ;; This is for XEmacs.
552 (mm-mule-charset-to-mime-charset charset)))
554 (defun mm-delete-duplicates (list)
555 "Simple substitute for CL `delete-duplicates', testing with `equal'."
558 (setq head (car list))
559 (setq list (delete head list))
560 (setq result (cons head result)))
563 ;; Fixme: This is used in places when it should be testing the
564 ;; default multibyteness. See mm-default-multibyte-p.
566 (if (and (not (featurep 'xemacs))
567 (boundp 'enable-multibyte-characters))
568 (defun mm-multibyte-p ()
569 "Non-nil if multibyte is enabled in the current buffer."
570 enable-multibyte-characters)
571 (defun mm-multibyte-p () (featurep 'mule))))
573 (defun mm-default-multibyte-p ()
574 "Return non-nil if the session is multibyte.
575 This affects whether coding conversion should be attempted generally."
577 (if (boundp 'default-enable-multibyte-characters)
578 default-enable-multibyte-characters
581 (defun mm-iso-8859-x-to-15-region (&optional b e)
582 (if (fboundp 'char-charset)
583 (let (charset item c inconvertible)
585 (if e (narrow-to-region b e))
586 (goto-char (point-min))
587 (skip-chars-forward "\0-\177")
590 ((not (setq item (assq (char-charset (setq c (char-after)))
591 mm-iso-8859-x-to-15-table)))
593 ((memq c (cdr (cdr item)))
594 (setq inconvertible t)
597 (insert-before-markers (prog1 (+ c (car (cdr item)))
599 (skip-chars-forward "\0-\177")))
600 (not inconvertible))))
602 (defun mm-sort-coding-systems-predicate (a b)
605 ;; Note: invalid entries are dropped silently
606 (and (setq cs (mm-coding-system-p cs))
607 (coding-system-base cs)))
608 mm-coding-system-priorities)))
609 (and (setq a (mm-coding-system-p a))
610 (if (setq b (mm-coding-system-p b))
611 (> (length (memq (coding-system-base a) priorities))
612 (length (memq (coding-system-base b) priorities)))
616 (autoload 'latin-unity-massage-name "latin-unity")
617 (autoload 'latin-unity-maybe-remap "latin-unity")
618 (autoload 'latin-unity-representations-feasible-region "latin-unity")
619 (autoload 'latin-unity-representations-present-region "latin-unity")
620 (defvar latin-unity-coding-systems)
621 (defvar latin-unity-ucs-list))
623 (defun mm-xemacs-find-mime-charset-1 (begin end)
624 "Determine which MIME charset to use to send region as message.
625 This uses the XEmacs-specific latin-unity package to better handle the
626 case where identical characters from diverse ISO-8859-? character sets
627 can be encoded using a single one of the corresponding coding systems.
629 It treats `mm-coding-system-priorities' as the list of preferred
630 coding systems; a useful example setting for this list in Western
631 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
632 to the very standard Latin 1 coding system, and only move to coding
633 systems that are less supported as is necessary to encode the
634 characters that exist in the buffer.
636 Latin Unity doesn't know about those non-ASCII Roman characters that
637 are available in various East Asian character sets. As such, its
638 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
639 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
640 But this is very much a corner case, so don't worry about it."
641 (let ((systems mm-coding-system-priorities) csets psets curset)
643 ;; Load the Latin Unity library, if available.
644 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
645 (require 'latin-unity))
647 ;; Now, can we use it?
648 (if (featurep 'latin-unity)
650 (setq csets (latin-unity-representations-feasible-region begin end)
651 psets (latin-unity-representations-present-region begin end))
655 ;; Pass back the first coding system in the preferred list
656 ;; that can encode the whole region.
657 (dolist (curset systems)
658 (setq curset (latin-unity-massage-name 'buffer-default curset))
660 ;; If the coding system is a universal coding system, then
661 ;; it can certainly encode all the characters in the region.
662 (if (memq curset latin-unity-ucs-list)
663 (throw 'done (list curset)))
665 ;; If a coding system isn't universal, and isn't in
666 ;; the list that latin unity knows about, we can't
667 ;; decide whether to use it here. Leave that until later
668 ;; in `mm-find-mime-charset-region' function, whence we
670 (unless (memq curset latin-unity-coding-systems)
673 ;; Right, we know about this coding system, and it may
674 ;; conceivably be able to encode all the characters in
676 (if (latin-unity-maybe-remap begin end curset csets psets t)
677 (throw 'done (list curset))))
679 ;; Can't encode using anything from the
680 ;; `mm-coding-system-priorities' list.
681 ;; Leave `mm-find-mime-charset' to do most of the work.
684 ;; Right, latin unity isn't available; let `mm-find-charset-region'
685 ;; take its default action, which equally applies to GNU Emacs.
688 (defmacro mm-xemacs-find-mime-charset (begin end)
689 (when (featurep 'xemacs)
690 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
692 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
693 "Return the MIME charsets needed to encode the region between B and E.
694 nil means ASCII, a single-element list represents an appropriate MIME
695 charset, and a longer list means no appropriate charset."
697 ;; The return possibilities of this function are a mess...
698 (or (and (mm-multibyte-p)
699 mm-use-find-coding-systems-region
700 ;; Find the mime-charset of the most preferred coding
701 ;; system that has one.
702 (let ((systems (find-coding-systems-region b e)))
703 (when mm-coding-system-priorities
705 (sort systems 'mm-sort-coding-systems-predicate)))
706 (setq systems (delq 'compound-text systems))
707 (unless (equal systems '(undecided))
709 (let* ((head (pop systems))
710 (cs (or (coding-system-get head :mime-charset)
711 (coding-system-get head 'mime-charset))))
712 ;; The mime-charset (`x-ctext') of
713 ;; `compound-text' is not in the IANA list. We
714 ;; shouldn't normally use anything here with a
715 ;; mime-charset having an `x-' prefix.
716 ;; Fixme: Allow this to be overridden, since
717 ;; there is existing use of x-ctext.
718 ;; Also people apparently need the coding system
719 ;; `iso-2022-jp-3' (which Mule-UCS defines with
720 ;; mime-charset, though it's not valid).
722 (not (string-match "^[Xx]-" (symbol-name cs)))
723 ;; UTF-16 of any variety is invalid for
724 ;; text parts and, unfortunately, has
725 ;; mime-charset defined both in Mule-UCS
726 ;; and versions of Emacs. (The name
727 ;; might be `mule-utf-16...' or
729 (not (string-match "utf-16" (symbol-name cs))))
731 charsets (list cs))))))
733 ;; If we're XEmacs, and some coding system is appropriate,
734 ;; mm-xemacs-find-mime-charset will return an appropriate list.
735 ;; Otherwise, we'll get nil, and the next setq will get invoked.
736 (setq charsets (mm-xemacs-find-mime-charset b e))
738 ;; We're not multibyte, or a single coding system won't cover it.
740 (mm-delete-duplicates
741 (mapcar 'mm-mime-charset
743 (mm-find-charset-region b e))))))
744 (if (and (> (length charsets) 1)
745 (memq 'iso-8859-15 charsets)
746 (memq 'iso-8859-15 hack-charsets)
747 (save-excursion (mm-iso-8859-x-to-15-region b e)))
748 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
749 mm-iso-8859-15-compatible))
750 (if (and (memq 'iso-2022-jp-2 charsets)
751 (memq 'iso-2022-jp-2 hack-charsets))
752 (setq charsets (delq 'iso-2022-jp charsets)))
755 (defmacro mm-with-unibyte-buffer (&rest forms)
756 "Create a temporary buffer, and evaluate FORMS there like `progn'.
757 Use unibyte mode for this."
758 `(let (default-enable-multibyte-characters)
759 (with-temp-buffer ,@forms)))
760 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
761 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
763 (defmacro mm-with-multibyte-buffer (&rest forms)
764 "Create a temporary buffer, and evaluate FORMS there like `progn'.
765 Use multibyte mode for this."
766 `(let ((default-enable-multibyte-characters t))
767 (with-temp-buffer ,@forms)))
768 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
769 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
771 (defmacro mm-with-unibyte-current-buffer (&rest forms)
772 "Evaluate FORMS with current buffer temporarily made unibyte.
773 Also bind `default-enable-multibyte-characters' to nil.
774 Equivalent to `progn' in XEmacs"
775 (let ((multibyte (make-symbol "multibyte"))
776 (buffer (make-symbol "buffer")))
778 (let ((,multibyte enable-multibyte-characters)
779 (,buffer (current-buffer)))
781 (let (default-enable-multibyte-characters)
782 (set-buffer-multibyte nil)
785 (set-buffer-multibyte ,multibyte)))
786 (let (default-enable-multibyte-characters)
788 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
789 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
791 (defmacro mm-with-unibyte (&rest forms)
792 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
793 `(let (default-enable-multibyte-characters)
795 (put 'mm-with-unibyte 'lisp-indent-function 0)
796 (put 'mm-with-unibyte 'edebug-form-spec '(body))
798 (defmacro mm-with-multibyte (&rest forms)
799 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
800 `(let ((default-enable-multibyte-characters t))
802 (put 'mm-with-multibyte 'lisp-indent-function 0)
803 (put 'mm-with-multibyte 'edebug-form-spec '(body))
805 (defun mm-find-charset-region (b e)
806 "Return a list of Emacs charsets in the region B to E."
808 ((and (mm-multibyte-p)
809 (fboundp 'find-charset-region))
810 ;; Remove composition since the base charsets have been included.
811 ;; Remove eight-bit-*, treat them as ascii.
812 (let ((css (find-charset-region b e)))
813 (mapcar (lambda (cs) (setq css (delq cs css)))
814 '(composition eight-bit-control eight-bit-graphic
818 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
821 (narrow-to-region b e)
822 (goto-char (point-min))
823 (skip-chars-forward "\0-\177")
828 (and (boundp 'current-language-environment)
829 (car (last (assq 'charset
830 (assoc current-language-environment
831 language-info-alist))))))
832 (if (eq charset 'ascii) (setq charset nil))
835 (car (last (assq mail-parse-charset
836 mm-mime-mule-charset-alist)))))
837 (list 'ascii (or charset 'latin-iso8859-1)))))))))
839 (defun mm-auto-mode-alist ()
840 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
841 (let ((alist auto-mode-alist)
844 (when (listp (cdar alist))
845 (push (car alist) out))
849 (defvar mm-inhibit-file-name-handlers
850 '(jka-compr-handler image-file-handler)
851 "A list of handlers doing (un)compression (etc) thingies.")
853 (defun mm-insert-file-contents (filename &optional visit beg end replace
855 "Like `insert-file-contents', but only reads in the file.
856 A buffer may be modified in several ways after reading into the buffer due
857 to advanced Emacs features, such as file-name-handlers, format decoding,
858 `find-file-hooks', etc.
859 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
860 This function ensures that none of these modifications will take place."
861 (let* ((format-alist nil)
862 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
863 (default-major-mode 'fundamental-mode)
864 (enable-local-variables nil)
865 (after-insert-file-functions nil)
866 (enable-local-eval nil)
867 (inhibit-file-name-operation (if inhibit
868 'insert-file-contents
869 inhibit-file-name-operation))
870 (inhibit-file-name-handlers
872 (append mm-inhibit-file-name-handlers
873 inhibit-file-name-handlers)
874 inhibit-file-name-handlers))
875 (ffh (if (boundp 'find-file-hook)
878 (val (symbol-value ffh)))
881 (insert-file-contents filename visit beg end replace)
884 (defun mm-append-to-file (start end filename &optional codesys inhibit)
885 "Append the contents of the region to the end of file FILENAME.
886 When called from a function, expects three arguments,
887 START, END and FILENAME. START and END are buffer positions
888 saying what text to write.
889 Optional fourth argument specifies the coding system to use when
891 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
892 (let ((coding-system-for-write
893 (or codesys mm-text-coding-system-for-write
894 mm-text-coding-system))
895 (inhibit-file-name-operation (if inhibit
897 inhibit-file-name-operation))
898 (inhibit-file-name-handlers
900 (append mm-inhibit-file-name-handlers
901 inhibit-file-name-handlers)
902 inhibit-file-name-handlers)))
903 (write-region start end filename t 'no-message)
904 (message "Appended to %s" filename)))
906 (defun mm-write-region (start end filename &optional append visit lockname
907 coding-system inhibit)
909 "Like `write-region'.
910 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
911 (let ((coding-system-for-write
912 (or coding-system 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 append visit lockname)))
924 (defun mm-image-load-path (&optional package)
926 (dolist (path load-path (nreverse result))
929 (setq dir (concat (file-name-directory
930 (directory-file-name path))
931 "etc/images/" (or package "gnus/")))))
933 (push path result))))
935 ;; Fixme: This doesn't look useful where it's used.
936 (if (fboundp 'detect-coding-region)
937 (defun mm-detect-coding-region (start end)
938 "Like `detect-coding-region' except returning the best one."
939 (let ((coding-systems
940 (detect-coding-region start end)))
941 (or (car-safe coding-systems)
943 (defun mm-detect-coding-region (start end)
944 (let ((point (point)))
946 (skip-chars-forward "\0-\177" end)
948 (if (eq (point) end) 'ascii (mm-guess-charset))
949 (goto-char point)))))
951 (if (fboundp 'coding-system-get)
952 (defun mm-detect-mime-charset-region (start end)
953 "Detect MIME charset of the text in the region between START and END."
954 (let ((cs (mm-detect-coding-region start end)))
955 (or (coding-system-get cs :mime-charset)
956 (coding-system-get cs 'mime-charset))))
957 (defun mm-detect-mime-charset-region (start end)
958 "Detect MIME charset of the text in the region between START and END."
959 (let ((cs (mm-detect-coding-region start end)))
963 (unless (fboundp 'coding-system-to-mime-charset)
964 (defalias 'coding-system-to-mime-charset 'ignore)))
966 (defun mm-coding-system-to-mime-charset (coding-system)
967 "Return the MIME charset corresponding to CODING-SYSTEM.
968 To make this function work with XEmacs, the APEL package is required."
970 (or (and (fboundp 'coding-system-get)
971 (or (coding-system-get coding-system :mime-charset)
972 (coding-system-get coding-system 'mime-charset)))
973 (and (featurep 'xemacs)
974 (or (and (fboundp 'coding-system-to-mime-charset)
975 (not (eq (symbol-function 'coding-system-to-mime-charset)
977 (and (condition-case nil
980 (fboundp 'coding-system-to-mime-charset)))
981 (coding-system-to-mime-charset coding-system)))))
984 (require 'jka-compr))
986 (defun mm-decompress-buffer (filename &optional inplace force)
987 "Decompress buffer's contents, depending on jka-compr.
988 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
989 agrees with `jka-compr-compression-info-list', decompression is done.
990 Signal an error if FORCE is neither nil nor t and compressed data are
991 not decompressed because `auto-compression-mode' is disabled.
992 If INPLACE is nil, return decompressed data or nil without modifying
993 the buffer. Otherwise, replace the buffer's contents with the
994 decompressed data. The buffer's multibyteness must be turned off."
997 (prog1 t (require 'jka-compr))
998 (and (fboundp 'jka-compr-installed-p)
999 (jka-compr-installed-p))))
1000 (let ((info (jka-compr-get-compression-info filename)))
1002 (unless (or (memq force (list nil t))
1003 (jka-compr-installed-p))
1005 (let ((prog (jka-compr-info-uncompress-program info))
1006 (args (jka-compr-info-uncompress-args info))
1007 (msg (format "%s %s..."
1008 (jka-compr-info-uncompress-message info)
1010 (err-file (jka-compr-make-temp-name))
1011 (cur (current-buffer))
1012 (coding-system-for-read mm-binary-coding-system)
1013 (coding-system-for-write mm-binary-coding-system)
1017 (insert-buffer-substring cur)
1020 (unless (memq (apply 'call-process-region
1021 (point-min) (point-max)
1022 prog t (list t err-file) nil args)
1023 jka-compr-acceptable-retval-list)
1027 (delete "" (split-string
1029 (insert-file-contents err-file)
1035 (format "Error while executing \"%s %s < %s\""
1036 prog (mapconcat 'identity args " ")
1038 (setq retval (buffer-string)))
1040 (setq err-msg (error-message-string err)))))
1041 (when (file-exists-p err-file)
1042 (ignore-errors (jka-compr-delete-temp-file err-file)))
1045 (delete-region (point-min) (point-max))
1048 (message "%s" (or err-msg (concat msg "done")))
1052 (unless (fboundp 'coding-system-name)
1053 (defalias 'coding-system-name 'ignore))
1054 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1055 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1056 (unless (fboundp 'find-operation-coding-system)
1057 (defalias 'find-operation-coding-system 'ignore)))
1059 (defun mm-find-buffer-file-coding-system (&optional filename)
1060 "Find coding system used to decode the contents of the current buffer.
1061 This function looks for the coding system magic cookie or examines the
1062 coding system specified by `file-coding-system-alist' being associated
1063 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1064 gzip, bzip2, etc. are allowed."
1066 (setq filename buffer-file-name))
1068 (let ((decomp (unless ;; No worth to examine charset of tar files.
1071 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1073 (mm-decompress-buffer filename nil t))))
1075 (set-buffer (let (default-enable-multibyte-characters)
1076 (generate-new-buffer " *temp*")))
1078 (setq filename (file-name-sans-extension filename)))
1079 (goto-char (point-min))
1082 ((boundp 'set-auto-coding-function) ;; Emacs
1084 (or (funcall (symbol-value 'set-auto-coding-function)
1085 filename (- (point-max) (point-min)))
1086 (car (find-operation-coding-system 'insert-file-contents
1088 (let (auto-coding-alist)
1090 (funcall (symbol-value 'set-auto-coding-function)
1091 nil (- (point-max) (point-min)))
1093 ((featurep 'file-coding) ;; XEmacs
1094 (let ((case-fold-search t)
1095 (end (point-at-eol))
1098 (and (re-search-forward "-\\*-+[\t ]*" end t)
1100 (setq start (match-end 0))
1101 (re-search-forward "[\t ]*-+\\*-" end t))
1103 (setq end (match-beginning 0))
1105 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1107 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1109 (find-coding-system (setq codesys
1110 (intern (match-string 1))))
1112 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1115 (setq start (match-end 0))
1116 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1118 (setq end (match-beginning 0))
1121 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1123 (find-coding-system (setq codesys
1124 (intern (match-string 1))))
1127 (goto-char (point-min))
1128 (setq case-fold-search nil)
1129 (re-search-forward "^;;;coding system: "
1130 ;;(+ (point-min) 3000) t))
1132 (looking-at "[^\t\n\r ]+")
1134 (setq codesys (intern (match-string 0))))
1138 (find-file-coding-system-for-read-from-filename
1140 (coding-system-name (coding-system-base codesys)))))))
1142 (kill-buffer (current-buffer)))))))
1146 ;;; mm-util.el ends here