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