* Makefile (elc): Ignore errors when removing emu*.elc.
[elisp/apel.git] / emu-latin1.el
1 ;;; emu-latin1.el --- emu module for Emacs 19 and XEmacs without MULE
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, Latin-1
7
8 ;; This file is part of emu.
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 ;;; @ buffer representation
28 ;;;
29
30 (defmacro-maybe set-buffer-multibyte (flag)
31   "Set the multibyte flag of the current buffer to FLAG.
32 If FLAG is t, this makes the buffer a multibyte buffer.
33 If FLAG is nil, this makes the buffer a single-byte buffer.
34 The buffer contents remain unchanged as a sequence of bytes
35 but the contents viewed as characters do change.
36 \[Emacs 20.3 emulating macro]"
37   )
38
39
40 ;;; @ character set
41 ;;;
42
43 (put 'ascii 'charset-description "Character set of ASCII")
44 (put 'ascii 'charset-registry "ASCII")
45
46 (put 'latin-iso8859-1 'charset-description "Character set of ISO-8859-1")
47 (put 'latin-iso8859-1 'charset-registry "ISO8859-1")
48
49 (defun charset-description (charset)
50   "Return description of CHARSET."
51   (get charset 'charset-description))
52
53 (defun charset-registry (charset)
54   "Return registry name of CHARSET."
55   (get charset 'charset-registry))
56
57 (defun charset-width (charset)
58   "Return number of columns a CHARSET occupies when displayed."
59   1)
60
61 (defun charset-direction (charset)
62   "Return the direction of a character of CHARSET by
63   0 (left-to-right) or 1 (right-to-left)."
64   0)
65
66 (defun find-charset-string (str)
67   "Return a list of charsets in the string."
68   (if (string-match "[\200-\377]" str)
69       '(latin-iso8859-1)
70     ))
71
72 (defalias 'find-non-ascii-charset-string 'find-charset-string)
73
74 (defun find-charset-region (start end)
75   "Return a list of charsets in the region between START and END."
76   (if (save-excursion
77         (goto-char start)
78         (re-search-forward "[\200-\377]" end t))
79       '(latin-iso8859-1)
80     ))
81
82 (defalias 'find-non-ascii-charset-region 'find-charset-region)
83
84
85 ;;; @ coding-system
86 ;;;
87
88 (defconst *internal* nil)
89 (defconst *ctext* nil)
90 (defconst *noconv* nil)
91
92 (defun decode-coding-string (string coding-system)
93   "Decode the STRING which is encoded in CODING-SYSTEM."
94   string)
95
96 (defun encode-coding-string (string coding-system)
97   "Encode the STRING as CODING-SYSTEM."
98   string)
99
100 (defun decode-coding-region (start end coding-system)
101   "Decode the text between START and END which is encoded in CODING-SYSTEM."
102   0)
103
104 (defun encode-coding-region (start end coding-system)
105   "Encode the text between START and END to CODING-SYSTEM."
106   0)
107
108 (defun detect-coding-region (start end)
109   "Detect coding-system of the text in the region between START and END."
110   )
111
112 (defun set-buffer-file-coding-system (coding-system &optional force)
113   "Set buffer-file-coding-system of the current buffer to CODING-SYSTEM."
114   )
115
116
117 ;;; @@ for old MULE emulation
118 ;;;
119
120 (defun code-convert-string (str ic oc)
121   "Convert code in STRING from SOURCE code to TARGET code,
122 On successful converion, returns the result string,
123 else returns nil. [emu-latin1.el; old MULE emulating function]"
124   str)
125
126 (defun code-convert-region (beg end ic oc)
127   "Convert code of the text between BEGIN and END from SOURCE
128 to TARGET. On successful conversion returns t,
129 else returns nil. [emu-latin1.el; old MULE emulating function]"
130   t)
131
132
133 ;;; @ without code-conversion
134 ;;;
135
136 (defmacro as-binary-process (&rest body)
137   (` (let (selective-display)   ; Disable ^M to nl translation.
138        (,@ body))))
139
140 (defmacro as-binary-input-file (&rest body)
141   (` (let ((emx-binary-mode t)) ; Stop CRLF to LF conversion in OS/2
142        (,@ body))))
143
144 (defmacro as-binary-output-file (&rest body)
145   (` (let ((emx-binary-mode t)) ; Stop CRLF to LF conversion in OS/2
146        (,@ body))))
147
148 (defun write-region-as-binary (start end filename
149                                      &optional append visit lockname)
150   "Like `write-region', q.v., but don't code conversion."
151   (let ((emx-binary-mode t))
152     (write-region start end filename append visit lockname)))
153
154 (defun insert-file-contents-as-binary (filename
155                                        &optional visit beg end replace)
156   "Like `insert-file-contents', q.v., but don't code and format conversion.
157 Like `insert-file-contents-literary', but it allows find-file-hooks,
158 automatic uncompression, etc.
159
160 Namely this function ensures that only format decoding and character
161 code conversion will not take place."
162   (let ((emx-binary-mode t))
163     ;; Returns list of absolute file name and length of data inserted.
164     (insert-file-contents filename visit beg end replace)))
165
166 (defalias 'insert-binary-file-contents 'insert-file-contents-as-binary)
167 (make-obsolete 'insert-binary-file-contents 'insert-file-contents-as-binary)
168
169 (defun insert-binary-file-contents-literally (filename
170                                               &optional visit beg end replace)
171   "Like `insert-file-contents-literally', q.v., but don't code conversion.
172 A buffer may be modified in several ways after reading into the buffer due
173 to advanced Emacs features, such as file-name-handlers, format decoding,
174 find-file-hooks, etc.
175   This function ensures that none of these modifications will take place."
176   (let ((emx-binary-mode t))
177     ;; Returns list of absolute file name and length of data inserted.
178     (insert-file-contents-literally filename visit beg end replace)))
179
180 (defalias 'insert-file-contents-as-raw-text 'insert-file-contents)
181
182 (defun write-region-as-raw-text-CRLF (start end filename
183                                             &optional append visit lockname)
184   "Like `write-region', q.v., but write as network representation."
185   (let ((the-buf (current-buffer)))
186     (with-temp-buffer
187       (insert-buffer-substring the-buf start end)
188       (goto-char (point-min))
189       (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
190         (replace-match "\\1\r\n"))
191       (write-region (point-min)(point-max) filename append visit lockname))))
192
193
194 ;;; @ MIME charset
195 ;;;
196
197 (defvar charsets-mime-charset-alist
198   '(((ascii) . us-ascii)))
199
200 (defvar default-mime-charset 'iso-8859-1)
201
202 (defun mime-charset-to-coding-system (charset)
203   (if (stringp charset)
204       (setq charset (intern (downcase charset)))
205     )
206   (if (memq charset (list 'us-ascii default-mime-charset))
207       charset
208     ))
209
210 (defun detect-mime-charset-region (start end)
211   "Return MIME charset for region between START and END."
212   (if (save-excursion
213         (goto-char start)
214         (re-search-forward "[\200-\377]" end t))
215       default-mime-charset
216     'us-ascii))
217
218 (defun encode-mime-charset-region (start end charset)
219   "Encode the text between START and END as MIME CHARSET."
220   )
221
222 (defun decode-mime-charset-region (start end charset &optional lbt)
223   "Decode the text between START and END as MIME CHARSET."
224   (cond ((eq lbt 'CRLF)
225          (save-excursion
226            (save-restriction
227              (narrow-to-region start end)
228              (goto-char (point-min))
229              (while (search-forward "\r\n" nil t)
230                (replace-match "\n"))
231              ))
232          )))
233
234 (defun encode-mime-charset-string (string charset)
235   "Encode the STRING as MIME CHARSET."
236   string)
237
238 (defun decode-mime-charset-string (string charset &optional lbt)
239   "Decode the STRING as MIME CHARSET."
240   (if lbt
241       (with-temp-buffer
242         (insert string)
243         (decode-mime-charset-region (point-min)(point-max) charset lbt)
244         (buffer-string))
245     string))
246
247 (defalias 'write-region-as-mime-charset 'write-region)
248
249
250 ;;; @ character
251 ;;;
252
253 (defun char-charset (char)
254   "Return the character set of char CHAR."
255   (if (< chr 128)
256       'ascii
257     'latin-iso8859-1))
258
259 (defun char-bytes (char)
260   "Return number of bytes a character in CHAR occupies in a buffer."
261   1)
262
263 (defun char-width (char)
264   "Return number of columns a CHAR occupies when displayed."
265   1)
266
267 (defun split-char (character)
268   "Return list of charset and one or two position-codes of CHARACTER."
269   (cons (char-charset character) character))
270
271 (defalias 'char-length 'char-bytes)
272
273 (defmacro char-next-index (char index)
274   "Return index of character succeeding CHAR whose index is INDEX."
275   (` (1+ (, index))))
276
277
278 ;;; @ string
279 ;;;
280
281 (defalias 'string-width 'length)
282
283 (defun string-to-char-list (str)
284   (mapcar (function identity) str))
285
286 (defalias 'string-to-int-list 'string-to-char-list)
287
288 (defalias 'sref 'aref)
289
290 (defun truncate-string (str width &optional start-column)
291   "Truncate STR to fit in WIDTH columns.
292 Optional non-nil arg START-COLUMN specifies the starting column.
293 \[emu-latin1.el; MULE 2.3 emulating function]"
294   (or start-column
295       (setq start-column 0))
296   (substring str start-column width))
297
298 (defalias 'looking-at-as-unibyte 'looking-at)
299
300 ;;; @@ obsoleted aliases
301 ;;;
302 ;;; You should not use them.
303
304 (defalias 'string-columns 'length)
305 (make-obsolete 'string-columns 'string-width)
306
307
308 ;;; @ end
309 ;;;
310
311 (provide 'emu-latin1)
312
313 ;;; emu-latin1.el ends here