LUA script to global search and replace

This forum allows you to share scripts with other Zeus users. Please do not post bug reports, feature requests or questions to this forum, but rather use it exclusively for posting scripts or for the discussion of scripts that have been posted.
Post Reply
mahub
Posts: 5
Joined: Mon Mar 07, 2011 9:11 pm

LUA script to global search and replace

Post by mahub »

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

Post by jussij »

Need some assistance creating a lua script to do multiple global replace on multiple strings.
I am happy to try and help but you are need to going to be a bit more specific.

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
mahub
Posts: 5
Joined: Mon Mar 07, 2011 9:11 pm

Post by mahub »

One file at a time.
Most fixed. Example LA_ replace with NY_.

Thanks for all your help.
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

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
But you could also implement this using this more long winded approach:

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
Cheers Jussi
mahub
Posts: 5
Joined: Mon Mar 07, 2011 9:11 pm

Thanks

Post by mahub »

Thanks so much. I will give it a try.
mahub
Posts: 5
Joined: Mon Mar 07, 2011 9:11 pm

Follow Up

Post by mahub »

Can you instruct me how to do it for multiple open files please?

Thanks
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

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
Cheers Jussi
Post Reply