Skip to content

Commit

Permalink
feature: recenter line if jump target point is not inside window. (vi…
Browse files Browse the repository at this point in the history
…-mode)
  • Loading branch information
sakurawald committed Dec 9, 2024
1 parent c44ffde commit 0472a32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 9 additions & 2 deletions extensions/vi-mode/commands.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,18 +1019,25 @@ on the same line or at eol if there are none."
(line-start p))))))
(move-to-column (current-point) column t)))

(defun recenter-line-if-point-not-inside-window (point &optional (window (current-window)))
(when point
(unless (lem-vi-mode/window::point-inside-window-p point window)
(vi-scroll-line-to-center))))

(define-command vi-jumps () ()
(line-end (current-point))
(lem:message-buffer (with-output-to-string (s)
(lem-vi-mode/jumplist::print-jumplist (current-jumplist) s))))

(define-command vi-jump-back (&optional (n 1)) (:universal)
(dotimes (i n)
(jump-back)))
(let ((target-point (jump-back)))
(recenter-line-if-point-not-inside-window target-point))))

(define-command vi-jump-next (&optional (n 1)) (:universal)
(dotimes (i n)
(jump-next)))
(let ((target-point (jump-next)))
(recenter-line-if-point-not-inside-window target-point))))

(define-motion vi-jump-previous () ()
(:jump t)
Expand Down
9 changes: 9 additions & 0 deletions extensions/vi-mode/window.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
(move-to-next-virtual-line point (1- (window-height* window)) window)
point))

(defun line-inside-window-p (line &optional (window (current-window)))
(let ((start-line-num (line-number-at-point (window-start-point window)))
(end-line-num (line-number-at-point (window-end-point window))))
(and (>= line start-line-num)
(<= line end-line-num))))

(defun point-inside-window-p (point &optional (window (current-window)))
(line-inside-window-p (line-number-at-point point)))

(defun window-has-following-lines-p (&optional (window (current-window)))
(let ((end-point (window-end-point window)))
(with-point ((p end-point))
Expand Down

0 comments on commit 0472a32

Please sign in to comment.