-
Notifications
You must be signed in to change notification settings - Fork 15
/
orgswiper.py
331 lines (281 loc) · 9.27 KB
/
orgswiper.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
import calendar
import sublime
import sublime_plugin
import datetime
from OrgExtended.orgparse.date import *
import OrgExtended.pymitter as evt
def CreateUniqueViewNamed(name, mapped=None):
# Close the view if it exists
win = sublime.active_window()
for view in win.views():
if view.name() == name:
win.focus_view(view)
win.run_command('close')
win.run_command('new_file')
view = win.active_view()
view.set_name(name)
# TODO: Change this.
view.set_syntax_file("Packages/OrgExtended/orgdatepicker.sublime-syntax")
#ViewMappings[view.name()] = mapped
return view
class SwiperView:
def __init__(self,sourceView):
self.sourceView = sourceView
self.view = CreateUniqueViewNamed("swiper")
# Create new window
#self.window.run_command("new_window")
# Open the current file in the new window
#sublime.windows()[-1:][0].open_file(self.window.active_view().file_name())
def SetView(self, view):
self.output = view
def SetStartRow(self, row):
self.startrow = row
def DateToRegion(self, date):
# Convert
monthIndex = (date.month - self.midmonth + 1)
if(monthIndex < 0 or monthIndex >= len(self.monthdata)):
return
monthData = self.monthdata[monthIndex]
weekIndex = 0
dayIndex = 0
for weekId in range(0,len(monthData)):
weekData = monthData[weekId]
for dayId in range(0,len(weekData)):
day = weekData[dayId]
if(day == date.day):
weekIndex = weekId
dayIndex = dayId
# 2 row heading?
# We should have an offset? Probably
row = self.headingLines + weekIndex
col = monthIndex * self.columnsPerMonth + dayIndex*self.columnsPerDay
#print("ROW: " + str(row) + " COL: " + str(col))
s = self.output.text_point(row,col)
e = self.output.text_point(row,col+2)
return sublime.Region(s, e)
def HighlightDay(self, date):
reg = self.DateToRegion(date)
self.output.add_regions("cur",[reg],"orgdatepicker.monthheader","",sublime.DRAW_NO_OUTLINE)
def AddToDayHighlights(self, date, key, highlight, drawtype = sublime.DRAW_NO_OUTLINE):
reg = self.DateToRegion(date)
regs = self.output.get_regions(key)
if not regs:
regs = []
regs.append(reg)
self.output.add_regions(key,regs,highlight,"",drawtype)
def MapRowColToDate(self,row,col):
weekid = int(row - self.headingLines)
monthid = int(col / self.columnsPerMonth)
dayid = int((col % self.columnsPerMonth) / self.columnsPerDay)
day = self.monthdata[monthid][weekid][dayid]
month = monthid + self.midmonth - 1
year = self.cdate.start.year
time = self.cdate.start.time()
duration = None
if(time):
result = datetime.datetime(year, month, day, time.hour, time.minute, time.second, time.microsecond)
else:
result = datetime.datetime(year, month, day)
return result
def MoveCDateToNextDay(self):
self.cdate.add_days(1)
self.ReShow()
self.HighlightDay(self.cdate.start)
def MoveCDateToPrevDay(self):
self.cdate.add_days(-1)
self.ReShow()
self.HighlightDay(self.cdate.start)
def MoveCDateToNextWeek(self):
self.cdate.add_days(7)
self.ReShow()
self.HighlightDay(self.cdate.start)
def MoveCDateToPrevWeek(self):
self.cdate.add_days(-7)
self.ReShow()
self.HighlightDay(self.cdate.start)
def MoveCDateToNextMonth(self):
self.cdate.add_months(1)
self.ReShow()
self.HighlightDay(self.cdate.start)
def MoveCDateToPrevMonth(self):
self.cdate.add_months(-1)
self.ReShow()
self.HighlightDay(self.cdate.start)
def ReShow(self):
now = self.cdate.start
mid = (now.month - self.midmonth + 1)
if(mid < 0 or mid > 2):
self.Render(now)
self.ResetRenderState()
@staticmethod
def NextMonth(now):
month = now.month + 1
year = now.year
if(month > 12):
year += 1
month = 1
return (month, year)
@staticmethod
def PrevMonth(now):
month = now.month - 1
year = now.year
if(month <= 0):
year -= 1
month = 12
return (month, year)
def Render(self,now):
c = calendar.TextCalendar(calendar.SUNDAY)
str = c.formatmonth(now.year, now.month)
calendar.setfirstweekday(calendar.SUNDAY)
self.midmonth = now.month
pmonth, pyear = DateView.PrevMonth(now)
nmonth, nyear = DateView.NextMonth(now)
self.monthdata = [ calendar.monthcalendar(pyear, pmonth),
calendar.monthcalendar(now.year, now.month),
calendar.monthcalendar(nyear, nmonth)]
#print(str)
m2 = str.split('\n')
month, year = DateView.NextMonth(now)
str = c.formatmonth(year, month)
#print(str)
m3 = str.split('\n')
month, year = DateView.PrevMonth(now)
str = c.formatmonth(year, month)
#print(str)
m1 = str.split('\n')
self.output.set_read_only(False)
self.output.sel().clear()
pt = self.output.text_point(self.startrow,0)
self.output.sel().add(pt)
l = max(len(m1),len(m2),len(m3))
self.endrow = self.startrow + l
row = self.startrow
for i in range(0,l):
pt = self.output.text_point(row,0)
row += 1
ml1 = m1[i] if i < len(m1) else ""
ml2 = m2[i] if i < len(m2) else ""
ml3 = m3[i] if i < len(m3) else ""
line = "{0:30}{1:30}{2:30}".format(ml1,ml2,ml3)
lreg = self.output.line(pt)
lreg = sublime.Region(lreg.begin(), lreg.end() + 1)
self.output.ReplaceRegion(lreg, line + "\n")
self.HighlightDay(now)
def ResetRenderState(self):
self.output.set_read_only(True)
self.output.set_scratch(True)
self.output.set_name("DatePicker")
class DatePicker:
def on_done(self, text):
self.dateView.output.close()
print("INPUT DONE")
if(self.onDone):
evt.Get().emit(self.onDone, self.dateView.cdate)
def on_canceled(self):
self.dateView.output.close()
if(self.onDone):
evt.Get().emit(self.onDone, None)
def on_changed(self, text):
print("CHANGED: " + text)
self.dateView.cdate = OrgDateFreeFloating.from_str(text)
if(self.dateView.cdate):
self.dateView.HighlightDay(self.dateView.cdate.start)
self.dateView.ReShow()
def __init__(self):
self.dateView = DateView()
self.months = []
def MapRowColToNewDate(self,row,col):
time = self.dateView.cdate.start.time()
duration = None
if(self.dateView.cdate.has_end()):
duration = self.dateView.cdate.end - self.dateView.cdate.start
self.dateView.cdate._start = self.dateView.MapRowColToDate(row,col)
if(duration):
self.dateView.cdate._end = self.dateView.cdate.start + duration
self.inputpane.ReplaceRegion(self.inputpane.line(self.inputpane.text_point(0,0)), OrgDate.format_datetime(self.dateView.cdate.start))
self.HighlightDay(self.dateView.cdate.start)
def RefreshInputPanelFromDateView(self):
self.inputpane.ReplaceRegion(self.inputpane.line(self.inputpane.text_point(0,0)), OrgDate.format_datetime(self.dateView.cdate.start))
def MoveNextDay(self):
self.dateView.MoveCDateToNextDay()
self.RefreshInputPanelFromDateView()
def MovePrevDay(self):
self.dateView.MoveCDateToPrevDay()
self.RefreshInputPanelFromDateView()
def MoveNextWeek(self):
self.dateView.MoveCDateToNextWeek()
self.RefreshInputPanelFromDateView()
def MovePrevWeek(self):
self.dateView.MoveCDateToPrevWeek()
self.RefreshInputPanelFromDateView()
def MoveNextMonth(self):
self.dateView.MoveCDateToNextMonth()
self.RefreshInputPanelFromDateView()
def MovePrevMonth(self):
self.dateView.MoveCDateToPrevMonth()
self.RefreshInputPanelFromDateView()
def Show(self,now, onDone):
self.onDone = onDone
window = sublime.active_window()
self.output = CreateUniqueViewNamed("DatePicker")
self.dateView.SetView(self.output)
self.dateView.Render(now)
self.dateView.ResetRenderState()
curstr = OrgDate.format_datetime(now)
self.cdate = OrgDateFreeFloating.from_str(curstr)
self.inputpane = window.show_input_panel(
"Date:",
curstr,
self.on_done,
self.on_changed,
self.on_canceled)
pt = self.inputpane.text_point(0,0)
self.inputpane.set_syntax_file("Packages/OrgExtended/orgdateeditor.sublime-syntax")
# =============================================================
datePicker = None
def is_pt_date_view(view, pt):
return 'source.orgdatepicker' in view.scope_name(pt)
def onMouse(pt, view, edit):
if(not is_pt_date_view(view, pt)):
return
row, col = view.rowcol(pt)
if(datePicker):
datePicker.MapRowColToNewDate(row,col)
# TODO Convert point back to a date and push that into
# the input box.
def SetupMouse():
evt.Get().on("orgmouse",onMouse)
class AOrgDatePickerCommand(sublime_plugin.TextCommand):
def run(self, edit):
global datePicker
datePicker = DatePicker()
datePicker.Show(datetime.datetime.now(),None)
class AOrgDatePickerNextDayCommand(sublime_plugin.TextCommand):
def run(self, edit):
global datePicker
datePicker.MoveNextDay()
class AOrgDatePickerPrevDayCommand(sublime_plugin.TextCommand):
def run(self, edit):
global datePicker
datePicker.MovePrevDay()
class AOrgDatePickerPrevWeekCommand(sublime_plugin.TextCommand):
def run(self, edit):
global datePicker
datePicker.MovePrevWeek()
class AOrgDatePickerNextWeekCommand(sublime_plugin.TextCommand):
def run(self, edit):
print("NEXT WEEK 1")
global datePicker
datePicker.MoveNextWeek()
class AOrgDatePickerPrevMonthCommand(sublime_plugin.TextCommand):
def run(self, edit):
global datePicker
datePicker.MovePrevMonth()
class AOrgDatePickerNextMonthCommand(sublime_plugin.TextCommand):
def run(self, edit):
global datePicker
datePicker.MoveNextMonth()
def Pick(onDone):
global datePicker
datePicker = DatePicker()
datePicker.Show(datetime.datetime.now(), onDone)