Search/Replace Question

Get help with the installation and running of the Zeus IDE. Please do not post bug reports or feature requests here. When in doubt post your question here.
Post Reply
pwdiener
Posts: 134
Joined: Wed Jul 11, 2007 3:45 pm

Search/Replace Question

Post by pwdiener »

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
pwdiener
Posts: 134
Joined: Wed Jul 11, 2007 3:45 pm

Post by pwdiener »

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
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

Hi Bill,

Based on the script you sent the problem is this line of code:

Code: Select all

ReplaceNext()
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:

Code: Select all

if (SearchNext() == 1) then
   ReplaceNext()
end
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:

Code: Select all

search_options_save()
set_search_option("RegExpress", 1)
I think you should set all the control flags as follows:

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)
and at the end of the macro restore the original find settings:

Code: Select all

search_options_restore()
Cheers Jussi
pwdiener
Posts: 134
Joined: Wed Jul 11, 2007 3:45 pm

Post by pwdiener »

Hi, Jussi

The SearchNext() did the trick.

Do all of the keyboard functions return some kind of success indicator or is it just selected ones? I don't find any indication that any of them, including SearchNext() return any sort of value. Is this is just a documentation issue?
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

Do all of the keyboard functions return some kind of success indicator or is it just selected ones?

They all return 1 for success or 0 for failure.
Is this is just a documentation issue?
Yep.

Cheers Jussi
Post Reply