Merge branch 'master' into comment-cache
[emacs.git] / lisp / gnus / mm-util.el
blob89f397e3ed0368970be3ab67d05d87e075aa4a1c
1 ;;; mm-util.el --- Utility functions for Mule and low level things
3 ;; Copyright (C) 1998-2017 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;;; Code:
26 (eval-when-compile (require 'cl))
27 (require 'mail-prsvr)
28 (require 'timer)
30 (defvar mm-mime-mule-charset-alist)
32 (defun mm-ucs-to-char (codepoint)
33 "Convert Unicode codepoint to character."
34 (or (decode-char 'ucs codepoint) ?#))
36 (defvar mm-coding-system-list nil)
37 (defun mm-get-coding-system-list ()
38 "Get the coding system list."
39 (or mm-coding-system-list
40 (setq mm-coding-system-list (coding-system-list))))
42 (defun mm-coding-system-p (cs)
43 "Return CS if CS is a coding system."
44 (and (coding-system-p cs)
45 cs))
47 (defvar mm-charset-synonym-alist
49 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
50 ,@(unless (mm-coding-system-p 'x-ctext)
51 '((x-ctext . ctext)))
52 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_ in 8
53 ;; positions!
54 ,@(unless (mm-coding-system-p 'iso-8859-15)
55 '((iso-8859-15 . iso-8859-1)))
56 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
57 ,@(unless (mm-coding-system-p 'big5-hkscs)
58 '((big5-hkscs . big5)))
59 ;; A Microsoft misunderstanding.
60 ,@(when (and (not (mm-coding-system-p 'unicode))
61 (mm-coding-system-p 'utf-16-le))
62 '((unicode . utf-16-le)))
63 ;; A Microsoft misunderstanding.
64 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
65 (if (mm-coding-system-p 'cp949)
66 '((ks_c_5601-1987 . cp949))
67 '((ks_c_5601-1987 . euc-kr))))
68 ;; Windows-31J is Windows Codepage 932.
69 ,@(when (and (not (mm-coding-system-p 'windows-31j))
70 (mm-coding-system-p 'cp932))
71 '((windows-31j . cp932)))
72 ;; Charset name: GBK, Charset aliases: CP936, MS936, windows-936
73 ;; http://www.iana.org/assignments/charset-reg/GBK
74 ;; Emacs 22.1 has cp936, but not gbk, so we alias it:
75 ,@(when (and (not (mm-coding-system-p 'gbk))
76 (mm-coding-system-p 'cp936))
77 '((gbk . cp936)))
78 ;; UTF8 is a bogus name for UTF-8
79 ,@(when (and (not (mm-coding-system-p 'utf8))
80 (mm-coding-system-p 'utf-8))
81 '((utf8 . utf-8)))
82 ;; ISO8859-1 is a bogus name for ISO-8859-1
83 ,@(when (and (not (mm-coding-system-p 'iso8859-1))
84 (mm-coding-system-p 'iso-8859-1))
85 '((iso8859-1 . iso-8859-1)))
86 ;; ISO_8859-1 is a bogus name for ISO-8859-1
87 ,@(when (and (not (mm-coding-system-p 'iso_8859-1))
88 (mm-coding-system-p 'iso-8859-1))
89 '((iso_8859-1 . iso-8859-1)))
91 "A mapping from unknown or invalid charset names to the real charset names.")
93 ;; Note: this has to be defined before `mm-charset-to-coding-system'.
94 (defcustom mm-charset-eval-alist nil
95 "An alist of (CHARSET . FORM) pairs.
96 If an article is encoded in an unknown CHARSET, FORM is
97 evaluated. This allows the loading of additional libraries
98 providing charsets on demand. If supported by your Emacs
99 version, you could use `autoload-coding-system' here."
100 :version "22.1" ;; Gnus 5.10.9
101 :type '(list (repeat :inline t
102 :tag "Other options"
103 (cons (symbol :tag "charset")
104 (symbol :tag "form"))))
105 :group 'mime)
106 (put 'mm-charset-eval-alist 'risky-local-variable t)
108 (defvar mm-charset-override-alist)
110 ;; Note: this function has to be defined before `mm-charset-override-alist'
111 ;; since it will use this function in order to determine its default value
112 ;; when loading mm-util.elc.
113 (defun mm-charset-to-coding-system (charset &optional lbt
114 allow-override silent)
115 "Return coding-system corresponding to CHARSET.
116 CHARSET is a symbol naming a MIME charset.
117 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
118 used as the line break code type of the coding system.
120 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
121 map undesired charset names to their replacement. This should
122 only be used for decoding, not for encoding.
124 A non-nil value of SILENT means don't issue a warning even if CHARSET
125 is not available."
126 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
127 (when (stringp charset)
128 (setq charset (intern (downcase charset))))
129 (when lbt
130 (setq charset (intern (format "%s-%s" charset lbt))))
131 (cond
132 ((null charset)
133 charset)
134 ;; Running in a non-MULE environment.
135 ((or (null (mm-get-coding-system-list))
136 (not (fboundp 'coding-system-get)))
137 charset)
138 ;; Check override list quite early. Should only used for decoding, not for
139 ;; encoding!
140 ((and allow-override
141 (let ((cs (cdr (assq charset mm-charset-override-alist))))
142 (and cs (mm-coding-system-p cs) cs))))
143 ;; ascii
144 ((or (eq charset 'us-ascii)
145 (string-match "ansi.x3.4" (symbol-name charset)))
146 'ascii)
147 ;; Check to see whether we can handle this charset. (This depends
148 ;; on there being some coding system matching each `mime-charset'
149 ;; property defined, as there should be.)
150 ((and (mm-coding-system-p charset)
151 ;;; Doing this would potentially weed out incorrect charsets.
152 ;;; charset
153 ;;; (eq charset (coding-system-get charset 'mime-charset))
155 charset)
156 ;; Use coding system Emacs knows.
157 ((and (fboundp 'coding-system-from-name)
158 (coding-system-from-name charset)))
159 ;; Eval expressions from `mm-charset-eval-alist'
160 ((let* ((el (assq charset mm-charset-eval-alist))
161 (cs (car el))
162 (form (cdr el)))
163 (and cs
164 form
165 (prog2
166 ;; Avoid errors...
167 (condition-case nil (eval form) (error nil))
168 ;; (message "Failed to eval `%s'" form))
169 (mm-coding-system-p cs)
170 (message "Added charset `%s' via `mm-charset-eval-alist'" cs))
171 cs)))
172 ;; Translate invalid charsets.
173 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
174 (and cs
175 (mm-coding-system-p cs)
176 ;; (message
177 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
178 ;; cs charset)
179 cs)))
180 ;; Last resort: search the coding system list for entries which
181 ;; have the right mime-charset in case the canonical name isn't
182 ;; defined (though it should be).
183 ((let (cs)
184 ;; mm-get-coding-system-list returns a list of cs without lbt.
185 ;; Do we need -lbt?
186 (dolist (c (mm-get-coding-system-list))
187 (if (and (null cs)
188 (eq charset (or (coding-system-get c :mime-charset)
189 (coding-system-get c 'mime-charset))))
190 (setq cs c)))
191 (unless (or silent cs)
192 ;; Warn the user about unknown charset:
193 (if (fboundp 'gnus-message)
194 (gnus-message 7 "Unknown charset: %s" charset)
195 (message "Unknown charset: %s" charset)))
196 cs))))
198 ;; Note: `mm-charset-to-coding-system' has to be defined before this.
199 (defcustom mm-charset-override-alist
200 ;; Note: pairs that cannot be used in the Emacs version currently running
201 ;; will be removed.
202 '((gb2312 . gbk)
203 (iso-8859-1 . windows-1252)
204 (iso-8859-8 . windows-1255)
205 (iso-8859-9 . windows-1254))
206 "A mapping from undesired charset names to their replacement.
208 You may add pairs like (iso-8859-1 . windows-1252) here,
209 i.e. treat iso-8859-1 as windows-1252. windows-1252 is a
210 superset of iso-8859-1."
211 :type
212 '(list
213 :convert-widget
214 (lambda (widget)
215 (let ((defaults
216 (delq nil
217 (mapcar (lambda (pair)
218 (if (mm-charset-to-coding-system (cdr pair)
219 nil nil t)
220 pair))
221 '((gb2312 . gbk)
222 (iso-8859-1 . windows-1252)
223 (iso-8859-8 . windows-1255)
224 (iso-8859-9 . windows-1254)
225 (undecided . windows-1252)))))
226 (val (copy-sequence (default-value 'mm-charset-override-alist)))
227 pair rest)
228 (while val
229 (push (if (and (prog1
230 (setq pair (assq (caar val) defaults))
231 (setq defaults (delq pair defaults)))
232 (equal (car val) pair))
233 `(const ,pair)
234 `(cons :format "%v"
235 (const :format "(%v" ,(caar val))
236 (symbol :size 3 :format " . %v)\n" ,(cdar val))))
237 rest)
238 (setq val (cdr val)))
239 (while defaults
240 (push `(const ,(pop defaults)) rest))
241 (widget-convert
242 'list
243 `(set :inline t :format "%v" ,@(nreverse rest))
244 `(repeat :inline t :tag "Other options"
245 (cons :format "%v"
246 (symbol :size 3 :format "(%v")
247 (symbol :size 3 :format " . %v)\n")))))))
248 ;; Remove pairs that cannot be used in the Emacs version currently
249 ;; running. Note that this section will be evaluated when loading
250 ;; mm-util.elc.
251 :set (lambda (symbol value)
252 (custom-set-default
253 symbol (delq nil
254 (mapcar (lambda (pair)
255 (if (mm-charset-to-coding-system (cdr pair)
256 nil nil t)
257 pair))
258 value))))
259 :version "22.1" ;; Gnus 5.10.9
260 :group 'mime)
262 (defvar mm-binary-coding-system
263 (cond
264 ((mm-coding-system-p 'binary) 'binary)
265 ((mm-coding-system-p 'no-conversion) 'no-conversion)
266 (t nil))
267 "100% binary coding system.")
269 (defvar mm-text-coding-system
270 (or (if (memq system-type '(windows-nt ms-dos))
271 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
272 (and (mm-coding-system-p 'raw-text) 'raw-text))
273 mm-binary-coding-system)
274 "Text-safe coding system (For removing ^M).")
276 (defvar mm-text-coding-system-for-write nil
277 "Text coding system for write.")
279 (defvar mm-auto-save-coding-system
280 (cond
281 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
282 (if (memq system-type '(windows-nt ms-dos))
283 (if (mm-coding-system-p 'utf-8-emacs-dos)
284 'utf-8-emacs-dos mm-binary-coding-system)
285 'utf-8-emacs))
286 ((mm-coding-system-p 'emacs-mule)
287 (if (memq system-type '(windows-nt ms-dos))
288 (if (mm-coding-system-p 'emacs-mule-dos)
289 'emacs-mule-dos mm-binary-coding-system)
290 'emacs-mule))
291 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
292 (t mm-binary-coding-system))
293 "Coding system of auto save file.")
295 (defvar mm-universal-coding-system mm-auto-save-coding-system
296 "The universal coding system.")
298 ;; Fixme: some of the cars here aren't valid MIME charsets. That
299 ;; should only matter with XEmacs, though.
300 (defvar mm-mime-mule-charset-alist
301 '((us-ascii ascii)
302 (iso-8859-1 latin-iso8859-1)
303 (iso-8859-2 latin-iso8859-2)
304 (iso-8859-3 latin-iso8859-3)
305 (iso-8859-4 latin-iso8859-4)
306 (iso-8859-5 cyrillic-iso8859-5)
307 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
308 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
309 ;; charset is koi8-r, not iso-8859-5.
310 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
311 (iso-8859-6 arabic-iso8859-6)
312 (iso-8859-7 greek-iso8859-7)
313 (iso-8859-8 hebrew-iso8859-8)
314 (iso-8859-9 latin-iso8859-9)
315 (iso-8859-14 latin-iso8859-14)
316 (iso-8859-15 latin-iso8859-15)
317 (viscii vietnamese-viscii-lower)
318 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
319 (euc-kr korean-ksc5601)
320 (gb2312 chinese-gb2312)
321 (gbk chinese-gbk)
322 (gb18030 gb18030-2-byte
323 gb18030-4-byte-bmp gb18030-4-byte-smp
324 gb18030-4-byte-ext-1 gb18030-4-byte-ext-2)
325 (big5 chinese-big5-1 chinese-big5-2)
326 (tibetan tibetan)
327 (thai-tis620 thai-tis620)
328 (windows-1251 cyrillic-iso8859-5)
329 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
330 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
331 latin-jisx0201 japanese-jisx0208-1978
332 chinese-gb2312 japanese-jisx0208
333 korean-ksc5601 japanese-jisx0212)
334 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
335 latin-jisx0201 japanese-jisx0208-1978
336 chinese-gb2312 japanese-jisx0208
337 korean-ksc5601 japanese-jisx0212
338 chinese-cns11643-1 chinese-cns11643-2)
339 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
340 cyrillic-iso8859-5 greek-iso8859-7
341 latin-jisx0201 japanese-jisx0208-1978
342 chinese-gb2312 japanese-jisx0208
343 korean-ksc5601 japanese-jisx0212
344 chinese-cns11643-1 chinese-cns11643-2
345 chinese-cns11643-3 chinese-cns11643-4
346 chinese-cns11643-5 chinese-cns11643-6
347 chinese-cns11643-7)
348 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
349 japanese-jisx0213-1 japanese-jisx0213-2)
350 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
351 (utf-8))
352 "Alist of MIME-charset/MULE-charsets.")
354 ;; Correct by construction, but should be unnecessary for Emacs:
355 (when (and (fboundp 'coding-system-list)
356 (fboundp 'sort-coding-systems))
357 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
358 cs mime mule alist)
359 (while css
360 (setq cs (pop css)
361 mime (or (coding-system-get cs :mime-charset) ; Emacs 23 (unicode)
362 (coding-system-get cs 'mime-charset)))
363 (when (and mime
364 (not (eq t (setq mule
365 (coding-system-get cs 'safe-charsets))))
366 (not (assq mime alist)))
367 (push (cons mime (delq 'ascii mule)) alist)))
368 (setq mm-mime-mule-charset-alist (nreverse alist))))
370 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
371 "A list of special charsets.
372 Valid elements include:
373 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
374 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
377 (defvar mm-iso-8859-15-compatible
378 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
379 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
380 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
382 (defvar mm-iso-8859-x-to-15-table
383 (and (fboundp 'coding-system-p)
384 (mm-coding-system-p 'iso-8859-15)
385 (mapcar
386 (lambda (cs)
387 (if (mm-coding-system-p (car cs))
388 (let ((c (string-to-char
389 (decode-coding-string "\341" (car cs)))))
390 (cons (char-charset c)
391 (cons
392 (- (string-to-char
393 (decode-coding-string "\341" 'iso-8859-15)) c)
394 (string-to-list (decode-coding-string (car (cdr cs))
395 (car cs))))))
396 '(gnus-charset 0)))
397 mm-iso-8859-15-compatible))
398 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
400 (defcustom mm-coding-system-priorities
401 (and (string-match "\\`Japanese" current-language-environment)
402 ;; Japanese users prefer iso-2022-jp to others usually used
403 ;; for `buffer-file-coding-system', however iso-8859-1 should
404 ;; be used when there are only ASCII and Latin-1 characters.
405 '(iso-8859-1 iso-2022-jp utf-8))
406 "Preferred coding systems for encoding outgoing messages.
408 More than one suitable coding system may be found for some text.
409 By default, the coding system with the highest priority is used
410 to encode outgoing messages (see `sort-coding-systems'). If this
411 variable is set, it overrides the default priority."
412 :version "24.4"
413 :type '(repeat (symbol :tag "Coding system"))
414 :group 'mime)
416 ;; ??
417 (defvar mm-use-find-coding-systems-region t
418 "Use `find-coding-systems-region' to find proper coding systems.
420 Setting it to nil is useful on Emacsen supporting Unicode if sending
421 mail with multiple parts is preferred to sending a Unicode one.")
423 (defcustom mm-extra-numeric-entities
424 (mapcar
425 (lambda (item)
426 (cons (car item) (mm-ucs-to-char (cdr item))))
427 '((#x80 . #x20AC) (#x82 . #x201A) (#x83 . #x0192) (#x84 . #x201E)
428 (#x85 . #x2026) (#x86 . #x2020) (#x87 . #x2021) (#x88 . #x02C6)
429 (#x89 . #x2030) (#x8A . #x0160) (#x8B . #x2039) (#x8C . #x0152)
430 (#x8E . #x017D) (#x91 . #x2018) (#x92 . #x2019) (#x93 . #x201C)
431 (#x94 . #x201D) (#x95 . #x2022) (#x96 . #x2013) (#x97 . #x2014)
432 (#x98 . #x02DC) (#x99 . #x2122) (#x9A . #x0161) (#x9B . #x203A)
433 (#x9C . #x0153) (#x9E . #x017E) (#x9F . #x0178)))
434 "*Alist of extra numeric entities and characters other than ISO 10646.
435 This table is used for decoding extra numeric entities to characters,
436 like \"&#128;\" to the euro sign, mainly in html messages."
437 :type '(alist :key-type character :value-type character)
438 :group 'mime)
440 ;;; Internal variables:
442 ;;; Functions:
444 (defun mm-mule-charset-to-mime-charset (charset)
445 "Return the MIME charset corresponding to the given Mule CHARSET."
446 (let ((css (sort (sort-coding-systems
447 (find-coding-systems-for-charsets (list charset)))
448 'mm-sort-coding-systems-predicate))
449 cs mime)
450 (while (and (not mime)
451 css)
452 (when (setq cs (pop css))
453 (setq mime (or (coding-system-get cs :mime-charset)
454 (coding-system-get cs 'mime-charset)))))
455 mime))
457 (defun mm-enable-multibyte ()
458 "Set the multibyte flag of the current buffer.
459 Only do this if the default value of `enable-multibyte-characters' is
460 non-nil."
461 (set-buffer-multibyte 'to))
463 (defun mm-disable-multibyte ()
464 "Unset the multibyte flag of in the current buffer."
465 (set-buffer-multibyte nil))
467 (defun mm-preferred-coding-system (charset)
468 ;; A typo in some Emacs versions.
469 (or (get-charset-property charset 'preferred-coding-system)
470 (get-charset-property charset 'prefered-coding-system)))
472 ;; Mule charsets shouldn't be used.
473 (defsubst mm-guess-charset ()
474 "Guess Mule charset from the language environment."
476 mail-parse-mule-charset ;; cached mule-charset
477 (progn
478 (setq mail-parse-mule-charset
479 (and (car (last
480 (assq 'charset
481 (assoc current-language-environment
482 language-info-alist))))))
483 (if (or (not mail-parse-mule-charset)
484 (eq mail-parse-mule-charset 'ascii))
485 (setq mail-parse-mule-charset
486 (or (car (last (assq mail-parse-charset
487 mm-mime-mule-charset-alist)))
488 ;; default
489 'latin-iso8859-1)))
490 mail-parse-mule-charset)))
492 (defun mm-charset-after (&optional pos)
493 "Return charset of a character in current buffer at position POS.
494 If POS is nil, it defaults to the current point.
495 If POS is out of range, the value is nil."
496 (let ((char (char-after pos)) charset)
497 (if (< char 128)
498 (setq charset 'ascii)
499 (setq charset (char-charset char))
500 (if (and charset (not (memq charset '(ascii eight-bit-control
501 eight-bit-graphic))))
502 charset
503 (mm-guess-charset)))))
505 (defun mm-mime-charset (charset)
506 "Return the MIME charset corresponding to the given Mule CHARSET."
507 (when (eq charset 'unknown)
508 (error "The message contains non-printable characters, please use attachment"))
510 (and (mm-preferred-coding-system charset)
511 (coding-system-get (mm-preferred-coding-system charset) 'mime-charset))
512 (and (eq charset 'ascii)
513 'us-ascii)
514 (mm-preferred-coding-system charset)
515 (mm-mule-charset-to-mime-charset charset)))
517 ;; Fixme: This is used in places when it should be testing the
518 ;; default multibyteness.
519 (defun mm-multibyte-p ()
520 "Non-nil if multibyte is enabled in the current buffer."
521 enable-multibyte-characters)
523 (defun mm-iso-8859-x-to-15-region (&optional b e)
524 (let (charset item c inconvertible)
525 (save-restriction
526 (if e (narrow-to-region b e))
527 (goto-char (point-min))
528 (skip-chars-forward "\0-\177")
529 (while (not (eobp))
530 (cond
531 ((not (setq item (assq (char-charset (setq c (char-after)))
532 mm-iso-8859-x-to-15-table)))
533 (forward-char))
534 ((memq c (cdr (cdr item)))
535 (setq inconvertible t)
536 (forward-char))
538 (insert-before-markers (prog1 (+ c (car (cdr item)))
539 (delete-char 1)))))
540 (skip-chars-forward "\0-\177")))
541 (not inconvertible)))
543 (defun mm-sort-coding-systems-predicate (a b)
544 (let ((priorities
545 (mapcar (lambda (cs)
546 ;; Note: invalid entries are dropped silently
547 (and (setq cs (mm-coding-system-p cs))
548 (coding-system-base cs)))
549 mm-coding-system-priorities)))
550 (and (setq a (mm-coding-system-p a))
551 (if (setq b (mm-coding-system-p b))
552 (> (length (memq (coding-system-base a) priorities))
553 (length (memq (coding-system-base b) priorities)))
554 t))))
556 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
557 "Return the MIME charsets needed to encode the region between B and E.
558 nil means ASCII, a single-element list represents an appropriate MIME
559 charset, and a longer list means no appropriate charset."
560 (let (charsets)
561 ;; The return possibilities of this function are a mess...
562 (or (and (mm-multibyte-p)
563 mm-use-find-coding-systems-region
564 ;; Find the mime-charset of the most preferred coding
565 ;; system that has one.
566 (let ((systems (find-coding-systems-region b e)))
567 (when mm-coding-system-priorities
568 (setq systems
569 (sort systems 'mm-sort-coding-systems-predicate)))
570 (setq systems (delq 'compound-text systems))
571 (unless (equal systems '(undecided))
572 (while systems
573 (let* ((head (pop systems))
574 (cs (or (coding-system-get head :mime-charset)
575 (coding-system-get head 'mime-charset))))
576 ;; The mime-charset (`x-ctext') of
577 ;; `compound-text' is not in the IANA list. We
578 ;; shouldn't normally use anything here with a
579 ;; mime-charset having an `x-' prefix.
580 ;; Fixme: Allow this to be overridden, since
581 ;; there is existing use of x-ctext.
582 ;; Also people apparently need the coding system
583 ;; `iso-2022-jp-3' (which Mule-UCS defines with
584 ;; mime-charset, though it's not valid).
585 (if (and cs
586 (not (string-match "^[Xx]-" (symbol-name cs)))
587 ;; UTF-16 of any variety is invalid for
588 ;; text parts and, unfortunately, has
589 ;; mime-charset defined both in Mule-UCS
590 ;; and versions of Emacs. (The name
591 ;; might be `mule-utf-16...' or
592 ;; `utf-16...'.)
593 (not (string-match "utf-16" (symbol-name cs))))
594 (setq systems nil
595 charsets (list cs))))))
596 charsets))
597 ;; We're not multibyte, or a single coding system won't cover it.
598 (setq charsets
599 (delete-dups
600 (mapcar 'mm-mime-charset
601 (delq 'ascii
602 (mm-find-charset-region b e))))))
603 (if (and (> (length charsets) 1)
604 (memq 'iso-8859-15 charsets)
605 (memq 'iso-8859-15 hack-charsets)
606 (save-excursion (mm-iso-8859-x-to-15-region b e)))
607 (dolist (x mm-iso-8859-15-compatible)
608 (setq charsets (delq (car x) charsets))))
609 (if (and (memq 'iso-2022-jp-2 charsets)
610 (memq 'iso-2022-jp-2 hack-charsets))
611 (setq charsets (delq 'iso-2022-jp charsets)))
612 charsets))
614 (defmacro mm-with-unibyte-buffer (&rest forms)
615 "Create a temporary buffer, and evaluate FORMS there like `progn'.
616 Use unibyte mode for this."
617 `(with-temp-buffer
618 (mm-disable-multibyte)
619 ,@forms))
620 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
621 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
623 (defmacro mm-with-multibyte-buffer (&rest forms)
624 "Create a temporary buffer, and evaluate FORMS there like `progn'.
625 Use multibyte mode for this."
626 `(with-temp-buffer
627 (mm-enable-multibyte)
628 ,@forms))
629 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
630 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
632 (defmacro mm-with-unibyte-current-buffer (&rest forms)
633 "Evaluate FORMS with current buffer temporarily made unibyte.
635 Note: We recommend not using this macro any more; there should be
636 better ways to do a similar thing. The previous version of this macro
637 bound the default value of `enable-multibyte-characters' to nil while
638 evaluating FORMS but it is no longer done. So, some programs assuming
639 it if any may malfunction."
640 (declare (obsolete nil "25.1") (indent 0) (debug t))
641 (let ((multibyte (make-symbol "multibyte")))
642 `(let ((,multibyte enable-multibyte-characters))
643 (when ,multibyte
644 (set-buffer-multibyte nil))
645 (prog1
646 (progn ,@forms)
647 (when ,multibyte
648 (set-buffer-multibyte t))))))
650 (defun mm-find-charset-region (b e)
651 "Return a list of Emacs charsets in the region B to E."
652 (cond
653 ((mm-multibyte-p)
654 ;; Remove composition since the base charsets have been included.
655 ;; Remove eight-bit-*, treat them as ascii.
656 (let ((css (find-charset-region b e)))
657 (dolist (cs '(composition eight-bit-control eight-bit-graphic control-1))
658 (setq css (delq cs css)))
659 css))
661 ;; We are in a unibyte buffer, so we futz around a bit.
662 (save-excursion
663 (save-restriction
664 (narrow-to-region b e)
665 (goto-char (point-min))
666 (skip-chars-forward "\0-\177")
667 (if (eobp)
668 '(ascii)
669 (let (charset)
670 (setq charset (car (last (assq 'charset
671 (assoc current-language-environment
672 language-info-alist)))))
673 (if (eq charset 'ascii) (setq charset nil))
674 (or charset
675 (setq charset
676 (car (last (assq mail-parse-charset
677 mm-mime-mule-charset-alist)))))
678 (list 'ascii (or charset 'latin-iso8859-1)))))))))
680 (defun mm-auto-mode-alist ()
681 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
682 (let ((alist auto-mode-alist)
683 out)
684 (while alist
685 (when (listp (cdar alist))
686 (push (car alist) out))
687 (pop alist))
688 (nreverse out)))
690 (defvar mm-inhibit-file-name-handlers
691 '(jka-compr-handler image-file-handler epa-file-handler)
692 "A list of handlers doing (un)compression (etc) thingies.")
694 (defun mm-insert-file-contents (filename &optional visit beg end replace
695 inhibit)
696 "Like `insert-file-contents', but only reads in the file.
697 A buffer may be modified in several ways after reading into the buffer due
698 to advanced Emacs features, such as file-name-handlers, format decoding,
699 `find-file-hook', etc.
700 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
701 This function ensures that none of these modifications will take place."
702 (letf* ((format-alist nil)
703 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
704 ((default-value 'major-mode) 'fundamental-mode)
705 (enable-local-variables nil)
706 (after-insert-file-functions nil)
707 (enable-local-eval nil)
708 (inhibit-file-name-operation (if inhibit
709 'insert-file-contents
710 inhibit-file-name-operation))
711 (inhibit-file-name-handlers
712 (if inhibit
713 (append mm-inhibit-file-name-handlers
714 inhibit-file-name-handlers)
715 inhibit-file-name-handlers))
716 (find-file-hook nil))
717 (insert-file-contents filename visit beg end replace)))
719 (defun mm-append-to-file (start end filename &optional codesys inhibit)
720 "Append the contents of the region to the end of file FILENAME.
721 When called from a function, expects three arguments,
722 START, END and FILENAME. START and END are buffer positions
723 saying what text to write.
724 Optional fourth argument specifies the coding system to use when
725 encoding the file.
726 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
727 (let ((coding-system-for-write
728 (or codesys mm-text-coding-system-for-write
729 mm-text-coding-system))
730 (inhibit-file-name-operation (if inhibit
731 'append-to-file
732 inhibit-file-name-operation))
733 (inhibit-file-name-handlers
734 (if inhibit
735 (append mm-inhibit-file-name-handlers
736 inhibit-file-name-handlers)
737 inhibit-file-name-handlers)))
738 (write-region start end filename t 'no-message)
739 (message "Appended to %s" filename)))
741 (defun mm-write-region (start end filename &optional append visit lockname
742 coding-system inhibit)
744 "Like `write-region'.
745 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
746 (let ((coding-system-for-write
747 (or coding-system mm-text-coding-system-for-write
748 mm-text-coding-system))
749 (inhibit-file-name-operation (if inhibit
750 'write-region
751 inhibit-file-name-operation))
752 (inhibit-file-name-handlers
753 (if inhibit
754 (append mm-inhibit-file-name-handlers
755 inhibit-file-name-handlers)
756 inhibit-file-name-handlers)))
757 (write-region start end filename append visit lockname)))
759 (defalias 'mm-make-temp-file 'make-temp-file)
760 (define-obsolete-function-alias 'mm-make-temp-file 'make-temp-file "26.1")
762 (defvar mm-image-load-path-cache nil)
764 (defun mm-image-load-path (&optional package)
765 (if (and mm-image-load-path-cache
766 (equal load-path (car mm-image-load-path-cache)))
767 (cdr mm-image-load-path-cache)
768 (let (dir result)
769 (dolist (path load-path)
770 (when (and path
771 (file-directory-p
772 (setq dir (concat (file-name-directory
773 (directory-file-name path))
774 "etc/images/" (or package "gnus/")))))
775 (push dir result)))
776 (setq result (nreverse result)
777 mm-image-load-path-cache (cons load-path result))
778 result)))
780 ;; Fixme: This doesn't look useful where it's used.
781 (defun mm-detect-coding-region (start end)
782 "Like `detect-coding-region' except returning the best one."
783 (let ((coding-systems (detect-coding-region start end)))
784 (or (car-safe coding-systems)
785 coding-systems)))
787 (declare-function mm-detect-coding-region "mm-util" (start end))
789 (defun mm-detect-mime-charset-region (start end)
790 "Detect MIME charset of the text in the region between START and END."
791 (let ((cs (mm-detect-coding-region start end)))
792 (coding-system-get cs 'mime-charset)))
794 (defun mm-coding-system-to-mime-charset (coding-system)
795 "Return the MIME charset corresponding to CODING-SYSTEM."
796 (and coding-system
797 (coding-system-get coding-system 'mime-charset)))
799 (defvar jka-compr-acceptable-retval-list)
800 (declare-function jka-compr-make-temp-name "jka-compr" (&optional local))
802 (defun mm-decompress-buffer (filename &optional inplace force)
803 "Decompress buffer's contents, depending on jka-compr.
804 Only when FORCE is t or `auto-compression-mode' is enabled and FILENAME
805 agrees with `jka-compr-compression-info-list', decompression is done.
806 Signal an error if FORCE is neither nil nor t and compressed data are
807 not decompressed because `auto-compression-mode' is disabled.
808 If INPLACE is nil, return decompressed data or nil without modifying
809 the buffer. Otherwise, replace the buffer's contents with the
810 decompressed data. The buffer's multibyteness must be turned off."
811 (when (and filename
812 (if force
813 (prog1 t (require 'jka-compr))
814 (and (fboundp 'jka-compr-installed-p)
815 (jka-compr-installed-p))))
816 (let ((info (jka-compr-get-compression-info filename)))
817 (when info
818 (unless (or (memq force (list nil t))
819 (jka-compr-installed-p))
820 (error ""))
821 (let ((prog (jka-compr-info-uncompress-program info))
822 (args (jka-compr-info-uncompress-args info))
823 (msg (format "%s %s..."
824 (jka-compr-info-uncompress-message info)
825 filename))
826 (err-file (jka-compr-make-temp-name))
827 (cur (current-buffer))
828 (coding-system-for-read mm-binary-coding-system)
829 (coding-system-for-write mm-binary-coding-system)
830 retval err-msg)
831 (message "%s" msg)
832 (mm-with-unibyte-buffer
833 (insert-buffer-substring cur)
834 (condition-case err
835 (progn
836 (unless (memq (apply 'call-process-region
837 (point-min) (point-max)
838 prog t (list t err-file) nil args)
839 jka-compr-acceptable-retval-list)
840 (erase-buffer)
841 (insert (mapconcat 'identity
842 (split-string
843 (prog2
844 (insert-file-contents err-file)
845 (buffer-string)
846 (erase-buffer)) t)
847 " ")
848 "\n")
849 (setq err-msg
850 (format "Error while executing \"%s %s < %s\""
851 prog (mapconcat 'identity args " ")
852 filename)))
853 (setq retval (buffer-string)))
854 (error
855 (setq err-msg (error-message-string err)))))
856 (when (file-exists-p err-file)
857 (ignore-errors (delete-file err-file)))
858 (when inplace
859 (unless err-msg
860 (delete-region (point-min) (point-max))
861 (insert retval))
862 (setq retval nil))
863 (message "%s" (or err-msg (concat msg "done")))
864 retval)))))
866 (defun mm-find-buffer-file-coding-system (&optional filename)
867 "Find coding system used to decode the contents of the current buffer.
868 This function looks for the coding system magic cookie or examines the
869 coding system specified by `file-coding-system-alist' being associated
870 with FILENAME which defaults to `buffer-file-name'. Data compressed by
871 gzip, bzip2, etc. are allowed."
872 (unless filename
873 (setq filename buffer-file-name))
874 (save-excursion
875 (let ((decomp (unless ;; Not worth it to examine charset of tar files.
876 (and filename
877 (string-match
878 "\\.\\(?:tar\\.[^.]+\\|tbz\\|tgz\\)\\'"
879 filename))
880 (mm-decompress-buffer filename nil t))))
881 (when decomp
882 (set-buffer (generate-new-buffer " *temp*"))
883 (mm-disable-multibyte)
884 (insert decomp)
885 (setq filename (file-name-sans-extension filename)))
886 (goto-char (point-min))
887 (unwind-protect
888 (if filename
889 (or (funcall (symbol-value 'set-auto-coding-function)
890 filename (- (point-max) (point-min)))
891 (car (find-operation-coding-system 'insert-file-contents
892 filename)))
893 (let (auto-coding-alist)
894 (condition-case nil
895 (funcall (symbol-value 'set-auto-coding-function)
896 nil (- (point-max) (point-min)))
897 (error nil))))
898 (when decomp
899 (kill-buffer (current-buffer)))))))
901 (provide 'mm-util)
903 ;;; mm-util.el ends here