userlock.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ;; Copyright (C) 1985, 1986 Free Software Foundation, inc.
  2. ;; This file is part of GNU Emacs.
  3. ;; GNU Emacs is distributed in the hope that it will be useful,
  4. ;; but WITHOUT ANY WARRANTY. No author or distributor
  5. ;; accepts responsibility to anyone for the consequences of using it
  6. ;; or for whether it serves any particular purpose or works at all,
  7. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  8. ;; License for full details.
  9. ;; Everyone is granted permission to copy, modify and redistribute
  10. ;; GNU Emacs, but only under the conditions described in the
  11. ;; GNU Emacs General Public License. A copy of this license is
  12. ;; supposed to have been given to you along with GNU Emacs so you
  13. ;; can know your rights and responsibilities. It should be in a
  14. ;; file named COPYING. Among other things, the copyright notice
  15. ;; and this notice must be preserved on all copies.
  16. ;; This file is autloaded to handle certain conditions
  17. ;; detected by the file-locking code within Emacs.
  18. ;; The two entry points are `ask-user-about-lock' and
  19. ;; `ask-user-about-supersession-threat'.
  20. (put 'file-locked 'error-conditions '(file-locked file-error error))
  21. (defun ask-user-about-lock (fn opponent)
  22. "Ask user what to do when he wants to edit FILE but it is locked by USER.
  23. This function has a choice of three things to do:
  24. do (signal 'buffer-file-locked (list FILE USER))
  25. to refrain from editing the file
  26. return t (grab the lock on the file)
  27. return nil (edit the file even though it is locked).
  28. You can rewrite it to use any criterion you like to choose which one to do."
  29. (discard-input)
  30. (save-window-excursion
  31. (let (answer)
  32. (while (null answer)
  33. (message "%s is locking %s: action (s, q, p, ?)? " opponent fn)
  34. (let ((tem (let ((inhibit-quit t)
  35. (cursor-in-echo-area t))
  36. (prog1 (downcase (read-char))
  37. (setq quit-flag nil)))))
  38. (if (= tem help-char)
  39. (ask-user-about-lock-help)
  40. (setq answer (assoc tem '((?s . t)
  41. (?q . yield)
  42. (?\C-g . yield)
  43. (?p . nil)
  44. (?? . help))))
  45. (cond ((null answer)
  46. (beep)
  47. (message "Please type q, s, or p; or ? for help")
  48. (sit-for 3))
  49. ((eq (cdr answer) 'help)
  50. (ask-user-about-lock-help)
  51. (setq answer nil))
  52. ((eq (cdr answer) 'yield)
  53. (signal 'file-locked (list "File is locked" fn opponent)))))))
  54. (cdr answer))))
  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. You can <s>teal the file; The other user becomes the
  60. intruder if (s)he ever unmodifies the file and then changes it again.
  61. You can <p>roceed; you edit at your own (and the other user's) risk.
  62. You can <q>uit; don't modify this file.")))
  63. (put
  64. 'file-supersession 'error-conditions '(file-supersession file-error error))
  65. (defun ask-user-about-supersession-threat (fn)
  66. "Ask a user who is about to modify an obsolete buffer what to do.
  67. This function has two choices: it can return, in which case the modification
  68. of the buffer will proceed, or it can (signal 'file-supersession (file)),
  69. in which case the proposed buffer modification will not be made.
  70. You can rewrite this to use any criterion you like to choose which one to do."
  71. (discard-input)
  72. (save-window-excursion
  73. (let (answer)
  74. (while (null answer)
  75. (message "File has changed on disk; really want to edit the buffer? (y, n or C-h) ")
  76. (let ((tem (downcase (let ((cursor-in-echo-area t))
  77. (read-char)))))
  78. (setq answer
  79. (if (= tem help-char)
  80. 'help
  81. (cdr (assoc tem '((?n . yield)
  82. (?\C-g . yield)
  83. (?y . proceed)
  84. (?? . help))))))
  85. (cond ((null answer)
  86. (beep)
  87. (message "Please type y or n; or ? for help")
  88. (sit-for 3))
  89. ((eq answer 'help)
  90. (ask-user-about-supersession-help)
  91. (setq answer nil))
  92. ((eq answer 'yield)
  93. (signal 'file-supersession
  94. (list "File changed on disk" fn))))))
  95. (message
  96. "File on disk now will become a backup file if you save these changes.")
  97. (setq buffer-backed-up nil))))
  98. (defun ask-user-about-supersession-help ()
  99. (with-output-to-temp-buffer "*Help*"
  100. (princ "It has been detected that you want to modify a buffer
  101. that is obsolete because changes in the file on disk have happened
  102. since you read it or wrote it with this buffer.
  103. If you say `y' to go ahead and modify this buffer,
  104. you risk ruining the work of whoever rewrote the file.
  105. If you say `n', whatever change you started to make in the buffer
  106. will not take place.
  107. You might consider answering `n', running `M-x revert-buffer' to
  108. bring the text in Emacs into accord with what is on disk, and then
  109. making the change again.")))