org-mhe.el 8.2 KB

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