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).