Community
    • Login

    Using Mark (Markers) and Styles

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    34 Posts 6 Posters 20.0k 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.
    • Sridhar RaghavanS
      Sridhar Raghavan
      last edited by

      Alan

      Just ran your code.
      It took only a few seconds to get going with it.

      Plugins->Python Scripts -> New -> Gave a Name to the file,
      copied and pasted your code,
      saved.

      The script showed up in Plugins->Python Scripts-> Scripts

      Works nicely creating different colored marked items.
      Thanks for the Nice Job creating this basic idiom for building further.

      br Sri.

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

        @Sridhar-Raghavan

        I’m still unsure why you even need the bookmarks.

        Say you use the script to mark multiple pieces of text with multiple different colors.

        Then you can use the Jump Down (or Jump Up) commands on the Search menu to navigate to any color you’d like:

        3be7fc72-d780-4b90-a365-bf019975492f-image.png

        Either one of these submenus contains the colors possible:

        08442ca6-3741-4fb2-b649-02b2df488573-image.png

        So for example, say you mark some text (with the script) in the 5th style, i.e. default of green color. Then you may find occurrences of your marked text from you current position to the end of your file by repeatedly pressing Ctrl+5.

        No need for bookmarks of any type, or am I still misunderstanding your desires?

        1 Reply Last reply Reply Quote 1
        • Sridhar RaghavanS
          Sridhar Raghavan
          last edited by

          Alan

          You are absolutely right. The ability to mark different colors and jump up/down gives all the functionality I needed. Bookmark is not needed for the stated purpose.

          I am happy to see that this exactly addresses the “canonical” problem I was referring to. I am sure there would be additional operations may be invoked on “Bookmarked” lines, that can be very useful.

          Side Note. Like Copy Bookmarked Lines, Remove Bookmarked Lines, Remove Un Marked Lines.

          br Sri.

          More…

          Here a few additional things I would like to do in the work activity context …

          a) Show Only Marked Areas. (and hide all Un-Marked Areas). Specific to a color.

          If a line has a marked text, then
          N lines above and
          M lines below will be
          considered as “Marked Areas”.
          (this will give a window surrounding the marked text).

          And the counterpart Unhide Marked Areas of Specific Color.

          Note: Current Mark Panel has a “Copy Marked Text” button, but it only copies the “marked text” and not the lines. So you get a redundant list of the same text many times (except one is using regex).

          b) Bookmarks can be inversed. Good to know and this is nice.

          Is there a function to hide bookmarked lines? (and of course unhide if it can be defined)

          I owe a deep and sincere thanks to you. This thread of discussion has been personally extremely valuable for understanding these features [which I had not been using], the main drive for this thread. I hope this also benefits others. Furthermore, it triggers a revisit of these related functions by developers, at some point, for expressive consistencies and completeness.

          I will certainly write a short note on things learned here and post it. A similar note by you might be far better given your solid overall understanding of individual functions and how they may be combined together.

          Alan KilbornA 3 Replies Last reply Reply Quote 2
          • Alan KilbornA
            Alan Kilborn @Sridhar Raghavan
            last edited by

            @Sridhar-Raghavan said in Using Mark (Markers) and Styles:

            I am sure there would be additional operations may be invoked on “Bookmarked” lines, that can be very useful.
            Side Note. Like Copy Bookmarked Lines, Remove Bookmarked Lines, Remove Un Marked Lines.

            You mean these kinds of commands? (note red dot is right-click point):

            2a2c2d16-a04d-4437-8f7d-a5e76a39d7c0-image.png

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

              @Sridhar-Raghavan said in Using Mark (Markers) and Styles:

              Note: Current Mark Panel has a “Copy Marked Text” button, but it only copies the “marked text” and not the lines. So you get a redundant list of the same text many times (except one is using regex).

              Yes, it often goes unstated here when people talk about Copy Marked Text, but it really is only useful when dealing with text that was marked by a regular expression search.

              1 Reply Last reply Reply Quote 1
              • Alan KilbornA
                Alan Kilborn @Sridhar Raghavan
                last edited by Alan Kilborn

                @Sridhar-Raghavan said in Using Mark (Markers) and Styles:

                Is there a function to hide bookmarked lines? (and of course unhide if it can be defined)

                Show Only Marked Areas. (and hide all Un-Marked Areas). Specific to a color.

                And the counterpart Unhide Marked Areas of Specific Color.

                Notepad++ has a “hide / unhide lines” feature, see View menu > Hide Lines, or maybe more handy, on the right-click context menu (bottom entry called Hide Lines).
                But it is purely a user selection thing – there is no way within Notepad++ to hide lines based upon a result of a search/mark operation.
                Scripting can do it, but there’s a limitation: It would get out of sync with user interface control of hidden lines.
                If that’s not a big deal to you (you would only hide/unhide lines via scripted commands), then that’s doable.

                1 Reply Last reply Reply Quote 2
                • Sridhar RaghavanS
                  Sridhar Raghavan
                  last edited by

                  Alan

                  I agree it is better to use Scripting to weave together simple building block functions into more powerful personalized ones. But, as you pointed out, unless Scripting is done carefully, one may easily break the consistencies ensured rigorously in the core code.

                  Just a quick/simple question.
                  I installed your code and it functions fine as intended.

                  How do I Edit that Script code? Do not see any obvious menus for it.

                  More generally, what is the basic edit/test iterate cycle?
                  So I can experiment with your code.

                  br Sri.
                  P.S. I am holding off on systematic reading the full Scripting Tutorial as yet.

                  Alan KilbornA 2 Replies Last reply Reply Quote 1
                  • Alan KilbornA
                    Alan Kilborn @Sridhar Raghavan
                    last edited by

                    @Sridhar-Raghavan said in Using Mark (Markers) and Styles:

                    How do I Edit that Script code? Do not see any obvious menus for it.

                    Ah, yes, that’s a bit of a “trick” for the first-timers.

                    To run the script you select it via the menus (as I’m sure you’ve found).
                    To edit it (after the first time you’ve closed it, of course, otherwise you don’t have to find/open it to edit it, it’s just there), you hold down Ctrl while you pick its name from the menus. This opens the file for editing rather than running the script.

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

                      I thought of another bit of a downside to hiding lines:
                      If you switch away from the tab in which you’ve hidden lines (via script), when you return to that tab, all the lines become visible again.

                      That makes for an easy way to make lines visible again in a simple demo, but in a real application the code would have to handle that situation reasonably, which makes the code much more complicated.

                      Here’s a simple demo for only showing lines with hits and two lines before and three lines after:

                      # -*- coding: utf-8 -*-
                      from __future__ import print_function
                      
                      from Npp import editor, notepad
                      
                      # Notepad++'s style numbers:
                      #  25 = Style #1
                      #  24 = Style #2
                      #  23 = Style #3
                      #  22 = Style #4
                      #  21 = Style #5
                      
                      class T_21275a(object):
                      
                          def __init__(self):
                              lines_above = 2
                              lines_below = 3
                              search_input = notepad.prompt('Enter search string:', '', '')
                              if search_input == None or len(search_input) == 0: return
                              matches = []
                              editor.search(search_input, lambda m: matches.append(m.span(0)))
                              if len(matches) > 0:
                                  color_input = notepad.prompt('Enter style color number (1-5):', '', '')
                                  try:
                                      color_input = int(color_input)
                                  except ValueError:
                                      return
                                  if 1 <= color_input <= 5:
                                      color_input -= 1
                                      editor.hideLines(1, editor.getLineCount() - 1)
                                      for (match_start_pos, match_end_pos) in matches:
                                          editor.setIndicatorCurrent(25 - color_input)
                                          editor.indicatorFillRange(match_start_pos, match_end_pos - match_start_pos)
                                          match_start_line = editor.lineFromPosition(match_start_pos)
                                          match_end_line = editor.lineFromPosition(match_end_pos)
                                          show_start_line = match_start_line - lines_above
                                          if show_start_line < 1: show_start_line = 1
                                          show_end_line = match_end_line + lines_below
                                          if show_end_line > editor.getLineCount() - 1: show_end_line = editor.getLineCount() - 1
                                          editor.showLines(show_start_line, show_end_line)
                      
                      if __name__ == '__main__': T_21275a()
                      
                      1 Reply Last reply Reply Quote 2
                      • Alan KilbornA
                        Alan Kilborn @Sridhar Raghavan
                        last edited by

                        @Sridhar-Raghavan said in Using Mark (Markers) and Styles:

                        unless Scripting is done carefully, one may easily break the consistencies ensured rigorously in the core code.

                        Well, to be honest, I think in the core code the hidden line feature is just not that good. So much for the need for “consistencies ensured rigorously”. :-)

                        And, strangely, in all of the forum postings here, hiding and showing lines again is very rarely discussed. Probably, because in Notepad++ the feature is very basic as I described earlier.

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

                          @Alan-Kilborn said in Using Mark (Markers) and Styles:

                          I think in the core code the hidden line feature is just not that good

                          As an example, it is easy to get the UI out of sync just using the built-in features (no scripting).

                          Example:

                          Start with text:

                          one
                          two
                          three
                          four
                          five
                          six
                          seven
                          eight
                          nine
                          ten
                          

                          Make a selection of some lines:

                          7d1b8e7d-3b23-4cf9-a08a-e36bf12f21cb-image.png

                          Right-click that selection and pick Hide Lines to obtain:

                          b1f41544-63c7-45aa-9258-5087ba51078e-image.png

                          Make another selection, like so:

                          b30efab0-2fb3-4a13-93ff-95d52c414754-image.png

                          Right-click that selection and pick Hide Lines to obtain:

                          62fa30c0-990a-4d46-87bc-2cd9f3a75866-image.png

                          Left-click the green arrow in the margin on line 2.
                          This seems like it should expand the text back to a no-lines-hidden state, or perhaps the previous state where lines 5, 6 and 7 were the only hidden ones.
                          But what we get is:

                          d98da195-f284-46a5-ae01-8a42b60dd8bf-image.png

                          So we do get the state where lines 5, 6 and 7 are the hidden ones.
                          But…there’s no way to ever unhide them (well, switching tabs and back does it!)
                          There’s no right-pointing green arrow, and clicking on the left-pointing arrow does nothing.

                          So the conclusion is that this feature is broken (if I can break it so easily).

                          Victorel PetrovichV 3 Replies Last reply Reply Quote 1
                          • Sridhar RaghavanS
                            Sridhar Raghavan
                            last edited by

                            Thanks Alan for that tip. I would have never found it by trying things (hoping for the hidden commands!). That gets me going now.

                            Thanks for the quick work on the code for the “windowed” display. Great.

                            I think your code with additional check boxes and drop downs is rapidly getting to becoming a good plugin!!

                            It was a general experiential statement on my part that APIs and Scripting languages are after-thought add-ons, done with less rigor. But not always the case, including the example you cite.

                            br Sri.

                            1 Reply Last reply Reply Quote 2
                            • guy038G
                              guy038
                              last edited by

                              Hi, @sridhar-raghavan, @alan-kilborn and All,

                              Just tried your second version, using 1 line before hit and 1 line after it. Interesting, indeed, with the “Hide Lines” mechanism. Just enable the line number margin !

                              Of course, as soon as you switch to another tab and switch back to your file, all the hidden lines appear again !


                              Now, I thought about two improvements. But, Alan, they are just suggestions and ONLY IF @sridhar-raghavan would need it, too !

                              • By default, the Python search is sensible to case et does not care about the whole word notion. So, could it possible to search for any case and to search for whole words only ?

                              • I thought that you could also enter all the parameters in an unique prompt window !

                              • With double quotes to surround expressions containing space characters

                              • With inside a couple of parentheses :

                                • w would mean Word only

                                • i would mean Ignore case

                                • r would mean Remove all styles

                                • # would mean Style #

                              When absent, each parameter would be supposed to be the default present Python script behavior !


                              So, for instance, the text License (rw5) software (i3) "GNU GENERAL PUBLIC LICENSE" (iw43) would mean :

                              • Remove all styles first

                              • Highlight any word License, with this exact case, in style 5

                              • Highlight any string software, whatever its case, in style 3

                              • Highlight any exact expression GNU GENERAL PUBLIC LICENSE, whatever its case, in style 4 AND in style 3, too

                              Best Regards,

                              guy038

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

                                @guy038 said in Using Mark (Markers) and Styles:

                                Of course, as soon as you switch to another tab and switch back to your file, all the hidden lines appear again !

                                Yes, this limitation was mentioned earlier.
                                It can be avoided with more code, but as this was just intended to be a demo to show @Sridhar-Raghavan the possibilities of scripting, I didn’t do it.

                                Regarding other changes: Yes, with scripting the possibilities are limitless! :-)

                                1 Reply Last reply Reply Quote 1
                                • Victorel PetrovichV
                                  Victorel Petrovich @Alan Kilborn
                                  last edited by

                                  @Alan-Kilborn said in Using Mark (Markers) and Styles:

                                  But…there’s no way to ever unhide them (well, switching tabs and back does it!)
                                  There’s no right-pointing green arrow, and clicking on the left-pointing arrow does nothing.
                                  So the conclusion is that this feature is broken (if I can break it so easily).

                                  Maybe you already know, as > 2 years passed, but if not: I could not reproduce that bug, so I guess it’s been fixed.

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

                                    @Victorel-Petrovich said in Using Mark (Markers) and Styles:

                                    Maybe you already know, as > 2 years passed, but if not: I could not reproduce that bug, so I guess it’s been fixed.

                                    Not sure what you mean has been fixed…

                                    1 Reply Last reply Reply Quote 0
                                    • Victorel PetrovichV
                                      Victorel Petrovich @Alan Kilborn
                                      last edited by Victorel Petrovich

                                      This post is deleted!
                                      1 Reply Last reply Reply Quote 0
                                      • Victorel PetrovichV
                                        Victorel Petrovich @Alan Kilborn
                                        last edited by

                                        @Alan-Kilborn , the bug in your message above:

                                        As an example, it is easy to get the UI out of sync just using the built-in features (no scripting).

                                        Example:

                                        Start with text:

                                        one
                                        two
                                        three
                                        four
                                        five
                                        six
                                        seven
                                        eight
                                        nine
                                        ten
                                        

                                        Make a selection of some lines:

                                        7d1b8e7d-3b23-4cf9-a08a-e36bf12f21cb-image.png

                                        Right-click that selection and pick Hide Lines to obtain:

                                        b1f41544-63c7-45aa-9258-5087ba51078e-image.png

                                        Make another selection, like so:

                                        b30efab0-2fb3-4a13-93ff-95d52c414754-image.png

                                        Right-click that selection and pick Hide Lines to obtain:

                                        62fa30c0-990a-4d46-87bc-2cd9f3a75866-image.png

                                        Left-click the green arrow in the margin on line 2.
                                        This seems like it should expand the text back to a no-lines-hidden state, or perhaps the previous state where lines 5, 6 and 7 were the only hidden ones.
                                        But what we get is:

                                        d98da195-f284-46a5-ae01-8a42b60dd8bf-image.png

                                        So we do get the state where lines 5, 6 and 7 are the hidden ones.
                                        But…there’s no way to ever unhide them (well, switching tabs and back does it!)
                                        There’s no right-pointing green arrow, and clicking on the left-pointing arrow does nothing.

                                        So the conclusion is that this feature is broken (if I can break it so easily).

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

                                          @Victorel-Petrovich said in Using Mark (Markers) and Styles:

                                          the conclusion is that this feature is broken (if I can break it so easily).

                                          it is easy to get the UI out of sync just using the built-in features (no scripting).

                                          Ah, OK – I thought you meant something with the script.
                                          Yes, the feature is very broken, for many reasons.
                                          This has long been known.

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