Community
    • Login

    Automatic selection of ALL instances of a searched string, in one go

    Scheduled Pinned Locked Moved General Discussion
    37 Posts 7 Posters 4.7k 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.
    • guy038G
      guy038
      last edited by guy038

      Hello, @mark-olson, @ekopalypse and All,

      So, after merging informations, provided by both @mark-olson and @ekopalypse, I ended up with this new version of the @mark-olson one, which uses the editor.addSelection mechanism

      # -*- coding: utf-8 -*-
      
      #  An Adaptation of the @Mark-Olson's Python script with the advice of @ekopalypse :
      #
      #      https://community.notepad-plus-plus.org/topic/24549/automatic-selection-of-all-instances-of-a-searched-string-in-one-go/2 ( and .../4 )
      #
      #
      #  See also :  https://github.com/notepad-plus-plus/notepad-plus-plus/issues/8203#issuecomment-624124396 ( from @sasumner )
      #
      #
      # from __future__ import print_function # for Python2 compatibility
      #
      
      from Npp import editor, notepad
      
      sel_start, sel_end = editor.getUserCharSelection()
      selected = sel_end - sel_start
      
      if selected < 50:
          notepad.messageBox('Selection >= 50 characters or NO selection is MANDATORY', 'IMPORTANT :', 0)
      
      else:
          user_search = notepad.prompt(
              'Use (?i) if INSENSITIVE search and / or SURROUND with \\b for a WHOLE WORD search',
              'Enter the WORD, STRING or REGEX to search for :',
              '(?i)')
      
          if user_search:
      
              span_match_list = []
      
              editor.research(user_search, lambda m: span_match_list.append(m.span(0)), 0, sel_start, sel_end)
      
              print()
      
              print('%d matches of \'%s\', in range [%d, %d]' % (len(span_match_list), user_search, sel_start, sel_end))
      
      #        if not editor.getMultipleSelection(): editor.setMultipleSelection(True)  #  If NOT enabled in 'Preferences... > Editing'
      
              first = True
      
              for m in span_match_list:
      
                  if first:
                      editor.setSelection(m[1], m[0])
                      first = False
      
                  else:
                      editor.addSelection(m[1], m[0])
      

      Notes :

      • Always click anywhere within current document to cancel any NON-wanted possible selection(s), BEFORE running this script !

      • Then, select a range of lines or regions to restrict the future matches to that range or do nothing if future matches will concern the whole current document

      • Finally, run the script and enter the word, string or regex to search for


      Remarks :

      • Apparently, the import from __future__ import print_function, for Python2 compatibility, does not seem necessary !?

      • On the same way, the instruction if not editor.getMultipleSelection(): editor.setMultipleSelection(True), If NOT enabled in Preferences... > Editing, is not needed, too !


      Now, regarding the @ekopalypse’s script, which uses the editor.multipleSelectAddEach mechanism, I simply modified the line

      editor.multipleSelectAddEach()
      

      As :

      if editor.getSelectionEmpty():
          editor.multipleSelectAddEach()
      
      editor.multipleSelectAddEach()
      

      Because, in case of NO selection, the first instruction editor.multipleSelectAddEach() simply selects the current word !

      Best Regards,

      guy038

      Mark OlsonM EkopalypseE 2 Replies Last reply Reply Quote 1
      • guy038G guy038 referenced this topic on
      • Mark OlsonM
        Mark Olson @guy038
        last edited by

        @guy038 said in Automatic selection of ALL instances of a searched string, in one go:

        Apparently, the import from future import print_function, for Python2 compatibility, does not seem necessary !?

        It is necessary for anyone running an older version of PythonScript that uses Python 2. In Python 2, you would write

        print 'hello world'
        

        but in python3 it’s

        print('hello world')
        
        Alan KilbornA 1 Reply Last reply Reply Quote 2
        • EkopalypseE
          Ekopalypse @guy038
          last edited by

          @guy038 said in Automatic selection of ALL instances of a searched string, in one go:

          Because, in case of NO selection, the first instruction editor.multipleSelectAddEach() simply selects the current word !

          The if-check is not necessary because the current word is automatically selected, as you said.

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

            @Mark-Olson said in Automatic selection of ALL instances of a searched string, in one go:

            older version of PythonScript that uses Python 2

            Note that PythonScript plugin version 2 is not necessarily “older”; until the devs of that plugin stop calling version 3 an “alpha” version and retire version 2, version 2 should be considered the mainstream version.

            1 Reply Last reply Reply Quote 0
            • wonkawillyW
              wonkawilly
              last edited by

              IMHO the Automatic selection of ALL instances of a searched string, in one go should be implemented into the Search Replace dialog of Npp

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

                @wonkawilly said in Automatic selection of ALL instances of a searched string, in one go:

                Automatic selection of ALL instances of a searched string, in one go should be implemented into the Search Replace dialog of Npp

                And the reason for this is…?
                Please be specific and give tangible advantages for needing to do this.

                (Guy’s script presented in this thread is one thing, and it is fine to have done, but if we’re debating what should/shouldn’t be in the N++ program natively, then things go a little deeper)

                wonkawillyW 1 Reply Last reply Reply Quote 0
                • wonkawillyW
                  wonkawilly @Alan Kilborn
                  last edited by

                  @Alan-Kilborn said in Automatic selection of ALL instances of a searched string, in one go:

                  @wonkawilly said in Automatic selection of ALL instances of a searched string, in one go:

                  Automatic selection of ALL instances of a searched string, in one go should be implemented into the Search Replace dialog of Npp

                  And the reason for this is…?
                  Please be specific and give tangible advantages for needing to do this.

                  Very trivial Scenarios

                  1. Suppose a user has a file that contains data that needs be to updated. That data is in columns: in this scenario usually it is used column / rectangular selection and, for example, column editor to add a progressive number. Easy and fast.

                  2. But in the moment that the data that must be updated is no more in column the user can’t use rectangular selection so neither column editor at the moment. The user will need to do it manually or to look for a work around of some kind like selecting by hand each one of the instances with all the consequences in term of time spend and even the risk to forget someone of them not processed.

                  So a feature that allows selecting all instances of a search string or a RegEx pattern will solve this issue in seconds instead of going for a workaround because Column Editor will work as well on selections that are spread around in the file even if not in column.

                  Also in find and replace dialog there is the possibility to apply a style to all matches but the use of that feature is less important than selection of all instances of matches because just highlighting them with a nice color do not allow any edit per se.

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

                    @wonkawilly

                    Sorry, I didn’t follow any of that… at all. :-(

                    wonkawillyW 1 Reply Last reply Reply Quote 0
                    • wonkawillyW
                      wonkawilly @Alan Kilborn
                      last edited by

                      @Alan-Kilborn said in Automatic selection of ALL instances of a searched string, in one go:

                      @wonkawilly

                      Sorry, I didn’t follow any of that… at all. :-(

                      Please clarify, I am not getting what you mean, sorry

                      EkopalypseE 1 Reply Last reply Reply Quote 0
                      • EkopalypseE
                        Ekopalypse @wonkawilly
                        last edited by

                        @wonkawilly

                        If I understand you correctly, you want something like this mock?

                        79fc909f-2ff9-4d10-9404-38bbd110e81c-image.png

                        A way to find and select each instance to work in multi-editor/instance mode, right?
                        Yes, that would be beneficial, but I think it would also open a can of worms. I already see that some select 1_000_000 instances of a word.

                        wonkawillyW Alan KilbornA 2 Replies Last reply Reply Quote 1
                        • wonkawillyW
                          wonkawilly @Ekopalypse
                          last edited by wonkawilly

                          @Ekopalypse
                          Yes that would be beneficial indeed IMHO too.

                          IMHO no need of the “only” word, just Select all will work fine

                          Of course everything could have drawbacks when used improperly: even regular expressions if improperly used can open not just one but multiple cans or worms.

                          Also into the dialog the button Count can help to prevent problems. And I suppose that the tools present into the tab Mark will help too for the same reason to check before act.

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

                            @Ekopalypse said in Automatic selection of ALL instances of a searched string, in one go:

                            If I understand you correctly, you want something like this mock?

                            For the record, I understand exactly what @wonkawilly wants.
                            But I wanted him to explain why he thinks having such a capability is so advantageous. I wanted him to do this on his own, so I wasn’t leading him, but he couldn’t figure out what I was asking for, so…

                            What are you going to do when you have all these matches as selected text, that you can’t do already in the N++ search/replace/mark interface, without having text selected as a result of a search?

                            Your choices are:

                            • start typing (this will replace the selected text with what you type; hmmm, there’s already a Replace function that will do this)

                            • copy (there’s already a Copy Marked Text function – ok, you have to mark text first)

                            • delete (just a Replace with nothing, use that function)

                            • cut (copy plus delete, a bit laborious with existing functionality, but is this a really common need?)

                            • scroll around post-search to “eyeball” all the matches (just use Mark All; also, if text was selected and you do this, be careful that you don’t do a keyboard/mouse action that causes all of your selected text to become immediately unselected)

                            In short, no big advantage that I see to having text selected as a result of a search. Sure, it is fine to want it, but perhaps there is a reason it doesn’t already exist.

                            EkopalypseE 1 Reply Last reply Reply Quote 3
                            • EkopalypseE
                              Ekopalypse @Alan Kilborn
                              last edited by

                              @Alan-Kilborn

                              It serves a similar but different purpose, in the end you have several cursors to work with. At the moment, the Find and Replace dialog cannot do this and the options mentioned are, for this case, also no options, because they leave only one cursor after the actions.

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

                                @Ekopalypse said in Automatic selection of ALL instances of a searched string, in one go:

                                in the end you have several cursors to work with

                                And the big advantage to that is…?

                                EkopalypseE 1 Reply Last reply Reply Quote 0
                                • guy038G
                                  guy038
                                  last edited by guy038

                                  Hi, @alan-kilborn and All,

                                  Alan, you asked @wonkawilly to describe the benefits of this potential new feature but, to be honest, I said exactly the same thing in my first post when I added, at the end :

                                  So, I wonder if we should propose an enhancement of the Mark dialog, with a new box option Selection of the Occurrences or equivalent, using this mechanism ?

                                  So, you would also be entitled to level the same criticism at me and ask me to justify the need for multiple cursors !

                                  Thus, I read your last post carefully and, of course, your arguments seem relevant. However, I have an intuitive feeling that this new functionality would be very powerful, especially when you decide to take consecutive actions on this selection of cursors !

                                  For example, considering the text :

                                  This is a simple test
                                    This is a simple test
                                      This is a simple test
                                        This is a simple test
                                          This is a simple test
                                            This is a simple test
                                              This is a simple test
                                                This is a simple test
                                                  This is a simple test
                                                    This is a simple test
                                  
                                  • Let’s say we’ve selected the ten words simple, using the latest version of the Mutliple-Selections.py script.

                                  • First, we decide to delete the word simple : We could use the Replace dialog with the search of simple, Nothing as the replacement and with the In selection box ticked, to obtain the following text :

                                  This is a  test
                                    This is a  test
                                      This is a  test
                                        This is a  test
                                          This is a  test
                                            This is a  test
                                              This is a  test
                                                This is a  test
                                                  This is a  test
                                                    This is a  test
                                  
                                  • However, suppose we change our mind and we want to add 10 line-breaks, AFTER deleting those 10 simple words. Now, using the Replace dialogue is not so easy, because we have to search for the string a test and change it to the string a \r\n test, just for those ten lines ! If we had used the multiple cursors’ technique, we would only have had to press the Enter key :-)

                                  And then, after all: if Neil Hodgson, and his team of devs, have added the SCI_MULTIPLESELECTADDNEXT and SCI_MULTIPLESELECTADDEACH commands, in the Scintilla project, there must be some point in using them !

                                  Best Regards,

                                  guy038

                                  BTW, once I selected the ten lines of my test and used the Multiple_Selections.py script to select the ten words simple and deleted them with the Del key, I tried a Ctrl + Z action in the hope of get the words simple back. But no success ! I suppose that my last version should include a sequence editor.beginUndoAction() and editor.endUndoAction() …

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

                                    @guy038

                                    No worries; it was merely my attempt to point out that it probably wouldn’t be as useful as people think on first consideration.

                                    Now consider the Select all only checkbox in the @Ekopalypse UI mockup. How is one to know which actions this applies to? Does one need it for Find Next? (no). Count? (maybe?). Find All in Current Document? (certainly). Find All in All Opened…? (probably?).

                                    The historical way of options that apply to only certain actions is to put them in a box that groups those actions; here we see In selection as the example of that. How would something like that best be done here, or is the Select All Only checkbox left floating out on its own?

                                    Would this apply to Find in Files as well, for documents that are already open?

                                    Is there any effect for Mark? Maybe the new checkbox only appears on the marking tab, as this has some logical commonality.

                                    It’s good to want things, but it is also good to consider why you want them, and then all the downstream concerns.

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

                                      @guy038 said in Automatic selection of ALL instances of a searched string, in one go:

                                      BTW, once I selected the ten lines of my test and used the Multiple_Selections.py script to select the ten words simple and deleted them with the Del key, I tried a Ctrl + Z action in the hope of get the words simple back. But no success ! I suppose that my last version should include a sequence editor.beginUndoAction() and editor.endUndoAction()

                                      I didn’t try your script, but setting up a similar test, where Delete is done with multiple selections active and then doing Undo DID achieve the undo for me. I see no reason why it should be different with a script pre-selecting data that you manually press the Delete key on post-script.

                                      In your script, editor.beginUndoAction() and editor.endUndoAction() aren’t going to do anything, because your script doesn’t change the text…so there’s nothing to undo.

                                      Lycan ThropeL 1 Reply Last reply Reply Quote 3
                                      • guy038G
                                        guy038
                                        last edited by

                                        Hello, @alan-kilborn,

                                        Of course, you’re right about it ! Don’t know which manipulation I did before ! I confirm that after deleting the 10 words simple of my test, with the script, adding a line-break then some dummy text, the Ctrl + Z actions did delete the added text as well as the line-breaks and get the words simple back ! The status of the file was also back to unchanged !

                                        BR

                                        guy038

                                        1 Reply Last reply Reply Quote 3
                                        • mkupperM
                                          mkupper
                                          last edited by mkupper

                                          I have always thought of selections as being temporary and fragile things and don’t think I would be comfortable with constructing selections using a search expression. I like to be able to look things over before I perform some action… One click of a mouse or keyboard can wipe out a carefully constructed selection.

                                          An alternative is to use marking and to add a Select Marked Text button. This button likely could replace the Copy Marked Text button on the Mark dialog box.

                                          Once the marked text is selected then people could then copy it, replace it, delete it, convert to upper/lower case, etc.

                                          At present, unmarking is moderately awkward but is possible. Ideally, the Mark dialog box was a little more consistent. The Mark All button marks using a combination of the search pattern plus the In selection checkbox. The Clear all marks button though ignores the search pattern field but does honor In selection.

                                          That leads me to thinking about adding a Mark selected text button so that I can drag-select some text, mark it, drag-select something else and mark it without needing to use the search pattern field.

                                          A potential downside to the idea is that because selecting and marking do not make changes to the text that the activity related to setting up the marked area(s) or selection(s) never show up in the undo/redo history. Someone could loose chunk of time.

                                          Alan KilbornA 1 Reply Last reply Reply Quote 4
                                          • Lycan ThropeL
                                            Lycan Thrope @Alan Kilborn
                                            last edited by

                                            @Alan-Kilborn ,

                                            I’m glad you did this, because I was thinking the same thing. I don’t know why his Undo didn’t work, perhaps because it was a script that did other things in addition, but whenever I do a change or search/replace, I feel it’s on me, to make sure I really want that change, as @mkupper suggests.

                                            For that, the usefulness of the Mark comes into play and allows one to see what they’ll be changing ahead of time using the supplied regex. Using these things in bulk text changes is always a risk so again, these changes need to be tempered with good reason, as @Alan-Kilborn suggests, rather than willly-nilly giving users more complex operations than they truly need which just adds to more confusion, support problems and trying to explain to people how to properly use it which will benefit, what? Maybe 1% of the total users?

                                            That’s what the scripting option is supposed to be for, so that the 1% (which most likely will also be superusers with the skills, anyways) can craft custom capabilities that the other 99% don’t need to be able to use or heaven forbid, stumble into and send them straight here looking for people to unscrew their lack of understanding of what “Don’t push the red button” means. :-)

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