Page 1 of 2

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Fri May 18, 2012 12:24 am
by Danilo
New version with selected text:
- If there is selected text (single line only), the selection is highlighted
- Nothing selected, the word under cursor gets highlighted
- Nothing selected and no word under cursor -> highlighting is cleared

Image

Image

Code: Select all

CompilerIf #PB_Compiler_Unicode = 0
    CompilerError "Compile with Unicode mode enabled"
CompilerEndIf


#INDIC_STRAIGHTBOX = 8
#INDIC_DASH        = 9
#INDIC_DOTS        = 10
#INDIC_SQUIGGLELOW = 11
#INDIC_DOTBOX      = 12

#SCI_INDICSETALPHA          = 2523
#SCI_INDICSETOUTLINEALPHA   = 2558


;-----------------------------------
; SETUP

; #INDIC_PLAIN       ; Underlined With a single, straight line.
; #INDIC_SQUIGGLE    ; A squiggly underline. Requires 3 pixels of descender space.
; #INDIC_TT          ; A line of small T shapes.
; #INDIC_DIAGONAL    ; Diagonal hatching.
; #INDIC_STRIKE      ; Strike out.
; #INDIC_HIDDEN      ; An indicator With no visual effect.
; #INDIC_BOX         ; A rectangle around the text.
;
; - the following styles don't work with Scintilla used by PB 4.60
; - maybe useful for future versions
;
; #INDIC_ROUNDBOX    ; A rectangle with rounded corners around the text using translucent drawing
;                    ; with the interior usually more transparent than the border.
;                    ; You can use SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA to control the alpha transparency values.
;                    ; The default alpha values are 30 for fill colour and 50 for outline colour.
; #INDIC_STRAIGHTBOX ; A rectangle around the text using translucent drawing With the interior
;                    ; usually more transparent than the border.
;                    ; You can use SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA to control the alpha transparency values.
;                    ; The default alpha values are 30 for fill colour and 50 for outline colour.
; #INDIC_DASH        ; A dashed underline.
; #INDIC_DOTS        ; A dotted underline.
; #INDIC_SQUIGGLELOW ; Similar To INDIC_SQUIGGLE but only using 2 vertical pixels so will fit under small fonts.
; #INDIC_DOTBOX      ; A dotted rectangle around the text using translucent drawing.
;                    ; Translucency alternates between the alpha and outline alpha settings with the top-left pixel
;                    ; using the alpha setting.
;                    ; SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA control the alpha transparency values.
;                    ; The default values are 30 For alpha And 50 For outline alpha.
;                    ; To avoid excessive memory allocation the maximum width of a dotted box is 4000 pixels.
;                    ; Not available For OS X Carbon.

style = #INDIC_BOX

color = RGB($00,$FF,$FF)

InnerAlpha  =  50
BorderAlpha = 200

#IGNORE_CASE = #True

;-----------------------------------

NewList positions.i()
NewList source.s()

Scintilla = Val( GetEnvironmentVariable("PB_TOOL_Scintilla") )


If Scintilla
    word$    = GetEnvironmentVariable("PB_TOOL_Word")

    SendMessageTimeout_(Scintilla,#SCI_GETSELECTIONSTART,0,0,#SMTO_ABORTIFHUNG,2000,@selectionStart)
    SendMessageTimeout_(Scintilla,#SCI_GETSELECTIONEND  ,0,0,#SMTO_ABORTIFHUNG,2000,@selectionEnd)

    If selectionStart <> selectionEnd
        ;
        ; get informations about the current selection
        ;
        SendMessageTimeout_(Scintilla,#SCI_LINEFROMPOSITION,selectionStart,0,#SMTO_ABORTIFHUNG,2000,@selectionLine)
        SendMessageTimeout_(Scintilla,#SCI_POSITIONFROMLINE,selectionLine ,0,#SMTO_ABORTIFHUNG,2000,@selectionLineStart)
        selectionEnd   - selectionStart
        selectionStart - selectionLineStart
        word$ = "..."
    Else
        ;
        ; no selection
        ;
        selectionLine = -1
    EndIf

    SendMessageTimeout_(Scintilla,#SCI_SETINDICATORCURRENT  ,9, 0          ,#SMTO_ABORTIFHUNG,2000,@result)
    SendMessageTimeout_(Scintilla,#SCI_INDICSETSTYLE        ,9, style      ,#SMTO_ABORTIFHUNG,2000,@result)
    SendMessageTimeout_(Scintilla,#SCI_INDICSETFORE         ,9, color      ,#SMTO_ABORTIFHUNG,2000,@result)
    SendMessageTimeout_(Scintilla,#SCI_INDICSETALPHA        ,9, InnerAlpha ,#SMTO_ABORTIFHUNG,2000,@result)
    SendMessageTimeout_(Scintilla,#SCI_INDICSETOUTLINEALPHA ,9, BorderAlpha,#SMTO_ABORTIFHUNG,2000,@result)
    SendMessageTimeout_(Scintilla,#SCI_INDICSETUNDER        ,9, #False     ,#SMTO_ABORTIFHUNG,2000,@result)
    
    If word$
    
        line = 0
        If ReadFile(0,ProgramParameter(0))
            format = ReadStringFormat(0)
            Repeat
                If AddElement( source() )
                    source() = ReadString(0,format)
                    If line = selectionLine
                        word$ = Mid( source(), selectionStart+1, selectionEnd )
                    EndIf
                EndIf
                line + 1
            Until Eof(0)
            CloseFile(0)
        EndIf

        wordlen = Len(word$)

        CompilerIf #IGNORE_CASE
            word$ = LCase(word$)
        CompilerEndIf

        line = 0
        ForEach source()
            text$ = source()

            CompilerIf #IGNORE_CASE
                text$ = LCase(text$)
            CompilerEndIf

            pos = 1
            Repeat
                pos = FindString(text$,word$,pos)
                If pos
                    If AddElement(positions())
                        SendMessageTimeout_(Scintilla,#SCI_POSITIONFROMLINE,line,0,#SMTO_ABORTIFHUNG,2000,@offset)
                        positions() = offset + pos - 1
                    EndIf
                    pos + wordlen
                EndIf
            Until pos = 0
            line + 1
        Next

        SendMessageTimeout_(Scintilla,#SCI_GETTEXTLENGTH ,0,0,#SMTO_ABORTIFHUNG,2000,@length)
        PostMessage_(Scintilla,#SCI_INDICATORCLEARRANGE,0,length)


        ForEach positions()
            PostMessage_(Scintilla,#SCI_INDICATORFILLRANGE,positions(),wordlen)
        Next
    Else
        SendMessageTimeout_(Scintilla,#SCI_GETTEXTLENGTH ,0,0,#SMTO_ABORTIFHUNG,2000,@length)
        PostMessage_(Scintilla,#SCI_INDICATORCLEARRANGE,0,length)
    EndIf

EndIf

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Fri May 18, 2012 3:52 pm
by Danilo
This version works in memory only, without using %TEMPFILE, but it requires the new Scintilla.dll (renamed SciLexer.dll)
because it uses #SCI_GETCHARACTERPOINTER.

Code: Select all

CompilerIf #PB_Compiler_Unicode = 0
    CompilerError "Compile with Unicode mode enabled"
CompilerEndIf


#INDIC_STRAIGHTBOX = 8
#INDIC_DASH        = 9
#INDIC_DOTS        = 10
#INDIC_SQUIGGLELOW = 11
#INDIC_DOTBOX      = 12

#SCI_INDICSETALPHA          = 2523
#SCI_INDICSETOUTLINEALPHA   = 2558


;-----------------------------------
; SETUP

; #INDIC_PLAIN       ; Underlined With a single, straight line.
; #INDIC_SQUIGGLE    ; A squiggly underline. Requires 3 pixels of descender space.
; #INDIC_TT          ; A line of small T shapes.
; #INDIC_DIAGONAL    ; Diagonal hatching.
; #INDIC_STRIKE      ; Strike out.
; #INDIC_HIDDEN      ; An indicator With no visual effect.
; #INDIC_BOX         ; A rectangle around the text.
;
; - the following styles don't work with Scintilla used by PB 4.60
; - maybe useful for future versions
;
; #INDIC_ROUNDBOX    ; A rectangle with rounded corners around the text using translucent drawing
;                    ; with the interior usually more transparent than the border.
;                    ; You can use SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA to control the alpha transparency values.
;                    ; The default alpha values are 30 for fill colour and 50 for outline colour.
; #INDIC_STRAIGHTBOX ; A rectangle around the text using translucent drawing With the interior
;                    ; usually more transparent than the border.
;                    ; You can use SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA to control the alpha transparency values.
;                    ; The default alpha values are 30 for fill colour and 50 for outline colour.
; #INDIC_DASH        ; A dashed underline.
; #INDIC_DOTS        ; A dotted underline.
; #INDIC_SQUIGGLELOW ; Similar To INDIC_SQUIGGLE but only using 2 vertical pixels so will fit under small fonts.
; #INDIC_DOTBOX      ; A dotted rectangle around the text using translucent drawing.
;                    ; Translucency alternates between the alpha and outline alpha settings with the top-left pixel
;                    ; using the alpha setting.
;                    ; SCI_INDICSETALPHA And SCI_INDICSETOUTLINEALPHA control the alpha transparency values.
;                    ; The default values are 30 For alpha And 50 For outline alpha.
;                    ; To avoid excessive memory allocation the maximum width of a dotted box is 4000 pixels.
;                    ; Not available For OS X Carbon.

style = #INDIC_STRAIGHTBOX

color = RGB($00,$FF,$FF)

InnerAlpha  =  50
BorderAlpha = 200

#IGNORE_CASE = #True

;-----------------------------------

NewList positions.i()
NewList source.s()

Global Scintilla = Val( GetEnvironmentVariable("PB_TOOL_Scintilla") )
Global numBytes, utf8Buffer

If Scintilla

    SendMessageTimeout_(Scintilla,#SCI_GETSELECTIONSTART,0,0,#SMTO_ABORTIFHUNG,2000,@selectionStart)
    SendMessageTimeout_(Scintilla,#SCI_GETSELECTIONEND  ,0,0,#SMTO_ABORTIFHUNG,2000,@selectionEnd)
    
    If selectionStart <> selectionEnd
        word$=""
        For i = selectionStart To selectionEnd-1
            SendMessageTimeout_(Scintilla,#SCI_GETCHARAT,i,0,#SMTO_ABORTIFHUNG,2000,@result)
            word$ + Chr(result)
        Next i
    Else
        word$    = GetEnvironmentVariable("PB_TOOL_Word")
    EndIf

    SendMessageTimeout_(Scintilla,#SCI_SETINDICATORCURRENT  ,9, 0          ,#SMTO_ABORTIFHUNG,2000,@result)
    SendMessageTimeout_(Scintilla,#SCI_INDICSETSTYLE        ,9, style      ,#SMTO_ABORTIFHUNG,2000,@result)
    SendMessageTimeout_(Scintilla,#SCI_INDICSETFORE         ,9, color      ,#SMTO_ABORTIFHUNG,2000,@result)
    SendMessageTimeout_(Scintilla,#SCI_INDICSETALPHA        ,9, InnerAlpha ,#SMTO_ABORTIFHUNG,2000,@result)
    SendMessageTimeout_(Scintilla,#SCI_INDICSETOUTLINEALPHA ,9, BorderAlpha,#SMTO_ABORTIFHUNG,2000,@result)
    SendMessageTimeout_(Scintilla,#SCI_INDICSETUNDER        ,9, #False     ,#SMTO_ABORTIFHUNG,2000,@result)

    If word$
        wordlen = Len(word$)

        CompilerIf #IGNORE_CASE
            word$ = LCase(word$)
        CompilerEndIf

        #SCI_GETCHARACTERPOINTER = 2520
    
        SendMessageTimeout_(Scintilla,#SCI_SETREADONLY,1,0,#SMTO_ABORTIFHUNG,2000,@result)
        SendMessageTimeout_(Scintilla,#SCI_GETCHARACTERPOINTER,0,0,#SMTO_ABORTIFHUNG,2000,@result)
        If result
            SendMessageTimeout_(Scintilla,#SCI_GETTEXTLENGTH,0,0,#SMTO_ABORTIFHUNG,2000,@length)
            memory = AllocateMemory(length+2)

            If Not memory
                SendMessageTimeout_(Scintilla,#SCI_SETREADONLY,0,0,#SMTO_ABORTIFHUNG,2000,@result)
                End
            EndIf

            GetWindowThreadProcessId_(Scintilla, @processId)
            hProcess = OpenProcess_(#PROCESS_ALL_ACCESS, #False, processId)
            ReadProcessMemory_(hProcess,result,memory,length,0)

            *p.Ascii = memory
            length = memory + length

            While *p < length And *p\a
                If *p\a = 10                     ; remove chr(10) + chr(13)
                   *p+1
                   If *p\a = 13 : *p+1 : EndIf
                ElseIf *p\a = 13                 ; remove chr(13) + chr(10)
                   *p+1
                   If *p\a = 10 : *p+1 : EndIf
                   line+1
                Else                             ; read line
                   offset = *p - memory
                   text$ = ""
                   While *p\a And *p\a <> 10 And *p\a <> 13
                       text$ + Chr(*p\a)
                       *p+1
                   Wend
                   If text$
                        CompilerIf #IGNORE_CASE
                            text$ = LCase(text$)
                        CompilerEndIf
                        pos = 1
                        Repeat
                            pos = FindString(text$,word$,pos)
                            If pos
                                If AddElement(positions())
                                    positions() = offset + pos - 1
                                EndIf
                                pos + wordlen
                            EndIf
                        Until pos = 0
                        line + 1
                   EndIf
                EndIf
            Wend
        EndIf

        SendMessageTimeout_(Scintilla,#SCI_SETREADONLY,0,0,#SMTO_ABORTIFHUNG,2000,@result)

        SendMessageTimeout_(Scintilla,#SCI_GETTEXTLENGTH ,0,0,#SMTO_ABORTIFHUNG,2000,@length)
        PostMessage_(Scintilla,#SCI_INDICATORCLEARRANGE,0,length)


        ForEach positions()
            PostMessage_(Scintilla,#SCI_INDICATORFILLRANGE,positions(),wordlen)
        Next
    Else
        SendMessageTimeout_(Scintilla,#SCI_GETTEXTLENGTH ,0,0,#SMTO_ABORTIFHUNG,2000,@length)
        PostMessage_(Scintilla,#SCI_INDICATORCLEARRANGE,0,length)
    EndIf

EndIf

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Sat May 19, 2012 3:37 pm
by Danilo
Scintilla.dll and SciLexer.dll binaries for 32bit and 64bit, compiled with latest Intel C++
and Microsoft VC++ 2010: Scintilla310.zip (3.40MB)

Just replace the PureBasic\Compilers\Scintilla.dll with a Scintilla.dll from the file above,
just choose the right version (32/64bit).

The last in-memory highlighting code works with this Scintilla.dll versions.

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Sun May 20, 2012 12:08 am
by Zebuddi123
@ Tenaja thanks for bringing the question to the forum :D

@ Danilo
Works great Just what the doctor ordered !!!
Thanks for sharing

Zebuddi. :D

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Sun Jul 01, 2012 6:49 am
by c4s
Does this code http://www.purebasic.fr/english/viewtop ... 36#p381036 (from May 17, 2012 6:52 pm) work without modifying Scintilla.dll?

I'm asking because I compiled it in Unicode, installed it as an IDE tool with the %TEMPFILE argument and assigned a shortcut - and it doesn't work. Any ideas?

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Sun Jul 01, 2012 4:25 pm
by Danilo
c4s wrote:Does this code http://www.purebasic.fr/english/viewtop ... 36#p381036 (from May 17, 2012 6:52 pm) work without modifying Scintilla.dll?

I'm asking because I compiled it in Unicode, installed it as an IDE tool with the %TEMPFILE argument and assigned a shortcut - and it doesn't work. Any ideas?
Works here with original PB Scintilla.dll

"%TEMPFILE" argument should have double quotes, and it works only with the word under the cursor, not with selections.

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Sun Jul 01, 2012 5:39 pm
by c4s
Ah, of course the double-quotes... now it works.

There are a few problems though: The selection doesn't go away, sometimes the shortcut doesn't work or not all occurrences will be selected.
Fortunately I was able to fix this by also enabling "Wait until tool finishes" and the selection can be cleared by highlighting no word. ;)

Later I'll try your selection-based version too (where Scintilla.dll has to be updated) as it seems to be an even greater improvement for the IDE!

Thank you Danilo, well done!


Edit:
Wait, I just noticed that http://www.purebasic.fr/english/viewtop ... 91#p381091 (from May 18, 2012 3:52 pm) works without modifying Scintilla.dll (but still needs the "%TEMPFILE" argument)!
The clear advantage over the previous code I've linked to, is that I'm now able to use selections. It even works for multiple words! 8)

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Sun Jul 01, 2012 6:53 pm
by Danilo
c4s wrote:Edit:
Wait, I just noticed that http://www.purebasic.fr/english/viewtop ... 91#p381091 (from May 18, 2012 3:52 pm) works without modifying Scintilla.dll (but still needs the "%TEMPFILE" argument)!
Not possible, as the code does not use %TEMPFILE (ProgramParameter(0)). :)

The code before (Fri May 18, 2012 12:24am) could work with old Scintilla.dll and "%TEMPFILE", but you can only use the first 7 styles.

I use the following style with the latest in-memory code:

Code: Select all

style = #INDIC_STRAIGHTBOX

color = RGB($00,$FF,$FF)

InnerAlpha  =  50
BorderAlpha = 200

#IGNORE_CASE = #True
AFAIK #INDIC_STRAIGHTBOX does not work with PureBasic's original Scintilla.dll

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Sun Jul 01, 2012 8:33 pm
by c4s
@Danilo
How would Homer Simpson say? D'oh! :wink:

Of course you are absolutely right. I confused the codes because my browser didn't jump to the specific post I linked to, but rather some posts below or so - maybe it's a phpBB issue.
Anyway, as you say the code before is the correct on: http://www.purebasic.fr/english/viewtop ... 60#p381060

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Sat Feb 16, 2013 9:41 pm
by skywalk
Thanks Danilo!
Really helpful stuff to build additional IDE tools. :D

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Sun Mar 08, 2015 9:02 pm
by Blue
I'm late to the party, i know, but i just discovered this great hiliting tool by Danilo.
And i really wanted to say thank you Danilo for a very useful addendum to the PB arsenal.
My psychiatrist also wanted to thank him...

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Sun Mar 08, 2015 11:45 pm
by Demivec
Blue wrote:I'm late to the party, i know, but i just discovered this great hiliting tool by Danilo.
And i really wanted to say thank you Danilo for a very useful addendum to the PB arsenal.
My psychiatrist also wanted to thank him...
@Blue: Danilo's tool was useful. This type of highlighting is currently part of the PureBasic IDE. The IDE just requires to to select the text and then highlights it everywhere in the source, no key-presses necessary.

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Mon Mar 09, 2015 12:50 am
by skywalk
Danilo's highlight is persistent. The IDE unhighlights once you move the cursor. Room for both modes but I prefer persistent since my code doesn't always fit on 1 screen.

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Mon Mar 09, 2015 9:03 am
by Blue
@Demivec : As Skywalk pointed out, the difference between the built-in hiliting and Danilo's method is important.
I don't find the built-in method at all useful, since it only provides a fleeting glance at the occurences of whatever you hilite.
With Danilo's way, you can visually follow your tracks across your code screens.

Try it, and you'll be hooked.

Re: PB IDE Smart Highlight (showing selected text) implement

Posted: Mon Feb 27, 2017 4:42 pm
by #NULL
great tool, thank you!

compared to the built-in IDE feature, this one also works inside strings, with parts of words / any selection, and most importantly it doesn't get confused by *asterisks and @-signs etc.