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