From 934c067f7d4b4e0edce86047f77d699cf11bc4ee Mon Sep 17 00:00:00 2001 From: Marc Marcos Date: Wed, 23 Sep 2026 14:10:14 +0200 Subject: [PATCH] bf: improvements to the top toolbar about open buffers --- emacs/.emacs.d/init.el | 59 ++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/emacs/.emacs.d/init.el b/emacs/.emacs.d/init.el index 47fe086..8d79759 100644 --- a/emacs/.emacs.d/init.el +++ b/emacs/.emacs.d/init.el @@ -108,13 +108,17 @@ (string= name org-agenda-buffer-name))))))) (defun marc/tab-line-buffers () - "Return the buffers to show as tabs, in a stable display order." + "Return the buffers to show as tabs, in a stable display order. +Buffers opened while a completion session is active (e.g. an Ivy +preview) are not shown until the session ends, so peeking does not +add tabs." (setq marc/tab-line-order (seq-filter #'marc/tab-line-buffer-p marc/tab-line-order)) - (dolist (b (buffer-list)) - (when (and (marc/tab-line-buffer-p b) - (not (memq b marc/tab-line-order))) - (setq marc/tab-line-order (append marc/tab-line-order (list b))))) + (unless (active-minibuffer-window) + (dolist (b (buffer-list)) + (when (and (marc/tab-line-buffer-p b) + (not (memq b marc/tab-line-order))) + (setq marc/tab-line-order (append marc/tab-line-order (list b)))))) marc/tab-line-order) (defun marc/tab-line-tab-name (buffer &optional tabs) @@ -272,34 +276,48 @@ "Point to restore if a `rune/counsel-rg-project' session is aborted.") (defvar rune/counsel-rg--session-buffers nil "Value of `buffer-list' when a `rune/counsel-rg-project' session started.") +(defvar rune/counsel-rg--window nil + "Window showing the preview during a `rune/counsel-rg-project' session.") + +(defun rune/counsel-rg--kill-preview-buffers (old-bufs keep) + "Kill unmodified file buffers opened since OLD-BUFS, except KEEP." + (dolist (buf (buffer-list)) + (when (and (buffer-live-p buf) + (not (memq buf old-bufs)) + (not (eq buf keep)) + (buffer-file-name buf) + (not (buffer-modified-p buf))) + (kill-buffer buf)))) (defun rune/counsel-rg--unwind-fn () - "Restore the originating buffer when preview is aborted. + "Clean up after a `rune/counsel-rg-project' session. Always cleans up the async rg process and swiper overlays. When the session did not finish with RET (`ivy-exit' is not `done'), -jump back to the original buffer/point and kill file buffers that -were opened purely for preview." +jump back to the original buffer/point. In both cases kill the +file buffers that were opened only to preview candidates, keeping +the settled buffer when the session finished with RET." (counsel-delete-process) (swiper--cleanup) (let ((orig rune/counsel-rg--orig-buffer) (pt rune/counsel-rg--orig-point) + (win rune/counsel-rg--window) (old-bufs rune/counsel-rg--session-buffers) (aborted (not (eq ivy-exit 'done)))) (setq rune/counsel-rg--orig-buffer nil rune/counsel-rg--orig-point nil + rune/counsel-rg--window nil rune/counsel-rg--session-buffers nil) - (when (and aborted orig (buffer-live-p orig)) - (with-ivy-window - (switch-to-buffer orig) - (when (and pt (<= pt (point-max))) - (goto-char pt)))) - (when aborted - (dolist (buf (seq-filter (lambda (b) (not (memq b old-bufs))) (buffer-list))) - (when (and (buffer-live-p buf) - (not (eq buf orig)) - (buffer-file-name buf) - (not (buffer-modified-p buf))) - (kill-buffer buf)))))) + (if aborted + (progn + (when (and orig (buffer-live-p orig)) + (with-ivy-window + (switch-to-buffer orig) + (when (and pt (<= pt (point-max))) + (goto-char pt)))) + (rune/counsel-rg--kill-preview-buffers old-bufs nil)) + (rune/counsel-rg--kill-preview-buffers + old-bufs + (and (window-live-p win) (window-buffer win)))))) (defun rune/counsel-rg-project () "Search the current project using counsel-rg with live preview. @@ -309,6 +327,7 @@ jumps to the selected match." (interactive) (setq rune/counsel-rg--orig-buffer (current-buffer)) (setq rune/counsel-rg--orig-point (point)) + (setq rune/counsel-rg--window (selected-window)) (setq rune/counsel-rg--session-buffers (buffer-list)) (let ((ivy-update-fns-alist '((counsel-rg . auto))) (ivy-unwind-fns-alist '((counsel-rg . rune/counsel-rg--unwind-fn))))