Page 1 of 1

Template like Macro

Posted: Sat May 05, 2012 6:57 am
by jussij
A simple Lua macro that wraps a console print line statement around the the current word or marked area around.

For example this:

Code: Select all

    "Hello World"
will become this:

Code: Select all

    System.out.println("Hello World");

Code: Select all

-- Simple Lua Macro
--
-- Take the current word or marked area and wrap it in a print
-- statement.
--
-- For example highlight "Hello World" run the macro and the text
-- will be convert into this:
--
--   System.out.println("Hello World")
--
-- This macro can be bound to any key combination or to the menu.

function key_macro()
    screen_update_disable()

    local word = ""

    if is_marked() then
        -- get the marked area
        word = macro_tag("$M")

        -- delete the marked text
        MarkDelete()
    else
        -- get the current word or marked area
        word = macro_tag("$WordEx")

        if string.len(word) > 0 then
            -- delete the current word
            WordDelete()
        end
    end

    if string.len(word) > 0 then
        -- write out the print line call
        print("System.out.println(" .. word .. ");")
    else
        message("No marked text was found and the cursor is not over a word!")
    end

    screen_update_enable()
    screen_update()
end

key_macro() -- run the macro