Date: 02-16-2022
Return to Index
created by gbSnippets
'A lexer scans text and breaks it up into separate language objects,
'e.g. keywords, strings, operators. The lexer then uses a different style
'to draw each object, typically with common background colors but with
'different foreground colors. This is often referred to as syntax
'highlighting.
'In addition to defining white space, punctuation characters, and other
'source code syntax characters, most lexers work off a single set of
'keywords to which syntax highlighting is applied.
'Some languages, however, such as HTML, may use several sets of keywords.
'This may be useful, for example, for keeping JavaScript or other embedded
'languages separate from the HTML language.
'Each language lexer implements its own unique styling.
'Once text is styled, the application can further style the text (such
'as creating URL hotspots).
'You can implement a lexer in 3 different ways
'1. Built-In (part of the Scintilla DLL)
SendMessage hSci, %SCI_SETLEXER, %SCLEX_PowerBASIC, 0 'built-in PB index
'2. External Lexer (create your own custom DLL)
txt = "powerbasic_lexer.dll"
SendMessage hSci, %SCI_LoadLexerLibrary, 0, StrPTR$(txt)
txt = "powerbasic_custom"
SendMessage hSci, %SCI_SETLEXERLANGUAGE, 0, StrPTR$(txt)
'3. Container (write the lexer code in container application)
SendMessage hSci, %SCI_SETLEXER, %SCLEX_CONTAINER, 0 'built-in PB index
'This snippet discusses use of built-in lexers.
'Built-in lexers are assigned a number (see the Scintilla documentation for
'a complete list). Some popular built-in lexer equates are:
' %SCLEX_CPP = 3
' %SCLEX_HTML = 4
' %SCLEX_Perl = 6
' %SCLEX_VB = 8
' %SCLEX_PowerBASIC = 51
'Lexers do not provide the list of keywords to which syntax highlighting will be
'applied. The container application must provide the list.
'Nor do lexers provide the colors that will be used in syntax highlighting keywords.
'The container application must define the color choices by assigning colors to
'styles that are referenced in the lexer code. You'll have to look at each lexer
'to know what styles to modify, but here are the ones which are used in the
'PowerBASIC lexer.
' SCE_B_Keyword SCE_B_Default SCE_B_Date
' SCE_B_String SCE_B_Comment SCE_B_Constant
' SCE_B_Number SCE_B_Operator SCE_B_ASM
'Primary Code:
'The steps for using a built in lexer are very straight forward
'1. provide a list of keywords and send it to Scintilla with this message
KeyWords = "case if else end select then" + Chr$(0)
SendMessage hSci, %SCI_SETKEYWORDS, 0, ByVal StrPTR(KeyWords) 'define keywords
'2. set the lexer to use with this message
SendMessage hSci, %SCI_SETLEXER, %SCLEX_PowerBASIC, 0 'user PB lexer
'3. set the colors of the styles your lexer uses
SendMessage hSci, %SCI_STYLESETFORE, %SCE_B_KEYWORD, Rgb(0, 0, 255) 'keyword FGcolor
SendMessage hSci, %SCI_STYLESETFORE, %SCE_B_STRING, Rgb(255, 0, 255) 'string color
SendMessage hSci, %SCI_STYLESETFORE, %SCE_B_NUMBER, Rgb(192,100,0) 'number colors
'Note that if you change to a new lexer you'll need to apply the new lexer
'syntax highlighting as follows
SendMessage hSci, %SCI_SETLEXER, %SCLEX_PowerBASIC, 0 'built-in PB lexer
SendMessage hSci, %SCI_Colourise, 0, -1 'colourize from 0 to end-of-doc
'Also, if you decide to change to using no lexer, to remove the styles applied by
'the previous lexer you'll have to manually set the style yourself for the entire document
SendMessage hSci, %SCI_SETLEXER, %SCLEX_Null, 0 'change to no lexer
SendMessage(hSci, %SCI_StartStyling, 0, 31) 'set start of styling to pos 0
iLen = SendMessage(hSci, %SCI_GetTextLength, 0, 0) 'get len of document
SendMessage(hSci, %SCI_SetStyling, iLen, %Style_Default) 'set style of entire document
'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 : %ID_BtnB = 1002
Global hDlg, hSci, hLib As DWord
Function PBMain() As Long
hLib = LoadLibrary("SCILEXER.DLL")
Dialog New Pixels, 0, "Scintilla Example",300,300,370,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %ID_BtnA, "Highlight On", 10,10,90,20, %WS_Child Or %WS_Visible
Control Add Button, hDlg, %ID_BtnB, "Highlight Off", 10,40,90,20, %WS_Child Or %WS_Visible
Control Add "Scintilla", hDlg, %ID_Sci, "", 120,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
Select Case CB.Msg
Case %WM_InitDialog
InitializeSci
PostMessage hSci, %SCI_SetSel, 0,0 'unselect initially
Case %WM_Command
Select Case CB.Ctl
Case %ID_BtnA : TestA
Case %ID_BtnB : TestB
End Select
Case %WM_Size
Control Set Size hDlg, %ID_Sci, Lo(Word, CB.lParam)-130, Hi(Word, CB.lParam)-20
Case %WM_Destroy
If hLib Then FreeLibrary hLib ' Free the Scintilla library
End Select
End Function
Sub InitializeSci
Local txt, KeyWords As String, iResult As Long
KeyWords = "case end function select"
txt = "Function Test(x as Long) As Long"
txt = txt + $crlf + "Select Case x"
txt = txt + $crlf + " Case 1 : y = 3"
txt = txt + $crlf + " Case 2 : y = 4"
txt = txt + $crlf + "End Select" + Chr$(0)
SendMessage hSci, %SCI_STYLESETFORE, %SCE_B_KEYWORD, Rgb(0, 0, 255) 'keyword FGcolor
SendMessage hSci, %SCI_STYLESETFORE, %SCE_B_STRING, Rgb(255, 0, 255) 'string color
SendMessage hSci, %SCI_STYLESETFORE, %SCE_B_NUMBER, Rgb(192,100,0) 'number colors
SendMessage hSci, %SCI_SETKEYWORDS, 0, ByVal StrPTR(KeyWords) 'define PB keywords
SendMessage(hSci, %SCI_SetText, 0, StrPTR(txt)) 'set text
SendMessage hSci, %SCI_SetMarginWidthN, 0, 20 'display line numbers
End Sub
Sub TestA
SendMessage hSci, %SCI_SETLEXER, %SCLEX_PowerBASIC, 0 'built-in PB lexer
SendMessage hSci, %SCI_Colourise, 0, -1 'colourize from 0 to end-of-doc
End Sub
Sub TestB
'just turning off the lexer isn't enough. you must change the
'style of the entire document.
Local iLen As Long
SendMessage hSci, %SCI_SETLEXER, %SCLEX_Null, 0 'no lexer
SendMessage(hSci, %SCI_StartStyling, 0, 31) 'set start of styling to pos 0
iLen = SendMessage(hSci, %SCI_GetTextLength, 0, 0) 'get len of document
SendMessage(hSci, %SCI_SetStyling, iLen, %Style_Default) 'set style of entire document
End Sub
'gbs_00663
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm