/
gedit-repl.py
397 lines (314 loc) · 14 KB
/
gedit-repl.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# -*- coding: utf8 -*-
# terminal.py - Embeded VTE terminal for gedit
# This file is part of gedit
#
# Copyright (C) 2005-2006 - Paolo Borelli
#
# gedit 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 2 of the License, or
# (at your option) any later version.
#
# gedit 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 gedit; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA
import gi
gi.require_version('Vte', '2.90')
from gi.repository import GObject, GLib, Gio, Pango, Gdk, Gtk, Gedit, Vte
import os
import gettext
from gpdefs import *
try:
gettext.bindtextdomain(GETTEXT_PACKAGE, GP_LOCALEDIR)
_ = lambda s: gettext.dgettext(GETTEXT_PACKAGE, s);
except:
_ = lambda s: s
class GeditTerminal(Vte.Terminal):
defaults = {
'emulation' : 'xterm',
'visible_bell' : False,
}
TARGET_URI_LIST = 200
def __init__(self):
Vte.Terminal.__init__(self)
self.set_size(self.get_column_count(), 5)
self.set_size_request(200, 50)
tl = Gtk.TargetList.new([])
tl.add_uri_targets(self.TARGET_URI_LIST)
self.drag_dest_set(Gtk.DestDefaults.HIGHLIGHT | Gtk.DestDefaults.DROP,
[], Gdk.DragAction.DEFAULT | Gdk.DragAction.COPY)
self.drag_dest_set_target_list(tl)
self.profile_settings = self.get_profile_settings()
self.profile_settings.connect("changed", self.on_profile_settings_changed)
self.system_settings = Gio.Settings.new("org.gnome.desktop.interface")
self.system_settings.connect("changed::monospace-font-name", self.font_changed)
self.reconfigure_vte()
self.fork_command_full(Vte.PtyFlags.DEFAULT, None, [Vte.get_user_shell()], None, GLib.SpawnFlags.SEARCH_PATH, None, None)
def do_drag_data_received(self, drag_context, x, y, data, info, time):
if info == self.TARGET_URI_LIST:
self.feed_child(' '.join(["'" + Gio.file_new_for_uri(item).get_path() + "'" for item in Gedit.utils_drop_get_uris(data)]), -1)
Gtk.drag_finish(drag_context, True, False, time);
else:
Vte.Terminal.do_drag_data_received(self, drag_context, x, y, data, info, time)
def settings_try_new(self, schema):
schemas = Gio.Settings.list_schemas()
if not schemas:
return None
for s in schemas:
if s == schema:
return Gio.Settings.new(schema)
return None
def get_profile_settings(self):
profiles = self.settings_try_new("org.gnome.Terminal.ProfilesList")
if profiles:
default_path = "/org/gnome/terminal/legacy/profiles:/:" + profiles.get_string("default") + "/"
settings = Gio.Settings.new_with_path("org.gnome.Terminal.Legacy.Profile",
default_path)
else:
settings = Gio.Settings.new("org.gnome.gedit.plugins.terminal")
return settings
def get_font(self):
if self.profile_settings.get_boolean("use-system-font"):
font = self.system_settings.get_string("monospace-font-name")
else:
font = self.profile_settings.get_string("font")
return font
def font_changed(self, settings=None, key=None):
font = self.get_font()
font_desc = Pango.font_description_from_string(font)
self.set_font(font_desc)
def reconfigure_vte(self):
# Fonts
self.font_changed()
# colors
context = self.get_style_context()
fg = context.get_color(Gtk.StateFlags.NORMAL)
bg = context.get_background_color(Gtk.StateFlags.NORMAL)
palette = []
if not self.profile_settings.get_boolean("use-theme-colors"):
fg_color = self.profile_settings.get_string("foreground-color")
if fg_color != "":
fg = Gdk.RGBA()
parsed = fg.parse(fg_color)
bg_color = self.profile_settings.get_string("background-color")
if bg_color != "":
bg = Gdk.RGBA()
parsed = bg.parse(bg_color)
str_colors = self.profile_settings.get_strv("palette")
if str_colors:
for str_color in str_colors:
try:
rgba = Gdk.RGBA()
rgba.parse(str_color)
palette.append(rgba)
except:
palette = []
break
self.set_colors_rgba(fg, bg, palette)
self.set_cursor_blink_mode(self.profile_settings.get_enum("cursor-blink-mode"))
self.set_cursor_shape(self.profile_settings.get_enum("cursor-shape"))
self.set_audible_bell(self.profile_settings.get_boolean("audible-bell"))
self.set_allow_bold(self.profile_settings.get_boolean("allow-bold"))
self.set_scroll_on_keystroke(self.profile_settings.get_boolean("scroll-on-keystroke"))
self.set_scroll_on_output(self.profile_settings.get_boolean("scroll-on-output"))
self.set_word_chars(self.profile_settings.get_string("word-chars"))
self.set_emulation(self.defaults['emulation'])
self.set_visible_bell(self.defaults['visible_bell'])
if self.profile_settings.get_boolean("scrollback-unlimited"):
lines = -1
else:
lines = self.profile_settings.get_int("scrollback-lines")
self.set_scrollback_lines(lines)
def on_profile_settings_changed(self, settings, key):
self.reconfigure_vte()
class GeditTerminalPanel(Gtk.Box):
"""VTE terminal which follows gnome-terminal default profile options"""
__gsignals__ = {
"populate-popup": (
GObject.SIGNAL_RUN_LAST,
None,
(GObject.TYPE_OBJECT,)
)
}
def __init__(self):
Gtk.Box.__init__(self)
self._accel_base = '<gedit>/plugins/terminal'
self._accels = {
'copy-clipboard': [Gdk.KEY_C, Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK, self.copy_clipboard],
'paste-clipboard': [Gdk.KEY_V, Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK, self.paste_clipboard]
}
for name in self._accels:
path = self._accel_base + '/' + name
accel = Gtk.AccelMap.lookup_entry(path)
if not accel[0]:
Gtk.AccelMap.add_entry(path, self._accels[name][0], self._accels[name][1])
self.add_terminal()
def add_terminal(self):
self._vte = GeditTerminal()
self._vte.show()
self.pack_start(self._vte, True, True, 0)
self._vte.connect("child-exited", self.on_vte_child_exited)
self._vte.connect("key-press-event", self.on_vte_key_press)
self._vte.connect("button-press-event", self.on_vte_button_press)
self._vte.connect("popup-menu", self.on_vte_popup_menu)
scrollbar = Gtk.Scrollbar.new(Gtk.Orientation.VERTICAL, self._vte.get_vadjustment())
scrollbar.show()
self.pack_start(scrollbar, False, False, 0)
def on_vte_child_exited(self, term):
for child in self.get_children():
child.destroy()
self.add_terminal()
self._vte.grab_focus()
def do_grab_focus(self):
self._vte.grab_focus()
def on_vte_key_press(self, term, event):
modifiers = event.state & Gtk.accelerator_get_default_mod_mask()
if event.keyval in (Gdk.KEY_Tab, Gdk.KEY_KP_Tab, Gdk.KEY_ISO_Left_Tab):
if modifiers == Gdk.ModifierType.CONTROL_MASK:
self.get_toplevel().child_focus(Gtk.DirectionType.TAB_FORWARD)
return True
elif modifiers == Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK:
self.get_toplevel().child_focus(Gtk.DirectionType.TAB_BACKWARD)
return True
for name in self._accels:
path = self._accel_base + '/' + name
entry = Gtk.AccelMap.lookup_entry(path)
if entry and entry[0] and entry[1].accel_key == event.keyval and entry[1].accel_mods == modifiers:
self._accels[name][2]()
return True
keyval_name = Gdk.keyval_name(Gdk.keyval_to_upper(event.keyval))
# Special case some Vte.Terminal shortcuts
# so the global shortcuts do not override them
if modifiers == Gdk.ModifierType.CONTROL_MASK and keyval_name in 'ACDEHKLRTUWZ':
return False
if modifiers == Gdk.ModifierType.MOD1_MASK and keyval_name in 'BF':
return False
return Gtk.accel_groups_activate(self.get_toplevel(),
event.keyval, modifiers)
def on_vte_button_press(self, term, event):
if event.button == 3:
self._vte.grab_focus()
self.make_popup(event)
return True
return False
def on_vte_popup_menu(self, term):
self.make_popup()
def create_popup_menu(self):
menu = Gtk.Menu()
item = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_COPY, None)
item.connect("activate", lambda menu_item: self.copy_clipboard())
item.set_accel_path(self._accel_base + '/copy-clipboard')
item.set_sensitive(self._vte.get_has_selection())
menu.append(item)
item = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_PASTE, None)
item.connect("activate", lambda menu_item: self.paste_clipboard())
item.set_accel_path(self._accel_base + '/paste-clipboard')
menu.append(item)
self.emit("populate-popup", menu)
menu.show_all()
return menu
def make_popup(self, event = None):
menu = self.create_popup_menu()
menu.attach_to_widget(self, None)
if event is not None:
menu.popup(None, None, None, None, event.button, event.time)
else:
menu.popup(None, None,
lambda m: Gedit.utils_menu_position_under_widget(m, self),
None,
0, Gtk.get_current_event_time())
menu.select_first(False)
def copy_clipboard(self):
self._vte.copy_clipboard()
self._vte.grab_focus()
def paste_clipboard(self):
self._vte.paste_clipboard()
self._vte.grab_focus()
def change_directory(self, path):
path = path.replace('\\', '\\\\').replace('"', '\\"')
self._vte.feed_child('cd "%s"\n' % path, -1)
self._vte.grab_focus()
def send_command(self, cmd):
self._vte.feed_child('%s\n' % cmd, -1)
class TerminalPlugin(GObject.Object, Gedit.WindowActivatable):
__gtype_name__ = "TerminalPlugin"
window = GObject.property(type=Gedit.Window)
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
self._panel = GeditTerminalPanel()
self._panel.connect("populate-popup", self.on_panel_populate_popup)
self._panel.show()
bottom = self.window.get_bottom_panel()
bottom.add_titled(self._panel, "GeditTerminalPanel", _("Terminal"))
action = Gio.SimpleAction(name="sendcmd")
action.connect('activate', self.on_send_selection)
self.window.add_action(action)
self.window.connect('key-press-event', self.on_key_press)
def do_deactivate(self):
bottom = self.window.get_bottom_panel()
bottom.remove(self._panel)
self.remove_action("sendcmd")
def do_update_state(self):
pass
def get_active_document_directory(self):
doc = self.window.get_active_document()
if doc:
location = doc.get_location()
if location and location.has_uri_scheme("file"):
directory = location.get_parent()
return directory.get_path()
return None
def on_panel_populate_popup(self, panel, menu):
menu.prepend(Gtk.SeparatorMenuItem())
path = self.get_active_document_directory()
item = Gtk.MenuItem.new_with_mnemonic(_("C_hange Directory"))
item.connect("activate", lambda menu_item: panel.change_directory(path))
item.set_sensitive(path is not None)
menu.prepend(item)
def on_send_selection(self, action=None, parameter=None, user_data=None):
doc = self.window.get_active_document()
if doc.get_has_selection():
start, end = doc.get_selection_bounds()
code = start.get_text(end)
else:
cursor = doc.get_insert()
start = doc.get_iter_at_mark(cursor)
start.set_line_offset(0)
if start.ends_line():
code = ""
doc.goto_line(start.get_line()+1)
else:
end = start.copy()
end.forward_to_line_end()
code = start.get_text(end)
doc.goto_line(start.get_line()+1)
self._panel.send_command(code)
def on_key_press(self, input_view, event):
if event.state == Gdk.ModifierType.CONTROL_MASK and event.keyval == Gdk.KEY_Return:
self.on_send_selection()
return True
else:
return False
class SynctexAppActivatable(GObject.Object, Gedit.AppActivatable):
app = GObject.property(type=Gedit.App)
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
# This is just to display the accel in the menu
# <Control>Return connected to keypress event
self.app.add_accelerator("<Control>Return", "win.sendcmd", None)
self.menu_ext = self.extend_menu("tools-section")
item = Gio.MenuItem.new(_("Send to Terminal"), "win.sendcmd")
self.menu_ext.append_menu_item(item)
def do_deactivate(self):
self.app.remove_accelerator("win.sendcmd", None)
self.menu_ext = None
# Let's conform to PEP8
# ex:ts=4:et: