org-effectiveness.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. ;;; org-effectiveness.el --- Measuring the personal effectiveness
  2. ;; Copyright (C) 2013-2018 Free Software Foundation, Inc.
  3. ;; Author: David Arroyo Menéndez <davidam@es.gnu.org>
  4. ;; Keywords: effectiveness, plot
  5. ;; Homepage: https://orgmode.org
  6. ;;
  7. ;; This file is not part of GNU Emacs, yet.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file implements functions to measure the effectiveness in org.
  23. ;; Org-mode doesn't load this module by default - if this is not what
  24. ;; you want, configure the variable `org-modules'. Thanks to #emacs-es
  25. ;; irc channel for your support.
  26. ;;; Code:
  27. (require 'org)
  28. (defcustom org-effectiveness-max-todo 50
  29. "This variable is useful to advice to the user about
  30. many TODO pending"
  31. :type 'integer
  32. :group 'org-effectiveness)
  33. (defun org-effectiveness-advice()
  34. "Advicing about a possible excess of TODOS"
  35. (interactive)
  36. (save-excursion
  37. (goto-char (point-min))
  38. (if (< org-effectiveness-max-todo (count-matches "* TODO"))
  39. (message "An excess of TODOS!"))))
  40. ;; Check advice starting an org file
  41. (add-hook 'org-mode-hook 'org-effectiveness-advice)
  42. (defun org-effectiveness-count-keyword(keyword)
  43. "Print a message with the number of keyword outline in the current buffer"
  44. (interactive "sKeyword: ")
  45. (save-excursion
  46. (goto-char (point-min))
  47. (message "Number of %s: %d" keyword (count-matches (concat "* " keyword)))))
  48. (defun org-effectiveness-count-todo()
  49. "Print a message with the number of todo tasks in the current buffer"
  50. (interactive)
  51. (save-excursion
  52. (goto-char (point-min))
  53. (message "Number of TODO: %d" (count-matches "* TODO"))))
  54. (defun org-effectiveness-count-done()
  55. "Print a message with the number of done tasks in the current buffer"
  56. (interactive)
  57. (save-excursion
  58. (goto-char (point-min))
  59. (message "Number of DONE: %d" (count-matches "* DONE"))))
  60. (defun org-effectiveness-count-canceled()
  61. "Print a message with the number of canceled tasks in the current buffer"
  62. (interactive)
  63. (save-excursion
  64. (goto-char (point-min))
  65. (message "Number of Canceled: %d" (count-matches "* CANCEL+ED"))))
  66. (defun org-effectiveness-count-task()
  67. "Print a message with the number of tasks and subtasks in the current buffer"
  68. (interactive)
  69. (save-excursion
  70. (goto-char (point-min))
  71. (message "Number of tasks: %d" (count-matches "^*"))))
  72. (defun org-effectiveness()
  73. "Returns the effectiveness in the current org buffer"
  74. (interactive)
  75. (save-excursion
  76. (goto-char (point-min))
  77. (let ((done (float (count-matches "* DONE.*\n.*")))
  78. (canc (float (count-matches "* CANCEL+ED.*\n.*"))))
  79. (if (and (= done canc) (zerop done))
  80. (setq effectiveness 0)
  81. (setq effectiveness (* 100 (/ done (+ done canc)))))
  82. (message "Effectiveness: %f" effectiveness))))
  83. (defun org-effectiveness-keywords-in-date(keyword date)
  84. (interactive "sKeyword: \nsDate: " keyword date)
  85. (setq count (count-matches (concat keyword ".*\n.*" date)))
  86. (message (concat "%sS: %d" keyword count)))
  87. (defun org-effectiveness-dones-in-date(date &optional notmessage)
  88. (interactive "sGive me a date: " date)
  89. (save-excursion
  90. (goto-char (point-min))
  91. (let ((count (count-matches (concat "DONE.*\n.*" date))))
  92. (if (eq notmessage 1)
  93. (message "%d" count)
  94. (message "DONES: %d " count)))))
  95. (defun org-effectiveness-todos-in-date(date)
  96. (interactive "sGive me a date: " date)
  97. (save-excursion
  98. (goto-char (point-min))
  99. (setq count (count-matches (concat "TODO.*\n.*" date)))
  100. (message "TODOS: %d" count)))
  101. (defun org-effectiveness-canceled-in-date(date)
  102. (interactive "sGive me a date: " date)
  103. (save-excursion
  104. (goto-char (point-min))
  105. (setq count (count-matches (concat "CANCEL+ED.*\n.*" date)))
  106. (message "CANCELEDS: %d" count)))
  107. (defun org-effectiveness-ntasks-in-date(date &optional notmessage)
  108. (interactive "sGive me a date: " date)
  109. (save-excursion
  110. (goto-char (point-min))
  111. (let ((tasks (float (count-matches (concat "^*.*\n.*" date)))))
  112. (message "%d" tasks))))
  113. (defun org-effectiveness-in-date(date &optional notmessage)
  114. (interactive "sGive me a date: " date)
  115. (save-excursion
  116. (goto-char (point-min))
  117. (let ((done (float (count-matches (concat "* DONE.*\n.*" date))))
  118. (canc (float (count-matches (concat "* CANCEL+ED.*\n.*" date)))))
  119. (if (and (= done canc) (zerop done))
  120. (setq effectiveness 0)
  121. (setq effectiveness (* 100 (/ done (+ done canc)))))
  122. (if (eq notmessage 1)
  123. (message "%d" effectiveness)
  124. (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)))
  129. (defun org-effectiveness-plot(startdate enddate &optional save)
  130. (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
  131. (setq dates (org-effectiveness-check-dates startdate enddate))
  132. (setq syear (cadr (assq 'startyear dates)))
  133. (setq smonth (cadr (assq 'startmonth dates)))
  134. (setq eyear (cadr (assq 'endyear dates)))
  135. (setq emonth (assq 'endmonth dates))
  136. ;; Checking the format of the dates
  137. (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" startdate))
  138. (message "The start date must have the next format YYYY-MM"))
  139. (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" enddate))
  140. (message "The end date must have the next format YYYY-MM"))
  141. ;; Checking if startdate < enddate
  142. (if (string-match "^[0-9][0-9][0-9][0-9]" startdate)
  143. (setq startyear (string-to-number (match-string 0 startdate))))
  144. (if (string-match "[0-9][0-9]$" startdate)
  145. (setq startmonth (string-to-number (match-string 0 startdate))))
  146. (if (string-match "^[0-9][0-9][0-9][0-9]" enddate)
  147. (setq endyear (string-to-number (match-string 0 enddate))))
  148. (if (string-match "[0-9][0-9]$" enddate)
  149. (setq endmonth (string-to-number (match-string 0 enddate))))
  150. (if (> startyear endyear)
  151. (message "The start date must be before that end date"))
  152. (if (and (= startyear endyear) (> startmonth endmonth))
  153. (message "The start date must be before that end date"))
  154. ;; Create a file
  155. (let ((month startmonth)
  156. (year startyear)
  157. (str ""))
  158. (while (or (> endyear year) (and (= endyear year) (>= endmonth month)))
  159. (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"))
  160. (if (= month 12)
  161. (progn
  162. (setq year (+ 1 year))
  163. (setq month 1))
  164. (setq month (+ 1 month))))
  165. (write-region str nil "/tmp/org-effectiveness"))
  166. ;; Create the bar graph
  167. (if (eq save t)
  168. (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")
  169. (setq strplot "/usr/bin/gnuplot -e 'plot \"/tmp/org-effectiveness\" using 2:xticlabels(1) with histograms' -p"))
  170. (if (file-exists-p "/usr/bin/gnuplot")
  171. (call-process "/bin/bash" nil t nil "-c" strplot)
  172. (message "gnuplot is not installed")))
  173. (defun org-effectiveness-plot-save(startdate enddate &optional save)
  174. (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
  175. (org-effectiveness-plot startdate enddate t))
  176. ;; (defun org-effectiveness-plot(startdate enddate)
  177. (defun org-effectiveness-ascii-bar(n &optional label)
  178. "Print a bar with the percentage from 0 to 100 printed in ascii"
  179. (interactive "nPercentage: \nsLabel: ")
  180. (if (or (< n 0) (> n 100))
  181. (message "The percentage must be between 0 to 100")
  182. (let ((x 0)
  183. (y 0)
  184. (z 0))
  185. (insert (format "\n### %s ###" label))
  186. (insert "\n-")
  187. (while (< x n)
  188. (insert "-")
  189. (setq x (+ x 1)))
  190. (insert "+\n")
  191. (insert (format "%d" n))
  192. (if (> n 10)
  193. (setq y (+ y 1)))
  194. (while (< y n)
  195. (insert " ")
  196. (setq y (+ y 1)))
  197. (insert "|\n")
  198. (insert "-")
  199. (while (< z n)
  200. (insert "-")
  201. (setq z (+ z 1)))
  202. (insert "+"))))
  203. (defun org-effectiveness-html-bar(n &optional label)
  204. "Print a bar with the percentage from 0 to 100 printed in html"
  205. (interactive "nPercentage: \nsLabel: ")
  206. (if (or (< n 0) (> n 100))
  207. (message "The percentage must be between 0 to 100")
  208. (let ((x 0)
  209. (y 0)
  210. (z 0))
  211. (insert (format "\n<div class='percentage-%d'>%d</div>" n n))
  212. )))
  213. (defun org-effectiveness-check-dates (startdate enddate)
  214. "Generate a list with ((startyear startmonth) (endyear endmonth))"
  215. (setq str nil)
  216. (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" startdate))
  217. (setq str "The start date must have the next format YYYY-MM"))
  218. (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" enddate))
  219. (setq str "The end date must have the next format YYYY-MM"))
  220. ;; Checking if startdate < enddate
  221. (if (string-match "^[0-9][0-9][0-9][0-9]" startdate)
  222. (setq startyear (string-to-number (match-string 0 startdate))))
  223. (if (string-match "[0-9][0-9]$" startdate)
  224. (setq startmonth (string-to-number (match-string 0 startdate))))
  225. (if (string-match "^[0-9][0-9][0-9][0-9]" enddate)
  226. (setq endyear (string-to-number (match-string 0 enddate))))
  227. (if (string-match "[0-9][0-9]$" enddate)
  228. (setq endmonth (string-to-number (match-string 0 enddate))))
  229. (if (> startyear endyear)
  230. (setq str "The start date must be before that end date"))
  231. (if (and (= startyear endyear) (> startmonth endmonth))
  232. (setq str "The start date must be before that end date"))
  233. (if str
  234. (message str)
  235. ;; (list (list startyear startmonth) (list endyear endmonth))))
  236. (list (list 'startyear startyear) (list 'startmonth startmonth) (list 'endyear endyear) (list 'endmonth endmonth))))
  237. (defun org-effectiveness-plot-ascii (startdate enddate)
  238. (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
  239. (setq dates (org-effectiveness-check-dates startdate enddate))
  240. (let ((syear (cadr (assq 'startyear dates)))
  241. (smonth (cadr (assq 'startmonth dates)))
  242. (year (cadr (assq 'startyear dates)))
  243. (month (cadr (assq 'startmonth dates)))
  244. (emonth (cadr (assq 'endmonth dates)))
  245. (eyear (cadr (assq 'endyear dates)))
  246. (buffer (current-buffer))
  247. (str ""))
  248. (while (or (> eyear year) (and (= eyear year) (>= emonth month)))
  249. (setq str (org-effectiveness-in-date (concat (number-to-string year) "-" (org-effectiveness-month-to-string month)) 1))
  250. (switch-to-buffer "*org-effectiveness*")
  251. (org-effectiveness-ascii-bar (string-to-number str) (format "%s-%s" year month))
  252. (switch-to-buffer buffer)
  253. (if (eq month 12)
  254. (progn
  255. (setq year (+ 1 year))
  256. (setq month 1))
  257. (setq month (+ 1 month)))))
  258. (switch-to-buffer "*org-effectiveness*"))
  259. (defun org-effectiveness-plot-ascii-ntasks (startdate enddate)
  260. (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
  261. (setq dates (org-effectiveness-check-dates startdate enddate))
  262. (let ((syear (cadr (assq 'startyear dates)))
  263. (smonth (cadr (assq 'startmonth dates)))
  264. (year (cadr (assq 'startyear dates)))
  265. (month (cadr (assq 'startmonth dates)))
  266. (emonth (cadr (assq 'endmonth dates)))
  267. (eyear (cadr (assq 'endyear dates)))
  268. (buffer (current-buffer))
  269. (str ""))
  270. (while (or (> eyear year) (and (= eyear year) (>= emonth month)))
  271. (setq str (org-effectiveness-ntasks-in-date (concat (number-to-string year) "-" (org-effectiveness-month-to-string month)) 1))
  272. (switch-to-buffer "*org-effectiveness*")
  273. (org-effectiveness-ascii-bar (string-to-number str) (format "%s-%s" year month))
  274. (switch-to-buffer buffer)
  275. (if (eq month 12)
  276. (progn
  277. (setq year (+ 1 year))
  278. (setq month 1))
  279. (setq month (+ 1 month)))))
  280. (switch-to-buffer "*org-effectiveness*"))
  281. (defun org-effectiveness-plot-ascii-dones (startdate enddate)
  282. (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
  283. (setq dates (org-effectiveness-check-dates startdate enddate))
  284. (let ((syear (cadr (assq 'startyear dates)))
  285. (smonth (cadr (assq 'startmonth dates)))
  286. (year (cadr (assq 'startyear dates)))
  287. (month (cadr (assq 'startmonth dates)))
  288. (emonth (cadr (assq 'endmonth dates)))
  289. (eyear (cadr (assq 'endyear dates)))
  290. (buffer (current-buffer))
  291. (str ""))
  292. (while (or (> eyear year) (and (= eyear year) (>= emonth month)))
  293. (setq str (org-effectiveness-dones-in-date (concat (number-to-string year) "-" (org-effectiveness-month-to-string month)) 1))
  294. (switch-to-buffer "*org-effectiveness*")
  295. (org-effectiveness-ascii-bar (string-to-number str) (format "%s-%s" year month))
  296. (switch-to-buffer buffer)
  297. (if (eq month 12)
  298. (progn
  299. (setq year (+ 1 year))
  300. (setq month 1))
  301. (setq month (+ 1 month)))))
  302. (switch-to-buffer "*org-effectiveness*"))
  303. (defun org-effectiveness-plot-html (startdate enddate)
  304. "Print html bars about the effectiveness in a buffer"
  305. (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
  306. (setq dates (org-effectiveness-check-dates startdate enddate))
  307. (let ((syear (cadr (assq 'startyear dates)))
  308. (smonth (cadr (assq 'startmonth dates)))
  309. (year (cadr (assq 'startyear dates)))
  310. (month (cadr (assq 'startmonth dates)))
  311. (emonth (cadr (assq 'endmonth dates)))
  312. (eyear (cadr (assq 'endyear dates)))
  313. (buffer (current-buffer))
  314. (str ""))
  315. (switch-to-buffer "*org-effectiveness-html*")
  316. (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>")
  317. (while (or (> eyear year) (and (= eyear year) (>= emonth month)))
  318. (setq str (org-effectiveness-in-date (concat (number-to-string year) "-" (org-effectiveness-month-to-string month)) 1))
  319. (switch-to-buffer "*org-effectiveness-html*")
  320. (org-effectiveness-html-bar (string-to-number str) (format "%s-%s" year month))
  321. (switch-to-buffer buffer)
  322. (format "%s-%s" year month)
  323. (if (eq month 12)
  324. (progn
  325. (setq year (+ 1 year))
  326. (setq month 1))
  327. (setq month (+ 1 month))))
  328. (switch-to-buffer "*org-effectiveness-html*")
  329. (insert "</body></html>")))
  330. (provide 'org-effectiveness)