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