Org-Mode: Return to Task Buffer When Closing Email

This is a small thing, but it’s been bugging me for a while, so I’m glad I finally took the time to find a solution.

When I read my email, I don’t respond to anything on the spot. Every message that requires a reply or any other action gets refiled as an org-mode task with a header, timestamp, and link to the message. When I’m finished going through mail and refiling everything that needs an action, I then go to my org-mode agenda, which shows those tasks, and clock each one in while I handle it. That way, the time I spend on each email is recorded, and I don’t get bogged down in the middle of checking email without knowing what else I have to do. If one of them needs priority over the others, I can tackle it first without worrying that unknown emergencies are waiting further down in my email.

Each of these tasks is saved with an underlined link directly to the message in Gnus, as in the following example. That’s very handy, since I can clock-in the task and then one keystroke gets me right back into the email, where I can reply or do whatever I need to do.

* NEXT Respond to Castalia House <books@castaliahouse.com> on NEW RELEASE: APPENDIX N: A Literary History of D&D
SCHEDULED: <2017-01-17 Tue>
[2017-01-17 Tue 10:01]
<u>Email from Castalia House: NEW RELEASE: APPENDIX N: A Lit</u>

The annoyance comes when exiting the email. Hitting ‘q’ to leave the summary buffer takes me back to the Gnus Group buffer, not back to the buffer containing the org-mode task I’m working on. In almost all cases, I want to return to that task to make notes and/or mark it done. I don’t need to go to the Group buffer, because I’m not in “email reading mode” now.

At first I thought I could add something to one of the hooks that runs when exiting a Gnus group. But both of the hooks, gnus-summary-exit-hook and gnus-group-exit-hook, run in gnus-summary-exit before the buffer is switched back to the Group buffer. No hooks run after that, and there’s no option to have it not switch to that buffer. So I would have to hack the function itself, which is a poor solution.

Then I remembered the “advice” ability of Emacs, to define a function to run after any other function, whether the first function has hooks for it or not. This worked nicely. A single line did the trick:

(defadvice gnus-summary-exit (after gnus-summary-exit-after activate) (bury-buffer))

What this does is to “bury” the Group buffer after gnus-summary-exit has returned to it. Burying a buffer just moves it to the bottom of the buffer list, which leaves the next buffer on top – in this case, the buffer I was in before I went into the email, which is the buffer containing the org-mode task. My cursor is even right where I left it in the task, ready to finish the action.

The only downside to this solution is that if I’m reading news in Gnus, each time I leave a group, it’s going to bury the Group buffer, and I’ll have to select it again. I almost never do that, though, so that’s not an issue for me.