|
1 | | -import os |
| 1 | +from __future__ import annotations |
| 2 | +import stat |
2 | 3 | import sublime |
3 | | -import time |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from typing import cast |
4 | 7 |
|
5 | 8 | from ...latextools.utils.external_command import check_call |
6 | 9 | from ...latextools.utils.external_command import check_output |
|
15 | 18 |
|
16 | 19 | class EvinceViewer(BaseViewer): |
17 | 20 |
|
18 | | - PYTHON = None |
| 21 | + _cached_python = None |
19 | 22 |
|
20 | | - def _get_evince_folder(self): |
21 | | - script_dir = os.path.join(sublime.cache_path(), "LaTeXTools", "viewer", "evince") |
22 | | - os.makedirs(script_dir, exist_ok=True) |
| 23 | + @property |
| 24 | + def _evince_folder(self) -> Path: |
| 25 | + script_dir = Path(sublime.cache_path()) / "LaTeXTools" / "viewer" / "evince" |
| 26 | + script_dir.mkdir(parents=True, exist_ok=True) |
23 | 27 |
|
24 | 28 | # extract scripts to cache dir and run them from there |
25 | 29 | for script_name in ("backward_search", "forward_search", "sync"): |
26 | | - script_file = os.path.join(script_dir, script_name) |
27 | | - if os.path.exists(script_file): |
28 | | - continue |
29 | | - try: |
30 | | - data = sublime.load_binary_resource( |
31 | | - f"Packages/LaTeXTools/plugins/viewer/evince/{script_name}" |
32 | | - ) |
33 | | - except FileNotFoundError: |
34 | | - sublime.error_message( |
35 | | - f"Cannot find required script '{script_name}'\n" |
36 | | - "for 'evince' viewer in LaTeXTools package." |
37 | | - ) |
38 | | - else: |
39 | | - with open(script_file, "wb") as fobj: |
40 | | - fobj.write(data) |
| 30 | + script_file = script_dir / script_name |
| 31 | + if not script_file.exists(): |
| 32 | + try: |
| 33 | + data = ( |
| 34 | + sublime.load_binary_resource( |
| 35 | + f"Packages/LaTeXTools/plugins/viewer/evince/{script_name}" |
| 36 | + ) |
| 37 | + .replace(b"\r\n", b"\n") |
| 38 | + .replace(b"\r", b"\n") |
| 39 | + ) |
| 40 | + except FileNotFoundError: |
| 41 | + sublime.error_message( |
| 42 | + f"Cannot find required script '{script_name}'\n" |
| 43 | + "for 'evince' viewer in LaTeXTools package." |
| 44 | + ) |
| 45 | + continue |
| 46 | + |
| 47 | + script_file.write_bytes(data) |
| 48 | + script_file.chmod(script_file.stat().st_mode | stat.S_IXUSR) |
41 | 49 |
|
42 | 50 | return script_dir |
43 | 51 |
|
44 | | - def _is_evince_running(self, pdf_file): |
| 52 | + def _is_evince_running(self, pdf_file: str) -> bool: |
45 | 53 | try: |
46 | 54 | return f"evince {pdf_file}" in check_output(["ps", "xv"], use_texpath=False) |
47 | 55 | except Exception: |
48 | 56 | return False |
49 | 57 |
|
50 | | - def _get_settings(self): |
51 | | - """ |
52 | | - returns evince-related settings as a tuple |
53 | | - (python, sync_wait) |
54 | | - """ |
55 | | - linux_settings = get_setting("linux", {}) |
| 58 | + @property |
| 59 | + def _python_binary(self) -> str: |
| 60 | + linux_settings = cast(dict, get_setting("linux", {})) |
56 | 61 | python = linux_settings.get("python") |
57 | | - if python is None or python == "": |
58 | | - if self.PYTHON is not None: |
59 | | - python = self.PYTHON |
60 | | - else: |
| 62 | + if python and isinstance(python, str): |
| 63 | + return python |
| 64 | + |
| 65 | + if self._cached_python is None: |
| 66 | + try: |
| 67 | + check_call(["python3", "-c", "import dbus"], use_texpath=False) |
| 68 | + self._cached_python = "python3" |
| 69 | + except Exception: |
61 | 70 | try: |
62 | 71 | check_call(["python", "-c", "import dbus"], use_texpath=False) |
63 | | - python = "python" |
| 72 | + self._cached_python = "python" |
64 | 73 | except Exception: |
65 | | - try: |
66 | | - check_call(["python3", "-c", "import dbus"], use_texpath=False) |
67 | | - python = "python3" |
68 | | - except Exception: |
69 | | - sublime.error_message( |
70 | | - """Cannot find a valid Python interpreter. |
71 | | - Please set the python setting in your LaTeXTools |
72 | | - settings.""".strip() |
73 | | - ) |
74 | | - # exit the viewer process |
75 | | - raise Exception("Cannot find a valid interpreter") |
76 | | - self.PYTHON = python |
77 | | - return (python, linux_settings.get("sync_wait") or 1.0) |
78 | | - |
79 | | - def _launch_evince(self, pdf_file): |
80 | | - ev_path = self._get_evince_folder() |
81 | | - py_binary, _ = self._get_settings() |
82 | | - st_binary = get_sublime_exe() |
83 | | - |
84 | | - external_command( |
85 | | - ["sh", os.path.join(ev_path, "sync"), py_binary, st_binary, pdf_file], |
86 | | - cwd=ev_path, |
87 | | - use_texpath=False, |
88 | | - ) |
89 | | - |
90 | | - def forward_sync(self, pdf_file, tex_file, line, col, **kwargs): |
91 | | - keep_focus = kwargs.pop("keep_focus", True) |
92 | | - bring_evince_forward = get_setting("viewer_settings", {}).get("bring_evince_forward", False) |
93 | | - |
94 | | - ev_path = self._get_evince_folder() |
95 | | - py_binary, sync_wait = self._get_settings() |
96 | | - |
97 | | - evince_running = self._is_evince_running(pdf_file) |
98 | | - if not keep_focus or not evince_running or bring_evince_forward: |
99 | | - self._launch_evince(pdf_file) |
| 74 | + sublime.error_message( |
| 75 | + "Cannot find a valid Python interpreter.\n" |
| 76 | + "Please set the python setting in your LaTeXTools settings." |
| 77 | + ) |
| 78 | + # exit the viewer process |
| 79 | + raise RuntimeError("Cannot find a valid python interpreter!") |
| 80 | + |
| 81 | + return self._cached_python |
| 82 | + |
| 83 | + def forward_sync(self, pdf_file: str, tex_file: str, line: int, col: int, **kwargs) -> None: |
| 84 | + viewer_settings = cast(dict, get_setting("viewer_settings", {})) |
| 85 | + bring_evince_forward = viewer_settings.get("bring_evince_forward", False) |
| 86 | + keep_focus = kwargs.get("keep_focus", True) |
| 87 | + evince_folder = self._evince_folder |
| 88 | + python_binary = self._python_binary |
| 89 | + |
| 90 | + def forward_search(): |
| 91 | + external_command( |
| 92 | + [python_binary, "./forward_search", pdf_file, str(line), tex_file], |
| 93 | + cwd=evince_folder, |
| 94 | + use_texpath=False, |
| 95 | + ) |
| 96 | + |
| 97 | + if bring_evince_forward or not keep_focus or not self._is_evince_running(pdf_file): |
| 98 | + external_command( |
| 99 | + ["/bin/sh", "./sync", python_binary, get_sublime_exe(), pdf_file], |
| 100 | + cwd=evince_folder, |
| 101 | + use_texpath=False, |
| 102 | + ) |
100 | 103 | if keep_focus: |
101 | 104 | self.focus_st() |
102 | 105 |
|
103 | | - time.sleep(sync_wait) |
| 106 | + linux_settings = cast(dict, get_setting("linux", {})) |
| 107 | + sync_wait = linux_settings.get("sync_wait", 1.0) |
| 108 | + sublime.set_timeout(forward_search, int(1000 * sync_wait)) |
| 109 | + |
| 110 | + else: |
| 111 | + forward_search() |
| 112 | + |
| 113 | + def view_file(self, pdf_file: str, **kwargs) -> None: |
| 114 | + keep_focus = kwargs.get("keep_focus", True) |
| 115 | + if keep_focus and self._is_evince_running(pdf_file): |
| 116 | + return |
104 | 117 |
|
105 | 118 | external_command( |
106 | | - [ |
107 | | - py_binary, |
108 | | - os.path.join(ev_path, "forward_search"), |
109 | | - pdf_file, |
110 | | - str(line), |
111 | | - tex_file, |
112 | | - ], |
| 119 | + ["/bin/sh", "./sync", self._python_binary, get_sublime_exe(), pdf_file], |
| 120 | + cwd=self._evince_folder, |
113 | 121 | use_texpath=False, |
114 | 122 | ) |
| 123 | + if keep_focus: |
| 124 | + self.focus_st() |
115 | 125 |
|
116 | | - def view_file(self, pdf_file, **kwargs): |
117 | | - keep_focus = kwargs.pop("keep_focus", True) |
118 | | - |
119 | | - if not keep_focus or not self._is_evince_running(pdf_file): |
120 | | - self._launch_evince(pdf_file) |
121 | | - if keep_focus: |
122 | | - self.focus_st() |
123 | | - |
124 | | - def supports_platform(self, platform): |
| 126 | + def supports_platform(self, platform: str) -> bool: |
125 | 127 | return platform == "linux" |
126 | 128 |
|
127 | | - def supports_keep_focus(self): |
| 129 | + def supports_keep_focus(self) -> bool: |
128 | 130 | return True |
0 commit comments