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)
492 (gb18030 gb18030-2-byte
493 gb18030-4-byte-bmp gb18030-4-byte-smp
494 gb18030-4-byte-ext-1 gb18030-4-byte-ext-2)
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 (defcustom mm-coding-system-priorities
573 (if (boundp 'current-language-environment)
574 (let ((lang (symbol-value 'current-language-environment)))
575 (cond ((string= lang "Japanese")
576 ;; Japanese users prefer iso-2022-jp to euc-japan or
577 ;; shift_jis, however iso-8859-1 should be used when
578 ;; there are only ASCII text and Latin-1 characters.
579 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
580 "Preferred coding systems for encoding outgoing messages.
582 More than one suitable coding system may be found for some text.
583 By default, the coding system with the highest priority is used
584 to encode outgoing messages (see `sort-coding-systems'). If this
585 variable is set, it overrides the default priority."
587 :type '(repeat (symbol :tag "Coding system"))
591 (defvar mm-use-find-coding-systems-region
592 (fboundp 'find-coding-systems-region)
593 "Use `find-coding-systems-region' to find proper coding systems.
595 Setting it to nil is useful on Emacsen supporting Unicode if sending
596 mail with multiple parts is preferred to sending a Unicode one.")
598 ;;; Internal variables:
602 (defun mm-mule-charset-to-mime-charset (charset)
603 "Return the MIME charset corresponding to the given Mule CHARSET."
604 (if (and (fboundp 'find-coding-systems-for-charsets)
605 (fboundp 'sort-coding-systems))
606 (let ((css (sort (sort-coding-systems
607 (find-coding-systems-for-charsets (list charset)))
608 'mm-sort-coding-systems-predicate))
610 (while (and (not mime)
612 (when (setq cs (pop css))
613 (setq mime (or (coding-system-get cs :mime-charset)
614 (coding-system-get cs 'mime-charset)))))
616 (let ((alist (mapcar (lambda (cs)
617 (assq cs mm-mime-mule-charset-alist))
618 (sort (mapcar 'car mm-mime-mule-charset-alist)
619 'mm-sort-coding-systems-predicate)))
622 (when (memq charset (cdar alist))
623 (setq out (caar alist)
628 (defun mm-charset-to-coding-system (charset &optional lbt
630 "Return coding-system corresponding to CHARSET.
631 CHARSET is a symbol naming a MIME charset.
632 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
633 used as the line break code type of the coding system.
635 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
636 map undesired charset names to their replacement. This should
637 only be used for decoding, not for encoding."
638 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
639 (when (stringp charset)
640 (setq charset (intern (downcase charset))))
642 (setq charset (intern (format "%s-%s" charset lbt))))
646 ;; Running in a non-MULE environment.
647 ((or (null (mm-get-coding-system-list))
648 (not (fboundp 'coding-system-get)))
650 ;; Check override list quite early. Should only used for decoding, not for
653 (let ((cs (cdr (assq charset mm-charset-override-alist))))
654 (and cs (mm-coding-system-p cs) cs))))
656 ((eq charset 'us-ascii)
658 ;; Check to see whether we can handle this charset. (This depends
659 ;; on there being some coding system matching each `mime-charset'
660 ;; property defined, as there should be.)
661 ((and (mm-coding-system-p charset)
662 ;;; Doing this would potentially weed out incorrect charsets.
664 ;;; (eq charset (coding-system-get charset 'mime-charset))
667 ;; Eval expressions from `mm-charset-eval-alist'
668 ((let* ((el (assq charset mm-charset-eval-alist))
675 (condition-case nil (eval form) (error nil))
676 ;; (message "Failed to eval `%s'" form))
677 (mm-coding-system-p cs)
678 (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
680 ;; Translate invalid charsets.
681 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
683 (mm-coding-system-p cs)
685 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
688 ;; Last resort: search the coding system list for entries which
689 ;; have the right mime-charset in case the canonical name isn't
690 ;; defined (though it should be).
692 ;; mm-get-coding-system-list returns a list of cs without lbt.
694 (dolist (c (mm-get-coding-system-list))
696 (eq charset (or (coding-system-get c :mime-charset)
697 (coding-system-get c 'mime-charset))))
700 ;; Warn the user about unknown charset:
701 (if (fboundp 'gnus-message)
702 (gnus-message 7 "Unknown charset: %s" charset)
703 (message "Unknown charset: %s" charset)))
707 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
708 (boundp 'default-enable-multibyte-characters)
709 default-enable-multibyte-characters
710 (fboundp 'set-buffer-multibyte))
711 "True in Emacs with Mule.")
714 (defun mm-enable-multibyte ()
715 "Set the multibyte flag of the current buffer.
716 Only do this if the default value of `enable-multibyte-characters' is
717 non-nil. This is a no-op in XEmacs."
718 (set-buffer-multibyte 'to))
719 (defalias 'mm-enable-multibyte 'ignore))
722 (defun mm-disable-multibyte ()
723 "Unset the multibyte flag of in the current buffer.
724 This is a no-op in XEmacs."
725 (set-buffer-multibyte nil))
726 (defalias 'mm-disable-multibyte 'ignore)))
728 (defun mm-preferred-coding-system (charset)
729 ;; A typo in some Emacs versions.
730 (or (get-charset-property charset 'preferred-coding-system)
731 (get-charset-property charset 'prefered-coding-system)))
733 ;; Mule charsets shouldn't be used.
734 (defsubst mm-guess-charset ()
735 "Guess Mule charset from the language environment."
737 mail-parse-mule-charset ;; cached mule-charset
739 (setq mail-parse-mule-charset
740 (and (boundp 'current-language-environment)
743 (assoc current-language-environment
744 language-info-alist))))))
745 (if (or (not mail-parse-mule-charset)
746 (eq mail-parse-mule-charset 'ascii))
747 (setq mail-parse-mule-charset
748 (or (car (last (assq mail-parse-charset
749 mm-mime-mule-charset-alist)))
752 mail-parse-mule-charset)))
754 (defun mm-charset-after (&optional pos)
755 "Return charset of a character in current buffer at position POS.
756 If POS is nil, it defauls to the current point.
757 If POS is out of range, the value is nil.
758 If the charset is `composition', return the actual one."
759 (let ((char (char-after pos)) charset)
760 (if (< (mm-char-int char) 128)
761 (setq charset 'ascii)
762 ;; charset-after is fake in some Emacsen.
763 (setq charset (and (fboundp 'char-charset) (char-charset char)))
764 (if (eq charset 'composition) ; Mule 4
765 (let ((p (or pos (point))))
766 (cadr (find-charset-region p (1+ p))))
767 (if (and charset (not (memq charset '(ascii eight-bit-control
768 eight-bit-graphic))))
770 (mm-guess-charset))))))
772 (defun mm-mime-charset (charset)
773 "Return the MIME charset corresponding to the given Mule CHARSET."
774 (if (eq charset 'unknown)
775 (error "The message contains non-printable characters, please use attachment"))
776 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
777 ;; This exists in Emacs 20.
779 (and (mm-preferred-coding-system charset)
780 (or (coding-system-get
781 (mm-preferred-coding-system charset) :mime-charset)
783 (mm-preferred-coding-system charset) 'mime-charset)))
784 (and (eq charset 'ascii)
786 (mm-preferred-coding-system charset)
787 (mm-mule-charset-to-mime-charset charset))
788 ;; This is for XEmacs.
789 (mm-mule-charset-to-mime-charset charset)))
791 (if (fboundp 'delete-dups)
792 (defalias 'mm-delete-duplicates 'delete-dups)
793 (defun mm-delete-duplicates (list)
794 "Destructively remove `equal' duplicates from LIST.
795 Store the result in LIST and return it. LIST must be a proper list.
796 Of several `equal' occurrences of an element in LIST, the first
799 This is a compatibility function for Emacsen without `delete-dups'."
800 ;; Code from `subr.el' in Emacs 22:
803 (setcdr tail (delete (car tail) (cdr tail)))
804 (setq tail (cdr tail))))
807 ;; Fixme: This is used in places when it should be testing the
808 ;; default multibyteness. See mm-default-multibyte-p.
810 (if (and (not (featurep 'xemacs))
811 (boundp 'enable-multibyte-characters))
812 (defun mm-multibyte-p ()
813 "Non-nil if multibyte is enabled in the current buffer."
814 enable-multibyte-characters)
815 (defun mm-multibyte-p () (featurep 'mule))))
817 (defun mm-default-multibyte-p ()
818 "Return non-nil if the session is multibyte.
819 This affects whether coding conversion should be attempted generally."
821 (if (boundp 'default-enable-multibyte-characters)
822 default-enable-multibyte-characters
825 (defun mm-sort-coding-systems-predicate (a b)
828 ;; Note: invalid entries are dropped silently
829 (and (setq cs (mm-coding-system-p cs))
830 (coding-system-base cs)))
831 mm-coding-system-priorities)))
832 (and (setq a (mm-coding-system-p a))
833 (if (setq b (mm-coding-system-p b))
834 (> (length (memq (coding-system-base a) priorities))
835 (length (memq (coding-system-base b) priorities)))
839 (autoload 'latin-unity-massage-name "latin-unity")
840 (autoload 'latin-unity-maybe-remap "latin-unity")
841 (autoload 'latin-unity-representations-feasible-region "latin-unity")
842 (autoload 'latin-unity-representations-present-region "latin-unity"))
844 (defvar latin-unity-coding-systems)
845 (defvar latin-unity-ucs-list)
847 (defun mm-xemacs-find-mime-charset-1 (begin end)
848 "Determine which MIME charset to use to send region as message.
849 This uses the XEmacs-specific latin-unity package to better handle the
850 case where identical characters from diverse ISO-8859-? character sets
851 can be encoded using a single one of the corresponding coding systems.
853 It treats `mm-coding-system-priorities' as the list of preferred
854 coding systems; a useful example setting for this list in Western
855 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
856 to the very standard Latin 1 coding system, and only move to coding
857 systems that are less supported as is necessary to encode the
858 characters that exist in the buffer.
860 Latin Unity doesn't know about those non-ASCII Roman characters that
861 are available in various East Asian character sets. As such, its
862 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
863 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
864 But this is very much a corner case, so don't worry about it."
865 (let ((systems mm-coding-system-priorities) csets psets curset)
867 ;; Load the Latin Unity library, if available.
868 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
869 (require 'latin-unity))
871 ;; Now, can we use it?
872 (if (featurep 'latin-unity)
874 (setq csets (latin-unity-representations-feasible-region begin end)
875 psets (latin-unity-representations-present-region begin end))
879 ;; Pass back the first coding system in the preferred list
880 ;; that can encode the whole region.
881 (dolist (curset systems)
882 (setq curset (latin-unity-massage-name 'buffer-default curset))
884 ;; If the coding system is a universal coding system, then
885 ;; it can certainly encode all the characters in the region.
886 (if (memq curset latin-unity-ucs-list)
887 (throw 'done (list curset)))
889 ;; If a coding system isn't universal, and isn't in
890 ;; the list that latin unity knows about, we can't
891 ;; decide whether to use it here. Leave that until later
892 ;; in `mm-find-mime-charset-region' function, whence we
894 (unless (memq curset latin-unity-coding-systems)
897 ;; Right, we know about this coding system, and it may
898 ;; conceivably be able to encode all the characters in
900 (if (latin-unity-maybe-remap begin end curset csets psets t)
901 (throw 'done (list curset))))
903 ;; Can't encode using anything from the
904 ;; `mm-coding-system-priorities' list.
905 ;; Leave `mm-find-mime-charset' to do most of the work.
908 ;; Right, latin unity isn't available; let `mm-find-charset-region'
909 ;; take its default action, which equally applies to GNU Emacs.
912 (defmacro mm-xemacs-find-mime-charset (begin end)
913 (when (featurep 'xemacs)
914 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
916 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
917 "Return the MIME charsets needed to encode the region between B and E.
918 nil means ASCII, a single-element list represents an appropriate MIME
919 charset, and a longer list means no appropriate charset."
921 ;; The return possibilities of this function are a mess...
922 (or (and (mm-multibyte-p)
923 mm-use-find-coding-systems-region
924 ;; Find the mime-charset of the most preferred coding
925 ;; system that has one.
926 (let ((systems (find-coding-systems-region b e)))
927 (when mm-coding-system-priorities
929 (sort systems 'mm-sort-coding-systems-predicate)))
930 (setq systems (delq 'compound-text systems))
931 (unless (equal systems '(undecided))
933 (let* ((head (pop systems))
934 (cs (or (coding-system-get head :mime-charset)
935 (coding-system-get head 'mime-charset))))
936 ;; The mime-charset (`x-ctext') of
937 ;; `compound-text' is not in the IANA list. We
938 ;; shouldn't normally use anything here with a
939 ;; mime-charset having an `x-' prefix.
940 ;; Fixme: Allow this to be overridden, since
941 ;; there is existing use of x-ctext.
942 ;; Also people apparently need the coding system
943 ;; `iso-2022-jp-3' (which Mule-UCS defines with
944 ;; mime-charset, though it's not valid).
946 (not (string-match "^[Xx]-" (symbol-name cs)))
947 ;; UTF-16 of any variety is invalid for
948 ;; text parts and, unfortunately, has
949 ;; mime-charset defined both in Mule-UCS
950 ;; and versions of Emacs. (The name
951 ;; might be `mule-utf-16...' or
953 (not (string-match "utf-16" (symbol-name cs))))
955 charsets (list cs))))))
957 ;; If we're XEmacs, and some coding system is appropriate,
958 ;; mm-xemacs-find-mime-charset will return an appropriate list.
959 ;; Otherwise, we'll get nil, and the next setq will get invoked.
960 (setq charsets (mm-xemacs-find-mime-charset b e))
962 ;; Fixme: won't work for unibyte Emacs 23:
964 ;; We're not multibyte, or a single coding system won't cover it.
966 (mm-delete-duplicates
967 (mapcar 'mm-mime-charset
969 (mm-find-charset-region b e))))))
972 (defmacro mm-with-unibyte-buffer (&rest forms)
973 "Create a temporary buffer, and evaluate FORMS there like `progn'.
974 Use unibyte mode for this."
975 `(let (default-enable-multibyte-characters)
976 (with-temp-buffer ,@forms)))
977 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
978 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
980 (defmacro mm-with-multibyte-buffer (&rest forms)
981 "Create a temporary buffer, and evaluate FORMS there like `progn'.
982 Use multibyte mode for this."
983 `(let ((default-enable-multibyte-characters t))
984 (with-temp-buffer ,@forms)))
985 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
986 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
988 (defmacro mm-with-unibyte-current-buffer (&rest forms)
989 "Evaluate FORMS with current buffer temporarily made unibyte.
990 Also bind `default-enable-multibyte-characters' to nil.
991 Equivalent to `progn' in XEmacs
993 NOTE: Use this macro with caution in multibyte buffers (it is not
994 worth using this macro in unibyte buffers of course). Use of
995 `(set-buffer-multibyte t)', which is run finally, is generally
996 harmful since it is likely to modify existing data in the buffer.
997 For instance, it converts \"\\300\\255\" into \"\\255\" in
999 (let ((multibyte (make-symbol "multibyte"))
1000 (buffer (make-symbol "buffer")))
1002 (let ((,multibyte enable-multibyte-characters)
1003 (,buffer (current-buffer)))
1005 (let (default-enable-multibyte-characters)
1006 (set-buffer-multibyte nil)
1008 (set-buffer ,buffer)
1009 (set-buffer-multibyte ,multibyte)))
1010 (let (default-enable-multibyte-characters)
1012 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
1013 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
1015 (defmacro mm-with-unibyte (&rest forms)
1016 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
1017 `(let (default-enable-multibyte-characters)
1019 (put 'mm-with-unibyte 'lisp-indent-function 0)
1020 (put 'mm-with-unibyte 'edebug-form-spec '(body))
1022 (defmacro mm-with-multibyte (&rest forms)
1023 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
1024 `(let ((default-enable-multibyte-characters t))
1026 (put 'mm-with-multibyte 'lisp-indent-function 0)
1027 (put 'mm-with-multibyte 'edebug-form-spec '(body))
1029 (defun mm-find-charset-region (b e)
1030 "Return a list of Emacs charsets in the region B to E."
1032 ((and (mm-multibyte-p)
1033 (fboundp 'find-charset-region))
1034 ;; Remove composition since the base charsets have been included.
1035 ;; Remove eight-bit-*, treat them as ascii.
1036 (let ((css (find-charset-region b e)))
1038 '(composition eight-bit-control eight-bit-graphic control-1)
1040 (setq css (delq cs css)))))
1042 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
1045 (narrow-to-region b e)
1046 (goto-char (point-min))
1047 (skip-chars-forward "\0-\177")
1052 (and (boundp 'current-language-environment)
1053 (car (last (assq 'charset
1054 (assoc current-language-environment
1055 language-info-alist))))))
1056 (if (eq charset 'ascii) (setq charset nil))
1059 (car (last (assq mail-parse-charset
1060 mm-mime-mule-charset-alist)))))
1061 (list 'ascii (or charset 'latin-iso8859-1)))))))))
1063 (defun mm-auto-mode-alist ()
1064 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1065 (let ((alist auto-mode-alist)
1068 (when (listp (cdar alist))
1069 (push (car alist) out))
1073 (defvar mm-inhibit-file-name-handlers
1074 '(jka-compr-handler image-file-handler epa-file-handler)
1075 "A list of handlers doing (un)compression (etc) thingies.")
1077 (defun mm-insert-file-contents (filename &optional visit beg end replace
1079 "Like `insert-file-contents', but only reads in the file.
1080 A buffer may be modified in several ways after reading into the buffer due
1081 to advanced Emacs features, such as file-name-handlers, format decoding,
1082 `find-file-hooks', etc.
1083 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
1084 This function ensures that none of these modifications will take place."
1085 (let* ((format-alist nil)
1086 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
1087 (default-major-mode 'fundamental-mode)
1088 (enable-local-variables nil)
1089 (after-insert-file-functions nil)
1090 (enable-local-eval nil)
1091 (inhibit-file-name-operation (if inhibit
1092 'insert-file-contents
1093 inhibit-file-name-operation))
1094 (inhibit-file-name-handlers
1096 (append mm-inhibit-file-name-handlers
1097 inhibit-file-name-handlers)
1098 inhibit-file-name-handlers))
1099 (ffh (if (boundp 'find-file-hook)
1102 (val (symbol-value ffh)))
1105 (insert-file-contents filename visit beg end replace)
1108 (defun mm-append-to-file (start end filename &optional codesys inhibit)
1109 "Append the contents of the region to the end of file FILENAME.
1110 When called from a function, expects three arguments,
1111 START, END and FILENAME. START and END are buffer positions
1112 saying what text to write.
1113 Optional fourth argument specifies the coding system to use when
1115 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1116 (let ((coding-system-for-write
1117 (or codesys mm-text-coding-system-for-write
1118 mm-text-coding-system))
1119 (inhibit-file-name-operation (if inhibit
1121 inhibit-file-name-operation))
1122 (inhibit-file-name-handlers
1124 (append mm-inhibit-file-name-handlers
1125 inhibit-file-name-handlers)
1126 inhibit-file-name-handlers)))
1127 (write-region start end filename t 'no-message)
1128 (message "Appended to %s" filename)))
1130 (defun mm-write-region (start end filename &optional append visit lockname
1131 coding-system inhibit)
1133 "Like `write-region'.
1134 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1135 (let ((coding-system-for-write
1136 (or coding-system mm-text-coding-system-for-write
1137 mm-text-coding-system))
1138 (inhibit-file-name-operation (if inhibit
1140 inhibit-file-name-operation))
1141 (inhibit-file-name-handlers
1143 (append mm-inhibit-file-name-handlers
1144 inhibit-file-name-handlers)
1145 inhibit-file-name-handlers)))
1146 (write-region start end filename append visit lockname)))
1148 ;; It is not a MIME function, but some MIME functions use it.
1149 (if (and (fboundp 'make-temp-file)
1151 (let ((def (symbol-function 'make-temp-file)))
1152 (and (byte-code-function-p def)
1153 (setq def (if (fboundp 'compiled-function-arglist)
1155 (eval (list 'compiled-function-arglist def))
1158 (eq (nth 3 def) 'suffix)))))
1159 (defalias 'mm-make-temp-file 'make-temp-file)
1160 ;; Stolen (and modified for XEmacs) from Emacs 22.
1161 (defun mm-make-temp-file (prefix &optional dir-flag suffix)
1162 "Create a temporary file.
1163 The returned file name (created by appending some random characters at the end
1164 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1165 is guaranteed to point to a newly created empty file.
1166 You can then use `write-region' to write new data into the file.
1168 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1170 If SUFFIX is non-nil, add that at the end of the file name."
1171 (let ((umask (default-file-modes))
1175 ;; Create temp files with strict access rights. It's easy to
1176 ;; loosen them later, whereas it's impossible to close the
1177 ;; time-window of loose permissions otherwise.
1178 (set-default-file-modes 448)
1179 (while (condition-case err
1185 (if (fboundp 'temp-directory)
1188 temporary-file-directory))))
1190 (setq file (concat file suffix)))
1192 (make-directory file)
1193 ;; NOTE: This is unsafe if Emacs 20
1194 ;; users and XEmacs users don't use
1195 ;; a secure temp directory.
1196 (gmm-write-region "" nil file nil 'silent
1199 (file-already-exists t)
1200 ;; The XEmacs version of `make-directory' issues
1202 (file-error (or (and (featurep 'xemacs)
1203 (file-exists-p file))
1204 (signal (car err) (cdr err)))))
1205 ;; the file was somehow created by someone else between
1206 ;; `make-temp-name' and `write-region', let's try again.
1210 (set-default-file-modes umask)))))
1212 (defun mm-image-load-path (&optional package)
1214 (dolist (path load-path (nreverse result))
1217 (setq dir (concat (file-name-directory
1218 (directory-file-name path))
1219 "etc/images/" (or package "gnus/")))))
1221 (push path result))))
1223 ;; Fixme: This doesn't look useful where it's used.
1224 (if (fboundp 'detect-coding-region)
1225 (defun mm-detect-coding-region (start end)
1226 "Like `detect-coding-region' except returning the best one."
1227 (let ((coding-systems
1228 (detect-coding-region start end)))
1229 (or (car-safe coding-systems)
1231 (defun mm-detect-coding-region (start end)
1232 (let ((point (point)))
1234 (skip-chars-forward "\0-\177" end)
1236 (if (eq (point) end) 'ascii (mm-guess-charset))
1237 (goto-char point)))))
1239 (if (fboundp 'coding-system-get)
1240 (defun mm-detect-mime-charset-region (start end)
1241 "Detect MIME charset of the text in the region between START and END."
1242 (let ((cs (mm-detect-coding-region start end)))
1243 (or (coding-system-get cs :mime-charset)
1244 (coding-system-get cs 'mime-charset))))
1245 (defun mm-detect-mime-charset-region (start end)
1246 "Detect MIME charset of the text in the region between START and END."
1247 (let ((cs (mm-detect-coding-region start end)))
1251 (unless (fboundp 'coding-system-to-mime-charset)
1252 (defalias 'coding-system-to-mime-charset 'ignore)))
1254 (defun mm-coding-system-to-mime-charset (coding-system)
1255 "Return the MIME charset corresponding to CODING-SYSTEM.
1256 To make this function work with XEmacs, the APEL package is required."
1258 (or (and (fboundp 'coding-system-get)
1259 (or (coding-system-get coding-system :mime-charset)
1260 (coding-system-get coding-system 'mime-charset)))
1261 (and (featurep 'xemacs)
1262 (or (and (fboundp 'coding-system-to-mime-charset)
1263 (not (eq (symbol-function 'coding-system-to-mime-charset)
1265 (and (condition-case nil
1268 (fboundp 'coding-system-to-mime-charset)))
1269 (coding-system-to-mime-charset coding-system)))))
1272 (require 'jka-compr))
1274 (defun mm-decompress-buffer (filename &optional inplace force)
1275 "Decompress buffer's contents, depending on jka-compr.
1276 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
1277 agrees with `jka-compr-compression-info-list', decompression is done.
1278 Signal an error if FORCE is neither nil nor t and compressed data are
1279 not decompressed because `auto-compression-mode' is disabled.
1280 If INPLACE is nil, return decompressed data or nil without modifying
1281 the buffer. Otherwise, replace the buffer's contents with the
1282 decompressed data. The buffer's multibyteness must be turned off."
1285 (prog1 t (require 'jka-compr))
1286 (and (fboundp 'jka-compr-installed-p)
1287 (jka-compr-installed-p))))
1288 (let ((info (jka-compr-get-compression-info filename)))
1290 (unless (or (memq force (list nil t))
1291 (jka-compr-installed-p))
1293 (let ((prog (jka-compr-info-uncompress-program info))
1294 (args (jka-compr-info-uncompress-args info))
1295 (msg (format "%s %s..."
1296 (jka-compr-info-uncompress-message info)
1298 (err-file (jka-compr-make-temp-name))
1299 (cur (current-buffer))
1300 (coding-system-for-read mm-binary-coding-system)
1301 (coding-system-for-write mm-binary-coding-system)
1304 (mm-with-unibyte-buffer
1305 (insert-buffer-substring cur)
1308 (unless (memq (apply 'call-process-region
1309 (point-min) (point-max)
1310 prog t (list t err-file) nil args)
1311 jka-compr-acceptable-retval-list)
1315 (delete "" (split-string
1317 (insert-file-contents err-file)
1323 (format "Error while executing \"%s %s < %s\""
1324 prog (mapconcat 'identity args " ")
1326 (setq retval (buffer-string)))
1328 (setq err-msg (error-message-string err)))))
1329 (when (file-exists-p err-file)
1330 (ignore-errors (jka-compr-delete-temp-file err-file)))
1333 (delete-region (point-min) (point-max))
1336 (message "%s" (or err-msg (concat msg "done")))
1340 (unless (fboundp 'coding-system-name)
1341 (defalias 'coding-system-name 'ignore))
1342 (unless (fboundp 'find-file-coding-system-for-read-from-filename)
1343 (defalias 'find-file-coding-system-for-read-from-filename 'ignore))
1344 (unless (fboundp 'find-operation-coding-system)
1345 (defalias 'find-operation-coding-system 'ignore)))
1347 (defun mm-find-buffer-file-coding-system (&optional filename)
1348 "Find coding system used to decode the contents of the current buffer.
1349 This function looks for the coding system magic cookie or examines the
1350 coding system specified by `file-coding-system-alist' being associated
1351 with FILENAME which defaults to `buffer-file-name'. Data compressed by
1352 gzip, bzip2, etc. are allowed."
1354 (setq filename buffer-file-name))
1356 (let ((decomp (unless ;; No worth to examine charset of tar files.
1359 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
1361 (mm-decompress-buffer filename nil t))))
1363 (set-buffer (let (default-enable-multibyte-characters)
1364 (generate-new-buffer " *temp*")))
1366 (setq filename (file-name-sans-extension filename)))
1367 (goto-char (point-min))
1370 ((boundp 'set-auto-coding-function) ;; Emacs
1372 (or (funcall (symbol-value 'set-auto-coding-function)
1373 filename (- (point-max) (point-min)))
1374 (car (find-operation-coding-system 'insert-file-contents
1376 (let (auto-coding-alist)
1378 (funcall (symbol-value 'set-auto-coding-function)
1379 nil (- (point-max) (point-min)))
1381 ((and (featurep 'xemacs) (featurep 'file-coding)) ;; XEmacs
1382 (let ((case-fold-search t)
1383 (end (point-at-eol))
1386 (and (re-search-forward "-\\*-+[\t ]*" end t)
1388 (setq start (match-end 0))
1389 (re-search-forward "[\t ]*-+\\*-" end t))
1391 (setq end (match-beginning 0))
1393 (or (looking-at "coding:[\t ]*\\([^\t ;]+\\)")
1395 "[\t ;]+coding:[\t ]*\\([^\t ;]+\\)"
1397 (find-coding-system (setq codesys
1398 (intern (match-string 1))))
1400 (and (re-search-forward "^[\t ]*;+[\t ]*Local[\t ]+Variables:"
1403 (setq start (match-end 0))
1404 (re-search-forward "^[\t ]*;+[\t ]*End:" nil t))
1406 (setq end (match-beginning 0))
1409 "^[\t ]*;+[\t ]*coding:[\t ]*\\([^\t\n\r ]+\\)"
1411 (find-coding-system (setq codesys
1412 (intern (match-string 1))))
1415 (goto-char (point-min))
1416 (setq case-fold-search nil)
1417 (re-search-forward "^;;;coding system: "
1418 ;;(+ (point-min) 3000) t))
1420 (looking-at "[^\t\n\r ]+")
1422 (setq codesys (intern (match-string 0))))
1426 (find-file-coding-system-for-read-from-filename
1428 (coding-system-name (coding-system-base codesys)))))))
1430 (kill-buffer (current-buffer)))))))
1434 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1435 ;;; mm-util.el ends here