Bad-ptr / persp-mode.el

named perspectives(set of buffers/window configs) for emacs
400 stars 44 forks source link

Starting in org-agenda view doesn't work with persp-mode enabled #91

Open rieje opened 6 years ago

rieje commented 6 years ago

I have a simple function that starts Emacs with 2 windows split vertically--a todo.org buffer on the left and an org-agenda view on the right:

(defun my-todo-buffers () (interactive) (setq initial-buffer-choice nil) (find-file "~/notes/todo.org") (split-window-horizontally) (org-agenda-list))

I launch it by launching Emacs through emacs -f my-todo-buffers and it works fine when persp-mode is not enabled. However, if persp-mode is enabled, then for some reason not only does both the todo.org buffer and the org agenda view buffer not show up, but a specific org file gets shown instead. That specific org file is not mentioned in any of my settings but it's a file for my org-agenda-files.

How can I have an org agenda view buffer shown on Emacs launch with persp-mode enabled?

Any help is much appreciated--I really depend on persp-mode.

Thanks.

Bad-ptr commented 6 years ago

Try to run your code in persp-after-load-state-functions(maybe it will require to run with delay(using timer) to work properly) :

(with-eval-after-load "persp-mode"
(defun persp-restore-org-agenda-after-init (file phash &rest _args)
  (when (and (eq phash *persp-hash*)
             (string= file (concat (or (file-name-directory file)
                                       (expand-file-name persp-save-dir))
                                   (file-name-nondirectory file))))
    (my-todo-buffers)
    (remove-hook 'persp-after-load-state-functions
                 #'persp-restore-org-agenda-after-init)))

(add-hook 'persp-after-load-state-functions
          #'persp-restore-org-agenda-after-init))
rieje commented 6 years ago

By using a timer for delay, did you mean specifically using sleep-for like (sleep-for 4)`? It doesn't work without it, but I'm hoping there might be a better method to sleep that doesn't freeze Emacs for 4 seconds on startup every time. Even better, would it be possible to not sleep an arbitrary amount of time and instead check and wait for a certain thing to complete instead?

Thanks.

Bad-ptr commented 6 years ago

By using a timer for delay, did you mean specifically using sleep-for

I mean run-at-time:

(run-at-time seconds nil #'(lambda () (your-code-here)))

I did a mistake in the code in my previous post, now it's fixed. Actually you can wrap this code in a function and call it like emacs -f function-with-persp-mode-settings-for-org-agenda.

Alternatively you can manually check the commandline arguments:

(let ((org-agenda (member "-org-agenda" command-line-args)))
        (when org-agenda
         (setq command-line-args (remove "-org-agenda" command-line-args))
     (add-hook 'persp....)))
rieje commented 6 years ago

I think I found the problem, it's described here. (Edit: Someone was able to reproduce what I'm experiencing and has a guess on what might be the issue). Also, further information here.

So is there a viable solution? I tried your code and it doesn't seem to do anything.

rieje commented 6 years ago

Solution posted here.

rieje commented 6 years ago

Actually, I'll leave it open for you to decide whether it's worth closing. The original problem is dealt with, but the underlying issue of org-agenda-files which apparently visits each org agenda file alphabetically so the last file becomes the focused window after a previous perspective is restored is most likely undesirable. I think there should be a way for persp-mode to deal with this, as it's not a behavior people would expect using org-agenda-files and persp-mode.

As you can tell, it took a while to figure out what's the problem.