1 ;;; mm-util.el --- Utility functions for Mule and low level things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
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.
28 (eval-when-compile (require 'cl))
29 (eval-when-compile (require 'gnus-clfns))
30 (eval-when-compile (require 'static))
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)
51 . (lambda (charset int)
59 (mapcar (lambda (e) (list (symbol-name (car e))))
60 mm-mime-mule-charset-alist)
63 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
64 "Replace characters in STRING from FROM to TO."
65 (let ((string (substring string 0)) ;Copy 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 . identity)
77 (multibyte-string-p . ignore)
78 ;; It is not a MIME function, but some MIME functions use it.
79 (make-temp-file . (lambda (prefix &optional dir-flag)
80 (let ((file (expand-file-name
81 (make-temp-name prefix)
82 (if (fboundp 'temp-directory)
84 temporary-file-directory))))
86 (make-directory file))
88 (insert-byte . insert-char)
89 (multibyte-char-to-unibyte . identity))))
92 (defalias 'mm-char-or-char-int-p
94 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
95 ((fboundp 'char-valid-p) 'char-valid-p)
98 ;; Fixme: This seems always to be used to read a MIME charset, so it
99 ;; should be re-named and fixed (in Emacs) to offer completion only on
100 ;; proper charset names (base coding systems which have a
101 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
103 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
104 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
105 ;; Actually, there should be an `mm-coding-system-mime-charset'.
107 (defalias 'mm-read-coding-system
109 ((fboundp 'read-coding-system)
110 (if (and (featurep 'xemacs)
111 (<= (string-to-number emacs-version) 21.1))
112 (lambda (prompt &optional default-coding-system)
113 (read-coding-system prompt))
114 'read-coding-system))
115 (t (lambda (prompt &optional default-coding-system)
116 "Prompt the user for a coding system."
118 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
119 mm-mime-mule-charset-alist)))))))
121 (defvar mm-coding-system-list nil)
122 (defun mm-get-coding-system-list ()
123 "Get the coding system list."
124 (or mm-coding-system-list
125 (setq mm-coding-system-list (mm-coding-system-list))))
127 (defun mm-coding-system-p (cs)
128 "Return non-nil if CS is a symbol naming a coding system.
129 In XEmacs, also return non-nil if CS is a coding system object."
130 (if (fboundp 'find-coding-system)
131 (find-coding-system cs)
132 (if (fboundp 'coding-system-p)
134 ;; Is this branch ever actually useful?
135 (memq cs (mm-get-coding-system-list)))))
137 (defvar mm-charset-synonym-alist
139 ;; Perfectly fine? A valid MIME name, anyhow.
140 ,@(unless (mm-coding-system-p 'big5)
142 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
143 ,@(unless (mm-coding-system-p 'x-ctext)
144 '((x-ctext . ctext)))
145 ;; Apparently not defined in Emacs 20, but is a valid MIME name.
146 ,@(unless (mm-coding-system-p 'gb2312)
147 '((gb2312 . cn-gb-2312)))
148 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
149 ,@(unless (mm-coding-system-p 'iso-8859-15)
150 '((iso-8859-15 . iso-8859-1)))
151 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
152 ,@(unless (mm-coding-system-p 'big5-hkscs)
153 '((big5-hkscs . big5)))
154 ;; Windows-1252 is actually a superset of Latin-1. See also
155 ;; `gnus-article-dumbquotes-map'.
156 ,@(unless (mm-coding-system-p 'windows-1252)
157 (if (mm-coding-system-p 'cp1252)
158 '((windows-1252 . cp1252))
159 '((windows-1252 . iso-8859-1))))
160 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
161 ;; Outlook users in Czech republic. Use this to allow reading of their
162 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
163 ,@(if (and (not (mm-coding-system-p 'windows-1250))
164 (mm-coding-system-p 'cp1250))
165 '((windows-1250 . cp1250)))
167 "A mapping from invalid charset names to the real charset names.")
169 (defvar mm-binary-coding-system
171 ((mm-coding-system-p 'binary) 'binary)
172 ((mm-coding-system-p 'no-conversion) 'no-conversion)
174 "100% binary coding system.")
176 (defvar mm-text-coding-system
177 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
178 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
179 (and (mm-coding-system-p 'raw-text) 'raw-text))
180 mm-binary-coding-system)
181 "Text-safe coding system (For removing ^M).")
183 (defvar mm-text-coding-system-for-write nil
184 "Text coding system for write.")
186 (defvar mm-auto-save-coding-system
188 ((mm-coding-system-p 'utf-8-emacs) ; Mule 7
189 (if (memq system-type '(windows-nt ms-dos ms-windows))
190 (if (mm-coding-system-p 'utf-8-emacs-dos)
191 'utf-8-emacs-dos mm-binary-coding-system)
193 ((mm-coding-system-p 'emacs-mule)
194 (if (memq system-type '(windows-nt ms-dos ms-windows))
195 (if (mm-coding-system-p 'emacs-mule-dos)
196 'emacs-mule-dos mm-binary-coding-system)
198 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
199 (t mm-binary-coding-system))
200 "Coding system of auto save file.")
202 (defvar mm-universal-coding-system mm-auto-save-coding-system
203 "The universal coding system.")
205 ;; Fixme: some of the cars here aren't valid MIME charsets. That
206 ;; should only matter with XEmacs, though.
207 (defvar mm-mime-mule-charset-alist
209 (iso-8859-1 latin-iso8859-1)
210 (iso-8859-2 latin-iso8859-2)
211 (iso-8859-3 latin-iso8859-3)
212 (iso-8859-4 latin-iso8859-4)
213 (iso-8859-5 cyrillic-iso8859-5)
214 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
215 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
216 ;; charset is koi8-r, not iso-8859-5.
217 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
218 (iso-8859-6 arabic-iso8859-6)
219 (iso-8859-7 greek-iso8859-7)
220 (iso-8859-8 hebrew-iso8859-8)
221 (iso-8859-9 latin-iso8859-9)
222 (iso-8859-14 latin-iso8859-14)
223 (iso-8859-15 latin-iso8859-15)
224 (viscii vietnamese-viscii-lower)
225 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
226 (euc-kr korean-ksc5601)
227 (gb2312 chinese-gb2312)
228 (big5 chinese-big5-1 chinese-big5-2)
230 (thai-tis620 thai-tis620)
231 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
232 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
233 latin-jisx0201 japanese-jisx0208-1978
234 chinese-gb2312 japanese-jisx0208
235 korean-ksc5601 japanese-jisx0212
237 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
238 latin-jisx0201 japanese-jisx0208-1978
239 chinese-gb2312 japanese-jisx0208
240 korean-ksc5601 japanese-jisx0212
241 chinese-cns11643-1 chinese-cns11643-2)
242 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
243 cyrillic-iso8859-5 greek-iso8859-7
244 latin-jisx0201 japanese-jisx0208-1978
245 chinese-gb2312 japanese-jisx0208
246 korean-ksc5601 japanese-jisx0212
247 chinese-cns11643-1 chinese-cns11643-2
248 chinese-cns11643-3 chinese-cns11643-4
249 chinese-cns11643-5 chinese-cns11643-6
251 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
252 (charsetp 'unicode-a)
253 (not (mm-coding-system-p 'mule-utf-8)))
254 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
255 ;; If we have utf-8 we're in Mule 5+.
258 (coding-system-get 'mule-utf-8 'safe-charsets)))))
259 "Alist of MIME-charset/MULE-charsets.")
261 ;; Correct by construction, but should be unnecessary:
263 (when (and (not (featurep 'xemacs))
264 (fboundp 'coding-system-list)
265 (fboundp 'sort-coding-systems))
266 (setq mm-mime-mule-charset-alist
271 (when (and (or (coding-system-get cs :mime-charset) ; Emacs 22
272 (coding-system-get cs 'mime-charset))
273 (not (eq t (coding-system-get cs 'safe-charsets))))
274 (list (cons (or (coding-system-get cs :mime-charset)
275 (coding-system-get cs 'mime-charset))
277 (coding-system-get cs 'safe-charsets))))))
278 (sort-coding-systems (coding-system-list 'base-only))))))
280 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
281 "A list of special charsets.
282 Valid elements include:
283 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
284 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
287 (defvar mm-iso-8859-15-compatible
288 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
289 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
290 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
292 (defvar mm-iso-8859-x-to-15-table
293 (and (fboundp 'coding-system-p)
294 (mm-coding-system-p 'iso-8859-15)
297 (if (mm-coding-system-p (car cs))
298 (let ((c (string-to-char
299 (decode-coding-string "\341" (car cs)))))
300 (cons (char-charset c)
303 (decode-coding-string "\341" 'iso-8859-15)) c)
304 (string-to-list (decode-coding-string (car (cdr cs))
307 mm-iso-8859-15-compatible))
308 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
310 (defcustom mm-coding-system-priorities
311 (if (boundp 'current-language-environment)
312 (let ((lang (symbol-value 'current-language-environment)))
313 (cond ((string= lang "Japanese")
314 ;; Japanese users may prefer iso-2022-jp to shift-jis.
315 '(iso-2022-jp iso-2022-jp-2 japanese-shift-jis
316 iso-latin-1 utf-8)))))
317 "Preferred coding systems for encoding outgoing messages.
319 More than one suitable coding system may be found for some text.
320 By default, the coding system with the highest priority is used
321 to encode outgoing messages (see `sort-coding-systems'). If this
322 variable is set, it overrides the default priority."
323 :type '(repeat (symbol :tag "Coding system"))
327 (defvar mm-use-find-coding-systems-region
328 (fboundp 'find-coding-systems-region)
329 "Use `find-coding-systems-region' to find proper coding systems.
331 Setting it to nil is useful on Emacsen supporting Unicode if sending
332 mail with multiple parts is preferred to sending a Unicode one.")
334 ;;; Internal variables:
338 (defun mm-mule-charset-to-mime-charset (charset)
339 "Return the MIME charset corresponding to the given Mule CHARSET."
340 (if (and (fboundp 'find-coding-systems-for-charsets)
341 (fboundp 'sort-coding-systems))
343 (dolist (cs (sort-coding-systems
345 (find-coding-systems-for-charsets (list charset)))))
348 (setq mime (or (coding-system-get cs :mime-charset)
349 (coding-system-get cs 'mime-charset))))))
351 (let ((alist mm-mime-mule-charset-alist)
354 (when (memq charset (cdar alist))
355 (setq out (caar alist)
360 (defun mm-charset-to-coding-system (charset &optional lbt)
361 "Return coding-system corresponding to CHARSET.
362 CHARSET is a symbol naming a MIME charset.
363 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
364 used as the line break code type of the coding system."
365 (when (stringp charset)
366 (setq charset (intern (downcase charset))))
368 (setq charset (intern (format "%s-%s" charset lbt))))
372 ;; Running in a non-MULE environment.
373 ((or (null (mm-get-coding-system-list))
374 (not (fboundp 'coding-system-get)))
377 ((eq charset 'us-ascii)
379 ;; Check to see whether we can handle this charset. (This depends
380 ;; on there being some coding system matching each `mime-charset'
381 ;; property defined, as there should be.)
382 ((and (mm-coding-system-p charset)
383 ;;; Doing this would potentially weed out incorrect charsets.
385 ;;; (eq charset (coding-system-get charset 'mime-charset))
388 ;; Translate invalid charsets.
389 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
390 (and cs (mm-coding-system-p cs) cs)))
391 ;; Last resort: search the coding system list for entries which
392 ;; have the right mime-charset in case the canonical name isn't
393 ;; defined (though it should be).
395 ;; mm-get-coding-system-list returns a list of cs without lbt.
397 (dolist (c (mm-get-coding-system-list))
399 (eq charset (or (coding-system-get c :mime-charset)
400 (coding-system-get c 'mime-charset))))
404 (defsubst mm-replace-chars-in-string (string from to)
405 (mm-subst-char-in-string from to string))
408 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
409 (boundp 'default-enable-multibyte-characters)
410 default-enable-multibyte-characters
411 (fboundp 'set-buffer-multibyte))
412 "True in Emacs with Mule.")
415 (defun mm-enable-multibyte ()
416 "Set the multibyte flag of the current buffer.
417 Only do this if the default value of `enable-multibyte-characters' is
418 non-nil. This is a no-op in XEmacs."
419 (set-buffer-multibyte 'to))
420 (defalias 'mm-enable-multibyte 'ignore))
423 (defun mm-disable-multibyte ()
424 "Unset the multibyte flag of in the current buffer.
425 This is a no-op in XEmacs."
426 (set-buffer-multibyte nil))
427 (defalias 'mm-disable-multibyte 'ignore)))
429 (defun mm-preferred-coding-system (charset)
430 ;; A typo in some Emacs versions.
431 (or (get-charset-property charset 'preferred-coding-system)
432 (get-charset-property charset 'prefered-coding-system)))
434 ;; Mule charsets shouldn't be used.
435 (defsubst mm-guess-charset ()
436 "Guess Mule charset from the language environment."
438 mail-parse-mule-charset ;; cached mule-charset
440 (setq mail-parse-mule-charset
441 (and (boundp 'current-language-environment)
444 (assoc current-language-environment
445 language-info-alist))))))
446 (if (or (not mail-parse-mule-charset)
447 (eq mail-parse-mule-charset 'ascii))
448 (setq mail-parse-mule-charset
449 (or (car (last (assq mail-parse-charset
450 mm-mime-mule-charset-alist)))
453 mail-parse-mule-charset)))
455 (defun mm-charset-after (&optional pos)
456 "Return charset of a character in current buffer at position POS.
457 If POS is nil, it defauls to the current point.
458 If POS is out of range, the value is nil.
459 If the charset is `composition', return the actual one."
460 (let ((char (char-after pos)) charset)
461 (if (< (mm-char-int char) 128)
462 (setq charset 'ascii)
463 ;; charset-after is fake in some Emacsen.
464 (setq charset (and (fboundp 'char-charset) (char-charset char)))
465 (if (eq charset 'composition) ; Mule 4
466 (let ((p (or pos (point))))
467 (cadr (find-charset-region p (1+ p))))
468 (if (and charset (not (memq charset '(ascii eight-bit-control
469 eight-bit-graphic))))
471 (mm-guess-charset))))))
473 (defun mm-mime-charset (charset)
474 "Return the MIME charset corresponding to the given Mule CHARSET."
475 (if (eq charset 'unknown)
476 (error "The message contains non-printable characters, please use attachment"))
477 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
478 ;; This exists in Emacs 20.
480 (and (mm-preferred-coding-system charset)
481 (or (coding-system-get
482 (mm-preferred-coding-system charset) :mime-charset)
484 (mm-preferred-coding-system charset) 'mime-charset)))
485 (and (eq charset 'ascii)
487 (mm-preferred-coding-system charset)
488 (mm-mule-charset-to-mime-charset charset))
489 ;; This is for XEmacs.
490 (mm-mule-charset-to-mime-charset charset)))
492 (defun mm-delete-duplicates (list)
493 "Simple substitute for CL `delete-duplicates', testing with `equal'."
496 (setq head (car list))
497 (setq list (delete head list))
498 (setq result (cons head result)))
501 ;; Fixme: This is used in places when it should be testing the
502 ;; default multibyteness. See mm-default-multibyte-p.
504 (if (and (not (featurep 'xemacs))
505 (boundp 'enable-multibyte-characters))
506 (defun mm-multibyte-p ()
507 "Non-nil if multibyte is enabled in the current buffer."
508 enable-multibyte-characters)
509 (defun mm-multibyte-p () (featurep 'mule))))
511 (defun mm-default-multibyte-p ()
512 "Return non-nil if the session is multibyte.
513 This affects whether coding conversion should be attempted generally."
515 (if (boundp 'default-enable-multibyte-characters)
516 default-enable-multibyte-characters
519 (defun mm-iso-8859-x-to-15-region (&optional b e)
520 (if (fboundp 'char-charset)
521 (let (charset item c inconvertible)
523 (if e (narrow-to-region b e))
524 (goto-char (point-min))
525 (skip-chars-forward "\0-\177")
528 ((not (setq item (assq (char-charset (setq c (char-after)))
529 mm-iso-8859-x-to-15-table)))
531 ((memq c (cdr (cdr item)))
532 (setq inconvertible t)
535 (insert-before-markers (prog1 (+ c (car (cdr item)))
537 (skip-chars-forward "\0-\177")))
538 (not inconvertible))))
540 (defun mm-sort-coding-systems-predicate (a b)
543 ;; Note: invalid entries are dropped silently
544 (and (coding-system-p cs)
545 (coding-system-base cs)))
546 mm-coding-system-priorities)))
547 (> (length (memq a priorities))
548 (length (memq b priorities)))))
550 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
551 "Return the MIME charsets needed to encode the region between B and E.
552 nil means ASCII, a single-element list represents an appropriate MIME
553 charset, and a longer list means no appropriate charset."
555 ;; The return possibilities of this function are a mess...
556 (or (and (mm-multibyte-p)
557 mm-use-find-coding-systems-region
558 ;; Find the mime-charset of the most preferred coding
559 ;; system that has one.
560 (let ((systems (find-coding-systems-region b e)))
561 (when mm-coding-system-priorities
563 (sort systems 'mm-sort-coding-systems-predicate)))
564 (setq systems (delq 'compound-text systems))
565 (unless (equal systems '(undecided))
567 (let* ((head (pop systems))
568 (cs (or (coding-system-get head :mime-charset)
569 (coding-system-get head 'mime-charset))))
570 ;; The mime-charset (`x-ctext') of
571 ;; `compound-text' is not in the IANA list. We
572 ;; shouldn't normally use anything here with a
573 ;; mime-charset having an `x-' prefix.
574 ;; Fixme: Allow this to be overridden, since
575 ;; there is existing use of x-ctext.
576 ;; Also people apparently need the coding system
577 ;; `iso-2022-jp-3' (which Mule-UCS defines with
578 ;; mime-charset, though it's not valid).
580 (not (string-match "^[Xx]-" (symbol-name cs)))
581 ;; UTF-16 of any variety is invalid for
582 ;; text parts and, unfortunately, has
583 ;; mime-charset defined both in Mule-UCS
584 ;; and versions of Emacs. (The name
585 ;; might be `mule-utf-16...' or
587 (not (string-match "utf-16" (symbol-name cs))))
589 charsets (list cs))))))
591 ;; Otherwise we're not multibyte, we're XEmacs, or a single
592 ;; coding system won't cover it.
594 (mm-delete-duplicates
595 (mapcar 'mm-mime-charset
597 (mm-find-charset-region b e))))))
598 (if (and (> (length charsets) 1)
599 (memq 'iso-8859-15 charsets)
600 (memq 'iso-8859-15 hack-charsets)
601 (save-excursion (mm-iso-8859-x-to-15-region b e)))
602 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
603 mm-iso-8859-15-compatible))
604 (if (and (memq 'iso-2022-jp-2 charsets)
605 (memq 'iso-2022-jp-2 hack-charsets))
606 (setq charsets (delq 'iso-2022-jp charsets)))
609 (defmacro mm-with-unibyte-buffer (&rest forms)
610 "Create a temporary buffer, and evaluate FORMS there like `progn'.
611 Use unibyte mode for this."
612 `(let (default-enable-multibyte-characters)
613 (with-temp-buffer ,@forms)))
614 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
615 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
617 (defmacro mm-with-unibyte-current-buffer (&rest forms)
618 "Evaluate FORMS with current buffer temporarily made unibyte.
619 Also bind `default-enable-multibyte-characters' to nil.
620 Equivalent to `progn' in XEmacs"
621 (let ((multibyte (make-symbol "multibyte"))
622 (buffer (make-symbol "buffer")))
624 (let ((,multibyte enable-multibyte-characters)
625 (,buffer (current-buffer)))
627 (let (default-enable-multibyte-characters)
628 (set-buffer-multibyte nil)
631 (set-buffer-multibyte ,multibyte)))
632 (let (default-enable-multibyte-characters)
634 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
635 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
637 (defmacro mm-with-unibyte (&rest forms)
638 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
639 `(let (default-enable-multibyte-characters)
641 (put 'mm-with-unibyte 'lisp-indent-function 0)
642 (put 'mm-with-unibyte 'edebug-form-spec '(body))
644 (defun mm-find-charset-region (b e)
645 "Return a list of Emacs charsets in the region B to E."
647 ((and (mm-multibyte-p)
648 (fboundp 'find-charset-region))
649 ;; Remove composition since the base charsets have been included.
650 ;; Remove eight-bit-*, treat them as ascii.
651 (let ((css (find-charset-region b e)))
652 (mapcar (lambda (cs) (setq css (delq cs css)))
653 '(composition eight-bit-control eight-bit-graphic
657 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
660 (narrow-to-region b e)
661 (goto-char (point-min))
662 (skip-chars-forward "\0-\177")
667 (and (boundp 'current-language-environment)
668 (car (last (assq 'charset
669 (assoc current-language-environment
670 language-info-alist))))))
671 (if (eq charset 'ascii) (setq charset nil))
674 (car (last (assq mail-parse-charset
675 mm-mime-mule-charset-alist)))))
676 (list 'ascii (or charset 'latin-iso8859-1)))))))))
678 (static-if (fboundp 'shell-quote-argument)
679 (defalias 'mm-quote-arg 'shell-quote-argument)
680 (defun mm-quote-arg (arg)
681 "Return a version of ARG that is safe to evaluate in a shell."
682 (let ((pos 0) new-pos accum)
683 ;; *** bug: we don't handle newline characters properly
684 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
685 (push (substring arg pos new-pos) accum)
687 (push (list (aref arg new-pos)) accum)
688 (setq pos (1+ new-pos)))
691 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
693 (defun mm-auto-mode-alist ()
694 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
695 (let ((alist auto-mode-alist)
698 (when (listp (cdar alist))
699 (push (car alist) out))
703 (defvar mm-inhibit-file-name-handlers
704 '(jka-compr-handler image-file-handler)
705 "A list of handlers doing (un)compression (etc) thingies.")
707 (defun mm-insert-file-contents (filename &optional visit beg end replace
709 "Like `insert-file-contents', but only reads in the file.
710 A buffer may be modified in several ways after reading into the buffer due
711 to advanced Emacs features, such as file-name-handlers, format decoding,
712 `find-file-hooks', etc.
713 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
714 This function ensures that none of these modifications will take place."
715 (let ((format-alist nil)
716 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
717 (default-major-mode 'fundamental-mode)
718 (enable-local-variables nil)
719 (after-insert-file-functions nil)
720 (enable-local-eval nil)
721 (find-file-hooks nil)
722 (inhibit-file-name-operation (if inhibit
723 'insert-file-contents
724 inhibit-file-name-operation))
725 (inhibit-file-name-handlers
727 (append mm-inhibit-file-name-handlers
728 inhibit-file-name-handlers)
729 inhibit-file-name-handlers)))
730 (insert-file-contents filename visit beg end replace)))
732 (defun mm-append-to-file (start end filename &optional codesys inhibit)
733 "Append the contents of the region to the end of file FILENAME.
734 When called from a function, expects three arguments,
735 START, END and FILENAME. START and END are buffer positions
736 saying what text to write.
737 Optional fourth argument specifies the coding system to use when
739 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
740 (let ((coding-system-for-write
741 (or codesys mm-text-coding-system-for-write
742 mm-text-coding-system))
743 (inhibit-file-name-operation (if inhibit
745 inhibit-file-name-operation))
746 (inhibit-file-name-handlers
748 (append mm-inhibit-file-name-handlers
749 inhibit-file-name-handlers)
750 inhibit-file-name-handlers)))
751 (write-region start end filename t 'no-message)
752 (message "Appended to %s" filename)))
754 (defun mm-write-region (start end filename &optional append visit lockname
755 coding-system inhibit)
757 "Like `write-region'.
758 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
759 (let ((coding-system-for-write
760 (or coding-system mm-text-coding-system-for-write
761 mm-text-coding-system))
762 (inhibit-file-name-operation (if inhibit
764 inhibit-file-name-operation))
765 (inhibit-file-name-handlers
767 (append mm-inhibit-file-name-handlers
768 inhibit-file-name-handlers)
769 inhibit-file-name-handlers)))
770 (write-region start end filename append visit lockname)))
772 (defun mm-image-load-path (&optional package)
774 (dolist (path load-path (nreverse result))
775 (if (file-directory-p
776 (setq dir (concat (file-name-directory
777 (directory-file-name path))
778 "etc/" (or package "gnus/"))))
780 (push path result))))
782 ;; Fixme: This doesn't look useful where it's used.
783 (if (fboundp 'detect-coding-region)
784 (defun mm-detect-coding-region (start end)
785 "Like `detect-coding-region' except returning the best one."
786 (let ((coding-systems
787 (detect-coding-region (point) (point-max))))
788 (or (car-safe coding-systems)
790 (defun mm-detect-coding-region (start end)
791 (let ((point (point)))
793 (skip-chars-forward "\0-\177" end)
795 (if (eq (point) end) 'ascii (mm-guess-charset))
796 (goto-char point)))))
798 (if (fboundp 'coding-system-get)
799 (defun mm-detect-mime-charset-region (start end)
800 "Detect MIME charset of the text in the region between START and END."
801 (let ((cs (mm-detect-coding-region start end)))
802 (coding-system-get cs 'mime-charset)))
803 (defun mm-detect-mime-charset-region (start end)
804 "Detect MIME charset of the text in the region between START and END."
805 (let ((cs (mm-detect-coding-region start end)))
811 ;;; mm-util.el ends here