*** empty log message ***
[bpt/emacs.git] / lisp / files.el
CommitLineData
b4da00e9
RM
1;; File input and output commands for Emacs
2;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
3
4;; This file is part of GNU Emacs.
5
6;; GNU Emacs is free software; you can redistribute it and/or modify
7;; it under the terms of the GNU General Public License as published by
8;; the Free Software Foundation; either version 1, or (at your option)
9;; any later version.
10
11;; GNU Emacs is distributed in the hope that it will be useful,
12;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;; GNU General Public License for more details.
15
16;; You should have received a copy of the GNU General Public License
17;; along with GNU Emacs; see the file COPYING. If not, write to
18;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20(defconst delete-auto-save-files t
21 "*Non-nil means delete a buffer's auto-save file when the buffer is saved.")
22
23(defconst directory-abbrev-alist
24 nil
25 "*Alist of abbreviations for file directories.
26A list of elements of the form (FROM . TO), each meaning to replace
27FROM with TO when it appears in a directory name. This replacement is
28done when setting up the default directory of a newly visited file.
29*Every* FROM string should start with `^'.
30
31Use this feature when you have directories which you normally refer to
32via absolute symbolic links. Make TO the name of the link, and FROM
33the name it is linked to.")
34
35;;; Turn off backup files on VMS since it has version numbers.
36(defconst make-backup-files (not (eq system-type 'vax-vms))
37 "*Create a backup of each file when it is saved for the first time.
38This can be done by renaming the file or by copying.
39
40Renaming means that Emacs renames the existing file so that it is a
41backup file, then writes the buffer into a new file. Any other names
42that the old file had will now refer to the backup file. The new file
43is owned by you and its group is defaulted.
44
45Copying means that Emacs copies the existing file into the backup
46file, then writes the buffer on top of the existing file. Any other
47names that the old file had will now refer to the new (edited) file.
48The file's owner and group are unchanged.
49
50The choice of renaming or copying is controlled by the variables
51`backup-by-copying', `backup-by-copying-when-linked' and
52`backup-by-copying-when-mismatch'.")
53
54;; Do this so that local variables based on the file name
55;; are not overridden by the major mode.
56(defvar backup-inhibited nil
57 "Non-nil means don't make a backup file for this buffer.")
58(put 'backup-inhibited 'permanent-local t)
59
60(defconst backup-by-copying nil
61 "*Non-nil means always use copying to create backup files.
62See documentation of variable `make-backup-files'.")
63
64(defconst backup-by-copying-when-linked nil
65 "*Non-nil means use copying to create backups for files with multiple names.
66This causes the alternate names to refer to the latest version as edited.
67This variable is relevant only if `backup-by-copying' is nil.")
68
69(defconst backup-by-copying-when-mismatch nil
70 "*Non-nil means create backups by copying if this preserves owner or group.
71Renaming may still be used (subject to control of other variables)
72when it would not result in changing the owner or group of the file;
73that is, for files which are owned by you and whose group matches
74the default for a new file created there by you.
75This variable is relevant only if `backup-by-copying' is nil.")
76
77(defvar backup-enable-predicate
78 '(lambda (name)
79 (or (< (length name) 5)
80 (not (string-equal "/tmp/" (substring name 0 5)))))
81 "Predicate that looks at a file name and decides whether to make backups.
82Called with an absolute file name as argument, it returns t to enable backup.")
83
84(defconst buffer-offer-save nil
85 "*Non-nil in a buffer means offer to save the buffer on exit
86even if the buffer is not visiting a file.
87Automatically local in all buffers.")
88(make-variable-buffer-local 'buffer-offer-save)
89
90(defconst file-precious-flag nil
91 "*Non-nil means protect against I/O errors while saving files.
92Some modes set this non-nil in particular buffers.")
93
94(defvar version-control nil
95 "*Control use of version numbers for backup files.
96t means make numeric backup versions unconditionally.
97nil means make them for files that have some already.
98never means do not make them.")
99
100(defvar dired-kept-versions 2
101 "*When cleaning directory, number of versions to keep.")
102
103(defvar trim-versions-without-asking nil
104 "*If true, deletes excess backup versions silently.
105Otherwise asks confirmation.")
106
107(defvar kept-old-versions 2
108 "*Number of oldest versions to keep when a new numbered backup is made.")
109
110(defvar kept-new-versions 2
111 "*Number of newest versions to keep when a new numbered backup is made.
112Includes the new backup. Must be > 0")
113
114(defconst require-final-newline nil
115 "*Value of t says silently ensure a file ends in a newline when it is saved.
116Non-nil but not t says ask user whether to add a newline when there isn't one.
117nil means don't add newlines.")
118
119(defconst auto-save-default t
120 "*Non-nil says by default do auto-saving of every file-visiting buffer.")
121
122(defconst auto-save-visited-file-name nil
123 "*Non-nil says auto-save a buffer in the file it is visiting, when practical.
124Normally auto-save files are written under other names.")
125
126(defconst save-abbrevs nil
127 "*Non-nil means save word abbrevs too when files are saved.
128Loading an abbrev file sets this to t.")
129
130(defconst find-file-run-dired t
131 "*Non-nil says run dired if find-file is given the name of a directory.")
132
133(put 'find-file-not-found-hooks 'permanent-local t)
134(defvar find-file-not-found-hooks nil
135 "List of functions to be called for `find-file' on nonexistent file.
136These functions are called as soon as the error is detected.
137`buffer-file-name' is already set up.
138The functions are called in the order given until one of them returns non-nil.")
139
140(put 'find-file-hooks 'permanent-local t)
141(defvar find-file-hooks nil
142 "List of functions to be called after a buffer is loaded from a file.
143The buffer's local variables (if any) will have been processed before the
144functions are called.")
145
146(put 'write-file-hooks 'permanent-local t)
147(defvar write-file-hooks nil
148 "List of functions to be called before writing out a buffer to a file.
149If one of them returns non-nil, the file is considered already written
150and the rest are not called.")
151
152(defconst enable-local-variables t
153 "*Control use of local-variables lists in files you visit.
154The value can be t, nil or something else.
155A value of t means local-variables lists are obeyed;
156nil means they are ignored; anything else means query.
157
158The command \\[normal-mode] always obeys local-variables lists
159and ignores this variable.")
160
161(defconst ignore-local-eval nil
162 "*Non-nil means ignore the \"variable\" `eval' in a file's local variables.
163This applies when the local-variables list is scanned automatically
164after you find a file. If you explicitly request such a scan with
165\\[normal-mode], there is no query, regardless of this variable.")
166
167;; Avoid losing in versions where CLASH_DETECTION is disabled.
168(or (fboundp 'lock-buffer)
169 (fset 'lock-buffer 'ignore))
170(or (fboundp 'unlock-buffer)
171 (fset 'unlock-buffer 'ignore))
172\f
173(defun pwd ()
174 "Show the current default directory."
175 (interactive nil)
176 (message "Directory %s" default-directory))
177
178(defun cd (dir)
179 "Make DIR become the current buffer's default directory."
180 (interactive "DChange default directory: ")
181 (setq dir (expand-file-name dir))
182 (if (not (eq system-type 'vax-vms))
183 (setq dir (file-name-as-directory dir)))
184 (if (not (file-directory-p dir))
185 (error "%s is not a directory" dir)
186 (if (file-executable-p dir)
187 (setq default-directory dir)
188 (error "Cannot cd to %s: Permission denied" dir)))
189 (pwd))
190
191(defun load-file (file)
192 "Load the Lisp file named FILE."
193 (interactive "fLoad file: ")
194 (load (expand-file-name file) nil nil t))
195
196(defun load-library (library)
197 "Load the library named LIBRARY.
198This is an interface to the function `load'."
199 (interactive "sLoad library: ")
200 (load library))
201\f
202(defun switch-to-buffer-other-window (buffer)
203 "Select buffer BUFFER in another window."
204 (interactive "BSwitch to buffer in other window: ")
205 (let ((pop-up-windows t))
206 (pop-to-buffer buffer t)))
207
208(defun find-file (filename)
209 "Edit file FILENAME.
210Switch to a buffer visiting file FILENAME,
211creating one if none already exists."
212 (interactive "FFind file: ")
213 (switch-to-buffer (find-file-noselect filename)))
214
215(defun find-file-other-window (filename)
216 "Edit file FILENAME, in another window.
217May create a new window, or reuse an existing one.
218See the function `display-buffer'."
219 (interactive "FFind file in other window: ")
220 (switch-to-buffer-other-window (find-file-noselect filename)))
221
222(defun find-file-read-only (filename)
223 "Edit file FILENAME but don't allow changes.
224Like \\[find-file] but marks buffer as read-only.
225Use \\[toggle-read-only] to permit editing."
226 (interactive "fFind file read-only: ")
227 (find-file filename)
228 (setq buffer-read-only t))
229
230(defun find-file-read-only-other-window (filename)
231 "Edit file FILENAME in another window but don't allow changes.
232Like \\[find-file-other-window] but marks buffer as read-only.
233Use \\[toggle-read-only] to permit editing."
234 (interactive "fFind file read-only other window: ")
235 (find-file filename)
236 (setq buffer-read-only t))
237
238(defun find-alternate-file (filename)
239 "Find file FILENAME, select its buffer, kill previous buffer.
240If the current buffer now contains an empty file that you just visited
241\(presumably by mistake), use this command to visit the file you really want."
242 (interactive
243 (let ((file buffer-file-name)
244 (file-name nil)
245 (file-dir nil))
246 (and file
247 (setq file-name (file-name-nondirectory file)
248 file-dir (file-name-directory file)))
249 (list (read-file-name "Find alternate file: " file-dir nil nil file-name))))
250 (and (buffer-modified-p)
251 ;; (not buffer-read-only)
252 (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? "
253 (buffer-name))))
254 (error "Aborted"))
255 (let ((obuf (current-buffer))
256 (ofile buffer-file-name)
257 (oname (buffer-name)))
258 (rename-buffer " **lose**")
259 (setq buffer-file-name nil)
260 (unwind-protect
261 (progn
262 (unlock-buffer)
263 (find-file filename))
264 (cond ((eq obuf (current-buffer))
265 (setq buffer-file-name ofile)
266 (lock-buffer)
267 (rename-buffer oname))))
268 (or (eq (current-buffer) obuf)
269 (kill-buffer obuf))))
270
271(defun create-file-buffer (filename)
272 "Create a suitably named buffer for visiting FILENAME, and return it.
273FILENAME (sans directory) is used unchanged if that name is free;
274otherwise a string <2> or <3> or ... is appended to get an unused name."
275 (let ((lastname (file-name-nondirectory filename)))
276 (if (string= lastname "")
277 (setq lastname filename))
278 (generate-new-buffer lastname)))
279
280(defun find-file-noselect (filename &optional nowarn)
281 "Read file FILENAME into a buffer and return the buffer.
282If a buffer exists visiting FILENAME, return that one, but
283verify that the file has not changed since visited or saved.
284The buffer is not selected, just returned to the caller."
285 (setq filename (expand-file-name filename))
286 ;; Get rid of the prefixes added by the automounter.
287 (if (and (string-match "^/tmp_mnt/" filename)
288 (file-exists-p (file-name-directory
289 (substring filename (1- (match-end 0))))))
290 (setq filename (substring filename (1- (match-end 0)))))
291 ;; Perform any appropriate abbreviations specified in directory-abbrev-alist.
292 (let ((tail directory-abbrev-alist))
293 (while tail
294 (if (string-match (car (car tail)) filename)
295 (setq filename
296 (concat (cdr (car tail)) (substring filename (match-end 0)))))
297 (setq tail (cdr tail))))
298 (if (file-directory-p filename)
299 (if find-file-run-dired
300 (dired-noselect filename)
301 (error "%s is a directory." filename))
302 (let ((buf (get-file-buffer filename))
303 error)
304 (if buf
305 (or nowarn
306 (verify-visited-file-modtime buf)
307 (cond ((not (file-exists-p filename))
308 (error "File %s no longer exists!" filename))
309 ((yes-or-no-p
310 (format
311 (if (buffer-modified-p buf)
312 "File %s changed on disk. Discard your edits? "
313 "File %s changed on disk. Read the new version? ")
314 (file-name-nondirectory filename)))
315 (save-excursion
316 (set-buffer buf)
317 (revert-buffer t t)))))
318 (save-excursion
319 (let* ((link-name (car (file-attributes filename)))
320 (linked-buf (and (stringp link-name)
321 (get-file-buffer link-name))))
322 (if (bufferp linked-buf)
323 (message "Symbolic link to file in buffer %s"
324 (buffer-name linked-buf))))
325 (setq buf (create-file-buffer filename))
326 (set-buffer buf)
327 (erase-buffer)
328 (condition-case ()
329 (insert-file-contents filename t)
330 (file-error
331 (setq error t)
332 ;; Run find-file-not-found-hooks until one returns non-nil.
333 (let ((hooks find-file-not-found-hooks))
334 (while (and hooks
335 (not (funcall (car hooks))))
336 (setq hooks (cdr hooks))))))
337 ;; Set buffer's default directory to that of the file.
338 (setq default-directory (file-name-directory filename))
339 ;; Turn off backup files for certain file names. Since
340 ;; this is a permanent local, the major mode won't eliminate it.
341 (and (not (funcall backup-enable-predicate buffer-file-name))
342 (progn
343 (make-local-variable 'backup-inhibited)
344 (setq backup-inhibited t)))
345 (after-find-file error (not nowarn))))
346 buf)))
347\f
348(defun after-find-file (&optional error warn)
349 "Called after finding a file and by the default revert function.
350Sets buffer mode, parses local variables.
351Optional args ERROR and WARN: ERROR non-nil means there was an
352error in reading the file. WARN non-nil means warn if there
353exists an auto-save file more recent than the visited file.
354Finishes by calling the functions in `find-file-hooks'."
355 (setq buffer-read-only (not (file-writable-p buffer-file-name)))
356 (if noninteractive
357 nil
358 (let* (not-serious
359 (msg
360 (cond ((and error (file-attributes buffer-file-name))
361 (setq buffer-read-only t)
362 "File exists, but is read-protected.")
363 ((not buffer-read-only)
364 (if (and warn
365 (file-newer-than-file-p (make-auto-save-file-name)
366 buffer-file-name))
367 "Auto save file is newer; consider M-x recover-file"
368 (setq not-serious t)
369 (if error "(New file)" nil)))
370 ((not error)
371 (setq not-serious t)
372 "Note: file is write protected")
373 ((file-attributes (directory-file-name default-directory))
374 "File not found and directory write-protected")
375 (t
376 "File not found and directory doesn't exist"))))
377 (if msg
378 (progn
379 (message msg)
380 (or not-serious (sit-for 1 nil t)))))
381 (if auto-save-default
382 (auto-save-mode t)))
383 (normal-mode t)
384 (mapcar 'funcall find-file-hooks))
385
386(defun normal-mode (&optional find-file)
387 "Choose the major mode for this buffer automatically.
388Also sets up any specified local variables of the file.
389Uses the visited file name, the -*- line, and the local variables spec.
390
391This function is called automatically from `find-file'. In that case,
392we may set up specified local variables depending on the value of
393`enable-local-variables': if it is t, we do; if it is nil, we don't;
394otherwise, we query. `enable-local-variables' is ignored if you
395run `normal-mode' explicitly."
396 (interactive)
397 (or find-file (funcall (or default-major-mode 'fundamental-mode)))
398 (condition-case err
399 (set-auto-mode)
400 (error (message "File mode specification error: %s"
401 (prin1-to-string err))))
402 (condition-case err
403 (hack-local-variables (not find-file))
404 (error (message "File local-variables error: %s"
405 (prin1-to-string err)))))
406
407;(defvar auto-mode-alist ...) now in loaddefs.el
408(defun set-auto-mode ()
409 "Select major mode appropriate for current buffer.
410May base decision on visited file name (see variable `auto-mode-alist')
411or on buffer contents (-*- line or local variables spec), but does not look
412for the \"mode:\" local variable. For that, use `hack-local-variables'."
413 ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
414 (let (beg end mode)
415 (save-excursion
416 (goto-char (point-min))
417 (skip-chars-forward " \t\n")
418 (if (and (search-forward "-*-" (save-excursion (end-of-line) (point)) t)
419 (progn
420 (skip-chars-forward " \t")
421 (setq beg (point))
422 (search-forward "-*-" (save-excursion (end-of-line) (point)) t))
423 (progn
424 (forward-char -3)
425 (skip-chars-backward " \t")
426 (setq end (point))
427 (goto-char beg)
428 (if (search-forward ":" end t)
429 (progn
430 (goto-char beg)
431 (if (let ((case-fold-search t))
432 (search-forward "mode:" end t))
433 (progn
434 (skip-chars-forward " \t")
435 (setq beg (point))
436 (if (search-forward ";" end t)
437 (forward-char -1)
438 (goto-char end))
439 (skip-chars-backward " \t")
440 (setq mode (buffer-substring beg (point))))))
441 (setq mode (buffer-substring beg end)))))
442 (setq mode (intern (concat (downcase mode) "-mode")))
443 (let ((alist auto-mode-alist)
444 (name buffer-file-name))
445 (let ((case-fold-search (eq system-type 'vax-vms)))
446 ;; Remove backup-suffixes from file name.
447 (setq name (file-name-sans-versions name))
448 ;; Find first matching alist entry.
449 (while (and (not mode) alist)
450 (if (string-match (car (car alist)) name)
451 (setq mode (cdr (car alist))))
452 (setq alist (cdr alist)))))))
453 (if mode (funcall mode))))
454
455(defun hack-local-variables (&optional force)
456 "Parse (and bind or evaluate as appropriate) any local variables
457for current buffer."
458 ;; Look for "Local variables:" line in last page.
459 (save-excursion
460 (goto-char (point-max))
461 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
462 (if (let ((case-fold-search t))
463 (and (search-forward "Local Variables:" nil t)
464 (or force (eq enable-local-variables t)
465 (and enable-local-variables
466 (save-window-excursion
467 (switch-to-buffer (current-buffer))
468 (save-excursion
469 (beginning-of-line)
470 (set-window-start (selected-window) (point)))
471 (y-or-n-p (format "Set local variables as specified at end of %s? "
472 (file-name-nondirectory buffer-file-name))))))))
473 (let ((continue t)
474 prefix prefixlen suffix beg)
475 ;; The prefix is what comes before "local variables:" in its line.
476 ;; The suffix is what comes after "local variables:" in its line.
477 (skip-chars-forward " \t")
478 (or (eolp)
479 (setq suffix (buffer-substring (point)
480 (progn (end-of-line) (point)))))
481 (goto-char (match-beginning 0))
482 (or (bolp)
483 (setq prefix
484 (buffer-substring (point)
485 (progn (beginning-of-line) (point)))))
486 (if prefix (setq prefixlen (length prefix)
487 prefix (regexp-quote prefix)))
488 (if suffix (setq suffix (concat (regexp-quote suffix) "$")))
489 (while continue
490 ;; Look at next local variable spec.
491 (if selective-display (re-search-forward "[\n\C-m]")
492 (forward-line 1))
493 ;; Skip the prefix, if any.
494 (if prefix
495 (if (looking-at prefix)
496 (forward-char prefixlen)
497 (error "Local variables entry is missing the prefix")))
498 ;; Find the variable name; strip whitespace.
499 (skip-chars-forward " \t")
500 (setq beg (point))
501 (skip-chars-forward "^:\n")
502 (if (eolp) (error "Missing colon in local variables entry"))
503 (skip-chars-backward " \t")
504 (let* ((str (buffer-substring beg (point)))
505 (var (read str))
506 val)
507 ;; Setting variable named "end" means end of list.
508 (if (string-equal (downcase str) "end")
509 (setq continue nil)
510 ;; Otherwise read the variable value.
511 (skip-chars-forward "^:")
512 (forward-char 1)
513 (setq val (read (current-buffer)))
514 (skip-chars-backward "\n")
515 (skip-chars-forward " \t")
516 (or (if suffix (looking-at suffix) (eolp))
517 (error "Local variables entry is terminated incorrectly"))
518 ;; Set the variable. "Variables" mode and eval are funny.
519 (cond ((eq var 'mode)
520 (funcall (intern (concat (downcase (symbol-name val))
521 "-mode"))))
522 ((eq var 'eval)
523 (if (or (and ignore-local-eval (not force))
524 (string= (user-login-name) "root"))
525 (message "Ignoring `eval:' in file's local variables")
526 (save-excursion (eval val))))
527 (t (make-local-variable var)
528 (set var val))))))))))
529\f
530(defun set-visited-file-name (filename)
531 "Change name of file visited in current buffer to FILENAME.
532The next time the buffer is saved it will go in the newly specified file.
533nil or empty string as argument means make buffer not be visiting any file.
534Remember to delete the initial contents of the minibuffer
535if you wish to pass an empty string as the argument."
536 (interactive "FSet visited file name: ")
537 (if filename
538 (setq filename
539 (if (string-equal filename "")
540 nil
541 (expand-file-name filename))))
542 (or (equal filename buffer-file-name)
543 (null filename)
544 (progn
545 (lock-buffer filename)
546 (unlock-buffer)))
547 (setq buffer-file-name filename)
548 (if filename ; make buffer name reflect filename.
549 (let ((new-name (file-name-nondirectory buffer-file-name))
550 (old-name (buffer-name (current-buffer))))
551 (if (string= new-name "")
552 (error "Empty file name"))
553 (if (eq system-type 'vax-vms)
554 (setq new-name (downcase new-name)))
555 (setq default-directory (file-name-directory buffer-file-name))
556 (and (get-buffer new-name)
557 (setq new-name
558 (buffer-name (create-file-buffer buffer-file-name)))
559 (kill-buffer new-name))
560 (rename-buffer new-name)
561 (if (string= (prog1 (setq new-name (buffer-name (create-file-buffer
562 buffer-file-name)))
563 (kill-buffer new-name))
564 old-name)
565 (rename-buffer old-name))))
566 (setq buffer-backed-up nil)
567 (clear-visited-file-modtime)
568 ;; write-file-hooks is normally used for things like ftp-find-file
569 ;; that visit things that are not local files as if they were files.
570 ;; Changing to visit an ordinary local file instead should flush the hook.
571 (kill-local-variable 'write-file-hooks)
572 (kill-local-variable 'revert-buffer-function)
573 (kill-local-variable 'backup-inhibited)
574 ;; Turn off backup files for certain file names.
575 ;; Since this is a permanent local, the major mode won't eliminate it.
576 (and (not (funcall backup-enable-predicate buffer-file-name))
577 (progn
578 (make-local-variable 'backup-inhibited)
579 (setq backup-inhibited t)))
580 ;; If auto-save was not already on, turn it on if appropriate.
581 (if (not buffer-auto-save-file-name)
582 (auto-save-mode (and buffer-file-name auto-save-default)))
583 (if buffer-file-name
584 (set-buffer-modified-p t)))
585
586(defun write-file (filename)
587 "Write current buffer into file FILENAME.
588Makes buffer visit that file, and marks it not modified."
589;; (interactive "FWrite file: ")
590 (interactive
591 (list (if buffer-file-name
592 (read-file-name "Write file: "
593 nil nil nil nil)
594 (read-file-name "Write file: "
595 (cdr (assq 'default-directory
596 (buffer-local-variables)))
597 nil nil (buffer-name)))))
598 (or (null filename) (string-equal filename "")
599 (set-visited-file-name filename))
600 (set-buffer-modified-p t)
601 (save-buffer))
602\f
603(defun backup-buffer ()
604 "Make a backup of the disk file visited by the current buffer, if appropriate.
605This is normally done before saving the buffer the first time.
606If the value is non-nil, it is the result of `file-modes' on the original
607file; this means that the caller, after saving the buffer, should change
608the modes of the new file to agree with the old modes."
609 (if (and make-backup-files (not backup-inhibited)
610 (not buffer-backed-up)
611 (file-exists-p buffer-file-name)
612 (memq (aref (elt (file-attributes buffer-file-name) 8) 0)
613 '(?- ?l)))
614 (let ((real-file-name buffer-file-name)
615 backup-info backupname targets setmodes)
616 ;; If specified name is a symbolic link, chase it to the target.
617 ;; Thus we make the backups in the directory where the real file is.
618 (while (let ((tem (file-symlink-p real-file-name)))
619 (if tem
620 (setq real-file-name
621 (expand-file-name tem
622 (file-name-directory real-file-name))))
623 tem))
624 (setq backup-info (find-backup-file-name real-file-name)
625 backupname (car backup-info)
626 targets (cdr backup-info))
627;;; (if (file-directory-p buffer-file-name)
628;;; (error "Cannot save buffer in directory %s" buffer-file-name))
629 (condition-case ()
630 (let ((delete-old-versions
631 ;; If have old versions to maybe delete,
632 ;; ask the user to confirm now, before doing anything.
633 ;; But don't actually delete til later.
634 (and targets
635 (or trim-versions-without-asking
636 (y-or-n-p (format "Delete excess backup versions of %s? "
637 real-file-name))))))
638 ;; Actually write the back up file.
639 (condition-case ()
640 (if (or file-precious-flag
641; (file-symlink-p buffer-file-name)
642 backup-by-copying
643 (and backup-by-copying-when-linked
644 (> (file-nlinks real-file-name) 1))
645 (and backup-by-copying-when-mismatch
646 (let ((attr (file-attributes real-file-name)))
647 (or (nth 9 attr)
648 (/= (nth 2 attr) (user-uid))))))
649 (copy-file real-file-name backupname t t)
650; rename-file should delete old backup.
651; (condition-case ()
652; (delete-file backupname)
653; (file-error nil))
654 (rename-file real-file-name backupname t)
655 (setq setmodes (file-modes backupname)))
656 (file-error
657 ;; If trouble writing the backup, write it in ~.
658 (setq backupname (expand-file-name "~/%backup%~"))
659 (message "Cannot write backup file; backing up in ~/%%backup%%~")
660 (sleep-for 1)
661 (copy-file real-file-name backupname t t)))
662 (setq buffer-backed-up t)
663 ;; Now delete the old versions, if desired.
664 (if delete-old-versions
665 (while targets
666 (condition-case ()
667 (delete-file (car targets))
668 (file-error nil))
669 (setq targets (cdr targets))))
670 setmodes)
671 (file-error nil)))))
672
673(defun file-name-sans-versions (name)
674 "Return FILENAME sans backup versions or strings.
675This is a separate procedure so your site-init or startup file can
676redefine it."
677 (substring name 0
678 (if (eq system-type 'vax-vms)
679 ;; VMS version number is (a) semicolon, optional
680 ;; sign, zero or more digits or (b) period, option
681 ;; sign, zero or more digits, provided this is the
682 ;; second period encountered outside of the
683 ;; device/directory part of the file name.
684 (or (string-match ";[---+]?[0-9]*\\'" name)
685 (if (string-match "\\.[^]>:]*\\(\\.[---+]?[0-9]*\\)\\'"
686 name)
687 (match-beginning 1))
688 (length name))
689 (or (string-match "\\.~[0-9]+~\\'" name)
690 (string-match "~\\'" name)
691 (length name)))))
692
693(defun make-backup-file-name (file)
694 "Create the non-numeric backup file name for FILE.
695This is a separate function so you can redefine it for customization."
696 (concat file "~"))
697
698(defun backup-file-name-p (file)
699 "Return non-nil if FILE is a backup file name (numeric or not).
700This is a separate function so you can redefine it for customization.
701You may need to redefine `file-name-sans-versions' as well."
702 (string-match "~$" file))
703
704;; I believe there is no need to alter this behavior for VMS;
705;; since backup files are not made on VMS, it should not get called.
706(defun find-backup-file-name (fn)
707 "Find a file name for a backup file, and suggestions for deletions.
708Value is a list whose car is the name for the backup file
709 and whose cdr is a list of old versions to consider deleting now."
710 (if (eq version-control 'never)
711 (list (make-backup-file-name fn))
712 (let* ((base-versions (concat (file-name-nondirectory fn) ".~"))
713 (bv-length (length base-versions))
714 (possibilities (file-name-all-completions
715 base-versions
716 (file-name-directory fn)))
717 (versions (sort (mapcar 'backup-extract-version possibilities)
718 '<))
719 (high-water-mark (apply 'max (cons 0 versions)))
720 (deserve-versions-p
721 (or version-control
722 (> high-water-mark 0)))
723 (number-to-delete (- (length versions)
724 kept-old-versions kept-new-versions -1)))
725 (if (not deserve-versions-p)
726 (list (make-backup-file-name fn))
727 (cons (concat fn ".~" (int-to-string (1+ high-water-mark)) "~")
728 (if (> number-to-delete 0)
729 (mapcar (function (lambda (n)
730 (concat fn ".~" (int-to-string n) "~")))
731 (let ((v (nthcdr kept-old-versions versions)))
732 (rplacd (nthcdr (1- number-to-delete) v) ())
733 v))))))))
734
735(defun backup-extract-version (fn)
736 (if (and (string-match "[0-9]+~$" fn bv-length)
737 (= (match-beginning 0) bv-length))
738 (string-to-int (substring fn bv-length -1))
739 0))
740
741(defun file-nlinks (filename)
742 "Return number of names file FILENAME has."
743 (car (cdr (file-attributes filename))))
744\f
745(defun save-buffer (&optional args)
746 "Save current buffer in visited file if modified. Versions described below.
747By default, makes the previous version into a backup file
748 if previously requested or if this is the first save.
749With 1 or 3 \\[universal-argument]'s, marks this version
750 to become a backup when the next save is done.
751With 2 or 3 \\[universal-argument]'s,
752 unconditionally makes the previous version into a backup file.
753With argument of 0, never makes the previous version into a backup file.
754
755If a file's name is FOO, the names of its numbered backup versions are
756 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~.
757Numeric backups (rather than FOO~) will be made if value of
758 `version-control' is not the atom `never' and either there are already
759 numeric versions of the file being backed up, or `version-control' is
760 non-nil.
761We don't want excessive versions piling up, so there are variables
762 `kept-old-versions', which tells Emacs how many oldest versions to keep,
763 and `kept-new-versions', which tells how many newest versions to keep.
764 Defaults are 2 old versions and 2 new.
765`dired-kept-versions' controls dired's clean-directory (.) command.
766If `trim-versions-without-asking' is nil, system will query user
767 before trimming versions. Otherwise it does it silently."
768 (interactive "p")
769 (let ((modp (buffer-modified-p))
770 (large (> (buffer-size) 50000))
771 (make-backup-files (and make-backup-files (not (eq args 0)))))
772 (and modp (memq args '(16 64)) (setq buffer-backed-up nil))
773 (if (and modp large) (message "Saving file %s..." (buffer-file-name)))
774 (basic-save-buffer)
775 (and modp (memq args '(4 64)) (setq buffer-backed-up nil))))
776
777(defun delete-auto-save-file-if-necessary (&optional force)
778 "Delete auto-save file for current buffer if `delete-auto-save-files' is t.
779Normally delete only if the file was written by this Emacs since
780the last real save, but optional arg FORCE non-nil means delete anyway."
781 (and buffer-auto-save-file-name delete-auto-save-files
782 (not (string= buffer-file-name buffer-auto-save-file-name))
783 (or force (recent-auto-save-p))
784 (progn
785 (condition-case ()
786 (delete-file buffer-auto-save-file-name)
787 (file-error nil))
788 (set-buffer-auto-saved))))
789
790(defun basic-save-buffer ()
791 "Save the current buffer in its visited file, if it has been modified."
792 (interactive)
793 (if (buffer-modified-p)
794 (let ((recent-save (recent-auto-save-p))
795 setmodes tempsetmodes)
796 ;; On VMS, rename file and buffer to get rid of version number.
797 (if (and (eq system-type 'vax-vms)
798 (not (string= buffer-file-name
799 (file-name-sans-versions buffer-file-name))))
800 (let (buffer-new-name)
801 ;; Strip VMS version number before save.
802 (setq buffer-file-name
803 (file-name-sans-versions buffer-file-name))
804 ;; Construct a (unique) buffer name to correspond.
805 (let ((buf (create-file-buffer (downcase buffer-file-name))))
806 (setq buffer-new-name (buffer-name buf))
807 (kill-buffer buf))
808 (rename-buffer buffer-new-name)))
809 ;; If buffer has no file name, ask user for one.
810 (or buffer-file-name
811 (progn
812 (setq buffer-file-name
813 (expand-file-name (read-file-name "File to save in: ") nil)
814 default-directory (file-name-directory buffer-file-name))
815 (auto-save-mode auto-save-default)))
816 (or (verify-visited-file-modtime (current-buffer))
817 (not (file-exists-p buffer-file-name))
818 (yes-or-no-p
819 (format "%s has changed since visited or saved. Save anyway? "
820 (file-name-nondirectory buffer-file-name)))
821 (error "Save not confirmed"))
822 (save-restriction
823 (widen)
824 (and (> (point-max) 1)
825 (/= (char-after (1- (point-max))) ?\n)
826 (or (eq require-final-newline t)
827 (and require-final-newline
828 (y-or-n-p
829 (format "Buffer %s does not end in newline. Add one? "
830 (buffer-name)))))
831 (save-excursion
832 (goto-char (point-max))
833 (insert ?\n)))
834 (let ((hooks write-file-hooks)
835 (done nil))
836 (while (and hooks
837 (not (setq done (funcall (car hooks)))))
838 (setq hooks (cdr hooks)))
839 ;; If a hook returned t, file is already "written".
840 (cond ((not done)
841 (if (not (file-writable-p buffer-file-name))
842 (let ((dir (file-name-directory buffer-file-name)))
843 (if (not (file-directory-p dir))
844 (error "%s is not a directory" dir)
845 (if (not (file-exists-p buffer-file-name))
846 (error "Directory %s write-protected" dir)
847 (if (yes-or-no-p
848 (format "File %s is write-protected; try to save anyway? "
849 (file-name-nondirectory
850 buffer-file-name)))
851 (setq tempsetmodes t)
852 (error "Attempt to save to a file which you aren't allowed to write"))))))
853 (or buffer-backed-up
854 (setq setmodes (backup-buffer)))
855 (if file-precious-flag
856 ;; If file is precious, rename it away before
857 ;; overwriting it.
858 (let ((rename t)
859 realname tempname temp)
860 ;; Chase symlinks; rename the ultimate actual file.
861 (setq realname buffer-file-name)
862 (while (setq temp (file-symlink-p realname))
863 (setq realname temp))
864 (setq tempname (concat realname "#"))
865 (condition-case ()
866 (progn (rename-file realname tempname t)
867 (setq setmodes (file-modes tempname)))
868 (file-error (setq rename nil tempname nil)))
869 (if (file-directory-p realname)
870 (error "%s is a directory" realname))
871 (unwind-protect
872 (progn (clear-visited-file-modtime)
873 (write-region (point-min) (point-max)
874 realname nil t)
875 (setq rename nil))
876 ;; If rename is still t, writing failed.
877 ;; So rename the old file back to original name,
878 (if rename
879 (progn
880 (rename-file tempname realname t)
881 (clear-visited-file-modtime))
882 ;; Otherwise we don't need the original file,
883 ;; so flush it, if we still have it.
884 ;; If rename failed due to name length restriction
885 ;; then TEMPNAME is now nil.
886 (if tempname
887 (condition-case ()
888 (delete-file tempname)
889 (error nil))))))
890 ;; If file not writable, see if we can make it writable
891 ;; temporarily while we write it.
892 ;; But no need to do so if we have just backed it up
893 ;; (setmodes is set) because that says we're superseding.
894 (cond ((and tempsetmodes (not setmodes))
895 ;; Change the mode back, after writing.
896 (setq setmodes (file-modes buffer-file-name))
897 (set-file-modes buffer-file-name 511)))
898 (write-region (point-min) (point-max)
899 buffer-file-name nil t)))))
900 (if setmodes
901 (condition-case ()
902 (set-file-modes buffer-file-name setmodes)
903 (error nil))))
904 ;; If the auto-save file was recent before this command,
905 ;; delete it now.
906 (delete-auto-save-file-if-necessary recent-save)
907 (run-hooks 'after-save-hooks))
908 (message "(No changes need to be saved)")))
909
910
911(require 'map-ynp)
912
913(defun save-some-buffers (&optional arg exiting)
914 "Save some modified file-visiting buffers. Asks user about each one.
915With argument, saves all with no questions."
916 (interactive "P")
917 (if (zerop (map-y-or-n-p
918 (function
919 (lambda (buffer)
920 (and (buffer-modified-p buffer)
921 (or
922 (buffer-file-name buffer)
923 (and exiting
924 (save-excursion
925 (set-buffer buffer)
926 buffer-offer-save (> (buffer-size) 0))))
927 (if arg
928 t
929 (if (buffer-file-name buffer)
930 (format "Save file %s? "
931 (buffer-file-name buffer))
932 (format "Save buffer %s? "
933 (buffer-name buffer)))))))
934 (function
935 (lambda (buffer)
936 (save-excursion
937 (set-buffer buffer)
938 (save-buffer))))
939 (buffer-list)
940 '("buffer" "buffers" "save")))
941 (message "(No files need saving)")))
942\f
943(defun not-modified (&optional arg)
944 "Mark current buffer as unmodified, not needing to be saved.
945With prefix arg, mark buffer as modified, so \\[save-buffer] will save."
946 (interactive "P")
947 (message (if arg "Modification-flag set"
948 "Modification-flag cleared"))
949 (set-buffer-modified-p arg))
950
951(defun toggle-read-only (&optional arg)
952 "Change whether this buffer is visiting its file read-only.
953With arg, set read-only iff arg is positive."
954 (interactive "P")
955 (setq buffer-read-only
956 (if (null arg)
957 (not buffer-read-only)
958 (> (prefix-numeric-value arg) 0)))
959 ;; Force mode-line redisplay
960 (set-buffer-modified-p (buffer-modified-p)))
961
962(defun insert-file (filename)
963 "Insert contents of file FILENAME into buffer after point.
964Set mark after the inserted text.
965
966This function is meant for the user to run interactively.
967Don't call it from programs! Use `insert-file-contents' instead.
968\(Its calling sequence is different; see its documentation)."
969 (interactive "fInsert file: ")
970 (let ((tem (insert-file-contents filename)))
971 (push-mark (+ (point) (car (cdr tem))))))
972
973(defun append-to-file (start end filename)
974 "Append the contents of the region to the end of file FILENAME.
975When called from a function, expects three arguments,
976START, END and FILENAME. START and END are buffer positions
977saying what text to write."
978 (interactive "r\nFAppend to file: ")
979 (write-region start end filename t))
980
981(defun file-newest-backup (filename)
982 "Return most recent backup file for FILENAME or nil if no backups exist."
983 (let* ((filename (expand-file-name filename))
984 (file (file-name-nondirectory filename))
985 (dir (file-name-directory filename))
986 (comp (file-name-all-completions file dir))
987 newest)
988 (while comp
989 (setq file (concat dir (car comp))
990 comp (cdr comp))
991 (if (and (backup-file-name-p file)
992 (or (null newest) (file-newer-than-file-p file newest)))
993 (setq newest file)))
994 newest))
995
996(defun rename-uniquely ()
997 "Rename current buffer to a similar name not already taken.
998This function is useful for creating multiple shell process buffers
999or multiple mail buffers, etc."
1000 (interactive)
1001 (let* ((new-buf (generate-new-buffer (buffer-name)))
1002 (name (buffer-name new-buf)))
1003 (kill-buffer new-buf)
1004 (rename-buffer name)
1005 (set-buffer-modified-p (buffer-modified-p)))) ; force mode line update
1006\f
1007(put 'revert-buffer-function 'permanent-local t)
1008(defvar revert-buffer-function nil
1009 "Function to use to revert this buffer, or nil to do the default.")
1010
1011(put 'revert-buffer-insert-file-contents-function 'permanent-local t)
1012(defvar revert-buffer-insert-file-contents-function nil
1013 "Function to use to insert contents when reverting this buffer.
1014Gets two args, first the nominal file name to use,
1015and second, t if reading the auto-save file.")
1016
1017(defun revert-buffer (&optional arg noconfirm)
1018 "Replace the buffer text with the text of the visited file on disk.
1019This undoes all changes since the file was visited or saved.
1020If latest auto-save file is more recent than the visited file,
1021asks user whether to use that instead.
1022
1023Optional first argument ARG means don't offer to use auto-save file.
1024This is the prefix arg when called interactively.
1025Optional second argument NOCONFIRM means don't ask for confirmation at all.
1026
1027If `revert-buffer-function' value is non-nil, it is called to do the work."
1028 (interactive "P")
1029 (if revert-buffer-function
1030 (funcall revert-buffer-function arg noconfirm)
1031 (let* ((opoint (point))
1032 (auto-save-p (and (null arg) (recent-auto-save-p)
1033 buffer-auto-save-file-name
1034 (file-readable-p buffer-auto-save-file-name)
1035 (y-or-n-p
1036 "Buffer has been auto-saved recently. Revert from auto-save file? ")))
1037 (file-name (if auto-save-p
1038 buffer-auto-save-file-name
1039 buffer-file-name)))
1040 (cond ((null file-name)
1041 (error "Buffer does not seem to be associated with any file"))
1042 ((or noconfirm
1043 (yes-or-no-p (format "Revert buffer from file %s? "
1044 file-name)))
1045 ;; If file was backed up but has changed since,
1046 ;; we shd make another backup.
1047 (and (not auto-save-p)
1048 (not (verify-visited-file-modtime))
1049 (setq buffer-backed-up nil))
1050 ;; Get rid of all undo records for this buffer.
1051 (or (eq buffer-undo-list t)
1052 (setq buffer-undo-list nil))
1053 (let ((buffer-read-only nil)
1054 ;; Don't make undo records for the reversion.
1055 (buffer-undo-list t))
1056 (if revert-buffer-insert-file-contents-function
1057 (funcall revert-buffer-insert-file-contents-function
1058 file-name auto-save-p)
1059 (if (not (file-exists-p file-name))
1060 (error "File %s no longer exists!" file-name))
1061 ;; Bind buffer-file-name to nil
1062 ;; so that we don't try to lock the file.
1063 (let ((buffer-file-name nil))
1064 (or auto-save-p
1065 (unlock-buffer))
1066 (erase-buffer))
1067 (insert-file-contents file-name (not auto-save-p))))
1068 (goto-char (min opoint (point-max)))
1069 (after-find-file nil)
1070 t)))))
1071
1072(defun recover-file (file)
1073 "Visit file FILE, but get contents from its last auto-save file."
1074 (interactive
1075 (let ((prompt-file buffer-file-name)
1076 (file-name nil)
1077 (file-dir nil))
1078 (and prompt-file
1079 (setq file-name (file-name-nondirectory prompt-file)
1080 file-dir (file-name-directory prompt-file)))
1081 (list (read-file-name "Recover file: "
1082 file-dir nil nil file-name))))
1083 (setq file (expand-file-name file))
1084 (if (auto-save-file-name-p file) (error "%s is an auto-save file" file))
1085 (let ((file-name (let ((buffer-file-name file))
1086 (make-auto-save-file-name))))
1087 (cond ((not (file-newer-than-file-p file-name file))
1088 (error "Auto-save file %s not current" file-name))
1089 ((save-window-excursion
1090 (if (not (eq system-type 'vax-vms))
1091 (with-output-to-temp-buffer "*Directory*"
1092 (buffer-disable-undo standard-output)
1093 (call-process "ls" nil standard-output nil
1094 "-l" file file-name)))
1095 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
1096 (switch-to-buffer (find-file-noselect file t))
1097 (let ((buffer-read-only nil))
1098 (erase-buffer)
1099 (insert-file-contents file-name nil))
1100 (after-find-file nil))
1101 (t (error "Recover-file cancelled.")))))
1102
1103(defun kill-some-buffers ()
1104 "For each buffer, ask whether to kill it."
1105 (interactive)
1106 (let ((list (buffer-list)))
1107 (while list
1108 (let* ((buffer (car list))
1109 (name (buffer-name buffer)))
1110 (and (not (string-equal name ""))
1111 (/= (aref name 0) ? )
1112 (yes-or-no-p
1113 (format "Buffer %s %s. Kill? "
1114 name
1115 (if (buffer-modified-p buffer)
1116 "HAS BEEN EDITED" "is unmodified")))
1117 (kill-buffer buffer)))
1118 (setq list (cdr list)))))
1119\f
1120(defun auto-save-mode (arg)
1121 "Toggle auto-saving of contents of current buffer.
1122With ARG, turn auto-saving on if positive, else off."
1123 (interactive "P")
1124 (setq buffer-auto-save-file-name
1125 (and (if (null arg)
1126 (not buffer-auto-save-file-name)
1127 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))
1128 (if (and buffer-file-name auto-save-visited-file-name
1129 (not buffer-read-only))
1130 buffer-file-name
1131 (make-auto-save-file-name))))
1132 (if (interactive-p)
1133 (message "Auto-save %s (in this buffer)"
1134 (if buffer-auto-save-file-name "on" "off")))
1135 buffer-auto-save-file-name)
1136
1137(defun rename-auto-save-file ()
1138 "Adjust current buffer's auto save file name for current conditions.
1139Also rename any existing auto save file, if it was made in this session."
1140 (let ((osave buffer-auto-save-file-name))
1141 (setq buffer-auto-save-file-name
1142 (make-auto-save-file-name))
1143 (if (and osave buffer-auto-save-file-name
1144 (not (string= buffer-auto-save-file-name buffer-file-name))
1145 (not (string= buffer-auto-save-file-name osave))
1146 (file-exists-p osave)
1147 (recent-auto-save-p))
1148 (rename-file osave buffer-auto-save-file-name t))))
1149
1150(defun make-auto-save-file-name ()
1151 "Return file name to use for auto-saves of current buffer.
1152Does not consider `auto-save-visited-file-name' as that variable is checked
1153before calling this function. You can redefine this for customization.
1154See also `auto-save-file-name-p'."
1155 (if buffer-file-name
1156 (concat (file-name-directory buffer-file-name)
1157 "#"
1158 (file-name-nondirectory buffer-file-name)
1159 "#")
1160 ;; For non-file bfr, use bfr name and Emacs pid.
1161 (expand-file-name (format "#%s#%s#" (buffer-name) (make-temp-name "")))))
1162
1163(defun auto-save-file-name-p (filename)
1164 "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'.
1165FILENAME should lack slashes. You can redefine this for customization."
1166 (string-match "^#.*#$" filename))
1167\f
1168(defconst list-directory-brief-switches
1169 (if (eq system-type 'vax-vms) "" "-CF")
1170 "*Switches for list-directory to pass to `ls' for brief listing,")
1171
1172(defconst list-directory-verbose-switches
1173 (if (eq system-type 'vax-vms)
1174 "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)"
1175 "-l")
1176 "*Switches for list-directory to pass to `ls' for verbose listing,")
1177
1178(defun list-directory (dirname &optional verbose)
1179 "Display a list of files in or matching DIRNAME, a la `ls'.
1180DIRNAME is globbed by the shell if necessary.
1181Prefix arg (second arg if noninteractive) means supply -l switch to `ls'.
1182Actions controlled by variables `list-directory-brief-switches'
1183and `list-directory-verbose-switches'."
1184 (interactive (let ((pfx current-prefix-arg))
1185 (list (read-file-name (if pfx "List directory (verbose): "
1186 "List directory (brief): ")
1187 nil default-directory nil)
1188 pfx)))
1189 (let ((switches (if verbose list-directory-verbose-switches
1190 list-directory-brief-switches)))
1191 (or dirname (setq dirname default-directory))
1192 (setq dirname (expand-file-name dirname))
1193 (with-output-to-temp-buffer "*Directory*"
1194 (buffer-disable-undo standard-output)
1195 (princ "Directory ")
1196 (princ dirname)
1197 (terpri)
1198 (if (eq system-type 'vax-vms)
1199 (vms-read-directory dirname switches standard-output)
1200 (if (file-directory-p dirname)
1201 (save-excursion
1202 (set-buffer "*Directory*")
1203 (call-process "ls" nil standard-output nil switches
1204 (setq default-directory
1205 (file-name-as-directory dirname))))
1206 (let ((default-directory (file-name-directory dirname)))
1207 (if (file-exists-p default-directory)
1208 (call-process shell-file-name nil standard-output nil
1209 "-c" (concat "exec ls "
1210 switches " "
1211 (file-name-nondirectory dirname)))
1212 (princ "No such directory: ")
1213 (princ dirname)
1214 (terpri))))))))
1215
1216(defun save-buffers-kill-emacs (&optional arg)
1217 "Offer to save each buffer, then kill this Emacs process.
1218With prefix arg, silently save all file-visiting buffers, then kill."
1219 (interactive "P")
1220 (save-some-buffers arg t)
1221 (and (or (not (memq t (mapcar (function
1222 (lambda (buf) (and (buffer-file-name buf)
1223 (buffer-modified-p buf))))
1224 (buffer-list))))
1225 (yes-or-no-p "Modified buffers exist; exit anyway? "))
1226 (or (not (fboundp 'process-list))
1227 ;; process-list is not defined on VMS.
1228 (let ((processes (process-list))
1229 active)
1230 (while processes
1231 (and (memq (process-status (car processes)) '(run stop))
1232 (let ((val (process-kill-without-query (car processes))))
1233 (process-kill-without-query (car processes) val)
1234 val)
1235 (setq active t))
1236 (setq processes (cdr processes)))
1237 (or (not active)
1238 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
1239 (kill-emacs)))
1240\f
1241(define-key ctl-x-map "\C-f" 'find-file)
1242(define-key ctl-x-map "\C-q" 'toggle-read-only)
1243(define-key ctl-x-map "\C-r" 'find-file-read-only)
1244(define-key ctl-x-map "\C-v" 'find-alternate-file)
1245(define-key ctl-x-map "\C-s" 'save-buffer)
1246(define-key ctl-x-map "s" 'save-some-buffers)
1247(define-key ctl-x-map "\C-w" 'write-file)
1248(define-key ctl-x-map "i" 'insert-file)
1249(define-key esc-map "~" 'not-modified)
1250(define-key ctl-x-map "\C-d" 'list-directory)
1251(define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs)
1252
1253(define-key ctl-x-4-map "f" 'find-file-other-window)
1254(define-key ctl-x-4-map "r" 'find-file-read-only-other-window)
1255(define-key ctl-x-4-map "\C-f" 'find-file-other-window)
1256(define-key ctl-x-4-map "b" 'switch-to-buffer-other-window)