forked from aziz/PlainTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlainTasks.py
executable file
·209 lines (185 loc) · 9.64 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import sublime
import sublime_plugin
from datetime import datetime
import urllib
import getpass
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')
self.date_format = self.view.settings().get('date_format')
if self.view.settings().get('done_tag'):
self.done_tag = "@done"
self.canc_tag = "@cancelled"
else:
self.done_tag = ""
self.canc_tag = ""
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()
#
# this part added by me
#
project_name = None
project_pos = self.view.find('@project ', 0, sublime.LITERAL)
if project_pos:
project_line = self.view.line(project_pos)
project_name = self.view.substr(project_line).rstrip()
project_name = project_name.replace('@project ', '')
self.post_to_grove(line_contents[2:], project_name)
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)
def post_to_grove(self, task, project):
username = getpass.getuser()
grove_key = self.view.settings().get('grove_key')
message = 'done: ' + task + ' - ' + username
if project is not None:
message = message + ' (' + project + ')'
params = {}
params['service'] = 'TaskBot'
params['message'] = message
params = urllib.urlencode(params)
urllib.urlopen('https://grove.io/api/notice/' + grove_key, params)
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('Packages/PlainTasks/PlainTasks.tmLanguage')