[PATCH 1/2] emacs function to perform a search with a look back time restriction

Jameson Rollins jrollins at finestructure.net
Sun Dec 19 19:54:39 PST 2010


This function allows for restricting the date range of a search, back
from the current time.  The look-back time range is given at a second
prompt.  An example time range is '4d' to indicate four days or '12y'
to indicate twelve years.

On slow machines this can speed up the search considerably.  I believe
this handles the most common usage where one wishes to perform a
search for a message that is known to have been received recently.

One might imagine extending this function to be able to handle date
ranges, such as "11/2004-2/2005", or it becoming obsolete if more
flexible date specs are folded into notmuch search directly.
---
 emacs/notmuch.el |   62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 3d82f0d..763d517 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -948,6 +948,68 @@ current search results AND that are tagged with the given tag."
    (list (notmuch-select-tag-with-completion "Filter by tag: ")))
   (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
 
+(defun notmuch-search-convert-time-spec (spec)
+  "Internal function to convert an abbreviated time spec into seconds."
+  (if (string= spec "")
+      0
+    (let ((factor (replace-regexp-in-string "[Mhdwmy]$" "" spec))
+	  (unit (replace-regexp-in-string "^[0123456789]*" "" spec))
+	  seconds)
+      (if (string= factor "")
+	  (setq factor 1)
+	(setq factor (string-to-number factor)))
+      (if (string= unit "")
+	  (setq seconds 1)
+	(cond ((string= unit "M")
+	       (setq seconds 60))
+	      ((string= unit "h")
+	       (setq seconds 3600))
+	      ((string= unit "d")
+	       (setq seconds 86400))
+	      ((string= unit "w")
+	       (setq seconds 604800))
+	      ((string= unit "m")
+	       (setq seconds 2678400))
+	      ((string= unit "y")
+	       (setq seconds 31536000))
+	      (t
+	       (setq seconds nil))))
+      (if (null seconds)
+	  nil
+	(* factor seconds)))))
+
+(defun notmuch-search-date-restrict (query time-spec)
+  "Run \"notmuch search\" but with a look-back time restriction.
+
+This first argument is the query string.
+The second argument is a look-back time spec of the form 'XY',
+where X is an integer and Y is one of:
+  'M'  minute
+  'h'  hour
+  'd'  day
+  'w'  week
+  'm'  month
+  'y'  year
+For instance, a time spec of '3w' would return only search
+results from within the last three weeks.
+A time spec of nil returns the full search results.
+
+For more information see the \"notmuch-search\"."
+  (interactive "sNotmuch search: \nslook back?: ")
+  (let ((look-back (notmuch-search-convert-time-spec time-spec)))
+    (cond ((null look-back)
+	   (message "unknown time spec"))
+	  ((= look-back 0)
+	   (notmuch-search query))
+	  (t
+	   (let* ((now (current-time))
+		  (start (time-subtract now (seconds-to-time look-back))))
+	     (notmuch-search (concat
+			      query " "
+			      (format-time-string "%s" start)
+			      ".."
+			      (format-time-string "%s" now))))))))
+
 ;;;###autoload
 (defun notmuch ()
   "Run notmuch and display saved searches, known tags, etc."
-- 
1.7.2.3



More information about the notmuch mailing list