emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Christoph LANGE <math.semantic.web@gmail.com>
To: "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: Function that splits a CLOCK interval
Date: Tue, 31 Mar 2015 18:59:55 +0200	[thread overview]
Message-ID: <551AD28B.2060405@gmail.com> (raw)

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


    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."
     ;; Part of the following code is copied from 
     ;; 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.
         ;; 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))

(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.



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. 
   Submission deadline 27 March (abstracts: 20 March)

             reply	other threads:[~2015-03-31 17:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-31 16:59 Christoph LANGE [this message]
2015-04-01 10:37 ` Function that splits a CLOCK interval Peter Frings
2015-04-07 16:07   ` Christoph LANGE
2015-04-08  3:50     ` Xavier Maillard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=551AD28B.2060405@gmail.com \
    --to=math.semantic.web@gmail.com \
    --cc=emacs-orgmode@gnu.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


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).