*** empty log message ***
[bpt/emacs.git] / lisp / userlock.el
CommitLineData
a2535589
JA
1;; Copyright (C) 1985, 1986 Free Software Foundation, inc.
2
3;; This file is part of GNU Emacs.
4
5;; GNU Emacs is free software; you can redistribute it and/or modify
6;; it under the terms of the GNU General Public License as published by
7;; the Free Software Foundation; either version 1, or (at your option)
8;; any later version.
9
10;; GNU Emacs is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13;; GNU General Public License for more details.
14
15;; You should have received a copy of the GNU General Public License
16;; along with GNU Emacs; see the file COPYING. If not, write to
17;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18
19
20;; This file is autloaded to handle certain conditions
21;; detected by the file-locking code within Emacs.
22;; The two entry points are `ask-user-about-lock' and
23;; `ask-user-about-supersession-threat'.
24
25
26(put 'file-locked 'error-conditions '(file-locked file-error error))
27
f9f9507e 28;;;###autoload
a2535589
JA
29(defun ask-user-about-lock (fn opponent)
30 "Ask user what to do when he wants to edit FILE but it is locked by USER.
31This function has a choice of three things to do:
32 do (signal 'buffer-file-locked (list FILE USER))
33 to refrain from editing the file
34 return t (grab the lock on the file)
35 return nil (edit the file even though it is locked).
36You can rewrite it to use any criterion you like to choose which one to do."
37 (discard-input)
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 "File is locked" 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
67already started modifying in EMACS.
68
69You can <s>teal the file; The other user becomes the
70 intruder if (s)he ever unmodifies the file and then changes it again.
71You can <p>roceed; you edit at your own (and the other user's) risk.
72You can <q>uit; don't modify this file.")))
73
74(put
75 'file-supersession 'error-conditions '(file-supersession file-error error))
76
f9f9507e 77;;;###autoload
a2535589
JA
78(defun ask-user-about-supersession-threat (fn)
79 "Ask a user who is about to modify an obsolete buffer what to do.
80This function has two choices: it can return, in which case the modification
81of the buffer will proceed, or it can (signal 'file-supersession (file)),
82in which case the proposed buffer modification will not be made.
83
84You can rewrite this to use any criterion you like to choose which one to do.
85The buffer in question is current when this function is called."
86 (discard-input)
87 (save-window-excursion
88 (let (answer)
89 (while (null answer)
90 (message "File has changed on disk; really want to edit the buffer? (y, n or C-h) ")
91 (let ((tem (downcase (let ((cursor-in-echo-area t))
92 (read-char)))))
93 (setq answer
94 (if (= tem help-char)
95 'help
96 (cdr (assoc tem '((?n . yield)
97 (?\C-g . yield)
98 (?y . proceed)
99 (?? . help))))))
100 (cond ((null answer)
101 (beep)
102 (message "Please type y or n; or ? for help")
103 (sit-for 3))
104 ((eq answer 'help)
105 (ask-user-about-supersession-help)
106 (setq answer nil))
107 ((eq answer 'yield)
108 (signal 'file-supersession
109 (list "File changed on disk" fn))))))
110 (message
111 "File on disk now will become a backup file if you save these changes.")
112 (setq buffer-backed-up nil))))
113
114(defun ask-user-about-supersession-help ()
115 (with-output-to-temp-buffer "*Help*"
116 (princ "You want to modify a buffer whose disk file has changed
117since you last read it in or saved it with this buffer.
118
119If you say `y' to go ahead and modify this buffer,
120you risk ruining the work of whoever rewrote the file.
121If you say `n', the change you started to make will be aborted.
122
123Usually, you should type `n' and then `M-x revert-buffer',
124to get the latest version of the file, then make the change again.")))
125
126