org-clock.el 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. ;;; org-clock.el --- The time clocking code for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.26c
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file contains the time clocking code for Org-mode
  25. (require 'org)
  26. (eval-when-compile
  27. (require 'cl)
  28. (require 'calendar))
  29. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  30. (defvar org-time-stamp-formats)
  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 org-log-into-drawer
  36. "Should clocking info be wrapped into a drawer?
  37. When t, clocking info will always be inserted into a :LOGBOOK: 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. When a string, it names the drawer to be used.
  43. The default for this variable is the value of `org-log-into-drawer',
  44. which see."
  45. :group 'org-todo
  46. :group 'org-clock
  47. :type '(choice
  48. (const :tag "Always" t)
  49. (const :tag "Only when drawer exists" nil)
  50. (integer :tag "When at least N clock entries")
  51. (const :tag "Into LOGBOOK drawer" "LOGBOOK")
  52. (string :tag "Into Drawer named...")))
  53. (defcustom org-clock-out-when-done t
  54. "When non-nil, clock will be stopped when the clocked entry is marked DONE.
  55. A nil value means, clock will keep running until stopped explicitly with
  56. `C-c C-x C-o', or until the clock is started in a different item."
  57. :group 'org-clock
  58. :type 'boolean)
  59. (defcustom org-clock-out-remove-zero-time-clocks nil
  60. "Non-nil means, remove the clock line when the resulting time is zero."
  61. :group 'org-clock
  62. :type 'boolean)
  63. (defcustom org-clock-in-switch-to-state nil
  64. "Set task to a special todo state while clocking it.
  65. The value should be the state to which the entry should be
  66. switched. If the value is a function, it must take one
  67. parameter (the current TODO state of the item) and return the
  68. state to switch it to."
  69. :group 'org-clock
  70. :group 'org-todo
  71. :type '(choice
  72. (const :tag "Don't force a state" nil)
  73. (string :tag "State")
  74. (symbol :tag "Function")))
  75. (defcustom org-clock-history-length 5
  76. "Number of clock tasks to remember in history."
  77. :group 'org-clock
  78. :type 'integer)
  79. (defcustom org-clock-heading-function nil
  80. "When non-nil, should be a function to create `org-clock-heading'.
  81. This is the string shown in the mode line when a clock is running.
  82. The function is called with point at the beginning of the headline."
  83. :group 'org-clock
  84. :type 'function)
  85. (defcustom org-clock-string-limit 0
  86. "Maximum length of clock strings in the modeline. 0 means no limit."
  87. :group 'org-clock
  88. :type 'integer)
  89. (defcustom org-clock-in-resume nil
  90. "If non-nil, resume clock when clocking into task with open clock.
  91. When clocking into a task with a clock entry which has not been closed,
  92. the clock can be resumed from that point."
  93. :group 'org-clock
  94. :type 'boolean)
  95. (defcustom org-clock-persist nil
  96. "When non-nil, save the running clock when emacs is closed.
  97. The clock is resumed when emacs restarts.
  98. When this is t, both the running clock, and the entire clock
  99. history are saved. When this is the symbol `clock', only the
  100. running clock is saved.
  101. When Emacs restarts with saved clock information, the file containing the
  102. running clock as well as all files mentioned in the clock history will
  103. be visited.
  104. All this depends on running `org-clock-persistence-insinuate' in .emacs"
  105. :group 'org-clock
  106. :type '(choice
  107. (const :tag "Just the running clock" clock)
  108. (const :tag "Clock and history" t)
  109. (const :tag "No persistence" nil)))
  110. (defcustom org-clock-persist-file (convert-standard-filename
  111. "~/.emacs.d/org-clock-save.el")
  112. "File to save clock data to."
  113. :group 'org-clock
  114. :type 'string)
  115. (defcustom org-clock-persist-query-save nil
  116. "When non-nil, ask before saving the current clock on exit."
  117. :group 'org-clock
  118. :type 'boolean)
  119. (defcustom org-clock-persist-query-resume t
  120. "When non-nil, ask before resuming any stored clock during load."
  121. :group 'org-clock
  122. :type 'boolean)
  123. ;;; The clock for measuring work time.
  124. (defvar org-mode-line-string "")
  125. (put 'org-mode-line-string 'risky-local-variable t)
  126. (defvar org-clock-mode-line-timer nil)
  127. (defvar org-clock-heading "")
  128. (defvar org-clock-heading-for-remember "")
  129. (defvar org-clock-start-time "")
  130. (defvar org-clock-history nil
  131. "List of marker pointing to recent clocked tasks.")
  132. (defvar org-clock-default-task (make-marker)
  133. "Marker pointing to the default task that should clock time.
  134. The clock can be made to switch to this task after clocking out
  135. of a different task.")
  136. (defvar org-clock-interrupted-task (make-marker)
  137. "Marker pointing to the task that has been interrupted by the current clock.")
  138. (defvar org-clock-mode-line-map (make-sparse-keymap))
  139. (define-key org-clock-mode-line-map [mode-line mouse-2] 'org-clock-goto)
  140. (defun org-clock-history-push (&optional pos buffer)
  141. "Push a marker to the clock history."
  142. (setq org-clock-history-length (max 1 (min 35 org-clock-history-length)))
  143. (let ((m (move-marker (make-marker) (or pos (point)) buffer)) n l)
  144. (while (setq n (member m org-clock-history))
  145. (move-marker (car n) nil))
  146. (setq org-clock-history
  147. (delq nil
  148. (mapcar (lambda (x) (if (marker-buffer x) x nil))
  149. org-clock-history)))
  150. (when (>= (setq l (length org-clock-history)) org-clock-history-length)
  151. (setq org-clock-history
  152. (nreverse
  153. (nthcdr (- l org-clock-history-length -1)
  154. (nreverse org-clock-history)))))
  155. (push m org-clock-history)))
  156. (defun org-clock-save-markers-for-cut-and-paste (beg end)
  157. "Save relative positions of markers in region."
  158. (org-check-and-save-marker org-clock-marker beg end)
  159. (org-check-and-save-marker org-clock-default-task beg end)
  160. (org-check-and-save-marker org-clock-interrupted-task beg end)
  161. (mapc (lambda (m) (org-check-and-save-marker m beg end))
  162. org-clock-history))
  163. (defun org-clock-select-task (&optional prompt)
  164. "Select a task that recently was associated with clocking."
  165. (interactive)
  166. (let (sel-list rpl (i 0) s)
  167. (save-window-excursion
  168. (org-switch-to-buffer-other-window
  169. (get-buffer-create "*Clock Task Select*"))
  170. (erase-buffer)
  171. (when (marker-buffer org-clock-default-task)
  172. (insert (org-add-props "Default Task\n" nil 'face 'bold))
  173. (setq s (org-clock-insert-selection-line ?d org-clock-default-task))
  174. (push s sel-list))
  175. (when (marker-buffer org-clock-interrupted-task)
  176. (insert (org-add-props "The task interrupted by starting the last one\n" nil 'face 'bold))
  177. (setq s (org-clock-insert-selection-line ?i org-clock-interrupted-task))
  178. (push s sel-list))
  179. (when (marker-buffer org-clock-marker)
  180. (insert (org-add-props "Current Clocking Task\n" nil 'face 'bold))
  181. (setq s (org-clock-insert-selection-line ?c org-clock-marker))
  182. (push s sel-list))
  183. (insert (org-add-props "Recent Tasks\n" nil 'face 'bold))
  184. (mapc
  185. (lambda (m)
  186. (when (marker-buffer m)
  187. (setq i (1+ i)
  188. s (org-clock-insert-selection-line
  189. (if (< i 10)
  190. (+ i ?0)
  191. (+ i (- ?A 10))) m))
  192. (push s sel-list)))
  193. org-clock-history)
  194. (org-fit-window-to-buffer)
  195. (message (or prompt "Select task for clocking:"))
  196. (setq rpl (read-char-exclusive))
  197. (cond
  198. ((eq rpl ?q) nil)
  199. ((eq rpl ?x) nil)
  200. ((assoc rpl sel-list) (cdr (assoc rpl sel-list)))
  201. (t (error "Invalid task choice %c" rpl))))))
  202. (defun org-clock-insert-selection-line (i marker)
  203. (when (marker-buffer marker)
  204. (let (file cat task)
  205. (with-current-buffer (org-base-buffer (marker-buffer marker))
  206. (save-excursion
  207. (save-restriction
  208. (widen)
  209. (goto-char marker)
  210. (setq file (buffer-file-name (marker-buffer marker))
  211. cat (or (org-get-category)
  212. (progn (org-refresh-category-properties)
  213. (org-get-category)))
  214. task (org-get-heading 'notags)))))
  215. (when (and cat task)
  216. (insert (format "[%c] %-15s %s\n" i cat task))
  217. (cons i marker)))))
  218. (defun org-clock-update-mode-line ()
  219. (let* ((delta (- (time-to-seconds (current-time))
  220. (time-to-seconds org-clock-start-time)))
  221. (h (floor delta 3600))
  222. (m (floor (- delta (* 3600 h)) 60)))
  223. (setq org-mode-line-string
  224. (org-propertize
  225. (let ((clock-string (format (concat "-[" org-time-clocksum-format " (%s)]")
  226. h m org-clock-heading))
  227. (help-text "Org-mode clock is running. Mouse-2 to go there."))
  228. (if (and (> org-clock-string-limit 0)
  229. (> (length clock-string) org-clock-string-limit))
  230. (org-propertize (substring clock-string 0 org-clock-string-limit)
  231. 'help-echo (concat help-text ": " org-clock-heading))
  232. (org-propertize clock-string 'help-echo help-text)))
  233. 'local-map org-clock-mode-line-map
  234. 'mouse-face (if (featurep 'xemacs) 'highlight 'mode-line-highlight)))
  235. (force-mode-line-update)))
  236. (defvar org-clock-mode-line-entry nil
  237. "Information for the modeline about the running clock.")
  238. (defun org-clock-in (&optional select)
  239. "Start the clock on the current item.
  240. If necessary, clock-out of the currently active clock.
  241. With prefix arg SELECT, offer a list of recently clocked tasks to
  242. clock into. When SELECT is `C-u C-u', clock into the current task and mark
  243. is as the default task, a special task that will always be offered in
  244. the clocking selection, associated with the letter `d'."
  245. (interactive "P")
  246. (catch 'abort
  247. (let ((interrupting (marker-buffer org-clock-marker))
  248. ts selected-task target-pos)
  249. (when (equal select '(4))
  250. (setq selected-task (org-clock-select-task "Clock-in on task: "))
  251. (if selected-task
  252. (setq selected-task (copy-marker selected-task))
  253. (error "Abort")))
  254. (when interrupting
  255. ;; We are interrupting the clocking of a different task.
  256. ;; Save a marker to this task, so that we can go back.
  257. (move-marker org-clock-interrupted-task
  258. (marker-position org-clock-marker)
  259. (marker-buffer org-clock-marker))
  260. (org-clock-out t))
  261. (when (equal select '(16))
  262. ;; Mark as default clocking task
  263. (save-excursion
  264. (org-back-to-heading t)
  265. (move-marker org-clock-default-task (point))))
  266. (setq target-pos (point)) ;; we want to clock in at this location
  267. (save-excursion
  268. (when (and selected-task (marker-buffer selected-task))
  269. ;; There is a selected task, move to the correct buffer
  270. ;; and set the new target position.
  271. (set-buffer (org-base-buffer (marker-buffer selected-task)))
  272. (setq target-pos (marker-position selected-task))
  273. (move-marker selected-task nil))
  274. (save-excursion
  275. (save-restriction
  276. (widen)
  277. (goto-char target-pos)
  278. (org-back-to-heading t)
  279. (or interrupting (move-marker org-clock-interrupted-task nil))
  280. (org-clock-history-push)
  281. (cond ((functionp org-clock-in-switch-to-state)
  282. (looking-at org-complex-heading-regexp)
  283. (let ((newstate (funcall org-clock-in-switch-to-state
  284. (match-string 2))))
  285. (if newstate (org-todo newstate))))
  286. ((and org-clock-in-switch-to-state
  287. (not (looking-at (concat outline-regexp "[ \t]*"
  288. org-clock-in-switch-to-state
  289. "\\>"))))
  290. (org-todo org-clock-in-switch-to-state)))
  291. (setq org-clock-heading-for-remember
  292. (and (looking-at org-complex-heading-regexp)
  293. (match-end 4)
  294. (org-trim (buffer-substring (match-end 1)
  295. (match-end 4)))))
  296. (setq org-clock-heading
  297. (cond ((and org-clock-heading-function
  298. (functionp org-clock-heading-function))
  299. (funcall org-clock-heading-function))
  300. ((looking-at org-complex-heading-regexp)
  301. (match-string 4))
  302. (t "???")))
  303. (setq org-clock-heading (org-propertize org-clock-heading
  304. 'face nil))
  305. (org-clock-find-position org-clock-in-resume)
  306. (cond
  307. ((and org-clock-in-resume
  308. (looking-at
  309. (concat "^[ \t]* " org-clock-string
  310. " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
  311. " +\\sw+ +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
  312. (message "Matched %s" (match-string 1))
  313. (setq ts (concat "[" (match-string 1) "]"))
  314. (goto-char (match-end 1))
  315. (setq org-clock-start-time
  316. (apply 'encode-time
  317. (org-parse-time-string (match-string 1)))))
  318. ((eq org-clock-in-resume 'auto-restart)
  319. ;; called from org-clock-load during startup,
  320. ;; do not interrupt, but warn!
  321. (message "Cannot restart clock because task does not contain unfinished clock")
  322. (ding)
  323. (sit-for 2)
  324. (throw 'abort nil))
  325. (t
  326. (insert-before-markers "\n")
  327. (backward-char 1)
  328. (org-indent-line-function)
  329. (when (and (save-excursion
  330. (end-of-line 0)
  331. (org-in-item-p)))
  332. (beginning-of-line 1)
  333. (org-indent-line-to (- (org-get-indentation) 2)))
  334. (insert org-clock-string " ")
  335. (setq org-clock-start-time (current-time))
  336. (setq ts (org-insert-time-stamp org-clock-start-time 'with-hm 'inactive))))
  337. (move-marker org-clock-marker (point) (buffer-base-buffer))
  338. (or global-mode-string (setq global-mode-string '("")))
  339. (or (memq 'org-mode-line-string global-mode-string)
  340. (setq global-mode-string
  341. (append global-mode-string '(org-mode-line-string))))
  342. (org-clock-update-mode-line)
  343. (setq org-clock-mode-line-timer
  344. (run-with-timer 60 60 'org-clock-update-mode-line))
  345. (message "Clock started at %s" ts)))))))
  346. (defun org-clock-find-position (find-unclosed)
  347. "Find the location where the next clock line should be inserted.
  348. When FIND-UNCLOSED is non-nil, first check if there is an unclosed clock
  349. line and position cursor in that line."
  350. (org-back-to-heading t)
  351. (catch 'exit
  352. (let ((beg (save-excursion
  353. (beginning-of-line 2)
  354. (or (bolp) (newline))
  355. (point)))
  356. (end (progn (outline-next-heading) (point)))
  357. (re (concat "^[ \t]*" org-clock-string))
  358. (cnt 0)
  359. (drawer (if (stringp org-clock-into-drawer)
  360. org-clock-into-drawer "LOGBOOK"))
  361. first last ind-last)
  362. (goto-char beg)
  363. (when (and find-unclosed
  364. (re-search-forward
  365. (concat "^[ \t]* " org-clock-string
  366. " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
  367. " +\\sw+ +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")
  368. end t))
  369. (beginning-of-line 1)
  370. (throw 'exit t))
  371. (when (eobp) (newline) (setq end (max (point) end)))
  372. (when (re-search-forward (concat "^[ \t]*:" drawer ":") end t)
  373. ;; we seem to have a CLOCK drawer, so go there.
  374. (beginning-of-line 2)
  375. (or org-log-states-order-reversed
  376. (and (re-search-forward org-property-end-re nil t)
  377. (goto-char (match-beginning 0))))
  378. (throw 'exit t))
  379. ;; Lets count the CLOCK lines
  380. (goto-char beg)
  381. (while (re-search-forward re end t)
  382. (setq first (or first (match-beginning 0))
  383. last (match-beginning 0)
  384. cnt (1+ cnt)))
  385. (when (and (integerp org-clock-into-drawer)
  386. last
  387. (>= (1+ cnt) org-clock-into-drawer))
  388. ;; Wrap current entries into a new drawer
  389. (goto-char last)
  390. (setq ind-last (org-get-indentation))
  391. (beginning-of-line 2)
  392. (if (and (>= (org-get-indentation) ind-last)
  393. (org-at-item-p))
  394. (org-end-of-item))
  395. (insert ":END:\n")
  396. (beginning-of-line 0)
  397. (org-indent-line-to ind-last)
  398. (goto-char first)
  399. (insert ":" drawer ":\n")
  400. (beginning-of-line 0)
  401. (org-indent-line-function)
  402. (org-flag-drawer t)
  403. (beginning-of-line 2)
  404. (or org-log-states-order-reversed
  405. (and (re-search-forward org-property-end-re nil t)
  406. (goto-char (match-beginning 0))))
  407. (throw 'exit nil))
  408. (goto-char beg)
  409. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  410. (not (equal (match-string 1) org-clock-string)))
  411. ;; Planning info, skip to after it
  412. (beginning-of-line 2)
  413. (or (bolp) (newline)))
  414. (when (or (eq org-clock-into-drawer t)
  415. (stringp org-clock-into-drawer)
  416. (and (integerp org-clock-into-drawer)
  417. (< org-clock-into-drawer 2)))
  418. (insert ":" drawer ":\n:END:\n")
  419. (beginning-of-line -1)
  420. (org-indent-line-function)
  421. (org-flag-drawer t)
  422. (beginning-of-line 2)
  423. (org-indent-line-function)
  424. (beginning-of-line)
  425. (or org-log-states-order-reversed
  426. (and (re-search-forward org-property-end-re nil t)
  427. (goto-char (match-beginning 0))))))))
  428. (defun org-clock-out (&optional fail-quietly)
  429. "Stop the currently running clock.
  430. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  431. (interactive)
  432. (catch 'exit
  433. (if (not (marker-buffer org-clock-marker))
  434. (if fail-quietly (throw 'exit t) (error "No active clock")))
  435. (let (ts te s h m remove)
  436. (save-excursion
  437. (set-buffer (marker-buffer org-clock-marker))
  438. (save-restriction
  439. (widen)
  440. (goto-char org-clock-marker)
  441. (beginning-of-line 1)
  442. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  443. (equal (match-string 1) org-clock-string))
  444. (setq ts (match-string 2))
  445. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  446. (goto-char (match-end 0))
  447. (delete-region (point) (point-at-eol))
  448. (insert "--")
  449. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  450. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  451. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  452. h (floor (/ s 3600))
  453. s (- s (* 3600 h))
  454. m (floor (/ s 60))
  455. s (- s (* 60 s)))
  456. (insert " => " (format "%2d:%02d" h m))
  457. (when (setq remove (and org-clock-out-remove-zero-time-clocks
  458. (= (+ h m) 0)))
  459. (beginning-of-line 1)
  460. (delete-region (point) (point-at-eol))
  461. (and (looking-at "\n") (> (point-max) (1+ (point)))
  462. (delete-char 1)))
  463. (move-marker org-clock-marker nil)
  464. (when org-log-note-clock-out
  465. (org-add-log-setup 'clock-out nil nil nil nil
  466. (concat "# Task: " (org-get-heading t) "\n\n")))
  467. (when org-clock-mode-line-timer
  468. (cancel-timer org-clock-mode-line-timer)
  469. (setq org-clock-mode-line-timer nil))
  470. (setq global-mode-string
  471. (delq 'org-mode-line-string global-mode-string))
  472. (force-mode-line-update)
  473. (message (concat "Clock stopped at %s after HH:MM = " org-time-clocksum-format "%s") te h m
  474. (if remove " => LINE REMOVED" "")))))))
  475. (defun org-clock-cancel ()
  476. "Cancel the running clock be removing the start timestamp."
  477. (interactive)
  478. (if (not (marker-buffer org-clock-marker))
  479. (error "No active clock"))
  480. (save-excursion
  481. (set-buffer (marker-buffer org-clock-marker))
  482. (goto-char org-clock-marker)
  483. (delete-region (1- (point-at-bol)) (point-at-eol)))
  484. (setq global-mode-string
  485. (delq 'org-mode-line-string global-mode-string))
  486. (force-mode-line-update)
  487. (message "Clock canceled"))
  488. (defun org-clock-goto (&optional select)
  489. "Go to the currently clocked-in entry, or to the most recently clocked one.
  490. With prefix arg SELECT, offer recently clocked tasks for selection."
  491. (interactive "P")
  492. (let* ((recent nil)
  493. (m (cond
  494. (select
  495. (or (org-clock-select-task "Select task to go to: ")
  496. (error "No task selected")))
  497. ((marker-buffer org-clock-marker) org-clock-marker)
  498. ((and (car org-clock-history)
  499. (marker-buffer (car org-clock-history)))
  500. (setq recent t)
  501. (car org-clock-history))
  502. (t (error "No active or recent clock task")))))
  503. (switch-to-buffer (marker-buffer m))
  504. (if (or (< m (point-min)) (> m (point-max))) (widen))
  505. (goto-char m)
  506. (org-show-entry)
  507. (org-back-to-heading)
  508. (org-cycle-hide-drawers 'children)
  509. (recenter)
  510. (if recent
  511. (message "No running clock, this is the most recently clocked task"))))
  512. (defvar org-clock-file-total-minutes nil
  513. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  514. (make-variable-buffer-local 'org-clock-file-total-minutes)
  515. (defun org-clock-sum (&optional tstart tend)
  516. "Sum the times for each subtree.
  517. Puts the resulting times in minutes as a text property on each headline."
  518. (interactive)
  519. (let* ((bmp (buffer-modified-p))
  520. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  521. org-clock-string
  522. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  523. (lmax 30)
  524. (ltimes (make-vector lmax 0))
  525. (t1 0)
  526. (level 0)
  527. ts te dt
  528. time)
  529. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  530. (save-excursion
  531. (goto-char (point-max))
  532. (while (re-search-backward re nil t)
  533. (cond
  534. ((match-end 2)
  535. ;; Two time stamps
  536. (setq ts (match-string 2)
  537. te (match-string 3)
  538. ts (time-to-seconds
  539. (apply 'encode-time (org-parse-time-string ts)))
  540. te (time-to-seconds
  541. (apply 'encode-time (org-parse-time-string te)))
  542. ts (if tstart (max ts tstart) ts)
  543. te (if tend (min te tend) te)
  544. dt (- te ts)
  545. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  546. ((match-end 4)
  547. ;; A naked time
  548. (setq t1 (+ t1 (string-to-number (match-string 5))
  549. (* 60 (string-to-number (match-string 4))))))
  550. (t ;; A headline
  551. (setq level (- (match-end 1) (match-beginning 1)))
  552. (when (or (> t1 0) (> (aref ltimes level) 0))
  553. (loop for l from 0 to level do
  554. (aset ltimes l (+ (aref ltimes l) t1)))
  555. (setq t1 0 time (aref ltimes level))
  556. (loop for l from level to (1- lmax) do
  557. (aset ltimes l 0))
  558. (goto-char (match-beginning 0))
  559. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  560. (setq org-clock-file-total-minutes (aref ltimes 0)))
  561. (set-buffer-modified-p bmp)))
  562. (defun org-clock-display (&optional total-only)
  563. "Show subtree times in the entire buffer.
  564. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  565. in the echo area."
  566. (interactive)
  567. (org-clock-remove-overlays)
  568. (let (time h m p)
  569. (org-clock-sum)
  570. (unless total-only
  571. (save-excursion
  572. (goto-char (point-min))
  573. (while (or (and (equal (setq p (point)) (point-min))
  574. (get-text-property p :org-clock-minutes))
  575. (setq p (next-single-property-change
  576. (point) :org-clock-minutes)))
  577. (goto-char p)
  578. (when (setq time (get-text-property p :org-clock-minutes))
  579. (org-clock-put-overlay time (funcall outline-level))))
  580. (setq h (/ org-clock-file-total-minutes 60)
  581. m (- org-clock-file-total-minutes (* 60 h)))
  582. ;; Arrange to remove the overlays upon next change.
  583. (when org-remove-highlights-with-change
  584. (org-add-hook 'before-change-functions 'org-clock-remove-overlays
  585. nil 'local))))
  586. (message (concat "Total file time: " org-time-clocksum-format " (%d hours and %d minutes)") h m h m)))
  587. (defvar org-clock-overlays nil)
  588. (make-variable-buffer-local 'org-clock-overlays)
  589. (defun org-clock-put-overlay (time &optional level)
  590. "Put an overlays on the current line, displaying TIME.
  591. If LEVEL is given, prefix time with a corresponding number of stars.
  592. This creates a new overlay and stores it in `org-clock-overlays', so that it
  593. will be easy to remove."
  594. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  595. (l (if level (org-get-valid-level level 0) 0))
  596. (fmt (concat "%s " org-time-clocksum-format "%s"))
  597. (off 0)
  598. ov tx)
  599. (org-move-to-column c)
  600. (unless (eolp) (skip-chars-backward "^ \t"))
  601. (skip-chars-backward " \t")
  602. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  603. tx (concat (buffer-substring (1- (point)) (point))
  604. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  605. (org-add-props (format fmt
  606. (make-string l ?*) h m
  607. (make-string (- 16 l) ?\ ))
  608. (list 'face 'org-clock-overlay))
  609. ""))
  610. (if (not (featurep 'xemacs))
  611. (org-overlay-put ov 'display tx)
  612. (org-overlay-put ov 'invisible t)
  613. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  614. (push ov org-clock-overlays)))
  615. (defun org-clock-remove-overlays (&optional beg end noremove)
  616. "Remove the occur highlights from the buffer.
  617. BEG and END are ignored. If NOREMOVE is nil, remove this function
  618. from the `before-change-functions' in the current buffer."
  619. (interactive)
  620. (unless org-inhibit-highlight-removal
  621. (mapc 'org-delete-overlay org-clock-overlays)
  622. (setq org-clock-overlays nil)
  623. (unless noremove
  624. (remove-hook 'before-change-functions
  625. 'org-clock-remove-overlays 'local))))
  626. (defvar state) ;; dynamically scoped into this function
  627. (defun org-clock-out-if-current ()
  628. "Clock out if the current entry contains the running clock.
  629. This is used to stop the clock after a TODO entry is marked DONE,
  630. and is only done if the variable `org-clock-out-when-done' is not nil."
  631. (when (and org-clock-out-when-done
  632. (member state org-done-keywords)
  633. (equal (or (buffer-base-buffer (marker-buffer org-clock-marker))
  634. (marker-buffer org-clock-marker))
  635. (or (buffer-base-buffer (current-buffer))
  636. (current-buffer)))
  637. (< (point) org-clock-marker)
  638. (> (save-excursion (outline-next-heading) (point))
  639. org-clock-marker))
  640. ;; Clock out, but don't accept a logging message for this.
  641. (let ((org-log-note-clock-out nil))
  642. (org-clock-out))))
  643. (add-hook 'org-after-todo-state-change-hook
  644. 'org-clock-out-if-current)
  645. ;;;###autoload
  646. (defun org-get-clocktable (&rest props)
  647. "Get a formatted clocktable with parameters according to PROPS.
  648. The table is created in a temporary buffer, fully formatted and
  649. fontified, and then returned."
  650. ;; Set the defaults
  651. (setq props (plist-put props :name "clocktable"))
  652. (unless (plist-member props :maxlevel)
  653. (setq props (plist-put props :maxlevel 2)))
  654. (unless (plist-member props :scope)
  655. (setq props (plist-put props :scope 'agenda)))
  656. (with-temp-buffer
  657. (org-mode)
  658. (org-create-dblock props)
  659. (org-update-dblock)
  660. (font-lock-fontify-buffer)
  661. (forward-line 2)
  662. (buffer-substring (point) (progn
  663. (re-search-forward "^#\\+END" nil t)
  664. (point-at-bol)))))
  665. (defun org-clock-report (&optional arg)
  666. "Create a table containing a report about clocked time.
  667. If the cursor is inside an existing clocktable block, then the table
  668. will be updated. If not, a new clocktable will be inserted.
  669. When called with a prefix argument, move to the first clock table in the
  670. buffer and update it."
  671. (interactive "P")
  672. (org-clock-remove-overlays)
  673. (when arg
  674. (org-find-dblock "clocktable")
  675. (org-show-entry))
  676. (if (org-in-clocktable-p)
  677. (goto-char (org-in-clocktable-p))
  678. (org-create-dblock (list :name "clocktable"
  679. :maxlevel 2 :scope 'file)))
  680. (org-update-dblock))
  681. (defun org-in-clocktable-p ()
  682. "Check if the cursor is in a clocktable."
  683. (let ((pos (point)) start)
  684. (save-excursion
  685. (end-of-line 1)
  686. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  687. (setq start (match-beginning 0))
  688. (re-search-forward "^#\\+END:.*" nil t)
  689. (>= (match-end 0) pos)
  690. start))))
  691. (defun org-clock-special-range (key &optional time as-strings)
  692. "Return two times bordering a special time range.
  693. Key is a symbol specifying the range and can be one of `today', `yesterday',
  694. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  695. A week starts Monday 0:00 and ends Sunday 24:00.
  696. The range is determined relative to TIME. TIME defaults to the current time.
  697. The return value is a cons cell with two internal times like the ones
  698. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  699. the returned times will be formatted strings."
  700. (if (integerp key) (setq key (intern (number-to-string key))))
  701. (let* ((tm (decode-time (or time (current-time))))
  702. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  703. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  704. (dow (nth 6 tm))
  705. (skey (symbol-name key))
  706. (shift 0)
  707. s1 m1 h1 d1 month1 y1 diff ts te fm txt w date)
  708. (cond
  709. ((string-match "^[0-9]+$" skey)
  710. (setq y (string-to-number skey) m 1 d 1 key 'year))
  711. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  712. (setq y (string-to-number (match-string 1 skey))
  713. month (string-to-number (match-string 2 skey))
  714. d 1 key 'month))
  715. ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey)
  716. (require 'cal-iso)
  717. (setq y (string-to-number (match-string 1 skey))
  718. w (string-to-number (match-string 2 skey)))
  719. (setq date (calendar-gregorian-from-absolute
  720. (calendar-absolute-from-iso (list w 1 y))))
  721. (setq d (nth 1 date) month (car date) y (nth 2 date)
  722. dow 1
  723. key 'week))
  724. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  725. (setq y (string-to-number (match-string 1 skey))
  726. month (string-to-number (match-string 2 skey))
  727. d (string-to-number (match-string 3 skey))
  728. key 'day))
  729. ((string-match "\\([-+][0-9]+\\)$" skey)
  730. (setq shift (string-to-number (match-string 1 skey))
  731. key (intern (substring skey 0 (match-beginning 1))))))
  732. (when (= shift 0)
  733. (cond ((eq key 'yesterday) (setq key 'today shift -1))
  734. ((eq key 'lastweek) (setq key 'week shift -1))
  735. ((eq key 'lastmonth) (setq key 'month shift -1))
  736. ((eq key 'lastyear) (setq key 'year shift -1))))
  737. (cond
  738. ((memq key '(day today))
  739. (setq d (+ d shift) h 0 m 0 h1 24 m1 0))
  740. ((memq key '(week thisweek))
  741. (setq diff (+ (* -7 shift) (if (= dow 0) 6 (1- dow)))
  742. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  743. ((memq key '(month thismonth))
  744. (setq d 1 h 0 m 0 d1 1 month (+ month shift) month1 (1+ month) h1 0 m1 0))
  745. ((memq key '(year thisyear))
  746. (setq m 0 h 0 d 1 month 1 y (+ y shift) y1 (1+ y)))
  747. (t (error "No such time block %s" key)))
  748. (setq ts (encode-time s m h d month y)
  749. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  750. (or d1 d) (or month1 month) (or y1 y)))
  751. (setq fm (cdr org-time-stamp-formats))
  752. (cond
  753. ((memq key '(day today))
  754. (setq txt (format-time-string "%A, %B %d, %Y" ts)))
  755. ((memq key '(week thisweek))
  756. (setq txt (format-time-string "week %G-W%V" ts)))
  757. ((memq key '(month thismonth))
  758. (setq txt (format-time-string "%B %Y" ts)))
  759. ((memq key '(year thisyear))
  760. (setq txt (format-time-string "the year %Y" ts))))
  761. (if as-strings
  762. (list (format-time-string fm ts) (format-time-string fm te) txt)
  763. (list ts te txt))))
  764. (defun org-clocktable-shift (dir n)
  765. "Try to shift the :block date of the clocktable at point.
  766. Point must be in the #+BEGIN: line of a clocktable, or this function
  767. will throw an error.
  768. DIR is a direction, a symbol `left', `right', `up', or `down'.
  769. Both `left' and `down' shift the block toward the past, `up' and `right'
  770. push it toward the future.
  771. N is the number of shift steps to take. The size of the step depends on
  772. the currently selected interval size."
  773. (setq n (prefix-numeric-value n))
  774. (and (memq dir '(left down)) (setq n (- n)))
  775. (save-excursion
  776. (goto-char (point-at-bol))
  777. (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
  778. (error "Line needs a :block definition before this command works")
  779. (let* ((b (match-beginning 1)) (e (match-end 1))
  780. (s (match-string 1))
  781. block shift ins y mw d date wp m)
  782. (cond
  783. ((equal s "yesterday") (setq s "today-1"))
  784. ((equal s "lastweek") (setq s "thisweek-1"))
  785. ((equal s "lastmonth") (setq s "thismonth-1"))
  786. ((equal s "lastyear") (setq s "thisyear-1")))
  787. (cond
  788. ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s)
  789. (setq block (match-string 1 s)
  790. shift (if (match-end 2)
  791. (string-to-number (match-string 2 s))
  792. 0))
  793. (setq shift (+ shift n))
  794. (setq ins (if (= shift 0) block (format "%s%+d" block shift))))
  795. ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s)
  796. ;; 1 1 2 3 3 4 4 5 6 6 5 2
  797. (setq y (string-to-number (match-string 1 s))
  798. wp (and (match-end 3) (match-string 3 s))
  799. mw (and (match-end 4) (string-to-number (match-string 4 s)))
  800. d (and (match-end 6) (string-to-number (match-string 6 s))))
  801. (cond
  802. (d (setq ins (format-time-string
  803. "%Y-%m-%d"
  804. (encode-time 0 0 0 (+ d n) m y))))
  805. ((and wp mw (> (length wp) 0))
  806. (require 'cal-iso)
  807. (setq date (calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n) 1 y))))
  808. (setq ins (format-time-string
  809. "%G-W%V"
  810. (encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
  811. (mw
  812. (setq ins (format-time-string
  813. "%Y-%m"
  814. (encode-time 0 0 0 1 (+ mw n) y))))
  815. (y
  816. (setq ins (number-to-string (+ y n))))))
  817. (t (error "Cannot shift clocktable block")))
  818. (when ins
  819. (goto-char b)
  820. (insert ins)
  821. (delete-region (point) (+ (point) (- e b)))
  822. (beginning-of-line 1)
  823. (org-update-dblock)
  824. t)))))
  825. (defun org-dblock-write:clocktable (params)
  826. "Write the standard clocktable."
  827. (catch 'exit
  828. (let* ((hlchars '((1 . "*") (2 . "/")))
  829. (ins (make-marker))
  830. (total-time nil)
  831. (scope (plist-get params :scope))
  832. (tostring (plist-get params :tostring))
  833. (multifile (plist-get params :multifile))
  834. (header (plist-get params :header))
  835. (maxlevel (or (plist-get params :maxlevel) 3))
  836. (step (plist-get params :step))
  837. (emph (plist-get params :emphasize))
  838. (ts (plist-get params :tstart))
  839. (te (plist-get params :tend))
  840. (block (plist-get params :block))
  841. (link (plist-get params :link))
  842. ipos time p level hlc hdl content recalc formula pcol
  843. cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list st)
  844. (setq org-clock-file-total-minutes nil)
  845. (when step
  846. (unless (or block (and ts te))
  847. (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
  848. (org-clocktable-steps params)
  849. (throw 'exit nil))
  850. (when block
  851. (setq cc (org-clock-special-range block nil t)
  852. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  853. (when (integerp ts) (setq ts (calendar-gregorian-from-absolute ts)))
  854. (when (integerp te) (setq te (calendar-gregorian-from-absolute te)))
  855. (when (and ts (listp ts))
  856. (setq ts (format "%4d-%02d-%02d" (nth 2 ts) (car ts) (nth 1 ts))))
  857. (when (and te (listp te))
  858. (setq te (format "%4d-%02d-%02d" (nth 2 te) (car te) (nth 1 te))))
  859. ;; Now the times are strings we can parse.
  860. (if ts (setq ts (time-to-seconds
  861. (apply 'encode-time (org-parse-time-string ts)))))
  862. (if te (setq te (time-to-seconds
  863. (apply 'encode-time (org-parse-time-string te)))))
  864. (move-marker ins (point))
  865. (setq ipos (point))
  866. ;; Get the right scope
  867. (setq pos (point))
  868. (cond
  869. ((and scope (listp scope) (symbolp (car scope)))
  870. (setq scope (eval scope)))
  871. ((eq scope 'agenda)
  872. (setq scope (org-agenda-files t)))
  873. ((eq scope 'agenda-with-archives)
  874. (setq scope (org-agenda-files t))
  875. (setq scope (org-add-archive-files scope)))
  876. ((eq scope 'file-with-archives)
  877. (setq scope (org-add-archive-files (list (buffer-file-name)))
  878. rm-file-column t)))
  879. (setq scope-is-list (and scope (listp scope)))
  880. (save-restriction
  881. (cond
  882. ((not scope))
  883. ((eq scope 'file) (widen))
  884. ((eq scope 'subtree) (org-narrow-to-subtree))
  885. ((eq scope 'tree)
  886. (while (org-up-heading-safe))
  887. (org-narrow-to-subtree))
  888. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  889. (symbol-name scope)))
  890. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  891. (catch 'exit
  892. (while (org-up-heading-safe)
  893. (looking-at outline-regexp)
  894. (if (<= (org-reduced-level (funcall outline-level)) level)
  895. (throw 'exit nil))))
  896. (org-narrow-to-subtree))
  897. (scope-is-list
  898. (let* ((files scope)
  899. (scope 'agenda)
  900. (p1 (copy-sequence params))
  901. file)
  902. (setq p1 (plist-put p1 :tostring t))
  903. (setq p1 (plist-put p1 :multifile t))
  904. (setq p1 (plist-put p1 :scope 'file))
  905. (org-prepare-agenda-buffers files)
  906. (while (setq file (pop files))
  907. (with-current-buffer (find-buffer-visiting file)
  908. (setq tbl1 (org-dblock-write:clocktable p1))
  909. (when tbl1
  910. (push (org-clocktable-add-file
  911. file
  912. (concat "| |*File time*|*"
  913. (org-minutes-to-hh:mm-string
  914. org-clock-file-total-minutes)
  915. "*|\n"
  916. tbl1)) tbl)
  917. (setq total-time (+ (or total-time 0)
  918. org-clock-file-total-minutes))))))))
  919. (goto-char pos)
  920. (unless scope-is-list
  921. (org-clock-sum ts te)
  922. (goto-char (point-min))
  923. (setq st t)
  924. (while (or (and (bobp) (prog1 st (setq st nil))
  925. (get-text-property (point) :org-clock-minutes)
  926. (setq p (point-min)))
  927. (setq p (next-single-property-change (point) :org-clock-minutes)))
  928. (goto-char p)
  929. (when (setq time (get-text-property p :org-clock-minutes))
  930. (save-excursion
  931. (beginning-of-line 1)
  932. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  933. (setq level (org-reduced-level
  934. (- (match-end 1) (match-beginning 1))))
  935. (<= level maxlevel))
  936. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  937. hdl (if (not link)
  938. (match-string 2)
  939. (org-make-link-string
  940. (format "file:%s::%s"
  941. (buffer-file-name)
  942. (save-match-data
  943. (org-make-org-heading-search-string
  944. (match-string 2))))
  945. (match-string 2))))
  946. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  947. (push (concat
  948. "| " (int-to-string level) "|" hlc hdl hlc " |"
  949. (make-string (1- level) ?|)
  950. hlc (org-minutes-to-hh:mm-string time) hlc
  951. " |") tbl))))))
  952. (setq tbl (nreverse tbl))
  953. (if tostring
  954. (if tbl (mapconcat 'identity tbl "\n") nil)
  955. (goto-char ins)
  956. (insert-before-markers
  957. (or header
  958. (concat
  959. "Clock summary at ["
  960. (substring
  961. (format-time-string (cdr org-time-stamp-formats))
  962. 1 -1)
  963. "]"
  964. (if block (concat ", for " range-text ".") "")
  965. "\n\n"))
  966. (if scope-is-list "|File" "")
  967. "|L|Headline|Time|\n")
  968. (setq total-time (or total-time org-clock-file-total-minutes))
  969. (insert-before-markers
  970. "|-\n|"
  971. (if scope-is-list "|" "")
  972. "|"
  973. "*Total time*| *"
  974. (org-minutes-to-hh:mm-string (or total-time 0))
  975. "*|\n|-\n")
  976. (setq tbl (delq nil tbl))
  977. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  978. (equal (substring (car tbl) 0 2) "|-"))
  979. (pop tbl))
  980. (insert-before-markers (mapconcat
  981. 'identity (delq nil tbl)
  982. (if scope-is-list "\n|-\n" "\n")))
  983. (backward-delete-char 1)
  984. (if (setq formula (plist-get params :formula))
  985. (cond
  986. ((eq formula '%)
  987. (setq pcol (+ (if scope-is-list 1 0) maxlevel 3))
  988. (insert
  989. (format
  990. "\n#+TBLFM: $%d='(org-clock-time%% @%d$%d $%d..$%d);%%.1f"
  991. pcol
  992. 2
  993. (+ 3 (if scope-is-list 1 0))
  994. (+ (if scope-is-list 1 0) 3)
  995. (1- pcol)))
  996. (setq recalc t))
  997. ((stringp formula)
  998. (insert "\n#+TBLFM: " formula)
  999. (setq recalc t))
  1000. (t (error "invalid formula in clocktable")))
  1001. ;; Should we rescue an old formula?
  1002. (when (stringp (setq content (plist-get params :content)))
  1003. (when (string-match "^\\(#\\+TBLFM:.*\\)" content)
  1004. (setq recalc t)
  1005. (insert "\n" (match-string 1 (plist-get params :content)))
  1006. (beginning-of-line 0))))
  1007. (goto-char ipos)
  1008. (skip-chars-forward "^|")
  1009. (org-table-align)
  1010. (when recalc
  1011. (if (eq formula '%)
  1012. (save-excursion (org-table-goto-column pcol nil 'force)
  1013. (insert "%")))
  1014. (org-table-recalculate 'all))
  1015. (when rm-file-column
  1016. (forward-char 1)
  1017. (org-table-delete-column)))))))
  1018. (defun org-clocktable-steps (params)
  1019. (let* ((p1 (copy-sequence params))
  1020. (ts (plist-get p1 :tstart))
  1021. (te (plist-get p1 :tend))
  1022. (step0 (plist-get p1 :step))
  1023. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  1024. (block (plist-get p1 :block))
  1025. cc range-text)
  1026. (when block
  1027. (setq cc (org-clock-special-range block nil t)
  1028. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  1029. (if ts (setq ts (time-to-seconds
  1030. (apply 'encode-time (org-parse-time-string ts)))))
  1031. (if te (setq te (time-to-seconds
  1032. (apply 'encode-time (org-parse-time-string te)))))
  1033. (setq p1 (plist-put p1 :header ""))
  1034. (setq p1 (plist-put p1 :step nil))
  1035. (setq p1 (plist-put p1 :block nil))
  1036. (while (< ts te)
  1037. (or (bolp) (insert "\n"))
  1038. (setq p1 (plist-put p1 :tstart (format-time-string
  1039. (org-time-stamp-format nil t)
  1040. (seconds-to-time ts))))
  1041. (setq p1 (plist-put p1 :tend (format-time-string
  1042. (org-time-stamp-format nil t)
  1043. (seconds-to-time (setq ts (+ ts step))))))
  1044. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  1045. (plist-get p1 :tstart) "\n")
  1046. (org-dblock-write:clocktable p1)
  1047. (re-search-forward "#\\+END:")
  1048. (end-of-line 0))))
  1049. (defun org-clocktable-add-file (file table)
  1050. (if table
  1051. (let ((lines (org-split-string table "\n"))
  1052. (ff (file-name-nondirectory file)))
  1053. (mapconcat 'identity
  1054. (mapcar (lambda (x)
  1055. (if (string-match org-table-dataline-regexp x)
  1056. (concat "|" ff x)
  1057. x))
  1058. lines)
  1059. "\n"))))
  1060. (defun org-clock-time% (total &rest strings)
  1061. "Compute a time fraction in percent.
  1062. TOTAL s a time string like 10:21 specifying the total times.
  1063. STRINGS is a list of strings that should be checked for a time.
  1064. The first string that does have a time will be used.
  1065. This function is made for clock tables."
  1066. (let ((re "\\([0-9]+\\):\\([0-9]+\\)")
  1067. tot s)
  1068. (save-match-data
  1069. (catch 'exit
  1070. (if (not (string-match re total))
  1071. (throw 'exit 0.)
  1072. (setq tot (+ (string-to-number (match-string 2 total))
  1073. (* 60 (string-to-number (match-string 1 total)))))
  1074. (if (= tot 0.) (throw 'exit 0.)))
  1075. (while (setq s (pop strings))
  1076. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  1077. (throw 'exit
  1078. (/ (* 100.0 (+ (string-to-number (match-string 2 s))
  1079. (* 60 (string-to-number (match-string 1 s)))))
  1080. tot))))
  1081. 0))))
  1082. (defun org-clock-save ()
  1083. "Persist various clock-related data to disk.
  1084. The details of what will be saved are regulated by the variable
  1085. `org-clock-persist'."
  1086. (when org-clock-persist
  1087. (let (b)
  1088. (with-current-buffer (find-file (expand-file-name org-clock-persist-file))
  1089. (progn
  1090. (delete-region (point-min) (point-max))
  1091. ;;Store clock
  1092. (insert (format ";; org-persist.el - %s at %s\n"
  1093. system-name (format-time-string
  1094. (cdr org-time-stamp-formats))))
  1095. (if (and (setq b (marker-buffer org-clock-marker))
  1096. (setq b (or (buffer-base-buffer b) b))
  1097. (buffer-live-p b)
  1098. (buffer-file-name b)
  1099. (or (not org-clock-persist-query-save)
  1100. (y-or-n-p (concat "Save current clock ("
  1101. (substring-no-properties org-clock-heading)
  1102. ") "))))
  1103. (insert "(setq resume-clock '(\""
  1104. (buffer-file-name (marker-buffer org-clock-marker))
  1105. "\" . " (int-to-string (marker-position org-clock-marker))
  1106. "))\n"))
  1107. ;; Store clocked task history. Tasks are stored reversed to make
  1108. ;; reading simpler
  1109. (when (and org-clock-history (eq org-clock-persist t))
  1110. (insert
  1111. "(setq stored-clock-history '("
  1112. (mapconcat
  1113. (lambda (m)
  1114. (when (and (setq b (marker-buffer m))
  1115. (setq b (or (buffer-base-buffer b) b))
  1116. (buffer-live-p b)
  1117. (buffer-file-name b))
  1118. (concat "(\"" (buffer-file-name b)
  1119. "\" . " (int-to-string (marker-position m))
  1120. ")")))
  1121. (reverse org-clock-history) " ") "))\n"))
  1122. (save-buffer)
  1123. (kill-buffer (current-buffer)))))))
  1124. (defvar org-clock-loaded nil
  1125. "Was the clock file loaded?")
  1126. (defun org-clock-load ()
  1127. "Load clock-related data from disk, maybe resuming a stored clock."
  1128. (when (and org-clock-persist (not org-clock-loaded))
  1129. (let ((filename (expand-file-name org-clock-persist-file))
  1130. (org-clock-in-resume 'auto-restart)
  1131. resume-clock stored-clock-history)
  1132. (if (not (file-readable-p filename))
  1133. (message "Not restoring clock data; %s not found"
  1134. org-clock-persist-file)
  1135. (message "%s" "Restoring clock data")
  1136. (setq org-clock-loaded t)
  1137. (load-file filename)
  1138. ;; load history
  1139. (when stored-clock-history
  1140. (save-window-excursion
  1141. (mapc (lambda (task)
  1142. (if (file-exists-p (car task))
  1143. (org-clock-history-push (cdr task)
  1144. (find-file (car task)))))
  1145. stored-clock-history)))
  1146. ;; resume clock
  1147. (when (and resume-clock org-clock-persist
  1148. (file-exists-p (car resume-clock))
  1149. (or (not org-clock-persist-query-resume)
  1150. (y-or-n-p
  1151. (concat
  1152. "Resume clock ("
  1153. (with-current-buffer (find-file (car resume-clock))
  1154. (save-excursion
  1155. (goto-char (cdr resume-clock))
  1156. (org-back-to-heading t)
  1157. (and (looking-at org-complex-heading-regexp)
  1158. (match-string 4))))
  1159. ") "))))
  1160. (when (file-exists-p (car resume-clock))
  1161. (with-current-buffer (find-file (car resume-clock))
  1162. (goto-char (cdr resume-clock))
  1163. (org-clock-in)
  1164. (if (org-invisible-p)
  1165. (org-show-context)))))))))
  1166. ;;;###autoload
  1167. (defun org-clock-persistence-insinuate ()
  1168. "Set up hooks for clock persistence"
  1169. (add-hook 'org-mode-hook 'org-clock-load)
  1170. (add-hook 'kill-emacs-hook 'org-clock-save))
  1171. (provide 'org-clock)
  1172. ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c
  1173. ;;; org-clock.el ends here