(set-buffer-multibyte): New function.
[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,1998 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         Katsumi Yamaoka <yamaoka@jpl.org>
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-e19)
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 (defun charset-chars (charset)
73   "Return the number of characters per dimension of CHARSET."
74   (if (= (logand (nth 2 (character-set charset)) 1) 1)
75       96
76     94))
77
78
79 ;;; @ coding system
80 ;;;
81
82 (defun encode-coding-region (start end coding-system)
83   "Encode the text between START and END to CODING-SYSTEM.
84 \[EMACS 20 emulating function]"
85   (code-convert-region start end *internal* coding-system)
86   )
87
88 (defun decode-coding-region (start end coding-system)
89   "Decode the text between START and END which is encoded in CODING-SYSTEM.
90 \[EMACS 20 emulating function]"
91   (code-convert-region start end coding-system *internal*)
92   )
93
94 (defun encode-coding-string (str coding-system)
95   "Encode the STRING to CODING-SYSTEM.
96 \[EMACS 20 emulating function]"
97   (code-convert-string str *internal* coding-system)
98   )
99
100 (defun decode-coding-string (str coding-system)
101   "Decode the string STR which is encoded in CODING-SYSTEM.
102 \[EMACS 20 emulating function]"
103   (let ((len (length str))
104         ret)
105     (while (and
106             (< 0 len)
107             (null
108              (setq ret
109                    (code-convert-string (substring str 0 len)
110                                         coding-system *internal*))
111              ))
112       (setq len (1- len))
113       )
114     (concat ret (substring str len))
115     ))
116
117 (defalias 'detect-coding-region 'code-detect-region)
118
119 (defalias 'set-buffer-file-coding-system 'set-file-coding-system)
120
121 (defmacro as-binary-process (&rest body)
122   (` (let (selective-display    ; Disable ^M to nl translation.
123            ;; Mule
124            mc-flag      
125            (default-process-coding-system (cons *noconv* *noconv*))
126            program-coding-system-alist)
127        (,@ body)
128        )))
129
130 (defmacro as-binary-input-file (&rest body)
131   (` (let (mc-flag
132            (file-coding-system-for-read *noconv*)
133            )
134        (,@ body)
135        )))
136
137 (defmacro as-binary-output-file (&rest body)
138   (` (let (mc-flag
139            (file-coding-system *noconv*)
140            )
141        (,@ body)
142        )))
143
144 (defalias 'set-process-input-coding-system 'set-process-coding-system)
145
146
147 ;;; @ binary access
148 ;;;
149
150 (defun insert-file-contents-as-binary (filename
151                                        &optional visit beg end replace)
152   "Like `insert-file-contents', q.v., but don't code and format conversion.
153 Like `insert-file-contents-literary', but it allows find-file-hooks,
154 automatic uncompression, etc.
155
156 Namely this function ensures that only format decoding and character
157 code conversion will not take place."
158   (let (mc-flag
159         (file-coding-system *noconv*))
160     (insert-file-contents filename visit beg end replace)
161     ))
162
163 (defalias 'insert-binary-file-contents 'insert-file-contents-as-binary)
164 (make-obsolete 'insert-binary-file-contents 'insert-file-contents-as-binary)
165
166 (defun insert-binary-file-contents-literally (filename
167                                               &optional visit beg end replace)
168   "Like `insert-file-contents-literally', q.v., but don't code conversion.
169 A buffer may be modified in several ways after reading into the buffer due
170 to advanced Emacs features, such as file-name-handlers, format decoding,
171 find-file-hooks, etc.
172   This function ensures that none of these modifications will take place."
173   (let (mc-flag
174         (file-coding-system *noconv*))
175     (insert-file-contents-literally filename visit beg end replace)
176     ))
177
178 (if running-emacs-19_29-or-later
179     ;; for MULE 2.3 based on Emacs 19.34.
180     (defun write-region-as-binary (start end filename
181                                          &optional append visit lockname)
182       "Like `write-region', q.v., but don't code conversion."
183       (let (mc-flag
184             (file-coding-system *noconv*))
185         (write-region start end filename append visit lockname)
186         ))
187   ;; for MULE 2.3 based on Emacs 19.28.
188   (defun write-region-as-binary (start end filename
189                                        &optional append visit lockname)
190     "Like `write-region', q.v., but don't code conversion."
191     (let (mc-flag
192           (file-coding-system *noconv*))
193       (write-region start end filename append visit)
194       ))
195   )
196
197
198 ;;; @ MIME charset
199 ;;;
200
201 (defun encode-mime-charset-region (start end charset)
202   "Encode the text between START and END as MIME CHARSET."
203   (let ((cs (mime-charset-to-coding-system charset)))
204     (if cs
205         (code-convert start end *internal* cs)
206       )))
207
208 (defun decode-mime-charset-region (start end charset)
209   "Decode the text between START and END as MIME CHARSET."
210   (let ((cs (mime-charset-to-coding-system charset)))
211     (if cs
212         (code-convert start end cs *internal*)
213       )))
214
215 (defun encode-mime-charset-string (string charset)
216   "Encode the STRING as MIME CHARSET."
217   (let ((cs (mime-charset-to-coding-system charset)))
218     (if cs
219         (code-convert-string string *internal* cs)
220       string)))
221
222 (defun decode-mime-charset-string (string charset)
223   "Decode the STRING which is encoded in MIME CHARSET."
224   (let ((cs (mime-charset-to-coding-system charset)))
225     (if cs
226         (decode-coding-string string cs)
227       string)))
228
229 (defun write-region-as-mime-charset (charset start end filename)
230   "Like `write-region', q.v., but code-convert by MIME CHARSET."
231   (let ((file-coding-system
232          (or (mime-charset-to-coding-system charset)
233              *noconv*)))
234     (write-region start end filename append visit)
235     ))
236
237
238 ;;; @@ to coding-system
239 ;;;
240
241 (defvar mime-charset-coding-system-alist
242   '((iso-8859-1      . *ctext*)
243     (x-ctext         . *ctext*)
244     (gb2312          . *euc-china*)
245     (koi8-r          . *koi8*)
246     (iso-2022-jp-2   . *iso-2022-ss2-7*)
247     (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
248     (shift_jis       . *sjis*)
249     (x-shiftjis      . *sjis*)
250     ))
251
252 (defun mime-charset-to-coding-system (charset &optional lbt)
253   (if (stringp charset)
254       (setq charset (intern (downcase charset)))
255     )
256   (let ((cs
257          (or (cdr (assq charset mime-charset-coding-system-alist))
258              (let ((cs (intern (concat "*" (symbol-name charset) "*"))))
259                (and (coding-system-p cs) cs)
260                ))))
261     (if (or (null lbt)
262             (null cs))
263         cs
264       (intern (concat (symbol-name cs) (symbol-name lbt)))
265       )))
266
267
268 ;;; @@ detection
269 ;;;
270
271 (defvar charsets-mime-charset-alist
272   (let ((alist
273          '(((lc-ascii)                                  . us-ascii)
274            ((lc-ascii lc-ltn1)                          . iso-8859-1)
275            ((lc-ascii lc-ltn2)                          . iso-8859-2)
276            ((lc-ascii lc-ltn3)                          . iso-8859-3)
277            ((lc-ascii lc-ltn4)                          . iso-8859-4)
278 ;;;        ((lc-ascii lc-crl)                           . iso-8859-5)
279            ((lc-ascii lc-crl)                           . koi8-r)
280            ((lc-ascii lc-arb)                           . iso-8859-6)
281            ((lc-ascii lc-grk)                           . iso-8859-7)
282            ((lc-ascii lc-hbw)                           . iso-8859-8)
283            ((lc-ascii lc-ltn5)                          . iso-8859-9)
284            ((lc-ascii lc-roman lc-jpold lc-jp)          . iso-2022-jp)
285            ((lc-ascii lc-kr)                            . euc-kr)
286            ((lc-ascii lc-cn)                            . gb2312)
287            ((lc-ascii lc-big5-1 lc-big5-2)              . big5)
288            ((lc-ascii lc-roman lc-ltn1 lc-grk
289                       lc-jpold lc-cn lc-jp lc-kr
290                       lc-jp2)                           . iso-2022-jp-2)
291            ((lc-ascii lc-roman lc-ltn1 lc-grk
292                       lc-jpold lc-cn lc-jp lc-kr lc-jp2
293                       lc-cns1 lc-cns2)                  . iso-2022-int-1)
294            ((lc-ascii lc-roman
295                       lc-ltn1 lc-ltn2 lc-crl lc-grk
296                       lc-jpold lc-cn lc-jp lc-kr lc-jp2
297                       lc-cns1 lc-cns2 lc-cns3 lc-cns4
298                       lc-cns5 lc-cns6 lc-cns7)          . iso-2022-int-1)
299            ))
300         dest)
301     (while alist
302       (catch 'not-found
303         (let ((pair (car alist)))
304           (setq dest
305                 (append dest
306                         (list
307                          (cons (mapcar (function
308                                         (lambda (cs)
309                                           (if (boundp cs)
310                                               (symbol-value cs)
311                                             (throw 'not-found nil)
312                                             )))
313                                        (car pair))
314                                (cdr pair)))))))
315       (setq alist (cdr alist)))
316     dest))
317
318 (defvar default-mime-charset 'x-ctext
319   "Default value of MIME-charset.
320 It is used when MIME-charset is not specified.
321 It must be symbol.")
322
323 (defun detect-mime-charset-region (start end)
324   "Return MIME charset for region between START and END."
325   (charsets-to-mime-charset
326    (cons lc-ascii (find-charset-region start end))))
327
328
329 ;;; @ buffer representation
330 ;;;
331
332 (defsubst-maybe set-buffer-multibyte (flag)
333   "Set the multibyte flag of the current buffer to FLAG.
334 If FLAG is t, this makes the buffer a multibyte buffer.
335 If FLAG is nil, this makes the buffer a single-byte buffer.
336 The buffer contents remain unchanged as a sequence of bytes
337 but the contents viewed as characters do change.
338 \[Emacs 20.3 emulating function]"
339   (setq mc-flag flag)
340   )
341
342
343 ;;; @ character
344 ;;;
345
346 (defalias 'char-charset 'char-leading-char)
347
348 (defmacro char-next-index (char index)
349   "Return index of character succeeding CHAR whose index is INDEX."
350   (` (+ (, index) (char-bytes (, char)))))
351
352 ;;; @@ obsoleted aliases
353 ;;;
354 ;;; You should not use them.
355
356 (defalias 'char-length 'char-bytes)
357 ;;(defalias 'char-columns 'char-width)
358
359
360 ;;; @ string
361 ;;;
362
363 (defalias 'string-columns 'string-width)
364
365 (defalias 'string-to-int-list 'string-to-char-list)
366
367 (or (fboundp 'truncate-string)
368 ;;; Imported from Mule-2.3
369 (defun truncate-string (str width &optional start-column)
370   "Truncate STR to fit in WIDTH columns.
371 Optional non-nil arg START-COLUMN specifies the starting column.
372 \[emu-mule.el; Mule 2.3 emulating function]"
373   (or start-column
374       (setq start-column 0))
375   (let ((max-width (string-width str))
376         (len (length str))
377         (from 0)
378         (column 0)
379         to-prev to ch)
380     (if (>= width max-width)
381         (setq width max-width))
382     (if (>= start-column width)
383         ""
384       (while (< column start-column)
385         (setq ch (aref str from)
386               column (+ column (char-width ch))
387               from (+ from (char-bytes ch))))
388       (if (< width max-width)
389           (progn
390             (setq to from)
391             (while (<= column width)
392               (setq ch (aref str to)
393                     column (+ column (char-width ch))
394                     to-prev to
395                     to (+ to (char-bytes ch))))
396             (setq to to-prev)))
397       (substring str from to))))
398 ;;;
399   )
400
401 (defalias 'looking-at-as-unibyte 'looking-at)
402
403
404 ;;; @ regulation
405 ;;;
406
407 (defun regulate-latin-char (chr)
408   (cond ((and (<= ?\e$B#A\e(B chr)(<= chr ?\e$B#Z\e(B))
409          (+ (- chr ?\e$B#A\e(B) ?A)
410          )
411         ((and (<= ?\e$B#a\e(B chr)(<= chr ?\e$B#z\e(B))
412          (+ (- chr ?\e$B#a\e(B) ?a)
413          )
414         ((eq chr ?\e$B!%\e(B) ?.)
415         ((eq chr ?\e$B!$\e(B) ?,)
416         (t chr)
417         ))
418
419 (defun regulate-latin-string (str)
420   (let ((len (length str))
421         (i 0)
422         chr (dest ""))
423     (while (< i len)
424       (setq chr (sref str i))
425       (setq dest (concat dest
426                          (char-to-string (regulate-latin-char chr))))
427       (setq i (+ i (char-bytes chr)))
428       )
429     dest))
430
431
432 ;;; @ end
433 ;;;
434
435 (provide 'emu-mule)
436
437 ;;; emu-mule.el ends here