Commit | Line | Data |
---|---|---|
c4667a1a | 1 | ;;; emacs-lock.el --- prevents you from exiting Emacs if a buffer is locked |
b578f267 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1994, 1997, 2001-2011 Free Software Foundation, Inc |
b578f267 | 4 | |
c9e2fc17 RS |
5 | ;; Author: Tom Wurgler <twurgler@goodyear.com> |
6 | ;; Created: 12/8/94 | |
1a2b7f51 | 7 | ;; Keywords: extensions, processes |
b578f267 EN |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
c9e2fc17 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
b578f267 EN |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
c9e2fc17 RS |
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. | |
b578f267 | 20 | |
c9e2fc17 | 21 | ;; You should have received a copy of the GNU General Public License |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
c9e2fc17 RS |
23 | |
24 | ;;; Commentary: | |
b578f267 | 25 | |
c9e2fc17 | 26 | ;; This code sets a buffer-local variable to t if toggle-emacs-lock is run, |
c4667a1a | 27 | ;; then if the user attempts to exit Emacs, the locked buffer name will be |
c9e2fc17 RS |
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 | |
c4667a1a | 30 | ;; program and exiting Emacs would abort that program, you may want to lock |
c9e2fc17 | 31 | ;; that buffer, then if you forget about it after a while, you won't |
c4667a1a | 32 | ;; accidentally exit Emacs. To unlock the buffer, just goto the buffer and |
c9e2fc17 RS |
33 | ;; run toggle-emacs-lock again. |
34 | ||
b578f267 EN |
35 | ;;; Code: |
36 | ||
0333a1cc | 37 | (defvar emacs-lock-from-exiting nil |
8ee47e4a | 38 | "Whether Emacs is locked to prevent exiting. See `check-emacs-lock'.") |
0333a1cc RS |
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) | |
c9e2fc17 RS |
45 | |
46 | (defun check-emacs-lock () | |
0333a1cc | 47 | "Check if variable `emacs-lock-from-exiting' is t for any buffer. |
c4667a1a JB |
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)))))) | |
c9e2fc17 RS |
54 | |
55 | (defun toggle-emacs-lock () | |
c4667a1a | 56 | "Toggle `emacs-lock-from-exiting' for the current buffer. |
c9e2fc17 RS |
57 | See `check-emacs-lock'." |
58 | (interactive) | |
c4667a1a | 59 | (setq emacs-lock-from-exiting (not emacs-lock-from-exiting)) |
0333a1cc RS |
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. | |
c4667a1a JB |
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)))) | |
0333a1cc RS |
69 | |
70 | ; These next defuns make it so if you exit a shell that is locked, the lock | |
c4667a1a | 71 | ; is shut off for that shell so you can exit Emacs. Same for telnet. |
0333a1cc RS |
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 | ||
06b60517 | 79 | (defun emacs-lock-clear-sentinel (_proc _str) |
0333a1cc RS |
80 | (if emacs-lock-from-exiting |
81 | (progn | |
a09aa659 | 82 | (setq emacs-lock-from-exiting nil) |
0333a1cc RS |
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))) | |
c9e2fc17 | 90 | |
845fc5e5 JB |
91 | (unless noninteractive |
92 | (add-hook 'kill-emacs-hook 'check-emacs-lock)) | |
0333a1cc RS |
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) | |
c9e2fc17 | 100 | |
0333a1cc | 101 | ;;; emacs-lock.el ends here |