2

Say I am using org-babel to tangle code blocks from an org document. I have several blocks with random code in them.

My goal is to dynamically tangle (or not) individual blocks following a condition, for example, contents of first line of block, text in above heading, tag in heading, etc.

More generally, I want to pass the same defun as an argument to :tangle in the header arguments of all code blocks, avoiding the use of heading or buffer properties. Similarly, I try to avoid setting the same identical form in all my code blocks. Instead, I try to define a default header argument to be set programatically, or similar in the example

*  First                  :maybe:

#+begin_src emacs-lisp
  ;; First : not to be tangled

#+end_src

* Second                  :no:

#+begin_src emacs-lisp
  ;; Second : not to be tangled

  ;; This code doesn't do it but it should !!
  (setq-local org-babel-default-header-args
          (cons `(:tangle . ,(test))
              (assq-delete-all :tangle org-babel-default-header-args)))
#+end_src

* Third                   :yes:

#+begin_src emacs-lisp
  ;; Third : this code is to be tangled

  (defun test ()
    (if (let ((mytags (org-get-tags-at (point) t)))
          (or (member "yes" mytags)
              (null mytags)))
        (format "%s.el" (file-name-base
                         (buffer-file-name))) "no"))
#+end_src

So my question is, is it possible to assign a function to the :tangle (or :prologue) header arguments in org-babel code blocks without using properties or explicitly put the form in all code blocks ?

5
  • I'm not sure what you mean, you can, for example, do something like that: #+BEGIN_SRC language :tangle (when condition "file.name"). Or, if you do not want to specify filename, you can do #+BEGIN_SRC language :tangle (when condition "no") or #+BEGIN_SRC language :tangle (when condition "yes")
    – theldoria
    Commented Aug 10, 2016 at 20:20
  • Edited. Sure, I could, but my goal is avoiding adding the same boring :tangle (defun) in all my code blocks; instead, I need to act on org-babel-default-header-args or similar.
    – csantosb
    Commented Aug 10, 2016 at 20:37
  • You can use property syntax to add that to all subtrees orgmode.org/manual/Header-arguments-in-Org-mode-properties.html
    – ibizaman
    Commented May 31, 2018 at 18:07
  • @ibizaman Recommending using the #+PROPERTY: syntax in order to affect all subtrees is almost equivalent to recommend setting buffer properties which OP explicitly stated that he wants to doing that. I think that what he wants to do is to set the header arguments through the variable org-babel-default-header-args so that all code blocks in all files (i.e. non-buffer specific) are affected by that setting.
    – doltes
    Commented Oct 17, 2020 at 17:45
  • @jio210 you are correct, I don't know how I missed that.
    – ibizaman
    Commented Oct 21, 2020 at 6:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.