Trailing whitepace deleted.
[emacs.git] / lisp / gnus / mm-util.el
blobf80560e63c1a9ccdb55a5dff9a245727f96bc660
1 ;;; mm-util.el --- Utility functions for Mule and low level things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile
28 (require 'cl)
29 (defvar mm-mime-mule-charset-alist))
30 (require 'mail-prsvr)
32 (eval-and-compile
33 (mapcar
34 (lambda (elem)
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)
44 (char-int . identity)
45 (device-type . ignore)
46 (coding-system-equal . equal)
47 (annotationp . ignore)
48 (set-buffer-file-coding-system . ignore)
49 (make-char
50 . (lambda (charset int)
51 (int-to-char int)))
52 (read-charset
53 . (lambda (prompt)
54 "Return a charset."
55 (intern
56 (completing-read
57 prompt
58 (mapcar (lambda (e) (list (symbol-name (car e))))
59 mm-mime-mule-charset-alist)
60 nil t))))
61 (subst-char-in-string
62 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
63 "Replace characters in STRING from FROM to TO."
64 (let ((string (substring string 0)) ;Copy string.
65 (len (length string))
66 (idx 0))
67 ;; Replace all occurrences of FROM with TO.
68 (while (< idx len)
69 (when (= (aref string idx) from)
70 (aset string idx to))
71 (setq idx (1+ idx)))
72 string)))
73 (string-as-unibyte . identity)
74 (string-as-multibyte . identity)
75 (multibyte-string-p . ignore)
76 (point-at-bol . line-beginning-position)
77 (point-at-eol . line-end-position)
78 (insert-byte . insert-char)
79 (multibyte-char-to-unibyte . identity))))
81 (eval-and-compile
82 (defalias 'mm-char-or-char-int-p
83 (cond
84 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
85 ((fboundp 'char-valid-p) 'char-valid-p)
86 (t 'identity))))
88 (eval-and-compile
89 (defalias 'mm-read-coding-system
90 (cond
91 ((fboundp 'read-coding-system)
92 (if (and (featurep 'xemacs)
93 (<= (string-to-number emacs-version) 21.1))
94 (lambda (prompt &optional default-coding-system)
95 (read-coding-system prompt))
96 'read-coding-system))
97 (t (lambda (prompt &optional default-coding-system)
98 "Prompt the user for a coding system."
99 (completing-read
100 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
101 mm-mime-mule-charset-alist)))))))
103 (defvar mm-coding-system-list nil)
104 (defun mm-get-coding-system-list ()
105 "Get the coding system list."
106 (or mm-coding-system-list
107 (setq mm-coding-system-list (mm-coding-system-list))))
109 (defun mm-coding-system-p (sym)
110 "Return non-nil if SYM is a coding system."
111 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
112 (memq sym (mm-get-coding-system-list))))
114 (defvar mm-charset-synonym-alist
116 ;; Perfectly fine? A valid MIME name, anyhow.
117 ,@(unless (mm-coding-system-p 'big5)
118 '((big5 . cn-big5)))
119 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
120 ,@(unless (mm-coding-system-p 'x-ctext)
121 '((x-ctext . ctext)))
122 ;; Apparently not defined in Emacs 20, but is a valid MIME name.
123 ,@(unless (mm-coding-system-p 'gb2312)
124 '((gb2312 . cn-gb-2312)))
125 ;; ISO-8859-15 is very similar to ISO-8859-1.
126 ;; But this is just wrong. --fx
127 ,@(unless (mm-coding-system-p 'iso-8859-15) ; Emacs 21 defines it.
128 '((iso-8859-15 . iso-8859-1)))
129 ;; Windows-1252 is actually a superset of Latin-1. See also
130 ;; `gnus-article-dumbquotes-map'.
131 ,@(unless (mm-coding-system-p 'windows-1252)
132 (if (mm-coding-system-p 'cp1252)
133 '((windows-1252 . cp1252))
134 '((windows-1252 . iso-8859-1))))
135 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
136 ;; Outlook users in Czech republic. Use this to allow reading of their
137 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
139 ;; This is not TRT, the MIME name, windows-1250, should be an
140 ;; alias, and cp1250 should have a mime-charset property, per
141 ;; code-page.el. -- fx
142 ,@(if (and (not (mm-coding-system-p 'windows-1250))
143 (mm-coding-system-p 'cp1250))
144 '((windows-1250 . cp1250)))
146 "A mapping from invalid charset names to the real charset names.")
148 (defvar mm-binary-coding-system
149 (cond
150 ((mm-coding-system-p 'binary) 'binary)
151 ((mm-coding-system-p 'no-conversion) 'no-conversion)
152 (t nil))
153 "100% binary coding system.")
155 (defvar mm-text-coding-system
156 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
157 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
158 (and (mm-coding-system-p 'raw-text) 'raw-text))
159 mm-binary-coding-system)
160 "Text-safe coding system (For removing ^M).")
162 (defvar mm-text-coding-system-for-write nil
163 "Text coding system for write.")
165 (defvar mm-auto-save-coding-system
166 (cond
167 ((mm-coding-system-p 'utf-8-emacs)
168 (if (memq system-type '(windows-nt ms-dos ms-windows))
169 (if (mm-coding-system-p 'utf-8-emacs-dos)
170 'utf-8-emacs-dos mm-binary-coding-system)
171 'utf-8-emacs))
172 ((mm-coding-system-p 'emacs-mule)
173 (if (memq system-type '(windows-nt ms-dos ms-windows))
174 (if (mm-coding-system-p 'emacs-mule-dos)
175 'emacs-mule-dos mm-binary-coding-system)
176 'emacs-mule))
177 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
178 (t mm-binary-coding-system))
179 "Coding system of auto save file.")
181 (defvar mm-universal-coding-system mm-auto-save-coding-system
182 "The universal coding system.")
184 ;; Fixme: some of the cars here aren't valid MIME charsets. That
185 ;; should only matter with XEmacs, though.
186 (defvar mm-mime-mule-charset-alist
187 `((us-ascii ascii)
188 (iso-8859-1 latin-iso8859-1)
189 (iso-8859-2 latin-iso8859-2)
190 (iso-8859-3 latin-iso8859-3)
191 (iso-8859-4 latin-iso8859-4)
192 (iso-8859-5 cyrillic-iso8859-5)
193 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
194 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
195 ;; charset is koi8-r, not iso-8859-5.
196 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
197 (iso-8859-6 arabic-iso8859-6)
198 (iso-8859-7 greek-iso8859-7)
199 (iso-8859-8 hebrew-iso8859-8)
200 (iso-8859-9 latin-iso8859-9)
201 (iso-8859-14 latin-iso8859-14)
202 (iso-8859-15 latin-iso8859-15)
203 (viscii vietnamese-viscii-lower)
204 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
205 (euc-kr korean-ksc5601)
206 (gb2312 chinese-gb2312)
207 (big5 chinese-big5-1 chinese-big5-2)
208 (tibetan tibetan)
209 (thai-tis620 thai-tis620)
210 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
211 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
212 latin-jisx0201 japanese-jisx0208-1978
213 chinese-gb2312 japanese-jisx0208
214 korean-ksc5601 japanese-jisx0212
215 katakana-jisx0201)
216 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
217 latin-jisx0201 japanese-jisx0208-1978
218 chinese-gb2312 japanese-jisx0208
219 korean-ksc5601 japanese-jisx0212
220 chinese-cns11643-1 chinese-cns11643-2)
221 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
222 cyrillic-iso8859-5 greek-iso8859-7
223 latin-jisx0201 japanese-jisx0208-1978
224 chinese-gb2312 japanese-jisx0208
225 korean-ksc5601 japanese-jisx0212
226 chinese-cns11643-1 chinese-cns11643-2
227 chinese-cns11643-3 chinese-cns11643-4
228 chinese-cns11643-5 chinese-cns11643-6
229 chinese-cns11643-7)
230 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
231 (charsetp 'unicode-a)
232 (not (mm-coding-system-p 'mule-utf-8)))
233 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
234 ;; If we have utf-8 we're in Mule 5+.
235 (append '(utf-8)
236 (delete 'ascii
237 (coding-system-get 'mule-utf-8 'safe-charsets)))))
238 "Alist of MIME-charset/MULE-charsets.")
240 ;; Correct by construction, but should be unnecessary:
241 ;; XEmacs hates it.
242 (when (and (not (featurep 'xemacs))
243 (fboundp 'coding-system-list)
244 (fboundp 'sort-coding-systems))
245 (setq mm-mime-mule-charset-alist
246 (apply
247 'nconc
248 (mapcar
249 (lambda (cs)
250 (when (and (or (coding-system-get cs :mime-charset) ; Emacs 22
251 (coding-system-get cs 'mime-charset))
252 (not (eq t (coding-system-get cs 'safe-charsets))))
253 (list (cons (or (coding-system-get cs :mime-charset)
254 (coding-system-get cs 'mime-charset))
255 (delq 'ascii
256 (coding-system-get cs 'safe-charsets))))))
257 (sort-coding-systems (coding-system-list 'base-only))))))
259 (defvar mm-hack-charsets '(iso-8859-15 iso-2022-jp-2)
260 "A list of special charsets.
261 Valid elements include:
262 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
263 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
266 (defvar mm-iso-8859-15-compatible
267 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
268 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
269 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
271 (defvar mm-iso-8859-x-to-15-table
272 (and (fboundp 'coding-system-p)
273 (mm-coding-system-p 'iso-8859-15)
274 (mapcar
275 (lambda (cs)
276 (if (mm-coding-system-p (car cs))
277 (let ((c (string-to-char
278 (decode-coding-string "\341" (car cs)))))
279 (cons (char-charset c)
280 (cons
281 (- (string-to-char
282 (decode-coding-string "\341" 'iso-8859-15)) c)
283 (string-to-list (decode-coding-string (car (cdr cs))
284 (car cs))))))
285 '(gnus-charset 0)))
286 mm-iso-8859-15-compatible))
287 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
289 (defvar mm-coding-system-priorities nil
290 "Preferred coding systems for encoding outgoing mails.
292 More than one suitable coding systems may be found for some texts. By
293 default, a coding system with the highest priority is used to encode
294 outgoing mails (see `sort-coding-systems'). If this variable is set,
295 it overrides the default priority. For example, Japanese users may
296 prefer iso-2022-jp to japanese-shift-jis:
298 \(setq mm-coding-system-priorities
299 '(iso-2022-jp iso-2022-jp-2 japanese-shift-jis utf-8))
302 ;; Why on earth was this broken out? -- fx
303 (defvar mm-use-find-coding-systems-region
304 (fboundp 'find-coding-systems-region)
305 "Use `find-coding-systems-region' to find proper coding systems.")
307 ;;; Internal variables:
309 ;;; Functions:
311 (defun mm-mule-charset-to-mime-charset (charset)
312 "Return the MIME charset corresponding to the given Mule CHARSET."
313 (if (fboundp 'find-coding-systems-for-charsets)
314 (let (mime)
315 (dolist (cs (find-coding-systems-for-charsets (list charset)))
316 (unless mime
317 (when cs
318 (setq mime (or (coding-system-get cs :mime-charset)
319 (coding-system-get cs 'mime-charset))))))
320 mime)
321 (let ((alist mm-mime-mule-charset-alist)
322 out)
323 (while alist
324 (when (memq charset (cdar alist))
325 (setq out (caar alist)
326 alist nil))
327 (pop alist))
328 out)))
330 (defun mm-charset-to-coding-system (charset &optional lbt)
331 "Return coding-system corresponding to CHARSET.
332 CHARSET is a symbol naming a MIME charset.
333 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
334 used as the line break code type of the coding system."
335 (when (stringp charset)
336 (setq charset (intern (downcase charset))))
337 (when lbt
338 (setq charset (intern (format "%s-%s" charset lbt))))
339 (cond
340 ((null charset)
341 charset)
342 ;; Running in a non-MULE environment.
343 ((null (mm-get-coding-system-list))
344 charset)
345 ;; ascii
346 ((eq charset 'us-ascii)
347 'ascii)
348 ;; Check to see whether we can handle this charset. (This depends
349 ;; on there being some coding system matching each `mime-charset'
350 ;; property defined, as there should be.)
351 ((and (mm-coding-system-p charset)
352 ;;; Doing this would potentially weed out incorrect charsets.
353 ;;; charset
354 ;;; (eq charset (coding-system-get charset 'mime-charset))
356 charset)
357 ;; Translate invalid charsets.
358 ((let ((cs (cdr (assq charset mm-charset-synonym-alist))))
359 (and cs (mm-coding-system-p charset) cs)))
360 ;; Last resort: search the coding system list for entries which
361 ;; have the right mime-charset in case the canonical name isn't
362 ;; defined (though it should be).
363 ((let (cs)
364 ;; mm-get-coding-system-list returns a list of cs without lbt.
365 ;; Do we need -lbt?
366 (dolist (c (mm-get-coding-system-list))
367 (if (and (null cs)
368 (eq charset (or (coding-system-get c :mime-charset)
369 (coding-system-get c 'mime-charset))))
370 (setq cs c)))
371 cs))))
373 (defsubst mm-replace-chars-in-string (string from to)
374 (mm-subst-char-in-string from to string))
376 (eval-and-compile
377 (defvar mm-emacs-mule (and (not (featurep 'xemacs))
378 (boundp 'default-enable-multibyte-characters)
379 default-enable-multibyte-characters
380 (fboundp 'set-buffer-multibyte))
381 "True in Emacs with Mule.")
383 (if mm-emacs-mule
384 (defun mm-enable-multibyte ()
385 "Set the multibyte flag of the current buffer.
386 Only do this if the default value of `enable-multibyte-characters' is
387 non-nil. This is a no-op in XEmacs."
388 (set-buffer-multibyte t))
389 (defalias 'mm-enable-multibyte 'ignore))
391 (if mm-emacs-mule
392 (defun mm-disable-multibyte ()
393 "Unset the multibyte flag of in the current buffer.
394 This is a no-op in XEmacs."
395 (set-buffer-multibyte nil))
396 (defalias 'mm-disable-multibyte 'ignore)))
398 (defun mm-preferred-coding-system (charset)
399 ;; A typo in some Emacs versions.
400 (or (get-charset-property charset 'preferred-coding-system)
401 (get-charset-property charset 'prefered-coding-system)))
403 (defun mm-charset-after (&optional pos)
404 "Return charset of a character in current buffer at position POS.
405 If POS is nil, it defauls to the current point.
406 If POS is out of range, the value is nil.
407 If the charset is `composition', return the actual one."
408 (let ((char (char-after pos)) charset)
409 (if (< (mm-char-int char) 128)
410 (setq charset 'ascii)
411 ;; charset-after is fake in some Emacsen.
412 (setq charset (and (fboundp 'char-charset) (char-charset char)))
413 (if (eq charset 'composition) ; Mule 4
414 (let ((p (or pos (point))))
415 (cadr (find-charset-region p (1+ p))))
416 (if (and charset (not (memq charset '(ascii eight-bit-control
417 eight-bit-graphic))))
418 charset
420 mail-parse-mule-charset ;; cached mule-charset
421 (progn
422 (setq mail-parse-mule-charset
423 (and (boundp 'current-language-environment)
424 (car (last
425 (assq 'charset
426 (assoc current-language-environment
427 language-info-alist))))))
428 (if (or (not mail-parse-mule-charset)
429 (eq mail-parse-mule-charset 'ascii))
430 (setq mail-parse-mule-charset
431 (or (car (last (assq mail-parse-charset
432 mm-mime-mule-charset-alist)))
433 ;; Fixme: don't fix that!
434 'latin-iso8859-1)))
435 mail-parse-mule-charset)))))))
437 (defun mm-mime-charset (charset)
438 "Return the MIME charset corresponding to the given Mule CHARSET."
439 (if (eq charset 'unknown)
440 (error "The message contains non-printable characters, please use attachment"))
441 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
442 ;; This exists in Emacs 20.
444 (and (mm-preferred-coding-system charset)
445 (or (coding-system-get
446 (mm-preferred-coding-system charset) :mime-charset)
447 (coding-system-get
448 (mm-preferred-coding-system charset) 'mime-charset)))
449 (and (eq charset 'ascii)
450 'us-ascii)
451 (mm-preferred-coding-system charset)
452 (mm-mule-charset-to-mime-charset charset))
453 ;; This is for XEmacs.
454 (mm-mule-charset-to-mime-charset charset)))
456 (defun mm-delete-duplicates (list)
457 "Simple substitute for CL `delete-duplicates', testing with `equal'."
458 (let (result head)
459 (while list
460 (setq head (car list))
461 (setq list (delete head list))
462 (setq result (cons head result)))
463 (nreverse result)))
465 ;; It's not clear whether this is supposed to mean the global or local
466 ;; setting. I think it's used inconsistently. -- fx
467 (defsubst mm-multibyte-p ()
468 "Say whether multibyte is enabled."
469 (if (and (not (featurep 'xemacs))
470 (boundp 'enable-multibyte-characters))
471 enable-multibyte-characters
472 (featurep 'mule)))
474 (defun mm-iso-8859-x-to-15-region (&optional b e)
475 (if (fboundp 'char-charset)
476 (let (charset item c inconvertible)
477 (save-restriction
478 (if e (narrow-to-region b e))
479 (goto-char (point-min))
480 (skip-chars-forward "\0-\177")
481 (while (not (eobp))
482 (cond
483 ((not (setq item (assq (char-charset (setq c (char-after)))
484 mm-iso-8859-x-to-15-table)))
485 (forward-char))
486 ((memq c (cdr (cdr item)))
487 (setq inconvertible t)
488 (forward-char))
490 (insert (prog1 (+ c (car (cdr item))) (delete-char 1))))
491 (skip-chars-forward "\0-\177"))))
492 (not inconvertible))))
494 (defun mm-sort-coding-systems-predicate (a b)
495 (> (length (memq a mm-coding-system-priorities))
496 (length (memq b mm-coding-system-priorities))))
498 (defun mm-find-mime-charset-region (b e &optional hack-charsets)
499 "Return the MIME charsets needed to encode the region between B and E.
500 nil means ASCII, a single-element list represents an appropriate MIME
501 charset, and a longer list means no appropriate charset."
502 (let (charsets)
503 ;; The return possibilities of this function are a mess...
504 (or (and (mm-multibyte-p)
505 mm-use-find-coding-systems-region
506 ;; Find the mime-charset of the most preferred coding
507 ;; system that has one.
508 (let ((systems (find-coding-systems-region b e)))
509 (when mm-coding-system-priorities
510 (setq systems
511 (sort systems 'mm-sort-coding-systems-predicate)))
512 ;; Fixme: The `mime-charset' (`x-ctext') of `compound-text'
513 ;; is not in the IANA list.
514 (setq systems (delq 'compound-text systems))
515 (unless (equal systems '(undecided))
516 (while systems
517 (let* ((head (pop systems))
518 (cs (or (coding-system-get head :mime-charset)
519 (coding-system-get head 'mime-charset))))
520 (if cs
521 (setq systems nil
522 charsets (list cs))))))
523 charsets))
524 ;; Otherwise we're not multibyte, XEmacs or a single coding
525 ;; system won't cover it.
526 (setq charsets
527 (mm-delete-duplicates
528 (mapcar 'mm-mime-charset
529 (delq 'ascii
530 (mm-find-charset-region b e))))))
531 (if (and (memq 'iso-8859-15 charsets)
532 (memq 'iso-8859-15 hack-charsets)
533 (save-excursion (mm-iso-8859-x-to-15-region b e)))
534 (mapcar (lambda (x) (setq charsets (delq (car x) charsets)))
535 mm-iso-8859-15-compatible))
536 (if (and (memq 'iso-2022-jp-2 charsets)
537 (memq 'iso-2022-jp-2 hack-charsets))
538 (setq charsets (delq 'iso-2022-jp charsets)))
539 charsets))
541 (defmacro mm-with-unibyte-buffer (&rest forms)
542 "Create a temporary buffer, and evaluate FORMS there like `progn'.
543 Use unibyte mode for this."
544 `(let (default-enable-multibyte-characters)
545 (with-temp-buffer ,@forms)))
546 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
547 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
549 (defmacro mm-with-unibyte-current-buffer (&rest forms)
550 "Evaluate FORMS with current buffer temporarily made unibyte.
551 Also bind `default-enable-multibyte-characters' to nil.
552 Equivalent to `progn' in XEmacs"
553 (let ((multibyte (make-symbol "multibyte"))
554 (buffer (make-symbol "buffer")))
555 `(if mm-emacs-mule
556 (let ((,multibyte enable-multibyte-characters)
557 (,buffer (current-buffer)))
558 (unwind-protect
559 (let (default-enable-multibyte-characters)
560 (set-buffer-multibyte nil)
561 ,@forms)
562 (set-buffer ,buffer)
563 (set-buffer-multibyte ,multibyte)))
564 (let (default-enable-multibyte-characters)
565 ,@forms))))
566 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
567 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
569 (defmacro mm-with-unibyte (&rest forms)
570 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
571 `(let (default-enable-multibyte-characters)
572 ,@forms))
573 (put 'mm-with-unibyte 'lisp-indent-function 0)
574 (put 'mm-with-unibyte 'edebug-form-spec '(body))
576 (defun mm-find-charset-region (b e)
577 "Return a list of Emacs charsets in the region B to E."
578 (cond
579 ((and (mm-multibyte-p)
580 (fboundp 'find-charset-region))
581 ;; Remove composition since the base charsets have been included.
582 ;; Remove eight-bit-*, treat them as ascii.
583 (let ((css (find-charset-region b e)))
584 (mapcar (lambda (cs) (setq css (delq cs css)))
585 '(composition eight-bit-control eight-bit-graphic
586 control-1))
587 css))
589 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
590 (save-excursion
591 (save-restriction
592 (narrow-to-region b e)
593 (goto-char (point-min))
594 (skip-chars-forward "\0-\177")
595 (if (eobp)
596 '(ascii)
597 (let (charset)
598 (setq charset
599 (and (boundp 'current-language-environment)
600 (car (last (assq 'charset
601 (assoc current-language-environment
602 language-info-alist))))))
603 (if (eq charset 'ascii) (setq charset nil))
604 (or charset
605 (setq charset
606 (car (last (assq mail-parse-charset
607 mm-mime-mule-charset-alist)))))
608 (list 'ascii (or charset 'latin-iso8859-1)))))))))
610 (if (fboundp 'shell-quote-argument)
611 (defalias 'mm-quote-arg 'shell-quote-argument)
612 (defun mm-quote-arg (arg)
613 "Return a version of ARG that is safe to evaluate in a shell."
614 (let ((pos 0) new-pos accum)
615 ;; *** bug: we don't handle newline characters properly
616 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
617 (push (substring arg pos new-pos) accum)
618 (push "\\" accum)
619 (push (list (aref arg new-pos)) accum)
620 (setq pos (1+ new-pos)))
621 (if (= pos 0)
623 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
625 (defun mm-auto-mode-alist ()
626 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
627 (let ((alist auto-mode-alist)
628 out)
629 (while alist
630 (when (listp (cdar alist))
631 (push (car alist) out))
632 (pop alist))
633 (nreverse out)))
635 (defvar mm-inhibit-file-name-handlers
636 '(jka-compr-handler image-file-handler)
637 "A list of handlers doing (un)compression (etc) thingies.")
639 (defun mm-insert-file-contents (filename &optional visit beg end replace
640 inhibit)
641 "Like `insert-file-contents', q.v., but only reads in the file.
642 A buffer may be modified in several ways after reading into the buffer due
643 to advanced Emacs features, such as file-name-handlers, format decoding,
644 find-file-hooks, etc.
645 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
646 This function ensures that none of these modifications will take place."
647 (let ((format-alist nil)
648 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
649 (default-major-mode 'fundamental-mode)
650 (enable-local-variables nil)
651 (after-insert-file-functions nil)
652 (enable-local-eval nil)
653 (find-file-hooks nil)
654 (inhibit-file-name-operation (if inhibit
655 'insert-file-contents
656 inhibit-file-name-operation))
657 (inhibit-file-name-handlers
658 (if inhibit
659 (append mm-inhibit-file-name-handlers
660 inhibit-file-name-handlers)
661 inhibit-file-name-handlers)))
662 (insert-file-contents filename visit beg end replace)))
664 (defun mm-append-to-file (start end filename &optional codesys inhibit)
665 "Append the contents of the region to the end of file FILENAME.
666 When called from a function, expects three arguments,
667 START, END and FILENAME. START and END are buffer positions
668 saying what text to write.
669 Optional fourth argument specifies the coding system to use when
670 encoding the file.
671 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
672 (let ((coding-system-for-write
673 (or codesys mm-text-coding-system-for-write
674 mm-text-coding-system))
675 (inhibit-file-name-operation (if inhibit
676 'append-to-file
677 inhibit-file-name-operation))
678 (inhibit-file-name-handlers
679 (if inhibit
680 (append mm-inhibit-file-name-handlers
681 inhibit-file-name-handlers)
682 inhibit-file-name-handlers)))
683 (append-to-file start end filename)))
685 (defun mm-write-region (start end filename &optional append visit lockname
686 coding-system inhibit)
688 "Like `write-region'.
689 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
690 (let ((coding-system-for-write
691 (or coding-system mm-text-coding-system-for-write
692 mm-text-coding-system))
693 (inhibit-file-name-operation (if inhibit
694 'write-region
695 inhibit-file-name-operation))
696 (inhibit-file-name-handlers
697 (if inhibit
698 (append mm-inhibit-file-name-handlers
699 inhibit-file-name-handlers)
700 inhibit-file-name-handlers)))
701 (write-region start end filename append visit lockname)))
703 (defun mm-image-load-path (&optional package)
704 (let (dir result)
705 (dolist (path load-path (nreverse result))
706 (if (file-directory-p
707 (setq dir (concat (file-name-directory
708 (directory-file-name path))
709 "etc/" (or package "gnus/"))))
710 (push dir result))
711 (push path result))))
713 ;; It is not a MIME function, but some MIME functions use it.
714 (defalias 'mm-make-temp-file
715 (if (fboundp 'make-temp-file)
716 'make-temp-file
717 (lambda (prefix &optional dir-flag)
718 (let ((file (expand-file-name
719 (make-temp-name prefix)
720 (if (fboundp 'temp-directory)
721 (temp-directory)
722 temporary-file-directory))))
723 (if dir-flag
724 (make-directory file))
725 file))))
727 (provide 'mm-util)
729 ;;; mm-util.el ends here