deee74510f28ab032e54c4616a994f862d5996f6
[elisp/liece.git] / lisp / liece-coding.el
1 ;;; liece-coding.el --- Converting string with apropriate coding system.
2 ;; Copyright (C) 1998-2000 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Revised: 1999-06-02
7 ;; Keywords: IRC, liece, coding-system, MULE
8
9 ;; This file is part of Liece.
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26
27 ;;; Commentary:
28 ;; 
29
30 ;;; Code:
31
32 (require 'poem)
33 (require 'mcharset)
34
35 (eval-when-compile (require 'liece-compat))
36
37 (defgroup liece-coding nil
38   "Code conversion group"
39   :tag "Coding"
40   :prefix "liece-"
41   :group 'liece)
42   
43 (defcustom liece-mime-charset-for-write 'iso-2022-jp-2
44   "Charset used in any transferred messages."
45   :type 'mime-charset
46   :group 'liece-coding)
47
48 (defcustom liece-mime-charset-for-read 'x-ctext
49   "Charset used in any transferred messages."
50   :type 'mime-charset
51   :group 'liece-coding)
52
53 (static-when (featurep 'xemacs)
54   (define-obsolete-variable-alias 'liece-default-mime-charset
55     'liece-mime-charset-for-write)
56   (define-obsolete-variable-alias 'liece-default-mime-charset-for-read
57     'liece-mime-charset-for-read))
58
59 (defcustom liece-detect-coding-system nil
60   "Whether use coding system auto detection."
61   :type 'boolean
62   :group 'liece-coding)
63
64 (defcustom liece-detect-coding-region-function
65   (function liece-detect-coding-region)
66   "User customizable `detect-coding-region'."
67   :type 'function
68   :group 'liece-coding)
69
70 (defcustom liece-detect-coding-string-function
71   (function liece-detect-coding-string)
72   "User customizable detect-coding-string."
73   :type 'function
74   :group 'liece-coding)
75
76 (defcustom liece-decode-coding-string-function
77   (function decode-coding-string)
78   "User customizable `decode-coding-string'."
79   :type 'function
80   :group 'liece-coding)
81
82 (defun liece-coding-encode-charset-region (start end &optional lbt)
83   (encode-mime-charset-region start end liece-mime-charset-for-write lbt))
84
85 (defun liece-coding-decode-charset-region (start end)
86   (let ((cs (if (and liece-detect-coding-system
87                      (fboundp liece-detect-coding-region-function))
88                 (funcall liece-detect-coding-region-function start end)
89               (mime-charset-to-coding-system liece-mime-charset-for-read))))
90     (decode-coding-region start end cs)))
91
92 (defun liece-detect-coding-region (start end)
93   (let ((cs (detect-coding-region start end)))
94     (if (listp cs)
95         (setq cs (car cs)))
96     (if (featurep 'xemacs)
97         (eval '(setq cs (coding-system-name cs))))
98     cs))
99
100 (defun liece-coding-encode-charset-string (str &optional lbt)
101   (encode-mime-charset-string str liece-mime-charset-for-write lbt))
102
103 (eval-and-compile
104   (when (fboundp 'detect-coding-string)
105     (defun liece-detect-coding-string (str)
106       (let ((cs (detect-coding-string str)))
107         (if (listp cs)
108             (setq cs (car cs)))
109         (static-if (fboundp 'coding-system-name)
110             (coding-system-name cs)
111           cs)))))
112
113 (defun liece-coding-decode-charset-string (str)
114   (let ((cs (or (and liece-detect-coding-system
115                      (fboundp liece-detect-coding-string-function)
116                      (funcall liece-detect-coding-string-function str))
117                 (mime-charset-to-coding-system liece-mime-charset-for-read))))
118     (funcall liece-decode-coding-string-function str cs)))
119
120 (provide 'liece-coding)
121
122 ;;; liece-coding.el ends here