🧋 a tiny plugin for smoother movement keybinds
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add zz align

robin 71c73041 9a08a42f

+49
+49
plugin/smoothie.lua
··· 96 96 end) 97 97 end 98 98 99 + function M.align() 100 + local win = vim.api.nvim_get_current_win() 101 + 102 + local function step(delta, m, start, dst) 103 + local diff = dst.row - start.row 104 + 105 + -- reached dst or overshot 106 + -- - diff == 0: no more distance to travel 107 + -- - diff * m: both need to be oriented in the same direction (positive product) 108 + if diff == 0 or diff * m < 0 then 109 + return true 110 + end 111 + 112 + -- travel t of the current diff (ceiled), at least 1 113 + local s = math.max(1, math.ceil(H.coillerp(math.abs(diff), delta))) 114 + start.row = start.row + (m * s) 115 + 116 + vim._with({ win = win }, function() 117 + for _ = 1, s do 118 + local key = vim.api.nvim_replace_termcodes(m > 0 and "<c-y>" or "<c-e>", true, false, true) 119 + vim.api.nvim_feedkeys(key, "n", false) 120 + end 121 + end) 122 + end 123 + 124 + M.smooth(win, step, function() 125 + local buf, lnum, col = unpack(vim.fn.getpos(".")) 126 + local start = vim.pos.cursor(buf, { lnum, col }) 127 + 128 + local m 129 + local wfirst = vim.fn.getpos("w0")[2] 130 + local wlast = vim.fn.getpos("w$")[2] 131 + -- no lines are visible 132 + if wlast < wfirst then 133 + m = 0 134 + end 135 + local dst = vim.pos.cursor(buf, { math.ceil(wfirst + (wlast - wfirst) / 2), col }) 136 + local diff = dst.row - start.row 137 + m = m or diff / math.abs(diff) 138 + 139 + return { m, start, dst } 140 + end) 141 + end 142 + 99 143 vim.keymap.set("n", "<plug>(smoothie-ctrl-d)", function() 100 144 vim._with({ win = 0 }, function() 101 145 M.scroll(vim.v.count1) ··· 104 148 vim.keymap.set("n", "<plug>(smoothie-ctrl-u)", function() 105 149 vim._with({ win = 0 }, function() 106 150 M.scroll(-vim.v.count1) 151 + end) 152 + end) 153 + vim.keymap.set("n", "<plug>(smoothie-zz)", function() 154 + vim._with({ win = 0 }, function() 155 + M.align() 107 156 end) 108 157 end) 109 158