Version number: Pushed to 6.16trans.
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-mhe.el
blob04c201f0a58967beeb80323cdbdc787ccc76c48c
1 ;;; org-mhe.el --- Support for links to MH-E messages from within Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 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
8 ;; Version: 6.16trans
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file implements links to MH-E messages from within Org-mode.
29 ;; Org-mode loads this module by default - if this is not what you want,
30 ;; configure the variable `org-modules'.
32 ;;; Code:
34 (require 'org)
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
44 supported by MH-E."
45 :group 'org-link-follow
46 :type 'boolean)
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
60 window-config))
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
68 range index-data))
69 (defvar mh-progs)
70 (defvar mh-current-folder)
71 (defvar mh-show-folder-buffer)
72 (defvar mh-index-folder)
73 (defvar mh-searcher)
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)
80 ;; Implementation
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 (let ((from (org-mhe-get-header "From:"))
86 (to (org-mhe-get-header "To:"))
87 (message-id (org-mhe-get-header "Message-Id:"))
88 (subject (org-mhe-get-header "Subject:"))
89 link desc)
90 (org-store-link-props :type "mh" :from from :to to
91 :subject subject :message-id message-id)
92 (setq desc (org-email-link-description))
93 (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
94 (org-remove-angle-brackets message-id)))
95 (org-add-link-props :link link :description desc)
96 link)))
98 (defun org-mhe-open (path)
99 "Follow an MH-E message link specified by PATH."
100 (let (folder article)
101 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
102 (error "Error in MH-E link"))
103 (setq folder (match-string 1 path)
104 article (match-string 3 path))
105 (org-mhe-follow-link folder article)))
107 ;;; mh-e integration based on planner-mode
108 (defun org-mhe-get-message-real-folder ()
109 "Return the name of the real folder for the current message.
110 So if you use sequences, it will now work."
111 (save-excursion
112 (let* ((folder
113 (if (equal major-mode 'mh-folder-mode)
114 mh-current-folder
115 ;; Refer to the show buffer
116 mh-show-folder-buffer))
117 (end-index
118 (if (boundp 'mh-index-folder)
119 (min (length mh-index-folder) (length folder))))
121 ;; a simple test on mh-index-data does not work, because
122 ;; mh-index-data is always nil in a show buffer.
123 (if (and (boundp 'mh-index-folder)
124 (string= mh-index-folder (substring folder 0 end-index)))
125 (if (equal major-mode 'mh-show-mode)
126 (save-window-excursion
127 (let (pop-up-frames)
128 (when (buffer-live-p (get-buffer folder))
129 (progn
130 (pop-to-buffer folder)
131 (org-mhe-get-message-folder-from-index)
134 (org-mhe-get-message-folder-from-index)
136 folder
140 (defun org-mhe-get-message-folder-from-index ()
141 "Return the name of the message folder in an index folder buffer."
142 (save-excursion
143 (mh-index-previous-folder)
144 (if (re-search-forward "^\\(+.*\\)$" nil t)
145 (message "%s" (match-string 1)))))
147 (defun org-mhe-get-message-folder ()
148 "Return the name of the current message folder.
149 Be careful if you use sequences."
150 (save-excursion
151 (if (equal major-mode 'mh-folder-mode)
152 mh-current-folder
153 ;; Refer to the show buffer
154 mh-show-folder-buffer)))
156 (defun org-mhe-get-message-num ()
157 "Return the number of the current message.
158 Be careful if you use sequences."
159 (save-excursion
160 (if (equal major-mode 'mh-folder-mode)
161 (mh-get-msg-num nil)
162 ;; Refer to the show buffer
163 (mh-show-buffer-message-number))))
165 (defun org-mhe-get-header (header)
166 "Return the field for HEADER of the message in folder mode.
167 This will create a show buffer for the corresponding message. If
168 you have a better idea of how to do this then please let us know."
169 (let* ((folder (org-mhe-get-message-folder))
170 (num (org-mhe-get-message-num))
171 (buffer (get-buffer-create (concat "show-" folder)))
172 (header-field))
173 (with-current-buffer buffer
174 (mh-display-msg num folder)
175 (if (equal major-mode 'mh-folder-mode)
176 (mh-header-display)
177 (mh-show-header-display))
178 (set-buffer buffer)
179 (setq header-field (mh-get-header-field header))
180 (if (equal major-mode 'mh-folder-mode)
181 (mh-show)
182 (mh-show-show))
183 header-field)))
185 (defun org-mhe-follow-link (folder article)
186 "Follow an MH-E link to FOLDER and ARTICLE.
187 If ARTICLE is nil FOLDER is shown. If the configuration variable
188 `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
189 ARTICLE is searched in all folders. Indexed searches (swish++,
190 namazu, and others supported by MH-E) will always search in all
191 folders."
192 (require 'mh-e)
193 (require 'mh-search)
194 (require 'mh-utils)
195 (mh-find-path)
196 (if (not article)
197 (mh-visit-folder (mh-normalize-folder-name folder))
198 (mh-search-choose)
199 (if (equal mh-searcher 'pick)
200 (progn
201 (setq article (org-add-angle-brackets article))
202 (mh-search folder (list "--message-id" article))
203 (when (and org-mhe-search-all-folders
204 (not (org-mhe-get-message-real-folder)))
205 (kill-this-buffer)
206 (mh-search "+" (list "--message-id" article))))
207 (if mh-search-regexp-builder
208 (mh-search "+" (funcall mh-search-regexp-builder
209 (list (cons 'message-id article))))
210 (mh-search "+" article)))
211 (if (org-mhe-get-message-real-folder)
212 (mh-show-msg 1)
213 (kill-this-buffer)
214 (error "Message not found"))))
216 (provide 'org-mhe)
218 ;; arch-tag: dcb05484-8627-491d-a8c1-01dbd2bde4ae
220 ;;; org-mhe.el ends here