(set-buffer-multibyte): Use `defun-maybe' instead of `defmacro-maybe'.
[elisp/apel.git] / poem-nemacs.el
1 ;;; poem-nemacs.el --- poem implementation for Nemacs
2
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, Mule
7
8 ;; This file is part of APEL (A Portable Emacs Library).
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 ;;; @ character set
28 ;;;
29
30 (put 'ascii
31      'charset-description "Character set of ASCII")
32 (put 'ascii
33      'charset-registry "ASCII")
34
35 (put 'japanese-jisx0208
36      'charset-description "Character set of JIS X0208-1983")
37 (put 'japanese-jisx0208
38      'charset-registry "JISX0208.1983")
39
40 (defun charset-description (charset)
41   "Return description of CHARSET. [emu-nemacs.el]"
42   (get charset 'charset-description))
43
44 (defun charset-registry (charset)
45   "Return registry name of CHARSET. [emu-nemacs.el]"
46   (get charset 'charset-registry))
47
48 (defun charset-width (charset)
49   "Return number of columns a CHARSET occupies when displayed.
50 \[emu-nemacs.el]"
51   (if (eq charset 'ascii)
52       1
53     2))
54
55 (defun charset-direction (charset)
56   "Return the direction of a character of CHARSET by
57   0 (left-to-right) or 1 (right-to-left). [emu-nemacs.el]"
58   0)
59
60 (defun find-charset-string (str)
61   "Return a list of charsets in the string.
62 \[emu-nemacs.el; Mule emulating function]"
63   (if (string-match "[\200-\377]" str)
64       '(japanese-jisx0208)
65     ))
66
67 (defalias 'find-non-ascii-charset-string 'find-charset-string)
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       '(japanese-jisx0208)
78     ))
79
80 (defalias 'find-non-ascii-charset-region 'find-charset-region)
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       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 (defvar coding-system-kanji-code-alist
104   '((binary      . 0)
105     (raw-text    . 0)
106     (shift_jis   . 1)
107     (iso-2022-jp . 2)
108     (ctext       . 2)
109     (euc-jp      . 3)
110     ))
111
112 (defun decode-coding-string (string coding-system)
113   "Decode the STRING which is encoded in CODING-SYSTEM.
114 \[emu-nemacs.el; EMACS 20 emulating function]"
115   (let ((code (cdr (assq coding-system coding-system-kanji-code-alist))))
116     (if (eq code 3)
117         string
118       (convert-string-kanji-code string code 3)
119       )))
120
121 (defun encode-coding-string (string coding-system)
122   "Encode the STRING to CODING-SYSTEM.
123 \[emu-nemacs.el; EMACS 20 emulating function]"
124   (let ((code (cdr (assq coding-system coding-system-kanji-code-alist))))
125     (if (eq code 3)
126         string
127       (convert-string-kanji-code string 3 code)
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   (let ((code (cdr (assq coding-system coding-system-kanji-code-alist))))
134     (save-excursion
135       (save-restriction
136         (narrow-to-region start end)
137         (convert-region-kanji-code start end code 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   (let ((code (cdr (assq coding-system coding-system-kanji-code-alist))))
144     (save-excursion
145       (save-restriction
146         (narrow-to-region start end)
147         (convert-region-kanji-code start end 3 code)
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       'euc-jp
159     ))
160
161 (defalias 'set-buffer-file-coding-system 'set-kanji-fileio-code)
162
163
164 ;;; @ without code-conversion
165 ;;;
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 (defmacro as-binary-input-file (&rest body)
176   (` (let (kanji-flag)
177        (,@ body))))
178
179 (defmacro as-binary-output-file (&rest body)
180   (` (let (kanji-flag)
181        (,@ body))))
182
183 (defun write-region-as-binary (start end filename
184                                      &optional append visit lockname)
185   "Like `write-region', q.v., but don't code conversion. [emu-nemacs.el]"
186   (as-binary-output-file
187    (write-region start end filename append visit)))
188
189 (defun insert-file-contents-as-binary (filename
190                                        &optional visit beg end replace)
191   "Like `insert-file-contents', q.v., but don't character code conversion.
192 \[emu-nemacs.el]"
193   (as-binary-input-file
194    ;; Returns list absolute file name and length of data inserted.
195    (insert-file-contents filename visit beg end replace)))
196
197 (defun insert-file-contents-as-raw-text (filename
198                                          &optional visit beg end replace)
199   "Like `insert-file-contents', q.v., but don't character code conversion.
200 \[emu-nemacs.el]"
201   (as-binary-input-file
202    ;; Returns list absolute file name and length of data inserted.
203    (insert-file-contents filename visit beg end replace)))
204
205 (defun write-region-as-raw-text-CRLF (start end filename
206                                             &optional append visit lockname)
207   "Like `write-region', q.v., but don't code conversion. [emu-nemacs.el]"
208   (let ((the-buf (current-buffer)))
209     (with-temp-buffer
210       (insert-buffer-substring the-buf start end)
211       (goto-char (point-min))
212       (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
213         (replace-match "\\1\r\n"))
214       (write-region-as-binary (point-min)(point-max)
215                               filename append visit))))
216
217
218 ;;; @ buffer representation
219 ;;;
220
221 (defsubst-maybe set-buffer-multibyte (flag)
222   "Set the multibyte flag of the current buffer to FLAG.
223 If FLAG is t, this makes the buffer a multibyte buffer.
224 If FLAG is nil, this makes the buffer a single-byte buffer.
225 The buffer contents remain unchanged as a sequence of bytes
226 but the contents viewed as characters do change.
227 \[Emacs 20.3 emulating function]"
228   (setq kanji-flag flag)
229   )
230
231
232 ;;; @ character
233 ;;;
234
235 (defun char-charset (chr)
236   "Return the character set of char CHR.
237 \[emu-nemacs.el; MULE emulating function]"
238   (if (< chr 128)
239       'ascii
240     'japanese-jisx0208))
241
242 (defun char-bytes (chr)
243   "Return number of bytes CHAR will occupy in a buffer.
244 \[emu-nemacs.el; Mule emulating function]"
245   (if (< chr 128)
246       1
247     2))
248
249 (defun char-width (char)
250   "Return number of columns a CHAR occupies when displayed.
251 \[emu-nemacs.el]"
252   (if (< char 128)
253       1
254     2))
255
256 (defalias 'char-length 'char-bytes)
257
258 (defmacro char-next-index (char index)
259   "Return index of character succeeding CHAR whose index is INDEX."
260   (` (+ (, index) (char-bytes (, char)))))
261
262
263 ;;; @ string
264 ;;;
265
266 (defalias 'string-width 'length)
267
268 (defun sref (str idx)
269   "Return the character in STR at index IDX.
270 \[emu-nemacs.el; Mule emulating function]"
271   (let ((chr (aref str idx)))
272     (if (< chr 128)
273         chr
274       (logior (lsh (aref str (1+ idx)) 8) chr))))
275
276 (defun string-to-char-list (str)
277   (let ((i 0)(len (length str)) dest chr)
278     (while (< i len)
279       (setq chr (aref str i))
280       (if (>= chr 128)
281           (setq i (1+ i)
282                 chr (+ (lsh chr 8) (aref str i)))
283         )
284       (setq dest (cons chr dest))
285       (setq i (1+ i)))
286     (reverse dest)))
287
288 (fset 'string-to-int-list (symbol-function 'string-to-char-list))
289
290 ;;; Imported from Mule-2.3
291 (defun truncate-string (str width &optional start-column)
292   "Truncate STR to fit in WIDTH columns.
293 Optional non-nil arg START-COLUMN specifies the starting column.
294 \[emu-mule.el; Mule 2.3 emulating function]"
295   (or start-column
296       (setq start-column 0))
297   (let ((max-width (string-width str))
298         (len (length str))
299         (from 0)
300         (column 0)
301         to-prev to ch)
302     (if (>= width max-width)
303         (setq width max-width))
304     (if (>= start-column width)
305         ""
306       (while (< column start-column)
307         (setq ch (aref str from)
308               column (+ column (char-columns ch))
309               from (+ from (char-bytes ch))))
310       (if (< width max-width)
311           (progn
312             (setq to from)
313             (while (<= column width)
314               (setq ch (aref str to)
315                     column (+ column (char-columns ch))
316                     to-prev to
317                     to (+ to (char-bytes ch))))
318             (setq to to-prev)))
319       (substring str from to))))
320
321 (defalias 'looking-at-as-unibyte 'looking-at)
322
323 ;;; @@ obsoleted aliases
324 ;;;
325 ;;; You should not use them.
326
327 (defalias 'string-columns 'length)
328
329
330 ;;; @ end
331 ;;;
332
333 (provide 'poem-nemacs)
334
335 ;;; poem-nemacs.el ends here