notmuch-mode snippet: auto-select "inbox" widget when launching

Subject: notmuch-mode snippet: auto-select "inbox" widget when launching

Date: Sun, 08 Nov 2020 10:14:37 +0100

To: notmuch@notmuchmail.org

Cc:

From: Christian Tietze


Hey folks!

I'm enjoying notmuch-mode for the better part of 2020 and really got going with emacs thanks to this.

In my day-to-day work, I noticed that when I leave the notmuch-hello buffer open, the point position remains like I left it, which is good. But sometimes I apparently hit `q` and on entering notmuch-hello the next time, I find that my point is in the upper-left corner where the logo is and I have to TAB to inbox.

My idea for a remedy: when `notmuch-hello` is executed and creates the buffer, move point to the inbox tag.

The existing hooks are kinda wonky for this, though. `notmuch-hello-mode-hook` is executed too early, before widgets are available to move the point to, for example. And adding an advice around notmuch-hello doesn't help because that's executed for every refresh, it seems.

My hacky solution is to flick a temp variable on in the mode hook and use this to distinguish the first advice execution from all the others. Code is at the end.

Maybe someone can make use of this, too.

I don't really like the way I do it :)

Do you have any suggestions? I don't want to propose a patch that changes when the hook is executed because I don't know why the hook runs before the widgets are available.

Thanks for any pointers, and enjoy!

Cheers,
Christian

-----
;; When enabling the mode for the first time in a buffer, set a temp variable to `t` ...
(defvar ct/notmuch-hello-mode-activated nil)

(defun ct/notmuch-hello-prepare-advice-hook ()
  (setq ct/notmuch-hello-mode-activated t))
(add-hook 'notmuch-hello-mode-hook #'ct/notmuch-hello-prepare-advice-hook)

(defun ct/notmuch-hello-post-launch-advice (&rest args)
  ;; ... search for the "inbox" widget in the buffer recursively.
  (defun go-to-inbox-widget ()
    (let ((widget (widget-at (point))))
      (if (and widget (string= "inbox" (widget-value widget)))
          nil
        (widget-forward 1)
        (go-to-inbox-widget))))
  ;; ... but only once (!) ...
  (unless (not ct/notmuch-hello-mode-activated)
    ;; ... so turn off the temp variable.
    (setq ct/notmuch-hello-mode-activated nil)
    (go-to-inbox-widget)))
(advice-add #'notmuch-hello :after #'ct/notmuch-hello-post-launch-advice)
-----
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: