- 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
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
