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