-
Notifications
You must be signed in to change notification settings - Fork 7
/
repl.scm
1593 lines (1408 loc) · 55.4 KB
/
repl.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; a repl
;;;
;;; (load "repl.scm") ((*repl* 'run))
(provide 'repl.scm)
(require libc.scm)
(unless (defined? '*repl*)
(define *repl* ; environment that holds the REPL functions
(let ((prompt #f) ; function to get/set prompt
(keymap #f) ; function to get/set keymap entries
(history #f) ; function to get/set history buffer entries
(history-size #f) ; function to get/set history buffer size
(save-history #f) ; function to save the current history buffer entries in a file
(restore-history #f) ; function to restore history buffer entries from a file
(helpers ()) ; list of functions displaying help strings
(run #f) ; function that fires up a REPL
(top-level-let (sublet (rootlet) :** #f)) ; environment in which evaluation takes place
(repl-let ; environment for keymap functions to access all the REPL innards (cursor-position etc)
(with-let (sublet *libc*)
;; -------- completion --------
(define (symbol-completion text)
(let ((st (symbol-table))
(text-len (length text))
(match #f))
(call-with-exit
(lambda (return)
(for-each
(lambda (symbol)
(let* ((sym (symbol->string symbol))
(sym-len (length sym)))
(when (and (>= sym-len text-len)
(string=? text (substring sym 0 text-len)))
(if match
(return text)
(set! match sym)))))
st)
(or match text)))))
(define (filename-completion text)
(let ((g (glob.make)))
(glob (string-append text "*")
(logior (if (and (defined? 'GLOB_TILDE)
(char=? (text 0) #\~))
GLOB_TILDE
0)
GLOB_MARK)
g)
(let ((files (map (lambda (f) ; get rid of emacs' *~ files
(if (and (> (length f) 1)
(char=? #\~ (f (- (length f) 1))))
(values)
f))
(glob.gl_pathv g))))
(globfree g)
(if (or (null? files)
(not (null? (cdr files))))
text
(car files)))))
;; -------- history --------
(let ((histbuf (make-vector 100 ""))
(histsize 100)
(histpos 0)
(m-p-pos 0)
(histtop ()))
(define push-line
(let ((histtail ()))
(lambda (line)
(if (null? histtop)
(begin
(set! histtop (list (copy line)))
(set! histtail histtop))
(begin
(set-cdr! histtail (list line))
(set! histtail (cdr histtail)))))))
(define (history-member line)
(do ((i 0 (+ i 1)))
((or (= i histsize)
(string=? (vector-ref histbuf i) line))
(and (< i histsize) i))))
(define history-size (dilambda
(lambda ()
histsize)
(lambda (new-size)
(unless (= new-size histsize)
(if (<= new-size 0)
(error 'out-of-range "new history buffer size must be positive")
(let ((new-hist (make-vector new-size ""))
(new-end (min (- new-size 1) histpos)))
(let loop ((oldpos histpos)
(newpos new-end))
(set! (new-hist newpos) (histbuf oldpos))
(set! newpos (- (if (zero? newpos) new-size newpos) 1))
(unless (= newpos new-end)
(set! oldpos (- (if (zero? oldpos) histsize oldpos) 1))
(unless (= oldpos histpos)
(loop oldpos newpos))))
(set! histsize new-size)
(set! histpos new-end)
(set! histbuf new-hist)
new-size))))))
(define history (dilambda
(lambda (back)
(let ((i (+ histpos back)))
(copy (histbuf (if (< i 0)
(+ histsize i)
(if (>= i histsize)
(- i histsize)
i))))))
(lambda (new-line)
(let ((pos (history-member new-line)))
(when (integer? pos) ; remove the earlier case, circularly compress the buffer
(when (>= pos histpos)
(do ((i pos (+ i 1)))
((>= i (- histsize 1)))
(set! (histbuf i) (histbuf (+ i 1))))
(set! (histbuf (- histsize 1)) (histbuf 0))
(set! pos 0))
(do ((i pos (+ i 1)))
((>= i (- histpos 1)))
(set! (histbuf i) (histbuf (+ i 1))))
(set! histpos (- histpos 1))))
(set! (histbuf histpos) (copy new-line))
(set! histpos (+ histpos 1))
(if (= histpos histsize)
(set! histpos 0)))))
(define (history-help)
(set! (*repl* 'helpers)
(list
(lambda (c)
(format #f "size: ~A, pos: ~A" histsize histpos))
(lambda (c)
(format #f "buf: ~A ~A ~A" (history -1) (history -2) (history -3)))
(lambda (c)
(format #f "line: ~A" histtop)))))
(define (pop-history) ; throw away most recent addition
(set! histpos (- histpos 1))
(if (negative? histpos)
(set! histpos (- histsize 1))))
(define* (write-history port (spaces 0))
(format port "~NC(set! histpos ~D)~%" spaces #\space histpos)
(format port "~NC(set! histsize ~D)~%" spaces #\space histsize)
(let-temporarily (((*s7* 'print-length) (* 2 histsize)))
(format port "~NC(set! histbuf ~A)" spaces #\space histbuf)))
(define* (save-history (file "repl-history.scm"))
(call-with-output-file file
write-history))
(define* (restore-history (file "repl-history.scm"))
(load file (curlet)))
(let ((prompt-string "<1> ") ; this doesn't look very good, but I can't find anything better
(prompt-length 4) ; perhaps the line number could go in the terminal's right margin?
(cur-line "") ; current expression (can contain newlines)
(prev-line ()) ; for undo via C-_
(selection #f) ; for C-y
(cursor-pos 0) ; cursor-pos is the logical position (index into cur-line)
(cur-row 0) ; catch window scrolling
(red-par-pos #f) ; paren match position (index into cur-line)
(prompt-row 0) ; need to tie everything to prompt position (it can move in media res if window scrolls)
(prompt-col 0)
(last-row 0) ; these hold the window bounds when we start
(last-col 0)
(input-fd (fileno stdin)) ; source of chars, either tty input or file input
(terminal-fd (fileno stdin))
(tab-as-space (make-string 6 #\space))
(next-char #f)
(chars 0) ; (sigh) a kludge to try to distinguish tab-as-space from tab-as-completion/indentation
(unbound-case #f)
(all-done #f)) ; if #t, repl returns to its caller, if any
;; -------- evaluation ---------
(define (badexpr h) ; *missing-close-paren-hook* function for Enter command
(let ((ow (owlet)))
(if (and (ow 'error-file)
(not (equal? (ow 'error-file) "repl.scm")))
(error 'syntax-error "missing close paren in ~S" (ow 'error-file))
(set! (h 'result) 'string-read-error))))
(define (shell? h) ; *unbound-variable-hook* function, also for Enter
;; examine cur-line -- only call system if the unbound variable matches the first non-whitespace chars
;; of cur-line, and command -v name returns 0 (indicating the shell thinks it is an executable command)
(do ((i 0 (+ i 1)))
((or (= i (length cur-line))
(not (char-whitespace? (cur-line i))))
(let ((var-name (symbol->string (h 'variable))))
(when (and (>= (- (length cur-line) i) (length var-name)) ; var-name might be unrelated to cur-line
(string=? var-name (substring cur-line i (+ i (length var-name))))
(zero? (system (string-append "command -v " var-name " >/dev/null"))))
(set! unbound-case #t)
(if (procedure? ((rootlet) 'system))
(begin
(set! ((*repl* 'top-level-let) '**) (((rootlet) 'system) cur-line #t))
(display ((*repl* 'top-level-let) '**) *stderr*))
(set! ((*repl* 'top-level-let) '**) (system cur-line)))
(set! (h 'result) (symbol " ")))))))
(define (with-repl-let body)
;; for multiline edits, we will use *missing-close-paren-hook* rather than try to parse the input ourselves.
(let ((old-badexpr-hook (hook-functions *missing-close-paren-hook*))
(old-unbound-var-hook (hook-functions *unbound-variable-hook*))
(old-eval #f)
(old-eval-string #f)
(old-load #f))
;; if the repl's top-level-let is not rootlet, and we load some file into rootlet
;; that (for example) defines a function at its top level, that function's definition
;; env is rootlet. If we then define something in the repl, it is in the
;; repl's top level. If we now call the function asking is the new thing defined,
;; it looks in rootlet, not the repl top-level, and says no.
;; so, locally redefine these three to use the repl top-level while we're in the repl.
;; in addition, for each of these, we need to report missing close parens and so on
(let ((repl-hooks
(lambda ()
(set! (hook-functions *missing-close-paren-hook*) (cons badexpr old-badexpr-hook))
(set! (hook-functions *unbound-variable-hook*) (cons shell? old-unbound-var-hook))))
(original-hooks
(lambda ()
(set! unbound-case #f)
(set! (hook-functions *missing-close-paren-hook*) old-badexpr-hook)
(set! (hook-functions *unbound-variable-hook*) old-unbound-var-hook))))
(let ((new-load
(let ((documentation "this is the repl's load replacement; its default is to use the repl's top-level-let.")
(signature '(values string? let?)))
(lambda* (file (e (*repl* 'top-level-let)))
(dynamic-wind original-hooks (lambda () (load file e)) repl-hooks))))
(new-eval
(let ((documentation "this is the repl's eval replacement; its default is to use the repl's top-level-let.")
(signature '(values list? let?)))
(lambda* (form (e (*repl* 'top-level-let)))
(dynamic-wind original-hooks (lambda () (eval form e)) repl-hooks))))
(new-eval-string
(let ((documentation "this is the repl's eval-string replacement; its default is to use the repl's top-level-let.")
(signature '(values string? let?)))
(lambda* (str (e (*repl* 'top-level-let)))
(dynamic-wind original-hooks (lambda () (eval-string str e)) repl-hooks)))))
(dynamic-wind
(lambda ()
(repl-hooks)
(set! eval new-eval)
(set! eval-string new-eval-string)
(set! load new-load))
body
(lambda ()
(set! eval old-eval)
(set! eval-string old-eval-string)
(set! load old-load)
(original-hooks)))))))
;; -------- match parens --------
(define check-parens
(let ((char-constant?
(lambda (pos)
(and (> pos 2)
(char=? (cur-line (- pos 1)) #\\)
(char=? (cur-line (- pos 2)) #\#)))))
(lambda ()
(let ((endpos (- cursor-pos 1)))
(if (or (<= cursor-pos 1)
(not (char=? (cur-line endpos) #\))) ; ")" on left of cursor
(char-constant? endpos)) ; it's not "#\)"
(if (number? red-par-pos)
(set! red-par-pos #f))
(let ((oparens ())
(new-red-pos #f))
(do ((i 0 (+ i 1)))
((>= i endpos))
(case (cur-line i)
((#\()
(set! oparens (cons i oparens)))
((#\))
(if (pair? oparens)
(set! oparens (cdr oparens))))
((#\;)
(do ((k (+ i 1) (+ k 1)))
((or (>= k endpos)
(char=? (cur-line k) #\newline))
(set! i k)
(if (>= i endpos) ; (f1 "(+ 1 3) should not show first paren as a match (similarly below)
(set! oparens ())))))
((#\")
(do ((k (+ i 1) (+ k 1)))
((or (>= k endpos)
(and (char=? (cur-line k) #\")
(not (char=? (cur-line (- k 1)) #\\))))
(set! i k)
(if (>= i endpos)
(set! oparens ())))))
((#\#)
(if (char=? (cur-line (+ i 1)) #\|)
(do ((k (+ i 1) (+ k 1)))
((or (>= k endpos)
(and (char=? (cur-line k) #\|)
(char=? (cur-line (+ k 1)) #\#)))
(set! i (+ k 1))
(if (>= i endpos)
(set! oparens ()))))))))
(if (pair? oparens)
(set! new-red-pos (car oparens)))
(unless (equal? new-red-pos red-par-pos)
(set! red-par-pos (and (number? new-red-pos) new-red-pos)))))))))
;; -------- indentation --------
(define (indent pos)
(let ((old-red red-par-pos)
(old-line (copy cur-line))
(old-cursor cursor-pos))
(set! cur-line (string-append (substring cur-line 0 cursor-pos) ")"))
(set! cursor-pos (length cur-line))
(check-parens)
(set! cur-line old-line)
(set! cursor-pos old-cursor)
(when red-par-pos
(let ((new-red red-par-pos))
(set! red-par-pos old-red)
(let ((col 0))
(do ((i 0 (+ i 1)))
((= i new-red))
(set! col (if (char=? (cur-line i) #\newline) 0 (+ col 1))))
(let ((spaces (let ((sym (do ((i (+ new-red 1) (+ i 1)))
((not (char-alphabetic? (cur-line i)))
(substring cur-line (+ new-red 1) i)))))
(+ col (if (member sym '("or" "and" "cond" "if"))
(+ (length sym) 2)
2)))))
(if (= cursor-pos (length cur-line))
(begin
(set! cur-line (format #f "~A~NC" cur-line spaces #\space))
(set! cursor-pos (length cur-line)))
(begin
(set! cur-line (format #f "~A~NC~A"
(substring cur-line 0 cursor-pos)
spaces #\space
(substring cur-line (+ cursor-pos 1))))
(set! cursor-pos (+ cursor-pos spaces))))))))))
;; -------- prompt --------
(define (original-prompt num)
(set! prompt-string (format #f "<~D> " num))
(set! prompt-length (length prompt-string)))
;; -------- vt100 --------
(define (red text) (format #f "~C[31m~A~C[0m" #\escape text #\escape)) ; black=30, green=32, yellow=33, blue=34
(define* (rgb text (r 0) (g 0) (b 0) all-colors)
(format #f (if all-colors
(values "~C[38;5;~Dm~A~C[0m" #\escape (+ 16 (* 36 (round (* r 5))) (* 6 (round (* g 5))) (round (* b 5))))
(values "~C[~Dm~A~C[0m" #\escape (+ 30 (ash (round b) 2) (ash (round g) 1) (round r))))
text #\escape))
(define (move-cursor y x)
(format *stderr* "~C[~D;~DH" #\escape y x))
(define (cursor-coords)
(let* ((c (string #\null #\null))
(cc (string->c-pointer c))
(terminal-fd (fileno stdin)))
(format *stderr* "~C[6n" #\escape)
(do ((b (read terminal-fd cc 1) (read terminal-fd cc 1)))
((char=? (c 0) #\escape)))
(read terminal-fd cc 1)
(and (char=? (c 0) #\[)
(let ((y 0)
(x 0))
(do ((b (read terminal-fd cc 1) (read terminal-fd cc 1)))
((not (char-numeric? (c 0))))
(set! y (- (+ (* 10 y) (char->integer (c 0))) (char->integer #\0))))
(and (char=? (c 0) #\;)
(do ((b (read terminal-fd cc 1) (read terminal-fd cc 1)))
((not (char-numeric? (c 0)))
(and (char=? (c 0) #\R)
(cons x y)))
(set! x (- (+ (* 10 x) (char->integer (c 0))) (char->integer #\0)))))))))
(define (cursor-bounds)
(let ((coords (cursor-coords)))
(set! prompt-col (car coords))
(set! prompt-row (cdr coords))
(move-cursor 4000 4000)
(let ((bounds (cursor-coords)))
(move-cursor (cdr coords) (car coords))
(set! last-col (car bounds))
(set! last-row (cdr bounds)))))
;; to enable mouse click coords (in xterm anyway), (format *stderr* "~C[?9h" #\escape)
;; disable: (format *stderr* "~C[?9l" #\escape)
;; while enabled, mouse selection instead sends coords to repl (so it's annoying)
;; also it's sticky! exit repl does not clear this flag so mouse is effectively dead
;; upon click, we get ESC [ M bxy -- need to poll for this?
;; -------- display --------
(define (display-prompt)
(format *stderr* "~A" prompt-string))
(define (new-prompt)
(set! cur-line "")
(set! cursor-pos 0)
(set! cur-row 0)
(set! prev-line ())
((*repl* 'prompt) (+ (length histtop) 1))
(display-prompt)
(cursor-bounds))
(define display-line
(let ((bold (lambda (text)
(format #f "~C[1m~A~C[0m" #\escape text #\escape))))
(lambda (start end)
;; if a line wraps, it will confuse the redisplay/cursor positioning code. so truncate the display
(let ((line-len (- (+ end prompt-length 1) start)))
(if (>= line-len last-col)
(set! end (- (+ start last-col) prompt-length 1))))
(if (and (integer? red-par-pos)
(<= start red-par-pos)
(< red-par-pos end))
(string-append
(format #f (if (zero? start)
(values "~A" prompt-string)
(values "~NC" prompt-length #\space)))
(format #f (if (= start red-par-pos)
(values "~A~A"
(bold (red "("))
(substring cur-line (+ start 1) end))
(values "~A~A~A"
(substring cur-line start red-par-pos)
(bold (red "("))
(substring cur-line (+ red-par-pos 1) end)))))
(format #f (if (zero? start)
(values "~A~A" prompt-string (substring cur-line 0 end))
(values "~NC~A" prompt-length #\space (substring cur-line start end))))))))
(define (display-cursor)
(do ((row 0)
(start 0)
(len (length cur-line))
(i 0 (+ i 1)))
((or (= i len)
(= i cursor-pos))
(move-cursor (+ prompt-row row) (- (+ prompt-col cursor-pos) start)))
(when (char=? (cur-line i) #\newline)
(set! row (+ row 1))
(set! start (+ i 1)))))
(define (display-lines)
(move-cursor prompt-row 0)
(format *stderr* "~C[J" #\escape)
(let ((len (length cur-line))
(new-line ""))
(do ((line-end 0)
(i 0 (+ line-end 2)))
((> i len))
(set! line-end (end-of-line i))
(set! new-line (string-append new-line (display-line i (min (+ line-end 2) len)))))
(format *stderr* "~A" new-line)
(display-cursor)))
;; -------- help/debugging --------
(define (one-line text)
(if (not (string? text))
text
(let ((ntext (copy text)))
(do ((i 0 (+ i 1)))
((= i (length ntext))
ntext)
(if (char=? (ntext i) #\newline) (set! (ntext i) #\|))))))
(define (help c)
(when (pair? (*repl* 'helpers))
(let ((coords (cursor-coords))
(col (floor (/ last-col 2))))
(move-cursor 1 col)
(format *stderr* "+~NC" (- col 2) #\-)
(do ((i 2 (+ i 1)) ; put box in top right corner so we don't get trailing output as we scroll
(lst (*repl* 'helpers) (cdr lst)))
((null? lst))
(let ((str ((car lst) c)))
(move-cursor i col)
(format *stderr* "~C[K| ~A" #\escape (if (> (length str) col) (substring str 0 (- col 1)) str))))
(move-cursor (+ 2 (length (*repl* 'helpers))) col)
(format *stderr* "+~NC" (- col 2) #\-)
(move-cursor (cdr coords) (car coords)))))
(define (debug-help)
(set! (*repl* 'helpers)
(list
(lambda (c)
(let ((cur-char (if (zero? (length cur-line))
#\space
(let ((c (cur-line (max 0 (min cursor-pos (- (length cur-line) 1))))))
(if (char=? c #\newline) #\| c)))))
(format #f "cursor: ~A, ~C, line: ~S"
cursor-pos cur-char
(one-line cur-line))))
(lambda (c)
(format #f "len: ~D, selection: ~S, previous: ~S"
(length cur-line)
(one-line selection)
(if (pair? prev-line) (one-line (cdar prev-line)) ())))
(lambda (c)
(format #f "cur-row: ~A, prompt-row: ~A"
cur-row
prompt-row))
(lambda (c)
(format #f "c: ~S ~D, start: ~A, end: ~A"
c
(char->integer c)
(start-of-line cursor-pos)
(end-of-line cursor-pos))))))
;; -------- keymap(s) --------
(define meta-keymap-functions (make-vector 256))
(define keymap-functions (make-vector 256 #f))
(define keymap (dilambda
(lambda (c)
(cond ((char? c)
(keymap-functions (char->integer c)))
((integer? c)
(keymap-functions c))
((not (string? c))
(error 'wrong-type-arg "keymap takes a character or string argument"))
((= (length c) 1)
(keymap-functions (char->integer (c 0))))
((and (= (length c) 2)
(char=? (c 0) #\escape))
(meta-keymap-functions (char->integer (c 1))))
(else (lambda (c) #t))))
(lambda (c f)
(cond ((char? c)
(set! (keymap-functions (char->integer c)) f))
((integer? c)
(set! (keymap-functions c) f))
((not (string? c))
(error 'wrong-type-arg "set! keymap takes a character or string first argument"))
((= (length c) 1)
(set! (keymap-functions (char->integer (c 0))) f))
((and (= (length c) 2)
(char=? (c 0) #\escape))
(set! (meta-keymap-functions (char->integer (c 1))) f))))))
(define C-a 1) ; #\x01 etc
(define C-b 2)
(define C-d 4)
(define C-e 5)
(define C-f 6)
(define C-h 8)
(define C-k 11)
(define C-l 12)
;(define C-m 13) ; #\return -- Enter handles this case (crlf?)
(define C-n 14)
(define C-o 15)
(define C-p 16)
;(define C-r 18)
(define C-t 20)
(define C-y 25)
(define C-_ 31)
(define Tab 9)
(define Enter 10) ; #\linefeed
(define Backspace 127)
(define Escape 27) ; #\escape
(define (end-of-line pos) ; prompt or #\newline mark the line boundary
(let ((len (length cur-line)))
(do ((i (max 0 (min pos len)) (+ i 1)))
((or (>= i len)
(char=? (cur-line i) #\newline))
(if (>= i len) len (max 0 (- i 1)))))))
(define (start-of-line pos)
(if (<= pos 0)
0
(do ((i (min pos (- (length cur-line) 1)) (- i 1)))
((or (zero? i)
(char=? (cur-line i) #\newline))
(if (zero? i) 0 (+ i 1))))))
(define (append-newline)
(set! cur-line (string-append cur-line (string #\space #\newline)))
(set! cursor-pos (length cur-line))
(when (= last-row (+ prompt-row cur-row))
(format *stderr* "~%")
(set! prompt-row (- prompt-row 1)))
(set! cur-row (+ cur-row 1)))
(define (word-break pos) ; assume we're at the start of a word
(do ((len (length cur-line))
(i pos (+ i 1)))
((or (>= i len)
(not (or (char-alphabetic? (cur-line i))
(char-numeric? (cur-line i)))))
i)))
(define (save-line)
(set! prev-line (cons (cons cursor-pos (copy cur-line)) prev-line)))
(let ((main-keyfunc (lambda (c)
(if (<= chars 1) (save-line))
(set! cur-line (if (= cursor-pos (length cur-line))
(string-append cur-line (string c))
(if (= cursor-pos 0)
(string-append (string c) cur-line)
(string-append
(substring cur-line 0 cursor-pos)
(string c)
(substring cur-line cursor-pos)))))
(set! cursor-pos (+ cursor-pos 1))
(set! m-p-pos 0)))
(no-op-keyfunc (lambda (c) #t)))
(do ((i 0 (+ i 1)))
((= i 32))
(set! (keymap-functions i) no-op-keyfunc))
(do ((i 32 (+ i 1)))
((= i 256))
(set! (keymap-functions i) main-keyfunc))
(do ((i 0 (+ i 1)))
((= i 256))
(set! (meta-keymap-functions i) no-op-keyfunc)))
;; -------- cursor movement
(set! (keymap-functions C-a)
(lambda (c)
(set! cursor-pos (start-of-line cursor-pos))
'just-cursor))
(set! (keymap-functions C-e)
(lambda (c)
(set! cursor-pos (end-of-line cursor-pos))
'just-cursor))
(set! (keymap-functions C-b)
(lambda (c)
(when (> cursor-pos 0)
(set! cursor-pos (- cursor-pos 1))
(if (char=? (cur-line cursor-pos) #\newline)
(set! cursor-pos (- cursor-pos 1))))
'just-cursor))
(set! (keymap-functions C-f)
(lambda (c)
(let ((len (length cur-line)))
(when (< cursor-pos len)
(set! cursor-pos (+ cursor-pos 1))
(if (and (< cursor-pos len)
(char=? (cur-line cursor-pos) #\newline))
(set! cursor-pos (+ cursor-pos 1)))))
'just-cursor))
(set! (keymap-functions C-p)
(lambda (c)
;; try to find corresponding column in previous line
(let ((start (start-of-line cursor-pos)))
(when (positive? start)
(let ((upstart (start-of-line (- start 2)))
(upend (end-of-line (- start 2)))
(line-pos (- cursor-pos start)))
(set! cursor-pos (min (+ upstart line-pos) upend)))))
'just-cursor))
(set! (keymap-functions C-n)
(lambda (c)
;; try to find corresponding column in next line
(let ((start (start-of-line cursor-pos))
(len (length cur-line))
(next-start (+ (end-of-line cursor-pos) 1))) ; should be at #\newline (if any)
(if (> len next-start) ; not already at last line
(let ((next-end (end-of-line (+ next-start 1)))
(line-pos (- cursor-pos start)))
(set! cursor-pos (min (+ next-start 1 line-pos) next-end)))))
'just-cursor))
(set! (keymap-functions C-l)
(lambda (c)
(format *stderr* "~C[H~C[J" #\escape #\escape)
(new-prompt)))
;; -------- deletion
(set! (keymap-functions C-d)
(lambda (c)
(let ((len (length cur-line)))
(when (< cursor-pos len)
(save-line)
(do ((i cursor-pos (+ i 1)))
((>= i (- len 1)))
(set! (cur-line i) (cur-line (+ i 1))))
(set! cur-line (substring cur-line 0 (- len 1)))))))
(set! (keymap-functions C-h)
(lambda (c)
(when (positive? cursor-pos)
(save-line)
(let ((len (length cur-line)))
(set! cursor-pos (- cursor-pos 1))
(do ((i cursor-pos (+ i 1)))
((>= i (- len 1)))
(set! (cur-line i) (cur-line (+ i 1))))
(set! cur-line (substring cur-line 0 (- len 1)))))))
(set! (keymap-functions Backspace)
(keymap-functions C-h))
(set! (keymap-functions C-k)
(lambda (c)
(let ((len (length cur-line)))
(when (< cursor-pos len)
(save-line)
(let ((end (end-of-line cursor-pos)))
(if (= end len)
(begin
(set! selection (substring cur-line cursor-pos))
(set! cur-line (substring cur-line 0 cursor-pos)))
(if (or (= cursor-pos end) ; delete following newline
(char=? (cur-line cursor-pos) #\newline))
(set! cur-line (string-append (substring cur-line 0 cursor-pos)
(substring cur-line (+ cursor-pos 1))))
(begin
(set! selection (substring cur-line cursor-pos (+ end 1)))
(set! cur-line (string-append (substring cur-line 0 cursor-pos)
(substring cur-line (+ end 1))))
))))))))
;; -------- undo/selection
(set! (keymap-functions C-y)
(lambda (c)
(when selection
(save-line)
(set! cur-line (if (zero? cursor-pos)
(string-append selection cur-line)
(if (>= cursor-pos (length cur-line))
(string-append cur-line selection)
(string-append (substring cur-line 0 cursor-pos)
selection
(substring cur-line cursor-pos)))))
(set! cursor-pos (+ cursor-pos (length selection))))))
(set! (keymap-functions C-_)
(lambda (c)
(when (pair? prev-line)
(set! cur-line (cdar prev-line))
(set! cursor-pos (caar prev-line))
(set! prev-line (cdr prev-line)))))
;; -------- add newline
(set! (keymap-functions C-o)
(lambda (c)
(set! cur-line (string-append (cond ((= cursor-pos 0)
(values (string #\space #\newline)
cur-line))
((>= cursor-pos (length cur-line))
(values cur-line
(string #\space #\newline)))
((char=? (cur-line (- cursor-pos 1)) #\newline)
(values (substring cur-line 0 cursor-pos)
(string #\space #\newline)
(substring cur-line cursor-pos)))
(else
(values (substring cur-line 0 cursor-pos)
(if (char=? (cur-line (+ cursor-pos 1)) #\newline)
(string #\space #\newline #\space)
(string #\space #\newline))
(substring cur-line (+ cursor-pos 1)))))))
(when (= last-row (+ prompt-row cur-row))
(set! prompt-row (- prompt-row 1))
(display-lines))))
;; -------- transpose
(set! (keymap-functions C-t)
(lambda (c)
(let ((end (end-of-line cursor-pos)))
(save-line)
(let ((cur (if (>= cursor-pos end)
(- cursor-pos 1)
cursor-pos)))
(if (positive? cur)
(let ((tmp-c (cur-line (- cur 1))))
(set! (cur-line (- cur 1)) (cur-line cur))
(set! (cur-line cur) tmp-c)
(set! cursor-pos (+ cur 1))))))))
;; -------- indentation/completion
(set! (keymap-functions Tab)
(lambda (c)
;; if user pastes in a selection, it may have embedded tabs which are just spacing,
;; not requests for filename completion! We'll try to catch this case by assuming
;; that a selection will not be the single character #\tab
(if (> chars 1)
(begin
(set! cur-line (string-append cur-line " "))
(set! cursor-pos (+ cursor-pos 4)))
(let ((start (start-of-line cursor-pos))
(end (end-of-line cursor-pos))
(completion #f))
(if (and (positive? start)
(= end start))
(indent start)
(when (= cursor-pos end)
(let ((loc (do ((i (- end 1) (- i 1)))
((or (< i 0)
(char-whitespace? (cur-line i))
(memv (cur-line i) '(#\( #\' #\" #\))))
i))))
(set! completion (if (< loc 0)
(symbol-completion cur-line)
((if (char=? (cur-line loc) #\") filename-completion symbol-completion)
(substring cur-line (+ loc 1)))))
(when (and completion
(> (length completion) (- end loc 1)))
(save-line)
(set! cur-line (if (= end (length cur-line))
(string-append (substring cur-line 0 (+ loc 1)) completion)
(string-append (substring cur-line 0 (+ loc 1))
completion
(substring cur-line (+ end 1)))))
(set! cursor-pos (end-of-line cursor-pos))))))))))
;; -------- evaluation/multiline
(set! (keymap-functions Enter)
(lambda (c)
(call-with-exit
(lambda (return)
(let ((len (length cur-line)))
(do ((i 0 (+ i 1))) ; check for just whitespace
((or (= i len)
(not (char-whitespace? (cur-line i))))
(when (= i len)
(append-newline)
(return))))
(set! red-par-pos #f)
(if (or (= chars 1)
(not (= input-fd terminal-fd)))
(display-lines))
(catch #t
(lambda ()
;; we want to add cur-line (copied) to the history buffer
;; unless it is an on-going edit (missing close paren)
(catch 'string-read-error
(lambda ()
(set! cursor-pos len)
(if (or (= chars 1)
(not (= input-fd terminal-fd)))
(display-lines))
(set! (history) cur-line)
(set! m-p-pos 0)
(with-repl-let
(lambda ()
;; get the newline out if the expression does not involve a read error
(let ((form (with-input-from-string cur-line #_read))) ; not libc's read
(newline *stderr*)
(let ((val (eval form (*repl* 'top-level-let))))
(if unbound-case
(set! unbound-case #f)
(begin
(format *stderr* "~S~%" val)
;; this set! of '** has one odd consequence: if val is a lambda expression
;; find_closure in s7 will fallback on the current environment trying to
;; find an associated name, and the only thing it finds is '**! So,
;; when we type **, we get back **, which seems perverse. I suppose
;; we could trap the string above, see "**" and change to ~W or something.
(set! ((*repl* 'top-level-let) '**) val))))))))
(lambda (type info)
(pop-history) ; remove last history entry
(append-newline)
(return))))
(lambda (type info)
(format *stderr* "~A:" (red "error"))
(if (and (pair? info)
(string? (car info)))
(format *stderr* " ~A" (apply format #f info))
(if (not (null? info))
(format *stderr* " ~A" info)))
(newline *stderr*)))
(push-line (copy cur-line))
(new-prompt))))))
;; -------- escaped (Meta/Alt/arrow) keys
(set! (keymap-functions Escape)
(lambda (esc)
(let ((chr (next-char)))
((meta-keymap-functions (char->integer chr)) chr))))
(set! (meta-keymap-functions (char->integer #\[))
(lambda (c)
(let ((chr (next-char)))
(case chr
((#\A) ((keymap-functions C-p) C-p)) ; arrow keys
((#\B) ((keymap-functions C-n) C-n))
((#\C) ((keymap-functions C-f) C-f))
((#\D) ((keymap-functions C-b) C-b))))))
(let ()
;; Meta key is a problem on the Mac, so I'll package these for easier disposal
(define fixup-new-line
(let ((count-newlines
(lambda (line)
(do ((len (length line))
(newlines 0)
(i 0 (+ i 1)))
((= i len) newlines)
(if (char=? (cur-line i) #\newline)
(set! newlines (+ newlines 1)))))))
(lambda ()
(set! cursor-pos (length cur-line))
(let ((newlines (count-newlines cur-line)))
(when (< last-row (+ prompt-row newlines))
(format *stderr* "~NC" (- (+ prompt-row newlines) last-row) #\newline)
(set! prompt-row (- prompt-row newlines)))
(set! cur-row newlines)))))
(define (get-previous-line c) ; get earlier line indexed by numeric arg
(let* ((len (length histtop))
(pos (or (string->number cur-line) len)))
(set! pos (min len (max pos 1)))
(set! cur-line (copy (histtop (- pos 1))))
(fixup-new-line)))
(define (move-forward-in-history c)
(set! m-p-pos (min 0 (+ m-p-pos 1)))
(when (positive? (length (history m-p-pos)))
(set! cur-line (history m-p-pos))
(fixup-new-line)))
(define (move-backward-in-history c)
(let ((old-index m-p-pos))
(when (and (zero? m-p-pos)
(> (length cur-line) 0))
(set! (history) cur-line)
(set! m-p-pos -1))
(set! m-p-pos (max (- m-p-pos 1) (- histsize)))
(if (positive? (length (history m-p-pos)))
(begin
(set! cur-line (history m-p-pos))
(fixup-new-line))
(set! m-p-pos old-index))))
(define (go-to-start c)
(set! cursor-pos 0)
'just-cursor)
(define (go-to-end c)
(set! cursor-pos (length cur-line))
'just-cursor)
(define (capitalize c)
(let ((len (length cur-line)))
(let loop ((i cursor-pos))
(if (< i len)
(if (char-alphabetic? (cur-line i))
(begin
(save-line)