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