summaryrefslogtreecommitdiff
path: root/lisp/userlock.el
diff options
context:
space:
mode:
authorStefan Monnier <monnier@iro.umontreal.ca>2016-09-02 11:44:13 -0400
committerStefan Monnier <monnier@iro.umontreal.ca>2016-09-02 11:44:13 -0400
commit5a4bffb6617a274ca19bc7f5c1b1ceb6345651ab (patch)
treee46b22767dd63377aa2ae58c845a9e2e29cb000e /lisp/userlock.el
parent7dc4c3ba3fd243efa51fe8c2092b4a030be8307d (diff)
downloademacs-5a4bffb6617a274ca19bc7f5c1b1ceb6345651ab.tar.gz
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.
Diffstat (limited to 'lisp/userlock.el')
-rw-r--r--lisp/userlock.el35
1 files changed, 35 insertions, 0 deletions
diff --git a/lisp/userlock.el b/lisp/userlock.el
index a0c55fd1e13..1cfc3b9d64a 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.