-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrepl-utils.lisp
49 lines (43 loc) · 1.59 KB
/
repl-utils.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
(in-package :sbcli)
(defun last-nested-expr (s/sexp)
"From an input with nested parens (or none), return the most nested
function call (or the first thing at the prompt).
(hello (foo (bar:qux *zz* ?
=>
bar:qux
"
(let* ((input (str:trim s/sexp))
(last-parens-token (first (last (str:split #\( input)))))
(first (str:words last-parens-token))))
#+or(nil)
(progn
(assert (string= "baz:qux"
(last-nested-expr "(hello (foo bar (baz:qux zz ?")))
(assert (string= "baz:qux"
(last-nested-expr "(baz:qux zz ?")))
(assert (string= "qux"
(last-nested-expr "(baz (qux ?")))
(assert (string= "sym"
(last-nested-expr "sym ?"))))
;;;;
;;;; Syntax highlighting if pygments is installed.
;;;;
(defun maybe-highlight (str)
(if *syntax-highlighting*
(let ((pygmentize (or *pygmentize*
(which:which "pygmentize"))))
(when pygmentize
(with-input-from-string (s str)
(let ((proc (uiop:launch-program (alexandria:flatten
(list pygmentize *pygmentize-options*))
:input s
:output :stream)))
(read-line (uiop:process-info-output proc) nil "")))))
str))
(defun syntax-hl ()
(rl:redisplay)
(let ((res (maybe-highlight rl:*line-buffer*)))
(format t "~c[2K~c~a~a~c[~aD" #\esc #\return rl:*display-prompt* res #\esc (- rl:+end+ rl:*point*))
(when (= rl:+end+ rl:*point*)
(format t "~c[1C" #\esc))
(finish-output)))