jump words like emacs in the text widget

Alt-b and Alt-f work like in emacs, jumping to the next word, back and forth. Feels good.

set tx .t
pack [text $tx] -fill both -expand 1

# alt-b and alt-f jump words like emacs
bind $tx <Alt-b> [list ::txcursor::group_backward $tx]
bind $tx <Alt-f> [list ::txcursor::group_forward  $tx]


namespace eval ::txcursor:: {}

proc ::txcursor::sconcat {args} {foreach el $args {append x $el} ; return $x}

proc ::txcursor::jump_cursor {tx ix} {
  $tx mark set insert $ix
  $tx see insert
}

proc ::txcursor::is_whitespace {s} {
  expr {[string is space -strict $s]}
}

proc ::txcursor::get_char_forward {tx ix} {
  $tx get $ix [sconcat $ix +1c]
}
proc ::txcursor::get_char_backward {tx ix} {
  $tx get [sconcat $ix -1c] $ix
}

proc ::txcursor::group_forward {tx} {

  set ix [$tx index insert]

  set s    [::txcursor::get_char_forward $tx $ix]
  set g_ws [::txcursor::is_whitespace $s]

  while {$ix ne "end"} {
    set s    [::txcursor::get_char_forward $tx $ix]
    set c_ws [::txcursor::is_whitespace $s]

    if {$c_ws != $g_ws} break
    set ix [$tx index [sconcat $ix +1c]]
  }

  ::txcursor::jump_cursor $tx $ix
}


proc ::txcursor::group_backward {tx} {

  set ix [$tx index insert]

  set s    [::txcursor::get_char_backward $tx $ix]
  set g_ws [::txcursor::is_whitespace $s]

  while {$ix ne "1.0"} {
    set s    [::txcursor::get_char_backward $tx $ix]
    set c_ws [::txcursor::is_whitespace $s]

    if {$c_ws != $g_ws} break
    set ix [$tx index [sconcat $ix -1c]]
  }

  ::txcursor::jump_cursor $tx $ix
}

MG just tried this, but both bindings only moved the cursor forwards/backwards by a single character for me, no matter where the cursor was. But I think Tk has standard bindings (and helper procs) for what it's intended to do anyway, on Control-Left and Control-Right?

Ro should work fine... regarding standard bindings, didn't know about that ;) This code considers whitespace and non-whitespace as two groups and jumps around the text differently than the standard bindings. Ah well, there you go.