Manage Org Agenda-Related Buffers via display-buffer-alist

Emacs’s automatic window management is a bit arcane, but Mickey Petersen’s excellent article “Demystifying Emacs’s Window Manager” is full of inspiring snippets.

A couple of months ago, I made some tweaks. One is to dedicate a tab to “Org Agenda”. (That’s a tab-bar-mode tab, not a tab-line-mode tab.)

The code for the Org Agenda is this:

(add-to-list 'display-buffer-alist
             '("\\*Org Agenda\\*"
               (display-buffer-in-tab display-buffer-reuse-mode-window)
               (ignore-current-tab . t)
               (tab-name . "Org Files")
               (window-height . .8)
               (dedicated . t)
               (inhibit-same-window . nil)))

This makes it so that the *Org Agenda* buffer is being displayed in a tab called “Org Files”. If I’m in another tab, this tab is being selected first, and the agenda is displayed inside, so it’s not spawning tons of tabs.

The (dedicated . t) attribute ensures that the agenda itself tries to stay visible. When I follow links to TODOs, for example, this makes it even more likely that the agenda will stay on screen. And I can’t accidentally visit another buffer in its window. I need to split the window first, or, more likely in my case, create a new tab or re-use an existing one.

That’s all fine and dandy.

But I also added something else – which I like quite a bit less!

The following rule is displaying my .org files in the same tab, but always splitting the window to the right. That was intended to make sure where my org-agenda-managed files would be displayed: to the right, so the agenda itself in turn stays visible to the left in its dedicated window split.

But this also applies to manual buffer switching. That’s where I’m most dissatisfied.

If I C-x b switch to an org buffer that is visiting a file from my agenda, the “Org Files” tab will be activated, a new split (to the right) will be created, and there, the buffer is displayed.

If I switch to another such buffer, a new split is created – so I have the agenda at 50% width, a buffer to the right at 25%, and then another to its right at 25%. That’s very useless because 25% width doesn’t make for a good reading experience.

I end up switching to a buffer and then remove all other windows to focus on it. That’s not terrible, but I now believe this new default isn’t that great at all.

Nevertheless, here’s the implementation:

(defun ct/display-buffer-org-agenda-managed-p (buffer-name action)
  "Determine whether BUFFER-NAME is an org-agenda managed buffer."
  (with-current-buffer buffer-name
    (and (derived-mode-p 'org-mode)
         (member (buffer-file-name) (org-agenda-files)))))

(add-to-list 'display-buffer-alist
             '(ct/display-buffer-org-agenda-managed-p
               (display-buffer-in-tab display-buffer-in-direction)
               (ignore-current-tab . t)
               (tab-name . "Org Files")
               (direction . right)
               (window-height . .6)))

Just now I notice the (window-height . .6) setting, so I must’ve experimented with vertical splits in the past as well :) Since (direction . right) is there, the vertical split apparently wasn’t great, either. (I don’t even remember.)

I felt clever for coming up with ct/display-buffer-org-agenda-managed-p to determine if a buffer’s file was part of the agenda.

But we should kill our darlings, right?

I’ll come back after some more experimentation.

In the meantime: do you have a setting like this you’d like to share?

Receive new .