Scintilla find text

Just starting out? Need help? Post your questions and find answers here.
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 183
Joined: Thu Dec 28, 2023 9:04 pm

Scintilla find text

Post by rndrei »

How to find a certain text?

Code: Select all

  ScintillaSendMessage(#SC,#SCI_FINDTEXT(SCFIND_NONE, "*end"))
It doesn't work!
AZJIO
Addict
Addict
Posts: 2223
Joined: Sun May 14, 2017 1:48 am

Re: Scintilla find text

Post by AZJIO »

Code: Select all

EnableExplicit
Define Search.SCTextToFind
Define *Text, pos

If OpenWindow(0, 0, 0, 330, 90, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

	If InitScintilla() ; 6.04
		ScintillaGadget(0, 10, 10, 320, 70, 0)

		*Text = UTF8("This is a simple ScintillaGadget with text... ")
		ScintillaSendMessage(0, #SCI_SETTEXT, 0, *Text)
		FreeMemory(*Text)

		*Text = UTF8("Sci")
		Search\chrg\cpMin = 0
		Search\chrg\cpMax = ScintillaSendMessage(0, #SCI_GETLENGTH)
		Search\lpstrText = *Text
		Search\chrgText\cpMin = 0
		Search\chrgText\cpMax = MemorySize(*Text)
		pos = ScintillaSendMessage(0, #SCI_FINDTEXT, 0, @Search)
		FreeMemory(*Text)
		ScintillaSendMessage(0, #SCI_SETSELECTIONSTART, pos, 0)
		ScintillaSendMessage(0, #SCI_SETSELECTIONEND, pos + Len("Sci") , 0)
	EndIf

	Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
And now the function

Code: Select all

EnableExplicit

#SciGt = 0
Define *Text


Procedure FindText(id, SearchText.s, flag = 0)
	Protected *Text, pos
	Protected Search.SCTextToFind

	*Text = UTF8(SearchText)
	Search\chrg\cpMin = 0
	Search\chrg\cpMax = ScintillaSendMessage(id, #SCI_GETLENGTH)
	Search\lpstrText = *Text
	Search\chrgText\cpMin = 0
	Search\chrgText\cpMax = MemorySize(*Text)
	pos = ScintillaSendMessage(id, #SCI_FINDTEXT, flag, @Search)
	FreeMemory(*Text)
	If pos > - 1
		ScintillaSendMessage(id, #SCI_SETSELECTIONSTART, pos, 0)
		ScintillaSendMessage(id, #SCI_SETSELECTIONEND, pos + Len(SearchText) , 0)
	EndIf
	ProcedureReturn pos
EndProcedure


If OpenWindow(0, 0, 0, 330, 90, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

	If InitScintilla()
		ScintillaGadget(#SciGt, 10, 10, 320, 70, 0)

		*Text = UTF8("This is a simple ScintillaGadget with text... ")
		ScintillaSendMessage(#SciGt, #SCI_SETTEXT, 0, *Text)
		FreeMemory(*Text)
		FindText(#SciGt, "th", #SCFIND_MATCHCASE) ; #SCFIND_WHOLEWORD, #SCFIND_WORDSTART, #SCFIND_REGEXP
	EndIf

	Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
It turns out that the structure returns the positions of what was found. Then we can continue our search

Code: Select all

EnableExplicit

#Window = 0

Enumeration
	#SciGt
	#btnNext
	#field
EndEnumeration

Define *Text, Search.SCTextToFind, SearchText.s, pos, Change

If Not InitScintilla()
	End
EndIf

Procedure FindText(id, *Search.SCTextToFind, flag = 0)
	Protected pos
	pos = ScintillaSendMessage(id, #SCI_FINDTEXT, flag, *Search)
	If pos > - 1
		ScintillaSendMessage(id, #SCI_SETSELECTIONSTART, *Search\chrgText\cpMin, 0)
		ScintillaSendMessage(id, #SCI_SETSELECTIONEND, *Search\chrgText\cpMax, 0)
	EndIf
	ProcedureReturn pos
EndProcedure


If OpenWindow(#Window, 0, 0, 530, 190, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	ScintillaGadget(#SciGt, 10, 10, 510, 130, 0)
	ButtonGadget(#btnNext, 10, 150, 100, 30, "Start")
	SearchText = "s"
	StringGadget(#field, 120, 150, 60, 30, SearchText)
	
	*Text = UTF8("This is a simple ScintillaGadget with text... ")
	ScintillaSendMessage(#SciGt, #SCI_SETTEXT, 0, *Text)
	FreeMemory(*Text)
	
	*Text = UTF8(SearchText)
	Search\chrg\cpMin = 0
	Search\chrg\cpMax = ScintillaSendMessage(#SciGt, #SCI_GETLENGTH)
	Search\lpstrText = *Text
	; 	Search\chrgText\cpMin = 0
	; 	Search\chrgText\cpMax = 0
	
	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Gadget
				Select EventGadget()
					Case #field
						If EventType() = #PB_EventType_Change
							Change = 1
							DisableGadget(#btnNext, #False)
							SetGadgetText(#btnNext, "Start")
						EndIf
					Case #btnNext
						If Change = 1
							Change = 0
							SearchText = GetGadgetText(#field)
							If Asc(SearchText)
								FreeMemory(*Text)
								*Text = UTF8(SearchText)
								Search\chrg\cpMin = 0
								Search\chrg\cpMax = ScintillaSendMessage(#SciGt, #SCI_GETLENGTH)
								Search\lpstrText = *Text
							EndIf
						EndIf
						If Asc(SearchText)
							pos = FindText(#SciGt, @Search, #SCFIND_MATCHCASE) ; #SCFIND_WHOLEWORD, #SCFIND_WORDSTART, #SCFIND_REGEXP
							If pos > -1
								Search\chrg\cpMin = Search\chrgText\cpMax
								SetGadgetText(#btnNext, "Next")
							Else
								DisableGadget(#btnNext, #True)
							EndIf
						EndIf
				EndSelect
			Case #PB_Event_CloseWindow
				FreeMemory(*Text)
				CloseWindow(#Window)
				End
		EndSelect
	ForEver
EndIf
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 183
Joined: Thu Dec 28, 2023 9:04 pm

Re: Scintilla find text

Post by rndrei »

Can't figure out how to output search text to a variable?

Code: Select all

find_string$=
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: Scintilla find text

Post by Axolotl »

well, I don't understand, because the find_string$ should be exactly like the searchtext$ or "".
Anyway I added some lines to extract the text between start and end position based on AZJIO's code.
Maybe this is what you asked for?

Code: Select all

Procedure.s GetFindText(id, *Search.SCTextToFind, flag = 0)
	Protected result.s, pos, txtr.TEXTRANGE

	pos = ScintillaSendMessage(id, #SCI_FINDTEXT, flag, *Search)
	If pos > - 1
		ScintillaSendMessage(id, #SCI_SETSELECTIONSTART, *Search\chrgText\cpMin, 0)
		ScintillaSendMessage(id, #SCI_SETSELECTIONEND, *Search\chrgText\cpMax, 0)

		txtr\chrg\cpMin = *Search\chrgText\cpMin  ; Start Position 
		txtr\chrg\cpMax = *Search\chrgText\cpMax  ; End Position 

		txtr\lpstrText = AllocateMemory(txtr\chrg\cpMax - txtr\chrg\cpMin + 1) 
		ScintillaSendMessage(id, #SCI_GETTEXTRANGE, 0, txtr)  ; Get Text between Start and End Position 
		result = PeekS(txtr\lpstrText, -1, #PB_Ascii) 
		FreeMemory(txtr\lpstrText) 
	EndIf   
	Debug "INFO: return == '" + result + "'" 
	ProcedureReturn result 
EndProcedure 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 183
Joined: Thu Dec 28, 2023 9:04 pm

Re: Scintilla find text

Post by rndrei »

Yes, that's what you need!
Post Reply