1 ;;; emu-nemacs.el --- emu API implementation for NEmacs
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, NEmacs, mule
8 ;; This file is part of emu.
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
34 'charset-description "Character set of ASCII")
36 'charset-registry "ASCII")
38 (put 'japanese-jisx0208
39 'charset-description "Character set of JIS X0208-1983")
40 (put 'japanese-jisx0208
41 'charset-registry "JISX0208.1983")
43 (defun charset-description (charset)
44 "Return description of CHARSET. [emu-nemacs.el]"
45 (get charset 'charset-description)
48 (defun charset-registry (charset)
49 "Return registry name of CHARSET. [emu-nemacs.el]"
50 (get charset 'charset-registry)
53 (defun charset-width (charset)
54 "Return number of columns a CHARSET occupies when displayed.
56 (if (eq charset 'ascii)
60 (defun charset-direction (charset)
61 "Return the direction of a character of CHARSET by
62 0 (left-to-right) or 1 (right-to-left). [emu-nemacs.el]"
65 (defun find-charset-string (str)
66 "Return a list of charsets in the string.
67 \[emu-nemacs.el; Mule emulating function]"
68 (if (string-match "[\200-\377]" str)
72 (defalias 'find-non-ascii-charset-string 'find-charset-string)
74 (defun find-charset-region (start end)
75 "Return a list of charsets in the region between START and END.
76 \[emu-nemacs.el; Mule emulating function]"
79 (narrow-to-region start end)
81 (re-search-forward "[\200-\377]" nil t)
86 (defalias 'find-non-ascii-charset-region 'find-charset-region)
88 (defun check-ASCII-string (str)
91 (setq len (length str))
94 (if (>= (elt str i) 128)
100 ;;; @@ for old MULE emulation
103 ;;(defconst lc-ascii 0)
104 ;;(defconst lc-jp 146)
110 (defconst *noconv* 0)
114 (defconst *internal* 3)
115 (defconst *euc-japan* 3)
117 (defun decode-coding-string (string coding-system)
118 "Decode the STRING which is encoded in CODING-SYSTEM.
119 \[emu-nemacs.el; EMACS 20 emulating function]"
120 (if (eq coding-system 3)
122 (convert-string-kanji-code string coding-system 3)
125 (defun encode-coding-string (string coding-system)
126 "Encode the STRING to CODING-SYSTEM.
127 \[emu-nemacs.el; EMACS 20 emulating function]"
128 (if (eq coding-system 3)
130 (convert-string-kanji-code string 3 coding-system)
133 (defun decode-coding-region (start end coding-system)
134 "Decode the text between START and END which is encoded in CODING-SYSTEM.
135 \[emu-nemacs.el; EMACS 20 emulating function]"
139 (narrow-to-region start end)
140 (convert-region-kanji-code start end coding-system 3)
143 (defun encode-coding-region (start end coding-system)
144 "Encode the text between START and END to CODING-SYSTEM.
145 \[emu-nemacs.el; EMACS 20 emulating function]"
149 (narrow-to-region start end)
150 (convert-region-kanji-code start end 3 coding-system)
153 (defun detect-coding-region (start end)
154 "Detect coding-system of the text in the region between START and END.
155 \[emu-nemacs.el; Emacs 20 emulating function]"
158 (narrow-to-region start end)
160 (re-search-forward "[\200-\377]" nil t)
165 (defalias 'set-buffer-file-coding-system 'set-kanji-fileio-code)
168 ;;; @@ for old MULE emulation
171 (defun code-convert-string (str ic oc)
172 "Convert code in STRING from SOURCE code to TARGET code,
173 On successful converion, returns the result string,
174 else returns nil. [emu-nemacs.el; Mule emulating function]"
176 (convert-string-kanji-code str ic oc)
179 (defun code-convert-region (beg end ic oc)
180 "Convert code of the text between BEGIN and END from SOURCE
181 to TARGET. On successful conversion returns t,
182 else returns nil. [emu-nemacs.el; Mule emulating function]"
186 (narrow-to-region beg end)
187 (convert-region-kanji-code beg end ic oc)
191 ;;; @ without code-conversion
194 (defmacro as-binary-process (&rest body)
195 (` (let (selective-display ; Disable ^M to nl translation.
198 (default-kanji-process-code 0)
199 program-kanji-code-alist)
203 (defmacro as-binary-input-file (&rest body)
208 (defmacro as-binary-output-file (&rest body)
213 (defun write-region-as-binary (start end filename
214 &optional append visit lockname)
215 "Like `write-region', q.v., but don't code conversion. [emu-nemacs.el]"
217 (write-region start end filename append visit)
220 (defun insert-file-contents-as-binary (filename
221 &optional visit beg end replace)
222 "Like `insert-file-contents', q.v., but don't character code conversion.
225 (insert-file-contents filename visit beg end replace)
228 (fset 'insert-binary-file-contents 'insert-file-contents-as-binary)
230 (defun insert-binary-file-contents-literally (filename
231 &optional visit beg end replace)
232 "Like `insert-file-contents-literally', q.v., but don't code conversion.
233 A buffer may be modified in several ways after reading into the buffer due
234 to advanced Emacs features, such as file-name-handlers, format decoding,
235 find-file-hooks, etc.
236 This function ensures that none of these modifications will take place.
239 (insert-file-contents-literally filename visit beg end replace)
242 (defun insert-file-contents-as-raw-text (filename
243 &optional visit beg end replace)
244 "Like `insert-file-contents', q.v., but don't character code conversion.
247 (insert-file-contents filename visit beg end replace)
250 (defun write-region-as-raw-text-CRLF (start end filename
251 &optional append visit lockname)
252 "Like `write-region', q.v., but don't code conversion. [emu-nemacs.el]"
253 (let ((the-buf (current-buffer)))
255 (insert-buffer-substring the-buf start end)
256 (goto-char (point-min))
257 (while (re-search-forward "\\([^\r]\\)\n" nil t)
258 (replace-match "\\1\r\n")
261 (write-region (point-min)(point-max) filename append visit)
268 (defvar charsets-mime-charset-alist
269 '(((ascii) . us-ascii)))
271 (defvar default-mime-charset 'iso-2022-jp)
273 (defvar mime-charset-coding-system-alist
278 (defun mime-charset-to-coding-system (charset)
279 (if (stringp charset)
280 (setq charset (intern (downcase charset)))
282 (cdr (assq charset mime-charset-coding-system-alist))
285 (defun detect-mime-charset-region (start end)
286 "Return MIME charset for region between START and END.
290 (narrow-to-region start end)
292 (re-search-forward "[\200-\377]" nil t)
297 (defun encode-mime-charset-region (start end charset)
298 "Encode the text between START and END as MIME CHARSET.
300 (let ((cs (mime-charset-to-coding-system charset)))
305 (narrow-to-region start end)
306 (convert-region-kanji-code start end 3 cs)
310 (defun decode-mime-charset-region (start end charset)
311 "Decode the text between START and END as MIME CHARSET.
313 (let ((cs (mime-charset-to-coding-system charset)))
318 (narrow-to-region start end)
319 (convert-region-kanji-code start end cs 3)
323 (defun encode-mime-charset-string (string charset)
324 "Encode the STRING as MIME CHARSET. [emu-nemacs.el]"
325 (let ((cs (mime-charset-to-coding-system charset)))
327 (convert-string-kanji-code string 3 cs)
330 (defun decode-mime-charset-string (string charset)
331 "Decode the STRING as MIME CHARSET. [emu-nemacs.el]"
332 (let ((cs (mime-charset-to-coding-system charset)))
334 (convert-string-kanji-code string cs 3)
337 (defun write-region-as-mime-charset (charset start end filename)
338 "Like `write-region', q.v., but code-convert by MIME CHARSET.
340 (let ((kanji-fileio-code
341 (or (mime-charset-to-coding-system charset)
343 (write-region start end filename)
347 ;;; @ buffer representation
350 (defsubst-maybe set-buffer-multibyte (flag)
351 "Set the multibyte flag of the current buffer to FLAG.
352 If FLAG is t, this makes the buffer a multibyte buffer.
353 If FLAG is nil, this makes the buffer a single-byte buffer.
354 The buffer contents remain unchanged as a sequence of bytes
355 but the contents viewed as characters do change.
356 \[Emacs 20.3 emulating function]"
357 (setq kanji-flag flag)
364 (defun char-charset (chr)
365 "Return the character set of char CHR.
366 \[emu-nemacs.el; MULE emulating function]"
371 (defun char-bytes (chr)
372 "Return number of bytes CHAR will occupy in a buffer.
373 \[emu-nemacs.el; Mule emulating function]"
374 (if (< chr 128) 1 2))
376 (defun char-width (char)
377 "Return number of columns a CHAR occupies when displayed.
383 (defalias 'char-length 'char-bytes)
385 (defmacro char-next-index (char index)
386 "Return index of character succeeding CHAR whose index is INDEX."
387 (` (+ (, index) (char-bytes (, char)))))
393 (defalias 'string-width 'length)
395 (defun sref (str idx)
396 "Return the character in STR at index IDX.
397 \[emu-nemacs.el; Mule emulating function]"
398 (let ((chr (aref str idx)))
401 (logior (lsh (aref str (1+ idx)) 8) chr)
404 (defun string-to-char-list (str)
405 (let ((i 0)(len (length str)) dest chr)
407 (setq chr (aref str i))
410 chr (+ (lsh chr 8) (aref str i))
412 (setq dest (cons chr dest))
418 (fset 'string-to-int-list (symbol-function 'string-to-char-list))
420 ;;; Imported from Mule-2.3
421 (defun truncate-string (str width &optional start-column)
422 "Truncate STR to fit in WIDTH columns.
423 Optional non-nil arg START-COLUMN specifies the starting column.
424 \[emu-mule.el; Mule 2.3 emulating function]"
426 (setq start-column 0))
427 (let ((max-width (string-width str))
432 (if (>= width max-width)
433 (setq width max-width))
434 (if (>= start-column width)
436 (while (< column start-column)
437 (setq ch (aref str from)
438 column (+ column (char-columns ch))
439 from (+ from (char-bytes ch))))
440 (if (< width max-width)
443 (while (<= column width)
444 (setq ch (aref str to)
445 column (+ column (char-columns ch))
447 to (+ to (char-bytes ch))))
449 (substring str from to))))
451 (defalias 'looking-at-as-unibyte 'looking-at)
453 ;;; @@ obsoleted aliases
455 ;;; You should not use them.
457 (defalias 'string-columns 'length)
460 ;;; @ text property emulation
463 (defvar emu:available-face-attribute-alist
465 ;;(bold . inversed-region)
466 (italic . underlined-region)
467 (underline . underlined-region)
470 ;; by YAMATE Keiichirou 1994/10/28
471 (defun attribute-add-narrow-attribute (attr from to)
472 (or (consp (symbol-value attr))
474 (let* ((attr-value (symbol-value attr))
475 (len (car attr-value))
478 (while (and (< posfrom len)
479 (> from (nth posfrom attr-value)))
480 (setq posfrom (1+ posfrom)))
482 (while (and (< posto len)
483 (> to (nth posto attr-value)))
484 (setq posto (1+ posto)))
485 (if (= posto posfrom)
486 (if (= (% posto 2) 1)
488 (= to (nth posto attr-value)))
489 (set-marker (nth posto attr-value) from)
490 (setcdr (nthcdr (1- posfrom) attr-value)
491 (cons (set-marker-type (set-marker (make-marker)
494 (cons (set-marker-type (set-marker (make-marker)
497 (nthcdr posto attr-value))))
498 (setcar attr-value (+ len 2))))
499 (if (= (% posfrom 2) 0)
500 (setq posfrom (1- posfrom))
501 (set-marker (nth posfrom attr-value) from))
502 (if (= (% posto 2) 0)
504 (setq posto (1- posto))
505 (set-marker (nth posto attr-value) to))
506 (setcdr (nthcdr posfrom attr-value)
507 (nthcdr posto attr-value)))))
509 (defalias 'make-overlay 'cons)
511 (defun overlay-put (overlay prop value)
512 (let ((ret (and (eq prop 'face)
513 (assq value emu:available-face-attribute-alist)
516 (attribute-add-narrow-attribute (cdr ret)
517 (car overlay)(cdr overlay))
524 (provide 'emu-nemacs)
526 ;;; emu-nemacs.el ends here