planner-publish: Change :after to :before-end
[planner-el.git] / planner-timeclock.el
blobcff8603d8bc34e9174ea17ea0286f1c2c681d1a4
1 ;;; planner-timeclock.el --- Timeclock integration for the Emacs Planner
3 ;; Copyright (C) 2001, 2004, 2005, 2006 Free Software Foundation, Inc.
4 ;; Parts copyright (C) 2005 Peter K. Lee
6 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; Keywords: planner, timeclock
8 ;; URL: http://www.plannerlove.com/
10 ;; This file is part of Planner. It is not part of GNU Emacs.
12 ;; Planner is free software; you can redistribute it and/or modify it
13 ;; 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 ;; Planner is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with Planner; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; This module allows you to clock in and clock out of your projects.
30 ;; You can also generate reports with the <timeclock-report> tag.
32 ;; timeclock.el is part of GNU Emacs. If you are using XEmacs, please
33 ;; use the version in the contrib directory -- otherwise, just use the
34 ;; version that comes with Emacs.
36 ;;; Contributors:
38 ;; Yann Hodique helped port this to Muse.
40 ;; Peter K. Lee helped fix an issue involving planner-multi.el with
41 ;; the Muse port.
43 ;;; Code:
45 (require 'planner)
46 (require 'timeclock)
47 (require 'planner-schedule)
48 (require 'easymenu)
49 (require 'advice)
51 (defvar planner-timeclock-current-task nil
52 "Description of current task.")
54 (defalias 'planner-timeclock-in 'planner-task-in-progress)
56 (defadvice timeclock-out (after planner-timeclock activate)
57 "Clear `planner-timeclock-current-task.'"
58 (setq planner-timeclock-current-task nil))
60 (defun planner-timeclock-plan-string (task-info &optional plans)
61 "Return the string for the plan part of the timeclock entry for TASK-INFO."
62 (let ((plan-link (if (featurep 'planner-multi)
63 ;; Remove the date links
64 (planner-multi-make-link
65 (or (planner-multi-filter-links
66 planner-date-regexp
67 (if plans
68 (planner-multi-split plans)
69 (planner-multi-task-link-as-list task-info))
71 (list (planner-task-plan task-info))))
72 (planner-make-link
73 (or plans (planner-task-plan task-info))))))
74 (if plan-link
75 (concat plan-link ": ")
76 "")))
78 (defun planner-timeclock-task-marked (old-status new-status)
79 "Clock out if the task description matches the one clocked in."
80 (cond
81 ((string= new-status "X")
82 (when (and planner-timeclock-current-task
83 (string= (planner-task-description (planner-current-task-info))
84 planner-timeclock-current-task)
85 (timeclock-currently-in-p))
86 (timeclock-out 1)))
87 ((string= new-status "P")
88 (when (and planner-timeclock-current-task
89 (string= (planner-task-description (planner-current-task-info))
90 planner-timeclock-current-task)
91 (timeclock-currently-in-p))
92 (timeclock-out)))
93 ((string= new-status "o")
94 (let* ((task-info (planner-current-task-info))
95 (project (concat
96 (planner-timeclock-plan-string task-info)
97 (planner-task-description task-info))))
98 (if (timeclock-currently-in-p)
99 (timeclock-change nil project)
100 (timeclock-in nil project))
101 (setq planner-timeclock-current-task (planner-task-description task-info)))))
103 (add-hook 'planner-mark-task-hook 'planner-timeclock-task-marked)
105 (defadvice planner-replan-task (around planner-timeclock activate)
106 "Edit the clocked in task as well."
107 (let ((info (planner-current-task-info)))
108 ad-do-it
109 (with-current-buffer (find-file-noselect timeclock-file)
110 (goto-char (point-min))
111 (while (re-search-forward
112 (concat
113 "^. [^ \n]+ [^ \n]+ "
114 "\\(" (regexp-quote (planner-timeclock-plan-string info)) "\\)"
115 (regexp-quote (planner-task-description info)) "$") nil t)
116 (replace-match
117 (save-match-data (planner-timeclock-plan-string nil (ad-get-arg 0)))
118 t t nil 1))
119 (save-buffer)
120 (kill-buffer (current-buffer)))))
122 (defadvice planner-edit-task-description (around planner-timeclock activate)
123 "Update the timelog as well. Warning! Do not have duplicate tasks!"
124 (let ((info (planner-current-task-info)))
125 (when (string= (planner-task-description info) planner-timeclock-current-task)
126 (setq planner-timeclock-current-task (ad-get-arg 0)))
127 ad-do-it
128 (with-current-buffer (find-file-noselect timeclock-file)
129 (goto-char (point-min))
130 (while (re-search-forward
131 (concat
132 "^. [^ \n]+ [^ \n]+ "
133 (regexp-quote (planner-timeclock-plan-string info))
134 "\\("
135 (regexp-quote (planner-task-description info))
136 "\\)$")
137 nil t)
138 (replace-match (ad-get-arg 0) t t nil 1))
139 (save-buffer)
140 (kill-buffer (current-buffer)))))
142 ;;;###autoload
143 (defun planner-colors-timeclock-report-tag (beg end)
144 "Replace the region BEG to END with a timeclock report, colorizing
145 the result."
146 (require 'timeclock)
147 (add-text-properties
148 beg end (list 'display
149 (with-temp-buffer
150 (timeclock-generate-report muse-publishing-p)
151 (buffer-string)))))
153 (defun planner-publish-timeclock-report-tag (beg end)
154 "Replace the region BEG to END with a timeclock report."
155 (require 'timeclock)
156 (delete-region beg end)
157 (timeclock-generate-report muse-publishing-p)
158 (add-text-properties beg (point) '(read-only t)))
160 (add-hook 'muse-colors-markup-tags
161 (if (featurep 'muse-nested-tags)
162 '("timeclock-report" nil nil nil
163 planner-colors-timeclock-report-tag)
164 '("timeclock-report" nil nil
165 planner-colors-timeclock-report-tag)))
167 (add-hook 'muse-publish-markup-tags
168 (if (featurep 'muse-nested-tags)
169 '("timeclock-report" nil nil nil
170 planner-publish-timeclock-report-tag)
171 '("timeclock-report" nil nil
172 planner-publish-timeclock-report-tag)))
174 (defun planner-timeclock-task-plan (info)
175 "Return the first plan page associated with INFO."
176 (car (elt info 0)))
178 (defun planner-timeclock-task-plan-as-list (info)
179 "Return all the plan pages associated with INFO."
180 (elt info 0))
182 (defun planner-timeclock-task-description (info)
183 "Return the descrption associated with INFO."
184 (elt info 1))
186 (defun planner-timeclock-task-length (info)
187 "Return the length associated with INFO."
188 (elt info 2))
190 (defun planner-timeclock-task-info (entry)
191 "Parse ENTRY and return a list of the form (plan task length).
192 See `timeclock-log-data' for the format of ENTRY. Note that the
193 project field in `timeclock-log-data' is 'project: task' here."
194 (let ((project (if (stringp entry) entry (timeclock-entry-project entry)))
195 plan task)
196 (when project
197 (with-planner
198 (cond
199 ;; No plan, just the task
200 ((string-match "^\\s-*:\\s-+" project)
201 (setq task (substring project (match-end 0))))
202 ;; Single link
203 ((string-match (concat "^\\(" muse-explicit-link-regexp "\\): ") project)
204 (setq plan (list (match-string 1 project)))
205 (setq task (substring project (match-end 0))))
206 ;; Multi link
207 ((and
208 (featurep 'planner-multi)
209 (string-match
210 (concat "^\\(\\(?:" muse-explicit-link-regexp "\\)"
211 "\\(?:" planner-multi-separator
212 "\\(?:" muse-explicit-link-regexp "\\)\\)*\\): ")
213 project))
214 (setq task (substring project (match-end 0)))
215 (setq plan (planner-multi-split (match-string 1 project))))
216 ;; Nothing whatsoever.
217 (t (setq task project)))
218 (list plan task (unless (stringp entry) (timeclock-entry-length entry)))))))
220 (condition-case nil
221 (let ((map planner-mode-map))
222 (define-key map "\C-c\C-i" 'planner-task-in-progress)
223 (define-key map "\C-c\C-o" 'timeclock-out)
224 (if (featurep 'xemacs)
225 (progn
226 (define-key map (kbd "C-c C-T C-i") 'planner-task-in-progress)
227 (define-key map (kbd "C-c C-T C-o") 'timeclock-out))
228 (define-key map (kbd "C-c C-S-t C-i") 'planner-task-in-progress)
229 (define-key map (kbd "C-c C-S-t C-o") 'timeclock-out)))
230 (error (message "Could not bind timeclock keys in planner mode")))
232 (condition-case nil
233 ;; XEmacs seems to be missing this function in some released
234 ;; versions of XEmacs21.
235 (if (fboundp 'easy-menu-create-menu)
236 (easy-menu-add-item planner-mode-map
237 '("Planner")
238 (easy-menu-create-menu
239 "Timeclock"
240 '(["Clock into a task" planner-timeclock-in]
241 ["Clock out" timeclock-out])))
242 (easy-menu-add-item planner-menu
243 '("Timeclock")
244 ["Clock into a task" planner-timeclock-in t]
245 "Plan")
246 (easy-menu-add-item planner-menu
247 '("Timeclock")
248 ["Clock out" timeclock-out t]
249 "Plan")))
251 (provide 'planner-timeclock)
253 ;;; planner-timeclock.el ends here