Skip to content

Commit 194f15c

Browse files
authored
babel: Support including blocks from the library of babel (#101)
Fixes #100
1 parent 29f912c commit 194f15c

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

rustic-babel.el

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ results block ? By default we display it as separate popup."
4545
When passing a toolchain to a block as argument, this variable won't be
4646
considered."
4747
:type '(choice (string :tag "String")
48-
(const :tag "Nil" nil))
48+
(const :tag "Nil" nil))
4949
:group 'rustic-babel)
5050

5151
(defvar rustic-babel-buffer-name '((:default . "*rust-babel*")))
@@ -146,17 +146,17 @@ execution with rustfmt."
146146
(rustic-babel-build-update-result-block result))
147147
(rustic-with-spinner rustic-babel-spinner nil nil)
148148
(if rustic-babel-display-error-popup
149-
(if (= (length (with-current-buffer proc-buffer (buffer-string))) 0)
150-
(kill-buffer proc-buffer)
151-
(pop-to-buffer proc-buffer))
149+
(if (= (length (with-current-buffer proc-buffer (buffer-string))) 0)
150+
(kill-buffer proc-buffer)
151+
(pop-to-buffer proc-buffer))
152152
(if (> (length (with-current-buffer proc-buffer (buffer-string))) 0)
153153
(progn
154154
(with-current-buffer proc-buffer
155-
(save-excursion
156-
(save-match-data
157-
(goto-char (point-min))
158-
(setq result (buffer-string))
159-
(rustic-babel-run-update-result-block result))))
155+
(save-excursion
156+
(save-match-data
157+
(goto-char (point-min))
158+
(setq result (buffer-string))
159+
(rustic-babel-run-update-result-block result))))
160160
(kill-buffer proc-buffer))
161161
(kill-buffer proc-buffer)))))))
162162

@@ -342,9 +342,20 @@ executed with the parameter `:include'."
342342
(defun rustic-babel-block-contents (block-name)
343343
"Return contents of block with the name BLOCK-NAME"
344344
(with-current-buffer (current-buffer)
345-
(save-excursion
346-
(org-babel-goto-named-src-block block-name)
347-
(org-element-property :value (org-element-at-point)))))
345+
(cond
346+
;; Block exists in current file
347+
((org-babel-find-named-block block-name)
348+
(save-excursion
349+
(org-babel-goto-named-src-block block-name)
350+
(org-element-property :value (org-element-at-point))))
351+
;; Block exists in library of babel
352+
;; (see https://orgmode.org/manual/Library-of-Babel.html)
353+
((nth 2 (assoc-string block-name org-babel-library-of-babel)))
354+
;; Fallback
355+
(t
356+
(progn
357+
(message "included source code block `%s' not found in this buffer or the library of babel" block-name)
358+
nil)))))
348359

349360
(defun rustic-babel-insert-mod (mods)
350361
"Build string with module declarations for MODS and return it."

0 commit comments

Comments
 (0)