428
|
1 ;;; code-files.el --- File I/O functions for XEmacs.
|
|
2
|
|
3 ;; Copyright (C) 1992,93,94,95 Free Software Foundation, Inc.
|
|
4 ;; Copyright (C) 1995 Amdahl Corporation.
|
|
5 ;; Copyright (C) 1995 Sun Microsystems.
|
793
|
6 ;; Copyright (C) 2001, 2002 Ben Wing.
|
428
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs 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
|
444
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the
|
428
|
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
23 ;; Boston, MA 02111-1307, USA.
|
|
24
|
|
25 ;;; Synched up with: Not synched.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; Derived from mule.el in the original Mule but heavily modified
|
|
30 ;; by Ben Wing.
|
|
31
|
|
32 ;; 1997/3/11 modified by MORIOKA Tomohiko to sync with Emacs 20 API.
|
|
33
|
|
34 ;; This file was derived from the former mule-files.el which has been removed
|
|
35 ;; as of XEmacs 21.2.15.
|
|
36
|
|
37 ;;; Code:
|
|
38
|
|
39 (put 'buffer-file-coding-system 'permanent-local t)
|
|
40
|
771
|
41 (defvar buffer-file-coding-system-when-loaded nil
|
|
42 "Coding system used when current buffer's file was read in.
|
|
43
|
|
44 Automatically buffer-local when set in any fashion. This is set
|
|
45 automatically when a file is loaded and is used when the file needs to be
|
|
46 reloaded (e.g. `revert-buffer'). Normally this will have the same value as
|
|
47 `buffer-file-coding-system', but the latter may be changed because it's
|
|
48 also used to specify the encoding when the file is written out.")
|
|
49 (make-variable-buffer-local 'buffer-file-coding-system-when-loaded)
|
|
50 (put 'buffer-file-coding-system-when-loaded 'permanent-local t)
|
|
51
|
428
|
52 (define-obsolete-variable-alias
|
|
53 'file-coding-system
|
|
54 'buffer-file-coding-system)
|
|
55
|
|
56 (define-obsolete-variable-alias
|
|
57 'overriding-file-coding-system
|
|
58 'coding-system-for-read)
|
|
59
|
771
|
60 ;; NOTE: The real default value is set in code-init.el.
|
|
61 (defvar buffer-file-coding-system-for-read nil
|
|
62 "Default coding system used when reading a file.
|
428
|
63 This provides coarse-grained control; for finer-grained control, use
|
|
64 `file-coding-system-alist'. From a Lisp program, if you wish to
|
|
65 unilaterally specify the coding system used for one particular
|
|
66 operation, you should bind the variable `coding-system-for-read'
|
|
67 rather than setting this variable, which is intended to be used for
|
771
|
68 global environment specification.
|
|
69
|
|
70 See `insert-file-contents' for a full description of how a file's
|
|
71 coding system is determined when it is read in.")
|
428
|
72
|
|
73 (define-obsolete-variable-alias
|
|
74 'file-coding-system-for-read
|
|
75 'buffer-file-coding-system-for-read)
|
|
76
|
|
77 (defvar file-coding-system-alist
|
|
78 `(
|
|
79 ;; This must not be necessary, slb suggests -kkm
|
|
80 ;; ("loaddefs.el$" . (binary . binary))
|
|
81 ,@(mapcar
|
|
82 #'(lambda (regexp) (cons regexp 'binary)) binary-file-regexps)
|
|
83 ("TUTORIAL\\.\\(?:hr\\|pl\\|ro\\)\\'" . iso-8859-2)
|
|
84 ;; ("\\.\\(el\\|emacs\\|info\\(-[0-9]+\\)?\\|texi\\)$" . iso-2022-8)
|
|
85 ;; ("\\(ChangeLog\\|CHANGES-beta\\)$" . iso-2022-8)
|
442
|
86
|
|
87 ;; This idea is totally broken, and the code didn't work anyway.
|
|
88 ;; Mailboxes should be decoded by mail clients, who actually know
|
|
89 ;; how to deal with them. Otherwise, their contents should be
|
|
90 ;; treated as `binary'.
|
|
91 ;("/spool/mail/.*$" . convert-mbox-coding-system)
|
|
92 )
|
428
|
93 "Alist to decide a coding system to use for a file I/O operation.
|
|
94 The format is ((PATTERN . VAL) ...),
|
|
95 where PATTERN is a regular expression matching a file name,
|
|
96 VAL is a coding system, a cons of coding systems, or a function symbol.
|
|
97 If VAL is a coding system, it is used for both decoding and encoding
|
|
98 the file contents.
|
|
99 If VAL is a cons of coding systems, the car part is used for decoding,
|
|
100 and the cdr part is used for encoding.
|
|
101 If VAL is a function symbol, the function must return a coding system
|
|
102 or a cons of coding systems which are used as above.
|
|
103
|
|
104 This overrides the more general specification in
|
|
105 `buffer-file-coding-system-for-read', but is overridden by
|
|
106 `coding-system-for-read'.")
|
|
107
|
|
108 (defun set-buffer-file-coding-system (coding-system &optional force)
|
|
109 "Set buffer-file-coding-system of the current buffer to CODING-SYSTEM.
|
|
110 If optional argument FORCE (interactively, the prefix argument) is not
|
|
111 given, attempt to match the EOL type of the new coding system to
|
|
112 the current value of `buffer-file-coding-system'."
|
|
113 (interactive "zFile coding system: \nP")
|
|
114 (get-coding-system coding-system) ;; correctness check
|
|
115 (if (not force)
|
|
116 (setq coding-system
|
|
117 (subsidiary-coding-system
|
|
118 coding-system
|
|
119 (coding-system-eol-type buffer-file-coding-system))))
|
|
120 (setq buffer-file-coding-system coding-system)
|
|
121 (redraw-modeline t))
|
|
122
|
|
123 (defun toggle-buffer-file-coding-system ()
|
|
124 "Set EOL type of buffer-file-coding-system of the current buffer to
|
|
125 something other than what it is at the moment."
|
|
126 (interactive)
|
|
127 (let ((eol-type
|
|
128 (coding-system-eol-type buffer-file-coding-system)))
|
|
129 (setq buffer-file-coding-system
|
|
130 (subsidiary-coding-system
|
|
131 (coding-system-base buffer-file-coding-system)
|
|
132 (cond ((eq eol-type 'lf) 'crlf)
|
|
133 ((eq eol-type 'crlf) 'lf)
|
673
|
134 ((eq eol-type 'cr) 'lf))))
|
|
135 (set-buffer-modified-p t)))
|
428
|
136
|
|
137 (define-obsolete-function-alias
|
|
138 'set-file-coding-system
|
|
139 'set-buffer-file-coding-system)
|
|
140
|
|
141 (defun set-buffer-file-coding-system-for-read (coding-system)
|
|
142 "Set the coding system used when reading in a file.
|
|
143 This is equivalent to setting the variable
|
|
144 `buffer-file-coding-system-for-read'. You can also use
|
|
145 `file-coding-system-alist' to specify the coding system for
|
|
146 particular files."
|
|
147 (interactive "zFile coding system for read: ")
|
|
148 (get-coding-system coding-system) ;; correctness check
|
|
149 (setq buffer-file-coding-system-for-read coding-system))
|
|
150
|
|
151 (define-obsolete-function-alias
|
|
152 'set-file-coding-system-for-read
|
|
153 'set-buffer-file-coding-system-for-read)
|
|
154
|
|
155 (defun set-default-buffer-file-coding-system (coding-system)
|
|
156 "Set the default value of `buffer-file-coding-system' to CODING-SYSTEM.
|
|
157 The default value is used both for buffers without associated files
|
|
158 and for files with no apparent coding system (i.e. primarily ASCII).
|
|
159 See `buffer-file-coding-system' for more information."
|
|
160 (interactive "zDefault file coding system: ")
|
|
161 (setq-default buffer-file-coding-system coding-system)
|
|
162 (redraw-modeline t))
|
|
163
|
|
164 (define-obsolete-function-alias
|
|
165 'set-default-file-coding-system
|
|
166 'set-default-buffer-file-coding-system)
|
|
167
|
|
168 (defun find-file-coding-system-for-read-from-filename (filename)
|
|
169 "Look up coding system to read a file in `file-coding-system-alist'.
|
|
170 The return value will be nil (no applicable entry) or a coding system
|
|
171 object (the entry specified a coding system)."
|
|
172 (let ((alist file-coding-system-alist)
|
|
173 (found nil)
|
|
174 (codesys nil))
|
|
175 (let ((case-fold-search nil))
|
|
176 (setq filename (file-name-sans-versions filename))
|
|
177 (while (and (not found) alist)
|
|
178 (if (string-match (car (car alist)) filename)
|
|
179 (setq codesys (cdr (car alist))
|
|
180 found t))
|
|
181 (setq alist (cdr alist))))
|
|
182 (when codesys
|
|
183 (if (functionp codesys)
|
|
184 (setq codesys (funcall codesys 'insert-file-contents filename))
|
|
185 )
|
|
186 (cond ((consp codesys) (find-coding-system (car codesys)))
|
|
187 ((find-coding-system codesys))
|
|
188 ))))
|
|
189
|
|
190 (define-obsolete-function-alias
|
|
191 'find-file-coding-system-from-filename
|
|
192 'find-file-coding-system-for-read-from-filename)
|
|
193
|
|
194 (defun find-file-coding-system-for-write-from-filename (filename)
|
|
195 "Look up coding system to write a file in `file-coding-system-alist'.
|
|
196 The return value will be nil (no applicable entry) or a coding system
|
|
197 object (the entry specified a coding system)."
|
|
198 (let ((alist file-coding-system-alist)
|
|
199 (found nil)
|
|
200 (codesys nil))
|
|
201 (let ((case-fold-search nil))
|
|
202 (setq filename (file-name-sans-versions filename))
|
|
203 (while (and (not found) alist)
|
|
204 (if (string-match (car (car alist)) filename)
|
|
205 (setq codesys (cdr (car alist))
|
|
206 found t))
|
|
207 (setq alist (cdr alist))))
|
|
208 (when codesys
|
|
209 (if (functionp codesys)
|
|
210 (setq codesys (funcall codesys 'write-region filename))
|
|
211 )
|
|
212 (cond ((consp codesys) (find-coding-system (cdr codesys)))
|
|
213 ((find-coding-system codesys))
|
|
214 ))))
|
|
215
|
748
|
216 ;; This was completely broken, not only in implementation (does not
|
442
|
217 ;; understand MIME), but in concept -- such high-level decoding should
|
748
|
218 ;; be done by mail readers, not by IO code! Removed 2000-04-18.
|
442
|
219
|
748
|
220 ;(defun convert-mbox-coding-system (filename visit start end) ...)
|
428
|
221
|
|
222 (defun load (file &optional noerror nomessage nosuffix)
|
883
|
223 "Execute a file of Lisp code named FILE, or load a binary module.
|
|
224 First tries to find a Lisp FILE with .elc appended, then with .el, then with
|
|
225 FILE unmodified. If unsuccessful, tries to find a binary module FILE with
|
1733
|
226 the elements of `module-extensions' appended, one at a time.
|
|
227 Searches directories in load-path for Lisp files, and in `module-load-path'
|
883
|
228 for binary modules.
|
428
|
229 If optional second arg NOERROR is non-nil,
|
|
230 report no error if FILE doesn't exist.
|
|
231 Print messages at start and end of loading unless
|
|
232 optional third arg NOMESSAGE is non-nil.
|
1733
|
233 If optional fourth arg NOSUFFIX is non-nil, don't try adding suffixes
|
|
234 .elc, .el, or elements of `module-extensions' to the specified name FILE.
|
428
|
235 Return t if file exists."
|
1111
|
236 (declare (special load-modules-quietly))
|
428
|
237 (let* ((filename (substitute-in-file-name file))
|
|
238 (handler (find-file-name-handler filename 'load))
|
|
239 (path nil))
|
|
240 (if handler
|
|
241 (funcall handler 'load filename noerror nomessage nosuffix)
|
883
|
242 ;; First try to load a Lisp file
|
|
243 (if (and (> (length filename) 0)
|
|
244 (setq path (locate-file filename load-path
|
502
|
245 (and (not nosuffix)
|
883
|
246 '(".elc" ".el" "")))))
|
|
247 ;; now use the internal load to actually load the file.
|
|
248 (load-internal
|
|
249 file noerror nomessage nosuffix
|
|
250 (let ((elc ; use string= instead of string-match to keep match-data.
|
801
|
251 (equalp ".elc" (substring path -4))))
|
883
|
252 (or (and (not elc) coding-system-for-read) ;prefer for source file
|
|
253 ;; find magic-cookie
|
|
254 (let ((codesys
|
|
255 (find-coding-system-magic-cookie-in-file path)))
|
|
256 (when codesys
|
|
257 (setq codesys (intern codesys))
|
|
258 (if (find-coding-system codesys) codesys)))
|
|
259 (if elc
|
|
260 ;; if reading a byte-compiled file and we didn't find
|
|
261 ;; a coding-system magic cookie, then use `binary'.
|
|
262 ;; We need to guarantee that we never do autodetection
|
|
263 ;; on byte-compiled files because confusion here would
|
|
264 ;; be a very bad thing. Pre-existing byte-compiled
|
|
265 ;; files are always in the `binary' coding system.
|
|
266 ;; Also, byte-compiled files always use `lf' to terminate
|
|
267 ;; a line; don't risk confusion here either.
|
|
268 'binary
|
|
269 (or (find-file-coding-system-for-read-from-filename path)
|
|
270 ;; looking up in `file-coding-system-alist'.
|
|
271 ;; otherwise use `buffer-file-coding-system-for-read',
|
|
272 ;; as normal
|
|
273 buffer-file-coding-system-for-read)
|
|
274 ))))
|
|
275 ;; The file name is invalid, or we want to load a binary module
|
|
276 (if (and (> (length filename) 0)
|
996
|
277 (locate-file filename module-load-path
|
1733
|
278 (and (not nosuffix) module-extensions)))
|
883
|
279 (if (featurep 'modules)
|
|
280 (let ((load-modules-quietly nomessage))
|
1111
|
281 (declare-fboundp (load-module filename)))
|
883
|
282 (signal 'file-error '("This XEmacs does not support modules")))
|
|
283 (and (null noerror)
|
|
284 (signal 'file-error (list "Cannot open load file" filename))))
|
|
285 ))))
|
428
|
286
|
|
287 (defvar insert-file-contents-access-hook nil
|
|
288 "A hook to make a file accessible before reading it.
|
|
289 `insert-file-contents' calls this hook before doing anything else.
|
|
290 Called with two arguments: FILENAME and VISIT, the same as the
|
|
291 corresponding arguments in the call to `insert-file-contents'.")
|
|
292
|
|
293 (defvar insert-file-contents-pre-hook nil
|
|
294 "A special hook to decide the coding system used for reading in a file.
|
|
295
|
|
296 Before reading a file, `insert-file-contents' calls the functions on
|
|
297 this hook with arguments FILENAME and VISIT, the same as the
|
|
298 corresponding arguments in the call to `insert-file-contents'. In
|
|
299 these functions, you may refer to the global variable
|
|
300 `buffer-file-coding-system-for-read'.
|
|
301
|
|
302 The return value of the functions should be either
|
|
303
|
|
304 -- nil
|
|
305 -- A coding system or a symbol denoting it, indicating the coding system
|
|
306 to be used for reading the file
|
|
307 -- A list of two elements (absolute pathname and length of data inserted),
|
|
308 which is used as the return value to `insert-file-contents'. In this
|
|
309 case, `insert-file-contents' assumes that the function has inserted
|
|
310 the file for itself and suppresses further reading.
|
|
311
|
|
312 If any function returns non-nil, the remaining functions are not called.")
|
|
313
|
|
314 (defvar insert-file-contents-error-hook nil
|
|
315 "A hook to set `buffer-file-coding-system' when a read error has occurred.
|
|
316
|
|
317 When a file error (e.g. nonexistent file) occurs while read a file,
|
|
318 `insert-file-contents' calls the functions on this hook with three
|
|
319 arguments: FILENAME and VISIT (the same as the corresponding arguments
|
|
320 in the call to `insert-file-contents') and a cons (SIGNALED-CONDITIONS
|
|
321 . SIGNAL-DATA).
|
|
322
|
|
323 After calling this hook, the error is signalled for real and
|
|
324 propagates to the caller of `insert-file-contents'.")
|
|
325
|
|
326 (defvar insert-file-contents-post-hook nil
|
|
327 "A hook to set `buffer-file-coding-system' for the current buffer.
|
|
328
|
|
329 After successful reading, `insert-file-contents' calls the functions
|
|
330 on this hook with four arguments: FILENAME and VISIT (the same as the
|
|
331 corresponding arguments in the call to `insert-file-contents'),
|
|
332 CODING-SYSTEM (the actual coding system used to decode the file), and
|
|
333 a cons of absolute pathname and length of data inserted (the same
|
|
334 thing as will be returned from `insert-file-contents').")
|
|
335
|
444
|
336 (defun insert-file-contents (filename &optional visit start end replace)
|
428
|
337 "Insert contents of file FILENAME after point.
|
|
338 Returns list of absolute file name and length of data inserted.
|
|
339 If second argument VISIT is non-nil, the buffer's visited filename
|
|
340 and last save file modtime are set, and it is marked unmodified.
|
|
341 If visiting and the file does not exist, visiting is completed
|
|
342 before the error is signaled.
|
|
343
|
444
|
344 The optional third and fourth arguments START and END
|
428
|
345 specify what portion of the file to insert.
|
444
|
346 If VISIT is non-nil, START and END must be nil.
|
428
|
347 If optional fifth argument REPLACE is non-nil,
|
|
348 it means replace the current buffer contents (in the accessible portion)
|
|
349 with the file contents. This is better than simply deleting and inserting
|
|
350 the whole thing because (1) it preserves some marker positions
|
|
351 and (2) it puts less data in the undo list.
|
|
352
|
|
353 The coding system used for decoding the file is determined as follows:
|
|
354
|
771
|
355 1. `coding-system-for-read', if non-nil. (Intended as a temporary overriding
|
|
356 mechanism for use by Lisp code.)
|
|
357 2. The result of `insert-file-contents-pre-hook', if non-nil. (Intended for
|
|
358 handling tricky cases where the coding system of the file cannot be
|
|
359 determined just by looking at the filename's extension and the standard
|
|
360 auto-detection mechanism isn't suitable, so more clever code is required.
|
|
361 In general, this hook should rarely be used.)
|
|
362 3. The matching value for this filename from `file-coding-system-alist',
|
|
363 if any. (Intended as the standard way of determining encoding from
|
|
364 the name, or esp. the extension, of the file. Akin to the way
|
|
365 file-name extensions are used under MS Windows to determine how to
|
|
366 handle the file, but more flexible.)
|
|
367 4. `buffer-file-coding-system-for-read', if non-nil. (Intended to be where
|
|
368 the global default coding system is set. Usually, you want to use
|
|
369 the value `undecided', to let the system auto-detect according to the
|
|
370 priorities set up by `set-coding-priority-list'. This is usually
|
|
371 initialized from the `coding-system' property of the current language
|
|
372 environment.)
|
428
|
373 5. The coding system 'raw-text.
|
|
374
|
|
375 If a local value for `buffer-file-coding-system' in the current buffer
|
|
376 does not exist, it is set to the coding system which was actually used
|
|
377 for reading.
|
|
378
|
771
|
379 #### This should explain in more detail the exact workings of the
|
|
380 coding-system determination procedure.
|
|
381
|
428
|
382 See also `insert-file-contents-access-hook',
|
|
383 `insert-file-contents-pre-hook', `insert-file-contents-error-hook',
|
|
384 and `insert-file-contents-post-hook'."
|
|
385 (let (return-val coding-system used-codesys)
|
|
386 ;; OK, first load the file.
|
|
387 (condition-case err
|
|
388 (progn
|
|
389 (run-hook-with-args 'insert-file-contents-access-hook
|
|
390 filename visit)
|
|
391 ;; determine the coding system to use, as described above.
|
|
392 (setq coding-system
|
|
393 (or
|
|
394 ;; #1.
|
|
395 coding-system-for-read
|
|
396 ;; #2.
|
|
397 (run-hook-with-args-until-success
|
|
398 'insert-file-contents-pre-hook
|
|
399 filename visit)
|
|
400 ;; #3.
|
|
401 (find-file-coding-system-for-read-from-filename filename)
|
|
402 ;; #4.
|
|
403 buffer-file-coding-system-for-read
|
|
404 ;; #5.
|
|
405 'raw-text))
|
|
406 (if (consp coding-system)
|
|
407 (setq return-val coding-system)
|
|
408 (if (null (find-coding-system coding-system))
|
|
409 (progn
|
793
|
410 (lwarn 'coding-system 'notice
|
428
|
411 "Invalid coding-system (%s), using 'undecided"
|
|
412 coding-system)
|
|
413 (setq coding-system 'undecided)))
|
|
414 (setq return-val
|
444
|
415 (insert-file-contents-internal filename visit start end
|
428
|
416 replace coding-system
|
|
417 ;; store here!
|
|
418 'used-codesys))
|
|
419 ))
|
|
420 (file-error
|
|
421 (run-hook-with-args 'insert-file-contents-error-hook
|
|
422 filename visit err)
|
|
423 (signal (car err) (cdr err))))
|
|
424 (setq coding-system used-codesys)
|
|
425 ;; call any `post-read-conversion' for the coding system that
|
|
426 ;; was used ...
|
|
427 (let ((func
|
|
428 (coding-system-property coding-system 'post-read-conversion))
|
|
429 (endmark (make-marker)))
|
|
430 (set-marker endmark (+ (point) (nth 1 return-val)))
|
|
431 (if func
|
|
432 (unwind-protect
|
|
433 (save-excursion
|
|
434 (let (buffer-read-only)
|
771
|
435 (if (>= (function-max-args func) 2)
|
|
436 ;; #### fuckme! Someone at FSF changed the calling
|
|
437 ;; convention of post-read-conversion. We try to
|
|
438 ;; support the old way. #### Should we kill this?
|
|
439 (funcall func (point) (marker-position endmark))
|
|
440 (funcall func (- (marker-position endmark) (point))))))
|
428
|
441 (if visit
|
|
442 (progn
|
|
443 (set-buffer-auto-saved)
|
|
444 (set-buffer-modified-p nil)))))
|
|
445 (setcar (cdr return-val) (- (marker-position endmark) (point))))
|
771
|
446 ;; now finally set the buffer's `buffer-file-coding-system' ...
|
428
|
447 (if (run-hook-with-args-until-success 'insert-file-contents-post-hook
|
|
448 filename visit return-val)
|
|
449 nil
|
|
450 (if (local-variable-p 'buffer-file-coding-system (current-buffer))
|
|
451 ;; if buffer-file-coding-system is already local, just
|
|
452 ;; set its eol type to what was found, if it wasn't
|
|
453 ;; set already.
|
|
454 (set-buffer-file-coding-system
|
|
455 (subsidiary-coding-system buffer-file-coding-system
|
1699
|
456 (coding-system-eol-type coding-system)) t)
|
428
|
457 ;; otherwise actually set buffer-file-coding-system.
|
1699
|
458 (set-buffer-file-coding-system coding-system t)))
|
771
|
459 ;; ... and `buffer-file-coding-system-when-loaded'. the machinations
|
|
460 ;; of set-buffer-file-coding-system cause the actual coding system
|
|
461 ;; object to be stored, so do that here, too.
|
|
462 (setq buffer-file-coding-system-when-loaded
|
|
463 (get-coding-system coding-system))
|
428
|
464 return-val))
|
|
465
|
|
466 (defvar write-region-pre-hook nil
|
|
467 "A special hook to decide the coding system used for writing out a file.
|
|
468
|
748
|
469 Before writing a file, `write-region' calls the functions on this hook with
|
863
|
470 arguments START, END, FILENAME, APPEND, VISIT, LOCKNAME and CODING-SYSTEM,
|
748
|
471 the same as the corresponding arguments in the call to `write-region'.
|
428
|
472
|
748
|
473 The return value of each function should be one of
|
428
|
474
|
|
475 -- nil
|
|
476 -- A coding system or a symbol denoting it, indicating the coding system
|
863
|
477 to be used for writing the file
|
428
|
478 -- A list of two elements (absolute pathname and length of data written),
|
863
|
479 which is used as the return value to `write-region'. In this case,
|
|
480 `write-region' assumes that the function has written the file and
|
|
481 returns.
|
428
|
482
|
|
483 If any function returns non-nil, the remaining functions are not called.")
|
|
484
|
|
485 (defvar write-region-post-hook nil
|
|
486 "A hook called by `write-region' after a file has been written out.
|
|
487
|
|
488 The functions on this hook are called with arguments START, END,
|
748
|
489 FILENAME, APPEND, VISIT, LOCKNAME, and CODING-SYSTEM, the same as the
|
428
|
490 corresponding arguments in the call to `write-region'.")
|
|
491
|
863
|
492 (defun write-region (start end filename
|
|
493 &optional append visit lockname coding-system)
|
428
|
494 "Write current region into specified file.
|
|
495 By default the file's existing contents are replaced by the specified region.
|
863
|
496 Called interactively, prompts for a file name. With a prefix arg, prompts
|
|
497 for a coding system as well.
|
748
|
498
|
|
499 When called from a program, takes three required arguments:
|
428
|
500 START, END and FILENAME. START and END are buffer positions.
|
|
501 Optional fourth argument APPEND if non-nil means
|
|
502 append to existing file contents (if any).
|
|
503 Optional fifth argument VISIT if t means
|
|
504 set last-save-file-modtime of buffer to this file's modtime
|
|
505 and mark buffer not modified.
|
|
506 If VISIT is a string, it is a second file name;
|
|
507 the output goes to FILENAME, but the buffer is marked as visiting VISIT.
|
|
508 VISIT is also the file name to lock and unlock for clash detection.
|
|
509 If VISIT is neither t nor nil nor a string,
|
|
510 that means do not print the \"Wrote file\" message.
|
|
511 The optional sixth arg LOCKNAME, if non-nil, specifies the name to
|
|
512 use for locking and unlocking, overriding FILENAME and VISIT.
|
|
513 Kludgy feature: if START is a string, then that string is written
|
|
514 to the file, instead of any buffer contents, and END is ignored.
|
|
515 Optional seventh argument CODING-SYSTEM specifies the coding system
|
|
516 used to encode the text when it is written out, and defaults to
|
|
517 the value of `buffer-file-coding-system' in the current buffer.
|
|
518 See also `write-region-pre-hook' and `write-region-post-hook'."
|
|
519 (interactive "r\nFWrite region to file: \ni\ni\ni\nZCoding-system: ")
|
|
520 (setq coding-system
|
|
521 (or coding-system-for-write
|
|
522 (run-hook-with-args-until-success
|
748
|
523 'write-region-pre-hook
|
|
524 start end filename append visit lockname coding-system)
|
428
|
525 coding-system
|
|
526 buffer-file-coding-system
|
|
527 (find-file-coding-system-for-write-from-filename filename)
|
|
528 ))
|
|
529 (if (consp coding-system)
|
748
|
530 ;; One of the `write-region-pre-hook' functions wrote the file
|
428
|
531 coding-system
|
|
532 (let ((func
|
|
533 (coding-system-property coding-system 'pre-write-conversion)))
|
|
534 (if func
|
|
535 (let ((curbuf (current-buffer))
|
|
536 (tempbuf (generate-new-buffer " *temp-write-buffer*"))
|
|
537 (modif (buffer-modified-p)))
|
|
538 (unwind-protect
|
|
539 (save-excursion
|
|
540 (set-buffer tempbuf)
|
|
541 (erase-buffer)
|
|
542 (insert-buffer-substring curbuf start end)
|
|
543 (funcall func (point-min) (point-max))
|
|
544 (write-region-internal (point-min) (point-max) filename
|
|
545 append
|
|
546 (if (eq visit t) nil visit)
|
|
547 lockname
|
|
548 coding-system))
|
|
549 ;; leaving a buffer associated with file will cause problems
|
|
550 ;; when next visiting.
|
|
551 (kill-buffer tempbuf)
|
|
552 (if (or visit (null modif))
|
|
553 (progn
|
|
554 (set-buffer-auto-saved)
|
|
555 (set-buffer-modified-p nil)
|
|
556 (if (buffer-file-name) (set-visited-file-modtime))))))
|
|
557 (write-region-internal start end filename append visit lockname
|
|
558 coding-system)))
|
|
559 (run-hook-with-args 'write-region-post-hook
|
|
560 start end filename append visit lockname
|
|
561 coding-system)))
|
|
562
|
|
563 ;;; code-files.el ends here
|