org-mairix.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. ;;; org-mairix.el - Support for hooking mairix search into Org for different MUAs
  2. ;;
  3. ;; Copyright (C) 2007-2012 Georg C. F. Greve
  4. ;; mutt support by Adam Spiers <orgmode at adamspiers dot org>
  5. ;;
  6. ;; Author: Georg C. F. Greve <greve at fsfeurope dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp, email, mairix
  8. ;; Purpose: Integrate mairix email searching into Org mode
  9. ;; See http://orgmode.org and http://www.rpcurnow.force9.co.uk/mairix/
  10. ;; Version: 0.5
  11. ;;
  12. ;; This file is Free Software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; It is distributed in the hope that it will be useful, but WITHOUT
  17. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  18. ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  19. ;; License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;; USAGE NOTE
  26. ;;
  27. ;; You will need to configure mairix first, which involves setting up your
  28. ;; .mairixrc in your home directory. Once it is working, you should set up
  29. ;; your way to display results in your favorite way -- usually a MUA.
  30. ;; Currently gnus and mutt are supported.
  31. ;;
  32. ;; After both steps are done, all you should need to hook mairix, org
  33. ;; and your MUA together is to do (require 'org-mairix) in your
  34. ;; startup file. Everything can then be configured normally through
  35. ;; Emacs customisation.
  36. ;;
  37. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  38. (require 'org)
  39. ;;; The custom variables
  40. (defgroup org-mairix nil
  41. "Mairix support/integration in org."
  42. :tag "Org Mairix"
  43. :group 'org)
  44. (defcustom org-mairix-threaded-links t
  45. "Should new links be created as threaded links?
  46. If t, links will be stored as threaded searches.
  47. If nil, links will be stored as non-threaded searches."
  48. :group 'org-mairix
  49. :type 'boolean)
  50. (defcustom org-mairix-augmented-links nil
  51. "Should new links be created as augmenting searches?
  52. If t, links will be stored as augmenting searches.
  53. If nil, links will be stored as normal searches.
  54. Attention: When activating this option, you will need
  55. to remove old articles from your mairix results group
  56. in some other way, mairix will not do it for you."
  57. :group 'org-mairix
  58. :type 'boolean)
  59. (defcustom org-mairix-display-hook 'org-mairix-gnus-display-results
  60. "Hook to call to display the results of a successful mairix search.
  61. Defaults to Gnus, feel free to add your own MUAs or methods."
  62. :group 'org-mairix
  63. :type 'hook)
  64. (defcustom org-mairix-open-command "mairix %args% '%search%'"
  65. "The mairix command-line to use. If your paths are set up
  66. correctly, you should not need to change this.
  67. '%search%' will get substituted with the search expression, and
  68. '%args%' with any additional arguments."
  69. :group 'org-mairix
  70. :type 'string)
  71. ;;; The hooks to integrate mairix into org
  72. (org-add-link-type "mairix" 'org-mairix-open)
  73. (add-hook 'org-store-link-functions 'org-mairix-store-gnus-link)
  74. ;;; Generic org-mairix functions
  75. (defun org-mairix-construct-link (message-id)
  76. "Construct a mairix: hyperlink based on message-id."
  77. (concat "mairix:"
  78. (if org-mairix-threaded-links "t:")
  79. (if org-mairix-augmented-links "a:")
  80. "@@"
  81. (org-remove-angle-brackets message-id)))
  82. (defun org-store-mairix-link-props (&rest plist)
  83. "Take a property list describing a mail, and add mairix link
  84. and description properties so that org can build a mairix link to
  85. it."
  86. ;; We have to call `org-store-link-props' twice:
  87. ;;
  88. ;; - It extracts 'fromname'/'fromaddress' from 'from' property,
  89. ;; and stores the updated plist to `org-store-link-plist'.
  90. ;;
  91. ;; - `org-email-link-description' uses these new properties to
  92. ;; build a description from the previously stored plist. I
  93. ;; wrote a tiny patch to `org-email-link-description' so it
  94. ;; could take a non-stored plist as an optional 2nd argument,
  95. ;; but the plist provided still needs 'fromname'/'fromaddress'.
  96. ;;
  97. ;; - Ideally we would decouple the storing bit of
  98. ;; `org-store-link-props' from the extraction bit, but lots of
  99. ;; stuff in `org-store-link' which calls it would need to be
  100. ;; changed. Maybe just factor out the extraction so it can be
  101. ;; reused separately?
  102. (let ((mid (plist-get plist :message-id)))
  103. (apply 'org-store-link-props
  104. (append plist
  105. (list :type "mairix"
  106. :link (org-mairix-construct-link mid))))
  107. (apply 'org-store-link-props
  108. (append org-store-link-plist
  109. (list :description (org-email-link-description))))))
  110. (defun org-mairix-message-send-and-exit-with-link ()
  111. "Function that can be assigned as an alternative sending function,
  112. it sends the message and then stores a mairix link to it before burying
  113. the buffer just like 'message-send-and-exit' does."
  114. (interactive)
  115. (message-send)
  116. (let* ((message-id (message-fetch-field "Message-Id"))
  117. (subject (message-fetch-field "Subject"))
  118. (link (org-mairix-construct-link message-id))
  119. (desc (concat "Email: '" subject "'")))
  120. (setq org-stored-links
  121. (cons (list link desc) org-stored-links)))
  122. (message-bury (current-buffer)))
  123. (defun org-mairix-open (search)
  124. "Function to open mairix link.
  125. We first need to split it into its individual parts, and then
  126. extract the message-id to be passed on to the display function
  127. before call mairix, evaluate the number of matches returned, and
  128. make sure to only call display of mairix succeeded in matching."
  129. (let* ((args ""))
  130. (if (equal (substring search 0 2) "t:" )
  131. (progn (setq search (substring search 2 nil))
  132. (setq args (concat args " --threads"))))
  133. (if (equal (substring search 0 2) "a:")
  134. (progn (setq search (substring search 2 nil))
  135. (setq args (concat args " --augment"))))
  136. (let ((cmdline (org-mairix-command-substitution
  137. org-mairix-open-command search args)))
  138. (print cmdline)
  139. (setq retval (shell-command-to-string cmdline))
  140. (string-match "\[0-9\]+" retval)
  141. (setq matches (string-to-number (match-string 0 retval)))
  142. (if (eq matches 0) (message "Link failed: no matches, sorry")
  143. (message "Link returned %d matches" matches)
  144. (run-hook-with-args 'org-mairix-display-hook search args)))))
  145. (defun org-mairix-command-substitution (cmd search args)
  146. "Substitute '%search%' and '%args% in mairix search command."
  147. (while (string-match "%search%" cmd)
  148. (setq cmd (replace-match search 'fixedcase 'literal cmd)))
  149. (while (string-match "%args%" cmd)
  150. (setq cmd (replace-match args 'fixedcase 'literal cmd)))
  151. cmd)
  152. ;;; Functions necessary for integration of external MUAs.
  153. ;; Of course we cannot call `org-store-link' from within an external
  154. ;; MUA, so we need some other way of storing a link for later
  155. ;; retrieval by org-mode and/or remember-mode. To do this we use a
  156. ;; temporary file as a kind of dedicated clipboard.
  157. (defcustom org-mairix-link-clipboard "~/.org-mairix-link"
  158. "Pseudo-clipboard file where mairix URLs get copied to by external
  159. applications in order to mimic `org-store-link'. Used by
  160. `org-mairix-insert-link'."
  161. :group 'org-mairix
  162. :type 'string)
  163. ;; When we resolve some of the issues with `org-store-link' detailed
  164. ;; at <http://thread.gmane.org/gmane.emacs.orgmode/4217/focus=4635>,
  165. ;; we might not need org-mairix-insert-link.
  166. (defun org-mairix-insert-link ()
  167. "Insert link from file defined by `org-mairix-link-clipboard'."
  168. (interactive)
  169. (let ((bytes (cadr (insert-file-contents
  170. (expand-file-name org-mairix-link-clipboard)))))
  171. (forward-char bytes)
  172. (save-excursion
  173. (forward-char -1)
  174. (if (looking-at "\n")
  175. (delete-char 1)))))
  176. ;;; Functions necessary for mutt integration
  177. (defgroup org-mairix-mutt nil
  178. "Use mutt for mairix support in org."
  179. :tag "Org Mairix Mutt"
  180. :group 'org-mairix)
  181. (defcustom org-mairix-mutt-display-command
  182. "xterm -title 'mairix search: %search%' -e 'unset COLUMNS; mutt -f
  183. ~/mail/mairix -e \"push <display-message>\"' &"
  184. "Command to execute to display mairix search results via mutt within
  185. an xterm.
  186. '%search%' will get substituted with the search expression, and
  187. '%args%' with any additional arguments used in the search."
  188. :group 'org-mairix-mutt
  189. :type 'string)
  190. (defun org-mairix-mutt-display-results (search args)
  191. "Display results of mairix search in mutt, using the command line
  192. defined in `org-mairix-mutt-display-command'."
  193. ;; By default, async `shell-command' invocations display the temp
  194. ;; buffer, which is annoying here. We choose a deterministic
  195. ;; buffer name so we can hide it again immediately.
  196. ;; Note: `call-process' is synchronous so not useful here.
  197. (let ((cmd (org-mairix-command-substitution
  198. org-mairix-mutt-display-command search args))
  199. (tmpbufname (generate-new-buffer-name " *mairix-view*")))
  200. (shell-command cmd tmpbufname)
  201. (delete-windows-on (get-buffer tmpbufname))))
  202. ;;; Functions necessary for gnus integration
  203. (defgroup org-mairix-gnus nil
  204. "Use gnus for mairix support in org."
  205. :tag "Org Mairix Gnus"
  206. :group 'org-mairix)
  207. (defcustom org-mairix-gnus-results-group "nnmaildir:mairix"
  208. "The group that is configured to hold the mairix search results,
  209. which needs to be setup independently of the org-mairix integration,
  210. along with general mairix configuration."
  211. :group 'org-mairix-gnus
  212. :type 'string)
  213. (defcustom org-mairix-gnus-select-display-group-function
  214. 'org-mairix-gnus-select-display-group-function-gg
  215. "Hook to call to select the group that contains the matching articles.
  216. We should not need this, it is owed to a problem of gnus that people were
  217. not yet able to figure out, see
  218. http://article.gmane.org/gmane.emacs.gnus.general/65248
  219. http://article.gmane.org/gmane.emacs.gnus.general/65265
  220. http://article.gmane.org/gmane.emacs.gnus.user/9596
  221. for reference.
  222. It seems gnus needs a 'forget/ignore everything you think you
  223. know about that group' function. Volunteers?"
  224. :group 'org-mairix-gnus
  225. :type 'hook)
  226. (defun org-mairix-store-gnus-link ()
  227. "Store a link to the current gnus message as a Mairix search for its
  228. Message ID."
  229. ;; gnus integration
  230. (when (memq major-mode '(gnus-summary-mode gnus-article-mode))
  231. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  232. (let* ((article (gnus-summary-article-number))
  233. (header (gnus-summary-article-header article))
  234. (from (mail-header-from header))
  235. (message-id (mail-header-id header))
  236. (subject (gnus-summary-subject-string)))
  237. (org-store-mairix-link-props :from from
  238. :subject subject
  239. :message-id message-id))))
  240. (defun org-mairix-gnus-display-results (search args)
  241. "Display results of mairix search in Gnus.
  242. Note: This does not work as cleanly as I would like it to. The
  243. problem being that Gnus should simply reread the group cleanly,
  244. without remembering anything. At the moment it seems to be unable
  245. to do that -- so you're likely to see zombies floating around.
  246. If you can improve this, please do!"
  247. (if (not (equal (substring search 0 2) "m:" ))
  248. (error "org-mairix-gnus-display-results: display of search other than
  249. message-id not implemented yet"))
  250. (setq message-id (substring search 2 nil))
  251. (require 'gnus)
  252. (require 'gnus-sum)
  253. ;; FIXME: (bzg/gg) We might need to make sure gnus is running here,
  254. ;; and to start it in case it isn't running already. Does
  255. ;; anyone know a function to do that? It seems main org mode
  256. ;; does not do this, either.
  257. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  258. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  259. ;; FIXME: This is horribly broken. Please see
  260. ;; http://article.gmane.org/gmane.emacs.gnus.general/65248
  261. ;; http://article.gmane.org/gmane.emacs.gnus.general/65265
  262. ;; http://article.gmane.org/gmane.emacs.gnus.user/9596
  263. ;; for reference.
  264. ;;
  265. ;; It seems gnus needs a "forget/ignore everything you think you
  266. ;; know about that group" function. Volunteers?
  267. ;;
  268. ;; For now different methods seem to work differently well for
  269. ;; different people. So we're playing hook-selection here to make
  270. ;; it easy to play around until we found a proper solution.
  271. (run-hook-with-args 'org-mairix-gnus-select-display-group-function)
  272. (gnus-summary-select-article
  273. nil t t (car (gnus-find-matching-articles "message-id" message-id))))
  274. (defun org-mairix-gnus-select-display-group-function-gg ()
  275. "Georg's hack to select a group that gnus (falsely) believes to be
  276. empty to then call rebuilding of the summary. It leaves zombies of
  277. old searches around, though."
  278. (gnus-group-quick-select-group 0 org-mairix-gnus-results-group)
  279. (gnus-group-clear-data)
  280. (gnus-summary-reselect-current-group t t))
  281. (defun org-mairix-gnus-select-display-group-function-bzg ()
  282. "This is the classic way the org mode is using, and it seems to be
  283. using better for Bastien, so it may work for you."
  284. (gnus-group-clear-data org-mairix-gnus-results-group)
  285. (gnus-group-read-group t nil org-mairix-gnus-results-group))
  286. (provide 'org-mairix)
  287. ;;; org-mairix.el ends here