Slightly extended bookmarks

Find Tips and tricks on how to better use the Zeus IDE. Feel free to post your own tips but please do not post bug reports, feature requests or questions here.
Post Reply
Michael

Slightly extended bookmarks

Post by Michael »

Hi,

I've written some simple scripts for a slightly extended bookmark handling. Using the scripts it's possible to toggle a bookmark for each line and cycle through them. As a side effect of having to use the global string database for storing the bookmarks, the bookmarks will be persistent even after restarting Zeus.
It may not be the most rock solid solution for each and every case but for me it's working well so far.

Save the code using the suggested filenames and map bookmarks_toggle.lua lua to ctrl-f2 and bookmarks_next.lua to f2 to get the commond handling used in most IDEs.

Regards,
Michael

Code: Select all

--
-- file bookmarks.lua
--
function bookmarks_read( name )
    name = name or "unknown"
    local tb = {}
    local s = get_global_string( name ) or ""
    local numlines = get_line_count()

    for bl in string.gfind( s, "%d+" ) do
        bl = tonumber(bl)
        if bl < numlines then
            table.insert( tb, bl )
        end
    end
    table.sort( tb )

    return tb
end


function bookmarks_write( name, tab )
    name = name or "unknown"
    local s = ""

    for idx, bl in pairs(tab) do
        s = s..bl.." "
    end

    set_global_string( name, s )
end


function bookmarks_next()
    local fname = get_file_name()
    local tab = bookmarks_read( fname )
    local line = get_line_pos()
    local numlines = get_line_count()

    for idx, bl in pairs(tab) do
        if bl > line then
            if bl < numlines then
                set_line_pos( bl )
                return
            end
        end
    end

    if tab[1] ~= nil then
        set_line_pos( tab[1] )
    end
end


function bookmarks_toggle()
    local fname = get_file_name()
    local tab = bookmarks_read( fname )
    local line = get_line_pos()

    local idxat = nil
    for idx, bl in pairs(tab) do
        if bl == line then
            idxat = idx
        end
    end

    if idxat ~= nil then
        table.remove( tab, idxat)
    else
        table.insert( tab, line )
        table.sort( tab )
    end

    bookmarks_write( fname, tab )
end

Code: Select all

-- file bookmarks_toggle.lua

dofile( "bookmarks.lua" )
bookmarks_toggle() -- run the macro

Code: Select all

-- file bookmarks_next.lua
dofile( "bookmarks.lua" )
bookmarks_next() -- run the macro
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

Hi Michael,

These macros are cool 8)

My only suggestion would be to add a little visual feedback to the macros. For example, I "** added" a small amount of user feedback code to the bookmarks_next and bookmarks_toggle functions found in your bookmarks.lua file:

Code: Select all

function bookmarks_next()
    local fname = get_file_name()
    local tab = bookmarks_read( fname )
    local line = get_line_pos()
    local numlines = get_line_count()

    for idx, bl in pairs(tab) do
        if bl > line then
            if bl < numlines then
                set_line_pos( bl )
                -- ** added: found bookmark
                message("Moved to bookmark at line ".. bl)
                return
            end
        end
    end

    -- ** added: default message
    local message_text = "No bookmarks found for this file!"

    if tab[1] ~= nil then
        set_line_pos( tab[1] )
        -- ** added: found bookmark
        message_text = "Moved to bookmark at line ".. tab[1]
    end
    -- ** added: display message
    message(message_text)
end


function bookmarks_toggle()
    local fname = get_file_name()
    local tab = bookmarks_read( fname )
    local line = get_line_pos()

    local idxat = nil
    for idx, bl in pairs(tab) do
        if bl == line then
            idxat = idx
        end
    end


    if idxat ~= nil then
        table.remove( tab, idxat)
        -- ** added: display message
        message("Book mark removed from line ".. line)
    else
        table.insert( tab, line )
        table.sort( tab )
        -- ** added: display message
        message("Book mark dropped at line ".. line)
    end

    bookmarks_write( fname, tab )
end
Post Reply