tm 7.79.
[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.48 1996/08/22 16:21:21 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 decode-coding-string (str coding-system)
72   "Decode the string STR which is encoded in CODING-SYSTEM.
73 \[emu-mule.el]"
74   (let ((len (length str))
75         ret)
76     (while (and
77             (< 0 len)
78             (null
79              (setq ret
80                    (code-convert-string (substring str 0 len)
81                                         coding-system *internal*))
82              ))
83       (setq len (1- len))
84       )
85     (concat ret (substring str len))
86     ))
87
88 (defmacro as-binary-process (&rest body)
89   (` (let (selective-display    ; Disable ^M to nl translation.
90            ;; Mule
91            mc-flag      
92            (default-process-coding-system (cons *noconv* *noconv*))
93            program-coding-system-alist)
94        (,@ body)
95        )))
96
97 (defalias 'set-process-input-coding-system 'set-process-coding-system)
98
99
100 ;;; @ MIME charset
101 ;;;
102
103 (defvar charsets-mime-charset-alist
104   (list
105    (cons (list lc-ascii)                                'us-ascii)
106    (cons (list lc-ascii lc-ltn1)                        'iso-8859-1)
107    (cons (list lc-ascii lc-ltn2)                        'iso-8859-2)
108    (cons (list lc-ascii lc-ltn3)                        'iso-8859-3)
109    (cons (list lc-ascii lc-ltn4)                        'iso-8859-4)
110 ;;;(cons (list lc-ascii lc-crl)                         'iso-8859-5)
111    (cons (list lc-ascii lc-crl)                         'koi8-r)
112    (cons (list lc-ascii lc-arb)                         'iso-8859-6)
113    (cons (list lc-ascii lc-grk)                         'iso-8859-7)
114    (cons (list lc-ascii lc-hbw)                         'iso-8859-8)
115    (cons (list lc-ascii lc-ltn5)                        'iso-8859-9)
116    (cons (list lc-ascii lc-roman lc-jpold lc-jp)        'iso-2022-jp)
117    (cons (list lc-ascii lc-kr)                          'euc-kr)
118    (cons (list lc-ascii lc-big5-1 lc-big5-2)            'big5)
119    (cons (list lc-ascii lc-roman lc-ltn1 lc-grk
120                lc-jpold lc-cn lc-jp lc-kr lc-jp2)       'iso-2022-jp-2)
121    (cons (list lc-ascii lc-roman lc-ltn1 lc-grk
122                lc-jpold lc-cn lc-jp lc-kr lc-jp2
123                lc-cns1 lc-cns2)                         'iso-2022-int-1)
124    (cons (list lc-ascii lc-roman
125                lc-ltn1 lc-ltn2 lc-crl lc-grk
126                lc-jpold lc-cn lc-jp lc-kr lc-jp2
127                lc-cns1 lc-cns2 lc-cns3 lc-cns4
128                lc-cns5 lc-cns6 lc-cns7)                 'iso-2022-int-1)
129    ))
130
131 (defvar default-mime-charset 'x-ctext)
132
133 (defvar mime-charset-coding-system-alist
134   '((iso-8859-1      . *ctext*)
135     (x-ctext         . *ctext*)
136     (gb2312          . *euc-china*)
137     (koi8-r          . *koi8*)
138     (iso-2022-jp-2   . *iso-2022-ss2-7*)
139     (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
140     (shift_jis       . *sjis*)
141     (x-shiftjis      . *sjis*)
142     ))
143
144 (defun mime-charset-to-coding-system (charset)
145   (if (stringp charset)
146       (setq charset (intern (downcase charset)))
147     )
148   (or (cdr (assq charset mime-charset-coding-system-alist))
149       (let ((cs (intern (concat "*" (symbol-name charset) "*"))))
150         (and (coding-system-p cs) cs)
151         )))
152
153 (defun detect-mime-charset-region (start end)
154   "Return MIME charset for region between START and END.
155 \[emu-mule.el]"
156   (charsets-to-mime-charset
157    (cons lc-ascii (find-charset-region start end))))
158
159 (defun encode-mime-charset-region (start end charset)
160   "Encode the text between START and END as MIME CHARSET.
161 \[emu-mule.el]"
162   (let ((cs (mime-charset-to-coding-system charset)))
163     (if cs
164         (code-convert start end *internal* cs)
165       )))
166
167 (defun decode-mime-charset-region (start end charset)
168   "Decode the text between START and END as MIME CHARSET.
169 \[emu-mule.el]"
170   (let ((cs (mime-charset-to-coding-system charset)))
171     (if cs
172         (code-convert start end cs *internal*)
173       )))
174
175 (defun encode-mime-charset-string (string charset)
176   "Encode the STRING as MIME CHARSET. [emu-mule.el]"
177   (let ((cs (mime-charset-to-coding-system charset)))
178     (if cs
179         (code-convert-string string *internal* cs)
180       string)))
181
182 (defun decode-mime-charset-string (string charset)
183   "Decode the STRING which is encoded in MIME CHARSET. [emu-mule.el]"
184   (let ((cs (mime-charset-to-coding-system charset)))
185     (if cs
186         (decode-coding-string string cs)
187       string)))
188
189
190 ;;; @ character
191 ;;;
192
193 (defalias 'char-charset 'char-leading-char)
194
195 (defalias 'char-length 'char-bytes)
196
197 (defalias 'char-columns 'char-width)
198
199
200 ;;; @ string
201 ;;;
202
203 (defalias 'string-columns 'string-width)
204
205 (defalias 'string-to-int-list 'string-to-char-list)
206
207 (or (fboundp 'truncate-string)
208 ;;; Imported from Mule-2.3
209 (defun truncate-string (str width &optional start-column)
210   "Truncate STR to fit in WIDTH columns.
211 Optional non-nil arg START-COLUMN specifies the starting column.
212 \[emu-mule.el; Mule 2.3 emulating function]"
213   (or start-column
214       (setq start-column 0))
215   (let ((max-width (string-width str))
216         (len (length str))
217         (from 0)
218         (column 0)
219         to-prev to ch)
220     (if (>= width max-width)
221         (setq width max-width))
222     (if (>= start-column width)
223         ""
224       (while (< column start-column)
225         (setq ch (aref str from)
226               column (+ column (char-width ch))
227               from (+ from (char-bytes ch))))
228       (if (< width max-width)
229           (progn
230             (setq to from)
231             (while (<= column width)
232               (setq ch (aref str to)
233                     column (+ column (char-width ch))
234                     to-prev to
235                     to (+ to (char-bytes ch))))
236             (setq to to-prev)))
237       (substring str from to))))
238 ;;;
239   )
240
241
242 ;;; @ regulation
243 ;;;
244
245 (defun regulate-latin-char (chr)
246   (cond ((and (<= ?\e$B#A\e(B chr)(<= chr ?\e$B#Z\e(B))
247          (+ (- chr ?\e$B#A\e(B) ?A)
248          )
249         ((and (<= ?\e$B#a\e(B chr)(<= chr ?\e$B#z\e(B))
250          (+ (- chr ?\e$B#a\e(B) ?a)
251          )
252         ((eq chr ?\e$B!%\e(B) ?.)
253         ((eq chr ?\e$B!$\e(B) ?,)
254         (t chr)
255         ))
256
257 (defun regulate-latin-string (str)
258   (let ((len (length str))
259         (i 0)
260         chr (dest ""))
261     (while (< i len)
262       (setq chr (sref str i))
263       (setq dest (concat dest
264                          (char-to-string (regulate-latin-char chr))))
265       (setq i (+ i (char-bytes chr)))
266       )
267     dest))
268
269
270 ;;; @ end
271 ;;;
272
273 (provide 'emu-mule)
274
275 ;;; emu-mule.el ends here