From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id cE2tNSdbwl9aKAAA0tVLHw (envelope-from ) for ; Sat, 28 Nov 2020 14:13:59 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id gEeOMSdbwl+uOAAAB5/wlQ (envelope-from ) for ; Sat, 28 Nov 2020 14:13:59 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 375799404C4 for ; Sat, 28 Nov 2020 14:13:59 +0000 (UTC) Received: from localhost ([::1]:46034 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kj0z8-00072F-6w for larch@yhetil.org; Sat, 28 Nov 2020 09:13:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:38206) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kj0yZ-000728-Bd for emacs-orgmode@gnu.org; Sat, 28 Nov 2020 09:13:23 -0500 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]:33175) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kj0yW-0008VT-Su for emacs-orgmode@gnu.org; Sat, 28 Nov 2020 09:13:23 -0500 Received: by mail-pf1-x42a.google.com with SMTP id q10so6962690pfn.0 for ; Sat, 28 Nov 2020 06:13:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=andrew-cmu-edu.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0EctzPCHWZ1RL6gm5MgpxHA4uib9oa48sYRePG2VsEc=; b=PvOuXwAsIdaTL9mm/ZCedXbyhtYQB5wC0oHDuTW31JmsP5WMQwhCeictQxfEE3w0zF rAMBQ6iGvpStXBwtD1JKv2buCCcJwVMIUHml375VRxN73DYn0HC465kZRqGpmEcitnBp OnIFWubNKmRLsAVWlcZgItSGoCr4gMqCUdFteLtrlD2ycFdMqzS1uwNYgHxJbjgToqlB 2GAzaylHyG1FqMbRQTO6fB1v7hVolIllG8C/O0Czc0QyihImXbKtYvGIAUdWdMKLKnsr OIL7nkyOKFquvY2gtt8ulYcooWVIznRoAI9wVEPwF6gB35VWIHCe5yTNIY6Pfbwraw/0 ARnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0EctzPCHWZ1RL6gm5MgpxHA4uib9oa48sYRePG2VsEc=; b=jBbHzg0WtxvNsNRz+4Xab35uwlRmpoh8hl8WCgHeJzfbCGSk7C9ivaAG/zgMrT3+jK iqxca9BHg12WyUAHjV/CCuW/dSwTVngkCPRzCXLUHaLg1RGF8JrGcJq/bDZsH/qJVCtA gh2tR61ecFdTVKSaFUG+hti4CqeXmEsN7+n1T2IG7pXnszjzTc491/fesKws9RjCaJv9 9NDlVVfzWZ2xxP5aiEmTCXzaO59y+hjUWNYUhwtD2tbB1pxH2uYvy1ZnO6Vk6d+S/CMi q8mztxhbffsAwRx0uNAZc2EunKDdbztMS+wIUKRfqJWYaTfmvNhfRsD6351BC1qQivJF Ermg== X-Gm-Message-State: AOAM531ku3ni/glXWTuBh0TiPwH+m3Mq89znwqP3AciEnNOiFt2oP3yd SWZSCoXCCk6d4MPbKRrWTsc7SAQ6LgZBRM0e/zY= X-Google-Smtp-Source: ABdhPJzRa9zJdL2Jj5ZjzeBCgv6Sq5QIh/EtGH8CS6orS/iYc+fcajtnRiRfc7wCgzTu+/+gqvy3sjIlsGILpGGGZEc= X-Received: by 2002:a62:1c88:0:b029:197:f6e4:bc2b with SMTP id c130-20020a621c880000b0290197f6e4bc2bmr11194616pfc.6.1606572798729; Sat, 28 Nov 2020 06:13:18 -0800 (PST) MIME-Version: 1.0 References: <87y2imqkxt.fsf@kyleam.com> In-Reply-To: From: John Kitchin Date: Sat, 28 Nov 2020 09:13:08 -0500 Message-ID: Subject: =?UTF-8?Q?Re=3A_How_to_get_a_block=E2=80=99s_contents_by_name?= To: gmauer@gmail.com Content-Type: multipart/alternative; boundary="000000000000f9629a05b52b60e5" Received-SPF: pass client-ip=2607:f8b0:4864:20::42a; envelope-from=johnrkitchin@gmail.com; helo=mail-pf1-x42a.google.com X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.25, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kyle Meyer , emacs-orgmode Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -2.37 X-Scanner: ns3122888.ip-94-23-21.eu Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=andrew-cmu-edu.20150623.gappssmtp.com header.s=20150623 header.b=PvOuXwAs; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=andrew.cmu.edu (policy=none); spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-TUID: c1/CU7h/KKFz --000000000000f9629a05b52b60e5 Content-Type: text/plain; charset="UTF-8" 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 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 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)) >> > --000000000000f9629a05b52b60e5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I would do this:

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

T= his 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 alter= native that involves parsing the buffer, especially for large buffers.
John

---= --------------------------------
Professor John Kitchin=C2=A0
Doherty= Hall A207F
Department of Chemical Engineering
Carnegie Mellon Univer= sity
Pittsburgh, PA 15213
412-268-7803

On Fri, Nov 27, 2020 at 10:47 PM George Mauer <gmauer@gmail.com> wrote:
Well that pains me on a = software-engineer-aip-design level but that works! Thanks a lot.

<= div class=3D"gmail_quote">
On Fri, Nov= 27, 2020 at 8:22 PM Kyle Meyer <kyle@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, (id= eally
> 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?

=C2=A0 (save-excursion
=C2=A0 =C2=A0 (goto-char (org-babel-find-named-block "b"))
=C2=A0 =C2=A0 (org-babel-expand-src-block))
--000000000000f9629a05b52b60e5--