forked from erinata/SublimeBullet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBullet.py
140 lines (131 loc) · 5.93 KB
/
Bullet.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
# -*- coding: UTF-8 -*-
import sublime
import sublime_plugin
import re
# TODO: Better multi line pasting behaviour (esp the bug that if you paste a 2 lines content, a * will be added to the end of the pasting )
# Just check whether the line is a empty line before inserting
# TODO: Can try to write to keymap files during start of sublime text. So the when one change the settings it will change the keymapping
# TODO: Write a text file in the plugin to indicate whether the settings are updated or not. When updated, write the keymap file accordingly.
# TODO: Also, put instruction text in the keymap file say they shouldn't manage the keymap file directly. If they really want to, they should put a false in the settings file "manage keymap autoamtically" part first.
class Bullet(sublime_plugin.EventListener):
Modifying = False
#is_markdown = False
md_enabled = False
rest_enabled = False
file_type = 0
last_line = 0
last_pos = 0
#selectors = []
selector_array = []
def __init__(self):
s = sublime.load_settings("Bullet.sublime-settings")
Bullet.md_enabled = s.get("markdown_bullet_enabled", True)
Bullet.rest_enabled = s.get("restructuredtext_bullet_enabled", False)
#Bullet.selector_array = selectors.split("|")
#Bullet.selector_array = ["text.html.markdown"]
def on_activated(self, view):
md_score = 0
rest_score = 0
if Bullet.md_enabled:
md_score = view.score_selector(0,'text.html.markdown')
if Bullet.rest_enabled:
rest_score = view.score_selector(0,'text.restructuredtext')
if md_score > 0 and md_score >= rest_score:
Bullet.file_type = 1
elif rest_score > 0 and rest_score >= md_score:
Bullet.file_type = 2
else:
Bullet.file_type = 0
#for x in range(len(Bullet.selector_array)):
# if (view.score_selector(0,Bullet.selector_array[x]) > 0):
# Bullet.is_markdown = True
# Bullet.update_row(self, view)
# return
# else:
# Bullet.is_markdown = False
def on_selection_modified(self, view):
#if (Bullet.is_markdown):
if (Bullet.file_type > 0):
Bullet.update_row(self, view)
def update_row(self, view):
loc = view.sel()[0]
Bullet.last_pos = loc.begin()
row, col = view.rowcol(Bullet.last_pos)
Bullet.last_line = row
def on_modified(self, view):
if Bullet.Modifying == False:
Bullet.Modifying = True
if Bullet.file_type == 1:
loc = view.sel()[0]
row, col = view.rowcol(loc.begin())
point_last_row = view.text_point(row - 1,0)
if (row - Bullet.last_line == 1):
previous_line = view.substr(view.line(Bullet.last_pos))
if row != 0 and previous_line != "":
match_pattern = re.search("^( *|\t*)(\*|\-|\>|\+|[0-9]+(\.))(.*)",previous_line)
if match_pattern != None:
if match_pattern.group(4) == " " or match_pattern.group(4) == "":
reg_remove = view.find("(\*|\-|\>|\+|[0-9]+\.)(.*)",point_last_row-1)
edit = view.begin_edit()
view.erase(edit,reg_remove)
view.end_edit(edit)
else:
if match_pattern.group(2) == "*":
insertion = "* "
elif match_pattern.group(2) == "-":
insertion = "- "
elif match_pattern.group(2) == ">":
insertion = "> "
elif match_pattern.group(2) == "+":
insertion = "+ "
else:
match_number = re.search("[0-9]+",match_pattern.group(2))
if match_number != None:
last_number = int(match_number.group(0))
insertion = str(last_number+1) + match_pattern.group(3) + " "
else:
insertion = ""
edit = view.begin_edit()
view.insert(edit, loc.end(), insertion)
view.end_edit(edit)
elif Bullet.file_type == 2:
loc = view.sel()[0]
row, col = view.rowcol(loc.begin())
point_last_row = view.text_point(row - 1,0)
if (row - Bullet.last_line == 1):
previous_line = view.substr(view.line(Bullet.last_pos))
if row != 0 and previous_line != "":
match_pattern = re.search(u"^( *|\t*)(\*|\-|\+|•|⁃|‣|#.|[0-9]+(\.))(.*)",previous_line)
if match_pattern != None:
if match_pattern.group(4) == " " or match_pattern.group(4) == "":
reg_remove = view.find(u"(\*|\-|\+|•|⁃|‣|#.|[0-9]+\.)(.*)",point_last_row-1)
edit = view.begin_edit()
view.erase(edit,reg_remove)
view.end_edit(edit)
else:
if match_pattern.group(2) == "*":
insertion = "* "
elif match_pattern.group(2) == "-":
insertion = "- "
elif match_pattern.group(2) == "+":
insertion = "+ "
elif match_pattern.group(2) == u"•":
insertion = u"• "
elif match_pattern.group(2) == u"⁃":
insertion = u"⁃ "
elif match_pattern.group(2) == u"‣":
insertion = u"‣ "
elif match_pattern.group(2) == "#.":
insertion = "#. "
else:
match_number = re.search("[0-9]+",match_pattern.group(2))
if match_number != None:
last_number = int(match_number.group(0))
insertion = str(last_number+1) + match_pattern.group(3) + " "
else:
insertion = ""
edit = view.begin_edit()
view.insert(edit, loc.end(), insertion)
view.end_edit(edit)
Bullet.update_row(self, view)
Bullet.Modifying = False