I would do this:

#+BEGIN_SRC emacs-lisp
(defun org-get-named-block-contents (name)
  (save-excursion
    (goto-char (point-min))
    (let ((regexp (org-babel-named-src-block-regexp-for-name name)))
      (or (and (looking-at regexp)
      (progn (goto-char (match-beginning 1))
     (line-beginning-position)))
 (ignore-errors (org-next-block 1 nil regexp)))
      (org-babel-expand-src-block))))
#+END_SRC

This is basically what org-babel-find-named-block does, with the
(org-babel-expand-src-block) return instead of the position. It avoids the
double save-excursion.

This is probably faster than any alternative that involves parsing the
buffer, especially for large buffers.
John

-----------------------------------
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu



On Fri, Nov 27, 2020 at 10:47 PM George Mauer <gma...@gmail.com> wrote:

> Well that pains me on a software-engineer-aip-design level but that works!
> Thanks a lot.
>
> On Fri, Nov 27, 2020 at 8:22 PM Kyle Meyer <k...@kyleam.com> wrote:
>
>> George Mauer writes:
>>
>> > I'm trying to figure out how I could fetch the contents of another
>> block by
>> > name from an elisp script
>> >
>> > I've seen `org-sbe` but I just want to get the block contents, (ideally
>> > with noweb and vars filled in - just as it would be tangled if we were
>> to
>> > tangle it)
>> >
>> > How do I do that?
>>
>> How about something like this?
>>
>>   (save-excursion
>>     (goto-char (org-babel-find-named-block "b"))
>>     (org-babel-expand-src-block))
>>
>

Reply via email to