* poem-20.el, poem-ltn1.el, poem-nemacs.el, poem-om.el
[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 (if (integerp coding-system)
116                   coding-system
117                 (cdr (assq coding-system coding-system-kanji-code-alist)))))
118     (if (eq code 3)
119         string
120       (convert-string-kanji-code string code 3)
121       )))
122
123 (defun encode-coding-string (string coding-system)
124   "Encode the STRING to CODING-SYSTEM.
125 \[emu-nemacs.el; EMACS 20 emulating function]"
126   (let ((code (if (integerp coding-system)
127                   coding-system
128                 (cdr (assq coding-system coding-system-kanji-code-alist)))))
129     (if (eq code 3)
130         string
131       (convert-string-kanji-code string 3 code)
132       )))
133
134 (defun decode-coding-region (start end coding-system)
135   "Decode the text between START and END which is encoded in CODING-SYSTEM.
136 \[emu-nemacs.el; EMACS 20 emulating function]"
137   (let ((code (if (integerp coding-system)
138                   coding-system
139                 (cdr (assq coding-system coding-system-kanji-code-alist)))))
140     (save-excursion
141       (save-restriction
142         (narrow-to-region start end)
143         (convert-region-kanji-code start end code 3)
144         ))))
145
146 (defun encode-coding-region (start end coding-system)
147   "Encode the text between START and END to CODING-SYSTEM.
148 \[emu-nemacs.el; EMACS 20 emulating function]"
149   (let ((code (if (integerp coding-system)
150                   coding-system
151                 (cdr (assq coding-system coding-system-kanji-code-alist)))))
152     (save-excursion
153       (save-restriction
154         (narrow-to-region start end)
155         (convert-region-kanji-code start end 3 code)
156         ))))
157
158 (defun detect-coding-region (start end)
159   "Detect coding-system of the text in the region between START and END.
160 \[emu-nemacs.el; Emacs 20 emulating function]"
161   (if (save-excursion
162         (save-restriction
163           (narrow-to-region start end)
164           (goto-char start)
165           (re-search-forward "[\200-\377]" nil t)))
166       'euc-jp
167     ))
168
169 (defalias 'set-buffer-file-coding-system 'set-kanji-fileio-code)
170
171
172 ;;; @ without code-conversion
173 ;;;
174
175 (defmacro as-binary-process (&rest body)
176   (` (let (selective-display    ; Disable ^M to nl translation.
177            ;; NEmacs
178            kanji-flag
179            (default-kanji-process-code 0)
180            program-kanji-code-alist)
181        (,@ body))))
182
183 (defmacro as-binary-input-file (&rest body)
184   (` (let (kanji-flag)
185        (,@ body))))
186
187 (defmacro as-binary-output-file (&rest body)
188   (` (let (kanji-flag)
189        (,@ body))))
190
191 (defun write-region-as-binary (start end filename
192                                      &optional append visit lockname)
193   "Like `write-region', q.v., but don't code conversion. [emu-nemacs.el]"
194   (as-binary-output-file
195    (write-region start end filename append visit)))
196
197 (defun insert-file-contents-as-binary (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)))
204
205 (defun insert-file-contents-as-raw-text (filename
206                                          &optional visit beg end replace)
207   "Like `insert-file-contents', q.v., but don't character code conversion.
208 \[emu-nemacs.el]"
209   (as-binary-input-file
210    ;; Returns list absolute file name and length of data inserted.
211    (insert-file-contents filename visit)))
212
213 (defun write-region-as-raw-text-CRLF (start end filename
214                                             &optional append visit lockname)
215   "Like `write-region', q.v., but don't code conversion. [emu-nemacs.el]"
216   (let ((the-buf (current-buffer)))
217     (with-temp-buffer
218       (insert-buffer-substring the-buf start end)
219       (goto-char (point-min))
220       (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
221         (replace-match "\\1\r\n"))
222       (write-region-as-binary (point-min)(point-max)
223                               filename append visit))))
224
225 (defun find-file-noselect-as-binary (filename &optional nowarn rawfile)
226   "Like `find-file-noselect', q.v., but don't code conversion."
227   (as-binary-input-file (find-file-noselect filename nowarn rawfile)))
228
229 (defun find-file-noselect-as-raw-text (filename &optional nowarn rawfile)
230   "Like `find-file-noselect', q.v., but it does not code conversion
231 except for line-break code."
232   (as-binary-input-file (find-file-noselect filename nowarn rawfile)))
233
234 (defun open-network-stream-as-binary (name buffer host service)
235   "Like `open-network-stream', q.v., but don't code conversion."
236   (let ((process (open-network-stream name buffer host service)))
237     (set-process-kanji-code process 0)
238     process))
239
240 (defun save-buffer-as-binary (&optional args)
241   "Like `save-buffer', q.v., but don't encode."
242   (as-binary-output-file
243    (save-buffer args)))
244
245 (defun save-buffer-as-raw-text-CRLF (&optional args)
246   "Like `save-buffer', q.v., but save as network representation."
247   (if (buffer-modified-p)
248       (save-restriction
249         (widen)
250         (let ((the-buf (current-buffer))
251               (filename (buffer-file-name)))
252           (if filename
253               (prog1
254                   (with-temp-buffer
255                     (insert-buffer the-buf)
256                     (goto-char (point-min))
257                     (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
258                       (replace-match "\\1\r\n"))
259                     (setq buffer-file-name filename)
260                     (save-buffer-as-binary args))
261                 (set-buffer-modified-p nil)
262                 (clear-visited-file-modtime)))))))
263
264
265 ;;; @ with code-conversion
266 ;;;
267
268 (defun insert-file-contents-as-coding-system
269   (coding-system filename &optional visit beg end replace)
270   "Like `insert-file-contents', q.v., but CODING-SYSTEM the first arg will
271 be applied to `kanji-fileio-code'."
272   (let ((kanji-fileio-code coding-system)
273         kanji-expected-code)
274     (insert-file-contents filename visit)))
275
276 (defun write-region-as-coding-system
277   (coding-system start end filename &optional append visit lockname)
278   "Like `write-region', q.v., but CODING-SYSTEM the first arg will be
279 applied to `kanji-fileio-code'."
280   (let ((kanji-fileio-code coding-system)
281         jka-compr-compression-info-list jam-zcat-filename-list)
282     (write-region start end filename append visit)))
283
284 (defun find-file-noselect-as-coding-system
285   (coding-system filename &optional nowarn rawfile)
286   "Like `find-file-noselect', q.v., but CODING-SYSTEM the first arg will
287 be applied to `kanji-fileio-code'."
288   (let ((kanji-fileio-code coding-system)
289         kanji-expected-code)
290     (find-file-noselect filename nowarn)))
291
292 (defun save-buffer-as-coding-system (coding-system &optional args)
293   "Like `save-buffer', q.v., but CODING-SYSTEM the first arg will be
294 applied to `kanji-fileio-code'."
295   (let ((kanji-fileio-code coding-system))
296     (save-buffer args)))
297
298
299 ;;; @ buffer representation
300 ;;;
301
302 (defsubst-maybe set-buffer-multibyte (flag)
303   "Set the multibyte flag of the current buffer to FLAG.
304 If FLAG is t, this makes the buffer a multibyte buffer.
305 If FLAG is nil, this makes the buffer a single-byte buffer.
306 The buffer contents remain unchanged as a sequence of bytes
307 but the contents viewed as characters do change.
308 \[Emacs 20.3 emulating function]"
309   (setq kanji-flag flag)
310   )
311
312
313 ;;; @ character
314 ;;;
315
316 (defun char-charset (chr)
317   "Return the character set of char CHR.
318 \[emu-nemacs.el; MULE emulating function]"
319   (if (< chr 128)
320       'ascii
321     'japanese-jisx0208))
322
323 (defun char-bytes (chr)
324   "Return number of bytes CHAR will occupy in a buffer.
325 \[emu-nemacs.el; Mule emulating function]"
326   (if (< chr 128)
327       1
328     2))
329
330 (defun char-width (char)
331   "Return number of columns a CHAR occupies when displayed.
332 \[emu-nemacs.el]"
333   (if (< char 128)
334       1
335     2))
336
337 (defalias 'char-length 'char-bytes)
338
339 (defmacro char-next-index (char index)
340   "Return index of character succeeding CHAR whose index is INDEX."
341   (` (+ (, index) (char-bytes (, char)))))
342
343
344 ;;; @ string
345 ;;;
346
347 (defalias 'string-width 'length)
348
349 (defun sref (str idx)
350   "Return the character in STR at index IDX.
351 \[emu-nemacs.el; Mule emulating function]"
352   (let ((chr (aref str idx)))
353     (if (< chr 128)
354         chr
355       (logior (lsh (aref str (1+ idx)) 8) chr))))
356
357 (defun string-to-char-list (str)
358   (let ((i 0)(len (length str)) dest chr)
359     (while (< i len)
360       (setq chr (aref str i))
361       (if (>= chr 128)
362           (setq i (1+ i)
363                 chr (+ (lsh chr 8) (aref str i)))
364         )
365       (setq dest (cons chr dest))
366       (setq i (1+ i)))
367     (reverse dest)))
368
369 (fset 'string-to-int-list (symbol-function 'string-to-char-list))
370
371 ;;; Imported from Mule-2.3
372 (defun truncate-string (str width &optional start-column)
373   "Truncate STR to fit in WIDTH columns.
374 Optional non-nil arg START-COLUMN specifies the starting column.
375 \[emu-mule.el; Mule 2.3 emulating function]"
376   (or start-column
377       (setq start-column 0))
378   (let ((max-width (string-width str))
379         (len (length str))
380         (from 0)
381         (column 0)
382         to-prev to ch)
383     (if (>= width max-width)
384         (setq width max-width))
385     (if (>= start-column width)
386         ""
387       (while (< column start-column)
388         (setq ch (aref str from)
389               column (+ column (char-columns ch))
390               from (+ from (char-bytes ch))))
391       (if (< width max-width)
392           (progn
393             (setq to from)
394             (while (<= column width)
395               (setq ch (aref str to)
396                     column (+ column (char-columns ch))
397                     to-prev to
398                     to (+ to (char-bytes ch))))
399             (setq to to-prev)))
400       (substring str from to))))
401
402 (defalias 'looking-at-as-unibyte 'looking-at)
403
404 ;;; @@ obsoleted aliases
405 ;;;
406 ;;; You should not use them.
407
408 (defalias 'string-columns 'length)
409
410
411 ;;; @ end
412 ;;;
413
414 (provide 'poem-nemacs)
415
416 ;;; poem-nemacs.el ends here