pragmat1c1 · 2022-06-13 12:10
`org-roam-dailies-goto-today` opens todays journal in the current window, but I'd prefer to open it in on other-window.
Internally this command uses `org-capture-goto-target`:
(defun org-capture-goto-target (&optional template-key)
"Go to the target location of a capture template.
If TEMPLATE-KEY is nil, the user is queried for the template."
(interactive)
(let ((entry (org-capture-select-template template-key)))
(unless entry (error "No capture template selected"))
(org-capture-set-plist entry)
(org-capture-set-target-location)
(pop-to-buffer-same-window (org-capture-get :buffer))
(goto-char (org-capture-get :pos))))
taken from `org-capture.el`
As you can see it says `pop-to-buffer-same-window`, not other window.
Any workaround for this?
## Edit: Here's my workaround
(defun ugt-org-roam-dailies-goto-today nil
"Open todays journal in other window."
(interactive)
(split-window-right)
(other-window 1)
(org-roam-dailies-goto-today))
ilemming · 2022-06-14 00:11
add `:around` advice for `org-capture-goto-target` and using `cl-letf` redefine `pop-to-buffer-same-window` to use `switch-to-buffer-other-window` instead.
I'm using Doom, therefore I'd use `defadvice!` macro, it would be something like this:
(defadvice! org-capture-goto-target-a (fn &rest args)
:around #'org-capture-goto-target
(cl-letf (((symbol-function pop-to-buffer-same-window) #'switch-to-buffer-other-window))
(apply fn args)))
Basically, it says:
"whenever `org-capture-goto-target` is called, instead of executing `pop-to-buffer-same-window` defined in window.el, make it execute my thing..."
In this particular case it just calls `switch-to-buffer-other-window`. But it can be for example a lambda. Just make sure that your function and the function you're overriding use the same number of arguments.
Now, I understand that for someone who's not seasoned in Elisp, there's a lot to unpack. But I promise you, this technique is outrageously useful and understanding the concepts makes you see why Emacs is absurdly extensible.
- `defadvice!` macro comes packed in the core-lib of Doom Emacs. There are a few useful macros there, but if you're not using Doom, it still trivial to advice functions.
- `org-capture-goto-target-a` is a function that macro would create, by convention you usually would add `-a` to the name of the function being advised.
- `:around` is a so called advice combinator, you can learn about them [here](https://www.gnu.org/software/emacs/manual/html_node/elisp/Advice-Combinators.html)
- `cl-letf` is one of the ways of overriding a symbol; there are a few different ways for that, this [Stackoverflow Post](https://stackoverflow.com/questions/39550578/in-emacs-what-is-the-difference-between-cl-flet-and-cl-letf) nicely explains the differences. You may want to bookmark it
- note the arguments passed into the macro - `fn` in this case, is specific to `:around` combinator, denoting the original function.
*I know this all may still feel confusing, let me know if something is unclear, I'd glady try to explain further.*