1 ;;; mm-util.el --- Utility functions for Mule and low level things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 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)
97 "Return a multibyte string with the same individual chars as string."
99 (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
101 (multibyte-string-p . ignore)
102 ;; It is not a MIME function, but some MIME functions use it.
103 (make-temp-file . (lambda (prefix &optional dir-flag)
104 (let ((file (expand-file-name
105 (make-temp-name prefix)
106 (if (fboundp 'temp-directory)
108 temporary-file-directory))))
110 (make-directory file))
112 (insert-byte . insert-char)
113 (multibyte-char-to-unibyte . identity))))
117 ((fboundp 'replace-in-string)
118 (defalias 'mm-replace-in-string 'replace-in-string))
119 ((fboundp 'replace-regexp-in-string)
120 (defun mm-replace-in-string (string regexp newtext &optional literal)
121 "Replace all matches for REGEXP with NEWTEXT in STRING.
122 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
123 string containing the replacements.
125 This is a compatibility function for different Emacsen."
126 (replace-regexp-in-string regexp newtext string nil literal)))
128 (defun mm-replace-in-string (string regexp newtext &optional literal)
129 "Replace all matches for REGEXP with NEWTEXT in STRING.
130 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
131 string containing the replacements.
133 This is a compatibility function for different Emacsen."
134 (let ((start 0) tail)
135 (while (string-match regexp string start)
136 (setq tail (- (length string) (match-end 0)))
137 (setq string (replace-match newtext nil literal string))
138 (setq start (- (length string) tail))))
142 (defalias 'mm-char-or-char-int-p
144 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
145 ((fboundp 'char-valid-p) 'char-valid-p)
148 ;; Fixme: This seems always to be used to read a MIME charset, so it
149 ;; should be re-named and fixed (in Emacs) to offer completion only on
150 ;; proper charset names (base coding systems which have a
151 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
153 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
154 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
155 ;; Actually, there should be an `mm-coding-system-mime-charset'.
157 (defalias 'mm-read-coding-system
159 ((fboundp 'read-coding-system)
160 (if (and (featurep 'xemacs)
161 (<= (string-to-number emacs-version) 21.1))
162 (lambda (prompt &optional default-coding-system)
163 (read-coding-system prompt))
164 'read-coding-system))
165 (t (lambda (prompt &optional default-coding-system)
166 "Prompt the user for a coding system."
168 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
169 mm-mime-mule-charset-alist)))))))
171 (defvar mm-coding-system-list nil)
172 (defun mm-get-coding-system-list ()
173 "Get the coding system list."
174 (or mm-coding-system-list
175 (setq mm-coding-system-list (mm-coding-system-list))))
177 (defun mm-coding-system-p (cs)
178 "Return non-nil if CS is a symbol naming a coding system.
179 In XEmacs, also return non-nil if CS is a coding system object.
180 If CS is available, return CS itself in Emacs, and return a coding
181 system object in XEmacs."
182 (if (fboundp 'find-coding-system)
183 (and cs (find-coding-system cs))
184 (if (fboundp 'coding-system-p)
185 (when (coding-system-p cs)
187 ;; Is this branch ever actually useful?
188 (car (memq cs (mm-get-coding-system-list))))))
190 (defvar mm-charset-synonym-alist
192 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
193 ,@(unless (mm-coding-system-p 'x-ctext)
194 '((x-ctext . ctext)))
195 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
196 ,@(unless (mm-coding-system-p 'iso-8859-15)
197 '((iso-8859-15 . iso-8859-1)))
198 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
199 ,@(unless (mm-coding-system-p 'big5-hkscs)
200 '((big5-hkscs . big5)))
201 ;; Windows-1252 is actually a superset of Latin-1. See also
202 ;; `gnus-article-dumbquotes-map'.
203 ,@(unless (mm-coding-system-p 'windows-1252)
204 (if (mm-coding-system-p 'cp1252)
205 '((windows-1252 . cp1252))
206 '((windows-1252 . iso-8859-1))))
207 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
208 ;; Outlook users in Czech republic. Use this to allow reading of their
209 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
210 ,@(if (and (not (mm-coding-system-p 'windows-1250))
211 (mm-coding-system-p 'cp1250))
212 '((windows-1250 . cp1250)))
214 "A mapping from invalid charset names to the real charset names.")
216 (defvar mm-binary-coding-system
218 ((mm-coding-system-p 'binary) 'binary)
219 ((mm-coding-system-p 'no-conversion) 'no-conversion)
221 "100% binary coding system.")
223 (defvar mm-text-coding-system
224 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
225 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
226 (and (mm-coding-system-p 'raw-text) 'raw-text))
227 mm-binary-coding-system)
228 "Text-safe coding system (For removing ^M).")
230 (defvar mm-text-coding-system-for-write nil
231 "Text coding system for write.")
233 (defvar mm-auto-save-coding-system
235 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
236 (if (memq system-type '(windows-nt ms-dos ms-windows))
237 (if (mm-coding-system-p 'utf-8-emacs-dos)
238 'utf-8-emacs-dos mm-binary-coding-system)
240 ((mm-coding-system-p 'emacs-mule)
241 (if (memq system-type '(windows-nt ms-dos ms-windows))
242 (if (mm-coding-system-p 'emacs-mule-dos)
243 'emacs-mule-dos mm-binary-coding-system)
245 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
246 (t mm-binary-coding-system))
247 "Coding system of auto save file.")
249 (defvar mm-universal-coding-system mm-auto-save-coding-system
250 "The universal coding system.")
252 ;; Fixme: some of the cars here aren't valid MIME charsets. That
253 ;; should only matter with XEmacs, though.
254 (defvar mm-mime-mule-charset-alist
256 (iso-8859-1 latin-iso8859-1)
257 (iso-8859-2 latin-iso8859-2)
258 (iso-8859-3 latin-iso8859-3)
259 (iso-8859-4 latin-iso8859-4)
260 (iso-8859-5 cyrillic-iso8859-5)
261 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
262 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
263 ;; charset is koi8-r, not iso-8859-5.
264 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
265 (iso-8859-6 arabic-iso8859-6)
266 (iso-8859-7 greek-iso8859-7)
267 (iso-8859-8 hebrew-iso8859-8)
268 (iso-8859-9 latin-iso8859-9)
269 (iso-8859-14 latin-iso8859-14)
270 (iso-8859-15 latin-iso8859-15)
271 (viscii vietnamese-viscii-lower)
272 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
273 (euc-kr korean-ksc5601)
274 (gb2312 chinese-gb2312)
275 (big5 chinese-big5-1 chinese-big5-2)
277 (thai-tis620 thai-tis620)
278 (windows-1251 cyrillic-iso8859-5)
279 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
280 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
281 latin-jisx0201 japanese-jisx0208-1978
282 chinese-gb2312 japanese-jisx0208
283 korean-ksc5601 japanese-jisx0212)
284 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
285 latin-jisx0201 japanese-jisx0208-1978
286 chinese-gb2312 japanese-jisx0208
287 korean-ksc5601 japanese-jisx0212
288 chinese-cns11643-1 chinese-cns11643-2)
289 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
290 cyrillic-iso8859-5 greek-iso8859-7
291 latin-jisx0201 japanese-jisx0208-1978
292 chinese-gb2312 japanese-jisx0208
293 korean-ksc5601 japanese-jisx0212
294 chinese-cns11643-1 chinese-cns11643-2
295 chinese-cns11643-3 chinese-cns11643-4
296 chinese-cns11643-5 chinese-cns11643-6
298 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
299 japanese-jisx0213-1 japanese-jisx0213-2)
300 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
301 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
302 (charsetp 'unicode-a)
303 (not (mm-coding-system-p 'mule-utf-8)))
304 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
305 ;; If we have utf-8 we're in Mule 5+.
308 (coding-system-get 'mule-utf-8 'safe-charsets)))))
309 "Alist of MIME-charset/MULE-charsets.")
311 (defun mm-enrich-utf-8-by-mule-ucs ()
312 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
313 This function will run when the `un-define' module is loaded under
314 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
315 with Mule charsets. It is completely useless for Emacs."
316 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
317 (assoc "un-define" after-load-alist)))
318 (setq after-load-alist
319 (delete '("un-define") after-load-alist)))
320 (when (boundp 'unicode-basic-translation-charset-order-list)
326 'unicode-basic-translation-charset-order-list))))
327 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
330 (setq mm-mime-mule-charset-alist
331 (nconc mm-mime-mule-charset-alist
332 (list (cons 'utf-8 val))))))
335 ;; Correct by construction, but should be unnecessary for Emacs:
336 (if (featurep 'xemacs)
337 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
338 (when (and (fboundp 'coding-system-list)
339 (fboundp 'sort-coding-systems))
340 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
344 mime (or (coding-system-get cs :mime-charset) ; Emacs 23
345 (coding-system-get cs 'mime-charset)))
347 (not (eq t (setq mule
348 (coding-system-get cs 'safe-charsets))))
349 (not (assq mime alist)))
350 (push (cons mime (delq 'ascii mule)) alist)))
351 (setq mm-mime-mule-charset-alist (nreverse alist)))))
353 (defcustom mm-coding-system-priorities
354 (if (boundp 'current-language-environment)
355 (let ((lang (symbol-value 'current-language-environment)))
356 (cond ((string= lang "Japanese")
357 ;; Japanese users prefer iso-2022-jp to euc-japan or
358 ;; shift_jis, however iso-8859-1 should be used when
359 ;; there are only ASCII text and Latin-1 characters.
360 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8)))))
361 "Preferred coding systems for encoding outgoing messages.
363 More than one suitable coding system may be found for some text.
364 By default, the coding system with the highest priority is used
365 to encode outgoing messages (see `sort-coding-systems'). If this
366 variable is set, it overrides the default priority."
368 :type '(repeat (symbol :tag "Coding system"))
372 (defvar mm-use-find-coding-systems-region
373 (fboundp 'find-coding-systems-region)
374 "Use `find-coding-systems-region' to find proper coding systems.
376 Setting it to nil is useful on Emacsen supporting Unicode if sending
377 mail with multiple parts is preferred to sending a Unicode one.")
379 ;;; Internal variables:
383 (defun mm-mule-charset-to-mime-charset (charset)
384 "Return the MIME charset corresponding to the given Mule CHARSET."
385 (if (and (fboundp 'find-coding-systems-for-charsets)
386 (fboundp 'sort-coding-systems))
387 (let ((css (sort (sort-coding-systems
388 (find-coding-systems-for-charsets (list charset)))
389 'mm-sort-coding-systems-predicate))
391 (while (and (not mime)
393 (when (setq cs (pop css))
394 (setq mime (or (coding-system-get cs :mime-charset)
395 (coding-system-get cs 'mime-charset)))))
397 (let ((alist (mapcar (lambda (cs)
398 (assq cs mm-mime-mule-charset-alist))
399 (sort (mapcar 'car mm-mime-mule-charset-alist)
400 'mm-sort-coding-systems-predicate)))
403 (when (memq charset (cdar alist))
404 (setq out (caar alist)
409 (defun mm-charset-to-coding-system (charset &optional lbt)
410 "Return coding-system corresponding to CHARSET.
411 CHARSET is a symbol naming a MIME charset.
412 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
413 used as the line break code type of the coding system."
414 (when (stringp charset)
415 (setq charset (intern (downcase charset))))
417 (setq charset (intern (format "%s-%s" charset lbt))))
421 ;; Running in a non-MULE environment.
422 ((or (null (mm-get-coding-system-list))
423 (not (fboundp 'coding-system-get)))
426 ((eq charset 'us-ascii)
428 ;; Check to see whether we can handle this charset. (This depends
429 ;; on there being some coding system matching each `mime-charset'
430 ;; property defined, as there should be.)
431 ((and (mm-coding-system-p charset)
432 ;;; Doing this would potentially weed out incorrect charsets.
434 ;;; (eq charset (coding-system-get charset 'mime-charset))
437 ;; Translate invalid charsets.
438 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
439 (and cs (mm-coding-system-p cs) cs)))
440 ;; Last resort: search the coding system list for entries which
441 ;; have the right mime-charset in case the canonical name isn't
442 ;; defined (though it should be).
444 ;; mm-get-coding-system-list returns a list of cs without lbt.
446 (dolist (c (mm-get-coding-system-list))
448 (eq charset (or (coding-system-get c :mime-charset)
449 (coding-system-get c 'mime-charset))))
453 (defsubst mm-replace-chars-in-string (string from to)
454 (mm-subst-char-in-string from to string))
457 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
458 (boundp 'default-enable-multibyte-characters)
459 default-enable-multibyte-characters
460 (fboundp 'set-buffer-multibyte))
461 "True in Emacs with Mule.")
464 (defun mm-enable-multibyte ()
465 "Set the multibyte flag of the current buffer.
466 Only do this if the default value of `enable-multibyte-characters' is
467 non-nil. This is a no-op in XEmacs."
468 (set-buffer-multibyte 'to))
469 (defalias 'mm-enable-multibyte 'ignore))
472 (defun mm-disable-multibyte ()
473 "Unset the multibyte flag of in the current buffer.
474 This is a no-op in XEmacs."
475 (set-buffer-multibyte nil))
476 (defalias 'mm-disable-multibyte 'ignore)))
478 (defun mm-preferred-coding-system (charset)
479 ;; A typo in some Emacs versions.
480 (or (get-charset-property charset 'preferred-coding-system)
481 (get-charset-property charset 'prefered-coding-system)))
483 ;; Mule charsets shouldn't be used.
484 (defsubst mm-guess-charset ()
485 "Guess Mule charset from the language environment."
487 mail-parse-mule-charset ;; cached mule-charset
489 (setq mail-parse-mule-charset
490 (and (boundp 'current-language-environment)
493 (assoc current-language-environment
494 language-info-alist))))))
495 (if (or (not mail-parse-mule-charset)
496 (eq mail-parse-mule-charset 'ascii))
497 (setq mail-parse-mule-charset
498 (or (car (last (assq mail-parse-charset
499 mm-mime-mule-charset-alist)))
502 mail-parse-mule-charset)))
504 (defun mm-charset-after (&optional pos)
505 "Return charset of a character in current buffer at position POS.
506 If POS is nil, it defauls to the current point.
507 If POS is out of range, the value is nil.
508 If the charset is `composition', return the actual one."
509 (let ((char (char-after pos)) charset)
510 (if (< (mm-char-int char) 128)
511 (setq charset 'ascii)
512 ;; charset-after is fake in some Emacsen.
513 (setq charset (and (fboundp 'char-charset) (char-charset char)))
514 (if (eq charset 'composition) ; Mule 4
515 (let ((p (or pos (point))))
516 (cadr (find-charset-region p (1+ p))))
517 (if (and charset (not (memq charset '(ascii eight-bit-control
518 eight-bit-graphic))))
520 (mm-guess-charset))))))
522 (defun mm-mime-charset (charset)
523 "Return the MIME charset corresponding to the given Mule CHARSET."
524 (if (eq charset 'unknown)
525 (error "The message contains non-printable characters, please use attachment"))
526 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
527 ;; This exists in Emacs 20.
529 (and (mm-preferred-coding-system charset)
530 (or (coding-system-get
531 (mm-preferred-coding-system charset) :mime-charset)
533 (mm-preferred-coding-system charset) 'mime-charset)))
534 (and (eq charset 'ascii)
536 (mm-preferred-coding-system charset)
537 (mm-mule-charset-to-mime-charset charset))
538 ;; This is for XEmacs.
539 (mm-mule-charset-to-mime-charset charset)))
541 (defun mm-delete-duplicates (list)
542 "Simple substitute for CL `delete-duplicates', testing with `equal'."
545 (setq head (car list))
546 (setq list (delete head list))
547 (setq result (cons head result)))
550 ;; Fixme: This is used in places when it should be testing the
551 ;; default multibyteness. See mm-default-multibyte-p.
553 (if (and (not (featurep 'xemacs))
554 (boundp 'enable-multibyte-characters))
555 (defun mm-multibyte-p ()
556 "Non-nil if multibyte is enabled in the current buffer."
557 enable-multibyte-characters)
558 (defun mm-multibyte-p () (featurep 'mule))))
560 (defun mm-default-multibyte-p ()
561 "Return non-nil if the session is multibyte.
562 This affects whether coding conversion should be attempted generally."
564 (if (boundp 'default-enable-multibyte-characters)
565 default-enable-multibyte-characters
568 (defun mm-sort-coding-systems-predicate (a b)
571 ;; Note: invalid entries are dropped silently
572 (and (setq cs (mm-coding-system-p cs))
573 (coding-system-base cs)))
574 mm-coding-system-priorities)))
575 (and (setq a (mm-coding-system-p a))
576 (if (setq b (mm-coding-system-p b))
577 (> (length (memq (coding-system-base a) priorities))
578 (length (memq (coding-system-base b) priorities)))
582 (autoload 'latin-unity-massage-name "latin-unity")
583 (autoload 'latin-unity-maybe-remap "latin-unity")
584 (autoload 'latin-unity-representations-feasible-region "latin-unity")
585 (autoload 'latin-unity-representations-present-region "latin-unity")
586 (defvar latin-unity-coding-systems)
587 (defvar latin-unity-ucs-list))
589 (defun mm-xemacs-find-mime-charset-1 (begin end)
590 "Determine which MIME charset to use to send region as message.
591 This uses the XEmacs-specific latin-unity package to better handle the
592 case where identical characters from diverse ISO-8859-? character sets
593 can be encoded using a single one of the corresponding coding systems.
595 It treats `mm-coding-system-priorities' as the list of preferred
596 coding systems; a useful example setting for this list in Western
597 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
598 to the very standard Latin 1 coding system, and only move to coding
599 systems that are less supported as is necessary to encode the
600 characters that exist in the buffer.
602 Latin Unity doesn't know about those non-ASCII Roman characters that
603 are available in various East Asian character sets. As such, its
604 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
605 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
606 But this is very much a corner case, so don't worry about it."
607 (let ((systems mm-coding-system-priorities) csets psets curset)
609 ;; Load the Latin Unity library, if available.
610 (when (and (not (featurep 'latin-unity)) (locate-library "latin-unity"))
611 (ignore-errors (require 'latin-unity)))
613 ;; Now, can we use it?
614 (if (featurep 'latin-unity)
616 (setq csets (latin-unity-representations-feasible-region begin end)
617 psets (latin-unity-representations-present-region begin end))
621 ;; Pass back the first coding system in the preferred list
622 ;; that can encode the whole region.
623 (dolist (curset systems)
624 (setq curset (latin-unity-massage-name 'buffer-default curset))
626 ;; If the coding system is a universal coding system, then
627 ;; it can certainly encode all the characters in the region.
628 (if (memq curset latin-unity-ucs-list)
629 (throw 'done (list curset)))
631 ;; If a coding system isn't universal, and isn't in
632 ;; the list that latin unity knows about, we can't
633 ;; decide whether to use it here. Leave that until later
634 ;; in `mm-find-mime-charset-region' function, whence we
636 (unless (memq curset latin-unity-coding-systems)
639 ;; Right, we know about this coding system, and it may
640 ;; conceivably be able to encode all the characters in
642 (if (latin-unity-maybe-remap begin end curset csets psets t)
643 (throw 'done (list curset))))
645 ;; Can't encode using anything from the
646 ;; `mm-coding-system-priorities' list.
647 ;; Leave `mm-find-mime-charset' to do most of the work.
650 ;; Right, latin unity isn't available; let `mm-find-charset-region'
651 ;; take its default action, which equally applies to GNU Emacs.
654 (defmacro mm-xemacs-find-mime-charset (begin end)
655 (when (featurep 'xemacs)
656 `(and (featurep 'mule) (mm-xemacs-find-mime-charset-1 ,begin ,end))))
658 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
659 "Return the MIME charsets needed to encode the region between B and E.
660 nil means ASCII, a single-element list represents an appropriate MIME
661 charset, and a longer list means no appropriate charset."
663 ;; The return possibilities of this function are a mess...
664 (or (and (mm-multibyte-p)
665 mm-use-find-coding-systems-region
666 ;; Find the mime-charset of the most preferred coding
667 ;; system that has one.
668 (let ((systems (find-coding-systems-region b e)))
669 (when mm-coding-system-priorities
671 (sort systems 'mm-sort-coding-systems-predicate)))
672 (setq systems (delq 'compound-text systems))
673 (unless (equal systems '(undecided))
675 (let* ((head (pop systems))
676 (cs (or (coding-system-get head :mime-charset)
677 (coding-system-get head 'mime-charset))))
678 ;; The mime-charset (`x-ctext') of
679 ;; `compound-text' is not in the IANA list. We
680 ;; shouldn't normally use anything here with a
681 ;; mime-charset having an `x-' prefix.
682 ;; Fixme: Allow this to be overridden, since
683 ;; there is existing use of x-ctext.
684 ;; Also people apparently need the coding system
685 ;; `iso-2022-jp-3' (which Mule-UCS defines with
686 ;; mime-charset, though it's not valid).
688 (not (string-match "^[Xx]-" (symbol-name cs)))
689 ;; UTF-16 of any variety is invalid for
690 ;; text parts and, unfortunately, has
691 ;; mime-charset defined both in Mule-UCS
692 ;; and versions of Emacs. (The name
693 ;; might be `mule-utf-16...' or
695 (not (string-match "utf-16" (symbol-name cs))))
697 charsets (list cs))))))
699 ;; If we're XEmacs, and some coding system is appropriate,
700 ;; mm-xemacs-find-mime-charset will return an appropriate list.
701 ;; Otherwise, we'll get nil, and the next setq will get invoked.
702 (setq charsets (mm-xemacs-find-mime-charset b e))
704 ;; Fixme: won't work for unibyte Emacs 23:
706 ;; We're not multibyte, or a single coding system won't cover it.
708 (mm-delete-duplicates
709 (mapcar 'mm-mime-charset
711 (mm-find-charset-region b e))))))
714 (defmacro mm-with-unibyte-buffer (&rest forms)
715 "Create a temporary buffer, and evaluate FORMS there like `progn'.
716 Use unibyte mode for this."
717 `(let (default-enable-multibyte-characters)
718 (with-temp-buffer ,@forms)))
719 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
720 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
722 (defmacro mm-with-multibyte-buffer (&rest forms)
723 "Create a temporary buffer, and evaluate FORMS there like `progn'.
724 Use multibyte mode for this."
725 `(let ((default-enable-multibyte-characters t))
726 (with-temp-buffer ,@forms)))
727 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
728 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
730 (defmacro mm-with-unibyte-current-buffer (&rest forms)
731 "Evaluate FORMS with current buffer temporarily made unibyte.
732 Also bind `default-enable-multibyte-characters' to nil.
733 Equivalent to `progn' in XEmacs"
734 (let ((multibyte (make-symbol "multibyte"))
735 (buffer (make-symbol "buffer")))
737 (let ((,multibyte enable-multibyte-characters)
738 (,buffer (current-buffer)))
740 (let (default-enable-multibyte-characters)
741 (set-buffer-multibyte nil)
744 (set-buffer-multibyte ,multibyte)))
745 (let (default-enable-multibyte-characters)
747 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
748 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
750 (defmacro mm-with-unibyte (&rest forms)
751 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
752 `(let (default-enable-multibyte-characters)
754 (put 'mm-with-unibyte 'lisp-indent-function 0)
755 (put 'mm-with-unibyte 'edebug-form-spec '(body))
757 (defmacro mm-with-multibyte (&rest forms)
758 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
759 `(let ((default-enable-multibyte-characters t))
761 (put 'mm-with-multibyte 'lisp-indent-function 0)
762 (put 'mm-with-multibyte 'edebug-form-spec '(body))
764 (defun mm-find-charset-region (b e)
765 "Return a list of Emacs charsets in the region B to E."
767 ((and (mm-multibyte-p)
768 (fboundp 'find-charset-region))
769 ;; Remove composition since the base charsets have been included.
770 ;; Remove eight-bit-*, treat them as ascii.
771 (let ((css (find-charset-region b e)))
772 (mapcar (lambda (cs) (setq css (delq cs css)))
773 '(composition eight-bit-control eight-bit-graphic
777 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
780 (narrow-to-region b e)
781 (goto-char (point-min))
782 (skip-chars-forward "\0-\177")
787 (and (boundp 'current-language-environment)
788 (car (last (assq 'charset
789 (assoc current-language-environment
790 language-info-alist))))))
791 (if (eq charset 'ascii) (setq charset nil))
794 (car (last (assq mail-parse-charset
795 mm-mime-mule-charset-alist)))))
796 (list 'ascii (or charset 'latin-iso8859-1)))))))))
798 (if (fboundp 'shell-quote-argument)
799 (defalias 'mm-quote-arg 'shell-quote-argument)
800 (defun mm-quote-arg (arg)
801 "Return a version of ARG that is safe to evaluate in a shell."
802 (let ((pos 0) new-pos accum)
803 ;; *** bug: we don't handle newline characters properly
804 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
805 (push (substring arg pos new-pos) accum)
807 (push (list (aref arg new-pos)) accum)
808 (setq pos (1+ new-pos)))
811 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
813 (defun mm-auto-mode-alist ()
814 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
815 (let ((alist auto-mode-alist)
818 (when (listp (cdar alist))
819 (push (car alist) out))
823 (defvar mm-inhibit-file-name-handlers
824 '(jka-compr-handler image-file-handler)
825 "A list of handlers doing (un)compression (etc) thingies.")
827 (defun mm-insert-file-contents (filename &optional visit beg end replace
829 "Like `insert-file-contents', but only reads in the file.
830 A buffer may be modified in several ways after reading into the buffer due
831 to advanced Emacs features, such as file-name-handlers, format decoding,
832 `find-file-hooks', etc.
833 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
834 This function ensures that none of these modifications will take place."
835 (let* ((format-alist nil)
836 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
837 (default-major-mode 'fundamental-mode)
838 (enable-local-variables nil)
839 (after-insert-file-functions nil)
840 (enable-local-eval nil)
841 (inhibit-file-name-operation (if inhibit
842 'insert-file-contents
843 inhibit-file-name-operation))
844 (inhibit-file-name-handlers
846 (append mm-inhibit-file-name-handlers
847 inhibit-file-name-handlers)
848 inhibit-file-name-handlers))
849 (ffh (if (boundp 'find-file-hook)
852 (val (symbol-value ffh)))
855 (insert-file-contents filename visit beg end replace)
858 (defun mm-append-to-file (start end filename &optional codesys inhibit)
859 "Append the contents of the region to the end of file FILENAME.
860 When called from a function, expects three arguments,
861 START, END and FILENAME. START and END are buffer positions
862 saying what text to write.
863 Optional fourth argument specifies the coding system to use when
865 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
866 (let ((coding-system-for-write
867 (or codesys mm-text-coding-system-for-write
868 mm-text-coding-system))
869 (inhibit-file-name-operation (if inhibit
871 inhibit-file-name-operation))
872 (inhibit-file-name-handlers
874 (append mm-inhibit-file-name-handlers
875 inhibit-file-name-handlers)
876 inhibit-file-name-handlers)))
877 (write-region start end filename t 'no-message)
878 (message "Appended to %s" filename)))
880 (defun mm-write-region (start end filename &optional append visit lockname
881 coding-system inhibit)
883 "Like `write-region'.
884 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
885 (let ((coding-system-for-write
886 (or coding-system mm-text-coding-system-for-write
887 mm-text-coding-system))
888 (inhibit-file-name-operation (if inhibit
890 inhibit-file-name-operation))
891 (inhibit-file-name-handlers
893 (append mm-inhibit-file-name-handlers
894 inhibit-file-name-handlers)
895 inhibit-file-name-handlers)))
896 (write-region start end filename append visit lockname)))
898 (defun mm-image-load-path (&optional package)
900 (dolist (path load-path (nreverse result))
903 (setq dir (concat (file-name-directory
904 (directory-file-name path))
905 "etc/images/" (or package "gnus/")))))
907 (push path result))))
909 ;; Fixme: This doesn't look useful where it's used.
910 (if (fboundp 'detect-coding-region)
911 (defun mm-detect-coding-region (start end)
912 "Like `detect-coding-region' except returning the best one."
913 (let ((coding-systems
914 (detect-coding-region start end)))
915 (or (car-safe coding-systems)
917 (defun mm-detect-coding-region (start end)
918 (let ((point (point)))
920 (skip-chars-forward "\0-\177" end)
922 (if (eq (point) end) 'ascii (mm-guess-charset))
923 (goto-char point)))))
925 (if (fboundp 'coding-system-get)
926 (defun mm-detect-mime-charset-region (start end)
927 "Detect MIME charset of the text in the region between START and END."
928 (let ((cs (mm-detect-coding-region start end)))
929 (coding-system-get cs 'mime-charset)))
930 (defun mm-detect-mime-charset-region (start end)
931 "Detect MIME charset of the text in the region between START and END."
932 (let ((cs (mm-detect-coding-region start end)))
938 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
939 ;;; mm-util.el ends here