Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 4259B41A558 for ; Sun, 19 Dec 2010 19:54:47 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -2.3 X-Spam-Level: X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id OJZ-zuwRPG28 for ; Sun, 19 Dec 2010 19:54:46 -0800 (PST) Received: from serrano.cc.columbia.edu (serrano.cc.columbia.edu [128.59.29.6]) by olra.theworths.org (Postfix) with ESMTP id 76971431FB5 for ; Sun, 19 Dec 2010 19:54:46 -0800 (PST) Received: from servo.finestructure.net (cpe-74-66-82-137.nyc.res.rr.com [74.66.82.137]) (user=jgr2110 author=jrollins@finestructure.net mech=PLAIN bits=0) by serrano.cc.columbia.edu (8.14.4/8.14.3) with ESMTP id oBK3siom009683 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT) for ; Sun, 19 Dec 2010 22:54:45 -0500 (EST) Received: from jrollins by servo.finestructure.net with local (Exim 4.72) (envelope-from ) id 1PUWpg-00066I-9C; Sun, 19 Dec 2010 22:54:44 -0500 From: Jameson Rollins To: Notmuch Mail Subject: [PATCH 1/2] emacs function to perform a search with a look back time restriction Date: Sun, 19 Dec 2010 22:54:39 -0500 Message-Id: <1292817280-20999-2-git-send-email-jrollins@finestructure.net> X-Mailer: git-send-email 1.7.2.3 In-Reply-To: <1292817280-20999-1-git-send-email-jrollins@finestructure.net> References: <1292817280-20999-1-git-send-email-jrollins@finestructure.net> X-No-Spam-Score: Local X-Scanned-By: MIMEDefang 2.68 on 128.59.29.6 X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Dec 2010 03:54:47 -0000 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