Hiliting Words in a Field
Peter Brigham MD
pmbrig at gmail.com
Tue Jun 8 11:01:18 EDT 2010
On Jun 7, 2010, at 5:10 PM, Jan Schenkel wrote:
> --- On Mon, 6/7/10, Gregory Lypny <gregory.lypny at videotron.ca> wrote:
>> Hello everyone,
>>
>> I have a data field with many lines and I'd like to script
>> a handler that hilites every appearance of a particular word
>> in yellow?
>>
>> Regards,
>>
>> Gregory
>>
>
> It depends a bit on your definition of the term 'word' in this
> context.
>
> The straightforward solution is to do a replace in the htmlText of
> the field, wrapping it with a font tag - but this simply hilites
> every occurence of the search string, rather than the whole word:
> ##
> put the htmlText of field "Data" into tHtmlText
> replace "foo" \
> with "<font bgcolor=" & quote & "yellow & quote & ">foo</font>" \
> in tHtmlText
> set the htmlText of field "Data" to tHtmlText
> ##
>
> If you're looking to hilite only complete words, your best option is
> the wordOffset function and a loop:
> ##
> put the text of field 1 into tText
> set the wholeMatches to true
> put wordOffset(tWordToFind, tText) into tWordOffset
> put 0 into tPrevOffset
> repeat until tWordOffset = 0
> add tWordOffset to tPrevOffset
> set the backgroundColor of word tPrevOffset of field 1 to "yellow"
> put wordOffset(tWordToFind, tText, tPrevOffset) into tWordOffset
> end repeat
> ##
> If you leave the 'wholeMatches' local property at its default value
> of 'false' the above script hilites the entire word, if part of it
> matches tWordToFind.
>
> HTH,
>
> Jan Schenkel
Try something like this:
on mouseUp
put fld "text" into tText
put "the" into targetString
put smartWordOffsets(targetString,tText) into wList
repeat for each line w in wList
set the backcolor of char (item 1 of w) to \
(item 2 of w) of fld "text" to yellow
-- or whatever formatting you want to do
end repeat
end mouseUp
function smartWordOffsets targetString,tText
-- returns a return-delimited list of chunk data
-- for matches of targetString in tText:
-- each line = <startChar>,<endChar>
put length(targetString) into strLength
put ".,?!$':;()#" & quote into puncList
repeat for each char c in puncList
replace c with space in tText
end repeat
-- the above handles the punctuation problem
put offsets(targetString,tText) into offsList
put empty into wList
repeat for each item j in offsList
put the number of words of char 1 to j of tText \
into wdNbr
put word wdNbr of tText into testWd
if testWd <> targetString then next repeat
-- whole matches only
put j & comma & j+strLength-1 & cr after wList
end repeat
delete char -1 of wList
return wList
end smartWordOffsets
function offsets str,cntr
-- returns a comma-delimited list
-- of all the offsets of str in cntr
put "" into mosList
put 0 into startPoint
repeat
put offset(str,cntr,startPoint) into os
if os = 0 then exit repeat
add os to startPoint
put startPoint & "," after mosList
end repeat
if char -1 of mosList = "," then delete last char of mosList
if mosList = "" then return "0"
return mosList
end offsets
Despite several repeat loops, this is extremely fast.
-- Peter
Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig
More information about the use-livecode
mailing list