org-mairix.el 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ;;; org-mairix.el ---
  2. ;; Copyright 2007 Bastien Guerry
  3. ;;
  4. ;; Author: Bastien.Guerry@ens.fr
  5. ;; Version: $Id: org-mairix.el,v 0.0 2007/08/11 17:23:40 guerry Exp $
  6. ;; Keywords:
  7. ;; X-URL: not distributed yet
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with this program; if not, write to the Free Software
  20. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. ;;; Commentary:
  22. ;; Code and ideas from Carsten Dominik, Adam Spiers and Georg C. F. Greve.
  23. ;; Put this file into your load-path and the following into your ~/.emacs:
  24. ;; (require 'org-mairix)
  25. ;;; Code:
  26. (require 'org)
  27. (defgroup org-mairix nil
  28. "Mairix link support for Org."
  29. :tag "Org Mairix"
  30. :group 'org)
  31. (defcustom mairix-results-group "nnmaildir+index:mfolder"
  32. "Gnus groupe where to list mairix search results."
  33. :group 'org-mairix
  34. :type '(string))
  35. (defun org-add-link-type (type &optional follow publish)
  36. "Add TYPE to the list of `org-link-types'.
  37. Re-compute all regular expressions depending on `org-link-types'."
  38. (add-to-list 'org-link-types type t)
  39. (setq org-link-re-with-space
  40. (concat
  41. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  42. "\\([^" org-non-link-chars " ]"
  43. "[^" org-non-link-chars "]*"
  44. "[^" org-non-link-chars " ]\\)>?"))
  45. (setq org-link-re-with-space2
  46. (concat
  47. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  48. "\\([^" org-non-link-chars " ]"
  49. "[^]\t\n\r]*"
  50. "[^" org-non-link-chars " ]\\)>?"))
  51. (setq org-angle-link-re
  52. (concat
  53. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  54. "\\([^" org-non-link-chars " ]"
  55. "[^" org-non-link-chars "]*"
  56. "\\)>"))
  57. (setq org-plain-link-re
  58. (concat
  59. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  60. "\\([^]\t\n\r<>,;() ]+\\)"))
  61. (setq org-bracket-link-analytic-regexp
  62. (concat
  63. "\\[\\["
  64. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  65. "\\([^]]+\\)"
  66. "\\]"
  67. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  68. "\\]"))
  69. (add-hook 'org-follow-link-functions follow)
  70. (add-hook 'org-publish-link-functions publish))
  71. (defun org-mairix-follow-link (path)
  72. "Follow a Mairix link."
  73. (require 'gnus)
  74. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  75. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  76. (mairix-search path))
  77. (defun org-mairix-publish-link (path)
  78. "Convert mairix PATH into a (dummy) raw link."
  79. ;; FIXME: should we have a format argument for HTML/LaTeX publishing?
  80. (if (string-match org-bracket-link-analytic-regexp path)
  81. (match-string 5 path) path))
  82. (defun org-mairix-store-link (path)
  83. "Store a mairix link."
  84. (when (memq major-mode '(gnus-summary-mode gnus-article-mode))
  85. (let* ((group gnus-newsgroup-name)
  86. (article (gnus-summary-article-number))
  87. (header (gnus-summary-article-header article))
  88. (from (mail-header-from header))
  89. (message-id (mail-header-id header))
  90. (date (mail-header-date header))
  91. (subject (gnus-summary-subject-string)))
  92. (org-store-link-props :type "mairix"
  93. :from from
  94. :subject subject
  95. :message-id message-id
  96. :group group)
  97. ;; FIXME: what about cpltxt and link vars we used so far?
  98. ;; (setq cpltxt (org-email-link-description))
  99. ;; (setq link (org-make-link "mairix:m:"
  100. ;; (substring message-id 1 -1))))))
  101. (org-make-link "mairix:m:" (substring message-id 1 -1)))))
  102. ;; mairix internals
  103. (defun mairix-result-evaluate (string)
  104. "Display search results of previous mairix process."
  105. (let ((mmatches (string-to-number (substring string 7 -8))))
  106. (if (eq mmatches 0)
  107. (message "Mairix returned no matches, sorry.")
  108. (message "Mairix returned %d matches." mmatches)
  109. (gnus-group-quick-select-group 0 mairix-results-group)
  110. (gnus-summary-reselect-current-group t t))))
  111. (org-add-link-type "mairix"
  112. 'org-mairix-follow-link
  113. 'org-mairix-publish-link)
  114. (add-hook 'org-store-link-functions 'org-mairix-store-link)
  115. (defun mairix-search (string)
  116. "Uses mairix to search through my mail, replacing current search results."
  117. (interactive "MMairix search: ")
  118. (mairix-result-evaluate
  119. (shell-command-to-string (concat "mairix " string))))
  120. (provide 'org-mairix)
  121. (eval-when-compile
  122. (require 'cl))
  123. ;;;;##########################################################################
  124. ;;;; User Options, Variables
  125. ;;;;##########################################################################
  126. ;;; org-mairix.el ends here