1 ;; Copyright (C) 1985, 1986 Free Software Foundation, inc.
3 ;; This file is part of GNU Emacs.
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)
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.
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.
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'.
26 (put 'file-locked
'error-conditions
'(file-locked file-error error
))
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.
31 This 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).
36 You can rewrite it to use any criterion you like to choose which one to do."
38 (save-window-excursion
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
)))))
47 (ask-user-about-lock-help)
48 (setq answer
(assoc tem
'((?s . t
)
55 (message "Please type q, s, or p; or ? for help")
57 ((eq (cdr answer
) 'help
)
58 (ask-user-about-lock-help)
60 ((eq (cdr answer
) 'yield
)
61 (signal 'file-locked
(list "File is locked" fn opponent
)))))))
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.
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.")))
75 'file-supersession
'error-conditions
'(file-supersession file-error error
))
78 (defun ask-user-about-supersession-threat (fn)
79 "Ask a user who is about to modify an obsolete buffer what to do.
80 This function has two choices: it can return, in which case the modification
81 of the buffer will proceed, or it can (signal 'file-supersession (file)),
82 in which case the proposed buffer modification will not be made.
84 You can rewrite this to use any criterion you like to choose which one to do.
85 The buffer in question is current when this function is called."
87 (save-window-excursion
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
))
96 (cdr (assoc tem
'((?n . yield
)
102 (message "Please type y or n; or ? for help")
105 (ask-user-about-supersession-help)
108 (signal 'file-supersession
109 (list "File changed on disk" fn
))))))
111 "File on disk now will become a backup file if you save these changes.")
112 (setq buffer-backed-up nil
))))
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
117 since you last read it in or saved it with this buffer.
119 If you say `y' to go ahead and modify this buffer,
120 you risk ruining the work of whoever rewrote the file.
121 If you say `n', the change you started to make will be aborted.
123 Usually, you should type `n' and then `M-x revert-buffer',
124 to get the latest version of the file, then make the change again.")))