0

I'm trying to run a hook that should start after buffers are loaded - it hooks into ein package that starts an "ipython server" that takes some time to start and it seems that the subsequent functions don't "await" for it.

Which makes me wonder if there is any way to have async/await behavior in emacs, so that the next function runs only after the buffers are ready:

(add-hook 'ein:ipynb-mode-hook
          (lambda ()
            (cl-letf (((symbol-function 'read-directory-name)
                       (lambda (_prompt dir &rest _args) dir)))
              (ein:process-find-file-callback)
              (mapc 'switch-to-buffer
                     (cl-remove-if-not (lambda (b)
                      (and
                       (string-match-p "ein" (buffer-name b))
                       (string-match-p "ipynb" (buffer-name b))))
                    (buffer-list)))
              (delete-other-windows)
                )))

1 Answer 1

1

Buffers don’t have a “ready” state, and don’t take time to load. You’re not waiting on the buffer, you are waiting for some Jupyter process to start up and accept the connection from ein.

ein already has a system of callbacks for actions that are to be taken once it has successfully opened the notebook. If you look at the source code, ein:process-find-file-callback just calls ein:process-open-notebook, which takes a callback argument. You should call it directly, and supply your own callback. Another possibility is to extend ein:process-find-file-callback to take a callback argument which its own callback can call.

5
  • Thank you very much for looking into this issue holistically. That is very helpful, I will try that again. May 30 at 10:18
  • Sorry, but this didn't work: pastebin.com/hH8MKkrG . It keeps behaving the same as before (callback code is being ignored). I think that on this level I should commission an experienced emacs programmer to do it professionally - if you knew anyone who might be willing to try please let me know. May 30 at 11:11
  • ein:process-open-notebook takes different arguments than you gave it. It takes a filename and callback function; you gave it a list of buffers. Always use C-h f to check out a new function before you assume you know what it does. The call should look like (ein:process-open-notebook nil (lambda (&rest _args) (your code here)))
    – db48x
    May 30 at 19:03
  • Oh wow, you are right, it is working now. Amazing, thank you very much! It still has some issues, like on the first start it doesn't pick it up, but on the subsequent ones it does, but it's good to finally have some interaction with this system May 30 at 19:24
  • 1
    You may need someone more familiar with ein specificially to help you with that, but I’m glad you’re making progress.
    – db48x
    May 30 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.