Quote Multiple Lines

This forum allows you to share scripts with other Zeus users. Please do not post bug reports, feature requests or questions to this forum, but rather use it exclusively for posting scripts or for the discussion of scripts that have been posted.
Post Reply
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Quote Multiple Lines

Post 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
Post Reply