Skip to content

Instantly share code, notes, and snippets.

@meedstrom
Last active March 15, 2026 19:00
Show Gist options
  • Select an option

  • Save meedstrom/3e95e3a36cdc9282fe57a308c100214a to your computer and use it in GitHub Desktop.

Select an option

Save meedstrom/3e95e3a36cdc9282fe57a308c100214a to your computer and use it in GitHub Desktop.
;; -*- lexical-binding: t; -*-
;;;; Auto-revert
;; Like Doom, don't actually enable `auto-revert-mode'; this is all you want.
;; Avoids perf suckage when batch-processing many file buffers.
(require 'autorevert)
(setq auto-revert-verbose t)
(setq auto-revert-use-notify nil)
(setq auto-revert-stop-on-user-input nil)
(setq revert-without-query (list "."))
;; see below page for implementing these hooks
(add-hook 'after-save-hook #'my-try-revert-visible-buffers)
(add-hook 'doom-switch-buffer-hook #'my-try-revert-buffer)
(add-hook 'doom-switch-window-hook #'my-try-revert-buffer)
(add-hook 'doom-switch-frame-hook #'my-try-revert-visible-buffers)
(defun my-try-revert-buffer ()
(unless (or auto-revert-mode
(active-minibuffer-window)
(and auto-revert-remote-files
buffer-file-name
(file-remote-p buffer-file-name nil t)))
(let ((auto-revert-mode t))
(auto-revert-handler))))
(defun my-try-revert-window-buffer (window)
(with-current-buffer (window-buffer window)
(my-try-revert-buffer)))
(defun my-try-revert-visible-buffers ()
(walk-windows #'my-try-revert-window-buffer nil 'visible))
;;;; Copy-pasted from Doom: set up hooks on switch buffer/window/frame.
;; These are generally useful, not just for the above.
;; https://github.com/doomemacs/doomemacs/blob/master/lisp/doom-ui.el
(defcustom doom-switch-buffer-hook nil
"A list of hooks run after changing the current buffer.")
(defcustom doom-switch-window-hook nil
"A list of hooks run after changing the focused windows.")
(defcustom doom-switch-frame-hook nil
"A list of hooks run after changing the focused frame.
This also serves as an analog for `focus-in-hook' or
`after-focus-change-function', but also preforms debouncing (see
`doom-switch-frame-hook-debounce-delay'). It's possible for this hook to be
triggered multiple times (because there are edge cases where Emacs can have
multiple frames focused at once).")
(defun doom-run-switch-buffer-hooks-h (&optional _)
"Trigger `doom-switch-buffer-hook' when selecting a new buffer."
(let ((gc-cons-threshold most-positive-fixnum))
(run-hooks 'doom-switch-buffer-hook)))
(defun doom-run-switch-window-hooks-h (&optional _)
"Trigger `doom-switch-window-hook' when selecting a window in the same frame."
(unless (or (minibufferp)
(not (equal (old-selected-frame) (selected-frame)))
(equal (old-selected-window) (minibuffer-window)))
(let ((gc-cons-threshold most-positive-fixnum))
(run-hooks 'doom-switch-window-hook))))
(defvar doom-switch-frame-hook-debounce-delay 2.0
"The delay for which `doom-switch-frame-hook' won't trigger again.
This exists to prevent switch-frame hooks getting triggered too aggressively due
to misbehaving desktop environments, packages incorrectly frame switching in
non-interactive code, or the user accidentally (and rapidly) un-and-refocusing
the frame through some other means.")
(defun doom--run-switch-frame-hooks-fn (_)
(remove-hook 'pre-redisplay-functions #'doom--run-switch-frame-hooks-fn)
(let ((gc-cons-threshold most-positive-fixnum))
(dolist (fr (visible-frame-list))
(let ((state (frame-focus-state fr)))
(when (and state (not (eq state 'unknown)))
(let ((last-update (frame-parameter fr '+last-focus)))
(when (or (null last-update)
(> (float-time (time-subtract (current-time) last-update))
doom-switch-frame-hook-debounce-delay))
(with-selected-frame fr
(unwind-protect
(let ((inhibit-redisplay t))
(run-hooks 'doom-switch-frame-hook))
(set-frame-parameter fr '+last-focus (current-time)))))))))))
(let (last-focus-state)
(defun doom-run-switch-frame-hooks-fn ()
"Trigger `doom-switch-frame-hook' once per frame focus change."
(or (equal last-focus-state
(setq last-focus-state
(mapcar #'frame-focus-state (frame-list))))
;; Defer until next redisplay
(add-hook 'pre-redisplay-functions #'doom--run-switch-frame-hooks-fn))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment