(save-some-buffers): Turn EXITING into the more general
[emacs.git] / lisp / files.el
blob44baf22f8bd31caafd5aeb72d254f16751df21a5
1 ;;; files.el --- file input and output commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 92, 93,
4 ;; 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; Defines most of Emacs's file- and directory-handling functions,
28 ;; including basic file visiting, backup generation, link handling,
29 ;; ITS-id version control, load- and write-hook handling, and the like.
31 ;;; Code:
33 (defgroup backup nil
34 "Backups of edited data files."
35 :group 'files)
37 (defgroup find-file nil
38 "Finding files."
39 :group 'files)
42 (defcustom delete-auto-save-files t
43 "*Non-nil means delete auto-save file when a buffer is saved or killed.
45 Note that auto-save file will not be deleted if the buffer is killed
46 when it has unsaved changes."
47 :type 'boolean
48 :group 'auto-save)
50 (defcustom directory-abbrev-alist
51 nil
52 "*Alist of abbreviations for file directories.
53 A list of elements of the form (FROM . TO), each meaning to replace
54 FROM with TO when it appears in a directory name. This replacement is
55 done when setting up the default directory of a newly visited file.
56 *Every* FROM string should start with `^'.
58 Do not use `~' in the TO strings.
59 They should be ordinary absolute directory names.
61 Use this feature when you have directories which you normally refer to
62 via absolute symbolic links. Make TO the name of the link, and FROM
63 the name it is linked to."
64 :type '(repeat (cons :format "%v"
65 :value ("" . "")
66 (regexp :tag "From")
67 (regexp :tag "To")))
68 :group 'abbrev
69 :group 'find-file)
71 ;;; Turn off backup files on VMS since it has version numbers.
72 (defcustom make-backup-files (not (eq system-type 'vax-vms))
73 "*Non-nil means make a backup of a file the first time it is saved.
74 This can be done by renaming the file or by copying.
76 Renaming means that Emacs renames the existing file so that it is a
77 backup file, then writes the buffer into a new file. Any other names
78 that the old file had will now refer to the backup file. The new file
79 is owned by you and its group is defaulted.
81 Copying means that Emacs copies the existing file into the backup
82 file, then writes the buffer on top of the existing file. Any other
83 names that the old file had will now refer to the new (edited) file.
84 The file's owner and group are unchanged.
86 The choice of renaming or copying is controlled by the variables
87 `backup-by-copying', `backup-by-copying-when-linked',
88 `backup-by-copying-when-mismatch' and
89 `backup-by-copying-when-privileged-mismatch'. See also `backup-inhibited'."
90 :type 'boolean
91 :group 'backup)
93 ;; Do this so that local variables based on the file name
94 ;; are not overridden by the major mode.
95 (defvar backup-inhibited nil
96 "Non-nil means don't make a backup, regardless of the other parameters.
97 This variable is intended for use by making it local to a buffer.
98 But it is local only if you make it local.")
99 (put 'backup-inhibited 'permanent-local t)
101 (defcustom backup-by-copying nil
102 "*Non-nil means always use copying to create backup files.
103 See documentation of variable `make-backup-files'."
104 :type 'boolean
105 :group 'backup)
107 (defcustom backup-by-copying-when-linked nil
108 "*Non-nil means use copying to create backups for files with multiple names.
109 This causes the alternate names to refer to the latest version as edited.
110 This variable is relevant only if `backup-by-copying' is nil."
111 :type 'boolean
112 :group 'backup)
114 (defcustom backup-by-copying-when-mismatch nil
115 "*Non-nil means create backups by copying if this preserves owner or group.
116 Renaming may still be used (subject to control of other variables)
117 when it would not result in changing the owner or group of the file;
118 that is, for files which are owned by you and whose group matches
119 the default for a new file created there by you.
120 This variable is relevant only if `backup-by-copying' is nil."
121 :type 'boolean
122 :group 'backup)
124 (defcustom backup-by-copying-when-privileged-mismatch 200
125 "*Non-nil means create backups by copying to preserve a privileged owner.
126 Renaming may still be used (subject to control of other variables)
127 when it would not result in changing the owner of the file or if the owner
128 has a user id greater than the value of this variable. This is useful
129 when low-numbered uid's are used for special system users (such as root)
130 that must maintain ownership of certain files.
131 This variable is relevant only if `backup-by-copying' and
132 `backup-by-copying-when-mismatch' are nil."
133 :type '(choice (const nil) integer)
134 :group 'backup)
136 (defvar backup-enable-predicate
137 '(lambda (name)
138 (or (< (length name) 5)
139 (not (string-equal "/tmp/" (substring name 0 5)))))
140 "Predicate that looks at a file name and decides whether to make backups.
141 Called with an absolute file name as argument, it returns t to enable backup.")
143 (defcustom buffer-offer-save nil
144 "*Non-nil in a buffer means offer to save the buffer on exit
145 even if the buffer is not visiting a file.
146 Automatically local in all buffers."
147 :type 'boolean
148 :group 'backup)
149 (make-variable-buffer-local 'buffer-offer-save)
151 (defcustom find-file-existing-other-name t
152 "*Non-nil means find a file under alternative names, in existing buffers.
153 This means if any existing buffer is visiting the file you want
154 under another name, you get the existing buffer instead of a new buffer."
155 :type 'boolean
156 :group 'find-file)
158 (defcustom find-file-visit-truename nil
159 "*Non-nil means visit a file under its truename.
160 The truename of a file is found by chasing all links
161 both at the file level and at the levels of the containing directories."
162 :type 'boolean
163 :group 'find-file)
165 (defcustom revert-without-query
167 "*Specify which files should be reverted without query.
168 The value is a list of regular expressions.
169 If the file name matches one of these regular expressions,
170 then `revert-buffer' reverts the file without querying
171 if the file has changed on disk and you have not edited the buffer."
172 :type '(repeat regexp)
173 :group 'find-file)
175 (defvar buffer-file-number nil
176 "The device number and file number of the file visited in the current buffer.
177 The value is a list of the form (FILENUM DEVNUM).
178 This pair of numbers uniquely identifies the file.
179 If the buffer is visiting a new file, the value is nil.")
180 (make-variable-buffer-local 'buffer-file-number)
181 (put 'buffer-file-number 'permanent-local t)
183 (defvar buffer-file-numbers-unique (not (memq system-type '(windows-nt)))
184 "Non-nil means that buffer-file-number uniquely identifies files.")
186 (defvar file-name-invalid-regexp
187 (cond ((and (eq system-type 'ms-dos) (not (msdos-long-file-names)))
188 (concat "^\\([^A-Z[-`a-z]\\|..+\\)?:\\|" ; colon except after drive
189 "[+, ;=|<>\"?*]\\|\\[\\|\\]\\|" ; invalid characters
190 "[\000-\031]\\|" ; control characters
191 "\\(/\\.\\.?[^/]\\)\\|" ; leading dots
192 "\\(/[^/.]+\\.[^/.]*\\.\\)")) ; more than a single dot
193 ((memq system-type '(ms-dos windows-nt))
194 (concat "^\\([^A-Z[-`a-z]\\|..+\\)?:\\|" ; colon except after drive
195 "[|<>\"?*\000-\031]")) ; invalid characters
196 (t "[\000]"))
197 "Regexp recognizing file names which aren't allowed by the filesystem.")
199 (defcustom file-precious-flag nil
200 "*Non-nil means protect against I/O errors while saving files.
201 Some modes set this non-nil in particular buffers.
203 This feature works by writing the new contents into a temporary file
204 and then renaming the temporary file to replace the original.
205 In this way, any I/O error in writing leaves the original untouched,
206 and there is never any instant where the file is nonexistent.
208 Note that this feature forces backups to be made by copying.
209 Yet, at the same time, saving a precious file
210 breaks any hard links between it and other files."
211 :type 'boolean
212 :group 'backup)
214 (defcustom version-control nil
215 "*Control use of version numbers for backup files.
216 t means make numeric backup versions unconditionally.
217 nil means make them for files that have some already.
218 `never' means do not make them."
219 :type '(choice (const :tag "Never" never)
220 (const :tag "If existing" nil)
221 (other :tag "Always" t))
222 :group 'backup
223 :group 'vc)
225 (defcustom dired-kept-versions 2
226 "*When cleaning directory, number of versions to keep."
227 :type 'integer
228 :group 'backup
229 :group 'dired)
231 (defcustom delete-old-versions nil
232 "*If t, delete excess backup versions silently.
233 If nil, ask confirmation. Any other value prevents any trimming."
234 :type '(choice (const :tag "Delete" t)
235 (const :tag "Ask" nil)
236 (other :tag "Leave" other))
237 :group 'backup)
239 (defcustom kept-old-versions 2
240 "*Number of oldest versions to keep when a new numbered backup is made."
241 :type 'integer
242 :group 'backup)
244 (defcustom kept-new-versions 2
245 "*Number of newest versions to keep when a new numbered backup is made.
246 Includes the new backup. Must be > 0"
247 :type 'integer
248 :group 'backup)
250 (defcustom require-final-newline nil
251 "*Value of t says silently ensure a file ends in a newline when it is saved.
252 Non-nil but not t says ask user whether to add a newline when there isn't one.
253 nil means don't add newlines."
254 :type '(choice (const :tag "Off" nil)
255 (const :tag "Add" t)
256 (other :tag "Ask" ask))
257 :group 'editing-basics)
259 (defcustom auto-save-default t
260 "*Non-nil says by default do auto-saving of every file-visiting buffer."
261 :type 'boolean
262 :group 'auto-save)
264 (defcustom auto-save-visited-file-name nil
265 "*Non-nil says auto-save a buffer in the file it is visiting, when practical.
266 Normally auto-save files are written under other names."
267 :type 'boolean
268 :group 'auto-save)
270 (defcustom save-abbrevs nil
271 "*Non-nil means save word abbrevs too when files are saved.
272 Loading an abbrev file sets this to t."
273 :type 'boolean
274 :group 'abbrev)
276 (defcustom find-file-run-dired t
277 "*Non-nil says run dired if `find-file' is given the name of a directory."
278 :type 'boolean
279 :group 'find-file)
281 ;;;It is not useful to make this a local variable.
282 ;;;(put 'find-file-not-found-hooks 'permanent-local t)
283 (defvar find-file-not-found-hooks nil
284 "List of functions to be called for `find-file' on nonexistent file.
285 These functions are called as soon as the error is detected.
286 `buffer-file-name' is already set up.
287 The functions are called in the order given until one of them returns non-nil.")
289 ;;;It is not useful to make this a local variable.
290 ;;;(put 'find-file-hooks 'permanent-local t)
291 (defvar find-file-hooks nil
292 "List of functions to be called after a buffer is loaded from a file.
293 The buffer's local variables (if any) will have been processed before the
294 functions are called.")
296 (defvar write-file-hooks nil
297 "List of functions to be called before writing out a buffer to a file.
298 If one of them returns non-nil, the file is considered already written
299 and the rest are not called.
300 These hooks are considered to pertain to the visited file.
301 So any buffer-local binding of `write-file-hooks' is
302 discarded if you change the visited file name with \\[set-visited-file-name].
304 Don't make this variable buffer-local; instead, use `local-write-file-hooks'.
305 See also `write-contents-hooks'.")
306 ;;; However, in case someone does make it local...
307 (put 'write-file-hooks 'permanent-local t)
309 (defvar local-write-file-hooks nil
310 "Just like `write-file-hooks', except intended for per-buffer use.
311 The functions in this list are called before the ones in
312 `write-file-hooks'.
314 This variable is meant to be used for hooks that have to do with a
315 particular visited file. Therefore, it is a permanent local, so that
316 changing the major mode does not clear it. However, calling
317 `set-visited-file-name' does clear it.")
318 (make-variable-buffer-local 'local-write-file-hooks)
319 (put 'local-write-file-hooks 'permanent-local t)
321 (defvar write-contents-hooks nil
322 "List of functions to be called before writing out a buffer to a file.
323 If one of them returns non-nil, the file is considered already written
324 and the rest are not called.
326 This variable is meant to be used for hooks that pertain to the
327 buffer's contents, not to the particular visited file; thus,
328 `set-visited-file-name' does not clear this variable; but changing the
329 major mode does clear it.
331 This variable automatically becomes buffer-local whenever it is set.
332 If you use `add-hook' to add elements to the list, use nil for the
333 LOCAL argument.
335 See also `write-file-hooks'.")
336 (make-variable-buffer-local 'write-contents-hooks)
338 (defcustom enable-local-variables t
339 "*Control use of local variables in files you visit.
340 The value can be t, nil or something else.
341 A value of t means file local variables specifications are obeyed;
342 nil means they are ignored; anything else means query.
343 This variable also controls use of major modes specified in
344 a -*- line.
346 The command \\[normal-mode], when used interactively,
347 always obeys file local variable specifications and the -*- line,
348 and ignores this variable."
349 :type '(choice (const :tag "Obey" t)
350 (const :tag "Ignore" nil)
351 (other :tag "Query" other))
352 :group 'find-file)
354 (defvar local-enable-local-variables t
355 "Like `enable-local-variables' but meant for buffer-local bindings.
356 The meaningful values are nil and non-nil. The default is non-nil.
357 If a major mode sets this to nil, buffer-locally, then any local
358 variables list in the file will be ignored.
360 This variable does not affect the use of major modes
361 specified in a -*- line.")
363 (defcustom enable-local-eval 'maybe
364 "*Control processing of the \"variable\" `eval' in a file's local variables.
365 The value can be t, nil or something else.
366 A value of t means obey `eval' variables;
367 nil means ignore them; anything else means query.
369 The command \\[normal-mode] always obeys local-variables lists
370 and ignores this variable."
371 :type '(choice (const :tag "Obey" t)
372 (const :tag "Ignore" nil)
373 (other :tag "Query" other))
374 :group 'find-file)
376 ;; Avoid losing in versions where CLASH_DETECTION is disabled.
377 (or (fboundp 'lock-buffer)
378 (defalias 'lock-buffer 'ignore))
379 (or (fboundp 'unlock-buffer)
380 (defalias 'unlock-buffer 'ignore))
381 (or (fboundp 'file-locked-p)
382 (defalias 'file-locked-p 'ignore))
384 (defvar view-read-only nil
385 "*Non-nil means buffers visiting files read-only, do it in view mode.")
387 (defvar temporary-file-directory
388 (file-name-as-directory
389 (cond ((memq system-type '(ms-dos windows-nt))
390 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
391 ((memq system-type '(vax-vms axp-vms))
392 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
394 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
395 "The directory for writing temporary files.")
397 (defvar small-temporary-file-directory
398 (if (eq system-type 'ms-dos) (getenv "TMPDIR"))
399 "The directory for writing small temporary files.
400 If non-nil, this directory is used instead of `temporary-file-directory'
401 by programs that create small temporary files. This is for systems that
402 have fast storage with limited space, such as a RAM disk.")
404 ;; The system null device. (Should reference NULL_DEVICE from C.)
405 (defvar null-device "/dev/null" "The system null device.")
407 ;; This hook function provides support for ange-ftp host name
408 ;; completion. It runs the usual ange-ftp hook, but only for
409 ;; completion operations. Having this here avoids the need
410 ;; to load ange-ftp when it's not really in use.
411 (defun ange-ftp-completion-hook-function (op &rest args)
412 (if (memq op '(file-name-completion file-name-all-completions))
413 (apply 'ange-ftp-hook-function op args)
414 (let ((inhibit-file-name-handlers
415 (cons 'ange-ftp-completion-hook-function
416 (and (eq inhibit-file-name-operation op)
417 inhibit-file-name-handlers)))
418 (inhibit-file-name-operation op))
419 (apply op args))))
421 (defun convert-standard-filename (filename)
422 "Convert a standard file's name to something suitable for the current OS.
423 This function's standard definition is trivial; it just returns the argument.
424 However, on some systems, the function is redefined
425 with a definition that really does change some file names."
426 filename)
428 (defun pwd ()
429 "Show the current default directory."
430 (interactive nil)
431 (message "Directory %s" default-directory))
433 (defvar cd-path nil
434 "Value of the CDPATH environment variable, as a list.
435 Not actually set up until the first time you you use it.")
437 (defun parse-colon-path (cd-path)
438 "Explode a colon-separated search path into a list of directory names."
439 (and cd-path
440 (let (cd-prefix cd-list (cd-start 0) cd-colon)
441 (setq cd-path (concat cd-path path-separator))
442 (while (setq cd-colon (string-match path-separator cd-path cd-start))
443 (setq cd-list
444 (nconc cd-list
445 (list (if (= cd-start cd-colon)
447 (substitute-in-file-name
448 (file-name-as-directory
449 (substring cd-path cd-start cd-colon)))))))
450 (setq cd-start (+ cd-colon 1)))
451 cd-list)))
453 (defun cd-absolute (dir)
454 "Change current directory to given absolute file name DIR."
455 ;; Put the name into directory syntax now,
456 ;; because otherwise expand-file-name may give some bad results.
457 (if (not (eq system-type 'vax-vms))
458 (setq dir (file-name-as-directory dir)))
459 (setq dir (abbreviate-file-name (expand-file-name dir)))
460 (if (not (file-directory-p dir))
461 (if (file-exists-p dir)
462 (error "%s is not a directory" dir)
463 (error "%s: no such directory" dir))
464 (if (file-executable-p dir)
465 (setq default-directory dir)
466 (error "Cannot cd to %s: Permission denied" dir))))
468 (defun cd (dir)
469 "Make DIR become the current buffer's default directory.
470 If your environment includes a `CDPATH' variable, try each one of that
471 colon-separated list of directories when resolving a relative directory name."
472 (interactive
473 (list (read-file-name "Change default directory: "
474 default-directory default-directory
475 (and (member cd-path '(nil ("./")))
476 (null (getenv "CDPATH"))))))
477 (if (file-name-absolute-p dir)
478 (cd-absolute (expand-file-name dir))
479 (if (null cd-path)
480 (let ((trypath (parse-colon-path (getenv "CDPATH"))))
481 (setq cd-path (or trypath (list "./")))))
482 (if (not (catch 'found
483 (mapcar
484 (function (lambda (x)
485 (let ((f (expand-file-name (concat x dir))))
486 (if (file-directory-p f)
487 (progn
488 (cd-absolute f)
489 (throw 'found t))))))
490 cd-path)
491 nil))
492 (error "No such directory found via CDPATH environment variable"))))
494 (defun load-file (file)
495 "Load the Lisp file named FILE."
496 (interactive "fLoad file: ")
497 (load (expand-file-name file) nil nil t))
499 (defun load-library (library)
500 "Load the library named LIBRARY.
501 This is an interface to the function `load'."
502 (interactive "sLoad library: ")
503 (load library))
505 (defun file-local-copy (file)
506 "Copy the file FILE into a temporary file on this machine.
507 Returns the name of the local copy, or nil, if FILE is directly
508 accessible."
509 ;; This formerly had an optional BUFFER argument that wasn't used by
510 ;; anything.
511 (let ((handler (find-file-name-handler file 'file-local-copy)))
512 (if handler
513 (funcall handler 'file-local-copy file)
514 nil)))
516 (defun file-truename (filename &optional counter prev-dirs)
517 "Return the truename of FILENAME, which should be absolute.
518 The truename of a file name is found by chasing symbolic links
519 both at the level of the file and at the level of the directories
520 containing it, until no links are left at any level.
522 The arguments COUNTER and PREV-DIRS are used only in recursive calls.
523 Do not specify them in other calls."
524 ;; COUNTER can be a cons cell whose car is the count of how many more links
525 ;; to chase before getting an error.
526 ;; PREV-DIRS can be a cons cell whose car is an alist
527 ;; of truenames we've just recently computed.
529 ;; The last test looks dubious, maybe `+' is meant here? --simon.
530 (if (or (string= filename "") (string= filename "~")
531 (and (string= (substring filename 0 1) "~")
532 (string-match "~[^/]*" filename)))
533 (progn
534 (setq filename (expand-file-name filename))
535 (if (string= filename "")
536 (setq filename "/"))))
537 (or counter (setq counter (list 100)))
538 (let (done
539 ;; For speed, remove the ange-ftp completion handler from the list.
540 ;; We know it's not needed here.
541 ;; For even more speed, do this only on the outermost call.
542 (file-name-handler-alist
543 (if prev-dirs file-name-handler-alist
544 (let ((tem (copy-sequence file-name-handler-alist)))
545 (delq (rassq 'ange-ftp-completion-hook-function tem) tem)))))
546 (or prev-dirs (setq prev-dirs (list nil)))
548 ;; andrewi@harlequin.co.uk - none of the following code (except for
549 ;; invoking the file-name handler) currently applies on Windows
550 ;; (ie. there are no native symlinks), but there is an issue with
551 ;; case differences being ignored by the OS, and short "8.3 DOS"
552 ;; name aliases existing for all files. (The short names are not
553 ;; reported by directory-files, but can be used to refer to files.)
554 ;; It seems appropriate for file-truename to resolve these issues in
555 ;; the most natural way, which on Windows is to call the function
556 ;; `w32-long-file-name' - this returns the exact name of a file as
557 ;; it is stored on disk (expanding short name aliases with the full
558 ;; name in the process).
559 (if (eq system-type 'windows-nt)
560 (let ((handler (find-file-name-handler filename 'file-truename))
561 newname)
562 ;; For file name that has a special handler, call handler.
563 ;; This is so that ange-ftp can save time by doing a no-op.
564 (if handler
565 (setq filename (funcall handler 'file-truename filename))
566 ;; If filename contains a wildcard, newname will be the old name.
567 (if (string-match "[*?]" filename)
568 (setq newname filename)
569 ;; If filename doesn't exist, newname will be nil.
570 (setq newname (w32-long-file-name filename)))
571 (setq filename (or newname filename)))
572 (setq done t)))
574 ;; If this file directly leads to a link, process that iteratively
575 ;; so that we don't use lots of stack.
576 (while (not done)
577 (setcar counter (1- (car counter)))
578 (if (< (car counter) 0)
579 (error "Apparent cycle of symbolic links for %s" filename))
580 (let ((handler (find-file-name-handler filename 'file-truename)))
581 ;; For file name that has a special handler, call handler.
582 ;; This is so that ange-ftp can save time by doing a no-op.
583 (if handler
584 (setq filename (funcall handler 'file-truename filename)
585 done t)
586 (let ((dir (or (file-name-directory filename) default-directory))
587 target dirfile)
588 ;; Get the truename of the directory.
589 (setq dirfile (directory-file-name dir))
590 ;; If these are equal, we have the (or a) root directory.
591 (or (string= dir dirfile)
592 ;; If this is the same dir we last got the truename for,
593 ;; save time--don't recalculate.
594 (if (assoc dir (car prev-dirs))
595 (setq dir (cdr (assoc dir (car prev-dirs))))
596 (let ((old dir)
597 (new (file-name-as-directory (file-truename dirfile counter prev-dirs))))
598 (setcar prev-dirs (cons (cons old new) (car prev-dirs)))
599 (setq dir new))))
600 (if (equal ".." (file-name-nondirectory filename))
601 (setq filename
602 (directory-file-name (file-name-directory (directory-file-name dir)))
603 done t)
604 (if (equal "." (file-name-nondirectory filename))
605 (setq filename (directory-file-name dir)
606 done t)
607 ;; Put it back on the file name.
608 (setq filename (concat dir (file-name-nondirectory filename)))
609 ;; Is the file name the name of a link?
610 (setq target (file-symlink-p filename))
611 (if target
612 ;; Yes => chase that link, then start all over
613 ;; since the link may point to a directory name that uses links.
614 ;; We can't safely use expand-file-name here
615 ;; since target might look like foo/../bar where foo
616 ;; is itself a link. Instead, we handle . and .. above.
617 (setq filename
618 (if (file-name-absolute-p target)
619 target
620 (concat dir target))
621 done nil)
622 ;; No, we are done!
623 (setq done t))))))))
624 filename))
626 (defun file-chase-links (filename)
627 "Chase links in FILENAME until a name that is not a link.
628 Does not examine containing directories for links,
629 unlike `file-truename'."
630 (let (tem (count 100) (newname filename))
631 (while (setq tem (file-symlink-p newname))
632 (save-match-data
633 (if (= count 0)
634 (error "Apparent cycle of symbolic links for %s" filename))
635 ;; In the context of a link, `//' doesn't mean what Emacs thinks.
636 (while (string-match "//+" tem)
637 (setq tem (replace-match "/" nil nil tem)))
638 ;; Handle `..' by hand, since it needs to work in the
639 ;; target of any directory symlink.
640 ;; This code is not quite complete; it does not handle
641 ;; embedded .. in some cases such as ./../foo and foo/bar/../../../lose.
642 (while (string-match "\\`\\.\\./" tem)
643 (setq tem (substring tem 3))
644 (setq newname (expand-file-name newname))
645 ;; Chase links in the default dir of the symlink.
646 (setq newname
647 (file-chase-links
648 (directory-file-name (file-name-directory newname))))
649 ;; Now find the parent of that dir.
650 (setq newname (file-name-directory newname)))
651 (setq newname (expand-file-name tem (file-name-directory newname)))
652 (setq count (1- count))))
653 newname))
655 (defun switch-to-buffer-other-window (buffer &optional norecord)
656 "Select buffer BUFFER in another window.
657 Optional second arg NORECORD non-nil means
658 do not put this buffer at the front of the list of recently selected ones."
659 (interactive "BSwitch to buffer in other window: ")
660 (let ((pop-up-windows t))
661 (pop-to-buffer buffer t norecord)))
663 (defun switch-to-buffer-other-frame (buffer &optional norecord)
664 "Switch to buffer BUFFER in another frame.
665 Optional second arg NORECORD non-nil means
666 do not put this buffer at the front of the list of recently selected ones."
667 (interactive "BSwitch to buffer in other frame: ")
668 (let ((pop-up-frames t))
669 (pop-to-buffer buffer t norecord)
670 (raise-frame (window-frame (selected-window)))))
672 (defun find-file (filename &optional wildcards)
673 "Edit file FILENAME.
674 Switch to a buffer visiting file FILENAME,
675 creating one if none already exists.
676 Interactively, or if WILDCARDS is non-nil in a call from Lisp,
677 expand wildcards (if any) and visit multiple files. Wildcard expansion
678 can be suppressed by setting `find-file-wildcards'."
679 (interactive "FFind file: \np")
680 (let ((value (find-file-noselect filename nil nil wildcards)))
681 (if (listp value)
682 (mapcar 'switch-to-buffer (nreverse value))
683 (switch-to-buffer value))))
685 (defun find-file-other-window (filename &optional wildcards)
686 "Edit file FILENAME, in another window.
687 May create a new window, or reuse an existing one.
688 See the function `display-buffer'.
689 Interactively, or if WILDCARDS is non-nil in a call from Lisp,
690 expand wildcards (if any) and visit multiple files."
691 (interactive "FFind file in other window: \np")
692 (let ((value (find-file-noselect filename nil nil wildcards)))
693 (if (listp value)
694 (progn
695 (setq value (nreverse value))
696 (switch-to-buffer-other-window (car value))
697 (mapcar 'switch-to-buffer (cdr value)))
698 (switch-to-buffer-other-window value))))
700 (defun find-file-other-frame (filename &optional wildcards)
701 "Edit file FILENAME, in another frame.
702 May create a new frame, or reuse an existing one.
703 See the function `display-buffer'.
704 Interactively, or if WILDCARDS is non-nil in a call from Lisp,
705 expand wildcards (if any) and visit multiple files."
706 (interactive "FFind file in other frame: \np")
707 (let ((value (find-file-noselect filename nil nil wildcards)))
708 (if (listp value)
709 (progn
710 (setq value (nreverse value))
711 (switch-to-buffer-other-frame (car value))
712 (mapcar 'switch-to-buffer (cdr value)))
713 (switch-to-buffer-other-frame value))))
715 (defun find-file-read-only (filename &optional wildcards)
716 "Edit file FILENAME but don't allow changes.
717 Like \\[find-file] but marks buffer as read-only.
718 Use \\[toggle-read-only] to permit editing."
719 (interactive "fFind file read-only: \np")
720 (find-file filename wildcards)
721 (toggle-read-only 1)
722 (current-buffer))
724 (defun find-file-read-only-other-window (filename &optional wildcards)
725 "Edit file FILENAME in another window but don't allow changes.
726 Like \\[find-file-other-window] but marks buffer as read-only.
727 Use \\[toggle-read-only] to permit editing."
728 (interactive "fFind file read-only other window: \np")
729 (find-file-other-window filename wildcards)
730 (toggle-read-only 1)
731 (current-buffer))
733 (defun find-file-read-only-other-frame (filename &optional wildcards)
734 "Edit file FILENAME in another frame but don't allow changes.
735 Like \\[find-file-other-frame] but marks buffer as read-only.
736 Use \\[toggle-read-only] to permit editing."
737 (interactive "fFind file read-only other frame: \np")
738 (find-file-other-frame filename wildcards)
739 (toggle-read-only 1)
740 (current-buffer))
742 (defun find-alternate-file-other-window (filename)
743 "Find file FILENAME as a replacement for the file in the next window.
744 This command does not select that window."
745 (interactive
746 (save-selected-window
747 (other-window 1)
748 (let ((file buffer-file-name)
749 (file-name nil)
750 (file-dir nil))
751 (and file
752 (setq file-name (file-name-nondirectory file)
753 file-dir (file-name-directory file)))
754 (list (read-file-name
755 "Find alternate file: " file-dir nil nil file-name)))))
756 (if (one-window-p)
757 (find-file-other-window filename)
758 (save-selected-window
759 (other-window 1)
760 (find-alternate-file filename))))
762 (defun find-alternate-file (filename)
763 "Find file FILENAME, select its buffer, kill previous buffer.
764 If the current buffer now contains an empty file that you just visited
765 \(presumably by mistake), use this command to visit the file you really want."
766 (interactive
767 (let ((file buffer-file-name)
768 (file-name nil)
769 (file-dir nil))
770 (and file
771 (setq file-name (file-name-nondirectory file)
772 file-dir (file-name-directory file)))
773 (list (read-file-name
774 "Find alternate file: " file-dir nil nil file-name))))
775 (and (buffer-modified-p) (buffer-file-name)
776 ;; (not buffer-read-only)
777 (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? "
778 (buffer-name))))
779 (error "Aborted"))
780 (let ((obuf (current-buffer))
781 (ofile buffer-file-name)
782 (onum buffer-file-number)
783 (otrue buffer-file-truename)
784 (oname (buffer-name)))
785 (if (get-buffer " **lose**")
786 (kill-buffer " **lose**"))
787 (rename-buffer " **lose**")
788 (unwind-protect
789 (progn
790 (unlock-buffer)
791 (setq buffer-file-name nil)
792 (setq buffer-file-number nil)
793 (setq buffer-file-truename nil)
794 (find-file filename))
795 (cond ((eq obuf (current-buffer))
796 (setq buffer-file-name ofile)
797 (setq buffer-file-number onum)
798 (setq buffer-file-truename otrue)
799 (lock-buffer)
800 (rename-buffer oname))))
801 (or (eq (current-buffer) obuf)
802 (kill-buffer obuf))))
804 (defun create-file-buffer (filename)
805 "Create a suitably named buffer for visiting FILENAME, and return it.
806 FILENAME (sans directory) is used unchanged if that name is free;
807 otherwise a string <2> or <3> or ... is appended to get an unused name."
808 (let ((lastname (file-name-nondirectory filename)))
809 (if (string= lastname "")
810 (setq lastname filename))
811 (generate-new-buffer lastname)))
813 (defun generate-new-buffer (name)
814 "Create and return a buffer with a name based on NAME.
815 Choose the buffer's name using `generate-new-buffer-name'."
816 (get-buffer-create (generate-new-buffer-name name)))
818 (defvar automount-dir-prefix "^/tmp_mnt/"
819 "Regexp to match the automounter prefix in a directory name.")
821 (defvar abbreviated-home-dir nil
822 "The user's homedir abbreviated according to `directory-abbrev-alist'.")
824 (defun abbreviate-file-name (filename)
825 "Return a version of FILENAME shortened using `directory-abbrev-alist'.
826 This also substitutes \"~\" for the user's home directory.
827 Type \\[describe-variable] directory-abbrev-alist RET for more information."
828 ;; Get rid of the prefixes added by the automounter.
829 (if (and automount-dir-prefix
830 (string-match automount-dir-prefix filename)
831 (file-exists-p (file-name-directory
832 (substring filename (1- (match-end 0))))))
833 (setq filename (substring filename (1- (match-end 0)))))
834 (let ((tail directory-abbrev-alist))
835 ;; If any elt of directory-abbrev-alist matches this name,
836 ;; abbreviate accordingly.
837 (while tail
838 (if (string-match (car (car tail)) filename)
839 (setq filename
840 (concat (cdr (car tail)) (substring filename (match-end 0)))))
841 (setq tail (cdr tail)))
842 ;; Compute and save the abbreviated homedir name.
843 ;; We defer computing this until the first time it's needed, to
844 ;; give time for directory-abbrev-alist to be set properly.
845 ;; We include a slash at the end, to avoid spurious matches
846 ;; such as `/usr/foobar' when the home dir is `/usr/foo'.
847 (or abbreviated-home-dir
848 (setq abbreviated-home-dir
849 (let ((abbreviated-home-dir "$foo"))
850 (concat "^" (abbreviate-file-name (expand-file-name "~"))
851 "\\(/\\|$\\)"))))
853 ;; If FILENAME starts with the abbreviated homedir,
854 ;; make it start with `~' instead.
855 (if (and (string-match abbreviated-home-dir filename)
856 ;; If the home dir is just /, don't change it.
857 (not (and (= (match-end 0) 1)
858 (= (aref filename 0) ?/)))
859 ;; MS-DOS root directories can come with a drive letter;
860 ;; Novell Netware allows drive letters beyond `Z:'.
861 (not (and (or (eq system-type 'ms-dos)
862 (eq system-type 'windows-nt))
863 (save-match-data
864 (string-match "^[a-zA-`]:/$" filename)))))
865 (setq filename
866 (concat "~"
867 (substring filename (match-beginning 1) (match-end 1))
868 (substring filename (match-end 0)))))
869 filename))
871 (defcustom find-file-not-true-dirname-list nil
872 "*List of logical names for which visiting shouldn't save the true dirname.
873 On VMS, when you visit a file using a logical name that searches a path,
874 you may or may not want the visited file name to record the specific
875 directory where the file was found. If you *do not* want that, add the logical
876 name to this list as a string."
877 :type '(repeat (string :tag "Name"))
878 :group 'find-file)
880 (defun find-buffer-visiting (filename)
881 "Return the buffer visiting file FILENAME (a string).
882 This is like `get-file-buffer', except that it checks for any buffer
883 visiting the same file, possibly under a different name.
884 If there is no such live buffer, return nil."
885 (let ((buf (get-file-buffer filename))
886 (truename (abbreviate-file-name (file-truename filename))))
887 (or buf
888 (let ((list (buffer-list)) found)
889 (while (and (not found) list)
890 (save-excursion
891 (set-buffer (car list))
892 (if (and buffer-file-name
893 (string= buffer-file-truename truename))
894 (setq found (car list))))
895 (setq list (cdr list)))
896 found)
897 (let ((number (nthcdr 10 (file-attributes truename)))
898 (list (buffer-list)) found)
899 (and buffer-file-numbers-unique
900 number
901 (while (and (not found) list)
902 (save-excursion
903 (set-buffer (car list))
904 (if (and buffer-file-name
905 (equal buffer-file-number number)
906 ;; Verify this buffer's file number
907 ;; still belongs to its file.
908 (file-exists-p buffer-file-name)
909 (equal (nthcdr 10 (file-attributes buffer-file-name))
910 number))
911 (setq found (car list))))
912 (setq list (cdr list))))
913 found))))
915 (defcustom find-file-wildcards t
916 "*Non-nil means file-visiting commands should handle wildcards.
917 For example, if you specify `*.c', that would visit all the files
918 whose names match the pattern."
919 :group 'files
920 :type 'boolean)
922 (defun find-file-noselect (filename &optional nowarn rawfile wildcards)
923 "Read file FILENAME into a buffer and return the buffer.
924 If a buffer exists visiting FILENAME, return that one, but
925 verify that the file has not changed since visited or saved.
926 The buffer is not selected, just returned to the caller.
927 Optional first arg NOWARN non-nil means suppress any warning messages.
928 Optional second arg RAWFILE non-nil means the file is read literally.
929 Optional third arg WILDCARDS non-nil means do wildcard processing
930 and visit all the matching files. When wildcards are actually
931 used and expanded, the value is a list of buffers
932 that are visiting the various files."
933 (setq filename
934 (abbreviate-file-name
935 (expand-file-name filename)))
936 (if (file-directory-p filename)
937 (if find-file-run-dired
938 (dired-noselect (if find-file-visit-truename
939 (abbreviate-file-name (file-truename filename))
940 filename))
941 (error "%s is a directory" filename))
942 (if (and wildcards
943 find-file-wildcards
944 (not (string-match "\\`/:" filename))
945 (string-match "[[*?]" filename))
946 (let ((files (condition-case nil
947 (file-expand-wildcards filename t)
948 (error (list filename))))
949 (find-file-wildcards nil))
950 (if (null files)
951 (find-file-noselect filename)
952 (car (mapcar #'(lambda (fn) (find-file-noselect fn))
953 files))))
954 (let* ((buf (get-file-buffer filename))
955 (truename (abbreviate-file-name (file-truename filename)))
956 (number (nthcdr 10 (file-attributes truename)))
957 ;; Find any buffer for a file which has same truename.
958 (other (and (not buf) (find-buffer-visiting filename))))
959 ;; Let user know if there is a buffer with the same truename.
960 (if other
961 (progn
962 (or nowarn
963 (string-equal filename (buffer-file-name other))
964 (message "%s and %s are the same file"
965 filename (buffer-file-name other)))
966 ;; Optionally also find that buffer.
967 (if (or find-file-existing-other-name find-file-visit-truename)
968 (setq buf other))))
969 (if buf
970 ;; We are using an existing buffer.
971 (progn
972 (or nowarn
973 (verify-visited-file-modtime buf)
974 (cond ((not (file-exists-p filename))
975 (error "File %s no longer exists!" filename))
976 ;; Certain files should be reverted automatically
977 ;; if they have changed on disk and not in the buffer.
978 ((and (not (buffer-modified-p buf))
979 (let ((tail revert-without-query)
980 (found nil))
981 (while tail
982 (if (string-match (car tail) filename)
983 (setq found t))
984 (setq tail (cdr tail)))
985 found))
986 (with-current-buffer buf
987 (message "Reverting file %s..." filename)
988 (revert-buffer t t)
989 (message "Reverting file %s...done" filename)))
990 ((yes-or-no-p
991 (if (string= (file-name-nondirectory filename)
992 (buffer-name buf))
993 (format
994 (if (buffer-modified-p buf)
995 "File %s changed on disk. Discard your edits? "
996 "File %s changed on disk. Reread from disk? ")
997 (file-name-nondirectory filename))
998 (format
999 (if (buffer-modified-p buf)
1000 "File %s changed on disk. Discard your edits in %s? "
1001 "File %s changed on disk. Reread from disk into %s? ")
1002 (file-name-nondirectory filename)
1003 (buffer-name buf))))
1004 (with-current-buffer buf
1005 (revert-buffer t t)))))
1006 (with-current-buffer buf
1007 (when (not (eq (not (null rawfile))
1008 (not (null find-file-literally))))
1009 (if (buffer-modified-p)
1010 (if (y-or-n-p (if rawfile
1011 "Save file and revisit literally? "
1012 "Save file and revisit non-literally? "))
1013 (progn
1014 (save-buffer)
1015 (find-file-noselect-1 buf filename nowarn
1016 rawfile truename number))
1017 (if (y-or-n-p (if rawfile
1018 "Discard your edits and revisit file literally? "
1019 "Discard your edits and revisit file non-literally? "))
1020 (find-file-noselect-1 buf filename nowarn
1021 rawfile truename number)
1022 (error (if rawfile "File already visited non-literally"
1023 "File already visited literally"))))
1024 (if (y-or-n-p (if rawfile
1025 "Revisit file literally? "
1026 "Revisit file non-literally? "))
1027 (find-file-noselect-1 buf filename nowarn
1028 rawfile truename number)
1029 (error (if rawfile "File already visited non-literally"
1030 "File already visited literally"))))))
1031 ;; Return the buffer we are using.
1032 buf)
1033 ;; Create a new buffer.
1034 (setq buf (create-file-buffer filename))
1035 (set-buffer-major-mode buf)
1036 ;; find-file-noselect-1 may use a different buffer.
1037 (find-file-noselect-1 buf filename nowarn
1038 rawfile truename number))))))
1040 (defun find-file-noselect-1 (buf filename nowarn rawfile truename number)
1041 (let ((inhibit-read-only t)
1042 error)
1043 (with-current-buffer buf
1044 (kill-local-variable 'find-file-literally)
1045 ;; Needed in case we are re-visiting the file with a different
1046 ;; text representation.
1047 (kill-local-variable 'buffer-file-coding-system)
1048 (erase-buffer)
1049 (and (default-value 'enable-multibyte-characters)
1050 (not rawfile)
1051 (set-buffer-multibyte t))
1052 (if rawfile
1053 (condition-case ()
1054 (insert-file-contents-literally filename t)
1055 (file-error
1056 (when (and (file-exists-p filename)
1057 (not (file-readable-p filename)))
1058 (kill-buffer buf)
1059 (signal 'file-error (list "File is not readable"
1060 filename)))
1061 ;; Unconditionally set error
1062 (setq error t)))
1063 (condition-case ()
1064 (insert-file-contents filename t)
1065 (file-error
1066 (when (and (file-exists-p filename)
1067 (not (file-readable-p filename)))
1068 (kill-buffer buf)
1069 (signal 'file-error (list "File is not readable"
1070 filename)))
1071 ;; Run find-file-not-found-hooks until one returns non-nil.
1072 (or (run-hook-with-args-until-success 'find-file-not-found-hooks)
1073 ;; If they fail too, set error.
1074 (setq error t)))))
1075 ;; Record the file's truename, and maybe use that as visited name.
1076 (if (equal filename buffer-file-name)
1077 (setq buffer-file-truename truename)
1078 (setq buffer-file-truename
1079 (abbreviate-file-name (file-truename buffer-file-name))))
1080 (setq buffer-file-number number)
1081 ;; On VMS, we may want to remember which directory in a search list
1082 ;; the file was found in.
1083 (and (eq system-type 'vax-vms)
1084 (let (logical)
1085 (if (string-match ":" (file-name-directory filename))
1086 (setq logical (substring (file-name-directory filename)
1087 0 (match-beginning 0))))
1088 (not (member logical find-file-not-true-dirname-list)))
1089 (setq buffer-file-name buffer-file-truename))
1090 (if find-file-visit-truename
1091 (setq buffer-file-name
1092 (setq filename
1093 (expand-file-name buffer-file-truename))))
1094 ;; Set buffer's default directory to that of the file.
1095 (setq default-directory (file-name-directory buffer-file-name))
1096 ;; Turn off backup files for certain file names. Since
1097 ;; this is a permanent local, the major mode won't eliminate it.
1098 (and (not (funcall backup-enable-predicate buffer-file-name))
1099 (progn
1100 (make-local-variable 'backup-inhibited)
1101 (setq backup-inhibited t)))
1102 (if rawfile
1103 (progn
1104 (set-buffer-multibyte nil)
1105 (setq buffer-file-coding-system 'no-conversion)
1106 (make-local-variable 'find-file-literally)
1107 (setq find-file-literally t))
1108 (after-find-file error (not nowarn)))
1109 (current-buffer))))
1111 (defun insert-file-contents-literally (filename &optional visit beg end replace)
1112 "Like `insert-file-contents', but only reads in the file literally.
1113 A buffer may be modified in several ways after reading into the buffer,
1114 to Emacs features such as format decoding, character code
1115 conversion, find-file-hooks, automatic uncompression, etc.
1117 This function ensures that none of these modifications will take place."
1118 (let ((format-alist nil)
1119 (after-insert-file-functions nil)
1120 (coding-system-for-read 'no-conversion)
1121 (coding-system-for-write 'no-conversion)
1122 (jka-compr-compression-info-list nil)
1123 (find-buffer-file-type-function
1124 (if (fboundp 'find-buffer-file-type)
1125 (symbol-function 'find-buffer-file-type)
1126 nil)))
1127 (unwind-protect
1128 (progn
1129 (fset 'find-buffer-file-type (lambda (filename) t))
1130 (insert-file-contents filename visit beg end replace))
1131 (if find-buffer-file-type-function
1132 (fset 'find-buffer-file-type find-buffer-file-type-function)
1133 (fmakunbound 'find-buffer-file-type)))))
1135 (defun insert-file-literally (filename)
1136 "Insert contents of file FILENAME into buffer after point with no conversion.
1138 This function is meant for the user to run interactively.
1139 Don't call it from programs! Use `insert-file-contents-literally' instead.
1140 \(Its calling sequence is different; see its documentation)."
1141 (interactive "*fInsert file literally: ")
1142 (if (file-directory-p filename)
1143 (signal 'file-error (list "Opening input file" "file is a directory"
1144 filename)))
1145 (let ((tem (insert-file-contents-literally filename)))
1146 (push-mark (+ (point) (car (cdr tem))))))
1148 (defvar find-file-literally nil
1149 "Non-nil if this buffer was made by `find-file-literally' or equivalent.
1150 This is a permanent local.")
1151 (put 'find-file-literally 'permanent-local t)
1153 (defun find-file-literally (filename)
1154 "Visit file FILENAME with no conversion of any kind.
1155 Format conversion and character code conversion are both disabled,
1156 and multibyte characters are disabled in the resulting buffer.
1157 The major mode used is Fundamental mode regardless of the file name,
1158 and local variable specifications in the file are ignored.
1159 Automatic uncompression is also disabled.
1161 You cannot absolutely rely on this function to result in
1162 visiting the file literally. If Emacs already has a buffer
1163 which is visiting the file, you get the existing buffer,
1164 regardless of whether it was created literally or not.
1166 In a Lisp program, if you want to be sure of accessing a file's
1167 contents literally, you should create a temporary buffer and then read
1168 the file contents into it using `insert-file-contents-literally'."
1169 (interactive "FFind file literally: ")
1170 (switch-to-buffer (find-file-noselect filename nil t)))
1172 (defvar after-find-file-from-revert-buffer nil)
1174 (defun after-find-file (&optional error warn noauto
1175 after-find-file-from-revert-buffer
1176 nomodes)
1177 "Called after finding a file and by the default revert function.
1178 Sets buffer mode, parses local variables.
1179 Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an
1180 error in reading the file. WARN non-nil means warn if there
1181 exists an auto-save file more recent than the visited file.
1182 NOAUTO means don't mess with auto-save mode.
1183 Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil
1184 means this call was from `revert-buffer'.
1185 Fifth arg NOMODES non-nil means don't alter the file's modes.
1186 Finishes by calling the functions in `find-file-hooks'
1187 unless NOMODES is non-nil."
1188 (setq buffer-read-only (not (file-writable-p buffer-file-name)))
1189 (if noninteractive
1191 (let* (not-serious
1192 (msg
1193 (cond ((and error (file-attributes buffer-file-name))
1194 (setq buffer-read-only t)
1195 "File exists, but cannot be read")
1196 ((not buffer-read-only)
1197 (if (and warn
1198 (file-newer-than-file-p (make-auto-save-file-name)
1199 buffer-file-name))
1200 (format "%s has auto save data; consider M-x recover-file"
1201 (file-name-nondirectory buffer-file-name))
1202 (setq not-serious t)
1203 (if error "(New file)" nil)))
1204 ((not error)
1205 (setq not-serious t)
1206 "Note: file is write protected")
1207 ((file-attributes (directory-file-name default-directory))
1208 "File not found and directory write-protected")
1209 ((file-exists-p (file-name-directory buffer-file-name))
1210 (setq buffer-read-only nil))
1212 (setq buffer-read-only nil)
1213 (if (file-exists-p (file-name-directory (directory-file-name (file-name-directory buffer-file-name))))
1214 "Use M-x make-directory RET RET to create the directory"
1215 "Use C-u M-x make-directory RET RET to create directory and its parents")))))
1216 (if msg
1217 (progn
1218 (message msg)
1219 (or not-serious (sit-for 1 nil t)))))
1220 (if (and auto-save-default (not noauto))
1221 (auto-save-mode t)))
1222 ;; Make people do a little extra work (C-x C-q)
1223 ;; before altering a backup file.
1224 (if (backup-file-name-p buffer-file-name)
1225 (setq buffer-read-only t))
1226 (if nomodes
1228 (and view-read-only view-mode
1229 (view-mode-disable))
1230 (normal-mode t)
1231 (if (and buffer-read-only view-read-only
1232 (not (eq (get major-mode 'mode-class) 'special)))
1233 (view-mode-enter))
1234 (run-hooks 'find-file-hooks)))
1236 (defun normal-mode (&optional find-file)
1237 "Choose the major mode for this buffer automatically.
1238 Also sets up any specified local variables of the file.
1239 Uses the visited file name, the -*- line, and the local variables spec.
1241 This function is called automatically from `find-file'. In that case,
1242 we may set up the file-specified mode and local variables,
1243 depending on the value of `enable-local-variables': if it is t, we do;
1244 if it is nil, we don't; otherwise, we query.
1245 In addition, if `local-enable-local-variables' is nil, we do
1246 not set local variables (though we do notice a mode specified with -*-.)
1248 `enable-local-variables' is ignored if you run `normal-mode' interactively,
1249 or from Lisp without specifying the optional argument FIND-FILE;
1250 in that case, this function acts as if `enable-local-variables' were t."
1251 (interactive)
1252 (or find-file (funcall (or default-major-mode 'fundamental-mode)))
1253 (condition-case err
1254 (set-auto-mode)
1255 (error (message "File mode specification error: %s"
1256 (prin1-to-string err))))
1257 (condition-case err
1258 (let ((enable-local-variables (or (not find-file)
1259 enable-local-variables)))
1260 (hack-local-variables))
1261 (error (message "File local-variables error: %s"
1262 (prin1-to-string err)))))
1264 (defvar auto-mode-alist
1265 '(("\\.te?xt\\'" . text-mode)
1266 ("\\.c\\'" . c-mode)
1267 ("\\.h\\'" . c-mode)
1268 ("\\.tex\\'" . tex-mode)
1269 ("\\.ltx\\'" . latex-mode)
1270 ("\\.el\\'" . emacs-lisp-mode)
1271 ("\\.scm\\'" . scheme-mode)
1272 ("\\.l\\'" . lisp-mode)
1273 ("\\.lisp\\'" . lisp-mode)
1274 ("\\.f\\'" . fortran-mode)
1275 ("\\.F\\'" . fortran-mode)
1276 ("\\.for\\'" . fortran-mode)
1277 ("\\.p\\'" . pascal-mode)
1278 ("\\.pas\\'" . pascal-mode)
1279 ("\\.ad[abs]\\'" . ada-mode)
1280 ("\\.\\([pP][Llm]\\|al\\)\\'" . perl-mode)
1281 ("\\.s?html?\\'" . html-mode)
1282 ("\\.cc\\'" . c++-mode)
1283 ("\\.hh\\'" . c++-mode)
1284 ("\\.hpp\\'" . c++-mode)
1285 ("\\.C\\'" . c++-mode)
1286 ("\\.H\\'" . c++-mode)
1287 ("\\.cpp\\'" . c++-mode)
1288 ("\\.cxx\\'" . c++-mode)
1289 ("\\.hxx\\'" . c++-mode)
1290 ("\\.c\\+\\+\\'" . c++-mode)
1291 ("\\.h\\+\\+\\'" . c++-mode)
1292 ("\\.m\\'" . objc-mode)
1293 ("\\.java\\'" . java-mode)
1294 ("\\.mk\\'" . makefile-mode)
1295 ("\\(M\\|m\\|GNUm\\)akefile\\(\\.in\\)?\\'" . makefile-mode)
1296 ("\\.am\\'" . makefile-mode) ;For Automake.
1297 ;;; Less common extensions come here
1298 ;;; so more common ones above are found faster.
1299 ("\\.texinfo\\'" . texinfo-mode)
1300 ("\\.te?xi\\'" . texinfo-mode)
1301 ("\\.s\\'" . asm-mode)
1302 ("\\.S\\'" . asm-mode)
1303 ("\\.asm\\'" . asm-mode)
1304 ("ChangeLog\\'" . change-log-mode)
1305 ("change\\.log\\'" . change-log-mode)
1306 ("changelo\\'" . change-log-mode)
1307 ("ChangeLog\\.[0-9]+\\'" . change-log-mode)
1308 ;; for MSDOS and MS-Windows (which are case-insensitive)
1309 ("changelog\\'" . change-log-mode)
1310 ("changelog\\.[0-9]+\\'" . change-log-mode)
1311 ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode)
1312 ("\\.scm\\.[0-9]*\\'" . scheme-mode)
1313 ("\\.[ck]?sh\\'\\|\\.shar\\'\\|/\\.z?profile\\'" . sh-mode)
1314 ("\\(/\\|\\`\\)\\.\\(bash_profile\\|z?login\\|bash_login\\|z?logout\\)\\'" . sh-mode)
1315 ("\\(/\\|\\`\\)\\.\\(bash_logout\\|[kz]shrc\\|bashrc\\|t?cshrc\\|esrc\\)\\'" . sh-mode)
1316 ("\\(/\\|\\`\\)\\.\\([kz]shenv\\|xinitrc\\|startxrc\\|xsession\\)\\'" . sh-mode)
1317 ("\\.m?spec$" . sh-mode)
1318 ("\\.mm\\'" . nroff-mode)
1319 ("\\.me\\'" . nroff-mode)
1320 ("\\.ms\\'" . nroff-mode)
1321 ("\\.man\\'" . nroff-mode)
1322 ("\\.\\(u?lpc\\|pike\\|pmod\\)\\'" . pike-mode)
1323 ;;; The following should come after the ChangeLog pattern
1324 ;;; for the sake of ChangeLog.1, etc.
1325 ;;; and after the .scm.[0-9] pattern too.
1326 ("\\.[12345678]\\'" . nroff-mode)
1327 ("\\.TeX\\'" . tex-mode)
1328 ("\\.sty\\'" . latex-mode)
1329 ("\\.cls\\'" . latex-mode) ;LaTeX 2e class
1330 ("\\.clo\\'" . latex-mode) ;LaTeX 2e class option
1331 ("\\.bbl\\'" . latex-mode)
1332 ("\\.bib\\'" . bibtex-mode)
1333 ("\\.sql\\'" . sql-mode)
1334 ("\\.m4\\'" . m4-mode)
1335 ("\\.mc\\'" . m4-mode)
1336 ("\\.mf\\'" . metafont-mode)
1337 ("\\.mp\\'" . metapost-mode)
1338 ("\\.vhdl?\\'" . vhdl-mode)
1339 ("\\.article\\'" . text-mode)
1340 ("\\.letter\\'" . text-mode)
1341 ("\\.tcl\\'" . tcl-mode)
1342 ("\\.exp\\'" . tcl-mode)
1343 ("\\.itcl\\'" . tcl-mode)
1344 ("\\.itk\\'" . tcl-mode)
1345 ("\\.icn\\'" . icon-mode)
1346 ("\\.sim\\'" . simula-mode)
1347 ("\\.mss\\'" . scribe-mode)
1348 ("\\.f90\\'" . f90-mode)
1349 ("\\.lsp\\'" . lisp-mode)
1350 ("\\.awk\\'" . awk-mode)
1351 ("\\.prolog\\'" . prolog-mode)
1352 ("\\.tar\\'" . tar-mode)
1353 ("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\)\\'" . archive-mode)
1354 ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|JAR\\)\\'" . archive-mode)
1355 ;; Mailer puts message to be edited in
1356 ;; /tmp/Re.... or Message
1357 ("\\`/tmp/Re" . text-mode)
1358 ("/Message[0-9]*\\'" . text-mode)
1359 ("/drafts/[0-9]+\\'" . mh-letter-mode)
1360 ("\\.zone\\'" . zone-mode)
1361 ;; some news reader is reported to use this
1362 ("\\`/tmp/fol/" . text-mode)
1363 ("\\.y\\'" . c-mode)
1364 ("\\.lex\\'" . c-mode)
1365 ("\\.oak\\'" . scheme-mode)
1366 ("\\.sgml?\\'" . sgml-mode)
1367 ("\\.xml\\'" . sgml-mode)
1368 ("\\.dtd\\'" . sgml-mode)
1369 ("\\.ds\\(ss\\)?l\\'" . dsssl-mode)
1370 ("\\.idl\\'" . idl-mode)
1371 ;; .emacs following a directory delimiter
1372 ;; in Unix, MSDOG or VMS syntax.
1373 ("[]>:/\\]\\..*emacs\\'" . emacs-lisp-mode)
1374 ("\\`\\..*emacs\\'" . emacs-lisp-mode)
1375 ;; _emacs following a directory delimiter
1376 ;; in MsDos syntax
1377 ("[:/]_emacs\\'" . emacs-lisp-mode)
1378 ("/crontab\\.X*[0-9]+\\'" . shell-script-mode)
1379 ("\\.ml\\'" . lisp-mode)
1380 ("\\.asn$" . snmp-mode)
1381 ("\\.mib$" . snmp-mode)
1382 ("\\.smi$" . snmp-mode)
1383 ("\\.as2$" . snmpv2-mode)
1384 ("\\.mi2$" . snmpv2-mode)
1385 ("\\.sm2$" . snmpv2-mode)
1386 ("\\.\\(diffs?\\|patch\\|rej\\)\\'" . diff-mode)
1387 ("\\.[eE]?[pP][sS]$" . ps-mode))
1389 Alist of filename patterns vs corresponding major mode functions.
1390 Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL).
1391 \(NON-NIL stands for anything that is not nil; the value does not matter.)
1392 Visiting a file whose name matches REGEXP specifies FUNCTION as the
1393 mode function to use. FUNCTION will be called, unless it is nil.
1395 If the element has the form (REGEXP FUNCTION NON-NIL), then after
1396 calling FUNCTION (if it's not nil), we delete the suffix that matched
1397 REGEXP and search the list again for another match.")
1400 (defvar interpreter-mode-alist
1401 '(("perl" . perl-mode)
1402 ("perl5" . perl-mode)
1403 ("miniperl" . perl-mode)
1404 ("wish" . tcl-mode)
1405 ("wishx" . tcl-mode)
1406 ("tcl" . tcl-mode)
1407 ("tclsh" . tcl-mode)
1408 ("awk" . awk-mode)
1409 ("mawk" . awk-mode)
1410 ("nawk" . awk-mode)
1411 ("gawk" . awk-mode)
1412 ("scm" . scheme-mode)
1413 ("ash" . sh-mode)
1414 ("bash" . sh-mode)
1415 ("csh" . sh-mode)
1416 ("dtksh" . sh-mode)
1417 ("es" . sh-mode)
1418 ("itcsh" . sh-mode)
1419 ("jsh" . sh-mode)
1420 ("ksh" . sh-mode)
1421 ("oash" . sh-mode)
1422 ("pdksh" . sh-mode)
1423 ("rc" . sh-mode)
1424 ("rpm" . sh-mode)
1425 ("sh" . sh-mode)
1426 ("sh5" . sh-mode)
1427 ("tcsh" . sh-mode)
1428 ("wksh" . sh-mode)
1429 ("wsh" . sh-mode)
1430 ("zsh" . sh-mode)
1431 ("tail" . text-mode)
1432 ("more" . text-mode)
1433 ("less" . text-mode)
1434 ("pg" . text-mode)
1435 ("make" . makefile-mode) ; Debian uses this
1436 ("guile" . scheme-mode)
1437 ("clisp" . lisp-mode))
1438 "Alist mapping interpreter names to major modes.
1439 This alist applies to files whose first line starts with `#!'.
1440 Each element looks like (INTERPRETER . MODE).
1441 The car of each element is compared with
1442 the name of the interpreter specified in the first line.
1443 If it matches, mode MODE is selected.")
1445 (defvar inhibit-first-line-modes-regexps '("\\.tar\\'" "\\.tgz\\'")
1446 "List of regexps; if one matches a file name, don't look for `-*-'.")
1448 (defvar inhibit-first-line-modes-suffixes nil
1449 "List of regexps for what to ignore, for `inhibit-first-line-modes-regexps'.
1450 When checking `inhibit-first-line-modes-regexps', we first discard
1451 from the end of the file name anything that matches one of these regexps.")
1453 (defvar user-init-file nil
1454 "File name, including directory, of user's initialization file.")
1456 (defun set-auto-mode (&optional just-from-file-name)
1457 "Select major mode appropriate for current buffer.
1458 This checks for a -*- mode tag in the buffer's text,
1459 compares the filename against the entries in `auto-mode-alist',
1460 or checks the interpreter that runs this file against
1461 `interpreter-mode-alist'.
1463 It does not check for the `mode:' local variable in the
1464 Local Variables section of the file; for that, use `hack-local-variables'.
1466 If `enable-local-variables' is nil, this function does not check for a
1467 -*- mode tag.
1469 If the optional argument JUST-FROM-FILE-NAME is non-nil,
1470 then we do not set anything but the major mode,
1471 and we don't even do that unless it would come from the file name."
1472 ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
1473 (let (beg end done modes)
1474 (save-excursion
1475 (goto-char (point-min))
1476 (skip-chars-forward " \t\n")
1477 (and enable-local-variables
1478 ;; Don't look for -*- if this file name matches any
1479 ;; of the regexps in inhibit-first-line-modes-regexps.
1480 (let ((temp inhibit-first-line-modes-regexps)
1481 (name (if buffer-file-name
1482 (file-name-sans-versions buffer-file-name)
1483 (buffer-name))))
1484 (while (let ((sufs inhibit-first-line-modes-suffixes))
1485 (while (and sufs (not (string-match (car sufs) name)))
1486 (setq sufs (cdr sufs)))
1487 sufs)
1488 (setq name (substring name 0 (match-beginning 0))))
1489 (while (and temp
1490 (not (string-match (car temp) name)))
1491 (setq temp (cdr temp)))
1492 (not temp))
1493 (search-forward "-*-" (save-excursion
1494 ;; If the file begins with "#!"
1495 ;; (exec interpreter magic), look
1496 ;; for mode frobs in the first two
1497 ;; lines. You cannot necessarily
1498 ;; put them in the first line of
1499 ;; such a file without screwing up
1500 ;; the interpreter invocation.
1501 (end-of-line (and (looking-at "^#!") 2))
1502 (point)) t)
1503 (progn
1504 (skip-chars-forward " \t")
1505 (setq beg (point))
1506 (search-forward "-*-"
1507 (save-excursion (end-of-line) (point))
1509 (progn
1510 (forward-char -3)
1511 (skip-chars-backward " \t")
1512 (setq end (point))
1513 (goto-char beg)
1514 (if (save-excursion (search-forward ":" end t))
1515 ;; Find all specifications for the `mode:' variable
1516 ;; and execute them left to right.
1517 (while (let ((case-fold-search t))
1518 (or (and (looking-at "mode:")
1519 (goto-char (match-end 0)))
1520 (re-search-forward "[ \t;]mode:" end t)))
1521 (skip-chars-forward " \t")
1522 (setq beg (point))
1523 (if (search-forward ";" end t)
1524 (forward-char -1)
1525 (goto-char end))
1526 (skip-chars-backward " \t")
1527 (setq modes (cons (intern (concat (downcase (buffer-substring beg (point))) "-mode"))
1528 modes)))
1529 ;; Simple -*-MODE-*- case.
1530 (setq modes (cons (intern (concat (downcase (buffer-substring beg end))
1531 "-mode"))
1532 modes))))))
1533 ;; If we found modes to use, invoke them now,
1534 ;; outside the save-excursion.
1535 (when modes
1536 (unless just-from-file-name
1537 (mapcar 'funcall (nreverse modes)))
1538 (setq done t))
1539 ;; If we didn't find a mode from a -*- line, try using the file name.
1540 (if (and (not done) buffer-file-name)
1541 (let ((name buffer-file-name)
1542 (keep-going t))
1543 ;; Remove backup-suffixes from file name.
1544 (setq name (file-name-sans-versions name))
1545 (while keep-going
1546 (setq keep-going nil)
1547 (let ((alist auto-mode-alist)
1548 (mode nil))
1549 ;; Find first matching alist entry.
1550 (let ((case-fold-search
1551 (memq system-type '(vax-vms windows-nt))))
1552 (while (and (not mode) alist)
1553 (if (string-match (car (car alist)) name)
1554 (if (and (consp (cdr (car alist)))
1555 (nth 2 (car alist)))
1556 (progn
1557 (setq mode (car (cdr (car alist)))
1558 name (substring name 0 (match-beginning 0))
1559 keep-going t))
1560 (setq mode (cdr (car alist))
1561 keep-going nil)))
1562 (setq alist (cdr alist))))
1563 (if mode
1564 ;; When JUST-FROM-FILE-NAME is set,
1565 ;; we are working on behalf of set-visited-file-name.
1566 ;; In that case, if the major mode specified is the
1567 ;; same one we already have, don't actually reset it.
1568 ;; We don't want to lose minor modes such as Font Lock.
1569 (unless (and just-from-file-name (eq mode major-mode))
1570 (funcall mode))
1571 ;; If we can't deduce a mode from the file name,
1572 ;; look for an interpreter specified in the first line.
1573 ;; As a special case, allow for things like "#!/bin/env perl",
1574 ;; which finds the interpreter anywhere in $PATH.
1575 (let ((interpreter
1576 (save-excursion
1577 (goto-char (point-min))
1578 (if (looking-at "#![ \t]?\\([^ \t\n]*/bin/env[ \t]\\)?\\([^ \t\n]+\\)")
1579 (buffer-substring (match-beginning 2)
1580 (match-end 2))
1581 "")))
1582 elt)
1583 ;; Map interpreter name to a mode.
1584 (setq elt (assoc (file-name-nondirectory interpreter)
1585 interpreter-mode-alist))
1586 (unless just-from-file-name
1587 (if elt
1588 (funcall (cdr elt))))))))))))
1590 (defun hack-local-variables-prop-line ()
1591 ;; Set local variables specified in the -*- line.
1592 ;; Ignore any specification for `mode:' and `coding:';
1593 ;; set-auto-mode should already have handled `mode:',
1594 ;; set-auto-coding should already have handled `coding:'.
1595 (save-excursion
1596 (goto-char (point-min))
1597 (let ((result nil)
1598 (end (save-excursion (end-of-line (and (looking-at "^#!") 2)) (point)))
1599 (enable-local-variables
1600 (and local-enable-local-variables enable-local-variables)))
1601 ;; Parse the -*- line into the `result' alist.
1602 (cond ((not (search-forward "-*-" end t))
1603 ;; doesn't have one.
1604 nil)
1605 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
1606 ;; Simple form: "-*- MODENAME -*-". Already handled.
1607 nil)
1609 ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
1610 ;; (last ";" is optional).
1611 (save-excursion
1612 (if (search-forward "-*-" end t)
1613 (setq end (- (point) 3))
1614 (error "-*- not terminated before end of line")))
1615 (while (< (point) end)
1616 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
1617 (error "malformed -*- line"))
1618 (goto-char (match-end 0))
1619 ;; There used to be a downcase here,
1620 ;; but the manual didn't say so,
1621 ;; and people want to set var names that aren't all lc.
1622 (let ((key (intern (buffer-substring
1623 (match-beginning 1)
1624 (match-end 1))))
1625 (val (save-restriction
1626 (narrow-to-region (point) end)
1627 (read (current-buffer)))))
1628 ;; It is traditional to ignore
1629 ;; case when checking for `mode' in set-auto-mode,
1630 ;; so we must do that here as well.
1631 ;; That is inconsistent, but we're stuck with it.
1632 ;; The same can be said for `coding' in set-auto-coding.
1633 (or (equal (downcase (symbol-name key)) "mode")
1634 (equal (downcase (symbol-name key)) "coding")
1635 (setq result (cons (cons key val) result)))
1636 (skip-chars-forward " \t;")))
1637 (setq result (nreverse result))))
1639 (if (and result
1640 (or (eq enable-local-variables t)
1641 (and enable-local-variables
1642 (save-window-excursion
1643 (condition-case nil
1644 (switch-to-buffer (current-buffer))
1645 (error
1646 ;; If we fail to switch in the selected window,
1647 ;; it is probably a minibuffer.
1648 ;; So try another window.
1649 (condition-case nil
1650 (switch-to-buffer-other-window (current-buffer))
1651 (error
1652 (switch-to-buffer-other-frame (current-buffer))))))
1653 (y-or-n-p (format "Set local variables as specified in -*- line of %s? "
1654 (file-name-nondirectory buffer-file-name)))))))
1655 (let ((enable-local-eval enable-local-eval))
1656 (while result
1657 (hack-one-local-variable (car (car result)) (cdr (car result)))
1658 (setq result (cdr result))))))))
1660 (defvar hack-local-variables-hook nil
1661 "Normal hook run after processing a file's local variables specs.
1662 Major modes can use this to examine user-specified local variables
1663 in order to initialize other data structure based on them.")
1665 (defun hack-local-variables (&optional mode-only)
1666 "Parse and put into effect this buffer's local variables spec.
1667 If MODE-ONLY is non-nil, all we do is check whether the major mode
1668 is specified, returning t if it is specified."
1669 (unless mode-only
1670 (hack-local-variables-prop-line))
1671 ;; Look for "Local variables:" line in last page.
1672 (let (mode-specified
1673 (enable-local-variables
1674 (and local-enable-local-variables enable-local-variables)))
1675 (save-excursion
1676 (goto-char (point-max))
1677 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
1678 (if (let ((case-fold-search t))
1679 (and (search-forward "Local Variables:" nil t)
1680 (or (eq enable-local-variables t)
1681 mode-only
1682 (and enable-local-variables
1683 (save-window-excursion
1684 (switch-to-buffer (current-buffer))
1685 (save-excursion
1686 (beginning-of-line)
1687 (set-window-start (selected-window) (point)))
1688 (y-or-n-p (format "Set local variables as specified at end of %s? "
1689 (if buffer-file-name
1690 (file-name-nondirectory
1691 buffer-file-name)
1692 (concat "buffer "
1693 (buffer-name))))))))))
1694 (let ((continue t)
1695 prefix prefixlen suffix beg
1696 mode-specified
1697 (enable-local-eval enable-local-eval))
1698 ;; The prefix is what comes before "local variables:" in its line.
1699 ;; The suffix is what comes after "local variables:" in its line.
1700 (skip-chars-forward " \t")
1701 (or (eolp)
1702 (setq suffix (buffer-substring (point)
1703 (progn (end-of-line) (point)))))
1704 (goto-char (match-beginning 0))
1705 (or (bolp)
1706 (setq prefix
1707 (buffer-substring (point)
1708 (progn (beginning-of-line) (point)))))
1710 (if prefix (setq prefixlen (length prefix)
1711 prefix (regexp-quote prefix)))
1712 (if suffix (setq suffix (concat (regexp-quote suffix) "$")))
1713 (while continue
1714 ;; Look at next local variable spec.
1715 (if selective-display (re-search-forward "[\n\C-m]")
1716 (forward-line 1))
1717 ;; Skip the prefix, if any.
1718 (if prefix
1719 (if (looking-at prefix)
1720 (forward-char prefixlen)
1721 (error "Local variables entry is missing the prefix")))
1722 ;; Find the variable name; strip whitespace.
1723 (skip-chars-forward " \t")
1724 (setq beg (point))
1725 (skip-chars-forward "^:\n")
1726 (if (eolp) (error "Missing colon in local variables entry"))
1727 (skip-chars-backward " \t")
1728 (let* ((str (buffer-substring beg (point)))
1729 (var (read str))
1730 val)
1731 ;; Setting variable named "end" means end of list.
1732 (if (string-equal (downcase str) "end")
1733 (setq continue nil)
1734 ;; Otherwise read the variable value.
1735 (skip-chars-forward "^:")
1736 (forward-char 1)
1737 (setq val (read (current-buffer)))
1738 (skip-chars-backward "\n")
1739 (skip-chars-forward " \t")
1740 (or (if suffix (looking-at suffix) (eolp))
1741 (error "Local variables entry is terminated incorrectly"))
1742 (if mode-only
1743 (if (eq var 'mode)
1744 (setq mode-specified t))
1745 ;; Set the variable. "Variables" mode and eval are funny.
1746 (hack-one-local-variable var val))))))))
1747 (unless mode-only
1748 (run-hooks 'hack-local-variables-hook))
1749 mode-specified))
1751 (defvar ignored-local-variables
1752 '(enable-local-eval)
1753 "Variables to be ignored in a file's local variable spec.")
1755 ;; Get confirmation before setting these variables as locals in a file.
1756 (put 'debugger 'risky-local-variable t)
1757 (put 'enable-local-eval 'risky-local-variable t)
1758 (put 'ignored-local-variables 'risky-local-variable t)
1759 (put 'eval 'risky-local-variable t)
1760 (put 'file-name-handler-alist 'risky-local-variable t)
1761 (put 'minor-mode-map-alist 'risky-local-variable t)
1762 (put 'after-load-alist 'risky-local-variable t)
1763 (put 'buffer-file-name 'risky-local-variable t)
1764 (put 'buffer-auto-save-file-name 'risky-local-variable t)
1765 (put 'buffer-file-truename 'risky-local-variable t)
1766 (put 'exec-path 'risky-local-variable t)
1767 (put 'load-path 'risky-local-variable t)
1768 (put 'exec-directory 'risky-local-variable t)
1769 (put 'process-environment 'risky-local-variable t)
1770 (put 'dabbrev-case-fold-search 'risky-local-variable t)
1771 (put 'dabbrev-case-replace 'risky-local-variable t)
1772 ;; Don't wait for outline.el to be loaded, for the sake of outline-minor-mode.
1773 (put 'outline-level 'risky-local-variable t)
1774 (put 'rmail-output-file-alist 'risky-local-variable t)
1776 ;; This one is safe because the user gets to check it before it is used.
1777 (put 'compile-command 'safe-local-variable t)
1779 (defun hack-one-local-variable-quotep (exp)
1780 (and (consp exp) (eq (car exp) 'quote) (consp (cdr exp))))
1782 ;; "Set" one variable in a local variables spec.
1783 ;; A few variable names are treated specially.
1784 (defun hack-one-local-variable (var val)
1785 (cond ((eq var 'mode)
1786 (funcall (intern (concat (downcase (symbol-name val))
1787 "-mode"))))
1788 ((eq var 'coding)
1789 ;; We have already handled coding: tag in set-auto-coding.
1790 nil)
1791 ((memq var ignored-local-variables)
1792 nil)
1793 ;; "Setting" eval means either eval it or do nothing.
1794 ;; Likewise for setting hook variables.
1795 ((or (get var 'risky-local-variable)
1796 (and
1797 (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$\\|-predicate$"
1798 (symbol-name var))
1799 (not (get var 'safe-local-variable))))
1800 ;; Permit evalling a put of a harmless property.
1801 ;; if the args do nothing tricky.
1802 (if (or (and (eq var 'eval)
1803 (consp val)
1804 (eq (car val) 'put)
1805 (hack-one-local-variable-quotep (nth 1 val))
1806 (hack-one-local-variable-quotep (nth 2 val))
1807 ;; Only allow safe values of lisp-indent-hook;
1808 ;; not functions.
1809 (or (numberp (nth 3 val))
1810 (equal (nth 3 val) ''defun))
1811 (memq (nth 1 (nth 2 val))
1812 '(lisp-indent-hook)))
1813 ;; Permit eval if not root and user says ok.
1814 (and (not (zerop (user-uid)))
1815 (or (eq enable-local-eval t)
1816 (and enable-local-eval
1817 (save-window-excursion
1818 (switch-to-buffer (current-buffer))
1819 (save-excursion
1820 (beginning-of-line)
1821 (set-window-start (selected-window) (point)))
1822 (setq enable-local-eval
1823 (y-or-n-p (format "Process `eval' or hook local variables in %s? "
1824 (if buffer-file-name
1825 (concat "file " (file-name-nondirectory buffer-file-name))
1826 (concat "buffer " (buffer-name)))))))))))
1827 (if (eq var 'eval)
1828 (save-excursion (eval val))
1829 (make-local-variable var)
1830 (set var val))
1831 (message "Ignoring `eval:' in the local variables list")))
1832 ;; Ordinary variable, really set it.
1833 (t (make-local-variable var)
1834 (set var val))))
1837 (defcustom change-major-mode-with-file-name t
1838 "*Non-nil means \\[write-file] should set the major mode from the file name.
1839 However, the mode will not be changed if
1840 \(1) a local variables list or the `-*-' line specifies a major mode, or
1841 \(2) the current major mode is a \"special\" mode,
1842 \ not suitable for ordinary files, or
1843 \(3) the new file name does not particularly specify any mode."
1844 :type 'boolean
1845 :group 'editing-basics)
1847 (defun set-visited-file-name (filename &optional no-query along-with-file)
1848 "Change name of file visited in current buffer to FILENAME.
1849 The next time the buffer is saved it will go in the newly specified file.
1850 nil or empty string as argument means make buffer not be visiting any file.
1851 Remember to delete the initial contents of the minibuffer
1852 if you wish to pass an empty string as the argument.
1854 The optional second argument NO-QUERY, if non-nil, inhibits asking for
1855 confirmation in the case where another buffer is already visiting FILENAME.
1857 The optional third argument ALONG-WITH-FILE, if non-nil, means that
1858 the old visited file has been renamed to the new name FILENAME."
1859 (interactive "FSet visited file name: ")
1860 (if (buffer-base-buffer)
1861 (error "An indirect buffer cannot visit a file"))
1862 (let (truename)
1863 (if filename
1864 (setq filename
1865 (if (string-equal filename "")
1867 (expand-file-name filename))))
1868 (if filename
1869 (progn
1870 (setq truename (file-truename filename))
1871 (if find-file-visit-truename
1872 (setq filename truename))))
1873 (let ((buffer (and filename (find-buffer-visiting filename))))
1874 (and buffer (not (eq buffer (current-buffer)))
1875 (not no-query)
1876 (not (y-or-n-p (message "A buffer is visiting %s; proceed? "
1877 filename)))
1878 (error "Aborted")))
1879 (or (equal filename buffer-file-name)
1880 (progn
1881 (and filename (lock-buffer filename))
1882 (unlock-buffer)))
1883 (setq buffer-file-name filename)
1884 (if filename ; make buffer name reflect filename.
1885 (let ((new-name (file-name-nondirectory buffer-file-name)))
1886 (if (string= new-name "")
1887 (error "Empty file name"))
1888 (if (eq system-type 'vax-vms)
1889 (setq new-name (downcase new-name)))
1890 (setq default-directory (file-name-directory buffer-file-name))
1891 (or (string= new-name (buffer-name))
1892 (rename-buffer new-name t))))
1893 (setq buffer-backed-up nil)
1894 (or along-with-file
1895 (clear-visited-file-modtime))
1896 ;; Abbreviate the file names of the buffer.
1897 (if truename
1898 (progn
1899 (setq buffer-file-truename (abbreviate-file-name truename))
1900 (if find-file-visit-truename
1901 (setq buffer-file-name buffer-file-truename))))
1902 (setq buffer-file-number
1903 (if filename
1904 (nthcdr 10 (file-attributes buffer-file-name))
1905 nil)))
1906 ;; write-file-hooks is normally used for things like ftp-find-file
1907 ;; that visit things that are not local files as if they were files.
1908 ;; Changing to visit an ordinary local file instead should flush the hook.
1909 (kill-local-variable 'write-file-hooks)
1910 (kill-local-variable 'local-write-file-hooks)
1911 (kill-local-variable 'revert-buffer-function)
1912 (kill-local-variable 'backup-inhibited)
1913 ;; If buffer was read-only because of version control,
1914 ;; that reason is gone now, so make it writable.
1915 (if vc-mode
1916 (setq buffer-read-only nil))
1917 (kill-local-variable 'vc-mode)
1918 ;; Turn off backup files for certain file names.
1919 ;; Since this is a permanent local, the major mode won't eliminate it.
1920 (and buffer-file-name
1921 (not (funcall backup-enable-predicate buffer-file-name))
1922 (progn
1923 (make-local-variable 'backup-inhibited)
1924 (setq backup-inhibited t)))
1925 (let ((oauto buffer-auto-save-file-name))
1926 ;; If auto-save was not already on, turn it on if appropriate.
1927 (if (not buffer-auto-save-file-name)
1928 (and buffer-file-name auto-save-default
1929 (auto-save-mode t))
1930 ;; If auto save is on, start using a new name.
1931 ;; We deliberately don't rename or delete the old auto save
1932 ;; for the old visited file name. This is because perhaps
1933 ;; the user wants to save the new state and then compare with the
1934 ;; previous state from the auto save file.
1935 (setq buffer-auto-save-file-name
1936 (make-auto-save-file-name)))
1937 ;; Rename the old auto save file if any.
1938 (and oauto buffer-auto-save-file-name
1939 (file-exists-p oauto)
1940 (rename-file oauto buffer-auto-save-file-name t)))
1941 (and buffer-file-name
1942 (not along-with-file)
1943 (set-buffer-modified-p t))
1944 ;; Update the major mode, if the file name determines it.
1945 (condition-case nil
1946 ;; Don't change the mode if it is special.
1947 (or (not change-major-mode-with-file-name)
1948 (get major-mode 'mode-class)
1949 ;; Don't change the mode if the local variable list specifies it.
1950 (hack-local-variables t)
1951 (set-auto-mode t))
1952 (error nil)))
1954 (defun write-file (filename &optional confirm)
1955 "Write current buffer into file FILENAME.
1956 This makes the buffer visit that file, and marks it as not modified.
1958 If you specify just a directory name as FILENAME, that means to use
1959 the default file name but in that directory. You can also yank
1960 the default file name into the minibuffer to edit it, using M-n.
1962 If the buffer is not already visiting a file, the default file name
1963 for the output file is the buffer name.
1965 If optional second arg CONFIRM is non-nil, this function
1966 asks for confirmation before overwriting an existing file.
1967 Interactively, confirmation is required unless you supply a prefix argument."
1968 ;; (interactive "FWrite file: ")
1969 (interactive
1970 (list (if buffer-file-name
1971 (read-file-name "Write file: "
1972 nil nil nil nil)
1973 (read-file-name "Write file: " default-directory
1974 (expand-file-name
1975 (file-name-nondirectory (buffer-name))
1976 default-directory)
1977 nil nil))
1978 (not current-prefix-arg)))
1979 (or (null filename) (string-equal filename "")
1980 (progn
1981 ;; If arg is just a directory,
1982 ;; use the default file name, but in that directory.
1983 (if (file-directory-p filename)
1984 (setq filename (concat (file-name-as-directory filename)
1985 (file-name-nondirectory
1986 (or buffer-file-name (buffer-name))))))
1987 (and confirm
1988 (file-exists-p filename)
1989 (or (y-or-n-p (format "File `%s' exists; overwrite? " filename))
1990 (error "Canceled")))
1991 (set-visited-file-name filename (not confirm))))
1992 (set-buffer-modified-p t)
1993 ;; Make buffer writable if file is writable.
1994 (and buffer-file-name
1995 (file-writable-p buffer-file-name)
1996 (setq buffer-read-only nil))
1997 (save-buffer))
1999 (defun backup-buffer ()
2000 "Make a backup of the disk file visited by the current buffer, if appropriate.
2001 This is normally done before saving the buffer the first time.
2002 If the value is non-nil, it is the result of `file-modes' on the original
2003 file; this means that the caller, after saving the buffer, should change
2004 the modes of the new file to agree with the old modes.
2006 A backup may be done by renaming or by copying; see documentation of
2007 variable `make-backup-files'. If it's done by renaming, then the file is
2008 no longer accessible under its old name."
2009 (if (and make-backup-files (not backup-inhibited)
2010 (not buffer-backed-up)
2011 (file-exists-p buffer-file-name)
2012 (memq (aref (elt (file-attributes buffer-file-name) 8) 0)
2013 '(?- ?l)))
2014 (let ((real-file-name buffer-file-name)
2015 backup-info backupname targets setmodes)
2016 ;; If specified name is a symbolic link, chase it to the target.
2017 ;; Thus we make the backups in the directory where the real file is.
2018 (setq real-file-name (file-chase-links real-file-name))
2019 (setq backup-info (find-backup-file-name real-file-name)
2020 backupname (car backup-info)
2021 targets (cdr backup-info))
2022 ;;; (if (file-directory-p buffer-file-name)
2023 ;;; (error "Cannot save buffer in directory %s" buffer-file-name))
2024 (if backup-info
2025 (condition-case ()
2026 (let ((delete-old-versions
2027 ;; If have old versions to maybe delete,
2028 ;; ask the user to confirm now, before doing anything.
2029 ;; But don't actually delete til later.
2030 (and targets
2031 (or (eq delete-old-versions t) (eq delete-old-versions nil))
2032 (or delete-old-versions
2033 (y-or-n-p (format "Delete excess backup versions of %s? "
2034 real-file-name))))))
2035 ;; Actually write the back up file.
2036 (condition-case ()
2037 (if (or file-precious-flag
2038 ; (file-symlink-p buffer-file-name)
2039 backup-by-copying
2040 (and backup-by-copying-when-linked
2041 (> (file-nlinks real-file-name) 1))
2042 (and (or backup-by-copying-when-mismatch
2043 (integerp backup-by-copying-when-privileged-mismatch))
2044 (let ((attr (file-attributes real-file-name)))
2045 (and (or backup-by-copying-when-mismatch
2046 (and (integerp (nth 2 attr))
2047 (integerp backup-by-copying-when-privileged-mismatch)
2048 (<= (nth 2 attr) backup-by-copying-when-privileged-mismatch)))
2049 (or (nth 9 attr)
2050 (not (file-ownership-preserved-p real-file-name)))))))
2051 (condition-case ()
2052 (copy-file real-file-name backupname t t)
2053 (file-error
2054 ;; If copying fails because file BACKUPNAME
2055 ;; is not writable, delete that file and try again.
2056 (if (and (file-exists-p backupname)
2057 (not (file-writable-p backupname)))
2058 (delete-file backupname))
2059 (copy-file real-file-name backupname t t)))
2060 ;; rename-file should delete old backup.
2061 (rename-file real-file-name backupname t)
2062 (setq setmodes (file-modes backupname)))
2063 (file-error
2064 ;; If trouble writing the backup, write it in ~.
2065 (setq backupname (expand-file-name
2066 (convert-standard-filename
2067 "~/%backup%~")))
2068 (message "Cannot write backup file; backing up in %s"
2069 (file-name-nondirectory backupname))
2070 (sleep-for 1)
2071 (condition-case ()
2072 (copy-file real-file-name backupname t t)
2073 (file-error
2074 ;; If copying fails because file BACKUPNAME
2075 ;; is not writable, delete that file and try again.
2076 (if (and (file-exists-p backupname)
2077 (not (file-writable-p backupname)))
2078 (delete-file backupname))
2079 (copy-file real-file-name backupname t t)))))
2080 (setq buffer-backed-up t)
2081 ;; Now delete the old versions, if desired.
2082 (if delete-old-versions
2083 (while targets
2084 (condition-case ()
2085 (delete-file (car targets))
2086 (file-error nil))
2087 (setq targets (cdr targets))))
2088 setmodes)
2089 (file-error nil))))))
2091 (defun file-name-sans-versions (name &optional keep-backup-version)
2092 "Return FILENAME sans backup versions or strings.
2093 This is a separate procedure so your site-init or startup file can
2094 redefine it.
2095 If the optional argument KEEP-BACKUP-VERSION is non-nil,
2096 we do not remove backup version numbers, only true file version numbers."
2097 (let ((handler (find-file-name-handler name 'file-name-sans-versions)))
2098 (if handler
2099 (funcall handler 'file-name-sans-versions name keep-backup-version)
2100 (substring name 0
2101 (if (eq system-type 'vax-vms)
2102 ;; VMS version number is (a) semicolon, optional
2103 ;; sign, zero or more digits or (b) period, option
2104 ;; sign, zero or more digits, provided this is the
2105 ;; second period encountered outside of the
2106 ;; device/directory part of the file name.
2107 (or (string-match ";[-+]?[0-9]*\\'" name)
2108 (if (string-match "\\.[^]>:]*\\(\\.[-+]?[0-9]*\\)\\'"
2109 name)
2110 (match-beginning 1))
2111 (length name))
2112 (if keep-backup-version
2113 (length name)
2114 (or (string-match "\\.~[0-9.]+~\\'" name)
2115 (string-match "~\\'" name)
2116 (length name))))))))
2118 (defun file-ownership-preserved-p (file)
2119 "Returns t if deleting FILE and rewriting it would preserve the owner."
2120 (let ((handler (find-file-name-handler file 'file-ownership-preserved-p)))
2121 (if handler
2122 (funcall handler 'file-ownership-preserved-p file)
2123 (let ((attributes (file-attributes file)))
2124 ;; Return t if the file doesn't exist, since it's true that no
2125 ;; information would be lost by an (attempted) delete and create.
2126 (or (null attributes)
2127 (= (nth 2 attributes) (user-uid)))))))
2129 (defun file-name-sans-extension (filename)
2130 "Return FILENAME sans final \"extension\".
2131 The extension, in a file name, is the part that follows the last `.'."
2132 (save-match-data
2133 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
2134 directory)
2135 (if (string-match "\\.[^.]*\\'" file)
2136 (if (setq directory (file-name-directory filename))
2137 (expand-file-name (substring file 0 (match-beginning 0))
2138 directory)
2139 (substring file 0 (match-beginning 0)))
2140 filename))))
2142 (defun file-name-extension (filename &optional period)
2143 "Return FILENAME's final \"extension\".
2144 The extension, in a file name, is the part that follows the last `.'.
2145 Return nil for extensionless file names such as `foo'.
2146 Return the empty string for file names such as `foo.'.
2148 If PERIOD is non-nil, then the returned value includes the period
2149 that delimits the extension, and if FILENAME has no extension,
2150 the value is \"\"."
2151 (save-match-data
2152 (let ((file (file-name-sans-versions (file-name-nondirectory filename))))
2153 (if (string-match "\\.[^.]*\\'" file)
2154 (substring file (+ (match-beginning 0) (if period 0 1)))
2155 (if period
2156 "")))))
2158 (defun make-backup-file-name (file)
2159 "Create the non-numeric backup file name for FILE.
2160 This is a separate function so you can redefine it for customization."
2161 (if (and (eq system-type 'ms-dos)
2162 (not (msdos-long-file-names)))
2163 (let ((fn (file-name-nondirectory file)))
2164 (concat (file-name-directory file)
2166 (and (string-match "\\`[^.]+\\'" fn)
2167 (concat (match-string 0 fn) ".~"))
2168 (and (string-match "\\`[^.]+\\.\\(..?\\)?" fn)
2169 (concat (match-string 0 fn) "~")))))
2170 (concat file "~")))
2172 (defun backup-file-name-p (file)
2173 "Return non-nil if FILE is a backup file name (numeric or not).
2174 This is a separate function so you can redefine it for customization.
2175 You may need to redefine `file-name-sans-versions' as well."
2176 (string-match "~\\'" file))
2178 (defvar backup-extract-version-start)
2180 ;; This is used in various files.
2181 ;; The usage of backup-extract-version-start is not very clean,
2182 ;; but I can't see a good alternative, so as of now I am leaving it alone.
2183 (defun backup-extract-version (fn)
2184 "Given the name of a numeric backup file, return the backup number.
2185 Uses the free variable `backup-extract-version-start', whose value should be
2186 the index in the name where the version number begins."
2187 (if (and (string-match "[0-9]+~$" fn backup-extract-version-start)
2188 (= (match-beginning 0) backup-extract-version-start))
2189 (string-to-int (substring fn backup-extract-version-start -1))
2192 ;; I believe there is no need to alter this behavior for VMS;
2193 ;; since backup files are not made on VMS, it should not get called.
2194 (defun find-backup-file-name (fn)
2195 "Find a file name for a backup file, and suggestions for deletions.
2196 Value is a list whose car is the name for the backup file
2197 and whose cdr is a list of old versions to consider deleting now.
2198 If the value is nil, don't make a backup."
2199 (let ((handler (find-file-name-handler fn 'find-backup-file-name)))
2200 ;; Run a handler for this function so that ange-ftp can refuse to do it.
2201 (if handler
2202 (funcall handler 'find-backup-file-name fn)
2203 (if (eq version-control 'never)
2204 (list (make-backup-file-name fn))
2205 (let* ((base-versions (concat (file-name-nondirectory fn) ".~"))
2206 (backup-extract-version-start (length base-versions))
2207 possibilities
2208 (versions nil)
2209 (high-water-mark 0)
2210 (deserve-versions-p nil)
2211 (number-to-delete 0))
2212 (condition-case ()
2213 (setq possibilities (file-name-all-completions
2214 base-versions
2215 (file-name-directory fn))
2216 versions (sort (mapcar
2217 (function backup-extract-version)
2218 possibilities)
2220 high-water-mark (apply 'max 0 versions)
2221 deserve-versions-p (or version-control
2222 (> high-water-mark 0))
2223 number-to-delete (- (length versions)
2224 kept-old-versions kept-new-versions -1))
2225 (file-error
2226 (setq possibilities nil)))
2227 (if (not deserve-versions-p)
2228 (list (make-backup-file-name fn))
2229 (cons (concat fn ".~" (int-to-string (1+ high-water-mark)) "~")
2230 (if (and (> number-to-delete 0)
2231 ;; Delete nothing if there is overflow
2232 ;; in the number of versions to keep.
2233 (>= (+ kept-new-versions kept-old-versions -1) 0))
2234 (mapcar (function (lambda (n)
2235 (concat fn ".~" (int-to-string n) "~")))
2236 (let ((v (nthcdr kept-old-versions versions)))
2237 (rplacd (nthcdr (1- number-to-delete) v) ())
2238 v))))))))))
2240 (defun file-nlinks (filename)
2241 "Return number of names file FILENAME has."
2242 (car (cdr (file-attributes filename))))
2244 (defun file-relative-name (filename &optional directory)
2245 "Convert FILENAME to be relative to DIRECTORY (default: default-directory).
2246 This function returns a relative file name which is equivalent to FILENAME
2247 when used with that default directory as the default.
2248 If this is impossible (which can happen on MSDOS and Windows
2249 when the file name and directory use different drive names)
2250 then it returns FILENAME."
2251 (save-match-data
2252 (let ((fname (expand-file-name filename)))
2253 (setq directory (file-name-as-directory
2254 (expand-file-name (or directory default-directory))))
2255 ;; On Microsoft OSes, if FILENAME and DIRECTORY have different
2256 ;; drive names, they can't be relative, so return the absolute name.
2257 (if (and (or (eq system-type 'ms-dos)
2258 (eq system-type 'windows-nt))
2259 (not (string-equal (substring fname 0 2)
2260 (substring directory 0 2))))
2261 filename
2262 (let ((ancestor ".")
2263 (fname-dir (file-name-as-directory fname)))
2264 (while (and (not (string-match (concat "^" (regexp-quote directory)) fname-dir))
2265 (not (string-match (concat "^" (regexp-quote directory)) fname)))
2266 (setq directory (file-name-directory (substring directory 0 -1))
2267 ancestor (if (equal ancestor ".")
2268 ".."
2269 (concat "../" ancestor))))
2270 ;; Now ancestor is empty, or .., or ../.., etc.
2271 (if (string-match (concat "^" (regexp-quote directory)) fname)
2272 ;; We matched within FNAME's directory part.
2273 ;; Add the rest of FNAME onto ANCESTOR.
2274 (let ((rest (substring fname (match-end 0))))
2275 (if (and (equal ancestor ".")
2276 (not (equal rest "")))
2277 ;; But don't bother with ANCESTOR if it would give us `./'.
2278 rest
2279 (concat (file-name-as-directory ancestor) rest)))
2280 ;; We matched FNAME's directory equivalent.
2281 ancestor))))))
2283 (defun save-buffer (&optional args)
2284 "Save current buffer in visited file if modified. Versions described below.
2285 By default, makes the previous version into a backup file
2286 if previously requested or if this is the first save.
2287 With 1 \\[universal-argument], marks this version
2288 to become a backup when the next save is done.
2289 With 2 \\[universal-argument]'s,
2290 unconditionally makes the previous version into a backup file.
2291 With 3 \\[universal-argument]'s, marks this version
2292 to become a backup when the next save is done,
2293 and unconditionally makes the previous version into a backup file.
2295 With argument of 0, never make the previous version into a backup file.
2297 If a file's name is FOO, the names of its numbered backup versions are
2298 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~.
2299 Numeric backups (rather than FOO~) will be made if value of
2300 `version-control' is not the atom `never' and either there are already
2301 numeric versions of the file being backed up, or `version-control' is
2302 non-nil.
2303 We don't want excessive versions piling up, so there are variables
2304 `kept-old-versions', which tells Emacs how many oldest versions to keep,
2305 and `kept-new-versions', which tells how many newest versions to keep.
2306 Defaults are 2 old versions and 2 new.
2307 `dired-kept-versions' controls dired's clean-directory (.) command.
2308 If `delete-old-versions' is nil, system will query user
2309 before trimming versions. Otherwise it does it silently.
2311 If `vc-make-backup-files' is nil, which is the default,
2312 no backup files are made for files managed by version control.
2313 (This is because the version control system itself records previous versions.)
2315 See the subroutine `basic-save-buffer' for more information."
2316 (interactive "p")
2317 (let ((modp (buffer-modified-p))
2318 (large (> (buffer-size) 50000))
2319 (make-backup-files (or (and make-backup-files (not (eq args 0)))
2320 (memq args '(16 64)))))
2321 (and modp (memq args '(16 64)) (setq buffer-backed-up nil))
2322 (if (and modp large) (message "Saving file %s..." (buffer-file-name)))
2323 (basic-save-buffer)
2324 (and modp (memq args '(4 64)) (setq buffer-backed-up nil))))
2326 (defun delete-auto-save-file-if-necessary (&optional force)
2327 "Delete auto-save file for current buffer if `delete-auto-save-files' is t.
2328 Normally delete only if the file was written by this Emacs since
2329 the last real save, but optional arg FORCE non-nil means delete anyway."
2330 (and buffer-auto-save-file-name delete-auto-save-files
2331 (not (string= buffer-file-name buffer-auto-save-file-name))
2332 (or force (recent-auto-save-p))
2333 (progn
2334 (condition-case ()
2335 (delete-file buffer-auto-save-file-name)
2336 (file-error nil))
2337 (set-buffer-auto-saved))))
2339 (defvar auto-save-hook nil
2340 "Normal hook run just before auto-saving.")
2342 (defvar after-save-hook nil
2343 "Normal hook that is run after a buffer is saved to its file.")
2345 (defvar save-buffer-coding-system nil
2346 "If non-nil, use this coding system for saving the buffer.
2347 More precisely, use this coding system in place of the
2348 value of `buffer-file-coding-system', when saving the buffer.
2349 Calling `write-region' for any purpose other than saving the buffer
2350 will still use `buffer-file-coding-system'; this variable has no effect
2351 in such cases.")
2353 (make-variable-buffer-local 'save-buffer-coding-system)
2354 (put 'save-buffer-coding-system 'permanent-local t)
2356 (defun basic-save-buffer ()
2357 "Save the current buffer in its visited file, if it has been modified.
2358 The hooks `write-contents-hooks', `local-write-file-hooks' and
2359 `write-file-hooks' get a chance to do the job of saving; if they do not,
2360 then the buffer is saved in the visited file file in the usual way.
2361 After saving the buffer, this function runs `after-save-hook'."
2362 (interactive)
2363 (save-current-buffer
2364 ;; In an indirect buffer, save its base buffer instead.
2365 (if (buffer-base-buffer)
2366 (set-buffer (buffer-base-buffer)))
2367 (if (buffer-modified-p)
2368 (let ((recent-save (recent-auto-save-p))
2369 setmodes tempsetmodes)
2370 ;; On VMS, rename file and buffer to get rid of version number.
2371 (if (and (eq system-type 'vax-vms)
2372 (not (string= buffer-file-name
2373 (file-name-sans-versions buffer-file-name))))
2374 (let (buffer-new-name)
2375 ;; Strip VMS version number before save.
2376 (setq buffer-file-name
2377 (file-name-sans-versions buffer-file-name))
2378 ;; Construct a (unique) buffer name to correspond.
2379 (let ((buf (create-file-buffer (downcase buffer-file-name))))
2380 (setq buffer-new-name (buffer-name buf))
2381 (kill-buffer buf))
2382 (rename-buffer buffer-new-name)))
2383 ;; If buffer has no file name, ask user for one.
2384 (or buffer-file-name
2385 (let ((filename
2386 (expand-file-name
2387 (read-file-name "File to save in: ") nil)))
2388 (and (file-exists-p filename)
2389 (or (y-or-n-p (format "File `%s' exists; overwrite? "
2390 filename))
2391 (error "Canceled")))
2392 (set-visited-file-name filename)))
2393 (or (verify-visited-file-modtime (current-buffer))
2394 (not (file-exists-p buffer-file-name))
2395 (yes-or-no-p
2396 (format "%s has changed since visited or saved. Save anyway? "
2397 (file-name-nondirectory buffer-file-name)))
2398 (error "Save not confirmed"))
2399 (save-restriction
2400 (widen)
2401 (save-excursion
2402 (and (> (point-max) 1)
2403 (/= (char-after (1- (point-max))) ?\n)
2404 (not (and (eq selective-display t)
2405 (= (char-after (1- (point-max))) ?\r)))
2406 (or (eq require-final-newline t)
2407 (and require-final-newline
2408 (y-or-n-p
2409 (format "Buffer %s does not end in newline. Add one? "
2410 (buffer-name)))))
2411 (save-excursion
2412 (goto-char (point-max))
2413 (insert ?\n))))
2414 (or (run-hook-with-args-until-success 'write-contents-hooks)
2415 (run-hook-with-args-until-success 'local-write-file-hooks)
2416 (run-hook-with-args-until-success 'write-file-hooks)
2417 ;; If a hook returned t, file is already "written".
2418 ;; Otherwise, write it the usual way now.
2419 (setq setmodes (basic-save-buffer-1)))
2420 ;; Now we have saved the current buffer. Let's make sure
2421 ;; that buffer-file-coding-system is fixed to what
2422 ;; actually used for saving by binding it locally.
2423 (if save-buffer-coding-system
2424 (setq save-buffer-coding-system last-coding-system-used)
2425 (setq buffer-file-coding-system last-coding-system-used))
2426 (setq buffer-file-number
2427 (nthcdr 10 (file-attributes buffer-file-name)))
2428 (if setmodes
2429 (condition-case ()
2430 (set-file-modes buffer-file-name setmodes)
2431 (error nil))))
2432 ;; If the auto-save file was recent before this command,
2433 ;; delete it now.
2434 (delete-auto-save-file-if-necessary recent-save)
2435 ;; Support VC `implicit' locking.
2436 (vc-after-save)
2437 (run-hooks 'after-save-hook))
2438 (message "(No changes need to be saved)"))))
2440 ;; This does the "real job" of writing a buffer into its visited file
2441 ;; and making a backup file. This is what is normally done
2442 ;; but inhibited if one of write-file-hooks returns non-nil.
2443 ;; It returns a value to store in setmodes.
2444 (defun basic-save-buffer-1 ()
2445 (if save-buffer-coding-system
2446 (let ((coding-system-for-write save-buffer-coding-system))
2447 (basic-save-buffer-2))
2448 (basic-save-buffer-2)))
2450 (defun basic-save-buffer-2 ()
2451 (let (tempsetmodes setmodes)
2452 (if (not (file-writable-p buffer-file-name))
2453 (let ((dir (file-name-directory buffer-file-name)))
2454 (if (not (file-directory-p dir))
2455 (if (file-exists-p dir)
2456 (error "%s is not a directory" dir)
2457 (error "%s: no such directory" buffer-file-name))
2458 (if (not (file-exists-p buffer-file-name))
2459 (error "Directory %s write-protected" dir)
2460 (if (yes-or-no-p
2461 (format "File %s is write-protected; try to save anyway? "
2462 (file-name-nondirectory
2463 buffer-file-name)))
2464 (setq tempsetmodes t)
2465 (error "Attempt to save to a file which you aren't allowed to write"))))))
2466 (or buffer-backed-up
2467 (setq setmodes (backup-buffer)))
2468 (let ((dir (file-name-directory buffer-file-name)))
2469 (if (and file-precious-flag
2470 (file-writable-p dir))
2471 ;; If file is precious, write temp name, then rename it.
2472 ;; This requires write access to the containing dir,
2473 ;; which is why we don't try it if we don't have that access.
2474 (let ((realname buffer-file-name)
2475 tempname temp nogood i succeed
2476 (old-modtime (visited-file-modtime)))
2477 (setq i 0)
2478 (setq nogood t)
2479 ;; Find the temporary name to write under.
2480 (while nogood
2481 (setq tempname (format
2482 (if (and (eq system-type 'ms-dos)
2483 (not (msdos-long-file-names)))
2484 "%s#%d.tm#" ; MSDOS limits files to 8+3
2485 "%s#tmp#%d")
2486 dir i))
2487 (setq nogood (file-exists-p tempname))
2488 (setq i (1+ i)))
2489 (unwind-protect
2490 (progn (clear-visited-file-modtime)
2491 (write-region (point-min) (point-max)
2492 tempname nil realname
2493 buffer-file-truename)
2494 (setq succeed t))
2495 ;; If writing the temp file fails,
2496 ;; delete the temp file.
2497 (or succeed
2498 (progn
2499 (delete-file tempname)
2500 (set-visited-file-modtime old-modtime))))
2501 ;; Since we have created an entirely new file
2502 ;; and renamed it, make sure it gets the
2503 ;; right permission bits set.
2504 (setq setmodes (file-modes buffer-file-name))
2505 ;; We succeeded in writing the temp file,
2506 ;; so rename it.
2507 (rename-file tempname buffer-file-name t))
2508 ;; If file not writable, see if we can make it writable
2509 ;; temporarily while we write it.
2510 ;; But no need to do so if we have just backed it up
2511 ;; (setmodes is set) because that says we're superseding.
2512 (cond ((and tempsetmodes (not setmodes))
2513 ;; Change the mode back, after writing.
2514 (setq setmodes (file-modes buffer-file-name))
2515 (set-file-modes buffer-file-name 511)))
2516 (write-region (point-min) (point-max)
2517 buffer-file-name nil t buffer-file-truename)))
2518 setmodes))
2520 (defun save-some-buffers (&optional arg pred)
2521 "Save some modified file-visiting buffers. Asks user about each one.
2522 Optional argument (the prefix) non-nil means save all with no questions.
2523 Optional second argument PRED determines which buffers are considered:
2524 If PRED is nil, all the file-visiting buffers are considered.
2525 If PRED is t, then certain non-file buffers will also be considered.
2526 If PRED is a zero-argument function, it indicates for each buffer whether
2527 to consider it or not."
2528 (interactive "P")
2529 (save-window-excursion
2530 (let* ((queried nil)
2531 (files-done
2532 (map-y-or-n-p
2533 (function
2534 (lambda (buffer)
2535 (and (buffer-modified-p buffer)
2536 (not (buffer-base-buffer buffer))
2538 (buffer-file-name buffer)
2539 (and pred
2540 (progn
2541 (set-buffer buffer)
2542 (and buffer-offer-save (> (buffer-size) 0)))))
2543 (or (not (functionp pred))
2544 (with-current-buffer buffer (funcall pred)))
2545 (if arg
2547 (setq queried t)
2548 (if (buffer-file-name buffer)
2549 (format "Save file %s? "
2550 (buffer-file-name buffer))
2551 (format "Save buffer %s? "
2552 (buffer-name buffer)))))))
2553 (function
2554 (lambda (buffer)
2555 (set-buffer buffer)
2556 (save-buffer)))
2557 (buffer-list)
2558 '("buffer" "buffers" "save")
2559 (list (list ?\C-r (lambda (buf)
2560 (view-buffer buf
2561 (function
2562 (lambda (ignore)
2563 (exit-recursive-edit))))
2564 (recursive-edit)
2565 ;; Return nil to ask about BUF again.
2566 nil)
2567 "display the current buffer"))))
2568 (abbrevs-done
2569 (and save-abbrevs abbrevs-changed
2570 (progn
2571 (if (or arg
2572 (y-or-n-p (format "Save abbrevs in %s? "
2573 abbrev-file-name)))
2574 (write-abbrev-file nil))
2575 ;; Don't keep bothering user if he says no.
2576 (setq abbrevs-changed nil)
2577 t))))
2578 (or queried (> files-done 0) abbrevs-done
2579 (message "(No files need saving)")))))
2581 (defun not-modified (&optional arg)
2582 "Mark current buffer as unmodified, not needing to be saved.
2583 With prefix arg, mark buffer as modified, so \\[save-buffer] will save.
2585 It is not a good idea to use this function in Lisp programs, because it
2586 prints a message in the minibuffer. Instead, use `set-buffer-modified-p'."
2587 (interactive "P")
2588 (message (if arg "Modification-flag set"
2589 "Modification-flag cleared"))
2590 (set-buffer-modified-p arg))
2592 (defun toggle-read-only (&optional arg)
2593 "Change whether this buffer is visiting its file read-only.
2594 With arg, set read-only iff arg is positive.
2595 If visiting file read-only and `view-read-only' is non-nil, enter view mode."
2596 (interactive "P")
2597 (cond
2598 ((and arg (if (> (prefix-numeric-value arg) 0) buffer-read-only
2599 (not buffer-read-only))) ; If buffer-read-only is set correctly,
2600 nil) ; do nothing.
2601 ;; Toggle.
2602 ((and buffer-read-only view-mode)
2603 (View-exit-and-edit)
2604 (make-local-variable 'view-read-only)
2605 (setq view-read-only t)) ; Must leave view mode.
2606 ((and (not buffer-read-only) view-read-only
2607 (not (eq (get major-mode 'mode-class) 'special)))
2608 (view-mode-enter))
2609 (t (setq buffer-read-only (not buffer-read-only))
2610 (force-mode-line-update))))
2612 (defun insert-file (filename)
2613 "Insert contents of file FILENAME into buffer after point.
2614 Set mark after the inserted text.
2616 This function is meant for the user to run interactively.
2617 Don't call it from programs! Use `insert-file-contents' instead.
2618 \(Its calling sequence is different; see its documentation)."
2619 (interactive "*fInsert file: ")
2620 (if (file-directory-p filename)
2621 (signal 'file-error (list "Opening input file" "file is a directory"
2622 filename)))
2623 (let ((tem (insert-file-contents filename)))
2624 (push-mark (+ (point) (car (cdr tem))))))
2626 (defun append-to-file (start end filename)
2627 "Append the contents of the region to the end of file FILENAME.
2628 When called from a function, expects three arguments,
2629 START, END and FILENAME. START and END are buffer positions
2630 saying what text to write."
2631 (interactive "r\nFAppend to file: ")
2632 (write-region start end filename t))
2634 (defun file-newest-backup (filename)
2635 "Return most recent backup file for FILENAME or nil if no backups exist."
2636 (let* ((filename (expand-file-name filename))
2637 (file (file-name-nondirectory filename))
2638 (dir (file-name-directory filename))
2639 (comp (file-name-all-completions file dir))
2640 (newest nil)
2641 tem)
2642 (while comp
2643 (setq tem (car comp)
2644 comp (cdr comp))
2645 (cond ((and (backup-file-name-p tem)
2646 (string= (file-name-sans-versions tem) file))
2647 (setq tem (concat dir tem))
2648 (if (or (null newest)
2649 (file-newer-than-file-p tem newest))
2650 (setq newest tem)))))
2651 newest))
2653 (defun rename-uniquely ()
2654 "Rename current buffer to a similar name not already taken.
2655 This function is useful for creating multiple shell process buffers
2656 or multiple mail buffers, etc."
2657 (interactive)
2658 (save-match-data
2659 (let ((base-name (buffer-name)))
2660 (and (string-match "<[0-9]+>\\'" base-name)
2661 (not (and buffer-file-name
2662 (string= base-name
2663 (file-name-nondirectory buffer-file-name))))
2664 ;; If the existing buffer name has a <NNN>,
2665 ;; which isn't part of the file name (if any),
2666 ;; then get rid of that.
2667 (setq base-name (substring base-name 0 (match-beginning 0))))
2668 (rename-buffer (generate-new-buffer-name base-name))
2669 (force-mode-line-update))))
2671 (defun make-directory (dir &optional parents)
2672 "Create the directory DIR and any nonexistent parent dirs.
2673 Interactively, the default choice of directory to create
2674 is the current default directory for file names.
2675 That is useful when you have visited a file in a nonexistent directory.
2677 Noninteractively, the second (optional) argument PARENTS says whether
2678 to create parent directories if they don't exist."
2679 (interactive
2680 (list (read-file-name "Make directory: " default-directory default-directory
2681 nil nil)
2683 (let ((handler (find-file-name-handler dir 'make-directory)))
2684 (if handler
2685 (funcall handler 'make-directory dir parents)
2686 (if (not parents)
2687 (make-directory-internal dir)
2688 (let ((dir (directory-file-name (expand-file-name dir)))
2689 create-list)
2690 (while (not (file-exists-p dir))
2691 (setq create-list (cons dir create-list)
2692 dir (directory-file-name (file-name-directory dir))))
2693 (while create-list
2694 (make-directory-internal (car create-list))
2695 (setq create-list (cdr create-list))))))))
2697 (put 'revert-buffer-function 'permanent-local t)
2698 (defvar revert-buffer-function nil
2699 "Function to use to revert this buffer, or nil to do the default.
2700 The function receives two arguments IGNORE-AUTO and NOCONFIRM,
2701 which are the arguments that `revert-buffer' received.")
2703 (put 'revert-buffer-insert-file-contents-function 'permanent-local t)
2704 (defvar revert-buffer-insert-file-contents-function nil
2705 "Function to use to insert contents when reverting this buffer.
2706 Gets two args, first the nominal file name to use,
2707 and second, t if reading the auto-save file.
2709 The function you specify is responsible for updating (or preserving) point.")
2711 (defvar before-revert-hook nil
2712 "Normal hook for `revert-buffer' to run before reverting.
2713 If `revert-buffer-function' is used to override the normal revert
2714 mechanism, this hook is not used.")
2716 (defvar after-revert-hook nil
2717 "Normal hook for `revert-buffer' to run after reverting.
2718 Note that the hook value that it runs is the value that was in effect
2719 before reverting; that makes a difference if you have buffer-local
2720 hook functions.
2722 If `revert-buffer-function' is used to override the normal revert
2723 mechanism, this hook is not used.")
2725 (defvar revert-buffer-internal-hook)
2727 (defun revert-buffer (&optional ignore-auto noconfirm preserve-modes)
2728 "Replace current buffer text with the text of the visited file on disk.
2729 This undoes all changes since the file was visited or saved.
2730 With a prefix argument, offer to revert from latest auto-save file, if
2731 that is more recent than the visited file.
2733 This command also works for special buffers that contain text which
2734 doesn't come from a file, but reflects some other data base instead:
2735 for example, Dired buffers and buffer-list buffers. In these cases,
2736 it reconstructs the buffer contents from the appropriate data base.
2738 When called from Lisp, the first argument is IGNORE-AUTO; only offer
2739 to revert from the auto-save file when this is nil. Note that the
2740 sense of this argument is the reverse of the prefix argument, for the
2741 sake of backward compatibility. IGNORE-AUTO is optional, defaulting
2742 to nil.
2744 Optional second argument NOCONFIRM means don't ask for confirmation at
2745 all. (The local variable `revert-without-query', if non-nil, prevents
2746 confirmation.)
2748 Optional third argument PRESERVE-MODES non-nil means don't alter
2749 the files modes. Normally we reinitialize them using `normal-mode'.
2751 If the value of `revert-buffer-function' is non-nil, it is called to
2752 do all the work for this command. Otherwise, the hooks
2753 `before-revert-hook' and `after-revert-hook' are run at the beginning
2754 and the end, and if `revert-buffer-insert-file-contents-function' is
2755 non-nil, it is called instead of rereading visited file contents."
2757 ;; I admit it's odd to reverse the sense of the prefix argument, but
2758 ;; there is a lot of code out there which assumes that the first
2759 ;; argument should be t to avoid consulting the auto-save file, and
2760 ;; there's no straightforward way to encourage authors to notice a
2761 ;; reversal of the argument sense. So I'm just changing the user
2762 ;; interface, but leaving the programmatic interface the same.
2763 (interactive (list (not current-prefix-arg)))
2764 (if revert-buffer-function
2765 (funcall revert-buffer-function ignore-auto noconfirm)
2766 (let* ((auto-save-p (and (not ignore-auto)
2767 (recent-auto-save-p)
2768 buffer-auto-save-file-name
2769 (file-readable-p buffer-auto-save-file-name)
2770 (y-or-n-p
2771 "Buffer has been auto-saved recently. Revert from auto-save file? ")))
2772 (file-name (if auto-save-p
2773 buffer-auto-save-file-name
2774 buffer-file-name)))
2775 (cond ((null file-name)
2776 (error "Buffer does not seem to be associated with any file"))
2777 ((or noconfirm
2778 (and (not (buffer-modified-p))
2779 (let ((tail revert-without-query)
2780 (found nil))
2781 (while tail
2782 (if (string-match (car tail) file-name)
2783 (setq found t))
2784 (setq tail (cdr tail)))
2785 found))
2786 (yes-or-no-p (format "Revert buffer from file %s? "
2787 file-name)))
2788 (run-hooks 'before-revert-hook)
2789 ;; If file was backed up but has changed since,
2790 ;; we shd make another backup.
2791 (and (not auto-save-p)
2792 (not (verify-visited-file-modtime (current-buffer)))
2793 (setq buffer-backed-up nil))
2794 ;; Get rid of all undo records for this buffer.
2795 (or (eq buffer-undo-list t)
2796 (setq buffer-undo-list nil))
2797 ;; Effectively copy the after-revert-hook status,
2798 ;; since after-find-file will clobber it.
2799 (let ((global-hook (default-value 'after-revert-hook))
2800 (local-hook-p (local-variable-p 'after-revert-hook))
2801 (local-hook (and (local-variable-p 'after-revert-hook)
2802 after-revert-hook)))
2803 (let (buffer-read-only
2804 ;; Don't make undo records for the reversion.
2805 (buffer-undo-list t))
2806 (if revert-buffer-insert-file-contents-function
2807 (funcall revert-buffer-insert-file-contents-function
2808 file-name auto-save-p)
2809 (if (not (file-exists-p file-name))
2810 (error "File %s no longer exists!" file-name))
2811 ;; Bind buffer-file-name to nil
2812 ;; so that we don't try to lock the file.
2813 (let ((buffer-file-name nil))
2814 (or auto-save-p
2815 (unlock-buffer)))
2816 (widen)
2817 (let ((coding-system-for-read
2818 ;; Auto-saved file shoule be read without
2819 ;; any code conversion.
2820 (if auto-save-p 'no-conversion
2821 coding-system-for-read)))
2822 ;; Note that this preserves point in an intelligent way.
2823 (insert-file-contents file-name (not auto-save-p)
2824 nil nil t))))
2825 ;; Recompute the truename in case changes in symlinks
2826 ;; have changed the truename.
2827 (setq buffer-file-truename
2828 (abbreviate-file-name (file-truename buffer-file-name)))
2829 (after-find-file nil nil t t preserve-modes)
2830 ;; Run after-revert-hook as it was before we reverted.
2831 (setq-default revert-buffer-internal-hook global-hook)
2832 (if local-hook-p
2833 (progn
2834 (make-local-variable 'revert-buffer-internal-hook)
2835 (setq revert-buffer-internal-hook local-hook))
2836 (kill-local-variable 'revert-buffer-internal-hook))
2837 (run-hooks 'revert-buffer-internal-hook))
2838 t)))))
2840 (defun recover-file (file)
2841 "Visit file FILE, but get contents from its last auto-save file."
2842 ;; Actually putting the file name in the minibuffer should be used
2843 ;; only rarely.
2844 ;; Not just because users often use the default.
2845 (interactive "FRecover file: ")
2846 (setq file (expand-file-name file))
2847 (if (auto-save-file-name-p (file-name-nondirectory file))
2848 (error "%s is an auto-save file" file))
2849 (let ((file-name (let ((buffer-file-name file))
2850 (make-auto-save-file-name))))
2851 (cond ((if (file-exists-p file)
2852 (not (file-newer-than-file-p file-name file))
2853 (not (file-exists-p file-name)))
2854 (error "Auto-save file %s not current" file-name))
2855 ((save-window-excursion
2856 (if (not (memq system-type '(vax-vms windows-nt)))
2857 (with-output-to-temp-buffer "*Directory*"
2858 (buffer-disable-undo standard-output)
2859 (call-process "ls" nil standard-output nil
2860 (if (file-symlink-p file) "-lL" "-l")
2861 file file-name)))
2862 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
2863 (switch-to-buffer (find-file-noselect file t))
2864 (let ((buffer-read-only nil)
2865 ;; Keep the current buffer-file-coding-system.
2866 (coding-system buffer-file-coding-system)
2867 ;; Auto-saved file shoule be read without any code conversion.
2868 (coding-system-for-read 'no-conversion))
2869 (erase-buffer)
2870 (insert-file-contents file-name nil)
2871 (set-buffer-file-coding-system coding-system))
2872 (after-find-file nil nil t))
2873 (t (error "Recover-file cancelled")))))
2875 (defun recover-session ()
2876 "Recover auto save files from a previous Emacs session.
2877 This command first displays a Dired buffer showing you the
2878 previous sessions that you could recover from.
2879 To choose one, move point to the proper line and then type C-c C-c.
2880 Then you'll be asked about a number of files to recover."
2881 (interactive)
2882 (if (null auto-save-list-file-prefix)
2883 (error "You set `auto-save-list-file-prefix' to disable making session files"))
2884 (let ((ls-lisp-support-shell-wildcards t))
2885 (dired (concat auto-save-list-file-prefix "*")
2886 (concat dired-listing-switches "t")))
2887 (save-excursion
2888 (goto-char (point-min))
2889 (or (looking-at " Move to the session you want to recover,")
2890 (let ((inhibit-read-only t))
2891 ;; Each line starts with a space
2892 ;; so that Font Lock mode won't highlight the first character.
2893 (insert " Move to the session you want to recover,\n"
2894 " then type C-c C-c to select it.\n\n"
2895 " You can also delete some of these files;\n"
2896 " type d on a line to mark that file for deletion.\n\n"))))
2897 (use-local-map (nconc (make-sparse-keymap) (current-local-map)))
2898 (define-key (current-local-map) "\C-c\C-c" 'recover-session-finish))
2900 (defun recover-session-finish ()
2901 "Choose one saved session to recover auto-save files from.
2902 This command is used in the special Dired buffer created by
2903 \\[recover-session]."
2904 (interactive)
2905 ;; Get the name of the session file to recover from.
2906 (let ((file (dired-get-filename))
2907 files
2908 (buffer (get-buffer-create " *recover*")))
2909 (dired-unmark 1)
2910 (dired-do-flagged-delete t)
2911 (unwind-protect
2912 (save-excursion
2913 ;; Read in the auto-save-list file.
2914 (set-buffer buffer)
2915 (erase-buffer)
2916 (insert-file-contents file)
2917 ;; Loop thru the text of that file
2918 ;; and get out the names of the files to recover.
2919 (while (not (eobp))
2920 (let (thisfile autofile)
2921 (if (eolp)
2922 ;; This is a pair of lines for a non-file-visiting buffer.
2923 ;; Get the auto-save file name and manufacture
2924 ;; a "visited file name" from that.
2925 (progn
2926 (forward-line 1)
2927 (setq autofile
2928 (buffer-substring-no-properties
2929 (point)
2930 (save-excursion
2931 (end-of-line)
2932 (point))))
2933 (setq thisfile
2934 (expand-file-name
2935 (substring
2936 (file-name-nondirectory autofile)
2937 1 -1)
2938 (file-name-directory autofile)))
2939 (forward-line 1))
2940 ;; This pair of lines is a file-visiting
2941 ;; buffer. Use the visited file name.
2942 (progn
2943 (setq thisfile
2944 (buffer-substring-no-properties
2945 (point) (progn (end-of-line) (point))))
2946 (forward-line 1)
2947 (setq autofile
2948 (buffer-substring-no-properties
2949 (point) (progn (end-of-line) (point))))
2950 (forward-line 1)))
2951 ;; Ignore a file if its auto-save file does not exist now.
2952 (if (file-exists-p autofile)
2953 (setq files (cons thisfile files)))))
2954 (setq files (nreverse files))
2955 ;; The file contains a pair of line for each auto-saved buffer.
2956 ;; The first line of the pair contains the visited file name
2957 ;; or is empty if the buffer was not visiting a file.
2958 ;; The second line is the auto-save file name.
2959 (if files
2960 (map-y-or-n-p "Recover %s? "
2961 (lambda (file)
2962 (condition-case nil
2963 (save-excursion (recover-file file))
2964 (error
2965 "Failed to recover `%s'" file)))
2966 files
2967 '("file" "files" "recover"))
2968 (message "No files can be recovered from this session now")))
2969 (kill-buffer buffer))))
2971 (defun kill-some-buffers (&optional list)
2972 "For each buffer in LIST, ask whether to kill it.
2973 LIST defaults to all existing live buffers."
2974 (interactive)
2975 (if (null list)
2976 (setq list (buffer-list)))
2977 (while list
2978 (let* ((buffer (car list))
2979 (name (buffer-name buffer)))
2980 (and (not (string-equal name ""))
2981 (/= (aref name 0) ? )
2982 (yes-or-no-p
2983 (format "Buffer %s %s. Kill? "
2984 name
2985 (if (buffer-modified-p buffer)
2986 "HAS BEEN EDITED" "is unmodified")))
2987 (kill-buffer buffer)))
2988 (setq list (cdr list))))
2990 (defun auto-save-mode (arg)
2991 "Toggle auto-saving of contents of current buffer.
2992 With prefix argument ARG, turn auto-saving on if positive, else off."
2993 (interactive "P")
2994 (setq buffer-auto-save-file-name
2995 (and (if (null arg)
2996 (or (not buffer-auto-save-file-name)
2997 ;; If auto-save is off because buffer has shrunk,
2998 ;; then toggling should turn it on.
2999 (< buffer-saved-size 0))
3000 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))
3001 (if (and buffer-file-name auto-save-visited-file-name
3002 (not buffer-read-only))
3003 buffer-file-name
3004 (make-auto-save-file-name))))
3005 ;; If -1 was stored here, to temporarily turn off saving,
3006 ;; turn it back on.
3007 (and (< buffer-saved-size 0)
3008 (setq buffer-saved-size 0))
3009 (if (interactive-p)
3010 (message "Auto-save %s (in this buffer)"
3011 (if buffer-auto-save-file-name "on" "off")))
3012 buffer-auto-save-file-name)
3014 (defun rename-auto-save-file ()
3015 "Adjust current buffer's auto save file name for current conditions.
3016 Also rename any existing auto save file, if it was made in this session."
3017 (let ((osave buffer-auto-save-file-name))
3018 (setq buffer-auto-save-file-name
3019 (make-auto-save-file-name))
3020 (if (and osave buffer-auto-save-file-name
3021 (not (string= buffer-auto-save-file-name buffer-file-name))
3022 (not (string= buffer-auto-save-file-name osave))
3023 (file-exists-p osave)
3024 (recent-auto-save-p))
3025 (rename-file osave buffer-auto-save-file-name t))))
3027 (defun make-auto-save-file-name ()
3028 "Return file name to use for auto-saves of current buffer.
3029 Does not consider `auto-save-visited-file-name' as that variable is checked
3030 before calling this function. You can redefine this for customization.
3031 See also `auto-save-file-name-p'."
3032 (if buffer-file-name
3033 (if (and (eq system-type 'ms-dos)
3034 (not (msdos-long-file-names)))
3035 (let ((fn (file-name-nondirectory buffer-file-name)))
3036 (string-match "\\`\\([^.]+\\)\\(\\.\\(..?\\)?.?\\|\\)\\'" fn)
3037 (concat (file-name-directory buffer-file-name)
3038 "#" (match-string 1 fn)
3039 "." (match-string 3 fn) "#"))
3040 (concat (file-name-directory buffer-file-name)
3042 (file-name-nondirectory buffer-file-name)
3043 "#"))
3045 ;; Deal with buffers that don't have any associated files. (Mail
3046 ;; mode tends to create a good number of these.)
3048 (let ((buffer-name (buffer-name))
3049 (limit 0))
3050 ;; Eliminate all slashes and backslashes by
3051 ;; replacing them with sequences that start with %.
3052 ;; Quote % also, to keep distinct names distinct.
3053 (while (string-match "[/\\%]" buffer-name limit)
3054 (let* ((character (aref buffer-name (match-beginning 0)))
3055 (replacement
3056 (cond ((eq character ?%) "%%")
3057 ((eq character ?/) "%+")
3058 ((eq character ?\\) "%-"))))
3059 (setq buffer-name (replace-match replacement t t buffer-name))
3060 (setq limit (1+ (match-end 0)))))
3061 ;; Generate the file name.
3062 (expand-file-name
3063 (format "#%s#%s#" buffer-name (make-temp-name ""))
3064 ;; Try a few alternative directories, to get one we can write it.
3065 (cond
3066 ((file-writable-p default-directory) default-directory)
3067 ((file-writable-p "/var/tmp/") "/var/tmp/")
3068 ("~/"))))))
3070 (defun auto-save-file-name-p (filename)
3071 "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'.
3072 FILENAME should lack slashes. You can redefine this for customization."
3073 (string-match "^#.*#$" filename))
3075 (defun wildcard-to-regexp (wildcard)
3076 "Given a shell file name pattern WILDCARD, return an equivalent regexp.
3077 The generated regexp will match a filename iff the filename
3078 matches that wildcard according to shell rules. Only wildcards known
3079 by `sh' are supported."
3080 (let* ((i (string-match "[[.*+\\^$?]" wildcard))
3081 ;; Copy the initial run of non-special characters.
3082 (result (substring wildcard 0 i))
3083 (len (length wildcard)))
3084 ;; If no special characters, we're almost done.
3085 (if i
3086 (while (< i len)
3087 (let ((ch (aref wildcard i))
3089 (setq
3090 result
3091 (concat result
3092 (cond
3093 ((and (eq ch ?\[)
3094 (< (1+ i) len)
3095 (eq (aref wildcard (1+ i)) ?\]))
3096 "\\[")
3097 ((eq ch ?\[) ; [...] maps to regexp char class
3098 (progn
3099 (setq i (1+ i))
3100 (concat
3101 (cond
3102 ((eq (aref wildcard i) ?!) ; [!...] -> [^...]
3103 (progn
3104 (setq i (1+ i))
3105 (if (eq (aref wildcard i) ?\])
3106 (progn
3107 (setq i (1+ i))
3108 "[^]")
3109 "[^")))
3110 ((eq (aref wildcard i) ?^)
3111 ;; Found "[^". Insert a `\0' character
3112 ;; (which cannot happen in a filename)
3113 ;; into the character class, so that `^'
3114 ;; is not the first character after `[',
3115 ;; and thus non-special in a regexp.
3116 (progn
3117 (setq i (1+ i))
3118 "[\000^"))
3119 ((eq (aref wildcard i) ?\])
3120 ;; I don't think `]' can appear in a
3121 ;; character class in a wildcard, but
3122 ;; let's be general here.
3123 (progn
3124 (setq i (1+ i))
3125 "[]"))
3126 (t "["))
3127 (prog1 ; copy everything upto next `]'.
3128 (substring wildcard
3130 (setq j (string-match
3131 "]" wildcard i)))
3132 (setq i (if j (1- j) (1- len)))))))
3133 ((eq ch ?.) "\\.")
3134 ((eq ch ?*) "[^\000]*")
3135 ((eq ch ?+) "\\+")
3136 ((eq ch ?^) "\\^")
3137 ((eq ch ?$) "\\$")
3138 ((eq ch ?\\) "\\\\") ; probably cannot happen...
3139 ((eq ch ??) "[^\000]")
3140 (t (char-to-string ch)))))
3141 (setq i (1+ i)))))
3142 ;; Shell wildcards should match the entire filename,
3143 ;; not its part. Make the regexp say so.
3144 (concat "\\`" result "\\'")))
3146 (defcustom list-directory-brief-switches
3147 (if (eq system-type 'vax-vms) "" "-CF")
3148 "*Switches for list-directory to pass to `ls' for brief listing,"
3149 :type 'string
3150 :group 'dired)
3152 (defcustom list-directory-verbose-switches
3153 (if (eq system-type 'vax-vms)
3154 "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)"
3155 "-l")
3156 "*Switches for list-directory to pass to `ls' for verbose listing,"
3157 :type 'string
3158 :group 'dired)
3160 (defun file-expand-wildcards (pattern &optional full)
3161 "Expand wildcard pattern PATTERN.
3162 This returns a list of file names which match the pattern.
3164 If PATTERN is written as an absolute relative file name,
3165 the values are absolute also.
3167 If PATTERN is written as a relative file name, it is interpreted
3168 relative to the current default directory, `default-directory'.
3169 The file names returned are normally also relative to the current
3170 default directory. However, if FULL is non-nil, they are absolute."
3171 (let* ((nondir (file-name-nondirectory pattern))
3172 (dirpart (file-name-directory pattern))
3173 ;; A list of all dirs that DIRPART specifies.
3174 ;; This can be more than one dir
3175 ;; if DIRPART contains wildcards.
3176 (dirs (if (and dirpart (string-match "[[*?]" dirpart))
3177 (mapcar 'file-name-as-directory
3178 (file-expand-wildcards (directory-file-name dirpart)))
3179 (list dirpart)))
3180 contents)
3181 (while dirs
3182 (when (or (null (car dirs)) ; Possible if DIRPART is not wild.
3183 (file-directory-p (directory-file-name (car dirs))))
3184 (let ((this-dir-contents
3185 ;; Filter out "." and ".."
3186 (delq nil
3187 (mapcar #'(lambda (name)
3188 (unless (string-match "\\`\\.\\.?\\'"
3189 (file-name-nondirectory name))
3190 name))
3191 (directory-files (or (car dirs) ".") full
3192 (wildcard-to-regexp nondir))))))
3193 (setq contents
3194 (nconc
3195 (if (and (car dirs) (not full))
3196 (mapcar (function (lambda (name) (concat (car dirs) name)))
3197 this-dir-contents)
3198 this-dir-contents)
3199 contents))))
3200 (setq dirs (cdr dirs)))
3201 contents))
3203 (defun list-directory (dirname &optional verbose)
3204 "Display a list of files in or matching DIRNAME, a la `ls'.
3205 DIRNAME is globbed by the shell if necessary.
3206 Prefix arg (second arg if noninteractive) means supply -l switch to `ls'.
3207 Actions controlled by variables `list-directory-brief-switches'
3208 and `list-directory-verbose-switches'."
3209 (interactive (let ((pfx current-prefix-arg))
3210 (list (read-file-name (if pfx "List directory (verbose): "
3211 "List directory (brief): ")
3212 nil default-directory nil)
3213 pfx)))
3214 (let ((switches (if verbose list-directory-verbose-switches
3215 list-directory-brief-switches)))
3216 (or dirname (setq dirname default-directory))
3217 (setq dirname (expand-file-name dirname))
3218 (with-output-to-temp-buffer "*Directory*"
3219 (buffer-disable-undo standard-output)
3220 (princ "Directory ")
3221 (princ dirname)
3222 (terpri)
3223 (save-excursion
3224 (set-buffer "*Directory*")
3225 (setq default-directory
3226 (if (file-directory-p dirname)
3227 (file-name-as-directory dirname)
3228 (file-name-directory dirname)))
3229 (let ((wildcard (not (file-directory-p dirname))))
3230 (insert-directory dirname switches wildcard (not wildcard)))))))
3232 (defvar insert-directory-program "ls"
3233 "Absolute or relative name of the `ls' program used by `insert-directory'.")
3235 ;; insert-directory
3236 ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and
3237 ;; FULL-DIRECTORY-P is nil.
3238 ;; The single line of output must display FILE's name as it was
3239 ;; given, namely, an absolute path name.
3240 ;; - must insert exactly one line for each file if WILDCARD or
3241 ;; FULL-DIRECTORY-P is t, plus one optional "total" line
3242 ;; before the file lines, plus optional text after the file lines.
3243 ;; Lines are delimited by "\n", so filenames containing "\n" are not
3244 ;; allowed.
3245 ;; File lines should display the basename.
3246 ;; - must be consistent with
3247 ;; - functions dired-move-to-filename, (these two define what a file line is)
3248 ;; dired-move-to-end-of-filename,
3249 ;; dired-between-files, (shortcut for (not (dired-move-to-filename)))
3250 ;; dired-insert-headerline
3251 ;; dired-after-subdir-garbage (defines what a "total" line is)
3252 ;; - variable dired-subdir-regexp
3253 (defun insert-directory (file switches &optional wildcard full-directory-p)
3254 "Insert directory listing for FILE, formatted according to SWITCHES.
3255 Leaves point after the inserted text.
3256 SWITCHES may be a string of options, or a list of strings.
3257 Optional third arg WILDCARD means treat FILE as shell wildcard.
3258 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
3259 switches do not contain `d', so that a full listing is expected.
3261 This works by running a directory listing program
3262 whose name is in the variable `insert-directory-program'.
3263 If WILDCARD, it also runs the shell specified by `shell-file-name'."
3264 ;; We need the directory in order to find the right handler.
3265 (let ((handler (find-file-name-handler (expand-file-name file)
3266 'insert-directory)))
3267 (if handler
3268 (funcall handler 'insert-directory file switches
3269 wildcard full-directory-p)
3270 (if (eq system-type 'vax-vms)
3271 (vms-read-directory file switches (current-buffer))
3272 (let* ((coding-system-for-read
3273 (and enable-multibyte-characters
3274 (or file-name-coding-system
3275 default-file-name-coding-system)))
3276 ;; This is to control encoding the arguments in call-process.
3277 (coding-system-for-write coding-system-for-read)
3278 (result
3279 (if wildcard
3280 ;; Run ls in the directory of the file pattern we asked for.
3281 (let ((default-directory
3282 (if (file-name-absolute-p file)
3283 (file-name-directory file)
3284 (file-name-directory (expand-file-name file))))
3285 (pattern (file-name-nondirectory file))
3286 (beg 0))
3287 ;; Quote some characters that have special meanings in shells;
3288 ;; but don't quote the wildcards--we want them to be special.
3289 ;; We also currently don't quote the quoting characters
3290 ;; in case people want to use them explicitly to quote
3291 ;; wildcard characters.
3292 (while (string-match "[ \t\n;<>&|()#$]" pattern beg)
3293 (setq pattern
3294 (concat (substring pattern 0 (match-beginning 0))
3295 "\\"
3296 (substring pattern (match-beginning 0)))
3297 beg (1+ (match-end 0))))
3298 (call-process shell-file-name nil t nil
3299 "-c" (concat "\\";; Disregard shell aliases!
3300 insert-directory-program
3301 " -d "
3302 (if (stringp switches)
3303 switches
3304 (mapconcat 'identity switches " "))
3305 " -- "
3306 pattern)))
3307 ;; SunOS 4.1.3, SVr4 and others need the "." to list the
3308 ;; directory if FILE is a symbolic link.
3309 (apply 'call-process
3310 insert-directory-program nil t nil
3311 (let (list)
3312 (if (listp switches)
3313 (setq list switches)
3314 (if (not (equal switches ""))
3315 (progn
3316 ;; Split the switches at any spaces
3317 ;; so we can pass separate options as separate args.
3318 (while (string-match " " switches)
3319 (setq list (cons (substring switches 0 (match-beginning 0))
3320 list)
3321 switches (substring switches (match-end 0))))
3322 (setq list (nreverse (cons switches list))))))
3323 (append list
3324 ;; Avoid lossage if FILE starts with `-'.
3325 '("--")
3326 (progn
3327 (if (string-match "\\`~" file)
3328 (setq file (expand-file-name file)))
3329 (list
3330 (if full-directory-p
3331 (concat (file-name-as-directory file) ".")
3332 file)))))))))
3333 (if (/= result 0)
3334 ;; We get here if ls failed.
3335 ;; Access the file to get a suitable error.
3336 (access-file file "Reading directory")))))))
3338 (defvar kill-emacs-query-functions nil
3339 "Functions to call with no arguments to query about killing Emacs.
3340 If any of these functions returns nil, killing Emacs is cancelled.
3341 `save-buffers-kill-emacs' (\\[save-buffers-kill-emacs]) calls these functions,
3342 but `kill-emacs', the low level primitive, does not.
3343 See also `kill-emacs-hook'.")
3345 (defun save-buffers-kill-emacs (&optional arg)
3346 "Offer to save each buffer, then kill this Emacs process.
3347 With prefix arg, silently save all file-visiting buffers, then kill."
3348 (interactive "P")
3349 (save-some-buffers arg t)
3350 (and (or (not (memq t (mapcar (function
3351 (lambda (buf) (and (buffer-file-name buf)
3352 (buffer-modified-p buf))))
3353 (buffer-list))))
3354 (yes-or-no-p "Modified buffers exist; exit anyway? "))
3355 (or (not (fboundp 'process-list))
3356 ;; process-list is not defined on VMS.
3357 (let ((processes (process-list))
3358 active)
3359 (while processes
3360 (and (memq (process-status (car processes)) '(run stop open))
3361 (let ((val (process-kill-without-query (car processes))))
3362 (process-kill-without-query (car processes) val)
3363 val)
3364 (setq active t))
3365 (setq processes (cdr processes)))
3366 (or (not active)
3367 (list-processes)
3368 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
3369 ;; Query the user for other things, perhaps.
3370 (run-hook-with-args-until-failure 'kill-emacs-query-functions)
3371 (kill-emacs)))
3373 ;; We use /: as a prefix to "quote" a file name
3374 ;; so that magic file name handlers will not apply to it.
3376 (setq file-name-handler-alist
3377 (cons '("\\`/:" . file-name-non-special)
3378 file-name-handler-alist))
3380 ;; We depend on being the last handler on the list,
3381 ;; so that anything else which does need handling
3382 ;; has been handled already.
3383 ;; So it is safe for us to inhibit *all* magic file name handlers.
3385 (defun file-name-non-special (operation &rest arguments)
3386 (let ((file-name-handler-alist nil)
3387 (default-directory
3388 (if (eq operation 'insert-directory)
3389 (directory-file-name
3390 (expand-file-name
3391 (unhandled-file-name-directory default-directory)))
3392 default-directory))
3393 ;; Get a list of the indices of the args which are file names.
3394 (file-arg-indices
3395 (cdr (or (assq operation
3396 ;; The first four are special because they
3397 ;; return a file name. We want to include the /:
3398 ;; in the return value.
3399 ;; So just avoid stripping it in the first place.
3400 '((expand-file-name . nil)
3401 ;; `identity' means just return the first arg
3402 ;; as stripped of its quoting.
3403 (substitute-in-file-name . identity)
3404 (file-name-directory . nil)
3405 (file-name-as-directory . nil)
3406 (directory-file-name . nil)
3407 (file-name-completion 0 1)
3408 (file-name-all-completions 0 1)
3409 (rename-file 0 1)
3410 (copy-file 0 1)
3411 (make-symbolic-link 0 1)
3412 (add-name-to-file 0 1)))
3413 ;; For all other operations, treat the first argument only
3414 ;; as the file name.
3415 '(nil 0))))
3416 ;; Copy ARGUMENTS so we can replace elements in it.
3417 (arguments (copy-sequence arguments)))
3418 ;; Strip off the /: from the file names that have this handler.
3419 (save-match-data
3420 (while (consp file-arg-indices)
3421 (let ((pair (nthcdr (car file-arg-indices) arguments)))
3422 (and (car pair)
3423 (string-match "\\`/:" (car pair))
3424 (setcar pair
3425 (if (= (length (car pair)) 2)
3427 (substring (car pair) 2)))))
3428 (setq file-arg-indices (cdr file-arg-indices))))
3429 (if (eq file-arg-indices 'identity)
3430 (car arguments)
3431 (apply operation arguments))))
3433 (define-key ctl-x-map "\C-f" 'find-file)
3434 (define-key ctl-x-map "\C-r" 'find-file-read-only)
3435 (define-key ctl-x-map "\C-v" 'find-alternate-file)
3436 (define-key ctl-x-map "\C-s" 'save-buffer)
3437 (define-key ctl-x-map "s" 'save-some-buffers)
3438 (define-key ctl-x-map "\C-w" 'write-file)
3439 (define-key ctl-x-map "i" 'insert-file)
3440 (define-key esc-map "~" 'not-modified)
3441 (define-key ctl-x-map "\C-d" 'list-directory)
3442 (define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs)
3444 (define-key ctl-x-4-map "f" 'find-file-other-window)
3445 (define-key ctl-x-4-map "r" 'find-file-read-only-other-window)
3446 (define-key ctl-x-4-map "\C-f" 'find-file-other-window)
3447 (define-key ctl-x-4-map "b" 'switch-to-buffer-other-window)
3448 (define-key ctl-x-4-map "\C-o" 'display-buffer)
3450 (define-key ctl-x-5-map "b" 'switch-to-buffer-other-frame)
3451 (define-key ctl-x-5-map "f" 'find-file-other-frame)
3452 (define-key ctl-x-5-map "\C-f" 'find-file-other-frame)
3453 (define-key ctl-x-5-map "r" 'find-file-read-only-other-frame)
3455 ;;; files.el ends here