aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorGravatar Mason Larobina <mason.larobina@gmail.com>2009-12-24 00:27:19 +0800
committerGravatar Mason Larobina <mason.larobina@gmail.com>2009-12-24 00:27:19 +0800
commitf200c865fb8a5daae30ff9c070eb5c929c720a4b (patch)
treeb1a8b00bbd8a637a0e6874dc12c5a98bdb84548b /examples
parent09d59497d1c06f07b1915e34c0110401916231b9 (diff)
Moved expand function to external plugin to reduce code duplication.
Diffstat (limited to 'examples')
-rw-r--r--examples/data/uzbl/plugins/bind.py27
-rw-r--r--examples/data/uzbl/plugins/cmd_expand.py43
-rw-r--r--examples/data/uzbl/plugins/on_event.py27
3 files changed, 47 insertions, 50 deletions
diff --git a/examples/data/uzbl/plugins/bind.py b/examples/data/uzbl/plugins/bind.py
index 668b595..9614df6 100644
--- a/examples/data/uzbl/plugins/bind.py
+++ b/examples/data/uzbl/plugins/bind.py
@@ -303,30 +303,6 @@ class Bind(object):
return self._repr_cache
-def expand(cmd, args):
- '''Replaces "%s %1 %2 %3..." with "<all args> <arg 0> <arg 1>...".'''
-
- # Direct string replace.
- if '%s' in cmd:
- cmd = cmd.replace('%s', ' '.join(map(unicode, args)))
-
- # Escaped and quoted string replace.
- if '%r' in cmd:
- joined = ('%r' % ' '.join(map(unicode, args)))[1:]
- for char in ['\\', '@']:
- joined = joined.replace(char, '\\'+char)
-
- cmd = cmd.replace('%r', joined)
-
- # Arg index string replace.
- for (index, arg) in enumerate(args):
- index += 1
- if '%%%d' % index in cmd:
- cmd = cmd.replace('%%%d' % index, unicode(arg))
-
- return cmd
-
-
def exec_bind(uzbl, bind, *args, **kargs):
'''Execute bind objects.'''
@@ -342,8 +318,9 @@ def exec_bind(uzbl, bind, *args, **kargs):
raise ArgumentError('cannot supply kargs for uzbl commands')
commands = []
+ cmd_expand = uzbl.cmd_expand
for cmd in bind.commands:
- cmd = expand(cmd, args)
+ cmd = cmd_expand(cmd, args)
uzbl.send(cmd)
diff --git a/examples/data/uzbl/plugins/cmd_expand.py b/examples/data/uzbl/plugins/cmd_expand.py
new file mode 100644
index 0000000..a5c279d
--- /dev/null
+++ b/examples/data/uzbl/plugins/cmd_expand.py
@@ -0,0 +1,43 @@
+__export__ = ['cmd_expand',]
+
+
+def escape(str):
+ for (level, char) in [(3, '\\'), (2, "'"), (2, '"'), (1, '@')]:
+ str = str.replace(char, (level * '\\') + char)
+
+ return str
+
+
+def cmd_expand(uzbl, cmd, args):
+ '''Exports a function that provides the following
+ expansions in any uzbl command string:
+
+ %s = replace('%s', ' '.join(args))
+ %r = replace('%r', "'%s'" % escaped(' '.join(args)))
+ %1 = replace('%1', arg[0])
+ %2 = replace('%2', arg[1])
+ %n = replace('%n', arg[n-1])
+ '''
+
+ # Ensure (1) all string representable and (2) correct string encoding.
+ args = map(unicode, args)
+
+ # Direct string replace.
+ if '%s' in cmd:
+ cmd = cmd.replace('%s', ' '.join(args))
+
+ # Escaped and quoted string replace.
+ if '%r' in cmd:
+ cmd = cmd.replace('%r', "'%s'" % escape(' '.join(args)))
+
+ # Arg index string replace.
+ for (index, arg) in enumerate(args):
+ index += 1
+ if '%%%d' % index in cmd:
+ cmd = cmd.replace('%%%d' % index, unicode(arg))
+
+ return cmd
+
+
+def init(*args):
+ pass
diff --git a/examples/data/uzbl/plugins/on_event.py b/examples/data/uzbl/plugins/on_event.py
index 9d2525b..f1ad0c9 100644
--- a/examples/data/uzbl/plugins/on_event.py
+++ b/examples/data/uzbl/plugins/on_event.py
@@ -45,30 +45,6 @@ def get_on_events(uzbl):
return UZBLS[uzbl]
-def expand(cmd, args):
- '''Replaces "%s %1 %2 %3..." with "<all args> <arg 0> <arg 1>...".'''
-
- # Direct string replace.
- if '%s' in cmd:
- cmd = cmd.replace('%s', ' '.join(map(unicode, args)))
-
- # Escaped and quoted string replace.
- if '%r' in cmd:
- joined = ('%r' % ' '.join(map(unicode, args)))[1:]
- for char in ['\\', '@']:
- joined = joined.replace(char, '\\'+char)
-
- cmd = cmd.replace('%r', joined)
-
- # Arg index string replace.
- for (index, arg) in enumerate(args):
- index += 1
- if '%%%d' % index in cmd:
- cmd = cmd.replace('%%%d' % index, unicode(arg))
-
- return cmd
-
-
def event_handler(uzbl, *args, **kargs):
'''This function handles all the events being watched by various
on_event definitions and responds accordingly.'''
@@ -79,8 +55,9 @@ def event_handler(uzbl, *args, **kargs):
return
commands = events[event]
+ cmd_expand = uzbl.cmd_expand
for cmd in commands:
- cmd = expand(cmd, args)
+ cmd = cmd_expand(cmd, args)
uzbl.send(cmd)