org-mhe.el 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. ;;; org-mhe.el --- Support for links to MH-E messages from within Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  3. ;; Author: Thomas Baumann <thomas dot baumann at ch dot tum dot de>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.13pre02
  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 of the License, or
  13. ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file implements links to MH-E messages from within Org-mode.
  24. ;; Org-mode loads this module by default - if this is not what you want,
  25. ;; configure the variable `org-modules'.
  26. ;;; Code:
  27. (require 'org)
  28. ;; Customization variables
  29. (defcustom org-mhe-search-all-folders nil
  30. "Non-nil means the search for the mh-message may extend to all folders.
  31. When non-nil, the search for a message will extend to all other
  32. folders if it cannot be found in the folder given in the link.
  33. Searching all folders may be slow with the default pick based
  34. search but is very efficient with one of the other search engines
  35. supported by MH-E."
  36. :group 'org-link-follow
  37. :type 'boolean)
  38. ;; Declare external functions and variables
  39. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  40. (declare-function mh-find-path "mh-utils" ())
  41. (declare-function mh-get-header-field "mh-utils" (field))
  42. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  43. (declare-function mh-header-display "mh-show" ())
  44. (declare-function mh-index-previous-folder "mh-search" ())
  45. (declare-function mh-normalize-folder-name "mh-utils"
  46. (folder &optional empty-string-okay dont-remove-trailing-slash
  47. return-nil-if-folder-empty))
  48. (declare-function mh-search "mh-search"
  49. (folder search-regexp &optional redo-search-flag
  50. window-config))
  51. (declare-function mh-search-choose "mh-search" (&optional searcher))
  52. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  53. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  54. (declare-function mh-show-header-display "mh-show" t t)
  55. (declare-function mh-show-msg "mh-show" (msg))
  56. (declare-function mh-show-show "mh-show" t t)
  57. (declare-function mh-visit-folder "mh-folder" (folder &optional
  58. range index-data))
  59. (defvar mh-progs)
  60. (defvar mh-current-folder)
  61. (defvar mh-show-folder-buffer)
  62. (defvar mh-index-folder)
  63. (defvar mh-searcher)
  64. (defvar mh-search-regexp-builder)
  65. ;; Install the link type
  66. (org-add-link-type "mhe" 'org-mhe-open)
  67. (add-hook 'org-store-link-functions 'org-mhe-store-link)
  68. ;; Implementation
  69. (defun org-mhe-store-link ()
  70. "Store a link to an MH-E folder or message."
  71. (when (or (equal major-mode 'mh-folder-mode)
  72. (equal major-mode 'mh-show-mode))
  73. (let ((from (org-mhe-get-header "From:"))
  74. (to (org-mhe-get-header "To:"))
  75. (message-id (org-mhe-get-header "Message-Id:"))
  76. (subject (org-mhe-get-header "Subject:"))
  77. link desc)
  78. (org-store-link-props :type "mh" :from from :to to
  79. :subject subject :message-id message-id)
  80. (setq desc (org-email-link-description))
  81. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  82. (org-remove-angle-brackets message-id)))
  83. (org-add-link-props :link link :description desc)
  84. link)))
  85. (defun org-mhe-open (path)
  86. "Follow an MH-E message link specified by PATH."
  87. (let (folder article)
  88. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  89. (error "Error in MH-E link"))
  90. (setq folder (match-string 1 path)
  91. article (match-string 3 path))
  92. (org-mhe-follow-link folder article)))
  93. ;;; mh-e integration based on planner-mode
  94. (defun org-mhe-get-message-real-folder ()
  95. "Return the name of the real folder for the current message.
  96. So if you use sequences, it will now work."
  97. (save-excursion
  98. (let* ((folder
  99. (if (equal major-mode 'mh-folder-mode)
  100. mh-current-folder
  101. ;; Refer to the show buffer
  102. mh-show-folder-buffer))
  103. (end-index
  104. (if (boundp 'mh-index-folder)
  105. (min (length mh-index-folder) (length folder))))
  106. )
  107. ;; a simple test on mh-index-data does not work, because
  108. ;; mh-index-data is always nil in a show buffer.
  109. (if (and (boundp 'mh-index-folder)
  110. (string= mh-index-folder (substring folder 0 end-index)))
  111. (if (equal major-mode 'mh-show-mode)
  112. (save-window-excursion
  113. (let (pop-up-frames)
  114. (when (buffer-live-p (get-buffer folder))
  115. (progn
  116. (pop-to-buffer folder)
  117. (org-mhe-get-message-folder-from-index)
  118. )
  119. )))
  120. (org-mhe-get-message-folder-from-index)
  121. )
  122. folder
  123. )
  124. )))
  125. (defun org-mhe-get-message-folder-from-index ()
  126. "Return the name of the message folder in an index folder buffer."
  127. (save-excursion
  128. (mh-index-previous-folder)
  129. (if (re-search-forward "^\\(+.*\\)$" nil t)
  130. (message "%s" (match-string 1)))))
  131. (defun org-mhe-get-message-folder ()
  132. "Return the name of the current message folder.
  133. Be careful if you use sequences."
  134. (save-excursion
  135. (if (equal major-mode 'mh-folder-mode)
  136. mh-current-folder
  137. ;; Refer to the show buffer
  138. mh-show-folder-buffer)))
  139. (defun org-mhe-get-message-num ()
  140. "Return the number of the current message.
  141. Be careful if you use sequences."
  142. (save-excursion
  143. (if (equal major-mode 'mh-folder-mode)
  144. (mh-get-msg-num nil)
  145. ;; Refer to the show buffer
  146. (mh-show-buffer-message-number))))
  147. (defun org-mhe-get-header (header)
  148. "Return the field for HEADER of the message in folder mode.
  149. This will create a show buffer for the corresponding message. If
  150. you have a better idea of how to do this then please let us know."
  151. (let* ((folder (org-mhe-get-message-folder))
  152. (num (org-mhe-get-message-num))
  153. (buffer (get-buffer-create (concat "show-" folder)))
  154. (header-field))
  155. (with-current-buffer buffer
  156. (mh-display-msg num folder)
  157. (if (equal major-mode 'mh-folder-mode)
  158. (mh-header-display)
  159. (mh-show-header-display))
  160. (set-buffer buffer)
  161. (setq header-field (mh-get-header-field header))
  162. (if (equal major-mode 'mh-folder-mode)
  163. (mh-show)
  164. (mh-show-show))
  165. header-field)))
  166. (defun org-mhe-follow-link (folder article)
  167. "Follow an MH-E link to FOLDER and ARTICLE.
  168. If ARTICLE is nil FOLDER is shown. If the configuration variable
  169. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  170. ARTICLE is searched in all folders. Indexed searches (swish++,
  171. namazu, and others supported by MH-E) will always search in all
  172. folders."
  173. (require 'mh-e)
  174. (require 'mh-search)
  175. (require 'mh-utils)
  176. (mh-find-path)
  177. (if (not article)
  178. (mh-visit-folder (mh-normalize-folder-name folder))
  179. (mh-search-choose)
  180. (if (equal mh-searcher 'pick)
  181. (progn
  182. (setq article (org-add-angle-brackets article))
  183. (mh-search folder (list "--message-id" article))
  184. (when (and org-mhe-search-all-folders
  185. (not (org-mhe-get-message-real-folder)))
  186. (kill-this-buffer)
  187. (mh-search "+" (list "--message-id" article))))
  188. (if mh-search-regexp-builder
  189. (mh-search "+" (funcall mh-search-regexp-builder
  190. (list (cons 'message-id article))))
  191. (mh-search "+" article)))
  192. (if (org-mhe-get-message-real-folder)
  193. (mh-show-msg 1)
  194. (kill-this-buffer)
  195. (error "Message not found"))))
  196. (provide 'org-mhe)
  197. ;; arch-tag: dcb05484-8627-491d-a8c1-01dbd2bde4ae
  198. ;;; org-mhe.el ends here