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