[Tex/LaTex] Synctex between Emacs and Evince, which would move focus for backward search

auctexemacsevinceforward-inverse-search

There is a great way to make Emacs, Evince and Synctex work together. However it has a minor caveat: when you do backward search, focus is not moved to Emacs (so you have to do that using mouse or Alt+Tab). This is a slight but nonetheless untoward problem.

In Linux (which I use) there is a CLI utility called wmctrl which can be used to move focus between windows. However I don't know how to add it to the script mentioned above, so I decided to ask.

(I believe the main point of this question is using Emacs Lisp so it is less relevant to Ask Ubuntu).

Best Answer

Add the following code, taken from https://lists.gnu.org/archive/html/bug-auctex/2013-04/msg00004.html, to your .emacs

(defun raise-client-frame ()
  (let ((wmctrl (executable-find "wmctrl")))
    (if wmctrl
    (start-process "wmctrl" nil wmctrl "-R" (frame-parameter nil 'name)))))
;; This raises the frame when using Evince.
(add-hook 'TeX-source-correlate-mode-hook
      (lambda ()
        (when (TeX-evince-dbus-p)
          (dbus-register-signal
           :session nil "/org/gnome/evince/Window/0"
           "org.gnome.evince.Window" "SyncSource"
           (lambda (file linecol &rest ignored)
         (TeX-source-correlate-sync-source file linecol ignored)
         (raise-client-frame))))))
;; This raises the frame when using all other viewers.
(add-hook 'server-switch-hook 'raise-client-frame)

Current version of AUCTeX supports forward/inverse search with Evince out-of-the-box, there is no more need to use old hacks.