]> code.delx.au - gnu-emacs/blob - lisp/gnus/mm-util.el
(mm-mime-mule-charset-alist)
[gnu-emacs] / lisp / gnus / mm-util.el
1 ;;; mm-util.el --- Utility functions for MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001 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
27 (eval-when-compile (require 'cl))
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 (iso-8859-14 latin-iso8859-14)
46 (iso-8859-15 latin-iso8859-15)
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 ,(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))))
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)
115 nil t))))
116 (string-as-unibyte . identity)
117 (multibyte-string-p . ignore)
118 )))
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
133 (defun mm-coding-system-p (sym)
134 "Return non-nil if SYM is a coding system."
135 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
136 (memq sym (mm-get-coding-system-list))))
137
138 (defvar mm-charset-synonym-alist
139 `((big5 . cn-big5)
140 (gb2312 . cn-gb-2312)
141 ;; Windows-1252 is actually a superset of Latin-1. See also
142 ;; `gnus-article-dumbquotes-map'.
143 ,(unless (mm-coding-system-p 'windows-1252) ; should be defined eventually
144 '(windows-1252 . iso-8859-1))
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)
181 "Return the MIME charset corresponding to the given Mule CHARSET."
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)
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.)
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 ()
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."
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 ()
244 "Unset the multibyte flag of in the current buffer.
245 This is a no-op in XEmacs."
246 (when (fboundp 'set-buffer-multibyte)
247 (set-buffer-multibyte nil)))
248
249 (defsubst mm-enable-multibyte-mule4 ()
250 "Enable multibyte in the current buffer.
251 Only used in Emacs Mule 4."
252 (when (and (fboundp 'set-buffer-multibyte)
253 (boundp 'enable-multibyte-characters)
254 (default-value 'enable-multibyte-characters)
255 (not (charsetp 'eight-bit-control)))
256 (set-buffer-multibyte t)))
257
258 (defsubst mm-disable-multibyte-mule4 ()
259 "Disable multibyte in the current buffer.
260 Only used in Emacs Mule 4."
261 (when (and (fboundp 'set-buffer-multibyte)
262 (not (charsetp 'eight-bit-control)))
263 (set-buffer-multibyte nil)))
264
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."
275 (let ((char (char-after pos)) charset)
276 (if (< (mm-char-int char) 128)
277 (setq charset 'ascii)
278 ;; charset-after is fake in some Emacsen.
279 (setq charset (and (fboundp 'char-charset) (char-charset char)))
280 (if (eq charset 'composition)
281 (let ((p (or pos (point))))
282 (cadr (find-charset-region p (1+ p))))
283 (if (and charset (not (memq charset '(ascii eight-bit-control
284 eight-bit-graphic))))
285 charset
286 (or
287 mail-parse-mule-charset ;; cached mule-charset
288 (progn
289 (setq mail-parse-mule-charset
290 (and (boundp 'current-language-environment)
291 (car (last
292 (assq 'charset
293 (assoc current-language-environment
294 language-info-alist))))))
295 (if (or (not mail-parse-mule-charset)
296 (eq mail-parse-mule-charset 'ascii))
297 (setq mail-parse-mule-charset
298 (or (car (last (assq mail-parse-charset
299 mm-mime-mule-charset-alist)))
300 ;; Fixme: don't fix that!
301 'latin-iso8859-1)))
302 mail-parse-mule-charset)))))))
303
304 (defun mm-mime-charset (charset)
305 "Return the MIME charset corresponding to the given Mule CHARSET."
306 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
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)
337 (let ((cs (find-coding-systems-region b e)))
338 (or (memq 'utf-8 cs) (memq 'mule-utf-8 cs))))
339 '(utf-8)
340 charsets)))
341
342 (defsubst mm-multibyte-p ()
343 "Say whether multibyte is enabled."
344 (if (and (not (featurep 'xemacs))
345 (boundp 'enable-multibyte-characters))
346 enable-multibyte-characters
347 (featurep 'mule)))
348
349 (defmacro mm-with-unibyte-buffer (&rest forms)
350 "Create a temporary buffer, and evaluate FORMS there like `progn'.
351 Use unibyte mode for this."
352 `(let (default-enable-multibyte-characters)
353 (with-temp-buffer ,@forms)))
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)
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"
361 (let ((multibyte (make-symbol "multibyte")))
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))))
371 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
372 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
373
374 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
375 "Evaluate FORMS there like `progn' in current buffer.
376 Mule4 only."
377 (let ((multibyte (make-symbol "multibyte")))
378 `(if (or (featurep 'xemacs)
379 (not (fboundp 'set-buffer-multibyte))
380 (charsetp 'eight-bit-control)) ;; For Emacs Mule 4 only.
381 (progn
382 ,@forms)
383 (let ((,multibyte (default-value 'enable-multibyte-characters)))
384 (unwind-protect
385 (let ((buffer-file-coding-system mm-binary-coding-system)
386 (coding-system-for-read mm-binary-coding-system)
387 (coding-system-for-write mm-binary-coding-system))
388 (set-buffer-multibyte nil)
389 (setq-default enable-multibyte-characters nil)
390 ,@forms)
391 (setq-default enable-multibyte-characters ,multibyte)
392 (set-buffer-multibyte ,multibyte))))))
393 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
394 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
395
396 (defmacro mm-with-unibyte (&rest forms)
397 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
398 `(let (default-enable-multibyte-characters)
399 ,@forms))
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)
404 "Return a list of Emacs charsets in the region B to E."
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)))
410 (t
411 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
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)
419 (let (charset)
420 (setq charset
421 (and (boundp 'current-language-environment)
422 (car (last (assq 'charset
423 (assoc current-language-environment
424 language-info-alist))))))
425 (if (eq charset 'ascii) (setq charset nil))
426 (or charset
427 (setq charset
428 (car (last (assq mail-parse-charset
429 mm-mime-mule-charset-alist)))))
430 (list 'ascii (or charset 'latin-iso8859-1)))))))))
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