Add a function to execute a named source block interactively

This commit is contained in:
Gerard Vermeulen 2024-01-24 14:50:46 +01:00
parent 6b38f282a5
commit d1f7e96f51
1 changed files with 56 additions and 10 deletions

View File

@ -2274,27 +2274,29 @@ list detailing and motivating each listing:
1. Listing [[lst:set-org-options]] handles basic [[https://orgmode.org/][Org-mode]] options.
2. Listing [[lst:setup-org-babel]] handles basic [[https://orgmode.org/][Org-mode]] options specific to
[[info:org#Working with Source Code][work with source code (info)]].
3. Listing [[lst:ob-tangle-plus]] extends [[info:org#Noweb Reference Syntax][source code export (info)]] with the
3. Listing [[lst:org-babel-execute-named-block]] adds a function to execute a named
source block interactively.
4. Listing [[lst:ob-tangle-plus]] extends [[info:org#Noweb Reference Syntax][source code export (info)]] with the
possibility to tangle of for instance Emacs Lisp files (or other programming
mode files) into a directory specified by ~org-babel-post-tangle-dir~ which
can be changed using =org-get-entry= to read lines like ~#+property:
tangle-dir: ANY-PLACE~ near the beginning an Org file.
4. Listing [[lst:set-org-link-options]] handles [[https://orgmode.org/][Org-mode]] options specific to
5. Listing [[lst:set-org-link-options]] handles [[https://orgmode.org/][Org-mode]] options specific to
[[info:org#Hyperlinks][hyperlinks (info)]].
5. Listing [[lst:setup-org-mode-map-1]] and [[lst:setup-org-mode-map-2]] extend the
6. Listing [[lst:setup-org-mode-map-1]] and [[lst:setup-org-mode-map-2]] extend the
=org-mode-map= with useful key-bindings.
6. Listing [[lst:setup-org-src]] facilitates [[info:org#Editing Source Code][editing source code blocks]], and it
7. Listing [[lst:setup-org-src]] facilitates [[info:org#Editing Source Code][editing source code blocks]], and it
provides functions to change the indentation of all =org-src-mode= blocks.
7. Listing [[lst:setup-ob-python]] allows to pretty-print Python session source
8. Listing [[lst:setup-ob-python]] allows to pretty-print Python session source
block values with [[https://github.com/psf/black#readme][black]] instead of [[https://docs.python.org/3/library/pprint.html][pprint]]. This snippet may break in the
future, because it sets =org-babel-python--def-format-value= which is a
constant declared "private" by two dashes in its name!
8. Listing [[lst:set-org-export-options]] selects the =non-intrusive= expert user
9. Listing [[lst:set-org-export-options]] selects the =non-intrusive= expert user
interface for export dispatching.
9. Listing [[lst:setup-org-for-lualatex-export]] and
[[lst:set-ox-latex-options-for-lualatex-export]] configure [[https://orgmode.org/][Org-mode]] to generate
output for the LuaLaTeX compiler.
10. Listing [[lst:setup-org-latex-classes]] defines [[info:org#LaTeXspecificexportsettings][org-latex-classes (info)]] for
10. Listing [[lst:setup-org-for-lualatex-export]] and
[[lst:set-ox-latex-options-for-lualatex-export]] configure [[https://orgmode.org/][Org-mode]] to generate
output for the LuaLaTeX compiler.
11. Listing [[lst:setup-org-latex-classes]] defines [[info:org#LaTeXspecificexportsettings][org-latex-classes (info)]] for
backward compatibility. See table [[tab:org-latex-class-tag-placeholder]] and
type {{{kbd(C-h v org-latex-classes)}}} for an explanation of the code in
listing [[lst:setup-org-latex-classes]].
@ -2365,6 +2367,50 @@ list detailing and motivating each listing:
(setopt org-babel-lisp-eval-fn #'sly-eval)))
#+end_src
#+caption[Add function to execute a named source block interactively]:
#+caption: Add function to execute a named source block interactively.
#+name: lst:org-babel-execute-named-block
#+begin_src emacs-lisp -n :results silent
(with-eval-after-load 'ob-core
(defun org-babel--source-block-names (&optional language)
"Return an alist of source block name strings and language symbols.
When LANGUAGE is non-nil, restrict the alist to named LANGUAGE blocks."
(when (stringp language)
(if (string= language "elisp")
(setq language nil)
(setq language (intern-soft language)))
(unless language
(user-error "Correct the LANGUAGE string or use a LANGUAGE symbol")))
(let (result)
(save-excursion
(goto-char (point-min))
(while (re-search-forward org-babel-src-block-regexp nil t)
(when (org-babel-active-location-p)
(when-let ((lang (intern-soft (match-string 2)))
(name (org-element-property :name (org-element-at-point))))
(when (or (not language) (eq lang language))
(push (cons name lang)
result))))))
(reverse result)))
(defun org-babel-execute-named-block (&optional arg)
"Execute a named source block after prompting for a source block name.
When prefix ARG is non-nil, prompt first for a language."
(interactive "P")
(let (language)
(when arg
(setq language (completing-read "language: " org-babel-load-languages)))
(if-let* ((names (org-babel--source-block-names language))
(name (completing-read "name: " names)))
(if (cl-remove-if-not (lambda (pair) (string= name (car pair))) names)
(save-excursion
(org-babel-goto-named-src-block name)
(org-babel-execute-maybe))
(user-error "There is no named %S of language %S block"
name (or language "any-org-babel-load-language")))
(user-error "There are no named language %S blocks" language)))))
#+end_src
#+caption[Change the =org-babel-tangle= destination to a specific directory]:
#+caption: Change the =org-babel-tangle= destination to a specific directory.
#+name: lst:ob-tangle-plus