70
|
1 ;;; mule-files.el --- File I/O functions for XEmacs/Mule.
|
|
2
|
|
3 ;; Copyright (C) 1992,93,94,95 Free Software Foundation, Inc.
|
|
4 ;; Copyright (C) 1995 Amdahl Corporation.
|
|
5 ;; Copyright (C) 1995 Sun Microsystems.
|
|
6
|
|
7 ;; This file is part of XEmacs.
|
|
8
|
|
9 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
10 ;; under the terms of the GNU General Public License as published by
|
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
14 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17 ;; General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 ;; Boston, MA 02111-1307, USA.
|
|
23
|
|
24 ;;; Commentary:
|
|
25
|
|
26 ;;; Derived from mule.el in the original Mule but heavily modified
|
|
27 ;;; by Ben Wing.
|
|
28
|
|
29 ;;; Code:
|
|
30
|
|
31 ;;;; #### also think more about `binary' vs. `no-conversion'
|
|
32
|
|
33 (setq-default file-coding-system 'iso-2022-8)
|
|
34 (put 'file-coding-system 'permanent-local t)
|
|
35
|
|
36 (defvar overriding-file-coding-system nil
|
|
37 "Overriding coding system used when reading a file.
|
|
38 You should *bind* this, not set it. If this is non-nil, it specifies
|
|
39 the coding system that will be used when a file is read in, and
|
|
40 overrides `file-coding-system-for-read', `file-coding-system-alist',
|
|
41 etc. Use those variables instead of this one for permanent changes
|
|
42 to the environment.")
|
|
43
|
|
44 (defvar file-coding-system-for-read 'autodetect
|
|
45 "Coding system used when reading a file.
|
|
46 This provides coarse-grained control; for finer-grained control,
|
|
47 use `file-coding-system-alist'. From a Lisp program, if you wish
|
|
48 to unilaterally specify the coding system used for one
|
|
49 particular operation, you should bind the variable
|
|
50 `overriding-file-coding-system' rather than setting this variable,
|
|
51 which is intended to be used for global environment specification.")
|
|
52
|
|
53 (defvar file-coding-system-alist
|
78
|
54 ;; '(; ("\\.el$" . euc-japan)
|
80
|
55 '(("\\.el$" . iso-2022-8)
|
|
56 ("\\.info$" . iso-2022-8)
|
78
|
57 ("\\.\\(gz\\|Z\\)$" . binary)
|
70
|
58 ("/spool/mail/.*$" . convert-mbox-coding-system))
|
|
59 "Alist specifying the coding system used for particular files.
|
|
60 Each element of the alist is a cons of a regexp, specifying the files
|
|
61 to be affected, and a coding system. This overrides the more
|
|
62 general specification in `file-coding-system-for-read', but is
|
|
63 overridden by `overriding-file-coding-system'.
|
|
64
|
|
65 Instead of a coding system you may specify a function, and it will be
|
|
66 called after the file has been read in to decode the file. It is
|
|
67 called with four arguments: FILENAME, VISIT, START, and END, the same
|
|
68 as the first four arguments to `insert-file-contents'.")
|
|
69
|
|
70 (defun set-file-coding-system (coding-system &optional force)
|
|
71 "Set the current `file-coding-system' to CODING-SYSTEM.
|
|
72 If optional argument FORCE (interactively, the prefix argument) is not
|
|
73 given, attempt to match the EOL type of the new coding system to
|
|
74 the current value of `file-coding-system'."
|
|
75 (interactive "zFile coding system: \nP")
|
|
76 (get-coding-system coding-system) ;; correctness check
|
|
77 (if (not force)
|
|
78 (setq coding-system
|
|
79 (subsidiary-coding-system coding-system (coding-system-eol-type
|
|
80 file-coding-system))))
|
|
81 (setq file-coding-system coding-system)
|
|
82 (redraw-modeline t))
|
|
83
|
|
84 (defun set-file-coding-system-for-read (coding-system)
|
|
85 "Set the coding system used when reading in a file.
|
|
86 This is equivalent to setting the variable `file-coding-system-for-read'.
|
|
87 You can also use `file-coding-system-alist' to specify the coding system
|
|
88 for particular files."
|
|
89 (interactive "zFile coding system for read: ")
|
|
90 (get-coding-system coding-system) ;; correctness check
|
|
91 (setq file-coding-system-for-read coding-system))
|
|
92
|
|
93 (defun set-default-file-coding-system (coding-system)
|
|
94 "Set the default value of `file-coding-system' to CODING-SYSTEM.
|
|
95 The default value is used both for buffers without associated files
|
|
96 and for files with no apparent coding system (i.e. primarily ASCII).
|
|
97 See `file-coding-system' for more information."
|
|
98 (interactive "zDefault file coding system: ")
|
|
99 (setq-default file-coding-system coding-system)
|
|
100 (redraw-modeline t))
|
|
101
|
|
102 (defun find-file-coding-system-from-filename (filename)
|
|
103 "Look up a file in `file-coding-system-alist'.
|
|
104 The return value will be nil (no applicable entry), a coding system object
|
|
105 \(the entry specified a coding system), or something else (the entry
|
|
106 specified a function to be called)."
|
|
107 (let ((alist file-coding-system-alist)
|
|
108 (found nil)
|
|
109 (codesys nil))
|
|
110 (let ((case-fold-search (eq system-type 'vax-vms)))
|
|
111 (setq filename (file-name-sans-versions filename))
|
|
112 (while (and (not found) alist)
|
|
113 (if (string-match (car (car alist)) filename)
|
|
114 (setq codesys (cdr (car alist))
|
|
115 found t))
|
|
116 (setq alist (cdr alist))))
|
|
117 (if codesys
|
|
118 (cond ((find-coding-system codesys))
|
|
119 (t codesys)))))
|
|
120
|
|
121 (defun convert-mbox-coding-system (filename visit start end)
|
|
122 "Decoding function for Unix mailboxes.
|
|
123 Does separate detection and decoding on each message, since each
|
|
124 message might be in a different encoding."
|
|
125 (let ((buffer-read-only nil))
|
|
126 (save-restriction
|
|
127 (narrow-to-region start end)
|
|
128 (goto-char (point-min))
|
|
129 (while (not (eobp))
|
|
130 (let ((start (point))
|
|
131 end)
|
|
132 (forward-char 1)
|
|
133 (if (re-search-forward "^From" nil 'move)
|
|
134 (beginning-of-line))
|
|
135 (setq end (point))
|
|
136 (decode-coding-region start end 'autodetect))))))
|
|
137
|
|
138 (defun find-coding-system-magic-cookie ()
|
|
139 "Look for the coding-system magic cookie in the current buffer.\n"
|
|
140 "The coding-system magic cookie is the exact string\n"
|
|
141 "\";;;###coding system: \" followed by a valid coding system symbol,\n"
|
|
142 "somewhere within the first 3000 characters of the file. If found,\n"
|
|
143 "the coding system symbol is returned; otherwise nil is returned.\n"
|
|
144 "Note that it is extremely unlikely that such a string would occur\n"
|
|
145 "coincidentally as the result of encoding some characters in a non-ASCII\n"
|
|
146 "charset, and that the spaces make it even less likely since the space\n"
|
|
147 "character is not a valid octet in any ISO 2022 encoding of most non-ASCII\n"
|
|
148 "charsets."
|
|
149 (save-excursion
|
|
150 (goto-char (point-min))
|
|
151 (let ((case-fold-search nil))
|
|
152 (if (search-forward ";;;###coding system: " (+ (point-min) 3000) t)
|
|
153 (let ((start (point))
|
|
154 (end (progn
|
|
155 (skip-chars-forward "^ \t\n\r")
|
|
156 (point))))
|
|
157 (if (> end start)
|
|
158 (let ((codesys (intern (buffer-substring start end))))
|
|
159 (if (find-coding-system codesys) codesys))))))))
|
|
160
|
|
161 (defun load (file &optional noerror nomessage nosuffix)
|
|
162 "Execute a file of Lisp code named FILE.
|
|
163 First tries FILE with .elc appended, then tries with .el,
|
|
164 then tries FILE unmodified. Searches directories in load-path.
|
|
165 If optional second arg NOERROR is non-nil,
|
|
166 report no error if FILE doesn't exist.
|
|
167 Print messages at start and end of loading unless
|
|
168 optional third arg NOMESSAGE is non-nil.
|
|
169 If optional fourth arg NOSUFFIX is non-nil, don't try adding
|
|
170 suffixes .elc or .el to the specified name FILE.
|
|
171 Return t if file exists."
|
|
172 (let* ((filename (substitute-in-file-name file))
|
|
173 (handler (find-file-name-handler filename 'load))
|
|
174 (path nil))
|
|
175 (if handler
|
|
176 (funcall handler 'load filename noerror nomessage nosuffix)
|
|
177 (if (or (<= (length filename) 0)
|
|
178 (null (setq path
|
|
179 (locate-file filename load-path
|
|
180 (and (not nosuffix) ".elc:.el:")))))
|
|
181 (and (null noerror)
|
|
182 (signal 'file-error (list "Cannot open load file" filename)))
|
|
183 (let (__codesys__)
|
|
184 (save-excursion
|
|
185 (set-buffer (get-buffer-create " *load*"))
|
|
186 (erase-buffer)
|
|
187 (let ((file-coding-system-for-read 'no-conversion))
|
|
188 (insert-file-contents path nil 1 3001))
|
|
189 (setq __codesys__ (find-coding-system-magic-cookie)))
|
|
190 ;; use string= instead of string-match to keep match-data.
|
|
191 (if (string= ".elc" (downcase (substring path -4)))
|
|
192 ;; if reading a byte-compiled file and we didn't find
|
|
193 ;; a coding-system magic cookie, then use `binary'.
|
|
194 ;; We need to guarantee that we never do autodetection
|
|
195 ;; on byte-compiled files because confusion here would
|
|
196 ;; be a very bad thing. Pre-existing byte-compiled
|
|
197 ;; files are always in the `no-conversion' system.
|
|
198 ;; Also, byte-compiled files always use `lf' to terminate
|
|
199 ;; a line; don't risk confusion here either.
|
|
200 (if (not __codesys__)
|
|
201 (setq __codesys__ 'binary))
|
|
202 ;; otherwise use `file-coding-system-for-read', as normal
|
|
203 ;; #### need to do some looking up in file-coding-system-alist!
|
|
204 (if (not __codesys__)
|
|
205 (setq __codesys__ file-coding-system-for-read)))
|
|
206 ;; now use the internal load to actually load the file.
|
|
207 (load-internal file noerror nomessage nosuffix __codesys__))))))
|
|
208
|
|
209 (defvar insert-file-contents-access-hook nil
|
|
210 "A hook to make a file accessible before reading it.
|
|
211 `insert-file-contents' calls this hook before doing anything else.
|
|
212 Called with two arguments: FILENAME and VISIT, the same as the
|
|
213 corresponding arguments in the call to `insert-file-contents'.")
|
|
214
|
|
215 (defvar insert-file-contents-pre-hook nil
|
|
216 "A special hook to decide the coding system used for reading in a file.
|
|
217
|
|
218 Before reading a file, `insert-file-contents' calls the functions
|
|
219 on this hook with arguments FILENAME and VISIT, the same as the
|
|
220 corresponding arguments in the call to `insert-file-contents'. In
|
|
221 these functions, you may refer to the global variable
|
|
222 `file-coding-system-for-read'.
|
|
223
|
|
224 The return value of the functions should be either
|
|
225
|
|
226 -- nil
|
|
227 -- A coding system or a symbol denoting it, indicating the coding system
|
|
228 to be used for reading the file
|
|
229 -- A list of two elements (absolute pathname and length of data inserted),
|
|
230 which is used as the return value to `insert-file-contents'. In this
|
|
231 case, `insert-file-contents' assumes that the function has inserted
|
|
232 the file for itself and suppresses further reading.
|
|
233
|
|
234 If any function returns non-nil, the remaining functions are not called.")
|
|
235
|
|
236 (defvar insert-file-contents-error-hook nil
|
|
237 "A hook to set `file-coding-system' when a read error has occurred.
|
|
238
|
|
239 When a file error (e.g. nonexistent file) occurs while read a file,
|
|
240 `insert-file-contents' calls the functions on this hook with three
|
|
241 arguments: FILENAME and VISIT (the same as the corresponding arguments
|
|
242 in the call to `insert-file-contents') and a cons (SIGNALED-CONDITIONS
|
|
243 . SIGNAL-DATA).
|
|
244
|
|
245 After calling this hook, the error is signalled for real and
|
|
246 propagates to the caller of `insert-file-contents'.")
|
|
247
|
|
248 (defvar insert-file-contents-post-hook nil
|
|
249 "A hook to set `file-coding-system' for the current buffer.
|
|
250
|
|
251 After successful reading, `insert-file-contents' calls the functions
|
|
252 on this hook with four arguments: FILENAME and VISIT (the same as the
|
|
253 corresponding arguments in the call to `insert-file-contents'),
|
|
254 CODING-SYSTEM (the actual coding system used to decode the file), and
|
|
255 a cons of absolute pathname and length of data inserted (the same
|
|
256 thing as will be returned from `insert-file-contents').")
|
|
257
|
|
258 (defun insert-file-contents (filename &optional visit beg end replace)
|
|
259 "Insert contents of file FILENAME after point.
|
|
260 Returns list of absolute file name and length of data inserted.
|
|
261 If second argument VISIT is non-nil, the buffer's visited filename
|
|
262 and last save file modtime are set, and it is marked unmodified.
|
|
263 If visiting and the file does not exist, visiting is completed
|
|
264 before the error is signaled.
|
|
265
|
|
266 The optional third and fourth arguments BEG and END
|
|
267 specify what portion of the file to insert.
|
|
268 If VISIT is non-nil, BEG and END must be nil.
|
|
269 If optional fifth argument REPLACE is non-nil,
|
|
270 it means replace the current buffer contents (in the accessible portion)
|
|
271 with the file contents. This is better than simply deleting and inserting
|
|
272 the whole thing because (1) it preserves some marker positions
|
|
273 and (2) it puts less data in the undo list.
|
|
274
|
|
275 NOTE: When Mule support is enabled, the REPLACE argument is
|
|
276 currently ignored.
|
|
277
|
|
278 The coding system used for decoding the file is determined as follows:
|
|
279
|
|
280 1. `overriding-file-coding-system', if non-nil.
|
|
281 2. The result of `insert-file-contents-pre-hook', if non-nil.
|
|
282 3. The matching value for this filename from `file-coding-system-alist',
|
|
283 if any.
|
|
284 4. `file-coding-system-for-read', if non-nil.
|
|
285 5. The coding system 'no-conversion.
|
|
286
|
|
287 If a local value for `file-coding-system' in the current buffer does
|
|
288 not exist, it is set to the coding system which was actually used for
|
|
289 reading.
|
|
290
|
|
291 See also `insert-file-contents-access-hook', `insert-file-contents-pre-hook',
|
|
292 `insert-file-contents-error-hook', and `insert-file-contents-post-hook'."
|
|
293 (let (return-val coding-system used-codesys conversion-func)
|
|
294 ;; OK, first load the file.
|
|
295 (condition-case err
|
|
296 (progn
|
|
297 (run-hook-with-args 'insert-file-contents-access-hook
|
|
298 filename visit)
|
|
299 ;; determine the coding system to use, as described above.
|
|
300 (setq coding-system
|
|
301 (or
|
|
302 ;; #1.
|
|
303 overriding-file-coding-system
|
|
304 ;; #2.
|
|
305 (run-special-hook-with-args 'insert-file-contents-pre-hook
|
|
306 filename visit)
|
|
307 ;; #3.
|
|
308 (let ((retval (find-file-coding-system-from-filename
|
|
309 filename)))
|
|
310 (if (or (null retval) (coding-system-p retval))
|
|
311 retval
|
|
312 (setq conversion-func retval)
|
|
313 'no-conversion))
|
|
314 ;; #4.
|
|
315 file-coding-system-for-read
|
|
316 ;; #5.
|
|
317 'no-conversion))
|
|
318 (if (consp coding-system)
|
|
319 (setq return-val coding-system)
|
|
320 (if (null (find-coding-system coding-system))
|
|
321 (progn
|
|
322 (message "Invalid coding-system (%s), using 'autodetect"
|
|
323 coding-system)
|
|
324 (setq coding-system 'autodetect)))
|
|
325 (setq return-val
|
|
326 (insert-file-contents-internal filename visit beg end
|
|
327 replace coding-system
|
|
328 ;; store here!
|
|
329 'used-codesys))
|
|
330 ))
|
|
331 (file-error
|
|
332 (run-hook-with-args 'insert-file-contents-error-hook
|
|
333 filename visit err)
|
|
334 (signal (car err) (cdr err))))
|
|
335 (setq coding-system used-codesys)
|
|
336 ;; call any `post-read-conversion' for the coding system that
|
|
337 ;; was used ...
|
|
338 (let ((func
|
|
339 (coding-system-property coding-system 'post-read-conversion))
|
|
340 (endmark (make-marker)))
|
|
341 (set-marker endmark (+ (point) (nth 1 return-val)))
|
|
342 (if func
|
|
343 (unwind-protect
|
|
344 (save-excursion
|
|
345 (let (buffer-read-only)
|
|
346 (funcall func (point) (marker-position endmark))))
|
|
347 (if visit
|
|
348 (progn
|
|
349 (set-buffer-auto-saved)
|
|
350 (set-buffer-modified-p nil)))))
|
|
351 (setcar (cdr return-val) (- (marker-position endmark) (point)))
|
|
352 ;; also call any post-conversion function called for by
|
|
353 ;; `file-coding-system-alist'
|
|
354 (if conversion-func
|
|
355 (unwind-protect
|
|
356 (save-excursion
|
|
357 (let (buffer-read-only)
|
|
358 (funcall conversion-func (point) (marker-position endmark))))
|
|
359 (if visit
|
|
360 (progn
|
|
361 (set-buffer-auto-saved)
|
|
362 (set-buffer-modified-p nil)))))
|
|
363 (setcar (cdr return-val) (- (marker-position endmark) (point))))
|
|
364 ;; now finally set the buffer's `file-coding-system'.
|
|
365 (if (run-special-hook-with-args 'insert-file-contents-post-hook
|
|
366 filename visit return-val)
|
|
367 nil
|
|
368 (if (local-variable-p 'file-coding-system (current-buffer))
|
|
369 ;; if file-coding-system is already local, just
|
|
370 ;; set its eol type to what was found, if it wasn't
|
|
371 ;; set already.
|
|
372 (set-file-coding-system
|
|
373 (subsidiary-coding-system file-coding-system
|
|
374 (coding-system-eol-type coding-system)))
|
|
375 ;; otherwise actually set file-coding-system.
|
|
376 (set-file-coding-system coding-system)))
|
|
377 return-val))
|
|
378
|
|
379 (defvar write-region-pre-hook nil
|
|
380 "A special hook to decide the coding system used for writing out a file.
|
|
381
|
|
382 Before writing a file, `write-region' calls the functions on this hook
|
|
383 with arguments START, END, FILENAME, APPEND, VISIT, and CODING-SYTEM,
|
|
384 the same as the corresponding arguments in the call to
|
|
385 `write-region'.
|
|
386
|
|
387 The return value of the functions should be either
|
|
388
|
|
389 -- nil
|
|
390 -- A coding system or a symbol denoting it, indicating the coding system
|
|
391 to be used for reading the file
|
|
392 -- A list of two elements (absolute pathname and length of data written),
|
|
393 which is used as the return value to `write-region'. In this
|
|
394 case, `write-region' assumes that the function has written
|
|
395 the file for itself and suppresses further writing.
|
|
396
|
|
397 If any function returns non-nil, the remaining functions are not called.")
|
|
398
|
|
399 (defvar write-region-post-hook nil
|
|
400 "A hook called by `write-region' after a file has been written out.
|
|
401
|
|
402 The functions on this hook are called with arguments START, END,
|
|
403 FILENAME, APPEND, VISIT, and CODING-SYSTEM, the same as the
|
|
404 corresponding arguments in the call to `write-region'.")
|
|
405
|
80
|
406 (defun write-region (start end filename &optional append visit lockname coding-system)
|
70
|
407 "Write current region into specified file.
|
|
408 When called from a program, takes three arguments:
|
|
409 START, END and FILENAME. START and END are buffer positions.
|
|
410 Optional fourth argument APPEND if non-nil means
|
|
411 append to existing file contents (if any).
|
|
412 Optional fifth argument VISIT if t means
|
|
413 set last-save-file-modtime of buffer to this file's modtime
|
|
414 and mark buffer not modified.
|
|
415 If VISIT is a string, it is a second file name;
|
|
416 the output goes to FILENAME, but the buffer is marked as visiting VISIT.
|
|
417 VISIT is also the file name to lock and unlock for clash detection.
|
|
418 If VISIT is neither t nor nil nor a string,
|
|
419 that means do not print the \"Wrote file\" message.
|
|
420 The optional sixth arg LOCKNAME, if non-nil, specifies the name to
|
|
421 use for locking and unlocking, overriding FILENAME and VISIT.
|
|
422 Kludgy feature: if START is a string, then that string is written
|
|
423 to the file, instead of any buffer contents, and END is ignored.
|
|
424 Optional seventh argument CODING-SYSTEM specifies the coding system
|
|
425 used to encode the text when it is written out, and defaults to
|
|
426 the value of `file-coding-system' in the current buffer.
|
|
427 Interactively, with a prefix arg, you will be prompted for the
|
|
428 coding system.
|
|
429 See also `write-region-pre-hook' and `write-region-post-hook'."
|
|
430 (interactive "r\nFWrite region to file: \ni\ni\ni\nZCoding-system: ")
|
|
431 (setq coding-system
|
|
432 (or (run-special-hook-with-args
|
|
433 'write-region-pre-hook start end filename append visit lockname)
|
|
434 coding-system
|
|
435 file-coding-system))
|
|
436 (if (consp coding-system)
|
|
437 coding-system
|
|
438 (let ((func
|
|
439 (coding-system-property coding-system 'pre-write-conversion)))
|
|
440 (if func
|
|
441 (let ((curbuf (current-buffer))
|
|
442 (tempbuf (generate-new-buffer " *temp-write-buffer*"))
|
|
443 (modif (buffer-modified-p)))
|
|
444 (unwind-protect
|
|
445 (save-excursion
|
|
446 (set-buffer tempbuf)
|
|
447 (erase-buffer)
|
|
448 (insert-buffer-substring curbuf start end)
|
|
449 (funcall func (point-min) (point-max))
|
|
450 (write-region-internal (point-min) (point-max) filename
|
|
451 append
|
|
452 (if (eq visit t) nil visit)
|
|
453 lockname
|
|
454 coding-system))
|
|
455 ;; leaving a buffer associated with file will cause problems
|
|
456 ;; when next visiting.
|
|
457 (kill-buffer tempbuf)
|
|
458 (if (or visit (null modif))
|
|
459 (progn
|
|
460 (set-buffer-auto-saved)
|
|
461 (set-buffer-modified-p nil)
|
|
462 (if (buffer-file-name) (set-visited-file-modtime))))))
|
|
463 (write-region-internal start end filename append visit lockname
|
|
464 coding-system)))
|
|
465 (run-hook-with-args 'write-region-post-hook
|
|
466 start end filename append visit lockname
|
|
467 coding-system)))
|
|
468
|
78
|
469 ;;; mule-files.el ends here
|