From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id eG7XHtVbw1+GegAA0tVLHw (envelope-from ) for ; Sun, 29 Nov 2020 08:29:09 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id gO6UGtVbw18kXQAAbx9fmQ (envelope-from ) for ; Sun, 29 Nov 2020 08:29:09 +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 36CFC94030E for ; Sun, 29 Nov 2020 08:29:07 +0000 (UTC) Received: from localhost ([::1]:39506 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kjI4w-00035Y-Go for larch@yhetil.org; Sun, 29 Nov 2020 03:29:06 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:57866) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kjI4B-00034w-59 for emacs-orgmode@gnu.org; Sun, 29 Nov 2020 03:28:19 -0500 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]:39239) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kjI47-0005pt-Ha for emacs-orgmode@gnu.org; Sun, 29 Nov 2020 03:28:18 -0500 Received: by mail-ej1-x62f.google.com with SMTP id pg6so4322359ejb.6 for ; Sun, 29 Nov 2020 00:28:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zzamboni-org.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=OP2a3xomFQLkl4UBsNf5kx8oERdv87FuxIgE8+ToJ7Y=; b=FHSdQewrRVCjZNVw+v1j2vSWnSk5HYAvlKL0NIMIndDIdeqrteTazsHaAa6O+PGQw2 QYXohM2MiXA3HvkPJGMXyLhAYA3xhecYdt2DPXGz79e/GbVZvp+1/HxEKwuTf3eZ78H1 XmY0v7P/40ftSd94sNqwt2dU5y4p3kZiLUAfdV2cfbklbVG+nETH8Vtp3glZOdBILgXq 4pXuaYJCoU73FsF/kHPHea8J6Fxyr5wyMcaKay5eXtBNtRcCBGn4QA0ADDZsoJUH5fKk GMqHk67bqKwxzVJzAFtQbYENyQtB2b504Af3S71e3pTejCAcW/0RG8gRur5/GvAcFUSp Fxwg== 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=OP2a3xomFQLkl4UBsNf5kx8oERdv87FuxIgE8+ToJ7Y=; b=I2+7CqSGzK+bBqv6KkcuFpGLcTvQF0mIqKUVLKi5492AFc/xBBTq70lM9gFlPDMAWQ yGWiujMjMGh54eH9ssExSZebkoG1Tb3mz2tuAWXOdw+rE5rfz0evFMcXDpdIPV+eqElS 9qFOvrKN4g8/3AgZuXjWHUqmLfKjKLDiBRmnFttngT6L2O5F9J8NKVtxz1Z/yu1pU+Mr scWD0TzP86EsjT/V8Qw6y2WBW2mg47QXqcDZZ1XnSbdNIS1yjTbwiSGKWNeLHalGgjxX 6osIcp0KVBzhWAHUP9NGkj4V9kRuqTzeV2vyaf/9tB9E5VSnYrFjCbymFeFfaCUq0/XP woMA== X-Gm-Message-State: AOAM532GgPGG5WAJK70n4xzhT0LPoDIMezq3AC73p8LJS+sowLPngsqN gSR1N5xm79XMlkt7We50xmWMH60TmGo7poQeLY6kxQ== X-Google-Smtp-Source: ABdhPJxrLbBJAWO8E2l1Y5WTxRt3AU+Dq9uctFehb/2u39rPkDlac23xABkvMl0YPWrwJ4nz9+ucI17JUrvkgE7ZFrU= X-Received: by 2002:a17:906:2642:: with SMTP id i2mr14891901ejc.493.1606638492972; Sun, 29 Nov 2020 00:28:12 -0800 (PST) MIME-Version: 1.0 References: <2PnBr0V8kH99j60xzpYf0HgMQZZtQ_FTnrNNhbHxzgTnxuJqv3y_tSGDNcv4Pqzvlfy4RLgmUbw83UIRiJXTgV9-_fDlwJOaCIon3zJo_-E=@protonmail.com> In-Reply-To: <2PnBr0V8kH99j60xzpYf0HgMQZZtQ_FTnrNNhbHxzgTnxuJqv3y_tSGDNcv4Pqzvlfy4RLgmUbw83UIRiJXTgV9-_fDlwJOaCIon3zJo_-E=@protonmail.com> From: Diego Zamboni Date: Sun, 29 Nov 2020 09:28:02 +0100 Message-ID: Subject: Re: Multiple named code blocks To: mooss Content-Type: multipart/alternative; boundary="000000000000a80e4705b53aacf0" Received-SPF: none client-ip=2a00:1450:4864:20::62f; envelope-from=diego@zzamboni.org; helo=mail-ej1-x62f.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham 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: "emacs-orgmode@gnu.org" Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -2.47 X-Scanner: ns3122888.ip-94-23-21.eu Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=zzamboni-org.20150623.gappssmtp.com header.s=20150623 header.b=FHSdQewr; dmarc=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: Zq0ISDCTfqRQ --000000000000a80e4705b53aacf0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Felix, You need to use the noweb-ref header argument instead of #+NAME, then the block are all concatenated on output. Best, --Diego On Sat, 28 Nov 2020 at 23:35, mooss wrote: > Hi, > > I have been using org-mode for almost three years and I loved it so much > that I started working on a literate programming tool based on it. > One particular technique that I use is having multiple named code blocks, > like so: > > #+begin_src perl :noweb yes :results output > <> > sub foo { > <> > } > foo; > #+end_src > > #+name: Before foo > #+begin_src perl > print "Before foo definition.\n"; > #+end_src > > #+name: Before foo > #+begin_src perl > my $value =3D 'Inside foo call'; > #+end_src > > #+name: In foo > #+begin_src perl > print $value . "\n"; > #+end_src > > This technique worked without issue until I recently updated Emacs and > org-mode with it. > I do not know the version of org-mode I was using before, but this was > with Emacs 26.3 and I upgraded to Emacs 27.1 with org-mode 9.4 according = to > the information at the top of elpa/org-plus-contrib-20201116/org.el. > > Before the update, the code block after expansion (obtained with > org-babel-expand-src-block via the C-c C-v C-v shortcut) looked like this= : > #+begin_src perl > print "Before foo definition.\n"; > my $value =3D 'Inside foo call'; > sub foo { > print $value . "\n"; > } > foo; > #+end_src > > Now the definition of $value is gone: > #+begin_src perl > print "Before foo definition.\n"; > sub foo { > print $value . "\n"; > } > foo; > #+end_src > > I have looked at the info manual so I realise that according to "15.2 > Structure of Code Blocks": "For duplicate names, Org mode=E2=80=99s behav= ior is > undefined" so it follows that: > - Up until now, I was incorrectly assuming that duplicated named code > blocks were supposed to result in them being concatenated in the noweb > expansion phase. > - This is not a bug report, org-mode is working as documented. > > I find this technique pretty useful for two reasons: > 1. Importing packages right when they are needed. > 2. Declaring variables in a broader scope than the one where they are > first used. > Here is an short example of this kind of situation: > > #+begin_src perl :noweb no > # Expansion of <>: > my $even_counter =3D 0; > my @array =3D (4, 8, 15, 16, 23, 42); > # A > # lot > # of > # other > # code > # [...] > foreach my $n (@array) { > # Expansion of <>: > $even_counter++ if $n % 2 =3D=3D 0; > } > print "$even_counter"; > #+end_src > > In this example, $even_counter could not have been declared on the > spot. > Of course, this example is too basic to really paint the usefulness o= f > this technique but an actual example would be too long, the goal here is > just to explain the general idea. > > With all that being said I would suggest to define the behaviour for > multiple named code blocks as resulting in a concatenation of the code > blocks, in the order of their apparition. > If you agree about defining this behaviour but think adapting the > implementation is of low priority, I could try to implement it myself > though I have little experience in emacs-lisp development beyond basic > configuration and no experience whatsoever in contributing to FOSS, but I= 'm > willing to start in both domains. > > Best regards, > F=C3=A9lix > > --000000000000a80e4705b53aacf0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Felix,

You need to use the noweb-ref header argument instead of #+NAME, then t= he block are all concatenated on output.

<= div dir=3D"auto">Best,
--Diego=C2=A0


On Sat, 28 Nov 2020 at 23:35, mooss <mooss@protonmail.com> wrote:
Hi,

I have been = using org-mode for almost three years and I loved it so much that I started= working on a literate programming tool based on it.
One part= icular technique that I use is having multiple named code blocks, like so:<= br>

#+begin_src perl :noweb yes :results output
<<Before foo>>
sub foo {
=C2=A0=C2=A0=C2=A0 <<In foo>>
}
f= oo;
#+end_src

#+name: Before foo=
#+begin_src perl
print "Before foo defini= tion.\n";
#+end_src

#+name:= Before foo
#+begin_src perl
my $value =3D '= ;Inside foo call';
#+end_src

#+name: In foo
#+begin_src perl
print $value .= "\n";
#+end_src

This = technique worked without issue until I recently updated Emacs and org-mode = with it.
I do not know the version of org-mode I was using be= fore, but this was with Emacs 26.3 and I upgraded to Emacs 27.1 with org-mo= de 9.4 according to the information at the top of elpa/org-plus-contrib-202= 01116/org.el.

Before the update, the code bloc= k after expansion (obtained with org-babel-expand-src-block via the C-c C-v= C-v shortcut) looked like this:
#+begin_src perl
print "Before foo definition.\n";
my $value =3D = 'Inside foo call';
sub foo {
=C2=A0=C2= =A0=C2=A0 print $value . "\n";
}
foo;=
#+end_src

Now the definition of= $value is gone:
#+begin_src perl
print "B= efore foo definition.\n";
sub foo {
=C2=A0= =C2=A0=C2=A0 print $value . "\n";
}
f= oo;
#+end_src

I have looked at t= he info manual so I realise that according to "15.2 Structure of Code = Blocks": "For duplicate names, Org mode=E2=80=99s behavior is und= efined" so it follows that:
- Up until now, I was incorr= ectly assuming that duplicated named code blocks were supposed to result in= them being concatenated in the noweb expansion phase.
- This= is not a bug report, org-mode is working as documented.

=
I find this technique pretty useful for two reasons:
1. Importing packages right when they are needed.
2. Decla= ring variables in a broader scope than the one where they are first used.
=C2=A0=C2=A0=C2=A0 Here is an short example of this kind of si= tuation:

=C2=A0=C2=A0=C2=A0 #+begin_src perl := noweb no
=C2=A0=C2=A0=C2=A0 # Expansion of <<Variable d= eclarations>>:
=C2=A0=C2=A0=C2=A0 my $even_counter =3D = 0;
=C2=A0=C2=A0=C2=A0 my @array =3D (4, 8, 15, 16, 23, 42);
=C2=A0=C2=A0=C2=A0 # A
=C2=A0=C2=A0=C2=A0 # lot<= br>
=C2=A0=C2=A0=C2=A0 # of
=C2=A0=C2=A0=C2=A0 # ot= her
=C2=A0=C2=A0=C2=A0 # code
=C2=A0=C2=A0=C2= =A0 # [...]
=C2=A0=C2=A0=C2=A0 foreach my $n (@array) {
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # Expansion of <<= Array processing>>:
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 $even_counter++ if $n % 2 =3D=3D 0;
=C2=A0=C2=A0=C2= =A0 }
=C2=A0=C2=A0=C2=A0 print "$even_counter";
=
=C2=A0=C2=A0=C2=A0 #+end_src

=C2=A0= =C2=A0=C2=A0 In this example, $even_counter could not have been declared on= the spot.
=C2=A0=C2=A0=C2=A0 Of course, this example is too = basic to really paint the usefulness of this technique but an actual exampl= e would be too long, the goal here is just to explain the general idea.
=

With all that being said I would suggest to defin= e the behaviour for multiple named code blocks as resulting in a concatenat= ion of the code blocks, in the order of their apparition.
If = you agree about defining this behaviour but think adapting the implementati= on is of low priority, I could try to implement it myself though I have lit= tle experience in emacs-lisp development beyond basic configuration and no = experience whatsoever in contributing to FOSS, but I'm willing to start= in both domains.

Best regards,
= F=C3=A9lix

--000000000000a80e4705b53aacf0--