Merge branch 'maint'
[org-mode.git] / contrib / lisp / org-effectiveness.el
bloba07084c9c681fed1848b29c4baa5e7e422a6d6cb
1 ;;; org-effectiveness.el --- Measuring the personal effectiveness
3 ;; Copyright (C) 2013-2017 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 (save-excursion
46 (goto-char (point-min))
47 (if (< org-effectiveness-max-todo (count-matches "* TODO"))
48 (message "An excess of TODOS!"))))
50 ;; Check advice starting an org file
51 (add-hook 'org-mode-hook 'org-effectiveness-advice)
53 (defun org-effectiveness-count-keyword(keyword)
54 "Print a message with the number of keyword outline in the current buffer"
55 (interactive "sKeyword: ")
56 (save-excursion
57 (goto-char (point-min))
58 (message "Number of %s: %d" keyword (count-matches (concat "* " keyword)))))
60 (defun org-effectiveness-count-todo()
61 "Print a message with the number of todo tasks in the current buffer"
62 (interactive)
63 (save-excursion
64 (goto-char (point-min))
65 (message "Number of TODO: %d" (count-matches "* TODO"))))
67 (defun org-effectiveness-count-done()
68 "Print a message with the number of done tasks in the current buffer"
69 (interactive)
70 (save-excursion
71 (goto-char (point-min))
72 (message "Number of DONE: %d" (count-matches "* DONE"))))
74 (defun org-effectiveness-count-canceled()
75 "Print a message with the number of canceled tasks in the current buffer"
76 (interactive)
77 (save-excursion
78 (goto-char (point-min))
79 (message "Number of Canceled: %d" (count-matches "* CANCEL+ED"))))
81 (defun org-effectiveness-count-task()
82 "Print a message with the number of tasks and subtasks in the current buffer"
83 (interactive)
84 (save-excursion
85 (goto-char (point-min))
86 (message "Number of tasks: %d" (count-matches "^*"))))
88 (defun org-effectiveness()
89 "Returns the effectiveness in the current org buffer"
90 (interactive)
91 (save-excursion
92 (goto-char (point-min))
93 (let ((done (float (count-matches "* DONE.*\n.*")))
94 (canc (float (count-matches "* CANCEL+ED.*\n.*"))))
95 (if (and (= done canc) (zerop done))
96 (setq effectiveness 0)
97 (setq effectiveness (* 100 (/ done (+ done canc)))))
98 (message "Effectiveness: %f" effectiveness))))
101 (defun org-effectiveness-keywords-in-date(keyword date)
102 (interactive "sKeyword: \nsDate: " keyword date)
103 (setq count (count-matches (concat keyword ".*\n.*" date)))
104 (message (concat "%sS: %d" keyword count)))
106 (defun org-effectiveness-dones-in-date(date &optional notmessage)
107 (interactive "sGive me a date: " date)
108 (save-excursion
109 (goto-char (point-min))
110 (let ((count (count-matches (concat "DONE.*\n.*" date))))
111 (if (eq notmessage 1)
112 (message "%d" count)
113 (message "DONES: %d " count)))))
115 (defun org-effectiveness-todos-in-date(date)
116 (interactive "sGive me a date: " date)
117 (save-excursion
118 (goto-char (point-min))
119 (setq count (count-matches (concat "TODO.*\n.*" date)))
120 (message "TODOS: %d" count)))
122 (defun org-effectiveness-canceled-in-date(date)
123 (interactive "sGive me a date: " date)
124 (save-excursion
125 (goto-char (point-min))
126 (setq count (count-matches (concat "CANCEL+ED.*\n.*" date)))
127 (message "CANCELEDS: %d" count)))
129 (defun org-effectiveness-ntasks-in-date(date &optional notmessage)
130 (interactive "sGive me a date: " date)
131 (save-excursion
132 (goto-char (point-min))
133 (let ((tasks (float (count-matches (concat "^*.*\n.*" date)))))
134 (message "%d" tasks))))
136 (defun org-effectiveness-in-date(date &optional notmessage)
137 (interactive "sGive me a date: " date)
138 (save-excursion
139 (goto-char (point-min))
140 (let ((done (float (count-matches (concat "* DONE.*\n.*" date))))
141 (canc (float (count-matches (concat "* CANCEL+ED.*\n.*" date)))))
142 (if (and (= done canc) (zerop done))
143 (setq effectiveness 0)
144 (setq effectiveness (* 100 (/ done (+ done canc)))))
145 (if (eq notmessage 1)
146 (message "%d" effectiveness)
147 (message "Effectiveness: %d " effectiveness)))))
149 (defun org-effectiveness-month-to-string (m)
150 (if (< m 10)
151 (concat "0" (number-to-string m))
152 (number-to-string m)))
154 (defun org-effectiveness-plot(startdate enddate &optional save)
155 (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
156 (setq dates (org-effectiveness-check-dates startdate enddate))
157 (setq syear (cadr (assq 'startyear dates)))
158 (setq smonth (cadr (assq 'startmonth dates)))
159 (setq eyear (cadr (assq 'endyear dates)))
160 (setq emonth (assq 'endmonth dates))
161 ;; Checking the format of the dates
162 (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" startdate))
163 (message "The start date must have the next format YYYY-MM"))
164 (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" enddate))
165 (message "The end date must have the next format YYYY-MM"))
166 ;; Checking if startdate < enddate
167 (if (string-match "^[0-9][0-9][0-9][0-9]" startdate)
168 (setq startyear (string-to-number (match-string 0 startdate))))
169 (if (string-match "[0-9][0-9]$" startdate)
170 (setq startmonth (string-to-number (match-string 0 startdate))))
171 (if (string-match "^[0-9][0-9][0-9][0-9]" enddate)
172 (setq endyear (string-to-number (match-string 0 enddate))))
173 (if (string-match "[0-9][0-9]$" enddate)
174 (setq endmonth (string-to-number (match-string 0 enddate))))
175 (if (> startyear endyear)
176 (message "The start date must be before that end date"))
177 (if (and (= startyear endyear) (> startmonth endmonth))
178 (message "The start date must be before that end date"))
179 ;; Create a file
180 (let ((month startmonth)
181 (year startyear)
182 (str ""))
183 (while (or (> endyear year) (and (= endyear year) (>= endmonth month)))
184 (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"))
185 (if (= month 12)
186 (progn
187 (setq year (+ 1 year))
188 (setq month 1))
189 (setq month (+ 1 month))))
190 (write-region str nil "/tmp/org-effectiveness"))
191 ;; Create the bar graph
192 (if (eq save t)
193 (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")
194 (setq strplot "/usr/bin/gnuplot -e 'plot \"/tmp/org-effectiveness\" using 2:xticlabels(1) with histograms' -p"))
195 (if (file-exists-p "/usr/bin/gnuplot")
196 (call-process "/bin/bash" nil t nil "-c" strplot)
197 (message "gnuplot is not installed")))
199 (defun org-effectiveness-plot-save(startdate enddate &optional save)
200 (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
201 (org-effectiveness-plot startdate enddate t))
203 ;; (defun org-effectiveness-plot(startdate enddate)
206 (defun org-effectiveness-ascii-bar(n &optional label)
207 "Print a bar with the percentage from 0 to 100 printed in ascii"
208 (interactive "nPercentage: \nsLabel: ")
209 (if (or (< n 0) (> n 100))
210 (message "The percentage must be between 0 to 100")
211 (let ((x 0)
212 (y 0)
213 (z 0))
214 (insert (format "\n### %s ###" label))
215 (insert "\n-")
216 (while (< x n)
217 (insert "-")
218 (setq x (+ x 1)))
219 (insert "+\n")
220 (insert (format "%d" n))
221 (if (> n 10)
222 (setq y (+ y 1)))
223 (while (< y n)
224 (insert " ")
225 (setq y (+ y 1)))
226 (insert "|\n")
227 (insert "-")
228 (while (< z n)
229 (insert "-")
230 (setq z (+ z 1)))
231 (insert "+"))))
233 (defun org-effectiveness-html-bar(n &optional label)
234 "Print a bar with the percentage from 0 to 100 printed in html"
235 (interactive "nPercentage: \nsLabel: ")
236 (if (or (< n 0) (> n 100))
237 (message "The percentage must be between 0 to 100")
238 (let ((x 0)
239 (y 0)
240 (z 0))
241 (insert (format "\n<div class='percentage-%d'>%d</div>" n n))
245 (defun org-effectiveness-check-dates (startdate enddate)
246 "Generate a list with ((startyear startmonth) (endyear endmonth))"
247 (setq str nil)
248 (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" startdate))
249 (setq str "The start date must have the next format YYYY-MM"))
250 (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" enddate))
251 (setq str "The end date must have the next format YYYY-MM"))
252 ;; Checking if startdate < enddate
253 (if (string-match "^[0-9][0-9][0-9][0-9]" startdate)
254 (setq startyear (string-to-number (match-string 0 startdate))))
255 (if (string-match "[0-9][0-9]$" startdate)
256 (setq startmonth (string-to-number (match-string 0 startdate))))
257 (if (string-match "^[0-9][0-9][0-9][0-9]" enddate)
258 (setq endyear (string-to-number (match-string 0 enddate))))
259 (if (string-match "[0-9][0-9]$" enddate)
260 (setq endmonth (string-to-number (match-string 0 enddate))))
261 (if (> startyear endyear)
262 (setq str "The start date must be before that end date"))
263 (if (and (= startyear endyear) (> startmonth endmonth))
264 (setq str "The start date must be before that end date"))
265 (if str
266 (message str)
267 ;; (list (list startyear startmonth) (list endyear endmonth))))
268 (list (list 'startyear startyear) (list 'startmonth startmonth) (list 'endyear endyear) (list 'endmonth endmonth))))
270 (defun org-effectiveness-plot-ascii (startdate enddate)
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 (assq 'startyear dates)))
274 (smonth (cadr (assq 'startmonth dates)))
275 (year (cadr (assq 'startyear dates)))
276 (month (cadr (assq 'startmonth dates)))
277 (emonth (cadr (assq 'endmonth dates)))
278 (eyear (cadr (assq 'endyear dates)))
279 (buffer (current-buffer))
280 (str ""))
281 (while (or (> eyear year) (and (= eyear year) (>= emonth month)))
282 (setq str (org-effectiveness-in-date (concat (number-to-string year) "-" (org-effectiveness-month-to-string month)) 1))
283 (switch-to-buffer "*org-effectiveness*")
284 (org-effectiveness-ascii-bar (string-to-number str) (format "%s-%s" year month))
285 (switch-to-buffer buffer)
286 (if (eq month 12)
287 (progn
288 (setq year (+ 1 year))
289 (setq month 1))
290 (setq month (+ 1 month)))))
291 (switch-to-buffer "*org-effectiveness*"))
294 (defun org-effectiveness-plot-ascii-ntasks (startdate enddate)
295 (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
296 (setq dates (org-effectiveness-check-dates startdate enddate))
297 (let ((syear (cadr (assq 'startyear dates)))
298 (smonth (cadr (assq 'startmonth dates)))
299 (year (cadr (assq 'startyear dates)))
300 (month (cadr (assq 'startmonth dates)))
301 (emonth (cadr (assq 'endmonth dates)))
302 (eyear (cadr (assq 'endyear dates)))
303 (buffer (current-buffer))
304 (str ""))
305 (while (or (> eyear year) (and (= eyear year) (>= emonth month)))
306 (setq str (org-effectiveness-ntasks-in-date (concat (number-to-string year) "-" (org-effectiveness-month-to-string month)) 1))
307 (switch-to-buffer "*org-effectiveness*")
308 (org-effectiveness-ascii-bar (string-to-number str) (format "%s-%s" year month))
309 (switch-to-buffer buffer)
310 (if (eq month 12)
311 (progn
312 (setq year (+ 1 year))
313 (setq month 1))
314 (setq month (+ 1 month)))))
315 (switch-to-buffer "*org-effectiveness*"))
317 (defun org-effectiveness-plot-ascii-dones (startdate enddate)
318 (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
319 (setq dates (org-effectiveness-check-dates startdate enddate))
320 (let ((syear (cadr (assq 'startyear dates)))
321 (smonth (cadr (assq 'startmonth dates)))
322 (year (cadr (assq 'startyear dates)))
323 (month (cadr (assq 'startmonth dates)))
324 (emonth (cadr (assq 'endmonth dates)))
325 (eyear (cadr (assq 'endyear dates)))
326 (buffer (current-buffer))
327 (str ""))
328 (while (or (> eyear year) (and (= eyear year) (>= emonth month)))
329 (setq str (org-effectiveness-dones-in-date (concat (number-to-string year) "-" (org-effectiveness-month-to-string month)) 1))
330 (switch-to-buffer "*org-effectiveness*")
331 (org-effectiveness-ascii-bar (string-to-number str) (format "%s-%s" year month))
332 (switch-to-buffer buffer)
333 (if (eq month 12)
334 (progn
335 (setq year (+ 1 year))
336 (setq month 1))
337 (setq month (+ 1 month)))))
338 (switch-to-buffer "*org-effectiveness*"))
341 (defun org-effectiveness-plot-html (startdate enddate)
342 "Print html bars about the effectiveness in a buffer"
343 (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
344 (setq dates (org-effectiveness-check-dates startdate enddate))
345 (let ((syear (cadr (assq 'startyear dates)))
346 (smonth (cadr (assq 'startmonth dates)))
347 (year (cadr (assq 'startyear dates)))
348 (month (cadr (assq 'startmonth dates)))
349 (emonth (cadr (assq 'endmonth dates)))
350 (eyear (cadr (assq 'endyear dates)))
351 (buffer (current-buffer))
352 (str ""))
353 (switch-to-buffer "*org-effectiveness-html*")
354 (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>")
355 (while (or (> eyear year) (and (= eyear year) (>= emonth month)))
356 (setq str (org-effectiveness-in-date (concat (number-to-string year) "-" (org-effectiveness-month-to-string month)) 1))
357 (switch-to-buffer "*org-effectiveness-html*")
358 (org-effectiveness-html-bar (string-to-number str) (format "%s-%s" year month))
359 (switch-to-buffer buffer)
360 (format "%s-%s" year month)
361 (if (eq month 12)
362 (progn
363 (setq year (+ 1 year))
364 (setq month 1))
365 (setq month (+ 1 month))))
366 (switch-to-buffer "*org-effectiveness-html*")
367 (insert "</body></html>")))
369 (provide 'org-effectiveness)