-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathorgproperties.py
369 lines (343 loc) · 12.8 KB
/
orgproperties.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import sublime
import sublime_plugin
import datetime
import re
from pathlib import Path
import os
import fnmatch
import OrgExtended.orgparse.node as node
from OrgExtended.orgparse.sublimenode import *
import OrgExtended.orgparse.date as orgdate
import OrgExtended.orgutil.util as util
import OrgExtended.orgutil.navigation as nav
import OrgExtended.orgutil.template as templateEngine
import logging
import sys
import traceback
import OrgExtended.orgdb as db
import OrgExtended.asettings as sets
import OrgExtended.pymitter as evt
import OrgExtended.orgduration as dur
import uuid
#import yaml
log = logging.getLogger(__name__)
RE_SDC = re.compile(r'^\s*(SCHEDULED|DEADLINE|CLOSED):')
def InsertDrawerIfNotPresent(view, node, drawer = ":PROPERTIES:", onDone=None):
if not node or node.is_root():
evt.EmitIf(onDone)
return False
if(drawer == ":PROPERTIES:"):
if(node.property_drawer_location):
evt.EmitIf(onDone)
return False
else:
for d in node.drawers:
if((":" + d["name"] + ":") == drawer):
evt.EmitIf(onDone)
return False
level = node.level
indent = " " + (" " * level)
drawerRow = node.body_lines_start
# Properties should be the first drawer...
if(drawer != ":PROPERTIES:" and node.property_drawer_location):
drawerRow = node.property_drawer_location[1] + 1
# Also skip over SCHEDULED, DEADLINE and CLOSED lines
for row in range(drawerRow, node.local_end_row + 1):
pt = view.text_point(row, 0)
line = view.line(pt)
txt = view.substr(line)
m = RE_SDC.search(txt)
if(not m):
drawerRow = row
break
else:
drawerRow = row + 1
drawerHere = view.text_point(drawerRow, 0)
newline = "\n" if view.isBeyondLastRow(drawerRow) else ""
if(drawer == ":PROPERTIES:"):
node.set_property_drawer_location((drawerRow,drawerRow+1))
view.run_command("org_internal_insert", {"location": drawerHere, "text": newline + indent + drawer +"\n" + indent + ":END:\n","onDone":onDone})
return True
def AddProperty(view, node, key, value, onDone=None):
if(InsertDrawerIfNotPresent(view, node)):
# File is reloaded have to regrab node
node = db.Get().At(view, node.start_row)
end = node.property_drawer_location[1]
pt = view.text_point(end, 0)
level = node.level
indent = " " + (" " * level)
view.run_command("org_internal_insert", {"location": pt, "text": indent + ":" + str(key) + ": " + str(value) + "\n", "onDone": onDone})
node.set_property_drawer_location((node.property_drawer_location[0],node.property_drawer_location[1] + 1))
# KEY has to have : and everything.
def AddLogbook(view, node, key, value, onDone=None):
if(InsertDrawerIfNotPresent(view, node, ":LOGBOOK:")):
# File is reloaded have to regrab node
node = db.Get().At(view, node.start_row)
drawer = node.get_drawer("LOGBOOK")
loc = drawer['loc']
end = loc[1]
pt = view.text_point(end, 0)
level = node.level
indent = " " + (" " * level)
drawer['loc'] = (loc[0], loc[1] + 1)
view.run_command("org_internal_insert", {"location": pt, "text": indent + str(key) + " " + str(value) + "\n", "onDone": onDone})
RE_PROPERTY_EXTRACT = re.compile(r"^\s*:([a-zA-Z0-9_@-]+):\s*(.*)")
def GetProperty(view, node, key):
if(not node or not node.property_drawer_location):
return False
start = node.property_drawer_location[0]
end = node.property_drawer_location[1]
lkey = key.lower()
v = None
mrow = None
# Work backwards, want to find the latest incarnation of this
for row in range(end-1, start, -1):
line = view.getLine(row)
m = RE_PROPERTY_EXTRACT.search(line)
if(m != None):
k = m.group(1).strip()
v = m.group(2).strip()
if(k.lower() == lkey):
mrow = row
break
return v
def UpdateProperty(view, node, key, value, onDone=None):
def OnDrawer():
# File is reloaded have to regrab node
n = db.Get().At(view, node.start_row)
if(not n):
log.error("Failed to look up property drawer! Something is wrong!")
start, end = n.property_drawer_location
lkey = key.lower().strip()
mrow = None
# Work backwards, want to find the latest incarnation of this
for row in range(end-1, start, -1):
line = view.getLine(row)
m = RE_PROPERTY_EXTRACT.search(line)
if(m):
k = m.group(1).strip()
v = m.group(2).strip()
if(k.lower() == lkey):
mrow = row
break
# We found our property, replace it!
if(mrow != None):
pt = view.text_point(mrow, 0)
rw = view.line(pt)
text = " {0}:{1}: {2}".format(n.indent(),key,value)
view.ReplaceRegion(rw,text,onDone)
# Otherwise add a new property
else:
AddProperty(view, n, key, value, onDone)
InsertDrawerIfNotPresent(view, node, ":PROPERTIES:", evt.Make(OnDrawer))
def UpdateLogbook(view, node, key, value, onDone=None):
if(InsertDrawerIfNotPresent(view, node, ":LOGBOOK:")):
# File is reloaded have to regrab node
node = db.Get().At(view, node.start_row)
if not node or node.is_root():
evt.EmitIf(onDone)
return
drawer = node.get_drawer("LOGBOOK")
loc = drawer['loc']
start = loc[0]
end = loc[1]
lkey = key.lower()
mrow = None
extract = re.compile(r'^\s*' + key)
# Work backwards, want to find the latest incarnation of this
for row in range(end-1, start, -1):
line = view.getLine(row)
m = extract.search(line)
if(m != None):
mrow = row
break
level = node.level
indent = " " + (" " * level)
# We found our property, replace it!
if(mrow != None):
pt = view.text_point(mrow, 0)
rw = view.line(pt)
view.run_command("org_internal_replace", {"start": rw.begin(), "end": rw.end(), "text": indent + str(key) + " " + str(value), "onDone": onDone})
# Otherwise add a new property
else:
AddLogbook(view, node, key, value, onDone)
def RemoveProperty(view, node, key):
if(not node or not node.property_drawer_location):
return False
start = node.property_drawer_location[0]
end = node.property_drawer_location[1]
lkey = key.lower()
mrow = None
# Work backwards, want to find the latest incarnation of this
for row in range(end-1, start, -1):
line = view.getLine(row)
m = RE_PROPERTY_EXTRACT.search(line)
if(m != None):
k = m.group(1).strip()
v = m.group(2).strip()
if(k.lower() == lkey):
mrow = row
break
# We found our property, erase it!
if(mrow != None):
pt = view.text_point(mrow, 0)
rw = view.line(pt)
view.run_command("org_internal_erase", {"start": rw.begin(), "end": (rw.end()+1)})
node.set_property_drawer_location((node.property_drawer_location[0],node.property_drawer_location[1] - 1))
return True
return False
def RemoveLogbook(view, node, key):
if(not node):
return False
drawer = node.get_drawer("LOGBOOK")
if(not drawer):
return False
loc = drawer['loc']
start = loc[0]
end = loc[1]
lkey = key.lower()
mrow = None
# Work backwards, want to find the latest incarnation of this
for row in range(end-1, start, -1):
line = view.getLine(row)
m = re.search(key,line)
if(m != None):
mrow = row
break
# We found our property, erase it!
if(mrow != None):
pt = view.text_point(mrow, 0)
rw = view.line(pt)
view.run_command("org_internal_erase", {"start": rw.begin(), "end": (rw.end()+1)})
drawer['loc'] = (start,end-1)
return True
return False
def GetLogbook(view, node, key):
if(not node):
return False
drawer = node.get_drawer("LOGBOOK")
if not drawer:
return False
loc = drawer['loc']
start = loc[0]
end = loc[1]
lkey = key.lower()
mrow = None
v = False
# Work backwards, want to find the latest incarnation of this
for row in range(end-1, start, -1):
line = view.getLine(row)
m = re.search(key,line)
if(m != None):
mrow = row
break
if(mrow != None):
pt = view.text_point(mrow, 0)
rw = view.line(pt)
#view.run_command("org_internal_erase", {"start": rw.begin(), "end": (rw.end()+1)})
#drawer['loc'] = (start,end-1)
v = re.sub(key,"",line).strip()
return v
# Removes all instances of a named property from the drawer.
def RemoveAllInstances(view, node, key):
while(RemoveProperty(view, node, key)):
pass
# Removes all instances of a named logbook entry from the drawer.
def RemoveAllLogbookInstances(view, node, key):
while(RemoveLogbook(view, node, key)):
pass
# Insert a new drawer into the node.
class OrgInsertDrawerCommand(sublime_plugin.TextCommand):
def run(self,edit):
self.view.window().show_input_panel(
"Drawer Name:",
"",
self.createDrawer, None, None)
def createDrawer(self, drawer):
if(not drawer):
return
node = db.Get().AtInView(self.view)
if(node and node.level > 0):
InsertDrawerIfNotPresent(self.view, node, ":" + drawer + ":")
class OrgInsertPropertyDrawerCommand(sublime_plugin.TextCommand):
def run(self,edit):
node = db.Get().AtInView(self.view)
if(node and node.level > 0):
InsertDrawerIfNotPresent(self.view, node, ":PROPERTIES:")
class OrgInsertPropertyCommand(sublime_plugin.TextCommand):
def run(self,edit,onDone=None,name=None,value=None):
self.onDone=onDone
if name != None:
self.pname = name
if value != None:
self.prop = value
self.createPropertyV2(self.prop)
else:
self.createProperty(name)
else:
self.view.window().show_input_panel(
"Property Name:",
"",
self.createProperty, None, None)
def createProperty(self, prop):
if(not prop):
return
self.pname = prop
self.view.window().show_input_panel(
"Property Value:",
"",
self.createPropertyV2, None, None)
def createPropertyV2(self, prop):
if(not prop):
return
node = db.Get().AtInView(self.view)
if(node and node.level > 0):
UpdateProperty(self.view, node, self.pname, prop, self.onDone)
class OrgInsertCreatedPropertyCommand(sublime_plugin.TextCommand):
def run(self,edit,onDone=None):
self.onDone=onDone
now = datetime.datetime.now()
toInsert = orgdate.OrgDate.format_clock(now, False)
self.view.run_command("org_insert_property", {"name": "Created","value": toInsert, "onDone":onDone})
class OrgInsertEffortCommand(sublime_plugin.TextCommand):
def run(self,edit,onDone=None):
self.onDone=onDone
node = db.Get().AtInView(self.view)
if(node and node.level > 0):
v = GetProperty(self.view, node, "EFFORT")
if(not v):
v = ""
self.view.window().show_input_panel(
"Effort:",
v,
self.createProperty, None, None)
def createProperty(self, prop):
if(not prop):
return
isint = False
try:
i = int(prop)
isint = True
except:
pass
if(isint):
inc = sets.Get("defaultEffortEstimateUnit","d")
prop = prop + inc
d = dur.OrgDuration.Parse(prop)
if(d):
node = db.Get().AtInView(self.view)
if(node and node.level > 0):
UpdateProperty(self.view, node, "Effort", str(d), self.onDone)
else:
log.error("Could not generate effort, please use org duration formation for your estimate")
view.set_status("Error: ", prop + "is not a valid effort, please use org duration formation 1y 1d 1h 1min")
evt.EmitIf(self.onDone)
class OrgCreateHeadingIdCommand(sublime_plugin.TextCommand):
def run(self,edit,onDone=None):
self.onDone=onDone
node = db.Get().AtInView(self.view)
if(node and node.level > 0):
v = GetProperty(self.view, node, "ID")
if(not v):
value = uuid.uuid4()
UpdateProperty(self.view, node, "ID", str(value), self.onDone)