1 ;;; mm-util.el --- Utility functions for Mule and low level things
2 ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
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)
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.
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.
27 (eval-when-compile (require 'cl))
30 (defvar mm-mime-mule-charset-alist
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 (gb2312 chinese-gb2312)
51 (big5 chinese-big5-1 chinese-big5-2)
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
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
74 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
75 (not (fboundp 'coding-system-p))
77 (not (coding-system-p 'mule-utf-8)))
78 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
79 ;; If we have utf-8 we're in Mule 5+.
80 (delete 'ascii (coding-system-get 'mule-utf-8 'safe-charsets))))
81 "Alist of MIME-charset/MULE-charsets.")
86 (let ((nfunc (intern (format "mm-%s" (car elem)))))
87 (if (fboundp (car elem))
88 (defalias nfunc (car elem))
89 (defalias nfunc (cdr elem)))))
90 '((decode-coding-string . (lambda (s a) s))
91 (encode-coding-string . (lambda (s a) s))
92 (encode-coding-region . ignore)
93 (coding-system-list . ignore)
94 (decode-coding-region . ignore)
96 (device-type . ignore)
97 (coding-system-equal . equal)
98 (annotationp . ignore)
99 (set-buffer-file-coding-system . ignore)
101 . (lambda (charset int)
109 (mapcar (lambda (e) (list (symbol-name (car e))))
110 mm-mime-mule-charset-alist)
112 (subst-char-in-string
113 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
114 "Replace characters in STRING from FROM to TO."
115 (let ((string (substring string 0)) ;Copy string.
116 (len (length string))
118 ;; Replace all occurrences of FROM with TO.
120 (when (= (aref string idx) from)
121 (aset string idx to))
124 (string-as-unibyte . identity)
125 (string-as-multibyte . identity)
126 (multibyte-string-p . ignore))))
129 (defalias 'mm-char-or-char-int-p
131 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
132 ((fboundp 'char-valid-p) 'char-valid-p)
136 (defalias 'mm-read-coding-system
138 ((fboundp 'read-coding-system)
139 (if (and (featurep 'xemacs)
140 (<= (string-to-number emacs-version) 21.1))
141 (lambda (prompt &optional default-coding-system)
142 (read-coding-system prompt))
143 'read-coding-system))
144 (t (lambda (prompt &optional default-coding-system)
145 "Prompt the user for a coding system."
147 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
148 mm-mime-mule-charset-alist)))))))
150 (defvar mm-coding-system-list nil)
151 (defun mm-get-coding-system-list ()
152 "Get the coding system list."
153 (or mm-coding-system-list
154 (setq mm-coding-system-list (mm-coding-system-list))))
156 (defun mm-coding-system-p (sym)
157 "Return non-nil if SYM is a coding system."
158 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
159 (memq sym (mm-get-coding-system-list))))
161 (defvar mm-charset-synonym-alist
163 (gb2312 . cn-gb-2312)
165 ;; Windows-1252 is actually a superset of Latin-1. See also
166 ;; `gnus-article-dumbquotes-map'.
167 ,(unless (mm-coding-system-p 'windows-1252) ; should be defined eventually
168 '(windows-1252 . iso-8859-1))
169 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
170 ;; Outlook users in Czech republic. Use this to allow reading of their
171 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
172 ,(unless (mm-coding-system-p 'windows-1250) ; should be defined eventually
173 '(windows-1250 . cp1250))
175 "A mapping from invalid charset names to the real charset names.")
177 (defvar mm-binary-coding-system
179 ((mm-coding-system-p 'binary) 'binary)
180 ((mm-coding-system-p 'no-conversion) 'no-conversion)
182 "100% binary coding system.")
184 (defvar mm-text-coding-system
185 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
186 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
187 (and (mm-coding-system-p 'raw-text) 'raw-text))
188 mm-binary-coding-system)
189 "Text-safe coding system (For removing ^M).")
191 (defvar mm-text-coding-system-for-write nil
192 "Text coding system for write.")
194 (defvar mm-auto-save-coding-system
196 ((mm-coding-system-p 'emacs-mule)
197 (if (memq system-type '(windows-nt ms-dos ms-windows))
198 (if (mm-coding-system-p 'emacs-mule-dos)
199 'emacs-mule-dos mm-binary-coding-system)
201 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
202 (t mm-binary-coding-system))
203 "Coding system of auto save file.")
205 (defvar mm-universal-coding-system mm-auto-save-coding-system
206 "The universal Coding system.")
208 ;;; Internal variables:
212 (defun mm-mule-charset-to-mime-charset (charset)
213 "Return the MIME charset corresponding to the given Mule CHARSET."
214 (let ((alist mm-mime-mule-charset-alist)
217 (when (memq charset (cdar alist))
218 (setq out (caar alist)
223 (defun mm-charset-to-coding-system (charset &optional lbt)
224 "Return coding-system corresponding to CHARSET.
225 CHARSET is a symbol naming a MIME charset.
226 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
227 used as the line break code type of the coding system."
228 (when (stringp charset)
229 (setq charset (intern (downcase charset))))
231 (or (cdr (assq charset mm-charset-synonym-alist))
234 (setq charset (intern (format "%s-%s" charset lbt))))
236 ;; Running in a non-MULE environment.
237 ((null (mm-get-coding-system-list))
240 ((eq charset 'us-ascii)
242 ;; Check to see whether we can handle this charset. (This depends
243 ;; on there being some coding system matching each `mime-charset'
244 ;; coding sysytem property defined, as there should be.)
245 ((memq charset (mm-get-coding-system-list))
251 (defsubst mm-replace-chars-in-string (string from to)
252 (mm-subst-char-in-string from to string))
254 (defsubst mm-enable-multibyte ()
255 "Set the multibyte flag of the current buffer.
256 Only do this if the default value of `enable-multibyte-characters' is
257 non-nil. This is a no-op in XEmacs."
258 (when (and (not (featurep 'xemacs))
259 (boundp 'default-enable-multibyte-characters)
260 default-enable-multibyte-characters
261 (fboundp 'set-buffer-multibyte))
262 (set-buffer-multibyte t)))
264 (defsubst mm-disable-multibyte ()
265 "Unset the multibyte flag of in the current buffer.
266 This is a no-op in XEmacs."
267 (when (and (not (featurep 'xemacs))
268 (fboundp 'set-buffer-multibyte))
269 (set-buffer-multibyte nil)))
271 (defsubst mm-enable-multibyte-mule4 ()
272 "Enable multibyte in the current buffer.
273 Only used in Emacs Mule 4."
274 (when (and (not (featurep 'xemacs))
275 (boundp 'default-enable-multibyte-characters)
276 default-enable-multibyte-characters
277 (fboundp 'set-buffer-multibyte)
279 (not (charsetp 'eight-bit-control)))
280 (set-buffer-multibyte t)))
282 (defsubst mm-disable-multibyte-mule4 ()
283 "Disable multibyte in the current buffer.
284 Only used in Emacs Mule 4."
285 (when (and (not (featurep 'xemacs))
286 (fboundp 'set-buffer-multibyte)
288 (not (charsetp 'eight-bit-control)))
289 (set-buffer-multibyte nil)))
291 (defun mm-preferred-coding-system (charset)
292 ;; A typo in some Emacs versions.
293 (or (get-charset-property charset 'prefered-coding-system)
294 (get-charset-property charset 'preferred-coding-system)))
296 (defun mm-charset-after (&optional pos)
297 "Return charset of a character in current buffer at position POS.
298 If POS is nil, it defauls to the current point.
299 If POS is out of range, the value is nil.
300 If the charset is `composition', return the actual one."
301 (let ((char (char-after pos)) charset)
302 (if (< (mm-char-int char) 128)
303 (setq charset 'ascii)
304 ;; charset-after is fake in some Emacsen.
305 (setq charset (and (fboundp 'char-charset) (char-charset char)))
306 (if (eq charset 'composition)
307 (let ((p (or pos (point))))
308 (cadr (find-charset-region p (1+ p))))
309 (if (and charset (not (memq charset '(ascii eight-bit-control
310 eight-bit-graphic))))
313 mail-parse-mule-charset ;; cached mule-charset
315 (setq mail-parse-mule-charset
316 (and (boundp 'current-language-environment)
319 (assoc current-language-environment
320 language-info-alist))))))
321 (if (or (not mail-parse-mule-charset)
322 (eq mail-parse-mule-charset 'ascii))
323 (setq mail-parse-mule-charset
324 (or (car (last (assq mail-parse-charset
325 mm-mime-mule-charset-alist)))
326 ;; Fixme: don't fix that!
328 mail-parse-mule-charset)))))))
330 (defun mm-mime-charset (charset)
331 "Return the MIME charset corresponding to the MULE CHARSET."
332 (if (eq charset 'unknown)
333 (error "The message contains non-printable characters, please use attachment"))
334 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
335 ;; This exists in Emacs 20.
337 (and (mm-preferred-coding-system charset)
339 (mm-preferred-coding-system charset) 'mime-charset))
340 (and (eq charset 'ascii)
342 (mm-preferred-coding-system charset)
343 (mm-mule-charset-to-mime-charset charset))
344 ;; This is for XEmacs.
345 (mm-mule-charset-to-mime-charset charset)))
347 (defun mm-delete-duplicates (list)
348 "Simple substitute for CL `delete-duplicates', testing with `equal'."
351 (setq head (car list))
352 (setq list (delete head list))
353 (setq result (cons head result)))
356 (defun mm-find-mime-charset-region (b e)
357 "Return the MIME charsets needed to encode the region between B and E."
358 (let ((charsets (mapcar 'mm-mime-charset
360 (mm-find-charset-region b e)))))
361 (when (memq 'iso-2022-jp-2 charsets)
362 (setq charsets (delq 'iso-2022-jp charsets)))
363 (setq charsets (mm-delete-duplicates charsets))
364 (if (and (> (length charsets) 1)
365 (fboundp 'find-coding-systems-region)
366 (let ((cs (find-coding-systems-region b e)))
367 (or (memq 'utf-8 cs) (memq 'mule-utf-8 cs))))
371 (defsubst mm-multibyte-p ()
372 "Say whether multibyte is enabled."
373 (if (and (not (featurep 'xemacs))
374 (boundp 'enable-multibyte-characters))
375 enable-multibyte-characters
378 (defmacro mm-with-unibyte-buffer (&rest forms)
379 "Create a temporary buffer, and evaluate FORMS there like `progn'.
380 Use unibyte mode for this."
381 `(let (default-enable-multibyte-characters)
382 (with-temp-buffer ,@forms)))
383 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
384 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
386 (defmacro mm-with-unibyte-current-buffer (&rest forms)
387 "Evaluate FORMS with current current buffer temporarily made unibyte.
388 Also bind `default-enable-multibyte-characters' to nil.
389 Equivalent to `progn' in XEmacs"
390 (let ((buffer (make-symbol "buffer")))
391 `(if (and (not (featurep 'xemacs))
392 (boundp 'enable-multibyte-characters)
393 enable-multibyte-characters
394 (fboundp 'set-buffer-multibyte))
395 (let ((,buffer (current-buffer)))
397 (let (default-enable-multibyte-characters)
398 (set-buffer-multibyte nil)
401 (set-buffer-multibyte t)))
402 (let (default-enable-multibyte-characters)
404 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
405 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
407 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
408 "Evaluate FORMS there like `progn' in current buffer.
410 (let ((buffer (make-symbol "buffer")))
411 `(if (and (not (featurep 'xemacs))
412 (boundp 'enable-multibyte-characters)
413 enable-multibyte-characters
414 (fboundp 'set-buffer-multibyte)
416 (not (charsetp 'eight-bit-control))) ;; For Emacs Mule 4 only.
417 (let ((,buffer (current-buffer)))
419 (let (default-enable-multibyte-characters)
420 (set-buffer-multibyte nil)
423 (set-buffer-multibyte t)))
424 (let (default-enable-multibyte-characters)
426 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
427 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
429 (defmacro mm-with-unibyte (&rest forms)
430 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
431 `(let (default-enable-multibyte-characters)
433 (put 'mm-with-unibyte 'lisp-indent-function 0)
434 (put 'mm-with-unibyte 'edebug-form-spec '(body))
436 (defun mm-find-charset-region (b e)
437 "Return a list of Emacs charsets in the region B to E."
439 ((and (mm-multibyte-p)
440 (fboundp 'find-charset-region))
441 ;; Remove composition since the base charsets have been included.
442 ;; Remove eight-bit-*, treat them as ascii.
443 (let ((css (find-charset-region b e)))
444 (mapcar (lambda (cs) (setq css (delq cs css)))
445 '(composition eight-bit-control eight-bit-graphic
449 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
452 (narrow-to-region b e)
453 (goto-char (point-min))
454 (skip-chars-forward "\0-\177")
459 (and (boundp 'current-language-environment)
460 (car (last (assq 'charset
461 (assoc current-language-environment
462 language-info-alist))))))
463 (if (eq charset 'ascii) (setq charset nil))
466 (car (last (assq mail-parse-charset
467 mm-mime-mule-charset-alist)))))
468 (list 'ascii (or charset 'latin-iso8859-1)))))))))
470 (if (fboundp 'shell-quote-argument)
471 (defalias 'mm-quote-arg 'shell-quote-argument)
472 (defun mm-quote-arg (arg)
473 "Return a version of ARG that is safe to evaluate in a shell."
474 (let ((pos 0) new-pos accum)
475 ;; *** bug: we don't handle newline characters properly
476 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
477 (push (substring arg pos new-pos) accum)
479 (push (list (aref arg new-pos)) accum)
480 (setq pos (1+ new-pos)))
483 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
485 (defun mm-auto-mode-alist ()
486 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
487 (let ((alist auto-mode-alist)
490 (when (listp (cdar alist))
491 (push (car alist) out))
495 (defvar mm-inhibit-file-name-handlers
496 '(jka-compr-handler image-file-handler)
497 "A list of handlers doing (un)compression (etc) thingies.")
499 (defun mm-insert-file-contents (filename &optional visit beg end replace
501 "Like `insert-file-contents', q.v., but only reads in the file.
502 A buffer may be modified in several ways after reading into the buffer due
503 to advanced Emacs features, such as file-name-handlers, format decoding,
504 find-file-hooks, etc.
505 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
506 This function ensures that none of these modifications will take place."
507 (let ((format-alist nil)
508 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
509 (default-major-mode 'fundamental-mode)
510 (enable-local-variables nil)
511 (after-insert-file-functions nil)
512 (enable-local-eval nil)
513 (find-file-hooks nil)
514 (inhibit-file-name-operation (if inhibit
515 'insert-file-contents
516 inhibit-file-name-operation))
517 (inhibit-file-name-handlers
519 (append mm-inhibit-file-name-handlers
520 inhibit-file-name-handlers)
521 inhibit-file-name-handlers)))
522 (insert-file-contents filename visit beg end replace)))
524 (defun mm-append-to-file (start end filename &optional codesys inhibit)
525 "Append the contents of the region to the end of file FILENAME.
526 When called from a function, expects three arguments,
527 START, END and FILENAME. START and END are buffer positions
528 saying what text to write.
529 Optional fourth argument specifies the coding system to use when
531 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
532 (let ((coding-system-for-write
533 (or codesys mm-text-coding-system-for-write
534 mm-text-coding-system))
535 (inhibit-file-name-operation (if inhibit
537 inhibit-file-name-operation))
538 (inhibit-file-name-handlers
540 (append mm-inhibit-file-name-handlers
541 inhibit-file-name-handlers)
542 inhibit-file-name-handlers)))
543 (append-to-file start end filename)))
545 (defun mm-write-region (start end filename &optional append visit lockname
546 coding-system inhibit)
548 "Like `write-region'.
549 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
550 (let ((coding-system-for-write
551 (or coding-system mm-text-coding-system-for-write
552 mm-text-coding-system))
553 (inhibit-file-name-operation (if inhibit
555 inhibit-file-name-operation))
556 (inhibit-file-name-handlers
558 (append mm-inhibit-file-name-handlers
559 inhibit-file-name-handlers)
560 inhibit-file-name-handlers)))
561 (write-region start end filename append visit lockname)))
563 (defun mm-image-load-path (&optional package)
565 (dolist (path load-path (nreverse result))
566 (if (file-directory-p
567 (setq dir (concat (file-name-directory
568 (directory-file-name path))
569 "etc/" (or package "gnus/"))))
571 (push path result))))
575 ;;; mm-util.el ends here