-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathst.lisp
1608 lines (1495 loc) · 60.8 KB
/
st.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
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
;;; See LICENSE for licence details.
(in-package #:3bst)
;;; todo: better interface for sending output to child process
;; possibly users could subclass TERM and define a method to handle output?
;; for now just bind this to a function like
;; (lambda (term string) ...)
(defparameter *write-to-child-hook* nil)
;; possibly these should be propertie of term?
(defvar *default-foreground* 7)
(defvar *default-background* 0)
(defvar *tab-spaces* 8)
(defvar *vt-iden* "[?6c") ;; "1;2"=vt100w/advanced video option, "6"=vt102
(defvar *redraw-timeout* (/ 80 1000.0)) ;; 80 ms
(defvar *bindings* (make-hash-table :test 'equalp))
;;; Arbitrary sizes
#++(defconstant +UTF-INVALID+ #xFFFD)
#++(defconstant +UTF-SIZ+ 4)
#++(defconstant +ESC-BUF-SIZ+ (128*+UTF-SIZ+))
(defconstant +esc-max-args+ 16)
#++(defconstant +STR-BUF-SIZ+ +ESC-BUF-SIZ+)
#++(defconstant +STR-ARG-SIZ+ +ESC-ARG-SIZ+)
(defconstant +csi-buf-max-size+ 128)
(defconstant +str-buf-max-size+ 128)
#++(defconstant +DRAW-BUF-SIZ+ 20*1024)
(defun controlc0-p (c)
(or (= c 177) (<= 0 c #x1f)))
(defun controlc1-p (c)
(<= #x80 c #x9f))
(defun control-p (c)
(or (controlc1-p c) (controlc0-p c)))
(defmacro ensure-value (x default)
;; fixme: get rid of multiple evaluation
`(unless (and ,x (not (zero ,x)))
(seft ,x ,default)))
(defmacro ensure-aref (array index default)
"adjust array to hold at least INDEX elements, and set element INDEX
to DEFAULt if not already set"
(let ((a (gensym))
(i (gensym)))
`(let ((,a ,array)
(,i ,index))
(when (< (fill-pointer ,a) (1+ ,i))
(unless (adjustable-array-p ,a)
(assert (< ,i (array-total-size ,a))))
(adjust-array ,a (max (1+ ,i) (array-total-size ,a))
:fill-pointer (1+ ,i)
:initial-element nil))
(unless (and (aref ,a ,i) (plusp (aref ,a ,i)))
(setf (aref ,a ,i) ,default)))))
#++(let ((a (make-array 6 :fill-pointer 0)))
(ensure-aref a 0 2)
(ensure-aref a 1 3)
a)
(defun limit (x a b)
(min b (max a x)))
(defmacro limitf (x a b)
;; fixme: get rid of repeated evaluation of X
`(setf ,x (limit ,x ,a ,b)))
(defun attribute/= (a b)
;; assuming mode is an int with flag bits for now
(or (/= (mode a) (mode b))
(/= (fg a) (fg b))
(/= (bg a) (bg b))))
(defvar *term*)
(defun attribute-set-p (flag &key (term *term*)) ;; IS_SET
;; fixme: might be nicer to have :keywords instead of +constants+ for flags?
(logtest flag (mode term)))
(defmacro modbit (x set mask)
;; fixme: avoid multiple evaluation
`(setf ,x
,(case set
((nil)
`(logandc2 ,x ,mask))
((t)
`(logior ,x ,mask))
(t
`(if ,set
(logior ,x ,mask)
(logandc2 ,x ,mask))))))
(defun truecolor (r g b)
(logior (ash 1 24)
(ash r 16)
(ash g 8)
b))
(defun truecolorp (x)
(logbitp 24 x))
(defun truecolor-red (x)
(ldb (byte 8 16) x))
(defun truecolor-green (x)
(ldb (byte 8 8) x))
(defun truecolor-blue (x)
(ldb (byte 8 0) x))
(defun color-rgb (color)
;; fixme: should this return list or (typed?) vector?
(labels ((c (r g b)
(list (/ r 255.0) (/ g 255.0) (/ b 255.0)))
(c6 (x)
(let ((b (mod x 6))
(g (mod (floor x 6) 6))
(r (mod (floor x 36) 6)))
(list (/ r 5.0) (/ g 5.0) (/ b 5.0))))
(g (x)
(c (* x 16) (* x 16) (* x 16))))
(if (truecolorp color)
(c (truecolor-red color)
(truecolor-green color)
(truecolor-blue color))
(case color
(0 (c 0 0 0))
(1 (c 205 0 0))
(2 (c 0 205 0))
(3 (c 205 205 0))
(4 (c 0 0 238))
(5 (c 205 0 205))
(6 (c 0 205 205))
(7 (c 229 229 229))
(8 (c 127 127 127))
(9 (c 255 0 0))
(10 (c 0 255 0))
(11 (c 255 255 0))
(12 (c 92 92 255))
(13 (c 255 0 255))
(14 (c 0 255 255))
(15 (c 255 255 255))
(t (let ((c (- color 16)))
(if (< c 216)
(c6 c)
(g (- c 216)))))))))
;; not sure if these should be bit masks or bit indices?
;; (or keywords?)
(defconstant +ATTR-NULL+ 0)
(defconstant +ATTR-BOLD+ (ash 1 0))
(defconstant +ATTR-FAINT+ (ash 1 1))
(defconstant +ATTR-ITALIC+ (ash 1 2))
(defconstant +ATTR-UNDERLINE+ (ash 1 3))
(defconstant +ATTR-BLINK+ (ash 1 4))
(defconstant +ATTR-REVERSE+ (ash 1 5))
(defconstant +ATTR-INVISIBLE+ (ash 1 6))
(defconstant +ATTR-STRUCK+ (ash 1 7))
(defconstant +ATTR-WRAP+ (ash 1 8))
(defconstant +ATTR-WIDE+ (ash 1 9))
(defconstant +ATTR-WDUMMY+ (ash 1 10))
(defconstant +cursor-default+ 0)
(defconstant +cursor-wrap-next+ 1)
(defconstant +cursor-origin+ 2)
(defconstant +MODE-WRAP+ (ash 1 0))
(defconstant +MODE-INSERT+ (ash 1 1))
(defconstant +MODE-APPKEYPAD+ (ash 1 2))
(defconstant +MODE-ALTSCREEN+ (ash 1 3))
(defconstant +MODE-CRLF+ (ash 1 4))
(defconstant +MODE-MOUSEBTN+ (ash 1 5))
(defconstant +MODE-MOUSEMOTION+ (ash 1 6))
(defconstant +MODE-REVERSE+ (ash 1 7))
(defconstant +MODE-KBDLOCK+ (ash 1 8))
(defconstant +MODE-HIDE+ (ash 1 9))
(defconstant +MODE-ECHO+ (ash 1 10))
(defconstant +MODE-APPCURSOR+ (ash 1 11))
(defconstant +MODE-MOUSESGR+ (ash 1 12))
(defconstant +MODE-8BIT+ (ash 1 13))
(defconstant +MODE-BLINK+ (ash 1 14))
(defconstant +MODE-FBLINK+ (ash 1 15))
(defconstant +MODE-FOCUS+ (ash 1 16))
(defconstant +MODE-MOUSEX10+ (ash 1 17))
(defconstant +MODE-MOUSEMANY+ (ash 1 18))
(defconstant +MODE-BRCKTPASTE+ (ash 1 19))
(defconstant +MODE-PRINT+ (ash 1 20))
(defconstant +MODE-MOUSE+ (logior +MODE-MOUSEBTN+
+MODE-MOUSEMOTION+
+MODE-MOUSEX10+
+MODE-MOUSEMANY+))
(defconstant +ESC-START+ 1)
(defconstant +ESC-CSI+ 2)
(defconstant +ESC-STR+ 4) ; DCS, OSC, PM, APC
(defconstant +ESC-ALTCHARSET+ 8)
(defconstant +ESC-STR-END+ 16) ; a final string was encountered
(defconstant +ESC-TEST+ 32) ; Enter in test mode
(defconstant +WIN-VISIBLE+ 1)
(defconstant +WIN-REDRAW+ 2)
(defconstant +WIN-FOCUSED+ 4)
(defconstant +SEL-REGULAR+ 1)
(defconstant +SEL-RECTANGULAR+ 2)
(defconstant +SNAP-WORD+ 1)
(defconstant +SNAP-LINE+ 2)
;; not sure if we want AoS or SoA for term data yet?
(defclass glyph ()
;; C stores a CL character instead of utf8 encoded string
((c :accessor c :initform #\space)
(mode :accessor mode :initform +attr-null+)
(fg :accessor fg :initform *default-foreground*)
(bg :accessor bg :initform *default-background*)))
(defmethod glyph-attributes ((g glyph))
(loop for mask in (list +ATTR-NULL+ +ATTR-BOLD+ +ATTR-FAINT+ +ATTR-ITALIC+
+ATTR-UNDERLINE+ +ATTR-BLINK+ +ATTR-REVERSE+
+ATTR-INVISIBLE+ +ATTR-STRUCK+ +ATTR-WRAP+
+ATTR-WIDE+ +ATTR-WDUMMY+)
for key in '(:NULL :BOLD :FAINT :ITALIC :UNDERLINE :BLINK
:REVERSE :INVISIBLE :STRUCK :WRAP :WIDE :WDUMMY)
when (logtest mask (mode g))
collect key))
(deftype line () '(vector glyph *))
(defun move-glyphs (line &key (start1 0) (start2 0)
(end1 (length line))
(end2 (length line)))
;; fixme: better representation of LINE and/or GLYPH for easier copying?
;; just using REPLACE on LINE ends up with same GLYPH object in
;; multiple places
;; -- maybe just use REPLACE and fill the gap with new GLYPHs?
(if (<= start1 start2)
(loop for i from start1 below end1
for j from start2 below end2
for d = (aref line i)
for s = (aref line j)
do (setf (c d) (c s)
(mode d) (mode s)
(fg d) (fg s)
(bg d) (bg s)))
(loop for i from (1- end1) downto start1
for j from (1- end2) downto start2
for d = (aref line i)
for s = (aref line j)
do (setf (c d) (c s)
(mode d) (mode s)
(fg d) (fg s)
(bg d) (bg s)))))
(defclass tcursor ()
((attributes :accessor attributes :initform (make-instance 'glyph))
(x :accessor x :initform 0)
(y :accessor y :initform 0)
(state :accessor state :initform 0)))
(defmethod (setf x) :before (new (c tcursor))
(assert (numberp new)))
(defmethod (setf y) :before (new (c tcursor))
(assert (numberp new)))
(defun copy-glyph (g &key (to (make-instance 'glyph)))
(setf (c to) (c g)
(mode to) (mode g)
(fg to) (fg g)
(bg to) (bg g))
to)
(defun copy-cursor (c &key (to (make-instance 'tcursor)))
(copy-glyph (attributes c) :to (attributes to))
(setf (x to) (x c)
(y to) (y c)
(state to) (state c))
to)
;; CSI Escape sequence structs
;; ESC '[' [[ [<priv>] <arg> [;]] <mode>]
(defclass csi-escape ()
;; fixme: decide initial sizes of these arrays based on usage
((buffer :accessor buffer :initform (make-array 8
:adjustable t
:fill-pointer 0
:element-type 'character))
(priv :accessor priv :initform nil)
(arguments :accessor arguments :initform (make-array 1
:adjustable t
:fill-pointer 0
:element-type 'integer))
(mode :accessor mode :initform 0)))
;;; STR Escape sequence structs
;;; ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\'
(defclass str-escape ()
((str-type :accessor str-type :initform 0 :initarg :c)
(buffer :accessor buffer :initform (make-array 8
:adjustable t
:fill-pointer 0
:element-type 'character))
(priv :accessor priv :initform nil)
(arguments :accessor arguments
:initform (make-array 2
:adjustable t
:fill-pointer 0))))
;;; Internal representation of the screen
(defun make-screen-array (rows columns)
;; array of arrays instead of 2d array to match original
;; which swaps lines for scrolling, and to allow sequence ops
;; within a line
(make-array rows
:element-type '(vector glyph *)
:initial-contents
(loop repeat rows
collect (coerce
(loop repeat columns
collect (make-instance 'glyph))
'(vector glyph)))))
(declaim (inline glyph-at))
(defun glyph-at (screen y x)
(aref (aref screen y) x))
(defun map-screen (screen function)
(loop for line across screen
for y from 0
do (loop for glyph across line
for x from 0
do (funcall function glyph y x))))
(defclass term ()
((rows :reader rows :initarg :rows :initform 25)
(columns :reader columns :initarg :columns :initform 80)
;; vector of vectors of glyphs?
(screen :reader screen)
(alternate-screen :reader alternate-screen)
;; bit/boolean array with element fpor each row?
(dirty :reader dirty)
(cursor :accessor cursor :initform (make-instance 'tcursor))
;; top/bottom scroll limits
(top :accessor top :initform 0)
(bottom :accessor bottom :initform 0)
;; terminal mode flags
(mode :accessor mode :initform +mode-crlf+ :initarg :mode)
;; escape state flags
(escape :accessor escape :initform 0)
;; charset table translation
;; fixme: initial value?
(translation-table :reader translation-table
:initform (make-array 4 :initial-element :cs-usa))
(charset :accessor charset :initform 0)
;; selected charset for sequence
(icharset :accessor icharset :initform 0)
(numlock :accessor numlock :initform t)
;; tab stops?
(tabs :reader tabs)
;; if processing input as raw bytes, store partial utf-8 characters
(partial-raw-input :accessor partial-raw-input :initform nil)
;;
(saved-cursors :reader saved-cursors
:initform (make-array 2 :initial-contents
(loop repeat 2
collect (make-instance 'tcursor))))
(allow-alt-screen :accessor allow-alt-screen :initform t)
(csi-escape :accessor csi-escape :initform (make-instance 'csi-escape))
(str-escape :accessor str-escape :initform (make-instance 'str-escape))
(user-object :accessor user-object :initform nil)
(set-title :accessor on-set-title :initform nil)))
(defmethod initialize-instance :after ((term term) &key)
(setf (slot-value term 'screen)
(make-screen-array (rows term) (columns term)))
(setf (slot-value term 'alternate-screen)
(make-screen-array (rows term) (columns term)))
(setf (slot-value term 'dirty)
(make-array (rows term) :element-type 'bit))
(setf (slot-value term 'tabs)
(make-array (columns term) :element-type 'bit))
(setf (bottom term) (1- (rows term))))
;;; Globals
(defparameter *term* (make-instance 'term :rows 25 :columns 80))
#++(defparameter *csi-escape* (make-instance 'csi-escape))
#++(defparameter *str-escape* (make-instance 'str-escape))
;; static Selection sel;
(defparameter *title* "")
;;
;;static uchar utfbyte[+UTF-SIZ+ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
;;static uchar utfmask[+UTF-SIZ+ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
;;static long utfmin[+UTF-SIZ+ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
;;static long utfmax[+UTF-SIZ+ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
(defun term-line-length (y &key (term *term*))
(let* ((screen (screen term))
(i (array-dimension screen 1)))
(when (logtest +attr-wrap+ (mode (glyph-at screen y (1- i))))
(return-from term-line-length i))
(loop while (and (plusp i)
(eql #\space (c (glyph-at screen y (1- i)))))
do (decf i))
i))
;;;; todo: mouse/selection stuff
;;;; todo: utils for running a shell with env etc? handle child closed, etc
;; (probably mostly let uiop deal with that)
(defun handle-input-raw (octets &key (term *term*))
"process OCTETS as (possibly incomplete) UTF8 encoded input from
child process"
(declare (ignore octets term))
(error "not done yet, use character input..."))
(defun handle-input (characters &key (term *term*))
""
(let ((*term* term))
(map 'nil #'tputc characters)))
(defun tty-write (characters &key (term *term*))
;; (format t "tty-write ~s~%" characters)
(when *write-to-child-hook*
(funcall *write-to-child-hook* term characters)))
(defun tty-send (characters &key (term *term*))
(tty-write characters :term term)
(when (attribute-set-p +mode-echo+ :term term)
(techo characters :term term)))
#++
(defun tty-resize ()
;; todo: implement some way of passing this to caller in case it has a TTY
;; and wants to do ioctl(..., TIOCSWINSZ, ...) or similar
)
(defun tattrset (attr &key (term *term*))
(loop for line across (screen term)
thereis (loop for glyph across line
thereis (logtest attr (mode glyph)))))
(defun tsetdirt (top bottom &key (term *term*))
(loop for i from (limit top 0 (1- (rows term)))
below (limit bottom 0 (1- (rows term)))
do (setf (aref (dirty term) i) 1)))
(defun tsetdirtattr (attr &key (term *term*))
(loop for line across (screen term)
for i from 0
do (loop for glyph across line
when (logtest attr (mode glyph))
do (tsetdirt i i :term term)
and return nil)))
(defun tfulldirt (&key (term *term*))
(tsetdirt 0 (1- (rows term)) :term term))
(defun tcursor (mode &key (term *term*))
(let ((index (if (attribute-set-p +mode-altscreen+ :term term) 1 0)))
(if (eql mode :cursor-save)
(setf (aref (saved-cursors term) index)
(copy-cursor (cursor term)))
(progn
(let ((c (aref (saved-cursors term) index)))
(copy-cursor c :to (cursor term))
(tmoveto (x c) (y c)))))))
(defun treset (&key (term *term*))
;; fixme: this should probably mostly be in reinitialize-instance or something
;; fixme: possibly should pass fg,bg,tab spaces, etc as keyword args? (depending on if user code calls this or not)
(let ((c (cursor term)))
(setf (attributes c) (make-instance 'glyph)
(x c) 0
(y c) 0
(state c) 0))
(fill (tabs term) 0)
(loop for i from *tab-spaces* below (columns term) by *tab-spaces*
do (setf (aref (tabs term) i) 1))
(setf (top term) 0
(bottom term) (1- (rows term))
(mode term) +mode-wrap+
(charset term) 0)
(fill (translation-table term) :cs-usa)
(loop repeat 2
do (tmoveto 0 0 :term term)
(tcursor :cursor-save :term term)
(tclearregion 0 0 (1- (columns term)) (1- (rows term)) :term term)
(tswapscreen :term term)))
(defun tswapscreen (&key (term *term*))
(rotatef (slot-value term 'screen) (slot-value term 'alternate-screen))
(setf (mode term) (logxor (mode term) +mode-altscreen+))
(tfulldirt :term term))
(defun tscrolldown (orig n &key (term *term*))
(let* ((bottom (bottom term)) (n (limit n 0 (1+ (- bottom orig))))
(screen (screen term)))
(tsetdirt orig bottom :term term)
(tclearregion 0 (1+ (- bottom n)) (1- (columns term)) bottom :term term)
(loop for i from bottom downto (+ orig n)
do (rotatef (aref screen i)
(aref screen (- i n))))
#++(selscroll orig n) ;; todo
))
(defun tscrollup (orig n &key (term *term*))
(let* ((bottom (bottom term)) (n (limit n 0 (1+ (- bottom orig))))
(screen (screen term)))
(tclearregion 0 orig (1- (columns term)) (1- (+ orig n)) :term term)
(tsetdirt (+ orig n) bottom :term term)
(loop for i from orig to (- bottom n)
do (rotatef (aref screen i)
(aref screen (+ i n))))
#++(selscroll orig (- n)) ;; todo
))
#++
(defun selscroll (orig n)
;; todo: selection stuff
)
(defun tnewline (first-column &key (term *term*))
(let ((y (y (cursor term))))
(if (= y (bottom term))
(tscrollup (top term) 1 :term term)
(incf y))
(tmoveto (if first-column 0 (x (cursor term)))
y :term term)))
(defun csiparse (csi)
(let ((p 0))
(flet ((*p ()
(aref (buffer csi) p)))
(setf (fill-pointer (arguments csi)) 0)
(setf (priv csi) nil)
(when (char= (*p) #\?)
(setf (priv csi) 1)
(incf p))
(loop while (< p (fill-pointer (buffer csi)))
do (multiple-value-bind (v np)
(parse-integer (buffer csi) :start p :radix 10
:junk-allowed t)
(vector-push-extend (or v 0)
(arguments csi))
(setf p np)
(when (or (char/= (*p) #\;)
;; possibly should ERROR with restarts
;; instead of just giving up?
(> (length (arguments csi))
+esc-max-args+))
(loop-finish))
(incf p)))
(setf (mode csi) (*p)))))
;;; for absolute user moves, when decom is set
(defun tmoveato (x y &key (term *term*))
#++(format *debug-io* "moveato ~s,~s -> ~s ~s~%"
(x (cursor term)) (y (cursor term))
x y)
(tmoveto x (+ y (if (logtest (state (cursor term)) +cursor-origin+)
(top term)
0))
:term term))
(defun tmoveto (x y &key (term *term*))
#++(format *debug-io* "moveto ~s,~s -> ~s ~s~%"
(x (cursor term)) (y (cursor term))
x y)
(let ((miny 0)
(maxy (1- (rows term)))
(c (cursor term)))
(when (logtest +cursor-origin+ (state c))
(setf miny (top term)
maxy (bottom term)))
(setf x (limit x 0 (1- (columns term))))
(setf y (limit y miny maxy))
(setf (state c) (logandc2 (state c) +cursor-wrap-next+)
(x c) x
(y c) y)))
(defparameter *vt100-0*
;; The table is proudly stolen from rxvt.
(map 'vector 'code-char
;; fixme: figure out encoding problems that prevented just
;; putting in characters...
#(8593 8595 8594 8592 9608 9626 9731
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 32
9670 9618 9225 9228 9229 9226 176 177
9252 9227 9496 9488 9484 9492 9532 9146
9147 9472 9148 9149 9500 9508 9524 9516
9474 8804 8805 960 8800 163 183)))
;; fixme: move attr to keyword args instead of allocating a tmp object?
(defun tsetchar (c attr x y &key (term *term*))
(when (and (eq (aref (translation-table term)
(charset term))
:cs-graphic0)
(< #x41 (char-code c) #x7e))
(setf c (aref *vt100-0* (- (char-code c) #x41))))
(if (logtest +attr-wide+ (mode (glyph-at (screen term) y x)))
(when (< (1+ x) (columns term))
(setf (c (glyph-at (screen term) y (1+ x))) #\space
(mode (glyph-at (screen term) y (1+ x)))
(logxor (mode (glyph-at (screen term) y (1+ x)))
+attr-wdummy+)))
(when (logtest +attr-wdummy+ (mode (glyph-at (screen term) y x)))
(setf (c (glyph-at (screen term) y (1- x))) #\space
(mode (glyph-at (screen term) y (1- x)))
(logxor (mode (glyph-at (screen term) y (1- x)))
+attr-wide+))))
(setf (aref (dirty term) y) 1)
(let ((g (glyph-at (screen term) y x)))
#++(format t "~& ~a @ ~a ~a (~a -> " c x y (c g))
(setf (c g) c
(mode g) (mode attr)
(fg g) (fg attr)
(bg g) (bg attr))
#++(format t "~a / ~a)~%" (c g)
(c (aref (aref (screen *term*) y) x)))))
(defun tclearregion (x1 y1 x2 y2 &key (term *term*))
(when (> x1 x2)
(rotatef x1 x2))
(when (> y1 y2)
(rotatef y1 y2))
(setf x1 (limit x1 0 (1- (columns term)))
x2 (limit x2 0 (1- (columns term)))
y1 (limit y1 0 (1- (rows term)))
y2 (limit y2 0 (1- (rows term))))
(loop for y from y1 to y2
do (setf (aref (dirty term) y) 1)
(loop for x from x1 to x2
for g = (glyph-at (screen term) y x)
do #++(when (selected x y)
(selclear nil))
(setf (fg g) (fg (attributes (cursor term)))
(bg g) (bg (attributes (cursor term)))
(mode g) 0
(c g) #\space))))
(defun tdeletechar (n &key (term *term*))
(limitf n 0 (- (columns term) (x (cursor term))))
(let* ((dest (x (cursor term)))
(source (+ dest n))
(line (aref (screen term) (y (cursor term)))))
(move-glyphs line :start1 dest :start2 source :end2 (columns term))
(tclearregion (- (columns term) n) (y (cursor term))
(1- (columns term)) (y (cursor term)))))
(defun tinsertblank (n &key (term *term*))
(limitf n 0 (- (columns term) (x (cursor term))))
(let* ((source (x (cursor term)))
(dest (+ source n))
(line (aref (screen term) (y (cursor term)))))
(move-glyphs line :start1 dest :start2 source :end1 (columns term))
(tclearregion source (y (cursor term))
(1- dest) (y (cursor term)))))
(defun tinsertblankline (n &key (term *term*))
(when (<= (top term) (y (cursor term)) (bottom term))
(tscrolldown (y (cursor term)) n :term term)))
(defun tdeleteline (n &key (term *term*))
(when (<= (top term) (y (cursor term)) (bottom term))
(tscrollup (y (cursor term)) n :term term)))
(defun tdefcolor (attributes start)
(case (aref attributes (1+ start))
(2 ;; direct color in RGB space
(when (> (+ start 4) (length attributes))
(warn "erresc(38): Incorrect number of parameters (~a)" start)
(return-from tdefcolor (values -1 start)))
(let ((r (aref attributes (+ start 2)))
(g (aref attributes (+ start 3)))
(b (aref attributes (+ start 4))))
(incf start 4)
(if (not (and (<= 0 r 255) (<= 0 g 255) (<= 0 b 255)))
(warn "erresc: bad rgb color (~a ~a ~a)" r g b)
(values (truecolor r g b) start))))
(5 ;; indexed color
(when (> (+ start 2) (length attributes))
(warn "erresc(38): Incorrect number of parameters (~a)" start)
(return-from tdefcolor (values -1 start)))
(incf start 2)
(if (not (<= 0 (aref attributes start) 255))
(warn "erresc: bad color ~a" (aref attributes start))
(values (aref attributes start) start)))
((0 ;; implemented defined (only foreground)
1 ;; transparent
3 ;; direct color in CMY space
4 ;; direct color in CMYK space
t)
(warn "erresc(38): gfx attr ~a/~a unknown" (aref attributes start)
(aref attributes (1+ start)))
(values -1 start))))
(defun tsetattr (attributes &key (term *term*))
(loop with attr = (attributes (cursor term))
;; can't use from 1 to x because we skip more than 1 sometimes
for i = 0 then (1+ i)
while (< i (length attributes))
do (flet ((on (&rest a)
(setf (mode attr) (apply #'logior (mode attr) a)))
(off (&rest a)
(setf (mode attr) (logandc2 (mode attr)
(apply #'logior a)))))
(case (aref attributes i)
(0
(off +ATTR-BOLD+
+ATTR-FAINT+
+ATTR-ITALIC+
+ATTR-UNDERLINE+
+ATTR-BLINK+
+ATTR-REVERSE+
+ATTR-INVISIBLE+
+ATTR-STRUCK+)
(setf (fg attr) *default-foreground*
(bg attr) *default-background*))
(1 (on +attr-bold+))
(2 (on +attr-faint+))
(3 (on +attr-italic+))
(4 (on +attr-underline+))
(5 (on +attr-blink+)) ;; slow blink
(6 (on +attr-blink+)) ;; rapid blink
(7 (on +attr-reverse+))
(8 (on +attr-invisible+))
(9 (on +attr-struck+))
;; 10 = primary font
;; 11-19 = alternate fonts
;; 20 = fraktur font
(21 (off +attr-bold+)) ;; bold off or underline double?
(22 (off +attr-bold+ +attr-faint+))
(23 (off +attr-italic+))
(24 (off +attr-underline+))
(25 (off +attr-blink+))
;; 26 reserved
(27 (off +attr-reverse+))
(28 (off +attr-invisible+))
(29 (off +attr-struck+))
;; 30-37 below
(38 (multiple-value-bind (index next-i)
(tdefcolor attributes i)
(setf (fg attr) index
i next-i)))
(39 (setf (fg attr) *default-foreground*))
;; 40-47 below
(48 (multiple-value-bind (index next-i)
(tdefcolor attributes i)
(setf (bg attr) index
i next-i)))
(49 (setf (bg attr) *default-background*))
;; 50 reserved
;; 51 framed
;; 52 encircled
;; 53 overline
;; 54 not framed or encircled
;; 55 not overlined
;; 56-59 reserved
;; 60-65 ideogram underline/overline/stress/etc
(t
(let ((a (aref attributes i)))
(cond
((<= 30 a 37) ;; text color
(setf (fg attr) (- a 30)))
((<= 40 a 47) ;; bg color
(setf (bg attr) (- a 40)))
((<= 90 a 97) ;; text color high intensity
(setf (fg attr) (+ 8 (- a 90))))
((<= 100 a 107) ;; bg color high intensity
(setf (bg attr) (+ 8 (- a 100)))))))))))
(defun tsetscroll (top bottom &key (term *term*))
(limitf top 0 (1- (rows term)))
(limitf bottom 0 (1- (rows term)))
(when (> top bottom)
(rotatef top bottom))
(setf (top term) top
(bottom term) bottom))
(defun tsetmode (priv set args &key (term *term*))
(setf set (and set (not (zerop set))))
; (break "tsetmode ~s ~s ~s ~s" priv set args term)
(loop for arg across args
if priv
do (case arg
(1 ;; DECCKM -- Cursor key
(modbit (mode term) set +mode-appcursor+))
(5 ;; DECSCNM -- Reverse video
(let ((old (mode term)))
(modbit (mode term) set +mode-reverse+)
(unless (= old (mode term))
;; not sure if this needs timeout?
;; probably should be handled differently if it does
#++(redraw *redraw-timeout* :term term))))
(6 ;; DECOM -- Origin
(modbit (state (cursor term)) set +cursor-origin+)
(tmoveato 0 0 :term term))
(7 ;; DECAWM -- Auto wrap
(modbit (mode term) set +mode-wrap+))
((0 ;; Error (IGNORED)
2 ;; DECANM -- ANSI/VT52 (IGNORED)
3 ;; DECCOLM -- Column (IGNORED)
4 ;; DECSCLM -- Scroll (IGNORED)
8 ;; DECARM -- Auto repeat (IGNORED)
18 ;; DECPFF -- Printer feed (IGNORED)
19 ;; DECPEX -- Printer extent (IGNORED)
42 ;; DECNRCM -- National characters (IGNORED)
12)) ;; att610 -- Start blinking cursor (IGNORED)
(25 ;; DECTCEM -- Text Cursor Enable Mode
(modbit (mode term) (not set) +mode-hide+))
(9 ;; X10 mouse compatibility mode
#++ (xsetpointermotion nil :term term)
(modbit (mode term) nil +mode-mouse+)
(modbit (mode term) set +mode-mousex10+))
(1000 ;;1000: report button press
#++(xsetpointermotion nil :term term)
(modbit (mode term) nil +mode-mouse+)
(modbit (mode term) set +mode-mousebtn+))
(1002 ;; 1002: report motion on button press
#++(xsetpointermotion nil :term term)
(modbit (mode term) nil +mode-mouse+)
(modbit (mode term) set +mode-mousemotion+))
(1003 ;; 1003: enable all mouse motions
#++(xsetpointermotion set :term term)
(modbit (mode term) nil +mode-mouse+)
(modbit (mode term) set +mode-mousemany+))
(1004 ;; 1004: send focus events to tty
(modbit (mode term) set +mode-focus+))
(1006 ;; 1006: extended reporting mode
(modbit (mode term) set +mode-mousesgr+))
(1034
(modbit (mode term) set +mode-8bit+))
((1049 ;; swap screen & set/restore cursor as xterm
47 ;; swap screen
1047
1048)
(when (and (eql arg 1049)
(allow-alt-screen term))
(tcursor (if set :cursor-save :cursor-load)
:term term))
(when (member arg '(1049 47 1047))
(when (allow-alt-screen term)
(let ((alt (logtest (mode term) +mode-altscreen+)))
(when alt
(tclearregion 0 0 (1- (columns term)) (1- (rows term))
:term term))
(unless (eql set alt)
(tswapscreen :term term)))))
(when (member arg '(1049 1048))
(tcursor (if set :cursor-save :cursor-load)
:term term)))
(2004 ;; 2004: bracketed paste mode
(modbit (mode term) set +mode-brcktpaste+))
;; Not implemented mouse modes. See comments there.
((;; mouse highlight mode; can hang the terminal by
;; design when implemented.
1001
;; UTF-8 mouse mode; will confuse applications not
;; supporting UTF-8 and luit.
1005
;; urxvt mangled mouse mode; incompatible and can be
;; mistaken for other control codes.
1015))
(t
(warn "unknown private set/reset mode ~a" arg)))
else do
(case arg
(0 ;; Error (IGNORED)
)
(2 ;; KAM -- keyboard action
(modbit (mode term) set +mode-kbdlock+))
(4 ;;IRM -- Insertion-replacement
(modbit (mode term) set +mode-insert+))
(12 ;; SRM -- Send/Receive
(modbit (mode term) (not set) +mode-echo+))
(20 ;;LNM -- Linefeed/new line
(modbit (mode term) set +mode-crlf+))
(t
(warn "erresc: unknown set/reset mode ~a" arg)))))
(defun csihandle (csi &key (term *term*))
(flet ((unknown ()
(warn "erresc: unknown csi ~a"
(csidump csi :term term))
;; die("");
))
(case (mode csi)
(#\@ ;; ICH -- Insert <n> blank char
(ensure-aref (arguments csi) 0 1))
(#\A ;; CUU -- Cursor <n> Up
(ensure-aref (arguments csi) 0 1)
(tmoveto (x (cursor term))
(- (y (cursor term)) (aref (arguments csi) 0))
:term term))
((#\B ;; CUD -- Cursor <n> Down
#\e) ;; VPR --Cursor <n> Down
(ensure-aref (arguments csi) 0 1)
(tmoveto (x (cursor term))
(+ (y (cursor term))
(aref (arguments csi) 0))
:term term))
(#\i ;; MC -- Media Copy
;; not sure if it should error or default here?
(ensure-aref (arguments csi) 0 0)
(case (aref (arguments csi) 0)
(0 (tdump :term term))
(1 (tdumpline (y (cursor term)) :term term))
(2 #++(tdumpsel :term term))
(4 (modbit (mode term) nil +mode-print+))
(5 (modbit (mode term) t +mode-print+))))
(#\c ;; DA -- Device Attributes
(when (zerop (aref (arguments csi) 0))
(tty-write *vt-iden* :term term)))
((#\C ;; CUF -- Cursor <n> Forward
#\a) ;; HPR -- Cursor <n> Forward
(ensure-aref (arguments csi) 0 1)
(tmoveto (+ (x (cursor term))
(aref (arguments csi) 0))
(y (cursor term))
:term term))
(#\D ;; CUB -- Cursor <n> Backward
(ensure-aref (arguments csi) 0 1)
(tmoveto (- (x (cursor term))
(aref (arguments csi) 0))
(y (cursor term))
:term term))
(#\E ;; CNL -- Cursor <n> Down and first col
(ensure-aref (arguments csi) 0 1)
(tmoveto 0 (+ (y (cursor term))
(aref (arguments csi) 0))
:term term))
(#\F ;; CPL -- Cursor <n> Up and first col
(ensure-aref (arguments csi) 0 1)
(tmoveto 0 (- (y (cursor term))
(aref (arguments csi) 0))
:term term))
(#\g ;; TBC -- Tabulation clear
;; not sure if it should error or default here?
(ensure-aref (arguments csi) 0 0)
(case (aref (arguments csi) 0)
(0 ;; clear current tab stop
(setf (aref (tabs term) (x (cursor term))) 0))
(3 ;; clear all the tabs
(fill (tabs term) 0))
(t
(unknown)))
)
((#\G ;; CHA -- Move to <col>
#\`);; HPA
(ensure-aref (arguments csi) 0 1)
(tmoveto (1- (aref (arguments csi) 0))
(y (cursor term))
:term term))
((#\H ;; CUP -- Move to <row> <col>
#\f);; HVP
(ensure-aref (arguments csi) 0 1)
(ensure-aref (arguments csi) 1 1)
(tmoveato (1- (aref (arguments csi) 1))
(1- (aref (arguments csi) 0))))
(#\I ;; CHT -- Cursor Forward Tabulation <n> tab stops
(ensure-aref (arguments csi) 0 1)
(tputtab (aref (arguments csi) 0) :term term))
(#\J ;; ED -- Clear screen
#++(selclear nil :term term)
;; not sure if it should error or default here?
(ensure-aref (arguments csi) 0 0)
(case (aref (arguments csi) 0)
(0 ;; below
(tclearregion (x (cursor term)) (y (cursor term))
(1- (columns term)) (y (cursor term))
:term term)
(when (< (y (cursor term)) (1- (rows term)))
(tclearregion 0 (1+ (y (cursor term)))
(1- (columns term)) (1- (rows term))
:term term)))
(1 ;; above
(when (> (y (cursor term)) 1)
(tclearregion 0 0 (1- (columns term)) (1- (y (cursor term)))
:term term))
(tclearregion 0 (y (cursor term))
(x (cursor term)) (y (cursor term))
:term term))
(2 ;; all
(tclearregion 0 0 (1- (columns term)) (1- (rows term))
:term term))