This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaderswitch.py
81 lines (68 loc) · 2.4 KB
/
headerswitch.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
#!/usr/bin/env python
#
# Copyright (C) 2016 Martin Willi
#
# 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 2 of the License, or (at your
# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
#
# 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.
import os
from gi.repository import GObject, Gtk, Gio, Gedit, GtkSource
class HeaderSwitchWindow(GObject.Object, Gedit.WindowActivatable):
__gtype_name__ = 'HeaderSwitchWindow'
window = GObject.property(type=Gedit.Window)
extpairs = [
[ '.c', '.h' ]
]
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
action = Gio.SimpleAction(name="headerswitch")
action.connect('activate', self.on_switch)
self.window.add_action(action)
def do_deactivate(self):
self.window.remove_action("headerswitch")
def do_update_state(self):
pass
def do_switch(self, path):
for doc in self.window.get_documents():
loc = doc.get_location()
if loc and path == loc.get_path():
tab = Gedit.Tab.get_from_document(doc)
if tab:
self.window.set_active_tab(tab)
return
self.window.create_tab_from_location(Gio.file_new_for_path(path),
None, 0, 0, True, True)
def on_switch(self, action, parameter, user_data=None):
doc = self.window.get_active_document()
if doc:
loc = doc.get_location()
if loc:
path = loc.get_path()
if path:
root, ext = os.path.splitext(path)
if ext:
for pair in self.extpairs:
if ext == pair[0]:
self.do_switch(root + pair[1])
if ext == pair[1]:
self.do_switch(root + pair[0])
class HeaderSwitchApp(GObject.Object, Gedit.AppActivatable):
__gtype_name__ = 'HeaderSwitchApp'
app = GObject.property(type=Gedit.App)
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
self.app.add_accelerator("<Ctrl>R", "win.headerswitch", None)
self.menu_ext = self.extend_menu("tools-section")
item = Gio.MenuItem.new("Switch .c/.h", "win.headerswitch")
self.menu_ext.append_menu_item(item)
def do_deactivate(self):
self.app.remove_accelerator("win.headerswitch", None)
self.menu_ext = None