Version 0 of jump words like emacs in the text widget

Updated 2010-11-19 03:33:05 by Ro

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

}

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