-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer-focus-hook.el
85 lines (67 loc) · 3.1 KB
/
buffer-focus-hook.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
;;; buffer-focus-hook.el --- A buffer focus hook -*- lexical-binding: t -*-
;; Copyright (C) 2017 Michael Schuldt
;; Author: Michael Schuldt <mbschuldt@gmail.com>
;; Version: 1.0
;; URL: https://github.com/mschuldt/buffer-focus-hook
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; The buffer focus hook provides a simple mechanism for reacting to buffer focus changes.
;; Similar to focus-in-hook and focus-out-hook but for buffers instead of windows.
;;
;; Example:
;;
;; (defun focus-in ()
;; (message "Buffer gained focus!"))
;;
;; (defun focus-out ()
;; (message "Buffer lost focus") t)
;;
;; ;; set the buffer focus hooks for the current buffer:
;; (buffer-focus-in-callback 'focus-in)
;; (buffer-focus-out-callback 'focus-out)
;;; Code:
(defvar buffer-focus-hook--current-buffer nil
"Buffer currently in focus.")
(defvar buffer-focus-hook--in nil
"Normal hook run when a buffers window gains focus.")
(defvar buffer-focus-hook--out nil
"Normal hook run when a buffers window looses focus.")
(defun buffer-focus-out-callback (callback &optional buffer)
"Set the CALLBACK to be run when BUFFER or current buffer window looses focus."
(with-current-buffer (or buffer (current-buffer))
(add-hook 'buffer-focus-hook--out callback nil t)))
(defun buffer-focus-in-callback (callback &optional buffer)
"Set the CALLBACK to be run when BUFFER or current buffer window gains focus."
(with-current-buffer (or buffer (current-buffer))
(add-hook 'buffer-focus-hook--in callback nil t)))
(defun buffer-focus-hook--updater ()
"Main buffer focus hook update function added for ‘buffer-list-update-hook’."
(when (not (buffer-live-p buffer-focus-hook--current-buffer))
(setq buffer-focus-hook--current-buffer nil))
(when (and (eq (window-buffer (selected-window))
(current-buffer))
(not (eq buffer-focus-hook--current-buffer
(current-buffer))))
;; selected window has current buffer
(when buffer-focus-hook--current-buffer
;; current buffer lost focus
(with-current-buffer buffer-focus-hook--current-buffer
(run-hooks 'buffer-focus-hook--out)
(setq buffer-focus-hook--current-buffer nil)))
(when (or buffer-focus-hook--in
buffer-focus-hook--out)
;; current buffer gaining focus
(setq buffer-focus-hook--current-buffer (current-buffer))
(run-hooks 'buffer-focus-hook--in))))
(add-hook 'buffer-list-update-hook 'buffer-focus-hook--updater)
(provide 'buffer-focus-hook)
;;; buffer-focus-hook.el ends here