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