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.
This commit is contained in:
Benno Schulenberg 2024-06-09 08:04:07 +02:00
parent d477bfd4ee
commit d1c723bc14
5 changed files with 31 additions and 0 deletions

View File

@ -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,

View File

@ -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)
{

View File

@ -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;

View File

@ -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

View File

@ -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"))