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 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, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
29 (eval-when-compile (require 'cl))
33 (if (featurep 'xemacs)
34 (unless (ignore-errors
35 (require 'timer-funcs))
42 (let ((nfunc (intern (format "mm-%s" (car elem)))))
43 (if (fboundp (car elem))
44 (defalias nfunc (car elem))
45 (defalias nfunc (cdr elem)))))
46 '((coding-system-list . ignore)
48 (coding-system-equal . equal)
49 (annotationp . ignore)
50 (set-buffer-file-coding-system . ignore)
57 (mapcar (lambda (e) (list (symbol-name (car e))))
58 mm-mime-mule-charset-alist)
61 . (lambda (from to string &optional inplace)
62 ;; stolen (and renamed) from nnheader.el
63 "Replace characters in STRING from FROM to TO.
64 Unless optional argument INPLACE is non-nil, return a new string."
65 (let ((string (if inplace string (copy-sequence string)))
68 ;; Replace all occurrences of FROM with TO.
70 (when (= (aref string idx) from)
75 . (lambda (string regexp rep &optional literal)
76 "See `replace-regexp-in-string', only the order of args differs."
77 (replace-regexp-in-string regexp rep string nil literal)))
78 (string-as-unibyte . identity)
79 (string-make-unibyte . identity)
80 ;; string-as-multibyte often doesn't really do what you think it does.
82 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
83 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
84 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
85 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
87 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
88 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
89 ;; Better use string-to-multibyte or encode-coding-string.
90 ;; If you really need string-as-multibyte somewhere it's usually
91 ;; because you're using the internal emacs-mule representation (maybe
92 ;; because you're using string-as-unibyte somewhere), which is
93 ;; generally a problem in itself.
94 ;; Here is an approximate equivalence table to help think about it:
95 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
96 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
97 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
98 (string-as-multibyte . identity)
99 (multibyte-string-p . ignore)
100 (insert-byte . insert-char)
101 (multibyte-char-to-unibyte . identity)
103 . (lambda (buffer-name)
104 "Returns non-nil if a buffer named BUFFER-NAME gets a special frame."
105 (and special-display-function
106 (or (and (member buffer-name special-display-buffer-names) t)
107 (cdr (assoc buffer-name special-display-buffer-names))
109 (dolist (elem special-display-regexps)
111 (string-match elem buffer-name)
115 (string-match (car elem) buffer-name)
116 (throw 'return (cdr elem))))))))))))
119 (if (featurep 'xemacs)
120 (if (featurep 'file-coding)
121 ;; Don't modify string if CODING-SYSTEM is nil.
123 (defun mm-decode-coding-string (str coding-system)
125 (decode-coding-string str coding-system)
127 (defun mm-encode-coding-string (str coding-system)
129 (encode-coding-string str coding-system)
131 (defun mm-decode-coding-region (start end coding-system)
133 (decode-coding-region start end coding-system)))
134 (defun mm-encode-coding-region (start end coding-system)
136 (encode-coding-region start end coding-system))))
137 (defun mm-decode-coding-string (str coding-system) str)
138 (defun mm-encode-coding-string (str coding-system) str)
139 (defalias 'mm-decode-coding-region 'ignore)
140 (defalias 'mm-encode-coding-region 'ignore))
141 (defalias 'mm-decode-coding-string 'decode-coding-string)
142 (defalias 'mm-encode-coding-string 'encode-coding-string)
143 (defalias 'mm-decode-coding-region 'decode-coding-region)
144 (defalias 'mm-encode-coding-region 'encode-coding-region)))
146 (defalias 'mm-string-to-multibyte
150 ((fboundp 'string-to-multibyte)
151 'string-to-multibyte)
154 "Return a multibyte string with the same individual chars as string."
156 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
160 (defalias 'mm-char-or-char-int-p
162 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
163 ((fboundp 'char-valid-p) 'char-valid-p)
166 ;; Fixme: This seems always to be used to read a MIME charset, so it
167 ;; should be re-named and fixed (in Emacs) to offer completion only on
168 ;; proper charset names (base coding systems which have a
169 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
171 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
172 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
173 ;; Actually, there should be an `mm-coding-system-mime-charset'.
175 (defalias 'mm-read-coding-system
177 ((fboundp 'read-coding-system)
178 (if (and (featurep 'xemacs)
179 (<= (string-to-number emacs-version) 21.1))
180 (lambda (prompt &optional default-coding-system)
181 (read-coding-system prompt))
182 'read-coding-system))
183 (t (lambda (prompt &optional default-coding-system)
184 "Prompt the user for a coding system."
186 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
187 mm-mime-mule-charset-alist)))))))
189 (defvar mm-coding-system-list nil)
190 (defun mm-get-coding-system-list ()
191 "Get the coding system list."
192 (or mm-coding-system-list
193 (setq mm-coding-system-list (mm-coding-system-list))))
195 (defun mm-coding-system-p (cs)
196 "Return non-nil if CS is a symbol naming a coding system.
197 In XEmacs, also return non-nil if CS is a coding system object.
198 If CS is available, return CS itself in Emacs, and return a coding
199 system object in XEmacs."
200 (if (fboundp 'find-coding-system)
201 (and cs (find-coding-system cs))
202 (if (fboundp 'coding-system-p)
203 (when (coding-system-p cs)
206 (car (memq cs (mm-get-coding-system-list))))))
208 (defun mm-codepage-setup (number &optional alias)
209 "Create a coding system cpNUMBER.
210 The coding system is created using `codepage-setup'. If ALIAS is
211 non-nil, an alias is created and added to
212 `mm-charset-synonym-alist'. If ALIAS is a string, it's used as
213 the alias. Else windows-NUMBER is used."
215 (let ((completion-ignore-case t)
216 (candidates (cp-supported-codepages)))
217 (list (completing-read "Setup DOS Codepage: (default 437) " candidates
218 nil t nil nil "437"))))
220 (setq alias (if (stringp alias)
222 (intern (format "windows-%s" number)))))
223 (let* ((cp (intern (format "cp%s" number))))
224 (unless (mm-coding-system-p cp)
225 (codepage-setup number))
227 ;; Don't add alias if setup of cp failed.
228 (mm-coding-system-p cp))
229 (add-to-list 'mm-charset-synonym-alist (cons alias cp)))))
231 (defvar mm-charset-synonym-alist
233 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
234 ,@(unless (mm-coding-system-p 'x-ctext)
235 '((x-ctext . ctext)))
236 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_ in 8
238 ,@(unless (mm-coding-system-p 'iso-8859-15)
239 '((iso-8859-15 . iso-8859-1)))
240 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
241 ,@(unless (mm-coding-system-p 'big5-hkscs)
242 '((big5-hkscs . big5)))
243 ;; A Microsoft misunderstanding.
244 ,@(when (and (not (mm-coding-system-p 'unicode))
245 (mm-coding-system-p 'utf-16-le))
246 '((unicode . utf-16-le)))
247 ;; A Microsoft misunderstanding.
248 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
249 (if (mm-coding-system-p 'cp949)
250 '((ks_c_5601-1987 . cp949))
251 '((ks_c_5601-1987 . euc-kr))))
252 ;; Windows-31J is Windows Codepage 932.
253 ,@(when (and (not (mm-coding-system-p 'windows-31j))
254 (mm-coding-system-p 'cp932))
255 '((windows-31j . cp932)))
256 ;; Charset name: GBK, Charset aliases: CP936, MS936, windows-936
257 ;; http://www.iana.org/assignments/charset-reg/GBK
258 ;; Emacs 22.1 has cp936, but not gbk, so we alias it:
259 ,@(when (and (not (mm-coding-system-p 'gbk))
260 (mm-coding-system-p 'cp936))
262 ;; ISO8859-1 is a bogus name for ISO-8859-1
263 ,@(when (and (not (mm-coding-system-p 'iso8859-1))
264 (mm-coding-system-p 'iso-8859-1))
265 '((iso8859-1 . iso-8859-1)))
267 "A mapping from unknown or invalid charset names to the real charset names.
269 See `mm-codepage-iso-8859-list' and `mm-codepage-ibm-list'.")
271 (defcustom mm-codepage-iso-8859-list
272 (list 1250 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
273 ;; Outlook users in Czech republic. Use this to allow reading of
274 ;; their e-mails. cp1250 should be defined by M-x codepage-setup
276 '(1252 . 1) ;; Windows-1252 is a superset of iso-8859-1 (West
277 ;; Europe). See also `gnus-article-dumbquotes-map'.
278 '(1254 . 9) ;; Windows-1254 is a superset of iso-8859-9 (Turkish).
279 '(1255 . 8));; Windows-1255 is a superset of iso-8859-8 (Hebrew).
280 "A list of Windows codepage numbers and iso-8859 charset numbers.
282 If an element is a number corresponding to a supported windows
283 codepage, appropriate entries to `mm-charset-synonym-alist' are
284 added by `mm-setup-codepage-iso-8859'. An element may also be a
285 cons cell where the car is a codepage number and the cdr is the
286 corresponding number of an iso-8859 charset."
287 :type '(list (set :inline t
288 (const 1250 :tag "Central and East European")
289 (const (1252 . 1) :tag "West European")
290 (const (1254 . 9) :tag "Turkish")
291 (const (1255 . 8) :tag "Hebrew"))
295 (integer :tag "Windows codepage number")
296 (cons (integer :tag "Windows codepage number")
297 (integer :tag "iso-8859 charset number")))))
298 :version "22.1" ;; Gnus 5.10.9
301 (defcustom mm-codepage-ibm-list
302 (list 437 ;; (US etc.)
306 863 ;; (Canadian French)
311 866 ;; (Cyrillic - Russian)
316 ;; In Emacs 23 (unicode), cp... and ibm... are aliases.
317 ;; Cf. http://thread.gmane.org/v9lkng5nwy.fsf@marauder.physik.uni-ulm.de
318 "List of IBM codepage numbers.
320 The codepage mappings slighly differ between IBM and other vendors.
321 See \"ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/IBM/README.TXT\".
323 If an element is a number corresponding to a supported windows
324 codepage, appropriate entries to `mm-charset-synonym-alist' are
325 added by `mm-setup-codepage-ibm'."
326 :type '(list (set :inline t
327 (const 437 :tag "US etc.")
328 (const 860 :tag "Portugal")
329 (const 861 :tag "Iceland")
330 (const 862 :tag "Israel")
331 (const 863 :tag "Canadian French")
332 (const 865 :tag "Nordic")
334 (const 850 :tag "Latin 1")
335 (const 855 :tag "Cyrillic")
336 (const 866 :tag "Cyrillic - Russian")
337 (const 857 :tag "Turkish")
338 (const 864 :tag "Arabic")
339 (const 869 :tag "Greek")
340 (const 874 :tag "Thai"))
343 (integer :tag "Codepage number")))
344 :version "22.1" ;; Gnus 5.10.9
347 (defun mm-setup-codepage-iso-8859 (&optional list)
348 "Add appropriate entries to `mm-charset-synonym-alist'.
349 Unless LIST is given, `mm-codepage-iso-8859-list' is used."
351 (setq list mm-codepage-iso-8859-list))
353 (let (cp windows iso)
355 (setq cp (intern (format "cp%d" (car i)))
356 windows (intern (format "windows-%d" (car i)))
357 iso (intern (format "iso-8859-%d" (cdr i))))
358 (setq cp (intern (format "cp%d" i))
359 windows (intern (format "windows-%d" i))))
360 (unless (mm-coding-system-p windows)
361 (if (mm-coding-system-p cp)
362 (add-to-list 'mm-charset-synonym-alist (cons windows cp))
363 (add-to-list 'mm-charset-synonym-alist (cons windows iso)))))))
365 (defun mm-setup-codepage-ibm (&optional list)
366 "Add appropriate entries to `mm-charset-synonym-alist'.
367 Unless LIST is given, `mm-codepage-ibm-list' is used."
369 (setq list mm-codepage-ibm-list))
370 (dolist (number list)
371 (let ((ibm (intern (format "ibm%d" number)))
372 (cp (intern (format "cp%d" number))))
373 (when (and (not (mm-coding-system-p ibm))
374 (mm-coding-system-p cp))
375 (add-to-list 'mm-charset-synonym-alist (cons ibm cp))))))
378 (mm-setup-codepage-iso-8859)
379 (mm-setup-codepage-ibm)
381 (defcustom mm-charset-override-alist
382 '((iso-8859-1 . windows-1252)
383 (iso-8859-8 . windows-1255)
384 (iso-8859-9 . windows-1254))
385 "A mapping from undesired charset names to their replacement.
387 You may add pairs like (iso-8859-1 . windows-1252) here,
388 i.e. treat iso-8859-1 as windows-1252. windows-1252 is a
389 superset of iso-8859-1."
390 :type '(list (set :inline t
391 (const (iso-8859-1 . windows-1252))
392 (const (iso-8859-8 . windows-1255))
393 (const (iso-8859-9 . windows-1254))
394 (const (undecided . windows-1252)))
397 (cons (symbol :tag "From charset")
398 (symbol :tag "To charset"))))
399 :version "22.1" ;; Gnus 5.10.9
402 (defcustom mm-charset-eval-alist
403 (if (featurep 'xemacs)
404 nil ;; I don't know what would be useful for XEmacs.
405 '(;; Emacs 21 offers 1250 1251 1253 1257. Emacs 22 provides autoloads for
406 ;; 1250-1258 (i.e. `mm-codepage-setup' does nothing).
407 (windows-1250 . (mm-codepage-setup 1250 t))
408 (windows-1251 . (mm-codepage-setup 1251 t))
409 (windows-1253 . (mm-codepage-setup 1253 t))
410 (windows-1257 . (mm-codepage-setup 1257 t))))
411 "An alist of (CHARSET . FORM) pairs.
412 If an article is encoded in an unknown CHARSET, FORM is
413 evaluated. This allows to load additional libraries providing
414 charsets on demand. If supported by your Emacs version, you
415 could use `autoload-coding-system' here."
416 :version "22.1" ;; Gnus 5.10.9
417 :type '(list (set :inline t
418 (const (windows-1250 . (mm-codepage-setup 1250 t)))
419 (const (windows-1251 . (mm-codepage-setup 1251 t)))
420 (const (windows-1253 . (mm-codepage-setup 1253 t)))
421 (const (windows-1257 . (mm-codepage-setup 1257 t)))
422 (const (cp850 . (mm-codepage-setup 850 nil))))
425 (cons (symbol :tag "charset")
426 (symbol :tag "form"))))
429 (defvar mm-binary-coding-system
431 ((mm-coding-system-p 'binary) 'binary)
432 ((mm-coding-system-p 'no-conversion) 'no-conversion)
434 "100% binary coding system.")
436 (defvar mm-text-coding-system
437 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
438 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
439 (and (mm-coding-system-p 'raw-text) 'raw-text))
440 mm-binary-coding-system)
441 "Text-safe coding system (For removing ^M).")
443 (defvar mm-text-coding-system-for-write nil
444 "Text coding system for write.")
446 (defvar mm-auto-save-coding-system
448 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
449 (if (memq system-type '(windows-nt ms-dos ms-windows))
450 (if (mm-coding-system-p 'utf-8-emacs-dos)
451 'utf-8-emacs-dos mm-binary-coding-system)
453 ((mm-coding-system-p 'emacs-mule)
454 (if (memq system-type '(windows-nt ms-dos ms-windows))
455 (if (mm-coding-system-p 'emacs-mule-dos)
456 'emacs-mule-dos mm-binary-coding-system)
458 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
459 (t mm-binary-coding-system))
460 "Coding system of auto save file.")
462 (defvar mm-universal-coding-system mm-auto-save-coding-system
463 "The universal coding system.")
465 ;; Fixme: some of the cars here aren't valid MIME charsets. That
466 ;; should only matter with XEmacs, though.
467 (defvar mm-mime-mule-charset-alist
469 (iso-8859-1 latin-iso8859-1)
470 (iso-8859-2 latin-iso8859-2)
471 (iso-8859-3 latin-iso8859-3)
472 (iso-8859-4 latin-iso8859-4)
473 (iso-8859-5 cyrillic-iso8859-5)
474 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
475 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
476 ;; charset is koi8-r, not iso-8859-5.
477 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
478 (iso-8859-6 arabic-iso8859-6)
479 (iso-8859-7 greek-iso8859-7)
480 (iso-8859-8 hebrew-iso8859-8)
481 (iso-8859-9 latin-iso8859-9)
482 (iso-8859-14 latin-iso8859-14)
483 (iso-8859-15 latin-iso8859-15)
484 (viscii vietnamese-viscii-lower)
485 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
486 (euc-kr korean-ksc5601)
487 (gb2312 chinese-gb2312)
488 (big5 chinese-big5-1 chinese-big5-2)
490 (thai-tis620 thai-tis620)
491 (windows-1251 cyrillic-iso8859-5)
492 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
493 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
494 latin-jisx0201 japanese-jisx0208-1978
495 chinese-gb2312 japanese-jisx0208
496 korean-ksc5601 japanese-jisx0212)
497 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
498 latin-jisx0201 japanese-jisx0208-1978
499 chinese-gb2312 japanese-jisx0208
500 korean-ksc5601 japanese-jisx0212
501 chinese-cns11643-1 chinese-cns11643-2)
502 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
503 cyrillic-iso8859-5 greek-iso8859-7
504 latin-jisx0201 japanese-jisx0208-1978
505 chinese-gb2312 japanese-jisx0208
506 korean-ksc5601 japanese-jisx0212
507 chinese-cns11643-1 chinese-cns11643-2
508 chinese-cns11643-3 chinese-cns11643-4
509 chinese-cns11643-5 chinese-cns11643-6
511 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
512 japanese-jisx0213-1 japanese-jisx0213-2)
513 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
514 ,(cond ((fboundp 'unicode-precedence-list)
515 (cons 'utf-8 (delq 'ascii (mapcar 'charset-name
516 (unicode-precedence-list)))))
517 ((or (not (fboundp 'charsetp)) ;; non-Mule case
518 (charsetp 'unicode-a)
519 (not (mm-coding-system-p 'mule-utf-8)))
520 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
521 (t ;; If we have utf-8 we're in Mule 5+.
524 (coding-system-get 'mule-utf-8 'safe-charsets))))))
525 "Alist of MIME-charset/MULE-charsets.")
527 (defun mm-enrich-utf-8-by-mule-ucs ()
528 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
529 This function will run when the `un-define' module is loaded under
530 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
531 with Mule charsets. It is completely useless for Emacs."
532 (when (boundp 'unicode-basic-translation-charset-order-list)
538 'unicode-basic-translation-charset-order-list))))
539 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
542 (setq mm-mime-mule-charset-alist
543 (nconc mm-mime-mule-charset-alist
544 (list (cons 'utf-8 val))))))
547 ;; Correct by construction, but should be unnecessary for Emacs:
548 (if (featurep 'xemacs)
549 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
550 (when (and (fboundp 'coding-system-list)
551 (fboundp 'sort-coding-systems))
552 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
556 mime (or (coding-system-get cs :mime-charset) ; Emacs 23 (unicode)
557 (coding-system-get cs 'mime-charset)))
559 (not (eq t (setq mule
560 (coding-system-get cs 'safe-charsets))))
561 (not (assq mime alist)))
562 (push (cons mime (delq 'ascii mule)) alist)))
563 (setq mm-mime-mule-charset-alist (nreverse alist)))))
565 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
566 "A list of special charsets.
567 Valid elements include:
568 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
569 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
572 (defvar mm-iso-8859-15-compatible
573 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
574 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
575 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
577 (defvar mm-iso-8859-x-to-15-table
578 (and (fboundp 'coding-system-p)
579 (mm-coding-system-p 'iso-8859-15)
582 (if (mm-coding-system-p (car cs))
583 (let ((c (string-to-char
584 (decode-coding-string "\341" (car cs)))))
585 (cons (char-charset c)
588 (decode-coding-string "\341" 'iso-8859-15)) c)
589 (string-to-list (decode-coding-string (car (cdr cs))
592 mm-iso-8859-15-compatible))
593 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
595 (defcustom mm-coding-system-priorities
596 (if (boundp 'current-language-environment)
597 (let ((lang (symbol-value 'current-language-environment)))
598 (cond ((string= lang "Japanese")
599 ;; Japanese users prefer iso-2022-jp to euc-japan or
600 ;; shift_jis, however iso-8859-1 should be used when
601 ;; there are only ASCII text and Latin-1 characters.
602 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
603 "Preferred coding systems for encoding outgoing messages.
605 More than one suitable coding system may be found for some text.
606 By default, the coding system with the highest priority is used
607 to encode outgoing messages (see `sort-coding-systems'). If this
608 variable is set, it overrides the default priority."
610 :type '(repeat (symbol :tag "Coding system"))
614 (defvar mm-use-find-coding-systems-region
615 (fboundp 'find-coding-systems-region)
616 "Use `find-coding-systems-region' to find proper coding systems.
618 Setting it to nil is useful on Emacsen supporting Unicode if sending
619 mail with multiple parts is preferred to sending a Unicode one.")
621 ;;; Internal variables:
625 (defun mm-mule-charset-to-mime-charset (charset)
626 "Return the MIME charset corresponding to the given Mule CHARSET."
627 (if (and (fboundp 'find-coding-systems-for-charsets)
628 (fboundp 'sort-coding-systems))
629 (let ((css (sort (sort-coding-systems
630 (find-coding-systems-for-charsets (list charset)))
631 'mm-sort-coding-systems-predicate))
633 (while (and (not mime)
635 (when (setq cs (pop css))
636 (setq mime (or (coding-system-get cs :mime-charset)
637 (coding-system-get cs 'mime-charset)))))
639 (let ((alist (mapcar (lambda (cs)
640 (assq cs mm-mime-mule-charset-alist))
641 (sort (mapcar 'car mm-mime-mule-charset-alist)
642 'mm-sort-coding-systems-predicate)))
645 (when (memq charset (cdar alist))
646 (setq out (caar alist)
651 (defun mm-charset-to-coding-system (charset &optional lbt
653 "Return coding-system corresponding to CHARSET.
654 CHARSET is a symbol naming a MIME charset.
655 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
656 used as the line break code type of the coding system.
658 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
659 map undesired charset names to their replacement. This should
660 only be used for decoding, not for encoding."
661 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
662 (when (stringp charset)
663 (setq charset (intern (downcase charset))))
665 (setq charset (intern (format "%s-%s" charset lbt))))
669 ;; Running in a non-MULE environment.
670 ((or (null (mm-get-coding-system-list))
671 (not (fboundp 'coding-system-get)))
673 ;; Check override list quite early. Should only used for decoding, not for
676 (let ((cs (cdr (assq charset mm-charset-override-alist))))
677 (and cs (mm-coding-system-p cs) cs))))
679 ((eq charset 'us-ascii)
681 ;; Check to see whether we can handle this charset. (This depends
682 ;; on there being some coding system matching each `mime-charset'
683 ;; property defined, as there should be.)
684 ((and (mm-coding-system-p charset)
685 ;;; Doing this would potentially weed out incorrect charsets.
687 ;;; (eq charset (coding-system-get charset 'mime-charset))
690 ;; Eval expressions from `mm-charset-eval-alist'
691 ((let* ((el (assq charset mm-charset-eval-alist))
698 (condition-case nil (eval form) (error nil))
699 ;; (message "Failed to eval `%s'" form))
700 (mm-coding-system-p cs)
701 (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
703 ;; Translate invalid charsets.
704 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
706 (mm-coding-system-p cs)
708 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
711 ;; Last resort: search the coding system list for entries which
712 ;; have the right mime-charset in case the canonical name isn't
713 ;; defined (though it should be).
715 ;; mm-get-coding-system-list returns a list of cs without lbt.
717 (dolist (c (mm-get-coding-system-list))
719 (eq charset (or (coding-system-get c :mime-charset)
720 (coding-system-get c 'mime-charset))))
723 ;; Warn the user about unknown charset:
724 (if (fboundp 'gnus-message)
725 (gnus-message 7 "Unknown charset: %s" charset)
726 (message "Unknown charset: %s" charset)))
730 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
731 (boundp 'default-enable-multibyte-characters)
732 default-enable-multibyte-characters
733 (fboundp 'set-buffer-multibyte))
734 "True in Emacs with Mule.")
737 (defun mm-enable-multibyte ()
738 "Set the multibyte flag of the current buffer.
739 Only do this if the default value of `enable-multibyte-characters' is
740 non-nil. This is a no-op in XEmacs."
741 (set-buffer-multibyte 'to))
742 (defalias 'mm-enable-multibyte 'ignore))
745 (defun mm-disable-multibyte ()
746 "Unset the multibyte flag of in the current buffer.
747 This is a no-op in XEmacs."
748 (set-buffer-multibyte nil))
749 (defalias 'mm-disable-multibyte 'ignore)))
751 (defun mm-preferred-coding-system (charset)
752 ;; A typo in some Emacs versions.
753 (or (get-charset-property charset 'preferred-coding-system)
754 (get-charset-property charset 'prefered-coding-system)))
756 ;; Mule charsets shouldn't be used.
757 (defsubst mm-guess-charset ()
758 "Guess Mule charset from the language environment."
760 mail-parse-mule-charset ;; cached mule-charset
762 (setq mail-parse-mule-charset
763 (and (boundp 'current-language-environment)
766 (assoc current-language-environment
767 language-info-alist))))))
768 (if (or (not mail-parse-mule-charset)
769 (eq mail-parse-mule-charset 'ascii))
770 (setq mail-parse-mule-charset
771 (or (car (last (assq mail-parse-charset
772 mm-mime-mule-charset-alist)))
775 mail-parse-mule-charset)))
777 (defun mm-charset-after (&optional pos)
778 "Return charset of a character in current buffer at position POS.
779 If POS is nil, it defauls to the current point.
780 If POS is out of range, the value is nil.
781 If the charset is `composition', return the actual one."
782 (let ((char (char-after pos)) charset)
783 (if (< (mm-char-int char) 128)
784 (setq charset 'ascii)
785 ;; charset-after is fake in some Emacsen.
786 (setq charset (and (fboundp 'char-charset) (char-charset char)))
787 (if (eq charset 'composition) ; Mule 4
788 (let ((p (or pos (point))))
789 (cadr (find-charset-region p (1+ p))))
790 (if (and charset (not (memq charset '(ascii eight-bit-control
791 eight-bit-graphic))))
793 (mm-guess-charset))))))
795 (defun mm-mime-charset (charset)
796 "Return the MIME charset corresponding to the given Mule CHARSET."
797 (if (eq charset 'unknown)
798 (error "The message contains non-printable characters, please use attachment"))
799 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
800 ;; This exists in Emacs 20.
802 (and (mm-preferred-coding-system charset)
803 (or (coding-system-get
804 (mm-preferred-coding-system charset) :mime-charset)
806 (mm-preferred-coding-system charset) 'mime-charset)))
807 (and (eq charset 'ascii)
809 (mm-preferred-coding-system charset)
810 (mm-mule-charset-to-mime-charset charset))
811 ;; This is for XEmacs.
812 (mm-mule-charset-to-mime-charset charset)))
814 (if (fboundp 'delete-dups)
815 (defalias 'mm-delete-duplicates 'delete-dups)
816 (defun mm-delete-duplicates (list)
817 "Destructively remove `equal' duplicates from LIST.
818 Store the result in LIST and return it. LIST must be a proper list.
819 Of several `equal' occurrences of an element in LIST, the first
822 This is a compatibility function for Emacsen without `delete-dups'."
823 ;; Code from `subr.el' in Emacs 22:
826 (setcdr tail (delete (car tail) (cdr tail)))
827 (setq tail (cdr tail))))
830 ;; Fixme: This is used in places when it should be testing the
831 ;; default multibyteness. See mm-default-multibyte-p.
833 (if (and (not (featurep 'xemacs))
834 (boundp 'enable-multibyte-characters))
835 (defun mm-multibyte-p ()
836 "Non-nil if multibyte is enabled in the current buffer."
837 enable-multibyte-characters)
838 (defun mm-multibyte-p () (featurep 'mule))))
840 (defun mm-default-multibyte-p ()
841 "Return non-nil if the session is multibyte.
842 This affects whether coding conversion should be attempted generally."
844 (if (boundp 'default-enable-multibyte-characters)
845 default-enable-multibyte-characters
848 (defun mm-iso-8859-x-to-15-region (&optional b e)
849 (if (fboundp 'char-charset)
850 (let (charset item c inconvertible)
852 (if e (narrow-to-region b e))
853 (goto-char (point-min))
854 (skip-chars-forward "\0-\177")
857 ((not (setq item (assq (char-charset (setq c (char-after)))
858 mm-iso-8859-x-to-15-table)))
860 ((memq c (cdr (cdr item)))
861 (setq inconvertible t)
864 (insert-before-markers (prog1 (+ c (car (cdr item)))
866 (skip-chars-forward "\0-\177")))
867 (not inconvertible))))
869 (defun mm-sort-coding-systems-predicate (a b)
872 ;; Note: invalid entries are dropped silently
873 (and (setq cs (mm-coding-system-p cs))
874 (coding-system-base cs)))
875 mm-coding-system-priorities)))
876 (and (setq a (mm-coding-system-p a))
877 (if (setq b (mm-coding-system-p b))
878 (> (length (memq (coding-system-base a) priorities))
879 (length (memq (coding-system-base b) priorities)))
883 (autoload 'latin-unity-massage-name "latin-unity")
884 (autoload 'latin-unity-maybe-remap "latin-unity")
885 (autoload 'latin-unity-representations-feasible-region "latin-unity")
886 (autoload 'latin-unity-representations-present-region "latin-unity")
887 (defvar latin-unity-coding-systems)
888 (defvar latin-unity-ucs-list))
890 (defun mm-xemacs-find-mime-charset-1 (begin end)
891 "Determine which MIME charset to use to send region as message.
892 This uses the XEmacs-specific latin-unity package to better handle the
893 case where identical characters from diverse ISO-8859-? character sets
894 can be encoded using a single one of the corresponding coding systems.
896 It treats `mm-coding-system-priorities' as the list of preferred
897 coding systems; a useful example setting for this list in Western
898 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
899 to the very standard Latin 1 coding system, and only move to coding
900 systems that are less supported as is necessary to encode the
901 characters that exist in the buffer.
903 Latin Unity doesn't know about those non-ASCII Roman characters that
904 are available in various East Asian character sets. As such, its
905 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
906 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
907 But this is very much a corner case, so don't worry about it."
908 (let ((systems mm-coding-system-priorities) csets psets curset)
910 ;; Load the Latin Unity library, if available.
911 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
912 (require 'latin-unity))
914 ;; Now, can we use it?
915 (if (featurep 'latin-unity)
917 (setq csets (latin-unity-representations-feasible-region begin end)
918 psets (latin-unity-representations-present-region begin end))
922 ;; Pass back the first coding system in the preferred list
923 ;; that can encode the whole region.
924 (dolist (curset systems)
925 (setq curset (latin-unity-massage-name 'buffer-default curset))
927 ;; If the coding system is a universal coding system, then
928 ;; it can certainly encode all the characters in the region.
929 (if (memq curset latin-unity-ucs-list)
930 (throw 'done (list curset)))
932 ;; If a coding system isn't universal, and isn't in
933 ;; the list that latin unity knows about, we can't
934 ;; decide whether to use it here. Leave that until later
935 ;; in `mm-find-mime-charset-region' function, whence we
937 (unless (memq curset latin-unity-coding-systems)
940 ;; Right, we know about this coding system, and it may
941 ;; conceivably be able to encode all the characters in
943 (if (latin-unity-maybe-remap begin end curset csets psets t)
944 (throw 'done (list curset))))
946 ;; Can't encode using anything from the
947 ;; `mm-coding-system-priorities' list.
948 ;; Leave `mm-find-mime-charset' to do most of the work.
951 ;; Right, latin unity isn't available; let `mm-find-charset-region'
952 ;; take its default action, which equally applies to GNU Emacs.
955 (defmacro mm-xemacs-find-mime-charset (begin end)
956 (when (featurep 'xemacs)
957 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
959 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
960 "Return the MIME charsets needed to encode the region between B and E.
961 nil means ASCII, a single-element list represents an appropriate MIME
962 charset, and a longer list means no appropriate charset."
964 ;; The return possibilities of this function are a mess...
965 (or (and (mm-multibyte-p)
966 mm-use-find-coding-systems-region
967 ;; Find the mime-charset of the most preferred coding
968 ;; system that has one.
969 (let ((systems (find-coding-systems-region b e)))
970 (when mm-coding-system-priorities
972 (sort systems 'mm-sort-coding-systems-predicate)))
973 (setq systems (delq 'compound-text systems))
974 (unless (equal systems '(undecided))
976 (let* ((head (pop systems))
977 (cs (or (coding-system-get head :mime-charset)
978 (coding-system-get head 'mime-charset))))
979 ;; The mime-charset (`x-ctext') of
980 ;; `compound-text' is not in the IANA list. We
981 ;; shouldn't normally use anything here with a
982 ;; mime-charset having an `x-' prefix.
983 ;; Fixme: Allow this to be overridden, since
984 ;; there is existing use of x-ctext.
985 ;; Also people apparently need the coding system
986 ;; `iso-2022-jp-3' (which Mule-UCS defines with
987 ;; mime-charset, though it's not valid).
989 (not (string-match "^[Xx]-" (symbol-name cs)))
990 ;; UTF-16 of any variety is invalid for
991 ;; text parts and, unfortunately, has
992 ;; mime-charset defined both in Mule-UCS
993 ;; and versions of Emacs. (The name
994 ;; might be `mule-utf-16...' or
996 (not (string-match "utf-16" (symbol-name cs))))
998 charsets (list cs))))))
1000 ;; If we're XEmacs, and some coding system is appropriate,
1001 ;; mm-xemacs-find-mime-charset will return an appropriate list.
1002 ;; Otherwise, we'll get nil, and the next setq will get invoked.
1003 (setq charsets (mm-xemacs-find-mime-charset b e))
1005 ;; We're not multibyte, or a single coding system won't cover it.
1007 (mm-delete-duplicates
1008 (mapcar 'mm-mime-charset
1010 (mm-find-charset-region b e))))))
1011 (if (and (> (length charsets) 1)
1012 (memq 'iso-8859-15 charsets)
1013 (memq 'iso-8859-15 hack-charsets)
1014 (save-excursion (mm-iso-8859-x-to-15-region b e)))
1015 (dolist (x mm-iso-8859-15-compatible)
1016 (setq charsets (delq (car x) charsets))))
1017 (if (and (memq 'iso-2022-jp-2 charsets)
1018 (memq 'iso-2022-jp-2 hack-charsets))
1019 (setq charsets (delq 'iso-2022-jp charsets)))
1020 ;; Attempt to reduce the number of charsets if utf-8 is available.
1021 (if (and (featurep 'xemacs)
1022 (> (length charsets) 1)
1023 (mm-coding-system-p 'utf-8))
1024 (let ((mm-coding-system-priorities
1025 (cons 'utf-8 mm-coding-system-priorities)))
1027 (mm-delete-duplicates
1028 (mapcar 'mm-mime-charset
1030 (mm-find-charset-region b e)))))))
1033 (defmacro mm-with-unibyte-buffer (&rest forms)
1034 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1035 Use unibyte mode for this."
1036 `(let (default-enable-multibyte-characters)
1037 (with-temp-buffer ,@forms)))
1038 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
1039 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
1041 (defmacro mm-with-multibyte-buffer (&rest forms)
1042 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1043 Use multibyte mode for this."
1044 `(let ((default-enable-multibyte-characters t))
1045 (with-temp-buffer ,@forms)))
1046 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
1047 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
1049 (defmacro mm-with-unibyte-current-buffer (&rest forms)
1050 "Evaluate FORMS with current buffer temporarily made unibyte.
1051 Also bind `default-enable-multibyte-characters' to nil.
1052 Equivalent to `progn' in XEmacs
1054 NOTE: Use this macro with caution in multibyte buffers (it is not
1055 worth using this macro in unibyte buffers of course). Use of
1056 `(set-buffer-multibyte t)', which is run finally, is generally
1057 harmful since it is likely to modify existing data in the buffer.
1058 For instance, it converts \"\\300\\255\" into \"\\255\" in
1059 Emacs 23 (unicode)."
1060 (let ((multibyte (make-symbol "multibyte"))
1061 (buffer (make-symbol "buffer")))
1063 (let ((,multibyte enable-multibyte-characters)
1064 (,buffer (current-buffer)))
1066 (let (default-enable-multibyte-characters)
1067 (set-buffer-multibyte nil)
1069 (set-buffer ,buffer)
1070 (set-buffer-multibyte ,multibyte)))
1071 (let (default-enable-multibyte-characters)
1073 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
1074 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
1076 (defmacro mm-with-unibyte (&rest forms)
1077 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
1078 `(let (default-enable-multibyte-characters)
1080 (put 'mm-with-unibyte 'lisp-indent-function 0)
1081 (put 'mm-with-unibyte 'edebug-form-spec '(body))
1083 (defmacro mm-with-multibyte (&rest forms)
1084 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
1085 `(let ((default-enable-multibyte-characters t))
1087 (put 'mm-with-multibyte 'lisp-indent-function 0)
1088 (put 'mm-with-multibyte 'edebug-form-spec '(body))
1090 (defun mm-find-charset-region (b e)
1091 "Return a list of Emacs charsets in the region B to E."
1093 ((and (mm-multibyte-p)
1094 (fboundp 'find-charset-region))
1095 ;; Remove composition since the base charsets have been included.
1096 ;; Remove eight-bit-*, treat them as ascii.
1097 (let ((css (find-charset-region b e)))
1099 '(composition eight-bit-control eight-bit-graphic control-1)
1101 (setq css (delq cs css)))))
1103 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
1106 (narrow-to-region b e)
1107 (goto-char (point-min))
1108 (skip-chars-forward "\0-\177")
1113 (and (boundp 'current-language-environment)
1114 (car (last (assq 'charset
1115 (assoc current-language-environment
1116 language-info-alist))))))
1117 (if (eq charset 'ascii) (setq charset nil))
1120 (car (last (assq mail-parse-charset
1121 mm-mime-mule-charset-alist)))))
1122 (list 'ascii (or charset 'latin-iso8859-1)))))))))
1124 (defun mm-auto-mode-alist ()
1125 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1126 (let ((alist auto-mode-alist)
1129 (when (listp (cdar alist))
1130 (push (car alist) out))
1134 (defvar mm-inhibit-file-name-handlers
1135 '(jka-compr-handler image-file-handler epa-file-handler)
1136 "A list of handlers doing (un)compression (etc) thingies.")
1138 (defun mm-insert-file-contents (filename &optional visit beg end replace
1140 "Like `insert-file-contents', but only reads in the file.
1141 A buffer may be modified in several ways after reading into the buffer due
1142 to advanced Emacs features, such as file-name-handlers, format decoding,
1143 `find-file-hooks', etc.
1144 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
1145 This function ensures that none of these modifications will take place."
1146 (let* ((format-alist nil)
1147 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
1148 (default-major-mode 'fundamental-mode)
1149 (enable-local-variables nil)
1150 (after-insert-file-functions nil)
1151 (enable-local-eval nil)
1152 (inhibit-file-name-operation (if inhibit
1153 'insert-file-contents
1154 inhibit-file-name-operation))
1155 (inhibit-file-name-handlers
1157 (append mm-inhibit-file-name-handlers
1158 inhibit-file-name-handlers)
1159 inhibit-file-name-handlers))
1160 (ffh (if (boundp 'find-file-hook)
1163 (val (symbol-value ffh)))
1166 (insert-file-contents filename visit beg end replace)
1169 (defun mm-append-to-file (start end filename &optional codesys inhibit)
1170 "Append the contents of the region to the end of file FILENAME.
1171 When called from a function, expects three arguments,
1172 START, END and FILENAME. START and END are buffer positions
1173 saying what text to write.
1174 Optional fourth argument specifies the coding system to use when
1176 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1177 (let ((coding-system-for-write
1178 (or codesys mm-text-coding-system-for-write
1179 mm-text-coding-system))
1180 (inhibit-file-name-operation (if inhibit
1182 inhibit-file-name-operation))
1183 (inhibit-file-name-handlers
1185 (append mm-inhibit-file-name-handlers
1186 inhibit-file-name-handlers)
1187 inhibit-file-name-handlers)))
1188 (write-region start end filename t 'no-message)
1189 (message "Appended to %s" filename)))
1191 (defun mm-write-region (start end filename &optional append visit lockname
1192 coding-system inhibit)
1194 "Like `write-region'.
1195 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1196 (let ((coding-system-for-write
1197 (or coding-system mm-text-coding-system-for-write
1198 mm-text-coding-system))
1199 (inhibit-file-name-operation (if inhibit
1201 inhibit-file-name-operation))
1202 (inhibit-file-name-handlers
1204 (append mm-inhibit-file-name-handlers
1205 inhibit-file-name-handlers)
1206 inhibit-file-name-handlers)))
1207 (write-region start end filename append visit lockname)))
1209 ;; It is not a MIME function, but some MIME functions use it.
1210 (if (and (fboundp 'make-temp-file)
1212 (let ((def (symbol-function 'make-temp-file)))
1213 (and (byte-code-function-p def)
1214 (setq def (if (fboundp 'compiled-function-arglist)
1216 (eval (list 'compiled-function-arglist def))
1219 (eq (nth 3 def) 'suffix)))))
1220 (defalias 'mm-make-temp-file 'make-temp-file)
1221 ;; Stolen (and modified for XEmacs) from Emacs 22.
1222 (defun mm-make-temp-file (prefix &optional dir-flag suffix)
1223 "Create a temporary file.
1224 The returned file name (created by appending some random characters at the end
1225 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1226 is guaranteed to point to a newly created empty file.
1227 You can then use `write-region' to write new data into the file.
1229 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1231 If SUFFIX is non-nil, add that at the end of the file name."
1232 (let ((umask (default-file-modes))
1236 ;; Create temp files with strict access rights. It's easy to
1237 ;; loosen them later, whereas it's impossible to close the
1238 ;; time-window of loose permissions otherwise.
1239 (set-default-file-modes 448)
1240 (while (condition-case err
1246 (if (fboundp 'temp-directory)
1249 temporary-file-directory))))
1251 (setq file (concat file suffix)))
1253 (make-directory file)
1254 ;; NOTE: This is unsafe if Emacs 20
1255 ;; users and XEmacs users don't use
1256 ;; a secure temp directory.
1257 (gmm-write-region "" nil file nil 'silent
1260 (file-already-exists t)
1261 ;; The XEmacs version of `make-directory' issues
1263 (file-error (or (and (featurep 'xemacs)
1264 (file-exists-p file))
1265 (signal (car err) (cdr err)))))
1266 ;; the file was somehow created by someone else between
1267 ;; `make-temp-name' and `write-region', let's try again.
1271 (set-default-file-modes umask)))))
1273 (defun mm-image-load-path (&optional package)
1275 (dolist (path load-path (nreverse result))
1278 (setq dir (concat (file-name-directory
1279 (directory-file-name path))
1280 "etc/images/" (or package "gnus/")))))
1282 (push path result))))
1284 ;; Fixme: This doesn't look useful where it's used.
1285 (if (fboundp 'detect-coding-region)
1286 (defun mm-detect-coding-region (start end)
1287 "Like `detect-coding-region' except returning the best one."
1288 (let ((coding-systems
1289 (detect-coding-region start end)))
1290 (or (car-safe coding-systems)
1292 (defun mm-detect-coding-region (start end)
1293 (let ((point (point)))
1295 (skip-chars-forward "\0-\177" end)
1297 (if (eq (point) end) 'ascii (mm-guess-charset))
1298 (goto-char point)))))
1300 (if (fboundp 'coding-system-get)
1301 (defun mm-detect-mime-charset-region (start end)
1302 "Detect MIME charset of the text in the region between START and END."
1303 (let ((cs (mm-detect-coding-region start end)))
1304 (or (coding-system-get cs :mime-charset)
1305 (coding-system-get cs 'mime-charset))))
1306 (defun mm-detect-mime-charset-region (start end)
1307 "Detect MIME charset of the text in the region between START and END."
1308 (let ((cs (mm-detect-coding-region start end)))
1312 (unless (fboundp 'coding-system-to-mime-charset)
1313 (defalias 'coding-system-to-mime-charset 'ignore)))
1315 (defun mm-coding-system-to-mime-charset (coding-system)
1316 "Return the MIME charset corresponding to CODING-SYSTEM.
1317 To make this function work with XEmacs, the APEL package is required."
1319 (or (and (fboundp 'coding-system-get)
1320 (or (coding-system-get coding-system :mime-charset)
1321 (coding-system-get coding-system 'mime-charset)))
1322 (and (featurep 'xemacs)
1323 (or (and (fboundp 'coding-system-to-mime-charset)
1324 (not (eq (symbol-function 'coding-system-to-mime-charset)
1326 (and (condition-case nil
1329 (fboundp 'coding-system-to-mime-charset)))
1330 (coding-system-to-mime-charset coding-system)))))
1333 (require 'jka-compr))
1335 (defun mm-decompress-buffer (filename &optional inplace force)
1336 "Decompress buffer's contents, depending on jka-compr.
1337 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
1338 agrees with `jka-compr-compression-info-list', decompression is done.
1339 Signal an error if FORCE is neither nil nor t and compressed data are
1340 not decompressed because `auto-compression-mode' is disabled.
1341 If INPLACE is nil, return decompressed data or nil without modifying
1342 the buffer. Otherwise, replace the buffer's contents with the
1343 decompressed data. The buffer's multibyteness must be turned off."
1346 (prog1 t (require 'jka-compr))
1347 (and (fboundp 'jka-compr-installed-p)
1348 (jka-compr-installed-p))))
1349 (let ((info (jka-compr-get-compression-info filename)))
1351 (unless (or (memq force (list nil t))
1352 (jka-compr-installed-p))
1354 (let ((prog (jka-compr-info-uncompress-program info))
1355 (args (jka-compr-info-uncompress-args info))
1356 (msg (format "%s %s..."
1357 (jka-compr-info-uncompress-message info)
1359 (err-file (jka-compr-make-temp-name))
1360 (cur (current-buffer))
1361 (coding-system-for-read mm-binary-coding-system)
1362 (coding-system-for-write mm-binary-coding-system)
1365 (mm-with-unibyte-buffer
1366 (insert-buffer-substring cur)
1369 (unless (memq (apply 'call-process-region
1370 (point-min) (point-max)
1371 prog t (list t err-file) nil args)
1372 jka-compr-acceptable-retval-list)
1376 (delete "" (split-string
1378 (insert-file-contents err-file)
1384 (format "Error while executing \"%s %s < %s\""
1385 prog (mapconcat 'identity args " ")
1387 (setq retval (buffer-string)))
1389 (setq err-msg (error-message-string err)))))
1390 (when (file-exists-p err-file)
1391 (ignore-errors (jka-compr-delete-temp-file err-file)))
1394 (delete-region (point-min) (point-max))
1397 (message "%s" (or err-msg (concat msg "done")))
1401 (unless (fboundp 'coding-system-name)
1402 (defalias 'coding-system-name 'ignore))
1403 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1404 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1405 (unless (fboundp 'find-operation-coding-system)
1406 (defalias 'find-operation-coding-system 'ignore)))
1408 (defun mm-find-buffer-file-coding-system (&optional filename)
1409 "Find coding system used to decode the contents of the current buffer.
1410 This function looks for the coding system magic cookie or examines the
1411 coding system specified by `file-coding-system-alist' being associated
1412 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1413 gzip, bzip2, etc. are allowed."
1415 (setq filename buffer-file-name))
1417 (let ((decomp (unless ;; No worth to examine charset of tar files.
1420 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1422 (mm-decompress-buffer filename nil t))))
1424 (set-buffer (let (default-enable-multibyte-characters)
1425 (generate-new-buffer " *temp*")))
1427 (setq filename (file-name-sans-extension filename)))
1428 (goto-char (point-min))
1431 ((boundp 'set-auto-coding-function) ;; Emacs
1433 (or (funcall (symbol-value 'set-auto-coding-function)
1434 filename (- (point-max) (point-min)))
1435 (car (find-operation-coding-system 'insert-file-contents
1437 (let (auto-coding-alist)
1439 (funcall (symbol-value 'set-auto-coding-function)
1440 nil (- (point-max) (point-min)))
1442 ((featurep 'file-coding) ;; XEmacs
1443 (let ((case-fold-search t)
1444 (end (point-at-eol))
1447 (and (re-search-forward "-\\*-+[\t ]*" end t)
1449 (setq start (match-end 0))
1450 (re-search-forward "[\t ]*-+\\*-" end t))
1452 (setq end (match-beginning 0))
1454 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1456 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1458 (find-coding-system (setq codesys
1459 (intern (match-string 1))))
1461 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1464 (setq start (match-end 0))
1465 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1467 (setq end (match-beginning 0))
1470 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1472 (find-coding-system (setq codesys
1473 (intern (match-string 1))))
1476 (goto-char (point-min))
1477 (setq case-fold-search nil)
1478 (re-search-forward "^;;;coding system: "
1479 ;;(+ (point-min) 3000) t))
1481 (looking-at "[^\t\n\r ]+")
1483 (setq codesys (intern (match-string 0))))
1487 (find-file-coding-system-for-read-from-filename
1489 (coding-system-name (coding-system-base codesys)))))))
1491 (kill-buffer (current-buffer)))))))
1495 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1496 ;;; mm-util.el ends here