Page 1 of 1

Todo Markers

Posted: Wed Nov 20, 2013 4:57 am
by jussij
The following two scripts will allow the user to add a Todo item and to also search for these Todo items.

todo_add_item.lua

Code: Select all

--
--        Name: Add Todo Marker and Text to Current File
--
--      Author: Jussi Jumppanen
--
--    Language: Lua Macro
--
-- Description: This simple Lua macro adds a todo marker an some user
--              defined text to the current file.
--
--              Use the todo_find_item.lua macro to then search for
--              and navigate to those todo markers.
--

dofile ("commentText.LUA")  -- #include file

function key_macro()
    -- macro only works for documents
    local document = is_document()

    -- macro only works for read/write documents.
    local locked = is_read_only()

    if (locked == 1) or (document == 0) then
        message("This macro only works for writable document files.");
        beep();
        return;
    end

    local nill = ""

    -- ask for a user for a command line
    local todo_text = user_input("Todo:", nill, "Enter the Todo Text")

    if string.len(todo_text) > 0 then
        -- the default comment strings
        local comment
        local comment2
        local commentcol

        screen_update_disable()

        -- get the current file extensions
        local extension = macro_tag("$Ext")

        -- get the comment based on the file extension
        comment, comment2, commentcol = getComment(extension)

        local result = comment .. "[TODO:] " .. todo_text .. "\n\n"

        MarkHide()
        MoveLineHome()
        cursor_save()
        write(result, 0)
        cursor_restore()
        SmartIndent()
        MoveWordNext()

        screen_update_enable()
        screen_update()
    else
        message("Operation cancelled by user.")
    end
end

key_macro() -- run the macro
todo_find_item.lua

Code: Select all

--
--        Name: Search Workspace for Todo Markers
--
--      Author: Jussi Jumppanen
--
--    Language: Lua Macro
--
-- Description: This simple Lua macro will search the current workspace
--              for TODO markers. Use the todo_add_item.lua macro to add
--              the todo markers to the file.
--

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
        file = project_files(name, 1, "\\n")

        if string.len(file) > 0 then
            -- write the list of files to the input response file
            fh:write(file)
            fh:write("\n")
        end
    end

    fh:close()

    return file_name
end

function key_macro()
    is_open = is_workspace_open()

    if (is_open == 1) then
        -- this is the todo marker (must match todo_add_item.lua)
        local pattern = "[TODO:] "

        -- 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
            if pattern:sub(1, 1) ~= '\"' then
                pattern = "\"" .. pattern .. "\""
            end
        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 -x -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("No workspace is currently open.")
        beep()
    end
end

key_macro() -- run the macro