-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlisp.lisp
1312 lines (1172 loc) · 46.9 KB
/
lisp.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
(in-package #:neomacs)
(sera:export-always
'(*autodoc-delay*))
;;; Lisp mode
(define-mode lisp-mode (prog-mode) ()
(:documentation "Lisp mode.")
(:hooks auto-completion-mode))
(define-keys lisp-mode
"C-M-x" 'eval-defun
"C-c C-c" 'compile-defun
"C-c C-k" 'lisp-compile-file
"M-p" 'previous-compiler-note
"M-n" 'next-compiler-note
"M-." 'goto-definition
"tab" 'show-completions
"C-x C-e" 'eval-last-expression
"C-c C-p" 'eval-print-last-expression)
(defvar *sexp-node-keymap*
(make-keymap
'sexp-node
"tab" 'show-completions
"M-(" 'wrap-paren
"M-;" 'wrap-comment
"M-r" 'lisp-raise
"M-s" 'lisp-splice
";" 'open-comment
"(" 'open-paren
"\"" 'open-string
"space" 'open-space))
(defvar *plaintext-node-keymap*
(make-keymap
'plaintext-node
"enter" 'new-line
"(" 'self-insert-command
"\"" 'self-insert-command
"space" 'self-insert-command))
(defvar *object-presentation-keymap*
(make-keymap
'object-presentation
"enter" 'inspect-presentation))
(defmethod selectable-p-aux ((buffer lisp-mode) pos)
(and (if (eql *this-command* 'self-insert-command)
(characterp (node-before pos))
(if-let (node (node-after pos))
(and (not (symbol-node-p node))
(not (and (new-line-node-p node)
(non-empty-symbol-node-p (node-before pos)))))
(not (non-empty-symbol-node-p (node-before pos)))))
(call-next-method)))
(defmethod render-focus-aux ((buffer lisp-mode) pos)
(match pos
((end-pos node)
(if (non-empty-symbol-node-p node)
(let ((next (next-sibling node)))
(cond ((not next)
(call-next-method buffer (end-pos (parent node))))
(t (call-next-method))))
(call-next-method)))
(_ (call-next-method))))
(defmethod block-element-p-aux ((buffer lisp-mode) element)
(if (equal (tag-name element) "div")
(not (class-p element "list" "comment"))
(call-next-method)))
(defgeneric sexp-parent-p (buffer node)
(:method ((buffer lisp-mode) node)
(or (class-p node "list") (tag-name-p node "body"))))
(defvar *current-level-in-print* 0)
(defgeneric print-dom (object &key &allow-other-keys))
(defun compute-operator (node)
(when (eql (first-child (parent node)) node)
""))
(defun compute-symbol (node)
(let ((name (nth-value 1 (parse-prefix (text-content node)))))
(if (plusp (length name))
(ignore-errors (swank::parse-symbol (text-content node)))
'ghost-symbol)))
(defun compute-symbol-type (node)
(when-let (symbol (compute-symbol node))
(cond ((eql symbol 'ghost-symbol) "ghost")
((or (and (special-operator-p symbol)
(not (member symbol '(function))))
(member symbol '(declare)))
"special-operator")
((macro-function symbol) "macro")
((keywordp symbol) "keyword")
(t ""))))
(defmethod revert-buffer-aux :before ((buffer lisp-mode))
(when (sexp-parent-p buffer (document-root buffer))
(setf (attribute (document-root buffer) 'keymap)
*sexp-node-keymap*)))
(defmethod on-node-setup progn ((buffer lisp-mode) (node element))
(with-post-command (node 'parent)
(let ((parent (parent node)))
(cond
((symbol-node-p parent)
(let ((prev (previous-sibling node))
(next (next-sibling node)))
(cond ((and (not prev) (not next))
(raise-node node))
((not prev)
(move-nodes node (pos-right node)
parent))
((not next)
(move-nodes node nil (pos-right parent)))
(t (let ((second-symbol (split-node node)))
(move-nodes node (pos-right node)
second-symbol))))))
((class-p parent "comment" "string")
(replace-node
node
(with-output-to-string (s)
(write-dom-aux buffer node s)))))))
(when (symbol-node-p node)
(set-attribute-function node "symbol-type" 'compute-symbol-type)
(set-attribute-function node "operator" 'compute-operator))
(when (class-p node "comment" "string")
(setf (attribute node 'keymap) *plaintext-node-keymap*))
(when (class-p node "object")
(setf (attribute node 'read-only) t)
(setf (attribute node 'keymap) *object-presentation-keymap*)))
(defmethod on-node-setup progn ((buffer lisp-mode) (node text-node))
(with-post-command (node 'parent)
(let ((parent (parent node)))
(when (sexp-parent-p buffer parent)
(let ((nodes (read-dom-from-string (text node))))
;; Assume NODES are non-empty and does not contain text-nodes
(apply #'insert-nodes (or (next-sibling node)
(end-pos parent))
nodes)
(delete-nodes (text-pos node 0) (car nodes)))))))
(defun make-list-node (children)
(lret ((node (make-instance 'element :tag-name "div")))
(setf (attribute node "class") "list")
(iter (for c in children)
(append-child node c))))
(defun make-atom-node (class text)
(lret ((node (make-instance 'element :tag-name "span")))
(setf (attribute node "class") class)
(when (plusp (length text))
(append-child node (make-instance 'text-node :text text)))))
(defun make-presentation-node (text object)
(lret ((node (make-atom-node "object" text)))
(setf (attribute node 'presentation) object)))
(defun list-node-p (node)
(class-p node "list"))
(defun atom-node-p (node)
(class-p node "symbol" "string" "object" "comment"))
(defun sexp-node-p (node)
(class-p node "list" "symbol" "string" "object"))
(defun symbol-node-p (node)
(class-p node "symbol"))
(defun non-empty-symbol-node-p (node)
(and (symbol-node-p node) (first-child node)))
(defmethod print-dom ((cons cons) &key)
(if (and *print-level*
(>= *current-level-in-print* *print-level*))
(make-presentation-node "#" cons)
(let ((*current-level-in-print*
(1+ *current-level-in-print*)))
(make-list-node
(iter (for i from 0)
(for tail first cons then (cdr tail))
(typecase tail
(cons
(if (and *print-length* (>= i *print-length*))
(progn
(collect (make-presentation-node "..." tail))
(finish))
(collect (print-dom (car tail)))))
(null)
(t (collect (make-atom-node "symbol" "."))
(collect (print-dom tail))))
(while (consp tail)))))))
(defmethod print-dom ((null null) &key)
(make-list-node nil))
(defmethod print-dom ((symbol (eql '%br)) &key)
(make-new-line-node))
(defmethod print-dom ((symbol symbol) &key)
(make-atom-node "symbol"
(let ((*print-case* :downcase))
(prin1-to-string symbol))))
(defmethod print-dom ((string string) &key)
(make-atom-node "string" string))
(defmethod print-dom ((obj number) &key)
(make-atom-node "symbol" (prin1-to-string obj)))
(defmethod print-dom ((obj character) &key)
(make-atom-node "symbol" (prin1-to-string obj)))
(defmethod print-dom ((obj t) &key)
;; Turn of CL's pretty printer because it introduces
;; unnecessary line breaks
(let (*print-pretty*)
(make-presentation-node (prin1-to-string obj) obj)))
(define-command open-paren :mode lisp-mode
(&optional (marker (focus)))
(let ((node (make-list-node nil)))
(insert-nodes marker node)
(setf (pos marker) (end-pos node))))
(define-command wrap-paren :mode lisp-mode
(&optional (pos (focus)))
(let ((node (make-list-node nil)))
(setq pos (or (pos-up-ensure pos #'sexp-node-p)
(error 'top-of-subtree)))
(wrap-node pos node)))
(define-command open-string :mode lisp-mode
(&optional (marker (focus)))
(let ((node (make-atom-node "string" "")))
(insert-nodes marker node)
(setf (pos marker) (end-pos node))))
(define-command open-space :mode lisp-mode
(&optional (marker (focus)))
(if (class-p (node-containing marker) "symbol")
(setf (pos marker) (pos-down (split-node marker)))
(let ((node (make-atom-node "symbol" "")))
(insert-nodes marker node)
(setf (pos marker) (end-pos node)))))
(define-command open-comment :mode lisp-mode
(&optional (marker (focus)))
(labels ((cycle-level (n)
(lret ((n (1+ (mod n 4))))
(message "Comment Level -> ~a" n))))
(if-let
(node (find-if (alex:rcurry #'class-p "comment")
(list (pos marker)
(pos-prev marker))))
(setf (attribute node "comment-level")
(prin1-to-string
(cycle-level
(parse-number:parse-number
(attribute node "comment-level")))))
(let ((node (make-atom-node "comment" "")))
(setf (attribute node "comment-level") "1")
(insert-nodes marker node)
(setf (pos marker) (end-pos node))))))
(define-command wrap-comment :mode lisp-mode
(&optional (pos (focus)))
(let ((node (make-atom-node "comment" "")))
(setq pos (or (pos-up-ensure pos #'sexp-node-p)
(error 'top-of-subtree)))
(wrap-node pos node)))
(define-command lisp-raise :mode lisp-mode
(&optional (pos (focus)))
(setq pos (or (pos-up-ensure pos #'sexp-node-p)
(error 'top-of-subtree)))
(raise-node pos))
(define-command lisp-splice :mode lisp-mode
(&optional (pos (focus)))
(setq pos (or (pos-up-until pos #'list-node-p)
(error 'top-of-subtree)))
(splice-node pos))
;;; DOM to Sexp parser
(defvar *form-node-table* nil
"Hash table that map forms to node that generates them.")
(defun parse-prefix (string)
"Parse prefix from STRING.
Return a list of wrapper functions and the rest of STRING. The list
of wrapper functions can be applied to some Lisp object one-by-one and
reproduce the effect of parsed prefix."
(let (wrappers (i 0))
(iter (while (< i (length string)))
(case (aref string i)
((#\')
(incf i)
(push (lambda (next) (list 'quote next)) wrappers))
((#\`)
(incf i)
(push (lambda (next) (list 'sb-int:quasiquote next)) wrappers))
((#\,)
(incf i)
(case (when (< i (length string))
(aref string i))
((#\@)
(incf i)
(push (lambda (next) (sb-int:unquote next 2)) wrappers))
(t
(push (lambda (next) (sb-int:unquote next 0)) wrappers))))
((#\#)
(incf i)
(case (when (< i (length string))
(aref string i))
((#\')
(incf i)
(push (lambda (next) (list 'function next)) wrappers))
((#\p)
(incf i)
(push (lambda (next) (pathname next)) wrappers))
((nil)
(push (lambda (next) (apply #'vector next)) wrappers))
(t (decf i) (return))))
(t (return))))
(values wrappers (subseq string i))))
(defun ghost-symbol-p (node)
"Test NODE is a symbol node with only prefix.
These symbol nodes do not correspond to Sexp symbols, instead act on
the following node."
(when (symbol-node-p node)
(bind (((:values wrappers rest)
(parse-prefix (text-content node))))
(when (zerop (length rest))
wrappers))))
(defun node-to-sexp (node &optional (intern t))
"Parse DOM NODE as a Lisp object.
It also takes into account any prefix preceding NODE.
If INTERN is t, this function interns symbol; otherwise, symbols not
found are replaced with a dummy symbol."
(labels ((apply-wrappers (wrappers node)
(iter (for w in wrappers)
(setq node (funcall w node)))
node)
(dot-p (node)
(and (symbol-node-p node)
(equal (text-content node) ".")))
(process (node)
(let ((sexp
(cond ((list-node-p node)
(let ((children (sexp-children node)))
(if (dot-p (car (last children 2)))
(nconc
(mapcar #'process (butlast children 2))
(process (lastcar children)))
(mapcar #'process children))))
((equal "string" (attribute node "class"))
(text-content node))
((symbol-node-p node)
(bind (((:values wrappers rest)
(parse-prefix (text-content node))))
(apply-wrappers
wrappers
(if intern
(read-from-string rest)
(or (find-symbol
;; TODO: handle *readtable-case*
(string-upcase rest)
*package*)
'dummy)))))
((new-line-node-p node) nil)
((class-p node "object")
(or (attribute node 'presentation)
(error "Presentation attribute missing from ~a" node)))
(t (error "Unrecognized DOM node: ~a" node)))))
(when *form-node-table*
(setf (gethash sexp *form-node-table*)
(print node)))
(if-let (wrappers (ghost-symbol-p (previous-sibling node)))
(apply-wrappers wrappers sexp)
sexp))))
(process node)))
(defgeneric current-package-aux (buffer pos)
(:method ((buffer buffer) (pos t))
(find-package "NEOMACS-USER"))
(:method ((buffer lisp-mode) pos)
(with-marker (marker pos)
(or (find-package
(handler-case
(iter (beginning-of-defun marker)
(for node = (node-after marker))
(for c = (sexp-children node))
(when (and
(car c)
(equal "IN-PACKAGE"
(string-upcase (text-content (car c)))))
(when-let (name (cadr c))
(return (string-upcase
(str:trim (text-content name)
:char-bag "#:"))))))
(motion-error ())))
(call-next-method)))))
(defun current-package (&optional (marker-or-pos (focus)))
"Return the package in the context of MARKER-OR-POS.
This is determined by searching for a `in-package' top-level form
before MARKER-OR-POS."
(let ((pos (resolve-marker marker-or-pos)))
(current-package-aux (host pos) pos)))
;;; Compiler notes
(defvar *compilation-buffer* nil
"The buffer for outputting compilation notes.")
(defvar *compilation-document-root* nil
"Document root of the buffer being compiled.
Used for resolving source-path to DOM node.")
(defvar *compilation-single-form* nil
"If t, the first element in source-path should always be 0 and is ignored.")
(defun evaluate-feature-expression-node (node)
(let ((*package* (find-package :keyword)))
(handler-bind ((warning #'muffle-warning))
(ignore-errors
(if (stringp node)
(sb-int:featurep
(find-symbol (string-upcase node) *package*))
(sb-int:featurep (node-to-sexp node nil)))))))
(defun handle-feature-expressions (nodes)
(iter
(while nodes)
(for n = (car nodes))
(cond
((and (symbol-node-p n)
(sera:string-prefix-p "#+" (text-content n)))
(if (evaluate-feature-expression-node
(if (> (length (text-content n)) 2)
(subseq (text-content n) 2)
(prog1 (cadr nodes)
(setq nodes (cdr nodes)))))
(setq nodes (cdr nodes))
(setq nodes (cddr nodes))))
((and (symbol-node-p n)
(sera:string-prefix-p "#-" (text-content n)))
(if (evaluate-feature-expression-node
(if (> (length (text-content n)) 2)
(subseq (text-content n) 2)
(prog1 (cadr nodes)
(setq nodes (cdr nodes)))))
(setq nodes (cddr nodes))
(setq nodes (cdr nodes))))
(t (collect n)
(setq nodes (cdr nodes))))))
(defun sexp-children (node)
(handle-feature-expressions
(remove-if
(lambda (node)
(or (not (sexp-node-p node))
(ghost-symbol-p node)))
(child-nodes node))))
(defun sexp-nth-child (node n)
(nth n (sexp-children node)))
(defun sexp-child-number (node)
(position node (sexp-children (parent node))))
(defun find-node-for-form-path (root path)
(iter (with node = root)
(for i in path)
(when-let (child (sexp-nth-child node i))
(setq node child))
(finally (return node))))
(defun find-node-for-compiler-note (context)
(or
(when-let (form (sb-c::compiler-error-context-original-form
context))
(gethash form *form-node-table*))
(when-let (path (reverse (sb-c::compiler-error-context-original-source-path context)))
(when *compilation-single-form*
(unless (eql (car path) 0)
(warn "Compiling single form but source path ~a does not start with 0." path))
(pop path))
(find-node-for-form-path
*compilation-document-root* path))))
(defun handle-notification-condition (condition)
(let (id)
(with-current-buffer *compilation-buffer*
(let ((*inhibit-read-only* t)
(node (make-element
"p" :children
(list (princ-to-string condition)))))
(insert-nodes (focus) node)
(setq id (attribute node "neomacs-identifier"))))
(when-let* ((context (sb-c::find-error-context nil))
(node (find-node-for-compiler-note context)))
(setf (attribute node "compiler-note-severity")
(typecase condition
(sb-ext:compiler-note "note")
(sb-c:compiler-error "error")
(error "error")
(style-warning "style-warning")
(warning "warning")
(t "error")))
(setf (attribute node "compiler-note-id") id))))
(define-mode compilation-buffer-mode (read-only-mode)
((for-buffer :initform nil :initarg :buffer)))
(defmacro with-collecting-notes (() &body body)
`(let ((*form-node-table* (make-hash-table))
(*compilation-buffer*
(get-buffer-create "*compilation*"
:mode 'compilation-buffer-mode)))
(clear-compiler-notes)
(setf (for-buffer *compilation-buffer*) (current-buffer))
(with-current-buffer *compilation-buffer*
(let ((*inhibit-read-only* t))
(erase-buffer)))
(handler-bind
((sb-c:fatal-compiler-error #'handle-notification-condition)
(sb-c:compiler-error #'handle-notification-condition)
(sb-ext:compiler-note #'handle-notification-condition)
(error #'handle-notification-condition)
(warning #'handle-notification-condition))
,@body)))
(defun clear-compiler-notes ()
(do-elements
(lambda (node)
(when (attribute node "compiler-note-severity")
(setf (attribute node "compiler-note-severity")
nil)))
(document-root (current-buffer))))
(defun goto-compiler-note (id)
(when-let (buffer (get-buffer "*compilation*"))
(if (eql (current-buffer) (for-buffer buffer))
(with-current-buffer buffer
(do-elements
(lambda (node)
(when (equal (attribute node "neomacs-identifier")
id)
(setf (pos (focus)) node)
(return-from goto-compiler-note)))
(document-root buffer)))
(message "Compilation output has been overwrite by other buffer"))))
(define-command previous-compiler-note (&optional (marker (focus)))
"Move to next compiler note."
(if-let (pos
(npos-prev-until
(pos marker)
(lambda (node)
(and (element-p node)
(attribute node "compiler-note-id")))))
(progn
(setf (pos marker) pos)
(goto-compiler-note (attribute pos "compiler-note-id")))
(user-error "No previous compiler note")))
(define-command next-compiler-note (&optional (marker (focus)))
"Move to previous compiler note."
(if-let (pos
(npos-next-until
(pos marker)
(lambda (node)
(and (element-p node)
(attribute node "compiler-note-id")))))
(progn
(setf (pos marker) pos)
(goto-compiler-note (attribute pos "compiler-note-id")))
(user-error "No next compiler note")))
;;; Eval/compile commands
(define-command eval-defun :mode lisp-mode ()
"Evaluate the surrounding top-level form.
Echo the result."
(with-marker (marker (focus))
(beginning-of-defun marker)
(let* ((node (pos marker))
(*package* (current-package marker))
(result (eval (node-to-sexp node))))
(message "=> ~a" result))))
(define-command compile-defun :mode lisp-mode ()
"Compile the surrounding top-level form.
Highlights compiler notes."
(with-marker (marker (focus))
(beginning-of-defun marker)
(let ((cookie (uuid:format-as-urn nil (uuid:make-v4-uuid)))
(node (pos marker)))
(setf (attribute node 'compile-cookie) cookie)
(with-collecting-notes ()
(let* ((*package* (current-package marker))
(*compilation-single-form* t)
(*compilation-document-root* node)
(input-file
(make-pathname
:name cookie :type "lisp"
:defaults (uiop:temporary-directory)))
output-file)
(with-compilation-unit
(:source-plist
(list :neomacs-buffer (name (current-buffer))
:neomacs-compile-cookie cookie))
(with-open-file (s input-file
:direction :output
:if-exists :supersede)
(write-dom-aux (current-buffer) node s))
(unwind-protect
(progn
(load (setq output-file (compile-file input-file)))
(message "Compiled and loaded"))
(uiop:delete-file-if-exists input-file)
(uiop:delete-file-if-exists output-file)))
(unless (frame-root *compilation-buffer*)
(when (first-child (document-root *compilation-buffer*))
(display-buffer *compilation-buffer*))))))))
(defun last-expression (pos)
(setq pos (resolve-marker pos))
(when (and (end-pos-p pos)
(symbol-node-p (end-pos-node pos)))
(return-from last-expression (end-pos-node pos)))
(iter
(unless pos (error 'beginning-of-subtree))
(for node = (node-before pos))
(until (sexp-node-p node))
(setq pos (npos-prev pos)))
(node-before pos))
(define-command eval-last-expression :mode lisp-mode
(&optional (marker (focus)))
"Evaluate the last expression and echo the result."
(let* ((*package* (current-package marker))
(result (eval (node-to-sexp (last-expression (pos marker))))))
(message "=> ~S" result)))
(define-command eval-print-last-expression :mode lisp-mode
(&optional (marker (focus)))
"Evaluate the last expression and insert result into current buffer."
(let* ((*package* (current-package marker))
(node (last-expression marker))
(pos (pos-right node))
(last-line (make-new-line-node)))
(if (new-line-node-p (node-after pos))
(setf pos (pos-right pos))
(insert-nodes pos (make-new-line-node)))
(insert-nodes pos (print-dom (eval (node-to-sexp node)))
last-line)
(setf (pos marker) (pos-right last-line))))
(define-command lisp-compile-file :mode lisp-mode ()
"Compile current file.
Highlight compiler notes."
(when (modified (current-buffer))
(when (read-yes-or-no "Save file? ")
(save-buffer)))
(let ((*compilation-document-root*
(document-root (current-buffer))))
(with-collecting-notes ()
(iter (with i = 0)
(for c in (child-nodes *compilation-document-root*))
(when (sexp-node-p c)
(setf (attribute c 'tlf-number) i)
(incf i)))
(load (compile-file (file-path (current-buffer))))
(message "Compiled and loaded"))))
;;; Xref
(defparameter *definition-types*
'(:variable :constant :type :symbol-macro :macro
:compiler-macro :function :generic-function :method
:setf-expander :structure :condition :class
:method-combination :package :transform :optimizer
:vop :source-transform :ir1-convert :declaration
:alien-type)
"SB-INTROSPECT definition types.")
(defun find-node-for-source
(pathname tlf-number form-number plist)
(when-let (buf (or (get-buffer (getf plist :neomacs-buffer))
(find-file-buffer pathname)))
(let* ((candidate-nodes
(if-let (c (getf plist :neomacs-compile-cookie))
(remove-if-not
(lambda (n)
(equal c (attribute n 'compile-cookie)))
(child-nodes (document-root (current-buffer))))
(sexp-children (document-root (current-buffer)))))
(tlf (or (find tlf-number candidate-nodes
:key (alex:rcurry #'attribute 'tlf-number))
(nth tlf-number candidate-nodes)))
(translation
(sb-di::form-number-translations
(node-to-sexp tlf) 0))
(form-path
(if (< form-number (length translation))
(reverse
(cdr (aref translation form-number)))
(progn
(message "Inconsistent form-number translation")
nil))))
(pop form-path)
(find-node-for-form-path tlf form-path))))
(defvar *relocated-source-list*
#.(cons 'list
(mapcar #'uiop:truenamize
'(#P"~/quicklisp/dists/ultralisp/software/"
#P"~/quicklisp/dists/quicklisp/software/"
#P"~/quicklisp/local-projects/"))))
(defun translate-relocated-source (pathname)
(when (or (not ceramic.runtime:*releasep*)
(uiop:file-exists-p pathname))
(return-from translate-relocated-source pathname))
(iter (for from in *relocated-source-list*)
(with to = (ceramic.runtime:executable-relative-pathname
#P"src/"))
(multiple-value-bind (p rest)
(alex:starts-with-subseq
(pathname-directory from)
(pathname-directory pathname)
:test 'equal :return-suffix t)
(when p
(return (make-pathname
:directory
(append (pathname-directory to) rest)
:defaults pathname))))
(finally (return pathname))))
(defun visit-source (pathname tlf-number form-number plist)
(with-current-buffer
(or (get-buffer (getf plist :neomacs-buffer))
(when pathname (find-file (translate-relocated-source pathname)))
(user-error "Unknown source location"))
(setf (pos (focus))
(find-node-for-source
pathname tlf-number form-number plist))))
(defun visit-definition (definition)
"Switch to a buffer displaying DEFINITION.
DEFINITION should be a `sb-introspect:definition-source'."
(visit-source
(sb-introspect:definition-source-pathname definition)
(car (sb-introspect:definition-source-form-path definition))
(sb-introspect:definition-source-form-number definition)
(sb-introspect:definition-source-plist definition)))
(define-mode xref-list-mode (list-mode)
((for-symbol :initform (alex:required-argument :symbol)
:initarg :symbol)))
(define-keys xref-list-mode
"q" 'quit-buffer
"enter" 'xref-list-goto-definition)
(defun render-xref-definition (symbol type def)
(attach-presentation
(make-element
"tr"
:children
(list (make-element
"td" :children
(list (prin1-to-string type)))
(make-element
"td" :children
(list (print-arglist
(sb-introspect::definition-source-description def)
(symbol-package symbol))))
(make-element
"td" :children
(list (princ-to-string
(translate-relocated-source
(sb-introspect::definition-source-pathname def)))))))
def))
(defmethod generate-rows ((buffer xref-list-mode))
(let ((*print-case* :downcase)
(symbol (for-symbol buffer)))
(iter (for (type def) in (find-definitions symbol))
(insert-nodes
(focus) (render-xref-definition symbol type def)))))
(defun find-definitions (symbol &optional (types *definition-types*))
"Find all definitions for SYMBOL.
Return a list with items of the form `(definition-type
sb-introspect:definition-source)'."
(iter (for type in types)
(appending
(mapcar (alex:curry #'list type)
(sb-introspect:find-definition-sources-by-name
symbol type)))))
(define-command xref-list-goto-definition
:mode xref-list-mode ()
(visit-definition
(presentation-at (focus) 'sb-introspect:definition-source t))
(quit-buffer))
(define-command goto-definition
:mode lisp-mode ()
"Goto definition of symbol under focus."
(if-let (node (symbol-around (focus)))
(let ((name (nth-value 1 (parse-prefix (text-content node)))))
(when (> (length name) 0)
(let ((*package* (current-package)))
(if-let (symbol (swank::parse-symbol name (current-package)))
(let ((definitions (find-definitions symbol)))
(case (length definitions)
(0 (user-error "No known definition for ~a" symbol))
(1 (push-global-marker)
(visit-definition (cadr (car definitions))))
(t (push-global-marker)
(pop-to-buffer
(make-buffer
"*xref*" :mode 'xref-list-mode
:symbol symbol :revert t)))))
(user-error "No symbol named ~a" name)))))
(user-error "No symbol under focus")))
;;; Auto-completion
(defun atom-around (pos)
(let ((parent (node-containing pos))
(before (node-before pos))
(after (node-after pos)))
(cond ((atom-node-p parent) parent)
((atom-node-p before) before)
((atom-node-p after) after))))
(defun symbol-around (pos)
(when-let (node (atom-around pos))
(when (equal "symbol" (attribute node "class"))
node)))
(defmethod compute-completion ((buffer lisp-mode) pos)
(when-let (node (symbol-around pos))
(let* ((package (current-package pos))
(text (text-content node))
(name (nth-value 1 (parse-prefix text)))
(swank::*buffer-package* package)
(completions (car (swank:fuzzy-completions
name package
:limit (completion-limit buffer)))))
(values
(range (text-pos (first-child node) (- (length text) (length name)))
(pos-down-last node))
(iter (for c in completions)
(collect (list (car c) (remove #\- (lastcar c)))))))))
;;; Autodoc
(define-mode autodoc-mode () ()
(:documentation
"Show documentation of form around focus.")
(:toggler t))
(defun format-swank-highlighted-arglist (string)
(let ((match (ppcre:all-matches "===>.*<===" string)))
(if match
(list (subseq string 0 (car match))
(make-element
"span" :class "focus-arg" :children
(list (str:trim (subseq string
(+ (car match) 4)
(- (cadr match) 4)))))
(subseq string (cadr match)))
(list string))))
(defun compute-autodoc (pos)
(let ((*package* (current-package pos)))
(let (form form-path operator arglist)
(setq pos (or (pos-up-ensure pos #'sexp-node-p)
(return-from compute-autodoc nil)))
(iter
(for last first nil then cur)
(for cur first pos then (pos-up cur))
(while (sexp-node-p cur))
(when last
(push (or (sexp-child-number last)
(return-from compute-autodoc))
form-path))
(for operator-node = (first-child cur))
(for symbol = (when (symbol-node-p operator-node)
(compute-symbol operator-node)))
(when (fboundp symbol)
(multiple-value-bind (l unavailable)
(sb-introspect:function-lambda-list symbol)
(unless unavailable
(setq arglist l operator symbol
form (ignore-errors (node-to-sexp cur nil)))
(return)))))
(when operator
(let ((arglist (swank::decode-arglist arglist)))
(append
(format-swank-highlighted-arglist
(swank::decoded-arglist-to-string
arglist
:operator operator
:highlight (swank::form-path-to-arglist-path form-path form arglist)))
(when (documentation operator 'function)
(list ": "
(make-element
"span" :class "docstring"
:children (short-doc operator))))))))))
(defun autodoc-for-thing (symbol)
(let ((doc
(when (documentation symbol 'function)
(list (make-element
"span" :class "docstring"
:children (short-doc symbol))))))
(append
(when (fboundp symbol)
(let ((*print-case* :downcase))
(list
(str:concat
(print-arglist
(cons symbol
(sb-introspect:function-lambda-list
symbol))
(symbol-package symbol))
(when doc ": ")))))
doc)))
(defvar *autodoc-helper* nil)
(defvar *autodoc-delay* 0.2
"Time to wait before displaying autodoc.")
(defun maybe-show-autodoc (buffer)
(sleep *autodoc-delay*)
(unless (sb-concurrency:mailbox-empty-p
(gethash (bt:current-thread) *helper-mailboxes*))
(return-from maybe-show-autodoc))
(with-current-buffer buffer
(when-let* ((frame-root (current-frame-root))
(echo-area (echo-area frame-root)))
(unless (first-child (document-root echo-area))
(let (*message-log-max*)
(if (typep (current-buffer) 'active-completion-mode)
(progn
(pushnew 'echo-area-autodoc (styles echo-area))
(message
(let ((*package* (current-package (focus)))
(row (node-after (focus (completion-buffer (current-buffer))))))
(autodoc-for-thing
(ignore-errors
(swank::parse-symbol
(text-content (first-child row))))))))
(when-let (nodes (compute-autodoc (pos (focus))))
(pushnew 'echo-area-autodoc (styles echo-area))
(message nodes))))))))
(defmethod on-post-command progn ((buffer autodoc-mode))
(when *this-command*
(run-in-helper '*autodoc-helper* 'maybe-show-autodoc buffer)))
;;; Parser
(defun read-symbol (stream c)
(unread-char c stream)
(append-child
*dom-output*
(make-atom-node "symbol" (read-constituent stream 'read-symbol '(#\\)))))
(defun read-string (stream c)