org-clock.el 114 KB

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