-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathopen-uri-context-menu.py
217 lines (180 loc) · 6.84 KB
/
open-uri-context-menu.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Copyright (C) 2007-2008 Martin Szulecki
# Copyright (C) 2011 Jean-Philippe Fleury <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
Adds context menu item to open an URI at the pointer position
'''
from gettext import gettext as _
from gi.repository import Gtk, Gedit, Gio, GObject, GtkSource
import re
import sys
import os
import subprocess
import string
ACCEPTED_SCHEMES = ['file', 'ftp', 'sftp', 'smb', 'dav', 'davs', 'ssh', 'http', 'https']
RE_DELIM = re.compile(r'[\w#/\?:%@&\=\+\.\\~-]+', re.UNICODE|re.MULTILINE)
RE_URI_RFC2396 = re.compile("((([a-zA-Z][0-9a-zA-Z+\\-\\.]*):)?/{0,2}([0-9a-zA-Z;:,/\?@&=\+\$\.\-_!~\*'\(\)%]+))?(#[0-9a-zA-Z;,/\?:@&\=+$\.\\-_!~\*'\(\)%]+)?")
class OpenURIContextMenuPlugin(GObject.Object, Gedit.WindowActivatable):
__gtype_name__ = "OpenURIContextMenuPlugin"
window = GObject.property(type=Gedit.Window)
def __init__(self):
GObject.Object.__init__(self)
self.uri = ""
self.window = None
self.encoding = GtkSource.Encoding.get_from_charset("UTF-8")
def do_activate(self):
handler_ids = []
for signal in ('tab-added', 'tab-removed'):
method = getattr(self, 'on_window_' + signal.replace('-', '_'))
handler_ids.append(self.window.connect(signal, method))
self.window.OpenURIContextMenuPluginID = handler_ids
for view in self.window.get_views():
self.connect_view(view)
def do_deactivate(self):
widgets = [self.window] + self.window.get_views()
for widget in widgets:
handler_ids = widget.OpenURIContextMenuPluginID
if not handler_ids is None:
for handler_id in handler_ids:
widget.disconnect(handler_id)
widget.OpenURIContextMenuPluginID = None
self.window = None
def connect_view(self, view):
handler_id = view.connect('populate-popup', self.on_view_populate_popup)
view.OpenURIContextMenuPluginID = [handler_id]
def update_ui(self, window):
pass
def browse_url(self, menu_item, url):
command = ['xdg-open', url]
# Avoid to run the browser as user root
if os.getuid() == 0 and os.environ.has_key('SUDO_USER'):
command = ['sudo', '-u', os.environ['SUDO_USER']] + command
subprocess.Popen(command)
def on_window_tab_added(self, window, tab):
self.connect_view(tab.get_view())
def on_window_tab_removed(self, window, tab):
pass
def on_view_populate_popup(self, view, menu):
doc = view.get_buffer()
win = view.get_window(Gtk.TextWindowType.TEXT);
ptr_window, x, y, mod = win.get_pointer()
x, y = view.window_to_buffer_coords(Gtk.TextWindowType.TEXT, x, y);
# First try at pointer location
# Starting with GTK 3.20, get_iter_at_location returns a tuple of type
# (gboolean, GtkTextIter). Earlier versions return only the GtkTextIter.
insert = view.get_iter_at_location(x, y);
if isinstance(insert, tuple):
insert = insert[1] if insert[0] else None
# Second try at cursor
if insert == None:
insert = doc.get_iter_at_mark(doc.get_insert())
if isinstance(insert, tuple):
insert = insert[1] if insert[0] else None
while insert.forward_char():
if not RE_DELIM.match(insert.get_char()):
break
start = insert.copy()
while start.backward_char():
if not RE_DELIM.match(start.get_char()):
start.forward_char();
break
word = doc.get_text(start, insert, False)
if len(word) == 0:
return True
word = self.validate_uri(word)
if not word:
return True
displayed_word = word
if len(displayed_word) > 50:
displayed_word = displayed_word[:50] + u"\u2026"
browse_to = False
if word.startswith("http://") or word.startswith("https://"):
browse_to = True
if browse_to:
browse_uri_item = Gtk.ImageMenuItem(_("Browse to '%s'") % (displayed_word))
browse_uri_item.set_image(Gtk.Image.new_from_stock(Gtk.STOCK_JUMP_TO,
Gtk.IconSize.MENU))
browse_uri_item.connect('activate', self.browse_url, word);
browse_uri_item.show();
displayed_word = displayed_word.replace('file://', '')
open_uri_item = Gtk.ImageMenuItem(_("Open '%s'") % (displayed_word))
open_uri_item.set_image(Gtk.Image.new_from_stock(Gtk.STOCK_OPEN, Gtk.IconSize.MENU))
open_uri_item.connect('activate', self.on_open_uri_activate, word);
open_uri_item.show();
separator = Gtk.SeparatorMenuItem()
separator.show();
menu.prepend(separator)
menu.prepend(open_uri_item)
if browse_to:
menu.prepend(browse_uri_item)
return True
def on_open_uri_activate(self, menu_item, uri):
self.open_uri(uri)
return True
def validate_uri(self, uri):
m = RE_URI_RFC2396.search(uri);
if not m:
return False
target = m.group()
if m.group(4) == None or m.group(4) == "/":
return False
if m.group(2) != None:
if m.group(3) in ACCEPTED_SCHEMES:
return target
else:
return False
else:
if m.group(4).startswith("www."):
return 'http://' + target
target = os.path.expanduser(target)
if os.path.isfile(target):
if os.path.isabs(target):
return 'file://' + target
doc_dir = self.window.get_active_document().get_uri_for_display()
if doc_dir != None:
if doc_dir.startswith('file://'):
f = os.path.join(os.path.dirname(doc_dir), target)
if os.path.isfile(f.replace('file://', '', 1)):
return f
else:
return os.path.join(os.path.dirname(doc_dir), target)
paths = string.split(os.environ["PATH"], os.pathsep)
for dirname in paths:
f = os.path.join(os.path.dirname(dirname), 'include', target)
if os.path.isfile(f):
return 'file://' + f
return False
def get_document_by_uri(self, uri):
docs = self.window.get_documents()
for d in docs [:]:
if d.get_location() == uri:
return d
return None
def open_uri(self, uri):
doc = self.get_document_by_uri(uri)
if doc != None :
tab = Gedit.tab_get_from_document(doc)
self.window.set_active_tab(tab)
else:
self.window.create_tab_from_location(Gio.file_new_for_uri(uri),
self.encoding, 0, 0, False, True)
status = self.window.get_statusbar()
status_id = status.push(status.get_context_id("OpenURIContextMenuPlugin"),
_("Loading file '%s'...") % (uri))
GObject.timeout_add(4000, self.on_statusbar_timeout, status,
status.get_context_id("OpenURIContextMenuPlugin"), status_id)
def on_statusbar_timeout(self, status, context_id, status_id):
status.remove(context_id, status_id)
return False