* lisp/net/tramp-cmds.el, lisp/allout.el: Avoid custom-set-variables
[emacs.git] / lisp / userlock.el
blob1d45b3a4add035ad123e5d0110bc6a57124d9158
1 ;;; userlock.el --- handle file access contention between multiple users
3 ;; Copyright (C) 1985-1986, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: Richard King
6 ;; (according to authors.el)
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: internal
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This file is autoloaded to handle certain conditions
29 ;; detected by the file-locking code within Emacs.
30 ;; The two entry points are `ask-user-about-lock' and
31 ;; `ask-user-about-supersession-threat'.
33 ;;; Code:
35 (define-error 'file-locked "File is locked" 'file-error)
37 ;;;###autoload
38 (defun ask-user-about-lock (file opponent)
39 "Ask user what to do when he wants to edit FILE but it is locked by OPPONENT.
40 This function has a choice of three things to do:
41 do (signal \\='file-locked (list FILE OPPONENT))
42 to refrain from editing the file
43 return t (grab the lock on the file)
44 return nil (edit the file even though it is locked).
45 You can redefine this function to choose among those three alternatives
46 in any way you like."
47 (discard-input)
48 (save-window-excursion
49 (let (answer short-opponent short-file)
50 (setq short-file
51 (if (> (length file) 22)
52 (concat "..." (substring file (- (length file) 22)))
53 file))
54 (setq short-opponent
55 (if (> (length opponent) 25)
56 (save-match-data
57 (string-match " (pid [0-9]+)" opponent)
58 (concat (substring opponent 0 13) "..."
59 (match-string 0 opponent)))
60 opponent))
61 (while (null answer)
62 (message "%s locked by %s: (s, q, p, ?)? "
63 short-file short-opponent)
64 (if noninteractive (error "Cannot resolve lock conflict in batch mode"))
65 (let ((tem (let ((inhibit-quit t)
66 (cursor-in-echo-area t))
67 (prog1 (downcase (read-char))
68 (setq quit-flag nil)))))
69 (if (= tem help-char)
70 (ask-user-about-lock-help)
71 (setq answer (assoc tem '((?s . t)
72 (?q . yield)
73 (?\C-g . yield)
74 (?p . nil)
75 (?? . help))))
76 (cond ((null answer)
77 (beep)
78 (message "Please type q, s, or p; or ? for help")
79 (sit-for 3))
80 ((eq (cdr answer) 'help)
81 (ask-user-about-lock-help)
82 (setq answer nil))
83 ((eq (cdr answer) 'yield)
84 (signal 'file-locked (list file opponent)))))))
85 (cdr answer))))
87 (defun ask-user-about-lock-help ()
88 (with-output-to-temp-buffer "*Help*"
89 (princ "It has been detected that you want to modify a file that someone else has
90 already started modifying in Emacs.
92 You can <s>teal the file; the other user becomes the
93 intruder if (s)he ever unmodifies the file and then changes it again.
94 You can <p>roceed; you edit at your own (and the other user's) risk.
95 You can <q>uit; don't modify this file.")
96 (with-current-buffer standard-output
97 (help-mode))))
99 (define-error 'file-supersession nil 'file-error)
101 (defun userlock--check-content-unchanged (fn)
102 (with-demoted-errors "Unchanged content check: %S"
103 ;; Even tho we receive `fn', we know that `fn' refers to the current
104 ;; buffer's file.
105 (cl-assert (equal fn (expand-file-name buffer-file-truename)))
106 ;; Note: rather than read the file and compare to the buffer, we could save
107 ;; the buffer and compare to the file, but for encrypted data this
108 ;; wouldn't work well (and would risk exposing the data).
109 (save-restriction
110 (widen)
111 (let ((buf (current-buffer))
112 (cs buffer-file-coding-system)
113 (start (point-min))
114 (end (point-max)))
115 ;; FIXME: To avoid a slow `insert-file-contents' on large or
116 ;; remote files, it'd be good to include file size in the
117 ;; "visited-modtime" check.
118 (when (with-temp-buffer
119 (let ((coding-system-for-read cs)
120 (non-essential t))
121 (insert-file-contents fn))
122 (when (= (buffer-size) (- end start)) ;Minor optimization.
123 (= 0 (let ((case-fold-search nil))
124 (compare-buffer-substrings
125 buf start end
126 (current-buffer) (point-min) (point-max))))))
127 (set-visited-file-modtime)
128 'unchanged)))))
130 ;;;###autoload
131 (defun userlock--ask-user-about-supersession-threat (fn)
132 ;; Called from filelock.c.
133 (unless (userlock--check-content-unchanged fn)
134 (ask-user-about-supersession-threat fn)))
136 ;;;###autoload
137 (defun ask-user-about-supersession-threat (fn)
138 "Ask a user who is about to modify an obsolete buffer what to do.
139 This function has two choices: it can return, in which case the modification
140 of the buffer will proceed, or it can (signal \\='file-supersession (file)),
141 in which case the proposed buffer modification will not be made.
143 You can rewrite this to use any criterion you like to choose which one to do.
144 The buffer in question is current when this function is called."
145 (discard-input)
146 (save-window-excursion
147 (let ((prompt
148 (format "%s changed on disk; \
149 really edit the buffer? (y, n, r or C-h) "
150 (file-name-nondirectory fn)))
151 (choices '(?y ?n ?r ?? ?\C-h))
152 answer)
153 (when noninteractive
154 (message "%s" prompt)
155 (error "Cannot resolve conflict in batch mode"))
156 (while (null answer)
157 (setq answer (read-char-choice prompt choices))
158 (cond ((memq answer '(?? ?\C-h))
159 (ask-user-about-supersession-help)
160 (setq answer nil))
161 ((eq answer ?r)
162 ;; Ask for confirmation if buffer modified
163 (revert-buffer nil (not (buffer-modified-p)))
164 (signal 'file-supersession
165 (list "File reverted" fn)))
166 ((eq answer ?n)
167 (signal 'file-supersession
168 (list "File changed on disk" fn)))))
169 (message
170 "File on disk now will become a backup file if you save these changes.")
171 (setq buffer-backed-up nil))))
173 (defun ask-user-about-supersession-help ()
174 (with-output-to-temp-buffer "*Help*"
175 (princ "You want to modify a buffer whose disk file has changed
176 since you last read it in or saved it with this buffer.
178 If you say `y' to go ahead and modify this buffer,
179 you risk ruining the work of whoever rewrote the file.
180 If you say `r' to revert, the contents of the buffer are refreshed
181 from the file on disk.
182 If you say `n', the change you started to make will be aborted.
184 Usually, you should type `n' and then `\\[revert-buffer]',
185 to get the latest version of the file, then make the change again.")
186 (with-current-buffer standard-output
187 (help-mode))))
189 ;;; userlock.el ends here