update.
[elisp/apel.git] / mcharset.el
1 ;;; mcharset.el --- MIME charset API
2
3 ;; Copyright (C) 1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: emulation, compatibility, Mule
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
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.
14
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.
19
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.
24
25 ;;; Code:
26
27 (require 'poe)
28 (require 'pcustom)
29
30 (cond ((featurep 'mule)
31        (if (>= emacs-major-version 20)
32            (require 'mcs-20)
33          ;; for MULE 1.* and 2.*
34          (require 'mcs-om)))
35       ((boundp 'NEMACS)
36        ;; for Nemacs and Nepoch
37        (require 'mcs-nemacs))
38       (t
39        (require 'mcs-ltn1)))
40
41 (defcustom default-mime-charset-for-write
42   (if (and (fboundp 'find-coding-system)
43            (find-coding-system 'utf-8))
44       'utf-8
45     default-mime-charset)
46   "Default value of MIME-charset for encoding.
47 It may be used when suitable MIME-charset is not found.
48 It must be symbol."
49   :group 'i18n
50   :type 'mime-charset)
51
52 (defcustom default-mime-charset-detect-method-for-write
53   nil
54   "Function called when suitable MIME-charset is not found to encode.
55 It must be nil or function.
56 If it is nil, variable `default-mime-charset-for-write' is used.
57 If it is a function, interface must be (TYPE CHARSETS &rest ARGS).
58 CHARSETS is list of charset.
59 If TYPE is 'region, ARGS has START and END."
60   :group 'i18n
61   :type '(choice function (const nil)))
62
63 (defun charsets-to-mime-charset (charsets)
64   "Return MIME charset from list of charset CHARSETS.
65 Return nil if suitable mime-charset is not found."
66   (if charsets
67       (catch 'tag
68         (let ((rest charsets-mime-charset-alist)
69               cell)
70           (while (setq cell (car rest))
71             (if (catch 'not-subset
72                   (let ((set1 charsets)
73                         (set2 (car cell))
74                         obj)
75                     (while set1
76                       (setq obj (car set1))
77                       (or (memq obj set2)
78                           (throw 'not-subset nil))
79                       (setq set1 (cdr set1)))
80                     t))
81                 (throw 'tag (cdr cell)))
82             (setq rest (cdr rest)))
83           ))))
84
85 (defun find-mime-charset-by-charsets (charsets &optional mode &rest args)
86   "Like `charsets-to-mime-charset', but it does not return nil.
87
88 When suitable mime-charset is not found and variable
89 `default-mime-charset-detect-method-for-write' is not nil,
90 `find-mime-charset-by-charsets' calls the variable as function and
91 return the return value of the function.
92 Interface of the function is (MODE CHARSETS &rest ARGS).
93
94 When suitable mime-charset is not found and variable
95 `default-mime-charset-detect-method-for-write' is nil,
96 variable `default-mime-charset-for-write' is returned."
97   (or (charsets-to-mime-charset charsets)
98       (if default-mime-charset-detect-method-for-write
99           (apply default-mime-charset-detect-method-for-write
100                  mode charsets args)
101         default-mime-charset-for-write)))
102
103
104 ;;; @ end
105 ;;;
106
107 (require 'product)
108 (product-provide (provide 'mcharset) (require 'apel-ver))
109
110 ;;; mcharset.el ends here