tm 7.68.
[elisp/apel.git] / emu-nemacs.el
1 ;;;
2 ;;; emu-nemacs.el --- Mule 2 emulation module for NEmacs
3 ;;;
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1993 .. 1996 MORIOKA Tomohiko
6 ;;;
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; modified by KOBAYASHI Shuhei <shuhei-k@jaist.ac.jp>
9 ;;; Version:
10 ;;;     $Id: emu-nemacs.el,v 7.30 1996/06/10 07:31:34 morioka Exp $
11 ;;; Keywords: emulation, compatibility, NEmacs, Mule
12 ;;;
13 ;;; This file is part of tl (Tiny Library).
14 ;;;
15 ;;; This program is free software; you can redistribute it and/or
16 ;;; modify it under the terms of the GNU General Public License as
17 ;;; published by the Free Software Foundation; either version 2, or
18 ;;; (at your option) any later version.
19 ;;;
20 ;;; This program is distributed in the hope that it will be useful,
21 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23 ;;; General Public License for more details.
24 ;;;
25 ;;; You should have received a copy of the GNU General Public License
26 ;;; along with This program.  If not, write to the Free Software
27 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;;;
29 ;;; Code:
30
31 (require 'emu-18)
32
33
34 ;;; @ character set
35 ;;;
36
37 (defconst charset-ascii 0 "Character set of ASCII")
38 (defconst charset-jisx0208 146 "Character set of JIS X0208-1983")
39
40 (defun charset-description (charset)
41   "Return description of CHARSET. [emu-nemacs.el]"
42   (if (< charset 128)
43       (documentation-property 'charset-ascii 'variable-documentation)
44     (documentation-property 'charset-jisx0208 'variable-documentation)
45     ))
46
47 (defun charset-registry (charset)
48   "Return registry name of CHARSET. [emu-nemacs.el]"
49   (if (< charset 128)
50       "ASCII"
51     "JISX0208.1983"))
52
53 (defun charset-columns (charset)
54   "Return number of columns a CHARSET occupies when displayed.
55 \[emu-nemacs.el]"
56   (if (< charset 128)
57       1
58     2))
59
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]"
63   0)
64
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)
69       (list lc-jp)
70     ))
71
72 (defun find-charset-region (start end)
73   "Return a list of charsets in the region between START and END.
74 \[emu-nemacs.el; Mule emulating function]"
75   (if (save-excursion
76         (save-restriction
77           (narrow-to-region start end)
78           (goto-char start)
79           (re-search-forward "[\200-\377]" nil t)
80           ))
81       (list lc-jp)
82     ))
83
84 (defun check-ASCII-string (str)
85   (let ((i 0)
86         len)
87     (setq len (length str))
88     (catch 'label
89       (while (< i len)
90         (if (>= (elt str i) 128)
91             (throw 'label nil))
92         (setq i (+ i 1))
93         )
94       str)))
95
96 ;;; @@ for Mule emulation
97 ;;;
98
99 (defconst lc-ascii 0)
100 (defconst lc-jp  146)
101
102
103 ;;; @ coding system
104 ;;;
105
106 (defconst *noconv*    0)
107 (defconst *sjis*      1)
108 (defconst *junet*     2)
109 (defconst *ctext*     2)
110 (defconst *internal*  3)
111 (defconst *euc-japan* 3)
112
113 (defun character-encode-string (str coding-system)
114   "Encode the string STR which is encoded in CODING-SYSTEM. [emu-nemacs.el]"
115   (convert-string-kanji-code str 3 coding-system)
116   )
117
118 (defun character-decode-string (str coding-system)
119   "Decode the string STR which is encoded in CODING-SYSTEM. [emu-nemacs.el]"
120   (convert-string-kanji-code str coding-system 3)
121   )
122
123 (defun character-encode-region (start end coding-system)
124   "Encode the text between START and END which is
125 encoded in CODING-SYSTEM. [emu-nemacs.el]"
126   (save-excursion
127     (save-restriction
128       (narrow-to-region beg end)
129       (convert-region-kanji-code start end 3 coding-system)
130       )))
131
132 (defun character-decode-region (start end coding-system)
133   "Decode the text between START and END which is
134 encoded in CODING-SYSTEM. [emu-nemacs.el]"
135   (save-excursion
136     (save-restriction
137       (narrow-to-region beg end)
138       (convert-region-kanji-code start end coding-system 3)
139       )))
140
141 (defun code-convert-string (str ic oc)
142   "Convert code in STRING from SOURCE code to TARGET code,
143 On successful converion, returns the result string,
144 else returns nil. [emu-nemacs.el; Mule emulating function]"
145   (if (not (eq ic oc))
146       (convert-string-kanji-code str ic oc)
147     str))
148
149 (defun code-convert-region (beg end ic oc)
150   "Convert code of the text between BEGIN and END from SOURCE
151 to TARGET. On successful conversion returns t,
152 else returns nil. [emu-nemacs.el; Mule emulating function]"
153   (if (/= ic oc)
154       (save-excursion
155         (save-restriction
156           (narrow-to-region beg end)
157           (convert-region-kanji-code beg end ic oc)
158           ))))
159
160 (defun code-detect-region (start end)
161   "Detect coding-system of the text in the region between START and END.
162 \[emu-nemacs.el; Mule emulating function]"
163   (if (save-excursion
164         (save-restriction
165           (narrow-to-region start end)
166           (goto-char start)
167           (re-search-forward "[\200-\377]" nil t)
168           ))
169       *euc-japan*
170     ))
171
172 (defun set-file-coding-system (coding-system &optional force)
173   (set-kanji-fileio-code coding-system)
174   )
175
176
177 ;;; @ character
178 ;;;
179
180 (defun char-charset (chr)
181   "Return the character set of char CHR.
182 \[emu-nemacs.el; XEmacs 20 emulating function]"
183   (if (< chr 128)
184       charset-ascii
185     charset-jisx0208))
186
187 (defun char-bytes (chr)
188   "Return number of bytes CHAR will occupy in a buffer.
189 \[emu-nemacs.el; Mule emulating function]"
190   (if (< chr 128) 1 2))
191
192 (defalias 'char-length 'char-bytes)
193
194 (defun char-columns (character)
195   "Return number of columns a CHARACTER occupies when displayed.
196 \[emu-nemacs.el]"
197   (if (< character 128)
198       1
199     2))
200
201 ;;; @@ for Mule emulation
202 ;;;
203
204 (defalias 'char-leading-char 'char-charset)
205
206 (defalias 'char-width 'char-columns)
207
208
209 ;;; @ string
210 ;;;
211
212 (defalias 'string-columns 'length)
213
214 (defun sref (str idx)
215   "Return the character in STR at index IDX.
216 \[emu-nemacs.el; Mule emulating function]"
217   (let ((chr (aref str idx)))
218     (if (< chr 128)
219         chr
220       (logior (lsh (aref str (1+ idx)) 8) chr)
221       )))
222
223 (defun string-to-char-list (str)
224   (let ((i 0)(len (length str)) dest chr)
225     (while (< i len)
226       (setq chr (aref str i))
227       (if (>= chr 128)
228           (setq i (1+ i)
229                 chr (+ (lsh chr 8) (aref str i))
230                 ))
231       (setq dest (cons chr dest))
232       (setq i (1+ i))
233       )
234     (reverse dest)
235     ))
236
237 (fset 'string-to-int-list (symbol-function 'string-to-char-list))
238
239 ;;; Imported from Mule-2.3
240 (defun truncate-string (str width &optional start-column)
241   "Truncate STR to fit in WIDTH columns.
242 Optional non-nil arg START-COLUMN specifies the starting column.
243 \[emu-mule.el; Mule 2.3 emulating function]"
244   (or start-column
245       (setq start-column 0))
246   (let ((max-width (string-width str))
247         (len (length str))
248         (from 0)
249         (column 0)
250         to-prev to ch)
251     (if (>= width max-width)
252         (setq width max-width))
253     (if (>= start-column width)
254         ""
255       (while (< column start-column)
256         (setq ch (aref str from)
257               column (+ column (char-columns ch))
258               from (+ from (char-bytes ch))))
259       (if (< width max-width)
260           (progn
261             (setq to from)
262             (while (<= column width)
263               (setq ch (aref str to)
264                     column (+ column (char-columns ch))
265                     to-prev to
266                     to (+ to (char-bytes ch))))
267             (setq to to-prev)))
268       (substring str from to))))
269
270 ;;; @@ for Mule emulation
271 ;;;
272
273 (defalias 'string-width 'length)
274
275
276 ;;; @ text property emulation
277 ;;;
278
279 (setq tl:available-face-attribute-alist
280       '(
281         ;;(bold      . inversed-region)
282         (italic    . underlined-region)
283         (underline . underlined-region)
284         ))
285
286 ;; by YAMATE Keiichirou 1994/10/28
287 (defun attribute-add-narrow-attribute (attr from to)
288   (or (consp (symbol-value attr))
289       (set attr (list 1)))
290   (let* ((attr-value (symbol-value attr))
291          (len (car attr-value))
292          (posfrom 1)
293          posto)
294     (while (and (< posfrom len)
295                 (> from (nth posfrom attr-value)))
296       (setq posfrom (1+ posfrom)))
297     (setq posto posfrom)
298     (while (and (< posto len)
299                 (> to (nth posto attr-value)))
300       (setq posto (1+ posto)))
301     (if  (= posto posfrom)
302         (if (= (% posto 2) 1)
303             (if (and (< to len)
304                      (= to (nth posto attr-value)))
305                 (set-marker (nth posto attr-value) from)
306               (setcdr (nthcdr (1- posfrom) attr-value)
307                       (cons (set-marker-type (set-marker (make-marker)
308                                                          from)
309                                              'point-type)
310                             (cons (set-marker-type (set-marker (make-marker)
311                                                                to)
312                                                    nil)
313                                   (nthcdr posto attr-value))))
314               (setcar attr-value (+ len 2))))
315       (if (= (% posfrom 2) 0)
316           (setq posfrom (1- posfrom))
317         (set-marker (nth posfrom attr-value) from))
318       (if (= (% posto 2) 0)
319           nil
320         (setq posto (1- posto))
321         (set-marker (nth posto attr-value) to))
322       (setcdr (nthcdr posfrom attr-value)
323               (nthcdr posto attr-value)))))
324
325 (defalias 'tl:make-overlay 'cons)
326
327 (defun tl:overlay-put (overlay prop value)
328   (let ((ret (and (eq prop 'face)
329                   (assq value tl:available-face-attribute-alist)
330                   )))
331     (if ret
332         (attribute-add-narrow-attribute (cdr ret)
333                                         (car overlay)(cdr overlay))
334       )))
335
336
337 ;;; @ end
338 ;;;
339
340 (provide 'emu-nemacs)
341
342 ;;; emu-nemacs.el ends here