forked from chagel/itodo
-
Notifications
You must be signed in to change notification settings - Fork 15
/
mdTodo.py
52 lines (44 loc) · 1.97 KB
/
mdTodo.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
from datetime import datetime
import sublime
import sublime_plugin
ALLOWED_FILETYPES = ('.md', '.markdown', '.mdown')
class MdTodoBase(sublime_plugin.TextCommand):
def run(self, edit):
filename = self.view.file_name()
# list of allowed filetypes
if not (filename or filename.endswith(ALLOWED_FILETYPES)):
return False
self.run(edit)
class MdTodoNewCommand(MdTodoBase):
def run(self, edit):
for region in self.view.sel():
lines = self.view.lines(region)
lines.reverse()
for line in lines:
# don't add a newline when creating new item with cursor is at an empty line
if not line:
line_contents = '- '
self.view.insert(edit, line.begin(), line_contents)
# add a newline when creating new item when cursor is at another line
else:
line_contents = self.view.substr(line) + '\n- '
self.view.replace(edit, line, line_contents)
class MdTodoDoneCommand(MdTodoBase):
def run(self, edit):
for region in self.view.sel():
lines = self.view.lines(region)
lines.reverse()
for line in lines:
line_head = self.view.find("[-\+]", line.begin())
line_contents = self.view.substr(line).strip()
# prepend @done if item is ongoing
if line_contents.startswith('-'):
self.view.insert(edit,
line.end(), " @done (%s)" %
datetime.now().strftime("%Y-%m-%d %H:%M"))
self.view.replace(edit, line_head, "+")
# undo @todo
elif line_contents.startswith('+'):
subfix = self.view.find('(\s)*@done(.)+\)$', line.begin())
self.view.erase(edit, subfix)
self.view.replace(edit, line_head, "-")