-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtabs_extend.py
206 lines (162 loc) · 7.08 KB
/
tabs_extend.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
# -*- coding: utf-8 -*-
#
# Copyright © 2008, Éverton Ribeiro <[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 2 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, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
from gi.repository import GObject, Gtk, Gedit, Gdk
# UI Manager XML
ACTIONS_UI = """<ui>
<menubar name="MenuBar">
<menu name="FileMenu" action="File">
<placeholder name="FileOps_2">
<menuitem name="UndoClose" action="UndoClose"/>
</placeholder>
</menu>
</menubar>
<popup name="NotebookPopup" action="NotebookPopupAction">
<placeholder name="NotebookPupupOps_1">
<menuitem name="UndoClose" action="UndoClose"/>
<menuitem name="CloseAll" action="CloseAll"/>
<menuitem name="CloseOthers" action="CloseOthers"/>
</placeholder>
</popup>
</ui>"""
def lookup_widget(base, widget_name):
""" Find widget by name """
widgets = []
for widget in base.get_children():
if widget.get_name() == widget_name:
widgets.append(widget)
if isinstance(widget, Gtk.Container):
widgets += lookup_widget(widget, widget_name)
return widgets
class TabsExtendPlugin(GObject.Object, Gedit.WindowActivatable):
__gtype_name__ = "TabsExtendPlugin"
window = GObject.property(type=Gedit.Window)
handler_ids = []
tabs_closed = []
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
"""Activate plugin."""
self.notebook = self.get_notebook()
self.add_all()
self.handler_ids.append((self.notebook,
self.notebook.connect("page-added", self.tab_added_handler)))
self.handler_ids.append((self.notebook,
self.notebook.connect("page-removed", self.tab_removed_handler)))
self.add_actions()
def do_deactivate(self):
"""Deactivate plugin."""
for (widget, handler_id) in self.handler_ids:
widget.disconnect(handler_id)
data = self.window.get_data(self.__class__.__name__)
ui_manager = self.window.get_ui_manager()
ui_manager.remove_ui(data['ui_id'])
ui_manager.remove_action_group(data['action_group'])
ui_manager.ensure_update()
self.window.set_data(self.__class__.__name__, None)
self.notebook = None
self.handler_ids = []
def do_update_state(self):
"""Update the sensitivities of actions."""
windowdata = self.window.get_data(self.__class__.__name__)
undo_close = windowdata['action_group'].get_action('UndoClose')
undo_close.set_sensitive(len(self.tabs_closed) > 0)
close_all = windowdata['action_group'].get_action('CloseAll')
close_all.set_sensitive(self.notebook.get_n_pages() > 0)
close_others = windowdata['action_group'].get_action('CloseOthers')
close_others.set_sensitive(self.notebook.get_n_pages() > 1)
def add_actions(self):
undoclose = ('UndoClose', 'gtk-undo', 'Undo close', '<Ctrl><Shift>T',
'Open the folder containing the current document',
self.on_undo_close)
closeall = ('CloseAll', 'gtk-close', 'Close all', '<Ctrl><Shift>W',
'Open the folder containing the current document',
self.on_close_all)
closeothers = ('CloseOthers', 'gtk-close', 'Close others',
'<Ctrl><Shift>O', 'Open the folder containing the current document',
self.on_close_outher)
action_group = Gtk.ActionGroup(self.__class__.__name__)
action_group.add_actions([undoclose, closeall, closeothers])
ui_manager = self.window.get_ui_manager()
ui_manager.insert_action_group(action_group, 0)
ui_id = ui_manager.add_ui_from_string(ACTIONS_UI)
data = { 'action_group': action_group, 'ui_id': ui_id }
self.window.set_data(self.__class__.__name__, data)
self.do_update_state()
def get_notebook(self):
return lookup_widget(self.window, 'GeditNotebook')[0]
def add_all(self):
for x in range(self.notebook.get_n_pages()):
tab = self.notebook.get_nth_page(x)
self.add_middle_click_in_tab(tab)
def add_middle_click_in_tab(self, tab):
eventbox = self.notebook.get_tab_label(tab).get_children()[0]
handler_id = eventbox.connect("button-press-event",
self.middle_click_handler, tab)
self.handler_ids.append([eventbox, handler_id])
def middle_click_handler(self, widget, event, tab):
if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 2:
if self.notebook.get_n_pages():
self.notebook.prev_page()
self.window.close_tab(tab)
def tab_added_handler(self, widget, tab, *args):
self.add_middle_click_in_tab(tab)
def tab_removed_handler(self, widget, tab, *args):
self.save_tab_to_undo(tab)
self.do_update_state()
for (widget, handler_id) in self.handler_ids:
if widget == tab:
widget.disconnect(handler_id)
self.handler_ids.remove(handler_id)
break
def get_current_line(self, document):
""" Get current line for documento """
return document.get_iter_at_mark(document.get_insert()).get_line() + 1
# TODO: Save position tab
def save_tab_to_undo(self, tab):
""" Save close tabs """
document = tab.get_document()
if document.get_location() != None:
self.tabs_closed.append(
(document.get_location(), self.get_current_line(document))
)
def on_undo_close(self, action):
if len(self.tabs_closed) > 0:
uri, line = tab = self.tabs_closed[-1:][0]
if uri == None:
self.window.create_tab(True)
else:
self.window.create_tab_from_uri(uri, None, line, True, True)
self.tabs_closed.remove(tab)
self.do_update_state()
def on_close_all(self, action):
if self.notebook.get_n_pages() > 0:
self.window.close_all_tabs()
self.do_update_state()
def on_close_outher(self, action):
if self.notebook.get_n_pages() > 1:
dont_close = self.window.get_active_tab()
tabs = []
for x in range(self.notebook.get_n_pages()):
tab = self.notebook.get_nth_page(x)
if tab != dont_close:
tabs.append(tab)
tabs.reverse()
for tab in tabs:
self.window.close_tab(tab)
self.do_update_state()