From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: selective tangling? Date: Thu, 29 Nov 2018 12:18:14 -0500 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000da5a43057bd0deb3" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:50417) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gSPxP-0000k2-Mj for emacs-orgmode@gnu.org; Thu, 29 Nov 2018 12:18:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gSPxM-0004W6-RS for emacs-orgmode@gnu.org; Thu, 29 Nov 2018 12:18:31 -0500 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]:55615) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gSPxM-0004Uq-2O for emacs-orgmode@gnu.org; Thu, 29 Nov 2018 12:18:28 -0500 Received: by mail-wm1-x32f.google.com with SMTP id y139so3066456wmc.5 for ; Thu, 29 Nov 2018 09:18:28 -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 Berry Cc: org-mode-email --000000000000da5a43057bd0deb3 Content-Type: text/plain; charset="UTF-8" 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 wrote: > > > > On Nov 29, 2018, at 5:10 AM, John Kitchin > 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 > > > > --000000000000da5a43057bd0deb3 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
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= 9;)
#+END_SRC

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

#+BEGIN_= SRC ipython :tangle (if (eq tangle-tag 'two) "two.py" "n= o")
print('hello2')
#+END_SRC
#+BEGIN_SRC makefile :tangle (if (eq tangle-tag 'two) &quo= t;Makefile" "no")
build:
python two.py
#+END_SRC

Now only tangle things with a 'two=C2=A0
#+BEGIN_SRC= emacs-lisp
(let ((tangle-tag 'two))
=C2=A0 (org-ba= bel-tangle))
#+END_SRC

I didn't see = a way to avoid having an intermediate variable to specify what to tangle. T= his should have a check on if tangle-tag is bound to avoid an error with re= gular 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

#+BE= GIN_SRC text :tangle "one.dat" :one
print('hello= 9;)
#+END_SRC

#+BEGIN_SRC ipython :tangl= e "two.py" :two
print('hello2')
#+END= _SRC

#+BEGIN_SRC makefile :tangle "Makefile&q= uot; :two
build:
python two.py
#+END_SRC


#+BEGIN_SRC emacs-lisp
(org-babel-map-src-blocks (buffer-fi= le-name)
=C2=A0 (let ((ha (read (format "(%s)" (substri= ng-no-properties header-args)))))
=C2=A0 =C2=A0 (when (memq :two = ha)
=C2=A0 =C2=A0 =C2=A0 (org-babel-tangle '(4)))))
#+END_SRC

#+RESULTS:
: 5235
=

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

John

----------------------= -------------
Professor John Kitchin=C2=A0
Doherty Hall A207F
Depa= rtment of Chemical Engineering
Carnegie Mellon University
Pittsburgh,= PA 15213
412-268-7803

=
On Thu, Nov 29,= 2018 at 11:44 AM Berry, Charles <cc= berry@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 w= ant 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 c= onfig files, a python script, and a makefile.
>
> The use case here is I have an org document that I use to document a s= imulation. The simulation has several config files, and a makefile, and the= re 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 usu= ally put a :var a=3D(org-babel-tangle) header in the sh block, which makes = sure the files are tangled, and then runs the shell commands. But this tang= les all the files in the buffer, which is usually not what I want (there ar= e sometimes multiple simulations described in one file).=C2=A0 The blocks a= re not always in one subtree, so it isn't a matter of just narrowing, a= nd 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-ma= p-src-blocks to run a check on each block to see if it matches my tangle cr= iteria 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:

=C2=A0 =C2=A0 =C2=A0 =C2=A0 :tangle (my-tangle-selector ...)

HTH,

Chuck



--000000000000da5a43057bd0deb3--