Page 9 of 20

Re: GoScintilla - 2.0 (beta 3)

Posted: Mon May 17, 2010 10:56 am
by srod
LCD wrote: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().
Yes, you are supposed to use GOSCI_SetStyleFont() for that. GOSCI_SetFont() sets the font for all styles in one go.

@Thomas : right, I understand what you are after. I didn't see the leading apostrophe in your '{ and '} symbols! Should be easy enough. Do you wish lines containing '{ and '} to be styled as comments?

Re: GoScintilla - 2.0 (beta 3)

Posted: Mon May 17, 2010 11:36 am
by srod
Here you are Thomas.

A simple matter of using a ' symbol as a #GOSCI_DELIMITTOENDOFLINE delimiter in order to style comments etc. and then using a simple user-defined line styling function to look out for the folding keywords '{ and '} etc.

We cannot of course achieve this without using a user-defined line styling function because the fact that ' is a delimiter, prevents GoScintilla 2 from subsequently recognising any keyword beginning with this symbol because it acts like a separator.

Code: Select all

;/////////////////////////////////////////////////////////////////////////////////
;***Go-Scintilla 2***
;*===================
;*
;*©nxSoftWare (www.nxSoftware.com) 2010.
;*======================================
;*    
;*  Folding demo.
;*  We show how to use a user-defined line styling function to enable a ' symbol to be recognised as a comment (similar to a #GOSCI_DELIMITTOENDOFLINE delimiter)
;*  and to also utilise '{ and '} as open/close folding keywords as well. 
;*  GoScintilla 2 cannot deal with this without our assistance because marking ' as a delimiter subsequently prevents '{ being
;*  recognised as a keyword because the ' symbol will act as a separator.
;/////////////////////////////////////////////////////////////////////////////////


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


Declare.i myLineStyler(id, *utf8Buffer.UNICODE, numUtf8Bytes, currentLine, startLine, originalEndLine)


;Initialise the Scintilla library for Windows.
  CompilerIf  #PB_Compiler_OS = #PB_OS_Windows 
    InitScintilla()
  CompilerEndIf

If OpenWindow(0, 100, 200, 600, 600, "GoScintilla demo!", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  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, 580, 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
        #STYLES_CONSTANTS
        #STYLES_FUNCTIONS
      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 individual styles for constants.
      GOSCI_SetStyleColors(1, #STYLES_CONSTANTS, $2193DE)  ;We have omitted the optional back color.

    ;Set individual styles for functions.
      GOSCI_SetStyleColors(1, #STYLES_FUNCTIONS, #Blue)  ;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. Note the use of #GOSCI_DELIMITTOENDOFLINE.
    ;Note also that this symbol will act as an additional separator.
      GOSCI_AddDelimiter(1, "'", "", #GOSCI_DELIMITTOENDOFLINE, #STYLES_COMMENTS)
    ;Now set up quotes to denote literal strings.
    ;We do this by passing the beginning and end delimiting characters; in this case both are quotation marks. Note the use of #GOSCI_DELIMITBETWEEN.
    ;Note also that a quote will subsequently act as an additional separator.
      GOSCI_AddDelimiter(1, Chr(34), Chr(34), #GOSCI_DELIMITBETWEEN, #STYLES_LITERALSTRINGS)
    ;Now set up a # symbol to denote a constant. Note the use of #GOSCI_LEFTDELIMITWITHOUTWHITESPACE.
      GOSCI_AddDelimiter(1, "#", "", #GOSCI_LEFTDELIMITWITHOUTWHITESPACE, #STYLES_CONSTANTS)
    ;Now set up a ( symbol to denote a function. Note the use of #GOSCI_RIGHTDELIMITWITHWHITESPACE.
      GOSCI_AddDelimiter(1, "(", "", #GOSCI_RIGHTDELIMITWITHWHITESPACE, #STYLES_FUNCTIONS)
    ;We arrange for a ) symbol to match the coloring of the ( symbol.
      GOSCI_AddDelimiter(1, ")", "", 0, #STYLES_FUNCTIONS)

    ;Basic command keywords.
      GOSCI_AddKeywords(1, "Debug End If ElseIf Else EndIf EndProcedure For To Next Repeat Step Procedure Protected ProcedureReturn Until", #STYLES_COMMANDS)

  ;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$ = "' GoScintilla 2.0." + #CRLF$
    text$ + "' By Stephen Rodriguez." + #CRLF$ + #CRLF$
    text$ + "If OpenWindow(0, 100, 200, 195, 260, " + Chr(34) + "PureBasic Window" + Chr(34) + ", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)" + #CRLF$
    text$ + "'{" + #CRLF$
    text$ + #TAB$ + "Repeat" + #CRLF$ 
    text$ + #TAB$ + #TAB$ + "eventID = WaitWindowEvent()" + #CRLF$
    text$ + #TAB$ + "Until eventID = #PB_Event_CloseWindow" + #CRLF$
    text$ + "'}" + #CRLF$ + #CRLF$
    text$ + "EndIf" + #CRLF$
    text$ + "End" + #CRLF$
    
    GOSCI_SetText(1, text$)

  Repeat
    eventID = WaitWindowEvent()
    Select eventID
      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.UNICODE, numUtf8Bytes, currentLine, startLine, originalEndLine)
  Protected result = #GOSCI_STYLELINESASREQUIRED, numBytesStyled, symbolJustStyled$, left$
  ;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 >= 2
          If *utf8Buffer\u = $7B27        ;'{
            GOSCI_IncFoldLevel(id)
          ElseIf *utf8Buffer\u = $7D27    ;'}
            GOSCI_DecFoldLevel(id)
          EndIf
        EndIf
      *utf8Buffer + numBytesStyled
    Wend
  ProcedureReturn result
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////
(srod sits back and awaits the next 'problem'! :wink: )

Re: GoScintilla - 2.0 (beta 3)

Posted: Mon May 17, 2010 12:24 pm
by ts-soft
srod wrote:(srod sits back and awaits the next 'problem'! :wink: )
I'm on work, so i can test it later. If it not work, i tell you :wink:

Thomas

Re: GoScintilla - 2.0 (beta 3)

Posted: Mon May 17, 2010 12:28 pm
by srod
ts-soft wrote:
srod wrote:(srod sits back and awaits the next 'problem'! :wink: )
I'm on work, so i can test it later. If it not work, i tell you :wink:

Thomas
You can blame Stephen Hawking if it doesn't work. His damn book is keeping me up so bloody late that I can barely keep my eyes open right now! Still, he is one clever chap. If I ever fall into a black-hole then you can rest assure that I will be taking GoScintilla with me!

Re: GoScintilla - 2.0 (beta 3)

Posted: Mon May 17, 2010 3:45 pm
by ts-soft
It doesn't work in combination with rem, i'm to stupid :oops:
The other foldings a broken after a "commentfolding"

Code: Select all

Procedure.i myLineStyler(id, *utf8Buffer.ASCII, numUtf8Bytes, currentLine, startLine, originalEndLine)
  Protected result = #GOSCI_STYLELINESASREQUIRED, numBytesStyled, symbolJustStyled$, *ptrAscii.ASCII
  Protected left$
  ;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
          ElseIf numBytesStyled >=2
            symbolJustStyled$ = LCase(PeekS(*utf8Buffer, numBytesStyled, #PB_UTF8))
            left$ = Left(symbolJustStyled$, 2)
            If left$ = "'{"
              GOSCI_IncFoldLevel(id)
            ElseIf left$ = "'}"
              GOSCI_DecFoldLevel(id)
            EndIf           
          Else
            *utf8Buffer + numBytesStyled
          EndIf
        EndIf     
    Wend
  ProcedureReturn result
EndProcedure

Re: GoScintilla - 2.0 (beta 3)

Posted: Mon May 17, 2010 5:54 pm
by srod
I have simplified the demo above by removing the PeekS().

As for your attempted almagamation of REM and the '{...'} folding... you nearly had it. Just needed an additional *utf8Buffer + numBytesStyled. Without this you were, in certain circumstances, styling the same bytes twice at the expense of other bytes!

Don't forget to add REM as a keyword etc. I have also altered your code in the same way as I altered my demo above.

Code: Select all

Procedure.i myLineStyler(id, *utf8Buffer.ASCII, numUtf8Bytes, currentLine, startLine, originalEndLine)
  Protected result = #GOSCI_STYLELINESASREQUIRED, numBytesStyled, symbolJustStyled$, *ptrAscii.ASCII, *ptrUnicode.UNICODE
  Protected left$
  ;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
          ElseIf numBytesStyled >=2
            *ptrUnicode = *utf8Buffer
            If *ptrUnicode\u = $7B27        ;'{
              GOSCI_IncFoldLevel(id)
            ElseIf *ptrUnicode\u = $7D27    ;'}
              GOSCI_DecFoldLevel(id)
            EndIf
            *utf8Buffer + numBytesStyled
          Else
            *utf8Buffer + numBytesStyled
          EndIf
        EndIf     
    Wend
  ProcedureReturn result
EndProcedure

Re: GoScintilla - 2.0 (beta 3)

Posted: Mon May 17, 2010 6:05 pm
by ts-soft
:D
That is it, great work, thanks again.

Thomas

Re: GoScintilla - 2.0 (beta 3)

Posted: Mon May 17, 2010 6:11 pm
by srod
ts-soft wrote::D
That is it, great work, thanks again.

Thomas
No problem. GoScintilla 2 is certainly getting a good work-out through whatever it is you are doing! :)

Re: GoScintilla - 2.0 (beta 3)

Posted: Mon May 17, 2010 6:36 pm
by ts-soft
Now Syntax-Highligtning for the language is ready, thanks to your great help.
Now many problems should not appear any more much too.

Thomas

Re: GoScintilla - 2.0 (beta 3)

Posted: Mon May 17, 2010 6:43 pm
by srod
Which language syntax you working with?

Re: GoScintilla - 2.0 (beta 3)

Posted: Mon May 17, 2010 6:53 pm
by ts-soft
I don't think you have ever seen :mrgreen:
http://xprofan.de/start.htm

It's a Basic-Like Interpreter and P-Code Compiler with OOP-Support, it's not so fast as PB.

With this language comes a editor with a scintilla class written by me, very old. Some people have
made better versions, but I think, I can outbid this :wink:

Re: GoScintilla - 2.0 (beta 3)

Posted: Tue May 18, 2010 12:32 am
by LCD
In GoScintilla you are checking only if the FOLDOPEN-Symbol is clicked. I think, it is better to not check this, so if any of the symbols are clicked (FOLDTAIL and FOLDMID), a fold will be done, just like in JaPBE.
Just change the lines

Code: Select all

Case #SCN_MARGINCLICK
      Select *scinotify\margin
        Case #GOSCI_MARGINFOLDINGSYMBOLS
          StartLine = ScintillaSendMessage(id, #SCI_LINEFROMPOSITION, *scinotify\Position)
          ;Check it is a header line.
            If ScintillaSendMessage(id, #SCI_GETFOLDLEVEL, StartLine) & #SC_FOLDLEVELHEADERFLAG
              ScintillaSendMessage(id, #SCI_TOGGLEFOLD, StartLine)
            EndIf
      EndSelect
to:

Code: Select all

Case #SCN_MARGINCLICK
      Select *scinotify\margin
        Case #GOSCI_MARGINFOLDINGSYMBOLS
          StartLine = ScintillaSendMessage(id, #SCI_LINEFROMPOSITION, *scinotify\Position)
            ScintillaSendMessage(id, #SCI_TOGGLEFOLD, StartLine)
      EndSelect
I think, this improves the usability a lot.

Re: GoScintilla - 2.0 (beta 3)

Posted: Tue May 18, 2010 10:25 am
by srod
I did that originally, but decided I didn't like it. Doesn't seem natural to me. :)

I can't say as this is something I wish to change.

Re: GoScintilla - 2.0 (beta 3)

Posted: Tue May 18, 2010 10:58 am
by LCD
srod wrote:I did that originally, but decided I didn't like it. Doesn't seem natural to me. :)

I can't say as this is something I wish to change.
Maybe as option? What do the other GoScintillaner think about it?

Re: GoScintilla - 2.0 (beta 3)

Posted: Tue May 18, 2010 12:07 pm
by srod
LCD wrote:
srod wrote:I did that originally, but decided I didn't like it. Doesn't seem natural to me. :)

I can't say as this is something I wish to change.
Maybe as option? What do the other GoScintillaner think about it?
Done.


GoScintilla 2.0 (beta 4) - 18th May 2010

GoScintilla 2 beta 4 adds a few more demo programs showing how to make use of user-defined line styling functions (demos are taken from this forum thread) and adds a new lexer state bit : #GOSCI_LEXERSTATE_ENABLECLICKANYWHERECODEFOLDING.

This new lexer state (following a suggestion from LCD) allows for code folding regions which can be collapsed by clicking any part of the folding margin etc. The default bit : #GOSCI_LEXERSTATE_ENABLECODEFOLDING requires the user to specifically click the appropriate folding marker.

Please see the nxSoftware site for the download.