1

Pressing q in an agenda view restores the window layout if org-agenda-restore-windows-after-quit is non-nil. Is there a way to do the same on RET (org-agenda-switch-to)?

2
  • Is it your intention to disable q as a regular key in the org-mode buffer such that you can no longer type a heading such as * queens and kings :qbert:?
    – lawlist
    Commented Jan 31, 2017 at 7:01
  • 1
    I don't want to do anything with q, just that RET in the agenda first close agenda and restore the window layout and then jump to the task in "other window". Commented Feb 1, 2017 at 18:15

2 Answers 2

1

Define the function my-org-agenda-switch-to:

(defun my-org-agenda-switch-to (&optional delete-other-windows)
  "Go to the Org mode file which contains the item at point.
When optional argument DELETE-OTHER-WINDOWS is non-nil, the
displayed Org file fills the frame.
Like `org-agenda-switch-to', but respects the value of the variable
`org-agenda-restore-windows-after-quit'.
It is recommended to replace `org-agenda-switch-to' by this function (using
advice)."
  (interactive)
  (if (and org-return-follows-link
       (not (org-get-at-bol 'org-marker))
       (org-in-regexp org-link-bracket-re))
      (org-link-open-from-string (match-string 0))
    (let* ((marker (or (org-get-at-bol 'org-marker)
               (org-agenda-error)))
       (buffer (marker-buffer marker))
       (pos (marker-position marker)))
      (unless buffer (user-error "Trying to switch to non-existent buffer"))
      (when org-agenda-restore-windows-after-quit
        (org-agenda-quit))
      (switch-to-buffer-other-window buffer)
      (when delete-other-windows (delete-other-windows))
      (widen)
      (goto-char pos)
      (when (derived-mode-p 'org-mode)
    (org-show-context 'agenda)
    (run-hooks 'org-agenda-after-show-hook)))))

This is a slight modification of the source code of org-agenda-switch-to. It will restore the window configuration if org-agenda-restore-windows-after-quit is non-nil.

Then use advice to overwrite the original function:

(advice-add #'org-agenda-switch-to :override #'my-org-agenda-switch-to)

If you want to open the file in the same window instead of "other window", replace (switch-to-buffer-other-window buffer) by (pop-to-buffer-same-window buffer).

2
  • Thank you. I have a small Elisp question: is it required to use advice-add to replace the original org-agenda-switch-to function, instead of just giving this one the same name? Commented Dec 14, 2020 at 14:58
  • Advice is a more "clean" way to change the behavior of built-in functions, but your suggestion also works. However, your suggestion will only work if the file with the new definition (probably your config file) is evaluated after the library that defines the function to be overwritten.
    – overideal
    Commented Dec 14, 2020 at 16:08
1

I think he means after selecting an entry, it splits the frame in two pieces. After some debugging, I found out the org-agenda-switch-to can be called with an "optional" argument, which instructs the function to close all other windows.

(add-hook 'org-agenda-mode-hook
      (lambda()
        (define-key org-agenda-mode-map
          (kbd "RET") (lambda () (interactive) (org-agenda-switch-to t)))))

Note: I'm not an emacs expert.

2
  • That's not quite what I meant, @anakha: I don't want to delete all other windows, but to restore the window layout that was active before I ran org-agenda-list, and then open the task in "other window", using the standard Emacs logic for switch-to-buffer-other-window and find-file-other-window. Commented Jul 14, 2017 at 13:49
  • In that case this can probably be done with a function that uses window-configuration-to-register then launch org-agenda-list, select your entry and have another hook at org-agenda-after-show-hook that will save the current point/buffer, jump-to-register and display-buffer in the frame you like. If I have some time later today, I will look into it.
    – anakha
    Commented Jul 15, 2017 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.