Community
    • Login

    PythonScript wrap around find/replace

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 244 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.
    • M Andre Z EckenrodeM
      M Andre Z Eckenrode
      last edited by

      Is there some way to enable/disable Wrap around for find/replace via PythonScript? I would have expected that to be one of the possible re module flags available, but doesn’t look like it to me.

      Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @M Andre Z Eckenrode
        last edited by Alan Kilborn

        @M-Andre-Z-Eckenrode

        No, if you want it to “wrap around”, you’d have to do two calls to editor.research(). The first one should search caret position to end-of-file position, then the second one should search position 0 to the original caret position.

        Note that in Notepad++ searches with “wrap around” enabled, it also does this; there is no native support in Scintilla for it.

        1 Reply Last reply Reply Quote 4
        • PeterJonesP
          PeterJones @M Andre Z Eckenrode
          last edited by

          @M-Andre-Z-Eckenrode ,

          There are two ways of looking at “wrap around”

          1. If you really mean, “I want to search the whole document, regardless of where my caret is right now.” That can easily be implemented using the startPosition=0 argument. (I believe that’s what effectively happens when you do a regular expression replacement in the N++ Replace dialog using Replace All)
          2. If you really mean, “I want to search the whole document, starting where I am right now and going until the end.” For that in PythonScript, if p is the current caret location, then do it with two calls to the editor.rereplace() or similar function: the first with startPosition=p and endPosition as the length of the file (or without it supplied, that’s what it will default to); and then a second with startPosition=0 and endPosition=p (… which is what Alan posted just as I was typing the last clause)
          M Andre Z EckenrodeM 1 Reply Last reply Reply Quote 2
          • M Andre Z EckenrodeM
            M Andre Z Eckenrode @PeterJones
            last edited by

            @Alan-Kilborn , @PeterJones :

            Thanks for weighing in. What I’m trying to do is a somewhat complicated regex find/replace on a file roughly represented as such:

            [1st line containing data to be used in replacement elsewhere]
            […]
            [2nd line containing data to be used in replacement elsewhere]
            […]
            [3rd line containing data to be used in replacement elsewhere]
            […]
            [1st line where replacement will take place]
            […]
            [2nd line where replacement will take place]
            […]
            [3rd line where replacement will take place]
            […]
            

            Each instance of […] is one or more lines of other data.

            Starting at the beginning of the file, my FIND pattern will match from [1st line containing data to be used in replacement elsewhere] all the way through [1st line where replacement will take place], then insert data retrieved from the former into the latter, and also deleting all lines from start of file up to but not including [2nd line containing data to be used in replacement elsewhere] in the same process. I was hoping it would be simple to have it go on finding/replacing for all the remaining corresponding line pairs if I could make it use Wrap around and Replace All— though perhaps it wouldn’t, since some of the data within the target match areas will have changed between quasi-iterations — but guess I’ll concede defeat and make a loop. Thanks again!

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