tm 7.87.
[elisp/apel.git] / emu-nemacs.el
1 ;;; emu-nemacs.el --- Mule 2 emulation module for NEmacs
2
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: emu-nemacs.el,v 7.47 1996/09/23 17:43:04 morioka Exp $
7 ;; Keywords: emulation, compatibility, NEmacs, mule
8
9 ;; This file is part of tl (Tiny Library).
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 (require 'emu-18)
29
30
31 ;;; @ character set
32 ;;;
33
34 (defconst charset-ascii 0 "Character set of ASCII")
35 (defconst charset-jisx0208 146 "Character set of JIS X0208-1983")
36
37 (defun charset-description (charset)
38   "Return description of CHARSET. [emu-nemacs.el]"
39   (if (< charset 128)
40       (documentation-property 'charset-ascii 'variable-documentation)
41     (documentation-property 'charset-jisx0208 'variable-documentation)
42     ))
43
44 (defun charset-registry (charset)
45   "Return registry name of CHARSET. [emu-nemacs.el]"
46   (if (< charset 128)
47       "ASCII"
48     "JISX0208.1983"))
49
50 (defun charset-columns (charset)
51   "Return number of columns a CHARSET occupies when displayed.
52 \[emu-nemacs.el]"
53   (if (< charset 128)
54       1
55     2))
56
57 (defun charset-direction (charset)
58   "Return the direction of a character of CHARSET by
59   0 (left-to-right) or 1 (right-to-left). [emu-nemacs.el]"
60   0)
61
62 (defun find-charset-string (str)
63   "Return a list of charsets in the string.
64 \[emu-nemacs.el; Mule emulating function]"
65   (if (string-match "[\200-\377]" str)
66       (list lc-jp)
67     ))
68
69 (defun find-charset-region (start end)
70   "Return a list of charsets in the region between START and END.
71 \[emu-nemacs.el; Mule emulating function]"
72   (if (save-excursion
73         (save-restriction
74           (narrow-to-region start end)
75           (goto-char start)
76           (re-search-forward "[\200-\377]" nil t)
77           ))
78       (list lc-jp)
79     ))
80
81 (defun check-ASCII-string (str)
82   (let ((i 0)
83         len)
84     (setq len (length str))
85     (catch 'label
86       (while (< i len)
87         (if (>= (elt str i) 128)
88             (throw 'label nil))
89         (setq i (+ i 1))
90         )
91       str)))
92
93 ;;; @@ for old MULE emulation
94 ;;;
95
96 (defconst lc-ascii 0)
97 (defconst lc-jp  146)
98
99
100 ;;; @ coding system
101 ;;;
102
103 (defconst *noconv*    0)
104 (defconst *sjis*      1)
105 (defconst *junet*     2)
106 (defconst *ctext*     2)
107 (defconst *internal*  3)
108 (defconst *euc-japan* 3)
109
110 (defun decode-coding-string (string coding-system)
111   "Decode the STRING which is encoded in CODING-SYSTEM.
112 \[emu-nemacs.el; EMACS 20 emulating function]"
113   (if (eq coding-system 3)
114       string
115     (convert-string-kanji-code string coding-system 3)
116     ))
117
118 (defun encode-coding-string (string coding-system)
119   "Encode the STRING to CODING-SYSTEM.
120 \[emu-nemacs.el; EMACS 20 emulating function]"
121   (if (eq coding-system 3)
122       string
123     (convert-string-kanji-code string 3 coding-system)
124     ))
125
126 (defun decode-coding-region (start end coding-system)
127   "Decode the text between START and END which is encoded in CODING-SYSTEM.
128 \[emu-nemacs.el; EMACS 20 emulating function]"
129   (if (/= ic oc)
130       (save-excursion
131         (save-restriction
132           (narrow-to-region start end)
133           (convert-region-kanji-code start end coding-system 3)
134           ))))
135
136 (defun encode-coding-region (start end coding-system)
137   "Encode the text between START and END to CODING-SYSTEM.
138 \[emu-nemacs.el; EMACS 20 emulating function]"
139   (if (/= ic oc)
140       (save-excursion
141         (save-restriction
142           (narrow-to-region start end)
143           (convert-region-kanji-code start end 3 coding-system)
144           ))))
145
146 (defun detect-coding-region (start end)
147   "Detect coding-system of the text in the region between START and END.
148 \[emu-nemacs.el; Emacs 20 emulating function]"
149   (if (save-excursion
150         (save-restriction
151           (narrow-to-region start end)
152           (goto-char start)
153           (re-search-forward "[\200-\377]" nil t)
154           ))
155       *euc-japan*
156     ))
157
158 (defun set-file-coding-system (coding-system &optional force)
159   (set-kanji-fileio-code coding-system)
160   )
161
162 (defmacro as-binary-process (&rest body)
163   (` (let (selective-display    ; Disable ^M to nl translation.
164            ;; NEmacs
165            kanji-flag
166            (default-kanji-process-code 0)
167            program-kanji-code-alist)
168        (,@ body)
169        )))
170
171 (defmacro as-binary-input-file (&rest body)
172   (` (let (kanji-flag)
173        (,@ body)
174        )))
175
176
177 ;;; @@ for old MULE emulation
178 ;;;
179
180 (defun code-convert-string (str ic oc)
181   "Convert code in STRING from SOURCE code to TARGET code,
182 On successful converion, returns the result string,
183 else returns nil. [emu-nemacs.el; Mule emulating function]"
184   (if (not (eq ic oc))
185       (convert-string-kanji-code str ic oc)
186     str))
187
188 (defun code-convert-region (beg end ic oc)
189   "Convert code of the text between BEGIN and END from SOURCE
190 to TARGET. On successful conversion returns t,
191 else returns nil. [emu-nemacs.el; Mule emulating function]"
192   (if (/= ic oc)
193       (save-excursion
194         (save-restriction
195           (narrow-to-region beg end)
196           (convert-region-kanji-code beg end ic oc)
197           ))))
198
199
200 ;;; @ MIME charset
201 ;;;
202
203 (defvar charsets-mime-charset-alist
204   (list (cons (list charset-ascii) 'us-ascii)))
205
206 (defvar default-mime-charset 'iso-2022-jp)
207
208 (defvar mime-charset-coding-system-alist
209   '((iso-2022-jp     . 2)
210     (shift_jis       . 1)
211     ))
212
213 (defun mime-charset-to-coding-system (charset)
214   (if (stringp charset)
215       (setq charset (intern (downcase charset)))
216     )
217   (cdr (assq charset mime-charset-coding-system-alist))
218   )
219
220 (defun detect-mime-charset-region (start end)
221   "Return MIME charset for region between START and END.
222 \[emu-nemacs.el]"
223   (if (save-excursion
224         (save-restriction
225           (narrow-to-region start end)
226           (goto-char start)
227           (re-search-forward "[\200-\377]" nil t)
228           ))
229       default-mime-charset
230     'us-ascii))
231
232 (defun encode-mime-charset-region (start end charset)
233   "Encode the text between START and END as MIME CHARSET.
234 \[emu-nemacs.el]"
235   (let ((cs (mime-charset-to-coding-system charset)))
236     (and (numberp cs)
237          (or (= cs 3)
238              (save-excursion
239                (save-restriction
240                  (narrow-to-region start end)
241                  (convert-region-kanji-code start end 3 cs)
242                  ))
243              ))))
244
245 (defun decode-mime-charset-region (start end charset)
246   "Decode the text between START and END as MIME CHARSET.
247 \[emu-nemacs.el]"
248   (let ((cs (mime-charset-to-coding-system charset)))
249     (and (numberp cs)
250          (or (= cs 3)
251              (save-excursion
252                (save-restriction
253                  (narrow-to-region start end)
254                  (convert-region-kanji-code start end cs 3)
255                  ))
256              ))))
257
258 (defun encode-mime-charset-string (string charset)
259   "Encode the STRING as MIME CHARSET. [emu-nemacs.el]"
260   (let ((cs (mime-charset-to-coding-system charset)))
261     (if cs
262         (convert-string-kanji-code string 3 cs)
263       string)))
264
265 (defun decode-mime-charset-string (string charset)
266   "Decode the STRING as MIME CHARSET. [emu-nemacs.el]"
267   (let ((cs (mime-charset-to-coding-system charset)))
268     (if cs
269         (convert-string-kanji-code string cs 3)
270       string)))
271
272
273 ;;; @ character
274 ;;;
275
276 (defun char-charset (chr)
277   "Return the character set of char CHR.
278 \[emu-nemacs.el; XEmacs 20 emulating function]"
279   (if (< chr 128)
280       charset-ascii
281     charset-jisx0208))
282
283 (defun char-bytes (chr)
284   "Return number of bytes CHAR will occupy in a buffer.
285 \[emu-nemacs.el; Mule emulating function]"
286   (if (< chr 128) 1 2))
287
288 (defalias 'char-length 'char-bytes)
289
290 (defun char-columns (character)
291   "Return number of columns a CHARACTER occupies when displayed.
292 \[emu-nemacs.el]"
293   (if (< character 128)
294       1
295     2))
296
297 ;;; @@ for Mule emulation
298 ;;;
299
300 (defalias 'char-leading-char 'char-charset)
301
302 (defalias 'char-width 'char-columns)
303
304
305 ;;; @ string
306 ;;;
307
308 (defalias 'string-columns 'length)
309
310 (defun sref (str idx)
311   "Return the character in STR at index IDX.
312 \[emu-nemacs.el; Mule emulating function]"
313   (let ((chr (aref str idx)))
314     (if (< chr 128)
315         chr
316       (logior (lsh (aref str (1+ idx)) 8) chr)
317       )))
318
319 (defun string-to-char-list (str)
320   (let ((i 0)(len (length str)) dest chr)
321     (while (< i len)
322       (setq chr (aref str i))
323       (if (>= chr 128)
324           (setq i (1+ i)
325                 chr (+ (lsh chr 8) (aref str i))
326                 ))
327       (setq dest (cons chr dest))
328       (setq i (1+ i))
329       )
330     (reverse dest)
331     ))
332
333 (fset 'string-to-int-list (symbol-function 'string-to-char-list))
334
335 ;;; Imported from Mule-2.3
336 (defun truncate-string (str width &optional start-column)
337   "Truncate STR to fit in WIDTH columns.
338 Optional non-nil arg START-COLUMN specifies the starting column.
339 \[emu-mule.el; Mule 2.3 emulating function]"
340   (or start-column
341       (setq start-column 0))
342   (let ((max-width (string-width str))
343         (len (length str))
344         (from 0)
345         (column 0)
346         to-prev to ch)
347     (if (>= width max-width)
348         (setq width max-width))
349     (if (>= start-column width)
350         ""
351       (while (< column start-column)
352         (setq ch (aref str from)
353               column (+ column (char-columns ch))
354               from (+ from (char-bytes ch))))
355       (if (< width max-width)
356           (progn
357             (setq to from)
358             (while (<= column width)
359               (setq ch (aref str to)
360                     column (+ column (char-columns ch))
361                     to-prev to
362                     to (+ to (char-bytes ch))))
363             (setq to to-prev)))
364       (substring str from to))))
365
366 ;;; @@ for Mule emulation
367 ;;;
368
369 (defalias 'string-width 'length)
370
371
372 ;;; @ text property emulation
373 ;;;
374
375 (setq tl:available-face-attribute-alist
376       '(
377         ;;(bold      . inversed-region)
378         (italic    . underlined-region)
379         (underline . underlined-region)
380         ))
381
382 ;; by YAMATE Keiichirou 1994/10/28
383 (defun attribute-add-narrow-attribute (attr from to)
384   (or (consp (symbol-value attr))
385       (set attr (list 1)))
386   (let* ((attr-value (symbol-value attr))
387          (len (car attr-value))
388          (posfrom 1)
389          posto)
390     (while (and (< posfrom len)
391                 (> from (nth posfrom attr-value)))
392       (setq posfrom (1+ posfrom)))
393     (setq posto posfrom)
394     (while (and (< posto len)
395                 (> to (nth posto attr-value)))
396       (setq posto (1+ posto)))
397     (if  (= posto posfrom)
398         (if (= (% posto 2) 1)
399             (if (and (< to len)
400                      (= to (nth posto attr-value)))
401                 (set-marker (nth posto attr-value) from)
402               (setcdr (nthcdr (1- posfrom) attr-value)
403                       (cons (set-marker-type (set-marker (make-marker)
404                                                          from)
405                                              'point-type)
406                             (cons (set-marker-type (set-marker (make-marker)
407                                                                to)
408                                                    nil)
409                                   (nthcdr posto attr-value))))
410               (setcar attr-value (+ len 2))))
411       (if (= (% posfrom 2) 0)
412           (setq posfrom (1- posfrom))
413         (set-marker (nth posfrom attr-value) from))
414       (if (= (% posto 2) 0)
415           nil
416         (setq posto (1- posto))
417         (set-marker (nth posto attr-value) to))
418       (setcdr (nthcdr posfrom attr-value)
419               (nthcdr posto attr-value)))))
420
421 (defalias 'tl:make-overlay 'cons)
422
423 (defun tl:overlay-put (overlay prop value)
424   (let ((ret (and (eq prop 'face)
425                   (assq value tl:available-face-attribute-alist)
426                   )))
427     (if ret
428         (attribute-add-narrow-attribute (cdr ret)
429                                         (car overlay)(cdr overlay))
430       )))
431
432
433 ;;; @ end
434 ;;;
435
436 (provide 'emu-nemacs)
437
438 ;;; emu-nemacs.el ends here