-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcustom_keys.py
95 lines (79 loc) · 3.13 KB
/
custom_keys.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
from gi.repository import GObject, Gedit, Gdk
class ExampleAppActivatable(GObject.Object, Gedit.AppActivatable):
app = GObject.property(type=Gedit.App)
__gtype_name__ = "CustomKeysAppActivatable"
def do_activate(self):
# Uncomment/comment any of the example keyboard shortcuts below and edit as desired
# "<Primary>" normally corresponds to Ctrl key
# See README.md for a list of known action names
self.add_keyboard_shortcut("win.redo", "<Primary>Y")
self.add_keyboard_shortcut("win.goto-line", "<Primary>G")
#Examples:
#self.add_keyboard_shortcut("app.new-window", "<Primary>N")
#self.add_keyboard_shortcut("app.help", "F1")
#self.add_keyboard_shortcut("win.save-as", "<Primary><Shift>S")
#self.add_keyboard_shortcut("win.reopen-closed-tab", "<Primary><Shift>T")
#self.add_keyboard_shortcut("win.focus-active-view", "Escape")
#self.add_keyboard_shortcut("win.bottom-panel", "<Primary>F9")
#self.add_keyboard_shortcut("win.new-tab-group", "<Primary><Alt>N")
#self.add_keyboard_shortcut("win.previous-tab-group", "<Primary><Shift><Alt>Page_Up")
#self.add_keyboard_shortcut("win.next-tab-group", "<Primary><Shift><Alt>Page_Down")
def do_deactivate(self):
self.app.set_accels_for_action("win.redo", ())
def __init__(self):
GObject.Object.__init__(self)
def add_keyboard_shortcut(self, action_name, keyboard_shortcut):
self.app.remove_accelerator(action_name, None)
self.app.set_accels_for_action(action_name, [keyboard_shortcut])
# https://github.com/baxterross/GEdit3TabSwitch
class GEdit3TabSwitch(GObject.Object, Gedit.WindowActivatable):
window = GObject.property(type=Gedit.Window)
KEYS_LEFT = ('ISO_Left_Tab', 'Page_Up')
KEYS_RIGHT = ('Tab', 'Page_Down')
KEYS = KEYS_LEFT + KEYS_RIGHT
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
handlers = []
handler_id = self.window.connect('key-press-event', self.on_key_press_event)
handlers.append(handler_id)
self.window.GEdit3TabSwitchHandlers = handlers
def do_deactivate(self):
handlers = self.window.GEdit3TabSwitchHandlers
for handler_id in handlers:
self.window.disconnect(handler_id)
def do_update_state(self):
pass
def on_key_press_event(self, window, event):
key = Gdk.keyval_name(event.keyval)
# Ctrl+E - delete line(s)
if event.state & Gdk.ModifierType.CONTROL_MASK and key == 'e':
doc = self.window.get_active_document()
doc.begin_user_action()
ins_ln = doc.get_iter_at_mark(doc.get_insert()).get_line()
sel_ln = doc.get_iter_at_mark(doc.get_selection_bound()).get_line()
it_beg = doc.get_iter_at_line(min(ins_ln, sel_ln))
it_end = doc.get_iter_at_line(max(ins_ln, sel_ln))
it_end.forward_line()
doc.delete(it_beg, it_end)
doc.end_user_action()
# Ctrl+Tab / Ctrl+Shift+Tab
if event.state & Gdk.ModifierType.CONTROL_MASK and key in self.KEYS:
atab = window.get_active_tab()
tabs = atab.get_parent().get_children()
tlen = len(tabs)
i = 0
for tab in tabs:
i += 1
if tab == atab:
break
if key in self.KEYS_LEFT:
i -= 2
if i < 0:
tab = tabs[tlen-1]
elif i >= tlen:
tab = tabs[0]
else:
tab = tabs[i]
window.set_active_tab(tab)
return True