-
Notifications
You must be signed in to change notification settings - Fork 3
/
gnuplot-interface.lisp
351 lines (291 loc) · 10 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
;; Mirko Vukovic
;; Time-stamp: <2014-10-11 12:10:15Eastern Daylight Time 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)
(defparameter *gnuplot* nil
"The gnuplot process")
;; streams returned by the external processes
(defparameter *gnuplot-output* nil "input from the gnuplot stream to lisp")
(defparameter *gnuplot-input* nil "lisp output to the gnuplot stream")
(defparameter *io* nil "gnuplot bidirectional stream")
(defparameter *error* nil "gnuplot error stream") ;; not used
#+skip(defparameter *command-copy* (make-string-output-stream)
"Receives a copy of gnuplot commands")
(defparameter *command* nil "Stream used to send commands")
;;; Variables for multiple windows. Currently not used
(defparameter *windows* nil "a-list of gnuplot streams and names")
(defparameter *windows-history* nil "list of most recently used windows")
;; my streams
(defparameter *native-external-program* t
"Controls compilation of the START-GNUPLOT command
If T, we use SBCL and CLISP native commands for starting the gnuplot
process. If NIL we use the EXTERNAL-PROGRAM package to start the
process.
Currently, we use native commands as I could not figure out a uniform
way of starting gnuplot across platforms. See code documentation in
START-GNUPLOT")
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun executable-path ()
"Return path to the executable"
(let ((candidates
(list
;; Darwin
#+darwin
(probe-file "/opt/local/bin/gnuplot")
;; Pure unix (not cygwin using windows gnuplot)
#+(and unix
(not darwin)
(not wgnuplot))
(or (probe-file "/usr/local/bin/gnuplot")
(probe-file "/usr/bin/gnuplot"))
;; cygwin and windows gnuplot (wgnuplot)
#+wgnuplot ;; implies windows
(user-setup:gnuplot-executable user-setup:*gnuplot-setup*)
)))
(let ((path (case (length candidates)
(0 (error "No paths to GNUPLOT executable - check gnuplot-interface.lisp"))
(1 (car candidates))
(t (error "Found more than one candidate path: ~a
Check gnuplot-interface.lisp" candidates)))))
(assert (stringp path) ()
"Path ~a must be a string or path" path)
(assert (probe-file path) ()
"Executable ~a does exist" path)
path))))
(defparameter *executable* (executable-path)
"Path to the executable")
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun default-terminal ()
(let ((term-candidates
(list
#+darwin nil
#+(and x11 (not cygwin)) 'wxt
;; cygwin+clisp+wgnuplot supports WXT
#+(and cygwin clisp wgnuplot) 'wxt
;; CCL+wgnuplot does not support WXT only WINDOWS
#+(and ccl wgnuplot) (user-setup:gnuplot-terminal user-setup:*gnuplot-setup*)
)))
(let ((term (case (length term-candidates)
(0 (error "No default terminal selected - check gnuplot-interface.lisp"))
(1 (car term-candidates))
(t (error "Found more than one terminal candidate: ~a
Check gnuplot-interface.lisp" term-candidates)))))
(assert term ()
"Terminal is undefined (set to nil) -- edit source file to fix this")
term))))
(defparameter *terminal* (default-terminal)
"Default gnuplot terminal")
(eval-when (:compile-toplevel :load-toplevel :execute)
#+(and (or unix darwin))
(when (string= "" (uiop/os:getenv "DISPLAY"))
#+sbcl (sb-ext:posix-setenv "DISPLAY" "127.0.0.1:0.0")
#+clisp (setf (ext:getenv "DISPLAY") "127.0.0.1:0.0")
#+ccl (ccl:setenv "DISPLAY") "127.0.0.1:0.0")
)
(defun start-gnuplot ()
"Start gnuplot executable and initialize input stream. Also create
the *command* broadcast stream.
"
(assert (uiop/os:getenv "DISPLAY") ()
"DISPLAY environemnt variable not set. Exiting
Check gnuplot-interface.lisp")
#+skip(setf *command-copy* (make-string-output-stream))
;;; I currently use native facilities for starting the gnuplot
;;; process. Note that in SBCL I set :WAIT to NIL and in CLISP I set
;;; :WAIT to T. Otherwise the processes will hang or consume 100% of
;;; CPU.
#+(and native-external-program windows ccl)
(setf *gnuplot*
(ccl:run-program ;;"cmd.exe"
;;(list *executable*)
;;(format nil "\"~a\"" *executable*)
*executable*
#+skip nil
'("-p" "-e" "plot sin(x)")
:wait nil
:input :stream
:output :stream
:error :output)
*gnuplot-input* (ccl:external-process-input-stream *gnuplot*)
*gnuplot-output* (ccl:external-process-output-stream *gnuplot*))
#+(and native-external-program
sbcl)
(setf
*gnuplot*
(sb-ext:run-program
*executable* nil ;;'("-persist" "-e" "plot sin(x)")
:wait nil
:input :stream
:output :stream
:error :output)
*gnuplot-input* (sb-ext:process-input *gnuplot*)
*gnuplot-output* (sb-ext:process-output *gnuplot*))
#+(and native-external-program
(and clisp cygwin))
(multiple-value-setq (*io* *gnuplot-output* *gnuplot-input*)
(ext:run-program *executable*
:arguments nil ;;'("-p" "-e" "plot sin(x)")
:input :stream
:output :stream
:wait t))
;;; The following does not work uniformly across SBCL and CLISP. I
;;; leave it here as documentation to use when I learn what is causing
;;; the issue between SBCL and CLISP.
#+external-program
(setf *gnuplot* (external-program:start *executable* nil ;;'("-p" "-e" "plot sin(x)")
:input :stream
:output :stream)
*gnuplot-input* (external-program:process-input-stream *gnuplot*)
*gnuplot-output* (external-program:process-output-stream *gnuplot*))
(setf *command* *gnuplot-input*)
(values))
(defun stop-gnuplot ()
"Stop gnuplot and close all the streams"
(unwind-protect
(command "quit")
#+ccl
(progn
)
#+sbcl
(progn
;;(close *gnuplot-output*)
(close *gnuplot-input*)
(close *command-copy*)
(close *command*))
#+clisp
(progn
(close *gnuplot-output*)
(close *gnuplot-input*)
;;(close *io*)
(close *command*)
#+skip(close *command-copy*))))
(defun stop ()
"Alias for STOP-GNUPLOT"
(stop-gnuplot))
(defparameter *gnuplot-input-string* ""
"Stores command string sent to gnuplot" )
(defmacro with-captured-gnuplot-input (&body body)
"Execute body while capturing commands sent to *command* into
*gnuplot-input-string*"
`(let* ((copy-stream (make-string-output-stream))
(command-stream (make-broadcast-stream *gnuplot-input*
copy-stream)))
(let ((*command* command-stream))
(unwind-protect
,@body
(setf *gnuplot-input-string* (get-output-stream-string copy-stream))
(close copy-stream)))))
(defun command (&rest command-and-args)
"Pass `command-and-args' to the *command* stream"
#+skip(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-string (string)
"Send a string to gnuplot
Do not send line return
Do not attempt to flush the buffer"
(princ string *command*))
(defun send-line (string &optional continuation)
"Pass a single line to the gnuplot stream
Send a line return
If CONTINUATION is T also append the \ character
Do not attempt to flush the buffer
This command is used to pass complex, multi-line input to gnuplot."
(princ string *command*)
(when continuation (princ #\\ *command*))
(princ "
" *command*))
(defun send-line-break ()
"Send a line break.
Do not attempt to flush the buffer"
(princ (format nil "
") *command*))
(defun flush-buffer ()
"Flush buffer"
(finish-output *command*))
(defun finish-command ()
"Send line-break and flush stream"
(princ "
" *command*)
(finish-output *command*))
(defun send-line-to-gnuplot (string)
(send-line string))
(defun send-line-break-to-gnuplot ()
(send-line-break))
#+skip(defun echo-command ()
"Return the last command sent to "
(get-output-stream-string *command-copy*))
#+skip(defun gnuplot-echo-command ()
(echo-command))
(defun init-gnuplot ()
(command "set terminal ~a" (string-downcase *terminal*) #|(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
*gnuplot-output* nil
*gnuplot-input* 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))
(defun gnuplot-for-windows-p ()
"Return true if running gnuplot for windows"
(search "Program Files" gpi::*executable*))
(defun normalize-namestring (path)
"Normalize namestring to underlying OS
This function is used when running clisp on cygwin and Gnuplot for windows"
#+cygwin
(if (gnuplot-for-windows-p)
(let ((stream
(ext:run-shell-command
(concatenate 'string
"cygpath -m "
(namestring path))
:output :stream)))
(prog1
(read-line stream)
(close stream)))
(namestring path))
#-cygwin
(namestring path))