Re: [O] export to org, header args disappear

2014-05-21 Thread Aaron Ecay
Hello Bastien and Brady,

The babel/export interface does not attempt to manage the header args
when it rewrites source blocks.  I think this code is pretty subtle.
Check out the (let (... (replacement ...)) ...) code in
‘org-babel-exp-process-buffer’ and the attached patch which fixes the
problem (superficially; I’m not sure if it’s the correct fundamental
approach).

From 7faf58afa659cf63042464dbd15ad62239416832 Mon Sep 17 00:00:00 2001
From: Aaron Ecay aarone...@gmail.com
Date: Wed, 21 May 2014 01:58:18 -0400
Subject: [PATCH] ox-org: fix export of source blocks with header args

---
 lisp/ob-exp.el  | 52 +
 testing/lisp/test-ob-exp.el |  6 +++---
 2 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el
index 220a3c3..ce45d84 100644
--- a/lisp/ob-exp.el
+++ b/lisp/ob-exp.el
@@ -311,7 +311,7 @@ The function respects the value of the :exports header argument.
 	 (org-babel-exp-code info)
 
 (defcustom org-babel-exp-code-template
-  #+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC
+  #+BEGIN_SRC %lang%switches%params\n%body\n#+END_SRC
   Template used to export the body of code blocks.
 This template may be customized to include additional information
 such as the code block name, or the values of particular header
@@ -322,7 +322,7 @@ and the following %keys may be used.
  name -- the name of the code block
  body -- the body of the code block
  switches -- the switches associated to the code block
- flags - the flags passed to the code block
+ params  the parameters passed to the code block
 
 In addition to the keys mentioned above, every header argument
 defined for the code block may be used as a key and will be
@@ -332,27 +332,33 @@ replaced with its value.
 
 (defun org-babel-exp-code (info)
   Return the original code block formatted for export.
-  (setf (nth 1 info)
-	(if (string= strip-export (cdr (assoc :noweb (nth 2 info
-	(replace-regexp-in-string
-	 (org-babel-noweb-wrap)  (nth 1 info))
-	  (if (org-babel-noweb-p (nth 2 info) :export)
-	  (org-babel-expand-noweb-references
-	   info org-babel-exp-reference-buffer)
-	(nth 1 info
-  (org-fill-template
-   org-babel-exp-code-template
-   `((lang  . ,(nth 0 info))
- (body  . ,(org-escape-code-in-string (nth 1 info)))
- (switches . ,(let ((f (nth 3 info)))
-		  (and (org-string-nw-p f) (concat   f
- (flags . ,(let ((f (assq :flags (nth 2 info
-		   (and f (concat   (cdr f)
- ,@(mapcar (lambda (pair)
-		 (cons (substring (symbol-name (car pair)) 1)
-		   (format %S (cdr pair
-	   (nth 2 info))
- (name  . ,(or (nth 4 info) )
+  ;; Here we assume that point is in the source block, an assumption
+  ;; we inherit from `org-babel-exp-src-block'.
+  (let* ((sb (org-element-at-point))
+	 (params (org-element-property :parameters sb)))
+(setf (nth 1 info)
+	  (if (string= strip-export (cdr (assoc :noweb (nth 2 info
+	  (replace-regexp-in-string
+	   (org-babel-noweb-wrap)  (nth 1 info))
+	(if (org-babel-noweb-p (nth 2 info) :export)
+		(org-babel-expand-noweb-references
+		 info org-babel-exp-reference-buffer)
+	  (nth 1 info
+(org-fill-template
+ org-babel-exp-code-template
+ `((lang  . ,(nth 0 info))
+   (body  . ,(org-escape-code-in-string (nth 1 info)))
+   (switches . ,(let ((f (nth 3 info)))
+			(and (org-string-nw-p f) (concat   f
+   (flags . ,(let ((f (assq :flags (nth 2 info
+		 (and f (concat   (cdr f)
+   (params . ,(when (and params (not (string= params )))
+		  (concat   params)))
+   ,@(mapcar (lambda (pair)
+		   (cons (substring (symbol-name (car pair)) 1)
+			 (format %S (cdr pair
+		 (nth 2 info))
+   (name  . ,(or (nth 4 info) ))
 
 (defun org-babel-exp-results (info type optional silent hash)
   Evaluate and return the results of the current code block for export.
diff --git a/testing/lisp/test-ob-exp.el b/testing/lisp/test-ob-exp.el
index 1fe810b..b5738d5 100644
--- a/testing/lisp/test-ob-exp.el
+++ b/testing/lisp/test-ob-exp.el
@@ -289,7 +289,7 @@ Here is one at the end of a line. =2=
 : 2
 
 #+NAME: src1
-#+BEGIN_SRC emacs-lisp
+#+BEGIN_SRC emacs-lisp :exports both
 \(+ 1 1)
 #+END_SRC
 (org-test-with-temp-text
@@ -316,9 +316,9 @@ Here is one at the end of a line. =2=
   Test exporting a source block with a flag.
   (should
(string-match
-\\`#\\+BEGIN_SRC emacs-lisp -some-flag$
+\\`#\\+BEGIN_SRC emacs-lisp -x$
 (org-test-with-temp-text
-	#+BEGIN_SRC emacs-lisp :flags -some-flag\n\(+ 1 1)\n#+END_SRC
+	#+BEGIN_SRC emacs-lisp -x\n\(+ 1 1)\n#+END_SRC
   (org-export-execute-babel-code)
   (buffer-string)
 
-- 
1.9.3


--
Aaron Ecay


Re: [O] export to org, header args disappear

2014-05-21 Thread Bastien
Hi Aaron,

Aaron Ecay aarone...@gmail.com writes:

 The babel/export interface does not attempt to manage the header args
 when it rewrites source blocks.  I think this code is pretty subtle.
 Check out the (let (... (replacement ...)) ...) code in
 ‘org-babel-exp-process-buffer’ and the attached patch which fixes the
 problem (superficially; I’m not sure if it’s the correct fundamental
 approach).

I'm not sure either this is fundamentally correct, but it works fine
for me.  I'll let Eric decide on whether this can go to master or not,
and if Eric does not have the time, just go ahead and push the change.

Thanks for this!

-- 
 Bastien



Re: [O] export to org, header args disappear

2014-05-20 Thread Bastien
Hi Brady,

Brady Trainor algeb...@uw.edu writes:

 I have code blocks such as

 #+BEGIN_SRC emacs-lisp :tangle no
 ...
 #+END_SRC

 and when I export the file to org, it becomes

 #+BEGIN_SRC emacs-lisp
 ...
 #+END_SRC

 Is there an option to include the header args on export?

Not to my knowledge -- I played a bit with implementing a new
verbatim value for the :exports parameter, but I finally find
this confusing.  Maybe someone will have better ideas.

-- 
 Bastien