[WIP PATCH 1/4] emacs: search: tidy notmuch-search-foreach-result

Subject: [WIP PATCH 1/4] emacs: search: tidy notmuch-search-foreach-result

Date: Tue, 22 Apr 2014 21:11:47 +0100

To: notmuch@notmuchmail.org

Cc:

From: Mark Walters


notmuch-search-foreach-result is cumbersome as it applies the callback
function with the position of the start of the thread as the
argument. It is more natural to move point to the start of each
relevant thread and not pass any arguments to the callback function.
---
 emacs/notmuch.el |   52 ++++++++++++++++++++++++----------------------------
 1 file changed, 24 insertions(+), 28 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 6c0bc1b..cb7c006 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -378,27 +378,24 @@ (defun notmuch-search-result-end (&optional pos)
 (defun notmuch-search-foreach-result (beg end function)
   "Invoke FUNCTION for each result between BEG and END.
 
-FUNCTION should take one argument.  It will be applied to the
-character position of the beginning of each result that overlaps
-the region between points BEG and END.  As a special case, if (=
-BEG END), FUNCTION will be applied to the result containing point
-BEG."
-
-  (lexical-let ((pos (notmuch-search-result-beginning beg))
-		;; End must be a marker in case function changes the
-		;; text.
-		(end (copy-marker end))
-		;; Make sure we examine at least one result, even if
-		;; (= beg end).
-		(first t))
-    ;; We have to be careful if the region extends beyond the results.
-    ;; In this case, pos could be null or there could be no result at
-    ;; pos.
-    (while (and pos (or (< pos end) first))
-      (when (notmuch-search-get-result pos)
-	(funcall function pos))
-      (setq pos (notmuch-search-result-end pos)
-	    first nil))))
+FUNCTION should take no arguments.  It will be applied at the
+beginning of each result that overlaps the region between points
+BEG and END.  As a special case, if (= BEG END), FUNCTION will be
+applied to the result containing point BEG."
+
+  (when (notmuch-search-get-result)
+    (save-excursion
+      ;; End must be a marker in case function changes the text.
+      (lexical-let ((end (copy-marker end)))
+	;; We always apply function at least once: even if (= BEG END).
+	(goto-char (notmuch-search-result-beginning beg))
+	(funcall function)
+	(notmuch-search-next-thread)
+	(while (and (notmuch-search-get-result)
+		    (< (notmuch-search-result-beginning) end))
+	  (funcall function)
+	  (notmuch-search-next-thread))))))
+
 ;; Unindent the function argument of notmuch-search-foreach-result so
 ;; the indentation of callers doesn't get out of hand.
 (put 'notmuch-search-foreach-result 'lisp-indent-function 2)
@@ -406,8 +403,8 @@ (defun notmuch-search-foreach-result (beg end function)
 (defun notmuch-search-properties-in-region (property beg end)
   (let (output)
     (notmuch-search-foreach-result beg end
-      (lambda (pos)
-	(push (plist-get (notmuch-search-get-result pos) property) output)))
+      (lambda ()
+	(push (plist-get (notmuch-search-get-result) property) output)))
     output))
 
 (defun notmuch-search-find-thread-id (&optional bare)
@@ -503,8 +500,8 @@ (defun notmuch-search-get-tags (&optional pos)
 (defun notmuch-search-get-tags-region (beg end)
   (let (output)
     (notmuch-search-foreach-result beg end
-      (lambda (pos)
-	(setq output (append output (notmuch-search-get-tags pos)))))
+      (lambda ()
+	(setq output (append output (notmuch-search-get-tags)))))
     output))
 
 (defun notmuch-search-interactive-region ()
@@ -543,10 +540,9 @@ (defun notmuch-search-tag (tag-changes &optional beg end only-matched)
 			beg end only-matched)))
     (notmuch-tag search-string tag-changes)
     (notmuch-search-foreach-result beg end
-      (lambda (pos)
+      (lambda ()
 	(notmuch-search-set-tags
-	 (notmuch-update-tags (notmuch-search-get-tags pos) tag-changes)
-	 pos)))))
+	 (notmuch-update-tags (notmuch-search-get-tags) tag-changes))))))
 
 (defun notmuch-search-add-tag (tag-changes &optional beg end)
   "Change tags for the current thread or region (defaulting to add).
-- 
1.7.10.4


Thread: