emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* RFC: sort subtree by most recently clocked in and most time spend on
@ 2009-11-23 12:38 Benjamin Andresen
  0 siblings, 0 replies; only message in thread
From: Benjamin Andresen @ 2009-11-23 12:38 UTC (permalink / raw)
  To: emacs-orgmode

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-11-23 12:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-23 12:38 RFC: sort subtree by most recently clocked in and most time spend on Benjamin Andresen

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