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