org-clock.el 27 KB

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