*** empty log message ***
[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 ;;; @ with code-conversion
170 ;;;
171
172 (defun insert-file-contents-as-coding-system
173   (coding-system filename &optional visit beg end replace)
174   "Like `insert-file-contents', q.v., but CODING-SYSTEM the first arg will
175 be applied to `file-coding-system-for-read'."
176   (let ((file-coding-system-for-read coding-system))
177     (insert-file-contents filename visit beg end replace)))
178
179 (cond
180  ((and (>= emacs-major-version 19) (>= emacs-minor-version 29))
181   ;; for MULE 2.3 based on Emacs 19.34.
182   (defun write-region-as-coding-system
183     (coding-system start end filename &optional append visit lockname)
184     "Like `write-region', q.v., but CODING-SYSTEM the first arg will be
185 applied to `file-coding-system'."
186     (let ((file-coding-system coding-system)
187           jka-compr-compression-info-list jam-zcat-filename-list)
188       (write-region start end filename append visit lockname)))
189
190   (defun find-file-noselect-as-coding-system
191     (coding-system filename &optional nowarn rawfile)
192     "Like `find-file-noselect', q.v., but CODING-SYSTEM the first arg will
193 be applied to `file-coding-system-for-read'."
194     (let ((file-coding-system-for-read coding-system))
195       (find-file-noselect filename nowarn rawfile)))
196   )
197  (t
198   ;; for MULE 2.3 based on Emacs 19.28 or MULE 1.*.
199   (defun write-region-as-coding-system
200     (coding-system start end filename &optional append visit lockname)
201     "Like `write-region', q.v., but CODING-SYSTEM the first arg will be
202 applied to `file-coding-system'."
203     (let ((file-coding-system coding-system)
204           jka-compr-compression-info-list jam-zcat-filename-list)
205       (write-region start end filename append visit)))
206
207   (defun find-file-noselect-as-coding-system
208     (coding-system filename &optional nowarn rawfile)
209     "Like `find-file-noselect', q.v., but CODING-SYSTEM the first arg will
210 be applied to `file-coding-system-for-read'."
211     (let ((file-coding-system-for-read coding-system))
212       (find-file-noselect filename nowarn)))
213   ))
214
215
216 ;;; @ without code-conversion
217 ;;;
218
219 (defmacro as-binary-process (&rest body)
220   (` (let (selective-display    ; Disable ^M to nl translation.
221            ;; Mule
222            mc-flag
223            (default-process-coding-system (cons *noconv* *noconv*))
224            program-coding-system-alist)
225        (,@ body))))
226
227 (defmacro as-binary-input-file (&rest body)
228   (` (let (mc-flag
229            (file-coding-system-for-read *noconv*)
230            )
231        (,@ body))))
232
233 (defmacro as-binary-output-file (&rest body)
234   (` (let (mc-flag
235            (file-coding-system *noconv*)
236            )
237        (,@ body))))
238
239 (defalias 'set-process-input-coding-system 'set-process-coding-system)
240
241 (defun insert-binary-file-contents-literally (filename
242                                               &optional visit beg end replace)
243   "Like `insert-file-contents-literally', q.v., but don't code conversion.
244 A buffer may be modified in several ways after reading into the buffer due
245 to advanced Emacs features, such as file-name-handlers, format decoding,
246 find-file-hooks, etc.
247   This function ensures that none of these modifications will take place."
248   (as-binary-input-file
249    ;; Returns list absolute file name and length of data inserted.
250    (insert-file-contents-literally filename visit beg end replace)))
251
252 (cond
253  ((>= emacs-major-version 19)
254   ;; for MULE 2.*.
255   (defun insert-file-contents-as-binary (filename
256                                          &optional visit beg end replace)
257     "Like `insert-file-contents', q.v., but don't code and format conversion.
258 Like `insert-file-contents-literary', but it allows find-file-hooks,
259 automatic uncompression, etc.
260
261 Namely this function ensures that only format decoding and character
262 code conversion will not take place."
263     ;; Returns list absolute file name and length of data inserted.
264     (insert-file-contents-as-coding-system 'binary
265                                            filename visit beg end replace))
266
267   (defun insert-file-contents-as-raw-text (filename
268                                            &optional visit beg end replace)
269     "Like `insert-file-contents', q.v., but don't code and format conversion.
270 Like `insert-file-contents-literary', but it allows find-file-hooks,
271 automatic uncompression, etc.
272 Like `insert-file-contents-as-binary', but it converts line-break
273 code."
274     ;; Returns list absolute file name and length of data inserted.
275     (insert-file-contents-as-coding-system 'raw-text
276                                            filename visit beg end replace))
277
278   (defun write-region-as-binary (start end filename
279                                        &optional append visit lockname)
280     "Like `write-region', q.v., but don't code conversion."
281     (write-region-as-coding-system 'binary
282                                    start end filename append visit lockname))
283
284   (defun write-region-as-raw-text-CRLF (start end filename
285                                               &optional append visit lockname)
286     "Like `write-region', q.v., but don't code conversion."
287     (write-region-as-coding-system 'raw-text-dos
288                                    start end filename append visit lockname))
289
290   (defun find-file-noselect-as-binary (filename &optional nowarn rawfile)
291     "Like `find-file-noselect', q.v., but don't code and format conversion."
292     (find-file-noselect-as-coding-system 'binary
293                                          filename nowarn rawfile))
294
295   (defun find-file-noselect-as-raw-text (filename &optional nowarn rawfile)
296     "Like `find-file-noselect', q.v., but it does not code and format
297 conversion except for line-break code."
298     (find-file-noselect-as-coding-system 'raw-text
299                                          filename nowarn rawfile))
300   )
301  (t
302   ;; for MULE 1.*.
303   (defun insert-file-contents-as-binary (filename
304                                          &optional visit beg end replace)
305     "Like `insert-file-contents', q.v., but don't code and format conversion.
306 Like `insert-file-contents-literary', but it allows find-file-hooks,
307 automatic uncompression, etc.
308
309 Namely this function ensures that only format decoding and character
310 code conversion will not take place."
311     (as-binary-input-file
312      ;; Returns list absolute file name and length of data inserted.
313      (insert-file-contents filename visit beg end replace)))
314
315   (defun insert-file-contents-as-raw-text (filename
316                                            &optional visit beg end replace)
317     "Like `insert-file-contents', q.v., but don't code and format conversion.
318 Like `insert-file-contents-literary', but it allows find-file-hooks,
319 automatic uncompression, etc.
320 Like `insert-file-contents-as-binary', but it converts line-break
321 code."
322     (save-excursion
323       (save-restriction
324         (narrow-to-region (point)(point))
325         (let ((return-val
326                ;; Returns list absolute file name and length of data inserted.
327                (insert-file-contents-as-binary filename
328                                                visit beg end replace)))
329           (goto-char (point-min))
330           (while (re-search-forward "\r$" nil t)
331             (replace-match ""))
332           (list (car return-val) (buffer-size))))))
333
334   (defun write-region-as-binary (start end filename
335                                        &optional append visit lockname)
336     "Like `write-region', q.v., but don't code conversion."
337     (as-binary-output-file
338      (write-region start end filename append visit)))
339
340   (defun write-region-as-raw-text-CRLF (start end filename
341                                               &optional append visit lockname)
342     "Like `write-region', q.v., but don't code conversion."
343     (let ((the-buf (current-buffer)))
344       (with-temp-buffer
345         (insert-buffer-substring the-buf start end)
346         (goto-char (point-min))
347         (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
348           (replace-match "\\1\r\n"))
349         (write-region-as-binary (point-min)(point-max)
350                                 filename append visit))))
351
352   (defun find-file-noselect-as-binary (filename &optional nowarn rawfile)
353     "Like `find-file-noselect', q.v., but don't code and format conversion."
354     (as-binary-input-file (find-file-noselect filename nowarn)))
355
356   (defun find-file-noselect-as-raw-text (filename &optional nowarn rawfile)
357     "Like `find-file-noselect', q.v., but it does not code and format
358 conversion except for line-break code."
359     (save-current-buffer
360       (prog1
361           (set-buffer (find-file-noselect-as-binary filename nowarn rawfile))
362         (let ((flag (buffer-modified-p)))
363           (save-excursion
364             (goto-char (point-min))
365             (while (re-search-forward "\r$" nil t)
366               (replace-match "")))
367           (set-buffer-modified-p flag)))))
368   ))
369
370 (defun open-network-stream-as-binary (name buffer host service)
371   "Like `open-network-stream', q.v., but don't code conversion."
372   (let ((process (open-network-stream name buffer host service)))
373     (set-process-coding-system process *noconv* *noconv*)
374     process))
375
376
377 ;;; @ buffer representation
378 ;;;
379
380 (defsubst-maybe set-buffer-multibyte (flag)
381   "Set the multibyte flag of the current buffer to FLAG.
382 If FLAG is t, this makes the buffer a multibyte buffer.
383 If FLAG is nil, this makes the buffer a single-byte buffer.
384 The buffer contents remain unchanged as a sequence of bytes
385 but the contents viewed as characters do change.
386 \[Emacs 20.3 emulating function]"
387   (setq mc-flag flag)
388   )
389
390
391 ;;; @ character
392 ;;;
393
394 (defalias 'char-charset 'char-leading-char)
395
396 (defun split-char (character)
397   "Return list of charset and one or two position-codes of CHARACTER."
398   (let ((p (1- (char-bytes character)))
399         dest)
400     (while (>= p 1)
401       (setq dest (cons (- (char-component character p) 128) dest)
402             p (1- p)))
403     (cons (char-charset character) dest)))
404
405 (defmacro char-next-index (char index)
406   "Return index of character succeeding CHAR whose index is INDEX."
407   (` (+ (, index) (char-bytes (, char)))))
408
409 (if (subr-fboundp 'char-before)
410     (condition-case err
411         (char-before)
412       (error
413        (when (and (eq (car (get (car err) 'error-conditions))
414                       'wrong-number-of-arguments)
415                   (not (boundp 'si:char-before)))
416          (fset 'si:char-before (symbol-function 'char-before))
417          (defun char-before (&optional pos)
418            "Return character in current buffer preceding position POS.
419 POS is an integer or a buffer pointer.
420 If POS is out of range, the value is nil."
421            (si:char-before (or pos (point)))
422            )))))
423
424 (if (subr-fboundp 'char-after)
425     (condition-case err
426         (char-after)
427       (error
428        (when (and (eq (car (get (car err) 'error-conditions))
429                       'wrong-number-of-arguments)
430                   (not (boundp 'si:char-after)))
431          (fset 'si:char-after (symbol-function 'char-after))
432          (defun char-after (&optional pos)
433            "Return character in current buffer at position POS.
434 POS is an integer or a buffer pointer.
435 If POS is out of range, the value is nil."
436            (si:char-after (or pos (point)))
437            )))))
438
439 ;;; @@ obsoleted aliases
440 ;;;
441 ;;; You should not use them.
442
443 (defalias 'char-length 'char-bytes)
444 ;;(defalias 'char-columns 'char-width)
445
446
447 ;;; @ string
448 ;;;
449
450 (defalias 'string-columns 'string-width)
451
452 (defalias 'string-to-int-list 'string-to-char-list)
453
454 ;; Imported from Mule-2.3
455 (defun-maybe truncate-string (str width &optional start-column)
456   "\
457 Truncate STR to fit in WIDTH columns.
458 Optional non-nil arg START-COLUMN specifies the starting column.
459 \[emu-mule.el; Mule 2.3 emulating function]"
460   (or start-column
461       (setq start-column 0))
462   (let ((max-width (string-width str))
463         (len (length str))
464         (from 0)
465         (column 0)
466         to-prev to ch)
467     (if (>= width max-width)
468         (setq width max-width))
469     (if (>= start-column width)
470         ""
471       (while (< column start-column)
472         (setq ch (aref str from)
473               column (+ column (char-width ch))
474               from (+ from (char-bytes ch))))
475       (if (< width max-width)
476           (progn
477             (setq to from)
478             (while (<= column width)
479               (setq ch (aref str to)
480                     column (+ column (char-width ch))
481                     to-prev to
482                     to (+ to (char-bytes ch))))
483             (setq to to-prev)))
484       (substring str from to))))
485
486 (defalias 'looking-at-as-unibyte 'looking-at)
487
488
489 ;;; @ end
490 ;;;
491
492 (provide 'poem-om)
493
494 ;;; poem-om.el ends here