pyreadline: Added new handling of exit keys: ctrl-d, ctrl-z etc. Including configfile entries

This commit is contained in:
jstenar
2006-01-29 22:38:25 +00:00
parent e295833165
commit 0741887eac
4 changed files with 48 additions and 6 deletions
+7
View File
@@ -1,3 +1,10 @@
2006-01-29 Jörgen Stenarson <jorgen.stenarson -at- bostream.nu>
* Moved exit key (control-D) logic to separate key dispatch handler
* Added bind_exit_key to config file
* Added un_bind_key command to configfile
* Added un_bind_exit_key command to configfile
2006-01-25 Jörgen Stenarson <jorgen.stenarson -at- bostream.nu>
* Added doc directory to setup scripts
+10
View File
@@ -1,3 +1,8 @@
#Bind keys for exit (keys only work on empty lines
bind_exit_key("Control-d")
bind_exit_key("Control-z")
#Commands for moving
bind_key("Home", "beginning_of_line")
bind_key("End", "end_of_line")
@@ -49,6 +54,11 @@ bind_key("Control-y", "paste")
bind_key("Control-z", "undo")
bind_key("Control-_", "undo")
#Unbinding keys:
#un_bind_key("Home")
#Other
bell_style("none") #modes: none, audible, visible(not implemented)
show_all_if_ambiguous("on")
+1
View File
@@ -39,6 +39,7 @@ features:
- Smart paste for convenient use with ipython. Converting tab separated data to
python list or numpy array. Converting file paths to use / and escaping any
spaces using \ .
- Configuration file
"""
license = 'BSD'
+30 -6
View File
@@ -63,6 +63,7 @@ class Readline:
self.prompt_color = None
self.command_color = None
self.key_dispatch = {}
self.exit_dispatch = {}
self.previous_func = None
self.first_prompt = True
self.next_meta = False # True to force meta on next character
@@ -197,6 +198,10 @@ class Readline:
control, meta, shift, code = event.keyinfo
event.keyinfo = (control, True, shift, code)
#Process exit keys. Only exit on empty line
if event.keyinfo in self.exit_dispatch:
if len(self.line_buffer) == 0:
raise EOFError
try:
dispatch_func = self.key_dispatch[event.keyinfo]
except KeyError:
@@ -636,10 +641,6 @@ class Readline:
'''Delete the character at point. If point is at the beginning of
the line, there are no characters in the line, and the last
character typed was not bound to delete-char, then return EOF.'''
if len(self.line_buffer) == 0:
if self.previous_func != self.delete_char:
raise EOFError
self._bell()
if self.line_cursor < len(self.line_buffer):
del self.line_buffer[self.line_cursor]
else:
@@ -1086,10 +1087,18 @@ class Readline:
# print key,keyinfo,func.__name__
self.key_dispatch[keyinfo] = func
def _bind_exit_key(self, key):
'''setup the mapping from key to call the function.'''
keyinfo = key_text_to_keyinfo(key)
self.exit_dispatch[keyinfo] = None
def emacs_editing_mode(self, e): # (C-e)
'''When in vi command mode, this causes a switch to emacs editing
mode.'''
self._bind_exit_key('Control-d')
self._bind_exit_key('Control-z')
# I often accidentally hold the shift or control while typing space
self._bind_key('Shift-space', self.self_insert)
self._bind_key('Control-space', self.self_insert)
@@ -1141,9 +1150,21 @@ class Readline:
pass
def read_inputrc(self,inputrcpath=os.path.expanduser("~/pyreadlineconfig.ini")):
def pb(key,name):
def bind_key(key,name):
if hasattr(self,name):
self._bind_key(key,getattr(self,name))
def un_bind_key(key):
keyinfo = key_text_to_keyinfo(key)
if keyinfo in self.key_dispatch:
del self.key_dispatch[keyinfo]
def bind_exit_key(key):
self._bind_exit_key(key)
def un_bind_exit_key(key):
keyinfo = key_text_to_keyinfo(key)
if keyinfo in self.exit_dispatch:
del self.exit_dispatch[keyinfo]
def setbellstyle(mode):
self.bell_style=mode
def setbellstyle(mode):
@@ -1154,7 +1175,10 @@ class Readline:
self.mark_directories=mode
def completer_delims(mode):
self.completer_delims=mode
loc={"bind_key":pb,
loc={"bind_key":bind_key,
"bind_exit_key":bind_exit_key,
"un_bind_key":un_bind_key,
"un_bind_exit_key":un_bind_exit_key,
"bell_style":setbellstyle,
"mark_directories":mark_directories,
"show_all_if_ambiguous":show_all_if_ambiguous,