emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/scroll-on-drag 8962f5f8a7 30/35: Add scroll-on-drag-clamp


From: ELPA Syncer
Subject: [nongnu] elpa/scroll-on-drag 8962f5f8a7 30/35: Add scroll-on-drag-clamp option to prevent scrolling past point-max
Date: Thu, 7 Jul 2022 12:02:52 -0400 (EDT)

branch: elpa/scroll-on-drag
commit 8962f5f8a79c9178a577732ddfbb333a101bc7fc
Author: Campbell Barton <ideasman42@gmail.com>
Commit: Campbell Barton <ideasman42@gmail.com>

    Add scroll-on-drag-clamp option to prevent scrolling past point-max
---
 readme.rst        |  2 ++
 scroll-on-drag.el | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/readme.rst b/readme.rst
index f26d2410c8..840b79fedb 100644
--- a/readme.rst
+++ b/readme.rst
@@ -75,6 +75,8 @@ While the defaults seem to work well, these values can be 
customized.
 
 ``scroll-on-drag-smooth``: t
    Smooth (pixel) scroll *(snapped to line on completion).*
+``scroll-on-drag-clamp``: nil
+   Prevent scrolling past the end of the buffer.
 ``scroll-on-drag-delay``: 0.01, typically in range [0.005 .. 0.1]
    Time between scroll updates.
 ``scroll-on-drag-motion-scale``: 0.25, typically in range [0.01 .. 1.0]
diff --git a/scroll-on-drag.el b/scroll-on-drag.el
index 92e7805252..f0b80f9893 100644
--- a/scroll-on-drag.el
+++ b/scroll-on-drag.el
@@ -51,6 +51,8 @@
 
 (defcustom scroll-on-drag-smooth t "Use smooth (pixel) scrolling." :type 
'boolean)
 
+(defcustom scroll-on-drag-clamp nil "Prevent scrolling past the buffer end." 
:type 'boolean)
+
 (defcustom scroll-on-drag-follow-mouse t
   "Scroll the window under the mouse cursor (instead of the current active 
window)."
   :type 'boolean)
@@ -187,6 +189,32 @@ Returns true when scrolling took place, otherwise nil."
       (restore-window-start (window-start))
       (restore-point (point))
 
+      ;; Don't move past the buffer end.
+      (point-clamp-max
+        (cond
+          (scroll-on-drag-clamp
+            (cond
+              ((eq (window-end) (point-max))
+                (line-beginning-position))
+              (t
+                (save-excursion
+                  (goto-char (line-beginning-position))
+                  (let
+                    (
+                      (lines
+                        (-
+                          (window-body-height)
+                          ;; When the point is at the window top,
+                          ;; account for it being clamped while scrolling.
+                          (1+
+                            (max
+                              scroll-margin (count-lines restore-window-start 
restore-point))))))
+                    (goto-char (point-max))
+                    (forward-line (- lines))
+                    (point))))))
+          (t
+            nil)))
+
       ;; Restore indent (lost when scrolling).
       (restore-column (current-column))
 
@@ -295,6 +323,27 @@ Returns true when scrolling took place, otherwise nil."
                         (scroll-on-drag--scroll-by-lines this-window lines t)
                         (setq do-draw t)))))
 
+                ;; Clamp the buffer inside the window.
+                (when point-clamp-max
+                  ;; Scrolling down.
+                  (when (< 0 delta)
+                    (let
+                      (
+                        (scroll-pt
+                          (cond
+                            ((eq restore-point (point))
+                              (line-beginning-position))
+                            (t
+                              (point)))))
+                      (cond
+                        ((= point-clamp-max scroll-pt)
+                          (set-window-vscroll this-window 0 t))
+
+                        ((< point-clamp-max scroll-pt)
+                          (let ((lines (count-lines scroll-pt 
point-clamp-max)))
+                            (set-window-vscroll this-window 0 t)
+                            (scroll-on-drag--scroll-by-lines this-window (- 
lines) t)))))))
+
                 (when do-draw
                   (let ((inhibit-redisplay nil))
                     (run-hooks 'scroll-on-drag-redisplay-hook)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]