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