org-notmuch.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ;;; org-notmuch.el --- Support for links to notmuch messages from within Org-mode
  2. ;; Copyright (C) 2010-2013 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. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This file implements links to notmuch messages and "searchs". A
  19. ;; search is a query to be performed by notmuch; it is the equivalent
  20. ;; to folders in other mail clients. Similarly, mails are refered to
  21. ;; by a query, so both a link can refer to several mails.
  22. ;; Links have one the following form
  23. ;; notmuch:<search terms>
  24. ;; notmuch-search:<search terms>.
  25. ;; The first form open the queries in notmuch-show mode, whereas the
  26. ;; second link open it in notmuch-search mode. Note that queries are
  27. ;; performed at the time the link is opened, and the result may be
  28. ;; different from whet the link was stored.
  29. ;;; Code:
  30. (require 'org)
  31. ;; Install the link type
  32. (org-add-link-type "notmuch" 'org-notmuch-open)
  33. (add-hook 'org-store-link-functions 'org-notmuch-store-link)
  34. (defun org-notmuch-store-link ()
  35. "Store a link to a notmuch search or message."
  36. (when (eq major-mode 'notmuch-show-mode)
  37. (let* ((message-id (notmuch-show-get-prop :id))
  38. (subject (notmuch-show-get-subject))
  39. (to (notmuch-show-get-to))
  40. (from (notmuch-show-get-from))
  41. desc link)
  42. (org-store-link-props :type "notmuch" :from from :to to
  43. :subject subject :message-id message-id)
  44. (setq desc (org-email-link-description))
  45. (setq link (concat "notmuch:" "id:" message-id))
  46. (org-add-link-props :link link :description desc)
  47. link)))
  48. (defun org-notmuch-open (path)
  49. "Follow a notmuch message link specified by PATH."
  50. (org-notmuch-follow-link path))
  51. (defun org-notmuch-follow-link (search)
  52. "Follow a notmuch link to SEARCH.
  53. Can link to more than one message, if so all matching messages are shown."
  54. (require 'notmuch)
  55. (notmuch-show (org-link-unescape search)))
  56. (org-add-link-type "notmuch-search" 'org-notmuch-search-open)
  57. (add-hook 'org-store-link-functions 'org-notmuch-search-store-link)
  58. (defun org-notmuch-search-store-link ()
  59. "Store a link to a notmuch search or message."
  60. (when (eq major-mode 'notmuch-search-mode)
  61. (let ((link (concat "notmuch-search:"
  62. (org-link-escape notmuch-search-query-string)))
  63. (desc (concat "Notmuch search: " notmuch-search-query-string)))
  64. (org-store-link-props :type "notmuch-search"
  65. :link link
  66. :description desc)
  67. link)))
  68. (defun org-notmuch-search-open (path)
  69. "Follow a notmuch message link specified by PATH."
  70. (message path)
  71. (org-notmuch-search-follow-link path))
  72. (defun org-notmuch-search-follow-link (search)
  73. "Follow a notmuch link by displaying SEARCH in notmuch-search mode."
  74. (require 'notmuch)
  75. (notmuch-search (org-link-unescape search)))
  76. (provide 'org-notmuch)
  77. ;;; org-notmuch.el ends here