如何在 Emacs Lisp 函数中等待事件?

发布于 2024-12-05 09:36:25 字数 520 浏览 0 评论 0原文

我正在尝试编写最简单的功能:向 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-searchset-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是我以为 2024-12-12 09:36:25

在 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.

淑女气质 2024-12-12 09:36:25

以防万一,如果有人有相同的想法,这就是我最终得到的感谢罗斯:

(defun w3m-goto-on-load (url)
  "Go to a position after page has been loaded."
  (cond
    ((string-match "domain" url)
      (progn
        (set-window-start nil (search-forward "Search" nil t) nil)))
    (t nil)))
(add-hook 'w3m-display-hook 'w3m-goto-on-load)

其中 "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:

(defun w3m-goto-on-load (url)
  "Go to a position after page has been loaded."
  (cond
    ((string-match "domain" url)
      (progn
        (set-window-start nil (search-forward "Search" nil t) nil)))
    (t nil)))
(add-hook 'w3m-display-hook 'w3m-goto-on-load)

where "domain" is a keyword in URL to match and "Search" is the unique string to jump to. Certainly, search-forward can be replaced with re-search-forward, if more flexible search is required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文