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@gnu.org

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

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