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 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"))))
424 (defvar mm-binary-coding-system
426 ((mm-coding-system-p 'binary
) 'binary
)
427 ((mm-coding-system-p 'no-conversion
) 'no-conversion
)
429 "100% binary coding system.")
431 (defvar mm-text-coding-system
432 (or (if (memq system-type
'(windows-nt ms-dos ms-windows
))
433 (and (mm-coding-system-p 'raw-text-dos
) 'raw-text-dos
)
434 (and (mm-coding-system-p 'raw-text
) 'raw-text
))
435 mm-binary-coding-system
)
436 "Text-safe coding system (For removing ^M).")
438 (defvar mm-text-coding-system-for-write nil
439 "Text coding system for write.")
441 (defvar mm-auto-save-coding-system
443 ((mm-coding-system-p 'utf-8-emacs
) ; Mule 7
444 (if (memq system-type
'(windows-nt ms-dos ms-windows
))
445 (if (mm-coding-system-p 'utf-8-emacs-dos
)
446 'utf-8-emacs-dos mm-binary-coding-system
)
448 ((mm-coding-system-p 'emacs-mule
)
449 (if (memq system-type
'(windows-nt ms-dos ms-windows
))
450 (if (mm-coding-system-p 'emacs-mule-dos
)
451 'emacs-mule-dos mm-binary-coding-system
)
453 ((mm-coding-system-p 'escape-quoted
) 'escape-quoted
)
454 (t mm-binary-coding-system
))
455 "Coding system of auto save file.")
457 (defvar mm-universal-coding-system mm-auto-save-coding-system
458 "The universal coding system.")
460 ;; Fixme: some of the cars here aren't valid MIME charsets. That
461 ;; should only matter with XEmacs, though.
462 (defvar mm-mime-mule-charset-alist
464 (iso-8859-1 latin-iso8859-1
)
465 (iso-8859-2 latin-iso8859-2
)
466 (iso-8859-3 latin-iso8859-3
)
467 (iso-8859-4 latin-iso8859-4
)
468 (iso-8859-5 cyrillic-iso8859-5
)
469 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
470 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
471 ;; charset is koi8-r, not iso-8859-5.
472 (koi8-r cyrillic-iso8859-5 gnus-koi8-r
)
473 (iso-8859-6 arabic-iso8859-6
)
474 (iso-8859-7 greek-iso8859-7
)
475 (iso-8859-8 hebrew-iso8859-8
)
476 (iso-8859-9 latin-iso8859-9
)
477 (iso-8859-14 latin-iso8859-14
)
478 (iso-8859-15 latin-iso8859-15
)
479 (viscii vietnamese-viscii-lower
)
480 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978
)
481 (euc-kr korean-ksc5601
)
482 (gb2312 chinese-gb2312
)
483 (big5 chinese-big5-1 chinese-big5-2
)
485 (thai-tis620 thai-tis620
)
486 (windows-1251 cyrillic-iso8859-5
)
487 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column
)
488 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
489 latin-jisx0201 japanese-jisx0208-1978
490 chinese-gb2312 japanese-jisx0208
491 korean-ksc5601 japanese-jisx0212
)
492 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
493 latin-jisx0201 japanese-jisx0208-1978
494 chinese-gb2312 japanese-jisx0208
495 korean-ksc5601 japanese-jisx0212
496 chinese-cns11643-1 chinese-cns11643-2
)
497 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
498 cyrillic-iso8859-5 greek-iso8859-7
499 latin-jisx0201 japanese-jisx0208-1978
500 chinese-gb2312 japanese-jisx0208
501 korean-ksc5601 japanese-jisx0212
502 chinese-cns11643-1 chinese-cns11643-2
503 chinese-cns11643-3 chinese-cns11643-4
504 chinese-cns11643-5 chinese-cns11643-6
506 (iso-2022-jp-3 latin-jisx0201 japanese-jisx0208-1978 japanese-jisx0208
507 japanese-jisx0213-1 japanese-jisx0213-2
)
508 (shift_jis latin-jisx0201 katakana-jisx0201 japanese-jisx0208
)
509 ,(cond ((fboundp 'unicode-precedence-list
)
510 (cons 'utf-8
(delq 'ascii
(mapcar 'charset-name
511 (unicode-precedence-list)))))
512 ((or (not (fboundp 'charsetp
)) ;; non-Mule case
513 (charsetp 'unicode-a
)
514 (not (mm-coding-system-p 'mule-utf-8
)))
515 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e
))
516 (t ;; If we have utf-8 we're in Mule 5+.
519 (coding-system-get 'mule-utf-8
'safe-charsets
))))))
520 "Alist of MIME-charset/MULE-charsets.")
522 (defun mm-enrich-utf-8-by-mule-ucs ()
523 "Make the `utf-8' MIME charset usable by the Mule-UCS package.
524 This function will run when the `un-define' module is loaded under
525 XEmacs, and fill the `utf-8' entry in `mm-mime-mule-charset-alist'
526 with Mule charsets. It is completely useless for Emacs."
527 (when (boundp 'unicode-basic-translation-charset-order-list
)
533 'unicode-basic-translation-charset-order-list
))))
534 (elem (assq 'utf-8 mm-mime-mule-charset-alist
)))
537 (setq mm-mime-mule-charset-alist
538 (nconc mm-mime-mule-charset-alist
539 (list (cons 'utf-8 val
))))))
542 ;; Correct by construction, but should be unnecessary for Emacs:
543 (if (featurep 'xemacs
)
544 (eval-after-load "un-define" '(mm-enrich-utf-8-by-mule-ucs))
545 (when (and (fboundp 'coding-system-list
)
546 (fboundp 'sort-coding-systems
))
547 (let ((css (sort-coding-systems (coding-system-list 'base-only
)))
551 mime
(or (coding-system-get cs
:mime-charset
) ; Emacs 23 (unicode)
552 (coding-system-get cs
'mime-charset
)))
554 (not (eq t
(setq mule
555 (coding-system-get cs
'safe-charsets
))))
556 (not (assq mime alist
)))
557 (push (cons mime
(delq 'ascii mule
)) alist
)))
558 (setq mm-mime-mule-charset-alist
(nreverse alist
)))))
560 (defvar mm-hack-charsets
'(iso-8859-15 iso-2022-jp-2
)
561 "A list of special charsets.
562 Valid elements include:
563 `iso-8859-15' convert ISO-8859-1, -9 to ISO-8859-15 if ISO-8859-15 exists.
564 `iso-2022-jp-2' convert ISO-2022-jp to ISO-2022-jp-2 if ISO-2022-jp-2 exists."
567 (defvar mm-iso-8859-15-compatible
568 '((iso-8859-1 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE")
569 (iso-8859-9 "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xD0\xDD\xDE\xF0\xFD\xFE"))
570 "ISO-8859-15 exchangeable coding systems and inconvertible characters.")
572 (defvar mm-iso-8859-x-to-15-table
573 (and (fboundp 'coding-system-p
)
574 (mm-coding-system-p 'iso-8859-15
)
577 (if (mm-coding-system-p (car cs
))
578 (let ((c (string-to-char
579 (decode-coding-string "\341" (car cs
)))))
580 (cons (char-charset c
)
583 (decode-coding-string "\341" 'iso-8859-15
)) c
)
584 (string-to-list (decode-coding-string (car (cdr cs
))
587 mm-iso-8859-15-compatible
))
588 "A table of the difference character between ISO-8859-X and ISO-8859-15.")
590 (defcustom mm-coding-system-priorities
591 (if (boundp 'current-language-environment
)
592 (let ((lang (symbol-value 'current-language-environment
)))
593 (cond ((string= lang
"Japanese")
594 ;; Japanese users prefer iso-2022-jp to euc-japan or
595 ;; shift_jis, however iso-8859-1 should be used when
596 ;; there are only ASCII text and Latin-1 characters.
597 '(iso-8859-1 iso-2022-jp iso-2022-jp-2 shift_jis utf-8
)))))
598 "Preferred coding systems for encoding outgoing messages.
600 More than one suitable coding system may be found for some text.
601 By default, the coding system with the highest priority is used
602 to encode outgoing messages (see `sort-coding-systems'). If this
603 variable is set, it overrides the default priority."
605 :type
'(repeat (symbol :tag
"Coding system"))
609 (defvar mm-use-find-coding-systems-region
610 (fboundp 'find-coding-systems-region
)
611 "Use `find-coding-systems-region' to find proper coding systems.
613 Setting it to nil is useful on Emacsen supporting Unicode if sending
614 mail with multiple parts is preferred to sending a Unicode one.")
616 ;;; Internal variables:
620 (defun mm-mule-charset-to-mime-charset (charset)
621 "Return the MIME charset corresponding to the given Mule CHARSET."
622 (if (and (fboundp 'find-coding-systems-for-charsets
)
623 (fboundp 'sort-coding-systems
))
624 (let ((css (sort (sort-coding-systems
625 (find-coding-systems-for-charsets (list charset
)))
626 'mm-sort-coding-systems-predicate
))
628 (while (and (not mime
)
630 (when (setq cs
(pop css
))
631 (setq mime
(or (coding-system-get cs
:mime-charset
)
632 (coding-system-get cs
'mime-charset
)))))
634 (let ((alist (mapcar (lambda (cs)
635 (assq cs mm-mime-mule-charset-alist
))
636 (sort (mapcar 'car mm-mime-mule-charset-alist
)
637 'mm-sort-coding-systems-predicate
)))
640 (when (memq charset
(cdar alist
))
641 (setq out
(caar alist
)
646 (defun mm-charset-to-coding-system (charset &optional lbt
648 "Return coding-system corresponding to CHARSET.
649 CHARSET is a symbol naming a MIME charset.
650 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
651 used as the line break code type of the coding system.
653 If ALLOW-OVERRIDE is given, use `mm-charset-override-alist' to
654 map undesired charset names to their replacement. This should
655 only be used for decoding, not for encoding."
656 ;; OVERRIDE is used (only) in `mm-decode-body' and `mm-decode-string'.
657 (when (stringp charset
)
658 (setq charset
(intern (downcase charset
))))
660 (setq charset
(intern (format "%s-%s" charset lbt
))))
664 ;; Running in a non-MULE environment.
665 ((or (null (mm-get-coding-system-list))
666 (not (fboundp 'coding-system-get
)))
668 ;; Check override list quite early. Should only used for decoding, not for
671 (let ((cs (cdr (assq charset mm-charset-override-alist
))))
672 (and cs
(mm-coding-system-p cs
) cs
))))
674 ((eq charset
'us-ascii
)
676 ;; Check to see whether we can handle this charset. (This depends
677 ;; on there being some coding system matching each `mime-charset'
678 ;; property defined, as there should be.)
679 ((and (mm-coding-system-p charset
)
680 ;;; Doing this would potentially weed out incorrect charsets.
682 ;;; (eq charset (coding-system-get charset 'mime-charset))
685 ;; Eval expressions from `mm-charset-eval-alist'
686 ((let* ((el (assq charset mm-charset-eval-alist
))
693 (condition-case nil
(eval form
) (error nil
))
694 ;; (message "Failed to eval `%s'" form))
695 (mm-coding-system-p cs
)
696 (message "Added charset `%s' via `mm-charset-eval-alist'" cs
))
698 ;; Translate invalid charsets.
699 ((let ((cs (cdr (assq charset mm-charset-synonym-alist
))))
701 (mm-coding-system-p cs
)
703 ;; "Using synonym `%s' from `mm-charset-synonym-alist' for `%s'"
706 ;; Last resort: search the coding system list for entries which
707 ;; have the right mime-charset in case the canonical name isn't
708 ;; defined (though it should be).
710 ;; mm-get-coding-system-list returns a list of cs without lbt.
712 (dolist (c (mm-get-coding-system-list))
714 (eq charset
(or (coding-system-get c
:mime-charset
)
715 (coding-system-get c
'mime-charset
))))
718 ;; Warn the user about unknown charset:
719 (if (fboundp 'gnus-message
)
720 (gnus-message 7 "Unknown charset: %s" charset
)
721 (message "Unknown charset: %s" charset
)))
724 (defsubst mm-replace-chars-in-string
(string from to
)
725 (mm-subst-char-in-string from to string
))
728 (defvar mm-emacs-mule
(and (not (featurep 'xemacs
))
729 (boundp 'default-enable-multibyte-characters
)
730 default-enable-multibyte-characters
731 (fboundp 'set-buffer-multibyte
))
732 "True in Emacs with Mule.")
735 (defun mm-enable-multibyte ()
736 "Set the multibyte flag of the current buffer.
737 Only do this if the default value of `enable-multibyte-characters' is
738 non-nil. This is a no-op in XEmacs."
739 (set-buffer-multibyte 'to
))
740 (defalias 'mm-enable-multibyte
'ignore
))
743 (defun mm-disable-multibyte ()
744 "Unset the multibyte flag of in the current buffer.
745 This is a no-op in XEmacs."
746 (set-buffer-multibyte nil
))
747 (defalias 'mm-disable-multibyte
'ignore
)))
749 (defun mm-preferred-coding-system (charset)
750 ;; A typo in some Emacs versions.
751 (or (get-charset-property charset
'preferred-coding-system
)
752 (get-charset-property charset
'prefered-coding-system
)))
754 ;; Mule charsets shouldn't be used.
755 (defsubst mm-guess-charset
()
756 "Guess Mule charset from the language environment."
758 mail-parse-mule-charset
;; cached mule-charset
760 (setq mail-parse-mule-charset
761 (and (boundp 'current-language-environment
)
764 (assoc current-language-environment
765 language-info-alist
))))))
766 (if (or (not mail-parse-mule-charset
)
767 (eq mail-parse-mule-charset
'ascii
))
768 (setq mail-parse-mule-charset
769 (or (car (last (assq mail-parse-charset
770 mm-mime-mule-charset-alist
)))
773 mail-parse-mule-charset
)))
775 (defun mm-charset-after (&optional pos
)
776 "Return charset of a character in current buffer at position POS.
777 If POS is nil, it defauls to the current point.
778 If POS is out of range, the value is nil.
779 If the charset is `composition', return the actual one."
780 (let ((char (char-after pos
)) charset
)
781 (if (< (mm-char-int char
) 128)
782 (setq charset
'ascii
)
783 ;; charset-after is fake in some Emacsen.
784 (setq charset
(and (fboundp 'char-charset
) (char-charset char
)))
785 (if (eq charset
'composition
) ; Mule 4
786 (let ((p (or pos
(point))))
787 (cadr (find-charset-region p
(1+ p
))))
788 (if (and charset
(not (memq charset
'(ascii eight-bit-control
789 eight-bit-graphic
))))
791 (mm-guess-charset))))))
793 (defun mm-mime-charset (charset)
794 "Return the MIME charset corresponding to the given Mule CHARSET."
795 (if (eq charset
'unknown
)
796 (error "The message contains non-printable characters, please use attachment"))
797 (if (and (fboundp 'coding-system-get
) (fboundp 'get-charset-property
))
798 ;; This exists in Emacs 20.
800 (and (mm-preferred-coding-system charset
)
801 (or (coding-system-get
802 (mm-preferred-coding-system charset
) :mime-charset
)
804 (mm-preferred-coding-system charset
) 'mime-charset
)))
805 (and (eq charset
'ascii
)
807 (mm-preferred-coding-system charset
)
808 (mm-mule-charset-to-mime-charset charset
))
809 ;; This is for XEmacs.
810 (mm-mule-charset-to-mime-charset charset
)))
812 (if (fboundp 'delete-dups
)
813 (defalias 'mm-delete-duplicates
'delete-dups
)
814 (defun mm-delete-duplicates (list)
815 "Destructively remove `equal' duplicates from LIST.
816 Store the result in LIST and return it. LIST must be a proper list.
817 Of several `equal' occurrences of an element in LIST, the first
820 This is a compatibility function for Emacsen without `delete-dups'."
821 ;; Code from `subr.el' in Emacs 22:
824 (setcdr tail
(delete (car tail
) (cdr tail
)))
825 (setq tail
(cdr tail
))))
828 ;; Fixme: This is used in places when it should be testing the
829 ;; default multibyteness. See mm-default-multibyte-p.
831 (if (and (not (featurep 'xemacs
))
832 (boundp 'enable-multibyte-characters
))
833 (defun mm-multibyte-p ()
834 "Non-nil if multibyte is enabled in the current buffer."
835 enable-multibyte-characters
)
836 (defun mm-multibyte-p () (featurep 'mule
))))
838 (defun mm-default-multibyte-p ()
839 "Return non-nil if the session is multibyte.
840 This affects whether coding conversion should be attempted generally."
842 (if (boundp 'default-enable-multibyte-characters
)
843 default-enable-multibyte-characters
846 (defun mm-iso-8859-x-to-15-region (&optional b e
)
847 (if (fboundp 'char-charset
)
848 (let (charset item c inconvertible
)
850 (if e
(narrow-to-region b e
))
851 (goto-char (point-min))
852 (skip-chars-forward "\0-\177")
855 ((not (setq item
(assq (char-charset (setq c
(char-after)))
856 mm-iso-8859-x-to-15-table
)))
858 ((memq c
(cdr (cdr item
)))
859 (setq inconvertible t
)
862 (insert-before-markers (prog1 (+ c
(car (cdr item
)))
864 (skip-chars-forward "\0-\177")))
865 (not inconvertible
))))
867 (defun mm-sort-coding-systems-predicate (a b
)
870 ;; Note: invalid entries are dropped silently
871 (and (setq cs
(mm-coding-system-p cs
))
872 (coding-system-base cs
)))
873 mm-coding-system-priorities
)))
874 (and (setq a
(mm-coding-system-p a
))
875 (if (setq b
(mm-coding-system-p b
))
876 (> (length (memq (coding-system-base a
) priorities
))
877 (length (memq (coding-system-base b
) priorities
)))
881 (autoload 'latin-unity-massage-name
"latin-unity")
882 (autoload 'latin-unity-maybe-remap
"latin-unity")
883 (autoload 'latin-unity-representations-feasible-region
"latin-unity")
884 (autoload 'latin-unity-representations-present-region
"latin-unity")
885 (defvar latin-unity-coding-systems
)
886 (defvar latin-unity-ucs-list
))
888 (defun mm-xemacs-find-mime-charset-1 (begin end
)
889 "Determine which MIME charset to use to send region as message.
890 This uses the XEmacs-specific latin-unity package to better handle the
891 case where identical characters from diverse ISO-8859-? character sets
892 can be encoded using a single one of the corresponding coding systems.
894 It treats `mm-coding-system-priorities' as the list of preferred
895 coding systems; a useful example setting for this list in Western
896 Europe would be '(iso-8859-1 iso-8859-15 utf-8), which would default
897 to the very standard Latin 1 coding system, and only move to coding
898 systems that are less supported as is necessary to encode the
899 characters that exist in the buffer.
901 Latin Unity doesn't know about those non-ASCII Roman characters that
902 are available in various East Asian character sets. As such, its
903 behavior if you have a JIS 0212 LATIN SMALL LETTER A WITH ACUTE in a
904 buffer and it can otherwise be encoded as Latin 1, won't be ideal.
905 But this is very much a corner case, so don't worry about it."
906 (let ((systems mm-coding-system-priorities
) csets psets curset
)
908 ;; Load the Latin Unity library, if available.
909 (when (and (not (featurep 'latin-unity
)) (locate-library "latin-unity"))
910 (ignore-errors (require 'latin-unity
)))
912 ;; Now, can we use it?
913 (if (featurep 'latin-unity
)
915 (setq csets
(latin-unity-representations-feasible-region begin end
)
916 psets
(latin-unity-representations-present-region begin end
))
920 ;; Pass back the first coding system in the preferred list
921 ;; that can encode the whole region.
922 (dolist (curset systems
)
923 (setq curset
(latin-unity-massage-name 'buffer-default curset
))
925 ;; If the coding system is a universal coding system, then
926 ;; it can certainly encode all the characters in the region.
927 (if (memq curset latin-unity-ucs-list
)
928 (throw 'done
(list curset
)))
930 ;; If a coding system isn't universal, and isn't in
931 ;; the list that latin unity knows about, we can't
932 ;; decide whether to use it here. Leave that until later
933 ;; in `mm-find-mime-charset-region' function, whence we
935 (unless (memq curset latin-unity-coding-systems
)
938 ;; Right, we know about this coding system, and it may
939 ;; conceivably be able to encode all the characters in
941 (if (latin-unity-maybe-remap begin end curset csets psets t
)
942 (throw 'done
(list curset
))))
944 ;; Can't encode using anything from the
945 ;; `mm-coding-system-priorities' list.
946 ;; Leave `mm-find-mime-charset' to do most of the work.
949 ;; Right, latin unity isn't available; let `mm-find-charset-region'
950 ;; take its default action, which equally applies to GNU Emacs.
953 (defmacro mm-xemacs-find-mime-charset
(begin end
)
954 (when (featurep 'xemacs
)
955 `(and (featurep 'mule
) (mm-xemacs-find-mime-charset-1 ,begin
,end
))))
957 (defun mm-find-mime-charset-region (b e
&optional hack-charsets
)
958 "Return the MIME charsets needed to encode the region between B and E.
959 nil means ASCII, a single-element list represents an appropriate MIME
960 charset, and a longer list means no appropriate charset."
962 ;; The return possibilities of this function are a mess...
963 (or (and (mm-multibyte-p)
964 mm-use-find-coding-systems-region
965 ;; Find the mime-charset of the most preferred coding
966 ;; system that has one.
967 (let ((systems (find-coding-systems-region b e
)))
968 (when mm-coding-system-priorities
970 (sort systems
'mm-sort-coding-systems-predicate
)))
971 (setq systems
(delq 'compound-text systems
))
972 (unless (equal systems
'(undecided))
974 (let* ((head (pop systems
))
975 (cs (or (coding-system-get head
:mime-charset
)
976 (coding-system-get head
'mime-charset
))))
977 ;; The mime-charset (`x-ctext') of
978 ;; `compound-text' is not in the IANA list. We
979 ;; shouldn't normally use anything here with a
980 ;; mime-charset having an `x-' prefix.
981 ;; Fixme: Allow this to be overridden, since
982 ;; there is existing use of x-ctext.
983 ;; Also people apparently need the coding system
984 ;; `iso-2022-jp-3' (which Mule-UCS defines with
985 ;; mime-charset, though it's not valid).
987 (not (string-match "^[Xx]-" (symbol-name cs
)))
988 ;; UTF-16 of any variety is invalid for
989 ;; text parts and, unfortunately, has
990 ;; mime-charset defined both in Mule-UCS
991 ;; and versions of Emacs. (The name
992 ;; might be `mule-utf-16...' or
994 (not (string-match "utf-16" (symbol-name cs
))))
996 charsets
(list cs
))))))
998 ;; If we're XEmacs, and some coding system is appropriate,
999 ;; mm-xemacs-find-mime-charset will return an appropriate list.
1000 ;; Otherwise, we'll get nil, and the next setq will get invoked.
1001 (setq charsets
(mm-xemacs-find-mime-charset b e
))
1003 ;; We're not multibyte, or a single coding system won't cover it.
1005 (mm-delete-duplicates
1006 (mapcar 'mm-mime-charset
1008 (mm-find-charset-region b e
))))))
1009 (if (and (> (length charsets
) 1)
1010 (memq 'iso-8859-15 charsets
)
1011 (memq 'iso-8859-15 hack-charsets
)
1012 (save-excursion (mm-iso-8859-x-to-15-region b e
)))
1013 (mapcar (lambda (x) (setq charsets
(delq (car x
) charsets
)))
1014 mm-iso-8859-15-compatible
))
1015 (if (and (memq 'iso-2022-jp-2 charsets
)
1016 (memq 'iso-2022-jp-2 hack-charsets
))
1017 (setq charsets
(delq 'iso-2022-jp charsets
)))
1018 ;; Attempt to reduce the number of charsets if utf-8 is available.
1019 (if (and (featurep 'xemacs
)
1020 (> (length charsets
) 1)
1021 (mm-coding-system-p 'utf-8
))
1022 (let ((mm-coding-system-priorities
1023 (cons 'utf-8 mm-coding-system-priorities
)))
1025 (mm-delete-duplicates
1026 (mapcar 'mm-mime-charset
1028 (mm-find-charset-region b e
)))))))
1031 (defmacro mm-with-unibyte-buffer
(&rest forms
)
1032 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1033 Use unibyte mode for this."
1034 `(let (default-enable-multibyte-characters)
1035 (with-temp-buffer ,@forms
)))
1036 (put 'mm-with-unibyte-buffer
'lisp-indent-function
0)
1037 (put 'mm-with-unibyte-buffer
'edebug-form-spec
'(body))
1039 (defmacro mm-with-multibyte-buffer
(&rest forms
)
1040 "Create a temporary buffer, and evaluate FORMS there like `progn'.
1041 Use multibyte mode for this."
1042 `(let ((default-enable-multibyte-characters t
))
1043 (with-temp-buffer ,@forms
)))
1044 (put 'mm-with-multibyte-buffer
'lisp-indent-function
0)
1045 (put 'mm-with-multibyte-buffer
'edebug-form-spec
'(body))
1047 (defmacro mm-with-unibyte-current-buffer
(&rest forms
)
1048 "Evaluate FORMS with current buffer temporarily made unibyte.
1049 Also bind `default-enable-multibyte-characters' to nil.
1050 Equivalent to `progn' in XEmacs
1052 NOTE: Use this macro with caution in multibyte buffers (it is not
1053 worth using this macro in unibyte buffers of course). Use of
1054 `(set-buffer-multibyte t)', which is run finally, is generally
1055 harmful since it is likely to modify existing data in the buffer.
1056 For instance, it converts \"\\300\\255\" into \"\\255\" in
1057 Emacs 23 (unicode)."
1058 (let ((multibyte (make-symbol "multibyte"))
1059 (buffer (make-symbol "buffer")))
1061 (let ((,multibyte enable-multibyte-characters
)
1062 (,buffer
(current-buffer)))
1064 (let (default-enable-multibyte-characters)
1065 (set-buffer-multibyte nil
)
1067 (set-buffer ,buffer
)
1068 (set-buffer-multibyte ,multibyte
)))
1069 (let (default-enable-multibyte-characters)
1071 (put 'mm-with-unibyte-current-buffer
'lisp-indent-function
0)
1072 (put 'mm-with-unibyte-current-buffer
'edebug-form-spec
'(body))
1074 (defmacro mm-with-unibyte
(&rest forms
)
1075 "Eval the FORMS with the default value of `enable-multibyte-characters' nil."
1076 `(let (default-enable-multibyte-characters)
1078 (put 'mm-with-unibyte
'lisp-indent-function
0)
1079 (put 'mm-with-unibyte
'edebug-form-spec
'(body))
1081 (defmacro mm-with-multibyte
(&rest forms
)
1082 "Eval the FORMS with the default value of `enable-multibyte-characters' t."
1083 `(let ((default-enable-multibyte-characters t
))
1085 (put 'mm-with-multibyte
'lisp-indent-function
0)
1086 (put 'mm-with-multibyte
'edebug-form-spec
'(body))
1088 (defun mm-find-charset-region (b e
)
1089 "Return a list of Emacs charsets in the region B to E."
1091 ((and (mm-multibyte-p)
1092 (fboundp 'find-charset-region
))
1093 ;; Remove composition since the base charsets have been included.
1094 ;; Remove eight-bit-*, treat them as ascii.
1095 (let ((css (find-charset-region b e
)))
1096 (mapcar (lambda (cs) (setq css
(delq cs css
)))
1097 '(composition eight-bit-control eight-bit-graphic
1101 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
1104 (narrow-to-region b e
)
1105 (goto-char (point-min))
1106 (skip-chars-forward "\0-\177")
1111 (and (boundp 'current-language-environment
)
1112 (car (last (assq 'charset
1113 (assoc current-language-environment
1114 language-info-alist
))))))
1115 (if (eq charset
'ascii
) (setq charset nil
))
1118 (car (last (assq mail-parse-charset
1119 mm-mime-mule-charset-alist
)))))
1120 (list 'ascii
(or charset
'latin-iso8859-1
)))))))))
1122 (if (fboundp 'shell-quote-argument
)
1123 (defalias 'mm-quote-arg
'shell-quote-argument
)
1124 (defun mm-quote-arg (arg)
1125 "Return a version of ARG that is safe to evaluate in a shell."
1126 (let ((pos 0) new-pos accum
)
1127 ;; *** bug: we don't handle newline characters properly
1128 (while (setq new-pos
(string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos
))
1129 (push (substring arg pos new-pos
) accum
)
1131 (push (list (aref arg new-pos
)) accum
)
1132 (setq pos
(1+ new-pos
)))
1135 (apply 'concat
(nconc (nreverse accum
) (list (substring arg pos
))))))))
1137 (defun mm-auto-mode-alist ()
1138 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1139 (let ((alist auto-mode-alist
)
1142 (when (listp (cdar alist
))
1143 (push (car alist
) out
))
1147 (defvar mm-inhibit-file-name-handlers
1148 '(jka-compr-handler image-file-handler
)
1149 "A list of handlers doing (un)compression (etc) thingies.")
1151 (defun mm-insert-file-contents (filename &optional visit beg end replace
1153 "Like `insert-file-contents', but only reads in the file.
1154 A buffer may be modified in several ways after reading into the buffer due
1155 to advanced Emacs features, such as file-name-handlers, format decoding,
1156 `find-file-hooks', etc.
1157 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'.
1158 This function ensures that none of these modifications will take place."
1159 (let* ((format-alist nil
)
1160 (auto-mode-alist (if inhibit nil
(mm-auto-mode-alist)))
1161 (default-major-mode 'fundamental-mode
)
1162 (enable-local-variables nil
)
1163 (after-insert-file-functions nil
)
1164 (enable-local-eval nil
)
1165 (inhibit-file-name-operation (if inhibit
1166 'insert-file-contents
1167 inhibit-file-name-operation
))
1168 (inhibit-file-name-handlers
1170 (append mm-inhibit-file-name-handlers
1171 inhibit-file-name-handlers
)
1172 inhibit-file-name-handlers
))
1173 (ffh (if (boundp 'find-file-hook
)
1176 (val (symbol-value ffh
)))
1179 (insert-file-contents filename visit beg end replace
)
1182 (defun mm-append-to-file (start end filename
&optional codesys inhibit
)
1183 "Append the contents of the region to the end of file FILENAME.
1184 When called from a function, expects three arguments,
1185 START, END and FILENAME. START and END are buffer positions
1186 saying what text to write.
1187 Optional fourth argument specifies the coding system to use when
1189 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1190 (let ((coding-system-for-write
1191 (or codesys mm-text-coding-system-for-write
1192 mm-text-coding-system
))
1193 (inhibit-file-name-operation (if inhibit
1195 inhibit-file-name-operation
))
1196 (inhibit-file-name-handlers
1198 (append mm-inhibit-file-name-handlers
1199 inhibit-file-name-handlers
)
1200 inhibit-file-name-handlers
)))
1201 (write-region start end filename t
'no-message
)
1202 (message "Appended to %s" filename
)))
1204 (defun mm-write-region (start end filename
&optional append visit lockname
1205 coding-system inhibit
)
1207 "Like `write-region'.
1208 If INHIBIT is non-nil, inhibit `mm-inhibit-file-name-handlers'."
1209 (let ((coding-system-for-write
1210 (or coding-system mm-text-coding-system-for-write
1211 mm-text-coding-system
))
1212 (inhibit-file-name-operation (if inhibit
1214 inhibit-file-name-operation
))
1215 (inhibit-file-name-handlers
1217 (append mm-inhibit-file-name-handlers
1218 inhibit-file-name-handlers
)
1219 inhibit-file-name-handlers
)))
1220 (write-region start end filename append visit lockname
)))
1222 ;; It is not a MIME function, but some MIME functions use it.
1223 (if (and (fboundp 'make-temp-file
)
1225 (let ((def (symbol-function 'make-temp-file
)))
1226 (and (byte-code-function-p def
)
1227 (setq def
(if (fboundp 'compiled-function-arglist
)
1229 (eval (list 'compiled-function-arglist def
))
1232 (eq (nth 3 def
) 'suffix
)))))
1233 (defalias 'mm-make-temp-file
'make-temp-file
)
1234 ;; Stolen (and modified for Emacs 20 and XEmacs) from Emacs 22.
1235 (defun mm-make-temp-file (prefix &optional dir-flag suffix
)
1236 "Create a temporary file.
1237 The returned file name (created by appending some random characters at the end
1238 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1239 is guaranteed to point to a newly created empty file.
1240 You can then use `write-region' to write new data into the file.
1242 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1244 If SUFFIX is non-nil, add that at the end of the file name."
1245 (let ((umask (default-file-modes))
1249 ;; Create temp files with strict access rights. It's easy to
1250 ;; loosen them later, whereas it's impossible to close the
1251 ;; time-window of loose permissions otherwise.
1252 (set-default-file-modes 448)
1253 (while (condition-case err
1259 (if (fboundp 'temp-directory
)
1262 temporary-file-directory
))))
1264 (setq file
(concat file suffix
)))
1266 (make-directory file
)
1267 ;; NOTE: This is unsafe if Emacs 20
1268 ;; users and XEmacs users don't use
1269 ;; a secure temp directory.
1270 (gmm-write-region "" nil file nil
'silent
1273 (file-already-exists t
)
1274 ;; The Emacs 20 and XEmacs versions of
1275 ;; `make-directory' issue `file-error'.
1276 (file-error (or (and (or (featurep 'xemacs
)
1277 (= emacs-major-version
20))
1278 (file-exists-p file
))
1279 (signal (car err
) (cdr err
)))))
1280 ;; the file was somehow created by someone else between
1281 ;; `make-temp-name' and `write-region', let's try again.
1285 (set-default-file-modes umask
)))))
1287 (defun mm-image-load-path (&optional package
)
1289 (dolist (path load-path
(nreverse result
))
1292 (setq dir
(concat (file-name-directory
1293 (directory-file-name path
))
1294 "etc/images/" (or package
"gnus/")))))
1296 (push path result
))))
1298 ;; Fixme: This doesn't look useful where it's used.
1299 (if (fboundp 'detect-coding-region
)
1300 (defun mm-detect-coding-region (start end
)
1301 "Like `detect-coding-region' except returning the best one."
1302 (let ((coding-systems
1303 (detect-coding-region start end
)))
1304 (or (car-safe coding-systems
)
1306 (defun mm-detect-coding-region (start end
)
1307 (let ((point (point)))
1309 (skip-chars-forward "\0-\177" end
)
1311 (if (eq (point) end
) 'ascii
(mm-guess-charset))
1312 (goto-char point
)))))
1314 (if (fboundp 'coding-system-get
)
1315 (defun mm-detect-mime-charset-region (start end
)
1316 "Detect MIME charset of the text in the region between START and END."
1317 (let ((cs (mm-detect-coding-region start end
)))
1318 (or (coding-system-get cs
:mime-charset
)
1319 (coding-system-get cs
'mime-charset
))))
1320 (defun mm-detect-mime-charset-region (start end
)
1321 "Detect MIME charset of the text in the region between START and END."
1322 (let ((cs (mm-detect-coding-region start end
)))
1328 ;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
1329 ;;; mm-util.el ends here