f52994fad3b082e81a703ab244d67642d3fd055b
[elisp/apel.git] / emu-e20.el
1 ;;; emu-e20.el --- emu API implementation for Emacs/mule (delta)
2
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: emu-e20.el,v 7.0 1996/11/27 13:40:42 morioka Exp $
7 ;; Keywords: emulation, compatibility, Mule
8
9 ;; This file is part of tl (Tiny Library).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; 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 ;;; Code:
27
28 ;;; @ version specific features
29 ;;;
30
31 (require 'emu-19)
32
33 (defun fontset-pixel-size (fontset)
34   (require 'cl)
35   (let* ((info (fontset-info fontset))
36          (height (aref info 1))
37          )
38     (if (> height 0)
39         height
40       (let ((str (car (find-if (function identity) (aref info 2)))))
41         (if (string-match "--\\([0-9]+\\)-\\*-\\*-\\*-\\*-\\*-ISO8859-1" str)
42             (string-to-number
43              (substring str (match-beginning 1)(match-end 1))
44              )
45           0)))))
46
47
48 ;;; @ character set
49 ;;;
50
51 (defalias 'charset-columns 'charset-width)
52
53 (defun charset-iso-class (charset)
54   "Return ISO-class of CHARSET.
55 \(0/CLASS94, 1/CLASS96, 2/CLASS94x94, 3/CLASS96x96) [emu-e20.el]"
56   (aref (charset-info charset) 5)
57   )
58
59 (defun find-non-ascii-charset-string (string)
60   "Return a list of charsets in the STRING except ascii.
61 \[emu-e20.el; Mule emulating function]"
62   (delq charset-ascii (find-charset-string string))
63   )
64
65 (defun find-non-ascii-charset-region (start end)
66   "Return a list of charsets except ascii
67 in the region between START and END.
68 \[emu-e20.el; Mule emulating function]"
69   (delq charset-ascii (find-charset-string (buffer-substring start end)))
70   )
71
72
73 ;;; @ coding system
74 ;;;
75
76 (defconst *noconv* 'no-conversion)
77
78 (defmacro as-binary-process (&rest body)
79   `(let (selective-display      ; Disable ^M to nl translation.
80          ;; for Emacs/mule
81          (default-process-coding-system 'no-conversion)
82          )
83      ,@ body))
84
85 (defmacro as-binary-input-file (&rest body)
86   `(let ((coding-system-for-read 'no-conversion))
87      ,@body))
88
89 (defalias 'set-process-input-coding-system 'set-process-coding-system)
90
91
92 ;;; @ MIME charset
93 ;;;
94
95 (defvar charsets-mime-charset-alist
96   (list
97    (cons (list charset-ascii)                           'us-ascii)
98    (cons (list charset-ascii charset-latin-iso8859-1)   'iso-8859-1)
99    (cons (list charset-ascii charset-latin-iso8859-2)   'iso-8859-2)
100    (cons (list charset-ascii charset-latin-iso8859-3)   'iso-8859-3)
101    (cons (list charset-ascii charset-latin-iso8859-4)   'iso-8859-4)
102 ;;;(cons (list charset-ascii
103 ;;;            charset-cyrillic-iso8859-5)              'iso-8859-5)
104    (cons (list charset-ascii
105                charset-cyrillic-iso8859-5)              'koi8-r)
106    (cons (list charset-ascii charset-arabic-iso8859-6)  'iso-8859-6)
107    (cons (list charset-ascii charset-greek-iso8859-7)   'iso-8859-7)
108    (cons (list charset-ascii charset-hebrew-iso8859-8)  'iso-8859-8)
109    (cons (list charset-ascii charset-latin-iso8859-9)   'iso-8859-9)
110    (cons (list charset-ascii
111                charset-latin-jisx0201
112                charset-japanese-jisx0208-1978
113                charset-japanese-jisx0208)               'iso-2022-jp)
114    (cons (list charset-ascii charset-korean-ksc5601)    'euc-kr)
115    (cons (list charset-ascii charset-chinese-gb2312)    'cn-gb-2312)
116    (cons (list charset-ascii
117                charset-chinese-big5-1
118                charset-chinese-big5-2)                  'cn-big5)
119    (cons (list charset-ascii charset-latin-iso8859-1
120                charset-greek-iso8859-7
121                charset-latin-jisx0201
122                charset-japanese-jisx0208-1978
123                charset-chinese-gb2312
124                charset-japanese-jisx0208
125                charset-korean-ksc5601
126                charset-japanese-jisx0212)               'iso-2022-jp-2)
127    (cons (list charset-ascii charset-latin-iso8859-1
128                charset-greek-iso8859-7
129                charset-latin-jisx0201
130                charset-japanese-jisx0208-1978
131                charset-chinese-gb2312
132                charset-japanese-jisx0208
133                charset-korean-ksc5601
134                charset-japanese-jisx0212
135                charset-chinese-cns11643-1
136                charset-chinese-cns11643-2)              'iso-2022-int-1)
137    (cons (list charset-ascii charset-latin-iso8859-1
138                charset-latin-iso8859-2
139                charset-cyrillic-iso8859-5
140                charset-greek-iso8859-7
141                charset-latin-jisx0201
142                charset-japanese-jisx0208-1978
143                charset-chinese-gb2312
144                charset-japanese-jisx0208
145                charset-korean-ksc5601
146                charset-japanese-jisx0212
147                charset-chinese-cns11643-1
148                charset-chinese-cns11643-2
149                charset-chinese-cns11643-3
150                charset-chinese-cns11643-4
151                charset-chinese-cns11643-5
152                charset-chinese-cns11643-6
153                charset-chinese-cns11643-7)              'iso-2022-int-1)
154    ))
155
156 (defvar default-mime-charset 'x-ctext)
157
158 (defvar mime-charset-coding-system-alist
159   '((x-ctext            . coding-system-ctext)
160     (hz-gb-2312         . coding-system-hz)
161     (cn-gb-2312         . coding-system-euc-china)
162     (gb2312             . coding-system-euc-china)
163     (cn-big5            . coding-system-big5)
164     (iso-2022-jp-2      . coding-system-iso-2022-ss2-7)
165     (iso-2022-int-1     . coding-system-iso-2022-int)
166     (shift_jis          . coding-system-sjis)
167     ))
168
169 (defun mime-charset-to-coding-system (charset &optional lbt)
170   (if (stringp charset)
171       (setq charset (intern (downcase charset)))
172     )
173   (let ((cs
174          (or (cdr (assq charset mime-charset-coding-system-alist))
175              (let ((cs (intern (concat "coding-system-"
176                                        (symbol-name charset)))))
177                (and (coding-system-p cs) cs)
178                ))))
179     (if lbt
180         (intern (concat (symbol-name cs) "-" (symbol-name lbt)))
181       cs)))
182
183 (defun detect-mime-charset-region (start end)
184   "Return MIME charset for region between START and END. [emu-e20.el]"
185   (charsets-to-mime-charset
186    (find-charset-string (buffer-substring start end))
187    ))
188
189 (defun encode-mime-charset-region (start end charset)
190   "Encode the text between START and END as MIME CHARSET. [emu-e20.el]"
191   (let ((cs (mime-charset-to-coding-system charset)))
192     (if cs
193         (encode-coding-region start end cs)
194       )))
195
196 (defun decode-mime-charset-region (start end charset)
197   "Decode the text between START and END as MIME CHARSET. [emu-e20.el]"
198   (let ((cs (mime-charset-to-coding-system charset)))
199     (if cs
200         (decode-coding-region start end cs)
201       )))
202
203 (defun encode-mime-charset-string (string charset)
204   "Encode the STRING as MIME CHARSET. [emu-e20.el]"
205   (let ((cs (mime-charset-to-coding-system charset)))
206     (if cs
207         (encode-coding-string string cs)
208       string)))
209
210 (defun decode-mime-charset-string (string charset)
211   "Decode the STRING as MIME CHARSET. [emu-e20.el]"
212   (let ((cs (mime-charset-to-coding-system charset)))
213     (if cs
214         (decode-coding-string string cs)
215       string)))
216
217
218 ;;; @ character
219 ;;;
220
221 (defalias 'char-length 'char-bytes)
222
223 (defalias 'char-columns 'char-width)
224
225
226 ;;; @@ Mule emulating aliases
227 ;;;
228 ;;; You should not use them.
229
230 (defalias 'make-character 'make-char)
231
232 (defun char-category (character)
233   "Return string of category mnemonics for CHAR in TABLE.
234 CHAR can be any multilingual character
235 TABLE defaults to the current buffer's category table.
236 \[emu-e20.el; Mule emulating function]"
237   (category-set-mnemonics (char-category-set character))
238   )
239
240
241 ;;; @ string
242 ;;;
243
244 (defalias 'string-columns 'string-width)
245
246 (defalias 'sset 'string-embed-string)
247
248 (defun string-to-char-list (string)
249   "Return a list of which elements are characters in the STRING.
250 \[emu-e20.el; Mule 2.3 emulating function]"
251   (let* ((len (length string))
252          (i 0)
253          l chr)
254     (while (< i len)
255       (setq chr (sref string i))
256       (setq l (cons chr l))
257       (setq i (+ i (char-bytes chr)))
258       )
259     (nreverse l)
260     ))
261
262 (defalias 'string-to-int-list 'string-to-char-list)
263
264
265 ;;; @ regulation
266 ;;;
267
268 (defun regulate-latin-char (chr)
269   (cond ((and (<= ?\e$B#A\e(B chr)(<= chr ?\e$B#Z\e(B))
270          (+ (- chr ?\e$B#A\e(B) ?A)
271          )
272         ((and (<= ?\e$B#a\e(B chr)(<= chr ?\e$B#z\e(B))
273          (+ (- chr ?\e$B#a\e(B) ?a)
274          )
275         ((eq chr ?\e$B!%\e(B) ?.)
276         ((eq chr ?\e$B!$\e(B) ?,)
277         (t chr)
278         ))
279
280 (defun regulate-latin-string (str)
281   (let ((len (length str))
282         (i 0)
283         chr (dest ""))
284     (while (< i len)
285       (setq chr (sref str i))
286       (setq dest (concat dest
287                          (char-to-string (regulate-latin-char chr))))
288       (setq i (+ i (char-bytes chr)))
289       )
290     dest))
291
292
293 ;;; @ end
294 ;;;
295
296 (provide 'emu-e20)
297
298 ;;; emu-e20.el ends here