tm 7.70.
[elisp/apel.git] / emu-mule.el
1 ;;;
2 ;;; emu-mule.el --- Mule 2.* emulation module for Mule
3 ;;;
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1994 .. 1996 MORIOKA Tomohiko
6 ;;;
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; Version:
9 ;;;     $Id: emu-mule.el,v 7.31 1996/07/08 07:46:02 morioka Exp $
10 ;;; Keywords: emulation, compatibility, Mule
11 ;;;
12 ;;; This file is part of tl (Tiny Library).
13 ;;;
14 ;;; This program is free software; you can redistribute it and/or
15 ;;; modify it under the terms of the GNU General Public License as
16 ;;; published by the Free Software Foundation; either version 2, or
17 ;;; (at your option) any later version.
18 ;;;
19 ;;; This program is distributed in the hope that it will be useful,
20 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 ;;; General Public License for more details.
23 ;;;
24 ;;; You should have received a copy of the GNU General Public License
25 ;;; along with This program.  If not, write to the Free Software
26 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;;;
28 ;;; Code:
29
30 ;;; @ version specific features
31 ;;;
32
33 (cond (running-emacs-19
34        (require 'emu-19)
35        
36        ;; Suggested by SASAKI Osamu <osamu@shuugr.bekkoame.or.jp>
37        ;; (cf. [os2-emacs-ja:78])
38        (defun fontset-pixel-size (fontset)
39          (let* ((font (get-font-info
40                        (aref (cdr (get-fontset-info fontset)) 0)))
41                 (open (aref font 4)))
42            (if (= open 1)
43                (aref font 5)
44              (if (= open 0)
45                  (let ((pat (aref font 1)))
46                    (if (string-match "-[0-9]+-" pat)
47                        (string-to-number
48                         (substring
49                          pat (1+ (match-beginning 0)) (1- (match-end 0))))
50                      0)))
51              )))
52        )
53       (running-emacs-18
54        (require 'emu-18)
55        (defun tl:make-overlay (beg end &optional buffer type))
56        (defun tl:overlay-put (overlay prop value))
57        ))
58
59
60 ;;; @ character set
61 ;;;
62
63 ;; 94 character set
64 (defconst charset-ascii          lc-ascii  "ASCII")
65 (defconst charset-jisx0201-kana  lc-kana   "JIS X0201 Katakana")
66 (defconst charset-jisx0201-latin lc-roman  "JIS X0201 Latin")
67
68 ;; 96 character set
69 (defconst charset-latin-1        lc-ltn1   "ISO-8859-1 (Latin-1)")
70 (defconst charset-latin-2        lc-ltn2   "ISO-8859-2 (Latin-2)")
71 (defconst charset-latin-3        lc-ltn3   "ISO-8859-3 (Latin-3)")
72 (defconst charset-latin-4        lc-ltn4   "ISO-8859-4 (Latin-4)")
73 (defconst charset-cyrillic       lc-crl    "ISO-8859-5 (Cyrillic)")
74 (defconst charset-arabic         lc-arb    "ISO-8859-6 (Arabic)")
75 (defconst charset-greek          lc-grk    "ISO-8859-7 (Greek)")
76 (defconst charset-hebrew         lc-hbw    "ISO-8859-8 (Hebrew)")
77 (defconst charset-latin-5        lc-ltn5   "ISO-8859-9 (Latin-5)")
78
79 ;; 94x94 character set
80 (defconst charset-jisx0208-1978  lc-jpold  "JIS X0208-1978")
81 (defconst charset-gb2312         lc-cn     "GB 2312-1980")
82 (defconst charset-jisx0208       lc-jp     "JIS X0208-1983")
83 (defconst charset-ksc5601        lc-kr     "KS C5601-1987")
84 (defconst charset-jisx0212       lc-jp2    "JIS X0212-1990")
85 (defconst charset-cns11643-1     lc-cns1   "CNS 11643-1986 plane 1")
86 (defconst charset-cns11643-2     lc-cns2   "CNS 11643-1986 plane 2")
87 (defconst charset-cns11643-3     lc-cns3   "CNS 11643-1986 plane 3")
88 (defconst charset-cns11643-4     lc-cns4   "CNS 11643-1986 plane 4")
89 (defconst charset-cns11643-5     lc-cns5   "CNS 11643-1986 plane 5")
90 (defconst charset-cns11643-6     lc-cns6   "CNS 11643-1986 plane 6")
91 (defconst charset-cns11643-7     lc-cns7   "CNS 11643-1986 plane 7")
92
93 ;; Big 5
94 (defconst charset-big5-1         lc-big5-1 "Big5 Level 1")
95 (defconst charset-big5-2         lc-big5-2 "Big5 Level 2")
96
97 (defalias 'charset-description 'char-description)
98 (defalias 'charset-registry    'char-registry)
99 (defalias 'charset-columns     'char-width)
100 (defalias 'charset-direction   'char-direction)
101
102
103 ;;; @ coding system
104 ;;;
105
106 (defun character-encode-string (str coding-system)
107   "Encode the string STR which is encoded in CODING-SYSTEM.
108 \[emu-mule.el]"
109   (code-convert-string str *internal* coding-system)
110   )
111
112 (defun character-decode-string (str coding-system)
113   "Decode the string STR which is encoded in CODING-SYSTEM.
114 \[emu-mule.el]"
115   (let ((len (length str))
116         ret)
117     (while (and
118             (< 0 len)
119             (null
120              (setq ret
121                    (code-convert-string (substring str 0 len)
122                                         coding-system *internal*))
123              ))
124       (setq len (1- len))
125       )
126     (concat ret (substring str len))
127     ))
128
129 (defun character-encode-region (start end coding-system)
130   "Encode the text between START and END which is
131 encoded in CODING-SYSTEM. [emu-mule.el]"
132   (code-convert start end *internal* coding-system)
133   )
134
135 (defun character-decode-region (start end coding-system)
136   "Decode the text between START and END which is
137 encoded in CODING-SYSTEM. [emu-mule.el]"
138   (code-convert start end coding-system *internal*)
139   )
140
141 (defmacro as-binary-process (&rest body)
142   (` (let (selective-display    ; Disable ^M to nl translation.
143            ;; Mule
144            mc-flag      
145            (default-process-coding-system (cons *noconv* *noconv*))
146            program-coding-system-alist)
147        (,@ body)
148        )))
149
150
151 ;;; @ MIME charset
152 ;;;
153
154 (defvar charsets-mime-charset-alist
155   (list
156    (cons (list lc-ascii)                                'us-ascii)
157    (cons (list lc-ascii lc-ltn1)                        'iso-8859-1)
158    (cons (list lc-ascii lc-ltn2)                        'iso-8859-2)
159    (cons (list lc-ascii lc-ltn3)                        'iso-8859-3)
160    (cons (list lc-ascii lc-ltn4)                        'iso-8859-4)
161 ;;;(cons (list lc-ascii lc-crl)                         'iso-8859-5)
162    (cons (list lc-ascii lc-crl)                         'koi8-r)
163    (cons (list lc-ascii lc-arb)                         'iso-8859-6)
164    (cons (list lc-ascii lc-grk)                         'iso-8859-7)
165    (cons (list lc-ascii lc-hbw)                         'iso-8859-8)
166    (cons (list lc-ascii lc-ltn5)                        'iso-8859-9)
167    (cons (list lc-ascii lc-jp)                          'iso-2022-jp)
168    (cons (list lc-ascii lc-kr)                          'euc-kr)
169    (cons (list lc-ascii lc-big5-1 lc-big5-2)            'big5)
170    (cons (list lc-ascii lc-cn lc-jp lc-kr lc-jp2
171                lc-ltn1 lc-grk)                          'iso-2022-jp-2)
172    (cons (list lc-ascii lc-cn lc-jp lc-kr lc-jp2
173                lc-cns1 lc-cns2 lc-ltn1 lc-grk)          'iso-2022-int-1)
174    ))
175
176 (defvar default-mime-charset 'iso-2022-int-1)
177
178 (defun charsets-to-mime-charset (charsets)
179   (if charsets
180       (or (catch 'tag
181             (let ((rest charsets-mime-charset-alist)
182                   cell csl)
183               (while (setq cell (car rest))
184                 (if (catch 'not-subset
185                       (let ((set1 charsets)
186                             (set2 (car cell))
187                             obj)
188                         (while set1
189                           (setq obj (car set1))
190                           (or (memq obj set2)
191                               (throw 'not-subset nil)
192                               )
193                           (setq set1 (cdr set1))
194                           )
195                         t))
196                     (throw 'tag (cdr cell))
197                   )
198                 (setq rest (cdr rest))
199                 )))
200           default-mime-charset)))
201
202 (defun detect-mime-charset-region (beg end)
203   (charsets-to-mime-charset
204    (cons lc-ascii (find-charset-region beg end))))
205
206 (defvar mime-charset-coding-system-alist
207   '((iso-8859-1      . *ctext*)
208     (gb2312          . *euc-china*)
209     (koi8-r          . *koi8*)
210     (iso-2022-jp-2   . *iso-2022-ss2-7*)
211     (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
212     (shift_jis       . *sjis*)
213     (x-shiftjis      . *sjis*)
214     ))
215
216 (defun mime-charset-to-coding-system (charset)
217   (if (stringp charset)
218       (setq charset (intern (downcase charset)))
219     )
220   (or (cdr (assq charset mime-charset-coding-system-alist))
221       (let ((cs (intern (concat "*" (symbol-name charset) "*"))))
222         (and (coding-system-p cs) cs)
223         )))
224
225
226 ;;; @ character
227 ;;;
228
229 (defalias 'char-charset 'char-leading-char)
230
231 (defalias 'char-length 'char-bytes)
232
233 (defalias 'char-columns 'char-width)
234
235
236 ;;; @ string
237 ;;;
238
239 (defalias 'string-columns 'string-width)
240
241 (defalias 'string-to-int-list 'string-to-char-list)
242
243 (or (fboundp 'truncate-string)
244 ;;; Imported from Mule-2.3
245 (defun truncate-string (str width &optional start-column)
246   "Truncate STR to fit in WIDTH columns.
247 Optional non-nil arg START-COLUMN specifies the starting column.
248 \[emu-mule.el; Mule 2.3 emulating function]"
249   (or start-column
250       (setq start-column 0))
251   (let ((max-width (string-width str))
252         (len (length str))
253         (from 0)
254         (column 0)
255         to-prev to ch)
256     (if (>= width max-width)
257         (setq width max-width))
258     (if (>= start-column width)
259         ""
260       (while (< column start-column)
261         (setq ch (aref str from)
262               column (+ column (char-width ch))
263               from (+ from (char-bytes ch))))
264       (if (< width max-width)
265           (progn
266             (setq to from)
267             (while (<= column width)
268               (setq ch (aref str to)
269                     column (+ column (char-width ch))
270                     to-prev to
271                     to (+ to (char-bytes ch))))
272             (setq to to-prev)))
273       (substring str from to))))
274 ;;;
275   )
276
277
278 ;;; @ regulation
279 ;;;
280
281 (defun regulate-latin-char (chr)
282   (cond ((and (<= ?\e$B#A\e(B chr)(<= chr ?\e$B#Z\e(B))
283          (+ (- chr ?\e$B#A\e(B) ?A)
284          )
285         ((and (<= ?\e$B#a\e(B chr)(<= chr ?\e$B#z\e(B))
286          (+ (- chr ?\e$B#a\e(B) ?a)
287          )
288         ((eq chr ?\e$B!%\e(B) ?.)
289         ((eq chr ?\e$B!$\e(B) ?,)
290         (t chr)
291         ))
292
293 (defun regulate-latin-string (str)
294   (let ((len (length str))
295         (i 0)
296         chr (dest ""))
297     (while (< i len)
298       (setq chr (sref str i))
299       (setq dest (concat dest
300                          (char-to-string (regulate-latin-char chr))))
301       (setq i (+ i (char-bytes chr)))
302       )
303     dest))
304
305
306 ;;; @ end
307 ;;;
308
309 (provide 'emu-mule)
310
311 ;;; emu-mule.el ends here