org-clock.el 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. ;;; org-clock.el --- The time clocking code for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.02b
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file contains the time clocking code for Org-mode
  24. (require 'org)
  25. (eval-when-compile
  26. (require 'cl)
  27. (require 'calendar))
  28. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  29. (defgroup org-clock nil
  30. "Options concerning clocking working time in Org-mode."
  31. :tag "Org Clock"
  32. :group 'org-progress)
  33. (defcustom org-clock-into-drawer 2
  34. "Should clocking info be wrapped into a drawer?
  35. When t, clocking info will always be inserted into a :CLOCK: drawer.
  36. If necessary, the drawer will be created.
  37. When nil, the drawer will not be created, but used when present.
  38. When an integer and the number of clocking entries in an item
  39. reaches or exceeds this number, a drawer will be created."
  40. :group 'org-todo
  41. :group 'org-clock
  42. :type '(choice
  43. (const :tag "Always" t)
  44. (const :tag "Only when drawer exists" nil)
  45. (integer :tag "When at least N clock entries")))
  46. (defcustom org-clock-out-when-done t
  47. "When t, the clock will be stopped when the relevant entry is marked DONE.
  48. Nil means, clock will keep running until stopped explicitly with
  49. `C-c C-x C-o', or until the clock is started in a different item."
  50. :group 'org-clock
  51. :type 'boolean)
  52. (defcustom org-clock-out-remove-zero-time-clocks nil
  53. "Non-nil means, remove the clock line when the resulting time is zero."
  54. :group 'org-clock
  55. :type 'boolean)
  56. (defcustom org-clock-in-switch-to-state nil
  57. "Set task to a special todo state while clocking it.
  58. The value should be the state to which the entry should be switched."
  59. :group 'org-clock
  60. :group 'org-todo
  61. :type '(choice
  62. (const :tag "Don't force a state" nil)
  63. (string :tag "State")))
  64. (defcustom org-clock-history-length 5
  65. "Number of clock tasks to remember in history."
  66. :group 'org-clock
  67. :type 'integer)
  68. (defcustom org-clock-heading-function nil
  69. "When non-nil, should be a function to create `org-clock-heading'.
  70. This is the string shown in the mode line when a clock is running.
  71. The function is called with point at the beginning of the headline."
  72. :group 'org-clock
  73. :type 'function)
  74. ;;; The clock for measuring work time.
  75. (defvar org-mode-line-string "")
  76. (put 'org-mode-line-string 'risky-local-variable t)
  77. (defvar org-mode-line-timer nil)
  78. (defvar org-clock-heading "")
  79. (defvar org-clock-start-time "")
  80. (defvar org-clock-history nil
  81. "List of marker pointing to recent clocked tasks.")
  82. (defvar org-clock-default-task (make-marker)
  83. "Marker pointing to the default task that should clock time.
  84. The clock can be made to switch to this task after clocking out
  85. of a different task.")
  86. (defvar org-clock-interrupted-task (make-marker)
  87. "Marker pointing to the task that has been interrupted by the current clock.")
  88. (defun org-clock-history-push (&optional pos buffer)
  89. "Push a marker to the clock history."
  90. (setq org-clock-history-length (max 1 (min 42 org-clock-history-length)))
  91. (let ((m (move-marker (make-marker) (or pos (point)) buffer)) n l)
  92. (while (setq n (member m org-clock-history))
  93. (move-marker (car n) nil))
  94. (setq org-clock-history
  95. (delq nil
  96. (mapcar (lambda (x) (if (marker-buffer x) x nil))
  97. org-clock-history)))
  98. (when (>= (setq l (length org-clock-history)) org-clock-history-length)
  99. (setq org-clock-history
  100. (nreverse
  101. (nthcdr (- l org-clock-history-length -1)
  102. (nreverse org-clock-history)))))
  103. (push m org-clock-history)))
  104. (defun org-clock-save-markers-for-cut-and-paste (beg end)
  105. "Save relative positions of markers in region."
  106. (org-check-and-save-marker org-clock-marker beg end)
  107. (org-check-and-save-marker org-clock-default-task beg end)
  108. (org-check-and-save-marker org-clock-interrupted-task beg end)
  109. (mapc (lambda (m) (org-check-and-save-marker m beg end))
  110. org-clock-history))
  111. (defun org-clock-select-task (&optional prompt)
  112. "Select a task that recently was associated with clocking."
  113. (interactive)
  114. (let (sel-list rpl file task (i 0) s)
  115. (save-window-excursion
  116. (org-switch-to-buffer-other-window
  117. (get-buffer-create "*Clock Task Select*"))
  118. (erase-buffer)
  119. (when (marker-buffer org-clock-default-task)
  120. (insert (org-add-props "Default Task\n" nil 'face 'bold))
  121. (setq s (org-clock-insert-selection-line ?d org-clock-default-task))
  122. (push s sel-list))
  123. (when (marker-buffer org-clock-interrupted-task)
  124. (insert (org-add-props "The task interrupted by starting the last one\n" nil 'face 'bold))
  125. (setq s (org-clock-insert-selection-line ?i org-clock-interrupted-task))
  126. (push s sel-list))
  127. (when (marker-buffer org-clock-marker)
  128. (insert (org-add-props "Current Clocking Task\n" nil 'face 'bold))
  129. (setq s (org-clock-insert-selection-line ?c org-clock-marker))
  130. (push s sel-list))
  131. (insert (org-add-props "Recent Tasks\n" nil 'face 'bold))
  132. (mapc
  133. (lambda (m)
  134. (when (marker-buffer m)
  135. (setq i (1+ i)
  136. s (org-clock-insert-selection-line
  137. (+ i ?0) m))
  138. (push s sel-list)))
  139. org-clock-history)
  140. (if (fboundp 'fit-window-to-buffer)
  141. (fit-window-to-buffer)
  142. (shrink-window-if-larger-than-buffer))
  143. (message (or prompt "Select task for clocking:"))
  144. (setq rpl (read-char-exclusive))
  145. (cond
  146. ((eq rpl ?q) nil)
  147. ((eq rpl ?x) nil)
  148. ((assoc rpl sel-list) (cdr (assoc rpl sel-list)))
  149. (t (error "Invalid task choice %c" rpl))))))
  150. (defun org-clock-insert-selection-line (i marker)
  151. (when (marker-buffer marker)
  152. (let (file cat task)
  153. (with-current-buffer (org-base-buffer (marker-buffer marker))
  154. (save-excursion
  155. (save-restriction
  156. (widen)
  157. (goto-char marker)
  158. (setq file (buffer-file-name (marker-buffer marker))
  159. cat (or (org-get-category)
  160. (progn (org-refresh-category-properties)
  161. (org-get-category)))
  162. task (org-get-heading 'notags)))))
  163. (when (and cat task)
  164. (insert (format "[%c] %-15s %s\n" i cat task))
  165. (cons i marker)))))
  166. (defun org-update-mode-line ()
  167. (let* ((delta (- (time-to-seconds (current-time))
  168. (time-to-seconds org-clock-start-time)))
  169. (h (floor delta 3600))
  170. (m (floor (- delta (* 3600 h)) 60)))
  171. (setq org-mode-line-string
  172. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  173. 'help-echo "Org-mode clock is running"))
  174. (force-mode-line-update)))
  175. (defvar org-clock-mode-line-entry nil
  176. "Information for the modeline about the running clock.")
  177. (defun org-clock-in (&optional select)
  178. "Start the clock on the current item.
  179. If necessary, clock-out of the currently active clock.
  180. With prefix arg SELECT, offer a list of recently clocked ta sks to
  181. clock into. When SELECT is `C-u C-u', clock into the current task and mark
  182. is as the default task, a special task that will always be offered in
  183. the clocking selection, associated with the letter `d'."
  184. (interactive "P")
  185. (let ((interrupting (marker-buffer org-clock-marker))
  186. ts selected-task target-pos)
  187. (when (equal select '(4))
  188. (setq selected-task (org-clock-select-task "Clock-in on task: "))
  189. (if selected-task
  190. (setq selected-task (copy-marker selected-task))
  191. (error "Abort")))
  192. (when interrupting
  193. ;; We are interrupting the clocking of a differnt task.
  194. ;; Save a marker to this task, so that we can go back.
  195. (move-marker org-clock-interrupted-task
  196. (marker-position org-clock-marker)
  197. (marker-buffer org-clock-marker))
  198. (org-clock-out t))
  199. (when (equal select '(16))
  200. ;; Mark as default clocking task
  201. (save-excursion
  202. (org-back-to-heading t)
  203. (move-marker org-clock-default-task (point))))
  204. (setq target-pos (point)) ;; we want to clock in at this location
  205. (save-excursion
  206. (when (and selected-task (marker-buffer selected-task))
  207. ;; There is a selected task, move to the correct buffer
  208. ;; and set the new target position.
  209. (set-buffer (org-base-buffer (marker-buffer selected-task)))
  210. (setq target-pos (marker-position selected-task))
  211. (move-marker selected-task nil))
  212. (save-excursion
  213. (save-restriction
  214. (widen)
  215. (goto-char target-pos)
  216. (org-back-to-heading t)
  217. (or interrupting (move-marker org-clock-interrupted-task nil))
  218. (org-clock-history-push)
  219. (when (and org-clock-in-switch-to-state
  220. (not (looking-at (concat outline-regexp "[ \t]*"
  221. org-clock-in-switch-to-state
  222. "\\>"))))
  223. (org-todo org-clock-in-switch-to-state))
  224. (if (and org-clock-heading-function
  225. (functionp org-clock-heading-function))
  226. (setq org-clock-heading (funcall org-clock-heading-function))
  227. (if (looking-at org-complex-heading-regexp)
  228. (setq org-clock-heading (match-string 4))
  229. (setq org-clock-heading "???")))
  230. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  231. (org-clock-find-position)
  232. (insert "\n") (backward-char 1)
  233. (indent-relative)
  234. (insert org-clock-string " ")
  235. (setq org-clock-start-time (current-time))
  236. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  237. (move-marker org-clock-marker (point) (buffer-base-buffer))
  238. (or global-mode-string (setq global-mode-string '("")))
  239. (or (memq 'org-mode-line-string global-mode-string)
  240. (setq global-mode-string
  241. (append global-mode-string '(org-mode-line-string))))
  242. (org-update-mode-line)
  243. (setq org-mode-line-timer
  244. (run-with-timer 60 60 'org-update-mode-line))
  245. (message "Clock started at %s" ts))))))
  246. (defun org-clock-find-position ()
  247. "Find the location where the next clock line should be inserted."
  248. (org-back-to-heading t)
  249. (catch 'exit
  250. (let ((beg (save-excursion
  251. (beginning-of-line 2)
  252. (or (bolp) (newline))
  253. (point)))
  254. (end (progn (outline-next-heading) (point)))
  255. (re (concat "^[ \t]*" org-clock-string))
  256. (cnt 0)
  257. first last)
  258. (goto-char beg)
  259. (when (eobp) (newline) (setq end (max (point) end)))
  260. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  261. ;; we seem to have a CLOCK drawer, so go there.
  262. (beginning-of-line 2)
  263. (throw 'exit t))
  264. ;; Lets count the CLOCK lines
  265. (goto-char beg)
  266. (while (re-search-forward re end t)
  267. (setq first (or first (match-beginning 0))
  268. last (match-beginning 0)
  269. cnt (1+ cnt)))
  270. (when (and (integerp org-clock-into-drawer)
  271. (>= (1+ cnt) org-clock-into-drawer))
  272. ;; Wrap current entries into a new drawer
  273. (goto-char last)
  274. (beginning-of-line 2)
  275. (insert ":END:\n")
  276. (beginning-of-line 0)
  277. (org-indent-line-function)
  278. (goto-char first)
  279. (insert ":CLOCK:\n")
  280. (beginning-of-line 0)
  281. (org-indent-line-function)
  282. (org-flag-drawer t)
  283. (beginning-of-line 2)
  284. (throw 'exit nil))
  285. (goto-char beg)
  286. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  287. (not (equal (match-string 1) org-clock-string)))
  288. ;; Planning info, skip to after it
  289. (beginning-of-line 2)
  290. (or (bolp) (newline)))
  291. (when (eq t org-clock-into-drawer)
  292. (insert ":CLOCK:\n:END:\n")
  293. (beginning-of-line -1)
  294. (org-indent-line-function)
  295. (org-flag-drawer t)
  296. (beginning-of-line 2)
  297. (org-indent-line-function)))))
  298. (defun org-clock-out (&optional fail-quietly)
  299. "Stop the currently running clock.
  300. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  301. (interactive)
  302. (catch 'exit
  303. (if (not (marker-buffer org-clock-marker))
  304. (if fail-quietly (throw 'exit t) (error "No active clock")))
  305. (let (ts te s h m remove)
  306. (save-excursion
  307. (set-buffer (marker-buffer org-clock-marker))
  308. (save-restriction
  309. (widen)
  310. (goto-char org-clock-marker)
  311. (beginning-of-line 1)
  312. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  313. (equal (match-string 1) org-clock-string))
  314. (setq ts (match-string 2))
  315. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  316. (goto-char (match-end 0))
  317. (delete-region (point) (point-at-eol))
  318. (insert "--")
  319. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  320. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  321. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  322. h (floor (/ s 3600))
  323. s (- s (* 3600 h))
  324. m (floor (/ s 60))
  325. s (- s (* 60 s)))
  326. (insert " => " (format "%2d:%02d" h m))
  327. (when (setq remove (and org-clock-out-remove-zero-time-clocks
  328. (= (+ h m) 0)))
  329. (beginning-of-line 1)
  330. (delete-region (point) (point-at-eol))
  331. (and (looking-at "\n") (> (point-max) (1+ (point)))
  332. (delete-char 1)))
  333. (move-marker org-clock-marker nil)
  334. (when org-log-note-clock-out
  335. (org-add-log-setup 'clock-out))
  336. (when org-mode-line-timer
  337. (cancel-timer org-mode-line-timer)
  338. (setq org-mode-line-timer nil))
  339. (setq global-mode-string
  340. (delq 'org-mode-line-string global-mode-string))
  341. (force-mode-line-update)
  342. (message "Clock stopped at %s after HH:MM = %d:%02d%s" te h m
  343. (if remove " => LINE REMOVED" "")))))))
  344. (defun org-clock-cancel ()
  345. "Cancel the running clock be removing the start timestamp."
  346. (interactive)
  347. (if (not (marker-buffer org-clock-marker))
  348. (error "No active clock"))
  349. (save-excursion
  350. (set-buffer (marker-buffer org-clock-marker))
  351. (goto-char org-clock-marker)
  352. (delete-region (1- (point-at-bol)) (point-at-eol)))
  353. (setq global-mode-string
  354. (delq 'org-mode-line-string global-mode-string))
  355. (force-mode-line-update)
  356. (message "Clock canceled"))
  357. (defun org-clock-goto (&optional select)
  358. "Go to the currently clocked-in entry.
  359. With prefix arg SELECT, offer recently clocked tasks."
  360. (interactive "P")
  361. (let ((m (if select
  362. (org-clock-select-task "Select task to go to: ")
  363. org-clock-marker)))
  364. (if (not (marker-buffer m))
  365. (if select
  366. (error "No task selected")
  367. (error "No active clock")))
  368. (switch-to-buffer (marker-buffer m))
  369. (if (or (< m (point-min)) (> m (point-max))) (widen))
  370. (goto-char m)
  371. (org-show-entry)
  372. (org-back-to-heading)
  373. (org-cycle-hide-drawers 'children)
  374. (recenter)))
  375. (defvar org-clock-file-total-minutes nil
  376. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  377. (make-variable-buffer-local 'org-clock-file-total-minutes)
  378. (defun org-clock-sum (&optional tstart tend)
  379. "Sum the times for each subtree.
  380. Puts the resulting times in minutes as a text property on each headline."
  381. (interactive)
  382. (let* ((bmp (buffer-modified-p))
  383. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  384. org-clock-string
  385. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  386. (lmax 30)
  387. (ltimes (make-vector lmax 0))
  388. (t1 0)
  389. (level 0)
  390. ts te dt
  391. time)
  392. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  393. (save-excursion
  394. (goto-char (point-max))
  395. (while (re-search-backward re nil t)
  396. (cond
  397. ((match-end 2)
  398. ;; Two time stamps
  399. (setq ts (match-string 2)
  400. te (match-string 3)
  401. ts (time-to-seconds
  402. (apply 'encode-time (org-parse-time-string ts)))
  403. te (time-to-seconds
  404. (apply 'encode-time (org-parse-time-string te)))
  405. ts (if tstart (max ts tstart) ts)
  406. te (if tend (min te tend) te)
  407. dt (- te ts)
  408. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  409. ((match-end 4)
  410. ;; A naket time
  411. (setq t1 (+ t1 (string-to-number (match-string 5))
  412. (* 60 (string-to-number (match-string 4))))))
  413. (t ;; A headline
  414. (setq level (- (match-end 1) (match-beginning 1)))
  415. (when (or (> t1 0) (> (aref ltimes level) 0))
  416. (loop for l from 0 to level do
  417. (aset ltimes l (+ (aref ltimes l) t1)))
  418. (setq t1 0 time (aref ltimes level))
  419. (loop for l from level to (1- lmax) do
  420. (aset ltimes l 0))
  421. (goto-char (match-beginning 0))
  422. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  423. (setq org-clock-file-total-minutes (aref ltimes 0)))
  424. (set-buffer-modified-p bmp)))
  425. (defun org-clock-display (&optional total-only)
  426. "Show subtree times in the entire buffer.
  427. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  428. in the echo area."
  429. (interactive)
  430. (org-remove-clock-overlays)
  431. (let (time h m p)
  432. (org-clock-sum)
  433. (unless total-only
  434. (save-excursion
  435. (goto-char (point-min))
  436. (while (or (and (equal (setq p (point)) (point-min))
  437. (get-text-property p :org-clock-minutes))
  438. (setq p (next-single-property-change
  439. (point) :org-clock-minutes)))
  440. (goto-char p)
  441. (when (setq time (get-text-property p :org-clock-minutes))
  442. (org-put-clock-overlay time (funcall outline-level))))
  443. (setq h (/ org-clock-file-total-minutes 60)
  444. m (- org-clock-file-total-minutes (* 60 h)))
  445. ;; Arrange to remove the overlays upon next change.
  446. (when org-remove-highlights-with-change
  447. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  448. nil 'local))))
  449. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  450. (defvar org-clock-overlays nil)
  451. (make-variable-buffer-local 'org-clock-overlays)
  452. (defun org-put-clock-overlay (time &optional level)
  453. "Put an overlays on the current line, displaying TIME.
  454. If LEVEL is given, prefix time with a corresponding number of stars.
  455. This creates a new overlay and stores it in `org-clock-overlays', so that it
  456. will be easy to remove."
  457. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  458. (l (if level (org-get-valid-level level 0) 0))
  459. (off 0)
  460. ov tx)
  461. (org-move-to-column c)
  462. (unless (eolp) (skip-chars-backward "^ \t"))
  463. (skip-chars-backward " \t")
  464. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  465. tx (concat (buffer-substring (1- (point)) (point))
  466. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  467. (org-add-props (format "%s %2d:%02d%s"
  468. (make-string l ?*) h m
  469. (make-string (- 16 l) ?\ ))
  470. '(face secondary-selection))
  471. ""))
  472. (if (not (featurep 'xemacs))
  473. (org-overlay-put ov 'display tx)
  474. (org-overlay-put ov 'invisible t)
  475. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  476. (push ov org-clock-overlays)))
  477. (defun org-remove-clock-overlays (&optional beg end noremove)
  478. "Remove the occur highlights from the buffer.
  479. BEG and END are ignored. If NOREMOVE is nil, remove this function
  480. from the `before-change-functions' in the current buffer."
  481. (interactive)
  482. (unless org-inhibit-highlight-removal
  483. (mapc 'org-delete-overlay org-clock-overlays)
  484. (setq org-clock-overlays nil)
  485. (unless noremove
  486. (remove-hook 'before-change-functions
  487. 'org-remove-clock-overlays 'local))))
  488. (defvar state) ;; dynamically scoped into this function
  489. (defun org-clock-out-if-current ()
  490. "Clock out if the current entry contains the running clock.
  491. This is used to stop the clock after a TODO entry is marked DONE,
  492. and is only done if the variable `org-clock-out-when-done' is not nil."
  493. (when (and org-clock-out-when-done
  494. (member state org-done-keywords)
  495. (equal (marker-buffer org-clock-marker) (current-buffer))
  496. (< (point) org-clock-marker)
  497. (> (save-excursion (outline-next-heading) (point))
  498. org-clock-marker))
  499. ;; Clock out, but don't accept a logging message for this.
  500. (let ((org-log-note-clock-out nil))
  501. (org-clock-out))))
  502. (add-hook 'org-after-todo-state-change-hook
  503. 'org-clock-out-if-current)
  504. ;;;###autoload
  505. (defun org-get-clocktable (&rest props)
  506. "Get a formatted clocktable with parameters according to PROPS.
  507. The table is created in a temporary buffer, fully formatted and
  508. fontified, and then returned."
  509. ;; Set the defaults
  510. (setq props (plist-put props :name "clocktable"))
  511. (unless (plist-member props :maxlevel)
  512. (setq props (plist-put props :maxlevel 2)))
  513. (unless (plist-member props :scope)
  514. (setq props (plist-put props :scope 'agenda)))
  515. (with-temp-buffer
  516. (org-mode)
  517. (org-create-dblock props)
  518. (org-update-dblock)
  519. (font-lock-fontify-buffer)
  520. (forward-line 2)
  521. (buffer-substring (point) (progn
  522. (re-search-forward "^#\\+END" nil t)
  523. (point-at-bol)))))
  524. (defun org-clock-report (&optional arg)
  525. "Create a table containing a report about clocked time.
  526. If the cursor is inside an existing clocktable block, then the table
  527. will be updated. If not, a new clocktable will be inserted.
  528. When called with a prefix argument, move to the first clock table in the
  529. buffer and update it."
  530. (interactive "P")
  531. (org-remove-clock-overlays)
  532. (when arg
  533. (org-find-dblock "clocktable")
  534. (org-show-entry))
  535. (if (org-in-clocktable-p)
  536. (goto-char (org-in-clocktable-p))
  537. (org-create-dblock (list :name "clocktable"
  538. :maxlevel 2 :scope 'file)))
  539. (org-update-dblock))
  540. (defun org-in-clocktable-p ()
  541. "Check if the cursor is in a clocktable."
  542. (let ((pos (point)) start)
  543. (save-excursion
  544. (end-of-line 1)
  545. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  546. (setq start (match-beginning 0))
  547. (re-search-forward "^#\\+END:.*" nil t)
  548. (>= (match-end 0) pos)
  549. start))))
  550. (defun org-clock-special-range (key &optional time as-strings)
  551. "Return two times bordering a special time range.
  552. Key is a symbol specifying the range and can be one of `today', `yesterday',
  553. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  554. A week starts Monday 0:00 and ends Sunday 24:00.
  555. The range is determined relative to TIME. TIME defaults to the current time.
  556. The return value is a cons cell with two internal times like the ones
  557. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  558. the returned times will be formatted strings."
  559. (if (integerp key) (setq key (intern (number-to-string key))))
  560. (let* ((tm (decode-time (or time (current-time))))
  561. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  562. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  563. (dow (nth 6 tm))
  564. (skey (symbol-name key))
  565. (shift 0)
  566. s1 m1 h1 d1 month1 y1 diff ts te fm txt w date)
  567. (cond
  568. ((string-match "^[0-9]+$" skey)
  569. (setq y (string-to-number skey) m 1 d 1 key 'year))
  570. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  571. (setq y (string-to-number (match-string 1 skey))
  572. month (string-to-number (match-string 2 skey))
  573. d 1 key 'month))
  574. ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey)
  575. (require 'cal-iso)
  576. (setq y (string-to-number (match-string 1 skey))
  577. w (string-to-number (match-string 2 skey)))
  578. (setq date (calendar-gregorian-from-absolute
  579. (calendar-absolute-from-iso (list w 1 y))))
  580. (setq d (nth 1 date) month (car date) y (nth 2 date)
  581. key 'week))
  582. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  583. (setq y (string-to-number (match-string 1 skey))
  584. month (string-to-number (match-string 2 skey))
  585. d (string-to-number (match-string 3 skey))
  586. key 'day))
  587. ((string-match "\\([-+][0-9]+\\)$" skey)
  588. (setq shift (string-to-number (match-string 1 skey))
  589. key (intern (substring skey 0 (match-beginning 1))))))
  590. (unless shift
  591. (cond ((eq key 'yesterday) (setq key 'today shift -1))
  592. ((eq key 'lastweek) (setq key 'week shift -1))
  593. ((eq key 'lastmonth) (setq key 'month shift -1))
  594. ((eq key 'lastyear) (setq key 'year shift -1))))
  595. (cond
  596. ((memq key '(day today))
  597. (setq d (+ d shift) h 0 m 0 h1 24 m1 0))
  598. ((memq key '(week thisweek))
  599. (setq diff (+ (* -7 shift) (if (= dow 0) 6 (1- dow)))
  600. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  601. ((memq key '(month thismonth))
  602. (setq d 1 h 0 m 0 d1 1 month (+ month shift) month1 (1+ month) h1 0 m1 0))
  603. ((memq key '(year thisyear))
  604. (setq m 0 h 0 d 1 month 1 y (+ y shift) y1 (1+ y)))
  605. (t (error "No such time block %s" key)))
  606. (setq ts (encode-time s m h d month y)
  607. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  608. (or d1 d) (or month1 month) (or y1 y)))
  609. (setq fm (cdr org-time-stamp-formats))
  610. (cond
  611. ((memq key '(day today))
  612. (setq txt (format-time-string "%A, %B %d, %Y" ts)))
  613. ((memq key '(week thisweek))
  614. (setq txt (format-time-string "week %G-W%V" ts)))
  615. ((memq key '(month thismonth))
  616. (setq txt (format-time-string "%B %Y" ts)))
  617. ((memq key '(year thisyear))
  618. (setq txt (format-time-string "the year %Y" ts))))
  619. (if as-strings
  620. (list (format-time-string fm ts) (format-time-string fm te) txt)
  621. (list ts te txt))))
  622. (defun org-clocktable-shift (dir n)
  623. "Try to shift the :block date of the clocktable at point.
  624. Point must be in the #+BEGIN: line of a clocktable, or this function
  625. will throw an error.
  626. DIR is a direction, a symbol `left', `right', `up', or `down'.
  627. Both `left' and `down' shift the block toward the past, `up' and `right'
  628. push it toward the future.
  629. N is the number of shift steps to take. The size of the step depends on
  630. the currently selected interval size."
  631. (setq n (prefix-numeric-value n))
  632. (and (memq dir '(left down)) (setq n (- n)))
  633. (save-excursion
  634. (goto-char (point-at-bol))
  635. (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
  636. (error "Line needs a :block definition before this command works")
  637. (let* ((b (match-beginning 1)) (e (match-end 1))
  638. (s (match-string 1))
  639. block shift ins y mw d date wp m)
  640. (cond
  641. ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s)
  642. (setq block (match-string 1 s)
  643. shift (if (match-end 2)
  644. (string-to-number (match-string 2 s))
  645. 0))
  646. (setq shift (+ shift n))
  647. (setq ins (if (= shift 0) block (format "%s%+d" block shift))))
  648. ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s)
  649. ;; 1 1 2 3 3 4 4 5 6 6 5 2
  650. (setq y (string-to-number (match-string 1 s))
  651. wp (and (match-end 3) (match-string 3 s))
  652. mw (and (match-end 4) (string-to-number (match-string 4 s)))
  653. d (and (match-end 6) (string-to-number (match-string 6 s))))
  654. (cond
  655. (d (setq ins (format-time-string
  656. "%Y-%m-%d"
  657. (encode-time 0 0 0 (+ d n) m y))))
  658. ((and wp mw (> (length wp) 0))
  659. (require 'cal-iso)
  660. (setq date (calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n) 1 y))))
  661. (setq ins (format-time-string
  662. "%G-W%V"
  663. (encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
  664. (mw
  665. (setq ins (format-time-string
  666. "%Y-%m"
  667. (encode-time 0 0 0 1 (+ mw n) y))))
  668. (y
  669. (setq ins (number-to-string (+ y n))))))
  670. (t (error "Cannot shift clocktable block")))
  671. (when ins
  672. (goto-char b)
  673. (insert ins)
  674. (delete-region (point) (+ (point) (- e b)))
  675. (beginning-of-line 1)
  676. (org-update-dblock)
  677. t)))))
  678. (defun org-dblock-write:clocktable (params)
  679. "Write the standard clocktable."
  680. (catch 'exit
  681. (let* ((hlchars '((1 . "*") (2 . "/")))
  682. (ins (make-marker))
  683. (total-time nil)
  684. (scope (plist-get params :scope))
  685. (tostring (plist-get params :tostring))
  686. (multifile (plist-get params :multifile))
  687. (header (plist-get params :header))
  688. (maxlevel (or (plist-get params :maxlevel) 3))
  689. (step (plist-get params :step))
  690. (emph (plist-get params :emphasize))
  691. (ts (plist-get params :tstart))
  692. (te (plist-get params :tend))
  693. (block (plist-get params :block))
  694. (link (plist-get params :link))
  695. ipos time p level hlc hdl
  696. cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list)
  697. (setq org-clock-file-total-minutes nil)
  698. (when step
  699. (unless (or block (and ts te))
  700. (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
  701. (org-clocktable-steps params)
  702. (throw 'exit nil))
  703. (when block
  704. (setq cc (org-clock-special-range block nil t)
  705. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  706. (when (integerp ts) (setq ts (calendar-gregorian-from-absolute ts)))
  707. (when (integerp te) (setq te (calendar-gregorian-from-absolute te)))
  708. (when (and ts (listp ts))
  709. (setq ts (format "%4d-%02d-%02d" (nth 2 ts) (car ts) (nth 1 ts))))
  710. (when (and te (listp te))
  711. (setq te (format "%4d-%02d-%02d" (nth 2 te) (car te) (nth 1 te))))
  712. ;; Now the times are strings we can parse.
  713. (if ts (setq ts (time-to-seconds
  714. (apply 'encode-time (org-parse-time-string ts)))))
  715. (if te (setq te (time-to-seconds
  716. (apply 'encode-time (org-parse-time-string te)))))
  717. (move-marker ins (point))
  718. (setq ipos (point))
  719. ;; Get the right scope
  720. (setq pos (point))
  721. (cond
  722. ((and scope (listp scope) (symbolp (car scope)))
  723. (setq scope (eval scope)))
  724. ((eq scope 'agenda)
  725. (setq scope (org-agenda-files t)))
  726. ((eq scope 'agenda-with-archives)
  727. (setq scope (org-agenda-files t))
  728. (setq scope (org-add-archive-files scope)))
  729. ((eq scope 'file-with-archives)
  730. (setq scope (org-add-archive-files (list (buffer-file-name)))
  731. rm-file-column t)))
  732. (setq scope-is-list (and scope (listp scope)))
  733. (save-restriction
  734. (cond
  735. ((not scope))
  736. ((eq scope 'file) (widen))
  737. ((eq scope 'subtree) (org-narrow-to-subtree))
  738. ((eq scope 'tree)
  739. (while (org-up-heading-safe))
  740. (org-narrow-to-subtree))
  741. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  742. (symbol-name scope)))
  743. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  744. (catch 'exit
  745. (while (org-up-heading-safe)
  746. (looking-at outline-regexp)
  747. (if (<= (org-reduced-level (funcall outline-level)) level)
  748. (throw 'exit nil))))
  749. (org-narrow-to-subtree))
  750. (scope-is-list
  751. (let* ((files scope)
  752. (scope 'agenda)
  753. (p1 (copy-sequence params))
  754. file)
  755. (setq p1 (plist-put p1 :tostring t))
  756. (setq p1 (plist-put p1 :multifile t))
  757. (setq p1 (plist-put p1 :scope 'file))
  758. (org-prepare-agenda-buffers files)
  759. (while (setq file (pop files))
  760. (with-current-buffer (find-buffer-visiting file)
  761. (setq tbl1 (org-dblock-write:clocktable p1))
  762. (when tbl1
  763. (push (org-clocktable-add-file
  764. file
  765. (concat "| |*File time*|*"
  766. (org-minutes-to-hh:mm-string
  767. org-clock-file-total-minutes)
  768. "*|\n"
  769. tbl1)) tbl)
  770. (setq total-time (+ (or total-time 0)
  771. org-clock-file-total-minutes))))))))
  772. (goto-char pos)
  773. (unless scope-is-list
  774. (org-clock-sum ts te)
  775. (goto-char (point-min))
  776. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  777. (goto-char p)
  778. (when (setq time (get-text-property p :org-clock-minutes))
  779. (save-excursion
  780. (beginning-of-line 1)
  781. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  782. (setq level (org-reduced-level
  783. (- (match-end 1) (match-beginning 1))))
  784. (<= level maxlevel))
  785. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  786. hdl (if (not link)
  787. (match-string 2)
  788. (org-make-link-string
  789. (format "file:%s::%s"
  790. (buffer-file-name)
  791. (save-match-data
  792. (org-make-org-heading-search-string
  793. (match-string 2))))
  794. (match-string 2))))
  795. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  796. (push (concat
  797. "| " (int-to-string level) "|" hlc hdl hlc " |"
  798. (make-string (1- level) ?|)
  799. hlc (org-minutes-to-hh:mm-string time) hlc
  800. " |") tbl))))))
  801. (setq tbl (nreverse tbl))
  802. (if tostring
  803. (if tbl (mapconcat 'identity tbl "\n") nil)
  804. (goto-char ins)
  805. (insert-before-markers
  806. (or header
  807. (concat
  808. "Clock summary at ["
  809. (substring
  810. (format-time-string (cdr org-time-stamp-formats))
  811. 1 -1)
  812. "]"
  813. (if block (concat ", for " range-text ".") "")
  814. "\n\n"))
  815. (if scope-is-list "|File" "")
  816. "|L|Headline|Time|\n")
  817. (setq total-time (or total-time org-clock-file-total-minutes))
  818. (insert-before-markers
  819. "|-\n|"
  820. (if scope-is-list "|" "")
  821. "|"
  822. "*Total time*| *"
  823. (org-minutes-to-hh:mm-string (or total-time 0))
  824. "*|\n|-\n")
  825. (setq tbl (delq nil tbl))
  826. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  827. (equal (substring (car tbl) 0 2) "|-"))
  828. (pop tbl))
  829. (insert-before-markers (mapconcat
  830. 'identity (delq nil tbl)
  831. (if scope-is-list "\n|-\n" "\n")))
  832. (backward-delete-char 1)
  833. (goto-char ipos)
  834. (skip-chars-forward "^|")
  835. (org-table-align)
  836. (when rm-file-column
  837. (forward-char 1)
  838. (org-table-delete-column)))))))
  839. (defun org-clocktable-steps (params)
  840. (let* ((p1 (copy-sequence params))
  841. (ts (plist-get p1 :tstart))
  842. (te (plist-get p1 :tend))
  843. (step0 (plist-get p1 :step))
  844. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  845. (block (plist-get p1 :block))
  846. cc range-text)
  847. (when block
  848. (setq cc (org-clock-special-range block nil t)
  849. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  850. (if ts (setq ts (time-to-seconds
  851. (apply 'encode-time (org-parse-time-string ts)))))
  852. (if te (setq te (time-to-seconds
  853. (apply 'encode-time (org-parse-time-string te)))))
  854. (setq p1 (plist-put p1 :header ""))
  855. (setq p1 (plist-put p1 :step nil))
  856. (setq p1 (plist-put p1 :block nil))
  857. (while (< ts te)
  858. (or (bolp) (insert "\n"))
  859. (setq p1 (plist-put p1 :tstart (format-time-string
  860. (car org-time-stamp-formats)
  861. (seconds-to-time ts))))
  862. (setq p1 (plist-put p1 :tend (format-time-string
  863. (car org-time-stamp-formats)
  864. (seconds-to-time (setq ts (+ ts step))))))
  865. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  866. (plist-get p1 :tstart) "\n")
  867. (org-dblock-write:clocktable p1)
  868. (re-search-forward "#\\+END:")
  869. (end-of-line 0))))
  870. (defun org-clocktable-add-file (file table)
  871. (if table
  872. (let ((lines (org-split-string table "\n"))
  873. (ff (file-name-nondirectory file)))
  874. (mapconcat 'identity
  875. (mapcar (lambda (x)
  876. (if (string-match org-table-dataline-regexp x)
  877. (concat "|" ff x)
  878. x))
  879. lines)
  880. "\n"))))
  881. (provide 'org-clock)
  882. ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c
  883. ;;; org-clock.el ends here