1 ;;; org-mhe.el --- Support for links to MH-E messages from within Org-mode
3 ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
5 ;; Author: Thomas Baumann <thomas dot baumann at ch dot tum dot de>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
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.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file implements links to MH-E messages from within Org-mode.
28 ;; Org-mode loads this module by default - if this is not what you want,
29 ;; configure the variable `org-modules'.
36 ;; Customization variables
38 (defcustom org-mhe-search-all-folders nil
39 "Non-nil means the search for the mh-message may extend to all folders.
40 When non-nil, the search for a message will extend to all other
41 folders if it cannot be found in the folder given in the link.
42 Searching all folders may be slow with the default pick based
43 search but is very efficient with one of the other search engines
45 :group
'org-link-follow
48 ;; Declare external functions and variables
49 (declare-function mh-display-msg
"mh-show" (msg-num folder-name
))
50 (declare-function mh-find-path
"mh-utils" ())
51 (declare-function mh-get-header-field
"mh-utils" (field))
52 (declare-function mh-get-msg-num
"mh-utils" (error-if-no-message))
53 (declare-function mh-header-display
"mh-show" ())
54 (declare-function mh-index-previous-folder
"mh-search" ())
55 (declare-function mh-normalize-folder-name
"mh-utils"
56 (folder &optional empty-string-okay dont-remove-trailing-slash
57 return-nil-if-folder-empty
))
58 (declare-function mh-search
"mh-search"
59 (folder search-regexp
&optional redo-search-flag
61 (declare-function mh-search-choose
"mh-search" (&optional searcher
))
62 (declare-function mh-show
"mh-show" (&optional message redisplay-flag
))
63 (declare-function mh-show-buffer-message-number
"mh-comp" (&optional buffer
))
64 (declare-function mh-show-header-display
"mh-show" t t
)
65 (declare-function mh-show-msg
"mh-show" (msg))
66 (declare-function mh-show-show
"mh-show" t t
)
67 (declare-function mh-visit-folder
"mh-folder" (folder &optional
70 (defvar mh-current-folder
)
71 (defvar mh-show-folder-buffer
)
72 (defvar mh-index-folder
)
74 (defvar mh-search-regexp-builder
)
76 ;; Install the link type
77 (org-add-link-type "mhe" 'org-mhe-open
)
78 (add-hook 'org-store-link-functions
'org-mhe-store-link
)
81 (defun org-mhe-store-link ()
82 "Store a link to an MH-E folder or message."
83 (when (or (equal major-mode
'mh-folder-mode
)
84 (equal major-mode
'mh-show-mode
))
85 (save-window-excursion
86 (let* ((from (org-mhe-get-header "From:"))
87 (to (org-mhe-get-header "To:"))
88 (message-id (org-mhe-get-header "Message-Id:"))
89 (subject (org-mhe-get-header "Subject:"))
90 (date (org-mhe-get-header "Date:"))
92 (org-store-link-props :type
"mh" :from from
:to to
:date date
93 :subject subject
:message-id message-id
)
94 (setq desc
(org-email-link-description))
95 (setq link
(concat "mhe:" (org-mhe-get-message-real-folder) "#"
96 (org-remove-angle-brackets message-id
)))
97 (org-add-link-props :link link
:description desc
)
100 (defun org-mhe-open (path)
101 "Follow an MH-E message link specified by PATH."
102 (let (folder article
)
103 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path
))
104 (error "Error in MH-E link"))
105 (setq folder
(match-string 1 path
)
106 article
(match-string 3 path
))
107 (org-mhe-follow-link folder article
)))
109 ;;; mh-e integration based on planner-mode
110 (defun org-mhe-get-message-real-folder ()
111 "Return the name of the real folder for the current message.
112 So if you use sequences, it will now work."
115 (if (equal major-mode
'mh-folder-mode
)
117 ;; Refer to the show buffer
118 mh-show-folder-buffer
))
120 (if (boundp 'mh-index-folder
)
121 (min (length mh-index-folder
) (length folder
))))
123 ;; a simple test on mh-index-data does not work, because
124 ;; mh-index-data is always nil in a show buffer.
125 (if (and (boundp 'mh-index-folder
)
126 (string= mh-index-folder
(substring folder
0 end-index
)))
127 (if (equal major-mode
'mh-show-mode
)
128 (save-window-excursion
130 (when (buffer-live-p (get-buffer folder
))
132 (pop-to-buffer folder
)
133 (org-mhe-get-message-folder-from-index)
136 (org-mhe-get-message-folder-from-index)
142 (defun org-mhe-get-message-folder-from-index ()
143 "Return the name of the message folder in an index folder buffer."
145 (mh-index-previous-folder)
146 (if (re-search-forward "^\\(+.*\\)$" nil t
)
147 (message "%s" (match-string 1)))))
149 (defun org-mhe-get-message-folder ()
150 "Return the name of the current message folder.
151 Be careful if you use sequences."
153 (if (equal major-mode
'mh-folder-mode
)
155 ;; Refer to the show buffer
156 mh-show-folder-buffer
)))
158 (defun org-mhe-get-message-num ()
159 "Return the number of the current message.
160 Be careful if you use sequences."
162 (if (equal major-mode
'mh-folder-mode
)
164 ;; Refer to the show buffer
165 (mh-show-buffer-message-number))))
167 (defun org-mhe-get-header (header)
168 "Return the field for HEADER of the message in folder mode.
169 This will create a show buffer for the corresponding message. If
170 you have a better idea of how to do this then please let us know."
171 (let* ((folder (org-mhe-get-message-folder))
172 (num (org-mhe-get-message-num))
173 (buffer (get-buffer-create (concat "show-" folder
)))
175 (with-current-buffer buffer
176 (mh-display-msg num folder
)
177 (if (equal major-mode
'mh-folder-mode
)
179 (mh-show-header-display))
181 (setq header-field
(mh-get-header-field header
))
182 (if (equal major-mode
'mh-folder-mode
)
185 (org-trim header-field
))))
187 (defun org-mhe-follow-link (folder article
)
188 "Follow an MH-E link to FOLDER and ARTICLE.
189 If ARTICLE is nil FOLDER is shown. If the configuration variable
190 `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
191 ARTICLE is searched in all folders. Indexed searches (swish++,
192 namazu, and others supported by MH-E) will always search in all
199 (mh-visit-folder (mh-normalize-folder-name folder
))
201 (if (equal mh-searcher
'pick
)
203 (setq article
(org-add-angle-brackets article
))
204 (mh-search folder
(list "--message-id" article
))
205 (when (and org-mhe-search-all-folders
206 (not (org-mhe-get-message-real-folder)))
208 (mh-search "+" (list "--message-id" article
))))
209 (if mh-search-regexp-builder
210 (mh-search "+" (funcall mh-search-regexp-builder
211 (list (cons 'message-id article
))))
212 (mh-search "+" article
)))
213 (if (org-mhe-get-message-real-folder)
216 (error "Message not found"))))
220 ;;; org-mhe.el ends here