emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [O] Function that splits a CLOCK interval


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

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)



reply via email to

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