1 ;;; userlock.el --- handle file access contention between multiple users
3 ;; Copyright (C) 1985, 1986, 1993 Free Software Foundation, inc.
8 ;; This file is part of XEmacs.
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)
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 ;;; Synched up with: FSF 19.34.
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'.
36 (define-error 'file-locked "File is locked" 'file-error) ; XEmacs
38 (defun ask-user-about-lock-minibuf (fn opponent)
39 (save-window-excursion
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)))))
48 (ask-user-about-lock-help)
49 (setq answer (assoc tem '((?s . t)
56 (message "Please type q, s, or p; or ? for help")
58 ((eq (cdr answer) 'help)
59 (ask-user-about-lock-help)
61 ((eq (cdr answer) 'yield)
62 (signal 'file-locked (list "File is locked" fn opponent)))))))
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.
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.")
75 (set-buffer standard-output)
78 (define-error 'file-supersession "File changed on disk" 'file-error) ; XEmacs
80 (defun ask-user-about-supersession-threat-minibuf (fn)
81 (save-window-excursion
84 (message "%s changed on disk; really edit the buffer? (y, n, r or C-h) "
85 (file-name-nondirectory fn))
86 (let ((tem (downcase (let ((cursor-in-echo-area t))
91 (cdr (assoc tem '((?n . yield)
98 (message "Please type y, n or r; or ? for help")
101 (ask-user-about-supersession-help)
104 (revert-buffer nil (not (buffer-modified-p)))
105 ; ask confirmation iff buffer modified
106 (signal 'file-supersession
107 (list "File reverted" fn)))
109 (signal 'file-supersession
110 (list "File changed on disk" fn))))))
112 "File on disk now will become a backup file if you save these changes.")
113 (setq buffer-backed-up nil))))
115 (defun ask-user-about-supersession-help ()
116 (with-output-to-temp-buffer "*Help*"
117 (princ "You want to modify a buffer whose disk file has changed
118 since you last read it in or saved it with this buffer.
120 If you say `y' to go ahead and modify this buffer,
121 you risk ruining the work of whoever rewrote the file.
122 If you say `r' to revert, the contents of the buffer are refreshed
123 from the file on disk.
124 If you say `n', the change you started to make will be aborted.
126 Usually, you should type `n' and then `M-x revert-buffer',
127 to get the latest version of the file, then make the change again.")
129 (set-buffer standard-output)
132 ;;; dialog-box versions [XEmacs]
134 (defun ask-user-about-lock-dbox (fn opponent)
135 (let ((echo-keystrokes 0)
138 (format "%s is locking %s\n
139 It has been detected that you want to modify a file that
140 someone else has already started modifying in XEmacs."
142 '(["Steal Lock\n\nThe other user will\nbecome the intruder" steal t]
143 ["Proceed\n\nEdit file at your own\n\(and the other user's) risk"
146 ["Abort\n\nDon't modify the buffer\n" yield t]))))
147 (popup-dialog-box dbox)
150 (let ((event (next-command-event)))
151 (cond ((and (misc-user-event-p event) (eq (event-object event) 'proceed))
152 (throw 'aual-done nil))
153 ((and (misc-user-event-p event) (eq (event-object event) 'steal))
154 (throw 'aual-done t))
155 ((and (misc-user-event-p event) (eq (event-object event) 'yield))
156 (signal 'file-locked (list "File is locked" fn opponent)))
157 ((and (misc-user-event-p event)
158 (eq (event-object event) 'menu-no-selection-hook))
160 ((button-release-event-p event) ;; don't beep twice
164 (message "please answer the dialog box"))))))))
166 (defun ask-user-about-supersession-threat-dbox (fn)
167 (let ((echo-keystrokes 0)
170 (format "File %s has changed on disk
171 since its buffer was last read in or saved.
173 Do you really want to edit the buffer? " fn)
174 '(["Yes\n\nEdit the buffer anyway,\nignoring the disk file"
176 ["No\n\nDon't modify the buffer\n" yield t]
178 ["No\n\nDon't modify the buffer\nbut revert it" revert t]
180 (popup-dialog-box dbox)
183 (let ((event (next-command-event)))
184 (cond ((and (misc-user-event-p event) (eq (event-object event) 'proceed))
185 (throw 'auast-done nil))
186 ((and (misc-user-event-p event) (eq (event-object event) 'yield))
187 (signal 'file-supersession (list fn)))
188 ((and (misc-user-event-p event) (eq (event-object event) 'revert))
189 (or (equal fn (buffer-file-name))
191 "ask-user-about-supersession-threat called bogusly"))
192 (revert-buffer nil t)
193 (signal 'file-supersession
194 (list fn "(reverted)")))
195 ((and (misc-user-event-p event)
196 (eq (event-object event) 'menu-no-selection-hook))
198 ((button-release-event-p event) ;; don't beep twice
202 (message "please answer the dialog box"))))))))
208 (defun ask-user-about-lock (fn opponent)
209 "Ask user what to do when he wants to edit FILE but it is locked by USER.
210 This function has a choice of three things to do:
211 do (signal 'file-locked (list FILE USER))
212 to refrain from editing the file
213 return t (grab the lock on the file)
214 return nil (edit the file even though it is locked).
215 You can rewrite it to use any criterion you like to choose which one to do."
217 (if (and (fboundp 'popup-dialog-box)
218 (or (button-press-event-p last-command-event)
219 (button-release-event-p last-command-event)
220 (misc-user-event-p last-command-event)))
221 (ask-user-about-lock-dbox fn opponent)
222 (ask-user-about-lock-minibuf fn opponent)))
225 (defun ask-user-about-supersession-threat (fn)
226 "Ask a user who is about to modify an obsolete buffer what to do.
227 This function has two choices: it can return, in which case the modification
228 of the buffer will proceed, or it can (signal 'file-supersession (file)),
229 in which case the proposed buffer modification will not be made.
231 You can rewrite this to use any criterion you like to choose which one to do.
232 The buffer in question is current when this function is called."
234 (if (and (fboundp 'popup-dialog-box)
235 (or (button-press-event-p last-command-event)
236 (button-release-event-p last-command-event)
237 (misc-user-event-p last-command-event)))
238 (ask-user-about-supersession-threat-dbox fn)
239 (ask-user-about-supersession-threat-minibuf fn)))
241 ;;; userlock.el ends here