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