Okular inverse search for multiple editors (e.g. Kile)

Setting up inverse search for Kile/Okular is well documented, e.g. in the official Kile docs. What has always bugged me, though, is that the inverse search editor is set globally in the okularpartrc config file, which means that you can't set up inverse search with different editors for different source code types.
I put together a workaround that changes Okular's editor choice to Kile when Okular is called from Kile by temporarily overwriting the okularpartrc file.
  • First, we set up the shell script handling the overwrite (download). It takes 2 arguments, the first ($1) is the editor choice (must be one of Okular's predefined ones), the second ($2) the file path.  If you want other settings than vanilla Okular without a left panel, copy them from your own okularpartrc. We have to use echo -e to render the newlines \n correctly. The --unique flag ensures there's only one Okular instance per document. The original config file is restored from a backup copy when the Okular instance is closed.
mv ~/.kde/share/config/okularpartrc ~/.kde/share/config/okularpartrc.backup
echo -e "[General]\nExternalEditor=$1\n\n[Main View]\nShowLeftPanel=false" > ~/.kde/share/config/okularpartrc
okular --unique $2
if [ -f ~/.kde/share/config/okularpartrc.backup ]
then mv ~/.kde/share/config/okularpartrc.backup ~/.kde/share/config/okularpartrc
  • We save the script to /usr/bin/okularinv and make it readable and executable:
    sudo chmod a+rx /usr/bin/okularinv
  • Now for the Kile configuration:
  • In Settings…→Configure Kile…→Tools→Build, make sure that PDFLaTeX/LaTeX is configured as 'Modern' (this invokes synctex and enables inverse search)
  • We also create a new configuration "Okularinv" under ViewPDF, with the command okularinv and the options 'Kile' '%target' (with exact quotes and spaces). These will be the arguments passed on to the shell script.
  • Inverse search should now work with shift+left click from Okular. Forward search is also available: if not already there, add 'ForwardPDF' to one of your toolbars under Settings…→Configure toolbars…

No comments: