tm 7.95.
[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.49 1996/10/01 13:26:34 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 (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
179 ;;; @@ for old MULE emulation
180 ;;;
181
182 (defun code-convert-string (str ic oc)
183   "Convert code in STRING from SOURCE code to TARGET code,
184 On successful converion, returns the result string,
185 else returns nil. [emu-nemacs.el; Mule emulating function]"
186   (if (not (eq ic oc))
187       (convert-string-kanji-code str ic oc)
188     str))
189
190 (defun code-convert-region (beg end ic oc)
191   "Convert code of the text between BEGIN and END from SOURCE
192 to TARGET. On successful conversion returns t,
193 else returns nil. [emu-nemacs.el; Mule emulating function]"
194   (if (/= ic oc)
195       (save-excursion
196         (save-restriction
197           (narrow-to-region beg end)
198           (convert-region-kanji-code beg end ic oc)
199           ))))
200
201
202 ;;; @ MIME charset
203 ;;;
204
205 (defvar charsets-mime-charset-alist
206   (list (cons (list charset-ascii) 'us-ascii)))
207
208 (defvar default-mime-charset 'iso-2022-jp)
209
210 (defvar mime-charset-coding-system-alist
211   '((iso-2022-jp     . 2)
212     (shift_jis       . 1)
213     ))
214
215 (defun mime-charset-to-coding-system (charset)
216   (if (stringp charset)
217       (setq charset (intern (downcase charset)))
218     )
219   (cdr (assq charset mime-charset-coding-system-alist))
220   )
221
222 (defun detect-mime-charset-region (start end)
223   "Return MIME charset for region between START and END.
224 \[emu-nemacs.el]"
225   (if (save-excursion
226         (save-restriction
227           (narrow-to-region start end)
228           (goto-char start)
229           (re-search-forward "[\200-\377]" nil t)
230           ))
231       default-mime-charset
232     'us-ascii))
233
234 (defun encode-mime-charset-region (start end charset)
235   "Encode the text between START and END as MIME CHARSET.
236 \[emu-nemacs.el]"
237   (let ((cs (mime-charset-to-coding-system charset)))
238     (and (numberp cs)
239          (or (= cs 3)
240              (save-excursion
241                (save-restriction
242                  (narrow-to-region start end)
243                  (convert-region-kanji-code start end 3 cs)
244                  ))
245              ))))
246
247 (defun decode-mime-charset-region (start end charset)
248   "Decode the text between START and END as MIME CHARSET.
249 \[emu-nemacs.el]"
250   (let ((cs (mime-charset-to-coding-system charset)))
251     (and (numberp cs)
252          (or (= cs 3)
253              (save-excursion
254                (save-restriction
255                  (narrow-to-region start end)
256                  (convert-region-kanji-code start end cs 3)
257                  ))
258              ))))
259
260 (defun encode-mime-charset-string (string charset)
261   "Encode the STRING as MIME CHARSET. [emu-nemacs.el]"
262   (let ((cs (mime-charset-to-coding-system charset)))
263     (if cs
264         (convert-string-kanji-code string 3 cs)
265       string)))
266
267 (defun decode-mime-charset-string (string charset)
268   "Decode the STRING as MIME CHARSET. [emu-nemacs.el]"
269   (let ((cs (mime-charset-to-coding-system charset)))
270     (if cs
271         (convert-string-kanji-code string cs 3)
272       string)))
273
274
275 ;;; @ character
276 ;;;
277
278 (defun char-charset (chr)
279   "Return the character set of char CHR.
280 \[emu-nemacs.el; XEmacs 20 emulating function]"
281   (if (< chr 128)
282       charset-ascii
283     charset-jisx0208))
284
285 (defun char-bytes (chr)
286   "Return number of bytes CHAR will occupy in a buffer.
287 \[emu-nemacs.el; Mule emulating function]"
288   (if (< chr 128) 1 2))
289
290 (defalias 'char-length 'char-bytes)
291
292 (defun char-columns (character)
293   "Return number of columns a CHARACTER occupies when displayed.
294 \[emu-nemacs.el]"
295   (if (< character 128)
296       1
297     2))
298
299 ;;; @@ for Mule emulation
300 ;;;
301
302 (defalias 'char-leading-char 'char-charset)
303
304 (defalias 'char-width 'char-columns)
305
306
307 ;;; @ string
308 ;;;
309
310 (defalias 'string-columns 'length)
311
312 (defun sref (str idx)
313   "Return the character in STR at index IDX.
314 \[emu-nemacs.el; Mule emulating function]"
315   (let ((chr (aref str idx)))
316     (if (< chr 128)
317         chr
318       (logior (lsh (aref str (1+ idx)) 8) chr)
319       )))
320
321 (defun string-to-char-list (str)
322   (let ((i 0)(len (length str)) dest chr)
323     (while (< i len)
324       (setq chr (aref str i))
325       (if (>= chr 128)
326           (setq i (1+ i)
327                 chr (+ (lsh chr 8) (aref str i))
328                 ))
329       (setq dest (cons chr dest))
330       (setq i (1+ i))
331       )
332     (reverse dest)
333     ))
334
335 (fset 'string-to-int-list (symbol-function 'string-to-char-list))
336
337 ;;; Imported from Mule-2.3
338 (defun truncate-string (str width &optional start-column)
339   "Truncate STR to fit in WIDTH columns.
340 Optional non-nil arg START-COLUMN specifies the starting column.
341 \[emu-mule.el; Mule 2.3 emulating function]"
342   (or start-column
343       (setq start-column 0))
344   (let ((max-width (string-width str))
345         (len (length str))
346         (from 0)
347         (column 0)
348         to-prev to ch)
349     (if (>= width max-width)
350         (setq width max-width))
351     (if (>= start-column width)
352         ""
353       (while (< column start-column)
354         (setq ch (aref str from)
355               column (+ column (char-columns ch))
356               from (+ from (char-bytes ch))))
357       (if (< width max-width)
358           (progn
359             (setq to from)
360             (while (<= column width)
361               (setq ch (aref str to)
362                     column (+ column (char-columns ch))
363                     to-prev to
364                     to (+ to (char-bytes ch))))
365             (setq to to-prev)))
366       (substring str from to))))
367
368 ;;; @@ for Mule emulation
369 ;;;
370
371 (defalias 'string-width 'length)
372
373
374 ;;; @ text property emulation
375 ;;;
376
377 (setq tl:available-face-attribute-alist
378       '(
379         ;;(bold      . inversed-region)
380         (italic    . underlined-region)
381         (underline . underlined-region)
382         ))
383
384 ;; by YAMATE Keiichirou 1994/10/28
385 (defun attribute-add-narrow-attribute (attr from to)
386   (or (consp (symbol-value attr))
387       (set attr (list 1)))
388   (let* ((attr-value (symbol-value attr))
389          (len (car attr-value))
390          (posfrom 1)
391          posto)
392     (while (and (< posfrom len)
393                 (> from (nth posfrom attr-value)))
394       (setq posfrom (1+ posfrom)))
395     (setq posto posfrom)
396     (while (and (< posto len)
397                 (> to (nth posto attr-value)))
398       (setq posto (1+ posto)))
399     (if  (= posto posfrom)
400         (if (= (% posto 2) 1)
401             (if (and (< to len)
402                      (= to (nth posto attr-value)))
403                 (set-marker (nth posto attr-value) from)
404               (setcdr (nthcdr (1- posfrom) attr-value)
405                       (cons (set-marker-type (set-marker (make-marker)
406                                                          from)
407                                              'point-type)
408                             (cons (set-marker-type (set-marker (make-marker)
409                                                                to)
410                                                    nil)
411                                   (nthcdr posto attr-value))))
412               (setcar attr-value (+ len 2))))
413       (if (= (% posfrom 2) 0)
414           (setq posfrom (1- posfrom))
415         (set-marker (nth posfrom attr-value) from))
416       (if (= (% posto 2) 0)
417           nil
418         (setq posto (1- posto))
419         (set-marker (nth posto attr-value) to))
420       (setcdr (nthcdr posfrom attr-value)
421               (nthcdr posto attr-value)))))
422
423 (defalias 'tl:make-overlay 'cons)
424
425 (defun tl:overlay-put (overlay prop value)
426   (let ((ret (and (eq prop 'face)
427                   (assq value tl:available-face-attribute-alist)
428                   )))
429     (if ret
430         (attribute-add-narrow-attribute (cdr ret)
431                                         (car overlay)(cdr overlay))
432       )))
433
434
435 ;;; @ end
436 ;;;
437
438 (provide 'emu-nemacs)
439
440 ;;; emu-nemacs.el ends here