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 2, 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))
35 (let ((nfunc (intern (format "mm-%s" (car elem)))))
36 (if (fboundp (car elem))
37 (defalias nfunc (car elem))
38 (defalias nfunc (cdr elem)))))
39 '((decode-coding-string . (lambda (s a) s))
40 (encode-coding-string . (lambda (s a) s))
41 (encode-coding-region . ignore)
42 (coding-system-list . ignore)
43 (decode-coding-region . ignore)
45 (coding-system-equal . equal)
46 (annotationp . ignore)
47 (set-buffer-file-coding-system . ignore)
49 . (lambda (charset int)
57 (mapcar (lambda (e) (list (symbol-name (car e))))
58 mm-mime-mule-charset-alist)
61 . (lambda (from to string &optional inplace)
62 ;; stolen (and renamed) from nnheader.el
63 "Replace characters in STRING from FROM to TO.
64 Unless optional argument INPLACE is non-nil, return a new string."
65 (let ((string (if inplace string (copy-sequence string)))
68 ;; Replace all occurrences of FROM with TO.
70 (when (= (aref string idx) from)
74 (string-as-unibyte . identity)
75 (string-make-unibyte . identity)
76 ;; string-as-multibyte often doesn't really do what you think it does.
78 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
79 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
80 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
81 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
83 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
84 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
85 ;; Better use string-to-multibyte or encode-coding-string.
86 ;; If you really need string-as-multibyte somewhere it's usually
87 ;; because you're using the internal emacs-mule representation (maybe
88 ;; because you're using string-as-unibyte somewhere), which is
89 ;; generally a problem in itself.
90 ;; Here is an approximate equivalence table to help think about it:
91 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
92 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
93 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
94 (string-as-multibyte . identity)
95 (multibyte-string-p . ignore)
96 (insert-byte . insert-char)
97 (multibyte-char-to-unibyte . identity))))
101 ((fboundp 'replace-in-string)
102 (defalias 'mm-replace-in-string 'replace-in-string))
103 ((fboundp 'replace-regexp-in-string)
104 (defun mm-replace-in-string (string regexp newtext &optional literal)
105 "Replace all matches for REGEXP with NEWTEXT in STRING.
106 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
107 string containing the replacements.
109 This is a compatibility function for different Emacsen."
110 (replace-regexp-in-string regexp newtext string nil literal)))
112 (defun mm-replace-in-string (string regexp newtext &optional literal)
113 "Replace all matches for REGEXP with NEWTEXT in STRING.
114 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
115 string containing the replacements.
117 This is a compatibility function for different Emacsen."
118 (let ((start 0) tail)
119 (while (string-match regexp string start)
120 (setq tail (- (length string) (match-end 0)))
121 (setq string (replace-match newtext nil literal string))
122 (setq start (- (length string) tail))))
125 (defalias 'mm-string-to-multibyte
129 ((fboundp 'string-to-multibyte)
130 'string-to-multibyte)
133 "Return a multibyte string with the same individual chars as string."
135 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
139 (defalias 'mm-char-or-char-int-p
141 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
142 ((fboundp 'char-valid-p) 'char-valid-p)
145 ;; Fixme: This seems always to be used to read a MIME charset, so it
146 ;; should be re-named and fixed (in Emacs) to offer completion only on
147 ;; proper charset names (base coding systems which have a
148 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
150 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
151 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
152 ;; Actually, there should be an `mm-coding-system-mime-charset'.
154 (defalias 'mm-read-coding-system
156 ((fboundp 'read-coding-system)
157 (if (and (featurep 'xemacs)
158 (<= (string-to-number emacs-version) 21.1))
159 (lambda (prompt &optional default-coding-system)
160 (read-coding-system prompt))
161 'read-coding-system))
162 (t (lambda (prompt &optional default-coding-system)
163 "Prompt the user for a coding system."
165 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
166 mm-mime-mule-charset-alist)))))))
168 (defvar mm-coding-system-list nil)
169 (defun mm-get-coding-system-list ()
170 "Get the coding system list."
171 (or mm-coding-system-list
172 (setq mm-coding-system-list (mm-coding-system-list))))
174 (defun mm-coding-system-p (cs)
175 "Return non-nil if CS is a symbol naming a coding system.
176 In XEmacs, also return non-nil if CS is a coding system object.
177 If CS is available, return CS itself in Emacs, and return a coding
178 system object in XEmacs."
179 (if (fboundp 'find-coding-system)
180 (and cs (find-coding-system cs))
181 (if (fboundp 'coding-system-p)
182 (when (coding-system-p cs)
185 (car (memq cs (mm-get-coding-system-list))))))
187 (defun mm-codepage-setup (number &optional alias)
188 "Create a coding system cpNUMBER.
189 The coding system is created using `codepage-setup'. If ALIAS is
190 non-nil, an alias is created and added to
191 `mm-charset-synonym-alist'. If ALIAS is a string, it's used as
192 the alias. Else windows-NUMBER is used."
194 (let ((completion-ignore-case t)
195 (candidates (cp-supported-codepages)))
196 (list (completing-read "Setup DOS Codepage: (default 437) " candidates
197 nil t nil nil "437"))))
199 (setq alias (if (stringp alias)
201 (intern (format "windows-%s" number)))))
202 (let* ((cp (intern (format "cp%s" number))))
203 (unless (mm-coding-system-p cp)
204 (codepage-setup number))
206 ;; Don't add alias if setup of cp failed.
207 (mm-coding-system-p cp))
208 (add-to-list 'mm-charset-synonym-alist (cons alias cp)))))
210 (defvar mm-charset-synonym-alist
212 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
213 ,@(unless (mm-coding-system-p 'x-ctext)
214 '((x-ctext . ctext)))
215 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_ in 8
217 ,@(unless (mm-coding-system-p 'iso-8859-15)
218 '((iso-8859-15 . iso-8859-1)))
219 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
220 ,@(unless (mm-coding-system-p 'big5-hkscs)
221 '((big5-hkscs . big5)))
222 ;; A Microsoft misunderstanding.
223 ,@(when (and (not (mm-coding-system-p 'unicode))
224 (mm-coding-system-p 'utf-16-le))
225 '((unicode . utf-16-le)))
226 ;; A Microsoft misunderstanding.
227 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
228 (if (mm-coding-system-p 'cp949)
229 '((ks_c_5601-1987 . cp949))
230 '((ks_c_5601-1987 . euc-kr))))
231 ;; Windows-31J is Windows Codepage 932.
232 ,@(when (and (not (mm-coding-system-p 'windows-31j))
233 (mm-coding-system-p 'cp932))
234 '((windows-31j . cp932)))
236 "A mapping from unknown or invalid charset names to the real charset names.
238 See `mm-codepage-iso-8859-list' and `mm-codepage-ibm-list'.")
240 (defcustom mm-codepage-iso-8859-list
241 (list 1250 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
242 ;; Outlook users in Czech republic. Use this to allow reading of
243 ;; their e-mails. cp1250 should be defined by M-x codepage-setup
245 '(1252 . 1) ;; Windows-1252 is a superset of iso-8859-1 (West
246 ;; Europe). See also `gnus-article-dumbquotes-map'.
247 '(1254 . 9) ;; Windows-1254 is a superset of iso-8859-9 (Turkish).
248 '(1255 . 8));; Windows-1255 is a superset of iso-8859-8 (Hebrew).
249 "A list of Windows codepage numbers and iso-8859 charset numbers.
251 If an element is a number corresponding to a supported windows
252 codepage, appropriate entries to `mm-charset-synonym-alist' are
253 added by `mm-setup-codepage-iso-8859'. An element may also be a
254 cons cell where the car is a codepage number and the cdr is the
255 corresponding number of an iso-8859 charset."
256 :type '(list (set :inline t
257 (const 1250 :tag "Central and East European")
258 (const (1252 . 1) :tag "West European")
259 (const (1254 . 9) :tag "Turkish")
260 (const (1255 . 8) :tag "Hebrew"))
264 (integer :tag "Windows codepage number")
265 (cons (integer :tag "Windows codepage number")
266 (integer :tag "iso-8859 charset number")))))
267 :version "22.1" ;; Gnus 5.10.9
270 (defcustom mm-codepage-ibm-list
271 (list 437 ;; (US etc.)
275 863 ;; (Canadian French)
280 866 ;; (Cyrillic - Russian)
285 ;; In Emacs 23 (unicode), cp... and ibm... are aliases.
286 ;; Cf. http://thread.gmane.org/v9lkng5nwy.fsf@marauder.physik.uni-ulm.de
287 "List of IBM codepage numbers.
289 The codepage mappings slighly differ between IBM and other vendors.
290 See \"ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/IBM/README.TXT\".
292 If an element is a number corresponding to a supported windows
293 codepage, appropriate entries to `mm-charset-synonym-alist' are
294 added by `mm-setup-codepage-ibm'."
295 :type '(list (set :inline t
296 (const 437 :tag "US etc.")
297 (const 860 :tag "Portugal")
298 (const 861 :tag "Iceland")
299 (const 862 :tag "Israel")
300 (const 863 :tag "Canadian French")
301 (const 865 :tag "Nordic")
303 (const 850 :tag "Latin 1")
304 (const 855 :tag "Cyrillic")
305 (const 866 :tag "Cyrillic - Russian")
306 (const 857 :tag "Turkish")
307 (const 864 :tag "Arabic")
308 (const 869 :tag "Greek")
309 (const 874 :tag "Thai"))
312 (integer :tag "Codepage number")))
313 :version "22.1" ;; Gnus 5.10.9
316 (defun mm-setup-codepage-iso-8859 (&optional list)
317 "Add appropriate entries to `mm-charset-synonym-alist'.
318 Unless LIST is given, `mm-codepage-iso-8859-list' is used."
320 (setq list mm-codepage-iso-8859-list))
322 (let (cp windows iso)
324 (setq cp (intern (format "cp%d" (car i)))
325 windows (intern (format "windows-%d" (car i)))
326 iso (intern (format "iso-8859-%d" (cdr i))))
327 (setq cp (intern (format "cp%d" i))
328 windows (intern (format "windows-%d" i))))
329 (unless (mm-coding-system-p windows)
330 (if (mm-coding-system-p cp)
331 (add-to-list 'mm-charset-synonym-alist (cons windows cp))
332 (add-to-list 'mm-charset-synonym-alist (cons windows iso)))))))
334 (defun mm-setup-codepage-ibm (&optional list)
335 "Add appropriate entries to `mm-charset-synonym-alist'.
336 Unless LIST is given, `mm-codepage-ibm-list' is used."
338 (setq list mm-codepage-ibm-list))
339 (dolist (number list)
340 (let ((ibm (intern (format "ibm%d" number)))
341 (cp (intern (format "cp%d" number))))
342 (when (and (not (mm-coding-system-p ibm))
343 (mm-coding-system-p cp))
344 (add-to-list 'mm-charset-synonym-alist (cons ibm cp))))))
347 (mm-setup-codepage-iso-8859)
348 (mm-setup-codepage-ibm)
350 (defcustom mm-charset-override-alist
351 `((iso-8859-1 . windows-1252))
352 "A mapping from undesired charset names to their replacement.
354 You may add pairs like (iso-8859-1 . windows-1252) here,
355 i.e. treat iso-8859-1 as windows-1252. windows-1252 is a
356 superset of iso-8859-1."
357 :type '(list (set :inline t
358 (const (iso-8859-1 . windows-1252))
359 (const (undecided . windows-1252)))
362 (cons (symbol :tag "From charset")
363 (symbol :tag "To charset"))))
364 :version "22.1" ;; Gnus 5.10.9
367 (defcustom mm-charset-eval-alist
368 (if (featurep 'xemacs)
369 nil ;; I don't know what would be useful for XEmacs.
370 '(;; Emacs 21 offers 1250 1251 1253 1257. Emacs 22 provides autoloads for
371 ;; 1250-1258 (i.e. `mm-codepage-setup' does nothing).
372 (windows-1250 . (mm-codepage-setup 1250 t))
373 (windows-1251 . (mm-codepage-setup 1251 t))
374 (windows-1253 . (mm-codepage-setup 1253 t))
375 (windows-1257 . (mm-codepage-setup 1257 t))))
376 "An alist of (CHARSET . FORM) pairs.
377 If an article is encoded in an unknown CHARSET, FORM is
378 evaluated. This allows to load additional libraries providing
379 charsets on demand. If supported by your Emacs version, you
380 could use `autoload-coding-system' here."
381 :version "22.1" ;; Gnus 5.10.9
382 :type '(list (set :inline t
383 (const (windows-1250 . (mm-codepage-setup 1250 t)))
384 (const (windows-1251 . (mm-codepage-setup 1251 t)))
385 (const (windows-1253 . (mm-codepage-setup 1253 t)))
386 (const (windows-1257 . (mm-codepage-setup 1257 t)))
387 (const (cp850 . (mm-codepage-setup 850 nil))))
390 (cons (symbol :tag "charset")
391 (symbol :tag "form"))))
394 (defvar mm-binary-coding-system
396 ((mm-coding-system-p 'binary) 'binary)
397 ((mm-coding-system-p 'no-conversion) 'no-conversion)
399 "100% binary coding system.")
401 (defvar mm-text-coding-system
402 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
403 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
404 (and (mm-coding-system-p 'raw-text) 'raw-text))
405 mm-binary-coding-system)
406 "Text-safe coding system (For removing ^M).")
408 (defvar mm-text-coding-system-for-write nil
409 "Text coding system for write.")
411 (defvar mm-auto-save-coding-system
413 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
414 (if (memq system-type '(windows-nt ms-dos ms-windows))
415 (if (mm-coding-system-p 'utf-8-emacs-dos)
416 'utf-8-emacs-dos mm-binary-coding-system)
418 ((mm-coding-system-p 'emacs-mule)
419 (if (memq system-type '(windows-nt ms-dos ms-windows))
420 (if (mm-coding-system-p 'emacs-mule-dos)
421 'emacs-mule-dos mm-binary-coding-system)
423 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
424 (t mm-binary-coding-system))
425 "Coding system of auto save file.")
427 (defvar mm-universal-coding-system mm-auto-save-coding-system
428 "The universal coding system.")
430 ;; Fixme: some of the cars here aren't valid MIME charsets. That
431 ;; should only matter with XEmacs, though.
432 (defvar mm-mime-mule-charset-alist
434 (iso-8859-1 latin-iso8859-1)
435 (iso-8859-2 latin-iso8859-2)
436 (iso-8859-3 latin-iso8859-3)
437 (iso-8859-4 latin-iso8859-4)
438 (iso-8859-5 cyrillic-iso8859-5)
439 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
440 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
441 ;; charset is koi8-r, not iso-8859-5.
442 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
443 (iso-8859-6 arabic-iso8859-6)
444 (iso-8859-7 greek-iso8859-7)
445 (iso-8859-8 hebrew-iso8859-8)
446 (iso-8859-9 latin-iso8859-9)
447 (iso-8859-14 latin-iso8859-14)
448 (iso-8859-15 latin-iso8859-15)
449 (viscii vietnamese-viscii-lower)
450 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
451 (euc-kr korean-ksc5601)
452 (gb2312 chinese-gb2312)
454 (gb18030 gb18030-2-byte
455 gb18030-4-byte-bmp gb18030-4-byte-smp
456 gb18030-4-byte-ext-1 gb18030-4-byte-ext-2)
457 (big5 chinese-big5-1 chinese-big5-2)
459 (thai-tis620 thai-tis620)
460 (windows-1251 cyrillic-iso8859-5)
461 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
462 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
463 latin-jisx0201 japanese-jisx0208-1978
464 chinese-gb2312 japanese-jisx0208
465 korean-ksc5601 japanese-jisx0212)
466 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
467 latin-jisx0201 japanese-jisx0208-1978
468 chinese-gb2312 japanese-jisx0208
469 korean-ksc5601 japanese-jisx0212
470 chinese-cns11643-1 chinese-cns11643-2)
471 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
472 cyrillic-iso8859-5 greek-iso8859-7
473 latin-jisx0201 japanese-jisx0208-1978
474 chinese-gb2312 japanese-jisx0208
475 korean-ksc5601 japanese-jisx0212
476 chinese-cns11643-1 chinese-cns11643-2
477 chinese-cns11643-3 chinese-cns11643-4
478 chinese-cns11643-5 chinese-cns11643-6
480 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
481 japanese-jisx0213-1 japanese-jisx0213-2)
482 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
483 ,(cond ((fboundp 'unicode-precedence-list)
484 (cons 'utf-8 (delq 'ascii (mapcar 'charset-name
485 (unicode-precedence-list)))))
486 ((or (not (fboundp 'charsetp)) ;; non-Mule case
487 (charsetp 'unicode-a)
488 (not (mm-coding-system-p 'mule-utf-8)))
489 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e))
490 (t ;; If we have utf-8 we're in Mule 5+.
493 (coding-system-get 'mule-utf-8 'safe-charsets))))))
494 "Alist of MIME-charset/MULE-charsets.")
496 (defun mm-enrich-utf-8-by-mule-ucs ()
497 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
498 This function will run when the `un-define' module is loaded under
499 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
500 with Mule charsets. It is completely useless for Emacs."
501 (when (boundp 'unicode-basic-translation-charset-order-list)
507 'unicode-basic-translation-charset-order-list))))
508 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
511 (setq mm-mime-mule-charset-alist
512 (nconc mm-mime-mule-charset-alist
513 (list (cons 'utf-8 val))))))
516 ;; Correct by construction, but should be unnecessary for Emacs:
517 (if (featurep 'xemacs)
518 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
519 (when (and (fboundp 'coding-system-list)
520 (fboundp 'sort-coding-systems))
521 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
525 mime (or (coding-system-get cs :mime-charset); Emacs 23 (unicode)
526 (coding-system-get cs 'mime-charset)))
528 (not (eq t (setq mule
529 (coding-system-get cs 'safe-charsets))))
530 (not (assq mime alist)))
531 (push (cons mime (delq 'ascii mule)) alist)))
532 (setq mm-mime-mule-charset-alist (nreverse alist)))))
534 (defcustom mm-coding-system-priorities
535 (if (boundp 'current-language-environment)
536 (let ((lang (symbol-value 'current-language-environment)))
537 (cond ((string= lang "Japanese")
538 ;; Japanese users prefer iso-2022-jp to euc-japan or
539 ;; shift_jis, however iso-8859-1 should be used when
540 ;; there are only ASCII text and Latin-1 characters.
541 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
542 "Preferred coding systems for encoding outgoing messages.
544 More than one suitable coding system may be found for some text.
545 By default, the coding system with the highest priority is used
546 to encode outgoing messages (see `sort-coding-systems'). If this
547 variable is set, it overrides the default priority."
549 :type '(repeat (symbol :tag "Coding system"))
553 (defvar mm-use-find-coding-systems-region
554 (fboundp 'find-coding-systems-region)
555 "Use `find-coding-systems-region' to find proper coding systems.
557 Setting it to nil is useful on Emacsen supporting Unicode if sending
558 mail with multiple parts is preferred to sending a Unicode one.")
560 ;;; Internal variables:
564 (defun mm-mule-charset-to-mime-charset (charset)
565 "Return the MIME charset corresponding to the given Mule CHARSET."
566 (if (and (fboundp 'find-coding-systems-for-charsets)
567 (fboundp 'sort-coding-systems))
568 (let ((css (sort (sort-coding-systems
569 (find-coding-systems-for-charsets (list charset)))
570 'mm-sort-coding-systems-predicate))
572 (while (and (not mime)
574 (when (setq cs (pop css))
575 (setq mime (or (coding-system-get cs :mime-charset)
576 (coding-system-get cs 'mime-charset)))))
578 (let ((alist (mapcar (lambda (cs)
579 (assq cs mm-mime-mule-charset-alist))
580 (sort (mapcar 'car mm-mime-mule-charset-alist)
581 'mm-sort-coding-systems-predicate)))
584 (when (memq charset (cdar alist))
585 (setq out (caar alist)
590 (defun mm-charset-to-coding-system (charset &optional lbt
592 "Return coding-system corresponding to CHARSET.
593 CHARSET is a symbol naming a MIME charset.
594 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
595 used as the line break code type of the coding system.
597 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
598 map undesired charset names to their replacement. This should
599 only be used for decoding, not for encoding."
600 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
601 (when (stringp charset)
602 (setq charset (intern (downcase charset))))
604 (setq charset (intern (format "%s-%s" charset lbt))))
608 ;; Running in a non-MULE environment.
609 ((or (null (mm-get-coding-system-list))
610 (not (fboundp 'coding-system-get)))
612 ;; Check override list quite early. Should only used for decoding, not for
615 (let ((cs (cdr (assq charset mm-charset-override-alist))))
616 (and cs (mm-coding-system-p cs) cs))))
618 ((eq charset 'us-ascii)
620 ;; Check to see whether we can handle this charset. (This depends
621 ;; on there being some coding system matching each `mime-charset'
622 ;; property defined, as there should be.)
623 ((and (mm-coding-system-p charset)
624 ;;; Doing this would potentially weed out incorrect charsets.
626 ;;; (eq charset (coding-system-get charset 'mime-charset))
629 ;; Eval expressions from `mm-charset-eval-alist'
630 ((let* ((el (assq charset mm-charset-eval-alist))
637 (condition-case nil (eval form) (error nil))
638 ;; (message "Failed to eval `%s'" form))
639 (mm-coding-system-p cs)
640 (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
642 ;; Translate invalid charsets.
643 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
645 (mm-coding-system-p cs)
647 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
650 ;; Last resort: search the coding system list for entries which
651 ;; have the right mime-charset in case the canonical name isn't
652 ;; defined (though it should be).
654 ;; mm-get-coding-system-list returns a list of cs without lbt.
656 (dolist (c (mm-get-coding-system-list))
658 (eq charset (or (coding-system-get c :mime-charset)
659 (coding-system-get c 'mime-charset))))
662 ;; Warn the user about unknown charset:
663 (if (fboundp 'gnus-message)
664 (gnus-message 7 "Unknown charset: %s" charset)
665 (message "Unknown charset: %s" charset)))
668 (defsubst mm-replace-chars-in-string (string from to)
669 (mm-subst-char-in-string from to string))
672 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
673 (boundp 'default-enable-multibyte-characters)
674 default-enable-multibyte-characters
675 (fboundp 'set-buffer-multibyte))
676 "True in Emacs with Mule.")
679 (defun mm-enable-multibyte ()
680 "Set the multibyte flag of the current buffer.
681 Only do this if the default value of `enable-multibyte-characters' is
682 non-nil. This is a no-op in XEmacs."
683 (set-buffer-multibyte 'to))
684 (defalias 'mm-enable-multibyte 'ignore))
687 (defun mm-disable-multibyte ()
688 "Unset the multibyte flag of in the current buffer.
689 This is a no-op in XEmacs."
690 (set-buffer-multibyte nil))
691 (defalias 'mm-disable-multibyte 'ignore)))
693 (defun mm-preferred-coding-system (charset)
694 ;; A typo in some Emacs versions.
695 (or (get-charset-property charset 'preferred-coding-system)
696 (get-charset-property charset 'prefered-coding-system)))
698 ;; Mule charsets shouldn't be used.
699 (defsubst mm-guess-charset ()
700 "Guess Mule charset from the language environment."
702 mail-parse-mule-charset ;; cached mule-charset
704 (setq mail-parse-mule-charset
705 (and (boundp 'current-language-environment)
708 (assoc current-language-environment
709 language-info-alist))))))
710 (if (or (not mail-parse-mule-charset)
711 (eq mail-parse-mule-charset 'ascii))
712 (setq mail-parse-mule-charset
713 (or (car (last (assq mail-parse-charset
714 mm-mime-mule-charset-alist)))
717 mail-parse-mule-charset)))
719 (defun mm-charset-after (&optional pos)
720 "Return charset of a character in current buffer at position POS.
721 If POS is nil, it defauls to the current point.
722 If POS is out of range, the value is nil.
723 If the charset is `composition', return the actual one."
724 (let ((char (char-after pos)) charset)
725 (if (< (mm-char-int char) 128)
726 (setq charset 'ascii)
727 ;; charset-after is fake in some Emacsen.
728 (setq charset (and (fboundp 'char-charset) (char-charset char)))
729 (if (eq charset 'composition) ; Mule 4
730 (let ((p (or pos (point))))
731 (cadr (find-charset-region p (1+ p))))
732 (if (and charset (not (memq charset '(ascii eight-bit-control
733 eight-bit-graphic))))
735 (mm-guess-charset))))))
737 (defun mm-mime-charset (charset)
738 "Return the MIME charset corresponding to the given Mule CHARSET."
739 (if (eq charset 'unknown)
740 (error "The message contains non-printable characters, please use attachment"))
741 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
742 ;; This exists in Emacs 20.
744 (and (mm-preferred-coding-system charset)
745 (or (coding-system-get
746 (mm-preferred-coding-system charset) :mime-charset)
748 (mm-preferred-coding-system charset) 'mime-charset)))
749 (and (eq charset 'ascii)
751 (mm-preferred-coding-system charset)
752 (mm-mule-charset-to-mime-charset charset))
753 ;; This is for XEmacs.
754 (mm-mule-charset-to-mime-charset charset)))
756 (if (fboundp 'delete-dups)
757 (defalias 'mm-delete-duplicates 'delete-dups)
758 (defun mm-delete-duplicates (list)
759 "Destructively remove `equal' duplicates from LIST.
760 Store the result in LIST and return it. LIST must be a proper list.
761 Of several `equal' occurrences of an element in LIST, the first
764 This is a compatibility function for Emacsen without `delete-dups'."
765 ;; Code from `subr.el' in Emacs 22:
768 (setcdr tail (delete (car tail) (cdr tail)))
769 (setq tail (cdr tail))))
772 ;; Fixme: This is used in places when it should be testing the
773 ;; default multibyteness. See mm-default-multibyte-p.
775 (if (and (not (featurep 'xemacs))
776 (boundp 'enable-multibyte-characters))
777 (defun mm-multibyte-p ()
778 "Non-nil if multibyte is enabled in the current buffer."
779 enable-multibyte-characters)
780 (defun mm-multibyte-p () (featurep 'mule))))
782 (defun mm-default-multibyte-p ()
783 "Return non-nil if the session is multibyte.
784 This affects whether coding conversion should be attempted generally."
786 (if (boundp 'default-enable-multibyte-characters)
787 default-enable-multibyte-characters
790 (defun mm-sort-coding-systems-predicate (a b)
793 ;; Note: invalid entries are dropped silently
794 (and (setq cs (mm-coding-system-p cs))
795 (coding-system-base cs)))
796 mm-coding-system-priorities)))
797 (and (setq a (mm-coding-system-p a))
798 (if (setq b (mm-coding-system-p b))
799 (> (length (memq (coding-system-base a) priorities))
800 (length (memq (coding-system-base b) priorities)))
804 (autoload 'latin-unity-massage-name "latin-unity")
805 (autoload 'latin-unity-maybe-remap "latin-unity")
806 (autoload 'latin-unity-representations-feasible-region "latin-unity")
807 (autoload 'latin-unity-representations-present-region "latin-unity")
808 (defvar latin-unity-coding-systems)
809 (defvar latin-unity-ucs-list))
811 (defun mm-xemacs-find-mime-charset-1 (begin end)
812 "Determine which MIME charset to use to send region as message.
813 This uses the XEmacs-specific latin-unity package to better handle the
814 case where identical characters from diverse ISO-8859-? character sets
815 can be encoded using a single one of the corresponding coding systems.
817 It treats `mm-coding-system-priorities' as the list of preferred
818 coding systems; a useful example setting for this list in Western
819 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
820 to the very standard Latin 1 coding system, and only move to coding
821 systems that are less supported as is necessary to encode the
822 characters that exist in the buffer.
824 Latin Unity doesn't know about those non-ASCII Roman characters that
825 are available in various East Asian character sets. As such, its
826 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
827 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
828 But this is very much a corner case, so don't worry about it."
829 (let ((systems mm-coding-system-priorities) csets psets curset)
831 ;; Load the Latin Unity library, if available.
832 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
833 (ignore-errors (require 'latin-unity)))
835 ;; Now, can we use it?
836 (if (featurep 'latin-unity)
838 (setq csets (latin-unity-representations-feasible-region begin end)
839 psets (latin-unity-representations-present-region begin end))
843 ;; Pass back the first coding system in the preferred list
844 ;; that can encode the whole region.
845 (dolist (curset systems)
846 (setq curset (latin-unity-massage-name 'buffer-default curset))
848 ;; If the coding system is a universal coding system, then
849 ;; it can certainly encode all the characters in the region.
850 (if (memq curset latin-unity-ucs-list)
851 (throw 'done (list curset)))
853 ;; If a coding system isn't universal, and isn't in
854 ;; the list that latin unity knows about, we can't
855 ;; decide whether to use it here. Leave that until later
856 ;; in `mm-find-mime-charset-region' function, whence we
858 (unless (memq curset latin-unity-coding-systems)
861 ;; Right, we know about this coding system, and it may
862 ;; conceivably be able to encode all the characters in
864 (if (latin-unity-maybe-remap begin end curset csets psets t)
865 (throw 'done (list curset))))
867 ;; Can't encode using anything from the
868 ;; `mm-coding-system-priorities' list.
869 ;; Leave `mm-find-mime-charset' to do most of the work.
872 ;; Right, latin unity isn't available; let `mm-find-charset-region'
873 ;; take its default action, which equally applies to GNU Emacs.
876 (defmacro mm-xemacs-find-mime-charset (begin end)
877 (when (featurep 'xemacs)
878 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
880 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
881 "Return the MIME charsets needed to encode the region between B and E.
882 nil means ASCII, a single-element list represents an appropriate MIME
883 charset, and a longer list means no appropriate charset."
885 ;; The return possibilities of this function are a mess...
886 (or (and (mm-multibyte-p)
887 mm-use-find-coding-systems-region
888 ;; Find the mime-charset of the most preferred coding
889 ;; system that has one.
890 (let ((systems (find-coding-systems-region b e)))
891 (when mm-coding-system-priorities
893 (sort systems 'mm-sort-coding-systems-predicate)))
894 (setq systems (delq 'compound-text systems))
895 (unless (equal systems '(undecided))
897 (let* ((head (pop systems))
898 (cs (or (coding-system-get head :mime-charset)
899 (coding-system-get head 'mime-charset))))
900 ;; The mime-charset (`x-ctext') of
901 ;; `compound-text' is not in the IANA list. We
902 ;; shouldn't normally use anything here with a
903 ;; mime-charset having an `x-' prefix.
904 ;; Fixme: Allow this to be overridden, since
905 ;; there is existing use of x-ctext.
906 ;; Also people apparently need the coding system
907 ;; `iso-2022-jp-3' (which Mule-UCS defines with
908 ;; mime-charset, though it's not valid).
910 (not (string-match "^[Xx]-" (symbol-name cs)))
911 ;; UTF-16 of any variety is invalid for
912 ;; text parts and, unfortunately, has
913 ;; mime-charset defined both in Mule-UCS
914 ;; and versions of Emacs. (The name
915 ;; might be `mule-utf-16...' or
917 (not (string-match "utf-16" (symbol-name cs))))
919 charsets (list cs))))))
921 ;; If we're XEmacs, and some coding system is appropriate,
922 ;; mm-xemacs-find-mime-charset will return an appropriate list.
923 ;; Otherwise, we'll get nil, and the next setq will get invoked.
924 (setq charsets (mm-xemacs-find-mime-charset b e))
926 ;; Fixme: won't work for unibyte Emacs 23:
928 ;; We're not multibyte, or a single coding system won't cover it.
930 (mm-delete-duplicates
931 (mapcar 'mm-mime-charset
933 (mm-find-charset-region b e))))))
936 (defmacro mm-with-unibyte-buffer (&rest forms)
937 "Create a temporary buffer, and evaluate FORMS there like `progn'.
938 Use unibyte mode for this."
939 `(let (default-enable-multibyte-characters)
940 (with-temp-buffer ,@forms)))
941 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
942 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
944 (defmacro mm-with-multibyte-buffer (&rest forms)
945 "Create a temporary buffer, and evaluate FORMS there like `progn'.
946 Use multibyte mode for this."
947 `(let ((default-enable-multibyte-characters t))
948 (with-temp-buffer ,@forms)))
949 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
950 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
952 (defmacro mm-with-unibyte-current-buffer (&rest forms)
953 "Evaluate FORMS with current buffer temporarily made unibyte.
954 Also bind `default-enable-multibyte-characters' to nil.
955 Equivalent to `progn' in XEmacs
957 NOTE: Use this macro with caution in multibyte buffers (it is not
958 worth using this macro in unibyte buffers of course). Use of
959 `(set-buffer-multibyte t)', which is run finally, is generally
960 harmful since it is likely to modify existing data in the buffer.
961 For instance, it converts \"\\300\\255\" into \"\\255\" in
963 (let ((multibyte (make-symbol "multibyte"))
964 (buffer (make-symbol "buffer")))
966 (let ((,multibyte enable-multibyte-characters)
967 (,buffer (current-buffer)))
969 (let (default-enable-multibyte-characters)
970 (set-buffer-multibyte nil)
973 (set-buffer-multibyte ,multibyte)))
974 (let (default-enable-multibyte-characters)
976 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
977 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
979 (defmacro mm-with-unibyte (&rest forms)
980 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
981 `(let (default-enable-multibyte-characters)
983 (put 'mm-with-unibyte 'lisp-indent-function 0)
984 (put 'mm-with-unibyte 'edebug-form-spec '(body))
986 (defmacro mm-with-multibyte (&rest forms)
987 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
988 `(let ((default-enable-multibyte-characters t))
990 (put 'mm-with-multibyte 'lisp-indent-function 0)
991 (put 'mm-with-multibyte 'edebug-form-spec '(body))
993 (defun mm-find-charset-region (b e)
994 "Return a list of Emacs charsets in the region B to E."
996 ((and (mm-multibyte-p)
997 (fboundp 'find-charset-region))
998 ;; Remove composition since the base charsets have been included.
999 ;; Remove eight-bit-*, treat them as ascii.
1000 (let ((css (find-charset-region b e)))
1001 (mapcar (lambda (cs) (setq css (delq cs css)))
1002 '(composition eight-bit-control eight-bit-graphic
1006 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
1009 (narrow-to-region b e)
1010 (goto-char (point-min))
1011 (skip-chars-forward "\0-\177")
1016 (and (boundp 'current-language-environment)
1017 (car (last (assq 'charset
1018 (assoc current-language-environment
1019 language-info-alist))))))
1020 (if (eq charset 'ascii) (setq charset nil))
1023 (car (last (assq mail-parse-charset
1024 mm-mime-mule-charset-alist)))))
1025 (list 'ascii (or charset 'latin-iso8859-1)))))))))
1027 (if (fboundp 'shell-quote-argument)
1028 (defalias 'mm-quote-arg 'shell-quote-argument)
1029 (defun mm-quote-arg (arg)
1030 "Return a version of ARG that is safe to evaluate in a shell."
1031 (let ((pos 0) new-pos accum)
1032 ;; *** bug: we don't handle newline characters properly
1033 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
1034 (push (substring arg pos new-pos) accum)
1036 (push (list (aref arg new-pos)) accum)
1037 (setq pos (1+ new-pos)))
1040 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
1042 (defun mm-auto-mode-alist ()
1043 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1044 (let ((alist auto-mode-alist)
1047 (when (listp (cdar alist))
1048 (push (car alist) out))
1052 (defvar mm-inhibit-file-name-handlers
1053 '(jka-compr-handler image-file-handler)
1054 "A list of handlers doing (un)compression (etc) thingies.")
1056 (defun mm-insert-file-contents (filename &optional visit beg end replace
1058 "Like `insert-file-contents', but only reads in the file.
1059 A buffer may be modified in several ways after reading into the buffer due
1060 to advanced Emacs features, such as file-name-handlers, format decoding,
1061 `find-file-hooks', etc.
1062 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
1063 This function ensures that none of these modifications will take place."
1064 (let* ((format-alist nil)
1065 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
1066 (default-major-mode 'fundamental-mode)
1067 (enable-local-variables nil)
1068 (after-insert-file-functions nil)
1069 (enable-local-eval nil)
1070 (inhibit-file-name-operation (if inhibit
1071 'insert-file-contents
1072 inhibit-file-name-operation))
1073 (inhibit-file-name-handlers
1075 (append mm-inhibit-file-name-handlers
1076 inhibit-file-name-handlers)
1077 inhibit-file-name-handlers))
1078 (ffh (if (boundp 'find-file-hook)
1081 (val (symbol-value ffh)))
1084 (insert-file-contents filename visit beg end replace)
1087 (defun mm-append-to-file (start end filename &optional codesys inhibit)
1088 "Append the contents of the region to the end of file FILENAME.
1089 When called from a function, expects three arguments,
1090 START, END and FILENAME. START and END are buffer positions
1091 saying what text to write.
1092 Optional fourth argument specifies the coding system to use when
1094 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1095 (let ((coding-system-for-write
1096 (or codesys mm-text-coding-system-for-write
1097 mm-text-coding-system))
1098 (inhibit-file-name-operation (if inhibit
1100 inhibit-file-name-operation))
1101 (inhibit-file-name-handlers
1103 (append mm-inhibit-file-name-handlers
1104 inhibit-file-name-handlers)
1105 inhibit-file-name-handlers)))
1106 (write-region start end filename t 'no-message)
1107 (message "Appended to %s" filename)))
1109 (defun mm-write-region (start end filename &optional append visit lockname
1110 coding-system inhibit)
1112 "Like `write-region'.
1113 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1114 (let ((coding-system-for-write
1115 (or coding-system mm-text-coding-system-for-write
1116 mm-text-coding-system))
1117 (inhibit-file-name-operation (if inhibit
1119 inhibit-file-name-operation))
1120 (inhibit-file-name-handlers
1122 (append mm-inhibit-file-name-handlers
1123 inhibit-file-name-handlers)
1124 inhibit-file-name-handlers)))
1125 (write-region start end filename append visit lockname)))
1127 ;; It is not a MIME function, but some MIME functions use it.
1128 (if (and (fboundp 'make-temp-file)
1130 (let ((def (symbol-function 'make-temp-file)))
1131 (and (byte-code-function-p def)
1132 (setq def (if (fboundp 'compiled-function-arglist)
1134 (eval (list 'compiled-function-arglist def))
1137 (eq (nth 3 def) 'suffix)))))
1138 (defalias 'mm-make-temp-file 'make-temp-file)
1139 ;; Stolen (and modified for Emacs 20 and XEmacs) from Emacs 22.
1140 (defun mm-make-temp-file (prefix &optional dir-flag suffix)
1141 "Create a temporary file.
1142 The returned file name (created by appending some random characters at the end
1143 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1144 is guaranteed to point to a newly created empty file.
1145 You can then use `write-region' to write new data into the file.
1147 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1149 If SUFFIX is non-nil, add that at the end of the file name."
1150 (let ((umask (default-file-modes))
1154 ;; Create temp files with strict access rights. It's easy to
1155 ;; loosen them later, whereas it's impossible to close the
1156 ;; time-window of loose permissions otherwise.
1157 (set-default-file-modes 448)
1158 (while (condition-case err
1164 (if (fboundp 'temp-directory)
1167 temporary-file-directory))))
1169 (setq file (concat file suffix)))
1171 (make-directory file)
1172 ;; NOTE: This is unsafe if Emacs 20
1173 ;; users and XEmacs users don't use
1174 ;; a secure temp directory.
1175 (gmm-write-region "" nil file nil 'silent
1178 (file-already-exists t)
1179 ;; The Emacs 20 and XEmacs versions of
1180 ;; `make-directory' issue `file-error'.
1181 (file-error (or (and (or (featurep 'xemacs)
1182 (= emacs-major-version 20))
1183 (file-exists-p file))
1184 (signal (car err) (cdr err)))))
1185 ;; the file was somehow created by someone else between
1186 ;; `make-temp-name' and `write-region', let's try again.
1190 (set-default-file-modes umask)))))
1192 (defun mm-image-load-path (&optional package)
1194 (dolist (path load-path (nreverse result))
1197 (setq dir (concat (file-name-directory
1198 (directory-file-name path))
1199 "etc/images/" (or package "gnus/")))))
1201 (push path result))))
1203 ;; Fixme: This doesn't look useful where it's used.
1204 (if (fboundp 'detect-coding-region)
1205 (defun mm-detect-coding-region (start end)
1206 "Like `detect-coding-region' except returning the best one."
1207 (let ((coding-systems
1208 (detect-coding-region start end)))
1209 (or (car-safe coding-systems)
1211 (defun mm-detect-coding-region (start end)
1212 (let ((point (point)))
1214 (skip-chars-forward "\0-\177" end)
1216 (if (eq (point) end) 'ascii (mm-guess-charset))
1217 (goto-char point)))))
1219 (if (fboundp 'coding-system-get)
1220 (defun mm-detect-mime-charset-region (start end)
1221 "Detect MIME charset of the text in the region between START and END."
1222 (let ((cs (mm-detect-coding-region start end)))
1223 (or (coding-system-get cs :mime-charset)
1224 (coding-system-get cs 'mime-charset))))
1225 (defun mm-detect-mime-charset-region (start end)
1226 "Detect MIME charset of the text in the region between START and END."
1227 (let ((cs (mm-detect-coding-region start end)))
1233 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1234 ;;; mm-util.el ends here