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 Answers
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)
.
-
Thank you. I have a small Elisp question: is it required to use
advice-add
to replace the originalorg-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. Commented Dec 14, 2020 at 16:08
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.
-
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 forswitch-to-buffer-other-window
andfind-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 launchorg-agenda-list
, select your entry and have another hook atorg-agenda-after-show-hook
that will save the current point/buffer,jump-to-register
anddisplay-buffer
in the frame you like. If I have some time later today, I will look into it.– anakhaCommented Jul 15, 2017 at 17:38
q
as a regular key in theorg-mode
buffer such that you can no longer type a heading such as* queens and kings :qbert:
?q
, just thatRET
in the agenda first close agenda and restore the window layout and then jump to the task in "other window".