echasnovski / mini.nvim

Library of 40+ independent Lua modules improving overall Neovim (version 0.8 and higher) experience with minimal effort
MIT License
4.54k stars 175 forks source link

add motion that move cursor to first/last line of the paragraph? #1014

Closed aidancz closed 1 week ago

aidancz commented 1 week ago

Contributing guidelines

Module(s)


Description

the { and } motion move cursor to blank line

but sometimes it's useful to move cursor to the first/last non-blank line of the paragraph (e.g. when using blockwise-visual mode)

Vim: How to jump to first/last line of the current paragraph?

i hope that mini can add this feature

maybe mini.motions? :)

here is how I implemented it:

local function paragraph_border_line_p(lnum)
    if lnum == 1 then
        return true
    end
    if lnum == vim.fn.line('$') then
        return true
    end
    if vim.fn.getline(lnum) ~= '' and vim.fn.getline(lnum - 1) == '' then
        return true
    end
    if vim.fn.getline(lnum) ~= '' and vim.fn.getline(lnum + 1) == '' then
        return true
    end
    return false
end

function paragraph_border_backward_lnum(lnum)
    if lnum == 1 then
        return lnum
    end
    if paragraph_border_line_p(lnum - 1) then
        return lnum - 1
    end
    return paragraph_border_backward_lnum(lnum - 1)
end

function paragraph_border_forward_lnum(lnum)
    if lnum == vim.fn.line('$') then
        return lnum
    end
    if paragraph_border_line_p(lnum + 1) then
        return lnum + 1
    end
    return paragraph_border_forward_lnum(lnum + 1)
end

function paragraph_border_backward()
    local line_number_current = vim.fn.line('.')
    local line_number_destination = paragraph_border_backward_lnum(line_number_current)
    vim.cmd(tostring(line_number_destination))
end

function paragraph_border_forward()
    local line_number_current = vim.fn.line('.')
    local line_number_destination = paragraph_border_forward_lnum(line_number_current)
    vim.cmd(tostring(line_number_destination))
end

vim.keymap.set({'n', 'v'}, '(', paragraph_border_backward)
vim.keymap.set('o', '(', ':normal V(<cr>', {silent = true})
vim.keymap.set({'n', 'v'}, ')', paragraph_border_forward)
vim.keymap.set('o', ')', ':normal V)<cr>', {silent = true})
echasnovski commented 1 week ago

Thanks for the suggestion!

Unfortunately, I don't think there will be a good place for this in 'mini.nvim'.

There is no plans for 'mini.motions' or something like it. Maybe if there are more non-trivial motions that can be fit in the same module nicely.

Closing as "probably not planned, but will keep in mind".