bf: improvements to the top toolbar about open buffers

This commit is contained in:
Marc Marcos 2026-09-23 14:10:14 +02:00
parent ae7e9d9b4b
commit 934c067f7d
Signed by: marc
GPG key ID: 30A62DFAC90731E6

View file

@ -108,13 +108,17 @@
(string= name org-agenda-buffer-name))))))) (string= name org-agenda-buffer-name)))))))
(defun marc/tab-line-buffers () (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 (setq marc/tab-line-order
(seq-filter #'marc/tab-line-buffer-p marc/tab-line-order)) (seq-filter #'marc/tab-line-buffer-p marc/tab-line-order))
(unless (active-minibuffer-window)
(dolist (b (buffer-list)) (dolist (b (buffer-list))
(when (and (marc/tab-line-buffer-p b) (when (and (marc/tab-line-buffer-p b)
(not (memq b marc/tab-line-order))) (not (memq b marc/tab-line-order)))
(setq marc/tab-line-order (append marc/tab-line-order (list b))))) (setq marc/tab-line-order (append marc/tab-line-order (list b))))))
marc/tab-line-order) marc/tab-line-order)
(defun marc/tab-line-tab-name (buffer &optional tabs) (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.") "Point to restore if a `rune/counsel-rg-project' session is aborted.")
(defvar rune/counsel-rg--session-buffers nil (defvar rune/counsel-rg--session-buffers nil
"Value of `buffer-list' when a `rune/counsel-rg-project' session started.") "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 () (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 Always cleans up the async rg process and swiper overlays. When
the session did not finish with RET (`ivy-exit' is not `done'), the session did not finish with RET (`ivy-exit' is not `done'),
jump back to the original buffer/point and kill file buffers that jump back to the original buffer/point. In both cases kill the
were opened purely for preview." file buffers that were opened only to preview candidates, keeping
the settled buffer when the session finished with RET."
(counsel-delete-process) (counsel-delete-process)
(swiper--cleanup) (swiper--cleanup)
(let ((orig rune/counsel-rg--orig-buffer) (let ((orig rune/counsel-rg--orig-buffer)
(pt rune/counsel-rg--orig-point) (pt rune/counsel-rg--orig-point)
(win rune/counsel-rg--window)
(old-bufs rune/counsel-rg--session-buffers) (old-bufs rune/counsel-rg--session-buffers)
(aborted (not (eq ivy-exit 'done)))) (aborted (not (eq ivy-exit 'done))))
(setq rune/counsel-rg--orig-buffer nil (setq rune/counsel-rg--orig-buffer nil
rune/counsel-rg--orig-point nil rune/counsel-rg--orig-point nil
rune/counsel-rg--window nil
rune/counsel-rg--session-buffers nil) rune/counsel-rg--session-buffers nil)
(when (and aborted orig (buffer-live-p orig)) (if aborted
(progn
(when (and orig (buffer-live-p orig))
(with-ivy-window (with-ivy-window
(switch-to-buffer orig) (switch-to-buffer orig)
(when (and pt (<= pt (point-max))) (when (and pt (<= pt (point-max)))
(goto-char pt)))) (goto-char pt))))
(when aborted (rune/counsel-rg--kill-preview-buffers old-bufs nil))
(dolist (buf (seq-filter (lambda (b) (not (memq b old-bufs))) (buffer-list))) (rune/counsel-rg--kill-preview-buffers
(when (and (buffer-live-p buf) old-bufs
(not (eq buf orig)) (and (window-live-p win) (window-buffer win))))))
(buffer-file-name buf)
(not (buffer-modified-p buf)))
(kill-buffer buf))))))
(defun rune/counsel-rg-project () (defun rune/counsel-rg-project ()
"Search the current project using counsel-rg with live preview. "Search the current project using counsel-rg with live preview.
@ -309,6 +327,7 @@ jumps to the selected match."
(interactive) (interactive)
(setq rune/counsel-rg--orig-buffer (current-buffer)) (setq rune/counsel-rg--orig-buffer (current-buffer))
(setq rune/counsel-rg--orig-point (point)) (setq rune/counsel-rg--orig-point (point))
(setq rune/counsel-rg--window (selected-window))
(setq rune/counsel-rg--session-buffers (buffer-list)) (setq rune/counsel-rg--session-buffers (buffer-list))
(let ((ivy-update-fns-alist '((counsel-rg . auto))) (let ((ivy-update-fns-alist '((counsel-rg . auto)))
(ivy-unwind-fns-alist '((counsel-rg . rune/counsel-rg--unwind-fn)))) (ivy-unwind-fns-alist '((counsel-rg . rune/counsel-rg--unwind-fn))))