emacs-orgmode
[Top][All Lists]
Advanced

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

[O] Function that splits a CLOCK interval


From: Christoph LANGE
Subject: [O] Function that splits a CLOCK interval
Date: Tue, 31 Mar 2015 18:59:55 +0200
User-agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0

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)



reply via email to

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