LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
lohill
- Posts: 770
- Joined: Tue Dec 08, 2009 6:37 pm
- Location: San Diego, CA USA
Post
by lohill » Tue Jun 01, 2021 6:16 pm
I have a text field that I would like to search for a particular string of characters. For each occurrence of that string which is found I would like to change the color of those characters to red. If I use "Find" on the field I can put little boxes around all the occurrences but can see no way to get the color changed to red. Now I am trying to use "offset" with "charsToSkip" and have come up with something that finds and colorizes the first occurrence but not the rest. Create a field "Script" with some text that has a particular string of characters occurring more than one time and make a button with the following mouseUp code:
Code: Select all
on mouseUp pMouseButton
put field "Script" into tText
ask information "Enter search characters"
put it into tFind
put len(tFind) into tFindLen
put 0 into tSkip
repeat
put offset(tFind,tText,tSkip) into nPos
if nPos=0 then exit repeat
put nPos + tFindLen into tEndPos
put the rtftext of char nPos to tEndPos of field "Script" into tRTFtext
replace "red0" with "red255" in tRTFtext
set the rtftext of char nPos to tEndPos of field "Script" to tRTFtext
put tEndPos+1 into tSkip
end repeat
end mouseUp
Put a break point at some convenient place inside the repeat loop because it helps you see what is going on and it will most likely be needed to get out of the loop. If you see a way to make this work please let me know. I am also open to any better approaches you might suggest.
Thanks,
Larry
-
dunbarx
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Post
by dunbarx » Tue Jun 01, 2021 10:17 pm
Hi.
Let us use the word "time" as the word of interest. Make a test field with some text in it, with "time" sprinkled randomly throughout. Now make a button with this in its script:
Code: Select all
on mouseUp
put 0 into wordsToSkip
repeat until wordOffset( "time",fld 1,wordsToSkip) = 0
put wordOffset( "time",fld 1,wordsToSkip) into lastWord
add lastWord to wordsToSkip
set the foreColor of word wordsToSkip of fld 1 to "red"
end repeat
end mouseUp
Check out this function in the dictionary, and you will see how it goes.
You can shorten this, just because:
Code: Select all
on mouseUp
put 0 into wordsToSkip
repeat until wordOffset( "time",fld 1,wordsToSkip) = 0
add wordOffset( "time",fld 1,wordsToSkip) to wordsToSkip
set the foreColor of word wordsToskip of fld 1 to "red"
end repeat
end mouseUp
Craig
-
FourthWorld
- VIP Livecode Opensource Backer
- Posts: 9802
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
-
Contact:
Post
by FourthWorld » Tue Jun 01, 2021 10:35 pm
Larry, it's been a long time since I've worked with RTF, but IIRC style runs in RTF don't operate in a way that will lend itself to what that code is doing.
It may be simpler to use htmlText text, e.g.:
Code: Select all
on mouseup
ask information "Enter search characters"
put it into tFind
--
put the htmlText of field "Script" into tHtmlText
replace tFind with "<font color=red>"& tFind &"</font>" in tHtmlText
set the htmlText of fld "Script" to tHtmlText
end mouseup
-
lohill
- Posts: 770
- Joined: Tue Dec 08, 2009 6:37 pm
- Location: San Diego, CA USA
Post
by lohill » Thu Jun 03, 2021 4:14 am
Thanks Craig and Richard,
Both these suggestions give me new approaches to my problem and they are both certainly better. Craigs use of "Word" does not give me the flexibility of working with text which are not simply LC words. I'll spend some time to see if "offset" rather than "wordOffset" can be used with Craig's code. LiveCode offers so many ways to do something and it is always nice to see new approaches. ( I wish I knew more.) I'm glad you guys are willing to help.
Larry
-
dunbarx
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Post
by dunbarx » Thu Jun 03, 2021 2:43 pm
Hi.
Not to derail the fun you are having with the fun I am having, try this variant of the "offset" idea. With a field 1 filled with text, and containing several disparate instances of a certain string, put this into a button script:
Code: Select all
on mouseUp
put 0 into charsToSkip
ask "Find what?"
put it into tText
put the length of tText into stringLength
repeat until offset(tText,fld 1,charsToSkip) = 0
get offset(tText,fld 1,charsToSkip)
set the foreColor of char (it + charsToSkip) to (it + charsToSkip + stringLength) of fld 1 to "green"
add it + stringLength to charsToSkip
end repeat
end mouseUp
Because a string of characters is sort of "spread out", as opposed to nice compact individual words, the extra fooling around is necessary.
I think.
Craig
-
lohill
- Posts: 770
- Joined: Tue Dec 08, 2009 6:37 pm
- Location: San Diego, CA USA
Post
by lohill » Thu Jun 03, 2021 6:27 pm
Craig,
The fooling around is necessary as you say but your latest rendition is picking up one more character than it should. You don't see it when the last char being colorized is a space. Your "Set" line should look like this:
set the foreColor of char (it + charsToSkip) to (it + charsToSkip + stringLength - 1) of fld 1 to "green"
Aside from my use of "rtftext" as opposed to your "forecolor", you did what I thought I was doing with my original script.( i.e charsToSkip just keeps getting bigger)
Here is How I paraphrased your script so I could see what was going on:
Code: Select all
on mouseUp
put 0 into charsToSkip
ask "Find what?"
put it into tText
put the length of tText into stringLength
repeat until offset(tText,fld "Script",charsToSkip) = 0
put offset(tText,fld "Script",charsToSkip) into nPos
put nPos + charsToSkip into nStart
put nPos + charsToSkip + stringLength - 1 into nEnd
set the foreColor of char nStart to nEnd of fld "Script" to "red"
add nPos + stringLength to charsToSkip
end repeat
end mouseUp
Larry
-
dunbarx
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Post
by dunbarx » Thu Jun 03, 2021 7:25 pm
Larry.
Shows what can happen when one is coding while driving.
And everyone has their own way to name variables. I am sloppy that way, and usually pay for it when I have to go back and fiddle around.
Craig
-
lohill
- Posts: 770
- Joined: Tue Dec 08, 2009 6:37 pm
- Location: San Diego, CA USA
Post
by lohill » Thu Jun 03, 2021 9:44 pm
I did finally find the errors in my original script thanks to you restoring my confidence in offset using charsToSkip. It now looks like this but I won't use it after seeing yours and Richards versions. My scripts are usually pretty ugly but fortunately LiveCode is so efficient that they seem to run quickly enough for me.
Code: Select all
on mouseUp pMouseButton
put field "Script" into tText
ask information "Enter search characters"
put it into tFind
put len(tFind) into tFindLen
put 0 into tSkip
repeat
put offset(tFind,tText,tSkip) into nPos
if nPos=0 then exit repeat
put nPos+ tSkip into tStartPos
put nPos + tSkip + tFindLen - 1 into tEndPos
put the rtftext of char tStartPos to tEndPos of field "Script" into tRTFtext
replace "red0" with "red255" in tRTFtext
set the rtftext of char tStartPos to tEndPos of field "Script" to tRTFtext
put tEndPos into tSkip
end repeat
end mouseUp
Larry