Update.
[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-20)
32
33
34 ;;; @ fix coding-system definition
35 ;;;
36
37 ;; It seems not bug, but I can not permit it...
38 (and (coding-system-property 'iso-2022-jp 'input-charset-conversion)
39      (copy-coding-system 'iso-2022-7bit 'iso-2022-jp))
40
41 ;; Redefine if -{dos|mac|unix} is not found.
42 (or (find-coding-system 'raw-text-dos)
43     (copy-coding-system 'no-conversion-dos 'raw-text-dos))
44 (or (find-coding-system 'raw-text-mac)
45     (copy-coding-system 'no-conversion-mac 'raw-text-mac))
46 (or (find-coding-system 'raw-text-unix)
47     (copy-coding-system 'no-conversion-unix 'raw-text-unix))
48
49 (or (find-coding-system 'ctext-dos)
50     (make-coding-system
51      'ctext 'iso2022
52      "Coding-system used in X as Compound Text Encoding."
53      '(charset-g0 ascii charset-g1 latin-iso8859-1
54                   eol-type nil
55                   mnemonic "CText")))
56
57 (or (find-coding-system 'iso-2022-jp-2-dos)
58     (make-coding-system
59      'iso-2022-jp-2 'iso2022
60      "ISO-2022 coding system using SS2 for 96-charset in 7-bit code."
61      '(charset-g0 ascii
62        charset-g2 t ;; unspecified but can be used later.
63        seven t
64        short t
65        mnemonic "ISO7/SS2"
66        eol-type nil)))
67
68 (or (find-coding-system 'euc-kr-dos)
69     (make-coding-system
70      'euc-kr 'iso2022
71      "Coding-system of Korean EUC (Extended Unix Code)."
72      '(charset-g0 ascii charset-g1 korean-ksc5601
73                   mnemonic "ko/EUC"
74                   eol-type nil)))
75
76
77 ;;; @ without code-conversion
78 ;;;
79
80 (define-obsolete-function-alias 'insert-binary-file-contents
81   'insert-file-contents-as-binary)
82
83 (defun insert-binary-file-contents-literally (filename
84                                               &optional visit beg end replace)
85   "Like `insert-file-contents-literally', q.v., but don't code conversion.
86 A buffer may be modified in several ways after reading into the buffer due
87 to advanced Emacs features, such as file-name-handlers, format decoding,
88 find-file-hooks, etc.
89   This function ensures that none of these modifications will take place."
90   (let ((coding-system-for-read 'binary))
91     ;; Returns list absolute file name and length of data inserted.
92     (insert-file-contents-literally filename visit beg end replace)))
93
94     
95 ;;; @ MIME charset
96 ;;;
97
98 (defun encode-mime-charset-region (start end charset)
99   "Encode the text between START and END as MIME CHARSET."
100   (let ((cs (mime-charset-to-coding-system charset)))
101     (if cs
102         (encode-coding-region start end cs)
103       )))
104
105 (defcustom mime-charset-decoder-alist
106   '((iso-2022-jp . decode-mime-charset-region-with-iso646-unification)
107     (iso-2022-jp-2 . decode-mime-charset-region-with-iso646-unification)
108     (x-ctext . decode-mime-charset-region-with-iso646-unification)
109     (hz-gb-2312 . decode-mime-charset-region-for-hz)
110     (t . decode-mime-charset-region-default))
111   "Alist MIME-charset vs. decoder function."
112   :group 'i18n
113   :type '(repeat (cons mime-charset function)))
114
115 (defsubst decode-mime-charset-region-default (start end charset lbt)
116   (let ((cs (mime-charset-to-coding-system charset lbt)))
117     (if cs
118         (decode-coding-region start end cs)
119       )))
120
121 (defcustom mime-iso646-character-unification-alist
122   `,(let (dest
123           (i 33))
124       (while (< i 92)
125         (setq dest
126               (cons (cons (char-to-string (make-char 'latin-jisx0201 i))
127                           (format "%c" i))
128                     dest))
129         (setq i (1+ i)))
130       (setq i 93)
131       (while (< i 126)
132         (setq dest
133               (cons (cons (char-to-string (make-char 'latin-jisx0201 i))
134                           (format "%c" i))
135                     dest))
136         (setq i (1+ i)))
137       (nreverse dest))
138   "Alist unified string vs. canonical string."
139   :group 'i18n
140   :type '(repeat (cons string string)))
141
142 (defcustom mime-unified-character-face nil
143   "*Face of unified character."
144   :group 'i18n
145   :type 'face)
146
147 (defcustom mime-character-unification-limit-size 2048
148   "*Limit size to unify characters."
149   :group 'i18n
150   :type 'integer)
151
152 (defun decode-mime-charset-region-with-iso646-unification (start end charset
153                                                                  lbt)
154   (decode-mime-charset-region-default start end charset lbt)
155   (if (<= (- end start) mime-character-unification-limit-size)
156       (save-excursion
157         (let ((rest mime-iso646-character-unification-alist))
158           (while rest
159             (let ((pair (car rest)))
160               (goto-char (point-min))
161               (while (search-forward (car pair) nil t)
162                 (let ((str (cdr pair)))
163                   (put-text-property 0 (length str)
164                                      'face mime-unified-character-face str)
165                   (replace-match str 'fixed-case 'literal)
166                   )
167                 ))
168             (setq rest (cdr rest)))))
169     ))
170
171 (defun decode-mime-charset-region-for-hz (start end charset lbt)
172   (if lbt
173       (save-restriction
174         (narrow-to-region start end)
175         (decode-coding-region (point-min)(point-max)
176                               (mime-charset-to-coding-system 'raw-text lbt))
177         (decode-hz-region (point-min)(point-max)))
178     (decode-hz-region start end)))
179
180 (defun decode-mime-charset-region (start end charset &optional lbt)
181   "Decode the text between START and END as MIME CHARSET."
182   (if (stringp charset)
183       (setq charset (intern (downcase charset)))
184     )
185   (let ((func (cdr (or (assq charset mime-charset-decoder-alist)
186                        (assq t mime-charset-decoder-alist)))))
187     (funcall func start end charset lbt)))
188
189 (defsubst encode-mime-charset-string (string charset)
190   "Encode the STRING as MIME CHARSET."
191   (let ((cs (mime-charset-to-coding-system charset)))
192     (if cs
193         (encode-coding-string string cs)
194       string)))
195
196 ;; (defsubst decode-mime-charset-string (string charset)
197 ;;   "Decode the STRING as MIME CHARSET."
198 ;;   (let ((cs (mime-charset-to-coding-system charset)))
199 ;;     (if cs
200 ;;         (decode-coding-string string cs)
201 ;;       string)))
202 (defun decode-mime-charset-string (string charset &optional lbt)
203   "Decode the STRING as MIME CHARSET."
204   (with-temp-buffer
205     (insert string)
206     (decode-mime-charset-region (point-min)(point-max) charset lbt)
207     (buffer-string)))
208
209
210 (defvar charsets-mime-charset-alist
211   '(((ascii)                                            . us-ascii)
212     ((ascii latin-iso8859-1)                            . iso-8859-1)
213     ((ascii latin-iso8859-2)                            . iso-8859-2)
214     ((ascii latin-iso8859-3)                            . iso-8859-3)
215     ((ascii latin-iso8859-4)                            . iso-8859-4)
216     ((ascii cyrillic-iso8859-5)                         . iso-8859-5)
217 ;;; ((ascii cyrillic-iso8859-5)                         . koi8-r)
218     ((ascii arabic-iso8859-6)                           . iso-8859-6)
219     ((ascii greek-iso8859-7)                            . iso-8859-7)
220     ((ascii hebrew-iso8859-8)                           . iso-8859-8)
221     ((ascii latin-iso8859-9)                            . iso-8859-9)
222     ((ascii latin-jisx0201
223             japanese-jisx0208-1978 japanese-jisx0208)   . iso-2022-jp)
224     ((ascii latin-jisx0201
225             katakana-jisx0201 japanese-jisx0208)        . shift_jis)
226     ((ascii korean-ksc5601)                             . euc-kr)
227     ((ascii chinese-gb2312)                             . cn-gb-2312)
228     ((ascii chinese-big5-1 chinese-big5-2)              . cn-big5)
229     ((ascii latin-iso8859-1 greek-iso8859-7
230             latin-jisx0201 japanese-jisx0208-1978
231             chinese-gb2312 japanese-jisx0208
232             korean-ksc5601 japanese-jisx0212)           . iso-2022-jp-2)
233     ((ascii latin-iso8859-1 greek-iso8859-7
234             latin-jisx0201 japanese-jisx0208-1978
235             chinese-gb2312 japanese-jisx0208
236             korean-ksc5601 japanese-jisx0212
237             chinese-cns11643-1 chinese-cns11643-2)      . iso-2022-int-1)
238     ((ascii latin-iso8859-1 latin-iso8859-2
239             cyrillic-iso8859-5 greek-iso8859-7
240             latin-jisx0201 japanese-jisx0208-1978
241             chinese-gb2312 japanese-jisx0208
242             korean-ksc5601 japanese-jisx0212
243             chinese-cns11643-1 chinese-cns11643-2
244             chinese-cns11643-3 chinese-cns11643-4
245             chinese-cns11643-5 chinese-cns11643-6
246             chinese-cns11643-7)                         . iso-2022-int-1)
247     ))
248
249
250 ;;; @ buffer representation
251 ;;;
252
253 (defsubst-maybe set-buffer-multibyte (flag)
254   "Set the multibyte flag of the current buffer to FLAG.
255 If FLAG is t, this makes the buffer a multibyte buffer.
256 If FLAG is nil, this makes the buffer a single-byte buffer.
257 The buffer contents remain unchanged as a sequence of bytes
258 but the contents viewed as characters do change.
259 \[Emacs 20.3 emulating function]"
260   flag)
261
262
263 ;;; @ character
264 ;;;
265
266 ;; avoid bug of XEmacs
267 (or (integerp (cdr (split-char ?a)))
268     (defun split-char (char)
269       "Return list of charset and one or two position-codes of CHAR."
270       (let ((charset (char-charset char)))
271         (if (eq charset 'ascii)
272             (list charset (char-int char))
273           (let ((i 0)
274                 (len (charset-dimension charset))
275                 (code (if (integerp char)
276                           char
277                         (char-int char)))
278                 dest)
279             (while (< i len)
280               (setq dest (cons (logand code 127) dest)
281                     code (lsh code -7)
282                     i (1+ i)))
283             (cons charset dest)))))
284     )
285
286 (defmacro char-next-index (char index)
287   "Return index of character succeeding CHAR whose index is INDEX."
288   `(1+ ,index))
289
290 ;;; @@ Mule emulating aliases
291 ;;;
292 ;;; You should not use them.
293
294 ;;(defalias 'char-leading-char 'char-charset)
295
296 (defun char-category (character)
297   "Return string of category mnemonics for CHAR in TABLE.
298 CHAR can be any multilingual character
299 TABLE defaults to the current buffer's category table."
300   (mapconcat (lambda (chr)
301                (char-to-string (int-char chr)))
302              (char-category-list character)
303              ""))
304
305
306 ;;; @ string
307 ;;;
308
309 (defun string-to-int-list (str)
310   (mapcar #'char-int str))
311
312 (defalias 'looking-at-as-unibyte 'looking-at)
313
314
315 ;;; @ end
316 ;;;
317
318 (provide 'emu-x20)
319
320 ;;; emu-x20.el ends here