1 ;;; files.el --- file input and output commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001
4 ;;; Free Software Foundation, Inc.
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)
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.
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.
34 "Backups of edited data files."
37 (defgroup find-file nil
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."
50 (defcustom directory-abbrev-alist
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"
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'."
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'."
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."
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."
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
)
136 (defun normal-backup-enable-predicate (name)
137 "Default `backup-enable-predicate' function.
138 Checks for files in `temporary-file-directory' or
139 `small-temporary-file-directory'."
140 (not (or (let ((comp (compare-strings temporary-file-directory
0 nil
142 ;; Directory is under temporary-file-directory.
143 (and (not (eq comp t
))
144 (< comp
(- (length temporary-file-directory
)))))
145 (if small-temporary-file-directory
146 (let ((comp (compare-strings small-temporary-file-directory
149 ;; Directory is under small-temporary-file-directory.
150 (and (not (eq comp t
))
151 (< comp
(- (length small-temporary-file-directory
)))))))))
153 (defvar backup-enable-predicate
'normal-backup-enable-predicate
154 "Predicate that looks at a file name and decides whether to make backups.
155 Called with an absolute file name as argument, it returns t to enable backup.")
157 (defcustom buffer-offer-save nil
158 "*Non-nil in a buffer means always offer to save buffer on exit.
159 Do so even if the buffer is not visiting a file.
160 Automatically local in all buffers."
163 (make-variable-buffer-local 'buffer-offer-save
)
165 (defcustom find-file-existing-other-name t
166 "*Non-nil means find a file under alternative names, in existing buffers.
167 This means if any existing buffer is visiting the file you want
168 under another name, you get the existing buffer instead of a new buffer."
172 (defcustom find-file-visit-truename nil
173 "*Non-nil means visit a file under its truename.
174 The truename of a file is found by chasing all links
175 both at the file level and at the levels of the containing directories."
179 (defcustom revert-without-query
181 "*Specify which files should be reverted without query.
182 The value is a list of regular expressions.
183 If the file name matches one of these regular expressions,
184 then `revert-buffer' reverts the file without querying
185 if the file has changed on disk and you have not edited the buffer."
186 :type
'(repeat regexp
)
189 (defvar buffer-file-number nil
190 "The device number and file number of the file visited in the current buffer.
191 The value is a list of the form (FILENUM DEVNUM).
192 This pair of numbers uniquely identifies the file.
193 If the buffer is visiting a new file, the value is nil.")
194 (make-variable-buffer-local 'buffer-file-number
)
195 (put 'buffer-file-number
'permanent-local t
)
197 (defvar buffer-file-numbers-unique
(not (memq system-type
'(windows-nt)))
198 "Non-nil means that buffer-file-number uniquely identifies files.")
200 (defvar temporary-file-directory
201 (file-name-as-directory
202 (cond ((memq system-type
'(ms-dos windows-nt
))
203 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
204 ((memq system-type
'(vax-vms axp-vms
))
205 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
207 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
208 "The directory for writing temporary files.")
210 (defvar small-temporary-file-directory
211 (if (eq system-type
'ms-dos
) (getenv "TMPDIR"))
212 "The directory for writing small temporary files.
213 If non-nil, this directory is used instead of `temporary-file-directory'
214 by programs that create small temporary files. This is for systems that
215 have fast storage with limited space, such as a RAM disk.")
217 ;; The system null device. (Should reference NULL_DEVICE from C.)
218 (defvar null-device
"/dev/null" "The system null device.")
220 (defvar file-name-invalid-regexp
221 (cond ((and (eq system-type
'ms-dos
) (not (msdos-long-file-names)))
222 (concat "^\\([^A-Z[-`a-z]\\|..+\\)?:\\|" ; colon except after drive
223 "[+, ;=|<>\"?*]\\|\\[\\|\\]\\|" ; invalid characters
224 "[\000-\031]\\|" ; control characters
225 "\\(/\\.\\.?[^/]\\)\\|" ; leading dots
226 "\\(/[^/.]+\\.[^/.]*\\.\\)")) ; more than a single dot
227 ((memq system-type
'(ms-dos windows-nt
))
228 (concat "^\\([^A-Z[-`a-z]\\|..+\\)?:\\|" ; colon except after drive
229 "[|<>\"?*\000-\031]")) ; invalid characters
231 "Regexp recognizing file names which aren't allowed by the filesystem.")
233 (defcustom file-precious-flag nil
234 "*Non-nil means protect against I/O errors while saving files.
235 Some modes set this non-nil in particular buffers.
237 This feature works by writing the new contents into a temporary file
238 and then renaming the temporary file to replace the original.
239 In this way, any I/O error in writing leaves the original untouched,
240 and there is never any instant where the file is nonexistent.
242 Note that this feature forces backups to be made by copying.
243 Yet, at the same time, saving a precious file
244 breaks any hard links between it and other files."
248 (defcustom version-control nil
249 "*Control use of version numbers for backup files.
250 t means make numeric backup versions unconditionally.
251 nil means make them for files that have some already.
252 `never' means do not make them."
253 :type
'(choice (const :tag
"Never" never
)
254 (const :tag
"If existing" nil
)
255 (other :tag
"Always" t
))
259 (defcustom dired-kept-versions
2
260 "*When cleaning directory, number of versions to keep."
265 (defcustom delete-old-versions nil
266 "*If t, delete excess backup versions silently.
267 If nil, ask confirmation. Any other value prevents any trimming."
268 :type
'(choice (const :tag
"Delete" t
)
269 (const :tag
"Ask" nil
)
270 (other :tag
"Leave" other
))
273 (defcustom kept-old-versions
2
274 "*Number of oldest versions to keep when a new numbered backup is made."
278 (defcustom kept-new-versions
2
279 "*Number of newest versions to keep when a new numbered backup is made.
280 Includes the new backup. Must be > 0"
284 (defcustom require-final-newline nil
285 "*Value of t says silently ensure a file ends in a newline when it is saved.
286 Non-nil but not t says ask user whether to add a newline when there isn't one.
287 nil means don't add newlines."
288 :type
'(choice (const :tag
"Off" nil
)
290 (other :tag
"Ask" ask
))
291 :group
'editing-basics
)
293 (defcustom auto-save-default t
294 "*Non-nil says by default do auto-saving of every file-visiting buffer."
298 (defcustom auto-save-visited-file-name nil
299 "*Non-nil says auto-save a buffer in the file it is visiting, when practical.
300 Normally auto-save files are written under other names."
304 (defcustom auto-save-file-name-transforms
305 `(("\\`/[^/]*:\\(.+/\\)*\\(.*\\)"
306 ,(expand-file-name "\\2" temporary-file-directory
)))
307 "*Transforms to apply to buffer file name before making auto-save file name.
308 Each transform is a list (REGEXP REPLACEMENT):
309 REGEXP is a regular expression to match against the file name.
310 If it matches, `replace-match' is used to replace the
311 matching part with REPLACEMENT.
312 All the transforms in the list are tried, in the order they are listed.
313 When one transform applies, its result is final;
314 no further transforms are tried.
316 The default value is set up to put the auto-save file into the
317 temporary directory (see the variable `temporary-file-directory') for
318 editing a remote file."
320 :type
'(repeat (list (string :tag
"Regexp") (string :tag
"Replacement")))
323 (defcustom save-abbrevs nil
324 "*Non-nil means save word abbrevs too when files are saved.
325 If `silently', don't ask the user before saving.
326 Loading an abbrev file sets this to t."
327 :type
'(choice (const t
) (const nil
) (const silently
))
330 (defcustom find-file-run-dired t
331 "*Non-nil means allow `find-file' to visit directories.
332 To visit the directory, `find-file' runs `find-directory-functions'."
336 (defcustom find-directory-functions
'(cvs-dired-noselect dired-noselect
)
337 "*List of functions to try in sequence to visit a directory.
338 Each function is called with the directory name as the sole argument
339 and should return either a buffer or nil."
340 :type
'(hook :options
(cvs-dired-noselect dired-noselect
))
343 ;;;It is not useful to make this a local variable.
344 ;;;(put 'find-file-not-found-hooks 'permanent-local t)
345 (defvar find-file-not-found-hooks nil
346 "List of functions to be called for `find-file' on nonexistent file.
347 These functions are called as soon as the error is detected.
348 Variable `buffer-file-name' is already set up.
349 The functions are called in the order given until one of them returns non-nil.")
351 ;;;It is not useful to make this a local variable.
352 ;;;(put 'find-file-hooks 'permanent-local t)
353 (defvar find-file-hooks nil
354 "List of functions to be called after a buffer is loaded from a file.
355 The buffer's local variables (if any) will have been processed before the
356 functions are called.")
358 (defvar write-file-hooks nil
359 "List of functions to be called before writing out a buffer to a file.
360 If one of them returns non-nil, the file is considered already written
361 and the rest are not called.
362 These hooks are considered to pertain to the visited file.
363 So any buffer-local binding of `write-file-hooks' is
364 discarded if you change the visited file name with \\[set-visited-file-name].
366 Don't make this variable buffer-local; instead, use `local-write-file-hooks'.
367 See also `write-contents-hooks'.")
368 ;;; However, in case someone does make it local...
369 (put 'write-file-hooks
'permanent-local t
)
371 (defvar local-write-file-hooks nil
372 "Just like `write-file-hooks', except intended for per-buffer use.
373 The functions in this list are called before the ones in
376 This variable is meant to be used for hooks that have to do with a
377 particular visited file. Therefore, it is a permanent local, so that
378 changing the major mode does not clear it. However, calling
379 `set-visited-file-name' does clear it.")
380 (make-variable-buffer-local 'local-write-file-hooks
)
381 (put 'local-write-file-hooks
'permanent-local t
)
383 (defvar write-contents-hooks nil
384 "List of functions to be called before writing out a buffer to a file.
385 If one of them returns non-nil, the file is considered already written
386 and the rest are not called.
388 This variable is meant to be used for hooks that pertain to the
389 buffer's contents, not to the particular visited file; thus,
390 `set-visited-file-name' does not clear this variable; but changing the
391 major mode does clear it.
393 This variable automatically becomes buffer-local whenever it is set.
394 If you use `add-hook' to add elements to the list, use nil for the
397 See also `write-file-hooks'.")
398 (make-variable-buffer-local 'write-contents-hooks
)
400 (defcustom enable-local-variables t
401 "*Control use of local variables in files you visit.
402 The value can be t, nil or something else.
403 A value of t means file local variables specifications are obeyed;
404 nil means they are ignored; anything else means query.
405 This variable also controls use of major modes specified in
408 The command \\[normal-mode], when used interactively,
409 always obeys file local variable specifications and the -*- line,
410 and ignores this variable."
411 :type
'(choice (const :tag
"Obey" t
)
412 (const :tag
"Ignore" nil
)
413 (other :tag
"Query" other
))
416 (defvar local-enable-local-variables t
417 "Like `enable-local-variables' but meant for buffer-local bindings.
418 The meaningful values are nil and non-nil. The default is non-nil.
419 If a major mode sets this to nil, buffer-locally, then any local
420 variables list in the file will be ignored.
422 This variable does not affect the use of major modes
423 specified in a -*- line.")
425 (defcustom enable-local-eval
'maybe
426 "*Control processing of the \"variable\" `eval' in a file's local variables.
427 The value can be t, nil or something else.
428 A value of t means obey `eval' variables;
429 nil means ignore them; anything else means query.
431 The command \\[normal-mode] always obeys local-variables lists
432 and ignores this variable."
433 :type
'(choice (const :tag
"Obey" t
)
434 (const :tag
"Ignore" nil
)
435 (other :tag
"Query" other
))
438 ;; Avoid losing in versions where CLASH_DETECTION is disabled.
439 (or (fboundp 'lock-buffer
)
440 (defalias 'lock-buffer
'ignore
))
441 (or (fboundp 'unlock-buffer
)
442 (defalias 'unlock-buffer
'ignore
))
443 (or (fboundp 'file-locked-p
)
444 (defalias 'file-locked-p
'ignore
))
446 (defvar view-read-only nil
447 "*Non-nil means buffers visiting files read-only, do it in view mode.")
449 (put 'ange-ftp-completion-hook-function
'safe-magic t
)
450 (defun ange-ftp-completion-hook-function (op &rest args
)
451 "Provides support for ange-ftp host name completion.
452 Runs the usual ange-ftp hook, but only for completion operations."
453 ;; Having this here avoids the need to load ange-ftp when it's not
455 (if (memq op
'(file-name-completion file-name-all-completions
))
456 (apply 'ange-ftp-hook-function op args
)
457 (let ((inhibit-file-name-handlers
458 (cons 'ange-ftp-completion-hook-function
459 (and (eq inhibit-file-name-operation op
)
460 inhibit-file-name-handlers
)))
461 (inhibit-file-name-operation op
))
464 (defun convert-standard-filename (filename)
465 "Convert a standard file's name to something suitable for the current OS.
466 This function's standard definition is trivial; it just returns the argument.
467 However, on some systems, the function is redefined with a definition
468 that really does change some file names to canonicalize certain
469 patterns and to guarantee valid names."
473 "Show the current default directory."
475 (message "Directory %s" default-directory
))
478 "Value of the CDPATH environment variable, as a list.
479 Not actually set up until the first time you use it.")
481 (defun parse-colon-path (cd-path)
482 "Explode a colon-separated search path into a list of directory names.
483 \(For values of `colon' equal to `path-separator'.)"
484 ;; We could use split-string here.
486 (let (cd-prefix cd-list
(cd-start 0) cd-colon
)
487 (setq cd-path
(concat cd-path path-separator
))
488 (while (setq cd-colon
(string-match path-separator cd-path cd-start
))
491 (list (if (= cd-start cd-colon
)
493 (substitute-in-file-name
494 (file-name-as-directory
495 (substring cd-path cd-start cd-colon
)))))))
496 (setq cd-start
(+ cd-colon
1)))
499 (defun cd-absolute (dir)
500 "Change current directory to given absolute file name DIR."
501 ;; Put the name into directory syntax now,
502 ;; because otherwise expand-file-name may give some bad results.
503 (if (not (eq system-type
'vax-vms
))
504 (setq dir
(file-name-as-directory dir
)))
505 (setq dir
(abbreviate-file-name (expand-file-name dir
)))
506 (if (not (file-directory-p dir
))
507 (if (file-exists-p dir
)
508 (error "%s is not a directory" dir
)
509 (error "%s: no such directory" dir
))
510 (if (file-executable-p dir
)
511 (setq default-directory dir
)
512 (error "Cannot cd to %s: Permission denied" dir
))))
515 "Make DIR become the current buffer's default directory.
516 If your environment includes a `CDPATH' variable, try each one of that
517 colon-separated list of directories when resolving a relative directory name."
519 (list (read-file-name "Change default directory: "
520 default-directory default-directory
521 (and (member cd-path
'(nil ("./")))
522 (null (getenv "CDPATH"))))))
523 (if (file-name-absolute-p dir
)
524 (cd-absolute (expand-file-name dir
))
526 (let ((trypath (parse-colon-path (getenv "CDPATH"))))
527 (setq cd-path
(or trypath
(list "./")))))
528 (if (not (catch 'found
530 (function (lambda (x)
531 (let ((f (expand-file-name (concat x dir
))))
532 (if (file-directory-p f
)
535 (throw 'found t
))))))
538 (error "No such directory found via CDPATH environment variable"))))
540 (defun load-file (file)
541 "Load the Lisp file named FILE."
542 ;; This is a case where .elc makes a lot of sense.
543 (interactive (list (let ((completion-ignored-extensions
544 (remove ".elc" completion-ignored-extensions
)))
545 (read-file-name "Load file: "))))
546 (load (expand-file-name file
) nil nil t
))
548 (defun load-library (library)
549 "Load the library named LIBRARY.
550 This is an interface to the function `load'."
551 (interactive "sLoad library: ")
554 (defun file-local-copy (file)
555 "Copy the file FILE into a temporary file on this machine.
556 Returns the name of the local copy, or nil, if FILE is directly
558 ;; This formerly had an optional BUFFER argument that wasn't used by
560 (let ((handler (find-file-name-handler file
'file-local-copy
)))
562 (funcall handler
'file-local-copy file
)
565 (defun file-truename (filename &optional counter prev-dirs
)
566 "Return the truename of FILENAME, which should be absolute.
567 The truename of a file name is found by chasing symbolic links
568 both at the level of the file and at the level of the directories
569 containing it, until no links are left at any level.
571 The arguments COUNTER and PREV-DIRS are used only in recursive calls.
572 Do not specify them in other calls."
573 ;; COUNTER can be a cons cell whose car is the count of how many more links
574 ;; to chase before getting an error.
575 ;; PREV-DIRS can be a cons cell whose car is an alist
576 ;; of truenames we've just recently computed.
578 ;; The last test looks dubious, maybe `+' is meant here? --simon.
579 (if (or (string= filename
"") (string= filename
"~")
580 (and (string= (substring filename
0 1) "~")
581 (string-match "~[^/]*" filename
)))
583 (setq filename
(expand-file-name filename
))
584 (if (string= filename
"")
585 (setq filename
"/"))))
586 (or counter
(setq counter
(list 100)))
588 ;; For speed, remove the ange-ftp completion handler from the list.
589 ;; We know it's not needed here.
590 ;; For even more speed, do this only on the outermost call.
591 (file-name-handler-alist
592 (if prev-dirs file-name-handler-alist
593 (let ((tem (copy-sequence file-name-handler-alist
)))
594 (delq (rassq 'ange-ftp-completion-hook-function tem
) tem
)))))
595 (or prev-dirs
(setq prev-dirs
(list nil
)))
597 ;; andrewi@harlequin.co.uk - none of the following code (except for
598 ;; invoking the file-name handler) currently applies on Windows
599 ;; (ie. there are no native symlinks), but there is an issue with
600 ;; case differences being ignored by the OS, and short "8.3 DOS"
601 ;; name aliases existing for all files. (The short names are not
602 ;; reported by directory-files, but can be used to refer to files.)
603 ;; It seems appropriate for file-truename to resolve these issues in
604 ;; the most natural way, which on Windows is to call the function
605 ;; `w32-long-file-name' - this returns the exact name of a file as
606 ;; it is stored on disk (expanding short name aliases with the full
607 ;; name in the process).
608 (if (eq system-type
'windows-nt
)
609 (let ((handler (find-file-name-handler filename
'file-truename
))
611 ;; For file name that has a special handler, call handler.
612 ;; This is so that ange-ftp can save time by doing a no-op.
614 (setq filename
(funcall handler
'file-truename filename
))
615 ;; If filename contains a wildcard, newname will be the old name.
616 (if (string-match "[[*?]" filename
)
617 (setq newname filename
)
618 ;; If filename doesn't exist, newname will be nil.
619 (setq newname
(w32-long-file-name filename
)))
620 (setq filename
(or newname filename
)))
623 ;; If this file directly leads to a link, process that iteratively
624 ;; so that we don't use lots of stack.
626 (setcar counter
(1- (car counter
)))
627 (if (< (car counter
) 0)
628 (error "Apparent cycle of symbolic links for %s" filename
))
629 (let ((handler (find-file-name-handler filename
'file-truename
)))
630 ;; For file name that has a special handler, call handler.
631 ;; This is so that ange-ftp can save time by doing a no-op.
633 (setq filename
(funcall handler
'file-truename filename
)
635 (let ((dir (or (file-name-directory filename
) default-directory
))
637 ;; Get the truename of the directory.
638 (setq dirfile
(directory-file-name dir
))
639 ;; If these are equal, we have the (or a) root directory.
640 (or (string= dir dirfile
)
641 ;; If this is the same dir we last got the truename for,
642 ;; save time--don't recalculate.
643 (if (assoc dir
(car prev-dirs
))
644 (setq dir
(cdr (assoc dir
(car prev-dirs
))))
646 (new (file-name-as-directory (file-truename dirfile counter prev-dirs
))))
647 (setcar prev-dirs
(cons (cons old new
) (car prev-dirs
)))
649 (if (equal ".." (file-name-nondirectory filename
))
651 (directory-file-name (file-name-directory (directory-file-name dir
)))
653 (if (equal "." (file-name-nondirectory filename
))
654 (setq filename
(directory-file-name dir
)
656 ;; Put it back on the file name.
657 (setq filename
(concat dir
(file-name-nondirectory filename
)))
658 ;; Is the file name the name of a link?
659 (setq target
(file-symlink-p filename
))
661 ;; Yes => chase that link, then start all over
662 ;; since the link may point to a directory name that uses links.
663 ;; We can't safely use expand-file-name here
664 ;; since target might look like foo/../bar where foo
665 ;; is itself a link. Instead, we handle . and .. above.
667 (if (file-name-absolute-p target
)
675 (defun file-chase-links (filename)
676 "Chase links in FILENAME until a name that is not a link.
677 Does not examine containing directories for links,
678 unlike `file-truename'."
679 (let (tem (count 100) (newname filename
))
680 (while (setq tem
(file-symlink-p newname
))
683 (error "Apparent cycle of symbolic links for %s" filename
))
684 ;; In the context of a link, `//' doesn't mean what Emacs thinks.
685 (while (string-match "//+" tem
)
686 (setq tem
(replace-match "/" nil nil tem
)))
687 ;; Handle `..' by hand, since it needs to work in the
688 ;; target of any directory symlink.
689 ;; This code is not quite complete; it does not handle
690 ;; embedded .. in some cases such as ./../foo and foo/bar/../../../lose.
691 (while (string-match "\\`\\.\\./" tem
)
692 (setq tem
(substring tem
3))
693 (setq newname
(expand-file-name newname
))
694 ;; Chase links in the default dir of the symlink.
697 (directory-file-name (file-name-directory newname
))))
698 ;; Now find the parent of that dir.
699 (setq newname
(file-name-directory newname
)))
700 (setq newname
(expand-file-name tem
(file-name-directory newname
)))
701 (setq count
(1- count
))))
704 (defun switch-to-buffer-other-window (buffer &optional norecord
)
705 "Select buffer BUFFER in another window.
706 Optional second arg NORECORD non-nil means
707 do not put this buffer at the front of the list of recently selected ones.
709 This uses the function `display-buffer' as a subroutine; see its
710 documentation for additional customization information."
711 (interactive "BSwitch to buffer in other window: ")
712 (let ((pop-up-windows t
))
713 (pop-to-buffer buffer t norecord
)))
715 (defun switch-to-buffer-other-frame (buffer &optional norecord
)
716 "Switch to buffer BUFFER in another frame.
717 Optional second arg NORECORD non-nil means
718 do not put this buffer at the front of the list of recently selected ones.
720 This uses the function `display-buffer' as a subroutine; see its
721 documentation for additional customization information."
722 (interactive "BSwitch to buffer in other frame: ")
723 (let ((pop-up-frames t
))
724 (pop-to-buffer buffer t norecord
)
725 (raise-frame (window-frame (selected-window)))))
727 (defun find-file (filename &optional wildcards
)
729 Switch to a buffer visiting file FILENAME,
730 creating one if none already exists.
731 Interactively, or if WILDCARDS is non-nil in a call from Lisp,
732 expand wildcards (if any) and visit multiple files. Wildcard expansion
733 can be suppressed by setting `find-file-wildcards'."
734 (interactive "FFind file: \np")
735 (let ((value (find-file-noselect filename nil nil wildcards
)))
737 (mapcar 'switch-to-buffer
(nreverse value
))
738 (switch-to-buffer value
))))
740 (defun find-file-other-window (filename &optional wildcards
)
741 "Edit file FILENAME, in another window.
742 May create a new window, or reuse an existing one.
743 See the function `display-buffer'.
744 Interactively, or if WILDCARDS is non-nil in a call from Lisp,
745 expand wildcards (if any) and visit multiple files."
746 (interactive "FFind file in other window: \np")
747 (let ((value (find-file-noselect filename nil nil wildcards
)))
750 (setq value
(nreverse value
))
751 (switch-to-buffer-other-window (car value
))
752 (mapcar 'switch-to-buffer
(cdr value
)))
753 (switch-to-buffer-other-window value
))))
755 (defun find-file-other-frame (filename &optional wildcards
)
756 "Edit file FILENAME, in another frame.
757 May create a new frame, or reuse an existing one.
758 See the function `display-buffer'.
759 Interactively, or if WILDCARDS is non-nil in a call from Lisp,
760 expand wildcards (if any) and visit multiple files."
761 (interactive "FFind file in other frame: \np")
762 (let ((value (find-file-noselect filename nil nil wildcards
)))
765 (setq value
(nreverse value
))
766 (switch-to-buffer-other-frame (car value
))
767 (mapcar 'switch-to-buffer
(cdr value
)))
768 (switch-to-buffer-other-frame value
))))
770 (defun find-file-read-only (filename &optional wildcards
)
771 "Edit file FILENAME but don't allow changes.
772 Like `find-file' but marks buffer as read-only.
773 Use \\[toggle-read-only] to permit editing."
774 (interactive "fFind file read-only: \np")
775 (find-file filename wildcards
)
779 (defun find-file-read-only-other-window (filename &optional wildcards
)
780 "Edit file FILENAME in another window but don't allow changes.
781 Like \\[find-file-other-window] but marks buffer as read-only.
782 Use \\[toggle-read-only] to permit editing."
783 (interactive "fFind file read-only other window: \np")
784 (find-file-other-window filename wildcards
)
788 (defun find-file-read-only-other-frame (filename &optional wildcards
)
789 "Edit file FILENAME in another frame but don't allow changes.
790 Like \\[find-file-other-frame] but marks buffer as read-only.
791 Use \\[toggle-read-only] to permit editing."
792 (interactive "fFind file read-only other frame: \np")
793 (find-file-other-frame filename wildcards
)
797 (defun find-alternate-file-other-window (filename)
798 "Find file FILENAME as a replacement for the file in the next window.
799 This command does not select that window."
801 (save-selected-window
803 (let ((file buffer-file-name
)
807 (setq file-name
(file-name-nondirectory file
)
808 file-dir
(file-name-directory file
)))
809 (list (read-file-name
810 "Find alternate file: " file-dir nil nil file-name
)))))
812 (find-file-other-window filename
)
813 (save-selected-window
815 (find-alternate-file filename
))))
817 (defun find-alternate-file (filename)
818 "Find file FILENAME, select its buffer, kill previous buffer.
819 If the current buffer now contains an empty file that you just visited
820 \(presumably by mistake), use this command to visit the file you really want."
822 (let ((file buffer-file-name
)
826 (setq file-name
(file-name-nondirectory file
)
827 file-dir
(file-name-directory file
)))
828 (list (read-file-name
829 "Find alternate file: " file-dir nil nil file-name
))))
830 (and (buffer-modified-p) (buffer-file-name)
831 ;; (not buffer-read-only)
832 (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? "
835 (let ((obuf (current-buffer))
836 (ofile buffer-file-name
)
837 (onum buffer-file-number
)
838 (otrue buffer-file-truename
)
839 (oname (buffer-name)))
840 (if (get-buffer " **lose**")
841 (kill-buffer " **lose**"))
842 (rename-buffer " **lose**")
846 (setq buffer-file-name nil
)
847 (setq buffer-file-number nil
)
848 (setq buffer-file-truename nil
)
849 (find-file filename
))
850 (cond ((eq obuf
(current-buffer))
851 (setq buffer-file-name ofile
)
852 (setq buffer-file-number onum
)
853 (setq buffer-file-truename otrue
)
855 (rename-buffer oname
))))
856 (or (eq (current-buffer) obuf
)
857 (kill-buffer obuf
))))
859 (defun create-file-buffer (filename)
860 "Create a suitably named buffer for visiting FILENAME, and return it.
861 FILENAME (sans directory) is used unchanged if that name is free;
862 otherwise a string <2> or <3> or ... is appended to get an unused name."
863 (let ((lastname (file-name-nondirectory filename
)))
864 (if (string= lastname
"")
865 (setq lastname filename
))
866 (generate-new-buffer lastname
)))
868 (defun generate-new-buffer (name)
869 "Create and return a buffer with a name based on NAME.
870 Choose the buffer's name using `generate-new-buffer-name'."
871 (get-buffer-create (generate-new-buffer-name name
)))
873 (defcustom automount-dir-prefix
"^/tmp_mnt/"
874 "Regexp to match the automounter prefix in a directory name."
878 (defvar abbreviated-home-dir nil
879 "The user's homedir abbreviated according to `directory-abbrev-alist'.")
881 (defun abbreviate-file-name (filename)
882 "Return a version of FILENAME shortened using `directory-abbrev-alist'.
883 This also substitutes \"~\" for the user's home directory.
884 Type \\[describe-variable] directory-abbrev-alist RET for more information."
885 ;; Get rid of the prefixes added by the automounter.
886 (if (and automount-dir-prefix
887 (string-match automount-dir-prefix filename
)
888 (file-exists-p (file-name-directory
889 (substring filename
(1- (match-end 0))))))
890 (setq filename
(substring filename
(1- (match-end 0)))))
891 (let ((tail directory-abbrev-alist
))
892 ;; If any elt of directory-abbrev-alist matches this name,
893 ;; abbreviate accordingly.
895 (if (string-match (car (car tail
)) filename
)
897 (concat (cdr (car tail
)) (substring filename
(match-end 0)))))
898 (setq tail
(cdr tail
)))
899 ;; Compute and save the abbreviated homedir name.
900 ;; We defer computing this until the first time it's needed, to
901 ;; give time for directory-abbrev-alist to be set properly.
902 ;; We include a slash at the end, to avoid spurious matches
903 ;; such as `/usr/foobar' when the home dir is `/usr/foo'.
904 (or abbreviated-home-dir
905 (setq abbreviated-home-dir
906 (let ((abbreviated-home-dir "$foo"))
907 (concat "^" (abbreviate-file-name (expand-file-name "~"))
910 ;; If FILENAME starts with the abbreviated homedir,
911 ;; make it start with `~' instead.
912 (if (and (string-match abbreviated-home-dir filename
)
913 ;; If the home dir is just /, don't change it.
914 (not (and (= (match-end 0) 1)
915 (= (aref filename
0) ?
/)))
916 ;; MS-DOS root directories can come with a drive letter;
917 ;; Novell Netware allows drive letters beyond `Z:'.
918 (not (and (or (eq system-type
'ms-dos
)
919 (eq system-type
'windows-nt
))
921 (string-match "^[a-zA-`]:/$" filename
)))))
924 (substring filename
(match-beginning 1) (match-end 1))
925 (substring filename
(match-end 0)))))
928 (defcustom find-file-not-true-dirname-list nil
929 "*List of logical names for which visiting shouldn't save the true dirname.
930 On VMS, when you visit a file using a logical name that searches a path,
931 you may or may not want the visited file name to record the specific
932 directory where the file was found. If you *do not* want that, add the logical
933 name to this list as a string."
934 :type
'(repeat (string :tag
"Name"))
937 (defun find-buffer-visiting (filename)
938 "Return the buffer visiting file FILENAME (a string).
939 This is like `get-file-buffer', except that it checks for any buffer
940 visiting the same file, possibly under a different name.
941 If there is no such live buffer, return nil."
942 (let ((buf (get-file-buffer filename
))
943 (truename (abbreviate-file-name (file-truename filename
))))
945 (let ((list (buffer-list)) found
)
946 (while (and (not found
) list
)
948 (set-buffer (car list
))
949 (if (and buffer-file-name
950 (string= buffer-file-truename truename
))
951 (setq found
(car list
))))
952 (setq list
(cdr list
)))
954 (let* ((attributes (file-attributes truename
))
955 (number (nthcdr 10 attributes
))
956 (list (buffer-list)) found
)
957 (and buffer-file-numbers-unique
959 (while (and (not found
) list
)
960 (with-current-buffer (car list
)
961 (if (and buffer-file-name
962 (equal buffer-file-number number
)
963 ;; Verify this buffer's file number
964 ;; still belongs to its file.
965 (file-exists-p buffer-file-name
)
966 (equal (file-attributes buffer-file-name
)
968 (setq found
(car list
))))
969 (setq list
(cdr list
))))
972 (defcustom find-file-wildcards t
973 "*Non-nil means file-visiting commands should handle wildcards.
974 For example, if you specify `*.c', that would visit all the files
975 whose names match the pattern."
980 (defcustom find-file-suppress-same-file-warnings nil
981 "*Non-nil means suppress warning messages for symlinked files.
982 When nil, Emacs prints a warning when visiting a file that is already
983 visited, but with a different name. Setting this option to t
984 suppresses this warning."
989 (defun find-file-noselect (filename &optional nowarn rawfile wildcards
)
990 "Read file FILENAME into a buffer and return the buffer.
991 If a buffer exists visiting FILENAME, return that one, but
992 verify that the file has not changed since visited or saved.
993 The buffer is not selected, just returned to the caller.
994 Optional first arg NOWARN non-nil means suppress any warning messages.
995 Optional second arg RAWFILE non-nil means the file is read literally.
996 Optional third arg WILDCARDS non-nil means do wildcard processing
997 and visit all the matching files. When wildcards are actually
998 used and expanded, the value is a list of buffers
999 that are visiting the various files."
1001 (abbreviate-file-name
1002 (expand-file-name filename
)))
1003 (if (file-directory-p filename
)
1004 (or (and find-file-run-dired
1005 (run-hook-with-args-until-success
1006 'find-directory-functions
1007 (if find-file-visit-truename
1008 (abbreviate-file-name (file-truename filename
))
1010 (error "%s is a directory" filename
))
1013 (not (string-match "\\`/:" filename
))
1014 (string-match "[[*?]" filename
))
1015 (let ((files (condition-case nil
1016 (file-expand-wildcards filename t
)
1017 (error (list filename
))))
1018 (find-file-wildcards nil
))
1020 (find-file-noselect filename
)
1021 (mapcar #'find-file-noselect files
)))
1022 (let* ((buf (get-file-buffer filename
))
1023 (truename (abbreviate-file-name (file-truename filename
)))
1024 (number (nthcdr 10 (file-attributes truename
)))
1025 ;; Find any buffer for a file which has same truename.
1026 (other (and (not buf
) (find-buffer-visiting filename
))))
1027 ;; Let user know if there is a buffer with the same truename.
1031 find-file-suppress-same-file-warnings
1032 (string-equal filename
(buffer-file-name other
))
1033 (message "%s and %s are the same file"
1034 filename
(buffer-file-name other
)))
1035 ;; Optionally also find that buffer.
1036 (if (or find-file-existing-other-name find-file-visit-truename
)
1039 ;; We are using an existing buffer.
1042 (verify-visited-file-modtime buf
)
1043 (cond ((not (file-exists-p filename
))
1044 (error "File %s no longer exists!" filename
))
1045 ;; Certain files should be reverted automatically
1046 ;; if they have changed on disk and not in the buffer.
1047 ((and (not (buffer-modified-p buf
))
1048 (let ((tail revert-without-query
)
1051 (if (string-match (car tail
) filename
)
1053 (setq tail
(cdr tail
)))
1055 (with-current-buffer buf
1056 (message "Reverting file %s..." filename
)
1058 (message "Reverting file %s...done" filename
)))
1060 (if (string= (file-name-nondirectory filename
)
1063 (if (buffer-modified-p buf
)
1064 "File %s changed on disk. Discard your edits? "
1065 "File %s changed on disk. Reread from disk? ")
1066 (file-name-nondirectory filename
))
1068 (if (buffer-modified-p buf
)
1069 "File %s changed on disk. Discard your edits in %s? "
1070 "File %s changed on disk. Reread from disk into %s? ")
1071 (file-name-nondirectory filename
)
1072 (buffer-name buf
))))
1073 (with-current-buffer buf
1074 (revert-buffer t t
)))))
1075 (with-current-buffer buf
1076 (when (and (not (eq (not (null rawfile
))
1077 (not (null find-file-literally
))))
1078 ;; It is confusing to ask whether to visit
1079 ;; non-literally if they have the file in
1081 (not (eq major-mode
'hexl-mode
)))
1082 (if (buffer-modified-p)
1083 (if (y-or-n-p (if rawfile
1084 "Save file and revisit literally? "
1085 "Save file and revisit non-literally? "))
1088 (find-file-noselect-1 buf filename nowarn
1089 rawfile truename number
))
1090 (if (y-or-n-p (if rawfile
1091 "Discard your edits and revisit file literally? "
1092 "Discard your edits and revisit file non-literally? "))
1093 (find-file-noselect-1 buf filename nowarn
1094 rawfile truename number
)
1095 (error (if rawfile
"File already visited non-literally"
1096 "File already visited literally"))))
1097 (if (y-or-n-p (if rawfile
1098 "Revisit file literally? "
1099 "Revisit file non-literally? "))
1100 (find-file-noselect-1 buf filename nowarn
1101 rawfile truename number
)
1102 (error (if rawfile
"File already visited non-literally"
1103 "File already visited literally"))))))
1104 ;; Return the buffer we are using.
1106 ;; Create a new buffer.
1107 (setq buf
(create-file-buffer filename
))
1108 (set-buffer-major-mode buf
)
1109 ;; find-file-noselect-1 may use a different buffer.
1110 (find-file-noselect-1 buf filename nowarn
1111 rawfile truename number
))))))
1113 (defun find-file-noselect-1 (buf filename nowarn rawfile truename number
)
1114 (let ((inhibit-read-only t
)
1116 (with-current-buffer buf
1117 (kill-local-variable 'find-file-literally
)
1118 ;; Needed in case we are re-visiting the file with a different
1119 ;; text representation.
1120 (kill-local-variable 'buffer-file-coding-system
)
1122 (and (default-value 'enable-multibyte-characters
)
1124 (set-buffer-multibyte t
))
1127 (insert-file-contents-literally filename t
)
1129 (when (and (file-exists-p filename
)
1130 (not (file-readable-p filename
)))
1132 (signal 'file-error
(list "File is not readable"
1134 ;; Unconditionally set error
1137 (insert-file-contents filename t
)
1139 (when (and (file-exists-p filename
)
1140 (not (file-readable-p filename
)))
1142 (signal 'file-error
(list "File is not readable"
1144 ;; Run find-file-not-found-hooks until one returns non-nil.
1145 (or (run-hook-with-args-until-success 'find-file-not-found-hooks
)
1146 ;; If they fail too, set error.
1148 ;; Record the file's truename, and maybe use that as visited name.
1149 (if (equal filename buffer-file-name
)
1150 (setq buffer-file-truename truename
)
1151 (setq buffer-file-truename
1152 (abbreviate-file-name (file-truename buffer-file-name
))))
1153 (setq buffer-file-number number
)
1154 ;; On VMS, we may want to remember which directory in a search list
1155 ;; the file was found in.
1156 (and (eq system-type
'vax-vms
)
1158 (if (string-match ":" (file-name-directory filename
))
1159 (setq logical
(substring (file-name-directory filename
)
1160 0 (match-beginning 0))))
1161 (not (member logical find-file-not-true-dirname-list
)))
1162 (setq buffer-file-name buffer-file-truename
))
1163 (if find-file-visit-truename
1164 (setq buffer-file-name
1166 (expand-file-name buffer-file-truename
))))
1167 ;; Set buffer's default directory to that of the file.
1168 (setq default-directory
(file-name-directory buffer-file-name
))
1169 ;; Turn off backup files for certain file names. Since
1170 ;; this is a permanent local, the major mode won't eliminate it.
1171 (and (not (funcall backup-enable-predicate buffer-file-name
))
1173 (make-local-variable 'backup-inhibited
)
1174 (setq backup-inhibited t
)))
1177 (set-buffer-multibyte nil
)
1178 (setq buffer-file-coding-system
'no-conversion
)
1179 (make-local-variable 'find-file-literally
)
1180 (setq find-file-literally t
))
1181 (after-find-file error
(not nowarn
)))
1184 (defun insert-file-contents-literally (filename &optional visit beg end replace
)
1185 "Like `insert-file-contents', but only reads in the file literally.
1186 A buffer may be modified in several ways after reading into the buffer,
1187 to Emacs features such as format decoding, character code
1188 conversion, `find-file-hooks', automatic uncompression, etc.
1190 This function ensures that none of these modifications will take place."
1191 (let ((format-alist nil
)
1192 (after-insert-file-functions nil
)
1193 (coding-system-for-read 'no-conversion
)
1194 (coding-system-for-write 'no-conversion
)
1195 (find-buffer-file-type-function
1196 (if (fboundp 'find-buffer-file-type
)
1197 (symbol-function 'find-buffer-file-type
)
1199 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler
))
1200 (inhibit-file-name-operation 'insert-file-contents
))
1203 (fset 'find-buffer-file-type
(lambda (filename) t
))
1204 (insert-file-contents filename visit beg end replace
))
1205 (if find-buffer-file-type-function
1206 (fset 'find-buffer-file-type find-buffer-file-type-function
)
1207 (fmakunbound 'find-buffer-file-type
)))))
1209 (defun insert-file-literally (filename)
1210 "Insert contents of file FILENAME into buffer after point with no conversion.
1212 This function is meant for the user to run interactively.
1213 Don't call it from programs! Use `insert-file-contents-literally' instead.
1214 \(Its calling sequence is different; see its documentation)."
1215 (interactive "*fInsert file literally: ")
1216 (if (file-directory-p filename
)
1217 (signal 'file-error
(list "Opening input file" "file is a directory"
1219 (let ((tem (insert-file-contents-literally filename
)))
1220 (push-mark (+ (point) (car (cdr tem
))))))
1222 (defvar find-file-literally nil
1223 "Non-nil if this buffer was made by `find-file-literally' or equivalent.
1224 This is a permanent local.")
1225 (put 'find-file-literally
'permanent-local t
)
1227 (defun find-file-literally (filename)
1228 "Visit file FILENAME with no conversion of any kind.
1229 Format conversion and character code conversion are both disabled,
1230 and multibyte characters are disabled in the resulting buffer.
1231 The major mode used is Fundamental mode regardless of the file name,
1232 and local variable specifications in the file are ignored.
1233 Automatic uncompression and adding a newline at the end of the
1234 file due to `require-final-newline' is also disabled.
1236 You cannot absolutely rely on this function to result in
1237 visiting the file literally. If Emacs already has a buffer
1238 which is visiting the file, you get the existing buffer,
1239 regardless of whether it was created literally or not.
1241 In a Lisp program, if you want to be sure of accessing a file's
1242 contents literally, you should create a temporary buffer and then read
1243 the file contents into it using `insert-file-contents-literally'."
1244 (interactive "FFind file literally: ")
1245 (switch-to-buffer (find-file-noselect filename nil t
)))
1247 (defvar after-find-file-from-revert-buffer nil
)
1249 (defun after-find-file (&optional error warn noauto
1250 after-find-file-from-revert-buffer
1252 "Called after finding a file and by the default revert function.
1253 Sets buffer mode, parses local variables.
1254 Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an
1255 error in reading the file. WARN non-nil means warn if there
1256 exists an auto-save file more recent than the visited file.
1257 NOAUTO means don't mess with auto-save mode.
1258 Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil
1259 means this call was from `revert-buffer'.
1260 Fifth arg NOMODES non-nil means don't alter the file's modes.
1261 Finishes by calling the functions in `find-file-hooks'
1262 unless NOMODES is non-nil."
1263 (setq buffer-read-only
(not (file-writable-p buffer-file-name
)))
1270 ((and error
(file-attributes buffer-file-name
))
1271 (setq buffer-read-only t
)
1272 "File exists, but cannot be read")
1273 ((not buffer-read-only
)
1275 ;; No need to warn if buffer is auto-saved
1276 ;; under the name of the visited file.
1277 (not (and buffer-file-name
1278 auto-save-visited-file-name
))
1279 (file-newer-than-file-p (or buffer-auto-save-file-name
1280 (make-auto-save-file-name))
1282 (format "%s has auto save data; consider M-x recover-file"
1283 (file-name-nondirectory buffer-file-name
))
1284 (setq not-serious t
)
1285 (if error
"(New file)" nil
)))
1287 (setq not-serious t
)
1288 "Note: file is write protected")
1289 ((file-attributes (directory-file-name default-directory
))
1290 "File not found and directory write-protected")
1291 ((file-exists-p (file-name-directory buffer-file-name
))
1292 (setq buffer-read-only nil
))
1294 (setq buffer-read-only nil
)
1295 (if (file-exists-p (file-name-directory (directory-file-name (file-name-directory buffer-file-name
))))
1296 "Use M-x make-directory RET RET to create the directory"
1297 "Use C-u M-x make-directory RET RET to create directory and its parents")))))
1300 (or not-serious
(sit-for 1 nil t
))))
1301 (when (and auto-save-default
(not noauto
))
1302 (auto-save-mode t
)))
1303 ;; Make people do a little extra work (C-x C-q)
1304 ;; before altering a backup file.
1305 (when (backup-file-name-p buffer-file-name
)
1306 (setq buffer-read-only t
))
1308 (when (and view-read-only view-mode
)
1309 (view-mode-disable))
1311 (when (and buffer-read-only
1313 (not (eq (get major-mode
'mode-class
) 'special
)))
1315 (run-hooks 'find-file-hooks
)))
1317 (defun normal-mode (&optional find-file
)
1318 "Choose the major mode for this buffer automatically.
1319 Also sets up any specified local variables of the file.
1320 Uses the visited file name, the -*- line, and the local variables spec.
1322 This function is called automatically from `find-file'. In that case,
1323 we may set up the file-specified mode and local variables,
1324 depending on the value of `enable-local-variables': if it is t, we do;
1325 if it is nil, we don't; otherwise, we query.
1326 In addition, if `local-enable-local-variables' is nil, we do
1327 not set local variables (though we do notice a mode specified with -*-.)
1329 `enable-local-variables' is ignored if you run `normal-mode' interactively,
1330 or from Lisp without specifying the optional argument FIND-FILE;
1331 in that case, this function acts as if `enable-local-variables' were t."
1333 (or find-file
(funcall (or default-major-mode
'fundamental-mode
)))
1336 (error (message "File mode specification error: %s"
1337 (prin1-to-string err
))))
1339 (let ((enable-local-variables (or (not find-file
)
1340 enable-local-variables
)))
1341 (hack-local-variables))
1342 (error (message "File local-variables error: %s"
1343 (prin1-to-string err
)))))
1345 (defvar auto-mode-alist
1348 (cons (purecopy (car elt
)) (cdr elt
)))
1349 '(("\\.te?xt\\'" . text-mode
)
1350 ("\\.c\\'" . c-mode
)
1351 ("\\.h\\'" . c-mode
)
1352 ("\\.tex\\'" . tex-mode
)
1353 ("\\.ltx\\'" . latex-mode
)
1354 ("\\.el\\'" . emacs-lisp-mode
)
1355 ("\\.scm\\'" . scheme-mode
)
1356 ("\\.l\\'" . lisp-mode
)
1357 ("\\.lisp\\'" . lisp-mode
)
1358 ("\\.f\\'" . fortran-mode
)
1359 ("\\.F\\'" . fortran-mode
)
1360 ("\\.for\\'" . fortran-mode
)
1361 ("\\.p\\'" . pascal-mode
)
1362 ("\\.pas\\'" . pascal-mode
)
1363 ("\\.ad[abs]\\'" . ada-mode
)
1364 ("\\.\\([pP]\\([Llm]\\|erl\\)\\|al\\)\\'" . perl-mode
)
1365 ("\\.s?html?\\'" . html-mode
)
1366 ("\\.cc\\'" . c
++-mode
)
1367 ("\\.hh\\'" . c
++-mode
)
1368 ("\\.hpp\\'" . c
++-mode
)
1369 ("\\.C\\'" . c
++-mode
)
1370 ("\\.H\\'" . c
++-mode
)
1371 ("\\.cpp\\'" . c
++-mode
)
1372 ("\\.cxx\\'" . c
++-mode
)
1373 ("\\.hxx\\'" . c
++-mode
)
1374 ("\\.c\\+\\+\\'" . c
++-mode
)
1375 ("\\.h\\+\\+\\'" . c
++-mode
)
1376 ("\\.m\\'" . objc-mode
)
1377 ("\\.java\\'" . java-mode
)
1378 ("\\.mk\\'" . makefile-mode
)
1379 ("\\(M\\|m\\|GNUm\\)akefile\\(\\.in\\)?\\'" . makefile-mode
)
1380 ("\\.am\\'" . makefile-mode
) ;For Automake.
1381 ;; Less common extensions come here
1382 ;; so more common ones above are found faster.
1383 ("\\.texinfo\\'" . texinfo-mode
)
1384 ("\\.te?xi\\'" . texinfo-mode
)
1385 ("\\.s\\'" . asm-mode
)
1386 ("\\.S\\'" . asm-mode
)
1387 ("\\.asm\\'" . asm-mode
)
1388 ("ChangeLog\\'" . change-log-mode
)
1389 ("change\\.log\\'" . change-log-mode
)
1390 ("changelo\\'" . change-log-mode
)
1391 ("ChangeLog\\.[0-9]+\\'" . change-log-mode
)
1392 ;; for MSDOS and MS-Windows (which are case-insensitive)
1393 ("changelog\\'" . change-log-mode
)
1394 ("changelog\\.[0-9]+\\'" . change-log-mode
)
1395 ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode
)
1396 ("\\.scm\\.[0-9]*\\'" . scheme-mode
)
1397 ("\\.[ck]?sh\\'\\|\\.shar\\'\\|/\\.z?profile\\'" . sh-mode
)
1398 ("\\(/\\|\\`\\)\\.\\(bash_profile\\|z?login\\|bash_login\\|z?logout\\)\\'" . sh-mode
)
1399 ("\\(/\\|\\`\\)\\.\\(bash_logout\\|shrc\\|[kz]shrc\\|bashrc\\|t?cshrc\\|esrc\\)\\'" . sh-mode
)
1400 ("\\(/\\|\\`\\)\\.\\([kz]shenv\\|xinitrc\\|startxrc\\|xsession\\)\\'" . sh-mode
)
1401 ("\\.m?spec\\'" . sh-mode
)
1402 ("\\.mm\\'" . nroff-mode
)
1403 ("\\.me\\'" . nroff-mode
)
1404 ("\\.ms\\'" . nroff-mode
)
1405 ("\\.man\\'" . nroff-mode
)
1406 ("\\.\\(u?lpc\\|pike\\|pmod\\)\\'" . pike-mode
)
1407 ("\\.TeX\\'" . tex-mode
)
1408 ("\\.sty\\'" . latex-mode
)
1409 ("\\.cls\\'" . latex-mode
) ;LaTeX 2e class
1410 ("\\.clo\\'" . latex-mode
) ;LaTeX 2e class option
1411 ("\\.bbl\\'" . latex-mode
)
1412 ("\\.bib\\'" . bibtex-mode
)
1413 ("\\.sql\\'" . sql-mode
)
1414 ("\\.m4\\'" . m4-mode
)
1415 ("\\.mc\\'" . m4-mode
)
1416 ("\\.mf\\'" . metafont-mode
)
1417 ("\\.mp\\'" . metapost-mode
)
1418 ("\\.vhdl?\\'" . vhdl-mode
)
1419 ("\\.article\\'" . text-mode
)
1420 ("\\.letter\\'" . text-mode
)
1421 ("\\.tcl\\'" . tcl-mode
)
1422 ("\\.exp\\'" . tcl-mode
)
1423 ("\\.itcl\\'" . tcl-mode
)
1424 ("\\.itk\\'" . tcl-mode
)
1425 ("\\.icn\\'" . icon-mode
)
1426 ("\\.sim\\'" . simula-mode
)
1427 ("\\.mss\\'" . scribe-mode
)
1428 ("\\.f90\\'" . f90-mode
)
1429 ("\\.indent\\.pro\\'" . fundamental-mode
) ; to avoid idlwave-mode
1430 ("\\.pro\\'" . idlwave-mode
)
1431 ("\\.lsp\\'" . lisp-mode
)
1432 ("\\.awk\\'" . awk-mode
)
1433 ("\\.prolog\\'" . prolog-mode
)
1434 ("\\.tar\\'" . tar-mode
)
1435 ("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\)\\'" . archive-mode
)
1436 ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|JAR\\)\\'" . archive-mode
)
1437 ;; Mailer puts message to be edited in
1438 ;; /tmp/Re.... or Message
1439 ("\\`/tmp/Re" . text-mode
)
1440 ("/Message[0-9]*\\'" . text-mode
)
1441 ("/drafts/[0-9]+\\'" . mh-letter-mode
)
1442 ("\\.zone\\'" . zone-mode
)
1443 ;; some news reader is reported to use this
1444 ("\\`/tmp/fol/" . text-mode
)
1445 ("\\.y\\'" . c-mode
)
1446 ("\\.lex\\'" . c-mode
)
1447 ("\\.oak\\'" . scheme-mode
)
1448 ("\\.sgml?\\'" . sgml-mode
)
1449 ("\\.xml\\'" . sgml-mode
)
1450 ("\\.dtd\\'" . sgml-mode
)
1451 ("\\.ds\\(ss\\)?l\\'" . dsssl-mode
)
1452 ("\\.idl\\'" . idl-mode
)
1453 ;; .emacs following a directory delimiter
1454 ;; in Unix, MSDOG or VMS syntax.
1455 ("[]>:/\\]\\..*emacs\\'" . emacs-lisp-mode
)
1456 ("\\`\\..*emacs\\'" . emacs-lisp-mode
)
1457 ;; _emacs following a directory delimiter
1459 ("[:/]_emacs\\'" . emacs-lisp-mode
)
1460 ("/crontab\\.X*[0-9]+\\'" . shell-script-mode
)
1461 ("\\.ml\\'" . lisp-mode
)
1462 ("\\.\\(asn\\|mib\\|smi\\)\\'" . snmp-mode
)
1463 ("\\.\\(as\\|mi\\|sm\\)2\\'" . snmpv2-mode
)
1464 ("\\.\\(diffs?\\|patch\\|rej\\)\\'" . diff-mode
)
1465 ("\\.\\(dif\\|pat\\)\\'" . diff-mode
) ; for MSDOG
1466 ("\\.[eE]?[pP][sS]\\'" . ps-mode
)
1467 ("configure\\.\\(ac\\|in\\)\\'" . autoconf-mode
)
1468 ("BROWSE\\'" . ebrowse-tree-mode
)
1469 ("\\.ebrowse\\'" . ebrowse-tree-mode
)
1470 ("#\\*mail\\*" . mail-mode
)
1471 ;; Get rid of any trailing .n.m and try again.
1472 ;; This is for files saved by cvs-merge that look like .#<file>.<rev>
1473 ;; or .#<file>.<rev>-<rev> or VC's <file>.~<rev>~.
1474 ;; Using mode nil rather than `ignore' would let the search continue
1475 ;; through this list (with the shortened name) rather than start over.
1476 ("\\.~?[0-9]+\\.[0-9][-.0-9]*~?\\'" ignore t
)
1477 ;; The following should come after the ChangeLog pattern
1478 ;; for the sake of ChangeLog.1, etc.
1479 ;; and after the .scm.[0-9] and CVS' <file>.<rev> patterns too.
1480 ("\\.[1-9]\\'" . nroff-mode
)
1481 ("\\.g\\'" . antlr-mode
)))
1482 "Alist of filename patterns vs corresponding major mode functions.
1483 Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL).
1484 \(NON-NIL stands for anything that is not nil; the value does not matter.)
1485 Visiting a file whose name matches REGEXP specifies FUNCTION as the
1486 mode function to use. FUNCTION will be called, unless it is nil.
1488 If the element has the form (REGEXP FUNCTION NON-NIL), then after
1489 calling FUNCTION (if it's not nil), we delete the suffix that matched
1490 REGEXP and search the list again for another match.")
1493 (defvar interpreter-mode-alist
1496 (cons (purecopy (car l
)) (cdr l
)))
1497 '(("perl" . perl-mode
)
1498 ("perl5" . perl-mode
)
1499 ("miniperl" . perl-mode
)
1501 ("wishx" . tcl-mode
)
1503 ("tclsh" . tcl-mode
)
1508 ("scm" . scheme-mode
)
1528 ("tail" . text-mode
)
1529 ("more" . text-mode
)
1530 ("less" . text-mode
)
1532 ("make" . makefile-mode
) ; Debian uses this
1533 ("guile" . scheme-mode
)
1534 ("clisp" . lisp-mode
)))
1535 "Alist mapping interpreter names to major modes.
1536 This alist applies to files whose first line starts with `#!'.
1537 Each element looks like (INTERPRETER . MODE).
1538 The car of each element is compared with
1539 the name of the interpreter specified in the first line.
1540 If it matches, mode MODE is selected.")
1542 (defvar inhibit-first-line-modes-regexps
'("\\.tar\\'" "\\.tgz\\'")
1543 "List of regexps; if one matches a file name, don't look for `-*-'.")
1545 (defvar inhibit-first-line-modes-suffixes nil
1546 "List of regexps for what to ignore, for `inhibit-first-line-modes-regexps'.
1547 When checking `inhibit-first-line-modes-regexps', we first discard
1548 from the end of the file name anything that matches one of these regexps.")
1550 (defvar auto-mode-interpreter-regexp
1551 "#![ \t]?\\([^ \t\n]*\
1552 /bin/env[ \t]\\)?\\([^ \t\n]+\\)"
1553 "Regular expression matching interpreters, for file mode determination.
1554 This regular expression is matched against the first line of a file
1555 to determine the file's mode in `set-auto-mode' when Emacs can't deduce
1556 a mode from the file's name. If it matches, the file is assumed to
1557 be interpreted by the interpreter matched by the second group of the
1558 regular expression. The mode is then determined as the mode associated
1559 with that interpreter in `interpreter-mode-alist'.")
1561 (defun set-auto-mode (&optional just-from-file-name
)
1562 "Select major mode appropriate for current buffer.
1563 This checks for a -*- mode tag in the buffer's text,
1564 compares the filename against the entries in `auto-mode-alist',
1565 or checks the interpreter that runs this file against
1566 `interpreter-mode-alist'.
1568 It does not check for the `mode:' local variable in the
1569 Local Variables section of the file; for that, use `hack-local-variables'.
1571 If `enable-local-variables' is nil, this function does not check for a
1574 If the optional argument JUST-FROM-FILE-NAME is non-nil,
1575 then we do not set anything but the major mode,
1576 and we don't even do that unless it would come from the file name."
1577 ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
1578 (let (beg end done modes
)
1580 (goto-char (point-min))
1581 (skip-chars-forward " \t\n")
1582 (and enable-local-variables
1583 ;; Don't look for -*- if this file name matches any
1584 ;; of the regexps in inhibit-first-line-modes-regexps.
1585 (let ((temp inhibit-first-line-modes-regexps
)
1586 (name (if buffer-file-name
1587 (file-name-sans-versions buffer-file-name
)
1589 (while (let ((sufs inhibit-first-line-modes-suffixes
))
1590 (while (and sufs
(not (string-match (car sufs
) name
)))
1591 (setq sufs
(cdr sufs
)))
1593 (setq name
(substring name
0 (match-beginning 0))))
1595 (not (string-match (car temp
) name
)))
1596 (setq temp
(cdr temp
)))
1598 (search-forward "-*-" (save-excursion
1599 ;; If the file begins with "#!"
1600 ;; (exec interpreter magic), look
1601 ;; for mode frobs in the first two
1602 ;; lines. You cannot necessarily
1603 ;; put them in the first line of
1604 ;; such a file without screwing up
1605 ;; the interpreter invocation.
1606 (end-of-line (and (looking-at "^#!") 2))
1609 (skip-chars-forward " \t")
1611 (search-forward "-*-"
1612 (save-excursion (end-of-line) (point))
1616 (skip-chars-backward " \t")
1619 (if (save-excursion (search-forward ":" end t
))
1620 ;; Find all specifications for the `mode:' variable
1621 ;; and execute them left to right.
1622 (while (let ((case-fold-search t
))
1623 (or (and (looking-at "mode:")
1624 (goto-char (match-end 0)))
1625 (re-search-forward "[ \t;]mode:" end t
)))
1626 (skip-chars-forward " \t")
1628 (if (search-forward ";" end t
)
1631 (skip-chars-backward " \t")
1632 (push (intern (concat (downcase (buffer-substring beg
(point))) "-mode"))
1634 ;; Simple -*-MODE-*- case.
1635 (push (intern (concat (downcase (buffer-substring beg end
))
1638 ;; If we found modes to use, invoke them now,
1639 ;; outside the save-excursion.
1640 (unless just-from-file-name
1641 (dolist (mode (nreverse modes
))
1642 (if (not (functionp mode
))
1643 (message "Ignoring unknown mode `%s'" mode
)
1646 ;; If we didn't find a mode from a -*- line, try using the file name.
1647 (if (and (not done
) buffer-file-name
)
1648 (let ((name buffer-file-name
)
1650 ;; Remove backup-suffixes from file name.
1651 (setq name
(file-name-sans-versions name
))
1653 (setq keep-going nil
)
1654 (let ((alist auto-mode-alist
)
1656 ;; Find first matching alist entry.
1657 (let ((case-fold-search
1658 (memq system-type
'(vax-vms windows-nt
))))
1659 (while (and (not mode
) alist
)
1660 (if (string-match (car (car alist
)) name
)
1661 (if (and (consp (cdr (car alist
)))
1662 (nth 2 (car alist
)))
1663 (setq mode
(car (cdr (car alist
)))
1664 name
(substring name
0 (match-beginning 0))
1666 (setq mode
(cdr (car alist
))
1668 (setq alist
(cdr alist
))))
1670 ;; When JUST-FROM-FILE-NAME is set,
1671 ;; we are working on behalf of set-visited-file-name.
1672 ;; In that case, if the major mode specified is the
1673 ;; same one we already have, don't actually reset it.
1674 ;; We don't want to lose minor modes such as Font Lock.
1675 (unless (and just-from-file-name
(eq mode major-mode
))
1677 ;; If we can't deduce a mode from the file name,
1678 ;; look for an interpreter specified in the first line.
1679 ;; As a special case, allow for things like "#!/bin/env perl",
1680 ;; which finds the interpreter anywhere in $PATH.
1683 (goto-char (point-min))
1684 (if (looking-at auto-mode-interpreter-regexp
)
1688 ;; Map interpreter name to a mode.
1689 (setq elt
(assoc (file-name-nondirectory interpreter
)
1690 interpreter-mode-alist
))
1691 (unless just-from-file-name
1693 (funcall (cdr elt
))))))))))))
1695 (defun hack-local-variables-prop-line ()
1696 "Set local variables specified in the -*- line.
1697 Ignore any specification for `mode:' and `coding:';
1698 `set-auto-mode' should already have handled `mode:',
1699 `set-auto-coding' should already have handled `coding:'."
1701 (goto-char (point-min))
1703 (end (save-excursion (end-of-line (and (looking-at "^#!") 2)) (point)))
1704 (enable-local-variables
1705 (and local-enable-local-variables enable-local-variables
)))
1706 ;; Parse the -*- line into the `result' alist.
1707 (cond ((not (search-forward "-*-" end t
))
1708 ;; doesn't have one.
1710 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
1711 ;; Simple form: "-*- MODENAME -*-". Already handled.
1714 ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
1715 ;; (last ";" is optional).
1717 (if (search-forward "-*-" end t
)
1718 (setq end
(- (point) 3))
1719 (error "-*- not terminated before end of line")))
1720 (while (< (point) end
)
1721 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
1722 (error "Malformed -*- line"))
1723 (goto-char (match-end 0))
1724 ;; There used to be a downcase here,
1725 ;; but the manual didn't say so,
1726 ;; and people want to set var names that aren't all lc.
1727 (let ((key (intern (buffer-substring
1730 (val (save-restriction
1731 (narrow-to-region (point) end
)
1732 (read (current-buffer)))))
1733 ;; It is traditional to ignore
1734 ;; case when checking for `mode' in set-auto-mode,
1735 ;; so we must do that here as well.
1736 ;; That is inconsistent, but we're stuck with it.
1737 ;; The same can be said for `coding' in set-auto-coding.
1738 (or (equal (downcase (symbol-name key
)) "mode")
1739 (equal (downcase (symbol-name key
)) "coding")
1740 (setq result
(cons (cons key val
) result
)))
1741 (skip-chars-forward " \t;")))
1742 (setq result
(nreverse result
))))
1745 (or (eq enable-local-variables t
)
1746 (and enable-local-variables
1747 (save-window-excursion
1749 (switch-to-buffer (current-buffer))
1751 ;; If we fail to switch in the selected window,
1752 ;; it is probably a minibuffer.
1753 ;; So try another window.
1755 (switch-to-buffer-other-window (current-buffer))
1757 (switch-to-buffer-other-frame (current-buffer))))))
1758 (y-or-n-p (format "Set local variables as specified in -*- line of %s? "
1759 (file-name-nondirectory buffer-file-name
)))))))
1760 (let ((enable-local-eval enable-local-eval
))
1762 (hack-one-local-variable (car (car result
)) (cdr (car result
)))
1763 (setq result
(cdr result
))))))))
1765 (defvar hack-local-variables-hook nil
1766 "Normal hook run after processing a file's local variables specs.
1767 Major modes can use this to examine user-specified local variables
1768 in order to initialize other data structure based on them.")
1770 (defun hack-local-variables (&optional mode-only
)
1771 "Parse and put into effect this buffer's local variables spec.
1772 If MODE-ONLY is non-nil, all we do is check whether the major mode
1773 is specified, returning t if it is specified."
1775 (hack-local-variables-prop-line))
1776 ;; Look for "Local variables:" line in last page.
1777 (let (mode-specified
1778 (enable-local-variables
1779 (and local-enable-local-variables enable-local-variables
)))
1781 (goto-char (point-max))
1782 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move
)
1783 (if (let ((case-fold-search t
))
1784 (and (search-forward "Local Variables:" nil t
)
1785 (or (eq enable-local-variables t
)
1787 (and enable-local-variables
1788 (save-window-excursion
1789 (switch-to-buffer (current-buffer))
1792 (set-window-start (selected-window) (point)))
1793 (y-or-n-p (format "Set local variables as specified at end of %s? "
1794 (if buffer-file-name
1795 (file-name-nondirectory
1798 (buffer-name))))))))))
1800 prefix prefixlen suffix beg
1802 (enable-local-eval enable-local-eval
))
1803 ;; The prefix is what comes before "local variables:" in its line.
1804 ;; The suffix is what comes after "local variables:" in its line.
1805 (skip-chars-forward " \t")
1807 (setq suffix
(buffer-substring (point)
1808 (progn (end-of-line) (point)))))
1809 (goto-char (match-beginning 0))
1812 (buffer-substring (point)
1813 (progn (beginning-of-line) (point)))))
1815 (if prefix
(setq prefixlen
(length prefix
)
1816 prefix
(regexp-quote prefix
)))
1817 (if suffix
(setq suffix
(concat (regexp-quote suffix
) "$")))
1819 ;; Look at next local variable spec.
1820 (if selective-display
(re-search-forward "[\n\C-m]")
1822 ;; Skip the prefix, if any.
1824 (if (looking-at prefix
)
1825 (forward-char prefixlen
)
1826 (error "Local variables entry is missing the prefix")))
1827 ;; Find the variable name; strip whitespace.
1828 (skip-chars-forward " \t")
1830 (skip-chars-forward "^:\n")
1831 (if (eolp) (error "Missing colon in local variables entry"))
1832 (skip-chars-backward " \t")
1833 (let* ((str (buffer-substring beg
(point)))
1836 ;; Setting variable named "end" means end of list.
1837 (if (string-equal (downcase str
) "end")
1839 ;; Otherwise read the variable value.
1840 (skip-chars-forward "^:")
1842 (setq val
(read (current-buffer)))
1843 (skip-chars-backward "\n")
1844 (skip-chars-forward " \t")
1845 (or (if suffix
(looking-at suffix
) (eolp))
1846 (error "Local variables entry is terminated incorrectly"))
1849 (setq mode-specified t
))
1850 ;; Set the variable. "Variables" mode and eval are funny.
1851 (hack-one-local-variable var val
))))))))
1853 (run-hooks 'hack-local-variables-hook
))
1856 (defvar ignored-local-variables
1857 '(enable-local-eval)
1858 "Variables to be ignored in a file's local variable spec.")
1860 ;; Get confirmation before setting these variables as locals in a file.
1861 (put 'debugger
'risky-local-variable t
)
1862 (put 'enable-local-eval
'risky-local-variable t
)
1863 (put 'ignored-local-variables
'risky-local-variable t
)
1864 (put 'eval
'risky-local-variable t
)
1865 (put 'file-name-handler-alist
'risky-local-variable t
)
1866 (put 'minor-mode-alist
'risky-local-variable t
)
1867 (put 'minor-mode-map-alist
'risky-local-variable t
)
1868 (put 'minor-mode-overriding-map-alist
'risky-local-variable t
)
1869 (put 'overriding-local-map
'risky-local-variable t
)
1870 (put 'overriding-terminal-local-map
'risky-local-variable t
)
1871 (put 'auto-mode-alist
'risky-local-variable t
)
1872 (put 'after-load-alist
'risky-local-variable t
)
1873 (put 'buffer-file-name
'risky-local-variable t
)
1874 (put 'buffer-undo-list
'risky-local-variable t
)
1875 (put 'buffer-auto-save-file-name
'risky-local-variable t
)
1876 (put 'buffer-file-truename
'risky-local-variable t
)
1877 (put 'default-text-properties
'risky-local-variable t
)
1878 (put 'exec-path
'risky-local-variable t
)
1879 (put 'load-path
'risky-local-variable t
)
1880 (put 'exec-directory
'risky-local-variable t
)
1881 (put 'process-environment
'risky-local-variable t
)
1882 (put 'dabbrev-case-fold-search
'risky-local-variable t
)
1883 (put 'dabbrev-case-replace
'risky-local-variable t
)
1884 ;; Don't wait for outline.el to be loaded, for the sake of outline-minor-mode.
1885 (put 'outline-level
'risky-local-variable t
)
1886 (put 'rmail-output-file-alist
'risky-local-variable t
)
1887 (put 'special-display-buffer-names
'risky-local-variable t
)
1888 (put 'frame-title-format
'risky-local-variable t
)
1889 (put 'global-mode-string
'risky-local-variable t
)
1890 (put 'header-line-format
'risky-local-variable t
)
1891 (put 'icon-title-format
'risky-local-variable t
)
1892 (put 'input-method-alist
'risky-local-variable t
)
1893 (put 'format-alist
'risky-local-variable t
)
1894 (put 'vc-mode
'risky-local-variable t
)
1895 (put 'imenu-generic-expression
'risky-local-variable t
)
1896 (put 'imenu-index-alist
'risky-local-variable t
)
1897 (put 'standard-input
'risky-local-variable t
)
1898 (put 'standard-output
'risky-local-variable t
)
1899 (put 'unread-command-events
'risky-local-variable t
)
1900 (put 'max-lisp-eval-depth
'risky-local-variable t
)
1901 (put 'max-specpdl-size
'risky-local-variable t
)
1902 (put 'mode-line-format
'risky-local-variable t
)
1903 (put 'mode-line-modified
'risky-local-variable t
)
1904 (put 'mode-line-mule-info
'risky-local-variable t
)
1905 (put 'mode-line-buffer-identification
'risky-local-variable t
)
1906 (put 'mode-line-modes
'risky-local-variable t
)
1907 (put 'mode-line-position
'risky-local-variable t
)
1909 ;; This case is safe because the user gets to check it before it is used.
1910 (put 'compile-command
'safe-local-variable
'stringp
)
1912 (defun risky-local-variable-p (sym val
)
1913 "Non-nil if SYM could be dangerous as a file-local variable with value VAL."
1914 (let ((safep (get sym
'safe-local-variable
)))
1915 (or (memq sym ignored-local-variables
)
1916 (get sym
'risky-local-variable
)
1917 (and (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$\\|-predicate$\\|font-lock-keywords$\\|font-lock-keywords-[0-9]+$\\|font-lock-syntactic-keywords$\\|-frame-alist$\\|-mode-alist$\\|-map$\\|-map-alist$"
1920 ;; If the safe-local-variable property isn't t or nil,
1921 ;; then it must return non-nil on the proposed value to be safe.
1922 (and (not (memq safep
'(t nil
)))
1923 (not (funcall safep val
))))))
1925 (defun hack-one-local-variable-quotep (exp)
1926 (and (consp exp
) (eq (car exp
) 'quote
) (consp (cdr exp
))))
1928 (defun hack-one-local-variable (var val
)
1929 "\"Set\" one variable in a local variables spec.
1930 A few patterns are specified so that any name which matches one
1931 is considered risky."
1932 (cond ((eq var
'mode
)
1933 (funcall (intern (concat (downcase (symbol-name val
))
1936 ;; We have already handled coding: tag in set-auto-coding.
1938 ((memq var ignored-local-variables
)
1940 ;; "Setting" eval means either eval it or do nothing.
1941 ;; Likewise for setting hook variables.
1942 ((risky-local-variable-p var val
)
1943 ;; Permit evalling a put of a harmless property.
1944 ;; if the args do nothing tricky.
1945 (if (or (and (eq var
'eval
)
1948 (hack-one-local-variable-quotep (nth 1 val
))
1949 (hack-one-local-variable-quotep (nth 2 val
))
1950 ;; Only allow safe values of lisp-indent-hook;
1952 (or (numberp (nth 3 val
))
1953 (equal (nth 3 val
) ''defun
))
1954 (memq (nth 1 (nth 2 val
))
1955 '(lisp-indent-hook)))
1956 ;; Permit eval if not root and user says ok.
1957 (and (not (zerop (user-uid)))
1958 (or (eq enable-local-eval t
)
1959 (and enable-local-eval
1960 (save-window-excursion
1961 (switch-to-buffer (current-buffer))
1964 (set-window-start (selected-window) (point)))
1965 (setq enable-local-eval
1966 (y-or-n-p (format "Process `eval' or hook local variables in %s? "
1967 (if buffer-file-name
1968 (concat "file " (file-name-nondirectory buffer-file-name
))
1969 (concat "buffer " (buffer-name)))))))))))
1971 (save-excursion (eval val
))
1972 (make-local-variable var
)
1974 (message "Ignoring risky spec in the local variables list")))
1975 ;; Ordinary variable, really set it.
1976 (t (make-local-variable var
)
1977 ;; Make sure the string has no text properties.
1978 ;; Some text properties can get evaluated in various ways,
1979 ;; so it is risky to put them on with a local variable list.
1981 (set-text-properties 0 (length val
) nil val
))
1985 (defcustom change-major-mode-with-file-name t
1986 "*Non-nil means \\[write-file] should set the major mode from the file name.
1987 However, the mode will not be changed if
1988 \(1) a local variables list or the `-*-' line specifies a major mode, or
1989 \(2) the current major mode is a \"special\" mode,
1990 \ not suitable for ordinary files, or
1991 \(3) the new file name does not particularly specify any mode."
1993 :group
'editing-basics
)
1995 (defun set-visited-file-name (filename &optional no-query along-with-file
)
1996 "Change name of file visited in current buffer to FILENAME.
1997 The next time the buffer is saved it will go in the newly specified file.
1998 nil or empty string as argument means make buffer not be visiting any file.
1999 Remember to delete the initial contents of the minibuffer
2000 if you wish to pass an empty string as the argument.
2002 The optional second argument NO-QUERY, if non-nil, inhibits asking for
2003 confirmation in the case where another buffer is already visiting FILENAME.
2005 The optional third argument ALONG-WITH-FILE, if non-nil, means that
2006 the old visited file has been renamed to the new name FILENAME."
2007 (interactive "FSet visited file name: ")
2008 (if (buffer-base-buffer)
2009 (error "An indirect buffer cannot visit a file"))
2013 (if (string-equal filename
"")
2015 (expand-file-name filename
))))
2018 (setq truename
(file-truename filename
))
2019 (if find-file-visit-truename
2020 (setq filename truename
))))
2021 (let ((buffer (and filename
(find-buffer-visiting filename
))))
2022 (and buffer
(not (eq buffer
(current-buffer)))
2024 (not (y-or-n-p (message "A buffer is visiting %s; proceed? "
2027 (or (equal filename buffer-file-name
)
2029 (and filename
(lock-buffer filename
))
2031 (setq buffer-file-name filename
)
2032 (if filename
; make buffer name reflect filename.
2033 (let ((new-name (file-name-nondirectory buffer-file-name
)))
2034 (if (string= new-name
"")
2035 (error "Empty file name"))
2036 (if (eq system-type
'vax-vms
)
2037 (setq new-name
(downcase new-name
)))
2038 (setq default-directory
(file-name-directory buffer-file-name
))
2039 (or (string= new-name
(buffer-name))
2040 (rename-buffer new-name t
))))
2041 (setq buffer-backed-up nil
)
2043 (clear-visited-file-modtime))
2044 ;; Abbreviate the file names of the buffer.
2047 (setq buffer-file-truename
(abbreviate-file-name truename
))
2048 (if find-file-visit-truename
2049 (setq buffer-file-name buffer-file-truename
))))
2050 (setq buffer-file-number
2052 (nthcdr 10 (file-attributes buffer-file-name
))
2054 ;; write-file-hooks is normally used for things like ftp-find-file
2055 ;; that visit things that are not local files as if they were files.
2056 ;; Changing to visit an ordinary local file instead should flush the hook.
2057 (kill-local-variable 'write-file-hooks
)
2058 (kill-local-variable 'local-write-file-hooks
)
2059 (kill-local-variable 'revert-buffer-function
)
2060 (kill-local-variable 'backup-inhibited
)
2061 ;; If buffer was read-only because of version control,
2062 ;; that reason is gone now, so make it writable.
2064 (setq buffer-read-only nil
))
2065 (kill-local-variable 'vc-mode
)
2066 ;; Turn off backup files for certain file names.
2067 ;; Since this is a permanent local, the major mode won't eliminate it.
2068 (and buffer-file-name
2069 (not (funcall backup-enable-predicate buffer-file-name
))
2071 (make-local-variable 'backup-inhibited
)
2072 (setq backup-inhibited t
)))
2073 (let ((oauto buffer-auto-save-file-name
))
2074 ;; If auto-save was not already on, turn it on if appropriate.
2075 (if (not buffer-auto-save-file-name
)
2076 (and buffer-file-name auto-save-default
2078 ;; If auto save is on, start using a new name.
2079 ;; We deliberately don't rename or delete the old auto save
2080 ;; for the old visited file name. This is because perhaps
2081 ;; the user wants to save the new state and then compare with the
2082 ;; previous state from the auto save file.
2083 (setq buffer-auto-save-file-name
2084 (make-auto-save-file-name)))
2085 ;; Rename the old auto save file if any.
2086 (and oauto buffer-auto-save-file-name
2087 (file-exists-p oauto
)
2088 (rename-file oauto buffer-auto-save-file-name t
)))
2089 (and buffer-file-name
2090 (not along-with-file
)
2091 (set-buffer-modified-p t
))
2092 ;; Update the major mode, if the file name determines it.
2094 ;; Don't change the mode if it is special.
2095 (or (not change-major-mode-with-file-name
)
2096 (get major-mode
'mode-class
)
2097 ;; Don't change the mode if the local variable list specifies it.
2098 (hack-local-variables t
)
2102 (defun write-file (filename &optional confirm
)
2103 "Write current buffer into file FILENAME.
2104 This makes the buffer visit that file, and marks it as not modified.
2106 If you specify just a directory name as FILENAME, that means to use
2107 the default file name but in that directory. You can also yank
2108 the default file name into the minibuffer to edit it, using M-n.
2110 If the buffer is not already visiting a file, the default file name
2111 for the output file is the buffer name.
2113 If optional second arg CONFIRM is non-nil, this function
2114 asks for confirmation before overwriting an existing file.
2115 Interactively, confirmation is required unless you supply a prefix argument."
2116 ;; (interactive "FWrite file: ")
2118 (list (if buffer-file-name
2119 (read-file-name "Write file: "
2121 (read-file-name "Write file: " default-directory
2123 (file-name-nondirectory (buffer-name))
2126 (not current-prefix-arg
)))
2127 (or (null filename
) (string-equal filename
"")
2129 ;; If arg is just a directory,
2130 ;; use the default file name, but in that directory.
2131 (if (file-directory-p filename
)
2132 (setq filename
(concat (file-name-as-directory filename
)
2133 (file-name-nondirectory
2134 (or buffer-file-name
(buffer-name))))))
2136 (file-exists-p filename
)
2137 (or (y-or-n-p (format "File `%s' exists; overwrite? " filename
))
2138 (error "Canceled")))
2139 (set-visited-file-name filename
(not confirm
))))
2140 (set-buffer-modified-p t
)
2141 ;; Make buffer writable if file is writable.
2142 (and buffer-file-name
2143 (file-writable-p buffer-file-name
)
2144 (setq buffer-read-only nil
))
2147 (defun backup-buffer ()
2148 "Make a backup of the disk file visited by the current buffer, if appropriate.
2149 This is normally done before saving the buffer the first time.
2150 If the value is non-nil, it is the result of `file-modes' on the original
2151 file; this means that the caller, after saving the buffer, should change
2152 the modes of the new file to agree with the old modes.
2154 A backup may be done by renaming or by copying; see documentation of
2155 variable `make-backup-files'. If it's done by renaming, then the file is
2156 no longer accessible under its old name."
2157 (if (and make-backup-files
(not backup-inhibited
)
2158 (not buffer-backed-up
)
2159 (file-exists-p buffer-file-name
)
2160 (memq (aref (elt (file-attributes buffer-file-name
) 8) 0)
2162 (let ((real-file-name buffer-file-name
)
2163 backup-info backupname targets setmodes
)
2164 ;; If specified name is a symbolic link, chase it to the target.
2165 ;; Thus we make the backups in the directory where the real file is.
2166 (setq real-file-name
(file-chase-links real-file-name
))
2167 (setq backup-info
(find-backup-file-name real-file-name
)
2168 backupname
(car backup-info
)
2169 targets
(cdr backup-info
))
2170 ;; (if (file-directory-p buffer-file-name)
2171 ;; (error "Cannot save buffer in directory %s" buffer-file-name))
2174 (let ((delete-old-versions
2175 ;; If have old versions to maybe delete,
2176 ;; ask the user to confirm now, before doing anything.
2177 ;; But don't actually delete til later.
2179 (or (eq delete-old-versions t
) (eq delete-old-versions nil
))
2180 (or delete-old-versions
2181 (y-or-n-p (format "Delete excess backup versions of %s? "
2182 real-file-name
))))))
2183 ;; Actually write the back up file.
2185 (if (or file-precious-flag
2186 ; (file-symlink-p buffer-file-name)
2188 (and backup-by-copying-when-linked
2189 (> (file-nlinks real-file-name
) 1))
2190 (and (or backup-by-copying-when-mismatch
2191 (integerp backup-by-copying-when-privileged-mismatch
))
2192 (let ((attr (file-attributes real-file-name
)))
2193 (and (or backup-by-copying-when-mismatch
2194 (and (integerp (nth 2 attr
))
2195 (integerp backup-by-copying-when-privileged-mismatch
)
2196 (<= (nth 2 attr
) backup-by-copying-when-privileged-mismatch
)))
2198 (not (file-ownership-preserved-p real-file-name
)))))))
2200 (copy-file real-file-name backupname t t
)
2202 ;; If copying fails because file BACKUPNAME
2203 ;; is not writable, delete that file and try again.
2204 (if (and (file-exists-p backupname
)
2205 (not (file-writable-p backupname
)))
2206 (delete-file backupname
))
2207 (copy-file real-file-name backupname t t
)))
2208 ;; rename-file should delete old backup.
2209 (rename-file real-file-name backupname t
)
2210 (setq setmodes
(file-modes backupname
)))
2212 ;; If trouble writing the backup, write it in ~.
2213 (setq backupname
(expand-file-name
2214 (convert-standard-filename
2216 (message "Cannot write backup file; backing up in %s"
2217 (file-name-nondirectory backupname
))
2220 (copy-file real-file-name backupname t t
)
2222 ;; If copying fails because file BACKUPNAME
2223 ;; is not writable, delete that file and try again.
2224 (if (and (file-exists-p backupname
)
2225 (not (file-writable-p backupname
)))
2226 (delete-file backupname
))
2227 (copy-file real-file-name backupname t t
)))))
2228 (setq buffer-backed-up t
)
2229 ;; Now delete the old versions, if desired.
2230 (if delete-old-versions
2233 (delete-file (car targets
))
2235 (setq targets
(cdr targets
))))
2237 (file-error nil
))))))
2239 (defun file-name-sans-versions (name &optional keep-backup-version
)
2240 "Return file NAME sans backup versions or strings.
2241 This is a separate procedure so your site-init or startup file can
2243 If the optional argument KEEP-BACKUP-VERSION is non-nil,
2244 we do not remove backup version numbers, only true file version numbers."
2245 (let ((handler (find-file-name-handler name
'file-name-sans-versions
)))
2247 (funcall handler
'file-name-sans-versions name keep-backup-version
)
2249 (if (eq system-type
'vax-vms
)
2250 ;; VMS version number is (a) semicolon, optional
2251 ;; sign, zero or more digits or (b) period, option
2252 ;; sign, zero or more digits, provided this is the
2253 ;; second period encountered outside of the
2254 ;; device/directory part of the file name.
2255 (or (string-match ";[-+]?[0-9]*\\'" name
)
2256 (if (string-match "\\.[^]>:]*\\(\\.[-+]?[0-9]*\\)\\'"
2258 (match-beginning 1))
2260 (if keep-backup-version
2262 (or (string-match "\\.~[0-9.]+~\\'" name
)
2263 (string-match "~\\'" name
)
2264 (length name
))))))))
2266 (defun file-ownership-preserved-p (file)
2267 "Return t if deleting FILE and rewriting it would preserve the owner."
2268 (let ((handler (find-file-name-handler file
'file-ownership-preserved-p
)))
2270 (funcall handler
'file-ownership-preserved-p file
)
2271 (let ((attributes (file-attributes file
)))
2272 ;; Return t if the file doesn't exist, since it's true that no
2273 ;; information would be lost by an (attempted) delete and create.
2274 (or (null attributes
)
2275 (= (nth 2 attributes
) (user-uid)))))))
2277 (defun file-name-sans-extension (filename)
2278 "Return FILENAME sans final \"extension\".
2279 The extension, in a file name, is the part that follows the last `.'."
2281 (let ((file (file-name-sans-versions (file-name-nondirectory filename
)))
2283 (if (string-match "\\.[^.]*\\'" file
)
2284 (if (setq directory
(file-name-directory filename
))
2285 (expand-file-name (substring file
0 (match-beginning 0))
2287 (substring file
0 (match-beginning 0)))
2290 (defun file-name-extension (filename &optional period
)
2291 "Return FILENAME's final \"extension\".
2292 The extension, in a file name, is the part that follows the last `.'.
2293 Return nil for extensionless file names such as `foo'.
2294 Return the empty string for file names such as `foo.'.
2296 If PERIOD is non-nil, then the returned value includes the period
2297 that delimits the extension, and if FILENAME has no extension,
2300 (let ((file (file-name-sans-versions (file-name-nondirectory filename
))))
2301 (if (string-match "\\.[^.]*\\'" file
)
2302 (substring file
(+ (match-beginning 0) (if period
0 1)))
2306 (defcustom make-backup-file-name-function nil
2307 "A function to use instead of the default `make-backup-file-name'.
2308 A value of nil gives the default `make-backup-file-name' behaviour.
2310 This could be buffer-local to do something special for specific
2311 files. If you define it, you may need to change `backup-file-name-p'
2312 and `file-name-sans-versions' too.
2314 See also `backup-directory-alist'."
2316 :type
'(choice (const :tag
"Default" nil
)
2317 (function :tag
"Your function")))
2319 (defcustom backup-directory-alist nil
2320 "Alist of filename patterns and backup directory names.
2321 Each element looks like (REGEXP . DIRECTORY). Backups of files with
2322 names matching REGEXP will be made in DIRECTORY. DIRECTORY may be
2323 relative or absolute. If it is absolute, so that all matching files
2324 are backed up into the same directory, the file names in this
2325 directory will be the full name of the file backed up with all
2326 directory separators changed to `!' to prevent clashes. This will not
2327 work correctly if your filesystem truncates the resulting name.
2329 For the common case of all backups going into one directory, the alist
2330 should contain a single element pairing \".\" with the appropriate
2333 If this variable is nil, or it fails to match a filename, the backup
2334 is made in the original file's directory.
2336 On MS-DOS filesystems without long names this variable is always
2339 :type
'(repeat (cons (regexp :tag
"Regexp matching filename")
2340 (directory :tag
"Backup directory name"))))
2342 (defun make-backup-file-name (file)
2343 "Create the non-numeric backup file name for FILE.
2344 Normally this will just be the file's name with `~' appended.
2345 Customization hooks are provided as follows.
2347 If the variable `make-backup-file-name-function' is non-nil, its value
2348 should be a function which will be called with FILE as its argument;
2349 the resulting name is used.
2351 Otherwise a match for FILE is sought in `backup-directory-alist'; see
2352 the documentation of that variable. If the directory for the backup
2353 doesn't exist, it is created."
2354 (if make-backup-file-name-function
2355 (funcall make-backup-file-name-function file
)
2356 (if (and (eq system-type
'ms-dos
)
2357 (not (msdos-long-file-names)))
2358 (let ((fn (file-name-nondirectory file
)))
2359 (concat (file-name-directory file
)
2360 (or (and (string-match "\\`[^.]+\\'" fn
)
2361 (concat (match-string 0 fn
) ".~"))
2362 (and (string-match "\\`[^.]+\\.\\(..?\\)?" fn
)
2363 (concat (match-string 0 fn
) "~")))))
2364 (concat (make-backup-file-name-1 file
) "~"))))
2366 (defun make-backup-file-name-1 (file)
2367 "Subroutine of `make-backup-file-name' and `find-backup-file-name'."
2368 (let ((alist backup-directory-alist
)
2369 elt backup-directory dir-sep-string
)
2371 (setq elt
(pop alist
))
2372 (if (string-match (car elt
) file
)
2373 (setq backup-directory
(cdr elt
)
2375 (if (null backup-directory
)
2377 (unless (file-exists-p backup-directory
)
2379 (make-directory backup-directory
'parents
)
2381 (if (file-name-absolute-p backup-directory
)
2383 (when (memq system-type
'(windows-nt ms-dos
))
2384 ;; Normalize DOSish file names: convert all slashes to
2385 ;; directory-sep-char, downcase the drive letter, if any,
2386 ;; and replace the leading "x:" with "/drive_x".
2387 (or (file-name-absolute-p file
)
2388 (setq file
(expand-file-name file
))) ; make defaults explicit
2389 ;; Replace any invalid file-name characters (for the
2390 ;; case of backing up remote files).
2391 (setq file
(expand-file-name (convert-standard-filename file
)))
2392 (setq dir-sep-string
(char-to-string directory-sep-char
))
2393 (if (eq (aref file
1) ?
:)
2394 (setq file
(concat dir-sep-string
2396 (char-to-string (downcase (aref file
0)))
2397 (if (eq (aref file
2) directory-sep-char
)
2400 (substring file
2)))))
2401 ;; Make the name unique by substituting directory
2402 ;; separators. It may not really be worth bothering about
2403 ;; doubling `!'s in the original name...
2405 (subst-char-in-string
2406 directory-sep-char ?
!
2407 (replace-regexp-in-string "!" "!!" file
))
2409 (expand-file-name (file-name-nondirectory file
)
2410 (file-name-as-directory
2411 (expand-file-name backup-directory
2412 (file-name-directory file
))))))))
2414 (defun backup-file-name-p (file)
2415 "Return non-nil if FILE is a backup file name (numeric or not).
2416 This is a separate function so you can redefine it for customization.
2417 You may need to redefine `file-name-sans-versions' as well."
2418 (string-match "~\\'" file
))
2420 (defvar backup-extract-version-start
)
2422 ;; This is used in various files.
2423 ;; The usage of backup-extract-version-start is not very clean,
2424 ;; but I can't see a good alternative, so as of now I am leaving it alone.
2425 (defun backup-extract-version (fn)
2426 "Given the name of a numeric backup file, FN, return the backup number.
2427 Uses the free variable `backup-extract-version-start', whose value should be
2428 the index in the name where the version number begins."
2429 (if (and (string-match "[0-9]+~$" fn backup-extract-version-start
)
2430 (= (match-beginning 0) backup-extract-version-start
))
2431 (string-to-int (substring fn backup-extract-version-start -
1))
2434 ;; I believe there is no need to alter this behavior for VMS;
2435 ;; since backup files are not made on VMS, it should not get called.
2436 (defun find-backup-file-name (fn)
2437 "Find a file name for a backup file FN, and suggestions for deletions.
2438 Value is a list whose car is the name for the backup file
2439 and whose cdr is a list of old versions to consider deleting now.
2440 If the value is nil, don't make a backup.
2441 Uses `backup-directory-alist' in the same way as does
2442 `make-backup-file-name'."
2443 (let ((handler (find-file-name-handler fn
'find-backup-file-name
)))
2444 ;; Run a handler for this function so that ange-ftp can refuse to do it.
2446 (funcall handler
'find-backup-file-name fn
)
2447 (if (or (eq version-control
'never
)
2448 ;; We don't support numbered backups on plain MS-DOS
2449 ;; when long file names are unavailable.
2450 (and (eq system-type
'ms-dos
)
2451 (not (msdos-long-file-names))))
2452 (list (make-backup-file-name fn
))
2453 (let* ((basic-name (make-backup-file-name-1 fn
))
2454 (base-versions (concat (file-name-nondirectory basic-name
)
2456 (backup-extract-version-start (length base-versions
))
2458 (number-to-delete 0)
2459 possibilities deserve-versions-p versions
)
2461 (setq possibilities
(file-name-all-completions
2463 (file-name-directory basic-name
))
2464 versions
(sort (mapcar #'backup-extract-version
2467 high-water-mark
(apply 'max
0 versions
)
2468 deserve-versions-p
(or version-control
2469 (> high-water-mark
0))
2470 number-to-delete
(- (length versions
)
2474 (file-error (setq possibilities nil
)))
2475 (if (not deserve-versions-p
)
2476 (list (make-backup-file-name fn
))
2477 (cons (format "%s.~%d~" basic-name
(1+ high-water-mark
))
2478 (if (and (> number-to-delete
0)
2479 ;; Delete nothing if there is overflow
2480 ;; in the number of versions to keep.
2481 (>= (+ kept-new-versions kept-old-versions -
1) 0))
2483 (format "%s.~%d~" basic-name n
))
2484 (let ((v (nthcdr kept-old-versions versions
)))
2485 (rplacd (nthcdr (1- number-to-delete
) v
) ())
2488 (defun file-nlinks (filename)
2489 "Return number of names file FILENAME has."
2490 (car (cdr (file-attributes filename
))))
2492 (defun file-relative-name (filename &optional directory
)
2493 "Convert FILENAME to be relative to DIRECTORY (default: `default-directory').
2494 This function returns a relative file name which is equivalent to FILENAME
2495 when used with that default directory as the default.
2496 If this is impossible (which can happen on MSDOS and Windows
2497 when the file name and directory use different drive names)
2498 then it returns FILENAME."
2500 (let ((fname (expand-file-name filename
)))
2501 (setq directory
(file-name-as-directory
2502 (expand-file-name (or directory default-directory
))))
2503 ;; On Microsoft OSes, if FILENAME and DIRECTORY have different
2504 ;; drive names, they can't be relative, so return the absolute name.
2505 (if (and (or (eq system-type
'ms-dos
)
2506 (eq system-type
'windows-nt
))
2507 (not (string-equal (substring fname
0 2)
2508 (substring directory
0 2))))
2510 (let ((ancestor ".")
2511 (fname-dir (file-name-as-directory fname
)))
2512 (while (and (not (string-match (concat "^" (regexp-quote directory
)) fname-dir
))
2513 (not (string-match (concat "^" (regexp-quote directory
)) fname
)))
2514 (setq directory
(file-name-directory (substring directory
0 -
1))
2515 ancestor
(if (equal ancestor
".")
2517 (concat "../" ancestor
))))
2518 ;; Now ancestor is empty, or .., or ../.., etc.
2519 (if (string-match (concat "^" (regexp-quote directory
)) fname
)
2520 ;; We matched within FNAME's directory part.
2521 ;; Add the rest of FNAME onto ANCESTOR.
2522 (let ((rest (substring fname
(match-end 0))))
2523 (if (and (equal ancestor
".")
2524 (not (equal rest
"")))
2525 ;; But don't bother with ANCESTOR if it would give us `./'.
2527 (concat (file-name-as-directory ancestor
) rest
)))
2528 ;; We matched FNAME's directory equivalent.
2531 (defun save-buffer (&optional args
)
2532 "Save current buffer in visited file if modified. Versions described below.
2533 By default, makes the previous version into a backup file
2534 if previously requested or if this is the first save.
2535 With 1 \\[universal-argument], marks this version
2536 to become a backup when the next save is done.
2537 With 2 \\[universal-argument]'s,
2538 unconditionally makes the previous version into a backup file.
2539 With 3 \\[universal-argument]'s, marks this version
2540 to become a backup when the next save is done,
2541 and unconditionally makes the previous version into a backup file.
2543 With argument of 0, never make the previous version into a backup file.
2545 If a file's name is FOO, the names of its numbered backup versions are
2546 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~.
2547 Numeric backups (rather than FOO~) will be made if value of
2548 `version-control' is not the atom `never' and either there are already
2549 numeric versions of the file being backed up, or `version-control' is
2551 We don't want excessive versions piling up, so there are variables
2552 `kept-old-versions', which tells Emacs how many oldest versions to keep,
2553 and `kept-new-versions', which tells how many newest versions to keep.
2554 Defaults are 2 old versions and 2 new.
2555 `dired-kept-versions' controls dired's clean-directory (.) command.
2556 If `delete-old-versions' is nil, system will query user
2557 before trimming versions. Otherwise it does it silently.
2559 If `vc-make-backup-files' is nil, which is the default,
2560 no backup files are made for files managed by version control.
2561 (This is because the version control system itself records previous versions.)
2563 See the subroutine `basic-save-buffer' for more information."
2565 (let ((modp (buffer-modified-p))
2566 (large (> (buffer-size) 50000))
2567 (make-backup-files (or (and make-backup-files
(not (eq args
0)))
2568 (memq args
'(16 64)))))
2569 (and modp
(memq args
'(16 64)) (setq buffer-backed-up nil
))
2570 (if (and modp large
(buffer-file-name))
2571 (message "Saving file %s..." (buffer-file-name)))
2573 (and modp
(memq args
'(4 64)) (setq buffer-backed-up nil
))))
2575 (defun delete-auto-save-file-if-necessary (&optional force
)
2576 "Delete auto-save file for current buffer if `delete-auto-save-files' is t.
2577 Normally delete only if the file was written by this Emacs since
2578 the last real save, but optional arg FORCE non-nil means delete anyway."
2579 (and buffer-auto-save-file-name delete-auto-save-files
2580 (not (string= buffer-file-name buffer-auto-save-file-name
))
2581 (or force
(recent-auto-save-p))
2584 (delete-file buffer-auto-save-file-name
)
2586 (set-buffer-auto-saved))))
2588 (defvar auto-save-hook nil
2589 "Normal hook run just before auto-saving.")
2591 (defcustom after-save-hook nil
2592 "Normal hook that is run after a buffer is saved to its file."
2593 :options
'(executable-make-buffer-file-executable-if-script-p)
2597 (defvar save-buffer-coding-system nil
2598 "If non-nil, use this coding system for saving the buffer.
2599 More precisely, use this coding system in place of the
2600 value of `buffer-file-coding-system', when saving the buffer.
2601 Calling `write-region' for any purpose other than saving the buffer
2602 will still use `buffer-file-coding-system'; this variable has no effect
2605 (make-variable-buffer-local 'save-buffer-coding-system
)
2606 (put 'save-buffer-coding-system
'permanent-local t
)
2608 (defun basic-save-buffer ()
2609 "Save the current buffer in its visited file, if it has been modified.
2610 The hooks `write-contents-hooks', `local-write-file-hooks' and
2611 `write-file-hooks' get a chance to do the job of saving; if they do not,
2612 then the buffer is saved in the visited file file in the usual way.
2613 After saving the buffer, this function runs `after-save-hook'."
2615 (save-current-buffer
2616 ;; In an indirect buffer, save its base buffer instead.
2617 (if (buffer-base-buffer)
2618 (set-buffer (buffer-base-buffer)))
2619 (if (buffer-modified-p)
2620 (let ((recent-save (recent-auto-save-p))
2621 setmodes tempsetmodes
)
2622 ;; On VMS, rename file and buffer to get rid of version number.
2623 (if (and (eq system-type
'vax-vms
)
2624 (not (string= buffer-file-name
2625 (file-name-sans-versions buffer-file-name
))))
2626 (let (buffer-new-name)
2627 ;; Strip VMS version number before save.
2628 (setq buffer-file-name
2629 (file-name-sans-versions buffer-file-name
))
2630 ;; Construct a (unique) buffer name to correspond.
2631 (let ((buf (create-file-buffer (downcase buffer-file-name
))))
2632 (setq buffer-new-name
(buffer-name buf
))
2634 (rename-buffer buffer-new-name
)))
2635 ;; If buffer has no file name, ask user for one.
2636 (or buffer-file-name
2639 (read-file-name "File to save in: ") nil
)))
2640 (and (file-exists-p filename
)
2641 (or (y-or-n-p (format "File `%s' exists; overwrite? "
2643 (error "Canceled")))
2644 (set-visited-file-name filename
)))
2645 (or (verify-visited-file-modtime (current-buffer))
2646 (not (file-exists-p buffer-file-name
))
2648 (format "%s has changed since visited or saved. Save anyway? "
2649 (file-name-nondirectory buffer-file-name
)))
2650 (error "Save not confirmed"))
2654 (and (> (point-max) 1)
2655 (not find-file-literally
)
2656 (/= (char-after (1- (point-max))) ?
\n)
2657 (not (and (eq selective-display t
)
2658 (= (char-after (1- (point-max))) ?
\r)))
2659 (or (eq require-final-newline t
)
2660 (and require-final-newline
2662 (format "Buffer %s does not end in newline. Add one? "
2665 (goto-char (point-max))
2667 ;; Support VC version backups.
2669 (or (run-hook-with-args-until-success 'write-contents-hooks
)
2670 (run-hook-with-args-until-success 'local-write-file-hooks
)
2671 (run-hook-with-args-until-success 'write-file-hooks
)
2672 ;; If a hook returned t, file is already "written".
2673 ;; Otherwise, write it the usual way now.
2674 (setq setmodes
(basic-save-buffer-1)))
2675 ;; Now we have saved the current buffer. Let's make sure
2676 ;; that buffer-file-coding-system is fixed to what
2677 ;; actually used for saving by binding it locally.
2678 (if save-buffer-coding-system
2679 (setq save-buffer-coding-system last-coding-system-used
)
2680 (setq buffer-file-coding-system last-coding-system-used
))
2681 (setq buffer-file-number
2682 (nthcdr 10 (file-attributes buffer-file-name
)))
2685 (set-file-modes buffer-file-name setmodes
)
2687 ;; If the auto-save file was recent before this command,
2689 (delete-auto-save-file-if-necessary recent-save
)
2690 ;; Support VC `implicit' locking.
2692 (run-hooks 'after-save-hook
))
2693 (message "(No changes need to be saved)"))))
2695 ;; This does the "real job" of writing a buffer into its visited file
2696 ;; and making a backup file. This is what is normally done
2697 ;; but inhibited if one of write-file-hooks returns non-nil.
2698 ;; It returns a value to store in setmodes.
2699 (defun basic-save-buffer-1 ()
2700 (if save-buffer-coding-system
2701 (let ((coding-system-for-write save-buffer-coding-system
))
2702 (basic-save-buffer-2))
2703 (basic-save-buffer-2)))
2705 (defun basic-save-buffer-2 ()
2706 (let (tempsetmodes setmodes
)
2707 (if (not (file-writable-p buffer-file-name
))
2708 (let ((dir (file-name-directory buffer-file-name
)))
2709 (if (not (file-directory-p dir
))
2710 (if (file-exists-p dir
)
2711 (error "%s is not a directory" dir
)
2712 (error "%s: no such directory" buffer-file-name
))
2713 (if (not (file-exists-p buffer-file-name
))
2714 (error "Directory %s write-protected" dir
)
2716 (format "File %s is write-protected; try to save anyway? "
2717 (file-name-nondirectory
2719 (setq tempsetmodes t
)
2720 (error "Attempt to save to a file which you aren't allowed to write"))))))
2721 (or buffer-backed-up
2722 (setq setmodes
(backup-buffer)))
2723 (let ((dir (file-name-directory buffer-file-name
)))
2724 (if (and file-precious-flag
2725 (file-writable-p dir
))
2726 ;; If file is precious, write temp name, then rename it.
2727 ;; This requires write access to the containing dir,
2728 ;; which is why we don't try it if we don't have that access.
2729 (let ((realname buffer-file-name
)
2730 tempname temp nogood i succeed
2731 (old-modtime (visited-file-modtime)))
2734 ;; Find the temporary name to write under.
2736 (setq tempname
(format
2737 (if (and (eq system-type
'ms-dos
)
2738 (not (msdos-long-file-names)))
2739 "%s#%d.tm#" ; MSDOS limits files to 8+3
2740 (if (memq system-type
'(vax-vms axp-vms
))
2744 (setq nogood
(file-exists-p tempname
))
2747 (progn (clear-visited-file-modtime)
2748 (write-region (point-min) (point-max)
2749 tempname nil realname
2750 buffer-file-truename
)
2752 ;; If writing the temp file fails,
2753 ;; delete the temp file.
2757 (delete-file tempname
)
2759 (set-visited-file-modtime old-modtime
))))
2760 ;; Since we have created an entirely new file
2761 ;; and renamed it, make sure it gets the
2762 ;; right permission bits set.
2763 (setq setmodes
(file-modes buffer-file-name
))
2764 ;; We succeeded in writing the temp file,
2766 (rename-file tempname buffer-file-name t
))
2767 ;; If file not writable, see if we can make it writable
2768 ;; temporarily while we write it.
2769 ;; But no need to do so if we have just backed it up
2770 ;; (setmodes is set) because that says we're superseding.
2771 (cond ((and tempsetmodes
(not setmodes
))
2772 ;; Change the mode back, after writing.
2773 (setq setmodes
(file-modes buffer-file-name
))
2774 (set-file-modes buffer-file-name
(logior setmodes
128))))
2775 (write-region (point-min) (point-max)
2776 buffer-file-name nil t buffer-file-truename
)))
2779 (defun save-some-buffers (&optional arg pred
)
2780 "Save some modified file-visiting buffers. Asks user about each one.
2781 Optional argument (the prefix) non-nil means save all with no questions.
2782 Optional second argument PRED determines which buffers are considered:
2783 If PRED is nil, all the file-visiting buffers are considered.
2784 If PRED is t, then certain non-file buffers will also be considered.
2785 If PRED is a zero-argument function, it indicates for each buffer whether
2786 to consider it or not when called with that buffer current."
2788 (save-window-excursion
2789 (let* ((queried nil
)
2794 (and (buffer-modified-p buffer
)
2795 (not (buffer-base-buffer buffer
))
2797 (buffer-file-name buffer
)
2801 (and buffer-offer-save
(> (buffer-size) 0)))))
2802 (or (not (functionp pred
))
2803 (with-current-buffer buffer
(funcall pred
)))
2807 (if (buffer-file-name buffer
)
2808 (format "Save file %s? "
2809 (buffer-file-name buffer
))
2810 (format "Save buffer %s? "
2811 (buffer-name buffer
)))))))
2817 '("buffer" "buffers" "save")
2818 (list (list ?\C-r
(lambda (buf)
2822 (exit-recursive-edit))))
2824 ;; Return nil to ask about BUF again.
2826 "display the current buffer"))))
2828 (and save-abbrevs abbrevs-changed
2831 (eq save-abbrevs
'silently
)
2832 (y-or-n-p (format "Save abbrevs in %s? "
2834 (write-abbrev-file nil
))
2835 ;; Don't keep bothering user if he says no.
2836 (setq abbrevs-changed nil
)
2838 (or queried
(> files-done
0) abbrevs-done
2839 (message "(No files need saving)")))))
2841 (defun not-modified (&optional arg
)
2842 "Mark current buffer as unmodified, not needing to be saved.
2843 With prefix arg, mark buffer as modified, so \\[save-buffer] will save.
2845 It is not a good idea to use this function in Lisp programs, because it
2846 prints a message in the minibuffer. Instead, use `set-buffer-modified-p'."
2848 (message (if arg
"Modification-flag set"
2849 "Modification-flag cleared"))
2850 (set-buffer-modified-p arg
))
2852 (defun toggle-read-only (&optional arg
)
2853 "Change whether this buffer is visiting its file read-only.
2854 With arg, set read-only iff arg is positive.
2855 If visiting file read-only and `view-read-only' is non-nil, enter view mode."
2858 ((and arg
(if (> (prefix-numeric-value arg
) 0) buffer-read-only
2859 (not buffer-read-only
))) ; If buffer-read-only is set correctly,
2862 ((and buffer-read-only view-mode
)
2863 (View-exit-and-edit)
2864 (make-local-variable 'view-read-only
)
2865 (setq view-read-only t
)) ; Must leave view mode.
2866 ((and (not buffer-read-only
) view-read-only
2867 (not (eq (get major-mode
'mode-class
) 'special
)))
2869 (t (setq buffer-read-only
(not buffer-read-only
))
2870 (force-mode-line-update))))
2872 (defun insert-file (filename)
2873 "Insert contents of file FILENAME into buffer after point.
2874 Set mark after the inserted text.
2876 This function is meant for the user to run interactively.
2877 Don't call it from programs! Use `insert-file-contents' instead.
2878 \(Its calling sequence is different; see its documentation)."
2879 (interactive "*fInsert file: ")
2880 (if (file-directory-p filename
)
2881 (signal 'file-error
(list "Opening input file" "file is a directory"
2883 (let ((tem (insert-file-contents filename
)))
2884 (push-mark (+ (point) (car (cdr tem
))))))
2886 (defun append-to-file (start end filename
)
2887 "Append the contents of the region to the end of file FILENAME.
2888 When called from a function, expects three arguments,
2889 START, END and FILENAME. START and END are buffer positions
2890 saying what text to write."
2891 (interactive "r\nFAppend to file: ")
2892 (write-region start end filename t
))
2894 (defun file-newest-backup (filename)
2895 "Return most recent backup file for FILENAME or nil if no backups exist."
2896 ;; `make-backup-file-name' will get us the right directory for
2897 ;; ordinary or numeric backups. It might create a directory for
2898 ;; backups as a side-effect, according to `backup-directory-alist'.
2899 (let* ((filename (file-name-sans-versions
2900 (make-backup-file-name filename
)))
2901 (file (file-name-nondirectory filename
))
2902 (dir (file-name-directory filename
))
2903 (comp (file-name-all-completions file dir
))
2907 (setq tem
(pop comp
))
2908 (cond ((and (backup-file-name-p tem
)
2909 (string= (file-name-sans-versions tem
) file
))
2910 (setq tem
(concat dir tem
))
2911 (if (or (null newest
)
2912 (file-newer-than-file-p tem newest
))
2913 (setq newest tem
)))))
2916 (defun rename-uniquely ()
2917 "Rename current buffer to a similar name not already taken.
2918 This function is useful for creating multiple shell process buffers
2919 or multiple mail buffers, etc."
2922 (let ((base-name (buffer-name)))
2923 (and (string-match "<[0-9]+>\\'" base-name
)
2924 (not (and buffer-file-name
2926 (file-name-nondirectory buffer-file-name
))))
2927 ;; If the existing buffer name has a <NNN>,
2928 ;; which isn't part of the file name (if any),
2929 ;; then get rid of that.
2930 (setq base-name
(substring base-name
0 (match-beginning 0))))
2931 (rename-buffer (generate-new-buffer-name base-name
))
2932 (force-mode-line-update))))
2934 (defun make-directory (dir &optional parents
)
2935 "Create the directory DIR and any nonexistent parent dirs.
2936 Interactively, the default choice of directory to create
2937 is the current default directory for file names.
2938 That is useful when you have visited a file in a nonexistent directory.
2940 Noninteractively, the second (optional) argument PARENTS says whether
2941 to create parent directories if they don't exist."
2943 (list (read-file-name "Make directory: " default-directory default-directory
2946 (let ((handler (find-file-name-handler dir
'make-directory
)))
2948 (funcall handler
'make-directory dir parents
)
2950 (make-directory-internal dir
)
2951 (let ((dir (directory-file-name (expand-file-name dir
)))
2953 (while (not (file-exists-p dir
))
2954 (setq create-list
(cons dir create-list
)
2955 dir
(directory-file-name (file-name-directory dir
))))
2957 (make-directory-internal (car create-list
))
2958 (setq create-list
(cdr create-list
))))))))
2960 (put 'revert-buffer-function
'permanent-local t
)
2961 (defvar revert-buffer-function nil
2962 "Function to use to revert this buffer, or nil to do the default.
2963 The function receives two arguments IGNORE-AUTO and NOCONFIRM,
2964 which are the arguments that `revert-buffer' received.")
2966 (put 'revert-buffer-insert-file-contents-function
'permanent-local t
)
2967 (defvar revert-buffer-insert-file-contents-function nil
2968 "Function to use to insert contents when reverting this buffer.
2969 Gets two args, first the nominal file name to use,
2970 and second, t if reading the auto-save file.
2972 The function you specify is responsible for updating (or preserving) point.")
2974 (defvar before-revert-hook nil
2975 "Normal hook for `revert-buffer' to run before reverting.
2976 If `revert-buffer-function' is used to override the normal revert
2977 mechanism, this hook is not used.")
2979 (defvar after-revert-hook nil
2980 "Normal hook for `revert-buffer' to run after reverting.
2981 Note that the hook value that it runs is the value that was in effect
2982 before reverting; that makes a difference if you have buffer-local
2985 If `revert-buffer-function' is used to override the normal revert
2986 mechanism, this hook is not used.")
2988 (defvar revert-buffer-internal-hook
)
2990 (defun revert-buffer (&optional ignore-auto noconfirm preserve-modes
)
2991 "Replace current buffer text with the text of the visited file on disk.
2992 This undoes all changes since the file was visited or saved.
2993 With a prefix argument, offer to revert from latest auto-save file, if
2994 that is more recent than the visited file.
2996 This command also works for special buffers that contain text which
2997 doesn't come from a file, but reflects some other data base instead:
2998 for example, Dired buffers and buffer-list buffers. In these cases,
2999 it reconstructs the buffer contents from the appropriate data base.
3001 When called from Lisp, the first argument is IGNORE-AUTO; only offer
3002 to revert from the auto-save file when this is nil. Note that the
3003 sense of this argument is the reverse of the prefix argument, for the
3004 sake of backward compatibility. IGNORE-AUTO is optional, defaulting
3007 Optional second argument NOCONFIRM means don't ask for confirmation at
3008 all. (The local variable `revert-without-query', if non-nil, prevents
3011 Optional third argument PRESERVE-MODES non-nil means don't alter
3012 the files modes. Normally we reinitialize them using `normal-mode'.
3014 If the value of `revert-buffer-function' is non-nil, it is called to
3015 do all the work for this command. Otherwise, the hooks
3016 `before-revert-hook' and `after-revert-hook' are run at the beginning
3017 and the end, and if `revert-buffer-insert-file-contents-function' is
3018 non-nil, it is called instead of rereading visited file contents."
3020 ;; I admit it's odd to reverse the sense of the prefix argument, but
3021 ;; there is a lot of code out there which assumes that the first
3022 ;; argument should be t to avoid consulting the auto-save file, and
3023 ;; there's no straightforward way to encourage authors to notice a
3024 ;; reversal of the argument sense. So I'm just changing the user
3025 ;; interface, but leaving the programmatic interface the same.
3026 (interactive (list (not current-prefix-arg
)))
3027 (if revert-buffer-function
3028 (funcall revert-buffer-function ignore-auto noconfirm
)
3029 (let* ((auto-save-p (and (not ignore-auto
)
3030 (recent-auto-save-p)
3031 buffer-auto-save-file-name
3032 (file-readable-p buffer-auto-save-file-name
)
3034 "Buffer has been auto-saved recently. Revert from auto-save file? ")))
3035 (file-name (if auto-save-p
3036 buffer-auto-save-file-name
3038 (cond ((null file-name
)
3039 (error "Buffer does not seem to be associated with any file"))
3041 (and (not (buffer-modified-p))
3042 (let ((tail revert-without-query
)
3045 (if (string-match (car tail
) file-name
)
3047 (setq tail
(cdr tail
)))
3049 (yes-or-no-p (format "Revert buffer from file %s? "
3051 (run-hooks 'before-revert-hook
)
3052 ;; If file was backed up but has changed since,
3053 ;; we shd make another backup.
3054 (and (not auto-save-p
)
3055 (not (verify-visited-file-modtime (current-buffer)))
3056 (setq buffer-backed-up nil
))
3057 ;; Get rid of all undo records for this buffer.
3058 (or (eq buffer-undo-list t
)
3059 (setq buffer-undo-list nil
))
3060 ;; Effectively copy the after-revert-hook status,
3061 ;; since after-find-file will clobber it.
3062 (let ((global-hook (default-value 'after-revert-hook
))
3063 (local-hook-p (local-variable-p 'after-revert-hook
))
3064 (local-hook (and (local-variable-p 'after-revert-hook
)
3065 after-revert-hook
)))
3066 (let (buffer-read-only
3067 ;; Don't make undo records for the reversion.
3068 (buffer-undo-list t
))
3069 (if revert-buffer-insert-file-contents-function
3070 (funcall revert-buffer-insert-file-contents-function
3071 file-name auto-save-p
)
3072 (if (not (file-exists-p file-name
))
3073 (error "File %s no longer exists!" file-name
))
3074 ;; Bind buffer-file-name to nil
3075 ;; so that we don't try to lock the file.
3076 (let ((buffer-file-name nil
))
3080 (let ((coding-system-for-read
3081 ;; Auto-saved file shoule be read without
3082 ;; any code conversion.
3083 (if auto-save-p
'emacs-mule-unix
3084 coding-system-for-read
)))
3085 ;; Note that this preserves point in an intelligent way.
3086 (insert-file-contents file-name
(not auto-save-p
)
3088 ;; Recompute the truename in case changes in symlinks
3089 ;; have changed the truename.
3090 (setq buffer-file-truename
3091 (abbreviate-file-name (file-truename buffer-file-name
)))
3092 (after-find-file nil nil t t preserve-modes
)
3093 ;; Run after-revert-hook as it was before we reverted.
3094 (setq-default revert-buffer-internal-hook global-hook
)
3097 (make-local-variable 'revert-buffer-internal-hook
)
3098 (setq revert-buffer-internal-hook local-hook
))
3099 (kill-local-variable 'revert-buffer-internal-hook
))
3100 (run-hooks 'revert-buffer-internal-hook
))
3103 (defun recover-file (file)
3104 "Visit file FILE, but get contents from its last auto-save file."
3105 ;; Actually putting the file name in the minibuffer should be used
3107 ;; Not just because users often use the default.
3108 (interactive "FRecover file: ")
3109 (setq file
(expand-file-name file
))
3110 (if (auto-save-file-name-p (file-name-nondirectory file
))
3111 (error "%s is an auto-save file" file
))
3112 (let ((file-name (let ((buffer-file-name file
))
3113 (make-auto-save-file-name))))
3114 (cond ((if (file-exists-p file
)
3115 (not (file-newer-than-file-p file-name file
))
3116 (not (file-exists-p file-name
)))
3117 (error "Auto-save file %s not current" file-name
))
3118 ((save-window-excursion
3119 (with-output-to-temp-buffer "*Directory*"
3120 (buffer-disable-undo standard-output
)
3122 (let ((switches dired-listing-switches
))
3123 (if (file-symlink-p file
)
3124 (setq switches
(concat switches
"L")))
3125 (set-buffer standard-output
)
3126 ;; Use insert-directory-safely, not insert-directory,
3127 ;; because these files might not exist. In particular,
3128 ;; FILE might not exist if the auto-save file was for
3129 ;; a buffer that didn't visit a file, such as "*mail*".
3130 ;; The code in v20.x called `ls' directly, so we need
3131 ;; to emulate what `ls' did in that case.
3132 (insert-directory-safely file switches
)
3133 (insert-directory-safely file-name switches
))))
3134 (yes-or-no-p (format "Recover auto save file %s? " file-name
)))
3135 (switch-to-buffer (find-file-noselect file t
))
3136 (let ((buffer-read-only nil
)
3137 ;; Keep the current buffer-file-coding-system.
3138 (coding-system buffer-file-coding-system
)
3139 ;; Auto-saved file shoule be read without any code conversion.
3140 (coding-system-for-read 'emacs-mule-unix
))
3142 (insert-file-contents file-name nil
)
3143 (set-buffer-file-coding-system coding-system
))
3144 (after-find-file nil nil t
))
3145 (t (error "Recover-file cancelled")))))
3147 (defun recover-session ()
3148 "Recover auto save files from a previous Emacs session.
3149 This command first displays a Dired buffer showing you the
3150 previous sessions that you could recover from.
3151 To choose one, move point to the proper line and then type C-c C-c.
3152 Then you'll be asked about a number of files to recover."
3154 (if (null auto-save-list-file-prefix
)
3155 (error "You set `auto-save-list-file-prefix' to disable making session files"))
3156 (let ((dir (file-name-directory auto-save-list-file-prefix
)))
3157 (unless (file-directory-p dir
)
3158 (make-directory dir t
)))
3159 (let ((ls-lisp-support-shell-wildcards t
))
3160 (dired (concat auto-save-list-file-prefix
"*")
3161 (concat dired-listing-switches
"t")))
3163 (goto-char (point-min))
3164 (or (looking-at " Move to the session you want to recover,")
3165 (let ((inhibit-read-only t
))
3166 ;; Each line starts with a space
3167 ;; so that Font Lock mode won't highlight the first character.
3168 (insert " Move to the session you want to recover,\n"
3169 " then type C-c C-c to select it.\n\n"
3170 " You can also delete some of these files;\n"
3171 " type d on a line to mark that file for deletion.\n\n"))))
3172 (use-local-map (nconc (make-sparse-keymap) (current-local-map)))
3173 (define-key (current-local-map) "\C-c\C-c" 'recover-session-finish
))
3175 (defun recover-session-finish ()
3176 "Choose one saved session to recover auto-save files from.
3177 This command is used in the special Dired buffer created by
3178 \\[recover-session]."
3180 ;; Get the name of the session file to recover from.
3181 (let ((file (dired-get-filename))
3183 (buffer (get-buffer-create " *recover*")))
3185 (dired-do-flagged-delete t
)
3188 ;; Read in the auto-save-list file.
3191 (insert-file-contents file
)
3192 ;; Loop thru the text of that file
3193 ;; and get out the names of the files to recover.
3195 (let (thisfile autofile
)
3197 ;; This is a pair of lines for a non-file-visiting buffer.
3198 ;; Get the auto-save file name and manufacture
3199 ;; a "visited file name" from that.
3203 (buffer-substring-no-properties
3211 (file-name-nondirectory autofile
)
3213 (file-name-directory autofile
)))
3215 ;; This pair of lines is a file-visiting
3216 ;; buffer. Use the visited file name.
3219 (buffer-substring-no-properties
3220 (point) (progn (end-of-line) (point))))
3223 (buffer-substring-no-properties
3224 (point) (progn (end-of-line) (point))))
3226 ;; Ignore a file if its auto-save file does not exist now.
3227 (if (file-exists-p autofile
)
3228 (setq files
(cons thisfile files
)))))
3229 (setq files
(nreverse files
))
3230 ;; The file contains a pair of line for each auto-saved buffer.
3231 ;; The first line of the pair contains the visited file name
3232 ;; or is empty if the buffer was not visiting a file.
3233 ;; The second line is the auto-save file name.
3235 (map-y-or-n-p "Recover %s? "
3238 (save-excursion (recover-file file
))
3240 "Failed to recover `%s'" file
)))
3242 '("file" "files" "recover"))
3243 (message "No files can be recovered from this session now")))
3244 (kill-buffer buffer
))))
3246 (defun kill-some-buffers (&optional list
)
3247 "For each buffer in LIST, ask whether to kill it.
3248 LIST defaults to all existing live buffers."
3251 (setq list
(buffer-list)))
3253 (let* ((buffer (car list
))
3254 (name (buffer-name buffer
)))
3255 (and (not (string-equal name
""))
3256 (/= (aref name
0) ?
)
3258 (format "Buffer %s %s. Kill? "
3260 (if (buffer-modified-p buffer
)
3261 "HAS BEEN EDITED" "is unmodified")))
3262 (kill-buffer buffer
)))
3263 (setq list
(cdr list
))))
3265 (defun auto-save-mode (arg)
3266 "Toggle auto-saving of contents of current buffer.
3267 With prefix argument ARG, turn auto-saving on if positive, else off."
3269 (setq buffer-auto-save-file-name
3271 (or (not buffer-auto-save-file-name
)
3272 ;; If auto-save is off because buffer has shrunk,
3273 ;; then toggling should turn it on.
3274 (< buffer-saved-size
0))
3275 (or (eq arg t
) (listp arg
) (and (integerp arg
) (> arg
0))))
3276 (if (and buffer-file-name auto-save-visited-file-name
3277 (not buffer-read-only
))
3279 (make-auto-save-file-name))))
3280 ;; If -1 was stored here, to temporarily turn off saving,
3282 (and (< buffer-saved-size
0)
3283 (setq buffer-saved-size
0))
3285 (message "Auto-save %s (in this buffer)"
3286 (if buffer-auto-save-file-name
"on" "off")))
3287 buffer-auto-save-file-name
)
3289 (defun rename-auto-save-file ()
3290 "Adjust current buffer's auto save file name for current conditions.
3291 Also rename any existing auto save file, if it was made in this session."
3292 (let ((osave buffer-auto-save-file-name
))
3293 (setq buffer-auto-save-file-name
3294 (make-auto-save-file-name))
3295 (if (and osave buffer-auto-save-file-name
3296 (not (string= buffer-auto-save-file-name buffer-file-name
))
3297 (not (string= buffer-auto-save-file-name osave
))
3298 (file-exists-p osave
)
3299 (recent-auto-save-p))
3300 (rename-file osave buffer-auto-save-file-name t
))))
3302 (defun make-auto-save-file-name ()
3303 "Return file name to use for auto-saves of current buffer.
3304 Does not consider `auto-save-visited-file-name' as that variable is checked
3305 before calling this function. You can redefine this for customization.
3306 See also `auto-save-file-name-p'."
3307 (if buffer-file-name
3308 (let ((list auto-save-file-name-transforms
)
3309 (filename buffer-file-name
)
3311 ;; Apply user-specified translations
3312 ;; to the file name.
3313 (while (and list
(not result
))
3314 (if (string-match (car (car list
)) filename
)
3315 (setq result
(replace-match (cadr (car list
)) t nil
3317 (setq list
(cdr list
)))
3318 (if result
(setq filename result
))
3320 (if (and (eq system-type
'ms-dos
)
3321 (not (msdos-long-file-names)))
3322 ;; We truncate the file name to DOS 8+3 limits before
3323 ;; doing anything else, because the regexp passed to
3324 ;; string-match below cannot handle extensions longer than
3325 ;; 3 characters, multiple dots, and other atrocities.
3326 (let ((fn (dos-8+3-filename
3327 (file-name-nondirectory buffer-file-name
))))
3328 (string-match "\\`\\([^.]+\\)\\(\\.\\(..?\\)?.?\\|\\)\\'" fn
)
3329 (concat (file-name-directory buffer-file-name
)
3330 "#" (match-string 1 fn
)
3331 "." (match-string 3 fn
) "#"))
3332 (concat (file-name-directory filename
)
3334 (file-name-nondirectory filename
)
3337 ;; Deal with buffers that don't have any associated files. (Mail
3338 ;; mode tends to create a good number of these.)
3340 (let ((buffer-name (buffer-name))
3342 ;; Eliminate all slashes and backslashes by
3343 ;; replacing them with sequences that start with %.
3344 ;; Quote % also, to keep distinct names distinct.
3345 (while (string-match "[/\\%]" buffer-name limit
)
3346 (let* ((character (aref buffer-name
(match-beginning 0)))
3348 (cond ((eq character ?%
) "%%")
3349 ((eq character ?
/) "%+")
3350 ((eq character ?
\\) "%-"))))
3351 (setq buffer-name
(replace-match replacement t t buffer-name
))
3352 (setq limit
(1+ (match-end 0)))))
3353 ;; Generate the file name.
3355 (format "#%s#%s#" buffer-name
(make-temp-name ""))
3356 ;; Try a few alternative directories, to get one we can write it.
3358 ((file-writable-p default-directory
) default-directory
)
3359 ((file-writable-p "/var/tmp/") "/var/tmp/")
3362 (defun auto-save-file-name-p (filename)
3363 "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'.
3364 FILENAME should lack slashes. You can redefine this for customization."
3365 (string-match "^#.*#$" filename
))
3367 (defun wildcard-to-regexp (wildcard)
3368 "Given a shell file name pattern WILDCARD, return an equivalent regexp.
3369 The generated regexp will match a filename iff the filename
3370 matches that wildcard according to shell rules. Only wildcards known
3371 by `sh' are supported."
3372 (let* ((i (string-match "[[.*+\\^$?]" wildcard
))
3373 ;; Copy the initial run of non-special characters.
3374 (result (substring wildcard
0 i
))
3375 (len (length wildcard
)))
3376 ;; If no special characters, we're almost done.
3379 (let ((ch (aref wildcard i
))
3387 (eq (aref wildcard
(1+ i
)) ?\
]))
3389 ((eq ch ?\
[) ; [...] maps to regexp char class
3394 ((eq (aref wildcard i
) ?
!) ; [!...] -> [^...]
3397 (if (eq (aref wildcard i
) ?\
])
3402 ((eq (aref wildcard i
) ?^
)
3403 ;; Found "[^". Insert a `\0' character
3404 ;; (which cannot happen in a filename)
3405 ;; into the character class, so that `^'
3406 ;; is not the first character after `[',
3407 ;; and thus non-special in a regexp.
3411 ((eq (aref wildcard i
) ?\
])
3412 ;; I don't think `]' can appear in a
3413 ;; character class in a wildcard, but
3414 ;; let's be general here.
3419 (prog1 ; copy everything upto next `]'.
3422 (setq j
(string-match
3424 (setq i
(if j
(1- j
) (1- len
)))))))
3426 ((eq ch ?
*) "[^\000]*")
3430 ((eq ch ?
\\) "\\\\") ; probably cannot happen...
3431 ((eq ch ??
) "[^\000]")
3432 (t (char-to-string ch
)))))
3434 ;; Shell wildcards should match the entire filename,
3435 ;; not its part. Make the regexp say so.
3436 (concat "\\`" result
"\\'")))
3438 (defcustom list-directory-brief-switches
3439 (if (eq system-type
'vax-vms
) "" "-CF")
3440 "*Switches for `list-directory' to pass to `ls' for brief listing."
3444 (defcustom list-directory-verbose-switches
3445 (if (eq system-type
'vax-vms
)
3446 "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)"
3448 "*Switches for `list-directory' to pass to `ls' for verbose listing."
3452 (defun file-expand-wildcards (pattern &optional full
)
3453 "Expand wildcard pattern PATTERN.
3454 This returns a list of file names which match the pattern.
3456 If PATTERN is written as an absolute relative file name,
3457 the values are absolute also.
3459 If PATTERN is written as a relative file name, it is interpreted
3460 relative to the current default directory, `default-directory'.
3461 The file names returned are normally also relative to the current
3462 default directory. However, if FULL is non-nil, they are absolute."
3464 (let* ((nondir (file-name-nondirectory pattern
))
3465 (dirpart (file-name-directory pattern
))
3466 ;; A list of all dirs that DIRPART specifies.
3467 ;; This can be more than one dir
3468 ;; if DIRPART contains wildcards.
3469 (dirs (if (and dirpart
(string-match "[[*?]" dirpart
))
3470 (mapcar 'file-name-as-directory
3471 (file-expand-wildcards
3472 (directory-file-name dirpart
)))
3476 (when (or (null (car dirs
)) ; Possible if DIRPART is not wild.
3477 (file-directory-p (directory-file-name (car dirs
))))
3478 (let ((this-dir-contents
3479 ;; Filter out "." and ".."
3481 (mapcar #'(lambda (name)
3482 (unless (string-match
3484 (file-name-nondirectory name
))
3486 (directory-files (or (car dirs
) ".") full
3487 (wildcard-to-regexp nondir
))))))
3490 (if (and (car dirs
) (not full
))
3492 (function (lambda (name) (concat (car dirs
) name
)))
3496 (setq dirs
(cdr dirs
)))
3499 (defun list-directory (dirname &optional verbose
)
3500 "Display a list of files in or matching DIRNAME, a la `ls'.
3501 DIRNAME is globbed by the shell if necessary.
3502 Prefix arg (second arg if noninteractive) means supply -l switch to `ls'.
3503 Actions controlled by variables `list-directory-brief-switches'
3504 and `list-directory-verbose-switches'."
3505 (interactive (let ((pfx current-prefix-arg
))
3506 (list (read-file-name (if pfx
"List directory (verbose): "
3507 "List directory (brief): ")
3508 nil default-directory nil
)
3510 (let ((switches (if verbose list-directory-verbose-switches
3511 list-directory-brief-switches
)))
3512 (or dirname
(setq dirname default-directory
))
3513 (setq dirname
(expand-file-name dirname
))
3514 (with-output-to-temp-buffer "*Directory*"
3515 (buffer-disable-undo standard-output
)
3516 (princ "Directory ")
3520 (set-buffer "*Directory*")
3521 (setq default-directory
3522 (if (file-directory-p dirname
)
3523 (file-name-as-directory dirname
)
3524 (file-name-directory dirname
)))
3525 (let ((wildcard (not (file-directory-p dirname
))))
3526 (insert-directory dirname switches wildcard
(not wildcard
)))))))
3528 (defun shell-quote-wildcard-pattern (pattern)
3529 "Quote characters special to the shell in PATTERN, leave wildcards alone.
3531 PATTERN is assumed to represent a file-name wildcard suitable for the
3532 underlying filesystem. For Unix and GNU/Linux, the characters from the
3533 set [ \\t\\n;<>&|()#$] are quoted with a backslash; for DOS/Windows, all
3534 the parts of the pattern which don't include wildcard characters are
3535 quoted with double quotes.
3536 Existing quote characters in PATTERN are left alone, so you can pass
3537 PATTERN that already quotes some of the special characters."
3540 ((memq system-type
'(ms-dos windows-nt
))
3541 ;; DOS/Windows don't allow `"' in file names. So if the
3542 ;; argument has quotes, we can safely assume it is already
3543 ;; quoted by the caller.
3544 (if (or (string-match "[\"]" pattern
)
3545 ;; We quote [&()#$'] in case their shell is a port of a
3546 ;; Unixy shell. We quote [,=+] because stock DOS and
3547 ;; Windows shells require that in some cases, such as
3548 ;; passing arguments to batch files that use positional
3549 ;; arguments like %1.
3550 (not (string-match "[ \t;&()#$',=+]" pattern
)))
3555 (while (string-match "[*?]+" pattern beg
)
3556 (setq end
(match-beginning 0)
3557 result
(concat result
(substring pattern beg end
)
3559 (substring pattern end
(match-end 0))
3562 (concat result
(substring pattern beg
) "\""))))
3565 (while (string-match "[ \t\n;<>&|()#$]" pattern beg
)
3567 (concat (substring pattern
0 (match-beginning 0))
3569 (substring pattern
(match-beginning 0)))
3570 beg
(1+ (match-end 0)))))
3574 (defvar insert-directory-program
"ls"
3575 "Absolute or relative name of the `ls' program used by `insert-directory'.")
3578 ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and
3579 ;; FULL-DIRECTORY-P is nil.
3580 ;; The single line of output must display FILE's name as it was
3581 ;; given, namely, an absolute path name.
3582 ;; - must insert exactly one line for each file if WILDCARD or
3583 ;; FULL-DIRECTORY-P is t, plus one optional "total" line
3584 ;; before the file lines, plus optional text after the file lines.
3585 ;; Lines are delimited by "\n", so filenames containing "\n" are not
3587 ;; File lines should display the basename.
3588 ;; - must be consistent with
3589 ;; - functions dired-move-to-filename, (these two define what a file line is)
3590 ;; dired-move-to-end-of-filename,
3591 ;; dired-between-files, (shortcut for (not (dired-move-to-filename)))
3592 ;; dired-insert-headerline
3593 ;; dired-after-subdir-garbage (defines what a "total" line is)
3594 ;; - variable dired-subdir-regexp
3595 (defun insert-directory (file switches
&optional wildcard full-directory-p
)
3596 "Insert directory listing for FILE, formatted according to SWITCHES.
3597 Leaves point after the inserted text.
3598 SWITCHES may be a string of options, or a list of strings.
3599 Optional third arg WILDCARD means treat FILE as shell wildcard.
3600 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
3601 switches do not contain `d', so that a full listing is expected.
3603 This works by running a directory listing program
3604 whose name is in the variable `insert-directory-program'.
3605 If WILDCARD, it also runs the shell specified by `shell-file-name'."
3606 ;; We need the directory in order to find the right handler.
3607 (let ((handler (find-file-name-handler (expand-file-name file
)
3608 'insert-directory
)))
3610 (funcall handler
'insert-directory file switches
3611 wildcard full-directory-p
)
3612 (if (eq system-type
'vax-vms
)
3613 (vms-read-directory file switches
(current-buffer))
3614 (let* ((coding-system-for-read
3615 (and enable-multibyte-characters
3616 (or file-name-coding-system
3617 default-file-name-coding-system
)))
3618 ;; This is to control encoding the arguments in call-process.
3619 (coding-system-for-write coding-system-for-read
)
3622 ;; Run ls in the directory of the file pattern we asked for
3623 (let ((default-directory
3624 (if (file-name-absolute-p file
)
3625 (file-name-directory file
)
3626 (file-name-directory (expand-file-name file
))))
3627 (pattern (file-name-nondirectory file
)))
3629 shell-file-name nil t nil
3630 "-c" (concat (if (memq system-type
'(ms-dos windows-nt
))
3632 "\\") ; Disregard Unix shell aliases!
3633 insert-directory-program
3635 (if (stringp switches
)
3637 (mapconcat 'identity switches
" "))
3639 ;; Quote some characters that have
3640 ;; special meanings in shells; but
3641 ;; don't quote the wildcards--we
3642 ;; want them to be special. We
3643 ;; also currently don't quote the
3644 ;; quoting characters in case
3645 ;; people want to use them
3646 ;; explicitly to quote wildcard
3648 (shell-quote-wildcard-pattern pattern
))))
3649 ;; SunOS 4.1.3, SVr4 and others need the "." to list the
3650 ;; directory if FILE is a symbolic link.
3651 (apply 'call-process
3652 insert-directory-program nil t nil
3654 (if (listp switches
) switches
3655 (unless (equal switches
"")
3656 ;; Split the switches at any spaces so we can
3657 ;; pass separate options as separate args.
3658 (split-string switches
)))
3659 ;; Avoid lossage if FILE starts with `-'.
3662 (if (string-match "\\`~" file
)
3663 (setq file
(expand-file-name file
)))
3665 (if full-directory-p
3666 (concat (file-name-as-directory file
) ".")
3669 ;; We get here if `insert-directory-program' failed.
3670 ;; On non-Posix systems, we cannot open a directory, so
3671 ;; don't even try, because that will always result in
3672 ;; the ubiquitous "Access denied". Instead, show them
3673 ;; the `ls' command line and let them guess what went
3675 (if (and (file-directory-p file
)
3676 (memq system-type
'(ms-dos windows-nt
)))
3678 "Reading directory: \"%s %s -- %s\" exited with status %s"
3679 insert-directory-program
3680 (if (listp switches
) (concat switches
) switches
)
3682 ;; Unix. Access the file to get a suitable error.
3683 (access-file file
"Reading directory"))
3684 ;; Replace "total" with "used", to avoid confusion.
3685 ;; Add in the amount of free space.
3687 (goto-char (point-min))
3688 (when (re-search-forward "^total" nil t
)
3689 (replace-match "used")
3693 (call-process "df" nil t nil
".")
3694 (goto-char (point-min))
3696 (skip-chars-forward "^ \t")
3698 (let ((end (point)))
3700 (setq available
(buffer-substring (point) end
))))
3701 (insert " available " available
))))))))))
3703 (defun insert-directory-safely (file switches
3704 &optional wildcard full-directory-p
)
3705 "Insert directory listing for FILE, formatted according to SWITCHES.
3707 Like `insert-directory', but if FILE does not exist, it inserts a
3708 message to that effect instead of signaling an error."
3709 (if (file-exists-p file
)
3710 (insert-directory file switches wildcard full-directory-p
)
3711 ;; Simulate the message printed by `ls'.
3712 (insert (format "%s: No such file or directory\n" file
))))
3714 (defvar kill-emacs-query-functions nil
3715 "Functions to call with no arguments to query about killing Emacs.
3716 If any of these functions returns nil, killing Emacs is cancelled.
3717 `save-buffers-kill-emacs' (\\[save-buffers-kill-emacs]) calls these functions,
3718 but `kill-emacs', the low level primitive, does not.
3719 See also `kill-emacs-hook'.")
3721 (defcustom confirm-kill-emacs nil
3722 "How to ask for confirmation when leaving Emacs.
3723 If nil, the default, don't ask at all. If the value is non-nil, it should
3724 be a predicate function such as `yes-or-no-p'."
3725 :type
'(choice (const :tag
"Ask with yes-or-no-p" yes-or-no-p
)
3726 (const :tag
"Ask with y-or-n-p" y-or-n-p
)
3727 (const :tag
"Don't confirm" nil
))
3731 (defun save-buffers-kill-emacs (&optional arg
)
3732 "Offer to save each buffer, then kill this Emacs process.
3733 With prefix arg, silently save all file-visiting buffers, then kill."
3735 (save-some-buffers arg t
)
3736 (and (or (not (memq t
(mapcar (function
3737 (lambda (buf) (and (buffer-file-name buf
)
3738 (buffer-modified-p buf
))))
3740 (yes-or-no-p "Modified buffers exist; exit anyway? "))
3741 (or (not (fboundp 'process-list
))
3742 ;; process-list is not defined on VMS.
3743 (let ((processes (process-list))
3746 (and (memq (process-status (car processes
)) '(run stop open
))
3747 (let ((val (process-kill-without-query (car processes
))))
3748 (process-kill-without-query (car processes
) val
)
3751 (setq processes
(cdr processes
)))
3754 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
3755 ;; Query the user for other things, perhaps.
3756 (run-hook-with-args-until-failure 'kill-emacs-query-functions
)
3757 (or (null confirm-kill-emacs
)
3758 (funcall confirm-kill-emacs
"Really exit Emacs? "))
3761 ;; We use /: as a prefix to "quote" a file name
3762 ;; so that magic file name handlers will not apply to it.
3764 (setq file-name-handler-alist
3765 (cons '("\\`/:" . file-name-non-special
)
3766 file-name-handler-alist
))
3768 ;; We depend on being the last handler on the list,
3769 ;; so that anything else which does need handling
3770 ;; has been handled already.
3771 ;; So it is safe for us to inhibit *all* magic file name handlers.
3773 (defun file-name-non-special (operation &rest arguments
)
3774 (let ((file-name-handler-alist nil
)
3776 (if (eq operation
'insert-directory
)
3777 (directory-file-name
3779 (unhandled-file-name-directory default-directory
)))
3781 ;; Get a list of the indices of the args which are file names.
3783 (cdr (or (assq operation
3784 ;; The first four are special because they
3785 ;; return a file name. We want to include the /:
3786 ;; in the return value.
3787 ;; So just avoid stripping it in the first place.
3788 '((expand-file-name . nil
)
3789 ;; `identity' means just return the first arg
3790 ;; as stripped of its quoting.
3791 (substitute-in-file-name . identity
)
3792 (file-name-directory . nil
)
3793 (file-name-as-directory . nil
)
3794 (directory-file-name . nil
)
3795 (file-name-completion 0 1)
3796 (file-name-all-completions 0 1)
3799 (make-symbolic-link 0 1)
3800 (add-name-to-file 0 1)))
3801 ;; For all other operations, treat the first argument only
3802 ;; as the file name.
3804 ;; Copy ARGUMENTS so we can replace elements in it.
3805 (arguments (copy-sequence arguments
)))
3806 ;; Strip off the /: from the file names that have this handler.
3808 (while (consp file-arg-indices
)
3809 (let ((pair (nthcdr (car file-arg-indices
) arguments
)))
3811 (string-match "\\`/:" (car pair
))
3813 (if (= (length (car pair
)) 2)
3815 (substring (car pair
) 2)))))
3816 (setq file-arg-indices
(cdr file-arg-indices
))))
3817 (if (eq file-arg-indices
'identity
)
3819 (apply operation arguments
))))
3821 (define-key ctl-x-map
"\C-f" 'find-file
)
3822 (define-key ctl-x-map
"\C-r" 'find-file-read-only
)
3823 (define-key ctl-x-map
"\C-v" 'find-alternate-file
)
3824 (define-key ctl-x-map
"\C-s" 'save-buffer
)
3825 (define-key ctl-x-map
"s" 'save-some-buffers
)
3826 (define-key ctl-x-map
"\C-w" 'write-file
)
3827 (define-key ctl-x-map
"i" 'insert-file
)
3828 (define-key esc-map
"~" 'not-modified
)
3829 (define-key ctl-x-map
"\C-d" 'list-directory
)
3830 (define-key ctl-x-map
"\C-c" 'save-buffers-kill-emacs
)
3832 (define-key ctl-x-4-map
"f" 'find-file-other-window
)
3833 (define-key ctl-x-4-map
"r" 'find-file-read-only-other-window
)
3834 (define-key ctl-x-4-map
"\C-f" 'find-file-other-window
)
3835 (define-key ctl-x-4-map
"b" 'switch-to-buffer-other-window
)
3836 (define-key ctl-x-4-map
"\C-o" 'display-buffer
)
3838 (define-key ctl-x-5-map
"b" 'switch-to-buffer-other-frame
)
3839 (define-key ctl-x-5-map
"f" 'find-file-other-frame
)
3840 (define-key ctl-x-5-map
"\C-f" 'find-file-other-frame
)
3841 (define-key ctl-x-5-map
"r" 'find-file-read-only-other-frame
)
3843 ;;; files.el ends here