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