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