Skip to content

Instantly share code, notes, and snippets.

@lesliesrussell
Created February 14, 2025 15:34
Show Gist options
  • Save lesliesrussell/b337a294c7430279aa6c7165cd2657bf to your computer and use it in GitHub Desktop.
Save lesliesrussell/b337a294c7430279aa6c7165cd2657bf to your computer and use it in GitHub Desktop.
make occur mode interactive
(defvar interactive-occur-timer nil
"Timer for updating the occur buffer.")
(defvar interactive-occur-delay 0.3
"Delay in seconds before updating the occur buffer.")
(defun interactive-occur ()
"Interactively search using occur, updating results as you type."
(interactive)
(let ((source-buffer (current-buffer))
(old-hook minibuffer-setup-hook))
(unwind-protect
(progn
(add-hook 'minibuffer-setup-hook
(lambda ()
(add-hook 'after-change-functions
(lambda (&rest _)
(when interactive-occur-timer
(cancel-timer interactive-occur-timer))
(setq interactive-occur-timer
(run-with-timer
interactive-occur-delay nil
(lambda ()
(let ((pattern (minibuffer-contents)))
(unless (string-empty-p pattern)
(with-current-buffer source-buffer
(occur pattern 1))))))))
nil t)))
(let ((pattern (read-from-minibuffer "Pattern: ")))
(unless (string-empty-p pattern)
(with-current-buffer source-buffer
(occur pattern))
(pop-to-buffer "*Occur*"))))
(setq minibuffer-setup-hook old-hook)
(when interactive-occur-timer
(cancel-timer interactive-occur-timer)
(setq interactive-occur-timer nil)))))
;; Bind it to a key (customize as needed)
(global-set-key (kbd "C-c s") 'interactive-occur)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment