diff --git a/core/autoload/ui.el b/core/autoload/ui.el index d4c0ea2f8..3f142e5eb 100644 --- a/core/autoload/ui.el +++ b/core/autoload/ui.el @@ -28,9 +28,19 @@ are open." ;;;###autoload (defun doom-recenter-a (&rest _) - "Generic advisor for recentering window (typically :after other functions)." + "Generic advice for recentering window (typically :after other functions)." (recenter)) +;;;###autoload +(defun doom-preserve-window-position-a (orig-fn &rest args) + "Generic advice for preserving cursor position on screen after scrolling." + (let ((row (cdr (posn-col-row (posn-at-point))))) + (prog1 (apply orig-fn args) + (save-excursion + (let ((target-row (- (line-number-at-pos) row))) + (unless (< target-row 0) + (evil-scroll-line-to-top target-row))))))) + ;;;###autoload (defun doom-shut-up-a (orig-fn &rest args) "Generic advisor for silencing noisy functions. diff --git a/modules/editor/evil/config.el b/modules/editor/evil/config.el index 439d8ebb8..bd15ec39c 100644 --- a/modules/editor/evil/config.el +++ b/modules/editor/evil/config.el @@ -174,7 +174,7 @@ directives. By default, this only recognizes C directives.") evil-ex-search-previous evil-ex-search-forward evil-ex-search-backward)) - (advice-add fn :after #'doom-recenter-a)) + (advice-add fn :around #'doom-preserve-window-position-a)) ;; --- custom interactive codes ----------- ;; These arg types will highlight matches in the current buffer