commit d1c723bc149566e9c4d039b99b380c39439e6262
parent d477bfd4eeb9bea2ff1351d43bc1a0db02dd8c60
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Sun, 9 Jun 2024 08:04:07 +0200
new feature: add bindable function `cycle` that pushes cursor line around
On the first call, `cycle` centers the line with the cursor, on the
second consecutive call it pushes that line to the top of the viewport,
and on the third consecutive call to the bottom of the viewport.
Diffstat:
5 files changed, 31 insertions(+), 0 deletions(-)
diff --git a/src/global.c b/src/global.c
@@ -139,6 +139,9 @@ int *bardata = NULL;
/* An array of characters that together depict the scrollbar. */
ssize_t stripe_column = 0;
/* The column at which a vertical bar will be drawn. */
+int cycling_aim = 0;
+ /* Whether to center the line with the cursor (0), push it
+ * to the top of the viewport (1), or to the bottom (2). */
#endif
linestruct *cutbuffer = NULL;
@@ -608,6 +611,7 @@ void shortcut_init(void)
const char *toprow_gist = N_("Go to first row in the viewport");
const char *bottomrow_gist = N_("Go to last row in the viewport");
const char *center_gist = N_("Center the line where the cursor is");
+ const char *cycle_gist = N_("Push the cursor line to the center, then top, then bottom");
#endif
const char *prevpage_gist = N_("Go one screenful up");
const char *nextpage_gist = N_("Go one screenful down");
@@ -1080,6 +1084,8 @@ void shortcut_init(void)
#ifndef NANO_TINY
add_to_funcs(do_center, MMAIN,
N_("Center"), WHENHELP(center_gist), BLANKAFTER);
+ add_to_funcs(do_cycle, MMAIN,
+ N_("Cycle"), WHENHELP(cycle_gist), BLANKAFTER);
#endif
add_to_funcs(do_savefile, MMAIN,
diff --git a/src/move.c b/src/move.c
@@ -208,6 +208,22 @@ void to_bottom_row(void)
place_the_cursor();
}
+/* Put the cursor line at the center, then the top, then the bottom. */
+void do_cycle(void)
+{
+ if (cycling_aim == 0)
+ adjust_viewport(CENTERING);
+ else {
+ openfile->cursor_row = (cycling_aim == 1) ? 0 : editwinrows - 1;
+ adjust_viewport(STATIONARY);
+ }
+
+ cycling_aim = (cycling_aim + 1) % 3;
+
+ draw_all_subwindows();
+ full_refresh();
+}
+
/* Scroll the line with the cursor to the center of the screen. */
void do_center(void)
{
diff --git a/src/nano.c b/src/nano.c
@@ -1643,6 +1643,11 @@ void process_a_keystroke(void)
depth = 0;
}
+#ifndef NANO_TINY
+ if (function != do_cycle)
+ cycling_aim = 0;
+#endif
+
if (!function) {
pletion_line = NULL;
keep_cutbuffer = FALSE;
diff --git a/src/prototypes.h b/src/prototypes.h
@@ -98,6 +98,7 @@ extern int sidebar;
#ifndef NANO_TINY
extern int *bardata;
extern ssize_t stripe_column;
+extern int cycling_aim;
#endif
extern linestruct *cutbuffer;
@@ -366,6 +367,7 @@ void do_page_down(void);
#ifndef NANO_TINY
void to_top_row(void);
void to_bottom_row(void);
+void do_cycle(void);
void do_center(void);
#endif
#ifdef ENABLE_JUSTIFY
diff --git a/src/rcfile.c b/src/rcfile.c
@@ -362,6 +362,8 @@ keystruct *strtosc(const char *input)
s->func = to_bottom_row;
else if (!strcmp(input, "center"))
s->func = do_center;
+ else if (!strcmp(input, "cycle"))
+ s->func = do_cycle;
#endif
else if (!strcmp(input, "pageup") ||
!strcmp(input, "prevpage"))