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