Merged from mwolson@gnu.org--2006 (patch 41)
[planner-el.git] / planner-mhe.el
blob295f2f4d4f2387171906425c38d463e72757e08e
1 ;;; planner-mhe.el --- MH-E integration for the Emacs Planner
3 ;; Copyright (C) 2004, 2005 Christophe Garion
4 ;; Parts copyright (C) 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: Christophe Garion <garion@supaero.fr>
7 ;; Author: Sandra Jean Chua <sacha@free.net.ph>
8 ;; Created: <2004-08-09 17:16:57 tof planner-mhe.el>
9 ;; Time-stamp: <12/04/2005 19:26:58 Yann Hodique>
10 ;; Keywords: planner, mh-e
12 ;; This file is part of Planner. It is not part of GNU Emacs.
14 ;; Planner is free software; you can redistribute it and/or modify it
15 ;; under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
19 ;; Planner is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with Planner; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;; Commentary:
31 ;; Inspired by planner-gnus (thanks to Sacha Chua)
33 ;; This file adds annotations for MH-E messages. You will then be able
34 ;; to use M-x planner-create-task-from-buffer to create tasks from
35 ;; MH-E folder or show buffers with the correct annotation. If you
36 ;; create an annotation from a mh-index folder, the message "real"
37 ;; folder will be taken into account.
39 ;;; Contributors:
41 ;; Yann Hodique helped port this to Muse.
43 ;;; Code:
45 (require 'planner)
46 (require 'mh-e)
47 (require 'mh-index)
49 (defun planner-mhe-get-message-folder-from-index ()
50 "Returns the name of the message folder in a index folder
51 buffer."
52 (save-excursion
53 (mh-index-previous-folder)
54 (buffer-substring
55 (planner-line-beginning-position)
56 (planner-line-end-position))))
58 (defun planner-mhe-get-message-real-folder ()
59 "Return the name of the current message real folder, so if you use
60 sequences, it will now work."
61 (save-excursion
62 (let* ((folder
63 (if (equal major-mode 'mh-folder-mode)
64 mh-current-folder
65 ;; Refer to the show buffer
66 mh-show-folder-buffer))
67 (end-index (min (length mh-index-folder) (length folder))))
68 ;; a simple test on mh-index-data does not work, because
69 ;; mh-index-data is always nil in a show buffer.
70 (if (string= mh-index-folder (substring folder 0 end-index))
71 (if (equal major-mode 'mh-show-mode)
72 (save-window-excursion
73 (when (buffer-live-p (get-buffer folder))
74 (progn
75 (pop-to-buffer folder)
76 (planner-mhe-get-message-folder-from-index))))
77 (planner-mhe-get-message-folder-from-index))
78 folder))))
80 (defun planner-mhe-get-message-folder ()
81 "Return the name of the current message folder."
82 (if (equal major-mode 'mh-folder-mode)
83 mh-current-folder
84 ;; Refer to the show buffer
85 mh-show-folder-buffer))
87 (defun planner-mhe-get-message-field (field)
88 "Return a particular field of the current message."
89 (save-excursion
90 (let ((num
91 (if (equal major-mode 'mh-folder-mode)
92 (mh-get-msg-num nil)
93 ;; Refer to the show buffer
94 (mh-show-buffer-message-number)))
95 (folder (planner-mhe-get-message-folder)))
96 (car (split-string
97 (with-temp-buffer
98 (call-process (expand-file-name "anno" mh-progs)
99 nil t nil
100 folder
101 "-list" "-component" field
102 (number-to-string num))
103 (buffer-string))
104 "\n")))))
106 ;;;###autoload
107 (defun planner-mhe-annotation ()
108 "If called from a MH-E folder or message buffer, return an annotation.
109 Suitable for use in `planner-annotation-functions'."
110 (when (or (equal major-mode 'mh-folder-mode)
111 (equal major-mode 'mh-show-mode))
112 (let ((from-header (planner-mhe-get-message-field "From"))
113 (to-header (planner-mhe-get-message-field "To")))
114 (planner-make-link
115 (concat "mhe://" (planner-mhe-get-message-real-folder) "/"
116 (planner-mhe-get-message-field "Message-Id"))
117 (concat "E-Mail "
118 (if (and planner-ignored-from-addresses
119 from-header
120 (string-match planner-ignored-from-addresses
121 from-header))
122 ;; Mail from me, so use the To: instead
123 (concat "to " (planner-get-name-from-address
124 to-header))
125 ;; Mail to me, so use the From:
126 (concat "from " (planner-get-name-from-address
127 from-header))))))))
129 ;;;###autoload
130 (defun planner-mhe-browse-url (url)
131 "If this is a MH-E URL, jump to it."
132 (when (string-match "\\`mhe://\\(.+\\)/\\([^>\n]+\\)" url)
133 (let* ((folder (match-string 1 url))
134 (num (match-string 2 url))
135 (show-buf (concat "show-" folder)))
136 (save-window-excursion
137 (mh-visit-folder folder)
138 (get-buffer-create show-buf)
139 (mh-display-msg
140 (string-to-number
141 (if (= (aref num 0) ?<) ; message-id
142 (car (split-string
143 (with-temp-buffer
144 (call-process
145 (expand-file-name "pick" mh-progs)
146 nil t nil
147 folder
148 "--message-id"
149 num)
150 (buffer-string))
151 "\n"))
152 num))
153 folder))
154 (pop-to-buffer show-buf))))
156 ;(fset 'planner-get-from 'planner-gnus-get-address)
157 ;(fset 'planner-get-message-id 'planner-gnus-get-message-id)
158 (custom-add-option 'planner-annotation-functions
159 'planner-mhe-annotation)
160 (add-hook 'planner-annotation-functions 'planner-mhe-annotation)
161 (planner-add-protocol "mhe://" 'planner-mhe-browse-url nil)
163 ; to set the mh-path etc. variables
164 (mh-find-path)
166 (provide 'planner-mhe)
167 ;;; planner-mhe.el ends here