), I'm not strong in regular expressions.
Please help fix my clumsy code.
Code: Select all
;/////////////////////////////////////////////////////////////////////////////////
;***Go-Scintilla 2***
;/////////////////////////////////////////////////////////////////////////////////
XIncludeFile "GoScintilla.pbi"
Procedure GOSCI_SetLineHighlightColor(id, color)
#GOSCI_HIGHLIGHTERMARKERNUM=#GOSCI_BOOKMARKMARKERNUM+1
ScintillaSendMessage(1, #SCI_MARKERSETBACK, #GOSCI_HIGHLIGHTERMARKERNUM, color)
ScintillaSendMessage(1, #SCI_MARKERDEFINE, #GOSCI_HIGHLIGHTERMARKERNUM, #SC_MARK_BACKGROUND)
EndProcedure
Procedure GOSCI_SetLineHighlighted(id, lineIndex, flag=#True)
If IsGadget(id) And GadgetType(id)=#PB_GadgetType_Scintilla
If flag
ScintillaSendMessage(id, #SCI_MARKERADD, lineIndex, #GOSCI_HIGHLIGHTERMARKERNUM)
Else
ScintillaSendMessage(id, #SCI_MARKERDELETE, lineIndex, #GOSCI_HIGHLIGHTERMARKERNUM)
EndIf
EndIf
EndProcedure
Declare.i MyLineStyler(id, *utf8Buffer.ASCII, numUtf8Bytes, currentLine, startLine, originalEndLine)
;Initialise the Scintilla library for Windows.
CompilerIf #PB_Compiler_OS=#PB_OS_Windows
InitScintilla()
CompilerEndIf
If OpenWindow(0, 100, 200, 700, 600, "Test!", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
;Required for the tab key to function correctly when the Scintilla control has the focus.
RemoveKeyboardShortcut(0, #PB_Shortcut_Tab)
RemoveKeyboardShortcut(0, #PB_Shortcut_Tab | #PB_Shortcut_Shift)
;Create our Scintilla control. Note that we do not specify a callback; this is optional for GoSctintilla.
GOSCI_Create(1, 10, 10, 680, 580, 0, #GOSCI_AUTOSIZELINENUMBERSMARGIN)
;Set the padding added to the width of the line-number margin.
GOSCI_SetAttribute(1, #GOSCI_LINENUMBERAUTOSIZEPADDING, 10)
;Set folding symbols margin width.
GOSCI_SetMarginWidth(1, #GOSCI_MARGINFOLDINGSYMBOLS, 12)
;Set font.
GOSCI_SetFont(1, "Courier New", 12)
;Set tabs. Here we use a 'hard' tab in which a tab character is physically inserted. Set the 3rd (optional) parameter to 1 to use soft-tabs.
GOSCI_SetTabs(1, 3)
;Set colors of text area
GOSCI_SetColor(1, #GOSCI_FORECOLOR, $FFFFFF)
GOSCI_SetColor(1, #GOSCI_BACKCOLOR, $9f5000)
;Set colors of caret (caret color and the back color of the line containing the caret)
GOSCI_SetColor(1, #GOSCI_CARETFORECOLOR, $FFFFFF)
GOSCI_SetColor(1, #GOSCI_CARETLINEBACKCOLOR, $b35900)
;Set colors of selection
GOSCI_SetColor(1, #GOSCI_SELECTIONBACKCOLOR, $8080ff)
GOSCI_SetColor(1, #GOSCI_SELECTIONFORECOLOR, $000000)
;Set colors of line margin
GOSCI_SetColor(1, #GOSCI_LINENUMBERFORECOLOR, $c0c0c0)
GOSCI_SetColor(1, #GOSCI_LINENUMBERBACKCOLOR, $804000)
;Set colors of fold margin
GOSCI_SetColor(1, #GOSCI_FOLDMARGINHIBACKCOLOR, $804000)
GOSCI_SetColor(1, #GOSCI_FOLDMARGINLOBACKCOLOR, $804000)
;Set colors of fold markers
GOSCI_SetColor(1, #GOSCI_FOLDMARKERSFORECOLOR, $804000)
GOSCI_SetColor(1, #GOSCI_FOLDMARKERSBACKCOLOR, $c0c0c0)
;Highlighters
;==========
;Set colors of line highlighter.
GOSCI_SetLineHighlightColor(1, $aa5500)
;Set styles for our syntax highlighting.
;=======================================
;First define some constants to identify our various styles.
;You can name these as we wish.
Enumeration
#STYLES_COMMANDS=1
#STYLES_VARIABLES
#STYLES_COMMENTS
#STYLES_LITERALSTRINGS
#STYLES_NUMBERS
#STYLES_CONSTANTS
#STYLES_FUNCTIONS
#STYLES_SEPARATORS
#STYLES_OPERATORS
#STYLES_TYPES
EndEnumeration
;Set individual styles for commands.
GOSCI_SetStyleFont(1, #STYLES_COMMANDS, "", -1, #PB_Font_Bold)
GOSCI_SetStyleColors(1, #STYLES_COMMANDS, $ffff80)
;Set individual styles for comments.
GOSCI_SetStyleColors(1, #STYLES_COMMENTS, $00ffff)
;Set individual styles for literal strings.
GOSCI_SetStyleColors(1, #STYLES_VARIABLES, $00ff00)
;Set individual styles for numbers.
GOSCI_SetStyleColors(1, #STYLES_NUMBERS, $FF00FF)
;Set individual styles for constants.
GOSCI_SetStyleColors(1, #STYLES_CONSTANTS, $66b3ff) ;We have omitted the optional back color.
;Set individual styles for functions.
GOSCI_SetStyleColors(1, #STYLES_FUNCTIONS, $fdb382) ;We have omitted the optional back color.
;Set individual styles for separators.
GOSCI_SetStyleColors(1, #STYLES_SEPARATORS, $0032FF) ;We have omitted the optional back color.
;Set individual styles for operators.
GOSCI_SetStyleColors(1, #STYLES_OPERATORS, $8080ff) ;We have omitted the optional back color.
;Set individual styles for types.
GOSCI_SetStyleColors(1, #STYLES_TYPES, $ffccff) ;We have omitted the optional back color.
;Set delimiters and keywords for our syntax highlighting.
;========================================================
;Delimiters.
;First set up a ; symbol to denote a comment.
GOSCI_AddDelimiter(1, ";", "", #GOSCI_DELIMITTOENDOFLINE, #STYLES_COMMENTS)
;Now set up quotes to denote literal strings.
GOSCI_AddDelimiter(1, "%", "%", #GOSCI_DELIMITBETWEEN, #STYLES_VARIABLES)
;Now set up a # symbol to denote a constant.
GOSCI_AddDelimiter(1, ":",Chr(34),#GOSCI_RIGHTDELIMITWITHOUTWHITESPACE , #STYLES_CONSTANTS)
GOSCI_AddKeywords(1, ";!@Install@!UTF-8!", #STYLES_COMMENTS, #GOSCI_OPENFOLDKEYWORD | #GOSCI_ADDTOCODECOMPLETION)
GOSCI_AddKeywords(1, ";!@InstallEnd", #STYLES_COMMENTS, #GOSCI_CLOSEFOLDKEYWORD | #GOSCI_ADDTOCODECOMPLETION)
;Basic command keywords.
GOSCI_AddKeywords(1, "Title Directory Shortcut Shortcut1 Flags SetEnvironment", #STYLES_COMMANDS)
GOSCI_AddKeywords(1,"Name FullName Vers ",#STYLES_CONSTANTS,#GOSCI_ADDTOCODECOMPLETION)
;Add separator keywords
GOSCI_AddKeywords(1, "/ \ { } = Chr(34) , .", #STYLES_SEPARATORS)
;Add operator keywords
GOSCI_AddKeywords(1, " + - * & | < > -", #STYLES_OPERATORS)
;Additional lexer options.
;=========================
GOSCI_SetLexerOption(1, #GOSCI_LEXEROPTION_SEPARATORSYMBOLS, @"/\={}Chr(34),-+")
GOSCI_SetLexerOption(1, #GOSCI_LEXEROPTION_NUMBERSSTYLEINDEX, #STYLES_NUMBERS)
;===========================================
GOSCI_SetLineStylingFunction(1, @MyLineStyler())
;======================
text$ = ";!@Install@!UTF-8!"+ #CRLF$
text$ + ";{ GoScintilla." + #CRLF$ + #CRLF$ + #CRLF$
text$ + ";} By Stephen Rodriguez." + #CRLF$ + #CRLF$
text$ + "SetEnvironment=" + Chr(34) + "Name=Programs" + Chr(34) + #CRLF$
text$ + "SetEnvironment=" + Chr(34) + "Vers=0.0.0." + Chr(34) + #CRLF$
text$ + "Title=" + Chr(34) + "Bla-Bla-Bla %Name% %Vers%" + Chr(34) + #CRLF$
text$ + "Flags=" + Chr(34) + "2+4+8+16+32+2048+4096" + Chr(34) + #CRLF$
text$ + "Directory=" + Chr(34) + "x86:fm20:\"+ Chr(34) + "%StartEXE%\" + Chr(34) + #CRLF$
text$ + "Shortcut=" + Chr(34) + "Du,{%%T\\Name.exe},{},{},{},{%FullName%}" + Chr(34) + #CRLF$
text$ + "Shortcut1=" + Chr(34) + "Pu,{%%S\\Name2.exe},{},{},{},{%FullName%},{1}" + Chr(34) + #CRLF$ + #CRLF$
text$ + ";!@InstallEnd" + #CRLF$
GOSCI_SetText(1, text$)
Repeat
eventID=WaitWindowEvent()
Select eventID
Case #PB_Event_Gadget
Select EventGadget()
EndSelect
EndSelect
Until eventID=#PB_Event_CloseWindow
GOSCI_Free(1)
EndIf
;/////////////////////////////////////////////////////////////////////////////////
;The following is our user-defined line-styling function, called whenever GoScintilla is called upon to style lines.
Procedure.i MyLineStyler(id, *utf8Buffer.ASCII, numUtf8Bytes, currentLine, startLine, originalEndLine)
Protected result=#GOSCI_STYLELINESASREQUIRED, numBytesStyled, symbolJustStyled$, *ptrAscii.ASCII
Static regexVAR
If Not regexVAR
;{ Init search patterns
Enumeration
#Highlight_Remove=-1
#Highlight_None=0
#Highlight_Visible=1
#Highlight_End=2
EndEnumeration
regexVAR=CreateRegularExpression(#PB_Any, "[%][%][\S]") ; %%S %%T %%^S %%^T
;}
EndIf
lineHighlighting=GOSCI_GetLineData(id, currentLine-1)
While numUtf8Bytes
Dim foundSymbol$(0)
Select *utf8Buffer\a
Case ';'
;{ search fold markers to update fold levels
textToExamine$=LCase(PeekS(*utf8Buffer, numUtf8Bytes, #PB_UTF8))
Select Left(textToExamine$, 2)
Case ";{"
GOSCI_IncFoldLevel(id)
Case ";}"
GOSCI_DecFoldLevel(id)
EndSelect
; Select Left(textToExamine$, 2)
; Case ";!"
; GOSCI_IncFoldLevel(id)
; Case ";!"
; GOSCI_DecFoldLevel(id)
; EndSelect
Case '%'
textToExamine$=LCase(PeekS(*utf8Buffer, numUtf8Bytes, #PB_UTF8))
If ExtractRegularExpression(regexVAR, textToExamine$, foundSymbol$()) Or ExtractRegularExpression(regexBIN, textToExamine$, foundSymbol$())
numBytesStyled=Len(foundSymbol$(0))
ScintillaSendMessage(id, #SCI_SETSTYLING, numBytesStyled, #STYLES_VARIABLES)
numUtf8Bytes-numBytesStyled
*utf8Buffer+numBytesStyled
Continue
EndIf
EndSelect
numBytesStyled=GOSCI_StyleNextSymbol(id, *utf8Buffer, numUtf8Bytes)
symbolJustStyled$=LCase(PeekS(*utf8Buffer, numBytesStyled, #PB_UTF8))
If symbolJustStyled$=";!@Install@!UTF-8!"
lineHighlighting=#Highlight_Visible
ElseIf symbolJustStyled$=";!@InstallEnd"
If lineHighlighting=#Highlight_Visible
lineHighlighting=#Highlight_End
Else
lineHighlighting=#Highlight_Remove
EndIf
EndIf
numUtf8Bytes-numBytesStyled
*utf8Buffer+numBytesStyled
Wend
; If GOSCI_GetLineData(id, currentLine)<>lineHighlighting
; Select lineHighlighting
; Case #Highlight_Visible
; GOSCI_SetLineData(id, currentLine, #Highlight_Visible)
; GOSCI_SetLineHighlighted(id,currentLine,1)
; Case #Highlight_End
; GOSCI_SetLineData(id, currentLine, #Highlight_None)
; GOSCI_SetLineHighlighted(id, currentLine, 1)
; Case #Highlight_None, #Highlight_Remove
; GOSCI_SetLineData(id, currentLine, #Highlight_None)
; GOSCI_SetLineHighlighted(id, currentLine, 0)
; EndSelect
; result=#GOSCI_STYLENEXTLINEREGARDLESS
; EndIf
ProcedureReturn result
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////