1 ;;; org-effectiveness.el --- Measuring the personal effectiveness
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Author: David Arroyo Menéndez <davidam@es.gnu.org>
6 ;; Keywords: effectiveness, plot
7 ;; Homepage: http://orgmode.org
9 ;; This file is not part of GNU Emacs, yet.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file implements functions to measure the effectiveness in org.
28 ;; Org-mode doesn't load this module by default - if this is not what
29 ;; you want, configure the variable `org-modules'. Thanks to #emacs-es
30 ;; irc channel for your support.
36 (defcustom org-effectiveness-max-todo
50
37 "This variable is useful to advice to the user about
40 :group
'org-effectiveness
)
42 (defun org-effectiveness-advice()
43 "Advicing about a possible excess of TODOS"
45 (goto-char (point-min))
46 (if (< org-effectiveness-max-todo
(count-matches "* TODO"))
47 (message "An excess of TODOS!")))
49 ;; Check advice starting an org file
50 (add-hook 'org-mode-hook
'org-effectiveness-advice
)
52 (defun org-effectiveness-count-keyword(keyword)
53 "Print a message with the number of keyword outline in the current buffer"
54 (interactive "sKeyword: ")
56 (goto-char (point-min))
57 (message "Number of %s: %d" keyword
(count-matches (concat "* " keyword
)))))
59 (defun org-effectiveness-count-todo()
60 "Print a message with the number of todo tasks in the current buffer"
63 (goto-char (point-min))
64 (message "Number of TODO: %d" (count-matches "* TODO"))))
66 (defun org-effectiveness-count-done()
67 "Print a message with the number of done tasks in the current buffer"
70 (goto-char (point-min))
71 (message "Number of DONE: %d" (count-matches "* DONE"))))
73 (defun org-effectiveness-count-canceled()
74 "Print a message with the number of canceled tasks in the current buffer"
77 (goto-char (point-min))
78 (message "Number of Canceled: %d" (count-matches "* CANCEL+ED"))))
80 (defun org-effectiveness()
81 "Returns the effectiveness in the current org buffer"
84 (goto-char (point-min))
85 (let ((done (float (count-matches "* DONE.*\n.*")))
86 (canc (float (count-matches "* CANCEL+ED.*\n.*"))))
87 (if (and (= done canc
) (zerop done
))
88 (setq effectiveness
0)
89 (setq effectiveness
(* 100 (/ done
(+ done canc
)))))
90 (message "Effectiveness: %f" effectiveness
))))
92 (defun org-effectiveness-keywords-in-date(keyword date
)
93 (interactive "sKeyword: \nsDate: " keyword date
)
94 (setq count
(count-matches (concat keyword
".*\n.*" date
)))
95 (message (concat "%sS: %d" keyword count
)))
97 (defun org-effectiveness-dones-in-date(date)
98 (interactive "sGive me a date: " date
)
99 (setq count
(count-matches (concat "DONE.*\n.*" date
)))
100 (message "DONES: %d" count
))
102 (defun org-effectivenes-todos-in-date(date)
103 (interactive "sGive me a date: " date
)
104 (setq count
(count-matches (concat "TODO.*\n.*" date
)))
105 (message "TODOS: %d" count
))
107 (defun org-effectiveness-canceled-in-date(date)
108 (interactive "sGive me a date: " date
)
109 (setq count
(count-matches (concat "CANCEL+ED.*\n.*" date
)))
110 (message "CANCELEDS: %d" count
))
112 (defun org-effectiveness-in-date(date &optional notmessage
)
113 (interactive "sGive me a date: " date
)
115 (goto-char (point-min))
116 (let ((done (float (count-matches (concat "* DONE.*\n.*" date
))))
117 (canc (float (count-matches (concat "* CANCEL+ED.*\n.*" date
)))))
118 (if (and (= done canc
) (zerop done
))
119 (setq effectiveness
0)
120 (setq effectiveness
(* 100 (/ done
(+ done canc
)))))
121 (if (eq notmessage
1)
122 (message "%d" effectiveness
)
123 (message "Effectiveness: %d " effectiveness
)))))
125 (defun org-effectiveness-month-to-string (m)
127 (concat "0" (number-to-string m
))
128 (number-to-string m
)))
130 (defun org-effectiveness-plot(startdate enddate
&optional save
)
131 (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate
)
132 (setq dates
(org-effectiveness-check-dates startdate enddate
))
133 (setq syear
(cadr (assoc 'startyear dates
)))
134 (setq smonth
(cadr (assoc 'startmonth dates
)))
135 (setq eyear
(cadr (assoc 'endyear dates
)))
136 (setq emonth
(assoc 'endmonth dates
))
137 ;; Checking the format of the dates
138 (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" startdate
))
139 (message "The start date must have the next format YYYY-MM"))
140 (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" enddate
))
141 (message "The end date must have the next format YYYY-MM"))
142 ;; Checking if startdate < enddate
143 (if (string-match "^[0-9][0-9][0-9][0-9]" startdate
)
144 (setq startyear
(string-to-number (match-string 0 startdate
))))
145 (if (string-match "[0-9][0-9]$" startdate
)
146 (setq startmonth
(string-to-number (match-string 0 startdate
))))
147 (if (string-match "^[0-9][0-9][0-9][0-9]" enddate
)
148 (setq endyear
(string-to-number (match-string 0 enddate
))))
149 (if (string-match "[0-9][0-9]$" enddate
)
150 (setq endmonth
(string-to-number (match-string 0 enddate
))))
151 (if (> startyear endyear
)
152 (message "The start date must be before that end date"))
153 (if (and (= startyear endyear
) (> startmonth endmonth
))
154 (message "The start date must be before that end date"))
156 (let ((month startmonth
)
159 (while (or (> endyear year
) (and (= endyear year
) (>= endmonth month
)))
160 (setq str
(concat str
(number-to-string year
) "-" (org-effectiveness-month-to-string month
) " " (org-effectiveness-in-date (concat (number-to-string year
) "-" (org-effectiveness-month-to-string month
)) 1) "\n"))
163 (setq year
(+ 1 year
))
165 (setq month
(+ 1 month
))))
166 (write-region str nil
"/tmp/org-effectiveness"))
167 ;; Create the bar graph
169 (setq strplot
"/usr/bin/gnuplot -e 'set term png; set output \"/tmp/org-effectiveness.png\"; plot \"/tmp/org-effectiveness\" using 2:xticlabels(1) with histograms' -p")
170 (setq strplot
"/usr/bin/gnuplot -e 'plot \"/tmp/org-effectiveness\" using 2:xticlabels(1) with histograms' -p"))
171 (if (file-exists-p "/usr/bin/gnuplot")
172 (call-process "/bin/bash" nil t nil
"-c" strplot
)
173 (message "gnuplot is not installed")))
175 (defun org-effectiveness-plot-save(startdate enddate
&optional save
)
176 (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate
)
177 (org-effectiveness-plot startdate enddate t
))
179 ;; (defun org-effectiveness-plot(startdate enddate)
182 (defun org-effectiveness-ascii-bar(n &optional label
)
183 "Print a bar with the percentage from 0 to 100 printed in ascii"
184 (interactive "nPercentage: \nsLabel: ")
185 (if (or (< n
0) (> n
100))
186 (message "The percentage must be between 0 to 100")
190 (insert (format "\n### %s ###" label
))
196 (insert (format "%d" n
))
209 (defun org-effectiveness-html-bar(n &optional label
)
210 "Print a bar with the percentage from 0 to 100 printed in html"
211 (interactive "nPercentage: \nsLabel: ")
212 (if (or (< n
0) (> n
100))
213 (message "The percentage must be between 0 to 100")
217 (insert (format "\n<div class='percentage-%d'>%d</div>" n n
))
221 (defun org-effectiveness-check-dates (startdate enddate
)
222 "Generate a list with ((startyear startmonth) (endyear endmonth))"
224 (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" startdate
))
225 (setq str
"The start date must have the next format YYYY-MM"))
226 (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" enddate
))
227 (setq str
"The end date must have the next format YYYY-MM"))
228 ;; Checking if startdate < enddate
229 (if (string-match "^[0-9][0-9][0-9][0-9]" startdate
)
230 (setq startyear
(string-to-number (match-string 0 startdate
))))
231 (if (string-match "[0-9][0-9]$" startdate
)
232 (setq startmonth
(string-to-number (match-string 0 startdate
))))
233 (if (string-match "^[0-9][0-9][0-9][0-9]" enddate
)
234 (setq endyear
(string-to-number (match-string 0 enddate
))))
235 (if (string-match "[0-9][0-9]$" enddate
)
236 (setq endmonth
(string-to-number (match-string 0 enddate
))))
237 (if (> startyear endyear
)
238 (setq str
"The start date must be before that end date"))
239 (if (and (= startyear endyear
) (> startmonth endmonth
))
240 (setq str
"The start date must be before that end date"))
243 ;; (list (list startyear startmonth) (list endyear endmonth))))
244 (list (list 'startyear startyear
) (list 'startmonth startmonth
) (list 'endyear endyear
) (list 'endmonth endmonth
))))
246 (defun org-effectiveness-plot-ascii (startdate enddate
)
247 (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate
)
248 (setq dates
(org-effectiveness-check-dates startdate enddate
))
249 (let ((syear (cadr (assoc 'startyear dates
)))
250 (smonth (cadr (assoc 'startmonth dates
)))
251 (year (cadr (assoc 'startyear dates
)))
252 (month (cadr (assoc 'startmonth dates
)))
253 (emonth (cadr (assoc 'endmonth dates
)))
254 (eyear (cadr (assoc 'endyear dates
)))
255 (buffer (current-buffer))
257 (while (or (> eyear year
) (and (= eyear year
) (>= emonth month
)))
258 (setq str
(org-effectiveness-in-date (concat (number-to-string year
) "-" (org-effectiveness-month-to-string month
)) 1))
259 (switch-to-buffer "*org-effectiveness*")
260 (org-effectiveness-ascii-bar (string-to-number str
) (format "%s-%s" year month
))
261 (switch-to-buffer buffer
)
264 (setq year
(+ 1 year
))
266 (setq month
(+ 1 month
)))))
267 (switch-to-buffer "*org-effectiveness*"))
269 (defun org-effectiveness-plot-html (startdate enddate
)
270 "Print html bars about the effectiveness in a buffer"
271 (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate
)
272 (setq dates
(org-effectiveness-check-dates startdate enddate
))
273 (let ((syear (cadr (assoc 'startyear dates
)))
274 (smonth (cadr (assoc 'startmonth dates
)))
275 (year (cadr (assoc 'startyear dates
)))
276 (month (cadr (assoc 'startmonth dates
)))
277 (emonth (cadr (assoc 'endmonth dates
)))
278 (eyear (cadr (assoc 'endyear dates
)))
279 (buffer (current-buffer))
281 (switch-to-buffer "*org-effectiveness-html*")
282 (insert "<html><head><title>Graphbar</title><meta http-equiv='Content-type' content='text/html; charset=utf-8'><link rel='stylesheet' type='text/css' href='graphbar.css' title='graphbar'></head><body>")
283 (while (or (> eyear year
) (and (= eyear year
) (>= emonth month
)))
284 (setq str
(org-effectiveness-in-date (concat (number-to-string year
) "-" (org-effectiveness-month-to-string month
)) 1))
285 (switch-to-buffer "*org-effectiveness-html*")
286 (org-effectiveness-html-bar (string-to-number str
) (format "%s-%s" year month
))
287 (switch-to-buffer buffer
)
288 (format "%s-%s" year month
)
291 (setq year
(+ 1 year
))
293 (setq month
(+ 1 month
))))
294 (switch-to-buffer "*org-effectiveness-html*")
295 (insert "</body></html>")))
297 (provide 'org-effectiveness
)