org-mhe.el 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ;;; org-mhe.el - Support for links to MHE messages in Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 1.0
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 3, or (at your option)
  13. ;; any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file implements links to MHE messages for Org-mode.
  26. ;; Org-mode loads this module by default - if this is not what you want,
  27. ;; configure the variable `org-modules'.
  28. (require 'org)
  29. ;; Customization variables
  30. (defcustom org-mhe-search-all-folders nil
  31. "Non-nil means, that the search for the mh-message will be extended to
  32. all folders if the message cannot be found in the folder given in the link.
  33. Searching all folders is very efficient with one of the search engines
  34. supported by MH-E, but will be slow with pick."
  35. :group 'org-link-follow
  36. :type 'boolean)
  37. ;; Declare external functions and variables
  38. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  39. (declare-function mh-find-path "mh-utils" ())
  40. (declare-function mh-get-header-field "mh-utils" (field))
  41. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  42. (declare-function mh-header-display "mh-show" ())
  43. (declare-function mh-index-previous-folder "mh-search" ())
  44. (declare-function mh-normalize-folder-name "mh-utils"
  45. (folder &optional empty-string-okay dont-remove-trailing-slash
  46. return-nil-if-folder-empty))
  47. (declare-function mh-search "mh-search"
  48. (folder search-regexp &optional redo-search-flag
  49. window-config))
  50. (declare-function mh-search-choose "mh-search" (&optional searcher))
  51. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  52. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  53. (declare-function mh-show-header-display "mh-show" t t)
  54. (declare-function mh-show-msg "mh-show" (msg))
  55. (declare-function mh-show-show "mh-show" t t)
  56. (declare-function mh-visit-folder "mh-folder" (folder &optional
  57. range index-data))
  58. (defvar mh-progs)
  59. (defvar mh-current-folder)
  60. (defvar mh-show-folder-buffer)
  61. (defvar mh-index-folder)
  62. (defvar mh-searcher)
  63. ;; Install the link type
  64. (org-add-link-type "mhe" 'org-mhe-open)
  65. (add-hook 'org-store-link-functions 'org-mhe-store-link)
  66. ;; Implementation
  67. (defun org-mhe-store-link ()
  68. "Store a link to an MHE folder or message."
  69. (when (or (equal major-mode 'mh-folder-mode)
  70. (equal major-mode 'mh-show-mode))
  71. (let ((from (org-mhe-get-header "From:"))
  72. (to (org-mhe-get-header "To:"))
  73. (message-id (org-mhe-get-header "Message-Id:"))
  74. (subject (org-mhe-get-header "Subject:"))
  75. link desc)
  76. (org-store-link-props :type "mh" :from from :to to
  77. :subject subject :message-id message-id)
  78. (setq desc (org-email-link-description))
  79. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  80. (org-remove-angle-brackets message-id)))
  81. (org-add-link-props :link link :description desc)
  82. link)))
  83. (defun org-mhe-open (path)
  84. "Follow an MHE message link."
  85. (let (folder article)
  86. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  87. (error "Error in MHE link"))
  88. (setq folder (match-string 1 path)
  89. article (match-string 3 path))
  90. (org-mhe-follow-link folder article)))
  91. ;;; mh-e integration based on planner-mode
  92. (defun org-mhe-get-message-real-folder ()
  93. "Return the name of the current message real folder, so if you use
  94. sequences, it will now work."
  95. (save-excursion
  96. (let* ((folder
  97. (if (equal major-mode 'mh-folder-mode)
  98. mh-current-folder
  99. ;; Refer to the show buffer
  100. mh-show-folder-buffer))
  101. (end-index
  102. (if (boundp 'mh-index-folder)
  103. (min (length mh-index-folder) (length folder))))
  104. )
  105. ;; a simple test on mh-index-data does not work, because
  106. ;; mh-index-data is always nil in a show buffer.
  107. (if (and (boundp 'mh-index-folder)
  108. (string= mh-index-folder (substring folder 0 end-index)))
  109. (if (equal major-mode 'mh-show-mode)
  110. (save-window-excursion
  111. (let (pop-up-frames)
  112. (when (buffer-live-p (get-buffer folder))
  113. (progn
  114. (pop-to-buffer folder)
  115. (org-mhe-get-message-folder-from-index)
  116. )
  117. )))
  118. (org-mhe-get-message-folder-from-index)
  119. )
  120. folder
  121. )
  122. )))
  123. (defun org-mhe-get-message-folder-from-index ()
  124. "Returns the name of the message folder in a index folder buffer."
  125. (save-excursion
  126. (mh-index-previous-folder)
  127. (re-search-forward "^\\(+.*\\)$" nil t)
  128. (message "%s" (match-string 1))))
  129. (defun org-mhe-get-message-folder ()
  130. "Return the name of the current message folder. Be careful if you
  131. use sequences."
  132. (save-excursion
  133. (if (equal major-mode 'mh-folder-mode)
  134. mh-current-folder
  135. ;; Refer to the show buffer
  136. mh-show-folder-buffer)))
  137. (defun org-mhe-get-message-num ()
  138. "Return the number of the current message. Be careful if you
  139. use sequences."
  140. (save-excursion
  141. (if (equal major-mode 'mh-folder-mode)
  142. (mh-get-msg-num nil)
  143. ;; Refer to the show buffer
  144. (mh-show-buffer-message-number))))
  145. (defun org-mhe-get-header (header)
  146. "Return a header of the message in folder mode. This will create a
  147. show buffer for the corresponding message. If you have a more clever
  148. idea..."
  149. (let* ((folder (org-mhe-get-message-folder))
  150. (num (org-mhe-get-message-num))
  151. (buffer (get-buffer-create (concat "show-" folder)))
  152. (header-field))
  153. (with-current-buffer buffer
  154. (mh-display-msg num folder)
  155. (if (equal major-mode 'mh-folder-mode)
  156. (mh-header-display)
  157. (mh-show-header-display))
  158. (set-buffer buffer)
  159. (setq header-field (mh-get-header-field header))
  160. (if (equal major-mode 'mh-folder-mode)
  161. (mh-show)
  162. (mh-show-show))
  163. header-field)))
  164. (defun org-mhe-follow-link (folder article)
  165. "Follow an MHE link to FOLDER and ARTICLE.
  166. If ARTICLE is nil FOLDER is shown. If the configuration variable
  167. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  168. ARTICLE is searched in all folders. Indexed searches (swish++,
  169. namazu, and others supported by MH-E) will always search in all
  170. folders."
  171. (require 'mh-e)
  172. (require 'mh-search)
  173. (require 'mh-utils)
  174. (mh-find-path)
  175. (if (not article)
  176. (mh-visit-folder (mh-normalize-folder-name folder))
  177. (setq article (org-add-angle-brackets article))
  178. (mh-search-choose)
  179. (if (equal mh-searcher 'pick)
  180. (progn
  181. (mh-search folder (list "--message-id" article))
  182. (when (and org-mhe-search-all-folders
  183. (not (org-mhe-get-message-real-folder)))
  184. (kill-this-buffer)
  185. (mh-search "+" (list "--message-id" article))))
  186. (mh-search "+" article))
  187. (if (org-mhe-get-message-real-folder)
  188. (mh-show-msg 1)
  189. (kill-this-buffer)
  190. (error "Message not found"))))
  191. (provide 'org-mhe)
  192. ;;; org-mhe.el ends here