* riece-identity.el (riece-coding-system-for-identity): Moved from
[elisp/riece.git] / lisp / riece-coding.el
1 ;;; riece-coding.el --- converting string with coding system
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece, coding-system, MULE
7
8 ;; This file is part of Riece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU 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 'riece-globals)
28
29 (defgroup riece-coding nil
30   "Coding system."
31   :tag "Coding"
32   :prefix "riece-"
33   :group 'riece)
34
35 (defcustom riece-default-coding-system
36   (if (featurep 'mule)
37       (cons 'ctext 'iso-2022-jp-2))
38   "Coding system for process I/O.
39 The value is a coding system, or a cons cell (DECODING . ENCODING)
40 specifying the coding systems for decoding and encoding respectively."
41   :type '(choice (symbol :tag "Coding system")
42                  (cons (symbol :tag "Input coding system")
43                        (symbol :tag "Output coding system"))
44                  (const nil :tag "No conversion"))
45   :group 'riece-coding)
46
47 (defcustom riece-coding-system-alist nil
48   "An alist mapping from identities to coding-systems."
49   :type '(repeat (cons (string :tag "Identity")
50                        (symbol :tag "Coding system")))
51   :group 'riece-coding)
52
53 (defun riece-encode-coding-string (string)
54   (if (and (local-variable-p 'riece-coding-system (current-buffer))
55            riece-coding-system)         ;should be nil on non-Mule environment
56       (if (consp riece-coding-system)
57           (encode-coding-string string (cdr riece-coding-system))
58         (encode-coding-string string riece-coding-system))
59     string))
60
61 (defun riece-decode-coding-string (string)
62   (if (and (local-variable-p 'riece-coding-system (current-buffer))
63            riece-coding-system)         ;should be nil on non-Mule environment
64       (riece-decode-coding-string-1 string
65                                     (if (consp riece-coding-system)
66                                         (car riece-coding-system)
67                                       riece-coding-system))
68     string))
69
70 (defun riece-decode-coding-string-1 (string coding-system)
71   (let* ((decoded (decode-coding-string string coding-system))
72          (length (length decoded)))
73     (put-text-property 0 length 'riece-decoded-encoded-string
74                        string decoded)
75     (put-text-property 0 length 'riece-decoded-coding-system
76                        coding-system decoded)
77     decoded))
78
79 ;; The following functions are API used by handler functions.  For the
80 ;; meantime DECODED is actually a string (with some text properties).
81 ;; In the future, however, the implementation _should_ be changed so
82 ;; that decoding phase is delayed until the body of handler functions.
83 (defun riece-decoded-coding-system (decoded)
84   "Return the coding-system used for decoding DECODED."
85   (get-text-property 0 'riece-decoded-coding-system decoded))
86
87 (defun riece-decoded-encoded-string (decoded)
88   "Return the string before decoding."
89   (get-text-property 0 'riece-decoded-encoded-string decoded))
90
91 (defalias 'riece-decoded-string 'identity)
92
93 (provide 'riece-coding)
94
95 ;;; riece-coding.el ends here