tm 7.87.
[elisp/apel.git] / emu-mule.el
1 ;;; emu-mule.el --- emu module for Mule 1.* and Mule 2.*
2
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: emu-mule.el,v 7.55 1996/09/23 17:43:43 morioka Exp $
7 ;; Keywords: emulation, compatibility, Mule
8
9 ;; This file is part of tl (Tiny Library).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 ;;; @ version specific features
29 ;;;
30
31 (cond (running-emacs-19
32        (require 'emu-19)
33        
34        ;; Suggested by SASAKI Osamu <osamu@shuugr.bekkoame.or.jp>
35        ;; (cf. [os2-emacs-ja:78])
36        (defun fontset-pixel-size (fontset)
37          (let* ((font (get-font-info
38                        (aref (cdr (get-fontset-info fontset)) 0)))
39                 (open (aref font 4)))
40            (if (= open 1)
41                (aref font 5)
42              (if (= open 0)
43                  (let ((pat (aref font 1)))
44                    (if (string-match "-[0-9]+-" pat)
45                        (string-to-number
46                         (substring
47                          pat (1+ (match-beginning 0)) (1- (match-end 0))))
48                      0)))
49              )))
50        )
51       (running-emacs-18
52        (require 'emu-18)
53        (defun tl:make-overlay (beg end &optional buffer type))
54        (defun tl:overlay-put (overlay prop value))
55        ))
56
57
58 ;;; @ character set
59 ;;;
60
61 (defalias 'charset-bytes        'char-bytes)
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 encode-coding-region (start end coding-system)
72   "Encode the text between START and END to CODING-SYSTEM.
73 \[emu-mule.el; EMACS 20 emulating function]"
74   (code-convert-region start end *internal* coding-system)
75   )
76
77 (defun decode-coding-string (str coding-system)
78   "Decode the string STR which is encoded in CODING-SYSTEM.
79 \[emu-mule.el]"
80   (let ((len (length str))
81         ret)
82     (while (and
83             (< 0 len)
84             (null
85              (setq ret
86                    (code-convert-string (substring str 0 len)
87                                         coding-system *internal*))
88              ))
89       (setq len (1- len))
90       )
91     (concat ret (substring str len))
92     ))
93
94 (defalias 'detect-coding-region 'code-detect-region)
95
96 (defmacro as-binary-process (&rest body)
97   (` (let (selective-display    ; Disable ^M to nl translation.
98            ;; Mule
99            mc-flag      
100            (default-process-coding-system (cons *noconv* *noconv*))
101            program-coding-system-alist)
102        (,@ body)
103        )))
104
105 (defmacro as-binary-input-file (&rest body)
106   (` (let (mc-flag
107            (file-coding-system-for-read *noconv*)
108            )
109        (,@ body)
110        )))
111
112 (defalias 'set-process-input-coding-system 'set-process-coding-system)
113
114
115 ;;; @ MIME charset
116 ;;;
117
118 (defvar charsets-mime-charset-alist
119   (list
120    (cons (list lc-ascii)                                'us-ascii)
121    (cons (list lc-ascii lc-ltn1)                        'iso-8859-1)
122    (cons (list lc-ascii lc-ltn2)                        'iso-8859-2)
123    (cons (list lc-ascii lc-ltn3)                        'iso-8859-3)
124    (cons (list lc-ascii lc-ltn4)                        'iso-8859-4)
125 ;;;(cons (list lc-ascii lc-crl)                         'iso-8859-5)
126    (cons (list lc-ascii lc-crl)                         'koi8-r)
127    (cons (list lc-ascii lc-arb)                         'iso-8859-6)
128    (cons (list lc-ascii lc-grk)                         'iso-8859-7)
129    (cons (list lc-ascii lc-hbw)                         'iso-8859-8)
130    (cons (list lc-ascii lc-ltn5)                        'iso-8859-9)
131    (cons (list lc-ascii lc-roman lc-jpold lc-jp)        'iso-2022-jp)
132    (cons (list lc-ascii lc-kr)                          'euc-kr)
133    (cons (list lc-ascii lc-cn)                          'gb2312)
134    (cons (list lc-ascii lc-big5-1 lc-big5-2)            'big5)
135    (cons (list lc-ascii lc-roman lc-ltn1 lc-grk
136                lc-jpold lc-cn lc-jp lc-kr lc-jp2)       'iso-2022-jp-2)
137    (cons (list lc-ascii lc-roman lc-ltn1 lc-grk
138                lc-jpold lc-cn lc-jp lc-kr lc-jp2
139                lc-cns1 lc-cns2)                         'iso-2022-int-1)
140    (cons (list lc-ascii lc-roman
141                lc-ltn1 lc-ltn2 lc-crl lc-grk
142                lc-jpold lc-cn lc-jp lc-kr lc-jp2
143                lc-cns1 lc-cns2 lc-cns3 lc-cns4
144                lc-cns5 lc-cns6 lc-cns7)                 'iso-2022-int-1)
145    ))
146
147 (defvar default-mime-charset 'x-ctext)
148
149 (defvar mime-charset-coding-system-alist
150   '((iso-8859-1      . *ctext*)
151     (x-ctext         . *ctext*)
152     (gb2312          . *euc-china*)
153     (koi8-r          . *koi8*)
154     (iso-2022-jp-2   . *iso-2022-ss2-7*)
155     (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
156     (shift_jis       . *sjis*)
157     (x-shiftjis      . *sjis*)
158     ))
159
160 (defun mime-charset-to-coding-system (charset)
161   (if (stringp charset)
162       (setq charset (intern (downcase charset)))
163     )
164   (or (cdr (assq charset mime-charset-coding-system-alist))
165       (let ((cs (intern (concat "*" (symbol-name charset) "*"))))
166         (and (coding-system-p cs) cs)
167         )))
168
169 (defun detect-mime-charset-region (start end)
170   "Return MIME charset for region between START and END.
171 \[emu-mule.el]"
172   (charsets-to-mime-charset
173    (cons lc-ascii (find-charset-region start end))))
174
175 (defun encode-mime-charset-region (start end charset)
176   "Encode the text between START and END as MIME CHARSET.
177 \[emu-mule.el]"
178   (let ((cs (mime-charset-to-coding-system charset)))
179     (if cs
180         (code-convert start end *internal* cs)
181       )))
182
183 (defun decode-mime-charset-region (start end charset)
184   "Decode the text between START and END as MIME CHARSET.
185 \[emu-mule.el]"
186   (let ((cs (mime-charset-to-coding-system charset)))
187     (if cs
188         (code-convert start end cs *internal*)
189       )))
190
191 (defun encode-mime-charset-string (string charset)
192   "Encode the STRING as MIME CHARSET. [emu-mule.el]"
193   (let ((cs (mime-charset-to-coding-system charset)))
194     (if cs
195         (code-convert-string string *internal* cs)
196       string)))
197
198 (defun decode-mime-charset-string (string charset)
199   "Decode the STRING which is encoded in MIME CHARSET. [emu-mule.el]"
200   (let ((cs (mime-charset-to-coding-system charset)))
201     (if cs
202         (decode-coding-string string cs)
203       string)))
204
205
206 ;;; @ character
207 ;;;
208
209 (defalias 'char-charset 'char-leading-char)
210
211 (defalias 'char-length 'char-bytes)
212
213 (defalias 'char-columns 'char-width)
214
215
216 ;;; @ string
217 ;;;
218
219 (defalias 'string-columns 'string-width)
220
221 (defalias 'string-to-int-list 'string-to-char-list)
222
223 (or (fboundp 'truncate-string)
224 ;;; Imported from Mule-2.3
225 (defun truncate-string (str width &optional start-column)
226   "Truncate STR to fit in WIDTH columns.
227 Optional non-nil arg START-COLUMN specifies the starting column.
228 \[emu-mule.el; Mule 2.3 emulating function]"
229   (or start-column
230       (setq start-column 0))
231   (let ((max-width (string-width str))
232         (len (length str))
233         (from 0)
234         (column 0)
235         to-prev to ch)
236     (if (>= width max-width)
237         (setq width max-width))
238     (if (>= start-column width)
239         ""
240       (while (< column start-column)
241         (setq ch (aref str from)
242               column (+ column (char-width ch))
243               from (+ from (char-bytes ch))))
244       (if (< width max-width)
245           (progn
246             (setq to from)
247             (while (<= column width)
248               (setq ch (aref str to)
249                     column (+ column (char-width ch))
250                     to-prev to
251                     to (+ to (char-bytes ch))))
252             (setq to to-prev)))
253       (substring str from to))))
254 ;;;
255   )
256
257
258 ;;; @ regulation
259 ;;;
260
261 (defun regulate-latin-char (chr)
262   (cond ((and (<= ?\e$B#A\e(B chr)(<= chr ?\e$B#Z\e(B))
263          (+ (- chr ?\e$B#A\e(B) ?A)
264          )
265         ((and (<= ?\e$B#a\e(B chr)(<= chr ?\e$B#z\e(B))
266          (+ (- chr ?\e$B#a\e(B) ?a)
267          )
268         ((eq chr ?\e$B!%\e(B) ?.)
269         ((eq chr ?\e$B!$\e(B) ?,)
270         (t chr)
271         ))
272
273 (defun regulate-latin-string (str)
274   (let ((len (length str))
275         (i 0)
276         chr (dest ""))
277     (while (< i len)
278       (setq chr (sref str i))
279       (setq dest (concat dest
280                          (char-to-string (regulate-latin-char chr))))
281       (setq i (+ i (char-bytes chr)))
282       )
283     dest))
284
285
286 ;;; @ end
287 ;;;
288
289 (provide 'emu-mule)
290
291 ;;; emu-mule.el ends here