home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / prim / userlock.el < prev    next >
Encoding:
Text File  |  1992-12-29  |  7.5 KB  |  211 lines

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