Any way to make org-babel properly indent noweb tangled code?

750 Views Asked by At

Tangling this:

#+BEGIN_SRC C :tangle no :noweb-ref begin
int main() {
  printf("Line 1\n");
#+END_SRC

#+BEGIN_SRC C :tangle no :noweb-ref middle
printf("Second\n");
#+END_SRC

#+BEGIN_SRC C :tangle no :noweb-ref end
}
#+END_SRC

#+BEGIN_SRC C :tangle ~/test.c :noweb no-export
<<begin>>
<<middle>>
<<end>>
#+END_SRC

Yields this:

int main() {
  printf("Line 1\n");
printf("Second\n");
}

I have org-src-preserve-indentation turned on, but it can't preserve what isn't there. The code editing windows can't set it correctly if it doesn't see the parts from the previous source code blocks. Finally, I don't want to have to go through all the previous snippets to figure out what the indentation should start at every time I start a new source code block.

Current hack is to tangle the source code, open the tangled file in a new buffer, select all and run c-indent-line-or-region, but I'm hoping there's something better than that.

Org-mode version: 8.2.5h

1

There are 1 best solutions below

0
On

As mentioned, hooking into the org-babel-post-tangle-hook is the way to go. I use the following:

(defun tnez/src-cleanup ()
  (indent-region (point-min) (point-max)))

(add-hook 'org-babel-post-tangle-hook 'tnez/src-cleanup)