Community
    • Login

    Preserve Style Tokens at Reload of File

    Scheduled Pinned Locked Moved General Discussion
    16 Posts 4 Posters 3.1k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • nsk7evenN
      nsk7even
      last edited by

      For analysing log files, the Notepad++ feature of applying style tokens is extremely helpful. Sadly, all of these tokens are gone, as soon as the log file is reloaded.

      For you people that are more into Notepad++ what is your advice: may this be possible via macro or should I create a feature request?

      Mark OlsonM mkupperM 2 Replies Last reply Reply Quote 0
      • Mark OlsonM
        Mark Olson @nsk7even
        last edited by

        @nsk7even
        What do you mean by “style tokens?”

        nsk7evenN 1 Reply Last reply Reply Quote 0
        • nsk7evenN
          nsk7even @Mark Olson
          last edited by

          @Mark-Olson 6e3a4a50-a202-4fae-94e0-44fbcbea1f2f-grafik.png

          1. double click on a word
          2. open context menu of/on this word selection
          3. open subgroup “style token” (other expression in other languages??)
          4. select one of the colors
          5. all of the occurences throughout the whole file (!) of the selected word are now marked with the color, selected in step 4

          Extremely great feature, I use it extensively! :)

          Alan KilbornA 1 Reply Last reply Reply Quote 2
          • Alan KilbornA
            Alan Kilborn @nsk7even
            last edited by Alan Kilborn

            @nsk7even

            There are some scripts to be found on this site that do such a thing, plus the author of Notepad++ hinted that this would become a native feature, see HERE (but in the intervening time may have lost enthusiasm for that feature). :-(

            nsk7evenN 1 Reply Last reply Reply Quote 2
            • Mark OlsonM
              Mark Olson
              last edited by

              So just thinking out loud, maybe a good way to remember styled tokens would be to put something like this in config.xml:

              <StyledTokens>
                  <StyledToken styleId="1" fileName="path\\to\\foo.txt" token="foo" />
                  <StyledToken styleId="2" fileName="path\\to\\bar.md" token="bar" />
              </StyledTokens>
              

              If we remembered styled tokens this way, the next time Notepad++ opened path\\to\\foo.txt it would apply marker 1 to the token foo, and it would apply marker 2 to the token bar in path\\to\\bar.txt. Would this meet your needs, @nsk7even ?

              Alan KilbornA nsk7evenN 2 Replies Last reply Reply Quote 1
              • mkupperM
                mkupper @nsk7even
                last edited by

                @nsk7even said in Preserve Style Tokens at Reload of File:

                s is extremely helpful. Sadly, all of these tokens are gone, as soon as the log file is reloaded.
                For you people that are more into Notepad++ what is your advice:

                It’s not an area I personally am familiar with setting up but perhaps Notepad++'s language feature would do the trick. Notepad++'s language feature allows the foreground and background colors to be set based on the content of the file you are working work.

                You would be setting up what’s known as a User Defined Language (UDL) that would color code your log files.

                https://npp-user-manual.org/docs/user-defined-language-system/

                nsk7evenN 1 Reply Last reply Reply Quote 1
                • Alan KilbornA
                  Alan Kilborn @Mark Olson
                  last edited by

                  @Mark-Olson said in Preserve Style Tokens at Reload of File:

                  So just thinking out loud, maybe a good way to remember styled tokens would be to put something like this in config.xml

                  I haven’t compared your idea with it, but “bookmarks” are remembered in the current session (e.g. session.xml). Thus config.xml isn’t the place for it. But, overall, probably modeling support for this after how bookmarks are supported is the way to go.

                  1 Reply Last reply Reply Quote 1
                  • nsk7evenN
                    nsk7even @Alan Kilborn
                    last edited by

                    @Alan-Kilborn said in Preserve Style Tokens at Reload of File:

                    @nsk7even

                    There are some scripts to be found on this site that do such a thing, plus the author of Notepad++ hinted that this would become a native feature, see HERE (but in the intervening time may have lost enthusiasm for that feature). :-(

                    ok thank you for this hint - I will search for them.

                    nsk7evenN 1 Reply Last reply Reply Quote 0
                    • nsk7evenN
                      nsk7even @Mark Olson
                      last edited by nsk7even

                      @Mark-Olson said in Preserve Style Tokens at Reload of File:

                      So just thinking out loud, maybe a good way to remember styled tokens would be to put something like this in config.xml:

                      <StyledTokens>
                          <StyledToken styleId="1" fileName="path\\to\\foo.txt" token="foo" />
                          <StyledToken styleId="2" fileName="path\\to\\bar.md" token="bar" />
                      </StyledTokens>
                      

                      If we remembered styled tokens this way, the next time Notepad++ opened path\\to\\foo.txt it would apply marker 1 to the token foo, and it would apply marker 2 to the token bar in path\\to\\bar.txt. Would this meet your needs, @nsk7even ?

                      This is another great idea and I am sure that me and many other will find great use cases for it. [edit: see my following post, there is already a use case and it is fulfilled, using language coloring]

                      For my current request this won’t fit exactly, I’m afraid to tell.
                      This is because for log analysis, the phrases to select are volatile - for now this may be id 123 and next time it may be id 234, while being both combined with expression “did some nasty stuff”; next case won’t be focussed on the latter, but will try to correlate with “was affected in another way”.

                      So there would be no need for saving these tokens permanently, but just between reloads of the current file (that clears all of the tokens currently).

                      1 Reply Last reply Reply Quote 0
                      • nsk7evenN
                        nsk7even @mkupper
                        last edited by

                        @mkupper said in Preserve Style Tokens at Reload of File:

                        @nsk7even said in Preserve Style Tokens at Reload of File:

                        s is extremely helpful. Sadly, all of these tokens are gone, as soon as the log file is reloaded.
                        For you people that are more into Notepad++ what is your advice:

                        It’s not an area I personally am familiar with setting up but perhaps Notepad++'s language feature would do the trick. Notepad++'s language feature allows the foreground and background colors to be set based on the content of the file you are working work.

                        You would be setting up what’s known as a User Defined Language (UDL) that would color code your log files.

                        https://npp-user-manual.org/docs/user-defined-language-system/

                        We are already using exactly this mechanism for basic expression coloring of our log files.
                        This applies on the basic structure of the log file entries like e. g. timestamps, category, level.
                        Additionally all numbers and specific keywords are being highlighted.
                        This is already a huge improvement of readability and we won’t miss that.

                        Now this is the basic environment of reading the logs. Then the contextual quest has to be found within the current logs - and this is where individual highlightnings step into.
                        This is all well for finished logs, because all style tokens will remain there.
                        But for live debugging an application, the nature of log files is to be appended all over the time. At reload of the log file, for being able to see the newly appended lines, all style tokens will be gone then.

                        1 Reply Last reply Reply Quote 1
                        • nsk7evenN
                          nsk7even @nsk7even
                          last edited by nsk7even

                          @nsk7even said in Preserve Style Tokens at Reload of File:

                          @Alan-Kilborn said in Preserve Style Tokens at Reload of File:

                          @nsk7even

                          There are some scripts to be found on this site that do such a thing, plus the author of Notepad++ hinted that this would become a native feature, see HERE (but in the intervening time may have lost enthusiasm for that feature). :-(

                          ok thank you for this hint - I will search for them.

                          Hey @Alan-Kilborn, can you please help me in finding what you referred to?
                          I tried searching the site manually, via tags and via search engine, but didn’t found anything else than this thread and one other that requested something else… :D

                          Alan KilbornA 1 Reply Last reply Reply Quote 0
                          • Alan KilbornA
                            Alan Kilborn @nsk7even
                            last edited by

                            @nsk7even said in Preserve Style Tokens at Reload of File:

                            can you please help me in finding what you referred to?

                            Try:

                            • https://community.notepad-plus-plus.org/topic/18134/style-token-not-saved/
                            • https://community.notepad-plus-plus.org/topic/18052/bookmark-by-style
                            • https://community.notepad-plus-plus.org/topic/21778/whenever-i-color-something-save-close-the-app-and-open-the-same-thing-again-all-the-colors-are-gone
                            nsk7evenN 1 Reply Last reply Reply Quote 3
                            • nsk7evenN
                              nsk7even @Alan Kilborn
                              last edited by nsk7even

                              @Alan-Kilborn said in Preserve Style Tokens at Reload of File:

                              @nsk7even said in Preserve Style Tokens at Reload of File:

                              can you please help me in finding what you referred to?

                              Try:

                              • https://community.notepad-plus-plus.org/topic/18134/style-token-not-saved/
                              • https://community.notepad-plus-plus.org/topic/18052/bookmark-by-style
                              • https://community.notepad-plus-plus.org/topic/21778/whenever-i-color-something-save-close-the-app-and-open-the-same-thing-again-all-the-colors-are-gone

                              thank you very much Alan!

                              I took your script and modified it to fit my needs. What am I talking about? For my use case, it is not necessary to create individual style files, as a log analysis task that is done will purge the need for these specific style token setups.

                              Therefore, I changed the script to use one style file (like in your first draft version of the script):

                              # -*- coding: utf-8 -*-
                              from __future__ import print_function
                              
                              # references:
                              #  https://community.notepad-plus-plus.org/topic/18134/style-token-not-saved/
                              
                              from Npp import *
                              import inspect
                              import os
                              
                              #-------------------------------------------------------------------------------
                              
                              class SSOL2(object):
                              
                                  def __init__(self):
                              
                                      self.debug = True if 0 else False
                                      if self.debug:
                                          pass
                                          #console.show()
                                          #console.clear()
                                      self.this_script_name = inspect.getframeinfo(inspect.currentframe()).filename.split(os.sep)[-1].rsplit('.', 1)[0]
                              
                                      # 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
                              
                                      self.indicator_number_list = [
                                          SCE_UNIVERSAL_FOUND_STYLE_EXT1,
                                          SCE_UNIVERSAL_FOUND_STYLE_EXT2,
                                          SCE_UNIVERSAL_FOUND_STYLE_EXT3,
                                          SCE_UNIVERSAL_FOUND_STYLE_EXT4,
                                          SCE_UNIVERSAL_FOUND_STYLE_EXT5,
                                          SCE_UNIVERSAL_FOUND_STYLE,
                                      ]
                              
                                  def get_data_file_path_for_active_tab(self):
                              	return notepad.getPluginConfigDir() + '\PythonScript\styleTokenBuffer.sty'
                                      # active_file_pathname = notepad.getCurrentFilename()
                                      # if not os.path.isfile(active_file_pathname): return None
                                      # if active_file_pathname.endswith('.sty'): return None
                                      # data_file_for_active_tab = active_file_pathname.rsplit('.', 1)[0] + '.sty'
                                      # return data_file_for_active_tab
                              
                                  def save_data_file(self):
                                      data_file_for_active_tab = self.get_data_file_path_for_active_tab()
                                      if data_file_for_active_tab == None:
                                          self.mb('Problem saving styling data for active tab file')
                                          return
                                      with open(data_file_for_active_tab, 'w') as fo:
                                          for indic_number in self.indicator_number_list:
                                              for (styled_start_pos, styled_end_pos) in self.highlight_indicator_range_tups_generator(indic_number):
                                                  fo.write('{i} {start} {stop}\n'.format(i=indic_number, start=styled_start_pos, stop=styled_end_pos))
                              
                                  def load_data_file(self):
                                      data_file_for_active_tab = self.get_data_file_path_for_active_tab()
                                      if data_file_for_active_tab == None or not os.path.isfile(data_file_for_active_tab):
                                          self.mb('Problem loading styling data for active tab file')
                                          return
                                      with open(data_file_for_active_tab) as fi:
                                          for line in fi:
                                              (indic, start, end) = line.rstrip().split()
                                              editor.setIndicatorCurrent(int(indic))
                                              editor.indicatorFillRange(int(start), int(end) - int(start))
                              
                                  def run(self):
                                      saving_not_loading = self.yes_no_cancel('\r\n'.join([
                                          "SAVE current doc's styling to disk file?\r\n",
                                          "YES = Yes, please SAVE it",
                                          "NO = LOAD styling info from file and apply to current doc",
                                          "CANCEL = I'm outta here"]))
                                      if saving_not_loading == None: return
                                      self.save_data_file() if saving_not_loading else self.load_data_file()
                              
                                  def highlight_indicator_range_tups_generator(self, 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)
                              
                                  def print(self, *args):
                                      if self.debug:
                                          print(self.__class__.__name__ + ':', *args)
                              
                                  def mb(self, msg, flags=0, title=''):  # a message-box function
                                      return notepad.messageBox(msg, title if title else self.this_script_name, flags)
                              
                                  def yes_no_cancel(self, question_text):
                                      retval = None
                                      answer = self.mb(question_text, MESSAGEBOXFLAGS.YESNOCANCEL, self.this_script_name)
                                      if answer == MESSAGEBOXFLAGS.RESULTYES: retval = True
                                      elif answer == MESSAGEBOXFLAGS.RESULTNO: retval = False
                                      return retval
                              
                              #-------------------------------------------------------------------------------
                              
                              if __name__ == '__main__':
                                  SSOL2().run()
                              

                              Please note that the comment I wrote in the other thread applies to both versions of your script: the unmodified and modified versions.

                              1 Reply Last reply Reply Quote 0
                              • Mark OlsonM
                                Mark Olson
                                last edited by

                                You know, I just remembered a plugin that could probably be helpful for this task, AnalysePlugin. I haven’t used it much myself, but it claims to be designed for log analysis and it looked promising when I tried it out.

                                nsk7evenN 1 Reply Last reply Reply Quote 1
                                • nsk7evenN
                                  nsk7even @Mark Olson
                                  last edited by

                                  @Mark-Olson said in Preserve Style Tokens at Reload of File:

                                  You know, I just remembered a plugin that could probably be helpful for this task, AnalysePlugin. I haven’t used it much myself, but it claims to be designed for log analysis and it looked promising when I tried it out.

                                  Never thought of looking for specialised software for that. Thank you for this idea, already installed it, will try this out!
                                  (as this looks quite sophisticated and may be really powerful and helpful, I think for quick log analyses it may be too bulky - maybe not, I will see - but if, I will use your script as well, cause it is quite slim and straightforward)

                                  1 Reply Last reply Reply Quote 1
                                  • nsk7evenN
                                    nsk7even
                                    last edited by

                                    Haaaa … what a pity, just found a problem with using this solution for my use case:
                                    The style highlightnings are being stored/restored for each occurence. This means, newly appended log file lines won’t get the matching style highlightnings.

                                    (I tested the AnalysePlugin also, but - as expected - it is too bulky, buggy and restricted to its own result window; there are good use cases for this solution for repeating scenarios with identical and complex patterns, but it won’t fit for dynamic quick’n’easy highlightnings of always-other-keywords)

                                    Will go back to manually re-applying the 1-5 style tokens at each log file reload, until I find a better solution.

                                    Thanks for all suggestions and ideas, this was really inspiring!

                                    1 Reply Last reply Reply Quote 1
                                    • First post
                                      Last post
                                    The Community of users of the Notepad++ text editor.
                                    Powered by NodeBB | Contributors