org-clock.el 34 KB

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