Download
(defvar protect-buffer-verbose t
"*If non-nil, print a message when attempting to kill a protected buffer.")
(defvar protect-buffer-bury-p t
"*If non-nil, bury buffer when attempting to kill it.
This only has an effect if the buffer to be killed is the one
visible in the selected window.")
(defvar protect-buffer-from-kill-mode nil
"*If non-`nil', then prevent buffer from being accidentally killed.
This variable is local to all buffers.")
(progn
(make-variable-buffer-local 'protect-buffer-from-kill-mode)
(put 'protect-buffer-from-kill-mode 'permanent-local t)
(or (assq 'protect-buffer-from-kill-mode minor-mode-alist)
(setq minor-mode-alist (cons '(protect-buffer-from-kill-mode " prot")
minor-mode-alist))))
(defvar protect-process-buffer-from-kill-mode nil
"*If non-`nil', then protect buffer with live process from being killed.
This variable is local to all buffers.")
(progn
(make-variable-buffer-local 'protect-process-buffer-from-kill-mode)
(put 'protect-process-buffer-from-kill-mode 'permanent-local t)
(or (assq 'protect-process-buffer-from-kill-mode minor-mode-alist)
(setq minor-mode-alist
(cons '(protect-process-buffer-from-kill-mode " protp")
minor-mode-alist))))
(defvar protect-process-buffer-from-kill-preserve-function nil
"*Function to run to determine whether to kill a process buffer.
If function returns non-nil, buffer is preserved. Otherwise, the buffer
may be killed.
If this variable is undefined, default action is to test whether a process
object is using this buffer as a process buffer.
This variable is buffer-local when set.")
(make-variable-buffer-local 'protect-process-buffer-from-kill-preserve-function)
(put 'protect-process-buffer-from-kill-preserve-function 'permanent-local t)
(defun protect-buffer-from-kill-mode (&optional prefix buffer)
"Protect buffer from being killed.
To remove this protection, call this command with a negative prefix argument."
(interactive "P")
(or buffer (setq buffer (current-buffer)))
(save-excursion
(cond
((null prefix)
(set-buffer buffer)
(setq protect-buffer-from-kill-mode
(not protect-buffer-from-kill-mode)))
((>= prefix 0)
(set-buffer buffer)
(setq protect-buffer-from-kill-mode t))
(t
(set-buffer buffer)
(setq protect-buffer-from-kill-mode nil)))
(add-hook 'kill-buffer-query-functions 'protect-buffer-from-kill)))
(defun protect-buffer-from-kill ()
(cond
(protect-buffer-from-kill-mode
(and protect-buffer-verbose
(message "Buffer \"%s\" is protected from being killed."
(buffer-name)))
(and protect-buffer-bury-p
(eq (current-buffer)
(window-buffer (selected-window)))
(bury-buffer))
nil)
(t)))
(defun protect-process-buffer-from-kill-mode (&optional prefix buffer)
"Protect buffer from being killed as long as it has an active process.
To remove this protection, call this command with a negative prefix argument."
(interactive "P")
(or buffer (setq buffer (current-buffer)))
(save-excursion
(cond
((null prefix)
(set-buffer buffer)
(setq protect-process-buffer-from-kill-mode
(not protect-process-buffer-from-kill-mode)))
((>= prefix 0)
(set-buffer buffer)
(setq protect-process-buffer-from-kill-mode t))
(t
(set-buffer buffer)
(setq protect-process-buffer-from-kill-mode nil)))
(add-hook 'kill-buffer-query-functions 'protect-process-buffer-from-kill)))
(defun protect-process-buffer-from-kill ()
(cond
((not protect-process-buffer-from-kill-mode) t)
((or (and (boundp 'protect-process-buffer-from-kill-preserve-function)
protect-process-buffer-from-kill-preserve-function
(funcall protect-process-buffer-from-kill-preserve-function))
(get-buffer-process (current-buffer)))
(and protect-buffer-verbose
(message "Buffer \"%s\" has live process; not killing."
(buffer-name)))
(and protect-buffer-bury-p
(eq (current-buffer)
(window-buffer (selected-window)))
(bury-buffer))
nil)
(t t)))
(add-hook 'kill-buffer-query-functions 'protect-buffer-from-kill)
(add-hook 'kill-buffer-query-functions 'protect-process-buffer-from-kill)
(provide 'protbuf)