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