org-clock.el 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866
  1. ;;; org-clock.el --- The time clocking code for Org-mode
  2. ;; Copyright (C) 2004-2013 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. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file contains the time clocking code for Org-mode
  23. (require 'org-exp)
  24. ;;; Code:
  25. (eval-when-compile
  26. (require 'cl))
  27. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  28. (declare-function notifications-notify "notifications" (&rest params))
  29. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  30. (declare-function org-refresh-properties "org" (dprop tprop))
  31. (defvar org-time-stamp-formats)
  32. (defvar org-ts-what)
  33. (defvar org-frame-title-format-backup frame-title-format)
  34. (defgroup org-clock nil
  35. "Options concerning clocking working time in Org-mode."
  36. :tag "Org Clock"
  37. :group 'org-progress)
  38. (defcustom org-clock-into-drawer org-log-into-drawer
  39. "Should clocking info be wrapped into a drawer?
  40. When t, clocking info will always be inserted into a :LOGBOOK: drawer.
  41. If necessary, the drawer will be created.
  42. When nil, the drawer will not be created, but used when present.
  43. When an integer and the number of clocking entries in an item
  44. reaches or exceeds this number, a drawer will be created.
  45. When a string, it names the drawer to be used.
  46. The default for this variable is the value of `org-log-into-drawer',
  47. which see."
  48. :group 'org-todo
  49. :group 'org-clock
  50. :type '(choice
  51. (const :tag "Always" t)
  52. (const :tag "Only when drawer exists" nil)
  53. (integer :tag "When at least N clock entries")
  54. (const :tag "Into LOGBOOK drawer" "LOGBOOK")
  55. (string :tag "Into Drawer named...")))
  56. (defun org-clock-into-drawer ()
  57. "Return the value of `org-clock-into-drawer', but let properties overrule.
  58. If the current entry has or inherits a CLOCK_INTO_DRAWER
  59. property, it will be used instead of the default value; otherwise
  60. if the current entry has or inherits a LOG_INTO_DRAWER property,
  61. it will be used instead of the default value.
  62. The default is the value of the customizable variable `org-clock-into-drawer',
  63. which see."
  64. (let ((p (org-entry-get nil "CLOCK_INTO_DRAWER" 'inherit))
  65. (q (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  66. (cond
  67. ((or (not (or p q)) (equal p "nil") (equal q "nil")) org-clock-into-drawer)
  68. ((or (equal p "t") (equal q "t")) "LOGBOOK")
  69. ((not p) q)
  70. (t p))))
  71. (defcustom org-clock-out-when-done t
  72. "When non-nil, clock will be stopped when the clocked entry is marked DONE.
  73. DONE here means any DONE-like state.
  74. A nil value means clock will keep running until stopped explicitly with
  75. `C-c C-x C-o', or until the clock is started in a different item.
  76. Instead of t, this can also be a list of TODO states that should trigger
  77. clocking out."
  78. :group 'org-clock
  79. :type '(choice
  80. (const :tag "No" nil)
  81. (const :tag "Yes, when done" t)
  82. (repeat :tag "State list"
  83. (string :tag "TODO keyword"))))
  84. (defcustom org-clock-rounding-minutes 0
  85. "Rounding minutes when clocking in or out.
  86. The default value is 0 so that no rounding is done.
  87. When set to a non-integer value, use the car of
  88. `org-time-stamp-rounding-minutes', like for setting a time-stamp.
  89. E.g. if `org-clock-rounding-minutes' is set to 5, time is 14:47
  90. and you clock in: then the clock starts at 14:45. If you clock
  91. out within the next 5 minutes, the clock line will be removed;
  92. if you clock out 8 minutes after your clocked in, the clock
  93. out time will be 14:50."
  94. :group 'org-clock
  95. :version "24.3"
  96. :type '(choice
  97. (integer :tag "Minutes (0 for no rounding)")
  98. (symbol :tag "Use `org-time-stamp-rounding-minutes'" 'same-as-time-stamp)))
  99. (defcustom org-clock-out-remove-zero-time-clocks nil
  100. "Non-nil means remove the clock line when the resulting time is zero."
  101. :group 'org-clock
  102. :type 'boolean)
  103. (defcustom org-clock-in-switch-to-state nil
  104. "Set task to a special todo state while clocking it.
  105. The value should be the state to which the entry should be
  106. switched. If the value is a function, it must take one
  107. parameter (the current TODO state of the item) and return the
  108. state to switch it to."
  109. :group 'org-clock
  110. :group 'org-todo
  111. :type '(choice
  112. (const :tag "Don't force a state" nil)
  113. (string :tag "State")
  114. (symbol :tag "Function")))
  115. (defcustom org-clock-out-switch-to-state nil
  116. "Set task to a special todo state after clocking out.
  117. The value should be the state to which the entry should be
  118. switched. If the value is a function, it must take one
  119. parameter (the current TODO state of the item) and return the
  120. state to switch it to."
  121. :group 'org-clock
  122. :group 'org-todo
  123. :type '(choice
  124. (const :tag "Don't force a state" nil)
  125. (string :tag "State")
  126. (symbol :tag "Function")))
  127. (defcustom org-clock-history-length 5
  128. "Number of clock tasks to remember in history."
  129. :group 'org-clock
  130. :type 'integer)
  131. (defcustom org-clock-goto-may-find-recent-task t
  132. "Non-nil means `org-clock-goto' can go to recent task if no active clock."
  133. :group 'org-clock
  134. :type 'boolean)
  135. (defcustom org-clock-heading-function nil
  136. "When non-nil, should be a function to create `org-clock-heading'.
  137. This is the string shown in the mode line when a clock is running.
  138. The function is called with point at the beginning of the headline."
  139. :group 'org-clock
  140. :type 'function)
  141. (defcustom org-clock-string-limit 0
  142. "Maximum length of clock strings in the mode line. 0 means no limit."
  143. :group 'org-clock
  144. :type 'integer)
  145. (defcustom org-clock-in-resume nil
  146. "If non-nil, resume clock when clocking into task with open clock.
  147. When clocking into a task with a clock entry which has not been closed,
  148. the clock can be resumed from that point."
  149. :group 'org-clock
  150. :type 'boolean)
  151. (defcustom org-clock-persist nil
  152. "When non-nil, save the running clock when Emacs is closed.
  153. The clock is resumed when Emacs restarts.
  154. When this is t, both the running clock, and the entire clock
  155. history are saved. When this is the symbol `clock', only the
  156. running clock is saved. When this is the symbol `history', only
  157. the clock history is saved.
  158. When Emacs restarts with saved clock information, the file containing
  159. the running clock as well as all files mentioned in the clock history
  160. will be visited.
  161. All this depends on running `org-clock-persistence-insinuate' in your
  162. Emacs initialization file."
  163. :group 'org-clock
  164. :type '(choice
  165. (const :tag "Just the running clock" clock)
  166. (const :tag "Just the history" history)
  167. (const :tag "Clock and history" t)
  168. (const :tag "No persistence" nil)))
  169. (defcustom org-clock-persist-file (convert-standard-filename
  170. "~/.emacs.d/org-clock-save.el")
  171. "File to save clock data to."
  172. :group 'org-clock
  173. :type 'string)
  174. (defcustom org-clock-persist-query-save nil
  175. "When non-nil, ask before saving the current clock on exit."
  176. :group 'org-clock
  177. :type 'boolean)
  178. (defcustom org-clock-persist-query-resume t
  179. "When non-nil, ask before resuming any stored clock during load."
  180. :group 'org-clock
  181. :type 'boolean)
  182. (defcustom org-clock-sound nil
  183. "Sound to use for notifications.
  184. Possible values are:
  185. nil No sound played
  186. t Standard Emacs beep
  187. file name Play this sound file, fall back to beep"
  188. :group 'org-clock
  189. :type '(choice
  190. (const :tag "No sound" nil)
  191. (const :tag "Standard beep" t)
  192. (file :tag "Play sound file")))
  193. (org-define-obsolete-variable-alias 'org-clock-modeline-total
  194. 'org-clock-mode-line-total "24.3")
  195. (defcustom org-clock-mode-line-total 'auto
  196. "Default setting for the time included for the mode line clock.
  197. This can be overruled locally using the CLOCK_MODELINE_TOTAL property.
  198. Allowed values are:
  199. current Only the time in the current instance of the clock
  200. today All time clocked into this task today
  201. repeat All time clocked into this task since last repeat
  202. all All time ever recorded for this task
  203. auto Automatically, either `all', or `repeat' for repeating tasks"
  204. :group 'org-clock
  205. :type '(choice
  206. (const :tag "Current clock" current)
  207. (const :tag "Today's task time" today)
  208. (const :tag "Since last repeat" repeat)
  209. (const :tag "All task time" all)
  210. (const :tag "Automatically, `all' or since `repeat'" auto)))
  211. (defvaralias 'org-task-overrun-text 'org-clock-task-overrun-text)
  212. (defcustom org-clock-task-overrun-text nil
  213. "Extra mode line text to indicate that the clock is overrun.
  214. The can be nil to indicate that instead of adding text, the clock time
  215. should get a different face (`org-mode-line-clock-overrun').
  216. When this is a string, it is prepended to the clock string as an indication,
  217. also using the face `org-mode-line-clock-overrun'."
  218. :group 'org-clock
  219. :version "24.1"
  220. :type '(choice
  221. (const :tag "Just mark the time string" nil)
  222. (string :tag "Text to prepend")))
  223. (defcustom org-show-notification-handler nil
  224. "Function or program to send notification with.
  225. The function or program will be called with the notification
  226. string as argument."
  227. :group 'org-clock
  228. :type '(choice
  229. (string :tag "Program")
  230. (function :tag "Function")))
  231. (defgroup org-clocktable nil
  232. "Options concerning the clock table in Org-mode."
  233. :tag "Org Clock Table"
  234. :group 'org-clock)
  235. (defcustom org-clocktable-defaults
  236. (list
  237. :maxlevel 2
  238. :lang org-export-default-language
  239. :scope 'file
  240. :block nil
  241. :wstart 1
  242. :tstart nil
  243. :tend nil
  244. :step nil
  245. :stepskip0 nil
  246. :fileskip0 nil
  247. :tags nil
  248. :emphasize nil
  249. :link nil
  250. :narrow '40!
  251. :indent t
  252. :formula nil
  253. :timestamp nil
  254. :level nil
  255. :tcolumns nil
  256. :formatter nil)
  257. "Default properties for clock tables."
  258. :group 'org-clock
  259. :version "24.1"
  260. :type 'plist)
  261. (defcustom org-clock-clocktable-formatter 'org-clocktable-write-default
  262. "Function to turn clocking data into a table.
  263. For more information, see `org-clocktable-write-default'."
  264. :group 'org-clocktable
  265. :version "24.1"
  266. :type 'function)
  267. ;; FIXME: translate es and nl last string "Clock summary at"
  268. (defcustom org-clock-clocktable-language-setup
  269. '(("en" "File" "L" "Timestamp" "Headline" "Time" "ALL" "Total time" "File time" "Clock summary at")
  270. ("es" "Archivo" "N" "Fecha y hora" "Tarea" "Tiempo" "TODO" "Tiempo total" "Tiempo archivo" "Clock summary at")
  271. ("fr" "Fichier" "N" "Horodatage" "En-tête" "Durée" "TOUT" "Durée totale" "Durée fichier" "Horodatage sommaire à")
  272. ("nl" "Bestand" "N" "Tijdstip" "Hoofding" "Duur" "ALLES" "Totale duur" "Bestandstijd" "Clock summary at"))
  273. "Terms used in clocktable, translated to different languages."
  274. :group 'org-clocktable
  275. :version "24.1"
  276. :type 'alist)
  277. (defcustom org-clock-clocktable-default-properties '(:maxlevel 2 :scope file)
  278. "Default properties for new clocktables.
  279. These will be inserted into the BEGIN line, to make it easy for users to
  280. play with them."
  281. :group 'org-clocktable
  282. :type 'plist)
  283. (defcustom org-clock-idle-time nil
  284. "When non-nil, resolve open clocks if the user is idle more than X minutes."
  285. :group 'org-clock
  286. :type '(choice
  287. (const :tag "Never" nil)
  288. (integer :tag "After N minutes")))
  289. (defcustom org-clock-auto-clock-resolution 'when-no-clock-is-running
  290. "When to automatically resolve open clocks found in Org buffers."
  291. :group 'org-clock
  292. :type '(choice
  293. (const :tag "Never" nil)
  294. (const :tag "Always" t)
  295. (const :tag "When no clock is running" when-no-clock-is-running)))
  296. (defcustom org-clock-report-include-clocking-task nil
  297. "When non-nil, include the current clocking task time in clock reports."
  298. :group 'org-clock
  299. :version "24.1"
  300. :type 'boolean)
  301. (defcustom org-clock-resolve-expert nil
  302. "Non-nil means do not show the splash buffer with the clock resolver."
  303. :group 'org-clock
  304. :version "24.1"
  305. :type 'boolean)
  306. (defcustom org-clock-continuously nil
  307. "Non-nil means to start clocking from the last clock-out time, if any."
  308. :type 'boolean
  309. :version "24.1"
  310. :group 'org-clock)
  311. (defcustom org-clock-total-time-cell-format "*%s*"
  312. "Format string for the total time cells."
  313. :group 'org-clock
  314. :version "24.1"
  315. :type 'boolean)
  316. (defcustom org-clock-file-time-cell-format "*%s*"
  317. "Format string for the file time cells."
  318. :group 'org-clock
  319. :version "24.1"
  320. :type 'boolean)
  321. (defcustom org-clock-clocked-in-display 'mode-line
  322. "When clocked in for a task, org-mode can display the current
  323. task and accumulated time in the mode line and/or frame title.
  324. Allowed values are:
  325. both displays in both mode line and frame title
  326. mode-line displays only in mode line (default)
  327. frame-title displays only in frame title
  328. nil current clock is not displayed"
  329. :group 'org-clock
  330. :type '(choice
  331. (const :tag "Mode line" mode-line)
  332. (const :tag "Frame title" frame-title)
  333. (const :tag "Both" both)
  334. (const :tag "None" nil)))
  335. (defcustom org-clock-frame-title-format '(t org-mode-line-string)
  336. "The value for `frame-title-format' when clocking in.
  337. When `org-clock-clocked-in-display' is set to 'frame-title
  338. or 'both, clocking in will replace `frame-title-format' with
  339. this value. Clocking out will restore `frame-title-format'.
  340. `org-frame-title-string' is a format string using the same
  341. specifications than `frame-title-format', which see."
  342. :version "24.1"
  343. :group 'org-clock
  344. :type 'sexp)
  345. (defvar org-clock-in-prepare-hook nil
  346. "Hook run when preparing the clock.
  347. This hook is run before anything happens to the task that
  348. you want to clock in. For example, you can use this hook
  349. to add an effort property.")
  350. (defvar org-clock-in-hook nil
  351. "Hook run when starting the clock.")
  352. (defvar org-clock-out-hook nil
  353. "Hook run when stopping the current clock.")
  354. (defvar org-clock-cancel-hook nil
  355. "Hook run when cancelling the current clock.")
  356. (defvar org-clock-goto-hook nil
  357. "Hook run when selecting the currently clocked-in entry.")
  358. (defvar org-clock-has-been-used nil
  359. "Has the clock been used during the current Emacs session?")
  360. ;;; The clock for measuring work time.
  361. (defvar org-mode-line-string "")
  362. (put 'org-mode-line-string 'risky-local-variable t)
  363. (defvar org-clock-mode-line-timer nil)
  364. (defvar org-clock-idle-timer nil)
  365. (defvar org-clock-heading) ; defined in org.el
  366. (defvar org-clock-heading-for-remember "")
  367. (defvar org-clock-start-time "")
  368. (defvar org-clock-leftover-time nil
  369. "If non-nil, user cancelled a clock; this is when leftover time started.")
  370. (defvar org-clock-effort ""
  371. "Effort estimate of the currently clocking task.")
  372. (defvar org-clock-total-time nil
  373. "Holds total time, spent previously on currently clocked item.
  374. This does not include the time in the currently running clock.")
  375. (defvar org-clock-history nil
  376. "List of marker pointing to recent clocked tasks.")
  377. (defvar org-clock-default-task (make-marker)
  378. "Marker pointing to the default task that should clock time.
  379. The clock can be made to switch to this task after clocking out
  380. of a different task.")
  381. (defvar org-clock-interrupted-task (make-marker)
  382. "Marker pointing to the task that has been interrupted by the current clock.")
  383. (defvar org-clock-mode-line-map (make-sparse-keymap))
  384. (define-key org-clock-mode-line-map [mode-line mouse-2] 'org-clock-goto)
  385. (define-key org-clock-mode-line-map [mode-line mouse-1] 'org-clock-menu)
  386. (defun org-clock-menu ()
  387. (interactive)
  388. (popup-menu
  389. '("Clock"
  390. ["Clock out" org-clock-out t]
  391. ["Change effort estimate" org-clock-modify-effort-estimate t]
  392. ["Go to clock entry" org-clock-goto t]
  393. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"])))
  394. (defun org-clock-history-push (&optional pos buffer)
  395. "Push a marker to the clock history."
  396. (setq org-clock-history-length (max 1 (min 35 org-clock-history-length)))
  397. (let ((m (move-marker (make-marker)
  398. (or pos (point)) (org-base-buffer
  399. (or buffer (current-buffer)))))
  400. n l)
  401. (while (setq n (member m org-clock-history))
  402. (move-marker (car n) nil))
  403. (setq org-clock-history
  404. (delq nil
  405. (mapcar (lambda (x) (if (marker-buffer x) x nil))
  406. org-clock-history)))
  407. (when (>= (setq l (length org-clock-history)) org-clock-history-length)
  408. (setq org-clock-history
  409. (nreverse
  410. (nthcdr (- l org-clock-history-length -1)
  411. (nreverse org-clock-history)))))
  412. (push m org-clock-history)))
  413. (defun org-clock-save-markers-for-cut-and-paste (beg end)
  414. "Save relative positions of markers in region."
  415. (org-check-and-save-marker org-clock-marker beg end)
  416. (org-check-and-save-marker org-clock-hd-marker beg end)
  417. (org-check-and-save-marker org-clock-default-task beg end)
  418. (org-check-and-save-marker org-clock-interrupted-task beg end)
  419. (mapc (lambda (m) (org-check-and-save-marker m beg end))
  420. org-clock-history))
  421. (defun org-clocking-buffer ()
  422. "Return the clocking buffer if we are currently clocking a task or nil."
  423. (marker-buffer org-clock-marker))
  424. (defun org-clocking-p ()
  425. "Return t when clocking a task."
  426. (not (equal (org-clocking-buffer) nil)))
  427. (defvar org-clock-before-select-task-hook nil
  428. "Hook called in task selection just before prompting the user.")
  429. (defun org-clock-select-task (&optional prompt)
  430. "Select a task that recently was associated with clocking."
  431. (interactive)
  432. (let (sel-list rpl (i 0) s)
  433. (save-window-excursion
  434. (org-switch-to-buffer-other-window
  435. (get-buffer-create "*Clock Task Select*"))
  436. (erase-buffer)
  437. (when (marker-buffer org-clock-default-task)
  438. (insert (org-add-props "Default Task\n" nil 'face 'bold))
  439. (setq s (org-clock-insert-selection-line ?d org-clock-default-task))
  440. (push s sel-list))
  441. (when (marker-buffer org-clock-interrupted-task)
  442. (insert (org-add-props "The task interrupted by starting the last one\n" nil 'face 'bold))
  443. (setq s (org-clock-insert-selection-line ?i org-clock-interrupted-task))
  444. (push s sel-list))
  445. (when (org-clocking-p)
  446. (insert (org-add-props "Current Clocking Task\n" nil 'face 'bold))
  447. (setq s (org-clock-insert-selection-line ?c org-clock-marker))
  448. (push s sel-list))
  449. (insert (org-add-props "Recent Tasks\n" nil 'face 'bold))
  450. (mapc
  451. (lambda (m)
  452. (when (marker-buffer m)
  453. (setq i (1+ i)
  454. s (org-clock-insert-selection-line
  455. (if (< i 10)
  456. (+ i ?0)
  457. (+ i (- ?A 10))) m))
  458. (if (fboundp 'int-to-char) (setf (car s) (int-to-char (car s))))
  459. (push s sel-list)))
  460. org-clock-history)
  461. (run-hooks 'org-clock-before-select-task-hook)
  462. (org-fit-window-to-buffer)
  463. (message (or prompt "Select task for clocking:"))
  464. (setq rpl (read-char-exclusive))
  465. (cond
  466. ((eq rpl ?q) nil)
  467. ((eq rpl ?x) nil)
  468. ((assoc rpl sel-list) (cdr (assoc rpl sel-list)))
  469. (t (error "Invalid task choice %c" rpl))))))
  470. (defun org-clock-insert-selection-line (i marker)
  471. "Insert a line for the clock selection menu.
  472. And return a cons cell with the selection character integer and the marker
  473. pointing to it."
  474. (when (marker-buffer marker)
  475. (let (file cat task heading prefix)
  476. (with-current-buffer (org-base-buffer (marker-buffer marker))
  477. (save-excursion
  478. (save-restriction
  479. (widen)
  480. (ignore-errors
  481. (goto-char marker)
  482. (setq file (buffer-file-name (marker-buffer marker))
  483. cat (org-get-category)
  484. heading (org-get-heading 'notags)
  485. prefix (save-excursion
  486. (org-back-to-heading t)
  487. (looking-at org-outline-regexp)
  488. (match-string 0))
  489. task (substring
  490. (org-fontify-like-in-org-mode
  491. (concat prefix heading)
  492. org-odd-levels-only)
  493. (length prefix)))))))
  494. (when (and cat task)
  495. (insert (format "[%c] %-15s %s\n" i cat task))
  496. (cons i marker)))))
  497. (defvar org-clock-task-overrun nil
  498. "Internal flag indicating if the clock has overrun the planned time.")
  499. (defvar org-clock-update-period 60
  500. "Number of seconds between mode line clock string updates.")
  501. (defun org-clock-get-clock-string ()
  502. "Form a clock-string, that will be shown in the mode line.
  503. If an effort estimate was defined for the current item, use
  504. 01:30/01:50 format (clocked/estimated).
  505. If not, show simply the clocked time like 01:50."
  506. (let ((clocked-time (org-clock-get-clocked-time)))
  507. (if org-clock-effort
  508. (let* ((effort-in-minutes
  509. (org-duration-string-to-minutes org-clock-effort))
  510. (work-done-str
  511. (org-propertize
  512. (org-minutes-to-clocksum-string clocked-time)
  513. 'face (if (and org-clock-task-overrun (not org-clock-task-overrun-text))
  514. 'org-mode-line-clock-overrun 'org-mode-line-clock)))
  515. (effort-str (org-minutes-to-clocksum-string effort-in-minutes))
  516. (clockstr (org-propertize
  517. (concat " [%s/" effort-str
  518. "] (" (replace-regexp-in-string "%" "%%" org-clock-heading) ")")
  519. 'face 'org-mode-line-clock)))
  520. (format clockstr work-done-str))
  521. (org-propertize (concat "[" (org-minutes-to-clocksum-string clocked-time)
  522. (format " (%s)" org-clock-heading) "]")
  523. 'face 'org-mode-line-clock))))
  524. (defun org-clock-get-last-clock-out-time ()
  525. "Get the last clock-out time for the current subtree."
  526. (save-excursion
  527. (let ((end (save-excursion (org-end-of-subtree))))
  528. (when (re-search-forward (concat org-clock-string
  529. ".*\\]--\\(\\[[^]]+\\]\\)") end t)
  530. (org-time-string-to-time (match-string 1))))))
  531. (defun org-clock-update-mode-line ()
  532. (if org-clock-effort
  533. (org-clock-notify-once-if-expired)
  534. (setq org-clock-task-overrun nil))
  535. (setq org-mode-line-string
  536. (org-propertize
  537. (let ((clock-string (org-clock-get-clock-string))
  538. (help-text "Org-mode clock is running.\nmouse-1 shows a menu\nmouse-2 will jump to task"))
  539. (if (and (> org-clock-string-limit 0)
  540. (> (length clock-string) org-clock-string-limit))
  541. (org-propertize
  542. (substring clock-string 0 org-clock-string-limit)
  543. 'help-echo (concat help-text ": " org-clock-heading))
  544. (org-propertize clock-string 'help-echo help-text)))
  545. 'local-map org-clock-mode-line-map
  546. 'mouse-face (if (featurep 'xemacs) 'highlight 'mode-line-highlight)))
  547. (if (and org-clock-task-overrun org-clock-task-overrun-text)
  548. (setq org-mode-line-string
  549. (concat (org-propertize
  550. org-clock-task-overrun-text
  551. 'face 'org-mode-line-clock-overrun) org-mode-line-string)))
  552. (force-mode-line-update))
  553. (defun org-clock-get-clocked-time ()
  554. "Get the clocked time for the current item in minutes.
  555. The time returned includes the time spent on this task in
  556. previous clocking intervals."
  557. (let ((currently-clocked-time
  558. (floor (- (org-float-time)
  559. (org-float-time org-clock-start-time)) 60)))
  560. (+ currently-clocked-time (or org-clock-total-time 0))))
  561. (defun org-clock-modify-effort-estimate (&optional value)
  562. "Add to or set the effort estimate of the item currently being clocked.
  563. VALUE can be a number of minutes, or a string with format hh:mm or mm.
  564. When the string starts with a + or a - sign, the current value of the effort
  565. property will be changed by that amount.
  566. This will update the \"Effort\" property of currently clocked item, and
  567. the mode line."
  568. (interactive)
  569. (if (org-clock-is-active)
  570. (let ((current org-clock-effort) sign)
  571. (unless value
  572. ;; Prompt user for a value or a change
  573. (setq value
  574. (read-string
  575. (format "Set effort (hh:mm or mm%s): "
  576. (if current
  577. (format ", prefix + to add to %s" org-clock-effort)
  578. "")))))
  579. (when (stringp value)
  580. ;; A string. See if it is a delta
  581. (setq sign (string-to-char value))
  582. (if (member sign '(?- ?+))
  583. (setq current (org-duration-string-to-minutes current)
  584. value (substring value 1))
  585. (setq current 0))
  586. (setq value (org-duration-string-to-minutes value))
  587. (if (equal ?- sign)
  588. (setq value (- current value))
  589. (if (equal ?+ sign) (setq value (+ current value)))))
  590. (setq value (max 0 value)
  591. org-clock-effort (org-minutes-to-clocksum-string value))
  592. (org-entry-put org-clock-marker "Effort" org-clock-effort)
  593. (org-clock-update-mode-line)
  594. (message "Effort is now %s" org-clock-effort))
  595. (message "Clock is not currently active")))
  596. (defvar org-clock-notification-was-shown nil
  597. "Shows if we have shown notification already.")
  598. (defun org-clock-notify-once-if-expired ()
  599. "Show notification if we spent more time than we estimated before.
  600. Notification is shown only once."
  601. (when (org-clocking-p)
  602. (let ((effort-in-minutes (org-duration-string-to-minutes org-clock-effort))
  603. (clocked-time (org-clock-get-clocked-time)))
  604. (if (setq org-clock-task-overrun
  605. (if (or (null effort-in-minutes) (zerop effort-in-minutes))
  606. nil
  607. (>= clocked-time effort-in-minutes)))
  608. (unless org-clock-notification-was-shown
  609. (setq org-clock-notification-was-shown t)
  610. (org-notify
  611. (format "Task '%s' should be finished by now. (%s)"
  612. org-clock-heading org-clock-effort) t))
  613. (setq org-clock-notification-was-shown nil)))))
  614. (defun org-notify (notification &optional play-sound)
  615. "Send a NOTIFICATION and maybe PLAY-SOUND.
  616. If PLAY-SOUND is non-nil, it overrides `org-clock-sound'."
  617. (org-show-notification notification)
  618. (if play-sound (org-clock-play-sound play-sound)))
  619. (defun org-show-notification (notification)
  620. "Show notification.
  621. Use `org-show-notification-handler' if defined,
  622. use libnotify if available, or fall back on a message."
  623. (cond ((functionp org-show-notification-handler)
  624. (funcall org-show-notification-handler notification))
  625. ((stringp org-show-notification-handler)
  626. (start-process "emacs-timer-notification" nil
  627. org-show-notification-handler notification))
  628. ((fboundp 'notifications-notify)
  629. (notifications-notify
  630. :title "Org-mode message"
  631. :body notification
  632. ;; FIXME how to link to the Org icon?
  633. ;; :app-icon "~/.emacs.d/icons/mail.png"
  634. :urgency 'low))
  635. ((executable-find "notify-send")
  636. (start-process "emacs-timer-notification" nil
  637. "notify-send" notification))
  638. ;; Maybe the handler will send a message, so only use message as
  639. ;; a fall back option
  640. (t (message "%s" notification))))
  641. (defun org-clock-play-sound (&optional clock-sound)
  642. "Play sound as configured by `org-clock-sound'.
  643. Use alsa's aplay tool if available.
  644. If CLOCK-SOUND is non-nil, it overrides `org-clock-sound'."
  645. (let ((org-clock-sound (or clock-sound org-clock-sound)))
  646. (cond
  647. ((not org-clock-sound))
  648. ((eq org-clock-sound t) (beep t) (beep t))
  649. ((stringp org-clock-sound)
  650. (let ((file (expand-file-name org-clock-sound)))
  651. (if (file-exists-p file)
  652. (if (executable-find "aplay")
  653. (start-process "org-clock-play-notification" nil
  654. "aplay" file)
  655. (condition-case nil
  656. (play-sound-file file)
  657. (error (beep t) (beep t))))))))))
  658. (defvar org-clock-mode-line-entry nil
  659. "Information for the mode line about the running clock.")
  660. (defun org-find-open-clocks (file)
  661. "Search through the given file and find all open clocks."
  662. (let ((buf (or (get-file-buffer file)
  663. (find-file-noselect file)))
  664. clocks)
  665. (with-current-buffer buf
  666. (save-excursion
  667. (goto-char (point-min))
  668. (while (re-search-forward "CLOCK: \\(\\[.*?\\]\\)$" nil t)
  669. (push (cons (copy-marker (match-end 1) t)
  670. (org-time-string-to-time (match-string 1))) clocks))))
  671. clocks))
  672. (defsubst org-is-active-clock (clock)
  673. "Return t if CLOCK is the currently active clock."
  674. (and (org-clock-is-active)
  675. (= org-clock-marker (car clock))))
  676. (defmacro org-with-clock-position (clock &rest forms)
  677. "Evaluate FORMS with CLOCK as the current active clock."
  678. `(with-current-buffer (marker-buffer (car ,clock))
  679. (save-excursion
  680. (save-restriction
  681. (widen)
  682. (goto-char (car ,clock))
  683. (beginning-of-line)
  684. ,@forms))))
  685. (def-edebug-spec org-with-clock-position (form body))
  686. (put 'org-with-clock-position 'lisp-indent-function 1)
  687. (defmacro org-with-clock (clock &rest forms)
  688. "Evaluate FORMS with CLOCK as the current active clock.
  689. This macro also protects the current active clock from being altered."
  690. `(org-with-clock-position ,clock
  691. (let ((org-clock-start-time (cdr ,clock))
  692. (org-clock-total-time)
  693. (org-clock-history)
  694. (org-clock-effort)
  695. (org-clock-marker (car ,clock))
  696. (org-clock-hd-marker (save-excursion
  697. (outline-back-to-heading t)
  698. (point-marker))))
  699. ,@forms)))
  700. (def-edebug-spec org-with-clock (form body))
  701. (put 'org-with-clock 'lisp-indent-function 1)
  702. (defsubst org-clock-clock-in (clock &optional resume start-time)
  703. "Clock in to the clock located by CLOCK.
  704. If necessary, clock-out of the currently active clock."
  705. (org-with-clock-position clock
  706. (let ((org-clock-in-resume (or resume org-clock-in-resume)))
  707. (org-clock-in nil start-time))))
  708. (defsubst org-clock-clock-out (clock &optional fail-quietly at-time)
  709. "Clock out of the clock located by CLOCK."
  710. (let ((temp (copy-marker (car clock)
  711. (marker-insertion-type (car clock)))))
  712. (if (org-is-active-clock clock)
  713. (org-clock-out nil fail-quietly at-time)
  714. (org-with-clock clock
  715. (org-clock-out nil fail-quietly at-time)))
  716. (setcar clock temp)))
  717. (defsubst org-clock-clock-cancel (clock)
  718. "Cancel the clock located by CLOCK."
  719. (let ((temp (copy-marker (car clock)
  720. (marker-insertion-type (car clock)))))
  721. (if (org-is-active-clock clock)
  722. (org-clock-cancel)
  723. (org-with-clock clock
  724. (org-clock-cancel)))
  725. (setcar clock temp)))
  726. (defvar org-clock-clocking-in nil)
  727. (defvar org-clock-resolving-clocks nil)
  728. (defvar org-clock-resolving-clocks-due-to-idleness nil)
  729. (defun org-clock-resolve-clock (clock resolve-to clock-out-time
  730. &optional close-p restart-p fail-quietly)
  731. "Resolve `CLOCK' given the time `RESOLVE-TO', and the present.
  732. `CLOCK' is a cons cell of the form (MARKER START-TIME)."
  733. (let ((org-clock-resolving-clocks t))
  734. (cond
  735. ((null resolve-to)
  736. (org-clock-clock-cancel clock)
  737. (if (and restart-p (not org-clock-clocking-in))
  738. (org-clock-clock-in clock)))
  739. ((eq resolve-to 'now)
  740. (if restart-p
  741. (error "RESTART-P is not valid here"))
  742. (if (or close-p org-clock-clocking-in)
  743. (org-clock-clock-out clock fail-quietly)
  744. (unless (org-is-active-clock clock)
  745. (org-clock-clock-in clock t))))
  746. ((not (time-less-p resolve-to (current-time)))
  747. (error "RESOLVE-TO must refer to a time in the past"))
  748. (t
  749. (if restart-p
  750. (error "RESTART-P is not valid here"))
  751. (org-clock-clock-out clock fail-quietly (or clock-out-time
  752. resolve-to))
  753. (unless org-clock-clocking-in
  754. (if close-p
  755. (setq org-clock-leftover-time (and (null clock-out-time)
  756. resolve-to))
  757. (org-clock-clock-in clock nil (and clock-out-time
  758. resolve-to))))))))
  759. (defun org-clock-jump-to-current-clock (&optional effective-clock)
  760. (interactive)
  761. (let ((org-clock-into-drawer (org-clock-into-drawer))
  762. (clock (or effective-clock (cons org-clock-marker
  763. org-clock-start-time))))
  764. (unless (marker-buffer (car clock))
  765. (error "No clock is currently running"))
  766. (org-with-clock clock (org-clock-goto))
  767. (with-current-buffer (marker-buffer (car clock))
  768. (goto-char (car clock))
  769. (if org-clock-into-drawer
  770. (let ((logbook
  771. (if (stringp org-clock-into-drawer)
  772. (concat ":" org-clock-into-drawer ":")
  773. ":LOGBOOK:")))
  774. (ignore-errors
  775. (outline-flag-region
  776. (save-excursion
  777. (outline-back-to-heading t)
  778. (search-forward logbook)
  779. (goto-char (match-beginning 0)))
  780. (save-excursion
  781. (outline-back-to-heading t)
  782. (search-forward logbook)
  783. (search-forward ":END:")
  784. (goto-char (match-end 0)))
  785. nil)))))))
  786. (defun org-clock-resolve (clock &optional prompt-fn last-valid fail-quietly)
  787. "Resolve an open org-mode clock.
  788. An open clock was found, with `dangling' possibly being non-nil.
  789. If this function was invoked with a prefix argument, non-dangling
  790. open clocks are ignored. The given clock requires some sort of
  791. user intervention to resolve it, either because a clock was left
  792. dangling or due to an idle timeout. The clock resolution can
  793. either be:
  794. (a) deleted, the user doesn't care about the clock
  795. (b) restarted from the current time (if no other clock is open)
  796. (c) closed, giving the clock X minutes
  797. (d) closed and then restarted
  798. (e) resumed, as if the user had never left
  799. The format of clock is (CONS MARKER START-TIME), where MARKER
  800. identifies the buffer and position the clock is open at (and
  801. thus, the heading it's under), and START-TIME is when the clock
  802. was started."
  803. (assert clock)
  804. (let* ((ch
  805. (save-window-excursion
  806. (save-excursion
  807. (unless org-clock-resolving-clocks-due-to-idleness
  808. (org-clock-jump-to-current-clock clock))
  809. (unless org-clock-resolve-expert
  810. (with-output-to-temp-buffer "*Org Clock*"
  811. (princ "Select a Clock Resolution Command:
  812. i/q/C-g Ignore this question; the same as keeping all the idle time.
  813. k/K Keep X minutes of the idle time (default is all). If this
  814. amount is less than the default, you will be clocked out
  815. that many minutes after the time that idling began, and then
  816. clocked back in at the present time.
  817. g/G Indicate that you \"got back\" X minutes ago. This is quite
  818. different from 'k': it clocks you out from the beginning of
  819. the idle period and clock you back in X minutes ago.
  820. s/S Subtract the idle time from the current clock. This is the
  821. same as keeping 0 minutes.
  822. C Cancel the open timer altogether. It will be as though you
  823. never clocked in.
  824. j/J Jump to the current clock, to make manual adjustments.
  825. For all these options, using uppercase makes your final state
  826. to be CLOCKED OUT.")))
  827. (org-fit-window-to-buffer (get-buffer-window "*Org Clock*"))
  828. (let (char-pressed)
  829. (when (featurep 'xemacs)
  830. (message (concat (funcall prompt-fn clock)
  831. " [jkKgGsScCiq]? "))
  832. (setq char-pressed (read-char-exclusive)))
  833. (while (or (null char-pressed)
  834. (and (not (memq char-pressed
  835. '(?k ?K ?g ?G ?s ?S ?C
  836. ?j ?J ?i ?q)))
  837. (or (ding) t)))
  838. (setq char-pressed
  839. (read-char (concat (funcall prompt-fn clock)
  840. " [jkKgGSscCiq]? ")
  841. nil 45)))
  842. (and (not (memq char-pressed '(?i ?q))) char-pressed)))))
  843. (default
  844. (floor (/ (org-float-time
  845. (time-subtract (current-time) last-valid)) 60)))
  846. (keep
  847. (and (memq ch '(?k ?K))
  848. (read-number "Keep how many minutes? " default)))
  849. (gotback
  850. (and (memq ch '(?g ?G))
  851. (read-number "Got back how many minutes ago? " default)))
  852. (subtractp (memq ch '(?s ?S)))
  853. (barely-started-p (< (- (org-float-time last-valid)
  854. (org-float-time (cdr clock))) 45))
  855. (start-over (and subtractp barely-started-p)))
  856. (cond
  857. ((memq ch '(?j ?J))
  858. (if (eq ch ?J)
  859. (org-clock-resolve-clock clock 'now nil t nil fail-quietly))
  860. (org-clock-jump-to-current-clock clock))
  861. ((or (null ch)
  862. (not (memq ch '(?k ?K ?g ?G ?s ?S ?C))))
  863. (message ""))
  864. (t
  865. (org-clock-resolve-clock
  866. clock (cond
  867. ((or (eq ch ?C)
  868. ;; If the time on the clock was less than a minute before
  869. ;; the user went away, and they've ask to subtract all the
  870. ;; time...
  871. start-over)
  872. nil)
  873. ((or subtractp
  874. (and gotback (= gotback 0)))
  875. last-valid)
  876. ((or (and keep (= keep default))
  877. (and gotback (= gotback default)))
  878. 'now)
  879. (keep
  880. (time-add last-valid (seconds-to-time (* 60 keep))))
  881. (gotback
  882. (time-subtract (current-time)
  883. (seconds-to-time (* 60 gotback))))
  884. (t
  885. (error "Unexpected, please report this as a bug")))
  886. (and gotback last-valid)
  887. (memq ch '(?K ?G ?S))
  888. (and start-over
  889. (not (memq ch '(?K ?G ?S ?C))))
  890. fail-quietly)))))
  891. ;;;###autoload
  892. (defun org-resolve-clocks (&optional only-dangling-p prompt-fn last-valid)
  893. "Resolve all currently open org-mode clocks.
  894. If `only-dangling-p' is non-nil, only ask to resolve dangling
  895. \(i.e., not currently open and valid) clocks."
  896. (interactive "P")
  897. (unless org-clock-resolving-clocks
  898. (let ((org-clock-resolving-clocks t))
  899. (dolist (file (org-files-list))
  900. (let ((clocks (org-find-open-clocks file)))
  901. (dolist (clock clocks)
  902. (let ((dangling (or (not (org-clock-is-active))
  903. (/= (car clock) org-clock-marker))))
  904. (if (or (not only-dangling-p) dangling)
  905. (org-clock-resolve
  906. clock
  907. (or prompt-fn
  908. (function
  909. (lambda (clock)
  910. (format
  911. "Dangling clock started %d mins ago"
  912. (floor
  913. (/ (- (org-float-time (current-time))
  914. (org-float-time (cdr clock))) 60))))))
  915. (or last-valid
  916. (cdr clock)))))))))))
  917. (defun org-emacs-idle-seconds ()
  918. "Return the current Emacs idle time in seconds, or nil if not idle."
  919. (let ((idle-time (current-idle-time)))
  920. (if idle-time
  921. (org-float-time idle-time)
  922. 0)))
  923. (defun org-mac-idle-seconds ()
  924. "Return the current Mac idle time in seconds."
  925. (string-to-number (shell-command-to-string "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle; last}'")))
  926. (defvar org-x11idle-exists-p
  927. ;; Check that x11idle exists
  928. (and (eq window-system 'x)
  929. (eq (call-process-shell-command "command" nil nil nil "-v" "x11idle") 0)
  930. ;; Check that x11idle can retrieve the idle time
  931. (eq (call-process-shell-command "x11idle" nil nil nil) 0)))
  932. (defun org-x11-idle-seconds ()
  933. "Return the current X11 idle time in seconds."
  934. (/ (string-to-number (shell-command-to-string "x11idle")) 1000))
  935. (defun org-user-idle-seconds ()
  936. "Return the number of seconds the user has been idle for.
  937. This routine returns a floating point number."
  938. (cond
  939. ((eq system-type 'darwin)
  940. (org-mac-idle-seconds))
  941. ((and (eq window-system 'x) org-x11idle-exists-p)
  942. (org-x11-idle-seconds))
  943. (t
  944. (org-emacs-idle-seconds))))
  945. (defvar org-clock-user-idle-seconds)
  946. (defun org-resolve-clocks-if-idle ()
  947. "Resolve all currently open org-mode clocks.
  948. This is performed after `org-clock-idle-time' minutes, to check
  949. if the user really wants to stay clocked in after being idle for
  950. so long."
  951. (when (and org-clock-idle-time (not org-clock-resolving-clocks)
  952. org-clock-marker)
  953. (let* ((org-clock-user-idle-seconds (org-user-idle-seconds))
  954. (org-clock-user-idle-start
  955. (time-subtract (current-time)
  956. (seconds-to-time org-clock-user-idle-seconds)))
  957. (org-clock-resolving-clocks-due-to-idleness t))
  958. (if (> org-clock-user-idle-seconds (* 60 org-clock-idle-time))
  959. (org-clock-resolve
  960. (cons org-clock-marker
  961. org-clock-start-time)
  962. (function
  963. (lambda (clock)
  964. (format "Clocked in & idle for %.1f mins"
  965. (/ (org-float-time
  966. (time-subtract (current-time)
  967. org-clock-user-idle-start))
  968. 60.0))))
  969. org-clock-user-idle-start)))))
  970. (defvar org-clock-current-task nil
  971. "Task currently clocked in.")
  972. (defun org-clock-set-current ()
  973. "Set `org-clock-current-task' to the task currently clocked in."
  974. (setq org-clock-current-task (nth 4 (org-heading-components))))
  975. (defun org-clock-delete-current ()
  976. "Reset `org-clock-current-task' to nil."
  977. (setq org-clock-current-task nil))
  978. (defvar org-clock-out-time nil) ; store the time of the last clock-out
  979. ;;;###autoload
  980. (defun org-clock-in (&optional select start-time)
  981. "Start the clock on the current item.
  982. If necessary, clock-out of the currently active clock.
  983. With a prefix argument SELECT (\\[universal-argument]), offer a list of recently clocked
  984. tasks to clock into. When SELECT is \\[universal-argument] \\[universal-argument], clock into the current task
  985. and mark it as the default task, a special task that will always be offered
  986. in the clocking selection, associated with the letter `d'.
  987. When SELECT is \\[universal-argument] \\[universal-argument] \\[universal-argument], \
  988. clock in by using the last clock-out
  989. time as the start time \(see `org-clock-continuously' to
  990. make this the default behavior.)"
  991. (interactive "P")
  992. (setq org-clock-notification-was-shown nil)
  993. (org-refresh-properties org-effort-property 'org-effort)
  994. (catch 'abort
  995. (let ((interrupting (and (not org-clock-resolving-clocks-due-to-idleness)
  996. (org-clocking-p)))
  997. ts selected-task target-pos (msg-extra "")
  998. (leftover (and (not org-clock-resolving-clocks)
  999. org-clock-leftover-time)))
  1000. (when (and org-clock-auto-clock-resolution
  1001. (or (not interrupting)
  1002. (eq t org-clock-auto-clock-resolution))
  1003. (not org-clock-clocking-in)
  1004. (not org-clock-resolving-clocks))
  1005. (setq org-clock-leftover-time nil)
  1006. (let ((org-clock-clocking-in t))
  1007. (org-resolve-clocks))) ; check if any clocks are dangling
  1008. (when (equal select '(64))
  1009. ;; Set start-time to `org-clock-out-time'
  1010. (let ((org-clock-continuously t))
  1011. (org-clock-in nil org-clock-out-time)))
  1012. (when (equal select '(4))
  1013. (setq selected-task (org-clock-select-task "Clock-in on task: "))
  1014. (if selected-task
  1015. (setq selected-task (copy-marker selected-task))
  1016. (error "Abort")))
  1017. (when (equal select '(16))
  1018. ;; Mark as default clocking task
  1019. (org-clock-mark-default-task))
  1020. (when interrupting
  1021. ;; We are interrupting the clocking of a different task.
  1022. ;; Save a marker to this task, so that we can go back.
  1023. ;; First check if we are trying to clock into the same task!
  1024. (when (save-excursion
  1025. (unless selected-task
  1026. (org-back-to-heading t))
  1027. (and (equal (marker-buffer org-clock-hd-marker)
  1028. (if selected-task
  1029. (marker-buffer selected-task)
  1030. (current-buffer)))
  1031. (= (marker-position org-clock-hd-marker)
  1032. (if selected-task
  1033. (marker-position selected-task)
  1034. (point)))
  1035. (equal org-clock-current-task (nth 4 (org-heading-components)))))
  1036. (message "Clock continues in \"%s\"" org-clock-heading)
  1037. (throw 'abort nil))
  1038. (move-marker org-clock-interrupted-task
  1039. (marker-position org-clock-marker)
  1040. (marker-buffer org-clock-marker))
  1041. (let ((org-clock-clocking-in t))
  1042. (org-clock-out nil t)))
  1043. ;; Clock in at which position?
  1044. (setq target-pos
  1045. (if (and (eobp) (not (org-at-heading-p)))
  1046. (point-at-bol 0)
  1047. (point)))
  1048. (save-excursion
  1049. (when (and selected-task (marker-buffer selected-task))
  1050. ;; There is a selected task, move to the correct buffer
  1051. ;; and set the new target position.
  1052. (set-buffer (org-base-buffer (marker-buffer selected-task)))
  1053. (setq target-pos (marker-position selected-task))
  1054. (move-marker selected-task nil))
  1055. (save-excursion
  1056. (save-restriction
  1057. (widen)
  1058. (goto-char target-pos)
  1059. (org-back-to-heading t)
  1060. (or interrupting (move-marker org-clock-interrupted-task nil))
  1061. (save-excursion
  1062. (forward-char) ;; make sure the marker is not at the
  1063. ;; beginning of the heading, since the
  1064. ;; user is liking to insert stuff here
  1065. ;; manually
  1066. (run-hooks 'org-clock-in-prepare-hook)
  1067. (org-clock-history-push))
  1068. (org-clock-set-current)
  1069. (cond ((functionp org-clock-in-switch-to-state)
  1070. (looking-at org-complex-heading-regexp)
  1071. (let ((newstate (funcall org-clock-in-switch-to-state
  1072. (match-string 2))))
  1073. (if newstate (org-todo newstate))))
  1074. ((and org-clock-in-switch-to-state
  1075. (not (looking-at (concat org-outline-regexp "[ \t]*"
  1076. org-clock-in-switch-to-state
  1077. "\\>"))))
  1078. (org-todo org-clock-in-switch-to-state)))
  1079. (setq org-clock-heading-for-remember
  1080. (and (looking-at org-complex-heading-regexp)
  1081. (match-end 4)
  1082. (org-trim (buffer-substring (match-end 1)
  1083. (match-end 4)))))
  1084. (setq org-clock-heading
  1085. (cond ((and org-clock-heading-function
  1086. (functionp org-clock-heading-function))
  1087. (funcall org-clock-heading-function))
  1088. ((and (looking-at org-complex-heading-regexp)
  1089. (match-string 4))
  1090. (replace-regexp-in-string
  1091. "\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]" "\\1"
  1092. (match-string 4)))
  1093. (t "???")))
  1094. (setq org-clock-heading (org-propertize org-clock-heading
  1095. 'face nil))
  1096. (org-clock-find-position org-clock-in-resume)
  1097. (cond
  1098. ((and org-clock-in-resume
  1099. (looking-at
  1100. (concat "^[ \t]*" org-clock-string
  1101. " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
  1102. " *\\sw+\.? +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
  1103. (message "Matched %s" (match-string 1))
  1104. (setq ts (concat "[" (match-string 1) "]"))
  1105. (goto-char (match-end 1))
  1106. (setq org-clock-start-time
  1107. (apply 'encode-time
  1108. (org-parse-time-string (match-string 1))))
  1109. (setq org-clock-effort (get-text-property (point) 'org-effort))
  1110. (setq org-clock-total-time (org-clock-sum-current-item
  1111. (org-clock-get-sum-start))))
  1112. ((eq org-clock-in-resume 'auto-restart)
  1113. ;; called from org-clock-load during startup,
  1114. ;; do not interrupt, but warn!
  1115. (message "Cannot restart clock because task does not contain unfinished clock")
  1116. (ding)
  1117. (sit-for 2)
  1118. (throw 'abort nil))
  1119. (t
  1120. (insert-before-markers "\n")
  1121. (backward-char 1)
  1122. (org-indent-line)
  1123. (when (and (save-excursion
  1124. (end-of-line 0)
  1125. (org-in-item-p)))
  1126. (beginning-of-line 1)
  1127. (org-indent-line-to (- (org-get-indentation) 2)))
  1128. (insert org-clock-string " ")
  1129. (setq org-clock-effort (get-text-property (point) 'org-effort))
  1130. (setq org-clock-total-time (org-clock-sum-current-item
  1131. (org-clock-get-sum-start)))
  1132. (setq org-clock-start-time
  1133. (or (and org-clock-continuously org-clock-out-time)
  1134. (and leftover
  1135. (y-or-n-p
  1136. (format
  1137. "You stopped another clock %d mins ago; start this one from then? "
  1138. (/ (- (org-float-time
  1139. (org-current-time org-clock-rounding-minutes))
  1140. (org-float-time leftover)) 60)))
  1141. leftover)
  1142. start-time
  1143. (org-current-time org-clock-rounding-minutes)))
  1144. (setq ts (org-insert-time-stamp org-clock-start-time
  1145. 'with-hm 'inactive))))
  1146. (move-marker org-clock-marker (point) (buffer-base-buffer))
  1147. (move-marker org-clock-hd-marker
  1148. (save-excursion (org-back-to-heading t) (point))
  1149. (buffer-base-buffer))
  1150. (setq org-clock-has-been-used t)
  1151. ;; add to mode line
  1152. (when (or (eq org-clock-clocked-in-display 'mode-line)
  1153. (eq org-clock-clocked-in-display 'both))
  1154. (or global-mode-string (setq global-mode-string '("")))
  1155. (or (memq 'org-mode-line-string global-mode-string)
  1156. (setq global-mode-string
  1157. (append global-mode-string '(org-mode-line-string)))))
  1158. ;; add to frame title
  1159. (when (or (eq org-clock-clocked-in-display 'frame-title)
  1160. (eq org-clock-clocked-in-display 'both))
  1161. (setq frame-title-format org-clock-frame-title-format))
  1162. (org-clock-update-mode-line)
  1163. (when org-clock-mode-line-timer
  1164. (cancel-timer org-clock-mode-line-timer)
  1165. (setq org-clock-mode-line-timer nil))
  1166. (when org-clock-clocked-in-display
  1167. (setq org-clock-mode-line-timer
  1168. (run-with-timer org-clock-update-period
  1169. org-clock-update-period
  1170. 'org-clock-update-mode-line)))
  1171. (when org-clock-idle-timer
  1172. (cancel-timer org-clock-idle-timer)
  1173. (setq org-clock-idle-timer nil))
  1174. (setq org-clock-idle-timer
  1175. (run-with-timer 60 60 'org-resolve-clocks-if-idle))
  1176. (message "Clock starts at %s - %s" ts msg-extra)
  1177. (run-hooks 'org-clock-in-hook)))))))
  1178. ;;;###autoload
  1179. (defun org-clock-in-last (&optional arg)
  1180. "Clock in the last closed clocked item.
  1181. When already clocking in, send an warning.
  1182. With a universal prefix argument, select the task you want to
  1183. clock in from the last clocked in tasks.
  1184. With two universal prefix arguments, start clocking using the
  1185. last clock-out time, if any.
  1186. With three universal prefix arguments, interactively prompt
  1187. for a todo state to switch to, overriding the existing value
  1188. `org-clock-in-switch-to-state'."
  1189. (interactive "P")
  1190. (if (equal arg '(4))
  1191. (org-clock-in (org-clock-select-task))
  1192. (let ((start-time (if (or org-clock-continuously (equal arg '(16)))
  1193. (or org-clock-out-time
  1194. (org-current-time org-clock-rounding-minutes))
  1195. (org-current-time org-clock-rounding-minutes))))
  1196. (if (null org-clock-history)
  1197. (message "No last clock")
  1198. (let ((org-clock-in-switch-to-state
  1199. (if (and (not org-clock-current-task) (equal arg '(64)))
  1200. (completing-read "Switch to state: "
  1201. (and org-clock-history
  1202. (with-current-buffer
  1203. (marker-buffer (car org-clock-history))
  1204. org-todo-keywords-1)))
  1205. org-clock-in-switch-to-state))
  1206. (already-clocking org-clock-current-task))
  1207. (org-clock-clock-in (list (car org-clock-history)) nil start-time)
  1208. (or already-clocking
  1209. ;; Don't display a message if we are already clocking in
  1210. (message "Clocking back: %s (in %s)"
  1211. org-clock-current-task
  1212. (buffer-name (marker-buffer org-clock-marker)))))))))
  1213. (defun org-clock-mark-default-task ()
  1214. "Mark current task as default task."
  1215. (interactive)
  1216. (save-excursion
  1217. (org-back-to-heading t)
  1218. (move-marker org-clock-default-task (point))))
  1219. (defvar msg-extra)
  1220. (defun org-clock-get-sum-start ()
  1221. "Return the time from which clock times should be counted.
  1222. This is for the currently running clock as it is displayed
  1223. in the mode line. This function looks at the properties
  1224. LAST_REPEAT and in particular CLOCK_MODELINE_TOTAL and the
  1225. corresponding variable `org-clock-mode-line-total' and then
  1226. decides which time to use."
  1227. (let ((cmt (or (org-entry-get nil "CLOCK_MODELINE_TOTAL")
  1228. (symbol-name org-clock-mode-line-total)))
  1229. (lr (org-entry-get nil "LAST_REPEAT")))
  1230. (cond
  1231. ((equal cmt "current")
  1232. (setq msg-extra "showing time in current clock instance")
  1233. (current-time))
  1234. ((equal cmt "today")
  1235. (setq msg-extra "showing today's task time.")
  1236. (let* ((dt (decode-time (current-time))))
  1237. (setq dt (append (list 0 0 0) (nthcdr 3 dt)))
  1238. (if org-extend-today-until
  1239. (setf (nth 2 dt) org-extend-today-until))
  1240. (apply 'encode-time dt)))
  1241. ((or (equal cmt "all")
  1242. (and (or (not cmt) (equal cmt "auto"))
  1243. (not lr)))
  1244. (setq msg-extra "showing entire task time.")
  1245. nil)
  1246. ((or (equal cmt "repeat")
  1247. (and (or (not cmt) (equal cmt "auto"))
  1248. lr))
  1249. (setq msg-extra "showing task time since last repeat.")
  1250. (if (not lr)
  1251. nil
  1252. (org-time-string-to-time lr)))
  1253. (t nil))))
  1254. (defun org-clock-find-position (find-unclosed)
  1255. "Find the location where the next clock line should be inserted.
  1256. When FIND-UNCLOSED is non-nil, first check if there is an unclosed clock
  1257. line and position cursor in that line."
  1258. (org-back-to-heading t)
  1259. (catch 'exit
  1260. (let* ((org-clock-into-drawer (org-clock-into-drawer))
  1261. (beg (save-excursion
  1262. (beginning-of-line 2)
  1263. (or (bolp) (newline))
  1264. (point)))
  1265. (end (progn (outline-next-heading) (point)))
  1266. (re (concat "^[ \t]*" org-clock-string))
  1267. (cnt 0)
  1268. (drawer (if (stringp org-clock-into-drawer)
  1269. org-clock-into-drawer "LOGBOOK"))
  1270. first last ind-last)
  1271. (goto-char beg)
  1272. (when (and find-unclosed
  1273. (re-search-forward
  1274. (concat "^[ \t]*" org-clock-string
  1275. " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
  1276. " *\\sw+ +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")
  1277. end t))
  1278. (beginning-of-line 1)
  1279. (throw 'exit t))
  1280. (when (eobp) (newline) (setq end (max (point) end)))
  1281. (when (re-search-forward (concat "^[ \t]*:" drawer ":") end t)
  1282. ;; we seem to have a CLOCK drawer, so go there.
  1283. (beginning-of-line 2)
  1284. (or org-log-states-order-reversed
  1285. (and (re-search-forward org-property-end-re nil t)
  1286. (goto-char (match-beginning 0))))
  1287. (throw 'exit t))
  1288. ;; Lets count the CLOCK lines
  1289. (goto-char beg)
  1290. (while (re-search-forward re end t)
  1291. (setq first (or first (match-beginning 0))
  1292. last (match-beginning 0)
  1293. cnt (1+ cnt)))
  1294. (when (and (integerp org-clock-into-drawer)
  1295. last
  1296. (>= (1+ cnt) org-clock-into-drawer))
  1297. ;; Wrap current entries into a new drawer
  1298. (goto-char last)
  1299. (setq ind-last (org-get-indentation))
  1300. (beginning-of-line 2)
  1301. (if (and (>= (org-get-indentation) ind-last)
  1302. (org-at-item-p))
  1303. (when (and (>= (org-get-indentation) ind-last)
  1304. (org-at-item-p))
  1305. (let ((struct (org-list-struct)))
  1306. (goto-char (org-list-get-bottom-point struct)))))
  1307. (insert ":END:\n")
  1308. (beginning-of-line 0)
  1309. (org-indent-line-to ind-last)
  1310. (goto-char first)
  1311. (insert ":" drawer ":\n")
  1312. (beginning-of-line 0)
  1313. (org-indent-line)
  1314. (org-flag-drawer t)
  1315. (beginning-of-line 2)
  1316. (or org-log-states-order-reversed
  1317. (and (re-search-forward org-property-end-re nil t)
  1318. (goto-char (match-beginning 0))))
  1319. (throw 'exit nil))
  1320. (goto-char beg)
  1321. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  1322. (not (equal (match-string 1) org-clock-string)))
  1323. ;; Planning info, skip to after it
  1324. (beginning-of-line 2)
  1325. (or (bolp) (newline)))
  1326. (when (or (eq org-clock-into-drawer t)
  1327. (stringp org-clock-into-drawer)
  1328. (and (integerp org-clock-into-drawer)
  1329. (< org-clock-into-drawer 2)))
  1330. (insert ":" drawer ":\n:END:\n")
  1331. (beginning-of-line -1)
  1332. (org-indent-line)
  1333. (org-flag-drawer t)
  1334. (beginning-of-line 2)
  1335. (org-indent-line)
  1336. (beginning-of-line)
  1337. (or org-log-states-order-reversed
  1338. (and (re-search-forward org-property-end-re nil t)
  1339. (goto-char (match-beginning 0))))))))
  1340. ;;;###autoload
  1341. (defun org-clock-out (&optional switch-to-state fail-quietly at-time)
  1342. "Stop the currently running clock.
  1343. Throw an error if there is no running clock and FAIL-QUIETLY is nil.
  1344. With a universal prefix, prompt for a state to switch the clocked out task
  1345. to, overriding the existing value of `org-clock-out-switch-to-state'."
  1346. (interactive "P")
  1347. (catch 'exit
  1348. (when (not (org-clocking-p))
  1349. (setq global-mode-string
  1350. (delq 'org-mode-line-string global-mode-string))
  1351. (setq frame-title-format org-frame-title-format-backup)
  1352. (force-mode-line-update)
  1353. (if fail-quietly (throw 'exit t) (user-error "No active clock")))
  1354. (let ((org-clock-out-switch-to-state
  1355. (if switch-to-state
  1356. (completing-read "Switch to state: "
  1357. (with-current-buffer
  1358. (marker-buffer org-clock-marker)
  1359. org-todo-keywords-1)
  1360. nil t "DONE")
  1361. org-clock-out-switch-to-state))
  1362. (now (org-current-time org-clock-rounding-minutes))
  1363. ts te s h m remove)
  1364. (setq org-clock-out-time now)
  1365. (save-excursion ; Do not replace this with `with-current-buffer'.
  1366. (org-no-warnings (set-buffer (org-clocking-buffer)))
  1367. (save-restriction
  1368. (widen)
  1369. (goto-char org-clock-marker)
  1370. (beginning-of-line 1)
  1371. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  1372. (equal (match-string 1) org-clock-string))
  1373. (setq ts (match-string 2))
  1374. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  1375. (goto-char (match-end 0))
  1376. (delete-region (point) (point-at-eol))
  1377. (insert "--")
  1378. (setq te (org-insert-time-stamp (or at-time now) 'with-hm 'inactive))
  1379. (setq s (- (org-float-time (apply 'encode-time (org-parse-time-string te)))
  1380. (org-float-time (apply 'encode-time (org-parse-time-string ts))))
  1381. h (floor (/ s 3600))
  1382. s (- s (* 3600 h))
  1383. m (floor (/ s 60))
  1384. s (- s (* 60 s)))
  1385. (insert " => " (format "%2d:%02d" h m))
  1386. (when (setq remove (and org-clock-out-remove-zero-time-clocks
  1387. (= (+ h m) 0)))
  1388. (beginning-of-line 1)
  1389. (delete-region (point) (point-at-eol))
  1390. (and (looking-at "\n") (> (point-max) (1+ (point)))
  1391. (delete-char 1)))
  1392. (move-marker org-clock-marker nil)
  1393. (move-marker org-clock-hd-marker nil)
  1394. (when org-log-note-clock-out
  1395. (org-add-log-setup 'clock-out nil nil nil nil
  1396. (concat "# Task: " (org-get-heading t) "\n\n")))
  1397. (when org-clock-mode-line-timer
  1398. (cancel-timer org-clock-mode-line-timer)
  1399. (setq org-clock-mode-line-timer nil))
  1400. (when org-clock-idle-timer
  1401. (cancel-timer org-clock-idle-timer)
  1402. (setq org-clock-idle-timer nil))
  1403. (setq global-mode-string
  1404. (delq 'org-mode-line-string global-mode-string))
  1405. (setq frame-title-format org-frame-title-format-backup)
  1406. (when org-clock-out-switch-to-state
  1407. (save-excursion
  1408. (org-back-to-heading t)
  1409. (let ((org-inhibit-logging t)
  1410. (org-clock-out-when-done nil))
  1411. (cond
  1412. ((functionp org-clock-out-switch-to-state)
  1413. (looking-at org-complex-heading-regexp)
  1414. (let ((newstate (funcall org-clock-out-switch-to-state
  1415. (match-string 2))))
  1416. (if newstate (org-todo newstate))))
  1417. ((and org-clock-out-switch-to-state
  1418. (not (looking-at (concat org-outline-regexp "[ \t]*"
  1419. org-clock-out-switch-to-state
  1420. "\\>"))))
  1421. (org-todo org-clock-out-switch-to-state))))))
  1422. (force-mode-line-update)
  1423. (message (concat "Clock stopped at %s after "
  1424. (org-minutes-to-clocksum-string (+ (* 60 h) m)) "%s")
  1425. te (if remove " => LINE REMOVED" ""))
  1426. (run-hooks 'org-clock-out-hook)
  1427. (unless (org-clocking-p)
  1428. (org-clock-delete-current)))))))
  1429. (add-hook 'org-clock-out-hook 'org-clock-remove-empty-clock-drawer)
  1430. (defun org-clock-remove-empty-clock-drawer nil
  1431. "Remove empty clock drawer in the current subtree."
  1432. (let* ((olid (or (org-entry-get (point) "LOG_INTO_DRAWER")
  1433. org-log-into-drawer))
  1434. (clock-drawer (if (eq t olid) "LOGBOOK" olid))
  1435. (end (save-excursion (org-end-of-subtree t t))))
  1436. (when clock-drawer
  1437. (save-excursion
  1438. (org-back-to-heading t)
  1439. (while (and (< (point) end)
  1440. (search-forward clock-drawer end t))
  1441. (goto-char (match-beginning 0))
  1442. (org-remove-empty-drawer-at clock-drawer (point))
  1443. (forward-line 1))))))
  1444. (defun org-at-clock-log-p nil
  1445. "Is the cursor on the clock log line?"
  1446. (save-excursion
  1447. (move-beginning-of-line 1)
  1448. (looking-at "^[ \t]*CLOCK:")))
  1449. (defun org-clock-timestamps-up (&optional n)
  1450. "Increase CLOCK timestamps at cursor.
  1451. Optional argument N tells to change by that many units."
  1452. (interactive "P")
  1453. (org-clock-timestamps-change 'up n))
  1454. (defun org-clock-timestamps-down (&optional n)
  1455. "Increase CLOCK timestamps at cursor.
  1456. Optional argument N tells to change by that many units."
  1457. (interactive "P")
  1458. (org-clock-timestamps-change 'down n))
  1459. (defun org-clock-timestamps-change (updown &optional n)
  1460. "Change CLOCK timestamps synchronously at cursor.
  1461. UPDOWN tells whether to change 'up or 'down.
  1462. Optional argument N tells to change by that many units."
  1463. (setq org-ts-what nil)
  1464. (when (org-at-timestamp-p t)
  1465. (let ((tschange (if (eq updown 'up) 'org-timestamp-up
  1466. 'org-timestamp-down))
  1467. ts1 begts1 ts2 begts2 updatets1 tdiff)
  1468. (save-excursion
  1469. (move-beginning-of-line 1)
  1470. (re-search-forward org-ts-regexp3 nil t)
  1471. (setq ts1 (match-string 0) begts1 (match-beginning 0))
  1472. (when (re-search-forward org-ts-regexp3 nil t)
  1473. (setq ts2 (match-string 0) begts2 (match-beginning 0))))
  1474. ;; Are we on the second timestamp?
  1475. (if (<= begts2 (point)) (setq updatets1 t))
  1476. (if (not ts2)
  1477. ;; fall back on org-timestamp-up if there is only one
  1478. (funcall tschange n)
  1479. ;; setq this so that (boundp 'org-ts-what is non-nil)
  1480. (funcall tschange n)
  1481. (let ((ts (if updatets1 ts2 ts1))
  1482. (begts (if updatets1 begts1 begts2)))
  1483. (setq tdiff
  1484. (subtract-time
  1485. (org-time-string-to-time org-last-changed-timestamp)
  1486. (org-time-string-to-time ts)))
  1487. (save-excursion
  1488. (goto-char begts)
  1489. (org-timestamp-change
  1490. (round (/ (org-float-time tdiff)
  1491. (cond ((eq org-ts-what 'minute) 60)
  1492. ((eq org-ts-what 'hour) 3600)
  1493. ((eq org-ts-what 'day) (* 24 3600))
  1494. ((eq org-ts-what 'month) (* 24 3600 31))
  1495. ((eq org-ts-what 'year) (* 24 3600 365.2)))))
  1496. org-ts-what 'updown)))))))
  1497. ;;;###autoload
  1498. (defun org-clock-cancel ()
  1499. "Cancel the running clock by removing the start timestamp."
  1500. (interactive)
  1501. (when (not (org-clocking-p))
  1502. (setq global-mode-string
  1503. (delq 'org-mode-line-string global-mode-string))
  1504. (setq frame-title-format org-frame-title-format-backup)
  1505. (force-mode-line-update)
  1506. (error "No active clock"))
  1507. (save-excursion ; Do not replace this with `with-current-buffer'.
  1508. (org-no-warnings (set-buffer (org-clocking-buffer)))
  1509. (goto-char org-clock-marker)
  1510. (if (org-looking-back (concat "^[ \t]*" org-clock-string ".*"))
  1511. (progn (delete-region (1- (point-at-bol)) (point-at-eol))
  1512. (org-remove-empty-drawer-at "LOGBOOK" (point)))
  1513. (message "Clock gone, cancel the timer anyway")
  1514. (sit-for 2)))
  1515. (move-marker org-clock-marker nil)
  1516. (move-marker org-clock-hd-marker nil)
  1517. (setq global-mode-string
  1518. (delq 'org-mode-line-string global-mode-string))
  1519. (setq frame-title-format org-frame-title-format-backup)
  1520. (force-mode-line-update)
  1521. (message "Clock canceled")
  1522. (run-hooks 'org-clock-cancel-hook))
  1523. ;;;###autoload
  1524. (defun org-clock-goto (&optional select)
  1525. "Go to the currently clocked-in entry, or to the most recently clocked one.
  1526. With prefix arg SELECT, offer recently clocked tasks for selection."
  1527. (interactive "@P")
  1528. (let* ((recent nil)
  1529. (m (cond
  1530. (select
  1531. (or (org-clock-select-task "Select task to go to: ")
  1532. (error "No task selected")))
  1533. ((org-clocking-p) org-clock-marker)
  1534. ((and org-clock-goto-may-find-recent-task
  1535. (car org-clock-history)
  1536. (marker-buffer (car org-clock-history)))
  1537. (setq recent t)
  1538. (car org-clock-history))
  1539. (t (error "No active or recent clock task")))))
  1540. (org-pop-to-buffer-same-window (marker-buffer m))
  1541. (if (or (< m (point-min)) (> m (point-max))) (widen))
  1542. (goto-char m)
  1543. (org-show-entry)
  1544. (org-back-to-heading t)
  1545. (org-cycle-hide-drawers 'children)
  1546. (recenter)
  1547. (org-reveal)
  1548. (if recent
  1549. (message "No running clock, this is the most recently clocked task"))
  1550. (run-hooks 'org-clock-goto-hook)))
  1551. (defvar org-clock-file-total-minutes nil
  1552. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  1553. (make-variable-buffer-local 'org-clock-file-total-minutes)
  1554. (defun org-clock-sum-today (&optional headline-filter)
  1555. "Sum the times for each subtree for today."
  1556. (interactive)
  1557. (let ((range (org-clock-special-range 'today)))
  1558. (org-clock-sum (car range) (cadr range) nil :org-clock-minutes-today)))
  1559. ;;;###autoload
  1560. (defun org-clock-sum (&optional tstart tend headline-filter propname)
  1561. "Sum the times for each subtree.
  1562. Puts the resulting times in minutes as a text property on each headline.
  1563. TSTART and TEND can mark a time range to be considered.
  1564. HEADLINE-FILTER is a zero-arg function that, if specified, is called for
  1565. each headline in the time range with point at the headline. Headlines for
  1566. which HEADLINE-FILTER returns nil are excluded from the clock summation.
  1567. PROPNAME lets you set a custom text property instead of :org-clock-minutes."
  1568. (interactive)
  1569. (let* ((bmp (buffer-modified-p))
  1570. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  1571. org-clock-string
  1572. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  1573. (lmax 30)
  1574. (ltimes (make-vector lmax 0))
  1575. (t1 0)
  1576. (level 0)
  1577. ts te dt
  1578. time)
  1579. (if (stringp tstart) (setq tstart (org-time-string-to-seconds tstart)))
  1580. (if (stringp tend) (setq tend (org-time-string-to-seconds tend)))
  1581. (if (consp tstart) (setq tstart (org-float-time tstart)))
  1582. (if (consp tend) (setq tend (org-float-time tend)))
  1583. (remove-text-properties (point-min) (point-max)
  1584. `(,(or propname :org-clock-minutes) t
  1585. :org-clock-force-headline-inclusion t))
  1586. (save-excursion
  1587. (goto-char (point-max))
  1588. (while (re-search-backward re nil t)
  1589. (cond
  1590. ((match-end 2)
  1591. ;; Two time stamps
  1592. (setq ts (match-string 2)
  1593. te (match-string 3)
  1594. ts (org-float-time
  1595. (apply 'encode-time (org-parse-time-string ts)))
  1596. te (org-float-time
  1597. (apply 'encode-time (org-parse-time-string te)))
  1598. ts (if tstart (max ts tstart) ts)
  1599. te (if tend (min te tend) te)
  1600. dt (- te ts)
  1601. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  1602. ((match-end 4)
  1603. ;; A naked time
  1604. (setq t1 (+ t1 (string-to-number (match-string 5))
  1605. (* 60 (string-to-number (match-string 4))))))
  1606. (t ;; A headline
  1607. ;; Add the currently clocking item time to the total
  1608. (when (and org-clock-report-include-clocking-task
  1609. (equal (org-clocking-buffer) (current-buffer))
  1610. (equal (marker-position org-clock-hd-marker) (point))
  1611. tstart
  1612. tend
  1613. (>= (org-float-time org-clock-start-time) tstart)
  1614. (<= (org-float-time org-clock-start-time) tend))
  1615. (let ((time (floor (- (org-float-time)
  1616. (org-float-time org-clock-start-time)) 60)))
  1617. (setq t1 (+ t1 time))))
  1618. (let* ((headline-forced
  1619. (get-text-property (point)
  1620. :org-clock-force-headline-inclusion))
  1621. (headline-included
  1622. (or (null headline-filter)
  1623. (save-excursion
  1624. (save-match-data (funcall headline-filter))))))
  1625. (setq level (- (match-end 1) (match-beginning 1)))
  1626. (when (or (> t1 0) (> (aref ltimes level) 0))
  1627. (when (or headline-included headline-forced)
  1628. (if headline-included
  1629. (loop for l from 0 to level do
  1630. (aset ltimes l (+ (aref ltimes l) t1))))
  1631. (setq time (aref ltimes level))
  1632. (goto-char (match-beginning 0))
  1633. (put-text-property (point) (point-at-eol)
  1634. (or propname :org-clock-minutes) time)
  1635. (if headline-filter
  1636. (save-excursion
  1637. (save-match-data
  1638. (while
  1639. (> (funcall outline-level) 1)
  1640. (outline-up-heading 1 t)
  1641. (put-text-property
  1642. (point) (point-at-eol)
  1643. :org-clock-force-headline-inclusion t))))))
  1644. (setq t1 0)
  1645. (loop for l from level to (1- lmax) do
  1646. (aset ltimes l 0)))))))
  1647. (setq org-clock-file-total-minutes (aref ltimes 0)))
  1648. (set-buffer-modified-p bmp)))
  1649. (defun org-clock-sum-current-item (&optional tstart)
  1650. "Return time, clocked on current item in total."
  1651. (save-excursion
  1652. (save-restriction
  1653. (org-narrow-to-subtree)
  1654. (org-clock-sum tstart)
  1655. org-clock-file-total-minutes)))
  1656. ;;;###autoload
  1657. (defun org-clock-display (&optional total-only)
  1658. "Show subtree times in the entire buffer.
  1659. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  1660. in the echo area.
  1661. Use \\[org-clock-remove-overlays] to remove the subtree times."
  1662. (interactive)
  1663. (org-clock-remove-overlays)
  1664. (let (time h m p)
  1665. (org-clock-sum)
  1666. (unless total-only
  1667. (save-excursion
  1668. (goto-char (point-min))
  1669. (while (or (and (equal (setq p (point)) (point-min))
  1670. (get-text-property p :org-clock-minutes))
  1671. (setq p (next-single-property-change
  1672. (point) :org-clock-minutes)))
  1673. (goto-char p)
  1674. (when (setq time (get-text-property p :org-clock-minutes))
  1675. (org-clock-put-overlay time (funcall outline-level))))
  1676. (setq h (/ org-clock-file-total-minutes 60)
  1677. m (- org-clock-file-total-minutes (* 60 h)))
  1678. ;; Arrange to remove the overlays upon next change.
  1679. (when org-remove-highlights-with-change
  1680. (org-add-hook 'before-change-functions 'org-clock-remove-overlays
  1681. nil 'local))))
  1682. (message (concat "Total file time: "
  1683. (org-minutes-to-clocksum-string org-clock-file-total-minutes)
  1684. " (%d hours and %d minutes)") h m)))
  1685. (defvar org-clock-overlays nil)
  1686. (make-variable-buffer-local 'org-clock-overlays)
  1687. (defun org-clock-put-overlay (time &optional level)
  1688. "Put an overlays on the current line, displaying TIME.
  1689. If LEVEL is given, prefix time with a corresponding number of stars.
  1690. This creates a new overlay and stores it in `org-clock-overlays', so that it
  1691. will be easy to remove."
  1692. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  1693. (l (if level (org-get-valid-level level 0) 0))
  1694. (off 0)
  1695. ov tx)
  1696. (org-move-to-column c)
  1697. (unless (eolp) (skip-chars-backward "^ \t"))
  1698. (skip-chars-backward " \t")
  1699. (setq ov (make-overlay (point-at-bol) (point-at-eol))
  1700. tx (concat (buffer-substring (point-at-bol) (point))
  1701. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  1702. (org-add-props (concat (make-string l ?*) " "
  1703. (org-minutes-to-clocksum-string time)
  1704. (make-string (- 16 l) ?\ ))
  1705. (list 'face 'org-clock-overlay))
  1706. ""))
  1707. (if (not (featurep 'xemacs))
  1708. (overlay-put ov 'display tx)
  1709. (overlay-put ov 'invisible t)
  1710. (overlay-put ov 'end-glyph (make-glyph tx)))
  1711. (push ov org-clock-overlays)))
  1712. (defun org-clock-remove-overlays (&optional beg end noremove)
  1713. "Remove the occur highlights from the buffer.
  1714. BEG and END are ignored. If NOREMOVE is nil, remove this function
  1715. from the `before-change-functions' in the current buffer."
  1716. (interactive)
  1717. (unless org-inhibit-highlight-removal
  1718. (mapc 'delete-overlay org-clock-overlays)
  1719. (setq org-clock-overlays nil)
  1720. (unless noremove
  1721. (remove-hook 'before-change-functions
  1722. 'org-clock-remove-overlays 'local))))
  1723. (defvar org-state) ;; dynamically scoped into this function
  1724. (defun org-clock-out-if-current ()
  1725. "Clock out if the current entry contains the running clock.
  1726. This is used to stop the clock after a TODO entry is marked DONE,
  1727. and is only done if the variable `org-clock-out-when-done' is not nil."
  1728. (when (and (org-clocking-p)
  1729. org-clock-out-when-done
  1730. (marker-buffer org-clock-marker)
  1731. (or (and (eq t org-clock-out-when-done)
  1732. (member org-state org-done-keywords))
  1733. (and (listp org-clock-out-when-done)
  1734. (member org-state org-clock-out-when-done)))
  1735. (equal (or (buffer-base-buffer (org-clocking-buffer))
  1736. (org-clocking-buffer))
  1737. (or (buffer-base-buffer (current-buffer))
  1738. (current-buffer)))
  1739. (< (point) org-clock-marker)
  1740. (> (save-excursion (outline-next-heading) (point))
  1741. org-clock-marker))
  1742. ;; Clock out, but don't accept a logging message for this.
  1743. (let ((org-log-note-clock-out nil)
  1744. (org-clock-out-switch-to-state nil))
  1745. (org-clock-out))))
  1746. (add-hook 'org-after-todo-state-change-hook
  1747. 'org-clock-out-if-current)
  1748. ;;;###autoload
  1749. (defun org-clock-get-clocktable (&rest props)
  1750. "Get a formatted clocktable with parameters according to PROPS.
  1751. The table is created in a temporary buffer, fully formatted and
  1752. fontified, and then returned."
  1753. ;; Set the defaults
  1754. (setq props (plist-put props :name "clocktable"))
  1755. (unless (plist-member props :maxlevel)
  1756. (setq props (plist-put props :maxlevel 2)))
  1757. (unless (plist-member props :scope)
  1758. (setq props (plist-put props :scope 'agenda)))
  1759. (with-temp-buffer
  1760. (org-mode)
  1761. (org-create-dblock props)
  1762. (org-update-dblock)
  1763. (font-lock-fontify-buffer)
  1764. (forward-line 2)
  1765. (buffer-substring (point) (progn
  1766. (re-search-forward "^[ \t]*#\\+END" nil t)
  1767. (point-at-bol)))))
  1768. ;;;###autoload
  1769. (defun org-clock-report (&optional arg)
  1770. "Create a table containing a report about clocked time.
  1771. If the cursor is inside an existing clocktable block, then the table
  1772. will be updated. If not, a new clocktable will be inserted. The scope
  1773. of the new clock will be subtree when called from within a subtree, and
  1774. file elsewhere.
  1775. When called with a prefix argument, move to the first clock table in the
  1776. buffer and update it."
  1777. (interactive "P")
  1778. (org-clock-remove-overlays)
  1779. (when arg
  1780. (org-find-dblock "clocktable")
  1781. (org-show-entry))
  1782. (if (org-in-clocktable-p)
  1783. (goto-char (org-in-clocktable-p))
  1784. (let ((props (if (ignore-errors
  1785. (save-excursion (org-back-to-heading)))
  1786. (list :name "clocktable" :scope 'subtree)
  1787. (list :name "clocktable"))))
  1788. (org-create-dblock
  1789. (org-combine-plists org-clock-clocktable-default-properties props))))
  1790. (org-update-dblock))
  1791. (defun org-day-of-week (day month year)
  1792. "Returns the day of the week as an integer."
  1793. (nth 6
  1794. (decode-time
  1795. (date-to-time
  1796. (format "%d-%02d-%02dT00:00:00" year month day)))))
  1797. (defun org-quarter-to-date (quarter year)
  1798. "Get the date (week day year) of the first day of a given quarter."
  1799. (let (startday)
  1800. (cond
  1801. ((= quarter 1)
  1802. (setq startday (org-day-of-week 1 1 year))
  1803. (cond
  1804. ((= startday 0)
  1805. (list 52 7 (- year 1)))
  1806. ((= startday 6)
  1807. (list 52 6 (- year 1)))
  1808. ((<= startday 4)
  1809. (list 1 startday year))
  1810. ((> startday 4)
  1811. (list 53 startday (- year 1)))
  1812. )
  1813. )
  1814. ((= quarter 2)
  1815. (setq startday (org-day-of-week 1 4 year))
  1816. (cond
  1817. ((= startday 0)
  1818. (list 13 startday year))
  1819. ((< startday 4)
  1820. (list 14 startday year))
  1821. ((>= startday 4)
  1822. (list 13 startday year))
  1823. )
  1824. )
  1825. ((= quarter 3)
  1826. (setq startday (org-day-of-week 1 7 year))
  1827. (cond
  1828. ((= startday 0)
  1829. (list 26 startday year))
  1830. ((< startday 4)
  1831. (list 27 startday year))
  1832. ((>= startday 4)
  1833. (list 26 startday year))
  1834. )
  1835. )
  1836. ((= quarter 4)
  1837. (setq startday (org-day-of-week 1 10 year))
  1838. (cond
  1839. ((= startday 0)
  1840. (list 39 startday year))
  1841. ((<= startday 4)
  1842. (list 40 startday year))
  1843. ((> startday 4)
  1844. (list 39 startday year)))))))
  1845. (defun org-clock-special-range (key &optional time as-strings wstart)
  1846. "Return two times bordering a special time range.
  1847. Key is a symbol specifying the range and can be one of `today', `yesterday',
  1848. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  1849. By default, a week starts Monday 0:00 and ends Sunday 24:00.
  1850. The range is determined relative to TIME. TIME defaults to the current time.
  1851. The return value is a cons cell with two internal times like the ones
  1852. returned by `current time' or `encode-time'. If AS-STRINGS is non-nil,
  1853. the returned times will be formatted strings. If WSTART is non-nil,
  1854. use this number to specify the starting day of a week (monday is 1)."
  1855. (if (integerp key) (setq key (intern (number-to-string key))))
  1856. (let* ((tm (decode-time (or time (current-time))))
  1857. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  1858. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  1859. (dow (nth 6 tm))
  1860. (ws (or wstart 1))
  1861. (skey (symbol-name key))
  1862. (shift 0)
  1863. (q (cond ((>= (nth 4 tm) 10) 4)
  1864. ((>= (nth 4 tm) 7) 3)
  1865. ((>= (nth 4 tm) 4) 2)
  1866. ((>= (nth 4 tm) 1) 1)))
  1867. s1 m1 h1 d1 month1 y1 diff ts te fm txt w date
  1868. interval tmp shiftedy shiftedm shiftedq)
  1869. (cond
  1870. ((string-match "^[0-9]+$" skey)
  1871. (setq y (string-to-number skey) m 1 d 1 key 'year))
  1872. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  1873. (setq y (string-to-number (match-string 1 skey))
  1874. month (string-to-number (match-string 2 skey))
  1875. d 1 key 'month))
  1876. ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey)
  1877. (require 'cal-iso)
  1878. (setq y (string-to-number (match-string 1 skey))
  1879. w (string-to-number (match-string 2 skey)))
  1880. (setq date (calendar-gregorian-from-absolute
  1881. (calendar-absolute-from-iso (list w 1 y))))
  1882. (setq d (nth 1 date) month (car date) y (nth 2 date)
  1883. dow 1
  1884. key 'week))
  1885. ((string-match "^\\([0-9]+\\)-[qQ]\\([1-4]\\)$" skey)
  1886. (require 'cal-iso)
  1887. (setq y (string-to-number (match-string 1 skey)))
  1888. (setq q (string-to-number (match-string 2 skey)))
  1889. (setq date (calendar-gregorian-from-absolute
  1890. (calendar-absolute-from-iso (org-quarter-to-date q y))))
  1891. (setq d (nth 1 date) month (car date) y (nth 2 date)
  1892. dow 1
  1893. key 'quarter))
  1894. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  1895. (setq y (string-to-number (match-string 1 skey))
  1896. month (string-to-number (match-string 2 skey))
  1897. d (string-to-number (match-string 3 skey))
  1898. key 'day))
  1899. ((string-match "\\([-+][0-9]+\\)$" skey)
  1900. (setq shift (string-to-number (match-string 1 skey))
  1901. key (intern (substring skey 0 (match-beginning 1))))
  1902. (if (and (memq key '(quarter thisq)) (> shift 0))
  1903. (error "Looking forward with quarters isn't implemented"))))
  1904. (when (= shift 0)
  1905. (cond ((eq key 'yesterday) (setq key 'today shift -1))
  1906. ((eq key 'lastweek) (setq key 'week shift -1))
  1907. ((eq key 'lastmonth) (setq key 'month shift -1))
  1908. ((eq key 'lastyear) (setq key 'year shift -1))
  1909. ((eq key 'lastq) (setq key 'quarter shift -1))))
  1910. (cond
  1911. ((memq key '(day today))
  1912. (setq d (+ d shift) h 0 m 0 h1 24 m1 0))
  1913. ((memq key '(week thisweek))
  1914. (setq diff (+ (* -7 shift) (if (= dow 0) (- 7 ws) (- dow ws)))
  1915. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  1916. ((memq key '(month thismonth))
  1917. (setq d 1 h 0 m 0 d1 1 month (+ month shift) month1 (1+ month) h1 0 m1 0))
  1918. ((memq key '(quarter thisq))
  1919. ; compute if this shift remains in this year
  1920. ; if not, compute how many years and quarters we have to shift (via floor*)
  1921. ; and compute the shifted years, months and quarters
  1922. (cond
  1923. ((< (+ (- q 1) shift) 0) ; shift not in this year
  1924. (setq interval (* -1 (+ (- q 1) shift)))
  1925. ; set tmp to ((years to shift) (quarters to shift))
  1926. (setq tmp (org-floor* interval 4))
  1927. ; due to the use of floor, 0 quarters actually means 4
  1928. (if (= 0 (nth 1 tmp))
  1929. (setq shiftedy (- y (nth 0 tmp))
  1930. shiftedm 1
  1931. shiftedq 1)
  1932. (setq shiftedy (- y (+ 1 (nth 0 tmp)))
  1933. shiftedm (- 13 (* 3 (nth 1 tmp)))
  1934. shiftedq (- 5 (nth 1 tmp))))
  1935. (setq d 1 h 0 m 0 d1 1 month shiftedm month1 (+ 3 shiftedm) h1 0 m1 0 y shiftedy))
  1936. ((> (+ q shift) 0) ; shift is within this year
  1937. (setq shiftedq (+ q shift))
  1938. (setq shiftedy y)
  1939. (setq d 1 h 0 m 0 d1 1 month (+ 1 (* 3 (- (+ q shift) 1))) month1 (+ 4 (* 3 (- (+ q shift) 1))) h1 0 m1 0))))
  1940. ((memq key '(year thisyear))
  1941. (setq m 0 h 0 d 1 month 1 y (+ y shift) y1 (1+ y)))
  1942. (t (error "No such time block %s" key)))
  1943. (setq ts (encode-time s m h d month y)
  1944. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  1945. (or d1 d) (or month1 month) (or y1 y)))
  1946. (setq fm (cdr org-time-stamp-formats))
  1947. (cond
  1948. ((memq key '(day today))
  1949. (setq txt (format-time-string "%A, %B %d, %Y" ts)))
  1950. ((memq key '(week thisweek))
  1951. (setq txt (format-time-string "week %G-W%V" ts)))
  1952. ((memq key '(month thismonth))
  1953. (setq txt (format-time-string "%B %Y" ts)))
  1954. ((memq key '(year thisyear))
  1955. (setq txt (format-time-string "the year %Y" ts)))
  1956. ((memq key '(quarter thisq))
  1957. (setq txt (concat (org-count-quarter shiftedq) " quarter of " (number-to-string shiftedy))))
  1958. )
  1959. (if as-strings
  1960. (list (format-time-string fm ts) (format-time-string fm te) txt)
  1961. (list ts te txt))))
  1962. (defun org-count-quarter (n)
  1963. (cond
  1964. ((= n 1) "1st")
  1965. ((= n 2) "2nd")
  1966. ((= n 3) "3rd")
  1967. ((= n 4) "4th")))
  1968. (defun org-clocktable-shift (dir n)
  1969. "Try to shift the :block date of the clocktable at point.
  1970. Point must be in the #+BEGIN: line of a clocktable, or this function
  1971. will throw an error.
  1972. DIR is a direction, a symbol `left', `right', `up', or `down'.
  1973. Both `left' and `down' shift the block toward the past, `up' and `right'
  1974. push it toward the future.
  1975. N is the number of shift steps to take. The size of the step depends on
  1976. the currently selected interval size."
  1977. (setq n (prefix-numeric-value n))
  1978. (and (memq dir '(left down)) (setq n (- n)))
  1979. (save-excursion
  1980. (goto-char (point-at-bol))
  1981. (if (not (looking-at "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
  1982. (error "Line needs a :block definition before this command works")
  1983. (let* ((b (match-beginning 1)) (e (match-end 1))
  1984. (s (match-string 1))
  1985. block shift ins y mw d date wp m)
  1986. (cond
  1987. ((equal s "yesterday") (setq s "today-1"))
  1988. ((equal s "lastweek") (setq s "thisweek-1"))
  1989. ((equal s "lastmonth") (setq s "thismonth-1"))
  1990. ((equal s "lastyear") (setq s "thisyear-1"))
  1991. ((equal s "lastq") (setq s "thisq-1")))
  1992. (cond
  1993. ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\|thisq\\)\\([-+][0-9]+\\)?$" s)
  1994. (setq block (match-string 1 s)
  1995. shift (if (match-end 2)
  1996. (string-to-number (match-string 2 s))
  1997. 0))
  1998. (setq shift (+ shift n))
  1999. (setq ins (if (= shift 0) block (format "%s%+d" block shift))))
  2000. ((string-match "\\([0-9]+\\)\\(-\\([wWqQ]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s)
  2001. ;; 1 1 2 3 3 4 4 5 6 6 5 2
  2002. (setq y (string-to-number (match-string 1 s))
  2003. wp (and (match-end 3) (match-string 3 s))
  2004. mw (and (match-end 4) (string-to-number (match-string 4 s)))
  2005. d (and (match-end 6) (string-to-number (match-string 6 s))))
  2006. (cond
  2007. (d (setq ins (format-time-string
  2008. "%Y-%m-%d"
  2009. (encode-time 0 0 0 (+ d n) m y))))
  2010. ((and wp (string-match "w\\|W" wp) mw (> (length wp) 0))
  2011. (require 'cal-iso)
  2012. (setq date (calendar-gregorian-from-absolute
  2013. (calendar-absolute-from-iso (list (+ mw n) 1 y))))
  2014. (setq ins (format-time-string
  2015. "%G-W%V"
  2016. (encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
  2017. ((and wp (string-match "q\\|Q" wp) mw (> (length wp) 0))
  2018. (require 'cal-iso)
  2019. ; if the 4th + 1 quarter is requested we flip to the 1st quarter of the next year
  2020. (if (> (+ mw n) 4)
  2021. (setq mw 0
  2022. y (+ 1 y))
  2023. ())
  2024. ; if the 1st - 1 quarter is requested we flip to the 4th quarter of the previous year
  2025. (if (= (+ mw n) 0)
  2026. (setq mw 5
  2027. y (- y 1))
  2028. ())
  2029. (setq date (calendar-gregorian-from-absolute
  2030. (calendar-absolute-from-iso (org-quarter-to-date (+ mw n) y))))
  2031. (setq ins (format-time-string
  2032. (concat (number-to-string y) "-Q" (number-to-string (+ mw n)))
  2033. (encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
  2034. (mw
  2035. (setq ins (format-time-string
  2036. "%Y-%m"
  2037. (encode-time 0 0 0 1 (+ mw n) y))))
  2038. (y
  2039. (setq ins (number-to-string (+ y n))))))
  2040. (t (error "Cannot shift clocktable block")))
  2041. (when ins
  2042. (goto-char b)
  2043. (insert ins)
  2044. (delete-region (point) (+ (point) (- e b)))
  2045. (beginning-of-line 1)
  2046. (org-update-dblock)
  2047. t)))))
  2048. ;;;###autoload
  2049. (defun org-dblock-write:clocktable (params)
  2050. "Write the standard clocktable."
  2051. (setq params (org-combine-plists org-clocktable-defaults params))
  2052. (catch 'exit
  2053. (let* ((scope (plist-get params :scope))
  2054. (block (plist-get params :block))
  2055. (ts (plist-get params :tstart))
  2056. (te (plist-get params :tend))
  2057. (link (plist-get params :link))
  2058. (maxlevel (or (plist-get params :maxlevel) 3))
  2059. (ws (plist-get params :wstart))
  2060. (step (plist-get params :step))
  2061. (timestamp (plist-get params :timestamp))
  2062. (formatter (or (plist-get params :formatter)
  2063. org-clock-clocktable-formatter
  2064. 'org-clocktable-write-default))
  2065. cc range-text ipos pos one-file-with-archives
  2066. scope-is-list tbls level)
  2067. ;; Check if we need to do steps
  2068. (when block
  2069. ;; Get the range text for the header
  2070. (setq cc (org-clock-special-range block nil t ws)
  2071. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  2072. (when step
  2073. ;; Write many tables, in steps
  2074. (unless (or block (and ts te))
  2075. (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
  2076. (org-clocktable-steps params)
  2077. (throw 'exit nil))
  2078. (setq ipos (point)) ; remember the insertion position
  2079. ;; Get the right scope
  2080. (setq pos (point))
  2081. (cond
  2082. ((and scope (listp scope) (symbolp (car scope)))
  2083. (setq scope (eval scope)))
  2084. ((eq scope 'agenda)
  2085. (setq scope (org-agenda-files t)))
  2086. ((eq scope 'agenda-with-archives)
  2087. (setq scope (org-agenda-files t))
  2088. (setq scope (org-add-archive-files scope)))
  2089. ((eq scope 'file-with-archives)
  2090. (setq scope (org-add-archive-files (list (buffer-file-name)))
  2091. one-file-with-archives t)))
  2092. (setq scope-is-list (and scope (listp scope)))
  2093. (if scope-is-list
  2094. ;; we collect from several files
  2095. (let* ((files scope)
  2096. file)
  2097. (org-agenda-prepare-buffers files)
  2098. (while (setq file (pop files))
  2099. (with-current-buffer (find-buffer-visiting file)
  2100. (save-excursion
  2101. (save-restriction
  2102. (push (org-clock-get-table-data file params) tbls))))))
  2103. ;; Just from the current file
  2104. (save-restriction
  2105. ;; get the right range into the restriction
  2106. (org-agenda-prepare-buffers (list (buffer-file-name)))
  2107. (cond
  2108. ((not scope)) ; use the restriction as it is now
  2109. ((eq scope 'file) (widen))
  2110. ((eq scope 'subtree) (org-narrow-to-subtree))
  2111. ((eq scope 'tree)
  2112. (while (org-up-heading-safe))
  2113. (org-narrow-to-subtree))
  2114. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  2115. (symbol-name scope)))
  2116. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  2117. (catch 'exit
  2118. (while (org-up-heading-safe)
  2119. (looking-at org-outline-regexp)
  2120. (if (<= (org-reduced-level (funcall outline-level)) level)
  2121. (throw 'exit nil))))
  2122. (org-narrow-to-subtree)))
  2123. ;; do the table, with no file name.
  2124. (push (org-clock-get-table-data nil params) tbls)))
  2125. ;; OK, at this point we tbls as a list of tables, one per file
  2126. (setq tbls (nreverse tbls))
  2127. (setq params (plist-put params :multifile scope-is-list))
  2128. (setq params (plist-put params :one-file-with-archives
  2129. one-file-with-archives))
  2130. (funcall formatter ipos tbls params))))
  2131. (defun org-clocktable-write-default (ipos tables params)
  2132. "Write out a clock table at position IPOS in the current buffer.
  2133. TABLES is a list of tables with clocking data as produced by
  2134. `org-clock-get-table-data'. PARAMS is the parameter property list obtained
  2135. from the dynamic block definition."
  2136. ;; This function looks quite complicated, mainly because there are a
  2137. ;; lot of options which can add or remove columns. I have massively
  2138. ;; commented this function, the I hope it is understandable. If
  2139. ;; someone wants to write their own special formatter, this maybe
  2140. ;; much easier because there can be a fixed format with a
  2141. ;; well-defined number of columns...
  2142. (let* ((hlchars '((1 . "*") (2 . "/")))
  2143. (lwords (assoc (or (plist-get params :lang)
  2144. org-export-default-language)
  2145. org-clock-clocktable-language-setup))
  2146. (multifile (plist-get params :multifile))
  2147. (block (plist-get params :block))
  2148. (ts (plist-get params :tstart))
  2149. (te (plist-get params :tend))
  2150. (header (plist-get params :header))
  2151. (narrow (plist-get params :narrow))
  2152. (ws (or (plist-get params :wstart) 1))
  2153. (link (plist-get params :link))
  2154. (maxlevel (or (plist-get params :maxlevel) 3))
  2155. (emph (plist-get params :emphasize))
  2156. (level-p (plist-get params :level))
  2157. (org-time-clocksum-use-effort-durations
  2158. (plist-get params :effort-durations))
  2159. (timestamp (plist-get params :timestamp))
  2160. (properties (plist-get params :properties))
  2161. (ntcol (max 1 (or (plist-get params :tcolumns) 100)))
  2162. (rm-file-column (plist-get params :one-file-with-archives))
  2163. (indent (plist-get params :indent))
  2164. (case-fold-search t)
  2165. range-text total-time tbl level hlc formula pcol
  2166. file-time entries entry headline
  2167. recalc content narrow-cut-p tcol)
  2168. ;; Implement abbreviations
  2169. (when (plist-get params :compact)
  2170. (setq level nil indent t narrow (or narrow '40!) ntcol 1))
  2171. ;; Some consistency test for parameters
  2172. (unless (integerp ntcol)
  2173. (setq params (plist-put params :tcolumns (setq ntcol 100))))
  2174. (when (and narrow (integerp narrow) link)
  2175. ;; We cannot have both integer narrow and link
  2176. (message
  2177. "Using hard narrowing in clocktable to allow for links")
  2178. (setq narrow (intern (format "%d!" narrow))))
  2179. (when narrow
  2180. (cond
  2181. ((integerp narrow))
  2182. ((and (symbolp narrow)
  2183. (string-match "\\`[0-9]+!\\'" (symbol-name narrow)))
  2184. (setq narrow-cut-p t
  2185. narrow (string-to-number (substring (symbol-name narrow)
  2186. 0 -1))))
  2187. (t
  2188. (error "Invalid value %s of :narrow property in clock table"
  2189. narrow))))
  2190. (when block
  2191. ;; Get the range text for the header
  2192. (setq range-text (nth 2 (org-clock-special-range block nil t ws))))
  2193. ;; Compute the total time
  2194. (setq total-time (apply '+ (mapcar 'cadr tables)))
  2195. ;; Now we need to output this tsuff
  2196. (goto-char ipos)
  2197. ;; Insert the text *before* the actual table
  2198. (insert-before-markers
  2199. (or header
  2200. ;; Format the standard header
  2201. (concat
  2202. "#+CAPTION: "
  2203. (nth 9 lwords) " ["
  2204. (substring
  2205. (format-time-string (cdr org-time-stamp-formats))
  2206. 1 -1)
  2207. "]"
  2208. (if block (concat ", for " range-text ".") "")
  2209. "\n")))
  2210. ;; Insert the narrowing line
  2211. (when (and narrow (integerp narrow) (not narrow-cut-p))
  2212. (insert-before-markers
  2213. "|" ; table line starter
  2214. (if multifile "|" "") ; file column, maybe
  2215. (if level-p "|" "") ; level column, maybe
  2216. (if timestamp "|" "") ; timestamp column, maybe
  2217. (if properties (make-string (length properties) ?|) "") ;properties columns, maybe
  2218. (format "<%d>| |\n" narrow))) ; headline and time columns
  2219. ;; Insert the table header line
  2220. (insert-before-markers
  2221. "|" ; table line starter
  2222. (if multifile (concat (nth 1 lwords) "|") "") ; file column, maybe
  2223. (if level-p (concat (nth 2 lwords) "|") "") ; level column, maybe
  2224. (if timestamp (concat (nth 3 lwords) "|") "") ; timestamp column, maybe
  2225. (if properties (concat (mapconcat 'identity properties "|") "|") "") ;properties columns, maybe
  2226. (concat (nth 4 lwords) "|"
  2227. (nth 5 lwords) "|\n")) ; headline and time columns
  2228. ;; Insert the total time in the table
  2229. (insert-before-markers
  2230. "|-\n" ; a hline
  2231. "|" ; table line starter
  2232. (if multifile (concat "| " (nth 6 lwords) " ") "")
  2233. ; file column, maybe
  2234. (if level-p "|" "") ; level column, maybe
  2235. (if timestamp "|" "") ; timestamp column, maybe
  2236. (if properties (make-string (length properties) ?|) "") ; properties columns, maybe
  2237. (concat (format org-clock-total-time-cell-format (nth 7 lwords)) "| ") ; instead of a headline
  2238. (format org-clock-total-time-cell-format
  2239. (org-minutes-to-clocksum-string (or total-time 0))) ; the time
  2240. "|\n") ; close line
  2241. ;; Now iterate over the tables and insert the data
  2242. ;; but only if any time has been collected
  2243. (when (and total-time (> total-time 0))
  2244. (while (setq tbl (pop tables))
  2245. ;; now tbl is the table resulting from one file.
  2246. (setq file-time (nth 1 tbl))
  2247. (when (or (and file-time (> file-time 0))
  2248. (not (plist-get params :fileskip0)))
  2249. (insert-before-markers "|-\n") ; a hline because a new file starts
  2250. ;; First the file time, if we have multiple files
  2251. (when multifile
  2252. ;; Summarize the time collected from this file
  2253. (insert-before-markers
  2254. (format (concat "| %s %s | %s%s"
  2255. (format org-clock-file-time-cell-format (nth 8 lwords))
  2256. " | *%s*|\n")
  2257. (file-name-nondirectory (car tbl))
  2258. (if level-p "| " "") ; level column, maybe
  2259. (if timestamp "| " "") ; timestamp column, maybe
  2260. (if properties (make-string (length properties) ?|) "") ;properties columns, maybe
  2261. (org-minutes-to-clocksum-string (nth 1 tbl))))) ; the time
  2262. ;; Get the list of node entries and iterate over it
  2263. (setq entries (nth 2 tbl))
  2264. (while (setq entry (pop entries))
  2265. (setq level (car entry)
  2266. headline (nth 1 entry)
  2267. hlc (if emph (or (cdr (assoc level hlchars)) "") ""))
  2268. (when narrow-cut-p
  2269. (if (and (string-match (concat "\\`" org-bracket-link-regexp
  2270. "\\'")
  2271. headline)
  2272. (match-end 3))
  2273. (setq headline
  2274. (format "[[%s][%s]]"
  2275. (match-string 1 headline)
  2276. (org-shorten-string (match-string 3 headline)
  2277. narrow)))
  2278. (setq headline (org-shorten-string headline narrow))))
  2279. (insert-before-markers
  2280. "|" ; start the table line
  2281. (if multifile "|" "") ; free space for file name column?
  2282. (if level-p (format "%d|" (car entry)) "") ; level, maybe
  2283. (if timestamp (concat (nth 2 entry) "|") "") ; timestamp, maybe
  2284. (if properties
  2285. (concat
  2286. (mapconcat
  2287. (lambda (p) (or (cdr (assoc p (nth 4 entry))) ""))
  2288. properties "|") "|") "") ;properties columns, maybe
  2289. (if indent (org-clocktable-indent-string level) "") ; indentation
  2290. hlc headline hlc "|" ; headline
  2291. (make-string (min (1- ntcol) (or (- level 1))) ?|)
  2292. ; empty fields for higher levels
  2293. hlc (org-minutes-to-clocksum-string (nth 3 entry)) hlc ; time
  2294. "|\n" ; close line
  2295. )))))
  2296. ;; When exporting subtrees or regions the region might be
  2297. ;; activated, so let's disable ̀delete-active-region'
  2298. (let ((delete-active-region nil)) (backward-delete-char 1))
  2299. (if (setq formula (plist-get params :formula))
  2300. (cond
  2301. ((eq formula '%)
  2302. ;; compute the column where the % numbers need to go
  2303. (setq pcol (+ 2
  2304. (if multifile 1 0)
  2305. (if level-p 1 0)
  2306. (if timestamp 1 0)
  2307. (min maxlevel (or ntcol 100))))
  2308. ;; compute the column where the total time is
  2309. (setq tcol (+ 2
  2310. (if multifile 1 0)
  2311. (if level-p 1 0)
  2312. (if timestamp 1 0)))
  2313. (insert
  2314. (format
  2315. "\n#+TBLFM: $%d='(org-clock-time%% @%d$%d $%d..$%d);%%.1f"
  2316. pcol ; the column where the % numbers should go
  2317. (if (and narrow (not narrow-cut-p)) 3 2) ; row of the total time
  2318. tcol ; column of the total time
  2319. tcol (1- pcol) ; range of columns where times can be found
  2320. ))
  2321. (setq recalc t))
  2322. ((stringp formula)
  2323. (insert "\n#+TBLFM: " formula)
  2324. (setq recalc t))
  2325. (t (error "Invalid formula in clocktable")))
  2326. ;; Should we rescue an old formula?
  2327. (when (stringp (setq content (plist-get params :content)))
  2328. (when (string-match "^\\([ \t]*#\\+tblfm:.*\\)" content)
  2329. (setq recalc t)
  2330. (insert "\n" (match-string 1 (plist-get params :content)))
  2331. (beginning-of-line 0))))
  2332. ;; Back to beginning, align the table, recalculate if necessary
  2333. (goto-char ipos)
  2334. (skip-chars-forward "^|")
  2335. (org-table-align)
  2336. (when org-hide-emphasis-markers
  2337. ;; we need to align a second time
  2338. (org-table-align))
  2339. (when recalc
  2340. (if (eq formula '%)
  2341. (save-excursion
  2342. (if (and narrow (not narrow-cut-p)) (beginning-of-line 2))
  2343. (org-table-goto-column pcol nil 'force)
  2344. (insert "%")))
  2345. (org-table-recalculate 'all))
  2346. (when rm-file-column
  2347. ;; The file column is actually not wanted
  2348. (forward-char 1)
  2349. (org-table-delete-column))
  2350. total-time))
  2351. (defun org-clocktable-indent-string (level)
  2352. (if (= level 1)
  2353. ""
  2354. (let ((str "\\__"))
  2355. (while (> level 2)
  2356. (setq level (1- level)
  2357. str (concat str "___")))
  2358. (concat str " "))))
  2359. (defun org-clocktable-steps (params)
  2360. "Step through the range to make a number of clock tables."
  2361. (let* ((p1 (copy-sequence params))
  2362. (ts (plist-get p1 :tstart))
  2363. (te (plist-get p1 :tend))
  2364. (ws (plist-get p1 :wstart))
  2365. (step0 (plist-get p1 :step))
  2366. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  2367. (stepskip0 (plist-get p1 :stepskip0))
  2368. (block (plist-get p1 :block))
  2369. cc range-text step-time)
  2370. (when block
  2371. (setq cc (org-clock-special-range block nil t ws)
  2372. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  2373. (cond
  2374. ((numberp ts)
  2375. ;; If ts is a number, it's an absolute day number from org-agenda.
  2376. (destructuring-bind (month day year) (calendar-gregorian-from-absolute ts)
  2377. (setq ts (org-float-time (encode-time 0 0 0 day month year)))))
  2378. (ts
  2379. (setq ts (org-float-time
  2380. (apply 'encode-time (org-parse-time-string ts))))))
  2381. (cond
  2382. ((numberp te)
  2383. ;; Likewise for te.
  2384. (destructuring-bind (month day year) (calendar-gregorian-from-absolute te)
  2385. (setq te (org-float-time (encode-time 0 0 0 day month year)))))
  2386. (te
  2387. (setq te (org-float-time
  2388. (apply 'encode-time (org-parse-time-string te))))))
  2389. (setq p1 (plist-put p1 :header ""))
  2390. (setq p1 (plist-put p1 :step nil))
  2391. (setq p1 (plist-put p1 :block nil))
  2392. (while (< ts te)
  2393. (or (bolp) (insert "\n"))
  2394. (setq p1 (plist-put p1 :tstart (format-time-string
  2395. (org-time-stamp-format nil t)
  2396. (seconds-to-time ts))))
  2397. (setq p1 (plist-put p1 :tend (format-time-string
  2398. (org-time-stamp-format nil t)
  2399. (seconds-to-time (setq ts (+ ts step))))))
  2400. (insert "\n" (if (eq step0 'day) "Daily report: "
  2401. "Weekly report starting on: ")
  2402. (plist-get p1 :tstart) "\n")
  2403. (setq step-time (org-dblock-write:clocktable p1))
  2404. (re-search-forward "^[ \t]*#\\+END:")
  2405. (when (and (equal step-time 0) stepskip0)
  2406. ;; Remove the empty table
  2407. (delete-region (point-at-bol)
  2408. (save-excursion
  2409. (re-search-backward "^\\(Daily\\|Weekly\\) report"
  2410. nil t)
  2411. (point))))
  2412. (end-of-line 0))))
  2413. (defun org-clock-get-table-data (file params)
  2414. "Get the clocktable data for file FILE, with parameters PARAMS.
  2415. FILE is only for identification - this function assumes that
  2416. the correct buffer is current, and that the wanted restriction is
  2417. in place.
  2418. The return value will be a list with the file name and the total
  2419. file time (in minutes) as 1st and 2nd elements. The third element
  2420. of this list will be a list of headline entries. Each entry has the
  2421. following structure:
  2422. (LEVEL HEADLINE TIMESTAMP TIME)
  2423. LEVEL: The level of the headline, as an integer. This will be
  2424. the reduced leve, so 1,2,3,... even if only odd levels
  2425. are being used.
  2426. HEADLINE: The text of the headline. Depending on PARAMS, this may
  2427. already be formatted like a link.
  2428. TIMESTAMP: If PARAMS require it, this will be a time stamp found in the
  2429. entry, any of SCHEDULED, DEADLINE, NORMAL, or first inactive,
  2430. in this sequence.
  2431. TIME: The sum of all time spend in this tree, in minutes. This time
  2432. will of cause be restricted to the time block and tags match
  2433. specified in PARAMS."
  2434. (let* ((maxlevel (or (plist-get params :maxlevel) 3))
  2435. (timestamp (plist-get params :timestamp))
  2436. (ts (plist-get params :tstart))
  2437. (te (plist-get params :tend))
  2438. (ws (plist-get params :wstart))
  2439. (block (plist-get params :block))
  2440. (link (plist-get params :link))
  2441. (tags (plist-get params :tags))
  2442. (properties (plist-get params :properties))
  2443. (inherit-property-p (plist-get params :inherit-props))
  2444. todo-only
  2445. (matcher (if tags (cdr (org-make-tags-matcher tags))))
  2446. cc range-text st p time level hdl props tsp tbl)
  2447. (setq org-clock-file-total-minutes nil)
  2448. (when block
  2449. (setq cc (org-clock-special-range block nil t ws)
  2450. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  2451. (when (integerp ts) (setq ts (calendar-gregorian-from-absolute ts)))
  2452. (when (integerp te) (setq te (calendar-gregorian-from-absolute te)))
  2453. (when (and ts (listp ts))
  2454. (setq ts (format "%4d-%02d-%02d" (nth 2 ts) (car ts) (nth 1 ts))))
  2455. (when (and te (listp te))
  2456. (setq te (format "%4d-%02d-%02d" (nth 2 te) (car te) (nth 1 te))))
  2457. ;; Now the times are strings we can parse.
  2458. (if ts (setq ts (org-float-time
  2459. (seconds-to-time (org-matcher-time ts)))))
  2460. (if te (setq te (org-float-time
  2461. (seconds-to-time (org-matcher-time te)))))
  2462. (save-excursion
  2463. (org-clock-sum ts te
  2464. (unless (null matcher)
  2465. (lambda ()
  2466. (let* ((tags-list (org-get-tags-at))
  2467. (org-scanner-tags tags-list)
  2468. (org-trust-scanner-tags t))
  2469. (eval matcher)))))
  2470. (goto-char (point-min))
  2471. (setq st t)
  2472. (while (or (and (bobp) (prog1 st (setq st nil))
  2473. (get-text-property (point) :org-clock-minutes)
  2474. (setq p (point-min)))
  2475. (setq p (next-single-property-change
  2476. (point) :org-clock-minutes)))
  2477. (goto-char p)
  2478. (when (setq time (get-text-property p :org-clock-minutes))
  2479. (save-excursion
  2480. (beginning-of-line 1)
  2481. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@#%:]+:\\)?[ \t]*$"))
  2482. (setq level (org-reduced-level
  2483. (- (match-end 1) (match-beginning 1))))
  2484. (<= level maxlevel))
  2485. (setq hdl (if (not link)
  2486. (match-string 2)
  2487. (org-make-link-string
  2488. (format "file:%s::%s"
  2489. (buffer-file-name)
  2490. (save-match-data
  2491. (org-make-org-heading-search-string
  2492. (match-string 2))))
  2493. (match-string 2)))
  2494. tsp (when timestamp
  2495. (setq props (org-entry-properties (point)))
  2496. (or (cdr (assoc "SCHEDULED" props))
  2497. (cdr (assoc "DEADLINE" props))
  2498. (cdr (assoc "TIMESTAMP" props))
  2499. (cdr (assoc "TIMESTAMP_IA" props))))
  2500. props (when properties
  2501. (remove nil
  2502. (mapcar
  2503. (lambda (p)
  2504. (when (org-entry-get (point) p inherit-property-p)
  2505. (cons p (org-entry-get (point) p inherit-property-p))))
  2506. properties))))
  2507. (when (> time 0) (push (list level hdl tsp time props) tbl))))))
  2508. (setq tbl (nreverse tbl))
  2509. (list file org-clock-file-total-minutes tbl))))
  2510. (defun org-clock-time% (total &rest strings)
  2511. "Compute a time fraction in percent.
  2512. TOTAL s a time string like 10:21 specifying the total times.
  2513. STRINGS is a list of strings that should be checked for a time.
  2514. The first string that does have a time will be used.
  2515. This function is made for clock tables."
  2516. (let ((re "\\([0-9]+\\):\\([0-9]+\\)")
  2517. tot s)
  2518. (save-match-data
  2519. (catch 'exit
  2520. (if (not (string-match re total))
  2521. (throw 'exit 0.)
  2522. (setq tot (+ (string-to-number (match-string 2 total))
  2523. (* 60 (string-to-number (match-string 1 total)))))
  2524. (if (= tot 0.) (throw 'exit 0.)))
  2525. (while (setq s (pop strings))
  2526. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  2527. (throw 'exit
  2528. (/ (* 100.0 (+ (string-to-number (match-string 2 s))
  2529. (* 60 (string-to-number
  2530. (match-string 1 s)))))
  2531. tot))))
  2532. 0))))
  2533. ;; Saving and loading the clock
  2534. (defvar org-clock-loaded nil
  2535. "Was the clock file loaded?")
  2536. (defun org-clock-update-time-maybe ()
  2537. "If this is a CLOCK line, update it and return t.
  2538. Otherwise, return nil."
  2539. (interactive)
  2540. (save-excursion
  2541. (beginning-of-line 1)
  2542. (skip-chars-forward " \t")
  2543. (when (looking-at org-clock-string)
  2544. (let ((re (concat "[ \t]*" org-clock-string
  2545. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2546. "\\([ \t]*=>.*\\)?\\)?"))
  2547. ts te h m s neg)
  2548. (cond
  2549. ((not (looking-at re))
  2550. nil)
  2551. ((not (match-end 2))
  2552. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2553. (> org-clock-marker (point))
  2554. (<= org-clock-marker (point-at-eol)))
  2555. ;; The clock is running here
  2556. (setq org-clock-start-time
  2557. (apply 'encode-time
  2558. (org-parse-time-string (match-string 1))))
  2559. (org-clock-update-mode-line)))
  2560. (t
  2561. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2562. (end-of-line 1)
  2563. (setq ts (match-string 1)
  2564. te (match-string 3))
  2565. (setq s (- (org-float-time
  2566. (apply 'encode-time (org-parse-time-string te)))
  2567. (org-float-time
  2568. (apply 'encode-time (org-parse-time-string ts))))
  2569. neg (< s 0)
  2570. s (abs s)
  2571. h (floor (/ s 3600))
  2572. s (- s (* 3600 h))
  2573. m (floor (/ s 60))
  2574. s (- s (* 60 s)))
  2575. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2576. t))))))
  2577. (defun org-clock-save ()
  2578. "Persist various clock-related data to disk.
  2579. The details of what will be saved are regulated by the variable
  2580. `org-clock-persist'."
  2581. (when (and org-clock-persist
  2582. (or org-clock-loaded
  2583. org-clock-has-been-used
  2584. (not (file-exists-p org-clock-persist-file))))
  2585. (let (b)
  2586. (with-current-buffer (find-file (expand-file-name org-clock-persist-file))
  2587. (progn
  2588. (delete-region (point-min) (point-max))
  2589. ;;Store clock
  2590. (insert (format ";; org-persist.el - %s at %s\n"
  2591. system-name (format-time-string
  2592. (cdr org-time-stamp-formats))))
  2593. (if (and (memq org-clock-persist '(t clock))
  2594. (setq b (org-clocking-buffer))
  2595. (setq b (or (buffer-base-buffer b) b))
  2596. (buffer-live-p b)
  2597. (buffer-file-name b)
  2598. (or (not org-clock-persist-query-save)
  2599. (y-or-n-p (concat "Save current clock ("
  2600. (substring-no-properties
  2601. org-clock-heading)
  2602. ") "))))
  2603. (insert "(setq resume-clock '(\""
  2604. (buffer-file-name (org-clocking-buffer))
  2605. "\" . " (int-to-string (marker-position org-clock-marker))
  2606. "))\n"))
  2607. ;; Store clocked task history. Tasks are stored reversed to make
  2608. ;; reading simpler
  2609. (when (and (memq org-clock-persist '(t history))
  2610. org-clock-history)
  2611. (insert
  2612. "(setq stored-clock-history '("
  2613. (mapconcat
  2614. (lambda (m)
  2615. (when (and (setq b (marker-buffer m))
  2616. (setq b (or (buffer-base-buffer b) b))
  2617. (buffer-live-p b)
  2618. (buffer-file-name b))
  2619. (concat "(\"" (buffer-file-name b)
  2620. "\" . " (int-to-string (marker-position m))
  2621. ")")))
  2622. (reverse org-clock-history) " ") "))\n"))
  2623. (save-buffer)
  2624. (kill-buffer (current-buffer)))))))
  2625. (defun org-clock-load ()
  2626. "Load clock-related data from disk, maybe resuming a stored clock."
  2627. (when (and org-clock-persist (not org-clock-loaded))
  2628. (let ((filename (expand-file-name org-clock-persist-file))
  2629. (org-clock-in-resume 'auto-restart)
  2630. resume-clock stored-clock-history)
  2631. (if (not (file-readable-p filename))
  2632. (message "Not restoring clock data; %s not found"
  2633. org-clock-persist-file)
  2634. (message "%s" "Restoring clock data")
  2635. (setq org-clock-loaded t)
  2636. (load-file filename)
  2637. ;; load history
  2638. (when stored-clock-history
  2639. (save-window-excursion
  2640. (mapc (lambda (task)
  2641. (if (file-exists-p (car task))
  2642. (org-clock-history-push (cdr task)
  2643. (find-file (car task)))))
  2644. stored-clock-history)))
  2645. ;; resume clock
  2646. (when (and resume-clock org-clock-persist
  2647. (file-exists-p (car resume-clock))
  2648. (or (not org-clock-persist-query-resume)
  2649. (y-or-n-p
  2650. (concat
  2651. "Resume clock ("
  2652. (with-current-buffer (find-file (car resume-clock))
  2653. (save-excursion
  2654. (goto-char (cdr resume-clock))
  2655. (org-back-to-heading t)
  2656. (and (looking-at org-complex-heading-regexp)
  2657. (match-string 4))))
  2658. ") "))))
  2659. (when (file-exists-p (car resume-clock))
  2660. (with-current-buffer (find-file (car resume-clock))
  2661. (goto-char (cdr resume-clock))
  2662. (let ((org-clock-auto-clock-resolution nil))
  2663. (org-clock-in)
  2664. (if (outline-invisible-p)
  2665. (org-show-context))))))))))
  2666. ;; Suggested bindings
  2667. (org-defkey org-mode-map "\C-c\C-x\C-e" 'org-clock-modify-effort-estimate)
  2668. (provide 'org-clock)
  2669. ;; Local variables:
  2670. ;; generated-autoload-file: "org-loaddefs.el"
  2671. ;; End:
  2672. ;;; org-clock.el ends here