emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: clock-table and hooking that into org-capture file+olp+datetree


From: Richard Lawrence
Subject: Re: clock-table and hooking that into org-capture file+olp+datetree
Date: Sat, 30 Jan 2021 13:53:40 +0100

Hi Christopher,

"Christopher Causer" <ml-emacs-orgmode@chyc.co.uk> writes:

> Hello everyone! Here's a reasonably easy (I think) question because I'm quite 
> new to Emacs and org-mode.
>
> I have an org-capture template using file+olp+datetree[1], which works great 
> at filing my thoughts for the day. Separately I know I can generate clock 
> tables[2] based on dynamic blocks to show me what I've been doing with my 
> time for any given period. What I'm struggling with is to glue parts of these 
> together to achieve the following:
>
> 1. I org-capture to a subheading of datetree. When it does so it either 
> creates or updates an org-clock-report just below the datetree header  (the 
> bit that says "2020-11-12 Thursday", for example.) I guess this would be the 
> parent of what I'm capturing.
>
> 2. For all my historical journal entries, if I could move point to a headline 
> with a date such as the example below and it would pull the date out and add 
> a clocktable below via an interactive function that would be my ideal. This 
> is less of a problem for me as I don't have much in the way of history in my 
> diary yet or my other org files.
>

If I understand right, what you need for both of these things is a
function to jump to a date in your diary datetree and update the
clocktable there. Right?

Some functions that will help with this:
- org-datetree-find-date-create
- org-narrow-to-subtree

So, something like this should get you started:

#+begin_src emacs-lisp
(defun org-update-clocktable-on-date (date)
  (save-excursion
    ;; open the file containing the datetree:
    (find-file (concat org-directory "/diary.org"))
    ;; jump to the subtree for the given date:
    ;; note: date must look like (m d y) where all three values are integers
    (org-datetree-find-date-create date)
    ;; narrow to the subtree for this date, so we don't update
    ;; any other clocktables
    (org-narrow-to-subtree)
    ;; update the clock report, or create it if it doesn't exist
    ;; note: we pass a prefix argument to tell org-clock-report to
    ;; update the first clocktable it finds in the (narrowed) buffer
    (org-clock-report t)
    ;; widen to the whole buffer again
    (widen)))
#+end_src

Then you can call this function, providing the date, in different
contexts where you want to create or update the clocktable.

Note that org-datetree-find-date has a slightly annoying interface, in
that you need to provide a list of three integers representing a
calendar date. One easy way to do that interactively is with
calendar-read-date, which prompts you for the year, month and day, so
you could say

(org-update-clocktable-on-date (calendar-read-date))

calendar-read-date is not as nice to use interactively as org-read-date,
but as far as I know, there is no easy way to get the calendar (m d y) format
out of its return value, which is either a string like "2021-01-30" or a
value in Emacs' internal time representation format.  But you can do
something like

(let*
    ;; prompt for the date and decode the resulting internal time as a list:
    ((decoded (decode-time (org-read-date nil t nil "Update on date:")))
    ;; unpack the date as a list (m d y) from the decoded time:
     (date (list (nth 4 decoded) ; month
                 (nth 3 decoded) ; day
                 (nth 5 decoded)))) ; year

  (org-update-clocktable-on-date date))

Hope that helps get you to your next step!

-- 
Best,
Richard



reply via email to

[Prev in Thread] Current Thread [Next in Thread]