Hi, Jussi,
I'm trying to use the ReplaceNext() keyboard function in a LUA macro and running in to a bit of a problem. What I would like is for it to search forward from the current line and replace the next occurrece. I do not want to use the Replace Dialog to start this - what I would like is to set the search options and find/replace text in the macro, then do the ReplaceNext().
My problem is that it appears that the replace dialog establishes the reference point for "next". My current line in the file is ignored by the test macro - instead it seaches from the previous replace. If I've moved forward in the file past some text that matches the find, the first matching text past the previous match is what is replaced, not the first match past the current position.
Is there a way to manipulate the starting point for ReplaceNext? Is there some other keyboard function or macro builtin that I should be calling before the ReplaceNext?
The way it works now is certainly not unreasonable, might even be beneficial in certain circumstances, but it's not what I need at the moment. Any alternatives come to mind?
Bill
Search/Replace Question
Hi, Jussi,
Some additional info: I thought to copy the line to a new file then run the ReplaceNext there. That works every other time.
What I did was to copy a single matching line, then open a new document and paste it in. When I then run my test macro with the ReplaceNext(), it works. If I undo and then run it again, it tells me it reached the end of the document. This is all consistent with current behavior. However, if I then run it again, it does the replace - presumably it reset the "current" point to the begining of the file. Odd.
I did a few more experiments and discovered that the reset point is to the current line, not necessarily the beginning of the file. For the single line file I started with, the effect was the same.
Not sure what this all means, but it struck me as a bit unexpected. I guess fooling around with edge cases is going to turn up some unexpected behaviors...
Bill
Some additional info: I thought to copy the line to a new file then run the ReplaceNext there. That works every other time.
What I did was to copy a single matching line, then open a new document and paste it in. When I then run my test macro with the ReplaceNext(), it works. If I undo and then run it again, it tells me it reached the end of the document. This is all consistent with current behavior. However, if I then run it again, it does the replace - presumably it reset the "current" point to the begining of the file. Odd.
I did a few more experiments and discovered that the reset point is to the current line, not necessarily the beginning of the file. For the single line file I started with, the effect was the same.
Not sure what this all means, but it struck me as a bit unexpected. I guess fooling around with edge cases is going to turn up some unexpected behaviors...
Bill
Hi Bill,
Based on the script you sent the problem is this line of code:
As the name suggests this function will do a replace using the details of the last find and this is why you are seeing the saved or cahced line details.
The drop in code that you need is this:
This code will run a find based on the current line location and then do a replace if anything is found.
Also in your code you have this code:
I think you should set all the control flags as follows:
and at the end of the macro restore the original find settings:
Cheers Jussi
Based on the script you sent the problem is this line of code:
Code: Select all
ReplaceNext()
The drop in code that you need is this:
Code: Select all
if (SearchNext() == 1) then
ReplaceNext()
end
Also in your code you have this code:
Code: Select all
search_options_save()
set_search_option("RegExpress", 1)
Code: Select all
search_options_save()
FORWARD = 0
set_search_option("Scope" , FORWARD)
set_search_option("UseCase" , 0)
set_search_option("WholeWord" , 0)
set_search_option("RegExpress", 1)
Code: Select all
search_options_restore()