[hackers] [scroll] Obey line length while scrolling || Jochen Sprickerhof

From: <git_AT_suckless.org>
Date: Wed, 29 Apr 2020 23:51:23 +0200 (CEST)

commit 247a9b042655291ab64aa9c43f9e89d820e932ee
Author: Jochen Sprickerhof <git_AT_jochen.sprickerhof.de>
AuthorDate: Wed Apr 29 23:46:11 2020 +0200
Commit: Jochen Sprickerhof <git_AT_jochen.sprickerhof.de>
CommitDate: Wed Apr 29 23:50:36 2020 +0200

    Obey line length while scrolling

diff --git a/scroll.c b/scroll.c
index 26b0527..9242936 100644
--- a/scroll.c
+++ b/scroll.c
_AT_@ -295,7 +295,7 @@ redraw()
 void
 scrollup(int n)
 {
- int rows = 2, x, y;
+ int rows = 2, x, y, extra = 0;
         struct line *scrollend = bottom;
 
         if (bottom == NULL)
_AT_@ -306,18 +306,24 @@ scrollup(int n)
         if (n < 0) /* scroll by fraction of ws.ws_row, but at least one line */
                 n = ws.ws_row > (-n) ? ws.ws_row / (-n) : 1;
 
- /* wind back scrollend pointer by one page plus n */
- for (; TAILQ_NEXT(scrollend, entries) != NULL &&
- rows < y + n; rows++)
+ /* wind back scrollend pointer by the current screen */
+ while (rows < y && TAILQ_NEXT(scrollend, entries) != NULL) {
                 scrollend = TAILQ_NEXT(scrollend, entries);
-
- rows -= y;
+ rows += (scrollend->len - 1) / ws.ws_col + 1;
+ }
 
         if (rows <= 0)
                 return;
 
+ /* wind back scrollend pointer n lines */
+ for (rows = 0; rows + extra < n &&
+ TAILQ_NEXT(scrollend, entries) != NULL; rows++) {
+ scrollend = TAILQ_NEXT(scrollend, entries);
+ extra += (scrollend->len - 1) / ws.ws_col;
+ }
+
         /* move the text in terminal rows lines down */
- dprintf(STDOUT_FILENO, "\033[%dT", rows);
+ dprintf(STDOUT_FILENO, "\033[%dT", n);
         /* set cursor position to upper left corner */
         write(STDOUT_FILENO, "\033[0;0H", 6);
         /* hide cursor */
_AT_@ -357,9 +363,10 @@ scrolldown(char *buf, size_t size, int n)
 
         bottom = TAILQ_PREV(bottom, tailhead, entries);
         /* print n lines */
- for (; n > 0 && bottom != NULL && bottom != TAILQ_FIRST(&head); n--) {
+ while (n > 0 && bottom != NULL && bottom != TAILQ_FIRST(&head)) {
                 bottom = TAILQ_PREV(bottom, tailhead, entries);
                 write(STDOUT_FILENO, bottom->buf, bottom->size);
+ n -= (bottom->len - 1) / ws.ws_col + 1;
         }
         if (n > 0 && bottom == TAILQ_FIRST(&head)) {
                 write(STDOUT_FILENO, "\033[?25h", 6); /* show cursor */
Received on Wed Apr 29 2020 - 23:51:23 CEST

This archive was generated by hypermail 2.3.0 : Thu Apr 30 2020 - 00:00:46 CEST