Skip to content
Takeshi Teshima edited this page Dec 3, 2019 · 6 revisions

Welcome to the org-babel-eval-in-repl wiki!

Example org-mode files

Async shell code blocks (with multiple sessions)

#+BEGIN_SRC sh :session *sh1*
echo 1
#+END_SRC

#+BEGIN_SRC sh :session *sh1*
echo 2
#+END_SRC

To enable multiple sessions, you may need to update the eval-in-repl package.

Example configurations (keybindings)

Example 1. More integrated to org-mode

If you write the following code in your .emacs.d:

  • C- will run the code under the cursor if in a source or example block.
  • C- will work the same as usual if outside a source or example block.
  • M- will run the block under the cursor if in a source or example block.
  • M- will work the same as usual if outside a source or example block.
  (defun org-ctrl-return-around (org-fun &rest args)
    "Run `ober-eval-in-repl' if in source code block and `org-insert-heading-respect-content' otherwise."
    (if (org-in-block-p '("src" "example"))
        (ober-eval-in-repl)
      (apply org-fun args)))
  (advice-add 'org-insert-heading-respect-content :around #'org-ctrl-return-around)

  (defun org-meta-return-around (org-fun &rest args)
    "Run `ober-eval-block-in-repl' if in source code block or example block and `org-meta-return' otherwise."
    (if (org-in-block-p '("src" "example"))
        (ober-eval-block-in-repl)
      (apply org-fun args)))
  (advice-add 'org-meta-return :around #'org-meta-return-around)

Thanks to @theldoria!

Example 2. One magic key

Copy the following and M-<return> will be the magic key (see function comment).

  (defun org-meta-return-around (org-fun &rest args)
    "Run `ober-eval-in-repl' if in source code block,
`ober-eval-block-in-repl' if at header,
and `org-meta-return' otherwise."
    (if (org-in-block-p '("src"))
        (let* ((point (point))
               (element (org-element-at-point))
               (area (org-src--contents-area element))
               (beg (copy-marker (nth 0 area))))
          (if (< point beg)
              (ober-eval-block-in-repl)
            (ober-eval-in-repl)))
      (apply org-fun args)))
  (advice-add 'org-meta-return :around #'org-meta-return-around)

Thanks to @theldoria!