LUA script to global search and replace
LUA script to global search and replace
Need some assistance creating a lua script to do multiple global replace on multiple strings. Not programmers but do have half a brain. Any help is greatly appreciated.
I am happy to try and help but you are need to going to be a bit more specific.Need some assistance creating a lua script to do multiple global replace on multiple strings.
Are you do the search and replace over a single file or multiple files

If it is multiple files, how are you deciding which files need to be processed

When you say multiple strings, are the search and replace patterns fixed and if so what is an example of one such typical pattern

If these patterns are not fixed where are you planning on getting them from

The difficulty is there are so many ways to achieve this and depending on answers to the questions above will determine which of these many approaches is the easiest.
Finally, if you need to do tens or hundreds of individual search and replace actions then it might be better to use a tool like SED to do this work.
It is easy to run tools from inside Zeus macros, so it might be a good idea to write a simple macro that just runs SED and lets SED do all the heavy lifting.
Cheers Jussi
One file at a time.
In that case this is fairly straight forward.
The simplest way to do this is as follows:
Code: Select all
function key_macro()
use_regexp = 0
screen_update_enable()
-- replace all text in current document
replace_all("LA_", "NY_", use_regexp)
screen_update()
search_options_restore()
end
key_macro() -- run the macro
Code: Select all
function replace_text(search, replace)
-- set the search and replace details
set_find_text(search)
set_replace_text(replace)
-- replace all the items in the document
while ReplaceNext() > 0 do
end
end
function key_macro()
screen_update_disable()
-- save current search settings
search_options_save()
-- different scope options
SCOPE_FORWARD = 0
SCOPE_REVERSE = 1
SCOPE_MARKED = 2
SCOPE_ENTIRE = 3
SCOPE_ALL = 4
-- set the search options
set_search_option("Scope" , SCOPE_ALL)
set_search_option("UseCase" , 0)
set_search_option("WholeWord" , 0)
set_search_option("RegExpress", 0)
-- replace all text in current document
replace_text("LA_", "NY_")
search_options_restore()
screen_update_enable()
screen_update()
end
key_macro() -- run the macro
Follow Up
Can you instruct me how to do it for multiple open files please?
Thanks
Thanks
Can you instruct me how to do it for multiple open files please?
The macro below will do the replace across all open, named document files.
Code: Select all
function key_macro()
screen_update_disable()
screen_update_enable()
use_regexp = 0
delimiter = '|'
FULL_DETAILS = 1
-- get all current document files delimited by the pipe character
all_files = document_files(FULL_DETAILS, delimiter)
current_window = get_window_id()
-- split the all the files into their individual file names
for file_name in string.gmatch(all_files, "([^|]*)|?" ) do
-- activate the file window
if window_activate_name(file_name) then
-- replace the text in current document
replace_all("LA_", "NY_", use_regexp)
end
end
-- restore the original window
window_activate(current_window)
screen_update()
end
key_macro() -- run the macro