Getting text from cursor coordinates

The gnocl::text widget has a wide range of signal callbacks but not all of these support the return of the line column reference. Mouse pointer and tag events are examples of this where only information regaring the mouse pointer is returned. Line and offset information is only returned in connection to changes in the insert cursor position.

When its necessary to get line and offset buffer coordinated it is possible to convert the pointer x y coordinates to something meaningful but the gnocl wordStart and wordEnd keywords are no useful as these relate to the insertion cursor.

The solution to this task is to extract the relavent line of text from the buffer and then using the offset returnd from the getIndex sub-command as a Tcl string index value. Again there are issues. The underlying Gtk api will disregard punctuation when determining a word break, Tcl doesnt. So, its necessary to strip out all punction. Here's a code segment showing how all this comes together.


proc gnocl::spellCheck::getWord {wid x y} {
   
    lassign [$wid getIndex $x $y] line offset
    set buff [lindex [split [$wid get ] \n] $line]
   
    set start [string wordstart $buff $offset]
    set end [string wordend $buff $offset]
   
    set wordStart [list $line $start]
    set wordEnd [list $line $end]
       
    return [gnocl::spellCheck::removePunctuation [string range $buff $start $end]]   
}


proc gnocl::spellCheck::removePunctuation { str } {
   
    set buff ""
   
    foreach ch [split $str ""] {
        if { [string is punc $ch] || $ch == "-"} { continue }
        append buff $ch
    }
    return [string trim $buff]
}



Comments