3

I want to execute selected source blocks in the active buffer. For example for the following org-mode file,

#+NAME: snippet1
#+BEGIN_SRC emacs-lisp
    (message "snippet 1")
#+END_SRC

#+NAME: snippet2
#+BEGIN_SRC emacs-lisp
    (message "snippet 2")
#+END_SRC

I can use

(org-babel-execute-buffer)

to execute both blocks from elisp, but how can I execute snippet1 without executing snippet2?

I found the following related functions that might help.

org-babel-get-src-block-info
org-babel-execute-src-block

Any pointers to relevant documentation is very much appreciated.

2 Answers 2

3

The documentation for org-babel is available at http://org-babel.readthedocs.io/en/latest/. And the snippet to interactively select and execute a named block in the current buffer:

(save-excursion
  (goto-char
   (org-babel-find-named-block
    (completing-read "Code Block: " (org-babel-src-block-names))))
  (org-babel-execute-src-block-maybe))
1
  • I just replaced "(completing-read "Code Block: " (org-babel-src-block-names))))" with "my-block-name" since I didn't need the interactive part. Thanks for the snippet it works great now :).
    – user14419
    May 14, 2017 at 15:46
1

Much easier than the AA: Using the built-in function (org-sbe ...:

* Example using (org-sbe):
#+NAME: snippet1
#+BEGIN_SRC emacs-lisp :var txt="snippet1 was called"
    (message txt)
#+END_SRC

If you want to run the above SCB, you could just place
the curson on the closing bracket ")" of “(org-sbe "snippet1")”,
and just run that with CTRL-x CTRL-e , and here once
more “(org-sbe "snippet1" (txt $"otherMessage"))” with parameters.
Documentation e.g. here:
http://doc.endlessparentheses.com/Fun/org-sbe.html

Your Answer

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

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