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