org-clock.el 34 KB

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