emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements
@ 2013-08-22 18:23 Nicolas Girard
  2013-08-22 18:41 ` Nicolas Goaziou
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Girard @ 2013-08-22 18:23 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1124 bytes --]

Hi all,

One thing I've been missing for long within org-mode is transclusion ;
so eventually I came up with some preliminary code which seems
promising regarding what I have in mind.

Now, I wish I would dedicate a full thread on this subject once my
code works in a predictable way ; but for now I'm experiencing
something annoying: while evaluating my test document using
=(org-babel-execute-buffer)=, or typing =C-c C-c= over the inline
Babel non block elements works fine, exporting the same document using
=C-c C-e t U= doesn't give the same results.

Looking into Babel source, I came up with the following conclusion:
- =(org-babel-execute-buffer)= produces the results I expect ;
- the exporter calls =org-export-execute-babel-code=, which eventually triggers
  =(org-babel-exp-non-block-elements (point-min) (point-max))=. And
this latter function doesn't lead to the same results as
=(org-babel-execute-buffer)=

Could anyone give me a hand on this ?

Attached is my test document, containing both my preliminary code for
transclusion, and some tests at the bottom.

Thanks very much in advance,

cheers,
Nicolas

[-- Attachment #2: transclusion.org --]
[-- Type: application/octet-stream, Size: 3180 bytes --]

#+AUTHOR: Nicolas Girard <girard.nicolas@gmail.com>

* Code to evaluate
Evaluate with: (org-babel-load-file buffer-file-name)

** ng/decode-link
#+BEGIN_SRC emacs-lisp
  (defun ng/decode-link (s)
    "[[x][y]] -> (x . y)"
    (let (desc link)
      (when
          (string-match org-bracket-link-regexp s)
        (setq desc (if (match-end 3) (org-match-string-no-properties 3 s)))
        (setq link (org-link-unescape
                    (org-match-string-no-properties 1 s)))
        (cons link desc))))
#+END_SRC
** ng/get-symlink-id
#+BEGIN_SRC emacs-lisp
  (defun ng/get-symlink-id (s)
    "[[id:888-77][ffff]] -> 888-77"
    (when 
      (and (string-match org-bracket-link-regexp s)
           (setq s (match-string 1 s))
           (string-match "^id\\:\\([^][]+\\)" s))
      (match-string 1 s)
      ))
#+END_SRC
** ng/delete-current-line
#+BEGIN_SRC emacs-lisp
  (defun ng/delete-current-line ()
    (let ((b (point-at-bol))
          (e (+ 1 (point-at-eol))))
      (delete-region b e)))
#+END_SRC
** ng/org-tc
#+BEGIN_SRC emacs-lisp
  (defun ng/org-tc (s)
    (let* ((at-heading-p (org-at-heading-p))
           (dec (ng/decode-link s))
           (link (first dec))
           (new-title (rest dec))
           (id (or (ng/get-symlink-id s) link))
           (lv-prop "_CURRENT_LEVEL_")
           (curr-lv (or (org-current-level) 0))
           ;(old-lv (string-to-number 
           ;         (or (org-entry-get-with-inheritance lv-prop) curr-lv)))
           )
      (unless id
        (error (format "No id found: %s" s)))
      (ng/delete-current-line)
      (save-excursion
        (save-window-excursion
          (org-id-open id)
          (org-copy-subtree)))
      (message (format "Levels: %s / %s" curr-lv old-lv))
      (with-temp-buffer
        (org-mode)
        (org-paste-subtree curr-lv nil nil)
        (org-delete-property "ID") ; prevents ID duplication if an entry gets pasted several times
        (org-set-property lv-prop (format "%d" old-lv))
        (org-back-to-heading)
        (if at-heading-p
          (progn
            ; Rename the heading
            (replace-string (ng/org-get-entry-title) new-title nil (point) (point-at-eol))
          ; body only
          (progn
            (ng/delete-current-line)))
        (buffer-string))))
#+END_SRC
* Source blocks
** Source bloc 1
:PROPERTIES:
:ID:       7002ef55-6d58-465c-ad89-b9394bf6f089
:DUMMY_PROP: dummy_value
:END:
Contents of source bloc 1
** Source bloc 2
:PROPERTIES:
:ID:       ad6acd70-b97e-4479-8388-7a5473bcb3bc
:END:
| A | B |
|---+---|
| 1 | 2 |
* Transclusion tests
Works as expected using =(org-babel-execute-buffer)=.

Doesn't work when exporting the buffer via =C-c C-e t U=.

Apparently, the exporter calls =org-export-execute-babel-code=, which eventually triggers
  =(org-babel-exp-non-block-elements (point-min) (point-max))=.
** src_emacs-lisp[:results raw]{(ng/org-tc "[[id:7002ef55-6d58-465c-ad89-b9394bf6f089][New title 1]]")}
Aa
*** src_emacs-lisp[:results raw]{(ng/org-tc "[[id:ad6acd70-b97e-4479-8388-7a5473bcb3bc][New title 2]]")}

** Body only
src_emacs-lisp[:results raw]{(ng/org-tc "[[id:7002ef55-6d58-465c-ad89-b9394bf6f089][Source bloc 1]]")}
B

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements
  2013-08-22 18:23 [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements Nicolas Girard
@ 2013-08-22 18:41 ` Nicolas Goaziou
  2013-08-22 19:28   ` Nicolas Girard
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Goaziou @ 2013-08-22 18:41 UTC (permalink / raw)
  To: Nicolas Girard; +Cc: emacs-orgmode

Hello,

Nicolas Girard <girard.nicolas@gmail.com> writes:

> Could anyone give me a hand on this ?
>
> Attached is my test document, containing both my preliminary code for
> transclusion, and some tests at the bottom.

I get errors when trying to execute the buffer. Also, could you provide
a minimal example?


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements
  2013-08-22 18:41 ` Nicolas Goaziou
@ 2013-08-22 19:28   ` Nicolas Girard
  2013-08-22 20:28     ` Nicolas Girard
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Girard @ 2013-08-22 19:28 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 875 bytes --]

2013/8/22 Nicolas Goaziou <n.goaziou@gmail.com>:
>
> I get errors when trying to execute the buffer.

Hi Nicolas,

oh yes, my bad, there was a parenthesis missing. Attached is the
corrected document.

> Also, could you provide
> a minimal example?

The attached document is as minimal as you can get IMHO ; but
admittedly I've been sparing with instructions !

Once loaded:

1. Evaluate the elisp code contained in the section "Code to
evaluate", either by executing =(org-babel-load-file
buffer-file-name)=, or by evaluating manually each function using =C-x
C-e=
2. Run the small tests contained in the section "Transclusion tests"
using =(org-babel-execute-buffer)=. This will behave as expected
3. Restore the buffer to its original contents using =C-_=
4. Now, run =(org-babel-exp-non-block-elements (point-min)
(point-max))= and compare with the behaviour in step (2.)

[-- Attachment #2: transclusion.org --]
[-- Type: application/octet-stream, Size: 3181 bytes --]

#+AUTHOR: Nicolas Girard <girard.nicolas@gmail.com>

* Code to evaluate
Evaluate with: (org-babel-load-file buffer-file-name)

** ng/decode-link
#+BEGIN_SRC emacs-lisp
  (defun ng/decode-link (s)
    "[[x][y]] -> (x . y)"
    (let (desc link)
      (when
          (string-match org-bracket-link-regexp s)
        (setq desc (if (match-end 3) (org-match-string-no-properties 3 s)))
        (setq link (org-link-unescape
                    (org-match-string-no-properties 1 s)))
        (cons link desc))))
#+END_SRC
** ng/get-symlink-id
#+BEGIN_SRC emacs-lisp
  (defun ng/get-symlink-id (s)
    "[[id:888-77][ffff]] -> 888-77"
    (when 
      (and (string-match org-bracket-link-regexp s)
           (setq s (match-string 1 s))
           (string-match "^id\\:\\([^][]+\\)" s))
      (match-string 1 s)
      ))
#+END_SRC
** ng/delete-current-line
#+BEGIN_SRC emacs-lisp
  (defun ng/delete-current-line ()
    (let ((b (point-at-bol))
          (e (+ 1 (point-at-eol))))
      (delete-region b e)))
#+END_SRC
** ng/org-tc
#+BEGIN_SRC emacs-lisp
  (defun ng/org-tc (s)
    (let* ((at-heading-p (org-at-heading-p))
           (dec (ng/decode-link s))
           (link (first dec))
           (new-title (rest dec))
           (id (or (ng/get-symlink-id s) link))
           (lv-prop "_CURRENT_LEVEL_")
           (curr-lv (or (org-current-level) 0))
           ;(old-lv (string-to-number 
           ;         (or (org-entry-get-with-inheritance lv-prop) curr-lv)))
           )
      (unless id
        (error (format "No id found: %s" s)))
      (ng/delete-current-line)
      (save-excursion
        (save-window-excursion
          (org-id-open id)
          (org-copy-subtree)))
      (message (format "Levels: %s / %s" curr-lv old-lv))
      (with-temp-buffer
        (org-mode)
        (org-paste-subtree curr-lv nil nil)
        (org-delete-property "ID") ; prevents ID duplication if an entry gets pasted several times
        (org-set-property lv-prop (format "%d" old-lv))
        (org-back-to-heading)
        (if at-heading-p
          (progn
            ; Rename the heading
            (replace-string (ng/org-get-entry-title) new-title nil (point) (point-at-eol)))
          ; body only
          (progn
            (ng/delete-current-line)))
        (buffer-string))))
#+END_SRC
* Source blocks
** Source bloc 1
:PROPERTIES:
:ID:       7002ef55-6d58-465c-ad89-b9394bf6f089
:DUMMY_PROP: dummy_value
:END:
Contents of source bloc 1
** Source bloc 2
:PROPERTIES:
:ID:       ad6acd70-b97e-4479-8388-7a5473bcb3bc
:END:
| A | B |
|---+---|
| 1 | 2 |
* Transclusion tests
Works as expected using =(org-babel-execute-buffer)=.

Doesn't work when exporting the buffer via =C-c C-e t U=.

Apparently, the exporter calls =org-export-execute-babel-code=, which eventually triggers
  =(org-babel-exp-non-block-elements (point-min) (point-max))=.
** src_emacs-lisp[:results raw]{(ng/org-tc "[[id:7002ef55-6d58-465c-ad89-b9394bf6f089][New title 1]]")}
Aa
*** src_emacs-lisp[:results raw]{(ng/org-tc "[[id:ad6acd70-b97e-4479-8388-7a5473bcb3bc][New title 2]]")}

** Body only
src_emacs-lisp[:results raw]{(ng/org-tc "[[id:7002ef55-6d58-465c-ad89-b9394bf6f089][Source bloc 1]]")}
B

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements
  2013-08-22 19:28   ` Nicolas Girard
@ 2013-08-22 20:28     ` Nicolas Girard
  2013-08-22 20:33       ` Nicolas Goaziou
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Girard @ 2013-08-22 20:28 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 612 bytes --]

Sorry again for the buggy minimal example.
Please try again with the attached version. Instructions as before:

1. Evaluate the elisp code contained in the section "Code to
evaluate", either by executing =(org-babel-load-file
buffer-file-name)=, or by evaluating manually each function using =C-x
C-e=
2. Run the small tests contained in the section "Transclusion tests"
using =(org-babel-execute-buffer)=. This will behave as expected
3. Restore the buffer to its original contents using =C-_=
4. Now, run =(org-babel-exp-non-block-elements (point-min)
(point-max))= and compare with the behaviour in step (2.)

[-- Attachment #2: transclusion.org --]
[-- Type: application/octet-stream, Size: 3528 bytes --]

#+AUTHOR: Nicolas Girard <girard.nicolas@gmail.com>

* Instructions
:PROPERTIES:
:VISIBILITY: all
:END:

1. Evaluate the elisp code contained in the section "Code to evaluate", either by executing =(org-babel-load-file buffer-file-name)=, or by evaluating manually each function using =C-x C-e=
2. Run the small tests contained in the section "Transclusion tests" using =(org-babel-execute-buffer)=. This will behave as expected
3. Restore the buffer to its original contents using =C-_=
4. Now, run =(org-babel-exp-non-block-elements (point-min) (point-max))= and compare with the behaviour in step (2.)
* Code to evaluate
Evaluate with: (org-babel-load-file buffer-file-name)

** ng/decode-link
#+BEGIN_SRC emacs-lisp
  (defun ng/decode-link (s)
    "[[x][y]] -> (x . y)"
    (let (desc link)
      (when
          (string-match org-bracket-link-regexp s)
        (setq desc (if (match-end 3) (org-match-string-no-properties 3 s)))
        (setq link (org-link-unescape
                    (org-match-string-no-properties 1 s)))
        (cons link desc))))
#+END_SRC
** ng/get-symlink-id
#+BEGIN_SRC emacs-lisp
  (defun ng/get-symlink-id (s)
    "[[id:888-77][ffff]] -> 888-77"
    (when 
      (and (string-match org-bracket-link-regexp s)
           (setq s (match-string 1 s))
           (string-match "^id\\:\\([^][]+\\)" s))
      (match-string 1 s)
      ))
#+END_SRC
** ng/delete-current-line
#+BEGIN_SRC emacs-lisp
  (defun ng/delete-current-line ()
    (let ((b (point-at-bol))
          (e (+ 1 (point-at-eol))))
      (delete-region b e)))
#+END_SRC
** ng/org-tc
#+BEGIN_SRC emacs-lisp
  (defun ng/org-tc (s)
    (let* ((at-heading-p (org-at-heading-p))
           (dec (ng/decode-link s))
           (link (first dec))
           (new-title (rest dec))
           (id (or (ng/get-symlink-id s) link))
           (lv-prop "_CURRENT_LEVEL_")
           (curr-lv (or (org-current-level) 0)))
      (unless id
        (error (format "No id found: %s" s)))
      (ng/delete-current-line)
      (save-excursion
        (save-window-excursion
          (org-id-open id)
          (org-copy-subtree)))
      (with-temp-buffer
        (org-mode)
        (org-paste-subtree curr-lv nil nil)
        (org-delete-property "ID") ; prevents ID duplication if an entry gets pasted several times
        (org-back-to-heading)
        (if at-heading-p
          (progn
            ; Rename the heading
            (replace-string (ng/org-get-entry-title) new-title nil (point) (point-at-eol)))
          ; body only
          (progn
            (ng/delete-current-line)))
        (buffer-string))))
#+END_SRC
* Source blocks
** Source bloc 1
:PROPERTIES:
:ID:       7002ef55-6d58-465c-ad89-b9394bf6f089
:DUMMY_PROP: dummy_value
:END:
Contents of source bloc 1
** Source bloc 2
:PROPERTIES:
:ID:       ad6acd70-b97e-4479-8388-7a5473bcb3bc
:END:
| A | B |
|---+---|
| 1 | 2 |
* Transclusion tests
:PROPERTIES:
:VISIBILITY: all
:END:

Works as expected using =(org-babel-execute-buffer)=.

Doesn't work when exporting the buffer via =C-c C-e t U=.

Apparently, the exporter calls =org-export-execute-babel-code=, which eventually triggers
  =(org-babel-exp-non-block-elements (point-min) (point-max))=.
** src_emacs-lisp[:results raw]{(ng/org-tc "[[id:7002ef55-6d58-465c-ad89-b9394bf6f089][New title 1]]")}
Aa
*** src_emacs-lisp[:results raw]{(ng/org-tc "[[id:ad6acd70-b97e-4479-8388-7a5473bcb3bc][New title 2]]")}

** Body only
src_emacs-lisp[:results raw]{(ng/org-tc "[[id:7002ef55-6d58-465c-ad89-b9394bf6f089][Source bloc 1]]")}
B

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements
  2013-08-22 20:28     ` Nicolas Girard
@ 2013-08-22 20:33       ` Nicolas Goaziou
  2013-08-22 21:01         ` Nicolas Girard
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Goaziou @ 2013-08-22 20:33 UTC (permalink / raw)
  To: Nicolas Girard; +Cc: emacs-orgmode

Nicolas Girard <girard.nicolas@gmail.com> writes:

> Sorry again for the buggy minimal example.
> Please try again with the attached version. Instructions as before:

Still get an error: (void-function ng/org-get-entry-title).


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements
  2013-08-22 20:33       ` Nicolas Goaziou
@ 2013-08-22 21:01         ` Nicolas Girard
  2013-08-22 23:06           ` Nicolas Goaziou
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Girard @ 2013-08-22 21:01 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 499 bytes --]

2013/8/22 Nicolas Goaziou <n.goaziou@gmail.com>:
> Nicolas Girard <girard.nicolas@gmail.com> writes:
>
>> Sorry again for the buggy minimal example.
>> Please try again with the attached version. Instructions as before:
>
> Still get an error: (void-function ng/org-get-entry-title).
>

I'm ashamed... but this time I swear I started a minimal emacs session
and got it to work. The instructions have slightly changed and are
written at the very beginning of the attached minimal document.

Thanks !

[-- Attachment #2: transclusion.org --]
[-- Type: application/octet-stream, Size: 3612 bytes --]

#+AUTHOR: Nicolas Girard <girard.nicolas@gmail.com>

* Instructions
:PROPERTIES:
:VISIBILITY: all
:END:

1. Evaluate the elisp code contained in the section "Code to evaluate", either by executing =(org-babel-load-file buffer-file-name)=, or by evaluating manually each function using =C-x C-e=
2. Run the small tests contained in the section "Transclusion tests" using =(org-babel-execute-buffer)=. This will behave as expected
3. Restore the buffer to its original contents using =C-_=
4. Now, run =(progn (require 'ox) (org-export-execute-babel-code))= and compare with the behaviour in step (2.)
* Code to evaluate
Evaluate with: (org-babel-load-file buffer-file-name)
** required
#+BEGIN_SRC emacs-lisp 
(eval-when-compile
    (require 'cl)
    (require 'org-id))
#+END_SRC
** ng/decode-link
#+BEGIN_SRC emacs-lisp 
(defun ng/decode-link (s)
  "[[x][y]] -> (x . y)"
  (let (desc link)
    (when
	(string-match org-bracket-link-regexp s)
      (setq desc (if (match-end 3) (org-match-string-no-properties 3 s)))
      (setq link (org-link-unescape
		  (org-match-string-no-properties 1 s)))
      (cons link desc))))
#+END_SRC
** ng/get-symlink-id
#+BEGIN_SRC emacs-lisp 
(defun ng/get-symlink-id (s)
  "[[id:888-77][ffff]] -> 888-77"
  (when 
    (and (string-match org-bracket-link-regexp s)
	 (setq s (match-string 1 s))
	 (string-match "^id\\:\\([^][]+\\)" s))
    (match-string 1 s)
    ))
#+END_SRC
** ng/delete-current-line
#+BEGIN_SRC emacs-lisp 
(defun ng/delete-current-line ()
  (let ((b (point-at-bol))
	(e (+ 1 (point-at-eol))))
    (delete-region b e)))
#+END_SRC
** ng/org-get-entry-title
#+BEGIN_SRC emacs-lisp 
(defun ng/org-get-entry-title (&optional pom)
  " * WORK aa :b: ---> aa"
  (org-with-point-at pom
    (org-no-properties (org-get-heading 'notags 'notodo))))
#+END_SRC
** ng/org-tc
#+BEGIN_SRC emacs-lisp 
(defun ng/org-tc (s)
  (let* ((at-heading-p (org-at-heading-p))
	 (dec (ng/decode-link s))
	 (link (first dec))
	 (new-title (rest dec))
	 (id (or (ng/get-symlink-id s) link))
	 (lv-prop "_CURRENT_LEVEL_")
	 (curr-lv (or (org-current-level) 0)))
    (unless id
      (error (format "No id found: %s" s)))
    (ng/delete-current-line)
    (save-excursion
      (save-window-excursion
	(org-id-open id)
	(org-copy-subtree)))
    (with-temp-buffer
      (org-mode)
      (org-paste-subtree curr-lv nil nil)
      (org-delete-property "ID") ; prevents ID duplication if an entry gets pasted several times
      (org-back-to-heading)
      (if at-heading-p
	(progn
	  ; Rename the heading
	  (replace-string (ng/org-get-entry-title) new-title nil (point) (point-at-eol)))
	; body only
	(progn
	  (ng/delete-current-line)))
      (buffer-string))))
#+END_SRC
* Source blocks
** Source bloc 1
:PROPERTIES:
:ID:       7002ef55-6d58-465c-ad89-b9394bf6f089
:DUMMY_PROP: dummy_value
:END:
Contents of source bloc 1
** Source bloc 2
:PROPERTIES:
:ID:       ad6acd70-b97e-4479-8388-7a5473bcb3bc
:END:
| A | B |
|---+---|
| 1 | 2 |
* Transclusion tests
:PROPERTIES:
:VISIBILITY: all
:END:

Works as expected using =(org-babel-execute-buffer)=.

Doesn't work when exporting the buffer via =C-c C-e t U=.

Apparently, the exporter calls =org-export-execute-babel-code=, which eventually triggers
  =(org-babel-exp-non-block-elements (point-min) (point-max))=.
** src_emacs-lisp[:results raw]{(ng/org-tc "[[id:7002ef55-6d58-465c-ad89-b9394bf6f089][New title 1]]")}
Aa
*** src_emacs-lisp[:results raw]{(ng/org-tc "[[id:ad6acd70-b97e-4479-8388-7a5473bcb3bc][New title 2]]")}

** Body only
src_emacs-lisp[:results raw]{(ng/org-tc "[[id:7002ef55-6d58-465c-ad89-b9394bf6f089][Source bloc 1]]")}
B

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements
  2013-08-22 21:01         ` Nicolas Girard
@ 2013-08-22 23:06           ` Nicolas Goaziou
  2013-08-22 23:20             ` Nicolas Girard
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Goaziou @ 2013-08-22 23:06 UTC (permalink / raw)
  To: Nicolas Girard; +Cc: emacs-orgmode

Nicolas Girard <girard.nicolas@gmail.com> writes:

> I'm ashamed... but this time I swear I started a minimal emacs session
> and got it to work. The instructions have slightly changed and are
> written at the very beginning of the attached minimal document.

I see.

There is an important difference between evaluating a buffer and
evaluating a buffer during export. In the latter, Babel has to deal with
replacement values, i.e., code block is replaced by its results. See the
difference between `org-export-execute-babel-code' and
`org-babel-execute-buffer' in a buffer containing only

  src_emacs-lisp[:results raw]{(+ 1 2)}

In this case, your code removes the code (and much more) as
a side-effect. This confuses `org-babel-exp-non-block-elements', which
also tries to remove it. As this fails, it removes a random part of the
buffer instead (probably as a mean revenge).

`org-babel-exp-non-block-elements' and `org-babel-exp-process-buffer'
could probably check if code still exists before trying to remove (and
replace) it.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements
  2013-08-22 23:06           ` Nicolas Goaziou
@ 2013-08-22 23:20             ` Nicolas Girard
  2013-08-23  8:11               ` Nicolas Goaziou
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Girard @ 2013-08-22 23:20 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1433 bytes --]

2013/8/23 Nicolas Goaziou <n.goaziou@gmail.com>:
> Nicolas Girard <girard.nicolas@gmail.com> writes:
>
> I see.
>
> There is an important difference between evaluating a buffer and
> evaluating a buffer during export. In the latter, Babel has to deal with
> replacement values, i.e., code block is replaced by its results. See the
> difference between `org-export-execute-babel-code' and
> `org-babel-execute-buffer' in a buffer containing only
>
>   src_emacs-lisp[:results raw]{(+ 1 2)}
>
> In this case, your code removes the code (and much more) as
> a side-effect. This confuses `org-babel-exp-non-block-elements', which
> also tries to remove it. As this fails, it removes a random part of the
> buffer instead (probably as a mean revenge).
>

Thanks very much for your insight ! I came to the same conclusions and
found a temporary solution, namely using the value (bufferp
org-current-export-file) to check wether the code is called by the
exporter or just Babel. Attached is a modified version of my code that
behaves differently depending of the value of (bufferp
org-current-export-file).

Do you think it could be robust enough ?

> `org-babel-exp-non-block-elements' and `org-babel-exp-process-buffer'
> could probably check if code still exists before trying to remove (and
> replace) it.
>

Yeah, I don't know how much work it would represent but it seems much
more appropriate that my hack-ish solution.

---
Nicolas

[-- Attachment #2: transclusion.org --]
[-- Type: application/octet-stream, Size: 4528 bytes --]

#+AUTHOR: Nicolas Girard <girard.nicolas@gmail.com>

* Instructions
:PROPERTIES:
:VISIBILITY: all
:END:

1. Evaluate the elisp code contained in the section "Code to evaluate", either by executing =(org-babel-load-file buffer-file-name)=, or by evaluating manually each function using =C-x C-e=
2. Run the small tests contained in the section "Transclusion tests" using =(org-babel-execute-buffer)=. This will behave as expected
=(org-babel-exp-process-buffer)=
3. Restore the buffer to its original contents using =C-_=
4. Now, run =(progn (require 'ox) (org-export-execute-babel-code))= and compare with the behaviour in step (2.)

   Ultimately this boils down to
   #+BEGIN_SRC emacs-lisp :tangle no
     (let ((reference (org-export-copy-buffer)))
         (unwind-protect (let ((org-current-export-file reference))
                           ; beginning of (org-babel-exp-process-buffer)
                           (org-babel-exp-non-block-elements (point-min) (point-max))
                           ; end of (org-babel-exp-process-buffer)
                           )
           (kill-buffer reference)))
   #+END_SRC

* Code to evaluate
Evaluate with: (org-babel-load-file buffer-file-name)
** required
#+BEGIN_SRC emacs-lisp 
(eval-when-compile
    (require 'cl)
    (require 'org-id))
#+END_SRC
** ng/decode-link
#+BEGIN_SRC emacs-lisp 
(defun ng/decode-link (s)
  "[[x][y]] -> (x . y)"
  (let (desc link)
    (when
	(string-match org-bracket-link-regexp s)
      (setq desc (if (match-end 3) (org-match-string-no-properties 3 s)))
      (setq link (org-link-unescape
		  (org-match-string-no-properties 1 s)))
      (cons link desc))))
#+END_SRC
** ng/get-symlink-id
#+BEGIN_SRC emacs-lisp 
(defun ng/get-symlink-id (s)
  "[[id:888-77][ffff]] -> 888-77"
  (when 
    (and (string-match org-bracket-link-regexp s)
	 (setq s (match-string 1 s))
	 (string-match "^id\\:\\([^][]+\\)" s))
    (match-string 1 s)
    ))
#+END_SRC
** ng/delete-current-line
#+BEGIN_SRC emacs-lisp 
(defun ng/delete-current-line ()
  (let ((b (point-at-bol))
	(e (+ 1 (point-at-eol))))
    (delete-region b e)))
#+END_SRC
** ng/org-get-entry-title
#+BEGIN_SRC emacs-lisp 
(defun ng/org-get-entry-title (&optional pom)
  " * WORK aa :b: ---> aa"
  (org-with-point-at pom
    (org-no-properties (org-get-heading 'notags 'notodo))))
#+END_SRC
** ng/org-tc
#+BEGIN_SRC emacs-lisp 
  (defun ng/org-tc (s)
    (let* ((at-heading-p (org-at-heading-p))
           ; are we called by the exporter, or just Babel ?
           (exporting-p (bufferp org-current-export-file))
           (dec (ng/decode-link s))
           (link (first dec))
           (new-title (rest dec))
           (id (or (ng/get-symlink-id s) link))
           (curr-lv (or (org-current-level) 0)))
      (unless id
        (error (format "No id found: %s" s)))
      ;(ng/delete-current-line)
      (save-excursion
        (save-window-excursion
          (org-id-open id)
          (org-copy-subtree)))
      (with-temp-buffer
        (org-mode)
        (org-paste-subtree curr-lv nil nil)
        (org-delete-property "ID") ; prevents ID duplication if an entry gets pasted several times
        (org-back-to-heading)
        (if at-heading-p
          (progn
            ; Rename the heading
            (replace-string (ng/org-get-entry-title) new-title nil (point) (point-at-eol))
            (when exporting-p
              (org-back-to-heading)
              (save-excursion (replace-regexp "\\*+ " "" nil (point) (point-at-eol)))))
          ; body only
          (progn
            (ng/delete-current-line)))
        (buffer-string))))
#+END_SRC
* Source blocks
** Source bloc 1
:PROPERTIES:
:ID:       7002ef55-6d58-465c-ad89-b9394bf6f089
:DUMMY_PROP: dummy_value
:END:
Contents of source bloc 1
** Source bloc 2
:PROPERTIES:
:ID:       ad6acd70-b97e-4479-8388-7a5473bcb3bc
:END:
| A | B |
|---+---|
| 1 | 2 |
* Transclusion tests
:PROPERTIES:
:VISIBILITY: all
:END:

Works as expected using =(org-babel-execute-buffer)=.

Doesn't work when exporting the buffer via =C-c C-e t U=.

Apparently, the exporter calls =org-export-execute-babel-code=, which eventually triggers
  =(org-babel-exp-non-block-elements (point-min) (point-max))=.

** src_emacs-lisp[:results raw]{(ng/org-tc "[[id:7002ef55-6d58-465c-ad89-b9394bf6f089][New title 1]]")}
Aa
*** src_emacs-lisp[:results raw]{(ng/org-tc "[[id:ad6acd70-b97e-4479-8388-7a5473bcb3bc][New title 2]]")}

** Body only
src_emacs-lisp[:results raw]{(ng/org-tc "[[id:7002ef55-6d58-465c-ad89-b9394bf6f089][Source bloc 1]]")}
B

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements
  2013-08-22 23:20             ` Nicolas Girard
@ 2013-08-23  8:11               ` Nicolas Goaziou
  2013-08-23 14:55                 ` Eric Schulte
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Goaziou @ 2013-08-23  8:11 UTC (permalink / raw)
  To: Nicolas Girard; +Cc: emacs-orgmode

Nicolas Girard <girard.nicolas@gmail.com> writes:

>> `org-babel-exp-non-block-elements' and `org-babel-exp-process-buffer'
>> could probably check if code still exists before trying to remove (and
>> replace) it.
>>
>
> Yeah, I don't know how much work it would represent but it seems much
> more appropriate that my hack-ish solution.

It might be tricky to set up. I'm not even sure Babel is meant for these
cases (where evaluating code removes its source).

Another possibility is to have a "side-effect only" behaviour for export
where code block is not removed nor replaced. ":exports none" could be
a candidate for this. Maybe Eric Schulte has an opinion on it.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements
  2013-08-23  8:11               ` Nicolas Goaziou
@ 2013-08-23 14:55                 ` Eric Schulte
  2013-08-26 10:33                   ` Nicolas Goaziou
  0 siblings, 1 reply; 11+ messages in thread
From: Eric Schulte @ 2013-08-23 14:55 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode, Nicolas Girard

Nicolas Goaziou <n.goaziou@gmail.com> writes:

> Nicolas Girard <girard.nicolas@gmail.com> writes:
>
>>> `org-babel-exp-non-block-elements' and `org-babel-exp-process-buffer'
>>> could probably check if code still exists before trying to remove (and
>>> replace) it.
>>>
>>
>> Yeah, I don't know how much work it would represent but it seems much
>> more appropriate that my hack-ish solution.
>
> It might be tricky to set up. I'm not even sure Babel is meant for these
> cases (where evaluating code removes its source).
>
> Another possibility is to have a "side-effect only" behaviour for export
> where code block is not removed nor replaced. ":exports none" could be
> a candidate for this. Maybe Eric Schulte has an opinion on it.
>

If I understand correctly, I think ":export both :results none" should
have the desired effect of executing the code block, not inserting the
results, and keeping the code block itself in place.

If you want to run emacs lisp at export time but don't want the code or
the results, maybe it would be a more natural fit for a filter function?

-- 
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements
  2013-08-23 14:55                 ` Eric Schulte
@ 2013-08-26 10:33                   ` Nicolas Goaziou
  0 siblings, 0 replies; 11+ messages in thread
From: Nicolas Goaziou @ 2013-08-26 10:33 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode, Nicolas Girard

Hello,

Eric Schulte <schulte.eric@gmail.com> writes:

> If I understand correctly, I think ":export both :results none" should
> have the desired effect of executing the code block, not inserting the
> results, and keeping the code block itself in place.

AFAICT, ":export both :results none" combination doesn't seem to produce
the desired outcome with the example file.

Note that the feature is only needed for elisp, since it is the only
language able to modify the buffer containing the source code.

> If you want to run emacs lisp at export time but don't want the code or
> the results, maybe it would be a more natural fit for a filter
> function?

I think the OP wants (org-babel-execute-buffer) and
(org-export-execute-babel-code) to behave the same in this specific
situation. So, it's not export-specific.

To put it differently, he wants to disable code replacement with value
(which is specific to the export part of Babel).


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-08-26 10:33 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-22 18:23 [Babel] org-babel-execute-buffer gives different results than org-babel-exp-non-block-elements Nicolas Girard
2013-08-22 18:41 ` Nicolas Goaziou
2013-08-22 19:28   ` Nicolas Girard
2013-08-22 20:28     ` Nicolas Girard
2013-08-22 20:33       ` Nicolas Goaziou
2013-08-22 21:01         ` Nicolas Girard
2013-08-22 23:06           ` Nicolas Goaziou
2013-08-22 23:20             ` Nicolas Girard
2013-08-23  8:11               ` Nicolas Goaziou
2013-08-23 14:55                 ` Eric Schulte
2013-08-26 10:33                   ` Nicolas Goaziou

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).