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 . identity)
79 "Return a multibyte string with the same individual chars as string."
81 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
83 (multibyte-string-p . ignore)
84 ;; It is not a MIME function, but some MIME functions use it.
85 (make-temp-file . (lambda (prefix &optional dir-flag)
86 (let ((file (expand-file-name
87 (make-temp-name prefix)
88 (if (fboundp 'temp-directory)
90 temporary-file-directory))))
92 (make-directory file))
94 (insert-byte . insert-char)
95 (multibyte-char-to-unibyte . identity))))
98 (defalias 'mm-char-or-char-int-p
100 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
101 ((fboundp 'char-valid-p) 'char-valid-p)
104 ;; Fixme: This seems always to be used to read a MIME charset, so it
105 ;; should be re-named and fixed (in Emacs) to offer completion only on
106 ;; proper charset names (base coding systems which have a
107 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
109 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
110 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
111 ;; Actually, there should be an `mm-coding-system-mime-charset'.
113 (defalias 'mm-read-coding-system
115 ((fboundp 'read-coding-system)
116 (if (and (featurep 'xemacs)
117 (<= (string-to-number emacs-version) 21.1))
118 (lambda (prompt &optional default-coding-system)
119 (read-coding-system prompt))
120 'read-coding-system))
121 (t (lambda (prompt &optional default-coding-system)
122 "Prompt the user for a coding system."
124 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
125 mm-mime-mule-charset-alist)))))))
127 (defvar mm-coding-system-list nil)
128 (defun mm-get-coding-system-list ()
129 "Get the coding system list."
130 (or mm-coding-system-list
131 (setq mm-coding-system-list (mm-coding-system-list))))
133 (defun mm-coding-system-p (cs)
134 "Return non-nil if CS is a symbol naming a coding system.
135 In XEmacs, also return non-nil if CS is a coding system object.
136 If CS is available, return CS itself in Emacs, and return a coding
137 system object in XEmacs."
138 (if (fboundp 'find-coding-system)
139 (and cs (find-coding-system cs))
140 (if (fboundp 'coding-system-p)
141 (when (coding-system-p cs)
143 ;; Is this branch ever actually useful?
144 (car (memq cs (mm-get-coding-system-list))))))
146 (defvar mm-charset-synonym-alist
148 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
149 ,@(unless (mm-coding-system-p 'x-ctext)
150 '((x-ctext . ctext)))
151 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
152 ,@(unless (mm-coding-system-p 'iso-8859-15)
153 '((iso-8859-15 . iso-8859-1)))
154 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
155 ,@(unless (mm-coding-system-p 'big5-hkscs)
156 '((big5-hkscs . big5)))
157 ;; Windows-1252 is actually a superset of Latin-1. See also
158 ;; `gnus-article-dumbquotes-map'.
159 ,@(unless (mm-coding-system-p 'windows-1252)
160 (if (mm-coding-system-p 'cp1252)
161 '((windows-1252 . cp1252))
162 '((windows-1252 . iso-8859-1))))
163 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
164 ;; Outlook users in Czech republic. Use this to allow reading of their
165 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
166 ,@(if (and (not (mm-coding-system-p 'windows-1250))
167 (mm-coding-system-p 'cp1250))
168 '((windows-1250 . cp1250)))
169 ;; A Microsoft misunderstanding.
170 ,@(if (and (not (mm-coding-system-p 'unicode))
171 (mm-coding-system-p 'utf-16-le))
172 '((unicode . utf-16-le)))
173 ;; A Microsoft misunderstanding.
174 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
175 (if (mm-coding-system-p 'cp949)
176 '((ks_c_5601-1987 . cp949))
177 '((ks_c_5601-1987 . euc-kr))))
179 "A mapping from invalid charset names to the real charset names.")
181 (defvar mm-binary-coding-system
183 ((mm-coding-system-p 'binary) 'binary)
184 ((mm-coding-system-p 'no-conversion) 'no-conversion)
186 "100% binary coding system.")
188 (defvar mm-text-coding-system
189 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
190 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
191 (and (mm-coding-system-p 'raw-text) 'raw-text))
192 mm-binary-coding-system)
193 "Text-safe coding system (For removing ^M).")
195 (defvar mm-text-coding-system-for-write nil
196 "Text coding system for write.")
198 (defvar mm-auto-save-coding-system
200 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
201 (if (memq system-type '(windows-nt ms-dos ms-windows))
202 (if (mm-coding-system-p 'utf-8-emacs-dos)
203 'utf-8-emacs-dos mm-binary-coding-system)
205 ((mm-coding-system-p 'emacs-mule)
206 (if (memq system-type '(windows-nt ms-dos ms-windows))
207 (if (mm-coding-system-p 'emacs-mule-dos)
208 'emacs-mule-dos mm-binary-coding-system)
210 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
211 (t mm-binary-coding-system))
212 "Coding system of auto save file.")
214 (defvar mm-universal-coding-system mm-auto-save-coding-system
215 "The universal coding system.")
217 ;; Fixme: some of the cars here aren't valid MIME charsets. That
218 ;; should only matter with XEmacs, though.
219 (defvar mm-mime-mule-charset-alist
221 (iso-8859-1 latin-iso8859-1)
222 (iso-8859-2 latin-iso8859-2)
223 (iso-8859-3 latin-iso8859-3)
224 (iso-8859-4 latin-iso8859-4)
225 (iso-8859-5 cyrillic-iso8859-5)
226 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
227 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
228 ;; charset is koi8-r, not iso-8859-5.
229 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
230 (iso-8859-6 arabic-iso8859-6)
231 (iso-8859-7 greek-iso8859-7)
232 (iso-8859-8 hebrew-iso8859-8)
233 (iso-8859-9 latin-iso8859-9)
234 (iso-8859-14 latin-iso8859-14)
235 (iso-8859-15 latin-iso8859-15)
236 (viscii vietnamese-viscii-lower)
237 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
238 (euc-kr korean-ksc5601)
239 (gb2312 chinese-gb2312)
240 (big5 chinese-big5-1 chinese-big5-2)
242 (thai-tis620 thai-tis620)
243 (windows-1251 cyrillic-iso8859-5)
244 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
245 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
246 latin-jisx0201 japanese-jisx0208-1978
247 chinese-gb2312 japanese-jisx0208
248 korean-ksc5601 japanese-jisx0212)
249 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
250 latin-jisx0201 japanese-jisx0208-1978
251 chinese-gb2312 japanese-jisx0208
252 korean-ksc5601 japanese-jisx0212
253 chinese-cns11643-1 chinese-cns11643-2)
254 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
255 cyrillic-iso8859-5 greek-iso8859-7
256 latin-jisx0201 japanese-jisx0208-1978
257 chinese-gb2312 japanese-jisx0208
258 korean-ksc5601 japanese-jisx0212
259 chinese-cns11643-1 chinese-cns11643-2
260 chinese-cns11643-3 chinese-cns11643-4
261 chinese-cns11643-5 chinese-cns11643-6
263 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
264 japanese-jisx0213-1 japanese-jisx0213-2)
265 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
266 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
267 (charsetp 'unicode-a)
268 (not (mm-coding-system-p 'mule-utf-8)))
269 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
270 ;; If we have utf-8 we're in Mule 5+.
273 (coding-system-get 'mule-utf-8 'safe-charsets)))))
274 "Alist of MIME-charset/MULE-charsets.")
276 (defun mm-enrich-utf-8-by-mule-ucs ()
277 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
278 This function will run when the `un-define' module is loaded under
279 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
280 with Mule charsets. It is completely useless for Emacs."
281 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
282 (assoc "un-define" after-load-alist)))
283 (setq after-load-alist
284 (delete '("un-define") after-load-alist)))
285 (when (boundp 'unicode-basic-translation-charset-order-list)
291 'unicode-basic-translation-charset-order-list))))
292 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
295 (setq mm-mime-mule-charset-alist
296 (nconc mm-mime-mule-charset-alist
297 (list (cons 'utf-8 val))))))
300 ;; Correct by construction, but should be unnecessary for Emacs:
301 (if (featurep 'xemacs)
302 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
303 (when (and (fboundp 'coding-system-list)
304 (fboundp 'sort-coding-systems))
305 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
309 mime (or (coding-system-get cs :mime-charset) ; Emacs 22
310 (coding-system-get cs 'mime-charset)))
312 (not (eq t (setq mule
313 (coding-system-get cs 'safe-charsets))))
314 (not (assq mime alist)))
315 (push (cons mime (delq 'ascii mule)) alist)))
316 (setq mm-mime-mule-charset-alist (nreverse alist)))))
318 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
319 "A list of special charsets.
320 Valid elements include:
321 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
322 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
325 (defvar mm-iso-8859-15-compatible
326 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
327 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
328 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
330 (defvar mm-iso-8859-x-to-15-table
331 (and (fboundp 'coding-system-p)
332 (mm-coding-system-p 'iso-8859-15)
335 (if (mm-coding-system-p (car cs))
336 (let ((c (string-to-char
337 (decode-coding-string "\341" (car cs)))))
338 (cons (char-charset c)
341 (decode-coding-string "\341" 'iso-8859-15)) c)
342 (string-to-list (decode-coding-string (car (cdr cs))
345 mm-iso-8859-15-compatible))
346 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
348 (defcustom mm-coding-system-priorities
349 (if (boundp 'current-language-environment)
350 (let ((lang (symbol-value 'current-language-environment)))
351 (cond ((string= lang "Japanese")
352 ;; Japanese users prefer iso-2022-jp to euc-japan or
353 ;; shift_jis, however iso-8859-1 should be used when
354 ;; there are only ASCII text and Latin-1 characters.
355 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
356 "Preferred coding systems for encoding outgoing messages.
358 More than one suitable coding system may be found for some text.
359 By default, the coding system with the highest priority is used
360 to encode outgoing messages (see `sort-coding-systems'). If this
361 variable is set, it overrides the default priority."
363 :type '(repeat (symbol :tag "Coding system"))
367 (defvar mm-use-find-coding-systems-region
368 (fboundp 'find-coding-systems-region)
369 "Use `find-coding-systems-region' to find proper coding systems.
371 Setting it to nil is useful on Emacsen supporting Unicode if sending
372 mail with multiple parts is preferred to sending a Unicode one.")
374 ;;; Internal variables:
378 (defun mm-mule-charset-to-mime-charset (charset)
379 "Return the MIME charset corresponding to the given Mule CHARSET."
380 (if (and (fboundp 'find-coding-systems-for-charsets)
381 (fboundp 'sort-coding-systems))
382 (let ((css (sort (sort-coding-systems
383 (find-coding-systems-for-charsets (list charset)))
384 'mm-sort-coding-systems-predicate))
386 (while (and (not mime)
388 (when (setq cs (pop css))
389 (setq mime (or (coding-system-get cs :mime-charset)
390 (coding-system-get cs 'mime-charset)))))
392 (let ((alist (mapcar (lambda (cs)
393 (assq cs mm-mime-mule-charset-alist))
394 (sort (mapcar 'car mm-mime-mule-charset-alist)
395 'mm-sort-coding-systems-predicate)))
398 (when (memq charset (cdar alist))
399 (setq out (caar alist)
404 (defun mm-charset-to-coding-system (charset &optional lbt)
405 "Return coding-system corresponding to CHARSET.
406 CHARSET is a symbol naming a MIME charset.
407 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
408 used as the line break code type of the coding system."
409 (when (stringp charset)
410 (setq charset (intern (downcase charset))))
412 (setq charset (intern (format "%s-%s" charset lbt))))
416 ;; Running in a non-MULE environment.
417 ((or (null (mm-get-coding-system-list))
418 (not (fboundp 'coding-system-get)))
421 ((eq charset 'us-ascii)
423 ;; Check to see whether we can handle this charset. (This depends
424 ;; on there being some coding system matching each `mime-charset'
425 ;; property defined, as there should be.)
426 ((and (mm-coding-system-p charset)
427 ;;; Doing this would potentially weed out incorrect charsets.
429 ;;; (eq charset (coding-system-get charset 'mime-charset))
432 ;; Translate invalid charsets.
433 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
434 (and cs (mm-coding-system-p cs) cs)))
435 ;; Last resort: search the coding system list for entries which
436 ;; have the right mime-charset in case the canonical name isn't
437 ;; defined (though it should be).
439 ;; mm-get-coding-system-list returns a list of cs without lbt.
441 (dolist (c (mm-get-coding-system-list))
443 (eq charset (or (coding-system-get c :mime-charset)
444 (coding-system-get c 'mime-charset))))
449 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
450 (boundp 'default-enable-multibyte-characters)
451 default-enable-multibyte-characters
452 (fboundp 'set-buffer-multibyte))
453 "True in Emacs with Mule.")
456 (defun mm-enable-multibyte ()
457 "Set the multibyte flag of the current buffer.
458 Only do this if the default value of `enable-multibyte-characters' is
459 non-nil. This is a no-op in XEmacs."
460 (set-buffer-multibyte 'to))
461 (defalias 'mm-enable-multibyte 'ignore))
464 (defun mm-disable-multibyte ()
465 "Unset the multibyte flag of in the current buffer.
466 This is a no-op in XEmacs."
467 (set-buffer-multibyte nil))
468 (defalias 'mm-disable-multibyte 'ignore)))
470 (defun mm-preferred-coding-system (charset)
471 ;; A typo in some Emacs versions.
472 (or (get-charset-property charset 'preferred-coding-system)
473 (get-charset-property charset 'prefered-coding-system)))
475 ;; Mule charsets shouldn't be used.
476 (defsubst mm-guess-charset ()
477 "Guess Mule charset from the language environment."
479 mail-parse-mule-charset ;; cached mule-charset
481 (setq mail-parse-mule-charset
482 (and (boundp 'current-language-environment)
485 (assoc current-language-environment
486 language-info-alist))))))
487 (if (or (not mail-parse-mule-charset)
488 (eq mail-parse-mule-charset 'ascii))
489 (setq mail-parse-mule-charset
490 (or (car (last (assq mail-parse-charset
491 mm-mime-mule-charset-alist)))
494 mail-parse-mule-charset)))
496 (defun mm-charset-after (&optional pos)
497 "Return charset of a character in current buffer at position POS.
498 If POS is nil, it defauls to the current point.
499 If POS is out of range, the value is nil.
500 If the charset is `composition', return the actual one."
501 (let ((char (char-after pos)) charset)
502 (if (< (mm-char-int char) 128)
503 (setq charset 'ascii)
504 ;; charset-after is fake in some Emacsen.
505 (setq charset (and (fboundp 'char-charset) (char-charset char)))
506 (if (eq charset 'composition) ; Mule 4
507 (let ((p (or pos (point))))
508 (cadr (find-charset-region p (1+ p))))
509 (if (and charset (not (memq charset '(ascii eight-bit-control
510 eight-bit-graphic))))
512 (mm-guess-charset))))))
514 (defun mm-mime-charset (charset)
515 "Return the MIME charset corresponding to the given Mule CHARSET."
516 (if (eq charset 'unknown)
517 (error "The message contains non-printable characters, please use attachment"))
518 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
519 ;; This exists in Emacs 20.
521 (and (mm-preferred-coding-system charset)
522 (or (coding-system-get
523 (mm-preferred-coding-system charset) :mime-charset)
525 (mm-preferred-coding-system charset) 'mime-charset)))
526 (and (eq charset 'ascii)
528 (mm-preferred-coding-system charset)
529 (mm-mule-charset-to-mime-charset charset))
530 ;; This is for XEmacs.
531 (mm-mule-charset-to-mime-charset charset)))
533 (defun mm-delete-duplicates (list)
534 "Simple substitute for CL `delete-duplicates', testing with `equal'."
537 (setq head (car list))
538 (setq list (delete head list))
539 (setq result (cons head result)))
542 ;; Fixme: This is used in places when it should be testing the
543 ;; default multibyteness. See mm-default-multibyte-p.
545 (if (and (not (featurep 'xemacs))
546 (boundp 'enable-multibyte-characters))
547 (defun mm-multibyte-p ()
548 "Non-nil if multibyte is enabled in the current buffer."
549 enable-multibyte-characters)
550 (defun mm-multibyte-p () (featurep 'mule))))
552 (defun mm-default-multibyte-p ()
553 "Return non-nil if the session is multibyte.
554 This affects whether coding conversion should be attempted generally."
556 (if (boundp 'default-enable-multibyte-characters)
557 default-enable-multibyte-characters
560 (defun mm-iso-8859-x-to-15-region (&optional b e)
561 (if (fboundp 'char-charset)
562 (let (charset item c inconvertible)
564 (if e (narrow-to-region b e))
565 (goto-char (point-min))
566 (skip-chars-forward "\0-\177")
569 ((not (setq item (assq (char-charset (setq c (char-after)))
570 mm-iso-8859-x-to-15-table)))
572 ((memq c (cdr (cdr item)))
573 (setq inconvertible t)
576 (insert-before-markers (prog1 (+ c (car (cdr item)))
578 (skip-chars-forward "\0-\177")))
579 (not inconvertible))))
581 (defun mm-sort-coding-systems-predicate (a b)
584 ;; Note: invalid entries are dropped silently
585 (and (setq cs (mm-coding-system-p cs))
586 (coding-system-base cs)))
587 mm-coding-system-priorities)))
588 (and (setq a (mm-coding-system-p a))
589 (if (setq b (mm-coding-system-p b))
590 (> (length (memq (coding-system-base a) priorities))
591 (length (memq (coding-system-base b) priorities)))
595 (autoload 'latin-unity-massage-name "latin-unity")
596 (autoload 'latin-unity-maybe-remap "latin-unity")
597 (autoload 'latin-unity-representations-feasible-region "latin-unity")
598 (autoload 'latin-unity-representations-present-region "latin-unity")
599 (defvar latin-unity-coding-systems)
600 (defvar latin-unity-ucs-list))
602 (defun mm-xemacs-find-mime-charset-1 (begin end)
603 "Determine which MIME charset to use to send region as message.
604 This uses the XEmacs-specific latin-unity package to better handle the
605 case where identical characters from diverse ISO-8859-? character sets
606 can be encoded using a single one of the corresponding coding systems.
608 It treats `mm-coding-system-priorities' as the list of preferred
609 coding systems; a useful example setting for this list in Western
610 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
611 to the very standard Latin 1 coding system, and only move to coding
612 systems that are less supported as is necessary to encode the
613 characters that exist in the buffer.
615 Latin Unity doesn't know about those non-ASCII Roman characters that
616 are available in various East Asian character sets. As such, its
617 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
618 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
619 But this is very much a corner case, so don't worry about it."
620 (let ((systems mm-coding-system-priorities) csets psets curset)
622 ;; Load the Latin Unity library, if available.
623 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
624 (require 'latin-unity))
626 ;; Now, can we use it?
627 (if (featurep 'latin-unity)
629 (setq csets (latin-unity-representations-feasible-region begin end)
630 psets (latin-unity-representations-present-region begin end))
634 ;; Pass back the first coding system in the preferred list
635 ;; that can encode the whole region.
636 (dolist (curset systems)
637 (setq curset (latin-unity-massage-name 'buffer-default curset))
639 ;; If the coding system is a universal coding system, then
640 ;; it can certainly encode all the characters in the region.
641 (if (memq curset latin-unity-ucs-list)
642 (throw 'done (list curset)))
644 ;; If a coding system isn't universal, and isn't in
645 ;; the list that latin unity knows about, we can't
646 ;; decide whether to use it here. Leave that until later
647 ;; in `mm-find-mime-charset-region' function, whence we
649 (unless (memq curset latin-unity-coding-systems)
652 ;; Right, we know about this coding system, and it may
653 ;; conceivably be able to encode all the characters in
655 (if (latin-unity-maybe-remap begin end curset csets psets t)
656 (throw 'done (list curset))))
658 ;; Can't encode using anything from the
659 ;; `mm-coding-system-priorities' list.
660 ;; Leave `mm-find-mime-charset' to do most of the work.
663 ;; Right, latin unity isn't available; let `mm-find-charset-region'
664 ;; take its default action, which equally applies to GNU Emacs.
667 (defmacro mm-xemacs-find-mime-charset (begin end)
668 (when (featurep 'xemacs)
669 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
671 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
672 "Return the MIME charsets needed to encode the region between B and E.
673 nil means ASCII, a single-element list represents an appropriate MIME
674 charset, and a longer list means no appropriate charset."
676 ;; The return possibilities of this function are a mess...
677 (or (and (mm-multibyte-p)
678 mm-use-find-coding-systems-region
679 ;; Find the mime-charset of the most preferred coding
680 ;; system that has one.
681 (let ((systems (find-coding-systems-region b e)))
682 (when mm-coding-system-priorities
684 (sort systems 'mm-sort-coding-systems-predicate)))
685 (setq systems (delq 'compound-text systems))
686 (unless (equal systems '(undecided))
688 (let* ((head (pop systems))
689 (cs (or (coding-system-get head :mime-charset)
690 (coding-system-get head 'mime-charset))))
691 ;; The mime-charset (`x-ctext') of
692 ;; `compound-text' is not in the IANA list. We
693 ;; shouldn't normally use anything here with a
694 ;; mime-charset having an `x-' prefix.
695 ;; Fixme: Allow this to be overridden, since
696 ;; there is existing use of x-ctext.
697 ;; Also people apparently need the coding system
698 ;; `iso-2022-jp-3' (which Mule-UCS defines with
699 ;; mime-charset, though it's not valid).
701 (not (string-match "^[Xx]-" (symbol-name cs)))
702 ;; UTF-16 of any variety is invalid for
703 ;; text parts and, unfortunately, has
704 ;; mime-charset defined both in Mule-UCS
705 ;; and versions of Emacs. (The name
706 ;; might be `mule-utf-16...' or
708 (not (string-match "utf-16" (symbol-name cs))))
710 charsets (list cs))))))
712 ;; If we're XEmacs, and some coding system is appropriate,
713 ;; mm-xemacs-find-mime-charset will return an appropriate list.
714 ;; Otherwise, we'll get nil, and the next setq will get invoked.
715 (setq charsets (mm-xemacs-find-mime-charset b e))
717 ;; We're not multibyte, or a single coding system won't cover it.
719 (mm-delete-duplicates
720 (mapcar 'mm-mime-charset
722 (mm-find-charset-region b e))))))
723 (if (and (> (length charsets) 1)
724 (memq 'iso-8859-15 charsets)
725 (memq 'iso-8859-15 hack-charsets)
726 (save-excursion (mm-iso-8859-x-to-15-region b e)))
727 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
728 mm-iso-8859-15-compatible))
729 (if (and (memq 'iso-2022-jp-2 charsets)
730 (memq 'iso-2022-jp-2 hack-charsets))
731 (setq charsets (delq 'iso-2022-jp charsets)))
734 (defmacro mm-with-unibyte-buffer (&rest forms)
735 "Create a temporary buffer, and evaluate FORMS there like `progn'.
736 Use unibyte mode for this."
737 `(let (default-enable-multibyte-characters)
738 (with-temp-buffer ,@forms)))
739 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
740 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
742 (defmacro mm-with-multibyte-buffer (&rest forms)
743 "Create a temporary buffer, and evaluate FORMS there like `progn'.
744 Use multibyte mode for this."
745 `(let ((default-enable-multibyte-characters t))
746 (with-temp-buffer ,@forms)))
747 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
748 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
750 (defmacro mm-with-unibyte-current-buffer (&rest forms)
751 "Evaluate FORMS with current buffer temporarily made unibyte.
752 Also bind `default-enable-multibyte-characters' to nil.
753 Equivalent to `progn' in XEmacs"
754 (let ((multibyte (make-symbol "multibyte"))
755 (buffer (make-symbol "buffer")))
757 (let ((,multibyte enable-multibyte-characters)
758 (,buffer (current-buffer)))
760 (let (default-enable-multibyte-characters)
761 (set-buffer-multibyte nil)
764 (set-buffer-multibyte ,multibyte)))
765 (let (default-enable-multibyte-characters)
767 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
768 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
770 (defmacro mm-with-unibyte (&rest forms)
771 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
772 `(let (default-enable-multibyte-characters)
774 (put 'mm-with-unibyte 'lisp-indent-function 0)
775 (put 'mm-with-unibyte 'edebug-form-spec '(body))
777 (defmacro mm-with-multibyte (&rest forms)
778 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
779 `(let ((default-enable-multibyte-characters t))
781 (put 'mm-with-multibyte 'lisp-indent-function 0)
782 (put 'mm-with-multibyte 'edebug-form-spec '(body))
784 (defun mm-find-charset-region (b e)
785 "Return a list of Emacs charsets in the region B to E."
787 ((and (mm-multibyte-p)
788 (fboundp 'find-charset-region))
789 ;; Remove composition since the base charsets have been included.
790 ;; Remove eight-bit-*, treat them as ascii.
791 (let ((css (find-charset-region b e)))
792 (mapcar (lambda (cs) (setq css (delq cs css)))
793 '(composition eight-bit-control eight-bit-graphic
797 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
800 (narrow-to-region b e)
801 (goto-char (point-min))
802 (skip-chars-forward "\0-\177")
807 (and (boundp 'current-language-environment)
808 (car (last (assq 'charset
809 (assoc current-language-environment
810 language-info-alist))))))
811 (if (eq charset 'ascii) (setq charset nil))
814 (car (last (assq mail-parse-charset
815 mm-mime-mule-charset-alist)))))
816 (list 'ascii (or charset 'latin-iso8859-1)))))))))
818 (defun mm-auto-mode-alist ()
819 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
820 (let ((alist auto-mode-alist)
823 (when (listp (cdar alist))
824 (push (car alist) out))
828 (defvar mm-inhibit-file-name-handlers
829 '(jka-compr-handler image-file-handler)
830 "A list of handlers doing (un)compression (etc) thingies.")
832 (defun mm-insert-file-contents (filename &optional visit beg end replace
834 "Like `insert-file-contents', but only reads in the file.
835 A buffer may be modified in several ways after reading into the buffer due
836 to advanced Emacs features, such as file-name-handlers, format decoding,
837 `find-file-hooks', etc.
838 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
839 This function ensures that none of these modifications will take place."
840 (let ((format-alist nil)
841 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
842 (default-major-mode 'fundamental-mode)
843 (enable-local-variables nil)
844 (after-insert-file-functions nil)
845 (enable-local-eval nil)
846 (find-file-hooks nil)
847 (inhibit-file-name-operation (if inhibit
848 'insert-file-contents
849 inhibit-file-name-operation))
850 (inhibit-file-name-handlers
852 (append mm-inhibit-file-name-handlers
853 inhibit-file-name-handlers)
854 inhibit-file-name-handlers)))
855 (insert-file-contents filename visit beg end replace)))
857 (defun mm-append-to-file (start end filename &optional codesys inhibit)
858 "Append the contents of the region to the end of file FILENAME.
859 When called from a function, expects three arguments,
860 START, END and FILENAME. START and END are buffer positions
861 saying what text to write.
862 Optional fourth argument specifies the coding system to use when
864 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
865 (let ((coding-system-for-write
866 (or codesys mm-text-coding-system-for-write
867 mm-text-coding-system))
868 (inhibit-file-name-operation (if inhibit
870 inhibit-file-name-operation))
871 (inhibit-file-name-handlers
873 (append mm-inhibit-file-name-handlers
874 inhibit-file-name-handlers)
875 inhibit-file-name-handlers)))
876 (write-region start end filename t 'no-message)
877 (message "Appended to %s" filename)))
879 (defun mm-write-region (start end filename &optional append visit lockname
880 coding-system inhibit)
882 "Like `write-region'.
883 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
884 (let ((coding-system-for-write
885 (or coding-system mm-text-coding-system-for-write
886 mm-text-coding-system))
887 (inhibit-file-name-operation (if inhibit
889 inhibit-file-name-operation))
890 (inhibit-file-name-handlers
892 (append mm-inhibit-file-name-handlers
893 inhibit-file-name-handlers)
894 inhibit-file-name-handlers)))
895 (write-region start end filename append visit lockname)))
897 (defun mm-image-load-path (&optional package)
899 (dolist (path load-path (nreverse result))
902 (setq dir (concat (file-name-directory
903 (directory-file-name path))
904 "etc/images/" (or package "gnus/")))))
906 (push path result))))
908 ;; Fixme: This doesn't look useful where it's used.
909 (if (fboundp 'detect-coding-region)
910 (defun mm-detect-coding-region (start end)
911 "Like `detect-coding-region' except returning the best one."
912 (let ((coding-systems
913 (detect-coding-region (point) (point-max))))
914 (or (car-safe coding-systems)
916 (defun mm-detect-coding-region (start end)
917 (let ((point (point)))
919 (skip-chars-forward "\0-\177" end)
921 (if (eq (point) end) 'ascii (mm-guess-charset))
922 (goto-char point)))))
924 (if (fboundp 'coding-system-get)
925 (defun mm-detect-mime-charset-region (start end)
926 "Detect MIME charset of the text in the region between START and END."
927 (let ((cs (mm-detect-coding-region start end)))
928 (or (coding-system-get cs :mime-charset)
929 (coding-system-get cs 'mime-charset))))
930 (defun mm-detect-mime-charset-region (start end)
931 "Detect MIME charset of the text in the region between START and END."
932 (let ((cs (mm-detect-coding-region start end)))
936 (unless (fboundp 'coding-system-to-mime-charset)
937 (defalias 'coding-system-to-mime-charset 'ignore)))
939 (defun mm-coding-system-to-mime-charset (coding-system)
940 "Return the MIME charset corresponding to CODING-SYSTEM.
941 To make this function work with XEmacs, the APEL package is required."
943 (or (and (fboundp 'coding-system-get)
944 (or (coding-system-get coding-system :mime-charset)
945 (coding-system-get coding-system 'mime-charset)))
946 (and (featurep 'xemacs)
947 (or (and (fboundp 'coding-system-to-mime-charset)
948 (not (eq (symbol-function 'coding-system-to-mime-charset)
950 (and (condition-case nil
953 (fboundp 'coding-system-to-mime-charset)))
954 (coding-system-to-mime-charset coding-system)))))
957 (require 'jka-compr))
959 (defun mm-decompress-buffer (filename &optional inplace force)
960 "Decompress buffer's contents, depending on jka-compr.
961 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
962 agrees with `jka-compr-compression-info-list', decompression is done.
963 Signal an error if FORCE is neither nil nor t and compressed data are
964 not decompressed because `auto-compression-mode' is disabled.
965 If INPLACE is nil, return decompressed data or nil without modifying
966 the buffer. Otherwise, replace the buffer's contents with the
967 decompressed data. The buffer's multibyteness must be turned off."
970 (prog1 t (require 'jka-compr))
971 (and (fboundp 'jka-compr-installed-p)
972 (jka-compr-installed-p))))
973 (let ((info (jka-compr-get-compression-info filename)))
975 (unless (or (memq force (list nil t))
976 (jka-compr-installed-p))
978 (let ((prog (jka-compr-info-uncompress-program info))
979 (args (jka-compr-info-uncompress-args info))
980 (msg (format "%s %s..."
981 (jka-compr-info-uncompress-message info)
983 (err-file (jka-compr-make-temp-name))
984 (cur (current-buffer))
985 (coding-system-for-read mm-binary-coding-system)
986 (coding-system-for-write mm-binary-coding-system)
990 (insert-buffer-substring cur)
993 (unless (memq (apply 'call-process-region
994 (point-min) (point-max)
995 prog t (list t err-file) nil args)
996 jka-compr-acceptable-retval-list)
1000 (delete "" (split-string
1002 (insert-file-contents err-file)
1008 (format "Error while executing \"%s %s < %s\""
1009 prog (mapconcat 'identity args " ")
1011 (setq retval (buffer-string)))
1013 (setq err-msg (error-message-string err)))))
1014 (when (file-exists-p err-file)
1015 (ignore-errors (jka-compr-delete-temp-file err-file)))
1018 (delete-region (point-min) (point-max))
1021 (message "%s" (or err-msg (concat msg "done")))
1025 (unless (fboundp 'coding-system-name)
1026 (defalias 'coding-system-name 'ignore))
1027 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1028 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1029 (unless (fboundp 'find-operation-coding-system)
1030 (defalias 'find-operation-coding-system 'ignore)))
1032 (defun mm-find-buffer-file-coding-system (&optional filename)
1033 "Find coding system used to decode the contents of the current buffer.
1034 This function looks for the coding system magic cookie or examines the
1035 coding system specified by `file-coding-system-alist' being associated
1036 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1037 gzip, bzip2, etc. are allowed."
1039 (setq filename buffer-file-name))
1041 (let ((decomp (mm-decompress-buffer filename nil t)))
1043 (set-buffer (let (default-enable-multibyte-characters)
1044 (generate-new-buffer " *temp*")))
1046 (setq filename (file-name-sans-extension filename)))
1047 (goto-char (point-min))
1050 ((boundp 'set-auto-coding-function) ;; Emacs
1052 (or (funcall (symbol-value 'set-auto-coding-function)
1053 filename (- (point-max) (point-min)))
1054 (car (find-operation-coding-system 'insert-file-contents
1056 (let (auto-coding-alist)
1058 (funcall (symbol-value 'set-auto-coding-function)
1059 nil (- (point-max) (point-min)))
1061 ((featurep 'file-coding) ;; XEmacs
1062 (let ((case-fold-search t)
1063 (end (point-at-eol))
1066 (and (re-search-forward "-\\*-+[\t ]*" end t)
1068 (setq start (match-end 0))
1069 (re-search-forward "[\t ]*-+\\*-" end t))
1071 (setq end (match-beginning 0))
1073 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1075 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1077 (find-coding-system (setq codesys
1078 (intern (match-string 1))))
1080 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1083 (setq start (match-end 0))
1084 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1086 (setq end (match-beginning 0))
1089 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1091 (find-coding-system (setq codesys
1092 (intern (match-string 1))))
1095 (goto-char (point-min))
1096 (setq case-fold-search nil)
1097 (re-search-forward "^;;;coding system: "
1098 ;;(+ (point-min) 3000) t))
1100 (looking-at "[^\t\n\r ]+")
1102 (setq codesys (intern (match-string 0))))
1106 (find-file-coding-system-for-read-from-filename
1108 (coding-system-name (coding-system-base codesys)))))))
1110 (kill-buffer (current-buffer)))))))
1114 ;;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1115 ;;; mm-util.el ends here