-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-linked-tasks.el
148 lines (117 loc) · 5.12 KB
/
org-linked-tasks.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;;; org-linked-tasks.el --- `org-mode' property for linking tasks together -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Mikhail Skorzhisnkii
;; Author: Mikhail Skorzhisnkii <mskorzhinskiy@eml.cc>
;; Created: August 15, 2020
;; Version: 0.1.0
;; Keywords:
;; Homepage: https://github.com/mskorzhinskiy/org-linked-tasks
;; Package-Requires: ((emacs "26.1") (org "9.0") (org-ql "0.5-pre"))
;; This file is not part of GNU Emacs.
;;; Commentary:
;; `org-linked-tasks' is implements special `org-mode' property to link
;; headlines together based on their IDs.
;;; License
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Code:
;;;; Requirements
(require 'org-id)
(require 'org-ql)
;;;; Customization
(defgroup org-linked-tasks nil
"Customisation for `org-linked-tasks'"
:group 'org
:link '(url-link "https://github.com/mskorzhinskiy/org-linked-tasks"))
(defcustom org-linked-tasks-files #'org-agenda-files
"Files to use when doing searched for linked tasks.
Function: accept no arguments, should return list of `org-mode' files."
:group 'org-linked-tasks
:type 'function)
(defcustom org-linked-tasks-property "LINKED"
"Property name to be used for linked tasks."
:group 'org-linked-tasks
:type 'string)
(defcustom org-linked-tasks-action #'org-linked-tasks-schedule-and-next
"What to do when linked all linked tasks are finally done.
Function: accept to arguments, called with pointer and buffer set
on parrent task."
:group 'org-linked-tasks
:type 'function)
;;;; Functions
(defun org-linked-tasks-schedule-and-next ()
"Schedule and switch headline to NEXT."
(org-schedule nil (current-time))
(org-todo "NEXT"))
(defun org-linked-tasks/schedule-and-mark-next-trigger (change-plist)
"Hook function for `org-trigger-hook'.
CHANGE-PLIST is a property list with TODO state changes.
Schedule for today and mark parent task as NEXT if this is the
last item."
(when (member (plist-get change-plist :to) org-done-keywords)
(let ((parent-id (org-entry-get (point) org-linked-tasks-property)))
(when parent-id
(org-delete-property org-linked-tasks-property)
(let ((entries (org-ql-select (funcall org-linked-tasks-files)
`(and (property ,org-linked-tasks-property ,parent-id)
(not (done))))))
(if (= 0 (length entries))
(let ((marker (org-id-find parent-id 'marker)))
(with-current-buffer (marker-buffer marker)
(goto-char (marker-position marker))
(funcall org-linked-tasks-action)))))))))
(defun org-linked-tasks/check-blocked-state (change-plist)
"Hook function for `org-blocker-hook'.
CHANGE-PLIST is a property list with TODO state changes.
Check blocked state of the task based on its ID."
(if (member (plist-get change-plist :to) org-done-keywords)
(let* ((id (org-id-get))
(headings (when id
(org-ql-select (funcall org-linked-tasks-files)
`(property ,org-linked-tasks-property ,id)
:action (lambda () (org-get-heading))))))
(if headings
(let* ((first-heading (pop headings))
(others-count (length headings)))
(setq org-block-entry-blocking
(if (> others-count 0)
(format "%s and %s others" first-heading others-count)
first-heading))
;; Blocked: QL search returned a list
nil)
;; Not blocked: QL search returned nothing
t))
;; Not blocked: Changing to non-done is always possible
t))
(defun org-linked-tasks/show-linked-tasks ()
"Helper function to show all tasks that are blocking this task."
(interactive)
(let ((id (org-id-get)))
(if id
(org-ql-search (funcall org-linked-tasks-files)
`(property ,org-linked-tasks-property ,id)
:title (concat "Linked for: " (org-get-heading t)))
(message "No ID stored in this task!"))))
;;;###autoload
(defun org-linked-tasks-load ()
"Install all required hooks for this package to operate."
(add-hook #'org-trigger-hook
#'org-linked-tasks/schedule-and-mark-next-trigger)
(add-hook #'org-blocker-hook
#'org-linked-tasks/check-blocked-state))
;;;###autoload
(defun org-linked-tasks-unload ()
"Uninstall all hooks."
(remove-hook #'org-trigger-hook
#'org-linked-tasks/schedule-and-mark-next-trigger)
(remove-hook #'org-blocker-hook
#'org-linked-tasks/check-blocked-state))
(provide 'org-linked-tasks)
;;; org-linked-tasks.el ends here