Function: treemacs-save-position

treemacs-save-position is a macro defined in treemacs-macros.el.

Signature

(treemacs-save-position MAIN-FORM &rest FINAL-FORM)

Documentation

Execute MAIN-FORM without switching position.

Finally execute FINAL-FORM after the code to restore the position has run.

This macro is meant for cases where a simple save-excursion will not do, like a refresh, which can potentially change the entire buffer layout. In practice this means attempt first to keep point on the same file/tag, and if that does not work keep it on the same line.

Source Code

;; Defined in ~/.emacs.d/elpa/treemacs-20251226.1307/treemacs-macros.el
(defmacro treemacs-save-position (main-form &rest final-form)
  "Execute MAIN-FORM without switching position.
Finally execute FINAL-FORM after the code to restore the position has run.

This macro is meant for cases where a simple `save-excursion' will not do, like
a refresh, which can potentially change the entire buffer layout.  In practice
this means attempt first to keep point on the same file/tag, and if that does
not work keep it on the same line."
  (declare (debug (form body)))
  `(treemacs-without-following
    (declare-function treemacs--current-screen-line "treemacs-rendering")
    (let* ((curr-btn       (treemacs-current-button))
           (curr-point     (point-marker))
           (next-path      (-some-> curr-btn (treemacs--next-non-child-button) (button-get :path)))
           (prev-path      (-some-> curr-btn (treemacs--prev-non-child-button) (button-get :path)))
           (curr-node-path (-some-> curr-btn (treemacs-button-get :path)))
           (curr-state     (-some-> curr-btn (treemacs-button-get :state)))
           (collapse       (-some-> curr-btn (treemacs-button-get :collapsed)))
           (curr-file      (if collapse (treemacs-button-get curr-btn :key) (-some-> curr-btn (treemacs--nearest-path))))
           (curr-window    (get-buffer-window (current-buffer)))
           (curr-win-line  (when curr-window
                             (with-selected-window curr-window
                               (treemacs--current-screen-line)))))
      ,main-form
      ;; try to stay at the same file/tag
      ;; if the tag no longer exists move to the tag's owning file node
      (pcase curr-state
        ((or 'root-node-open 'root-node-closed)
         ;; root nodes are always visible even if deleted.
         (treemacs-goto-file-node curr-file))
        ((or 'dir-node-open 'dir-node-closed 'file-node-open 'file-node-closed)
         ;; stay on the same file
         (if (and (treemacs-is-path-visible? curr-file)
                  (or treemacs-show-hidden-files
                      (not (s-matches? treemacs-dotfiles-regex (treemacs--filename curr-file)))))
             (treemacs-goto-file-node curr-file)
           ;; file we were on is no longer visible
           ;; try dodging to our immediate neighbours, if they are no longer visible either
           ;; keep going up
           (cl-labels
               ((can-move-to (it) (and (treemacs-is-path-visible? it)
                                       (or treemacs-show-hidden-files
                                           (not (s-matches? treemacs-dotfiles-regex (treemacs--filename it)))))))
             (cond
              ((and next-path (can-move-to next-path))
               (treemacs-goto-file-node next-path))
              ((and prev-path (can-move-to prev-path))
               (treemacs-goto-file-node prev-path))
              (t
               (-when-let (detour (treemacs--parent curr-file))
                 (while (not (can-move-to detour))
                   (setf detour (treemacs--parent detour)))
                 (treemacs-goto-file-node detour)))))))
        ((or 'tag-node-open 'tag-node-closed 'tag-node)
         (treemacs-goto-node curr-node-path))
        ((pred null)
         (goto-char curr-point))
        (_
         ;; point is on a custom node
         (cond
          ((treemacs-is-path-visible? curr-node-path)
           (treemacs-goto-extension-node curr-node-path))
          ((and next-path (treemacs-is-path-visible? next-path))
           (treemacs-goto-extension-node next-path))
          ((and prev-path (treemacs-is-path-visible? prev-path))
           (treemacs-goto-extension-node prev-path))
          (t
           (-when-let (detour (treemacs--parent curr-file))
             (while (not (treemacs-is-path-visible? detour))
               (setf detour (treemacs--parent detour)))
             (treemacs-goto-extension-node detour))))))
      (treemacs--evade-image)
      (when (get-text-property (point) 'invisible)
        (goto-char (or
                    (next-single-property-change (point) 'invisible)
                    (point-min))))
      (when curr-win-line
        (-let [buffer-point (point)]
          (with-selected-window curr-window
            ;; recenter starts counting at 0
            (-let [scroll-margin 0]
              (recenter (1- curr-win-line)))
            (set-window-point (selected-window) buffer-point))))
      ,@final-form)))