300 |
A value of `visit' means do this right after the file is visited. |
A value of `visit' means do this right after the file is visited. |
301 |
A value of `visit-save' means do it at both of those times. |
A value of `visit-save' means do it at both of those times. |
302 |
Any other non-nil value means ask user whether to add a newline, when saving. |
Any other non-nil value means ask user whether to add a newline, when saving. |
303 |
nil means don't add newlines. |
nil means do not add newlines when saving. |
304 |
|
|
305 |
You will have to be careful if you set this to nil: you will have |
If you set this to nil, you must careful to manually add a final newline |
306 |
to remember to manually add a final newline whenever you finish a |
whenever you save a file that really needs one." |
|
file that really needs one." |
|
307 |
:type '(choice (const :tag "When visiting" visit) |
:type '(choice (const :tag "When visiting" visit) |
308 |
(const :tag "When saving" t) |
(const :tag "When saving" t) |
309 |
(const :tag "When visiting or saving" visit-save) |
(const :tag "When visiting or saving" visit-save) |
310 |
(const :tag "Never" nil) |
(other :tag "Ask" ask) |
311 |
(other :tag "Ask" ask)) |
(const :tag "Don't add newlines" nil)) |
312 |
:group 'editing-basics |
:group 'editing-basics |
313 |
:version "22.1") |
:version "22.1") |
314 |
|
|
528 |
Value is not expanded---you must call `expand-file-name' yourself. |
Value is not expanded---you must call `expand-file-name' yourself. |
529 |
Default name to DEFAULT-DIRNAME if user exits with the same |
Default name to DEFAULT-DIRNAME if user exits with the same |
530 |
non-empty string that was inserted by this function. |
non-empty string that was inserted by this function. |
531 |
(If DEFAULT-DIRNAME is omitted, the current buffer's directory is used, |
(If DEFAULT-DIRNAME is omitted, DIR combined with INITIAL is used, |
532 |
except that if INITIAL is specified, that combined with DIR is used.) |
or just DIR if INITIAL is nil.) |
533 |
If the user exits with an empty minibuffer, this function returns |
If the user exits with an empty minibuffer, this function returns |
534 |
an empty string. (This can only happen if the user erased the |
an empty string. (This can only happen if the user erased the |
535 |
pre-inserted contents or if `insert-default-directory' is nil.) |
pre-inserted contents or if `insert-default-directory' is nil.) |
543 |
(unless default-dirname |
(unless default-dirname |
544 |
(setq default-dirname |
(setq default-dirname |
545 |
(if initial (concat dir initial) default-directory))) |
(if initial (concat dir initial) default-directory))) |
546 |
(read-file-name prompt dir default-dirname mustmatch initial |
(read-file-name prompt dir (or default-dirname |
547 |
|
(if initial (expand-file-name initial dir) |
548 |
|
dir)) |
549 |
|
mustmatch initial |
550 |
'file-directory-p)) |
'file-directory-p)) |
551 |
|
|
552 |
|
|
942 |
Recursive uses of the minibuffer will not be affected." |
Recursive uses of the minibuffer will not be affected." |
943 |
(declare (indent 1) (debug t)) |
(declare (indent 1) (debug t)) |
944 |
(let ((hook (make-symbol "setup-hook"))) |
(let ((hook (make-symbol "setup-hook"))) |
945 |
`(let ((,hook |
`(let (,hook) |
946 |
(lambda () |
(setq ,hook |
947 |
;; Clear out this hook so it does not interfere |
(lambda () |
948 |
;; with any recursive minibuffer usage. |
;; Clear out this hook so it does not interfere |
949 |
(remove-hook 'minibuffer-setup-hook ,hook) |
;; with any recursive minibuffer usage. |
950 |
(,fun)))) |
(remove-hook 'minibuffer-setup-hook ,hook) |
951 |
|
(,fun))) |
952 |
(unwind-protect |
(unwind-protect |
953 |
(progn |
(progn |
954 |
(add-hook 'minibuffer-setup-hook ,hook) |
(add-hook 'minibuffer-setup-hook ,hook) |
1976 |
("%![^V]" . ps-mode) |
("%![^V]" . ps-mode) |
1977 |
("# xmcd " . conf-unix-mode)) |
("# xmcd " . conf-unix-mode)) |
1978 |
"Alist of buffer beginnings vs. corresponding major mode functions. |
"Alist of buffer beginnings vs. corresponding major mode functions. |
1979 |
Each element looks like (REGEXP . FUNCTION). FUNCTION will be |
Each element looks like (REGEXP . FUNCTION). After visiting a file, |
1980 |
called, unless it is nil (to allow `auto-mode-alist' to override).") |
if REGEXP matches the text at the beginning of the buffer, |
1981 |
|
`normal-mode' will call FUNCTION rather than allowing `auto-mode-alist' |
1982 |
|
to decide the buffer's major mode. |
1983 |
|
|
1984 |
|
If FUNCTION is nil, then it is not called. (That is a way of saying |
1985 |
|
\"allow `auto-mode-alist' to decide for these files.") |
1986 |
|
|
1987 |
(defun set-auto-mode (&optional keep-mode-if-same) |
(defun set-auto-mode (&optional keep-mode-if-same) |
1988 |
"Select major mode appropriate for current buffer. |
"Select major mode appropriate for current buffer. |
2748 |
(file-error nil)))))) |
(file-error nil)))))) |
2749 |
|
|
2750 |
(defun backup-buffer-copy (from-name to-name modes) |
(defun backup-buffer-copy (from-name to-name modes) |
2751 |
(condition-case () |
(let ((umask (default-file-modes))) |
2752 |
(copy-file from-name to-name t t) |
(unwind-protect |
2753 |
(file-error |
(progn |
2754 |
;; If copying fails because file TO-NAME |
;; Create temp files with strict access rights. It's easy to |
2755 |
;; is not writable, delete that file and try again. |
;; loosen them later, whereas it's impossible to close the |
2756 |
(if (and (file-exists-p to-name) |
;; time-window of loose permissions otherwise. |
2757 |
(not (file-writable-p to-name))) |
(set-default-file-modes ?\700) |
2758 |
(delete-file to-name)) |
(while (condition-case () |
2759 |
(copy-file from-name to-name t t))) |
(progn |
2760 |
|
(condition-case nil |
2761 |
|
(delete-file to-name) |
2762 |
|
(file-error nil)) |
2763 |
|
(write-region "" nil to-name nil 'silent nil 'excl) |
2764 |
|
nil) |
2765 |
|
(file-already-exists t)) |
2766 |
|
;; the file was somehow created by someone else between |
2767 |
|
;; `make-temp-name' and `write-region', let's try again. |
2768 |
|
nil) |
2769 |
|
(copy-file from-name to-name t t 'excl)) |
2770 |
|
;; Reset the umask. |
2771 |
|
(set-default-file-modes umask))) |
2772 |
(and modes |
(and modes |
2773 |
(set-file-modes to-name (logand modes #o1777)))) |
(set-file-modes to-name (logand modes #o1777)))) |
2774 |
|
|
3351 |
;; This requires write access to the containing dir, |
;; This requires write access to the containing dir, |
3352 |
;; which is why we don't try it if we don't have that access. |
;; which is why we don't try it if we don't have that access. |
3353 |
(let ((realname buffer-file-name) |
(let ((realname buffer-file-name) |
3354 |
tempname nogood i succeed |
tempname succeed |
3355 |
|
(umask (default-file-modes)) |
3356 |
(old-modtime (visited-file-modtime))) |
(old-modtime (visited-file-modtime))) |
3357 |
(setq i 0) |
;; Create temp files with strict access rights. It's easy to |
3358 |
(setq nogood t) |
;; loosen them later, whereas it's impossible to close the |
3359 |
;; Find the temporary name to write under. |
;; time-window of loose permissions otherwise. |
|
(while nogood |
|
|
(setq tempname (format |
|
|
(if (and (eq system-type 'ms-dos) |
|
|
(not (msdos-long-file-names))) |
|
|
"%s#%d.tm#" ; MSDOS limits files to 8+3 |
|
|
(if (memq system-type '(vax-vms axp-vms)) |
|
|
"%s$tmp$%d" |
|
|
"%s#tmp#%d")) |
|
|
dir i)) |
|
|
(setq nogood (file-exists-p tempname)) |
|
|
(setq i (1+ i))) |
|
3360 |
(unwind-protect |
(unwind-protect |
3361 |
(progn (clear-visited-file-modtime) |
(progn |
3362 |
(write-region (point-min) (point-max) |
(clear-visited-file-modtime) |
3363 |
tempname nil realname |
(set-default-file-modes ?\700) |
3364 |
buffer-file-truename) |
;; Try various temporary names. |
3365 |
(setq succeed t)) |
;; This code follows the example of make-temp-file, |
3366 |
;; If writing the temp file fails, |
;; but it calls write-region in the appropriate way |
3367 |
;; delete the temp file. |
;; for saving the buffer. |
3368 |
(or succeed |
(while (condition-case () |
3369 |
(progn |
(progn |
3370 |
(condition-case nil |
(setq tempname |
3371 |
(delete-file tempname) |
(make-temp-name |
3372 |
(file-error nil)) |
(expand-file-name "tmp" dir))) |
3373 |
(set-visited-file-modtime old-modtime)))) |
(write-region (point-min) (point-max) |
3374 |
;; Since we have created an entirely new file |
tempname nil realname |
3375 |
;; and renamed it, make sure it gets the |
buffer-file-truename 'excl) |
3376 |
;; right permission bits set. |
nil) |
3377 |
|
(file-already-exists t)) |
3378 |
|
;; The file was somehow created by someone else between |
3379 |
|
;; `make-temp-name' and `write-region', let's try again. |
3380 |
|
nil) |
3381 |
|
(setq succeed t)) |
3382 |
|
;; Reset the umask. |
3383 |
|
(set-default-file-modes umask) |
3384 |
|
;; If we failed, restore the buffer's modtime. |
3385 |
|
(unless succeed |
3386 |
|
(set-visited-file-modtime old-modtime))) |
3387 |
|
;; Since we have created an entirely new file, |
3388 |
|
;; make sure it gets the right permission bits set. |
3389 |
(setq setmodes (or setmodes (cons (file-modes buffer-file-name) |
(setq setmodes (or setmodes (cons (file-modes buffer-file-name) |
3390 |
buffer-file-name))) |
buffer-file-name))) |
3391 |
;; We succeeded in writing the temp file, |
;; We succeeded in writing the temp file, |
3671 |
(defvar buffer-stale-function nil |
(defvar buffer-stale-function nil |
3672 |
"Function to check whether a non-file buffer needs reverting. |
"Function to check whether a non-file buffer needs reverting. |
3673 |
This should be a function with one optional argument NOCONFIRM. |
This should be a function with one optional argument NOCONFIRM. |
3674 |
Auto Revert Mode sets NOCONFIRM to t. The function should return |
Auto Revert Mode passes t for NOCONFIRM. The function should return |
3675 |
non-nil if the buffer should be reverted. A return value of |
non-nil if the buffer should be reverted. A return value of |
3676 |
`fast' means that the need for reverting was not checked, but |
`fast' means that the need for reverting was not checked, but |
3677 |
that reverting the buffer is fast. The buffer is current when |
that reverting the buffer is fast. The buffer is current when |
3740 |
(interactive (list (not current-prefix-arg))) |
(interactive (list (not current-prefix-arg))) |
3741 |
(if revert-buffer-function |
(if revert-buffer-function |
3742 |
(funcall revert-buffer-function ignore-auto noconfirm) |
(funcall revert-buffer-function ignore-auto noconfirm) |
3743 |
(let* ((auto-save-p (and (not ignore-auto) |
(with-current-buffer (or (buffer-base-buffer (current-buffer)) |
3744 |
(recent-auto-save-p) |
(current-buffer)) |
3745 |
buffer-auto-save-file-name |
(let* ((auto-save-p (and (not ignore-auto) |
3746 |
(file-readable-p buffer-auto-save-file-name) |
(recent-auto-save-p) |
3747 |
(y-or-n-p |
buffer-auto-save-file-name |
3748 |
"Buffer has been auto-saved recently. Revert from auto-save file? "))) |
(file-readable-p buffer-auto-save-file-name) |
3749 |
(file-name (if auto-save-p |
(y-or-n-p |
3750 |
buffer-auto-save-file-name |
"Buffer has been auto-saved recently. Revert from auto-save file? "))) |
3751 |
buffer-file-name))) |
(file-name (if auto-save-p |
3752 |
(cond ((null file-name) |
buffer-auto-save-file-name |
3753 |
(error "Buffer does not seem to be associated with any file")) |
buffer-file-name))) |
3754 |
((or noconfirm |
(cond ((null file-name) |
3755 |
(and (not (buffer-modified-p)) |
(error "Buffer does not seem to be associated with any file")) |
3756 |
(let ((tail revert-without-query) |
((or noconfirm |
3757 |
(found nil)) |
(and (not (buffer-modified-p)) |
3758 |
(while tail |
(let ((tail revert-without-query) |
3759 |
(if (string-match (car tail) file-name) |
(found nil)) |
3760 |
(setq found t)) |
(while tail |
3761 |
(setq tail (cdr tail))) |
(if (string-match (car tail) file-name) |
3762 |
found)) |
(setq found t)) |
3763 |
(yes-or-no-p (format "Revert buffer from file %s? " |
(setq tail (cdr tail))) |
3764 |
file-name))) |
found)) |
3765 |
(run-hooks 'before-revert-hook) |
(yes-or-no-p (format "Revert buffer from file %s? " |
3766 |
;; If file was backed up but has changed since, |
file-name))) |
3767 |
;; we shd make another backup. |
(run-hooks 'before-revert-hook) |
3768 |
(and (not auto-save-p) |
;; If file was backed up but has changed since, |
3769 |
(not (verify-visited-file-modtime (current-buffer))) |
;; we shd make another backup. |
3770 |
(setq buffer-backed-up nil)) |
(and (not auto-save-p) |
3771 |
;; Get rid of all undo records for this buffer. |
(not (verify-visited-file-modtime (current-buffer))) |
3772 |
(or (eq buffer-undo-list t) |
(setq buffer-backed-up nil)) |
3773 |
(setq buffer-undo-list nil)) |
;; Get rid of all undo records for this buffer. |
3774 |
;; Effectively copy the after-revert-hook status, |
(or (eq buffer-undo-list t) |
3775 |
;; since after-find-file will clobber it. |
(setq buffer-undo-list nil)) |
3776 |
(let ((global-hook (default-value 'after-revert-hook)) |
;; Effectively copy the after-revert-hook status, |
3777 |
(local-hook-p (local-variable-p 'after-revert-hook)) |
;; since after-find-file will clobber it. |
3778 |
(local-hook (and (local-variable-p 'after-revert-hook) |
(let ((global-hook (default-value 'after-revert-hook)) |
3779 |
after-revert-hook))) |
(local-hook-p (local-variable-p 'after-revert-hook)) |
3780 |
(let (buffer-read-only |
(local-hook (and (local-variable-p 'after-revert-hook) |
3781 |
;; Don't make undo records for the reversion. |
after-revert-hook))) |
3782 |
(buffer-undo-list t)) |
(let (buffer-read-only |
3783 |
(if revert-buffer-insert-file-contents-function |
;; Don't make undo records for the reversion. |
3784 |
(funcall revert-buffer-insert-file-contents-function |
(buffer-undo-list t)) |
3785 |
file-name auto-save-p) |
(if revert-buffer-insert-file-contents-function |
3786 |
(if (not (file-exists-p file-name)) |
(funcall revert-buffer-insert-file-contents-function |
3787 |
(error (if buffer-file-number |
file-name auto-save-p) |
3788 |
"File %s no longer exists!" |
(if (not (file-exists-p file-name)) |
3789 |
"Cannot revert nonexistent file %s") |
(error (if buffer-file-number |
3790 |
file-name)) |
"File %s no longer exists!" |
3791 |
;; Bind buffer-file-name to nil |
"Cannot revert nonexistent file %s") |
3792 |
;; so that we don't try to lock the file. |
file-name)) |
3793 |
(let ((buffer-file-name nil)) |
;; Bind buffer-file-name to nil |
3794 |
(or auto-save-p |
;; so that we don't try to lock the file. |
3795 |
(unlock-buffer))) |
(let ((buffer-file-name nil)) |
3796 |
(widen) |
(or auto-save-p |
3797 |
(let ((coding-system-for-read |
(unlock-buffer))) |
3798 |
;; Auto-saved file shoule be read by Emacs' |
(widen) |
3799 |
;; internal coding. |
(let ((coding-system-for-read |
3800 |
(if auto-save-p 'auto-save-coding |
;; Auto-saved file shoule be read by Emacs' |
3801 |
(or coding-system-for-read |
;; internal coding. |
3802 |
buffer-file-coding-system-explicit)))) |
(if auto-save-p 'auto-save-coding |
3803 |
;; This force after-insert-file-set-coding |
(or coding-system-for-read |
3804 |
;; (called from insert-file-contents) to set |
buffer-file-coding-system-explicit)))) |
3805 |
;; buffer-file-coding-system to a proper value. |
;; This force after-insert-file-set-coding |
3806 |
(kill-local-variable 'buffer-file-coding-system) |
;; (called from insert-file-contents) to set |
3807 |
|
;; buffer-file-coding-system to a proper value. |
3808 |
;; Note that this preserves point in an intelligent way. |
(kill-local-variable 'buffer-file-coding-system) |
3809 |
(if preserve-modes |
|
3810 |
(let ((buffer-file-format buffer-file-format)) |
;; Note that this preserves point in an intelligent way. |
3811 |
(insert-file-contents file-name (not auto-save-p) |
(if preserve-modes |
3812 |
nil nil t)) |
(let ((buffer-file-format buffer-file-format)) |
3813 |
(insert-file-contents file-name (not auto-save-p) |
(insert-file-contents file-name (not auto-save-p) |
3814 |
nil nil t))))) |
nil nil t)) |
3815 |
;; Recompute the truename in case changes in symlinks |
(insert-file-contents file-name (not auto-save-p) |
3816 |
;; have changed the truename. |
nil nil t))))) |
3817 |
(setq buffer-file-truename |
;; Recompute the truename in case changes in symlinks |
3818 |
(abbreviate-file-name (file-truename buffer-file-name))) |
;; have changed the truename. |
3819 |
(after-find-file nil nil t t preserve-modes) |
(setq buffer-file-truename |
3820 |
;; Run after-revert-hook as it was before we reverted. |
(abbreviate-file-name (file-truename buffer-file-name))) |
3821 |
(setq-default revert-buffer-internal-hook global-hook) |
(after-find-file nil nil t t preserve-modes) |
3822 |
(if local-hook-p |
;; Run after-revert-hook as it was before we reverted. |
3823 |
(set (make-local-variable 'revert-buffer-internal-hook) |
(setq-default revert-buffer-internal-hook global-hook) |
3824 |
local-hook) |
(if local-hook-p |
3825 |
(kill-local-variable 'revert-buffer-internal-hook)) |
(set (make-local-variable 'revert-buffer-internal-hook) |
3826 |
(run-hooks 'revert-buffer-internal-hook)) |
local-hook) |
3827 |
t))))) |
(kill-local-variable 'revert-buffer-internal-hook)) |
3828 |
|
(run-hooks 'revert-buffer-internal-hook)) |
3829 |
|
t)))))) |
3830 |
|
|
3831 |
(defun recover-this-file () |
(defun recover-this-file () |
3832 |
"Recover the visited file--get contents from its last auto-save file." |
"Recover the visited file--get contents from its last auto-save file." |