fix.
[elisp/apel.git] / pces-om.el
1 ;;; pces-om.el --- pces implementation for Mule 1.* and Mule 2.*
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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        (define-ccl-program poem-ccl-decode-raw-text
36          '(1
37            ((r2 = 0)
38             (read r0)
39             (loop
40               (if (r0 == ?\x0d)
41                   ((r2 = 1)
42                    (read-if (r1 == ?\x0a)
43                             ((r0 = ?\x0a)
44                              (r2 = 0)
45                              (write-read-repeat r0))
46                             ((write r0)
47                              (r0 = (r1 + 0))
48                              (repeat))))
49                 ((r2 = 0)
50                  (write-read-repeat r0)))))
51            ;; This EOF BLOCK won't work out in practice. So the last datum
52            ;; might be lost if it's value is ?\x0d.
53            (if r2
54                (write r0))
55            )
56          "Convert line-break code from CRLF to LF.")
57
58        (define-ccl-program poem-ccl-encode-raw-text
59          '(1
60            ((read r0)
61             (loop (write-read-repeat r0))))
62          "Pass through without any conversions.")
63
64        (define-ccl-program poem-ccl-encode-raw-text-CRLF
65          '(2
66            ((loop
67               (read-if (r0 == ?\x0a)
68                        (write "\x0d\x0a")
69                        (write r0))
70               (repeat))))
71          "Convert line-break code from LF to CRLF.")
72
73        (make-coding-system
74         'raw-text 4 ?=
75         "No conversion"
76         nil
77         (cons poem-ccl-decode-raw-text poem-ccl-encode-raw-text))
78
79        (make-coding-system
80         'raw-text-dos 4 ?=
81         "No conversion"
82         nil
83         (cons poem-ccl-decode-raw-text poem-ccl-encode-raw-text-CRLF))
84        )
85       (t
86        (defun poem-decode-raw-text (from to)
87          (save-restriction
88            (narrow-to-region from to)
89            (goto-char (point-min))
90            (while (re-search-forward "\r$" nil t)
91              (replace-match "")
92              )))
93        (defun poem-encode-raw-text-CRLF (from to)
94          (save-restriction
95            (narrow-to-region from to)
96            (goto-char (point-min))
97            (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
98              (replace-match "\\1\r\n")
99              )))
100
101        (make-coding-system 'raw-text nil ?= "No conversion")
102        (put 'raw-text 'post-read-conversion 'poem-decode-raw-text)
103        
104        (make-coding-system 'raw-text-dos nil ?= "No conversion")
105        (put 'raw-text-dos 'post-read-conversion 'poem-decode-raw-text)
106        (put 'raw-text-dos 'pre-write-conversion 'poem-encode-raw-text-CRLF)
107        ))
108
109
110 ;;; @ coding system
111 ;;;
112
113 (defun encode-coding-region (start end coding-system)
114   "Encode the text between START and END to CODING-SYSTEM.
115 \[EMACS 20 emulating function]"
116   ;; If `coding-system' is nil, do nothing.
117   (code-convert-region start end *internal* coding-system))
118
119 (defun decode-coding-region (start end coding-system)
120   "Decode the text between START and END which is encoded in CODING-SYSTEM.
121 \[EMACS 20 emulating function]"
122   ;; If `coding-system' is nil, do nothing.
123   (code-convert-region start end coding-system *internal*))
124
125 ;; XXX: Should we support optional NOCOPY argument? (only in Emacs 20.x)
126 (defun encode-coding-string (str coding-system)
127   "Encode the STRING to CODING-SYSTEM.
128 \[EMACS 20 emulating function]"
129   (if coding-system
130       (code-convert-string str *internal* coding-system)
131     ;;(code-convert-string str *internal* nil) returns nil instead of str.
132     str))
133
134 ;; XXX: Should we support optional NOCOPY argument? (only in Emacs 20.x)
135 (defun decode-coding-string (str coding-system)
136   "Decode the string STR which is encoded in CODING-SYSTEM.
137 \[EMACS 20 emulating function]"
138   (if coding-system
139       (let ((len (length str))
140             ret)
141         (while (and (< 0 len)
142                     (null (setq ret
143                                 (code-convert-string
144                                  (substring str 0 len)
145                                  coding-system *internal*))))
146           (setq len (1- len)))
147         (concat ret (substring str len)))
148     str))
149
150 (defalias 'detect-coding-region 'code-detect-region)
151
152 (defalias 'set-buffer-file-coding-system 'set-file-coding-system)
153
154
155 ;;; @ with code-conversion
156 ;;;
157
158 (defun insert-file-contents-as-coding-system
159   (coding-system filename &optional visit beg end replace)
160   "Like `insert-file-contents', q.v., but CODING-SYSTEM the first arg will
161 be applied to `file-coding-system-for-read'."
162   (let ((file-coding-system-for-read coding-system))
163     (insert-file-contents filename visit beg end replace)))
164
165 (cond
166  ((and (>= emacs-major-version 19) (>= emacs-minor-version 29))
167   ;; for MULE 2.3 based on Emacs 19.34.
168   (defun write-region-as-coding-system
169     (coding-system start end filename &optional append visit lockname)
170     "Like `write-region', q.v., but CODING-SYSTEM the first arg will be
171 applied to `file-coding-system'."
172     (let ((file-coding-system coding-system)
173           jka-compr-compression-info-list jam-zcat-filename-list)
174       (write-region start end filename append visit lockname)))
175
176   (defun find-file-noselect-as-coding-system
177     (coding-system filename &optional nowarn rawfile)
178     "Like `find-file-noselect', q.v., but CODING-SYSTEM the first arg will
179 be applied to `file-coding-system-for-read'."
180     (let ((file-coding-system-for-read coding-system))
181       (find-file-noselect filename nowarn rawfile)))
182   )
183  (t
184   ;; for MULE 2.3 based on Emacs 19.28 or MULE 1.*.
185   (defun write-region-as-coding-system
186     (coding-system start end filename &optional append visit lockname)
187     "Like `write-region', q.v., but CODING-SYSTEM the first arg will be
188 applied to `file-coding-system'."
189     (let ((file-coding-system coding-system)
190           jka-compr-compression-info-list jam-zcat-filename-list)
191       (write-region start end filename append visit)))
192
193   (defun find-file-noselect-as-coding-system
194     (coding-system filename &optional nowarn rawfile)
195     "Like `find-file-noselect', q.v., but CODING-SYSTEM the first arg will
196 be applied to `file-coding-system-for-read'."
197     (let ((file-coding-system-for-read coding-system))
198       (find-file-noselect filename nowarn)))
199   ))
200
201 (defun save-buffer-as-coding-system (coding-system &optional args)
202   "Like `save-buffer', q.v., but CODING-SYSTEM the first arg will be
203 applied to `coding-system-for-write'."
204   (let ((file-coding-system coding-system))
205     (save-buffer args)))
206
207
208 ;;; @ without code-conversion
209 ;;;
210
211 (make-coding-system 'binary nil ?= "No conversion")
212
213 (defmacro as-binary-process (&rest body)
214   (` (let (selective-display    ; Disable ^M to nl translation.
215            ;; Mule
216            mc-flag
217            (default-process-coding-system (cons *noconv* *noconv*))
218            program-coding-system-alist)
219        (,@ body))))
220
221 (defmacro as-binary-input-file (&rest body)
222   (` (let (mc-flag
223            (file-coding-system-for-read *noconv*)
224            )
225        (,@ body))))
226
227 (defmacro as-binary-output-file (&rest body)
228   (` (let (mc-flag
229            (file-coding-system *noconv*)
230            )
231        (,@ body))))
232
233 (defalias 'set-process-input-coding-system 'set-process-coding-system)
234
235 (defun insert-file-contents-as-binary (filename
236                                        &optional visit beg end replace)
237   "Like `insert-file-contents', q.v., but don't code and format conversion.
238 Like `insert-file-contents-literary', but it allows find-file-hooks,
239 automatic uncompression, etc.
240
241 Namely this function ensures that only format decoding and character
242 code conversion will not take place."
243   (as-binary-input-file
244    ;; Returns list absolute file name and length of data inserted.
245    (insert-file-contents filename visit beg end replace)))
246
247 (defun insert-file-contents-as-raw-text (filename
248                                          &optional visit beg end replace)
249   "Like `insert-file-contents', q.v., but don't code and format conversion.
250 Like `insert-file-contents-literary', but it allows find-file-hooks,
251 automatic uncompression, etc.
252 Like `insert-file-contents-as-binary', but it converts line-break
253 code."
254   ;; Returns list absolute file name and length of data inserted.
255   (insert-file-contents-as-coding-system 'raw-text
256                                          filename visit beg end replace))
257
258 (defalias 'insert-file-contents-as-raw-text-CRLF
259   'insert-file-contents-as-raw-text)
260
261 (defun write-region-as-binary (start end filename
262                                      &optional append visit lockname)
263   "Like `write-region', q.v., but don't code conversion."
264   (write-region-as-coding-system 'binary
265                                  start end filename append visit lockname))
266
267 (defun write-region-as-raw-text-CRLF (start end filename
268                                             &optional append visit lockname)
269   "Like `write-region', q.v., but don't code conversion."
270   (write-region-as-coding-system 'raw-text-dos
271                                  start end filename append visit lockname))
272
273 (defun find-file-noselect-as-binary (filename &optional nowarn rawfile)
274   "Like `find-file-noselect', q.v., but don't code and format conversion."
275   (find-file-noselect-as-coding-system 'binary filename nowarn rawfile))
276
277 (defun find-file-noselect-as-raw-text (filename &optional nowarn rawfile)
278   "Like `find-file-noselect', q.v., but it does not code and format
279 conversion except for line-break code."
280   (find-file-noselect-as-coding-system 'raw-text filename nowarn rawfile))
281
282 (defalias 'find-file-noselect-as-raw-text-CRLF
283   'find-file-noselect-as-raw-text)
284
285 (defun save-buffer-as-binary (&optional args)
286   "Like `save-buffer', q.v., but don't encode."
287   (let ((file-coding-system 'binary))
288     (save-buffer args)))
289
290 (defun save-buffer-as-raw-text-CRLF (&optional args)
291   "Like `save-buffer', q.v., but save as network representation."
292   (let ((file-coding-system 'raw-text-dos))
293     (save-buffer args)))
294
295 (defun open-network-stream-as-binary (name buffer host service)
296   "Like `open-network-stream', q.v., but don't code conversion."
297   (let ((process (open-network-stream name buffer host service)))
298     (set-process-coding-system process *noconv* *noconv*)
299     process))
300
301
302 ;;; @ end
303 ;;;
304
305 (provide 'pces-om)
306
307 ;;; pces-om.el ends here