0
|
1 ;;; userlock.el --- handle file access contention between multiple users
|
|
2
|
|
3 ;; Copyright (C) 1985, 1986, 1993 Free Software Foundation, inc.
|
|
4
|
|
5 ;; Maintainer: FSF
|
|
6 ;; Keywords: internal
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
70
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
22 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
23 ;; Boston, MA 02111-1307, USA.
|
0
|
24
|
70
|
25 ;;; Synched up with: FSF 19.30.
|
0
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; This file is autoloaded to handle certain conditions
|
|
30 ;; detected by the file-locking code within XEmacs.
|
|
31 ;; The two entry points are `ask-user-about-lock' and
|
|
32 ;; `ask-user-about-supersession-threat'.
|
|
33
|
|
34 ;;; Code:
|
|
35
|
70
|
36 (define-error 'file-locked "File is locked" 'file-error)
|
0
|
37
|
|
38 (defun ask-user-about-lock-minibuf (fn opponent)
|
|
39 (save-window-excursion
|
|
40 (let (answer)
|
|
41 (while (null answer)
|
|
42 (message "%s is locking %s: action (s, q, p, ?)? " opponent fn)
|
|
43 (let ((tem (let ((inhibit-quit t)
|
|
44 (cursor-in-echo-area t))
|
|
45 (prog1 (downcase (read-char))
|
|
46 (setq quit-flag nil)))))
|
|
47 (if (= tem help-char)
|
|
48 (ask-user-about-lock-help)
|
|
49 (setq answer (assoc tem '((?s . t)
|
|
50 (?q . yield)
|
|
51 (?\C-g . yield)
|
|
52 (?p . nil)
|
|
53 (?? . help))))
|
|
54 (cond ((null answer)
|
|
55 (beep)
|
|
56 (message "Please type q, s, or p; or ? for help")
|
|
57 (sit-for 3))
|
|
58 ((eq (cdr answer) 'help)
|
|
59 (ask-user-about-lock-help)
|
|
60 (setq answer nil))
|
|
61 ((eq (cdr answer) 'yield)
|
70
|
62 (signal 'file-locked (list fn opponent)))))))
|
0
|
63 (cdr answer))))
|
|
64
|
|
65 (defun ask-user-about-lock-help ()
|
|
66 (with-output-to-temp-buffer "*Help*"
|
|
67 (princ "It has been detected that you want to modify a file that someone else has
|
|
68 already started modifying in EMACS.
|
|
69
|
|
70 You can <s>teal the file; The other user becomes the
|
|
71 intruder if (s)he ever unmodifies the file and then changes it again.
|
|
72 You can <p>roceed; you edit at your own (and the other user's) risk.
|
|
73 You can <q>uit; don't modify this file.")
|
|
74 (save-excursion
|
|
75 (set-buffer standard-output)
|
|
76 (help-mode))))
|
|
77
|
70
|
78 (define-error 'file-supersession "File changed on disk" 'file-error)
|
0
|
79
|
|
80 (defun ask-user-about-supersession-threat-minibuf (fn)
|
|
81 (save-window-excursion
|
|
82 (let (answer)
|
|
83 (while (null answer)
|
70
|
84 (message "%s changed on disk; really edit the buffer? (y, n or C-h) "
|
0
|
85 (file-name-nondirectory fn))
|
|
86 (let ((tem (downcase (let ((cursor-in-echo-area t))
|
|
87 (read-char)))))
|
|
88 (setq answer
|
|
89 (if (= tem help-char)
|
|
90 'help
|
|
91 (cdr (assoc tem '((?n . yield)
|
|
92 (?\C-g . yield)
|
|
93 (?y . proceed)
|
|
94 (?? . help))))))
|
|
95 (cond ((null answer)
|
|
96 (beep)
|
70
|
97 (message "Please type y or n; or ? for help")
|
0
|
98 (sit-for 3))
|
|
99 ((eq answer 'help)
|
|
100 (ask-user-about-supersession-help)
|
|
101 (setq answer nil))
|
|
102 ((eq answer 'yield)
|
|
103 (signal 'file-supersession
|
70
|
104 (list fn))))))
|
0
|
105 (message
|
|
106 "File on disk now will become a backup file if you save these changes.")
|
|
107 (setq buffer-backed-up nil))))
|
|
108
|
|
109 (defun ask-user-about-supersession-help ()
|
|
110 (with-output-to-temp-buffer "*Help*"
|
|
111 (princ "You want to modify a buffer whose disk file has changed
|
|
112 since you last read it in or saved it with this buffer.
|
|
113
|
|
114 If you say `y' to go ahead and modify this buffer,
|
|
115 you risk ruining the work of whoever rewrote the file.
|
|
116 If you say `n', the change you started to make will be aborted.
|
|
117
|
|
118 Usually, you should type `n' and then `M-x revert-buffer',
|
|
119 to get the latest version of the file, then make the change again.")
|
|
120 (save-excursion
|
|
121 (set-buffer standard-output)
|
|
122 (help-mode))))
|
70
|
123
|
0
|
124
|
70
|
125 ;;; dialog-box versions
|
0
|
126
|
|
127 (defun ask-user-about-lock-dbox (fn opponent)
|
|
128 (let ((echo-keystrokes 0)
|
|
129 (dbox
|
|
130 (cons
|
|
131 (format "%s is locking %s\n
|
|
132 It has been detected that you want to modify a file that
|
|
133 someone else has already started modifying in XEmacs."
|
|
134 opponent fn)
|
|
135 '(["Steal Lock\n\nThe other user will\nbecome the intruder" steal t]
|
|
136 ["Proceed\n\nEdit file at your own\n\(and the other user's) risk"
|
|
137 proceed t]
|
|
138 nil
|
|
139 ["Abort\n\nDon't modify the buffer\n" yield t]))))
|
|
140 (popup-dialog-box dbox)
|
|
141 (catch 'aual-done
|
|
142 (while t
|
|
143 (let ((event (next-command-event)))
|
|
144 (cond ((and (misc-user-event-p event) (eq (event-object event) 'proceed))
|
|
145 (throw 'aual-done nil))
|
|
146 ((and (misc-user-event-p event) (eq (event-object event) 'steal))
|
|
147 (throw 'aual-done t))
|
|
148 ((and (misc-user-event-p event) (eq (event-object event) 'yield))
|
70
|
149 (signal 'file-locked (list fn opponent)))
|
0
|
150 ((button-release-event-p event) ;; don't beep twice
|
|
151 nil)
|
|
152 (t
|
|
153 (beep)
|
|
154 (message "please answer the dialog box"))))))))
|
|
155
|
|
156 (defun ask-user-about-supersession-threat-dbox (fn)
|
|
157 (let ((echo-keystrokes 0)
|
|
158 (dbox
|
|
159 (cons
|
|
160 (format "File %s has changed on disk
|
|
161 since its buffer was last read in or saved.
|
|
162
|
|
163 Do you really want to edit the buffer? " fn)
|
|
164 '(["Yes\n\nEdit the buffer anyway,\nignoring the disk file"
|
|
165 proceed t]
|
|
166 ["No\n\nDon't modify the buffer\n" yield t]
|
|
167 nil
|
|
168 ["No\n\nDon't modify the buffer\nbut revert it" revert t]
|
|
169 ))))
|
|
170 (popup-dialog-box dbox)
|
|
171 (catch 'auast-done
|
|
172 (while t
|
|
173 (let ((event (next-command-event)))
|
|
174 (cond ((and (misc-user-event-p event) (eq (event-object event) 'proceed))
|
|
175 (throw 'auast-done nil))
|
|
176 ((and (misc-user-event-p event) (eq (event-object event) 'yield))
|
|
177 (signal 'file-supersession (list fn)))
|
|
178 ((and (misc-user-event-p event) (eq (event-object event) 'revert))
|
|
179 (or (equal fn (buffer-file-name))
|
|
180 (error
|
|
181 "ask-user-about-supersession-threat called bogusly"))
|
|
182 (revert-buffer nil t)
|
|
183 (signal 'file-supersession
|
|
184 (list fn "(reverted)")))
|
|
185 ((button-release-event-p event) ;; don't beep twice
|
|
186 nil)
|
|
187 (t
|
|
188 (beep)
|
|
189 (message "please answer the dialog box"))))))))
|
|
190
|
|
191
|
|
192 ;;; top-level
|
|
193
|
|
194 ;;;###autoload
|
|
195 (defun ask-user-about-lock (fn opponent)
|
|
196 "Ask user what to do when he wants to edit FILE but it is locked by USER.
|
|
197 This function has a choice of three things to do:
|
|
198 do (signal 'file-locked (list FILE USER))
|
|
199 to refrain from editing the file
|
|
200 return t (grab the lock on the file)
|
|
201 return nil (edit the file even though it is locked).
|
|
202 You can rewrite it to use any criterion you like to choose which one to do."
|
|
203 (discard-input)
|
|
204 (if (and (fboundp 'popup-dialog-box)
|
|
205 (or (button-press-event-p last-command-event)
|
|
206 (button-release-event-p last-command-event)
|
|
207 (misc-user-event-p last-command-event)))
|
|
208 (ask-user-about-lock-dbox fn opponent)
|
|
209 (ask-user-about-lock-minibuf fn opponent)))
|
|
210
|
|
211 ;;;###autoload
|
|
212 (defun ask-user-about-supersession-threat (fn)
|
|
213 "Ask a user who is about to modify an obsolete buffer what to do.
|
|
214 This function has two choices: it can return, in which case the modification
|
|
215 of the buffer will proceed, or it can (signal 'file-supersession (file)),
|
|
216 in which case the proposed buffer modification will not be made.
|
|
217
|
|
218 You can rewrite this to use any criterion you like to choose which one to do.
|
|
219 The buffer in question is current when this function is called."
|
|
220 (discard-input)
|
|
221 (if (and (fboundp 'popup-dialog-box)
|
|
222 (or (button-press-event-p last-command-event)
|
|
223 (button-release-event-p last-command-event)
|
|
224 (misc-user-event-p last-command-event)))
|
|
225 (ask-user-about-supersession-threat-dbox fn)
|
|
226 (ask-user-about-supersession-threat-minibuf fn)))
|
|
227
|
|
228 ;;; userlock.el ends here
|