Merge branch 'master' of ssh://repo.or.cz/srv/git/planner-el
[planner-el.git] / planner-timeclock.el
blob01381a2ae5508ed1f3a758c9519e9d672f9dc990
1 ;;; planner-timeclock.el --- Timeclock integration for the Emacs Planner
3 ;; Copyright (C) 2001, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 ;; Parts copyright (C) 2005, 2008 Peter K. Lee
6 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; Keywords: planner, timeclock
8 ;; URL: http://www.wjsullivan.net/PlannerMode.html
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 3, 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 'easymenu)
48 (require 'advice)
50 (defvar planner-timeclock-current-task nil
51 "Description of current task.")
53 (defalias 'planner-timeclock-in 'planner-task-in-progress)
55 (defadvice timeclock-out (after planner-timeclock activate)
56 "Clear `planner-timeclock-current-task.'"
57 (setq planner-timeclock-current-task nil))
59 (defun planner-timeclock-plan-string (task-info &optional plans)
60 "Return the string for the plan part of the timeclock entry for TASK-INFO."
61 (let ((plan-link (if (featurep 'planner-multi)
62 ;; Remove the date links
63 (planner-multi-make-link
64 (or (planner-multi-filter-links
65 planner-date-regexp
66 (if plans
67 (planner-multi-split plans)
68 (planner-multi-task-link-as-list task-info))
70 (list (planner-task-plan task-info))))
71 (planner-make-link
72 (or plans (planner-task-plan task-info))))))
73 (if plan-link
74 (concat plan-link ": ")
75 "")))
77 (defun planner-timeclock-task-marked (old-status new-status)
78 "Clock out if the task description matches the one clocked in."
79 (cond
80 ((string= new-status "X")
81 (when (and planner-timeclock-current-task
82 (string= (planner-task-description (planner-current-task-info))
83 planner-timeclock-current-task)
84 (timeclock-currently-in-p))
85 (timeclock-out 1)))
86 ((string= new-status "P")
87 (when (and planner-timeclock-current-task
88 (string= (planner-task-description (planner-current-task-info))
89 planner-timeclock-current-task)
90 (timeclock-currently-in-p))
91 (timeclock-out)))
92 ((string= new-status "o")
93 (let* ((task-info (planner-current-task-info))
94 (project (concat
95 (planner-timeclock-plan-string task-info)
96 (planner-task-description task-info))))
97 (if (timeclock-currently-in-p)
98 (timeclock-change nil project)
99 (timeclock-in nil project))
100 (setq planner-timeclock-current-task (planner-task-description task-info)))))
102 (add-hook 'planner-mark-task-hook 'planner-timeclock-task-marked)
104 (defadvice planner-replan-task (around planner-timeclock activate)
105 "Edit the clocked in task as well."
106 (let ((info (planner-current-task-info)))
107 ad-do-it
108 (with-current-buffer (find-file-noselect timeclock-file)
109 (goto-char (point-min))
110 (while (re-search-forward
111 (concat
112 "^. [^ \n]+ [^ \n]+ "
113 ;; make project match optional (for tasks added manually).
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 '("timeclock-report" nil nil nil
162 planner-colors-timeclock-report-tag))
164 (add-hook 'muse-publish-markup-tags
165 '("timeclock-report" nil nil nil
166 planner-publish-timeclock-report-tag))
168 (defun planner-timeclock-task-plan (info)
169 "Return the first plan page associated with INFO."
170 (car (elt info 0)))
172 (defun planner-timeclock-task-plan-as-list (info)
173 "Return all the plan pages associated with INFO."
174 (elt info 0))
176 (defun planner-timeclock-task-description (info)
177 "Return the descrption associated with INFO."
178 (elt info 1))
180 (defun planner-timeclock-task-length (info)
181 "Return the length associated with INFO."
182 (elt info 2))
184 (defun planner-timeclock-task-info (entry)
185 "Parse ENTRY and return a list of the form (plan task length).
186 See `timeclock-log-data' for the format of ENTRY. Note that the
187 project field in `timeclock-log-data' is 'project: task' here."
188 (let ((project (if (stringp entry) entry (timeclock-entry-project entry)))
189 plan task)
190 (when project
191 (with-planner
192 (cond
193 ;; No plan, just the task
194 ((string-match "^\\s-*:\\s-+" project)
195 (setq task (substring project (match-end 0))))
196 ;; Single link
197 ((string-match (concat "^\\(" muse-explicit-link-regexp "\\): ") project)
198 (setq plan (list (match-string 1 project)))
199 (setq task (substring project (match-end 0))))
200 ;; Multi link
201 ((and
202 (featurep 'planner-multi)
203 (string-match
204 (concat "^\\(\\(?:" muse-explicit-link-regexp "\\)"
205 "\\(?:" planner-multi-separator
206 "\\(?:" muse-explicit-link-regexp "\\)\\)*\\): ")
207 project))
208 (setq task (substring project (match-end 0)))
209 (setq plan (planner-multi-split (match-string 1 project))))
210 ;; Nothing whatsoever.
211 (t (setq task project)))
212 (list plan task (unless (stringp entry) (timeclock-entry-length entry)))))))
214 (condition-case nil
215 (let ((map planner-mode-map))
216 (define-key map "\C-c\C-i" 'planner-task-in-progress)
217 (define-key map "\C-c\C-o" 'timeclock-out)
218 (if (featurep 'xemacs)
219 (progn
220 (define-key map (kbd "C-c C-T C-i") 'planner-task-in-progress)
221 (define-key map (kbd "C-c C-T C-o") 'timeclock-out))
222 (define-key map (kbd "C-c C-S-t C-i") 'planner-task-in-progress)
223 (define-key map (kbd "C-c C-S-t C-o") 'timeclock-out)))
224 (error (message "Could not bind timeclock keys in planner mode")))
226 (condition-case nil
227 ;; XEmacs seems to be missing this function in some released
228 ;; versions of XEmacs21.
229 (if (fboundp 'easy-menu-create-menu)
230 (easy-menu-add-item planner-mode-map
231 '("Planner")
232 (easy-menu-create-menu
233 "Timeclock"
234 '(["Clock into a task" planner-timeclock-in]
235 ["Clock out" timeclock-out])))
236 (easy-menu-add-item planner-menu
237 '("Timeclock")
238 ["Clock into a task" planner-timeclock-in t]
239 "Plan")
240 (easy-menu-add-item planner-menu
241 '("Timeclock")
242 ["Clock out" timeclock-out t]
243 "Plan")))
245 (provide 'planner-timeclock)
247 ;;; planner-timeclock.el ends here