From 1a2d0c17705d6b7fc347bc27b10fdc08a4518c48 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 27 Apr 2016 15:10:06 +0200 Subject: [PATCH] scrolling: add a function and a key binding to center the cursor line By default, this function is bound to ^L, to make that keystroke do something actually useful. To not lose the Refresh function that this keystroke had, the centering function additionally does a full redraw and refresh of the screen. --- doc/nano.texi | 3 +++ doc/nanorc.5 | 3 +++ src/global.c | 10 +++++++++- src/move.c | 10 +++++++++- src/prototypes.h | 1 + src/rcfile.c | 2 ++ 6 files changed, 27 insertions(+), 2 deletions(-) diff --git a/doc/nano.texi b/doc/nano.texi index 0d56ec7e..350c6c47 100644 --- a/doc/nano.texi +++ b/doc/nano.texi @@ -1370,6 +1370,9 @@ while keeping the cursor in the same text position, if possible. Scrolls the viewport down one row (meaning that the text slides up) while keeping the cursor in the same text position, if possible. +@item center +Scrolls the line with the cursor to the middle of the screen. + @item prevword Moves the cursor to the beginning of the previous word. diff --git a/doc/nanorc.5 b/doc/nanorc.5 index d79fb52b..48df4cb6 100644 --- a/doc/nanorc.5 +++ b/doc/nanorc.5 @@ -674,6 +674,9 @@ while keeping the cursor in the same text position, if possible. Scrolls the viewport down one row (meaning that the text slides up) while keeping the cursor in the same text position, if possible. .TP +.B center +Scrolls the line with the cursor to the middle of the screen. +.TP .B prevword Moves the cursor to the beginning of the previous word. .TP diff --git a/src/global.c b/src/global.c index 18d983e1..3c311229 100644 --- a/src/global.c +++ b/src/global.c @@ -622,6 +622,7 @@ void shortcut_init(void) N_("Scroll up one line without moving the cursor textually"); const char *scrolldown_gist = N_("Scroll down one line without moving the cursor textually"); + const char *center_gist = N_("Center the line where the cursor is"); #endif #ifdef ENABLE_MULTIBUFFER const char *prevfile_gist = N_("Switch to the previous file buffer"); @@ -1035,7 +1036,11 @@ void shortcut_init(void) N_("Formatter"), WITHORSANS(formatter_gist), BLANKAFTER, NOVIEW); #endif } -#endif /* NANO_TINY */ +#endif /* !NANO_TINY */ +#if !defined(NANO_TINY) || defined(ENABLE_HELP) + add_to_funcs(do_center, MMAIN, + N_("Center"), WITHORSANS(center_gist), BLANKAFTER, VIEW); +#endif add_to_funcs(do_savefile, MMAIN, N_("Save"), WITHORSANS(savefile_gist), BLANKAFTER, NOVIEW); @@ -1322,6 +1327,9 @@ void shortcut_init(void) #endif #ifdef ENABLE_JUSTIFY add_to_sclist(MEXECUTE, "^J", 0, do_full_justify, 0); +#endif +#if !defined(NANO_TINY) || defined(ENABLE_HELP) + add_to_sclist(MMAIN, "^L", 0, do_center, 0); #endif if (!ISSET(PRESERVE)) add_to_sclist(MMOST|MBROWSER|MHELP|MYESNO, "^L", 0, full_refresh, 0); diff --git a/src/move.c b/src/move.c index 214b2692..5f31b313 100644 --- a/src/move.c +++ b/src/move.c @@ -562,7 +562,15 @@ void do_scroll_down(void) )) edit_scroll(FORWARD); } -#endif + +/* Scroll the line with the cursor to the center of the screen. */ +void do_center(void) +{ + adjust_viewport(CENTERING); + draw_all_subwindows(); + full_refresh(); +} +#endif /* !NANO_TINY || ENABLE_HELP */ /* Move left one character. */ void do_left(void) diff --git a/src/prototypes.h b/src/prototypes.h index 36904ffa..93377b78 100644 --- a/src/prototypes.h +++ b/src/prototypes.h @@ -386,6 +386,7 @@ void do_down(void); #if !defined(NANO_TINY) || defined(ENABLE_HELP) void do_scroll_up(void); void do_scroll_down(void); +void do_center(void); #endif void do_left(void); void do_right(void); diff --git a/src/rcfile.c b/src/rcfile.c index 6198960b..822674fc 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -348,6 +348,8 @@ keystruct *strtosc(const char *input) s->func = do_scroll_up; else if (!strcmp(input, "scrolldown")) s->func = do_scroll_down; + else if (!strcmp(input, "center")) + s->func = do_center; #endif else if (!strcmp(input, "prevword")) s->func = to_prev_word;