Skip to content

Commit

Permalink
refactored open link command
Browse files Browse the repository at this point in the history
  • Loading branch information
aziz committed Oct 19, 2015
1 parent 133df3b commit f7edacb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},

{ "keys": ["enter"], "command": "note_open_url", "context":
[{ "key": "selector", "operator": "equal", "operand": "text.html.markdown.note markup.underline.link.markdown" }]
[{ "key": "selector", "operator": "equal", "operand": "text.html.markdown.note meta.link.inline.markdown | text.html.markdown.note meta.link.inet.markdown" }]
},

// Notes: Jotter keymaps
Expand Down
36 changes: 7 additions & 29 deletions note_support.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
import sublime, sublime_plugin
import webbrowser


class NoteOpenUrlCommand(sublime_plugin.TextCommand):

def run(self, edit):
s = self.view.sel()[0]

# Expand selection to possible URL
start = s.a
end = s.b

view_size = self.view.size()
terminator = ['\t', ' ', '\"', '\'', '(', ')']

while (start > 0
and not self.view.substr(start - 1) in terminator
and self.view.classify(start) & sublime.CLASS_LINE_START == 0):
start -= 1

while (end < view_size
and not self.view.substr(end) in terminator
and self.view.classify(end) & sublime.CLASS_LINE_END == 0):
end += 1

# Check if this is URL
url = self.view.substr(sublime.Region(start, end))

if url.startswith(('http://', 'https://')):
webbrowser.open_new_tab(url)
v = self.view
s = v.sel()[0]
link_region = v.extract_scope(s.a)
url = v.substr(link_region)
webbrowser.open_new_tab(url)

def is_enabled(self):
is_note = sublime.active_window().active_view().settings().get("is_note")
if is_note:
return is_note
else:
return False
return 'Note.tmLanguage' in self.view.settings().get("syntax")

0 comments on commit f7edacb

Please sign in to comment.