diff options
| author | Bastien Guerry | 2013-11-12 14:06:26 +0100 |
|---|---|---|
| committer | Bastien Guerry | 2013-11-12 14:06:26 +0100 |
| commit | 271672fad74cdbc9065d23d6e6cee1b8540f571b (patch) | |
| tree | d322b956ec0e74ee33b22354ef00839b23b1618d /lisp/org/ob-gnuplot.el | |
| parent | f201cf3a8143b0b34b07769fc7d73dd14761b87b (diff) | |
| download | emacs-271672fad74cdbc9065d23d6e6cee1b8540f571b.tar.gz emacs-271672fad74cdbc9065d23d6e6cee1b8540f571b.zip | |
Merge Org version 8.2.3a.
Diffstat (limited to 'lisp/org/ob-gnuplot.el')
| -rw-r--r-- | lisp/org/ob-gnuplot.el | 106 |
1 files changed, 75 insertions, 31 deletions
diff --git a/lisp/org/ob-gnuplot.el b/lisp/org/ob-gnuplot.el index 488d2508e6d..cc9186b1adc 100644 --- a/lisp/org/ob-gnuplot.el +++ b/lisp/org/ob-gnuplot.el | |||
| @@ -39,8 +39,6 @@ | |||
| 39 | 39 | ||
| 40 | ;;; Code: | 40 | ;;; Code: |
| 41 | (require 'ob) | 41 | (require 'ob) |
| 42 | (require 'ob-ref) | ||
| 43 | (require 'ob-comint) | ||
| 44 | (eval-when-compile (require 'cl)) | 42 | (eval-when-compile (require 'cl)) |
| 45 | 43 | ||
| 46 | (declare-function org-time-string-to-time "org" (s)) | 44 | (declare-function org-time-string-to-time "org" (s)) |
| @@ -54,77 +52,117 @@ | |||
| 54 | '((:results . "file") (:exports . "results") (:session . nil)) | 52 | '((:results . "file") (:exports . "results") (:session . nil)) |
| 55 | "Default arguments to use when evaluating a gnuplot source block.") | 53 | "Default arguments to use when evaluating a gnuplot source block.") |
| 56 | 54 | ||
| 55 | (defvar org-babel-header-args:gnuplot | ||
| 56 | '((title . :any) | ||
| 57 | (lines . :any) | ||
| 58 | (sets . :any) | ||
| 59 | (x-labels . :any) | ||
| 60 | (y-labels . :any) | ||
| 61 | (timefmt . :any) | ||
| 62 | (time-ind . :any) | ||
| 63 | (missing . :any) | ||
| 64 | (term . :any)) | ||
| 65 | "Gnuplot specific header args.") | ||
| 66 | |||
| 57 | (defvar org-babel-gnuplot-timestamp-fmt nil) | 67 | (defvar org-babel-gnuplot-timestamp-fmt nil) |
| 58 | 68 | ||
| 69 | (defvar *org-babel-gnuplot-missing* nil) | ||
| 70 | |||
| 71 | (defcustom *org-babel-gnuplot-terms* | ||
| 72 | '((eps . "postscript eps")) | ||
| 73 | "List of file extensions and the associated gnuplot terminal." | ||
| 74 | :group 'org-babel | ||
| 75 | :type '(repeat (cons (symbol :tag "File extension") | ||
| 76 | (string :tag "Gnuplot terminal")))) | ||
| 77 | |||
| 59 | (defun org-babel-gnuplot-process-vars (params) | 78 | (defun org-babel-gnuplot-process-vars (params) |
| 60 | "Extract variables from PARAMS and process the variables. | 79 | "Extract variables from PARAMS and process the variables. |
| 61 | Dumps all vectors into files and returns an association list | 80 | Dumps all vectors into files and returns an association list |
| 62 | of variable names and the related value to be used in the gnuplot | 81 | of variable names and the related value to be used in the gnuplot |
| 63 | code." | 82 | code." |
| 64 | (mapcar | 83 | (let ((*org-babel-gnuplot-missing* (cdr (assoc :missing params)))) |
| 65 | (lambda (pair) | 84 | (mapcar |
| 66 | (cons | 85 | (lambda (pair) |
| 67 | (car pair) ;; variable name | 86 | (cons |
| 68 | (if (listp (cdr pair)) ;; variable value | 87 | (car pair) ;; variable name |
| 69 | (org-babel-gnuplot-table-to-data | 88 | (if (listp (cdr pair)) ;; variable value |
| 70 | (cdr pair) (org-babel-temp-file "gnuplot-") params) | 89 | (org-babel-gnuplot-table-to-data |
| 71 | (cdr pair)))) | 90 | (cdr pair) (org-babel-temp-file "gnuplot-") params) |
| 72 | (mapcar #'cdr (org-babel-get-header params :var)))) | 91 | (cdr pair)))) |
| 92 | (mapcar #'cdr (org-babel-get-header params :var))))) | ||
| 73 | 93 | ||
| 74 | (defun org-babel-expand-body:gnuplot (body params) | 94 | (defun org-babel-expand-body:gnuplot (body params) |
| 75 | "Expand BODY according to PARAMS, return the expanded body." | 95 | "Expand BODY according to PARAMS, return the expanded body." |
| 76 | (save-window-excursion | 96 | (save-window-excursion |
| 77 | (let* ((vars (org-babel-gnuplot-process-vars params)) | 97 | (let* ((vars (org-babel-gnuplot-process-vars params)) |
| 78 | (out-file (cdr (assoc :file params))) | 98 | (out-file (cdr (assoc :file params))) |
| 79 | (term (or (cdr (assoc :term params)) | 99 | (prologue (cdr (assoc :prologue params))) |
| 80 | (when out-file (file-name-extension out-file)))) | 100 | (epilogue (cdr (assoc :epilogue params))) |
| 101 | (term (or (cdr (assoc :term params)) | ||
| 102 | (when out-file | ||
| 103 | (let ((ext (file-name-extension out-file))) | ||
| 104 | (or (cdr (assoc (intern (downcase ext)) | ||
| 105 | *org-babel-gnuplot-terms*)) | ||
| 106 | ext))))) | ||
| 81 | (cmdline (cdr (assoc :cmdline params))) | 107 | (cmdline (cdr (assoc :cmdline params))) |
| 82 | (title (plist-get params :title)) | 108 | (title (cdr (assoc :title params))) |
| 83 | (lines (plist-get params :line)) | 109 | (lines (cdr (assoc :line params))) |
| 84 | (sets (plist-get params :set)) | 110 | (sets (cdr (assoc :set params))) |
| 85 | (x-labels (plist-get params :xlabels)) | 111 | (x-labels (cdr (assoc :xlabels params))) |
| 86 | (y-labels (plist-get params :ylabels)) | 112 | (y-labels (cdr (assoc :ylabels params))) |
| 87 | (timefmt (plist-get params :timefmt)) | 113 | (timefmt (cdr (assoc :timefmt params))) |
| 88 | (time-ind (or (plist-get params :timeind) | 114 | (time-ind (or (cdr (assoc :timeind params)) |
| 89 | (when timefmt 1))) | 115 | (when timefmt 1))) |
| 116 | (missing (cdr (assoc :missing params))) | ||
| 90 | (add-to-body (lambda (text) (setq body (concat text "\n" body)))) | 117 | (add-to-body (lambda (text) (setq body (concat text "\n" body)))) |
| 91 | output) | 118 | output) |
| 92 | ;; append header argument settings to body | 119 | ;; append header argument settings to body |
| 93 | (when title (funcall add-to-body (format "set title '%s'" title))) ;; title | 120 | (when title (funcall add-to-body (format "set title '%s'" title))) |
| 94 | (when lines (mapc (lambda (el) (funcall add-to-body el)) lines)) ;; line | 121 | (when lines (mapc (lambda (el) (funcall add-to-body el)) lines)) |
| 122 | (when missing | ||
| 123 | (funcall add-to-body (format "set datafile missing '%s'" missing))) | ||
| 95 | (when sets | 124 | (when sets |
| 96 | (mapc (lambda (el) (funcall add-to-body (format "set %s" el))) sets)) | 125 | (mapc (lambda (el) (funcall add-to-body (format "set %s" el))) sets)) |
| 97 | (when x-labels | 126 | (when x-labels |
| 98 | (funcall add-to-body | 127 | (funcall add-to-body |
| 99 | (format "set xtics (%s)" | 128 | (format "set xtics (%s)" |
| 100 | (mapconcat (lambda (pair) | 129 | (mapconcat (lambda (pair) |
| 101 | (format "\"%s\" %d" (cdr pair) (car pair))) | 130 | (format "\"%s\" %d" |
| 131 | (cdr pair) (car pair))) | ||
| 102 | x-labels ", ")))) | 132 | x-labels ", ")))) |
| 103 | (when y-labels | 133 | (when y-labels |
| 104 | (funcall add-to-body | 134 | (funcall add-to-body |
| 105 | (format "set ytics (%s)" | 135 | (format "set ytics (%s)" |
| 106 | (mapconcat (lambda (pair) | 136 | (mapconcat (lambda (pair) |
| 107 | (format "\"%s\" %d" (cdr pair) (car pair))) | 137 | (format "\"%s\" %d" |
| 138 | (cdr pair) (car pair))) | ||
| 108 | y-labels ", ")))) | 139 | y-labels ", ")))) |
| 109 | (when time-ind | 140 | (when time-ind |
| 110 | (funcall add-to-body "set xdata time") | 141 | (funcall add-to-body "set xdata time") |
| 111 | (funcall add-to-body (concat "set timefmt \"" | 142 | (funcall add-to-body (concat "set timefmt \"" |
| 112 | (or timefmt | 143 | (or timefmt |
| 113 | "%Y-%m-%d-%H:%M:%S") "\""))) | 144 | "%Y-%m-%d-%H:%M:%S") "\""))) |
| 114 | (when out-file (funcall add-to-body (format "set output \"%s\"" out-file))) | 145 | (when out-file |
| 146 | ;; set the terminal at the top of the block | ||
| 147 | (funcall add-to-body (format "set output \"%s\"" out-file)) | ||
| 148 | ;; and close the terminal at the bottom of the block | ||
| 149 | (setq body (concat body "\nset output\n"))) | ||
| 115 | (when term (funcall add-to-body (format "set term %s" term))) | 150 | (when term (funcall add-to-body (format "set term %s" term))) |
| 116 | ;; insert variables into code body: this should happen last | 151 | ;; insert variables into code body: this should happen last |
| 117 | ;; placing the variables at the *top* of the code in case their | 152 | ;; placing the variables at the *top* of the code in case their |
| 118 | ;; values are used later | 153 | ;; values are used later |
| 119 | (funcall add-to-body (mapconcat #'identity | 154 | (funcall add-to-body |
| 120 | (org-babel-variable-assignments:gnuplot params) | 155 | (mapconcat #'identity |
| 121 | "\n")) | 156 | (org-babel-variable-assignments:gnuplot params) |
| 157 | "\n")) | ||
| 122 | ;; replace any variable names preceded by '$' with the actual | 158 | ;; replace any variable names preceded by '$' with the actual |
| 123 | ;; value of the variable | 159 | ;; value of the variable |
| 124 | (mapc (lambda (pair) | 160 | (mapc (lambda (pair) |
| 125 | (setq body (replace-regexp-in-string | 161 | (setq body (replace-regexp-in-string |
| 126 | (format "\\$%s" (car pair)) (cdr pair) body))) | 162 | (format "\\$%s" (car pair)) (cdr pair) body))) |
| 127 | vars)) | 163 | vars) |
| 164 | (when prologue (funcall add-to-body prologue)) | ||
| 165 | (when epilogue (setq body (concat body "\n" epilogue)))) | ||
| 128 | body)) | 166 | body)) |
| 129 | 167 | ||
| 130 | (defun org-babel-execute:gnuplot (body params) | 168 | (defun org-babel-execute:gnuplot (body params) |
| @@ -201,7 +239,8 @@ then create one. Return the initialized session. The current | |||
| 201 | 239 | ||
| 202 | (defun org-babel-gnuplot-quote-timestamp-field (s) | 240 | (defun org-babel-gnuplot-quote-timestamp-field (s) |
| 203 | "Convert S from timestamp to Unix time and export to gnuplot." | 241 | "Convert S from timestamp to Unix time and export to gnuplot." |
| 204 | (format-time-string org-babel-gnuplot-timestamp-fmt (org-time-string-to-time s))) | 242 | (format-time-string org-babel-gnuplot-timestamp-fmt |
| 243 | (org-time-string-to-time s))) | ||
| 205 | 244 | ||
| 206 | (defvar org-table-number-regexp) | 245 | (defvar org-table-number-regexp) |
| 207 | (defvar org-ts-regexp3) | 246 | (defvar org-ts-regexp3) |
| @@ -212,7 +251,12 @@ then create one. Return the initialized session. The current | |||
| 212 | (if (string-match org-table-number-regexp s) s | 251 | (if (string-match org-table-number-regexp s) s |
| 213 | (if (string-match org-ts-regexp3 s) | 252 | (if (string-match org-ts-regexp3 s) |
| 214 | (org-babel-gnuplot-quote-timestamp-field s) | 253 | (org-babel-gnuplot-quote-timestamp-field s) |
| 215 | (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")))) | 254 | (if (zerop (length s)) |
| 255 | (or *org-babel-gnuplot-missing* s) | ||
| 256 | (if (string-match "[ \"]" "?") | ||
| 257 | (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") | ||
| 258 | "\"") | ||
| 259 | s))))) | ||
| 216 | 260 | ||
| 217 | (defun org-babel-gnuplot-table-to-data (table data-file params) | 261 | (defun org-babel-gnuplot-table-to-data (table data-file params) |
| 218 | "Export TABLE to DATA-FILE in a format readable by gnuplot. | 262 | "Export TABLE to DATA-FILE in a format readable by gnuplot. |