pythonemacsjupyterhookorg-mode

Evaluating a function definition with `C-M-x` versus using a hook


The below functions enable the use of eglot in org jupyter src blocks (after https://github.com/joaotavora/eglot/issues/216#issuecomment-1051931693)

(defun org-babel-edit-prep:jupyter-python (babel-info)
  (setq-local buffer-file-name (expand-file-name (->> babel-info caddr (alist-get :tangle))))
  (eglot-ensure))
(defun mb/org-babel-edit:jupyter-python ()
    (interactive)
    (org-babel-tangle '(4))
    (org-edit-special))

Opening an org file and executing the functions in the scratch buffer, works like a charm, but I would like to use this approach in my init file. In my naive approach, I figured I place the functions on a hook like this:

(add-hook 'org-mode-hook 'org-babel-edit-prep:jupyter-python)
(add-hook 'org-mode-hook 'mb/org-babel-edit:jupyter-python)

Alas, this is not the same as using C-M-x and does not work. Worse, it screws up fontlocking.


Solution

  • It turns out that it is unnecessary to adapt the original solution (see link in OP) to jupyter-python src blocks since setting:

    (setq org-babel-jupyter-override-src-block "python")
    

    will treat a python src block similar to a jupyter-python src block. So the following will start eglot as expected when editing python-src blocks even so we use the jupyter-python backend.

    (setq org-babel-jupyter-override-src-block "python")
    (defun org-babel-edit-prep:python (babel-info)
        (setq-local buffer-file-name (expand-file-name (->> babel-info caddr (alist-get :tangle))))
        (eglot-ensure))
    (defun mb/org-babel-edit:python ()
          (interactive)
          (org-babel-tangle '(4))
          (org-edit-special))