emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: reverting buffer from non-existent file


From: martin rudalics
Subject: Re: reverting buffer from non-existent file
Date: Fri, 10 Feb 2006 09:45:03 +0100
User-agent: Mozilla Thunderbird 1.0 (Windows/20041206)

> ... shouldn't
>
> (set (make-local-variable 'revert-buffer-internal-hook)
>               local-hook)
>
> be handled by add-hook?

My mind was wandering, please disregard that.  I now also modified
recover-file to use inhibit-read-only and fixed some doc-strings.
        * files.el (revert-buffer, recover-file): Replace
        buffer-read-only by 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.

*** files.el    Wed Feb  1 10:17:44 2006
--- files.el    Fri Feb 10 08:22:16 2006
***************
*** 1024,1030 ****

  Interactively, or if WILDCARDS is non-nil in a call from Lisp,
  expand wildcards (if any) and visit multiple files.  You can
! suppress wildcard expansion by setting `find-file-wildcards'.

  To visit a file without any kind of conversion and without
  automatically choosing a major mode, use \\[find-file-literally]."
--- 1024,1030 ----

  Interactively, or if WILDCARDS is non-nil in a call from Lisp,
  expand wildcards (if any) and visit multiple files.  You can
! suppress wildcard expansion by setting `find-file-wildcards' to nil.

  To visit a file without any kind of conversion and without
  automatically choosing a major mode, use \\[find-file-literally]."
***************
*** 1076,1082 ****

  (defun find-file-existing (filename &optional wildcards)
    "Edit the existing file FILENAME.
! Like \\[find-file] but only allow files that exists."
    (interactive (find-file-read-args "Find existing file: " t))
    (unless (file-exists-p filename) (error "%s does not exist" filename))
    (find-file filename wildcards)
--- 1076,1082 ----

  (defun find-file-existing (filename &optional wildcards)
    "Edit the existing file FILENAME.
! Like \\[find-file] but only allow a file that exists."
    (interactive (find-file-read-args "Find existing file: " t))
    (unless (file-exists-p filename) (error "%s does not exist" filename))
    (find-file filename wildcards)
***************
*** 3787,3794 ****
  to nil.

  Optional second argument NOCONFIRM means don't ask for confirmation at
! all.  (The local variable `revert-without-query', if non-nil, prevents
! confirmation.)

  Optional third argument PRESERVE-MODES non-nil means don't alter
  the files modes.  Normally we reinitialize them using `normal-mode'.
--- 3787,3794 ----
  to nil.

  Optional second argument NOCONFIRM means don't ask for confirmation at
! all.  \(The variable `revert-without-query' offers another way to
! revert buffers without querying for confirmation.)

  Optional third argument PRESERVE-MODES non-nil means don't alter
  the files modes.  Normally we reinitialize them using `normal-mode'.
***************
*** 3823,3835 ****
               (error "Buffer does not seem to be associated with any file"))
              ((or noconfirm
                   (and (not (buffer-modified-p))
!                       (let ((tail revert-without-query)
!                             (found nil))
!                         (while tail
!                           (if (string-match (car tail) file-name)
!                               (setq found t))
!                           (setq tail (cdr tail)))
!                         found))
                   (yes-or-no-p (format "Revert buffer from file %s? "
                                        file-name)))
               (run-hooks 'before-revert-hook)
--- 3823,3832 ----
               (error "Buffer does not seem to be associated with any file"))
              ((or noconfirm
                   (and (not (buffer-modified-p))
!                       (catch 'found
!                         (dolist (regexp revert-without-query)
!                           (when (string-match regexp file-name)
!                             (throw 'found t)))))
                   (yes-or-no-p (format "Revert buffer from file %s? "
                                        file-name)))
               (run-hooks 'before-revert-hook)
***************
*** 3838,3887 ****
               (and (not auto-save-p)
                    (not (verify-visited-file-modtime (current-buffer)))
                    (setq buffer-backed-up nil))
-              ;; Get rid of all undo records for this buffer.
-              (or (eq buffer-undo-list t)
-                  (setq buffer-undo-list nil))
               ;; Effectively copy the after-revert-hook status,
               ;; since after-find-file will clobber it.
               (let ((global-hook (default-value 'after-revert-hook))
!                    (local-hook-p (local-variable-p 'after-revert-hook))
!                    (local-hook (and (local-variable-p 'after-revert-hook)
!                                     after-revert-hook)))
!                (let (buffer-read-only
!                      ;; Don't make undo records for the reversion.
!                      (buffer-undo-list t))
!                  (if revert-buffer-insert-file-contents-function
!                      (funcall revert-buffer-insert-file-contents-function
!                               file-name auto-save-p)
!                    (if (not (file-exists-p file-name))
!                        (error (if buffer-file-number
!                                   "File %s no longer exists!"
!                                 "Cannot revert nonexistent file %s")
!                               file-name))
!                    ;; Bind buffer-file-name to nil
!                    ;; so that we don't try to lock the file.
!                    (let ((buffer-file-name nil))
!                      (or auto-save-p
!                          (unlock-buffer)))
!                    (widen)
!                    (let ((coding-system-for-read
!                           ;; Auto-saved file shoule be read by Emacs'
!                           ;; internal coding.
!                           (if auto-save-p 'auto-save-coding
!                             (or coding-system-for-read
!                                 buffer-file-coding-system-explicit))))
!                      ;; This force after-insert-file-set-coding
!                      ;; (called from insert-file-contents) to set
!                      ;; buffer-file-coding-system to a proper value.
!                      (kill-local-variable 'buffer-file-coding-system)
! 
!                      ;; Note that this preserves point in an intelligent way.
!                      (if preserve-modes
!                          (let ((buffer-file-format buffer-file-format))
!                            (insert-file-contents file-name (not auto-save-p)
!                                                  nil nil t))
!                        (insert-file-contents file-name (not auto-save-p)
!                                              nil nil t)))))
                 ;; Recompute the truename in case changes in symlinks
                 ;; have changed the truename.
                 (setq buffer-file-truename
--- 3835,3885 ----
               (and (not auto-save-p)
                    (not (verify-visited-file-modtime (current-buffer)))
                    (setq buffer-backed-up nil))
               ;; Effectively copy the after-revert-hook status,
               ;; since after-find-file will clobber it.
               (let ((global-hook (default-value 'after-revert-hook))
!                    (local-hook (when (local-variable-p 'after-revert-hook)
!                                  after-revert-hook))
!                    (inhibit-read-only t))
!                (cond
!                 (revert-buffer-insert-file-contents-function
!                  (unless (eq buffer-undo-list t)
!                    ;; Get rid of all undo records for this buffer.
!                    (setq buffer-undo-list nil))
!                  ;; Don't make undo records for the reversion.
!                  (let ((buffer-undo-list t))
!                    (funcall revert-buffer-insert-file-contents-function
!                             file-name auto-save-p)))
!                 ((not (file-exists-p file-name))
!                  (error (if buffer-file-number
!                             "File %s no longer exists!"
!                           "Cannot revert nonexistent file %s")
!                         file-name))
!                 (t
!                  ;; Bind buffer-file-name to nil
!                  ;; so that we don't try to lock the file.
!                  (let ((buffer-file-name nil))
!                    (or auto-save-p
!                        (unlock-buffer)))
!                  (widen)
!                  (let ((coding-system-for-read
!                         ;; Auto-saved file should be read by Emacs'
!                         ;; internal coding.
!                         (if auto-save-p 'auto-save-coding
!                           (or coding-system-for-read
!                               buffer-file-coding-system-explicit))))
!                    ;; This force after-insert-file-set-coding
!                    ;; (called from insert-file-contents) to set
!                    ;; buffer-file-coding-system to a proper value.
!                    (kill-local-variable 'buffer-file-coding-system)
! 
!                    ;; Note that this preserves point in an intelligent way.
!                    (if preserve-modes
!                        (let ((buffer-file-format buffer-file-format))
!                          (insert-file-contents file-name (not auto-save-p)
!                                                nil nil t))
!                      (insert-file-contents file-name (not auto-save-p)
!                                            nil nil t)))))
                 ;; Recompute the truename in case changes in symlinks
                 ;; have changed the truename.
                 (setq buffer-file-truename
***************
*** 3889,3895 ****
                 (after-find-file nil nil t t preserve-modes)
                 ;; Run after-revert-hook as it was before we reverted.
                 (setq-default revert-buffer-internal-hook global-hook)
!                (if local-hook-p
                     (set (make-local-variable 'revert-buffer-internal-hook)
                          local-hook)
                   (kill-local-variable 'revert-buffer-internal-hook))
--- 3887,3893 ----
                 (after-find-file nil nil t t preserve-modes)
                 ;; Run after-revert-hook as it was before we reverted.
                 (setq-default revert-buffer-internal-hook global-hook)
!                (if local-hook
                     (set (make-local-variable 'revert-buffer-internal-hook)
                          local-hook)
                   (kill-local-variable 'revert-buffer-internal-hook))
***************
*** 3935,3941 ****
                   (insert-directory-safely file-name switches))))
             (yes-or-no-p (format "Recover auto save file %s? " file-name)))
           (switch-to-buffer (find-file-noselect file t))
!          (let ((buffer-read-only nil)
                 ;; Keep the current buffer-file-coding-system.
                 (coding-system buffer-file-coding-system)
                 ;; Auto-saved file shoule be read with special coding.
--- 3933,3939 ----
                   (insert-directory-safely file-name switches))))
             (yes-or-no-p (format "Recover auto save file %s? " file-name)))
           (switch-to-buffer (find-file-noselect file t))
!          (let ((inhibit-read-only t)
                 ;; Keep the current buffer-file-coding-system.
                 (coding-system buffer-file-coding-system)
                 ;; Auto-saved file shoule be read with special coding.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]