My strategy is to regsub the specific term with the term + the tag
before displaying it in the text widget. However, instead of the term
picking up the tag attributes, the literal regsub is displayed in the
text widget. For instance, one tag is configured .f.t tag configure
htmltag -foreground blue. Then I use this statement to get the tag
attributes into the widget: regsub html $data [list html htmltag]
data. However, the result is that instead of seeing a file name like
index.html displayed with the html in blue, the file name is displayed
as index.html htmltag.
Can anyone tell me what I'm doing wrong? Sample code below. Thanks.
frame .f
pack .f -side top -fill both -expand yes
text .f.t -yscrollcommand [list .f.s set]
pack .f.t -side left -fill both -expand yes
scrollbar .f.s -command [list .f.t yview]
pack .f.s -side right -fill y -expand no
button .b -text "Get List" -command getlist
pack .b -side bottom
.f.t tag configure jpgtag -foreground blue
.f.t tag configure htmltag -foreground blue
proc getlist {} {
set fd [open "| ls -a" r]
fileevent $fd readable [list logging_command $fd]
}
#command to get packet data into text widget
proc logging_command {fd} {
global log done pipe
fconfigure $fd -blocking 0 -buffering none
set pipe $fd
set data [gets $fd]
regsub jpg $data [list jpg jpgtag] data
regsub html $data [list html htmltag] data
.f.t insert end $data\n
}
--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
The text widget insert command expects data in the following format:
.text insert $index $string $tags $string $tags ...
so your regsub method won't work. If you're confident that after
regsubbing you'll get a proper list of string-tags then of course you
can:
.f.t insert end {*}$data
but from your code that looks very unlikely. What I often do is
something along the lines of:
proc splitParts {txt re} {
if {[regexp -indices -- $re $txt range]} {
lassign $range s n
return [list \
[string range $txt 0 [expr {$s-1}]] \
[string range $txt $s $n] \
[string range $txt [expr {$n+1}] end] \
]
} else {
return [list $txt {} {}]
}
}
while {$data != ""} {
lassign [splitParts $data {jpg|html}] pre match data
.f.t insert end $pre
set tags [list]
switch -- $match {
jpg {lappend tags jpegtag}
html {lappend tags htmltag}
}
.f.t insert end $match $tags
}
Of course, this can be improved by having splitParts accept a list of
regexp to match instead of just one and return the index of the rule
matched so you wouldn't need to switch on the $match part after the
split. Something like this would be nice:
set taglist {
jpegtag
htmltag
{funnytag htmltag}
}
lassign [smartSplit $data {
jpg
html
{some.*funny\s+match}
}
] pre match idx data
.f.t insert end $pre
if {$match != ""} {
.f.t insert end $match [lindex $taglist $idx]
}
I think you're just missing one step about text tags: they are not "in-
band" text, they are manipulated separately, eg as an extra arg of the
[.t insert] widget command. For a quick summary see
-Alex
Better try adding "eval" in front of the .t.f insert command
The syntax is .f.t insert TEXT ?TAGLIST TEXT TAGLIST? ....
You have just regsub'ed into the text string and inserted the
resulting single text string. You need to break up the original string
at appropriate positions and insert the substrings as separate TEXT
arguments with appropriate tags after each text argument.
/Ashok