org-clock.el 45 KB

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