persp-mode.el icon indicating copy to clipboard operation
persp-mode.el copied to clipboard

indirect buffers not saved by persp-save-state-to-file doesn't save

Open notuntoward opened this issue 7 years ago • 5 comments

I like persp-mode but wish that it could have indirect files (the result of, for example clone-indirect-buffer-other-window).

Say I have a file named

~/tmp/tmp.txt

and that I've cloned it, so that I have another buffer viewing the same file. The emacs modeline will call this ~/tmp/tmp.txt<2>.

Then, if I save the window configuration with persp-save-state-to-file, kill emacs, and then try to restore the config with persp-load-state-from-file, then the buffer positions will be correctly restored; the file ~/tmp/tmp.txt will be reloaded; and it will be in its original buffer.

However, the buffer where the indirect buffer was will be blank, and it will be given the name -tmp.txt<2>

It looks like persp-mode is trying to load a file named -tmp.txt<2> instead of recreating the indirect view.

notuntoward avatar May 20 '18 11:05 notuntoward

You can define a custom save/load function https://github.com/Bad-ptr/persp-mode.el#custom-saveload-buffer-function-example

Something like this:

(with-eval-after-load "persp-mode"
  (defvar persp-indirrect-buffers-to-restore nil)

  (persp-def-buffer-save/load
   :tag-symbol 'def-indirect-buffer
   :predicate #'buffer-base-buffer
   :save-function
    #'(lambda (buf tag vars)
        (list tag (buffer-name buf) vars
              (buffer-name (buffer-base-buffer))))
    :load-function
    #'(lambda (savelist &rest _rest)
        (destructuring-bind
            (buf-name vars base-buf-name &rest _rest) (cdr savelist)
          (push (cons buf-name base-buf-name)
                persp-indirrect-buffers-to-restore)
          nil)))

  (add-hook 'persp-after-load-state-functions
            #'(lambda (&rest _args)
                (dolist (ibc persp-indirrect-buffers-to-restore)
                  (let* ((nbn (car ibc))
                         (bbn (cdr ibc))
                         (bb (get-buffer bbn)))
                    (when bb
                      (when (get-buffer nbn)
                        (setq nbn (generate-new-buffer-name nbn)))
                      (make-indirect-buffer bb nbn t))))
                (setq persp-indirrect-buffers-to-restore nil))))

Bad-ptr avatar May 26 '18 19:05 Bad-ptr

Thanks! However, it doesn't run on my machine (Window emacs 25.3.1). There are no errors when I save the configuration but when I try to load it with persp-load-state-from-file, I get the message:

persp-load-state-from-file: Invalid read syntax: "#"

I get the same error, if I open the saved configuration file and run it with M-x eval-buffer

notuntoward avatar May 27 '18 10:05 notuntoward

I updated the code in the previous post. But before running it, you must update the persp-mode.

Bad-ptr avatar Jun 04 '18 17:06 Bad-ptr

@Bad-ptr Your code above seemed to work, but then I noticed the indirect buffer had the entire contents of the file it was derived from. Is there anyway to make sure the indirect buffer is only the part of the buffer you cloned?

Here's an example:

persp-mode-org-indirect-buf-issue

codygman avatar Oct 03 '18 04:10 codygman

With the latest persp-mode, I no longer get error messages but still can't save indirect buffers. There is also now a problem with package-autoremove.

With the latest persp-mode, I can save emacs buffer state with M-x persp-save-state-to-file, kill/restart emacs, and then restore the state with M-x persp-load-state-from-file. This correctly loads the files I was visiting before. If there were no indirect buffers, there are no other messages.

However, if I save/load an emacs state containing an indrect buffer, I get a minibuffer full of code and the indirect buffer is not restored. All the files are loaded in some buffer but only a blank scratch buffer is shown (screenshot below).

buffer-full-of-code

I should say that I'm also using the custom save/load function contributed by @Bad-ptr on May 26.

The other problems is that M-x package auto-remove now thinks that persp-mode is unused and wants to delete it. It looks like persp-mode isn't registering itself with the package manager, somehow.

This is on Windows emacs 26.1, and with the attached persp-mode invocation in my .emacs:

persp-mode-in-dotemacs.txt

notuntoward avatar Oct 15 '18 06:10 notuntoward