Rename INSTALL.BZR to UNSTALL.REPOm and carry that through in other files.
[bpt/emacs.git] / lisp / userlock.el
CommitLineData
d501f516
ER
1;;; userlock.el --- handle file access contention between multiple users
2
ba318903 3;; Copyright (C) 1985-1986, 2001-2014 Free Software Foundation, Inc.
58142744 4
6251ee24 5;; Maintainer: FSF
6251ee24 6;; Keywords: internal
d0b822e3 7;; Package: emacs
e5167999 8
a2535589
JA
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
a2535589 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
a2535589
JA
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
a2535589 23
e5167999 24;;; Commentary:
a2535589 25
e5167999 26;; This file is autoloaded to handle certain conditions
a2535589
JA
27;; detected by the file-locking code within Emacs.
28;; The two entry points are `ask-user-about-lock' and
29;; `ask-user-about-supersession-threat'.
30
e5167999 31;;; Code:
a2535589 32
54bd972f 33(define-error 'file-locked "File is locked" 'file-error)
a2535589 34
f9f9507e 35;;;###autoload
bdca5405
RS
36(defun ask-user-about-lock (file opponent)
37 "Ask user what to do when he wants to edit FILE but it is locked by OPPONENT.
a2535589 38This function has a choice of three things to do:
5f60927d 39 do (signal 'file-locked (list FILE OPPONENT))
a2535589
JA
40 to refrain from editing the file
41 return t (grab the lock on the file)
42 return nil (edit the file even though it is locked).
bdca5405
RS
43You can redefine this function to choose among those three alternatives
44in any way you like."
a2535589
JA
45 (discard-input)
46 (save-window-excursion
bdca5405
RS
47 (let (answer short-opponent short-file)
48 (setq short-file
49 (if (> (length file) 22)
50 (concat "..." (substring file (- (length file) 22)))
51 file))
52 (setq short-opponent
53 (if (> (length opponent) 25)
54 (save-match-data
55 (string-match " (pid [0-9]+)" opponent)
56 (concat (substring opponent 0 13) "..."
57 (match-string 0 opponent)))
58 opponent))
a2535589 59 (while (null answer)
bdca5405
RS
60 (message "%s locked by %s: (s, q, p, ?)? "
61 short-file short-opponent)
a2535589
JA
62 (let ((tem (let ((inhibit-quit t)
63 (cursor-in-echo-area t))
64 (prog1 (downcase (read-char))
65 (setq quit-flag nil)))))
66 (if (= tem help-char)
67 (ask-user-about-lock-help)
68 (setq answer (assoc tem '((?s . t)
69 (?q . yield)
70 (?\C-g . yield)
71 (?p . nil)
72 (?? . help))))
73 (cond ((null answer)
74 (beep)
75 (message "Please type q, s, or p; or ? for help")
76 (sit-for 3))
77 ((eq (cdr answer) 'help)
78 (ask-user-about-lock-help)
79 (setq answer nil))
80 ((eq (cdr answer) 'yield)
bdca5405 81 (signal 'file-locked (list file opponent)))))))
a2535589
JA
82 (cdr answer))))
83
84(defun ask-user-about-lock-help ()
85 (with-output-to-temp-buffer "*Help*"
86 (princ "It has been detected that you want to modify a file that someone else has
95d2516a 87already started modifying in Emacs.
a2535589 88
95d2516a 89You can <s>teal the file; the other user becomes the
a2535589
JA
90 intruder if (s)he ever unmodifies the file and then changes it again.
91You can <p>roceed; you edit at your own (and the other user's) risk.
0dc07483 92You can <q>uit; don't modify this file.")
7fdbcd83 93 (with-current-buffer standard-output
0dc07483 94 (help-mode))))
a2535589 95
54bd972f 96(define-error 'file-supersession nil 'file-error)
a2535589 97
f9f9507e 98;;;###autoload
a2535589
JA
99(defun ask-user-about-supersession-threat (fn)
100 "Ask a user who is about to modify an obsolete buffer what to do.
101This function has two choices: it can return, in which case the modification
102of the buffer will proceed, or it can (signal 'file-supersession (file)),
103in which case the proposed buffer modification will not be made.
104
105You can rewrite this to use any criterion you like to choose which one to do.
106The buffer in question is current when this function is called."
107 (discard-input)
108 (save-window-excursion
145823ec
CY
109 (let ((prompt
110 (format "%s changed on disk; \
111really edit the buffer? (y, n, r or C-h) "
112 (file-name-nondirectory fn)))
113 (choices '(?y ?n ?r ?? ?\C-h))
114 answer)
a2535589 115 (while (null answer)
145823ec
CY
116 (setq answer (read-char-choice prompt choices))
117 (cond ((memq answer '(?? ?\C-h))
118 (ask-user-about-supersession-help)
119 (setq answer nil))
120 ((eq answer ?r)
121 ;; Ask for confirmation if buffer modified
122 (revert-buffer nil (not (buffer-modified-p)))
123 (signal 'file-supersession
124 (list "File reverted" fn)))
125 ((eq answer ?n)
126 (signal 'file-supersession
127 (list "File changed on disk" fn)))))
a2535589 128 (message
145823ec 129 "File on disk now will become a backup file if you save these changes.")
a2535589
JA
130 (setq buffer-backed-up nil))))
131
132(defun ask-user-about-supersession-help ()
133 (with-output-to-temp-buffer "*Help*"
134 (princ "You want to modify a buffer whose disk file has changed
135since you last read it in or saved it with this buffer.
136
137If you say `y' to go ahead and modify this buffer,
138you risk ruining the work of whoever rewrote the file.
11fc9165
RS
139If you say `r' to revert, the contents of the buffer are refreshed
140from the file on disk.
a2535589
JA
141If you say `n', the change you started to make will be aborted.
142
143Usually, you should type `n' and then `M-x revert-buffer',
0dc07483 144to get the latest version of the file, then make the change again.")
7fdbcd83 145 (with-current-buffer standard-output
0dc07483 146 (help-mode))))
a2535589 147
d501f516 148;;; userlock.el ends here