org-effectiveness.el 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. ;;; org-effectiveness.el --- Measuring the personal effectiveness
  2. ;; Copyright (C) 2013 Free Software Foundation, Inc.
  3. ;; Author: David Arroyo Menéndez <davidam@es.gnu.org>
  4. ;; Keywords: effectiveness, plot
  5. ;; Homepage: http://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. (defun org-effectiveness-count-keyword(keyword)
  29. "Print a message with the number of keyword outline in the current buffer"
  30. (interactive "sKeyword: ")
  31. (save-excursion
  32. (goto-char (point-min))
  33. (message "Number of %s: %d" keyword (count-matches (concat "* " keyword)))))
  34. (defun org-effectiveness-count-todo()
  35. "Print a message with the number of todo tasks in the current buffer"
  36. (interactive)
  37. (save-excursion
  38. (goto-char (point-min))
  39. (message "Number of TODO: %d" (count-matches "* TODO"))))
  40. (defun org-effectiveness-count-done()
  41. "Print a message with the number of done tasks in the current buffer"
  42. (interactive)
  43. (save-excursion
  44. (goto-char (point-min))
  45. (message "Number of DONE: %d" (count-matches "* DONE"))))
  46. (defun org-effectiveness-count-canceled()
  47. "Print a message with the number of canceled tasks in the current buffer"
  48. (interactive)
  49. (save-excursion
  50. (goto-char (point-min))
  51. (message "Number of Canceled: %d" (count-matches "* CANCEL+ED"))))
  52. (defun org-effectiveness()
  53. "Returns the effectiveness in the current org buffer"
  54. (interactive)
  55. (save-excursion
  56. (goto-char (point-min))
  57. (let ((done (float (count-matches "* DONE.*\n.*")))
  58. (canc (float (count-matches "* CANCEL+ED.*\n.*"))))
  59. (if (and (= done canc) (zerop done))
  60. (setq effectiveness 0)
  61. (setq effectiveness (* 100 (/ done (+ done canc)))))
  62. (message "Effectiveness: %f" effectiveness))))
  63. (defun org-effectiveness-keywords-in-date(keyword date)
  64. (interactive "sKeyword: \nsDate: " keyword date)
  65. (setq count (count-matches (concat keyword ".*\n.*" date)))
  66. (message (concat "%sS: %d" keyword count)))
  67. (defun org-effectiveness-dones-in-date(date)
  68. (interactive "sGive me a date: " date)
  69. (setq count (count-matches (concat "DONE.*\n.*" date)))
  70. (message "DONES: %d" count))
  71. (defun org-effectivenes-todos-in-date(date)
  72. (interactive "sGive me a date: " date)
  73. (setq count (count-matches (concat "TODO.*\n.*" date)))
  74. (message "TODOS: %d" count))
  75. (defun org-effectiveness-canceled-in-date(date)
  76. (interactive "sGive me a date: " date)
  77. (setq count (count-matches (concat "CANCEL+ED.*\n.*" date)))
  78. (message "CANCELEDS: %d" count))
  79. (defun org-effectiveness-in-date(date &optional notmessage)
  80. (interactive "sGive me a date: " date)
  81. (save-excursion
  82. (goto-char (point-min))
  83. (let ((done (float (count-matches (concat "* DONE.*\n.*" date))))
  84. (canc (float (count-matches (concat "* CANCEL+ED.*\n.*" date)))))
  85. (if (and (= done canc) (zerop done))
  86. (setq effectiveness 0)
  87. (setq effectiveness (* 100 (/ done (+ done canc)))))
  88. (if (eq notmessage 1)
  89. (message "%d" effectiveness)
  90. (message "Effectiveness: %d " effectiveness)))))
  91. (defun org-effectiveness-month-to-string (m)
  92. (if (< m 10)
  93. (concat "0" (number-to-string m))
  94. (number-to-string m)))
  95. (defun org-effectiveness-plot(startdate enddate)
  96. (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
  97. (setq dates (org-effectiveness-check-dates startdate enddate))
  98. (setq syear (cadr (assoc 'startyear dates)))
  99. (setq smonth (cadr (assoc 'startmonth dates)))
  100. (setq eyear (cadr (assoc 'endyear dates)))
  101. (setq emonth (assoc 'endmonth dates))
  102. ;; Checking the format of the dates
  103. (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" startdate))
  104. (message "The start date must have the next format YYYY-MM"))
  105. (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" enddate))
  106. (message "The end date must have the next format YYYY-MM"))
  107. ;; Checking if startdate < enddate
  108. (if (string-match "^[0-9][0-9][0-9][0-9]" startdate)
  109. (setq startyear (string-to-number (match-string 0 startdate))))
  110. (if (string-match "[0-9][0-9]$" startdate)
  111. (setq startmonth (string-to-number (match-string 0 startdate))))
  112. (if (string-match "^[0-9][0-9][0-9][0-9]" enddate)
  113. (setq endyear (string-to-number (match-string 0 enddate))))
  114. (if (string-match "[0-9][0-9]$" enddate)
  115. (setq endmonth (string-to-number (match-string 0 enddate))))
  116. (if (> startyear endyear)
  117. (message "The start date must be before that end date"))
  118. (if (and (= startyear endyear) (> startmonth endmonth))
  119. (message "The start date must be before that end date"))
  120. ;; Create a file
  121. (let ((month startmonth)
  122. (year startyear)
  123. (str ""))
  124. (while (and (>= endyear year) (>= endmonth month))
  125. (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"))
  126. (if (= month 12)
  127. (progn
  128. (setq year (+ 1 year))
  129. (setq month 1))
  130. (setq month (+ 1 month))))
  131. (write-region str nil "/tmp/org-effectiveness"))
  132. ;; Create the bar graph
  133. (if (file-exists-p "/usr/bin/gnuplot")
  134. (call-process "/bin/bash" nil t nil "-c" "/usr/bin/gnuplot -e 'plot \"/tmp/org-effectiveness\" using 2:xticlabels(1) with histograms' -p")
  135. (message "gnuplot is not installed")))
  136. (defun org-effectiveness-ascii-bar(n &optional label)
  137. "Print a bar with the percentage from 0 to 100 printed in ascii"
  138. (interactive "nPercentage: \nsLabel: ")
  139. (if (or (< n 0) (> n 100))
  140. (message "The percentage must be between 0 to 100")
  141. (let ((x 0)
  142. (y 0)
  143. (z 0))
  144. (insert (format "\n### %s ###" label))
  145. (insert "\n-")
  146. (while (< x n)
  147. (insert "-")
  148. (setq x (+ x 1)))
  149. (insert "+\n")
  150. (insert (format "%d" n))
  151. (if (> n 10)
  152. (setq y (+ y 1)))
  153. (while (< y n)
  154. (insert " ")
  155. (setq y (+ y 1)))
  156. (insert "|\n")
  157. (insert "-")
  158. (while (< z n)
  159. (insert "-")
  160. (setq z (+ z 1)))
  161. (insert "+"))))
  162. (defun org-effectiveness-check-dates (startdate enddate)
  163. "Generate a list with ((startyear startmonth) (endyear endmonth))"
  164. (setq str nil)
  165. (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" startdate))
  166. (setq str "The start date must have the next format YYYY-MM"))
  167. (if (not (string-match "[0-9][0-9][0-9][0-9]-[0-9][0-9]" enddate))
  168. (setq str "The end date must have the next format YYYY-MM"))
  169. ;; Checking if startdate < enddate
  170. (if (string-match "^[0-9][0-9][0-9][0-9]" startdate)
  171. (setq startyear (string-to-number (match-string 0 startdate))))
  172. (if (string-match "[0-9][0-9]$" startdate)
  173. (setq startmonth (string-to-number (match-string 0 startdate))))
  174. (if (string-match "^[0-9][0-9][0-9][0-9]" enddate)
  175. (setq endyear (string-to-number (match-string 0 enddate))))
  176. (if (string-match "[0-9][0-9]$" enddate)
  177. (setq endmonth (string-to-number (match-string 0 enddate))))
  178. (if (> startyear endyear)
  179. (setq str "The start date must be before that end date"))
  180. (if (and (= startyear endyear) (> startmonth endmonth))
  181. (setq str "The start date must be before that end date"))
  182. (if str
  183. (message str)
  184. ;; (list (list startyear startmonth) (list endyear endmonth))))
  185. (list (list 'startyear startyear) (list 'startmonth startmonth) (list 'endyear endyear) (list 'endmonth endmonth))))
  186. (defun org-effectiveness-plot-ascii (startdate enddate)
  187. (interactive "sGive me the start date: \nsGive me the end date: " startdate enddate)
  188. (setq dates (org-effectiveness-check-dates startdate enddate))
  189. (setq syear (cadr (assoc 'startyear dates)))
  190. (setq smonth (cadr (assoc 'startmonth dates)))
  191. (setq eyear (cadr (assoc 'endyear dates)))
  192. (setq emonth (cadr (assoc 'endmonth dates)))
  193. ;; (switch-to-buffer "*org-effectiveness*")
  194. (let ((month smonth)
  195. (year syear)
  196. (str ""))
  197. (while (and (>= eyear year) (>= emonth month))
  198. (org-effectiveness-ascii-bar (string-to-number (org-effectiveness-in-date (concat (number-to-string year) "-" (org-effectiveness-month-to-string month)) 1)) (format "%s-%s" year month))
  199. (if (= month 12)
  200. (progn
  201. (setq year (+ 1 year))
  202. (setq month 1))
  203. (setq month (+ 1 month))))))
  204. (provide 'org-effectiveness)