diff options
author | Mason Larobina <mason.larobina@gmail.com> | 2009-11-29 12:24:29 +0800 |
---|---|---|
committer | Mason Larobina <mason.larobina@gmail.com> | 2009-11-29 12:35:11 +0800 |
commit | 6a33e0043932fbb2ab36d8e2557c5758d1f7004b (patch) | |
tree | 08db3c9d0ca0e0c715665cc42d2e3b784d939dee /examples/data/uzbl/plugins/mode.py | |
parent | e62c36c07d360e73543f864af69e97bd5d348e73 (diff) |
Refactor mode plugin, remove load_reset function.
Diffstat (limited to 'examples/data/uzbl/plugins/mode.py')
-rw-r--r-- | examples/data/uzbl/plugins/mode.py | 116 |
1 files changed, 66 insertions, 50 deletions
diff --git a/examples/data/uzbl/plugins/mode.py b/examples/data/uzbl/plugins/mode.py index 74e805a..568abc1 100644 --- a/examples/data/uzbl/plugins/mode.py +++ b/examples/data/uzbl/plugins/mode.py @@ -1,7 +1,7 @@ import sys import re -__export__ = ['set_mode', 'get_mode'] +__export__ = ['set_mode', 'get_mode', 'set_mode_config', 'get_mode_config'] UZBLS = {} @@ -12,19 +12,16 @@ DEFAULTS = { 'forward_keys': True, 'keycmd_events': False, 'modcmd_updates': False, - 'indicator': 'I'}, + 'mode_indicator': 'I'}, 'command': { 'forward_keys': False, 'keycmd_events': True, 'modcmd_updates': True, - 'indicator': 'C'}}} + 'mode_indicator': 'C'}}} FINDSPACES = re.compile("\s+") VALID_KEY = re.compile("^[\w_]+$").match -def error(msg): - sys.stderr.write("mode plugin: error: %s\n" % msg) - def add_instance(uzbl, *args): UZBLS[uzbl] = dict(DEFAULTS) @@ -36,6 +33,8 @@ def del_instance(uzbl, *args): def get_mode_dict(uzbl): + '''Return the mode dict for an instance.''' + if uzbl not in UZBLS: add_instance(uzbl) @@ -43,6 +42,8 @@ def get_mode_dict(uzbl): def get_mode_config(uzbl, mode): + '''Return the mode config for a given mode.''' + modes = get_mode_dict(uzbl)['modes'] if mode not in modes: modes[mode] = {} @@ -54,26 +55,13 @@ def get_mode(uzbl): return get_mode_dict(uzbl)['mode'] -def set_mode(uzbl, mode=None): - mode_dict = get_mode_dict(uzbl) - config = uzbl.get_config() - - if mode is None: - if 'default_mode' not in config: - return - - mode = config['default_mode'] +def mode_changed(uzbl, mode): + '''The mode has just been changed, now set the per-mode config.''' - if not VALID_KEY(mode): - raise KeyError("invalid mode name: %r" % mode) - - if 'mode' not in config or config['mode'] != mode: - config['mode'] = mode - return + get_mode_dict(uzbl)['mode'] = mode - mode_dict['mode'] = mode + config = uzbl.get_config() mode_config = get_mode_config(uzbl, mode) - for (key, value) in mode_config.items(): if key not in config: config[key] = value @@ -84,11 +72,38 @@ def set_mode(uzbl, mode=None): if 'mode_indicator' not in mode_config: config['mode_indicator'] = mode - uzbl.clear_keycmd() + +def set_mode(uzbl, mode=None): + '''Set the mode and raise the MODE_CHANGED event if the mode has changed. + Fallback on the default mode if no mode argument was given and the default + mode is not null.''' + + config = uzbl.get_config() + mode_dict = get_mode_dict(uzbl) + if mode is None: + mode_dict['mode'] = '' + if 'default_mode' in config: + mode = config['default_mode'] + + else: + mode = 'command' + + if not VALID_KEY(mode): + raise KeyError("invalid mode name: %r" % mode) + + if 'mode' not in config or config['mode'] != mode: + config['mode'] = mode + return + + elif get_mode(uzbl) == mode: + return + uzbl.event("MODE_CHANGED", mode) def config_changed(uzbl, key, value): + '''Check for mode related config changes.''' + value = None if not value else value if key == 'default_mode': if not get_mode(uzbl): @@ -98,11 +113,25 @@ def config_changed(uzbl, key, value): set_mode(uzbl, value) +def set_mode_config(uzbl, mode, key, value): + '''Set mode specific configs. If the mode being modified is the current + mode then apply the changes on the go.''' + + assert VALID_KEY(mode) and VALID_KEY(key) + + mode_config = get_mode_config(uzbl, mode) + mode_config[key] = value + + if get_mode(uzbl) == mode: + uzbl.set(key, value) + + def mode_config(uzbl, args): + '''Parse mode config events.''' split = map(unicode.strip, FINDSPACES.split(args.lstrip(), 1)) if len(split) != 2: - raise SyntaxError('invalid config syntax: %r' % args) + raise SyntaxError('invalid mode config syntax: %r' % args) mode, set = split split = map(unicode.strip, set.split('=', 1)) @@ -110,35 +139,22 @@ def mode_config(uzbl, args): raise SyntaxError('invalid set syntax: %r' % args) key, value = split - if not VALID_KEY(key): - raise KeyError('invalid config key: %r' % key) - - mode_config = get_mode_config(uzbl, mode) - mode_config[key] = value - - if get_mode(uzbl) == mode: - uzbl.set(key, value) - - -def load_reset(uzbl, *args): - config = uzbl.get_config() - if 'reset_on_commit' not in config or config['reset_on_commit'] == '1': - set_mode(uzbl) + set_mode_config(uzbl, mode, key, value) def toggle_modes(uzbl, modes): + '''Toggle or cycle between or through a list of modes.''' - modelist = [s.strip() for s in modes.split(' ') if s] - if not len(modelist): - return error("no modes specified to toggle") + assert len(modes.strip()) - mode_dict = get_mode_dict(uzbl) - oldmode = mode_dict['mode'] - if oldmode not in modelist: - return set_mode(uzbl, modelist[0]) + modelist = filter(None, map(unicode.strip, modes.split(' '))) + mode = get_mode(uzbl) + + index = 0 + if mode in modelist: + index = (modelist.index(mode)+1) % len(modelist) - newmode = modelist[(modelist.index(oldmode)+1) % len(modelist)] - set_mode(uzbl, newmode) + set_mode(uzbl, modelist[index]) def init(uzbl): @@ -147,7 +163,7 @@ def init(uzbl): 'INSTANCE_EXIT': del_instance, 'INSTANCE_START': add_instance, 'MODE_CONFIG': mode_config, - 'LOAD_START': load_reset, - 'TOGGLE_MODES': toggle_modes} + 'TOGGLE_MODES': toggle_modes, + 'MODE_CHANGED': mode_changed} uzbl.connect_dict(connects) |