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))
39 (defvar mm-mime-mule-charset-alist )
44 (let ((nfunc (intern (format "mm-%s" (car elem)))))
45 (if (fboundp (car elem))
46 (defalias nfunc (car elem))
47 (defalias nfunc (cdr elem)))))
48 '((coding-system-list . ignore)
50 (coding-system-equal . equal)
51 (annotationp . ignore)
52 (set-buffer-file-coding-system . ignore)
59 (mapcar (lambda (e) (list (symbol-name (car e))))
60 mm-mime-mule-charset-alist)
63 . (lambda (from to string &optional inplace)
64 ;; stolen (and renamed) from nnheader.el
65 "Replace characters in STRING from FROM to TO.
66 Unless optional argument INPLACE is non-nil, return a new string."
67 (let ((string (if inplace string (copy-sequence string)))
70 ;; Replace all occurrences of FROM with TO.
72 (when (= (aref string idx) from)
77 . (lambda (string regexp rep &optional literal)
78 "See `replace-regexp-in-string', only the order of args differs."
79 (replace-regexp-in-string regexp rep string nil literal)))
80 (string-as-unibyte . identity)
81 (string-make-unibyte . identity)
82 ;; string-as-multibyte often doesn't really do what you think it does.
84 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
85 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
86 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
87 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
89 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
90 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
91 ;; Better use string-to-multibyte or encode-coding-string.
92 ;; If you really need string-as-multibyte somewhere it's usually
93 ;; because you're using the internal emacs-mule representation (maybe
94 ;; because you're using string-as-unibyte somewhere), which is
95 ;; generally a problem in itself.
96 ;; Here is an approximate equivalence table to help think about it:
97 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
98 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
99 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
100 (string-as-multibyte . identity)
101 (multibyte-string-p . ignore)
102 (insert-byte . insert-char)
103 (multibyte-char-to-unibyte . identity)
105 . (lambda (buffer-name)
106 "Returns non-nil if a buffer named BUFFER-NAME gets a special frame."
107 (and special-display-function
108 (or (and (member buffer-name special-display-buffer-names) t)
109 (cdr (assoc buffer-name special-display-buffer-names))
111 (dolist (elem special-display-regexps)
113 (string-match elem buffer-name)
117 (string-match (car elem) buffer-name)
118 (throw 'return (cdr elem))))))))))))
121 (if (featurep 'xemacs)
122 (if (featurep 'file-coding)
123 ;; Don't modify string if CODING-SYSTEM is nil.
125 (defun mm-decode-coding-string (str coding-system)
127 (decode-coding-string str coding-system)
129 (defun mm-encode-coding-string (str coding-system)
131 (encode-coding-string str coding-system)
133 (defun mm-decode-coding-region (start end coding-system)
135 (decode-coding-region start end coding-system)))
136 (defun mm-encode-coding-region (start end coding-system)
138 (encode-coding-region start end coding-system))))
139 (defun mm-decode-coding-string (str coding-system) str)
140 (defun mm-encode-coding-string (str coding-system) str)
141 (defalias 'mm-decode-coding-region 'ignore)
142 (defalias 'mm-encode-coding-region 'ignore))
143 (defalias 'mm-decode-coding-string 'decode-coding-string)
144 (defalias 'mm-encode-coding-string 'encode-coding-string)
145 (defalias 'mm-decode-coding-region 'decode-coding-region)
146 (defalias 'mm-encode-coding-region 'encode-coding-region)))
148 (defalias 'mm-string-to-multibyte
152 ((fboundp 'string-to-multibyte)
153 'string-to-multibyte)
156 "Return a multibyte string with the same individual chars as string."
158 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
162 (defalias 'mm-char-or-char-int-p
164 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
165 ((fboundp 'char-valid-p) 'char-valid-p)
168 ;; Fixme: This seems always to be used to read a MIME charset, so it
169 ;; should be re-named and fixed (in Emacs) to offer completion only on
170 ;; proper charset names (base coding systems which have a
171 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
173 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
174 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
175 ;; Actually, there should be an `mm-coding-system-mime-charset'.
177 (defalias 'mm-read-coding-system
179 ((fboundp 'read-coding-system)
180 (if (and (featurep 'xemacs)
181 (<= (string-to-number emacs-version) 21.1))
182 (lambda (prompt &optional default-coding-system)
183 (read-coding-system prompt))
184 'read-coding-system))
185 (t (lambda (prompt &optional default-coding-system)
186 "Prompt the user for a coding system."
188 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
189 mm-mime-mule-charset-alist)))))))
191 (defvar mm-coding-system-list nil)
192 (defun mm-get-coding-system-list ()
193 "Get the coding system list."
194 (or mm-coding-system-list
195 (setq mm-coding-system-list (mm-coding-system-list))))
197 (defun mm-coding-system-p (cs)
198 "Return non-nil if CS is a symbol naming a coding system.
199 In XEmacs, also return non-nil if CS is a coding system object.
200 If CS is available, return CS itself in Emacs, and return a coding
201 system object in XEmacs."
202 (if (fboundp 'find-coding-system)
203 (and cs (find-coding-system cs))
204 (if (fboundp 'coding-system-p)
205 (when (coding-system-p cs)
208 (car (memq cs (mm-get-coding-system-list))))))
210 (defun mm-codepage-setup (number &optional alias)
211 "Create a coding system cpNUMBER.
212 The coding system is created using `codepage-setup'. If ALIAS is
213 non-nil, an alias is created and added to
214 `mm-charset-synonym-alist'. If ALIAS is a string, it's used as
215 the alias. Else windows-NUMBER is used."
217 (let ((completion-ignore-case t)
218 (candidates (cp-supported-codepages)))
219 (list (completing-read "Setup DOS Codepage: (default 437) " candidates
220 nil t nil nil "437"))))
222 (setq alias (if (stringp alias)
224 (intern (format "windows-%s" number)))))
225 (let* ((cp (intern (format "cp%s" number))))
226 (unless (mm-coding-system-p cp)
227 (codepage-setup number))
229 ;; Don't add alias if setup of cp failed.
230 (mm-coding-system-p cp))
231 (add-to-list 'mm-charset-synonym-alist (cons alias cp)))))
233 (defvar mm-charset-synonym-alist
235 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
236 ,@(unless (mm-coding-system-p 'x-ctext)
237 '((x-ctext . ctext)))
238 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_ in 8
240 ,@(unless (mm-coding-system-p 'iso-8859-15)
241 '((iso-8859-15 . iso-8859-1)))
242 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
243 ,@(unless (mm-coding-system-p 'big5-hkscs)
244 '((big5-hkscs . big5)))
245 ;; A Microsoft misunderstanding.
246 ,@(when (and (not (mm-coding-system-p 'unicode))
247 (mm-coding-system-p 'utf-16-le))
248 '((unicode . utf-16-le)))
249 ;; A Microsoft misunderstanding.
250 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
251 (if (mm-coding-system-p 'cp949)
252 '((ks_c_5601-1987 . cp949))
253 '((ks_c_5601-1987 . euc-kr))))
254 ;; Windows-31J is Windows Codepage 932.
255 ,@(when (and (not (mm-coding-system-p 'windows-31j))
256 (mm-coding-system-p 'cp932))
257 '((windows-31j . cp932)))
258 ;; Charset name: GBK, Charset aliases: CP936, MS936, windows-936
259 ;; http://www.iana.org/assignments/charset-reg/GBK
260 ;; Emacs 22.1 has cp936, but not gbk, so we alias it:
261 ,@(when (and (not (mm-coding-system-p 'gbk))
262 (mm-coding-system-p 'cp936))
264 ;; ISO8859-1 is a bogus name for ISO-8859-1
265 ,@(when (and (not (mm-coding-system-p 'iso8859-1))
266 (mm-coding-system-p 'iso-8859-1))
267 '((iso8859-1 . iso-8859-1)))
269 "A mapping from unknown or invalid charset names to the real charset names.
271 See `mm-codepage-iso-8859-list' and `mm-codepage-ibm-list'.")
273 (defcustom mm-codepage-iso-8859-list
274 (list 1250 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
275 ;; Outlook users in Czech republic. Use this to allow reading of
276 ;; their e-mails. cp1250 should be defined by M-x codepage-setup
278 '(1252 . 1) ;; Windows-1252 is a superset of iso-8859-1 (West
279 ;; Europe). See also `gnus-article-dumbquotes-map'.
280 '(1254 . 9) ;; Windows-1254 is a superset of iso-8859-9 (Turkish).
281 '(1255 . 8));; Windows-1255 is a superset of iso-8859-8 (Hebrew).
282 "A list of Windows codepage numbers and iso-8859 charset numbers.
284 If an element is a number corresponding to a supported windows
285 codepage, appropriate entries to `mm-charset-synonym-alist' are
286 added by `mm-setup-codepage-iso-8859'. An element may also be a
287 cons cell where the car is a codepage number and the cdr is the
288 corresponding number of an iso-8859 charset."
289 :type '(list (set :inline t
290 (const 1250 :tag "Central and East European")
291 (const (1252 . 1) :tag "West European")
292 (const (1254 . 9) :tag "Turkish")
293 (const (1255 . 8) :tag "Hebrew"))
297 (integer :tag "Windows codepage number")
298 (cons (integer :tag "Windows codepage number")
299 (integer :tag "iso-8859 charset number")))))
300 :version "22.1" ;; Gnus 5.10.9
303 (defcustom mm-codepage-ibm-list
304 (list 437 ;; (US etc.)
308 863 ;; (Canadian French)
313 866 ;; (Cyrillic - Russian)
318 ;; In Emacs 23 (unicode), cp... and ibm... are aliases.
319 ;; Cf. http://thread.gmane.org/v9lkng5nwy.fsf@marauder.physik.uni-ulm.de
320 "List of IBM codepage numbers.
322 The codepage mappings slighly differ between IBM and other vendors.
323 See \"ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/IBM/README.TXT\".
325 If an element is a number corresponding to a supported windows
326 codepage, appropriate entries to `mm-charset-synonym-alist' are
327 added by `mm-setup-codepage-ibm'."
328 :type '(list (set :inline t
329 (const 437 :tag "US etc.")
330 (const 860 :tag "Portugal")
331 (const 861 :tag "Iceland")
332 (const 862 :tag "Israel")
333 (const 863 :tag "Canadian French")
334 (const 865 :tag "Nordic")
336 (const 850 :tag "Latin 1")
337 (const 855 :tag "Cyrillic")
338 (const 866 :tag "Cyrillic - Russian")
339 (const 857 :tag "Turkish")
340 (const 864 :tag "Arabic")
341 (const 869 :tag "Greek")
342 (const 874 :tag "Thai"))
345 (integer :tag "Codepage number")))
346 :version "22.1" ;; Gnus 5.10.9
349 (defun mm-setup-codepage-iso-8859 (&optional list)
350 "Add appropriate entries to `mm-charset-synonym-alist'.
351 Unless LIST is given, `mm-codepage-iso-8859-list' is used."
353 (setq list mm-codepage-iso-8859-list))
355 (let (cp windows iso)
357 (setq cp (intern (format "cp%d" (car i)))
358 windows (intern (format "windows-%d" (car i)))
359 iso (intern (format "iso-8859-%d" (cdr i))))
360 (setq cp (intern (format "cp%d" i))
361 windows (intern (format "windows-%d" i))))
362 (unless (mm-coding-system-p windows)
363 (if (mm-coding-system-p cp)
364 (add-to-list 'mm-charset-synonym-alist (cons windows cp))
365 (add-to-list 'mm-charset-synonym-alist (cons windows iso)))))))
367 (defun mm-setup-codepage-ibm (&optional list)
368 "Add appropriate entries to `mm-charset-synonym-alist'.
369 Unless LIST is given, `mm-codepage-ibm-list' is used."
371 (setq list mm-codepage-ibm-list))
372 (dolist (number list)
373 (let ((ibm (intern (format "ibm%d" number)))
374 (cp (intern (format "cp%d" number))))
375 (when (and (not (mm-coding-system-p ibm))
376 (mm-coding-system-p cp))
377 (add-to-list 'mm-charset-synonym-alist (cons ibm cp))))))
380 (mm-setup-codepage-iso-8859)
381 (mm-setup-codepage-ibm)
383 (defcustom mm-charset-override-alist
384 '((iso-8859-1 . windows-1252)
385 (iso-8859-8 . windows-1255)
386 (iso-8859-9 . windows-1254))
387 "A mapping from undesired charset names to their replacement.
389 You may add pairs like (iso-8859-1 . windows-1252) here,
390 i.e. treat iso-8859-1 as windows-1252. windows-1252 is a
391 superset of iso-8859-1."
392 :type '(list (set :inline t
393 (const (iso-8859-1 . windows-1252))
394 (const (iso-8859-8 . windows-1255))
395 (const (iso-8859-9 . windows-1254))
396 (const (undecided . windows-1252)))
399 (cons (symbol :tag "From charset")
400 (symbol :tag "To charset"))))
401 :version "22.1" ;; Gnus 5.10.9
404 (defcustom mm-charset-eval-alist
405 (if (featurep 'xemacs)
406 nil ;; I don't know what would be useful for XEmacs.
407 '(;; Emacs 21 offers 1250 1251 1253 1257. Emacs 22 provides autoloads for
408 ;; 1250-1258 (i.e. `mm-codepage-setup' does nothing).
409 (windows-1250 . (mm-codepage-setup 1250 t))
410 (windows-1251 . (mm-codepage-setup 1251 t))
411 (windows-1253 . (mm-codepage-setup 1253 t))
412 (windows-1257 . (mm-codepage-setup 1257 t))))
413 "An alist of (CHARSET . FORM) pairs.
414 If an article is encoded in an unknown CHARSET, FORM is
415 evaluated. This allows to load additional libraries providing
416 charsets on demand. If supported by your Emacs version, you
417 could use `autoload-coding-system' here."
418 :version "22.1" ;; Gnus 5.10.9
419 :type '(list (set :inline t
420 (const (windows-1250 . (mm-codepage-setup 1250 t)))
421 (const (windows-1251 . (mm-codepage-setup 1251 t)))
422 (const (windows-1253 . (mm-codepage-setup 1253 t)))
423 (const (windows-1257 . (mm-codepage-setup 1257 t)))
424 (const (cp850 . (mm-codepage-setup 850 nil))))
427 (cons (symbol :tag "charset")
428 (symbol :tag "form"))))
430 (put 'mm-charset-eval-alist 'risky-local-variable t)
432 (defvar mm-binary-coding-system
434 ((mm-coding-system-p 'binary) 'binary)
435 ((mm-coding-system-p 'no-conversion) 'no-conversion)
437 "100% binary coding system.")
439 (defvar mm-text-coding-system
440 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
441 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
442 (and (mm-coding-system-p 'raw-text) 'raw-text))
443 mm-binary-coding-system)
444 "Text-safe coding system (For removing ^M).")
446 (defvar mm-text-coding-system-for-write nil
447 "Text coding system for write.")
449 (defvar mm-auto-save-coding-system
451 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
452 (if (memq system-type '(windows-nt ms-dos ms-windows))
453 (if (mm-coding-system-p 'utf-8-emacs-dos)
454 'utf-8-emacs-dos mm-binary-coding-system)
456 ((mm-coding-system-p 'emacs-mule)
457 (if (memq system-type '(windows-nt ms-dos ms-windows))
458 (if (mm-coding-system-p 'emacs-mule-dos)
459 'emacs-mule-dos mm-binary-coding-system)
461 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
462 (t mm-binary-coding-system))
463 "Coding system of auto save file.")
465 (defvar mm-universal-coding-system mm-auto-save-coding-system
466 "The universal coding system.")
468 ;; Fixme: some of the cars here aren't valid MIME charsets. That
469 ;; should only matter with XEmacs, though.
470 (defvar mm-mime-mule-charset-alist
472 (iso-8859-1 latin-iso8859-1)
473 (iso-8859-2 latin-iso8859-2)
474 (iso-8859-3 latin-iso8859-3)
475 (iso-8859-4 latin-iso8859-4)
476 (iso-8859-5 cyrillic-iso8859-5)
477 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
478 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
479 ;; charset is koi8-r, not iso-8859-5.
480 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
481 (iso-8859-6 arabic-iso8859-6)
482 (iso-8859-7 greek-iso8859-7)
483 (iso-8859-8 hebrew-iso8859-8)
484 (iso-8859-9 latin-iso8859-9)
485 (iso-8859-14 latin-iso8859-14)
486 (iso-8859-15 latin-iso8859-15)
487 (viscii vietnamese-viscii-lower)
488 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
489 (euc-kr korean-ksc5601)
490 (gb2312 chinese-gb2312)
491 (big5 chinese-big5-1 chinese-big5-2)
493 (thai-tis620 thai-tis620)
494 (windows-1251 cyrillic-iso8859-5)
495 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
496 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
497 latin-jisx0201 japanese-jisx0208-1978
498 chinese-gb2312 japanese-jisx0208
499 korean-ksc5601 japanese-jisx0212)
500 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
501 latin-jisx0201 japanese-jisx0208-1978
502 chinese-gb2312 japanese-jisx0208
503 korean-ksc5601 japanese-jisx0212
504 chinese-cns11643-1 chinese-cns11643-2)
505 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
506 cyrillic-iso8859-5 greek-iso8859-7
507 latin-jisx0201 japanese-jisx0208-1978
508 chinese-gb2312 japanese-jisx0208
509 korean-ksc5601 japanese-jisx0212
510 chinese-cns11643-1 chinese-cns11643-2
511 chinese-cns11643-3 chinese-cns11643-4
512 chinese-cns11643-5 chinese-cns11643-6
514 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
515 japanese-jisx0213-1 japanese-jisx0213-2)
516 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
517 ,(cond ((fboundp 'unicode-precedence-list)
518 (cons 'utf-8 (delq 'ascii (mapcar 'charset-name
519 (unicode-precedence-list)))))
520 ((or (not (fboundp 'charsetp)) ;; non-Mule case
521 (charsetp 'unicode-a)
522 (not (mm-coding-system-p 'mule-utf-8)))
523 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
524 (t ;; If we have utf-8 we're in Mule 5+.
527 (coding-system-get 'mule-utf-8 'safe-charsets))))))
528 "Alist of MIME-charset/MULE-charsets.")
530 (defun mm-enrich-utf-8-by-mule-ucs ()
531 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
532 This function will run when the `un-define' module is loaded under
533 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
534 with Mule charsets. It is completely useless for Emacs."
535 (when (boundp 'unicode-basic-translation-charset-order-list)
541 'unicode-basic-translation-charset-order-list))))
542 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
545 (setq mm-mime-mule-charset-alist
546 (nconc mm-mime-mule-charset-alist
547 (list (cons 'utf-8 val))))))
550 ;; Correct by construction, but should be unnecessary for Emacs:
551 (if (featurep 'xemacs)
552 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
553 (when (and (fboundp 'coding-system-list)
554 (fboundp 'sort-coding-systems))
555 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
559 mime (or (coding-system-get cs :mime-charset) ; Emacs 23 (unicode)
560 (coding-system-get cs 'mime-charset)))
562 (not (eq t (setq mule
563 (coding-system-get cs 'safe-charsets))))
564 (not (assq mime alist)))
565 (push (cons mime (delq 'ascii mule)) alist)))
566 (setq mm-mime-mule-charset-alist (nreverse alist)))))
568 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
569 "A list of special charsets.
570 Valid elements include:
571 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
572 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
575 (defvar mm-iso-8859-15-compatible
576 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
577 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
578 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
580 (defvar mm-iso-8859-x-to-15-table
581 (and (fboundp 'coding-system-p)
582 (mm-coding-system-p 'iso-8859-15)
585 (if (mm-coding-system-p (car cs))
586 (let ((c (string-to-char
587 (decode-coding-string "\341" (car cs)))))
588 (cons (char-charset c)
591 (decode-coding-string "\341" 'iso-8859-15)) c)
592 (string-to-list (decode-coding-string (car (cdr cs))
595 mm-iso-8859-15-compatible))
596 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
598 (defcustom mm-coding-system-priorities
599 (if (boundp 'current-language-environment)
600 (let ((lang (symbol-value 'current-language-environment)))
601 (cond ((string= lang "Japanese")
602 ;; Japanese users prefer iso-2022-jp to euc-japan or
603 ;; shift_jis, however iso-8859-1 should be used when
604 ;; there are only ASCII text and Latin-1 characters.
605 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
606 "Preferred coding systems for encoding outgoing messages.
608 More than one suitable coding system may be found for some text.
609 By default, the coding system with the highest priority is used
610 to encode outgoing messages (see `sort-coding-systems'). If this
611 variable is set, it overrides the default priority."
613 :type '(repeat (symbol :tag "Coding system"))
617 (defvar mm-use-find-coding-systems-region
618 (fboundp 'find-coding-systems-region)
619 "Use `find-coding-systems-region' to find proper coding systems.
621 Setting it to nil is useful on Emacsen supporting Unicode if sending
622 mail with multiple parts is preferred to sending a Unicode one.")
624 ;;; Internal variables:
628 (defun mm-mule-charset-to-mime-charset (charset)
629 "Return the MIME charset corresponding to the given Mule CHARSET."
630 (if (and (fboundp 'find-coding-systems-for-charsets)
631 (fboundp 'sort-coding-systems))
632 (let ((css (sort (sort-coding-systems
633 (find-coding-systems-for-charsets (list charset)))
634 'mm-sort-coding-systems-predicate))
636 (while (and (not mime)
638 (when (setq cs (pop css))
639 (setq mime (or (coding-system-get cs :mime-charset)
640 (coding-system-get cs 'mime-charset)))))
642 (let ((alist (mapcar (lambda (cs)
643 (assq cs mm-mime-mule-charset-alist))
644 (sort (mapcar 'car mm-mime-mule-charset-alist)
645 'mm-sort-coding-systems-predicate)))
648 (when (memq charset (cdar alist))
649 (setq out (caar alist)
654 (defun mm-charset-to-coding-system (charset &optional lbt
656 "Return coding-system corresponding to CHARSET.
657 CHARSET is a symbol naming a MIME charset.
658 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
659 used as the line break code type of the coding system.
661 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
662 map undesired charset names to their replacement. This should
663 only be used for decoding, not for encoding."
664 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
665 (when (stringp charset)
666 (setq charset (intern (downcase charset))))
668 (setq charset (intern (format "%s-%s" charset lbt))))
672 ;; Running in a non-MULE environment.
673 ((or (null (mm-get-coding-system-list))
674 (not (fboundp 'coding-system-get)))
676 ;; Check override list quite early. Should only used for decoding, not for
679 (let ((cs (cdr (assq charset mm-charset-override-alist))))
680 (and cs (mm-coding-system-p cs) cs))))
682 ((eq charset 'us-ascii)
684 ;; Check to see whether we can handle this charset. (This depends
685 ;; on there being some coding system matching each `mime-charset'
686 ;; property defined, as there should be.)
687 ((and (mm-coding-system-p charset)
688 ;;; Doing this would potentially weed out incorrect charsets.
690 ;;; (eq charset (coding-system-get charset 'mime-charset))
693 ;; Eval expressions from `mm-charset-eval-alist'
694 ((let* ((el (assq charset mm-charset-eval-alist))
701 (condition-case nil (eval form) (error nil))
702 ;; (message "Failed to eval `%s'" form))
703 (mm-coding-system-p cs)
704 (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
706 ;; Translate invalid charsets.
707 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
709 (mm-coding-system-p cs)
711 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
714 ;; Last resort: search the coding system list for entries which
715 ;; have the right mime-charset in case the canonical name isn't
716 ;; defined (though it should be).
718 ;; mm-get-coding-system-list returns a list of cs without lbt.
720 (dolist (c (mm-get-coding-system-list))
722 (eq charset (or (coding-system-get c :mime-charset)
723 (coding-system-get c 'mime-charset))))
726 ;; Warn the user about unknown charset:
727 (if (fboundp 'gnus-message)
728 (gnus-message 7 "Unknown charset: %s" charset)
729 (message "Unknown charset: %s" charset)))
733 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
734 (boundp 'default-enable-multibyte-characters)
735 default-enable-multibyte-characters
736 (fboundp 'set-buffer-multibyte))
737 "True in Emacs with Mule.")
740 (defun mm-enable-multibyte ()
741 "Set the multibyte flag of the current buffer.
742 Only do this if the default value of `enable-multibyte-characters' is
743 non-nil. This is a no-op in XEmacs."
744 (set-buffer-multibyte 'to))
745 (defalias 'mm-enable-multibyte 'ignore))
748 (defun mm-disable-multibyte ()
749 "Unset the multibyte flag of in the current buffer.
750 This is a no-op in XEmacs."
751 (set-buffer-multibyte nil))
752 (defalias 'mm-disable-multibyte 'ignore)))
754 (defun mm-preferred-coding-system (charset)
755 ;; A typo in some Emacs versions.
756 (or (get-charset-property charset 'preferred-coding-system)
757 (get-charset-property charset 'prefered-coding-system)))
759 ;; Mule charsets shouldn't be used.
760 (defsubst mm-guess-charset ()
761 "Guess Mule charset from the language environment."
763 mail-parse-mule-charset ;; cached mule-charset
765 (setq mail-parse-mule-charset
766 (and (boundp 'current-language-environment)
769 (assoc current-language-environment
770 language-info-alist))))))
771 (if (or (not mail-parse-mule-charset)
772 (eq mail-parse-mule-charset 'ascii))
773 (setq mail-parse-mule-charset
774 (or (car (last (assq mail-parse-charset
775 mm-mime-mule-charset-alist)))
778 mail-parse-mule-charset)))
780 (defun mm-charset-after (&optional pos)
781 "Return charset of a character in current buffer at position POS.
782 If POS is nil, it defauls to the current point.
783 If POS is out of range, the value is nil.
784 If the charset is `composition', return the actual one."
785 (let ((char (char-after pos)) charset)
786 (if (< (mm-char-int char) 128)
787 (setq charset 'ascii)
788 ;; charset-after is fake in some Emacsen.
789 (setq charset (and (fboundp 'char-charset) (char-charset char)))
790 (if (eq charset 'composition) ; Mule 4
791 (let ((p (or pos (point))))
792 (cadr (find-charset-region p (1+ p))))
793 (if (and charset (not (memq charset '(ascii eight-bit-control
794 eight-bit-graphic))))
796 (mm-guess-charset))))))
798 (defun mm-mime-charset (charset)
799 "Return the MIME charset corresponding to the given Mule CHARSET."
800 (if (eq charset 'unknown)
801 (error "The message contains non-printable characters, please use attachment"))
802 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
803 ;; This exists in Emacs 20.
805 (and (mm-preferred-coding-system charset)
806 (or (coding-system-get
807 (mm-preferred-coding-system charset) :mime-charset)
809 (mm-preferred-coding-system charset) 'mime-charset)))
810 (and (eq charset 'ascii)
812 (mm-preferred-coding-system charset)
813 (mm-mule-charset-to-mime-charset charset))
814 ;; This is for XEmacs.
815 (mm-mule-charset-to-mime-charset charset)))
817 (if (fboundp 'delete-dups)
818 (defalias 'mm-delete-duplicates 'delete-dups)
819 (defun mm-delete-duplicates (list)
820 "Destructively remove `equal' duplicates from LIST.
821 Store the result in LIST and return it. LIST must be a proper list.
822 Of several `equal' occurrences of an element in LIST, the first
825 This is a compatibility function for Emacsen without `delete-dups'."
826 ;; Code from `subr.el' in Emacs 22:
829 (setcdr tail (delete (car tail) (cdr tail)))
830 (setq tail (cdr tail))))
833 ;; Fixme: This is used in places when it should be testing the
834 ;; default multibyteness. See mm-default-multibyte-p.
836 (if (and (not (featurep 'xemacs))
837 (boundp 'enable-multibyte-characters))
838 (defun mm-multibyte-p ()
839 "Non-nil if multibyte is enabled in the current buffer."
840 enable-multibyte-characters)
841 (defun mm-multibyte-p () (featurep 'mule))))
843 (defun mm-default-multibyte-p ()
844 "Return non-nil if the session is multibyte.
845 This affects whether coding conversion should be attempted generally."
847 (if (boundp 'default-enable-multibyte-characters)
848 default-enable-multibyte-characters
851 (defun mm-iso-8859-x-to-15-region (&optional b e)
852 (if (fboundp 'char-charset)
853 (let (charset item c inconvertible)
855 (if e (narrow-to-region b e))
856 (goto-char (point-min))
857 (skip-chars-forward "\0-\177")
860 ((not (setq item (assq (char-charset (setq c (char-after)))
861 mm-iso-8859-x-to-15-table)))
863 ((memq c (cdr (cdr item)))
864 (setq inconvertible t)
867 (insert-before-markers (prog1 (+ c (car (cdr item)))
869 (skip-chars-forward "\0-\177")))
870 (not inconvertible))))
872 (defun mm-sort-coding-systems-predicate (a b)
875 ;; Note: invalid entries are dropped silently
876 (and (setq cs (mm-coding-system-p cs))
877 (coding-system-base cs)))
878 mm-coding-system-priorities)))
879 (and (setq a (mm-coding-system-p a))
880 (if (setq b (mm-coding-system-p b))
881 (> (length (memq (coding-system-base a) priorities))
882 (length (memq (coding-system-base b) priorities)))
886 (autoload 'latin-unity-massage-name "latin-unity")
887 (autoload 'latin-unity-maybe-remap "latin-unity")
888 (autoload 'latin-unity-representations-feasible-region "latin-unity")
889 (autoload 'latin-unity-representations-present-region "latin-unity"))
891 (defvar latin-unity-coding-systems)
892 (defvar latin-unity-ucs-list)
894 (defun mm-xemacs-find-mime-charset-1 (begin end)
895 "Determine which MIME charset to use to send region as message.
896 This uses the XEmacs-specific latin-unity package to better handle the
897 case where identical characters from diverse ISO-8859-? character sets
898 can be encoded using a single one of the corresponding coding systems.
900 It treats `mm-coding-system-priorities' as the list of preferred
901 coding systems; a useful example setting for this list in Western
902 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
903 to the very standard Latin 1 coding system, and only move to coding
904 systems that are less supported as is necessary to encode the
905 characters that exist in the buffer.
907 Latin Unity doesn't know about those non-ASCII Roman characters that
908 are available in various East Asian character sets. As such, its
909 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
910 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
911 But this is very much a corner case, so don't worry about it."
912 (let ((systems mm-coding-system-priorities) csets psets curset)
914 ;; Load the Latin Unity library, if available.
915 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
916 (require 'latin-unity))
918 ;; Now, can we use it?
919 (if (featurep 'latin-unity)
921 (setq csets (latin-unity-representations-feasible-region begin end)
922 psets (latin-unity-representations-present-region begin end))
926 ;; Pass back the first coding system in the preferred list
927 ;; that can encode the whole region.
928 (dolist (curset systems)
929 (setq curset (latin-unity-massage-name 'buffer-default curset))
931 ;; If the coding system is a universal coding system, then
932 ;; it can certainly encode all the characters in the region.
933 (if (memq curset latin-unity-ucs-list)
934 (throw 'done (list curset)))
936 ;; If a coding system isn't universal, and isn't in
937 ;; the list that latin unity knows about, we can't
938 ;; decide whether to use it here. Leave that until later
939 ;; in `mm-find-mime-charset-region' function, whence we
941 (unless (memq curset latin-unity-coding-systems)
944 ;; Right, we know about this coding system, and it may
945 ;; conceivably be able to encode all the characters in
947 (if (latin-unity-maybe-remap begin end curset csets psets t)
948 (throw 'done (list curset))))
950 ;; Can't encode using anything from the
951 ;; `mm-coding-system-priorities' list.
952 ;; Leave `mm-find-mime-charset' to do most of the work.
955 ;; Right, latin unity isn't available; let `mm-find-charset-region'
956 ;; take its default action, which equally applies to GNU Emacs.
959 (defmacro mm-xemacs-find-mime-charset (begin end)
960 (when (featurep 'xemacs)
961 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
963 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
964 "Return the MIME charsets needed to encode the region between B and E.
965 nil means ASCII, a single-element list represents an appropriate MIME
966 charset, and a longer list means no appropriate charset."
968 ;; The return possibilities of this function are a mess...
969 (or (and (mm-multibyte-p)
970 mm-use-find-coding-systems-region
971 ;; Find the mime-charset of the most preferred coding
972 ;; system that has one.
973 (let ((systems (find-coding-systems-region b e)))
974 (when mm-coding-system-priorities
976 (sort systems 'mm-sort-coding-systems-predicate)))
977 (setq systems (delq 'compound-text systems))
978 (unless (equal systems '(undecided))
980 (let* ((head (pop systems))
981 (cs (or (coding-system-get head :mime-charset)
982 (coding-system-get head 'mime-charset))))
983 ;; The mime-charset (`x-ctext') of
984 ;; `compound-text' is not in the IANA list. We
985 ;; shouldn't normally use anything here with a
986 ;; mime-charset having an `x-' prefix.
987 ;; Fixme: Allow this to be overridden, since
988 ;; there is existing use of x-ctext.
989 ;; Also people apparently need the coding system
990 ;; `iso-2022-jp-3' (which Mule-UCS defines with
991 ;; mime-charset, though it's not valid).
993 (not (string-match "^[Xx]-" (symbol-name cs)))
994 ;; UTF-16 of any variety is invalid for
995 ;; text parts and, unfortunately, has
996 ;; mime-charset defined both in Mule-UCS
997 ;; and versions of Emacs. (The name
998 ;; might be `mule-utf-16...' or
1000 (not (string-match "utf-16" (symbol-name cs))))
1002 charsets (list cs))))))
1004 ;; If we're XEmacs, and some coding system is appropriate,
1005 ;; mm-xemacs-find-mime-charset will return an appropriate list.
1006 ;; Otherwise, we'll get nil, and the next setq will get invoked.
1007 (setq charsets (mm-xemacs-find-mime-charset b e))
1009 ;; We're not multibyte, or a single coding system won't cover it.
1011 (mm-delete-duplicates
1012 (mapcar 'mm-mime-charset
1014 (mm-find-charset-region b e))))))
1015 (if (and (> (length charsets) 1)
1016 (memq 'iso-8859-15 charsets)
1017 (memq 'iso-8859-15 hack-charsets)
1018 (save-excursion (mm-iso-8859-x-to-15-region b e)))
1019 (dolist (x mm-iso-8859-15-compatible)
1020 (setq charsets (delq (car x) charsets))))
1021 (if (and (memq 'iso-2022-jp-2 charsets)
1022 (memq 'iso-2022-jp-2 hack-charsets))
1023 (setq charsets (delq 'iso-2022-jp charsets)))
1024 ;; Attempt to reduce the number of charsets if utf-8 is available.
1025 (if (and (featurep 'xemacs)
1026 (> (length charsets) 1)
1027 (mm-coding-system-p 'utf-8))
1028 (let ((mm-coding-system-priorities
1029 (cons 'utf-8 mm-coding-system-priorities)))
1031 (mm-delete-duplicates
1032 (mapcar 'mm-mime-charset
1034 (mm-find-charset-region b e)))))))
1037 (defmacro mm-with-unibyte-buffer (&rest forms)
1038 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1039 Use unibyte mode for this."
1040 `(let (default-enable-multibyte-characters)
1041 (with-temp-buffer ,@forms)))
1042 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
1043 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
1045 (defmacro mm-with-multibyte-buffer (&rest forms)
1046 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1047 Use multibyte mode for this."
1048 `(let ((default-enable-multibyte-characters t))
1049 (with-temp-buffer ,@forms)))
1050 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
1051 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
1053 (defmacro mm-with-unibyte-current-buffer (&rest forms)
1054 "Evaluate FORMS with current buffer temporarily made unibyte.
1055 Also bind `default-enable-multibyte-characters' to nil.
1056 Equivalent to `progn' in XEmacs
1058 NOTE: Use this macro with caution in multibyte buffers (it is not
1059 worth using this macro in unibyte buffers of course). Use of
1060 `(set-buffer-multibyte t)', which is run finally, is generally
1061 harmful since it is likely to modify existing data in the buffer.
1062 For instance, it converts \"\\300\\255\" into \"\\255\" in
1063 Emacs 23 (unicode)."
1064 (let ((multibyte (make-symbol "multibyte"))
1065 (buffer (make-symbol "buffer")))
1067 (let ((,multibyte enable-multibyte-characters)
1068 (,buffer (current-buffer)))
1070 (let (default-enable-multibyte-characters)
1071 (set-buffer-multibyte nil)
1073 (set-buffer ,buffer)
1074 (set-buffer-multibyte ,multibyte)))
1075 (let (default-enable-multibyte-characters)
1077 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
1078 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
1080 (defmacro mm-with-unibyte (&rest forms)
1081 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
1082 `(let (default-enable-multibyte-characters)
1084 (put 'mm-with-unibyte 'lisp-indent-function 0)
1085 (put 'mm-with-unibyte 'edebug-form-spec '(body))
1087 (defmacro mm-with-multibyte (&rest forms)
1088 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
1089 `(let ((default-enable-multibyte-characters t))
1091 (put 'mm-with-multibyte 'lisp-indent-function 0)
1092 (put 'mm-with-multibyte 'edebug-form-spec '(body))
1094 (defun mm-find-charset-region (b e)
1095 "Return a list of Emacs charsets in the region B to E."
1097 ((and (mm-multibyte-p)
1098 (fboundp 'find-charset-region))
1099 ;; Remove composition since the base charsets have been included.
1100 ;; Remove eight-bit-*, treat them as ascii.
1101 (let ((css (find-charset-region b e)))
1103 '(composition eight-bit-control eight-bit-graphic control-1)
1105 (setq css (delq cs css)))))
1107 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
1110 (narrow-to-region b e)
1111 (goto-char (point-min))
1112 (skip-chars-forward "\0-\177")
1117 (and (boundp 'current-language-environment)
1118 (car (last (assq 'charset
1119 (assoc current-language-environment
1120 language-info-alist))))))
1121 (if (eq charset 'ascii) (setq charset nil))
1124 (car (last (assq mail-parse-charset
1125 mm-mime-mule-charset-alist)))))
1126 (list 'ascii (or charset 'latin-iso8859-1)))))))))
1128 (defun mm-auto-mode-alist ()
1129 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1130 (let ((alist auto-mode-alist)
1133 (when (listp (cdar alist))
1134 (push (car alist) out))
1138 (defvar mm-inhibit-file-name-handlers
1139 '(jka-compr-handler image-file-handler epa-file-handler)
1140 "A list of handlers doing (un)compression (etc) thingies.")
1142 (defun mm-insert-file-contents (filename &optional visit beg end replace
1144 "Like `insert-file-contents', but only reads in the file.
1145 A buffer may be modified in several ways after reading into the buffer due
1146 to advanced Emacs features, such as file-name-handlers, format decoding,
1147 `find-file-hooks', etc.
1148 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
1149 This function ensures that none of these modifications will take place."
1150 (let* ((format-alist nil)
1151 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
1152 (default-major-mode 'fundamental-mode)
1153 (enable-local-variables nil)
1154 (after-insert-file-functions nil)
1155 (enable-local-eval nil)
1156 (inhibit-file-name-operation (if inhibit
1157 'insert-file-contents
1158 inhibit-file-name-operation))
1159 (inhibit-file-name-handlers
1161 (append mm-inhibit-file-name-handlers
1162 inhibit-file-name-handlers)
1163 inhibit-file-name-handlers))
1164 (ffh (if (boundp 'find-file-hook)
1167 (val (symbol-value ffh)))
1170 (insert-file-contents filename visit beg end replace)
1173 (defun mm-append-to-file (start end filename &optional codesys inhibit)
1174 "Append the contents of the region to the end of file FILENAME.
1175 When called from a function, expects three arguments,
1176 START, END and FILENAME. START and END are buffer positions
1177 saying what text to write.
1178 Optional fourth argument specifies the coding system to use when
1180 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1181 (let ((coding-system-for-write
1182 (or codesys mm-text-coding-system-for-write
1183 mm-text-coding-system))
1184 (inhibit-file-name-operation (if inhibit
1186 inhibit-file-name-operation))
1187 (inhibit-file-name-handlers
1189 (append mm-inhibit-file-name-handlers
1190 inhibit-file-name-handlers)
1191 inhibit-file-name-handlers)))
1192 (write-region start end filename t 'no-message)
1193 (message "Appended to %s" filename)))
1195 (defun mm-write-region (start end filename &optional append visit lockname
1196 coding-system inhibit)
1198 "Like `write-region'.
1199 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1200 (let ((coding-system-for-write
1201 (or coding-system mm-text-coding-system-for-write
1202 mm-text-coding-system))
1203 (inhibit-file-name-operation (if inhibit
1205 inhibit-file-name-operation))
1206 (inhibit-file-name-handlers
1208 (append mm-inhibit-file-name-handlers
1209 inhibit-file-name-handlers)
1210 inhibit-file-name-handlers)))
1211 (write-region start end filename append visit lockname)))
1213 ;; It is not a MIME function, but some MIME functions use it.
1214 (if (and (fboundp 'make-temp-file)
1216 (let ((def (symbol-function 'make-temp-file)))
1217 (and (byte-code-function-p def)
1218 (setq def (if (fboundp 'compiled-function-arglist)
1220 (eval (list 'compiled-function-arglist def))
1223 (eq (nth 3 def) 'suffix)))))
1224 (defalias 'mm-make-temp-file 'make-temp-file)
1225 ;; Stolen (and modified for XEmacs) from Emacs 22.
1226 (defun mm-make-temp-file (prefix &optional dir-flag suffix)
1227 "Create a temporary file.
1228 The returned file name (created by appending some random characters at the end
1229 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1230 is guaranteed to point to a newly created empty file.
1231 You can then use `write-region' to write new data into the file.
1233 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1235 If SUFFIX is non-nil, add that at the end of the file name."
1236 (let ((umask (default-file-modes))
1240 ;; Create temp files with strict access rights. It's easy to
1241 ;; loosen them later, whereas it's impossible to close the
1242 ;; time-window of loose permissions otherwise.
1243 (set-default-file-modes 448)
1244 (while (condition-case err
1250 (if (fboundp 'temp-directory)
1253 temporary-file-directory))))
1255 (setq file (concat file suffix)))
1257 (make-directory file)
1258 ;; NOTE: This is unsafe if Emacs 20
1259 ;; users and XEmacs users don't use
1260 ;; a secure temp directory.
1261 (gmm-write-region "" nil file nil 'silent
1264 (file-already-exists t)
1265 ;; The XEmacs version of `make-directory' issues
1267 (file-error (or (and (featurep 'xemacs)
1268 (file-exists-p file))
1269 (signal (car err) (cdr err)))))
1270 ;; the file was somehow created by someone else between
1271 ;; `make-temp-name' and `write-region', let's try again.
1275 (set-default-file-modes umask)))))
1277 (defun mm-image-load-path (&optional package)
1279 (dolist (path load-path (nreverse result))
1282 (setq dir (concat (file-name-directory
1283 (directory-file-name path))
1284 "etc/images/" (or package "gnus/")))))
1286 (push path result))))
1288 ;; Fixme: This doesn't look useful where it's used.
1289 (if (fboundp 'detect-coding-region)
1290 (defun mm-detect-coding-region (start end)
1291 "Like `detect-coding-region' except returning the best one."
1292 (let ((coding-systems
1293 (detect-coding-region start end)))
1294 (or (car-safe coding-systems)
1296 (defun mm-detect-coding-region (start end)
1297 (let ((point (point)))
1299 (skip-chars-forward "\0-\177" end)
1301 (if (eq (point) end) 'ascii (mm-guess-charset))
1302 (goto-char point)))))
1304 (if (fboundp 'coding-system-get)
1305 (defun mm-detect-mime-charset-region (start end)
1306 "Detect MIME charset of the text in the region between START and END."
1307 (let ((cs (mm-detect-coding-region start end)))
1308 (or (coding-system-get cs :mime-charset)
1309 (coding-system-get cs 'mime-charset))))
1310 (defun mm-detect-mime-charset-region (start end)
1311 "Detect MIME charset of the text in the region between START and END."
1312 (let ((cs (mm-detect-coding-region start end)))
1316 (unless (fboundp 'coding-system-to-mime-charset)
1317 (defalias 'coding-system-to-mime-charset 'ignore)))
1319 (defun mm-coding-system-to-mime-charset (coding-system)
1320 "Return the MIME charset corresponding to CODING-SYSTEM.
1321 To make this function work with XEmacs, the APEL package is required."
1323 (or (and (fboundp 'coding-system-get)
1324 (or (coding-system-get coding-system :mime-charset)
1325 (coding-system-get coding-system 'mime-charset)))
1326 (and (featurep 'xemacs)
1327 (or (and (fboundp 'coding-system-to-mime-charset)
1328 (not (eq (symbol-function 'coding-system-to-mime-charset)
1330 (and (condition-case nil
1333 (fboundp 'coding-system-to-mime-charset)))
1334 (coding-system-to-mime-charset coding-system)))))
1337 (require 'jka-compr))
1339 (defun mm-decompress-buffer (filename &optional inplace force)
1340 "Decompress buffer's contents, depending on jka-compr.
1341 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
1342 agrees with `jka-compr-compression-info-list', decompression is done.
1343 Signal an error if FORCE is neither nil nor t and compressed data are
1344 not decompressed because `auto-compression-mode' is disabled.
1345 If INPLACE is nil, return decompressed data or nil without modifying
1346 the buffer. Otherwise, replace the buffer's contents with the
1347 decompressed data. The buffer's multibyteness must be turned off."
1350 (prog1 t (require 'jka-compr))
1351 (and (fboundp 'jka-compr-installed-p)
1352 (jka-compr-installed-p))))
1353 (let ((info (jka-compr-get-compression-info filename)))
1355 (unless (or (memq force (list nil t))
1356 (jka-compr-installed-p))
1358 (let ((prog (jka-compr-info-uncompress-program info))
1359 (args (jka-compr-info-uncompress-args info))
1360 (msg (format "%s %s..."
1361 (jka-compr-info-uncompress-message info)
1363 (err-file (jka-compr-make-temp-name))
1364 (cur (current-buffer))
1365 (coding-system-for-read mm-binary-coding-system)
1366 (coding-system-for-write mm-binary-coding-system)
1369 (mm-with-unibyte-buffer
1370 (insert-buffer-substring cur)
1373 (unless (memq (apply 'call-process-region
1374 (point-min) (point-max)
1375 prog t (list t err-file) nil args)
1376 jka-compr-acceptable-retval-list)
1380 (delete "" (split-string
1382 (insert-file-contents err-file)
1388 (format "Error while executing \"%s %s < %s\""
1389 prog (mapconcat 'identity args " ")
1391 (setq retval (buffer-string)))
1393 (setq err-msg (error-message-string err)))))
1394 (when (file-exists-p err-file)
1395 (ignore-errors (jka-compr-delete-temp-file err-file)))
1398 (delete-region (point-min) (point-max))
1401 (message "%s" (or err-msg (concat msg "done")))
1405 (unless (fboundp 'coding-system-name)
1406 (defalias 'coding-system-name 'ignore))
1407 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1408 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1409 (unless (fboundp 'find-operation-coding-system)
1410 (defalias 'find-operation-coding-system 'ignore)))
1412 (defun mm-find-buffer-file-coding-system (&optional filename)
1413 "Find coding system used to decode the contents of the current buffer.
1414 This function looks for the coding system magic cookie or examines the
1415 coding system specified by `file-coding-system-alist' being associated
1416 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1417 gzip, bzip2, etc. are allowed."
1419 (setq filename buffer-file-name))
1421 (let ((decomp (unless ;; No worth to examine charset of tar files.
1424 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1426 (mm-decompress-buffer filename nil t))))
1428 (set-buffer (let (default-enable-multibyte-characters)
1429 (generate-new-buffer " *temp*")))
1431 (setq filename (file-name-sans-extension filename)))
1432 (goto-char (point-min))
1435 ((boundp 'set-auto-coding-function) ;; Emacs
1437 (or (funcall (symbol-value 'set-auto-coding-function)
1438 filename (- (point-max) (point-min)))
1439 (car (find-operation-coding-system 'insert-file-contents
1441 (let (auto-coding-alist)
1443 (funcall (symbol-value 'set-auto-coding-function)
1444 nil (- (point-max) (point-min)))
1446 ((and (featurep 'xemacs) (featurep 'file-coding)) ;; XEmacs
1447 (let ((case-fold-search t)
1448 (end (point-at-eol))
1451 (and (re-search-forward "-\\*-+[\t ]*" end t)
1453 (setq start (match-end 0))
1454 (re-search-forward "[\t ]*-+\\*-" end t))
1456 (setq end (match-beginning 0))
1458 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1460 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1462 (find-coding-system (setq codesys
1463 (intern (match-string 1))))
1465 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1468 (setq start (match-end 0))
1469 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1471 (setq end (match-beginning 0))
1474 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1476 (find-coding-system (setq codesys
1477 (intern (match-string 1))))
1480 (goto-char (point-min))
1481 (setq case-fold-search nil)
1482 (re-search-forward "^;;;coding system: "
1483 ;;(+ (point-min) 3000) t))
1485 (looking-at "[^\t\n\r ]+")
1487 (setq codesys (intern (match-string 0))))
1491 (find-file-coding-system-for-read-from-filename
1493 (coding-system-name (coding-system-base codesys)))))))
1495 (kill-buffer (current-buffer)))))))
1499 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1500 ;;; mm-util.el ends here