Search Current Workspace

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

Search Current Workspace

Post by jussij » Fri Sep 02, 2011 3:03 am

This is simple macro that will search the current workspace for a user supplied string.

NOTE: This macro requires the newer xfgrep version 1.10 which can be found here.

Code: Select all

--
--        Name: Workspace Search Example
--
--      Author: Jussi Jumppanen
--
--    Language: Lua Macro
--
-- Description: This simple Lua macro will search the current workspace
--              for the user supplied input string.
--

function build_response_file()
    -- get then names of all the projects
    local names = projects(",")

    local file_name = macro_tag("$BackupDir") .. "response_file.txt"

    local fh = assert(io.open(file_name, "w"))

    -- get all the files in each all the projects
    for name in string.gmatch(names, "[^,]+") do
        files = project_files(name, 1, "\\n")

        -- write the list of files to the input response file
        fh:write(files)
        fh:write("\n")
    end

    fh:close()

    return file_name
end

function key_macro()
    is_open = is_workspace_open()

    if (is_open == 1) then

        -- default to the current word or highlighted text
        local nill = macro_tag("$WEX")

        -- ask for a user for a command line
        local pattern = user_input("Find Text:", nill)

        if string.len(pattern) > 0 then

            -- get then names of all files in all the projects
            local file_name = build_response_file()

            -- check the pattern for spaces
            if (pattern:find(' ') ~= nill) then
                pattern = "\"" .. pattern .. "\""
            end

            -- check the response file name for spaces
            if (file_name:find(' ') ~= nill) then
                file_name = "\"" .. file_name .. "\""
            end

            -- create the cmd string
            local cmd = "xfgrep.exe -f -n -i -r " .. pattern .. " " .. file_name

            -- setup command control falgs (see the Zeus Macro help for details)
            local flags = 2+4+16+32

            -- run grep command using the response file as an input
            system(cmd, "", flags)
        else
            message("Operation cancelled by user.")
        end
    else
        message("Now workspace currently open")
    end
end

key_macro() -- run the macro

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

Post by jussij » Mon Oct 10, 2011 4:57 am

NOTE: By using the Zeus project_files function as shown above and combining it with a simple search and replace macro, it should be possible to create a macro that does a regexp search and replace across all files of the current project or alll the files in the current workspace.

Cheers Jussi

Post Reply
kuchnie warszawa opinie

Who is online

Users browsing this forum: No registered users and 3 guests