ol-mhe.el 7.8 KB

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