Mercurial > emacs
annotate lisp/gnus/mm-util.el @ 35612:446e8151c90f
*** empty log message ***
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Fri, 26 Jan 2001 14:37:45 +0000 |
parents | 26726eff41ca |
children | 04f8ee49a6db |
rev | line source |
---|---|
31717 | 1 ;;; mm-util.el --- Utility functions for MIME things |
2 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. | |
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 | |
32976
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
27 (eval-when-compile (require 'cl)) |
31717 | 28 (require 'mail-prsvr) |
29 | |
30 (defvar mm-mime-mule-charset-alist | |
31 '((us-ascii ascii) | |
32 (iso-8859-1 latin-iso8859-1) | |
33 (iso-8859-2 latin-iso8859-2) | |
34 (iso-8859-3 latin-iso8859-3) | |
35 (iso-8859-4 latin-iso8859-4) | |
36 (iso-8859-5 cyrillic-iso8859-5) | |
37 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters. | |
38 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default | |
39 ;; charset is koi8-r, not iso-8859-5. | |
40 (koi8-r cyrillic-iso8859-5 gnus-koi8-r) | |
41 (iso-8859-6 arabic-iso8859-6) | |
42 (iso-8859-7 greek-iso8859-7) | |
43 (iso-8859-8 hebrew-iso8859-8) | |
44 (iso-8859-9 latin-iso8859-9) | |
35148
a408e1d28e3d
(mm-mime-mule-charset-alist): Add Latin-{8,9}.
Dave Love <fx@gnu.org>
parents:
34724
diff
changeset
|
45 (iso-8859-14 latin-iso8859-14) |
a408e1d28e3d
(mm-mime-mule-charset-alist): Add Latin-{8,9}.
Dave Love <fx@gnu.org>
parents:
34724
diff
changeset
|
46 (iso-8859-15 latin-iso8859-15) |
31717 | 47 (viscii vietnamese-viscii-lower) |
48 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978) | |
49 (euc-kr korean-ksc5601) | |
50 (cn-gb-2312 chinese-gb2312) | |
51 (cn-big5 chinese-big5-1 chinese-big5-2) | |
52 (tibetan tibetan) | |
53 (thai-tis620 thai-tis620) | |
54 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column) | |
55 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7 | |
56 latin-jisx0201 japanese-jisx0208-1978 | |
57 chinese-gb2312 japanese-jisx0208 | |
58 korean-ksc5601 japanese-jisx0212 | |
59 katakana-jisx0201) | |
60 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7 | |
61 latin-jisx0201 japanese-jisx0208-1978 | |
62 chinese-gb2312 japanese-jisx0208 | |
63 korean-ksc5601 japanese-jisx0212 | |
64 chinese-cns11643-1 chinese-cns11643-2) | |
65 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2 | |
66 cyrillic-iso8859-5 greek-iso8859-7 | |
67 latin-jisx0201 japanese-jisx0208-1978 | |
68 chinese-gb2312 japanese-jisx0208 | |
69 korean-ksc5601 japanese-jisx0212 | |
70 chinese-cns11643-1 chinese-cns11643-2 | |
71 chinese-cns11643-3 chinese-cns11643-4 | |
72 chinese-cns11643-5 chinese-cns11643-6 | |
73 chinese-cns11643-7) | |
74 (utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)) | |
75 "Alist of MIME-charset/MULE-charsets.") | |
76 | |
77 (eval-and-compile | |
78 (mapcar | |
79 (lambda (elem) | |
80 (let ((nfunc (intern (format "mm-%s" (car elem))))) | |
81 (if (fboundp (car elem)) | |
82 (defalias nfunc (car elem)) | |
83 (defalias nfunc (cdr elem))))) | |
84 '((decode-coding-string . (lambda (s a) s)) | |
85 (encode-coding-string . (lambda (s a) s)) | |
86 (encode-coding-region . ignore) | |
87 (coding-system-list . ignore) | |
88 (decode-coding-region . ignore) | |
89 (char-int . identity) | |
90 (device-type . ignore) | |
91 (coding-system-equal . equal) | |
92 (annotationp . ignore) | |
93 (set-buffer-file-coding-system . ignore) | |
94 (make-char | |
95 . (lambda (charset int) | |
96 (int-to-char int))) | |
97 (read-coding-system | |
98 . (lambda (prompt) | |
99 "Prompt the user for a coding system." | |
100 (completing-read | |
101 prompt (mapcar (lambda (s) (list (symbol-name (car s)))) | |
102 mm-mime-mule-charset-alist)))) | |
103 (read-charset | |
104 . (lambda (prompt) | |
105 "Return a charset." | |
106 (intern | |
107 (completing-read | |
108 prompt | |
109 (mapcar (lambda (e) (list (symbol-name (car e)))) | |
110 mm-mime-mule-charset-alist) | |
35453
26726eff41ca
2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
35148
diff
changeset
|
111 nil t)))) |
26726eff41ca
2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
35148
diff
changeset
|
112 (string-as-unibyte . identity) |
26726eff41ca
2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
35148
diff
changeset
|
113 ))) |
31717 | 114 |
115 (eval-and-compile | |
116 (defalias 'mm-char-or-char-int-p | |
117 (cond | |
118 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p) | |
119 ((fboundp 'char-valid-p) 'char-valid-p) | |
120 (t 'identity)))) | |
121 | |
122 (defvar mm-coding-system-list nil) | |
123 (defun mm-get-coding-system-list () | |
124 "Get the coding system list." | |
125 (or mm-coding-system-list | |
126 (setq mm-coding-system-list (mm-coding-system-list)))) | |
127 | |
34724
15c6663e5676
2000-12-19 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
33378
diff
changeset
|
128 (defun mm-coding-system-p (sym) |
15c6663e5676
2000-12-19 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
33378
diff
changeset
|
129 "Return non-nil if SYM is a coding system." |
15c6663e5676
2000-12-19 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
33378
diff
changeset
|
130 (or (and (fboundp 'coding-system-p) (coding-system-p sym)) |
15c6663e5676
2000-12-19 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
33378
diff
changeset
|
131 (memq sym (mm-get-coding-system-list)))) |
15c6663e5676
2000-12-19 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
33378
diff
changeset
|
132 |
31717 | 133 (defvar mm-charset-synonym-alist |
32414
cbdba3c57536
(mm-charset-synonym-alist): Add windows-1252.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
134 `((big5 . cn-big5) |
31717 | 135 (gb2312 . cn-gb-2312) |
32414
cbdba3c57536
(mm-charset-synonym-alist): Add windows-1252.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
136 ;; Windows-1252 is actually a superset of Latin-1. See also |
cbdba3c57536
(mm-charset-synonym-alist): Add windows-1252.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
137 ;; `gnus-article-dumbquotes-map'. |
34724
15c6663e5676
2000-12-19 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
33378
diff
changeset
|
138 ,(unless (mm-coding-system-p 'windows-1252) ; should be defined eventually |
15c6663e5676
2000-12-19 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
33378
diff
changeset
|
139 '(windows-1252 . iso-8859-1)) |
31717 | 140 (x-ctext . ctext)) |
141 "A mapping from invalid charset names to the real charset names.") | |
142 | |
143 (defvar mm-binary-coding-system | |
144 (cond | |
145 ((mm-coding-system-p 'binary) 'binary) | |
146 ((mm-coding-system-p 'no-conversion) 'no-conversion) | |
147 (t nil)) | |
148 "100% binary coding system.") | |
149 | |
150 (defvar mm-text-coding-system | |
151 (or (if (memq system-type '(windows-nt ms-dos ms-windows)) | |
152 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos) | |
153 (and (mm-coding-system-p 'raw-text) 'raw-text)) | |
154 mm-binary-coding-system) | |
155 "Text-safe coding system (For removing ^M).") | |
156 | |
157 (defvar mm-text-coding-system-for-write nil | |
158 "Text coding system for write.") | |
159 | |
160 (defvar mm-auto-save-coding-system | |
161 (cond | |
162 ((mm-coding-system-p 'emacs-mule) | |
163 (if (memq system-type '(windows-nt ms-dos ms-windows)) | |
164 (if (mm-coding-system-p 'emacs-mule-dos) | |
165 'emacs-mule-dos mm-binary-coding-system) | |
166 'emacs-mule)) | |
167 ((mm-coding-system-p 'escape-quoted) 'escape-quoted) | |
168 (t mm-binary-coding-system)) | |
169 "Coding system of auto save file.") | |
170 | |
171 ;;; Internal variables: | |
172 | |
173 ;;; Functions: | |
174 | |
175 (defun mm-mule-charset-to-mime-charset (charset) | |
33378 | 176 "Return the MIME charset corresponding to the given Mule CHARSET." |
31717 | 177 (let ((alist mm-mime-mule-charset-alist) |
178 out) | |
179 (while alist | |
180 (when (memq charset (cdar alist)) | |
181 (setq out (caar alist) | |
182 alist nil)) | |
183 (pop alist)) | |
184 out)) | |
185 | |
186 (defun mm-charset-to-coding-system (charset &optional lbt) | |
187 "Return coding-system corresponding to CHARSET. | |
188 CHARSET is a symbol naming a MIME charset. | |
189 If optional argument LBT (`unix', `dos' or `mac') is specified, it is | |
190 used as the line break code type of the coding system." | |
191 (when (stringp charset) | |
192 (setq charset (intern (downcase charset)))) | |
193 (setq charset | |
194 (or (cdr (assq charset mm-charset-synonym-alist)) | |
195 charset)) | |
196 (when lbt | |
197 (setq charset (intern (format "%s-%s" charset lbt)))) | |
198 (cond | |
199 ;; Running in a non-MULE environment. | |
200 ((null (mm-get-coding-system-list)) | |
201 charset) | |
202 ;; ascii | |
203 ((eq charset 'us-ascii) | |
204 'ascii) | |
33378 | 205 ;; Check to see whether we can handle this charset. (This depends |
206 ;; on there being some coding system matching each `mime-charset' | |
207 ;; coding sysytem property defined, as there should be.) | |
31717 | 208 ((memq charset (mm-get-coding-system-list)) |
209 charset) | |
210 ;; Nope. | |
211 (t | |
212 nil))) | |
213 | |
214 (if (fboundp 'subst-char-in-string) | |
215 (defsubst mm-replace-chars-in-string (string from to) | |
216 (subst-char-in-string from to string)) | |
217 (defun mm-replace-chars-in-string (string from to) | |
218 "Replace characters in STRING from FROM to TO." | |
219 (let ((string (substring string 0)) ;Copy string. | |
220 (len (length string)) | |
221 (idx 0)) | |
222 ;; Replace all occurrences of FROM with TO. | |
223 (while (< idx len) | |
224 (when (= (aref string idx) from) | |
225 (aset string idx to)) | |
226 (setq idx (1+ idx))) | |
227 string))) | |
228 | |
229 (defsubst mm-enable-multibyte () | |
33378 | 230 "Set the multibyte flag of the current buffer. |
231 Only do this if the default value of `enable-multibyte-characters' is | |
232 non-nil. This is a no-op in XEmacs." | |
31717 | 233 (when (and (fboundp 'set-buffer-multibyte) |
234 (boundp 'enable-multibyte-characters) | |
235 (default-value 'enable-multibyte-characters)) | |
236 (set-buffer-multibyte t))) | |
237 | |
238 (defsubst mm-disable-multibyte () | |
33378 | 239 "Unset the multibyte flag of in the current buffer. |
240 This is a no-op in XEmacs." | |
31717 | 241 (when (fboundp 'set-buffer-multibyte) |
242 (set-buffer-multibyte nil))) | |
243 | |
32976
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
244 (defsubst mm-enable-multibyte-mule4 () |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
245 "Enable multibyte in the current buffer. |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
246 Only used in Emacs Mule 4." |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
247 (when (and (fboundp 'set-buffer-multibyte) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
248 (boundp 'enable-multibyte-characters) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
249 (default-value 'enable-multibyte-characters) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
250 (not (charsetp 'eight-bit-control))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
251 (set-buffer-multibyte t))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
252 |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
253 (defsubst mm-disable-multibyte-mule4 () |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
254 "Disable multibyte in the current buffer. |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
255 Only used in Emacs Mule 4." |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
256 (when (and (fboundp 'set-buffer-multibyte) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
257 (not (charsetp 'eight-bit-control))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
258 (set-buffer-multibyte nil))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
259 |
31717 | 260 (defun mm-preferred-coding-system (charset) |
261 ;; A typo in some Emacs versions. | |
262 (or (get-charset-property charset 'prefered-coding-system) | |
263 (get-charset-property charset 'preferred-coding-system))) | |
264 | |
265 (defun mm-charset-after (&optional pos) | |
266 "Return charset of a character in current buffer at position POS. | |
267 If POS is nil, it defauls to the current point. | |
268 If POS is out of range, the value is nil. | |
269 If the charset is `composition', return the actual one." | |
32976
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
270 (let ((char (char-after pos)) charset) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
271 (if (< (mm-char-int char) 128) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
272 (setq charset 'ascii) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
273 ;; charset-after is fake in some Emacsen. |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
274 (setq charset (and (fboundp 'char-charset) (char-charset char))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
275 (if (eq charset 'composition) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
276 (let ((p (or pos (point)))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
277 (cadr (find-charset-region p (1+ p)))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
278 (if (and charset (not (memq charset '(ascii eight-bit-control |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
279 eight-bit-graphic)))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
280 charset |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
281 (or |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
282 mail-parse-mule-charset ;; cached mule-charset |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
283 (progn |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
284 (setq mail-parse-mule-charset |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
285 (and (boundp 'current-language-environment) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
286 (car (last |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
287 (assq 'charset |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
288 (assoc current-language-environment |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
289 language-info-alist)))))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
290 (if (or (not mail-parse-mule-charset) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
291 (eq mail-parse-mule-charset 'ascii)) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
292 (setq mail-parse-mule-charset |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
293 (or (car (last (assq mail-parse-charset |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
294 mm-mime-mule-charset-alist))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
295 'latin-iso8859-1))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
296 mail-parse-mule-charset))))))) |
31717 | 297 |
298 (defun mm-mime-charset (charset) | |
33378 | 299 "Return the MIME charset corresponding to the given Mule CHARSET." |
32976
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
300 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property)) |
31717 | 301 ;; This exists in Emacs 20. |
302 (or | |
303 (and (mm-preferred-coding-system charset) | |
304 (coding-system-get | |
305 (mm-preferred-coding-system charset) 'mime-charset)) | |
306 (and (eq charset 'ascii) | |
307 'us-ascii) | |
308 (mm-mule-charset-to-mime-charset charset)) | |
309 ;; This is for XEmacs. | |
310 (mm-mule-charset-to-mime-charset charset))) | |
311 | |
312 (defun mm-delete-duplicates (list) | |
313 "Simple substitute for CL `delete-duplicates', testing with `equal'." | |
314 (let (result head) | |
315 (while list | |
316 (setq head (car list)) | |
317 (setq list (delete head list)) | |
318 (setq result (cons head result))) | |
319 (nreverse result))) | |
320 | |
321 (defun mm-find-mime-charset-region (b e) | |
322 "Return the MIME charsets needed to encode the region between B and E." | |
323 (let ((charsets (mapcar 'mm-mime-charset | |
324 (delq 'ascii | |
325 (mm-find-charset-region b e))))) | |
326 (when (memq 'iso-2022-jp-2 charsets) | |
327 (setq charsets (delq 'iso-2022-jp charsets))) | |
328 (setq charsets (mm-delete-duplicates charsets)) | |
329 (if (and (> (length charsets) 1) | |
330 (fboundp 'find-coding-systems-region) | |
331 (memq 'utf-8 (find-coding-systems-region b e))) | |
332 '(utf-8) | |
333 charsets))) | |
334 | |
335 (defsubst mm-multibyte-p () | |
336 "Say whether multibyte is enabled." | |
32976
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
337 (if (and (not (featurep 'xemacs)) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
338 (boundp 'enable-multibyte-characters)) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
339 enable-multibyte-characters |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
340 (featurep 'mule))) |
31717 | 341 |
342 (defmacro mm-with-unibyte-buffer (&rest forms) | |
343 "Create a temporary buffer, and evaluate FORMS there like `progn'. | |
33378 | 344 Use unibyte mode for this." |
345 `(let (default-enable-multibyte-characters) | |
346 (with-temp-buffer ,@forms))) | |
31717 | 347 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0) |
348 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body)) | |
349 | |
350 (defmacro mm-with-unibyte-current-buffer (&rest forms) | |
33378 | 351 "Evaluate FORMS with current current buffer temporarily made unibyte. |
352 Also bind `default-enable-multibyte-characters' to nil. | |
353 Equivalent to `progn' in XEmacs" | |
31717 | 354 (let ((multibyte (make-symbol "multibyte"))) |
33378 | 355 `(if (fboundp 'set-buffer-multibyte) |
356 (let ((,multibyte enable-multibyte-characters)) | |
357 (unwind-protect | |
358 (let (default-enable-multibyte-characters) | |
359 (set-buffer-multibyte nil) | |
360 ,@forms) | |
361 (set-buffer-multibyte ,multibyte))) | |
362 (progn | |
363 ,@forms)))) | |
31717 | 364 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0) |
365 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body)) | |
366 | |
32976
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
367 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
368 "Evaluate FORMS there like `progn' in current buffer. |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
369 Mule4 only." |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
370 (let ((multibyte (make-symbol "multibyte"))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
371 `(if (or (featurep 'xemacs) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
372 (not (fboundp 'set-buffer-multibyte)) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
373 (charsetp 'eight-bit-control)) ;; For Emacs Mule 4 only. |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
374 (progn |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
375 ,@forms) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
376 (let ((,multibyte (default-value 'enable-multibyte-characters))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
377 (unwind-protect |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
378 (let ((buffer-file-coding-system mm-binary-coding-system) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
379 (coding-system-for-read mm-binary-coding-system) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
380 (coding-system-for-write mm-binary-coding-system)) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
381 (set-buffer-multibyte nil) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
382 (setq-default enable-multibyte-characters nil) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
383 ,@forms) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
384 (setq-default enable-multibyte-characters ,multibyte) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
385 (set-buffer-multibyte ,multibyte)))))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
386 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
387 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body)) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
388 |
31717 | 389 (defmacro mm-with-unibyte (&rest forms) |
33378 | 390 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ." |
391 `(let (default-enable-multibyte-characters) | |
392 ,@forms)) | |
31717 | 393 (put 'mm-with-unibyte 'lisp-indent-function 0) |
394 (put 'mm-with-unibyte 'edebug-form-spec '(body)) | |
395 | |
396 (defun mm-find-charset-region (b e) | |
33378 | 397 "Return a list of Emacs charsets in the region B to E." |
31717 | 398 (cond |
399 ((and (mm-multibyte-p) | |
400 (fboundp 'find-charset-region)) | |
401 ;; Remove composition since the base charsets have been included. | |
402 (delq 'composition (find-charset-region b e))) | |
32976
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
403 (t |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
404 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit. |
31717 | 405 (save-excursion |
406 (save-restriction | |
407 (narrow-to-region b e) | |
408 (goto-char (point-min)) | |
409 (skip-chars-forward "\0-\177") | |
410 (if (eobp) | |
411 '(ascii) | |
32976
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
412 (let (charset) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
413 (setq charset |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
414 (and (boundp 'current-language-environment) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
415 (car (last (assq 'charset |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
416 (assoc current-language-environment |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
417 language-info-alist)))))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
418 (if (eq charset 'ascii) (setq charset nil)) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
419 (or charset |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
420 (setq charset |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
421 (car (last (assq mail-parse-charset |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
422 mm-mime-mule-charset-alist))))) |
aa9dc4e7c5ac
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
32414
diff
changeset
|
423 (list 'ascii (or charset 'latin-iso8859-1))))))))) |
31717 | 424 |
425 (if (fboundp 'shell-quote-argument) | |
426 (defalias 'mm-quote-arg 'shell-quote-argument) | |
427 (defun mm-quote-arg (arg) | |
428 "Return a version of ARG that is safe to evaluate in a shell." | |
429 (let ((pos 0) new-pos accum) | |
430 ;; *** bug: we don't handle newline characters properly | |
431 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos)) | |
432 (push (substring arg pos new-pos) accum) | |
433 (push "\\" accum) | |
434 (push (list (aref arg new-pos)) accum) | |
435 (setq pos (1+ new-pos))) | |
436 (if (= pos 0) | |
437 arg | |
438 (apply 'concat (nconc (nreverse accum) (list (substring arg pos)))))))) | |
439 | |
440 (defun mm-auto-mode-alist () | |
441 "Return an `auto-mode-alist' with only the .gz (etc) thingies." | |
442 (let ((alist auto-mode-alist) | |
443 out) | |
444 (while alist | |
445 (when (listp (cdar alist)) | |
446 (push (car alist) out)) | |
447 (pop alist)) | |
448 (nreverse out))) | |
449 | |
450 (defvar mm-inhibit-file-name-handlers | |
451 '(jka-compr-handler) | |
452 "A list of handlers doing (un)compression (etc) thingies.") | |
453 | |
454 (defun mm-insert-file-contents (filename &optional visit beg end replace | |
455 inhibit) | |
456 "Like `insert-file-contents', q.v., but only reads in the file. | |
457 A buffer may be modified in several ways after reading into the buffer due | |
458 to advanced Emacs features, such as file-name-handlers, format decoding, | |
459 find-file-hooks, etc. | |
460 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers. | |
461 This function ensures that none of these modifications will take place." | |
462 (let ((format-alist nil) | |
463 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist))) | |
464 (default-major-mode 'fundamental-mode) | |
465 (enable-local-variables nil) | |
466 (after-insert-file-functions nil) | |
467 (enable-local-eval nil) | |
468 (find-file-hooks nil) | |
469 (inhibit-file-name-operation (if inhibit | |
470 'insert-file-contents | |
471 inhibit-file-name-operation)) | |
472 (inhibit-file-name-handlers | |
473 (if inhibit | |
474 (append mm-inhibit-file-name-handlers | |
475 inhibit-file-name-handlers) | |
476 inhibit-file-name-handlers))) | |
477 (insert-file-contents filename visit beg end replace))) | |
478 | |
479 (defun mm-append-to-file (start end filename &optional codesys inhibit) | |
480 "Append the contents of the region to the end of file FILENAME. | |
481 When called from a function, expects three arguments, | |
482 START, END and FILENAME. START and END are buffer positions | |
483 saying what text to write. | |
484 Optional fourth argument specifies the coding system to use when | |
485 encoding the file. | |
486 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers." | |
487 (let ((coding-system-for-write | |
488 (or codesys mm-text-coding-system-for-write | |
489 mm-text-coding-system)) | |
490 (inhibit-file-name-operation (if inhibit | |
491 'append-to-file | |
492 inhibit-file-name-operation)) | |
493 (inhibit-file-name-handlers | |
494 (if inhibit | |
495 (append mm-inhibit-file-name-handlers | |
496 inhibit-file-name-handlers) | |
497 inhibit-file-name-handlers))) | |
498 (append-to-file start end filename))) | |
499 | |
500 (defun mm-write-region (start end filename &optional append visit lockname | |
501 coding-system inhibit) | |
502 | |
503 "Like `write-region'. | |
504 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers." | |
505 (let ((coding-system-for-write | |
506 (or coding-system mm-text-coding-system-for-write | |
507 mm-text-coding-system)) | |
508 (inhibit-file-name-operation (if inhibit | |
509 'write-region | |
510 inhibit-file-name-operation)) | |
511 (inhibit-file-name-handlers | |
512 (if inhibit | |
513 (append mm-inhibit-file-name-handlers | |
514 inhibit-file-name-handlers) | |
515 inhibit-file-name-handlers))) | |
516 (write-region start end filename append visit lockname))) | |
517 | |
518 (provide 'mm-util) | |
519 | |
520 ;;; mm-util.el ends here |