Page 1 of 1

Second Align Text macro

Posted: Wed Jun 03, 2009 3:40 am
by jussij
This script is based on the oriiginal align text macro: http://www.zeusedit.com/forum/viewtopic.php?t=2439

For example assume you code looks like this where | is used to indicate the cursor location:

Code: Select all

        Some |            text
        that      is 
        all    over 
        the           place
Putting the cursor at the | location and running the macro four times gives you the nicely aligned code shown below:

Code: Select all

        Some text
        that is 
        all  over 
        the  place
This script builds on the original by allowing the user to column mark a range of lines and have all marked lines aligned in the one go.

Code: Select all

-- 
--        Name: Align  Macro 
-- 
--      Author: Omer Kircher 
-- 
-- Modified By: Jussi Jumppanen 
-- 
--    Language: Lua Macro 
-- 
-- Description: This function facilitates lining up columns of text. 
-- 
--              To use, load the macro (i.e. F9 key), position the cursor 
--              and then run macro using the Macro Execute (i.e. F8 key) 
-- 
--              Alternatively you can bind the macro to the keyboard. 
-- 
--              This version also lets you do several lines by just using 
--              the column marking to mark all the lines that need to be
--              aligned.
-- 
--              Blank spaces between the cursor and the next word will 
--              be deleted. 
-- 
--              The cursor is then moved to the next line ready for 
--              another operation. 
-- 

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

    screen_update_disable() 

    -- default to the current line
    local top   = get_line_pos()
    local range = 1
    
    -- save the current cursor
    cursor_save()
    
    -- macro only works for marked documents
    local marked = is_marked()
    
    if marked == 1 then
        -- get the marked text details
        top   = get_marked_top()
        left  = get_marked_left()
        range = get_marked_bottom() - top + 1

        -- move to the fist line of the marked text
        set_line_pos(top, left)

        -- remove the markings
        MarkHide()
    end -- if

    
    -- allign the lines selected or the current line
    repeat
        -- look for a white space (32) ascii character 
        if get_char_at() == 32 then 
            -- remove the white space 
            MarkColumnToggle() 
            MoveWordNext() 
            MarkDeleteEx() 
        end 

        -- position the cursor on the next line 
        MoveLineDown() 

        range = range - 1
    until range == 0
    
    -- restore original cursor
    cursor_restore()

    if marked == 0 then
        -- for the unmarked case position the cursor on the next line 
        MoveLineDown() 
    end -- if

    screen_update_enable() 
    screen_update() 
end 

key_macro() -- run the macro