Date: 02-16-2022
Return to Index
created by gbSnippets
'Most text searches are for a specific string of text. But some searches, such as
'finding all words that start with a particular letter, cannot be represented as
'a single, specific text string.
'That's where regular expressions come in - allowing the programmer to create a
'search template which may describe many strings. Although Scintilla recognizes
'a limited number of regex special characters, it's capabilities are adequate
'for many useful searches.
'Scintilla also allows you to provide your own RegExp function within the container.
'See the Scintilla documentation for more details.
'Primary Code:
'There are two specific differences in performing a RegExp search, as compared to
'performing a normal text search.
'First, the search text must be replaced by a regular expression:
'text search
FindText = "find me" + Chr$(0) 'search for the text "find me"
FindStructure.lpstrText = StrPTR(FindText)
'regexp search
FindText = "\<E.*" + Chr$(0) 'search for any word starting with "E"
FindStructure.lpstrText = StrPTR(FindText)
'Second, the flag used in %SCI_FindText must include %SCFind_RegExp
iResult = SendMessage (hSci, %SCI_FindText, %SCFind_RegExp, VarPTR(FindStructure))
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "scintilla_gb.inc"
%ID_Sci = 1000 : %ID_BtnA = 1001
Global hDlg, hSci, hLib As DWord
Function PBMain() As Long
hLib = LoadLibrary("SCILEXER.DLL")
Dialog New Pixels, 0, "Scintilla Example",300,300,300,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %ID_BtnA, "Find Next", 10,10,70,20, %WS_Child Or %WS_Visible
Control Add "Scintilla", hDlg, %ID_Sci, "", 100,10,180,130, %WS_Child Or %WS_Visible
Control Handle hDlg, %ID_Sci To hSci 'get handle to Scintilla window
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local txt As String
txt = "Select Case var$ 'first line" + $CrLf + "End Select 'last line" + Chr$(0)
Select Case CB.Msg
Case %WM_InitDialog
InitializeScintilla
PostMessage hSci, %SCI_SetSel, 0,0 'unselect initially
Case %WM_Command
Select Case CB.Ctl
Case %ID_BtnA : TestA
End Select
Case %WM_Size
Control Set Size hDlg, %ID_Sci, Lo(Word, CB.lParam)-110, Hi(Word, CB.lParam)-20
Case %WM_Destroy
If hLib Then FreeLibrary hLib 'free the Scintilla library
End Select
End Function
Sub InitializeScintilla
Local txt As String
txt = "If x = 2 Then" + $CrLf + " 'do nothing" + $Crlf
txt = txt + "Else" + $crlf + " x = 2" + $crlf + "End If" + Chr$(0)
SendMessage hSci, %SCI_SetText, 0, StrPTR(txt)
SendMessage hSci, %SCI_SetMarginWidthN, 0, 20
End Sub
Sub TestA
Static NewPos As Long
Local iResult As Long
Local xFindText As String
Local FindStructure As Sci_TextToFind
'set Find parameters
xFindText = "\<E.*\>" + Chr$(0) 'word starting with E
FindStructure.Chrg.cpMin = SendMessage(hSci,%SCI_GetCurrentPos,0,0)
FindStructure.Chrg.cpMax = SendMessage(hSci,%SCI_GetLength,0,0)
FindStructure.lpstrText = StrPTR(xFindText)
'find, then select matching string
iResult = SendMessage(hSci, %SCI_FindText, %SCFind_RegExp, VarPTR(FindStructure))
If iResult <> -1 Then
SendMessage hSci, %SCI_SetSel, FindStructure.ChrgText.cpmin, FindStructure.ChrgText.cpmax
Else
'take no action
End If
Control Set Focus hDlg, %ID_Sci
End Sub
'gbs_00638
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm