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

Reapply scroll bar after editing buffer #39

Merged
merged 1 commit into from
Feb 8, 2022
Merged
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
56 changes: 31 additions & 25 deletions yascroll.el
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
;;; Code:

(require 'cl-lib)
(require 'subr-x)



Expand Down Expand Up @@ -144,9 +145,8 @@ not be displayed."

;;; Scroll Bar Thumb

(defvar yascroll:thumb-overlays nil
(defvar-local yascroll:thumb-overlays nil
"Overlays for scroll bar thum.")
(make-variable-buffer-local 'yascroll:thumb-overlays)

(defun yascroll:compute-thumb-size (window-lines buffer-lines)
"Return the proper size (height) of scroll bar thumb.
Expand Down Expand Up @@ -267,27 +267,26 @@ Doc-this WINDOW-LINES, BUFFER-LINES and SCROLL-TOP."

(defun yascroll:show-scroll-bar-internal ()
"Show scroll bar in buffer."
(let ((scroll-bar (yascroll:choose-scroll-bar)))
(when scroll-bar
(let ((window-lines (yascroll:window-height))
(buffer-lines (count-lines (point-min) (point-max))))
(when (< window-lines buffer-lines)
(let* ((scroll-top (count-lines (point-min) (window-start)))
(thumb-window-line (yascroll:compute-thumb-window-line
window-lines buffer-lines scroll-top))
(thumb-buffer-line (+ scroll-top thumb-window-line))
(thumb-size (yascroll:compute-thumb-size
window-lines buffer-lines))
(make-thumb-overlay
(cl-ecase scroll-bar
(left-fringe 'yascroll:make-thumb-overlay-left-fringe)
(right-fringe 'yascroll:make-thumb-overlay-right-fringe)
(text-area 'yascroll:make-thumb-overlay-text-area))))
(when (<= thumb-buffer-line buffer-lines)
(yascroll:make-thumb-overlays make-thumb-overlay
thumb-window-line
thumb-size)
(yascroll:schedule-hide-scroll-bar))))))))
(when-let ((scroll-bar (yascroll:choose-scroll-bar)))
(let ((window-lines (yascroll:window-height))
(buffer-lines (count-lines (point-min) (point-max))))
(when (< window-lines buffer-lines)
(let* ((scroll-top (count-lines (point-min) (window-start)))
(thumb-window-line (yascroll:compute-thumb-window-line
window-lines buffer-lines scroll-top))
(thumb-buffer-line (+ scroll-top thumb-window-line))
(thumb-size (yascroll:compute-thumb-size
window-lines buffer-lines))
(make-thumb-overlay
(cl-ecase scroll-bar
(left-fringe 'yascroll:make-thumb-overlay-left-fringe)
(right-fringe 'yascroll:make-thumb-overlay-right-fringe)
(text-area 'yascroll:make-thumb-overlay-text-area))))
(when (<= thumb-buffer-line buffer-lines)
(yascroll:make-thumb-overlays make-thumb-overlay
thumb-window-line
thumb-size)
(yascroll:schedule-hide-scroll-bar)))))))

;;;###autoload
(defun yascroll:show-scroll-bar ()
Expand Down Expand Up @@ -340,10 +339,15 @@ to the selected window if the value is nil."
(when (yascroll:scroll-bar-visible-p)
(yascroll:safe-show-scroll-bar)))

(defun yascroll:before-change (beg end)
"Before change BEG point and END point."
(defun yascroll:before-change (&rest _)
"Before modifiying the buffer."
(yascroll:hide-scroll-bar))

(defun yascroll:after-change (&rest _)
"After modifiying the buffer."
(unless yascroll:delay-to-hide
(yascroll:safe-show-scroll-bar)))

(defvar yascroll--last-scroll nil
"Record the after scroll parameters.")

Expand All @@ -364,10 +368,12 @@ to the selected window if the value is nil."
(if yascroll-bar-mode
(progn
(add-hook 'before-change-functions 'yascroll:before-change nil t)
(add-hook 'after-change-functions 'yascroll:after-change nil t)
(add-hook 'window-scroll-functions 'yascroll:after-window-scroll nil t)
(add-hook 'window-configuration-change-hook 'yascroll:after-window-configuration-change nil t))
(yascroll:hide-scroll-bar)
(remove-hook 'before-change-functions 'yascroll:before-change t)
(remove-hook 'after-change-functions 'yascroll:after-change t)
(remove-hook 'window-scroll-functions 'yascroll:after-window-scroll t)
(remove-hook 'window-configuration-change-hook 'yascroll:after-window-configuration-change t)))

Expand Down