commit 16c0a92ee589e0d40802fddd9d9c0d75ef4b823d
parent 7e2bb889e617302c82e28d04cb2e0db5d35fbb08
Author: Hamish Downer <dmishd@gmail.com>
Date: Mon, 21 Feb 2011 01:13:12 +0000
Fix behaviour of Ctrl-W
Ctrl-W will now delete trailing spaces, rather than having to press
Ctrl-W twice to delete each label.
Diffstat:
1 file changed, 35 insertions(+), 0 deletions(-)
diff --git a/lib/sup/textfield.rb b/lib/sup/textfield.rb
@@ -120,6 +120,9 @@ class TextField
nop
Ncurses::Form::REQ_BEG_FIELD
when ?\C-w.ord
+ while action = after_space
+ Ncurses::Form.form_driver @form, action
+ end
Ncurses::Form.form_driver @form, Ncurses::Form::REQ_PREV_CHAR
Ncurses::Form.form_driver @form, Ncurses::Form::REQ_DEL_WORD
when Ncurses::KEY_UP, Ncurses::KEY_DOWN
@@ -167,6 +170,38 @@ private
end
end
+ def after_space
+ return nil unless @field
+
+ Ncurses::Form.form_driver @form, Ncurses::Form::REQ_VALIDATION
+ x = Ncurses.curx
+ v = @field.field_buffer(0).gsub(/^\s+|\s+$/, "")
+ v_index = x - @question.length
+
+ # at start of line
+ if v_index < 1
+ nil
+ ## cursor <= end of text
+ elsif v_index < v.length
+ # is the character before the cursor a space?
+ if v[v_index-1] == ?\s
+ # if there is a non-space char under cursor then go back
+ if v[v_index] != ?\s
+ return Ncurses::Form::REQ_PREV_CHAR
+ # otherwise delete the space
+ else
+ return Ncurses::Form::REQ_DEL_PREV
+ end
+ end
+ elsif v_index == v.length
+ # at end of string, with non-space before us
+ return nil
+ else
+ # trailing spaces
+ return Ncurses::Form::REQ_PREV_CHAR
+ end
+ end
+
def set_cursed_value v
@field.set_field_buffer 0, v
end