aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorGravatar Mason Larobina <mason.larobina@gmail.com>2009-09-19 20:02:00 +0800
committerGravatar Mason Larobina <mason.larobina@gmail.com>2009-09-19 20:02:00 +0800
commitafe6eea69f73a8af2880389c2639afd690989450 (patch)
treedfb127c3c84dca1a26cb7d8c4e2709472be611e6 /examples
parent9f22f916de0a21e649e00b2811b569f548faac1e (diff)
Finished alpha mode plugin.
Diffstat (limited to 'examples')
-rw-r--r--examples/data/uzbl/scripts/plugins/config.py11
-rw-r--r--examples/data/uzbl/scripts/plugins/keycmd.py18
-rw-r--r--examples/data/uzbl/scripts/plugins/mode.py158
3 files changed, 177 insertions, 10 deletions
diff --git a/examples/data/uzbl/scripts/plugins/config.py b/examples/data/uzbl/scripts/plugins/config.py
index cdf203f..5ce614a 100644
--- a/examples/data/uzbl/scripts/plugins/config.py
+++ b/examples/data/uzbl/scripts/plugins/config.py
@@ -15,6 +15,13 @@ def escape(value):
return str(value)
+def get_config(uzbl):
+ if uzbl not in UZBLS:
+ add_instance(uzbl)
+
+ return UZBLS[uzbl]
+
+
def set(uzbl, key, value):
'''Sends a: "set key = value" command to the uzbl instance.'''
@@ -28,7 +35,9 @@ def set(uzbl, key, value):
if '\n' in value:
value = value.replace("\n", "\\n")
- uzbl.send('set %s = %s' % (key, value))
+ config = get_config(uzbl)
+ if key not in config or str(config[key]) != str(value):
+ uzbl.send('set %s = %s' % (key, value))
def add_instance(uzbl, *args):
diff --git a/examples/data/uzbl/scripts/plugins/keycmd.py b/examples/data/uzbl/scripts/plugins/keycmd.py
index 9d98cea..e263e9d 100644
--- a/examples/data/uzbl/scripts/plugins/keycmd.py
+++ b/examples/data/uzbl/scripts/plugins/keycmd.py
@@ -142,7 +142,7 @@ def update_event(uzbl, keylet):
if keycmd == keylet.to_string():
config['keycmd'] = keylet.to_string()
- else:
+ elif 'keycmd_events' not in config or config['keycmd_events'] == '1':
keycmd = keylet.cmd
uzbl.event('KEYCMD_UPDATE', keylet)
if keycmd == keylet.cmd:
@@ -195,13 +195,10 @@ def key_press(uzbl, key):
else:
cmdmod = True
- elif not k.modcmd and key == 'Return':
+ elif not k.modcmd and key == 'Return' and k.cmd or k.held:
uzbl.event('KEYCMD_EXEC', k)
clear_keycmd(uzbl)
- elif not k.modcmd and key == 'Escape':
- clear_keycmd(uzbl)
-
elif not k.held and not k.cmd and len(key) > 1:
k.modcmd = True
k.held.append(key)
@@ -267,7 +264,10 @@ def key_release(uzbl, key):
def init(uzbl):
'''Connect handlers to uzbl events.'''
- uzbl.connect('INSTANCE_START', add_instance)
- uzbl.connect('INSTANCE_EXIT', del_instance)
- uzbl.connect('KEY_PRESS', key_press)
- uzbl.connect('KEY_RELEASE', key_release)
+ connects = {'INSTANCE_START': add_instance,
+ 'INSTANCE_EXIT': del_instance,
+ 'KEY_PRESS': key_press,
+ 'KEY_RELEASE': key_release}
+
+ for (event, handler) in connects.items():
+ uzbl.connect(event, handler)
diff --git a/examples/data/uzbl/scripts/plugins/mode.py b/examples/data/uzbl/scripts/plugins/mode.py
new file mode 100644
index 0000000..72c4158
--- /dev/null
+++ b/examples/data/uzbl/scripts/plugins/mode.py
@@ -0,0 +1,158 @@
+import sys
+import re
+
+__export__ = ['set_mode', 'get_mode']
+
+UZBLS = {}
+
+DEFAULTS = {
+ 'mode': '',
+ 'default': '',
+ 'modes': {
+ 'insert': {
+ 'forward_keys': True,
+ 'keycmd_events': False,
+ 'indicator': 'I'},
+ 'command': {
+ 'forward_keys': False,
+ 'keycmd_events': True,
+ 'indicator': 'C'}}}
+
+_RE_FINDSPACES = re.compile("\s+")
+
+
+def error(msg):
+ sys.stderr.write("mode plugin: error: %s\n" % msg)
+
+
+def add_instance(uzbl, *args):
+ UZBLS[uzbl] = dict(DEFAULTS)
+
+
+def del_instance(uzbl, *args):
+ if uzbl in UZBLS:
+ del UZBLS[uzbl]
+
+
+def get_mode_dict(uzbl):
+ if uzbl not in UZBLS:
+ add_instance(uzbl)
+
+ return UZBLS[uzbl]
+
+
+def get_mode_config(uzbl, mode):
+ modes = get_mode_dict(uzbl)['modes']
+ if mode not in modes:
+ modes[mode] = {}
+
+ return modes[mode]
+
+
+def get_mode(uzbl):
+ return get_mode_dict(uzbl)['mode']
+
+
+def key_press(uzbl, key):
+ if key != "Escape":
+ return
+
+ set_mode(uzbl)
+
+
+def set_mode(uzbl, mode=None):
+ mode_dict = get_mode_dict(uzbl)
+ if mode is None:
+ if not mode_dict['default']:
+ return error("no default mode to fallback on")
+
+ mode = mode_dict['default']
+
+ config = uzbl.get_config()
+ if 'mode' not in config or config['mode'] != mode:
+ config['mode'] = mode
+
+ mode_dict['mode'] = mode
+ mode_config = get_mode_config(uzbl, mode)
+
+ for (key, value) in mode_config.items():
+ if key not in config:
+ config[key] = value
+
+ elif config[key] != value:
+ config[key] = value
+
+ if 'indicator' not in mode_config:
+ config['indicator'] = mode
+
+ uzbl.clear_keycmd()
+ uzbl.send('update_gui')
+ uzbl.event("MODE_CHANGED", mode)
+
+
+def config_changed(uzbl, key, value):
+ if key == 'default_mode':
+ mode_dict = get_mode_dict(uzbl)
+ mode_dict['default'] = value
+ if value and not mode_dict['mode']:
+ set_mode(uzbl, value)
+
+ elif key == 'mode':
+ if not value:
+ value = None
+
+ set_mode(uzbl, value)
+
+
+def mode_config(uzbl, args):
+
+ split = map(str.strip, _RE_FINDSPACES.split(args.lstrip(), 1))
+ if len(split) != 2:
+ return error("invalid MODE_CONFIG syntax: %r" % args)
+
+ mode, set = split
+ split = map(str.strip, set.split('=', 1))
+ if len(split) != 2:
+ return error("invalid MODE_CONFIG set command: %r" % args)
+
+ key, value = split
+ mode_config = get_mode_config(uzbl, mode)
+ mode_config[key] = value
+
+ if get_mode(uzbl) == mode:
+ uzbl.set(key, value)
+
+
+def commit_reset(uzbl, *args):
+ config = uzbl.get_config()
+ if 'reset_on_commit' not in config or config['reset_on_commit'] == '1':
+ set_mode(uzbl)
+
+
+def toggle_modes(uzbl, modes):
+
+ modelist = [s.strip() for s in modes.split(' ') if s]
+ if not len(modelist):
+ return error("no modes specified to toggle")
+
+ mode_dict = get_mode_dict(uzbl)
+ oldmode = mode_dict['mode']
+ if oldmode not in modelist:
+ return set_mode(uzbl, modelist[0])
+
+ newmode = modelist[(modelist.index(oldmode)+1) % len(modelist)]
+ set_mode(uzbl, newmode)
+
+
+def init(uzbl):
+
+ connects = {'CONFIG_CHANGED': config_changed,
+ 'INSTANCE_EXIT': del_instance,
+ 'INSTANCE_START': add_instance,
+ 'KEY_PRESS': key_press,
+ 'MODE_CONFIG': mode_config,
+ 'LOAD_COMMIT': commit_reset,
+ 'TOGGLE_MODES': toggle_modes}
+
+ for (event, handler) in connects.items():
+ uzbl.connect(event, handler)