tm 7.76.
[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.44 1996/08/05 15:15:18 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 'x-ctext)
125
126 (defvar mime-charset-coding-system-alist
127   '((iso-8859-1      . *ctext*)
128     (x-ctext         . *ctext*)
129     (gb2312          . *euc-china*)
130     (koi8-r          . *koi8*)
131     (iso-2022-jp-2   . *iso-2022-ss2-7*)
132     (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
133     (shift_jis       . *sjis*)
134     (x-shiftjis      . *sjis*)
135     ))
136
137 (defun mime-charset-to-coding-system (charset)
138   (if (stringp charset)
139       (setq charset (intern (downcase charset)))
140     )
141   (or (cdr (assq charset mime-charset-coding-system-alist))
142       (let ((cs (intern (concat "*" (symbol-name charset) "*"))))
143         (and (coding-system-p cs) cs)
144         )))
145
146 (defun detect-mime-charset-region (start end)
147   "Return MIME charset for region between START and END.
148 \[emu-mule.el]"
149   (charsets-to-mime-charset
150    (cons lc-ascii (find-charset-region start end))))
151
152 (defun encode-mime-charset-region (start end charset)
153   "Encode the text between START and END as MIME CHARSET.
154 \[emu-mule.el]"
155   (let ((cs (mime-charset-to-coding-system charset)))
156     (if cs
157         (code-convert start end *internal* cs)
158       )))
159
160 (defun decode-mime-charset-region (start end charset)
161   "Decode the text between START and END as MIME CHARSET.
162 \[emu-mule.el]"
163   (let ((cs (mime-charset-to-coding-system charset)))
164     (if cs
165         (code-convert start end cs *internal*)
166       )))
167
168 (defun encode-mime-charset-string (string charset)
169   "Encode the STRING as MIME CHARSET. [emu-mule.el]"
170   (let ((cs (mime-charset-to-coding-system charset)))
171     (if cs
172         (code-convert-string string *internal* cs)
173       string)))
174
175 (defun decode-mime-charset-string (string charset)
176   "Decode the STRING which is encoded in MIME CHARSET. [emu-mule.el]"
177   (let ((cs (mime-charset-to-coding-system charset)))
178     (if cs
179         (character-decode-string string cs)
180       string)))
181
182
183 ;;; @ character
184 ;;;
185
186 (defalias 'char-charset 'char-leading-char)
187
188 (defalias 'char-length 'char-bytes)
189
190 (defalias 'char-columns 'char-width)
191
192
193 ;;; @ string
194 ;;;
195
196 (defalias 'string-columns 'string-width)
197
198 (defalias 'string-to-int-list 'string-to-char-list)
199
200 (or (fboundp 'truncate-string)
201 ;;; Imported from Mule-2.3
202 (defun truncate-string (str width &optional start-column)
203   "Truncate STR to fit in WIDTH columns.
204 Optional non-nil arg START-COLUMN specifies the starting column.
205 \[emu-mule.el; Mule 2.3 emulating function]"
206   (or start-column
207       (setq start-column 0))
208   (let ((max-width (string-width str))
209         (len (length str))
210         (from 0)
211         (column 0)
212         to-prev to ch)
213     (if (>= width max-width)
214         (setq width max-width))
215     (if (>= start-column width)
216         ""
217       (while (< column start-column)
218         (setq ch (aref str from)
219               column (+ column (char-width ch))
220               from (+ from (char-bytes ch))))
221       (if (< width max-width)
222           (progn
223             (setq to from)
224             (while (<= column width)
225               (setq ch (aref str to)
226                     column (+ column (char-width ch))
227                     to-prev to
228                     to (+ to (char-bytes ch))))
229             (setq to to-prev)))
230       (substring str from to))))
231 ;;;
232   )
233
234
235 ;;; @ regulation
236 ;;;
237
238 (defun regulate-latin-char (chr)
239   (cond ((and (<= ?\e$B#A\e(B chr)(<= chr ?\e$B#Z\e(B))
240          (+ (- chr ?\e$B#A\e(B) ?A)
241          )
242         ((and (<= ?\e$B#a\e(B chr)(<= chr ?\e$B#z\e(B))
243          (+ (- chr ?\e$B#a\e(B) ?a)
244          )
245         ((eq chr ?\e$B!%\e(B) ?.)
246         ((eq chr ?\e$B!$\e(B) ?,)
247         (t chr)
248         ))
249
250 (defun regulate-latin-string (str)
251   (let ((len (length str))
252         (i 0)
253         chr (dest ""))
254     (while (< i len)
255       (setq chr (sref str i))
256       (setq dest (concat dest
257                          (char-to-string (regulate-latin-char chr))))
258       (setq i (+ i (char-bytes chr)))
259       )
260     dest))
261
262
263 ;;; @ end
264 ;;;
265
266 (provide 'emu-mule)
267
268 ;;; emu-mule.el ends here