From mboxrd@z Thu Jan 1 00:00:00 1970 From: Philip Hudson Subject: Re: Tangling flow control Date: Wed, 9 Nov 2016 20:32:02 +0000 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:44465) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c4ZXl-00010A-JZ for emacs-orgmode@gnu.org; Wed, 09 Nov 2016 15:32:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c4ZXk-00047Y-6x for emacs-orgmode@gnu.org; Wed, 09 Nov 2016 15:32:25 -0500 Received: from mail-ua0-x229.google.com ([2607:f8b0:400c:c08::229]:36860) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1c4ZXk-000477-2n for emacs-orgmode@gnu.org; Wed, 09 Nov 2016 15:32:24 -0500 Received: by mail-ua0-x229.google.com with SMTP id b35so183746728uaa.3 for ; Wed, 09 Nov 2016 12:32:23 -0800 (PST) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: "Charles C. Berry" Cc: emacs orgmode-mailinglist On 9 November 2016 at 17:54, Charles C. Berry wrote: > On Tue, 8 Nov 2016, Philip Hudson wrote: > > [snip] >> >> How do you do "looping" flow control? >> >> For context, what I'm trying to write is a single Org file from which >> I can tangle out a number of =~/.ssh/config= files, one for each of >> several hosts on a LAN. Within this file I need to repeatedly place a >> template =BEGIN_SRC ssh-config= block, each time with a few words and >> numbers changed. Do you do this anywhere? If so, how have you >> implemented it? > > It sounds like what you want is a template for the src block and another src > block that does substitutions in that template using a table of values > inside a loop. > > Just to get you started, with this template: > > #+NAME: template > #+BEGIN_SRC org > ,#+BEGIN_SRC shell :tangle %to-file > ls -lt %filename > ,#+END_SRC > #+END_SRC > > and this helper src-block > > #+NAME: get-body > #+BEGIN_SRC emacs-lisp :var src-block-name="c-code" > (save-excursion > (org-babel-goto-named-src-block > src-block-name) > (cadr (org-babel-get-src-block-info))) > > #+END_SRC > > > running > > #+header: :wrap src org :var tmpl=get-body("template") > #+BEGIN_SRC emacs-lisp > (org-fill-template tmpl > '(("to-file" . "abc.sh")("filename" . "my-dir"))) > #+END_SRC > > yields > > #+RESULTS: > #+BEGIN_src org > ,#+BEGIN_SRC shell :tangle abc.sh > ls -lt my-dir > ,#+END_SRC > #+END_src > > > To revise this for your application, you need to provide a table of the > associated values for the "to-file" and "filename" keys in the alist, read > that table using a :var header, loop thru the table reconstructing the alist > each time creating src blocks, and send the output to an org tempfile. Then > you tangle the tempfile. > > Alternatively, you simply write the script files directly without bothering > to write to an org tempfile. Thanks Chuck. I think I've got that straight. I hadn't come across `org-fill-template' before. I think I know how to loop thru a table, though I haven't done it myself before; I've seen how to "get" a table as a list. What I'm not clear about is why the template nests a shell-script block inside an Org block. Should the outer Org block not have a %-escaped placeholder for a :tangle target, an intermediate Org file? -- Phil Hudson http://hudson-it.ddns.net Pretty Good Privacy (PGP) ID: 0x887DCA63