2026-06-15 Scrolling pdfs in other windows

I have written about pdf-tools quite a few times – it’s a fantastic Emacs package for viewing and annotating pdfs without leaving the comfort of Emacs. It is not ideal, though – or at least, not ideal for me. One feature of Emacs I often use is the scroll-other-window command (bound to C-M-v), and its sibling scroll-other-window-down (C-M-S-v). They are extremely useful for example when reading documentation or watching live Markdown preview, and I wish they worked with the TeX and pdf-tools duo, too.

Well, it’s Emacs, so it shouldn’t be difficult to make them!

(defun pdf-view-scroll-up-or-next-page-next-window (_ignored)
  "Scroll pdf in other window up.
Return nil if other window does not show a pdf."
  (when (eq (buffer-local-value
             'major-mode (window-buffer (next-window)))
            'pdf-view-mode)
    (with-selected-window (next-window)
      (pdf-view-scroll-up-or-next-page))))

(defun pdf-view-scroll-down-or-previous-page-next-window (_ignored)
  "Scroll pdf in other window up.
Return nil if other window does not show a pdf."
  (when (eq (buffer-local-value
             'major-mode (window-buffer (next-window)))
            'pdf-view-mode)
    (with-selected-window (next-window)
      (pdf-view-scroll-down-or-previous-page))))

(advice-add 'scroll-other-window
            :before-until
            #'pdf-view-scroll-up-or-next-page-next-window)
(advice-add 'scroll-other-window-down
            :before-until
            #'pdf-view-scroll-down-or-previous-page-next-window)

This solution could be made more elegant, of course – for instance, these two functions are almost identical, so I could write one function accepting pdf-view-scroll-up-or-next-page or pdf-view-scroll-down-or-previous-page as an argument and avoid code duplication. That’s not the point, however – this works, was easy to write and is easy to understand. Notice how I used the :before-until advice combinator – this way, if the condition in my functions’ when clause are true, the commands I advised are never run.

That’s it for today! As usual, let me remind you that if I learned to extend Emacs to fit my personal workflow better, so can you, and the main resource I recommend to learn to do that is late Robert J. Chassell’s fantastic Introduction to Programming in Emacs Lisp. If that isn’t enough, I wrote Hacking your way around in Emacs as a next step, showing step-by-step how to write three useful Emacs extensions.

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryTeX