diff options
| author | Carsten Dominik | 2008-10-12 11:53:26 +0000 |
|---|---|---|
| committer | Carsten Dominik | 2008-10-12 11:53:26 +0000 |
| commit | 006793d017b24aefc4a4709a91ca92cf9f4a5dc4 (patch) | |
| tree | 73b5fffd5f069f50f0195fb49ad1d6d18168e8c3 | |
| parent | a3c1e192ceb49b6a6dccdee604c1ba8b6a5d9f55 (diff) | |
| download | emacs-006793d017b24aefc4a4709a91ca92cf9f4a5dc4.tar.gz emacs-006793d017b24aefc4a4709a91ca92cf9f4a5dc4.zip | |
Switch coding system to unix.
| -rw-r--r-- | lisp/org/org-plot.el | 628 |
1 files changed, 314 insertions, 314 deletions
diff --git a/lisp/org/org-plot.el b/lisp/org/org-plot.el index f8e268de8da..1d495d85d5a 100644 --- a/lisp/org/org-plot.el +++ b/lisp/org/org-plot.el | |||
| @@ -1,314 +1,314 @@ | |||
| 1 | ;;; org-plot.el --- Support for plotting from Org-mode | 1 | ;;; org-plot.el --- Support for plotting from Org-mode |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 2008 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 2008 Free Software Foundation, Inc. |
| 4 | ;; | 4 | ;; |
| 5 | ;; Author: Eric Schulte <schulte dot eric at gmail dot com> | 5 | ;; Author: Eric Schulte <schulte dot eric at gmail dot com> |
| 6 | ;; Keywords: tables, plotting | 6 | ;; Keywords: tables, plotting |
| 7 | ;; Homepage: http://orgmode.org | 7 | ;; Homepage: http://orgmode.org |
| 8 | ;; Version: 6.06b | 8 | ;; Version: 6.06b |
| 9 | ;; | 9 | ;; |
| 10 | ;; This file is part of GNU Emacs. | 10 | ;; This file is part of GNU Emacs. |
| 11 | ;; | 11 | ;; |
| 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 13 | ;; it under the terms of the GNU General Public License as published by | 13 | ;; it under the terms of the GNU General Public License as published by |
| 14 | ;; the Free Software Foundation, either version 3 of the License, or | 14 | ;; the Free Software Foundation, either version 3 of the License, or |
| 15 | ;; (at your option) any later version. | 15 | ;; (at your option) any later version. |
| 16 | 16 | ||
| 17 | ;; GNU Emacs is distributed in the hope that it will be useful, | 17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | ;; GNU General Public License for more details. | 20 | ;; GNU General Public License for more details. |
| 21 | 21 | ||
| 22 | ;; You should have received a copy of the GNU General Public License | 22 | ;; You should have received a copy of the GNU General Public License |
| 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 24 | 24 | ||
| 25 | ;;; Commentary: | 25 | ;;; Commentary: |
| 26 | 26 | ||
| 27 | ;; Borrows ideas and a couple of lines of code from org-exp.el. | 27 | ;; Borrows ideas and a couple of lines of code from org-exp.el. |
| 28 | 28 | ||
| 29 | ;; Thanks to the org-mode mailing list for testing and implementation | 29 | ;; Thanks to the org-mode mailing list for testing and implementation |
| 30 | ;; and feature suggestions | 30 | ;; and feature suggestions |
| 31 | 31 | ||
| 32 | ;;; Code: | 32 | ;;; Code: |
| 33 | (require 'org) | 33 | (require 'org) |
| 34 | (require 'org-exp) | 34 | (require 'org-exp) |
| 35 | (require 'org-table) | 35 | (require 'org-table) |
| 36 | (eval-and-compile | 36 | (eval-and-compile |
| 37 | (require 'cl)) | 37 | (require 'cl)) |
| 38 | 38 | ||
| 39 | (declare-function gnuplot-delchar-or-maybe-eof "ext:gnuplot" (arg)) | 39 | (declare-function gnuplot-delchar-or-maybe-eof "ext:gnuplot" (arg)) |
| 40 | (declare-function gnuplot-mode "ext:gnuplot" ()) | 40 | (declare-function gnuplot-mode "ext:gnuplot" ()) |
| 41 | (declare-function gnuplot-send-buffer-to-gnuplot "ext:gnuplot" ()) | 41 | (declare-function gnuplot-send-buffer-to-gnuplot "ext:gnuplot" ()) |
| 42 | 42 | ||
| 43 | (defvar org-plot/gnuplot-default-options | 43 | (defvar org-plot/gnuplot-default-options |
| 44 | '((:plot-type . 2d) | 44 | '((:plot-type . 2d) |
| 45 | (:with . lines) | 45 | (:with . lines) |
| 46 | (:ind . 0)) | 46 | (:ind . 0)) |
| 47 | "Default options to gnuplot used by `org-plot/gnuplot'") | 47 | "Default options to gnuplot used by `org-plot/gnuplot'") |
| 48 | 48 | ||
| 49 | (defun org-plot/add-options-to-plist (p options) | 49 | (defun org-plot/add-options-to-plist (p options) |
| 50 | "Parse an OPTIONS line and set values in the property list P. | 50 | "Parse an OPTIONS line and set values in the property list P. |
| 51 | Returns the resulting property list." | 51 | Returns the resulting property list." |
| 52 | (let (o) | 52 | (let (o) |
| 53 | (when options | 53 | (when options |
| 54 | (let ((op '(("type" . :plot-type) | 54 | (let ((op '(("type" . :plot-type) |
| 55 | ("script" . :script) | 55 | ("script" . :script) |
| 56 | ("line" . :line) | 56 | ("line" . :line) |
| 57 | ("set" . :set) | 57 | ("set" . :set) |
| 58 | ("title" . :title) | 58 | ("title" . :title) |
| 59 | ("ind" . :ind) | 59 | ("ind" . :ind) |
| 60 | ("deps" . :deps) | 60 | ("deps" . :deps) |
| 61 | ("with" . :with) | 61 | ("with" . :with) |
| 62 | ("file" . :file) | 62 | ("file" . :file) |
| 63 | ("labels" . :labels) | 63 | ("labels" . :labels) |
| 64 | ("map" . :map))) | 64 | ("map" . :map))) |
| 65 | (multiples '("set" "line")) | 65 | (multiples '("set" "line")) |
| 66 | (regexp ":\\([\"][^\"]+?[\"]\\|[(][^)]+?[)]\\|[^ \t\n\r;,.]*\\)") | 66 | (regexp ":\\([\"][^\"]+?[\"]\\|[(][^)]+?[)]\\|[^ \t\n\r;,.]*\\)") |
| 67 | (start 0) | 67 | (start 0) |
| 68 | o) | 68 | o) |
| 69 | (while (setq o (pop op)) | 69 | (while (setq o (pop op)) |
| 70 | (if (member (car o) multiples) ;; keys with multiple values | 70 | (if (member (car o) multiples) ;; keys with multiple values |
| 71 | (while (string-match | 71 | (while (string-match |
| 72 | (concat (regexp-quote (car o)) regexp) | 72 | (concat (regexp-quote (car o)) regexp) |
| 73 | options start) | 73 | options start) |
| 74 | (setq start (match-end 0)) | 74 | (setq start (match-end 0)) |
| 75 | (setq p (plist-put p (cdr o) | 75 | (setq p (plist-put p (cdr o) |
| 76 | (cons (car (read-from-string | 76 | (cons (car (read-from-string |
| 77 | (match-string 1 options))) | 77 | (match-string 1 options))) |
| 78 | (plist-get p (cdr o))))) | 78 | (plist-get p (cdr o))))) |
| 79 | p) | 79 | p) |
| 80 | (if (string-match (concat (regexp-quote (car o)) regexp) | 80 | (if (string-match (concat (regexp-quote (car o)) regexp) |
| 81 | options) | 81 | options) |
| 82 | (setq p (plist-put p (cdr o) | 82 | (setq p (plist-put p (cdr o) |
| 83 | (car (read-from-string | 83 | (car (read-from-string |
| 84 | (match-string 1 options))))))))))) | 84 | (match-string 1 options))))))))))) |
| 85 | p) | 85 | p) |
| 86 | 86 | ||
| 87 | (defun org-plot/goto-nearest-table () | 87 | (defun org-plot/goto-nearest-table () |
| 88 | "Move the point forward to the beginning of nearest table. | 88 | "Move the point forward to the beginning of nearest table. |
| 89 | Return value is the point at the beginning of the table." | 89 | Return value is the point at the beginning of the table." |
| 90 | (interactive) (move-beginning-of-line 1) | 90 | (interactive) (move-beginning-of-line 1) |
| 91 | (while (not (or (org-at-table-p) (< 0 (forward-line 1))))) | 91 | (while (not (or (org-at-table-p) (< 0 (forward-line 1))))) |
| 92 | (goto-char (org-table-begin))) | 92 | (goto-char (org-table-begin))) |
| 93 | 93 | ||
| 94 | (defun org-plot/collect-options (&optional params) | 94 | (defun org-plot/collect-options (&optional params) |
| 95 | "Collect options from an org-plot '#+Plot:' line. | 95 | "Collect options from an org-plot '#+Plot:' line. |
| 96 | Accepts an optional property list PARAMS, to which the options | 96 | Accepts an optional property list PARAMS, to which the options |
| 97 | will be added. Returns the resulting property list." | 97 | will be added. Returns the resulting property list." |
| 98 | (interactive) | 98 | (interactive) |
| 99 | (let ((line (thing-at-point 'line))) | 99 | (let ((line (thing-at-point 'line))) |
| 100 | (if (string-match "#\\+PLOT: +\\(.*\\)$" line) | 100 | (if (string-match "#\\+PLOT: +\\(.*\\)$" line) |
| 101 | (org-plot/add-options-to-plist params (match-string 1 line)) | 101 | (org-plot/add-options-to-plist params (match-string 1 line)) |
| 102 | params))) | 102 | params))) |
| 103 | 103 | ||
| 104 | (defun org-plot-quote-tsv-field (s) | 104 | (defun org-plot-quote-tsv-field (s) |
| 105 | "Quote field S for export to gnuplot." | 105 | "Quote field S for export to gnuplot." |
| 106 | (if (string-match org-table-number-regexp s) s | 106 | (if (string-match org-table-number-regexp s) s |
| 107 | (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))) | 107 | (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))) |
| 108 | 108 | ||
| 109 | (defun org-plot/gnuplot-to-data (table data-file params) | 109 | (defun org-plot/gnuplot-to-data (table data-file params) |
| 110 | "Export TABLE to DATA-FILE in a format readable by gnuplot. | 110 | "Export TABLE to DATA-FILE in a format readable by gnuplot. |
| 111 | Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE." | 111 | Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE." |
| 112 | (with-temp-file | 112 | (with-temp-file |
| 113 | data-file (insert (orgtbl-to-generic | 113 | data-file (insert (orgtbl-to-generic |
| 114 | table | 114 | table |
| 115 | (org-combine-plists | 115 | (org-combine-plists |
| 116 | '(:sep "\t" :fmt org-plot-quote-tsv-field) | 116 | '(:sep "\t" :fmt org-plot-quote-tsv-field) |
| 117 | params)))) | 117 | params)))) |
| 118 | nil) | 118 | nil) |
| 119 | 119 | ||
| 120 | (defun org-plot/gnuplot-to-grid-data (table data-file params) | 120 | (defun org-plot/gnuplot-to-grid-data (table data-file params) |
| 121 | "Export the data in TABLE to DATA-FILE for gnuplot. | 121 | "Export the data in TABLE to DATA-FILE for gnuplot. |
| 122 | This means, in a format appropriate for grid plotting by gnuplot. | 122 | This means, in a format appropriate for grid plotting by gnuplot. |
| 123 | PARAMS specifies which columns of TABLE should be plotted as independant | 123 | PARAMS specifies which columns of TABLE should be plotted as independant |
| 124 | and dependant variables." | 124 | and dependant variables." |
| 125 | (interactive) | 125 | (interactive) |
| 126 | (let* ((ind (- (plist-get params :ind) 1)) | 126 | (let* ((ind (- (plist-get params :ind) 1)) |
| 127 | (deps (if (plist-member params :deps) | 127 | (deps (if (plist-member params :deps) |
| 128 | (mapcar (lambda (val) (- val 1)) (plist-get params :deps)) | 128 | (mapcar (lambda (val) (- val 1)) (plist-get params :deps)) |
| 129 | (let (collector) | 129 | (let (collector) |
| 130 | (dotimes (col (length (first table))) | 130 | (dotimes (col (length (first table))) |
| 131 | (setf collector (cons col collector))) | 131 | (setf collector (cons col collector))) |
| 132 | collector))) | 132 | collector))) |
| 133 | row-vals (counter 0)) | 133 | row-vals (counter 0)) |
| 134 | (when (>= ind 0) ;; collect values of ind col | 134 | (when (>= ind 0) ;; collect values of ind col |
| 135 | (setf row-vals (mapcar (lambda (row) (setf counter (+ 1 counter)) | 135 | (setf row-vals (mapcar (lambda (row) (setf counter (+ 1 counter)) |
| 136 | (cons counter (nth ind row))) table))) | 136 | (cons counter (nth ind row))) table))) |
| 137 | (when (or deps (>= ind 0)) ;; remove non-plotting columns | 137 | (when (or deps (>= ind 0)) ;; remove non-plotting columns |
| 138 | (setf deps (delq ind deps)) | 138 | (setf deps (delq ind deps)) |
| 139 | (setf table (mapcar (lambda (row) | 139 | (setf table (mapcar (lambda (row) |
| 140 | (dotimes (col (length row)) | 140 | (dotimes (col (length row)) |
| 141 | (unless (memq col deps) | 141 | (unless (memq col deps) |
| 142 | (setf (nth col row) nil))) | 142 | (setf (nth col row) nil))) |
| 143 | (delq nil row)) | 143 | (delq nil row)) |
| 144 | table))) | 144 | table))) |
| 145 | ;; write table to gnuplot grid datafile format | 145 | ;; write table to gnuplot grid datafile format |
| 146 | (with-temp-file data-file | 146 | (with-temp-file data-file |
| 147 | (let ((num-rows (length table)) (num-cols (length (first table))) | 147 | (let ((num-rows (length table)) (num-cols (length (first table))) |
| 148 | front-edge back-edge) | 148 | front-edge back-edge) |
| 149 | (flet ((gnuplot-row (col row value) | 149 | (flet ((gnuplot-row (col row value) |
| 150 | (setf col (+ 1 col)) (setf row (+ 1 row)) | 150 | (setf col (+ 1 col)) (setf row (+ 1 row)) |
| 151 | (format "%f %f %f\n%f %f %f\n" | 151 | (format "%f %f %f\n%f %f %f\n" |
| 152 | col (- row 0.5) value ;; lower edge | 152 | col (- row 0.5) value ;; lower edge |
| 153 | col (+ row 0.5) value))) ;; upper edge | 153 | col (+ row 0.5) value))) ;; upper edge |
| 154 | (dotimes (col num-cols) | 154 | (dotimes (col num-cols) |
| 155 | (dotimes (row num-rows) | 155 | (dotimes (row num-rows) |
| 156 | (setf back-edge | 156 | (setf back-edge |
| 157 | (concat back-edge | 157 | (concat back-edge |
| 158 | (gnuplot-row (- col 1) row (string-to-number | 158 | (gnuplot-row (- col 1) row (string-to-number |
| 159 | (nth col (nth row table)))))) | 159 | (nth col (nth row table)))))) |
| 160 | (setf front-edge | 160 | (setf front-edge |
| 161 | (concat front-edge | 161 | (concat front-edge |
| 162 | (gnuplot-row col row (string-to-number | 162 | (gnuplot-row col row (string-to-number |
| 163 | (nth col (nth row table))))))) | 163 | (nth col (nth row table))))))) |
| 164 | ;; only insert once per row | 164 | ;; only insert once per row |
| 165 | (insert back-edge) (insert "\n") ;; back edge | 165 | (insert back-edge) (insert "\n") ;; back edge |
| 166 | (insert front-edge) (insert "\n") ;; front edge | 166 | (insert front-edge) (insert "\n") ;; front edge |
| 167 | (setf back-edge "") (setf front-edge ""))))) | 167 | (setf back-edge "") (setf front-edge ""))))) |
| 168 | row-vals)) | 168 | row-vals)) |
| 169 | 169 | ||
| 170 | (defun org-plot/gnuplot-script (data-file num-cols params) | 170 | (defun org-plot/gnuplot-script (data-file num-cols params) |
| 171 | "Write a gnuplot script to DATA-FILE respecting the options set in PARAMS. | 171 | "Write a gnuplot script to DATA-FILE respecting the options set in PARAMS. |
| 172 | NUM-COLS controls the number of columns plotted in a 2-d plot." | 172 | NUM-COLS controls the number of columns plotted in a 2-d plot." |
| 173 | (let* ((type (plist-get params :plot-type)) | 173 | (let* ((type (plist-get params :plot-type)) |
| 174 | (with (if (equal type 'grid) | 174 | (with (if (equal type 'grid) |
| 175 | 'pm3d | 175 | 'pm3d |
| 176 | (plist-get params :with))) | 176 | (plist-get params :with))) |
| 177 | (sets (plist-get params :set)) | 177 | (sets (plist-get params :set)) |
| 178 | (lines (plist-get params :line)) | 178 | (lines (plist-get params :line)) |
| 179 | (map (plist-get params :map)) | 179 | (map (plist-get params :map)) |
| 180 | (title (plist-get params :title)) | 180 | (title (plist-get params :title)) |
| 181 | (file (plist-get params :file)) | 181 | (file (plist-get params :file)) |
| 182 | (ind (plist-get params :ind)) | 182 | (ind (plist-get params :ind)) |
| 183 | (text-ind (plist-get params :textind)) | 183 | (text-ind (plist-get params :textind)) |
| 184 | (deps (if (plist-member params :deps) (plist-get params :deps))) | 184 | (deps (if (plist-member params :deps) (plist-get params :deps))) |
| 185 | (col-labels (plist-get params :labels)) | 185 | (col-labels (plist-get params :labels)) |
| 186 | (x-labels (plist-get params :xlabels)) | 186 | (x-labels (plist-get params :xlabels)) |
| 187 | (y-labels (plist-get params :ylabels)) | 187 | (y-labels (plist-get params :ylabels)) |
| 188 | (plot-str "'%s' using %s%d%s with %s title '%s'") | 188 | (plot-str "'%s' using %s%d%s with %s title '%s'") |
| 189 | (plot-cmd (case type | 189 | (plot-cmd (case type |
| 190 | ('2d "plot") | 190 | ('2d "plot") |
| 191 | ('3d "splot") | 191 | ('3d "splot") |
| 192 | ('grid "splot"))) | 192 | ('grid "splot"))) |
| 193 | (script "reset") plot-lines) | 193 | (script "reset") plot-lines) |
| 194 | (flet ((add-to-script (line) (setf script (format "%s\n%s" script line)))) | 194 | (flet ((add-to-script (line) (setf script (format "%s\n%s" script line)))) |
| 195 | (when file ;; output file | 195 | (when file ;; output file |
| 196 | (add-to-script (format "set term %s" (file-name-extension file))) | 196 | (add-to-script (format "set term %s" (file-name-extension file))) |
| 197 | (add-to-script (format "set output '%s'" file))) | 197 | (add-to-script (format "set output '%s'" file))) |
| 198 | (case type ;; type | 198 | (case type ;; type |
| 199 | ('2d ()) | 199 | ('2d ()) |
| 200 | ('3d (if map (add-to-script "set map"))) | 200 | ('3d (if map (add-to-script "set map"))) |
| 201 | ('grid (if map | 201 | ('grid (if map |
| 202 | (add-to-script "set pm3d map") | 202 | (add-to-script "set pm3d map") |
| 203 | (add-to-script "set pm3d")))) | 203 | (add-to-script "set pm3d")))) |
| 204 | (when title (add-to-script (format "set title '%s'" title))) ;; title | 204 | (when title (add-to-script (format "set title '%s'" title))) ;; title |
| 205 | (when lines (mapc (lambda (el) (add-to-script el)) lines)) ;; line | 205 | (when lines (mapc (lambda (el) (add-to-script el)) lines)) ;; line |
| 206 | (when sets ;; set | 206 | (when sets ;; set |
| 207 | (mapc (lambda (el) (add-to-script (format "set %s" el))) sets)) | 207 | (mapc (lambda (el) (add-to-script (format "set %s" el))) sets)) |
| 208 | (when x-labels ;; x labels (xtics) | 208 | (when x-labels ;; x labels (xtics) |
| 209 | (add-to-script | 209 | (add-to-script |
| 210 | (format "set xtics (%s)" | 210 | (format "set xtics (%s)" |
| 211 | (mapconcat (lambda (pair) | 211 | (mapconcat (lambda (pair) |
| 212 | (format "\"%s\" %d" (cdr pair) (car pair))) | 212 | (format "\"%s\" %d" (cdr pair) (car pair))) |
| 213 | x-labels ", ")))) | 213 | x-labels ", ")))) |
| 214 | (when y-labels ;; y labels (ytics) | 214 | (when y-labels ;; y labels (ytics) |
| 215 | (add-to-script | 215 | (add-to-script |
| 216 | (format "set ytics (%s)" | 216 | (format "set ytics (%s)" |
| 217 | (mapconcat (lambda (pair) | 217 | (mapconcat (lambda (pair) |
| 218 | (format "\"%s\" %d" (cdr pair) (car pair))) | 218 | (format "\"%s\" %d" (cdr pair) (car pair))) |
| 219 | y-labels ", ")))) | 219 | y-labels ", ")))) |
| 220 | (case type ;; plot command | 220 | (case type ;; plot command |
| 221 | ('2d (dotimes (col num-cols) | 221 | ('2d (dotimes (col num-cols) |
| 222 | (unless (and (equal type '2d) | 222 | (unless (and (equal type '2d) |
| 223 | (or (and ind (equal (+ 1 col) ind)) | 223 | (or (and ind (equal (+ 1 col) ind)) |
| 224 | (and deps (not (member (+ 1 col) deps))))) | 224 | (and deps (not (member (+ 1 col) deps))))) |
| 225 | (setf plot-lines | 225 | (setf plot-lines |
| 226 | (cons | 226 | (cons |
| 227 | (format plot-str data-file | 227 | (format plot-str data-file |
| 228 | (or (and (not text-ind) ind | 228 | (or (and (not text-ind) ind |
| 229 | (> ind 0) (format "%d:" ind)) "") | 229 | (> ind 0) (format "%d:" ind)) "") |
| 230 | (+ 1 col) | 230 | (+ 1 col) |
| 231 | (if text-ind (format ":xticlabel(%d)" ind) "") | 231 | (if text-ind (format ":xticlabel(%d)" ind) "") |
| 232 | with | 232 | with |
| 233 | (or (nth col col-labels) (format "%d" (+ 1 col)))) | 233 | (or (nth col col-labels) (format "%d" (+ 1 col)))) |
| 234 | plot-lines))))) | 234 | plot-lines))))) |
| 235 | ('3d | 235 | ('3d |
| 236 | (setq plot-lines (list (format "'%s' matrix with %s title ''" | 236 | (setq plot-lines (list (format "'%s' matrix with %s title ''" |
| 237 | data-file with)))) | 237 | data-file with)))) |
| 238 | ('grid | 238 | ('grid |
| 239 | (setq plot-lines (list (format "'%s' with %s title ''" | 239 | (setq plot-lines (list (format "'%s' with %s title ''" |
| 240 | data-file with))))) | 240 | data-file with))))) |
| 241 | (add-to-script | 241 | (add-to-script |
| 242 | (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) ",\\\n "))) | 242 | (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) ",\\\n "))) |
| 243 | script))) | 243 | script))) |
| 244 | 244 | ||
| 245 | ;;----------------------------------------------------------------------------- | 245 | ;;----------------------------------------------------------------------------- |
| 246 | ;; facade functions | 246 | ;; facade functions |
| 247 | ;;;###autoload | 247 | ;;;###autoload |
| 248 | (defun org-plot/gnuplot (&optional params) | 248 | (defun org-plot/gnuplot (&optional params) |
| 249 | "Plot table using gnuplot. Gnuplot options can be specified with PARAMS. | 249 | "Plot table using gnuplot. Gnuplot options can be specified with PARAMS. |
| 250 | If not given options will be taken from the +PLOT | 250 | If not given options will be taken from the +PLOT |
| 251 | line directly before or after the table." | 251 | line directly before or after the table." |
| 252 | (interactive) | 252 | (interactive) |
| 253 | (require 'gnuplot) | 253 | (require 'gnuplot) |
| 254 | (save-window-excursion | 254 | (save-window-excursion |
| 255 | (delete-other-windows) | 255 | (delete-other-windows) |
| 256 | (when (get-buffer "*gnuplot*") ;; reset *gnuplot* if it already running | 256 | (when (get-buffer "*gnuplot*") ;; reset *gnuplot* if it already running |
| 257 | (save-excursion | 257 | (save-excursion |
| 258 | (set-buffer "*gnuplot*") (goto-char (point-max)) | 258 | (set-buffer "*gnuplot*") (goto-char (point-max)) |
| 259 | (gnuplot-delchar-or-maybe-eof nil))) | 259 | (gnuplot-delchar-or-maybe-eof nil))) |
| 260 | (org-plot/goto-nearest-table) | 260 | (org-plot/goto-nearest-table) |
| 261 | ;; set default options | 261 | ;; set default options |
| 262 | (mapc | 262 | (mapc |
| 263 | (lambda (pair) | 263 | (lambda (pair) |
| 264 | (unless (plist-member params (car pair)) | 264 | (unless (plist-member params (car pair)) |
| 265 | (setf params (plist-put params (car pair) (cdr pair))))) | 265 | (setf params (plist-put params (car pair) (cdr pair))))) |
| 266 | org-plot/gnuplot-default-options) | 266 | org-plot/gnuplot-default-options) |
| 267 | ;; collect table and table information | 267 | ;; collect table and table information |
| 268 | (let* ((data-file (make-temp-file "org-plot")) | 268 | (let* ((data-file (make-temp-file "org-plot")) |
| 269 | (table (org-table-to-lisp)) | 269 | (table (org-table-to-lisp)) |
| 270 | (num-cols (length (if (eq (first table) 'hline) (second table) | 270 | (num-cols (length (if (eq (first table) 'hline) (second table) |
| 271 | (first table))))) | 271 | (first table))))) |
| 272 | (while (equal 'hline (first table)) (setf table (cdr table))) | 272 | (while (equal 'hline (first table)) (setf table (cdr table))) |
| 273 | (when (equal (second table) 'hline) | 273 | (when (equal (second table) 'hline) |
| 274 | (setf params (plist-put params :labels (first table))) ;; headers to labels | 274 | (setf params (plist-put params :labels (first table))) ;; headers to labels |
| 275 | (setf table (delq 'hline (cdr table)))) ;; clean non-data from table | 275 | (setf table (delq 'hline (cdr table)))) ;; clean non-data from table |
| 276 | ;; collect options | 276 | ;; collect options |
| 277 | (save-excursion (while (and (equal 0 (forward-line -1)) | 277 | (save-excursion (while (and (equal 0 (forward-line -1)) |
| 278 | (looking-at "#\\+")) | 278 | (looking-at "#\\+")) |
| 279 | (setf params (org-plot/collect-options params)))) | 279 | (setf params (org-plot/collect-options params)))) |
| 280 | ;; dump table to datafile (very different for grid) | 280 | ;; dump table to datafile (very different for grid) |
| 281 | (case (plist-get params :plot-type) | 281 | (case (plist-get params :plot-type) |
| 282 | ('2d (org-plot/gnuplot-to-data table data-file params)) | 282 | ('2d (org-plot/gnuplot-to-data table data-file params)) |
| 283 | ('3d (org-plot/gnuplot-to-data table data-file params)) | 283 | ('3d (org-plot/gnuplot-to-data table data-file params)) |
| 284 | ('grid (let ((y-labels (org-plot/gnuplot-to-grid-data | 284 | ('grid (let ((y-labels (org-plot/gnuplot-to-grid-data |
| 285 | table data-file params))) | 285 | table data-file params))) |
| 286 | (when y-labels (plist-put params :ylabels y-labels))))) | 286 | (when y-labels (plist-put params :ylabels y-labels))))) |
| 287 | ;; check for text ind column | 287 | ;; check for text ind column |
| 288 | (let ((ind (- (plist-get params :ind) 1))) | 288 | (let ((ind (- (plist-get params :ind) 1))) |
| 289 | (when (and (>= ind 0) (equal '2d (plist-get params :plot-type))) | 289 | (when (and (>= ind 0) (equal '2d (plist-get params :plot-type))) |
| 290 | (if (> (length | 290 | (if (> (length |
| 291 | (delq 0 (mapcar | 291 | (delq 0 (mapcar |
| 292 | (lambda (el) | 292 | (lambda (el) |
| 293 | (if (string-match org-table-number-regexp el) | 293 | (if (string-match org-table-number-regexp el) |
| 294 | 0 1)) | 294 | 0 1)) |
| 295 | (mapcar (lambda (row) (nth ind row)) table)))) 0) | 295 | (mapcar (lambda (row) (nth ind row)) table)))) 0) |
| 296 | (plist-put params :textind t)))) | 296 | (plist-put params :textind t)))) |
| 297 | ;; write script | 297 | ;; write script |
| 298 | (with-temp-buffer | 298 | (with-temp-buffer |
| 299 | (if (plist-get params :script) ;; user script | 299 | (if (plist-get params :script) ;; user script |
| 300 | (progn (insert-file-contents (plist-get params :script)) | 300 | (progn (insert-file-contents (plist-get params :script)) |
| 301 | (goto-char (point-min)) | 301 | (goto-char (point-min)) |
| 302 | (while (re-search-forward "$datafile" nil t) | 302 | (while (re-search-forward "$datafile" nil t) |
| 303 | (replace-match data-file nil nil))) | 303 | (replace-match data-file nil nil))) |
| 304 | (insert | 304 | (insert |
| 305 | (org-plot/gnuplot-script data-file num-cols params))) | 305 | (org-plot/gnuplot-script data-file num-cols params))) |
| 306 | ;; graph table | 306 | ;; graph table |
| 307 | (gnuplot-mode) | 307 | (gnuplot-mode) |
| 308 | (gnuplot-send-buffer-to-gnuplot)) | 308 | (gnuplot-send-buffer-to-gnuplot)) |
| 309 | ;; cleanup | 309 | ;; cleanup |
| 310 | (bury-buffer (get-buffer "*gnuplot*"))(delete-file data-file)))) | 310 | (bury-buffer (get-buffer "*gnuplot*"))(delete-file data-file)))) |
| 311 | 311 | ||
| 312 | (provide 'org-plot) | 312 | (provide 'org-plot) |
| 313 | 313 | ||
| 314 | ;;; org-plot.el ends here | 314 | ;;; org-plot.el ends here |