Page 1 of 1

Workspace Search File and Load

Posted: Tue Feb 19, 2013 11:44 pm
by jussij

Code: Select all

--
--        Name: Workspace File Search and Load Example
--
--      Author: Jussi Jumppanen
--
--    Language: Lua Macro
--
-- Description: This simple Lua macro searches all the file names of the
--              current workspace for the user supplied input string value
--              and load any file that contains that value.
--
function seaarch_for_files(file_pattern)
    local count = 0

    -- create a table to store the results
    local files_found = {}

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

    file_pattern = string.lower(file_pattern)

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

        for file in string.gmatch(files, "[^,]+") do
            if string.find(string.lower(file), file_pattern) ~= nil then
                count = count + 1
                files_found[count] = file;
                --message_box(0, files_found[count])
            end
        end
    end

    if count > 0 then
        for i = 1, count, 1 do
            file_open(files_found[i])
        end
    end

    return count
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 file_name = user_input("File Name:", nill, "Search Workspace")

        if string.len(file_name) > 0 then
            -- try to load the files
            if seaarch_for_files(file_name) == 0 then
                message("No files of this pattern found in workspace: " .. file_name)
            end
        else
            message("Operation cancelled by user.")
        end
    else
        message("No workspace currently open.")
        beep()
    end
end

key_macro() -- run the macro