Highlight instances of found text

Peter Brigham MD pmbrig at gmail.com
Tue Jul 7 12:22:49 EDT 2009


I'm a little late to this party, Sivakatirswami, but if you haven't  
found something better, try this. (The offsets function is quite fast;  
it's one of my utility functions when I need all instances of a string  
in a text block.)

on colorize tString, fldRef
    put length(tString) into tLen
    put the text of fldRef into tText
    put offsets(tString,tText) into oList
    if oList = 0 then exit colorize
    repeat for each item i in oList
        set the textColor of char i to i+tLen-1 of fldRef to 247,9,9
        -- or whatever color you like
    end repeat
end colorize

on revertToBlack fldRef
    set the textColor of char 1 to -1 of fldRef to black
end revertToBlack

function offsets str, at cntr
   -- returns a comma-delimited list of all the offsets of str in cntr
    -- cntr is passed by reference to avoid unnecessary duplication
    -- of very large text blocks in memory
    -- cntr contents are not altered by this function
    if str is not in cntr then return 0
    put "" into osList
    put 0 into startPoint
    repeat
       put offset(str,cntr,startPoint) into os
       if os = 0 then exit repeat
       add os to startPoint
       put startPoint & comma after osList
    end repeat
    delete last char of osList
    return osList
end offsets

If you want only whole words colorized, you can use this:

on colorizeWords tString, fldRef
    put the text of fldRef into tText
    put the number of words of tString into L
    put wordOffsets(tString,tText) into oList
    if oList = 0 then exit colorizeWords
    repeat for each item i in oList
       if word i of tText <> tString then next repeat
       set the textColor of word i to i+L-1 of fldRef to 247,9,9
       -- or whatever color
    end repeat
end colorizeWords

function wordOffsets str, at cntr
    -- returns a comma-delimited list of all the wordoffsets of str in  
cntr
    -- not limited to whole words (will catch str as part of a word)
    -- cntr is passed by reference to avoid unnecessary duplication
    -- of very large text blocks in memory
    -- cntr contents are not altered by this function
    put offsets(str,cntr) into charList
    if charList = 0 then return 0
    put "" into woList
    repeat for each item i in charList
       put the number of words of (char 1 to i of cntr) & "," after  
woList
    end repeat
    delete char -1 of woList
    return woList
end wordOffsets

You can add caseSensitive = true if you like.

One caveat about the word colorizing routine -- Rev is idiosyncratic  
in defining anything enclosed by quotes as a single word. (Why?? If  
you command-right-arrow to step through words of a text, Rev knows to  
treat quotation marks as just another character and will move from  
word to word the way any other text app does, so the engine knows what  
the usual definition of a word is. Bug report anyone?) To colorize all  
words including those within a quotation, you have to first replace  
the quote character by some other character in the field, then restore  
the quotes.

(The obvious remaining utility functions, itemOffsets() and  
lineOffsets(), are left as an exercise for the reader, but are quite  
handy to have in a library.)

HTH,

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig



On Jul 3, 2009, at 10:41 PM, Sivakatirswami wrote:

> Does anyone have a function that will highlight/colorize instances  
> of found text in a field?
>
> e.g. say we have a search function, user types "chapati"
>
> In the particular ebook I have, all instances (lines or paragraphs  
> containing) of "chapati" are "gathered" and then posted to a field  
> for review... I would like to have all instance of "chapati" in that  
> field be highlighted or colorized.
>
> I'm sure I can figure it out but someone I'm sure has already cooked  
> this dosai before and I'm a fan of using recipes.
>
> Thanks
> Sivakatirswami




More information about the use-livecode mailing list