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