org-clock.el 115 KB

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