1 ;;; mm-util.el --- Utility functions for Mule and low level things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;; 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 2, or (at your option)
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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
37 (let ((nfunc (intern (format "mm-%s" (car elem)))))
38 (if (fboundp (car elem))
39 (defalias nfunc (car elem))
40 (defalias nfunc (cdr elem)))))
41 '((decode-coding-string . (lambda (s a) s))
42 (encode-coding-string . (lambda (s a) s))
43 (encode-coding-region . ignore)
44 (coding-system-list . ignore)
45 (decode-coding-region . ignore)
47 (coding-system-equal . equal)
48 (annotationp . ignore)
49 (set-buffer-file-coding-system . ignore)
56 (mapcar (lambda (e) (list (symbol-name (car e))))
57 mm-mime-mule-charset-alist)
60 . (lambda (from to string &optional inplace) ;; stolen (and renamed) from nnheader.el
61 "Replace characters in STRING from FROM to TO.
62 Unless optional argument INPLACE is non-nil, return a new string."
63 (let ((string (if inplace string (copy-sequence string)))
66 ;; Replace all occurrences of FROM with TO.
68 (when (= (aref string idx) from)
73 . (lambda (string regexp rep &optional literal)
74 "See `replace-regexp-in-string', only the order of args differs."
75 (replace-regexp-in-string regexp rep string nil literal)))
76 (string-as-unibyte . identity)
77 (string-make-unibyte . identity)
78 (string-as-multibyte . identity)
79 (multibyte-string-p . ignore)
80 ;; It is not a MIME function, but some MIME functions use it.
81 (make-temp-file . (lambda (prefix &optional dir-flag)
82 (let ((file (expand-file-name
83 (make-temp-name prefix)
84 (if (fboundp 'temp-directory)
86 temporary-file-directory))))
88 (make-directory file))
90 (insert-byte . insert-char)
91 (multibyte-char-to-unibyte . identity))))
94 (defalias 'mm-char-or-char-int-p
96 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
97 ((fboundp 'char-valid-p) 'char-valid-p)
100 ;; Fixme: This seems always to be used to read a MIME charset, so it
101 ;; should be re-named and fixed (in Emacs) to offer completion only on
102 ;; proper charset names (base coding systems which have a
103 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
105 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
106 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
107 ;; Actually, there should be an `mm-coding-system-mime-charset'.
109 (defalias 'mm-read-coding-system
111 ((fboundp 'read-coding-system)
112 (if (and (featurep 'xemacs)
113 (<= (string-to-number emacs-version) 21.1))
114 (lambda (prompt &optional default-coding-system)
115 (read-coding-system prompt))
116 'read-coding-system))
117 (t (lambda (prompt &optional default-coding-system)
118 "Prompt the user for a coding system."
120 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
121 mm-mime-mule-charset-alist)))))))
123 (defvar mm-coding-system-list nil)
124 (defun mm-get-coding-system-list ()
125 "Get the coding system list."
126 (or mm-coding-system-list
127 (setq mm-coding-system-list (mm-coding-system-list))))
129 (defun mm-coding-system-p (cs)
130 "Return non-nil if CS is a symbol naming a coding system.
131 In XEmacs, also return non-nil if CS is a coding system object.
132 If CS is available, return CS itself in Emacs, and return a coding
133 system object in XEmacs."
134 (if (fboundp 'find-coding-system)
135 (find-coding-system cs)
136 (if (fboundp 'coding-system-p)
137 (when (coding-system-p cs)
139 ;; Is this branch ever actually useful?
140 (car (memq cs (mm-get-coding-system-list))))))
142 (defvar mm-charset-synonym-alist
144 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
145 ,@(unless (mm-coding-system-p 'x-ctext)
146 '((x-ctext . ctext)))
147 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
148 ,@(unless (mm-coding-system-p 'iso-8859-15)
149 '((iso-8859-15 . iso-8859-1)))
150 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
151 ,@(unless (mm-coding-system-p 'big5-hkscs)
152 '((big5-hkscs . big5)))
153 ;; Windows-1252 is actually a superset of Latin-1. See also
154 ;; `gnus-article-dumbquotes-map'.
155 ,@(unless (mm-coding-system-p 'windows-1252)
156 (if (mm-coding-system-p 'cp1252)
157 '((windows-1252 . cp1252))
158 '((windows-1252 . iso-8859-1))))
159 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
160 ;; Outlook users in Czech republic. Use this to allow reading of their
161 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
162 ,@(if (and (not (mm-coding-system-p 'windows-1250))
163 (mm-coding-system-p 'cp1250))
164 '((windows-1250 . cp1250)))
165 ;; A Microsoft misunderstanding.
166 ,@(unless (mm-coding-system-p 'ks_c_5601-1987)
167 (if (mm-coding-system-p 'cp949)
168 '((ks_c_5601-1987 . cp949))
169 '((ks_c_5601-1987 . euc-kr))))
171 "A mapping from invalid charset names to the real charset names.")
173 (defvar mm-binary-coding-system
175 ((mm-coding-system-p 'binary) 'binary)
176 ((mm-coding-system-p 'no-conversion) 'no-conversion)
178 "100% binary coding system.")
180 (defvar mm-text-coding-system
181 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
182 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
183 (and (mm-coding-system-p 'raw-text) 'raw-text))
184 mm-binary-coding-system)
185 "Text-safe coding system (For removing ^M).")
187 (defvar mm-text-coding-system-for-write nil
188 "Text coding system for write.")
190 (defvar mm-auto-save-coding-system
192 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
193 (if (memq system-type '(windows-nt ms-dos ms-windows))
194 (if (mm-coding-system-p 'utf-8-emacs-dos)
195 'utf-8-emacs-dos mm-binary-coding-system)
197 ((mm-coding-system-p 'emacs-mule)
198 (if (memq system-type '(windows-nt ms-dos ms-windows))
199 (if (mm-coding-system-p 'emacs-mule-dos)
200 'emacs-mule-dos mm-binary-coding-system)
202 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
203 (t mm-binary-coding-system))
204 "Coding system of auto save file.")
206 (defvar mm-universal-coding-system mm-auto-save-coding-system
207 "The universal coding system.")
209 ;; Fixme: some of the cars here aren't valid MIME charsets. That
210 ;; should only matter with XEmacs, though.
211 (defvar mm-mime-mule-charset-alist
213 (iso-8859-1 latin-iso8859-1)
214 (iso-8859-2 latin-iso8859-2)
215 (iso-8859-3 latin-iso8859-3)
216 (iso-8859-4 latin-iso8859-4)
217 (iso-8859-5 cyrillic-iso8859-5)
218 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
219 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
220 ;; charset is koi8-r, not iso-8859-5.
221 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
222 (iso-8859-6 arabic-iso8859-6)
223 (iso-8859-7 greek-iso8859-7)
224 (iso-8859-8 hebrew-iso8859-8)
225 (iso-8859-9 latin-iso8859-9)
226 (iso-8859-14 latin-iso8859-14)
227 (iso-8859-15 latin-iso8859-15)
228 (viscii vietnamese-viscii-lower)
229 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
230 (euc-kr korean-ksc5601)
231 (gb2312 chinese-gb2312)
232 (big5 chinese-big5-1 chinese-big5-2)
234 (thai-tis620 thai-tis620)
235 (windows-1251 cyrillic-iso8859-5)
236 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
237 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
238 latin-jisx0201 japanese-jisx0208-1978
239 chinese-gb2312 japanese-jisx0208
240 korean-ksc5601 japanese-jisx0212)
241 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
242 latin-jisx0201 japanese-jisx0208-1978
243 chinese-gb2312 japanese-jisx0208
244 korean-ksc5601 japanese-jisx0212
245 chinese-cns11643-1 chinese-cns11643-2)
246 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
247 cyrillic-iso8859-5 greek-iso8859-7
248 latin-jisx0201 japanese-jisx0208-1978
249 chinese-gb2312 japanese-jisx0208
250 korean-ksc5601 japanese-jisx0212
251 chinese-cns11643-1 chinese-cns11643-2
252 chinese-cns11643-3 chinese-cns11643-4
253 chinese-cns11643-5 chinese-cns11643-6
255 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
256 japanese-jisx0213-1 japanese-jisx0213-2)
257 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208)
258 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
259 (charsetp 'unicode-a)
260 (not (mm-coding-system-p 'mule-utf-8)))
261 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
262 ;; If we have utf-8 we're in Mule 5+.
265 (coding-system-get 'mule-utf-8 'safe-charsets)))))
266 "Alist of MIME-charset/MULE-charsets.")
268 (defun mm-enrich-utf-8-by-mule-ucs ()
269 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
270 This function will run when the `un-define' module is loaded under
271 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
272 with Mule charsets. It is completely useless for Emacs."
273 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
274 (assoc "un-define" after-load-alist)))
275 (setq after-load-alist
276 (delete '("un-define") after-load-alist)))
277 (when (boundp 'unicode-basic-translation-charset-order-list)
283 'unicode-basic-translation-charset-order-list))))
284 (elem (assq 'utf-8 mm-mime-mule-charset-alist)))
287 (setq mm-mime-mule-charset-alist
288 (nconc mm-mime-mule-charset-alist
289 (list (cons 'utf-8 val))))))
292 ;; Correct by construction, but should be unnecessary for Emacs:
293 (if (featurep 'xemacs)
294 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
295 (when (and (fboundp 'coding-system-list)
296 (fboundp 'sort-coding-systems))
297 (let ((css (sort-coding-systems (coding-system-list 'base-only)))
301 mime (or (coding-system-get cs :mime-charset) ; Emacs 22
302 (coding-system-get cs 'mime-charset)))
304 (not (eq t (setq mule
305 (coding-system-get cs 'safe-charsets))))
306 (not (assq mime alist)))
307 (push (cons mime (delq 'ascii mule)) alist)))
308 (setq mm-mime-mule-charset-alist (nreverse alist)))))
310 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
311 "A list of special charsets.
312 Valid elements include:
313 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
314 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
317 (defvar mm-iso-8859-15-compatible
318 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
319 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
320 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
322 (defvar mm-iso-8859-x-to-15-table
323 (and (fboundp 'coding-system-p)
324 (mm-coding-system-p 'iso-8859-15)
327 (if (mm-coding-system-p (car cs))
328 (let ((c (string-to-char
329 (decode-coding-string "\341" (car cs)))))
330 (cons (char-charset c)
333 (decode-coding-string "\341" 'iso-8859-15)) c)
334 (string-to-list (decode-coding-string (car (cdr cs))
337 mm-iso-8859-15-compatible))
338 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
340 (defcustom mm-coding-system-priorities
341 (if (boundp 'current-language-environment)
342 (let ((lang (symbol-value 'current-language-environment)))
343 (cond ((string= lang "Japanese")
344 ;; Japanese users may prefer iso-2022-jp to shift_jis.
345 '(iso-2022-jp iso-2022-jp-2 shift_jis iso-8859-1 utf-8)))))
346 "Preferred coding systems for encoding outgoing messages.
348 More than one suitable coding system may be found for some text.
349 By default, the coding system with the highest priority is used
350 to encode outgoing messages (see `sort-coding-systems'). If this
351 variable is set, it overrides the default priority."
352 :type '(repeat (symbol :tag "Coding system"))
356 (defvar mm-use-find-coding-systems-region
357 (fboundp 'find-coding-systems-region)
358 "Use `find-coding-systems-region' to find proper coding systems.
360 Setting it to nil is useful on Emacsen supporting Unicode if sending
361 mail with multiple parts is preferred to sending a Unicode one.")
363 ;;; Internal variables:
367 (defun mm-mule-charset-to-mime-charset (charset)
368 "Return the MIME charset corresponding to the given Mule CHARSET."
369 (if (and (fboundp 'find-coding-systems-for-charsets)
370 (fboundp 'sort-coding-systems))
371 (let ((css (sort (sort-coding-systems
372 (find-coding-systems-for-charsets (list charset)))
373 'mm-sort-coding-systems-predicate))
375 (while (and (not mime)
377 (when (setq cs (pop css))
378 (setq mime (or (coding-system-get cs :mime-charset)
379 (coding-system-get cs 'mime-charset)))))
381 (let ((alist (mapcar (lambda (cs)
382 (assq cs mm-mime-mule-charset-alist))
383 (sort (mapcar 'car mm-mime-mule-charset-alist)
384 'mm-sort-coding-systems-predicate)))
387 (when (memq charset (cdar alist))
388 (setq out (caar alist)
393 (defun mm-charset-to-coding-system (charset &optional lbt)
394 "Return coding-system corresponding to CHARSET.
395 CHARSET is a symbol naming a MIME charset.
396 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
397 used as the line break code type of the coding system."
398 (when (stringp charset)
399 (setq charset (intern (downcase charset))))
401 (setq charset (intern (format "%s-%s" charset lbt))))
405 ;; Running in a non-MULE environment.
406 ((or (null (mm-get-coding-system-list))
407 (not (fboundp 'coding-system-get)))
410 ((eq charset 'us-ascii)
412 ;; Check to see whether we can handle this charset. (This depends
413 ;; on there being some coding system matching each `mime-charset'
414 ;; property defined, as there should be.)
415 ((and (mm-coding-system-p charset)
416 ;;; Doing this would potentially weed out incorrect charsets.
418 ;;; (eq charset (coding-system-get charset 'mime-charset))
421 ;; Translate invalid charsets.
422 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
423 (and cs (mm-coding-system-p cs) cs)))
424 ;; Last resort: search the coding system list for entries which
425 ;; have the right mime-charset in case the canonical name isn't
426 ;; defined (though it should be).
428 ;; mm-get-coding-system-list returns a list of cs without lbt.
430 (dolist (c (mm-get-coding-system-list))
432 (eq charset (or (coding-system-get c :mime-charset)
433 (coding-system-get c 'mime-charset))))
438 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
439 (boundp 'default-enable-multibyte-characters)
440 default-enable-multibyte-characters
441 (fboundp 'set-buffer-multibyte))
442 "True in Emacs with Mule.")
445 (defun mm-enable-multibyte ()
446 "Set the multibyte flag of the current buffer.
447 Only do this if the default value of `enable-multibyte-characters' is
448 non-nil. This is a no-op in XEmacs."
449 (set-buffer-multibyte 'to))
450 (defalias 'mm-enable-multibyte 'ignore))
453 (defun mm-disable-multibyte ()
454 "Unset the multibyte flag of in the current buffer.
455 This is a no-op in XEmacs."
456 (set-buffer-multibyte nil))
457 (defalias 'mm-disable-multibyte 'ignore)))
459 (defun mm-preferred-coding-system (charset)
460 ;; A typo in some Emacs versions.
461 (or (get-charset-property charset 'preferred-coding-system)
462 (get-charset-property charset 'prefered-coding-system)))
464 ;; Mule charsets shouldn't be used.
465 (defsubst mm-guess-charset ()
466 "Guess Mule charset from the language environment."
468 mail-parse-mule-charset ;; cached mule-charset
470 (setq mail-parse-mule-charset
471 (and (boundp 'current-language-environment)
474 (assoc current-language-environment
475 language-info-alist))))))
476 (if (or (not mail-parse-mule-charset)
477 (eq mail-parse-mule-charset 'ascii))
478 (setq mail-parse-mule-charset
479 (or (car (last (assq mail-parse-charset
480 mm-mime-mule-charset-alist)))
483 mail-parse-mule-charset)))
485 (defun mm-charset-after (&optional pos)
486 "Return charset of a character in current buffer at position POS.
487 If POS is nil, it defauls to the current point.
488 If POS is out of range, the value is nil.
489 If the charset is `composition', return the actual one."
490 (let ((char (char-after pos)) charset)
491 (if (< (mm-char-int char) 128)
492 (setq charset 'ascii)
493 ;; charset-after is fake in some Emacsen.
494 (setq charset (and (fboundp 'char-charset) (char-charset char)))
495 (if (eq charset 'composition) ; Mule 4
496 (let ((p (or pos (point))))
497 (cadr (find-charset-region p (1+ p))))
498 (if (and charset (not (memq charset '(ascii eight-bit-control
499 eight-bit-graphic))))
501 (mm-guess-charset))))))
503 (defun mm-mime-charset (charset)
504 "Return the MIME charset corresponding to the given Mule CHARSET."
505 (if (eq charset 'unknown)
506 (error "The message contains non-printable characters, please use attachment"))
507 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
508 ;; This exists in Emacs 20.
510 (and (mm-preferred-coding-system charset)
511 (or (coding-system-get
512 (mm-preferred-coding-system charset) :mime-charset)
514 (mm-preferred-coding-system charset) 'mime-charset)))
515 (and (eq charset 'ascii)
517 (mm-preferred-coding-system charset)
518 (mm-mule-charset-to-mime-charset charset))
519 ;; This is for XEmacs.
520 (mm-mule-charset-to-mime-charset charset)))
522 (defun mm-delete-duplicates (list)
523 "Simple substitute for CL `delete-duplicates', testing with `equal'."
526 (setq head (car list))
527 (setq list (delete head list))
528 (setq result (cons head result)))
531 ;; Fixme: This is used in places when it should be testing the
532 ;; default multibyteness. See mm-default-multibyte-p.
534 (if (and (not (featurep 'xemacs))
535 (boundp 'enable-multibyte-characters))
536 (defun mm-multibyte-p ()
537 "Non-nil if multibyte is enabled in the current buffer."
538 enable-multibyte-characters)
539 (defun mm-multibyte-p () (featurep 'mule))))
541 (defun mm-default-multibyte-p ()
542 "Return non-nil if the session is multibyte.
543 This affects whether coding conversion should be attempted generally."
545 (if (boundp 'default-enable-multibyte-characters)
546 default-enable-multibyte-characters
549 (defun mm-iso-8859-x-to-15-region (&optional b e)
550 (if (fboundp 'char-charset)
551 (let (charset item c inconvertible)
553 (if e (narrow-to-region b e))
554 (goto-char (point-min))
555 (skip-chars-forward "\0-\177")
558 ((not (setq item (assq (char-charset (setq c (char-after)))
559 mm-iso-8859-x-to-15-table)))
561 ((memq c (cdr (cdr item)))
562 (setq inconvertible t)
565 (insert-before-markers (prog1 (+ c (car (cdr item)))
567 (skip-chars-forward "\0-\177")))
568 (not inconvertible))))
570 (defun mm-sort-coding-systems-predicate (a b)
573 ;; Note: invalid entries are dropped silently
574 (and (setq cs (mm-coding-system-p cs))
575 (coding-system-base cs)))
576 mm-coding-system-priorities)))
577 (and (setq a (mm-coding-system-p a))
578 (if (setq b (mm-coding-system-p b))
579 (> (length (memq (coding-system-base a) priorities))
580 (length (memq (coding-system-base b) priorities)))
583 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
584 "Return the MIME charsets needed to encode the region between B and E.
585 nil means ASCII, a single-element list represents an appropriate MIME
586 charset, and a longer list means no appropriate charset."
588 ;; The return possibilities of this function are a mess...
589 (or (and (mm-multibyte-p)
590 mm-use-find-coding-systems-region
591 ;; Find the mime-charset of the most preferred coding
592 ;; system that has one.
593 (let ((systems (find-coding-systems-region b e)))
594 (when mm-coding-system-priorities
596 (sort systems 'mm-sort-coding-systems-predicate)))
597 (setq systems (delq 'compound-text systems))
598 (unless (equal systems '(undecided))
600 (let* ((head (pop systems))
601 (cs (or (coding-system-get head :mime-charset)
602 (coding-system-get head 'mime-charset))))
603 ;; The mime-charset (`x-ctext') of
604 ;; `compound-text' is not in the IANA list. We
605 ;; shouldn't normally use anything here with a
606 ;; mime-charset having an `x-' prefix.
607 ;; Fixme: Allow this to be overridden, since
608 ;; there is existing use of x-ctext.
609 ;; Also people apparently need the coding system
610 ;; `iso-2022-jp-3' (which Mule-UCS defines with
611 ;; mime-charset, though it's not valid).
613 (not (string-match "^[Xx]-" (symbol-name cs)))
614 ;; UTF-16 of any variety is invalid for
615 ;; text parts and, unfortunately, has
616 ;; mime-charset defined both in Mule-UCS
617 ;; and versions of Emacs. (The name
618 ;; might be `mule-utf-16...' or
620 (not (string-match "utf-16" (symbol-name cs))))
622 charsets (list cs))))))
624 ;; Otherwise we're not multibyte, we're XEmacs, or a single
625 ;; coding system won't cover it.
627 (mm-delete-duplicates
628 (mapcar 'mm-mime-charset
630 (mm-find-charset-region b e))))))
631 (if (and (> (length charsets) 1)
632 (memq 'iso-8859-15 charsets)
633 (memq 'iso-8859-15 hack-charsets)
634 (save-excursion (mm-iso-8859-x-to-15-region b e)))
635 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
636 mm-iso-8859-15-compatible))
637 (if (and (memq 'iso-2022-jp-2 charsets)
638 (memq 'iso-2022-jp-2 hack-charsets))
639 (setq charsets (delq 'iso-2022-jp charsets)))
642 (defmacro mm-with-unibyte-buffer (&rest forms)
643 "Create a temporary buffer, and evaluate FORMS there like `progn'.
644 Use unibyte mode for this."
645 `(let (default-enable-multibyte-characters)
646 (with-temp-buffer ,@forms)))
647 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
648 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
650 (defmacro mm-with-multibyte-buffer (&rest forms)
651 "Create a temporary buffer, and evaluate FORMS there like `progn'.
652 Use multibyte mode for this."
653 `(let ((default-enable-multibyte-characters t))
654 (with-temp-buffer ,@forms)))
655 (put 'mm-with-multibyte-buffer 'lisp-indent-function 0)
656 (put 'mm-with-multibyte-buffer 'edebug-form-spec '(body))
658 (defmacro mm-with-unibyte-current-buffer (&rest forms)
659 "Evaluate FORMS with current buffer temporarily made unibyte.
660 Also bind `default-enable-multibyte-characters' to nil.
661 Equivalent to `progn' in XEmacs"
662 (let ((multibyte (make-symbol "multibyte"))
663 (buffer (make-symbol "buffer")))
665 (let ((,multibyte enable-multibyte-characters)
666 (,buffer (current-buffer)))
668 (let (default-enable-multibyte-characters)
669 (set-buffer-multibyte nil)
672 (set-buffer-multibyte ,multibyte)))
673 (let (default-enable-multibyte-characters)
675 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
676 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
678 (defmacro mm-with-unibyte (&rest forms)
679 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
680 `(let (default-enable-multibyte-characters)
682 (put 'mm-with-unibyte 'lisp-indent-function 0)
683 (put 'mm-with-unibyte 'edebug-form-spec '(body))
685 (defmacro mm-with-multibyte (&rest forms)
686 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
687 `(let ((default-enable-multibyte-characters t))
689 (put 'mm-with-multibyte 'lisp-indent-function 0)
690 (put 'mm-with-multibyte 'edebug-form-spec '(body))
692 (defun mm-find-charset-region (b e)
693 "Return a list of Emacs charsets in the region B to E."
695 ((and (mm-multibyte-p)
696 (fboundp 'find-charset-region))
697 ;; Remove composition since the base charsets have been included.
698 ;; Remove eight-bit-*, treat them as ascii.
699 (let ((css (find-charset-region b e)))
700 (mapcar (lambda (cs) (setq css (delq cs css)))
701 '(composition eight-bit-control eight-bit-graphic
705 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
708 (narrow-to-region b e)
709 (goto-char (point-min))
710 (skip-chars-forward "\0-\177")
715 (and (boundp 'current-language-environment)
716 (car (last (assq 'charset
717 (assoc current-language-environment
718 language-info-alist))))))
719 (if (eq charset 'ascii) (setq charset nil))
722 (car (last (assq mail-parse-charset
723 mm-mime-mule-charset-alist)))))
724 (list 'ascii (or charset 'latin-iso8859-1)))))))))
726 (defun mm-auto-mode-alist ()
727 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
728 (let ((alist auto-mode-alist)
731 (when (listp (cdar alist))
732 (push (car alist) out))
736 (defvar mm-inhibit-file-name-handlers
737 '(jka-compr-handler image-file-handler)
738 "A list of handlers doing (un)compression (etc) thingies.")
740 (defun mm-insert-file-contents (filename &optional visit beg end replace
742 "Like `insert-file-contents', but only reads in the file.
743 A buffer may be modified in several ways after reading into the buffer due
744 to advanced Emacs features, such as file-name-handlers, format decoding,
745 `find-file-hooks', etc.
746 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
747 This function ensures that none of these modifications will take place."
748 (let ((format-alist nil)
749 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
750 (default-major-mode 'fundamental-mode)
751 (enable-local-variables nil)
752 (after-insert-file-functions nil)
753 (enable-local-eval nil)
754 (find-file-hooks nil)
755 (inhibit-file-name-operation (if inhibit
756 'insert-file-contents
757 inhibit-file-name-operation))
758 (inhibit-file-name-handlers
760 (append mm-inhibit-file-name-handlers
761 inhibit-file-name-handlers)
762 inhibit-file-name-handlers)))
763 (insert-file-contents filename visit beg end replace)))
765 (defun mm-append-to-file (start end filename &optional codesys inhibit)
766 "Append the contents of the region to the end of file FILENAME.
767 When called from a function, expects three arguments,
768 START, END and FILENAME. START and END are buffer positions
769 saying what text to write.
770 Optional fourth argument specifies the coding system to use when
772 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
773 (let ((coding-system-for-write
774 (or codesys mm-text-coding-system-for-write
775 mm-text-coding-system))
776 (inhibit-file-name-operation (if inhibit
778 inhibit-file-name-operation))
779 (inhibit-file-name-handlers
781 (append mm-inhibit-file-name-handlers
782 inhibit-file-name-handlers)
783 inhibit-file-name-handlers)))
784 (write-region start end filename t 'no-message)
785 (message "Appended to %s" filename)))
787 (defun mm-write-region (start end filename &optional append visit lockname
788 coding-system inhibit)
790 "Like `write-region'.
791 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
792 (let ((coding-system-for-write
793 (or coding-system mm-text-coding-system-for-write
794 mm-text-coding-system))
795 (inhibit-file-name-operation (if inhibit
797 inhibit-file-name-operation))
798 (inhibit-file-name-handlers
800 (append mm-inhibit-file-name-handlers
801 inhibit-file-name-handlers)
802 inhibit-file-name-handlers)))
803 (write-region start end filename append visit lockname)))
805 (defun mm-image-load-path (&optional package)
807 (dolist (path load-path (nreverse result))
810 (setq dir (concat (file-name-directory
811 (directory-file-name path))
812 "etc/" (or package "gnus/")))))
814 (push path result))))
816 ;; Fixme: This doesn't look useful where it's used.
817 (if (fboundp 'detect-coding-region)
818 (defun mm-detect-coding-region (start end)
819 "Like `detect-coding-region' except returning the best one."
820 (let ((coding-systems
821 (detect-coding-region (point) (point-max))))
822 (or (car-safe coding-systems)
824 (defun mm-detect-coding-region (start end)
825 (let ((point (point)))
827 (skip-chars-forward "\0-\177" end)
829 (if (eq (point) end) 'ascii (mm-guess-charset))
830 (goto-char point)))))
832 (if (fboundp 'coding-system-get)
833 (defun mm-detect-mime-charset-region (start end)
834 "Detect MIME charset of the text in the region between START and END."
835 (let ((cs (mm-detect-coding-region start end)))
836 (coding-system-get cs 'mime-charset)))
837 (defun mm-detect-mime-charset-region (start end)
838 "Detect MIME charset of the text in the region between START and END."
839 (let ((cs (mm-detect-coding-region start end)))
845 ;;; mm-util.el ends here