]> code.delx.au - gnu-emacs/blob - lisp/files.el
(find-file-noselect): On VMS, maybe set buffer-file-name
[gnu-emacs] / lisp / files.el
1 ;;; files.el --- file input and output commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1993 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;; Defines most of Emacs's file- and directory-handling functions,
26 ;; including basic file visiting, backup generation, link handling,
27 ;; ITS-id version control, load- and write-hook handling, and the like.
28
29 ;;; Code:
30
31 (defconst delete-auto-save-files t
32 "*Non-nil means delete a buffer's auto-save file when the buffer is saved.")
33
34 (defconst directory-abbrev-alist
35 nil
36 "*Alist of abbreviations for file directories.
37 A list of elements of the form (FROM . TO), each meaning to replace
38 FROM with TO when it appears in a directory name. This replacement is
39 done when setting up the default directory of a newly visited file.
40 *Every* FROM string should start with `^'.
41
42 Use this feature when you have directories which you normally refer to
43 via absolute symbolic links. Make TO the name of the link, and FROM
44 the name it is linked to.")
45
46 ;;; Turn off backup files on VMS since it has version numbers.
47 (defconst make-backup-files (not (eq system-type 'vax-vms))
48 "*Create a backup of each file when it is saved for the first time.
49 This can be done by renaming the file or by copying.
50
51 Renaming means that Emacs renames the existing file so that it is a
52 backup file, then writes the buffer into a new file. Any other names
53 that the old file had will now refer to the backup file. The new file
54 is owned by you and its group is defaulted.
55
56 Copying means that Emacs copies the existing file into the backup
57 file, then writes the buffer on top of the existing file. Any other
58 names that the old file had will now refer to the new (edited) file.
59 The file's owner and group are unchanged.
60
61 The choice of renaming or copying is controlled by the variables
62 `backup-by-copying', `backup-by-copying-when-linked' and
63 `backup-by-copying-when-mismatch'.")
64
65 ;; Do this so that local variables based on the file name
66 ;; are not overridden by the major mode.
67 (defvar backup-inhibited nil
68 "Non-nil means don't make a backup file for this buffer.")
69 (put 'backup-inhibited 'permanent-local t)
70
71 (defconst backup-by-copying nil
72 "*Non-nil means always use copying to create backup files.
73 See documentation of variable `make-backup-files'.")
74
75 (defconst backup-by-copying-when-linked nil
76 "*Non-nil means use copying to create backups for files with multiple names.
77 This causes the alternate names to refer to the latest version as edited.
78 This variable is relevant only if `backup-by-copying' is nil.")
79
80 (defconst backup-by-copying-when-mismatch nil
81 "*Non-nil means create backups by copying if this preserves owner or group.
82 Renaming may still be used (subject to control of other variables)
83 when it would not result in changing the owner or group of the file;
84 that is, for files which are owned by you and whose group matches
85 the default for a new file created there by you.
86 This variable is relevant only if `backup-by-copying' is nil.")
87
88 (defvar backup-enable-predicate
89 '(lambda (name)
90 (or (< (length name) 5)
91 (not (string-equal "/tmp/" (substring name 0 5)))))
92 "Predicate that looks at a file name and decides whether to make backups.
93 Called with an absolute file name as argument, it returns t to enable backup.")
94
95 (defconst buffer-offer-save nil
96 "*Non-nil in a buffer means offer to save the buffer on exit
97 even if the buffer is not visiting a file.
98 Automatically local in all buffers.")
99 (make-variable-buffer-local 'buffer-offer-save)
100
101 (defconst find-file-existing-other-name nil
102 "*Non-nil means find a file under alternative names, in existing buffers.
103 This means if any existing buffer is visiting the file you want
104 under another name, you get the existing buffer instead of a new buffer.")
105
106 (defconst find-file-visit-truename nil
107 "*Non-nil means visit a file under its truename.
108 The truename of a file is found by chasing all links
109 both at the file level and at the levels of the containing directories.")
110
111 (defvar buffer-file-truename nil
112 "The truename of the file visited in the current buffer.
113 This variable is automatically local in all buffers, when non-nil.")
114 (make-variable-buffer-local 'buffer-file-truename)
115 (put 'buffer-file-truename 'permanent-local t)
116
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)
124
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.")
128
129 (defvar version-control nil
130 "*Control use of version numbers for backup files.
131 t means make numeric backup versions unconditionally.
132 nil means make them for files that have some already.
133 never means do not make them.")
134
135 (defvar dired-kept-versions 2
136 "*When cleaning directory, number of versions to keep.")
137
138 (defvar trim-versions-without-asking nil
139 "*If t, deletes excess backup versions silently.
140 If nil, asks confirmation. Any other value prevents any trimming.")
141
142 (defvar kept-old-versions 2
143 "*Number of oldest versions to keep when a new numbered backup is made.")
144
145 (defvar kept-new-versions 2
146 "*Number of newest versions to keep when a new numbered backup is made.
147 Includes the new backup. Must be > 0")
148
149 (defconst require-final-newline nil
150 "*Value of t says silently ensure a file ends in a newline when it is saved.
151 Non-nil but not t says ask user whether to add a newline when there isn't one.
152 nil means don't add newlines.")
153
154 (defconst auto-save-default t
155 "*Non-nil says by default do auto-saving of every file-visiting buffer.")
156
157 (defconst auto-save-visited-file-name nil
158 "*Non-nil says auto-save a buffer in the file it is visiting, when practical.
159 Normally auto-save files are written under other names.")
160
161 (defconst save-abbrevs nil
162 "*Non-nil means save word abbrevs too when files are saved.
163 Loading an abbrev file sets this to t.")
164
165 (defconst find-file-run-dired t
166 "*Non-nil says run dired if find-file is given the name of a directory.")
167
168 ;;;It is not useful to make this a local variable.
169 ;;;(put 'find-file-not-found-hooks 'permanent-local t)
170 (defvar find-file-not-found-hooks nil
171 "List of functions to be called for `find-file' on nonexistent file.
172 These functions are called as soon as the error is detected.
173 `buffer-file-name' is already set up.
174 The functions are called in the order given until one of them returns non-nil.")
175
176 ;;;It is not useful to make this a local variable.
177 ;;;(put 'find-file-hooks 'permanent-local t)
178 (defvar find-file-hooks nil
179 "List of functions to be called after a buffer is loaded from a file.
180 The buffer's local variables (if any) will have been processed before the
181 functions are called.")
182
183 ;;; In case someone does make it local.
184 (put 'write-file-hooks 'permanent-local t)
185 (defvar write-file-hooks nil
186 "List of functions to be called before writing out a buffer to a file.
187 If one of them returns non-nil, the file is considered already written
188 and the rest are not called.
189 These hooks are considered to pertain to the visited file.
190 So this list is cleared if you change the visited file name.
191 See also `write-contents-hooks'.
192 Don't make this variable buffer-local; instead, use `local-write-file-hooks'.")
193
194 (put 'local-write-file-hooks 'permanent-local t)
195 (defvar local-write-file-hooks nil
196 "Just like `write-file-hooks', except intended for per-buffer use.
197 The functions in this list are called before the ones in
198 `write-file-hooks'.")
199
200 (defvar write-contents-hooks nil
201 "List of functions to be called before writing out a buffer to a file.
202 If one of them returns non-nil, the file is considered already written
203 and the rest are not called.
204 These hooks are considered to pertain to the buffer's contents,
205 not to the particular visited file; thus, `set-visited-file-name' does
206 not clear this variable, but changing the major mode does clear it.
207 See also `write-file-hooks'.")
208
209 (defconst enable-local-variables t
210 "*Control use of local-variables lists in files you visit.
211 The value can be t, nil or something else.
212 A value of t means local-variables lists are obeyed;
213 nil means they are ignored; anything else means query.
214
215 The command \\[normal-mode] always obeys local-variables lists
216 and ignores this variable.")
217
218 (defconst enable-local-eval 'maybe
219 "*Control processing of the \"variable\" `eval' in a file's local variables.
220 The value can be t, nil or something else.
221 A value of t means obey `eval' variables;
222 nil means ignore them; anything else means query.
223
224 The command \\[normal-mode] always obeys local-variables lists
225 and ignores this variable.")
226
227 ;; Avoid losing in versions where CLASH_DETECTION is disabled.
228 (or (fboundp 'lock-buffer)
229 (defalias 'lock-buffer 'ignore))
230 (or (fboundp 'unlock-buffer)
231 (defalias 'unlock-buffer 'ignore))
232 \f
233 (defun pwd ()
234 "Show the current default directory."
235 (interactive nil)
236 (message "Directory %s" default-directory))
237
238 (defvar cd-path nil
239 "Value of the CDPATH environment variable, as a list.
240 Not actually set up until the first time you you use it.")
241
242 (defun parse-colon-path (cd-path)
243 "Explode a colon-separated list of paths into a string list."
244 (and cd-path
245 (let (cd-prefix cd-list (cd-start 0) cd-colon)
246 (setq cd-path (concat cd-path ":"))
247 (while (setq cd-colon (string-match ":" cd-path cd-start))
248 (setq cd-list
249 (nconc cd-list (list (substitute-in-file-name (file-name-as-directory (substring cd-path cd-start cd-colon))))))
250 (setq cd-start (+ cd-colon 1)))
251 cd-list)))
252
253 (defun cd-absolute (dir)
254 "Change current directory to given absolute path DIR."
255 (interactive "DChange default directory: ")
256 (setq dir (expand-file-name dir))
257 (if (not (eq system-type 'vax-vms))
258 (setq dir (file-name-as-directory dir)))
259 (if (not (file-directory-p dir))
260 (error "%s is not a directory" dir)
261 (if (file-executable-p dir)
262 (setq default-directory dir)
263 (error "Cannot cd to %s: Permission denied" dir)))
264 )
265
266 (defun cd (dir)
267 "Make DIR become the current buffer's default directory.
268 If your environment imcludes a $CDPATH variable, cd tries each one of that
269 colon-separated list of directories when resolving a relative cd."
270 (interactive "FChange default directory: ")
271 (let ((first (aref dir 0)))
272 (if (or (= first ?/) (= first ?~))
273 (cd-absolute (expand-file-name dir))
274 (if (null cd-path)
275 (let ((trypath (parse-colon-path (getenv "CDPATH"))))
276 (setq cd-path (or trypath "./"))))
277 (if (not (catch 'found
278 (mapcar
279 (function (lambda (x)
280 (let ((f (expand-file-name (concat x dir))))
281 (if (file-directory-p f)
282 (progn
283 (cd-absolute f)
284 (throw 'found t))))))
285 cd-path)
286 nil))
287 (error "No such directory on your cd path.")))
288 ))
289
290 (defun load-file (file)
291 "Load the Lisp file named FILE."
292 (interactive "fLoad file: ")
293 (load (expand-file-name file) nil nil t))
294
295 (defun load-library (library)
296 "Load the library named LIBRARY.
297 This is an interface to the function `load'."
298 (interactive "sLoad library: ")
299 (load library))
300
301 ;; OTHER is the other file to be compared.
302 (defun file-local-copy (file)
303 "Copy the file FILE into a temporary file on this machine.
304 Returns the name of the local copy, or nil, if FILE is directly
305 accessible."
306 (let ((handler (find-file-name-handler file)))
307 (if handler
308 (funcall handler 'file-local-copy file)
309 nil)))
310
311 (defun file-truename (filename)
312 "Return the truename of FILENAME, which should be absolute.
313 The truename of a file name is found by chasing symbolic links
314 both at the level of the file and at the level of the directories
315 containing it, until no links are left at any level."
316 (if (string= filename "~")
317 (progn
318 (setq filename (expand-file-name filename))
319 (if (string= filename "")
320 (setq filename "/"))))
321 (let ((handler (find-file-name-handler filename)))
322 ;; For file name that has a special handler, call handler.
323 ;; This is so that ange-ftp can save time by doing a no-op.
324 (if handler
325 (funcall handler 'file-truename filename)
326 (let ((dir (file-name-directory filename))
327 target dirfile)
328 ;; Get the truename of the directory.
329 (setq dirfile (directory-file-name dir))
330 ;; If these are equal, we have the (or a) root directory.
331 (or (string= dir dirfile)
332 (setq dir (file-name-as-directory (file-truename dirfile))))
333 ;; Put it back on the file name.
334 (setq filename (concat dir (file-name-nondirectory filename)))
335 ;; Is the file name the name of a link?
336 (setq target (file-symlink-p filename))
337 (if target
338 ;; Yes => chase that link, then start all over
339 ;; since the link may point to a directory name that uses links.
340 (file-truename (expand-file-name target dir))
341 ;; No, we are done!
342 filename)))))
343
344 \f
345 (defun switch-to-buffer-other-window (buffer)
346 "Select buffer BUFFER in another window."
347 (interactive "BSwitch to buffer in other window: ")
348 (let ((pop-up-windows t))
349 (pop-to-buffer buffer t)))
350
351 (defun switch-to-buffer-other-frame (buffer)
352 "Switch to buffer BUFFER in another frame."
353 (interactive "BSwitch to buffer in other frame: ")
354 (let ((pop-up-frames t))
355 (pop-to-buffer buffer t)))
356
357 (defun find-file (filename)
358 "Edit file FILENAME.
359 Switch to a buffer visiting file FILENAME,
360 creating one if none already exists."
361 (interactive "FFind file: ")
362 (switch-to-buffer (find-file-noselect filename)))
363
364 (defun find-file-other-window (filename)
365 "Edit file FILENAME, in another window.
366 May create a new window, or reuse an existing one.
367 See the function `display-buffer'."
368 (interactive "FFind file in other window: ")
369 (switch-to-buffer-other-window (find-file-noselect filename)))
370
371 (defun find-file-other-frame (filename)
372 "Edit file FILENAME, in another frame.
373 May create a new frame, or reuse an existing one.
374 See the function `display-buffer'."
375 (interactive "FFind file in other frame: ")
376 (switch-to-buffer-other-frame (find-file-noselect filename)))
377
378 (defun find-file-read-only (filename)
379 "Edit file FILENAME but don't allow changes.
380 Like \\[find-file] but marks buffer as read-only.
381 Use \\[toggle-read-only] to permit editing."
382 (interactive "fFind file read-only: ")
383 (find-file filename)
384 (setq buffer-read-only t))
385
386 (defun find-file-read-only-other-window (filename)
387 "Edit file FILENAME in another window but don't allow changes.
388 Like \\[find-file-other-window] but marks buffer as read-only.
389 Use \\[toggle-read-only] to permit editing."
390 (interactive "fFind file read-only other window: ")
391 (find-file filename)
392 (setq buffer-read-only t))
393
394 (defun find-file-read-only-other-frame (filename)
395 "Edit file FILENAME in another frame but don't allow changes.
396 Like \\[find-file-other-frame] but marks buffer as read-only.
397 Use \\[toggle-read-only] to permit editing."
398 (interactive "fFind file read-only other frame: ")
399 (find-file-other-frame filename)
400 (setq buffer-read-only t))
401
402 (defun find-alternate-file (filename)
403 "Find file FILENAME, select its buffer, kill previous buffer.
404 If the current buffer now contains an empty file that you just visited
405 \(presumably by mistake), use this command to visit the file you really want."
406 (interactive
407 (let ((file buffer-file-name)
408 (file-name nil)
409 (file-dir nil))
410 (and file
411 (setq file-name (file-name-nondirectory file)
412 file-dir (file-name-directory file)))
413 (list (read-file-name
414 "Find alternate file: " file-dir nil nil file-name))))
415 (and (buffer-modified-p)
416 ;; (not buffer-read-only)
417 (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? "
418 (buffer-name))))
419 (error "Aborted"))
420 (let ((obuf (current-buffer))
421 (ofile buffer-file-name)
422 (oname (buffer-name)))
423 (rename-buffer " **lose**")
424 (setq buffer-file-name nil)
425 (unwind-protect
426 (progn
427 (unlock-buffer)
428 (find-file filename))
429 (cond ((eq obuf (current-buffer))
430 (setq buffer-file-name ofile)
431 (lock-buffer)
432 (rename-buffer oname))))
433 (or (eq (current-buffer) obuf)
434 (kill-buffer obuf))))
435
436 (defun create-file-buffer (filename)
437 "Create a suitably named buffer for visiting FILENAME, and return it.
438 FILENAME (sans directory) is used unchanged if that name is free;
439 otherwise a string <2> or <3> or ... is appended to get an unused name."
440 (let ((lastname (file-name-nondirectory filename)))
441 (if (string= lastname "")
442 (setq lastname filename))
443 (generate-new-buffer lastname)))
444
445 (defun generate-new-buffer (name)
446 "Create and return a buffer with a name based on NAME.
447 Choose the buffer's name using `generate-new-buffer-name'."
448 (get-buffer-create (generate-new-buffer-name name)))
449
450 (defconst automount-dir-prefix "^/tmp_mnt/"
451 "Regexp to match the automounter prefix in a directory name.")
452
453 (defvar abbreviated-home-dir nil
454 "The the user's homedir abbreviated according to `directory-abbrev-list'.")
455
456 (defun abbreviate-file-name (filename)
457 "Return a version of FILENAME shortened using `directory-abbrev-alist'.
458 This also substitutes \"~\" for the user's home directory.
459 Type \\[describe-variable] directory-abbrev-alist RET for more information."
460 ;; Get rid of the prefixes added by the automounter.
461 (if (and (string-match automount-dir-prefix filename)
462 (file-exists-p (file-name-directory
463 (substring filename (1- (match-end 0))))))
464 (setq filename (substring filename (1- (match-end 0)))))
465 (let ((tail directory-abbrev-alist))
466 ;; If any elt of directory-abbrev-alist matches this name,
467 ;; abbreviate accordingly.
468 (while tail
469 (if (string-match (car (car tail)) filename)
470 (setq filename
471 (concat (cdr (car tail)) (substring filename (match-end 0)))))
472 (setq tail (cdr tail)))
473 ;; Compute and save the abbreviated homedir name.
474 ;; We defer computing this until the first time it's needed, to
475 ;; give time for directory-abbrev-alist to be set properly.
476 (or abbreviated-home-dir
477 (setq abbreviated-home-dir
478 (let ((abbreviated-home-dir "$foo"))
479 (concat "^" (abbreviate-file-name (expand-file-name "~"))))))
480 ;; If FILENAME starts with the abbreviated homedir,
481 ;; make it start with `~' instead.
482 (if (string-match abbreviated-home-dir filename)
483 (setq filename
484 (concat "~" (substring filename (match-end 0)))))
485 filename))
486
487 (defvar find-file-not-true-dirname-list nil
488 "*List of logical names for which visiting shouldn't save the true dirname.
489 On VMS, when you visit a file using a logical name that searches a path,
490 you may or may not want the visited file name to record the specific
491 directory where the file was found. If you *do not* want that, add the logical
492 name to this list as a string.")
493
494 (defun find-file-noselect (filename &optional nowarn)
495 "Read file FILENAME into a buffer and return the buffer.
496 If a buffer exists visiting FILENAME, return that one, but
497 verify that the file has not changed since visited or saved.
498 The buffer is not selected, just returned to the caller."
499 (setq filename
500 (abbreviate-file-name
501 (expand-file-name filename)))
502 (if (file-directory-p filename)
503 (if find-file-run-dired
504 (dired-noselect filename)
505 (error "%s is a directory." filename))
506 (let* ((buf (get-file-buffer filename))
507 (truename (abbreviate-file-name (file-truename filename)))
508 (number (nthcdr 10 (file-attributes truename)))
509 ;; Find any buffer for a file which has same truename.
510 (same-truename
511 (or buf ; Shortcut
512 (let (found
513 (list (buffer-list)))
514 (while (and (not found) list)
515 (save-excursion
516 (set-buffer (car list))
517 (if (and buffer-file-name
518 (string= buffer-file-truename truename))
519 (setq found (car list))))
520 (setq list (cdr list)))
521 found)))
522 (same-number
523 (or buf ; Shortcut
524 (and number
525 (let (found
526 (list (buffer-list)))
527 (while (and (not found) list)
528 (save-excursion
529 (set-buffer (car list))
530 (if (and (equal buffer-file-number number)
531 ;; Verify this buffer's file number
532 ;; still belongs to its file.
533 (file-exists-p buffer-file-name)
534 (equal (nthcdr 10 (file-attributes buffer-file-name)) number))
535 (setq found (car list))))
536 (setq list (cdr list)))
537 found))))
538 error)
539 ;; Let user know if there is a buffer with the same truename.
540 (if (and (not buf) same-truename (not nowarn))
541 (message "%s and %s are the same file (%s)"
542 filename (buffer-file-name same-truename)
543 truename)
544 (if (and (not buf) same-number (not nowarn))
545 (message "%s and %s are the same file"
546 filename (buffer-file-name same-number))))
547
548 ;; Optionally also find that buffer.
549 (if (or find-file-existing-other-name find-file-visit-truename)
550 (setq buf (or same-truename same-number)))
551 (if buf
552 (or nowarn
553 (verify-visited-file-modtime buf)
554 (cond ((not (file-exists-p filename))
555 (error "File %s no longer exists!" filename))
556 ((yes-or-no-p
557 (format
558 (if (buffer-modified-p buf)
559 "File %s changed on disk. Discard your edits? "
560 "File %s changed on disk. Read the new version? ")
561 (file-name-nondirectory filename)))
562 (save-excursion
563 (set-buffer buf)
564 (revert-buffer t t)))))
565 (save-excursion
566 ;;; The truename stuff makes this obsolete.
567 ;;; (let* ((link-name (car (file-attributes filename)))
568 ;;; (linked-buf (and (stringp link-name)
569 ;;; (get-file-buffer link-name))))
570 ;;; (if (bufferp linked-buf)
571 ;;; (message "Symbolic link to file in buffer %s"
572 ;;; (buffer-name linked-buf))))
573 (setq buf (create-file-buffer filename))
574 (set-buffer buf)
575 (erase-buffer)
576 (condition-case ()
577 (insert-file-contents filename t)
578 (file-error
579 (setq error t)
580 ;; Run find-file-not-found-hooks until one returns non-nil.
581 (let ((hooks find-file-not-found-hooks))
582 (while (and hooks
583 (not (funcall (car hooks))))
584 (setq hooks (cdr hooks))))))
585 ;; Find the file's truename, and maybe use that as visited name.
586 (setq buffer-file-truename (abbreviate-file-name truename))
587 (setq buffer-file-number number)
588 ;; On VMS, we may want to remember which directory in a search list
589 ;; the file was found in.
590 (and (eq system-type 'vax-vms)
591 (let (logical)
592 (if (string-match ":" (file-name-directory filename))
593 (setq logical (substring (file-name-directory filename)
594 0 (match-beginning 0))))
595 (not (member logical find-file-not-true-dirname-list)))
596 (setq buffer-file-name buffer-file-truename))
597 (if find-file-visit-truename
598 (setq buffer-file-name (setq filename buffer-file-truename)))
599 ;; Set buffer's default directory to that of the file.
600 (setq default-directory (file-name-directory filename))
601 ;; Turn off backup files for certain file names. Since
602 ;; this is a permanent local, the major mode won't eliminate it.
603 (and (not (funcall backup-enable-predicate buffer-file-name))
604 (progn
605 (make-local-variable 'backup-inhibited)
606 (setq backup-inhibited t)))
607 (after-find-file error (not nowarn))))
608 buf)))
609 \f
610 (defun after-find-file (&optional error warn noauto)
611 "Called after finding a file and by the default revert function.
612 Sets buffer mode, parses local variables.
613 Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an
614 error in reading the file. WARN non-nil means warn if there
615 exists an auto-save file more recent than the visited file.
616 NOAUTO means don't mess with auto-save mode.
617 Finishes by calling the functions in `find-file-hooks'."
618 (setq buffer-read-only (not (file-writable-p buffer-file-name)))
619 (if noninteractive
620 nil
621 (let* (not-serious
622 (msg
623 (cond ((and error (file-attributes buffer-file-name))
624 (setq buffer-read-only t)
625 "File exists, but is read-protected.")
626 ((not buffer-read-only)
627 (if (and warn
628 (file-newer-than-file-p (make-auto-save-file-name)
629 buffer-file-name))
630 "Auto save file is newer; consider M-x recover-file"
631 (setq not-serious t)
632 (if error "(New file)" nil)))
633 ((not error)
634 (setq not-serious t)
635 "Note: file is write protected")
636 ((file-attributes (directory-file-name default-directory))
637 "File not found and directory write-protected")
638 ((file-exists-p (file-name-directory buffer-file-name))
639 (setq buffer-read-only nil))
640 (t
641 (setq buffer-read-only nil)
642 (if (file-exists-p (file-name-directory (directory-file-name (file-name-directory buffer-file-name))))
643 "Use M-x make-dir RET RET to create the directory"
644 "Use C-u M-x make-dir RET RET to create directory and its parents")))))
645 (if msg
646 (progn
647 (message msg)
648 (or not-serious (sit-for 1 nil t)))))
649 (if (and auto-save-default (not noauto))
650 (auto-save-mode t)))
651 (normal-mode t)
652 (mapcar 'funcall find-file-hooks))
653
654 (defun normal-mode (&optional find-file)
655 "Choose the major mode for this buffer automatically.
656 Also sets up any specified local variables of the file.
657 Uses the visited file name, the -*- line, and the local variables spec.
658
659 This function is called automatically from `find-file'. In that case,
660 we may set up specified local variables depending on the value of
661 `enable-local-variables': if it is t, we do; if it is nil, we don't;
662 otherwise, we query. `enable-local-variables' is ignored if you
663 run `normal-mode' explicitly."
664 (interactive)
665 (or find-file (funcall (or default-major-mode 'fundamental-mode)))
666 (condition-case err
667 (set-auto-mode)
668 (error (message "File mode specification error: %s"
669 (prin1-to-string err))))
670 (condition-case err
671 (let ((enable-local-variables (or (not find-file)
672 enable-local-variables)))
673 (hack-local-variables))
674 (error (message "File local-variables error: %s"
675 (prin1-to-string err)))))
676
677 (defvar auto-mode-alist (mapcar 'purecopy
678 '(("\\.text\\'" . text-mode)
679 ("\\.c\\'" . c-mode)
680 ("\\.h\\'" . c-mode)
681 ("\\.tex\\'" . TeX-mode)
682 ("\\.ltx\\'" . LaTeX-mode)
683 ("\\.el\\'" . emacs-lisp-mode)
684 ("\\.mm\\'" . nroff-mode)
685 ("\\.me\\'" . nroff-mode)
686 ("\\.ms\\'" . nroff-mode)
687 ("\\.man\\'" . nroff-mode)
688 ("\\.scm\\'" . scheme-mode)
689 ("\\.l\\'" . lisp-mode)
690 ("\\.lisp\\'" . lisp-mode)
691 ("\\.f\\'" . fortran-mode)
692 ("\\.for\\'" . fortran-mode)
693 ("\\.mss\\'" . scribe-mode)
694 ("\\.pl\\'" . prolog-mode)
695 ("\\.cc\\'" . c++-mode)
696 ("\\.C\\'" . c++-mode)
697 ("\\.mk\\'" . makefile-mode)
698 ("[Mm]akefile" . makefile-mode)
699 ;;; Less common extensions come here
700 ;;; so more common ones above are found faster.
701 ("\\.s\\'" . asm-mode)
702 ("ChangeLog\\'" . change-log-mode)
703 ("ChangeLog.[0-9]+\\'" . change-log-mode)
704 ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode)
705 ;; The following should come after the ChangeLog pattern
706 ;; for the sake of ChangeLog.1, etc.
707 ("\\.[12345678]\\'" . nroff-mode)
708 ("\\.TeX\\'" . TeX-mode)
709 ("\\.sty\\'" . LaTeX-mode)
710 ("\\.bbl\\'" . LaTeX-mode)
711 ("\\.bib\\'" . bibtex-mode)
712 ("\\.article\\'" . text-mode)
713 ("\\.letter\\'" . text-mode)
714 ("\\.texinfo\\'" . texinfo-mode)
715 ("\\.texi\\'" . texinfo-mode)
716 ("\\.lsp\\'" . lisp-mode)
717 ("\\.awk\\'" . awk-mode)
718 ("\\.prolog\\'" . prolog-mode)
719 ("\\.tar\\'" . tar-mode)
720 ;; Mailer puts message to be edited in
721 ;; /tmp/Re.... or Message
722 ("^/tmp/Re" . text-mode)
723 ("/Message[0-9]*\\'" . text-mode)
724 ;; some news reader is reported to use this
725 ("^/tmp/fol/" . text-mode)
726 ("\\.y\\'" . c-mode)
727 ("\\.lex\\'" . c-mode)
728 ("\\.oak\\'" . scheme-mode)
729 ("\\.scm.[0-9]*\\'" . scheme-mode)
730 ;; .emacs following a directory delimiter
731 ;; in either Unix or VMS syntax.
732 ("[]>:/]\\..*emacs\\'" . emacs-lisp-mode)
733 ("\\.ml\\'" . lisp-mode)))
734 "\
735 Alist of filename patterns vs corresponding major mode functions.
736 Each element looks like (REGEXP . FUNCTION).
737 Visiting a file whose name matches REGEXP causes FUNCTION to be called.")
738
739 (defun set-auto-mode ()
740 "Select major mode appropriate for current buffer.
741 This checks for a -*- mode tag in the buffer's text, or
742 compares the filename against the entries in auto-mode-alist. It does
743 not check for the \"mode:\" local variable in the Local Variables
744 section of the file; for that, use `hack-local-variables'.
745
746 If `enable-local-variables' is nil, this function does not check for a
747 -*- mode tag."
748 ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
749 (let (beg end mode)
750 (save-excursion
751 (goto-char (point-min))
752 (skip-chars-forward " \t\n")
753 (if (and enable-local-variables
754 (search-forward "-*-" (save-excursion
755 ;; If the file begins with "#!"
756 ;; (exec interpreter magic), look
757 ;; for mode frobs in the first two
758 ;; lines. You cannot necessarily
759 ;; put them in the first line of
760 ;; such a file without screwing up
761 ;; the interpreter invocation.
762 (end-of-line (and (looking-at "^#!") 2))
763 (point)) t)
764 (progn
765 (skip-chars-forward " \t")
766 (setq beg (point))
767 (search-forward "-*-"
768 (save-excursion (end-of-line) (point))
769 t))
770 (progn
771 (forward-char -3)
772 (skip-chars-backward " \t")
773 (setq end (point))
774 (goto-char beg)
775 (if (search-forward ":" end t)
776 (progn
777 (goto-char beg)
778 (if (let ((case-fold-search t))
779 (search-forward "mode:" end t))
780 (progn
781 (skip-chars-forward " \t")
782 (setq beg (point))
783 (if (search-forward ";" end t)
784 (forward-char -1)
785 (goto-char end))
786 (skip-chars-backward " \t")
787 (setq mode (buffer-substring beg (point))))))
788 (setq mode (buffer-substring beg end)))))
789 (setq mode (intern (concat (downcase mode) "-mode")))
790 (let ((alist auto-mode-alist)
791 (name buffer-file-name))
792 (let ((case-fold-search (eq system-type 'vax-vms)))
793 ;; Remove backup-suffixes from file name.
794 (setq name (file-name-sans-versions name))
795 ;; Find first matching alist entry.
796 (while (and (not mode) alist)
797 (if (string-match (car (car alist)) name)
798 (setq mode (cdr (car alist))))
799 (setq alist (cdr alist)))))))
800 (if mode (funcall mode))))
801
802 (defun hack-local-variables-prop-line ()
803 ;; Set local variables specified in the -*- line.
804 ;; Returns t if mode was set.
805 (save-excursion
806 (goto-char (point-min))
807 (skip-chars-forward " \t\n\r")
808 (let ((result '())
809 (end (save-excursion (end-of-line) (point)))
810 mode-p)
811 ;; Parse the -*- line into the `result' alist.
812 (cond ((not (search-forward "-*-" end t))
813 ;; doesn't have one.
814 nil)
815 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
816 ;; Simple form: "-*- MODENAME -*-".
817 (setq result
818 (list (cons 'mode
819 (intern (buffer-substring
820 (match-beginning 1)
821 (match-end 1)))))))
822 (t
823 ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
824 ;; (last ";" is optional).
825 (save-excursion
826 (if (search-forward "-*-" end t)
827 (setq end (- (point) 3))
828 (error "-*- not terminated before end of line")))
829 (while (< (point) end)
830 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
831 (error "malformed -*- line"))
832 (goto-char (match-end 0))
833 (let ((key (intern (downcase (buffer-substring
834 (match-beginning 1)
835 (match-end 1)))))
836 (val (save-restriction
837 (narrow-to-region (point) end)
838 (read (current-buffer)))))
839 (setq result (cons (cons key val) result))
840 (skip-chars-forward " \t;")))
841 (setq result (nreverse result))))
842
843 ;; Mode is magic.
844 (let (mode)
845 (while (setq mode (assq 'mode result))
846 (setq mode-p t result (delq mode result))
847 (funcall (intern (concat (downcase (symbol-name (cdr mode)))
848 "-mode")))))
849
850 (if (and result
851 (or (eq enable-local-variables t)
852 (and enable-local-variables
853 (save-window-excursion
854 (switch-to-buffer (current-buffer))
855 (y-or-n-p (format "Set local variables as specified in -*- line of %s? "
856 (file-name-nondirectory buffer-file-name)))))))
857 (while result
858 (let ((key (car (car result)))
859 (val (cdr (car result))))
860 ;; 'mode has already been removed from this list.
861 (hack-one-local-variable key val))
862 (setq result (cdr result))))
863 mode-p)))
864
865 (defun hack-local-variables ()
866 "Parse and put into effect this buffer's local variables spec."
867 (hack-local-variables-prop-line)
868 ;; Look for "Local variables:" line in last page.
869 (save-excursion
870 (goto-char (point-max))
871 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
872 (if (let ((case-fold-search t))
873 (and (search-forward "Local Variables:" nil t)
874 (or (eq enable-local-variables t)
875 (and enable-local-variables
876 (save-window-excursion
877 (switch-to-buffer (current-buffer))
878 (save-excursion
879 (beginning-of-line)
880 (set-window-start (selected-window) (point)))
881 (y-or-n-p (format "Set local variables as specified at end of %s? "
882 (file-name-nondirectory buffer-file-name))))))))
883 (let ((continue t)
884 prefix prefixlen suffix beg
885 (enable-local-eval enable-local-eval))
886 ;; The prefix is what comes before "local variables:" in its line.
887 ;; The suffix is what comes after "local variables:" in its line.
888 (skip-chars-forward " \t")
889 (or (eolp)
890 (setq suffix (buffer-substring (point)
891 (progn (end-of-line) (point)))))
892 (goto-char (match-beginning 0))
893 (or (bolp)
894 (setq prefix
895 (buffer-substring (point)
896 (progn (beginning-of-line) (point)))))
897
898 (if prefix (setq prefixlen (length prefix)
899 prefix (regexp-quote prefix)))
900 (if suffix (setq suffix (concat (regexp-quote suffix) "$")))
901 (while continue
902 ;; Look at next local variable spec.
903 (if selective-display (re-search-forward "[\n\C-m]")
904 (forward-line 1))
905 ;; Skip the prefix, if any.
906 (if prefix
907 (if (looking-at prefix)
908 (forward-char prefixlen)
909 (error "Local variables entry is missing the prefix")))
910 ;; Find the variable name; strip whitespace.
911 (skip-chars-forward " \t")
912 (setq beg (point))
913 (skip-chars-forward "^:\n")
914 (if (eolp) (error "Missing colon in local variables entry"))
915 (skip-chars-backward " \t")
916 (let* ((str (buffer-substring beg (point)))
917 (var (read str))
918 val)
919 ;; Setting variable named "end" means end of list.
920 (if (string-equal (downcase str) "end")
921 (setq continue nil)
922 ;; Otherwise read the variable value.
923 (skip-chars-forward "^:")
924 (forward-char 1)
925 (setq val (read (current-buffer)))
926 (skip-chars-backward "\n")
927 (skip-chars-forward " \t")
928 (or (if suffix (looking-at suffix) (eolp))
929 (error "Local variables entry is terminated incorrectly"))
930 ;; Set the variable. "Variables" mode and eval are funny.
931 (hack-one-local-variable var val))))))))
932
933 (defconst ignored-local-variables
934 '(enable-local-eval)
935 "Variables to be ignored in a file's local variable spec.")
936
937 ;; "Set" one variable in a local variables spec.
938 ;; A few variable names are treated specially.
939 (defun hack-one-local-variable (var val)
940 (cond ((eq var 'mode)
941 (funcall (intern (concat (downcase (symbol-name val))
942 "-mode"))))
943 ((memq var ignored-local-variables)
944 nil)
945 ;; "Setting" eval means either eval it or do nothing.
946 ((eq var 'eval)
947 (if (and (not (string= (user-login-name) "root"))
948 (or (eq enable-local-eval t)
949 (and enable-local-eval
950 (save-window-excursion
951 (switch-to-buffer (current-buffer))
952 (save-excursion
953 (beginning-of-line)
954 (set-window-start (selected-window) (point)))
955 (setq enable-local-eval
956 (y-or-n-p (format "Process `eval' local variable in file %s? "
957 (file-name-nondirectory buffer-file-name))))))))
958 (save-excursion (eval val))
959 (message "Ignoring `eval:' in file's local variables")))
960 ;; Ordinary variable, really set it.
961 (t (make-local-variable var)
962 (set var val))))
963
964 \f
965 (defun set-visited-file-name (filename)
966 "Change name of file visited in current buffer to FILENAME.
967 The next time the buffer is saved it will go in the newly specified file.
968 nil or empty string as argument means make buffer not be visiting any file.
969 Remember to delete the initial contents of the minibuffer
970 if you wish to pass an empty string as the argument."
971 (interactive "FSet visited file name: ")
972 (if filename
973 (setq filename
974 (if (string-equal filename "")
975 nil
976 (expand-file-name filename))))
977 (or (equal filename buffer-file-name)
978 (null filename)
979 (progn
980 (lock-buffer filename)
981 (unlock-buffer)))
982 (setq buffer-file-name filename)
983 (if filename ; make buffer name reflect filename.
984 (let ((new-name (file-name-nondirectory buffer-file-name)))
985 (if (string= new-name "")
986 (error "Empty file name"))
987 (if (eq system-type 'vax-vms)
988 (setq new-name (downcase new-name)))
989 (setq default-directory (file-name-directory buffer-file-name))
990 (rename-buffer new-name t)))
991 (setq buffer-backed-up nil)
992 (clear-visited-file-modtime)
993 (if filename
994 (progn
995 (setq buffer-file-truename
996 (abbreviate-file-name (file-truename buffer-file-name)))
997 (if find-file-visit-truename
998 (setq buffer-file-name buffer-file-truename))
999 (setq buffer-file-number (nth 10 (file-attributes buffer-file-name))))
1000 (setq buffer-file-truename nil buffer-file-number nil))
1001 ;; write-file-hooks is normally used for things like ftp-find-file
1002 ;; that visit things that are not local files as if they were files.
1003 ;; Changing to visit an ordinary local file instead should flush the hook.
1004 (kill-local-variable 'write-file-hooks)
1005 (kill-local-variable 'local-write-file-hooks)
1006 (kill-local-variable 'revert-buffer-function)
1007 (kill-local-variable 'backup-inhibited)
1008 ;; Turn off backup files for certain file names.
1009 ;; Since this is a permanent local, the major mode won't eliminate it.
1010 (and (not (funcall backup-enable-predicate buffer-file-name))
1011 (progn
1012 (make-local-variable 'backup-inhibited)
1013 (setq backup-inhibited t)))
1014 ;; If auto-save was not already on, turn it on if appropriate.
1015 (if (not buffer-auto-save-file-name)
1016 (auto-save-mode (and buffer-file-name auto-save-default))
1017 ;; If auto save is on, start using a new name.
1018 ;; We deliberately don't rename or delete the old auto save
1019 ;; for the old visited file name. This is because perhaps
1020 ;; the user wants to save the new state and then compare with the
1021 ;; previous state from the auto save file.
1022 (setq buffer-auto-save-file-name
1023 (make-auto-save-file-name)))
1024 (if buffer-file-name
1025 (set-buffer-modified-p t)))
1026
1027 (defun write-file (filename)
1028 "Write current buffer into file FILENAME.
1029 Makes buffer visit that file, and marks it not modified.
1030 If the buffer is already visiting a file, you can specify
1031 a directory name as FILENAME, to write a file of the same
1032 old name in that directory."
1033 ;; (interactive "FWrite file: ")
1034 (interactive
1035 (list (if buffer-file-name
1036 (read-file-name "Write file: "
1037 nil nil nil nil)
1038 (read-file-name "Write file: "
1039 (cdr (assq 'default-directory
1040 (buffer-local-variables)))
1041 nil nil (buffer-name)))))
1042 (or (null filename) (string-equal filename "")
1043 (progn
1044 ;; If arg is just a directory,
1045 ;; use same file name, but in that directory.
1046 (if (and (file-directory-p filename) buffer-file-name)
1047 (setq filename (concat (file-name-as-directory filename)
1048 (file-name-nondirectory buffer-file-name))))
1049 (set-visited-file-name filename)))
1050 (set-buffer-modified-p t)
1051 (save-buffer))
1052 \f
1053 (defun backup-buffer ()
1054 "Make a backup of the disk file visited by the current buffer, if appropriate.
1055 This is normally done before saving the buffer the first time.
1056 If the value is non-nil, it is the result of `file-modes' on the original
1057 file; this means that the caller, after saving the buffer, should change
1058 the modes of the new file to agree with the old modes."
1059 (if (and make-backup-files (not backup-inhibited)
1060 (not buffer-backed-up)
1061 (file-exists-p buffer-file-name)
1062 (memq (aref (elt (file-attributes buffer-file-name) 8) 0)
1063 '(?- ?l)))
1064 (let ((real-file-name buffer-file-name)
1065 backup-info backupname targets setmodes)
1066 ;; If specified name is a symbolic link, chase it to the target.
1067 ;; Thus we make the backups in the directory where the real file is.
1068 (while (let ((tem (file-symlink-p real-file-name)))
1069 (if tem
1070 (setq real-file-name
1071 (expand-file-name tem
1072 (file-name-directory real-file-name))))
1073 tem))
1074 (setq backup-info (find-backup-file-name real-file-name)
1075 backupname (car backup-info)
1076 targets (cdr backup-info))
1077 ;;; (if (file-directory-p buffer-file-name)
1078 ;;; (error "Cannot save buffer in directory %s" buffer-file-name))
1079 (condition-case ()
1080 (let ((delete-old-versions
1081 ;; If have old versions to maybe delete,
1082 ;; ask the user to confirm now, before doing anything.
1083 ;; But don't actually delete til later.
1084 (and targets
1085 (or (eq trim-versions-without-asking t) (eq trim-versions-without-asking nil))
1086 (or trim-versions-without-asking
1087 (y-or-n-p (format "Delete excess backup versions of %s? "
1088 real-file-name))))))
1089 ;; Actually write the back up file.
1090 (condition-case ()
1091 (if (or file-precious-flag
1092 ; (file-symlink-p buffer-file-name)
1093 backup-by-copying
1094 (and backup-by-copying-when-linked
1095 (> (file-nlinks real-file-name) 1))
1096 (and backup-by-copying-when-mismatch
1097 (let ((attr (file-attributes real-file-name)))
1098 (or (nth 9 attr)
1099 (/= (nth 2 attr) (user-uid))))))
1100 (condition-case ()
1101 (copy-file real-file-name backupname t t)
1102 (file-error
1103 ;; If copying fails because file BACKUPNAME
1104 ;; is not writable, delete that file and try again.
1105 (if (and (file-exists-p backupname)
1106 (not (file-writable-p backupname)))
1107 (delete-file backupname))
1108 (copy-file real-file-name backupname t t)))
1109 ;; rename-file should delete old backup.
1110 (rename-file real-file-name backupname t)
1111 (setq setmodes (file-modes backupname)))
1112 (file-error
1113 ;; If trouble writing the backup, write it in ~.
1114 (setq backupname (expand-file-name "~/%backup%~"))
1115 (message "Cannot write backup file; backing up in ~/%%backup%%~")
1116 (sleep-for 1)
1117 (condition-case ()
1118 (copy-file real-file-name backupname t t)
1119 (file-error
1120 ;; If copying fails because file BACKUPNAME
1121 ;; is not writable, delete that file and try again.
1122 (if (and (file-exists-p backupname)
1123 (not (file-writable-p backupname)))
1124 (delete-file backupname))
1125 (copy-file real-file-name backupname t t)))))
1126 (setq buffer-backed-up t)
1127 ;; Now delete the old versions, if desired.
1128 (if delete-old-versions
1129 (while targets
1130 (condition-case ()
1131 (delete-file (car targets))
1132 (file-error nil))
1133 (setq targets (cdr targets))))
1134 setmodes)
1135 (file-error nil)))))
1136
1137 (defun file-name-sans-versions (name &optional keep-backup-version)
1138 "Return FILENAME sans backup versions or strings.
1139 This is a separate procedure so your site-init or startup file can
1140 redefine it.
1141 If the optional argument KEEP-BACKUP-VERSION is non-nil,
1142 we do not remove backup version numbers, only true file version numbers."
1143 (let ((handler (find-file-name-handler name)))
1144 (if handler
1145 (funcall handler 'file-name-sans-versions name keep-backup-version)
1146 (substring name 0
1147 (if (eq system-type 'vax-vms)
1148 ;; VMS version number is (a) semicolon, optional
1149 ;; sign, zero or more digits or (b) period, option
1150 ;; sign, zero or more digits, provided this is the
1151 ;; second period encountered outside of the
1152 ;; device/directory part of the file name.
1153 (or (string-match ";[---+]?[0-9]*\\'" name)
1154 (if (string-match "\\.[^]>:]*\\(\\.[---+]?[0-9]*\\)\\'"
1155 name)
1156 (match-beginning 1))
1157 (length name))
1158 (if keep-backup-version
1159 (length name)
1160 (or (string-match "\\.~[0-9]+~\\'" name)
1161 (string-match "~\\'" name)
1162 (length name))))))))
1163
1164 (defun make-backup-file-name (file)
1165 "Create the non-numeric backup file name for FILE.
1166 This is a separate function so you can redefine it for customization."
1167 (concat file "~"))
1168
1169 (defun backup-file-name-p (file)
1170 "Return non-nil if FILE is a backup file name (numeric or not).
1171 This is a separate function so you can redefine it for customization.
1172 You may need to redefine `file-name-sans-versions' as well."
1173 (string-match "~$" file))
1174
1175 ;; This is used in various files.
1176 ;; The usage of bv-length is not very clean,
1177 ;; but I can't see a good alternative,
1178 ;; so as of now I am leaving it alone.
1179 (defun backup-extract-version (fn)
1180 "Given the name of a numeric backup file, return the backup number.
1181 Uses the free variable `bv-length', whose value should be
1182 the index in the name where the version number begins."
1183 (if (and (string-match "[0-9]+~$" fn bv-length)
1184 (= (match-beginning 0) bv-length))
1185 (string-to-int (substring fn bv-length -1))
1186 0))
1187
1188 ;; I believe there is no need to alter this behavior for VMS;
1189 ;; since backup files are not made on VMS, it should not get called.
1190 (defun find-backup-file-name (fn)
1191 "Find a file name for a backup file, and suggestions for deletions.
1192 Value is a list whose car is the name for the backup file
1193 and whose cdr is a list of old versions to consider deleting now."
1194 (if (eq version-control 'never)
1195 (list (make-backup-file-name fn))
1196 (let* ((base-versions (concat (file-name-nondirectory fn) ".~"))
1197 (bv-length (length base-versions))
1198 (possibilities (file-name-all-completions
1199 base-versions
1200 (file-name-directory fn)))
1201 (versions (sort (mapcar
1202 (function backup-extract-version)
1203 possibilities)
1204 '<))
1205 (high-water-mark (apply 'max 0 versions))
1206 (deserve-versions-p
1207 (or version-control
1208 (> high-water-mark 0)))
1209 (number-to-delete (- (length versions)
1210 kept-old-versions kept-new-versions -1)))
1211 (if (not deserve-versions-p)
1212 (list (make-backup-file-name fn))
1213 (cons (concat fn ".~" (int-to-string (1+ high-water-mark)) "~")
1214 (if (and (> number-to-delete 0)
1215 ;; Delete nothing if there is overflow
1216 ;; in the number of versions to keep.
1217 (>= (+ kept-new-versions kept-old-versions -1) 0))
1218 (mapcar (function (lambda (n)
1219 (concat fn ".~" (int-to-string n) "~")))
1220 (let ((v (nthcdr kept-old-versions versions)))
1221 (rplacd (nthcdr (1- number-to-delete) v) ())
1222 v))))))))
1223
1224 (defun file-nlinks (filename)
1225 "Return number of names file FILENAME has."
1226 (car (cdr (file-attributes filename))))
1227
1228 (defun file-relative-name-1 (directory)
1229 (cond ((string= directory "/")
1230 filename)
1231 ((string-match (concat "^" (regexp-quote directory))
1232 filename)
1233 (substring filename (match-end 0)))
1234 (t
1235 (file-relative-name-1
1236 (file-name-directory (substring directory 0 -1))))))
1237
1238 (defun file-relative-name (filename &optional directory)
1239 "Convert FILENAME to be relative to DIRECTORY (default: default-directory)."
1240 (setq filename (expand-file-name filename)
1241 directory (file-name-as-directory (if directory
1242 (expand-file-name directory)
1243 default-directory)))
1244 (file-relative-name-1 directory))
1245 \f
1246 (defun save-buffer (&optional args)
1247 "Save current buffer in visited file if modified. Versions described below.
1248 By default, makes the previous version into a backup file
1249 if previously requested or if this is the first save.
1250 With 1 or 3 \\[universal-argument]'s, marks this version
1251 to become a backup when the next save is done.
1252 With 2 or 3 \\[universal-argument]'s,
1253 unconditionally makes the previous version into a backup file.
1254 With argument of 0, never makes the previous version into a backup file.
1255
1256 If a file's name is FOO, the names of its numbered backup versions are
1257 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~.
1258 Numeric backups (rather than FOO~) will be made if value of
1259 `version-control' is not the atom `never' and either there are already
1260 numeric versions of the file being backed up, or `version-control' is
1261 non-nil.
1262 We don't want excessive versions piling up, so there are variables
1263 `kept-old-versions', which tells Emacs how many oldest versions to keep,
1264 and `kept-new-versions', which tells how many newest versions to keep.
1265 Defaults are 2 old versions and 2 new.
1266 `dired-kept-versions' controls dired's clean-directory (.) command.
1267 If `trim-versions-without-asking' is nil, system will query user
1268 before trimming versions. Otherwise it does it silently."
1269 (interactive "p")
1270 (let ((modp (buffer-modified-p))
1271 (large (> (buffer-size) 50000))
1272 (make-backup-files (and make-backup-files (not (eq args 0)))))
1273 (and modp (memq args '(16 64)) (setq buffer-backed-up nil))
1274 (if (and modp large) (message "Saving file %s..." (buffer-file-name)))
1275 (basic-save-buffer)
1276 (and modp (memq args '(4 64)) (setq buffer-backed-up nil))))
1277
1278 (defun delete-auto-save-file-if-necessary (&optional force)
1279 "Delete auto-save file for current buffer if `delete-auto-save-files' is t.
1280 Normally delete only if the file was written by this Emacs since
1281 the last real save, but optional arg FORCE non-nil means delete anyway."
1282 (and buffer-auto-save-file-name delete-auto-save-files
1283 (not (string= buffer-file-name buffer-auto-save-file-name))
1284 (or force (recent-auto-save-p))
1285 (progn
1286 (condition-case ()
1287 (delete-file buffer-auto-save-file-name)
1288 (file-error nil))
1289 (set-buffer-auto-saved))))
1290
1291 (defun basic-save-buffer ()
1292 "Save the current buffer in its visited file, if it has been modified."
1293 (interactive)
1294 (if (buffer-modified-p)
1295 (let ((recent-save (recent-auto-save-p))
1296 setmodes tempsetmodes)
1297 ;; On VMS, rename file and buffer to get rid of version number.
1298 (if (and (eq system-type 'vax-vms)
1299 (not (string= buffer-file-name
1300 (file-name-sans-versions buffer-file-name))))
1301 (let (buffer-new-name)
1302 ;; Strip VMS version number before save.
1303 (setq buffer-file-name
1304 (file-name-sans-versions buffer-file-name))
1305 ;; Construct a (unique) buffer name to correspond.
1306 (let ((buf (create-file-buffer (downcase buffer-file-name))))
1307 (setq buffer-new-name (buffer-name buf))
1308 (kill-buffer buf))
1309 (rename-buffer buffer-new-name)))
1310 ;; If buffer has no file name, ask user for one.
1311 (or buffer-file-name
1312 (progn
1313 (setq buffer-file-name
1314 (expand-file-name (read-file-name "File to save in: ") nil)
1315 default-directory (file-name-directory buffer-file-name))
1316 (auto-save-mode auto-save-default)))
1317 (or (verify-visited-file-modtime (current-buffer))
1318 (not (file-exists-p buffer-file-name))
1319 (yes-or-no-p
1320 (format "%s has changed since visited or saved. Save anyway? "
1321 (file-name-nondirectory buffer-file-name)))
1322 (error "Save not confirmed"))
1323 (save-restriction
1324 (widen)
1325 (and (> (point-max) 1)
1326 (/= (char-after (1- (point-max))) ?\n)
1327 (or (eq require-final-newline t)
1328 (and require-final-newline
1329 (y-or-n-p
1330 (format "Buffer %s does not end in newline. Add one? "
1331 (buffer-name)))))
1332 (save-excursion
1333 (goto-char (point-max))
1334 (insert ?\n)))
1335 (let ((hooks (append write-contents-hooks local-write-file-hooks
1336 write-file-hooks))
1337 (done nil))
1338 (while (and hooks
1339 (not (setq done (funcall (car hooks)))))
1340 (setq hooks (cdr hooks)))
1341 ;; If a hook returned t, file is already "written".
1342 (cond ((not done)
1343 (if (not (file-writable-p buffer-file-name))
1344 (let ((dir (file-name-directory buffer-file-name)))
1345 (if (not (file-directory-p dir))
1346 (error "%s is not a directory" dir)
1347 (if (not (file-exists-p buffer-file-name))
1348 (error "Directory %s write-protected" dir)
1349 (if (yes-or-no-p
1350 (format "File %s is write-protected; try to save anyway? "
1351 (file-name-nondirectory
1352 buffer-file-name)))
1353 (setq tempsetmodes t)
1354 (error "Attempt to save to a file which you aren't allowed to write"))))))
1355 (or buffer-backed-up
1356 (setq setmodes (backup-buffer)))
1357 (if file-precious-flag
1358 ;; If file is precious, write temp name, then rename it.
1359 (let ((dir (file-name-directory buffer-file-name))
1360 (realname buffer-file-name)
1361 tempname temp nogood i succeed)
1362 (setq i 0)
1363 (setq nogood t)
1364 ;; Find the temporary name to write under.
1365 (while nogood
1366 (setq tempname (format "%s#tmp#%d" dir i))
1367 (setq nogood (file-exists-p tempname))
1368 (setq i (1+ i)))
1369 (unwind-protect
1370 (progn (clear-visited-file-modtime)
1371 (write-region (point-min) (point-max)
1372 tempname nil realname)
1373 (setq succeed t))
1374 ;; If writing the temp file fails,
1375 ;; delete the temp file.
1376 (or succeed (delete-file tempname)))
1377 ;; Since we have created an entirely new file
1378 ;; and renamed it, make sure it gets the
1379 ;; right permission bits set.
1380 (setq setmodes (file-modes buffer-file-name))
1381 ;; We succeeded in writing the temp file,
1382 ;; so rename it.
1383 (rename-file tempname buffer-file-name t))
1384 ;; If file not writable, see if we can make it writable
1385 ;; temporarily while we write it.
1386 ;; But no need to do so if we have just backed it up
1387 ;; (setmodes is set) because that says we're superseding.
1388 (cond ((and tempsetmodes (not setmodes))
1389 ;; Change the mode back, after writing.
1390 (setq setmodes (file-modes buffer-file-name))
1391 (set-file-modes buffer-file-name 511)))
1392 (write-region (point-min) (point-max)
1393 buffer-file-name nil t)))))
1394 (setq buffer-file-number (nth 10 (file-attributes buffer-file-name)))
1395 (if setmodes
1396 (condition-case ()
1397 (set-file-modes buffer-file-name setmodes)
1398 (error nil))))
1399 ;; If the auto-save file was recent before this command,
1400 ;; delete it now.
1401 (delete-auto-save-file-if-necessary recent-save)
1402 (run-hooks 'after-save-hooks))
1403 (message "(No changes need to be saved)")))
1404
1405 (defun save-some-buffers (&optional arg exiting)
1406 "Save some modified file-visiting buffers. Asks user about each one.
1407 Optional argument (the prefix) non-nil means save all with no questions.
1408 Optional second argument EXITING means ask about certain non-file buffers
1409 as well as about file buffers."
1410 (interactive "P")
1411 (save-window-excursion
1412 (if (zerop (map-y-or-n-p
1413 (function
1414 (lambda (buffer)
1415 (and (buffer-modified-p buffer)
1416 (or
1417 (buffer-file-name buffer)
1418 (and exiting
1419 (progn
1420 (set-buffer buffer)
1421 (and buffer-offer-save (> (buffer-size) 0)))))
1422 (if arg
1423 t
1424 (if (buffer-file-name buffer)
1425 (format "Save file %s? "
1426 (buffer-file-name buffer))
1427 (format "Save buffer %s? "
1428 (buffer-name buffer)))))))
1429 (function
1430 (lambda (buffer)
1431 (set-buffer buffer)
1432 (save-buffer)))
1433 (buffer-list)
1434 '("buffer" "buffers" "save")
1435 (list (list ?\C-r (lambda (buf)
1436 (view-buffer buf)
1437 (setq view-exit-action
1438 '(lambda (ignore)
1439 (exit-recursive-edit)))
1440 (recursive-edit)
1441 ;; Return nil to ask about BUF again.
1442 nil)
1443 "display the current buffer"))
1444 ))
1445 (message "(No files need saving)"))))
1446 \f
1447 (defun not-modified (&optional arg)
1448 "Mark current buffer as unmodified, not needing to be saved.
1449 With prefix arg, mark buffer as modified, so \\[save-buffer] will save."
1450 (interactive "P")
1451 (message (if arg "Modification-flag set"
1452 "Modification-flag cleared"))
1453 (set-buffer-modified-p arg))
1454
1455 (defun toggle-read-only (&optional arg)
1456 "Change whether this buffer is visiting its file read-only.
1457 With arg, set read-only iff arg is positive."
1458 (interactive "P")
1459 (setq buffer-read-only
1460 (if (null arg)
1461 (not buffer-read-only)
1462 (> (prefix-numeric-value arg) 0)))
1463 ;; Force mode-line redisplay
1464 (set-buffer-modified-p (buffer-modified-p)))
1465
1466 (defun insert-file (filename)
1467 "Insert contents of file FILENAME into buffer after point.
1468 Set mark after the inserted text.
1469
1470 This function is meant for the user to run interactively.
1471 Don't call it from programs! Use `insert-file-contents' instead.
1472 \(Its calling sequence is different; see its documentation)."
1473 (interactive "fInsert file: ")
1474 (let ((tem (insert-file-contents filename)))
1475 (push-mark (+ (point) (car (cdr tem))))))
1476
1477 (defun append-to-file (start end filename)
1478 "Append the contents of the region to the end of file FILENAME.
1479 When called from a function, expects three arguments,
1480 START, END and FILENAME. START and END are buffer positions
1481 saying what text to write."
1482 (interactive "r\nFAppend to file: ")
1483 (write-region start end filename t))
1484
1485 (defun file-newest-backup (filename)
1486 "Return most recent backup file for FILENAME or nil if no backups exist."
1487 (let* ((filename (expand-file-name filename))
1488 (file (file-name-nondirectory filename))
1489 (dir (file-name-directory filename))
1490 (comp (file-name-all-completions file dir))
1491 newest)
1492 (while comp
1493 (setq file (concat dir (car comp))
1494 comp (cdr comp))
1495 (if (and (backup-file-name-p file)
1496 (or (null newest) (file-newer-than-file-p file newest)))
1497 (setq newest file)))
1498 newest))
1499
1500 (defun rename-uniquely ()
1501 "Rename current buffer to a similar name not already taken.
1502 This function is useful for creating multiple shell process buffers
1503 or multiple mail buffers, etc."
1504 (interactive)
1505 (let* ((new-buf (generate-new-buffer (buffer-name)))
1506 (name (buffer-name new-buf)))
1507 (kill-buffer new-buf)
1508 (rename-buffer name)
1509 (set-buffer-modified-p (buffer-modified-p)))) ; force mode line update
1510
1511 (defun make-directory (dir &optional parents)
1512 "Create the directory DIR and any nonexistent parent dirs."
1513 (interactive "FMake directory: \nP")
1514 (let ((handler (find-file-name-handler dir)))
1515 (if handler
1516 (funcall handler 'make-directory dir parents)
1517 (if (not parents)
1518 (make-directory-internal dir)
1519 (let ((dir (directory-file-name (expand-file-name dir)))
1520 create-list)
1521 (while (not (file-exists-p dir))
1522 (setq create-list (cons dir create-list)
1523 dir (directory-file-name (file-name-directory dir))))
1524 (while create-list
1525 (make-directory-internal (car create-list))
1526 (setq create-list (cdr create-list))))))))
1527 \f
1528 (put 'revert-buffer-function 'permanent-local t)
1529 (defvar revert-buffer-function nil
1530 "Function to use to revert this buffer, or nil to do the default.")
1531
1532 (put 'revert-buffer-insert-file-contents-function 'permanent-local t)
1533 (defvar revert-buffer-insert-file-contents-function nil
1534 "Function to use to insert contents when reverting this buffer.
1535 Gets two args, first the nominal file name to use,
1536 and second, t if reading the auto-save file.")
1537
1538 (defun revert-buffer (&optional ignore-auto noconfirm)
1539 "Replace the buffer text with the text of the visited file on disk.
1540 This undoes all changes since the file was visited or saved.
1541 With a prefix argument, offer to revert from latest auto-save file, if
1542 that is more recent than the visited file.
1543
1544 When called from lisp, the first argument is IGNORE-AUTO; only offer
1545 to revert from the auto-save file when this is nil. Note that the
1546 sense of this argument is the reverse of the prefix argument, for the
1547 sake of backward compatibility. IGNORE-AUTO is optional, defaulting
1548 to nil.
1549
1550 Optional second argument NOCONFIRM means don't ask for confirmation at
1551 all.
1552
1553 If the value of `revert-buffer-function' is non-nil, it is called to
1554 do the work."
1555 ;; I admit it's odd to reverse the sense of the prefix argument, but
1556 ;; there is a lot of code out there which assumes that the first
1557 ;; argument should be t to avoid consulting the auto-save file, and
1558 ;; there's no straightforward way to encourage authors to notice a
1559 ;; reversal of the argument sense. So I'm just changing the user
1560 ;; interface, but leaving the programmatic interface the same.
1561 (interactive (list (not prefix-arg)))
1562 (if revert-buffer-function
1563 (funcall revert-buffer-function ignore-auto noconfirm)
1564 (let* ((opoint (point))
1565 (auto-save-p (and (not ignore-auto)
1566 (recent-auto-save-p)
1567 buffer-auto-save-file-name
1568 (file-readable-p buffer-auto-save-file-name)
1569 (y-or-n-p
1570 "Buffer has been auto-saved recently. Revert from auto-save file? ")))
1571 (file-name (if auto-save-p
1572 buffer-auto-save-file-name
1573 buffer-file-name)))
1574 (cond ((null file-name)
1575 (error "Buffer does not seem to be associated with any file"))
1576 ((or noconfirm
1577 (yes-or-no-p (format "Revert buffer from file %s? "
1578 file-name)))
1579 ;; If file was backed up but has changed since,
1580 ;; we shd make another backup.
1581 (and (not auto-save-p)
1582 (not (verify-visited-file-modtime (current-buffer)))
1583 (setq buffer-backed-up nil))
1584 ;; Get rid of all undo records for this buffer.
1585 (or (eq buffer-undo-list t)
1586 (setq buffer-undo-list nil))
1587 (let ((buffer-read-only nil)
1588 ;; Don't make undo records for the reversion.
1589 (buffer-undo-list t))
1590 (if revert-buffer-insert-file-contents-function
1591 (funcall revert-buffer-insert-file-contents-function
1592 file-name auto-save-p)
1593 (if (not (file-exists-p file-name))
1594 (error "File %s no longer exists!" file-name))
1595 ;; Bind buffer-file-name to nil
1596 ;; so that we don't try to lock the file.
1597 (let ((buffer-file-name nil))
1598 (or auto-save-p
1599 (unlock-buffer))
1600 (erase-buffer))
1601 (insert-file-contents file-name (not auto-save-p))))
1602 (goto-char (min opoint (point-max)))
1603 (after-find-file nil nil t)
1604 t)))))
1605
1606 (defun recover-file (file)
1607 "Visit file FILE, but get contents from its last auto-save file."
1608 (interactive
1609 (let ((prompt-file buffer-file-name)
1610 (file-name nil)
1611 (file-dir nil))
1612 (and prompt-file
1613 (setq file-name (file-name-nondirectory prompt-file)
1614 file-dir (file-name-directory prompt-file)))
1615 (list (read-file-name "Recover file: "
1616 file-dir nil nil file-name))))
1617 (setq file (expand-file-name file))
1618 (if (auto-save-file-name-p file) (error "%s is an auto-save file" file))
1619 (let ((file-name (let ((buffer-file-name file))
1620 (make-auto-save-file-name))))
1621 (cond ((not (file-newer-than-file-p file-name file))
1622 (error "Auto-save file %s not current" file-name))
1623 ((save-window-excursion
1624 (if (not (eq system-type 'vax-vms))
1625 (with-output-to-temp-buffer "*Directory*"
1626 (buffer-disable-undo standard-output)
1627 (call-process "ls" nil standard-output nil
1628 (if (file-symlink-p file) "-lL" "-l")
1629 file file-name)))
1630 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
1631 (switch-to-buffer (find-file-noselect file t))
1632 (let ((buffer-read-only nil))
1633 (erase-buffer)
1634 (insert-file-contents file-name nil))
1635 (after-find-file nil nil t))
1636 (t (error "Recover-file cancelled.")))))
1637
1638 (defun kill-some-buffers ()
1639 "For each buffer, ask whether to kill it."
1640 (interactive)
1641 (let ((list (buffer-list)))
1642 (while list
1643 (let* ((buffer (car list))
1644 (name (buffer-name buffer)))
1645 (and (not (string-equal name ""))
1646 (/= (aref name 0) ? )
1647 (yes-or-no-p
1648 (format "Buffer %s %s. Kill? "
1649 name
1650 (if (buffer-modified-p buffer)
1651 "HAS BEEN EDITED" "is unmodified")))
1652 (kill-buffer buffer)))
1653 (setq list (cdr list)))))
1654 \f
1655 (defun auto-save-mode (arg)
1656 "Toggle auto-saving of contents of current buffer.
1657 With prefix argument ARG, turn auto-saving on if positive, else off."
1658 (interactive "P")
1659 (setq buffer-auto-save-file-name
1660 (and (if (null arg)
1661 (not buffer-auto-save-file-name)
1662 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))
1663 (if (and buffer-file-name auto-save-visited-file-name
1664 (not buffer-read-only))
1665 buffer-file-name
1666 (make-auto-save-file-name))))
1667 (if (interactive-p)
1668 (message "Auto-save %s (in this buffer)"
1669 (if buffer-auto-save-file-name "on" "off")))
1670 buffer-auto-save-file-name)
1671
1672 (defun rename-auto-save-file ()
1673 "Adjust current buffer's auto save file name for current conditions.
1674 Also rename any existing auto save file, if it was made in this session."
1675 (let ((osave buffer-auto-save-file-name))
1676 (setq buffer-auto-save-file-name
1677 (make-auto-save-file-name))
1678 (if (and osave buffer-auto-save-file-name
1679 (not (string= buffer-auto-save-file-name buffer-file-name))
1680 (not (string= buffer-auto-save-file-name osave))
1681 (file-exists-p osave)
1682 (recent-auto-save-p))
1683 (rename-file osave buffer-auto-save-file-name t))))
1684
1685 (defun make-auto-save-file-name ()
1686 "Return file name to use for auto-saves of current buffer.
1687 Does not consider `auto-save-visited-file-name' as that variable is checked
1688 before calling this function. You can redefine this for customization.
1689 See also `auto-save-file-name-p'."
1690 (if buffer-file-name
1691 (concat (file-name-directory buffer-file-name)
1692 "#"
1693 (file-name-nondirectory buffer-file-name)
1694 "#")
1695 ;; For non-file bfr, use bfr name and Emacs pid.
1696 (expand-file-name (format "#%s#%s#" (buffer-name) (make-temp-name "")))))
1697
1698 (defun auto-save-file-name-p (filename)
1699 "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'.
1700 FILENAME should lack slashes. You can redefine this for customization."
1701 (string-match "^#.*#$" filename))
1702 \f
1703 (defconst list-directory-brief-switches
1704 (if (eq system-type 'vax-vms) "" "-CF")
1705 "*Switches for list-directory to pass to `ls' for brief listing,")
1706
1707 (defconst list-directory-verbose-switches
1708 (if (eq system-type 'vax-vms)
1709 "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)"
1710 "-l")
1711 "*Switches for list-directory to pass to `ls' for verbose listing,")
1712
1713 (defun list-directory (dirname &optional verbose)
1714 "Display a list of files in or matching DIRNAME, a la `ls'.
1715 DIRNAME is globbed by the shell if necessary.
1716 Prefix arg (second arg if noninteractive) means supply -l switch to `ls'.
1717 Actions controlled by variables `list-directory-brief-switches'
1718 and `list-directory-verbose-switches'."
1719 (interactive (let ((pfx current-prefix-arg))
1720 (list (read-file-name (if pfx "List directory (verbose): "
1721 "List directory (brief): ")
1722 nil default-directory nil)
1723 pfx)))
1724 (let ((switches (if verbose list-directory-verbose-switches
1725 list-directory-brief-switches)))
1726 (or dirname (setq dirname default-directory))
1727 (setq dirname (expand-file-name dirname))
1728 (with-output-to-temp-buffer "*Directory*"
1729 (buffer-disable-undo standard-output)
1730 (princ "Directory ")
1731 (princ dirname)
1732 (terpri)
1733 (save-excursion
1734 (set-buffer "*Directory*")
1735 (let ((wildcard (not (file-directory-p dirname))))
1736 (insert-directory dirname switches wildcard (not wildcard)))))))
1737
1738 (defvar insert-directory-program "ls"
1739 "Absolute or relative name of the `ls' program used by `insert-directory'.")
1740
1741 ;; insert-directory
1742 ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and
1743 ;; FULL-DIRECTORY-P is nil.
1744 ;; The single line of output must display FILE's name as it was
1745 ;; given, namely, an absolute path name.
1746 ;; - must insert exactly one line for each file if WILDCARD or
1747 ;; FULL-DIRECTORY-P is t, plus one optional "total" line
1748 ;; before the file lines, plus optional text after the file lines.
1749 ;; Lines are delimited by "\n", so filenames containing "\n" are not
1750 ;; allowed.
1751 ;; File lines should display the basename.
1752 ;; - must be consistent with
1753 ;; - functions dired-move-to-filename, (these two define what a file line is)
1754 ;; dired-move-to-end-of-filename,
1755 ;; dired-between-files, (shortcut for (not (dired-move-to-filename)))
1756 ;; dired-insert-headerline
1757 ;; dired-after-subdir-garbage (defines what a "total" line is)
1758 ;; - variable dired-subdir-regexp
1759 (defun insert-directory (file switches &optional wildcard full-directory-p)
1760 "Insert directory listing for of FILE, formatted according to SWITCHES.
1761 Leaves point after the inserted text.
1762 Optional third arg WILDCARD means treat FILE as shell wildcard.
1763 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
1764 switches do not contain `d', so that a full listing is expected.
1765
1766 This works by running a directory listing program
1767 whose name is in the variable `insert-directory-program'.
1768 If WILDCARD, it also runs the shell specified by `shell-file-name'."
1769 (let ((handler (find-file-name-handler file)))
1770 (if handler
1771 (funcall handler 'insert-directory file switches
1772 wildcard full-directory-p)
1773 (if (eq system-type 'vax-vms)
1774 (vms-read-directory file switches (current-buffer))
1775 (if wildcard
1776 (let ((default-directory (file-name-directory file)))
1777 (call-process shell-file-name nil t nil
1778 "-c" (concat insert-directory-program
1779 " -d " switches " "
1780 (file-name-nondirectory file))))
1781 ;; Chase links till we reach a non-link.
1782 ;; This used to be commented out, but Barry Margolin says:
1783 ;; SunOS 4.1.3 (and SV and POSIX?) lists the link
1784 ;; if we give a link to a directory - yuck!
1785 (let (symlink)
1786 (while (setq symlink (file-symlink-p file))
1787 (setq file symlink)))
1788 (call-process insert-directory-program nil t nil switches file))))))
1789
1790 (defun save-buffers-kill-emacs (&optional arg)
1791 "Offer to save each buffer, then kill this Emacs process.
1792 With prefix arg, silently save all file-visiting buffers, then kill."
1793 (interactive "P")
1794 (save-some-buffers arg t)
1795 (and (or (not (memq t (mapcar (function
1796 (lambda (buf) (and (buffer-file-name buf)
1797 (buffer-modified-p buf))))
1798 (buffer-list))))
1799 (yes-or-no-p "Modified buffers exist; exit anyway? "))
1800 (or (not (fboundp 'process-list))
1801 ;; process-list is not defined on VMS.
1802 (let ((processes (process-list))
1803 active)
1804 (while processes
1805 (and (memq (process-status (car processes)) '(run stop open))
1806 (let ((val (process-kill-without-query (car processes))))
1807 (process-kill-without-query (car processes) val)
1808 val)
1809 (setq active t))
1810 (setq processes (cdr processes)))
1811 (or (not active)
1812 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
1813 (kill-emacs)))
1814 \f
1815 (define-key ctl-x-map "\C-f" 'find-file)
1816 (define-key ctl-x-map "\C-q" 'toggle-read-only)
1817 (define-key ctl-x-map "\C-r" 'find-file-read-only)
1818 (define-key ctl-x-map "\C-v" 'find-alternate-file)
1819 (define-key ctl-x-map "\C-s" 'save-buffer)
1820 (define-key ctl-x-map "s" 'save-some-buffers)
1821 (define-key ctl-x-map "\C-w" 'write-file)
1822 (define-key ctl-x-map "i" 'insert-file)
1823 (define-key esc-map "~" 'not-modified)
1824 (define-key ctl-x-map "\C-d" 'list-directory)
1825 (define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs)
1826
1827 (define-key ctl-x-4-map "f" 'find-file-other-window)
1828 (define-key ctl-x-4-map "r" 'find-file-read-only-other-window)
1829 (define-key ctl-x-4-map "\C-f" 'find-file-other-window)
1830 (define-key ctl-x-4-map "b" 'switch-to-buffer-other-window)
1831 (define-key ctl-x-4-map "\C-o" 'display-buffer)
1832
1833 (define-key ctl-x-5-map "b" 'switch-to-buffer-other-frame)
1834 (define-key ctl-x-5-map "f" 'find-file-other-frame)
1835 (define-key ctl-x-5-map "\C-f" 'find-file-other-frame)
1836 (define-key ctl-x-5-map "r" 'find-file-read-only-other-frame)
1837
1838 ;;; files.el ends here