Abolish constant `charset-ascii' and `charset-jisx0208'.
[elisp/apel.git] / emu-nemacs.el
1 ;;; emu-nemacs.el --- emu API implementation for NEmacs
2
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, NEmacs, mule
7
8 ;; This file is part of emu.
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'emu-18)
28
29
30 ;;; @ character set
31 ;;;
32
33 (put 'ascii
34      'charset-description "Character set of ASCII")
35 (put 'ascii
36      'charset-registry "ASCII")
37
38 (put 'japanese-jisx0208
39      'charset-description "Character set of JIS X0208-1983")
40 (put 'japanese-jisx0208
41      'charset-registry "JISX0208.1983")
42
43 (defun charset-description (charset)
44   "Return description of CHARSET. [emu-nemacs.el]"
45   (get charset 'charset-description)
46   )
47
48 (defun charset-registry (charset)
49   "Return registry name of CHARSET. [emu-nemacs.el]"
50   (get charset 'charset-registry)
51   )
52
53 (defun charset-columns (charset)
54   "Return number of columns a CHARSET occupies when displayed.
55 \[emu-nemacs.el]"
56   (if (eq charset 'ascii)
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       '(japanese-jisx0208)
70     ))
71
72 (defalias 'find-non-ascii-charset-string 'find-charset-string)
73
74 (defun find-charset-region (start end)
75   "Return a list of charsets in the region between START and END.
76 \[emu-nemacs.el; Mule emulating function]"
77   (if (save-excursion
78         (save-restriction
79           (narrow-to-region start end)
80           (goto-char start)
81           (re-search-forward "[\200-\377]" nil t)
82           ))
83       '(japanese-jisx0208)
84     ))
85
86 (defalias 'find-non-ascii-charset-region 'find-charset-region)
87
88 (defun check-ASCII-string (str)
89   (let ((i 0)
90         len)
91     (setq len (length str))
92     (catch 'label
93       (while (< i len)
94         (if (>= (elt str i) 128)
95             (throw 'label nil))
96         (setq i (+ i 1))
97         )
98       str)))
99
100 ;;; @@ for old MULE emulation
101 ;;;
102
103 ;;(defconst lc-ascii 0)
104 ;;(defconst lc-jp  146)
105
106
107 ;;; @ coding system
108 ;;;
109
110 (defconst *noconv*    0)
111 (defconst *sjis*      1)
112 (defconst *junet*     2)
113 (defconst *ctext*     2)
114 (defconst *internal*  3)
115 (defconst *euc-japan* 3)
116
117 (defun decode-coding-string (string coding-system)
118   "Decode the STRING which is encoded in CODING-SYSTEM.
119 \[emu-nemacs.el; EMACS 20 emulating function]"
120   (if (eq coding-system 3)
121       string
122     (convert-string-kanji-code string coding-system 3)
123     ))
124
125 (defun encode-coding-string (string coding-system)
126   "Encode the STRING to CODING-SYSTEM.
127 \[emu-nemacs.el; EMACS 20 emulating function]"
128   (if (eq coding-system 3)
129       string
130     (convert-string-kanji-code string 3 coding-system)
131     ))
132
133 (defun decode-coding-region (start end coding-system)
134   "Decode the text between START and END which is encoded in CODING-SYSTEM.
135 \[emu-nemacs.el; EMACS 20 emulating function]"
136   (if (/= ic oc)
137       (save-excursion
138         (save-restriction
139           (narrow-to-region start end)
140           (convert-region-kanji-code start end coding-system 3)
141           ))))
142
143 (defun encode-coding-region (start end coding-system)
144   "Encode the text between START and END to CODING-SYSTEM.
145 \[emu-nemacs.el; EMACS 20 emulating function]"
146   (if (/= ic oc)
147       (save-excursion
148         (save-restriction
149           (narrow-to-region start end)
150           (convert-region-kanji-code start end 3 coding-system)
151           ))))
152
153 (defun detect-coding-region (start end)
154   "Detect coding-system of the text in the region between START and END.
155 \[emu-nemacs.el; Emacs 20 emulating function]"
156   (if (save-excursion
157         (save-restriction
158           (narrow-to-region start end)
159           (goto-char start)
160           (re-search-forward "[\200-\377]" nil t)
161           ))
162       *euc-japan*
163     ))
164
165 (defalias 'set-buffer-file-coding-system 'set-kanji-fileio-code)
166
167 (defmacro as-binary-process (&rest body)
168   (` (let (selective-display    ; Disable ^M to nl translation.
169            ;; NEmacs
170            kanji-flag
171            (default-kanji-process-code 0)
172            program-kanji-code-alist)
173        (,@ body)
174        )))
175
176 (defmacro as-binary-input-file (&rest body)
177   (` (let (kanji-flag)
178        (,@ body)
179        )))
180
181 (defmacro as-binary-output-file (&rest body)
182   (` (let (kanji-flag)
183        (,@ body)
184        )))
185
186
187 ;;; @@ for old MULE emulation
188 ;;;
189
190 (defun code-convert-string (str ic oc)
191   "Convert code in STRING from SOURCE code to TARGET code,
192 On successful converion, returns the result string,
193 else returns nil. [emu-nemacs.el; Mule emulating function]"
194   (if (not (eq ic oc))
195       (convert-string-kanji-code str ic oc)
196     str))
197
198 (defun code-convert-region (beg end ic oc)
199   "Convert code of the text between BEGIN and END from SOURCE
200 to TARGET. On successful conversion returns t,
201 else returns nil. [emu-nemacs.el; Mule emulating function]"
202   (if (/= ic oc)
203       (save-excursion
204         (save-restriction
205           (narrow-to-region beg end)
206           (convert-region-kanji-code beg end ic oc)
207           ))))
208
209
210 ;;; @ binary access
211 ;;;
212
213 (defun insert-file-contents-as-binary (filename
214                                        &optional visit beg end replace)
215   "Like `insert-file-contents', q.v., but don't character code conversion.
216 \[emu-nemacs.el]"
217   (let (kanji-flag)
218     (insert-file-contents filename visit beg end replace)
219     ))
220
221 (fset 'insert-binary-file-contents 'insert-file-contents-as-binary)
222
223 (defun insert-binary-file-contents-literally (filename
224                                               &optional visit beg end replace)
225   "Like `insert-file-contents-literally', q.v., but don't code conversion.
226 A buffer may be modified in several ways after reading into the buffer due
227 to advanced Emacs features, such as file-name-handlers, format decoding,
228 find-file-hooks, etc.
229   This function ensures that none of these modifications will take place.
230 \[emu-nemacs.el]"
231   (let (kanji-flag)
232     (insert-file-contents-literally filename visit beg end replace)
233     ))
234
235 (defun write-region-as-binary (start end filename
236                                      &optional append visit lockname)
237   "Like `write-region', q.v., but don't code conversion. [emu-nemacs.el]"
238   (let (kanji-flag)
239     (write-region start end filename append visit)
240     ))
241
242
243 ;;; @ MIME charset
244 ;;;
245
246 (defvar charsets-mime-charset-alist
247   '(((ascii) . us-ascii)))
248
249 (defvar default-mime-charset 'iso-2022-jp)
250
251 (defvar mime-charset-coding-system-alist
252   '((iso-2022-jp     . 2)
253     (shift_jis       . 1)
254     ))
255
256 (defun mime-charset-to-coding-system (charset)
257   (if (stringp charset)
258       (setq charset (intern (downcase charset)))
259     )
260   (cdr (assq charset mime-charset-coding-system-alist))
261   )
262
263 (defun detect-mime-charset-region (start end)
264   "Return MIME charset for region between START and END.
265 \[emu-nemacs.el]"
266   (if (save-excursion
267         (save-restriction
268           (narrow-to-region start end)
269           (goto-char start)
270           (re-search-forward "[\200-\377]" nil t)
271           ))
272       default-mime-charset
273     'us-ascii))
274
275 (defun encode-mime-charset-region (start end charset)
276   "Encode the text between START and END as MIME CHARSET.
277 \[emu-nemacs.el]"
278   (let ((cs (mime-charset-to-coding-system charset)))
279     (and (numberp cs)
280          (or (= cs 3)
281              (save-excursion
282                (save-restriction
283                  (narrow-to-region start end)
284                  (convert-region-kanji-code start end 3 cs)
285                  ))
286              ))))
287
288 (defun decode-mime-charset-region (start end charset)
289   "Decode the text between START and END as MIME CHARSET.
290 \[emu-nemacs.el]"
291   (let ((cs (mime-charset-to-coding-system charset)))
292     (and (numberp cs)
293          (or (= cs 3)
294              (save-excursion
295                (save-restriction
296                  (narrow-to-region start end)
297                  (convert-region-kanji-code start end cs 3)
298                  ))
299              ))))
300
301 (defun encode-mime-charset-string (string charset)
302   "Encode the STRING as MIME CHARSET. [emu-nemacs.el]"
303   (let ((cs (mime-charset-to-coding-system charset)))
304     (if cs
305         (convert-string-kanji-code string 3 cs)
306       string)))
307
308 (defun decode-mime-charset-string (string charset)
309   "Decode the STRING as MIME CHARSET. [emu-nemacs.el]"
310   (let ((cs (mime-charset-to-coding-system charset)))
311     (if cs
312         (convert-string-kanji-code string cs 3)
313       string)))
314
315
316 ;;; @ character
317 ;;;
318
319 (defun char-charset (chr)
320   "Return the character set of char CHR.
321 \[emu-nemacs.el; MULE emulating function]"
322   (if (< chr 128)
323       'ascii
324     'japanese-jisx0208))
325
326 (defun char-bytes (chr)
327   "Return number of bytes CHAR will occupy in a buffer.
328 \[emu-nemacs.el; Mule emulating function]"
329   (if (< chr 128) 1 2))
330
331 (defun char-width (char)
332   "Return number of columns a CHAR occupies when displayed.
333 \[emu-nemacs.el]"
334   (if (< char 128)
335       1
336     2))
337
338 (defmacro char-next-index (char index)
339   "Return index of character succeeding CHAR whose index is INDEX."
340   (` (1+ index)))
341
342 ;;; @@ Old Mule emulating aliases
343 ;;;
344 ;;; You should not use them.
345
346 (defalias 'char-leading-char 'char-charset)
347
348 ;;; @@ obsoleted aliases
349 ;;;
350 ;;; You should not use them.
351
352 (defalias 'char-length 'char-bytes)
353 (defalias 'char-columns 'char-width)
354
355
356 ;;; @ string
357 ;;;
358
359 (defalias 'string-columns 'length)
360
361 (defun sref (str idx)
362   "Return the character in STR at index IDX.
363 \[emu-nemacs.el; Mule emulating function]"
364   (let ((chr (aref str idx)))
365     (if (< chr 128)
366         chr
367       (logior (lsh (aref str (1+ idx)) 8) chr)
368       )))
369
370 (defun string-to-char-list (str)
371   (let ((i 0)(len (length str)) dest chr)
372     (while (< i len)
373       (setq chr (aref str i))
374       (if (>= chr 128)
375           (setq i (1+ i)
376                 chr (+ (lsh chr 8) (aref str i))
377                 ))
378       (setq dest (cons chr dest))
379       (setq i (1+ i))
380       )
381     (reverse dest)
382     ))
383
384 (fset 'string-to-int-list (symbol-function 'string-to-char-list))
385
386 ;;; Imported from Mule-2.3
387 (defun truncate-string (str width &optional start-column)
388   "Truncate STR to fit in WIDTH columns.
389 Optional non-nil arg START-COLUMN specifies the starting column.
390 \[emu-mule.el; Mule 2.3 emulating function]"
391   (or start-column
392       (setq start-column 0))
393   (let ((max-width (string-width str))
394         (len (length str))
395         (from 0)
396         (column 0)
397         to-prev to ch)
398     (if (>= width max-width)
399         (setq width max-width))
400     (if (>= start-column width)
401         ""
402       (while (< column start-column)
403         (setq ch (aref str from)
404               column (+ column (char-columns ch))
405               from (+ from (char-bytes ch))))
406       (if (< width max-width)
407           (progn
408             (setq to from)
409             (while (<= column width)
410               (setq ch (aref str to)
411                     column (+ column (char-columns ch))
412                     to-prev to
413                     to (+ to (char-bytes ch))))
414             (setq to to-prev)))
415       (substring str from to))))
416
417 ;;; @@ for Mule emulation
418 ;;;
419
420 (defalias 'string-width 'length)
421
422
423 ;;; @ text property emulation
424 ;;;
425
426 (defvar emu:available-face-attribute-alist
427   '(
428     ;;(bold      . inversed-region)
429     (italic    . underlined-region)
430     (underline . underlined-region)
431     ))
432
433 ;; by YAMATE Keiichirou 1994/10/28
434 (defun attribute-add-narrow-attribute (attr from to)
435   (or (consp (symbol-value attr))
436       (set attr (list 1)))
437   (let* ((attr-value (symbol-value attr))
438          (len (car attr-value))
439          (posfrom 1)
440          posto)
441     (while (and (< posfrom len)
442                 (> from (nth posfrom attr-value)))
443       (setq posfrom (1+ posfrom)))
444     (setq posto posfrom)
445     (while (and (< posto len)
446                 (> to (nth posto attr-value)))
447       (setq posto (1+ posto)))
448     (if  (= posto posfrom)
449         (if (= (% posto 2) 1)
450             (if (and (< to len)
451                      (= to (nth posto attr-value)))
452                 (set-marker (nth posto attr-value) from)
453               (setcdr (nthcdr (1- posfrom) attr-value)
454                       (cons (set-marker-type (set-marker (make-marker)
455                                                          from)
456                                              'point-type)
457                             (cons (set-marker-type (set-marker (make-marker)
458                                                                to)
459                                                    nil)
460                                   (nthcdr posto attr-value))))
461               (setcar attr-value (+ len 2))))
462       (if (= (% posfrom 2) 0)
463           (setq posfrom (1- posfrom))
464         (set-marker (nth posfrom attr-value) from))
465       (if (= (% posto 2) 0)
466           nil
467         (setq posto (1- posto))
468         (set-marker (nth posto attr-value) to))
469       (setcdr (nthcdr posfrom attr-value)
470               (nthcdr posto attr-value)))))
471
472 (defalias 'make-overlay 'cons)
473
474 (defun overlay-put (overlay prop value)
475   (let ((ret (and (eq prop 'face)
476                   (assq value emu:available-face-attribute-alist)
477                   )))
478     (if ret
479         (attribute-add-narrow-attribute (cdr ret)
480                                         (car overlay)(cdr overlay))
481       )))
482
483
484 ;;; @ end
485 ;;;
486
487 (provide 'emu-nemacs)
488
489 ;;; emu-nemacs.el ends here