Subject: Re: [sup-devel] [BRANCH] Ctrl-W behaviour

From: Alvaro Herrera <[email-masked]>
Date: Wed, 23 Feb 2011 10:19:15 -0300

Excerpts from Hamish's message of mar feb 22 14:01:29 -0300 2011:
> Excerpts from Hamish's message of Mon Feb 21 01:29:20 +0000 2011:
> > I use Ctrl-W when deleting labels from a thread, and it's been a minor
> > annoyance that this is slightly broken - when there is a space before
> > the cursor, Ctrl-W will just delete the space, and you need to press
> > Ctrl-W a second time to delete the word aswell. This is not how I'm used
> > to it working in vim or on the command line.
> 
> I've now merged this into next. I forgot to put the patches in email
> last night, so I've put the branch diff at the end of this email.

I've been using this and it works great.  I was annoyed by the original
behavior too.

-- 
Álvaro Herrera <[email-masked]>
_______________________________________________
Sup-devel mailing list
[email-masked]
http://rubyforge.org/mailman/listinfo/sup-devel

---

From: Hamish <[email-masked]>
Date: Tue, 22 Feb 2011 17:01:29 +0000

Hamish Downer

diff --git a/lib/sup/textfield.rb b/lib/sup/textfield.rb
index a3d002a..343e450 100644
--- 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 = remove_extra_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,40 @@ private
     end
   end
 
+  def remove_extra_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
+          Ncurses::Form::REQ_PREV_CHAR
+        # otherwise delete the space
+        else
+          Ncurses::Form::REQ_DEL_PREV
+        end
+      else
+        nil
+      end
+    elsif v_index == v.length
+      # at end of string, with non-space before us
+      nil
+    else
+      # trailing spaces
+      Ncurses::Form::REQ_PREV_CHAR
+    end
+  end
+
   def set_cursed_value v
     @field.set_field_buffer 0, v
   end
_______________________________________________
Sup-devel mailing list
[email-masked]
http://rubyforge.org/mailman/listinfo/sup-devel

---

From: Hamish <[email-masked]>
Date: Mon, 21 Feb 2011 01:29:20 +0000

So I've now had a go at fixing it, in the branch called ctrl_w - just
one commit so far, and it took quite a bit of farting around to get this
far. I'll review it when it's not the early hours of the morning and
merge it into next at that point.

Please have a go and see if you can get any strange behaviour - I had
some strange behaviour with single-letter labels, but I seem to have
fixed that ...

Hamish Downer