org-clock.el 29 KB

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