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