This commit was manufactured by cvs2svn to create branch 'emu-unify'.
[elisp/apel.git] / emu-x20.el
1 ;;; emu-x20.el --- emu API implementation for XEmacs with mule
2
3 ;; Copyright (C) 1994,1995,1996,1997,1998 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, Mule, XEmacs
7
8 ;; This file is part of emu.
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 ;;; Commentary:
26
27 ;;    This module requires XEmacs 20.3-b5 or later with mule.
28
29 ;;; Code:
30
31 (require 'emu-xemacs)
32 (require 'emu-20)
33
34
35 (and (coding-system-property 'iso-2022-jp 'input-charset-conversion)
36      (copy-coding-system 'iso-2022-7bit 'iso-2022-jp))
37
38
39 ;;; @ binary access
40 ;;;
41
42 (defun insert-file-contents-as-binary (filename
43                                        &optional visit beg end replace)
44   "Like `insert-file-contents', q.v., but don't code and format conversion.
45 Like `insert-file-contents-literary', but it allows find-file-hooks,
46 automatic uncompression, etc.
47
48 Namely this function ensures that only format decoding and character
49 code conversion will not take place."
50   (let ((coding-system-for-read 'binary)
51         format-alist)
52     (insert-file-contents filename visit beg end replace)
53     ))
54
55 (define-obsolete-function-alias 'insert-binary-file-contents
56   'insert-file-contents-as-binary)
57
58 (defun insert-binary-file-contents-literally (filename
59                                               &optional visit beg end replace)
60   "Like `insert-file-contents-literally', q.v., but don't code conversion.
61 A buffer may be modified in several ways after reading into the buffer due
62 to advanced Emacs features, such as file-name-handlers, format decoding,
63 find-file-hooks, etc.
64   This function ensures that none of these modifications will take place."
65   (let ((coding-system-for-read 'binary))
66     (insert-file-contents-literally filename visit beg end replace)
67     ))
68
69     
70 ;;; @ MIME charset
71 ;;;
72
73 (defun encode-mime-charset-region (start end charset)
74   "Encode the text between START and END as MIME CHARSET."
75   (let ((cs (mime-charset-to-coding-system charset)))
76     (if cs
77         (encode-coding-region start end cs)
78       )))
79
80 (defcustom mime-charset-decoder-alist
81   '((iso-2022-jp . decode-mime-charset-region-with-iso646-unification)
82     (iso-2022-jp-2 . decode-mime-charset-region-with-iso646-unification)
83     (x-ctext . decode-mime-charset-region-with-iso646-unification)
84     (t . decode-mime-charset-region-default))
85   "Alist MIME-charset vs. decoder function."
86   :group 'i18n
87   :type '(repeat (cons mime-charset function)))
88
89 (defsubst decode-mime-charset-region-default (start end charset)
90   (let ((cs (mime-charset-to-coding-system charset)))
91     (if cs
92         (decode-coding-region start end cs)
93       )))
94
95 (defcustom mime-iso646-character-unification-alist
96   `,(let (dest
97           (i 33))
98       (while (< i 92)
99         (setq dest
100               (cons (cons (char-to-string (make-char 'latin-jisx0201 i))
101                           (format "%c" i))
102                     dest))
103         (setq i (1+ i)))
104       (setq i 93)
105       (while (< i 126)
106         (setq dest
107               (cons (cons (char-to-string (make-char 'latin-jisx0201 i))
108                           (format "%c" i))
109                     dest))
110         (setq i (1+ i)))
111       (nreverse dest))
112   "Alist unified string vs. canonical string."
113   :group 'i18n
114   :type '(repeat (cons string string)))
115
116 (defcustom mime-unified-character-face nil
117   "*Face of unified character."
118   :group 'i18n
119   :type 'face)
120
121 (defun decode-mime-charset-region-with-iso646-unification (start end charset)
122   (decode-mime-charset-region-default start end charset)
123   (save-excursion
124     (let ((rest mime-iso646-character-unification-alist))
125       (while rest
126         (let ((pair (car rest)))
127           (goto-char (point-min))
128           (while (search-forward (car pair) nil t)
129             (let ((str (cdr pair)))
130               (put-text-property 0 (length str)
131                                  'face mime-unified-character-face str)
132               (replace-match str 'fixed-case 'literal)
133               )
134             ))
135         (setq rest (cdr rest))))))
136
137 (defun decode-mime-charset-region (start end charset)
138   "Decode the text between START and END as MIME CHARSET."
139   (if (stringp charset)
140       (setq charset (intern (downcase charset)))
141     )
142   (let ((func (cdr (or (assq charset mime-charset-decoder-alist)
143                        (assq t mime-charset-decoder-alist)))))
144     (funcall func start end charset)
145     ))
146
147 (defsubst encode-mime-charset-string (string charset)
148   "Encode the STRING as MIME CHARSET."
149   (let ((cs (mime-charset-to-coding-system charset)))
150     (if cs
151         (encode-coding-string string cs)
152       string)))
153
154 (defsubst decode-mime-charset-string (string charset)
155   "Decode the STRING as MIME CHARSET."
156   (let ((cs (mime-charset-to-coding-system charset)))
157     (if cs
158         (decode-coding-string string cs)
159       string)))
160
161
162 (defvar charsets-mime-charset-alist
163   '(((ascii)                                            . us-ascii)
164     ((ascii latin-iso8859-1)                            . iso-8859-1)
165     ((ascii latin-iso8859-2)                            . iso-8859-2)
166     ((ascii latin-iso8859-3)                            . iso-8859-3)
167     ((ascii latin-iso8859-4)                            . iso-8859-4)
168     ((ascii cyrillic-iso8859-5)                         . iso-8859-5)
169 ;;; ((ascii cyrillic-iso8859-5)                         . koi8-r)
170     ((ascii arabic-iso8859-6)                           . iso-8859-6)
171     ((ascii greek-iso8859-7)                            . iso-8859-7)
172     ((ascii hebrew-iso8859-8)                           . iso-8859-8)
173     ((ascii latin-iso8859-9)                            . iso-8859-9)
174     ((ascii latin-jisx0201
175             japanese-jisx0208-1978 japanese-jisx0208)   . iso-2022-jp)
176     ((ascii korean-ksc5601)                             . euc-kr)
177     ((ascii chinese-gb2312)                             . cn-gb-2312)
178     ((ascii chinese-big5-1 chinese-big5-2)              . cn-big5)
179     ((ascii latin-iso8859-1 greek-iso8859-7
180             latin-jisx0201 japanese-jisx0208-1978
181             chinese-gb2312 japanese-jisx0208
182             korean-ksc5601 japanese-jisx0212)           . iso-2022-jp-2)
183     ((ascii latin-iso8859-1 greek-iso8859-7
184             latin-jisx0201 japanese-jisx0208-1978
185             chinese-gb2312 japanese-jisx0208
186             korean-ksc5601 japanese-jisx0212
187             chinese-cns11643-1 chinese-cns11643-2)      . iso-2022-int-1)
188     ((ascii latin-iso8859-1 latin-iso8859-2
189             cyrillic-iso8859-5 greek-iso8859-7
190             latin-jisx0201 japanese-jisx0208-1978
191             chinese-gb2312 japanese-jisx0208
192             korean-ksc5601 japanese-jisx0212
193             chinese-cns11643-1 chinese-cns11643-2
194             chinese-cns11643-3 chinese-cns11643-4
195             chinese-cns11643-5 chinese-cns11643-6
196             chinese-cns11643-7)                         . iso-2022-int-1)
197     ))
198
199 (defun detect-mime-charset-region (start end)
200   "Return MIME charset for region between START and END."
201   (charsets-to-mime-charset (charsets-in-region start end)))
202
203
204 ;;; @ character
205 ;;;
206
207 (defmacro char-next-index (char index)
208   "Return index of character succeeding CHAR whose index is INDEX."
209   `(1+ index))
210
211 ;;; @@ Mule emulating aliases
212 ;;;
213 ;;; You should not use them.
214
215 ;;(defalias 'char-leading-char 'char-charset)
216
217 (defun char-category (character)
218   "Return string of category mnemonics for CHAR in TABLE.
219 CHAR can be any multilingual character
220 TABLE defaults to the current buffer's category table."
221   (mapconcat (lambda (chr)
222                (char-to-string (int-char chr))
223                )
224              (char-category-list character)
225              ""))
226
227
228 ;;; @ string
229 ;;;
230
231 (defun string-to-int-list (str)
232   (mapcar #'char-int str)
233   )
234
235
236 ;;; @ end
237 ;;;
238
239 (provide 'emu-x20)
240
241 ;;; emu-x20.el ends here