Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / userlock.el
blob73bb0d2aae0155a70d3282110f86bcf11d0c550e
1 ;;; userlock.el --- handle file access contention between multiple users
3 ;; Copyright (C) 1985-1986, 2001-2018 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 (eval-when-compile (require 'cl-lib))
37 (define-error 'file-locked "File is locked" 'file-error)
39 ;;;###autoload
40 (defun ask-user-about-lock (file opponent)
41 "Ask user what to do when he wants to edit FILE but it is locked by OPPONENT.
42 This function has a choice of three things to do:
43 do (signal \\='file-locked (list FILE OPPONENT))
44 to refrain from editing the file
45 return t (grab the lock on the file)
46 return nil (edit the file even though it is locked).
47 You can redefine this function to choose among those three alternatives
48 in any way you like."
49 (discard-input)
50 (save-window-excursion
51 (let (answer short-opponent short-file)
52 (setq short-file
53 (if (> (length file) 22)
54 (concat "..." (substring file (- (length file) 22)))
55 file))
56 (setq short-opponent
57 (if (> (length opponent) 25)
58 (save-match-data
59 (string-match " (pid [0-9]+)" opponent)
60 (concat (substring opponent 0 13) "..."
61 (match-string 0 opponent)))
62 opponent))
63 (while (null answer)
64 (message "%s locked by %s: (s, q, p, ?)? "
65 short-file short-opponent)
66 (if noninteractive (error "Cannot resolve lock conflict in batch mode"))
67 (let ((tem (let ((inhibit-quit t)
68 (cursor-in-echo-area t))
69 (prog1 (downcase (read-char))
70 (setq quit-flag nil)))))
71 (if (= tem help-char)
72 (ask-user-about-lock-help)
73 (setq answer (assoc tem '((?s . t)
74 (?q . yield)
75 (?\C-g . yield)
76 (?p . nil)
77 (?? . help))))
78 (cond ((null answer)
79 (beep)
80 (message "Please type q, s, or p; or ? for help")
81 (sit-for 3))
82 ((eq (cdr answer) 'help)
83 (ask-user-about-lock-help)
84 (setq answer nil))
85 ((eq (cdr answer) 'yield)
86 (signal 'file-locked (list file opponent)))))))
87 (cdr answer))))
89 (defun ask-user-about-lock-help ()
90 (with-output-to-temp-buffer "*Help*"
91 (princ "It has been detected that you want to modify a file that someone else has
92 already started modifying in Emacs.
94 You can <s>teal the file; the other user becomes the
95 intruder if (s)he ever unmodifies the file and then changes it again.
96 You can <p>roceed; you edit at your own (and the other user's) risk.
97 You can <q>uit; don't modify this file.")
98 (with-current-buffer standard-output
99 (help-mode))))
101 (define-error 'file-supersession nil 'file-error)
103 (defun userlock--check-content-unchanged (fn)
104 (with-demoted-errors "Unchanged content check: %S"
105 ;; Even tho we receive `fn', we know that `fn' refers to the current
106 ;; buffer's file.
107 (cl-assert (equal fn (expand-file-name buffer-file-truename)))
108 ;; Note: rather than read the file and compare to the buffer, we could save
109 ;; the buffer and compare to the file, but for encrypted data this
110 ;; wouldn't work well (and would risk exposing the data).
111 (save-restriction
112 (widen)
113 (let ((buf (current-buffer))
114 (cs buffer-file-coding-system)
115 (start (point-min))
116 (end (point-max)))
117 ;; FIXME: To avoid a slow `insert-file-contents' on large or
118 ;; remote files, it'd be good to include file size in the
119 ;; "visited-modtime" check.
120 (when (with-temp-buffer
121 (let ((coding-system-for-read cs)
122 (non-essential t))
123 (insert-file-contents fn))
124 (when (= (buffer-size) (- end start)) ;Minor optimization.
125 (= 0 (let ((case-fold-search nil))
126 (compare-buffer-substrings
127 buf start end
128 (current-buffer) (point-min) (point-max))))))
129 (set-visited-file-modtime)
130 'unchanged)))))
132 ;;;###autoload
133 (defun userlock--ask-user-about-supersession-threat (fn)
134 ;; Called from filelock.c.
135 (unless (userlock--check-content-unchanged fn)
136 (ask-user-about-supersession-threat fn)))
138 ;;;###autoload
139 (defun ask-user-about-supersession-threat (fn)
140 "Ask a user who is about to modify an obsolete buffer what to do.
141 This function has two choices: it can return, in which case the modification
142 of the buffer will proceed, or it can (signal \\='file-supersession (file)),
143 in which case the proposed buffer modification will not be made.
145 You can rewrite this to use any criterion you like to choose which one to do.
146 The buffer in question is current when this function is called."
147 (discard-input)
148 (save-window-excursion
149 (let ((prompt
150 (format "%s changed on disk; \
151 really edit the buffer? (y, n, r or C-h) "
152 (file-name-nondirectory fn)))
153 (choices '(?y ?n ?r ?? ?\C-h))
154 answer)
155 (when noninteractive
156 (message "%s" prompt)
157 (error "Cannot resolve conflict in batch mode"))
158 (while (null answer)
159 (setq answer (read-char-choice prompt choices))
160 (cond ((memq answer '(?? ?\C-h))
161 (ask-user-about-supersession-help)
162 (setq answer nil))
163 ((eq answer ?r)
164 ;; Ask for confirmation if buffer modified
165 (revert-buffer nil (not (buffer-modified-p)))
166 (signal 'file-supersession
167 (list "File reverted" fn)))
168 ((eq answer ?n)
169 (signal 'file-supersession
170 (list "File changed on disk" fn)))))
171 (message
172 "File on disk now will become a backup file if you save these changes.")
173 (setq buffer-backed-up nil))))
175 (defun ask-user-about-supersession-help ()
176 (with-output-to-temp-buffer "*Help*"
177 (princ "You want to modify a buffer whose disk file has changed
178 since you last read it in or saved it with this buffer.
180 If you say `y' to go ahead and modify this buffer,
181 you risk ruining the work of whoever rewrote the file.
182 If you say `r' to revert, the contents of the buffer are refreshed
183 from the file on disk.
184 If you say `n', the change you started to make will be aborted.
186 Usually, you should type `n' and then `\\[revert-buffer]',
187 to get the latest version of the file, then make the change again.")
188 (with-current-buffer standard-output
189 (help-mode))))
191 ;;; userlock.el ends here