Lennart Borgman <lennart.borgman at gmail.com>
[emacs.git] / lisp / emacs-lock.el
blob3e3216c29abbbf42bd1d1d7c4721050e05674c7d
1 ;;; emacs-lock.el --- prevents you from exiting Emacs if a buffer is locked
3 ;; Copyright (C) 1994, 1997, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc
6 ;; Author: Tom Wurgler <twurgler@goodyear.com>
7 ;; Created: 12/8/94
8 ;; Keywords: extensions, processes
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
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.
36 ;;; Code:
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)
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)
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))))))
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")))
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))))
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.
76 (defun emacs-lock-shell-sentinel ()
77 (set-process-sentinel
78 (get-buffer-process (buffer-name)) (function emacs-lock-clear-sentinel)))
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)))
88 (defun emacs-lock-was-buffer-locked ()
89 (if emacs-lock-buffer-locked
90 (setq emacs-lock-from-exiting t)))
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)
99 (provide 'emacs-lock)
101 ;; arch-tag: 58e6cb43-7cf0-401a-bcb6-4902a0b8bdc1
102 ;;; emacs-lock.el ends here