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