org-clock.el 45 KB

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