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