0

I'm trying to set a scroll margin to emacs with the scroll-margin variable, but I've noticed that when you do that and you have display-line-numbers-mode enabled, the scrolling doesn't work properly when coming up from the bottom of the file.

Here's a gif illustrating the issue

Notice that, when scrolling down, it scrolls properly, but, when the cursor reaches the bottom of the file, and starts going up, it doesn't respect the scroll-margin until the bottom of the file is not visible anymore.

If I disable display-line-numbers-mode the scrolling works properly. Is there a way to achieve scroll margin along with line numbers without falling back to linum-mode?

6
  • I don't see anything out of the ordinary in the screen gif that illustrates the issue. Could you please add a little more description to the question to tell us what to look for. Eli Z. is the person who wrote the native line-numbers and I have not seen him ever post a comment or answer on emacs.stackexchange.com. He does read and respond to posts on reddit.com/r/emacs and also any bug report that gets filed: M-x report-emacs-bug.
    – lawlist
    Commented Mar 14, 2019 at 16:15
  • I personally do not touch the scroll-margin variable. I like to use (setq scroll-conservatively 101) -- contrary to the posts regarding scroll-conservatively that set the value to a hundred million trillion, anything larger than 100 will suffice.
    – lawlist
    Commented Mar 14, 2019 at 16:18
  • @lawlist I do have scroll-conservatively enabled along with scroll-margin. Notice that, when scrolling down, it does scroll "conservatively", but, when the cursor reaches the bottom of the file, and starts going up, it doesn't respect the scroll-margin until the bottom of the file is not visible anymore.
    – Jesse
    Commented Mar 14, 2019 at 16:37
  • I edited my question with the information above. I'll try to post it on reddit too and maybe file a bug if that's the case. Thanks!
    – Jesse
    Commented Mar 14, 2019 at 16:39
  • 2
    When the scroll-margin is at its default setting of 0 and when I have (setq scroll-conservatively 101) and native line numbers active, I am unable to reproduce the behavior depicted in the gif that illustrates the issue. I would suggest adjusting scroll-conservatively within the guidelines of the doc-string and leave scroll-margin at its default setting of 0. To the extent you wish to combine the two variables to non-default specifications and you believe there is a bug, then a bug-report would be appropriate. These are things in the C code base that cannot be fixed with Lisp.
    – lawlist
    Commented Mar 14, 2019 at 18:25

1 Answer 1

0

I came up with a workaround using an advice around the previous-line funtion. Here's the code:

(defvar sm-use-visual-line t)

(defun sm-fix-enable()
  (advice-add 'previous-line :around 'sm-fix))

(defun sm-fix-disable()
  (advice-remove 'previous-line 'sm-fix))

(defun sm-get-lines-from-top ()
  (if sm-use-visual-line
      (save-excursion
        (beginning-of-line)
        (count-screen-lines (point) (window-start)))
    (- (line-number-at-pos (point)) (line-number-at-pos (window-start)) )))

(defun sm-fix(old-function &rest args)
  (apply old-function args)
  (let ((diff (- scroll-margin (sm-get-lines-from-top))))
    (when (> diff 0)
    (scroll-down diff))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.