-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRegExLink.py
120 lines (101 loc) · 4.16 KB
/
RegExLink.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sublime
import sublime_plugin
import webbrowser
import subprocess
import shlex
import os
REGION_NAME = "RegExLink"
class RegExLinkEventCommand(sublime_plugin.EventListener):
def on_activated_async(self, view):
self._highlight(view)
def on_post_save_async(self, view):
self._highlight(view)
def on_load(self, view):
self._highlight(view)
def _highlight(self, view):
settings = sublime.load_settings("RegExLink.sublime-settings")
regex_link_def = settings.get('regex_link_def')
regex_link_mark_style = settings.get('regex_link_mark_style')
regex_link_outlines = settings.get('regex_link_outlines', False)
flags = sublime.DRAW_NO_FILL
if not regex_link_outlines:
flags = sublime.HIDDEN
for link_def in regex_link_def:
if 'link' in link_def:
extract = []
result = view.find_all(
link_def['regex'], 0, link_def['link'], extract)
for sel in view.sel():
view.add_regions(
REGION_NAME +
link_def['name'], result, link_def['style'],
regex_link_mark_style, flags)
if 'command' in link_def:
extract = []
result = view.find_all(
link_def['regex'], 0, link_def['command'], extract)
for sel in view.sel():
view.add_regions(
REGION_NAME +
link_def['name'], result, link_def['style'],
regex_link_mark_style, flags)
class RegExLinkCommand(sublime_plugin.TextCommand):
contentmenu = ""
def run(self, edit):
settings = sublime.load_settings("RegExLink.sublime-settings")
regex_link_def = settings.get('regex_link_def')
currfolder = sublime.expand_variables(
"$folder", sublime.active_window().extract_variables())
if (currfolder is not None and currfolder != ''):
os.chdir(currfolder)
for link_def in regex_link_def:
extract = []
if 'link' in link_def:
linktype = 'link'
elif 'command' in link_def:
linktype = 'command'
else:
continue
result = self.view.find_all(
link_def['regex'], 0, link_def[linktype], extract)
for sel in self.view.sel():
for region in zip(result, extract):
if sel.b >= region[0].a and sel.a <= region[0].b:
if linktype == 'link':
webbrowser.open(region[1])
elif linktype == 'command':
command = shlex.split(region[1])
try:
subprocess.Popen(command)
except:
sublime.error_message(
"Error executing: \n\n" + " ".join(command))
def is_visible(self, paths=None):
if self.contentmenu != "":
return True
else:
return False
def description(self):
settings = sublime.load_settings("RegExLink.sublime-settings")
regex_link_def = settings.get('regex_link_def')
for link_def in regex_link_def:
extract = []
if 'link' in link_def:
linktype = 'link'
elif 'command' in link_def:
linktype = 'command'
else:
continue
result = self.view.find_all(
link_def['regex'], 0, link_def[linktype], extract)
for sel in self.view.sel():
for region in zip(result, extract):
if sel.b >= region[0].a and sel.a <= region[0].b:
self.contentmenu = link_def[
'name'] + " " + self.view.substr(region[0])
return "Open " + self.contentmenu
else:
self.contentmenu = ""
return "Open " + self.contentmenu