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.
28 (eval-when-compile (require 'cl
))
34 (let ((nfunc (intern (format "mm-%s" (car elem
)))))
35 (if (fboundp (car elem
))
36 (defalias nfunc
(car elem
))
37 (defalias nfunc
(cdr elem
)))))
38 '((decode-coding-string .
(lambda (s a
) s
))
39 (encode-coding-string .
(lambda (s a
) s
))
40 (encode-coding-region . ignore
)
41 (coding-system-list . ignore
)
42 (decode-coding-region . ignore
)
44 (coding-system-equal . equal
)
45 (annotationp . ignore
)
46 (set-buffer-file-coding-system . ignore
)
48 .
(lambda (charset int
)
56 (mapcar (lambda (e) (list (symbol-name (car e
))))
57 mm-mime-mule-charset-alist
)
60 .
(lambda (from to string
) ;; stolen (and renamed) from nnheader.el
61 "Replace characters in STRING from FROM to TO."
62 (let ((string (substring string
0)) ;Copy string.
65 ;; Replace all occurrences of FROM with TO.
67 (when (= (aref string idx
) from
)
71 (string-as-unibyte . identity
)
72 (string-make-unibyte . identity
)
73 (string-as-multibyte . identity
)
74 (multibyte-string-p . ignore
)
75 ;; It is not a MIME function, but some MIME functions use it.
76 (make-temp-file .
(lambda (prefix &optional dir-flag
)
77 (let ((file (expand-file-name
78 (make-temp-name prefix
)
79 (if (fboundp 'temp-directory
)
81 temporary-file-directory
))))
83 (make-directory file
))
85 (insert-byte . insert-char
)
86 (multibyte-char-to-unibyte . identity
))))
89 (defalias 'mm-char-or-char-int-p
91 ((fboundp 'char-or-char-int-p
) 'char-or-char-int-p
)
92 ((fboundp 'char-valid-p
) 'char-valid-p
)
95 ;; Fixme: This seems always to be used to read a MIME charset, so it
96 ;; should be re-named and fixed (in Emacs) to offer completion only on
97 ;; proper charset names (base coding systems which have a
98 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
100 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
101 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
102 ;; Actually, there should be an `mm-coding-system-mime-charset'.
104 (defalias 'mm-read-coding-system
106 ((fboundp 'read-coding-system
)
107 (if (and (featurep 'xemacs
)
108 (<= (string-to-number emacs-version
) 21.1))
109 (lambda (prompt &optional default-coding-system
)
110 (read-coding-system prompt
))
111 'read-coding-system
))
112 (t (lambda (prompt &optional default-coding-system
)
113 "Prompt the user for a coding system."
115 prompt
(mapcar (lambda (s) (list (symbol-name (car s
))))
116 mm-mime-mule-charset-alist
)))))))
118 (defvar mm-coding-system-list nil
)
119 (defun mm-get-coding-system-list ()
120 "Get the coding system list."
121 (or mm-coding-system-list
122 (setq mm-coding-system-list
(mm-coding-system-list))))
124 (defun mm-coding-system-p (cs)
125 "Return non-nil if CS is a symbol naming a coding system.
126 In XEmacs, also return non-nil if CS is a coding system object."
127 (if (fboundp 'find-coding-system
)
128 (find-coding-system cs
)
129 (if (fboundp 'coding-system-p
)
131 ;; Is this branch ever actually useful?
132 (memq cs
(mm-get-coding-system-list)))))
134 (defvar mm-charset-synonym-alist
136 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
137 ,@(unless (mm-coding-system-p 'x-ctext
)
138 '((x-ctext . ctext
)))
139 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
140 ,@(unless (mm-coding-system-p 'iso-8859-15
)
141 '((iso-8859-15 . iso-8859-1
)))
142 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
143 ,@(unless (mm-coding-system-p 'big5-hkscs
)
144 '((big5-hkscs . big5
)))
145 ;; Windows-1252 is actually a superset of Latin-1. See also
146 ;; `gnus-article-dumbquotes-map'.
147 ,@(unless (mm-coding-system-p 'windows-1252
)
148 (if (mm-coding-system-p 'cp1252
)
149 '((windows-1252 . cp1252
))
150 '((windows-1252 . iso-8859-1
))))
151 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
152 ;; Outlook users in Czech republic. Use this to allow reading of their
153 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
154 ,@(if (and (not (mm-coding-system-p 'windows-1250
))
155 (mm-coding-system-p 'cp1250
))
156 '((windows-1250 . cp1250
)))
158 "A mapping from invalid charset names to the real charset names.")
160 (defvar mm-binary-coding-system
162 ((mm-coding-system-p 'binary
) 'binary
)
163 ((mm-coding-system-p 'no-conversion
) 'no-conversion
)
165 "100% binary coding system.")
167 (defvar mm-text-coding-system
168 (or (if (memq system-type
'(windows-nt ms-dos ms-windows
))
169 (and (mm-coding-system-p 'raw-text-dos
) 'raw-text-dos
)
170 (and (mm-coding-system-p 'raw-text
) 'raw-text
))
171 mm-binary-coding-system
)
172 "Text-safe coding system (For removing ^M).")
174 (defvar mm-text-coding-system-for-write nil
175 "Text coding system for write.")
177 (defvar mm-auto-save-coding-system
179 ((mm-coding-system-p 'utf-8-emacs
) ; Mule 7
180 (if (memq system-type
'(windows-nt ms-dos ms-windows
))
181 (if (mm-coding-system-p 'utf-8-emacs-dos
)
182 'utf-8-emacs-dos mm-binary-coding-system
)
184 ((mm-coding-system-p 'emacs-mule
)
185 (if (memq system-type
'(windows-nt ms-dos ms-windows
))
186 (if (mm-coding-system-p 'emacs-mule-dos
)
187 'emacs-mule-dos mm-binary-coding-system
)
189 ((mm-coding-system-p 'escape-quoted
) 'escape-quoted
)
190 (t mm-binary-coding-system
))
191 "Coding system of auto save file.")
193 (defvar mm-universal-coding-system mm-auto-save-coding-system
194 "The universal coding system.")
196 ;; Fixme: some of the cars here aren't valid MIME charsets. That
197 ;; should only matter with XEmacs, though.
198 (defvar mm-mime-mule-charset-alist
200 (iso-8859-1 latin-iso8859-1
)
201 (iso-8859-2 latin-iso8859-2
)
202 (iso-8859-3 latin-iso8859-3
)
203 (iso-8859-4 latin-iso8859-4
)
204 (iso-8859-5 cyrillic-iso8859-5
)
205 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
206 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
207 ;; charset is koi8-r, not iso-8859-5.
208 (koi8-r cyrillic-iso8859-5 gnus-koi8-r
)
209 (iso-8859-6 arabic-iso8859-6
)
210 (iso-8859-7 greek-iso8859-7
)
211 (iso-8859-8 hebrew-iso8859-8
)
212 (iso-8859-9 latin-iso8859-9
)
213 (iso-8859-14 latin-iso8859-14
)
214 (iso-8859-15 latin-iso8859-15
)
215 (viscii vietnamese-viscii-lower
)
216 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978
)
217 (euc-kr korean-ksc5601
)
218 (gb2312 chinese-gb2312
)
219 (big5 chinese-big5-1 chinese-big5-2
)
221 (thai-tis620 thai-tis620
)
222 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column
)
223 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
224 latin-jisx0201 japanese-jisx0208-1978
225 chinese-gb2312 japanese-jisx0208
226 korean-ksc5601 japanese-jisx0212
228 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
229 latin-jisx0201 japanese-jisx0208-1978
230 chinese-gb2312 japanese-jisx0208
231 korean-ksc5601 japanese-jisx0212
232 chinese-cns11643-1 chinese-cns11643-2
)
233 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
234 cyrillic-iso8859-5 greek-iso8859-7
235 latin-jisx0201 japanese-jisx0208-1978
236 chinese-gb2312 japanese-jisx0208
237 korean-ksc5601 japanese-jisx0212
238 chinese-cns11643-1 chinese-cns11643-2
239 chinese-cns11643-3 chinese-cns11643-4
240 chinese-cns11643-5 chinese-cns11643-6
242 ,(if (or (not (fboundp 'charsetp
)) ;; non-Mule case
243 (charsetp 'unicode-a
)
244 (not (mm-coding-system-p 'mule-utf-8
)))
245 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e
)
246 ;; If we have utf-8 we're in Mule 5+.
249 (coding-system-get 'mule-utf-8
'safe-charsets
)))))
250 "Alist of MIME-charset/MULE-charsets.")
252 ;; Correct by construction, but should be unnecessary:
254 (when (and (not (featurep 'xemacs
))
255 (fboundp 'coding-system-list
)
256 (fboundp 'sort-coding-systems
))
257 (setq mm-mime-mule-charset-alist
262 (when (and (or (coding-system-get cs
:mime-charset
) ; Emacs 22
263 (coding-system-get cs
'mime-charset
))
264 (not (eq t
(coding-system-get cs
'safe-charsets
))))
265 (list (cons (or (coding-system-get cs
:mime-charset
)
266 (coding-system-get cs
'mime-charset
))
268 (coding-system-get cs
'safe-charsets
))))))
269 (sort-coding-systems (coding-system-list 'base-only
))))))
271 (defvar mm-hack-charsets
'(iso-8859-15 iso-2022-jp-2
)
272 "A list of special charsets.
273 Valid elements include:
274 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
275 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
278 (defvar mm-iso-8859-15-compatible
279 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
280 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
281 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
283 (defvar mm-iso-8859-x-to-15-table
284 (and (fboundp 'coding-system-p
)
285 (mm-coding-system-p 'iso-8859-15
)
288 (if (mm-coding-system-p (car cs
))
289 (let ((c (string-to-char
290 (decode-coding-string "\341" (car cs
)))))
291 (cons (char-charset c
)
294 (decode-coding-string "\341" 'iso-8859-15
)) c
)
295 (string-to-list (decode-coding-string (car (cdr cs
))
298 mm-iso-8859-15-compatible
))
299 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
301 (defcustom mm-coding-system-priorities
302 (if (boundp 'current-language-environment
)
303 (let ((lang (symbol-value 'current-language-environment
)))
304 (cond ((string= lang
"Japanese")
305 ;; Japanese users may prefer iso-2022-jp to shift-jis.
306 '(iso-2022-jp iso-2022-jp-2 japanese-shift-jis
307 iso-latin-1 utf-8
)))))
308 "Preferred coding systems for encoding outgoing messages.
310 More than one suitable coding system may be found for some text.
311 By default, the coding system with the highest priority is used
312 to encode outgoing messages (see `sort-coding-systems'). If this
313 variable is set, it overrides the default priority."
315 :type
'(repeat (symbol :tag
"Coding system"))
319 (defvar mm-use-find-coding-systems-region
320 (fboundp 'find-coding-systems-region
)
321 "Use `find-coding-systems-region' to find proper coding systems.
323 Setting it to nil is useful on Emacsen supporting Unicode if sending
324 mail with multiple parts is preferred to sending a Unicode one.")
326 ;;; Internal variables:
330 (defun mm-mule-charset-to-mime-charset (charset)
331 "Return the MIME charset corresponding to the given Mule CHARSET."
332 (if (and (fboundp 'find-coding-systems-for-charsets
)
333 (fboundp 'sort-coding-systems
))
335 (dolist (cs (sort-coding-systems
337 (find-coding-systems-for-charsets (list charset
)))))
340 (setq mime
(or (coding-system-get cs
:mime-charset
)
341 (coding-system-get cs
'mime-charset
))))))
343 (let ((alist mm-mime-mule-charset-alist
)
346 (when (memq charset
(cdar alist
))
347 (setq out
(caar alist
)
352 (defun mm-charset-to-coding-system (charset &optional lbt
)
353 "Return coding-system corresponding to CHARSET.
354 CHARSET is a symbol naming a MIME charset.
355 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
356 used as the line break code type of the coding system."
357 (when (stringp charset
)
358 (setq charset
(intern (downcase charset
))))
360 (setq charset
(intern (format "%s-%s" charset lbt
))))
364 ;; Running in a non-MULE environment.
365 ((or (null (mm-get-coding-system-list))
366 (not (fboundp 'coding-system-get
)))
369 ((eq charset
'us-ascii
)
371 ;; Check to see whether we can handle this charset. (This depends
372 ;; on there being some coding system matching each `mime-charset'
373 ;; property defined, as there should be.)
374 ((and (mm-coding-system-p charset
)
375 ;;; Doing this would potentially weed out incorrect charsets.
377 ;;; (eq charset (coding-system-get charset 'mime-charset))
380 ;; Translate invalid charsets.
381 ((let ((cs (cdr (assq charset mm-charset-synonym-alist
))))
382 (and cs
(mm-coding-system-p cs
) cs
)))
383 ;; Last resort: search the coding system list for entries which
384 ;; have the right mime-charset in case the canonical name isn't
385 ;; defined (though it should be).
387 ;; mm-get-coding-system-list returns a list of cs without lbt.
389 (dolist (c (mm-get-coding-system-list))
391 (eq charset
(or (coding-system-get c
:mime-charset
)
392 (coding-system-get c
'mime-charset
))))
396 (defsubst mm-replace-chars-in-string
(string from to
)
397 (mm-subst-char-in-string from to string
))
400 (defvar mm-emacs-mule
(and (not (featurep 'xemacs
))
401 (boundp 'default-enable-multibyte-characters
)
402 default-enable-multibyte-characters
403 (fboundp 'set-buffer-multibyte
))
404 "True in Emacs with Mule.")
407 (defun mm-enable-multibyte ()
408 "Set the multibyte flag of the current buffer.
409 Only do this if the default value of `enable-multibyte-characters' is
410 non-nil. This is a no-op in XEmacs."
411 (set-buffer-multibyte 'to
))
412 (defalias 'mm-enable-multibyte
'ignore
))
415 (defun mm-disable-multibyte ()
416 "Unset the multibyte flag of in the current buffer.
417 This is a no-op in XEmacs."
418 (set-buffer-multibyte nil
))
419 (defalias 'mm-disable-multibyte
'ignore
)))
421 (defun mm-preferred-coding-system (charset)
422 ;; A typo in some Emacs versions.
423 (or (get-charset-property charset
'preferred-coding-system
)
424 (get-charset-property charset
'prefered-coding-system
)))
426 ;; Mule charsets shouldn't be used.
427 (defsubst mm-guess-charset
()
428 "Guess Mule charset from the language environment."
430 mail-parse-mule-charset
;; cached mule-charset
432 (setq mail-parse-mule-charset
433 (and (boundp 'current-language-environment
)
436 (assoc current-language-environment
437 language-info-alist
))))))
438 (if (or (not mail-parse-mule-charset
)
439 (eq mail-parse-mule-charset
'ascii
))
440 (setq mail-parse-mule-charset
441 (or (car (last (assq mail-parse-charset
442 mm-mime-mule-charset-alist
)))
445 mail-parse-mule-charset
)))
447 (defun mm-charset-after (&optional pos
)
448 "Return charset of a character in current buffer at position POS.
449 If POS is nil, it defauls to the current point.
450 If POS is out of range, the value is nil.
451 If the charset is `composition', return the actual one."
452 (let ((char (char-after pos
)) charset
)
453 (if (< (mm-char-int char
) 128)
454 (setq charset
'ascii
)
455 ;; charset-after is fake in some Emacsen.
456 (setq charset
(and (fboundp 'char-charset
) (char-charset char
)))
457 (if (eq charset
'composition
) ; Mule 4
458 (let ((p (or pos
(point))))
459 (cadr (find-charset-region p
(1+ p
))))
460 (if (and charset
(not (memq charset
'(ascii eight-bit-control
461 eight-bit-graphic
))))
463 (mm-guess-charset))))))
465 (defun mm-mime-charset (charset)
466 "Return the MIME charset corresponding to the given Mule CHARSET."
467 (if (eq charset
'unknown
)
468 (error "The message contains non-printable characters, please use attachment"))
469 (if (and (fboundp 'coding-system-get
) (fboundp 'get-charset-property
))
470 ;; This exists in Emacs 20.
472 (and (mm-preferred-coding-system charset
)
473 (or (coding-system-get
474 (mm-preferred-coding-system charset
) :mime-charset
)
476 (mm-preferred-coding-system charset
) 'mime-charset
)))
477 (and (eq charset
'ascii
)
479 (mm-preferred-coding-system charset
)
480 (mm-mule-charset-to-mime-charset charset
))
481 ;; This is for XEmacs.
482 (mm-mule-charset-to-mime-charset charset
)))
484 (defun mm-delete-duplicates (list)
485 "Simple substitute for CL `delete-duplicates', testing with `equal'."
488 (setq head
(car list
))
489 (setq list
(delete head list
))
490 (setq result
(cons head result
)))
493 ;; Fixme: This is used in places when it should be testing the
494 ;; default multibyteness. See mm-default-multibyte-p.
496 (if (and (not (featurep 'xemacs
))
497 (boundp 'enable-multibyte-characters
))
498 (defun mm-multibyte-p ()
499 "Non-nil if multibyte is enabled in the current buffer."
500 enable-multibyte-characters
)
501 (defun mm-multibyte-p () (featurep 'mule
))))
503 (defun mm-default-multibyte-p ()
504 "Return non-nil if the session is multibyte.
505 This affects whether coding conversion should be attempted generally."
507 (if (boundp 'default-enable-multibyte-characters
)
508 default-enable-multibyte-characters
511 (defun mm-iso-8859-x-to-15-region (&optional b e
)
512 (if (fboundp 'char-charset
)
513 (let (charset item c inconvertible
)
515 (if e
(narrow-to-region b e
))
516 (goto-char (point-min))
517 (skip-chars-forward "\0-\177")
520 ((not (setq item
(assq (char-charset (setq c
(char-after)))
521 mm-iso-8859-x-to-15-table
)))
523 ((memq c
(cdr (cdr item
)))
524 (setq inconvertible t
)
527 (insert-before-markers (prog1 (+ c
(car (cdr item
)))
529 (skip-chars-forward "\0-\177")))
530 (not inconvertible
))))
532 (defun mm-sort-coding-systems-predicate (a b
)
535 ;; Note: invalid entries are dropped silently
536 (and (coding-system-p cs
)
537 (coding-system-base cs
)))
538 mm-coding-system-priorities
)))
539 (> (length (memq a priorities
))
540 (length (memq b priorities
)))))
542 (defun mm-find-mime-charset-region (b e
&optional hack-charsets
)
543 "Return the MIME charsets needed to encode the region between B and E.
544 nil means ASCII, a single-element list represents an appropriate MIME
545 charset, and a longer list means no appropriate charset."
547 ;; The return possibilities of this function are a mess...
548 (or (and (mm-multibyte-p)
549 mm-use-find-coding-systems-region
550 ;; Find the mime-charset of the most preferred coding
551 ;; system that has one.
552 (let ((systems (find-coding-systems-region b e
)))
553 (when mm-coding-system-priorities
555 (sort systems
'mm-sort-coding-systems-predicate
)))
556 (setq systems
(delq 'compound-text systems
))
557 (unless (equal systems
'(undecided))
559 (let* ((head (pop systems
))
560 (cs (or (coding-system-get head
:mime-charset
)
561 (coding-system-get head
'mime-charset
))))
562 ;; The mime-charset (`x-ctext') of
563 ;; `compound-text' is not in the IANA list. We
564 ;; shouldn't normally use anything here with a
565 ;; mime-charset having an `x-' prefix.
566 ;; Fixme: Allow this to be overridden, since
567 ;; there is existing use of x-ctext.
568 ;; Also people apparently need the coding system
569 ;; `iso-2022-jp-3' (which Mule-UCS defines with
570 ;; mime-charset, though it's not valid).
572 (not (string-match "^[Xx]-" (symbol-name cs
)))
573 ;; UTF-16 of any variety is invalid for
574 ;; text parts and, unfortunately, has
575 ;; mime-charset defined both in Mule-UCS
576 ;; and versions of Emacs. (The name
577 ;; might be `mule-utf-16...' or
579 (not (string-match "utf-16" (symbol-name cs
))))
581 charsets
(list cs
))))))
583 ;; Otherwise we're not multibyte, we're XEmacs, or a single
584 ;; coding system won't cover it.
586 (mm-delete-duplicates
587 (mapcar 'mm-mime-charset
589 (mm-find-charset-region b e
))))))
590 (if (and (> (length charsets
) 1)
591 (memq 'iso-8859-15 charsets
)
592 (memq 'iso-8859-15 hack-charsets
)
593 (save-excursion (mm-iso-8859-x-to-15-region b e
)))
594 (mapcar (lambda (x) (setq charsets
(delq (car x
) charsets
)))
595 mm-iso-8859-15-compatible
))
596 (if (and (memq 'iso-2022-jp-2 charsets
)
597 (memq 'iso-2022-jp-2 hack-charsets
))
598 (setq charsets
(delq 'iso-2022-jp charsets
)))
601 (defmacro mm-with-unibyte-buffer
(&rest forms
)
602 "Create a temporary buffer, and evaluate FORMS there like `progn'.
603 Use unibyte mode for this."
604 `(let (default-enable-multibyte-characters)
605 (with-temp-buffer ,@forms
)))
606 (put 'mm-with-unibyte-buffer
'lisp-indent-function
0)
607 (put 'mm-with-unibyte-buffer
'edebug-form-spec
'(body))
609 (defmacro mm-with-multibyte-buffer
(&rest forms
)
610 "Create a temporary buffer, and evaluate FORMS there like `progn'.
611 Use multibyte mode for this."
612 `(let ((default-enable-multibyte-characters t
))
613 (with-temp-buffer ,@forms
)))
614 (put 'mm-with-multibyte-buffer
'lisp-indent-function
0)
615 (put 'mm-with-multibyte-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 (defmacro mm-with-multibyte
(&rest forms
)
645 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
646 `(let ((default-enable-multibyte-characters t
))
648 (put 'mm-with-multibyte
'lisp-indent-function
0)
649 (put 'mm-with-multibyte
'edebug-form-spec
'(body))
651 (defun mm-find-charset-region (b e
)
652 "Return a list of Emacs charsets in the region B to E."
654 ((and (mm-multibyte-p)
655 (fboundp 'find-charset-region
))
656 ;; Remove composition since the base charsets have been included.
657 ;; Remove eight-bit-*, treat them as ascii.
658 (let ((css (find-charset-region b e
)))
659 (mapcar (lambda (cs) (setq css
(delq cs css
)))
660 '(composition eight-bit-control eight-bit-graphic
664 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
667 (narrow-to-region b e
)
668 (goto-char (point-min))
669 (skip-chars-forward "\0-\177")
674 (and (boundp 'current-language-environment
)
675 (car (last (assq 'charset
676 (assoc current-language-environment
677 language-info-alist
))))))
678 (if (eq charset
'ascii
) (setq charset nil
))
681 (car (last (assq mail-parse-charset
682 mm-mime-mule-charset-alist
)))))
683 (list 'ascii
(or charset
'latin-iso8859-1
)))))))))
685 (if (fboundp 'shell-quote-argument
)
686 (defalias 'mm-quote-arg
'shell-quote-argument
)
687 (defun mm-quote-arg (arg)
688 "Return a version of ARG that is safe to evaluate in a shell."
689 (let ((pos 0) new-pos accum
)
690 ;; *** bug: we don't handle newline characters properly
691 (while (setq new-pos
(string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos
))
692 (push (substring arg pos new-pos
) accum
)
694 (push (list (aref arg new-pos
)) accum
)
695 (setq pos
(1+ new-pos
)))
698 (apply 'concat
(nconc (nreverse accum
) (list (substring arg pos
))))))))
700 (defun mm-auto-mode-alist ()
701 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
702 (let ((alist auto-mode-alist
)
705 (when (listp (cdar alist
))
706 (push (car alist
) out
))
710 (defvar mm-inhibit-file-name-handlers
711 '(jka-compr-handler image-file-handler
)
712 "A list of handlers doing (un)compression (etc) thingies.")
714 (defun mm-insert-file-contents (filename &optional visit beg end replace
716 "Like `insert-file-contents', but only reads in the file.
717 A buffer may be modified in several ways after reading into the buffer due
718 to advanced Emacs features, such as file-name-handlers, format decoding,
719 `find-file-hooks', etc.
720 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
721 This function ensures that none of these modifications will take place."
722 (let ((format-alist nil
)
723 (auto-mode-alist (if inhibit nil
(mm-auto-mode-alist)))
724 (default-major-mode 'fundamental-mode
)
725 (enable-local-variables nil
)
726 (after-insert-file-functions nil
)
727 (enable-local-eval nil
)
728 (find-file-hooks nil
)
729 (inhibit-file-name-operation (if inhibit
730 'insert-file-contents
731 inhibit-file-name-operation
))
732 (inhibit-file-name-handlers
734 (append mm-inhibit-file-name-handlers
735 inhibit-file-name-handlers
)
736 inhibit-file-name-handlers
)))
737 (insert-file-contents filename visit beg end replace
)))
739 (defun mm-append-to-file (start end filename
&optional codesys inhibit
)
740 "Append the contents of the region to the end of file FILENAME.
741 When called from a function, expects three arguments,
742 START, END and FILENAME. START and END are buffer positions
743 saying what text to write.
744 Optional fourth argument specifies the coding system to use when
746 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
747 (let ((coding-system-for-write
748 (or codesys mm-text-coding-system-for-write
749 mm-text-coding-system
))
750 (inhibit-file-name-operation (if inhibit
752 inhibit-file-name-operation
))
753 (inhibit-file-name-handlers
755 (append mm-inhibit-file-name-handlers
756 inhibit-file-name-handlers
)
757 inhibit-file-name-handlers
)))
758 (write-region start end filename t
'no-message
)
759 (message "Appended to %s" filename
)))
761 (defun mm-write-region (start end filename
&optional append visit lockname
762 coding-system inhibit
)
764 "Like `write-region'.
765 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
766 (let ((coding-system-for-write
767 (or coding-system mm-text-coding-system-for-write
768 mm-text-coding-system
))
769 (inhibit-file-name-operation (if inhibit
771 inhibit-file-name-operation
))
772 (inhibit-file-name-handlers
774 (append mm-inhibit-file-name-handlers
775 inhibit-file-name-handlers
)
776 inhibit-file-name-handlers
)))
777 (write-region start end filename append visit lockname
)))
779 (defun mm-image-load-path (&optional package
)
781 (dolist (path load-path
(nreverse result
))
784 (setq dir
(concat (file-name-directory
785 (directory-file-name path
))
786 "etc/" (or package
"gnus/")))))
788 (push path result
))))
790 ;; Fixme: This doesn't look useful where it's used.
791 (if (fboundp 'detect-coding-region
)
792 (defun mm-detect-coding-region (start end
)
793 "Like `detect-coding-region' except returning the best one."
794 (let ((coding-systems
795 (detect-coding-region (point) (point-max))))
796 (or (car-safe coding-systems
)
798 (defun mm-detect-coding-region (start end
)
799 (let ((point (point)))
801 (skip-chars-forward "\0-\177" end
)
803 (if (eq (point) end
) 'ascii
(mm-guess-charset))
804 (goto-char point
)))))
806 (if (fboundp 'coding-system-get
)
807 (defun mm-detect-mime-charset-region (start end
)
808 "Detect MIME charset of the text in the region between START and END."
809 (let ((cs (mm-detect-coding-region start end
)))
810 (coding-system-get cs
'mime-charset
)))
811 (defun mm-detect-mime-charset-region (start end
)
812 "Detect MIME charset of the text in the region between START and END."
813 (let ((cs (mm-detect-coding-region start end
)))
819 ;;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
820 ;;; mm-util.el ends here