1 ;;; emu-mule.el --- emu module for Mule 1.* and Mule 2.*
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Keywords: emulation, compatibility, Mule
9 ;; This file is part of emu.
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.
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.
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.
28 ;;; @ version specific features
31 (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)))
43 (let ((pat (aref font 1)))
44 (if (string-match "-[0-9]+-" pat)
47 pat (1+ (match-beginning 0)) (1- (match-end 0))))
53 (defun make-overlay (beg end &optional buffer type))
54 (defun overlay-put (overlay prop value))
61 (defalias 'make-char 'make-character)
63 (defalias 'find-non-ascii-charset-string 'find-charset-string)
64 (defalias 'find-non-ascii-charset-region 'find-charset-region)
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)
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)
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))
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*))
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]"
99 (code-convert-string str *internal* coding-system)
100 ;;(code-convert-string str *internal* nil) returns nil instead of str.
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]"
108 (let ((len (length str))
110 (while (and (< 0 len)
113 (substring str 0 len)
114 coding-system *internal*))))
116 (concat ret (substring str len)))
119 (defalias 'detect-coding-region 'code-detect-region)
121 (defalias 'set-buffer-file-coding-system 'set-file-coding-system)
123 (defmacro as-binary-process (&rest body)
124 (` (let (selective-display ; Disable ^M to nl translation.
127 (default-process-coding-system (cons *noconv* *noconv*))
128 program-coding-system-alist)
131 (defmacro as-binary-input-file (&rest body)
133 (file-coding-system-for-read *noconv*)
137 (defmacro as-binary-output-file (&rest body)
139 (file-coding-system *noconv*)
143 (defalias 'set-process-input-coding-system 'set-process-coding-system)
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.
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)))
161 (defalias 'insert-binary-file-contents 'insert-file-contents-as-binary)
162 (make-obsolete 'insert-binary-file-contents 'insert-file-contents-as-binary)
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
173 (narrow-to-region (point)(point))
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)
180 (list (car return-val) (buffer-size))))))
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)))
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)))
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)))
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))))
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)))
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)))
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))))
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)))
243 (code-convert start end *internal* cs)
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))
251 (code-convert start end cs *internal*)
252 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
254 (if (setq newline (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")))))
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*)))))))
264 (defun encode-mime-charset-string (string charset)
265 "Encode the STRING as MIME CHARSET."
266 (let ((cs (mime-charset-to-coding-system charset)))
268 (code-convert-string string *internal* cs)
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))
276 (decode-coding-string string cs)
277 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
279 (if (setq newline (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")))))
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*)
287 (decode-coding-string string cs)))
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)
299 (write-region start end filename append visit lockname)))
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)
309 (write-region start end filename append visit)))
313 ;;; @@ to coding-system
318 (defvar mime-charset-coding-system-alist
319 '((iso-8859-1 . *ctext*)
321 (gb2312 . *euc-china*)
323 (iso-2022-jp-2 . *iso-2022-ss2-7*)
324 (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
326 (x-shiftjis . *sjis*)
329 (defsubst mime-charset-to-coding-system (charset &optional lbt)
330 (if (stringp charset)
331 (setq charset (intern (downcase charset)))
333 (setq charset (or (cdr (assq charset mime-charset-coding-system-alist))
334 (intern (concat "*" (symbol-name charset) "*"))))
336 (setq charset (intern (format "%s%s" charset
337 (cond ((eq lbt 'CRLF) 'dos)
342 (if (coding-system-p charset)
350 (defvar charsets-mime-charset-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)
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)
382 (let ((pair (car alist)))
386 (cons (mapcar (function
390 (throw 'not-found nil)
394 (setq alist (cdr alist)))
397 (defvar default-mime-charset 'x-ctext
398 "Default value of MIME-charset.
399 It is used when MIME-charset is not specified.
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))))
408 ;;; @ buffer representation
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]"
425 (defalias 'char-charset 'char-leading-char)
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)))
432 (setq dest (cons (- (char-component character p) 128) dest)
434 (cons (char-charset character) dest)))
436 (defmacro char-next-index (char index)
437 "Return index of character succeeding CHAR whose index is INDEX."
438 (` (+ (, index) (char-bytes (, char)))))
440 ;;; @@ obsoleted aliases
442 ;;; You should not use them.
444 (defalias 'char-length 'char-bytes)
445 ;;(defalias 'char-columns 'char-width)
451 (defalias 'string-columns 'string-width)
453 (defalias 'string-to-int-list 'string-to-char-list)
455 (or (fboundp 'truncate-string)
456 ;; Imported from Mule-2.3
457 (defun truncate-string (str width &optional start-column)
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]"
463 (setq start-column 0))
464 (let ((max-width (string-width str))
469 (if (>= width max-width)
470 (setq width max-width))
471 (if (>= start-column width)
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)
480 (while (<= column width)
481 (setq ch (aref str to)
482 column (+ column (char-width ch))
484 to (+ to (char-bytes ch))))
486 (substring str from to))))
489 (defalias 'looking-at-as-unibyte 'looking-at)
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) ?,)
504 (defun regulate-latin-string (str)
505 (let ((len (length str))
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))))
518 (eval-when-compile (require 'ccl))
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).
525 Because emu provides functions accepting symbol as CCL program,
526 user programs should not refer this variable.")
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)))
539 (define-ccl-program test-ccl-eof-block
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)
549 (defconst ccl-encoder-eof-block-is-broken
551 (not (equal (encode-coding-string "" 'test-ccl-eof-block-cs)
553 "t if CCL_EOF_BLOCK is not executed when coding system encounts EOF on
556 (defconst ccl-decoder-eof-block-is-broken
558 (not (equal (decode-coding-string "" 'test-ccl-eof-block-cs)
560 "t if CCL_EOF_BLOCK is not executed when coding system encounts EOF on
563 (defconst ccl-eof-block-is-broken
564 (or ccl-encoder-eof-block-is-broken
565 ccl-decoder-eof-block-is-broken))
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]"
572 (if (symbolp ccl-prog) (symbol-value ccl-prog) ccl-prog)
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]"
580 (if (symbolp ccl-prog) (symbol-value ccl-prog) ccl-prog)
589 ;;; emu-mule.el ends here