APEL 3.4.
[elisp/apel.git] / emu-mule.el
1 ;;; emu-mule.el --- emu module for Mule 1.* and Mule 2.*
2
3 ;; Copyright (C) 1995,1996,1997 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: emu-mule.el,v 7.63 1997/05/22 04:46:57 morioka Exp $
7 ;; Keywords: emulation, compatibility, Mule
8
9 ;; This file is part of emu.
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 make-overlay (beg end &optional buffer type))
54        (defun overlay-put (overlay prop value))
55        ))
56
57
58 ;;; @ character set
59 ;;;
60
61 (defalias 'make-char 'make-character)
62
63 (defalias 'find-non-ascii-charset-string 'find-charset-string)
64 (defalias 'find-non-ascii-charset-region 'find-charset-region)
65
66 (defalias 'charset-bytes        'char-bytes)
67 (defalias 'charset-description  'char-description)
68 (defalias 'charset-registry     'char-registry)
69 (defalias 'charset-columns      'char-width)
70 (defalias 'charset-direction    'char-direction)
71
72
73 ;;; @ coding system
74 ;;;
75
76 (defun encode-coding-region (start end coding-system)
77   "Encode the text between START and END to CODING-SYSTEM.
78 \[emu-mule.el; EMACS 20 emulating function]"
79   (code-convert-region start end *internal* coding-system)
80   )
81
82 (defun decode-coding-string (str coding-system)
83   "Decode the string STR which is encoded in CODING-SYSTEM.
84 \[emu-mule.el]"
85   (let ((len (length str))
86         ret)
87     (while (and
88             (< 0 len)
89             (null
90              (setq ret
91                    (code-convert-string (substring str 0 len)
92                                         coding-system *internal*))
93              ))
94       (setq len (1- len))
95       )
96     (concat ret (substring str len))
97     ))
98
99 (defalias 'detect-coding-region 'code-detect-region)
100
101 (defalias 'set-buffer-file-coding-system 'set-file-coding-system)
102
103 (defmacro as-binary-process (&rest body)
104   (` (let (selective-display    ; Disable ^M to nl translation.
105            ;; Mule
106            mc-flag      
107            (default-process-coding-system (cons *noconv* *noconv*))
108            program-coding-system-alist)
109        (,@ body)
110        )))
111
112 (defmacro as-binary-input-file (&rest body)
113   (` (let (mc-flag
114            (file-coding-system-for-read *noconv*)
115            )
116        (,@ body)
117        )))
118
119 (defmacro as-binary-output-file (&rest body)
120   (` (let (mc-flag
121            (file-coding-system *noconv*)
122            )
123        (,@ body)
124        )))
125
126 (defalias 'set-process-input-coding-system 'set-process-coding-system)
127
128
129 ;;; @ binary access
130 ;;;
131
132 (defun insert-binary-file-contents-literally
133   (filename &optional visit beg end replace)
134   "Like `insert-file-contents-literally', q.v., but don't code conversion.
135 A buffer may be modified in several ways after reading into the buffer due
136 to advanced Emacs features, such as file-name-handlers, format decoding,
137 find-file-hooks, etc.
138   This function ensures that none of these modifications will take place."
139   (let (mc-flag
140         (file-coding-system *noconv*)
141         )
142     (insert-file-contents-literally filename visit beg end replace)
143     ))
144
145
146 ;;; @ MIME charset
147 ;;;
148
149 (defvar charsets-mime-charset-alist
150   (list
151    (cons (list lc-ascii)                                'us-ascii)
152    (cons (list lc-ascii lc-ltn1)                        'iso-8859-1)
153    (cons (list lc-ascii lc-ltn2)                        'iso-8859-2)
154    (cons (list lc-ascii lc-ltn3)                        'iso-8859-3)
155    (cons (list lc-ascii lc-ltn4)                        'iso-8859-4)
156 ;;;(cons (list lc-ascii lc-crl)                         'iso-8859-5)
157    (cons (list lc-ascii lc-crl)                         'koi8-r)
158    (cons (list lc-ascii lc-arb)                         'iso-8859-6)
159    (cons (list lc-ascii lc-grk)                         'iso-8859-7)
160    (cons (list lc-ascii lc-hbw)                         'iso-8859-8)
161    (cons (list lc-ascii lc-ltn5)                        'iso-8859-9)
162    (cons (list lc-ascii lc-roman lc-jpold lc-jp)        'iso-2022-jp)
163    (cons (list lc-ascii lc-kr)                          'euc-kr)
164    (cons (list lc-ascii lc-cn)                          'gb2312)
165    (cons (list lc-ascii lc-big5-1 lc-big5-2)            'big5)
166    (cons (list lc-ascii lc-roman lc-ltn1 lc-grk
167                lc-jpold lc-cn lc-jp lc-kr lc-jp2)       'iso-2022-jp-2)
168    (cons (list lc-ascii lc-roman lc-ltn1 lc-grk
169                lc-jpold lc-cn lc-jp lc-kr lc-jp2
170                lc-cns1 lc-cns2)                         'iso-2022-int-1)
171    (cons (list lc-ascii lc-roman
172                lc-ltn1 lc-ltn2 lc-crl lc-grk
173                lc-jpold lc-cn lc-jp lc-kr lc-jp2
174                lc-cns1 lc-cns2 lc-cns3 lc-cns4
175                lc-cns5 lc-cns6 lc-cns7)                 'iso-2022-int-1)
176    ))
177
178 (defvar default-mime-charset 'x-ctext)
179
180 (defvar mime-charset-coding-system-alist
181   '((iso-8859-1      . *ctext*)
182     (x-ctext         . *ctext*)
183     (gb2312          . *euc-china*)
184     (koi8-r          . *koi8*)
185     (iso-2022-jp-2   . *iso-2022-ss2-7*)
186     (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
187     (shift_jis       . *sjis*)
188     (x-shiftjis      . *sjis*)
189     ))
190
191 (defun mime-charset-to-coding-system (charset &optional lbt)
192   (if (stringp charset)
193       (setq charset (intern (downcase charset)))
194     )
195   (let ((cs
196          (or (cdr (assq charset mime-charset-coding-system-alist))
197              (let ((cs (intern (concat "*" (symbol-name charset) "*"))))
198                (and (coding-system-p cs) cs)
199                ))))
200     (if (or (null lbt)
201             (null cs))
202         cs
203       (intern (concat (symbol-name cs) (symbol-name lbt)))
204       )))
205
206 (defun detect-mime-charset-region (start end)
207   "Return MIME charset for region between START and END.
208 \[emu-mule.el]"
209   (charsets-to-mime-charset
210    (cons lc-ascii (find-charset-region start end))))
211
212 (defun encode-mime-charset-region (start end charset)
213   "Encode the text between START and END as MIME CHARSET.
214 \[emu-mule.el]"
215   (let ((cs (mime-charset-to-coding-system charset)))
216     (if cs
217         (code-convert start end *internal* cs)
218       )))
219
220 (defun decode-mime-charset-region (start end charset)
221   "Decode the text between START and END as MIME CHARSET.
222 \[emu-mule.el]"
223   (let ((cs (mime-charset-to-coding-system charset)))
224     (if cs
225         (code-convert start end cs *internal*)
226       )))
227
228 (defun encode-mime-charset-string (string charset)
229   "Encode the STRING as MIME CHARSET. [emu-mule.el]"
230   (let ((cs (mime-charset-to-coding-system charset)))
231     (if cs
232         (code-convert-string string *internal* cs)
233       string)))
234
235 (defun decode-mime-charset-string (string charset)
236   "Decode the STRING which is encoded in MIME CHARSET. [emu-mule.el]"
237   (let ((cs (mime-charset-to-coding-system charset)))
238     (if cs
239         (decode-coding-string string cs)
240       string)))
241
242
243 ;;; @ character
244 ;;;
245
246 (defalias 'char-charset 'char-leading-char)
247
248 (defalias 'char-length 'char-bytes)
249
250 (defalias 'char-columns 'char-width)
251
252
253 ;;; @ string
254 ;;;
255
256 (defalias 'string-columns 'string-width)
257
258 (defalias 'string-to-int-list 'string-to-char-list)
259
260 (or (fboundp 'truncate-string)
261 ;;; Imported from Mule-2.3
262 (defun truncate-string (str width &optional start-column)
263   "Truncate STR to fit in WIDTH columns.
264 Optional non-nil arg START-COLUMN specifies the starting column.
265 \[emu-mule.el; Mule 2.3 emulating function]"
266   (or start-column
267       (setq start-column 0))
268   (let ((max-width (string-width str))
269         (len (length str))
270         (from 0)
271         (column 0)
272         to-prev to ch)
273     (if (>= width max-width)
274         (setq width max-width))
275     (if (>= start-column width)
276         ""
277       (while (< column start-column)
278         (setq ch (aref str from)
279               column (+ column (char-width ch))
280               from (+ from (char-bytes ch))))
281       (if (< width max-width)
282           (progn
283             (setq to from)
284             (while (<= column width)
285               (setq ch (aref str to)
286                     column (+ column (char-width ch))
287                     to-prev to
288                     to (+ to (char-bytes ch))))
289             (setq to to-prev)))
290       (substring str from to))))
291 ;;;
292   )
293
294
295 ;;; @ regulation
296 ;;;
297
298 (defun regulate-latin-char (chr)
299   (cond ((and (<= ?\e$B#A\e(B chr)(<= chr ?\e$B#Z\e(B))
300          (+ (- chr ?\e$B#A\e(B) ?A)
301          )
302         ((and (<= ?\e$B#a\e(B chr)(<= chr ?\e$B#z\e(B))
303          (+ (- chr ?\e$B#a\e(B) ?a)
304          )
305         ((eq chr ?\e$B!%\e(B) ?.)
306         ((eq chr ?\e$B!$\e(B) ?,)
307         (t chr)
308         ))
309
310 (defun regulate-latin-string (str)
311   (let ((len (length str))
312         (i 0)
313         chr (dest ""))
314     (while (< i len)
315       (setq chr (sref str i))
316       (setq dest (concat dest
317                          (char-to-string (regulate-latin-char chr))))
318       (setq i (+ i (char-bytes chr)))
319       )
320     dest))
321
322
323 ;;; @ end
324 ;;;
325
326 (provide 'emu-mule)
327
328 ;;; emu-mule.el ends here