all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Charles C. Berry" <ccberry@ucsd.edu>
To: Philip Hudson <phil.hudson@iname.com>
Cc: emacs orgmode-mailinglist <emacs-orgmode@gnu.org>
Subject: Re: Tangling flow control
Date: Wed, 9 Nov 2016 09:54:03 -0800	[thread overview]
Message-ID: <alpine.OSX.2.20.1611090930110.719@charles-berrys-macbook.local> (raw)
In-Reply-To: <CAJ1MqVGeJZ1nbAABhbPe+XXjvJ0f2HjrwvuoSh4kx4pqyuMkkQ@mail.gmail.com>

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.

HTH,

Chuck

  reply	other threads:[~2016-11-09 17:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-08 21:21 Tangling flow control Philip Hudson
2016-11-09 17:54 ` Charles C. Berry [this message]
2016-11-09 20:32   ` Philip Hudson
2016-11-09 21:41     ` Charles C. Berry
2016-11-09 22:59       ` Philip Hudson
2016-11-10  1:19         ` Charles C. Berry
2016-11-10 10:36           ` Philip Hudson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.OSX.2.20.1611090930110.719@charles-berrys-macbook.local \
    --to=ccberry@ucsd.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=phil.hudson@iname.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.