emacs-lock.el 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. ;;; emacs-lock.el --- protect buffers against killing or exiting -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2011-2017 Free Software Foundation, Inc.
  3. ;; Author: Juanma Barranquero <lekktu@gmail.com>
  4. ;; Inspired by emacs-lock.el by Tom Wurgler <twurgler@goodyear.com>
  5. ;; Maintainer: emacs-devel@gnu.org
  6. ;; Keywords: extensions, processes
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This package defines a minor mode Emacs Lock to mark a buffer as
  20. ;; protected against accidental killing, or exiting Emacs, or both.
  21. ;; Buffers associated with inferior modes, like shell or telnet, can
  22. ;; be treated specially, by auto-unlocking them if their inferior
  23. ;; processes are dead.
  24. ;;; Code:
  25. (defgroup emacs-lock nil
  26. "Emacs-Lock mode."
  27. :version "24.1"
  28. :group 'convenience)
  29. (defcustom emacs-lock-default-locking-mode 'all
  30. "Default locking mode of Emacs-Locked buffers.
  31. Its value is used as the default for `emacs-lock-mode' (which
  32. see) the first time that Emacs Lock mode is turned on in a buffer
  33. without passing an explicit locking mode.
  34. Possible values are:
  35. exit -- Emacs cannot exit while the buffer is locked
  36. kill -- the buffer cannot be killed, but Emacs can exit as usual
  37. all -- the buffer is locked against both actions
  38. nil -- the buffer is not locked"
  39. :type '(choice
  40. (const :tag "Do not allow Emacs to exit" exit)
  41. (const :tag "Do not allow killing the buffer" kill)
  42. (const :tag "Do not allow killing the buffer or exiting Emacs" all)
  43. (const :tag "Do not lock the buffer" nil))
  44. :group 'emacs-lock
  45. :version "24.1")
  46. ;; Note: as auto-unlocking can lead to data loss, it would be better
  47. ;; to default to nil; but the value below is for compatibility with
  48. ;; the old emacs-lock.el.
  49. (defcustom emacs-lock-unlockable-modes '((shell-mode . all)
  50. (telnet-mode . all))
  51. "Alist of auto-unlockable modes.
  52. Each element is a pair (MAJOR-MODE . ACTION), where ACTION is
  53. one of `kill', `exit' or `all'. Buffers with matching major
  54. modes are auto-unlocked for the specific action if their
  55. inferior processes are not alive. If this variable is t, all
  56. buffers associated to inferior processes are auto-unlockable
  57. for both actions (NOT RECOMMENDED)."
  58. :type '(choice
  59. (const :tag "All buffers with inferior processes" t)
  60. (repeat :tag "Selected modes"
  61. (cons :tag "Set auto-unlock for"
  62. (symbol :tag "Major mode")
  63. (radio
  64. (const :tag "Allow exiting" exit)
  65. (const :tag "Allow killing" kill)
  66. (const :tag "Allow both" all)))))
  67. :group 'emacs-lock
  68. :version "24.1")
  69. (defcustom emacs-lock-locked-buffer-functions nil
  70. "Abnormal hook run when Emacs Lock prevents exiting Emacs, or killing a buffer.
  71. The functions get one argument, the first locked buffer found."
  72. :type 'hook
  73. :group 'emacs-lock
  74. :version "24.3")
  75. (defvar-local emacs-lock-mode nil
  76. "If non-nil, the current buffer is locked.
  77. It can be one of the following values:
  78. exit -- Emacs cannot exit while the buffer is locked
  79. kill -- the buffer cannot be killed, but Emacs can exit as usual
  80. all -- the buffer is locked against both actions
  81. nil -- the buffer is not locked")
  82. (put 'emacs-lock-mode 'permanent-local t)
  83. (defvar-local emacs-lock--old-mode nil
  84. "Most recent locking mode set on the buffer.
  85. Internal use only.")
  86. (put 'emacs-lock--old-mode 'permanent-local t)
  87. (defvar-local emacs-lock--try-unlocking nil
  88. "Non-nil if current buffer should be checked for auto-unlocking.
  89. Internal use only.")
  90. (put 'emacs-lock--try-unlocking 'permanent-local t)
  91. (defun emacs-lock-live-process-p (buffer-or-name)
  92. "Return t if BUFFER-OR-NAME is associated with a live process."
  93. (process-live-p (get-buffer-process buffer-or-name)))
  94. (defun emacs-lock--can-auto-unlock (action)
  95. "Return t if the current buffer can auto-unlock for ACTION.
  96. ACTION must be one of `kill' or `exit'.
  97. See `emacs-lock-unlockable-modes'."
  98. (and emacs-lock--try-unlocking
  99. (not (emacs-lock-live-process-p (current-buffer)))
  100. (or (eq emacs-lock-unlockable-modes t)
  101. (let ((unlock (cdr (assq major-mode emacs-lock-unlockable-modes))))
  102. (or (eq unlock 'all) (eq unlock action))))))
  103. (defun emacs-lock--exit-locked-buffer ()
  104. "Return the first exit-locked buffer found."
  105. (save-current-buffer
  106. (catch :found
  107. (dolist (buffer (buffer-list))
  108. (set-buffer buffer)
  109. (unless (or (emacs-lock--can-auto-unlock 'exit)
  110. (memq emacs-lock-mode '(nil kill)))
  111. (throw :found buffer)))
  112. nil)))
  113. (defun emacs-lock--kill-emacs-hook ()
  114. "Signal an error if any buffer is exit-locked.
  115. Used from `kill-emacs-hook' (which see)."
  116. (let ((locked (emacs-lock--exit-locked-buffer)))
  117. (when locked
  118. (run-hook-with-args 'emacs-lock-locked-buffer-functions locked)
  119. (error "Emacs cannot exit because buffer %S is locked"
  120. (buffer-name locked)))))
  121. (defun emacs-lock--kill-emacs-query-functions ()
  122. "Display a message if any buffer is exit-locked.
  123. Return a value appropriate for `kill-emacs-query-functions' (which see)."
  124. (let ((locked (emacs-lock--exit-locked-buffer)))
  125. (if (not locked)
  126. t
  127. (run-hook-with-args 'emacs-lock-locked-buffer-functions locked)
  128. (message "Emacs cannot exit because buffer %S is locked"
  129. (buffer-name locked))
  130. nil)))
  131. (defun emacs-lock--kill-buffer-query-functions ()
  132. "Display a message if the current buffer is kill-locked.
  133. Return a value appropriate for `kill-buffer-query-functions' (which see)."
  134. (if (or (emacs-lock--can-auto-unlock 'kill)
  135. (memq emacs-lock-mode '(nil exit)))
  136. t
  137. (run-hook-with-args 'emacs-lock-locked-buffer-functions (current-buffer))
  138. (message "Buffer %S is locked and cannot be killed" (buffer-name))
  139. nil))
  140. (defun emacs-lock--set-mode (mode arg)
  141. "Setter function for `emacs-lock-mode'."
  142. (setq emacs-lock-mode
  143. (cond ((memq arg '(all exit kill))
  144. ;; explicit locking mode arg, use it
  145. arg)
  146. ((and (eq arg current-prefix-arg) (consp current-prefix-arg))
  147. ;; called with C-u M-x emacs-lock-mode, so ask the user
  148. (intern (completing-read "Locking mode: "
  149. '("all" "exit" "kill")
  150. nil t nil nil
  151. (symbol-name
  152. emacs-lock-default-locking-mode))))
  153. ((eq mode t)
  154. ;; turn on, so use previous setting, or customized default
  155. (or emacs-lock--old-mode emacs-lock-default-locking-mode))
  156. (t
  157. ;; anything else (turn off)
  158. mode))))
  159. (define-obsolete-variable-alias 'emacs-lock-from-exiting
  160. 'emacs-lock-mode "24.1")
  161. ;;;###autoload
  162. (define-minor-mode emacs-lock-mode
  163. "Toggle Emacs Lock mode in the current buffer.
  164. If called with a plain prefix argument, ask for the locking mode
  165. to be used. With any other prefix ARG, turn mode on if ARG is
  166. positive, off otherwise. If called from Lisp, enable the mode if
  167. ARG is omitted or nil.
  168. Initially, if the user does not pass an explicit locking mode, it
  169. defaults to `emacs-lock-default-locking-mode' (which see);
  170. afterwards, the locking mode most recently set on the buffer is
  171. used instead.
  172. When called from Elisp code, ARG can be any locking mode:
  173. exit -- Emacs cannot exit while the buffer is locked
  174. kill -- the buffer cannot be killed, but Emacs can exit as usual
  175. all -- the buffer is locked against both actions
  176. Other values are interpreted as usual."
  177. :init-value nil
  178. :lighter (""
  179. (emacs-lock--try-unlocking " locked:" " Locked:")
  180. (:eval (symbol-name emacs-lock-mode)))
  181. :group 'emacs-lock
  182. :variable (emacs-lock-mode .
  183. (lambda (mode)
  184. (emacs-lock--set-mode mode arg)))
  185. (when emacs-lock-mode
  186. (setq emacs-lock--old-mode emacs-lock-mode)
  187. (setq emacs-lock--try-unlocking
  188. (and (if (eq emacs-lock-unlockable-modes t)
  189. (emacs-lock-live-process-p (current-buffer))
  190. (assq major-mode emacs-lock-unlockable-modes))
  191. t))))
  192. (unless noninteractive
  193. (add-hook 'kill-buffer-query-functions 'emacs-lock--kill-buffer-query-functions)
  194. ;; We set a hook in both kill-emacs-hook and kill-emacs-query-functions because
  195. ;; we really want to use k-e-q-f to stop as soon as possible, but don't want to
  196. ;; be caught by surprise if someone calls `kill-emacs' instead.
  197. (add-hook 'kill-emacs-hook 'emacs-lock--kill-emacs-hook)
  198. (add-hook 'kill-emacs-query-functions 'emacs-lock--kill-emacs-query-functions))
  199. (defun emacs-lock-unload-function ()
  200. "Unload the Emacs Lock library."
  201. (catch :continue
  202. (dolist (buffer (buffer-list))
  203. (set-buffer buffer)
  204. (when emacs-lock-mode
  205. (if (y-or-n-p (format "Buffer %S is locked, unlock it? " (buffer-name)))
  206. (emacs-lock-mode -1)
  207. (message "Unloading of feature `emacs-lock' aborted.")
  208. (throw :continue t))))
  209. ;; continue standard unloading
  210. nil))
  211. ;;; Compatibility
  212. (defun toggle-emacs-lock ()
  213. "Toggle `emacs-lock-from-exiting' for the current buffer."
  214. (declare (obsolete emacs-lock-mode "24.1"))
  215. (interactive)
  216. (call-interactively 'emacs-lock-mode))
  217. (provide 'emacs-lock)
  218. ;;; emacs-lock.el ends here