Function: dirvish-kill-buffer-h

dirvish-kill-buffer-h is a natively compiled function defined in dirvish.el.

Signature

(dirvish-kill-buffer-h)

Documentation

Remove buffer from session's roots, clear session when roots is empty.

Source Code

;; Defined in /nix/store/1gijwny2yx4fggrsb9l110h1ybs1gdk5-emacs-packages-deps/share/emacs/site-lisp/elpa/dirvish-20260725.1520/dirvish.el
(defun dirvish-kill-buffer-h ()
  "Remove buffer from session's roots, clear session when roots is empty."
  (when-let* ((dv (dirvish-curr)) (buf (current-buffer)))
    (setf (dv-roots dv) (cl-remove-if (lambda (i) (eq (cdr i) buf)) (dv-roots dv)))
    (when (eq (cdr (dv-index dv)) buf) (setf (dv-index dv) (car (dv-roots dv))))
    (if (dv-roots dv) ; killed by user in `ibuffer' or using `kill-current-buffer'
        (when-let* ((win (dv-root-window dv))
                    ((and (window-live-p win) (window-dedicated-p win))))
          (with-selected-window win ; prevend this dedicated window get deleted
            (dirvish-save-dedication (switch-to-buffer (cdr (dv-index dv))))))
      (when-let* ((layout (dv-curr-layout dv)) (wc (dv-winconf dv)))
        (cond ((eq buf (window-buffer (selected-window))) ; in a session, reset
               (set-window-configuration wc nil t))
              (t (cl-loop for tab in (funcall tab-bar-tabs-function)
                          for ws = (alist-get 'ws tab)
                          for bs = (window-state-buffers ws)
                          if (or (memq buf bs) (member (buffer-name buf) bs))
                          do (setf (alist-get 'wc tab) wc)))))
      (mapc #'dirvish--kill-buffer (dv-preview-buffers dv))
      (mapc #'dirvish--kill-buffer (dv-special-buffers dv))
      (remhash (dv-id dv) dirvish--sessions))
    (when (memq this-command ; clear lingering sessions when killing manually
                '(kill-current-buffer ibuffer-do-kill-on-deletion-marks))
      (cl-loop for b in (buffer-list) with rs = nil
               unless (eq b buf) ; this buffer is not killed yet
               if (with-current-buffer b (derived-mode-p 'dired-mode))
               do (push b rs) ; in case there is any lingering sessions
               finally do (unless rs (setq dirvish--sessions (dirvish--ht)))))))