Skip to content

Instantly share code, notes, and snippets.

@martinec
Created March 3, 2017 03:45
Show Gist options
  • Save martinec/31e55ffe4bb8e7d8dcf60114a79e7609 to your computer and use it in GitHub Desktop.
Save martinec/31e55ffe4bb8e7d8dcf60114a79e7609 to your computer and use it in GitHub Desktop.
SyncTeX between LaTeX and PDF with Geany and Zathura
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Synchronize between LaTeX and PDF with Geany and Zathura
# This plugin add both a new menu item under the Tools menu
# and a keyboard shortcut to launch Zathura using its SyncTeX
# forward search feature. A simpler approach would be to add
# a custom build command (Build > Set Build Commands), however
# this lack of a key binding and current column information
# To make this plugin work, you must:
# compile (make WITH_SYNCTEX=1) and install zathura from https://git.pwmt.org/pwmt/zathura.git
# compile and install zathura-pdf-mupdf from https://git.pwmt.org/pwmt/zathura-pdf-mupdf.git
# compile (./waf configure --enable-gtk3) and install geany from https://github.com/geany/geany
# compile and install geanypy Gtk3 Branch from https://github.com/sagarchalise/geanypy/tree/gtk3
# copy this file to ~/.config/geany/plugins/geanypy/plugins/synctex.py
# update the working_dir and pdf_file variables below
try:
from gi import pygtkcompat
except ImportError:
pygtkcompat = None
if pygtkcompat is not None:
pygtkcompat.enable()
pygtkcompat.enable_gtk(version='3.0')
import gtk
import geany
import subprocess
import sys
import os
class SyncTeX(geany.Plugin):
__plugin_name__ = "SyncTeX"
__plugin_version__ = "0.0.1"
__plugin_description__ = "Synchronize between LaTeX and PDF with Geany and Zathura"
__plugin_author__ = "Cristian Martinez <me-at-martinec.org>"
file_types = ('LaTeX')
working_dir = r'/path/to/latex/files'
pdf_file = "foo.pdf"
zathura_cmd = "zathura"
@classmethod
def check_filetype(cls):
cur_file_type = geany.document.get_current().file_type.name
if cur_file_type in cls.file_types:
return True
return False
def __init__(self):
super(geany.Plugin, self).__init__()
try:
geany.bindings.register_binding("SyncTeX Forward Search", "Go to position", self.go_to_synctex_position)
except AttributeError:
geany.ui_utils.set_statusbar("GeanyPy was not compiled with keybindings support.")
self.menu_item = gtk.MenuItem("SyncTeX Forward Search")
self.menu_item.show()
geany.main_widgets.tools_menu.append(self.menu_item)
self.menu_item.connect("activate", self.go_to_synctex_position)
def go_to_synctex_position(self, key_id):
if self.check_filetype():
line, col, filename = self.get_synctex_position()
command_args = [self.zathura_cmd, "--synctex-forward", "%s:%s:%s" % (line, col, filename) , "%s/%s" % (self.working_dir,self.pdf_file)]
geany.ui_utils.set_statusbar("Launching %s" % (' '.join(map(str,command_args))))
# @see http://stackoverflow.com/a/4453495/2042871
environment = os.environ.copy()
environment["GDK_SCALE"] = "1"
# @see http://stackoverflow.com/a/2251026/2042871
pid = subprocess.Popen(command_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, env=environment,cwd=self.working_dir)
else:
geany.ui_utils.set_statusbar("SyncTeX: This is not a LaTeX file!")
def get_synctex_position(self):
document = geany.document.get_current()
filename = document.file_name
editor = document.editor
scintilla = editor.scintilla
pos = scintilla.get_current_position()
line = scintilla.get_line_from_position(pos) + 1
col = scintilla.get_col_from_position(pos) + 1
return (line, col, filename)
def cleanup(self):
self.menu_item.destroy()
def main():
return 0
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment