Treemacs: Restore

Created on 25 Apr 2017  路  17Comments  路  Source: Alexander-Miller/treemacs

I save my windows-configuration - previously with persp-mode I think, but recently with desktop-save in vanilla emacs instead. Problem is neither neotree nor dired gets restored, not in plain Emacs nor with a package for saving sessions. I suspect its not a simple fix, but havent looked into it.
So use-case is set (desktop-save-mode 1). Get your windows configured, probably with a narrow treemacs window on the side. Restart emacs and treemacs is back the way it was.
Or M-x desktop-save, change window configuration or restart emacs. Then desktop-change-dir to where you saved and treemacs get restored.
Understandable?

All 17 comments

Working with the builtin desktop-mode cannot be done. Desktop-mode only saves buffers associated with real files, which treemacs is not.

What I can do is save the treemacs buffer content (and some other information, like the width of its window) when emacs quits, or optionally when the treemacs buffer is killed. First part to be saved would be just the root, I'd add more stuff, like expanded directories, later.

Restoring that persisted state could be done in a couple of ways, like an explicit function call, or the first time you run treemacs-toggle after starting emacs, or the saved treemacs window could just popup when emacs starts.

So much for the treemacs buffer content. The position of the window is another story. Treemacs only knows about treemacs, trying to track the state of other windows and try to resotre treemacs' position among them is IMO unreasonably excessive, espeicially since treemacs is by default supposed to stay in one place anyway.

Hm, I may have been wrong about restoring dired buffers with desktop-save-mode - it now appears to work.
Too bad its not possible to work with the built in desktop, but the alternative sounds ok. Not sure if I read you correctly, is it possible to restore treemacs in the same window as it was when quitting emacs? Or is it that when you start treemacs it can be restored the way it was.
Those icons look great

I may have an idea that may work with desktop mode. Emphasis on the may, no promises. I'll need to test it.

For now I'll be adding the persistence in small parts. First part will be simple and manual saving and restoration. Only current root directory is saved, saving and loading are functions you need to call yourself.

Second part is making things automitac, save treemacs when it's killed, restore it at emacs start.

Then I'll see if I can work with desktop mode. I'll let you know when part 1 is ready.

Part 1 is ready and up on the persist branch. You now have the two basic treemacs-persist and treemacs-restore functions. Only the current root is saved so far, but I'll add more stuff later.

Please go ahead and give them a try, I need to know if the wrting stuff to disk part works on systems other than my own.

Experiments with automatic restoration are going well, I'm pretty sure I can make it work at least with persp-mode.

Those icons look great

Those icons are real png files, so I have quality, but not quantity. Let me know if there's some specific filetype you'd want covered.

I've now uploaded a version that ought to work with desktop-save-mode.

Never mind it doesn't work yet.

Suggestion for icons: javascript, typescript, json, html
There is also a namingconvention for angular, files end with .component.ts, .component.html, .directive.ts, .module.ts, services.ts, .model.ts. Guess that is overkill, but it could look good.

Ok, now I'm failry sure that persistence is where it should be. Try it out, both persp and desktop mode should work.

javascript, typescript, json, html

I can keep an eye out for those. Angular will be difficult. The way it's done now is that icons are stored in a hashtable with the file extension serving as the key. So .module.ts would just get the typescript icon, or just the default since I have no typescript icon atm.

Best I can do is offer a customization to use a different file-extension grab function (currently using the one from f.el), but that would have to apply to all files.

Cloned the persist branch and installed by package-install-file and then choose treemacs.el. Is that the way to do it? If I do treemacs-toggle or treemacs-init I get eval-buffer: Cannot open load file: no such file or directory, treemacs-faces.
Edit: Moved the rest of the files manually and now it works. Not used to install manually, how is it done?

If I desktop-save then desktop-change-dir to where I saved I get Desktop: 1 frame, 2 buffers restored, 1 failed to restore.
The angular thing was just a thought. What would be cool is if some folders could hint whats inside. Guess there are lots of typical project-folders. But don't listen too much to users, they know not what they need :)

Not used to install manually, how is it done?

Add the the treemacs directory to the load-path var and then do (require 'treemacs). Getting treemacs on melpa is next on my todo list after this and also adding the same thing neotree-find does.

If I desktop-save then desktop-change-dir I get Desktop: 1 frame, 2 buffers restored, 1 failed to restore.

That's probably because no treemacs state had been saved yet. It works like this: when treemacs-mode is activated it adds a hook to emacs-kill-hook which runs treemacs-persist if treemacs--never-persist is not t and either desktop- or persp-mode is on,

If you just want to try things open a treemacs buffer and then manually call treemacs-persist (it's not interactive) before saving the desktop. If manually saving is part of your workflow I'll probably need to advise desktop-save.

New version up. Persistence now works out of the box with desktop-save-mode, saving state whenever desktop-save-mode does. That's the default, supposed-to-always-seamlessly-work persist option. For other persist systems users need to to some minimal bit of setup on their own. More on that in the readme, there's now a section explaining how these things work. Let me know if anythins's missing or is badly explained.

This ought to be the final version, it's been working for me for a while now, so unless you find bugs or missing features I'll be pushing this to master.

Looks great, follow-mode seems very useful. Not getting Persistence to work yet, but will give it a more thorough shot soon.

Stuff to consider or check when trying persistence, let me know if any part of this is missing:

  • All the new persist stuff is in its own file - treemacs-persist.el. You'll need to require it when you've just switched from master.
  • Desktop mode integration should be automatically loaded when desktop mode is loaded.
    You know it's all set up right when treemacs--maybe-persist is part of desktop-save-hook and
    (treemacs-mode . treemacs--desktop-handler) is part of desktop-buffer-mode-handlers
  • If this part is as it should be saving the desktop should also save the treemacs buffer state if it exists
  • Treemacs saves its state in treemacs--persist-file which is defined as $user-emacs-directory/.cache/treemacs-persist
  • Its content should look like this:
    ROOT : /home/a/Documents/git/treemacs
    OPEN-DIRS : /home/a/Documents/git/treemacs/icons
    POINT-AT : /home/a/Documents/git/treemacs/icons
  • Doing desktop-read should restore this state

When quitting I am getting Error while saving the desktop. Ignore? (yes or no) and Opening output file: no such file or directory, /home/user/.emacs.d/.cache/treemacs-persist

Pretty sure that happens because you have no .cache dir and (f-)touch will not create directories. I've uploaded a ore thourough check now.

Just gave it a try and looks like problem solved, thanks

Great. Changes are now up on master. With persistence done I've finished everything I wanted to get done before publishing. Next step is to get this up on melpa.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fiveNinePlusR picture fiveNinePlusR  路  12Comments

gerdint picture gerdint  路  5Comments

simurgh9 picture simurgh9  路  3Comments

maxwedwards picture maxwedwards  路  3Comments

ianpan870102 picture ianpan870102  路  4Comments