[PATCH 3/3] Make notmuch-hello-insert-saved-searches return (priority . target-pos)

Tomi Ollila tomi.ollila at iki.fi
Tue Apr 24 14:11:10 PDT 2012

From: Tomi Ollila <too at iki.fi>

In order to figure out cursor position on notmuch-hello buffer, whatever
sections there are inserted, a following method is introduced:

While inserting sections, notmuch-hello () holds up 2 variables;
final-target-pos-pri and final-target-pos, initial values -1 and 0,
In case the insert function in question returns cons cell and the
car is integer and it's value creater than final-target-pos-pri --
and cdr is integer or marker, final-target-pos is updated to 
this (cdr) value (and final-target-pos-pri is updated to car value).

Pre-defined priority values are thought as:

'notmuch-hello-insert-header' & 'notmuch-hello-insert-footer' use 0
by default (in case these have no spesific reason to promote themselves)
All other sections uses 1 by default.
In case any of these sections know (for some reason) cursor should be
positioned inside their sections these use 2.
In case these definitely know the cursor should be positioned inside
their section (over some known widget (same that cursor was over before
reresh)) these use priority value of 10.

This patch just converts 'notmuch-hello-insert-saved-searches' to use
this scheme -- as it previously  was the only one updating

This change is pretty simple; If this scheme is generally accepted
I'll complete the work; Either just as planned above or with chances
acquired by constructive discussion.

I'm currently using this -- works good when cursor is over 
saved-searches section; Otherwise cursor jumps over [inbox] widget
(which us currenly first (second) in saved-search section. This
is expected behaviour until all sections are converted.

 emacs/notmuch-hello.el |   26 +++++++++++++++-----------
 1 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index c2cda19..2fdce03 100644
--- a/emacs/notmuch-hello.el
+++ b/emacs/notmuch-hello.el
@@ -204,11 +204,10 @@ function produces a section simply by adding content to the current
 buffer.  A section should not end with an empty line, because a
 newline will be inserted after each section by `notmuch-hello'.
-Each function should take no arguments.  If the produced section
-includes `notmuch-hello-target' (i.e. cursor should be positioned
-inside this section), the function should return this element's
-Otherwise, it should return nil.
+Each function should take no arguments. The return value should
+either be nil, or a cons cell containing priority value and a
+suggested cursor position inside this section. Finally cursor will be
+positioned to a position which had highest priority value.
 For convenience an element can also be a list of the form (FUNC ARG1
 ARG2 .. ARGN) in which case FUNC will be applied to the rest of the
@@ -568,9 +567,11 @@ Complete list of currently available key bindings:
 			       (customize-variable 'notmuch-saved-searches))
       (widget-insert "\n\n")
-      (let ((start (point)))
-	(setq found-target-pos
-	      (notmuch-hello-insert-buttons searches))
+      (setq found-target-pos (cons 1 (point)))
+      (let ((start (point))
+	    (target-pos (notmuch-hello-insert-buttons searches)))
+	(if target-pos
+	    (setq found-target-pos (cons 10 target-pos)))
 	(indent-rigidly start (point) notmuch-hello-indent)
@@ -775,15 +776,18 @@ following:
       (mapc 'delete-overlay (car all))
       (mapc 'delete-overlay (cdr all)))
-    (let (final-target-pos)
+    (let ((final-target-pos 0) (final-target-pos-pri -1))
        (lambda (section)
 	 (let ((point-before (point))
 	       (result (if (functionp section)
 			   (funcall section)
 			 (apply (car section) (cdr section)))))
-	   (if (and (not final-target-pos) (integer-or-marker-p result))
-	       (setq final-target-pos result))
+	   (if (and (integerp (car-safe result))
+		    (> (car result) final-target-pos-pri)
+		    (integer-or-marker-p (cdr-safe result)))
+	       (setq final-target-pos (cdr result)
+		     final-target-pos-pri (car result)))
 	   ;; don't insert a newline when the previous section didn't show
 	   ;; anything.
 	   (unless (eq (point) point-before)

More information about the notmuch mailing list