-
Notifications
You must be signed in to change notification settings - Fork 15
/
orgdaypage.py
307 lines (276 loc) · 11 KB
/
orgdaypage.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import sublime
import sublime_plugin
import datetime
import re
import os
import logging
import getpass
import OrgExtended.asettings as sets
import OrgExtended.pymitter as evt
import OrgExtended.orgextension as ext
import OrgExtended.orgparse.date as orgdate
import OrgExtended.orgdb as db
log = logging.getLogger(__name__)
# I think I will model this feature after org-roam dailies.
# olBc has not really gotten back to me on the subject and I think
# dailies makes a lot of sense.
def dayPageGetToday():
dt = datetime.datetime.now()
change = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"]
if(sets.Get("dayPageMode","day") == "week"):
firstDay = sets.Get("dayPageModeWeekDay","Monday").lower()
startAt = [idx for idx, element in enumerate(change) if element.startswith(firstDay)]
if(len(startAt) > 0):
startAt = startAt[0]
else:
startAt = 0
offset = (dt.weekday() - startAt)
if(offset != 0):
dt = dt - datetime.timedelta(days=offset)
return dt
def dayPageGetPath():
dpPath = sets.Get("dayPagePath",None)
if(dpPath == None):
return None
try:
if isinstance(dpPath, list):
sublime.status_message("Day Page error. dayPagePath setting should be a string not a list! ABORT!")
log.error(" Cannot create day page without propper dayPathPath in configuration. Expected string, found list")
return None
if isinstance(dpPath, str) and not dpPath.strip() == "":
os.makedirs(dpPath, exist_ok=True)
except Exception as e:
sublime.status_message("Day Page error. dayPagePath setting is not valid could not create daypage! ABORT!")
log.error("Cannot create day page without propper dayPathPath in configuration: \n" + str(e))
return None
return dpPath
def dayPageGetDateString(dt):
formatStr = sets.Get("dayPageNameFormat","%a_%Y_%m_%d")
return dt.strftime(formatStr)
def dayPageFilenameToDateTime(view):
filename = view.file_name()
if(not filename):
return None
formatStr = sets.Get("dayPageNameFormat","%a_%Y_%m_%d")
filename = os.path.splitext(os.path.basename(filename))[0]
return datetime.datetime.strptime(filename,formatStr)
def dayPageGetName(dt):
path = dayPageGetPath()
if path == None:
return "DAY_PAGE_NOT_SET.org"
else:
return os.path.join(dayPageGetPath(),dayPageGetDateString(dt) + ".org")
def OnLoaded(view,dt):
view.sel().clear()
view.sel().add(0)
snippet = sets.Get("dayPageSnippet","dayPageSnippet")
snipName = ext.find_extension_file('orgsnippets',snippet,'.sublime-snippet')
if(snipName == None):
log.error(" Could not locate snippet file: " + str(snippet) + ".sublime-snippet using default")
snipName = ext.find_extension_file('orgsnippets','dayPageSnippet.sublime-snippet')
# NeoVintageous users probably prefern not to have to hit insert when editing things.
view.run_command('_enter_insert_mode', {"count": 1, "mode": "mode_internal_normal"})
now = dt
inow = orgdate.OrgDate.format_date(now, False)
anow = orgdate.OrgDate.format_date(now, True)
ai = view.settings().get('auto_indent')
view.settings().set('auto_indent',False)
# "Packages/OrgExtended/orgsnippets/"+snippet+".sublime-snippet"
# OTHER VARIABLES:
# TM_FULLNAME - Users full name
# TM_FILENAME - File name of the file being edited
# TM_CURRENT_WORD - Word under cursor when snippet was triggered
# TM_SELECTED_TEXT - Selected text when snippet was triggered
# TM_CURRENT_LINE - Line of snippet when snippet was triggered
#insert_snippet {"name": "Packages/OrgExtended/orgsnippets/page.sublime-snippet"}
view.run_command("insert_snippet",
{ "name" : snipName
, "ORG_INACTIVE_DATE": inow
, "ORG_ACTIVE_DATE": anow
, "ORG_DATE": str(dt.date().today())
, "ORG_TIME": dt.strftime("%H:%M:%S")
, "ORG_CLIPBOARD": sublime.get_clipboard()
, "ORG_SELECTION": view.substr(view.sel()[0])
, "ORG_LINENUM": str(view.curRow())
, "ORG_FILENAME": dayPageGetDateString(dt)
, "ORG_AUTHOR": getpass.getuser()
})
view.settings().set('auto_indent',ai)
def LoadedCheck(view,dt):
if(view.is_loading()):
sublime.set_timeout(lambda: LoadedCheck(view,dt),1)
else:
OnLoaded(view,dt)
def LoadedCheck2(view,dt, onDone):
if(view.is_loading()):
sublime.set_timeout(lambda: LoadedCheck2(view,dt,onDone),1)
else:
onDone(view,dt)
def dayPageInsertSnippet(view,dt):
window = view.window()
window.focus_view(view)
LoadedCheck(view,dt)
def dayPageFindOldPage(dt):
maxScan = 90
for i in range(maxScan):
dt = dt - datetime.timedelta(days=1)
fn = dayPageGetName(dt)
if(os.path.exists(fn)):
return fn
return None
def dayPageArchiveOld(dt):
fn = dayPageFindOldPage(dt)
if(fn == None):
return
f = db.Get().FindFileByFilename(os.path.basename(fn))
if(f == None):
return
if(f.org[0].set_comment("FILETAGS","ARCHIVE")):
f.Save()
def IsTodo(n):
return n.todo and n.todo in n.env.todo_keys
def IsDone(n):
return n.todo and n.todo in n.env.done_keys
def IsArchived(n):
return "ARCHIVE" in n.tags
def EnsureDate(ts):
if(isinstance(ts,datetime.datetime)):
return ts.date()
return ts
def dayPageCopyOpenTasks(tview, dt):
fn = dayPageFindOldPage(dt)
if(fn == None):
dayPageInsertSnippet(tview,dt)
return
f = db.Get().FindFileByFilename(os.path.basename(fn))
if(f == None):
dayPageInsertSnippet(tview,dt)
return
out = ""
for h in f.org[0].children:
if IsTodo(h):
for line in h._lines:
out += line + "\n"
if(out != ""):
LoadedCheck2(tview, dt, lambda a,b: tview.run_command("org_internal_insert", {"location": 0, "text": out, "onDone": evt.Make(lambda : dayPageInsertSnippet(tview, dt))}))
else:
dayPageInsertSnippet(tview,dt)
pass
def dayPageCopyOpenPhase(tview,dt):
if(sets.Get("dayPageCopyOpenTasks", True)):
dayPageCopyOpenTasks(tview, dt)
else:
dayPageInsertSnippet(tview,dt)
def dayPageCopyTodayTasks(path, tview, dt):
allowOutsideOrgDir = sets.Get("dayPageIncludeFilesOutsideOrgDir", False)
out = ""
for file in db.Get().Files:
# Quick out if ARCHIVE is marked on the file
globalTags = file.org.list_comment("FILETAGS",[])
if("ARCHIVE" in globalTags):
continue
# Skip over files not in orgDir
if(not file.isOrgDir and not allowOutsideOrgDir):
continue
# Skip over ourselves.
if(path.lower() == file.GetFilename().lower()):
continue
skipTill = 0
for i in range(1,len(file.org)):
if(i < skipTill):
continue
n = file.org[i]
if(IsTodo(n)):
ok = False
timestamps = n.get_timestamps(active=True,point=True,range=True)
for t in timestamps:
if(t.start.day == dt.day and t.start.month == dt.month and t.start.year == dt.year):
ok = True
break
if(n.scheduled and (EnsureDate(n.scheduled.start) < EnsureDate(dt) and not IsDone(n) and not IsArchived(n) or EnsureDate(n.scheduled.start) == EnsureDate(dt))):
ok = True
if(n.deadline and (EnsureDate(n.deadline.deadline_start) < EnsureDate(dt) and not IsDone(n) and not IsArchived(n) or EnsureDate(n.deadline.deadline_start) == EnsureDate(dt))):
ok = True
if(ok):
for line in n._lines:
out += line + "\n"
skipTill = n.find_last_child_index() + 1
if(out != ""):
LoadedCheck2(tview, dt, lambda a,b: tview.run_command("org_internal_insert", {"location": 0, "text": out, "onDone": evt.Make(lambda : dayPageCopyOpenPhase(tview, dt))}))
else:
dayPageCopyOpenPhase(tview,dt)
def dayPageCreateOrOpen(dt):
dpPath = dayPageGetName(dt)
dateString = dayPageGetDateString(dt)
didCreate = False
if(not os.path.exists(dpPath)):
with open(dpPath,"w") as f:
f.write("")
didCreate = True
tview = sublime.active_window().open_file(dpPath, sublime.ENCODED_POSITION)
if(didCreate):
if(sets.Get("dayPageArchiveOld", True)):
dayPageArchiveOld(dt)
if(sets.Get("dayPageCopyTasksForToday", True)):
dayPageCopyTodayTasks(dpPath, tview, dt)
else:
dayPageCopyOpenPhase(tview,dt)
class OrgDayPagePreviousCommand(sublime_plugin.TextCommand):
def OnDone(self):
evt.EmitIf(self.onDone)
def run(self, edit, onDone=None):
self.edit = edit
self.onDone = onDone
self.dt = datetime.datetime.now()
dt = dayPageFilenameToDateTime(self.view)
maxScan = 90
for i in range(maxScan):
dt = dt - datetime.timedelta(days=1)
if(sets.Get("dayPageCreateOldPages",False)):
dayPageCreateOrOpen(dt)
break
else:
fn = dayPageGetName(dt)
if(os.path.exists(fn)):
tview = sublime.active_window().open_file(fn, sublime.ENCODED_POSITION)
sublime.active_window().focus_view(tview)
break
else:
#log.warning("Day page does not exist: " + fn)
pass
class OrgDayPageNextCommand(sublime_plugin.TextCommand):
def OnDone(self):
evt.EmitIf(self.onDone)
def run(self, edit, onDone=None):
self.edit = edit
self.onDone = onDone
self.now = datetime.datetime.now()
dt = dayPageFilenameToDateTime(self.view)
maxScan = 90
for i in range(maxScan):
dt = dt + datetime.timedelta(days=1)
if(dt.date() < self.now.date()):
fn = dayPageGetName(dt)
if(os.path.exists(fn)):
tview = sublime.active_window().open_file(fn, sublime.ENCODED_POSITION)
sublime.active_window().focus_view(tview)
break
else:
#log.warning("Day page does not exist: " + fn)
pass
elif(dt.date() == self.now.date()):
dayPageCreateOrOpen(dt)
break
else:
fn = dayPageGetName(dt)
log.error(" Create day page in the future? " + fn)
break
class OrgDayPageCreateCommand(sublime_plugin.TextCommand):
def OnDone(self):
evt.EmitIf(self.onDone)
def run(self, edit, onDone=None):
self.edit = edit
self.onDone = onDone
self.dt = dayPageGetToday()
dayPageCreateOrOpen(self.dt)
self.OnDone()