| 1 | ;;; emacs-lock.el --- prevents you from exiting Emacs if a buffer is locked |
| 2 | |
| 3 | ;; Copyright (C) 1994, 1997, 2001-2011 Free Software Foundation, Inc |
| 4 | |
| 5 | ;; Author: Tom Wurgler <twurgler@goodyear.com> |
| 6 | ;; Created: 12/8/94 |
| 7 | ;; Keywords: extensions, processes |
| 8 | |
| 9 | ;; This file is part of GNU Emacs. |
| 10 | |
| 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 12 | ;; it under the terms of the GNU General Public License as published by |
| 13 | ;; the Free Software Foundation, either version 3 of the License, or |
| 14 | ;; (at your option) any later version. |
| 15 | |
| 16 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | ;; GNU General Public License for more details. |
| 20 | |
| 21 | ;; You should have received a copy of the GNU General Public License |
| 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | ;;; Commentary: |
| 25 | |
| 26 | ;; This code sets a buffer-local variable to t if toggle-emacs-lock is run, |
| 27 | ;; then if the user attempts to exit Emacs, the locked buffer name will be |
| 28 | ;; displayed and the exit aborted. This is just a way of protecting |
| 29 | ;; yourself from yourself. For example, if you have a shell running a big |
| 30 | ;; program and exiting Emacs would abort that program, you may want to lock |
| 31 | ;; that buffer, then if you forget about it after a while, you won't |
| 32 | ;; accidentally exit Emacs. To unlock the buffer, just goto the buffer and |
| 33 | ;; run toggle-emacs-lock again. |
| 34 | |
| 35 | ;;; Code: |
| 36 | |
| 37 | (defvar emacs-lock-from-exiting nil |
| 38 | "Whether Emacs is locked to prevent exiting. See `check-emacs-lock'.") |
| 39 | (make-variable-buffer-local 'emacs-lock-from-exiting) |
| 40 | |
| 41 | (defvar emacs-lock-buffer-locked nil |
| 42 | "Whether a shell or telnet buffer was locked when its process was killed.") |
| 43 | (make-variable-buffer-local 'emacs-lock-buffer-locked) |
| 44 | (put 'emacs-lock-buffer-locked 'permanent-local t) |
| 45 | |
| 46 | (defun check-emacs-lock () |
| 47 | "Check if variable `emacs-lock-from-exiting' is t for any buffer. |
| 48 | If any locked buffer is found, signal error and display the buffer's name." |
| 49 | (save-excursion |
| 50 | (dolist (buffer (buffer-list)) |
| 51 | (set-buffer buffer) |
| 52 | (when emacs-lock-from-exiting |
| 53 | (error "Emacs is locked from exit due to buffer: %s" (buffer-name)))))) |
| 54 | |
| 55 | (defun toggle-emacs-lock () |
| 56 | "Toggle `emacs-lock-from-exiting' for the current buffer. |
| 57 | See `check-emacs-lock'." |
| 58 | (interactive) |
| 59 | (setq emacs-lock-from-exiting (not emacs-lock-from-exiting)) |
| 60 | (if emacs-lock-from-exiting |
| 61 | (message "Buffer is now locked") |
| 62 | (message "Buffer is now unlocked"))) |
| 63 | |
| 64 | (defun emacs-lock-check-buffer-lock () |
| 65 | "Check if variable `emacs-lock-from-exiting' is t for a buffer. |
| 66 | If the buffer is locked, signal error and display its name." |
| 67 | (when emacs-lock-from-exiting |
| 68 | (error "Buffer `%s' is locked, can't delete it" (buffer-name)))) |
| 69 | |
| 70 | ; These next defuns make it so if you exit a shell that is locked, the lock |
| 71 | ; is shut off for that shell so you can exit Emacs. Same for telnet. |
| 72 | ; Also, if a shell or a telnet buffer was locked and the process killed, |
| 73 | ; turn the lock back on again if the process is restarted. |
| 74 | |
| 75 | (defun emacs-lock-shell-sentinel () |
| 76 | (set-process-sentinel |
| 77 | (get-buffer-process (buffer-name)) (function emacs-lock-clear-sentinel))) |
| 78 | |
| 79 | (defun emacs-lock-clear-sentinel (proc str) |
| 80 | (if emacs-lock-from-exiting |
| 81 | (progn |
| 82 | (setq emacs-lock-from-exiting nil) |
| 83 | (setq emacs-lock-buffer-locked t) |
| 84 | (message "Buffer is now unlocked")) |
| 85 | (setq emacs-lock-buffer-locked nil))) |
| 86 | |
| 87 | (defun emacs-lock-was-buffer-locked () |
| 88 | (if emacs-lock-buffer-locked |
| 89 | (setq emacs-lock-from-exiting t))) |
| 90 | |
| 91 | (unless noninteractive |
| 92 | (add-hook 'kill-emacs-hook 'check-emacs-lock)) |
| 93 | (add-hook 'kill-buffer-hook 'emacs-lock-check-buffer-lock) |
| 94 | (add-hook 'shell-mode-hook 'emacs-lock-was-buffer-locked) |
| 95 | (add-hook 'shell-mode-hook 'emacs-lock-shell-sentinel) |
| 96 | (add-hook 'telnet-mode-hook 'emacs-lock-was-buffer-locked) |
| 97 | (add-hook 'telnet-mode-hook 'emacs-lock-shell-sentinel) |
| 98 | |
| 99 | (provide 'emacs-lock) |
| 100 | |
| 101 | ;;; emacs-lock.el ends here |