1 ;;; mm-util.el --- Utility functions for Mule and low level things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, 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.
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
33 (eval-when-compile (require 'cl))
37 (if (featurep 'xemacs)
38 (unless (ignore-errors
39 (require 'timer-funcs))
43 (defvar mm-mime-mule-charset-alist )
48 (let ((nfunc (intern (format "mm-%s" (car elem)))))
49 (if (fboundp (car elem))
50 (defalias nfunc (car elem))
51 (defalias nfunc (cdr elem)))))
52 '((coding-system-list . ignore)
54 (coding-system-equal . equal)
55 (annotationp . ignore)
56 (set-buffer-file-coding-system . ignore)
63 (mapcar (lambda (e) (list (symbol-name (car e))))
64 mm-mime-mule-charset-alist)
67 . (lambda (from to string &optional inplace)
68 ;; stolen (and renamed) from nnheader.el
69 "Replace characters in STRING from FROM to TO.
70 Unless optional argument INPLACE is non-nil, return a new string."
71 (let ((string (if inplace string (copy-sequence string)))
74 ;; Replace all occurrences of FROM with TO.
76 (when (= (aref string idx) from)
81 . (lambda (string regexp rep &optional literal)
82 "See `replace-regexp-in-string', only the order of args differs."
83 (replace-regexp-in-string regexp rep string nil literal)))
84 (string-as-unibyte . identity)
85 (string-make-unibyte . identity)
86 ;; string-as-multibyte often doesn't really do what you think it does.
88 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
89 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
90 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
91 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
93 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
94 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
95 ;; Better use string-to-multibyte or encode-coding-string.
96 ;; If you really need string-as-multibyte somewhere it's usually
97 ;; because you're using the internal emacs-mule representation (maybe
98 ;; because you're using string-as-unibyte somewhere), which is
99 ;; generally a problem in itself.
100 ;; Here is an approximate equivalence table to help think about it:
101 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
102 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
103 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
104 (string-as-multibyte . identity)
105 (multibyte-string-p . ignore)
106 (insert-byte . insert-char)
107 (multibyte-char-to-unibyte . identity)
109 . (lambda (buffer-name)
110 "Returns non-nil if a buffer named BUFFER-NAME gets a special frame."
111 (and special-display-function
112 (or (and (member buffer-name special-display-buffer-names) t)
113 (cdr (assoc buffer-name special-display-buffer-names))
115 (dolist (elem special-display-regexps)
117 (string-match elem buffer-name)
121 (string-match (car elem) buffer-name)
122 (throw 'return (cdr elem))))))))))))
125 (if (featurep 'xemacs)
126 (if (featurep 'file-coding)
127 ;; Don't modify string if CODING-SYSTEM is nil.
129 (defun mm-decode-coding-string (str coding-system)
131 (decode-coding-string str coding-system)
133 (defun mm-encode-coding-string (str coding-system)
135 (encode-coding-string str coding-system)
137 (defun mm-decode-coding-region (start end coding-system)
139 (decode-coding-region start end coding-system)))
140 (defun mm-encode-coding-region (start end coding-system)
142 (encode-coding-region start end coding-system))))
143 (defun mm-decode-coding-string (str coding-system) str)
144 (defun mm-encode-coding-string (str coding-system) str)
145 (defalias 'mm-decode-coding-region 'ignore)
146 (defalias 'mm-encode-coding-region 'ignore))
147 (defalias 'mm-decode-coding-string 'decode-coding-string)
148 (defalias 'mm-encode-coding-string 'encode-coding-string)
149 (defalias 'mm-decode-coding-region 'decode-coding-region)
150 (defalias 'mm-encode-coding-region 'encode-coding-region)))
152 (defalias 'mm-string-to-multibyte
156 ((fboundp 'string-to-multibyte)
157 'string-to-multibyte)
160 "Return a multibyte string with the same individual chars as string."
162 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
166 (defalias 'mm-char-or-char-int-p
168 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
169 ((fboundp 'char-valid-p) 'char-valid-p)
172 ;; Fixme: This seems always to be used to read a MIME charset, so it
173 ;; should be re-named and fixed (in Emacs) to offer completion only on
174 ;; proper charset names (base coding systems which have a
175 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
177 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
178 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
179 ;; Actually, there should be an `mm-coding-system-mime-charset'.
181 (defalias 'mm-read-coding-system
183 ((fboundp 'read-coding-system)
184 (if (and (featurep 'xemacs)
185 (<= (string-to-number emacs-version) 21.1))
186 (lambda (prompt &optional default-coding-system)
187 (read-coding-system prompt))
188 'read-coding-system))
189 (t (lambda (prompt &optional default-coding-system)
190 "Prompt the user for a coding system."
192 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
193 mm-mime-mule-charset-alist)))))))
195 (defvar mm-coding-system-list nil)
196 (defun mm-get-coding-system-list ()
197 "Get the coding system list."
198 (or mm-coding-system-list
199 (setq mm-coding-system-list (mm-coding-system-list))))
201 (defun mm-coding-system-p (cs)
202 "Return non-nil if CS is a symbol naming a coding system.
203 In XEmacs, also return non-nil if CS is a coding system object.
204 If CS is available, return CS itself in Emacs, and return a coding
205 system object in XEmacs."
206 (if (fboundp 'find-coding-system)
207 (and cs (find-coding-system cs))
208 (if (fboundp 'coding-system-p)
209 (when (coding-system-p cs)
212 (car (memq cs (mm-get-coding-system-list))))))
214 (defun mm-codepage-setup (number &optional alias)
215 "Create a coding system cpNUMBER.
216 The coding system is created using `codepage-setup'. If ALIAS is
217 non-nil, an alias is created and added to
218 `mm-charset-synonym-alist'. If ALIAS is a string, it's used as
219 the alias. Else windows-NUMBER is used."
221 (let ((completion-ignore-case t)
222 (candidates (cp-supported-codepages)))
223 (list (completing-read "Setup DOS Codepage: (default 437) " candidates
224 nil t nil nil "437"))))
226 (setq alias (if (stringp alias)
228 (intern (format "windows-%s" number)))))
229 (let* ((cp (intern (format "cp%s" number))))
230 (unless (mm-coding-system-p cp)
231 (codepage-setup number))
233 ;; Don't add alias if setup of cp failed.
234 (mm-coding-system-p cp))
235 (add-to-list 'mm-charset-synonym-alist (cons alias cp)))))
237 (defvar mm-charset-synonym-alist
239 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
240 ,@(unless (mm-coding-system-p 'x-ctext)
241 '((x-ctext . ctext)))
242 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_ in 8
244 ,@(unless (mm-coding-system-p 'iso-8859-15)
245 '((iso-8859-15 . iso-8859-1)))
246 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
247 ,@(unless (mm-coding-system-p 'big5-hkscs)
248 '((big5-hkscs . big5)))
249 ;; A Microsoft misunderstanding.
250 ,@(when (and (not (mm-coding-system-p 'unicode))
251 (mm-coding-system-p 'utf-16-le))
252 '((unicode . utf-16-le)))
253 ;; A Microsoft misunderstanding.
254 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
255 (if (mm-coding-system-p 'cp949)
256 '((ks_c_5601-1987 . cp949))
257 '((ks_c_5601-1987 . euc-kr))))
258 ;; Windows-31J is Windows Codepage 932.
259 ,@(when (and (not (mm-coding-system-p 'windows-31j))
260 (mm-coding-system-p 'cp932))
261 '((windows-31j . cp932)))
262 ;; Charset name: GBK, Charset aliases: CP936, MS936, windows-936
263 ;; http://www.iana.org/assignments/charset-reg/GBK
264 ;; Emacs 22.1 has cp936, but not gbk, so we alias it:
265 ,@(when (and (not (mm-coding-system-p 'gbk))
266 (mm-coding-system-p 'cp936))
268 ;; ISO8859-1 is a bogus name for ISO-8859-1
269 ,@(when (and (not (mm-coding-system-p 'iso8859-1))
270 (mm-coding-system-p 'iso-8859-1))
271 '((iso8859-1 . iso-8859-1)))
273 "A mapping from unknown or invalid charset names to the real charset names.
275 See `mm-codepage-iso-8859-list' and `mm-codepage-ibm-list'.")
277 (defcustom mm-codepage-iso-8859-list
278 (list 1250 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
279 ;; Outlook users in Czech republic. Use this to allow reading of
280 ;; their e-mails. cp1250 should be defined by M-x codepage-setup
282 '(1252 . 1) ;; Windows-1252 is a superset of iso-8859-1 (West
283 ;; Europe). See also `gnus-article-dumbquotes-map'.
284 '(1254 . 9) ;; Windows-1254 is a superset of iso-8859-9 (Turkish).
285 '(1255 . 8));; Windows-1255 is a superset of iso-8859-8 (Hebrew).
286 "A list of Windows codepage numbers and iso-8859 charset numbers.
288 If an element is a number corresponding to a supported windows
289 codepage, appropriate entries to `mm-charset-synonym-alist' are
290 added by `mm-setup-codepage-iso-8859'. An element may also be a
291 cons cell where the car is a codepage number and the cdr is the
292 corresponding number of an iso-8859 charset."
293 :type '(list (set :inline t
294 (const 1250 :tag "Central and East European")
295 (const (1252 . 1) :tag "West European")
296 (const (1254 . 9) :tag "Turkish")
297 (const (1255 . 8) :tag "Hebrew"))
301 (integer :tag "Windows codepage number")
302 (cons (integer :tag "Windows codepage number")
303 (integer :tag "iso-8859 charset number")))))
304 :version "22.1" ;; Gnus 5.10.9
307 (defcustom mm-codepage-ibm-list
308 (list 437 ;; (US etc.)
312 863 ;; (Canadian French)
317 866 ;; (Cyrillic - Russian)
322 ;; In Emacs 23 (unicode), cp... and ibm... are aliases.
323 ;; Cf. http://thread.gmane.org/v9lkng5nwy.fsf@marauder.physik.uni-ulm.de
324 "List of IBM codepage numbers.
326 The codepage mappings slighly differ between IBM and other vendors.
327 See \"ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/IBM/README.TXT\".
329 If an element is a number corresponding to a supported windows
330 codepage, appropriate entries to `mm-charset-synonym-alist' are
331 added by `mm-setup-codepage-ibm'."
332 :type '(list (set :inline t
333 (const 437 :tag "US etc.")
334 (const 860 :tag "Portugal")
335 (const 861 :tag "Iceland")
336 (const 862 :tag "Israel")
337 (const 863 :tag "Canadian French")
338 (const 865 :tag "Nordic")
340 (const 850 :tag "Latin 1")
341 (const 855 :tag "Cyrillic")
342 (const 866 :tag "Cyrillic - Russian")
343 (const 857 :tag "Turkish")
344 (const 864 :tag "Arabic")
345 (const 869 :tag "Greek")
346 (const 874 :tag "Thai"))
349 (integer :tag "Codepage number")))
350 :version "22.1" ;; Gnus 5.10.9
353 (defun mm-setup-codepage-iso-8859 (&optional list)
354 "Add appropriate entries to `mm-charset-synonym-alist'.
355 Unless LIST is given, `mm-codepage-iso-8859-list' is used."
357 (setq list mm-codepage-iso-8859-list))
359 (let (cp windows iso)
361 (setq cp (intern (format "cp%d" (car i)))
362 windows (intern (format "windows-%d" (car i)))
363 iso (intern (format "iso-8859-%d" (cdr i))))
364 (setq cp (intern (format "cp%d" i))
365 windows (intern (format "windows-%d" i))))
366 (unless (mm-coding-system-p windows)
367 (if (mm-coding-system-p cp)
368 (add-to-list 'mm-charset-synonym-alist (cons windows cp))
369 (add-to-list 'mm-charset-synonym-alist (cons windows iso)))))))
371 (defun mm-setup-codepage-ibm (&optional list)
372 "Add appropriate entries to `mm-charset-synonym-alist'.
373 Unless LIST is given, `mm-codepage-ibm-list' is used."
375 (setq list mm-codepage-ibm-list))
376 (dolist (number list)
377 (let ((ibm (intern (format "ibm%d" number)))
378 (cp (intern (format "cp%d" number))))
379 (when (and (not (mm-coding-system-p ibm))
380 (mm-coding-system-p cp))
381 (add-to-list 'mm-charset-synonym-alist (cons ibm cp))))))
384 (mm-setup-codepage-iso-8859)
385 (mm-setup-codepage-ibm)
387 (defcustom mm-charset-override-alist
388 '((iso-8859-1 . windows-1252)
389 (iso-8859-8 . windows-1255)
390 (iso-8859-9 . windows-1254))
391 "A mapping from undesired charset names to their replacement.
393 You may add pairs like (iso-8859-1 . windows-1252) here,
394 i.e. treat iso-8859-1 as windows-1252. windows-1252 is a
395 superset of iso-8859-1."
396 :type '(list (set :inline t
397 (const (iso-8859-1 . windows-1252))
398 (const (iso-8859-8 . windows-1255))
399 (const (iso-8859-9 . windows-1254))
400 (const (undecided . windows-1252)))
403 (cons (symbol :tag "From charset")
404 (symbol :tag "To charset"))))
405 :version "22.1" ;; Gnus 5.10.9
408 (defcustom mm-charset-eval-alist
409 (if (featurep 'xemacs)
410 nil ;; I don't know what would be useful for XEmacs.
411 '(;; Emacs 21 offers 1250 1251 1253 1257. Emacs 22 provides autoloads for
412 ;; 1250-1258 (i.e. `mm-codepage-setup' does nothing).
413 (windows-1250 . (mm-codepage-setup 1250 t))
414 (windows-1251 . (mm-codepage-setup 1251 t))
415 (windows-1253 . (mm-codepage-setup 1253 t))
416 (windows-1257 . (mm-codepage-setup 1257 t))))
417 "An alist of (CHARSET . FORM) pairs.
418 If an article is encoded in an unknown CHARSET, FORM is
419 evaluated. This allows to load additional libraries providing
420 charsets on demand. If supported by your Emacs version, you
421 could use `autoload-coding-system' here."
422 :version "22.1" ;; Gnus 5.10.9
423 :type '(list (set :inline t
424 (const (windows-1250 . (mm-codepage-setup 1250 t)))
425 (const (windows-1251 . (mm-codepage-setup 1251 t)))
426 (const (windows-1253 . (mm-codepage-setup 1253 t)))
427 (const (windows-1257 . (mm-codepage-setup 1257 t)))
428 (const (cp850 . (mm-codepage-setup 850 nil))))
431 (cons (symbol :tag "charset")
432 (symbol :tag "form"))))
434 (put 'mm-charset-eval-alist 'risky-local-variable t)
436 (defvar mm-binary-coding-system
438 ((mm-coding-system-p 'binary) 'binary)
439 ((mm-coding-system-p 'no-conversion) 'no-conversion)
441 "100% binary coding system.")
443 (defvar mm-text-coding-system
444 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
445 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
446 (and (mm-coding-system-p 'raw-text) 'raw-text))
447 mm-binary-coding-system)
448 "Text-safe coding system (For removing ^M).")
450 (defvar mm-text-coding-system-for-write nil
451 "Text coding system for write.")
453 (defvar mm-auto-save-coding-system
455 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
456 (if (memq system-type '(windows-nt ms-dos ms-windows))
457 (if (mm-coding-system-p 'utf-8-emacs-dos)
458 'utf-8-emacs-dos mm-binary-coding-system)
460 ((mm-coding-system-p 'emacs-mule)
461 (if (memq system-type '(windows-nt ms-dos ms-windows))
462 (if (mm-coding-system-p 'emacs-mule-dos)
463 'emacs-mule-dos mm-binary-coding-system)
465 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
466 (t mm-binary-coding-system))
467 "Coding system of auto save file.")
469 (defvar mm-universal-coding-system mm-auto-save-coding-system
470 "The universal coding system.")
472 ;; Fixme: some of the cars here aren't valid MIME charsets. That
473 ;; should only matter with XEmacs, though.
474 (defvar mm-mime-mule-charset-alist
476 (iso-8859-1 latin-iso8859-1)
477 (iso-8859-2 latin-iso8859-2)
478 (iso-8859-3 latin-iso8859-3)
479 (iso-8859-4 latin-iso8859-4)
480 (iso-8859-5 cyrillic-iso8859-5)
481 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
482 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
483 ;; charset is koi8-r, not iso-8859-5.
484 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
485 (iso-8859-6 arabic-iso8859-6)
486 (iso-8859-7 greek-iso8859-7)
487 (iso-8859-8 hebrew-iso8859-8)
488 (iso-8859-9 latin-iso8859-9)
489 (iso-8859-14 latin-iso8859-14)
490 (iso-8859-15 latin-iso8859-15)
491 (viscii vietnamese-viscii-lower)
492 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
493 (euc-kr korean-ksc5601)
494 (gb2312 chinese-gb2312)
495 (big5 chinese-big5-1 chinese-big5-2)
497 (thai-tis620 thai-tis620)
498 (windows-1251 cyrillic-iso8859-5)
499 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
500 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
501 latin-jisx0201 japanese-jisx0208-1978
502 chinese-gb2312 japanese-jisx0208
503 korean-ksc5601 japanese-jisx0212)
504 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
505 latin-jisx0201 japanese-jisx0208-1978
506 chinese-gb2312 japanese-jisx0208
507 korean-ksc5601 japanese-jisx0212
508 chinese-cns11643-1 chinese-cns11643-2)
509 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
510 cyrillic-iso8859-5 greek-iso8859-7
511 latin-jisx0201 japanese-jisx0208-1978
512 chinese-gb2312 japanese-jisx0208
513 korean-ksc5601 japanese-jisx0212
514 chinese-cns11643-1 chinese-cns11643-2
515 chinese-cns11643-3 chinese-cns11643-4
516 chinese-cns11643-5 chinese-cns11643-6
518 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
519 japanese-jisx0213-1 japanese-jisx0213-2)
520 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
521 ,(cond ((fboundp 'unicode-precedence-list)
522 (cons 'utf-8 (delq 'ascii (mapcar 'charset-name
523 (unicode-precedence-list)))))
524 ((or (not (fboundp 'charsetp)) ;; non-Mule case
525 (charsetp 'unicode-a)
526 (not (mm-coding-system-p 'mule-utf-8)))
527 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
528 (t ;; If we have utf-8 we're in Mule 5+.
531 (coding-system-get 'mule-utf-8 'safe-charsets))))))
532 "Alist of MIME-charset/MULE-charsets.")
534 (defun mm-enrich-utf-8-by-mule-ucs ()
535 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
536 This function will run when the `un-define' module is loaded under
537 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
538 with Mule charsets. It is completely useless for Emacs."
539 (when (boundp 'unicode-basic-translation-charset-order-list)
545 'unicode-basic-translation-charset-order-list))))
546 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
549 (setq mm-mime-mule-charset-alist
550 (nconc mm-mime-mule-charset-alist
551 (list (cons 'utf-8 val))))))
554 ;; Correct by construction, but should be unnecessary for Emacs:
555 (if (featurep 'xemacs)
556 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
557 (when (and (fboundp 'coding-system-list)
558 (fboundp 'sort-coding-systems))
559 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
563 mime (or (coding-system-get cs :mime-charset) ; Emacs 23 (unicode)
564 (coding-system-get cs 'mime-charset)))
566 (not (eq t (setq mule
567 (coding-system-get cs 'safe-charsets))))
568 (not (assq mime alist)))
569 (push (cons mime (delq 'ascii mule)) alist)))
570 (setq mm-mime-mule-charset-alist (nreverse alist)))))
572 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
573 "A list of special charsets.
574 Valid elements include:
575 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
576 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
579 (defvar mm-iso-8859-15-compatible
580 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
581 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
582 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
584 (defvar mm-iso-8859-x-to-15-table
585 (and (fboundp 'coding-system-p)
586 (mm-coding-system-p 'iso-8859-15)
589 (if (mm-coding-system-p (car cs))
590 (let ((c (string-to-char
591 (decode-coding-string "\341" (car cs)))))
592 (cons (char-charset c)
595 (decode-coding-string "\341" 'iso-8859-15)) c)
596 (string-to-list (decode-coding-string (car (cdr cs))
599 mm-iso-8859-15-compatible))
600 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
602 (defcustom mm-coding-system-priorities
603 (if (boundp 'current-language-environment)
604 (let ((lang (symbol-value 'current-language-environment)))
605 (cond ((string= lang "Japanese")
606 ;; Japanese users prefer iso-2022-jp to euc-japan or
607 ;; shift_jis, however iso-8859-1 should be used when
608 ;; there are only ASCII text and Latin-1 characters.
609 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
610 "Preferred coding systems for encoding outgoing messages.
612 More than one suitable coding system may be found for some text.
613 By default, the coding system with the highest priority is used
614 to encode outgoing messages (see `sort-coding-systems'). If this
615 variable is set, it overrides the default priority."
617 :type '(repeat (symbol :tag "Coding system"))
621 (defvar mm-use-find-coding-systems-region
622 (fboundp 'find-coding-systems-region)
623 "Use `find-coding-systems-region' to find proper coding systems.
625 Setting it to nil is useful on Emacsen supporting Unicode if sending
626 mail with multiple parts is preferred to sending a Unicode one.")
628 ;;; Internal variables:
632 (defun mm-mule-charset-to-mime-charset (charset)
633 "Return the MIME charset corresponding to the given Mule CHARSET."
634 (if (and (fboundp 'find-coding-systems-for-charsets)
635 (fboundp 'sort-coding-systems))
636 (let ((css (sort (sort-coding-systems
637 (find-coding-systems-for-charsets (list charset)))
638 'mm-sort-coding-systems-predicate))
640 (while (and (not mime)
642 (when (setq cs (pop css))
643 (setq mime (or (coding-system-get cs :mime-charset)
644 (coding-system-get cs 'mime-charset)))))
646 (let ((alist (mapcar (lambda (cs)
647 (assq cs mm-mime-mule-charset-alist))
648 (sort (mapcar 'car mm-mime-mule-charset-alist)
649 'mm-sort-coding-systems-predicate)))
652 (when (memq charset (cdar alist))
653 (setq out (caar alist)
658 (defun mm-charset-to-coding-system (charset &optional lbt
660 "Return coding-system corresponding to CHARSET.
661 CHARSET is a symbol naming a MIME charset.
662 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
663 used as the line break code type of the coding system.
665 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
666 map undesired charset names to their replacement. This should
667 only be used for decoding, not for encoding."
668 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
669 (when (stringp charset)
670 (setq charset (intern (downcase charset))))
672 (setq charset (intern (format "%s-%s" charset lbt))))
676 ;; Running in a non-MULE environment.
677 ((or (null (mm-get-coding-system-list))
678 (not (fboundp 'coding-system-get)))
680 ;; Check override list quite early. Should only used for decoding, not for
683 (let ((cs (cdr (assq charset mm-charset-override-alist))))
684 (and cs (mm-coding-system-p cs) cs))))
686 ((eq charset 'us-ascii)
688 ;; Check to see whether we can handle this charset. (This depends
689 ;; on there being some coding system matching each `mime-charset'
690 ;; property defined, as there should be.)
691 ((and (mm-coding-system-p charset)
692 ;;; Doing this would potentially weed out incorrect charsets.
694 ;;; (eq charset (coding-system-get charset 'mime-charset))
697 ;; Eval expressions from `mm-charset-eval-alist'
698 ((let* ((el (assq charset mm-charset-eval-alist))
705 (condition-case nil (eval form) (error nil))
706 ;; (message "Failed to eval `%s'" form))
707 (mm-coding-system-p cs)
708 (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
710 ;; Translate invalid charsets.
711 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
713 (mm-coding-system-p cs)
715 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
718 ;; Last resort: search the coding system list for entries which
719 ;; have the right mime-charset in case the canonical name isn't
720 ;; defined (though it should be).
722 ;; mm-get-coding-system-list returns a list of cs without lbt.
724 (dolist (c (mm-get-coding-system-list))
726 (eq charset (or (coding-system-get c :mime-charset)
727 (coding-system-get c 'mime-charset))))
730 ;; Warn the user about unknown charset:
731 (if (fboundp 'gnus-message)
732 (gnus-message 7 "Unknown charset: %s" charset)
733 (message "Unknown charset: %s" charset)))
737 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
738 (boundp 'default-enable-multibyte-characters)
739 default-enable-multibyte-characters
740 (fboundp 'set-buffer-multibyte))
741 "True in Emacs with Mule.")
744 (defun mm-enable-multibyte ()
745 "Set the multibyte flag of the current buffer.
746 Only do this if the default value of `enable-multibyte-characters' is
747 non-nil. This is a no-op in XEmacs."
748 (set-buffer-multibyte 'to))
749 (defalias 'mm-enable-multibyte 'ignore))
752 (defun mm-disable-multibyte ()
753 "Unset the multibyte flag of in the current buffer.
754 This is a no-op in XEmacs."
755 (set-buffer-multibyte nil))
756 (defalias 'mm-disable-multibyte 'ignore)))
758 (defun mm-preferred-coding-system (charset)
759 ;; A typo in some Emacs versions.
760 (or (get-charset-property charset 'preferred-coding-system)
761 (get-charset-property charset 'prefered-coding-system)))
763 ;; Mule charsets shouldn't be used.
764 (defsubst mm-guess-charset ()
765 "Guess Mule charset from the language environment."
767 mail-parse-mule-charset ;; cached mule-charset
769 (setq mail-parse-mule-charset
770 (and (boundp 'current-language-environment)
773 (assoc current-language-environment
774 language-info-alist))))))
775 (if (or (not mail-parse-mule-charset)
776 (eq mail-parse-mule-charset 'ascii))
777 (setq mail-parse-mule-charset
778 (or (car (last (assq mail-parse-charset
779 mm-mime-mule-charset-alist)))
782 mail-parse-mule-charset)))
784 (defun mm-charset-after (&optional pos)
785 "Return charset of a character in current buffer at position POS.
786 If POS is nil, it defauls to the current point.
787 If POS is out of range, the value is nil.
788 If the charset is `composition', return the actual one."
789 (let ((char (char-after pos)) charset)
790 (if (< (mm-char-int char) 128)
791 (setq charset 'ascii)
792 ;; charset-after is fake in some Emacsen.
793 (setq charset (and (fboundp 'char-charset) (char-charset char)))
794 (if (eq charset 'composition) ; Mule 4
795 (let ((p (or pos (point))))
796 (cadr (find-charset-region p (1+ p))))
797 (if (and charset (not (memq charset '(ascii eight-bit-control
798 eight-bit-graphic))))
800 (mm-guess-charset))))))
802 (defun mm-mime-charset (charset)
803 "Return the MIME charset corresponding to the given Mule CHARSET."
804 (if (eq charset 'unknown)
805 (error "The message contains non-printable characters, please use attachment"))
806 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
807 ;; This exists in Emacs 20.
809 (and (mm-preferred-coding-system charset)
810 (or (coding-system-get
811 (mm-preferred-coding-system charset) :mime-charset)
813 (mm-preferred-coding-system charset) 'mime-charset)))
814 (and (eq charset 'ascii)
816 (mm-preferred-coding-system charset)
817 (mm-mule-charset-to-mime-charset charset))
818 ;; This is for XEmacs.
819 (mm-mule-charset-to-mime-charset charset)))
821 (if (fboundp 'delete-dups)
822 (defalias 'mm-delete-duplicates 'delete-dups)
823 (defun mm-delete-duplicates (list)
824 "Destructively remove `equal' duplicates from LIST.
825 Store the result in LIST and return it. LIST must be a proper list.
826 Of several `equal' occurrences of an element in LIST, the first
829 This is a compatibility function for Emacsen without `delete-dups'."
830 ;; Code from `subr.el' in Emacs 22:
833 (setcdr tail (delete (car tail) (cdr tail)))
834 (setq tail (cdr tail))))
837 ;; Fixme: This is used in places when it should be testing the
838 ;; default multibyteness. See mm-default-multibyte-p.
840 (if (and (not (featurep 'xemacs))
841 (boundp 'enable-multibyte-characters))
842 (defun mm-multibyte-p ()
843 "Non-nil if multibyte is enabled in the current buffer."
844 enable-multibyte-characters)
845 (defun mm-multibyte-p () (featurep 'mule))))
847 (defun mm-default-multibyte-p ()
848 "Return non-nil if the session is multibyte.
849 This affects whether coding conversion should be attempted generally."
851 (if (boundp 'default-enable-multibyte-characters)
852 default-enable-multibyte-characters
855 (defun mm-iso-8859-x-to-15-region (&optional b e)
856 (if (fboundp 'char-charset)
857 (let (charset item c inconvertible)
859 (if e (narrow-to-region b e))
860 (goto-char (point-min))
861 (skip-chars-forward "\0-\177")
864 ((not (setq item (assq (char-charset (setq c (char-after)))
865 mm-iso-8859-x-to-15-table)))
867 ((memq c (cdr (cdr item)))
868 (setq inconvertible t)
871 (insert-before-markers (prog1 (+ c (car (cdr item)))
873 (skip-chars-forward "\0-\177")))
874 (not inconvertible))))
876 (defun mm-sort-coding-systems-predicate (a b)
879 ;; Note: invalid entries are dropped silently
880 (and (setq cs (mm-coding-system-p cs))
881 (coding-system-base cs)))
882 mm-coding-system-priorities)))
883 (and (setq a (mm-coding-system-p a))
884 (if (setq b (mm-coding-system-p b))
885 (> (length (memq (coding-system-base a) priorities))
886 (length (memq (coding-system-base b) priorities)))
890 (autoload 'latin-unity-massage-name "latin-unity")
891 (autoload 'latin-unity-maybe-remap "latin-unity")
892 (autoload 'latin-unity-representations-feasible-region "latin-unity")
893 (autoload 'latin-unity-representations-present-region "latin-unity"))
895 (defvar latin-unity-coding-systems)
896 (defvar latin-unity-ucs-list)
898 (defun mm-xemacs-find-mime-charset-1 (begin end)
899 "Determine which MIME charset to use to send region as message.
900 This uses the XEmacs-specific latin-unity package to better handle the
901 case where identical characters from diverse ISO-8859-? character sets
902 can be encoded using a single one of the corresponding coding systems.
904 It treats `mm-coding-system-priorities' as the list of preferred
905 coding systems; a useful example setting for this list in Western
906 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
907 to the very standard Latin 1 coding system, and only move to coding
908 systems that are less supported as is necessary to encode the
909 characters that exist in the buffer.
911 Latin Unity doesn't know about those non-ASCII Roman characters that
912 are available in various East Asian character sets. As such, its
913 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
914 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
915 But this is very much a corner case, so don't worry about it."
916 (let ((systems mm-coding-system-priorities) csets psets curset)
918 ;; Load the Latin Unity library, if available.
919 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
920 (require 'latin-unity))
922 ;; Now, can we use it?
923 (if (featurep 'latin-unity)
925 (setq csets (latin-unity-representations-feasible-region begin end)
926 psets (latin-unity-representations-present-region begin end))
930 ;; Pass back the first coding system in the preferred list
931 ;; that can encode the whole region.
932 (dolist (curset systems)
933 (setq curset (latin-unity-massage-name 'buffer-default curset))
935 ;; If the coding system is a universal coding system, then
936 ;; it can certainly encode all the characters in the region.
937 (if (memq curset latin-unity-ucs-list)
938 (throw 'done (list curset)))
940 ;; If a coding system isn't universal, and isn't in
941 ;; the list that latin unity knows about, we can't
942 ;; decide whether to use it here. Leave that until later
943 ;; in `mm-find-mime-charset-region' function, whence we
945 (unless (memq curset latin-unity-coding-systems)
948 ;; Right, we know about this coding system, and it may
949 ;; conceivably be able to encode all the characters in
951 (if (latin-unity-maybe-remap begin end curset csets psets t)
952 (throw 'done (list curset))))
954 ;; Can't encode using anything from the
955 ;; `mm-coding-system-priorities' list.
956 ;; Leave `mm-find-mime-charset' to do most of the work.
959 ;; Right, latin unity isn't available; let `mm-find-charset-region'
960 ;; take its default action, which equally applies to GNU Emacs.
963 (defmacro mm-xemacs-find-mime-charset (begin end)
964 (when (featurep 'xemacs)
965 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
967 (declare-function mm-delete-duplicates "mm-util" (list))
969 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
970 "Return the MIME charsets needed to encode the region between B and E.
971 nil means ASCII, a single-element list represents an appropriate MIME
972 charset, and a longer list means no appropriate charset."
974 ;; The return possibilities of this function are a mess...
975 (or (and (mm-multibyte-p)
976 mm-use-find-coding-systems-region
977 ;; Find the mime-charset of the most preferred coding
978 ;; system that has one.
979 (let ((systems (find-coding-systems-region b e)))
980 (when mm-coding-system-priorities
982 (sort systems 'mm-sort-coding-systems-predicate)))
983 (setq systems (delq 'compound-text systems))
984 (unless (equal systems '(undecided))
986 (let* ((head (pop systems))
987 (cs (or (coding-system-get head :mime-charset)
988 (coding-system-get head 'mime-charset))))
989 ;; The mime-charset (`x-ctext') of
990 ;; `compound-text' is not in the IANA list. We
991 ;; shouldn't normally use anything here with a
992 ;; mime-charset having an `x-' prefix.
993 ;; Fixme: Allow this to be overridden, since
994 ;; there is existing use of x-ctext.
995 ;; Also people apparently need the coding system
996 ;; `iso-2022-jp-3' (which Mule-UCS defines with
997 ;; mime-charset, though it's not valid).
999 (not (string-match "^[Xx]-" (symbol-name cs)))
1000 ;; UTF-16 of any variety is invalid for
1001 ;; text parts and, unfortunately, has
1002 ;; mime-charset defined both in Mule-UCS
1003 ;; and versions of Emacs. (The name
1004 ;; might be `mule-utf-16...' or
1006 (not (string-match "utf-16" (symbol-name cs))))
1008 charsets (list cs))))))
1010 ;; If we're XEmacs, and some coding system is appropriate,
1011 ;; mm-xemacs-find-mime-charset will return an appropriate list.
1012 ;; Otherwise, we'll get nil, and the next setq will get invoked.
1013 (setq charsets (mm-xemacs-find-mime-charset b e))
1015 ;; We're not multibyte, or a single coding system won't cover it.
1017 (mm-delete-duplicates
1018 (mapcar 'mm-mime-charset
1020 (mm-find-charset-region b e))))))
1021 (if (and (> (length charsets) 1)
1022 (memq 'iso-8859-15 charsets)
1023 (memq 'iso-8859-15 hack-charsets)
1024 (save-excursion (mm-iso-8859-x-to-15-region b e)))
1025 (dolist (x mm-iso-8859-15-compatible)
1026 (setq charsets (delq (car x) charsets))))
1027 (if (and (memq 'iso-2022-jp-2 charsets)
1028 (memq 'iso-2022-jp-2 hack-charsets))
1029 (setq charsets (delq 'iso-2022-jp charsets)))
1030 ;; Attempt to reduce the number of charsets if utf-8 is available.
1031 (if (and (featurep 'xemacs)
1032 (> (length charsets) 1)
1033 (mm-coding-system-p 'utf-8))
1034 (let ((mm-coding-system-priorities
1035 (cons 'utf-8 mm-coding-system-priorities)))
1037 (mm-delete-duplicates
1038 (mapcar 'mm-mime-charset
1040 (mm-find-charset-region b e)))))))
1043 (defmacro mm-with-unibyte-buffer (&rest forms)
1044 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1045 Use unibyte mode for this."
1046 `(let (default-enable-multibyte-characters)
1047 (with-temp-buffer ,@forms)))
1048 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
1049 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
1051 (defmacro mm-with-multibyte-buffer (&rest forms)
1052 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1053 Use multibyte mode for this."
1054 `(let ((default-enable-multibyte-characters t))
1055 (with-temp-buffer ,@forms)))
1056 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
1057 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
1059 (defmacro mm-with-unibyte-current-buffer (&rest forms)
1060 "Evaluate FORMS with current buffer temporarily made unibyte.
1061 Also bind `default-enable-multibyte-characters' to nil.
1062 Equivalent to `progn' in XEmacs
1064 NOTE: Use this macro with caution in multibyte buffers (it is not
1065 worth using this macro in unibyte buffers of course). Use of
1066 `(set-buffer-multibyte t)', which is run finally, is generally
1067 harmful since it is likely to modify existing data in the buffer.
1068 For instance, it converts \"\\300\\255\" into \"\\255\" in
1069 Emacs 23 (unicode)."
1070 (let ((multibyte (make-symbol "multibyte"))
1071 (buffer (make-symbol "buffer")))
1073 (let ((,multibyte enable-multibyte-characters)
1074 (,buffer (current-buffer)))
1076 (let (default-enable-multibyte-characters)
1077 (set-buffer-multibyte nil)
1079 (set-buffer ,buffer)
1080 (set-buffer-multibyte ,multibyte)))
1081 (let (default-enable-multibyte-characters)
1083 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
1084 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
1086 (defmacro mm-with-unibyte (&rest forms)
1087 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
1088 `(let (default-enable-multibyte-characters)
1090 (put 'mm-with-unibyte 'lisp-indent-function 0)
1091 (put 'mm-with-unibyte 'edebug-form-spec '(body))
1093 (defmacro mm-with-multibyte (&rest forms)
1094 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
1095 `(let ((default-enable-multibyte-characters t))
1097 (put 'mm-with-multibyte 'lisp-indent-function 0)
1098 (put 'mm-with-multibyte 'edebug-form-spec '(body))
1100 (defun mm-find-charset-region (b e)
1101 "Return a list of Emacs charsets in the region B to E."
1103 ((and (mm-multibyte-p)
1104 (fboundp 'find-charset-region))
1105 ;; Remove composition since the base charsets have been included.
1106 ;; Remove eight-bit-*, treat them as ascii.
1107 (let ((css (find-charset-region b e)))
1109 '(composition eight-bit-control eight-bit-graphic control-1)
1111 (setq css (delq cs css)))))
1113 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
1116 (narrow-to-region b e)
1117 (goto-char (point-min))
1118 (skip-chars-forward "\0-\177")
1123 (and (boundp 'current-language-environment)
1124 (car (last (assq 'charset
1125 (assoc current-language-environment
1126 language-info-alist))))))
1127 (if (eq charset 'ascii) (setq charset nil))
1130 (car (last (assq mail-parse-charset
1131 mm-mime-mule-charset-alist)))))
1132 (list 'ascii (or charset 'latin-iso8859-1)))))))))
1134 (defun mm-auto-mode-alist ()
1135 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1136 (let ((alist auto-mode-alist)
1139 (when (listp (cdar alist))
1140 (push (car alist) out))
1144 (defvar mm-inhibit-file-name-handlers
1145 '(jka-compr-handler image-file-handler epa-file-handler)
1146 "A list of handlers doing (un)compression (etc) thingies.")
1148 (defun mm-insert-file-contents (filename &optional visit beg end replace
1150 "Like `insert-file-contents', but only reads in the file.
1151 A buffer may be modified in several ways after reading into the buffer due
1152 to advanced Emacs features, such as file-name-handlers, format decoding,
1153 `find-file-hooks', etc.
1154 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
1155 This function ensures that none of these modifications will take place."
1156 (let* ((format-alist nil)
1157 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
1158 (default-major-mode 'fundamental-mode)
1159 (enable-local-variables nil)
1160 (after-insert-file-functions nil)
1161 (enable-local-eval nil)
1162 (inhibit-file-name-operation (if inhibit
1163 'insert-file-contents
1164 inhibit-file-name-operation))
1165 (inhibit-file-name-handlers
1167 (append mm-inhibit-file-name-handlers
1168 inhibit-file-name-handlers)
1169 inhibit-file-name-handlers))
1170 (ffh (if (boundp 'find-file-hook)
1173 (val (symbol-value ffh)))
1176 (insert-file-contents filename visit beg end replace)
1179 (defun mm-append-to-file (start end filename &optional codesys inhibit)
1180 "Append the contents of the region to the end of file FILENAME.
1181 When called from a function, expects three arguments,
1182 START, END and FILENAME. START and END are buffer positions
1183 saying what text to write.
1184 Optional fourth argument specifies the coding system to use when
1186 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1187 (let ((coding-system-for-write
1188 (or codesys mm-text-coding-system-for-write
1189 mm-text-coding-system))
1190 (inhibit-file-name-operation (if inhibit
1192 inhibit-file-name-operation))
1193 (inhibit-file-name-handlers
1195 (append mm-inhibit-file-name-handlers
1196 inhibit-file-name-handlers)
1197 inhibit-file-name-handlers)))
1198 (write-region start end filename t 'no-message)
1199 (message "Appended to %s" filename)))
1201 (defun mm-write-region (start end filename &optional append visit lockname
1202 coding-system inhibit)
1204 "Like `write-region'.
1205 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1206 (let ((coding-system-for-write
1207 (or coding-system mm-text-coding-system-for-write
1208 mm-text-coding-system))
1209 (inhibit-file-name-operation (if inhibit
1211 inhibit-file-name-operation))
1212 (inhibit-file-name-handlers
1214 (append mm-inhibit-file-name-handlers
1215 inhibit-file-name-handlers)
1216 inhibit-file-name-handlers)))
1217 (write-region start end filename append visit lockname)))
1219 (autoload 'gmm-write-region "gmm-utils")
1221 ;; It is not a MIME function, but some MIME functions use it.
1222 (if (and (fboundp 'make-temp-file)
1224 (let ((def (symbol-function 'make-temp-file)))
1225 (and (byte-code-function-p def)
1226 (setq def (if (fboundp 'compiled-function-arglist)
1228 (eval (list 'compiled-function-arglist def))
1231 (eq (nth 3 def) 'suffix)))))
1232 (defalias 'mm-make-temp-file 'make-temp-file)
1233 ;; Stolen (and modified for XEmacs) from Emacs 22.
1234 (defun mm-make-temp-file (prefix &optional dir-flag suffix)
1235 "Create a temporary file.
1236 The returned file name (created by appending some random characters at the end
1237 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1238 is guaranteed to point to a newly created empty file.
1239 You can then use `write-region' to write new data into the file.
1241 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1243 If SUFFIX is non-nil, add that at the end of the file name."
1244 (let ((umask (default-file-modes))
1248 ;; Create temp files with strict access rights. It's easy to
1249 ;; loosen them later, whereas it's impossible to close the
1250 ;; time-window of loose permissions otherwise.
1251 (set-default-file-modes 448)
1252 (while (condition-case err
1258 (if (fboundp 'temp-directory)
1261 temporary-file-directory))))
1263 (setq file (concat file suffix)))
1265 (make-directory file)
1266 ;; NOTE: This is unsafe if Emacs 20
1267 ;; users and XEmacs users don't use
1268 ;; a secure temp directory.
1269 (gmm-write-region "" nil file nil 'silent
1272 (file-already-exists t)
1273 ;; The XEmacs version of `make-directory' issues
1275 (file-error (or (and (featurep 'xemacs)
1276 (file-exists-p file))
1277 (signal (car err) (cdr err)))))
1278 ;; the file was somehow created by someone else between
1279 ;; `make-temp-name' and `write-region', let's try again.
1283 (set-default-file-modes umask)))))
1285 (defun mm-image-load-path (&optional package)
1287 (dolist (path load-path (nreverse result))
1290 (setq dir (concat (file-name-directory
1291 (directory-file-name path))
1292 "etc/images/" (or package "gnus/")))))
1294 (push path result))))
1296 ;; Fixme: This doesn't look useful where it's used.
1297 (if (fboundp 'detect-coding-region)
1298 (defun mm-detect-coding-region (start end)
1299 "Like `detect-coding-region' except returning the best one."
1300 (let ((coding-systems
1301 (detect-coding-region start end)))
1302 (or (car-safe coding-systems)
1304 (defun mm-detect-coding-region (start end)
1305 (let ((point (point)))
1307 (skip-chars-forward "\0-\177" end)
1309 (if (eq (point) end) 'ascii (mm-guess-charset))
1310 (goto-char point)))))
1312 (declare-function mm-detect-coding-region "mm-util" (start end))
1314 (if (fboundp 'coding-system-get)
1315 (defun mm-detect-mime-charset-region (start end)
1316 "Detect MIME charset of the text in the region between START and END."
1317 (let ((cs (mm-detect-coding-region start end)))
1318 (or (coding-system-get cs :mime-charset)
1319 (coding-system-get cs 'mime-charset))))
1320 (defun mm-detect-mime-charset-region (start end)
1321 "Detect MIME charset of the text in the region between START and END."
1322 (let ((cs (mm-detect-coding-region start end)))
1326 (unless (fboundp 'coding-system-to-mime-charset)
1327 (defalias 'coding-system-to-mime-charset 'ignore)))
1329 (defun mm-coding-system-to-mime-charset (coding-system)
1330 "Return the MIME charset corresponding to CODING-SYSTEM.
1331 To make this function work with XEmacs, the APEL package is required."
1333 (or (and (fboundp 'coding-system-get)
1334 (or (coding-system-get coding-system :mime-charset)
1335 (coding-system-get coding-system 'mime-charset)))
1336 (and (featurep 'xemacs)
1337 (or (and (fboundp 'coding-system-to-mime-charset)
1338 (not (eq (symbol-function 'coding-system-to-mime-charset)
1340 (and (condition-case nil
1343 (fboundp 'coding-system-to-mime-charset)))
1344 (coding-system-to-mime-charset coding-system)))))
1347 (require 'jka-compr))
1349 (defun mm-decompress-buffer (filename &optional inplace force)
1350 "Decompress buffer's contents, depending on jka-compr.
1351 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
1352 agrees with `jka-compr-compression-info-list', decompression is done.
1353 Signal an error if FORCE is neither nil nor t and compressed data are
1354 not decompressed because `auto-compression-mode' is disabled.
1355 If INPLACE is nil, return decompressed data or nil without modifying
1356 the buffer. Otherwise, replace the buffer's contents with the
1357 decompressed data. The buffer's multibyteness must be turned off."
1360 (prog1 t (require 'jka-compr))
1361 (and (fboundp 'jka-compr-installed-p)
1362 (jka-compr-installed-p))))
1363 (let ((info (jka-compr-get-compression-info filename)))
1365 (unless (or (memq force (list nil t))
1366 (jka-compr-installed-p))
1368 (let ((prog (jka-compr-info-uncompress-program info))
1369 (args (jka-compr-info-uncompress-args info))
1370 (msg (format "%s %s..."
1371 (jka-compr-info-uncompress-message info)
1373 (err-file (jka-compr-make-temp-name))
1374 (cur (current-buffer))
1375 (coding-system-for-read mm-binary-coding-system)
1376 (coding-system-for-write mm-binary-coding-system)
1379 (mm-with-unibyte-buffer
1380 (insert-buffer-substring cur)
1383 (unless (memq (apply 'call-process-region
1384 (point-min) (point-max)
1385 prog t (list t err-file) nil args)
1386 jka-compr-acceptable-retval-list)
1390 (delete "" (split-string
1392 (insert-file-contents err-file)
1398 (format "Error while executing \"%s %s < %s\""
1399 prog (mapconcat 'identity args " ")
1401 (setq retval (buffer-string)))
1403 (setq err-msg (error-message-string err)))))
1404 (when (file-exists-p err-file)
1405 (ignore-errors (jka-compr-delete-temp-file err-file)))
1408 (delete-region (point-min) (point-max))
1411 (message "%s" (or err-msg (concat msg "done")))
1415 (unless (fboundp 'coding-system-name)
1416 (defalias 'coding-system-name 'ignore))
1417 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1418 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1419 (unless (fboundp 'find-operation-coding-system)
1420 (defalias 'find-operation-coding-system 'ignore)))
1422 (defun mm-find-buffer-file-coding-system (&optional filename)
1423 "Find coding system used to decode the contents of the current buffer.
1424 This function looks for the coding system magic cookie or examines the
1425 coding system specified by `file-coding-system-alist' being associated
1426 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1427 gzip, bzip2, etc. are allowed."
1429 (setq filename buffer-file-name))
1431 (let ((decomp (unless ;; No worth to examine charset of tar files.
1434 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1436 (mm-decompress-buffer filename nil t))))
1438 (set-buffer (let (default-enable-multibyte-characters)
1439 (generate-new-buffer " *temp*")))
1441 (setq filename (file-name-sans-extension filename)))
1442 (goto-char (point-min))
1445 ((boundp 'set-auto-coding-function) ;; Emacs
1447 (or (funcall (symbol-value 'set-auto-coding-function)
1448 filename (- (point-max) (point-min)))
1449 (car (find-operation-coding-system 'insert-file-contents
1451 (let (auto-coding-alist)
1453 (funcall (symbol-value 'set-auto-coding-function)
1454 nil (- (point-max) (point-min)))
1456 ((and (featurep 'xemacs) (featurep 'file-coding)) ;; XEmacs
1457 (let ((case-fold-search t)
1458 (end (point-at-eol))
1461 (and (re-search-forward "-\\*-+[\t ]*" end t)
1463 (setq start (match-end 0))
1464 (re-search-forward "[\t ]*-+\\*-" end t))
1466 (setq end (match-beginning 0))
1468 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1470 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1472 (find-coding-system (setq codesys
1473 (intern (match-string 1))))
1475 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1478 (setq start (match-end 0))
1479 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1481 (setq end (match-beginning 0))
1484 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1486 (find-coding-system (setq codesys
1487 (intern (match-string 1))))
1490 (goto-char (point-min))
1491 (setq case-fold-search nil)
1492 (re-search-forward "^;;;coding system: "
1493 ;;(+ (point-min) 3000) t))
1495 (looking-at "[^\t\n\r ]+")
1497 (setq codesys (intern (match-string 0))))
1501 (find-file-coding-system-for-read-from-filename
1503 (coding-system-name (coding-system-base codesys)))))))
1505 (kill-buffer (current-buffer)))))))
1509 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1510 ;;; mm-util.el ends here