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