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