Download
(require 'protbuf)
(defcustom protect-buffer-names
'("*scratch*")
"List of names of buffers to protect from killing."
:type '(repeat string)
:group 'protect-buffer)
(defcustom protect-buffer-matches
'()
"List of regexps or functions matching buffer names to protect."
:type '(repeat (choice regexp function))
:group 'protect-buffer)
(defun buffer-protected-by-name-p (buf)
"Returns non-nil if buffer is protected by name"
(let ((bufname (buffer-name buf)))
(or
(some (lambda (protected-name)
(string= protected-name bufname))
protect-buffer-names)
(some (lambda (protected-match)
(cond
((stringp protected-match) (string-match protected-match bufname))
((functionp protected-match) (funcall protected-match bufname))
(t nil)))
protect-buffer-matches))))
(defadvice protect-buffer-from-kill (before protect-by-name activate)
"Protect buffers with certain names from killing."
(when (and (not protect-buffer-from-kill-mode)
(buffer-protected-by-name-p (current-buffer)))
(protect-buffer-from-kill-mode t)))
(defcustom protect-process-buffer-names
'()
"List of names of buffers to protect while they have active processes."
:type '(repeat string)
:group 'protect-buffer)
(defcustom protect-process-buffer-matches
'()
"List of regexps or functions matching names of buffers to protect while they have active processes."
:type '(repeat (choice regexp function))
:group 'protect-buffer)
(defun process-buffer-protected-by-name-p (buf)
"Returns non-nil if buffer is process-protected by name"
(let ((bufname (buffer-name buf)))
(or
(some (lambda (protected-name)
(string= protected-name bufname))
protect-process-buffer-names)
(some (lambda (protected-match)
(cond
((stringp protected-match) (string-match protected-match bufname))
((functionp protected-match) (funcall protected-match bufname))
(t nil)))
protect-process-buffer-matches))))
(defadvice protect-process-buffer-from-kill (before protect-by-name activate)
"Protect active process buffers with certain names from killing."
(when (and (not protect-buffer-from-kill-mode)
(buffer-protected-by-name-p (current-buffer)))
(protect-buffer-from-kill-mode t)))
(provide 'protbuf-by-name)