Skip to content

Commit b6aeb80

Browse files
committed
Map ctrl-e, ctrl-y in normal mode to scroll down/up one line
ctrl-e: scroll one line down (https://vimhelp.org/scroll.txt.html#CTRL-E) ctrl-y: scroll one line up (https://vimhelp.org/scroll.txt.html#CTRL-Y) Difference between j/k: - cursor stays stationary, text around cursor shifts one line at a time - if scrolling would move the cursor outside of the buffer's viewport, the cursor is moved to stay in the viewport
1 parent 9e6f6a5 commit b6aeb80

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/normal.cc

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ constexpr auto enum_desc(Meta::Type<SelectMode>)
5050
});
5151
}
5252

53+
enum class ScrollMode
54+
{
55+
Page,
56+
HalfPage,
57+
Line,
58+
};
59+
5360
void merge_selections(Selection& sel, const Selection& new_sel)
5461
{
5562
const bool forward = sel.cursor() >= sel.anchor();
@@ -1590,12 +1597,20 @@ void select_object(Context& context, NormalParams params)
15901597
return select_object(context, params, flags, mode);
15911598
}
15921599

1593-
template<Direction direction, bool half = false>
1600+
template<Direction direction, ScrollMode mode = ScrollMode::Page>
15941601
void scroll(Context& context, NormalParams params)
15951602
{
15961603
const Window& window = context.window();
15971604
const int count = params.count ? params.count : 1;
1598-
const LineCount offset = (window.dimensions().line - 2) / (half ? 2 : 1) * count;
1605+
1606+
const LineCount offset = [](const Window& window, const int count) {
1607+
if constexpr (mode == ScrollMode::Page)
1608+
return (window.dimensions().line - 2) / 1 * count;
1609+
else if constexpr (mode == ScrollMode::HalfPage)
1610+
return (window.dimensions().line - 2) / 2 * count;
1611+
else
1612+
return count;
1613+
}(window, count);
15991614

16001615
scroll_window(context, offset * direction, OnHiddenCursor::MoveCursorAndAnchor);
16011616
}
@@ -2625,10 +2640,12 @@ static constexpr HashMap<Key, NormalCmd, MemoryDomain::Undefined, KeymapBackend>
26252640
{ {Key::PageUp}, { "scroll one page up", scroll<Backward>} },
26262641
{ {Key::PageDown}, {"scroll one page down", scroll<Forward>} },
26272642

2628-
{ {ctrl('b')}, {"scroll one page up", scroll<Backward >} },
2643+
{ {ctrl('b')}, {"scroll one page up", scroll<Backward>} },
26292644
{ {ctrl('f')}, {"scroll one page down", scroll<Forward>} },
2630-
{ {ctrl('u')}, {"scroll half a page up", scroll<Backward, true>} },
2631-
{ {ctrl('d')}, {"scroll half a page down", scroll<Forward, true>} },
2645+
{ {ctrl('u')}, {"scroll half a page up", scroll<Backward, ScrollMode::HalfPage>} },
2646+
{ {ctrl('d')}, {"scroll half a page down", scroll<Forward, ScrollMode::HalfPage>} },
2647+
{ {ctrl('y')}, {"scroll one line up", scroll<Backward, ScrollMode::Line>} },
2648+
{ {ctrl('e')}, {"scroll one line down", scroll<Forward, ScrollMode::Line>} },
26322649

26332650
{ {'z'}, {"restore selections from register", restore_selections<false>} },
26342651
{ {alt('z')}, {"combine selections from register", restore_selections<true>} },

0 commit comments

Comments
 (0)