Header 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-maybe find-coding-system (obj)
114   "Return OBJ if it is a coding-system."
115   (if (coding-system-p obj)
116       obj))
117
118 (defun encode-coding-region (start end coding-system)
119   "Encode the text between START and END to CODING-SYSTEM.
120 \[EMACS 20 emulating function]"
121   ;; If `coding-system' is nil, do nothing.
122   (code-convert-region start end *internal* coding-system))
123
124 (defun decode-coding-region (start end coding-system)
125   "Decode the text between START and END which is encoded in CODING-SYSTEM.
126 \[EMACS 20 emulating function]"
127   ;; If `coding-system' is nil, do nothing.
128   (code-convert-region start end coding-system *internal*))
129
130 ;; XXX: Should we support optional NOCOPY argument? (only in Emacs 20.x)
131 (defun encode-coding-string (str coding-system)
132   "Encode the STRING to CODING-SYSTEM.
133 \[EMACS 20 emulating function]"
134   (if coding-system
135       (code-convert-string str *internal* coding-system)
136     ;;(code-convert-string str *internal* nil) returns nil instead of str.
137     str))
138
139 ;; XXX: Should we support optional NOCOPY argument? (only in Emacs 20.x)
140 (defun decode-coding-string (str coding-system)
141   "Decode the string STR which is encoded in CODING-SYSTEM.
142 \[EMACS 20 emulating function]"
143   (if coding-system
144       (let ((len (length str))
145             ret)
146         (while (and (< 0 len)
147                     (null (setq ret
148                                 (code-convert-string
149                                  (substring str 0 len)
150                                  coding-system *internal*))))
151           (setq len (1- len)))
152         (concat ret (substring str len)))
153     str))
154
155 (defalias 'detect-coding-region 'code-detect-region)
156
157 (defalias 'set-buffer-file-coding-system 'set-file-coding-system)
158
159
160 ;;; @ with code-conversion
161 ;;;
162
163 (defun insert-file-contents-as-coding-system
164   (coding-system filename &optional visit beg end replace)
165   "Like `insert-file-contents', q.v., but CODING-SYSTEM the first arg will
166 be applied to `file-coding-system-for-read'."
167   (let ((file-coding-system-for-read coding-system))
168     (insert-file-contents filename visit beg end replace)))
169
170 (cond
171  ((and (>= emacs-major-version 19) (>= emacs-minor-version 29))
172   ;; for MULE 2.3 based on Emacs 19.34.
173   (defun write-region-as-coding-system
174     (coding-system start end filename &optional append visit lockname)
175     "Like `write-region', q.v., but CODING-SYSTEM the first arg will be
176 applied to `file-coding-system'."
177     (let ((file-coding-system coding-system)
178           jka-compr-compression-info-list jam-zcat-filename-list)
179       (write-region start end filename append visit lockname)))
180
181   (defun find-file-noselect-as-coding-system
182     (coding-system filename &optional nowarn rawfile)
183     "Like `find-file-noselect', q.v., but CODING-SYSTEM the first arg will
184 be applied to `file-coding-system-for-read'."
185     (let ((file-coding-system-for-read coding-system))
186       (find-file-noselect filename nowarn rawfile)))
187   )
188  (t
189   ;; for MULE 2.3 based on Emacs 19.28 or MULE 1.*.
190   (defun write-region-as-coding-system
191     (coding-system start end filename &optional append visit lockname)
192     "Like `write-region', q.v., but CODING-SYSTEM the first arg will be
193 applied to `file-coding-system'."
194     (let ((file-coding-system coding-system)
195           jka-compr-compression-info-list jam-zcat-filename-list)
196       (write-region start end filename append visit)))
197
198   (defun find-file-noselect-as-coding-system
199     (coding-system filename &optional nowarn rawfile)
200     "Like `find-file-noselect', q.v., but CODING-SYSTEM the first arg will
201 be applied to `file-coding-system-for-read'."
202     (let ((file-coding-system-for-read coding-system))
203       (find-file-noselect filename nowarn)))
204   ))
205
206 (defun save-buffer-as-coding-system (coding-system &optional args)
207   "Like `save-buffer', q.v., but CODING-SYSTEM the first arg will be
208 applied to `coding-system-for-write'."
209   (let ((file-coding-system coding-system))
210     (save-buffer args)))
211
212
213 ;;; @ without code-conversion
214 ;;;
215
216 (make-coding-system 'binary nil ?= "No conversion")
217
218 (defmacro as-binary-process (&rest body)
219   (` (let (selective-display    ; Disable ^M to nl translation.
220            ;; Mule
221            mc-flag
222            (default-process-coding-system (cons *noconv* *noconv*))
223            program-coding-system-alist)
224        (,@ body))))
225
226 (defmacro as-binary-input-file (&rest body)
227   (` (let (mc-flag
228            (file-coding-system-for-read *noconv*)
229            )
230        (,@ body))))
231
232 (defmacro as-binary-output-file (&rest body)
233   (` (let (mc-flag
234            (file-coding-system *noconv*)
235            )
236        (,@ body))))
237
238 (defalias 'set-process-input-coding-system 'set-process-coding-system)
239
240 (defun insert-file-contents-as-binary (filename
241                                        &optional visit beg end replace)
242   "Like `insert-file-contents', q.v., but don't code and format conversion.
243 Like `insert-file-contents-literary', but it allows find-file-hooks,
244 automatic uncompression, etc.
245
246 Namely this function ensures that only format decoding and character
247 code conversion will not take place."
248   (as-binary-input-file
249    ;; Returns list absolute file name and length of data inserted.
250    (insert-file-contents filename visit beg end replace)))
251
252 (defun insert-file-contents-as-raw-text (filename
253                                          &optional visit beg end replace)
254   "Like `insert-file-contents', q.v., but don't code and format conversion.
255 Like `insert-file-contents-literary', but it allows find-file-hooks,
256 automatic uncompression, etc.
257 Like `insert-file-contents-as-binary', but it converts line-break
258 code."
259   ;; Returns list absolute file name and length of data inserted.
260   (insert-file-contents-as-coding-system 'raw-text
261                                          filename visit beg end replace))
262
263 (defalias 'insert-file-contents-as-raw-text-CRLF
264   'insert-file-contents-as-raw-text)
265
266 (defun write-region-as-binary (start end filename
267                                      &optional append visit lockname)
268   "Like `write-region', q.v., but don't code conversion."
269   (write-region-as-coding-system 'binary
270                                  start end filename append visit lockname))
271
272 (defun write-region-as-raw-text-CRLF (start end filename
273                                             &optional append visit lockname)
274   "Like `write-region', q.v., but don't code conversion."
275   (write-region-as-coding-system 'raw-text-dos
276                                  start end filename append visit lockname))
277
278 (defun find-file-noselect-as-binary (filename &optional nowarn rawfile)
279   "Like `find-file-noselect', q.v., but don't code and format conversion."
280   (find-file-noselect-as-coding-system 'binary filename nowarn rawfile))
281
282 (defun find-file-noselect-as-raw-text (filename &optional nowarn rawfile)
283   "Like `find-file-noselect', q.v., but it does not code and format
284 conversion except for line-break code."
285   (find-file-noselect-as-coding-system 'raw-text filename nowarn rawfile))
286
287 (defalias 'find-file-noselect-as-raw-text-CRLF
288   'find-file-noselect-as-raw-text)
289
290 (defun save-buffer-as-binary (&optional args)
291   "Like `save-buffer', q.v., but don't encode."
292   (let ((file-coding-system 'binary))
293     (save-buffer args)))
294
295 (defun save-buffer-as-raw-text-CRLF (&optional args)
296   "Like `save-buffer', q.v., but save as network representation."
297   (let ((file-coding-system 'raw-text-dos))
298     (save-buffer args)))
299
300 (defun open-network-stream-as-binary (name buffer host service)
301   "Like `open-network-stream', q.v., but don't code conversion."
302   (let ((process (open-network-stream name buffer host service)))
303     (set-process-coding-system process *noconv* *noconv*)
304     process))
305
306
307 ;;; @ end
308 ;;;
309
310 (require 'product)
311 (product-provide (provide 'pces-om) (require 'apel-ver))
312
313 ;;; pces-om.el ends here