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