PythonScript wrap around find/replace
-
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. -
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.
-
There are two ways of looking at “wrap around”
- 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)
- 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)
-
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 useWrap around
andReplace 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!