Skip to content

Commit

Permalink
add highlight.lisp
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxxr committed Nov 19, 2023
1 parent e32c590 commit 1b83278
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
71 changes: 71 additions & 0 deletions extensions/lisp-mode/highlight.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
(defpackage :lem-lisp-mode/highlight
(:use :cl :lem :lem-lisp-mode/internal))
(in-package :lem-lisp-mode/highlight)

(defvar *timer*)

(define-attribute highlight-attribute
(t :underline t :foreground "cyan"))

(define-overlay-accessors highlight-overlays
:clear-function clear-highlight-overlays
:add-function add-highlight-overlays)

(defun toplevel-form-p (point)
(start-line-p point))

(defun compute-path-at-point (point)
(with-point ((point point))
(skip-chars-backward point #'syntax-symbol-char-p)
(loop :collect (loop :while (form-offset point -1) :count t)
:until (or (null (backward-up-list point t))
(toplevel-form-p point)))))

(defun form-string-at-point (point)
(with-point ((start point)
(end point))
(loop :while (backward-up-list start t))
(loop :while (forward-up-list end t))
(points-to-string start end)))

(defun move-path (point path)
(loop :for n :in (reverse path)
:do (forward-down-list point t)
(form-offset point n))
(skip-whitespace-forward point))

(defun highlight-symbol (point)
(with-point ((start point)
(end point))
(form-offset end 1)
(add-highlight-overlays (point-buffer point) (make-overlay start end 'highlight-attribute))))

(define-command lisp-highlight () ()
(clear-highlight-overlays (current-buffer))
(lisp-eval-async `(micros/walker:highlight ,(form-string-at-point (current-point))
',(compute-path-at-point (current-point))
,(buffer-package (current-buffer)))
(lambda (result)
(alexandria:destructuring-ecase result
((:read-error message)
(declare (ignore message)))
((:error message)
(log:error message))
((:ok paths)
(with-point ((start (current-point)))
(loop :while (backward-up-list start t))
(dolist (path paths)
(with-point ((point start))
(move-path point path)
(highlight-symbol point)))))))))

(defun init-highlight-timer ()
(let ((timer (make-idle-timer 'lisp-highlight :name "lisp-show"
:handle-function 'stop-highlight-timer)))
(setf *timer* timer)
(start-timer timer 100 t)))

(defun stop-highlight-timer ()
(when *timer*
(stop-timer *timer*)
(setf *timer* nil)))
1 change: 1 addition & 0 deletions extensions/lisp-mode/lem-lisp-mode.asd
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
(:file "class-browser")
(:file "macroexpand")
(:file "test-runner")
(:file "highlight")
(:file "package")))

(defsystem "lem-lisp-mode/v2"
Expand Down

0 comments on commit 1b83278

Please sign in to comment.