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.
29 (eval-when-compile (require 'cl))
35 (let ((nfunc (intern (format "mm-%s" (car elem)))))
36 (if (fboundp (car elem))
37 (defalias nfunc (car elem))
38 (defalias nfunc (cdr elem)))))
39 '((decode-coding-string . (lambda (s a) s))
40 (encode-coding-string . (lambda (s a) s))
41 (encode-coding-region . ignore)
42 (coding-system-list . ignore)
43 (decode-coding-region . ignore)
45 (coding-system-equal . equal)
46 (annotationp . ignore)
47 (set-buffer-file-coding-system . ignore)
54 (mapcar (lambda (e) (list (symbol-name (car e))))
55 mm-mime-mule-charset-alist)
58 . (lambda (from to string &optional inplace)
59 ;; stolen (and renamed) from nnheader.el
60 "Replace characters in STRING from FROM to TO.
61 Unless optional argument INPLACE is non-nil, return a new string."
62 (let ((string (if inplace string (copy-sequence string)))
65 ;; Replace all occurrences of FROM with TO.
67 (when (= (aref string idx) from)
72 . (lambda (string regexp rep &optional literal)
73 "See `replace-regexp-in-string', only the order of args differs."
74 (replace-regexp-in-string regexp rep string nil literal)))
75 (string-as-unibyte . identity)
76 (string-make-unibyte . identity)
77 ;; string-as-multibyte often doesn't really do what you think it does.
79 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
80 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
81 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
82 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
84 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
85 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
86 ;; Better use string-to-multibyte or encode-coding-string.
87 ;; If you really need string-as-multibyte somewhere it's usually
88 ;; because you're using the internal emacs-mule representation (maybe
89 ;; because you're using string-as-unibyte somewhere), which is
90 ;; generally a problem in itself.
91 ;; Here is an approximate equivalence table to help think about it:
92 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
93 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
94 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
95 (string-as-multibyte . identity)
98 "Return a multibyte string with the same individual chars as string."
100 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
102 (multibyte-string-p . ignore)
103 ;; It is not a MIME function, but some MIME functions use it.
104 (make-temp-file . (lambda (prefix &optional dir-flag)
105 (let ((file (expand-file-name
106 (make-temp-name prefix)
107 (if (fboundp 'temp-directory)
109 temporary-file-directory))))
111 (make-directory file))
113 (insert-byte . insert-char)
114 (multibyte-char-to-unibyte . identity))))
117 (defalias 'mm-char-or-char-int-p
119 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
120 ((fboundp 'char-valid-p) 'char-valid-p)
123 ;; Fixme: This seems always to be used to read a MIME charset, so it
124 ;; should be re-named and fixed (in Emacs) to offer completion only on
125 ;; proper charset names (base coding systems which have a
126 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
128 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
129 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
130 ;; Actually, there should be an `mm-coding-system-mime-charset'.
132 (defalias 'mm-read-coding-system
134 ((fboundp 'read-coding-system)
135 (if (and (featurep 'xemacs)
136 (<= (string-to-number emacs-version) 21.1))
137 (lambda (prompt &optional default-coding-system)
138 (read-coding-system prompt))
139 'read-coding-system))
140 (t (lambda (prompt &optional default-coding-system)
141 "Prompt the user for a coding system."
143 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
144 mm-mime-mule-charset-alist)))))))
146 (defvar mm-coding-system-list nil)
147 (defun mm-get-coding-system-list ()
148 "Get the coding system list."
149 (or mm-coding-system-list
150 (setq mm-coding-system-list (mm-coding-system-list))))
152 (defun mm-coding-system-p (cs)
153 "Return non-nil if CS is a symbol naming a coding system.
154 In XEmacs, also return non-nil if CS is a coding system object.
155 If CS is available, return CS itself in Emacs, and return a coding
156 system object in XEmacs."
157 (if (fboundp 'find-coding-system)
158 (and cs (find-coding-system cs))
159 (if (fboundp 'coding-system-p)
160 (when (coding-system-p cs)
162 ;; Is this branch ever actually useful?
163 (car (memq cs (mm-get-coding-system-list))))))
165 (defvar mm-charset-synonym-alist
167 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
168 ,@(unless (mm-coding-system-p 'x-ctext)
169 '((x-ctext . ctext)))
170 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
171 ,@(unless (mm-coding-system-p 'iso-8859-15)
172 '((iso-8859-15 . iso-8859-1)))
173 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
174 ,@(unless (mm-coding-system-p 'big5-hkscs)
175 '((big5-hkscs . big5)))
176 ;; Windows-1252 is actually a superset of Latin-1. See also
177 ;; `gnus-article-dumbquotes-map'.
178 ,@(unless (mm-coding-system-p 'windows-1252)
179 (if (mm-coding-system-p 'cp1252)
180 '((windows-1252 . cp1252))
181 '((windows-1252 . iso-8859-1))))
182 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
183 ;; Outlook users in Czech republic. Use this to allow reading of their
184 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
185 ,@(if (and (not (mm-coding-system-p 'windows-1250))
186 (mm-coding-system-p 'cp1250))
187 '((windows-1250 . cp1250)))
188 ;; A Microsoft misunderstanding.
189 ,@(if (and (not (mm-coding-system-p 'unicode))
190 (mm-coding-system-p 'utf-16-le))
191 '((unicode . utf-16-le)))
192 ;; A Microsoft misunderstanding.
193 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
194 (if (mm-coding-system-p 'cp949)
195 '((ks_c_5601-1987 . cp949))
196 '((ks_c_5601-1987 . euc-kr))))
198 "A mapping from invalid charset names to the real charset names.")
200 (defvar mm-binary-coding-system
202 ((mm-coding-system-p 'binary) 'binary)
203 ((mm-coding-system-p 'no-conversion) 'no-conversion)
205 "100% binary coding system.")
207 (defvar mm-text-coding-system
208 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
209 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
210 (and (mm-coding-system-p 'raw-text) 'raw-text))
211 mm-binary-coding-system)
212 "Text-safe coding system (For removing ^M).")
214 (defvar mm-text-coding-system-for-write nil
215 "Text coding system for write.")
217 (defvar mm-auto-save-coding-system
219 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
220 (if (memq system-type '(windows-nt ms-dos ms-windows))
221 (if (mm-coding-system-p 'utf-8-emacs-dos)
222 'utf-8-emacs-dos mm-binary-coding-system)
224 ((mm-coding-system-p 'emacs-mule)
225 (if (memq system-type '(windows-nt ms-dos ms-windows))
226 (if (mm-coding-system-p 'emacs-mule-dos)
227 'emacs-mule-dos mm-binary-coding-system)
229 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
230 (t mm-binary-coding-system))
231 "Coding system of auto save file.")
233 (defvar mm-universal-coding-system mm-auto-save-coding-system
234 "The universal coding system.")
236 ;; Fixme: some of the cars here aren't valid MIME charsets. That
237 ;; should only matter with XEmacs, though.
238 (defvar mm-mime-mule-charset-alist
240 (iso-8859-1 latin-iso8859-1)
241 (iso-8859-2 latin-iso8859-2)
242 (iso-8859-3 latin-iso8859-3)
243 (iso-8859-4 latin-iso8859-4)
244 (iso-8859-5 cyrillic-iso8859-5)
245 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
246 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
247 ;; charset is koi8-r, not iso-8859-5.
248 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
249 (iso-8859-6 arabic-iso8859-6)
250 (iso-8859-7 greek-iso8859-7)
251 (iso-8859-8 hebrew-iso8859-8)
252 (iso-8859-9 latin-iso8859-9)
253 (iso-8859-14 latin-iso8859-14)
254 (iso-8859-15 latin-iso8859-15)
255 (viscii vietnamese-viscii-lower)
256 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
257 (euc-kr korean-ksc5601)
258 (gb2312 chinese-gb2312)
259 (big5 chinese-big5-1 chinese-big5-2)
261 (thai-tis620 thai-tis620)
262 (windows-1251 cyrillic-iso8859-5)
263 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
264 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
265 latin-jisx0201 japanese-jisx0208-1978
266 chinese-gb2312 japanese-jisx0208
267 korean-ksc5601 japanese-jisx0212)
268 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
269 latin-jisx0201 japanese-jisx0208-1978
270 chinese-gb2312 japanese-jisx0208
271 korean-ksc5601 japanese-jisx0212
272 chinese-cns11643-1 chinese-cns11643-2)
273 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
274 cyrillic-iso8859-5 greek-iso8859-7
275 latin-jisx0201 japanese-jisx0208-1978
276 chinese-gb2312 japanese-jisx0208
277 korean-ksc5601 japanese-jisx0212
278 chinese-cns11643-1 chinese-cns11643-2
279 chinese-cns11643-3 chinese-cns11643-4
280 chinese-cns11643-5 chinese-cns11643-6
282 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
283 japanese-jisx0213-1 japanese-jisx0213-2)
284 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
285 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
286 (charsetp 'unicode-a)
287 (not (mm-coding-system-p 'mule-utf-8)))
288 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
289 ;; If we have utf-8 we're in Mule 5+.
292 (coding-system-get 'mule-utf-8 'safe-charsets)))))
293 "Alist of MIME-charset/MULE-charsets.")
295 (defun mm-enrich-utf-8-by-mule-ucs ()
296 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
297 This function will run when the `un-define' module is loaded under
298 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
299 with Mule charsets. It is completely useless for Emacs."
300 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
301 (assoc "un-define" after-load-alist)))
302 (setq after-load-alist
303 (delete '("un-define") after-load-alist)))
304 (when (boundp 'unicode-basic-translation-charset-order-list)
310 'unicode-basic-translation-charset-order-list))))
311 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
314 (setq mm-mime-mule-charset-alist
315 (nconc mm-mime-mule-charset-alist
316 (list (cons 'utf-8 val))))))
319 ;; Correct by construction, but should be unnecessary for Emacs:
320 (if (featurep 'xemacs)
321 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
322 (when (and (fboundp 'coding-system-list)
323 (fboundp 'sort-coding-systems))
324 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
328 mime (or (coding-system-get cs :mime-charset) ; Emacs 22
329 (coding-system-get cs 'mime-charset)))
331 (not (eq t (setq mule
332 (coding-system-get cs 'safe-charsets))))
333 (not (assq mime alist)))
334 (push (cons mime (delq 'ascii mule)) alist)))
335 (setq mm-mime-mule-charset-alist (nreverse alist)))))
337 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
338 "A list of special charsets.
339 Valid elements include:
340 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
341 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
344 (defvar mm-iso-8859-15-compatible
345 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
346 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
347 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
349 (defvar mm-iso-8859-x-to-15-table
350 (and (fboundp 'coding-system-p)
351 (mm-coding-system-p 'iso-8859-15)
354 (if (mm-coding-system-p (car cs))
355 (let ((c (string-to-char
356 (decode-coding-string "\341" (car cs)))))
357 (cons (char-charset c)
360 (decode-coding-string "\341" 'iso-8859-15)) c)
361 (string-to-list (decode-coding-string (car (cdr cs))
364 mm-iso-8859-15-compatible))
365 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
367 (defcustom mm-coding-system-priorities
368 (if (boundp 'current-language-environment)
369 (let ((lang (symbol-value 'current-language-environment)))
370 (cond ((string= lang "Japanese")
371 ;; Japanese users prefer iso-2022-jp to euc-japan or
372 ;; shift_jis, however iso-8859-1 should be used when
373 ;; there are only ASCII text and Latin-1 characters.
374 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
375 "Preferred coding systems for encoding outgoing messages.
377 More than one suitable coding system may be found for some text.
378 By default, the coding system with the highest priority is used
379 to encode outgoing messages (see `sort-coding-systems'). If this
380 variable is set, it overrides the default priority."
382 :type '(repeat (symbol :tag "Coding system"))
386 (defvar mm-use-find-coding-systems-region
387 (fboundp 'find-coding-systems-region)
388 "Use `find-coding-systems-region' to find proper coding systems.
390 Setting it to nil is useful on Emacsen supporting Unicode if sending
391 mail with multiple parts is preferred to sending a Unicode one.")
393 ;;; Internal variables:
397 (defun mm-mule-charset-to-mime-charset (charset)
398 "Return the MIME charset corresponding to the given Mule CHARSET."
399 (if (and (fboundp 'find-coding-systems-for-charsets)
400 (fboundp 'sort-coding-systems))
401 (let ((css (sort (sort-coding-systems
402 (find-coding-systems-for-charsets (list charset)))
403 'mm-sort-coding-systems-predicate))
405 (while (and (not mime)
407 (when (setq cs (pop css))
408 (setq mime (or (coding-system-get cs :mime-charset)
409 (coding-system-get cs 'mime-charset)))))
411 (let ((alist (mapcar (lambda (cs)
412 (assq cs mm-mime-mule-charset-alist))
413 (sort (mapcar 'car mm-mime-mule-charset-alist)
414 'mm-sort-coding-systems-predicate)))
417 (when (memq charset (cdar alist))
418 (setq out (caar alist)
423 (defun mm-charset-to-coding-system (charset &optional lbt)
424 "Return coding-system corresponding to CHARSET.
425 CHARSET is a symbol naming a MIME charset.
426 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
427 used as the line break code type of the coding system."
428 (when (stringp charset)
429 (setq charset (intern (downcase charset))))
431 (setq charset (intern (format "%s-%s" charset lbt))))
435 ;; Running in a non-MULE environment.
436 ((or (null (mm-get-coding-system-list))
437 (not (fboundp 'coding-system-get)))
440 ((eq charset 'us-ascii)
442 ;; Check to see whether we can handle this charset. (This depends
443 ;; on there being some coding system matching each `mime-charset'
444 ;; property defined, as there should be.)
445 ((and (mm-coding-system-p charset)
446 ;;; Doing this would potentially weed out incorrect charsets.
448 ;;; (eq charset (coding-system-get charset 'mime-charset))
451 ;; Translate invalid charsets.
452 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
453 (and cs (mm-coding-system-p cs) cs)))
454 ;; Last resort: search the coding system list for entries which
455 ;; have the right mime-charset in case the canonical name isn't
456 ;; defined (though it should be).
458 ;; mm-get-coding-system-list returns a list of cs without lbt.
460 (dolist (c (mm-get-coding-system-list))
462 (eq charset (or (coding-system-get c :mime-charset)
463 (coding-system-get c 'mime-charset))))
468 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
469 (boundp 'default-enable-multibyte-characters)
470 default-enable-multibyte-characters
471 (fboundp 'set-buffer-multibyte))
472 "True in Emacs with Mule.")
475 (defun mm-enable-multibyte ()
476 "Set the multibyte flag of the current buffer.
477 Only do this if the default value of `enable-multibyte-characters' is
478 non-nil. This is a no-op in XEmacs."
479 (set-buffer-multibyte 'to))
480 (defalias 'mm-enable-multibyte 'ignore))
483 (defun mm-disable-multibyte ()
484 "Unset the multibyte flag of in the current buffer.
485 This is a no-op in XEmacs."
486 (set-buffer-multibyte nil))
487 (defalias 'mm-disable-multibyte 'ignore)))
489 (defun mm-preferred-coding-system (charset)
490 ;; A typo in some Emacs versions.
491 (or (get-charset-property charset 'preferred-coding-system)
492 (get-charset-property charset 'prefered-coding-system)))
494 ;; Mule charsets shouldn't be used.
495 (defsubst mm-guess-charset ()
496 "Guess Mule charset from the language environment."
498 mail-parse-mule-charset ;; cached mule-charset
500 (setq mail-parse-mule-charset
501 (and (boundp 'current-language-environment)
504 (assoc current-language-environment
505 language-info-alist))))))
506 (if (or (not mail-parse-mule-charset)
507 (eq mail-parse-mule-charset 'ascii))
508 (setq mail-parse-mule-charset
509 (or (car (last (assq mail-parse-charset
510 mm-mime-mule-charset-alist)))
513 mail-parse-mule-charset)))
515 (defun mm-charset-after (&optional pos)
516 "Return charset of a character in current buffer at position POS.
517 If POS is nil, it defauls to the current point.
518 If POS is out of range, the value is nil.
519 If the charset is `composition', return the actual one."
520 (let ((char (char-after pos)) charset)
521 (if (< (mm-char-int char) 128)
522 (setq charset 'ascii)
523 ;; charset-after is fake in some Emacsen.
524 (setq charset (and (fboundp 'char-charset) (char-charset char)))
525 (if (eq charset 'composition) ; Mule 4
526 (let ((p (or pos (point))))
527 (cadr (find-charset-region p (1+ p))))
528 (if (and charset (not (memq charset '(ascii eight-bit-control
529 eight-bit-graphic))))
531 (mm-guess-charset))))))
533 (defun mm-mime-charset (charset)
534 "Return the MIME charset corresponding to the given Mule CHARSET."
535 (if (eq charset 'unknown)
536 (error "The message contains non-printable characters, please use attachment"))
537 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
538 ;; This exists in Emacs 20.
540 (and (mm-preferred-coding-system charset)
541 (or (coding-system-get
542 (mm-preferred-coding-system charset) :mime-charset)
544 (mm-preferred-coding-system charset) 'mime-charset)))
545 (and (eq charset 'ascii)
547 (mm-preferred-coding-system charset)
548 (mm-mule-charset-to-mime-charset charset))
549 ;; This is for XEmacs.
550 (mm-mule-charset-to-mime-charset charset)))
552 (defun mm-delete-duplicates (list)
553 "Simple substitute for CL `delete-duplicates', testing with `equal'."
556 (setq head (car list))
557 (setq list (delete head list))
558 (setq result (cons head result)))
561 ;; Fixme: This is used in places when it should be testing the
562 ;; default multibyteness. See mm-default-multibyte-p.
564 (if (and (not (featurep 'xemacs))
565 (boundp 'enable-multibyte-characters))
566 (defun mm-multibyte-p ()
567 "Non-nil if multibyte is enabled in the current buffer."
568 enable-multibyte-characters)
569 (defun mm-multibyte-p () (featurep 'mule))))
571 (defun mm-default-multibyte-p ()
572 "Return non-nil if the session is multibyte.
573 This affects whether coding conversion should be attempted generally."
575 (if (boundp 'default-enable-multibyte-characters)
576 default-enable-multibyte-characters
579 (defun mm-iso-8859-x-to-15-region (&optional b e)
580 (if (fboundp 'char-charset)
581 (let (charset item c inconvertible)
583 (if e (narrow-to-region b e))
584 (goto-char (point-min))
585 (skip-chars-forward "\0-\177")
588 ((not (setq item (assq (char-charset (setq c (char-after)))
589 mm-iso-8859-x-to-15-table)))
591 ((memq c (cdr (cdr item)))
592 (setq inconvertible t)
595 (insert-before-markers (prog1 (+ c (car (cdr item)))
597 (skip-chars-forward "\0-\177")))
598 (not inconvertible))))
600 (defun mm-sort-coding-systems-predicate (a b)
603 ;; Note: invalid entries are dropped silently
604 (and (setq cs (mm-coding-system-p cs))
605 (coding-system-base cs)))
606 mm-coding-system-priorities)))
607 (and (setq a (mm-coding-system-p a))
608 (if (setq b (mm-coding-system-p b))
609 (> (length (memq (coding-system-base a) priorities))
610 (length (memq (coding-system-base b) priorities)))
614 (autoload 'latin-unity-massage-name "latin-unity")
615 (autoload 'latin-unity-maybe-remap "latin-unity")
616 (autoload 'latin-unity-representations-feasible-region "latin-unity")
617 (autoload 'latin-unity-representations-present-region "latin-unity")
618 (defvar latin-unity-coding-systems)
619 (defvar latin-unity-ucs-list))
621 (defun mm-xemacs-find-mime-charset-1 (begin end)
622 "Determine which MIME charset to use to send region as message.
623 This uses the XEmacs-specific latin-unity package to better handle the
624 case where identical characters from diverse ISO-8859-? character sets
625 can be encoded using a single one of the corresponding coding systems.
627 It treats `mm-coding-system-priorities' as the list of preferred
628 coding systems; a useful example setting for this list in Western
629 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
630 to the very standard Latin 1 coding system, and only move to coding
631 systems that are less supported as is necessary to encode the
632 characters that exist in the buffer.
634 Latin Unity doesn't know about those non-ASCII Roman characters that
635 are available in various East Asian character sets. As such, its
636 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
637 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
638 But this is very much a corner case, so don't worry about it."
639 (let ((systems mm-coding-system-priorities) csets psets curset)
641 ;; Load the Latin Unity library, if available.
642 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
643 (require 'latin-unity))
645 ;; Now, can we use it?
646 (if (featurep 'latin-unity)
648 (setq csets (latin-unity-representations-feasible-region begin end)
649 psets (latin-unity-representations-present-region begin end))
653 ;; Pass back the first coding system in the preferred list
654 ;; that can encode the whole region.
655 (dolist (curset systems)
656 (setq curset (latin-unity-massage-name 'buffer-default curset))
658 ;; If the coding system is a universal coding system, then
659 ;; it can certainly encode all the characters in the region.
660 (if (memq curset latin-unity-ucs-list)
661 (throw 'done (list curset)))
663 ;; If a coding system isn't universal, and isn't in
664 ;; the list that latin unity knows about, we can't
665 ;; decide whether to use it here. Leave that until later
666 ;; in `mm-find-mime-charset-region' function, whence we
668 (unless (memq curset latin-unity-coding-systems)
671 ;; Right, we know about this coding system, and it may
672 ;; conceivably be able to encode all the characters in
674 (if (latin-unity-maybe-remap begin end curset csets psets t)
675 (throw 'done (list curset))))
677 ;; Can't encode using anything from the
678 ;; `mm-coding-system-priorities' list.
679 ;; Leave `mm-find-mime-charset' to do most of the work.
682 ;; Right, latin unity isn't available; let `mm-find-charset-region'
683 ;; take its default action, which equally applies to GNU Emacs.
686 (defmacro mm-xemacs-find-mime-charset (begin end)
687 (when (featurep 'xemacs)
688 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
690 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
691 "Return the MIME charsets needed to encode the region between B and E.
692 nil means ASCII, a single-element list represents an appropriate MIME
693 charset, and a longer list means no appropriate charset."
695 ;; The return possibilities of this function are a mess...
696 (or (and (mm-multibyte-p)
697 mm-use-find-coding-systems-region
698 ;; Find the mime-charset of the most preferred coding
699 ;; system that has one.
700 (let ((systems (find-coding-systems-region b e)))
701 (when mm-coding-system-priorities
703 (sort systems 'mm-sort-coding-systems-predicate)))
704 (setq systems (delq 'compound-text systems))
705 (unless (equal systems '(undecided))
707 (let* ((head (pop systems))
708 (cs (or (coding-system-get head :mime-charset)
709 (coding-system-get head 'mime-charset))))
710 ;; The mime-charset (`x-ctext') of
711 ;; `compound-text' is not in the IANA list. We
712 ;; shouldn't normally use anything here with a
713 ;; mime-charset having an `x-' prefix.
714 ;; Fixme: Allow this to be overridden, since
715 ;; there is existing use of x-ctext.
716 ;; Also people apparently need the coding system
717 ;; `iso-2022-jp-3' (which Mule-UCS defines with
718 ;; mime-charset, though it's not valid).
720 (not (string-match "^[Xx]-" (symbol-name cs)))
721 ;; UTF-16 of any variety is invalid for
722 ;; text parts and, unfortunately, has
723 ;; mime-charset defined both in Mule-UCS
724 ;; and versions of Emacs. (The name
725 ;; might be `mule-utf-16...' or
727 (not (string-match "utf-16" (symbol-name cs))))
729 charsets (list cs))))))
731 ;; If we're XEmacs, and some coding system is appropriate,
732 ;; mm-xemacs-find-mime-charset will return an appropriate list.
733 ;; Otherwise, we'll get nil, and the next setq will get invoked.
734 (setq charsets (mm-xemacs-find-mime-charset b e))
736 ;; We're not multibyte, or a single coding system won't cover it.
738 (mm-delete-duplicates
739 (mapcar 'mm-mime-charset
741 (mm-find-charset-region b e))))))
742 (if (and (> (length charsets) 1)
743 (memq 'iso-8859-15 charsets)
744 (memq 'iso-8859-15 hack-charsets)
745 (save-excursion (mm-iso-8859-x-to-15-region b e)))
746 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
747 mm-iso-8859-15-compatible))
748 (if (and (memq 'iso-2022-jp-2 charsets)
749 (memq 'iso-2022-jp-2 hack-charsets))
750 (setq charsets (delq 'iso-2022-jp charsets)))
751 ;; Attempt to reduce the number of charsets if utf-8 is available.
752 (if (and (featurep 'xemacs)
753 (> (length charsets) 1)
754 (mm-coding-system-p 'utf-8))
755 (let ((mm-coding-system-priorities
756 (cons 'utf-8 mm-coding-system-priorities)))
758 (mm-delete-duplicates
759 (mapcar 'mm-mime-charset
761 (mm-find-charset-region b e)))))))
764 (defmacro mm-with-unibyte-buffer (&rest forms)
765 "Create a temporary buffer, and evaluate FORMS there like `progn'.
766 Use unibyte mode for this."
767 `(let (default-enable-multibyte-characters)
768 (with-temp-buffer ,@forms)))
769 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
770 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
772 (defmacro mm-with-multibyte-buffer (&rest forms)
773 "Create a temporary buffer, and evaluate FORMS there like `progn'.
774 Use multibyte mode for this."
775 `(let ((default-enable-multibyte-characters t))
776 (with-temp-buffer ,@forms)))
777 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
778 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
780 (defmacro mm-with-unibyte-current-buffer (&rest forms)
781 "Evaluate FORMS with current buffer temporarily made unibyte.
782 Also bind `default-enable-multibyte-characters' to nil.
783 Equivalent to `progn' in XEmacs"
784 (let ((multibyte (make-symbol "multibyte"))
785 (buffer (make-symbol "buffer")))
787 (let ((,multibyte enable-multibyte-characters)
788 (,buffer (current-buffer)))
790 (let (default-enable-multibyte-characters)
791 (set-buffer-multibyte nil)
794 (set-buffer-multibyte ,multibyte)))
795 (let (default-enable-multibyte-characters)
797 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
798 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
800 (defmacro mm-with-unibyte (&rest forms)
801 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
802 `(let (default-enable-multibyte-characters)
804 (put 'mm-with-unibyte 'lisp-indent-function 0)
805 (put 'mm-with-unibyte 'edebug-form-spec '(body))
807 (defmacro mm-with-multibyte (&rest forms)
808 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
809 `(let ((default-enable-multibyte-characters t))
811 (put 'mm-with-multibyte 'lisp-indent-function 0)
812 (put 'mm-with-multibyte 'edebug-form-spec '(body))
814 (defun mm-find-charset-region (b e)
815 "Return a list of Emacs charsets in the region B to E."
817 ((and (mm-multibyte-p)
818 (fboundp 'find-charset-region))
819 ;; Remove composition since the base charsets have been included.
820 ;; Remove eight-bit-*, treat them as ascii.
821 (let ((css (find-charset-region b e)))
822 (mapcar (lambda (cs) (setq css (delq cs css)))
823 '(composition eight-bit-control eight-bit-graphic
827 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
830 (narrow-to-region b e)
831 (goto-char (point-min))
832 (skip-chars-forward "\0-\177")
837 (and (boundp 'current-language-environment)
838 (car (last (assq 'charset
839 (assoc current-language-environment
840 language-info-alist))))))
841 (if (eq charset 'ascii) (setq charset nil))
844 (car (last (assq mail-parse-charset
845 mm-mime-mule-charset-alist)))))
846 (list 'ascii (or charset 'latin-iso8859-1)))))))))
848 (defun mm-auto-mode-alist ()
849 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
850 (let ((alist auto-mode-alist)
853 (when (listp (cdar alist))
854 (push (car alist) out))
858 (defvar mm-inhibit-file-name-handlers
859 '(jka-compr-handler image-file-handler)
860 "A list of handlers doing (un)compression (etc) thingies.")
862 (defun mm-insert-file-contents (filename &optional visit beg end replace
864 "Like `insert-file-contents', but only reads in the file.
865 A buffer may be modified in several ways after reading into the buffer due
866 to advanced Emacs features, such as file-name-handlers, format decoding,
867 `find-file-hooks', etc.
868 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
869 This function ensures that none of these modifications will take place."
870 (let* ((format-alist nil)
871 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
872 (default-major-mode 'fundamental-mode)
873 (enable-local-variables nil)
874 (after-insert-file-functions nil)
875 (enable-local-eval nil)
876 (inhibit-file-name-operation (if inhibit
877 'insert-file-contents
878 inhibit-file-name-operation))
879 (inhibit-file-name-handlers
881 (append mm-inhibit-file-name-handlers
882 inhibit-file-name-handlers)
883 inhibit-file-name-handlers))
884 (ffh (if (boundp 'find-file-hook)
887 (val (symbol-value ffh)))
890 (insert-file-contents filename visit beg end replace)
893 (defun mm-append-to-file (start end filename &optional codesys inhibit)
894 "Append the contents of the region to the end of file FILENAME.
895 When called from a function, expects three arguments,
896 START, END and FILENAME. START and END are buffer positions
897 saying what text to write.
898 Optional fourth argument specifies the coding system to use when
900 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
901 (let ((coding-system-for-write
902 (or codesys mm-text-coding-system-for-write
903 mm-text-coding-system))
904 (inhibit-file-name-operation (if inhibit
906 inhibit-file-name-operation))
907 (inhibit-file-name-handlers
909 (append mm-inhibit-file-name-handlers
910 inhibit-file-name-handlers)
911 inhibit-file-name-handlers)))
912 (write-region start end filename t 'no-message)
913 (message "Appended to %s" filename)))
915 (defun mm-write-region (start end filename &optional append visit lockname
916 coding-system inhibit)
918 "Like `write-region'.
919 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
920 (let ((coding-system-for-write
921 (or coding-system mm-text-coding-system-for-write
922 mm-text-coding-system))
923 (inhibit-file-name-operation (if inhibit
925 inhibit-file-name-operation))
926 (inhibit-file-name-handlers
928 (append mm-inhibit-file-name-handlers
929 inhibit-file-name-handlers)
930 inhibit-file-name-handlers)))
931 (write-region start end filename append visit lockname)))
933 (defun mm-image-load-path (&optional package)
935 (dolist (path load-path (nreverse result))
938 (setq dir (concat (file-name-directory
939 (directory-file-name path))
940 "etc/images/" (or package "gnus/")))))
942 (push path result))))
944 ;; Fixme: This doesn't look useful where it's used.
945 (if (fboundp 'detect-coding-region)
946 (defun mm-detect-coding-region (start end)
947 "Like `detect-coding-region' except returning the best one."
948 (let ((coding-systems
949 (detect-coding-region start end)))
950 (or (car-safe coding-systems)
952 (defun mm-detect-coding-region (start end)
953 (let ((point (point)))
955 (skip-chars-forward "\0-\177" end)
957 (if (eq (point) end) 'ascii (mm-guess-charset))
958 (goto-char point)))))
960 (if (fboundp 'coding-system-get)
961 (defun mm-detect-mime-charset-region (start end)
962 "Detect MIME charset of the text in the region between START and END."
963 (let ((cs (mm-detect-coding-region start end)))
964 (or (coding-system-get cs :mime-charset)
965 (coding-system-get cs 'mime-charset))))
966 (defun mm-detect-mime-charset-region (start end)
967 "Detect MIME charset of the text in the region between START and END."
968 (let ((cs (mm-detect-coding-region start end)))
972 (unless (fboundp 'coding-system-to-mime-charset)
973 (defalias 'coding-system-to-mime-charset 'ignore)))
975 (defun mm-coding-system-to-mime-charset (coding-system)
976 "Return the MIME charset corresponding to CODING-SYSTEM.
977 To make this function work with XEmacs, the APEL package is required."
979 (or (and (fboundp 'coding-system-get)
980 (or (coding-system-get coding-system :mime-charset)
981 (coding-system-get coding-system 'mime-charset)))
982 (and (featurep 'xemacs)
983 (or (and (fboundp 'coding-system-to-mime-charset)
984 (not (eq (symbol-function 'coding-system-to-mime-charset)
986 (and (condition-case nil
989 (fboundp 'coding-system-to-mime-charset)))
990 (coding-system-to-mime-charset coding-system)))))
993 (require 'jka-compr))
995 (defun mm-decompress-buffer (filename &optional inplace force)
996 "Decompress buffer's contents, depending on jka-compr.
997 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
998 agrees with `jka-compr-compression-info-list', decompression is done.
999 Signal an error if FORCE is neither nil nor t and compressed data are
1000 not decompressed because `auto-compression-mode' is disabled.
1001 If INPLACE is nil, return decompressed data or nil without modifying
1002 the buffer. Otherwise, replace the buffer's contents with the
1003 decompressed data. The buffer's multibyteness must be turned off."
1006 (prog1 t (require 'jka-compr))
1007 (and (fboundp 'jka-compr-installed-p)
1008 (jka-compr-installed-p))))
1009 (let ((info (jka-compr-get-compression-info filename)))
1011 (unless (or (memq force (list nil t))
1012 (jka-compr-installed-p))
1014 (let ((prog (jka-compr-info-uncompress-program info))
1015 (args (jka-compr-info-uncompress-args info))
1016 (msg (format "%s %s..."
1017 (jka-compr-info-uncompress-message info)
1019 (err-file (jka-compr-make-temp-name))
1020 (cur (current-buffer))
1021 (coding-system-for-read mm-binary-coding-system)
1022 (coding-system-for-write mm-binary-coding-system)
1026 (insert-buffer-substring cur)
1029 (unless (memq (apply 'call-process-region
1030 (point-min) (point-max)
1031 prog t (list t err-file) nil args)
1032 jka-compr-acceptable-retval-list)
1036 (delete "" (split-string
1038 (insert-file-contents err-file)
1044 (format "Error while executing \"%s %s < %s\""
1045 prog (mapconcat 'identity args " ")
1047 (setq retval (buffer-string)))
1049 (setq err-msg (error-message-string err)))))
1050 (when (file-exists-p err-file)
1051 (ignore-errors (jka-compr-delete-temp-file err-file)))
1054 (delete-region (point-min) (point-max))
1057 (message "%s" (or err-msg (concat msg "done")))
1061 (unless (fboundp 'coding-system-name)
1062 (defalias 'coding-system-name 'ignore))
1063 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1064 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1065 (unless (fboundp 'find-operation-coding-system)
1066 (defalias 'find-operation-coding-system 'ignore)))
1068 (defun mm-find-buffer-file-coding-system (&optional filename)
1069 "Find coding system used to decode the contents of the current buffer.
1070 This function looks for the coding system magic cookie or examines the
1071 coding system specified by `file-coding-system-alist' being associated
1072 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1073 gzip, bzip2, etc. are allowed."
1075 (setq filename buffer-file-name))
1077 (let ((decomp (unless ;; No worth to examine charset of tar files.
1080 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1082 (mm-decompress-buffer filename nil t))))
1084 (set-buffer (let (default-enable-multibyte-characters)
1085 (generate-new-buffer " *temp*")))
1087 (setq filename (file-name-sans-extension filename)))
1088 (goto-char (point-min))
1091 ((boundp 'set-auto-coding-function) ;; Emacs
1093 (or (funcall (symbol-value 'set-auto-coding-function)
1094 filename (- (point-max) (point-min)))
1095 (car (find-operation-coding-system 'insert-file-contents
1097 (let (auto-coding-alist)
1099 (funcall (symbol-value 'set-auto-coding-function)
1100 nil (- (point-max) (point-min)))
1102 ((featurep 'file-coding) ;; XEmacs
1103 (let ((case-fold-search t)
1104 (end (point-at-eol))
1107 (and (re-search-forward "-\\*-+[\t ]*" end t)
1109 (setq start (match-end 0))
1110 (re-search-forward "[\t ]*-+\\*-" end t))
1112 (setq end (match-beginning 0))
1114 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1116 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1118 (find-coding-system (setq codesys
1119 (intern (match-string 1))))
1121 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1124 (setq start (match-end 0))
1125 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1127 (setq end (match-beginning 0))
1130 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1132 (find-coding-system (setq codesys
1133 (intern (match-string 1))))
1136 (goto-char (point-min))
1137 (setq case-fold-search nil)
1138 (re-search-forward "^;;;coding system: "
1139 ;;(+ (point-min) 3000) t))
1141 (looking-at "[^\t\n\r ]+")
1143 (setq codesys (intern (match-string 0))))
1147 (find-file-coding-system-for-read-from-filename
1149 (coding-system-name (coding-system-base codesys)))))))
1151 (kill-buffer (current-buffer)))))))
1155 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1156 ;;; mm-util.el ends here