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