1 ;;; mm-util.el --- Utility functions for Mule and low level things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
34 (let ((nfunc (intern (format "mm-%s" (car elem)))))
35 (if (fboundp (car elem))
36 (defalias nfunc (car elem))
37 (defalias nfunc (cdr elem)))))
38 '((decode-coding-string . (lambda (s a) s))
39 (encode-coding-string . (lambda (s a) s))
40 (encode-coding-region . ignore)
41 (coding-system-list . ignore)
42 (decode-coding-region . ignore)
44 (coding-system-equal . equal)
45 (annotationp . ignore)
46 (set-buffer-file-coding-system . ignore)
53 (mapcar (lambda (e) (list (symbol-name (car e))))
54 mm-mime-mule-charset-alist)
57 . (lambda (from to string &optional inplace)
58 ;; stolen (and renamed) from nnheader.el
59 "Replace characters in STRING from FROM to TO.
60 Unless optional argument INPLACE is non-nil, return a new string."
61 (let ((string (if inplace string (copy-sequence string)))
64 ;; Replace all occurrences of FROM with TO.
66 (when (= (aref string idx) from)
71 . (lambda (string regexp rep &optional literal)
72 "See `replace-regexp-in-string', only the order of args differs."
73 (replace-regexp-in-string regexp rep string nil literal)))
74 (string-as-unibyte . identity)
75 (string-make-unibyte . identity)
76 ;; string-as-multibyte often doesn't really do what you think it does.
78 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
79 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
80 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
81 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
83 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
84 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
85 ;; Better use string-to-multibyte or encode-coding-string.
86 ;; If you really need string-as-multibyte somewhere it's usually
87 ;; because you're using the internal emacs-mule representation (maybe
88 ;; because you're using string-as-unibyte somewhere), which is
89 ;; generally a problem in itself.
90 ;; Here is an approximate equivalence table to help think about it:
91 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
92 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
93 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
94 (string-as-multibyte . identity)
97 "Return a multibyte string with the same individual chars as string."
99 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
101 (multibyte-string-p . ignore)
102 ;; It is not a MIME function, but some MIME functions use it.
103 (make-temp-file . (lambda (prefix &optional dir-flag)
104 (let ((file (expand-file-name
105 (make-temp-name prefix)
106 (if (fboundp 'temp-directory)
108 temporary-file-directory))))
110 (make-directory file))
112 (insert-byte . insert-char)
113 (multibyte-char-to-unibyte . identity))))
116 (defalias 'mm-char-or-char-int-p
118 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
119 ((fboundp 'char-valid-p) 'char-valid-p)
122 ;; Fixme: This seems always to be used to read a MIME charset, so it
123 ;; should be re-named and fixed (in Emacs) to offer completion only on
124 ;; proper charset names (base coding systems which have a
125 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
127 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
128 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
129 ;; Actually, there should be an `mm-coding-system-mime-charset'.
131 (defalias 'mm-read-coding-system
133 ((fboundp 'read-coding-system)
134 (if (and (featurep 'xemacs)
135 (<= (string-to-number emacs-version) 21.1))
136 (lambda (prompt &optional default-coding-system)
137 (read-coding-system prompt))
138 'read-coding-system))
139 (t (lambda (prompt &optional default-coding-system)
140 "Prompt the user for a coding system."
142 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
143 mm-mime-mule-charset-alist)))))))
145 (defvar mm-coding-system-list nil)
146 (defun mm-get-coding-system-list ()
147 "Get the coding system list."
148 (or mm-coding-system-list
149 (setq mm-coding-system-list (mm-coding-system-list))))
151 (defun mm-coding-system-p (cs)
152 "Return non-nil if CS is a symbol naming a coding system.
153 In XEmacs, also return non-nil if CS is a coding system object.
154 If CS is available, return CS itself in Emacs, and return a coding
155 system object in XEmacs."
156 (if (fboundp 'find-coding-system)
157 (and cs (find-coding-system cs))
158 (if (fboundp 'coding-system-p)
159 (when (coding-system-p cs)
161 ;; Is this branch ever actually useful?
162 (car (memq cs (mm-get-coding-system-list))))))
164 (defvar mm-charset-synonym-alist
166 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
167 ,@(unless (mm-coding-system-p 'x-ctext)
168 '((x-ctext . ctext)))
169 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
170 ,@(unless (mm-coding-system-p 'iso-8859-15)
171 '((iso-8859-15 . iso-8859-1)))
172 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
173 ,@(unless (mm-coding-system-p 'big5-hkscs)
174 '((big5-hkscs . big5)))
175 ;; Windows-1252 is actually a superset of Latin-1. See also
176 ;; `gnus-article-dumbquotes-map'.
177 ,@(unless (mm-coding-system-p 'windows-1252)
178 (if (mm-coding-system-p 'cp1252)
179 '((windows-1252 . cp1252))
180 '((windows-1252 . iso-8859-1))))
181 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
182 ;; Outlook users in Czech republic. Use this to allow reading of their
183 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
184 ,@(if (and (not (mm-coding-system-p 'windows-1250))
185 (mm-coding-system-p 'cp1250))
186 '((windows-1250 . cp1250)))
187 ;; A Microsoft misunderstanding.
188 ,@(if (and (not (mm-coding-system-p 'unicode))
189 (mm-coding-system-p 'utf-16-le))
190 '((unicode . utf-16-le)))
191 ;; A Microsoft misunderstanding.
192 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
193 (if (mm-coding-system-p 'cp949)
194 '((ks_c_5601-1987 . cp949))
195 '((ks_c_5601-1987 . euc-kr))))
197 "A mapping from invalid charset names to the real charset names.")
199 (defvar mm-binary-coding-system
201 ((mm-coding-system-p 'binary) 'binary)
202 ((mm-coding-system-p 'no-conversion) 'no-conversion)
204 "100% binary coding system.")
206 (defvar mm-text-coding-system
207 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
208 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
209 (and (mm-coding-system-p 'raw-text) 'raw-text))
210 mm-binary-coding-system)
211 "Text-safe coding system (For removing ^M).")
213 (defvar mm-text-coding-system-for-write nil
214 "Text coding system for write.")
216 (defvar mm-auto-save-coding-system
218 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
219 (if (memq system-type '(windows-nt ms-dos ms-windows))
220 (if (mm-coding-system-p 'utf-8-emacs-dos)
221 'utf-8-emacs-dos mm-binary-coding-system)
223 ((mm-coding-system-p 'emacs-mule)
224 (if (memq system-type '(windows-nt ms-dos ms-windows))
225 (if (mm-coding-system-p 'emacs-mule-dos)
226 'emacs-mule-dos mm-binary-coding-system)
228 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
229 (t mm-binary-coding-system))
230 "Coding system of auto save file.")
232 (defvar mm-universal-coding-system mm-auto-save-coding-system
233 "The universal coding system.")
235 ;; Fixme: some of the cars here aren't valid MIME charsets. That
236 ;; should only matter with XEmacs, though.
237 (defvar mm-mime-mule-charset-alist
239 (iso-8859-1 latin-iso8859-1)
240 (iso-8859-2 latin-iso8859-2)
241 (iso-8859-3 latin-iso8859-3)
242 (iso-8859-4 latin-iso8859-4)
243 (iso-8859-5 cyrillic-iso8859-5)
244 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
245 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
246 ;; charset is koi8-r, not iso-8859-5.
247 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
248 (iso-8859-6 arabic-iso8859-6)
249 (iso-8859-7 greek-iso8859-7)
250 (iso-8859-8 hebrew-iso8859-8)
251 (iso-8859-9 latin-iso8859-9)
252 (iso-8859-14 latin-iso8859-14)
253 (iso-8859-15 latin-iso8859-15)
254 (viscii vietnamese-viscii-lower)
255 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
256 (euc-kr korean-ksc5601)
257 (gb2312 chinese-gb2312)
258 (big5 chinese-big5-1 chinese-big5-2)
260 (thai-tis620 thai-tis620)
261 (windows-1251 cyrillic-iso8859-5)
262 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
263 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
264 latin-jisx0201 japanese-jisx0208-1978
265 chinese-gb2312 japanese-jisx0208
266 korean-ksc5601 japanese-jisx0212)
267 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
268 latin-jisx0201 japanese-jisx0208-1978
269 chinese-gb2312 japanese-jisx0208
270 korean-ksc5601 japanese-jisx0212
271 chinese-cns11643-1 chinese-cns11643-2)
272 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
273 cyrillic-iso8859-5 greek-iso8859-7
274 latin-jisx0201 japanese-jisx0208-1978
275 chinese-gb2312 japanese-jisx0208
276 korean-ksc5601 japanese-jisx0212
277 chinese-cns11643-1 chinese-cns11643-2
278 chinese-cns11643-3 chinese-cns11643-4
279 chinese-cns11643-5 chinese-cns11643-6
281 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
282 japanese-jisx0213-1 japanese-jisx0213-2)
283 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
284 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
285 (charsetp 'unicode-a)
286 (not (mm-coding-system-p 'mule-utf-8)))
287 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
288 ;; If we have utf-8 we're in Mule 5+.
291 (coding-system-get 'mule-utf-8 'safe-charsets)))))
292 "Alist of MIME-charset/MULE-charsets.")
294 (defun mm-enrich-utf-8-by-mule-ucs ()
295 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
296 This function will run when the `un-define' module is loaded under
297 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
298 with Mule charsets. It is completely useless for Emacs."
299 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
300 (assoc "un-define" after-load-alist)))
301 (setq after-load-alist
302 (delete '("un-define") after-load-alist)))
303 (when (boundp 'unicode-basic-translation-charset-order-list)
309 'unicode-basic-translation-charset-order-list))))
310 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
313 (setq mm-mime-mule-charset-alist
314 (nconc mm-mime-mule-charset-alist
315 (list (cons 'utf-8 val))))))
318 ;; Correct by construction, but should be unnecessary for Emacs:
319 (if (featurep 'xemacs)
320 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
321 (when (and (fboundp 'coding-system-list)
322 (fboundp 'sort-coding-systems))
323 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
327 mime (or (coding-system-get cs :mime-charset) ; Emacs 22
328 (coding-system-get cs 'mime-charset)))
330 (not (eq t (setq mule
331 (coding-system-get cs 'safe-charsets))))
332 (not (assq mime alist)))
333 (push (cons mime (delq 'ascii mule)) alist)))
334 (setq mm-mime-mule-charset-alist (nreverse alist)))))
336 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
337 "A list of special charsets.
338 Valid elements include:
339 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
340 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
343 (defvar mm-iso-8859-15-compatible
344 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
345 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
346 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
348 (defvar mm-iso-8859-x-to-15-table
349 (and (fboundp 'coding-system-p)
350 (mm-coding-system-p 'iso-8859-15)
353 (if (mm-coding-system-p (car cs))
354 (let ((c (string-to-char
355 (decode-coding-string "\341" (car cs)))))
356 (cons (char-charset c)
359 (decode-coding-string "\341" 'iso-8859-15)) c)
360 (string-to-list (decode-coding-string (car (cdr cs))
363 mm-iso-8859-15-compatible))
364 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
366 (defcustom mm-coding-system-priorities
367 (if (boundp 'current-language-environment)
368 (let ((lang (symbol-value 'current-language-environment)))
369 (cond ((string= lang "Japanese")
370 ;; Japanese users prefer iso-2022-jp to euc-japan or
371 ;; shift_jis, however iso-8859-1 should be used when
372 ;; there are only ASCII text and Latin-1 characters.
373 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
374 "Preferred coding systems for encoding outgoing messages.
376 More than one suitable coding system may be found for some text.
377 By default, the coding system with the highest priority is used
378 to encode outgoing messages (see `sort-coding-systems'). If this
379 variable is set, it overrides the default priority."
381 :type '(repeat (symbol :tag "Coding system"))
385 (defvar mm-use-find-coding-systems-region
386 (fboundp 'find-coding-systems-region)
387 "Use `find-coding-systems-region' to find proper coding systems.
389 Setting it to nil is useful on Emacsen supporting Unicode if sending
390 mail with multiple parts is preferred to sending a Unicode one.")
392 ;;; Internal variables:
396 (defun mm-mule-charset-to-mime-charset (charset)
397 "Return the MIME charset corresponding to the given Mule CHARSET."
398 (if (and (fboundp 'find-coding-systems-for-charsets)
399 (fboundp 'sort-coding-systems))
400 (let ((css (sort (sort-coding-systems
401 (find-coding-systems-for-charsets (list charset)))
402 'mm-sort-coding-systems-predicate))
404 (while (and (not mime)
406 (when (setq cs (pop css))
407 (setq mime (or (coding-system-get cs :mime-charset)
408 (coding-system-get cs 'mime-charset)))))
410 (let ((alist (mapcar (lambda (cs)
411 (assq cs mm-mime-mule-charset-alist))
412 (sort (mapcar 'car mm-mime-mule-charset-alist)
413 'mm-sort-coding-systems-predicate)))
416 (when (memq charset (cdar alist))
417 (setq out (caar alist)
422 (defun mm-charset-to-coding-system (charset &optional lbt)
423 "Return coding-system corresponding to CHARSET.
424 CHARSET is a symbol naming a MIME charset.
425 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
426 used as the line break code type of the coding system."
427 (when (stringp charset)
428 (setq charset (intern (downcase charset))))
430 (setq charset (intern (format "%s-%s" charset lbt))))
434 ;; Running in a non-MULE environment.
435 ((or (null (mm-get-coding-system-list))
436 (not (fboundp 'coding-system-get)))
439 ((eq charset 'us-ascii)
441 ;; Check to see whether we can handle this charset. (This depends
442 ;; on there being some coding system matching each `mime-charset'
443 ;; property defined, as there should be.)
444 ((and (mm-coding-system-p charset)
445 ;;; Doing this would potentially weed out incorrect charsets.
447 ;;; (eq charset (coding-system-get charset 'mime-charset))
450 ;; Translate invalid charsets.
451 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
452 (and cs (mm-coding-system-p cs) cs)))
453 ;; Last resort: search the coding system list for entries which
454 ;; have the right mime-charset in case the canonical name isn't
455 ;; defined (though it should be).
457 ;; mm-get-coding-system-list returns a list of cs without lbt.
459 (dolist (c (mm-get-coding-system-list))
461 (eq charset (or (coding-system-get c :mime-charset)
462 (coding-system-get c 'mime-charset))))
467 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
468 (boundp 'default-enable-multibyte-characters)
469 default-enable-multibyte-characters
470 (fboundp 'set-buffer-multibyte))
471 "True in Emacs with Mule.")
474 (defun mm-enable-multibyte ()
475 "Set the multibyte flag of the current buffer.
476 Only do this if the default value of `enable-multibyte-characters' is
477 non-nil. This is a no-op in XEmacs."
478 (set-buffer-multibyte 'to))
479 (defalias 'mm-enable-multibyte 'ignore))
482 (defun mm-disable-multibyte ()
483 "Unset the multibyte flag of in the current buffer.
484 This is a no-op in XEmacs."
485 (set-buffer-multibyte nil))
486 (defalias 'mm-disable-multibyte 'ignore)))
488 (defun mm-preferred-coding-system (charset)
489 ;; A typo in some Emacs versions.
490 (or (get-charset-property charset 'preferred-coding-system)
491 (get-charset-property charset 'prefered-coding-system)))
493 ;; Mule charsets shouldn't be used.
494 (defsubst mm-guess-charset ()
495 "Guess Mule charset from the language environment."
497 mail-parse-mule-charset ;; cached mule-charset
499 (setq mail-parse-mule-charset
500 (and (boundp 'current-language-environment)
503 (assoc current-language-environment
504 language-info-alist))))))
505 (if (or (not mail-parse-mule-charset)
506 (eq mail-parse-mule-charset 'ascii))
507 (setq mail-parse-mule-charset
508 (or (car (last (assq mail-parse-charset
509 mm-mime-mule-charset-alist)))
512 mail-parse-mule-charset)))
514 (defun mm-charset-after (&optional pos)
515 "Return charset of a character in current buffer at position POS.
516 If POS is nil, it defauls to the current point.
517 If POS is out of range, the value is nil.
518 If the charset is `composition', return the actual one."
519 (let ((char (char-after pos)) charset)
520 (if (< (mm-char-int char) 128)
521 (setq charset 'ascii)
522 ;; charset-after is fake in some Emacsen.
523 (setq charset (and (fboundp 'char-charset) (char-charset char)))
524 (if (eq charset 'composition) ; Mule 4
525 (let ((p (or pos (point))))
526 (cadr (find-charset-region p (1+ p))))
527 (if (and charset (not (memq charset '(ascii eight-bit-control
528 eight-bit-graphic))))
530 (mm-guess-charset))))))
532 (defun mm-mime-charset (charset)
533 "Return the MIME charset corresponding to the given Mule CHARSET."
534 (if (eq charset 'unknown)
535 (error "The message contains non-printable characters, please use attachment"))
536 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
537 ;; This exists in Emacs 20.
539 (and (mm-preferred-coding-system charset)
540 (or (coding-system-get
541 (mm-preferred-coding-system charset) :mime-charset)
543 (mm-preferred-coding-system charset) 'mime-charset)))
544 (and (eq charset 'ascii)
546 (mm-preferred-coding-system charset)
547 (mm-mule-charset-to-mime-charset charset))
548 ;; This is for XEmacs.
549 (mm-mule-charset-to-mime-charset charset)))
551 (defun mm-delete-duplicates (list)
552 "Simple substitute for CL `delete-duplicates', testing with `equal'."
555 (setq head (car list))
556 (setq list (delete head list))
557 (setq result (cons head result)))
560 ;; Fixme: This is used in places when it should be testing the
561 ;; default multibyteness. See mm-default-multibyte-p.
563 (if (and (not (featurep 'xemacs))
564 (boundp 'enable-multibyte-characters))
565 (defun mm-multibyte-p ()
566 "Non-nil if multibyte is enabled in the current buffer."
567 enable-multibyte-characters)
568 (defun mm-multibyte-p () (featurep 'mule))))
570 (defun mm-default-multibyte-p ()
571 "Return non-nil if the session is multibyte.
572 This affects whether coding conversion should be attempted generally."
574 (if (boundp 'default-enable-multibyte-characters)
575 default-enable-multibyte-characters
578 (defun mm-iso-8859-x-to-15-region (&optional b e)
579 (if (fboundp 'char-charset)
580 (let (charset item c inconvertible)
582 (if e (narrow-to-region b e))
583 (goto-char (point-min))
584 (skip-chars-forward "\0-\177")
587 ((not (setq item (assq (char-charset (setq c (char-after)))
588 mm-iso-8859-x-to-15-table)))
590 ((memq c (cdr (cdr item)))
591 (setq inconvertible t)
594 (insert-before-markers (prog1 (+ c (car (cdr item)))
596 (skip-chars-forward "\0-\177")))
597 (not inconvertible))))
599 (defun mm-sort-coding-systems-predicate (a b)
602 ;; Note: invalid entries are dropped silently
603 (and (setq cs (mm-coding-system-p cs))
604 (coding-system-base cs)))
605 mm-coding-system-priorities)))
606 (and (setq a (mm-coding-system-p a))
607 (if (setq b (mm-coding-system-p b))
608 (> (length (memq (coding-system-base a) priorities))
609 (length (memq (coding-system-base b) priorities)))
613 (autoload 'latin-unity-massage-name "latin-unity")
614 (autoload 'latin-unity-maybe-remap "latin-unity")
615 (autoload 'latin-unity-representations-feasible-region "latin-unity")
616 (autoload 'latin-unity-representations-present-region "latin-unity")
617 (defvar latin-unity-coding-systems)
618 (defvar latin-unity-ucs-list))
620 (defun mm-xemacs-find-mime-charset-1 (begin end)
621 "Determine which MIME charset to use to send region as message.
622 This uses the XEmacs-specific latin-unity package to better handle the
623 case where identical characters from diverse ISO-8859-? character sets
624 can be encoded using a single one of the corresponding coding systems.
626 It treats `mm-coding-system-priorities' as the list of preferred
627 coding systems; a useful example setting for this list in Western
628 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
629 to the very standard Latin 1 coding system, and only move to coding
630 systems that are less supported as is necessary to encode the
631 characters that exist in the buffer.
633 Latin Unity doesn't know about those non-ASCII Roman characters that
634 are available in various East Asian character sets. As such, its
635 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
636 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
637 But this is very much a corner case, so don't worry about it."
638 (let ((systems mm-coding-system-priorities) csets psets curset)
640 ;; Load the Latin Unity library, if available.
641 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
642 (require 'latin-unity))
644 ;; Now, can we use it?
645 (if (featurep 'latin-unity)
647 (setq csets (latin-unity-representations-feasible-region begin end)
648 psets (latin-unity-representations-present-region begin end))
652 ;; Pass back the first coding system in the preferred list
653 ;; that can encode the whole region.
654 (dolist (curset systems)
655 (setq curset (latin-unity-massage-name 'buffer-default curset))
657 ;; If the coding system is a universal coding system, then
658 ;; it can certainly encode all the characters in the region.
659 (if (memq curset latin-unity-ucs-list)
660 (throw 'done (list curset)))
662 ;; If a coding system isn't universal, and isn't in
663 ;; the list that latin unity knows about, we can't
664 ;; decide whether to use it here. Leave that until later
665 ;; in `mm-find-mime-charset-region' function, whence we
667 (unless (memq curset latin-unity-coding-systems)
670 ;; Right, we know about this coding system, and it may
671 ;; conceivably be able to encode all the characters in
673 (if (latin-unity-maybe-remap begin end curset csets psets t)
674 (throw 'done (list curset))))
676 ;; Can't encode using anything from the
677 ;; `mm-coding-system-priorities' list.
678 ;; Leave `mm-find-mime-charset' to do most of the work.
681 ;; Right, latin unity isn't available; let `mm-find-charset-region'
682 ;; take its default action, which equally applies to GNU Emacs.
685 (defmacro mm-xemacs-find-mime-charset (begin end)
686 (when (featurep 'xemacs)
687 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
689 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
690 "Return the MIME charsets needed to encode the region between B and E.
691 nil means ASCII, a single-element list represents an appropriate MIME
692 charset, and a longer list means no appropriate charset."
694 ;; The return possibilities of this function are a mess...
695 (or (and (mm-multibyte-p)
696 mm-use-find-coding-systems-region
697 ;; Find the mime-charset of the most preferred coding
698 ;; system that has one.
699 (let ((systems (find-coding-systems-region b e)))
700 (when mm-coding-system-priorities
702 (sort systems 'mm-sort-coding-systems-predicate)))
703 (setq systems (delq 'compound-text systems))
704 (unless (equal systems '(undecided))
706 (let* ((head (pop systems))
707 (cs (or (coding-system-get head :mime-charset)
708 (coding-system-get head 'mime-charset))))
709 ;; The mime-charset (`x-ctext') of
710 ;; `compound-text' is not in the IANA list. We
711 ;; shouldn't normally use anything here with a
712 ;; mime-charset having an `x-' prefix.
713 ;; Fixme: Allow this to be overridden, since
714 ;; there is existing use of x-ctext.
715 ;; Also people apparently need the coding system
716 ;; `iso-2022-jp-3' (which Mule-UCS defines with
717 ;; mime-charset, though it's not valid).
719 (not (string-match "^[Xx]-" (symbol-name cs)))
720 ;; UTF-16 of any variety is invalid for
721 ;; text parts and, unfortunately, has
722 ;; mime-charset defined both in Mule-UCS
723 ;; and versions of Emacs. (The name
724 ;; might be `mule-utf-16...' or
726 (not (string-match "utf-16" (symbol-name cs))))
728 charsets (list cs))))))
730 ;; If we're XEmacs, and some coding system is appropriate,
731 ;; mm-xemacs-find-mime-charset will return an appropriate list.
732 ;; Otherwise, we'll get nil, and the next setq will get invoked.
733 (setq charsets (mm-xemacs-find-mime-charset b e))
735 ;; We're not multibyte, or a single coding system won't cover it.
737 (mm-delete-duplicates
738 (mapcar 'mm-mime-charset
740 (mm-find-charset-region b e))))))
741 (if (and (> (length charsets) 1)
742 (memq 'iso-8859-15 charsets)
743 (memq 'iso-8859-15 hack-charsets)
744 (save-excursion (mm-iso-8859-x-to-15-region b e)))
745 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
746 mm-iso-8859-15-compatible))
747 (if (and (memq 'iso-2022-jp-2 charsets)
748 (memq 'iso-2022-jp-2 hack-charsets))
749 (setq charsets (delq 'iso-2022-jp charsets)))
752 (defmacro mm-with-unibyte-buffer (&rest forms)
753 "Create a temporary buffer, and evaluate FORMS there like `progn'.
754 Use unibyte mode for this."
755 `(let (default-enable-multibyte-characters)
756 (with-temp-buffer ,@forms)))
757 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
758 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
760 (defmacro mm-with-multibyte-buffer (&rest forms)
761 "Create a temporary buffer, and evaluate FORMS there like `progn'.
762 Use multibyte mode for this."
763 `(let ((default-enable-multibyte-characters t))
764 (with-temp-buffer ,@forms)))
765 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
766 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
768 (defmacro mm-with-unibyte-current-buffer (&rest forms)
769 "Evaluate FORMS with current buffer temporarily made unibyte.
770 Also bind `default-enable-multibyte-characters' to nil.
771 Equivalent to `progn' in XEmacs"
772 (let ((multibyte (make-symbol "multibyte"))
773 (buffer (make-symbol "buffer")))
775 (let ((,multibyte enable-multibyte-characters)
776 (,buffer (current-buffer)))
778 (let (default-enable-multibyte-characters)
779 (set-buffer-multibyte nil)
782 (set-buffer-multibyte ,multibyte)))
783 (let (default-enable-multibyte-characters)
785 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
786 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
788 (defmacro mm-with-unibyte (&rest forms)
789 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
790 `(let (default-enable-multibyte-characters)
792 (put 'mm-with-unibyte 'lisp-indent-function 0)
793 (put 'mm-with-unibyte 'edebug-form-spec '(body))
795 (defmacro mm-with-multibyte (&rest forms)
796 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
797 `(let ((default-enable-multibyte-characters t))
799 (put 'mm-with-multibyte 'lisp-indent-function 0)
800 (put 'mm-with-multibyte 'edebug-form-spec '(body))
802 (defun mm-find-charset-region (b e)
803 "Return a list of Emacs charsets in the region B to E."
805 ((and (mm-multibyte-p)
806 (fboundp 'find-charset-region))
807 ;; Remove composition since the base charsets have been included.
808 ;; Remove eight-bit-*, treat them as ascii.
809 (let ((css (find-charset-region b e)))
810 (mapcar (lambda (cs) (setq css (delq cs css)))
811 '(composition eight-bit-control eight-bit-graphic
815 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
818 (narrow-to-region b e)
819 (goto-char (point-min))
820 (skip-chars-forward "\0-\177")
825 (and (boundp 'current-language-environment)
826 (car (last (assq 'charset
827 (assoc current-language-environment
828 language-info-alist))))))
829 (if (eq charset 'ascii) (setq charset nil))
832 (car (last (assq mail-parse-charset
833 mm-mime-mule-charset-alist)))))
834 (list 'ascii (or charset 'latin-iso8859-1)))))))))
836 (defun mm-auto-mode-alist ()
837 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
838 (let ((alist auto-mode-alist)
841 (when (listp (cdar alist))
842 (push (car alist) out))
846 (defvar mm-inhibit-file-name-handlers
847 '(jka-compr-handler image-file-handler)
848 "A list of handlers doing (un)compression (etc) thingies.")
850 (defun mm-insert-file-contents (filename &optional visit beg end replace
852 "Like `insert-file-contents', but only reads in the file.
853 A buffer may be modified in several ways after reading into the buffer due
854 to advanced Emacs features, such as file-name-handlers, format decoding,
855 `find-file-hooks', etc.
856 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
857 This function ensures that none of these modifications will take place."
858 (let* ((format-alist nil)
859 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
860 (default-major-mode 'fundamental-mode)
861 (enable-local-variables nil)
862 (after-insert-file-functions nil)
863 (enable-local-eval nil)
864 (inhibit-file-name-operation (if inhibit
865 'insert-file-contents
866 inhibit-file-name-operation))
867 (inhibit-file-name-handlers
869 (append mm-inhibit-file-name-handlers
870 inhibit-file-name-handlers)
871 inhibit-file-name-handlers))
872 (ffh (if (boundp 'find-file-hook)
875 (val (symbol-value ffh)))
878 (insert-file-contents filename visit beg end replace)
881 (defun mm-append-to-file (start end filename &optional codesys inhibit)
882 "Append the contents of the region to the end of file FILENAME.
883 When called from a function, expects three arguments,
884 START, END and FILENAME. START and END are buffer positions
885 saying what text to write.
886 Optional fourth argument specifies the coding system to use when
888 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
889 (let ((coding-system-for-write
890 (or codesys mm-text-coding-system-for-write
891 mm-text-coding-system))
892 (inhibit-file-name-operation (if inhibit
894 inhibit-file-name-operation))
895 (inhibit-file-name-handlers
897 (append mm-inhibit-file-name-handlers
898 inhibit-file-name-handlers)
899 inhibit-file-name-handlers)))
900 (write-region start end filename t 'no-message)
901 (message "Appended to %s" filename)))
903 (defun mm-write-region (start end filename &optional append visit lockname
904 coding-system inhibit)
906 "Like `write-region'.
907 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
908 (let ((coding-system-for-write
909 (or coding-system mm-text-coding-system-for-write
910 mm-text-coding-system))
911 (inhibit-file-name-operation (if inhibit
913 inhibit-file-name-operation))
914 (inhibit-file-name-handlers
916 (append mm-inhibit-file-name-handlers
917 inhibit-file-name-handlers)
918 inhibit-file-name-handlers)))
919 (write-region start end filename append visit lockname)))
921 (defun mm-image-load-path (&optional package)
923 (dolist (path load-path (nreverse result))
926 (setq dir (concat (file-name-directory
927 (directory-file-name path))
928 "etc/images/" (or package "gnus/")))))
930 (push path result))))
932 ;; Fixme: This doesn't look useful where it's used.
933 (if (fboundp 'detect-coding-region)
934 (defun mm-detect-coding-region (start end)
935 "Like `detect-coding-region' except returning the best one."
936 (let ((coding-systems
937 (detect-coding-region start end)))
938 (or (car-safe coding-systems)
940 (defun mm-detect-coding-region (start end)
941 (let ((point (point)))
943 (skip-chars-forward "\0-\177" end)
945 (if (eq (point) end) 'ascii (mm-guess-charset))
946 (goto-char point)))))
948 (if (fboundp 'coding-system-get)
949 (defun mm-detect-mime-charset-region (start end)
950 "Detect MIME charset of the text in the region between START and END."
951 (let ((cs (mm-detect-coding-region start end)))
952 (or (coding-system-get cs :mime-charset)
953 (coding-system-get cs 'mime-charset))))
954 (defun mm-detect-mime-charset-region (start end)
955 "Detect MIME charset of the text in the region between START and END."
956 (let ((cs (mm-detect-coding-region start end)))
960 (unless (fboundp 'coding-system-to-mime-charset)
961 (defalias 'coding-system-to-mime-charset 'ignore)))
963 (defun mm-coding-system-to-mime-charset (coding-system)
964 "Return the MIME charset corresponding to CODING-SYSTEM.
965 To make this function work with XEmacs, the APEL package is required."
967 (or (and (fboundp 'coding-system-get)
968 (or (coding-system-get coding-system :mime-charset)
969 (coding-system-get coding-system 'mime-charset)))
970 (and (featurep 'xemacs)
971 (or (and (fboundp 'coding-system-to-mime-charset)
972 (not (eq (symbol-function 'coding-system-to-mime-charset)
974 (and (condition-case nil
977 (fboundp 'coding-system-to-mime-charset)))
978 (coding-system-to-mime-charset coding-system)))))
981 (require 'jka-compr))
983 (defun mm-decompress-buffer (filename &optional inplace force)
984 "Decompress buffer's contents, depending on jka-compr.
985 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
986 agrees with `jka-compr-compression-info-list', decompression is done.
987 Signal an error if FORCE is neither nil nor t and compressed data are
988 not decompressed because `auto-compression-mode' is disabled.
989 If INPLACE is nil, return decompressed data or nil without modifying
990 the buffer. Otherwise, replace the buffer's contents with the
991 decompressed data. The buffer's multibyteness must be turned off."
994 (prog1 t (require 'jka-compr))
995 (and (fboundp 'jka-compr-installed-p)
996 (jka-compr-installed-p))))
997 (let ((info (jka-compr-get-compression-info filename)))
999 (unless (or (memq force (list nil t))
1000 (jka-compr-installed-p))
1002 (let ((prog (jka-compr-info-uncompress-program info))
1003 (args (jka-compr-info-uncompress-args info))
1004 (msg (format "%s %s..."
1005 (jka-compr-info-uncompress-message info)
1007 (err-file (jka-compr-make-temp-name))
1008 (cur (current-buffer))
1009 (coding-system-for-read mm-binary-coding-system)
1010 (coding-system-for-write mm-binary-coding-system)
1014 (insert-buffer-substring cur)
1017 (unless (memq (apply 'call-process-region
1018 (point-min) (point-max)
1019 prog t (list t err-file) nil args)
1020 jka-compr-acceptable-retval-list)
1024 (delete "" (split-string
1026 (insert-file-contents err-file)
1032 (format "Error while executing \"%s %s < %s\""
1033 prog (mapconcat 'identity args " ")
1035 (setq retval (buffer-string)))
1037 (setq err-msg (error-message-string err)))))
1038 (when (file-exists-p err-file)
1039 (ignore-errors (jka-compr-delete-temp-file err-file)))
1042 (delete-region (point-min) (point-max))
1045 (message "%s" (or err-msg (concat msg "done")))
1049 (unless (fboundp 'coding-system-name)
1050 (defalias 'coding-system-name 'ignore))
1051 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1052 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1053 (unless (fboundp 'find-operation-coding-system)
1054 (defalias 'find-operation-coding-system 'ignore)))
1056 (defun mm-find-buffer-file-coding-system (&optional filename)
1057 "Find coding system used to decode the contents of the current buffer.
1058 This function looks for the coding system magic cookie or examines the
1059 coding system specified by `file-coding-system-alist' being associated
1060 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1061 gzip, bzip2, etc. are allowed."
1063 (setq filename buffer-file-name))
1065 (let ((decomp (unless ;; No worth to examine charset of tar files.
1068 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1070 (mm-decompress-buffer filename nil t))))
1072 (set-buffer (let (default-enable-multibyte-characters)
1073 (generate-new-buffer " *temp*")))
1075 (setq filename (file-name-sans-extension filename)))
1076 (goto-char (point-min))
1079 ((boundp 'set-auto-coding-function) ;; Emacs
1081 (or (funcall (symbol-value 'set-auto-coding-function)
1082 filename (- (point-max) (point-min)))
1083 (car (find-operation-coding-system 'insert-file-contents
1085 (let (auto-coding-alist)
1087 (funcall (symbol-value 'set-auto-coding-function)
1088 nil (- (point-max) (point-min)))
1090 ((featurep 'file-coding) ;; XEmacs
1091 (let ((case-fold-search t)
1092 (end (point-at-eol))
1095 (and (re-search-forward "-\\*-+[\t ]*" end t)
1097 (setq start (match-end 0))
1098 (re-search-forward "[\t ]*-+\\*-" end t))
1100 (setq end (match-beginning 0))
1102 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1104 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1106 (find-coding-system (setq codesys
1107 (intern (match-string 1))))
1109 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1112 (setq start (match-end 0))
1113 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1115 (setq end (match-beginning 0))
1118 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1120 (find-coding-system (setq codesys
1121 (intern (match-string 1))))
1124 (goto-char (point-min))
1125 (setq case-fold-search nil)
1126 (re-search-forward "^;;;coding system: "
1127 ;;(+ (point-min) 3000) t))
1129 (looking-at "[^\t\n\r ]+")
1131 (setq codesys (intern (match-string 0))))
1135 (find-file-coding-system-for-read-from-filename
1137 (coding-system-name (coding-system-base codesys)))))))
1139 (kill-buffer (current-buffer)))))))
1143 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1144 ;;; mm-util.el ends here