For example given this text:
Code: Select all
long_var_name = 25
var = 5
another_value = 10
assignment = 5
very_long_var_name = 25
Code: Select all
long_var_name = 25
var = 5
another_value = 10
assignment = 5
very_long_var_name = 25
Code: Select all
-- Author: Jussi Jumppanen
--
-- Language: Lua Macro
--
-- Description: This macro will align a marked region based on the user
-- supplied character. You can also selectively control
-- which regions of the line that get aligned by using the
-- column marking mode. This macro alligns to the right.
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
-- macro only works for marked documents
local marked = is_marked()
if marked == 0 then
message("This macro requires you to mark a region of text.");
beep();
return;
end
if marked == 1 then
-- get the marked text details
top = get_marked_top()
left = get_marked_left()
range = get_marked_bottom() - top + 1
end
if range > 1 then
screen_update_disable()
-- save the current cursor
cursor_save()
-- the initial value
local nill = ""
-- ask for a user provide the alignment character
local char_data = user_input("Alignment Character:", nill)
if char_data ~= nill then
local cursor = -1
-- remove the markings
MarkHide()
-- move to the fist line of the marked text
set_line_pos(top, left)
-- the last line number
local last = top + range - 1
-- calculate the maximum index value
for i = top, last, 1 do
local text = get_line_text(i)
local found = string.find(text, char_data, cursor_to_index(i, left), 0)
if (found ~= nil) then
-- look for the maximum cursor location
cursor = math.max(index_to_cursor(i, found), cursor)
-- message_box(0, text .. " : " .. found .. " f: " .. cursor)
end
end
if (cursor > 1) then
-- apply the padding to all the lines
for i = top, last, 1 do
text = get_line_text(i)
local found = string.find(text, char_data, cursor_to_index(i, left), 0)
if (found ~= nil) then
local cursor_found = index_to_cursor(i, found)
-- position the line and cursor location
set_line_pos(i, cursor_found)
-- add the pad required
print(string.rep(" ", cursor - cursor_found))
end
end
end
else
message("Operation cancelled by user.")
end
-- restore original cursor
cursor_restore()
screen_update_enable()
screen_update()
else
message("More than one line of text needs to be selected.")
beep();
end
end
key_macro() -- run the macro