Orgmode: how to filter the blocks to be tangle?
emacs, org-babel, org-mode
Solution
To achieve this behavior you can make use of the fact that aside from `yes` and `no`, the `:tangle` header argument for Org Babel code blocks also understands file names; i.e., for any given code block you can tell Org Babel which file you would like the block to be tangled to. My idea is to automatically set the file name for each code block under a certain headline when adding a tag to the headline:
(defun org-babel-set-tangle-file ()
(let ((tag (car (org-get-local-tags))))
(org-narrow-to-subtree)
(while (re-search-forward "\\(:tangle \\).*" nil t)
(replace-match (concat "\\1" tag ".el")))
(widen)))
(add-hook 'org-after-tags-change-hook 'org-babel-set-tangle-file)
The resulting behavior is that when you call `org-babel-tangle` for the current file, all code blocks belonging to
- headlines without a tag will be tangled to the default tangle file(s)
- a tagged headline will be tangled to a file named after the tag.
Note that the function above sets the file extension of tag-specific tangle files to `.el`; since you mention that you would like to produce different Emacs configurations I figured that would be a reasonable default (even though you are showing C code in your example).
Problem
In Orgmode, is there a way to tangle just the blocks in subtree matching (or not matching) a specific tag? For instance with the following code ``` * A #+BEGIN_SRC c printf("Not exported"); #+END_SRC * B :D: #+BEGIN_SRC c printf("Exported"); #+END_SRC ``` exporting along tag D, the tangle C file will only contains `printf("Exported");` I'm using `org-mode` to organise my emacs config, and my goal is to derive different configs from the master one `emacs-config.org`. (for instance a lightconfig by marking just the specific)