Browse Source

org-plot: Tiny fixes

* lisp/org-plot.el (org-plot/gnuplot-script, org-plot/gnuplot): Tiny
  fixes.
Nicolas Goaziou 5 years ago
parent
commit
de7c599669
1 changed files with 67 additions and 71 deletions
  1. 67 71
      lisp/org-plot.el

+ 67 - 71
lisp/org-plot.el

@@ -1,6 +1,6 @@
 ;;; org-plot.el --- Support for plotting from Org-mode
 
-;; Copyright (C) 2008-2014 Free Software Foundation, Inc.
+;; Copyright (C) 2008-2015 Free Software Foundation, Inc.
 ;;
 ;; Author: Eric Schulte <schulte dot eric at gmail dot com>
 ;; Keywords: tables, plotting
@@ -187,9 +187,7 @@ NUM-COLS controls the number of columns plotted in a 2-d plot.
 Optional argument PREFACE returns only option parameters in a
 manner suitable for prepending to a user-specified script."
   (let* ((type (plist-get params :plot-type))
-	 (with (if (equal type 'grid)
-		   'pm3d
-		 (plist-get params :with)))
+	 (with (if (eq type 'grid) 'pm3d (plist-get params :with)))
 	 (sets (plist-get params :set))
 	 (lines (plist-get params :line))
 	 (map (plist-get params :map))
@@ -209,63 +207,63 @@ manner suitable for prepending to a user-specified script."
 		     ('3d "splot")
 		     ('grid "splot")))
 	 (script "reset")
-					; ats = add-to-script
-	 (ats (lambda (line) (setf script (format "%s\n%s" script line))))
+	 ;; ats = add-to-script
+	 (ats (lambda (line) (setf script (concat script "\n" line))))
 	 plot-lines)
-    (when file ;; output file
+    (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)
+    (case type				; type
+      (2d ())
+      (3d (when 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
+    (mapc ats lines)					       ; line
+    (dolist (el sets) (funcall ats (format "set %s" el)))      ; set
+    (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)
+    (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
+    (when time-ind			; timestamp index
       (funcall ats "set xdata time")
       (funcall ats (concat "set timefmt \""
-			   (or timefmt ;; timefmt passed to gnuplot
+			   (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)))))
-		 (setf plot-lines
-		       (cons
-			(format plot-str data-file
-				(or (and ind (> ind 0)
-                                         (not text-ind)
-                                         (format "%d:" ind)) "")
-				(+ 1 col)
-				(if text-ind (format ":xticlabel(%d)" ind) "")
-				with
-				(or (nth col col-labels) (format "%d" (+ 1 col))))
-			plot-lines)))))
-	('3d
+      (case type			; plot command
+	(2d (dotimes (col num-cols)
+	      (unless (and (eq type '2d)
+			   (or (and ind (equal (1+ col) ind))
+			       (and deps (not (member (1+ col) deps)))))
+		(setf plot-lines
+		      (cons
+		       (format plot-str data-file
+			       (or (and ind (> ind 0)
+					(not text-ind)
+					(format "%d:" ind)) "")
+			       (1+ col)
+			       (if text-ind (format ":xticlabel(%d)" ind) "")
+			       with
+			       (or (nth col col-labels) (format "%d" (1+ col))))
+		       plot-lines)))))
+	(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)))))
       (funcall ats
-	       (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) ",\\\n    "))))
+	       (concat plot-cmd " " (mapconcat #'identity
+					       (reverse plot-lines)
+					       ",\\\n    "))))
     script))
 
 ;;-----------------------------------------------------------------------------
@@ -279,59 +277,58 @@ line directly before or after the table."
   (require 'gnuplot)
   (save-window-excursion
     (delete-other-windows)
-    (when (get-buffer "*gnuplot*") ;; reset *gnuplot* if it already running
+    (when (get-buffer "*gnuplot*") ; reset *gnuplot* if it already running
       (with-current-buffer "*gnuplot*"
 	(goto-char (point-max))))
     (org-plot/goto-nearest-table)
-    ;; set default options
-    (mapc
-     (lambda (pair)
-       (unless (plist-member params (car pair))
-	 (setf params (plist-put params (car pair) (cdr pair)))))
-     org-plot/gnuplot-default-options)
+    ;; Set default options.
+    (dolist (pair org-plot/gnuplot-default-options)
+      (unless (plist-member params (car pair))
+	(setf params (plist-put params (car pair) (cdr pair)))))
     ;; collect table and table information
     (let* ((data-file (make-temp-file "org-plot"))
 	   (table (org-table-to-lisp))
 	   (num-cols (length (if (eq (first table) 'hline) (second table)
 			       (first table)))))
       (run-with-idle-timer 0.1 nil #'delete-file data-file)
-      (while (equal 'hline (first table)) (setf table (cdr table)))
-      (when (equal (second table) 'hline)
-	(setf params (plist-put params :labels (first table))) ;; headers to labels
-	(setf table (delq 'hline (cdr table)))) ;; clean non-data from table
-      ;; collect options
+      (while (eq 'hline (car table)) (setf table (cdr table)))
+      (when (eq (cadr table) 'hline)
+	(setf params (plist-put params :labels (first table))) ; headers to labels
+	(setf table (delq 'hline (cdr table)))) ; clean non-data from table
+      ;; Collect options.
       (save-excursion (while (and (equal 0 (forward-line -1))
 				  (looking-at "[[:space:]]*#\\+"))
 			(setf params (org-plot/collect-options params))))
-      ;; dump table to datafile (very different for grid)
+      ;; 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
-				table data-file params)))
-		 (when y-labels (plist-put params :ylabels y-labels)))))
-      ;; check for timestamp ind column
-      (let ((ind (- (plist-get params :ind) 1)))
-	(when (and (>= ind 0) (equal '2d (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
+			       table data-file params)))
+		(when y-labels (plist-put params :ylabels y-labels)))))
+      ;; Check for timestamp ind column.
+      (let ((ind (1- (plist-get params :ind))))
+	(when (and (>= ind 0) (eq '2d (plist-get params :plot-type)))
 	  (if (= (length
 		  (delq 0 (mapcar
 			   (lambda (el)
-			     (if (string-match org-ts-regexp3 el)
-				 0 1))
-			   (mapcar (lambda (row) (nth ind row)) table)))) 0)
+			     (if (string-match org-ts-regexp3 el) 0 1))
+			   (mapcar (lambda (row) (nth ind row)) table))))
+		 0)
 	      (plist-put params :timeind t)
-	    ;; check for text ind column
+	    ;; Check for text ind column.
 	    (if (or (string= (plist-get params :with) "hist")
 		    (> (length
 			(delq 0 (mapcar
 				 (lambda (el)
 				   (if (string-match org-table-number-regexp el)
 				       0 1))
-				 (mapcar (lambda (row) (nth ind row)) table)))) 0))
+				 (mapcar (lambda (row) (nth ind row)) table))))
+		       0))
 		(plist-put params :textind t)))))
-      ;; write script
+      ;; Write script.
       (with-temp-buffer
-	(if (plist-get params :script) ;; user script
+	(if (plist-get params :script)	; user script
 	    (progn (insert
                     (org-plot/gnuplot-script data-file num-cols params t))
                    (insert "\n")
@@ -339,12 +336,11 @@ line directly before or after the table."
                    (goto-char (point-min))
                    (while (re-search-forward "$datafile" nil t)
                      (replace-match data-file nil nil)))
-	  (insert
-	   (org-plot/gnuplot-script data-file num-cols params)))
-	;; graph table
+	  (insert (org-plot/gnuplot-script data-file num-cols params)))
+	;; Graph table.
 	(gnuplot-mode)
 	(gnuplot-send-buffer-to-gnuplot))
-      ;; cleanup
+      ;; Cleanup.
       (bury-buffer (get-buffer "*gnuplot*")))))
 
 (provide 'org-plot)