org-clock.el 34 KB

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