[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/scroll-on-jump 9767013ca8 13/31: Support wrapping function
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/scroll-on-jump 9767013ca8 13/31: Support wrapping functions that themselves scroll |
Date: |
Thu, 7 Jul 2022 12:03:12 -0400 (EDT) |
branch: elpa/scroll-on-jump
commit 9767013ca85928b822226cf5a8d8e0554e14b65f
Author: Campbell Barton <ideasman42@gmail.com>
Commit: Campbell Barton <ideasman42@gmail.com>
Support wrapping functions that themselves scroll
Fixes #1
---
readme.rst | 22 +++++++++++--
scroll-on-jump.el | 97 ++++++++++++++++++++++++++++++++++++++++++++++---------
2 files changed, 101 insertions(+), 18 deletions(-)
diff --git a/readme.rst b/readme.rst
index 15eb40d8da..3358cd3472 100644
--- a/readme.rst
+++ b/readme.rst
@@ -53,7 +53,6 @@ The following functions are exposed.
``scroll-on-jump-advice-remove``
Remove the advice added to the function.
-
Commands that work well include:
- Jump to search result, paragraph, function ... etc.
@@ -61,6 +60,16 @@ Commands that work well include:
- Go to declaration.
+Wrapping Commands That Scroll
+-----------------------------
+
+If a command it's self sets a new scroll location,
+these can be wrapped using ``scroll-on-jump-with-scroll-`` prefix,
+so ``scroll-on-jump-with-scroll-interactive``,
``scroll-on-jump-with-scroll-advice-add`` .. etc.
+
+In this case the newly set scroll location will be used when displaying the
animation.
+
+
Key Binding Example
-------------------
@@ -129,7 +138,14 @@ Here is a more complete example for evil-mode users.
(scroll-on-jump-advice-add evil-ex-search-next)
(scroll-on-jump-advice-add evil-ex-search-previous)
(scroll-on-jump-advice-add evil-forward-paragraph)
- (scroll-on-jump-advice-add evil-backward-paragraph))
+ (scroll-on-jump-advice-add evil-backward-paragraph)
+
+ ;; Actions that themselves scroll.
+ (scroll-on-jump-with-scroll-advice-add evil-scroll-down)
+ (scroll-on-jump-with-scroll-advice-add evil-scroll-up)
+ (scroll-on-jump-with-scroll-advice-add evil-scroll-line-to-center)
+ (scroll-on-jump-with-scroll-advice-add evil-scroll-line-to-top)
+ (scroll-on-jump-with-scroll-advice-add evil-scroll-line-to-bottom))
(with-eval-after-load 'goto-chg
(scroll-on-jump-advice-add goto-last-change)
@@ -173,5 +189,5 @@ Until this is available on melpa, straight can be used to
install this package.
Limitations
===========
-- Any commands that themselves scroll to a new location will not work as
expected
+- Any commands that themselves scroll to a new location *and* modify the
buffer will not work as expected
(they may scroll too far for example).
diff --git a/scroll-on-jump.el b/scroll-on-jump.el
index 640be27ee0..f318f669f1 100644
--- a/scroll-on-jump.el
+++ b/scroll-on-jump.el
@@ -401,11 +401,7 @@ Argument ALSO-MOVE-POINT When non-nil, move the POINT as
well."
(goto-char point-next))
-;; ---------------------------------------------------------------------------
-;; Public Functions
-
-;;;###autoload
-(defmacro scroll-on-jump (&rest body)
+(defmacro scroll-on-jump--impl (use-window-start &rest body)
"Main macro that wraps BODY in logic that reacts to change in `point'."
`
(let
@@ -414,7 +410,13 @@ Argument ALSO-MOVE-POINT When non-nil, move the POINT as
well."
(window (selected-window))
(point-prev (point))
- (point-next nil))
+ (point-next nil)
+
+ (window-start-prev nil)
+ (window-start-next nil))
+
+ (when ,use-window-start
+ (setq window-start-prev (window-start window)))
(prog1
(save-excursion
@@ -431,19 +433,53 @@ Argument ALSO-MOVE-POINT When non-nil, move the POINT as
well."
(setq point-next (point))))
(cond
- ( ;; Perform animated scroll.
- (and
- ;; Buffer/Context changed.
- (eq buf (window-buffer window)) (eq buf (current-buffer)) (eq
window (selected-window))
+ ( ;; Context changed or recursed, simply jump.
+ (not
+ (and
+ ;; Buffer/Context changed.
+ (eq buf (window-buffer window))
+ (eq buf (current-buffer))
+ (eq window (selected-window))
- ;; Disallow recursion.
- (not (boundp 'scroll-on-jump--resurse)))
+ ;; Disallow recursion.
+ (not (boundp 'scroll-on-jump--resurse))))
+ (goto-char point-next))
+
+ (t ;; Perform animated scroll.
(let ((scroll-on-jump--resurse t))
- (scroll-on-jump-auto-center window point-prev point-next)))
+ (if window-start-prev
+ (progn
+ (setq window-start-next (window-start window))
+ (unless (eq window-start-prev window-start-next)
+ (set-window-start window window-start-prev)
+ (let
+ (
+ (lines-scroll
+ (1- (count-screen-lines window-start-prev
window-start-next t window)))
+ (dir
+ (if (< window-start-prev window-start-next)
+ 1
+ -1)))
+ (scroll-on-jump--scroll-impl
+ window
+ (* dir lines-scroll)
+ dir
+ (not (eq (point) point-next)))))
+ (goto-char point-next))
+ (scroll-on-jump-auto-center window point-prev point-next))))))))
- (t ;; Context changed or recursed, simply jump.
- (goto-char point-next))))))
+
+;; ---------------------------------------------------------------------------
+;; Public Functions
+
+;; ----------------
+;; Default Behavior
+;;
+;; Use for wrapping functions that set the point.
+
+;;;###autoload
+(defmacro scroll-on-jump (&rest body) `(scroll-on-jump--impl nil ,@body))
;;;###autoload
(defmacro scroll-on-jump-interactive (fn)
@@ -468,6 +504,37 @@ without changing behavior anywhere else."
"Remove advice on FN added by `scroll-on-jump-advice-add'."
(advice-remove fn #'scroll-on-jump-advice--wrapper))
+;; -----------
+;; With-Scroll
+;;
+;; Use when wrapping actions that themselves scroll.
+
+;;;###autoload
+(defmacro scroll-on-jump-with-scroll (&rest body) `(scroll-on-jump--impl t
,@body))
+
+;;;###autoload
+(defmacro scroll-on-jump-with-scroll-interactive (fn)
+ "Macro that wraps interactive call to function FN.
+
+Use if you want to use `scroll-on-jump-with-scroll' for a single `key-binding',
+without changing behavior anywhere else."
+ `(lambda () (interactive) (scroll-on-jump-with-scroll (call-interactively
,fn))))
+
+;; Helper function (not public).
+(defun scroll-on-jump-advice--with-scroll-wrapper (old-fn &rest args)
+ "Internal function use to advise using `scroll-on-jump-advice-add' (calling
OLD-FN with ARGS)."
+ (scroll-on-jump-with-scroll (apply old-fn args)))
+
+;;;###autoload
+(defmacro scroll-on-jump-with-scroll-advice-remove (fn)
+ "Remove advice on FN added by `scroll-on-jump-with-scroll-advice-add'."
+ (advice-remove fn #'scroll-on-jump-advice--with-scroll-wrapper))
+
+;;;###autoload
+(defmacro scroll-on-jump-with-scroll-advice-add (fn)
+ "Add advice to FN, to instrument it with scrolling capabilities."
+ (advice-add fn :around #'scroll-on-jump-advice--with-scroll-wrapper))
+
(provide 'scroll-on-jump)
;;; scroll-on-jump.el ends here
- [nongnu] elpa/scroll-on-jump 42329b5707 20/31: Fix visual glitch when used with next/previous line, (continued)
- [nongnu] elpa/scroll-on-jump 42329b5707 20/31: Fix visual glitch when used with next/previous line, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 5007e9ea8a 18/31: readme: add examples suggested in #2, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 69c86542a1 15/31: Docs: add docstrings, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump dd2818d906 17/31: Cleanup: make docstring under 80 chars, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 30dc5f5e50 21/31: Cleanup: remove recursion check, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 28bac2cdc3 22/31: Cleanup: quiet checkdoc warnings for wide docstrings, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 0ce93b3ea6 19/31: readme: remove 'evil-goto-first-line' as 'evil-goto-line' already scrolls, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 000dc41039 25/31: Fix error passing out-of-range point to count-screen-lines, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 99386fc01b 30/31: Cleanup: use brief SPDX license, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 556e9a7a81 28/31: Fix error detecting scroll changes, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 9767013ca8 13/31: Support wrapping functions that themselves scroll,
ELPA Syncer <=
- [nongnu] elpa/scroll-on-jump 8fde237ae6 31/31: Change URL to codeberg, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump a614bc7f8f 06/31: Fix the case when the windows buffer is not the current-buffer, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 5a6f698b6f 23/31: Cleanup: replace 'if' with 'cond', ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 1e9e09f0cc 29/31: Fix error counting lines jumping to a point outside buffer range, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump a2d6996a36 24/31: Cleanup: use defgroup, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 7b2919f8d8 27/31: Fix smooth (pixel) scrolling not being used in most cases, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 1f2349edf4 04/31: Fix scrolling down when the point is at the beginning of the buffer, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump e8b79d35d4 09/31: Cleanup: split scroll-on-jump--scroll-impl into it's own function, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump 028e672391 03/31: Cleanup: no need for `let*`, ELPA Syncer, 2022/07/07
- [nongnu] elpa/scroll-on-jump eb77ab1920 02/31: readme: add a more complete example, ELPA Syncer, 2022/07/07