1 ;;; mcs-20.el --- MIME charset implementation for Emacs 20 and XEmacs/mule
3 ;; Copyright (C) 1997,1998,1999 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, Mule
8 ;; This file is part of APEL (A Portable Emacs Library).
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This module requires Emacs 20.0.93, XEmacs 20.3-b5 (with mule)
34 (eval-when-compile (require 'wid-edit))
40 (defcustom mime-charset-coding-system-alist
42 '((us-ascii . raw-text)
45 (iso-2022-jp-2 . iso-2022-7bit-ss2)
47 (windows-874 . tis620)
50 (x-unknown . undecided)
54 (let ((pair (car rest)))
55 (or (find-coding-system (car pair))
56 (setq dest (cons pair dest))
58 (setq rest (cdr rest))
61 "Alist MIME CHARSET vs CODING-SYSTEM.
62 MIME CHARSET and CODING-SYSTEM must be symbol."
64 :type '(repeat (cons symbol coding-system)))
66 (defcustom mime-charset-to-coding-system-default-method
68 "Function called when suitable coding-system is not found from MIME-charset.
69 It must be nil or function.
70 If it is a function, interface must be (CHARSET LBT CODING-SYSTEM)."
72 :type '(choice function (const nil)))
74 (defun mime-charset-to-coding-system (charset &optional lbt)
75 "Return coding-system corresponding with CHARSET.
76 CHARSET is a symbol whose name is MIME charset.
77 If optional argument LBT (`CRLF', `LF', `CR', `unix', `dos' or `mac')
78 is specified, it is used as line break code type of coding-system."
80 (setq charset (intern (downcase charset)))
82 (let ((cs (assq charset mime-charset-coding-system-alist)))
88 (setq cs (intern (format "%s-%s" cs
89 (cond ((eq lbt 'CRLF) 'dos)
94 (if (find-coding-system cs)
96 (if mime-charset-to-coding-system-default-method
97 (funcall mime-charset-to-coding-system-default-method
101 (defvar widget-mime-charset-prompt-value-history nil
102 "History of input to `widget-mime-charset-prompt-value'.")
104 (define-widget 'mime-charset 'coding-system
108 :prompt-history 'widget-mime-charset-prompt-value-history
109 :prompt-value 'widget-mime-charset-prompt-value
110 :action 'widget-mime-charset-action)
112 (defun widget-mime-charset-prompt-value (widget prompt value unbound)
113 ;; Read mime-charset from minibuffer.
115 (completing-read (format "%s (default %s) " prompt value)
118 (list (symbol-name sym))))
119 (mime-charset-list)))))
121 (defun widget-mime-charset-action (widget &optional event)
122 ;; Read a mime-charset from the minibuffer.
124 (widget-mime-charset-prompt-value
126 (widget-apply widget :menu-tag-get)
127 (widget-value widget)
129 (widget-value-set widget answer)
130 (widget-apply widget :notify widget event)
133 (defcustom default-mime-charset 'x-ctext
134 "Default value of MIME-charset.
135 It is used when MIME-charset is not specified.
140 (defun detect-mime-charset-region (start end)
141 "Return MIME charset for region between START and END."
142 (let ((charsets (find-charset-region start end)))
143 (or (charsets-to-mime-charset charsets)
144 (if default-mime-charset-detect-method-for-write
145 (funcall default-mime-charset-detect-method-for-write
146 'region charsets start end)
147 default-mime-charset-for-write)
150 (defun write-region-as-mime-charset (charset start end filename
151 &optional append visit lockname)
152 "Like `write-region', q.v., but encode by MIME CHARSET."
153 (let ((coding-system-for-write
154 (or (mime-charset-to-coding-system charset)
156 (write-region start end filename append visit lockname)))
164 ;;; mcs-20.el ends here