diff options
author | Mason Larobina <mason.larobina@gmail.com> | 2009-09-21 17:34:17 +0800 |
---|---|---|
committer | Mason Larobina <mason.larobina@gmail.com> | 2009-09-21 17:34:17 +0800 |
commit | 1b8df44d31a47354148cb82948b5ad6011a44168 (patch) | |
tree | 73b44642c13f57c5e19769d9dac4a2e1f8861194 /examples | |
parent | fb0a6b8208de81d70d65ca4511aefc83cded4e79 (diff) |
Added on_event plugin and replaced the load_{start|commit|finish}_handler's
Diffstat (limited to 'examples')
-rw-r--r-- | examples/config/uzbl/config | 50 | ||||
-rw-r--r-- | examples/data/uzbl/scripts/plugins/on_event.py | 109 |
2 files changed, 142 insertions, 17 deletions
diff --git a/examples/config/uzbl/config b/examples/config/uzbl/config index b611aa3..9ff27bc 100644 --- a/examples/config/uzbl/config +++ b/examples/config/uzbl/config @@ -2,24 +2,41 @@ # all settings are optional. you can use uzbl without any config at all (but it won't do much) # set some shortcuts + +# request BIND <keycmd> = <command> set bind = request BIND +# request MODE_CONFIG <mode> <key> = <value set mode_config = request MODE_CONFIG +# request TOGGLE_MODES <mode1> <mode2> ... <moden> set toggle_modes = request TOGGLE_MODES +# request ON_EVENT <EVENT_NAME> <command> +set on_event = request ON_EVENT + + set set_mode = set mode = +set set_status = set status_message = set shell_cmd = sh -c -# keyboard behavior in this sample config is sort of vimstyle + # Handlers -set download_handler = spawn $XDG_DATA_HOME/uzbl/scripts/download.sh -#set cookie_handler = spawn $XDG_DATA_HOME/uzbl/scripts/cookies.py -set cookie_handler = talk_to_socket $XDG_CACHE_HOME/uzbl/cookie_daemon_socket -#set new_window = sh 'echo uri "$8" > $4' # open in same window -set new_window = sh 'uzbl-browser -u $8' # equivalent to the default behaviour +set download_handler = spawn $XDG_DATA_HOME/uzbl/scripts/download.sh +set cookie_handler = talk_to_socket $XDG_CACHE_HOME/uzbl/cookie_daemon_socket +set scheme_handler = spawn $XDG_DATA_HOME/uzbl/scripts/scheme.py + +# New window handler options +#set new_window = sh 'echo uri "$8" > $4' # open in same window +set new_window = sh 'uzbl-browser -u $8' # equivalent to the default behaviour -set scheme_handler = spawn $XDG_DATA_HOME/uzbl/scripts/scheme.py -set load_start_handler = set status_message = <span foreground="khaki">wait</span> -set load_commit_handler = set status_message = <span foreground="green">recv</span> -set load_finish_handler = chain 'set status_message = <span foreground="gold">done</span>' 'spawn $XDG_DATA_HOME/uzbl/scripts/history.sh' + +# Load start handler +@on_event LOAD_START @set_status <span foreground="khaki">wait</span> + +# Load commit handler +@on_event LOAD_COMMIT @set_status <span foreground="green">recv</span> + +# Load finish handlers +@on_event LOAD_FINISH @set_status <span foreground="gold">done</span> +@on_event LOAD_FINISH spawn $XDG_DATA_HOME/uzbl/scripts/history.sh # Behaviour and appearance @@ -41,10 +58,12 @@ set selected_section = <span foreground="#606060"> \@[\@SELECTED_URI]\@</span> set status_format = <span font_family="monospace">@mode_section @keycmd_section @progress_section @uri_section @name_section @status_section @selected_section</span> # Core settings -set useragent = Uzbl (Webkit @WEBKIT_MAJOR.@WEBKIT_MINOR.@WEBKIT_MICRO) (@(uname -o)@ @(uname -m)@ [@ARCH_UZBL]) (Commit @COMMIT) -set fifo_dir = /tmp -set socket_dir = /tmp +set useragent = Uzbl (Webkit @WEBKIT_MAJOR.@WEBKIT_MINOR.@WEBKIT_MICRO) (@(uname -o)@ @(uname -m)@ [@ARCH_UZBL]) (Commit @COMMIT) +set fifo_dir = /tmp +set socket_dir = /tmp + +### Keyboard binding section: # like this you can enter any command at runtime, interactively. prefixed by ':' @bind :_ = chain '%s' @@ -78,9 +97,6 @@ set socket_dir = /tmp @bind s _ = set %s @bind \wiki _ = uri http://wiki.archlinux.org/index.php/Special:Search?search=%s&go=Go @bind gg _ = uri http://www.google.com/search?q=%s -@bind i = toggle_insert_mode -# disable insert mode (1 to enable). note that Esc works to disable, regardless of this setting -@bind I = toggle_insert_mode 0 # Enclose the executable in quotes if it has spaces. Any additional parameters you use will # appear AFTER the default parameters #@bind B = spawn $XDG_DATA_HOME/uzbl/scripts/insert_bookmark.sh @@ -143,7 +159,7 @@ set socket_dir = /tmp @bind <Ctrl>a<:>h = uri http://uzbl.org/ -### Mode config +### Mode config section: set default_mode = command diff --git a/examples/data/uzbl/scripts/plugins/on_event.py b/examples/data/uzbl/scripts/plugins/on_event.py new file mode 100644 index 0000000..ccfab4c --- /dev/null +++ b/examples/data/uzbl/scripts/plugins/on_event.py @@ -0,0 +1,109 @@ +'''Plugin provides arbitrarily binding uzbl events to uzbl commands. + You can use $1,$2 to refer to the arguments appearing in the relevant event messages + +For example: + request ON_EVENT LINK_HOVER 'set SELECTED_URI = $1' + this will set the SELECTED_URI variable which you can display in your statusbar +''' + +import sys +import re +from event_manager import config + +__export__ = ['get_on_events', 'on_event'] + +UZBLS = {} + +def echo(msg): + if config['verbose']: + print 'on_event plugin:', msg + + +def error(msg): + sys.stderr.write('on_event plugin: error: %s\n' % msg) + + +def add_instance(uzbl, *args): + UZBLS[uzbl] = {} + + +def del_instance(uzbl, *args): + if uzbl in UZBLS: + del UZBLS[uzbl] + + +def get_on_events(uzbl): + if uzbl not in UZBLS: + add_instance(uzbl) + + return UZBLS[uzbl] + + +def expand(cmd, args): + '''Replaces "%s %s %s.." with "arg1 arg2 arg3..". + + This could be improved by specifing explicitly which argument to substitue + for what by parsing "$@ $0 $1 $2 $3.." found in the command string.''' + + if '%s' not in cmd or not len(args): + return cmd + + if len(args) > 1: + for arg in args: + cmd = cmd.replace('%s', str(arg), 1) + + else: + cmd = cmd.replace('%s', str(args[0])) + + return cmd + + +def event_handler(uzbl, *args, **kargs): + '''This function handles all the events being watched by various + on_event definitions and responds accordingly.''' + + events = get_on_events(uzbl) + event = kargs['on_event'] + if event not in events: + return + + commands = events[event] + for cmd in commands: + cmd = expand(cmd, args) + uzbl.send(cmd) + + +def on_event(uzbl, event, cmd): + '''Add a new event to watch and respond to.''' + + events = get_on_events(uzbl) + if event not in events: + uzbl.connect(event, event_handler, on_event=event) + events[event] = [] + + cmds = events[event] + if cmd not in cmds: + cmds.append(cmd) + + +def parse_on_event(uzbl, args): + '''Parse ON_EVENT events and pass them to the on_event function. + + Syntax: "event ON_EVENT <EVENT_NAME> commands" + ''' + + split = args.split(' ', 1) + if len(split) != 2: + return error("invalid ON_EVENT syntax: %r" % args) + + event, cmd = split + on_event(uzbl, event, cmd) + + +def init(uzbl): + connects = {'ON_EVENT': parse_on_event, + 'INSTANCE_START': add_instance, + 'INSTANCE_EXIT': del_instance} + + for (event, handler) in connects.items(): + uzbl.connect(event, handler) |