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

Line Folding Macro

Post by jussij »

The Lua macro below allows the user to fold away certain lines.

The macro works by asking the user for a search string and then any line that contains the search string which is also a fold point will have its fold closed.

Cheers Jussi

Code: Select all

--
--        Name: Line Folding Macro
--
--      Author: Jussi Jumppanen
--
--    Language: Lua Macro
--
-- Description: This Lua macro will prompt the user search string and
--              close any line fold points that contain that string.
--

function key_macro()
    -- macro only works for documents
    local document = is_document()

    if document > 0 then
        -- get the last search string
        local last_search = get_global_string("FIND_TEXT", true)
    
        -- ask for a user supplied decimal number
        local pattern = user_input("Search Pattern:", last_search)
    
        if string.len(pattern) > 0 then
            local FOLD_BEGIN = 2
    
            -- save the last search string
            set_global_string("FIND_TEXT", pattern, true)
    
            -- disable screen updates
            screen_update_disable()
    
            -- save the current cursor
            cursor_save()
    
            -- open up all fold points
            FoldingOpenAll()
    
            local line_total = get_line_count()
    
            -- search the whole file for the pattern
            for i = 1, line_total, 1 do

                -- get the line text
                local line = get_line_text(i)
    
                if ((string.len(line) > 0) and (string.find(line, pattern) ~= nil)) then
                    -- move to the line found
                    set_line_pos(i)
    
                    -- check for a fold point
                    local fold_style = get_line_folding(i)
    
                    if fold_style == FOLD_BEGIN then
                        -- close the begin fold found
                        FoldingLineClose()
                    end
                end
            end
    
            -- restore cursor
            cursor_restore()
    
            -- restore screen updates
            screen_update_enable()
    
            -- user feedback
            message("Action completed for pattern: '" .. pattern .. "")
        else
            message("Operation cancelled by user.")
            beep()
        end
    else
        message("This macro only works for document files!")
        beep()
    end
end

key_macro() -- run the macro
Post Reply