It requires a version of notmuch from git after April 5 2010.  The
code here is based on org-wl.el. One thing to note is that links to
threads are faked as a collection of message ids. This is because
notmuch thread-ids are currently not stable between dump/restore of
the database.
---
 lisp/org-notmuch.el |   87 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 87 insertions(+), 0 deletions(-)
 create mode 100644 lisp/org-notmuch.el

diff --git a/lisp/org-notmuch.el b/lisp/org-notmuch.el
new file mode 100644
index 0000000..82c31a5
--- /dev/null
+++ b/lisp/org-notmuch.el
@@ -0,0 +1,87 @@
+;;; org-notmuch.el --- Support for links to notmuch messages from within 
Org-mode
+;; Author: David Bremner <da...@tethera.net>
+;; License: GPL3+
+;;; Commentary:
+
+;; This file implements links to notmuch messages from within Org-mode.
+;; Link types supported include 
+;; - notmuch:id:message-id
+;; - notmuch:show:search-terms
+;; - notmuch:search:search-terms
+;;
+;; The latter two pass the search terms to the corresponding notmuch-*
+;; function.  'id:' is an abbreviation for 'show:id:' search-terms is
+;; a space delimited list of notmuch search-terms.
+;;
+;; Currently storing links is supported in notmuch-search and
+;; notmuch-show mode.  It might make sense to support notmuch-folder
+;; mode too.  Because threads-ids are currently not save/restore safe,
+;; they are converted into a list of message-ids.
+;;
+;; Org-mode loads this module by default - if this is not what you want,
+;; configure the variable `org-modules'.
+;;; Code:
+
+;; Install the link type
+(org-add-link-type "notmuch" 'org-notmuch-open)
+(add-hook 'org-store-link-functions 'org-notmuch-store-link)
+
+;; Configuration
+(defvar org-notmuch-mid-limit 10
+  "Maximum number of message ids to store for a thread")
+
+;; Implementation
+(defun org-notmuch-store-link ()
+  "Store a link to the currently selected thread."
+  (require 'notmuch)
+  (when (memq major-mode '(notmuch-show-mode notmuch-search-mode))
+    (if (equal major-mode 'notmuch-search-mode)
+       (org-notmuch-do-store-link 
+        (org-notmuch-build-thread-link)
+        (notmuch-search-find-authors)
+        (notmuch-search-find-subject))
+      (org-notmuch-do-store-link  (notmuch-show-get-message-id)
+                                 (notmuch-show-get-from)
+                                 (notmuch-show-get-subject)))))
+
+;; sigh. there doesn't seem to be such a function.
+(defun org-notmuch-n-first (list n)
+  (if (> n 0)
+      (cons (car list)
+           (org-notmuch-n-first (cdr list) (1- n)))
+    nil))
+
+(defun org-notmuch-build-thread-link ()
+  "Expand the thread-id on the current line to a list of message-ids"
+  (require 'notmuch-query)
+  (let* ((current-thread (or (notmuch-search-find-thread-id) 
+                           (error "End of search results")))
+        (message-ids 
+         (org-notmuch-n-first 
+          (notmuch-query-get-message-ids current-thread)
+          org-notmuch-mid-limit)))
+    (concat "show:" 
+           (mapconcat (lambda (id) (concat "id:" id)) message-ids " "))))
+
+(defun org-notmuch-do-store-link (id author subject)
+  (let ((link  (org-make-link "notmuch:" id)))
+    (org-store-link-props :type "notmuch" :from author :subject subject)
+    (org-add-link-props :link link :description (org-email-link-description))
+    link))
+
+
+(defun org-notmuch-open (link)
+  "Open a link with notmuch. id: or show: links are opened directly with 
notmuch-show
+otherwise notmuch-search is used to give an index view"
+  (require 'notmuch)
+  (cond 
+   ((string-match "^show:\\(.*\\)" link)
+    (notmuch-show (match-string 1 link)))
+   ((string-match "^search:\\(.*\\)" link)
+    (notmuch-search (match-string 1 link)))
+   ((string-match "^id:.*" link)
+    (notmuch-show link))
+   (t (notmuch-search link))))
+
+    
+(provide 'org-notmuch)
-- 
1.7.0



_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

Reply via email to