-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg-export-todoist.el
278 lines (251 loc) · 11.8 KB
/
org-export-todoist.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
;;; org-export-todoist.el --- Push org-agenda as tasks of todoist -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Masaki Waga
;; This file is NOT part of Emacs.
;; 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 2 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, write to the Free Software
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
;; USA
;; Version: HEAD
;; Author: Masaki Waga
;; Keywords: todoist org-mode
;; URL: https://github.com/MasWag/org-export-todoist
;; License: GNU General Public License >= 3
;;; Commentary:
;; This library implements a function to export tasks of org-agenda as
;; tasks of todoist.
;;
;; This file is based on ox-icalendar.el (bundled with org).
;; Most of the functions in this file depend on todoist.el
;; <https://github.com/abrochard/emacs-todoist>.
;;; Setup:
;; You need to set up todoist.el
;;; Usage:
;; 1. =M-x org-agenda= to open a list of agenda
;; 2. =M-x org-current-agenda-export-todoist= to export the current tasks.
;;; Code:
(require 'ox-icalendar)
(require 'todoist)
(defcustom ox-agenda-todoist-export-unscheduled nil
"If not nil, it always exports unscheduled entries."
:group 'ox-agenda-todoist
:type 'bool)
(defun org-export-todoist--find-todoist-project-id (projects project_name)
"Find the project id of the project.
This function does not require the set up of todoist's API key.
PROJECTS the list of all projects.
PROJECT_NAME the name of the project."
(todoist--project-id
(-first (lambda (project) (equal (todoist--project-name project) project_name))
projects)))
(defun org-export-todoist--new-task (summary start priority categories timezone)
"Add a simplified task in Todoist with essential information."
(let* ((due (if start
(format-time-string "%Y-%m-%d" (org-time-string-to-time start))
""))
;; Priority in Todoist: 4 is the highest priority, 1 is the lowest priority.
;; Adjusting the priority to match Todoist's scale.
(todoist-priority (+ (- 3 (- org-priority-lowest org-priority-highest))
(- org-priority-lowest
(or priority org-priority-default))))
;; Find the project ID, defaulting to Inbox if not found.
(project-id (org-export-todoist--find-todoist-project-id
(todoist--get-projects)
(or categories "Inbox"))))
;; POST the task to Todoist
(message "Export the following task to Todoist: %s %s %s %s" summary due project-id todoist-priority)
(todoist--query "POST" "/tasks"
`(("content" . ,summary)
("due_string" . ,due)
("project_id" . ,project-id)
("priority" . ,todoist-priority)))))
(defun org-export-todoist--add-task
(entry summary location description categories timezone class)
"Add a task in todoist.This function REQUIRES the set up of todoist's API key."
(let ((start (org-element-property :scheduled entry))
;; Convert Org-mode priority to a number. Org-mode priorities are letters by default.
(priority (or (org-element-property :priority entry)
org-priority-default)))
;; We need to extract a string from a text-property
(set-text-properties 0 (length summary) nil summary)
;; Make the due string
(let ((due (if start
(replace-regexp-in-string
"\\([0-9][0-9][0-9][0-9]\\)\\([0-9][0-9]\\)"
"\\1-\\2-"
(replace-regexp-in-string
".*:" ""
(org-icalendar-convert-timestamp start "" nil timezone)))
;; If the entry is unscheduled, we use empty due_string
"")))
;; Call the simplified Todoist task adding function.
(org-export-todoist--new-task summary due priority categories timezone)
;; This must return a string
"DUMMY")))
(defun org-export-current-line-to-todoist ()
"Export the current line in org-agenda to a Todoist task."
(interactive)
(if (eq major-mode 'org-agenda-mode)
(let* ((marker (or (org-get-at-bol 'org-hd-marker)
(org-get-at-bol 'org-marker)))
(buffer (and marker (marker-buffer marker)))
(pos (and marker (marker-position marker)))
summary start priority category)
(when buffer
(with-current-buffer buffer
(goto-char pos)
;; Extract properties from the org item
(setq summary (org-get-heading t t t t)
start (org-entry-get pos "SCHEDULED")
priority (string-to-char
(org-entry-get pos "PRIORITY"))
category (org-entry-get pos "CATEGORY"))))
;; Convert Org-mode priority to a number (if present)
(let (;; Assuming the local timezone; adjust as necessary
(timezone (current-time-zone)))
;; Export to Todoist using the simplified function
(org-export-todoist--new-task summary start priority category timezone))
(message "Exported current line to Todoist"))
(message "Not in org-agenda mode.")))
;; (defun org-export-todoist--add-task
;; (entry summary location description categories timezone class)
;; "Add a task in todoist.This function REQUIRES the set up of todoist's API key."
;; (let ((start (org-element-property :scheduled entry))
;; ;; priority: 4 is the highest priority, 1 is the lowest priority
;; (priority (+ (- 3 (- org-priority-lowest org-priority-highest))
;; (- org-priority-lowest
;; (or (org-element-property :priority entry)
;; org-priority-default))))
;; ;; ID of the proejct. We use Inbox by default.
;; (project-id (org-export-todoist--find-todoist-project-id
;; (todoist--get-projects)
;; (or categories "Inbox"))))
;; ;; We need to extract a string from a text-property
;; (set-text-properties 0 (length summary) nil summary)
;; ;; Make the due string
;; (let ((due (if start
;; (replace-regexp-in-string
;; "\\([0-9][0-9][0-9][0-9]\\)\\([0-9][0-9]\\)"
;; "\\1-\\2-"
;; (replace-regexp-in-string
;; ".*:" ""
;; (org-icalendar-convert-timestamp start "" nil timezone)))
;; ;; If the entry is unscheduled, we use empty due_string
;; "")))
;; ;; We add task only if it is TODO
;; (if (eq (org-element-property :todo-type entry) 'todo)
;; (todoist--query
;; "POST" "/tasks"
;; (append `(("content" . ,summary)
;; ("due_string" . ,due)
;; ("project_id" . ,project-id)
;; ("priority" . ,priority)))))
;; "DUMMY")))
(defun org-todoist-entry (entry contents info)
"This function REQUIRES the set up of todoist's API key."
(when (and
(or ox-agenda-todoist-export-unscheduled
(plist-get info :todoist-export-unscheduled)
;; Check that the entry is scheduled
(org-element-property :scheduled entry))
(not (org-element-property :footnote-section-p entry)))
(let* ((type (org-element-type entry))
;; Determine contents really associated to the entry. For
;; a headline, limit them to section, if any. For an
;; inlinetask, this is every element within the task.
(inside
(if (eq type 'inlinetask)
(cons 'org-data (cons nil (org-element-contents entry)))
(let ((first (car (org-element-contents entry))))
(and (eq (org-element-type first) 'section)
(cons 'org-data
(cons nil (org-element-contents first))))))))
(let ((todo-type (org-element-property :todo-type entry))
(summary (org-icalendar-cleanup-string
(or (org-element-property :SUMMARY entry)
(org-export-data
(org-element-property :title entry) info))))
(priority (org-element-property :priority entry))
(loc (org-icalendar-cleanup-string
(org-export-get-node-property
:LOCATION entry
(org-property-inherit-p "LOCATION"))))
(class (org-icalendar-cleanup-string
(org-export-get-node-property
:CLASS entry
(org-property-inherit-p "CLASS"))))
;; Build description of the entry from associated section
;; (headline) or contents (inlinetask).
(desc
(org-icalendar-cleanup-string
(or (org-element-property :DESCRIPTION entry)
(let ((contents (org-export-data inside info)))
(cond
((not (org-string-nw-p contents)) nil)
((wholenump org-icalendar-include-body)
(let ((contents (org-trim contents)))
(substring
contents 0 (min (length contents)
org-icalendar-include-body))))
(org-icalendar-include-body (org-trim contents)))))))
(cat (org-icalendar-get-categories entry info))
(tz (org-export-get-node-property
:TIMEZONE entry
(org-property-inherit-p "TIMEZONE"))))
;; Task: First check if it is appropriate to export it. If
;; so, call `org-export-todoist--add-task' to transcode it into
;; a "VTODO" component.
(when (and todo-type
(cl-case (plist-get info :icalendar-include-todo)
(all t)
(unblocked
(and (eq type 'headline)
(not (org-icalendar-blocked-headline-p
entry info))))
((t) (eq todo-type 'todo))))
(org-export-todoist--add-task entry summary loc desc cat tz class))))))
(defun org-current-agenda-export-todoist ()
"Export the current agenda as tasks of todoist.
This function REQUIRES the set up of todoist's API key."
(interactive)
(org-export-string-as
(with-output-to-string
(save-excursion
(let ((p (point-min))
(seen nil)) ;prevent duplicates
(while (setq p (next-single-property-change p 'org-hd-marker))
(let ((m (get-text-property p 'org-hd-marker)))
(when (and m (not (member m seen)))
(push m seen)
(with-current-buffer (marker-buffer m)
(org-with-wide-buffer
(goto-char (marker-position m))
(princ
(org-element-normalize-string
(buffer-substring (point)
(org-entry-end-position))))))))
(forward-line)))))
'todoist t
'(:ascii-charset utf-8 :ascii-links-to-notes nil
:todoist-export-unscheduled nil
:icalendar-include-todo all)))
;;; Define an org exporter for todoist.
(org-export-define-derived-backend 'todoist 'ascii
:translate-alist '((clock . ignore)
(footnote-definition . ignore)
(footnote-reference . ignore)
(headline . org-todoist-entry)
(inner-template . ignore)
(inlinetask . ignore)
(planning . ignore)
(section . ignore)
(template . ignore)))
(provide 'org-export-todoist)
;;; org-export-todoist.el ends here