Function: treemacs--visit-or-expand/collapse-tag-node

treemacs--visit-or-expand/collapse-tag-node is an autoloaded and byte-compiled function defined in treemacs-tags.el.

Signature

(treemacs--visit-or-expand/collapse-tag-node BTN ARG FIND-WINDOW)

Documentation

Visit tag section BTN if possible, expand or collapse it otherwise.

Pass prefix ARG on to either visit or toggle action.

FIND-WINDOW is a special provision depending on this function's invocation context and decides whether to find the window to display in (if the tag is visited instead of the node being expanded).

On the one hand it can be called based on treemacs-RET-actions-config (or TAB). The functions in these configs are expected to find the windows they need to display in themselves, so FIND-WINDOW must be t. On the other hand this function is also called from the top level vist-node functions like treemacs-visit-node-vertical-split which delegates to the treemacs--execute-button-action macro which includes the determination of the display window.

Source Code

;; Defined in ~/.emacs.d/elpa/treemacs-20251226.1307/treemacs-tags.el
;;;###autoload
(defun treemacs--visit-or-expand/collapse-tag-node (btn arg find-window)
  "Visit tag section BTN if possible, expand or collapse it otherwise.
Pass prefix ARG on to either visit or toggle action.

FIND-WINDOW is a special provision depending on this function's invocation
context and decides whether to find the window to display in (if the tag is
visited instead of the node being expanded).

On the one hand it can be called based on `treemacs-RET-actions-config' (or
TAB).  The functions in these configs are expected to find the windows they need
to display in themselves, so FIND-WINDOW must be t. On the other hand this
function is also called from the top level vist-node functions like
`treemacs-visit-node-vertical-split' which delegates to the
`treemacs--execute-button-action' macro which includes the determination of
the display window."
  (let* ((path (treemacs--nearest-path btn))
         (extension (file-name-extension path)))
    (pcase extension
      ("py"
       (let* ((first-child (car (treemacs-button-get btn :index)))
              (name (car first-child))
              (marker (cdr first-child)))
         ;; name of first subelement of a section node ends in "definition" means we have a function
         ;; nested inside a function, so we move to the definition here instead of expanding
         (if (not (s-ends-with? " definition*" name))
             (treemacs--expand-tag-node btn arg)
           ;; select the window as visit-no-split would
           (when find-window
             (--if-let (-some-> path (get-file-buffer) (get-buffer-window))
                 (select-window it)
               (other-window 1)))
           (find-file path)
           (if (buffer-live-p (marker-buffer marker))
               (goto-char marker)
             ;; marker is stale and we need a live child button to grab its tag path to re-call imenu, but the
             ;; child button may not be there so we briefly expand the button to grab the first child to whose
             ;; position we need to move
             (-let [need-to-close-section nil]
               (treemacs-with-button-buffer btn
                 (when (eq 'tag-node-closed (treemacs-button-get btn :state))
                   (setq need-to-close-section t)
                   (treemacs--expand-tag-node btn)))
               (treemacs--call-imenu-and-goto-tag
                (treemacs-with-button-buffer btn (treemacs-button-get (next-button (treemacs-button-end btn)) :path)))
               (when need-to-close-section
                 (treemacs-with-button-buffer btn
                   (treemacs--collapse-tag-node btn))))
             (when arg (treemacs-select-window))))))
      ("org"
       (treemacs-unless-let (pos (treemacs-button-get btn 'org-imenu-marker))
           (treemacs--expand-tag-node btn arg)
         ;; select the window as visit-no-split would
         (when find-window
           (--if-let (-some-> path (get-file-buffer) (get-buffer-window))
               (select-window it)
             (other-window 1)))
         (find-file path)
         (if (marker-position pos)
             (goto-char pos)
           (treemacs--call-imenu-and-goto-tag (treemacs-with-button-buffer btn (treemacs-button-get btn :path)) t))))
      (_ (pcase (treemacs-button-get btn :state)
           ('tag-node-open   (treemacs--collapse-tag-node btn arg))
           ('tag-node-closed (treemacs--expand-tag-node btn arg)))))))