Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recenter-top-bottom (emacs-mode) #1767

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/commands/window.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@
(redraw-display)
t)

(define-command recenter-top-bottom (p) (:universal-nil)
"Scroll so that the cursor is in the middle/top/bottom."
(clear-screens-of-window-list)
(unless p (window-recenter-top-bottom (current-window)))
(redraw-display)
t)

(define-command split-active-window-vertically (&optional n) (:universal-nil)
"Split the current window vertically."
(split-window-vertically (current-window) :height n)
Expand Down
1 change: 1 addition & 0 deletions src/internal-packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@
;; virtual-line
(:export
:window-recenter
:window-recenter-top-bottom
:window-cursor-x
:window-cursor-y
:backward-line-wrap
Expand Down
14 changes: 14 additions & 0 deletions src/window/virtual-line.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ If FROM-BOTTOM is T, start counting from the bottom."
(window-scroll window n)
n)))

(defun window-recenter-top-bottom (window)
"In first call recenter WINDOW to the middle line.
If cursor is already in the middle of WINDOW then move cursor in the top position.
If cursor is on top then move move WINDOW to the bottom."
(let* ((line (window-cursor-y window))
(window-height (window-height-without-modeline window))
(middle (floor window-height 2))
(scrolloff (min (floor (/ (window-height window) 2)) 0))
(top 0))
(cond
((= line middle) (window-recenter window :line scrolloff :from-bottom nil))
((= line top) (window-recenter window :line scrolloff :from-bottom t))
(t (window-recenter window :line nil :from-bottom nil)))))

(defun %calc-window-cursor-x (point window)
"Return (values cur-x next). the 'next' is a flag if the cursor goes to
next line because it is at the end of width."
Expand Down