Untitled Document added on reopening
The plugin works great, just one issue. Every time I reopen gedit, besides the saved tabs from the last session, another tab is opened with a new, empty document called Untitled Document 1. I just close that tab and start using gedit, however it would be desired if this new tab would not be opened.
In the schema org.gnome.gedit.plugins.restoretabs (viewed with dconf-editor) the Untitled Document is not there, just the regular saved tabs.
Gedit 3.18.3, Ubuntu 16.04, gedit-restore-tabs 3.0.0 forked version by @raelgc (https://github.com/raelgc/gedit-restore-tabs/commit/1f67d107b44e8f9ba40c847c8deeb63ef2dd16e4), Python 3.5.2.
After looking in the code, I found that the Untitled Document tab is created after on_window_show is finished. Therefore it is not seen by the plugin and it is not deleted (however I figure that the original code intended to delete this tab).
I tried to add a listener for added tabs and delete the Untitled Document as soon as it is added. However, this causes a segfault, presumably the tab is not ready for deletion when it is being added. As a workaround I added a callback on idle to remove the tab. The listener and the callback are both removed after single use to allow empty tabs being opened in normal operation.
This works, but it is a bit slow: gedit is opened with the Untitled Document, and after ~1sec the idle is called and the document is removed. I hope to find a better solution, suggestions welcome.
Relevant code (I might fork when I have a better solution):
def do_activate(self):
(...)
# handler to catch the Untitled Document tab
self.tab_handler_id = self.window.connect("tab-added", self.on_tab_added)
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.is_untitled():
# 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
Interesting also keeping the Untitled file if not previous file to open. I added the code to this propose
# handler to catch the Untitled Document tab
settings = Gio.Settings.new(SETTINGS_SCHEMA)
uris = settings.get_value('uris')
if uris:
self.tab_handler_id = self.window.connect("tab-added", self.on_tab_added)
I was able to resolve more simply...
self._temp_handler = self.window.connect("show", self.on_window_show)
-->
self._temp_handler = self.window.connect("draw", self.on_window_show)
I guess this is working because it takes some time before the window is drawn, more time than it takes it to create that untitled document. So that's a bit of a hack, but one that works for me at least.