Skip to content

Commit

Permalink
Give special treatment to @-expands with escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
keis committed Feb 2, 2017
1 parent 19e6ae4 commit ad7d7f4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
8 changes: 4 additions & 4 deletions examples/config/config
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,12 @@ set history_disable_easter_egg 1
@cbind <Ctrl>p = hardcopy page

# Web searching binds
@cbind gg<Google:>_ = uri http://www.google.com/search?q=\@-JSON.stringify(encodeURIComponent(%r))-\@
@cbind ddg<DuckDuckGo:>_ = uri http://duckduckgo.com/?q=\@-JSON.stringify(encodeURIComponent(%r))-\@
@cbind gg<Google:>_ = uri http://www.google.com/search?q=\@-encodeURIComponent(%r)-\@
@cbind ddg<DuckDuckGo:>_ = uri http://duckduckgo.com/?q=\@-encodeURIComponent(%r)-\@
# These bindings should be entered as simply \wiki but due to escaping
# happening in multiple stages it needs to be written like this
@cbind \\\\awiki<Archwiki:>_ = uri http://wiki.archlinux.org/index.php/Special:Search?search=\@-JSON.stringify(encodeURIComponent(%r))-\@&go=Go
@cbind \\\\wiki<Wikipedia:>_ = uri https://secure.wikimedia.org/wikipedia/en/w/index.php?fulltext=Search&title=Special%3ASearch&search=\@-JSON.stringify(encodeURIComponent(%r))-\@
@cbind \\\\awiki<Archwiki:>_ = uri http://wiki.archlinux.org/index.php/Special:Search?search=\@-encodeURIComponent(%r)-\@&go=Go
@cbind \\\\wiki<Wikipedia:>_ = uri https://secure.wikimedia.org/wikipedia/en/w/index.php?fulltext=Search&title=Special%3ASearch&search=\@-encodeURIComponent(%r)-\@

# Handy binds
# Set function shortcut
Expand Down
2 changes: 1 addition & 1 deletion tests/event-manager/testonevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_command(self):

def test_command_with_quotes(self):
oe = OnEventPlugin[self.uzbl]
event, command = 'FOO', "test 'string with spaces'"
event, command = 'FOO', 'test "string with spaces"'

oe.parse_on_event('FOO test "string with spaces"')
oe.event_handler('', on_event=event)
Expand Down
22 changes: 13 additions & 9 deletions uzbl/plugins/cmd_expand.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import re

SIMPLE = re.compile('^[a-zA-Z]+$')
UZBL_EXPAND = re.compile('^(@[({<*/-].*[)}>*/-]@)')
ARG_EXPAND = re.compile('^%[sr0-9]')
SIMPLE = re.compile('[a-zA-Z]+$')
UZBL_EXPAND = re.compile('(@[({<*/-].*[)}>*/-]@)')
ARG_EXPAND = re.compile('%[sr0-9]')


def escape(str):
Expand Down Expand Up @@ -45,23 +45,27 @@ def cmd_expand(cmd, args):
return cmd


def format_arg(a):
def format_arg(a, args):
v = cmd_expand(a, args)
if SIMPLE.match(a) or UZBL_EXPAND.match(a) or ARG_EXPAND.match(a):
return a
return repr(a)
return v
# Uzbl expands are evaluated before quotes so don't escape them
return '"%s"' % ''.join(
p if p.startswith('@') else escape(p)
for p in UZBL_EXPAND.split(v)
)


def send_user_command(uzbl, cmd, args):
if cmd[0] == 'event':
has_var = any('@' in x for x in cmd)
event = cmd[1]
args = cmd_expand(' '.join(format_arg(c) for c in cmd[2:]), args)
args = ' '.join(format_arg(c, args) for c in cmd[2:])
if not has_var:
# Bypass the roundtrip to uzbl and dispatch immediately
uzbl.event(event, args)
else:
uzbl.send(' '.join(('event', event, args)))
else:
cmd = ' '.join((cmd[0],) + tuple(format_arg(c) for c in cmd[1:]))
cmd = cmd_expand(cmd, args)
cmd = ' '.join((cmd[0],) + tuple(format_arg(c, args) for c in cmd[1:]))
uzbl.send(cmd)

0 comments on commit ad7d7f4

Please sign in to comment.