Code: Select all
+-+------+--------+----------+----------+
| | | | | |
+-+------+--------+----------+----------+
| | | | | |
+-+------+--------+----------+----------+
| | |
+----------+----------+
Code: Select all
--
-- Name: Box Drawing Macro
--
-- Author: Jussi Jumppanen
--
-- Language: Lua Macro
--
-- Description: This Lua macro will draw boxes uses the
-- numeric keypad.
--
-- +-----+-------------+------------+----+
-- | | | | |
-- +-----+-------------+------------+----+
-- | | | | |
-- +-----+-------------+------------+----+
-- | |
-- +----+
--
local minus = 45 -- '-'
local plus = 43 -- '+'
local vert = 124 -- '|'
-- key codes figured out using the sk.zm macro
local key_up = 2490368
local key_down = 2621440
local key_left = 2424832
local key_right = 2555904
function write_char(ch_value, line, cursor)
set_line_pos(line, cursor)
local ch_current = get_char_at(line, cursor)
-- don't overwrite the '+' symbol
if ch_current == plus then
write('+', 0)
else
write(ch_value, 0)
end
MoveLineLeft()
end
function key_macro()
-- macro only works for documents
local document = is_document()
if document > 0 then
-- the initial value
local nill = ""
local quit = 0
InsertModeReset()
--debug_enable ()
local char_fill_1
local char_fill_2
while quit == 0 do
local line = get_line_pos()
local cursor = get_cursor_pos()
local current = get_char_at(line, cursor)
local horz_next = get_char_at(line, cursor + 1)
local vert_next = get_char_at(line + 1, cursor)
local vert_prev = 0
local horz_prev = 0
if line > 1 then
vert_prev = get_char_at(line - 1, cursor)
end
if cursor > 1 then
horz_prev = get_char_at(line, cursor - 1)
end
message("Use the numeric keypad arrows to move the cursor and draw or hit any other key to cancel.")
-- get the user keyboard input
local key = keyboard_input()
local line_new = line
local cursor_new = cursor
-- left key
if key == key_left then
if current == vert or current == plus then
char_fill_1 = '+'
char_fill_2 = '-'
elseif horz_prev == vert or horz_prev == plus then
char_fill_1 = '-'
char_fill_2 = '+'
else
char_fill_1 = '-'
char_fill_2 = '-'
end
if horz_prev == plus or horz_prev == vert then
char_fill_2 = '+'
end
cursor_new = cursor - 1
-- right key
elseif key == key_right then
if current == vert or current == plus then
char_fill_1 = '+'
char_fill_2 = '-'
elseif horz_next == vert or horz_next == plus then
char_fill_1 = '-'
char_fill_2 = '+'
else
char_fill_1 = '-'
char_fill_2 = '-'
end
if horz_next == plus or horz_next == vert then
char_fill_2 = '+'
end
cursor_new = cursor + 1
-- up key
elseif key == key_up then
line_new = line - 1
cursor_new = cursor
if current == minus or current == plus then
char_fill_1 = '+'
char_fill_2 = '|'
elseif vert_prev == minus or vert_prev == plus then
char_fill_1 = '|'
char_fill_2 = '+'
else
char_fill_1 = '|'
char_fill_2 = '|'
end
if vert_prev == plus or vert_prev == minus then
char_fill_2 = '+'
end
-- down key
elseif key == key_down then
line_new = line + 1
cursor_new = cursor
if current == minus or current == plus then
char_fill_1 = '+'
char_fill_2 = '|'
elseif vert_next == minus or vert_next == plus then
char_fill_1 = '|'
char_fill_2 = '+'
else
char_fill_1 = '|'
char_fill_2 = '|'
end
if vert_next == plus or vert_next == minus then
char_fill_2 = '+'
end
else
quit = 1
end
if quit <= 0 then
write_char(char_fill_1, line, cursor)
if line_new <= 0 then
line_new = 1
end
if cursor_new <= 0 then
cursor_new = 1
end
write_char(char_fill_2, line_new, cursor_new)
end
end
InsertModeSet()
else
message("This macro only works for document files!")
beep()
end
end
key_macro() -- run the macro