-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
org-gtd-refile.el
140 lines (115 loc) · 4.53 KB
/
org-gtd-refile.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
;;; org-gtd-refile.el --- refiling logic for org gtd -*- lexical-binding: t; coding: utf-8 -*-
;;
;; Copyright © 2019-2023 Aldric Giacomoni
;; Author: Aldric Giacomoni <[email protected]>
;; This file is not part of GNU Emacs.
;; This file 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, or (at your option)
;; any later version.
;; This file 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 file. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Refiling logic for org-gtd.
;;
;;; Code:
(require 'org)
(require 'org-refile)
(require 'org-element)
(require 'org-gtd-core)
(defcustom org-gtd-refile-to-any-target t
"Set to true if you do not need to choose where to refile processed items.
When this is true, org-gtd will refile to the first target it finds, or creates
it if necessary, without confirmation. When this is false, it will ask for
confirmation regardless of the number of options. Note that setting this to
false does not mean you can safely create new targets. See the documentation
to create new refile targets.
Defaults to true to carry over pre-2.0 behavior. You will need to change this
setting as part of following the instructions to add your own refile targets."
:group 'org-gtd
:type 'boolean
:package-version '(org-gtd . "2.0.0"))
(defconst org-gtd-projects-template
"* Projects
:PROPERTIES:
:TRIGGER: relatives(forward-no-wrap todo-only 1 no-sort) todo!(NEXT)
:ORG_GTD: Projects
:END:
")
(defconst org-gtd-calendar-template
"* Calendar
:PROPERTIES:
:ORG_GTD: Calendar
:END:
")
(defconst org-gtd-actions-template
"* Actions
:PROPERTIES:
:ORG_GTD: Actions
:END:
")
(defconst org-gtd-incubated-template
"* Incubate
:PROPERTIES:
:ORG_GTD: Incubated
:END:
"
"Template for the GTD someday/maybe list.")
(defconst org-gtd--file-template
(let ((myhash (make-hash-table :test 'equal)))
(puthash org-gtd-actions org-gtd-actions-template myhash)
(puthash org-gtd-calendar org-gtd-calendar-template myhash)
(puthash org-gtd-projects org-gtd-projects-template myhash)
(puthash org-gtd-incubated org-gtd-incubated-template myhash)
myhash))
(defconst org-gtd-refile--prompt
(let ((myhash (make-hash-table :test 'equal)))
(puthash org-gtd-actions "Refile single action to: " myhash)
(puthash org-gtd-incubated "Refile incubated item to: " myhash)
(puthash org-gtd-delegated "Refile delegated item to: " myhash)
(puthash org-gtd-projects "Refile project to: " myhash)
(puthash org-gtd-calendar "Refile calendar item to: " myhash)
myhash))
;;;###autoload
(defmacro with-org-gtd-refile (type &rest body)
"Macro to refile specifically within org-gtd context.
TYPE is the org-gtd action type. BODY is the rest of the code."
(declare (debug t) (indent 1))
`(let ((org-refile-target-verify-function (lambda () (org-gtd-refile--group-p ,type)))
(org-refile-targets '((org-agenda-files :level . 1))))
(unwind-protect
(with-org-gtd-context (progn ,@body)))))
(defun org-gtd--refile (type)
"Refile an item to the single action file.
TYPE is one of the org-gtd action types. This is a private function."
(with-org-gtd-refile type
(unless (org-refile-get-targets) (org-gtd-refile--add-target type))
(if org-gtd-refile-to-any-target
(org-refile nil nil (car (org-refile-get-targets)))
(org-refile nil nil nil (org-gtd-refile--prompt type)))))
(defun org-gtd-refile--add-target (gtd-type)
"Private function used to create a missing org-gtd refile target.
GTD-TYPE is an action type."
(with-current-buffer (org-gtd--default-file)
(end-of-buffer)
(newline)
(insert (gethash gtd-type org-gtd--file-template))
(basic-save-buffer)
))
(defun org-gtd-refile--group-p (type)
"Determine whether the current heading is of a given gtd TYPE."
(string-equal (org-gtd-refile--group type)
(org-element-property :ORG_GTD (org-element-at-point))))
(defun org-gtd-refile--group (type)
"What kind of gtd group is TYPE."
(gethash type org-gtd--properties))
(defun org-gtd-refile--prompt (type)
"What is the right refile prompt for this gtd TYPE."
(gethash type org-gtd-refile--prompt))
(provide 'org-gtd-refile)
;;; org-gtd-refile.el ends here