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