autoreplace

Richard Suchenwirth 2008-01-17 - In Powerpoint (and possibly other software) you have a feature that when you type (TM), it is immediately replaced by the Trademark character ™. Not sure whether one needs it, but I did, and in a quiet moment I reimplemented that behavior for Tk's text widget.

The autoreplacement is triggered by a closing paren, so all such replacement strings (defined in the "automap" global variable) must end in ), but that would e.g. include smilies :^). If you want to temporarily suppress autoreplacement, just type a space before the closing paren, and delete it split-seconds later.

Here it is, simple enough:

 package require Tk

 pack [text .t -wrap word] -fill both -expand 1
 bind .t <KeyRelease-)> {autochange  %W $automap}

 set automap "(TM) \u2122 (C) \u00A9 (R) \u00AE"

 proc autochange {w map} {
    foreach {abbrev value} $map {
        set n [string length $abbrev]c
        if {[$w get insert-$n insert] eq $abbrev} {
            $w delete insert-$n insert
            $w insert insert $value
            break
        }
    }
 }