• Login
Community
  • Login

search and highlight 2 or more words with different colors

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
12 Posts 4 Posters 1.2k 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.
  • A
    Alberto Bongiorni
    last edited by Jan 20, 2021, 10:39 AM

    it would be very useful to search and highlight 2 or more words with different colors … especially useful in log analysis

    A G 2 Replies Last reply Jan 20, 2021, 12:42 PM Reply Quote 0
    • A
      Alan Kilborn @Alberto Bongiorni
      last edited by Jan 20, 2021, 12:42 PM

      @Alberto-Bongiorni

      Maybe you wanna look into User Defined Language (UDL) setup for your log-analysis needs, as it can “color” such things very well. If you choose to look into it and need help, feel free to post back here.

      A 1 Reply Last reply Jan 20, 2021, 3:40 PM Reply Quote 1
      • A
        Alberto Bongiorni @Alan Kilborn
        last edited by Jan 20, 2021, 3:40 PM

        @Alan-Kilborn
        Thanks Alan,
        but I would like to have a dynamic search tool such as a non-language specific find.
        With your solution I should create a specific language for each type of analysis.
        If instead there was a dialog that allows you to specify 3-5 terms and highlight each with a color, that would be great!

        A 2 Replies Last reply Jan 20, 2021, 3:46 PM Reply Quote 0
        • A
          Alan Kilborn @Alberto Bongiorni
          last edited by Jan 20, 2021, 3:46 PM

          @Alberto-Bongiorni said in search and highlight 2 or more words with different colors:

          If instead there was a dialog that allows you to specify 3-5 terms and highlight each with a color, that would be great!

          Probably the closest you can get to this is putting your caret on a term and then invoking Search menu > Mark All > and selecting a color. You could assign a keycombo to that so that it is faster than mousing around in the menus (for the coloring part).

          You could “macroize” the whole thing, but that would require having your terms “fixed”, which it doesn’t sound like something you want to have.

          1 Reply Last reply Reply Quote 0
          • A
            Alan Kilborn @Alberto Bongiorni
            last edited by Jan 20, 2021, 4:02 PM

            @Alberto-Bongiorni

            Another thought is that something could be scripted for this if you’re willing to use a scripting plugin, e.g. Pythonscript.
            Let us know what you think about that.

            A 1 Reply Last reply Jan 20, 2021, 4:43 PM Reply Quote 0
            • A
              Alberto Bongiorni @Alan Kilborn
              last edited by Jan 20, 2021, 4:43 PM

              @Alan-Kilborn said in search and highlight 2 or more words with different colors:

              Another thought is that something could be scripted for this if you’re willing to use a scripting plugin, e.g. Pythonscript.
              Let us know what you think about that.

              Sounds like a great idea to me. If anyone can realize it I would appreciate it, otherwise where can I learn how to write a python plugin for notepad ++?

              A 1 Reply Last reply Jan 20, 2021, 7:12 PM Reply Quote 0
              • A
                Alan Kilborn @Alberto Bongiorni
                last edited by Jan 20, 2021, 7:12 PM

                @Alberto-Bongiorni

                Here’s a little Pythonscript demo of the functionality:

                # -*- coding: utf-8 -*-
                
                from Npp import editor, notepad
                
                for indic in [25, 24, 23, 22, 21]:
                    user_input = notepad.prompt('\r\nEnter comma separated terms to show in Style #{}:'.format(25 - indic + 1), '', '')
                    if user_input == None: break
                    if len(user_input) > 0:
                        input_list = user_input.split(',')
                        for inp in input_list:
                            matches = []
                            editor.search(inp, lambda m: matches.append(m.span(0)))
                            for m in matches:
                                editor.setIndicatorCurrent(indic)
                                editor.indicatorFillRange(m[0], m[1] - m[0])
                

                Some nice instructions for how to acquire the Pythonscript plugin and set up a script are found HERE.

                A 1 Reply Last reply Jan 21, 2021, 9:28 AM Reply Quote 3
                • A
                  Alberto Bongiorni @Alan Kilborn
                  last edited by Jan 21, 2021, 9:28 AM

                  @Alan-Kilborn said in search and highlight 2 or more words with different colors:

                  -- coding: utf-8 --

                  from Npp import editor, notepad

                  for indic in [25, 24, 23, 22, 21]:
                  user_input = notepad.prompt(‘\r\nEnter comma separated terms to show in Style #{}:’.format(25 - indic + 1), ‘’, ‘’)
                  if user_input == None: break
                  if len(user_input) > 0:
                  input_list = user_input.split(‘,’)
                  for inp in input_list:
                  matches = []
                  editor.search(inp, lambda m: matches.append(m.span(0)))
                  for m in matches:
                  editor.setIndicatorCurrent(indic)
                  editor.indicatorFillRange(m[0], m[1] - m[0])

                  thanks, it’s almost perfect, can I change the background color depending on the position of the search term? For example 1st blue term, 2nd yellow term etc … ?

                  A 1 Reply Last reply Jan 21, 2021, 1:15 PM Reply Quote 0
                  • G
                    guy038
                    last edited by guy038 Jan 21, 2021, 2:13 PM Jan 21, 2021, 1:14 PM

                    Hello, @alberto-bongiorni, @alan-kilborn and All,

                    Thanks again, Alan for this new script ! After some testing ,I preferred to use the regex mode. So I simply changed the line :

                                editor.search(inp, lambda m: matches.append(m.span(0)))
                    

                    by the line

                                editor.research(inp, lambda m: matches.append(m.span(0)))
                    

                    This modification provides two advantages :

                    • Firstly, we can simulate the Match whole word only and Match case options of the Find/Replace/Find in Files/Mark dialogs. For instance :

                      • Program will mark this string, with this exact case ( Default case )

                      • (?i)Program will mark this string, wahtever its case

                      • \bProgram\b will mark this word, with this exact case

                      • (?i)\bProgram\b will mark this word, whatever its case

                    • Secondly, it allows us to mark areas of differents lengths ! Test, against the well-known license.txt file, the two regexes below :

                      • b.*q

                      • (?s)b.*q

                    Nice, as this kind of marking is impossible with the Search > Mark All >.... native N++ options ;-))


                    One more tip :

                    If you use a same expression for two or more styles, you can mark these expressions with the blend of all the styles involved ! For instance, the picture, below, shows the default style colors and the blend of two styles :

                    f56816eb-3c69-489b-8fef-012f57c9c4b1-image.png

                    Best Regards,

                    guy038

                    P.S. :

                    In the picture below, I added the combinations with 3 styles !

                    d6001d87-6c32-46ac-8d39-970afc0b3c34-image.png

                    1 Reply Last reply Reply Quote 3
                    • A
                      Alan Kilborn @Alberto Bongiorni
                      last edited by Jan 21, 2021, 1:15 PM

                      @Alberto-Bongiorni said in search and highlight 2 or more words with different colors:

                      can I change the background color depending on the position of the search term? For example 1st blue term, 2nd yellow term

                      Not sure what you mean by that, in context of how the script works.
                      Can you re-ask that question with a concrete example?

                      1 Reply Last reply Reply Quote 0
                      • G
                        gstavi @Alberto Bongiorni
                        last edited by Jan 21, 2021, 1:59 PM

                        @Alberto-Bongiorni said in search and highlight 2 or more words with different colors:

                        it would be very useful to search and highlight 2 or more words with different colors … especially useful in log analysis

                        In what way does this improve over right click -> Style token?

                        A 1 Reply Last reply Jan 21, 2021, 2:27 PM Reply Quote 2
                        • A
                          Alan Kilborn @gstavi
                          last edited by Alan Kilborn Jan 21, 2021, 2:29 PM Jan 21, 2021, 2:27 PM

                          @gstavi said in search and highlight 2 or more words with different colors:

                          In what way does this improve over right click -> Style token?

                          It really doesn’t. :-)
                          Except that you don’t have to find an occurrence (by hand or search) of your “token” first, in order to rclick it.
                          The script is really just a demo that someone could build something related upon.
                          I can’t see someone typing all those “tokens” very often…

                          It really makes way more sense to use a UDL for such a purpose (OP’s original “log analysis”). And that was my first suggestion to OP.

                          But everyone has their own workflow.
                          And their own likes, dislikes, and "I want"s.

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