Highlight instances of found text
Sivakatirswami
katir at hindu.org
Wed Jul 8 01:54:07 EDT 2009
Aloha and Namaste, Peter
Thank you! this is very helpful... it's what I was looking for, but my
string manipulation skills are not as strong as yours and I defaulted to
the old "hack" which is to use find and then FoundChunk, which always
*seems* to work well, but often introduces some unsolvable gremlin.
e.g. my function passes all lines/Paragraphs containing the search
string to a field for review, highlighting every instance of the found
text. The user can then print this out after editing it.
Using "Supreme Being" it found all the instances, but for some
mysterious reason highlighted all instances except
"Supreme Being;
where the phrase was bounded by a quotation mark and a semi colon, the
find function will not find that string a such if "find whole" is
used... but your routine surely will...
I will try yours instead.
I offer the user both options "Partial Match" (any string in any
location) or "Whole words" so I can use both of these routines Thanks!
# example of the "Find... Foundchunk" "hack"
on gatherRefs
# set the next instance hilite color back to light blue
Global gHiliteColorNo
put 1 into gHiliteColorNo
push card
ask "What topic?"
if it is empty then exit gatherRefs
put it into jai
Put "REFERENCES TO " "e& Jai "e& " in the Saiva Dharma
Shastras" & cr &cr into Output
repeat with x = 2 to (the number of cards of this stack)
if (fld "paraSubject" of cd x contains jai) or (fld "text" of cd
x contains jai) then
put fld "ParaNumber" of cd x && fld "ParaSubject" of cd x &cr&
fld "text" of cd x & cr & cr after output
end if
end repeat
Put "GLOSSARY REFERENCES" & " TO " "e& Jai "e& " in the Saiva
Dharma Shastras"& cr & cr after output
open stack "glossary"
repeat with x = 1 to (the number of cards of this stack)
put fld "text" of card x into tEntries
repeat for each line y in tEntries
if y contains jai then put y & cr & cr after tGlossaryResults
end repeat
end repeat
if tGlossaryResults <> empty then
put tGlossaryResults after output
else
put "--NONE" after output
end if
close stack "glossary"
put output into fld "ref-text" of cd 1 of stack "read references"
open stack "Read References"
put jai into fld "readRef_StringToFind"
repeat until FoundIt is empty
find whole jai in fld "ref-Text"
put the foundChunk into FoundIt
if foundIt is not empty then
set the backgroundcolor of foundit to yellow
end if
end repeat
set the scroll of fld "ref-text" to 0
pop card
toplevel stack "Read References"
end gatherRefs
Peter Brigham MD wrote:
> 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
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
>
More information about the use-livecode
mailing list