get text line by line from Scintilla

Just starting out? Need help? Post your questions and find answers here.
Dano
User
User
Posts: 41
Joined: Fri Jul 16, 2004 4:20 am
Location: Edmonton, Ab, Canada

get text line by line from Scintilla

Post by Dano »

hi all,
As the title suggests, I can get the complete text from Scintilla into one string, but in my program I want to bring it line by line into a linked list of lines. I had some code which I inadvertently
left at work, which uses Sci_numLines or whatever, but doesn't work. anyways I used to like Scintilla but lately it seems every little thing is complicated, it wants to put the lines into memory, and I am but a simple
programmer who would rather use GetGadgetItemText and do it that way, but it is like a beautiful lady, never making things simple for you, looks good at the beginning, but living with her ends
up being too much trouble. But I digress. Does anyone have a simple way of getting text in from my Scintilla Gadget line by line?

Many thanks in advance,
Dan
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: get text line by line from Scintilla

Post by Tenaja »

The scintilla command is... SCI_GETLINE(int line, char *text)

So, the PB command will be SendScintillaMessage(ID, #Sci_GETLINE, Line, utf8buffer)
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: get text line by line from Scintilla

Post by Tenaja »

...and here is the complete GoSci (by SRod) procedure:

Code: Select all

;/////////////////////////////////////////////////////////////////////////////////
;The following function retrieves the text (minus any EOL characters) for a given line.
Procedure.s GOSCI_GetLineText(id, lineIndex)
  Protected text$, numLines, lineLength, utf8Buffer, *ptrAscii.ASCII
  If IsGadget(id) And GadgetType(id) = #PB_GadgetType_Scintilla
    numLines = ScintillaSendMessage(id, #SCI_GETLINECOUNT)
    If lineIndex >=0 And lineIndex < numLines
      lineLength = ScintillaSendMessage(id, #SCI_LINELENGTH, lineIndex)
      If lineLength
        utf8Buffer = AllocateMemory(lineLength+1,#PB_Memory_NoClear)
        If utf8Buffer
          ScintillaSendMessage(id, #SCI_GETLINE, lineIndex, utf8Buffer)
          ;Remove any terminating EOL characters.
            *ptrAscii = utf8Buffer + lineLength - 1
            While (*ptrAscii\a = 10 Or *ptrAscii\a = 13) And lineLength
              lineLength - 1
              *ptrAscii - 1
            Wend
          text$ = PeekS(utf8Buffer, lineLength, #PB_UTF8)
          FreeMemory(utf8Buffer)
        EndIf
      EndIf
    EndIf
  EndIf
  ProcedureReturn text$
EndProcedure
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: get text line by line from Scintilla

Post by Danilo »

Too late, but using #SCI_GETCHARACTERPOINTER could be an alternative:

Code: Select all

Procedure.s ScintillaGetLine(scintillaGadget,line)
    ; get line text from character pointer
    Protected text.s, start, length, *buffer
    start = ScintillaSendMessage(scintillaGadget,#SCI_POSITIONFROMLINE,line)
    length = ScintillaSendMessage(scintillaGadget,#SCI_GETLINEENDPOSITION,line) - start
    If length>0
        *buffer = ScintillaSendMessage(scintillaGadget,#SCI_GETCHARACTERPOINTER)
        If *buffer
            ProcedureReturn PeekS(*buffer+start,length,#PB_UTF8)
        EndIf
    EndIf
EndProcedure

Procedure.s ScintillaGetLine2(scintillaGadget,line)
    ; get line text
    Protected text.s, size, *buffer
    size = ScintillaSendMessage(scintillaGadget,#SCI_GETLINE,line,0)
    If size>0
        *buffer = AllocateMemory(size)
        If *buffer
            size = ScintillaSendMessage(scintillaGadget,#SCI_GETLINE,line,*buffer)
            If size
                size = ScintillaSendMessage(scintillaGadget,#SCI_GETLINEENDPOSITION,line) - ScintillaSendMessage(scintillaGadget,#SCI_POSITIONFROMLINE,line)
                text = PeekS(*buffer,size,#PB_UTF8)
            EndIf
            FreeMemory(*buffer)
        EndIf
    EndIf
    ProcedureReturn text
EndProcedure

Macro ScintillaGetLineLength2(scintillaGadget,line)
    ; get line length including line end characters
    ScintillaSendMessage(scintillaGadget,#SCI_LINELENGTH,line)
EndMacro

Macro ScintillaGetLineLength(scintillaGadget,line)
    ; get line length excluding line end characters
    ( ScintillaSendMessage(scintillaGadget,#SCI_GETLINEENDPOSITION,line) - ScintillaSendMessage(scintillaGadget,#SCI_POSITIONFROMLINE,line) )
EndMacro

Macro ScintillaGetLineCount(scintillaGadget)
    ScintillaSendMessage(scintillaGadget,#SCI_GETLINECOUNT)
EndMacro
Dano
User
User
Posts: 41
Joined: Fri Jul 16, 2004 4:20 am
Location: Edmonton, Ab, Canada

Re: get text line by line from Scintilla

Post by Dano »

thanks Danilo and Tenaja for the code, and I will end up using it only because I started with the Scintilla gadget, but when I started using Scintilla I was hoping for something in the lines of the editor gadget that
I could understand and was simple to program. I rely on Fred to shelter me from the complexities of API and other such complicated structures. When simply getting a line of text becomes that complicated as is
in your examples I want GetGadgetItemText so it becomes something I can do. I have no programming training but I can do a lot if I can understand it, and with Purebasic I can now do incredible things, but to get
a line of text I don't want 20 lines of code when one statement in Purebasic does the same thing. I mean the most basic things you want from an editor gadget is to put text in and get it out, and I can do that
with the Editor Gadget, but the Scintilla gadget added some nice features, but I need it dumbified so that the simplest things are simple to me. I'm hoping Fred that you incorporate GetGadgetText and
GetGadgetItemText and the rest for the Scintilla gadget and then my editor dreams will be fulfilled.

Dan
Dano
User
User
Posts: 41
Joined: Fri Jul 16, 2004 4:20 am
Location: Edmonton, Ab, Canada

Re: get text line by line from Scintilla

Post by Dano »

Actually scratch what I said. I was looking at the second example, but the first one is much easier to follow, and I can understand it for the most part.
Thanks both of you for the code.

Dan
Post Reply