如何在 Emacs Lisp 函数中等待事件?
我正在尝试编写最简单的功能:向 w3m 浏览器发送查询,然后在网页上查找特定位置:
(defun w3m-define-word (word)
(interactive "sDefine: ")
(progn (w3m-search "Dictionary" word)
(set-window-start nil (search-forward "Search Results"))))
这里的问题是 w3m-search
不会等到页面重新加载并且 < code>set-window-start 在旧页面上执行。然后页面重新加载并将光标放置在缓冲区的开头。
w3m-search
和 set-window-start
之间的 (sleep-for ..)
有帮助,但由于加载时间是任意的,所以不是非常方便。
我如何重写这个函数,以便它会等到缓冲区重新加载,然后才执行其余的操作?
I'm trying to write the simplest function: send a query to w3m browser and then find a particular place on the webpage:
(defun w3m-define-word (word)
(interactive "sDefine: ")
(progn (w3m-search "Dictionary" word)
(set-window-start nil (search-forward "Search Results"))))
What is wrong here is that w3m-search
does not wait until page reloads and set-window-start
executes on the older page. Then the page reloads and places a cursor at the beginning of the buffer.
(sleep-for ..)
between w3m-search
and set-window-start
helps, but since loading time is arbitrary, it is not very convenient.
How can I rewrite this function, so it would wait until buffer reloads and only then do the rest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 elisp 中完成此操作的方法是使用 hooks。因此,您需要查看加载页面时 w3m 是否调用挂钩。如果是这样,那么您可以为该钩子注册一个钩子函数来执行您想要的操作。
看起来
Ch v w3m-display-hook RET
就是您要找的。这是一个很好的例子< /a> 开始。The way to accomplish this in elisp is using hooks. So you'd need to see if w3m calls a hook when the page is loaded. If so then you can register a hook function for that hook that does what you want.
It looks like
C-h v w3m-display-hook RET
is what you're looking for. Here's a good example to start from.以防万一,如果有人有相同的想法,这就是我最终得到的感谢罗斯:
其中
"domain"
是 URL 中要匹配的关键字,"Search"
是要跳转到的唯一字符串。当然,如果需要更灵活的搜索,可以将search-forward
替换为re-search-forward
。Just in case if anyone has same ideas, that's what I have ended up with thanks to Ross:
where
"domain"
is a keyword in URL to match and"Search"
is the unique string to jump to. Certainly,search-forward
can be replaced withre-search-forward
, if more flexible search is required.