Style token not saved



  • I am a newbie, so hope I am not wasting peoples time. I am using the Style token to ‘highlight’ some lines of code. I save the file and when I reopen it, the highlighting is gone. I have also tried to ‘save session’ but the results are the same.

    Please advise if what I am asking for is possible, or the steps I should be doing to acomplish my goal.

    Thanks in advance for your time to reply.

    John R. in NC



  • @John-Romero said:

    Please advise if what I am asking for is possible

    Not currently possible.

    the steps I should be doing to acomplish my goal

    I don’t know that there are any. :(



  • @Alan-Kilborn said:

    I don’t know that there are any. :(

    I’m not good with the bookmarks / mark-styles… but I seem to remember there’s been some PythonScript about them before… I’m wondering if a script could be written that could store a session-like record of what’s been marked, and then re-mark when that file is reloaded again.



  • @PeterJones

    It could be done, not sure I remember a script, tho.

    A problem could be that people then start thinking it is a dynamic thing, whereas styling is an on-demand thing.

    Say you had a document with several occurrences of supercalifragilisticexpialidocious in it, and the user styles that as say style #4, and then a script remembers that. From that point forward, if a new occurrence of that word occurs, either by the user typing it out (horror for the example word!) or by a paste of it, the expectation might be that the new occurrence gets the styling. But that goes against the concept of on-demand…

    So I’m not quite sure what to make of this. I (or Eko!) could do a script, but the spec is uncertain.



  • Thanks for your responses. What I am doing is migrating code code from one financial trading platform to another. No color - means I still need to do something. So, as I migrate a group of lines, I highlight them one color to know what I have / have not done. If I am not migrating them, then I highlight them another color. This way, when I get done, it will be easier for me to see if a line of code I did not migrate, was really needed, or ‘rethink’ how to do it in the new platform.

    Again - to all. Thanks for your time to look at this. I am currently using Word, as it lets me do this, but does not have any of the nice to have as a coder. (matching brackets, etc. in c#)

    John



  • Peter said:

    I’m not good with the bookmarks / mark-styles… but I seem to remember there’s been some PythonScript about them before

    Peter was thinking of this I think: https://notepad-plus-plus.org/community/topic/18052/bookmark-by-style

    @John-Romero :

    Sure, this type of thing could be adapted for a Load/Save type functionality. Let me see what I can do. But no promises… [Note, I only go off and do these types of things because there could potentially be value in it for my use as well.]



  • So as luck would have it I had some time immediately. :)

    I would caution users to work with this with documents that are read-only, otherwise be very careful about not changing any data until you’ve reloaded the styles with it from a previous run of N++. Otherwise the styling that is loaded will be “skewed” and will be, well, basically lost and unrecoverable. You’ve been warned. No actual text data will be harmed in any event.

    Here’s the Pythonscript that works for either saving or loading the styling. It works for the currently loaded Notepad++ file (name doesn’t matter) and saves its info to a fixed-name data file. All this behavior can be changed, but that’s left as an exercise for the reader; I’m just trying to get the core logic expressed here. Error-checking is minimal to non-existent. Niceties: missing-in-acton as well. Much of the core of the script copied from the link referenced earlier.

    def main():
    
        result = notepad.messageBox("SAVE current doc's styling to disk file?\r\n\r\nYES = Yes, please\r\nNO = LOAD styling info from file and apply to current doc\r\nCANCEL = I'm outta here", '', MESSAGEBOXFLAGS.YESNOCANCEL)
        if result == MESSAGEBOXFLAGS.RESULTCANCEL: return
        saving_not_loading = True if result == MESSAGEBOXFLAGS.RESULTYES else False
    
        # identifiers pulled from N++ source code:
        SCE_UNIVERSAL_FOUND_STYLE_EXT1 = 25  # N++ style 1 indicator number
        SCE_UNIVERSAL_FOUND_STYLE_EXT2 = 24  # N++ style 2 indicator number
        SCE_UNIVERSAL_FOUND_STYLE_EXT3 = 23  # N++ style 3 indicator number
        SCE_UNIVERSAL_FOUND_STYLE_EXT4 = 22  # N++ style 4 indicator number
        SCE_UNIVERSAL_FOUND_STYLE_EXT5 = 21  # N++ style 5 indicator number
        SCE_UNIVERSAL_FOUND_STYLE = 31  # N++ red-"mark" feature highlighting style indicator number
    
        indicator_number_list = []
        indicator_number_list.append(SCE_UNIVERSAL_FOUND_STYLE_EXT1)
        indicator_number_list.append(SCE_UNIVERSAL_FOUND_STYLE_EXT2)
        indicator_number_list.append(SCE_UNIVERSAL_FOUND_STYLE_EXT3)
        indicator_number_list.append(SCE_UNIVERSAL_FOUND_STYLE_EXT4)
        indicator_number_list.append(SCE_UNIVERSAL_FOUND_STYLE_EXT5)
        indicator_number_list.append(SCE_UNIVERSAL_FOUND_STYLE)
    
        if saving_not_loading:
    
            def highlight_indicator_range_tups_generator(indicator_number):
                '''
                the following logic depends upon behavior that isn't exactly documented;
                it was noticed that calling editor.indicatorEnd() will yield the "edge"
                (either leading or trailing) of the specified indicator greater than the position
                specified by the caller
                this is definitely different than the scintilla documentation:
                "Find the start or end of a range with one value from a position within the range"
                '''
                if editor.indicatorEnd(indicator_number, 0) == 0: return
                indicator_end_pos = 0  # set special value to key a check the first time thru the while loop
                while True:
                    if indicator_end_pos == 0 and editor.indicatorValueAt(indicator_number, 0) == 1:
                        # we have an indicator starting at position 0!
                        # when an indicator highlight starts at position 0, editor.indicatorEnd()
                        #  gives us the END of the marking rather than the beginning;
                        #  have to compensate for that:
                        indicator_start_pos = 0
                    else:
                        indicator_start_pos = editor.indicatorEnd(indicator_number, indicator_end_pos)
                    indicator_end_pos = editor.indicatorEnd(indicator_number, indicator_start_pos)
                    if indicator_start_pos == indicator_end_pos: break  # no more matches
                    yield (indicator_start_pos, indicator_end_pos)
    
            with open('styling.txt', 'w') as f:
                for indic_number in indicator_number_list:
                    for (styled_start_pos, styled_end_pos) in highlight_indicator_range_tups_generator(indic_number):
                        f.write('{i} {start} {stop}\n'.format(i=indic_number, start=styled_start_pos, stop=styled_end_pos))
    
        else:
    
            with open('styling.txt') as f:
                for line in f:
                    (indic, start, end) = line.rstrip().split()
                    editor.setIndicatorCurrent(int(indic))
                    editor.indicatorFillRange(int(start), int(end) - int(start))
    
    main()
    

Log in to reply