2008-06-26 23:24:52 +00:00
|
|
|
#!/usr/bin/python
|
2010-01-11 20:06:53 +00:00
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
|
|
|
"""terminator.py - class for the master Terminator singleton"""
|
|
|
|
|
|
|
|
import gtk
|
|
|
|
|
|
|
|
from borg import Borg
|
|
|
|
from config import Config
|
|
|
|
from keybindings import Keybindings
|
2010-01-29 23:41:18 +00:00
|
|
|
from util import dbg, err
|
|
|
|
from factory import Factory
|
2010-01-11 20:06:53 +00:00
|
|
|
|
|
|
|
class Terminator(Borg):
|
|
|
|
"""master object for the application"""
|
|
|
|
|
|
|
|
windows = None
|
|
|
|
windowtitle = None
|
|
|
|
terminals = None
|
|
|
|
groups = None
|
|
|
|
config = None
|
|
|
|
keybindings = None
|
|
|
|
|
2010-01-29 23:52:21 +00:00
|
|
|
origcwd = None
|
|
|
|
|
2010-02-17 19:57:05 +00:00
|
|
|
doing_layout = None
|
|
|
|
|
2010-01-11 20:06:53 +00:00
|
|
|
groupsend = None
|
|
|
|
groupsend_type = {'all':0, 'group':1, 'off':2}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
|
|
|
|
|
|
|
Borg.__init__(self, self.__class__.__name__)
|
|
|
|
self.prepare_attributes()
|
|
|
|
|
|
|
|
def prepare_attributes(self):
|
|
|
|
"""Initialise anything that isn't already"""
|
|
|
|
|
|
|
|
if not self.windows:
|
|
|
|
self.windows = []
|
|
|
|
if not self.terminals:
|
|
|
|
self.terminals = []
|
|
|
|
if not self.groups:
|
|
|
|
self.groups = []
|
|
|
|
if not self.groupsend:
|
|
|
|
self.groupsend = self.groupsend_type['group']
|
|
|
|
if not self.config:
|
|
|
|
self.config = Config()
|
|
|
|
if not self.keybindings:
|
|
|
|
self.keybindings = Keybindings()
|
|
|
|
self.keybindings.configure(self.config['keybindings'])
|
2010-02-17 19:57:05 +00:00
|
|
|
if not self.doing_layout:
|
|
|
|
self.doing_layout = False
|
2010-01-11 20:06:53 +00:00
|
|
|
|
|
|
|
def register_window(self, window):
|
|
|
|
"""Register a new window widget"""
|
|
|
|
if window not in self.windows:
|
|
|
|
dbg('Terminator::register_window: registering %s:%s' % (id(window),
|
|
|
|
type(window)))
|
|
|
|
self.windows.append(window)
|
|
|
|
|
|
|
|
def deregister_window(self, window):
|
|
|
|
"""de-register a window widget"""
|
|
|
|
dbg('Terminator::deregister_window: de-registering %s:%s' %
|
|
|
|
(id(window), type(window)))
|
2010-01-29 23:37:25 +00:00
|
|
|
if window in self.windows:
|
|
|
|
self.windows.remove(window)
|
|
|
|
else:
|
|
|
|
err('%s is not in registered window list' % window)
|
|
|
|
|
2010-01-11 20:06:53 +00:00
|
|
|
if len(self.windows) == 0:
|
|
|
|
# We have no windows left, we should exit
|
2010-01-28 13:41:44 +00:00
|
|
|
dbg('no windows remain, quitting')
|
2010-01-11 20:06:53 +00:00
|
|
|
gtk.main_quit()
|
|
|
|
|
|
|
|
def register_terminal(self, terminal):
|
|
|
|
"""Register a new terminal widget"""
|
|
|
|
if terminal not in self.terminals:
|
|
|
|
dbg('Terminator::register_terminal: registering %s:%s' %
|
|
|
|
(id(terminal), type(terminal)))
|
|
|
|
self.terminals.append(terminal)
|
|
|
|
terminal.connect('ungroup-all', self.ungroup_all)
|
|
|
|
|
|
|
|
def deregister_terminal(self, terminal):
|
|
|
|
"""De-register a terminal widget"""
|
|
|
|
dbg('Terminator::deregister_terminal: de-registering %s:%s' %
|
|
|
|
(id(terminal), type(terminal)))
|
|
|
|
self.terminals.remove(terminal)
|
|
|
|
|
|
|
|
if len(self.terminals) == 0:
|
2010-01-28 13:41:44 +00:00
|
|
|
dbg('no terminals remain, destroying all windows')
|
2010-01-11 20:06:53 +00:00
|
|
|
for window in self.windows:
|
|
|
|
window.destroy()
|
2008-07-08 18:38:51 +00:00
|
|
|
else:
|
2010-01-11 20:06:53 +00:00
|
|
|
dbg('Terminator::deregister_terminal: %d terminals remain' %
|
|
|
|
len(self.terminals))
|
|
|
|
|
2010-01-29 23:41:18 +00:00
|
|
|
def new_window(self):
|
|
|
|
"""Create a window with a Terminal in it"""
|
|
|
|
maker = Factory()
|
|
|
|
window = maker.make('Window')
|
|
|
|
terminal = maker.make('Terminal')
|
|
|
|
window.add(terminal)
|
|
|
|
window.show()
|
|
|
|
terminal.spawn_child()
|
|
|
|
|
|
|
|
return(window, terminal)
|
|
|
|
|
2010-02-01 12:11:44 +00:00
|
|
|
def create_layout(self, layoutname):
|
|
|
|
"""Create all the parts necessary to satisfy the specified layout"""
|
|
|
|
layout = None
|
2010-02-02 00:39:41 +00:00
|
|
|
objects = {}
|
2010-02-01 12:11:44 +00:00
|
|
|
|
2010-02-17 19:57:05 +00:00
|
|
|
self.doing_layout = True
|
|
|
|
|
2010-02-01 12:11:44 +00:00
|
|
|
layout = self.config.layout_get_config(layoutname)
|
|
|
|
if not layout:
|
|
|
|
# User specified a non-existent layout. default to one Terminal
|
|
|
|
err('layout %s not defined' % layout)
|
|
|
|
raise(KeyError)
|
|
|
|
|
2010-02-02 00:39:41 +00:00
|
|
|
# Wind the flat objects into a hierarchy
|
|
|
|
hierarchy = {}
|
|
|
|
count = 0
|
|
|
|
# Loop over the layout until we have consumed it, or hit 1000 loops.
|
|
|
|
# This is a stupid artificial limit, but it's safe.
|
|
|
|
while len(layout) > 0 and count < 1000:
|
|
|
|
count = count + 1
|
|
|
|
if count == 1000:
|
|
|
|
err('hit maximum loop boundary. THIS IS VERY LIKELY A BUG')
|
|
|
|
for obj in layout.keys():
|
|
|
|
if layout[obj]['type'].lower() == 'window':
|
|
|
|
hierarchy[obj] = {}
|
|
|
|
hierarchy[obj]['type'] = 'Window'
|
|
|
|
hierarchy[obj]['children'] = {}
|
2010-02-27 13:55:38 +00:00
|
|
|
|
|
|
|
# Copy any additional keys
|
|
|
|
for objkey in layout[obj].keys():
|
|
|
|
if layout[obj][objkey] != '' and not hierarchy[obj].has_key(objkey):
|
|
|
|
hierarchy[obj][objkey] = layout[obj][objkey]
|
|
|
|
|
2010-02-02 00:39:41 +00:00
|
|
|
objects[obj] = hierarchy[obj]
|
|
|
|
del(layout[obj])
|
|
|
|
else:
|
|
|
|
# Now examine children to see if their parents exist yet
|
|
|
|
if not layout[obj].has_key('parent'):
|
|
|
|
err('Invalid object: %s' % obj)
|
|
|
|
del(layout[obj])
|
|
|
|
continue
|
|
|
|
if objects.has_key(layout[obj]['parent']):
|
2010-02-17 19:47:33 +00:00
|
|
|
# Our parent has been created, add ourselves
|
2010-02-02 00:39:41 +00:00
|
|
|
childobj = {}
|
|
|
|
childobj['type'] = layout[obj]['type']
|
|
|
|
childobj['children'] = {}
|
2010-02-17 19:47:33 +00:00
|
|
|
|
|
|
|
# Copy over any additional object keys
|
|
|
|
for objkey in layout[obj].keys():
|
|
|
|
if not childobj.has_key(objkey):
|
|
|
|
childobj[objkey] = layout[obj][objkey]
|
|
|
|
|
2010-02-02 00:39:41 +00:00
|
|
|
objects[layout[obj]['parent']]['children'][obj] = childobj
|
|
|
|
objects[obj] = childobj
|
|
|
|
del(layout[obj])
|
|
|
|
|
|
|
|
layout = hierarchy
|
|
|
|
|
2010-02-01 12:11:44 +00:00
|
|
|
for windef in layout:
|
2010-02-02 00:39:41 +00:00
|
|
|
if layout[windef]['type'] != 'Window':
|
2010-02-01 12:11:44 +00:00
|
|
|
err('invalid layout format. %s' % layout)
|
|
|
|
raise(ValueError)
|
|
|
|
window, terminal = self.new_window()
|
2010-02-02 00:39:41 +00:00
|
|
|
window.create_layout(layout[windef])
|
2010-02-27 13:55:38 +00:00
|
|
|
if layout[windef].has_key('position'):
|
|
|
|
parts = layout[windef]['position'].split(':')
|
|
|
|
if len(parts) == 2:
|
|
|
|
window.move(int(parts[0]), int(parts[1]))
|
2010-02-27 14:18:08 +00:00
|
|
|
if layout[windef].has_key('size'):
|
|
|
|
parts = layout[windef]['size']
|
|
|
|
window.resize(int(parts[0]), int(parts[1]))
|
2010-02-01 12:11:44 +00:00
|
|
|
|
2010-02-17 19:57:05 +00:00
|
|
|
def layout_done(self):
|
|
|
|
"""Layout operations have finished, record that fact"""
|
|
|
|
self.doing_layout = False
|
|
|
|
|
|
|
|
for terminal in self.terminals:
|
|
|
|
if not terminal.pid:
|
|
|
|
terminal.spawn_child()
|
|
|
|
|
2010-01-11 20:06:53 +00:00
|
|
|
def reconfigure(self):
|
|
|
|
"""Update configuration for the whole application"""
|
|
|
|
|
2010-01-11 23:46:18 +00:00
|
|
|
if self.config['handle_size'] in xrange(0, 6):
|
|
|
|
gtk.rc_parse_string("""style "terminator-paned-style" {
|
|
|
|
GtkPaned::handle_size = %s }
|
|
|
|
class "GtkPaned" style "terminator-paned-style" """ %
|
|
|
|
self.config['handle_size'])
|
|
|
|
gtk.rc_reset_styles(gtk.settings_get_default())
|
2010-01-11 20:06:53 +00:00
|
|
|
|
|
|
|
# Cause all the terminals to reconfigure
|
|
|
|
for terminal in self.terminals:
|
|
|
|
terminal.reconfigure()
|
|
|
|
|
2010-02-07 22:32:55 +00:00
|
|
|
# Reparse our keybindings
|
|
|
|
self.keybindings.configure(self.config['keybindings'])
|
|
|
|
|
2010-01-11 20:06:53 +00:00
|
|
|
def create_group(self, name):
|
|
|
|
"""Create a new group"""
|
|
|
|
if name not in self.groups:
|
|
|
|
dbg('Terminator::create_group: registering group %s' % name)
|
|
|
|
self.groups.append(name)
|
|
|
|
|
|
|
|
def ungroup_all(self, widget):
|
|
|
|
"""Remove all groups"""
|
|
|
|
for terminal in self.terminals:
|
|
|
|
terminal.set_group(None, None)
|
|
|
|
self.groups = []
|
|
|
|
|
|
|
|
def closegroupedterms(self, group):
|
|
|
|
"""Close all terminals in a group"""
|
|
|
|
for terminal in self.terminals:
|
|
|
|
if terminal.group == group:
|
|
|
|
terminal.close()
|
|
|
|
|
|
|
|
def group_hoover(self):
|
|
|
|
"""Clean out unused groups"""
|
|
|
|
|
|
|
|
if self.config['autoclean_groups']:
|
|
|
|
inuse = []
|
|
|
|
todestroy = []
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
dbg('Terminator::group_hoover: %d groups, hoovering %d' %
|
|
|
|
(len(self.groups), len(todestroy)))
|
|
|
|
for group in todestroy:
|
|
|
|
self.groups.remove(group)
|
|
|
|
|
|
|
|
def group_emit(self, terminal, group, type, event):
|
|
|
|
"""Emit to each terminal in a group"""
|
|
|
|
dbg('Terminator::group_emit: emitting a keystroke for group %s' %
|
|
|
|
group)
|
|
|
|
for term in self.terminals:
|
|
|
|
if term != terminal and term.group == group:
|
|
|
|
term.vte.emit(type, event)
|
|
|
|
|
|
|
|
def all_emit(self, terminal, type, event):
|
|
|
|
"""Emit to all terminals"""
|
|
|
|
for term in self.terminals:
|
|
|
|
if term != terminal:
|
|
|
|
term.vte.emit(type, event)
|
|
|
|
|
|
|
|
def do_enumerate(self, widget, pad):
|
|
|
|
"""Insert the number of each terminal in a group, into that terminal"""
|
|
|
|
if pad:
|
|
|
|
numstr = '%0'+str(len(str(len(self.terminals))))+'d'
|
2008-06-26 23:24:52 +00:00
|
|
|
else:
|
2010-01-11 20:06:53 +00:00
|
|
|
numstr = '%d'
|
|
|
|
|
|
|
|
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']:
|
|
|
|
termset = []
|
|
|
|
for term in self.terminals:
|
|
|
|
if term == widget or (term.group != None and term.group ==
|
|
|
|
widget.group):
|
|
|
|
termset.append(term)
|
|
|
|
return(termset)
|
2009-12-19 02:08:35 +00:00
|
|
|
else:
|
2010-01-11 20:06:53 +00:00
|
|
|
return([widget])
|
|
|
|
|
|
|
|
def group_tab(self, widget):
|
|
|
|
"""Group all the terminals in a tab"""
|
2010-01-28 12:49:38 +00:00
|
|
|
# FIXME: Implement or drop
|
2010-01-11 20:06:53 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def ungroup_tab(self, widget):
|
|
|
|
"""Ungroup all the terminals in a tab"""
|
2010-01-28 12:49:38 +00:00
|
|
|
# FIXME: Implement or drop
|
2010-01-11 20:06:53 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def focus_changed(self, widget):
|
|
|
|
"""We just moved focus to a new terminal"""
|
|
|
|
for terminal in self.terminals:
|
2010-01-18 23:27:22 +00:00
|
|
|
terminal.titlebar.update(widget)
|
2010-01-11 20:06:53 +00:00
|
|
|
return
|
2010-02-01 12:11:44 +00:00
|
|
|
|
|
|
|
def describe_layout(self):
|
|
|
|
"""Describe our current layout"""
|
2010-02-02 00:39:41 +00:00
|
|
|
layout = {}
|
|
|
|
count = 0
|
2010-02-01 12:11:44 +00:00
|
|
|
for window in self.windows:
|
2010-02-02 00:39:41 +00:00
|
|
|
parent = ''
|
|
|
|
count = window.describe_layout(count, parent, layout)
|
2010-02-01 12:11:44 +00:00
|
|
|
|
|
|
|
return(layout)
|
|
|
|
|
2010-01-11 20:06:53 +00:00
|
|
|
# vim: set expandtab ts=4 sw=4:
|