Page 1 of 1

Quote Multiple Lines

Posted: Sat Jan 25, 2020 12:36 am
by jussij
This Zeus macro will take a marked region of lines and turn those lines into a single comma separated line.

For example a marked region containing these values:

Code: Select all

apples
oranges
lemons
will be converted into this single line of text:

Code: Select all

'apple', 'orange', 'lemon'
The source for this script is found below:

Code: Select all

--
--        Name: Qlines
--
--    Language: Lua Macro
--
-- Description: This quote lines macro will converts a region of marked
--              lines into a single comma separated line.
--
--     Example: Running the macro for these three marked lines:
--
--                  apples
--                  oranges
--                  lemons
--
--               will become this line:
--
--                  'apple', 'orange', 'lemon'
--
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

    if is_marked() == 0 then
        message("This macro requires a marked region of lines.")
        beep()
        return;
    end

    screen_update_disable()

    -- get the marked text details
    local top    = get_marked_top()
    local bottom = get_marked_bottom()
    local range  = bottom - top + 1;

    if range > 1 then
        -- remove the markings
        MarkHide()

        -- first line of marked region
        set_line_pos(top, 1)

        -- quote the lines
        repeat
            MoveLineHome()
            set_cursor_pos(1)
            print("'")
            MoveLineEnd()

            if range > 1 then
                print("', ")
            else
                print("'")
            end
            MoveLineDown()
            range = range - 1
        until range == 0

        -- first line of marked region
        set_line_pos(top, 1)

        range = bottom - top;

        -- join the lines
        repeat
             LineJoinNext()
             range = range - 1
        until range == 0

        -- position the line and cursor location
        set_line_pos(top, 1)
    end

    screen_update_enable()
    screen_update()
end

key_macro() -- run the macro