add planner-multi support to planner-authz
[planner-el.git] / planner-mhe.el
blob4d7783e8d1f52329445be83394db95914fac64a4
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 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 2, or (at your option)
15 ;; any later version.
17 ;; This file 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; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; Inspired by planner-gnus (thanks to Sacha Chua)
31 ;; This file adds annotations for MH-E messages. You will then be able
32 ;; to use M-x planner-create-task-from-buffer to create tasks from
33 ;; MH-E folder or show buffers with the correct annotation. If you
34 ;; create an annotation from a mh-index folder, the message "real"
35 ;; folder will be taken into account.
37 ;;; Contributors:
39 ;; Yann Hodique helped port this to Muse.
41 ;;; Code:
43 (require 'planner)
44 (require 'mh-e)
45 (require 'mh-index)
47 (defun planner-mhe-get-message-folder-from-index ()
48 "Returns the name of the message folder in a index folder
49 buffer."
50 (save-excursion
51 (mh-index-previous-folder)
52 (buffer-substring
53 (planner-line-beginning-position)
54 (planner-line-end-position))))
56 (defun planner-mhe-get-message-real-folder ()
57 "Return the name of the current message real folder, so if you use
58 sequences, it will now work."
59 (save-excursion
60 (let* ((folder
61 (if (equal major-mode 'mh-folder-mode)
62 mh-current-folder
63 ;; Refer to the show buffer
64 mh-show-folder-buffer))
65 (end-index (min (length mh-index-folder) (length folder))))
66 ;; a simple test on mh-index-data does not work, because
67 ;; mh-index-data is always nil in a show buffer.
68 (if (string= mh-index-folder (substring folder 0 end-index))
69 (if (equal major-mode 'mh-show-mode)
70 (save-window-excursion
71 (when (buffer-live-p (get-buffer folder))
72 (progn
73 (pop-to-buffer folder)
74 (planner-mhe-get-message-folder-from-index))))
75 (planner-mhe-get-message-folder-from-index))
76 folder))))
78 (defun planner-mhe-get-message-folder ()
79 "Return the name of the current message folder."
80 (if (equal major-mode 'mh-folder-mode)
81 mh-current-folder
82 ;; Refer to the show buffer
83 mh-show-folder-buffer))
85 (defun planner-mhe-get-message-field (field)
86 "Return a particular field of the current message."
87 (save-excursion
88 (let ((num
89 (if (equal major-mode 'mh-folder-mode)
90 (mh-get-msg-num nil)
91 ;; Refer to the show buffer
92 (mh-show-buffer-message-number)))
93 (folder (planner-mhe-get-message-folder)))
94 (car (split-string
95 (with-temp-buffer
96 (call-process (expand-file-name "anno" mh-progs)
97 nil t nil
98 folder
99 "-list" "-component" field
100 (number-to-string num))
101 (buffer-string))
102 "\n")))))
104 ;;;###autoload
105 (defun planner-mhe-annotation ()
106 "If called from a MH-E folder or message buffer, return an annotation.
107 Suitable for use in `planner-annotation-functions'."
108 (when (or (equal major-mode 'mh-folder-mode)
109 (equal major-mode 'mh-show-mode))
110 (let ((from-header (planner-mhe-get-message-field "From"))
111 (to-header (planner-mhe-get-message-field "To")))
112 (planner-make-link
113 (concat "mhe://" (planner-mhe-get-message-real-folder) "/"
114 (planner-mhe-get-message-field "Message-Id"))
115 (concat "E-Mail "
116 (if (and planner-ignored-from-addresses
117 from-header
118 (string-match planner-ignored-from-addresses
119 from-header))
120 ;; Mail from me, so use the To: instead
121 (concat "to " (planner-get-name-from-address
122 to-header))
123 ;; Mail to me, so use the From:
124 (concat "from " (planner-get-name-from-address
125 from-header))))))))
127 ;;;###autoload
128 (defun planner-mhe-browse-url (url)
129 "If this is a MH-E URL, jump to it."
130 (when (string-match "\\`mhe://\\(.+\\)/\\([^>]+\\)" url)
131 (let* ((folder (match-string 1 url))
132 (num (match-string 2 url))
133 (show-buf (concat "show-" folder)))
134 (save-window-excursion
135 (mh-visit-folder folder)
136 (get-buffer-create show-buf)
137 (mh-display-msg
138 (string-to-number
139 (if (= (aref num 0) ?<) ; message-id
140 (car (split-string
141 (with-temp-buffer
142 (call-process
143 (expand-file-name "pick" mh-progs)
144 nil t nil
145 folder
146 "--message-id"
147 num)
148 (buffer-string))
149 "\n"))
150 num))
151 folder))
152 (pop-to-buffer show-buf))))
154 ;(fset 'planner-get-from 'planner-gnus-get-address)
155 ;(fset 'planner-get-message-id 'planner-gnus-get-message-id)
156 (custom-add-option 'planner-annotation-functions
157 'planner-mhe-annotation)
158 (add-hook 'planner-annotation-functions 'planner-mhe-annotation)
159 (planner-add-protocol "mhe://" 'planner-mhe-browse-url nil)
161 ; to set the mh-path etc. variables
162 (mh-find-path)
164 (provide 'planner-mhe)
165 ;;; planner-mhe.el ends here