userlock.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ;;; userlock.el --- handle file access contention between multiple users
  2. ;; Copyright (C) 1985-1986, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: internal
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; 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. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This file is autoloaded to handle certain conditions
  19. ;; detected by the file-locking code within Emacs.
  20. ;; The two entry points are `ask-user-about-lock' and
  21. ;; `ask-user-about-supersession-threat'.
  22. ;;; Code:
  23. (put 'file-locked 'error-conditions '(file-locked file-error error))
  24. (put 'file-locked 'error-message "File is locked")
  25. ;;;###autoload
  26. (defun ask-user-about-lock (file opponent)
  27. "Ask user what to do when he wants to edit FILE but it is locked by OPPONENT.
  28. This function has a choice of three things to do:
  29. do (signal 'file-locked (list FILE OPPONENT))
  30. to refrain from editing the file
  31. return t (grab the lock on the file)
  32. return nil (edit the file even though it is locked).
  33. You can redefine this function to choose among those three alternatives
  34. in any way you like."
  35. (discard-input)
  36. (save-window-excursion
  37. (let (answer short-opponent short-file)
  38. (setq short-file
  39. (if (> (length file) 22)
  40. (concat "..." (substring file (- (length file) 22)))
  41. file))
  42. (setq short-opponent
  43. (if (> (length opponent) 25)
  44. (save-match-data
  45. (string-match " (pid [0-9]+)" opponent)
  46. (concat (substring opponent 0 13) "..."
  47. (match-string 0 opponent)))
  48. opponent))
  49. (while (null answer)
  50. (message "%s locked by %s: (s, q, p, ?)? "
  51. short-file short-opponent)
  52. (let ((tem (let ((inhibit-quit t)
  53. (cursor-in-echo-area t))
  54. (prog1 (downcase (read-char))
  55. (setq quit-flag nil)))))
  56. (if (= tem help-char)
  57. (ask-user-about-lock-help)
  58. (setq answer (assoc tem '((?s . t)
  59. (?q . yield)
  60. (?\C-g . yield)
  61. (?p . nil)
  62. (?? . help))))
  63. (cond ((null answer)
  64. (beep)
  65. (message "Please type q, s, or p; or ? for help")
  66. (sit-for 3))
  67. ((eq (cdr answer) 'help)
  68. (ask-user-about-lock-help)
  69. (setq answer nil))
  70. ((eq (cdr answer) 'yield)
  71. (signal 'file-locked (list file opponent)))))))
  72. (cdr answer))))
  73. (defun ask-user-about-lock-help ()
  74. (with-output-to-temp-buffer "*Help*"
  75. (princ "It has been detected that you want to modify a file that someone else has
  76. already started modifying in Emacs.
  77. You can <s>teal the file; the other user becomes the
  78. intruder if (s)he ever unmodifies the file and then changes it again.
  79. You can <p>roceed; you edit at your own (and the other user's) risk.
  80. You can <q>uit; don't modify this file.")
  81. (with-current-buffer standard-output
  82. (help-mode))))
  83. (put
  84. 'file-supersession 'error-conditions '(file-supersession file-error error))
  85. ;;;###autoload
  86. (defun ask-user-about-supersession-threat (fn)
  87. "Ask a user who is about to modify an obsolete buffer what to do.
  88. This function has two choices: it can return, in which case the modification
  89. of the buffer will proceed, or it can (signal 'file-supersession (file)),
  90. in which case the proposed buffer modification will not be made.
  91. You can rewrite this to use any criterion you like to choose which one to do.
  92. The buffer in question is current when this function is called."
  93. (discard-input)
  94. (save-window-excursion
  95. (let (answer)
  96. (while (null answer)
  97. (message "%s changed on disk; really edit the buffer? (y, n, r or C-h) "
  98. (file-name-nondirectory fn))
  99. (let ((tem (downcase (let ((cursor-in-echo-area t))
  100. (read-char-exclusive)))))
  101. (setq answer
  102. (if (= tem help-char)
  103. 'help
  104. (cdr (assoc tem '((?n . yield)
  105. (?\C-g . yield)
  106. (?y . proceed)
  107. (?r . revert)
  108. (?? . help))))))
  109. (cond ((null answer)
  110. (beep)
  111. (message "Please type y, n or r; or ? for help")
  112. (sit-for 3))
  113. ((eq answer 'help)
  114. (ask-user-about-supersession-help)
  115. (setq answer nil))
  116. ((eq answer 'revert)
  117. (revert-buffer nil (not (buffer-modified-p)))
  118. ; ask confirmation if buffer modified
  119. (signal 'file-supersession
  120. (list "File reverted" fn)))
  121. ((eq answer 'yield)
  122. (signal 'file-supersession
  123. (list "File changed on disk" fn))))))
  124. (message
  125. "File on disk now will become a backup file if you save these changes.")
  126. (setq buffer-backed-up nil))))
  127. (defun ask-user-about-supersession-help ()
  128. (with-output-to-temp-buffer "*Help*"
  129. (princ "You want to modify a buffer whose disk file has changed
  130. since you last read it in or saved it with this buffer.
  131. If you say `y' to go ahead and modify this buffer,
  132. you risk ruining the work of whoever rewrote the file.
  133. If you say `r' to revert, the contents of the buffer are refreshed
  134. from the file on disk.
  135. If you say `n', the change you started to make will be aborted.
  136. Usually, you should type `n' and then `M-x revert-buffer',
  137. to get the latest version of the file, then make the change again.")
  138. (with-current-buffer standard-output
  139. (help-mode))))
  140. ;;; userlock.el ends here