2009-08-10 23:15:40 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
|
|
|
"""terminator.py - class for the master Terminator singleton"""
|
|
|
|
|
2009-08-11 22:19:06 +00:00
|
|
|
from borg import Borg
|
2009-10-14 12:05:07 +00:00
|
|
|
from config import Config
|
|
|
|
from keybindings import Keybindings
|
2009-11-14 23:12:38 +00:00
|
|
|
from util import dbg
|
2009-09-02 22:17:54 +00:00
|
|
|
|
2009-08-11 22:19:06 +00:00
|
|
|
class Terminator(Borg):
|
2009-08-10 23:15:40 +00:00
|
|
|
"""master object for the application"""
|
|
|
|
|
2009-11-14 23:12:38 +00:00
|
|
|
windows = None
|
2009-08-10 23:15:40 +00:00
|
|
|
windowtitle = None
|
|
|
|
terminals = None
|
|
|
|
groups = None
|
|
|
|
config = None
|
2009-10-14 12:05:07 +00:00
|
|
|
keybindings = None
|
2009-09-04 21:11:52 +00:00
|
|
|
|
2009-09-02 22:17:54 +00:00
|
|
|
groupsend = None
|
2009-09-03 12:59:17 +00:00
|
|
|
groupsend_type = {'all':0, 'group':1, 'off':2}
|
2009-08-10 23:15:40 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
|
|
|
|
2009-08-11 22:19:06 +00:00
|
|
|
Borg.__init__(self)
|
|
|
|
self.prepare_attributes()
|
|
|
|
|
|
|
|
def prepare_attributes(self):
|
2009-08-18 11:55:52 +00:00
|
|
|
"""Initialise anything that isn't already"""
|
2009-08-11 22:19:06 +00:00
|
|
|
|
2009-11-14 23:12:38 +00:00
|
|
|
if not self.windows:
|
|
|
|
self.windows = []
|
2009-08-11 22:19:06 +00:00
|
|
|
if not self.terminals:
|
|
|
|
self.terminals = []
|
|
|
|
if not self.groups:
|
|
|
|
self.groups = []
|
2009-09-02 22:17:54 +00:00
|
|
|
if not self.groupsend:
|
2009-09-03 12:59:17 +00:00
|
|
|
self.groupsend = self.groupsend_type['group']
|
2009-10-14 12:05:07 +00:00
|
|
|
if not self.config:
|
|
|
|
self.config = Config()
|
|
|
|
if not self.keybindings:
|
|
|
|
self.keybindings = Keybindings()
|
|
|
|
self.keybindings.configure(self.config['keybindings'])
|
2009-08-10 23:15:40 +00:00
|
|
|
|
2009-11-14 23:12:38 +00:00
|
|
|
def register_window(self, window):
|
|
|
|
"""Register a new window widget"""
|
|
|
|
dbg('Terminator::register_window: registering %s' % window)
|
|
|
|
self.windows.append(window)
|
|
|
|
|
|
|
|
def deregister_window(self, window):
|
|
|
|
"""de-register a window widget"""
|
|
|
|
dbg('Terminator::deregister_window: de-registering %s' % window)
|
|
|
|
self.windows.remove(window)
|
|
|
|
|
2009-09-02 22:17:54 +00:00
|
|
|
def register_terminal(self, terminal):
|
|
|
|
"""Register a new terminal widget"""
|
2009-11-14 23:12:38 +00:00
|
|
|
dbg('Terminator::register_terminal: registering %s' % terminal)
|
2009-08-10 23:15:40 +00:00
|
|
|
self.terminals.append(terminal)
|
2009-11-04 23:28:09 +00:00
|
|
|
terminal.connect('ungroup-all', self.ungroup_all)
|
2009-11-09 22:33:17 +00:00
|
|
|
terminal.connect('navigate', self.navigate_terminal)
|
2009-08-10 23:15:40 +00:00
|
|
|
|
2009-10-05 21:15:22 +00:00
|
|
|
def deregister_terminal(self, terminal):
|
|
|
|
"""De-register a terminal widget"""
|
2009-11-14 23:12:38 +00:00
|
|
|
dbg('Terminator::deregister_terminal: de-registering %s' % terminal)
|
2009-10-05 21:15:22 +00:00
|
|
|
self.terminals.remove(terminal)
|
|
|
|
|
2009-11-20 05:45:33 +00:00
|
|
|
if len(self.terminals) == 0:
|
|
|
|
for window in self.windows:
|
|
|
|
window.destroy()
|
|
|
|
|
2009-08-10 23:15:40 +00:00
|
|
|
def reconfigure_terminals(self):
|
|
|
|
"""Tell all terminals to update their configuration"""
|
|
|
|
|
|
|
|
for terminal in self.terminals:
|
|
|
|
terminal.reconfigure()
|
|
|
|
|
2009-11-09 22:33:17 +00:00
|
|
|
def navigate_terminal(self, terminal, direction):
|
|
|
|
"""Nagivate around the terminals"""
|
|
|
|
current = self.terminals.index(terminal)
|
|
|
|
length = len(self.terminals)
|
|
|
|
next = None
|
|
|
|
|
|
|
|
if length <= 1:
|
|
|
|
return
|
|
|
|
|
|
|
|
print "Current term: %d" % current
|
|
|
|
print "Number of terms: %d" % length
|
|
|
|
|
|
|
|
if direction == 'next':
|
|
|
|
next = current + 1
|
|
|
|
if next >= length:
|
|
|
|
next = 0
|
|
|
|
elif direction == 'prev':
|
|
|
|
next = current - 1
|
|
|
|
if next < 0:
|
|
|
|
next = length - 1
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
# FIXME: Do the directional navigation
|
|
|
|
|
|
|
|
if next is not None:
|
|
|
|
print "sending focus to term %d" % next
|
2009-11-09 22:35:55 +00:00
|
|
|
self.terminals[next].grab_focus()
|
2009-11-09 22:33:17 +00:00
|
|
|
|
2009-11-04 23:28:09 +00:00
|
|
|
def create_group(self, name):
|
|
|
|
"""Create a new group"""
|
|
|
|
if name not in self.groups:
|
2009-11-20 05:40:31 +00:00
|
|
|
dbg('Terminator::create_group: registering group %s' % name)
|
2009-11-04 23:28:09 +00:00
|
|
|
self.groups.append(name)
|
|
|
|
|
|
|
|
def ungroup_all(self, widget):
|
|
|
|
"""Remove all groups"""
|
|
|
|
for terminal in self.terminals:
|
|
|
|
terminal.set_group(None, None)
|
|
|
|
self.groups = []
|
|
|
|
|
2009-11-04 23:42:54 +00:00
|
|
|
def closegroupedterms(self, group):
|
|
|
|
"""Close all terminals in a group"""
|
|
|
|
for terminal in self.terminals:
|
|
|
|
if terminal.group == group:
|
|
|
|
terminal.close()
|
|
|
|
|
2009-08-10 23:15:40 +00:00
|
|
|
def group_hoover(self):
|
|
|
|
"""Clean out unused groups"""
|
|
|
|
|
2009-11-09 22:33:17 +00:00
|
|
|
if self.config['autoclean_groups']:
|
2009-11-20 05:40:31 +00:00
|
|
|
inuse = []
|
2009-08-10 23:15:40 +00:00
|
|
|
todestroy = []
|
|
|
|
|
2009-11-20 05:40:31 +00:00
|
|
|
for terminal in self.terminals:
|
|
|
|
if terminal.group:
|
|
|
|
if not terminal.group in inuse:
|
|
|
|
inuse.append(terminal.group)
|
|
|
|
|
|
|
|
for group in self.groups:
|
|
|
|
if not group in inuse:
|
|
|
|
todestroy.append(group)
|
2009-08-10 23:15:40 +00:00
|
|
|
|
2009-11-20 05:40:31 +00:00
|
|
|
dbg('Terminator::group_hoover: %d groups, hoovering %d' %
|
|
|
|
(len(self.groups), len(todestroy)))
|
2009-08-10 23:15:40 +00:00
|
|
|
for group in todestroy:
|
|
|
|
self.groups.remove(group)
|
|
|
|
|
2009-09-04 21:48:35 +00:00
|
|
|
def do_enumerate(self, widget, pad):
|
|
|
|
"""Insert the number of each terminal in a group, into that terminal"""
|
|
|
|
if pad:
|
2009-10-08 23:30:03 +00:00
|
|
|
numstr = '%0'+str(len(str(len(self.terminals))))+'d'
|
2009-09-04 21:48:35 +00:00
|
|
|
else:
|
2009-10-08 23:30:03 +00:00
|
|
|
numstr = '%d'
|
2009-09-04 21:48:35 +00:00
|
|
|
|
|
|
|
for term in self.get_target_terms(widget):
|
|
|
|
idx = self.terminals.index(term)
|
|
|
|
term.feed(numstr % (idx + 1))
|
|
|
|
|
|
|
|
def get_target_terms(self, widget):
|
|
|
|
"""Get the terminals we should currently be broadcasting to"""
|
|
|
|
if self.groupsend == self.groupsend_type['all']:
|
|
|
|
return(self.terminals)
|
|
|
|
elif self.groupsend == self.groupsend_type['group']:
|
2009-10-08 23:30:03 +00:00
|
|
|
termset = []
|
2009-09-04 21:48:35 +00:00
|
|
|
for term in self.terminals:
|
|
|
|
if term == widget or (term.group != None and term.group ==
|
|
|
|
widget.group):
|
2009-10-08 23:30:03 +00:00
|
|
|
termset.append(term)
|
|
|
|
return(termset)
|
2009-09-04 21:48:35 +00:00
|
|
|
else:
|
|
|
|
return([widget])
|
|
|
|
|
2009-09-04 23:34:09 +00:00
|
|
|
def group_tab(self, widget):
|
|
|
|
"""Group all the terminals in a tab"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def ungroup_tab(self, widget):
|
|
|
|
"""Ungroup all the terminals in a tab"""
|
|
|
|
pass
|
2009-10-08 23:22:01 +00:00
|
|
|
|
|
|
|
def focus_changed(self, widget):
|
|
|
|
"""We just moved focus to a new terminal"""
|
|
|
|
for terminal in self.terminals:
|
|
|
|
terminal.titlebar.update()
|
|
|
|
return
|
2009-08-10 23:15:40 +00:00
|
|
|
# vim: set expandtab ts=4 sw=4:
|