-
Notifications
You must be signed in to change notification settings - Fork 2
/
emacs_hooks.py
276 lines (214 loc) · 8.96 KB
/
emacs_hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# SPDX-License-Identifier: GPL-3.0-or-later
"""Manager for Emacs hooks."""
from PyQt6.QtCore import pyqtSlot
from qutebrowser.api import message
from qutebrowser.browser.browsertab import AbstractTab
from qutebrowser.keyinput import modeman
from qutebrowser.mainwindow.mainwindow import MainWindow
from qutebrowser.misc import objects
from qutebrowser.utils import objreg, usertypes
from qutebrowser.qt.core import QUrl
from functools import partial
from tempfile import mkstemp
import os
class EmacsHookManager:
"""Manager for Emacs hooks.
Subscribes to a set of Qt signals exposed by Qutebrowser and
forwards them to Emacs. Communication is done through the central
EmacsRPCServer instance registered in objreg.
"""
# TODO: Delete old favicon tempfiles on init
def __init__(self) -> None:
old_manager = objreg.get("emacs-hook-manager", None)
objreg.register(name="emacs-hook-manager",
obj=self,
update=True)
# Enable the window and tab hooks on startup
if objreg.window_registry:
for window in objreg.window_registry.values():
self.enable_window_hooks(window)
for tab in window.tabbed_browser.widgets():
self.enable_tab_hooks(tab, None)
# Enable new window hook
if objects.qapp:
if old_manager:
objects.qapp.new_window.disconnect(old_manager.on_new_window)
objects.qapp.new_window.connect(self.on_new_window)
def on_url_changed(self, window: MainWindow, url: QUrl) -> None:
"""Called when the URL changed.
Args:
window: The window that the change happened in.
url: The new URL.
"""
url = url.toString()
window_id = int(window.winId())
data = {"x11-win-id": window_id,
"win-id": window.win_id,
"url": url}
self.send_signal("url-changed", data)
def on_link_hovered(self, window: MainWindow, url: QUrl) -> None:
"""Called when a link is hover or unhovered.
Args:
window: The window that the link was hovered in.
url: The URL of the link that was hovered. If the event
was an unhovering, the URL is ''.
"""
window_id = int(window.winId())
data = {"x11-win-id": window_id,
"win-id": window.win_id,
"hover": url}
self.send_signal("link-hovered", data)
def on_icon_changed(self, tab: AbstractTab) -> None:
"""Called when the favicon changes.
Saves the new favicon to a tempfile and sends the filename and
X11 window ID to Emacs.
Args:
tab: The tab that the favicon changed in.
"""
window = tab.window()
window_id = int(window.winId())
fd, path = mkstemp(prefix="qutebrowser-favicon-", suffix=".png")
os.close(fd)
window.windowIcon().pixmap(16, 16).save(path)
data = {"x11-win-id": window_id,
"win-id": window.win_id,
"icon-file": path}
self.send_signal("icon-changed", data)
def on_scroll_perc_changed(self,
window: MainWindow,
x_perc: int,
y_perc: int):
"""Called when the scroll position changes.
Args:
window: The window that the scroll position changed in.
x_perc: X scroll position in percentage.
y_perc: Y scroll position in percentage.
"""
window_id = int(window.winId())
data = {"x11-win-id": window_id,
"win-id": window.win_id,
"x-scroll-perc": x_perc,
"y-scroll-perc": y_perc}
self.send_signal("scroll-perc-changed", data)
def on_recently_audible_changed(self, tab: AbstractTab) -> None:
"""Called when the audible state changes.
Args:
tab: The tab that the audible state changed in.
"""
window = tab.window()
window_id = int(window.winId())
recently_audible = tab.audio.is_recently_audible()
data = {"x11-win-id": window_id,
"win-id": window.win_id,
"recently-audible": recently_audible}
self.send_signal("recently-audible-changed", data)
def on_search(self, window: MainWindow, search: str) -> None:
"""Called when a search is performed.
Args:
window: The window that search was performed in.
search: The entered search term.
"""
window_id = int(window.winId())
data = {"x11-win-id": window_id,
"win-id": window.win_id,
"search": search}
self.send_signal("got-search", data)
def on_enter_mode(self,
window: MainWindow,
mode: usertypes.KeyMode):
"""Called when a mode is entered.
Args:
window: The window that entered a mode.
mode: The mode that was entered.
"""
window_id = int(window.winId())
data = {"x11-win-id": window_id,
"win-id": window.win_id,
"mode": str(mode)}
self.send_signal("entered-mode", data)
def on_leave_mode(self,
window: MainWindow,
mode: usertypes.KeyMode):
"""Called when a mode is left.
Args:
window: The window that left a mode.
left-mode: The mode that was left.
mode: The new mode.
"""
window_id = int(window.winId())
data = {"x11-win-id": window_id,
"win-id": window.win_id,
"left-mode": str(mode),
"mode": "KeyMode.normal"}
self.send_signal("left-mode", data)
def on_load_started(self, window: MainWindow) -> None:
"""Called when starting to load a new webpage.
Args:
window: The window that started loading.
"""
window_id = int(window.winId())
self.send_signal("load-started", {"x11-win-id": window_id,
"win-id": window.win_id})
def on_load_finished(self, window: MainWindow) -> None:
"""Called when finished loading a new webpage.
Args:
window: The window that finished loading.
"""
window_id = int(window.winId())
self.send_signal("load-finished", {"x11-win-id": window_id,
"win-id": window.win_id})
def enable_tab_hooks(self, tab: AbstractTab, _) -> None:
"""Enable tab local hooks.
Args:
tab: The tab to enable hooks for.
"""
tab.icon_changed.connect(partial(self.on_icon_changed, tab))
tab.audio.recently_audible_changed.connect(
partial(self.on_recently_audible_changed, tab))
def enable_window_hooks(self, window: MainWindow) -> None:
"""Enable window local hooks.
Args:
window: The window to enable hooks for.
"""
mode_manager = modeman.instance(window.win_id)
tabbed_browser = window.tabbed_browser
status = window.status
mode_manager.entered.connect(partial(self.on_enter_mode, window))
mode_manager.left.connect(partial(self.on_leave_mode, window))
tabbed_browser.cur_url_changed.connect(
partial(self.on_url_changed, window))
tabbed_browser.cur_link_hovered.connect(
partial(self.on_link_hovered, window))
tabbed_browser.new_tab.connect(self.enable_tab_hooks)
tabbed_browser.cur_load_started.connect(
partial(self.on_load_started, window))
tabbed_browser.cur_load_finished.connect(
partial(self.on_load_finished, window))
tabbed_browser.cur_scroll_perc_changed.connect(
partial(self.on_scroll_perc_changed, window))
status.cmd.got_search.connect(partial(self.on_search, window))
def on_new_window(self, window: MainWindow) -> None:
"""Called when a new window is created.
Responsible for setting up all window local hooks in new windows.
Args:
window: The window that was created.
"""
self.send_signal("new-window", {"x11-win-id": int(window.winId()),
"win-id": window.win_id})
self.enable_window_hooks(window)
def send_signal(self, signal: str, args: dict[str, object] = {}) -> None:
"""Send a signal to Emacs.
Args:
signal: The name of the signal to be sent.
args: The arguments to pass to the signal handler in Emacs.
"""
server = objreg.get("emacs-rpc", None)
if server:
server.send_notification(method=signal, params=args)
else:
message.info("No RPC server found! Could not send signal!")
if __name__ == "config":
EmacsHookManager()
# Local Variables:
# eval: (qutebrowser-config-mode)
# End: