emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] Tangling with cross-references coming from included files


From: address@hidden
Subject: [O] Tangling with cross-references coming from included files
Date: Tue, 28 Mar 2017 09:20:48 +0200

Hi

I am trying to achieve the tangling of an org-file which is composed by
multiple (included) org-files.

I am using Emacs 25.1.1 and Org mode 9.0.5.

Here an example of what I am trying to achieve.

——————Example starts here——————
==== File A.org ====

[Text here …]

#+BEGIN_SRC python :tangle src/a.py :noweb tangle
class Foo(object):
    <<file-b-method-foo>>
#+END

[More text here ...]

#+INCLUDE: “./b.org"

==== File b.org ====

[Text …]

#+BEGIN_SRC python :exports none
def foo(self):
    print(“bar”)
#+END
——————Example ends here——————

My problem is, that the included parts are not being tangled. When
having all the parts in one file, there is no problem at all, but having only 
file is not an option.

An easy approach would be, as I thought at first, to use 
"org-org-export-as-org", which does include all into one buffer.
Then one could simply tangle that buffer. The problem with that approach is 
however, that all arguments
get stripped from #+BEGIN_SRC and therefore “:tangle …” too.

So, looking at the list’s archive and on org mode hacks, I found a workaround, 
which uses a hook
when exporting ([1] and [2]):

#+begin_src emacs-lisp
 (defun ded/org-export-as-org-to-buffer ()
   (interactive)
   (let* ((tmp-file (make-temp-file "org-tangle-with-include"))
          (org-export-preprocess-after-include-files-hook
           `((lambda () (let ((s (buffer-string)))
                          (with-temp-file ,tmp-file (insert s)))))))
     (save-window-excursion (org-export-as-html-to-buffer nil))
     (switch-to-buffer
      (get-buffer-create "*Org Org Export*"))
     (insert-file-contents tmp-file))
   (org-mode))

 (defun ded/tangle-with-include-files ()
   (interactive)
   (save-window-excursion
     (ded/org-export-as-org-to-buffer)
     (org-babel-tangle)))
#+end_src

That is to use the hook to make a copy of the buffer right before exporting. 
This buffer, which has all files tied
together, gets written to a temp. file and this file is then tangled. This is 
done using the string “buffer-string”
generated by the exporter.

The problem with that solution is, that it concerns org versions < 9, using the 
“old” exporter.
Within the new exporter there is no such hook and therefore no string 
“buffer-string” holding
the complete buffer (as far as I saw).

Instead there is org-before-parsing-hook [3], wich does essentially the same 
(as far as I understood).
But instead using a string for copying the buffer for exporting, the exporter 
uses a temporary buffer,
“buf-copy”.

I tried to get the contents of that buffer within the hook like so:

#+begin_src emacs-lisp
 (defun ded/org-export-as-org-to-buffer ()
   (interactive)
   (let* ((tmp-file (make-temp-file "org-tangle-with-include"))
          (org-before-parsing-hook
           `((lambda () (setq ((b (get-buffer ,buf-copy)))
                          (with-temp-file ,tmp-file (insert b)))))))
     (save-window-excursion (org-export-as-html-to-buffer nil))
     (switch-to-buffer
      (get-buffer-create "*Org Org Export*"))
     (insert-file-contents tmp-file))
   (org-mode))

 (defun ded/tangle-with-include-files ()
   (interactive)
   (save-window-excursion
     (ded/org-export-as-org-to-buffer)
     (org-babel-tangle)))
#+end_src

But it does not work as buf-copy is void. What am I doing wrong? How do I get 
the buffer “buf-copy”?
I am assuming that it is “buf-copy” as the call of the hook happens within the 
context of “org-export-with-buffer-copy”.
Is my assumption wrong?

Any help is greatly appreciated.


Sven


[1]: http://orgmode.org/worg/org-hacks.html#sec-1-10-2
[2]: https://lists.gnu.org/archive/html/emacs-orgmode/2010-09/msg00771.html
[3]: http://orgmode.org/worg/doc.html#org-export-before-parsing-hook

Attachment: signature.asc
Description: Message signed with OpenPGP


reply via email to

[Prev in Thread] Current Thread [Next in Thread]