GoScintilla - 2.7 (Purebasic 4.5 onwards)

Developed or developing a new product in PureBasic? Tell the world about it.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GoScintilla - 2.0 (beta 3)

Post by ts-soft »

Sry, next problem.
How can i add "REM" for comments?

greetings
Thomas
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: GoScintilla - 2.0 (beta 3)

Post by srod »

ts-soft wrote:Sry, next problem.
How can i add "REM" for comments?

greetings
Thomas
Well, as you are undoubtedly aware, you will probably need a delimiter of type #GOSCI_DELIMITTOENDOFLINE (unless a : symbol separates lines etc.) but GoScintilla delimiters are single characters only!

You will therefore need to supplement things with a user-defined line styling function to facilitate this.

If I have a little time later on then I'll hack up some code for you. Shouldn't be too difficult.
I may look like a mule, but I'm not a complete ass.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GoScintilla - 2.0 (beta 3)

Post by ts-soft »

srod wrote:If I have a little time later on then I'll hack up some code for you. Shouldn't be too difficult.
:D
Many thanks for your big help!
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: GoScintilla - 2.0 (beta 3)

Post by srod »

Here you are Thomas.

Code: Select all

;/////////////////////////////////////////////////////////////////////////////////
;***Go-Scintilla 2***
;*===================
;*
;*©nxSoftWare (www.nxSoftware.com) 2010.
;*======================================
;*    
;*  REM demo.
;*  We show how to use a simple user-defined line styling function to facilitate a REM statement and style accordingly.
;*  This kind of thing would normally by achieved through a #GOSCI_DELIMITTOENDOFLINE delimiter, but such delimiters are restricted
;*  to single characters only.
;/////////////////////////////////////////////////////////////////////////////////


IncludePath "..\..\..\"
XIncludeFile "GoScintilla.pbi"


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, 640, 600, "GoScintilla demo!", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
  RemoveKeyboardShortcut(0, #PB_Shortcut_Tab) ;Required for the tab key to function correctly when the Scintilla control has the focus.

  ;Create our Scintilla control. Note that we do not specify a callback; this is optional for GoSctintilla.
    GOSCI_Create(1, 10, 10, 620, 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, 24)

  ;Set the back color of the line containing the caret.
    GOSCI_SetColor(1, #GOSCI_CARETLINEBACKCOLOR, $B4FFFF)

  ;Set font.
    GOSCI_SetFont(1, "Courier New", 10)

  ;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, 2)

  ;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_COMMENTS
        #STYLES_LITERALSTRINGS
        #STYLES_NUMBERS
      EndEnumeration

    ;Set individual styles for commands.
      GOSCI_SetStyleFont(1, #STYLES_COMMANDS, "", -1, #PB_Font_Bold)
      GOSCI_SetStyleColors(1, #STYLES_COMMANDS, $800000)  ;We have omitted the optional back color.

    ;Set individual styles for comments.
      GOSCI_SetStyleFont(1, #STYLES_COMMENTS, "", -1, #PB_Font_Italic)
      GOSCI_SetStyleColors(1, #STYLES_COMMENTS, $006400)  ;We have omitted the optional back color.
      
    ;Set individual styles for literal strings.
      GOSCI_SetStyleColors(1, #STYLES_LITERALSTRINGS, #Gray)  ;We have omitted the optional back color.

    ;Set individual styles for numbers.
      GOSCI_SetStyleColors(1, #STYLES_NUMBERS, #Red)  ;We have omitted the optional back color.

  ;Set delimiters and keywords for our syntax highlighting.
  ;========================================================
    ;First some commands.
      GOSCI_AddKeywords(1, "Let", #STYLES_COMMANDS)
    ;Now some comments.
    ;This will need supplementing with our user-defined line styling function.
      GOSCI_AddKeywords(1, "Rem", #STYLES_COMMENTS)
    ;Now set up quotes to denote literal strings.
      GOSCI_AddDelimiter(1, Chr(34), Chr(34), #GOSCI_DELIMITBETWEEN, #STYLES_LITERALSTRINGS)

  ;Additional lexer options.
  ;=========================
    ;The lexer needs to know what separator characters we are using.
      GOSCI_SetLexerOption(1, #GOSCI_LEXEROPTION_SEPARATORSYMBOLS, @"=+-*/%()[],.") ;You would use GOSCI_AddKeywords() to set a style for some of these if required.
    ;We can also set a style for numbers.
      GOSCI_SetLexerOption(1, #GOSCI_LEXEROPTION_NUMBERSSTYLEINDEX, #STYLES_NUMBERS)

  ;Set our user-defined line styling function.
  ;===========================================
    GOSCI_SetLineStylingFunction(1, @MyLineStyler())

  ;Set some initial text.
  ;======================
    text$ = "REM - A simple demo utilising a user-defined line styling function." + #CRLF$
    text$ + "REM - Shows how to trap REM statements!" + #CRLF$ + #CRLF$
    text$ + "Let x$ = " + Chr(34) + "Heyho!" + Chr(34) + #CRLF$
    
    GOSCI_SetText(1, text$)

  Repeat
    eventID = WaitWindowEvent()
    Select eventID
      Case #PB_Event_SizeWindow
        ResizeGadget(1, #PB_Ignore, #PB_Ignore, WindowWidth(0)-20, WindowHeight(0)-20)
      
      Case #PB_Event_Gadget
        Select EventGadget()
        EndSelect
    EndSelect
  Until eventID = #PB_Event_CloseWindow 

  ;Free the Scintilla gadget.
  ;This needs explicitly calling in order to free resources used by GoScintilla.
    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
  ;Need to loop through the UTF-8 buffer invoking GoScintilla's styling lexer as appropriate.
    While numUtf8Bytes
      numBytesStyled = GOSCI_StyleNextSymbol(id, *utf8Buffer, numUtf8Bytes)
      numUtf8Bytes - numBytesStyled
      ;Examine the symbol just styled.
        If numBytesStyled
          symbolJustStyled$ = LCase(PeekS(*utf8Buffer, numBytesStyled, #PB_UTF8))
          If symbolJustStyled$ = "rem"
            ;We now apply the comments style to the rest of the line, excluding any #LF or #CR characters as this will 
            ;cause Scintilla to force us to restyle the entire document.
              *utf8Buffer + numBytesStyled
              If numUtf8Bytes
                numBytesStyled = numUtf8Bytes
                *ptrAscii = *utf8Buffer + numUtf8Bytes - 1
                While *ptrAscii\a = #LF Or *ptrAscii\a = #CR
                  *ptrAscii - 1
                  numBytesStyled - 1
                Wend
                If numBytesStyled
                  ScintillaSendMessage(id, #SCI_SETSTYLING, numBytesStyled, #STYLES_COMMENTS)
                  numUtf8Bytes - numBytesStyled
                  *utf8Buffer + numBytesStyled
                EndIf
              EndIf
          Else
            *utf8Buffer + numBytesStyled
          EndIf
        EndIf      
    Wend
  ProcedureReturn result
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////
I may look like a mule, but I'm not a complete ass.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GoScintilla - 2.0 (beta 3)

Post by ts-soft »

Stephen, i am very happy, thank you.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: GoScintilla - 2.0 (beta 3)

Post by srod »

ts-soft wrote:Stephen, i am very happy, thank you.
No problem.

Note how I added "rem" to a keyword list to enable GoScintilla 2's lexer to identify the keyword etc. I could remove this because REM would normally be delimited by a space character anyhow, but then I would need to alter the user-defined line styling function and introduce some repetition.

Swings and roundabouts! :)
I may look like a mule, but I'm not a complete ass.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GoScintilla - 2.0 (beta 3)

Post by ts-soft »

Code: Select all

  GOSCI_SetColor(id, #GOSCI_BACKCOLOR, 16771801)
  GOSCI_SetColor(id, #GOSCI_CARETLINEBACKCOLOR, 16773606)
  GOSCI_SetColor(id, #GOSCI_LINENUMBERBACKCOLOR, 8404992)
  GOSCI_SetColor(id, #GOSCI_LINENUMBERFORECOLOR, 8454143)
  GOSCI_SetColor(id, #GOSCI_FOLDMARGINHIBACKCOLOR, 8404992)
  GOSCI_SetColor(id, #GOSCI_FOLDMARGINLOBACKCOLOR, 8404992)
  GOSCI_SetColor(id, #GOSCI_FOLDMARKERSBACKCOLOR, 8454143)
  GOSCI_SetColor(id, #GOSCI_FOLDMARKERSFORECOLOR,  8404992)
#GOSCI_LINENUMBERBACKCOLOR and #GOSCI_LINENUMBERFORECOLOR do nothing?

Thomas
LCD
Enthusiast
Enthusiast
Posts: 206
Joined: Sun Jun 01, 2003 10:55 pm
Location: Austria, Vienna
Contact:

Re: GoScintilla - 2.0 (beta 3)

Post by LCD »

ts-soft wrote:

Code: Select all

#GOSCI_LINENUMBERBACKCOLOR and #GOSCI_LINENUMBERFORECOLOR do nothing?
Thomas[/quote]
Works here as expected.
My PC
Ryzen 9 5950, 64 GB RAM, nVidia RTX A4000, Win 10
Ryzen 7 1700, 32 GB RAM, nVidia RTX A2000, Win 10
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: GoScintilla - 2.0 (beta 3)

Post by srod »

Works fine here as well.

Thomas, the #STYLE_LINENUMBER style-index is essentially reserved for use by Scintilla itself (for line numbers). Make sure that one of your own style indexes does not clash with this one (#STYLE_LINENUMBER = 33).

**EDIT : Also, things like GOSCI_SetFont() resets all style indexes to match the default one so you may simply need to reposition your GOSCI_SetColor() lines is all.
I may look like a mule, but I'm not a complete ass.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GoScintilla - 2.0 (beta 3)

Post by ts-soft »

Thx for the hint with font. i have to set the font before the colors.

Thx again.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GoScintilla - 2.0 (beta 3)

Post by ts-soft »

Every Day a new problem :wink:
For Folding i will add the commentsign '{ for open and '} for closing?

Is there a way to do this?

greetings
Thomas
LCD
Enthusiast
Enthusiast
Posts: 206
Joined: Sun Jun 01, 2003 10:55 pm
Location: Austria, Vienna
Contact:

Re: GoScintilla - 2.0 (beta 3)

Post by LCD »

ts-soft wrote:Every Day a new problem :wink:
For Folding i will add the commentsign '{ for open and '} for closing?

Is there a way to do this?

greetings
Thomas
I think, it is possible only if the comment command itself is not a comment but a command:
REM{ or ;{: REM and ; are NOT styled as comment
REM} or ;{: REM and ; are NOT styled as comment
Not tested this yet.
My PC
Ryzen 9 5950, 64 GB RAM, nVidia RTX A4000, Win 10
Ryzen 7 1700, 32 GB RAM, nVidia RTX A2000, Win 10
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: GoScintilla - 2.0 (beta 3)

Post by srod »

You will need to be a bit more specific Thomas.

There is no problem in having delimiters function also as keywords (open fold or close fold). But if you are looking to style multi-lined comments etc. then you need a user-defined line styling function.
I may look like a mule, but I'm not a complete ass.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GoScintilla - 2.0 (beta 3)

Post by ts-soft »

In PB you can use ";{" and ";}" for folding. I will use "'{" "'}" for folding, but this doesn't work.
' is the comment sign like in pb the ;
LCD
Enthusiast
Enthusiast
Posts: 206
Joined: Sun Jun 01, 2003 10:55 pm
Location: Austria, Vienna
Contact:

Re: GoScintilla - 2.0 (beta 3)

Post by LCD »

Other remark from me: I can change colors of line numbers, but not the font style using GOSCI_SetFont() as #GOSCI_LINENUMBERFONT does not exist. Line number margin has style number 33 by the way, so I can use GOSCI_SetStyleFont().

@ts-soft:
if you delete the delimiter:

Code: Select all

 GOSCI_AddDelimiter(LexerNr, "'", "", #GOSCI_DELIMITTOENDOFLINE, #STYLES_COMMENTS)
And change some lines in myLineStyler:

Code: Select all

If numBytesStyled
      symbolJustStyled$ = LCase(PeekS(*utf8Buffer, numBytesStyled, #PB_UTF8))
      Select symbolJustStyled$
        Case "function","sub","asm"
          ;Was the previous symbol "end" ? If so, we decrease the current line's fold level as appropriate for a close-fold keyword.
          ;Otherwise, we increment the fold level as appropriate for an open-fold keyword.
          If lastSymbolStyled$ = "end"
            GOSCI_DecFoldLevel(id)
          Else
            GOSCI_IncFoldLevel(id)
          EndIf
        Case "rem{","'{"
          GOSCI_IncFoldLevel(id)
        Case "rem}","'}"
          GOSCI_DecFoldLevel(id)
          
      EndSelect
      ;If the current symbol is not a stream of whitespace characters (space or #TAB) then record it in the lastSymbolStyled$.
      If *utf8Buffer\a <> 32 And *utf8Buffer\a <> 9
        lastSymbolStyled$ = symbolJustStyled$
      EndIf
    EndIf
it will work, but it won't style the line as comment anymore. Folding anything styled as comments is ignored.
My PC
Ryzen 9 5950, 64 GB RAM, nVidia RTX A4000, Win 10
Ryzen 7 1700, 32 GB RAM, nVidia RTX A2000, Win 10
Post Reply