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