Gnus development mailing list
 help / color / mirror / Atom feed
From: Kan-Ru Chen <kanru@kanru.info>
To: ding@gnus.org
Cc: Andrew Cohen <cohen@andy.bu.edu>, Kan-Ru Chen <kanru@kanru.info>
Subject: [PATCH 2/2] nnir.el: New `notmuch' backend.
Date: Sun,  3 Apr 2011 22:48:25 +0800	[thread overview]
Message-ID: <1301842105-18495-2-git-send-email-kanru@kanru.info> (raw)
In-Reply-To: <1301842105-18495-1-git-send-email-kanru@kanru.info>

Signed-off-by: Kan-Ru Chen <kanru@kanru.info>
---
 lisp/ChangeLog |    6 +++
 lisp/nnir.el   |  101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+), 0 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 4a802c5..30fc228 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,11 @@
 2011-04-03  Kan-Ru Chen  <kanru@kanru.info>
 
+	* nnir.el (nnir-notmuch-program, nnir-notmuch-additional-switches)
+	(nnir-notmuch-remove-prefix, nnir-engines, nnir-run-notmuch): New nnir
+	`notmuch' backend.
+
+2011-04-03  Kan-Ru Chen  <kanru@kanru.info>
+
 	* nnir.el (nnir-read-server-parm): Use default value.
 
 2011-04-02  Chong Yidong  <cyd@stupidchicken.com>
diff --git a/lisp/nnir.el b/lisp/nnir.el
index 6f3cd9e..a2a56a8 100644
--- a/lisp/nnir.el
+++ b/lisp/nnir.el
@@ -499,6 +499,31 @@ arrive at the correct group name, \"mail.misc\"."
   :type '(directory)
   :group 'nnir)
 
+(defcustom nnir-notmuch-program "notmuch"
+  "*Name of notmuch search executable."
+  :type '(string)
+  :group 'nnir)
+
+(defcustom nnir-notmuch-additional-switches '()
+  "*A list of strings, to be given as additional arguments to notmuch.
+
+Note that this should be a list.  Ie, do NOT use the following:
+    (setq nnir-notmuch-additional-switches \"-i -w\") ; wrong
+Instead, use this:
+    (setq nnir-notmuch-additional-switches '(\"-i\" \"-w\"))"
+  :type '(repeat (string))
+  :group 'nnir)
+
+(defcustom nnir-notmuch-remove-prefix (concat (getenv "HOME") "/Mail/")
+  "*The prefix to remove from each file name returned by notmuch
+in order to get a group name (albeit with / instead of .).  This is a
+regular expression.
+
+This variable is very similar to `nnir-namazu-remove-prefix', except
+that it is for notmuch, not Namazu."
+  :type '(regexp)
+  :group 'nnir)
+
 ;;; Developer Extension Variable:
 
 (defvar nnir-engines
@@ -519,6 +544,8 @@ arrive at the correct group name, \"mail.misc\"."
              ((group . "Swish-e Group spec: ")))
     (namazu  nnir-run-namazu
              ())
+    (notmuch nnir-run-notmuch
+             ())
     (hyrex   nnir-run-hyrex
 	     ((group . "Hyrex Group spec: ")))
     (find-grep nnir-run-find-grep
@@ -1317,6 +1344,80 @@ Tested with Namazu 2.0.6 on a GNU/Linux system."
                                (> (nnir-artitem-rsv x)
                                   (nnir-artitem-rsv y)))))))))
 
+(defun nnir-run-notmuch (query server &optional group)
+  "Run QUERY against notmuch.
+Returns a vector of (group name, file name) pairs (also vectors,
+actually)."
+
+  ;; (when group
+  ;;   (error "The notmuch backend cannot search specific groups"))
+
+  (save-excursion
+    (let ( (qstring (cdr (assq 'query query)))
+	   (groupspec (cdr (assq 'group query)))
+	   (prefix (nnir-read-server-parm 'nnir-notmuch-remove-prefix server))
+           artlist
+           (article-pattern (if (string= (gnus-group-server server) "nnmaildir")
+			       ":[0-9]+"
+			     "^[0-9]+$"))
+           artno dirnam filenam)
+
+      (when (equal "" qstring)
+        (error "notmuch: You didn't enter anything"))
+
+      (set-buffer (get-buffer-create nnir-tmp-buffer))
+      (erase-buffer)
+
+      (if groupspec
+          (message "Doing notmuch query %s on %s..." qstring groupspec)
+        (message "Doing notmuch query %s..." qstring))
+
+      (let* ((cp-list `( ,nnir-notmuch-program
+                         nil            ; input from /dev/null
+                         t              ; output
+                         nil            ; don't redisplay
+                         "search"
+                         "--format=text"
+                         "--output=files"
+                         ,@(nnir-read-server-parm 'nnir-notmuch-additional-switches server)
+                         ,qstring       ; the query, in notmuch format
+                         ))
+             (exitstatus
+              (progn
+                (message "%s args: %s" nnir-notmuch-program
+                         (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
+                (apply 'call-process cp-list))))
+        (unless (or (null exitstatus)
+                    (zerop exitstatus))
+          (nnheader-report 'nnir "Couldn't run notmuch: %s" exitstatus)
+          ;; notmuch failure reason is in this buffer, show it if
+          ;; the user wants it.
+          (when (> gnus-verbose 6)
+            (display-buffer nnir-tmp-buffer))))
+
+      ;; The results are output in the format of:
+      ;; absolute-path-name
+      (goto-char (point-min))
+      (while (not (eobp))
+        (setq filenam (buffer-substring-no-properties (line-beginning-position)
+                                                      (line-end-position))
+              artno (file-name-nondirectory filenam)
+              dirnam (file-name-directory filenam))
+        (forward-line 1)
+
+        ;; don't match directories
+        (when (string-match article-pattern artno)
+          (when (not (null dirnam))
+
+	    ;; maybe limit results to matching groups.
+	    (when (or (not groupspec)
+		      (string-match groupspec dirnam))
+	      (nnir-add-result dirnam artno "" prefix server artlist)))))
+
+      (message "Massaging notmuch output...done")
+
+      artlist)))
+
 (defun nnir-run-find-grep (query server &optional grouplist)
   "Run find and grep to obtain matching articles."
   (let* ((method (gnus-server-to-method server))
-- 
1.7.4.1




  reply	other threads:[~2011-04-03 14:48 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-03 14:48 [PATCH 1/2] nnir.el (nnir-read-server-parm): Use default value Kan-Ru Chen
2011-04-03 14:48 ` Kan-Ru Chen [this message]
2011-04-03 17:43   ` [PATCH 2/2] nnir.el: New `notmuch' backend Lars Magne Ingebrigtsen
2011-04-04  1:04     ` Kan-Ru Chen
2011-04-04 13:43     ` Ted Zlatanov
2011-04-05  1:02       ` Kan-Ru Chen
2011-04-05 14:34         ` Ted Zlatanov
2011-04-06  6:08           ` Erik Colson
2011-04-06 13:22             ` Ted Zlatanov
2011-04-06  8:12           ` Kan-Ru Chen
2011-04-06  9:30       ` Julien Danjou
2011-07-04 16:09     ` Kan-Ru Chen
2011-07-05 20:22       ` Lars Magne Ingebrigtsen
2013-01-09 16:40         ` Gour
2013-08-01 22:14           ` Lars Magne Ingebrigtsen
2013-08-02  1:28             ` Kan-Ru Chen (陳侃如)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1301842105-18495-2-git-send-email-kanru@kanru.info \
    --to=kanru@kanru.info \
    --cc=cohen@andy.bu.edu \
    --cc=ding@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).