old-emacs-lock.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ;;; emacs-lock.el --- prevents you from exiting Emacs if a buffer is locked
  2. ;; Copyright (C) 1994, 1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Tom Wurgler <twurgler@goodyear.com>
  4. ;; Created: 12/8/94
  5. ;; Keywords: extensions, processes
  6. ;; Obsolete-since: 24.1
  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 code sets a buffer-local variable to t if toggle-emacs-lock is run,
  20. ;; then if the user attempts to exit Emacs, the locked buffer name will be
  21. ;; displayed and the exit aborted. This is just a way of protecting
  22. ;; yourself from yourself. For example, if you have a shell running a big
  23. ;; program and exiting Emacs would abort that program, you may want to lock
  24. ;; that buffer, then if you forget about it after a while, you won't
  25. ;; accidentally exit Emacs. To unlock the buffer, just goto the buffer and
  26. ;; run toggle-emacs-lock again.
  27. ;;; Code:
  28. (defvar emacs-lock-from-exiting nil
  29. "Whether Emacs is locked to prevent exiting. See `check-emacs-lock'.")
  30. (make-variable-buffer-local 'emacs-lock-from-exiting)
  31. (defvar emacs-lock-buffer-locked nil
  32. "Whether a shell or telnet buffer was locked when its process was killed.")
  33. (make-variable-buffer-local 'emacs-lock-buffer-locked)
  34. (put 'emacs-lock-buffer-locked 'permanent-local t)
  35. (defun check-emacs-lock ()
  36. "Check if variable `emacs-lock-from-exiting' is t for any buffer.
  37. If any locked buffer is found, signal error and display the buffer's name."
  38. (save-excursion
  39. (dolist (buffer (buffer-list))
  40. (set-buffer buffer)
  41. (when emacs-lock-from-exiting
  42. (error "Emacs is locked from exit due to buffer: %s" (buffer-name))))))
  43. (defun toggle-emacs-lock ()
  44. "Toggle `emacs-lock-from-exiting' for the current buffer.
  45. See `check-emacs-lock'."
  46. (interactive)
  47. (setq emacs-lock-from-exiting (not emacs-lock-from-exiting))
  48. (if emacs-lock-from-exiting
  49. (message "Buffer is now locked")
  50. (message "Buffer is now unlocked")))
  51. (defun emacs-lock-check-buffer-lock ()
  52. "Check if variable `emacs-lock-from-exiting' is t for a buffer.
  53. If the buffer is locked, signal error and display its name."
  54. (when emacs-lock-from-exiting
  55. (error "Buffer `%s' is locked, can't delete it" (buffer-name))))
  56. ; These next defuns make it so if you exit a shell that is locked, the lock
  57. ; is shut off for that shell so you can exit Emacs. Same for telnet.
  58. ; Also, if a shell or a telnet buffer was locked and the process killed,
  59. ; turn the lock back on again if the process is restarted.
  60. (defun emacs-lock-shell-sentinel ()
  61. (set-process-sentinel
  62. (get-buffer-process (buffer-name)) (function emacs-lock-clear-sentinel)))
  63. (defun emacs-lock-clear-sentinel (_proc _str)
  64. (if emacs-lock-from-exiting
  65. (progn
  66. (setq emacs-lock-from-exiting nil)
  67. (setq emacs-lock-buffer-locked t)
  68. (message "Buffer is now unlocked"))
  69. (setq emacs-lock-buffer-locked nil)))
  70. (defun emacs-lock-was-buffer-locked ()
  71. (if emacs-lock-buffer-locked
  72. (setq emacs-lock-from-exiting t)))
  73. (unless noninteractive
  74. (add-hook 'kill-emacs-hook 'check-emacs-lock))
  75. (add-hook 'kill-buffer-hook 'emacs-lock-check-buffer-lock)
  76. (add-hook 'shell-mode-hook 'emacs-lock-was-buffer-locked)
  77. (add-hook 'shell-mode-hook 'emacs-lock-shell-sentinel)
  78. (add-hook 'telnet-mode-hook 'emacs-lock-was-buffer-locked)
  79. (add-hook 'telnet-mode-hook 'emacs-lock-shell-sentinel)
  80. (provide 'emacs-lock)
  81. ;;; emacs-lock.el ends here