0

I use to edit LaTeX documents with okular as pdf viewer in Ubuntu Linux. I have this code:

(defun okular-forward-search ()
  (interactive)
  (start-process
   "okular"
   "okular-output"
   "okular" ;;; src-args
    ;;; args for -sourceposition:
   "--unique"
   (concat
    "file:"
    (concat (file-name-sans-extension (buffer-file-name)) ".pdf")
    "#src:"
    (number-to-string (line-number-at-pos (point)))
    (buffer-file-name))))

that performs forward-search from LaTeX code in emacs to the corresponding page in the generated .pdf file. My problem is that this shift the "focus" from emacs to okular and this is, for me, very annoying.

Is there a hack to keep the focus on emacs?

EDIT I modified the function:

(defun okular-forward-search-mod ()
  (interactive)
  (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 "wmctrl -a emacs"))

But I still have an issue. If more instances of emacs are open the focus is on the first opened. How can I get the window PID of the current emacs' process?

1 Answer 1

0

Ok, I solved my problem rewriting my function:

(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}')"))
)

It seems to work fine.

I found some hints here https://unix.stackexchange.com/questions/45154/switch-to-an-application-using-its-pid

Your Answer

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

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