1 ;;; mm-util.el --- Utility functions for Mule and low level things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
29 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile (require 'cl))
35 (if (featurep 'xemacs)
36 (unless (ignore-errors
37 (require 'timer-funcs))
41 (defvar mm-mime-mule-charset-alist )
43 ;; Emulate functions that are not available in every (X)Emacs version.
44 ;; The name of a function is prefixed with mm-, like `mm-char-int' for
45 ;; `char-int' that is a native XEmacs function, not available in Emacs.
46 ;; Gnus programs all should use mm- functions, not the original ones.
50 (let ((nfunc (intern (format "mm-%s" (car elem)))))
51 (if (fboundp (car elem))
52 (defalias nfunc (car elem))
53 (defalias nfunc (cdr elem)))))
54 `(;; `coding-system-list' is not available in XEmacs 21.4 built
55 ;; without the `file-coding' feature.
56 (coding-system-list . ignore)
57 ;; `char-int' is an XEmacs function, not available in Emacs.
59 ;; `coding-system-equal' is an Emacs function, not available in XEmacs.
60 (coding-system-equal . equal)
61 ;; `annotationp' is an XEmacs function, not available in Emacs.
62 (annotationp . ignore)
63 ;; `set-buffer-file-coding-system' is not available in XEmacs 21.4
64 ;; built without the `file-coding' feature.
65 (set-buffer-file-coding-system . ignore)
66 ;; `read-charset' is an Emacs function, not available in XEmacs.
73 (mapcar (lambda (e) (list (symbol-name (car e))))
74 mm-mime-mule-charset-alist)
76 ;; `subst-char-in-string' is not available in XEmacs 21.4.
78 . ,(lambda (from to string &optional inplace)
79 ;; stolen (and renamed) from nnheader.el
80 "Replace characters in STRING from FROM to TO.
81 Unless optional argument INPLACE is non-nil, return a new string."
82 (let ((string (if inplace string (copy-sequence string)))
85 ;; Replace all occurrences of FROM with TO.
87 (when (= (aref string idx) from)
91 ;; `replace-in-string' is an XEmacs function, not available in Emacs.
93 . ,(lambda (string regexp rep &optional literal)
94 "See `replace-regexp-in-string', only the order of args differs."
95 (replace-regexp-in-string regexp rep string nil literal)))
96 ;; `string-as-unibyte' is an Emacs function, not available in XEmacs.
97 (string-as-unibyte . identity)
98 ;; `string-make-unibyte' is an Emacs function, not available in XEmacs.
99 (string-make-unibyte . identity)
100 ;; string-as-multibyte often doesn't really do what you think it does.
102 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
103 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
104 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
105 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
107 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
108 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
109 ;; Better use string-to-multibyte or encode-coding-string.
110 ;; If you really need string-as-multibyte somewhere it's usually
111 ;; because you're using the internal emacs-mule representation (maybe
112 ;; because you're using string-as-unibyte somewhere), which is
113 ;; generally a problem in itself.
114 ;; Here is an approximate equivalence table to help think about it:
115 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
116 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
117 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
118 ;; `string-as-multibyte' is an Emacs function, not available in XEmacs.
119 (string-as-multibyte . identity)
120 ;; `multibyte-string-p' is an Emacs function, not available in XEmacs.
121 (multibyte-string-p . ignore)
122 ;; `insert-byte' is available only in Emacs 23.1 or greater.
123 (insert-byte . insert-char)
124 ;; `multibyte-char-to-unibyte' is an Emacs function, not available
126 (multibyte-char-to-unibyte . identity)
127 ;; `set-buffer-multibyte' is an Emacs function, not available in XEmacs.
128 (set-buffer-multibyte . ignore)
129 ;; `special-display-p' is an Emacs function, not available in XEmacs.
131 . ,(lambda (buffer-name)
132 "Returns non-nil if a buffer named BUFFER-NAME gets a special frame."
133 (and special-display-function
134 (or (and (member buffer-name special-display-buffer-names) t)
135 (cdr (assoc buffer-name special-display-buffer-names))
137 (dolist (elem special-display-regexps)
139 (string-match elem buffer-name)
143 (string-match (car elem) buffer-name)
144 (throw 'return (cdr elem)))))))))
145 ;; `substring-no-properties' is available only in Emacs 22.1 or greater.
146 (substring-no-properties
147 . ,(lambda (string &optional from to)
148 "Return a substring of STRING, without text properties.
149 It starts at index FROM and ending before TO.
150 TO may be nil or omitted; then the substring runs to the end of STRING.
151 If FROM is nil or omitted, the substring starts at the beginning of STRING.
152 If FROM or TO is negative, it counts from the end.
154 With one argument, just copy STRING without its properties."
155 (setq string (substring string (or from 0) to))
156 (set-text-properties 0 (length string) nil string)
159 ;; `decode-coding-string', `encode-coding-string', `decode-coding-region'
160 ;; and `encode-coding-region' are available in Emacs and XEmacs built with
161 ;; the `file-coding' feature, but the XEmacs versions treat nil, that is
162 ;; given as the `coding-system' argument, as the `binary' coding system.
164 (if (featurep 'xemacs)
165 (if (featurep 'file-coding)
167 (defun mm-decode-coding-string (str coding-system)
169 (decode-coding-string str coding-system)
171 (defun mm-encode-coding-string (str coding-system)
173 (encode-coding-string str coding-system)
175 (defun mm-decode-coding-region (start end coding-system)
177 (decode-coding-region start end coding-system)))
178 (defun mm-encode-coding-region (start end coding-system)
180 (encode-coding-region start end coding-system))))
181 (defun mm-decode-coding-string (str coding-system) str)
182 (defun mm-encode-coding-string (str coding-system) str)
183 (defalias 'mm-decode-coding-region 'ignore)
184 (defalias 'mm-encode-coding-region 'ignore))
185 (defalias 'mm-decode-coding-string 'decode-coding-string)
186 (defalias 'mm-encode-coding-string 'encode-coding-string)
187 (defalias 'mm-decode-coding-region 'decode-coding-region)
188 (defalias 'mm-encode-coding-region 'encode-coding-region)))
190 ;; `string-to-multibyte' is available only in Emacs 22.1 or greater.
191 (defalias 'mm-string-to-multibyte
195 ((fboundp 'string-to-multibyte)
196 'string-to-multibyte)
199 "Return a multibyte string with the same individual chars as STRING."
201 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
204 ;; `char-or-char-int-p' is an XEmacs function, not available in Emacs.
206 (defalias 'mm-char-or-char-int-p
208 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
209 ((fboundp 'char-valid-p) 'char-valid-p)
212 ;; Fixme: This seems always to be used to read a MIME charset, so it
213 ;; should be re-named and fixed (in Emacs) to offer completion only on
214 ;; proper charset names (base coding systems which have a
215 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
217 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
218 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
219 ;; Actually, there should be an `mm-coding-system-mime-charset'.
221 (defalias 'mm-read-coding-system
223 ((fboundp 'read-coding-system)
224 (if (and (featurep 'xemacs)
225 (<= (string-to-number emacs-version) 21.1))
226 (lambda (prompt &optional default-coding-system)
227 (read-coding-system prompt))
228 'read-coding-system))
229 (t (lambda (prompt &optional default-coding-system)
230 "Prompt the user for a coding system."
232 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
233 mm-mime-mule-charset-alist)))))))
235 (defvar mm-coding-system-list nil)
236 (defun mm-get-coding-system-list ()
237 "Get the coding system list."
238 (or mm-coding-system-list
239 (setq mm-coding-system-list (mm-coding-system-list))))
241 (defun mm-coding-system-p (cs)
242 "Return non-nil if CS is a symbol naming a coding system.
243 In XEmacs, also return non-nil if CS is a coding system object.
244 If CS is available, return CS itself in Emacs, and return a coding
245 system object in XEmacs."
246 (if (fboundp 'find-coding-system)
247 (and cs (find-coding-system cs))
248 (if (fboundp 'coding-system-p)
249 (when (coding-system-p cs)
252 (car (memq cs (mm-get-coding-system-list))))))
254 (defun mm-codepage-setup (number &optional alias)
255 "Create a coding system cpNUMBER.
256 The coding system is created using `codepage-setup'. If ALIAS is
257 non-nil, an alias is created and added to
258 `mm-charset-synonym-alist'. If ALIAS is a string, it's used as
259 the alias. Else windows-NUMBER is used."
261 (let ((completion-ignore-case t)
262 (candidates (if (fboundp 'cp-supported-codepages)
263 (cp-supported-codepages)
264 ;; Removed in Emacs 23 (unicode), so signal an error:
265 (error "`codepage-setup' not present in this Emacs version."))))
266 (list (completing-read "Setup DOS Codepage: (default 437) " candidates
267 nil t nil nil "437"))))
269 (setq alias (if (stringp alias)
271 (intern (format "windows-%s" number)))))
272 (let* ((cp (intern (format "cp%s" number))))
273 (unless (mm-coding-system-p cp)
274 (if (fboundp 'codepage-setup) ; silence compiler
275 (codepage-setup number)
276 (error "`codepage-setup' not present in this Emacs version.")))
278 ;; Don't add alias if setup of cp failed.
279 (mm-coding-system-p cp))
280 (add-to-list 'mm-charset-synonym-alist (cons alias cp)))))
282 (defvar mm-charset-synonym-alist
284 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
285 ,@(unless (mm-coding-system-p 'x-ctext)
286 '((x-ctext . ctext)))
287 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_ in 8
289 ,@(unless (mm-coding-system-p 'iso-8859-15)
290 '((iso-8859-15 . iso-8859-1)))
291 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
292 ,@(unless (mm-coding-system-p 'big5-hkscs)
293 '((big5-hkscs . big5)))
294 ;; A Microsoft misunderstanding.
295 ,@(when (and (not (mm-coding-system-p 'unicode))
296 (mm-coding-system-p 'utf-16-le))
297 '((unicode . utf-16-le)))
298 ;; A Microsoft misunderstanding.
299 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
300 (if (mm-coding-system-p 'cp949)
301 '((ks_c_5601-1987 . cp949))
302 '((ks_c_5601-1987 . euc-kr))))
303 ;; Windows-31J is Windows Codepage 932.
304 ,@(when (and (not (mm-coding-system-p 'windows-31j))
305 (mm-coding-system-p 'cp932))
306 '((windows-31j . cp932)))
307 ;; Charset name: GBK, Charset aliases: CP936, MS936, windows-936
308 ;; http://www.iana.org/assignments/charset-reg/GBK
309 ;; Emacs 22.1 has cp936, but not gbk, so we alias it:
310 ,@(when (and (not (mm-coding-system-p 'gbk))
311 (mm-coding-system-p 'cp936))
313 ;; UTF8 is a bogus name for UTF-8
314 ,@(when (and (not (mm-coding-system-p 'utf8))
315 (mm-coding-system-p 'utf-8))
317 ;; ISO8859-1 is a bogus name for ISO-8859-1
318 ,@(when (and (not (mm-coding-system-p 'iso8859-1))
319 (mm-coding-system-p 'iso-8859-1))
320 '((iso8859-1 . iso-8859-1)))
321 ;; ISO_8859-1 is a bogus name for ISO-8859-1
322 ,@(when (and (not (mm-coding-system-p 'iso_8859-1))
323 (mm-coding-system-p 'iso-8859-1))
324 '((iso_8859-1 . iso-8859-1)))
326 "A mapping from unknown or invalid charset names to the real charset names.
328 See `mm-codepage-iso-8859-list' and `mm-codepage-ibm-list'.")
330 (defcustom mm-codepage-iso-8859-list
331 (list 1250 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
332 ;; Outlook users in Czech republic. Use this to allow reading of
333 ;; their e-mails. cp1250 should be defined by M-x codepage-setup
335 '(1252 . 1) ;; Windows-1252 is a superset of iso-8859-1 (West
336 ;; Europe). See also `gnus-article-dumbquotes-map'.
337 '(1254 . 9) ;; Windows-1254 is a superset of iso-8859-9 (Turkish).
338 '(1255 . 8));; Windows-1255 is a superset of iso-8859-8 (Hebrew).
339 "A list of Windows codepage numbers and iso-8859 charset numbers.
341 If an element is a number corresponding to a supported windows
342 codepage, appropriate entries to `mm-charset-synonym-alist' are
343 added by `mm-setup-codepage-iso-8859'. An element may also be a
344 cons cell where the car is a codepage number and the cdr is the
345 corresponding number of an iso-8859 charset."
346 :type '(list (set :inline t
347 (const 1250 :tag "Central and East European")
348 (const (1252 . 1) :tag "West European")
349 (const (1254 . 9) :tag "Turkish")
350 (const (1255 . 8) :tag "Hebrew"))
354 (integer :tag "Windows codepage number")
355 (cons (integer :tag "Windows codepage number")
356 (integer :tag "iso-8859 charset number")))))
357 :version "22.1" ;; Gnus 5.10.9
360 (defcustom mm-codepage-ibm-list
361 (list 437 ;; (US etc.)
365 863 ;; (Canadian French)
370 866 ;; (Cyrillic - Russian)
375 ;; In Emacs 23 (unicode), cp... and ibm... are aliases.
376 ;; Cf. http://thread.gmane.org/v9lkng5nwy.fsf@marauder.physik.uni-ulm.de
377 "List of IBM codepage numbers.
379 The codepage mappings slighly differ between IBM and other vendors.
380 See \"ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/IBM/README.TXT\".
382 If an element is a number corresponding to a supported windows
383 codepage, appropriate entries to `mm-charset-synonym-alist' are
384 added by `mm-setup-codepage-ibm'."
385 :type '(list (set :inline t
386 (const 437 :tag "US etc.")
387 (const 860 :tag "Portugal")
388 (const 861 :tag "Iceland")
389 (const 862 :tag "Israel")
390 (const 863 :tag "Canadian French")
391 (const 865 :tag "Nordic")
393 (const 850 :tag "Latin 1")
394 (const 855 :tag "Cyrillic")
395 (const 866 :tag "Cyrillic - Russian")
396 (const 857 :tag "Turkish")
397 (const 864 :tag "Arabic")
398 (const 869 :tag "Greek")
399 (const 874 :tag "Thai"))
402 (integer :tag "Codepage number")))
403 :version "22.1" ;; Gnus 5.10.9
406 (defun mm-setup-codepage-iso-8859 (&optional list)
407 "Add appropriate entries to `mm-charset-synonym-alist'.
408 Unless LIST is given, `mm-codepage-iso-8859-list' is used."
410 (setq list mm-codepage-iso-8859-list))
412 (let (cp windows iso)
414 (setq cp (intern (format "cp%d" (car i)))
415 windows (intern (format "windows-%d" (car i)))
416 iso (intern (format "iso-8859-%d" (cdr i))))
417 (setq cp (intern (format "cp%d" i))
418 windows (intern (format "windows-%d" i))))
419 (unless (mm-coding-system-p windows)
420 (if (mm-coding-system-p cp)
421 (add-to-list 'mm-charset-synonym-alist (cons windows cp))
422 (add-to-list 'mm-charset-synonym-alist (cons windows iso)))))))
424 (defun mm-setup-codepage-ibm (&optional list)
425 "Add appropriate entries to `mm-charset-synonym-alist'.
426 Unless LIST is given, `mm-codepage-ibm-list' is used."
428 (setq list mm-codepage-ibm-list))
429 (dolist (number list)
430 (let ((ibm (intern (format "ibm%d" number)))
431 (cp (intern (format "cp%d" number))))
432 (when (and (not (mm-coding-system-p ibm))
433 (mm-coding-system-p cp))
434 (add-to-list 'mm-charset-synonym-alist (cons ibm cp))))))
437 (mm-setup-codepage-iso-8859)
438 (mm-setup-codepage-ibm)
440 ;; Note: this has to be defined before `mm-charset-to-coding-system'.
441 (defcustom mm-charset-eval-alist
442 (if (featurep 'xemacs)
443 nil ;; I don't know what would be useful for XEmacs.
444 '(;; Emacs 21 offers 1250 1251 1253 1257. Emacs 22 provides autoloads for
445 ;; 1250-1258 (i.e. `mm-codepage-setup' does nothing).
446 (windows-1250 . (mm-codepage-setup 1250 t))
447 (windows-1251 . (mm-codepage-setup 1251 t))
448 (windows-1253 . (mm-codepage-setup 1253 t))
449 (windows-1257 . (mm-codepage-setup 1257 t))))
450 "An alist of (CHARSET . FORM) pairs.
451 If an article is encoded in an unknown CHARSET, FORM is
452 evaluated. This allows to load additional libraries providing
453 charsets on demand. If supported by your Emacs version, you
454 could use `autoload-coding-system' here."
455 :version "22.1" ;; Gnus 5.10.9
456 :type '(list (set :inline t
457 (const (windows-1250 . (mm-codepage-setup 1250 t)))
458 (const (windows-1251 . (mm-codepage-setup 1251 t)))
459 (const (windows-1253 . (mm-codepage-setup 1253 t)))
460 (const (windows-1257 . (mm-codepage-setup 1257 t)))
461 (const (cp850 . (mm-codepage-setup 850 nil))))
464 (cons (symbol :tag "charset")
465 (symbol :tag "form"))))
467 (put 'mm-charset-eval-alist 'risky-local-variable t)
469 (defvar mm-charset-override-alist)
471 ;; Note: this function has to be defined before `mm-charset-override-alist'
472 ;; since it will use this function in order to determine its default value
473 ;; when loading mm-util.elc.
474 (defun mm-charset-to-coding-system (charset &optional lbt
475 allow-override silent)
476 "Return coding-system corresponding to CHARSET.
477 CHARSET is a symbol naming a MIME charset.
478 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
479 used as the line break code type of the coding system.
481 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
482 map undesired charset names to their replacement. This should
483 only be used for decoding, not for encoding.
485 A non-nil value of SILENT means don't issue a warning even if CHARSET
487 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
488 (when (stringp charset)
489 (setq charset (intern (downcase charset))))
491 (setq charset (intern (format "%s-%s" charset lbt))))
495 ;; Running in a non-MULE environment.
496 ((or (null (mm-get-coding-system-list))
497 (not (fboundp 'coding-system-get)))
499 ;; Check override list quite early. Should only used for decoding, not for
502 (let ((cs (cdr (assq charset mm-charset-override-alist))))
503 (and cs (mm-coding-system-p cs) cs))))
505 ((eq charset 'us-ascii)
507 ;; Check to see whether we can handle this charset. (This depends
508 ;; on there being some coding system matching each `mime-charset'
509 ;; property defined, as there should be.)
510 ((and (mm-coding-system-p charset)
511 ;;; Doing this would potentially weed out incorrect charsets.
513 ;;; (eq charset (coding-system-get charset 'mime-charset))
516 ;; Eval expressions from `mm-charset-eval-alist'
517 ((let* ((el (assq charset mm-charset-eval-alist))
524 (condition-case nil (eval form) (error nil))
525 ;; (message "Failed to eval `%s'" form))
526 (mm-coding-system-p cs)
527 (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
529 ;; Translate invalid charsets.
530 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
532 (mm-coding-system-p cs)
534 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
537 ;; Last resort: search the coding system list for entries which
538 ;; have the right mime-charset in case the canonical name isn't
539 ;; defined (though it should be).
541 ;; mm-get-coding-system-list returns a list of cs without lbt.
543 (dolist (c (mm-get-coding-system-list))
545 (eq charset (or (coding-system-get c :mime-charset)
546 (coding-system-get c 'mime-charset))))
548 (unless (or silent cs)
549 ;; Warn the user about unknown charset:
550 (if (fboundp 'gnus-message)
551 (gnus-message 7 "Unknown charset: %s" charset)
552 (message "Unknown charset: %s" charset)))
555 ;; Note: `mm-charset-to-coding-system' has to be defined before this.
556 (defcustom mm-charset-override-alist
557 ;; Note: pairs that cannot be used in the Emacs version currently running
560 (iso-8859-1 . windows-1252)
561 (iso-8859-8 . windows-1255)
562 (iso-8859-9 . windows-1254))
563 "A mapping from undesired charset names to their replacement.
565 You may add pairs like (iso-8859-1 . windows-1252) here,
566 i.e. treat iso-8859-1 as windows-1252. windows-1252 is a
567 superset of iso-8859-1."
574 (mapcar (lambda (pair)
575 (if (mm-charset-to-coding-system (cdr pair)
579 (iso-8859-1 . windows-1252)
580 (iso-8859-8 . windows-1255)
581 (iso-8859-9 . windows-1254)
582 (undecided . windows-1252)))))
583 (val (copy-sequence (default-value 'mm-charset-override-alist)))
586 (push (if (and (prog1
587 (setq pair (assq (caar val) defaults))
588 (setq defaults (delq pair defaults)))
589 (equal (car val) pair))
592 (const :format "(%v" ,(caar val))
593 (symbol :size 3 :format " . %v)\n" ,(cdar val))))
595 (setq val (cdr val)))
597 (push `(const ,(pop defaults)) rest))
600 `(set :inline t :format "%v" ,@(nreverse rest))
601 `(repeat :inline t :tag "Other options"
603 (symbol :size 3 :format "(%v")
604 (symbol :size 3 :format " . %v)\n")))))))
605 ;; Remove pairs that cannot be used in the Emacs version currently
606 ;; running. Note that this section will be evaluated when loading
608 :set (lambda (symbol value)
611 (mapcar (lambda (pair)
612 (if (mm-charset-to-coding-system (cdr pair)
616 :version "22.1" ;; Gnus 5.10.9
619 (defvar mm-binary-coding-system
621 ((mm-coding-system-p 'binary) 'binary)
622 ((mm-coding-system-p 'no-conversion) 'no-conversion)
624 "100% binary coding system.")
626 (defvar mm-text-coding-system
627 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
628 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
629 (and (mm-coding-system-p 'raw-text) 'raw-text))
630 mm-binary-coding-system)
631 "Text-safe coding system (For removing ^M).")
633 (defvar mm-text-coding-system-for-write nil
634 "Text coding system for write.")
636 (defvar mm-auto-save-coding-system
638 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
639 (if (memq system-type '(windows-nt ms-dos ms-windows))
640 (if (mm-coding-system-p 'utf-8-emacs-dos)
641 'utf-8-emacs-dos mm-binary-coding-system)
643 ((mm-coding-system-p 'emacs-mule)
644 (if (memq system-type '(windows-nt ms-dos ms-windows))
645 (if (mm-coding-system-p 'emacs-mule-dos)
646 'emacs-mule-dos mm-binary-coding-system)
648 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
649 (t mm-binary-coding-system))
650 "Coding system of auto save file.")
652 (defvar mm-universal-coding-system mm-auto-save-coding-system
653 "The universal coding system.")
655 ;; Fixme: some of the cars here aren't valid MIME charsets. That
656 ;; should only matter with XEmacs, though.
657 (defvar mm-mime-mule-charset-alist
659 (iso-8859-1 latin-iso8859-1)
660 (iso-8859-2 latin-iso8859-2)
661 (iso-8859-3 latin-iso8859-3)
662 (iso-8859-4 latin-iso8859-4)
663 (iso-8859-5 cyrillic-iso8859-5)
664 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
665 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
666 ;; charset is koi8-r, not iso-8859-5.
667 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
668 (iso-8859-6 arabic-iso8859-6)
669 (iso-8859-7 greek-iso8859-7)
670 (iso-8859-8 hebrew-iso8859-8)
671 (iso-8859-9 latin-iso8859-9)
672 (iso-8859-14 latin-iso8859-14)
673 (iso-8859-15 latin-iso8859-15)
674 (viscii vietnamese-viscii-lower)
675 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
676 (euc-kr korean-ksc5601)
677 (gb2312 chinese-gb2312)
679 (gb18030 gb18030-2-byte
680 gb18030-4-byte-bmp gb18030-4-byte-smp
681 gb18030-4-byte-ext-1 gb18030-4-byte-ext-2)
682 (big5 chinese-big5-1 chinese-big5-2)
684 (thai-tis620 thai-tis620)
685 (windows-1251 cyrillic-iso8859-5)
686 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
687 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
688 latin-jisx0201 japanese-jisx0208-1978
689 chinese-gb2312 japanese-jisx0208
690 korean-ksc5601 japanese-jisx0212)
691 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
692 latin-jisx0201 japanese-jisx0208-1978
693 chinese-gb2312 japanese-jisx0208
694 korean-ksc5601 japanese-jisx0212
695 chinese-cns11643-1 chinese-cns11643-2)
696 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
697 cyrillic-iso8859-5 greek-iso8859-7
698 latin-jisx0201 japanese-jisx0208-1978
699 chinese-gb2312 japanese-jisx0208
700 korean-ksc5601 japanese-jisx0212
701 chinese-cns11643-1 chinese-cns11643-2
702 chinese-cns11643-3 chinese-cns11643-4
703 chinese-cns11643-5 chinese-cns11643-6
705 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
706 japanese-jisx0213-1 japanese-jisx0213-2)
707 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
708 ,(cond ((fboundp 'unicode-precedence-list)
709 (cons 'utf-8 (delq 'ascii (mapcar 'charset-name
710 (unicode-precedence-list)))))
711 ((or (not (fboundp 'charsetp)) ;; non-Mule case
712 (charsetp 'unicode-a)
713 (not (mm-coding-system-p 'mule-utf-8)))
714 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
715 (t ;; If we have utf-8 we're in Mule 5+.
718 (coding-system-get 'mule-utf-8 'safe-charsets))))))
719 "Alist of MIME-charset/MULE-charsets.")
721 (defun mm-enrich-utf-8-by-mule-ucs ()
722 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
723 This function will run when the `un-define' module is loaded under
724 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
725 with Mule charsets. It is completely useless for Emacs."
726 (when (boundp 'unicode-basic-translation-charset-order-list)
732 'unicode-basic-translation-charset-order-list))))
733 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
736 (setq mm-mime-mule-charset-alist
737 (nconc mm-mime-mule-charset-alist
738 (list (cons 'utf-8 val))))))
741 ;; Correct by construction, but should be unnecessary for Emacs:
742 (if (featurep 'xemacs)
743 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
744 (when (and (fboundp 'coding-system-list)
745 (fboundp 'sort-coding-systems))
746 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
750 mime (or (coding-system-get cs :mime-charset); Emacs 23 (unicode)
751 (coding-system-get cs 'mime-charset)))
753 (not (eq t (setq mule
754 (coding-system-get cs 'safe-charsets))))
755 (not (assq mime alist)))
756 (push (cons mime (delq 'ascii mule)) alist)))
757 (setq mm-mime-mule-charset-alist (nreverse alist)))))
759 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
760 "A list of special charsets.
761 Valid elements include:
762 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
763 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
766 (defvar mm-iso-8859-15-compatible
767 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
768 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
769 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
771 (defvar mm-iso-8859-x-to-15-table
772 (and (fboundp 'coding-system-p)
773 (mm-coding-system-p 'iso-8859-15)
776 (if (mm-coding-system-p (car cs))
777 (let ((c (string-to-char
778 (decode-coding-string "\341" (car cs)))))
779 (cons (char-charset c)
782 (decode-coding-string "\341" 'iso-8859-15)) c)
783 (string-to-list (decode-coding-string (car (cdr cs))
786 mm-iso-8859-15-compatible))
787 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
789 (defcustom mm-coding-system-priorities
790 (if (boundp 'current-language-environment)
791 (let ((lang (symbol-value 'current-language-environment)))
792 (cond ((string= lang "Japanese")
793 ;; Japanese users prefer iso-2022-jp to euc-japan or
794 ;; shift_jis, however iso-8859-1 should be used when
795 ;; there are only ASCII text and Latin-1 characters.
796 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
797 "Preferred coding systems for encoding outgoing messages.
799 More than one suitable coding system may be found for some text.
800 By default, the coding system with the highest priority is used
801 to encode outgoing messages (see `sort-coding-systems'). If this
802 variable is set, it overrides the default priority."
804 :type '(repeat (symbol :tag "Coding system"))
808 (defvar mm-use-find-coding-systems-region
809 (fboundp 'find-coding-systems-region)
810 "Use `find-coding-systems-region' to find proper coding systems.
812 Setting it to nil is useful on Emacsen supporting Unicode if sending
813 mail with multiple parts is preferred to sending a Unicode one.")
815 ;;; Internal variables:
819 (defun mm-mule-charset-to-mime-charset (charset)
820 "Return the MIME charset corresponding to the given Mule CHARSET."
821 (if (and (fboundp 'find-coding-systems-for-charsets)
822 (fboundp 'sort-coding-systems))
823 (let ((css (sort (sort-coding-systems
824 (find-coding-systems-for-charsets (list charset)))
825 'mm-sort-coding-systems-predicate))
827 (while (and (not mime)
829 (when (setq cs (pop css))
830 (setq mime (or (coding-system-get cs :mime-charset)
831 (coding-system-get cs 'mime-charset)))))
833 (let ((alist (mapcar (lambda (cs)
834 (assq cs mm-mime-mule-charset-alist))
835 (sort (mapcar 'car mm-mime-mule-charset-alist)
836 'mm-sort-coding-systems-predicate)))
839 (when (memq charset (cdar alist))
840 (setq out (caar alist)
846 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
847 (boundp 'default-enable-multibyte-characters)
848 default-enable-multibyte-characters
849 (fboundp 'set-buffer-multibyte))
850 "True in Emacs with Mule.")
853 (defun mm-enable-multibyte ()
854 "Set the multibyte flag of the current buffer.
855 Only do this if the default value of `enable-multibyte-characters' is
856 non-nil. This is a no-op in XEmacs."
857 (set-buffer-multibyte 'to))
858 (defalias 'mm-enable-multibyte 'ignore))
861 (defun mm-disable-multibyte ()
862 "Unset the multibyte flag of in the current buffer.
863 This is a no-op in XEmacs."
864 (set-buffer-multibyte nil))
865 (defalias 'mm-disable-multibyte 'ignore)))
867 (defun mm-preferred-coding-system (charset)
868 ;; A typo in some Emacs versions.
869 (or (get-charset-property charset 'preferred-coding-system)
870 (get-charset-property charset 'prefered-coding-system)))
872 ;; Mule charsets shouldn't be used.
873 (defsubst mm-guess-charset ()
874 "Guess Mule charset from the language environment."
876 mail-parse-mule-charset ;; cached mule-charset
878 (setq mail-parse-mule-charset
879 (and (boundp 'current-language-environment)
882 (assoc current-language-environment
883 language-info-alist))))))
884 (if (or (not mail-parse-mule-charset)
885 (eq mail-parse-mule-charset 'ascii))
886 (setq mail-parse-mule-charset
887 (or (car (last (assq mail-parse-charset
888 mm-mime-mule-charset-alist)))
891 mail-parse-mule-charset)))
893 (defun mm-charset-after (&optional pos)
894 "Return charset of a character in current buffer at position POS.
895 If POS is nil, it defauls to the current point.
896 If POS is out of range, the value is nil.
897 If the charset is `composition', return the actual one."
898 (let ((char (char-after pos)) charset)
899 (if (< (mm-char-int char) 128)
900 (setq charset 'ascii)
901 ;; charset-after is fake in some Emacsen.
902 (setq charset (and (fboundp 'char-charset) (char-charset char)))
903 (if (eq charset 'composition) ; Mule 4
904 (let ((p (or pos (point))))
905 (cadr (find-charset-region p (1+ p))))
906 (if (and charset (not (memq charset '(ascii eight-bit-control
907 eight-bit-graphic))))
909 (mm-guess-charset))))))
911 (defun mm-mime-charset (charset)
912 "Return the MIME charset corresponding to the given Mule CHARSET."
913 (if (eq charset 'unknown)
914 (error "The message contains non-printable characters, please use attachment"))
915 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
916 ;; This exists in Emacs 20.
918 (and (mm-preferred-coding-system charset)
919 (or (coding-system-get
920 (mm-preferred-coding-system charset) :mime-charset)
922 (mm-preferred-coding-system charset) 'mime-charset)))
923 (and (eq charset 'ascii)
925 (mm-preferred-coding-system charset)
926 (mm-mule-charset-to-mime-charset charset))
927 ;; This is for XEmacs.
928 (mm-mule-charset-to-mime-charset charset)))
930 (if (fboundp 'delete-dups)
931 (defalias 'mm-delete-duplicates 'delete-dups)
932 (defun mm-delete-duplicates (list)
933 "Destructively remove `equal' duplicates from LIST.
934 Store the result in LIST and return it. LIST must be a proper list.
935 Of several `equal' occurrences of an element in LIST, the first
938 This is a compatibility function for Emacsen without `delete-dups'."
939 ;; Code from `subr.el' in Emacs 22:
942 (setcdr tail (delete (car tail) (cdr tail)))
943 (setq tail (cdr tail))))
946 ;; Fixme: This is used in places when it should be testing the
947 ;; default multibyteness. See mm-default-multibyte-p.
949 (if (and (not (featurep 'xemacs))
950 (boundp 'enable-multibyte-characters))
951 (defun mm-multibyte-p ()
952 "Non-nil if multibyte is enabled in the current buffer."
953 enable-multibyte-characters)
954 (defun mm-multibyte-p () (featurep 'mule))))
956 (defun mm-default-multibyte-p ()
957 "Return non-nil if the session is multibyte.
958 This affects whether coding conversion should be attempted generally."
960 (if (boundp 'default-enable-multibyte-characters)
961 default-enable-multibyte-characters
964 (defun mm-iso-8859-x-to-15-region (&optional b e)
965 (if (fboundp 'char-charset)
966 (let (charset item c inconvertible)
968 (if e (narrow-to-region b e))
969 (goto-char (point-min))
970 (skip-chars-forward "\0-\177")
973 ((not (setq item (assq (char-charset (setq c (char-after)))
974 mm-iso-8859-x-to-15-table)))
976 ((memq c (cdr (cdr item)))
977 (setq inconvertible t)
980 (insert-before-markers (prog1 (+ c (car (cdr item)))
982 (skip-chars-forward "\0-\177")))
983 (not inconvertible))))
985 (defun mm-sort-coding-systems-predicate (a b)
988 ;; Note: invalid entries are dropped silently
989 (and (setq cs (mm-coding-system-p cs))
990 (coding-system-base cs)))
991 mm-coding-system-priorities)))
992 (and (setq a (mm-coding-system-p a))
993 (if (setq b (mm-coding-system-p b))
994 (> (length (memq (coding-system-base a) priorities))
995 (length (memq (coding-system-base b) priorities)))
999 (autoload 'latin-unity-massage-name "latin-unity")
1000 (autoload 'latin-unity-maybe-remap "latin-unity")
1001 (autoload 'latin-unity-representations-feasible-region "latin-unity")
1002 (autoload 'latin-unity-representations-present-region "latin-unity"))
1004 (defvar latin-unity-coding-systems)
1005 (defvar latin-unity-ucs-list)
1007 (defun mm-xemacs-find-mime-charset-1 (begin end)
1008 "Determine which MIME charset to use to send region as message.
1009 This uses the XEmacs-specific latin-unity package to better handle the
1010 case where identical characters from diverse ISO-8859-? character sets
1011 can be encoded using a single one of the corresponding coding systems.
1013 It treats `mm-coding-system-priorities' as the list of preferred
1014 coding systems; a useful example setting for this list in Western
1015 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
1016 to the very standard Latin 1 coding system, and only move to coding
1017 systems that are less supported as is necessary to encode the
1018 characters that exist in the buffer.
1020 Latin Unity doesn't know about those non-ASCII Roman characters that
1021 are available in various East Asian character sets. As such, its
1022 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
1023 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
1024 But this is very much a corner case, so don't worry about it."
1025 (let ((systems mm-coding-system-priorities) csets psets curset)
1027 ;; Load the Latin Unity library, if available.
1028 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
1029 (require 'latin-unity))
1031 ;; Now, can we use it?
1032 (if (featurep 'latin-unity)
1034 (setq csets (latin-unity-representations-feasible-region begin end)
1035 psets (latin-unity-representations-present-region begin end))
1039 ;; Pass back the first coding system in the preferred list
1040 ;; that can encode the whole region.
1041 (dolist (curset systems)
1042 (setq curset (latin-unity-massage-name 'buffer-default curset))
1044 ;; If the coding system is a universal coding system, then
1045 ;; it can certainly encode all the characters in the region.
1046 (if (memq curset latin-unity-ucs-list)
1047 (throw 'done (list curset)))
1049 ;; If a coding system isn't universal, and isn't in
1050 ;; the list that latin unity knows about, we can't
1051 ;; decide whether to use it here. Leave that until later
1052 ;; in `mm-find-mime-charset-region' function, whence we
1053 ;; have been called.
1054 (unless (memq curset latin-unity-coding-systems)
1057 ;; Right, we know about this coding system, and it may
1058 ;; conceivably be able to encode all the characters in
1060 (if (latin-unity-maybe-remap begin end curset csets psets t)
1061 (throw 'done (list curset))))
1063 ;; Can't encode using anything from the
1064 ;; `mm-coding-system-priorities' list.
1065 ;; Leave `mm-find-mime-charset' to do most of the work.
1068 ;; Right, latin unity isn't available; let `mm-find-charset-region'
1069 ;; take its default action, which equally applies to GNU Emacs.
1072 (defmacro mm-xemacs-find-mime-charset (begin end)
1073 (when (featurep 'xemacs)
1074 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
1076 (declare-function mm-delete-duplicates "mm-util" (list))
1078 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
1079 "Return the MIME charsets needed to encode the region between B and E.
1080 nil means ASCII, a single-element list represents an appropriate MIME
1081 charset, and a longer list means no appropriate charset."
1083 ;; The return possibilities of this function are a mess...
1084 (or (and (mm-multibyte-p)
1085 mm-use-find-coding-systems-region
1086 ;; Find the mime-charset of the most preferred coding
1087 ;; system that has one.
1088 (let ((systems (find-coding-systems-region b e)))
1089 (when mm-coding-system-priorities
1091 (sort systems 'mm-sort-coding-systems-predicate)))
1092 (setq systems (delq 'compound-text systems))
1093 (unless (equal systems '(undecided))
1095 (let* ((head (pop systems))
1096 (cs (or (coding-system-get head :mime-charset)
1097 (coding-system-get head 'mime-charset))))
1098 ;; The mime-charset (`x-ctext') of
1099 ;; `compound-text' is not in the IANA list. We
1100 ;; shouldn't normally use anything here with a
1101 ;; mime-charset having an `x-' prefix.
1102 ;; Fixme: Allow this to be overridden, since
1103 ;; there is existing use of x-ctext.
1104 ;; Also people apparently need the coding system
1105 ;; `iso-2022-jp-3' (which Mule-UCS defines with
1106 ;; mime-charset, though it's not valid).
1108 (not (string-match "^[Xx]-" (symbol-name cs)))
1109 ;; UTF-16 of any variety is invalid for
1110 ;; text parts and, unfortunately, has
1111 ;; mime-charset defined both in Mule-UCS
1112 ;; and versions of Emacs. (The name
1113 ;; might be `mule-utf-16...' or
1115 (not (string-match "utf-16" (symbol-name cs))))
1117 charsets (list cs))))))
1119 ;; If we're XEmacs, and some coding system is appropriate,
1120 ;; mm-xemacs-find-mime-charset will return an appropriate list.
1121 ;; Otherwise, we'll get nil, and the next setq will get invoked.
1122 (setq charsets (mm-xemacs-find-mime-charset b e))
1124 ;; Fixme: won't work for unibyte Emacs 23:
1126 ;; We're not multibyte, or a single coding system won't cover it.
1128 (mm-delete-duplicates
1129 (mapcar 'mm-mime-charset
1131 (mm-find-charset-region b e))))))
1132 (if (and (> (length charsets) 1)
1133 (memq 'iso-8859-15 charsets)
1134 (memq 'iso-8859-15 hack-charsets)
1135 (save-excursion (mm-iso-8859-x-to-15-region b e)))
1136 (dolist (x mm-iso-8859-15-compatible)
1137 (setq charsets (delq (car x) charsets))))
1138 (if (and (memq 'iso-2022-jp-2 charsets)
1139 (memq 'iso-2022-jp-2 hack-charsets))
1140 (setq charsets (delq 'iso-2022-jp charsets)))
1141 ;; Attempt to reduce the number of charsets if utf-8 is available.
1142 (if (and (featurep 'xemacs)
1143 (> (length charsets) 1)
1144 (mm-coding-system-p 'utf-8))
1145 (let ((mm-coding-system-priorities
1146 (cons 'utf-8 mm-coding-system-priorities)))
1148 (mm-delete-duplicates
1149 (mapcar 'mm-mime-charset
1151 (mm-find-charset-region b e)))))))
1154 (defmacro mm-with-unibyte-buffer (&rest forms)
1155 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1156 Use unibyte mode for this."
1158 (mm-disable-multibyte)
1160 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
1161 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
1163 (defmacro mm-with-multibyte-buffer (&rest forms)
1164 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1165 Use multibyte mode for this."
1167 (mm-enable-multibyte)
1169 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
1170 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
1172 (defmacro mm-with-unibyte-current-buffer (&rest forms)
1173 "Evaluate FORMS with current buffer temporarily made unibyte.
1174 Also bind `default-enable-multibyte-characters' to nil.
1175 Equivalent to `progn' in XEmacs
1177 NOTE: Use this macro with caution in multibyte buffers (it is not
1178 worth using this macro in unibyte buffers of course). Use of
1179 `(set-buffer-multibyte t)', which is run finally, is generally
1180 harmful since it is likely to modify existing data in the buffer.
1181 For instance, it converts \"\\300\\255\" into \"\\255\" in
1182 Emacs 23 (unicode)."
1183 (let ((multibyte (make-symbol "multibyte"))
1184 (buffer (make-symbol "buffer")))
1186 (let ((,multibyte enable-multibyte-characters)
1187 (,buffer (current-buffer)))
1189 (let (default-enable-multibyte-characters)
1190 (set-buffer-multibyte nil)
1192 (set-buffer ,buffer)
1193 (set-buffer-multibyte ,multibyte)))
1194 (let (default-enable-multibyte-characters)
1196 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
1197 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
1199 (defun mm-find-charset-region (b e)
1200 "Return a list of Emacs charsets in the region B to E."
1202 ((and (mm-multibyte-p)
1203 (fboundp 'find-charset-region))
1204 ;; Remove composition since the base charsets have been included.
1205 ;; Remove eight-bit-*, treat them as ascii.
1206 (let ((css (find-charset-region b e)))
1208 '(composition eight-bit-control eight-bit-graphic control-1)
1210 (setq css (delq cs css)))))
1212 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
1215 (narrow-to-region b e)
1216 (goto-char (point-min))
1217 (skip-chars-forward "\0-\177")
1222 (and (boundp 'current-language-environment)
1223 (car (last (assq 'charset
1224 (assoc current-language-environment
1225 language-info-alist))))))
1226 (if (eq charset 'ascii) (setq charset nil))
1229 (car (last (assq mail-parse-charset
1230 mm-mime-mule-charset-alist)))))
1231 (list 'ascii (or charset 'latin-iso8859-1)))))))))
1233 (defun mm-auto-mode-alist ()
1234 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1235 (let ((alist auto-mode-alist)
1238 (when (listp (cdar alist))
1239 (push (car alist) out))
1243 (defvar mm-inhibit-file-name-handlers
1244 '(jka-compr-handler image-file-handler epa-file-handler)
1245 "A list of handlers doing (un)compression (etc) thingies.")
1247 (defun mm-insert-file-contents (filename &optional visit beg end replace
1249 "Like `insert-file-contents', but only reads in the file.
1250 A buffer may be modified in several ways after reading into the buffer due
1251 to advanced Emacs features, such as file-name-handlers, format decoding,
1252 `find-file-hooks', etc.
1253 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
1254 This function ensures that none of these modifications will take place."
1255 (let* ((format-alist nil)
1256 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
1257 (default-major-mode 'fundamental-mode)
1258 (enable-local-variables nil)
1259 (after-insert-file-functions nil)
1260 (enable-local-eval nil)
1261 (inhibit-file-name-operation (if inhibit
1262 'insert-file-contents
1263 inhibit-file-name-operation))
1264 (inhibit-file-name-handlers
1266 (append mm-inhibit-file-name-handlers
1267 inhibit-file-name-handlers)
1268 inhibit-file-name-handlers))
1269 (ffh (if (boundp 'find-file-hook)
1272 (val (symbol-value ffh)))
1275 (insert-file-contents filename visit beg end replace)
1278 (defun mm-append-to-file (start end filename &optional codesys inhibit)
1279 "Append the contents of the region to the end of file FILENAME.
1280 When called from a function, expects three arguments,
1281 START, END and FILENAME. START and END are buffer positions
1282 saying what text to write.
1283 Optional fourth argument specifies the coding system to use when
1285 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1286 (let ((coding-system-for-write
1287 (or codesys mm-text-coding-system-for-write
1288 mm-text-coding-system))
1289 (inhibit-file-name-operation (if inhibit
1291 inhibit-file-name-operation))
1292 (inhibit-file-name-handlers
1294 (append mm-inhibit-file-name-handlers
1295 inhibit-file-name-handlers)
1296 inhibit-file-name-handlers)))
1297 (write-region start end filename t 'no-message)
1298 (message "Appended to %s" filename)))
1300 (defun mm-write-region (start end filename &optional append visit lockname
1301 coding-system inhibit)
1303 "Like `write-region'.
1304 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1305 (let ((coding-system-for-write
1306 (or coding-system mm-text-coding-system-for-write
1307 mm-text-coding-system))
1308 (inhibit-file-name-operation (if inhibit
1310 inhibit-file-name-operation))
1311 (inhibit-file-name-handlers
1313 (append mm-inhibit-file-name-handlers
1314 inhibit-file-name-handlers)
1315 inhibit-file-name-handlers)))
1316 (write-region start end filename append visit lockname)))
1318 (autoload 'gmm-write-region "gmm-utils")
1320 ;; It is not a MIME function, but some MIME functions use it.
1321 (if (and (fboundp 'make-temp-file)
1323 (let ((def (symbol-function 'make-temp-file)))
1324 (and (byte-code-function-p def)
1325 (setq def (if (fboundp 'compiled-function-arglist)
1327 (eval (list 'compiled-function-arglist def))
1330 (eq (nth 3 def) 'suffix)))))
1331 (defalias 'mm-make-temp-file 'make-temp-file)
1332 ;; Stolen (and modified for XEmacs) from Emacs 22.
1333 (defun mm-make-temp-file (prefix &optional dir-flag suffix)
1334 "Create a temporary file.
1335 The returned file name (created by appending some random characters at the end
1336 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1337 is guaranteed to point to a newly created empty file.
1338 You can then use `write-region' to write new data into the file.
1340 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1342 If SUFFIX is non-nil, add that at the end of the file name."
1343 (let ((umask (default-file-modes))
1347 ;; Create temp files with strict access rights. It's easy to
1348 ;; loosen them later, whereas it's impossible to close the
1349 ;; time-window of loose permissions otherwise.
1350 (set-default-file-modes 448)
1351 (while (condition-case err
1357 (if (fboundp 'temp-directory)
1360 temporary-file-directory))))
1362 (setq file (concat file suffix)))
1364 (make-directory file)
1365 ;; NOTE: This is unsafe if Emacs 20
1366 ;; users and XEmacs users don't use
1367 ;; a secure temp directory.
1368 (gmm-write-region "" nil file nil 'silent
1371 (file-already-exists t)
1372 ;; The XEmacs version of `make-directory' issues
1374 (file-error (or (and (featurep 'xemacs)
1375 (file-exists-p file))
1376 (signal (car err) (cdr err)))))
1377 ;; the file was somehow created by someone else between
1378 ;; `make-temp-name' and `write-region', let's try again.
1382 (set-default-file-modes umask)))))
1384 (defun mm-image-load-path (&optional package)
1386 (dolist (path load-path (nreverse result))
1389 (setq dir (concat (file-name-directory
1390 (directory-file-name path))
1391 "etc/images/" (or package "gnus/")))))
1393 (push path result))))
1395 ;; Fixme: This doesn't look useful where it's used.
1396 (if (fboundp 'detect-coding-region)
1397 (defun mm-detect-coding-region (start end)
1398 "Like `detect-coding-region' except returning the best one."
1399 (let ((coding-systems
1400 (detect-coding-region start end)))
1401 (or (car-safe coding-systems)
1403 (defun mm-detect-coding-region (start end)
1404 (let ((point (point)))
1406 (skip-chars-forward "\0-\177" end)
1408 (if (eq (point) end) 'ascii (mm-guess-charset))
1409 (goto-char point)))))
1411 (declare-function mm-detect-coding-region "mm-util" (start end))
1413 (if (fboundp 'coding-system-get)
1414 (defun mm-detect-mime-charset-region (start end)
1415 "Detect MIME charset of the text in the region between START and END."
1416 (let ((cs (mm-detect-coding-region start end)))
1417 (or (coding-system-get cs :mime-charset)
1418 (coding-system-get cs 'mime-charset))))
1419 (defun mm-detect-mime-charset-region (start end)
1420 "Detect MIME charset of the text in the region between START and END."
1421 (let ((cs (mm-detect-coding-region start end)))
1425 (unless (fboundp 'coding-system-to-mime-charset)
1426 (defalias 'coding-system-to-mime-charset 'ignore)))
1428 (defun mm-coding-system-to-mime-charset (coding-system)
1429 "Return the MIME charset corresponding to CODING-SYSTEM.
1430 To make this function work with XEmacs, the APEL package is required."
1432 (or (and (fboundp 'coding-system-get)
1433 (or (coding-system-get coding-system :mime-charset)
1434 (coding-system-get coding-system 'mime-charset)))
1435 (and (featurep 'xemacs)
1436 (or (and (fboundp 'coding-system-to-mime-charset)
1437 (not (eq (symbol-function 'coding-system-to-mime-charset)
1439 (and (condition-case nil
1442 (fboundp 'coding-system-to-mime-charset)))
1443 (coding-system-to-mime-charset coding-system)))))
1446 (require 'jka-compr))
1448 (defun mm-decompress-buffer (filename &optional inplace force)
1449 "Decompress buffer's contents, depending on jka-compr.
1450 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
1451 agrees with `jka-compr-compression-info-list', decompression is done.
1452 Signal an error if FORCE is neither nil nor t and compressed data are
1453 not decompressed because `auto-compression-mode' is disabled.
1454 If INPLACE is nil, return decompressed data or nil without modifying
1455 the buffer. Otherwise, replace the buffer's contents with the
1456 decompressed data. The buffer's multibyteness must be turned off."
1459 (prog1 t (require 'jka-compr))
1460 (and (fboundp 'jka-compr-installed-p)
1461 (jka-compr-installed-p))))
1462 (let ((info (jka-compr-get-compression-info filename)))
1464 (unless (or (memq force (list nil t))
1465 (jka-compr-installed-p))
1467 (let ((prog (jka-compr-info-uncompress-program info))
1468 (args (jka-compr-info-uncompress-args info))
1469 (msg (format "%s %s..."
1470 (jka-compr-info-uncompress-message info)
1472 (err-file (jka-compr-make-temp-name))
1473 (cur (current-buffer))
1474 (coding-system-for-read mm-binary-coding-system)
1475 (coding-system-for-write mm-binary-coding-system)
1478 (mm-with-unibyte-buffer
1479 (insert-buffer-substring cur)
1482 (unless (memq (apply 'call-process-region
1483 (point-min) (point-max)
1484 prog t (list t err-file) nil args)
1485 jka-compr-acceptable-retval-list)
1489 (delete "" (split-string
1491 (insert-file-contents err-file)
1497 (format "Error while executing \"%s %s < %s\""
1498 prog (mapconcat 'identity args " ")
1500 (setq retval (buffer-string)))
1502 (setq err-msg (error-message-string err)))))
1503 (when (file-exists-p err-file)
1504 (ignore-errors (jka-compr-delete-temp-file err-file)))
1507 (delete-region (point-min) (point-max))
1510 (message "%s" (or err-msg (concat msg "done")))
1514 (unless (fboundp 'coding-system-name)
1515 (defalias 'coding-system-name 'ignore))
1516 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1517 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1518 (unless (fboundp 'find-operation-coding-system)
1519 (defalias 'find-operation-coding-system 'ignore)))
1521 (defun mm-find-buffer-file-coding-system (&optional filename)
1522 "Find coding system used to decode the contents of the current buffer.
1523 This function looks for the coding system magic cookie or examines the
1524 coding system specified by `file-coding-system-alist' being associated
1525 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1526 gzip, bzip2, etc. are allowed."
1528 (setq filename buffer-file-name))
1530 (let ((decomp (unless ;; No worth to examine charset of tar files.
1533 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1535 (mm-decompress-buffer filename nil t))))
1537 (set-buffer (let (default-enable-multibyte-characters)
1538 (generate-new-buffer " *temp*")))
1540 (setq filename (file-name-sans-extension filename)))
1541 (goto-char (point-min))
1544 ((boundp 'set-auto-coding-function) ;; Emacs
1546 (or (funcall (symbol-value 'set-auto-coding-function)
1547 filename (- (point-max) (point-min)))
1548 (car (find-operation-coding-system 'insert-file-contents
1550 (let (auto-coding-alist)
1552 (funcall (symbol-value 'set-auto-coding-function)
1553 nil (- (point-max) (point-min)))
1555 ((and (featurep 'xemacs) (featurep 'file-coding)) ;; XEmacs
1556 (let ((case-fold-search t)
1557 (end (point-at-eol))
1560 (and (re-search-forward "-\\*-+[\t ]*" end t)
1562 (setq start (match-end 0))
1563 (re-search-forward "[\t ]*-+\\*-" end t))
1565 (setq end (match-beginning 0))
1567 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1569 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1571 (find-coding-system (setq codesys
1572 (intern (match-string 1))))
1574 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1577 (setq start (match-end 0))
1578 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1580 (setq end (match-beginning 0))
1583 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1585 (find-coding-system (setq codesys
1586 (intern (match-string 1))))
1589 (goto-char (point-min))
1590 (setq case-fold-search nil)
1591 (re-search-forward "^;;;coding system: "
1592 ;;(+ (point-min) 3000) t))
1594 (looking-at "[^\t\n\r ]+")
1596 (setq codesys (intern (match-string 0))))
1600 (find-file-coding-system-for-read-from-filename
1602 (coding-system-name (coding-system-base codesys)))))))
1604 (kill-buffer (current-buffer)))))))
1608 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1609 ;;; mm-util.el ends here