XEmacs 21.2.11
[chise/xemacs-chise.git.1] / lisp / userlock.el
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, Inc., 59 Temple Place - Suite 330, Boston, MA
23 ;; 02111-1307, USA.
24
25 ;;; Synched up with: FSF 19.34.
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
36 (define-error 'file-locked "File is locked" 'file-error) ; XEmacs
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)
62                    (signal 'file-locked (list "File is locked" fn opponent)))))))
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
78 (define-error 'file-supersession "File changed on disk" 'file-error) ; XEmacs
79
80 (defun ask-user-about-supersession-threat-minibuf (fn)
81   (save-window-excursion
82     (let (answer)
83       (while (null answer)
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))
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                                     (?r . revert)
95                                     (?? . help))))))
96           (cond ((null answer)
97                  (beep)
98                  (message "Please type y, n or r; or ? for help")
99                  (sit-for 3))
100                 ((eq answer 'help)
101                  (ask-user-about-supersession-help)
102                  (setq answer nil))
103                 ((eq answer 'revert)
104                  (revert-buffer nil (not (buffer-modified-p)))
105                  ; ask confirmation iff buffer modified
106                  (signal 'file-supersession
107                          (list "File reverted" fn)))
108                 ((eq answer 'yield)
109                  (signal 'file-supersession
110                          (list "File changed on disk" fn))))))
111       (message
112         "File on disk now will become a backup file if you save these changes.")
113       (setq buffer-backed-up nil))))
114
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.
119
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.
125
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.")
128     (save-excursion
129       (set-buffer standard-output)
130       (help-mode))))
131 \f
132 ;;; dialog-box versions [XEmacs]
133
134 (defun ask-user-about-lock-dbox (fn opponent)
135   (let ((echo-keystrokes 0)
136         (dbox
137          (cons
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."
141                   opponent fn)
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"
144              proceed t]
145             nil
146             ["Abort\n\nDon't modify the buffer\n" yield t]))))
147     (popup-dialog-box dbox)
148     (catch 'aual-done
149       (while t
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))
159                  (signal 'quit nil))
160                 ((button-release-event-p event) ;; don't beep twice
161                  nil)
162                 (t
163                  (beep)
164                  (message "please answer the dialog box"))))))))
165
166 (defun ask-user-about-supersession-threat-dbox (fn)
167   (let ((echo-keystrokes 0)
168         (dbox
169          (cons
170           (format "File %s has changed on disk
171 since its buffer was last read in or saved.
172
173 Do you really want to edit the buffer? " fn)
174           '(["Yes\n\nEdit the buffer anyway,\nignoring the disk file"
175              proceed t]
176             ["No\n\nDon't modify the buffer\n" yield t]
177             nil
178             ["No\n\nDon't modify the buffer\nbut revert it" revert t]
179             ))))
180     (popup-dialog-box dbox)
181     (catch 'auast-done
182       (while t
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))
190                      (error
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))
197                  (signal 'quit nil))
198                 ((button-release-event-p event) ;; don't beep twice
199                  nil)
200                 (t
201                  (beep)
202                  (message "please answer the dialog box"))))))))
203
204 \f
205 ;;; top-level
206
207 ;;;###autoload
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."
216   (discard-input)
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)))
223
224 ;;;###autoload
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.
230
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."
233   (discard-input)
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)))
240
241 ;;; userlock.el ends here