-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathezd.scm
371 lines (320 loc) · 10.5 KB
/
ezd.scm
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
;;; ezd - easy drawing for X11.
;;;
;;; Batch command loop and Scheme command interpreter.
;* Copyright 1990-1993 Digital Equipment Corporation
;* All Rights Reserved
;*
;* Permission to use, copy, and modify this software and its documentation is
;* hereby granted only under the following terms and conditions. Both the
;* above copyright notice and this permission notice must appear in all copies
;* of the software, derivative works or modified versions, and any portions
;* thereof, and both notices must appear in supporting documentation.
;*
;* Users of this software agree to the terms and conditions set forth herein,
;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
;* right and license under any changes, enhancements or extensions made to the
;* core functions of the software, including but not limited to those affording
;* compatibility with other hardware or software environments, but excluding
;* applications which incorporate this software. Users further agree to use
;* their best efforts to return to Digital any such changes, enhancements or
;* extensions that they make and inform Digital of noteworthy uses of this
;* software. Correspondence should be provided to Digital at:
;*
;* Director of Licensing
;* Western Research Laboratory
;* Digital Equipment Corporation
;* 250 University Avenue
;* Palo Alto, California 94301
;*
;* This software may be distributed (but not offered for sale or transferred
;* for compensation) to third parties, provided such third parties agree to
;* abide by the terms and conditions of this notice.
;*
;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
;* SOFTWARE.
#; (with xlib
struct
match
commands
pps
ginfo
display
window
drawing
view
graphic
rectangle
line
text
arc
quilt
psdraw
events
interact
stringinput
popupmenu
buttons
slider
textree
jtextree
textdrawing
mouseedit
editcolor
transpbuttons)
(module ezd ()
(import
scheme
(chicken base)
(chicken condition)
(chicken file posix)
(rename (only (chicken foreign) define-external location)
(define-external ckn-define-external))
(chicken format)
(chicken locative)
(chicken memory)
(chicken memory representation)
(chicken module)
(chicken platform)
(chicken plist)
(chicken port)
(chicken process signal)
(chicken time posix)
srfi-1
srfi-4
srfi-18
scheme2c-compatibility
xlib)
(import-for-syntax srfi-1 srfi-13)
;; disable buffering so that select works correctly.
(set-buffering-mode! (current-input-port) #:none)
(set-buffering-mode! (current-output-port) #:none)
(export any?)
(define (any? x) #t)
(include "scheme2c.scm")
;; Exports
(include "commands.sch")
(include "display.sch")
(include "window.sch")
(include "view.sch")
(include "drawing.sch")
(include "graphic.sch")
(include "psdraw.sch")
(include "events.sch")
(include "ezd.sch")
(include "interact.sch")
;; Actual code
(include "struct.scm")
(include "match.scm")
(include "commands.scm")
(include "ginfo.scm")
(include "display.scm")
(include "window.scm")
(include "drawing.scm")
(include "view.scm")
(include "graphic.scm")
(include "rectangle.scm")
(include "line.scm")
(include "text.scm")
(include "arc.scm")
(include "quilt.scm")
(include "psdraw.scm")
(include "events.scm")
(include "interact.scm")
(include "stringinput.scm")
(include "popupmenu.scm")
(include "buttons.scm")
(include "slider.scm")
(include "textree.scm")
(include "jtextree.scm")
(include "textdrawing.scm")
(include "mouseedit.scm")
(include "transpbuttons.scm")
;;; Version tag
(define *ezd-version* "2019.12.24")
;;; The program accepts five command line arguments arguments:
;;;
;;; -i interactive Scheme interpreter
;;; -l log commands to ezd.LOG
;;; -nopixmap don't use a pixmap when updating windows
;;; -p treat an end-of-file on stdin as a pause command
;;; -s allow interrupt signal
;;;
;;; If the environment variable EZDLOG is set, then commands are logged to a
;;; file with EZDLOG's value as its name. If the environment flag EZDNOPIXMAP
;;; is set, then pixmaps are not used for window updates.
(define ezd-done #f)
(define ezd-log #f)
(define env-ezdlog (or (getenv "EZDLOG") ""))
(define env-ezdnopixmap (or (getenv "EZDNOPIXMAP") ""))
(define nopixmap (if (eq? env-ezdnopixmap "") #f #t))
(define (reset) #t) ;; TODO should this be ezd-reset?
(define (read-eval-draw clargs)
(define pause (member "-p" clargs))
(set! ezd-log (if (or (member "-l" clargs) (not (equal? env-ezdlog "")))
(let ((port (open-output-file (if (equal? env-ezdlog "")
"ezd.LOG"
env-ezdlog))))
(format port ";;; *EZD-VERSION* ~a~%" *ezd-version*)
port)
#f))
(set! nopixmap (or (member "-nopixmap" clargs) nopixmap))
(if (member "-i" clargs)
(ezd-repl)
(let ((old-reset reset))
(if (and (not (member "-p" clargs)) (not (member "-s" clargs)))
(set-signal-handler! 2 #f))
(set! ezd-done #f)
(set! in-read-eval-draw #t)
(call-with-current-continuation
(lambda (return)
(set! reset (lambda () (return #t)))))
(let loop ((command (or ezd-done (ezd-read))))
(if ezd-log
(if (or ezd-done (eof-object? command))
(close-output-port ezd-log)
(begin (write command ezd-log)
(newline ezd-log))))
(if (or ezd-done (eof-object? command))
(begin (if (and (eof-object? command) pause)
(ezd '(pause)))
(ezd-reset))
(begin (ezd command)
(loop (or ezd-done (ezd-read))))))
(set! reset old-reset)
(set! ezd-done #f)
(set! in-read-eval-draw #f))))
;;; One or more ezd commands are executed by the following function. Any
;;; graphical objects generated are added to the current drawing. This
;;; procedure is the command interface for ezd from Scheme user programs.
(define (ezd . commands)
(for-each
(lambda (c)
(let ((value (ezd-one c)))
(if (isa-graphic? value)
(drawing-add *current-drawing* value))))
commands)
#f)
;;; Execute a single ezd command and return its value. This is not an external
;;; interface.
(define (ezd-one command)
(if (pair? command)
(let* ((x (assoc (car command) ezd-commands))
(template (if x (cadr x)))
(description (if x (caddr x)))
(action (if x (cadddr x))))
(when (and (not *display*)
(not (memq (car command) '(include quit scheme))))
(set! *display* (make-display ""))
(ezd '(set-drawing ezd))
(if (not (memq (car command)
'(window save-drawing set-drawing)))
(ezd '(window ezd 400 400 points "ezd")
'(overlay ezd ezd))))
(if x
(let ((args (arg-parse template command)))
(if (eq? args #f)
(ezd-error 'ezd
"Illegal command: ~s~% expected - ~a"
command description)
(apply action args)))
(ezd-error 'ezd "Unrecognized command: ~s" command)))
(ezd-error 'ezd "Command is not a list: ~s" command)))
;;; The following procedure resets the entire drawing system. All modules
;;; needing initialization in order to be rerun must define a reset procedure
;;; and have it called from here.
(define (ezd-reset)
(ezd-module-init)
(commands-module-init)
(display-module-init)
(window-module-init)
(view-module-init)
(drawing-module-init)
(graphic-module-init)
(events-module-init))
;;; The SCHEME command allows Scheme expressions to be evaluated within ezd.
(define-ezd-command
`(scheme (repeat ,any?))
"(scheme <scheme expression> ...)"
(lambda args (for-each eval (car args))))
;;; The INCLUDE command loads ezd commands or Scheme expressions (files with
;;; a suffix of .sc) from a file.
(define (ezd-include file)
(if (and (>= (string-length file) 3)
(equal? (substring file (- (string-length file) 3)
(string-length file))
".sc"))
(load file)
(with-input-from-file
file
(lambda ()
(let loop ((exp (ezd-read)))
(unless (eof-object? exp)
(ezd exp)
(loop (ezd-read))))))))
(define-ezd-command
`(include ,string?)
"(include \"file-name\")"
ezd-include)
;;; The QUIT command terminates ezd processing.
(define-ezd-command
`(quit)
"(quit)"
(lambda ()
(let ((handling-events (and (isa-display? *display*)
(display-handling-events *display*)))
(command-stream in-read-eval-draw))
(ezd-reset)
(if command-stream (set! ezd-done #t))
(if handling-events
(exit)))))
;;; Command stepping. Each step identifies itself by a STEP command with some
;;; expression. The expression is assigned to *STEP* and then the value of
;;; *STEPPER* is evaluated. If it is true, then the program pauses until some
;;; event calls NEXT-STEP or *STEPPER* is set to #f.
(define *step* #f) ;;; Value associated with this step.
(define *stepper* #f) ;;; Expression to evaluate at each step to decide
;;; whether or not to continue.
(define *next-step* #f) ;;; Stepper advances when this is true.
;;; Define the stepper.
(define (stepper exp)
(set! *stepper* exp))
(define-ezd-command
`(stepper ,any?)
"(stepper expression)"
stepper)
;;; Test to see if the stepper requires a stop at this step.
(define (step exp)
(set! *step* exp)
(set! *next-step* #f)
(when (eval *stepper*)
(display-event-handler *display*)
(let loop ()
(yselect *dpy* 1000000 0)
(display-event-handler *display*)
(if (and *stepper* (not *next-step*)) (loop)))))
(define-ezd-command
`(step ,any?)
"(step expression)"
step)
;;; Call this from an event handler to allow further processing.
(define (next-step)
(set! *next-step* #t))
;;; The PAUSE command waits for some number of milliseconds, or until ezd
;;; completes, before returning.
(define-ezd-command
`(pause (optional ,positive-number?))
"(pause [ millisecond pause time ])"
(lambda (ms-pause)
(if ms-pause
(thread-sleep! (/ ms-pause 1000))
(wait-system-file #f))))
;;; Module reset/initialization procedure.
(define (ezd-module-init)
(set! ezd-done #f))
)