unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* new emacs function for date-restricted searches
@ 2010-12-20  3:54 Jameson Rollins
  2010-12-20  3:54 ` [PATCH 1/2] emacs function to perform a search with a look back time restriction Jameson Rollins
  2010-12-20  3:54 ` [PATCH 2/2] bind notmuch-search-date-restrict to 'S' Jameson Rollins
  0 siblings, 2 replies; 3+ messages in thread
From: Jameson Rollins @ 2010-12-20  3:54 UTC (permalink / raw)
  To: Notmuch Mail

The following patch introduces a new function for restricting search
results to messages within a certain look-back time.  For instance, if
the time specification '3w' is given then the search will be
restricted to messages received within the last three weeks.

The second patch binds this function to the 'S' key.

I have a feeling that Carl won't be into this patch, but I'm throwing
it out there anyway, just in case.  On my slow machine, use of this
function speeds up searches considerably, especially since most of the
time I'm just searching for recent messages anyway.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] emacs function to perform a search with a look back time restriction
  2010-12-20  3:54 new emacs function for date-restricted searches Jameson Rollins
@ 2010-12-20  3:54 ` Jameson Rollins
  2010-12-20  3:54 ` [PATCH 2/2] bind notmuch-search-date-restrict to 'S' Jameson Rollins
  1 sibling, 0 replies; 3+ messages in thread
From: Jameson Rollins @ 2010-12-20  3:54 UTC (permalink / raw)
  To: Notmuch Mail

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

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] bind notmuch-search-date-restrict to 'S'
  2010-12-20  3:54 new emacs function for date-restricted searches Jameson Rollins
  2010-12-20  3:54 ` [PATCH 1/2] emacs function to perform a search with a look back time restriction Jameson Rollins
@ 2010-12-20  3:54 ` Jameson Rollins
  1 sibling, 0 replies; 3+ messages in thread
From: Jameson Rollins @ 2010-12-20  3:54 UTC (permalink / raw)
  To: Notmuch Mail

---
 emacs/notmuch-show.el |    1 +
 emacs/notmuch.el      |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index 3a60d43..e1ace7c 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -579,6 +579,7 @@ function is used. "
 	(define-key map (kbd "<backtab>") 'notmuch-show-previous-button)
 	(define-key map (kbd "TAB") 'notmuch-show-next-button)
 	(define-key map "s" 'notmuch-search)
+	(define-key map "S" 'notmuch-search-date-restrict)
 	(define-key map "m" 'notmuch-mua-mail)
 	(define-key map "f" 'notmuch-show-forward-message)
 	(define-key map "r" 'notmuch-show-reply)
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 763d517..af5e310 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -206,6 +206,7 @@ For a mouse binding, return nil."
     (define-key map "r" 'notmuch-search-reply-to-thread)
     (define-key map "m" 'notmuch-mua-mail)
     (define-key map "s" 'notmuch-search)
+    (define-key map "S" 'notmuch-search-date-restrict)
     (define-key map "o" 'notmuch-search-toggle-order)
     (define-key map "c" 'notmuch-search-stash-map)
     (define-key map "=" 'notmuch-search-refresh-view)
-- 
1.7.2.3

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-12-20  3:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-20  3:54 new emacs function for date-restricted searches Jameson Rollins
2010-12-20  3:54 ` [PATCH 1/2] emacs function to perform a search with a look back time restriction Jameson Rollins
2010-12-20  3:54 ` [PATCH 2/2] bind notmuch-search-date-restrict to 'S' Jameson Rollins

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).