Fix customize interface for planner-annotation-functions
[planner-el.git] / planner-mhe.el
blobc46b5bb90ee346c17cb51efd86e29c11053565d1
1 ;;; planner-mhe.el --- MH-E integration for the Emacs Planner
3 ;; Copyright (C) 2004, 2005, 2006 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: <2006-09-28 15:05:05 tof planner-mhe.el>
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)
48 (defun planner-mhe-get-message-folder-from-index ()
49 "Returns the name of the message folder in a index folder
50 buffer."
51 (save-excursion
52 (mh-index-previous-folder)
53 (buffer-substring
54 (planner-line-beginning-position)
55 (planner-line-end-position))))
57 (defun planner-mhe-get-message-real-folder ()
58 "Return the name of the current message real folder, so if you use
59 sequences, it will now work."
60 (save-excursion
61 (let* ((folder
62 (if (equal major-mode 'mh-folder-mode)
63 mh-current-folder
64 ;; Refer to the show buffer
65 mh-show-folder-buffer))
66 (end-index (min (length mh-index-folder) (length folder))))
67 ;; a simple test on mh-index-data does not work, because
68 ;; mh-index-data is always nil in a show buffer.
69 (if (string= mh-index-folder (substring folder 0 end-index))
70 (if (equal major-mode 'mh-show-mode)
71 (save-window-excursion
72 (when (buffer-live-p (get-buffer folder))
73 (progn
74 (pop-to-buffer folder)
75 (planner-mhe-get-message-folder-from-index))))
76 (planner-mhe-get-message-folder-from-index))
77 folder))))
79 (defun planner-mhe-get-message-folder ()
80 "Return the name of the current message folder."
81 (if (equal major-mode 'mh-folder-mode)
82 mh-current-folder
83 ;; Refer to the show buffer
84 mh-show-folder-buffer))
86 (defun planner-mhe-get-message-field (field)
87 "Return a particular field of the current message."
88 (save-excursion
89 (let ((num
90 (if (equal major-mode 'mh-folder-mode)
91 (mh-get-msg-num nil)
92 ;; Refer to the show buffer
93 (mh-show-buffer-message-number)))
94 (folder (planner-mhe-get-message-folder)))
95 (car (split-string
96 (with-temp-buffer
97 (call-process (expand-file-name "anno" mh-progs)
98 nil t nil
99 folder
100 "-list" "-component" field
101 (number-to-string num))
102 (buffer-string))
103 "\n")))))
105 ;;;###autoload
106 (defun planner-mhe-annotation ()
107 "If called from a MH-E folder or message buffer, return an annotation.
108 Suitable for use in `planner-annotation-functions'."
109 (when (or (equal major-mode 'mh-folder-mode)
110 (equal major-mode 'mh-show-mode))
111 (let ((from-header (planner-mhe-get-message-field "From"))
112 (to-header (planner-mhe-get-message-field "To")))
113 (planner-make-link
114 (concat "mhe://" (planner-mhe-get-message-real-folder) "/"
115 (planner-mhe-get-message-field "Message-Id"))
116 (concat "E-Mail "
117 (if (and planner-ignored-from-addresses
118 from-header
119 (string-match planner-ignored-from-addresses
120 from-header))
121 ;; Mail from me, so use the To: instead
122 (concat "to " (planner-get-name-from-address
123 to-header))
124 ;; Mail to me, so use the From:
125 (concat "from " (planner-get-name-from-address
126 from-header))))))))
128 ;;;###autoload
129 (defun planner-mhe-browse-url (url)
130 "If this is a MH-E URL, jump to it."
131 (when (string-match "\\`mhe://\\(.+\\)/\\([^>\n]+\\)" url)
132 (let* ((folder (match-string 1 url))
133 (num (match-string 2 url))
134 (show-buf (concat "show-" folder)))
135 (save-window-excursion
136 (mh-visit-folder folder)
137 (get-buffer-create show-buf)
138 (mh-display-msg
139 (string-to-number
140 (if (= (aref num 0) ?<) ; message-id
141 (car (split-string
142 (with-temp-buffer
143 (call-process
144 (expand-file-name "pick" mh-progs)
145 nil t nil
146 folder
147 "--message-id"
148 num)
149 (buffer-string))
150 "\n"))
151 num))
152 folder))
153 (pop-to-buffer show-buf))))
155 ;(fset 'planner-get-from 'planner-gnus-get-address)
156 ;(fset 'planner-get-message-id 'planner-gnus-get-message-id)
157 (custom-add-option 'planner-annotation-functions
158 'planner-mhe-annotation)
159 (add-hook 'planner-annotation-functions 'planner-mhe-annotation)
160 (planner-add-protocol "mhe://" 'planner-mhe-browse-url nil)
162 ; to set the mh-path etc. variables
163 (mh-find-path)
165 (provide 'planner-mhe)
166 ;;; planner-mhe.el ends here