瀏覽代碼

added a :file header argument to gnuplot

Eric Schulte 16 年之前
父節點
當前提交
c81479b161
共有 2 個文件被更改,包括 53 次插入39 次删除
  1. 42 36
      lisp/langs/org-babel-gnuplot.el
  2. 11 3
      org-babel.org

+ 42 - 36
lisp/langs/org-babel-gnuplot.el

@@ -68,6 +68,8 @@ called by `org-babel-execute-src-block'."
   (let* ((vars (org-babel-gnuplot-process-vars params))
          (result-params (split-string (or (cdr (assoc :results params)) "")))
          (out-file (cdr (assoc :file params)))
+         (term (or (cdr (assoc :term params))
+                   (when out-file (file-name-extension out-file))))
          (cmdline (cdr (assoc :cmdline params)))
          (in-file (make-temp-file "org-babel-ditaa"))
 	 (title (plist-get params :title))
@@ -75,43 +77,47 @@ called by `org-babel-execute-src-block'."
 	 (sets (plist-get params :set))
 	 (x-labels (plist-get params :xlabels))
 	 (y-labels (plist-get params :ylabels))
-	 (time-ind (plist-get params :timeind)))
-    ;; insert variables into code body
-    (setq body
-          (concat
-           (mapconcat
-            (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
-            vars "\n")
-           "\n"
-           body))
-    ;; append header argument settings to body
-    (when title (add-to-script (format "set title '%s'" title))) ;; title
-    (when lines (mapc (lambda (el) (add-to-script el)) lines)) ;; line
-    (when sets ;; set
-      (mapc (lambda (el) (add-to-script (format "set %s" el))) sets))
-    (when x-labels ;; x labels (xtics)
-      (add-to-script
-       (format "set xtics (%s)"
-               (mapconcat (lambda (pair)
-                            (format "\"%s\" %d" (cdr pair) (car pair)))
-                          x-labels ", "))))
-    (when y-labels ;; y labels (ytics)
-      (add-to-script
-       (format "set ytics (%s)"
-               (mapconcat (lambda (pair)
-                            (format "\"%s\" %d" (cdr pair) (car pair)))
-                          y-labels ", "))))
-    (when time-ind ;; timestamp index
-      (add-to-script "set xdata time")
-      (add-to-script (concat "set timefmt \""
-                             (or timefmt ;; timefmt passed to gnuplot
+	 (timefmt (plist-get params :timefmt))
+         (time-ind (or (plist-get params :timeind)
+                       (when timefmt 1))))
+    (flet ((add-to-body (text)
+                        (setq body (concat text "\n" body))))
+      ;; append header argument settings to body
+      (when title (add-to-body (format "set title '%s'" title))) ;; title
+      (when lines (mapc (lambda (el) (add-to-body el)) lines)) ;; line
+      (when sets
+        (mapc (lambda (el) (add-to-body (format "set %s" el))) sets))
+      (when x-labels
+        (add-to-body
+         (format "set xtics (%s)"
+                 (mapconcat (lambda (pair)
+                              (format "\"%s\" %d" (cdr pair) (car pair)))
+                            x-labels ", "))))
+      (when y-labels
+        (add-to-body
+         (format "set ytics (%s)"
+                 (mapconcat (lambda (pair)
+                              (format "\"%s\" %d" (cdr pair) (car pair)))
+                            y-labels ", "))))
+      (when time-ind
+        (add-to-body "set xdata time")
+        (add-to-body (concat "set timefmt \""
+                             (or timefmt
                                  "%Y-%m-%d-%H:%M:%S") "\"")))
-    ;; evaluate the code body with gnuplot
-    (with-temp-buffer
-      (insert (concat body "\n"))
-      (gnuplot-mode)
-      (gnuplot-send-buffer-to-gnuplot))
-    out-file))
+      (when out-file (add-to-body (format "set output \"%s\"" out-file)))
+      (when term (add-to-body (format "set term %s" term)))
+      ;; insert variables into code body: this should happen last
+      ;; placing the variables at the *top* of the code in case their
+      ;; values are used later
+      (add-to-body (mapconcat
+                    (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
+                    vars "\n"))
+      ;; evaluate the code body with gnuplot
+      (with-temp-buffer
+        (insert (concat body "\n"))
+        (gnuplot-mode)
+        (gnuplot-send-buffer-to-gnuplot))
+      out-file)))
 
 (defun org-babel-prep-session:gnuplot (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."

+ 11 - 3
org-babel.org

@@ -2095,7 +2095,7 @@ This could probably be added to [[file:lisp/org-babel-script.el][org-babel-scrip
 #+resname: implementing-ditaa
 [[file:blue.png][blue.png]]
 
-*** STARTED gnuplot [3/8]
+*** STARTED gnuplot [4/8]
 (see [[* file result types][file result types]])
 
 #+PLOT: title:"Citas" ind:1 deps:(3) type:2d with:histograms set:"yrange [0:]"
@@ -2117,7 +2117,7 @@ This could probably be added to [[file:lisp/org-babel-script.el][org-babel-scrip
 |             1.3 |          0.19615368 |           0.21730748 |
 
 #+srcname: implementing-gnuplot
-#+begin_src gnuplot :var data=gnuplot-data
+#+begin_src gnuplot :var data=gnuplot-data :results silent
 set title "Implementing Gnuplot"
 plot data using 1:2 with lines
 #+end_src
@@ -2144,9 +2144,17 @@ This may be walking too close to an entirely prebuilt plotting tool
 rather than straight gnuplot code evaluation.  For now I think this
 can wait.
 
-**** TODO a =file= header argument
+**** DONE a =file= header argument
 to specify a file holding the results
 
+#+srcname: gnuplot-to-file-implementation
+#+begin_src gnuplot :file plot.png :var data=gnuplot-data
+plot data using 1:2, data using 1:3 with lines
+#+end_src
+
+#+resname:
+[[file:plot.png][plot.png]]
+
 **** STARTED helpers from org-plot.el
 there are a variety of helpers in org-plot which can be fit nicely
 into custom gnuplot header arguments