userlock.el 5.5 KB

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