1

I have a pipeline that opens an emacs client in notmuch-mode when new mail comes in, currently using (notmuch-show "thread:..."). I'd rather use tree view with the most recent message displayed. In two steps this is easy.

  1. run notmuch-thread-view (automatic)
  2. then manually run notmuch-thread-view-in-tree.

I'd like for it to be automatic, but I cannot seem to combine the functions. I imagine notmuch-thread-view-in-tree runs before notmuch-thread-view has filled the buffer. So I think a hook is necessary. But how?

(defun notmuch-thread-view ()
  (interactive)
  (notmuch-tree "thread:0000000000000384")
  ;(notmuch-thread-view-in-tree) ; doesn't work
)

(defun notmuch-thread-view-in-tree ()
  "Open the most recent message in a thread when in tree view.
   will not work for nested threads."
  (interactive)
  (goto-char (point-max))
  (notmuch-tree-prev-message)
  (notmuch-tree-prev-message)
  (notmuch-tree-show-message-in)
)

; not run?
(add-hook 'notmuch-thread-view 'notmuch-thread-view-in-tree)
; also not helpful
(add-hook 'post-notmuch-thread-view 'notmuch-thread-view-in-tree)
phils
  • 48,657
  • 3
  • 76
  • 115
Will
  • 203
  • 1
  • 6
  • I think you just want an `advice` using `:after`, but since I don't use `notmuch`, I cannot test anything. See: https://www.emacswiki.org/emacs/AdvisingFunctions . There may be no need for a hook per se, unless you plan on adding/removing stuff frequently -- i.e., just run your function `:after` using the `advice`. If you are running a sub-process and want to execute something when the process ends, then it may be necessary to examine the process filters and/or sentinels to decide where to plug in your custom modification. – lawlist Jul 29 '18 at 00:20

2 Answers2

2

how to add a hook to a custom interactive command

Call one of the run-hook* functions, as appropriate.

See:

  • C-uC-ha run-hook
  • C-hig (elisp)Running Hooks

That said, I'm doubtful that your question actually helps you. You've said that calling (notmuch-thread-view-in-tree) directly doesn't work, and I don't see how using a hook to do the same thing will make any difference. If I'm right about that, you should probably ask a separate question and make it more specifically about your actual problem.

phils
  • 48,657
  • 3
  • 76
  • 115
2

notmuch-tree calls notmuch-tree-worker. notmuch-tree-worker starts a notmuch process via notmuch-start-notmuch.

notmuch-start-notmuch assignes the process notmuch-start-notmuch-sentinel as process sentinel.

I think that is the place where you should hook in your notmuch-thread-view-in-tree function as :after advice with the help of advice-add.

I don't use notmuch and I am also not willing to install it on my system since a test would imply too much set-up effort on my system.

But something like the following could probably work. Note once again that it is untested.

(defvar my-notmuch-after-thread-view nil)

(defun notmuch-thread-view ()
  (interactive)
  (notmuch-tree "thread:0000000000000384")
  (setq my-notmuch-after-thread-view t)
)

(defun my-notmuch-thread-view-in-tree (proc status)
  "Open the most recent message in a thread when in tree view.
   will not work for nested threads."
  (when (and my-notmuch-after-thread-view
         proc
         (stringp status)
         (string-match "finished" status))
    (setq my-notmuch-after-thread-view nil)
    (with-current-buffer (process-buffer proc)
      (goto-char (point-max))
      (notmuch-tree-prev-message)
      (notmuch-tree-prev-message)
      (notmuch-tree-show-message-in))))

(advice-add 'notmuch-start-notmuch-sentinel :after #'my-notmuch-thread-view-in-tree)
Tobias
  • 32,569
  • 1
  • 34
  • 75