comparison lisp/prim/userlock.el @ 0:376386a54a3c r19-14

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