diff options
| author | Gilles Castel <66gilles99@gmail.com> | 2019-03-08 14:16:01 +0100 |
|---|---|---|
| committer | Gilles Castel <66gilles99@gmail.com> | 2019-03-08 14:16:01 +0100 |
| commit | 278de2d7f69898b07bdd69edba7d84f9ddf2acab (patch) | |
| tree | 0639b63d64ffe24b8835498e44d0adc9656e0fbe /vim.py | |
Initial commit
Diffstat (limited to 'vim.py')
| -rw-r--r-- | vim.py | 76 |
1 files changed, 76 insertions, 0 deletions
@@ -0,0 +1,76 @@ +import os +import tempfile +import subprocess +from constants import TARGET +from clipboard import copy +from press import press +from evdev import ecodes + +def open_vim(compile_latex): + f = tempfile.NamedTemporaryFile(mode='w+', delete=False) + + f.write('$$') + f.close() + + subprocess.run([ + 'urxvt', + '-fn', 'xft:Iosevka Term:pixelsize=24', + '-geometry', '60x5', + '-name', 'popup-bottom-center', + '-e', "vim", + "-u", "~/.minimal-tex-vimrc", + f"{f.name}", + ]) + + latex = "" + with open(f.name, 'r') as g: + latex = g.read().strip() + + os.remove(f.name) + + if latex != '$$': + if not compile_latex: + svg = f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?> + <svg> + <text + style="font-size:15px; font-family:'Latin Modern Math';-inkscape-font-specification:'Latin Modern Math, Normal';fill:#000000;fill-opacity:1;stroke:none;" + xml:space="preserve"><tspan sodipodi:role="line">{latex}</tspan></text> + </svg> """ + copy(svg, target=TARGET) + else: + m = tempfile.NamedTemporaryFile(mode='w+', delete=False) + m.write(r""" + \documentclass[12pt,border=12pt]{standalone} + + \usepackage[utf8]{inputenc} + \usepackage[T1]{fontenc} + \usepackage{textcomp} + \usepackage[dutch]{babel} + \usepackage{amsmath, amssymb} + \newcommand{\R}{\mathbb R} + + \begin{document} + """ + latex + r"""\end{document}""") + m.close() + + working_directory = tempfile.gettempdir() + subprocess.run( + ['pdflatex', m.name], + cwd=working_directory, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL + ) + + subprocess.run( + ['pdf2svg', f'{m.name}.pdf', f'{m.name}.svg'], + cwd=working_directory + ) + + with open(f'{m.name}.svg') as svg: + subprocess.run( + ['xclip', '-selection', 'c', '-target', TARGET], + stdin=svg + ) + + press(ecodes.KEY_V, [ecodes.KEY_LEFTCTRL]) + press(ecodes.KEY_ESC) |
