Skip to content

GotoNearestOccurrence

mitchell edited this page Dec 12, 2020 · 2 revisions

Summary

This is basically a really quick "Find Next". Calling this function will take you to the next (or previous) occurrence of the word under the cursor.

The code

function goto_nearest_occurrence(reverse)
        local buffer = buffer
        local s, e = buffer.selection_start, buffer.selection_end
        if s == e then
                s, e = buffer:word_start_position(s), buffer:word_end_position(s)
        end
        local word = buffer:text_range(s, e)
        if word == '' then return end
        buffer.search_flags = buffer.FIND_WHOLEWORD + buffer.FIND_MATCHCASE
        if reverse then
                buffer.target_start = s - 1
                buffer.target_end = 0
        else
                buffer.target_start = e + 1
                buffer.target_end = buffer.length + 1
        end
        if buffer:search_in_target(word) == -1 then
                if reverse then
                        buffer.target_start = buffer.length + 1
                        buffer.target_end = e + 1
                else
                        buffer.target_start = 0
                        buffer.target_end = s - 1
                end
                if buffer:search_in_target(word) == -1 then return end
        end
        buffer:set_sel(buffer.target_start, buffer.target_end)
end

keys['ctrl+k'] = function() goto_nearest_occurrence(false) end
keys['ctrl+K'] = function() goto_nearest_occurrence(true) end
Clone this wiki locally