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