emacs-orgmode
[Top][All Lists]
Advanced

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

[Orgmode] RFC: sort subtree by most recently clocked in and most time sp


From: Benjamin Andresen
Subject: [Orgmode] RFC: sort subtree by most recently clocked in and most time spend on
Date: Mon, 23 Nov 2009 13:38:16 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

Hey there,

I wrote a bit of code to do the things in the subject.

I'm interested if someone has a better way to deal with the subtree
copying thing I'm doing. I'm not really happy with using the kill-ring
like that.

`org-narrow-subtree' won't work, because `save-excursion' doesn't revert
it to the previous state and the org-sort already uses it.

Thanks in advance for looking over it!

(defun ba/org-heading-clock-times ()
  "Return alist of clocktimes from current heading."
  (let* ((re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
                     org-clock-string
                     "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ 
\t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
         t1 ts te tsf tef dt
         results)
    (save-excursion
      ;; shut up about copying
      (flet ((message (&rest ignored) nil))
        (org-copy-subtree))
      (with-temp-buffer
        (yank)
        (while (re-search-backward re nil t)
          (when (match-end 2)
            ;; Two time stamps
             (setq ts (match-string-no-properties 2)
                   te (match-string-no-properties 3)
                   tsf (org-float-time (apply 'encode-time 
(org-parse-time-string ts)))
                   tef (org-float-time (apply 'encode-time 
(org-parse-time-string te)))
                   dt (- tef tsf)
                   t1 (floor (/ dt 60)))
             (add-to-list 'results `(,ts . ,t1))))))
    results))


(defun ba/org-sort-most-time-spend ()
  (let ((org-ts-w/mins (ba/org-heading-clock-times)))
    (- (apply '+ (mapcar '(lambda (a)
                           (cdr a))
                         org-ts-w/mins)))))

(defun ba/org-sort-most-recently-clocked ()
  (let ((org-ts-w/mins (ba/org-heading-clock-times)))
    (if (not org-ts-w/mins)
        (org-float-time)
        (- (org-float-time)
           (org-float-time (apply 'encode-time
                                  (org-parse-time-string (caar 
org-ts-w/mins))))))))

br,
benny




reply via email to

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