[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] master 5a4bffb: Check actual contents before promting abou
From: |
Stefan Monnier |
Subject: |
[Emacs-diffs] master 5a4bffb: Check actual contents before promting about changed file |
Date: |
Fri, 2 Sep 2016 15:41:10 +0000 (UTC) |
branch: master
commit 5a4bffb6617a274ca19bc7f5c1b1ceb6345651ab
Author: Stefan Monnier <address@hidden>
Commit: Stefan Monnier <address@hidden>
Check actual contents before promting about changed file
* lisp/userlock.el (userlock--check-content-unchanged)
(userlock--ask-user-about-supersession-threat): New functions.
* src/filelock.c (lock_file): Use them to avoid spurious prompting.
* doc/lispref/buffers.texi (Modification Time): Update doc of
ask-user-about-supersession-threat.
---
doc/lispref/buffers.texi | 5 +++--
etc/NEWS | 4 ++++
lisp/userlock.el | 35 +++++++++++++++++++++++++++++++++++
src/filelock.c | 2 +-
4 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/doc/lispref/buffers.texi b/doc/lispref/buffers.texi
index 740d7cf..e4ef4d5 100644
--- a/doc/lispref/buffers.texi
+++ b/doc/lispref/buffers.texi
@@ -669,8 +669,9 @@ reason.
This function is used to ask a user how to proceed after an attempt to
modify an buffer visiting file @var{filename} when the file is newer
than the buffer text. Emacs detects this because the modification
-time of the file on disk is newer than the last save-time of the
-buffer. This means some other program has probably altered the file.
+time of the file on disk is newer than the last save-time and its contents
+have changed.
+This means some other program has probably altered the file.
@kindex file-supersession
Depending on the user's answer, the function may return normally, in
diff --git a/etc/NEWS b/etc/NEWS
index 18975cb..3092e9f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -213,6 +213,10 @@ In modes where form feed was treated as a whitespace
character,
It now deletes whitespace after the last form feed thus behaving the
same as in modes where the character is not whitespace.
+** No more prompt about changed file when the file's content is unchanged.
+Instead of only checking the modification time, Emacs now also checks
+the file's actual content before prompting the user.
+
* Changes in Specialized Modes and Packages in Emacs 25.2
diff --git a/lisp/userlock.el b/lisp/userlock.el
index a0c55fd..1cfc3b9 100644
--- a/lisp/userlock.el
+++ b/lisp/userlock.el
@@ -97,6 +97,41 @@ You can <q>uit; don't modify this file.")
(define-error 'file-supersession nil 'file-error)
+(defun userlock--check-content-unchanged (fn)
+ (with-demoted-errors "Unchanged content check: %S"
+ ;; Even tho we receive `fn', we know that `fn' refers to the current
+ ;; buffer's file.
+ (cl-assert (equal fn (expand-file-name buffer-file-truename)))
+ ;; Note: rather than read the file and compare to the buffer, we could save
+ ;; the buffer and compare to the file, but for encrypted data this
+ ;; wouldn't work well (and would risk exposing the data).
+ (save-restriction
+ (widen)
+ (let ((buf (current-buffer))
+ (cs buffer-file-coding-system)
+ (start (point-min))
+ (end (point-max)))
+ ;; FIXME: To avoid a slow `insert-file-contents' on large or
+ ;; remote files, it'd be good to include file size in the
+ ;; "visited-modtime" check.
+ (when (with-temp-buffer
+ (let ((coding-system-for-read cs)
+ (non-essential t))
+ (insert-file-contents fn))
+ (when (= (buffer-size) (- end start)) ;Minor optimization.
+ (= 0 (let ((case-fold-search nil))
+ (compare-buffer-substrings
+ buf start end
+ (current-buffer) (point-min) (point-max))))))
+ (set-visited-file-modtime)
+ 'unchanged)))))
+
+;;;###autoload
+(defun userlock--ask-user-about-supersession-threat (fn)
+ ;; Called from filelock.c.
+ (unless (userlock--check-content-unchanged fn)
+ (ask-user-about-supersession-threat fn)))
+
;;;###autoload
(defun ask-user-about-supersession-threat (fn)
"Ask a user who is about to modify an obsolete buffer what to do.
diff --git a/src/filelock.c b/src/filelock.c
index 2f92e0f..bde34e2 100644
--- a/src/filelock.c
+++ b/src/filelock.c
@@ -684,7 +684,7 @@ lock_file (Lisp_Object fn)
if (!NILP (subject_buf)
&& NILP (Fverify_visited_file_modtime (subject_buf))
&& !NILP (Ffile_exists_p (fn)))
- call1 (intern ("ask-user-about-supersession-threat"), fn);
+ call1 (intern ("userlock--ask-user-about-supersession-threat"), fn);
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Emacs-diffs] master 5a4bffb: Check actual contents before promting about changed file,
Stefan Monnier <=