org-notmuch.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ;;; org-notmuch.el --- Support for links to notmuch messages from within Org-mode
  2. ;; Copyright (C) 2010-2012 Matthieu Lemerre
  3. ;; Author: Matthieu Lemerre <racin@free.fr>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is not part of GNU Emacs.
  7. ;; This file is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11. ;; This file is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs; see the file COPYING. If not, write to
  17. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. ;; Boston, MA 02110-1301, USA.
  19. ;;; Commentary:
  20. ;; This file implements links to notmuch messages and "searchs". A
  21. ;; search is a query to be performed by notmuch; it is the equivalent
  22. ;; to folders in other mail clients. Similarly, mails are refered to
  23. ;; by a query, so both a link can refer to several mails.
  24. ;; Links have one the following form
  25. ;; notmuch:<search terms>
  26. ;; notmuch-search:<search terms>.
  27. ;; The first form open the queries in notmuch-show mode, whereas the
  28. ;; second link open it in notmuch-search mode. Note that queries are
  29. ;; performed at the time the link is opened, and the result may be
  30. ;; different from whet the link was stored.
  31. ;;; Code:
  32. (require 'org)
  33. ;; Install the link type
  34. (org-add-link-type "notmuch" 'org-notmuch-open)
  35. (add-hook 'org-store-link-functions 'org-notmuch-store-link)
  36. (defun org-notmuch-store-link ()
  37. "Store a link to a notmuch search or message."
  38. (when (eq major-mode 'notmuch-show-mode)
  39. (let* ((message-id (notmuch-show-get-prop :id))
  40. (subject (notmuch-show-get-subject))
  41. (to (notmuch-show-get-to))
  42. (from (notmuch-show-get-from))
  43. desc link)
  44. (org-store-link-props :type "notmuch" :from from :to to
  45. :subject subject :message-id message-id)
  46. (setq desc (org-email-link-description))
  47. (setq link (org-make-link "notmuch:" "id:" message-id))
  48. (org-add-link-props :link link :description desc)
  49. link)))
  50. (defun org-notmuch-open (path)
  51. "Follow a notmuch message link specified by PATH."
  52. (org-notmuch-follow-link path))
  53. (defun org-notmuch-follow-link (search)
  54. "Follow a notmuch link to SEARCH.
  55. Can link to more than one message, if so all matching messages are shown."
  56. (require 'notmuch)
  57. (notmuch-show (org-link-unescape search)))
  58. (org-add-link-type "notmuch-search" 'org-notmuch-search-open)
  59. (add-hook 'org-store-link-functions 'org-notmuch-search-store-link)
  60. (defun org-notmuch-search-store-link ()
  61. "Store a link to a notmuch search or message."
  62. (when (eq major-mode 'notmuch-search-mode)
  63. (let ((link (org-make-link "notmuch-search:"
  64. (org-link-escape notmuch-search-query-string)))
  65. (desc (concat "Notmuch search: " notmuch-search-query-string)))
  66. (org-store-link-props :type "notmuch-search"
  67. :link link
  68. :description desc)
  69. link)))
  70. (defun org-notmuch-search-open (path)
  71. "Follow a notmuch message link specified by PATH."
  72. (message path)
  73. (org-notmuch-search-follow-link path))
  74. (defun org-notmuch-search-follow-link (search)
  75. "Follow a notmuch link by displaying SEARCH in notmuch-search mode."
  76. (require 'notmuch)
  77. (notmuch-search (org-link-unescape search)))
  78. (provide 'org-notmuch)
  79. ;;; org-notmuch.el ends here