update.
[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 (require 'poe)
32
33 (cond (running-emacs-19
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        (defun make-overlay (beg end &optional buffer type))
53        (defun overlay-put (overlay prop value))
54        ))
55
56
57 ;;; @ character set
58 ;;;
59
60 (defalias 'make-char 'make-character)
61
62 (defalias 'find-non-ascii-charset-string 'find-charset-string)
63 (defalias 'find-non-ascii-charset-region 'find-charset-region)
64
65 (defalias 'charset-bytes        'char-bytes)
66 (defalias 'charset-description  'char-description)
67 (defalias 'charset-registry     'char-registry)
68 (defalias 'charset-columns      'char-width)
69 (defalias 'charset-direction    'char-direction)
70
71 (defun charset-chars (charset)
72   "Return the number of characters per dimension of CHARSET."
73   (if (= (logand (nth 2 (character-set charset)) 1) 1)
74       96
75     94))
76
77
78 ;;; @ coding system
79 ;;;
80
81 (defun encode-coding-region (start end coding-system)
82   "Encode the text between START and END to CODING-SYSTEM.
83 \[EMACS 20 emulating function]"
84   ;; If `coding-system' is nil, do nothing.
85   (code-convert-region start end *internal* coding-system))
86
87 (defun decode-coding-region (start end coding-system)
88   "Decode the text between START and END which is encoded in CODING-SYSTEM.
89 \[EMACS 20 emulating function]"
90   ;; If `coding-system' is nil, do nothing.
91   (code-convert-region start end coding-system *internal*))
92
93 ;; XXX: Should we support optional NOCOPY argument? (only in Emacs 20.x)
94 (defun encode-coding-string (str coding-system)
95   "Encode the STRING to CODING-SYSTEM.
96 \[EMACS 20 emulating function]"
97   (if coding-system
98       (code-convert-string str *internal* coding-system)
99     ;;(code-convert-string str *internal* nil) returns nil instead of str.
100     str))
101
102 ;; XXX: Should we support optional NOCOPY argument? (only in Emacs 20.x)
103 (defun decode-coding-string (str coding-system)
104   "Decode the string STR which is encoded in CODING-SYSTEM.
105 \[EMACS 20 emulating function]"
106   (if coding-system
107       (let ((len (length str))
108             ret)
109         (while (and (< 0 len)
110                     (null (setq ret
111                                 (code-convert-string
112                                  (substring str 0 len)
113                                  coding-system *internal*))))
114           (setq len (1- len)))
115         (concat ret (substring str len)))
116     str))
117
118 (defalias 'detect-coding-region 'code-detect-region)
119
120 (defalias 'set-buffer-file-coding-system 'set-file-coding-system)
121
122 (defmacro as-binary-process (&rest body)
123   (` (let (selective-display    ; Disable ^M to nl translation.
124            ;; Mule
125            mc-flag      
126            (default-process-coding-system (cons *noconv* *noconv*))
127            program-coding-system-alist)
128        (,@ body))))
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 (defmacro as-binary-output-file (&rest body)
137   (` (let (mc-flag
138            (file-coding-system *noconv*)
139            )
140        (,@ body))))
141
142 (defalias 'set-process-input-coding-system 'set-process-coding-system)
143
144
145 ;;; @ binary access
146 ;;;
147
148 (defun insert-file-contents-as-binary (filename
149                                        &optional visit beg end replace)
150   "Like `insert-file-contents', q.v., but don't code and format conversion.
151 Like `insert-file-contents-literary', but it allows find-file-hooks,
152 automatic uncompression, etc.
153
154 Namely this function ensures that only format decoding and character
155 code conversion will not take place."
156   (as-binary-input-file
157    ;; Returns list absolute file name and length of data inserted.
158    (insert-file-contents filename visit beg end replace)))
159
160 (defalias 'insert-binary-file-contents 'insert-file-contents-as-binary)
161 (make-obsolete 'insert-binary-file-contents 'insert-file-contents-as-binary)
162
163 (defun insert-file-contents-as-raw-text (filename
164                                          &optional visit beg end replace)
165   "Like `insert-file-contents', q.v., but don't code and format conversion.
166 Like `insert-file-contents-literary', but it allows find-file-hooks,
167 automatic uncompression, etc.
168 Like `insert-file-contents-as-binary', but it converts line-break
169 code."
170   (save-excursion
171     (save-restriction
172       (narrow-to-region (point)(point))
173       (let ((return-val
174              ;; Returns list absolute file name and length of data inserted.
175              (insert-file-contents-as-binary filename visit beg end replace)))
176         (goto-char (point-min))
177         (while (re-search-forward "\r$" nil t)
178           (replace-match ""))
179         (list (car return-val) (buffer-size))))))
180
181 (defun insert-binary-file-contents-literally (filename
182                                               &optional visit beg end replace)
183   "Like `insert-file-contents-literally', q.v., but don't code conversion.
184 A buffer may be modified in several ways after reading into the buffer due
185 to advanced Emacs features, such as file-name-handlers, format decoding,
186 find-file-hooks, etc.
187   This function ensures that none of these modifications will take place."
188   (as-binary-input-file
189    ;; Returns list absolute file name and length of data inserted.
190    (insert-file-contents-literally filename visit beg end replace)))
191
192 (cond
193  (running-emacs-19_29-or-later
194   ;; for MULE 2.3 based on Emacs 19.34.
195   (defun write-region-as-binary (start end filename
196                                        &optional append visit lockname)
197     "Like `write-region', q.v., but don't code conversion."
198     (as-binary-output-file
199      (write-region start end filename append visit lockname)))
200
201   (defun write-region-as-raw-text-CRLF (start end filename
202                                               &optional append visit lockname)
203     "Like `write-region', q.v., but don't code conversion."
204     (let ((the-buf (current-buffer)))
205       (with-temp-buffer
206         (insert-buffer-substring the-buf start end)
207         (goto-char (point-min))
208         (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
209           (replace-match "\\1\r\n"))
210         (write-region-as-binary (point-min)(point-max)
211                                 filename append visit lockname))))
212   )
213  (t
214   ;; for MULE 2.3 based on Emacs 19.28.
215   (defun write-region-as-binary (start end filename
216                                        &optional append visit lockname)
217     "Like `write-region', q.v., but don't code conversion."
218     (as-binary-output-file
219      (write-region start end filename append visit)))
220
221   (defun write-region-as-raw-text-CRLF (start end filename
222                                               &optional append visit lockname)
223     "Like `write-region', q.v., but don't code conversion."
224     (let ((the-buf (current-buffer)))
225       (with-temp-buffer
226         (insert-buffer-substring the-buf start end)
227         (goto-char (point-min))
228         (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
229           (replace-match "\\1\r\n"))
230         (write-region-as-binary (point-min)(point-max)
231                                 filename append visit))))
232   ))
233
234
235 ;;; @ MIME charset
236 ;;;
237
238 (defun encode-mime-charset-region (start end charset)
239   "Encode the text between START and END as MIME CHARSET."
240   (let ((cs (mime-charset-to-coding-system charset)))
241     (if cs
242         (code-convert start end *internal* cs)
243       )))
244
245 (defun decode-mime-charset-region (start end charset &optional lbt)
246   "Decode the text between START and END as MIME CHARSET."
247   (let ((cs (mime-charset-to-coding-system charset lbt))
248         newline)
249     (if cs
250         (code-convert start end cs *internal*)
251       (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
252           (progn
253             (if (setq newline (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")))))
254                 (save-excursion
255                   (save-restriction
256                     (narrow-to-region start end)
257                     (goto-char (point-min))
258                     (while (search-forward newline nil t)
259                       (replace-match "\n")))
260                   (code-convert (point-min) (point-max) cs *internal*))
261               (code-convert start end cs *internal*)))))))
262
263 (defun encode-mime-charset-string (string charset)
264   "Encode the STRING as MIME CHARSET."
265   (let ((cs (mime-charset-to-coding-system charset)))
266     (if cs
267         (code-convert-string string *internal* cs)
268       string)))
269
270 (defun decode-mime-charset-string (string charset &optional lbt)
271   "Decode the STRING which is encoded in MIME CHARSET."
272   (let ((cs (mime-charset-to-coding-system charset lbt))
273         newline)
274     (if cs
275         (decode-coding-string string cs)
276       (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
277           (progn
278             (if (setq newline (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")))))
279                 (with-temp-buffer
280                  (insert string)
281                  (goto-char (point-min))
282                  (while (search-forward newline nil t)
283                    (replace-match "\n"))
284                  (code-convert (point-min) (point-max) cs *internal*)
285                  (buffer-string))
286               (decode-coding-string string cs)))
287         string))))
288
289 (cond
290  (running-emacs-19_29-or-later
291   ;; for MULE 2.3 based on Emacs 19.34.
292   (defun write-region-as-mime-charset (charset start end filename
293                                                &optional append visit lockname)
294     "Like `write-region', q.v., but code-convert by MIME CHARSET."
295     (let ((file-coding-system
296            (or (mime-charset-to-coding-system charset)
297                *noconv*)))
298       (write-region start end filename append visit lockname)))
299   )
300  (t
301   ;; for MULE 2.3 based on Emacs 19.28.
302   (defun write-region-as-mime-charset (charset start end filename
303                                                &optional append visit lockname)
304     "Like `write-region', q.v., but code-convert by MIME CHARSET."
305     (let ((file-coding-system
306            (or (mime-charset-to-coding-system charset)
307                *noconv*)))
308       (write-region start end filename append visit)))
309   ))
310
311
312 ;;; @@ to coding-system
313 ;;;
314
315 (require 'cyrillic)
316
317 (defvar mime-charset-coding-system-alist
318   '((iso-8859-1      . *ctext*)
319     (x-ctext         . *ctext*)
320     (gb2312          . *euc-china*)
321     (koi8-r          . *koi8*)
322     (iso-2022-jp-2   . *iso-2022-ss2-7*)
323     (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
324     (shift_jis       . *sjis*)
325     (x-shiftjis      . *sjis*)
326     ))
327
328 (defsubst mime-charset-to-coding-system (charset &optional lbt)
329   (if (stringp charset)
330       (setq charset (intern (downcase charset)))
331     )
332   (setq charset (or (cdr (assq charset mime-charset-coding-system-alist))
333                     (intern (concat "*" (symbol-name charset) "*"))))
334   (if lbt
335       (setq charset (intern (format "%s%s" charset
336                                     (cond ((eq lbt 'CRLF) 'dos)
337                                           ((eq lbt 'LF) 'unix)
338                                           ((eq lbt 'CR) 'mac)
339                                           (t lbt)))))
340     )
341   (if (coding-system-p charset)
342       charset
343     ))
344
345
346 ;;; @@ detection
347 ;;;
348
349 (defvar charsets-mime-charset-alist
350   (let ((alist
351          '(((lc-ascii)                                  . us-ascii)
352            ((lc-ascii lc-ltn1)                          . iso-8859-1)
353            ((lc-ascii lc-ltn2)                          . iso-8859-2)
354            ((lc-ascii lc-ltn3)                          . iso-8859-3)
355            ((lc-ascii lc-ltn4)                          . iso-8859-4)
356 ;;;        ((lc-ascii lc-crl)                           . iso-8859-5)
357            ((lc-ascii lc-crl)                           . koi8-r)
358            ((lc-ascii lc-arb)                           . iso-8859-6)
359            ((lc-ascii lc-grk)                           . iso-8859-7)
360            ((lc-ascii lc-hbw)                           . iso-8859-8)
361            ((lc-ascii lc-ltn5)                          . iso-8859-9)
362            ((lc-ascii lc-roman lc-jpold lc-jp)          . iso-2022-jp)
363            ((lc-ascii lc-kr)                            . euc-kr)
364            ((lc-ascii lc-cn)                            . gb2312)
365            ((lc-ascii lc-big5-1 lc-big5-2)              . big5)
366            ((lc-ascii lc-roman lc-ltn1 lc-grk
367                       lc-jpold lc-cn lc-jp lc-kr
368                       lc-jp2)                           . iso-2022-jp-2)
369            ((lc-ascii lc-roman lc-ltn1 lc-grk
370                       lc-jpold lc-cn lc-jp lc-kr lc-jp2
371                       lc-cns1 lc-cns2)                  . iso-2022-int-1)
372            ((lc-ascii lc-roman
373                       lc-ltn1 lc-ltn2 lc-crl lc-grk
374                       lc-jpold lc-cn lc-jp lc-kr lc-jp2
375                       lc-cns1 lc-cns2 lc-cns3 lc-cns4
376                       lc-cns5 lc-cns6 lc-cns7)          . iso-2022-int-1)
377            ))
378         dest)
379     (while alist
380       (catch 'not-found
381         (let ((pair (car alist)))
382           (setq dest
383                 (append dest
384                         (list
385                          (cons (mapcar (function
386                                         (lambda (cs)
387                                           (if (boundp cs)
388                                               (symbol-value cs)
389                                             (throw 'not-found nil)
390                                             )))
391                                        (car pair))
392                                (cdr pair)))))))
393       (setq alist (cdr alist)))
394     dest))
395
396 (defvar default-mime-charset 'x-ctext
397   "Default value of MIME-charset.
398 It is used when MIME-charset is not specified.
399 It must be symbol.")
400
401 (defun detect-mime-charset-region (start end)
402   "Return MIME charset for region between START and END."
403   (charsets-to-mime-charset
404    (cons lc-ascii (find-charset-region start end))))
405
406
407 ;;; @ buffer representation
408 ;;;
409
410 (defsubst-maybe set-buffer-multibyte (flag)
411   "Set the multibyte flag of the current buffer to FLAG.
412 If FLAG is t, this makes the buffer a multibyte buffer.
413 If FLAG is nil, this makes the buffer a single-byte buffer.
414 The buffer contents remain unchanged as a sequence of bytes
415 but the contents viewed as characters do change.
416 \[Emacs 20.3 emulating function]"
417   (setq mc-flag flag)
418   )
419
420
421 ;;; @ character
422 ;;;
423
424 (defalias 'char-charset 'char-leading-char)
425
426 (defun split-char (character)
427   "Return list of charset and one or two position-codes of CHARACTER."
428   (let ((p (1- (char-bytes character)))
429         dest)
430     (while (>= p 1)
431       (setq dest (cons (- (char-component character p) 128) dest)
432             p (1- p)))
433     (cons (char-charset character) dest)))
434
435 (defmacro char-next-index (char index)
436   "Return index of character succeeding CHAR whose index is INDEX."
437   (` (+ (, index) (char-bytes (, char)))))
438
439 ;;; @@ obsoleted aliases
440 ;;;
441 ;;; You should not use them.
442
443 (defalias 'char-length 'char-bytes)
444 ;;(defalias 'char-columns 'char-width)
445
446
447 ;;; @ string
448 ;;;
449
450 (defalias 'string-columns 'string-width)
451
452 (defalias 'string-to-int-list 'string-to-char-list)
453
454 (or (fboundp 'truncate-string)
455     ;; Imported from Mule-2.3
456     (defun truncate-string (str width &optional start-column)
457       "\
458 Truncate STR to fit in WIDTH columns.
459 Optional non-nil arg START-COLUMN specifies the starting column.
460 \[emu-mule.el; Mule 2.3 emulating function]"
461       (or start-column
462           (setq start-column 0))
463       (let ((max-width (string-width str))
464             (len (length str))
465             (from 0)
466             (column 0)
467             to-prev to ch)
468         (if (>= width max-width)
469             (setq width max-width))
470         (if (>= start-column width)
471             ""
472           (while (< column start-column)
473             (setq ch (aref str from)
474                   column (+ column (char-width ch))
475                   from (+ from (char-bytes ch))))
476           (if (< width max-width)
477               (progn
478                 (setq to from)
479                 (while (<= column width)
480                   (setq ch (aref str to)
481                         column (+ column (char-width ch))
482                         to-prev to
483                         to (+ to (char-bytes ch))))
484                 (setq to to-prev)))
485           (substring str from to))))
486     )
487
488 (defalias 'looking-at-as-unibyte 'looking-at)
489
490
491 ;;; @ regulation
492 ;;;
493
494 (defun regulate-latin-char (chr)
495   (cond ((and (<= ?\e$B#A\e(B chr)(<= chr ?\e$B#Z\e(B))
496          (+ (- chr ?\e$B#A\e(B) ?A))
497         ((and (<= ?\e$B#a\e(B chr)(<= chr ?\e$B#z\e(B))
498          (+ (- chr ?\e$B#a\e(B) ?a))
499         ((eq chr ?\e$B!%\e(B) ?.)
500         ((eq chr ?\e$B!$\e(B) ?,)
501         (t chr)))
502
503 (defun regulate-latin-string (str)
504   (let ((len (length str))
505         (i 0)
506         chr (dest ""))
507     (while (< i len)
508       (setq chr (sref str i))
509       (setq dest (concat dest
510                          (char-to-string (regulate-latin-char chr))))
511       (setq i (+ i (char-bytes chr))))
512     dest))
513
514
515 ;;; @ CCL
516 ;;;
517 (eval-when-compile (require 'ccl))
518
519 (defconst ccl-use-symbol-as-program nil
520   "t if CCL related builtins accept symbol as CCL program.
521 (20.2 with ExCCL, 20.3 or later)
522 Otherwise nil (20.2 without ExCCL or former).
523
524 Because emu provides functions accepting symbol as CCL program,
525 user programs should not refer this variable.")
526
527 (defun make-ccl-coding-system
528   (coding-system mnemonic doc-string decoder encoder)
529   "Define a new CODING-SYSTEM (symbol) by CCL programs
530 DECODER (symbol) and ENCODER (symbol)."
531   (setq decoder (symbol-value decoder)
532         encoder (symbol-value encoder))
533   (make-coding-system coding-system 4 mnemonic doc-string
534                       nil ; Mule takes one more optional argument: EOL-TYPE.
535                       (cons decoder encoder)))
536
537 (eval-when-compile
538   (define-ccl-program test-ccl-eof-block
539     '(1
540       (read r0)
541       (write "[EOF]")))
542
543   (make-ccl-coding-system
544    'test-ccl-eof-block-cs ?T "CCL_EOF_BLOCK tester"
545    'test-ccl-eof-block 'test-ccl-eof-block)
546   )
547
548 (defconst ccl-encoder-eof-block-is-broken
549   (eval-when-compile
550     (not (equal (encode-coding-string "" 'test-ccl-eof-block-cs)
551                 "[EOF]")))
552   "t if CCL_EOF_BLOCK is not executed when coding system encounts EOF on
553 encoding.")
554
555 (defconst ccl-decoder-eof-block-is-broken
556   (eval-when-compile
557     (not (equal (decode-coding-string "" 'test-ccl-eof-block-cs)
558                 "[EOF]")))
559   "t if CCL_EOF_BLOCK is not executed when coding system encounts EOF on
560 decoding.")
561
562 (defconst ccl-eof-block-is-broken
563   (or ccl-encoder-eof-block-is-broken
564       ccl-decoder-eof-block-is-broken))
565
566 (defun ccl-execute (ccl-prog reg)
567   "Execute CCL-PROG with registers initialized by REGISTERS.
568 If CCL-PROG is symbol, it is dereferenced.
569 \[Emacs 20.3 emulating function]"
570   (exec-ccl
571    (if (symbolp ccl-prog) (symbol-value ccl-prog) ccl-prog)
572    reg))
573
574 (defun ccl-execute-on-string (ccl-prog status string &optional contin)
575   "Execute CCL-PROG with initial STATUS on STRING.
576 If CCL-PROG is symbol, it is dereferenced.
577 \[Emacs 20.3 emulating function]"
578   (exec-ccl-string
579    (if (symbolp ccl-prog) (symbol-value ccl-prog) ccl-prog)
580    status string))
581
582
583 ;;; @ end
584 ;;;
585
586 (provide 'emu-mule)
587
588 ;;; emu-mule.el ends here