* poem-om.el (poem-ccl-decode-raw-text): Rewrite for fixing a bug that the last
[elisp/apel.git] / poem-om.el
1 ;;; poem-om.el --- poem implementation for Mule 1.* and Mule 2.*
2
3 ;; Copyright (C) 1995-1999 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
7 ;; Keywords: emulation, compatibility, Mule
8
9 ;; This file is part of APEL (A Portable Emacs 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 'poe)
29
30
31 ;;; @ version specific features
32 ;;;
33
34 (cond ((= emacs-major-version 19)
35        ;; Suggested by SASAKI Osamu <osamu@shuugr.bekkoame.or.jp>
36        ;; (cf. [os2-emacs-ja:78])
37        (defun fontset-pixel-size (fontset)
38          (let* ((font (get-font-info
39                        (aref (cdr (get-fontset-info fontset)) 0)))
40                 (open (aref font 4)))
41            (if (= open 1)
42                (aref font 5)
43              (if (= open 0)
44                  (let ((pat (aref font 1)))
45                    (if (string-match "-[0-9]+-" pat)
46                        (string-to-number
47                         (substring
48                          pat (1+ (match-beginning 0)) (1- (match-end 0))))
49                      0))
50                ))))
51
52        (define-ccl-program poem-ccl-decode-raw-text
53          '(1
54            (loop
55              (read-if (r0 == ?\x0d)
56                       (read-if (r1 == ?\x0a)
57                                (write r1)
58                                ((write r0)
59                                 (write r1)))
60                       (write r0))
61              (repeat)))
62          "Convert line-break code from CRLF to LF.")
63
64        (define-ccl-program poem-ccl-encode-raw-text
65          '(1
66            ((read r0)
67             (loop (write-read-repeat r0))))
68          "Pass through without any conversions.")
69
70        (define-ccl-program poem-ccl-encode-raw-text-CRLF
71          '(2
72            ((loop
73               (read-if (r0 == ?\x0a)
74                        (write "\x0d\x0a")
75                        (write r0))
76               (repeat))))
77          "Convert line-break code from LF to CRLF.")
78
79        (make-coding-system
80         'raw-text 4 ?=
81         "No conversion"
82         nil
83         (cons poem-ccl-decode-raw-text poem-ccl-encode-raw-text))
84
85        (make-coding-system
86         'raw-text-dos 4 ?=
87         "No conversion"
88         nil
89         (cons poem-ccl-decode-raw-text poem-ccl-encode-raw-text-CRLF))
90        )
91       (t
92        (defun poem-decode-raw-text (from to)
93          (save-restriction
94            (narrow-to-region from to)
95            (goto-char (point-min))
96            (while (re-search-forward "\r$" nil t)
97              (replace-match "")
98              )))
99        (defun poem-encode-raw-text-CRLF (from to)
100          (save-restriction
101            (narrow-to-region from to)
102            (goto-char (point-min))
103            (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
104              (replace-match "\\1\r\n")
105              )))
106
107        (make-coding-system 'raw-text nil ?= "No conversion")
108        (put 'raw-text 'post-read-conversion 'poem-decode-raw-text)
109        
110        (make-coding-system 'raw-text-dos nil ?= "No conversion")
111        (put 'raw-text-dos 'post-read-conversion 'poem-decode-raw-text)
112        (put 'raw-text-dos 'pre-write-conversion 'poem-encode-raw-text-CRLF)
113        ))
114
115
116 ;;; @ character set
117 ;;;
118
119 (defalias 'make-char 'make-character)
120
121 (defalias 'find-non-ascii-charset-string 'find-charset-string)
122 (defalias 'find-non-ascii-charset-region 'find-charset-region)
123
124 (defalias 'charset-bytes        'char-bytes)
125 (defalias 'charset-description  'char-description)
126 (defalias 'charset-registry     'char-registry)
127 (defalias 'charset-columns      'char-width)
128 (defalias 'charset-direction    'char-direction)
129
130 (defun charset-chars (charset)
131   "Return the number of characters per dimension of CHARSET."
132   (if (= (logand (nth 2 (character-set charset)) 1) 1)
133       96
134     94))
135
136
137 ;;; @ coding system
138 ;;;
139
140 (defun encode-coding-region (start end coding-system)
141   "Encode the text between START and END to CODING-SYSTEM.
142 \[EMACS 20 emulating function]"
143   ;; If `coding-system' is nil, do nothing.
144   (code-convert-region start end *internal* coding-system))
145
146 (defun decode-coding-region (start end coding-system)
147   "Decode the text between START and END which is encoded in CODING-SYSTEM.
148 \[EMACS 20 emulating function]"
149   ;; If `coding-system' is nil, do nothing.
150   (code-convert-region start end coding-system *internal*))
151
152 ;; XXX: Should we support optional NOCOPY argument? (only in Emacs 20.x)
153 (defun encode-coding-string (str coding-system)
154   "Encode the STRING to CODING-SYSTEM.
155 \[EMACS 20 emulating function]"
156   (if coding-system
157       (code-convert-string str *internal* coding-system)
158     ;;(code-convert-string str *internal* nil) returns nil instead of str.
159     str))
160
161 ;; XXX: Should we support optional NOCOPY argument? (only in Emacs 20.x)
162 (defun decode-coding-string (str coding-system)
163   "Decode the string STR which is encoded in CODING-SYSTEM.
164 \[EMACS 20 emulating function]"
165   (if coding-system
166       (let ((len (length str))
167             ret)
168         (while (and (< 0 len)
169                     (null (setq ret
170                                 (code-convert-string
171                                  (substring str 0 len)
172                                  coding-system *internal*))))
173           (setq len (1- len)))
174         (concat ret (substring str len)))
175     str))
176
177 (defalias 'detect-coding-region 'code-detect-region)
178
179 (defalias 'set-buffer-file-coding-system 'set-file-coding-system)
180
181
182 ;;; @ with code-conversion
183 ;;;
184
185 (defun insert-file-contents-as-coding-system
186   (coding-system filename &optional visit beg end replace)
187   "Like `insert-file-contents', q.v., but CODING-SYSTEM the first arg will
188 be applied to `file-coding-system-for-read'."
189   (let ((file-coding-system-for-read coding-system))
190     (insert-file-contents filename visit beg end replace)))
191
192 (cond
193  ((and (>= emacs-major-version 19) (>= emacs-minor-version 29))
194   ;; for MULE 2.3 based on Emacs 19.34.
195   (defun write-region-as-coding-system
196     (coding-system start end filename &optional append visit lockname)
197     "Like `write-region', q.v., but CODING-SYSTEM the first arg will be
198 applied to `file-coding-system'."
199     (let ((file-coding-system coding-system)
200           jka-compr-compression-info-list jam-zcat-filename-list)
201       (write-region start end filename append visit lockname)))
202
203   (defun find-file-noselect-as-coding-system
204     (coding-system filename &optional nowarn rawfile)
205     "Like `find-file-noselect', q.v., but CODING-SYSTEM the first arg will
206 be applied to `file-coding-system-for-read'."
207     (let ((file-coding-system-for-read coding-system))
208       (find-file-noselect filename nowarn rawfile)))
209   )
210  (t
211   ;; for MULE 2.3 based on Emacs 19.28 or MULE 1.*.
212   (defun write-region-as-coding-system
213     (coding-system start end filename &optional append visit lockname)
214     "Like `write-region', q.v., but CODING-SYSTEM the first arg will be
215 applied to `file-coding-system'."
216     (let ((file-coding-system coding-system)
217           jka-compr-compression-info-list jam-zcat-filename-list)
218       (write-region start end filename append visit)))
219
220   (defun find-file-noselect-as-coding-system
221     (coding-system filename &optional nowarn rawfile)
222     "Like `find-file-noselect', q.v., but CODING-SYSTEM the first arg will
223 be applied to `file-coding-system-for-read'."
224     (let ((file-coding-system-for-read coding-system))
225       (find-file-noselect filename nowarn)))
226   ))
227
228
229 ;;; @ without code-conversion
230 ;;;
231
232 (make-coding-system 'binary nil ?= "No conversion")
233
234 (defmacro as-binary-process (&rest body)
235   (` (let (selective-display    ; Disable ^M to nl translation.
236            ;; Mule
237            mc-flag
238            (default-process-coding-system (cons *noconv* *noconv*))
239            program-coding-system-alist)
240        (,@ body))))
241
242 (defmacro as-binary-input-file (&rest body)
243   (` (let (mc-flag
244            (file-coding-system-for-read *noconv*)
245            )
246        (,@ body))))
247
248 (defmacro as-binary-output-file (&rest body)
249   (` (let (mc-flag
250            (file-coding-system *noconv*)
251            )
252        (,@ body))))
253
254 (defalias 'set-process-input-coding-system 'set-process-coding-system)
255
256 (defun insert-binary-file-contents-literally (filename
257                                               &optional visit beg end replace)
258   "Like `insert-file-contents-literally', q.v., but don't code conversion.
259 A buffer may be modified in several ways after reading into the buffer due
260 to advanced Emacs features, such as file-name-handlers, format decoding,
261 find-file-hooks, etc.
262   This function ensures that none of these modifications will take place."
263   (as-binary-input-file
264    ;; Returns list absolute file name and length of data inserted.
265    (insert-file-contents-literally filename visit beg end replace)))
266
267 (defun insert-file-contents-as-binary (filename
268                                        &optional visit beg end replace)
269   "Like `insert-file-contents', q.v., but don't code and format conversion.
270 Like `insert-file-contents-literary', but it allows find-file-hooks,
271 automatic uncompression, etc.
272
273 Namely this function ensures that only format decoding and character
274 code conversion will not take place."
275   (as-binary-input-file
276    ;; Returns list absolute file name and length of data inserted.
277    (insert-file-contents filename visit beg end replace)))
278
279 (defun insert-file-contents-as-raw-text (filename
280                                          &optional visit beg end replace)
281   "Like `insert-file-contents', q.v., but don't code and format conversion.
282 Like `insert-file-contents-literary', but it allows find-file-hooks,
283 automatic uncompression, etc.
284 Like `insert-file-contents-as-binary', but it converts line-break
285 code."
286   ;; Returns list absolute file name and length of data inserted.
287   (insert-file-contents-as-coding-system 'raw-text
288                                          filename visit beg end replace))
289
290 (defun write-region-as-binary (start end filename
291                                      &optional append visit lockname)
292   "Like `write-region', q.v., but don't code conversion."
293   (write-region-as-coding-system 'binary
294                                  start end filename append visit lockname))
295
296 (defun write-region-as-raw-text-CRLF (start end filename
297                                             &optional append visit lockname)
298   "Like `write-region', q.v., but don't code conversion."
299   (write-region-as-coding-system 'raw-text-dos
300                                  start end filename append visit lockname))
301
302 (defun open-network-stream-as-binary (name buffer host service)
303   "Like `open-network-stream', q.v., but don't code conversion."
304   (let ((process (open-network-stream name buffer host service)))
305     (set-process-coding-system process *noconv* *noconv*)
306     process))
307
308 (defun find-file-noselect-as-binary (filename &optional nowarn rawfile)
309   "Like `find-file-noselect', q.v., but don't code and format conversion."
310   (find-file-noselect-as-coding-system 'binary filename nowarn rawfile))
311
312 (defun find-file-noselect-as-raw-text (filename &optional nowarn rawfile)
313   "Like `find-file-noselect', q.v., but it does not code and format
314 conversion except for line-break code."
315   (find-file-noselect-as-coding-system 'raw-text filename nowarn rawfile))
316
317
318 ;;; @ buffer representation
319 ;;;
320
321 (defsubst-maybe set-buffer-multibyte (flag)
322   "Set the multibyte flag of the current buffer to FLAG.
323 If FLAG is t, this makes the buffer a multibyte buffer.
324 If FLAG is nil, this makes the buffer a single-byte buffer.
325 The buffer contents remain unchanged as a sequence of bytes
326 but the contents viewed as characters do change.
327 \[Emacs 20.3 emulating function]"
328   (setq mc-flag flag)
329   )
330
331
332 ;;; @ character
333 ;;;
334
335 (defalias 'char-charset 'char-leading-char)
336
337 (defun split-char (character)
338   "Return list of charset and one or two position-codes of CHARACTER."
339   (let ((p (1- (char-bytes character)))
340         dest)
341     (while (>= p 1)
342       (setq dest (cons (- (char-component character p) 128) dest)
343             p (1- p)))
344     (cons (char-charset character) dest)))
345
346 (defmacro char-next-index (char index)
347   "Return index of character succeeding CHAR whose index is INDEX."
348   (` (+ (, index) (char-bytes (, char)))))
349
350 (if (subr-fboundp 'char-before)
351     (condition-case err
352         (char-before)
353       (error
354        (when (and (eq (car (get (car err) 'error-conditions))
355                       'wrong-number-of-arguments)
356                   (not (boundp 'si:char-before)))
357          (fset 'si:char-before (symbol-function 'char-before))
358          (defun char-before (&optional pos)
359            "Return character in current buffer preceding position POS.
360 POS is an integer or a buffer pointer.
361 If POS is out of range, the value is nil."
362            (si:char-before (or pos (point)))
363            )))))
364
365 (if (subr-fboundp 'char-after)
366     (condition-case err
367         (char-after)
368       (error
369        (when (and (eq (car (get (car err) 'error-conditions))
370                       'wrong-number-of-arguments)
371                   (not (boundp 'si:char-after)))
372          (fset 'si:char-after (symbol-function 'char-after))
373          (defun char-after (&optional pos)
374            "Return character in current buffer at position POS.
375 POS is an integer or a buffer pointer.
376 If POS is out of range, the value is nil."
377            (si:char-after (or pos (point)))
378            )))))
379
380 ;;; @@ obsoleted aliases
381 ;;;
382 ;;; You should not use them.
383
384 (defalias 'char-length 'char-bytes)
385 ;;(defalias 'char-columns 'char-width)
386
387
388 ;;; @ string
389 ;;;
390
391 (defalias 'string-columns 'string-width)
392
393 (defalias 'string-to-int-list 'string-to-char-list)
394
395 ;; Imported from Mule-2.3
396 (defun-maybe truncate-string (str width &optional start-column)
397   "\
398 Truncate STR to fit in WIDTH columns.
399 Optional non-nil arg START-COLUMN specifies the starting column.
400 \[emu-mule.el; Mule 2.3 emulating function]"
401   (or start-column
402       (setq start-column 0))
403   (let ((max-width (string-width str))
404         (len (length str))
405         (from 0)
406         (column 0)
407         to-prev to ch)
408     (if (>= width max-width)
409         (setq width max-width))
410     (if (>= start-column width)
411         ""
412       (while (< column start-column)
413         (setq ch (aref str from)
414               column (+ column (char-width ch))
415               from (+ from (char-bytes ch))))
416       (if (< width max-width)
417           (progn
418             (setq to from)
419             (while (<= column width)
420               (setq ch (aref str to)
421                     column (+ column (char-width ch))
422                     to-prev to
423                     to (+ to (char-bytes ch))))
424             (setq to to-prev)))
425       (substring str from to))))
426
427 (defalias 'looking-at-as-unibyte 'looking-at)
428
429
430 ;;; @ end
431 ;;;
432
433 (provide 'poem-om)
434
435 ;;; poem-om.el ends here