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+.
82 (coding-system-get 'mule-utf-8 'safe-charsets)))))
83 "Alist of MIME-charset/MULE-charsets.")
88 (let ((nfunc (intern (format "mm-%s" (car elem)))))
89 (if (fboundp (car elem))
90 (defalias nfunc (car elem))
91 (defalias nfunc (cdr elem)))))
92 '((decode-coding-string . (lambda (s a) s))
93 (encode-coding-string . (lambda (s a) s))
94 (encode-coding-region . ignore)
95 (coding-system-list . ignore)
96 (decode-coding-region . ignore)
98 (device-type . ignore)
99 (coding-system-equal . equal)
100 (annotationp . ignore)
101 (set-buffer-file-coding-system . ignore)
103 . (lambda (charset int)
111 (mapcar (lambda (e) (list (symbol-name (car e))))
112 mm-mime-mule-charset-alist)
114 (subst-char-in-string
115 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
116 "Replace characters in STRING from FROM to TO."
117 (let ((string (substring string 0)) ;Copy string.
118 (len (length string))
120 ;; Replace all occurrences of FROM with TO.
122 (when (= (aref string idx) from)
123 (aset string idx to))
126 (string-as-unibyte . identity)
127 (string-as-multibyte . identity)
128 (multibyte-string-p . ignore))))
131 (defalias 'mm-char-or-char-int-p
133 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
134 ((fboundp 'char-valid-p) 'char-valid-p)
138 (defalias 'mm-read-coding-system
140 ((fboundp 'read-coding-system)
141 (if (and (featurep 'xemacs)
142 (<= (string-to-number emacs-version) 21.1))
143 (lambda (prompt &optional default-coding-system)
144 (read-coding-system prompt))
145 'read-coding-system))
146 (t (lambda (prompt &optional default-coding-system)
147 "Prompt the user for a coding system."
149 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
150 mm-mime-mule-charset-alist)))))))
152 (defvar mm-coding-system-list nil)
153 (defun mm-get-coding-system-list ()
154 "Get the coding system list."
155 (or mm-coding-system-list
156 (setq mm-coding-system-list (mm-coding-system-list))))
158 (defun mm-coding-system-p (sym)
159 "Return non-nil if SYM is a coding system."
160 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
161 (memq sym (mm-get-coding-system-list))))
163 (defvar mm-charset-synonym-alist
165 (gb2312 . cn-gb-2312)
167 ;; Windows-1252 is actually a superset of Latin-1. See also
168 ;; `gnus-article-dumbquotes-map'.
169 ,(unless (mm-coding-system-p 'windows-1252) ; should be defined eventually
170 '(windows-1252 . iso-8859-1))
171 ;; Windows-1250 is a variant of Latin-2 heavily used by Microsoft
172 ;; Outlook users in Czech republic. Use this to allow reading of their
173 ;; e-mails. cp1250 should be defined by M-x codepage-setup.
174 ,(unless (mm-coding-system-p 'windows-1250) ; should be defined eventually
175 '(windows-1250 . cp1250))
177 "A mapping from invalid charset names to the real charset names.")
179 (defvar mm-binary-coding-system
181 ((mm-coding-system-p 'binary) 'binary)
182 ((mm-coding-system-p 'no-conversion) 'no-conversion)
184 "100% binary coding system.")
186 (defvar mm-text-coding-system
187 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
188 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
189 (and (mm-coding-system-p 'raw-text) 'raw-text))
190 mm-binary-coding-system)
191 "Text-safe coding system (For removing ^M).")
193 (defvar mm-text-coding-system-for-write nil
194 "Text coding system for write.")
196 (defvar mm-auto-save-coding-system
198 ((mm-coding-system-p 'emacs-mule)
199 (if (memq system-type '(windows-nt ms-dos ms-windows))
200 (if (mm-coding-system-p 'emacs-mule-dos)
201 'emacs-mule-dos mm-binary-coding-system)
203 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
204 (t mm-binary-coding-system))
205 "Coding system of auto save file.")
207 (defvar mm-universal-coding-system mm-auto-save-coding-system
208 "The universal Coding system.")
210 ;;; Internal variables:
214 (defun mm-mule-charset-to-mime-charset (charset)
215 "Return the MIME charset corresponding to the given Mule CHARSET."
216 (let ((alist mm-mime-mule-charset-alist)
219 (when (memq charset (cdar alist))
220 (setq out (caar alist)
225 (defun mm-charset-to-coding-system (charset &optional lbt)
226 "Return coding-system corresponding to CHARSET.
227 CHARSET is a symbol naming a MIME charset.
228 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
229 used as the line break code type of the coding system."
230 (when (stringp charset)
231 (setq charset (intern (downcase charset))))
233 (or (cdr (assq charset mm-charset-synonym-alist))
236 (setq charset (intern (format "%s-%s" charset lbt))))
238 ;; Running in a non-MULE environment.
239 ((null (mm-get-coding-system-list))
242 ((eq charset 'us-ascii)
244 ;; Check to see whether we can handle this charset. (This depends
245 ;; on there being some coding system matching each `mime-charset'
246 ;; coding sysytem property defined, as there should be.)
247 ((memq charset (mm-get-coding-system-list))
253 (defsubst mm-replace-chars-in-string (string from to)
254 (mm-subst-char-in-string from to string))
256 (defsubst mm-enable-multibyte ()
257 "Set the multibyte flag of the current buffer.
258 Only do this if the default value of `enable-multibyte-characters' is
259 non-nil. This is a no-op in XEmacs."
260 (when (and (not (featurep 'xemacs))
261 (boundp 'default-enable-multibyte-characters)
262 default-enable-multibyte-characters
263 (fboundp 'set-buffer-multibyte))
264 (set-buffer-multibyte t)))
266 (defsubst mm-disable-multibyte ()
267 "Unset the multibyte flag of in the current buffer.
268 This is a no-op in XEmacs."
269 (when (and (not (featurep 'xemacs))
270 (fboundp 'set-buffer-multibyte))
271 (set-buffer-multibyte nil)))
273 (defsubst mm-enable-multibyte-mule4 ()
274 "Enable multibyte in the current buffer.
275 Only used in Emacs Mule 4."
276 (when (and (not (featurep 'xemacs))
277 (boundp 'default-enable-multibyte-characters)
278 default-enable-multibyte-characters
279 (fboundp 'set-buffer-multibyte)
281 (not (charsetp 'eight-bit-control)))
282 (set-buffer-multibyte t)))
284 (defsubst mm-disable-multibyte-mule4 ()
285 "Disable multibyte in the current buffer.
286 Only used in Emacs Mule 4."
287 (when (and (not (featurep 'xemacs))
288 (fboundp 'set-buffer-multibyte)
290 (not (charsetp 'eight-bit-control)))
291 (set-buffer-multibyte nil)))
293 (defun mm-preferred-coding-system (charset)
294 ;; A typo in some Emacs versions.
295 (or (get-charset-property charset 'prefered-coding-system)
296 (get-charset-property charset 'preferred-coding-system)))
298 (defun mm-charset-after (&optional pos)
299 "Return charset of a character in current buffer at position POS.
300 If POS is nil, it defauls to the current point.
301 If POS is out of range, the value is nil.
302 If the charset is `composition', return the actual one."
303 (let ((char (char-after pos)) charset)
304 (if (< (mm-char-int char) 128)
305 (setq charset 'ascii)
306 ;; charset-after is fake in some Emacsen.
307 (setq charset (and (fboundp 'char-charset) (char-charset char)))
308 (if (eq charset 'composition)
309 (let ((p (or pos (point))))
310 (cadr (find-charset-region p (1+ p))))
311 (if (and charset (not (memq charset '(ascii eight-bit-control
312 eight-bit-graphic))))
315 mail-parse-mule-charset ;; cached mule-charset
317 (setq mail-parse-mule-charset
318 (and (boundp 'current-language-environment)
321 (assoc current-language-environment
322 language-info-alist))))))
323 (if (or (not mail-parse-mule-charset)
324 (eq mail-parse-mule-charset 'ascii))
325 (setq mail-parse-mule-charset
326 (or (car (last (assq mail-parse-charset
327 mm-mime-mule-charset-alist)))
328 ;; Fixme: don't fix that!
330 mail-parse-mule-charset)))))))
332 (defun mm-mime-charset (charset)
333 "Return the MIME charset corresponding to the MULE CHARSET."
334 (if (eq charset 'unknown)
335 (error "The message contains non-printable characters, please use attachment"))
336 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
337 ;; This exists in Emacs 20.
339 (and (mm-preferred-coding-system charset)
341 (mm-preferred-coding-system charset) 'mime-charset))
342 (and (eq charset 'ascii)
344 (mm-preferred-coding-system charset)
345 (mm-mule-charset-to-mime-charset charset))
346 ;; This is for XEmacs.
347 (mm-mule-charset-to-mime-charset charset)))
349 (defun mm-delete-duplicates (list)
350 "Simple substitute for CL `delete-duplicates', testing with `equal'."
353 (setq head (car list))
354 (setq list (delete head list))
355 (setq result (cons head result)))
358 (defun mm-find-mime-charset-region (b e)
359 "Return the MIME charsets needed to encode the region between B and E."
360 (let ((charsets (mapcar 'mm-mime-charset
362 (mm-find-charset-region b e)))))
363 (when (memq 'iso-2022-jp-2 charsets)
364 (setq charsets (delq 'iso-2022-jp charsets)))
365 (setq charsets (mm-delete-duplicates charsets))
366 (if (and (> (length charsets) 1)
367 (fboundp 'find-coding-systems-region)
368 (let ((cs (find-coding-systems-region b e)))
369 (or (memq 'utf-8 cs) (memq 'mule-utf-8 cs))))
373 (defsubst mm-multibyte-p ()
374 "Say whether multibyte is enabled."
375 (if (and (not (featurep 'xemacs))
376 (boundp 'enable-multibyte-characters))
377 enable-multibyte-characters
380 (defmacro mm-with-unibyte-buffer (&rest forms)
381 "Create a temporary buffer, and evaluate FORMS there like `progn'.
382 Use unibyte mode for this."
383 `(let (default-enable-multibyte-characters)
384 (with-temp-buffer ,@forms)))
385 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
386 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
388 (defmacro mm-with-unibyte-current-buffer (&rest forms)
389 "Evaluate FORMS with current current buffer temporarily made unibyte.
390 Also bind `default-enable-multibyte-characters' to nil.
391 Equivalent to `progn' in XEmacs"
392 (let ((buffer (make-symbol "buffer")))
393 `(if (and (not (featurep 'xemacs))
394 (boundp 'enable-multibyte-characters)
395 enable-multibyte-characters
396 (fboundp 'set-buffer-multibyte))
397 (let ((,buffer (current-buffer)))
399 (let (default-enable-multibyte-characters)
400 (set-buffer-multibyte nil)
403 (set-buffer-multibyte t)))
404 (let (default-enable-multibyte-characters)
406 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
407 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
409 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
410 "Evaluate FORMS there like `progn' in current buffer.
412 (let ((buffer (make-symbol "buffer")))
413 `(if (and (not (featurep 'xemacs))
414 (boundp 'enable-multibyte-characters)
415 enable-multibyte-characters
416 (fboundp 'set-buffer-multibyte)
418 (not (charsetp 'eight-bit-control))) ;; For Emacs Mule 4 only.
419 (let ((,buffer (current-buffer)))
421 (let (default-enable-multibyte-characters)
422 (set-buffer-multibyte nil)
425 (set-buffer-multibyte t)))
426 (let (default-enable-multibyte-characters)
428 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
429 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
431 (defmacro mm-with-unibyte (&rest forms)
432 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
433 `(let (default-enable-multibyte-characters)
435 (put 'mm-with-unibyte 'lisp-indent-function 0)
436 (put 'mm-with-unibyte 'edebug-form-spec '(body))
438 (defun mm-find-charset-region (b e)
439 "Return a list of Emacs charsets in the region B to E."
441 ((and (mm-multibyte-p)
442 (fboundp 'find-charset-region))
443 ;; Remove composition since the base charsets have been included.
444 ;; Remove eight-bit-*, treat them as ascii.
445 (let ((css (find-charset-region b e)))
446 (mapcar (lambda (cs) (setq css (delq cs css)))
447 '(composition eight-bit-control eight-bit-graphic
451 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
454 (narrow-to-region b e)
455 (goto-char (point-min))
456 (skip-chars-forward "\0-\177")
461 (and (boundp 'current-language-environment)
462 (car (last (assq 'charset
463 (assoc current-language-environment
464 language-info-alist))))))
465 (if (eq charset 'ascii) (setq charset nil))
468 (car (last (assq mail-parse-charset
469 mm-mime-mule-charset-alist)))))
470 (list 'ascii (or charset 'latin-iso8859-1)))))))))
472 (if (fboundp 'shell-quote-argument)
473 (defalias 'mm-quote-arg 'shell-quote-argument)
474 (defun mm-quote-arg (arg)
475 "Return a version of ARG that is safe to evaluate in a shell."
476 (let ((pos 0) new-pos accum)
477 ;; *** bug: we don't handle newline characters properly
478 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
479 (push (substring arg pos new-pos) accum)
481 (push (list (aref arg new-pos)) accum)
482 (setq pos (1+ new-pos)))
485 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
487 (defun mm-auto-mode-alist ()
488 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
489 (let ((alist auto-mode-alist)
492 (when (listp (cdar alist))
493 (push (car alist) out))
497 (defvar mm-inhibit-file-name-handlers
498 '(jka-compr-handler image-file-handler)
499 "A list of handlers doing (un)compression (etc) thingies.")
501 (defun mm-insert-file-contents (filename &optional visit beg end replace
503 "Like `insert-file-contents', q.v., but only reads in the file.
504 A buffer may be modified in several ways after reading into the buffer due
505 to advanced Emacs features, such as file-name-handlers, format decoding,
506 find-file-hooks, etc.
507 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
508 This function ensures that none of these modifications will take place."
509 (let ((format-alist nil)
510 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
511 (default-major-mode 'fundamental-mode)
512 (enable-local-variables nil)
513 (after-insert-file-functions nil)
514 (enable-local-eval nil)
515 (find-file-hooks nil)
516 (inhibit-file-name-operation (if inhibit
517 'insert-file-contents
518 inhibit-file-name-operation))
519 (inhibit-file-name-handlers
521 (append mm-inhibit-file-name-handlers
522 inhibit-file-name-handlers)
523 inhibit-file-name-handlers)))
524 (insert-file-contents filename visit beg end replace)))
526 (defun mm-append-to-file (start end filename &optional codesys inhibit)
527 "Append the contents of the region to the end of file FILENAME.
528 When called from a function, expects three arguments,
529 START, END and FILENAME. START and END are buffer positions
530 saying what text to write.
531 Optional fourth argument specifies the coding system to use when
533 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
534 (let ((coding-system-for-write
535 (or codesys mm-text-coding-system-for-write
536 mm-text-coding-system))
537 (inhibit-file-name-operation (if inhibit
539 inhibit-file-name-operation))
540 (inhibit-file-name-handlers
542 (append mm-inhibit-file-name-handlers
543 inhibit-file-name-handlers)
544 inhibit-file-name-handlers)))
545 (append-to-file start end filename)))
547 (defun mm-write-region (start end filename &optional append visit lockname
548 coding-system inhibit)
550 "Like `write-region'.
551 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
552 (let ((coding-system-for-write
553 (or coding-system mm-text-coding-system-for-write
554 mm-text-coding-system))
555 (inhibit-file-name-operation (if inhibit
557 inhibit-file-name-operation))
558 (inhibit-file-name-handlers
560 (append mm-inhibit-file-name-handlers
561 inhibit-file-name-handlers)
562 inhibit-file-name-handlers)))
563 (write-region start end filename append visit lockname)))
565 (defun mm-image-load-path (&optional package)
567 (dolist (path load-path (nreverse result))
568 (if (file-directory-p
569 (setq dir (concat (file-name-directory
570 (directory-file-name path))
571 "etc/" (or package "gnus/"))))
573 (push path result))))
577 ;;; mm-util.el ends here