Make Neotree "Tab-Local"
Not entirely sure if this is already a thing, but here goes.
I'm using whaler.el instead of project.el, because it's important to me to be able to treat arbitrary, non-git repositories as projects. I also like that it's minimal and easy to understand. I've also swapped to using neotree instead of treemacs, and generally I'm trying to build a more tab-oriented workflow for myself.
It would be nice, then, if neotree automatically updated to show the right project directory when I change tabs, but this isn't default functionality with whaler. Here's my solution, copied from my config:
(defvar tab-bar-select-tab-hook nil "Hook for `tab-bar-select-tab'") (advice-add 'tab-bar-select-tab :after (lambda (x) (run-hooks 'tab-bar-select-tab-hook)))
As far as I can tell, tab-bar-mode
doesn't expose any hooks for selecting tabs natively, so this advice does that for me.
To save the working directory as a "tab-local variable", I'm just setting it as the tab bar group name (while keeping it somewhat readable):
(defun theurgy-open-project () "Select a project and update neotree to use it as root." (interactive) (whaler :action (lambda (dir) (find-file (get-project-default-file dir)) (tab-rename (file-name-nondirectory (string-remove-suffix "/" dir))) (tab-bar-change-tab-group (concat "project: " dir)) (neotree-dir dir))))
Finally I can use the previous hook to swap back to the right directory after tab change:
(defun theurgy-swap-to-tab-project () "Go to the project dir of the tab group." (let ((tab-group-name (tab-bar-tab-group-default (tab-bar--current-tab)))) (when (string-match-p "^project:" tab-group-name) (neotree-dir (string-remove-prefix "project: " tab-group-name))))) (add-hook 'tab-bar-select-tab-hook #'theurgy-swap-to-tab-project)