pyreadline-refactor: Adding argument handling

This commit is contained in:
jstenar
2006-10-19 19:35:35 +00:00
parent 5c9cda833b
commit 17c25b3a0e
4 changed files with 191 additions and 67 deletions
+8
View File
@@ -1,3 +1,11 @@
2006-10-19 Jörgen Stenarson <jorgen.stenarson -at- bostream.nu>
* Adding argument handling.
* Adding argument to functions:forward_char, backward_char, forward_word, backward_word
forward_word_end, backward_word_end, beginning_of_line_extend_selection, end_of_line_extend_selection
forward_char_extend_selection, backward_char_extend_selection, forward_word_extend_selection,
backward_word_extend_selection, forward_word_end_extend_selection, backward_word_end_extend_selection,
delete_char, backward_delete_char, backward_delete_word, forward_delete_word,
2006-10-18 Jörgen Stenarson <jorgen.stenarson -at- bostream.nu>
* Adding functionality to dump_functions
* fixing some clipboard bugs
+99 -40
View File
@@ -415,26 +415,48 @@ class ReadLineTextBuffer(TextLine):
def end_of_line(self):
self.selection_mark=-1
self.point=EndOfLine
def forward_char(self):
def forward_char(self,argument=1):
if argument<0:
self.backward_char(-argument)
self.selection_mark=-1
self.point=NextChar
for x in range(argument):
self.point=NextChar
def backward_char(self):
def backward_char(self,argument=1):
if argument<0:
self.forward_char(-argument)
self.selection_mark=-1
self.point=PrevChar
for x in range(argument):
self.point=PrevChar
def forward_word(self):
def forward_word(self,argument=1):
if argument<0:
self.backward_word(-argument)
self.selection_mark=-1
self.point=NextWordStart
for x in range(argument):
self.point=NextWordStart
def forward_word_end(self):
def backward_word(self,argument=1):
if argument<0:
self.forward_word(-argument)
self.selection_mark=-1
self.point=NextWordEnd
def backward_word(self):
for x in range(argument):
self.point=PrevWordStart
def forward_word_end(self,argument=1):
if argument<0:
self.backward_word_end(-argument)
self.selection_mark=-1
self.point=PrevWordStart
for x in range(argument):
self.point=NextWordEnd
def backward_word_end(self,argument=1):
if argument<0:
self.forward_word_end(-argument)
self.selection_mark=-1
for x in range(argument):
self.point=NextWordEnd
######### Movement select
def beginning_of_line_extend_selection(self):
@@ -446,31 +468,56 @@ class ReadLineTextBuffer(TextLine):
if self.enable_selection and self.selection_mark<0:
self.selection_mark=self.point
self.point=EndOfLine
def forward_char_extend_selection(self):
def forward_char_extend_selection(self,argument=1):
if argument<0:
self.backward_char_extend_selection(-argument)
if self.enable_selection and self.selection_mark<0:
self.selection_mark=self.point
self.point=NextChar
for x in range(argument):
self.point=NextChar
def backward_char_extend_selection(self):
def backward_char_extend_selection(self,argument=1):
if argument<0:
self.forward_char_extend_selection(-argument)
if self.enable_selection and self.selection_mark<0:
self.selection_mark=self.point
self.point=PrevChar
for x in range(argument):
self.point=PrevChar
def forward_word_extend_selection(self):
def forward_word_extend_selection(self,argument=1):
if argument<0:
self.backward_word_extend_selection(-argument)
if self.enable_selection and self.selection_mark<0:
self.selection_mark=self.point
self.point=NextWordStart
for x in range(argument):
self.point=NextWordStart
def forward_word_end_extend_selection(self):
def backward_word_extend_selection(self,argument=1):
if argument<0:
self.forward_word_extend_selection(-argument)
if self.enable_selection and self.selection_mark<0:
self.selection_mark=self.point
self.point=NextWordEnd
for x in range(argument):
self.point=PrevWordStart
def backward_word_extend_selection(self):
def forward_word_end_extend_selection(self,argument=1):
if argument<0:
self.backward_word_end_extend_selection(-argument)
if self.enable_selection and self.selection_mark<0:
self.selection_mark=self.point
self.point=PrevWordStart
for x in range(argument):
self.point=NextWordEnd
def backward_word_end_extend_selection(self,argument=1):
if argument<0:
self.forward_word_end_extend_selection(-argument)
if self.enable_selection and self.selection_mark<0:
self.selection_mark=self.point
for x in range(argument):
self.point=PrevWordEnd
######### delete
@@ -478,36 +525,48 @@ class ReadLineTextBuffer(TextLine):
if self.enable_selection and self.selection_mark>=0:
if self.selection_mark<self.point:
del self[self.selection_mark:self.point]
self.selection_mark=-1
else:
del self[self.point:self.selection_mark]
self.selection_mark=-1
return True
else:
self.selection_mark=-1
return False
self.selection_mark=-1
def delete_char(self):
if not self.delete_selection():
def delete_char(self,argument=1):
if argument<0:
self.backward_delete_char(-argument)
if self.delete_selection():
argument-=1
for x in range(argument):
del self[Point]
self.selection_mark=-1
def backward_delete_char(self):
if not self.delete_selection():
def backward_delete_char(self,argument=1):
if argument<0:
self.delete_char(-argument)
if self.delete_selection():
argument-=1
for x in range(argument):
if self.point>0:
self.backward_char()
self.delete_char()
self.selection_mark=-1
def backward_delete_word(self):
if not self.delete_selection():
#del self[PrevWordEnd:Point]
del self[PrevWordStart:Point]
self.selection_mark=-1
def forward_delete_word(self):
if not self.delete_selection():
#del self[PrevWordEnd:Point]
def forward_delete_word(self,argument=1):
if argument<0:
self.backward_delete_word(-argument)
if self.delete_selection():
argument-=1
for x in range(argument):
del self[Point:NextWordStart]
self.selection_mark=-1
def backward_delete_word(self,argument=1):
if argument<0:
self.forward_delete_word(-argument)
if self.delete_selection():
argument-=1
for x in range(argument):
del self[PrevWordStart:Point]
def delete_current_word(self):
if not self.delete_selection():
+45 -26
View File
@@ -23,6 +23,8 @@ class BaseMode(object):
self.key_dispatch = {}
self.startup_hook=None
self.pre_input_hook=None
self.argument=1
self.prevargument=None
def __repr__(self):
return "<BaseMode>"
@@ -38,6 +40,12 @@ class BaseMode(object):
def g(self):
return getattr(self.rlobj,x)
return g
def _argreset(self):
val=self.argument
self.argument=1
return val
argument_reset=property(_argreset)
l_buffer=property(*_gs("l_buffer"))
next_meta=property(*_gs("next_meta"))
@@ -57,7 +65,6 @@ class BaseMode(object):
_bell=property(_g("_bell"))
_clear_after=property(_g("_clear_after"))
_set_cursor=property(_g("_set_cursor"))
_print_prompt=property(_g("_print_prompt"))
_update_prompt_pos=property(_g("_update_prompt_pos"))
_update_line=property(_g("_update_line"))
enable_win32_clipboard=property(_g("enable_win32_clipboard"))
@@ -229,59 +236,71 @@ class BaseMode(object):
def forward_char(self, e): # (C-f)
'''Move forward a character. '''
self.l_buffer.forward_char()
self.l_buffer.forward_char(self.argument_reset)
def backward_char(self, e): # (C-b)
'''Move back a character. '''
self.l_buffer.backward_char()
self.l_buffer.backward_char(self.argument_reset)
def forward_word(self, e): # (M-f)
'''Move forward to the end of the next word. Words are composed of
letters and digits.'''
self.l_buffer.forward_word()
def forward_word_end(self, e): # (M-f)
'''Move forward to the end of the next word. Words are composed of
letters and digits.'''
self.l_buffer.forward_word_end()
self.l_buffer.forward_word(self.argument_reset)
def backward_word(self, e): # (M-b)
'''Move back to the start of the current or previous word. Words are
composed of letters and digits.'''
self.l_buffer.backward_word()
self.l_buffer.backward_word(self.argument_reset)
def forward_word_end(self, e): # ()
'''Move forward to the end of the next word. Words are composed of
letters and digits.'''
self.l_buffer.forward_word_end(self.argument_reset)
def backward_word_end(self, e): # ()
'''Move forward to the end of the next word. Words are composed of
letters and digits.'''
self.l_buffer.backward_word_end(self.argument_reset)
### Movement with extend selection
def beginning_of_line_extend_selection(self, e): #
'''Move to the start of the current line. '''
self.l_buffer.beginning_of_line_extend_selection()
self.l_buffer.beginning_of_line_extend_selection(self.argument_reset)
def end_of_line_extend_selection(self, e): #
'''Move to the end of the line. '''
self.l_buffer.end_of_line_extend_selection()
self.l_buffer.end_of_line_extend_selection(self.argument_reset)
def forward_char_extend_selection(self, e): #
'''Move forward a character. '''
self.l_buffer.forward_char_extend_selection()
self.l_buffer.forward_char_extend_selection(self.argument_reset)
def backward_char_extend_selection(self, e): #
'''Move back a character. '''
self.l_buffer.backward_char_extend_selection()
self.l_buffer.backward_char_extend_selection(self.argument_reset)
def forward_word_extend_selection(self, e): #
'''Move forward to the end of the next word. Words are composed of
letters and digits.'''
self.l_buffer.forward_word_extend_selection()
def forward_word_end_extend_selection(self, e): #
'''Move forward to the end of the next word. Words are composed of
letters and digits.'''
self.l_buffer.forward_word_end_extend_selection()
self.l_buffer.forward_word_extend_selection(self.argument_reset)
def backward_word_extend_selection(self, e): #
'''Move back to the start of the current or previous word. Words are
composed of letters and digits.'''
self.l_buffer.backward_word_extend_selection()
self.l_buffer.backward_word_extend_selection(self.argument_reset)
def forward_word_end_extend_selection(self, e): #
'''Move forward to the end of the next word. Words are composed of
letters and digits.'''
self.l_buffer.forward_word_end_extend_selection(self.argument_reset)
def backward_word_end_extend_selection(self, e): #
'''Move forward to the end of the next word. Words are composed of
letters and digits.'''
self.l_buffer.forward_word_end_extend_selection(self.argument_reset)
######## Change case
def upcase_word(self, e): # (M-u)
'''Uppercase the current (or following) word. With a negative
@@ -299,7 +318,7 @@ class BaseMode(object):
self.l_buffer.capitalize_word()
########
def clear_screen(self, e): # (C-l)
'''Clear the screen and redraw the current line, leaving the current
line at the top of the screen.'''
@@ -321,22 +340,22 @@ class BaseMode(object):
'''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.'''
self.l_buffer.delete_char()
self.l_buffer.delete_char(self.argument_reset)
def backward_delete_char(self, e): # (Rubout)
'''Delete the character behind the cursor. A numeric argument means
to kill the characters instead of deleting them.'''
self.l_buffer.backward_delete_char()
self.l_buffer.backward_delete_char(self.argument_reset)
def backward_delete_word(self, e): # (Control-Rubout)
'''Delete the character behind the cursor. A numeric argument means
to kill the characters instead of deleting them.'''
self.l_buffer.backward_delete_word()
self.l_buffer.backward_delete_word(self.argument_reset)
def forward_delete_word(self, e): # (Control-Delete)
'''Delete the character behind the cursor. A numeric argument means
to kill the characters instead of deleting them.'''
self.l_buffer.forward_delete_word()
self.l_buffer.forward_delete_word(self.argument_reset)
def delete_horizontal_space(self, e): # ()
'''Delete all spaces and tabs around point. By default, this is unbound. '''
+39 -1
View File
@@ -356,7 +356,45 @@ class EmacsMode(basemode.BaseMode):
def digit_argument(self, e): # (M-0, M-1, ... M--)
'''Add this digit to the argument already accumulating, or start a
new argument. M-- starts a negative argument.'''
pass
args=e.char
c = self.console
line = self.l_buffer.get_line_text()
oldprompt=self.prompt
def nop(e):
pass
while 1:
x, y = self.prompt_end_pos
c.pos(0, y)
self.prompt="(arg: %s) "%args
self._print_prompt()
self._update_line()
event = c.getkeypress()
if event.keyinfo.keyname == 'enter':
break
elif event.char in "0123456789":
args+=event.char
else:
self.argument=int(args)
keyinfo=event.keyinfo.tuple()
if len(keyinfo[-1])>1:
default=nop
else:
default=self.self_insert
dispatch_func = self.key_dispatch.get(keyinfo,default)
log_sock("%s|%s"%(dispatch_func,str(keyinfo)))
dispatch_func(event)
break
log_sock("END arg=%s"%(self.argument))
self.prompt=oldprompt
x, y = self.prompt_end_pos
c.pos(0, y)
self._print_prompt()
self._update_line()
def universal_argument(self, e): # ()
'''This is another way to specify an argument. If this command is