dotfiles

Alpine Linux dotfiles

git clone git://git.lin.moe/dotfiles.git

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