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
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)))
43 (let ((pat (aref font 1)))
44 (if (string-match "-[0-9]+-" pat)
47 pat (1+ (match-beginning 0)) (1- (match-end 0))))
52 (defun make-overlay (beg end &optional buffer type))
53 (defun overlay-put (overlay prop value))
60 (defalias 'make-char 'make-character)
62 (defalias 'find-non-ascii-charset-string 'find-charset-string)
63 (defalias 'find-non-ascii-charset-region 'find-charset-region)
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)
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)
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))
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*))
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]"
98 (code-convert-string str *internal* coding-system)
99 ;;(code-convert-string str *internal* nil) returns nil instead of str.
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]"
107 (let ((len (length str))
109 (while (and (< 0 len)
112 (substring str 0 len)
113 coding-system *internal*))))
115 (concat ret (substring str len)))
118 (defalias 'detect-coding-region 'code-detect-region)
120 (defalias 'set-buffer-file-coding-system 'set-file-coding-system)
122 (defmacro as-binary-process (&rest body)
123 (` (let (selective-display ; Disable ^M to nl translation.
126 (default-process-coding-system (cons *noconv* *noconv*))
127 program-coding-system-alist)
130 (defmacro as-binary-input-file (&rest body)
132 (file-coding-system-for-read *noconv*)
136 (defmacro as-binary-output-file (&rest body)
138 (file-coding-system *noconv*)
142 (defalias 'set-process-input-coding-system 'set-process-coding-system)
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.
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)))
160 (defalias 'insert-binary-file-contents 'insert-file-contents-as-binary)
161 (make-obsolete 'insert-binary-file-contents 'insert-file-contents-as-binary)
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
172 (narrow-to-region (point)(point))
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)
179 (list (car return-val) (buffer-size))))))
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)))
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)))
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)))
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))))
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)))
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)))
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))))
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)))
242 (code-convert start end *internal* cs)
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))
250 (code-convert start end cs *internal*)
251 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
253 (if (setq newline (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")))))
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*)))))))
263 (defun encode-mime-charset-string (string charset)
264 "Encode the STRING as MIME CHARSET."
265 (let ((cs (mime-charset-to-coding-system charset)))
267 (code-convert-string string *internal* cs)
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))
275 (decode-coding-string string cs)
276 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
278 (if (setq newline (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")))))
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*)
286 (decode-coding-string string cs)))
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)
298 (write-region start end filename append visit lockname)))
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)
308 (write-region start end filename append visit)))
312 ;;; @@ to coding-system
317 (defvar mime-charset-coding-system-alist
318 '((iso-8859-1 . *ctext*)
320 (gb2312 . *euc-china*)
322 (iso-2022-jp-2 . *iso-2022-ss2-7*)
323 (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
325 (x-shiftjis . *sjis*)
328 (defsubst mime-charset-to-coding-system (charset &optional lbt)
329 (if (stringp charset)
330 (setq charset (intern (downcase charset)))
332 (setq charset (or (cdr (assq charset mime-charset-coding-system-alist))
333 (intern (concat "*" (symbol-name charset) "*"))))
335 (setq charset (intern (format "%s%s" charset
336 (cond ((eq lbt 'CRLF) 'dos)
341 (if (coding-system-p charset)
349 (defvar charsets-mime-charset-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)
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)
381 (let ((pair (car alist)))
385 (cons (mapcar (function
389 (throw 'not-found nil)
393 (setq alist (cdr alist)))
396 (defvar default-mime-charset 'x-ctext
397 "Default value of MIME-charset.
398 It is used when MIME-charset is not specified.
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))))
407 ;;; @ buffer representation
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]"
424 (defalias 'char-charset 'char-leading-char)
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)))
431 (setq dest (cons (- (char-component character p) 128) dest)
433 (cons (char-charset character) dest)))
435 (defmacro char-next-index (char index)
436 "Return index of character succeeding CHAR whose index is INDEX."
437 (` (+ (, index) (char-bytes (, char)))))
439 ;;; @@ obsoleted aliases
441 ;;; You should not use them.
443 (defalias 'char-length 'char-bytes)
444 ;;(defalias 'char-columns 'char-width)
450 (defalias 'string-columns 'string-width)
452 (defalias 'string-to-int-list 'string-to-char-list)
454 (or (fboundp 'truncate-string)
455 ;; Imported from Mule-2.3
456 (defun truncate-string (str width &optional start-column)
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]"
462 (setq start-column 0))
463 (let ((max-width (string-width str))
468 (if (>= width max-width)
469 (setq width max-width))
470 (if (>= start-column width)
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)
479 (while (<= column width)
480 (setq ch (aref str to)
481 column (+ column (char-width ch))
483 to (+ to (char-bytes ch))))
485 (substring str from to))))
488 (defalias 'looking-at-as-unibyte 'looking-at)
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) ?,)
503 (defun regulate-latin-string (str)
504 (let ((len (length str))
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))))
517 (eval-when-compile (require 'ccl))
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).
524 Because emu provides functions accepting symbol as CCL program,
525 user programs should not refer this variable.")
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)))
538 (define-ccl-program test-ccl-eof-block
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)
548 (defconst ccl-encoder-eof-block-is-broken
550 (not (equal (encode-coding-string "" 'test-ccl-eof-block-cs)
552 "t if CCL_EOF_BLOCK is not executed when coding system encounts EOF on
555 (defconst ccl-decoder-eof-block-is-broken
557 (not (equal (decode-coding-string "" 'test-ccl-eof-block-cs)
559 "t if CCL_EOF_BLOCK is not executed when coding system encounts EOF on
562 (defconst ccl-eof-block-is-broken
563 (or ccl-encoder-eof-block-is-broken
564 ccl-decoder-eof-block-is-broken))
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]"
571 (if (symbolp ccl-prog) (symbol-value ccl-prog) ccl-prog)
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]"
579 (if (symbolp ccl-prog) (symbol-value ccl-prog) ccl-prog)
588 ;;; emu-mule.el ends here