Skip to content

Instantly share code, notes, and snippets.

@ironchicken
Forked from ffevotte/clocktable-by-tag.el
Last active June 16, 2022 12:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ironchicken/6b5424bc2024b3d0a58a8a130f73c2ee to your computer and use it in GitHub Desktop.
Save ironchicken/6b5424bc2024b3d0a58a8a130f73c2ee to your computer and use it in GitHub Desktop.
Emacs org-mode dynamic block similar to clocktable, but grouped by tagSee: http://stackoverflow.com/q/17353591/1225607
(defun clocktable-by-tag/shift-cell (n)
(let ((str ""))
(dotimes (i n)
(setq str (concat str "| ")))
str))
(defun clocktable-by-tag/insert-tag (params)
(let ((tag (plist-get params :tags)))
(insert "|--\n")
(insert (format "| %s | *Tag time* |\n" tag))
(let ((total 0))
(mapcar
(lambda (file)
(let ((clock-data (with-current-buffer (find-file-noselect file)
(org-clock-get-table-data (buffer-name) params))))
(when (> (nth 1 clock-data) 0)
(setq total (+ total (nth 1 clock-data)))
(insert (format "| | File *%s* | %.2f |\n"
(file-name-nondirectory file)
(/ (nth 1 clock-data) 60.0)))
(dolist (entry (nth 2 clock-data))
(insert (format "| | . %s%s | %s %.2f |\n"
(org-clocktable-indent-string (nth 0 entry))
(nth 1 entry)
(clocktable-by-tag/shift-cell (nth 0 entry))
(/ (nth 4 entry) 60.0)))))))
(org-agenda-files))
(save-excursion
(re-search-backward "*Tag time*")
(org-table-next-field)
(org-table-blank-field)
(insert (format "*%.2f*" (/ total 60.0)))))
(org-table-align)))
(defun org-dblock-write:clocktable-by-tag (params)
(insert "| Tag | Headline | Time (h) |\n")
(insert "| | | <r> |\n")
(let ((tags (plist-get params :tags)))
(mapcar (lambda (tag)
(clocktable-by-tag/insert-tag (plist-put (plist-put params :match tag) :tags tag)))
tags)))
@philohistoria
Copy link

Anyway to sort the table by the time on tag? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment