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))
28 (eval-when-compile (require 'gnus-clfns))
29 (eval-when-compile (require 'static))
33 (defvar mm-mime-mule-charset-alist
35 (iso-8859-1 latin-iso8859-1)
36 (iso-8859-2 latin-iso8859-2)
37 (iso-8859-3 latin-iso8859-3)
38 (iso-8859-4 latin-iso8859-4)
39 (iso-8859-5 cyrillic-iso8859-5)
40 ;; Non-mule (X)Emacs uses the last mule-charset for 8bit characters.
41 ;; The fake mule-charset, gnus-koi8-r, tells Gnus that the default
42 ;; charset is koi8-r, not iso-8859-5.
43 (koi8-r cyrillic-iso8859-5 gnus-koi8-r)
44 (iso-8859-6 arabic-iso8859-6)
45 (iso-8859-7 greek-iso8859-7)
46 (iso-8859-8 hebrew-iso8859-8)
47 (iso-8859-9 latin-iso8859-9)
48 (iso-8859-14 latin-iso8859-14)
49 (iso-8859-15 latin-iso8859-15)
50 (viscii vietnamese-viscii-lower)
51 (iso-2022-jp latin-jisx0201 japanese-jisx0208 japanese-jisx0208-1978)
52 (euc-kr korean-ksc5601)
53 (gb2312 chinese-gb2312)
54 (big5 chinese-big5-1 chinese-big5-2)
56 (thai-tis620 thai-tis620)
57 (iso-2022-7bit ethiopic arabic-1-column arabic-2-column)
58 (iso-2022-jp-2 latin-iso8859-1 greek-iso8859-7
59 latin-jisx0201 japanese-jisx0208-1978
60 chinese-gb2312 japanese-jisx0208
61 korean-ksc5601 japanese-jisx0212
63 (iso-2022-int-1 latin-iso8859-1 greek-iso8859-7
64 latin-jisx0201 japanese-jisx0208-1978
65 chinese-gb2312 japanese-jisx0208
66 korean-ksc5601 japanese-jisx0212
67 chinese-cns11643-1 chinese-cns11643-2)
68 (iso-2022-int-1 latin-iso8859-1 latin-iso8859-2
69 cyrillic-iso8859-5 greek-iso8859-7
70 latin-jisx0201 japanese-jisx0208-1978
71 chinese-gb2312 japanese-jisx0208
72 korean-ksc5601 japanese-jisx0212
73 chinese-cns11643-1 chinese-cns11643-2
74 chinese-cns11643-3 chinese-cns11643-4
75 chinese-cns11643-5 chinese-cns11643-6
77 ,(if (or (not (fboundp 'charsetp)) ;; non-Mule case
78 (not (fboundp 'coding-system-p))
80 (not (coding-system-p 'mule-utf-8)))
81 '(utf-8 unicode-a unicode-b unicode-c unicode-d unicode-e)
82 ;; If we have utf-8 we're in Mule 5+.
83 (delete 'ascii (coding-system-get 'mule-utf-8 'safe-charsets))))
84 "Alist of MIME-charset/MULE-charsets.")
89 (let ((nfunc (intern (format "mm-%s" (car elem)))))
90 (if (fboundp (car elem))
91 (defalias nfunc (car elem))
92 (defalias nfunc (cdr elem)))))
93 '((decode-coding-string . (lambda (s a) s))
94 (encode-coding-string . (lambda (s a) s))
95 (encode-coding-region . ignore)
96 (coding-system-list . ignore)
97 (decode-coding-region . ignore)
99 (device-type . ignore)
100 (coding-system-equal . equal)
101 (annotationp . ignore)
102 (set-buffer-file-coding-system . ignore)
104 . (lambda (charset int)
108 "Prompt the user for a coding system."
110 prompt (mapcar (lambda (s) (list (symbol-name (car s))))
111 mm-mime-mule-charset-alist))))
118 (mapcar (lambda (e) (list (symbol-name (car e))))
119 mm-mime-mule-charset-alist)
121 (subst-char-in-string
122 . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
123 "Replace characters in STRING from FROM to TO."
124 (let ((string (substring string 0)) ;Copy string.
125 (len (length string))
127 ;; Replace all occurrences of FROM with TO.
129 (when (= (aref string idx) from)
130 (aset string idx to))
133 (string-as-unibyte . identity)
134 (multibyte-string-p . ignore)
138 (defalias 'mm-char-or-char-int-p
140 ((fboundp 'char-or-char-int-p) 'char-or-char-int-p)
141 ((fboundp 'char-valid-p) 'char-valid-p)
144 (defvar mm-coding-system-list nil)
145 (defun mm-get-coding-system-list ()
146 "Get the coding system list."
147 (or mm-coding-system-list
148 (setq mm-coding-system-list (mm-coding-system-list))))
150 (defun mm-coding-system-p (sym)
151 "Return non-nil if SYM is a coding system."
152 (or (and (fboundp 'coding-system-p) (coding-system-p sym))
153 (memq sym (mm-get-coding-system-list))))
155 (defvar mm-charset-synonym-alist
157 (gb2312 . cn-gb-2312)
159 ;; Windows-1252 is actually a superset of Latin-1. See also
160 ;; `gnus-article-dumbquotes-map'.
161 ,(unless (mm-coding-system-p 'windows-1252) ; should be defined eventually
162 '(windows-1252 . iso-8859-1))
164 "A mapping from invalid charset names to the real charset names.")
166 (defvar mm-binary-coding-system
168 ((mm-coding-system-p 'binary) 'binary)
169 ((mm-coding-system-p 'no-conversion) 'no-conversion)
171 "100% binary coding system.")
173 (defvar mm-text-coding-system
174 (or (if (memq system-type '(windows-nt ms-dos ms-windows))
175 (and (mm-coding-system-p 'raw-text-dos) 'raw-text-dos)
176 (and (mm-coding-system-p 'raw-text) 'raw-text))
177 mm-binary-coding-system)
178 "Text-safe coding system (For removing ^M).")
180 (defvar mm-text-coding-system-for-write nil
181 "Text coding system for write.")
183 (defvar mm-auto-save-coding-system
185 ((mm-coding-system-p 'emacs-mule)
186 (if (memq system-type '(windows-nt ms-dos ms-windows))
187 (if (mm-coding-system-p 'emacs-mule-dos)
188 'emacs-mule-dos mm-binary-coding-system)
190 ((mm-coding-system-p 'escape-quoted) 'escape-quoted)
191 (t mm-binary-coding-system))
192 "Coding system of auto save file.")
194 ;;; Internal variables:
198 (defun mm-mule-charset-to-mime-charset (charset)
199 "Return the MIME charset corresponding to the given Mule CHARSET."
200 (let ((alist mm-mime-mule-charset-alist)
203 (when (memq charset (cdar alist))
204 (setq out (caar alist)
209 (defun mm-charset-to-coding-system (charset &optional lbt)
210 "Return coding-system corresponding to CHARSET.
211 CHARSET is a symbol naming a MIME charset.
212 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
213 used as the line break code type of the coding system."
214 (when (stringp charset)
215 (setq charset (intern (downcase charset))))
217 (or (cdr (assq charset mm-charset-synonym-alist))
220 (setq charset (intern (format "%s-%s" charset lbt))))
222 ;; Running in a non-MULE environment.
223 ((null (mm-get-coding-system-list))
226 ((eq charset 'us-ascii)
228 ;; Check to see whether we can handle this charset. (This depends
229 ;; on there being some coding system matching each `mime-charset'
230 ;; coding sysytem property defined, as there should be.)
231 ((memq charset (mm-get-coding-system-list))
237 (defsubst mm-replace-chars-in-string (string from to)
238 (mm-subst-char-in-string from to string))
240 (defsubst mm-enable-multibyte ()
241 "Set the multibyte flag of the current buffer.
242 Only do this if the default value of `enable-multibyte-characters' is
243 non-nil. This is a no-op in XEmacs."
244 (when (and (not (featurep 'xemacs))
245 (boundp 'default-enable-multibyte-characters)
246 default-enable-multibyte-characters
247 (fboundp 'set-buffer-multibyte))
248 (set-buffer-multibyte t)))
250 (defsubst mm-disable-multibyte ()
251 "Unset the multibyte flag of in the current buffer.
252 This is a no-op in XEmacs."
253 (when (and (not (featurep 'xemacs))
254 (fboundp 'set-buffer-multibyte))
255 (set-buffer-multibyte nil)))
257 (defsubst mm-enable-multibyte-mule4 ()
258 "Enable multibyte in the current buffer.
259 Only used in Emacs Mule 4."
260 (when (and (not (featurep 'xemacs))
261 (boundp 'default-enable-multibyte-characters)
262 default-enable-multibyte-characters
263 (fboundp 'set-buffer-multibyte)
265 (not (charsetp 'eight-bit-control)))
266 (set-buffer-multibyte t)))
268 (defsubst mm-disable-multibyte-mule4 ()
269 "Disable multibyte in the current buffer.
270 Only used in Emacs Mule 4."
271 (when (and (not (featurep 'xemacs))
272 (fboundp 'set-buffer-multibyte)
274 (not (charsetp 'eight-bit-control)))
275 (set-buffer-multibyte nil)))
277 (defun mm-preferred-coding-system (charset)
278 ;; A typo in some Emacs versions.
279 (or (get-charset-property charset 'prefered-coding-system)
280 (get-charset-property charset 'preferred-coding-system)))
282 (defun mm-charset-after (&optional pos)
283 "Return charset of a character in current buffer at position POS.
284 If POS is nil, it defauls to the current point.
285 If POS is out of range, the value is nil.
286 If the charset is `composition', return the actual one."
287 (let ((char (char-after pos)) charset)
288 (if (< (mm-char-int char) 128)
289 (setq charset 'ascii)
290 ;; charset-after is fake in some Emacsen.
291 (setq charset (and (fboundp 'char-charset) (char-charset char)))
292 (if (eq charset 'composition)
293 (let ((p (or pos (point))))
294 (cadr (find-charset-region p (1+ p))))
295 (if (and charset (not (memq charset '(ascii eight-bit-control
296 eight-bit-graphic))))
299 mail-parse-mule-charset ;; cached mule-charset
301 (setq mail-parse-mule-charset
302 (and (boundp 'current-language-environment)
305 (assoc current-language-environment
306 language-info-alist))))))
307 (if (or (not mail-parse-mule-charset)
308 (eq mail-parse-mule-charset 'ascii))
309 (setq mail-parse-mule-charset
310 (or (car (last (assq mail-parse-charset
311 mm-mime-mule-charset-alist)))
312 ;; Fixme: don't fix that!
314 mail-parse-mule-charset)))))))
316 (defun mm-mime-charset (charset)
317 "Return the MIME charset corresponding to the MULE CHARSET."
318 (if (eq charset 'unknown)
319 (error "The message contains non-printable characters, please use attachment."))
320 (if (and (fboundp 'coding-system-get) (fboundp 'get-charset-property))
321 ;; This exists in Emacs 20.
323 (and (mm-preferred-coding-system charset)
325 (mm-preferred-coding-system charset) 'mime-charset))
326 (and (eq charset 'ascii)
328 (mm-preferred-coding-system charset)
329 (mm-mule-charset-to-mime-charset charset))
330 ;; This is for XEmacs.
331 (mm-mule-charset-to-mime-charset charset)))
333 (defun mm-delete-duplicates (list)
334 "Simple substitute for CL `delete-duplicates', testing with `equal'."
337 (setq head (car list))
338 (setq list (delete head list))
339 (setq result (cons head result)))
342 (defun mm-find-mime-charset-region (b e)
343 "Return the MIME charsets needed to encode the region between B and E."
344 (let ((charsets (mapcar 'mm-mime-charset
346 (mm-find-charset-region b e)))))
347 (when (memq 'iso-2022-jp-2 charsets)
348 (setq charsets (delq 'iso-2022-jp charsets)))
349 (setq charsets (mm-delete-duplicates charsets))
350 (if (and (> (length charsets) 1)
351 (fboundp 'find-coding-systems-region)
352 (let ((cs (find-coding-systems-region b e)))
353 (or (memq 'utf-8 cs) (memq 'mule-utf-8 cs))))
357 (defsubst mm-multibyte-p ()
358 "Say whether multibyte is enabled."
359 (if (and (not (featurep 'xemacs))
360 (boundp 'enable-multibyte-characters))
361 enable-multibyte-characters
364 (defmacro mm-with-unibyte-buffer (&rest forms)
365 "Create a temporary buffer, and evaluate FORMS there like `progn'.
366 Use unibyte mode for this."
367 `(let (default-enable-multibyte-characters)
368 (with-temp-buffer ,@forms)))
369 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
370 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
372 (defmacro mm-with-unibyte-current-buffer (&rest forms)
373 "Evaluate FORMS with current current buffer temporarily made unibyte.
374 Also bind `default-enable-multibyte-characters' to nil.
375 Equivalent to `progn' in XEmacs"
376 (let ((buffer (make-symbol "buffer")))
377 `(if (and (not (featurep 'xemacs))
378 (boundp 'enable-multibyte-characters)
379 enable-multibyte-characters
380 (fboundp 'set-buffer-multibyte))
381 (let ((,buffer (current-buffer)))
383 (let (default-enable-multibyte-characters)
384 (set-buffer-multibyte nil)
387 (set-buffer-multibyte t)))
388 (let (default-enable-multibyte-characters)
390 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
391 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
393 (defmacro mm-with-unibyte-current-buffer-mule4 (&rest forms)
394 "Evaluate FORMS there like `progn' in current buffer.
396 (let ((buffer (make-symbol "buffer")))
397 `(if (and (not (featurep 'xemacs))
398 (boundp 'enable-multibyte-characters)
399 enable-multibyte-characters
400 (fboundp 'set-buffer-multibyte)
402 (not (charsetp 'eight-bit-control))) ;; For Emacs Mule 4 only.
403 (let ((,buffer (current-buffer)))
405 (let (default-enable-multibyte-characters)
406 (set-buffer-multibyte nil)
409 (set-buffer-multibyte t)))
410 (let (default-enable-multibyte-characters)
412 (put 'mm-with-unibyte-current-buffer-mule4 'lisp-indent-function 0)
413 (put 'mm-with-unibyte-current-buffer-mule4 'edebug-form-spec '(body))
415 (defmacro mm-with-unibyte (&rest forms)
416 "Eval the FORMS with the default value of `enable-multibyte-characters' nil, ."
417 `(let (default-enable-multibyte-characters)
419 (put 'mm-with-unibyte 'lisp-indent-function 0)
420 (put 'mm-with-unibyte 'edebug-form-spec '(body))
422 (defun mm-find-charset-region (b e)
423 "Return a list of Emacs charsets in the region B to E."
425 ((and (mm-multibyte-p)
426 (fboundp 'find-charset-region))
427 ;; Remove composition since the base charsets have been included.
428 ;; Remove eight-bit-*, treat them as ascii.
429 (let ((css (find-charset-region b e)))
430 (mapcar (lambda (cs) (setq css (delq cs css)))
431 '(composition eight-bit-control eight-bit-graphic))
434 ;; We are in a unibyte buffer or XEmacs non-mule, so we futz around a bit.
437 (narrow-to-region b e)
438 (goto-char (point-min))
439 (skip-chars-forward "\0-\177")
444 (and (boundp 'current-language-environment)
445 (car (last (assq 'charset
446 (assoc current-language-environment
447 language-info-alist))))))
448 (if (eq charset 'ascii) (setq charset nil))
451 (car (last (assq mail-parse-charset
452 mm-mime-mule-charset-alist)))))
453 (list 'ascii (or charset 'latin-iso8859-1)))))))))
455 (static-if (fboundp 'shell-quote-argument)
456 (defalias 'mm-quote-arg 'shell-quote-argument)
457 (defun mm-quote-arg (arg)
458 "Return a version of ARG that is safe to evaluate in a shell."
459 (let ((pos 0) new-pos accum)
460 ;; *** bug: we don't handle newline characters properly
461 (while (setq new-pos (string-match "[]*[;!'`\"$\\& \t{} |()<>]" arg pos))
462 (push (substring arg pos new-pos) accum)
464 (push (list (aref arg new-pos)) accum)
465 (setq pos (1+ new-pos)))
468 (apply 'concat (nconc (nreverse accum) (list (substring arg pos))))))))
470 (defun mm-auto-mode-alist ()
471 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
472 (let ((alist auto-mode-alist)
475 (when (listp (cdar alist))
476 (push (car alist) out))
480 (defvar mm-inhibit-file-name-handlers
482 "A list of handlers doing (un)compression (etc) thingies.")
484 (defun mm-insert-file-contents (filename &optional visit beg end replace
486 "Like `insert-file-contents', q.v., but only reads in the file.
487 A buffer may be modified in several ways after reading into the buffer due
488 to advanced Emacs features, such as file-name-handlers, format decoding,
489 find-file-hooks, etc.
490 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers.
491 This function ensures that none of these modifications will take place."
492 (let ((format-alist nil)
493 (auto-mode-alist (if inhibit nil (mm-auto-mode-alist)))
494 (default-major-mode 'fundamental-mode)
495 (enable-local-variables nil)
496 (after-insert-file-functions nil)
497 (enable-local-eval nil)
498 (find-file-hooks nil)
499 (inhibit-file-name-operation (if inhibit
500 'insert-file-contents
501 inhibit-file-name-operation))
502 (inhibit-file-name-handlers
504 (append mm-inhibit-file-name-handlers
505 inhibit-file-name-handlers)
506 inhibit-file-name-handlers)))
507 (insert-file-contents filename visit beg end replace)))
509 (defun mm-append-to-file (start end filename &optional codesys inhibit)
510 "Append the contents of the region to the end of file FILENAME.
511 When called from a function, expects three arguments,
512 START, END and FILENAME. START and END are buffer positions
513 saying what text to write.
514 Optional fourth argument specifies the coding system to use when
516 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
517 (let ((coding-system-for-write
518 (or codesys mm-text-coding-system-for-write
519 mm-text-coding-system))
520 (inhibit-file-name-operation (if inhibit
522 inhibit-file-name-operation))
523 (inhibit-file-name-handlers
525 (append mm-inhibit-file-name-handlers
526 inhibit-file-name-handlers)
527 inhibit-file-name-handlers)))
528 (append-to-file start end filename)))
530 (defun mm-write-region (start end filename &optional append visit lockname
531 coding-system inhibit)
533 "Like `write-region'.
534 If INHIBIT is non-nil, inhibit mm-inhibit-file-name-handlers."
535 (let ((coding-system-for-write
536 (or coding-system mm-text-coding-system-for-write
537 mm-text-coding-system))
538 (inhibit-file-name-operation (if inhibit
540 inhibit-file-name-operation))
541 (inhibit-file-name-handlers
543 (append mm-inhibit-file-name-handlers
544 inhibit-file-name-handlers)
545 inhibit-file-name-handlers)))
546 (write-region start end filename append visit lockname)))
548 (defun mm-image-load-path (&optional package)
550 (dolist (path load-path (nreverse result))
551 (if (file-directory-p
552 (setq dir (concat (file-name-directory
553 (directory-file-name path))
554 "etc/" (or package "gnus/"))))
556 (push path result))))
560 ;;; mm-util.el ends here