home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / prim / userlock.el < prev    next >
Encoding:
Text File  |  1995-06-30  |  7.6 KB  |  222 lines

  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.28.
  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. (put 'file-locked 'error-conditions '(file-locked file-error 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 "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
  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.  
  74. (put
  75.  'file-supersession 'error-conditions '(file-supersession file-error error))
  76.  
  77. (defun ask-user-about-supersession-threat-minibuf (fn)
  78.   (save-window-excursion
  79.     (let (answer)
  80.       (while (null answer)
  81.     (message "%s changed on disk; really edit the buffer? (y, n or C-h) "
  82.                  (file-name-nondirectory fn))
  83.     (let ((tem (downcase (let ((cursor-in-echo-area t))
  84.                    (read-char)))))
  85.       (setq answer
  86.         (if (= tem help-char)
  87.             'help
  88.           (cdr (assoc tem '((?n . yield)
  89.                     (?\C-g . yield)
  90.                     (?y . proceed)
  91.                     (?? . help))))))
  92.       (cond ((null answer)
  93.          (beep)
  94.          (message "Please type y or n; or ? for help")
  95.          (sit-for 3))
  96.         ((eq answer 'help)
  97.          (ask-user-about-supersession-help)
  98.          (setq answer nil))
  99.         ((eq answer 'yield)
  100.          (signal 'file-supersession
  101.              (list "File changed on disk" fn))))))
  102.       (message
  103.         "File on disk now will become a backup file if you save these changes.")
  104.       (setq buffer-backed-up nil))))
  105.  
  106. (defun ask-user-about-supersession-help ()
  107.   (with-output-to-temp-buffer "*Help*"
  108.     (princ "You want to modify a buffer whose disk file has changed
  109. since you last read it in or saved it with this buffer.
  110.  
  111. If you say `y' to go ahead and modify this buffer,
  112. you risk ruining the work of whoever rewrote the file.
  113. If you say `n', the change you started to make will be aborted.
  114.  
  115. Usually, you should type `n' and then `M-x revert-buffer',
  116. to get the latest version of the file, then make the change again.")))
  117.  
  118.  
  119.  
  120. ;;; dialog-box versions
  121.  
  122. (defun ask-user-about-lock-dbox (fn opponent)
  123.   (let ((echo-keystrokes 0)
  124.     (dbox
  125.      (cons
  126.       (format "%s is locking %s\n
  127.     It has been detected that you want to modify a file that
  128.     someone else has already started modifying in XEmacs."
  129.           opponent fn)
  130.       '(["Steal Lock\n\nThe other user will\nbecome the intruder" steal t]
  131.         ["Proceed\n\nEdit file at your own\n\(and the other user's) risk"
  132.          proceed t]
  133.         nil
  134.         ["Abort\n\nDon't modify the buffer\n" yield t]))))
  135.     (popup-dialog-box dbox)
  136.     (catch 'aual-done
  137.       (while t
  138.     (let ((event (next-command-event)))
  139.       (cond ((and (misc-user-event-p event) (eq (event-object event) 'proceed))
  140.          (throw 'aual-done nil))
  141.         ((and (misc-user-event-p event) (eq (event-object event) 'steal))
  142.          (throw 'aual-done t))
  143.         ((and (misc-user-event-p event) (eq (event-object event) 'yield))
  144.          (signal 'file-locked (list "File is locked" fn opponent)))
  145.         ((button-release-event-p event) ;; don't beep twice
  146.          nil)
  147.         (t
  148.          (beep)
  149.          (message "please answer the dialog box"))))))))
  150.  
  151. (defun ask-user-about-supersession-threat-dbox (fn)
  152.   (let ((echo-keystrokes 0)
  153.     (dbox
  154.      (cons
  155.       (format "File %s has changed on disk
  156. since its buffer was last read in or saved.
  157.  
  158. Do you really want to edit the buffer? " fn)
  159.       '(["Yes\n\nEdit the buffer anyway,\nignoring the disk file"
  160.          proceed t]
  161.         ["No\n\nDon't modify the buffer\n" yield t]
  162.         nil
  163.         ["No\n\nDon't modify the buffer\nbut revert it" revert t]
  164.         ))))
  165.     (popup-dialog-box dbox)
  166.     (catch 'auast-done
  167.       (while t
  168.     (let ((event (next-command-event)))
  169.       (cond ((and (misc-user-event-p event) (eq (event-object event) 'proceed))
  170.          (throw 'auast-done nil))
  171.         ((and (misc-user-event-p event) (eq (event-object event) 'yield))
  172.          (signal 'file-supersession (list "File changed on disk" fn)))
  173.         ((and (misc-user-event-p event) (eq (event-object event) 'revert))
  174.          (or (equal fn (buffer-file-name))
  175.              (error
  176.               "ask-user-about-supersession-threat called bogusly"))
  177.          (revert-buffer nil t)
  178.          (signal 'file-supersession
  179.              (list "File changed on disk; reverted" fn)))
  180.         ((button-release-event-p event) ;; don't beep twice
  181.          nil)
  182.         (t
  183.          (beep)
  184.          (message "please answer the dialog box"))))))))
  185.  
  186.  
  187. ;;; top-level
  188.  
  189. ;;;###autoload
  190. (defun ask-user-about-lock (fn opponent)
  191.   "Ask user what to do when he wants to edit FILE but it is locked by USER.
  192. This function has a choice of three things to do:
  193.   do (signal 'buffer-file-locked (list FILE USER))
  194.     to refrain from editing the file
  195.   return t (grab the lock on the file)
  196.   return nil (edit the file even though it is locked).
  197. You can rewrite it to use any criterion you like to choose which one to do."
  198.   (discard-input)
  199.   (if (and (fboundp 'popup-dialog-box)
  200.        (or (button-press-event-p last-command-event)
  201.            (button-release-event-p last-command-event)
  202.            (misc-user-event-p last-command-event)))
  203.       (ask-user-about-lock-dbox fn opponent)
  204.     (ask-user-about-lock-minibuf fn opponent)))
  205.  
  206. ;;;###autoload
  207. (defun ask-user-about-supersession-threat (fn)
  208.   "Ask a user who is about to modify an obsolete buffer what to do.
  209. This function has two choices: it can return, in which case the modification
  210. of the buffer will proceed, or it can (signal 'file-supersession (file)),
  211. in which case the proposed buffer modification will not be made.
  212.  
  213. You can rewrite this to use any criterion you like to choose which one to do.
  214. The buffer in question is current when this function is called."
  215.   (discard-input)
  216.   (if (and (fboundp 'popup-dialog-box)
  217.        (or (button-press-event-p last-command-event)
  218.            (button-release-event-p last-command-event)
  219.            (misc-user-event-p last-command-event)))
  220.       (ask-user-about-supersession-threat-dbox fn)
  221.     (ask-user-about-supersession-threat-minibuf fn)))
  222.