Box Drawing Macro

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: 2443
Joined: Fri Aug 13, 2004 5:10 pm

Box Drawing Macro

Post by jussij » Sun Jun 28, 2009 11:57 pm

This macro can be used to create boxes like these:

Code: Select all

+-+------+--------+----------+----------+
| |      |        |          |          |
+-+------+--------+----------+----------+
| |      |        |          |          |
+-+------+--------+----------+----------+
                  |          |          |
                  +----------+----------+
Here is the script:

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
Last edited by jussij on Wed Mar 06, 2013 1:48 am, edited 2 times in total.

amix
Posts: 49
Joined: Wed Aug 22, 2007 1:26 pm

Post by amix » Wed Sep 30, 2009 10:18 am

Great! I used to have a little setup (scripts and toolbars) on my old text-editor (GoldED for AmigaOS), that would do ASCII drawing, so I could create diagrams for UseNet and Email. Nice macro!

jussij
Site Admin
Posts: 2443
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij » Sat Oct 24, 2009 3:21 am

ditaa - http://ditaa.sourceforge.net/

A tool that converts box diagrams into html images.

sfeldner
Posts: 13
Joined: Wed Sep 24, 2008 3:44 pm

Box drawing using ASCII Box Drawing characters

Post by sfeldner » Wed Nov 28, 2012 9:52 pm

Hey Jussi,
Have you ever considered changing your Box Drawing Macro to use the ASCII box drawing characters?

Code: Select all

▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀■
Years ago I had a DOS program (Form Maker or some such name.) that would draw forms to be printed using the keys just like you are and those characters. I’d love to be able to use them in files just like you use your simpler boxes.

Right now I can’t find a way to enter any of them via the Alt-Keypad method because they are all over ASCII 126 decimal but yet I can insert them using Windows Character Map – which only lists them as Unicode Hex character.

jussij
Site Admin
Posts: 2443
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij » Thu Nov 29, 2012 5:45 am

Hi Steve,
Have you ever considered changing your Box Drawing Macro to use the ASCII box drawing characters?
Unfortunately, doing the box drawing using the extended DOS character set is a lot harder :(

If you look at the Lua code that does the box drawing, it gets away with only having to deal with 3 drawing characters, making the macro very easy to write.

Code: Select all

local minus = 45   -- '-'
local plus  = 43   -- '+'
local vert  = 124  -- '|'
To do the drawing using the DOS character set this would expand to having to use 11 characters (4 corners, vertical, horizontal and 5 intersection) for the drawing, making the macro much more complex.
Right now I can’t find a way to enter any of them via the Alt-Keypad method
I think this is a bug/feature of Zeus caused by the fact that Zeus also takes an interest in the Alt key :(

For example I suspect the Alt keypad entry would work in Notepad.

Cheers Jussi

Post Reply
kuchnie warszawa opinie

Who is online

Users browsing this forum: No registered users and 1 guest