]> code.delx.au - gnu-emacs/blobdiff - lisp/org/org-plot.el
Merge branch 'master' of git.savannah.gnu.org:/srv/git/emacs
[gnu-emacs] / lisp / org / org-plot.el
index 10722403f7e5ebec39ce5303f9e36b58fa7fb3f2..066fbc934da20e042185d3a190413dba6548399b 100644 (file)
@@ -1,11 +1,10 @@
 ;;; org-plot.el --- Support for plotting from Org-mode
 
-;; Copyright (C) 2008-2011 Free Software Foundation, Inc.
+;; Copyright (C) 2008-2016 Free Software Foundation, Inc.
 ;;
 ;; Author: Eric Schulte <schulte dot eric at gmail dot com>
 ;; Keywords: tables, plotting
 ;; Homepage: http://orgmode.org
-;; Version: 7.4
 ;;
 ;; This file is part of GNU Emacs.
 ;;
@@ -31,7 +30,6 @@
 
 ;;; Code:
 (require 'org)
-(require 'org-exp)
 (require 'org-table)
 (eval-when-compile
   (require 'cl))
@@ -96,7 +94,7 @@ Return value is the point at the beginning of the table."
   (goto-char (org-table-begin)))
 
 (defun org-plot/collect-options (&optional params)
-  "Collect options from an org-plot '#+Plot:' line.
+  "Collect options from an org-plot `#+Plot:' line.
 Accepts an optional property list PARAMS, to which the options
 will be added.  Returns the resulting property list."
   (interactive)
@@ -145,7 +143,8 @@ and dependant variables."
                   (dotimes (col (length (first table)))
                     (setf collector (cons col collector)))
                   collector)))
-        row-vals (counter 0))
+        (counter 0)
+        row-vals)
     (when (>= ind 0) ;; collect values of ind col
       (setf row-vals (mapcar (lambda (row) (setf counter (+ 1 counter))
                               (cons counter (nth ind row))) table)))
@@ -160,26 +159,26 @@ and dependant variables."
     ;; write table to gnuplot grid datafile format
     (with-temp-file data-file
       (let ((num-rows (length table)) (num-cols (length (first table)))
+           (gnuplot-row (lambda (col row value)
+                          (setf col (+ 1 col)) (setf row (+ 1 row))
+                          (format "%f  %f  %f\n%f  %f  %f\n"
+                                  col (- row 0.5) value ;; lower edge
+                                  col (+ row 0.5) value))) ;; upper edge
            front-edge back-edge)
-       (flet ((gnuplot-row (col row value)
-                           (setf col (+ 1 col)) (setf row (+ 1 row))
-                           (format "%f  %f  %f\n%f  %f  %f\n"
-                                   col (- row 0.5) value ;; lower edge
-                                   col (+ row 0.5) value))) ;; upper edge
-         (dotimes (col num-cols)
-           (dotimes (row num-rows)
-             (setf back-edge
-                   (concat back-edge
-                           (gnuplot-row (- col 1) row (string-to-number
-                                                       (nth col (nth row table))))))
-             (setf front-edge
-                   (concat front-edge
-                           (gnuplot-row col row (string-to-number
-                                                 (nth col (nth row table)))))))
-           ;; only insert once per row
-           (insert back-edge) (insert "\n") ;; back edge
-           (insert front-edge) (insert "\n") ;; front edge
-           (setf back-edge "") (setf front-edge "")))))
+       (dotimes (col num-cols)
+         (dotimes (row num-rows)
+           (setf back-edge
+                 (concat back-edge
+                         (funcall gnuplot-row (- col 1) row
+                                  (string-to-number (nth col (nth row table))))))
+           (setf front-edge
+                 (concat front-edge
+                         (funcall gnuplot-row col row
+                                  (string-to-number (nth col (nth row table)))))))
+         ;; only insert once per row
+         (insert back-edge) (insert "\n") ;; back edge
+         (insert front-edge) (insert "\n") ;; front edge
+         (setf back-edge "") (setf front-edge ""))))
     row-vals))
 
 (defun org-plot/gnuplot-script (data-file num-cols params &optional preface)
@@ -206,44 +205,45 @@ manner suitable for prepending to a user-specified script."
         (y-labels (plist-get params :ylabels))
         (plot-str "'%s' using %s%d%s with %s title '%s'")
         (plot-cmd (case type
-                    (2d "plot")
-                    (3d "splot")
-                    (grid "splot")))
-        (script "reset") plot-lines)
-    (flet ((add-to-script (line) (setf script (format "%s\n%s" script line))))
-      (when file ;; output file
-       (add-to-script (format "set term %s" (file-name-extension file)))
-       (add-to-script (format "set output '%s'" file)))
-      (case type ;; type
-       (2d ())
-       (3d (if map (add-to-script "set map")))
-       (grid (if map
-                  (add-to-script "set pm3d map")
-                (add-to-script "set pm3d"))))
-      (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
-                                  "%Y-%m-%d-%H:%M:%S") "\"")))
-      (unless preface
-        (case type ;; plot command
-       (2d (dotimes (col num-cols)
+                    ('2d "plot")
+                    ('3d "splot")
+                    ('grid "splot")))
+        (script "reset")
+                                       ; ats = add-to-script
+        (ats (lambda (line) (setf script (format "%s\n%s" script line))))
+        plot-lines)
+    (when file ;; output file
+      (funcall ats (format "set term %s" (file-name-extension file)))
+      (funcall ats (format "set output '%s'" file)))
+    (case type ;; type
+      ('2d ())
+      ('3d (if map (funcall ats "set map")))
+      ('grid (if map (funcall ats "set pm3d map")
+              (funcall ats "set pm3d"))))
+    (when title (funcall ats (format "set title '%s'" title))) ;; title
+    (when lines (mapc (lambda (el) (funcall ats el)) lines)) ;; line
+    (when sets ;; set
+      (mapc (lambda (el) (funcall ats (format "set %s" el))) sets))
+    (when x-labels ;; x labels (xtics)
+      (funcall ats
+              (format "set xtics (%s)"
+                      (mapconcat (lambda (pair)
+                                   (format "\"%s\" %d" (cdr pair) (car pair)))
+                                 x-labels ", "))))
+    (when y-labels ;; y labels (ytics)
+      (funcall ats
+              (format "set ytics (%s)"
+                      (mapconcat (lambda (pair)
+                                   (format "\"%s\" %d" (cdr pair) (car pair)))
+                                 y-labels ", "))))
+    (when time-ind ;; timestamp index
+      (funcall ats "set xdata time")
+      (funcall ats (concat "set timefmt \""
+                          (or timefmt ;; timefmt passed to gnuplot
+                              "%Y-%m-%d-%H:%M:%S") "\"")))
+    (unless preface
+      (case type ;; plot command
+       ('2d (dotimes (col num-cols)
               (unless (and (equal type '2d)
                            (or (and ind (equal (+ 1 col) ind))
                                (and deps (not (member (+ 1 col) deps)))))
@@ -258,15 +258,15 @@ manner suitable for prepending to a user-specified script."
                                with
                                (or (nth col col-labels) (format "%d" (+ 1 col))))
                        plot-lines)))))
-       (3d
+       ('3d
         (setq plot-lines (list (format "'%s' matrix with %s title ''"
                                        data-file with))))
-       (grid
+       ('grid
         (setq plot-lines (list (format "'%s' with %s title ''"
                                        data-file with)))))
-        (add-to-script
-         (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) ",\\\n    "))))
-      script)))
+      (funcall ats
+              (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) ",\\\n    "))))
+    script))
 
 ;;-----------------------------------------------------------------------------
 ;; facade functions
@@ -305,9 +305,9 @@ line directly before or after the table."
                        (setf params (org-plot/collect-options params))))
       ;; dump table to datafile (very different for grid)
       (case (plist-get params :plot-type)
-       (2d   (org-plot/gnuplot-to-data table data-file params))
-       (3d   (org-plot/gnuplot-to-data table data-file params))
-       (grid (let ((y-labels (org-plot/gnuplot-to-grid-data
+       ('2d   (org-plot/gnuplot-to-data table data-file params))
+       ('3d   (org-plot/gnuplot-to-data table data-file params))
+       ('grid (let ((y-labels (org-plot/gnuplot-to-grid-data
                                table data-file params)))
                 (when y-labels (plist-put params :ylabels y-labels)))))
       ;; check for timestamp ind column
@@ -350,4 +350,8 @@ line directly before or after the table."
 
 (provide 'org-plot)
 
+;; Local variables:
+;; generated-autoload-file: "org-loaddefs.el"
+;; End:
+
 ;;; org-plot.el ends here