Add more style token for highlighting



  • Hello All,

    I use notepad++ to go through very large log files. Sometimes I have to go through more than 50 open files at the same time and search for more than 20 text strings and it get hard to track them down after certain time.
    I think more marking style will help me a lot.

    In the contextMenu.xml, I modified the code following other posts,

        <Item FolderName="Style token" PluginEntryName="Python Script" PluginCommandItemName="color_scheme_veb_ver_1" ItemNameAs="Using 6th Style" />
        <Item FolderName="Style token" PluginEntryName="Python Script" PluginCommandItemName="color_scheme_veb_ver_2" ItemNameAs="Using 7th Style" />*
    

    Similarly for removing the style.

    <Item FolderName="Remove style" PluginEntryName="Python Script" PluginCommandItemName="color_scheme_veb_ver_1" ItemNameAs="Clear 6th Style" />
    	<Item FolderName="Remove style" PluginEntryName="Python Script" PluginCommandItemName="color_scheme_veb_ver_2" ItemNameAs="Clear 7th Style" />
    

    Here is the code for python script:

    def clearIndicators(): 
        text_end_position = editor.getLength()
        editor.setIndicatorCurrent(8)
        editor.indicatorClearRange(0, text_end_position)
    
    def colorize(text_to_be_painted):
        matches = []
        editor.research(text_to_be_painted, lambda m: matches.append(m.span(0)))
    
        for match in matches:
            editor.indicSetStyle(8,INDICATORSTYLE.ROUNDBOX)
            editor.indicSetFore(8,(0,255,0))
            editor.indicSetAlpha(8,255)
            editor.indicSetOutlineAlpha(8,255)
            editor.indicSetUnder(8,True)   
            editor.setIndicatorCurrent(8)
            editor.indicatorFillRange(match[0], match[1] - match[0])  
    
    if editor.getProperty('has_been_styled') != '1':
        selected_text = editor.getSelText()
            if len(selected_text) > 0 and selected_text.isspace() == False:    
                editor.setProperty('has_been_styled', '1')
                colorize(selected_text)
    else:
        editor.setProperty('has_been_styled', '0')
        clearIndicators()
    

    Problems I am facing:

    • randomly the scripts works, sometimes it doesn’t.
    • Sometimes it marks the text which is not even highlighted.

    My objectives:

    • Have ten style token with different colors.
    • If i mark a text string for example “my text string” in one file, then the script should color “my text string” in all the files which are opened in editor.
    • I should be able to do that in just one script instead of creating different scripts and adding them in the “PluginCommandItemName” as I am not able to find a way to send any argument while calling the script which could be used a control variable in the script for different color.

    I tried the combine plugin to merge all the opened file in one and then mark them however that plugin sometimes merge files in a random way which throws the timestamp in the logs in frenzy.

    I would be grateful for your help good community of notepad++.

    Regards,
    Vaibhav



  • @vaibhav-solanki

    a couple of thoughts,
    if you want to have more styles you need to define them, you current solution
    defines only one and then you might provide a dialog which color/style
    should be used before executing the code.

    research is a regular expression search which means if you select text which
    has regex specific chars in it, like for example the dot, question mark, backslash …
    then it won’t treat it as pure text, instead uses it as a regular expression.
    You may think about using searchintarget or findtext instead.

    If you want to have the same text colored in different documents you need to find a way to execute the script automatically when the doucment(buffer) changes.
    The most obvious way would be to register a callback for bufferactivated
    (is a notepad callback, not a scintilla one) and then act accordingly to your needs.

    If there is anything unclear the us know.
    Happy coding.

    Cheers
    Claudia



  • Hello Claudia,
    Thanks you for the reply.
    I would appreciate if you could point me towards some documentation regarding the same.
    Thank you,
    Vaibhav



  • Hello Vaibhav,

    the python script plugin comes with a help file, see plugins->python script-Context help.
    Which gives a good overview of the wrapped functions as well as the usage.

    More detailed about the scintilla component at http://www.scintilla.org/ScintillaDoc.html
    and regarding styling at http://www.scintilla.org/ScintillaDoc.html#Styling

    Examples of scripts here in the forum and at https://sourceforge.net/p/npppythonscript/discussion/1199074/

    Tip: concerning callbacks, use the synchronous version if possible

    Happy coding

    Cheers
    Claudia



  • Claudia,

    I am going to take a lot of time going through all this since I am a newbie.
    Thanks for the guidance. :)

    Regards,
    Vaibhav Solanki



  • @vaibhav-solanki

    MY random thoughts:

    • You can continue to use editor.research() in your code, but you need some code ahead of it to “escape” any special regex characters that might occur in your search string.

    • There isn’t a great way to pass arguments to Pythonscripts, especially from the the right-click context menu. The earlier suggestion of a “dialog” (to get user input for what color) is good and might be best implemented using the notepad.prompt() functionality.

    • A clean implementation may be to do the buffer-activated callback (as suggested), and you may also want to go ahead and implement an update-UI callback. This will allow you to colorize only what is currently on-screen; if you are working with “very large” files and you colorize the whole document, you could noticeably slow-down editor performance (such as when moving around in a log file).



  • Maybe you want to have a look at https://sourceforge.net/projects/analyseplugin/, also it helps you in logfile analysis just for a single file at a time.


Log in to reply