2670cf681b5a49448544f0429a23f81bcbe8d40b
[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
44   (if (mime-charset-p 'iso-2022-jp-2)
45       'iso-2022-jp-2
46     default-mime-charset-for-write)
47   "Charset used in any transferred messages."
48   :type 'mime-charset
49   :group 'liece-coding)
50
51 (defcustom liece-mime-charset-for-read
52   (if (mime-charset-p 'x-ctext)
53       'x-ctext
54     default-mime-charset)
55   "Charset used in any transferred messages."
56   :type 'mime-charset
57   :group 'liece-coding)
58
59 (static-when (featurep 'xemacs)
60   (define-obsolete-variable-alias 'liece-default-mime-charset
61     'liece-mime-charset-for-write)
62   (define-obsolete-variable-alias 'liece-default-mime-charset-for-read
63     'liece-mime-charset-for-read))
64
65 (defcustom liece-detect-coding-system nil
66   "Whether use coding system auto detection."
67   :type 'boolean
68   :group 'liece-coding)
69
70 (defcustom liece-detect-coding-region-function
71   (function liece-detect-coding-region)
72   "User customizable `detect-coding-region'."
73   :type 'function
74   :group 'liece-coding)
75
76 (defcustom liece-detect-coding-string-function
77   (function liece-detect-coding-string)
78   "User customizable detect-coding-string."
79   :type 'function
80   :group 'liece-coding)
81
82 (defcustom liece-decode-coding-string-function
83   (function decode-coding-string)
84   "User customizable `decode-coding-string'."
85   :type 'function
86   :group 'liece-coding)
87
88 (defun liece-coding-encode-charset-region (start end &optional lbt)
89   (encode-mime-charset-region start end liece-mime-charset-for-write lbt))
90
91 (defun liece-coding-decode-charset-region (start end)
92   (let ((cs (if (and liece-detect-coding-system
93                      (fboundp liece-detect-coding-region-function))
94                 (funcall liece-detect-coding-region-function start end)
95               (mime-charset-to-coding-system liece-mime-charset-for-read))))
96     (decode-coding-region start end cs)))
97
98 (defun liece-detect-coding-region (start end)
99   (let ((cs (detect-coding-region start end)))
100     (if (listp cs)
101         (setq cs (car cs)))
102     (if (featurep 'xemacs)
103         (eval '(setq cs (coding-system-name cs))))
104     cs))
105
106 (defun liece-coding-encode-charset-string (str &optional lbt)
107   (encode-mime-charset-string str liece-mime-charset-for-write lbt))
108
109 (eval-and-compile
110   (when (fboundp 'detect-coding-string)
111     (defun liece-detect-coding-string (str)
112       (let ((cs (detect-coding-string str)))
113         (if (listp cs)
114             (setq cs (car cs)))
115         (static-if (fboundp 'coding-system-name)
116             (coding-system-name cs)
117           cs)))))
118
119 (defun liece-coding-decode-charset-string (str)
120   (let ((cs (or (and liece-detect-coding-system
121                      (fboundp liece-detect-coding-string-function)
122                      (funcall liece-detect-coding-string-function str))
123                 (mime-charset-to-coding-system liece-mime-charset-for-read))))
124     (funcall liece-decode-coding-string-function str cs)))
125
126 (provide 'liece-coding)
127
128 ;;; liece-coding.el ends here