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