Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / emacs-lock.el
CommitLineData
c4667a1a 1;;; emacs-lock.el --- prevents you from exiting Emacs if a buffer is locked
b578f267 2
e91081eb 3;; Copyright (C) 1994, 1997, 2001, 2002, 2003, 2004,
409cc4a3 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc
b578f267 5
c9e2fc17
RS
6;; Author: Tom Wurgler <twurgler@goodyear.com>
7;; Created: 12/8/94
1a2b7f51 8;; Keywords: extensions, processes
b578f267
EN
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
c9e2fc17 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
b578f267
EN
16
17;; GNU Emacs is distributed in the hope that it will be useful,
c9e2fc17
RS
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.
b578f267 21
c9e2fc17 22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c9e2fc17
RS
24
25;;; Commentary:
b578f267 26
c9e2fc17 27;; This code sets a buffer-local variable to t if toggle-emacs-lock is run,
c4667a1a 28;; then if the user attempts to exit Emacs, the locked buffer name will be
c9e2fc17
RS
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
c4667a1a 31;; program and exiting Emacs would abort that program, you may want to lock
c9e2fc17 32;; that buffer, then if you forget about it after a while, you won't
c4667a1a 33;; accidentally exit Emacs. To unlock the buffer, just goto the buffer and
c9e2fc17
RS
34;; run toggle-emacs-lock again.
35
b578f267
EN
36;;; Code:
37
0333a1cc 38(defvar emacs-lock-from-exiting nil
8ee47e4a 39 "Whether Emacs is locked to prevent exiting. See `check-emacs-lock'.")
0333a1cc
RS
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)
c9e2fc17
RS
46
47(defun check-emacs-lock ()
0333a1cc 48 "Check if variable `emacs-lock-from-exiting' is t for any buffer.
c4667a1a
JB
49If 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))))))
c9e2fc17
RS
55
56(defun toggle-emacs-lock ()
c4667a1a 57 "Toggle `emacs-lock-from-exiting' for the current buffer.
c9e2fc17
RS
58See `check-emacs-lock'."
59 (interactive)
c4667a1a 60 (setq emacs-lock-from-exiting (not emacs-lock-from-exiting))
0333a1cc
RS
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.
c4667a1a
JB
67If 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))))
0333a1cc
RS
70
71; These next defuns make it so if you exit a shell that is locked, the lock
c4667a1a 72; is shut off for that shell so you can exit Emacs. Same for telnet.
0333a1cc
RS
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
a09aa659 83 (setq emacs-lock-from-exiting nil)
0333a1cc
RS
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)))
c9e2fc17
RS
91
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
cbee283d 101;; arch-tag: 58e6cb43-7cf0-401a-bcb6-4902a0b8bdc1
0333a1cc 102;;; emacs-lock.el ends here