1;;; buffer-focus-hook.el --- A buffer focus hook -*- lexical-binding: t -*-23;; Copyright (C) 2017 Michael Schuldt45;; Author: Michael Schuldt <mbschuldt@gmail.com>6;; Version: 1.07;; URL: https://github.com/mschuldt/buffer-focus-hook89;; This program is free software; you can redistribute it and/or10;; modify it under the terms of the GNU General Public License as11;; published by the Free Software Foundation; either version 2, or (at12;; your option) any later version.1314;; This program is distributed in the hope that it will be useful, but15;; WITHOUT ANY WARRANTY; without even the implied warranty of16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU17;; General Public License for more details.1819;; You should have received a copy of the GNU General Public License20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.2122;;; Commentary:2324;; 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)3839;;; Code:4041(defvar buffer-focus-hook--current-buffer nil42 "Buffer currently in focus.")4344(defvar buffer-focus-in-hook nil45 "Normal hook run when a buffers window gains focus.")4647(defvar buffer-focus-out-hook nil48 "Normal hook run when a buffers window looses focus.")4950(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)))5455(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)))5960(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-buffer67 (current-buffer))))68 ;; selected window has current buffer69 (when (and buffer-focus-hook--current-buffer (not (minibufferp)))70 ;; current buffer lost focus71 (with-current-buffer buffer-focus-hook--current-buffer72 (run-hooks 'buffer-focus-out-hook)73 (setq buffer-focus-hook--current-buffer nil)))7475 (when (or buffer-focus-in-hook76 buffer-focus-out-hook)77 ;; current buffer gaining focus78 (setq buffer-focus-hook--current-buffer (current-buffer))79 (run-hooks 'buffer-focus-in-hook))))8081;; (advice-add 'after-focus-change-function :after #'buffer-focus-hook--updater)82(add-hook 'buffer-list-update-hook 'buffer-focus-hook--updater)838485(provide 'buffer-focus-hook)8687;;; buffer-focus-hook.el ends here