forked from aziz/PlainTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlainTasks.py
executable file
·271 lines (230 loc) · 12.2 KB
/
PlainTasks.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import sublime
import sublime_plugin
import webbrowser
from datetime import datetime
SYNTAX_FILE_NAME = r'Packages/Donster2kPlainTasks/PlainTasks.tmLanguage'
class PlainTasksBase(sublime_plugin.TextCommand):
def run(self, edit):
self.open_tasks_bullet = self.view.settings().get('open_tasks_bullet')
self.done_tasks_bullet = self.view.settings().get('done_tasks_bullet')
self.canc_tasks_bullet = self.view.settings().get('canc_tasks_bullet')
self.before_tasks_bullet_spaces = ' ' * self.view.settings().get('before_tasks_bullet_margin')
if self.view.settings().get('done_tag'):
self.done_tag = "@done"
self.canc_tag = "@cancelled"
else:
self.done_tag = ""
self.canc_tag = ""
self.extensions = self.view.settings().get('extensions')
self.date_format_short = self.view.settings().get('date_format_short')
self.date_format = self.view.settings().get('date_format')
self.runCommand(edit)
class PlainTasksNewCommand(PlainTasksBase):
def runCommand(self, edit):
for region in self.view.sel():
line = self.view.line(region)
line_contents = self.view.substr(line).rstrip()
has_bullet = re.match('^(\s*)[' + re.escape(self.open_tasks_bullet) + re.escape(self.done_tasks_bullet) + re.escape(self.canc_tasks_bullet) + ']', self.view.substr(line))
current_scope = self.view.scope_name(self.view.sel()[0].b)
if has_bullet:
grps = has_bullet.groups()
line_contents = self.view.substr(line) + '\n' + grps[0] + self.open_tasks_bullet + ' '
self.view.replace(edit, line, line_contents)
elif 'header' in current_scope:
header = re.match('^(\s*)\S+', self.view.substr(line))
if header:
grps = header.groups()
line_contents = self.view.substr(line) + '\n' + grps[0] + self.before_tasks_bullet_spaces + self.open_tasks_bullet + ' '
else:
line_contents = self.before_tasks_bullet_spaces + self.open_tasks_bullet + ' '
self.view.replace(edit, line, line_contents)
end = self.view.sel()[0].b
pt = sublime.Region(end, end)
self.view.sel().clear()
self.view.sel().add(pt)
else:
has_space = re.match('^(\s+)(.*)', self.view.substr(line))
if has_space:
grps = has_space.groups()
spaces = grps[0]
line_contents = spaces + self.open_tasks_bullet + ' ' + grps[1]
self.view.replace(edit, line, line_contents)
else:
line_contents = self.before_tasks_bullet_spaces + self.open_tasks_bullet + ' ' + self.view.substr(line)
self.view.replace(edit, line, line_contents)
end = self.view.sel()[0].b
pt = sublime.Region(end, end)
self.view.sel().clear()
self.view.sel().add(pt)
class PlainTasksCompleteCommand(PlainTasksBase):
def runCommand(self, edit):
original = [r for r in self.view.sel()]
done_line_end = ' %s %s' % (self.done_tag, datetime.now().strftime(self.date_format))
offset = len(done_line_end)
for region in self.view.sel():
line = self.view.line(region)
line_contents = self.view.substr(line).rstrip()
rom = '^(\s*)' + re.escape(self.open_tasks_bullet) + '\s*(.*)$'
rdm = '^(\s*)' + re.escape(self.done_tasks_bullet) + '\s*([^\b]*?)\s*(%s)?[\(\)\d\w,\.:\-/ ]*\s*$' % self.done_tag
rcm = '^(\s*)' + re.escape(self.canc_tasks_bullet) + '\s*([^\b]*?)\s*(%s)?[\(\)\d\w,\.:\-/ ]*\s*$' % self.canc_tag
open_matches = re.match(rom, line_contents)
done_matches = re.match(rdm, line_contents)
canc_matches = re.match(rcm, line_contents)
if open_matches:
grps = open_matches.groups()
self.view.insert(edit, line.end(), done_line_end)
replacement = u'%s%s %s' % (grps[0], self.done_tasks_bullet, grps[1].rstrip())
self.view.replace(edit, line, replacement)
elif done_matches:
grps = done_matches.groups()
replacement = u'%s%s %s' % (grps[0], self.open_tasks_bullet, grps[1].rstrip())
self.view.replace(edit, line, replacement)
offset = -offset
elif canc_matches:
grps = canc_matches.groups()
self.view.insert(edit, line.end(), done_line_end)
replacement = u'%s%s %s' % (grps[0], self.done_tasks_bullet, grps[1].rstrip())
self.view.replace(edit, line, replacement)
offset = -offset
self.view.sel().clear()
for ind, pt in enumerate(original):
ofs = ind * offset
new_pt = sublime.Region(pt.a + ofs, pt.b + ofs)
self.view.sel().add(new_pt)
class PlainTasksCancelCommand(PlainTasksBase):
def runCommand(self, edit):
original = [r for r in self.view.sel()]
canc_line_end = ' %s %s' % (self.canc_tag, datetime.now().strftime(self.date_format))
offset = len(canc_line_end)
for region in self.view.sel():
line = self.view.line(region)
line_contents = self.view.substr(line).rstrip()
rom = '^(\s*)' + re.escape(self.open_tasks_bullet) + '\s*(.*)$'
rdm = '^(\s*)' + re.escape(self.done_tasks_bullet) + '\s*([^\b]*?)\s*(%s)?[\(\)\d\w,\.:\-/ ]*\s*$' % self.done_tag
rcm = '^(\s*)' + re.escape(self.canc_tasks_bullet) + '\s*([^\b]*?)\s*(%s)?[\(\)\d\w,\.:\-/ ]*\s*$' % self.canc_tag
open_matches = re.match(rom, line_contents)
done_matches = re.match(rdm, line_contents)
canc_matches = re.match(rcm, line_contents)
if open_matches:
grps = open_matches.groups()
self.view.insert(edit, line.end(), canc_line_end)
replacement = u'%s%s %s' % (grps[0], self.canc_tasks_bullet, grps[1].rstrip())
self.view.replace(edit, line, replacement)
elif done_matches:
pass
# grps = done_matches.groups()
# replacement = u'%s%s %s' % (grps[0], self.canc_tasks_bullet, grps[1].rstrip())
# self.view.replace(edit, line, replacement)
# offset = -offset
elif canc_matches:
grps = canc_matches.groups()
replacement = u'%s%s %s' % (grps[0], self.open_tasks_bullet, grps[1].rstrip())
self.view.replace(edit, line, replacement)
offset = -offset
self.view.sel().clear()
for ind, pt in enumerate(original):
ofs = ind * offset
new_pt = sublime.Region(pt.a + ofs, pt.b + ofs)
self.view.sel().add(new_pt)
class PlainTasksArchiveCommand(PlainTasksBase):
def runCommand(self, edit):
rdm = '^(\s*)' + re.escape(self.done_tasks_bullet) + '\s*([^\b]*?)\s*(%s)?[\(\)\d\.:\-/ ]*[ \t]*$' % self.done_tag
rcm = '^(\s*)' + re.escape(self.canc_tasks_bullet) + '\s*([^\b]*?)\s*(%s)?[\(\)\d\.:\-/ ]*[ \t]*$' % self.canc_tag
# finding archive section
archive_pos = self.view.find('Archive:', 0, sublime.LITERAL)
done_tasks = []
done_task = self.view.find(rdm, 0)
# print(done_task)
while done_task and (not archive_pos or done_task < archive_pos):
done_tasks.append(done_task)
done_task = self.view.find(rdm, done_task.end() + 1)
canc_tasks = []
canc_task = self.view.find(rcm, 0)
# print(canc_task)
while canc_task and (not archive_pos or canc_task < archive_pos):
canc_tasks.append(canc_task)
canc_task = self.view.find(rcm, canc_task.end() + 1)
all_tasks = done_tasks + canc_tasks
all_tasks.sort()
if all_tasks:
if archive_pos:
line = self.view.full_line(archive_pos).end()
else:
self.view.insert(edit, self.view.size(), u'\n\n___________________\nArchive:\n')
line = self.view.size()
# adding tasks to archive section
self.view.insert(edit, line, '\n'.join(self.before_tasks_bullet_spaces + self.view.substr(task).lstrip() for task in all_tasks) + '\n')
# remove moved tasks (starting from the last one otherwise it screw up regions after the first delete)
for task in reversed(all_tasks):
self.view.erase(edit, self.view.full_line(task))
class PlainTasksNewTaskDocCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.new_file()
view.set_syntax_file(SYNTAX_FILE_NAME)
class PlainTasksOpenUrlCommand(sublime_plugin.TextCommand):
#It is horrible regex but it works perfectly
URL_REGEX = r"""(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))
+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))"""
def run(self, edit):
s = self.view.sel()[0]
# expand selection to possible URL
start = s.a
end = s.b
# expand selection to nearest stopSymbols
view_size = self.view.size()
stopSymbols = ['\t', ' ', '\"', '\'', '>', '<', ',']
# move the selection back to the start of the url
while (start > 0
and not self.view.substr(start - 1) in stopSymbols
and self.view.classify(start) & sublime.CLASS_LINE_START == 0):
start -= 1
# move end of selection forward to the end of the url
while (end < view_size
and not self.view.substr(end) in stopSymbols
and self.view.classify(end) & sublime.CLASS_LINE_END == 0):
end += 1
# grab the URL
url = self.view.substr(sublime.Region(start, end))
# optional select URL
self.view.sel().add(sublime.Region(start, end))
exp = re.search(self.URL_REGEX, url, re.X)
if exp and exp.group(0):
strUrl = exp.group(0)
if strUrl.find("://") == -1:
strUrl = "http://" + strUrl
webbrowser.open_new_tab(strUrl)
else:
sublime.status_message("Looks like there is nothing to open")
class PlainTasksNewDayTaskDocCommand(PlainTasksBase):
"""Creates a new todo file based on the current open file. Archived tasks are ignored.
If the current view isn't a TODO, a new blank one view will be created."""
def run(self, edit):
#If the current view is a task file, create a new file based on it.
if (self.view.settings().get('syntax') == SYNTAX_FILE_NAME):
self.createNewDayTaskDocumentBasedOnCurrent()
else:
self.createEmptyNewDayTaskDocument()
def createEmptyNewDayTaskDocument(self):
#Create the new view
new_todo_file = self.view.window().new_file()
new_todo_file.set_syntax_file(SYNTAX_FILE_NAME)
#extension = new_todo_file.settings().get('default_new_day_extension')
#date_format_short = new_todo_file.settings().get('date_format_short')
# Assign a name to it, but the user will have to manually save the file...
#new_todo_file.set_name(datetime.now().strftime(date_format_short + "." + extension))
new_todo_file.set_name(datetime.now().strftime("%y-%m-%d-%A" + "." + "tasks"))
return new_todo_file
def createNewDayTaskDocumentBasedOnCurrent(self):
#Create an empty view.
new_task_view = self.createEmptyNewDayTaskDocument()
#Get all of the data before the archive section.
archive_pos = self.view.find('Archive:', 0, sublime.LITERAL)
edit = new_task_view.begin_edit()
#region = sublime.Region(0, archive_pos)
lines = self.view.lines(sublime.Region(0, archive_pos.end()))
#new_task_view.insert(edit, 0, new_task_view.substr(lines))
new_task_view.insert(edit, 0, '\n'.join(self.view.substr(line) for line in lines) + '\n')
new_task_view.end_edit(edit)