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 unknown or invalid charset names to the real charset names.")
200 (defcustom mm-charset-override-alist
201 `((iso-8859-1 . windows-1252))
202 "A mapping from undesired charset names to their replacement.
204 You may add pair like (iso-8859-1 . windows-1252) here,
205 i.e. treat iso-8859-1 as windows-1252. windows-1252 is a
206 superset of iso-8859-1."
207 :type '(list (set :inline t
208 (const (iso-8859-1 . windows-1252))
209 (const (undecided . windows-1252)))
212 (cons (symbol :tag "From charset")
213 (symbol :tag "To charset"))))
214 :version "23.0" ;; No Gnus
217 (defvar mm-binary-coding-system
219 ((mm-coding-system-p 'binary) 'binary)
220 ((mm-coding-system-p 'no-conversion) 'no-conversion)
222 "100% binary coding system.")
224 (defvar mm-text-coding-system
225 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
226 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
227 (and (mm-coding-system-p 'raw-text) 'raw-text))
228 mm-binary-coding-system)
229 "Text-safe coding system (For removing ^M).")
231 (defvar mm-text-coding-system-for-write nil
232 "Text coding system for write.")
234 (defvar mm-auto-save-coding-system
236 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
237 (if (memq system-type '(windows-nt ms-dos ms-windows))
238 (if (mm-coding-system-p 'utf-8-emacs-dos)
239 'utf-8-emacs-dos mm-binary-coding-system)
241 ((mm-coding-system-p 'emacs-mule)
242 (if (memq system-type '(windows-nt ms-dos ms-windows))
243 (if (mm-coding-system-p 'emacs-mule-dos)
244 'emacs-mule-dos mm-binary-coding-system)
246 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
247 (t mm-binary-coding-system))
248 "Coding system of auto save file.")
250 (defvar mm-universal-coding-system mm-auto-save-coding-system
251 "The universal coding system.")
253 ;; Fixme: some of the cars here aren't valid MIME charsets. That
254 ;; should only matter with XEmacs, though.
255 (defvar mm-mime-mule-charset-alist
257 (iso-8859-1 latin-iso8859-1)
258 (iso-8859-2 latin-iso8859-2)
259 (iso-8859-3 latin-iso8859-3)
260 (iso-8859-4 latin-iso8859-4)
261 (iso-8859-5 cyrillic-iso8859-5)
262 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
263 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
264 ;; charset is koi8-r, not iso-8859-5.
265 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
266 (iso-8859-6 arabic-iso8859-6)
267 (iso-8859-7 greek-iso8859-7)
268 (iso-8859-8 hebrew-iso8859-8)
269 (iso-8859-9 latin-iso8859-9)
270 (iso-8859-14 latin-iso8859-14)
271 (iso-8859-15 latin-iso8859-15)
272 (viscii vietnamese-viscii-lower)
273 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
274 (euc-kr korean-ksc5601)
275 (gb2312 chinese-gb2312)
276 (big5 chinese-big5-1 chinese-big5-2)
278 (thai-tis620 thai-tis620)
279 (windows-1251 cyrillic-iso8859-5)
280 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
281 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
282 latin-jisx0201 japanese-jisx0208-1978
283 chinese-gb2312 japanese-jisx0208
284 korean-ksc5601 japanese-jisx0212)
285 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
286 latin-jisx0201 japanese-jisx0208-1978
287 chinese-gb2312 japanese-jisx0208
288 korean-ksc5601 japanese-jisx0212
289 chinese-cns11643-1 chinese-cns11643-2)
290 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
291 cyrillic-iso8859-5 greek-iso8859-7
292 latin-jisx0201 japanese-jisx0208-1978
293 chinese-gb2312 japanese-jisx0208
294 korean-ksc5601 japanese-jisx0212
295 chinese-cns11643-1 chinese-cns11643-2
296 chinese-cns11643-3 chinese-cns11643-4
297 chinese-cns11643-5 chinese-cns11643-6
299 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
300 japanese-jisx0213-1 japanese-jisx0213-2)
301 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
302 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
303 (charsetp 'unicode-a)
304 (not (mm-coding-system-p 'mule-utf-8)))
305 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
306 ;; If we have utf-8 we're in Mule 5+.
309 (coding-system-get 'mule-utf-8 'safe-charsets)))))
310 "Alist of MIME-charset/MULE-charsets.")
312 (defun mm-enrich-utf-8-by-mule-ucs ()
313 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
314 This function will run when the `un-define' module is loaded under
315 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
316 with Mule charsets. It is completely useless for Emacs."
317 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
318 (assoc "un-define" after-load-alist)))
319 (setq after-load-alist
320 (delete '("un-define") after-load-alist)))
321 (when (boundp 'unicode-basic-translation-charset-order-list)
327 'unicode-basic-translation-charset-order-list))))
328 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
331 (setq mm-mime-mule-charset-alist
332 (nconc mm-mime-mule-charset-alist
333 (list (cons 'utf-8 val))))))
336 ;; Correct by construction, but should be unnecessary for Emacs:
337 (if (featurep 'xemacs)
338 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
339 (when (and (fboundp 'coding-system-list)
340 (fboundp 'sort-coding-systems))
341 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
345 mime (or (coding-system-get cs :mime-charset) ; Emacs 22
346 (coding-system-get cs 'mime-charset)))
348 (not (eq t (setq mule
349 (coding-system-get cs 'safe-charsets))))
350 (not (assq mime alist)))
351 (push (cons mime (delq 'ascii mule)) alist)))
352 (setq mm-mime-mule-charset-alist (nreverse alist)))))
354 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
355 "A list of special charsets.
356 Valid elements include:
357 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
358 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
361 (defvar mm-iso-8859-15-compatible
362 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
363 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
364 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
366 (defvar mm-iso-8859-x-to-15-table
367 (and (fboundp 'coding-system-p)
368 (mm-coding-system-p 'iso-8859-15)
371 (if (mm-coding-system-p (car cs))
372 (let ((c (string-to-char
373 (decode-coding-string "\341" (car cs)))))
374 (cons (char-charset c)
377 (decode-coding-string "\341" 'iso-8859-15)) c)
378 (string-to-list (decode-coding-string (car (cdr cs))
381 mm-iso-8859-15-compatible))
382 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
384 (defcustom mm-coding-system-priorities
385 (if (boundp 'current-language-environment)
386 (let ((lang (symbol-value 'current-language-environment)))
387 (cond ((string= lang "Japanese")
388 ;; Japanese users prefer iso-2022-jp to euc-japan or
389 ;; shift_jis, however iso-8859-1 should be used when
390 ;; there are only ASCII text and Latin-1 characters.
391 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
392 "Preferred coding systems for encoding outgoing messages.
394 More than one suitable coding system may be found for some text.
395 By default, the coding system with the highest priority is used
396 to encode outgoing messages (see `sort-coding-systems'). If this
397 variable is set, it overrides the default priority."
399 :type '(repeat (symbol :tag "Coding system"))
403 (defvar mm-use-find-coding-systems-region
404 (fboundp 'find-coding-systems-region)
405 "Use `find-coding-systems-region' to find proper coding systems.
407 Setting it to nil is useful on Emacsen supporting Unicode if sending
408 mail with multiple parts is preferred to sending a Unicode one.")
410 ;;; Internal variables:
414 (defun mm-mule-charset-to-mime-charset (charset)
415 "Return the MIME charset corresponding to the given Mule CHARSET."
416 (if (and (fboundp 'find-coding-systems-for-charsets)
417 (fboundp 'sort-coding-systems))
418 (let ((css (sort (sort-coding-systems
419 (find-coding-systems-for-charsets (list charset)))
420 'mm-sort-coding-systems-predicate))
422 (while (and (not mime)
424 (when (setq cs (pop css))
425 (setq mime (or (coding-system-get cs :mime-charset)
426 (coding-system-get cs 'mime-charset)))))
428 (let ((alist (mapcar (lambda (cs)
429 (assq cs mm-mime-mule-charset-alist))
430 (sort (mapcar 'car mm-mime-mule-charset-alist)
431 'mm-sort-coding-systems-predicate)))
434 (when (memq charset (cdar alist))
435 (setq out (caar alist)
440 (defun mm-charset-to-coding-system (charset &optional lbt)
441 "Return coding-system corresponding to CHARSET.
442 CHARSET is a symbol naming a MIME charset.
443 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
444 used as the line break code type of the coding system."
445 (when (stringp charset)
446 (setq charset (intern (downcase charset))))
448 (setq charset (intern (format "%s-%s" charset lbt))))
452 ;; Running in a non-MULE environment.
453 ((or (null (mm-get-coding-system-list))
454 (not (fboundp 'coding-system-get)))
456 ;; Check override list quite early:
457 ((let ((cs (cdr (assq charset mm-charset-override-alist))))
458 (and cs (mm-coding-system-p cs) cs)))
460 ((eq charset 'us-ascii)
462 ;; Check to see whether we can handle this charset. (This depends
463 ;; on there being some coding system matching each `mime-charset'
464 ;; property defined, as there should be.)
465 ((and (mm-coding-system-p charset)
466 ;;; Doing this would potentially weed out incorrect charsets.
468 ;;; (eq charset (coding-system-get charset 'mime-charset))
471 ;; Translate invalid charsets.
472 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
473 (and cs (mm-coding-system-p cs) cs)))
474 ;; Last resort: search the coding system list for entries which
475 ;; have the right mime-charset in case the canonical name isn't
476 ;; defined (though it should be).
478 ;; mm-get-coding-system-list returns a list of cs without lbt.
480 (dolist (c (mm-get-coding-system-list))
482 (eq charset (or (coding-system-get c :mime-charset)
483 (coding-system-get c 'mime-charset))))
488 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
489 (boundp 'default-enable-multibyte-characters)
490 default-enable-multibyte-characters
491 (fboundp 'set-buffer-multibyte))
492 "True in Emacs with Mule.")
495 (defun mm-enable-multibyte ()
496 "Set the multibyte flag of the current buffer.
497 Only do this if the default value of `enable-multibyte-characters' is
498 non-nil. This is a no-op in XEmacs."
499 (set-buffer-multibyte 'to))
500 (defalias 'mm-enable-multibyte 'ignore))
503 (defun mm-disable-multibyte ()
504 "Unset the multibyte flag of in the current buffer.
505 This is a no-op in XEmacs."
506 (set-buffer-multibyte nil))
507 (defalias 'mm-disable-multibyte 'ignore)))
509 (defun mm-preferred-coding-system (charset)
510 ;; A typo in some Emacs versions.
511 (or (get-charset-property charset 'preferred-coding-system)
512 (get-charset-property charset 'prefered-coding-system)))
514 ;; Mule charsets shouldn't be used.
515 (defsubst mm-guess-charset ()
516 "Guess Mule charset from the language environment."
518 mail-parse-mule-charset ;; cached mule-charset
520 (setq mail-parse-mule-charset
521 (and (boundp 'current-language-environment)
524 (assoc current-language-environment
525 language-info-alist))))))
526 (if (or (not mail-parse-mule-charset)
527 (eq mail-parse-mule-charset 'ascii))
528 (setq mail-parse-mule-charset
529 (or (car (last (assq mail-parse-charset
530 mm-mime-mule-charset-alist)))
533 mail-parse-mule-charset)))
535 (defun mm-charset-after (&optional pos)
536 "Return charset of a character in current buffer at position POS.
537 If POS is nil, it defauls to the current point.
538 If POS is out of range, the value is nil.
539 If the charset is `composition', return the actual one."
540 (let ((char (char-after pos)) charset)
541 (if (< (mm-char-int char) 128)
542 (setq charset 'ascii)
543 ;; charset-after is fake in some Emacsen.
544 (setq charset (and (fboundp 'char-charset) (char-charset char)))
545 (if (eq charset 'composition) ; Mule 4
546 (let ((p (or pos (point))))
547 (cadr (find-charset-region p (1+ p))))
548 (if (and charset (not (memq charset '(ascii eight-bit-control
549 eight-bit-graphic))))
551 (mm-guess-charset))))))
553 (defun mm-mime-charset (charset)
554 "Return the MIME charset corresponding to the given Mule CHARSET."
555 (if (eq charset 'unknown)
556 (error "The message contains non-printable characters, please use attachment"))
557 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
558 ;; This exists in Emacs 20.
560 (and (mm-preferred-coding-system charset)
561 (or (coding-system-get
562 (mm-preferred-coding-system charset) :mime-charset)
564 (mm-preferred-coding-system charset) 'mime-charset)))
565 (and (eq charset 'ascii)
567 (mm-preferred-coding-system charset)
568 (mm-mule-charset-to-mime-charset charset))
569 ;; This is for XEmacs.
570 (mm-mule-charset-to-mime-charset charset)))
572 (if (fboundp 'delete-dups)
573 (defalias 'mm-delete-duplicates 'delete-dups)
574 (defun mm-delete-duplicates (list)
575 "Destructively remove `equal' duplicates from LIST.
576 Store the result in LIST and return it. LIST must be a proper list.
577 Of several `equal' occurrences of an element in LIST, the first
580 This is a compatibility function for Emacsen without `delete-dups'."
581 ;; Code from `subr.el' in Emacs 22:
584 (setcdr tail (delete (car tail) (cdr tail)))
585 (setq tail (cdr tail))))
588 ;; Fixme: This is used in places when it should be testing the
589 ;; default multibyteness. See mm-default-multibyte-p.
591 (if (and (not (featurep 'xemacs))
592 (boundp 'enable-multibyte-characters))
593 (defun mm-multibyte-p ()
594 "Non-nil if multibyte is enabled in the current buffer."
595 enable-multibyte-characters)
596 (defun mm-multibyte-p () (featurep 'mule))))
598 (defun mm-default-multibyte-p ()
599 "Return non-nil if the session is multibyte.
600 This affects whether coding conversion should be attempted generally."
602 (if (boundp 'default-enable-multibyte-characters)
603 default-enable-multibyte-characters
606 (defun mm-iso-8859-x-to-15-region (&optional b e)
607 (if (fboundp 'char-charset)
608 (let (charset item c inconvertible)
610 (if e (narrow-to-region b e))
611 (goto-char (point-min))
612 (skip-chars-forward "\0-\177")
615 ((not (setq item (assq (char-charset (setq c (char-after)))
616 mm-iso-8859-x-to-15-table)))
618 ((memq c (cdr (cdr item)))
619 (setq inconvertible t)
622 (insert-before-markers (prog1 (+ c (car (cdr item)))
624 (skip-chars-forward "\0-\177")))
625 (not inconvertible))))
627 (defun mm-sort-coding-systems-predicate (a b)
630 ;; Note: invalid entries are dropped silently
631 (and (setq cs (mm-coding-system-p cs))
632 (coding-system-base cs)))
633 mm-coding-system-priorities)))
634 (and (setq a (mm-coding-system-p a))
635 (if (setq b (mm-coding-system-p b))
636 (> (length (memq (coding-system-base a) priorities))
637 (length (memq (coding-system-base b) priorities)))
641 (autoload 'latin-unity-massage-name "latin-unity")
642 (autoload 'latin-unity-maybe-remap "latin-unity")
643 (autoload 'latin-unity-representations-feasible-region "latin-unity")
644 (autoload 'latin-unity-representations-present-region "latin-unity")
645 (defvar latin-unity-coding-systems)
646 (defvar latin-unity-ucs-list))
648 (defun mm-xemacs-find-mime-charset-1 (begin end)
649 "Determine which MIME charset to use to send region as message.
650 This uses the XEmacs-specific latin-unity package to better handle the
651 case where identical characters from diverse ISO-8859-? character sets
652 can be encoded using a single one of the corresponding coding systems.
654 It treats `mm-coding-system-priorities' as the list of preferred
655 coding systems; a useful example setting for this list in Western
656 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
657 to the very standard Latin 1 coding system, and only move to coding
658 systems that are less supported as is necessary to encode the
659 characters that exist in the buffer.
661 Latin Unity doesn't know about those non-ASCII Roman characters that
662 are available in various East Asian character sets. As such, its
663 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
664 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
665 But this is very much a corner case, so don't worry about it."
666 (let ((systems mm-coding-system-priorities) csets psets curset)
668 ;; Load the Latin Unity library, if available.
669 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
670 (require 'latin-unity))
672 ;; Now, can we use it?
673 (if (featurep 'latin-unity)
675 (setq csets (latin-unity-representations-feasible-region begin end)
676 psets (latin-unity-representations-present-region begin end))
680 ;; Pass back the first coding system in the preferred list
681 ;; that can encode the whole region.
682 (dolist (curset systems)
683 (setq curset (latin-unity-massage-name 'buffer-default curset))
685 ;; If the coding system is a universal coding system, then
686 ;; it can certainly encode all the characters in the region.
687 (if (memq curset latin-unity-ucs-list)
688 (throw 'done (list curset)))
690 ;; If a coding system isn't universal, and isn't in
691 ;; the list that latin unity knows about, we can't
692 ;; decide whether to use it here. Leave that until later
693 ;; in `mm-find-mime-charset-region' function, whence we
695 (unless (memq curset latin-unity-coding-systems)
698 ;; Right, we know about this coding system, and it may
699 ;; conceivably be able to encode all the characters in
701 (if (latin-unity-maybe-remap begin end curset csets psets t)
702 (throw 'done (list curset))))
704 ;; Can't encode using anything from the
705 ;; `mm-coding-system-priorities' list.
706 ;; Leave `mm-find-mime-charset' to do most of the work.
709 ;; Right, latin unity isn't available; let `mm-find-charset-region'
710 ;; take its default action, which equally applies to GNU Emacs.
713 (defmacro mm-xemacs-find-mime-charset (begin end)
714 (when (featurep 'xemacs)
715 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
717 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
718 "Return the MIME charsets needed to encode the region between B and E.
719 nil means ASCII, a single-element list represents an appropriate MIME
720 charset, and a longer list means no appropriate charset."
722 ;; The return possibilities of this function are a mess...
723 (or (and (mm-multibyte-p)
724 mm-use-find-coding-systems-region
725 ;; Find the mime-charset of the most preferred coding
726 ;; system that has one.
727 (let ((systems (find-coding-systems-region b e)))
728 (when mm-coding-system-priorities
730 (sort systems 'mm-sort-coding-systems-predicate)))
731 (setq systems (delq 'compound-text systems))
732 (unless (equal systems '(undecided))
734 (let* ((head (pop systems))
735 (cs (or (coding-system-get head :mime-charset)
736 (coding-system-get head 'mime-charset))))
737 ;; The mime-charset (`x-ctext') of
738 ;; `compound-text' is not in the IANA list. We
739 ;; shouldn't normally use anything here with a
740 ;; mime-charset having an `x-' prefix.
741 ;; Fixme: Allow this to be overridden, since
742 ;; there is existing use of x-ctext.
743 ;; Also people apparently need the coding system
744 ;; `iso-2022-jp-3' (which Mule-UCS defines with
745 ;; mime-charset, though it's not valid).
747 (not (string-match "^[Xx]-" (symbol-name cs)))
748 ;; UTF-16 of any variety is invalid for
749 ;; text parts and, unfortunately, has
750 ;; mime-charset defined both in Mule-UCS
751 ;; and versions of Emacs. (The name
752 ;; might be `mule-utf-16...' or
754 (not (string-match "utf-16" (symbol-name cs))))
756 charsets (list cs))))))
758 ;; If we're XEmacs, and some coding system is appropriate,
759 ;; mm-xemacs-find-mime-charset will return an appropriate list.
760 ;; Otherwise, we'll get nil, and the next setq will get invoked.
761 (setq charsets (mm-xemacs-find-mime-charset b e))
763 ;; We're not multibyte, or a single coding system won't cover it.
765 (mm-delete-duplicates
766 (mapcar 'mm-mime-charset
768 (mm-find-charset-region b e))))))
769 (if (and (> (length charsets) 1)
770 (memq 'iso-8859-15 charsets)
771 (memq 'iso-8859-15 hack-charsets)
772 (save-excursion (mm-iso-8859-x-to-15-region b e)))
773 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
774 mm-iso-8859-15-compatible))
775 (if (and (memq 'iso-2022-jp-2 charsets)
776 (memq 'iso-2022-jp-2 hack-charsets))
777 (setq charsets (delq 'iso-2022-jp charsets)))
778 ;; Attempt to reduce the number of charsets if utf-8 is available.
779 (if (and (featurep 'xemacs)
780 (> (length charsets) 1)
781 (mm-coding-system-p 'utf-8))
782 (let ((mm-coding-system-priorities
783 (cons 'utf-8 mm-coding-system-priorities)))
785 (mm-delete-duplicates
786 (mapcar 'mm-mime-charset
788 (mm-find-charset-region b e)))))))
791 (defmacro mm-with-unibyte-buffer (&rest forms)
792 "Create a temporary buffer, and evaluate FORMS there like `progn'.
793 Use unibyte mode for this."
794 `(let (default-enable-multibyte-characters)
795 (with-temp-buffer ,@forms)))
796 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
797 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
799 (defmacro mm-with-multibyte-buffer (&rest forms)
800 "Create a temporary buffer, and evaluate FORMS there like `progn'.
801 Use multibyte mode for this."
802 `(let ((default-enable-multibyte-characters t))
803 (with-temp-buffer ,@forms)))
804 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
805 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
807 (defmacro mm-with-unibyte-current-buffer (&rest forms)
808 "Evaluate FORMS with current buffer temporarily made unibyte.
809 Also bind `default-enable-multibyte-characters' to nil.
810 Equivalent to `progn' in XEmacs"
811 (let ((multibyte (make-symbol "multibyte"))
812 (buffer (make-symbol "buffer")))
814 (let ((,multibyte enable-multibyte-characters)
815 (,buffer (current-buffer)))
817 (let (default-enable-multibyte-characters)
818 (set-buffer-multibyte nil)
821 (set-buffer-multibyte ,multibyte)))
822 (let (default-enable-multibyte-characters)
824 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
825 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
827 (defmacro mm-with-unibyte (&rest forms)
828 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
829 `(let (default-enable-multibyte-characters)
831 (put 'mm-with-unibyte 'lisp-indent-function 0)
832 (put 'mm-with-unibyte 'edebug-form-spec '(body))
834 (defmacro mm-with-multibyte (&rest forms)
835 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
836 `(let ((default-enable-multibyte-characters t))
838 (put 'mm-with-multibyte 'lisp-indent-function 0)
839 (put 'mm-with-multibyte 'edebug-form-spec '(body))
841 (defun mm-find-charset-region (b e)
842 "Return a list of Emacs charsets in the region B to E."
844 ((and (mm-multibyte-p)
845 (fboundp 'find-charset-region))
846 ;; Remove composition since the base charsets have been included.
847 ;; Remove eight-bit-*, treat them as ascii.
848 (let ((css (find-charset-region b e)))
849 (mapcar (lambda (cs) (setq css (delq cs css)))
850 '(composition eight-bit-control eight-bit-graphic
854 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
857 (narrow-to-region b e)
858 (goto-char (point-min))
859 (skip-chars-forward "\0-\177")
864 (and (boundp 'current-language-environment)
865 (car (last (assq 'charset
866 (assoc current-language-environment
867 language-info-alist))))))
868 (if (eq charset 'ascii) (setq charset nil))
871 (car (last (assq mail-parse-charset
872 mm-mime-mule-charset-alist)))))
873 (list 'ascii (or charset 'latin-iso8859-1)))))))))
875 (defun mm-auto-mode-alist ()
876 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
877 (let ((alist auto-mode-alist)
880 (when (listp (cdar alist))
881 (push (car alist) out))
885 (defvar mm-inhibit-file-name-handlers
886 '(jka-compr-handler image-file-handler)
887 "A list of handlers doing (un)compression (etc) thingies.")
889 (defun mm-insert-file-contents (filename &optional visit beg end replace
891 "Like `insert-file-contents', but only reads in the file.
892 A buffer may be modified in several ways after reading into the buffer due
893 to advanced Emacs features, such as file-name-handlers, format decoding,
894 `find-file-hooks', etc.
895 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
896 This function ensures that none of these modifications will take place."
897 (let* ((format-alist nil)
898 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
899 (default-major-mode 'fundamental-mode)
900 (enable-local-variables nil)
901 (after-insert-file-functions nil)
902 (enable-local-eval nil)
903 (inhibit-file-name-operation (if inhibit
904 'insert-file-contents
905 inhibit-file-name-operation))
906 (inhibit-file-name-handlers
908 (append mm-inhibit-file-name-handlers
909 inhibit-file-name-handlers)
910 inhibit-file-name-handlers))
911 (ffh (if (boundp 'find-file-hook)
914 (val (symbol-value ffh)))
917 (insert-file-contents filename visit beg end replace)
920 (defun mm-append-to-file (start end filename &optional codesys inhibit)
921 "Append the contents of the region to the end of file FILENAME.
922 When called from a function, expects three arguments,
923 START, END and FILENAME. START and END are buffer positions
924 saying what text to write.
925 Optional fourth argument specifies the coding system to use when
927 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
928 (let ((coding-system-for-write
929 (or codesys mm-text-coding-system-for-write
930 mm-text-coding-system))
931 (inhibit-file-name-operation (if inhibit
933 inhibit-file-name-operation))
934 (inhibit-file-name-handlers
936 (append mm-inhibit-file-name-handlers
937 inhibit-file-name-handlers)
938 inhibit-file-name-handlers)))
939 (write-region start end filename t 'no-message)
940 (message "Appended to %s" filename)))
942 (defun mm-write-region (start end filename &optional append visit lockname
943 coding-system inhibit)
945 "Like `write-region'.
946 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
947 (let ((coding-system-for-write
948 (or coding-system mm-text-coding-system-for-write
949 mm-text-coding-system))
950 (inhibit-file-name-operation (if inhibit
952 inhibit-file-name-operation))
953 (inhibit-file-name-handlers
955 (append mm-inhibit-file-name-handlers
956 inhibit-file-name-handlers)
957 inhibit-file-name-handlers)))
958 (write-region start end filename append visit lockname)))
960 (defun mm-image-load-path (&optional package)
962 (dolist (path load-path (nreverse result))
965 (setq dir (concat (file-name-directory
966 (directory-file-name path))
967 "etc/images/" (or package "gnus/")))))
969 (push path result))))
971 ;; Fixme: This doesn't look useful where it's used.
972 (if (fboundp 'detect-coding-region)
973 (defun mm-detect-coding-region (start end)
974 "Like `detect-coding-region' except returning the best one."
975 (let ((coding-systems
976 (detect-coding-region start end)))
977 (or (car-safe coding-systems)
979 (defun mm-detect-coding-region (start end)
980 (let ((point (point)))
982 (skip-chars-forward "\0-\177" end)
984 (if (eq (point) end) 'ascii (mm-guess-charset))
985 (goto-char point)))))
987 (if (fboundp 'coding-system-get)
988 (defun mm-detect-mime-charset-region (start end)
989 "Detect MIME charset of the text in the region between START and END."
990 (let ((cs (mm-detect-coding-region start end)))
991 (or (coding-system-get cs :mime-charset)
992 (coding-system-get cs 'mime-charset))))
993 (defun mm-detect-mime-charset-region (start end)
994 "Detect MIME charset of the text in the region between START and END."
995 (let ((cs (mm-detect-coding-region start end)))
999 (unless (fboundp 'coding-system-to-mime-charset)
1000 (defalias 'coding-system-to-mime-charset 'ignore)))
1002 (defun mm-coding-system-to-mime-charset (coding-system)
1003 "Return the MIME charset corresponding to CODING-SYSTEM.
1004 To make this function work with XEmacs, the APEL package is required."
1006 (or (and (fboundp 'coding-system-get)
1007 (or (coding-system-get coding-system :mime-charset)
1008 (coding-system-get coding-system 'mime-charset)))
1009 (and (featurep 'xemacs)
1010 (or (and (fboundp 'coding-system-to-mime-charset)
1011 (not (eq (symbol-function 'coding-system-to-mime-charset)
1013 (and (condition-case nil
1016 (fboundp 'coding-system-to-mime-charset)))
1017 (coding-system-to-mime-charset coding-system)))))
1020 (require 'jka-compr))
1022 (defun mm-decompress-buffer (filename &optional inplace force)
1023 "Decompress buffer's contents, depending on jka-compr.
1024 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
1025 agrees with `jka-compr-compression-info-list', decompression is done.
1026 Signal an error if FORCE is neither nil nor t and compressed data are
1027 not decompressed because `auto-compression-mode' is disabled.
1028 If INPLACE is nil, return decompressed data or nil without modifying
1029 the buffer. Otherwise, replace the buffer's contents with the
1030 decompressed data. The buffer's multibyteness must be turned off."
1033 (prog1 t (require 'jka-compr))
1034 (and (fboundp 'jka-compr-installed-p)
1035 (jka-compr-installed-p))))
1036 (let ((info (jka-compr-get-compression-info filename)))
1038 (unless (or (memq force (list nil t))
1039 (jka-compr-installed-p))
1041 (let ((prog (jka-compr-info-uncompress-program info))
1042 (args (jka-compr-info-uncompress-args info))
1043 (msg (format "%s %s..."
1044 (jka-compr-info-uncompress-message info)
1046 (err-file (jka-compr-make-temp-name))
1047 (cur (current-buffer))
1048 (coding-system-for-read mm-binary-coding-system)
1049 (coding-system-for-write mm-binary-coding-system)
1053 (insert-buffer-substring cur)
1056 (unless (memq (apply 'call-process-region
1057 (point-min) (point-max)
1058 prog t (list t err-file) nil args)
1059 jka-compr-acceptable-retval-list)
1063 (delete "" (split-string
1065 (insert-file-contents err-file)
1071 (format "Error while executing \"%s %s < %s\""
1072 prog (mapconcat 'identity args " ")
1074 (setq retval (buffer-string)))
1076 (setq err-msg (error-message-string err)))))
1077 (when (file-exists-p err-file)
1078 (ignore-errors (jka-compr-delete-temp-file err-file)))
1081 (delete-region (point-min) (point-max))
1084 (message "%s" (or err-msg (concat msg "done")))
1088 (unless (fboundp 'coding-system-name)
1089 (defalias 'coding-system-name 'ignore))
1090 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1091 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1092 (unless (fboundp 'find-operation-coding-system)
1093 (defalias 'find-operation-coding-system 'ignore)))
1095 (defun mm-find-buffer-file-coding-system (&optional filename)
1096 "Find coding system used to decode the contents of the current buffer.
1097 This function looks for the coding system magic cookie or examines the
1098 coding system specified by `file-coding-system-alist' being associated
1099 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1100 gzip, bzip2, etc. are allowed."
1102 (setq filename buffer-file-name))
1104 (let ((decomp (unless ;; No worth to examine charset of tar files.
1107 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1109 (mm-decompress-buffer filename nil t))))
1111 (set-buffer (let (default-enable-multibyte-characters)
1112 (generate-new-buffer " *temp*")))
1114 (setq filename (file-name-sans-extension filename)))
1115 (goto-char (point-min))
1118 ((boundp 'set-auto-coding-function) ;; Emacs
1120 (or (funcall (symbol-value 'set-auto-coding-function)
1121 filename (- (point-max) (point-min)))
1122 (car (find-operation-coding-system 'insert-file-contents
1124 (let (auto-coding-alist)
1126 (funcall (symbol-value 'set-auto-coding-function)
1127 nil (- (point-max) (point-min)))
1129 ((featurep 'file-coding) ;; XEmacs
1130 (let ((case-fold-search t)
1131 (end (point-at-eol))
1134 (and (re-search-forward "-\\*-+[\t ]*" end t)
1136 (setq start (match-end 0))
1137 (re-search-forward "[\t ]*-+\\*-" end t))
1139 (setq end (match-beginning 0))
1141 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1143 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1145 (find-coding-system (setq codesys
1146 (intern (match-string 1))))
1148 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1151 (setq start (match-end 0))
1152 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1154 (setq end (match-beginning 0))
1157 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1159 (find-coding-system (setq codesys
1160 (intern (match-string 1))))
1163 (goto-char (point-min))
1164 (setq case-fold-search nil)
1165 (re-search-forward "^;;;coding system: "
1166 ;;(+ (point-min) 3000) t))
1168 (looking-at "[^\t\n\r ]+")
1170 (setq codesys (intern (match-string 0))))
1174 (find-file-coding-system-for-read-from-filename
1176 (coding-system-name (coding-system-base codesys)))))))
1178 (kill-buffer (current-buffer)))))))
1182 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1183 ;;; mm-util.el ends here