Report abuse

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
#################################### IMPORTS ###################################

import sublime
import sublimeplugin

############################### HELPER FUNCTIONS ###############################

def find_all(view, search, start, end, flags=0):
    regions = [sublime.Region(start, start)]

    while True:
        regions.append(view.find(search, regions[-1].end(), flags))
        if not regions[-1] or regions[-1].begin() > end:
            break    

    return regions[1:-1]

################################### COMMANDS ###################################

class BookmarkArea(sublimeplugin.TextCommand):
    def run(self, view, args):
        sels = view.sel()
        start = sels[0].begin()
        end = sels[-1].end()

        sels.clear()

        for pos in (start, end):
            sels.add(sublime.Region(pos, pos))
            view.runCommand('toggleBookmark')
            sels.clear()

        sels.add(sublime.Region(start, start))

class SearchBetweenBookmarks(sublimeplugin.TextCommand):
    def run(self, view, args):
        if len(view.sel()) == 3:
            start, search_sel, end = view.sel()
        else:
            search_sel = view.sel()[0]

            view.runCommand('prevBookmark')
            start = view.sel()[0]

            view.runCommand('nextBookmark')
            end = view.sel()[-1]

        full_word = view.word(search_sel)
        search_sel = search_sel or full_word
        search = view.substr(search_sel)
        if search_sel == full_word:
            search = r'\b%s\b' % search        

        finds = find_all(view, search, start.begin(), end.end())

        view.sel().clear()
        for sel in [search_sel] + finds: 
            view.sel().add(sel)


################################################################################