forked from MicahCarrick/gedit-restore-tabs
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrestoretabs.py
102 lines (85 loc) · 3.75 KB
/
restoretabs.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
# Written by Micah Carrick.
# Distribute under GNU GPL 3.0 License.
import os
from gi.repository import GObject, GLib, Gtk, Gio, Gedit
SETTINGS_SCHEMA = "org.gnome.gedit.plugins.restoretabs"
class RestoreTabsWindowActivatable(GObject.Object, Gedit.WindowActivatable):
__gtype_name__ = "RestoreTabsWindowActivatable"
window = GObject.property(type=Gedit.Window)
def __init__(self):
GObject.Object.__init__(self)
self._handlers = []
def do_activate(self):
"""
Connect signal handlers.
"""
handlers = []
handler_id = self.window.connect("delete-event",
self.on_window_delete_event)
self._handlers.append(handler_id)
# temporary handler to catch the first time a window is shown
self._temp_handler = self.window.connect("show", self.on_window_show)
# handler to catch the Untitled Document tab
self.tab_handler_id = self.window.connect("tab-added", self.on_tab_added)
def do_deactivate(self):
"""
Disconect any signal handlers that were added in do_activate().
"""
[self.window.disconnect(handler_id) for handler_id in self._handlers]
def do_update_state(self):
pass
def is_first_window(self):
"""
Return True if the window being added is the first window instance.
"""
app = Gedit.App.get_default()
if len(app.get_windows()) <= 1:
return True
else:
return False
def on_window_delete_event(self, window, event, data=None):
uris = []
for document in window.get_documents():
gfile = document.get_file().get_location()
if gfile:
uris.append(gfile.get_uri())
settings = Gio.Settings.new(SETTINGS_SCHEMA)
settings.set_value('uris', GLib.Variant("as", uris))
return False
def on_window_show(self, window, data=None):
"""
Only restore tabs if this window is the first Gedit window instance.
"""
if self.is_first_window():
tab = self.window.get_active_tab()
if tab and tab.get_state() == 0 and not tab.get_document().get_file():
self.window.close_tab(tab)
settings = Gio.Settings.new(SETTINGS_SCHEMA)
uris = settings.get_value('uris')
if uris:
self.nbDocuments = len(uris)
for uri in uris:
location = Gio.file_new_for_uri(uri)
if location.query_exists():
tab = self.window.get_tab_from_location(location)
if not tab:
tab = self.window.create_tab(True) #create new tab and jump_to it
tab.load_file(location, None, 0, 0, True) #GFile *location,*encoding,line_pos,column_pos,create
else:
self.window.disconnect(self.tab_handler_id)
self.window.disconnect(self._temp_handler)
def on_tab_added(self, window, tab, data=None):
"""
Catch the default created Untitled Document and mark for deletion on idle.
Remove handler after first use.
"""
document = tab.get_document()
if document.get_file().get_location() is None and len(window.get_documents()) > self.nbDocuments:
# # crash with segfault
# #self.window.close_tab(tab)
# # workaround
source_id = GObject.idle_add(self.tabclose, tab)
self.window.disconnect(self.tab_handler_id)
def tabclose(self, tab):
self.window.close_tab(tab)
return False