iqbalansari / restart-emacs

A simple emacs package to restart emacs from within emacs
GNU General Public License v3.0
158 stars 15 forks source link

Integrate with desktop-mode #6

Closed mattiasb closed 7 years ago

mattiasb commented 7 years ago

Hi!

I just wrote a little snippet (below) to my init.el that lets me restore the currently opened buffers with the help of the desktop.el module after a restart. Would you be interested in a PR?

(defun my/restart-emacs ()
  "Restart Emacs with desktop restored."
  (interactive)
  (let ((desktop-dir (getenv "XDG_RUNTIME_DIR")))
    (desktop-save desktop-dir)
    (restart-emacs (list "--eval"
                         (format "(progn (desktop-read \"%s\") (desktop-remove))"
                                 desktop-dir)))))
iqbalansari commented 7 years ago

Hi,

Thanks a lot for the contribution. This looks like a useful addition and I am interested in adding this functionality, however I think it will need some more polish before we add it. My main concern is that this package's use of desktop-mode should not interfere with that of the user. Ideally the above should be disabled if the user is already using desktop mode.

Also how do suggest this functionality should work. I think there are two ways to support this

1) It can be a customizable variable that the user can set to t if she wants the buffers to be automatically restored after restart 2) It can be a separate command that the user can call if she wants the buffers to be automatically restored after restart 3) Obviously it can be a combination of both

I am leaning towards the third option since that seems to provide the most flexibility. Thoughts?

Thanks

mattiasb commented 7 years ago

So the default path for desktop-mode is somewhere under ~/.emacs.d.

What the above code does is put it under the user's runtime directory (mounted as tmpfs on Linux) and delete it directly after the restore. So I kind of thought about conflicts already.

I'm thinking a polished up version would make the path to the temporary desktop configurable but default to something like (concat (getenv "XDG_RUNTIME_DIR") "/restart-emacs/"):

Regarding how to expose it I'm not really sure. I'd go with option 2 personally.

iqbalansari commented 7 years ago

Other option might be to use the function make-temp-file to create a temporary file, and store the desktop-mode configuration there, this would work on all platforms that Emacs supports the make-temp-file functionality irrespective of the whether XDG_RUNTIME_DIR is set or no.

There may be multiple issues with desktop mode, I know atleast one, which encountered while trying to use to restore frames after daemon restarts.

It sets the variable desktop-dirname whenever it reads from a desktop file, similarly desktop-remove sets the desktop-dirname to nil. Now this is not an issue if your configuration does not use desktop-save-mode, but if you are using it, then it prompts whether you want to save the buffers and location of the desktop file, this is a bit annoying because in the usual flow desktop mode works transparently without any prompts.

Perhaps we can workaround that issues by modifying you code to the follow (warning completely untested)

(defun my/restart-emacs ()
  "Restart Emacs with desktop restored."
  (interactive)
  (let ((desktop-dir temporary-file-directory)
        desktop-dirname)
    (desktop-save desktop-dir)
    (restart-emacs (list "--eval"
                         (prin1-to-string `(let (desktop-dirname)
                                             (desktop-read ,desktop-dir)
                                             (desktop-remove)))))))

We can keep this a separate command for now, also we should make sure this new command behaves similar to the current restart-emacs command (single prefix argument starts Emacs with --debug-init, two prefix arguments start Emacs with -Q and three prefix arguments read additional flags from the user).

Feel free to open a pull request with above changes.

Thanks

mattiasb commented 7 years ago

This all makes sense. Hopefully I'll get on this tomorrow or later this week.

alagalah commented 7 years ago

Regardless of how its implemented, it would make this great plugin extremely useful if you could "restart-emacs" and pickup where you left off.

iqbalansari commented 7 years ago

Hi @alagalah, I had tried using desktop-mode on feature/daemon branch. However I was not satisfied with the code let me see I could clean it up a bit and merge to master. For now you can just enable desktop-mode and your buffers and frames would be restored on across restarts. Does that work for you?

iqbalansari commented 7 years ago

Hi @mattiasb, @alagalah

I have pushed initial support for this on the master branch, right now it is disabled by default to enable it put something like the below to your init file

(setq restart-emacs-restore-frames t)
mattiasb commented 7 years ago

Awesomesauce! Sorry for not getting back, life kind of got in the way.

iqbalansari commented 7 years ago

No worries @mattiasb, I can understand :smile: