Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support Continous Scrolling in pdf-tools #18

Open
odanoburu opened this issue May 25, 2021 · 5 comments
Open

Support Continous Scrolling in pdf-tools #18

odanoburu opened this issue May 25, 2021 · 5 comments
Labels
feature request Requests for missing / new functionality in pdf-tools help wanted Extra attention is needed

Comments

@odanoburu
Copy link

a very popular request for pdf-tools is the implementation of actual continuous scrolling, with multiple PDF pages per buffer (see politza/pdf-tools#27). but alas, this is hard to implement, although a hack is available. the former pdf-tools maintainer suggested that a compromise fix might be a fixed-scroll-mode, in which a call to pdf-view-scroll-up-or-next-page always performs the same amount of scrolling whenever it does not move to next page, so that we don't lose the location of the line we were reading when the scroll hits the bottom of the page. I'd be happy enough with this fix, and I think a lot of people would too (the person who first opened the issue made a good case: they put the unpredictability of the current line after scrolling as the problem, and suggested contiguous view as a mere possible fix, even if the most common and expected one).

if I can get some pointers as to how to actually implement this I think I can manage a PR :)

@vedang vedang changed the title fixed-scroll-mode? Support Continous Scrolling in pdf-tools May 29, 2021
@vedang vedang added feature request Requests for missing / new functionality in pdf-tools help wanted Extra attention is needed labels May 29, 2021
@odanoburu
Copy link
Author

odanoburu commented Jun 3, 2021

I've taken a stab at a fixed-scroll-mode, and it's not hard to implement, although I'm not sure how best we can integrate it in pdf-tools since it required changes to functions in the image library. the image library does not support scrolling images past their bottom, so I had to change the functions below to do it anyway. I've just implemented the scrolling up (going towards the end of the pdf) functionality, but scrolling down should be analogous; there is still work to do to support proper mouse scrolling and stuff, this is a prototype. do note that the functions below are slightly changed versions of the ones from Emacs 27.2 image library and pdf-tools version 20210511.1739.

to incorporate these in pdf-tools, should we first patch image, incorporate the changes in pdf-tools, or is there some other way of going about doing this?

(defun my-image-next-line (n)
  "Scroll image in current window upward by N lines.
Don't stop if the bottom edge of the image is reached."
  (interactive "p")
  ;; Convert N to pixels.
  (setq n (* n (frame-char-height)))
  (cond ((= n 0) nil)
	((< n 0)
	 (image-set-window-vscroll (max 0 (+ (window-vscroll nil t) n))))
	(t
	 (image-set-window-vscroll (+ n (window-vscroll nil t))))))

(defun my-image-scroll-up (&optional n)
  "Scroll image in current window upward by N lines.
Don't stop if the bottom edge of the image is reached.
If ARG is omitted or nil, scroll upward by a near full screen.
A near full screen is `next-screen-context-lines' less than a full screen.
Negative ARG means scroll downward.
If ARG is the atom `-', scroll downward by nearly full screen.
When calling from a program, supply as argument a number, nil, or `-'."
  (interactive "P")
  (cond ((null n)
	 (let* ((edges (window-inside-edges))
		(win-height (- (nth 3 edges) (nth 1 edges))))
	   (my-image-next-line
	    (max 0 (- win-height next-screen-context-lines)))))
	((eq n '-)
	 (let* ((edges (window-inside-edges))
		(win-height (- (nth 3 edges) (nth 1 edges))))
	   (my-image-next-line
	    (min 0 (- next-screen-context-lines win-height)))))
	(t (my-image-next-line (prefix-numeric-value n)))))

(defun my-pdf-view-scroll-up-or-next-page (&optional arg)
  "Scroll page up ARG lines if possible, else go to the next page.

When `pdf-view-continuous' is non-nil, scrolling upward at the
bottom edge of the page moves to the next page.  Otherwise, go to
next page only on typing SPC (ARG is nil)."
  (interactive "P")
  (if (or pdf-view-continuous (null arg))
      (seq-let (_ top _ bot)
	  (window-edges nil nil nil
			pdf-view-have-image-mode-pixel-vscroll)
	(let* ((win-height (- bot top))
	       (image (image-get-display-property))
	       (img-height (ceiling (cdr (image-display-size image t))))
	       (cur-page (pdf-view-current-page)))
	  (if (< (+ win-height (window-vscroll nil pdf-view-have-image-mode-pixel-vscroll))
		 img-height)
	      (my-image-scroll-up arg)
	    (pdf-view-next-page)
	    (when (/= cur-page (pdf-view-current-page))
	      (image-bob)
	      (image-bol 1))
	    (image-set-window-hscroll (window-hscroll)))))
    (my-image-scroll-up arg)))

@dalanicolai
Copy link
Contributor

@odanoburu The example code you posted misses some my-image-next-line function. Did you forget to add it here?

@odanoburu
Copy link
Author

odanoburu commented Jun 15, 2021

@dalanicolai oops, it's fixed now! thanks for the heads-up :) (I actually had posted an extra function, and missed this one)

@dalanicolai
Copy link
Contributor

dalanicolai commented Aug 1, 2021

I have just created a (not very, but still) simple sketch to obtain "real" continuous scrolling (i.e. in a single buffer). You can check it out here. It would be great if others would also like to hack on it. With pdf-tools, and docview as an example, together with the very simple to use python-epc and the amazing pymupdf documentation, a lot should be possible. On the other hand I understand that, like for myself, this will not have priority. But it could be fun and educational. I started this for fun and to show a proof of concept.

@kimim
Copy link

kimim commented May 6, 2023

I create a small package to indicate the remaining text: https://github.com/kimim/pdf-view-pagemark

Demo:
https://github.com/kimim/pdf-view-pagemark/blob/master/pdf-view-pagemark-demo.gif

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
feature request Requests for missing / new functionality in pdf-tools help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants