Skip to content

Instantly share code, notes, and snippets.

@pejobo
Last active June 10, 2018 21:08
Show Gist options
  • Save pejobo/2ac6a8a76599e229705351f46f0bc093 to your computer and use it in GitHub Desktop.
Save pejobo/2ac6a8a76599e229705351f46f0bc093 to your computer and use it in GitHub Desktop.
gedit plugin to save tabs between editor restarts - copy files to ~/.local/share/gedit/plugins
[Plugin]
Loader=python3
Module=gedit_save_tabs
IAge=3
Name=Gedit Save Tabs
Description=Save and restore tabs in gedit
Authors=pejobo70@gmail.com
Copyright=Copyright © 2018 pejobo70@gmail.com
Website=https://github.com/pejobo
#
# gedit plugin to save tabs between restarts
#
from gi.repository import GObject, Gedit, Gio, GtkSource
from threading import Timer, Lock
import os
import zlib
class GeditSaveTabsPlugin(GObject.Object, Gedit.AppActivatable):
__gtype_name__ = "GeditSaveTabsPlugin"
app = GObject.property(type=Gedit.App)
loading = False
uris = None
tab_file = os.path.expanduser('~') + '/.local/share/gedit/tabs'
timer = None
lock = Lock()
def __init__(self):
GObject.Object.__init__(self)
def do_activate(self):
self.timer = Timer(0.7, self.on_timer)
self.timer.deamon = True
self.timer.start()
def do_deactivate(self):
self.timer.cancel()
def do_update_state(self):
pass
def on_timer(self):
with self.lock:
self.timer = Timer(15, self.on_timer)
self.timer.deamon = True
self.timer.start()
self.update_tabs()
def update_tabs(self, *args):
if self.uris == None:
if not self.loading and self.app.get_main_windows():
self.load_tabs()
return
list = []
unsaved_index = 0
for doc in self.app.get_documents():
if doc.is_untitled():
list.append(None)
self.save_unsaved(unsaved_index, doc)
unsaved_index += 1
else:
list.append(doc.get_uri_for_display())
if self.uris != list:
self.uris = list
self.save_tabs()
while self.delete_unsaved(unsaved_index):
unsaved_index += 1
def load_tabs(self):
if not self.app.get_main_windows():
return
self.loading = True
if not os.path.isfile(self.tab_file):
self.uris = []
return
with open(self.tab_file, 'r') as f:
uris = [line.strip() for line in f]
# open files
window = self.app.get_main_windows()[0]
encoding = GtkSource.Encoding.get_utf8()
unsaved_index = 0
for uri in uris:
if uri != 'None':
if uri[0] == '/' and os.path.isfile(uri):
Gedit.commands_load_location(window, Gio.File.new_for_path(uri), encoding, 0, 0)
else:
self.load_unsaved(unsaved_index, window)
unsaved_index += 1
self.uris = uris
self.loading = False
def save_tabs(self):
file = open(self.tab_file, 'w')
for uri in self.uris:
file.write(str(uri))
file.write('\n')
file.close()
def unsaved_name(self, index):
return os.path.expanduser('~') + '/.local/share/gedit/unsaved-' + str(index)
def save_unsaved(self, index, doc):
# save content to temp file (if changed since last cycle)
file_name = self.unsaved_name(index)
text = doc.get_text(doc.get_start_iter(), doc.get_end_iter(), False)
hash = zlib.adler32(text.encode('utf-8'))
if hash != doc.__dict__.get('hash'):
with open(file_name, 'w') as f:
f.write(text)
doc.hash = hash
def load_unsaved(self, index, window):
# load content from temp file and open tab
file_name = self.unsaved_name(index)
if os.path.isfile(file_name):
with open(file_name, 'r') as f:
text = f.read()
window.create_tab(False)
doc = self.app.get_documents()[-1]
doc.insert(doc.get_start_iter(), text, len(text))
def delete_unsaved(self, index):
file_name = self.unsaved_name(index)
if os.path.isfile(file_name):
os.remove(file_name)
return True
return False
@pejobo
Copy link
Author

pejobo commented Jun 8, 2018

tested with ubuntu 18.04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment