* pccl-om.el (toplevel): Don't require `poem'.
[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
52
53 ;;; @ character set
54 ;;;
55
56 (defalias 'make-char 'make-character)
57
58 (defalias 'find-non-ascii-charset-string 'find-charset-string)
59 (defalias 'find-non-ascii-charset-region 'find-charset-region)
60
61 (defalias 'charset-bytes        'char-bytes)
62 (defalias 'charset-description  'char-description)
63 (defalias 'charset-registry     'char-registry)
64 (defalias 'charset-columns      'char-width)
65 (defalias 'charset-direction    'char-direction)
66
67 (defun charset-chars (charset)
68   "Return the number of characters per dimension of CHARSET."
69   (if (= (logand (nth 2 (character-set charset)) 1) 1)
70       96
71     94))
72
73
74 ;;; @ coding system
75 ;;;
76
77 (defun encode-coding-region (start end coding-system)
78   "Encode the text between START and END to CODING-SYSTEM.
79 \[EMACS 20 emulating function]"
80   ;; If `coding-system' is nil, do nothing.
81   (code-convert-region start end *internal* coding-system))
82
83 (defun decode-coding-region (start end coding-system)
84   "Decode the text between START and END which is encoded in CODING-SYSTEM.
85 \[EMACS 20 emulating function]"
86   ;; If `coding-system' is nil, do nothing.
87   (code-convert-region start end coding-system *internal*))
88
89 ;; XXX: Should we support optional NOCOPY argument? (only in Emacs 20.x)
90 (defun encode-coding-string (str coding-system)
91   "Encode the STRING to CODING-SYSTEM.
92 \[EMACS 20 emulating function]"
93   (if coding-system
94       (code-convert-string str *internal* coding-system)
95     ;;(code-convert-string str *internal* nil) returns nil instead of str.
96     str))
97
98 ;; XXX: Should we support optional NOCOPY argument? (only in Emacs 20.x)
99 (defun decode-coding-string (str coding-system)
100   "Decode the string STR which is encoded in CODING-SYSTEM.
101 \[EMACS 20 emulating function]"
102   (if coding-system
103       (let ((len (length str))
104             ret)
105         (while (and (< 0 len)
106                     (null (setq ret
107                                 (code-convert-string
108                                  (substring str 0 len)
109                                  coding-system *internal*))))
110           (setq len (1- len)))
111         (concat ret (substring str len)))
112     str))
113
114 (defalias 'detect-coding-region 'code-detect-region)
115
116 (defalias 'set-buffer-file-coding-system 'set-file-coding-system)
117
118 (when (and (boundp 'MULE) running-emacs-19)
119   (define-ccl-program ccl-decode-raw-text
120     '(1
121       ((read r1 r0)
122        (loop
123          (r2 = (r1 == ?\x0d))
124          (r2 &= (r0 == ?\x0a))
125          (if r2
126              ((write ?\x0a)
127               (read r1 r0)
128               (repeat))
129            ((write r1)
130             (r1 = (r0 + 0))
131             (read r0)
132             (repeat)
133             ))))
134       (write r1))
135     "Convert line-break code from CRLF to LF.")
136
137   (define-ccl-program ccl-encode-raw-text
138     '(1
139       ((read r0)
140        (loop (write-read-repeat r0))))
141     "Pass through without any conversions.")
142
143   (define-ccl-program ccl-encode-raw-text-CRLF
144     '(2
145       ((loop
146          (read r0)
147          (if (r0 == ?\x0a)
148              (write "\x0d\x0a")
149            (write r0))
150          (repeat))))
151     "Convert line-break code from LF to CRLF.")
152
153   (make-coding-system
154    'raw-text 4 ?=
155    "No conversion"
156    nil
157    (cons ccl-decode-raw-text ccl-encode-raw-text))
158
159   (make-coding-system
160    'raw-text-dos 4 ?=
161    "No conversion"
162    nil
163    (cons ccl-decode-raw-text ccl-encode-raw-text-CRLF))
164
165   (make-coding-system
166    'binary nil ?=
167    "No conversion")
168   )
169
170 ;;; @ without code-conversion
171 ;;;
172
173 (defmacro as-binary-process (&rest body)
174   (` (let (selective-display    ; Disable ^M to nl translation.
175            ;; Mule
176            mc-flag      
177            (default-process-coding-system (cons *noconv* *noconv*))
178            program-coding-system-alist)
179        (,@ body))))
180
181 (defmacro as-binary-input-file (&rest body)
182   (` (let (mc-flag
183            (file-coding-system-for-read *noconv*)
184            )
185        (,@ body))))
186
187 (defmacro as-binary-output-file (&rest body)
188   (` (let (mc-flag
189            (file-coding-system *noconv*)
190            )
191        (,@ body))))
192
193 (defalias 'set-process-input-coding-system 'set-process-coding-system)
194
195 (defun insert-file-contents-as-binary (filename
196                                        &optional visit beg end replace)
197   "Like `insert-file-contents', q.v., but don't code and format conversion.
198 Like `insert-file-contents-literary', but it allows find-file-hooks,
199 automatic uncompression, etc.
200
201 Namely this function ensures that only format decoding and character
202 code conversion will not take place."
203   (as-binary-input-file
204    ;; Returns list absolute file name and length of data inserted.
205    (insert-file-contents filename visit beg end replace)))
206
207 (defun insert-file-contents-as-raw-text (filename
208                                          &optional visit beg end replace)
209   "Like `insert-file-contents', q.v., but don't code and format conversion.
210 Like `insert-file-contents-literary', but it allows find-file-hooks,
211 automatic uncompression, etc.
212 Like `insert-file-contents-as-binary', but it converts line-break
213 code."
214   (save-excursion
215     (save-restriction
216       (narrow-to-region (point)(point))
217       (let ((return-val
218              ;; Returns list absolute file name and length of data inserted.
219              (insert-file-contents-as-binary filename visit beg end replace)))
220         (goto-char (point-min))
221         (while (re-search-forward "\r$" nil t)
222           (replace-match ""))
223         (list (car return-val) (buffer-size))))))
224
225 (defun insert-binary-file-contents-literally (filename
226                                               &optional visit beg end replace)
227   "Like `insert-file-contents-literally', q.v., but don't code conversion.
228 A buffer may be modified in several ways after reading into the buffer due
229 to advanced Emacs features, such as file-name-handlers, format decoding,
230 find-file-hooks, etc.
231   This function ensures that none of these modifications will take place."
232   (as-binary-input-file
233    ;; Returns list absolute file name and length of data inserted.
234    (insert-file-contents-literally filename visit beg end replace)))
235
236 (cond
237  (running-emacs-19_29-or-later
238   ;; for MULE 2.3 based on Emacs 19.34.
239   (defun write-region-as-binary (start end filename
240                                        &optional append visit lockname)
241     "Like `write-region', q.v., but don't code conversion."
242     (as-binary-output-file
243      (write-region start end filename append visit lockname)))
244
245   (defun write-region-as-raw-text-CRLF (start end filename
246                                               &optional append visit lockname)
247     "Like `write-region', q.v., but don't code conversion."
248     (let ((the-buf (current-buffer)))
249       (with-temp-buffer
250         (insert-buffer-substring the-buf start end)
251         (goto-char (point-min))
252         (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
253           (replace-match "\\1\r\n"))
254         (write-region-as-binary (point-min)(point-max)
255                                 filename append visit lockname))))
256
257   (defun find-file-noselect-as-binary (filename &optional nowarn rawfile)
258     "Like `find-file-noselect', q.v., but don't code and format conversion."
259     (as-binary-input-file (find-file-noselect filename nowarn rawfile)))
260   )
261  (t
262   ;; for MULE 2.3 based on Emacs 19.28.
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     (as-binary-output-file
267      (write-region start end filename append visit)))
268
269   (defun write-region-as-raw-text-CRLF (start end filename
270                                               &optional append visit lockname)
271     "Like `write-region', q.v., but don't code conversion."
272     (let ((the-buf (current-buffer)))
273       (with-temp-buffer
274         (insert-buffer-substring the-buf start end)
275         (goto-char (point-min))
276         (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
277           (replace-match "\\1\r\n"))
278         (write-region-as-binary (point-min)(point-max)
279                                 filename append visit))))
280
281   (defun find-file-noselect-as-binary (filename &optional nowarn rawfile)
282     "Like `find-file-noselect', q.v., but don't code and format conversion."
283     (as-binary-input-file (find-file-noselect filename nowarn)))
284   ))
285
286 (defun find-file-noselect-as-raw-text (filename &optional nowarn rawfile)
287   "Like `find-file-noselect', q.v., but it does not code and format conversion
288 except for line-break code."
289   (save-current-buffer
290     (prog1
291         (set-buffer (find-file-noselect-as-binary filename nowarn rawfile))
292       (let ((flag (buffer-modified-p)))
293         (save-excursion
294           (goto-char (point-min))
295           (while (re-search-forward "\r$" nil t)
296             (replace-match "")))
297         (set-buffer-modified-p flag)))))
298
299 (defun open-network-stream-as-binary (name buffer host service)
300   "Like `open-network-stream', q.v., but don't code conversion."
301   (let ((process (open-network-stream name buffer host service)))
302     (set-process-coding-system process *noconv* *noconv*)
303     process))
304
305
306 ;;; @ with code-conversion
307 ;;;
308
309 (defun insert-file-contents-as-coding-system
310   (coding-system filename &optional visit beg end replace)
311   "Like `insert-file-contents', q.v., but CODING-SYSTEM the first arg will
312 be applied to `file-coding-system-for-read'."
313   (let ((file-coding-system-for-read coding-system))
314     (insert-file-contents filename visit beg end replace)))
315
316 (cond
317  (running-emacs-19_29-or-later
318   ;; for MULE 2.3 based on Emacs 19.34.
319   (defun write-region-as-coding-system
320     (coding-system start end filename &optional append visit lockname)
321     "Like `write-region', q.v., but CODING-SYSTEM the first arg will be
322 applied to `file-coding-system'."
323     (let ((file-coding-system coding-system)
324           jka-compr-compression-info-list jam-zcat-filename-list)
325       (write-region start end filename append visit lockname)))
326
327   (defun find-file-noselect-as-coding-system
328     (coding-system filename &optional nowarn rawfile)
329     "Like `find-file-noselect', q.v., but CODING-SYSTEM the first arg will
330 be applied to `file-coding-system-for-read'."
331     (let ((file-coding-system-for-read coding-system))
332       (find-file-noselect filename nowarn rawfile)))
333   )
334  (t
335   ;; for MULE 2.3 based on Emacs 19.28.
336   (defun write-region-as-coding-system
337     (coding-system start end filename &optional append visit lockname)
338     "Like `write-region', q.v., but CODING-SYSTEM the first arg will be
339 applied to `file-coding-system'."
340     (let ((file-coding-system coding-system)
341           jka-compr-compression-info-list jam-zcat-filename-list)
342       (write-region start end filename append visit)))
343
344   (defun find-file-noselect-as-coding-system
345     (coding-system filename &optional nowarn rawfile)
346     "Like `find-file-noselect', q.v., but CODING-SYSTEM the first arg will
347 be applied to `file-coding-system-for-read'."
348     (let ((file-coding-system-for-read coding-system))
349       (find-file-noselect filename nowarn)))
350   ))
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 (or (fboundp 'truncate-string)
431     ;; Imported from Mule-2.3
432     (defun truncate-string (str width &optional start-column)
433       "\
434 Truncate STR to fit in WIDTH columns.
435 Optional non-nil arg START-COLUMN specifies the starting column.
436 \[emu-mule.el; Mule 2.3 emulating function]"
437       (or start-column
438           (setq start-column 0))
439       (let ((max-width (string-width str))
440             (len (length str))
441             (from 0)
442             (column 0)
443             to-prev to ch)
444         (if (>= width max-width)
445             (setq width max-width))
446         (if (>= start-column width)
447             ""
448           (while (< column start-column)
449             (setq ch (aref str from)
450                   column (+ column (char-width ch))
451                   from (+ from (char-bytes ch))))
452           (if (< width max-width)
453               (progn
454                 (setq to from)
455                 (while (<= column width)
456                   (setq ch (aref str to)
457                         column (+ column (char-width ch))
458                         to-prev to
459                         to (+ to (char-bytes ch))))
460                 (setq to to-prev)))
461           (substring str from to))))
462     )
463
464 (defalias 'looking-at-as-unibyte 'looking-at)
465
466
467 ;;; @ end
468 ;;;
469
470 (provide 'poem-om)
471
472 ;;; poem-om.el ends here