(find-file-noselect-as-raw-text): Convert line-break code each time.
[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
119 ;;; @ without code-conversion
120 ;;;
121
122 (defmacro as-binary-process (&rest body)
123   (` (let (selective-display    ; Disable ^M to nl translation.
124            ;; Mule
125            mc-flag      
126            (default-process-coding-system (cons *noconv* *noconv*))
127            program-coding-system-alist)
128        (,@ body))))
129
130 (defmacro as-binary-input-file (&rest body)
131   (` (let (mc-flag
132            (file-coding-system-for-read *noconv*)
133            )
134        (,@ body))))
135
136 (defmacro as-binary-output-file (&rest body)
137   (` (let (mc-flag
138            (file-coding-system *noconv*)
139            )
140        (,@ body))))
141
142 (defalias 'set-process-input-coding-system 'set-process-coding-system)
143
144 (defun insert-file-contents-as-binary (filename
145                                        &optional visit beg end replace)
146   "Like `insert-file-contents', q.v., but don't code and format conversion.
147 Like `insert-file-contents-literary', but it allows find-file-hooks,
148 automatic uncompression, etc.
149
150 Namely this function ensures that only format decoding and character
151 code conversion will not take place."
152   (as-binary-input-file
153    ;; Returns list absolute file name and length of data inserted.
154    (insert-file-contents filename visit beg end replace)))
155
156 (defun insert-file-contents-as-raw-text (filename
157                                          &optional visit beg end replace)
158   "Like `insert-file-contents', q.v., but don't code and format conversion.
159 Like `insert-file-contents-literary', but it allows find-file-hooks,
160 automatic uncompression, etc.
161 Like `insert-file-contents-as-binary', but it converts line-break
162 code."
163   (save-excursion
164     (save-restriction
165       (narrow-to-region (point)(point))
166       (let ((return-val
167              ;; Returns list absolute file name and length of data inserted.
168              (insert-file-contents-as-binary filename visit beg end replace)))
169         (goto-char (point-min))
170         (while (re-search-forward "\r$" nil t)
171           (replace-match ""))
172         (list (car return-val) (buffer-size))))))
173
174 (defun insert-binary-file-contents-literally (filename
175                                               &optional visit beg end replace)
176   "Like `insert-file-contents-literally', q.v., but don't code conversion.
177 A buffer may be modified in several ways after reading into the buffer due
178 to advanced Emacs features, such as file-name-handlers, format decoding,
179 find-file-hooks, etc.
180   This function ensures that none of these modifications will take place."
181   (as-binary-input-file
182    ;; Returns list absolute file name and length of data inserted.
183    (insert-file-contents-literally filename visit beg end replace)))
184
185 (cond
186  (running-emacs-19_29-or-later
187   ;; for MULE 2.3 based on Emacs 19.34.
188   (defun write-region-as-binary (start end filename
189                                        &optional append visit lockname)
190     "Like `write-region', q.v., but don't code conversion."
191     (as-binary-output-file
192      (write-region start end filename append visit lockname)))
193
194   (defun write-region-as-raw-text-CRLF (start end filename
195                                               &optional append visit lockname)
196     "Like `write-region', q.v., but don't code conversion."
197     (let ((the-buf (current-buffer)))
198       (with-temp-buffer
199         (insert-buffer-substring the-buf start end)
200         (goto-char (point-min))
201         (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
202           (replace-match "\\1\r\n"))
203         (write-region-as-binary (point-min)(point-max)
204                                 filename append visit lockname))))
205   )
206  (t
207   ;; for MULE 2.3 based on Emacs 19.28.
208   (defun write-region-as-binary (start end filename
209                                        &optional append visit lockname)
210     "Like `write-region', q.v., but don't code conversion."
211     (as-binary-output-file
212      (write-region start end filename append visit)))
213
214   (defun write-region-as-raw-text-CRLF (start end filename
215                                               &optional append visit lockname)
216     "Like `write-region', q.v., but don't code conversion."
217     (let ((the-buf (current-buffer)))
218       (with-temp-buffer
219         (insert-buffer-substring the-buf start end)
220         (goto-char (point-min))
221         (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
222           (replace-match "\\1\r\n"))
223         (write-region-as-binary (point-min)(point-max)
224                                 filename append visit))))
225   ))
226
227 (defun find-file-noselect-as-binary (filename &optional nowarn rawfile)
228   "Like `find-file-noselect', q.v., but don't code and format conversion."
229   (as-binary-input-file (find-file-noselect filename nowarn rawfile)))
230
231 (defun find-file-noselect-as-raw-text (filename &optional nowarn rawfile)
232   "Like `find-file-noselect', q.v., but it does not code and format conversion
233 except for line-break code."
234   (save-current-buffer
235     (prog1
236         (set-buffer (find-file-noselect-as-binary filename nowarn rawfile))
237       (let ((flag (buffer-modified-p)))
238         (save-excursion
239           (goto-char (point-min))
240           (while (re-search-forward "\r$" nil t)
241             (replace-match "")))
242         (set-buffer-modified-p flag)))))
243
244 (defun open-network-stream-as-binary (name buffer host service)
245   "Like `open-network-stream', q.v., but don't code conversion."
246   (let ((process (open-network-stream name buffer host service)))
247     (set-process-coding-system process *noconv* *noconv*)
248     process))
249
250
251 ;;; @ with code-conversion
252 ;;;
253
254 (defun insert-file-contents-as-specified-coding-system (filename &rest args)
255   "Like `insert-file-contents', q.v., but code convert by the specified
256 coding-system. ARGS the optional arguments are passed to
257 `insert-file-contents' except for the last element. The last element of
258 ARGS must be a coding-system."
259   (let ((file-coding-system-for-read (car (reverse args))))
260     (apply 'insert-file-contents filename (nreverse (cdr (nreverse args))))))
261
262 (cond
263  (running-emacs-19_29-or-later
264   ;; for MULE 2.3 based on Emacs 19.34.
265   (defun write-region-as-specified-coding-system (start end filename
266                                                         &rest args)
267     "Like `write-region', q.v., but code convert by the specified
268 coding-system. ARGS the optional arguments are passed to `write-region'
269 except for the last element. The last element of ARGS must be a
270 coding-system."
271     (let ((file-coding-system (car (reverse args)))
272           jka-compr-compression-info-list jam-zcat-filename-list)
273       (apply 'write-region start end filename
274              (nreverse (cdr (nreverse args))))))
275   )
276  (t
277   ;; for MULE 2.3 based on Emacs 19.28.
278   (defun write-region-as-specified-coding-system (start end filename
279                                                         &rest args)
280     "Like `write-region', q.v., but code convert by the specified
281 coding-system. ARGS the optional arguments are passed to `write-region'
282 except for the last element. The last element of ARGS must be a
283 coding-system."
284     (let ((code (car (reverse args)))
285           (args (nreverse (cdr (nreverse args))))
286           jka-compr-compression-info-list jam-zcat-filename-list)
287       (write-region start end filename (car args) (car (cdr args)) code)))
288   ))
289
290 (defun find-file-noselect-as-specified-coding-system (filename &optional args)
291   "Like `find-file-noselect', q.v., but code convert by the specified
292 coding-system. ARGS the optional arguments are passed to `find-file-noselect'
293 except for the last element. The last element of ARGS must be a
294 coding-system."
295   (let ((file-coding-system-for-read (car (reverse args))))
296     (apply' find-file-noselect filename (nreverse (cdr (nreverse args))))))
297
298
299 ;;; @ buffer representation
300 ;;;
301
302 (defsubst-maybe set-buffer-multibyte (flag)
303   "Set the multibyte flag of the current buffer to FLAG.
304 If FLAG is t, this makes the buffer a multibyte buffer.
305 If FLAG is nil, this makes the buffer a single-byte buffer.
306 The buffer contents remain unchanged as a sequence of bytes
307 but the contents viewed as characters do change.
308 \[Emacs 20.3 emulating function]"
309   (setq mc-flag flag)
310   )
311
312
313 ;;; @ character
314 ;;;
315
316 (defalias 'char-charset 'char-leading-char)
317
318 (defun split-char (character)
319   "Return list of charset and one or two position-codes of CHARACTER."
320   (let ((p (1- (char-bytes character)))
321         dest)
322     (while (>= p 1)
323       (setq dest (cons (- (char-component character p) 128) dest)
324             p (1- p)))
325     (cons (char-charset character) dest)))
326
327 (defmacro char-next-index (char index)
328   "Return index of character succeeding CHAR whose index is INDEX."
329   (` (+ (, index) (char-bytes (, char)))))
330
331 (if (subr-fboundp 'char-before)
332     (condition-case err
333         (char-before)
334       (error
335        (when (and (eq (car (get (car err) 'error-conditions))
336                       'wrong-number-of-arguments)
337                   (not (boundp 'si:char-before)))
338          (fset 'si:char-before (symbol-function 'char-before))
339          (defun char-before (&optional pos)
340            "Return character in current buffer preceding position POS.
341 POS is an integer or a buffer pointer.
342 If POS is out of range, the value is nil."
343            (si:char-before (or pos (point)))
344            )))))
345
346 (if (subr-fboundp 'char-after)
347     (condition-case err
348         (char-after)
349       (error
350        (when (and (eq (car (get (car err) 'error-conditions))
351                       'wrong-number-of-arguments)
352                   (not (boundp 'si:char-after)))
353          (fset 'si:char-after (symbol-function 'char-after))
354          (defun char-after (&optional pos)
355            "Return character in current buffer at position POS.
356 POS is an integer or a buffer pointer.
357 If POS is out of range, the value is nil."
358            (si:char-after (or pos (point)))
359            )))))
360
361 ;;; @@ obsoleted aliases
362 ;;;
363 ;;; You should not use them.
364
365 (defalias 'char-length 'char-bytes)
366 ;;(defalias 'char-columns 'char-width)
367
368
369 ;;; @ string
370 ;;;
371
372 (defalias 'string-columns 'string-width)
373
374 (defalias 'string-to-int-list 'string-to-char-list)
375
376 (or (fboundp 'truncate-string)
377     ;; Imported from Mule-2.3
378     (defun truncate-string (str width &optional start-column)
379       "\
380 Truncate STR to fit in WIDTH columns.
381 Optional non-nil arg START-COLUMN specifies the starting column.
382 \[emu-mule.el; Mule 2.3 emulating function]"
383       (or start-column
384           (setq start-column 0))
385       (let ((max-width (string-width str))
386             (len (length str))
387             (from 0)
388             (column 0)
389             to-prev to ch)
390         (if (>= width max-width)
391             (setq width max-width))
392         (if (>= start-column width)
393             ""
394           (while (< column start-column)
395             (setq ch (aref str from)
396                   column (+ column (char-width ch))
397                   from (+ from (char-bytes ch))))
398           (if (< width max-width)
399               (progn
400                 (setq to from)
401                 (while (<= column width)
402                   (setq ch (aref str to)
403                         column (+ column (char-width ch))
404                         to-prev to
405                         to (+ to (char-bytes ch))))
406                 (setq to to-prev)))
407           (substring str from to))))
408     )
409
410 (defalias 'looking-at-as-unibyte 'looking-at)
411
412
413 ;;; @ end
414 ;;;
415
416 (provide 'poem-om)
417
418 ;;; poem-om.el ends here