emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Function that splits a CLOCK interval
@ 2015-03-31 16:59 Christoph LANGE
  2015-04-01 10:37 ` Peter Frings
  0 siblings, 1 reply; 4+ messages in thread
From: Christoph LANGE @ 2015-03-31 16:59 UTC (permalink / raw)
  To: emacs-orgmode

Dear org-mode community,

I use org's clocking facility to clock all my working time.  I 
frequently find myself clocking time for a task T1, but later realise 
that I actually spent part of this time on some other task T2.  In such 
a situation I go to the corresponding CLOCK: line for T1, split the 
interval clocked, e.g. from

    CLOCK: [2015-03-30 Mon 16:27]--[2015-03-30 Mon 16:30] =>  0:03

to

    CLOCK: [2015-03-30 Mon 16:28]--[2015-03-30 Mon 16:30] =>  0:02
    CLOCK: [2015-03-30 Mon 16:27]--[2015-03-30 Mon 16:28] =>  0:01

and move one of the two lines to the LOGBOOK of task T2.

The following function now automates the task of splitting:

--- %< --- %< --- %< --- %< --- %< --- %< --- %< --- %< --- %< --- %< ---
(defun org-clock-split-current-interval ()
   "If this is a CLOCK line, split its clock time interval into two.
t the current time interval be A--C; then this function interactively 
prompts for a time B (suggesting A as a default), and then replaces A--C 
by B--C and A--B.  The point is left on the line B--C, so that this line 
can, e.g., be moved to another entry."
   (interactive)
   (save-excursion
     ;; Part of the following code is copied from 
org-clock-update-time-maybe.
     ;; If this function becomes part of org-clock.el, some refactoring 
would be in order.
     (beginning-of-line 1)
     (skip-chars-forward " \t")
     (when (looking-at org-clock-string)
       (let ((re (concat "[ \t]*" org-clock-string
                         " *[[<][^]>]+[]>]-+[[<][^]>]+[]>]"
                         "\\(?:[ \t]*=>.*\\)?")))
(when (looking-at re)
         ;; duplicate current line (resulting in A--C newline A--C)
         (let ((current-line (thing-at-point 'line t)))
           (when (or (= 1 (forward-line 1)) (eq (point) (point-max)))
             (insert "\n"))
           (insert current-line))
         ;; interactively change start time of the later interval
         ;; (resulting in B--C newline A--C)
         ;; TODO when universal-argument is provided, we might 
alternatively offer changing the end time of the earlier interval, 
resulting in A--C newline A--B.
         (forward-line -2)
         ;; we currently assume that all timestamps in clock intervals 
are inactive
         (search-forward (concat org-clock-string " ["))
         ;; TODO call org-time-stamp with arguments that are conditional 
on whether an active or an inactive timestamp was found above
         (call-interactively 'org-time-stamp-inactive)
         ;; If there were a function that implemented the actual body of 
org-clock-update-time-maybe, we could call that function, as in this 
context we _know_ that we are on a CLOCK line.
         (org-clock-update-time-maybe)
         ;; copy changed time and also make it the end time of the 
earlier interval
         ;; (resulting in B--C newline A--B)
         (re-search-backward org-ts-regexp-both)
         (let ((ts (match-string 0)))
           (move-end-of-line 2)
           (when (re-search-backward org-ts-regexp-both nil t)
             (replace-match ts))
           (org-clock-update-time-maybe)))))))

(define-key org-mode-map (kbd "\C-cs") 'org-clock-split-current-interval)
--- %< --- %< --- %< --- %< --- %< --- %< --- %< --- %< --- %< --- %< ---

As I said in my previous email:

I would even be happy to contribute it to the codebase of org-mode (core 
or contrib); however in this case someone would have to point me to a 
fool-proof guide for how to do this.  I know that for contributing code 
I will have to sign some FSF copyright forms, and I know how to use git, 
but I don't know the exact org-mode specific steps of doing so.

Cheers,

Christoph

-- 
Dr. Christoph Lange, Enterprise Information Systems Department
Applied Computer Science @ University of Bonn; Fraunhofer IAIS
http://langec.wordpress.com/about, Skype duke4701

→ Semantic Publishing Challenge: Assessing the Quality of Scientific Output
   ESWC, 31 May–4 June 2014, Portorož, Slovenia. 
https://tinyurl.com/SPChallenge15
   Submission deadline 27 March (abstracts: 20 March)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Function that splits a CLOCK interval
  2015-03-31 16:59 Function that splits a CLOCK interval Christoph LANGE
@ 2015-04-01 10:37 ` Peter Frings
  2015-04-07 16:07   ` Christoph LANGE
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Frings @ 2015-04-01 10:37 UTC (permalink / raw)
  To: Christoph LANGE; +Cc: emacs-orgmode


On 31 Mar 2015, at 18:59, Christoph LANGE <math.semantic.web@gmail.com> wrote:

> I use org's clocking facility to clock all my working time.  I frequently find myself clocking time for a task T1, but later realize that I actually spent part of this time on some other task T2.
[snip]
> The following function now automates the task of splitting:

Fantastic, just what I needed! 

One little thing, though. When I interrupt the function with C-g at the prompt, the current line is already duplicated. It would by nice that C-g left the buffer unchanged.

But other than that, this is a godsend for sloppy time-loggers like me.

Cheers,
Peter.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Function that splits a CLOCK interval
  2015-04-01 10:37 ` Peter Frings
@ 2015-04-07 16:07   ` Christoph LANGE
  2015-04-08  3:50     ` Xavier Maillard
  0 siblings, 1 reply; 4+ messages in thread
From: Christoph LANGE @ 2015-04-07 16:07 UTC (permalink / raw)
  To: Peter Frings; +Cc: emacs-orgmode

Hi Peter,

Peter Frings on 2015-04-01 12:37:
>> The following function now automates the task of splitting:
>> …
>
> Fantastic, just what I needed!

thanks for your feedback!

I have now made a few improvements but not yet found time to get started 
with contributing the code to Worg, so one more email with the 
improvements.

> One little thing, though. When I interrupt the function with C-g at the prompt, the current line is already duplicated. It would by nice that C-g left the buffer unchanged.

Done, see code below.  Plus, the function now accepts a prefix argument 
and works with active time stamps.  When a prefix argument is given, the 
interactive editing of the timestamp uses C as a default before changing 
A--C into A--B B--C.

Cheers,

Christoph

--- %< --- %< --- %< --- %< --- %< --- %< --- %< --- %< --- %< --- %< ---

   (defun org-clock-split-current-interval (end-as-default)
     "If this is a CLOCK line, split its clock time interval into two.
Let the current time interval be A--C.  By default, this function 
interactively prompts for a time B (suggesting A as a default), and then 
replaces A--C by B--C and A--B.  When called with a prefix argument, the 
function uses C as a default for B.  The point is left on the later 
interval, so that this line can, e.g., be moved to another entry."
     (interactive "P")
     (save-excursion
       ;; Part of the following code is copied from 
org-clock-update-time-maybe.
       ;; If this function becomes part of org-clock.el, some 
refactoring would be in order.
       (beginning-of-line nil)
       (skip-chars-forward " \t")
       (when (looking-at org-clock-string)
         (beginning-of-line nil)
         (let ((re (concat "\\([ \t]*" org-clock-string " *\\)"
 
"\\([[<][^]>]+[]>]\\)\\(-+\\)\\([[<][^]>]+[]>]\\)"
                           "\\(?:[ \t]*=>.*\\)?")))
           (when (looking-at re)
             (let ((indentation (match-string 1))
                   (start (match-string 2))
                   (to (match-string 3))
                   (end (match-string 4))
                   (use-start-as-default (equal end-as-default nil)))
               ;; interactively change A--C to B--C,
               ;; or (given prefix argument) to A--B, …
               (re-search-forward (concat org-clock-string " \\([[<]\\)"))
               (when (not use-start-as-default) (re-search-forward 
"\\([[<]\\)"))
               ;; … respecting whether A or C is an active or an 
inactive timestamp
               (call-interactively (if (equal (match-string 1) "<")
                                         'org-time-stamp
                                       'org-time-stamp-inactive))
               ;; If there were a function that implemented the actual 
body of org-clock-update-time-maybe, we could call that function, as in 
this context we _know_ that we are on a CLOCK line.
               (org-clock-update-time-maybe)
               ;; copy changed time B
               (re-search-backward org-ts-regexp-both)
               (let ((middle (match-string 0)))
                 ;; insert A--B below, or (given prefix argument) insert 
B--C above
                 (end-of-line (if use-start-as-default 1 0))
                 (insert "\n" indentation
                         (if use-start-as-default start middle)
                         to
                         (if use-start-as-default middle end))
                 (org-clock-update-time-maybe))))))))

-- 
Dr. Christoph Lange, Enterprise Information Systems Department
Applied Computer Science @ University of Bonn; Fraunhofer IAIS
http://langec.wordpress.com/about, Skype duke4701

→ Semantic Publishing Challenge: Assessing the Quality of Scientific Output
   ESWC, 31 May–4 June 2014, Portorož, Slovenia. 
https://tinyurl.com/SPChallenge15
   Submission deadline 27 March (abstracts: 20 March)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Function that splits a CLOCK interval
  2015-04-07 16:07   ` Christoph LANGE
@ 2015-04-08  3:50     ` Xavier Maillard
  0 siblings, 0 replies; 4+ messages in thread
From: Xavier Maillard @ 2015-04-08  3:50 UTC (permalink / raw)
  To: Christoph LANGE; +Cc: emacs-orgmode


Christoph LANGE <math.semantic.web@gmail.com> writes:

> Hi Peter,
>
> Peter Frings on 2015-04-01 12:37:
>>> The following function now automates the task of splitting:
>>> …
>>
>> Fantastic, just what I needed!
>
> thanks for your feedback!
>
> I have now made a few improvements but not yet found time to get started
> with contributing the code to Worg, so one more email with the
> improvements.

This is really useful for me too. Thank you.

-- Xavier.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-04-08  3:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-31 16:59 Function that splits a CLOCK interval Christoph LANGE
2015-04-01 10:37 ` Peter Frings
2015-04-07 16:07   ` Christoph LANGE
2015-04-08  3:50     ` Xavier Maillard

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).