1 ;;; mm-util.el --- Utility functions for Mule and low level things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
29 (eval-when-compile (require 'cl
))
35 (let ((nfunc (intern (format "mm-%s" (car elem
)))))
36 (if (fboundp (car elem
))
37 (defalias nfunc
(car elem
))
38 (defalias nfunc
(cdr elem
)))))
39 '((decode-coding-string .
(lambda (s a
) s
))
40 (encode-coding-string .
(lambda (s a
) s
))
41 (encode-coding-region . ignore
)
42 (coding-system-list . ignore
)
43 (decode-coding-region . ignore
)
45 (coding-system-equal . equal
)
46 (annotationp . ignore
)
47 (set-buffer-file-coding-system . ignore
)
49 .
(lambda (charset int
)
57 (mapcar (lambda (e) (list (symbol-name (car e
))))
58 mm-mime-mule-charset-alist
)
61 .
(lambda (from to string
&optional inplace
)
62 ;; stolen (and renamed) from nnheader.el
63 "Replace characters in STRING from FROM to TO.
64 Unless optional argument INPLACE is non-nil, return a new string."
65 (let ((string (if inplace string
(copy-sequence string
)))
68 ;; Replace all occurrences of FROM with TO.
70 (when (= (aref string idx
) from
)
74 (string-as-unibyte . identity
)
75 (string-make-unibyte . identity
)
76 ;; string-as-multibyte often doesn't really do what you think it does.
78 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
79 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
80 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
81 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
83 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
84 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
85 ;; Better use string-to-multibyte or encode-coding-string.
86 ;; If you really need string-as-multibyte somewhere it's usually
87 ;; because you're using the internal emacs-mule representation (maybe
88 ;; because you're using string-as-unibyte somewhere), which is
89 ;; generally a problem in itself.
90 ;; Here is an approximate equivalence table to help think about it:
91 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
92 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
93 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
94 (string-as-multibyte . identity
)
97 "Return a multibyte string with the same individual chars as string."
99 (lambda (ch) (mm-string-as-multibyte (char-to-string ch
)))
101 (multibyte-string-p . ignore
)
102 (insert-byte . insert-char
)
103 (multibyte-char-to-unibyte . identity
))))
107 ((fboundp 'replace-in-string
)
108 (defalias 'mm-replace-in-string
'replace-in-string
))
109 ((fboundp 'replace-regexp-in-string
)
110 (defun mm-replace-in-string (string regexp newtext
&optional literal
)
111 "Replace all matches for REGEXP with NEWTEXT in STRING.
112 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
113 string containing the replacements.
115 This is a compatibility function for different Emacsen."
116 (replace-regexp-in-string regexp newtext string nil literal
)))
118 (defun mm-replace-in-string (string regexp newtext
&optional literal
)
119 "Replace all matches for REGEXP with NEWTEXT in STRING.
120 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
121 string containing the replacements.
123 This is a compatibility function for different Emacsen."
124 (let ((start 0) tail
)
125 (while (string-match regexp string start
)
126 (setq tail
(- (length string
) (match-end 0)))
127 (setq string
(replace-match newtext nil literal string
))
128 (setq start
(- (length string
) tail
))))
132 (defalias 'mm-char-or-char-int-p
134 ((fboundp 'char-or-char-int-p
) 'char-or-char-int-p
)
135 ((fboundp 'char-valid-p
) 'char-valid-p
)
138 ;; Fixme: This seems always to be used to read a MIME charset, so it
139 ;; should be re-named and fixed (in Emacs) to offer completion only on
140 ;; proper charset names (base coding systems which have a
141 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
143 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
144 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
145 ;; Actually, there should be an `mm-coding-system-mime-charset'.
147 (defalias 'mm-read-coding-system
149 ((fboundp 'read-coding-system
)
150 (if (and (featurep 'xemacs
)
151 (<= (string-to-number emacs-version
) 21.1))
152 (lambda (prompt &optional default-coding-system
)
153 (read-coding-system prompt
))
154 'read-coding-system
))
155 (t (lambda (prompt &optional default-coding-system
)
156 "Prompt the user for a coding system."
158 prompt
(mapcar (lambda (s) (list (symbol-name (car s
))))
159 mm-mime-mule-charset-alist
)))))))
161 (defvar mm-coding-system-list nil
)
162 (defun mm-get-coding-system-list ()
163 "Get the coding system list."
164 (or mm-coding-system-list
165 (setq mm-coding-system-list
(mm-coding-system-list))))
167 (defun mm-coding-system-p (cs)
168 "Return non-nil if CS is a symbol naming a coding system.
169 In XEmacs, also return non-nil if CS is a coding system object.
170 If CS is available, return CS itself in Emacs, and return a coding
171 system object in XEmacs."
172 (if (fboundp 'find-coding-system
)
173 (and cs
(find-coding-system cs
))
174 (if (fboundp 'coding-system-p
)
175 (when (coding-system-p cs
)
178 (car (memq cs
(mm-get-coding-system-list))))))
180 (defvar mm-charset-synonym-alist
182 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
183 ,@(unless (mm-coding-system-p 'x-ctext
)
184 '((x-ctext . ctext
)))
185 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_!
186 ,@(unless (mm-coding-system-p 'iso-8859-15
)
187 '((iso-8859-15 . iso-8859-1
)))
188 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
189 ,@(unless (mm-coding-system-p 'big5-hkscs
)
190 '((big5-hkscs . big5
)))
191 ;; Windows-1252 is actually a superset of Latin-1. See also
192 ;; `gnus-article-dumbquotes-map'.
193 ,@(unless (mm-coding-system-p 'windows-1252
)
194 (if (mm-coding-system-p 'cp1252
)
195 '((windows-1252 . cp1252
))
196 '((windows-1252 . iso-8859-1
))))
197 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
198 ;; Outlook users in Czech republic. Use this to allow reading of their
199 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
200 ,@(if (and (not (mm-coding-system-p 'windows-1250
))
201 (mm-coding-system-p 'cp1250
))
202 '((windows-1250 . cp1250
)))
204 "A mapping from invalid charset names to the real charset names.")
206 (defvar mm-binary-coding-system
208 ((mm-coding-system-p 'binary
) 'binary
)
209 ((mm-coding-system-p 'no-conversion
) 'no-conversion
)
211 "100% binary coding system.")
213 (defvar mm-text-coding-system
214 (or (if (memq system-type
'(windows-nt ms-dos ms-windows
))
215 (and (mm-coding-system-p 'raw-text-dos
) 'raw-text-dos
)
216 (and (mm-coding-system-p 'raw-text
) 'raw-text
))
217 mm-binary-coding-system
)
218 "Text-safe coding system (For removing ^M).")
220 (defvar mm-text-coding-system-for-write nil
221 "Text coding system for write.")
223 (defvar mm-auto-save-coding-system
225 ((mm-coding-system-p 'utf-8-emacs
) ; Mule 7
226 (if (memq system-type
'(windows-nt ms-dos ms-windows
))
227 (if (mm-coding-system-p 'utf-8-emacs-dos
)
228 'utf-8-emacs-dos mm-binary-coding-system
)
230 ((mm-coding-system-p 'emacs-mule
)
231 (if (memq system-type
'(windows-nt ms-dos ms-windows
))
232 (if (mm-coding-system-p 'emacs-mule-dos
)
233 'emacs-mule-dos mm-binary-coding-system
)
235 ((mm-coding-system-p 'escape-quoted
) 'escape-quoted
)
236 (t mm-binary-coding-system
))
237 "Coding system of auto save file.")
239 (defvar mm-universal-coding-system mm-auto-save-coding-system
240 "The universal coding system.")
242 ;; Fixme: some of the cars here aren't valid MIME charsets. That
243 ;; should only matter with XEmacs, though.
244 (defvar mm-mime-mule-charset-alist
246 (iso-8859-1 latin-iso8859-1
)
247 (iso-8859-2 latin-iso8859-2
)
248 (iso-8859-3 latin-iso8859-3
)
249 (iso-8859-4 latin-iso8859-4
)
250 (iso-8859-5 cyrillic-iso8859-5
)
251 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
252 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
253 ;; charset is koi8-r, not iso-8859-5.
254 (koi8-r cyrillic-iso8859-5 gnus-koi8-r
)
255 (iso-8859-6 arabic-iso8859-6
)
256 (iso-8859-7 greek-iso8859-7
)
257 (iso-8859-8 hebrew-iso8859-8
)
258 (iso-8859-9 latin-iso8859-9
)
259 (iso-8859-14 latin-iso8859-14
)
260 (iso-8859-15 latin-iso8859-15
)
261 (viscii vietnamese-viscii-lower
)
262 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978
)
263 (euc-kr korean-ksc5601
)
264 (gb2312 chinese-gb2312
)
265 (big5 chinese-big5-1 chinese-big5-2
)
267 (thai-tis620 thai-tis620
)
268 (windows-1251 cyrillic-iso8859-5
)
269 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column
)
270 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
271 latin-jisx0201 japanese-jisx0208-1978
272 chinese-gb2312 japanese-jisx0208
273 korean-ksc5601 japanese-jisx0212
)
274 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
275 latin-jisx0201 japanese-jisx0208-1978
276 chinese-gb2312 japanese-jisx0208
277 korean-ksc5601 japanese-jisx0212
278 chinese-cns11643-1 chinese-cns11643-2
)
279 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
280 cyrillic-iso8859-5 greek-iso8859-7
281 latin-jisx0201 japanese-jisx0208-1978
282 chinese-gb2312 japanese-jisx0208
283 korean-ksc5601 japanese-jisx0212
284 chinese-cns11643-1 chinese-cns11643-2
285 chinese-cns11643-3 chinese-cns11643-4
286 chinese-cns11643-5 chinese-cns11643-6
288 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
289 japanese-jisx0213-1 japanese-jisx0213-2
)
290 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208
)
291 ,(if (or (not (fboundp 'charsetp
)) ;; non-Mule case
292 (charsetp 'unicode-a
)
293 (not (mm-coding-system-p 'mule-utf-8
)))
294 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e
)
295 ;; If we have utf-8 we're in Mule 5+.
298 (coding-system-get 'mule-utf-8
'safe-charsets
)))))
299 "Alist of MIME-charset/MULE-charsets.")
301 (defun mm-enrich-utf-8-by-mule-ucs ()
302 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
303 This function will run when the `un-define' module is loaded under
304 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
305 with Mule charsets. It is completely useless for Emacs."
306 (unless (cdr (delete '(mm-enrich-utf-8-by-mule-ucs)
307 (assoc "un-define" after-load-alist
)))
308 (setq after-load-alist
309 (delete '("un-define") after-load-alist
)))
310 (when (boundp 'unicode-basic-translation-charset-order-list
)
316 'unicode-basic-translation-charset-order-list
))))
317 (elem (assq 'utf-8 mm-mime-mule-charset-alist
)))
320 (setq mm-mime-mule-charset-alist
321 (nconc mm-mime-mule-charset-alist
322 (list (cons 'utf-8 val
))))))
325 ;; Correct by construction, but should be unnecessary for Emacs:
326 (if (featurep 'xemacs
)
327 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
328 (when (and (fboundp 'coding-system-list
)
329 (fboundp 'sort-coding-systems
))
330 (let ((css (sort-coding-systems (coding-system-list 'base-only
)))
334 mime
(or (coding-system-get cs
:mime-charset
) ; Emacs 23 (unicode)
335 (coding-system-get cs
'mime-charset
)))
337 (not (eq t
(setq mule
338 (coding-system-get cs
'safe-charsets
))))
339 (not (assq mime alist
)))
340 (push (cons mime
(delq 'ascii mule
)) alist
)))
341 (setq mm-mime-mule-charset-alist
(nreverse alist
)))))
343 (defvar mm-hack-charsets
'(iso-8859-15 iso-2022-jp-2
)
344 "A list of special charsets.
345 Valid elements include:
346 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
347 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
350 (defvar mm-iso-8859-15-compatible
351 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
352 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
353 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
355 (defvar mm-iso-8859-x-to-15-table
356 (and (fboundp 'coding-system-p
)
357 (mm-coding-system-p 'iso-8859-15
)
360 (if (mm-coding-system-p (car cs
))
361 (let ((c (string-to-char
362 (decode-coding-string "\341" (car cs
)))))
363 (cons (char-charset c
)
366 (decode-coding-string "\341" 'iso-8859-15
)) c
)
367 (string-to-list (decode-coding-string (car (cdr cs
))
370 mm-iso-8859-15-compatible
))
371 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
373 (defcustom mm-coding-system-priorities
374 (if (boundp 'current-language-environment
)
375 (let ((lang (symbol-value 'current-language-environment
)))
376 (cond ((string= lang
"Japanese")
377 ;; Japanese users prefer iso-2022-jp to euc-japan or
378 ;; shift_jis, however iso-8859-1 should be used when
379 ;; there are only ASCII text and Latin-1 characters.
380 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8
)))))
381 "Preferred coding systems for encoding outgoing messages.
383 More than one suitable coding system may be found for some text.
384 By default, the coding system with the highest priority is used
385 to encode outgoing messages (see `sort-coding-systems'). If this
386 variable is set, it overrides the default priority."
388 :type
'(repeat (symbol :tag
"Coding system"))
392 (defvar mm-use-find-coding-systems-region
393 (fboundp 'find-coding-systems-region
)
394 "Use `find-coding-systems-region' to find proper coding systems.
396 Setting it to nil is useful on Emacsen supporting Unicode if sending
397 mail with multiple parts is preferred to sending a Unicode one.")
399 ;;; Internal variables:
403 (defun mm-mule-charset-to-mime-charset (charset)
404 "Return the MIME charset corresponding to the given Mule CHARSET."
405 (if (and (fboundp 'find-coding-systems-for-charsets
)
406 (fboundp 'sort-coding-systems
))
407 (let ((css (sort (sort-coding-systems
408 (find-coding-systems-for-charsets (list charset
)))
409 'mm-sort-coding-systems-predicate
))
411 (while (and (not mime
)
413 (when (setq cs
(pop css
))
414 (setq mime
(or (coding-system-get cs
:mime-charset
)
415 (coding-system-get cs
'mime-charset
)))))
417 (let ((alist (mapcar (lambda (cs)
418 (assq cs mm-mime-mule-charset-alist
))
419 (sort (mapcar 'car mm-mime-mule-charset-alist
)
420 'mm-sort-coding-systems-predicate
)))
423 (when (memq charset
(cdar alist
))
424 (setq out
(caar alist
)
429 (defun mm-charset-to-coding-system (charset &optional lbt
)
430 "Return coding-system corresponding to CHARSET.
431 CHARSET is a symbol naming a MIME charset.
432 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
433 used as the line break code type of the coding system."
434 (when (stringp charset
)
435 (setq charset
(intern (downcase charset
))))
437 (setq charset
(intern (format "%s-%s" charset lbt
))))
441 ;; Running in a non-MULE environment.
442 ((or (null (mm-get-coding-system-list))
443 (not (fboundp 'coding-system-get
)))
446 ((eq charset
'us-ascii
)
448 ;; Check to see whether we can handle this charset. (This depends
449 ;; on there being some coding system matching each `mime-charset'
450 ;; property defined, as there should be.)
451 ((and (mm-coding-system-p charset
)
452 ;;; Doing this would potentially weed out incorrect charsets.
454 ;;; (eq charset (coding-system-get charset 'mime-charset))
457 ;; Translate invalid charsets.
458 ((let ((cs (cdr (assq charset mm-charset-synonym-alist
))))
459 (and cs
(mm-coding-system-p cs
) cs
)))
460 ;; Last resort: search the coding system list for entries which
461 ;; have the right mime-charset in case the canonical name isn't
462 ;; defined (though it should be).
464 ;; mm-get-coding-system-list returns a list of cs without lbt.
466 (dolist (c (mm-get-coding-system-list))
468 (eq charset
(or (coding-system-get c
:mime-charset
)
469 (coding-system-get c
'mime-charset
))))
473 (defsubst mm-replace-chars-in-string
(string from to
)
474 (mm-subst-char-in-string from to string
))
477 (defvar mm-emacs-mule
(and (not (featurep 'xemacs
))
478 (boundp 'default-enable-multibyte-characters
)
479 default-enable-multibyte-characters
480 (fboundp 'set-buffer-multibyte
))
481 "True in Emacs with Mule.")
484 (defun mm-enable-multibyte ()
485 "Set the multibyte flag of the current buffer.
486 Only do this if the default value of `enable-multibyte-characters' is
487 non-nil. This is a no-op in XEmacs."
488 (set-buffer-multibyte 'to
))
489 (defalias 'mm-enable-multibyte
'ignore
))
492 (defun mm-disable-multibyte ()
493 "Unset the multibyte flag of in the current buffer.
494 This is a no-op in XEmacs."
495 (set-buffer-multibyte nil
))
496 (defalias 'mm-disable-multibyte
'ignore
)))
498 (defun mm-preferred-coding-system (charset)
499 ;; A typo in some Emacs versions.
500 (or (get-charset-property charset
'preferred-coding-system
)
501 (get-charset-property charset
'prefered-coding-system
)))
503 ;; Mule charsets shouldn't be used.
504 (defsubst mm-guess-charset
()
505 "Guess Mule charset from the language environment."
507 mail-parse-mule-charset
;; cached mule-charset
509 (setq mail-parse-mule-charset
510 (and (boundp 'current-language-environment
)
513 (assoc current-language-environment
514 language-info-alist
))))))
515 (if (or (not mail-parse-mule-charset
)
516 (eq mail-parse-mule-charset
'ascii
))
517 (setq mail-parse-mule-charset
518 (or (car (last (assq mail-parse-charset
519 mm-mime-mule-charset-alist
)))
522 mail-parse-mule-charset
)))
524 (defun mm-charset-after (&optional pos
)
525 "Return charset of a character in current buffer at position POS.
526 If POS is nil, it defauls to the current point.
527 If POS is out of range, the value is nil.
528 If the charset is `composition', return the actual one."
529 (let ((char (char-after pos
)) charset
)
530 (if (< (mm-char-int char
) 128)
531 (setq charset
'ascii
)
532 ;; charset-after is fake in some Emacsen.
533 (setq charset
(and (fboundp 'char-charset
) (char-charset char
)))
534 (if (eq charset
'composition
) ; Mule 4
535 (let ((p (or pos
(point))))
536 (cadr (find-charset-region p
(1+ p
))))
537 (if (and charset
(not (memq charset
'(ascii eight-bit-control
538 eight-bit-graphic
))))
540 (mm-guess-charset))))))
542 (defun mm-mime-charset (charset)
543 "Return the MIME charset corresponding to the given Mule CHARSET."
544 (if (eq charset
'unknown
)
545 (error "The message contains non-printable characters, please use attachment"))
546 (if (and (fboundp 'coding-system-get
) (fboundp 'get-charset-property
))
547 ;; This exists in Emacs 20.
549 (and (mm-preferred-coding-system charset
)
550 (or (coding-system-get
551 (mm-preferred-coding-system charset
) :mime-charset
)
553 (mm-preferred-coding-system charset
) 'mime-charset
)))
554 (and (eq charset
'ascii
)
556 (mm-preferred-coding-system charset
)
557 (mm-mule-charset-to-mime-charset charset
))
558 ;; This is for XEmacs.
559 (mm-mule-charset-to-mime-charset charset
)))
561 (if (fboundp 'delete-dups
)
562 (defalias 'mm-delete-duplicates
'delete-dups
)
563 (defun mm-delete-duplicates (list)
564 "Destructively remove `equal' duplicates from LIST.
565 Store the result in LIST and return it. LIST must be a proper list.
566 Of several `equal' occurrences of an element in LIST, the first
569 This is a compatibility function for Emacsen without `delete-dups'."
570 ;; Code from `subr.el' in Emacs 22:
573 (setcdr tail
(delete (car tail
) (cdr tail
)))
574 (setq tail
(cdr tail
))))
577 ;; Fixme: This is used in places when it should be testing the
578 ;; default multibyteness. See mm-default-multibyte-p.
580 (if (and (not (featurep 'xemacs
))
581 (boundp 'enable-multibyte-characters
))
582 (defun mm-multibyte-p ()
583 "Non-nil if multibyte is enabled in the current buffer."
584 enable-multibyte-characters
)
585 (defun mm-multibyte-p () (featurep 'mule
))))
587 (defun mm-default-multibyte-p ()
588 "Return non-nil if the session is multibyte.
589 This affects whether coding conversion should be attempted generally."
591 (if (boundp 'default-enable-multibyte-characters
)
592 default-enable-multibyte-characters
595 (defun mm-iso-8859-x-to-15-region (&optional b e
)
596 (if (fboundp 'char-charset
)
597 (let (charset item c inconvertible
)
599 (if e
(narrow-to-region b e
))
600 (goto-char (point-min))
601 (skip-chars-forward "\0-\177")
604 ((not (setq item
(assq (char-charset (setq c
(char-after)))
605 mm-iso-8859-x-to-15-table
)))
607 ((memq c
(cdr (cdr item
)))
608 (setq inconvertible t
)
611 (insert-before-markers (prog1 (+ c
(car (cdr item
)))
613 (skip-chars-forward "\0-\177")))
614 (not inconvertible
))))
616 (defun mm-sort-coding-systems-predicate (a b
)
619 ;; Note: invalid entries are dropped silently
620 (and (setq cs
(mm-coding-system-p cs
))
621 (coding-system-base cs
)))
622 mm-coding-system-priorities
)))
623 (and (setq a
(mm-coding-system-p a
))
624 (if (setq b
(mm-coding-system-p b
))
625 (> (length (memq (coding-system-base a
) priorities
))
626 (length (memq (coding-system-base b
) priorities
)))
630 (autoload 'latin-unity-massage-name
"latin-unity")
631 (autoload 'latin-unity-maybe-remap
"latin-unity")
632 (autoload 'latin-unity-representations-feasible-region
"latin-unity")
633 (autoload 'latin-unity-representations-present-region
"latin-unity")
634 (defvar latin-unity-coding-systems
)
635 (defvar latin-unity-ucs-list
))
637 (defun mm-xemacs-find-mime-charset-1 (begin end
)
638 "Determine which MIME charset to use to send region as message.
639 This uses the XEmacs-specific latin-unity package to better handle the
640 case where identical characters from diverse ISO-8859-? character sets
641 can be encoded using a single one of the corresponding coding systems.
643 It treats `mm-coding-system-priorities' as the list of preferred
644 coding systems; a useful example setting for this list in Western
645 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
646 to the very standard Latin 1 coding system, and only move to coding
647 systems that are less supported as is necessary to encode the
648 characters that exist in the buffer.
650 Latin Unity doesn't know about those non-ASCII Roman characters that
651 are available in various East Asian character sets. As such, its
652 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
653 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
654 But this is very much a corner case, so don't worry about it."
655 (let ((systems mm-coding-system-priorities
) csets psets curset
)
657 ;; Load the Latin Unity library, if available.
658 (when (and (not (featurep 'latin-unity
)) (locate-library "latin-unity"))
659 (ignore-errors (require 'latin-unity
)))
661 ;; Now, can we use it?
662 (if (featurep 'latin-unity
)
664 (setq csets
(latin-unity-representations-feasible-region begin end
)
665 psets
(latin-unity-representations-present-region begin end
))
669 ;; Pass back the first coding system in the preferred list
670 ;; that can encode the whole region.
671 (dolist (curset systems
)
672 (setq curset
(latin-unity-massage-name 'buffer-default curset
))
674 ;; If the coding system is a universal coding system, then
675 ;; it can certainly encode all the characters in the region.
676 (if (memq curset latin-unity-ucs-list
)
677 (throw 'done
(list curset
)))
679 ;; If a coding system isn't universal, and isn't in
680 ;; the list that latin unity knows about, we can't
681 ;; decide whether to use it here. Leave that until later
682 ;; in `mm-find-mime-charset-region' function, whence we
684 (unless (memq curset latin-unity-coding-systems
)
687 ;; Right, we know about this coding system, and it may
688 ;; conceivably be able to encode all the characters in
690 (if (latin-unity-maybe-remap begin end curset csets psets t
)
691 (throw 'done
(list curset
))))
693 ;; Can't encode using anything from the
694 ;; `mm-coding-system-priorities' list.
695 ;; Leave `mm-find-mime-charset' to do most of the work.
698 ;; Right, latin unity isn't available; let `mm-find-charset-region'
699 ;; take its default action, which equally applies to GNU Emacs.
702 (defmacro mm-xemacs-find-mime-charset
(begin end
)
703 (when (featurep 'xemacs
)
704 `(and (featurep 'mule
) (mm-xemacs-find-mime-charset-1 ,begin
,end
))))
706 (defun mm-find-mime-charset-region (b e
&optional hack-charsets
)
707 "Return the MIME charsets needed to encode the region between B and E.
708 nil means ASCII, a single-element list represents an appropriate MIME
709 charset, and a longer list means no appropriate charset."
711 ;; The return possibilities of this function are a mess...
712 (or (and (mm-multibyte-p)
713 mm-use-find-coding-systems-region
714 ;; Find the mime-charset of the most preferred coding
715 ;; system that has one.
716 (let ((systems (find-coding-systems-region b e
)))
717 (when mm-coding-system-priorities
719 (sort systems
'mm-sort-coding-systems-predicate
)))
720 (setq systems
(delq 'compound-text systems
))
721 (unless (equal systems
'(undecided))
723 (let* ((head (pop systems
))
724 (cs (or (coding-system-get head
:mime-charset
)
725 (coding-system-get head
'mime-charset
))))
726 ;; The mime-charset (`x-ctext') of
727 ;; `compound-text' is not in the IANA list. We
728 ;; shouldn't normally use anything here with a
729 ;; mime-charset having an `x-' prefix.
730 ;; Fixme: Allow this to be overridden, since
731 ;; there is existing use of x-ctext.
732 ;; Also people apparently need the coding system
733 ;; `iso-2022-jp-3' (which Mule-UCS defines with
734 ;; mime-charset, though it's not valid).
736 (not (string-match "^[Xx]-" (symbol-name cs
)))
737 ;; UTF-16 of any variety is invalid for
738 ;; text parts and, unfortunately, has
739 ;; mime-charset defined both in Mule-UCS
740 ;; and versions of Emacs. (The name
741 ;; might be `mule-utf-16...' or
743 (not (string-match "utf-16" (symbol-name cs
))))
745 charsets
(list cs
))))))
747 ;; If we're XEmacs, and some coding system is appropriate,
748 ;; mm-xemacs-find-mime-charset will return an appropriate list.
749 ;; Otherwise, we'll get nil, and the next setq will get invoked.
750 (setq charsets
(mm-xemacs-find-mime-charset b e
))
752 ;; We're not multibyte, or a single coding system won't cover it.
754 (mm-delete-duplicates
755 (mapcar 'mm-mime-charset
757 (mm-find-charset-region b e
))))))
758 (if (and (> (length charsets
) 1)
759 (memq 'iso-8859-15 charsets
)
760 (memq 'iso-8859-15 hack-charsets
)
761 (save-excursion (mm-iso-8859-x-to-15-region b e
)))
762 (mapcar (lambda (x) (setq charsets
(delq (car x
) charsets
)))
763 mm-iso-8859-15-compatible
))
764 (if (and (memq 'iso-2022-jp-2 charsets
)
765 (memq 'iso-2022-jp-2 hack-charsets
))
766 (setq charsets
(delq 'iso-2022-jp charsets
)))
767 ;; Attempt to reduce the number of charsets if utf-8 is available.
768 (if (and (featurep 'xemacs
)
769 (> (length charsets
) 1)
770 (mm-coding-system-p 'utf-8
))
771 (let ((mm-coding-system-priorities
772 (cons 'utf-8 mm-coding-system-priorities
)))
774 (mm-delete-duplicates
775 (mapcar 'mm-mime-charset
777 (mm-find-charset-region b e
)))))))
780 (defmacro mm-with-unibyte-buffer
(&rest forms
)
781 "Create a temporary buffer, and evaluate FORMS there like `progn'.
782 Use unibyte mode for this."
783 `(let (default-enable-multibyte-characters)
784 (with-temp-buffer ,@forms
)))
785 (put 'mm-with-unibyte-buffer
'lisp-indent-function
0)
786 (put 'mm-with-unibyte-buffer
'edebug-form-spec
'(body))
788 (defmacro mm-with-multibyte-buffer
(&rest forms
)
789 "Create a temporary buffer, and evaluate FORMS there like `progn'.
790 Use multibyte mode for this."
791 `(let ((default-enable-multibyte-characters t
))
792 (with-temp-buffer ,@forms
)))
793 (put 'mm-with-multibyte-buffer
'lisp-indent-function
0)
794 (put 'mm-with-multibyte-buffer
'edebug-form-spec
'(body))
796 (defmacro mm-with-unibyte-current-buffer
(&rest forms
)
797 "Evaluate FORMS with current buffer temporarily made unibyte.
798 Also bind `default-enable-multibyte-characters' to nil.
799 Equivalent to `progn' in XEmacs
801 NOTE: Use this macro with caution in multibyte buffers (it is not
802 worth using this macro in unibyte buffers of course). Use of
803 `(set-buffer-multibyte t)', which is run finally, is generally
804 harmful since it is likely to modify existing data in the buffer.
805 For instance, it converts \"\\300\\255\" into \"\\255\" in
807 (let ((multibyte (make-symbol "multibyte"))
808 (buffer (make-symbol "buffer")))
810 (let ((,multibyte enable-multibyte-characters
)
811 (,buffer
(current-buffer)))
813 (let (default-enable-multibyte-characters)
814 (set-buffer-multibyte nil
)
817 (set-buffer-multibyte ,multibyte
)))
818 (let (default-enable-multibyte-characters)
820 (put 'mm-with-unibyte-current-buffer
'lisp-indent-function
0)
821 (put 'mm-with-unibyte-current-buffer
'edebug-form-spec
'(body))
823 (defmacro mm-with-unibyte
(&rest forms
)
824 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
825 `(let (default-enable-multibyte-characters)
827 (put 'mm-with-unibyte
'lisp-indent-function
0)
828 (put 'mm-with-unibyte
'edebug-form-spec
'(body))
830 (defmacro mm-with-multibyte
(&rest forms
)
831 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
832 `(let ((default-enable-multibyte-characters t
))
834 (put 'mm-with-multibyte
'lisp-indent-function
0)
835 (put 'mm-with-multibyte
'edebug-form-spec
'(body))
837 (defun mm-find-charset-region (b e
)
838 "Return a list of Emacs charsets in the region B to E."
840 ((and (mm-multibyte-p)
841 (fboundp 'find-charset-region
))
842 ;; Remove composition since the base charsets have been included.
843 ;; Remove eight-bit-*, treat them as ascii.
844 (let ((css (find-charset-region b e
)))
845 (mapcar (lambda (cs) (setq css
(delq cs css
)))
846 '(composition eight-bit-control eight-bit-graphic
850 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
853 (narrow-to-region b e
)
854 (goto-char (point-min))
855 (skip-chars-forward "\0-\177")
860 (and (boundp 'current-language-environment
)
861 (car (last (assq 'charset
862 (assoc current-language-environment
863 language-info-alist
))))))
864 (if (eq charset
'ascii
) (setq charset nil
))
867 (car (last (assq mail-parse-charset
868 mm-mime-mule-charset-alist
)))))
869 (list 'ascii
(or charset
'latin-iso8859-1
)))))))))
871 (if (fboundp 'shell-quote-argument
)
872 (defalias 'mm-quote-arg
'shell-quote-argument
)
873 (defun mm-quote-arg (arg)
874 "Return a version of ARG that is safe to evaluate in a shell."
875 (let ((pos 0) new-pos accum
)
876 ;; *** bug: we don't handle newline characters properly
877 (while (setq new-pos
(string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos
))
878 (push (substring arg pos new-pos
) accum
)
880 (push (list (aref arg new-pos
)) accum
)
881 (setq pos
(1+ new-pos
)))
884 (apply 'concat
(nconc (nreverse accum
) (list (substring arg pos
))))))))
886 (defun mm-auto-mode-alist ()
887 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
888 (let ((alist auto-mode-alist
)
891 (when (listp (cdar alist
))
892 (push (car alist
) out
))
896 (defvar mm-inhibit-file-name-handlers
897 '(jka-compr-handler image-file-handler
)
898 "A list of handlers doing (un)compression (etc) thingies.")
900 (defun mm-insert-file-contents (filename &optional visit beg end replace
902 "Like `insert-file-contents', but only reads in the file.
903 A buffer may be modified in several ways after reading into the buffer due
904 to advanced Emacs features, such as file-name-handlers, format decoding,
905 `find-file-hooks', etc.
906 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
907 This function ensures that none of these modifications will take place."
908 (let* ((format-alist nil
)
909 (auto-mode-alist (if inhibit nil
(mm-auto-mode-alist)))
910 (default-major-mode 'fundamental-mode
)
911 (enable-local-variables nil
)
912 (after-insert-file-functions nil
)
913 (enable-local-eval nil
)
914 (inhibit-file-name-operation (if inhibit
915 'insert-file-contents
916 inhibit-file-name-operation
))
917 (inhibit-file-name-handlers
919 (append mm-inhibit-file-name-handlers
920 inhibit-file-name-handlers
)
921 inhibit-file-name-handlers
))
922 (ffh (if (boundp 'find-file-hook
)
925 (val (symbol-value ffh
)))
928 (insert-file-contents filename visit beg end replace
)
931 (defun mm-append-to-file (start end filename
&optional codesys inhibit
)
932 "Append the contents of the region to the end of file FILENAME.
933 When called from a function, expects three arguments,
934 START, END and FILENAME. START and END are buffer positions
935 saying what text to write.
936 Optional fourth argument specifies the coding system to use when
938 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
939 (let ((coding-system-for-write
940 (or codesys mm-text-coding-system-for-write
941 mm-text-coding-system
))
942 (inhibit-file-name-operation (if inhibit
944 inhibit-file-name-operation
))
945 (inhibit-file-name-handlers
947 (append mm-inhibit-file-name-handlers
948 inhibit-file-name-handlers
)
949 inhibit-file-name-handlers
)))
950 (write-region start end filename t
'no-message
)
951 (message "Appended to %s" filename
)))
953 (defun mm-write-region (start end filename
&optional append visit lockname
954 coding-system inhibit
)
956 "Like `write-region'.
957 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
958 (let ((coding-system-for-write
959 (or coding-system mm-text-coding-system-for-write
960 mm-text-coding-system
))
961 (inhibit-file-name-operation (if inhibit
963 inhibit-file-name-operation
))
964 (inhibit-file-name-handlers
966 (append mm-inhibit-file-name-handlers
967 inhibit-file-name-handlers
)
968 inhibit-file-name-handlers
)))
969 (write-region start end filename append visit lockname
)))
971 ;; It is not a MIME function, but some MIME functions use it.
972 (if (and (fboundp 'make-temp-file
)
974 (let ((def (symbol-function 'make-temp-file
)))
975 (and (byte-code-function-p def
)
976 (setq def
(if (fboundp 'compiled-function-arglist
)
978 (eval (list 'compiled-function-arglist def
))
981 (eq (nth 3 def
) 'suffix
)))))
982 (defalias 'mm-make-temp-file
'make-temp-file
)
983 ;; Stolen (and modified for Emacs 20 and XEmacs) from Emacs 22.
984 (defun mm-make-temp-file (prefix &optional dir-flag suffix
)
985 "Create a temporary file.
986 The returned file name (created by appending some random characters at the end
987 of PREFIX, and expanding against `temporary-file-directory' if necessary),
988 is guaranteed to point to a newly created empty file.
989 You can then use `write-region' to write new data into the file.
991 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
993 If SUFFIX is non-nil, add that at the end of the file name."
994 (let ((umask (default-file-modes))
998 ;; Create temp files with strict access rights. It's easy to
999 ;; loosen them later, whereas it's impossible to close the
1000 ;; time-window of loose permissions otherwise.
1001 (set-default-file-modes 448)
1002 (while (condition-case err
1008 (if (fboundp 'temp-directory
)
1011 temporary-file-directory
))))
1013 (setq file
(concat file suffix
)))
1015 (make-directory file
)
1016 (if (or (featurep 'xemacs
)
1017 (= emacs-major-version
20))
1018 ;; NOTE: This is unsafe if Emacs 20
1019 ;; users and XEmacs users don't use
1020 ;; a secure temp directory.
1021 (if (file-exists-p file
)
1022 (signal 'file-already-exists
1023 (list "File exists" file
))
1024 (write-region "" nil file nil
'silent
))
1025 (write-region "" nil file nil
'silent
1028 (file-already-exists t
)
1029 ;; The Emacs 20 and XEmacs versions of
1030 ;; `make-directory' issue `file-error'.
1031 (file-error (or (and (or (featurep 'xemacs
)
1032 (= emacs-major-version
20))
1033 (file-exists-p file
))
1034 (signal (car err
) (cdr err
)))))
1035 ;; the file was somehow created by someone else between
1036 ;; `make-temp-name' and `write-region', let's try again.
1040 (set-default-file-modes umask
)))))
1042 (defun mm-image-load-path (&optional package
)
1044 (dolist (path load-path
(nreverse result
))
1047 (setq dir
(concat (file-name-directory
1048 (directory-file-name path
))
1049 "etc/images/" (or package
"gnus/")))))
1051 (push path result
))))
1053 ;; Fixme: This doesn't look useful where it's used.
1054 (if (fboundp 'detect-coding-region
)
1055 (defun mm-detect-coding-region (start end
)
1056 "Like `detect-coding-region' except returning the best one."
1057 (let ((coding-systems
1058 (detect-coding-region start end
)))
1059 (or (car-safe coding-systems
)
1061 (defun mm-detect-coding-region (start end
)
1062 (let ((point (point)))
1064 (skip-chars-forward "\0-\177" end
)
1066 (if (eq (point) end
) 'ascii
(mm-guess-charset))
1067 (goto-char point
)))))
1069 (if (fboundp 'coding-system-get
)
1070 (defun mm-detect-mime-charset-region (start end
)
1071 "Detect MIME charset of the text in the region between START and END."
1072 (let ((cs (mm-detect-coding-region start end
)))
1073 (coding-system-get cs
'mime-charset
)))
1074 (defun mm-detect-mime-charset-region (start end
)
1075 "Detect MIME charset of the text in the region between START and END."
1076 (let ((cs (mm-detect-coding-region start end
)))
1082 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1083 ;;; mm-util.el ends here