Add rudimentary buffer scrolling when cursor leaves window

Squashed in fix for crash when only moving one line
This commit is contained in:
Skylar Hill 2023-11-08 02:11:14 -06:00
parent 29c5c9b6a6
commit 8fbeeccfdb
3 changed files with 37 additions and 12 deletions

View File

@ -5,12 +5,16 @@
get-main-win
set-cursor-pos
end
width
height
write-buffer))
(define-class <sloth-frontend> ()
(main-win #:getter get-main-win #:init-keyword #:main-win))
(main-win #:getter get-main-win #:init-keyword #:main-win)
(height #:accessor height)
(width #:accessor width))
(define-generic fetch-input) ; args: <sloth-frontend>
(define-generic set-cursor-pos) ; args: <sloth-frontend> x y
(define-generic set-cursor-pos) ; args: <sloth-frontend> x y scroll line
(define-generic end) ; args: <sloth-frontend>
(define-generic write-buffer) ; args: <sloth-frontend> buffer lines-scrolled
(define-generic write-buffer) ; args: <sloth-frontend> buffer scroll

View File

@ -123,9 +123,20 @@
(if relative?
(+ y (cury state))
y)))
(define scroll (lines-scrolled state))
(define screen-height (height (get-frontend state)))
(define relative-y (- (cury state) scroll))
(if (>= relative-y
screen-height)
(set! (lines-scrolled state) (cury state)))
(if (< relative-y 0)
(set! (lines-scrolled state)
(max 0 (- (cury state) screen-height))))
(set-cursor-pos (get-frontend state)
(curx state)
(cury state)))
(cury state)
(lines-scrolled state)
(list-ref (buffer state) (cury state))))
(define (string-insert s ch k)
(string-append

View File

@ -1,7 +1,8 @@
(define-module (sloth ncurses)
#:use-module (ncurses curses)
#:use-module (sloth interface)
#:use-module (sloth editor)
#:use-module (sloth common)
#:use-module (sloth interface)
#:use-module (oop goops)
#:export (init-ncurses))
@ -12,6 +13,11 @@
(curx #:init-value 0 #:accessor curx)
(cury #:init-value 0 #:accessor cury))
(define-method (initialize (nc <ncurses-frontend>) . initargs)
(set! (height nc) (lines))
(set! (width nc) (cols))
(next-method))
(define (init-ncurses)
(define win (initscr))
(define state (make <ncurses-frontend>
@ -278,22 +284,26 @@
((#\/) 'm-/)
((#\?) 'm-?)))))))))
(define-method (set-cursor-pos (nc <ncurses-frontend>) x y)
(set! (curx nc) x)
(set! (cury nc) y)
(move (get-main-win nc) y x))
(define-method (set-cursor-pos (nc <ncurses-frontend>) x y scroll line)
(define y-real (max 0 (- y scroll)))
(define tabcount (string-count line #\tab))
(define x-real (+ (- x tabcount)
(* (tabsize) tabcount)))
(set! (curx nc) x-real)
(set! (cury nc) y-real)
(move (get-main-win nc) y-real x-real))
(define-method (end (nc <ncurses-frontend>))
(endwin)
(quit))
(define-method (write-buffer (nc <ncurses-frontend>) buffer lines-scrolled)
(define-method (write-buffer (nc <ncurses-frontend>) buffer scroll)
(define win (get-main-win nc))
(erase win)
(do ((i 0 (1+ i)))
((or (> i (lines))
(>= (+ i lines-scrolled) (length buffer))))
(addstr win (list-ref buffer (+ i lines-scrolled))
(>= (+ i scroll) (length buffer))))
(addstr win (list-ref buffer (+ i scroll))
#:y i #:x 0))
(move win (cury nc) (curx nc))
(refresh win))