Code

list_window: Added scroll up/down commands
authorJeffrey Middleton <jefromi@gmail.com>
Thu, 5 Mar 2009 22:43:02 +0000 (16:43 -0600)
committerJeffrey Middleton <jefromi@gmail.com>
Thu, 5 Mar 2009 19:15:24 +0000 (13:15 -0600)
Commands scroll a list window up/down by one line, mapped by default to
Ctrl-N and Ctrl-B (mnemonics next and back).  The scrolling functions can
scroll by an arbitrary amount, so we can use them later for half-screen scroll
commands like Vim's Ctrl-D/U.

src/command.c
src/command.h
src/list_window.c
src/screen_help.c

index 3a16e801409ffd09d8cdcc9b82472515d4335c17..7182d168689a9eef59c998676bc1eadee4e69ee1 100644 (file)
@@ -92,6 +92,10 @@ static command_definition_t cmds[] = {
          N_("Page down") },
        { { 'v',  0, 0 }, 0, CMD_LIST_VISUAL_SELECT, "visual-select",
          N_("Visual selection") },
+       { { 14,  0, 0 }, 0, CMD_LIST_SCROLL_DOWN_LINE, "scroll-down-line",
+         N_("Scroll up one line") },
+       { { 2,  0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE, "scroll-up-line",
+         N_("Scroll down one line") },
 
 
        /* basic screens */
index c7514c421a4159eb30cd1e5f9e19d42303558c90..abfca4401ab99183bc741db7cf313be75e2985cd 100644 (file)
@@ -74,6 +74,8 @@ typedef enum {
        CMD_LIST_MOVE_UP,
        CMD_LIST_MOVE_DOWN,
        CMD_LIST_VISUAL_SELECT,
+       CMD_LIST_SCROLL_UP_LINE,
+       CMD_LIST_SCROLL_DOWN_LINE,
        CMD_MOUSE_EVENT,
        CMD_SCREEN_UPDATE,
        CMD_SCREEN_PREVIOUS,
index 8d1a2d577b18fbe0753e36b6f804da599ed89a1f..a127a5b07b93bdde85302d638670b488e42c6269 100644 (file)
@@ -221,6 +221,56 @@ list_window_previous_page(struct list_window *lw)
                list_window_first(lw);
 }
 
+static void
+list_window_scroll_up(struct list_window *lw, unsigned n)
+{
+       if (lw->start > 0) {
+               if (n > lw->start)
+                       lw->start = 0;
+               else
+                       lw->start -= n;
+               if (lw->selected > lw->start + lw->rows - 1) {
+                       lw->selected = lw->start + lw->rows - 1;
+                       if (lw->visual_selection) {
+                               if (lw->selected < lw->visual_base) {
+                                       lw->selected_start = lw->selected;
+                                       lw->selected_end = lw->visual_base;
+                               } else {
+                                       lw->selected_end = lw->selected;
+                               }
+                       } else {
+                               lw->selected_start = lw->selected;
+                               lw->selected_end = lw->selected;
+                       }
+               }
+       }
+}
+
+static void
+list_window_scroll_down(struct list_window *lw, unsigned length, unsigned n)
+{
+       if (lw->start + lw->rows < length - 1)
+       {
+               if ( lw->start + lw->rows + n > length - 1)
+                       lw->start = length-1;
+               else
+                       lw->start += n;
+               if (lw->selected < lw->start) {
+                       lw->selected = lw->start;
+                       if (lw->visual_selection) {
+                               if (lw->selected > lw->visual_base) {
+                                       lw->selected_end = lw->selected;
+                                       lw->selected_start = lw->visual_base;
+                               } else {
+                                       lw->selected_start = lw->selected;
+                               }
+                       } else {
+                               lw->selected_start = lw->selected;
+                               lw->selected_end = lw->selected;
+                       }
+               }
+       }
+}
 
 void
 list_window_paint(struct list_window *lw,
@@ -434,6 +484,12 @@ list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
                        lw->visual_selection = true;
                }
                break;
+       case CMD_LIST_SCROLL_UP_LINE:
+               list_window_scroll_up(lw, 1);
+               break;
+       case CMD_LIST_SCROLL_DOWN_LINE:
+               list_window_scroll_down(lw, rows, 1);
+               break;
        default:
                return false;
        }
index 5a0654fb3b6d3df9482a54a947d61b49db893151..e731664f66898f2ae5ecf644b6c4ff7546bddf6a 100644 (file)
@@ -47,6 +47,8 @@ static help_text_row_t help_text[] = {
        { 0, CMD_LIST_FIRST, NULL },
        { 0, CMD_LIST_LAST, NULL },
        { 0, CMD_LIST_VISUAL_SELECT, NULL },
+       { 0, CMD_LIST_SCROLL_UP_LINE, NULL},
+       { 0, CMD_LIST_SCROLL_DOWN_LINE, NULL},
        { 0, CMD_NONE, NULL },
        { 0, CMD_SCREEN_PREVIOUS,NULL },
        { 0, CMD_SCREEN_NEXT, NULL },