I need to execute a function every time a window scrolls. I mean the function to be executed at point "after" the window has been scrolled (with C-v
, M-v
or with the <down>
key). I tried:
(add-hook 'window-scroll-functions 'my-function)
but it didn't work (emacs
froze until C-g
). How can I achieve it?
Additional details. My goal is to sync the Emacs window that contains some LaTeX code with the PDF viewer (Okular). I know I can do it with AUCTeX but, for some reasons, I can't use it.
I imagined a solution like:
(defun my-sync-scroll-up ()
(interactive)
(scroll-up-command)
(my-function))
(global-set-key (kbd "C-v") 'my-sync-scroll-up)
rebinding the C-v
shortcut to my new function but I'd prefer something more generalized and automatic.
The function I want to run after window scrolls is:
(defun okular-forward-search-mod ()
(interactive)
(setq PID (number-to-string (emacs-pid)))
(call-process-shell-command
(concat "okular --unique file:"
(concat (file-name-sans-extension (buffer-file-name)) ".pdf")
"#src:"
(number-to-string (line-number-at-pos (point)))
(buffer-file-name))
)
(call-process-shell-command (concat "wmctrl -ia $(wmctrl -lp | awk -vpid=$PID '$3=="
PID
" {print $1; exit}')"))
)
(add-hook 'window-scroll-functions 'okular-forward-search-mod)
On Ubuntu 16.04, Emacs 25.3.2.