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 )
46 (let ((nfunc (intern (format "mm-%s" (car elem)))))
47 (if (fboundp (car elem))
48 (defalias nfunc (car elem))
49 (defalias nfunc (cdr elem)))))
50 `((coding-system-list . ignore)
52 (coding-system-equal . equal)
53 (annotationp . ignore)
54 (set-buffer-file-coding-system . ignore)
61 (mapcar (lambda (e) (list (symbol-name (car e))))
62 mm-mime-mule-charset-alist)
65 . ,(lambda (from to string &optional inplace)
66 ;; stolen (and renamed) from nnheader.el
67 "Replace characters in STRING from FROM to TO.
68 Unless optional argument INPLACE is non-nil, return a new string."
69 (let ((string (if inplace string (copy-sequence string)))
72 ;; Replace all occurrences of FROM with TO.
74 (when (= (aref string idx) from)
79 . ,(lambda (string regexp rep &optional literal)
80 "See `replace-regexp-in-string', only the order of args differs."
81 (replace-regexp-in-string regexp rep string nil literal)))
82 (string-as-unibyte . identity)
83 (string-make-unibyte . identity)
84 ;; string-as-multibyte often doesn't really do what you think it does.
86 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
87 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
88 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
89 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
91 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
92 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
93 ;; Better use string-to-multibyte or encode-coding-string.
94 ;; If you really need string-as-multibyte somewhere it's usually
95 ;; because you're using the internal emacs-mule representation (maybe
96 ;; because you're using string-as-unibyte somewhere), which is
97 ;; generally a problem in itself.
98 ;; Here is an approximate equivalence table to help think about it:
99 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
100 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
101 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
102 (string-as-multibyte . identity)
103 (multibyte-string-p . ignore)
104 (insert-byte . insert-char)
105 (multibyte-char-to-unibyte . identity)
106 (set-buffer-multibyte . ignore)
108 . ,(lambda (buffer-name)
109 "Returns non-nil if a buffer named BUFFER-NAME gets a special frame."
110 (and special-display-function
111 (or (and (member buffer-name special-display-buffer-names) t)
112 (cdr (assoc buffer-name special-display-buffer-names))
114 (dolist (elem special-display-regexps)
116 (string-match elem buffer-name)
120 (string-match (car elem) buffer-name)
121 (throw 'return (cdr elem)))))))))
122 (substring-no-properties
123 . ,(lambda (string &optional from to)
124 "Return a substring of STRING, without text properties.
125 It starts at index FROM and ending before TO.
126 TO may be nil or omitted; then the substring runs to the end of STRING.
127 If FROM is nil or omitted, the substring starts at the beginning of STRING.
128 If FROM or TO is negative, it counts from the end.
130 With one argument, just copy STRING without its properties."
131 (setq string (substring string (or from 0) to))
132 (set-text-properties 0 (length string) nil string)
136 (if (featurep 'xemacs)
137 (if (featurep 'file-coding)
138 ;; Don't modify string if CODING-SYSTEM is nil.
140 (defun mm-decode-coding-string (str coding-system)
142 (decode-coding-string str coding-system)
144 (defun mm-encode-coding-string (str coding-system)
146 (encode-coding-string str coding-system)
148 (defun mm-decode-coding-region (start end coding-system)
150 (decode-coding-region start end coding-system)))
151 (defun mm-encode-coding-region (start end coding-system)
153 (encode-coding-region start end coding-system))))
154 (defun mm-decode-coding-string (str coding-system) str)
155 (defun mm-encode-coding-string (str coding-system) str)
156 (defalias 'mm-decode-coding-region 'ignore)
157 (defalias 'mm-encode-coding-region 'ignore))
158 (defalias 'mm-decode-coding-string 'decode-coding-string)
159 (defalias 'mm-encode-coding-string 'encode-coding-string)
160 (defalias 'mm-decode-coding-region 'decode-coding-region)
161 (defalias 'mm-encode-coding-region 'encode-coding-region)))
163 (defalias 'mm-string-to-multibyte
167 ((fboundp 'string-to-multibyte)
168 'string-to-multibyte)
171 "Return a multibyte string with the same individual chars as STRING."
173 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
177 (defalias 'mm-char-or-char-int-p
179 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
180 ((fboundp 'char-valid-p) 'char-valid-p)
183 ;; Fixme: This seems always to be used to read a MIME charset, so it
184 ;; should be re-named and fixed (in Emacs) to offer completion only on
185 ;; proper charset names (base coding systems which have a
186 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
188 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
189 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
190 ;; Actually, there should be an `mm-coding-system-mime-charset'.
192 (defalias 'mm-read-coding-system
194 ((fboundp 'read-coding-system)
195 (if (and (featurep 'xemacs)
196 (<= (string-to-number emacs-version) 21.1))
197 (lambda (prompt &optional default-coding-system)
198 (read-coding-system prompt))
199 'read-coding-system))
200 (t (lambda (prompt &optional default-coding-system)
201 "Prompt the user for a coding system."
203 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
204 mm-mime-mule-charset-alist)))))))
206 (defvar mm-coding-system-list nil)
207 (defun mm-get-coding-system-list ()
208 "Get the coding system list."
209 (or mm-coding-system-list
210 (setq mm-coding-system-list (mm-coding-system-list))))
212 (defun mm-coding-system-p (cs)
213 "Return non-nil if CS is a symbol naming a coding system.
214 In XEmacs, also return non-nil if CS is a coding system object.
215 If CS is available, return CS itself in Emacs, and return a coding
216 system object in XEmacs."
217 (if (fboundp 'find-coding-system)
218 (and cs (find-coding-system cs))
219 (if (fboundp 'coding-system-p)
220 (when (coding-system-p cs)
223 (car (memq cs (mm-get-coding-system-list))))))
225 (defun mm-codepage-setup (number &optional alias)
226 "Create a coding system cpNUMBER.
227 The coding system is created using `codepage-setup'. If ALIAS is
228 non-nil, an alias is created and added to
229 `mm-charset-synonym-alist'. If ALIAS is a string, it's used as
230 the alias. Else windows-NUMBER is used."
232 (let ((completion-ignore-case t)
233 (candidates (if (fboundp 'cp-supported-codepages)
234 (cp-supported-codepages)
235 ;; Removed in Emacs 23 (unicode), so signal an error:
236 (error "`codepage-setup' not present in this Emacs version."))))
237 (list (completing-read "Setup DOS Codepage: (default 437) " candidates
238 nil t nil nil "437"))))
240 (setq alias (if (stringp alias)
242 (intern (format "windows-%s" number)))))
243 (let* ((cp (intern (format "cp%s" number))))
244 (unless (mm-coding-system-p cp)
245 (if (fboundp 'codepage-setup) ; silence compiler
246 (codepage-setup number)
247 (error "`codepage-setup' not present in this Emacs version.")))
249 ;; Don't add alias if setup of cp failed.
250 (mm-coding-system-p cp))
251 (add-to-list 'mm-charset-synonym-alist (cons alias cp)))))
253 (defvar mm-charset-synonym-alist
255 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
256 ,@(unless (mm-coding-system-p 'x-ctext)
257 '((x-ctext . ctext)))
258 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_ in 8
260 ,@(unless (mm-coding-system-p 'iso-8859-15)
261 '((iso-8859-15 . iso-8859-1)))
262 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
263 ,@(unless (mm-coding-system-p 'big5-hkscs)
264 '((big5-hkscs . big5)))
265 ;; A Microsoft misunderstanding.
266 ,@(when (and (not (mm-coding-system-p 'unicode))
267 (mm-coding-system-p 'utf-16-le))
268 '((unicode . utf-16-le)))
269 ;; A Microsoft misunderstanding.
270 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
271 (if (mm-coding-system-p 'cp949)
272 '((ks_c_5601-1987 . cp949))
273 '((ks_c_5601-1987 . euc-kr))))
274 ;; Windows-31J is Windows Codepage 932.
275 ,@(when (and (not (mm-coding-system-p 'windows-31j))
276 (mm-coding-system-p 'cp932))
277 '((windows-31j . cp932)))
278 ;; Charset name: GBK, Charset aliases: CP936, MS936, windows-936
279 ;; http://www.iana.org/assignments/charset-reg/GBK
280 ;; Emacs 22.1 has cp936, but not gbk, so we alias it:
281 ,@(when (and (not (mm-coding-system-p 'gbk))
282 (mm-coding-system-p 'cp936))
284 ;; UTF8 is a bogus name for UTF-8
285 ,@(when (and (not (mm-coding-system-p 'utf8))
286 (mm-coding-system-p 'utf-8))
288 ;; ISO8859-1 is a bogus name for ISO-8859-1
289 ,@(when (and (not (mm-coding-system-p 'iso8859-1))
290 (mm-coding-system-p 'iso-8859-1))
291 '((iso8859-1 . iso-8859-1)))
292 ;; ISO_8859-1 is a bogus name for ISO-8859-1
293 ,@(when (and (not (mm-coding-system-p 'iso_8859-1))
294 (mm-coding-system-p 'iso-8859-1))
295 '((iso_8859-1 . iso-8859-1)))
297 "A mapping from unknown or invalid charset names to the real charset names.
299 See `mm-codepage-iso-8859-list' and `mm-codepage-ibm-list'.")
301 (defcustom mm-codepage-iso-8859-list
302 (list 1250 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
303 ;; Outlook users in Czech republic. Use this to allow reading of
304 ;; their e-mails. cp1250 should be defined by M-x codepage-setup
306 '(1252 . 1) ;; Windows-1252 is a superset of iso-8859-1 (West
307 ;; Europe). See also `gnus-article-dumbquotes-map'.
308 '(1254 . 9) ;; Windows-1254 is a superset of iso-8859-9 (Turkish).
309 '(1255 . 8));; Windows-1255 is a superset of iso-8859-8 (Hebrew).
310 "A list of Windows codepage numbers and iso-8859 charset numbers.
312 If an element is a number corresponding to a supported windows
313 codepage, appropriate entries to `mm-charset-synonym-alist' are
314 added by `mm-setup-codepage-iso-8859'. An element may also be a
315 cons cell where the car is a codepage number and the cdr is the
316 corresponding number of an iso-8859 charset."
317 :type '(list (set :inline t
318 (const 1250 :tag "Central and East European")
319 (const (1252 . 1) :tag "West European")
320 (const (1254 . 9) :tag "Turkish")
321 (const (1255 . 8) :tag "Hebrew"))
325 (integer :tag "Windows codepage number")
326 (cons (integer :tag "Windows codepage number")
327 (integer :tag "iso-8859 charset number")))))
328 :version "22.1" ;; Gnus 5.10.9
331 (defcustom mm-codepage-ibm-list
332 (list 437 ;; (US etc.)
336 863 ;; (Canadian French)
341 866 ;; (Cyrillic - Russian)
346 ;; In Emacs 23 (unicode), cp... and ibm... are aliases.
347 ;; Cf. http://thread.gmane.org/v9lkng5nwy.fsf@marauder.physik.uni-ulm.de
348 "List of IBM codepage numbers.
350 The codepage mappings slighly differ between IBM and other vendors.
351 See \"ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/IBM/README.TXT\".
353 If an element is a number corresponding to a supported windows
354 codepage, appropriate entries to `mm-charset-synonym-alist' are
355 added by `mm-setup-codepage-ibm'."
356 :type '(list (set :inline t
357 (const 437 :tag "US etc.")
358 (const 860 :tag "Portugal")
359 (const 861 :tag "Iceland")
360 (const 862 :tag "Israel")
361 (const 863 :tag "Canadian French")
362 (const 865 :tag "Nordic")
364 (const 850 :tag "Latin 1")
365 (const 855 :tag "Cyrillic")
366 (const 866 :tag "Cyrillic - Russian")
367 (const 857 :tag "Turkish")
368 (const 864 :tag "Arabic")
369 (const 869 :tag "Greek")
370 (const 874 :tag "Thai"))
373 (integer :tag "Codepage number")))
374 :version "22.1" ;; Gnus 5.10.9
377 (defun mm-setup-codepage-iso-8859 (&optional list)
378 "Add appropriate entries to `mm-charset-synonym-alist'.
379 Unless LIST is given, `mm-codepage-iso-8859-list' is used."
381 (setq list mm-codepage-iso-8859-list))
383 (let (cp windows iso)
385 (setq cp (intern (format "cp%d" (car i)))
386 windows (intern (format "windows-%d" (car i)))
387 iso (intern (format "iso-8859-%d" (cdr i))))
388 (setq cp (intern (format "cp%d" i))
389 windows (intern (format "windows-%d" i))))
390 (unless (mm-coding-system-p windows)
391 (if (mm-coding-system-p cp)
392 (add-to-list 'mm-charset-synonym-alist (cons windows cp))
393 (add-to-list 'mm-charset-synonym-alist (cons windows iso)))))))
395 (defun mm-setup-codepage-ibm (&optional list)
396 "Add appropriate entries to `mm-charset-synonym-alist'.
397 Unless LIST is given, `mm-codepage-ibm-list' is used."
399 (setq list mm-codepage-ibm-list))
400 (dolist (number list)
401 (let ((ibm (intern (format "ibm%d" number)))
402 (cp (intern (format "cp%d" number))))
403 (when (and (not (mm-coding-system-p ibm))
404 (mm-coding-system-p cp))
405 (add-to-list 'mm-charset-synonym-alist (cons ibm cp))))))
408 (mm-setup-codepage-iso-8859)
409 (mm-setup-codepage-ibm)
411 ;; Note: this has to be defined before `mm-charset-to-coding-system'.
412 (defcustom mm-charset-eval-alist
413 (if (featurep 'xemacs)
414 nil ;; I don't know what would be useful for XEmacs.
415 '(;; Emacs 21 offers 1250 1251 1253 1257. Emacs 22 provides autoloads for
416 ;; 1250-1258 (i.e. `mm-codepage-setup' does nothing).
417 (windows-1250 . (mm-codepage-setup 1250 t))
418 (windows-1251 . (mm-codepage-setup 1251 t))
419 (windows-1253 . (mm-codepage-setup 1253 t))
420 (windows-1257 . (mm-codepage-setup 1257 t))))
421 "An alist of (CHARSET . FORM) pairs.
422 If an article is encoded in an unknown CHARSET, FORM is
423 evaluated. This allows to load additional libraries providing
424 charsets on demand. If supported by your Emacs version, you
425 could use `autoload-coding-system' here."
426 :version "22.1" ;; Gnus 5.10.9
427 :type '(list (set :inline t
428 (const (windows-1250 . (mm-codepage-setup 1250 t)))
429 (const (windows-1251 . (mm-codepage-setup 1251 t)))
430 (const (windows-1253 . (mm-codepage-setup 1253 t)))
431 (const (windows-1257 . (mm-codepage-setup 1257 t)))
432 (const (cp850 . (mm-codepage-setup 850 nil))))
435 (cons (symbol :tag "charset")
436 (symbol :tag "form"))))
438 (put 'mm-charset-eval-alist 'risky-local-variable t)
440 (defvar mm-charset-override-alist)
442 ;; Note: this function has to be defined before `mm-charset-override-alist'
443 ;; since it will use this function in order to determine its default value
444 ;; when loading mm-util.elc.
445 (defun mm-charset-to-coding-system (charset &optional lbt
446 allow-override silent)
447 "Return coding-system corresponding to CHARSET.
448 CHARSET is a symbol naming a MIME charset.
449 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
450 used as the line break code type of the coding system.
452 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
453 map undesired charset names to their replacement. This should
454 only be used for decoding, not for encoding.
456 A non-nil value of SILENT means don't issue a warning even if CHARSET
458 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
459 (when (stringp charset)
460 (setq charset (intern (downcase charset))))
462 (setq charset (intern (format "%s-%s" charset lbt))))
466 ;; Running in a non-MULE environment.
467 ((or (null (mm-get-coding-system-list))
468 (not (fboundp 'coding-system-get)))
470 ;; Check override list quite early. Should only used for decoding, not for
473 (let ((cs (cdr (assq charset mm-charset-override-alist))))
474 (and cs (mm-coding-system-p cs) cs))))
476 ((eq charset 'us-ascii)
478 ;; Check to see whether we can handle this charset. (This depends
479 ;; on there being some coding system matching each `mime-charset'
480 ;; property defined, as there should be.)
481 ((and (mm-coding-system-p charset)
482 ;;; Doing this would potentially weed out incorrect charsets.
484 ;;; (eq charset (coding-system-get charset 'mime-charset))
487 ;; Eval expressions from `mm-charset-eval-alist'
488 ((let* ((el (assq charset mm-charset-eval-alist))
495 (condition-case nil (eval form) (error nil))
496 ;; (message "Failed to eval `%s'" form))
497 (mm-coding-system-p cs)
498 (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
500 ;; Translate invalid charsets.
501 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
503 (mm-coding-system-p cs)
505 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
508 ;; Last resort: search the coding system list for entries which
509 ;; have the right mime-charset in case the canonical name isn't
510 ;; defined (though it should be).
512 ;; mm-get-coding-system-list returns a list of cs without lbt.
514 (dolist (c (mm-get-coding-system-list))
516 (eq charset (or (coding-system-get c :mime-charset)
517 (coding-system-get c 'mime-charset))))
519 (unless (or silent cs)
520 ;; Warn the user about unknown charset:
521 (if (fboundp 'gnus-message)
522 (gnus-message 7 "Unknown charset: %s" charset)
523 (message "Unknown charset: %s" charset)))
526 ;; Note: `mm-charset-to-coding-system' has to be defined before this.
527 (defcustom mm-charset-override-alist
528 ;; Note: pairs that cannot be used in the Emacs version currently running
531 (iso-8859-1 . windows-1252)
532 (iso-8859-8 . windows-1255)
533 (iso-8859-9 . windows-1254))
534 "A mapping from undesired charset names to their replacement.
536 You may add pairs like (iso-8859-1 . windows-1252) here,
537 i.e. treat iso-8859-1 as windows-1252. windows-1252 is a
538 superset of iso-8859-1."
545 (mapcar (lambda (pair)
546 (if (mm-charset-to-coding-system (cdr pair)
550 (iso-8859-1 . windows-1252)
551 (iso-8859-8 . windows-1255)
552 (iso-8859-9 . windows-1254)
553 (undecided . windows-1252)))))
554 (val (copy-sequence (default-value 'mm-charset-override-alist)))
557 (push (if (and (prog1
558 (setq pair (assq (caar val) defaults))
559 (setq defaults (delq pair defaults)))
560 (equal (car val) pair))
563 (const :format "(%v" ,(caar val))
564 (symbol :size 3 :format " . %v)\n" ,(cdar val))))
566 (setq val (cdr val)))
568 (push `(const ,(pop defaults)) rest))
571 `(set :inline t :format "%v" ,@(nreverse rest))
572 `(repeat :inline t :tag "Other options"
574 (symbol :size 3 :format "(%v")
575 (symbol :size 3 :format " . %v)\n")))))))
576 ;; Remove pairs that cannot be used in the Emacs version currently
577 ;; running. Note that this section will be evaluated when loading
579 :set (lambda (symbol value)
582 (mapcar (lambda (pair)
583 (if (mm-charset-to-coding-system (cdr pair)
587 :version "22.1" ;; Gnus 5.10.9
590 (defvar mm-binary-coding-system
592 ((mm-coding-system-p 'binary) 'binary)
593 ((mm-coding-system-p 'no-conversion) 'no-conversion)
595 "100% binary coding system.")
597 (defvar mm-text-coding-system
598 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
599 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
600 (and (mm-coding-system-p 'raw-text) 'raw-text))
601 mm-binary-coding-system)
602 "Text-safe coding system (For removing ^M).")
604 (defvar mm-text-coding-system-for-write nil
605 "Text coding system for write.")
607 (defvar mm-auto-save-coding-system
609 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
610 (if (memq system-type '(windows-nt ms-dos ms-windows))
611 (if (mm-coding-system-p 'utf-8-emacs-dos)
612 'utf-8-emacs-dos mm-binary-coding-system)
614 ((mm-coding-system-p 'emacs-mule)
615 (if (memq system-type '(windows-nt ms-dos ms-windows))
616 (if (mm-coding-system-p 'emacs-mule-dos)
617 'emacs-mule-dos mm-binary-coding-system)
619 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
620 (t mm-binary-coding-system))
621 "Coding system of auto save file.")
623 (defvar mm-universal-coding-system mm-auto-save-coding-system
624 "The universal coding system.")
626 ;; Fixme: some of the cars here aren't valid MIME charsets. That
627 ;; should only matter with XEmacs, though.
628 (defvar mm-mime-mule-charset-alist
630 (iso-8859-1 latin-iso8859-1)
631 (iso-8859-2 latin-iso8859-2)
632 (iso-8859-3 latin-iso8859-3)
633 (iso-8859-4 latin-iso8859-4)
634 (iso-8859-5 cyrillic-iso8859-5)
635 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
636 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
637 ;; charset is koi8-r, not iso-8859-5.
638 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
639 (iso-8859-6 arabic-iso8859-6)
640 (iso-8859-7 greek-iso8859-7)
641 (iso-8859-8 hebrew-iso8859-8)
642 (iso-8859-9 latin-iso8859-9)
643 (iso-8859-14 latin-iso8859-14)
644 (iso-8859-15 latin-iso8859-15)
645 (viscii vietnamese-viscii-lower)
646 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
647 (euc-kr korean-ksc5601)
648 (gb2312 chinese-gb2312)
650 (gb18030 gb18030-2-byte
651 gb18030-4-byte-bmp gb18030-4-byte-smp
652 gb18030-4-byte-ext-1 gb18030-4-byte-ext-2)
653 (big5 chinese-big5-1 chinese-big5-2)
655 (thai-tis620 thai-tis620)
656 (windows-1251 cyrillic-iso8859-5)
657 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
658 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
659 latin-jisx0201 japanese-jisx0208-1978
660 chinese-gb2312 japanese-jisx0208
661 korean-ksc5601 japanese-jisx0212)
662 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
663 latin-jisx0201 japanese-jisx0208-1978
664 chinese-gb2312 japanese-jisx0208
665 korean-ksc5601 japanese-jisx0212
666 chinese-cns11643-1 chinese-cns11643-2)
667 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
668 cyrillic-iso8859-5 greek-iso8859-7
669 latin-jisx0201 japanese-jisx0208-1978
670 chinese-gb2312 japanese-jisx0208
671 korean-ksc5601 japanese-jisx0212
672 chinese-cns11643-1 chinese-cns11643-2
673 chinese-cns11643-3 chinese-cns11643-4
674 chinese-cns11643-5 chinese-cns11643-6
676 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
677 japanese-jisx0213-1 japanese-jisx0213-2)
678 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
679 ,(cond ((fboundp 'unicode-precedence-list)
680 (cons 'utf-8 (delq 'ascii (mapcar 'charset-name
681 (unicode-precedence-list)))))
682 ((or (not (fboundp 'charsetp)) ;; non-Mule case
683 (charsetp 'unicode-a)
684 (not (mm-coding-system-p 'mule-utf-8)))
685 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
686 (t ;; If we have utf-8 we're in Mule 5+.
689 (coding-system-get 'mule-utf-8 'safe-charsets))))))
690 "Alist of MIME-charset/MULE-charsets.")
692 (defun mm-enrich-utf-8-by-mule-ucs ()
693 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
694 This function will run when the `un-define' module is loaded under
695 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
696 with Mule charsets. It is completely useless for Emacs."
697 (when (boundp 'unicode-basic-translation-charset-order-list)
703 'unicode-basic-translation-charset-order-list))))
704 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
707 (setq mm-mime-mule-charset-alist
708 (nconc mm-mime-mule-charset-alist
709 (list (cons 'utf-8 val))))))
712 ;; Correct by construction, but should be unnecessary for Emacs:
713 (if (featurep 'xemacs)
714 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
715 (when (and (fboundp 'coding-system-list)
716 (fboundp 'sort-coding-systems))
717 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
721 mime (or (coding-system-get cs :mime-charset); Emacs 23 (unicode)
722 (coding-system-get cs 'mime-charset)))
724 (not (eq t (setq mule
725 (coding-system-get cs 'safe-charsets))))
726 (not (assq mime alist)))
727 (push (cons mime (delq 'ascii mule)) alist)))
728 (setq mm-mime-mule-charset-alist (nreverse alist)))))
730 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
731 "A list of special charsets.
732 Valid elements include:
733 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
734 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
737 (defvar mm-iso-8859-15-compatible
738 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
739 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
740 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
742 (defvar mm-iso-8859-x-to-15-table
743 (and (fboundp 'coding-system-p)
744 (mm-coding-system-p 'iso-8859-15)
747 (if (mm-coding-system-p (car cs))
748 (let ((c (string-to-char
749 (decode-coding-string "\341" (car cs)))))
750 (cons (char-charset c)
753 (decode-coding-string "\341" 'iso-8859-15)) c)
754 (string-to-list (decode-coding-string (car (cdr cs))
757 mm-iso-8859-15-compatible))
758 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
760 (defcustom mm-coding-system-priorities
761 (if (boundp 'current-language-environment)
762 (let ((lang (symbol-value 'current-language-environment)))
763 (cond ((string= lang "Japanese")
764 ;; Japanese users prefer iso-2022-jp to euc-japan or
765 ;; shift_jis, however iso-8859-1 should be used when
766 ;; there are only ASCII text and Latin-1 characters.
767 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
768 "Preferred coding systems for encoding outgoing messages.
770 More than one suitable coding system may be found for some text.
771 By default, the coding system with the highest priority is used
772 to encode outgoing messages (see `sort-coding-systems'). If this
773 variable is set, it overrides the default priority."
775 :type '(repeat (symbol :tag "Coding system"))
779 (defvar mm-use-find-coding-systems-region
780 (fboundp 'find-coding-systems-region)
781 "Use `find-coding-systems-region' to find proper coding systems.
783 Setting it to nil is useful on Emacsen supporting Unicode if sending
784 mail with multiple parts is preferred to sending a Unicode one.")
786 ;;; Internal variables:
790 (defun mm-mule-charset-to-mime-charset (charset)
791 "Return the MIME charset corresponding to the given Mule CHARSET."
792 (if (and (fboundp 'find-coding-systems-for-charsets)
793 (fboundp 'sort-coding-systems))
794 (let ((css (sort (sort-coding-systems
795 (find-coding-systems-for-charsets (list charset)))
796 'mm-sort-coding-systems-predicate))
798 (while (and (not mime)
800 (when (setq cs (pop css))
801 (setq mime (or (coding-system-get cs :mime-charset)
802 (coding-system-get cs 'mime-charset)))))
804 (let ((alist (mapcar (lambda (cs)
805 (assq cs mm-mime-mule-charset-alist))
806 (sort (mapcar 'car mm-mime-mule-charset-alist)
807 'mm-sort-coding-systems-predicate)))
810 (when (memq charset (cdar alist))
811 (setq out (caar alist)
817 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
818 (boundp 'default-enable-multibyte-characters)
819 default-enable-multibyte-characters
820 (fboundp 'set-buffer-multibyte))
821 "True in Emacs with Mule.")
824 (defun mm-enable-multibyte ()
825 "Set the multibyte flag of the current buffer.
826 Only do this if the default value of `enable-multibyte-characters' is
827 non-nil. This is a no-op in XEmacs."
828 (set-buffer-multibyte 'to))
829 (defalias 'mm-enable-multibyte 'ignore))
832 (defun mm-disable-multibyte ()
833 "Unset the multibyte flag of in the current buffer.
834 This is a no-op in XEmacs."
835 (set-buffer-multibyte nil))
836 (defalias 'mm-disable-multibyte 'ignore)))
838 (defun mm-preferred-coding-system (charset)
839 ;; A typo in some Emacs versions.
840 (or (get-charset-property charset 'preferred-coding-system)
841 (get-charset-property charset 'prefered-coding-system)))
843 ;; Mule charsets shouldn't be used.
844 (defsubst mm-guess-charset ()
845 "Guess Mule charset from the language environment."
847 mail-parse-mule-charset ;; cached mule-charset
849 (setq mail-parse-mule-charset
850 (and (boundp 'current-language-environment)
853 (assoc current-language-environment
854 language-info-alist))))))
855 (if (or (not mail-parse-mule-charset)
856 (eq mail-parse-mule-charset 'ascii))
857 (setq mail-parse-mule-charset
858 (or (car (last (assq mail-parse-charset
859 mm-mime-mule-charset-alist)))
862 mail-parse-mule-charset)))
864 (defun mm-charset-after (&optional pos)
865 "Return charset of a character in current buffer at position POS.
866 If POS is nil, it defauls to the current point.
867 If POS is out of range, the value is nil.
868 If the charset is `composition', return the actual one."
869 (let ((char (char-after pos)) charset)
870 (if (< (mm-char-int char) 128)
871 (setq charset 'ascii)
872 ;; charset-after is fake in some Emacsen.
873 (setq charset (and (fboundp 'char-charset) (char-charset char)))
874 (if (eq charset 'composition) ; Mule 4
875 (let ((p (or pos (point))))
876 (cadr (find-charset-region p (1+ p))))
877 (if (and charset (not (memq charset '(ascii eight-bit-control
878 eight-bit-graphic))))
880 (mm-guess-charset))))))
882 (defun mm-mime-charset (charset)
883 "Return the MIME charset corresponding to the given Mule CHARSET."
884 (if (eq charset 'unknown)
885 (error "The message contains non-printable characters, please use attachment"))
886 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
887 ;; This exists in Emacs 20.
889 (and (mm-preferred-coding-system charset)
890 (or (coding-system-get
891 (mm-preferred-coding-system charset) :mime-charset)
893 (mm-preferred-coding-system charset) 'mime-charset)))
894 (and (eq charset 'ascii)
896 (mm-preferred-coding-system charset)
897 (mm-mule-charset-to-mime-charset charset))
898 ;; This is for XEmacs.
899 (mm-mule-charset-to-mime-charset charset)))
901 (if (fboundp 'delete-dups)
902 (defalias 'mm-delete-duplicates 'delete-dups)
903 (defun mm-delete-duplicates (list)
904 "Destructively remove `equal' duplicates from LIST.
905 Store the result in LIST and return it. LIST must be a proper list.
906 Of several `equal' occurrences of an element in LIST, the first
909 This is a compatibility function for Emacsen without `delete-dups'."
910 ;; Code from `subr.el' in Emacs 22:
913 (setcdr tail (delete (car tail) (cdr tail)))
914 (setq tail (cdr tail))))
917 ;; Fixme: This is used in places when it should be testing the
918 ;; default multibyteness. See mm-default-multibyte-p.
920 (if (and (not (featurep 'xemacs))
921 (boundp 'enable-multibyte-characters))
922 (defun mm-multibyte-p ()
923 "Non-nil if multibyte is enabled in the current buffer."
924 enable-multibyte-characters)
925 (defun mm-multibyte-p () (featurep 'mule))))
927 (defun mm-default-multibyte-p ()
928 "Return non-nil if the session is multibyte.
929 This affects whether coding conversion should be attempted generally."
931 (if (boundp 'default-enable-multibyte-characters)
932 default-enable-multibyte-characters
935 (defun mm-iso-8859-x-to-15-region (&optional b e)
936 (if (fboundp 'char-charset)
937 (let (charset item c inconvertible)
939 (if e (narrow-to-region b e))
940 (goto-char (point-min))
941 (skip-chars-forward "\0-\177")
944 ((not (setq item (assq (char-charset (setq c (char-after)))
945 mm-iso-8859-x-to-15-table)))
947 ((memq c (cdr (cdr item)))
948 (setq inconvertible t)
951 (insert-before-markers (prog1 (+ c (car (cdr item)))
953 (skip-chars-forward "\0-\177")))
954 (not inconvertible))))
956 (defun mm-sort-coding-systems-predicate (a b)
959 ;; Note: invalid entries are dropped silently
960 (and (setq cs (mm-coding-system-p cs))
961 (coding-system-base cs)))
962 mm-coding-system-priorities)))
963 (and (setq a (mm-coding-system-p a))
964 (if (setq b (mm-coding-system-p b))
965 (> (length (memq (coding-system-base a) priorities))
966 (length (memq (coding-system-base b) priorities)))
970 (autoload 'latin-unity-massage-name "latin-unity")
971 (autoload 'latin-unity-maybe-remap "latin-unity")
972 (autoload 'latin-unity-representations-feasible-region "latin-unity")
973 (autoload 'latin-unity-representations-present-region "latin-unity"))
975 (defvar latin-unity-coding-systems)
976 (defvar latin-unity-ucs-list)
978 (defun mm-xemacs-find-mime-charset-1 (begin end)
979 "Determine which MIME charset to use to send region as message.
980 This uses the XEmacs-specific latin-unity package to better handle the
981 case where identical characters from diverse ISO-8859-? character sets
982 can be encoded using a single one of the corresponding coding systems.
984 It treats `mm-coding-system-priorities' as the list of preferred
985 coding systems; a useful example setting for this list in Western
986 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
987 to the very standard Latin 1 coding system, and only move to coding
988 systems that are less supported as is necessary to encode the
989 characters that exist in the buffer.
991 Latin Unity doesn't know about those non-ASCII Roman characters that
992 are available in various East Asian character sets. As such, its
993 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
994 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
995 But this is very much a corner case, so don't worry about it."
996 (let ((systems mm-coding-system-priorities) csets psets curset)
998 ;; Load the Latin Unity library, if available.
999 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
1000 (require 'latin-unity))
1002 ;; Now, can we use it?
1003 (if (featurep 'latin-unity)
1005 (setq csets (latin-unity-representations-feasible-region begin end)
1006 psets (latin-unity-representations-present-region begin end))
1010 ;; Pass back the first coding system in the preferred list
1011 ;; that can encode the whole region.
1012 (dolist (curset systems)
1013 (setq curset (latin-unity-massage-name 'buffer-default curset))
1015 ;; If the coding system is a universal coding system, then
1016 ;; it can certainly encode all the characters in the region.
1017 (if (memq curset latin-unity-ucs-list)
1018 (throw 'done (list curset)))
1020 ;; If a coding system isn't universal, and isn't in
1021 ;; the list that latin unity knows about, we can't
1022 ;; decide whether to use it here. Leave that until later
1023 ;; in `mm-find-mime-charset-region' function, whence we
1024 ;; have been called.
1025 (unless (memq curset latin-unity-coding-systems)
1028 ;; Right, we know about this coding system, and it may
1029 ;; conceivably be able to encode all the characters in
1031 (if (latin-unity-maybe-remap begin end curset csets psets t)
1032 (throw 'done (list curset))))
1034 ;; Can't encode using anything from the
1035 ;; `mm-coding-system-priorities' list.
1036 ;; Leave `mm-find-mime-charset' to do most of the work.
1039 ;; Right, latin unity isn't available; let `mm-find-charset-region'
1040 ;; take its default action, which equally applies to GNU Emacs.
1043 (defmacro mm-xemacs-find-mime-charset (begin end)
1044 (when (featurep 'xemacs)
1045 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
1047 (declare-function mm-delete-duplicates "mm-util" (list))
1049 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
1050 "Return the MIME charsets needed to encode the region between B and E.
1051 nil means ASCII, a single-element list represents an appropriate MIME
1052 charset, and a longer list means no appropriate charset."
1054 ;; The return possibilities of this function are a mess...
1055 (or (and (mm-multibyte-p)
1056 mm-use-find-coding-systems-region
1057 ;; Find the mime-charset of the most preferred coding
1058 ;; system that has one.
1059 (let ((systems (find-coding-systems-region b e)))
1060 (when mm-coding-system-priorities
1062 (sort systems 'mm-sort-coding-systems-predicate)))
1063 (setq systems (delq 'compound-text systems))
1064 (unless (equal systems '(undecided))
1066 (let* ((head (pop systems))
1067 (cs (or (coding-system-get head :mime-charset)
1068 (coding-system-get head 'mime-charset))))
1069 ;; The mime-charset (`x-ctext') of
1070 ;; `compound-text' is not in the IANA list. We
1071 ;; shouldn't normally use anything here with a
1072 ;; mime-charset having an `x-' prefix.
1073 ;; Fixme: Allow this to be overridden, since
1074 ;; there is existing use of x-ctext.
1075 ;; Also people apparently need the coding system
1076 ;; `iso-2022-jp-3' (which Mule-UCS defines with
1077 ;; mime-charset, though it's not valid).
1079 (not (string-match "^[Xx]-" (symbol-name cs)))
1080 ;; UTF-16 of any variety is invalid for
1081 ;; text parts and, unfortunately, has
1082 ;; mime-charset defined both in Mule-UCS
1083 ;; and versions of Emacs. (The name
1084 ;; might be `mule-utf-16...' or
1086 (not (string-match "utf-16" (symbol-name cs))))
1088 charsets (list cs))))))
1090 ;; If we're XEmacs, and some coding system is appropriate,
1091 ;; mm-xemacs-find-mime-charset will return an appropriate list.
1092 ;; Otherwise, we'll get nil, and the next setq will get invoked.
1093 (setq charsets (mm-xemacs-find-mime-charset b e))
1095 ;; Fixme: won't work for unibyte Emacs 23:
1097 ;; We're not multibyte, or a single coding system won't cover it.
1099 (mm-delete-duplicates
1100 (mapcar 'mm-mime-charset
1102 (mm-find-charset-region b e))))))
1103 (if (and (> (length charsets) 1)
1104 (memq 'iso-8859-15 charsets)
1105 (memq 'iso-8859-15 hack-charsets)
1106 (save-excursion (mm-iso-8859-x-to-15-region b e)))
1107 (dolist (x mm-iso-8859-15-compatible)
1108 (setq charsets (delq (car x) charsets))))
1109 (if (and (memq 'iso-2022-jp-2 charsets)
1110 (memq 'iso-2022-jp-2 hack-charsets))
1111 (setq charsets (delq 'iso-2022-jp charsets)))
1112 ;; Attempt to reduce the number of charsets if utf-8 is available.
1113 (if (and (featurep 'xemacs)
1114 (> (length charsets) 1)
1115 (mm-coding-system-p 'utf-8))
1116 (let ((mm-coding-system-priorities
1117 (cons 'utf-8 mm-coding-system-priorities)))
1119 (mm-delete-duplicates
1120 (mapcar 'mm-mime-charset
1122 (mm-find-charset-region b e)))))))
1125 (defmacro mm-with-unibyte-buffer (&rest forms)
1126 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1127 Use unibyte mode for this."
1129 (mm-disable-multibyte)
1131 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
1132 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
1134 (defmacro mm-with-multibyte-buffer (&rest forms)
1135 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1136 Use multibyte mode for this."
1138 (mm-enable-multibyte)
1140 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
1141 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
1143 (defmacro mm-with-unibyte-current-buffer (&rest forms)
1144 "Evaluate FORMS with current buffer temporarily made unibyte.
1145 Also bind `default-enable-multibyte-characters' to nil.
1146 Equivalent to `progn' in XEmacs
1148 NOTE: Use this macro with caution in multibyte buffers (it is not
1149 worth using this macro in unibyte buffers of course). Use of
1150 `(set-buffer-multibyte t)', which is run finally, is generally
1151 harmful since it is likely to modify existing data in the buffer.
1152 For instance, it converts \"\\300\\255\" into \"\\255\" in
1153 Emacs 23 (unicode)."
1154 (let ((multibyte (make-symbol "multibyte"))
1155 (buffer (make-symbol "buffer")))
1157 (let ((,multibyte enable-multibyte-characters)
1158 (,buffer (current-buffer)))
1160 (let (default-enable-multibyte-characters)
1161 (set-buffer-multibyte nil)
1163 (set-buffer ,buffer)
1164 (set-buffer-multibyte ,multibyte)))
1165 (let (default-enable-multibyte-characters)
1167 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
1168 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
1170 (defun mm-find-charset-region (b e)
1171 "Return a list of Emacs charsets in the region B to E."
1173 ((and (mm-multibyte-p)
1174 (fboundp 'find-charset-region))
1175 ;; Remove composition since the base charsets have been included.
1176 ;; Remove eight-bit-*, treat them as ascii.
1177 (let ((css (find-charset-region b e)))
1179 '(composition eight-bit-control eight-bit-graphic control-1)
1181 (setq css (delq cs css)))))
1183 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
1186 (narrow-to-region b e)
1187 (goto-char (point-min))
1188 (skip-chars-forward "\0-\177")
1193 (and (boundp 'current-language-environment)
1194 (car (last (assq 'charset
1195 (assoc current-language-environment
1196 language-info-alist))))))
1197 (if (eq charset 'ascii) (setq charset nil))
1200 (car (last (assq mail-parse-charset
1201 mm-mime-mule-charset-alist)))))
1202 (list 'ascii (or charset 'latin-iso8859-1)))))))))
1204 (defun mm-auto-mode-alist ()
1205 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1206 (let ((alist auto-mode-alist)
1209 (when (listp (cdar alist))
1210 (push (car alist) out))
1214 (defvar mm-inhibit-file-name-handlers
1215 '(jka-compr-handler image-file-handler epa-file-handler)
1216 "A list of handlers doing (un)compression (etc) thingies.")
1218 (defun mm-insert-file-contents (filename &optional visit beg end replace
1220 "Like `insert-file-contents', but only reads in the file.
1221 A buffer may be modified in several ways after reading into the buffer due
1222 to advanced Emacs features, such as file-name-handlers, format decoding,
1223 `find-file-hooks', etc.
1224 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
1225 This function ensures that none of these modifications will take place."
1226 (let* ((format-alist nil)
1227 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
1228 (default-major-mode 'fundamental-mode)
1229 (enable-local-variables nil)
1230 (after-insert-file-functions nil)
1231 (enable-local-eval nil)
1232 (inhibit-file-name-operation (if inhibit
1233 'insert-file-contents
1234 inhibit-file-name-operation))
1235 (inhibit-file-name-handlers
1237 (append mm-inhibit-file-name-handlers
1238 inhibit-file-name-handlers)
1239 inhibit-file-name-handlers))
1240 (ffh (if (boundp 'find-file-hook)
1243 (val (symbol-value ffh)))
1246 (insert-file-contents filename visit beg end replace)
1249 (defun mm-append-to-file (start end filename &optional codesys inhibit)
1250 "Append the contents of the region to the end of file FILENAME.
1251 When called from a function, expects three arguments,
1252 START, END and FILENAME. START and END are buffer positions
1253 saying what text to write.
1254 Optional fourth argument specifies the coding system to use when
1256 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1257 (let ((coding-system-for-write
1258 (or codesys mm-text-coding-system-for-write
1259 mm-text-coding-system))
1260 (inhibit-file-name-operation (if inhibit
1262 inhibit-file-name-operation))
1263 (inhibit-file-name-handlers
1265 (append mm-inhibit-file-name-handlers
1266 inhibit-file-name-handlers)
1267 inhibit-file-name-handlers)))
1268 (write-region start end filename t 'no-message)
1269 (message "Appended to %s" filename)))
1271 (defun mm-write-region (start end filename &optional append visit lockname
1272 coding-system inhibit)
1274 "Like `write-region'.
1275 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1276 (let ((coding-system-for-write
1277 (or coding-system mm-text-coding-system-for-write
1278 mm-text-coding-system))
1279 (inhibit-file-name-operation (if inhibit
1281 inhibit-file-name-operation))
1282 (inhibit-file-name-handlers
1284 (append mm-inhibit-file-name-handlers
1285 inhibit-file-name-handlers)
1286 inhibit-file-name-handlers)))
1287 (write-region start end filename append visit lockname)))
1289 (autoload 'gmm-write-region "gmm-utils")
1291 ;; It is not a MIME function, but some MIME functions use it.
1292 (if (and (fboundp 'make-temp-file)
1294 (let ((def (symbol-function 'make-temp-file)))
1295 (and (byte-code-function-p def)
1296 (setq def (if (fboundp 'compiled-function-arglist)
1298 (eval (list 'compiled-function-arglist def))
1301 (eq (nth 3 def) 'suffix)))))
1302 (defalias 'mm-make-temp-file 'make-temp-file)
1303 ;; Stolen (and modified for XEmacs) from Emacs 22.
1304 (defun mm-make-temp-file (prefix &optional dir-flag suffix)
1305 "Create a temporary file.
1306 The returned file name (created by appending some random characters at the end
1307 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1308 is guaranteed to point to a newly created empty file.
1309 You can then use `write-region' to write new data into the file.
1311 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1313 If SUFFIX is non-nil, add that at the end of the file name."
1314 (let ((umask (default-file-modes))
1318 ;; Create temp files with strict access rights. It's easy to
1319 ;; loosen them later, whereas it's impossible to close the
1320 ;; time-window of loose permissions otherwise.
1321 (set-default-file-modes 448)
1322 (while (condition-case err
1328 (if (fboundp 'temp-directory)
1331 temporary-file-directory))))
1333 (setq file (concat file suffix)))
1335 (make-directory file)
1336 ;; NOTE: This is unsafe if Emacs 20
1337 ;; users and XEmacs users don't use
1338 ;; a secure temp directory.
1339 (gmm-write-region "" nil file nil 'silent
1342 (file-already-exists t)
1343 ;; The XEmacs version of `make-directory' issues
1345 (file-error (or (and (featurep 'xemacs)
1346 (file-exists-p file))
1347 (signal (car err) (cdr err)))))
1348 ;; the file was somehow created by someone else between
1349 ;; `make-temp-name' and `write-region', let's try again.
1353 (set-default-file-modes umask)))))
1355 (defun mm-image-load-path (&optional package)
1357 (dolist (path load-path (nreverse result))
1360 (setq dir (concat (file-name-directory
1361 (directory-file-name path))
1362 "etc/images/" (or package "gnus/")))))
1364 (push path result))))
1366 ;; Fixme: This doesn't look useful where it's used.
1367 (if (fboundp 'detect-coding-region)
1368 (defun mm-detect-coding-region (start end)
1369 "Like `detect-coding-region' except returning the best one."
1370 (let ((coding-systems
1371 (detect-coding-region start end)))
1372 (or (car-safe coding-systems)
1374 (defun mm-detect-coding-region (start end)
1375 (let ((point (point)))
1377 (skip-chars-forward "\0-\177" end)
1379 (if (eq (point) end) 'ascii (mm-guess-charset))
1380 (goto-char point)))))
1382 (declare-function mm-detect-coding-region "mm-util" (start end))
1384 (if (fboundp 'coding-system-get)
1385 (defun mm-detect-mime-charset-region (start end)
1386 "Detect MIME charset of the text in the region between START and END."
1387 (let ((cs (mm-detect-coding-region start end)))
1388 (or (coding-system-get cs :mime-charset)
1389 (coding-system-get cs 'mime-charset))))
1390 (defun mm-detect-mime-charset-region (start end)
1391 "Detect MIME charset of the text in the region between START and END."
1392 (let ((cs (mm-detect-coding-region start end)))
1396 (unless (fboundp 'coding-system-to-mime-charset)
1397 (defalias 'coding-system-to-mime-charset 'ignore)))
1399 (defun mm-coding-system-to-mime-charset (coding-system)
1400 "Return the MIME charset corresponding to CODING-SYSTEM.
1401 To make this function work with XEmacs, the APEL package is required."
1403 (or (and (fboundp 'coding-system-get)
1404 (or (coding-system-get coding-system :mime-charset)
1405 (coding-system-get coding-system 'mime-charset)))
1406 (and (featurep 'xemacs)
1407 (or (and (fboundp 'coding-system-to-mime-charset)
1408 (not (eq (symbol-function 'coding-system-to-mime-charset)
1410 (and (condition-case nil
1413 (fboundp 'coding-system-to-mime-charset)))
1414 (coding-system-to-mime-charset coding-system)))))
1417 (require 'jka-compr))
1419 (defun mm-decompress-buffer (filename &optional inplace force)
1420 "Decompress buffer's contents, depending on jka-compr.
1421 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
1422 agrees with `jka-compr-compression-info-list', decompression is done.
1423 Signal an error if FORCE is neither nil nor t and compressed data are
1424 not decompressed because `auto-compression-mode' is disabled.
1425 If INPLACE is nil, return decompressed data or nil without modifying
1426 the buffer. Otherwise, replace the buffer's contents with the
1427 decompressed data. The buffer's multibyteness must be turned off."
1430 (prog1 t (require 'jka-compr))
1431 (and (fboundp 'jka-compr-installed-p)
1432 (jka-compr-installed-p))))
1433 (let ((info (jka-compr-get-compression-info filename)))
1435 (unless (or (memq force (list nil t))
1436 (jka-compr-installed-p))
1438 (let ((prog (jka-compr-info-uncompress-program info))
1439 (args (jka-compr-info-uncompress-args info))
1440 (msg (format "%s %s..."
1441 (jka-compr-info-uncompress-message info)
1443 (err-file (jka-compr-make-temp-name))
1444 (cur (current-buffer))
1445 (coding-system-for-read mm-binary-coding-system)
1446 (coding-system-for-write mm-binary-coding-system)
1449 (mm-with-unibyte-buffer
1450 (insert-buffer-substring cur)
1453 (unless (memq (apply 'call-process-region
1454 (point-min) (point-max)
1455 prog t (list t err-file) nil args)
1456 jka-compr-acceptable-retval-list)
1460 (delete "" (split-string
1462 (insert-file-contents err-file)
1468 (format "Error while executing \"%s %s < %s\""
1469 prog (mapconcat 'identity args " ")
1471 (setq retval (buffer-string)))
1473 (setq err-msg (error-message-string err)))))
1474 (when (file-exists-p err-file)
1475 (ignore-errors (jka-compr-delete-temp-file err-file)))
1478 (delete-region (point-min) (point-max))
1481 (message "%s" (or err-msg (concat msg "done")))
1485 (unless (fboundp 'coding-system-name)
1486 (defalias 'coding-system-name 'ignore))
1487 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1488 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1489 (unless (fboundp 'find-operation-coding-system)
1490 (defalias 'find-operation-coding-system 'ignore)))
1492 (defun mm-find-buffer-file-coding-system (&optional filename)
1493 "Find coding system used to decode the contents of the current buffer.
1494 This function looks for the coding system magic cookie or examines the
1495 coding system specified by `file-coding-system-alist' being associated
1496 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1497 gzip, bzip2, etc. are allowed."
1499 (setq filename buffer-file-name))
1501 (let ((decomp (unless ;; No worth to examine charset of tar files.
1504 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1506 (mm-decompress-buffer filename nil t))))
1508 (set-buffer (let (default-enable-multibyte-characters)
1509 (generate-new-buffer " *temp*")))
1511 (setq filename (file-name-sans-extension filename)))
1512 (goto-char (point-min))
1515 ((boundp 'set-auto-coding-function) ;; Emacs
1517 (or (funcall (symbol-value 'set-auto-coding-function)
1518 filename (- (point-max) (point-min)))
1519 (car (find-operation-coding-system 'insert-file-contents
1521 (let (auto-coding-alist)
1523 (funcall (symbol-value 'set-auto-coding-function)
1524 nil (- (point-max) (point-min)))
1526 ((and (featurep 'xemacs) (featurep 'file-coding)) ;; XEmacs
1527 (let ((case-fold-search t)
1528 (end (point-at-eol))
1531 (and (re-search-forward "-\\*-+[\t ]*" end t)
1533 (setq start (match-end 0))
1534 (re-search-forward "[\t ]*-+\\*-" end t))
1536 (setq end (match-beginning 0))
1538 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1540 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1542 (find-coding-system (setq codesys
1543 (intern (match-string 1))))
1545 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1548 (setq start (match-end 0))
1549 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1551 (setq end (match-beginning 0))
1554 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1556 (find-coding-system (setq codesys
1557 (intern (match-string 1))))
1560 (goto-char (point-min))
1561 (setq case-fold-search nil)
1562 (re-search-forward "^;;;coding system: "
1563 ;;(+ (point-min) 3000) t))
1565 (looking-at "[^\t\n\r ]+")
1567 (setq codesys (intern (match-string 0))))
1571 (find-file-coding-system-for-read-from-filename
1573 (coding-system-name (coding-system-base codesys)))))))
1575 (kill-buffer (current-buffer)))))))
1579 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1580 ;;; mm-util.el ends here