-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbp_mark.py
95 lines (70 loc) · 2.79 KB
/
sbp_mark.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
import sublime
import sublime_plugin
# Remove any existing marks
#
class SbpCancelMarkCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
m = self.view.get_regions("mark")
if m:
self.view.erase_regions("mark")
self.view.sel().clear()
self.view.sel().add(sublime.Region(m[0].end(), m[0].end()))
class SbpSetMarkCommand(sublime_plugin.TextCommand):
def run(self, edit):
mark = [s for s in self.view.sel()]
self.view.add_regions("mark", mark, "mark", "dot",
sublime.HIDDEN | sublime.PERSISTENT)
class SbpSwapWithMarkCommand(sublime_plugin.TextCommand):
def run(self, edit):
old_mark = self.view.get_regions("mark")
mark = [s for s in self.view.sel()]
self.view.add_regions("mark", mark, "mark", "dot",
sublime.HIDDEN | sublime.PERSISTENT)
if len(old_mark):
self.view.sel().clear()
for r in old_mark:
self.view.sel().add(r)
class SbpSelectToMarkCommand(sublime_plugin.TextCommand):
def run(self, edit):
mark = self.view.get_regions("mark")
num = min(len(mark), len(self.view.sel()))
regions = []
for i in xrange(num):
regions.append(self.view.sel()[i].cover(mark[i]))
for i in xrange(num, len(self.view.sel())):
regions.append(self.view.sel()[i])
self.view.sel().clear()
for r in regions:
self.view.sel().add(r)
class SbpDeleteToMark(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command("copy")
self.view.run_command("sbp_add_to_kill_ring", {"forward": False})
#self.view.run_command("sbp_select_to_mark")
self.view.run_command("left_delete")
self.view.run_command("sbp_cancel_mark")
#
# If a mark has been set, color the region between the mark and the point
#
class SbpEmacsMarkDetector(sublime_plugin.EventListener):
def __init__(self, *args, **kwargs):
sublime_plugin.EventListener.__init__(self, *args, **kwargs)
# When text is modified, we cancel the mark.
def on_modified(self, view):
#view.erase_regions("mark")
pass
def on_selection_modified(self, view):
mark = view.get_regions("mark")
num = min(len(mark), len(view.sel()))
regions = []
for i in xrange(num):
regions.append(view.sel()[i].cover(mark[i]))
for i in xrange(num, len(view.sel())):
regions.append(view.sel()[i])
view.sel().clear()
for r in regions:
view.sel().add(r)
def on_query_context(self, view, key, operator, operand, match_all):
if key == "sbp_emacs_has_mark":
if operator == sublime.OP_EQUAL:
return len(view.get_regions("mark")) > 0