Can org-mode tangle during export - emacs

I can use the org-babel-tangle to tangle the current file. I was wondering if you could have org export tangle all the code blocks in the org file.
thanks
EA

This runs org-babel-tangle when exporting:
#+NAME: tangle-it
#+BEGIN_SRC emacs-lisp :exports none
(org-babel-tangle)
#+END_SRC
#+BEGIN_SRC text :results silent :noweb yes :exports results
<<tangle-it()>>
#+END_SRC
#+BEGIN_SRC css :tangle test.css
body {
font-size: 12px;
}
#+END_SRC
It doesn't work when using it with #+CALL: tangle-it().
It is also possible to do this with a macro:
#+MACRO: tangle-it (eval (progn (org-babel-tangle) ""))
{{{tangle-it()}}}
#+BEGIN_SRC css :tangle test.css
body {
font-size: 12px;
}
#+END_SRC

I do this to export my elisp source blocks to specific files
#+BEGIN_SRC emacs-lisp :tangle lisp-file.el
(message "Hello lisp-file")
#+END_SRC
I think you can also set this as a property, so you could set properties at the node/tree level.

I would do it like this:
* build :noexport:
#+BEGIN_SRC emacs-lisp
(org-babel-tangle)
(org-latex-export-as-latex)
#+END_SRC
Then just type C-c C-c in the code block to tangle, then export. You can change the export command to whatever you want for other export types.

Related

What can cause org-auto-tangle to result nil for all noweb code block evaluations

I'm using literate programming for some configuration files and would like to have some parts from elisp code block evaluations. I tried evaluating named code blocks with :noweb tangle but they always results nil and I do not see any errors in the *Messages*. Here's a simplified hello world example and the results I got.
Org file
#+title: Hello
#+PROPERTY: header-args :tangle hello.txt :cache no :exports none
#+auto_tangle: t
#+name: hello-world-output
#+begin_src emacs-lisp :tangle no :eval no-export :results output
(print "Hello world")
#+end_src
#+name: hello-world-value
#+begin_src emacs-lisp :tangle no :eval no-export :results value
"Hello world"
#+end_src
#+begin_src text :noweb tangle
<<hello-world-output>> -> <<hello-world-output()>>
<<hello-world-value>> -> <<hello-world-value()>>
#+end_src
Tangled results
(print "Hello world") -> nil
"Hello world" -> nil
I also checked that org-link-elisp-confirm-function and org-confirm-babel-evaluate both have nil value, so they should not be preventing evaluation.
EDIT: I forgot to mention that I used org-auto-tangle. Issue doesn't occur when calling org-bable-tangle directly.
I had the same issue and below solved it for me.
According to the code of org-auto-tangle the code will not be evaluated by default. In order to have the code auto evaluated you need to add your org file to the org-auto-tangle-babel-safelist. I've posted the definition of the variable and a link to the README with an example of how to set the variable.
(defvar org-auto-tangle-babel-safelist '()
"List of full path of files for which code blocks need to be evaluated.
By default, code blocks are not evaluated during the auto-tangle to avoid
possible code execution from unstrusted source. To enable code blocks evaluation
for a specific file, add its full path to this list.")
https://github.com/yilkalargaw/org-auto-tangle#babel-auto-tangle-safelist

Remove the suffix of a returned filename

I set the yasnippet of elisp src as
#+begin_src emacs-lisp :session `(current-buffer)` :lexical t
$0
#+end_src
Which set the current-buffer as session name
#+begin_src emacs-lisp :current-file-name sicp :lexical t
(current-buffer)
#+end_src
#+RESULTS:
: #<buffer yasnippet-offprint.org>
A minor problem is to remove suffix org from yasnippet-offprint.org.
This might be trivial, but search the elisp manual, thus did not got an immediate solution.
How could remove the suffix the yasnippet-offprint.org
The result I desire is
#+begin_src emacs-lisp :session yasnippet-offprint :lexical t
#+end_src
You can use file-name-sans-extension, e.g.,
(file-name-sans-extension "yasnippet-offprint.org")
;; => "yasnippet-offprint"
You can use C-h f (M-x describe-function) to read its docstring. And (info "(elisp) File Name Components") lists functions on manipulating filenames.

Org babel print elisp variable

Using org, I'd like to execute Lisp code which prints the value of a variable into a results block. E.g. I'm trying to print the value of org-babel-default-header-args.
I've tried this:
#+BEGIN_SRC elisp :exports both
(print 'org-babel-default-header-args)
#+END_SRC
#+BEGIN_SRC elisp :exports both
(org-babel-default-header-args)
#+END_SRC
#+BEGIN_SRC elisp :exports both
(symbol-value 'org-babel-default-header-args)
#+END_SRC
The closest thing I've gotten to work is this:
#+BEGIN_SRC elisp :exports both
(describe-variable 'org-babel-default-header-args)
#+END_SRC
But that prints out some extra text. I'd like to literally just print the value of the variable.
To print the value of a variable foo, use
(print foo)
without quoting it. Quote inhibits evaluation: that's exactly what you don't want to do here.

How to export ditaa or dot blocks as captioned figures (LaTeX)?

I have an org-mode document with captioned ditaa and dot figures. When I export to LaTeX, the resulting images are not placed inside a figure environment, not given a \caption, and not given a \label. Other source code blocks export fine.
How can I fix this?
Here is an example org-mode document:
* Plain source code works
#+CAPTION: This works
#+LABEL: fig:works
#+BEGIN_SRC
this is a test
#+END_SRC
The above (Figure [[fig:works]]) is a figure with some source code. When
exported to LaTeX, it is placed inside a ~figure~ environment and
given a caption and label as expected.
* Ditaa doesn't work
#+CAPTION: Foo
#+LABEL: fig:foo
#+BEGIN_SRC ditaa :file foo.png :cmdline -E
/-----\
----------------------------->| foo |<-----------------------------
\-----/
#+END_SRC
The above (Figure [[fig:foo]]) is a ditaa figure. When exported to LaTeX,
the image is not inside a ~figure~ environment, it is missing the
caption, and there is no ~\label~.
* Dot doesn't work
#+CAPTION: Bar
#+LABEL: fig:bar
#+BEGIN_SRC dot :file bar.png
digraph foo {
asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf -> b;
}
#+END_SRC
The above (Figure [[fig:bar]]) is a dot figure. When exported to LaTeX,
the image is not inside a ~figure~ environment, it is missing the
caption, and there is no ~\label~.
#+OPTIONS: toc:nil author:nil
#+TITLE:
#+DATE:
I'm using org-mode 8.2.10 on Emacs 24.3.1.
To get it to work you need to have a #+RESULTS: section under the #+BEGIN_SRC block and caption that instead:
* Plain source code
#+CAPTION: This works
#+LABEL: fig:works
#+BEGIN_SRC
this is a test
#+END_SRC
See Figure [[fig:works]].
* Ditaa
#+BEGIN_SRC ditaa :file foo.png :cmdline -E
/-----\
----------------------------->| foo |<-----------------------------
\-----/
#+END_SRC
#+CAPTION: Foo
#+LABEL: fig:foo
#+RESULTS:
[[file:foo.png]]
See Figure [[fig:foo]].
* Dot
#+BEGIN_SRC dot :file bar.png
digraph foo {
asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf -> b;
}
#+END_SRC
#+CAPTION: Bar
#+LABEL: fig:bar
#+RESULTS:
[[file:bar.png]]
See Figure [[fig:bar]].
#+OPTIONS: toc:nil author:nil
#+TITLE:
#+DATE:

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

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