comparison lisp/files.el @ 68799:739ab5567adc

(revert-buffer, recover-file): Replace buffer-read-only with inhibit-read-only. Suggested by Stefan Monnier. (revert-buffer): Let insert-file-contents discard buffer-undo-list. Simplify code. (find-file, find-file-existing, revert-buffer): Doc-string fixes.
author Eli Zaretskii <eliz@gnu.org>
date Sat, 11 Feb 2006 18:42:23 +0000
parents 3bd95f4f2941
children 9e3243a12c5f b98066f4aa10
comparison
equal deleted inserted replaced
68798:8327f0369e4b 68799:739ab5567adc
1023 but the visited file name is available through the minibuffer history: 1023 but the visited file name is available through the minibuffer history:
1024 type M-n to pull it into the minibuffer. 1024 type M-n to pull it into the minibuffer.
1025 1025
1026 Interactively, or if WILDCARDS is non-nil in a call from Lisp, 1026 Interactively, or if WILDCARDS is non-nil in a call from Lisp,
1027 expand wildcards (if any) and visit multiple files. You can 1027 expand wildcards (if any) and visit multiple files. You can
1028 suppress wildcard expansion by setting `find-file-wildcards'. 1028 suppress wildcard expansion by setting `find-file-wildcards' to nil.
1029 1029
1030 To visit a file without any kind of conversion and without 1030 To visit a file without any kind of conversion and without
1031 automatically choosing a major mode, use \\[find-file-literally]." 1031 automatically choosing a major mode, use \\[find-file-literally]."
1032 (interactive (find-file-read-args "Find file: " nil)) 1032 (interactive (find-file-read-args "Find file: " nil))
1033 (let ((value (find-file-noselect filename nil nil wildcards))) 1033 (let ((value (find-file-noselect filename nil nil wildcards)))
1075 (mapcar 'switch-to-buffer (cdr value)))) 1075 (mapcar 'switch-to-buffer (cdr value))))
1076 (switch-to-buffer-other-frame value)))) 1076 (switch-to-buffer-other-frame value))))
1077 1077
1078 (defun find-file-existing (filename &optional wildcards) 1078 (defun find-file-existing (filename &optional wildcards)
1079 "Edit the existing file FILENAME. 1079 "Edit the existing file FILENAME.
1080 Like \\[find-file] but only allow files that exists." 1080 Like \\[find-file] but only allow a file that exists."
1081 (interactive (find-file-read-args "Find existing file: " t)) 1081 (interactive (find-file-read-args "Find existing file: " t))
1082 (unless (file-exists-p filename) (error "%s does not exist" filename)) 1082 (unless (file-exists-p filename) (error "%s does not exist" filename))
1083 (find-file filename wildcards) 1083 (find-file filename wildcards)
1084 (current-buffer)) 1084 (current-buffer))
1085 1085
3793 sense of this argument is the reverse of the prefix argument, for the 3793 sense of this argument is the reverse of the prefix argument, for the
3794 sake of backward compatibility. IGNORE-AUTO is optional, defaulting 3794 sake of backward compatibility. IGNORE-AUTO is optional, defaulting
3795 to nil. 3795 to nil.
3796 3796
3797 Optional second argument NOCONFIRM means don't ask for confirmation at 3797 Optional second argument NOCONFIRM means don't ask for confirmation at
3798 all. (The local variable `revert-without-query', if non-nil, prevents 3798 all. \(The variable `revert-without-query' offers another way to
3799 confirmation.) 3799 revert buffers without querying for confirmation.)
3800 3800
3801 Optional third argument PRESERVE-MODES non-nil means don't alter 3801 Optional third argument PRESERVE-MODES non-nil means don't alter
3802 the files modes. Normally we reinitialize them using `normal-mode'. 3802 the files modes. Normally we reinitialize them using `normal-mode'.
3803 3803
3804 If the value of `revert-buffer-function' is non-nil, it is called to 3804 If the value of `revert-buffer-function' is non-nil, it is called to
3829 buffer-file-name))) 3829 buffer-file-name)))
3830 (cond ((null file-name) 3830 (cond ((null file-name)
3831 (error "Buffer does not seem to be associated with any file")) 3831 (error "Buffer does not seem to be associated with any file"))
3832 ((or noconfirm 3832 ((or noconfirm
3833 (and (not (buffer-modified-p)) 3833 (and (not (buffer-modified-p))
3834 (let ((tail revert-without-query) 3834 (catch 'found
3835 (found nil)) 3835 (dolist (regexp revert-without-query)
3836 (while tail 3836 (when (string-match regexp file-name)
3837 (if (string-match (car tail) file-name) 3837 (throw 'found t)))))
3838 (setq found t))
3839 (setq tail (cdr tail)))
3840 found))
3841 (yes-or-no-p (format "Revert buffer from file %s? " 3838 (yes-or-no-p (format "Revert buffer from file %s? "
3842 file-name))) 3839 file-name)))
3843 (run-hooks 'before-revert-hook) 3840 (run-hooks 'before-revert-hook)
3844 ;; If file was backed up but has changed since, 3841 ;; If file was backed up but has changed since,
3845 ;; we shd make another backup. 3842 ;; we shd make another backup.
3846 (and (not auto-save-p) 3843 (and (not auto-save-p)
3847 (not (verify-visited-file-modtime (current-buffer))) 3844 (not (verify-visited-file-modtime (current-buffer)))
3848 (setq buffer-backed-up nil)) 3845 (setq buffer-backed-up nil))
3849 ;; Get rid of all undo records for this buffer.
3850 (or (eq buffer-undo-list t)
3851 (setq buffer-undo-list nil))
3852 ;; Effectively copy the after-revert-hook status, 3846 ;; Effectively copy the after-revert-hook status,
3853 ;; since after-find-file will clobber it. 3847 ;; since after-find-file will clobber it.
3854 (let ((global-hook (default-value 'after-revert-hook)) 3848 (let ((global-hook (default-value 'after-revert-hook))
3855 (local-hook-p (local-variable-p 'after-revert-hook)) 3849 (local-hook (when (local-variable-p 'after-revert-hook)
3856 (local-hook (and (local-variable-p 'after-revert-hook) 3850 after-revert-hook))
3857 after-revert-hook))) 3851 (inhibit-read-only t))
3858 (let (buffer-read-only 3852 (cond
3859 ;; Don't make undo records for the reversion. 3853 (revert-buffer-insert-file-contents-function
3860 (buffer-undo-list t)) 3854 (unless (eq buffer-undo-list t)
3861 (if revert-buffer-insert-file-contents-function 3855 ;; Get rid of all undo records for this buffer.
3862 (funcall revert-buffer-insert-file-contents-function 3856 (setq buffer-undo-list nil))
3863 file-name auto-save-p) 3857 ;; Don't make undo records for the reversion.
3864 (if (not (file-exists-p file-name)) 3858 (let ((buffer-undo-list t))
3865 (error (if buffer-file-number 3859 (funcall revert-buffer-insert-file-contents-function
3866 "File %s no longer exists!" 3860 file-name auto-save-p)))
3867 "Cannot revert nonexistent file %s") 3861 ((not (file-exists-p file-name))
3868 file-name)) 3862 (error (if buffer-file-number
3869 ;; Bind buffer-file-name to nil 3863 "File %s no longer exists!"
3870 ;; so that we don't try to lock the file. 3864 "Cannot revert nonexistent file %s")
3871 (let ((buffer-file-name nil)) 3865 file-name))
3872 (or auto-save-p 3866 (t
3873 (unlock-buffer))) 3867 ;; Bind buffer-file-name to nil
3874 (widen) 3868 ;; so that we don't try to lock the file.
3875 (let ((coding-system-for-read 3869 (let ((buffer-file-name nil))
3876 ;; Auto-saved file shoule be read by Emacs' 3870 (or auto-save-p
3877 ;; internal coding. 3871 (unlock-buffer)))
3878 (if auto-save-p 'auto-save-coding 3872 (widen)
3879 (or coding-system-for-read 3873 (let ((coding-system-for-read
3880 buffer-file-coding-system-explicit)))) 3874 ;; Auto-saved file should be read by Emacs'
3881 ;; This force after-insert-file-set-coding 3875 ;; internal coding.
3882 ;; (called from insert-file-contents) to set 3876 (if auto-save-p 'auto-save-coding
3883 ;; buffer-file-coding-system to a proper value. 3877 (or coding-system-for-read
3884 (kill-local-variable 'buffer-file-coding-system) 3878 buffer-file-coding-system-explicit))))
3885 3879 ;; This force after-insert-file-set-coding
3886 ;; Note that this preserves point in an intelligent way. 3880 ;; (called from insert-file-contents) to set
3887 (if preserve-modes 3881 ;; buffer-file-coding-system to a proper value.
3888 (let ((buffer-file-format buffer-file-format)) 3882 (kill-local-variable 'buffer-file-coding-system)
3889 (insert-file-contents file-name (not auto-save-p) 3883
3890 nil nil t)) 3884 ;; Note that this preserves point in an intelligent way.
3891 (insert-file-contents file-name (not auto-save-p) 3885 (if preserve-modes
3892 nil nil t))))) 3886 (let ((buffer-file-format buffer-file-format))
3887 (insert-file-contents file-name (not auto-save-p)
3888 nil nil t))
3889 (insert-file-contents file-name (not auto-save-p)
3890 nil nil t)))))
3893 ;; Recompute the truename in case changes in symlinks 3891 ;; Recompute the truename in case changes in symlinks
3894 ;; have changed the truename. 3892 ;; have changed the truename.
3895 (setq buffer-file-truename 3893 (setq buffer-file-truename
3896 (abbreviate-file-name (file-truename buffer-file-name))) 3894 (abbreviate-file-name (file-truename buffer-file-name)))
3897 (after-find-file nil nil t t preserve-modes) 3895 (after-find-file nil nil t t preserve-modes)
3898 ;; Run after-revert-hook as it was before we reverted. 3896 ;; Run after-revert-hook as it was before we reverted.
3899 (setq-default revert-buffer-internal-hook global-hook) 3897 (setq-default revert-buffer-internal-hook global-hook)
3900 (if local-hook-p 3898 (if local-hook
3901 (set (make-local-variable 'revert-buffer-internal-hook) 3899 (set (make-local-variable 'revert-buffer-internal-hook)
3902 local-hook) 3900 local-hook)
3903 (kill-local-variable 'revert-buffer-internal-hook)) 3901 (kill-local-variable 'revert-buffer-internal-hook))
3904 (run-hooks 'revert-buffer-internal-hook)) 3902 (run-hooks 'revert-buffer-internal-hook))
3905 t)))))) 3903 t))))))
3941 ;; to emulate what `ls' did in that case. 3939 ;; to emulate what `ls' did in that case.
3942 (insert-directory-safely file switches) 3940 (insert-directory-safely file switches)
3943 (insert-directory-safely file-name switches)))) 3941 (insert-directory-safely file-name switches))))
3944 (yes-or-no-p (format "Recover auto save file %s? " file-name))) 3942 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
3945 (switch-to-buffer (find-file-noselect file t)) 3943 (switch-to-buffer (find-file-noselect file t))
3946 (let ((buffer-read-only nil) 3944 (let ((inhibit-read-only t)
3947 ;; Keep the current buffer-file-coding-system. 3945 ;; Keep the current buffer-file-coding-system.
3948 (coding-system buffer-file-coding-system) 3946 (coding-system buffer-file-coding-system)
3949 ;; Auto-saved file shoule be read with special coding. 3947 ;; Auto-saved file shoule be read with special coding.
3950 (coding-system-for-read 'auto-save-coding)) 3948 (coding-system-for-read 'auto-save-coding))
3951 (erase-buffer) 3949 (erase-buffer)