Doom-emacs: Neotree/treemacs doesn't change with workspace

Created on 21 Apr 2019  路  21Comments  路  Source: hlissner/doom-emacs

Observed behavior

  • Have two workspaces open each with different projects.
  • Open treemacs/neotree in one workspace.
  • Switch to another workspace (SPC TAB [), the project tree is still open with the old project.

Expected behavior

  • Treemacs/neetree shoud be isolated per workspace (if opened in one workspace, should not remain opened when switching to another workspace). Or the directory should be update while switching, based on the project in each workspace.

System information


Click to expand

  • OS: darwin (x86_64-apple-darwin18.5.0)
  • Emacs: 26.1.92 (Apr 08, 2019)
  • Doom: 2.0.9 (HEAD -> develop, _upgrade/develop cf7ef0f9 2019-04-17 21:24:17 -0400)
  • Graphic display: t (daemon: nil)
  • System features: DBUS NOTIFY ACL GNUTLS LIBXML2 ZLIB TOOLKIT_SCROLL_BARS MODULES THREADS LCMS2
  • Details:
    elisp env bootstrapper: nil elc count: 0 uname -a: Darwin 18.5.0 Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64 modules: (:feature eval (evil +everywhere) file-templates (lookup +docsets) snippets workspaces :completion company (ivy +fuzzy) :ui deft doom doom-dashboard modeline fill-column hl-todo nav-flash neotree (popup +all +defaults) (pretty-code +iosevka) unicode vi-tilde-fringe vc-gutter window-select :editor fold format multiple-cursors rotate-text :emacs dired electric imenu vc :tools ansible docker editorconfig gist macos make magit pdf tmux upload vterm lsp flycheck :lang cc common-lisp data emacs-lisp go (haskell +lsp) idris (javascript +lsp) lua (markdown +pandoc) nix ocaml (org +attach +babel +capture +export +present) perl purescript (python +pyenv +pyvenv +lsp) rest ruby (rust +lsp) scala (sh +fish) swift web :app calendar :config default) packages: ((org-wiki :recipe (org-wiki :fetcher github :repo caiorss/org-wiki)) (evil-matchit :recipe (evil-matchit :fetcher github :repo redguardtoo/evil-matchit :commit 7d65b4167b1f0086c2b42b3aec805e47a0d355c4)) (exec-path-from-shell :disable t) (lsp-python-ms :recipe (lsp-python-ms :fetcher github :repo andrew-christianson/lsp-python-ms)) flycheck-pycheckers (flycheck-popup-tip :disable t) (flycheck-posframe :disable t) dap-mode) exec-path: (~/.pyenv/plugins/pyenv-virtualenv/shims ~/.pyenv/shims ~/.pyenv/bin ~/.cargo/bin /usr/local/bin /usr/bin /bin /usr/sbin /sbin /usr/local/share/dotnet ~/.dotnet/tools /Applications/Wireshark.app/Contents/MacOS ~/.gem/bin ~/scripts /usr/local/opt/coreutils/libexec/gnubin ~/.cargo/bin ~/.cabal/bin ~/.ghcup/bin ~/.local/bin /usr/local/sbin ~/bin ~/Library/Android/sdk/tools ~/Library/Android/sdk/platform-tools /usr/local/opt/go/libexec/bin ~/go/bin ~/n/bin /usr/local/Cellar/emacs-mac/emacs-26.2-rc1-mac-7.5/libexec/emacs/26.1.92/x86_64-apple-darwin18.5.0)
:ui neotree :ui treemacs :ui workspaces bug external resolved-elsewhere

Most helpful comment

treemacs has workspaces support. It might be a solution in our case.

UPD: https://github.com/Alexander-Miller/treemacs/issues/247

All 21 comments

the same issue

I am aware of this issue, but I'm afraid the solution is non-trivial, as it requires us to save and load the state of neotree/treemacs before and after switching perspectives. It would look something like this:

(defun save-sidebar-state (&rest _)
  ;; Save neotree/treemacs state to a persp parameter
  (set-persp-parameter 'sidebar-state ...))

(defun load-sidebar-state (&rest _)
  (when-let* ((state (persp-parameter 'sidebar-state)))
    ;; Restore and refresh neotree/treemacs
    ))

(add-hook 'persp-before-deactivate-functions #'save-sidebar-state)
(add-hook 'persp-activated-functions #'load-sidebar-state)

I currently have my plate full and don't use either plugin (often), so I'd appreciate any help. 4e0802e won't fix this issue, but it should prevent treemacs windows from "surviving" when creating a new workspace.

What about just hide and then show it to refresh?
A bit ugly, but might work.

@myrgy I'm afraid that would be more problematic. What buffer would we open the new neotree/treemacs window from? It will use the project root of whatever buffer is focused after we switch perspectives, which may not be desirable if we have selected a window containing a buffer in the wrong project. For example: the *Messages* buffer, a REPL, or that one file from another project left open as a reference.

Persisting state across perspectives is the best solution, but I don't personally know what state exactly needs to be persisted for treemacs and neotree, nor how to get them to recognize new state injected back into them.

treemacs has workspaces support. It might be a solution in our case.

UPD: https://github.com/Alexander-Miller/treemacs/issues/247

It has a good foundation for workspace support, but the finishing touches - the final api needed so users and packages like doom can make proper use of workspaces - isn't there yet.

I do plan to finish up old issues like these in the future, so workspaces are near the top of the todo pile.

This now works with a newer treemacs, however see https://github.com/Alexander-Miller/treemacs/issues/588

Apparently there's now explicit support for persp-mode: https://github.com/Alexander-Miller/treemacs/issues/222

I'm still trying to figure out how it works but maybe @hlissner can take a look at integrate it in doom quicker?

I've added treemacs-persp in 5a07e8f. Let me know if that resolves the issue without any additional configuration.

@hlissner thanks for working on this. However I'm not seeing any difference after 5a07e8f65. I did check that I have the :ui workspaces feature enabled, treemacs-persp in installed, and the only treemacs customization I've added is (treemacs-follow-mode) and some font tweaks.

my repro:

  1. open emacs
  2. switch to a project (SPC p p) and open any file in the project tree
  3. open treemacs (SPC o P)
  4. switch to another project (SPC p p) and open any file in the project tree
  5. open treemacs (SPC o P)
  6. switch back to the first project (SPC TAB [)

at this point treemacs is still showing the project from step 5.

The code in that commit won't actually do anything. treemacs-persp only
gives you the option to change the buffer scope, you have to actually
select it. And for now that can only be done interactively with
treemacs-select-buffer-scope. I'll be making it programmatically
setable soon.

@Alexander-Miller, the buffer scope is the treemacs buffer scope or is per open editing buffer?

Another point is that I noticed that doom doesn't configure treemacs workspaces per project/perspective, would that be needed to have the scope set to an specific project that is related to a treemacs workspace?

@Alexander-Miller, the buffer scope is the treemacs buffer scope or is per open editing buffer?

It refers to the scope that will each see a unique treemacs buffer. The
default is based on frames.

would that be needed to have the scope set to an specific project that
is related to a treemacs workspace?

Sounds like you're asking for
https://github.com/Alexander-Miller/treemacs/issues/592

@Alexander-Miller, the buffer scope is the treemacs buffer scope or is per open editing buffer?
It refers to the scope that will each see a unique treemacs buffer. The default is based on frames.
would that be needed to have the scope set to an specific project that is related to a treemacs workspace?

Thx @Alexander-Miller, yep I understood the concept of scopes but the "buffer" term confused me in thinking that was related to a whatever editing buffer and is actually related to the buffer where treemacs is displayed.

Sounds like you're asking for Alexander-Miller/treemacs#592

Yes, exactly this. I was thinking on hacking something in doom to do that but if that's already happening upstream I prefer to wait for that, thank you.

Pushed the change. You can now programmatically set the scope type with
treemacs-set-scope-type.

@hlissner I think to make this happen what is needed is to sync treemacs workspaces with doom workspaces, by creating a new treemacs workspace each time a new emacs workspace is open and teardown when closing. By doing that and also setting the default scope of treemacs to 'Perspective, it looks like this works. The thing is that I did most of this steps by using interactive functions as my elisp-fu is almost not existent for the moment.

Also noticed that the treemacs toggle function in spacemacs looks more simple than in doom (https://github.com/syl20bnr/spacemacs/blob/develop/layers/%2Bfiletree/treemacs/funcs.el). I think that can be copied except for changing the projectile references with doom-project, but again is an assumption as I haven't tested that.

@richerve

Also noticed that the treemacs toggle function in spacemacs looks more simple than in doom

+treemacs--init does two things differently:

  1. It suppresses prompting for the project and always uses the current project root.
  2. It removes all other projects in the treemacs window so that only the current project is present.

Perhaps it can be refactored; I wrote it pretty hastily, but spacemacs' version has a different intention.

I think to make this happen what is needed is to sync treemacs workspaces with doom workspaces, by creating a new treemacs workspace each time a new emacs workspace is open and teardown when closing. By doing that and also setting the default scope of treemacs to 'Perspective, it looks like this works. The thing is that I did most of this steps by using interactive functions as my elisp-fu is almost not existent for the moment.

I'll look into it, but it may take me some time; I'm preoccupied with other work at the moment.

Thank you @hlissner,

Don't worry I can't say that this is highly urgent. Will try to hack something locally meanwhile.

In terms of point 2, that's correctly what is happening, but I think that the issue is that when you change to other doom workspace, treemacs still points to the project of the previous workspace, exactly as this comment from mattsawyer77: https://github.com/hlissner/doom-emacs/issues/1348#issuecomment-574038277.

Also in case of treemacs there could be more than one project in the same workspace (treemacs workspace) and that's something that I think most likely doom emacs won't want for the moment.

Thank you again for your help.

I've enabled the treemacs-persp changes required in https://github.com/hlissner/doom-emacs/pull/2819. This seems to fix this issue for me (scoped treemacs buffers across workspaces). Please give it a shot and see if you experience any problems. You might need to remove ~/.emacs.d/.local/cache/treemacs-persist as well.

You can apply the changes locally like so:

packages.el

(package! treemacs-persp)

config.el

(use-package! treemacs-persp
  :when (featurep! :ui workspaces)
  :after (treemacs persp-mode)
  :config
  (treemacs-set-scope-type 'Perspectives))

(after! treemacs
  (defun +treemacs--init ()
    (require 'treemacs)
    (let ((origin-buffer (current-buffer)))
      (cl-letf (((symbol-function 'treemacs-workspace->is-empty?)
                 (symbol-function 'ignore)))
        (treemacs--init))
      (unless (bound-and-true-p persp-mode)
        (dolist (project (treemacs-workspace->projects (treemacs-current-workspace)))
          (treemacs-do-remove-project-from-workspace project)))
      (with-current-buffer origin-buffer
        (let ((project-root (or (doom-project-root) default-directory)))
          (treemacs-do-add-project-to-workspace
           (treemacs--canonical-path project-root)
           (doom-project-name project-root)))
        (setq treemacs--ready-to-follow t)
        (when (or treemacs-follow-after-init treemacs-follow-mode)
          (treemacs--follow))))))

just tested this. it's something I've been craving for a long time and it seems to work perfectly for me so far. thanks so much @hlissner , @Alexander-Miller , @jsravn !!

Was this page helpful?
0 / 5 - 0 ratings