emacselispw3m

How to wait for an event in Emacs Lisp function?


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?


Solution

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