Table of Contents

  1. How to tangle specific blocks of code
    1. Problem description
    2. Hints:
    3. Solution

TODO How to tangle specific blocks of code

Problem description

Let’s say we have the following org file: Now, I want to tangle only those source blocks that are meant to be tangled to target1.py and filter out those ones that are meant to be tangled to the other files.

According to this answer one should use:

(org-babel-tangle-file "myfile.org" "target1.py")

The problem is that this command does not work as required. It tangles all the source blocks and creates all three files, not just target1.py.

I’m using emacs 29.1 and org-mode 9.6.1.

Hints:

Basing on these threads:

I came up with a partial solution, which utilized the fact that org-babel-tangle with two prefix arguments (meaning C-u C-u before C-c C-v t) tangles all the blocks that has the target file the same as the current block of code.

(defun org-babel-tangle-with-2-prefix-args ()
  (interactive)
  (setq current-prefix-arg '(16)) ; 4 for C-u, 16 for C-u C-u
  (call-interactively 'org-babel-tangle))

But how to do it by giving the name of the target file?

Solution

a=11

a=21

b=12

a=31

  (defun my/tangle-exclusively (source-file target-file)
    (let* ((visited (find-buffer-visiting file))
       (buffer (or visited (find-file-noselect file))))


      )

    )


(my/tangle-exclusively "2024-01-27-how-to-tangle-specific-blocks-of-code.org" "target