-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkm2-tasks.el
141 lines (122 loc) · 8.06 KB
/
pkm2-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
;;; pkm2-tasks.el --- Description -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2023 Jinder
;;
;; Author: Jinder <jinder1s@gmail.com>
;; Maintainer: Jinder <jinder1s@gmail.com>
;; Created: December 08, 2023
;; Modified: December 08, 2023
;; Version: 0.0.1
;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp
;; Homepage: https://github.com/msingh15/pkm2-tasks
;; Package-Requires: ((emacs "24.3"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Description
;;
;;; Code:
(require 'pkm-new-core)
(require 'pkm-hammerspoon)
(defvar pkm2-task-status-types (list "TODO" "DOING" "DONE" "HOLD" "KILL"))
(defvar pkm2-tasks-status-face-plist '(TODO org-todo DOING org-todo DONE org-done KILL +org-todo-cancel HOLD +org-todo-onhold ))
(defvar pkm2-tasks-priority-types (list '("1" . 1) '("2" . 2) '("3" . 3) '("4" . 4) '("5" . 5)))
(pkm2-register-behavior-2 (behavior-schema
:name "task"
:assets (list
(kvd-schema :name "is-todo" :key "node-type" :value "todo" :data-type 'TEXT)
(kvd-schema :name "task-status" :key "task-status" :choices pkm2-task-status-types
:prompt "What is todo's status?" :data-type 'TEXT :log-change t)
(kvd-schema :name "task-priority" :key "task-priority" :choices pkm2-tasks-priority-types
:prompt "What is todo's priority?" :data-type 'INTEGER :log-change t))))
(pkm2-register-behavior `(:name deadline :assets ((:pkm-type kvd :name "deadline" :key "deadline" :value ,#'pkm2-get-user-selected-timestamp
:link-to ("base-node") :data-type DATETIME )
(:pkm-type kvd :name "deadline-alert-minutes" :key "deadline-alert-minutes"
:link-to ("base-node") :data-type INTEGER :optional t))))
(pkm-register-structure 'schedule-node
(list :parent 'dependent-node
:browse-insert-format-string (concat
"[<insert>(:display kvd-value :key \"schedule-start\")</insert>]"
"--"
"[<insert>(:display kvd-value :key \"schedule-end\")</insert>]"
"<insert>(:display hidden :prefix \"\\n\")</insert>")
:managed-type t
:assets (list
'(:pkm-type node :name "base-node" :primary-node t :no-input t)
'(:pkm-type kvd :name "is-schedule" :key "node-type" :value "schedule" :link-to ("base-node") :data-type TEXT)
`(:pkm-type kvd :name "schedule-start" :key "schedule-start" :value ,#'pkm2-get-user-selected-timestamp
:link-to ("base-node") :data-type DATETIME )
`(:pkm-type kvd :name "schedule-end" :key "schedule-end" :value ,#'pkm2-get-user-selected-timestamp
:link-to ("base-node") :data-type DATETIME :optional t))))
(pkm-register-structure-2
(object-schema :name "task-n"
:parent-schema-name "base-n"
:is-behavior "task"
:behaviors (list `(:name "task" :link-to (primary)))
:log-primary t
:browse-insert-format-string (format "%s %s %s %s"
"<insert>(:display kvd-value :key \"task-status\" :face-value pkm2-tasks-status-face-plist)</insert>"
"<insert>(:display kvd-value :key \"task-priority\")</insert>"
"<insert>(:display content)</insert>"
"<insert>(:display hidden :prefix \"\\n\")</insert>")))
(pkm-register-structure-2
(object-schema :name "project-s"
:parent-schema-name "task-n"
:browse-insert-format-string (format "%s %s PROJ %s %s"
"<insert>(:display kvd-value :key \"task-status\" :face-value pkm2-tasks-status-face-plist)</insert>"
"<insert>(:display kvd-value :key \"task-priority\")</insert>"
"<insert>(:display content)</insert>"
"<insert>(:display hidden :prefix \"\\n\")</insert>")
:assets (list
(kvd-schema :name "is-project" :key "node-type" :value "project" :link-to '("base-node") :data-type 'TEXT))))
(defun pkm2-tasks-schedule-node (&optional db-id)
(interactive)
(let* ((structure-name 'schedule-node)
(parent-node-db-id (or db-id
(--> (funcall pkm2-get-pkm-node-at-point-func)
(when it (pkm-get-db-id it)))
(pkm2-nodes-search "Search node to schedule into: ")))
(link-label "instance")
(link-definition (list :pkm-link-label link-label
:parent 'parent
:child 'child))
(structure-schema (list :name 'parent-child-node
:assets (list
`(:pkm-type node :name "parent-node" :parent-node t :db-id ,parent-node-db-id)
`(:pkm-type ,structure-name :name "child-node" :child-node t))
:links (list link-definition))))
(pkm2--object-capture-object-verify structure-schema "pkm-schedule" t)))
(defun pkm2-tasks-set-node-deadline (&optional db-id)
(interactive)
(let* ((target-node (or db-id (--> (funcall pkm2-get-pkm-node-at-point-func)
(when it (pkm-get-db-id it)))
(pkm2-nodes-search "Search node to schedule into: ")))
(link-label "instance")
(structure-schema (list :name 'parent-child-node
:assets (list
`(:pkm-type node :name "target-node" :db-id ,target-node)
`(:pkm-type kvd :name "deadline" :key "deadline" :value ,#'pkm2-get-user-selected-timestamp :link-to ("target-node") :data-type DATETIME )))))
(pkm2--object-capture-object-verify structure-schema "pkm-schedule" t)))
(defun pkm2-tasks-alert ()
(let* ((node-with-deadline
(pkm2-get-nodes-with-pkm-query `((:or kvd (:key "deadline" :data-type INTEGER :after (hour -1) :before (hour 1)))
(:and kvd (:key "task-status" :data-type TEXT :choices ("DOING" "TODO" "HOLD"))))))
(deadlines (-map (lambda (h-i-d)
(pkm2-node-get-kvd-value-with-key h-i-d "deadline"))
node-with-deadline))
(alert-seconds (-map (lambda (h-i-d)
(--> (pkm2-node-get-kvd-value-with-key h-i-d "deadline-alert-minutes")
(when it (* 60 it))))
node-with-deadline)))
(-each-indexed node-with-deadline
(lambda (index h-i-d)
(when (nth index alert-seconds)
(when (< (- (nth index deadlines)
(nth index alert-seconds))
(pkm2-get-current-timestamp))
(--> (oref h-i-d :content)
(pkm-hammerspoon-alert it))))))))
(provide 'pkm2-tasks)
;;; pkm2-tasks.el ends here