summaryrefslogtreecommitdiff
path: root/lisp/org/org-plot.el
diff options
context:
space:
mode:
authorRasmus <rasmus@gmx.us>2017-06-21 13:20:20 +0200
committerRasmus <rasmus@gmx.us>2017-06-22 11:54:18 +0200
commit5cecd275820df825c51bf9a27fcc7e35f30ff273 (patch)
treeb3f72e63953613d565e6d5a35bec97f158eb603c /lisp/org/org-plot.el
parent386a3da920482b8cb3e962fb944d135c8a770e26 (diff)
downloademacs-5cecd275820df825c51bf9a27fcc7e35f30ff273.tar.gz
Update Org to v9.0.9
Please see etc/ORG-NEWS for details.
Diffstat (limited to 'lisp/org/org-plot.el')
-rw-r--r--lisp/org/org-plot.el233
1 files changed, 115 insertions, 118 deletions
diff --git a/lisp/org/org-plot.el b/lisp/org/org-plot.el
index 5ccfbb1e662..449143a47af 100644
--- a/lisp/org/org-plot.el
+++ b/lisp/org/org-plot.el
@@ -1,4 +1,4 @@
-;;; org-plot.el --- Support for plotting from Org-mode
+;;; org-plot.el --- Support for Plotting from Org -*- lexical-binding: t; -*-
;; Copyright (C) 2008-2017 Free Software Foundation, Inc.
;;
@@ -25,14 +25,14 @@
;; Borrows ideas and a couple of lines of code from org-exp.el.
-;; Thanks to the org-mode mailing list for testing and implementation
-;; and feature suggestions
+;; Thanks to the Org mailing list for testing and implementation and
+;; feature suggestions
;;; Code:
+
+(require 'cl-lib)
(require 'org)
(require 'org-table)
-(eval-when-compile
- (require 'cl))
(declare-function gnuplot-delchar-or-maybe-eof "ext:gnuplot" (arg))
(declare-function gnuplot-mode "ext:gnuplot" ())
@@ -49,41 +49,39 @@
(defun org-plot/add-options-to-plist (p options)
"Parse an OPTIONS line and set values in the property list P.
Returns the resulting property list."
- (let (o)
- (when options
- (let ((op '(("type" . :plot-type)
- ("script" . :script)
- ("line" . :line)
- ("set" . :set)
- ("title" . :title)
- ("ind" . :ind)
- ("deps" . :deps)
- ("with" . :with)
- ("file" . :file)
- ("labels" . :labels)
- ("map" . :map)
- ("timeind" . :timeind)
- ("timefmt" . :timefmt)))
- (multiples '("set" "line"))
- (regexp ":\\([\"][^\"]+?[\"]\\|[(][^)]+?[)]\\|[^ \t\n\r;,.]*\\)")
- (start 0)
- o)
- (while (setq o (pop op))
- (if (member (car o) multiples) ;; keys with multiple values
- (while (string-match
- (concat (regexp-quote (car o)) regexp)
- options start)
- (setq start (match-end 0))
- (setq p (plist-put p (cdr o)
- (cons (car (read-from-string
- (match-string 1 options)))
- (plist-get p (cdr o)))))
- p)
- (if (string-match (concat (regexp-quote (car o)) regexp)
- options)
- (setq p (plist-put p (cdr o)
- (car (read-from-string
- (match-string 1 options)))))))))))
+ (when options
+ (let ((op '(("type" . :plot-type)
+ ("script" . :script)
+ ("line" . :line)
+ ("set" . :set)
+ ("title" . :title)
+ ("ind" . :ind)
+ ("deps" . :deps)
+ ("with" . :with)
+ ("file" . :file)
+ ("labels" . :labels)
+ ("map" . :map)
+ ("timeind" . :timeind)
+ ("timefmt" . :timefmt)))
+ (multiples '("set" "line"))
+ (regexp ":\\([\"][^\"]+?[\"]\\|[(][^)]+?[)]\\|[^ \t\n\r;,.]*\\)")
+ (start 0))
+ (dolist (o op)
+ (if (member (car o) multiples) ;; keys with multiple values
+ (while (string-match
+ (concat (regexp-quote (car o)) regexp)
+ options start)
+ (setq start (match-end 0))
+ (setq p (plist-put p (cdr o)
+ (cons (car (read-from-string
+ (match-string 1 options)))
+ (plist-get p (cdr o)))))
+ p)
+ (if (string-match (concat (regexp-quote (car o)) regexp)
+ options)
+ (setq p (plist-put p (cdr o)
+ (car (read-from-string
+ (match-string 1 options))))))))))
p)
(defun org-plot/goto-nearest-table ()
@@ -119,10 +117,9 @@ will be added. Returns the resulting property list."
Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
(with-temp-file
data-file
- (make-local-variable 'org-plot-timestamp-fmt)
- (setq org-plot-timestamp-fmt (or
- (plist-get params :timefmt)
- "%Y-%m-%d-%H:%M:%S"))
+ (setq-local org-plot-timestamp-fmt (or
+ (plist-get params :timefmt)
+ "%Y-%m-%d-%H:%M:%S"))
(insert (orgtbl-to-generic
table
(org-combine-plists
@@ -140,7 +137,7 @@ and dependant variables."
(deps (if (plist-member params :deps)
(mapcar (lambda (val) (- val 1)) (plist-get params :deps))
(let (collector)
- (dotimes (col (length (first table)))
+ (dotimes (col (length (nth 0 table)))
(setf collector (cons col collector)))
collector)))
(counter 0)
@@ -158,7 +155,7 @@ and dependant variables."
table)))
;; write table to gnuplot grid datafile format
(with-temp-file data-file
- (let ((num-rows (length table)) (num-cols (length (first table)))
+ (let ((num-rows (length table)) (num-cols (length (nth 0 table)))
(gnuplot-row (lambda (col row value)
(setf col (+ 1 col)) (setf row (+ 1 row))
(format "%f %f %f\n%f %f %f\n"
@@ -187,9 +184,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))
@@ -204,68 +199,72 @@ manner suitable for prepending to a user-specified script."
(x-labels (plist-get params :xlabels))
(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")))
+ (plot-cmd (pcase type
+ (`2d "plot")
+ (`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)
+ (pcase type ; type
+ (`2d ())
+ (`3d (when map (funcall ats "set map")))
+ (`grid (funcall ats (if map "set pm3d map" "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
+ ;; Unless specified otherwise, values are TAB separated.
+ (unless (string-match-p "^set datafile separator" script)
+ (funcall ats "set datafile separator \"\\t\""))
+ (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)))))
+ (pcase 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)
+ (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))))
+ (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)))))
(funcall ats
- (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) ",\\\n "))))
+ (concat plot-cmd " " (mapconcat #'identity
+ (reverse plot-lines)
+ ",\\\n "))))
script))
;;-----------------------------------------------------------------------------
@@ -279,59 +278,59 @@ 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))
- (gnuplot-delchar-or-maybe-eof nil)))
+ (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)))))
- (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
+ (num-cols (length (if (eq (nth 0 table) 'hline) (nth 1 table)
+ (nth 0 table)))))
+ (run-with-idle-timer 0.1 nil #'delete-file data-file)
+ (while (eq 'hline (car table)) (setf table (cdr table)))
+ (when (eq (cadr table) 'hline)
+ (setf params
+ (plist-put params :labels (nth 0 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)
- (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
+ ;; Dump table to datafile (very different for grid).
+ (pcase (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)))
+ ;; 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,14 +338,12 @@ 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
- (bury-buffer (get-buffer "*gnuplot*"))
- (run-with-idle-timer 0.1 nil (lambda () (delete-file data-file))))))
+ ;; Cleanup.
+ (bury-buffer (get-buffer "*gnuplot*")))))
(provide 'org-plot)