Merge branch 'maint'
[org-mode.git] / contrib / lisp / org-effectiveness.el
blob1e80d1f90e2fc2ccf919204a7726b8c4fad41db7
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
8 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
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.
32 ;;; Code:
34 (require 'org)
36 (defcustom org-effectiveness-max-todo 50
37 "This variable is useful to advice to the user about
38 many TODO pending"
39 :type 'integer
40 :group 'org-effectiveness)
42 (defun org-effectiveness-advice()
43 "Advicing about a possible excess of TODOS"
44 (interactive)
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: ")
55 (save-excursion
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"
61 (interactive)
62 (save-excursion
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"
68 (interactive)
69 (save-excursion
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"
75 (interactive)
76 (save-excursion
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"
82 (interactive)
83 (save-excursion
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)
114 (save-excursion
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)
126 (if (< m 10)
127 (concat "0" (number-to-string m))
128 (number-to-string m)))
130 (defun org-effectiveness-plot(startdate enddate)
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"))
155 ;; Create a file
156 (let ((month startmonth)
157 (year startyear)
158 (str ""))
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"))
161 (if (= month 12)
162 (progn
163 (setq year (+ 1 year))
164 (setq month 1))
165 (setq month (+ 1 month))))
166 (write-region str nil "/tmp/org-effectiveness"))
167 ;; Create the bar graph
168 (if (file-exists-p "/usr/bin/gnuplot")
169 (call-process "/bin/bash" nil t nil "-c" "/usr/bin/gnuplot -e 'plot \"/tmp/org-effectiveness\" using 2:xticlabels(1) with histograms' -p")
170 (message "gnuplot is not installed")))
172 (defun org-effectiveness-ascii-bar(n &optional label)
173 "Print a bar with the percentage from 0 to 100 printed in ascii"
174 (interactive "nPercentage: \nsLabel: ")
175 (if (or (< n 0) (> n 100))
176 (message "The percentage must be between 0 to 100")
177 (let ((x 0)
178 (y 0)
179 (z 0))
180 (insert (format "\n### %s ###" label))
181 (insert "\n-")
182 (while (< x n)
183 (insert "-")
184 (setq x (+ x 1)))
185 (insert "+\n")
186 (insert (format "%d" n))
187 (if (> n 10)
188 (setq y (+ y 1)))
189 (while (< y n)
190 (insert " ")
191 (setq y (+ y 1)))
192 (insert "|\n")
193 (insert "-")
194 (while (< z n)
195 (insert "-")
196 (setq z (+ z 1)))
197 (insert "+"))))
199 (defun org-effectiveness-html-bar(n &optional label)
200 "Print a bar with the percentage from 0 to 100 printed in html"
201 (interactive "nPercentage: \nsLabel: ")
202 (if (or (< n 0) (> n 100))
203 (message "The percentage must be between 0 to 100")
204 (let ((x 0)
205 (y 0)
206 (z 0))
207 (insert (format "\n<div class='percentage-%d'>%d</div>" n n))
211 (defun org-effectiveness-check-dates (startdate enddate)
212 "Generate a list with ((startyear startmonth) (endyear endmonth))"
213 (setq str nil)
214 (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" startdate))
215 (setq str "The start date must have the next format YYYY-MM"))
216 (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" enddate))
217 (setq str "The end date must have the next format YYYY-MM"))
218 ;; Checking if startdate < enddate
219 (if (string-match "^[0-9][0-9][0-9][0-9]" startdate)
220 (setq startyear (string-to-number (match-string 0 startdate))))
221 (if (string-match "[0-9][0-9]$" startdate)
222 (setq startmonth (string-to-number (match-string 0 startdate))))
223 (if (string-match "^[0-9][0-9][0-9][0-9]" enddate)
224 (setq endyear (string-to-number (match-string 0 enddate))))
225 (if (string-match "[0-9][0-9]$" enddate)
226 (setq endmonth (string-to-number (match-string 0 enddate))))
227 (if (> startyear endyear)
228 (setq str "The start date must be before that end date"))
229 (if (and (= startyear endyear) (> startmonth endmonth))
230 (setq str "The start date must be before that end date"))
231 (if str
232 (message str)
233 ;; (list (list startyear startmonth) (list endyear endmonth))))
234 (list (list 'startyear startyear) (list 'startmonth startmonth) (list 'endyear endyear) (list 'endmonth endmonth))))
236 (defun org-effectiveness-plot-ascii (startdate enddate)
237 (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
238 (setq dates (org-effectiveness-check-dates startdate enddate))
239 (let ((syear (cadr (assoc 'startyear dates)))
240 (smonth (cadr (assoc 'startmonth dates)))
241 (year (cadr (assoc 'startyear dates)))
242 (month (cadr (assoc 'startmonth dates)))
243 (emonth (cadr (assoc 'endmonth dates)))
244 (eyear (cadr (assoc 'endyear dates)))
245 (buffer (current-buffer))
246 (str ""))
247 (while (or (> eyear year) (and (= eyear year) (>= emonth month)))
248 (setq str (org-effectiveness-in-date (concat (number-to-string year) "-" (org-effectiveness-month-to-string month)) 1))
249 (switch-to-buffer "*org-effectiveness*")
250 (org-effectiveness-ascii-bar (string-to-number str) (format "%s-%s" year month))
251 (switch-to-buffer buffer)
252 (if (eq month 12)
253 (progn
254 (setq year (+ 1 year))
255 (setq month 1))
256 (setq month (+ 1 month)))))
257 (switch-to-buffer "*org-effectiveness*"))
259 (defun org-effectiveness-plot-html (startdate enddate)
260 "Print html bars about the effectiveness in a buffer"
261 (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
262 (setq dates (org-effectiveness-check-dates startdate enddate))
263 (let ((syear (cadr (assoc 'startyear dates)))
264 (smonth (cadr (assoc 'startmonth dates)))
265 (year (cadr (assoc 'startyear dates)))
266 (month (cadr (assoc 'startmonth dates)))
267 (emonth (cadr (assoc 'endmonth dates)))
268 (eyear (cadr (assoc 'endyear dates)))
269 (buffer (current-buffer))
270 (str ""))
271 (switch-to-buffer "*org-effectiveness-html*")
272 (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>")
273 (while (or (> eyear year) (and (= eyear year) (>= emonth month)))
274 (setq str (org-effectiveness-in-date (concat (number-to-string year) "-" (org-effectiveness-month-to-string month)) 1))
275 (switch-to-buffer "*org-effectiveness-html*")
276 (org-effectiveness-html-bar (string-to-number str) (format "%s-%s" year month))
277 (switch-to-buffer buffer)
278 (format "%s-%s" year month)
279 (if (eq month 12)
280 (progn
281 (setq year (+ 1 year))
282 (setq month 1))
283 (setq month (+ 1 month))))
284 (switch-to-buffer "*org-effectiveness-html*")
285 (insert "</body></html>")))
287 (provide 'org-effectiveness)