/
focus_autosave.py
50 lines (37 loc) · 1.46 KB
/
focus_autosave.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Save document when losing focus.
# Gautier Portet <kassoulet gmail.com>
from gi.repository import GObject, Gtk, Gdk, Gedit, Gio
import datetime
import os
# You can change here the default folder for unsaved files.
dirname = os.path.expanduser("~/.gedit_unsaved/")
def assure_path_exists(path):
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
class FocusAutoSavePlugin(GObject.Object, Gedit.WindowActivatable):
__gtype_name__ = "FocusAutoSavePlugin"
window = GObject.property(type=Gedit.Window)
def __init__(self):
GObject.Object.__init__(self)
def on_focus_out_event(self, widget, focus):
for n, doc in enumerate(self.window.get_unsaved_documents()):
if doc.is_untouched():
# nothing to do
continue
if doc.is_untitled():
# provide a default filename
now = datetime.datetime.now()
assure_path_exists(dirname)
filename = now.strftime(dirname + "%Y%m%d-%H%M%S-%%d.txt") % (n + 1)
doc.set_location(Gio.file_parse_name(filename))
# save the document
Gedit.commands_save_document(self.window, doc)
def do_activate(self):
self.signal = self.window.connect("focus-out-event", self.on_focus_out_event)
def do_deactivate(self):
self.window.disconnect(self.signal)
del self.signal