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, 2007, 2008 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 3, 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 '((coding-system-list . ignore
)
41 (coding-system-equal . equal
)
42 (annotationp . ignore
)
43 (set-buffer-file-coding-system . ignore
)
45 .
(lambda (charset int
)
53 (mapcar (lambda (e) (list (symbol-name (car e
))))
54 mm-mime-mule-charset-alist
)
57 .
(lambda (from to string
&optional inplace
)
58 ;; stolen (and renamed) from nnheader.el
59 "Replace characters in STRING from FROM to TO.
60 Unless optional argument INPLACE is non-nil, return a new string."
61 (let ((string (if inplace string
(copy-sequence string
)))
64 ;; Replace all occurrences of FROM with TO.
66 (when (= (aref string idx
) from
)
70 (string-as-unibyte . identity
)
71 (string-make-unibyte . identity
)
72 ;; string-as-multibyte often doesn't really do what you think it does.
74 ;; (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
75 ;; (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
76 ;; (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
77 ;; (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
79 ;; (aref (string-as-multibyte "\201\300") 0) -> 2240
80 ;; (aref (string-as-multibyte "\201\300") 1) -> <error>
81 ;; Better use string-to-multibyte or encode-coding-string.
82 ;; If you really need string-as-multibyte somewhere it's usually
83 ;; because you're using the internal emacs-mule representation (maybe
84 ;; because you're using string-as-unibyte somewhere), which is
85 ;; generally a problem in itself.
86 ;; Here is an approximate equivalence table to help think about it:
87 ;; (string-as-multibyte s) ~= (decode-coding-string s 'emacs-mule)
88 ;; (string-to-multibyte s) ~= (decode-coding-string s 'binary)
89 ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
90 (string-as-multibyte . identity
)
91 (multibyte-string-p . ignore
)
92 (insert-byte . insert-char
)
93 (multibyte-char-to-unibyte . identity
))))
96 (if (featurep 'xemacs
)
97 (if (featurep 'file-coding
)
98 ;; Don't modify string if CODING-SYSTEM is nil.
100 (defun mm-decode-coding-string (str coding-system
)
102 (decode-coding-string str coding-system
)
104 (defun mm-encode-coding-string (str coding-system
)
106 (encode-coding-string str coding-system
)
108 (defun mm-decode-coding-region (start end coding-system
)
110 (decode-coding-region start end coding-system
)))
111 (defun mm-encode-coding-region (start end coding-system
)
113 (encode-coding-region start end coding-system
))))
114 (defun mm-decode-coding-string (str coding-system
) str
)
115 (defun mm-encode-coding-string (str coding-system
) str
)
116 (defalias 'mm-decode-coding-region
'ignore
)
117 (defalias 'mm-encode-coding-region
'ignore
))
118 (defalias 'mm-decode-coding-string
'decode-coding-string
)
119 (defalias 'mm-encode-coding-string
'encode-coding-string
)
120 (defalias 'mm-decode-coding-region
'decode-coding-region
)
121 (defalias 'mm-encode-coding-region
'encode-coding-region
)))
125 ((fboundp 'replace-in-string
)
126 (defalias 'mm-replace-in-string
'replace-in-string
))
127 ((fboundp 'replace-regexp-in-string
)
128 (defun mm-replace-in-string (string regexp newtext
&optional literal
)
129 "Replace all matches for REGEXP with NEWTEXT in STRING.
130 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
131 string containing the replacements.
133 This is a compatibility function for different Emacsen."
134 (replace-regexp-in-string regexp newtext string nil literal
)))
136 (defun mm-replace-in-string (string regexp newtext
&optional literal
)
137 "Replace all matches for REGEXP with NEWTEXT in STRING.
138 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
139 string containing the replacements.
141 This is a compatibility function for different Emacsen."
142 (let ((start 0) tail
)
143 (while (string-match regexp string start
)
144 (setq tail
(- (length string
) (match-end 0)))
145 (setq string
(replace-match newtext nil literal string
))
146 (setq start
(- (length string
) tail
))))
149 (defalias 'mm-string-to-multibyte
153 ((fboundp 'string-to-multibyte
)
154 'string-to-multibyte
)
157 "Return a multibyte string with the same individual chars as string."
159 (lambda (ch) (mm-string-as-multibyte (char-to-string ch
)))
163 (defalias 'mm-char-or-char-int-p
165 ((fboundp 'char-or-char-int-p
) 'char-or-char-int-p
)
166 ((fboundp 'char-valid-p
) 'char-valid-p
)
169 ;; Fixme: This seems always to be used to read a MIME charset, so it
170 ;; should be re-named and fixed (in Emacs) to offer completion only on
171 ;; proper charset names (base coding systems which have a
172 ;; mime-charset defined). XEmacs doesn't believe in mime-charset;
174 ;; `(or (coding-system-get 'iso-8859-1 'mime-charset)
175 ;; (coding-system-get 'iso-8859-1 :mime-charset))'
176 ;; Actually, there should be an `mm-coding-system-mime-charset'.
178 (defalias 'mm-read-coding-system
180 ((fboundp 'read-coding-system
)
181 (if (and (featurep 'xemacs
)
182 (<= (string-to-number emacs-version
) 21.1))
183 (lambda (prompt &optional default-coding-system
)
184 (read-coding-system prompt
))
185 'read-coding-system
))
186 (t (lambda (prompt &optional default-coding-system
)
187 "Prompt the user for a coding system."
189 prompt
(mapcar (lambda (s) (list (symbol-name (car s
))))
190 mm-mime-mule-charset-alist
)))))))
192 (defvar mm-coding-system-list nil
)
193 (defun mm-get-coding-system-list ()
194 "Get the coding system list."
195 (or mm-coding-system-list
196 (setq mm-coding-system-list
(mm-coding-system-list))))
198 (defun mm-coding-system-p (cs)
199 "Return non-nil if CS is a symbol naming a coding system.
200 In XEmacs, also return non-nil if CS is a coding system object.
201 If CS is available, return CS itself in Emacs, and return a coding
202 system object in XEmacs."
203 (if (fboundp 'find-coding-system
)
204 (and cs
(find-coding-system cs
))
205 (if (fboundp 'coding-system-p
)
206 (when (coding-system-p cs
)
209 (car (memq cs
(mm-get-coding-system-list))))))
211 (defun mm-codepage-setup (number &optional alias
)
212 "Create a coding system cpNUMBER.
213 The coding system is created using `codepage-setup'. If ALIAS is
214 non-nil, an alias is created and added to
215 `mm-charset-synonym-alist'. If ALIAS is a string, it's used as
216 the alias. Else windows-NUMBER is used."
218 (let ((completion-ignore-case t
)
219 (candidates (cp-supported-codepages)))
220 (list (completing-read "Setup DOS Codepage: (default 437) " candidates
221 nil t nil nil
"437"))))
223 (setq alias
(if (stringp alias
)
225 (intern (format "windows-%s" number
)))))
226 (let* ((cp (intern (format "cp%s" number
))))
227 (unless (mm-coding-system-p cp
)
228 (codepage-setup number
))
230 ;; Don't add alias if setup of cp failed.
231 (mm-coding-system-p cp
))
232 (add-to-list 'mm-charset-synonym-alist
(cons alias cp
)))))
234 (defvar mm-charset-synonym-alist
236 ;; Not in XEmacs, but it's not a proper MIME charset anyhow.
237 ,@(unless (mm-coding-system-p 'x-ctext
)
238 '((x-ctext . ctext
)))
239 ;; ISO-8859-15 is very similar to ISO-8859-1. But it's _different_ in 8
241 ,@(unless (mm-coding-system-p 'iso-8859-15
)
242 '((iso-8859-15 . iso-8859-1
)))
243 ;; BIG-5HKSCS is similar to, but different than, BIG-5.
244 ,@(unless (mm-coding-system-p 'big5-hkscs
)
245 '((big5-hkscs . big5
)))
246 ;; A Microsoft misunderstanding.
247 ,@(when (and (not (mm-coding-system-p 'unicode
))
248 (mm-coding-system-p 'utf-16-le
))
249 '((unicode . utf-16-le
)))
250 ;; A Microsoft misunderstanding.
251 ,@(unless (mm-coding-system-p 'ks_c_5601-1987
)
252 (if (mm-coding-system-p 'cp949
)
253 '((ks_c_5601-1987 . cp949
))
254 '((ks_c_5601-1987 . euc-kr
))))
255 ;; Windows-31J is Windows Codepage 932.
256 ,@(when (and (not (mm-coding-system-p 'windows-31j
))
257 (mm-coding-system-p 'cp932
))
258 '((windows-31j . cp932
)))
259 ;; Charset name: GBK, Charset aliases: CP936, MS936, windows-936
260 ;; http://www.iana.org/assignments/charset-reg/GBK
261 ;; Emacs 22.1 has cp936, but not gbk, so we alias it:
262 ,@(when (and (not (mm-coding-system-p 'gbk
))
263 (mm-coding-system-p 'cp936
))
266 "A mapping from unknown or invalid charset names to the real charset names.
268 See `mm-codepage-iso-8859-list' and `mm-codepage-ibm-list'.")
270 (defcustom mm-codepage-iso-8859-list
271 (list 1250 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
272 ;; Outlook users in Czech republic. Use this to allow reading of
273 ;; their e-mails. cp1250 should be defined by M-x codepage-setup
275 '(1252 .
1) ;; Windows-1252 is a superset of iso-8859-1 (West
276 ;; Europe). See also `gnus-article-dumbquotes-map'.
277 '(1254 .
9) ;; Windows-1254 is a superset of iso-8859-9 (Turkish).
278 '(1255 .
8));; Windows-1255 is a superset of iso-8859-8 (Hebrew).
279 "A list of Windows codepage numbers and iso-8859 charset numbers.
281 If an element is a number corresponding to a supported windows
282 codepage, appropriate entries to `mm-charset-synonym-alist' are
283 added by `mm-setup-codepage-iso-8859'. An element may also be a
284 cons cell where the car is a codepage number and the cdr is the
285 corresponding number of an iso-8859 charset."
286 :type
'(list (set :inline t
287 (const 1250 :tag
"Central and East European")
288 (const (1252 .
1) :tag
"West European")
289 (const (1254 .
9) :tag
"Turkish")
290 (const (1255 .
8) :tag
"Hebrew"))
294 (integer :tag
"Windows codepage number")
295 (cons (integer :tag
"Windows codepage number")
296 (integer :tag
"iso-8859 charset number")))))
297 :version
"22.1" ;; Gnus 5.10.9
300 (defcustom mm-codepage-ibm-list
301 (list 437 ;; (US etc.)
305 863 ;; (Canadian French)
310 866 ;; (Cyrillic - Russian)
315 ;; In Emacs 23 (unicode), cp... and ibm... are aliases.
316 ;; Cf. http://thread.gmane.org/v9lkng5nwy.fsf@marauder.physik.uni-ulm.de
317 "List of IBM codepage numbers.
319 The codepage mappings slighly differ between IBM and other vendors.
320 See \"ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/IBM/README.TXT\".
322 If an element is a number corresponding to a supported windows
323 codepage, appropriate entries to `mm-charset-synonym-alist' are
324 added by `mm-setup-codepage-ibm'."
325 :type
'(list (set :inline t
326 (const 437 :tag
"US etc.")
327 (const 860 :tag
"Portugal")
328 (const 861 :tag
"Iceland")
329 (const 862 :tag
"Israel")
330 (const 863 :tag
"Canadian French")
331 (const 865 :tag
"Nordic")
333 (const 850 :tag
"Latin 1")
334 (const 855 :tag
"Cyrillic")
335 (const 866 :tag
"Cyrillic - Russian")
336 (const 857 :tag
"Turkish")
337 (const 864 :tag
"Arabic")
338 (const 869 :tag
"Greek")
339 (const 874 :tag
"Thai"))
342 (integer :tag
"Codepage number")))
343 :version
"22.1" ;; Gnus 5.10.9
346 (defun mm-setup-codepage-iso-8859 (&optional list
)
347 "Add appropriate entries to `mm-charset-synonym-alist'.
348 Unless LIST is given, `mm-codepage-iso-8859-list' is used."
350 (setq list mm-codepage-iso-8859-list
))
352 (let (cp windows iso
)
354 (setq cp
(intern (format "cp%d" (car i
)))
355 windows
(intern (format "windows-%d" (car i
)))
356 iso
(intern (format "iso-8859-%d" (cdr i
))))
357 (setq cp
(intern (format "cp%d" i
))
358 windows
(intern (format "windows-%d" i
))))
359 (unless (mm-coding-system-p windows
)
360 (if (mm-coding-system-p cp
)
361 (add-to-list 'mm-charset-synonym-alist
(cons windows cp
))
362 (add-to-list 'mm-charset-synonym-alist
(cons windows iso
)))))))
364 (defun mm-setup-codepage-ibm (&optional list
)
365 "Add appropriate entries to `mm-charset-synonym-alist'.
366 Unless LIST is given, `mm-codepage-ibm-list' is used."
368 (setq list mm-codepage-ibm-list
))
369 (dolist (number list
)
370 (let ((ibm (intern (format "ibm%d" number
)))
371 (cp (intern (format "cp%d" number
))))
372 (when (and (not (mm-coding-system-p ibm
))
373 (mm-coding-system-p cp
))
374 (add-to-list 'mm-charset-synonym-alist
(cons ibm cp
))))))
377 (mm-setup-codepage-iso-8859)
378 (mm-setup-codepage-ibm)
380 (defcustom mm-charset-override-alist
381 `((iso-8859-1 . windows-1252
))
382 "A mapping from undesired charset names to their replacement.
384 You may add pairs like (iso-8859-1 . windows-1252) here,
385 i.e. treat iso-8859-1 as windows-1252. windows-1252 is a
386 superset of iso-8859-1."
387 :type
'(list (set :inline t
388 (const (iso-8859-1 . windows-1252
))
389 (const (undecided . windows-1252
)))
392 (cons (symbol :tag
"From charset")
393 (symbol :tag
"To charset"))))
394 :version
"22.1" ;; Gnus 5.10.9
397 (defcustom mm-charset-eval-alist
398 (if (featurep 'xemacs
)
399 nil
;; I don't know what would be useful for XEmacs.
400 '(;; Emacs 21 offers 1250 1251 1253 1257. Emacs 22 provides autoloads for
401 ;; 1250-1258 (i.e. `mm-codepage-setup' does nothing).
402 (windows-1250 .
(mm-codepage-setup 1250 t
))
403 (windows-1251 .
(mm-codepage-setup 1251 t
))
404 (windows-1253 .
(mm-codepage-setup 1253 t
))
405 (windows-1257 .
(mm-codepage-setup 1257 t
))))
406 "An alist of (CHARSET . FORM) pairs.
407 If an article is encoded in an unknown CHARSET, FORM is
408 evaluated. This allows to load additional libraries providing
409 charsets on demand. If supported by your Emacs version, you
410 could use `autoload-coding-system' here."
411 :version
"22.1" ;; Gnus 5.10.9
412 :type
'(list (set :inline t
413 (const (windows-1250 .
(mm-codepage-setup 1250 t
)))
414 (const (windows-1251 .
(mm-codepage-setup 1251 t
)))
415 (const (windows-1253 .
(mm-codepage-setup 1253 t
)))
416 (const (windows-1257 .
(mm-codepage-setup 1257 t
)))
417 (const (cp850 .
(mm-codepage-setup 850 nil
))))
420 (cons (symbol :tag
"charset")
421 (symbol :tag
"form"))))
423 (put 'mm-charset-eval-alist
'risky-local-variable t
)
425 (defvar mm-binary-coding-system
427 ((mm-coding-system-p 'binary
) 'binary
)
428 ((mm-coding-system-p 'no-conversion
) 'no-conversion
)
430 "100% binary coding system.")
432 (defvar mm-text-coding-system
433 (or (if (memq system-type
'(windows-nt ms-dos ms-windows
))
434 (and (mm-coding-system-p 'raw-text-dos
) 'raw-text-dos
)
435 (and (mm-coding-system-p 'raw-text
) 'raw-text
))
436 mm-binary-coding-system
)
437 "Text-safe coding system (For removing ^M).")
439 (defvar mm-text-coding-system-for-write nil
440 "Text coding system for write.")
442 (defvar mm-auto-save-coding-system
444 ((mm-coding-system-p 'utf-8-emacs
) ; Mule 7
445 (if (memq system-type
'(windows-nt ms-dos ms-windows
))
446 (if (mm-coding-system-p 'utf-8-emacs-dos
)
447 'utf-8-emacs-dos mm-binary-coding-system
)
449 ((mm-coding-system-p 'emacs-mule
)
450 (if (memq system-type
'(windows-nt ms-dos ms-windows
))
451 (if (mm-coding-system-p 'emacs-mule-dos
)
452 'emacs-mule-dos mm-binary-coding-system
)
454 ((mm-coding-system-p 'escape-quoted
) 'escape-quoted
)
455 (t mm-binary-coding-system
))
456 "Coding system of auto save file.")
458 (defvar mm-universal-coding-system mm-auto-save-coding-system
459 "The universal coding system.")
461 ;; Fixme: some of the cars here aren't valid MIME charsets. That
462 ;; should only matter with XEmacs, though.
463 (defvar mm-mime-mule-charset-alist
465 (iso-8859-1 latin-iso8859-1
)
466 (iso-8859-2 latin-iso8859-2
)
467 (iso-8859-3 latin-iso8859-3
)
468 (iso-8859-4 latin-iso8859-4
)
469 (iso-8859-5 cyrillic-iso8859-5
)
470 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
471 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
472 ;; charset is koi8-r, not iso-8859-5.
473 (koi8-r cyrillic-iso8859-5 gnus-koi8-r
)
474 (iso-8859-6 arabic-iso8859-6
)
475 (iso-8859-7 greek-iso8859-7
)
476 (iso-8859-8 hebrew-iso8859-8
)
477 (iso-8859-9 latin-iso8859-9
)
478 (iso-8859-14 latin-iso8859-14
)
479 (iso-8859-15 latin-iso8859-15
)
480 (viscii vietnamese-viscii-lower
)
481 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978
)
482 (euc-kr korean-ksc5601
)
483 (gb2312 chinese-gb2312
)
484 (big5 chinese-big5-1 chinese-big5-2
)
486 (thai-tis620 thai-tis620
)
487 (windows-1251 cyrillic-iso8859-5
)
488 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column
)
489 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
490 latin-jisx0201 japanese-jisx0208-1978
491 chinese-gb2312 japanese-jisx0208
492 korean-ksc5601 japanese-jisx0212
)
493 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
494 latin-jisx0201 japanese-jisx0208-1978
495 chinese-gb2312 japanese-jisx0208
496 korean-ksc5601 japanese-jisx0212
497 chinese-cns11643-1 chinese-cns11643-2
)
498 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
499 cyrillic-iso8859-5 greek-iso8859-7
500 latin-jisx0201 japanese-jisx0208-1978
501 chinese-gb2312 japanese-jisx0208
502 korean-ksc5601 japanese-jisx0212
503 chinese-cns11643-1 chinese-cns11643-2
504 chinese-cns11643-3 chinese-cns11643-4
505 chinese-cns11643-5 chinese-cns11643-6
507 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
508 japanese-jisx0213-1 japanese-jisx0213-2
)
509 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208
)
510 ,(cond ((fboundp 'unicode-precedence-list
)
511 (cons 'utf-8
(delq 'ascii
(mapcar 'charset-name
512 (unicode-precedence-list)))))
513 ((or (not (fboundp 'charsetp
)) ;; non-Mule case
514 (charsetp 'unicode-a
)
515 (not (mm-coding-system-p 'mule-utf-8
)))
516 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e
))
517 (t ;; If we have utf-8 we're in Mule 5+.
520 (coding-system-get 'mule-utf-8
'safe-charsets
))))))
521 "Alist of MIME-charset/MULE-charsets.")
523 (defun mm-enrich-utf-8-by-mule-ucs ()
524 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
525 This function will run when the `un-define' module is loaded under
526 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
527 with Mule charsets. It is completely useless for Emacs."
528 (when (boundp 'unicode-basic-translation-charset-order-list
)
534 'unicode-basic-translation-charset-order-list
))))
535 (elem (assq 'utf-8 mm-mime-mule-charset-alist
)))
538 (setq mm-mime-mule-charset-alist
539 (nconc mm-mime-mule-charset-alist
540 (list (cons 'utf-8 val
))))))
543 ;; Correct by construction, but should be unnecessary for Emacs:
544 (if (featurep 'xemacs
)
545 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
546 (when (and (fboundp 'coding-system-list
)
547 (fboundp 'sort-coding-systems
))
548 (let ((css (sort-coding-systems (coding-system-list 'base-only
)))
552 mime
(or (coding-system-get cs
:mime-charset
) ; Emacs 23 (unicode)
553 (coding-system-get cs
'mime-charset
)))
555 (not (eq t
(setq mule
556 (coding-system-get cs
'safe-charsets
))))
557 (not (assq mime alist
)))
558 (push (cons mime
(delq 'ascii mule
)) alist
)))
559 (setq mm-mime-mule-charset-alist
(nreverse alist
)))))
561 (defvar mm-hack-charsets
'(iso-8859-15 iso-2022-jp-2
)
562 "A list of special charsets.
563 Valid elements include:
564 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
565 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
568 (defvar mm-iso-8859-15-compatible
569 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
570 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
571 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
573 (defvar mm-iso-8859-x-to-15-table
574 (and (fboundp 'coding-system-p
)
575 (mm-coding-system-p 'iso-8859-15
)
578 (if (mm-coding-system-p (car cs
))
579 (let ((c (string-to-char
580 (decode-coding-string "\341" (car cs
)))))
581 (cons (char-charset c
)
584 (decode-coding-string "\341" 'iso-8859-15
)) c
)
585 (string-to-list (decode-coding-string (car (cdr cs
))
588 mm-iso-8859-15-compatible
))
589 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
591 (defcustom mm-coding-system-priorities
592 (if (boundp 'current-language-environment
)
593 (let ((lang (symbol-value 'current-language-environment
)))
594 (cond ((string= lang
"Japanese")
595 ;; Japanese users prefer iso-2022-jp to euc-japan or
596 ;; shift_jis, however iso-8859-1 should be used when
597 ;; there are only ASCII text and Latin-1 characters.
598 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8
)))))
599 "Preferred coding systems for encoding outgoing messages.
601 More than one suitable coding system may be found for some text.
602 By default, the coding system with the highest priority is used
603 to encode outgoing messages (see `sort-coding-systems'). If this
604 variable is set, it overrides the default priority."
606 :type
'(repeat (symbol :tag
"Coding system"))
610 (defvar mm-use-find-coding-systems-region
611 (fboundp 'find-coding-systems-region
)
612 "Use `find-coding-systems-region' to find proper coding systems.
614 Setting it to nil is useful on Emacsen supporting Unicode if sending
615 mail with multiple parts is preferred to sending a Unicode one.")
617 ;;; Internal variables:
621 (defun mm-mule-charset-to-mime-charset (charset)
622 "Return the MIME charset corresponding to the given Mule CHARSET."
623 (if (and (fboundp 'find-coding-systems-for-charsets
)
624 (fboundp 'sort-coding-systems
))
625 (let ((css (sort (sort-coding-systems
626 (find-coding-systems-for-charsets (list charset
)))
627 'mm-sort-coding-systems-predicate
))
629 (while (and (not mime
)
631 (when (setq cs
(pop css
))
632 (setq mime
(or (coding-system-get cs
:mime-charset
)
633 (coding-system-get cs
'mime-charset
)))))
635 (let ((alist (mapcar (lambda (cs)
636 (assq cs mm-mime-mule-charset-alist
))
637 (sort (mapcar 'car mm-mime-mule-charset-alist
)
638 'mm-sort-coding-systems-predicate
)))
641 (when (memq charset
(cdar alist
))
642 (setq out
(caar alist
)
647 (defun mm-charset-to-coding-system (charset &optional lbt
649 "Return coding-system corresponding to CHARSET.
650 CHARSET is a symbol naming a MIME charset.
651 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
652 used as the line break code type of the coding system.
654 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
655 map undesired charset names to their replacement. This should
656 only be used for decoding, not for encoding."
657 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
658 (when (stringp charset
)
659 (setq charset
(intern (downcase charset
))))
661 (setq charset
(intern (format "%s-%s" charset lbt
))))
665 ;; Running in a non-MULE environment.
666 ((or (null (mm-get-coding-system-list))
667 (not (fboundp 'coding-system-get
)))
669 ;; Check override list quite early. Should only used for decoding, not for
672 (let ((cs (cdr (assq charset mm-charset-override-alist
))))
673 (and cs
(mm-coding-system-p cs
) cs
))))
675 ((eq charset
'us-ascii
)
677 ;; Check to see whether we can handle this charset. (This depends
678 ;; on there being some coding system matching each `mime-charset'
679 ;; property defined, as there should be.)
680 ((and (mm-coding-system-p charset
)
681 ;;; Doing this would potentially weed out incorrect charsets.
683 ;;; (eq charset (coding-system-get charset 'mime-charset))
686 ;; Eval expressions from `mm-charset-eval-alist'
687 ((let* ((el (assq charset mm-charset-eval-alist
))
694 (condition-case nil
(eval form
) (error nil
))
695 ;; (message "Failed to eval `%s'" form))
696 (mm-coding-system-p cs
)
697 (message "Added charset `%s' via `mm-charset-eval-alist'" cs
))
699 ;; Translate invalid charsets.
700 ((let ((cs (cdr (assq charset mm-charset-synonym-alist
))))
702 (mm-coding-system-p cs
)
704 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
707 ;; Last resort: search the coding system list for entries which
708 ;; have the right mime-charset in case the canonical name isn't
709 ;; defined (though it should be).
711 ;; mm-get-coding-system-list returns a list of cs without lbt.
713 (dolist (c (mm-get-coding-system-list))
715 (eq charset
(or (coding-system-get c
:mime-charset
)
716 (coding-system-get c
'mime-charset
))))
719 ;; Warn the user about unknown charset:
720 (if (fboundp 'gnus-message
)
721 (gnus-message 7 "Unknown charset: %s" charset
)
722 (message "Unknown charset: %s" charset
)))
725 (defsubst mm-replace-chars-in-string
(string from to
)
726 (mm-subst-char-in-string from to string
))
729 (defvar mm-emacs-mule
(and (not (featurep 'xemacs
))
730 (boundp 'default-enable-multibyte-characters
)
731 default-enable-multibyte-characters
732 (fboundp 'set-buffer-multibyte
))
733 "True in Emacs with Mule.")
736 (defun mm-enable-multibyte ()
737 "Set the multibyte flag of the current buffer.
738 Only do this if the default value of `enable-multibyte-characters' is
739 non-nil. This is a no-op in XEmacs."
740 (set-buffer-multibyte 'to
))
741 (defalias 'mm-enable-multibyte
'ignore
))
744 (defun mm-disable-multibyte ()
745 "Unset the multibyte flag of in the current buffer.
746 This is a no-op in XEmacs."
747 (set-buffer-multibyte nil
))
748 (defalias 'mm-disable-multibyte
'ignore
)))
750 (defun mm-preferred-coding-system (charset)
751 ;; A typo in some Emacs versions.
752 (or (get-charset-property charset
'preferred-coding-system
)
753 (get-charset-property charset
'prefered-coding-system
)))
755 ;; Mule charsets shouldn't be used.
756 (defsubst mm-guess-charset
()
757 "Guess Mule charset from the language environment."
759 mail-parse-mule-charset
;; cached mule-charset
761 (setq mail-parse-mule-charset
762 (and (boundp 'current-language-environment
)
765 (assoc current-language-environment
766 language-info-alist
))))))
767 (if (or (not mail-parse-mule-charset
)
768 (eq mail-parse-mule-charset
'ascii
))
769 (setq mail-parse-mule-charset
770 (or (car (last (assq mail-parse-charset
771 mm-mime-mule-charset-alist
)))
774 mail-parse-mule-charset
)))
776 (defun mm-charset-after (&optional pos
)
777 "Return charset of a character in current buffer at position POS.
778 If POS is nil, it defauls to the current point.
779 If POS is out of range, the value is nil.
780 If the charset is `composition', return the actual one."
781 (let ((char (char-after pos
)) charset
)
782 (if (< (mm-char-int char
) 128)
783 (setq charset
'ascii
)
784 ;; charset-after is fake in some Emacsen.
785 (setq charset
(and (fboundp 'char-charset
) (char-charset char
)))
786 (if (eq charset
'composition
) ; Mule 4
787 (let ((p (or pos
(point))))
788 (cadr (find-charset-region p
(1+ p
))))
789 (if (and charset
(not (memq charset
'(ascii eight-bit-control
790 eight-bit-graphic
))))
792 (mm-guess-charset))))))
794 (defun mm-mime-charset (charset)
795 "Return the MIME charset corresponding to the given Mule CHARSET."
796 (if (eq charset
'unknown
)
797 (error "The message contains non-printable characters, please use attachment"))
798 (if (and (fboundp 'coding-system-get
) (fboundp 'get-charset-property
))
799 ;; This exists in Emacs 20.
801 (and (mm-preferred-coding-system charset
)
802 (or (coding-system-get
803 (mm-preferred-coding-system charset
) :mime-charset
)
805 (mm-preferred-coding-system charset
) 'mime-charset
)))
806 (and (eq charset
'ascii
)
808 (mm-preferred-coding-system charset
)
809 (mm-mule-charset-to-mime-charset charset
))
810 ;; This is for XEmacs.
811 (mm-mule-charset-to-mime-charset charset
)))
813 (if (fboundp 'delete-dups
)
814 (defalias 'mm-delete-duplicates
'delete-dups
)
815 (defun mm-delete-duplicates (list)
816 "Destructively remove `equal' duplicates from LIST.
817 Store the result in LIST and return it. LIST must be a proper list.
818 Of several `equal' occurrences of an element in LIST, the first
821 This is a compatibility function for Emacsen without `delete-dups'."
822 ;; Code from `subr.el' in Emacs 22:
825 (setcdr tail
(delete (car tail
) (cdr tail
)))
826 (setq tail
(cdr tail
))))
829 ;; Fixme: This is used in places when it should be testing the
830 ;; default multibyteness. See mm-default-multibyte-p.
832 (if (and (not (featurep 'xemacs
))
833 (boundp 'enable-multibyte-characters
))
834 (defun mm-multibyte-p ()
835 "Non-nil if multibyte is enabled in the current buffer."
836 enable-multibyte-characters
)
837 (defun mm-multibyte-p () (featurep 'mule
))))
839 (defun mm-default-multibyte-p ()
840 "Return non-nil if the session is multibyte.
841 This affects whether coding conversion should be attempted generally."
843 (if (boundp 'default-enable-multibyte-characters
)
844 default-enable-multibyte-characters
847 (defun mm-iso-8859-x-to-15-region (&optional b e
)
848 (if (fboundp 'char-charset
)
849 (let (charset item c inconvertible
)
851 (if e
(narrow-to-region b e
))
852 (goto-char (point-min))
853 (skip-chars-forward "\0-\177")
856 ((not (setq item
(assq (char-charset (setq c
(char-after)))
857 mm-iso-8859-x-to-15-table
)))
859 ((memq c
(cdr (cdr item
)))
860 (setq inconvertible t
)
863 (insert-before-markers (prog1 (+ c
(car (cdr item
)))
865 (skip-chars-forward "\0-\177")))
866 (not inconvertible
))))
868 (defun mm-sort-coding-systems-predicate (a b
)
871 ;; Note: invalid entries are dropped silently
872 (and (setq cs
(mm-coding-system-p cs
))
873 (coding-system-base cs
)))
874 mm-coding-system-priorities
)))
875 (and (setq a
(mm-coding-system-p a
))
876 (if (setq b
(mm-coding-system-p b
))
877 (> (length (memq (coding-system-base a
) priorities
))
878 (length (memq (coding-system-base b
) priorities
)))
882 (autoload 'latin-unity-massage-name
"latin-unity")
883 (autoload 'latin-unity-maybe-remap
"latin-unity")
884 (autoload 'latin-unity-representations-feasible-region
"latin-unity")
885 (autoload 'latin-unity-representations-present-region
"latin-unity")
886 (defvar latin-unity-coding-systems
)
887 (defvar latin-unity-ucs-list
))
889 (defun mm-xemacs-find-mime-charset-1 (begin end
)
890 "Determine which MIME charset to use to send region as message.
891 This uses the XEmacs-specific latin-unity package to better handle the
892 case where identical characters from diverse ISO-8859-? character sets
893 can be encoded using a single one of the corresponding coding systems.
895 It treats `mm-coding-system-priorities' as the list of preferred
896 coding systems; a useful example setting for this list in Western
897 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
898 to the very standard Latin 1 coding system, and only move to coding
899 systems that are less supported as is necessary to encode the
900 characters that exist in the buffer.
902 Latin Unity doesn't know about those non-ASCII Roman characters that
903 are available in various East Asian character sets. As such, its
904 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
905 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
906 But this is very much a corner case, so don't worry about it."
907 (let ((systems mm-coding-system-priorities
) csets psets curset
)
909 ;; Load the Latin Unity library, if available.
910 (when (and (not (featurep 'latin-unity
)) (locate-library "latin-unity"))
911 (ignore-errors (require 'latin-unity
)))
913 ;; Now, can we use it?
914 (if (featurep 'latin-unity
)
916 (setq csets
(latin-unity-representations-feasible-region begin end
)
917 psets
(latin-unity-representations-present-region begin end
))
921 ;; Pass back the first coding system in the preferred list
922 ;; that can encode the whole region.
923 (dolist (curset systems
)
924 (setq curset
(latin-unity-massage-name 'buffer-default curset
))
926 ;; If the coding system is a universal coding system, then
927 ;; it can certainly encode all the characters in the region.
928 (if (memq curset latin-unity-ucs-list
)
929 (throw 'done
(list curset
)))
931 ;; If a coding system isn't universal, and isn't in
932 ;; the list that latin unity knows about, we can't
933 ;; decide whether to use it here. Leave that until later
934 ;; in `mm-find-mime-charset-region' function, whence we
936 (unless (memq curset latin-unity-coding-systems
)
939 ;; Right, we know about this coding system, and it may
940 ;; conceivably be able to encode all the characters in
942 (if (latin-unity-maybe-remap begin end curset csets psets t
)
943 (throw 'done
(list curset
))))
945 ;; Can't encode using anything from the
946 ;; `mm-coding-system-priorities' list.
947 ;; Leave `mm-find-mime-charset' to do most of the work.
950 ;; Right, latin unity isn't available; let `mm-find-charset-region'
951 ;; take its default action, which equally applies to GNU Emacs.
954 (defmacro mm-xemacs-find-mime-charset
(begin end
)
955 (when (featurep 'xemacs
)
956 `(and (featurep 'mule
) (mm-xemacs-find-mime-charset-1 ,begin
,end
))))
958 (defun mm-find-mime-charset-region (b e
&optional hack-charsets
)
959 "Return the MIME charsets needed to encode the region between B and E.
960 nil means ASCII, a single-element list represents an appropriate MIME
961 charset, and a longer list means no appropriate charset."
963 ;; The return possibilities of this function are a mess...
964 (or (and (mm-multibyte-p)
965 mm-use-find-coding-systems-region
966 ;; Find the mime-charset of the most preferred coding
967 ;; system that has one.
968 (let ((systems (find-coding-systems-region b e
)))
969 (when mm-coding-system-priorities
971 (sort systems
'mm-sort-coding-systems-predicate
)))
972 (setq systems
(delq 'compound-text systems
))
973 (unless (equal systems
'(undecided))
975 (let* ((head (pop systems
))
976 (cs (or (coding-system-get head
:mime-charset
)
977 (coding-system-get head
'mime-charset
))))
978 ;; The mime-charset (`x-ctext') of
979 ;; `compound-text' is not in the IANA list. We
980 ;; shouldn't normally use anything here with a
981 ;; mime-charset having an `x-' prefix.
982 ;; Fixme: Allow this to be overridden, since
983 ;; there is existing use of x-ctext.
984 ;; Also people apparently need the coding system
985 ;; `iso-2022-jp-3' (which Mule-UCS defines with
986 ;; mime-charset, though it's not valid).
988 (not (string-match "^[Xx]-" (symbol-name cs
)))
989 ;; UTF-16 of any variety is invalid for
990 ;; text parts and, unfortunately, has
991 ;; mime-charset defined both in Mule-UCS
992 ;; and versions of Emacs. (The name
993 ;; might be `mule-utf-16...' or
995 (not (string-match "utf-16" (symbol-name cs
))))
997 charsets
(list cs
))))))
999 ;; If we're XEmacs, and some coding system is appropriate,
1000 ;; mm-xemacs-find-mime-charset will return an appropriate list.
1001 ;; Otherwise, we'll get nil, and the next setq will get invoked.
1002 (setq charsets
(mm-xemacs-find-mime-charset b e
))
1004 ;; We're not multibyte, or a single coding system won't cover it.
1006 (mm-delete-duplicates
1007 (mapcar 'mm-mime-charset
1009 (mm-find-charset-region b e
))))))
1010 (if (and (> (length charsets
) 1)
1011 (memq 'iso-8859-15 charsets
)
1012 (memq 'iso-8859-15 hack-charsets
)
1013 (save-excursion (mm-iso-8859-x-to-15-region b e
)))
1014 (mapcar (lambda (x) (setq charsets
(delq (car x
) charsets
)))
1015 mm-iso-8859-15-compatible
))
1016 (if (and (memq 'iso-2022-jp-2 charsets
)
1017 (memq 'iso-2022-jp-2 hack-charsets
))
1018 (setq charsets
(delq 'iso-2022-jp charsets
)))
1019 ;; Attempt to reduce the number of charsets if utf-8 is available.
1020 (if (and (featurep 'xemacs
)
1021 (> (length charsets
) 1)
1022 (mm-coding-system-p 'utf-8
))
1023 (let ((mm-coding-system-priorities
1024 (cons 'utf-8 mm-coding-system-priorities
)))
1026 (mm-delete-duplicates
1027 (mapcar 'mm-mime-charset
1029 (mm-find-charset-region b e
)))))))
1032 (defmacro mm-with-unibyte-buffer
(&rest forms
)
1033 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1034 Use unibyte mode for this."
1035 `(let (default-enable-multibyte-characters)
1036 (with-temp-buffer ,@forms
)))
1037 (put 'mm-with-unibyte-buffer
'lisp-indent-function
0)
1038 (put 'mm-with-unibyte-buffer
'edebug-form-spec
'(body))
1040 (defmacro mm-with-multibyte-buffer
(&rest forms
)
1041 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1042 Use multibyte mode for this."
1043 `(let ((default-enable-multibyte-characters t
))
1044 (with-temp-buffer ,@forms
)))
1045 (put 'mm-with-multibyte-buffer
'lisp-indent-function
0)
1046 (put 'mm-with-multibyte-buffer
'edebug-form-spec
'(body))
1048 (defmacro mm-with-unibyte-current-buffer
(&rest forms
)
1049 "Evaluate FORMS with current buffer temporarily made unibyte.
1050 Also bind `default-enable-multibyte-characters' to nil.
1051 Equivalent to `progn' in XEmacs
1053 NOTE: Use this macro with caution in multibyte buffers (it is not
1054 worth using this macro in unibyte buffers of course). Use of
1055 `(set-buffer-multibyte t)', which is run finally, is generally
1056 harmful since it is likely to modify existing data in the buffer.
1057 For instance, it converts \"\\300\\255\" into \"\\255\" in
1058 Emacs 23 (unicode)."
1059 (let ((multibyte (make-symbol "multibyte"))
1060 (buffer (make-symbol "buffer")))
1062 (let ((,multibyte enable-multibyte-characters
)
1063 (,buffer
(current-buffer)))
1065 (let (default-enable-multibyte-characters)
1066 (set-buffer-multibyte nil
)
1068 (set-buffer ,buffer
)
1069 (set-buffer-multibyte ,multibyte
)))
1070 (let (default-enable-multibyte-characters)
1072 (put 'mm-with-unibyte-current-buffer
'lisp-indent-function
0)
1073 (put 'mm-with-unibyte-current-buffer
'edebug-form-spec
'(body))
1075 (defmacro mm-with-unibyte
(&rest forms
)
1076 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
1077 `(let (default-enable-multibyte-characters)
1079 (put 'mm-with-unibyte
'lisp-indent-function
0)
1080 (put 'mm-with-unibyte
'edebug-form-spec
'(body))
1082 (defmacro mm-with-multibyte
(&rest forms
)
1083 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
1084 `(let ((default-enable-multibyte-characters t
))
1086 (put 'mm-with-multibyte
'lisp-indent-function
0)
1087 (put 'mm-with-multibyte
'edebug-form-spec
'(body))
1089 (defun mm-find-charset-region (b e
)
1090 "Return a list of Emacs charsets in the region B to E."
1092 ((and (mm-multibyte-p)
1093 (fboundp 'find-charset-region
))
1094 ;; Remove composition since the base charsets have been included.
1095 ;; Remove eight-bit-*, treat them as ascii.
1096 (let ((css (find-charset-region b e
)))
1097 (mapcar (lambda (cs) (setq css
(delq cs css
)))
1098 '(composition eight-bit-control eight-bit-graphic
1102 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
1105 (narrow-to-region b e
)
1106 (goto-char (point-min))
1107 (skip-chars-forward "\0-\177")
1112 (and (boundp 'current-language-environment
)
1113 (car (last (assq 'charset
1114 (assoc current-language-environment
1115 language-info-alist
))))))
1116 (if (eq charset
'ascii
) (setq charset nil
))
1119 (car (last (assq mail-parse-charset
1120 mm-mime-mule-charset-alist
)))))
1121 (list 'ascii
(or charset
'latin-iso8859-1
)))))))))
1123 (if (fboundp 'shell-quote-argument
)
1124 (defalias 'mm-quote-arg
'shell-quote-argument
)
1125 (defun mm-quote-arg (arg)
1126 "Return a version of ARG that is safe to evaluate in a shell."
1127 (let ((pos 0) new-pos accum
)
1128 ;; *** bug: we don't handle newline characters properly
1129 (while (setq new-pos
(string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos
))
1130 (push (substring arg pos new-pos
) accum
)
1132 (push (list (aref arg new-pos
)) accum
)
1133 (setq pos
(1+ new-pos
)))
1136 (apply 'concat
(nconc (nreverse accum
) (list (substring arg pos
))))))))
1138 (defun mm-auto-mode-alist ()
1139 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1140 (let ((alist auto-mode-alist
)
1143 (when (listp (cdar alist
))
1144 (push (car alist
) out
))
1148 (defvar mm-inhibit-file-name-handlers
1149 '(jka-compr-handler image-file-handler
)
1150 "A list of handlers doing (un)compression (etc) thingies.")
1152 (defun mm-insert-file-contents (filename &optional visit beg end replace
1154 "Like `insert-file-contents', but only reads in the file.
1155 A buffer may be modified in several ways after reading into the buffer due
1156 to advanced Emacs features, such as file-name-handlers, format decoding,
1157 `find-file-hooks', etc.
1158 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
1159 This function ensures that none of these modifications will take place."
1160 (let* ((format-alist nil
)
1161 (auto-mode-alist (if inhibit nil
(mm-auto-mode-alist)))
1162 (default-major-mode 'fundamental-mode
)
1163 (enable-local-variables nil
)
1164 (after-insert-file-functions nil
)
1165 (enable-local-eval nil
)
1166 (inhibit-file-name-operation (if inhibit
1167 'insert-file-contents
1168 inhibit-file-name-operation
))
1169 (inhibit-file-name-handlers
1171 (append mm-inhibit-file-name-handlers
1172 inhibit-file-name-handlers
)
1173 inhibit-file-name-handlers
))
1174 (ffh (if (boundp 'find-file-hook
)
1177 (val (symbol-value ffh
)))
1180 (insert-file-contents filename visit beg end replace
)
1183 (defun mm-append-to-file (start end filename
&optional codesys inhibit
)
1184 "Append the contents of the region to the end of file FILENAME.
1185 When called from a function, expects three arguments,
1186 START, END and FILENAME. START and END are buffer positions
1187 saying what text to write.
1188 Optional fourth argument specifies the coding system to use when
1190 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1191 (let ((coding-system-for-write
1192 (or codesys mm-text-coding-system-for-write
1193 mm-text-coding-system
))
1194 (inhibit-file-name-operation (if inhibit
1196 inhibit-file-name-operation
))
1197 (inhibit-file-name-handlers
1199 (append mm-inhibit-file-name-handlers
1200 inhibit-file-name-handlers
)
1201 inhibit-file-name-handlers
)))
1202 (write-region start end filename t
'no-message
)
1203 (message "Appended to %s" filename
)))
1205 (defun mm-write-region (start end filename
&optional append visit lockname
1206 coding-system inhibit
)
1208 "Like `write-region'.
1209 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1210 (let ((coding-system-for-write
1211 (or coding-system mm-text-coding-system-for-write
1212 mm-text-coding-system
))
1213 (inhibit-file-name-operation (if inhibit
1215 inhibit-file-name-operation
))
1216 (inhibit-file-name-handlers
1218 (append mm-inhibit-file-name-handlers
1219 inhibit-file-name-handlers
)
1220 inhibit-file-name-handlers
)))
1221 (write-region start end filename append visit lockname
)))
1223 ;; It is not a MIME function, but some MIME functions use it.
1224 (if (and (fboundp 'make-temp-file
)
1226 (let ((def (symbol-function 'make-temp-file
)))
1227 (and (byte-code-function-p def
)
1228 (setq def
(if (fboundp 'compiled-function-arglist
)
1230 (eval (list 'compiled-function-arglist def
))
1233 (eq (nth 3 def
) 'suffix
)))))
1234 (defalias 'mm-make-temp-file
'make-temp-file
)
1235 ;; Stolen (and modified for Emacs 20 and XEmacs) from Emacs 22.
1236 (defun mm-make-temp-file (prefix &optional dir-flag suffix
)
1237 "Create a temporary file.
1238 The returned file name (created by appending some random characters at the end
1239 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1240 is guaranteed to point to a newly created empty file.
1241 You can then use `write-region' to write new data into the file.
1243 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1245 If SUFFIX is non-nil, add that at the end of the file name."
1246 (let ((umask (default-file-modes))
1250 ;; Create temp files with strict access rights. It's easy to
1251 ;; loosen them later, whereas it's impossible to close the
1252 ;; time-window of loose permissions otherwise.
1253 (set-default-file-modes 448)
1254 (while (condition-case err
1260 (if (fboundp 'temp-directory
)
1263 temporary-file-directory
))))
1265 (setq file
(concat file suffix
)))
1267 (make-directory file
)
1268 ;; NOTE: This is unsafe if Emacs 20
1269 ;; users and XEmacs users don't use
1270 ;; a secure temp directory.
1271 (gmm-write-region "" nil file nil
'silent
1274 (file-already-exists t
)
1275 ;; The Emacs 20 and XEmacs versions of
1276 ;; `make-directory' issue `file-error'.
1277 (file-error (or (and (or (featurep 'xemacs
)
1278 (= emacs-major-version
20))
1279 (file-exists-p file
))
1280 (signal (car err
) (cdr err
)))))
1281 ;; the file was somehow created by someone else between
1282 ;; `make-temp-name' and `write-region', let's try again.
1286 (set-default-file-modes umask
)))))
1288 (defun mm-image-load-path (&optional package
)
1290 (dolist (path load-path
(nreverse result
))
1293 (setq dir
(concat (file-name-directory
1294 (directory-file-name path
))
1295 "etc/images/" (or package
"gnus/")))))
1297 (push path result
))))
1299 ;; Fixme: This doesn't look useful where it's used.
1300 (if (fboundp 'detect-coding-region
)
1301 (defun mm-detect-coding-region (start end
)
1302 "Like `detect-coding-region' except returning the best one."
1303 (let ((coding-systems
1304 (detect-coding-region start end
)))
1305 (or (car-safe coding-systems
)
1307 (defun mm-detect-coding-region (start end
)
1308 (let ((point (point)))
1310 (skip-chars-forward "\0-\177" end
)
1312 (if (eq (point) end
) 'ascii
(mm-guess-charset))
1313 (goto-char point
)))))
1315 (if (fboundp 'coding-system-get
)
1316 (defun mm-detect-mime-charset-region (start end
)
1317 "Detect MIME charset of the text in the region between START and END."
1318 (let ((cs (mm-detect-coding-region start end
)))
1319 (or (coding-system-get cs
:mime-charset
)
1320 (coding-system-get cs
'mime-charset
))))
1321 (defun mm-detect-mime-charset-region (start end
)
1322 "Detect MIME charset of the text in the region between START and END."
1323 (let ((cs (mm-detect-coding-region start end
)))
1329 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1330 ;;; mm-util.el ends here