1 ;;; files.el --- file input and output commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 92, 93, 94, 1995 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; Defines most of Emacs's file- and directory-handling functions,
27 ;; including basic file visiting, backup generation, link handling,
28 ;; ITS-id version control, load- and write-hook handling, and the like.
32 (defconst delete-auto-save-files t
33 "*Non-nil means delete auto-save file when a buffer is saved or killed.")
35 (defconst directory-abbrev-alist
37 "*Alist of abbreviations for file directories.
38 A list of elements of the form (FROM . TO), each meaning to replace
39 FROM with TO when it appears in a directory name. This replacement is
40 done when setting up the default directory of a newly visited file.
41 *Every* FROM string should start with `^'.
43 Do not use `~' in the TO strings.
44 They should be ordinary absolute directory names.
46 Use this feature when you have directories which you normally refer to
47 via absolute symbolic links. Make TO the name of the link, and FROM
48 the name it is linked to.")
50 ;;; Turn off backup files on VMS since it has version numbers.
51 (defconst make-backup-files
(not (eq system-type
'vax-vms
))
52 "*Non-nil means make a backup of a file the first time it is saved.
53 This can be done by renaming the file or by copying.
55 Renaming means that Emacs renames the existing file so that it is a
56 backup file, then writes the buffer into a new file. Any other names
57 that the old file had will now refer to the backup file. The new file
58 is owned by you and its group is defaulted.
60 Copying means that Emacs copies the existing file into the backup
61 file, then writes the buffer on top of the existing file. Any other
62 names that the old file had will now refer to the new (edited) file.
63 The file's owner and group are unchanged.
65 The choice of renaming or copying is controlled by the variables
66 `backup-by-copying', `backup-by-copying-when-linked' and
67 `backup-by-copying-when-mismatch'. See also `backup-inhibited'.")
69 ;; Do this so that local variables based on the file name
70 ;; are not overridden by the major mode.
71 (defvar backup-inhibited nil
72 "Non-nil means don't make a backup, regardless of the other parameters.
73 This variable is intended for use by making it local to a buffer.
74 But it is local only if you make it local.")
75 (put 'backup-inhibited
'permanent-local t
)
77 (defconst backup-by-copying nil
78 "*Non-nil means always use copying to create backup files.
79 See documentation of variable `make-backup-files'.")
81 (defconst backup-by-copying-when-linked nil
82 "*Non-nil means use copying to create backups for files with multiple names.
83 This causes the alternate names to refer to the latest version as edited.
84 This variable is relevant only if `backup-by-copying' is nil.")
86 (defconst backup-by-copying-when-mismatch nil
87 "*Non-nil means create backups by copying if this preserves owner or group.
88 Renaming may still be used (subject to control of other variables)
89 when it would not result in changing the owner or group of the file;
90 that is, for files which are owned by you and whose group matches
91 the default for a new file created there by you.
92 This variable is relevant only if `backup-by-copying' is nil.")
94 (defvar backup-enable-predicate
96 (or (< (length name
) 5)
97 (not (string-equal "/tmp/" (substring name
0 5)))))
98 "Predicate that looks at a file name and decides whether to make backups.
99 Called with an absolute file name as argument, it returns t to enable backup.")
101 (defconst buffer-offer-save nil
102 "*Non-nil in a buffer means offer to save the buffer on exit
103 even if the buffer is not visiting a file.
104 Automatically local in all buffers.")
105 (make-variable-buffer-local 'buffer-offer-save
)
107 (defconst find-file-existing-other-name nil
108 "*Non-nil means find a file under alternative names, in existing buffers.
109 This means if any existing buffer is visiting the file you want
110 under another name, you get the existing buffer instead of a new buffer.")
112 (defconst find-file-visit-truename nil
113 "*Non-nil means visit a file under its truename.
114 The truename of a file is found by chasing all links
115 both at the file level and at the levels of the containing directories.")
117 (defvar buffer-file-number nil
118 "The device number and file number of the file visited in the current buffer.
119 The value is a list of the form (FILENUM DEVNUM).
120 This pair of numbers uniquely identifies the file.
121 If the buffer is visiting a new file, the value is nil.")
122 (make-variable-buffer-local 'buffer-file-number
)
123 (put 'buffer-file-number
'permanent-local t
)
125 (defconst file-precious-flag nil
126 "*Non-nil means protect against I/O errors while saving files.
127 Some modes set this non-nil in particular buffers.
129 This feature works by writing the new contents into a temporary file
130 and then renaming the temporary file to replace the original.
131 In this way, any I/O error in writing leaves the original untouched,
132 and there is never any instant where the file is nonexistent.
134 Note that this feature forces backups to be made by copying.
135 Yet, at the same time, saving a precious file
136 breaks any hard links between it and other files.")
138 (defvar version-control nil
139 "*Control use of version numbers for backup files.
140 t means make numeric backup versions unconditionally.
141 nil means make them for files that have some already.
142 `never' means do not make them.")
144 (defvar dired-kept-versions
2
145 "*When cleaning directory, number of versions to keep.")
147 (defvar delete-old-versions nil
148 "*If t, delete excess backup versions silently.
149 If nil, ask confirmation. Any other value prevents any trimming.")
151 (defvar kept-old-versions
2
152 "*Number of oldest versions to keep when a new numbered backup is made.")
154 (defvar kept-new-versions
2
155 "*Number of newest versions to keep when a new numbered backup is made.
156 Includes the new backup. Must be > 0")
158 (defconst require-final-newline nil
159 "*Value of t says silently ensure a file ends in a newline when it is saved.
160 Non-nil but not t says ask user whether to add a newline when there isn't one.
161 nil means don't add newlines.")
163 (defconst auto-save-default t
164 "*Non-nil says by default do auto-saving of every file-visiting buffer.")
166 (defconst auto-save-visited-file-name nil
167 "*Non-nil says auto-save a buffer in the file it is visiting, when practical.
168 Normally auto-save files are written under other names.")
170 (defconst save-abbrevs nil
171 "*Non-nil means save word abbrevs too when files are saved.
172 Loading an abbrev file sets this to t.")
174 (defconst find-file-run-dired t
175 "*Non-nil says run dired if `find-file' is given the name of a directory.")
177 ;;;It is not useful to make this a local variable.
178 ;;;(put 'find-file-not-found-hooks 'permanent-local t)
179 (defvar find-file-not-found-hooks nil
180 "List of functions to be called for `find-file' on nonexistent file.
181 These functions are called as soon as the error is detected.
182 `buffer-file-name' is already set up.
183 The functions are called in the order given until one of them returns non-nil.")
185 ;;;It is not useful to make this a local variable.
186 ;;;(put 'find-file-hooks 'permanent-local t)
187 (defvar find-file-hooks nil
188 "List of functions to be called after a buffer is loaded from a file.
189 The buffer's local variables (if any) will have been processed before the
190 functions are called.")
192 (defvar write-file-hooks nil
193 "List of functions to be called before writing out a buffer to a file.
194 If one of them returns non-nil, the file is considered already written
195 and the rest are not called.
196 These hooks are considered to pertain to the visited file.
197 So this list is cleared if you change the visited file name.
199 Don't make this variable buffer-local; instead, use `local-write-file-hooks'.
200 See also `write-contents-hooks'.")
201 ;;; However, in case someone does make it local...
202 (put 'write-file-hooks
'permanent-local t
)
204 (defvar local-write-file-hooks nil
205 "Just like `write-file-hooks', except intended for per-buffer use.
206 The functions in this list are called before the ones in
209 This variable is meant to be used for hooks that have to do with a
210 particular visited file. Therefore, it is a permanent local, so that
211 changing the major mode does not clear it. However, calling
212 `set-visited-file-name' does clear it.")
213 (make-variable-buffer-local 'local-write-file-hooks
)
214 (put 'local-write-file-hooks
'permanent-local t
)
216 (defvar write-contents-hooks nil
217 "List of functions to be called before writing out a buffer to a file.
218 If one of them returns non-nil, the file is considered already written
219 and the rest are not called.
221 This variable is meant to be used for hooks that pertain to the
222 buffer's contents, not to the particular visited file; thus,
223 `set-visited-file-name' does not clear this variable; but changing the
224 major mode does clear it.
226 This variable automatically becomes buffer-local whenever it is set.
227 If you use `add-hooks' to add elements to the list, use nil for the
230 See also `write-file-hooks'.")
231 (make-variable-buffer-local 'write-contents-hooks
)
233 (defconst enable-local-variables t
234 "*Control use of local-variables lists in files you visit.
235 The value can be t, nil or something else.
236 A value of t means local-variables lists are obeyed;
237 nil means they are ignored; anything else means query.
239 The command \\[normal-mode] always obeys local-variables lists
240 and ignores this variable.")
242 (defconst enable-local-eval
'maybe
243 "*Control processing of the \"variable\" `eval' in a file's local variables.
244 The value can be t, nil or something else.
245 A value of t means obey `eval' variables;
246 nil means ignore them; anything else means query.
248 The command \\[normal-mode] always obeys local-variables lists
249 and ignores this variable.")
251 ;; Avoid losing in versions where CLASH_DETECTION is disabled.
252 (or (fboundp 'lock-buffer
)
253 (defalias 'lock-buffer
'ignore
))
254 (or (fboundp 'unlock-buffer
)
255 (defalias 'unlock-buffer
'ignore
))
257 ;; This hook function provides support for ange-ftp host name
258 ;; completion. It runs the usual ange-ftp hook, but only for
259 ;; completion operations. Having this here avoids the need
260 ;; to load ange-ftp when it's not really in use.
261 (defun ange-ftp-completion-hook-function (op &rest args
)
262 (if (memq op
'(file-name-completion file-name-all-completions
))
263 (apply 'ange-ftp-hook-function op args
)
264 (let ((inhibit-file-name-handlers
265 (cons 'ange-ftp-completion-hook-function
266 (and (eq inhibit-file-name-operation op
)
267 inhibit-file-name-handlers
)))
268 (inhibit-file-name-operation op
))
271 (defun convert-standard-filename (filename)
272 "Convert a standard file's name to something suitable for the current OS.
273 This function's standard definition is trivial; it just returns the argument.
274 However, on some systems, the function is redefined
275 with a definition that really does change some file names."
279 "Show the current default directory."
281 (message "Directory %s" default-directory
))
284 "Value of the CDPATH environment variable, as a list.
285 Not actually set up until the first time you you use it.")
287 (defvar path-separator
":"
288 "Character used to separate concatenated paths.")
290 (defun parse-colon-path (cd-path)
291 "Explode a colon-separated list of paths into a string list."
293 (let (cd-prefix cd-list
(cd-start 0) cd-colon
)
294 (setq cd-path
(concat cd-path path-separator
))
295 (while (setq cd-colon
(string-match path-separator cd-path cd-start
))
298 (list (if (= cd-start cd-colon
)
300 (substitute-in-file-name
301 (file-name-as-directory
302 (substring cd-path cd-start cd-colon
)))))))
303 (setq cd-start
(+ cd-colon
1)))
306 (defun cd-absolute (dir)
307 "Change current directory to given absolute file name DIR."
308 ;; Put the name into directory syntax now,
309 ;; because otherwise expand-file-name may give some bad results.
310 (if (not (eq system-type
'vax-vms
))
311 (setq dir
(file-name-as-directory dir
)))
312 (setq dir
(abbreviate-file-name (expand-file-name dir
)))
313 (if (not (file-directory-p dir
))
314 (error "%s is not a directory" dir
)
315 (if (file-executable-p dir
)
316 (setq default-directory dir
)
317 (error "Cannot cd to %s: Permission denied" dir
))))
320 "Make DIR become the current buffer's default directory.
321 If your environment includes a `CDPATH' variable, try each one of that
322 colon-separated list of directories when resolving a relative directory name."
324 (list (read-file-name "Change default directory: "
325 default-directory default-directory
326 (and (member cd-path
'(nil ("./")))
327 (null (getenv "CDPATH"))))))
328 (if (file-name-absolute-p dir
)
329 (cd-absolute (expand-file-name dir
))
331 (let ((trypath (parse-colon-path (getenv "CDPATH"))))
332 (setq cd-path
(or trypath
(list "./")))))
333 (if (not (catch 'found
335 (function (lambda (x)
336 (let ((f (expand-file-name (concat x dir
))))
337 (if (file-directory-p f
)
340 (throw 'found t
))))))
343 (error "No such directory found via CDPATH environment variable"))))
345 (defun load-file (file)
346 "Load the Lisp file named FILE."
347 (interactive "fLoad file: ")
348 (load (expand-file-name file
) nil nil t
))
350 (defun load-library (library)
351 "Load the library named LIBRARY.
352 This is an interface to the function `load'."
353 (interactive "sLoad library: ")
356 (defun file-local-copy (file &optional buffer
)
357 "Copy the file FILE into a temporary file on this machine.
358 Returns the name of the local copy, or nil, if FILE is directly
360 (let ((handler (find-file-name-handler file
'file-local-copy
)))
362 (funcall handler
'file-local-copy file
)
365 (defun file-truename (filename &optional counter prev-dirs
)
366 "Return the truename of FILENAME, which should be absolute.
367 The truename of a file name is found by chasing symbolic links
368 both at the level of the file and at the level of the directories
369 containing it, until no links are left at any level.
371 The arguments COUNTER and PREV-DIRS are used only in recursive calls.
372 Do not specify them in other calls."
373 ;; COUNTER can be a cons cell whose car is the count of how many more links
374 ;; to chase before getting an error.
375 ;; PREV-DIRS can be a cons cell whose car is an alist
376 ;; of truenames we've just recently computed.
378 ;; The last test looks dubious, maybe `+' is meant here? --simon.
379 (if (or (string= filename
"") (string= filename
"~")
380 (and (string= (substring filename
0 1) "~")
381 (string-match "~[^/]*" filename
)))
383 (setq filename
(expand-file-name filename
))
384 (if (string= filename
"")
385 (setq filename
"/"))))
386 (or counter
(setq counter
(list 100)))
388 ;; For speed, remove the ange-ftp completion handler from the list.
389 ;; We know it's not needed here.
390 ;; For even more speed, do this only on the outermost call.
391 (file-name-handler-alist
392 (if prev-dirs file-name-handler-alist
393 (let ((tem (copy-sequence file-name-handler-alist
)))
394 (delq (rassq 'ange-ftp-completion-hook-function tem
) tem
)))))
395 (or prev-dirs
(setq prev-dirs
(list nil
)))
396 ;; If this file directly leads to a link, process that iteratively
397 ;; so that we don't use lots of stack.
399 (setcar counter
(1- (car counter
)))
400 (if (< (car counter
) 0)
401 (error "Apparent cycle of symbolic links for %s" filename
))
402 (let ((handler (find-file-name-handler filename
'file-truename
)))
403 ;; For file name that has a special handler, call handler.
404 ;; This is so that ange-ftp can save time by doing a no-op.
406 (setq filename
(funcall handler
'file-truename filename
)
408 (let ((dir (or (file-name-directory filename
) default-directory
))
410 ;; Get the truename of the directory.
411 (setq dirfile
(directory-file-name dir
))
412 ;; If these are equal, we have the (or a) root directory.
413 (or (string= dir dirfile
)
414 ;; If this is the same dir we last got the truename for,
415 ;; save time--don't recalculate.
416 (if (assoc dir
(car prev-dirs
))
417 (setq dir
(cdr (assoc dir
(car prev-dirs
))))
419 (new (file-name-as-directory (file-truename dirfile counter prev-dirs
))))
420 (setcar prev-dirs
(cons (cons old new
) (car prev-dirs
)))
422 (if (equal ".." (file-name-nondirectory filename
))
424 (directory-file-name (file-name-directory (directory-file-name dir
)))
426 (if (equal "." (file-name-nondirectory filename
))
427 (setq filename
(directory-file-name dir
)
429 ;; Put it back on the file name.
430 (setq filename
(concat dir
(file-name-nondirectory filename
)))
431 ;; Is the file name the name of a link?
432 (setq target
(file-symlink-p filename
))
434 ;; Yes => chase that link, then start all over
435 ;; since the link may point to a directory name that uses links.
436 ;; We can't safely use expand-file-name here
437 ;; since target might look like foo/../bar where foo
438 ;; is itself a link. Instead, we handle . and .. above.
440 (if (file-name-absolute-p target
)
448 (defun file-chase-links (filename)
449 "Chase links in FILENAME until a name that is not a link.
450 Does not examine containing directories for links,
451 unlike `file-truename'."
452 (let (tem (count 100) (newname filename
))
453 (while (setq tem
(file-symlink-p newname
))
455 (error "Apparent cycle of symbolic links for %s" filename
))
456 ;; In the context of a link, `//' doesn't mean what Emacs thinks.
457 (while (string-match "//+" tem
)
458 (setq tem
(concat (substring tem
0 (1+ (match-beginning 0)))
459 (substring tem
(match-end 0)))))
460 ;; Handle `..' by hand, since it needs to work in the
461 ;; target of any directory symlink.
462 ;; This code is not quite complete; it does not handle
463 ;; embedded .. in some cases such as ./../foo and foo/bar/../../../lose.
464 (while (string-match "\\`\\.\\./" tem
)
465 (setq tem
(substring tem
3))
466 (setq newname
(file-name-as-directory
467 ;; Do the .. by hand.
470 ;; Chase links in the default dir of the symlink.
473 (file-name-directory newname
))))))))
474 (setq newname
(expand-file-name tem
(file-name-directory newname
)))
475 (setq count
(1- count
)))
478 (defun switch-to-buffer-other-window (buffer)
479 "Select buffer BUFFER in another window."
480 (interactive "BSwitch to buffer in other window: ")
481 (let ((pop-up-windows t
))
482 (pop-to-buffer buffer t
)))
484 (defun switch-to-buffer-other-frame (buffer)
485 "Switch to buffer BUFFER in another frame."
486 (interactive "BSwitch to buffer in other frame: ")
487 (let ((pop-up-frames t
))
488 (pop-to-buffer buffer t
)
489 (raise-frame (window-frame (selected-window)))))
491 (defun find-file (filename)
493 Switch to a buffer visiting file FILENAME,
494 creating one if none already exists."
495 (interactive "FFind file: ")
496 (switch-to-buffer (find-file-noselect filename
)))
498 (defun find-file-other-window (filename)
499 "Edit file FILENAME, in another window.
500 May create a new window, or reuse an existing one.
501 See the function `display-buffer'."
502 (interactive "FFind file in other window: ")
503 (switch-to-buffer-other-window (find-file-noselect filename
)))
505 (defun find-file-other-frame (filename)
506 "Edit file FILENAME, in another frame.
507 May create a new frame, or reuse an existing one.
508 See the function `display-buffer'."
509 (interactive "FFind file in other frame: ")
510 (switch-to-buffer-other-frame (find-file-noselect filename
)))
512 (defun find-file-read-only (filename)
513 "Edit file FILENAME but don't allow changes.
514 Like \\[find-file] but marks buffer as read-only.
515 Use \\[toggle-read-only] to permit editing."
516 (interactive "fFind file read-only: ")
518 (setq buffer-read-only t
)
521 (defun find-file-read-only-other-window (filename)
522 "Edit file FILENAME in another window but don't allow changes.
523 Like \\[find-file-other-window] but marks buffer as read-only.
524 Use \\[toggle-read-only] to permit editing."
525 (interactive "fFind file read-only other window: ")
526 (find-file-other-window filename
)
527 (setq buffer-read-only t
)
530 (defun find-file-read-only-other-frame (filename)
531 "Edit file FILENAME in another frame but don't allow changes.
532 Like \\[find-file-other-frame] but marks buffer as read-only.
533 Use \\[toggle-read-only] to permit editing."
534 (interactive "fFind file read-only other frame: ")
535 (find-file-other-frame filename
)
536 (setq buffer-read-only t
)
539 (defun find-alternate-file-other-window (filename)
540 "Find file FILENAME as a replacement for the file in the next window.
541 This command does not select that window."
543 (save-selected-window
545 (let ((file buffer-file-name
)
549 (setq file-name
(file-name-nondirectory file
)
550 file-dir
(file-name-directory file
)))
551 (list (read-file-name
552 "Find alternate file: " file-dir nil nil file-name
)))))
554 (find-file-other-window filename
)
555 (save-selected-window
557 (find-alternate-file filename
))))
559 (defun find-alternate-file (filename)
560 "Find file FILENAME, select its buffer, kill previous buffer.
561 If the current buffer now contains an empty file that you just visited
562 \(presumably by mistake), use this command to visit the file you really want."
564 (let ((file buffer-file-name
)
568 (setq file-name
(file-name-nondirectory file
)
569 file-dir
(file-name-directory file
)))
570 (list (read-file-name
571 "Find alternate file: " file-dir nil nil file-name
))))
572 (and (buffer-modified-p) (buffer-file-name)
573 ;; (not buffer-read-only)
574 (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? "
577 (let ((obuf (current-buffer))
578 (ofile buffer-file-name
)
579 (onum buffer-file-number
)
580 (otrue buffer-file-truename
)
581 (oname (buffer-name)))
582 (if (get-buffer " **lose**")
583 (kill-buffer " **lose**"))
584 (rename-buffer " **lose**")
585 (setq buffer-file-name nil
)
586 (setq buffer-file-number nil
)
587 (setq buffer-file-truename nil
)
591 (find-file filename
))
592 (cond ((eq obuf
(current-buffer))
593 (setq buffer-file-name ofile
)
594 (setq buffer-file-number onum
)
595 (setq buffer-file-truename otrue
)
597 (rename-buffer oname
))))
598 (or (eq (current-buffer) obuf
)
599 (kill-buffer obuf
))))
601 (defun create-file-buffer (filename)
602 "Create a suitably named buffer for visiting FILENAME, and return it.
603 FILENAME (sans directory) is used unchanged if that name is free;
604 otherwise a string <2> or <3> or ... is appended to get an unused name."
605 (let ((lastname (file-name-nondirectory filename
)))
606 (if (string= lastname
"")
607 (setq lastname filename
))
608 (generate-new-buffer lastname
)))
610 (defun generate-new-buffer (name)
611 "Create and return a buffer with a name based on NAME.
612 Choose the buffer's name using `generate-new-buffer-name'."
613 (get-buffer-create (generate-new-buffer-name name
)))
615 (defconst automount-dir-prefix
"^/tmp_mnt/"
616 "Regexp to match the automounter prefix in a directory name.")
618 (defvar abbreviated-home-dir nil
619 "The user's homedir abbreviated according to `directory-abbrev-list'.")
621 (defun abbreviate-file-name (filename)
622 "Return a version of FILENAME shortened using `directory-abbrev-alist'.
623 This also substitutes \"~\" for the user's home directory.
624 Type \\[describe-variable] directory-abbrev-alist RET for more information."
625 ;; Get rid of the prefixes added by the automounter.
626 (if (and (string-match automount-dir-prefix filename
)
627 (file-exists-p (file-name-directory
628 (substring filename
(1- (match-end 0))))))
629 (setq filename
(substring filename
(1- (match-end 0)))))
630 (let ((tail directory-abbrev-alist
))
631 ;; If any elt of directory-abbrev-alist matches this name,
632 ;; abbreviate accordingly.
634 (if (string-match (car (car tail
)) filename
)
636 (concat (cdr (car tail
)) (substring filename
(match-end 0)))))
637 (setq tail
(cdr tail
)))
638 ;; Compute and save the abbreviated homedir name.
639 ;; We defer computing this until the first time it's needed, to
640 ;; give time for directory-abbrev-alist to be set properly.
641 ;; We include a slash at the end, to avoid spurious matches
642 ;; such as `/usr/foobar' when the home dir is `/usr/foo'.
643 (or abbreviated-home-dir
644 (setq abbreviated-home-dir
645 (let ((abbreviated-home-dir "$foo"))
646 (concat "^" (abbreviate-file-name (expand-file-name "~"))
649 ;; If FILENAME starts with the abbreviated homedir,
650 ;; make it start with `~' instead.
651 (if (and (string-match abbreviated-home-dir filename
)
652 ;; If the home dir is just /, don't change it.
653 (not (and (= (match-end 0) 1)
654 (= (aref filename
0) ?
/)))
655 ;; MS-DOS root directories can come with a drive letter;
656 ;; Novell Netware allows drive letters beyond `Z:'.
657 (not (and (or (eq system-type
'ms-dos
)
658 (eq system-type
'windows-nt
))
660 (string-match "^[a-zA-`]:/$" filename
)))))
663 (substring filename
(match-beginning 1) (match-end 1))
664 (substring filename
(match-end 0)))))
667 (defvar find-file-not-true-dirname-list nil
668 "*List of logical names for which visiting shouldn't save the true dirname.
669 On VMS, when you visit a file using a logical name that searches a path,
670 you may or may not want the visited file name to record the specific
671 directory where the file was found. If you *do not* want that, add the logical
672 name to this list as a string.")
674 (defun find-buffer-visiting (filename)
675 "Return the buffer visiting file FILENAME (a string).
676 This is like `get-file-buffer', except that it checks for any buffer
677 visiting the same file, possibly under a different name.
678 If there is no such live buffer, return nil."
679 (let ((buf (get-file-buffer filename
))
680 (truename (abbreviate-file-name (file-truename filename
))))
682 (let ((list (buffer-list)) found
)
683 (while (and (not found
) list
)
685 (set-buffer (car list
))
686 (if (and buffer-file-name
687 (string= buffer-file-truename truename
))
688 (setq found
(car list
))))
689 (setq list
(cdr list
)))
691 (let ((number (nthcdr 10 (file-attributes truename
)))
692 (list (buffer-list)) found
)
694 (while (and (not found
) list
)
696 (set-buffer (car list
))
697 (if (and buffer-file-name
698 (equal buffer-file-number number
)
699 ;; Verify this buffer's file number
700 ;; still belongs to its file.
701 (file-exists-p buffer-file-name
)
702 (equal (nthcdr 10 (file-attributes buffer-file-name
))
704 (setq found
(car list
))))
705 (setq list
(cdr list
))))
708 (defun insert-file-contents-literally (filename &optional visit beg end replace
)
709 "Like `insert-file-contents', q.v., but only reads in the file.
710 A buffer may be modified in several ways after reading into the buffer due
711 to advanced Emacs features, such as file-name-handlers, format decoding,
712 find-file-hooks, etc.
713 This function ensures that none of these modifications will take place."
714 (let ((file-name-handler-alist nil
)
716 (after-insert-file-functions nil
)
717 (find-buffer-file-type-function
718 (if (fboundp 'find-buffer-file-type
)
719 (symbol-function 'find-buffer-file-type
)
723 (fset 'find-buffer-file-type
(lambda (filename) t
))
724 (insert-file-contents filename visit beg end replace
))
725 (if find-buffer-file-type-function
726 (fset 'find-buffer-file-type find-buffer-file-type-function
)
727 (fmakunbound 'find-buffer-file-type
)))))
729 (defun find-file-noselect (filename &optional nowarn rawfile
)
730 "Read file FILENAME into a buffer and return the buffer.
731 If a buffer exists visiting FILENAME, return that one, but
732 verify that the file has not changed since visited or saved.
733 The buffer is not selected, just returned to the caller."
735 (abbreviate-file-name
736 (expand-file-name filename
)))
737 (if (file-directory-p filename
)
738 (if find-file-run-dired
739 (dired-noselect (if find-file-visit-truename
740 (abbreviate-file-name (file-truename filename
))
742 (error "%s is a directory" filename
))
743 (let* ((buf (get-file-buffer filename
))
744 (truename (abbreviate-file-name (file-truename filename
)))
745 (number (nthcdr 10 (file-attributes truename
)))
746 ;; Find any buffer for a file which has same truename.
747 (other (and (not buf
) (find-buffer-visiting filename
)))
749 ;; Let user know if there is a buffer with the same truename.
753 (string-equal filename
(buffer-file-name other
))
754 (message "%s and %s are the same file"
755 filename
(buffer-file-name other
)))
756 ;; Optionally also find that buffer.
757 (if (or find-file-existing-other-name find-file-visit-truename
)
761 (verify-visited-file-modtime buf
)
762 (cond ((not (file-exists-p filename
))
763 (error "File %s no longer exists!" filename
))
765 (if (string= (file-name-nondirectory filename
)
768 (if (buffer-modified-p buf
)
769 "File %s changed on disk. Discard your edits? "
770 "File %s changed on disk. Reread from disk? ")
771 (file-name-nondirectory filename
))
773 (if (buffer-modified-p buf
)
774 "File %s changed on disk. Discard your edits in %s? "
775 "File %s changed on disk. Reread from disk into %s? ")
776 (file-name-nondirectory filename
)
780 (revert-buffer t t
)))))
782 ;;; The truename stuff makes this obsolete.
783 ;;; (let* ((link-name (car (file-attributes filename)))
784 ;;; (linked-buf (and (stringp link-name)
785 ;;; (get-file-buffer link-name))))
786 ;;; (if (bufferp linked-buf)
787 ;;; (message "Symbolic link to file in buffer %s"
788 ;;; (buffer-name linked-buf))))
789 (setq buf
(create-file-buffer filename
))
790 (set-buffer-major-mode buf
)
795 (insert-file-contents-literally filename t
)
797 ;; Unconditionally set error
800 (insert-file-contents filename t
)
802 ;; Run find-file-not-found-hooks until one returns non-nil.
803 (or (run-hook-with-args-until-success 'find-file-not-found-hooks
)
804 ;; If they fail too, set error.
806 ;; Find the file's truename, and maybe use that as visited name.
807 (setq buffer-file-truename truename
)
808 (setq buffer-file-number number
)
809 ;; On VMS, we may want to remember which directory in a search list
810 ;; the file was found in.
811 (and (eq system-type
'vax-vms
)
813 (if (string-match ":" (file-name-directory filename
))
814 (setq logical
(substring (file-name-directory filename
)
815 0 (match-beginning 0))))
816 (not (member logical find-file-not-true-dirname-list
)))
817 (setq buffer-file-name buffer-file-truename
))
818 (if find-file-visit-truename
819 (setq buffer-file-name
821 (expand-file-name buffer-file-truename
))))
822 ;; Set buffer's default directory to that of the file.
823 (setq default-directory
(file-name-directory filename
))
824 ;; Turn off backup files for certain file names. Since
825 ;; this is a permanent local, the major mode won't eliminate it.
826 (and (not (funcall backup-enable-predicate buffer-file-name
))
828 (make-local-variable 'backup-inhibited
)
829 (setq backup-inhibited t
)))
832 (after-find-file error
(not nowarn
)))))
835 (defvar after-find-file-from-revert-buffer nil
)
837 (defun after-find-file (&optional error warn noauto
838 after-find-file-from-revert-buffer
840 "Called after finding a file and by the default revert function.
841 Sets buffer mode, parses local variables.
842 Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an
843 error in reading the file. WARN non-nil means warn if there
844 exists an auto-save file more recent than the visited file.
845 NOAUTO means don't mess with auto-save mode.
846 Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil
847 means this call was from `revert-buffer'.
848 Fifth arg NOMODES non-nil means don't alter the file's modes.
849 Finishes by calling the functions in `find-file-hooks'
850 unless NOMODES is non-nil."
851 (setq buffer-read-only
(not (file-writable-p buffer-file-name
)))
856 (cond ((and error
(file-attributes buffer-file-name
))
857 (setq buffer-read-only t
)
858 "File exists, but cannot be read.")
859 ((not buffer-read-only
)
861 (file-newer-than-file-p (make-auto-save-file-name)
863 "Auto save file is newer; consider M-x recover-file"
865 (if error
"(New file)" nil
)))
868 "Note: file is write protected")
869 ((file-attributes (directory-file-name default-directory
))
870 "File not found and directory write-protected")
871 ((file-exists-p (file-name-directory buffer-file-name
))
872 (setq buffer-read-only nil
))
874 (setq buffer-read-only nil
)
875 (if (file-exists-p (file-name-directory (directory-file-name (file-name-directory buffer-file-name
))))
876 "Use M-x make-dir RET RET to create the directory"
877 "Use C-u M-x make-dir RET RET to create directory and its parents")))))
881 (or not-serious
(sit-for 1 nil t
)))))
882 (if (and auto-save-default
(not noauto
))
887 (run-hooks 'find-file-hooks
)))
889 (defun normal-mode (&optional find-file
)
890 "Choose the major mode for this buffer automatically.
891 Also sets up any specified local variables of the file.
892 Uses the visited file name, the -*- line, and the local variables spec.
894 This function is called automatically from `find-file'. In that case,
895 we may set up specified local variables depending on the value of
896 `enable-local-variables': if it is t, we do; if it is nil, we don't;
897 otherwise, we query. `enable-local-variables' is ignored if you
898 run `normal-mode' explicitly."
900 (or find-file
(funcall (or default-major-mode
'fundamental-mode
)))
903 (error (message "File mode specification error: %s"
904 (prin1-to-string err
))))
906 (let ((enable-local-variables (or (not find-file
)
907 enable-local-variables
)))
908 (hack-local-variables))
909 (error (message "File local-variables error: %s"
910 (prin1-to-string err
)))))
912 (defvar auto-mode-alist
913 '(("\\.te?xt\\'" . text-mode
)
916 ("\\.tex\\'" . tex-mode
)
917 ("\\.ltx\\'" . latex-mode
)
918 ("\\.el\\'" . emacs-lisp-mode
)
919 ("\\.mm\\'" . nroff-mode
)
920 ("\\.me\\'" . nroff-mode
)
921 ("\\.ms\\'" . nroff-mode
)
922 ("\\.man\\'" . nroff-mode
)
923 ("\\.scm\\'" . scheme-mode
)
924 ("\\.l\\'" . lisp-mode
)
925 ("\\.lisp\\'" . lisp-mode
)
926 ("\\.f\\'" . fortran-mode
)
927 ("\\.for\\'" . fortran-mode
)
928 ("\\.p\\'" . pascal-mode
)
929 ("\\.pas\\'" . pascal-mode
)
930 ("\\.mss\\'" . scribe-mode
)
931 ("\\.ad[abs]\\'" . ada-mode
)
932 ("\\.icn\\'" . icon-mode
)
933 ("\\.pl\\'" . perl-mode
)
934 ("\\.cc\\'" . c
++-mode
)
935 ("\\.hh\\'" . c
++-mode
)
936 ("\\.C\\'" . c
++-mode
)
937 ("\\.H\\'" . c
++-mode
)
938 ("\\.cpp\\'" . c
++-mode
)
939 ("\\.cxx\\'" . c
++-mode
)
940 ("\\.hxx\\'" . c
++-mode
)
941 ("\\.c\\+\\+\\'" . c
++-mode
)
942 ("\\.h\\+\\+\\'" . c
++-mode
)
943 ("\\.mk\\'" . makefile-mode
)
944 ("[Mm]akefile\\(.in\\)?\\'" . makefile-mode
)
945 ;;; Less common extensions come here
946 ;;; so more common ones above are found faster.
947 ("\\.texinfo\\'" . texinfo-mode
)
948 ("\\.te?xi\\'" . texinfo-mode
)
949 ("\\.s\\'" . asm-mode
)
950 ("\\.S\\'" . asm-mode
)
951 ("\\.asm\\'" . asm-mode
)
952 ("ChangeLog\\'" . change-log-mode
)
953 ("change.log\\'" . change-log-mode
)
954 ("changelo\\'" . change-log-mode
)
955 ("ChangeLog.[0-9]+\\'" . change-log-mode
)
956 ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode
)
957 ("\\.scm\\.[0-9]*\\'" . scheme-mode
)
958 ("\\.c?sh\\'\\|\\.shar\\'\\|/\\.z?profile\\'" . sh-mode
)
959 ("/\\.\\(bash_profile\\|z?login\\|bash_login\\|z?logout\\)\\'" . sh-mode
)
960 ("/\\.\\(bash_logout\\|[kz]shrc\\|bashrc\\|t?cshrc\\|esrc\\)\\'" . sh-mode
)
961 ("/\\.\\([kz]shenv\\|xinitrc\\|startxrc\\|xsession\\)\\'" . sh-mode
)
962 ;;; The following should come after the ChangeLog pattern
963 ;;; for the sake of ChangeLog.1, etc.
964 ;;; and after the .scm.[0-9] pattern too.
965 ("\\.[12345678]\\'" . nroff-mode
)
966 ("\\.TeX\\'" . tex-mode
)
967 ("\\.sty\\'" . latex-mode
)
968 ("\\.cls\\'" . latex-mode
) ;LaTeX 2e class
969 ("\\.bbl\\'" . latex-mode
)
970 ("\\.bib\\'" . bibtex-mode
)
971 ("\\.article\\'" . text-mode
)
972 ("\\.letter\\'" . text-mode
)
973 ("\\.tcl\\'" . tcl-mode
)
974 ("\\.exp\\'" . tcl-mode
)
975 ("\\.f90\\'" . f90-mode
)
976 ("\\.lsp\\'" . lisp-mode
)
977 ("\\.awk\\'" . awk-mode
)
978 ("\\.prolog\\'" . prolog-mode
)
979 ("\\.tar\\'" . tar-mode
)
980 ("\\.\\(arc\\|zip\\|lzh\\|zoo\\)\\'" . archive-mode
)
981 ;; Mailer puts message to be edited in
982 ;; /tmp/Re.... or Message
983 ("\\`/tmp/Re" . text-mode
)
984 ("/Message[0-9]*\\'" . text-mode
)
985 ("/drafts/[0-9]+\\'" . mh-letter-mode
)
986 ;; some news reader is reported to use this
987 ("\\`/tmp/fol/" . text-mode
)
989 ("\\.lex\\'" . c-mode
)
990 ("\\.oak\\'" . scheme-mode
)
991 ("\\.sgml?\\'" . sgml-mode
)
992 ("\\.dtd\\'" . sgml-mode
)
993 ("\\.s?html?\\'" . html-mode
)
994 ;; .emacs following a directory delimiter
995 ;; in either Unix or VMS syntax.
996 ("[]>:/]\\..*emacs\\'" . emacs-lisp-mode
)
997 ;; _emacs following a directory delimiter
999 ("[:/]_emacs\\'" . emacs-lisp-mode
)
1000 ("\\.ml\\'" . lisp-mode
))
1002 Alist of filename patterns vs corresponding major mode functions.
1003 Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL).
1004 \(NON-NIL stands for anything that is not nil; the value does not matter.)
1005 Visiting a file whose name matches REGEXP specifies FUNCTION as the
1006 mode function to use. FUNCTION will be called, unless it is nil.
1008 If the element has the form (REGEXP FUNCTION NON-NIL), then after
1009 calling FUNCTION (if it's not nil), we delete the suffix that matched
1010 REGEXP and search the list again for another match.")
1012 (defconst interpreter-mode-alist
1013 '(("perl" . perl-mode
)
1015 ("wishx" . tcl-mode
)
1017 ("tclsh" . tcl-mode
)
1021 ("scm" . scheme-mode
)
1039 ("tail" . text-mode
)
1040 ("more" . text-mode
)
1041 ("less" . text-mode
)
1043 "Alist mapping interpreter names to major modes.
1044 This alist applies to files whose first line starts with `#!'.
1045 Each element looks like (INTERPRETER . MODE).
1046 The car of each element is compared with
1047 the name of the interpreter specified in the first line.
1048 If it matches, mode MODE is selected.")
1050 (defconst inhibit-first-line-modes-regexps
'("\\.tar\\'")
1051 "List of regexps; if one matches a file name, don't look for `-*-'.")
1053 (defconst inhibit-first-line-modes-suffixes nil
1054 "List of regexps for what to ignore, for `inhibit-first-line-modes-regexps'.
1055 When checking `inhibit-first-line-modes-regexps', we first discard
1056 from the end of the file name anything that matches one of these regexps.")
1058 (defvar user-init-file
1059 "" ; set by command-line
1060 "File name including directory of user's initialization file.")
1062 (defun set-auto-mode ()
1063 "Select major mode appropriate for current buffer.
1064 This checks for a -*- mode tag in the buffer's text,
1065 compares the filename against the entries in `auto-mode-alist',
1066 or checks the interpreter that runs this file against
1067 `interpreter-mode-alist'.
1069 It does not check for the `mode:' local variable in the
1070 Local Variables section of the file; for that, use `hack-local-variables'.
1072 If `enable-local-variables' is nil, this function does not check for a
1074 ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
1075 (let (beg end done modes
)
1077 (goto-char (point-min))
1078 (skip-chars-forward " \t\n")
1079 (and enable-local-variables
1080 ;; Don't look for -*- if this file name matches any
1081 ;; of the regexps in inhibit-first-line-modes-regexps.
1082 (let ((temp inhibit-first-line-modes-regexps
)
1083 (name (if buffer-file-name
1084 (file-name-sans-versions buffer-file-name
)
1086 (while (let ((sufs inhibit-first-line-modes-suffixes
))
1087 (while (and sufs
(not (string-match (car sufs
) name
)))
1088 (setq sufs
(cdr sufs
)))
1090 (setq name
(substring name
0 (match-beginning 0))))
1092 (not (string-match (car temp
) name
)))
1093 (setq temp
(cdr temp
)))
1095 (search-forward "-*-" (save-excursion
1096 ;; If the file begins with "#!"
1097 ;; (exec interpreter magic), look
1098 ;; for mode frobs in the first two
1099 ;; lines. You cannot necessarily
1100 ;; put them in the first line of
1101 ;; such a file without screwing up
1102 ;; the interpreter invocation.
1103 (end-of-line (and (looking-at "^#!") 2))
1106 (skip-chars-forward " \t")
1108 (search-forward "-*-"
1109 (save-excursion (end-of-line) (point))
1113 (skip-chars-backward " \t")
1116 (if (save-excursion (search-forward ":" end t
))
1117 ;; Find all specifications for the `mode:' variable
1118 ;; and execute them left to right.
1119 (while (let ((case-fold-search t
))
1120 (or (and (looking-at "mode:")
1121 (goto-char (match-end 0)))
1122 (re-search-forward "[ \t;]mode:" end t
)))
1123 (skip-chars-forward " \t")
1125 (if (search-forward ";" end t
)
1128 (skip-chars-backward " \t")
1129 (setq modes
(cons (intern (concat (downcase (buffer-substring beg
(point))) "-mode"))
1131 ;; Simple -*-MODE-*- case.
1132 (setq modes
(cons (intern (concat (downcase (buffer-substring beg end
))
1135 ;; If we found modes to use, invoke them now,
1136 ;; outside the save-excursion.
1138 (progn (mapcar 'funcall modes
)
1140 ;; If we didn't find a mode from a -*- line, try using the file name.
1141 (if (and (not done
) buffer-file-name
)
1142 (let ((name buffer-file-name
)
1144 ;; Remove backup-suffixes from file name.
1145 (setq name
(file-name-sans-versions name
))
1147 (setq keep-going nil
)
1148 (let ((alist auto-mode-alist
)
1150 ;; Find first matching alist entry.
1151 (let ((case-fold-search
1152 (memq system-type
'(vax-vms windows-nt
))))
1153 (while (and (not mode
) alist
)
1154 (if (string-match (car (car alist
)) name
)
1155 (if (and (consp (cdr (car alist
)))
1156 (nth 2 (car alist
)))
1158 (setq mode
(car (cdr (car alist
)))
1159 name
(substring name
0 (match-beginning 0))
1161 (setq mode
(cdr (car alist
))
1163 (setq alist
(cdr alist
))))
1166 ;; If we can't deduce a mode from the file name,
1167 ;; look for an interpreter specified in the first line.
1168 ;; As a special case, allow for things like "#!/bin/env perl",
1169 ;; which finds the interpreter anywhere in $PATH.
1172 (goto-char (point-min))
1173 (if (looking-at "#! *\\([^ \t\n]*/bin/env +\\)?\\([^ \t\n]+\\)")
1174 (buffer-substring (match-beginning 2)
1178 ;; Map interpreter name to a mode.
1179 (setq elt
(assoc (file-name-nondirectory interpreter
)
1180 interpreter-mode-alist
))
1182 (funcall (cdr elt
)))))))))))
1184 (defun hack-local-variables-prop-line ()
1185 ;; Set local variables specified in the -*- line.
1186 ;; Ignore any specification for `mode:';
1187 ;; set-auto-mode should already have handled that.
1189 (goto-char (point-min))
1191 (end (save-excursion (end-of-line (and (looking-at "^#!") 2)) (point))))
1192 ;; Parse the -*- line into the `result' alist.
1193 (cond ((not (search-forward "-*-" end t
))
1194 ;; doesn't have one.
1196 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
1197 ;; Simple form: "-*- MODENAME -*-". Already handled.
1200 ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
1201 ;; (last ";" is optional).
1203 (if (search-forward "-*-" end t
)
1204 (setq end
(- (point) 3))
1205 (error "-*- not terminated before end of line")))
1206 (while (< (point) end
)
1207 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
1208 (error "malformed -*- line"))
1209 (goto-char (match-end 0))
1210 ;; There used to be a downcase here,
1211 ;; but the manual didn't say so,
1212 ;; and people want to set var names that aren't all lc.
1213 (let ((key (intern (buffer-substring
1216 (val (save-restriction
1217 (narrow-to-region (point) end
)
1218 (read (current-buffer)))))
1220 (setq result
(cons (cons key val
) result
)))
1221 (skip-chars-forward " \t;")))
1222 (setq result
(nreverse result
))))
1225 (or (eq enable-local-variables t
)
1226 (and enable-local-variables
1227 (save-window-excursion
1229 (switch-to-buffer (current-buffer))
1231 ;; If we fail to switch in the selected window,
1232 ;; it is probably a minibuffer.
1233 ;; So try another window.
1235 (switch-to-buffer-other-window (current-buffer))
1237 (switch-to-buffer-other-frame (current-buffer))))))
1238 (y-or-n-p (format "Set local variables as specified in -*- line of %s? "
1239 (file-name-nondirectory buffer-file-name
)))))))
1241 (hack-one-local-variable (car (car result
)) (cdr (car result
)))
1242 (setq result
(cdr result
)))))))
1244 (defvar hack-local-variables-hook nil
1245 "Normal hook run after processing a file's local variables specs.
1246 Major modes can use this to examine user-specified local variables
1247 in order to initialize other data structure based on them.")
1249 (defun hack-local-variables ()
1250 "Parse and put into effect this buffer's local variables spec."
1251 (hack-local-variables-prop-line)
1252 ;; Look for "Local variables:" line in last page.
1254 (goto-char (point-max))
1255 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move
)
1256 (if (let ((case-fold-search t
))
1257 (and (search-forward "Local Variables:" nil t
)
1258 (or (eq enable-local-variables t
)
1259 (and enable-local-variables
1260 (save-window-excursion
1261 (switch-to-buffer (current-buffer))
1264 (set-window-start (selected-window) (point)))
1265 (y-or-n-p (format "Set local variables as specified at end of %s? "
1266 (if buffer-file-name
1267 (file-name-nondirectory
1270 (buffer-name))))))))))
1272 prefix prefixlen suffix beg
1273 (enable-local-eval enable-local-eval
))
1274 ;; The prefix is what comes before "local variables:" in its line.
1275 ;; The suffix is what comes after "local variables:" in its line.
1276 (skip-chars-forward " \t")
1278 (setq suffix
(buffer-substring (point)
1279 (progn (end-of-line) (point)))))
1280 (goto-char (match-beginning 0))
1283 (buffer-substring (point)
1284 (progn (beginning-of-line) (point)))))
1286 (if prefix
(setq prefixlen
(length prefix
)
1287 prefix
(regexp-quote prefix
)))
1288 (if suffix
(setq suffix
(concat (regexp-quote suffix
) "$")))
1290 ;; Look at next local variable spec.
1291 (if selective-display
(re-search-forward "[\n\C-m]")
1293 ;; Skip the prefix, if any.
1295 (if (looking-at prefix
)
1296 (forward-char prefixlen
)
1297 (error "Local variables entry is missing the prefix")))
1298 ;; Find the variable name; strip whitespace.
1299 (skip-chars-forward " \t")
1301 (skip-chars-forward "^:\n")
1302 (if (eolp) (error "Missing colon in local variables entry"))
1303 (skip-chars-backward " \t")
1304 (let* ((str (buffer-substring beg
(point)))
1307 ;; Setting variable named "end" means end of list.
1308 (if (string-equal (downcase str
) "end")
1310 ;; Otherwise read the variable value.
1311 (skip-chars-forward "^:")
1313 (setq val
(read (current-buffer)))
1314 (skip-chars-backward "\n")
1315 (skip-chars-forward " \t")
1316 (or (if suffix
(looking-at suffix
) (eolp))
1317 (error "Local variables entry is terminated incorrectly"))
1318 ;; Set the variable. "Variables" mode and eval are funny.
1319 (hack-one-local-variable var val
)))))))
1320 (run-hooks 'hack-local-variables-hook
))
1322 (defconst ignored-local-variables
1323 '(enable-local-eval)
1324 "Variables to be ignored in a file's local variable spec.")
1326 ;; Get confirmation before setting these variables as locals in a file.
1327 (put 'debugger
'risky-local-variable t
)
1328 (put 'enable-local-eval
'risky-local-variable t
)
1329 (put 'ignored-local-variables
'risky-local-variable t
)
1330 (put 'eval
'risky-local-variable t
)
1331 (put 'file-name-handler-alist
'risky-local-variable t
)
1332 (put 'minor-mode-map-alist
'risky-local-variable t
)
1333 (put 'after-load-alist
'risky-local-variable t
)
1334 (put 'buffer-file-name
'risky-local-variable t
)
1335 (put 'buffer-auto-save-file-name
'risky-local-variable t
)
1336 (put 'buffer-file-truename
'risky-local-variable t
)
1337 (put 'exec-path
'risky-local-variable t
)
1338 (put 'load-path
'risky-local-variable t
)
1339 (put 'exec-directory
'risky-local-variable t
)
1340 (put 'process-environment
'risky-local-variable t
)
1341 ;; Don't wait for outline.el to be loaded, for the sake of outline-minor-mode.
1342 (put 'outline-level
'risky-local-variable t
)
1343 (put 'rmail-output-file-alist
'risky-local-variable t
)
1345 ;; This one is safe because the user gets to check it before it is used.
1346 (put 'compile-command
'safe-local-variable t
)
1348 (defun hack-one-local-variable-quotep (exp)
1349 (and (consp exp
) (eq (car exp
) 'quote
) (consp (cdr exp
))))
1351 ;; "Set" one variable in a local variables spec.
1352 ;; A few variable names are treated specially.
1353 (defun hack-one-local-variable (var val
)
1354 (cond ((eq var
'mode
)
1355 (funcall (intern (concat (downcase (symbol-name val
))
1357 ((memq var ignored-local-variables
)
1359 ;; "Setting" eval means either eval it or do nothing.
1360 ;; Likewise for setting hook variables.
1361 ((or (get var
'risky-local-variable
)
1363 (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$"
1365 (not (get var
'safe-local-variable
))))
1366 ;; Permit evalling a put of a harmless property.
1367 ;; if the args do nothing tricky.
1368 (if (or (and (eq var
'eval
)
1371 (hack-one-local-variable-quotep (nth 1 val
))
1372 (hack-one-local-variable-quotep (nth 2 val
))
1373 ;; Only allow safe values of lisp-indent-hook;
1375 (or (numberp (nth 3 val
))
1376 (equal (nth 3 val
) ''defun
))
1377 (memq (nth 1 (nth 2 val
))
1378 '(lisp-indent-hook)))
1379 ;; Permit eval if not root and user says ok.
1380 (and (not (zerop (user-uid)))
1381 (or (eq enable-local-eval t
)
1382 (and enable-local-eval
1383 (save-window-excursion
1384 (switch-to-buffer (current-buffer))
1387 (set-window-start (selected-window) (point)))
1388 (setq enable-local-eval
1389 (y-or-n-p (format "Process `eval' or hook local variables in file %s? "
1390 (file-name-nondirectory buffer-file-name
)))))))))
1392 (save-excursion (eval val
))
1393 (make-local-variable var
)
1395 (message "Ignoring `eval:' in file's local variables")))
1396 ;; Ordinary variable, really set it.
1397 (t (make-local-variable var
)
1401 (defun set-visited-file-name (filename)
1402 "Change name of file visited in current buffer to FILENAME.
1403 The next time the buffer is saved it will go in the newly specified file.
1404 nil or empty string as argument means make buffer not be visiting any file.
1405 Remember to delete the initial contents of the minibuffer
1406 if you wish to pass an empty string as the argument."
1407 (interactive "FSet visited file name: ")
1408 (if (buffer-base-buffer)
1409 (error "An indirect buffer cannot visit a file"))
1413 (if (string-equal filename
"")
1415 (expand-file-name filename
))))
1418 (setq truename
(file-truename filename
))
1419 (if find-file-visit-truename
1420 (setq filename truename
))))
1421 (or (equal filename buffer-file-name
)
1423 (and filename
(lock-buffer filename
))
1425 (setq buffer-file-name filename
)
1426 (if filename
; make buffer name reflect filename.
1427 (let ((new-name (file-name-nondirectory buffer-file-name
)))
1428 (if (string= new-name
"")
1429 (error "Empty file name"))
1430 (if (eq system-type
'vax-vms
)
1431 (setq new-name
(downcase new-name
)))
1432 (setq default-directory
(file-name-directory buffer-file-name
))
1433 (or (string= new-name
(buffer-name))
1434 (rename-buffer new-name t
))))
1435 (setq buffer-backed-up nil
)
1436 (clear-visited-file-modtime)
1437 ;; Abbreviate the file names of the buffer.
1440 (setq buffer-file-truename
(abbreviate-file-name truename
))
1441 (if find-file-visit-truename
1442 (setq buffer-file-name buffer-file-truename
))))
1443 (setq buffer-file-number
1445 (nthcdr 10 (file-attributes buffer-file-name
))
1447 ;; write-file-hooks is normally used for things like ftp-find-file
1448 ;; that visit things that are not local files as if they were files.
1449 ;; Changing to visit an ordinary local file instead should flush the hook.
1450 (kill-local-variable 'write-file-hooks
)
1451 (kill-local-variable 'local-write-file-hooks
)
1452 (kill-local-variable 'revert-buffer-function
)
1453 (kill-local-variable 'backup-inhibited
)
1454 ;; If buffer was read-only because of version control,
1455 ;; that reason is gone now, so make it writable.
1457 (setq buffer-read-only nil
))
1458 (kill-local-variable 'vc-mode
)
1459 ;; Turn off backup files for certain file names.
1460 ;; Since this is a permanent local, the major mode won't eliminate it.
1461 (and (not (funcall backup-enable-predicate buffer-file-name
))
1463 (make-local-variable 'backup-inhibited
)
1464 (setq backup-inhibited t
)))
1465 (let ((oauto buffer-auto-save-file-name
))
1466 ;; If auto-save was not already on, turn it on if appropriate.
1467 (if (not buffer-auto-save-file-name
)
1468 (and buffer-file-name auto-save-default
1470 ;; If auto save is on, start using a new name.
1471 ;; We deliberately don't rename or delete the old auto save
1472 ;; for the old visited file name. This is because perhaps
1473 ;; the user wants to save the new state and then compare with the
1474 ;; previous state from the auto save file.
1475 (setq buffer-auto-save-file-name
1476 (make-auto-save-file-name)))
1477 ;; Rename the old auto save file if any.
1478 (and oauto buffer-auto-save-file-name
1479 (file-exists-p oauto
)
1480 (rename-file oauto buffer-auto-save-file-name t
)))
1481 (if buffer-file-name
1482 (set-buffer-modified-p t
)))
1484 (defun write-file (filename &optional confirm
)
1485 "Write current buffer into file FILENAME.
1486 Makes buffer visit that file, and marks it not modified.
1487 If the buffer is already visiting a file, you can specify
1488 a directory name as FILENAME, to write a file of the same
1489 old name in that directory.
1490 If optional second arg CONFIRM is non-nil,
1491 ask for confirmation for overwriting an existing file."
1492 ;; (interactive "FWrite file: ")
1494 (list (if buffer-file-name
1495 (read-file-name "Write file: "
1497 (read-file-name "Write file: "
1498 (cdr (assq 'default-directory
1499 (buffer-local-variables)))
1500 nil nil
(buffer-name)))
1502 (or (null filename
) (string-equal filename
"")
1504 ;; If arg is just a directory,
1505 ;; use same file name, but in that directory.
1506 (if (and (file-directory-p filename
) buffer-file-name
)
1507 (setq filename
(concat (file-name-as-directory filename
)
1508 (file-name-nondirectory buffer-file-name
))))
1510 (file-exists-p filename
)
1511 (or (y-or-n-p (format "File `%s' exists; overwrite? " filename
))
1512 (error "Canceled")))
1513 (set-visited-file-name filename
)))
1514 (set-buffer-modified-p t
)
1517 (defun backup-buffer ()
1518 "Make a backup of the disk file visited by the current buffer, if appropriate.
1519 This is normally done before saving the buffer the first time.
1520 If the value is non-nil, it is the result of `file-modes' on the original
1521 file; this means that the caller, after saving the buffer, should change
1522 the modes of the new file to agree with the old modes."
1523 (if (and make-backup-files
(not backup-inhibited
)
1524 (not buffer-backed-up
)
1525 (file-exists-p buffer-file-name
)
1526 (memq (aref (elt (file-attributes buffer-file-name
) 8) 0)
1528 (let ((real-file-name buffer-file-name
)
1529 backup-info backupname targets setmodes
)
1530 ;; If specified name is a symbolic link, chase it to the target.
1531 ;; Thus we make the backups in the directory where the real file is.
1532 (setq real-file-name
(file-chase-links real-file-name
))
1533 (setq backup-info
(find-backup-file-name real-file-name
)
1534 backupname
(car backup-info
)
1535 targets
(cdr backup-info
))
1536 ;;; (if (file-directory-p buffer-file-name)
1537 ;;; (error "Cannot save buffer in directory %s" buffer-file-name))
1540 (let ((delete-old-versions
1541 ;; If have old versions to maybe delete,
1542 ;; ask the user to confirm now, before doing anything.
1543 ;; But don't actually delete til later.
1545 (or (eq delete-old-versions t
) (eq delete-old-versions nil
))
1546 (or delete-old-versions
1547 (y-or-n-p (format "Delete excess backup versions of %s? "
1548 real-file-name
))))))
1549 ;; Actually write the back up file.
1551 (if (or file-precious-flag
1552 ; (file-symlink-p buffer-file-name)
1554 (and backup-by-copying-when-linked
1555 (> (file-nlinks real-file-name
) 1))
1556 (and backup-by-copying-when-mismatch
1557 (let ((attr (file-attributes real-file-name
)))
1559 (not (file-ownership-preserved-p real-file-name
))))))
1561 (copy-file real-file-name backupname t t
)
1563 ;; If copying fails because file BACKUPNAME
1564 ;; is not writable, delete that file and try again.
1565 (if (and (file-exists-p backupname
)
1566 (not (file-writable-p backupname
)))
1567 (delete-file backupname
))
1568 (copy-file real-file-name backupname t t
)))
1569 ;; rename-file should delete old backup.
1570 (rename-file real-file-name backupname t
)
1571 (setq setmodes
(file-modes backupname
)))
1573 ;; If trouble writing the backup, write it in ~.
1574 (setq backupname
(expand-file-name
1575 (convert-standard-filename
1577 (message "Cannot write backup file; backing up in %s"
1578 (file-name-nondirectory backupname
))
1581 (copy-file real-file-name backupname t t
)
1583 ;; If copying fails because file BACKUPNAME
1584 ;; is not writable, delete that file and try again.
1585 (if (and (file-exists-p backupname
)
1586 (not (file-writable-p backupname
)))
1587 (delete-file backupname
))
1588 (copy-file real-file-name backupname t t
)))))
1589 (setq buffer-backed-up t
)
1590 ;; Now delete the old versions, if desired.
1591 (if delete-old-versions
1594 (delete-file (car targets
))
1596 (setq targets
(cdr targets
))))
1598 (file-error nil
))))))
1600 (defun file-name-sans-versions (name &optional keep-backup-version
)
1601 "Return FILENAME sans backup versions or strings.
1602 This is a separate procedure so your site-init or startup file can
1604 If the optional argument KEEP-BACKUP-VERSION is non-nil,
1605 we do not remove backup version numbers, only true file version numbers."
1606 (let ((handler (find-file-name-handler name
'file-name-sans-versions
)))
1608 (funcall handler
'file-name-sans-versions name keep-backup-version
)
1610 (if (eq system-type
'vax-vms
)
1611 ;; VMS version number is (a) semicolon, optional
1612 ;; sign, zero or more digits or (b) period, option
1613 ;; sign, zero or more digits, provided this is the
1614 ;; second period encountered outside of the
1615 ;; device/directory part of the file name.
1616 (or (string-match ";[-+]?[0-9]*\\'" name
)
1617 (if (string-match "\\.[^]>:]*\\(\\.[-+]?[0-9]*\\)\\'"
1619 (match-beginning 1))
1621 (if keep-backup-version
1623 (or (string-match "\\.~[0-9.]+~\\'" name
)
1624 (string-match "~\\'" name
)
1625 (length name
))))))))
1627 (defun file-ownership-preserved-p (file)
1628 "Returns t if deleting FILE and rewriting it would preserve the owner."
1629 (let ((handler (find-file-name-handler file
'file-ownership-preserved-p
)))
1631 (funcall handler
'file-ownership-preserved-p file
)
1632 (let ((attributes (file-attributes file
)))
1633 ;; Return t if the file doesn't exist, since it's true that no
1634 ;; information would be lost by an (attempted) delete and create.
1635 (or (null attributes
)
1636 (= (nth 2 attributes
) (user-uid)))))))
1638 (defun file-name-sans-extension (filename)
1639 "Return FILENAME sans final \"extension\".
1640 The extension, in a file name, is the part that follows the last `.'."
1642 (let ((file (file-name-sans-versions (file-name-nondirectory filename
)))
1644 (if (string-match "\\.[^.]*\\'" file
)
1645 (if (setq directory
(file-name-directory filename
))
1646 (expand-file-name (substring file
0 (match-beginning 0))
1648 (substring file
0 (match-beginning 0)))
1651 (defun make-backup-file-name (file)
1652 "Create the non-numeric backup file name for FILE.
1653 This is a separate function so you can redefine it for customization."
1654 (if (eq system-type
'ms-dos
)
1655 (let ((fn (file-name-nondirectory file
)))
1656 (concat (file-name-directory file
)
1658 (and (string-match "\\`[^.]+\\'" fn
)
1659 (concat (match-string 0 fn
) ".~"))
1660 (and (string-match "\\`[^.]+\\.\\(..?\\)?" fn
)
1661 (concat (match-string 0 fn
) "~")))))
1664 (defun backup-file-name-p (file)
1665 "Return non-nil if FILE is a backup file name (numeric or not).
1666 This is a separate function so you can redefine it for customization.
1667 You may need to redefine `file-name-sans-versions' as well."
1668 (string-match "~\\'" file
))
1670 ;; This is used in various files.
1671 ;; The usage of bv-length is not very clean,
1672 ;; but I can't see a good alternative,
1673 ;; so as of now I am leaving it alone.
1674 (defun backup-extract-version (fn)
1675 "Given the name of a numeric backup file, return the backup number.
1676 Uses the free variable `bv-length', whose value should be
1677 the index in the name where the version number begins."
1678 (if (and (string-match "[0-9]+~$" fn bv-length
)
1679 (= (match-beginning 0) bv-length
))
1680 (string-to-int (substring fn bv-length -
1))
1683 ;; I believe there is no need to alter this behavior for VMS;
1684 ;; since backup files are not made on VMS, it should not get called.
1685 (defun find-backup-file-name (fn)
1686 "Find a file name for a backup file, and suggestions for deletions.
1687 Value is a list whose car is the name for the backup file
1688 and whose cdr is a list of old versions to consider deleting now.
1689 If the value is nil, don't make a backup."
1690 (let ((handler (find-file-name-handler fn
'find-backup-file-name
)))
1691 ;; Run a handler for this function so that ange-ftp can refuse to do it.
1693 (funcall handler
'find-backup-file-name fn
)
1694 (if (eq version-control
'never
)
1695 (list (make-backup-file-name fn
))
1696 (let* ((base-versions (concat (file-name-nondirectory fn
) ".~"))
1697 (bv-length (length base-versions
))
1701 (deserve-versions-p nil
)
1702 (number-to-delete 0))
1704 (setq possibilities
(file-name-all-completions
1706 (file-name-directory fn
))
1707 versions
(sort (mapcar
1708 (function backup-extract-version
)
1711 high-water-mark
(apply 'max
0 versions
)
1712 deserve-versions-p
(or version-control
1713 (> high-water-mark
0))
1714 number-to-delete
(- (length versions
)
1715 kept-old-versions kept-new-versions -
1))
1717 (setq possibilities nil
)))
1718 (if (not deserve-versions-p
)
1719 (list (make-backup-file-name fn
))
1720 (cons (concat fn
".~" (int-to-string (1+ high-water-mark
)) "~")
1721 (if (and (> number-to-delete
0)
1722 ;; Delete nothing if there is overflow
1723 ;; in the number of versions to keep.
1724 (>= (+ kept-new-versions kept-old-versions -
1) 0))
1725 (mapcar (function (lambda (n)
1726 (concat fn
".~" (int-to-string n
) "~")))
1727 (let ((v (nthcdr kept-old-versions versions
)))
1728 (rplacd (nthcdr (1- number-to-delete
) v
) ())
1731 (defun file-nlinks (filename)
1732 "Return number of names file FILENAME has."
1733 (car (cdr (file-attributes filename
))))
1735 (defun file-relative-name (filename &optional directory
)
1736 "Convert FILENAME to be relative to DIRECTORY (default: default-directory)."
1737 (setq filename
(expand-file-name filename
)
1738 directory
(file-name-as-directory (expand-file-name
1739 (or directory default-directory
))))
1740 (let ((ancestor ""))
1741 (while (not (string-match (concat "^" (regexp-quote directory
)) filename
))
1742 (setq directory
(file-name-directory (substring directory
0 -
1))
1743 ancestor
(concat "../" ancestor
)))
1744 (concat ancestor
(substring filename
(match-end 0)))))
1746 (defun save-buffer (&optional args
)
1747 "Save current buffer in visited file if modified. Versions described below.
1748 By default, makes the previous version into a backup file
1749 if previously requested or if this is the first save.
1750 With 1 \\[universal-argument], marks this version
1751 to become a backup when the next save is done.
1752 With 2 \\[universal-argument]'s,
1753 unconditionally makes the previous version into a backup file.
1754 With 3 \\[universal-argument]'s, marks this version
1755 to become a backup when the next save is done,
1756 and unconditionally makes the previous version into a backup file.
1758 With argument of 0, never makes the previous version into a backup file.
1760 If a file's name is FOO, the names of its numbered backup versions are
1761 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~.
1762 Numeric backups (rather than FOO~) will be made if value of
1763 `version-control' is not the atom `never' and either there are already
1764 numeric versions of the file being backed up, or `version-control' is
1766 We don't want excessive versions piling up, so there are variables
1767 `kept-old-versions', which tells Emacs how many oldest versions to keep,
1768 and `kept-new-versions', which tells how many newest versions to keep.
1769 Defaults are 2 old versions and 2 new.
1770 `dired-kept-versions' controls dired's clean-directory (.) command.
1771 If `delete-old-versions' is nil, system will query user
1772 before trimming versions. Otherwise it does it silently."
1774 (let ((modp (buffer-modified-p))
1775 (large (> (buffer-size) 50000))
1776 (make-backup-files (or (and make-backup-files
(not (eq args
0)))
1777 (memq args
'(16 64)))))
1778 (and modp
(memq args
'(16 64)) (setq buffer-backed-up nil
))
1779 (if (and modp large
) (message "Saving file %s..." (buffer-file-name)))
1781 (and modp
(memq args
'(4 64)) (setq buffer-backed-up nil
))))
1783 (defun delete-auto-save-file-if-necessary (&optional force
)
1784 "Delete auto-save file for current buffer if `delete-auto-save-files' is t.
1785 Normally delete only if the file was written by this Emacs since
1786 the last real save, but optional arg FORCE non-nil means delete anyway."
1787 (and buffer-auto-save-file-name delete-auto-save-files
1788 (not (string= buffer-file-name buffer-auto-save-file-name
))
1789 (or force
(recent-auto-save-p))
1792 (delete-file buffer-auto-save-file-name
)
1794 (set-buffer-auto-saved))))
1796 (defvar after-save-hook nil
1797 "Normal hook that is run after a buffer is saved to its file.")
1799 (defun basic-save-buffer ()
1800 "Save the current buffer in its visited file, if it has been modified.
1801 After saving the buffer, run `after-save-hook'."
1804 ;; In an indirect buffer, save its base buffer instead.
1805 (if (buffer-base-buffer)
1806 (set-buffer (buffer-base-buffer)))
1807 (if (buffer-modified-p)
1808 (let ((recent-save (recent-auto-save-p))
1809 setmodes tempsetmodes
)
1810 ;; On VMS, rename file and buffer to get rid of version number.
1811 (if (and (eq system-type
'vax-vms
)
1812 (not (string= buffer-file-name
1813 (file-name-sans-versions buffer-file-name
))))
1814 (let (buffer-new-name)
1815 ;; Strip VMS version number before save.
1816 (setq buffer-file-name
1817 (file-name-sans-versions buffer-file-name
))
1818 ;; Construct a (unique) buffer name to correspond.
1819 (let ((buf (create-file-buffer (downcase buffer-file-name
))))
1820 (setq buffer-new-name
(buffer-name buf
))
1822 (rename-buffer buffer-new-name
)))
1823 ;; If buffer has no file name, ask user for one.
1824 (or buffer-file-name
1827 (read-file-name "File to save in: ") nil
)))
1828 (and (file-exists-p filename
)
1829 (or (y-or-n-p (format "File `%s' exists; overwrite? "
1831 (error "Canceled")))
1832 (set-visited-file-name filename
)))
1833 (or (verify-visited-file-modtime (current-buffer))
1834 (not (file-exists-p buffer-file-name
))
1836 (format "%s has changed since visited or saved. Save anyway? "
1837 (file-name-nondirectory buffer-file-name
)))
1838 (error "Save not confirmed"))
1841 (and (> (point-max) 1)
1842 (/= (char-after (1- (point-max))) ?
\n)
1843 (not (and (eq selective-display t
)
1844 (= (char-after (1- (point-max))) ?
\r)))
1845 (or (eq require-final-newline t
)
1846 (and require-final-newline
1848 (format "Buffer %s does not end in newline. Add one? "
1851 (goto-char (point-max))
1853 (or (run-hook-with-args-until-success 'write-contents-hooks
)
1854 (run-hook-with-args-until-success 'local-write-file-hooks
)
1855 (run-hook-with-args-until-success 'write-file-hooks
)
1856 ;; If a hook returned t, file is already "written".
1857 ;; Otherwise, write it the usual way now.
1858 (setq setmodes
(basic-save-buffer-1)))
1859 (setq buffer-file-number
1860 (nthcdr 10 (file-attributes buffer-file-name
)))
1863 (set-file-modes buffer-file-name setmodes
)
1865 ;; If the auto-save file was recent before this command,
1867 (delete-auto-save-file-if-necessary recent-save
)
1868 ;; Support VC `implicit' locking.
1870 (run-hooks 'after-save-hook
))
1871 (message "(No changes need to be saved)"))))
1873 ;; This does the "real job" of writing a buffer into its visited file
1874 ;; and making a backup file. This is what is normally done
1875 ;; but inhibited if one of write-file-hooks returns non-nil.
1876 ;; It returns a value to store in setmodes.
1877 (defun basic-save-buffer-1 ()
1878 (let (tempsetmodes setmodes
)
1879 (if (not (file-writable-p buffer-file-name
))
1880 (let ((dir (file-name-directory buffer-file-name
)))
1881 (if (not (file-directory-p dir
))
1882 (error "%s is not a directory" dir
)
1883 (if (not (file-exists-p buffer-file-name
))
1884 (error "Directory %s write-protected" dir
)
1886 (format "File %s is write-protected; try to save anyway? "
1887 (file-name-nondirectory
1889 (setq tempsetmodes t
)
1890 (error "Attempt to save to a file which you aren't allowed to write"))))))
1891 (or buffer-backed-up
1892 (setq setmodes
(backup-buffer)))
1893 (let ((dir (file-name-directory buffer-file-name
)))
1894 (if (and file-precious-flag
1895 (file-writable-p dir
))
1896 ;; If file is precious, write temp name, then rename it.
1897 ;; This requires write access to the containing dir,
1898 ;; which is why we don't try it if we don't have that access.
1899 (let ((realname buffer-file-name
)
1900 tempname temp nogood i succeed
1901 (old-modtime (visited-file-modtime)))
1904 ;; Find the temporary name to write under.
1906 (setq tempname
(format
1907 (if (eq system-type
'ms-dos
)
1908 "%s#%d.tm#" ; MSDOS limits files to 8+3
1911 (setq nogood
(file-exists-p tempname
))
1914 (progn (clear-visited-file-modtime)
1915 (write-region (point-min) (point-max)
1916 tempname nil realname
1917 buffer-file-truename
)
1919 ;; If writing the temp file fails,
1920 ;; delete the temp file.
1923 (delete-file tempname
)
1924 (set-visited-file-modtime old-modtime
))))
1925 ;; Since we have created an entirely new file
1926 ;; and renamed it, make sure it gets the
1927 ;; right permission bits set.
1928 (setq setmodes
(file-modes buffer-file-name
))
1929 ;; We succeeded in writing the temp file,
1931 (rename-file tempname buffer-file-name t
))
1932 ;; If file not writable, see if we can make it writable
1933 ;; temporarily while we write it.
1934 ;; But no need to do so if we have just backed it up
1935 ;; (setmodes is set) because that says we're superseding.
1936 (cond ((and tempsetmodes
(not setmodes
))
1937 ;; Change the mode back, after writing.
1938 (setq setmodes
(file-modes buffer-file-name
))
1939 (set-file-modes buffer-file-name
511)))
1940 (write-region (point-min) (point-max)
1941 buffer-file-name nil t buffer-file-truename
)))
1944 (defun save-some-buffers (&optional arg exiting
)
1945 "Save some modified file-visiting buffers. Asks user about each one.
1946 Optional argument (the prefix) non-nil means save all with no questions.
1947 Optional second argument EXITING means ask about certain non-file buffers
1948 as well as about file buffers."
1950 (save-window-excursion
1951 (let* ((queried nil
)
1956 (and (buffer-modified-p buffer
)
1957 (not (buffer-base-buffer buffer
))
1959 (buffer-file-name buffer
)
1963 (and buffer-offer-save
(> (buffer-size) 0)))))
1967 (if (buffer-file-name buffer
)
1968 (format "Save file %s? "
1969 (buffer-file-name buffer
))
1970 (format "Save buffer %s? "
1971 (buffer-name buffer
)))))))
1977 '("buffer" "buffers" "save")
1978 (list (list ?\C-r
(lambda (buf)
1980 (setq view-exit-action
1982 (exit-recursive-edit)))
1984 ;; Return nil to ask about BUF again.
1986 "display the current buffer"))))
1988 (and save-abbrevs abbrevs-changed
1991 (y-or-n-p (format "Save abbrevs in %s? "
1993 (write-abbrev-file nil
))
1994 ;; Don't keep bothering user if he says no.
1995 (setq abbrevs-changed nil
)
1997 (or queried
(> files-done
0) abbrevs-done
1998 (message "(No files need saving)")))))
2000 (defun not-modified (&optional arg
)
2001 "Mark current buffer as unmodified, not needing to be saved.
2002 With prefix arg, mark buffer as modified, so \\[save-buffer] will save.
2004 It is not a good idea to use this function in Lisp programs, because it
2005 prints a message in the minibuffer. Instead, use `set-buffer-modified-p'."
2007 (message (if arg
"Modification-flag set"
2008 "Modification-flag cleared"))
2009 (set-buffer-modified-p arg
))
2011 (defun toggle-read-only (&optional arg
)
2012 "Change whether this buffer is visiting its file read-only.
2013 With arg, set read-only iff arg is positive."
2015 (setq buffer-read-only
2017 (not buffer-read-only
)
2018 (> (prefix-numeric-value arg
) 0)))
2019 (force-mode-line-update))
2021 (defun insert-file (filename)
2022 "Insert contents of file FILENAME into buffer after point.
2023 Set mark after the inserted text.
2025 This function is meant for the user to run interactively.
2026 Don't call it from programs! Use `insert-file-contents' instead.
2027 \(Its calling sequence is different; see its documentation)."
2028 (interactive "*fInsert file: ")
2029 (if (file-directory-p filename
)
2030 (signal 'file-error
(list "Opening input file" "file is a directory"
2032 (let ((tem (insert-file-contents filename
)))
2033 (push-mark (+ (point) (car (cdr tem
))))))
2035 (defun append-to-file (start end filename
)
2036 "Append the contents of the region to the end of file FILENAME.
2037 When called from a function, expects three arguments,
2038 START, END and FILENAME. START and END are buffer positions
2039 saying what text to write."
2040 (interactive "r\nFAppend to file: ")
2041 (write-region start end filename t
))
2043 (defun file-newest-backup (filename)
2044 "Return most recent backup file for FILENAME or nil if no backups exist."
2045 (let* ((filename (expand-file-name filename
))
2046 (file (file-name-nondirectory filename
))
2047 (dir (file-name-directory filename
))
2048 (comp (file-name-all-completions file dir
))
2051 (setq file
(concat dir
(car comp
))
2053 (if (and (backup-file-name-p file
)
2054 (or (null newest
) (file-newer-than-file-p file newest
)))
2055 (setq newest file
)))
2058 (defun rename-uniquely ()
2059 "Rename current buffer to a similar name not already taken.
2060 This function is useful for creating multiple shell process buffers
2061 or multiple mail buffers, etc."
2064 (let* ((base-name (if (and (string-match "<[0-9]+>\\'" (buffer-name))
2065 (not (and buffer-file-name
2066 (string= (buffer-name)
2067 (file-name-nondirectory
2068 buffer-file-name
)))))
2069 ;; If the existing buffer name has a <NNN>,
2070 ;; which isn't part of the file name (if any),
2071 ;; then get rid of that.
2072 (substring (buffer-name) 0 (match-beginning 0))
2074 (new-buf (generate-new-buffer base-name
))
2075 (name (buffer-name new-buf
)))
2076 (kill-buffer new-buf
)
2077 (rename-buffer name
)
2078 (force-mode-line-update))))
2080 (defun make-directory (dir &optional parents
)
2081 "Create the directory DIR and any nonexistent parent dirs.
2082 Interactively, the default choice of directory to create
2083 is the current default directory for file names.
2084 That is useful when you have visited a file in a nonexistent directory.
2086 Noninteractively, the second (optional) argument PARENTS says whether
2087 to create parent directories if they don't exist."
2089 (list (read-file-name "Make directory: " default-directory default-directory
2092 (let ((handler (find-file-name-handler dir
'make-directory
)))
2094 (funcall handler
'make-directory dir parents
)
2096 (make-directory-internal dir
)
2097 (let ((dir (directory-file-name (expand-file-name dir
)))
2099 (while (not (file-exists-p dir
))
2100 (setq create-list
(cons dir create-list
)
2101 dir
(directory-file-name (file-name-directory dir
))))
2103 (make-directory-internal (car create-list
))
2104 (setq create-list
(cdr create-list
))))))))
2106 (put 'revert-buffer-function
'permanent-local t
)
2107 (defvar revert-buffer-function nil
2108 "Function to use to revert this buffer, or nil to do the default.
2109 The function receives two arguments IGNORE-AUTO and NOCONFIRM,
2110 which are the arguments that `revert-buffer' received.")
2112 (put 'revert-buffer-insert-file-contents-function
'permanent-local t
)
2113 (defvar revert-buffer-insert-file-contents-function nil
2114 "Function to use to insert contents when reverting this buffer.
2115 Gets two args, first the nominal file name to use,
2116 and second, t if reading the auto-save file.")
2118 (defvar before-revert-hook nil
2119 "Normal hook for `revert-buffer' to run before reverting.
2120 If `revert-buffer-function' is used to override the normal revert
2121 mechanism, this hook is not used.")
2123 (defvar after-revert-hook nil
2124 "Normal hook for `revert-buffer' to run after reverting.
2125 Note that the hook value that it runs is the value that was in effect
2126 before reverting; that makes a difference if you have buffer-local
2129 If `revert-buffer-function' is used to override the normal revert
2130 mechanism, this hook is not used.")
2132 (defun revert-buffer (&optional ignore-auto noconfirm preserve-modes
)
2133 "Replace the buffer text with the text of the visited file on disk.
2134 This undoes all changes since the file was visited or saved.
2135 With a prefix argument, offer to revert from latest auto-save file, if
2136 that is more recent than the visited file.
2138 When called from Lisp, the first argument is IGNORE-AUTO; only offer
2139 to revert from the auto-save file when this is nil. Note that the
2140 sense of this argument is the reverse of the prefix argument, for the
2141 sake of backward compatibility. IGNORE-AUTO is optional, defaulting
2144 Optional second argument NOCONFIRM means don't ask for confirmation at
2147 If the value of `revert-buffer-function' is non-nil, it is called to
2150 The default revert function runs the hook `before-revert-hook' at the
2151 beginning and `after-revert-hook' at the end."
2152 ;; I admit it's odd to reverse the sense of the prefix argument, but
2153 ;; there is a lot of code out there which assumes that the first
2154 ;; argument should be t to avoid consulting the auto-save file, and
2155 ;; there's no straightforward way to encourage authors to notice a
2156 ;; reversal of the argument sense. So I'm just changing the user
2157 ;; interface, but leaving the programmatic interface the same.
2158 (interactive (list (not current-prefix-arg
)))
2159 (if revert-buffer-function
2160 (funcall revert-buffer-function ignore-auto noconfirm
)
2161 (let* ((opoint (point))
2162 (auto-save-p (and (not ignore-auto
)
2163 (recent-auto-save-p)
2164 buffer-auto-save-file-name
2165 (file-readable-p buffer-auto-save-file-name
)
2167 "Buffer has been auto-saved recently. Revert from auto-save file? ")))
2168 (file-name (if auto-save-p
2169 buffer-auto-save-file-name
2171 (cond ((null file-name
)
2172 (error "Buffer does not seem to be associated with any file"))
2174 (yes-or-no-p (format "Revert buffer from file %s? "
2176 (run-hooks 'before-revert-hook
)
2177 ;; If file was backed up but has changed since,
2178 ;; we shd make another backup.
2179 (and (not auto-save-p
)
2180 (not (verify-visited-file-modtime (current-buffer)))
2181 (setq buffer-backed-up nil
))
2182 ;; Get rid of all undo records for this buffer.
2183 (or (eq buffer-undo-list t
)
2184 (setq buffer-undo-list nil
))
2185 ;; Effectively copy the after-revert-hook status,
2186 ;; since after-find-file will clobber it.
2187 (let ((global-hook (default-value 'after-revert-hook
))
2188 (local-hook-p (local-variable-p 'after-revert-hook
))
2189 (local-hook (and (local-variable-p 'after-revert-hook
)
2190 after-revert-hook
)))
2191 (let (buffer-read-only
2192 ;; Don't make undo records for the reversion.
2193 (buffer-undo-list t
))
2194 (if revert-buffer-insert-file-contents-function
2195 (funcall revert-buffer-insert-file-contents-function
2196 file-name auto-save-p
)
2197 (if (not (file-exists-p file-name
))
2198 (error "File %s no longer exists!" file-name
))
2199 ;; Bind buffer-file-name to nil
2200 ;; so that we don't try to lock the file.
2201 (let ((buffer-file-name nil
))
2205 (insert-file-contents file-name
(not auto-save-p
)
2207 (goto-char (min opoint
(point-max)))
2208 ;; Recompute the truename in case changes in symlinks
2209 ;; have changed the truename.
2210 (setq buffer-file-truename
2211 (abbreviate-file-name (file-truename buffer-file-name
)))
2212 (after-find-file nil nil t t preserve-modes
)
2213 ;; Run after-revert-hook as it was before we reverted.
2214 (setq-default revert-buffer-internal-hook global-hook
)
2217 (make-local-variable 'revert-buffer-internal-hook
)
2218 (setq revert-buffer-internal-hook local-hook
))
2219 (kill-local-variable 'revert-buffer-internal-hook
))
2220 (run-hooks 'revert-buffer-internal-hook
))
2223 (defun recover-file (file)
2224 "Visit file FILE, but get contents from its last auto-save file."
2225 ;; Actually putting the file name in the minibuffer should be used
2227 ;; Not just because users often use the default.
2228 (interactive "FRecover file: ")
2229 (setq file
(expand-file-name file
))
2230 (if (auto-save-file-name-p (file-name-nondirectory file
))
2231 (error "%s is an auto-save file" file
))
2232 (let ((file-name (let ((buffer-file-name file
))
2233 (make-auto-save-file-name))))
2234 (cond ((if (file-exists-p file
)
2235 (not (file-newer-than-file-p file-name file
))
2236 (not (file-exists-p file-name
)))
2237 (error "Auto-save file %s not current" file-name
))
2238 ((save-window-excursion
2239 (if (not (eq system-type
'vax-vms
))
2240 (with-output-to-temp-buffer "*Directory*"
2241 (buffer-disable-undo standard-output
)
2242 (call-process "ls" nil standard-output nil
2243 (if (file-symlink-p file
) "-lL" "-l")
2245 (yes-or-no-p (format "Recover auto save file %s? " file-name
)))
2246 (switch-to-buffer (find-file-noselect file t
))
2247 (let ((buffer-read-only nil
))
2249 (insert-file-contents file-name nil
))
2250 (after-find-file nil nil t
))
2251 (t (error "Recover-file cancelled.")))))
2253 (defun recover-session ()
2254 "Recover auto save files from a previous Emacs session.
2255 This command first displays a Dired buffer showing you the
2256 previous sessions that you could recover from.
2257 To choose one, move point to the proper line and then type C-c C-c.
2258 Then you'll be asked about a number of files to recover."
2260 (let ((ls-lisp-support-shell-wildcards t
))
2261 (dired (concat auto-save-list-file-prefix
"*")))
2262 (goto-char (point-min))
2263 (or (looking-at "Move to the session you want to recover,")
2264 (let ((inhibit-read-only t
))
2265 (insert "Move to the session you want to recover,\n"
2266 "then type C-c C-c to select it.\n\n"
2267 "You can also delete some of these files;\n"
2268 "type d on a line to mark that file for deletion.\n\n")))
2269 (use-local-map (nconc (make-sparse-keymap) (current-local-map)))
2270 (define-key (current-local-map) "\C-c\C-c" 'recover-session-finish
))
2272 (defun recover-session-finish ()
2273 "Choose one saved session to recover auto-save files from.
2274 This command is used in the special Dired buffer created by
2275 \\[recover-session]."
2277 ;; Get the name of the session file to recover from.
2278 (let ((file (dired-get-filename))
2280 (buffer (get-buffer-create " *recover*")))
2281 (dired-do-flagged-delete t
)
2284 ;; Read in the auto-save-list file.
2287 (insert-file-contents file
)
2288 ;; Loop thru the text of that file
2289 ;; and get out the names of the files to recover.
2291 (let (thisfile autofile
)
2293 ;; This is a pair of lines for a non-file-visiting buffer.
2294 ;; Get the auto-save file name and manufacture
2295 ;; a "visited file name" from that.
2299 (buffer-substring-no-properties
2307 (file-name-nondirectory autofile
)
2309 (file-name-directory autofile
)))
2311 ;; This pair of lines is a file-visiting
2312 ;; buffer. Use the visited file name.
2315 (buffer-substring-no-properties
2316 (point) (progn (end-of-line) (point))))
2319 (buffer-substring-no-properties
2320 (point) (progn (end-of-line) (point))))
2322 ;; Ignore a file if its auto-save file does not exist now.
2323 (if (file-exists-p autofile
)
2324 (setq files
(cons thisfile files
)))))
2325 (setq files
(nreverse files
))
2326 ;; The file contains a pair of line for each auto-saved buffer.
2327 ;; The first line of the pair contains the visited file name
2328 ;; or is empty if the buffer was not visiting a file.
2329 ;; The second line is the auto-save file name.
2331 (map-y-or-n-p "Recover %s? "
2334 (save-excursion (recover-file file
))
2336 "Failed to recover `%s'" file
)))
2338 '("file" "files" "recover"))
2339 (message "No files can be recovered from this session now")))
2340 (kill-buffer buffer
))))
2342 (defun kill-some-buffers ()
2343 "For each buffer, ask whether to kill it."
2345 (let ((list (buffer-list)))
2347 (let* ((buffer (car list
))
2348 (name (buffer-name buffer
)))
2349 (and (not (string-equal name
""))
2350 (/= (aref name
0) ?
)
2352 (format "Buffer %s %s. Kill? "
2354 (if (buffer-modified-p buffer
)
2355 "HAS BEEN EDITED" "is unmodified")))
2356 (kill-buffer buffer
)))
2357 (setq list
(cdr list
)))))
2359 (defun auto-save-mode (arg)
2360 "Toggle auto-saving of contents of current buffer.
2361 With prefix argument ARG, turn auto-saving on if positive, else off."
2363 (setq buffer-auto-save-file-name
2365 (or (not buffer-auto-save-file-name
)
2366 ;; If autosave is off because buffer has shrunk,
2367 ;; then toggling should turn it on.
2368 (< buffer-saved-size
0))
2369 (or (eq arg t
) (listp arg
) (and (integerp arg
) (> arg
0))))
2370 (if (and buffer-file-name auto-save-visited-file-name
2371 (not buffer-read-only
))
2373 (make-auto-save-file-name))))
2374 ;; If -1 was stored here, to temporarily turn off saving,
2376 (and (< buffer-saved-size
0)
2377 (setq buffer-saved-size
0))
2379 (message "Auto-save %s (in this buffer)"
2380 (if buffer-auto-save-file-name
"on" "off")))
2381 buffer-auto-save-file-name
)
2383 (defun rename-auto-save-file ()
2384 "Adjust current buffer's auto save file name for current conditions.
2385 Also rename any existing auto save file, if it was made in this session."
2386 (let ((osave buffer-auto-save-file-name
))
2387 (setq buffer-auto-save-file-name
2388 (make-auto-save-file-name))
2389 (if (and osave buffer-auto-save-file-name
2390 (not (string= buffer-auto-save-file-name buffer-file-name
))
2391 (not (string= buffer-auto-save-file-name osave
))
2392 (file-exists-p osave
)
2393 (recent-auto-save-p))
2394 (rename-file osave buffer-auto-save-file-name t
))))
2396 (defun make-auto-save-file-name ()
2397 "Return file name to use for auto-saves of current buffer.
2398 Does not consider `auto-save-visited-file-name' as that variable is checked
2399 before calling this function. You can redefine this for customization.
2400 See also `auto-save-file-name-p'."
2401 (if buffer-file-name
2402 (if (eq system-type
'ms-dos
)
2403 (let ((fn (file-name-nondirectory buffer-file-name
)))
2404 (string-match "\\`\\([^.]+\\)\\(\\.\\(..?\\)?.?\\|\\)\\'" fn
)
2405 (concat (file-name-directory buffer-file-name
)
2406 "#" (match-string 1 fn
)
2407 "." (match-string 3 fn
) "#"))
2408 (concat (file-name-directory buffer-file-name
)
2410 (file-name-nondirectory buffer-file-name
)
2413 ;; Deal with buffers that don't have any associated files. (Mail
2414 ;; mode tends to create a good number of these.)
2416 (let ((buffer-name (buffer-name))
2418 ;; Use technique from Sebastian Kremer's auto-save
2419 ;; package to turn slashes into \\!. This ensures that
2420 ;; the auto-save buffer name is unique.
2422 (while (string-match "[/\\]" buffer-name limit
)
2423 (setq buffer-name
(concat (substring buffer-name
0 (match-beginning 0))
2424 (if (string= (substring buffer-name
2430 (substring buffer-name
(match-end 0))))
2431 (setq limit
(1+ (match-end 0))))
2433 (expand-file-name (format "#%s#%s#" buffer-name
(make-temp-name ""))))))
2435 (defun auto-save-file-name-p (filename)
2436 "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'.
2437 FILENAME should lack slashes. You can redefine this for customization."
2438 (string-match "^#.*#$" filename
))
2440 (defun wildcard-to-regexp (wildcard)
2441 "Given a shell file name pattern WILDCARD, return an equivalent regexp.
2442 The generated regexp will match a filename iff the filename
2443 matches that wildcard according to shell rules. Only wildcards known
2444 by `sh' are supported."
2445 (let* ((i (string-match "[[.*+\\^$?]" wildcard
))
2446 ;; Copy the initial run of non-special characters.
2447 (result (substring wildcard
0 i
))
2448 (len (length wildcard
)))
2449 ;; If no special characters, we're almost done.
2452 (let ((ch (aref wildcard i
))
2458 ((eq ch ?\
[) ; [...] maps to regexp char class
2463 ((eq (aref wildcard i
) ?
!) ; [!...] -> [^...]
2466 (if (eq (aref wildcard i
) ?\
])
2471 ((eq (aref wildcard i
) ?^
)
2472 ;; Found "[^". Insert a `\0' character
2473 ;; (which cannot happen in a filename)
2474 ;; into the character class, so that `^'
2475 ;; is not the first character after `[',
2476 ;; and thus non-special in a regexp.
2480 ((eq (aref wildcard i
) ?\
])
2481 ;; I don't think `]' can appear in a
2482 ;; character class in a wildcard, but
2483 ;; let's be general here.
2488 (prog1 ; copy everything upto next `]'.
2491 (setq j
(string-match
2493 (setq i
(if j
(1- j
) (1- len
)))))))
2495 ((eq ch ?
*) "[^\000]*")
2499 ((eq ch ?
\\) "\\\\") ; probably cannot happen...
2500 ((eq ch ??
) "[^\000]")
2501 (t (char-to-string ch
)))))
2503 ;; Shell wildcards should match the entire filename,
2504 ;; not its part. Make the regexp say so.
2505 (concat "\\`" result
"\\'")))
2507 (defconst list-directory-brief-switches
2508 (if (eq system-type
'vax-vms
) "" "-CF")
2509 "*Switches for list-directory to pass to `ls' for brief listing,")
2511 (defconst list-directory-verbose-switches
2512 (if (eq system-type
'vax-vms
)
2513 "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)"
2515 "*Switches for list-directory to pass to `ls' for verbose listing,")
2517 (defun list-directory (dirname &optional verbose
)
2518 "Display a list of files in or matching DIRNAME, a la `ls'.
2519 DIRNAME is globbed by the shell if necessary.
2520 Prefix arg (second arg if noninteractive) means supply -l switch to `ls'.
2521 Actions controlled by variables `list-directory-brief-switches'
2522 and `list-directory-verbose-switches'."
2523 (interactive (let ((pfx current-prefix-arg
))
2524 (list (read-file-name (if pfx
"List directory (verbose): "
2525 "List directory (brief): ")
2526 nil default-directory nil
)
2528 (let ((switches (if verbose list-directory-verbose-switches
2529 list-directory-brief-switches
)))
2530 (or dirname
(setq dirname default-directory
))
2531 (setq dirname
(expand-file-name dirname
))
2532 (with-output-to-temp-buffer "*Directory*"
2533 (buffer-disable-undo standard-output
)
2534 (princ "Directory ")
2538 (set-buffer "*Directory*")
2539 (setq default-directory
(file-name-directory dirname
))
2540 (let ((wildcard (not (file-directory-p dirname
))))
2541 (insert-directory dirname switches wildcard
(not wildcard
)))))))
2543 (defvar insert-directory-program
"ls"
2544 "Absolute or relative name of the `ls' program used by `insert-directory'.")
2547 ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and
2548 ;; FULL-DIRECTORY-P is nil.
2549 ;; The single line of output must display FILE's name as it was
2550 ;; given, namely, an absolute path name.
2551 ;; - must insert exactly one line for each file if WILDCARD or
2552 ;; FULL-DIRECTORY-P is t, plus one optional "total" line
2553 ;; before the file lines, plus optional text after the file lines.
2554 ;; Lines are delimited by "\n", so filenames containing "\n" are not
2556 ;; File lines should display the basename.
2557 ;; - must be consistent with
2558 ;; - functions dired-move-to-filename, (these two define what a file line is)
2559 ;; dired-move-to-end-of-filename,
2560 ;; dired-between-files, (shortcut for (not (dired-move-to-filename)))
2561 ;; dired-insert-headerline
2562 ;; dired-after-subdir-garbage (defines what a "total" line is)
2563 ;; - variable dired-subdir-regexp
2564 (defun insert-directory (file switches
&optional wildcard full-directory-p
)
2565 "Insert directory listing for FILE, formatted according to SWITCHES.
2566 Leaves point after the inserted text.
2567 SWITCHES may be a string of options, or a list of strings.
2568 Optional third arg WILDCARD means treat FILE as shell wildcard.
2569 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
2570 switches do not contain `d', so that a full listing is expected.
2572 This works by running a directory listing program
2573 whose name is in the variable `insert-directory-program'.
2574 If WILDCARD, it also runs the shell specified by `shell-file-name'."
2575 ;; We need the directory in order to find the right handler.
2576 (let ((handler (find-file-name-handler (expand-file-name file
)
2577 'insert-directory
)))
2579 (funcall handler
'insert-directory file switches
2580 wildcard full-directory-p
)
2581 (if (eq system-type
'vax-vms
)
2582 (vms-read-directory file switches
(current-buffer))
2584 ;; Run ls in the directory of the file pattern we asked for.
2585 (let ((default-directory
2586 (if (file-name-absolute-p file
)
2587 (file-name-directory file
)
2588 (file-name-directory (expand-file-name file
))))
2589 (pattern (file-name-nondirectory file
))
2591 ;; Quote some characters that have special meanings in shells;
2592 ;; but don't quote the wildcards--we want them to be special.
2593 ;; We also currently don't quote the quoting characters
2594 ;; in case people want to use them explicitly to quote
2595 ;; wildcard characters.
2596 (while (string-match "[ \t\n;<>&|()#$]" pattern beg
)
2598 (concat (substring pattern
0 (match-beginning 0))
2600 (substring pattern
(match-beginning 0)))
2601 beg
(1+ (match-end 0))))
2602 (call-process shell-file-name nil t nil
2603 "-c" (concat "\\" ;; Disregard shell aliases!
2604 insert-directory-program
2606 (if (stringp switches
)
2608 (mapconcat 'identity switches
" "))
2611 ;; SunOS 4.1.3, SVr4 and others need the "." to list the
2612 ;; directory if FILE is a symbolic link.
2613 (apply 'call-process
2614 insert-directory-program nil t nil
2616 (if (listp switches
)
2617 (setq list switches
)
2618 (if (not (equal switches
""))
2620 ;; Split the switches at any spaces
2621 ;; so we can pass separate options as separate args.
2622 (while (string-match " " switches
)
2623 (setq list
(cons (substring switches
0 (match-beginning 0))
2625 switches
(substring switches
(match-end 0))))
2626 (setq list
(cons switches list
)))))
2629 (if full-directory-p
2630 (concat (file-name-as-directory file
) ".")
2633 (defvar kill-emacs-query-functions nil
2634 "Functions to call with no arguments to query about killing Emacs.
2635 If any of these functions returns nil, killing Emacs is cancelled.
2636 `save-buffers-kill-emacs' (\\[save-buffers-kill-emacs]) calls these functions,
2637 but `kill-emacs', the low level primitive, does not.
2638 See also `kill-emacs-hook'.")
2640 (defun save-buffers-kill-emacs (&optional arg
)
2641 "Offer to save each buffer, then kill this Emacs process.
2642 With prefix arg, silently save all file-visiting buffers, then kill."
2644 (save-some-buffers arg t
)
2645 (and (or (not (memq t
(mapcar (function
2646 (lambda (buf) (and (buffer-file-name buf
)
2647 (buffer-modified-p buf
))))
2649 (yes-or-no-p "Modified buffers exist; exit anyway? "))
2650 (or (not (fboundp 'process-list
))
2651 ;; process-list is not defined on VMS.
2652 (let ((processes (process-list))
2655 (and (memq (process-status (car processes
)) '(run stop open
))
2656 (let ((val (process-kill-without-query (car processes
))))
2657 (process-kill-without-query (car processes
) val
)
2660 (setq processes
(cdr processes
)))
2662 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
2663 ;; Query the user for other things, perhaps.
2664 (run-hook-with-args-until-failure 'kill-emacs-query-functions
)
2667 (define-key ctl-x-map
"\C-f" 'find-file
)
2668 (define-key ctl-x-map
"\C-q" 'toggle-read-only
)
2669 (define-key ctl-x-map
"\C-r" 'find-file-read-only
)
2670 (define-key ctl-x-map
"\C-v" 'find-alternate-file
)
2671 (define-key ctl-x-map
"\C-s" 'save-buffer
)
2672 (define-key ctl-x-map
"s" 'save-some-buffers
)
2673 (define-key ctl-x-map
"\C-w" 'write-file
)
2674 (define-key ctl-x-map
"i" 'insert-file
)
2675 (define-key esc-map
"~" 'not-modified
)
2676 (define-key ctl-x-map
"\C-d" 'list-directory
)
2677 (define-key ctl-x-map
"\C-c" 'save-buffers-kill-emacs
)
2679 (define-key ctl-x-4-map
"f" 'find-file-other-window
)
2680 (define-key ctl-x-4-map
"r" 'find-file-read-only-other-window
)
2681 (define-key ctl-x-4-map
"\C-f" 'find-file-other-window
)
2682 (define-key ctl-x-4-map
"b" 'switch-to-buffer-other-window
)
2683 (define-key ctl-x-4-map
"\C-o" 'display-buffer
)
2685 (define-key ctl-x-5-map
"b" 'switch-to-buffer-other-frame
)
2686 (define-key ctl-x-5-map
"f" 'find-file-other-frame
)
2687 (define-key ctl-x-5-map
"\C-f" 'find-file-other-frame
)
2688 (define-key ctl-x-5-map
"r" 'find-file-read-only-other-frame
)
2690 ;;; files.el ends here