emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* selective tangling?
@ 2018-11-29 13:10 John Kitchin
  2018-11-29 16:44 ` Berry, Charles
  2018-11-29 20:32 ` Eric S Fraga
  0 siblings, 2 replies; 4+ messages in thread
From: John Kitchin @ 2018-11-29 13:10 UTC (permalink / raw)
  To: org-mode-email

[-- Attachment #1: Type: text/plain, Size: 1603 bytes --]

Are there any ways to selectively tangle blocks?

By that I mean suppose there are a dozen src blocks in a file, but I want
to selectively tangle only a few of them, selecting them by a tag, for
example, or some other property. These might have mixed languages, e.g. a
config files, a python script, and a makefile.

The use case here is I have an org document that I use to document a
simulation. The simulation has several config files, and a makefile, and
there is a python script that does analysis. I like to put all of these in
src blocks and then use a sh block to run the actual simulation command. I
usually put a :var a=(org-babel-tangle) header in the sh block, which makes
sure the files are tangled, and then runs the shell commands. But this
tangles all the files in the buffer, which is usually not what I want
(there are sometimes multiple simulations described in one file).  The
blocks are not always in one subtree, so it isn't a matter of just
narrowing, and they are mixed languages (text, make, python, etc.) and
target files so I can't just target one file.

The only mechanism for this i have come up with is to use
org-babel-map-src-blocks to run a check on each block to see if it matches
my tangle criteria and then run (org-babel-tangle t) on that block. This
seems to work fine, but I thought I would check if anyone else has a better
solution.


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

[-- Attachment #2: Type: text/html, Size: 2008 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: selective tangling?
  2018-11-29 13:10 selective tangling? John Kitchin
@ 2018-11-29 16:44 ` Berry, Charles
  2018-11-29 17:18   ` John Kitchin
  2018-11-29 20:32 ` Eric S Fraga
  1 sibling, 1 reply; 4+ messages in thread
From: Berry, Charles @ 2018-11-29 16:44 UTC (permalink / raw)
  To: John Kitchin; +Cc: org-mode-email



> On Nov 29, 2018, at 5:10 AM, John Kitchin <jkitchin@andrew.cmu.edu> wrote:
> 
> Are there any ways to selectively tangle blocks?
> 
> By that I mean suppose there are a dozen src blocks in a file, but I want to selectively tangle only a few of them, selecting them by a tag, for example, or some other property. These might have mixed languages, e.g. a config files, a python script, and a makefile.
> 
> The use case here is I have an org document that I use to document a simulation. The simulation has several config files, and a makefile, and there is a python script that does analysis. I like to put all of these in src blocks and then use a sh block to run the actual simulation command. I usually put a :var a=(org-babel-tangle) header in the sh block, which makes sure the files are tangled, and then runs the shell commands. But this tangles all the files in the buffer, which is usually not what I want (there are sometimes multiple simulations described in one file).  The blocks are not always in one subtree, so it isn't a matter of just narrowing, and they are mixed languages (text, make, python, etc.) and target files so I can't just target one file.
> 
> The only mechanism for this i have come up with is to use org-babel-map-src-blocks to run a check on each block to see if it matches my tangle criteria and then run (org-babel-tangle t) on that block. This seems to work fine, but I thought I would check if anyone else has a better solution.
> 
> 

Use the :tangle header arg with your check function and its args:

	:tangle (my-tangle-selector ...)

HTH,

Chuck

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: selective tangling?
  2018-11-29 16:44 ` Berry, Charles
@ 2018-11-29 17:18   ` John Kitchin
  0 siblings, 0 replies; 4+ messages in thread
From: John Kitchin @ 2018-11-29 17:18 UTC (permalink / raw)
  To: Charles Berry; +Cc: org-mode-email

[-- Attachment #1: Type: text/plain, Size: 3441 bytes --]

I tried something like this already, for example this does what I want:


#+BEGIN_SRC ipython :tangle (if (eq tangle-tag 'one) "one.py" "no")
print('hello')
#+END_SRC

#+BEGIN_SRC text :tangle (if (eq tangle-tag 'one) "one.dat" "no")
print('hello')
#+END_SRC

#+BEGIN_SRC ipython :tangle (if (eq tangle-tag 'two) "two.py" "no")
print('hello2')
#+END_SRC

#+BEGIN_SRC makefile :tangle (if (eq tangle-tag 'two) "Makefile" "no")
build:
python two.py
#+END_SRC

Now only tangle things with a 'two
#+BEGIN_SRC emacs-lisp
(let ((tangle-tag 'two))
  (org-babel-tangle))
#+END_SRC

I didn't see a way to avoid having an intermediate variable to specify what
to tangle. This should have a check on if tangle-tag is bound to avoid an
error with regular tangle. Maybe it could be cleaned up by a function as
you describe, e.g. (selective-tangle 'two filename).

Another way I tried is this:

** example two

#+BEGIN_SRC ipython :tangle "one.py" :one
print('hello')
#+END_SRC

#+BEGIN_SRC text :tangle "one.dat" :one
print('hello')
#+END_SRC

#+BEGIN_SRC ipython :tangle "two.py" :two
print('hello2')
#+END_SRC

#+BEGIN_SRC makefile :tangle "Makefile" :two
build:
python two.py
#+END_SRC


#+BEGIN_SRC emacs-lisp
(org-babel-map-src-blocks (buffer-file-name)
  (let ((ha (read (format "(%s)" (substring-no-properties header-args)))))
    (when (memq :two ha)
      (org-babel-tangle '(4)))))
#+END_SRC

#+RESULTS:
: 5235

I don't have strong feelings yet which way is better.

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 Thu, Nov 29, 2018 at 11:44 AM Berry, Charles <ccberry@ucsd.edu> wrote:

>
>
> > On Nov 29, 2018, at 5:10 AM, John Kitchin <jkitchin@andrew.cmu.edu>
> wrote:
> >
> > Are there any ways to selectively tangle blocks?
> >
> > By that I mean suppose there are a dozen src blocks in a file, but I
> want to selectively tangle only a few of them, selecting them by a tag, for
> example, or some other property. These might have mixed languages, e.g. a
> config files, a python script, and a makefile.
> >
> > The use case here is I have an org document that I use to document a
> simulation. The simulation has several config files, and a makefile, and
> there is a python script that does analysis. I like to put all of these in
> src blocks and then use a sh block to run the actual simulation command. I
> usually put a :var a=(org-babel-tangle) header in the sh block, which makes
> sure the files are tangled, and then runs the shell commands. But this
> tangles all the files in the buffer, which is usually not what I want
> (there are sometimes multiple simulations described in one file).  The
> blocks are not always in one subtree, so it isn't a matter of just
> narrowing, and they are mixed languages (text, make, python, etc.) and
> target files so I can't just target one file.
> >
> > The only mechanism for this i have come up with is to use
> org-babel-map-src-blocks to run a check on each block to see if it matches
> my tangle criteria and then run (org-babel-tangle t) on that block. This
> seems to work fine, but I thought I would check if anyone else has a better
> solution.
> >
> >
>
> Use the :tangle header arg with your check function and its args:
>
>         :tangle (my-tangle-selector ...)
>
> HTH,
>
> Chuck
>
>
>
>

[-- Attachment #2: Type: text/html, Size: 5153 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: selective tangling?
  2018-11-29 13:10 selective tangling? John Kitchin
  2018-11-29 16:44 ` Berry, Charles
@ 2018-11-29 20:32 ` Eric S Fraga
  1 sibling, 0 replies; 4+ messages in thread
From: Eric S Fraga @ 2018-11-29 20:32 UTC (permalink / raw)
  To: John Kitchin; +Cc: org-mode-email

On Thursday, 29 Nov 2018 at 08:10, John Kitchin wrote:
> Are there any ways to selectively tangle blocks?

I cannot help but wanted to thank you for this little bit of magic:

> I usually put a :var a=(org-babel-tangle) header in the sh block,
> which makes sure the files are tangled, and then runs the shell

as I'm always forgetting to tangle...  So easy but so
effective.  Thanks!

-- 
Eric S Fraga via Emacs 27.0.50, Org release_9.1.13-894-gf79545

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-11-29 20:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-29 13:10 selective tangling? John Kitchin
2018-11-29 16:44 ` Berry, Charles
2018-11-29 17:18   ` John Kitchin
2018-11-29 20:32 ` Eric S Fraga

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).