ol-notmuch.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ;;; ol-notmuch.el --- Links to notmuch messages
  2. ;; Copyright (C) 2010-2014 Matthieu Lemerre
  3. ;; Author: Matthieu Lemerre <racin@free.fr>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: https://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 <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This file implements links to notmuch messages and "searches". 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 referred 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 when the link was stored.
  29. ;;; Code:
  30. (require 'ol)
  31. ;; customisable notmuch open functions
  32. (defcustom org-notmuch-open-function
  33. 'org-notmuch-follow-link
  34. "Function used to follow notmuch links.
  35. Should accept a notmuch search string as the sole argument."
  36. :group 'org-notmuch
  37. :version "24.4"
  38. :package-version '(Org . "8.0")
  39. :type 'function)
  40. (defcustom org-notmuch-search-open-function
  41. 'org-notmuch-search-follow-link
  42. "Function used to follow notmuch-search links.
  43. Should accept a notmuch search string as the sole argument."
  44. :group 'org-notmuch
  45. :version "24.4"
  46. :package-version '(Org . "8.0")
  47. :type 'function)
  48. (make-obsolete-variable 'org-notmuch-search-open-function nil "9.3")
  49. ;; Install the link type
  50. (org-link-set-parameters "notmuch"
  51. :follow #'org-notmuch-open
  52. :store #'org-notmuch-store-link)
  53. (defun org-notmuch-store-link ()
  54. "Store a link to a notmuch search or message."
  55. (when (memq major-mode '(notmuch-show-mode notmuch-tree-mode))
  56. (let* ((message-id (notmuch-show-get-message-id t))
  57. (subject (notmuch-show-get-subject))
  58. (to (notmuch-show-get-to))
  59. (from (notmuch-show-get-from))
  60. (date (org-trim (notmuch-show-get-date)))
  61. desc link)
  62. (org-link-store-props :type "notmuch" :from from :to to :date date
  63. :subject subject :message-id message-id)
  64. (setq desc (org-link-email-description))
  65. (setq link (concat "notmuch:id:" message-id))
  66. (org-link-add-props :link link :description desc)
  67. link)))
  68. (defun org-notmuch-open (path _)
  69. "Follow a notmuch message link specified by PATH."
  70. (funcall org-notmuch-open-function path))
  71. (defun org-notmuch-follow-link (search)
  72. "Follow a notmuch link to SEARCH.
  73. Can link to more than one message, if so all matching messages are shown."
  74. (require 'notmuch)
  75. (notmuch-show search))
  76. (org-link-set-parameters "notmuch-search"
  77. :follow #'org-notmuch-search-open
  78. :store #'org-notmuch-search-store-link)
  79. (defun org-notmuch-search-store-link ()
  80. "Store a link to a notmuch search or message."
  81. (when (eq major-mode 'notmuch-search-mode)
  82. (let ((link (concat "notmuch-search:" notmuch-search-query-string))
  83. (desc (concat "Notmuch search: " notmuch-search-query-string)))
  84. (org-link-store-props :type "notmuch-search"
  85. :link link
  86. :description desc)
  87. link)))
  88. (defun org-notmuch-search-open (path _)
  89. "Follow a notmuch message link specified by PATH."
  90. (message "%s" path)
  91. (org-notmuch-search-follow-link path))
  92. (defun org-notmuch-search-follow-link (search)
  93. "Follow a notmuch link by displaying SEARCH in notmuch-search mode."
  94. (require 'notmuch)
  95. (notmuch-search search))
  96. (org-link-set-parameters "notmuch-tree"
  97. :follow #'org-notmuch-tree-open
  98. :store #'org-notmuch-tree-store-link)
  99. (defun org-notmuch-tree-store-link ()
  100. "Store a link to a notmuch search or message."
  101. (when (eq major-mode 'notmuch-tree-mode)
  102. (let ((link (concat "notmuch-tree:" (notmuch-tree-get-query)))
  103. (desc (concat "Notmuch tree: " (notmuch-tree-get-query))))
  104. (org-link-store-props :type "notmuch-tree"
  105. :link link
  106. :description desc)
  107. link)))
  108. (defun org-notmuch-tree-open (path _)
  109. "Follow a notmuch message link specified by PATH."
  110. (message "%s" path)
  111. (org-notmuch-tree-follow-link path))
  112. (defun org-notmuch-tree-follow-link (search)
  113. "Follow a notmuch link by displaying SEARCH in notmuch-tree mode."
  114. (require 'notmuch)
  115. (notmuch-tree search))
  116. (provide 'ol-notmuch)
  117. ;;; ol-notmuch.el ends here