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
|
3950
|
108 (defun set-buffer-file-coding-system (coding-system &optional force nomodify)
|
|
109 "Set the file coding-system of the current buffer to CODING-SYSTEM.
|
|
110 This means that when you save the buffer, it will be converted
|
|
111 according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
|
|
112 use \\[list-coding-systems].
|
|
113
|
|
114 If CODING-SYSTEM leaves the text conversion unspecified, or if it
|
|
115 leaves the end-of-line conversion unspecified, FORCE controls what to
|
|
116 do. If FORCE is nil, get the unspecified aspect (or aspects) from the
|
|
117 buffer's previous `buffer-file-coding-system' value (if it is
|
|
118 specified there). Otherwise, leave it unspecified.
|
|
119
|
|
120 This marks the buffer modified so that the succeeding \\[save-buffer]
|
|
121 surely saves the buffer with CODING-SYSTEM. From a program, if you
|
|
122 don't want to mark the buffer modified, specify t for NOMODIFY.
|
|
123 If you know exactly what coding system you want to use,
|
|
124 just set the variable `buffer-file-coding-system' directly."
|
|
125 (interactive "zCoding system for saving file (default nil): \nP")
|
|
126 (check-coding-system coding-system)
|
|
127 (if (and coding-system buffer-file-coding-system (null force))
|
|
128 (setq coding-system
|
428
|
129 (subsidiary-coding-system
|
|
130 coding-system
|
|
131 (coding-system-eol-type buffer-file-coding-system))))
|
3950
|
132 (setq buffer-file-coding-system coding-system)
|
|
133 ;; XEmacs change; remove a call to ucs-set-table-for-input, which we don't
|
|
134 ;; have.
|
|
135 (unless nomodify
|
|
136 (set-buffer-modified-p t))
|
|
137 (force-mode-line-update))
|
428
|
138
|
|
139 (defun toggle-buffer-file-coding-system ()
|
|
140 "Set EOL type of buffer-file-coding-system of the current buffer to
|
|
141 something other than what it is at the moment."
|
|
142 (interactive)
|
|
143 (let ((eol-type
|
|
144 (coding-system-eol-type buffer-file-coding-system)))
|
|
145 (setq buffer-file-coding-system
|
|
146 (subsidiary-coding-system
|
|
147 (coding-system-base buffer-file-coding-system)
|
|
148 (cond ((eq eol-type 'lf) 'crlf)
|
|
149 ((eq eol-type 'crlf) 'lf)
|
673
|
150 ((eq eol-type 'cr) 'lf))))
|
|
151 (set-buffer-modified-p t)))
|
428
|
152
|
|
153 (define-obsolete-function-alias
|
|
154 'set-file-coding-system
|
|
155 'set-buffer-file-coding-system)
|
|
156
|
|
157 (defun set-buffer-file-coding-system-for-read (coding-system)
|
|
158 "Set the coding system used when reading in a file.
|
|
159 This is equivalent to setting the variable
|
|
160 `buffer-file-coding-system-for-read'. You can also use
|
|
161 `file-coding-system-alist' to specify the coding system for
|
|
162 particular files."
|
|
163 (interactive "zFile coding system for read: ")
|
|
164 (get-coding-system coding-system) ;; correctness check
|
|
165 (setq buffer-file-coding-system-for-read coding-system))
|
|
166
|
|
167 (define-obsolete-function-alias
|
|
168 'set-file-coding-system-for-read
|
|
169 'set-buffer-file-coding-system-for-read)
|
|
170
|
|
171 (defun set-default-buffer-file-coding-system (coding-system)
|
|
172 "Set the default value of `buffer-file-coding-system' to CODING-SYSTEM.
|
|
173 The default value is used both for buffers without associated files
|
|
174 and for files with no apparent coding system (i.e. primarily ASCII).
|
|
175 See `buffer-file-coding-system' for more information."
|
|
176 (interactive "zDefault file coding system: ")
|
|
177 (setq-default buffer-file-coding-system coding-system)
|
|
178 (redraw-modeline t))
|
|
179
|
|
180 (define-obsolete-function-alias
|
|
181 'set-default-file-coding-system
|
|
182 'set-default-buffer-file-coding-system)
|
|
183
|
|
184 (defun find-file-coding-system-for-read-from-filename (filename)
|
|
185 "Look up coding system to read a file in `file-coding-system-alist'.
|
|
186 The return value will be nil (no applicable entry) or a coding system
|
|
187 object (the entry specified a coding system)."
|
|
188 (let ((alist file-coding-system-alist)
|
|
189 (found nil)
|
|
190 (codesys nil))
|
|
191 (let ((case-fold-search nil))
|
|
192 (setq filename (file-name-sans-versions filename))
|
|
193 (while (and (not found) alist)
|
|
194 (if (string-match (car (car alist)) filename)
|
|
195 (setq codesys (cdr (car alist))
|
|
196 found t))
|
|
197 (setq alist (cdr alist))))
|
|
198 (when codesys
|
|
199 (if (functionp codesys)
|
|
200 (setq codesys (funcall codesys 'insert-file-contents filename))
|
|
201 )
|
|
202 (cond ((consp codesys) (find-coding-system (car codesys)))
|
|
203 ((find-coding-system codesys))
|
|
204 ))))
|
|
205
|
|
206 (define-obsolete-function-alias
|
|
207 'find-file-coding-system-from-filename
|
|
208 'find-file-coding-system-for-read-from-filename)
|
|
209
|
|
210 (defun find-file-coding-system-for-write-from-filename (filename)
|
|
211 "Look up coding system to write a file in `file-coding-system-alist'.
|
|
212 The return value will be nil (no applicable entry) or a coding system
|
|
213 object (the entry specified a coding system)."
|
|
214 (let ((alist file-coding-system-alist)
|
|
215 (found nil)
|
|
216 (codesys nil))
|
|
217 (let ((case-fold-search nil))
|
|
218 (setq filename (file-name-sans-versions filename))
|
|
219 (while (and (not found) alist)
|
|
220 (if (string-match (car (car alist)) filename)
|
|
221 (setq codesys (cdr (car alist))
|
|
222 found t))
|
|
223 (setq alist (cdr alist))))
|
|
224 (when codesys
|
|
225 (if (functionp codesys)
|
|
226 (setq codesys (funcall codesys 'write-region filename))
|
|
227 )
|
|
228 (cond ((consp codesys) (find-coding-system (cdr codesys)))
|
|
229 ((find-coding-system codesys))
|
|
230 ))))
|
|
231
|
748
|
232 ;; This was completely broken, not only in implementation (does not
|
442
|
233 ;; understand MIME), but in concept -- such high-level decoding should
|
748
|
234 ;; be done by mail readers, not by IO code! Removed 2000-04-18.
|
442
|
235
|
748
|
236 ;(defun convert-mbox-coding-system (filename visit start end) ...)
|
428
|
237
|
|
238 (defun load (file &optional noerror nomessage nosuffix)
|
883
|
239 "Execute a file of Lisp code named FILE, or load a binary module.
|
|
240 First tries to find a Lisp FILE with .elc appended, then with .el, then with
|
|
241 FILE unmodified. If unsuccessful, tries to find a binary module FILE with
|
1733
|
242 the elements of `module-extensions' appended, one at a time.
|
|
243 Searches directories in load-path for Lisp files, and in `module-load-path'
|
883
|
244 for binary modules.
|
428
|
245 If optional second arg NOERROR is non-nil,
|
|
246 report no error if FILE doesn't exist.
|
|
247 Print messages at start and end of loading unless
|
|
248 optional third arg NOMESSAGE is non-nil.
|
1733
|
249 If optional fourth arg NOSUFFIX is non-nil, don't try adding suffixes
|
|
250 .elc, .el, or elements of `module-extensions' to the specified name FILE.
|
428
|
251 Return t if file exists."
|
1111
|
252 (declare (special load-modules-quietly))
|
428
|
253 (let* ((filename (substitute-in-file-name file))
|
|
254 (handler (find-file-name-handler filename 'load))
|
|
255 (path nil))
|
|
256 (if handler
|
|
257 (funcall handler 'load filename noerror nomessage nosuffix)
|
883
|
258 ;; First try to load a Lisp file
|
|
259 (if (and (> (length filename) 0)
|
|
260 (setq path (locate-file filename load-path
|
502
|
261 (and (not nosuffix)
|
883
|
262 '(".elc" ".el" "")))))
|
|
263 ;; now use the internal load to actually load the file.
|
|
264 (load-internal
|
|
265 file noerror nomessage nosuffix
|
|
266 (let ((elc ; use string= instead of string-match to keep match-data.
|
801
|
267 (equalp ".elc" (substring path -4))))
|
883
|
268 (or (and (not elc) coding-system-for-read) ;prefer for source file
|
|
269 ;; find magic-cookie
|
|
270 (let ((codesys
|
|
271 (find-coding-system-magic-cookie-in-file path)))
|
|
272 (when codesys
|
|
273 (setq codesys (intern codesys))
|
|
274 (if (find-coding-system codesys) codesys)))
|
|
275 (if elc
|
|
276 ;; if reading a byte-compiled file and we didn't find
|
|
277 ;; a coding-system magic cookie, then use `binary'.
|
|
278 ;; We need to guarantee that we never do autodetection
|
|
279 ;; on byte-compiled files because confusion here would
|
|
280 ;; be a very bad thing. Pre-existing byte-compiled
|
|
281 ;; files are always in the `binary' coding system.
|
|
282 ;; Also, byte-compiled files always use `lf' to terminate
|
|
283 ;; a line; don't risk confusion here either.
|
|
284 'binary
|
|
285 (or (find-file-coding-system-for-read-from-filename path)
|
|
286 ;; looking up in `file-coding-system-alist'.
|
|
287 ;; otherwise use `buffer-file-coding-system-for-read',
|
|
288 ;; as normal
|
|
289 buffer-file-coding-system-for-read)
|
|
290 ))))
|
|
291 ;; The file name is invalid, or we want to load a binary module
|
|
292 (if (and (> (length filename) 0)
|
996
|
293 (locate-file filename module-load-path
|
1733
|
294 (and (not nosuffix) module-extensions)))
|
883
|
295 (if (featurep 'modules)
|
|
296 (let ((load-modules-quietly nomessage))
|
1111
|
297 (declare-fboundp (load-module filename)))
|
883
|
298 (signal 'file-error '("This XEmacs does not support modules")))
|
|
299 (and (null noerror)
|
|
300 (signal 'file-error (list "Cannot open load file" filename))))
|
|
301 ))))
|
428
|
302
|
|
303 (defvar insert-file-contents-access-hook nil
|
|
304 "A hook to make a file accessible before reading it.
|
|
305 `insert-file-contents' calls this hook before doing anything else.
|
|
306 Called with two arguments: FILENAME and VISIT, the same as the
|
|
307 corresponding arguments in the call to `insert-file-contents'.")
|
|
308
|
|
309 (defvar insert-file-contents-pre-hook nil
|
|
310 "A special hook to decide the coding system used for reading in a file.
|
|
311
|
|
312 Before reading a file, `insert-file-contents' calls the functions on
|
|
313 this hook with arguments FILENAME and VISIT, the same as the
|
|
314 corresponding arguments in the call to `insert-file-contents'. In
|
|
315 these functions, you may refer to the global variable
|
|
316 `buffer-file-coding-system-for-read'.
|
|
317
|
|
318 The return value of the functions should be either
|
|
319
|
|
320 -- nil
|
|
321 -- A coding system or a symbol denoting it, indicating the coding system
|
|
322 to be used for reading the file
|
|
323 -- A list of two elements (absolute pathname and length of data inserted),
|
|
324 which is used as the return value to `insert-file-contents'. In this
|
|
325 case, `insert-file-contents' assumes that the function has inserted
|
|
326 the file for itself and suppresses further reading.
|
|
327
|
|
328 If any function returns non-nil, the remaining functions are not called.")
|
|
329
|
|
330 (defvar insert-file-contents-error-hook nil
|
|
331 "A hook to set `buffer-file-coding-system' when a read error has occurred.
|
|
332
|
|
333 When a file error (e.g. nonexistent file) occurs while read a file,
|
|
334 `insert-file-contents' calls the functions on this hook with three
|
|
335 arguments: FILENAME and VISIT (the same as the corresponding arguments
|
|
336 in the call to `insert-file-contents') and a cons (SIGNALED-CONDITIONS
|
|
337 . SIGNAL-DATA).
|
|
338
|
|
339 After calling this hook, the error is signalled for real and
|
|
340 propagates to the caller of `insert-file-contents'.")
|
|
341
|
|
342 (defvar insert-file-contents-post-hook nil
|
|
343 "A hook to set `buffer-file-coding-system' for the current buffer.
|
|
344
|
|
345 After successful reading, `insert-file-contents' calls the functions
|
|
346 on this hook with four arguments: FILENAME and VISIT (the same as the
|
|
347 corresponding arguments in the call to `insert-file-contents'),
|
|
348 CODING-SYSTEM (the actual coding system used to decode the file), and
|
|
349 a cons of absolute pathname and length of data inserted (the same
|
|
350 thing as will be returned from `insert-file-contents').")
|
|
351
|
444
|
352 (defun insert-file-contents (filename &optional visit start end replace)
|
428
|
353 "Insert contents of file FILENAME after point.
|
|
354 Returns list of absolute file name and length of data inserted.
|
|
355 If second argument VISIT is non-nil, the buffer's visited filename
|
|
356 and last save file modtime are set, and it is marked unmodified.
|
|
357 If visiting and the file does not exist, visiting is completed
|
|
358 before the error is signaled.
|
|
359
|
4205
|
360 The optional third and fourth arguments START and END specify what portion
|
|
361 of the file to insert, and start at zero, in direct and needless contrast to
|
|
362 buffer offsets. That is, values of 0 and 10 for START and END respectively
|
|
363 will give the first ten octets of a file.
|
|
364
|
444
|
365 If VISIT is non-nil, START and END must be nil.
|
428
|
366 If optional fifth argument REPLACE is non-nil,
|
|
367 it means replace the current buffer contents (in the accessible portion)
|
|
368 with the file contents. This is better than simply deleting and inserting
|
|
369 the whole thing because (1) it preserves some marker positions
|
|
370 and (2) it puts less data in the undo list.
|
|
371
|
|
372 The coding system used for decoding the file is determined as follows:
|
|
373
|
771
|
374 1. `coding-system-for-read', if non-nil. (Intended as a temporary overriding
|
|
375 mechanism for use by Lisp code.)
|
|
376 2. The result of `insert-file-contents-pre-hook', if non-nil. (Intended for
|
|
377 handling tricky cases where the coding system of the file cannot be
|
|
378 determined just by looking at the filename's extension and the standard
|
|
379 auto-detection mechanism isn't suitable, so more clever code is required.
|
|
380 In general, this hook should rarely be used.)
|
|
381 3. The matching value for this filename from `file-coding-system-alist',
|
|
382 if any. (Intended as the standard way of determining encoding from
|
|
383 the name, or esp. the extension, of the file. Akin to the way
|
|
384 file-name extensions are used under MS Windows to determine how to
|
|
385 handle the file, but more flexible.)
|
|
386 4. `buffer-file-coding-system-for-read', if non-nil. (Intended to be where
|
|
387 the global default coding system is set. Usually, you want to use
|
|
388 the value `undecided', to let the system auto-detect according to the
|
|
389 priorities set up by `set-coding-priority-list'. This is usually
|
|
390 initialized from the `coding-system' property of the current language
|
|
391 environment.)
|
428
|
392 5. The coding system 'raw-text.
|
|
393
|
|
394 If a local value for `buffer-file-coding-system' in the current buffer
|
|
395 does not exist, it is set to the coding system which was actually used
|
|
396 for reading.
|
|
397
|
771
|
398 #### This should explain in more detail the exact workings of the
|
|
399 coding-system determination procedure.
|
|
400
|
428
|
401 See also `insert-file-contents-access-hook',
|
|
402 `insert-file-contents-pre-hook', `insert-file-contents-error-hook',
|
|
403 and `insert-file-contents-post-hook'."
|
3722
|
404 (let* ((expanded (substitute-in-file-name filename))
|
|
405 (handler (find-file-name-handler expanded 'insert-file-contents)))
|
|
406 (if handler
|
|
407 (funcall handler 'insert-file-contents filename visit start end replace)
|
|
408 (let (return-val coding-system used-codesys)
|
|
409 ;; OK, first load the file.
|
|
410 (condition-case err
|
|
411 (progn
|
|
412 (run-hook-with-args 'insert-file-contents-access-hook
|
|
413 filename visit)
|
|
414 ;; determine the coding system to use, as described above.
|
|
415 (setq coding-system
|
|
416 (or
|
|
417 ;; #1.
|
|
418 coding-system-for-read
|
|
419 ;; #2.
|
|
420 (run-hook-with-args-until-success
|
|
421 'insert-file-contents-pre-hook
|
|
422 filename visit)
|
|
423 ;; #3.
|
|
424 (find-file-coding-system-for-read-from-filename filename)
|
|
425 ;; #4.
|
|
426 buffer-file-coding-system-for-read
|
|
427 ;; #5.
|
|
428 'raw-text))
|
|
429 (if (consp coding-system)
|
|
430 (setq return-val coding-system)
|
|
431 (if (null (find-coding-system coding-system))
|
|
432 (progn
|
|
433 (lwarn 'coding-system 'notice
|
|
434 "Invalid coding-system (%s), using 'undecided"
|
|
435 coding-system)
|
|
436 (setq coding-system 'undecided)))
|
|
437 (setq return-val
|
|
438 (insert-file-contents-internal filename visit start end
|
|
439 replace coding-system
|
|
440 ;; store here!
|
|
441 'used-codesys))
|
|
442 ))
|
|
443 (file-error
|
|
444 (run-hook-with-args 'insert-file-contents-error-hook
|
|
445 filename visit err)
|
|
446 (signal (car err) (cdr err))))
|
|
447 (setq coding-system used-codesys)
|
|
448 ;; call any `post-read-conversion' for the coding system that
|
|
449 ;; was used ...
|
|
450 (let ((func
|
|
451 (coding-system-property coding-system 'post-read-conversion))
|
|
452 (endmark (make-marker)))
|
|
453 (set-marker endmark (+ (point) (nth 1 return-val)))
|
|
454 (if func
|
|
455 (unwind-protect
|
|
456 (save-excursion
|
|
457 (let (buffer-read-only)
|
|
458 (if (>= (function-max-args func) 2)
|
|
459 ;; #### fuckme! Someone at FSF changed the calling
|
|
460 ;; convention of post-read-conversion. We try to
|
|
461 ;; support the old way. #### Should we kill this?
|
|
462 (funcall func (point) (marker-position endmark))
|
|
463 (funcall func (- (marker-position endmark) (point))))))
|
|
464 (if visit
|
|
465 (progn
|
|
466 (set-buffer-auto-saved)
|
|
467 (set-buffer-modified-p nil)))))
|
|
468 (setcar (cdr return-val) (- (marker-position endmark) (point))))
|
|
469 ;; now finally set the buffer's `buffer-file-coding-system' ...
|
|
470 (if (run-hook-with-args-until-success 'insert-file-contents-post-hook
|
|
471 filename visit return-val)
|
|
472 nil
|
|
473 (if (local-variable-p 'buffer-file-coding-system (current-buffer))
|
|
474 ;; if buffer-file-coding-system is already local, just
|
|
475 ;; set its eol type to what was found, if it wasn't
|
|
476 ;; set already.
|
|
477 (set-buffer-file-coding-system
|
3950
|
478 (subsidiary-coding-system
|
|
479 buffer-file-coding-system
|
|
480 (coding-system-eol-type coding-system)) t t)
|
3722
|
481 ;; otherwise actually set buffer-file-coding-system.
|
3950
|
482 (set-buffer-file-coding-system coding-system t t)))
|
3722
|
483 ;; ... and `buffer-file-coding-system-when-loaded'. the machinations
|
|
484 ;; of set-buffer-file-coding-system cause the actual coding system
|
|
485 ;; object to be stored, so do that here, too.
|
|
486 (setq buffer-file-coding-system-when-loaded
|
|
487 (get-coding-system coding-system))
|
|
488 return-val))))
|
428
|
489
|
|
490 (defvar write-region-pre-hook nil
|
|
491 "A special hook to decide the coding system used for writing out a file.
|
|
492
|
748
|
493 Before writing a file, `write-region' calls the functions on this hook with
|
863
|
494 arguments START, END, FILENAME, APPEND, VISIT, LOCKNAME and CODING-SYSTEM,
|
748
|
495 the same as the corresponding arguments in the call to `write-region'.
|
428
|
496
|
748
|
497 The return value of each function should be one of
|
428
|
498
|
|
499 -- nil
|
|
500 -- A coding system or a symbol denoting it, indicating the coding system
|
863
|
501 to be used for writing the file
|
428
|
502 -- A list of two elements (absolute pathname and length of data written),
|
863
|
503 which is used as the return value to `write-region'. In this case,
|
|
504 `write-region' assumes that the function has written the file and
|
|
505 returns.
|
428
|
506
|
|
507 If any function returns non-nil, the remaining functions are not called.")
|
|
508
|
|
509 (defvar write-region-post-hook nil
|
|
510 "A hook called by `write-region' after a file has been written out.
|
|
511
|
|
512 The functions on this hook are called with arguments START, END,
|
748
|
513 FILENAME, APPEND, VISIT, LOCKNAME, and CODING-SYSTEM, the same as the
|
428
|
514 corresponding arguments in the call to `write-region'.")
|
|
515
|
4308
|
516 (defun write-region (start end filename &optional append visit lockname
|
4266
|
517 coding-system-or-mustbenew)
|
428
|
518 "Write current region into specified file.
|
4308
|
519 Called interactively, prompts for a file name.
|
|
520 With a prefix arg, prompts for a coding system as well.
|
748
|
521
|
|
522 When called from a program, takes three required arguments:
|
428
|
523 START, END and FILENAME. START and END are buffer positions.
|
4308
|
524 APPEND, if non-nil, means append to existing file contents (if any), else
|
|
525 the file's existing contents are replaced by the specified region.
|
|
526 VISIT, if non-nil, should be a string naming a file. The buffer is marked
|
|
527 as visiting VISIT. VISIT is also the file name to lock
|
|
528 and unlock for clash detection.
|
|
529 LOCKNAME, if non-nil, specifies the name to use for locking and unlocking,
|
|
530 overriding FILENAME and VISIT.
|
|
531 CODING-SYSTEM-OR-MUSTBENEW specifies the coding system used to encode the
|
|
532 text written. It defaults to the value of `buffer-file-coding-system'
|
|
533 in the current buffer.
|
4266
|
534
|
4308
|
535 For compatibility with GNU Emacs, several arguments are overloaded:
|
|
536 START may be a string, which is written to the file. END is ignored.
|
|
537 VISIT may take the value t, meaning to set last-save-file-modtime of buffer
|
|
538 to this file's modtime and mark buffer not modified. With any other
|
|
539 non-nil value of VISIT, suppress printing of the \"Wrote file\" message.
|
|
540 CODING-SYSTEM-OR-MUSTBENEW may be a non-nil, non-coding-system value.
|
|
541 If it is `excl' and FILENAME already exists, signal `file-already-exists'.
|
|
542 Otherwise, if FILENAME already exists, ask for confirmation before
|
|
543 writing, and signal `file-already-exists' if not confirmed.
|
4266
|
544
|
428
|
545 See also `write-region-pre-hook' and `write-region-post-hook'."
|
|
546 (interactive "r\nFWrite region to file: \ni\ni\ni\nZCoding-system: ")
|
4266
|
547 (let (mustbenew coding-system func hook-result)
|
|
548 (setq hook-result
|
|
549 (or coding-system-for-write
|
|
550 (run-hook-with-args-until-success
|
|
551 'write-region-pre-hook
|
|
552 start end filename append visit lockname
|
|
553 coding-system-or-mustbenew)
|
4271
|
554 (if (and coding-system-or-mustbenew
|
|
555 (coding-system-p
|
|
556 (find-coding-system coding-system-or-mustbenew)))
|
|
557 coding-system-or-mustbenew)
|
4266
|
558 buffer-file-coding-system
|
|
559 (find-file-coding-system-for-write-from-filename filename)))
|
|
560 (if (consp hook-result)
|
|
561 ;; One of the `write-region-pre-hook' functions wrote the file.
|
|
562 hook-result
|
|
563 ;; The hooks didn't do the work; do it ourselves.
|
4271
|
564 (setq hook-result (find-coding-system hook-result)
|
|
565 mustbenew (unless (coding-system-p
|
|
566 (find-coding-system coding-system-or-mustbenew))
|
4266
|
567 coding-system-or-mustbenew)
|
|
568 coding-system (cond ((coding-system-p hook-result) hook-result)
|
|
569 ((null mustbenew) coding-system-or-mustbenew))
|
|
570 func (coding-system-property coding-system 'pre-write-conversion))
|
428
|
571 (if func
|
|
572 (let ((curbuf (current-buffer))
|
|
573 (tempbuf (generate-new-buffer " *temp-write-buffer*"))
|
|
574 (modif (buffer-modified-p)))
|
|
575 (unwind-protect
|
|
576 (save-excursion
|
|
577 (set-buffer tempbuf)
|
|
578 (erase-buffer)
|
|
579 (insert-buffer-substring curbuf start end)
|
|
580 (funcall func (point-min) (point-max))
|
|
581 (write-region-internal (point-min) (point-max) filename
|
|
582 append
|
|
583 (if (eq visit t) nil visit)
|
|
584 lockname
|
4266
|
585 coding-system
|
|
586 mustbenew))
|
428
|
587 ;; leaving a buffer associated with file will cause problems
|
|
588 ;; when next visiting.
|
|
589 (kill-buffer tempbuf)
|
|
590 (if (or visit (null modif))
|
|
591 (progn
|
|
592 (set-buffer-auto-saved)
|
|
593 (set-buffer-modified-p nil)
|
|
594 (if (buffer-file-name) (set-visited-file-modtime))))))
|
|
595 (write-region-internal start end filename append visit lockname
|
4266
|
596 coding-system mustbenew)))
|
428
|
597 (run-hook-with-args 'write-region-post-hook
|
|
598 start end filename append visit lockname
|
|
599 coding-system)))
|
|
600
|
|
601 ;;; code-files.el ends here
|