e5623a15ad5431eb45b306b5cdcce59a713a1460
[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.2 1996/12/18 13:12:15 morioka Exp $
7 ;; Keywords: emulation, compatibility, Mule
8
9 ;; This file is part of emu.
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          (coding-system-for-read  'no-conversion)
82          (coding-system-for-write 'no-conversion)
83          )
84      ,@ body))
85
86 (defmacro as-binary-input-file (&rest body)
87   `(let ((coding-system-for-read 'no-conversion))
88      ,@body))
89
90 (defalias 'set-process-input-coding-system 'set-process-coding-system)
91
92
93 ;;; @ MIME charset
94 ;;;
95
96 (defvar charsets-mime-charset-alist
97   (list
98    (cons (list charset-ascii)                           'us-ascii)
99    (cons (list charset-ascii charset-latin-iso8859-1)   'iso-8859-1)
100    (cons (list charset-ascii charset-latin-iso8859-2)   'iso-8859-2)
101    (cons (list charset-ascii charset-latin-iso8859-3)   'iso-8859-3)
102    (cons (list charset-ascii charset-latin-iso8859-4)   'iso-8859-4)
103 ;;;(cons (list charset-ascii
104 ;;;            charset-cyrillic-iso8859-5)              'iso-8859-5)
105    (cons (list charset-ascii
106                charset-cyrillic-iso8859-5)              'koi8-r)
107    (cons (list charset-ascii charset-arabic-iso8859-6)  'iso-8859-6)
108    (cons (list charset-ascii charset-greek-iso8859-7)   'iso-8859-7)
109    (cons (list charset-ascii charset-hebrew-iso8859-8)  'iso-8859-8)
110    (cons (list charset-ascii charset-latin-iso8859-9)   'iso-8859-9)
111    (cons (list charset-ascii
112                charset-latin-jisx0201
113                charset-japanese-jisx0208-1978
114                charset-japanese-jisx0208)               'iso-2022-jp)
115    (cons (list charset-ascii charset-korean-ksc5601)    'euc-kr)
116    (cons (list charset-ascii charset-chinese-gb2312)    'cn-gb-2312)
117    (cons (list charset-ascii
118                charset-chinese-big5-1
119                charset-chinese-big5-2)                  'cn-big5)
120    (cons (list charset-ascii charset-latin-iso8859-1
121                charset-greek-iso8859-7
122                charset-latin-jisx0201
123                charset-japanese-jisx0208-1978
124                charset-chinese-gb2312
125                charset-japanese-jisx0208
126                charset-korean-ksc5601
127                charset-japanese-jisx0212)               'iso-2022-jp-2)
128    (cons (list charset-ascii charset-latin-iso8859-1
129                charset-greek-iso8859-7
130                charset-latin-jisx0201
131                charset-japanese-jisx0208-1978
132                charset-chinese-gb2312
133                charset-japanese-jisx0208
134                charset-korean-ksc5601
135                charset-japanese-jisx0212
136                charset-chinese-cns11643-1
137                charset-chinese-cns11643-2)              'iso-2022-int-1)
138    (cons (list charset-ascii charset-latin-iso8859-1
139                charset-latin-iso8859-2
140                charset-cyrillic-iso8859-5
141                charset-greek-iso8859-7
142                charset-latin-jisx0201
143                charset-japanese-jisx0208-1978
144                charset-chinese-gb2312
145                charset-japanese-jisx0208
146                charset-korean-ksc5601
147                charset-japanese-jisx0212
148                charset-chinese-cns11643-1
149                charset-chinese-cns11643-2
150                charset-chinese-cns11643-3
151                charset-chinese-cns11643-4
152                charset-chinese-cns11643-5
153                charset-chinese-cns11643-6
154                charset-chinese-cns11643-7)              'iso-2022-int-1)
155    ))
156
157 (defvar default-mime-charset 'x-ctext)
158
159 (defvar mime-charset-coding-system-alist
160   '((x-ctext            . coding-system-ctext)
161     (hz-gb-2312         . coding-system-hz)
162     (cn-gb-2312         . coding-system-euc-china)
163     (gb2312             . coding-system-euc-china)
164     (cn-big5            . coding-system-big5)
165     (iso-2022-jp-2      . coding-system-iso-2022-ss2-7)
166     (iso-2022-int-1     . coding-system-iso-2022-int)
167     (shift_jis          . coding-system-sjis)
168     ))
169
170 (defun mime-charset-to-coding-system (charset &optional lbt)
171   (if (stringp charset)
172       (setq charset (intern (downcase charset)))
173     )
174   (let ((cs
175          (or (cdr (assq charset mime-charset-coding-system-alist))
176              (let ((cs (intern (concat "coding-system-"
177                                        (symbol-name charset)))))
178                (and (coding-system-p cs) cs)
179                ))))
180     (if lbt
181         (intern (concat (symbol-name cs) "-" (symbol-name lbt)))
182       cs)))
183
184 (defun detect-mime-charset-region (start end)
185   "Return MIME charset for region between START and END. [emu-e20.el]"
186   (charsets-to-mime-charset
187    (find-charset-string (buffer-substring start end))
188    ))
189
190 (defun encode-mime-charset-region (start end charset)
191   "Encode the text between START and END as MIME CHARSET. [emu-e20.el]"
192   (let ((cs (mime-charset-to-coding-system charset)))
193     (if cs
194         (encode-coding-region start end cs)
195       )))
196
197 (defun decode-mime-charset-region (start end charset)
198   "Decode the text between START and END as MIME CHARSET. [emu-e20.el]"
199   (let ((cs (mime-charset-to-coding-system charset)))
200     (if cs
201         (decode-coding-region start end cs)
202       )))
203
204 (defun encode-mime-charset-string (string charset)
205   "Encode the STRING as MIME CHARSET. [emu-e20.el]"
206   (let ((cs (mime-charset-to-coding-system charset)))
207     (if cs
208         (encode-coding-string string cs)
209       string)))
210
211 (defun decode-mime-charset-string (string charset)
212   "Decode the STRING as MIME CHARSET. [emu-e20.el]"
213   (let ((cs (mime-charset-to-coding-system charset)))
214     (if cs
215         (decode-coding-string string cs)
216       string)))
217
218
219 ;;; @ character
220 ;;;
221
222 (defalias 'char-length 'char-bytes)
223
224 (defalias 'char-columns 'char-width)
225
226
227 ;;; @@ Mule emulating aliases
228 ;;;
229 ;;; You should not use them.
230
231 (defalias 'make-character 'make-char)
232
233 (defun char-category (character)
234   "Return string of category mnemonics for CHAR in TABLE.
235 CHAR can be any multilingual character
236 TABLE defaults to the current buffer's category table.
237 \[emu-e20.el; Mule emulating function]"
238   (category-set-mnemonics (char-category-set character))
239   )
240
241
242 ;;; @ string
243 ;;;
244
245 (defalias 'string-columns 'string-width)
246
247 (defalias 'sset 'string-embed-string)
248
249 (defun string-to-char-list (string)
250   "Return a list of which elements are characters in the STRING.
251 \[emu-e20.el; Mule 2.3 emulating function]"
252   (let* ((len (length string))
253          (i 0)
254          l chr)
255     (while (< i len)
256       (setq chr (sref string i))
257       (setq l (cons chr l))
258       (setq i (+ i (char-bytes chr)))
259       )
260     (nreverse l)
261     ))
262
263 (defalias 'string-to-int-list 'string-to-char-list)
264
265
266 ;;; @ regulation
267 ;;;
268
269 (defun regulate-latin-char (chr)
270   (cond ((and (<= ?\e$B#A\e(B chr)(<= chr ?\e$B#Z\e(B))
271          (+ (- chr ?\e$B#A\e(B) ?A)
272          )
273         ((and (<= ?\e$B#a\e(B chr)(<= chr ?\e$B#z\e(B))
274          (+ (- chr ?\e$B#a\e(B) ?a)
275          )
276         ((eq chr ?\e$B!%\e(B) ?.)
277         ((eq chr ?\e$B!$\e(B) ?,)
278         (t chr)
279         ))
280
281 (defun regulate-latin-string (str)
282   (let ((len (length str))
283         (i 0)
284         chr (dest ""))
285     (while (< i len)
286       (setq chr (sref str i))
287       (setq dest (concat dest
288                          (char-to-string (regulate-latin-char chr))))
289       (setq i (+ i (char-bytes chr)))
290       )
291     dest))
292
293
294 ;;; @ end
295 ;;;
296
297 (provide 'emu-e20)
298
299 ;;; emu-e20.el ends here