Browse Source

prolog and epilog header arguments

* lisp/ob-gnuplot.el (org-babel-expand-body:gnuplot): Use new header
  arguments.
* lisp/ob-core.el (org-babel-common-header-args-w-values): Mention new
  header arguments.
  (org-babel-expand-body:generic): Use new header arguments.
* doc/org.texi (Specific header arguments): Document new header
  arguments.
Eric Schulte 11 years ago
parent
commit
56ac8f8b69
3 changed files with 38 additions and 12 deletions
  1. 23 1
      doc/org.texi
  2. 10 1
      lisp/ob-core.el
  3. 5 10
      lisp/ob-gnuplot.el

+ 23 - 1
doc/org.texi

@@ -726,6 +726,8 @@ Specific header arguments
 * eval::                        Limit evaluation of specific code blocks
 * wrap::                        Mark source block evaluation results
 * post::                        Post processing of code block results
+* prologue::                    Text to prepend to code block body
+* epilogue::                    Text to append to code block body
 
 Miscellaneous
 
@@ -14172,6 +14174,8 @@ argument in lowercase letters.  The following header arguments are defined:
 * eval::                        Limit evaluation of specific code blocks
 * wrap::                        Mark source block evaluation results
 * post::                        Post processing of code block results
+* prologue::                    Text to prepend to code block body
+* epilogue::                    Text to append to code block body
 @end menu
 
 Additional header arguments are defined on a language-specific basis, see
@@ -15118,7 +15122,7 @@ to @code{#+BEGIN_} and @code{#+END_}, which will then be used to wrap the
 results.  If not string is specified then the results will be wrapped in a
 @code{#+BEGIN/END_RESULTS} block.
 
-@node post,  , wrap, Specific header arguments
+@node post, prologue, wrap, Specific header arguments
 @subsubsection @code{:post}
 The @code{:post} header argument is used to post-process the results of a
 code block execution.  When a post argument is given, the results of the code
@@ -15153,6 +15157,24 @@ argument.
 :END:
 @end example
 
+@node prologue, epilogue, post, Specific header arguments
+
+The value of the @code{prologue} header argument will be prepended to the
+code block body before execution.  For example, @code{:prologue "reset"} may
+be used to reset a gnuplot session before execution of a particular code
+block, or the following configuration may be used to do this for all gnuplot
+code blocks.  Also see @ref{epilogue}.
+
+@lisp
+(add-to-list 'org-babel-default-header-args:gnuplot
+             '((:prologue . "reset")))
+@end lisp
+
+@node epilogue, , prologue, Specific header arguments
+
+The value of the @code{epilogue} header argument will be appended to the code
+block body before execution.  Also see @ref{prologue}.
+
 @node Results of evaluation, Noweb reference syntax, Header arguments, Working With Source Code
 @section Results of evaluation
 @cindex code block, results of evaluation

+ 10 - 1
lisp/ob-core.el

@@ -444,6 +444,7 @@ then run `org-babel-switch-to-session'."
     (dir	. :any)
     (eval	. ((never query)))
     (exports	. ((code results both none)))
+    (epilogue   . :any)
     (file	. :any)
     (file-desc  . :any)
     (hlines	. ((no yes)))
@@ -455,6 +456,7 @@ then run `org-babel-switch-to-session'."
     (noweb-sep  . :any)
     (padline	. ((yes no)))
     (post       . :any)
+    (prologue   . :any)
     (results	. ((file list vector table scalar verbatim)
 		   (raw html latex org code pp drawer)
 		   (replace silent none append prepend)
@@ -671,7 +673,14 @@ Expand a block of code with org-babel according to its header
 arguments.  This generic implementation of body expansion is
 called for languages which have not defined their own specific
 org-babel-expand-body:lang function."
-  (mapconcat #'identity (append var-lines (list body)) "\n"))
+  (let ((pro (cdr (assoc :prologue params)))
+	(epi (cdr (assoc :epilogue params))))
+    (mapconcat #'identity
+	       (append (when pro (list pro))
+		       var-lines
+		       (list body)
+		       (when epi (list epi)))
+	       "\n")))
 
 ;;;###autoload
 (defun org-babel-expand-src-block (&optional arg info params)

+ 5 - 10
lisp/ob-gnuplot.el

@@ -68,13 +68,6 @@
 
 (defvar *org-babel-gnuplot-missing* nil)
 
-(defcustom *org-babel-gnuplot-prefix* nil
-  "Optional prefix to send to gnuplot before the body of every code block.
-For example \"reset\" may be used to reset gnuplot between
-blocks."
-  :group 'org-babel
-  :type 'string)
-
 (defcustom *org-babel-gnuplot-terms*
   '((eps . "postscript eps"))
   "List of file extensions and the associated gnuplot terminal."
@@ -103,7 +96,9 @@ code."
   (save-window-excursion
     (let* ((vars (org-babel-gnuplot-process-vars params))
            (out-file (cdr (assoc :file params)))
-           (term (or (cdr (assoc :term params))
+	   (prologue (cdr (assoc :prologue params)))
+	   (epilogue (cdr (assoc :epilogue params)))
+	   (term (or (cdr (assoc :term params))
                      (when out-file
 		       (let ((ext (file-name-extension out-file)))
 			 (or (cdr (assoc (intern (downcase ext))
@@ -166,8 +161,8 @@ code."
 	      (setq body (replace-regexp-in-string
 			  (format "\\$%s" (car pair)) (cdr pair) body)))
 	    vars)
-      (when *org-babel-gnuplot-prefix*
-	(funcall add-to-body *org-babel-gnuplot-prefix*)))
+      (when prologue (funcall add-to-body prologue))
+      (when epilogue (setq body (concat body "\n" epilogue))))
     body))
 
 (defun org-babel-execute:gnuplot (body params)