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