ADD giant child->parent master linker function

This commit is contained in:
Nathan Dwarshuis 2022-04-10 17:02:48 -04:00
parent 8b30bc8e49
commit d69735c6c7
1 changed files with 207 additions and 132 deletions

View File

@ -167,6 +167,10 @@
(-let (((y m _) date)) (-let (((y m _) date))
(list y (1+ (/ m 3))))) (list y (1+ (/ m 3)))))
(defun org-x-dag-date-to-quarter-start (date)
(->> (org-x-dag-date-to-quarter)
(org-x-dag-quarter-to-date)))
(defun org-x-dag-shift-quarter (quarter n unit) (defun org-x-dag-shift-quarter (quarter n unit)
(-let (((y q) quarter)) (-let (((y q) quarter))
(pcase unit (pcase unit
@ -638,8 +642,8 @@ be uncommitted if it is also incubated."
(defun org-x-dag->qtp-ids () (defun org-x-dag->qtp-ids ()
(org-x-dag-file->ids (org-x-dag->planning-file :quarterly))) (org-x-dag-file->ids (org-x-dag->planning-file :quarterly)))
;; (defun org-x-dag->wkp-ids () (defun org-x-dag->wkp-ids ()
;; (org-x-dag-file->ids (org-x-get-weekly-plan-file))) (org-x-dag-file->ids (org-x-dag->planning-file :weekly)))
(defun org-x-dag-filter-ids-tags (tags ids) (defun org-x-dag-filter-ids-tags (tags ids)
(--filter (-intersection (org-x-dag-id->tags nil it) tags) ids)) (--filter (-intersection (org-x-dag-id->tags nil it) tags) ids))
@ -651,8 +655,8 @@ be uncommitted if it is also incubated."
(defun org-x-dag-weekly-tags-to-date (tags) (defun org-x-dag-weekly-tags-to-date (tags)
(-let (((y w) (reverse tags))) (-let (((y w) (reverse tags)))
(org-x-dag-week-number-to-date (list (org-x-dag-tag-to-year y) (org-x-dag-week-number-to-date (org-x-dag-tag-to-year y)
(org-x-dag-tag-to-week w))))) (org-x-dag-tag-to-week w))))
(defun org-x-dag-daily-tags-to-date (tags) (defun org-x-dag-daily-tags-to-date (tags)
(-let (((y m d) (reverse tags))) (-let (((y m d) (reverse tags)))
@ -693,7 +697,7 @@ be uncommitted if it is also incubated."
(defun org-x-dag-date->wkp-ids (date) (defun org-x-dag-date->wkp-ids (date)
(org-x-dag-date->tagged-ids (org-x-dag->wkp-ids) (org-x-dag-date->tagged-ids (org-x-dag->wkp-ids)
#'org-x-dag-week-tags-to-date #'org-x-dag-weekly-tags-to-date
date)) date))
;; (defun org-x-dag->qtp-current-ids () ;; (defun org-x-dag->qtp-current-ids ()
@ -3432,7 +3436,7 @@ except it ignores inactive timestamps."
(apply #'org-ml-build-plain-list) (apply #'org-ml-build-plain-list)
(org-ml-build-drawer org-x-drwr-parent-links))) (org-ml-build-drawer org-x-drwr-parent-links)))
(defun org-x-dag-section-get-parent-links (children) (defun org-x-dag-drawer-get-parent-links (drawer)
(cl-flet (cl-flet
((parse-item ((parse-item
(item) (item)
@ -3441,23 +3445,27 @@ except it ignores inactive timestamps."
(equal (org-ml-get-property :type first) "id")) (equal (org-ml-get-property :type first) "id"))
(org-ml-get-property :path first) (org-ml-get-property :path first)
(error "Invalid link node: %S" first))))) (error "Invalid link node: %S" first)))))
(-when-let (first (->> children (-when-let (first (car (org-ml-get-children drawer)))
(--find (org-x--is-drawer-with-name org-x-drwr-parent-links it))
(org-ml-get-children)
(car)))
(if (org-ml-is-type 'plain-list first) (if (org-ml-is-type 'plain-list first)
(->> (org-ml-get-children first) (->> (org-ml-get-children first)
(-map #'parse-item)) (-map #'parse-item))
(error "Invalid parent link drawer"))))) (error "Invalid parent link drawer")))))
(defun org-x-dag-drawer-set-parent-links (ids drawer)
(-when-let (pl (-some->> (-map #'org-x-dag-id->link-item ids)
(apply #'org-ml-build-plain-list)))
(org-ml-set-children (list pl) drawer)))
(defun org-x-dag-section-get-parent-links (children)
(->> (--find (org-x--is-drawer-with-name org-x-drwr-parent-links it) children)
(org-x-dag-drawer-get-parent-links)))
(defun org-x-dag-section-set-parent-links (ids children) (defun org-x-dag-section-set-parent-links (ids children)
(-if-let (i (--find-index (org-x--is-drawer-with-name org-x-drwr-parent-links it) (-if-let (i (--find-index (org-x--is-drawer-with-name org-x-drwr-parent-links it)
children)) children))
(let ((d (nth i children))) (-if-let (d (org-x-dag-drawer-set-parent-links ids (nth i children)))
(-if-let (pl (-some->> (-map #'org-x-dag-id->link-item ids) (-replace-at i d children)
(apply #'org-ml-build-plain-list))) (-remove-at i children))
(-replace-at i (org-ml-set-children (list pl) d) children)
(-remove-at i children)))
(if ids (cons (org-x-dag-build-parent-link-drawer ids) children) children))) (if ids (cons (org-x-dag-build-parent-link-drawer ids) children) children)))
(defmacro org-x-dag-section-map-parent-links* (form children) (defmacro org-x-dag-section-map-parent-links* (form children)
@ -3474,6 +3482,29 @@ except it ignores inactive timestamps."
(--remove-first (equal it id) it) (--remove-first (equal it id) it)
children)) children))
(defun org-x-dag-tl-section-get-parent-links (section)
(->> (org-ml-get-children section)
(org-x-dag-section-get-parent-links)))
(defun org-x-dag-tl-section-set-parent-links (ids section)
(org-ml-map-children*
(org-x-dag-section-set-parent-links ids it)
section))
(defmacro org-x-dag-tl-section-map-parent-links* (form children)
(let ((s (make-symbol "--section")))
`(let* ((,s ,children)
(it (org-x-dag-tl-section-get-parent-links ,s)))
(org-x-dag-tl-section-set-parent-links ,form ,s))))
(defun org-x-dag-tl-section-add-parent-link (id section)
(org-x-dag-tl-section-map-parent-links* (cons id it) section))
(defun org-x-dag-tl-section-remove-parent-link (id section)
(org-x-dag-tl-section-map-parent-links*
(--remove-first (equal it id) it)
section))
(defun org-x-dag-headline-get-parent-links (headline) (defun org-x-dag-headline-get-parent-links (headline)
(->> headline (->> headline
(org-ml-headline-get-contents (org-x-logbook-config)) (org-ml-headline-get-contents (org-x-logbook-config))
@ -3567,7 +3598,7 @@ except it ignores inactive timestamps."
(let ((path (->> (org-x-dag-id->buffer-lineage id) (let ((path (->> (org-x-dag-id->buffer-lineage id)
(-map #'org-x-dag-id->title) (-map #'org-x-dag-id->title)
(s-join "/") (s-join "/")
(s-prepend)))) (s-prepend "/"))))
(if category? (if category?
(format "%s:%s" (org-x-dag-id->hl-meta-prop id :category) path) (format "%s:%s" (org-x-dag-id->hl-meta-prop id :category) path)
path))) path)))
@ -3601,7 +3632,7 @@ except it ignores inactive timestamps."
`(,line :id ,id :presentp ,presentp))) `(,line :id ,id :presentp ,presentp)))
(choose-child-id (choose-child-id
(this-id) (this-id)
(-if-let (collection (->> (funcall child-id-fun it) (-if-let (collection (->> (funcall child-id-fun)
;; (-filter #'is-valid-node) ;; (-filter #'is-valid-node)
(--map (to-menu-line this-id it)))) (--map (to-menu-line this-id it))))
(-let* (((&plist :id :presentp) (-let* (((&plist :id :presentp)
@ -3619,11 +3650,9 @@ except it ignores inactive timestamps."
(funcall update-fun this-id it))) (funcall update-fun this-id it)))
(message fmt (org-x-dag-id->title id) (org-x-dag-id->title this-id))) (message fmt (org-x-dag-id->title id) (org-x-dag-id->title this-id)))
(message "No children available")))) (message "No children available"))))
(if (org-on-heading-p) (either-from (funcall parent-id-fun)
(either-from (funcall parent-id-fun) (message it)
(message it) (choose-child-id it))))
(choose-child-id it))
(message "Not on a headline"))))
(defun org-x-dag-link-parent-to-child () (defun org-x-dag-link-parent-to-child ()
(interactive) (interactive)
@ -3658,7 +3687,7 @@ except it ignores inactive timestamps."
;; child id functions ;; child id functions
(action-qtp-getter (action-qtp-getter
(_) ()
(let ((action (->> (org-x-dag->action-files) (let ((action (->> (org-x-dag->action-files)
(org-x-dag-files->ids) (org-x-dag-files->ids)
;; TODO could also remove DONE/CANC and things ;; TODO could also remove DONE/CANC and things
@ -3670,7 +3699,7 @@ except it ignores inactive timestamps."
(org-x-dag-date->qtp-ids)))) (org-x-dag-date->qtp-ids))))
(append epg action qtp))) (append epg action qtp)))
(svg-action-getter (svg-action-getter
(_) ()
(->> (org-x-dag->action-files) (->> (org-x-dag->action-files)
(org-x-dag-files->ids) (org-x-dag-files->ids)
;; TODO could also remove DONE/CANC and things ;; TODO could also remove DONE/CANC and things
@ -3678,32 +3707,28 @@ except it ignores inactive timestamps."
(--remove (and (org-x-dag-id->ns-key :committed it) (--remove (and (org-x-dag-id->ns-key :committed it)
(not (org-x-dag-id->ns-key :survivalp it)))))) (not (org-x-dag-id->ns-key :survivalp it))))))
(epg-action-qtp-getter (epg-action-qtp-getter
(id) ()
`(,@(org-x-dag->epg-ids) ,@(funcall action-qtp-getter id))) `(,@(org-x-dag->epg-ids) ,@(action-qtp-getter)))
(wkp-getter (wkp-getter
(_) ()
(-> (plist-get org-x-dag :selected-date) (-> (plist-get org-x-dag :selected-date)
(org-x-dag-date-to-week-number) (org-x-dag-date-to-week-number)
(org-x-dag-week-number-to-date)
(org-x-dag-date->wkp-ids))) (org-x-dag-date->wkp-ids)))
(dlp-getter (dlp-getter
(_) ()
(-> (plist-get org-x-dag :selected-date) (-> (plist-get org-x-dag :selected-date)
(org-x-dag-date->dlp-ids))) (org-x-dag-date->dlp-ids)))
(add-group
(s id)
(let ((g (->> (org-x-dag-id->hl-meta-prop id :group)
(org-x-dag-group-code))))
(format "%s | %s" g s)))
;; formatters ;; formatters
(toplevel-formatter (toplevel-formatter
(id) (id)
(let ((title (if (eq group :quarterly) (let* ((group (org-x-dag-id->hl-meta-prop id :group))
(org-x-dag-id->title id) (s (if (eq group :quarterly)
(org-x-dag-id->path (eq group :action) id)))) (org-x-dag-id->title id)
(add-group title id))) (org-x-dag-id->path (eq group :action) id)))
(toplevel-formatter (g (org-x-dag-group-code group)))
(format "%s | %s" g s)))
(svg-formatter
(id) (id)
(org-x-dag-id->path t id)) (org-x-dag-id->path t id))
(plan-formatter (plan-formatter
@ -3751,102 +3776,152 @@ except it ignores inactive timestamps."
;; functions to retrieve a parent headline id and add it to the current ;; functions to retrieve a parent headline id and add it to the current
;; headline's (or file's) parent links ;; headline's (or file's) parent links
(defun org-x-dag-read-id (ids cur-ids) (defun org-x-dag--link-child-to-parent (parse-fun parent-id-fun fmt-fun)
(cl-flet (cl-flet*
((make-cell (;; (is-valid-node
(id type) ;; (id)
(-let ((title (->> (org-x-dag-id->parents id) ;; (-some-> (org-x-dag-id->ns id)
(--map (org-x-dag-id->title it)) ;; (either-is-right-p)))
(s-join "/") (to-menu-line
(s-prepend "/"))) (child-ids id)
(group (pcase (org-x-dag-id->hl-meta-prop id :group) (let* ((presentp (member id child-ids))
(:endpoint "EPG") (title (funcall fmt-fun id))
(:lifetime "LTG"))) (line (format "%c %s" (if presentp ?* ?\s) title)))
(presentp (not (eq type 'toadd))) `(,line :id ,id :presentp ,presentp)))
(prefix (pcase type (update
(`present ?*) (add-fun rem-fun child-id-fun what node)
(`noexist ?!) (let ((child-ids (funcall child-id-fun node)))
(`toadd ?\s)))) (-if-let (collection (->> (funcall parent-id-fun)
(list (format "%c %s | %s" prefix group title) ;; (-filter #'is-valid-node)
:id id (--map (to-menu-line child-ids it))))
:title title (-let* (((&plist :id :presentp)
:presentp presentp)))) (-> (completing-read "Parent: " collection nil t)
(let* ((present (--map (list it 'present) (-intersection cur-ids ids))) (alist-get collection nil nil #'equal)))
(noexist (--map (list it 'noexist) (-difference cur-ids ids))) (title (org-x-dag-id->title id))
(toadd (--map (list it 'toadd) (-difference ids cur-ids))) ((verb fun) (if presentp
(mapper (->> (append present noexist toadd) `("removed" ,rem-fun)
(--map (apply #'make-cell it)) `("added" ,add-fun))))
(--sort (plist-get (cdr it) :presentp))))) (org-ml~update nil fun node)
(alist-get (completing-read "Node: " mapper) mapper nil nil #'equal)))) (message "Successfully %s '%s' from %s" verb title what))
(message "No parents available"))))
(defun org-x-dag-this-headline-add-link (toplevel-allowed? legal-files msg ids) (update-headline
(cl-flet (hl)
((update-nodes (update #'org-x-dag-headline-add-parent-link
(link-getter remover adder ids children) #'org-x-dag-headline-remove-parent-link
(-let* ((cur-ids (funcall link-getter children)) #'org-x-dag-headline-get-parent-links
((&plist :id i :presentp p) (org-x-dag-read-id ids cur-ids))) "current headline"
(funcall (if p remover adder) i children)))) hl))
(if (not (member (buffer-file-name) legal-files)) (message "Not in %s" msg) (update-tl-section
(if (org-before-first-heading-p) (sec)
(if (not toplevel-allowed?) (message "Cannot set toplevel drawer.") (update #'org-x-dag-tl-section-add-parent-link
(org-ml~update-this-section* nil #'org-x-dag-tl-section-remove-parent-link
;; TODO org-ml shouldn't require this, just map the children #'org-x-dag-tl-section-get-parent-links
;; directly "toplevel section"
(org-ml-map-children* sec)))
(update-nodes #'org-x-dag-section-get-parent-links (either-from (funcall parse-fun)
#'org-x-dag-section-remove-parent-link (message it)
#'org-x-dag-section-add-parent-link (if it (update-headline it) (update-tl-section it)))))
ids it)
it)))
(org-ml~update-this-headline* nil
(update-nodes #'org-x-dag-headline-get-parent-links
#'org-x-dag-headline-remove-parent-link
#'org-x-dag-headline-add-parent-link
ids it))))))
(defun org-x-dag-link-ltg-to-epg ()
(interactive)
(let ((ids (org-x-dag->ltg-ids))
(legal (list (org-x-get-endpoint-goal-file))))
(org-x-dag-this-headline-add-link nil legal "endpoint goal file" ids)))
(defun org-x-dag-link-qtp-to-goal ()
(interactive)
(let ((ids (->> (list (org-x-dag->goal-file :endpoint)
(org-x-dag->goal-file :lifetime))
(org-x-dag-files->ids)
(-filter #'org-x-dag-id->is-buffer-leaf-p)))
(legal (list (org-x-dag->planning-file :quarterly))))
(org-x-dag-this-headline-add-link nil legal "quarterly plan file" ids)))
(defun org-x-dag-link-action-to-goal ()
(interactive)
(let ((ids (append (org-x-dag->ltg-ids) (org-x-dag->epg-ids)))
(legal (org-x-dag->action-files)))
;; TODO this won't work on the toplevel section
(org-x-dag-this-headline-add-link t legal "an action file" ids)))
(defun org-x-dag-link-dlp-to-action ()
(interactive)
;; TODO there are lots of ids here that I likely don't need
;; TODO show the path
(let ((ids (->> (org-x-dag->action-files)
(org-x-dag-files->ids)
(--remove (org-x-dag-id->ns-key :survivalp it))))
(legal (list (org-x-dag->planning-file :daily))))
(org-x-dag-this-headline-add-link t legal "the daily metablock file" ids)))
(defun org-x-dag-link-child-to-parent () (defun org-x-dag-link-child-to-parent ()
(interactive) (interactive)
(let ((f (buffer-file-name))) (cl-flet*
(cond ((parse-hl
((equal f (org-x-dag->goal-file :endpoint))) ()
((member f (org-x-dag->action-files))) ;; TODO could also test for DONE/CANC nodes since those are useless
((equal f (org-x-dag->planning-file :quarterly))) (-if-let (hl (org-ml-parse-this-headline))
((equal f (org-x-dag->planning-file :weekly))) (if (->> (org-x-dag-headline-get-id hl)
((equal f (org-x-dag->planning-file :daily))) (org-x-dag-id->todo))
(t (either :right hl)
(message "Cannot link parent from child in current file"))))) (either :left "Headline is not a node"))
(either :left "Not on headline")))
(parse-hl-sec
()
(if (org-before-first-heading-p)
(->> (org-ml-parse-this-toplevel-section)
(either :right))
(parse-hl)))
;; parent id getters
(ltg-getter
()
(->> (org-x-dag->goal-file :lifetime)
(org-x-dag-file->ids)))
(tlg-getter
()
(->> (org-x-dag->goal-file :endpoint)
(org-x-dag-file->ids)
(append (ltg-getter))))
(goal-getter
()
(->> (org-x-dag->goal-file :survival)
(org-x-dag-file->ids)
(append (tlg-getter))))
(wkp-getter
()
(->> (plist-get org-x-dag :selected-date)
(org-x-dag-date-to-quarter-start)
(org-x-dag-date->qtp-ids)))
(dlp-getter
()
(let ((wkp-ids (->> (plist-get org-x-dag :selected-date)
(org-x-dag-date-to-week-start)
(org-x-dag-date->wkp-ids)))
(action-ids (->> (org-x-dag->action-files)
(org-x-dag-files->ids))))
(append wkp-ids action-ids)))
;; formatters
(ltg-formatter
(id)
(org-x-dag-id->path t id))
(goal-formatter
(id)
(let* ((group (org-x-dag-id->hl-meta-prop id :group))
(s (org-x-dag-id->path nil id))
(g (org-x-dag-group-code group)))
(format "%s | %s" g s)))
(plan-formatter
(id)
(org-x-dag-id->title id))
(dlp-formatter
(id)
(let* ((group (org-x-dag-id->hl-meta-prop id :group))
(s (if (eq group :weekly)
(org-x-dag-id->title id)
(org-x-dag-id->path (eq group :action) id)))
(g (org-x-dag-group-code group)))
(format "%s | %s" g s))))
(org-x-dag-sync)
(let ((f (buffer-file-name)))
(cond
((equal f (org-x-dag->goal-file :endpoint))
(org-x-dag--link-child-to-parent
#'parse-hl
#'ltg-getter
#'ltg-formatter))
((member f (org-x-dag->action-files))
(org-x-dag--link-child-to-parent
#'parse-hl-sec
#'goal-getter
#'goal-formatter))
((equal f (org-x-dag->planning-file :quarterly))
(org-x-dag--link-child-to-parent
#'parse-hl
#'tlg-getter
#'goal-formatter))
((equal f (org-x-dag->planning-file :weekly))
(org-x-dag--link-child-to-parent
#'parse-hl
#'wkp-getter
#'plan-formatter))
((equal f (org-x-dag->planning-file :daily))
(org-x-dag--link-child-to-parent
#'parse-hl
#'dlp-getter
#'dlp-formatter))
(t
(message "Cannot link parent from child in current file"))))))
;; add nodes ;; add nodes