text .t
# Hide the default widget command
rename .t _.t
# Create our wrapper command
proc .t {cmd args} {
switch -- $cmd {
insert {
# Filter text we're going to insert
# Copy the index into the new argument list
set newargs [lindex $args 0]
# The remaining arguments should be text
# followed by an optional tag(s) argument,
# possibly repeated multiple times.
set index 0
foreach arg [lrange $args 1 end] {
incr index
# Filter only the text, not the tags
if {($index % 2) == 1} {
# Delete anything other than digits,
# spaces, tabs, and newlines.
regsub -all -- "\[^0-9 \t\n]" $arg "" arg
}
lappend newargs $arg
}
# Call the hidden text widget command with
# our filtered arguments.
uplevel 1 _.t insert $newargs
}
default {
# Anything other than an insert operation we
# pass to the hidden text widget command untouched.
uplevel 1 _.t $cmd $args
}
}
}
pack .t