forked from mirkov/gnuplot-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnuplot-interface.lisp
193 lines (158 loc) · 5.39 KB
/
gnuplot-interface.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
;; Mirko Vukovic
;; Time-stamp: <2012-02-26 10:15:17 gnuplot-interface.lisp>
;;
;; Copyright 2011 Mirko Vukovic
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(in-package #:gnuplot-interface)
(defvar *gnuplot* "Value returned by the command that invokes
gnuplot")
;; streams returned by the external processes
(defvar *input* nil "input from the gnuplot stream to lisp")
(defvar *output* nil "lisp output to the gnuplot stream")
(defvar *io* nil "gnuplot bidirectional stream")
(defvar *error* nil "gnuplot error stream") ;; not used
(defvar *command-copy* (make-string-output-stream)
"Receives a copy of gnuplot commands")
(defvar *command* nil "Stream used to send commands")
;;; Variables for multiple windows. Currently not used
(defvar *windows* nil "a-list of gnuplot streams and names")
(defvar *windows-history* nil "list of most recently used windows")
;; my streams
(defparameter *executable* #+sbcl "/usr/local/bin/gnuplot"
#+(and clisp (not wgnuplot)) "gnuplot"
#+(and clisp wgnuplot)
(getf (symbol-plist :wgnuplot) :executable)
"Path to the executable")
(defparameter *terminal*
#+(and cygwin (not wgnuplot)) 'x11
#+(and x11 (not cygwin)) 'wxt
#+(and clisp wgnuplot) 'wxt
"Default gnuplot terminal")
(eval-when (:compile-toplevel :load-toplevel :execute)
#+(and cygwin (not wgnuplot)) (when (string= "" (sys::getenv "DISPLAY"))
(sys::setenv "DISPLAY" "127.0.0.1:0.0"))
#+sbcl (unless (sb-ext:posix-getenv "DISPLAY")
(warn "DISPLAY is not set. X11 terminal will not be operational" ))
)
(defun start-gnuplot ()
"Start gnuplot executable and initialize input stream. Also create
the *command* broadcast stream
Return multiple values the *gnuplot* executable and the *command*
stream"
(setf *command-copy* (make-string-output-stream))
#+sbcl
(setf *gnuplot* (sb-ext:run-program *executable* nil
:wait nil
:input :stream
:output t
:error :output)
*output* (sb-ext:process-input *gnuplot*)
*input* (sb-ext:process-output *gnuplot*))
#+(or (and clisp linux)
(and clisp cygwin))
(multiple-value-setq (*io* *input* *output*)
(ext:make-pipe-io-stream *executable*
:buffered t))
;; ext:run-program returns an error that the executable could not be
;; found:
;; /bin/sh: line 0: exec: /c/Program\ Files/wgnuplot/binary/gnuplot.exe:
;; cannot execute: No such file or directory
#|(multiple-value-setq (*io* *input* *output*)
(ext:run-program *executable*
:input :stream
:output :stream
:wait nil))|#
(setf *command*
(make-broadcast-stream *output* *command-copy*))
(values))
(defun stop-gnuplot ()
"Stop gnuplot and close all the streams"
(command "quit")
#+sbcl
(progn
(close *input*)
(close *output*)
(close *command-copy*)
(close *command*))
#+clisp
(progn
(close *input*)
(close *output*)
(close *io*)
(close *command*)
(close *command-copy*)))
(defun command (&rest command-and-args)
"Pass `command-and-args' to the *command* stream"
#| (get-output-stream-string *command-copy*)|#
(when command-and-args
(apply #'format *command* command-and-args)
(format *command* "~%")
(finish-output *command*)))
(defun gnuplot-command (&rest args)
"Alias for COMMAND"
(apply #'command args))
(defun send-line (string)
"Pass a single line to the gnuplot stream
This command is used to pass complex, multi-line input to gnuplot.
For it to take effect, the sequence of SEND-LINE commands must be
finished by the SEND-LINE-BREAK command."
(princ string *command*)
(finish-output *command*))
(defun send-line-break ()
"Send a line break"
(princ (format nil "~%") *command*)
(finish-output *command*))
(defun send-line-to-gnuplot (string)
(send-line string))
(defun send-line-break-to-gnuplot ()
(send-line-break))
(defun echo-command ()
"Return the last command sent to "
(get-output-stream-string *command-copy*))
(defun gnuplot-echo-command ()
(echo-command))
(defun init-gnuplot ()
(command "set terminal ~a" (alexandria:symbolicate *terminal*)))
(defun hello-world ()
"Throw test plot"
(command "plot cos(x)"))
(defun gnuplot-hello-world ()
(hello-world))
(defun clean-process-info ()
"Set all special vars to nil"
(setf *gnuplot* nil
*input* nil
*output* nil
*io* nil
*windows* nil
*windows-history* nil
*command-copy* nil
*command* nil ))
(defun test (&key (terminal t) (palette :rgb))
"Invoke gnuplot `test' command"
(when terminal
(command "test terminal")
(return-from test))
(when palette
(command (format nil "test palette ~a" palette))
(return-from test))
(command "test"))
(defun gnuplot-test (&rest keywords &key &allow-other-keys)
(apply #'test :allow-other-keys t keywords))
(defun reset ()
(command "reset"))
(defun gnuplot-reset ()
(reset))