0
|
1 ;;; files.el --- file input and output commands for XEmacs.
|
|
2
|
|
3 ;; Copyright (C) 1985-1987, 1992-1995 Free Software Foundation, Inc.
|
|
4 ;; Copyright (C) 1995 Sun Microsystems.
|
|
5
|
|
6 ;; This file is part of XEmacs.
|
|
7
|
|
8 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
9 ;; under the terms of the GNU General Public License as published by
|
|
10 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
11 ;; any later version.
|
|
12
|
|
13 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 ;; General Public License for more details.
|
|
17
|
|
18 ;; You should have received a copy of the GNU General Public License
|
|
19 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
20 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
21
|
|
22 ;;; Synched up with: FSF 19.30.
|
|
23 ;;; Warning: Merging this file is tough. Beware.
|
|
24
|
|
25 ;;; Commentary:
|
|
26
|
|
27 ;; Defines most of XEmacs's file- and directory-handling functions,
|
|
28 ;; including basic file visiting, backup generation, link handling,
|
|
29 ;; ITS-id version control, load- and write-hook handling, and the like.
|
|
30
|
|
31 ;;; Code:
|
|
32
|
|
33 ;; Avoid compilation warnings.
|
|
34 (defvar overriding-file-coding-system)
|
|
35 (defvar file-coding-system)
|
|
36
|
|
37 ;; In buffer.c
|
|
38 ;(defconst delete-auto-save-files t
|
|
39 ; "*Non-nil means delete auto-save file when a buffer is saved or killed.")
|
|
40
|
|
41 ;;; Turn off backup files on VMS since it has version numbers.
|
|
42 (defconst make-backup-files (not (eq system-type 'vax-vms))
|
|
43 "*Non-nil means make a backup of a file the first time it is saved.
|
|
44 This can be done by renaming the file or by copying.
|
|
45
|
|
46 Renaming means that XEmacs renames the existing file so that it is a
|
|
47 backup file, then writes the buffer into a new file. Any other names
|
|
48 that the old file had will now refer to the backup file. The new file
|
|
49 is owned by you and its group is defaulted.
|
|
50
|
|
51 Copying means that XEmacs copies the existing file into the backup
|
|
52 file, then writes the buffer on top of the existing file. Any other
|
|
53 names that the old file had will now refer to the new (edited) file.
|
|
54 The file's owner and group are unchanged.
|
|
55
|
|
56 The choice of renaming or copying is controlled by the variables
|
|
57 `backup-by-copying', `backup-by-copying-when-linked' and
|
|
58 `backup-by-copying-when-mismatch'. See also `backup-inhibited'.")
|
|
59
|
|
60 ;; Do this so that local variables based on the file name
|
|
61 ;; are not overridden by the major mode.
|
|
62 (defvar backup-inhibited nil
|
|
63 "Non-nil means don't make a backup, regardless of the other parameters.
|
|
64 This variable is intended for use by making it local to a buffer.
|
|
65 But it is local only if you make it local.")
|
|
66 (put 'backup-inhibited 'permanent-local t)
|
|
67
|
|
68 (defconst backup-by-copying nil
|
|
69 "*Non-nil means always use copying to create backup files.
|
|
70 See documentation of variable `make-backup-files'.")
|
|
71
|
|
72 (defconst backup-by-copying-when-linked nil
|
|
73 "*Non-nil means use copying to create backups for files with multiple names.
|
|
74 This causes the alternate names to refer to the latest version as edited.
|
|
75 This variable is relevant only if `backup-by-copying' is nil.")
|
|
76
|
|
77 (defconst backup-by-copying-when-mismatch nil
|
|
78 "*Non-nil means create backups by copying if this preserves owner or group.
|
|
79 Renaming may still be used (subject to control of other variables)
|
|
80 when it would not result in changing the owner or group of the file;
|
|
81 that is, for files which are owned by you and whose group matches
|
|
82 the default for a new file created there by you.
|
|
83 This variable is relevant only if `backup-by-copying' is nil.")
|
|
84
|
|
85 (defvar backup-enable-predicate
|
|
86 '(lambda (name)
|
|
87 (or (< (length name) 5)
|
|
88 (not (string-equal "/tmp/" (substring name 0 5)))))
|
|
89 "Predicate that looks at a file name and decides whether to make backups.
|
|
90 Called with an absolute file name as argument, it returns t to enable backup.")
|
|
91
|
|
92 (defconst buffer-offer-save nil
|
|
93 "*Non-nil in a buffer means offer to save the buffer on exit
|
|
94 even if the buffer is not visiting a file.
|
|
95 Automatically local in all buffers.")
|
|
96 (make-variable-buffer-local 'buffer-offer-save)
|
|
97
|
|
98 (defvaralias 'find-file-visit-truename 'find-file-use-truenames)
|
|
99 (defvaralias 'find-file-existing-other-name 'find-file-compare-truenames)
|
|
100
|
|
101 (defvar buffer-file-number nil
|
|
102 "The device number and file number of the file visited in the current buffer.
|
|
103 The value is a list of the form (FILENUM DEVNUM).
|
|
104 This pair of numbers uniquely identifies the file.
|
|
105 If the buffer is visiting a new file, the value is nil.")
|
|
106 (make-variable-buffer-local 'buffer-file-number)
|
|
107 (put 'buffer-file-number 'permanent-local t)
|
|
108
|
|
109 (defconst file-precious-flag nil
|
|
110 "*Non-nil means protect against I/O errors while saving files.
|
|
111 Some modes set this non-nil in particular buffers.
|
|
112
|
|
113 This feature works by writing the new contents into a temporary file
|
|
114 and then renaming the temporary file to replace the original.
|
|
115 In this way, any I/O error in writing leaves the original untouched,
|
|
116 and there is never any instant where the file is nonexistent.
|
|
117
|
|
118 Note that this feature forces backups to be made by copying.
|
|
119 Yet, at the same time, saving a precious file
|
|
120 breaks any hard links between it and other files.")
|
|
121
|
|
122 (defvar version-control nil
|
|
123 "*Control use of version numbers for backup files.
|
|
124 t means make numeric backup versions unconditionally.
|
|
125 nil means make them for files that have some already.
|
|
126 `never' means do not make them.")
|
|
127
|
|
128 (defvar dired-kept-versions 2
|
|
129 "*When cleaning directory, number of versions to keep.")
|
|
130
|
|
131 (defvar delete-old-versions nil
|
|
132 "*If t, delete excess backup versions silently.
|
|
133 If nil, ask confirmation. Any other value prevents any trimming.")
|
|
134
|
|
135 (defvar kept-old-versions 2
|
|
136 "*Number of oldest versions to keep when a new numbered backup is made.")
|
|
137
|
|
138 (defvar kept-new-versions 2
|
|
139 "*Number of newest versions to keep when a new numbered backup is made.
|
|
140 Includes the new backup. Must be > 0")
|
|
141
|
|
142 (defconst require-final-newline nil
|
|
143 "*Value of t says silently ensure a file ends in a newline when it is saved.
|
|
144 Non-nil but not t says ask user whether to add a newline when there isn't one.
|
|
145 nil means don't add newlines.")
|
|
146
|
|
147 (defconst auto-save-default t
|
|
148 "*Non-nil says by default do auto-saving of every file-visiting buffer.")
|
|
149
|
|
150 (defconst auto-save-visited-file-name nil
|
|
151 "*Non-nil says auto-save a buffer in the file it is visiting, when practical.
|
|
152 Normally auto-save files are written under other names.")
|
|
153
|
|
154 (defconst save-abbrevs nil
|
|
155 "*Non-nil means save word abbrevs too when files are saved.
|
|
156 Loading an abbrev file sets this to t.")
|
|
157
|
|
158 (defconst find-file-run-dired t
|
|
159 "*Non-nil says run dired if `find-file' is given the name of a directory.")
|
|
160
|
|
161 ;;;It is not useful to make this a local variable.
|
|
162 ;;;(put 'find-file-not-found-hooks 'permanent-local t)
|
|
163 (defvar find-file-not-found-hooks nil
|
|
164 "List of functions to be called for `find-file' on nonexistent file.
|
|
165 These functions are called as soon as the error is detected.
|
|
166 `buffer-file-name' is already set up.
|
|
167 The functions are called in the order given until one of them returns non-nil.")
|
|
168
|
|
169 ;;;It is not useful to make this a local variable.
|
|
170 ;;;(put 'find-file-hooks 'permanent-local t)
|
|
171 (defvar find-file-hooks nil
|
|
172 "List of functions to be called after a buffer is loaded from a file.
|
|
173 The buffer's local variables (if any) will have been processed before the
|
|
174 functions are called.")
|
|
175
|
|
176 (defvar write-file-hooks nil
|
|
177 "List of functions to be called before writing out a buffer to a file.
|
|
178 If one of them returns non-nil, the file is considered already written
|
|
179 and the rest are not called.
|
|
180 These hooks are considered to pertain to the visited file.
|
|
181 So this list is cleared if you change the visited file name.
|
|
182 See also `write-contents-hooks' and `continue-save-buffer'.
|
|
183 Don't make this variable buffer-local; instead, use `local-write-file-hooks'.")
|
|
184 ;;; However, in case someone does make it local...
|
|
185 (put 'write-file-hooks 'permanent-local t)
|
|
186
|
|
187 (defvar local-write-file-hooks nil
|
|
188 "Just like `write-file-hooks', except intended for per-buffer use.
|
|
189 The functions in this list are called before the ones in
|
|
190 `write-file-hooks'.")
|
|
191 (make-variable-buffer-local 'local-write-file-hooks)
|
|
192 (put 'local-write-file-hooks 'permanent-local t)
|
|
193
|
|
194
|
|
195 ;; #### think about this (added by Sun).
|
|
196 (put 'after-set-visited-file-name-hooks 'permanent-local t)
|
|
197 (defvar after-set-visited-file-name-hooks nil
|
|
198 "List of functions to be called after \\[set-visited-file-name]
|
|
199 or during \\[write-file].
|
|
200 You can use this hook to restore local values of write-file-hooks,
|
|
201 after-save-hook, and revert-buffer-function, which pertain
|
|
202 to a specific file and therefore are normally killed by a rename.
|
|
203 Put hooks pertaining to the buffer contents on write-contents-hooks
|
|
204 and revert-buffer-insert-file-contents-function.")
|
|
205
|
|
206 (defvar write-contents-hooks nil
|
|
207 "List of functions to be called before writing out a buffer to a file.
|
|
208 If one of them returns non-nil, the file is considered already written
|
|
209 and the rest are not called.
|
|
210 These hooks are considered to pertain to the buffer's contents,
|
|
211 not to the particular visited file; thus, `set-visited-file-name' does
|
|
212 not clear this variable, but changing the major mode does clear it.
|
|
213 See also `write-file-hooks' and `continue-save-buffer'.")
|
|
214
|
|
215 ;; Not in FSF19
|
|
216 ;; Energize needed this to hook into save-buffer at a lower level; we need
|
|
217 ;; to provide a new output method, but don't want to have to duplicate all
|
|
218 ;; of the backup file and file modes logic.that does not occur if one uses
|
|
219 ;; a write-file-hook which returns non-nil.
|
|
220 (put 'write-file-data-hooks 'permanent-local t)
|
|
221 (defvar write-file-data-hooks nil
|
|
222 "List of functions to be called to put the bytes on disk.
|
|
223 These functions receive the name of the file to write to as argument.
|
|
224 The default behavior is to call
|
|
225 (write-region (point-min) (point-max) filename nil t)
|
|
226 If one of them returns non-nil, the file is considered already written
|
|
227 and the rest are not called.
|
|
228 These hooks are considered to pertain to the visited file.
|
|
229 So this list is cleared if you change the visited file name.
|
|
230 See also `write-file-hooks'.")
|
|
231
|
|
232 (defconst enable-local-variables t
|
|
233 "*Control use of local-variables lists in files you visit.
|
|
234 The value can be t, nil or something else.
|
|
235 A value of t means local-variables lists are obeyed;
|
|
236 nil means they are ignored; anything else means query.
|
|
237
|
|
238 The command \\[normal-mode] always obeys local-variables lists
|
|
239 and ignores this variable.")
|
|
240
|
|
241 (defconst enable-local-eval 'maybe
|
|
242 "*Control processing of the \"variable\" `eval' in a file's local variables.
|
|
243 The value can be t, nil or something else.
|
|
244 A value of t means obey `eval' variables;
|
|
245 nil means ignore them; anything else means query.
|
|
246
|
|
247 The command \\[normal-mode] always obeys local-variables lists
|
|
248 and ignores this variable.")
|
|
249
|
|
250 (defvar hack-local-variables-hook nil
|
|
251 "Normal hook run after processing a file's local variables specs.
|
|
252 Major modes can use this to examine user-specified local variables
|
|
253 in order to initialize other data structure based on them.
|
|
254
|
|
255 This hook runs even if there were no local variables or if their
|
|
256 evaluation was suppressed. See also `enable-local-variables' and
|
|
257 `enable-local-eval'.")
|
|
258
|
|
259 ;; Avoid losing in versions where CLASH_DETECTION is disabled.
|
|
260 (or (fboundp 'lock-buffer)
|
|
261 (defalias 'lock-buffer 'ignore))
|
|
262 (or (fboundp 'unlock-buffer)
|
|
263 (defalias 'unlock-buffer 'ignore))
|
|
264
|
|
265 ;;FSFmacs bastardized ange-ftp cruft
|
|
266 ;; This hook function provides support for ange-ftp host name
|
|
267 ;; completion. It runs the usual ange-ftp hook, but only for
|
|
268 ;; completion operations. Having this here avoids the need
|
|
269 ;; to load ange-ftp when it's not really in use.
|
|
270 ;(defun ange-ftp-completion-hook-function (op &rest args)
|
|
271 ; (if (memq op '(file-name-completion file-name-all-completions))
|
|
272 ; (apply 'ange-ftp-hook-function op args)
|
|
273 ; (let ((inhibit-file-name-handlers
|
|
274 ; (cons 'ange-ftp-completion-hook-function
|
|
275 ; (and (eq inhibit-file-name-operation op)
|
|
276 ; inhibit-file-name-handlers)))
|
|
277 ; (inhibit-file-name-operation op))
|
|
278 ; (apply op args))
|
2
|
279
|
|
280 (defun convert-standard-filename (filename)
|
|
281 "Convert a standard file's name to something suitable for the current OS.
|
|
282 This function's standard definition is trivial; it just returns the argument.
|
|
283 However, on some systems, the function is redefined
|
|
284 with a definition that really does change some file names."
|
|
285 filename)
|
0
|
286
|
|
287 (defun pwd ()
|
|
288 "Show the current default directory."
|
|
289 (interactive nil)
|
|
290 (message "Directory %s" default-directory))
|
|
291
|
|
292 (defvar cd-path nil
|
|
293 "Value of the CDPATH environment variable, as a list.
|
2
|
294 Not actually set up until the first time you use it.")
|
0
|
295
|
|
296 (defvar path-separator ":"
|
|
297 "Character used to separate concatenated paths.")
|
|
298
|
|
299 (defun parse-colon-path (cd-path)
|
|
300 "Explode a colon-separated list of paths into a string list."
|
|
301 (and cd-path
|
|
302 (let (cd-list (cd-start 0) cd-colon)
|
|
303 (setq cd-path (concat cd-path path-separator))
|
|
304 (while (setq cd-colon (string-match path-separator cd-path cd-start))
|
|
305 (setq cd-list
|
|
306 (nconc cd-list
|
|
307 (list (if (= cd-start cd-colon)
|
|
308 nil
|
|
309 (substitute-in-file-name
|
|
310 (file-name-as-directory
|
|
311 (substring cd-path cd-start cd-colon)))))))
|
|
312 (setq cd-start (+ cd-colon 1)))
|
|
313 cd-list)))
|
|
314
|
|
315 (defun cd-absolute (dir)
|
|
316 "Change current directory to given absolute file name DIR."
|
|
317 ;; Put the name into directory syntax now,
|
|
318 ;; because otherwise expand-file-name may give some bad results.
|
|
319 (if (not (eq system-type 'vax-vms))
|
|
320 (setq dir (file-name-as-directory dir)))
|
|
321 ;; XEmacs change: stig@hackvan.com
|
|
322 (if find-file-use-truenames
|
|
323 (setq dir (file-truename dir)))
|
|
324 (setq dir (abbreviate-file-name (expand-file-name dir)))
|
|
325 (cond ((not (file-directory-p dir))
|
|
326 (error "%s is not a directory" dir))
|
|
327 ;; this breaks ange-ftp, which doesn't (can't?) overload `file-executable-p'.
|
|
328 ;;((not (file-executable-p dir))
|
|
329 ;; (error "Cannot cd to %s: Permission denied" dir))
|
|
330 (t
|
|
331 (setq default-directory dir))))
|
|
332
|
|
333 (defun cd (dir)
|
|
334 "Make DIR become the current buffer's default directory.
|
|
335 If your environment includes a `CDPATH' variable, try each one of that
|
|
336 colon-separated list of directories when resolving a relative directory name."
|
|
337 ; (interactive "DChange default directory: ")
|
|
338 (interactive
|
|
339 ;; XEmacs change?
|
|
340 (list (read-directory-name "Change default directory: "
|
|
341 default-directory default-directory
|
|
342 (and (member cd-path '(nil ("./")))
|
|
343 (null (getenv "CDPATH"))))))
|
|
344 (if (file-name-absolute-p dir)
|
|
345 (cd-absolute (expand-file-name dir))
|
|
346 (progn
|
|
347 (if (null cd-path)
|
|
348 ;;#### Unix-specific
|
|
349 (let ((trypath (parse-colon-path (getenv "CDPATH"))))
|
|
350 (setq cd-path (or trypath (list "./")))))
|
|
351 (or (catch 'found
|
|
352 (mapcar #'(lambda (x)
|
|
353 (let ((f (expand-file-name (concat x dir))))
|
|
354 (if (file-directory-p f)
|
|
355 (progn
|
|
356 (cd-absolute f)
|
|
357 (throw 'found t)))))
|
|
358 cd-path)
|
|
359 nil)
|
|
360 ;; jwz: give a better error message to those of us with the
|
|
361 ;; good taste not to use a kludge like $CDPATH.
|
|
362 (if (equal cd-path '("./"))
|
|
363 (error "No such directory: %s" (expand-file-name dir))
|
|
364 (error "Directory not found in $CDPATH: %s" dir))))))
|
|
365
|
|
366 (defun load-file (file)
|
|
367 "Load the Lisp file named FILE."
|
|
368 (interactive "fLoad file: ")
|
|
369 (load (expand-file-name file) nil nil t))
|
|
370
|
|
371 ; We now dump utils/lib-complete.el which has improved versions of these.
|
|
372 ;(defun load-library (library)
|
|
373 ; "Load the library named LIBRARY.
|
|
374 ;This is an interface to the function `load'."
|
|
375 ; (interactive "sLoad library: ")
|
|
376 ; (load library))
|
|
377 ;
|
|
378 ;(defun find-library (library)
|
|
379 ; "Find the library of Lisp code named LIBRARY.
|
|
380 ;This searches `load-path' for a file named either \"LIBRARY\" or \"LIBRARY.el\"."
|
|
381 ; (interactive "sFind library file: ")
|
|
382 ; (let ((f (locate-file library load-path ":.el:")))
|
|
383 ; (if f
|
|
384 ; (find-file f)
|
|
385 ; (error "Couldn't locate library %s" library))))
|
|
386
|
|
387 (defun file-local-copy (file &optional buffer)
|
|
388 "Copy the file FILE into a temporary file on this machine.
|
|
389 Returns the name of the local copy, or nil, if FILE is directly
|
|
390 accessible."
|
|
391 (let ((handler (find-file-name-handler file 'file-local-copy)))
|
|
392 (if handler
|
|
393 (funcall handler 'file-local-copy file)
|
|
394 nil)))
|
|
395
|
|
396 ; We have this in C and use the realpath() system call.
|
|
397
|
|
398 ;(defun file-truename (filename &optional counter prev-dirs)
|
|
399 ; "Return the truename of FILENAME, which should be absolute.
|
|
400 ;The truename of a file name is found by chasing symbolic links
|
|
401 ;both at the level of the file and at the level of the directories
|
|
402 ;containing it, until no links are left at any level.
|
|
403 ;
|
|
404 ;The arguments COUNTER and PREV-DIRS are used only in recursive calls.
|
|
405 ;Do not specify them in other calls."
|
|
406 ; ;; COUNTER can be a cons cell whose car is the count of how many more links
|
|
407 ; ;; to chase before getting an error.
|
|
408 ; ;; PREV-DIRS can be a cons cell whose car is an alist
|
|
409 ; ;; of truenames we've just recently computed.
|
|
410 ; ;; The last test looks dubious, maybe `+' is meant here? --simon.
|
|
411 ; (if (or (string= filename "") (string= filename "~")
|
|
412 ; (and (string= (substring filename 0 1) "~")
|
|
413 ; (string-match "~[^/]*" filename)))
|
|
414 ; (progn
|
|
415 ; (setq filename (expand-file-name filename))
|
|
416 ; (if (string= filename "")
|
|
417 ; (setq filename "/"))))
|
|
418 ; (or counter (setq counter (list 100)))
|
|
419 ; (let (done
|
|
420 ; ;; For speed, remove the ange-ftp completion handler from the list.
|
|
421 ; ;; We know it's not needed here.
|
|
422 ; ;; For even more speed, do this only on the outermost call.
|
|
423 ; (file-name-handler-alist
|
|
424 ; (if prev-dirs file-name-handler-alist
|
|
425 ; (let ((tem (copy-sequence file-name-handler-alist)))
|
|
426 ; (delq (rassq 'ange-ftp-completion-hook-function tem) tem)))))
|
|
427 ; (or prev-dirs (setq prev-dirs (list nil)))
|
|
428 ; ;; If this file directly leads to a link, process that iteratively
|
|
429 ; ;; so that we don't use lots of stack.
|
|
430 ; (while (not done)
|
|
431 ; (setcar counter (1- (car counter)))
|
|
432 ; (if (< (car counter) 0)
|
|
433 ; (error "Apparent cycle of symbolic links for %s" filename))
|
|
434 ; (let ((handler (find-file-name-handler filename 'file-truename)))
|
|
435 ; ;; For file name that has a special handler, call handler.
|
|
436 ; ;; This is so that ange-ftp can save time by doing a no-op.
|
|
437 ; (if handler
|
|
438 ; (setq filename (funcall handler 'file-truename filename)
|
|
439 ; done t)
|
|
440 ; (let ((dir (or (file-name-directory filename) default-directory))
|
|
441 ; target dirfile)
|
|
442 ; ;; Get the truename of the directory.
|
|
443 ; (setq dirfile (directory-file-name dir))
|
|
444 ; ;; If these are equal, we have the (or a) root directory.
|
|
445 ; (or (string= dir dirfile)
|
|
446 ; ;; If this is the same dir we last got the truename for,
|
|
447 ; ;; save time--don't recalculate.
|
|
448 ; (if (assoc dir (car prev-dirs))
|
|
449 ; (setq dir (cdr (assoc dir (car prev-dirs))))
|
|
450 ; (let ((old dir)
|
|
451 ; (new (file-name-as-directory (file-truename dirfile counter prev-dirs))))
|
|
452 ; (setcar prev-dirs (cons (cons old new) (car prev-dirs)))
|
|
453 ; (setq dir new))))
|
|
454 ; (if (equal ".." (file-name-nondirectory filename))
|
|
455 ; (setq filename
|
|
456 ; (directory-file-name (file-name-directory (directory-file-name dir)))
|
|
457 ; done t)
|
|
458 ; (if (equal "." (file-name-nondirectory filename))
|
|
459 ; (setq filename (directory-file-name dir)
|
|
460 ; done t)
|
|
461 ; ;; Put it back on the file name.
|
|
462 ; (setq filename (concat dir (file-name-nondirectory filename)))
|
|
463 ; ;; Is the file name the name of a link?
|
|
464 ; (setq target (file-symlink-p filename))
|
|
465 ; (if target
|
|
466 ; ;; Yes => chase that link, then start all over
|
|
467 ; ;; since the link may point to a directory name that uses links.
|
|
468 ; ;; We can't safely use expand-file-name here
|
|
469 ; ;; since target might look like foo/../bar where foo
|
|
470 ; ;; is itself a link. Instead, we handle . and .. above.
|
|
471 ; (setq filename
|
|
472 ; (if (file-name-absolute-p target)
|
|
473 ; target
|
|
474 ; (concat dir target))
|
|
475 ; done nil)
|
|
476 ; ;; No, we are done!
|
|
477 ; (setq done t))))))))
|
|
478 ; filename))
|
|
479
|
|
480 ;; XEmacs addition. Called from `insert-file-contents-internal'
|
|
481 ;; at the appropriate time.
|
|
482 (defun compute-buffer-file-truename (&optional buffer)
|
|
483 "Recomputes BUFFER's value of `buffer-file-truename'
|
|
484 based on the current value of `buffer-file-name'.
|
|
485 BUFFER defaults to the current buffer if unspecified."
|
|
486 (save-excursion
|
|
487 (set-buffer (or buffer (current-buffer)))
|
|
488 (cond ((null buffer-file-name)
|
|
489 (setq buffer-file-truename nil))
|
|
490 ((setq buffer-file-truename (file-truename buffer-file-name))
|
|
491 ;; it exists, we're done.
|
|
492 nil)
|
|
493 (t
|
|
494 ;; the file doesn't exist, but maybe the directory does.
|
|
495 (let* ((dir (file-name-directory buffer-file-name))
|
|
496 (truedir (file-truename dir)))
|
|
497 (if truedir (setq dir truedir))
|
|
498 (setq buffer-file-truename
|
|
499 (expand-file-name (file-name-nondirectory buffer-file-name)
|
|
500 dir)))))
|
|
501 (if (and find-file-use-truenames buffer-file-truename)
|
|
502 (setq buffer-file-name (abbreviate-file-name buffer-file-truename)
|
|
503 default-directory (file-name-directory buffer-file-name)))
|
|
504 buffer-file-truename))
|
|
505
|
|
506 (defun file-chase-links (filename)
|
|
507 "Chase links in FILENAME until a name that is not a link.
|
|
508 Does not examine containing directories for links,
|
|
509 unlike `file-truename'."
|
|
510 (let (tem (count 100) (newname filename))
|
|
511 (while (setq tem (file-symlink-p newname))
|
|
512 (if (= count 0)
|
|
513 (error "Apparent cycle of symbolic links for %s" filename))
|
|
514 ;; In the context of a link, `//' doesn't mean what XEmacs thinks.
|
|
515 (while (string-match "//+" tem)
|
|
516 (setq tem (concat (substring tem 0 (1+ (match-beginning 0)))
|
|
517 (substring tem (match-end 0)))))
|
|
518 ;; Handle `..' by hand, since it needs to work in the
|
|
519 ;; target of any directory symlink.
|
|
520 ;; This code is not quite complete; it does not handle
|
|
521 ;; embedded .. in some cases such as ./../foo and foo/bar/../../../lose.
|
|
522 (while (string-match "\\`\\.\\./" tem) ;#### Unix specific
|
|
523 (setq tem (substring tem 3))
|
|
524 (setq newname (file-name-as-directory
|
|
525 ;; Do the .. by hand.
|
|
526 (directory-file-name
|
|
527 (file-name-directory
|
|
528 ;; Chase links in the default dir of the symlink.
|
|
529 (file-chase-links
|
|
530 (directory-file-name
|
|
531 (file-name-directory newname))))))))
|
|
532 (setq newname (expand-file-name tem (file-name-directory newname)))
|
|
533 (setq count (1- count)))
|
|
534 newname))
|
|
535
|
|
536 (defun switch-to-other-buffer (arg)
|
|
537 "Switch to the previous buffer. With a numeric arg, n, switch to the nth
|
|
538 most recent buffer. With an arg of 0, buries the current buffer at the
|
|
539 bottom of the buffer stack."
|
|
540 (interactive "p")
|
|
541 (if (eq arg 0)
|
|
542 (bury-buffer (current-buffer)))
|
|
543 (switch-to-buffer
|
|
544 (if (<= arg 1) (other-buffer (current-buffer))
|
|
545 (nth (1+ arg) (buffer-list)))))
|
|
546
|
|
547 (defun switch-to-buffer-other-window (buffer)
|
|
548 "Select buffer BUFFER in another window."
|
|
549 (interactive "BSwitch to buffer in other window: ")
|
|
550 (let ((pop-up-windows t))
|
|
551 ;; XEmacs: this used to have (selected-frame) as the third argument,
|
|
552 ;; but this is obnoxious. If the user wants the buffer in a
|
|
553 ;; different frame, then it should be this way.
|
2
|
554
|
|
555 ;; Change documented above undone --mrb
|
|
556 (pop-to-buffer buffer t (selected-frame))))
|
0
|
557
|
|
558 (defun switch-to-buffer-other-frame (buffer)
|
|
559 "Switch to buffer BUFFER in a newly-created frame."
|
|
560 (interactive "BSwitch to buffer in other frame: ")
|
|
561 (let* ((name (get-frame-name-for-buffer buffer))
|
|
562 (frame (make-frame (if name
|
|
563 (list (cons 'name (symbol-name name)))))))
|
|
564 (pop-to-buffer buffer t frame)
|
|
565 (make-frame-visible frame)
|
|
566 buffer))
|
|
567
|
|
568 (defun find-file (filename)
|
|
569 "Edit file FILENAME.
|
|
570 Switch to a buffer visiting file FILENAME,
|
|
571 creating one if none already exists."
|
|
572 (interactive "FFind file: ")
|
|
573 (switch-to-buffer (find-file-noselect filename)))
|
|
574
|
|
575 (defun find-file-other-window (filename)
|
|
576 "Edit file FILENAME, in another window.
|
|
577 May create a new window, or reuse an existing one.
|
|
578 See the function `display-buffer'."
|
|
579 (interactive "FFind file in other window: ")
|
|
580 (switch-to-buffer-other-window (find-file-noselect filename)))
|
|
581
|
|
582 (defun find-file-other-frame (filename)
|
|
583 "Edit file FILENAME, in a newly-created frame."
|
|
584 (interactive "FFind file in other frame: ")
|
|
585 (switch-to-buffer-other-frame (find-file-noselect filename)))
|
|
586
|
|
587 (defun find-file-read-only (filename)
|
|
588 "Edit file FILENAME but don't allow changes.
|
|
589 Like \\[find-file] but marks buffer as read-only.
|
|
590 Use \\[toggle-read-only] to permit editing."
|
|
591 (interactive "fFind file read-only: ")
|
|
592 (find-file filename)
|
|
593 (setq buffer-read-only t)
|
|
594 (current-buffer))
|
|
595
|
|
596 (defun find-file-read-only-other-window (filename)
|
|
597 "Edit file FILENAME in another window but don't allow changes.
|
|
598 Like \\[find-file-other-window] but marks buffer as read-only.
|
|
599 Use \\[toggle-read-only] to permit editing."
|
|
600 (interactive "fFind file read-only other window: ")
|
|
601 (find-file-other-window filename)
|
|
602 (setq buffer-read-only t)
|
|
603 (current-buffer))
|
|
604
|
|
605 (defun find-file-read-only-other-frame (filename)
|
|
606 "Edit file FILENAME in another frame but don't allow changes.
|
|
607 Like \\[find-file-other-frame] but marks buffer as read-only.
|
|
608 Use \\[toggle-read-only] to permit editing."
|
|
609 (interactive "fFind file read-only other frame: ")
|
|
610 (find-file-other-frame filename)
|
|
611 (setq buffer-read-only t)
|
|
612 (current-buffer))
|
|
613
|
|
614 (defun find-alternate-file-other-window (filename)
|
|
615 "Find file FILENAME as a replacement for the file in the next window.
|
|
616 This command does not select that window."
|
|
617 (interactive
|
|
618 (save-selected-window
|
|
619 (other-window 1)
|
|
620 (let ((file buffer-file-name)
|
|
621 (file-name nil)
|
|
622 (file-dir nil))
|
|
623 (and file
|
|
624 (setq file-name (file-name-nondirectory file)
|
|
625 file-dir (file-name-directory file)))
|
|
626 (list (read-file-name
|
|
627 "Find alternate file: " file-dir nil nil file-name)
|
|
628 ))))
|
|
629 (if (one-window-p)
|
|
630 (find-file-other-window filename)
|
|
631 (save-selected-window
|
|
632 (other-window 1)
|
|
633 (find-alternate-file filename))))
|
|
634
|
|
635 (defun find-alternate-file (filename)
|
|
636 "Find file FILENAME, select its buffer, kill previous buffer.
|
|
637 If the current buffer now contains an empty file that you just visited
|
|
638 \(presumably by mistake), use this command to visit the file you really want."
|
|
639 (interactive
|
|
640 (let ((file buffer-file-name)
|
|
641 (file-name nil)
|
|
642 (file-dir nil))
|
|
643 (and file
|
|
644 (setq file-name (file-name-nondirectory file)
|
|
645 file-dir (file-name-directory file)))
|
|
646 (list (read-file-name
|
|
647 "Find alternate file: " file-dir nil nil file-name))))
|
|
648 (and (buffer-modified-p) (buffer-file-name)
|
|
649 ;; (not buffer-read-only)
|
|
650 (not (yes-or-no-p (format
|
|
651 "Buffer %s is modified; kill anyway? "
|
|
652 (buffer-name))))
|
|
653 (error "Aborted"))
|
|
654 (let ((obuf (current-buffer))
|
|
655 (ofile buffer-file-name)
|
|
656 (onum buffer-file-number)
|
|
657 (otrue buffer-file-truename)
|
|
658 (oname (buffer-name)))
|
|
659 (if (get-buffer " **lose**")
|
|
660 (kill-buffer " **lose**"))
|
|
661 (rename-buffer " **lose**")
|
|
662 (setq buffer-file-name nil)
|
|
663 (setq buffer-file-number nil)
|
|
664 (setq buffer-file-truename nil)
|
|
665 (unwind-protect
|
|
666 (progn
|
|
667 (unlock-buffer)
|
|
668 (find-file filename))
|
|
669 (cond ((eq obuf (current-buffer))
|
|
670 (setq buffer-file-name ofile)
|
|
671 (setq buffer-file-number onum)
|
|
672 (setq buffer-file-truename otrue)
|
|
673 (lock-buffer)
|
|
674 (rename-buffer oname))))
|
|
675 (or (eq (current-buffer) obuf)
|
|
676 (kill-buffer obuf))))
|
|
677
|
|
678 (defun create-file-buffer (filename)
|
|
679 "Create a suitably named buffer for visiting FILENAME, and return it.
|
|
680 FILENAME (sans directory) is used unchanged if that name is free;
|
|
681 otherwise a string <2> or <3> or ... is appended to get an unused name."
|
|
682 (let ((lastname (file-name-nondirectory filename)))
|
|
683 (if (string= lastname "")
|
|
684 (setq lastname filename))
|
|
685 (generate-new-buffer lastname)))
|
|
686
|
|
687 (defun generate-new-buffer (name)
|
|
688 "Create and return a buffer with a name based on NAME.
|
|
689 Choose the buffer's name using `generate-new-buffer-name'."
|
|
690 (get-buffer-create (generate-new-buffer-name name)))
|
|
691
|
|
692 ;; FSF has automount-dir-prefix. Our directory-abbrev-alist is more general.
|
|
693 ;; note: tmp_mnt bogosity conversion is established in paths.el.
|
|
694 (defvar directory-abbrev-alist nil
|
|
695 "*Alist of abbreviations for file directories.
|
|
696 A list of elements of the form (FROM . TO), each meaning to replace
|
|
697 FROM with TO when it appears in a directory name.
|
|
698 This replacement is done when setting up the default directory of a
|
|
699 newly visited file. *Every* FROM string should start with \\\\` or ^.
|
|
700
|
|
701 Use this feature when you have directories which you normally refer to
|
|
702 via absolute symbolic links or to eliminate automounter mount points
|
|
703 from the beginning of your filenames. Make TO the name of the link,
|
|
704 and FROM the name it is linked to.")
|
|
705
|
|
706 (defvar abbreviated-home-dir nil
|
|
707 "The user's homedir abbreviated according to `directory-abbrev-alist'.")
|
|
708
|
|
709 (defun abbreviate-file-name (filename &optional hack-homedir)
|
|
710 "Return a version of FILENAME shortened using `directory-abbrev-alist'.
|
|
711 See documentation of variable `directory-abbrev-alist' for more information.
|
|
712 If optional argument HACK-HOMEDIR is non-nil, then this also substitutes
|
|
713 \"~\" for the user's home directory."
|
|
714 ;; Get rid of the prefixes added by the automounter.
|
|
715 ;(if (and (string-match automount-dir-prefix filename)
|
|
716 ; (file-exists-p (file-name-directory
|
|
717 ; (substring filename (1- (match-end 0))))))
|
|
718 ; (setq filename (substring filename (1- (match-end 0)))))
|
|
719 (let ((tail directory-abbrev-alist))
|
|
720 ;; If any elt of directory-abbrev-alist matches this name,
|
|
721 ;; abbreviate accordingly.
|
|
722 (while tail
|
|
723 (if (string-match (car (car tail)) filename)
|
|
724 (setq filename
|
|
725 (concat (cdr (car tail)) (substring filename (match-end 0)))))
|
|
726 (setq tail (cdr tail))))
|
|
727 (if hack-homedir
|
|
728 (progn
|
|
729 ;; Compute and save the abbreviated homedir name.
|
|
730 ;; We defer computing this until the first time it's needed, to
|
|
731 ;; give time for directory-abbrev-alist to be set properly.
|
|
732 ;; We include a slash at the end, to avoid spurious matches
|
|
733 ;; such as `/usr/foobar' when the home dir is `/usr/foo'.
|
|
734 (or abbreviated-home-dir
|
|
735 (setq abbreviated-home-dir
|
|
736 (let ((abbreviated-home-dir "$foo"))
|
|
737 (concat "\\`" (regexp-quote (abbreviate-file-name
|
|
738 (expand-file-name "~")))
|
|
739 "\\(/\\|\\'\\)"))))
|
|
740 ;; If FILENAME starts with the abbreviated homedir,
|
|
741 ;; make it start with `~' instead.
|
|
742 (if (and (string-match abbreviated-home-dir filename)
|
|
743 ;; If the home dir is just /, don't change it.
|
|
744 (not (and (= (match-end 0) 1) ;#### unix-specific
|
|
745 (= (aref filename 0) ?/)))
|
|
746 (not (and (or (eq system-type 'ms-dos)
|
|
747 (eq system-type 'windows-nt))
|
|
748 (save-match-data
|
|
749 (string-match "^[a-zA-Z]:/$" filename)))))
|
|
750 (setq filename
|
|
751 (concat "~"
|
|
752 (substring filename
|
|
753 (match-beginning 1) (match-end 1))
|
|
754 (substring filename (match-end 0)))))))
|
|
755 filename)
|
|
756
|
|
757 (defvar find-file-not-true-dirname-list nil
|
|
758 "*List of logical names for which visiting shouldn't save the true dirname.
|
|
759 On VMS, when you visit a file using a logical name that searches a path,
|
|
760 you may or may not want the visited file name to record the specific
|
|
761 directory where the file was found. If you *do not* want that, add the logical
|
|
762 name to this list as a string.")
|
|
763
|
|
764 ;(defun find-buffer-visiting (filename)
|
|
765 ; "Return the buffer visiting file FILENAME (a string).
|
|
766 ;This is like `get-file-buffer', except that it checks for any buffer
|
|
767 ;visiting the same file, possibly under a different name.
|
|
768 ;If there is no such live buffer, return nil."
|
|
769 ; (let ((buf (get-file-buffer filename))
|
|
770 ; (truename (abbreviate-file-name (file-truename filename))))
|
|
771 ; (or buf
|
|
772 ; (let ((list (buffer-list)) found)
|
|
773 ; (while (and (not found) list)
|
|
774 ; (save-excursion
|
|
775 ; (set-buffer (car list))
|
|
776 ; (if (and buffer-file-name
|
|
777 ; (string= buffer-file-truename truename))
|
|
778 ; (setq found (car list))))
|
|
779 ; (setq list (cdr list)))
|
|
780 ; found)
|
|
781 ; (let ((number (nthcdr 10 (file-attributes truename)))
|
|
782 ; (list (buffer-list)) found)
|
|
783 ; (and number
|
|
784 ; (while (and (not found) list)
|
|
785 ; (save-excursion
|
|
786 ; (set-buffer (car list))
|
|
787 ; (if (and buffer-file-number
|
|
788 ; (equal buffer-file-number number)
|
|
789 ; ;; Verify this buffer's file number
|
|
790 ; ;; still belongs to its file.
|
|
791 ; (file-exists-p buffer-file-name)
|
|
792 ; (equal (nthcdr 10 (file-attributes buffer-file-name))
|
|
793 ; number))
|
|
794 ; (setq found (car list))))
|
|
795 ; (setq list (cdr list))))
|
|
796 ; found))))
|
|
797
|
|
798 (defun insert-file-contents-literally (filename &optional visit beg end replace)
|
|
799 "Like `insert-file-contents', q.v., but only reads in the file.
|
|
800 A buffer may be modified in several ways after reading into the buffer due
|
|
801 to advanced Emacs features, such as file-name-handlers, format decoding,
|
|
802 find-file-hooks, etc.
|
|
803 This function ensures that none of these modifications will take place."
|
|
804 (let ((file-name-handler-alist nil)
|
|
805 (format-alist nil)
|
|
806 (after-insert-file-functions nil)
|
|
807 (find-buffer-file-type-function
|
|
808 (if (fboundp 'find-buffer-file-type)
|
|
809 (symbol-function 'find-buffer-file-type)
|
|
810 nil)))
|
|
811 (unwind-protect
|
|
812 (progn
|
|
813 (fset 'find-buffer-file-type (lambda (filename) t))
|
|
814 (insert-file-contents filename visit beg end replace))
|
|
815 (if find-buffer-file-type-function
|
|
816 (fset 'find-buffer-file-type find-buffer-file-type-function)
|
|
817 (fmakunbound 'find-buffer-file-type)))))
|
|
818
|
|
819 (defun find-file-noselect (filename &optional nowarn rawfile)
|
|
820 "Read file FILENAME into a buffer and return the buffer.
|
|
821 If a buffer exists visiting FILENAME, return that one, but
|
|
822 verify that the file has not changed since visited or saved.
|
|
823 The buffer is not selected, just returned to the caller.
|
|
824 If NOWARN is non-nil warning messages about several potential
|
|
825 problems will be suppressed."
|
|
826 (setq filename (abbreviate-file-name (expand-file-name filename)))
|
|
827 (if (file-directory-p filename)
|
|
828 (if find-file-run-dired
|
|
829 (dired-noselect (if find-file-use-truenames
|
|
830 (abbreviate-file-name (file-truename filename))
|
|
831 filename))
|
|
832 (error "%s is a directory." filename))
|
|
833 (let* ((buf (get-file-buffer filename))
|
|
834 ; (truename (abbreviate-file-name (file-truename filename)))
|
|
835 ; (number (nthcdr 10 (file-attributes truename)))
|
|
836 (number (and buffer-file-truename
|
|
837 (nthcdr 10 (file-attributes buffer-file-truename))))
|
|
838 ; ;; Find any buffer for a file which has same truename.
|
|
839 ; (other (and (not buf) (find-buffer-visiting filename)))
|
|
840 (error nil))
|
|
841
|
|
842 ; ;; Let user know if there is a buffer with the same truename.
|
|
843 ; (if (and (not buf) same-truename (not nowarn))
|
|
844 ; (message "%s and %s are the same file (%s)"
|
|
845 ; filename (buffer-file-name same-truename)
|
|
846 ; truename)
|
|
847 ; (if (and (not buf) same-number (not nowarn))
|
|
848 ; (message "%s and %s are the same file"
|
|
849 ; filename (buffer-file-name same-number))))
|
|
850 ; ;; Optionally also find that buffer.
|
|
851 ; (if (or find-file-existing-other-name find-file-visit-truename)
|
|
852 ; (setq buf (or same-truename same-number)))
|
|
853
|
|
854 (if (and buf
|
|
855 (or find-file-compare-truenames find-file-use-truenames)
|
|
856 (not nowarn))
|
|
857 (save-excursion
|
|
858 (set-buffer buf)
|
|
859 (if (not (string-equal buffer-file-name filename))
|
|
860 (message "%s and %s are the same file (%s)"
|
|
861 filename buffer-file-name
|
|
862 buffer-file-truename))))
|
|
863
|
|
864 (if buf
|
|
865 (or nowarn
|
|
866 (verify-visited-file-modtime buf)
|
|
867 (cond ((not (file-exists-p filename))
|
|
868 (error "File %s no longer exists!" filename))
|
|
869 ((yes-or-no-p
|
|
870 (if (string= (file-name-nondirectory filename)
|
|
871 (buffer-name buf))
|
|
872 (format
|
|
873 (if (buffer-modified-p buf)
|
|
874 (gettext "File %s changed on disk. Discard your edits? ")
|
|
875 (gettext "File %s changed on disk. Reread from disk? "))
|
|
876 (file-name-nondirectory filename))
|
|
877 (format
|
|
878 (if (buffer-modified-p buf)
|
|
879 (gettext "File %s changed on disk. Discard your edits in %s? ")
|
|
880 (gettext "File %s changed on disk. Reread from disk into %s? "))
|
|
881 (file-name-nondirectory filename)
|
|
882 (buffer-name buf))))
|
|
883 (save-excursion
|
|
884 (set-buffer buf)
|
|
885 (revert-buffer t t)))))
|
|
886 (save-excursion
|
|
887 ;;; The truename stuff makes this obsolete.
|
|
888 ;;; (let* ((link-name (car (file-attributes filename)))
|
|
889 ;;; (linked-buf (and (stringp link-name)
|
|
890 ;;; (get-file-buffer link-name))))
|
|
891 ;;; (if (bufferp linked-buf)
|
|
892 ;;; (message "Symbolic link to file in buffer %s"
|
|
893 ;;; (buffer-name linked-buf))))
|
|
894 (setq buf (create-file-buffer filename))
|
|
895 (set-buffer-major-mode buf)
|
|
896 (set-buffer buf)
|
|
897 (erase-buffer)
|
|
898 (if rawfile
|
|
899 (condition-case ()
|
|
900 (insert-file-contents-literally filename t)
|
|
901 (file-error
|
|
902 ;; Unconditionally set error
|
|
903 (setq error t)))
|
|
904 (condition-case e
|
|
905 (insert-file-contents filename t)
|
|
906 (file-error
|
|
907 ;; Run find-file-not-found-hooks until one returns non-nil.
|
|
908 (or (run-hook-with-args-until-success 'find-file-not-found-hooks)
|
|
909 ;; If they fail too, set error.
|
|
910 (setq error e)))))
|
|
911 ;; Find the file's truename, and maybe use that as visited name.
|
|
912 ;; automatically computed in XEmacs.
|
|
913 ; (setq buffer-file-truename truename)
|
|
914 (setq buffer-file-number number)
|
|
915 ;; On VMS, we may want to remember which directory in a search list
|
|
916 ;; the file was found in.
|
|
917 (and (eq system-type 'vax-vms)
|
|
918 (let (logical)
|
|
919 (if (string-match ":" (file-name-directory filename))
|
|
920 (setq logical (substring (file-name-directory filename)
|
|
921 0 (match-beginning 0))))
|
|
922 (not (member logical find-file-not-true-dirname-list)))
|
|
923 (setq buffer-file-name buffer-file-truename))
|
|
924 ; (if find-file-visit-truename
|
|
925 ; (setq buffer-file-name
|
|
926 ; (setq filename
|
|
927 ; (expand-file-name buffer-file-truename))))
|
|
928 (and find-file-use-truenames
|
|
929 ;; This should be in C. Put pathname abbreviations that have
|
|
930 ;; been explicitly requested back into the pathname. Most
|
|
931 ;; importantly, strip out automounter /tmp_mnt directories so
|
|
932 ;; that auto-save will work
|
|
933 (setq buffer-file-name (abbreviate-file-name buffer-file-name)))
|
|
934 ;; Set buffer's default directory to that of the file.
|
|
935 (setq default-directory (file-name-directory buffer-file-name))
|
|
936 ;; Turn off backup files for certain file names. Since
|
|
937 ;; this is a permanent local, the major mode won't eliminate it.
|
|
938 (and (not (funcall backup-enable-predicate buffer-file-name))
|
|
939 (progn
|
|
940 (make-local-variable 'backup-inhibited)
|
|
941 (setq backup-inhibited t)))
|
|
942 (if rawfile
|
|
943 nil
|
|
944 (after-find-file error (not nowarn)))))
|
|
945 buf)))
|
|
946
|
|
947 (defvar after-find-file-from-revert-buffer nil)
|
|
948
|
|
949 (defun after-find-file (&optional error warn noauto
|
|
950 after-find-file-from-revert-buffer)
|
|
951 "Called after finding a file and by the default revert function.
|
|
952 Sets buffer mode, parses local variables.
|
|
953 Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an
|
|
954 error in reading the file. WARN non-nil means warn if there
|
|
955 exists an auto-save file more recent than the visited file.
|
|
956 NOAUTO means don't mess with auto-save mode.
|
|
957 Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil
|
|
958 means this call was from `revert-buffer'.
|
|
959 Finishes by calling the functions in `find-file-hooks'."
|
|
960 (setq buffer-read-only (not (file-writable-p buffer-file-name)))
|
|
961 (if noninteractive
|
|
962 nil
|
|
963 (let* (not-serious
|
|
964 (msg
|
|
965 (cond ((and error (file-attributes buffer-file-name))
|
|
966 (setq buffer-read-only t)
|
|
967 (gettext "File exists, but cannot be read."))
|
|
968 ((not buffer-read-only)
|
|
969 (if (and warn
|
|
970 (file-newer-than-file-p (make-auto-save-file-name)
|
|
971 buffer-file-name))
|
|
972 (gettext "Auto save file is newer; consider M-x recover-file")
|
|
973 (setq not-serious t)
|
|
974 (if error (gettext "(New file)") nil)))
|
|
975 ((not error)
|
|
976 (setq not-serious t)
|
|
977 (gettext "Note: file is write protected"))
|
|
978 ((file-attributes (directory-file-name default-directory))
|
|
979 (gettext "File not found and directory write-protected"))
|
|
980 ((file-exists-p (file-name-directory buffer-file-name))
|
|
981 (setq buffer-read-only nil))
|
|
982 (t
|
|
983 ;; If the directory the buffer is in doesn't exist,
|
|
984 ;; offer to create it. It's better to do this now
|
|
985 ;; than when we save the buffer, because we want
|
|
986 ;; autosaving to work.
|
|
987 (setq buffer-read-only nil)
|
|
988 (or (file-exists-p (file-name-directory buffer-file-name))
|
|
989 (if (yes-or-no-p
|
|
990 (format
|
|
991 "The directory containing %s does not exist. Create? "
|
|
992 (abbreviate-file-name buffer-file-name)))
|
|
993 (make-directory (file-name-directory
|
|
994 buffer-file-name)
|
|
995 t)))
|
|
996 nil))))
|
|
997 (if msg
|
|
998 (progn
|
|
999 (message msg)
|
|
1000 (or not-serious (sit-for 1 t)))))
|
|
1001 (if (and auto-save-default (not noauto))
|
|
1002 (auto-save-mode t)))
|
|
1003 (normal-mode t)
|
|
1004 (run-hooks 'find-file-hooks))
|
|
1005
|
|
1006 (defun normal-mode (&optional find-file)
|
|
1007 "Choose the major mode for this buffer automatically.
|
|
1008 Also sets up any specified local variables of the file.
|
|
1009 Uses the visited file name, the -*- line, and the local variables spec.
|
|
1010
|
|
1011 This function is called automatically from `find-file'. In that case,
|
|
1012 we may set up specified local variables depending on the value of
|
|
1013 `enable-local-variables': if it is t, we do; if it is nil, we don't;
|
|
1014 otherwise, we query. `enable-local-variables' is ignored if you
|
|
1015 run `normal-mode' explicitly."
|
|
1016 (interactive)
|
|
1017 (or find-file (funcall (or default-major-mode 'fundamental-mode)))
|
|
1018 (and (condition-case err
|
|
1019 (progn (set-auto-mode)
|
|
1020 t)
|
|
1021 (error (message "File mode specification error: %s"
|
|
1022 (prin1-to-string err))
|
|
1023 nil))
|
|
1024 (condition-case err
|
|
1025 (hack-local-variables (not find-file))
|
|
1026 (error (message "File local-variables error: %s"
|
|
1027 (prin1-to-string err))))))
|
|
1028
|
2
|
1029 (defvar auto-mode-alist
|
|
1030 (mapcar
|
|
1031 'purecopy
|
|
1032 '(("\\.te?xt\\'" . text-mode)
|
|
1033 ("\\.[ch]\\'" . c-mode)
|
|
1034 ("\\.ltx\\'" . latex-mode)
|
|
1035 ("\\.el\\'" . emacs-lisp-mode)
|
|
1036 ("\\.l\\(i?sp\\)?\\'" . lisp-mode)
|
|
1037 ("\\.f\\(or\\)?\\'" . fortran-mode)
|
|
1038 ("\\.p\\(as\\)?\\'" . pascal-mode)
|
|
1039 ("\\.ad[abs]\\'" . ada-mode)
|
|
1040 ("\\.pl\\'" . perl-mode)
|
|
1041 ("\\.\\([CH]\\|cc\\|hh\\)\\'" . c++-mode)
|
|
1042 ("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode)
|
|
1043 ("\\.java\\'" . java-mode)
|
|
1044 ("\\.ma?k\\'" . makefile-mode)
|
|
1045 ("[Mm]akefile\\(.in\\)?\\(.in\\)?\\'" . makefile-mode)
|
0
|
1046 ;;; Less common extensions come here
|
|
1047 ;;; so more common ones above are found faster.
|
2
|
1048 ("\\.texi\\(nfo\\)?\\'" . texinfo-mode)
|
|
1049 ("\\.s\\'" . asm-mode)
|
|
1050 ("[Cc]hange.?[Ll]og?\\(.[0-9]+\\)?\\'" . change-log-mode)
|
|
1051 ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode)
|
|
1052 ("\\.scm\\(\\.[0-9]*\\)?\\'" . scheme-mode)
|
|
1053 ("\\.py\\'" . python-mode)
|
|
1054 ("\\.e\\'" . eiffel-mode)
|
|
1055 ("\\.mss\\'" . scribe-mode)
|
|
1056 ("\\.m\\([mes]\\|an\\)\\'" . nroff-mode)
|
|
1057 ("\\.icn\\'" . icon-mode)
|
0
|
1058 ;;; The following should come after the ChangeLog pattern
|
|
1059 ;;; for the sake of ChangeLog.1, etc.
|
|
1060 ;;; and after the .scm.[0-9] pattern too.
|
2
|
1061 ("\\.[12345678]\\'" . nroff-mode)
|
|
1062 ("\\.[tT]e[xX]\\'" . tex-mode)
|
|
1063 ("\\.\\(sty\\|cls\\|bbl\\)\\'" . latex-mode)
|
|
1064 ("\\.bib\\'" . bibtex-mode)
|
|
1065 ("\\.article\\'" . text-mode)
|
|
1066 ("\\.letter\\'" . text-mode)
|
|
1067 ("\\.\\(tcl\\|exp\\)\\'" . tcl-mode)
|
|
1068 ("\\.wrl\\'" . vrml-mode)
|
|
1069 ("\\.f90\\'" . f90-mode)
|
|
1070 ("\\.awk\\'" . awk-mode)
|
|
1071 ("\\.prolog\\'" . prolog-mode)
|
|
1072 ("\\.tar\\'" . tar-mode)
|
|
1073 ("\\.\\(arc\\|zip\\|lzh\\|zoo\\)\\'" . archive-mode)
|
|
1074 ;; Mailer puts message to be edited in
|
|
1075 ;; /tmp/Re.... or Message
|
|
1076 ("^/tmp/Re" . text-mode)
|
|
1077 ("/Message[0-9]*\\'" . text-mode)
|
|
1078 ("/drafts/[0-9]+\\'" . mh-letter-mode)
|
|
1079 ;; some news reader is reported to use this
|
|
1080 ("^/tmp/fol/" . text-mode)
|
|
1081 ("\\.y\\'" . c-mode)
|
|
1082 ("\\.lex\\'" . c-mode)
|
|
1083 ("\\.oak\\'" . scheme-mode)
|
|
1084 ("\\.s?html?\\'" . html-mode)
|
|
1085 ("\\.htm?l?3\\'" . html3-mode)
|
|
1086 ("\\.\\(sgml?\\|dtd\\)\\'" . sgml-mode)
|
|
1087 ("\\.c?ps\\'" . postscript-mode)
|
|
1088 ;; .emacs following a directory delimiter
|
|
1089 ;; in either Unix or VMS syntax.
|
|
1090 ("[]>:/]\\..*emacs\\'" . emacs-lisp-mode)
|
|
1091 ;; _emacs following a directory delimiter
|
|
1092 ;; in MsDos syntax
|
|
1093 ("[:/]_emacs\\'" . emacs-lisp-mode)
|
|
1094 ("\\.ml\\'" . lisp-mode)))
|
|
1095 "Alist of filename patterns vs. corresponding major mode functions.
|
0
|
1096 Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL).
|
|
1097 \(NON-NIL stands for anything that is not nil; the value does not matter.)
|
|
1098 Visiting a file whose name matches REGEXP specifies FUNCTION as the
|
|
1099 mode function to use. FUNCTION will be called, unless it is nil.
|
|
1100
|
|
1101 If the element has the form (REGEXP FUNCTION NON-NIL), then after
|
|
1102 calling FUNCTION (if it's not nil), we delete the suffix that matched
|
|
1103 REGEXP and search the list again for another match.")
|
|
1104
|
2
|
1105 (defconst interpreter-mode-alist
|
|
1106 (mapcar 'purecopy
|
|
1107 '(("^#!.*csh" . csh-mode)
|
|
1108 ("^#!.*sh\\b" . ksh-mode)
|
|
1109 ("^#!.*\\b\\(scope\\|wish\\|tcl\\|expect\\)" . tcl-mode)
|
|
1110 ("perl" . perl-mode)
|
|
1111 ("python" . python-mode)
|
|
1112 ("awk\\b" . awk-mode)
|
|
1113 ("rexx" . rexx-mode)
|
|
1114 ("scm" . scheme-mode)
|
|
1115 ("^:" . ksh-mode)
|
|
1116 ))
|
0
|
1117 "Alist mapping interpreter names to major modes.
|
2
|
1118 This alist is used to guess the major mode of a file based on the
|
|
1119 contents of the first line. This line often contains something like:
|
|
1120 #!/bin/sh
|
|
1121 but may contain something more imaginative like
|
|
1122 #! /bin/env python
|
|
1123 or
|
|
1124 eval 'exec perl -w -S $0 ${1+\"$@\"}'.
|
|
1125
|
|
1126 Each alist element looks like (INTERPRETER . MODE).
|
|
1127 The car of each element is a regular expression which is compared
|
|
1128 with the name of the interpreter specified in the first line.
|
0
|
1129 If it matches, mode MODE is selected.")
|
|
1130
|
|
1131 (defconst inhibit-first-line-modes-regexps (purecopy '("\\.tar\\'"))
|
|
1132 "List of regexps; if one matches a file name, don't look for `-*-'.")
|
|
1133
|
|
1134 (defconst inhibit-first-line-modes-suffixes nil
|
|
1135 "List of regexps for what to ignore, for `inhibit-first-line-modes-regexps'.
|
|
1136 When checking `inhibit-first-line-modes-regexps', we first discard
|
|
1137 from the end of the file name anything that matches one of these regexps.")
|
|
1138
|
|
1139 (defvar user-init-file
|
|
1140 "" ; set by command-line
|
|
1141 "File name including directory of user's initialization file.")
|
|
1142
|
|
1143 (defun set-auto-mode ()
|
|
1144 "Select major mode appropriate for current buffer.
|
|
1145 This checks for a -*- mode tag in the buffer's text,
|
|
1146 compares the filename against the entries in `auto-mode-alist',
|
|
1147 or checks the interpreter that runs this file against
|
|
1148 `interpreter-mode-alist'.
|
|
1149
|
|
1150 It does not check for the `mode:' local variable in the
|
|
1151 Local Variables section of the file; for that, use `hack-local-variables'.
|
|
1152
|
|
1153 If `enable-local-variables' is nil, this function does not check for a
|
|
1154 -*- mode tag."
|
|
1155 (save-excursion
|
|
1156 ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
|
|
1157 ;; Do this by calling the hack-local-variables helper to avoid redundancy.
|
|
1158 ;; We bind enable-local-variables to nil this time because we're going to
|
|
1159 ;; call hack-local-variables-prop-line again later, "for real."
|
|
1160 (or (let ((enable-local-variables nil))
|
|
1161 (hack-local-variables-prop-line nil))
|
|
1162 ;; It's not in the -*- line, so check the auto-mode-alist, unless
|
|
1163 ;; this buffer isn't associated with a file.
|
|
1164 (null buffer-file-name)
|
|
1165 (let ((name (file-name-sans-versions buffer-file-name))
|
|
1166 (keep-going t))
|
|
1167 (while keep-going
|
|
1168 (setq keep-going nil)
|
|
1169 (let ((alist auto-mode-alist)
|
|
1170 (mode nil))
|
|
1171 ;; Find first matching alist entry.
|
|
1172 (let ((case-fold-search
|
|
1173 (memq system-type '(vax-vms windows-nt))))
|
|
1174 (while (and (not mode) alist)
|
|
1175 (if (string-match (car (car alist)) name)
|
|
1176 (if (and (consp (cdr (car alist)))
|
|
1177 (nth 2 (car alist)))
|
|
1178 (progn
|
|
1179 (setq mode (car (cdr (car alist)))
|
|
1180 name (substring name 0 (match-beginning 0))
|
|
1181 keep-going t))
|
|
1182 (setq mode (cdr (car alist))
|
|
1183 keep-going nil)))
|
|
1184 (setq alist (cdr alist))))
|
2
|
1185 ;; If we can't deduce a mode from the file name,
|
|
1186 ;; look for an interpreter specified in the first line.
|
|
1187 (if (null mode)
|
|
1188 (let ((firstline
|
|
1189 (buffer-substring
|
|
1190 (point-min)
|
|
1191 (save-excursion
|
|
1192 (goto-char (point-min)) (end-of-line) (point)))))
|
|
1193 (setq alist interpreter-mode-alist)
|
|
1194 (while alist
|
|
1195 (if (string-match (car (car alist)) firstline)
|
|
1196 (progn
|
|
1197 (setq mode (cdr (car alist)))
|
|
1198 (setq alist nil))
|
|
1199 (setq alist (cdr alist))))))
|
|
1200 (if mode
|
|
1201 (funcall mode))
|
|
1202 ))))))
|
0
|
1203
|
|
1204 (defun hack-local-variables (&optional force)
|
|
1205 "Parse, and bind or evaluate as appropriate, any local variables
|
|
1206 for current buffer."
|
|
1207 ;; Don't look for -*- if this file name matches any
|
|
1208 ;; of the regexps in inhibit-first-line-modes-regexps.
|
|
1209 (if (or (null buffer-file-name) ; don't lose if buffer has no file!
|
|
1210 (not (let ((temp inhibit-first-line-modes-regexps)
|
|
1211 (name (if buffer-file-name
|
|
1212 (file-name-sans-versions buffer-file-name)
|
|
1213 (buffer-name))))
|
|
1214 (while (let ((sufs inhibit-first-line-modes-suffixes))
|
|
1215 (while (and sufs (not
|
|
1216 (string-match (car sufs) name)))
|
|
1217 (setq sufs (cdr sufs)))
|
|
1218 sufs)
|
|
1219 (setq name (substring name 0 (match-beginning 0))))
|
|
1220 (while (and temp
|
|
1221 (not (string-match (car temp) name)))
|
|
1222 (setq temp (cdr temp))
|
|
1223 temp))))
|
|
1224 (progn
|
|
1225 ;; Look for variables in the -*- line.
|
|
1226 (hack-local-variables-prop-line force)
|
|
1227 ;; Look for "Local variables:" block in last page.
|
|
1228 (hack-local-variables-last-page force)))
|
|
1229 (run-hooks 'hack-local-variables-hook))
|
|
1230
|
|
1231 ;;; Local variables may be specified in the last page of the file (within 3k
|
|
1232 ;;; from the end of the file and after the last ^L) in the form
|
|
1233 ;;;
|
|
1234 ;;; Local variables:
|
|
1235 ;;; variable-name: variable-value
|
|
1236 ;;; end:
|
|
1237 ;;;
|
|
1238 ;;; The lines may begin with a common prefix, like ";;; " in the above
|
|
1239 ;;; example. They may also have a common suffix (" */" for example). In
|
|
1240 ;;; this form, the local variable "mode" can be used to change the major
|
|
1241 ;;; mode, and the local variable "eval" can be used to evaluate an arbitrary
|
|
1242 ;;; form.
|
|
1243 ;;;
|
|
1244 ;;; Local variables may also be specified in the first line of the file.
|
|
1245 ;;; Embedded in this line are a pair of "-*-" sequences. What lies between
|
|
1246 ;;; them are variable-name/variable-value pairs, like:
|
|
1247 ;;;
|
|
1248 ;;; -*- mode: emacs-lisp -*-
|
|
1249 ;;; or -*- mode: postscript; version-control: never -*-
|
|
1250 ;;; or -*- tags-file-name: "/foo/bar/TAGS" -*-
|
|
1251 ;;;
|
|
1252 ;;; The local variable "eval" is not used with this form. For hysterical
|
|
1253 ;;; reasons, the syntax "-*- modename -*-" is allowed as well.
|
|
1254 ;;;
|
|
1255
|
|
1256 (defun hack-local-variables-p (modeline)
|
|
1257 (or (eq enable-local-variables t)
|
|
1258 (and enable-local-variables
|
|
1259 (save-window-excursion
|
|
1260 (condition-case nil
|
|
1261 (switch-to-buffer (current-buffer))
|
|
1262 (error
|
|
1263 ;; If we fail to switch in the selected window,
|
|
1264 ;; it is probably a minibuffer.
|
|
1265 ;; So try another window.
|
|
1266 (condition-case nil
|
|
1267 (switch-to-buffer-other-window (current-buffer))
|
|
1268 (error
|
|
1269 (switch-to-buffer-other-frame (current-buffer))))))
|
|
1270 (or modeline (save-excursion
|
|
1271 (beginning-of-line)
|
|
1272 (set-window-start (selected-window) (point))))
|
|
1273 (y-or-n-p (format
|
|
1274 "Set local variables as specified %s of %s? "
|
|
1275 (if modeline "in -*- line" "at end")
|
|
1276 (if buffer-file-name
|
|
1277 (file-name-nondirectory buffer-file-name)
|
|
1278 (concat "buffer " (buffer-name)))))))))
|
|
1279
|
|
1280 (defun hack-local-variables-last-page (&optional force)
|
|
1281 ;; Set local variables set in the "Local Variables:" block of the last page.
|
|
1282 (save-excursion
|
|
1283 (goto-char (point-max))
|
|
1284 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
|
|
1285 (if (let ((case-fold-search t))
|
|
1286 (and (search-forward "Local Variables:" nil t)
|
|
1287 (or force
|
|
1288 (hack-local-variables-p nil))))
|
|
1289 (let ((continue t)
|
|
1290 prefix prefixlen suffix beg
|
|
1291 (enable-local-eval enable-local-eval))
|
|
1292 ;; The prefix is what comes before "local variables:" in its line.
|
|
1293 ;; The suffix is what comes after "local variables:" in its line.
|
|
1294 (skip-chars-forward " \t")
|
|
1295 (or (eolp)
|
|
1296 (setq suffix (buffer-substring (point)
|
|
1297 (progn (end-of-line) (point)))))
|
|
1298 (goto-char (match-beginning 0))
|
|
1299 (or (bolp)
|
|
1300 (setq prefix
|
|
1301 (buffer-substring (point)
|
|
1302 (progn (beginning-of-line) (point)))))
|
|
1303 (if prefix (setq prefixlen (length prefix)
|
|
1304 prefix (regexp-quote prefix)))
|
|
1305 (if suffix (setq suffix (concat (regexp-quote suffix) "$")))
|
|
1306 (while continue
|
|
1307 ;; Look at next local variable spec.
|
|
1308 (if selective-display (re-search-forward "[\n\C-m]")
|
|
1309 (forward-line 1))
|
|
1310 ;; Skip the prefix, if any.
|
|
1311 (if prefix
|
|
1312 (if (looking-at prefix)
|
|
1313 (forward-char prefixlen)
|
|
1314 (error "Local variables entry is missing the prefix")))
|
|
1315 ;; Find the variable name; strip whitespace.
|
|
1316 (skip-chars-forward " \t")
|
|
1317 (setq beg (point))
|
|
1318 (skip-chars-forward "^:\n")
|
|
1319 (if (eolp) (error "Missing colon in local variables entry"))
|
|
1320 (skip-chars-backward " \t")
|
|
1321 (let* ((str (buffer-substring beg (point)))
|
|
1322 (var (read str))
|
|
1323 val)
|
|
1324 ;; Setting variable named "end" means end of list.
|
|
1325 (if (string-equal (downcase str) "end")
|
|
1326 (setq continue nil)
|
|
1327 ;; Otherwise read the variable value.
|
|
1328 (skip-chars-forward "^:")
|
|
1329 (forward-char 1)
|
|
1330 (setq val (read (current-buffer)))
|
|
1331 (skip-chars-backward "\n")
|
|
1332 (skip-chars-forward " \t")
|
|
1333 (or (if suffix (looking-at suffix) (eolp))
|
|
1334 (error "Local variables entry is terminated incorrectly"))
|
|
1335 ;; Set the variable. "Variables" mode and eval are funny.
|
|
1336 (hack-one-local-variable var val))))))))
|
|
1337
|
|
1338
|
|
1339 (defun hack-local-variables-prop-line (&optional force)
|
|
1340 ;; Set local variables specified in the -*- line.
|
|
1341 ;; Returns t if mode was set.
|
2
|
1342 (let ((result nil))
|
0
|
1343 (save-excursion
|
|
1344 (goto-char (point-min))
|
|
1345 (skip-chars-forward " \t\n\r")
|
2
|
1346 (let ((end (save-excursion
|
0
|
1347 ;; If the file begins with "#!"
|
|
1348 ;; (un*x exec interpreter magic), look
|
|
1349 ;; for mode frobs in the first two
|
|
1350 ;; lines. You cannot necessarily
|
|
1351 ;; put them in the first line of
|
|
1352 ;; such a file without screwing up
|
|
1353 ;; the interpreter invocation.
|
|
1354 (end-of-line (and (looking-at "^#!") 2))
|
|
1355 (point))))
|
|
1356 ;; Parse the -*- line into the `result' alist.
|
|
1357 (cond ((not (search-forward "-*-" end t))
|
|
1358 ;; doesn't have one.
|
|
1359 nil)
|
|
1360 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
|
|
1361 ;; Antiquated form: "-*- ModeName -*-".
|
|
1362 (setq result
|
|
1363 (list (cons 'mode
|
|
1364 (intern (buffer-substring
|
|
1365 (match-beginning 1)
|
|
1366 (match-end 1)))))
|
|
1367 ))
|
|
1368 (t
|
|
1369 ;; Usual form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
|
|
1370 ;; (last ";" is optional).
|
|
1371 (save-excursion
|
|
1372 (if (search-forward "-*-" end t)
|
|
1373 (setq end (- (point) 3))
|
|
1374 (error "-*- not terminated before end of line")))
|
|
1375 (while (< (point) end)
|
|
1376 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
|
|
1377 (error "malformed -*- line"))
|
|
1378 (goto-char (match-end 0))
|
|
1379 ;; There used to be a downcase here,
|
|
1380 ;; but the manual didn't say so,
|
|
1381 ;; and people want to set var names that aren't all lc.
|
|
1382 (let ((key (intern (buffer-substring
|
|
1383 (match-beginning 1)
|
|
1384 (match-end 1))))
|
|
1385 (val (save-restriction
|
|
1386 (narrow-to-region (point) end)
|
|
1387 (read (current-buffer)))))
|
2
|
1388 ;; Case sensitivity! Icepicks in my forehead!
|
|
1389 (if (equal (downcase (symbol-name key)) "mode")
|
|
1390 (setq key 'mode))
|
0
|
1391 (setq result (cons (cons key val) result))
|
|
1392 (skip-chars-forward " \t;")))
|
2
|
1393 (setq result (nreverse result))))))
|
0
|
1394
|
2
|
1395 (let ((set-any-p (or force (hack-local-variables-p t)))
|
|
1396 (mode-p nil))
|
|
1397 (while result
|
|
1398 (let ((key (car (car result)))
|
|
1399 (val (cdr (car result))))
|
|
1400 (cond ((eq key 'mode)
|
|
1401 (setq mode-p t)
|
|
1402 (funcall (intern (concat (downcase (symbol-name val))
|
|
1403 "-mode"))))
|
|
1404 (set-any-p
|
|
1405 (hack-one-local-variable key val))
|
|
1406 (t
|
|
1407 nil)))
|
|
1408 (setq result (cdr result)))
|
|
1409 mode-p)))
|
0
|
1410
|
|
1411 (defconst ignored-local-variables
|
|
1412 (list 'enable-local-eval)
|
|
1413 "Variables to be ignored in a file's local variable spec.")
|
|
1414
|
|
1415 ;; Get confirmation before setting these variables as locals in a file.
|
|
1416 (put 'debugger 'risky-local-variable t)
|
|
1417 (put 'enable-local-eval 'risky-local-variable t)
|
|
1418 (put 'ignored-local-variables 'risky-local-variable t)
|
|
1419 (put 'eval 'risky-local-variable t)
|
|
1420 (put 'file-name-handler-alist 'risky-local-variable t)
|
|
1421 (put 'minor-mode-map-alist 'risky-local-variable t)
|
|
1422 (put 'after-load-alist 'risky-local-variable t)
|
|
1423 (put 'buffer-file-name 'risky-local-variable t)
|
|
1424 (put 'buffer-auto-save-file-name 'risky-local-variable t)
|
|
1425 (put 'buffer-file-truename 'risky-local-variable t)
|
|
1426 (put 'exec-path 'risky-local-variable t)
|
|
1427 (put 'load-path 'risky-local-variable t)
|
|
1428 (put 'exec-directory 'risky-local-variable t)
|
|
1429 (put 'process-environment 'risky-local-variable t)
|
|
1430 ;; Don't wait for outline.el to be loaded, for the sake of outline-minor-mode.
|
|
1431 (put 'outline-level 'risky-local-variable t)
|
|
1432 (put 'rmail-output-file-alist 'risky-local-variable t)
|
|
1433
|
|
1434 ;; This one is safe because the user gets to check it before it is used.
|
|
1435 (put 'compile-command 'safe-local-variable t)
|
|
1436
|
|
1437 ;(defun hack-one-local-variable-quotep (exp)
|
|
1438 ; (and (consp exp) (eq (car exp) 'quote) (consp (cdr exp))))
|
|
1439
|
|
1440 ;; "Set" one variable in a local variables spec.
|
|
1441 ;; A few variable names are treated specially.
|
|
1442 (defun hack-one-local-variable (var val)
|
|
1443 (cond ((eq var 'mode)
|
|
1444 (funcall (intern (concat (downcase (symbol-name val))
|
|
1445 "-mode"))))
|
|
1446 ((memq var ignored-local-variables)
|
|
1447 nil)
|
|
1448 ;; "Setting" eval means either eval it or do nothing.
|
|
1449 ;; Likewise for setting hook variables.
|
|
1450 ((or (get var 'risky-local-variable)
|
|
1451 (and
|
|
1452 (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$"
|
|
1453 (symbol-name var))
|
|
1454 (not (get var 'safe-local-variable))))
|
|
1455 ; ;; Permit evaling a put of a harmless property
|
|
1456 ; ;; if the args do nothing tricky.
|
|
1457 ; (if (or (and (eq var 'eval)
|
|
1458 ; (consp val)
|
|
1459 ; (eq (car val) 'put)
|
|
1460 ; (hack-one-local-variable-quotep (nth 1 val))
|
|
1461 ; (hack-one-local-variable-quotep (nth 2 val))
|
|
1462 ; ;; Only allow safe values of lisp-indent-hook;
|
|
1463 ; ;; not functions.
|
|
1464 ; (or (numberp (nth 3 val))
|
|
1465 ; (equal (nth 3 val) ''defun))
|
|
1466 ; (memq (nth 1 (nth 2 val))
|
|
1467 ; '(lisp-indent-hook)))
|
|
1468 (if (and (not (zerop (user-uid)))
|
|
1469 (or (eq enable-local-eval t)
|
|
1470 (and enable-local-eval
|
|
1471 (save-window-excursion
|
|
1472 (switch-to-buffer (current-buffer))
|
|
1473 (save-excursion
|
|
1474 (beginning-of-line)
|
|
1475 (set-window-start (selected-window) (point)))
|
|
1476 (setq enable-local-eval
|
|
1477 (y-or-n-p (format "Process `eval' or hook local variables in file %s? "
|
|
1478 (file-name-nondirectory buffer-file-name))))))))
|
|
1479 (if (eq var 'eval)
|
|
1480 (save-excursion (eval val))
|
|
1481 (make-local-variable var)
|
|
1482 (set var val))
|
|
1483 (message "Ignoring `eval:' in file's local variables")))
|
|
1484 ;; Ordinary variable, really set it.
|
|
1485 (t (make-local-variable var)
|
|
1486 (set var val))))
|
|
1487
|
|
1488 (defun set-visited-file-name (filename)
|
|
1489 "Change name of file visited in current buffer to FILENAME.
|
|
1490 The next time the buffer is saved it will go in the newly specified file.
|
|
1491 nil or empty string as argument means make buffer not be visiting any file.
|
|
1492 Remember to delete the initial contents of the minibuffer
|
|
1493 if you wish to pass an empty string as the argument."
|
|
1494 (interactive "FSet visited file name: ")
|
|
1495 (if (buffer-base-buffer)
|
|
1496 (error "An indirect buffer cannot visit a file"))
|
|
1497 (let (truename)
|
|
1498 (if filename
|
|
1499 (setq filename
|
|
1500 (if (string-equal filename "")
|
|
1501 nil
|
|
1502 (expand-file-name filename))))
|
|
1503 (if filename
|
|
1504 (progn
|
|
1505 (setq truename (file-truename filename))
|
|
1506 ;; #### Do we need to check if truename is non-nil?
|
|
1507 (if find-file-use-truenames
|
|
1508 (setq filename truename))))
|
|
1509 (or (equal filename buffer-file-name)
|
|
1510 (progn
|
|
1511 (and filename (lock-buffer filename))
|
|
1512 (unlock-buffer)))
|
|
1513 (setq buffer-file-name filename)
|
|
1514 (if filename ; make buffer name reflect filename.
|
|
1515 (let ((new-name (file-name-nondirectory buffer-file-name)))
|
|
1516 (if (string= new-name "")
|
|
1517 (error "Empty file name"))
|
|
1518 (if (eq system-type 'vax-vms)
|
|
1519 (setq new-name (downcase new-name)))
|
|
1520 (setq default-directory (file-name-directory buffer-file-name))
|
|
1521 (or (string= new-name (buffer-name))
|
|
1522 (rename-buffer new-name t))))
|
|
1523 (setq buffer-backed-up nil)
|
|
1524 (clear-visited-file-modtime)
|
|
1525 (compute-buffer-file-truename) ; insert-file-contents does this too.
|
|
1526 ; ;; Abbreviate the file names of the buffer.
|
|
1527 ; (if truename
|
|
1528 ; (progn
|
|
1529 ; (setq buffer-file-truename (abbreviate-file-name truename))
|
|
1530 ; (if find-file-visit-truename
|
|
1531 ; (setq buffer-file-name buffer-file-truename))))
|
|
1532 (setq buffer-file-number
|
|
1533 (if filename
|
|
1534 (nthcdr 10 (file-attributes buffer-file-name))
|
|
1535 nil)))
|
|
1536 ;; write-file-hooks is normally used for things like ftp-find-file
|
|
1537 ;; that visit things that are not local files as if they were files.
|
|
1538 ;; Changing to visit an ordinary local file instead should flush the hook.
|
|
1539 (kill-local-variable 'write-file-hooks)
|
|
1540 (kill-local-variable 'after-save-hook)
|
|
1541 (kill-local-variable 'local-write-file-hooks)
|
|
1542 (kill-local-variable 'write-file-data-hooks)
|
|
1543 (kill-local-variable 'revert-buffer-function)
|
|
1544 (kill-local-variable 'backup-inhibited)
|
|
1545 ;; If buffer was read-only because of version control,
|
|
1546 ;; that reason is gone now, so make it writable.
|
|
1547 (if (and (boundp 'vc-mode) vc-mode)
|
|
1548 (setq buffer-read-only nil))
|
|
1549 (kill-local-variable 'vc-mode)
|
|
1550 ;; Turn off backup files for certain file names.
|
|
1551 ;; Since this is a permanent local, the major mode won't eliminate it.
|
|
1552 (and (not (funcall backup-enable-predicate buffer-file-name))
|
|
1553 (progn
|
|
1554 (make-local-variable 'backup-inhibited)
|
|
1555 (setq backup-inhibited t)))
|
|
1556 (let ((oauto buffer-auto-save-file-name))
|
|
1557 ;; If auto-save was not already on, turn it on if appropriate.
|
|
1558 (if (not buffer-auto-save-file-name)
|
|
1559 (and buffer-file-name auto-save-default
|
|
1560 (auto-save-mode t))
|
|
1561 ;; If auto save is on, start using a new name.
|
|
1562 ;; We deliberately don't rename or delete the old auto save
|
|
1563 ;; for the old visited file name. This is because perhaps
|
|
1564 ;; the user wants to save the new state and then compare with the
|
|
1565 ;; previous state from the auto save file.
|
|
1566 (setq buffer-auto-save-file-name
|
|
1567 (make-auto-save-file-name)))
|
|
1568 ;; Rename the old auto save file if any.
|
|
1569 (and oauto buffer-auto-save-file-name
|
|
1570 (file-exists-p oauto)
|
|
1571 (rename-file oauto buffer-auto-save-file-name t)))
|
|
1572 (if buffer-file-name
|
|
1573 (set-buffer-modified-p t))
|
|
1574 ;; #### ??
|
|
1575 (run-hooks 'after-set-visited-file-name-hooks))
|
|
1576
|
|
1577 (defun write-file (filename &optional confirm)
|
|
1578 "Write current buffer into file FILENAME.
|
|
1579 Makes buffer visit that file, and marks it not modified.
|
|
1580 If the buffer is already visiting a file, you can specify
|
|
1581 a directory name as FILENAME, to write a file of the same
|
|
1582 old name in that directory.
|
|
1583 If optional second arg CONFIRM is non-nil,
|
|
1584 ask for confirmation for overwriting an existing file."
|
|
1585 ;; (interactive "FWrite file: ")
|
|
1586 (interactive
|
|
1587 (list (if buffer-file-name
|
|
1588 (read-file-name "Write file: "
|
|
1589 nil nil nil nil)
|
|
1590 (read-file-name "Write file: "
|
|
1591 (cdr (assq 'default-directory
|
|
1592 (buffer-local-variables)))
|
|
1593 nil nil (buffer-name)))
|
|
1594 t))
|
|
1595 (and (eq (current-buffer) mouse-grabbed-buffer)
|
|
1596 (error "Can't write minibuffer window"))
|
|
1597 (or (null filename) (string-equal filename "")
|
|
1598 (progn
|
|
1599 ;; If arg is just a directory,
|
|
1600 ;; use same file name, but in that directory.
|
|
1601 (if (and (file-directory-p filename) buffer-file-name)
|
|
1602 (setq filename (concat (file-name-as-directory filename)
|
|
1603 (file-name-nondirectory buffer-file-name))))
|
|
1604 (and confirm
|
|
1605 (file-exists-p filename)
|
|
1606 (or (y-or-n-p (format "File `%s' exists; overwrite? " filename))
|
|
1607 (error "Canceled")))
|
|
1608 (set-visited-file-name filename)))
|
|
1609 (set-buffer-modified-p t)
|
|
1610 (setq buffer-read-only nil)
|
|
1611 (save-buffer))
|
|
1612
|
|
1613 (defun backup-buffer ()
|
|
1614 "Make a backup of the disk file visited by the current buffer, if appropriate.
|
|
1615 This is normally done before saving the buffer the first time.
|
|
1616 If the value is non-nil, it is the result of `file-modes' on the original file;
|
|
1617 this means that the caller, after saving the buffer, should change the modes
|
|
1618 of the new file to agree with the old modes."
|
|
1619 (if (and make-backup-files
|
|
1620 (not backup-inhibited)
|
|
1621 (not buffer-backed-up)
|
|
1622 (file-exists-p buffer-file-name)
|
|
1623 (memq (aref (elt (file-attributes buffer-file-name) 8) 0)
|
|
1624 '(?- ?l)))
|
|
1625 (let ((real-file-name buffer-file-name)
|
|
1626 backup-info backupname targets setmodes)
|
|
1627 ;; If specified name is a symbolic link, chase it to the target.
|
|
1628 ;; Thus we make the backups in the directory where the real file is.
|
|
1629 (setq real-file-name (file-chase-links real-file-name))
|
|
1630 (setq backup-info (find-backup-file-name real-file-name)
|
|
1631 backupname (car backup-info)
|
|
1632 targets (cdr backup-info))
|
|
1633 ;;; (if (file-directory-p buffer-file-name)
|
|
1634 ;;; (error "Cannot save buffer in directory %s" buffer-file-name))
|
|
1635 (if backup-info
|
|
1636 (condition-case ()
|
|
1637 (let ((delete-old-versions
|
|
1638 ;; If have old versions to maybe delete,
|
|
1639 ;; ask the user to confirm now, before doing anything.
|
|
1640 ;; But don't actually delete til later.
|
|
1641 (and targets
|
|
1642 (or (eq delete-old-versions t)
|
|
1643 (eq delete-old-versions nil))
|
|
1644 (or delete-old-versions
|
|
1645 (y-or-n-p (format "Delete excess backup versions of %s? "
|
|
1646 real-file-name))))))
|
|
1647 ;; Actually write the back up file.
|
|
1648 (condition-case ()
|
|
1649 (if (or file-precious-flag
|
|
1650 ; (file-symlink-p buffer-file-name)
|
|
1651 backup-by-copying
|
|
1652 (and backup-by-copying-when-linked
|
|
1653 (> (file-nlinks real-file-name) 1))
|
|
1654 (and backup-by-copying-when-mismatch
|
|
1655 (let ((attr (file-attributes real-file-name)))
|
|
1656 (or (nth 9 attr)
|
|
1657 (not (file-ownership-preserved-p real-file-name))))))
|
|
1658 (condition-case ()
|
|
1659 (copy-file real-file-name backupname t t)
|
|
1660 (file-error
|
|
1661 ;; If copying fails because file BACKUPNAME
|
|
1662 ;; is not writable, delete that file and try again.
|
|
1663 (if (and (file-exists-p backupname)
|
|
1664 (not (file-writable-p backupname)))
|
|
1665 (delete-file backupname))
|
|
1666 (copy-file real-file-name backupname t t)))
|
|
1667 ;; rename-file should delete old backup.
|
|
1668 (rename-file real-file-name backupname t)
|
|
1669 (setq setmodes (file-modes backupname)))
|
|
1670 (file-error
|
|
1671 ;; If trouble writing the backup, write it in ~.
|
|
1672 (setq backupname (expand-file-name "~/%backup%~"))
|
|
1673 (message "Cannot write backup file; backing up in ~/%%backup%%~")
|
|
1674 (sleep-for 1)
|
|
1675 (condition-case ()
|
|
1676 (copy-file real-file-name backupname t t)
|
|
1677 (file-error
|
|
1678 ;; If copying fails because file BACKUPNAME
|
|
1679 ;; is not writable, delete that file and try again.
|
|
1680 (if (and (file-exists-p backupname)
|
|
1681 (not (file-writable-p backupname)))
|
|
1682 (delete-file backupname))
|
|
1683 (copy-file real-file-name backupname t t)))))
|
|
1684 (setq buffer-backed-up t)
|
|
1685 ;; Now delete the old versions, if desired.
|
|
1686 (if delete-old-versions
|
|
1687 (while targets
|
|
1688 (condition-case ()
|
|
1689 (delete-file (car targets))
|
|
1690 (file-error nil))
|
|
1691 (setq targets (cdr targets))))
|
|
1692 setmodes)
|
|
1693 (file-error nil))))))
|
|
1694
|
|
1695 (defun file-name-sans-versions (name &optional keep-backup-version)
|
|
1696 "Return FILENAME sans backup versions or strings.
|
|
1697 This is a separate procedure so your site-init or startup file can
|
|
1698 redefine it.
|
|
1699 If the optional argument KEEP-BACKUP-VERSION is non-nil,
|
|
1700 we do not remove backup version numbers, only true file version numbers."
|
|
1701 (let ((handler (find-file-name-handler name 'file-name-sans-versions)))
|
|
1702 (if handler
|
|
1703 (funcall handler 'file-name-sans-versions name keep-backup-version)
|
|
1704 (substring name 0
|
|
1705 (if (eq system-type 'vax-vms)
|
|
1706 ;; VMS version number is (a) semicolon, optional
|
|
1707 ;; sign, zero or more digits or (b) period, option
|
|
1708 ;; sign, zero or more digits, provided this is the
|
|
1709 ;; second period encountered outside of the
|
|
1710 ;; device/directory part of the file name.
|
|
1711 (or (string-match ";[-+]?[0-9]*\\'" name)
|
|
1712 (if (string-match "\\.[^]>:]*\\(\\.[-+]?[0-9]*\\)\\'"
|
|
1713 name)
|
|
1714 (match-beginning 1))
|
|
1715 (length name))
|
|
1716 (if keep-backup-version
|
|
1717 (length name)
|
|
1718 (or (string-match "\\.~[0-9.]+~\\'" name)
|
|
1719 ;; XEmacs - VC uses extensions like ".~tagname~" or ".~1.1.5.2~"
|
|
1720 (let ((pos (string-match "\\.~\\([^.~ \t]+\\|[0-9.]+\\)~\\'" name)))
|
|
1721 (and pos
|
|
1722 ;; #### - is this filesystem check too paranoid?
|
|
1723 (file-exists-p (substring name 0 pos))
|
|
1724 pos))
|
|
1725 (string-match "~\\'" name)
|
|
1726 (length name))))))))
|
|
1727
|
|
1728 (defun file-ownership-preserved-p (file)
|
|
1729 "Returns t if deleting FILE and rewriting it would preserve the owner."
|
|
1730 (let ((handler (find-file-name-handler file 'file-ownership-preserved-p)))
|
|
1731 (if handler
|
|
1732 (funcall handler 'file-ownership-preserved-p file)
|
|
1733 (let ((attributes (file-attributes file)))
|
|
1734 ;; Return t if the file doesn't exist, since it's true that no
|
|
1735 ;; information would be lost by an (attempted) delete and create.
|
|
1736 (or (null attributes)
|
|
1737 (= (nth 2 attributes) (user-uid)))))))
|
|
1738
|
|
1739 (defun file-name-sans-extension (filename)
|
|
1740 "Return FILENAME sans final \"extension\".
|
|
1741 The extension, in a file name, is the part that follows the last `.'."
|
|
1742 (save-match-data
|
|
1743 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
|
|
1744 directory)
|
|
1745 (if (string-match "\\.[^.]*\\'" file)
|
|
1746 (if (setq directory (file-name-directory filename))
|
|
1747 (expand-file-name (substring file 0 (match-beginning 0))
|
|
1748 directory)
|
|
1749 (substring file 0 (match-beginning 0)))
|
|
1750 filename))))
|
|
1751
|
|
1752 (defun make-backup-file-name (file)
|
|
1753 "Create the non-numeric backup file name for FILE.
|
|
1754 This is a separate function so you can redefine it for customization."
|
|
1755 (if (eq system-type 'ms-dos)
|
|
1756 (let ((fn (file-name-nondirectory file)))
|
|
1757 (concat (file-name-directory file)
|
|
1758 (if (string-match "\\([^.]*\\)\\(\\..*\\)?" fn)
|
|
1759 (substring fn 0 (match-end 1)))
|
|
1760 ".bak"))
|
|
1761 (concat file "~")))
|
|
1762
|
|
1763 (defun backup-file-name-p (file)
|
|
1764 "Return non-nil if FILE is a backup file name (numeric or not).
|
|
1765 This is a separate function so you can redefine it for customization.
|
|
1766 You may need to redefine `file-name-sans-versions' as well."
|
|
1767 (if (eq system-type 'ms-dos)
|
|
1768 (string-match "\\.bak\\'" file)
|
|
1769 (string-match "~\\'" file)))
|
|
1770
|
|
1771 ;; This is used in various files.
|
|
1772 ;; The usage of bv-length is not very clean,
|
|
1773 ;; but I can't see a good alternative,
|
|
1774 ;; so as of now I am leaving it alone.
|
|
1775 (defun backup-extract-version (fn)
|
|
1776 "Given the name of a numeric backup file, return the backup number.
|
|
1777 Uses the free variable `bv-length', whose value should be
|
|
1778 the index in the name where the version number begins."
|
|
1779 (if (and (string-match "[0-9]+~\\'" fn bv-length)
|
|
1780 (= (match-beginning 0) bv-length))
|
|
1781 (string-to-int (substring fn bv-length -1))
|
|
1782 0))
|
|
1783
|
|
1784 ;; I believe there is no need to alter this behavior for VMS;
|
|
1785 ;; since backup files are not made on VMS, it should not get called.
|
|
1786 (defun find-backup-file-name (fn)
|
|
1787 "Find a file name for a backup file, and suggestions for deletions.
|
|
1788 Value is a list whose car is the name for the backup file
|
|
1789 and whose cdr is a list of old versions to consider deleting now.
|
|
1790 If the value is nil, don't make a backup."
|
|
1791 (let ((handler (find-file-name-handler fn 'find-backup-file-name)))
|
|
1792 ;; Run a handler for this function so that ange-ftp can refuse to do it.
|
|
1793 (if handler
|
|
1794 (funcall handler 'find-backup-file-name fn)
|
|
1795 (if (eq version-control 'never)
|
|
1796 (list (make-backup-file-name fn))
|
|
1797 (let* ((base-versions (concat (file-name-nondirectory fn) ".~"))
|
|
1798 ;; used by backup-extract-version:
|
|
1799 (bv-length (length base-versions))
|
|
1800 possibilities
|
|
1801 (versions nil)
|
|
1802 (high-water-mark 0)
|
|
1803 (deserve-versions-p nil)
|
|
1804 (number-to-delete 0))
|
|
1805 (condition-case ()
|
|
1806 (setq possibilities (file-name-all-completions
|
|
1807 base-versions
|
|
1808 (file-name-directory fn))
|
|
1809 versions (sort (mapcar
|
|
1810 #'backup-extract-version
|
|
1811 possibilities)
|
|
1812 '<)
|
|
1813 high-water-mark (apply #'max 0 versions)
|
|
1814 deserve-versions-p (or version-control
|
|
1815 (> high-water-mark 0))
|
|
1816 number-to-delete (- (length versions)
|
|
1817 kept-old-versions kept-new-versions -1))
|
|
1818 (file-error
|
|
1819 (setq possibilities nil)))
|
|
1820 (if (not deserve-versions-p)
|
|
1821 (list (make-backup-file-name fn))
|
|
1822 (cons (concat fn ".~" (int-to-string (1+ high-water-mark)) "~")
|
|
1823 (if (and (> number-to-delete 0)
|
|
1824 ;; Delete nothing if there is overflow
|
|
1825 ;; in the number of versions to keep.
|
|
1826 (>= (+ kept-new-versions kept-old-versions -1) 0))
|
|
1827 (mapcar #'(lambda (n)
|
|
1828 (concat fn ".~" (int-to-string n) "~"))
|
|
1829 (let ((v (nthcdr kept-old-versions versions)))
|
|
1830 (rplacd (nthcdr (1- number-to-delete) v) ())
|
|
1831 v))))))))))
|
|
1832
|
|
1833 (defun file-nlinks (filename)
|
|
1834 "Return number of names file FILENAME has."
|
|
1835 (car (cdr (file-attributes filename))))
|
|
1836
|
|
1837 (defun file-relative-name (filename &optional directory)
|
|
1838 "Convert FILENAME to be relative to DIRECTORY (default: default-directory)."
|
|
1839 (setq filename (expand-file-name filename)
|
|
1840 directory (file-name-as-directory (if directory
|
|
1841 (expand-file-name directory)
|
|
1842 default-directory)))
|
|
1843 (while directory
|
|
1844 (let ((up (file-name-directory (directory-file-name directory))))
|
|
1845 (cond ((and (string= directory up)
|
|
1846 (file-name-absolute-p directory))
|
|
1847 ;; "/"
|
|
1848 (setq directory nil))
|
|
1849 ((string-match (concat "\\`" (regexp-quote directory))
|
|
1850 filename)
|
|
1851 (setq filename (substring filename (match-end 0)))
|
|
1852 (setq directory nil))
|
|
1853 (t
|
|
1854 ;; go up one level
|
|
1855 (setq directory up)))))
|
|
1856 filename)
|
|
1857
|
|
1858 (defun save-buffer (&optional args)
|
|
1859 "Save current buffer in visited file if modified. Versions described below.
|
|
1860
|
|
1861 By default, makes the previous version into a backup file
|
|
1862 if previously requested or if this is the first save.
|
|
1863 With 1 or 3 \\[universal-argument]'s, marks this version
|
|
1864 to become a backup when the next save is done.
|
|
1865 With 2 or 3 \\[universal-argument]'s,
|
|
1866 unconditionally makes the previous version into a backup file.
|
|
1867 With argument of 0, never makes the previous version into a backup file.
|
|
1868
|
|
1869 If a file's name is FOO, the names of its numbered backup versions are
|
|
1870 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~.
|
|
1871 Numeric backups (rather than FOO~) will be made if value of
|
|
1872 `version-control' is not the atom `never' and either there are already
|
|
1873 numeric versions of the file being backed up, or `version-control' is
|
|
1874 non-nil.
|
|
1875 We don't want excessive versions piling up, so there are variables
|
|
1876 `kept-old-versions', which tells XEmacs how many oldest versions to keep,
|
|
1877 and `kept-new-versions', which tells how many newest versions to keep.
|
|
1878 Defaults are 2 old versions and 2 new.
|
|
1879 `dired-kept-versions' controls dired's clean-directory (.) command.
|
|
1880 If `delete-old-versions' is nil, system will query user
|
|
1881 before trimming versions. Otherwise it does it silently."
|
|
1882 (interactive "p")
|
|
1883 (let ((modp (buffer-modified-p))
|
|
1884 (large (> (buffer-size) 50000))
|
|
1885 (make-backup-files (or (and make-backup-files (not (eq args 0)))
|
|
1886 (memq args '(16 64)))))
|
|
1887 (and modp (memq args '(16 64)) (setq buffer-backed-up nil))
|
|
1888 (if (and modp large) (message "Saving file %s..."
|
|
1889 (buffer-file-name)))
|
|
1890 (basic-save-buffer)
|
|
1891 (and modp (memq args '(4 64)) (setq buffer-backed-up nil))))
|
|
1892
|
|
1893 (defun delete-auto-save-file-if-necessary (&optional force)
|
|
1894 "Delete auto-save file for current buffer if `delete-auto-save-files' is t.
|
|
1895 Normally delete only if the file was written by this XEmacs
|
|
1896 since the last real save, but optional arg FORCE non-nil means delete anyway."
|
|
1897 (and buffer-auto-save-file-name delete-auto-save-files
|
|
1898 (not (string= buffer-file-name buffer-auto-save-file-name))
|
|
1899 (or force (recent-auto-save-p))
|
|
1900 (progn
|
|
1901 (condition-case ()
|
|
1902 (delete-file buffer-auto-save-file-name)
|
|
1903 (file-error nil))
|
|
1904 (set-buffer-auto-saved))))
|
|
1905
|
|
1906 ;; XEmacs change (from Sun)
|
|
1907 ;; used to communicate with continue-save-buffer:
|
|
1908 (defvar continue-save-buffer-hooks-tail nil)
|
|
1909
|
|
1910 ;; Not in FSFmacs
|
|
1911 (defun basic-write-file-data (realname truename)
|
|
1912 ;; call the hooks until the bytes are put
|
|
1913 ;; call write-region as a last resort
|
|
1914 (let ((region-written nil)
|
|
1915 (hooks write-file-data-hooks))
|
|
1916 (while (and hooks (not region-written))
|
|
1917 (setq region-written (funcall (car hooks) realname)
|
|
1918 hooks (cdr hooks)))
|
|
1919 (if (not region-written)
|
|
1920 (write-region (point-min) (point-max) realname nil t truename))))
|
|
1921
|
|
1922 (put 'after-save-hook 'permanent-local t)
|
|
1923 (defvar after-save-hook nil
|
|
1924 "Normal hook that is run after a buffer is saved to its file.
|
|
1925 These hooks are considered to pertain to the visited file.
|
|
1926 So this list is cleared if you change the visited file name.")
|
|
1927
|
|
1928 (defun files-fetch-hook-value (hook)
|
|
1929 (let ((localval (symbol-value hook))
|
|
1930 (globalval (default-value hook)))
|
|
1931 (if (memq t localval)
|
|
1932 (setq localval (append (delq t localval) (delq t globalval))))
|
|
1933 localval))
|
|
1934
|
|
1935 (defun basic-save-buffer ()
|
|
1936 "Save the current buffer in its visited file, if it has been modified.
|
|
1937 After saving the buffer, run `after-save-hook'."
|
|
1938 (interactive)
|
|
1939 (save-excursion
|
|
1940 ;; In an indirect buffer, save its base buffer instead.
|
|
1941 (if (buffer-base-buffer)
|
|
1942 (set-buffer (buffer-base-buffer)))
|
|
1943 (if (buffer-modified-p)
|
|
1944 (let ((recent-save (recent-auto-save-p)))
|
|
1945 ;; On VMS, rename file and buffer to get rid of version number.
|
|
1946 (if (and (eq system-type 'vax-vms)
|
|
1947 (not (string= buffer-file-name
|
|
1948 (file-name-sans-versions buffer-file-name))))
|
|
1949 (let (buffer-new-name)
|
|
1950 ;; Strip VMS version number before save.
|
|
1951 (setq buffer-file-name
|
|
1952 (file-name-sans-versions buffer-file-name))
|
|
1953 ;; Construct a (unique) buffer name to correspond.
|
|
1954 (let ((buf (create-file-buffer (downcase buffer-file-name))))
|
|
1955 (setq buffer-new-name (buffer-name buf))
|
|
1956 (kill-buffer buf))
|
|
1957 (rename-buffer buffer-new-name)))
|
|
1958 ;; If buffer has no file name, ask user for one.
|
|
1959 (or buffer-file-name
|
|
1960 (let ((filename
|
|
1961 (expand-file-name
|
|
1962 (read-file-name "File to save in: ") nil)))
|
|
1963 (and (file-exists-p filename)
|
|
1964 (or (y-or-n-p (format "File `%s' exists; overwrite? "
|
|
1965 filename))
|
|
1966 (error "Canceled")))
|
|
1967 (set-visited-file-name filename)))
|
|
1968 (or (verify-visited-file-modtime (current-buffer))
|
|
1969 (not (file-exists-p buffer-file-name))
|
|
1970 (yes-or-no-p
|
|
1971 (format "%s has changed since visited or saved. Save anyway? "
|
|
1972 (file-name-nondirectory buffer-file-name)))
|
|
1973 (error "Save not confirmed"))
|
|
1974 (save-restriction
|
|
1975 (widen)
|
|
1976 (and (> (point-max) 1)
|
|
1977 (/= (char-after (1- (point-max))) ?\n)
|
|
1978 (not (and (eq selective-display t)
|
|
1979 (= (char-after (1- (point-max))) ?\r)))
|
|
1980 (or (eq require-final-newline t)
|
|
1981 (and require-final-newline
|
|
1982 (y-or-n-p
|
|
1983 (format "Buffer %s does not end in newline. Add one? "
|
|
1984 (buffer-name)))))
|
|
1985 (save-excursion
|
|
1986 (goto-char (point-max))
|
|
1987 (insert ?\n)))
|
|
1988 ;;
|
|
1989 ;; Run the write-file-hooks until one returns non-null.
|
|
1990 ;; Bind after-save-hook to nil while running the
|
|
1991 ;; write-file-hooks so that if this function is called
|
|
1992 ;; recursively (from inside a write-file-hook) the
|
|
1993 ;; after-hooks will only get run once (from the
|
|
1994 ;; outermost call).
|
|
1995 ;;
|
|
1996 ;; Ugh, have to duplicate logic of run-hook-with-args-until-success
|
|
1997 (let ((hooks (append (files-fetch-hook-value 'write-contents-hooks)
|
|
1998 (files-fetch-hook-value
|
|
1999 'local-write-file-hooks)
|
|
2000 (files-fetch-hook-value 'write-file-hooks)))
|
|
2001 (after-save-hook nil)
|
|
2002 (local-write-file-hooks nil)
|
|
2003 (write-contents-hooks nil)
|
|
2004 (write-file-hooks nil)
|
|
2005 done)
|
|
2006 (while (and hooks
|
|
2007 (let ((continue-save-buffer-hooks-tail hooks))
|
|
2008 (not (setq done (funcall (car hooks))))))
|
|
2009 (setq hooks (cdr hooks)))
|
|
2010 ;; If a hook returned t, file is already "written".
|
|
2011 ;; Otherwise, write it the usual way now.
|
|
2012 (if (not done)
|
|
2013 (basic-save-buffer-1)))
|
|
2014 ;; XEmacs: next two clauses (buffer-file-number setting and
|
|
2015 ;; set-file-modes) moved into basic-save-buffer-1.
|
|
2016 )
|
|
2017 ;; If the auto-save file was recent before this command,
|
|
2018 ;; delete it now.
|
|
2019 (delete-auto-save-file-if-necessary recent-save)
|
|
2020 ;; Support VC `implicit' locking.
|
|
2021 (vc-after-save)
|
|
2022 (run-hooks 'after-save-hook))
|
|
2023 (message "(No changes need to be saved)"))))
|
|
2024
|
|
2025 ;; This does the "real job" of writing a buffer into its visited file
|
|
2026 ;; and making a backup file. This is what is normally done
|
|
2027 ;; but inhibited if one of write-file-hooks returns non-nil.
|
|
2028 ;; It returns a value to store in setmodes.
|
|
2029 (defun basic-save-buffer-1 ()
|
|
2030 (let (setmodes tempsetmodes)
|
|
2031 (if (not (file-writable-p buffer-file-name))
|
|
2032 (let ((dir (file-name-directory buffer-file-name)))
|
|
2033 (if (not (file-directory-p dir))
|
|
2034 (error "%s is not a directory" dir)
|
|
2035 (if (not (file-exists-p buffer-file-name))
|
|
2036 (error "Directory %s write-protected" dir)
|
|
2037 (if (yes-or-no-p
|
|
2038 (format "File %s is write-protected; try to save anyway? "
|
|
2039 (file-name-nondirectory
|
|
2040 buffer-file-name)))
|
|
2041 (setq tempsetmodes t)
|
|
2042 (error
|
|
2043 "Attempt to save to a file which you aren't allowed to write"))))))
|
|
2044 (or buffer-backed-up
|
|
2045 (setq setmodes (backup-buffer)))
|
|
2046 (let ((dir (file-name-directory buffer-file-name)))
|
|
2047 (if (and file-precious-flag
|
|
2048 (file-writable-p dir))
|
|
2049 ;; If file is precious, write temp name, then rename it.
|
|
2050 ;; This requires write access to the containing dir,
|
|
2051 ;; which is why we don't try it if we don't have that access.
|
|
2052 (let ((realname buffer-file-name)
|
|
2053 tempname nogood i succeed
|
|
2054 (old-modtime (visited-file-modtime)))
|
|
2055 (setq i 0)
|
|
2056 (setq nogood t)
|
|
2057 ;; Find the temporary name to write under.
|
|
2058 (while nogood
|
|
2059 (setq tempname (format "%s#tmp#%d" dir i))
|
|
2060 (setq nogood (file-exists-p tempname))
|
|
2061 (setq i (1+ i)))
|
|
2062 (unwind-protect
|
|
2063 (progn (clear-visited-file-modtime)
|
|
2064 (write-region (point-min) (point-max)
|
|
2065 tempname nil realname
|
|
2066 buffer-file-truename)
|
|
2067 (setq succeed t))
|
|
2068 ;; If writing the temp file fails,
|
|
2069 ;; delete the temp file.
|
|
2070 (or succeed
|
|
2071 (progn
|
|
2072 (delete-file tempname)
|
|
2073 (set-visited-file-modtime old-modtime))))
|
|
2074 ;; Since we have created an entirely new file
|
|
2075 ;; and renamed it, make sure it gets the
|
|
2076 ;; right permission bits set.
|
|
2077 (setq setmodes (file-modes buffer-file-name))
|
|
2078 ;; We succeeded in writing the temp file,
|
|
2079 ;; so rename it.
|
|
2080 (rename-file tempname buffer-file-name t))
|
|
2081 ;; If file not writable, see if we can make it writable
|
|
2082 ;; temporarily while we write it.
|
|
2083 ;; But no need to do so if we have just backed it up
|
|
2084 ;; (setmodes is set) because that says we're superseding.
|
|
2085 (cond ((and tempsetmodes (not setmodes))
|
|
2086 ;; Change the mode back, after writing.
|
|
2087 (setq setmodes (file-modes buffer-file-name))
|
|
2088 (set-file-modes buffer-file-name 511)))
|
|
2089 (basic-write-file-data buffer-file-name buffer-file-truename)))
|
|
2090 (setq buffer-file-number
|
|
2091 (if buffer-file-name
|
|
2092 (nth 10 (file-attributes buffer-file-name))
|
|
2093 nil))
|
|
2094 (if setmodes
|
|
2095 (condition-case ()
|
|
2096 (set-file-modes buffer-file-name setmodes)
|
|
2097 (error nil)))))
|
|
2098
|
|
2099 ;; XEmacs change, from Sun
|
|
2100 (defun continue-save-buffer ()
|
|
2101 "Provide a clean way for a write-file-hook to wrap AROUND
|
|
2102 the execution of the remaining hooks and writing to disk.
|
|
2103 Do not call this function except from a functions
|
|
2104 on the write-file-hooks or write-contents-hooks list.
|
|
2105 A hook that calls this function must return non-nil,
|
|
2106 to signal completion to its caller. continue-save-buffer
|
|
2107 always returns non-nil."
|
|
2108 (let ((hooks (cdr (or continue-save-buffer-hooks-tail
|
|
2109 (error
|
|
2110 "continue-save-buffer called outside a write-file-hook!"))))
|
|
2111 (done nil))
|
|
2112 ;; Do something like this:
|
|
2113 ;; (let ((write-file-hooks hooks)) (basic-save-buffer))
|
|
2114 ;; First run the rest of the hooks.
|
|
2115 (while (and hooks
|
|
2116 (let ((continue-save-buffer-hooks-tail hooks))
|
|
2117 (not (setq done (funcall (car hooks))))))
|
|
2118 (setq hooks (cdr hooks)))
|
|
2119 ;;
|
|
2120 ;; If a hook returned t, file is already "written".
|
|
2121 (if (not done)
|
|
2122 (basic-save-buffer-1))
|
|
2123 'continue-save-buffer))
|
|
2124
|
|
2125 (defun save-some-buffers (&optional arg exiting)
|
|
2126 "Save some modified file-visiting buffers. Asks user about each one.
|
|
2127 Optional argument (the prefix) non-nil means save all with no questions.
|
|
2128 Optional second argument EXITING means ask about certain non-file buffers
|
|
2129 as well as about file buffers."
|
|
2130 (interactive "P")
|
|
2131 (save-window-excursion
|
|
2132 (let ((files-done
|
|
2133 (map-y-or-n-p
|
|
2134 (function
|
|
2135 (lambda (buffer)
|
|
2136 (and (buffer-modified-p buffer)
|
|
2137 (not (buffer-base-buffer buffer))
|
|
2138 ;; XEmacs addition:
|
|
2139 (not (symbol-value-in-buffer 'save-buffers-skip buffer))
|
|
2140 (or
|
|
2141 (buffer-file-name buffer)
|
|
2142 (and exiting
|
|
2143 (progn
|
|
2144 (set-buffer buffer)
|
|
2145 (and buffer-offer-save (> (buffer-size) 0)))))
|
|
2146 (if arg
|
|
2147 t
|
|
2148 (if (buffer-file-name buffer)
|
|
2149 (format "Save file %s? "
|
|
2150 (buffer-file-name buffer))
|
|
2151 (format "Save buffer %s? "
|
|
2152 (buffer-name buffer)))))))
|
|
2153 (function
|
|
2154 (lambda (buffer)
|
|
2155 (set-buffer buffer)
|
|
2156 (condition-case ()
|
|
2157 (save-buffer)
|
|
2158 (error nil))))
|
|
2159 (buffer-list)
|
|
2160 '("buffer" "buffers" "save")
|
|
2161 ;instead of this we just say "yes all", "no all", etc.
|
|
2162 ;"save all the rest"
|
|
2163 ;"save only this buffer" "save no more buffers")
|
|
2164 ; this is rather bogus. --ben
|
|
2165 ; (it makes the dialog box too big, and you get an error
|
|
2166 ; "wrong type argument: framep, nil" when you hit q after
|
|
2167 ; choosing the option from the dialog box)
|
|
2168 ; (list (list ?\C-r (lambda (buf)
|
|
2169 ; (view-buffer buf)
|
|
2170 ; (setq view-exit-action
|
|
2171 ; '(lambda (ignore)
|
|
2172 ; (exit-recursive-edit)))
|
|
2173 ; (recursive-edit)
|
|
2174 ; ;; Return nil to ask about BUF again.
|
|
2175 ; nil)
|
|
2176 ; "display the current buffer"))
|
|
2177 ))
|
|
2178 (abbrevs-done
|
|
2179 (and save-abbrevs abbrevs-changed
|
|
2180 (progn
|
|
2181 (if (or arg
|
|
2182 (y-or-n-p (format "Save abbrevs in %s? " abbrev-file-name)))
|
|
2183 (write-abbrev-file nil))
|
|
2184 ;; Don't keep bothering user if he says no.
|
|
2185 (setq abbrevs-changed nil)
|
|
2186 t))))
|
|
2187 (or (> files-done 0) abbrevs-done
|
|
2188 (message "(No files need saving)")))))
|
|
2189
|
|
2190
|
|
2191 (defun not-modified (&optional arg)
|
|
2192 "Mark current buffer as unmodified, not needing to be saved.
|
|
2193 With prefix arg, mark buffer as modified, so \\[save-buffer] will save.
|
|
2194
|
|
2195 It is not a good idea to use this function in Lisp programs, because it
|
|
2196 prints a message in the minibuffer. Instead, use `set-buffer-modified-p'."
|
|
2197 (interactive "_P")
|
|
2198 (if arg ;; rewritten for I18N3 snarfing
|
|
2199 (message "Modification-flag set")
|
|
2200 (message "Modification-flag cleared"))
|
|
2201 (set-buffer-modified-p arg))
|
|
2202
|
|
2203 (defun toggle-read-only (&optional arg)
|
|
2204 "Change whether this buffer is visiting its file read-only.
|
|
2205 With arg, set read-only iff arg is positive."
|
|
2206 (interactive "_P")
|
|
2207 (setq buffer-read-only
|
|
2208 (if (null arg)
|
|
2209 (not buffer-read-only)
|
|
2210 (> (prefix-numeric-value arg) 0)))
|
|
2211 ;; Force modeline redisplay
|
|
2212 (redraw-modeline))
|
|
2213
|
|
2214 (defun insert-file (filename)
|
|
2215 "Insert contents of file FILENAME into buffer after point.
|
|
2216 Set mark after the inserted text.
|
|
2217
|
|
2218 This function is meant for the user to run interactively.
|
|
2219 Don't call it from programs! Use `insert-file-contents' instead.
|
|
2220 \(Its calling sequence is different; see its documentation)."
|
|
2221 (interactive "*fInsert file: ")
|
|
2222 (if (file-directory-p filename)
|
|
2223 (signal 'file-error (list "Opening input file" "file is a directory"
|
|
2224 filename)))
|
|
2225 (let ((tem (insert-file-contents filename)))
|
|
2226 (push-mark (+ (point) (car (cdr tem))))))
|
|
2227
|
|
2228 (defun append-to-file (start end filename)
|
|
2229 "Append the contents of the region to the end of file FILENAME.
|
|
2230 When called from a function, expects three arguments,
|
|
2231 START, END and FILENAME. START and END are buffer positions
|
|
2232 saying what text to write."
|
|
2233 (interactive "r\nFAppend to file: ")
|
|
2234 (write-region start end filename t))
|
|
2235
|
|
2236 (defun file-newest-backup (filename)
|
|
2237 "Return most recent backup file for FILENAME or nil if no backups exist."
|
|
2238 (let* ((filename (expand-file-name filename))
|
|
2239 (file (file-name-nondirectory filename))
|
|
2240 (dir (file-name-directory filename))
|
|
2241 (comp (file-name-all-completions file dir))
|
|
2242 newest)
|
|
2243 (while comp
|
|
2244 (setq file (concat dir (car comp))
|
|
2245 comp (cdr comp))
|
|
2246 (if (and (backup-file-name-p file)
|
|
2247 (or (null newest) (file-newer-than-file-p file newest)))
|
|
2248 (setq newest file)))
|
|
2249 newest))
|
|
2250
|
|
2251 (defun rename-uniquely ()
|
|
2252 "Rename current buffer to a similar name not already taken.
|
|
2253 This function is useful for creating multiple shell process buffers
|
|
2254 or multiple mail buffers, etc."
|
|
2255 (interactive)
|
|
2256 (save-match-data
|
|
2257 (let* ((base-name (if (and (string-match "<[0-9]+>\\'" (buffer-name))
|
|
2258 (not (and buffer-file-name
|
|
2259 (string= (buffer-name)
|
|
2260 (file-name-nondirectory
|
|
2261 buffer-file-name)))))
|
|
2262 ;; If the existing buffer name has a <NNN>,
|
|
2263 ;; which isn't part of the file name (if any),
|
|
2264 ;; then get rid of that.
|
|
2265 (substring (buffer-name) 0 (match-beginning 0))
|
|
2266 (buffer-name)))
|
|
2267 (new-buf (generate-new-buffer base-name))
|
|
2268 (name (buffer-name new-buf)))
|
|
2269 (kill-buffer new-buf)
|
|
2270 (rename-buffer name)
|
|
2271 (redraw-modeline))))
|
|
2272
|
|
2273 (defun make-directory-path (path)
|
|
2274 "Create all the directories along path that don't exist yet."
|
|
2275 (interactive "Fdirectory path to create: ")
|
|
2276 (make-directory path t))
|
|
2277
|
|
2278 (defun make-directory (dir &optional parents)
|
|
2279 "Create the directory DIR and any nonexistent parent dirs.
|
|
2280 Interactively, the default choice of directory to create
|
|
2281 is the current default directory for file names.
|
|
2282 That is useful when you have visited a file in a nonexistent directory.
|
|
2283
|
|
2284 Noninteractively, the second (optional) argument PARENTS says whether
|
|
2285 to create parent directories if they don't exist."
|
|
2286 (interactive (list (let ((current-prefix-arg current-prefix-arg))
|
|
2287 (read-directory-name "Create directory: "))
|
|
2288 current-prefix-arg))
|
|
2289 (let ((handler (find-file-name-handler dir 'make-directory)))
|
|
2290 (if handler
|
|
2291 (funcall handler 'make-directory dir parents)
|
|
2292 (if (not parents)
|
|
2293 (make-directory-internal dir)
|
|
2294 (let ((dir (directory-file-name (expand-file-name dir)))
|
|
2295 create-list)
|
|
2296 (while (not (file-exists-p dir))
|
|
2297 (setq create-list (cons dir create-list)
|
|
2298 dir (directory-file-name (file-name-directory dir))))
|
|
2299 (while create-list
|
|
2300 (make-directory-internal (car create-list))
|
|
2301 (setq create-list (cdr create-list))))))))
|
|
2302
|
|
2303 (put 'revert-buffer-function 'permanent-local t)
|
|
2304 (defvar revert-buffer-function nil
|
|
2305 "Function to use to revert this buffer, or nil to do the default.
|
|
2306 The function receives two arguments IGNORE-AUTO and NOCONFIRM,
|
|
2307 which are the arguments that `revert-buffer' received.")
|
|
2308
|
|
2309 (put 'revert-buffer-insert-file-contents-function 'permanent-local t)
|
|
2310 (defvar revert-buffer-insert-file-contents-function nil
|
|
2311 "Function to use to insert contents when reverting this buffer.
|
|
2312 Gets two args, first the nominal file name to use,
|
|
2313 and second, t if reading the auto-save file.")
|
|
2314
|
|
2315 (defvar before-revert-hook nil
|
|
2316 "Normal hook for `revert-buffer' to run before reverting.
|
|
2317 If `revert-buffer-function' is used to override the normal revert
|
|
2318 mechanism, this hook is not used.")
|
|
2319
|
|
2320 (defvar after-revert-hook nil
|
|
2321 "Normal hook for `revert-buffer' to run after reverting.
|
|
2322 Note that the hook value that it runs is the value that was in effect
|
|
2323 before reverting; that makes a difference if you have buffer-local
|
|
2324 hook functions.
|
|
2325
|
|
2326 If `revert-buffer-function' is used to override the normal revert
|
|
2327 mechanism, this hook is not used.")
|
|
2328
|
|
2329 (defun revert-buffer (&optional ignore-auto noconfirm)
|
|
2330 "Replace the buffer text with the text of the visited file on disk.
|
|
2331 This undoes all changes since the file was visited or saved.
|
|
2332 With a prefix argument, offer to revert from latest auto-save file, if
|
|
2333 that is more recent than the visited file.
|
|
2334 When called from Lisp, the first argument is IGNORE-AUTO; only offer
|
|
2335 to revert from the auto-save file when this is nil. Note that the
|
|
2336 sense of this argument is the reverse of the prefix argument, for the
|
|
2337 sake of backward compatibility. IGNORE-AUTO is optional, defaulting
|
|
2338 to nil.
|
|
2339
|
|
2340 Optional second argument NOCONFIRM means don't ask for confirmation at
|
|
2341 all.
|
|
2342
|
|
2343 If the value of `revert-buffer-function' is non-nil, it is called to
|
|
2344 do the work.
|
|
2345
|
|
2346 The default revert function runs the hook `before-revert-hook' at the
|
|
2347 beginning and `after-revert-hook' at the end."
|
|
2348 ;; I admit it's odd to reverse the sense of the prefix argument, but
|
|
2349 ;; there is a lot of code out there which assumes that the first
|
|
2350 ;; argument should be t to avoid consulting the auto-save file, and
|
|
2351 ;; there's no straightforward way to encourage authors to notice a
|
|
2352 ;; reversal of the argument sense. So I'm just changing the user
|
|
2353 ;; interface, but leaving the programmatic interface the same.
|
|
2354 (interactive (list (not current-prefix-arg)))
|
|
2355 (if revert-buffer-function
|
|
2356 (funcall revert-buffer-function ignore-auto noconfirm)
|
|
2357 (let* ((opoint (point))
|
|
2358 (auto-save-p (and (not ignore-auto)
|
|
2359 (recent-auto-save-p)
|
|
2360 buffer-auto-save-file-name
|
|
2361 (file-readable-p buffer-auto-save-file-name)
|
|
2362 (y-or-n-p
|
|
2363 "Buffer has been auto-saved recently. Revert from auto-save file? ")))
|
|
2364 (file-name (if auto-save-p
|
|
2365 buffer-auto-save-file-name
|
|
2366 buffer-file-name)))
|
|
2367 (cond ((null file-name)
|
|
2368 (error "Buffer does not seem to be associated with any file"))
|
|
2369 ((or noconfirm
|
|
2370 (yes-or-no-p (format "Revert buffer from file %s? "
|
|
2371 file-name)))
|
|
2372 (run-hooks 'before-revert-hook)
|
|
2373 ;; If file was backed up but has changed since,
|
|
2374 ;; we shd make another backup.
|
|
2375 (and (not auto-save-p)
|
|
2376 (not (verify-visited-file-modtime (current-buffer)))
|
|
2377 (setq buffer-backed-up nil))
|
|
2378 ;; Get rid of all undo records for this buffer.
|
|
2379 (or (eq buffer-undo-list t)
|
|
2380 (setq buffer-undo-list nil))
|
|
2381 ;; Effectively copy the after-revert-hook status,
|
|
2382 ;; since after-find-file will clobber it.
|
|
2383 (let ((global-hook (default-value 'after-revert-hook))
|
|
2384 (local-hook-p (local-variable-p 'after-revert-hook
|
|
2385 (current-buffer)))
|
|
2386 (local-hook (and (local-variable-p 'after-revert-hook
|
|
2387 (current-buffer))
|
|
2388 after-revert-hook)))
|
|
2389 (let (buffer-read-only
|
|
2390 ;; Don't make undo records for the reversion.
|
|
2391 (buffer-undo-list t))
|
|
2392 (if revert-buffer-insert-file-contents-function
|
|
2393 (funcall revert-buffer-insert-file-contents-function
|
|
2394 file-name auto-save-p)
|
|
2395 (if (not (file-exists-p file-name))
|
|
2396 (error "File %s no longer exists!" file-name))
|
|
2397 ;; Bind buffer-file-name to nil
|
|
2398 ;; so that we don't try to lock the file.
|
|
2399 (let ((buffer-file-name nil))
|
|
2400 (or auto-save-p
|
|
2401 (unlock-buffer)))
|
|
2402 (widen)
|
|
2403 (insert-file-contents file-name (not auto-save-p)
|
|
2404 nil nil t)))
|
|
2405 (goto-char (min opoint (point-max)))
|
|
2406 ;; Recompute the truename in case changes in symlinks
|
|
2407 ;; have changed the truename.
|
|
2408 ;XEmacs: already done by insert-file-contents
|
|
2409 ;(compute-buffer-file-truename)
|
|
2410 (after-find-file nil nil t t)
|
|
2411 ;; Run after-revert-hook as it was before we reverted.
|
|
2412 (setq-default revert-buffer-internal-hook global-hook)
|
|
2413 (if local-hook-p
|
|
2414 (progn
|
|
2415 (make-local-variable 'revert-buffer-internal-hook)
|
|
2416 (setq revert-buffer-internal-hook local-hook))
|
|
2417 (kill-local-variable 'revert-buffer-internal-hook))
|
|
2418 (run-hooks 'revert-buffer-internal-hook))
|
|
2419 t)))))
|
|
2420
|
|
2421 (defun recover-file (file)
|
|
2422 "Visit file FILE, but get contents from its last auto-save file."
|
|
2423 ;; Actually putting the file name in the minibuffer should be used
|
|
2424 ;; only rarely.
|
|
2425 ;; Not just because users often use the default.
|
|
2426 (interactive "FRecover file: ")
|
|
2427 (setq file (expand-file-name file))
|
|
2428 (if (auto-save-file-name-p file)
|
|
2429 (error "%s is an auto-save file" file))
|
|
2430 (let ((file-name (let ((buffer-file-name file))
|
|
2431 (make-auto-save-file-name))))
|
|
2432 (cond ((if (file-exists-p file)
|
|
2433 (not (file-newer-than-file-p file-name file))
|
|
2434 (not (file-exists-p file-name)))
|
|
2435 (error "Auto-save file %s not current" file-name))
|
|
2436 ((save-window-excursion
|
|
2437 (if (not (eq system-type 'vax-vms))
|
|
2438 (with-output-to-temp-buffer "*Directory*"
|
|
2439 (buffer-disable-undo standard-output)
|
|
2440 (call-process "ls" nil standard-output nil
|
|
2441 (if (file-symlink-p file) "-lL" "-l")
|
|
2442 file file-name)))
|
|
2443 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
|
|
2444 (switch-to-buffer (find-file-noselect file t))
|
|
2445 (let ((buffer-read-only nil))
|
|
2446 (erase-buffer)
|
|
2447 (insert-file-contents file-name nil))
|
|
2448 (after-find-file nil nil t))
|
|
2449 (t (error "Recover-file cancelled.")))))
|
|
2450
|
|
2451 (defun recover-session ()
|
|
2452 "Recover auto save files from a previous Emacs session.
|
|
2453 This command first displays a Dired buffer showing you the
|
|
2454 previous sessions that you could recover from.
|
|
2455 To choose one, move point to the proper line and then type C-c C-c.
|
|
2456 Then you'll be asked about a number of files to recover."
|
|
2457 (interactive)
|
|
2458 (dired (concat auto-save-list-file-prefix "*"))
|
|
2459 (goto-char (point-min))
|
|
2460 (or (looking-at "Move to the session you want to recover,")
|
|
2461 (let ((inhibit-read-only t))
|
|
2462 (insert "Move to the session you want to recover,\n"
|
|
2463 "then type C-c C-c to select it.\n\n"
|
|
2464 "You can also delete some of these files;\n"
|
|
2465 "type d on a line to mark that file for deletion.\n\n")))
|
|
2466 (use-local-map (let ((map (make-sparse-keymap)))
|
|
2467 (set-keymap-parents map (list (current-local-map)))
|
|
2468 map))
|
|
2469 (define-key (current-local-map) "\C-c\C-c" 'recover-session-finish))
|
|
2470
|
|
2471 (defun recover-session-finish ()
|
|
2472 "Choose one saved session to recover auto-save files from.
|
|
2473 This command is used in the special Dired buffer created by
|
|
2474 \\[recover-session]."
|
|
2475 (interactive)
|
|
2476 ;; Get the name of the session file to recover from.
|
|
2477 (let ((file (dired-get-filename))
|
|
2478 files
|
|
2479 (buffer (get-buffer-create " *recover*")))
|
|
2480 ;; #### dired-do-flagged-delete in FSF.
|
|
2481 (dired-do-deletions t)
|
|
2482 (unwind-protect
|
|
2483 (save-excursion
|
|
2484 ;; Read in the auto-save-list file.
|
|
2485 (set-buffer buffer)
|
|
2486 (erase-buffer)
|
|
2487 (insert-file-contents file)
|
|
2488 ;; Loop thru the text of that file
|
|
2489 ;; and get out the names of the files to recover.
|
|
2490 (while (not (eobp))
|
|
2491 (let (thisfile autofile)
|
|
2492 (if (eolp)
|
|
2493 ;; This is a pair of lines for a non-file-visiting buffer.
|
|
2494 ;; Get the auto-save file name and manufacture
|
|
2495 ;; a "visited file name" from that.
|
|
2496 (progn
|
|
2497 (forward-line 1)
|
|
2498 (setq autofile
|
|
2499 (buffer-substring-no-properties
|
|
2500 (point)
|
|
2501 (save-excursion
|
|
2502 (end-of-line)
|
|
2503 (point))))
|
|
2504 (setq thisfile
|
|
2505 (expand-file-name
|
|
2506 (substring
|
|
2507 (file-name-nondirectory autofile)
|
|
2508 1 -1)
|
|
2509 (file-name-directory autofile)))
|
|
2510 (forward-line 1))
|
|
2511 ;; This pair of lines is a file-visiting
|
|
2512 ;; buffer. Use the visited file name.
|
|
2513 (progn
|
|
2514 (setq thisfile
|
|
2515 (buffer-substring-no-properties
|
|
2516 (point) (progn (end-of-line) (point))))
|
|
2517 (forward-line 1)
|
|
2518 (setq autofile
|
|
2519 (buffer-substring-no-properties
|
|
2520 (point) (progn (end-of-line) (point))))
|
|
2521 (forward-line 1)))
|
|
2522 ;; Ignore a file if its auto-save file does not exist now.
|
|
2523 (if (file-exists-p autofile)
|
|
2524 (setq files (cons thisfile files)))))
|
|
2525 (setq files (nreverse files))
|
|
2526 ;; The file contains a pair of line for each auto-saved buffer.
|
|
2527 ;; The first line of the pair contains the visited file name
|
|
2528 ;; or is empty if the buffer was not visiting a file.
|
|
2529 ;; The second line is the auto-save file name.
|
|
2530 (if files
|
|
2531 (map-y-or-n-p "Recover %s? "
|
|
2532 (lambda (file)
|
|
2533 (condition-case nil
|
|
2534 (save-excursion (recover-file file))
|
|
2535 (error
|
|
2536 "Failed to recover `%s'" file)))
|
|
2537 files
|
|
2538 '("file" "files" "recover"))
|
|
2539 (message "No files can be recovered from this session now")))
|
|
2540 (kill-buffer buffer))))
|
|
2541
|
|
2542 (defun kill-some-buffers ()
|
|
2543 "For each buffer, ask whether to kill it."
|
|
2544 (interactive)
|
|
2545 (let ((list (buffer-list)))
|
|
2546 (while list
|
|
2547 (let* ((buffer (car list))
|
|
2548 (name (buffer-name buffer)))
|
|
2549 (and (not (string-equal name ""))
|
|
2550 (/= (aref name 0) ? )
|
|
2551 (yes-or-no-p
|
|
2552 (format
|
|
2553 (if (buffer-modified-p buffer)
|
|
2554 (gettext "Buffer %s HAS BEEN EDITED. Kill? ")
|
|
2555 (gettext "Buffer %s is unmodified. Kill? "))
|
|
2556 name))
|
|
2557 (kill-buffer buffer)))
|
|
2558 (setq list (cdr list)))))
|
|
2559
|
|
2560 (defun auto-save-mode (arg)
|
|
2561 "Toggle auto-saving of contents of current buffer.
|
|
2562 With prefix argument ARG, turn auto-saving on if positive, else off."
|
|
2563 (interactive "P")
|
|
2564 (setq buffer-auto-save-file-name
|
|
2565 (and (if (null arg)
|
|
2566 (or (not buffer-auto-save-file-name)
|
|
2567 ;; If autosave is off because buffer has shrunk,
|
|
2568 ;; then toggling should turn it on.
|
|
2569 (< buffer-saved-size 0))
|
|
2570 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))
|
|
2571 (if (and buffer-file-name auto-save-visited-file-name
|
|
2572 (not buffer-read-only))
|
|
2573 buffer-file-name
|
|
2574 (make-auto-save-file-name))))
|
|
2575 ;; If -1 was stored here, to temporarily turn off saving,
|
|
2576 ;; turn it back on.
|
|
2577 (and (< buffer-saved-size 0)
|
|
2578 (setq buffer-saved-size 0))
|
|
2579 (if (interactive-p)
|
|
2580 (if buffer-auto-save-file-name ;; rewritten for I18N3 snarfing
|
|
2581 (message "Auto-save on (in this buffer)")
|
|
2582 (message "Auto-save off (in this buffer)")))
|
|
2583 buffer-auto-save-file-name)
|
|
2584
|
|
2585 (defun rename-auto-save-file ()
|
|
2586 "Adjust current buffer's auto save file name for current conditions.
|
|
2587 Also rename any existing auto save file, if it was made in this session."
|
|
2588 (let ((osave buffer-auto-save-file-name))
|
|
2589 (setq buffer-auto-save-file-name
|
|
2590 (make-auto-save-file-name))
|
|
2591 (if (and osave buffer-auto-save-file-name
|
|
2592 (not (string= buffer-auto-save-file-name buffer-file-name))
|
|
2593 (not (string= buffer-auto-save-file-name osave))
|
|
2594 (file-exists-p osave)
|
|
2595 (recent-auto-save-p))
|
|
2596 (rename-file osave buffer-auto-save-file-name t))))
|
|
2597
|
|
2598 ;; see also ../packages/auto-save.el
|
|
2599 (defun make-auto-save-file-name (&optional filename)
|
|
2600 "Return file name to use for auto-saves of current buffer.
|
|
2601 Does not consider `auto-save-visited-file-name' as that variable is checked
|
|
2602 before calling this function. You can redefine this for customization.
|
|
2603 See also `auto-save-file-name-p'."
|
|
2604 (let ((fname (or filename buffer-file-name))
|
|
2605 name)
|
|
2606 (setq name
|
|
2607 (if fname
|
|
2608 (concat (file-name-directory fname)
|
|
2609 "#"
|
|
2610 (file-name-nondirectory fname)
|
|
2611 "#")
|
|
2612
|
|
2613 ;; Deal with buffers that don't have any associated files. (Mail
|
|
2614 ;; mode tends to create a good number of these.)
|
|
2615
|
|
2616 (let ((buffer-name (buffer-name))
|
|
2617 (limit 0))
|
|
2618 ;; Use technique from Sebastian Kremer's auto-save
|
|
2619 ;; package to turn slashes into \\!. This ensures that
|
|
2620 ;; the auto-save buffer name is unique.
|
|
2621
|
|
2622 ;; #### - yuck! yuck! yuck! move this functionality
|
|
2623 ;; somewhere else and make the name translation customizable.
|
|
2624 ;; Using "\!" as part of a filename on a UNIX filesystem is nearly
|
|
2625 ;; IMPOSSIBLE to get past a shell parser. -stig
|
|
2626
|
|
2627 (while (string-match "[/\\]" buffer-name limit)
|
|
2628 (setq buffer-name
|
|
2629 (concat (substring buffer-name 0 (match-beginning 0))
|
|
2630 (if (string= (substring buffer-name
|
|
2631 (match-beginning 0)
|
|
2632 (match-end 0))
|
|
2633 "/")
|
|
2634 "\\!"
|
|
2635 "\\\\")
|
|
2636 (substring buffer-name (match-end 0))))
|
|
2637 (setq limit (1+ (match-end 0))))
|
|
2638
|
|
2639 ;; (expand-file-name (format "#%s#%s#" (buffer-name) (make-temp-name "")))
|
|
2640
|
|
2641 ;; jwz: putting the emacs PID in the auto-save file name
|
|
2642 ;; is bad news, because that defeats auto-save-recovery of
|
|
2643 ;; *mail* buffers -- the (sensible) code in sendmail.el
|
|
2644 ;; calls (make-auto-save-file-name) to determine whether
|
|
2645 ;; there is unsent, auto-saved mail to recover. If that
|
|
2646 ;; mail came from a previous emacs process (far and away
|
|
2647 ;; the most likely case) then this can never succeed as
|
|
2648 ;; the pid differs.
|
|
2649
|
|
2650 (expand-file-name (format "#%s#" buffer-name)))
|
|
2651 ))
|
|
2652 ;; don't try to write auto-save files in unwritable places. Unless
|
|
2653 ;; there's already an autosave file here, put ours somewhere safe. --Stig
|
|
2654 (if (or (file-writable-p name)
|
|
2655 (file-exists-p name))
|
|
2656 name
|
|
2657 (expand-file-name (concat "~/" (file-name-nondirectory name))))))
|
|
2658
|
|
2659 (defun auto-save-file-name-p (filename)
|
|
2660 "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'.
|
|
2661 FILENAME should lack slashes.
|
|
2662 You can redefine this for customization."
|
|
2663 (string-match "\\`#.*#\\'" filename))
|
|
2664
|
|
2665 (defconst list-directory-brief-switches
|
|
2666 (if (eq system-type 'vax-vms) "" "-CF")
|
|
2667 "*Switches for list-directory to pass to `ls' for brief listing,")
|
|
2668
|
|
2669 (defconst list-directory-verbose-switches
|
|
2670 (if (eq system-type 'vax-vms)
|
|
2671 "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)"
|
|
2672 "-l")
|
|
2673 "*Switches for list-directory to pass to `ls' for verbose listing,")
|
|
2674
|
|
2675 (defun list-directory (dirname &optional verbose)
|
|
2676 "Display a list of files in or matching DIRNAME, a la `ls'.
|
|
2677 DIRNAME is globbed by the shell if necessary.
|
|
2678 Prefix arg (second arg if noninteractive) means supply -l switch to `ls'.
|
|
2679 Actions controlled by variables `list-directory-brief-switches'
|
|
2680 and `list-directory-verbose-switches'."
|
|
2681 (interactive (let ((pfx current-prefix-arg))
|
|
2682 (list (read-file-name (if pfx (gettext "List directory (verbose): ")
|
|
2683 (gettext "List directory (brief): "))
|
|
2684 nil default-directory nil)
|
|
2685 pfx)))
|
|
2686 (let ((switches (if verbose list-directory-verbose-switches
|
|
2687 list-directory-brief-switches)))
|
|
2688 (or dirname (setq dirname default-directory))
|
|
2689 (setq dirname (expand-file-name dirname))
|
|
2690 (with-output-to-temp-buffer "*Directory*"
|
|
2691 (buffer-disable-undo standard-output)
|
|
2692 (princ "Directory ")
|
|
2693 (princ dirname)
|
|
2694 (terpri)
|
|
2695 (save-excursion
|
|
2696 (set-buffer "*Directory*")
|
|
2697 (setq default-directory (file-name-directory dirname))
|
|
2698 (let ((wildcard (not (file-directory-p dirname))))
|
|
2699 (insert-directory dirname switches wildcard (not wildcard)))))))
|
|
2700
|
|
2701 (defvar insert-directory-program "ls"
|
|
2702 "Absolute or relative name of the `ls' program used by `insert-directory'.")
|
|
2703
|
|
2704 ;; insert-directory
|
|
2705 ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and
|
|
2706 ;; FULL-DIRECTORY-P is nil.
|
|
2707 ;; The single line of output must display FILE's name as it was
|
|
2708 ;; given, namely, an absolute path name.
|
|
2709 ;; - must insert exactly one line for each file if WILDCARD or
|
|
2710 ;; FULL-DIRECTORY-P is t, plus one optional "total" line
|
|
2711 ;; before the file lines, plus optional text after the file lines.
|
|
2712 ;; Lines are delimited by "\n", so filenames containing "\n" are not
|
|
2713 ;; allowed.
|
|
2714 ;; File lines should display the basename.
|
|
2715 ;; - must be consistent with
|
|
2716 ;; - functions dired-move-to-filename, (these two define what a file line is)
|
|
2717 ;; dired-move-to-end-of-filename,
|
|
2718 ;; dired-between-files, (shortcut for (not (dired-move-to-filename)))
|
|
2719 ;; dired-insert-headerline
|
|
2720 ;; dired-after-subdir-garbage (defines what a "total" line is)
|
|
2721 ;; - variable dired-subdir-regexp
|
|
2722 (defun insert-directory (file switches &optional wildcard full-directory-p)
|
|
2723 "Insert directory listing for FILE, formatted according to SWITCHES.
|
|
2724 Leaves point after the inserted text.
|
|
2725 SWITCHES may be a string of options, or a list of strings.
|
|
2726 Optional third arg WILDCARD means treat FILE as shell wildcard.
|
|
2727 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
|
|
2728 switches do not contain `d', so that a full listing is expected.
|
|
2729
|
|
2730 This works by running a directory listing program
|
|
2731 whose name is in the variable `insert-directory-program'.
|
|
2732 If WILDCARD, it also runs the shell specified by `shell-file-name'."
|
|
2733 ;; We need the directory in order to find the right handler.
|
|
2734 (let ((handler (find-file-name-handler (expand-file-name file)
|
|
2735 'insert-directory)))
|
|
2736 (if handler
|
|
2737 (funcall handler 'insert-directory file switches
|
|
2738 wildcard full-directory-p)
|
|
2739 (if (eq system-type 'vax-vms)
|
|
2740 (vms-read-directory file switches (current-buffer))
|
|
2741 (if wildcard
|
|
2742 ;; Run ls in the directory of the file pattern we asked for.
|
|
2743 (let ((default-directory
|
|
2744 (if (file-name-absolute-p file)
|
|
2745 (file-name-directory file)
|
|
2746 (file-name-directory (expand-file-name file))))
|
|
2747 (pattern (file-name-nondirectory file))
|
|
2748 (beg 0))
|
|
2749 ;; Quote some characters that have special meanings in shells;
|
|
2750 ;; but don't quote the wildcards--we want them to be special.
|
|
2751 ;; We also currently don't quote the quoting characters
|
|
2752 ;; in case people want to use them explicitly to quote
|
|
2753 ;; wildcard characters.
|
|
2754 ;;#### Unix-specific
|
|
2755 (while (string-match "[ \t\n;<>&|()#$]" pattern beg)
|
|
2756 (setq pattern
|
|
2757 (concat (substring pattern 0 (match-beginning 0))
|
|
2758 "\\"
|
|
2759 (substring pattern (match-beginning 0)))
|
|
2760 beg (1+ (match-end 0))))
|
|
2761 (call-process shell-file-name nil t nil
|
|
2762 "-c" (concat "\\" ;; Disregard shell aliases!
|
|
2763 insert-directory-program
|
|
2764 " -d "
|
|
2765 (if (stringp switches)
|
|
2766 switches
|
|
2767 (mapconcat 'identity switches " "))
|
|
2768 " "
|
|
2769 pattern)))
|
|
2770 ;; SunOS 4.1.3, SVr4 and others need the "." to list the
|
|
2771 ;; directory if FILE is a symbolic link.
|
|
2772 (apply 'call-process
|
|
2773 insert-directory-program nil t nil
|
|
2774 (let (list)
|
|
2775 (if (listp switches)
|
|
2776 (setq list switches)
|
|
2777 (if (not (equal switches ""))
|
|
2778 (progn
|
|
2779 ;; Split the switches at any spaces
|
|
2780 ;; so we can pass separate options as separate args.
|
|
2781 (while (string-match " " switches)
|
|
2782 (setq list (cons (substring switches 0 (match-beginning 0))
|
|
2783 list)
|
|
2784 switches (substring switches (match-end 0))))
|
|
2785 (setq list (cons switches list)))))
|
|
2786 (append list
|
|
2787 (list
|
|
2788 (if full-directory-p
|
|
2789 (concat (file-name-as-directory file)
|
|
2790 ;;#### Unix-specific
|
|
2791 ".")
|
|
2792 file))))))))))
|
|
2793
|
|
2794 (defvar kill-emacs-query-functions nil
|
|
2795 "Functions to call with no arguments to query about killing XEmacs.
|
|
2796 If any of these functions returns nil, killing Emacs is cancelled.
|
|
2797 `save-buffers-kill-emacs' (\\[save-buffers-kill-emacs]) calls these functions,
|
|
2798 but `kill-emacs', the low level primitive, does not.
|
|
2799 See also `kill-emacs-hook'.")
|
|
2800
|
|
2801 (defun save-buffers-kill-emacs (&optional arg)
|
|
2802 "Offer to save each buffer, then kill this XEmacs process.
|
|
2803 With prefix arg, silently save all file-visiting buffers, then kill."
|
|
2804 (interactive "P")
|
|
2805 (save-some-buffers arg t)
|
|
2806 (and (or (not (memq t (mapcar #'(lambda (buf) (and (buffer-file-name buf)
|
|
2807 (buffer-modified-p buf)))
|
|
2808 (buffer-list))))
|
|
2809 (yes-or-no-p "Modified buffers exist; exit anyway? "))
|
|
2810 (or (not (fboundp 'process-list))
|
|
2811 ;; process-list is not defined on VMS.
|
|
2812 (let ((processes (process-list))
|
|
2813 active)
|
|
2814 (while processes
|
|
2815 (and (memq (process-status (car processes)) '(run stop open))
|
|
2816 (let ((val (process-kill-without-query (car processes))))
|
|
2817 (process-kill-without-query (car processes) val)
|
|
2818 val)
|
|
2819 (setq active t))
|
|
2820 (setq processes (cdr processes)))
|
|
2821 (or (not active)
|
|
2822 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
|
|
2823 ;; Query the user for other things, perhaps.
|
|
2824 (run-hook-with-args-until-failure 'kill-emacs-query-functions)
|
|
2825 (kill-emacs)))
|
|
2826
|
|
2827 (defun symlink-expand-file-name (filename)
|
|
2828 "If FILENAME is a symlink, return its non-symlink equivalent.
|
|
2829 Unlike `file-truename', this doesn't chase symlinks in directory
|
|
2830 components of the file or expand a relative pathname into an
|
|
2831 absolute one."
|
|
2832 (let ((count 20))
|
|
2833 (while (and (> count 0) (file-symlink-p filename))
|
|
2834 (setq filename (file-symlink-p filename)
|
|
2835 count (1- count)))
|
|
2836 (if (> count 0)
|
|
2837 filename
|
|
2838 (error "Apparently circular symlink path"))))
|
|
2839
|
|
2840
|
|
2841
|
|
2842 (defun insert-file-contents (filename &optional visit beg end replace)
|
|
2843 "Insert contents of file FILENAME after point.
|
|
2844 Returns list of absolute file name and length of data inserted.
|
|
2845 If second argument VISIT is non-nil, the buffer's visited filename
|
|
2846 and last save file modtime are set, and it is marked unmodified.
|
|
2847 If visiting and the file does not exist, visiting is completed
|
|
2848 before the error is signaled.
|
|
2849
|
|
2850 The optional third and fourth arguments BEG and END
|
|
2851 specify what portion of the file to insert.
|
|
2852 If VISIT is non-nil, BEG and END must be nil.
|
|
2853 If optional fifth argument REPLACE is non-nil,
|
|
2854 it means replace the current buffer contents (in the accessible portion)
|
|
2855 with the file contents. This is better than simply deleting and inserting
|
|
2856 the whole thing because (1) it preserves some marker positions
|
|
2857 and (2) it puts less data in the undo list."
|
|
2858 (insert-file-contents-internal filename visit beg end replace))
|
|
2859
|
|
2860 (defun write-region (start end filename &optional append visit lockname)
|
|
2861 "Write current region into specified file.
|
|
2862 When called from a program, takes three arguments:
|
|
2863 START, END and FILENAME. START and END are buffer positions.
|
|
2864 Optional fourth argument APPEND if non-nil means
|
|
2865 append to existing file contents (if any).
|
|
2866 Optional fifth argument VISIT if t means
|
|
2867 set the last-save-file-modtime of buffer to this file's modtime
|
|
2868 and mark buffer not modified.
|
|
2869 If VISIT is a string, it is a second file name;
|
|
2870 the output goes to FILENAME, but the buffer is marked as visiting VISIT.
|
|
2871 VISIT is also the file name to lock and unlock for clash detection.
|
|
2872 If VISIT is neither t nor nil nor a string,
|
|
2873 that means do not print the \"Wrote file\" message.
|
|
2874 The optional sixth arg LOCKNAME, if non-nil, specifies the name to
|
|
2875 use for locking and unlocking, overriding FILENAME and VISIT.
|
|
2876 Kludgy feature: if START is a string, then that string is written
|
|
2877 to the file, instead of any buffer contents, and END is ignored."
|
|
2878 (interactive "r\nFWrite region to file: ")
|
|
2879 (write-region-internal start end filename append visit lockname))
|
|
2880
|
|
2881 (defun load (file &optional noerror nomessage nosuffix)
|
|
2882 "Execute a file of Lisp code named FILE.
|
|
2883 First try FILE with `.elc' appended, then try with `.el',
|
|
2884 then try FILE unmodified.
|
|
2885 This function searches the directories in `load-path'.
|
|
2886 If optional second arg NOERROR is non-nil,
|
|
2887 report no error if FILE doesn't exist.
|
|
2888 Print messages at start and end of loading unless
|
|
2889 optional third arg NOMESSAGE is non-nil (ignored in -batch mode).
|
|
2890 If optional fourth arg NOSUFFIX is non-nil, don't try adding
|
|
2891 suffixes `.elc' or `.el' to the specified name FILE.
|
|
2892 Return t if file exists."
|
|
2893 (load-internal file noerror nomessage nosuffix))
|
|
2894
|
|
2895 ;;; files.el ends here
|