aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/plugins/completion.py
diff options
context:
space:
mode:
authorGravatar Mason Larobina <mason.larobina@gmail.com>2010-04-04 04:44:03 +0800
committerGravatar Mason Larobina <mason.larobina@gmail.com>2010-04-04 04:44:03 +0800
commitd3cbe16bf16ff63c0e3db15d645e958712bd02d8 (patch)
tree6bea7bda62f07b73329fdb136a0b907661ca356c /examples/data/plugins/completion.py
parentae15d257a858fe27090f3e5798357aea2f5a76da (diff)
Huge plugin & event manager upgrades.
1. Removed unused modules 2. Re-use event handlers with identical callbacks and args. 3. Removed plugin exceptions in favour of assertions. 4. Remove useless raw_keycmd and raw_bind config vars. 5. Implemented and use `after` and `cleanup` plugin hooks (correctly) 6. EM & plugins now use the python logging module to output messages 7. Null config items are removed automatically 8. Simpler mode plugin 9. The init plugins function is called after the INSTANCE_START event 10. New optparse option to silence event echoing to stdout 11. Close instance socket on INSTANCE_EXIT before event handling 12. Caught signals are logged 13. Show times on the messages in the log file 14. Refactor bind pluin to use uzbl.bindlet directly. 15. Refactor keycmd plugin to use uzbl.keycmd directly. 16. Refactored on_event plugin to use uzbl.on_events dict over UZBLS dict 17. Refactor completion plugin to use uzbl.completion set object. 18. Modified progress plugin to use config vars instead of `@progress k = v` 19. mode_config now a defaultdict(dict) (I.e. this allows you to `uzbl.mode_config[mode][var] = value` without needing to check `mode` is in the `uzbl.mode_config` dict). 20. Removed all default mode config values. 21. Removed all `get_mode()` and `set_mode(..)` functions (and the like). 22. Setting the mode is now done via the config object directly (I.e. `uzbl.config['mode'] = 'insert'`). 23. Uses the on_set plugin to watch for 'mode' and 'default_mode' config changes. 24. Don't raise the useless NEW_ON_SET event, missing ON_SET connect. 25. Plugin and EventHandler aren't suited as dict objects. 26. Also using collections.defaultdict(list) for uzbl.handlers dict. 27. Plugin `on_set.py` allows you to attach handlers to config var changes 28. Config plugin reduced to one `uzbl.config` dict-like object. 29. Update export and connect calls in plugins. 30. The functions connect, connect_dict, export, export_dict, require, logging are exported directly to the plugin namespace. 31. Moved parse_msg into Uzbl class. 32. Generally improved comments. 33. UzblEventDaemon now an object. 34. Various variable, function & class renames.
Diffstat (limited to 'examples/data/plugins/completion.py')
-rw-r--r--examples/data/plugins/completion.py129
1 files changed, 51 insertions, 78 deletions
diff --git a/examples/data/plugins/completion.py b/examples/data/plugins/completion.py
index 8cea203..e8c7f34 100644
--- a/examples/data/plugins/completion.py
+++ b/examples/data/plugins/completion.py
@@ -1,19 +1,10 @@
'''Keycmd completion.'''
-# A list of functions this plugin exports to be used via uzbl object.
-__export__ = ['start_completion', 'get_completion_dict']
-
import re
-# Holds the per-instance completion dicts.
-UZBLS = {}
-
# Completion level
NONE, ONCE, LIST, COMPLETE = range(4)
-# Default instance dict.
-DEFAULTS = {'completions': [], 'level': NONE, 'lock': False}
-
# The reverse keyword finding re.
FIND_SEGMENT = re.compile("(\@[\w_]+|set[\s]+[\w_]+|[\w_]+)$").findall
@@ -21,39 +12,17 @@ FIND_SEGMENT = re.compile("(\@[\w_]+|set[\s]+[\w_]+|[\w_]+)$").findall
LIST_FORMAT = "<span> %s </span>"
ITEM_FORMAT = "<span @hint_style>%s</span>%s"
-
def escape(str):
return str.replace("@", "\@")
-def add_instance(uzbl, *args):
- UZBLS[uzbl] = dict(DEFAULTS)
-
- # Make sure the config keys for all possible completions are known.
- uzbl.send('dump_config_as_events')
-
-
-def del_instance(uzbl, *args):
- if uzbl in UZBLS:
- del UZBLS[uzbl]
-
-
-def get_completion_dict(uzbl):
- '''Get data stored for an instance.'''
-
- if uzbl not in UZBLS:
- add_instance(uzbl)
-
- return UZBLS[uzbl]
-
-
def get_incomplete_keyword(uzbl):
'''Gets the segment of the keycmd leading up to the cursor position and
uses a regular expression to search backwards finding parially completed
keywords or @variables. Returns a null string if the correct completion
conditions aren't met.'''
- keylet = uzbl.get_keylet()
+ keylet = uzbl.keylet
left_segment = keylet.keycmd[:keylet.cursor]
partial = (FIND_SEGMENT(left_segment) + ['',])[0].lstrip()
if partial.startswith('set '):
@@ -65,9 +34,8 @@ def get_incomplete_keyword(uzbl):
def stop_completion(uzbl, *args):
'''Stop command completion and return the level to NONE.'''
- d = get_completion_dict(uzbl)
- d['level'] = NONE
- uzbl.set('completion_list')
+ uzbl.completion.level = NONE
+ del uzbl.config['completion_list']
def complete_completion(uzbl, partial, hint, set_completion=False):
@@ -99,46 +67,46 @@ def update_completion_list(uzbl, *args):
if not partial:
return stop_completion(uzbl)
- d = get_completion_dict(uzbl)
- if d['level'] < LIST:
+ if uzbl.completion.level < LIST:
return
- hints = [h for h in d['completions'] if h.startswith(partial)]
+ hints = filter(lambda h: h.startswith(partial), uzbl.completion)
if not hints:
- return uzbl.set('completion_list')
+ del uzbl.config['completion_list']
+ return
j = len(partial)
l = [ITEM_FORMAT % (escape(h[:j]), h[j:]) for h in sorted(hints)]
- uzbl.set('completion_list', LIST_FORMAT % ' '.join(l))
+ uzbl.config['completion_list'] = LIST_FORMAT % ' '.join(l)
def start_completion(uzbl, *args):
- d = get_completion_dict(uzbl)
- if d['lock']:
+ comp = uzbl.completion
+ if comp.locked:
return
(partial, set_completion) = get_incomplete_keyword(uzbl)
if not partial:
return stop_completion(uzbl)
- if d['level'] < COMPLETE:
- d['level'] += 1
+ if comp.level < COMPLETE:
+ comp.level += 1
- hints = [h for h in d['completions'] if h.startswith(partial)]
+ hints = filter(lambda h: h.startswith(partial), comp)
if not hints:
return
elif len(hints) == 1:
- d['lock'] = True
+ comp.lock()
complete_completion(uzbl, partial, hints[0], set_completion)
- d['lock'] = False
+ comp.unlock()
return
- elif partial in hints and d['level'] == COMPLETE:
- d['lock'] = True
+ elif partial in hints and comp.level == COMPLETE:
+ comp.lock()
complete_completion(uzbl, partial, partial, set_completion)
- d['lock'] = False
+ comp.unlock()
return
smalllen, smallest = sorted([(len(h), h) for h in hints])[0]
@@ -156,51 +124,56 @@ def start_completion(uzbl, *args):
common += char
if common:
- d['lock'] = True
+ comp.lock()
partial_completion(uzbl, partial, partial+common)
- d['lock'] = False
+ comp.unlock()
update_completion_list(uzbl)
-def add_builtins(uzbl, args):
+def add_builtins(uzbl, builtins):
'''Pump the space delimited list of builtin commands into the
builtin list.'''
- completions = get_completion_dict(uzbl)['completions']
- builtins = filter(None, map(unicode.strip, args.split(" ")))
- for builtin in builtins:
- if builtin not in completions:
- completions.append(builtin)
+ uzbl.completion.update(builtins.split())
def add_config_key(uzbl, key, value):
'''Listen on the CONFIG_CHANGED event and add config keys to the variable
list for @var<Tab> like expansion support.'''
- completions = get_completion_dict(uzbl)['completions']
- key = "@%s" % key
- if key not in completions:
- completions.append(key)
+ uzbl.completion.add("@%s" % key)
+
+
+class Completions(set):
+ def __init__(self):
+ set.__init__(self)
+ self.locked = False
+ self.level = NONE
+
+ def lock(self):
+ self.locked = True
+
+ def unlock(self):
+ self.locked = False
def init(uzbl):
- # Event handling hooks.
- uzbl.connect_dict({
- 'BUILTINS': add_builtins,
- 'CONFIG_CHANGED': add_config_key,
- 'INSTANCE_EXIT': del_instance,
- 'INSTANCE_START': add_instance,
- 'KEYCMD_CLEARED': stop_completion,
- 'KEYCMD_EXEC': stop_completion,
- 'KEYCMD_UPDATE': update_completion_list,
- 'START_COMPLETION': start_completion,
- 'STOP_COMPLETION': stop_completion,
+ '''Export functions and connect handlers to events.'''
+
+ export_dict(uzbl, {
+ 'completion': Completions(),
+ 'start_completion': start_completion,
})
- # Function exports to the uzbl object, `function(uzbl, *args, ..)`
- # becomes `uzbl.function(*args, ..)`.
- uzbl.export_dict({
- 'get_completion_dict': get_completion_dict,
- 'start_completion': start_completion,
+ connect_dict(uzbl, {
+ 'BUILTINS': add_builtins,
+ 'CONFIG_CHANGED': add_config_key,
+ 'KEYCMD_CLEARED': stop_completion,
+ 'KEYCMD_EXEC': stop_completion,
+ 'KEYCMD_UPDATE': update_completion_list,
+ 'START_COMPLETION': start_completion,
+ 'STOP_COMPLETION': stop_completion,
})
+
+ uzbl.send('dump_config_as_events')