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