diff --git a/init.el b/init.el index df5f301..ade1ddb 100644 --- a/init.el +++ b/init.el @@ -686,6 +686,55 @@ _h_ _l_ _o_k _y_ank :group 'personal-settings :type 'string) +;; Helper Functions for Clocking + +(defun db/find-parent-task () + ;; http://doc.norang.ca/org-mode.html#Clocking + "Return point of the nearest parent task, and NIL if no such task exists." + (save-mark-and-excursion + (save-restriction + (widen) + (let ((parent-task nil)) + (or (org-at-heading-p) + (org-back-to-heading t)) + (while (and (not parent-task) + (org-up-heading-safe)) + (let ((tags (nth 5 (org-heading-components)))) + (unless (and tags (member "NOP" (split-string tags ":" t))) + (setq parent-task (point))))) + parent-task)))) + +(defun db/ensure-running-clock () + "Clocks in into the parent task, if it exists, or the default task." + (when (and (not org-clock-clocking-in) + (not org-clock-resolving-clocks-due-to-idleness)) + (let ((parent-task (db/find-parent-task))) + (save-mark-and-excursion + (cond + (parent-task + ;; found parent task + (org-with-point-at parent-task + (org-clock-in))) + ((and (markerp org-clock-default-task) + (marker-buffer org-clock-default-task)) + ;; default task is set + (org-with-point-at org-clock-default-task + (org-clock-in))) + (t + (org-clock-in '(4)))))))) + +(defvar db/org-clock-current-task-file + "~/.org-current-task") + +(defun db/org-current-task () + "Format currently clocked task and write it to +`db/org-clock-current-task-file'." + (with-temp-file db/org-clock-current-task-file + (let ((clock-buffer (marker-buffer org-clock-marker))) + (if (null clock-buffer) + (insert "No running clock") + (insert org-clock-heading))))) + (use-package org-clock :init (setq org-clock-history-length 23 org-clock-in-resume t @@ -722,58 +771,9 @@ _h_ _l_ _o_k _y_ank (org-clock-mark-default-task))))) ;; Clock in default task if no other task is given - - (defun db/find-parent-task () - ;; http://doc.norang.ca/org-mode.html#Clocking - "Return point of the nearest parent task, and NIL if no such task exists." - (save-mark-and-excursion - (save-restriction - (widen) - (let ((parent-task nil)) - (or (org-at-heading-p) - (org-back-to-heading t)) - (while (and (not parent-task) - (org-up-heading-safe)) - (let ((tags (nth 5 (org-heading-components)))) - (unless (and tags (member "NOP" (split-string tags ":" t))) - (setq parent-task (point))))) - parent-task)))) - - (defun db/ensure-running-clock () - "Clocks in into the parent task, if it exists, or the default task." - (when (and (not org-clock-clocking-in) - (not org-clock-resolving-clocks-due-to-idleness)) - (let ((parent-task (db/find-parent-task))) - (save-mark-and-excursion - (cond - (parent-task - ;; found parent task - (org-with-point-at parent-task - (org-clock-in))) - ((and (markerp org-clock-default-task) - (marker-buffer org-clock-default-task)) - ;; default task is set - (org-with-point-at org-clock-default-task - (org-clock-in))) - (t - (org-clock-in '(4)))))))) - (add-hook 'org-clock-out-hook #'db/ensure-running-clock 'append) ;; Communicate the currently clocked in task to the outside world - - (defvar db/org-clock-current-task-file - "~/.org-current-task") - - (defun db/org-current-task () - "Format currently clocked task and write it to -`db/org-clock-current-task-file'." - (with-temp-file db/org-clock-current-task-file - (let ((clock-buffer (marker-buffer org-clock-marker))) - (if (null clock-buffer) - (insert "No running clock") - (insert org-clock-heading))))) - (add-hook 'org-clock-in-hook #'db/org-current-task))) (use-package ox-icalendar