mirror of
https://github.com/wassname/pyreadline.git
synced 2026-06-27 16:10:38 +08:00
Make it possible to bind a function to a key
This commit is contained in:
@@ -157,9 +157,9 @@ class BaseMode(object):
|
||||
|
||||
def _bind_key(self, key, func):
|
||||
u"""setup the mapping from key to call the function."""
|
||||
if type(func) != type(self._bind_key):
|
||||
if not callable(func):
|
||||
print u"Trying to bind non method to keystroke:%s,%s"%(key,func)
|
||||
raise PyreadlineError(u"Trying to bind non method to keystroke:%s,%s,%s,%s"%(key,func,type(func),type(self._bind_key)))
|
||||
raise ReadlineError(u"Trying to bind non method to keystroke:%s,%s,%s,%s"%(key,func,type(func),type(self._bind_key)))
|
||||
keyinfo = make_KeyPress_from_keydescr(key.lower()).tuple()
|
||||
log(u">>>%s -> %s<<<"%(keyinfo,func.__name__))
|
||||
self.key_dispatch[keyinfo] = func
|
||||
|
||||
@@ -272,7 +272,10 @@ class BaseReadline(object):
|
||||
self.mode = modes[name]
|
||||
|
||||
def bind_key(key, name):
|
||||
if hasattr(modes[mode], name):
|
||||
import new
|
||||
if callable(name):
|
||||
modes[mode]._bind_key(key, new.instancemethod(name, modes[mode], modes[mode].__class__))
|
||||
elif hasattr(modes[mode], name):
|
||||
modes[mode]._bind_key(key, getattr(modes[mode], name))
|
||||
else:
|
||||
print u"Trying to bind unknown command '%s' to key '%s'"%(name, key)
|
||||
|
||||
Reference in New Issue
Block a user