Page 1 of 1

Search Current Workspace

Posted: Fri Sep 02, 2011 3:03 am
by jussij
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

Posted: Mon Oct 10, 2011 4:57 am
by jussij
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