Page 1 of 1

Script to Comment/Uncomment Code

Posted: Tue Jun 08, 2010 12:22 am
by jussij
The Lua macro below will add or remove line comments from the current line or selected text.

Just save this code the the Zeus zScript folder, mark a few lines of text and then run the macro.

Cheers Jussi

Code: Select all

--
--        Name: Line Comment Macro
--
--      Author: Jussi Jumppanen, Alan Stewart
--
--    Language: Lua Macro
--
-- Description: This Lua macro will line comment/uncomment the current
--              line or the set of marked lines with the appropriate line
--              comment character. To use the macro first mark the range
--              of lines to be commented/uncommented and then run this
--              macro.
--
--  How To Run: All Zeus macros can be run using the macro execute menu
--              item or they can be bound to the keyboard or they can
--              be attached to the macro drop down or popup menus.
--

-- the comments file determines the appropriate comment string
dofile ("commentText.lua")

function add_comment(range, comment, comment2)
    -- comment the lines selected
    repeat
        -- move to the start of line
        MoveLineHome()

        -- write out the comment string
        print(comment .. " ")

        if (comment2 ~= nil) then
            -- move to the end of the line
            MoveLineEnd()

            -- add the end of line comment
            print (comment2)
        end

        -- next line
        MoveLineDown()
        range = range - 1
    until range == 0
end

function remove_comment(range, comment, comment2)
    -- un-comment the lines selected
    repeat
        -- get a copy of the current line to be uncommented.
        local text = get_line_text()

        -- Apparently, "--" does some sort of regular-expression matching that
        -- ends up deleting the first two characters of the line, even if they
        -- aren't an exact match.  Oops!  The first '1' is where to start, '1'
        -- being the first character of the string, and the second '1' means to
        -- turn off the regular expression pattern matching.

        MoveLineHome()

        if (string.find (text, comment, 1, 1) == 1) then
            local length = string.len(comment)

            repeat
                CharDelete()
                length = length - 1
            until length == 0

            text = get_line_text()

            -- check for the single white space put there by the add
            if (string.starts(text, " ") == 1) then
                -- remove the extra white space
                CharDelete()
            end
        end

        -- Now handle end-of-line comments.
        if (comment2 ~= nil) then
            local len    = string.len(text)
            local cmtlen = string.len(comment2)
            local start  = len - cmtlen+1

            if (start >= 1) then
                if (string.find (text, comment2, start, 1) == start) then
                    MoveLineEnd ()
                    repeat
                        Backspace()
                        cmtlen = cmtlen - 1
                    until cmtlen == 0
                end
            end
        end

        MoveLineDown()

        range = range - 1

    until range == 0
end

function string.starts(s1, s2)
    if (string.sub(s1, 1, string.len(s2)) == s2) then
        return 1
    end

    return 0
end

function key_macro()
    -- macro only works for writable documents so do a check
    if (dofile ("AS_NotDoc.LUA") == 1) then return end

    -- default to the current line
    local top   = get_line_pos()
    local range = 1

    -- macro only works for marked documents
    local marked = is_marked()

    if marked == 1 then
        -- get the marked text details
        top   = get_marked_top()
        range = get_marked_bottom() - top + 1
    end

    -- the default comment strings
    local comment
    local comment2

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

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

    -- disable screen updates
    screen_update_disable()

    -- save the current cursor
    cursor_save()

    -- move to the fist line of the commented text
    set_line_pos(top)

    -- get the first line of text
    local text = get_line_text(top)

    if (string.starts(text, comment) == 0) then
        -- comment the lines selected
        add_comment(range, comment, comment2)
    else
        -- un-comment the lines selected
        remove_comment(range, comment, comment2)
    end

    -- restore original cursor
    cursor_restore()

    -- restore screen updates
    screen_update_enable()
end

key_macro() -- run the macro