aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/plugins/cmd_expand.py
diff options
context:
space:
mode:
authorGravatar Dieter Plaetinck <dieter@plaetinck.be>2010-01-24 14:33:28 +0100
committerGravatar Dieter Plaetinck <dieter@plaetinck.be>2010-01-24 14:33:28 +0100
commit27b23bc15985aea687aecec580d668b7f9701eb3 (patch)
treeb82659a8350c09d85e85e74af639ee909892d37e /examples/data/plugins/cmd_expand.py
parent6d4547a49805b552e9b56d41a653a0912e7b0534 (diff)
parentc8fb24d1069aa776bccdf3141adb9d3c3f6e8101 (diff)
merge in experimental
Diffstat (limited to 'examples/data/plugins/cmd_expand.py')
-rw-r--r--examples/data/plugins/cmd_expand.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/examples/data/plugins/cmd_expand.py b/examples/data/plugins/cmd_expand.py
new file mode 100644
index 0000000..3f6ae2b
--- /dev/null
+++ b/examples/data/plugins/cmd_expand.py
@@ -0,0 +1,42 @@
+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(uzbl):
+ # Function exports to the uzbl object, `function(uzbl, *args, ..)`
+ # becomes `uzbl.function(*args, ..)`.
+ uzbl.export('cmd_expand', cmd_expand)