Hello,

I'm trying to figure out how I would make a text editor or a DrRacket extension 
that circled nested s-expressions instead of displaying the parentheses. 
DrRacket circles text for the find/replace feature, so I looked at that and 
found that it uses the highlight-range method of text:basic<%> in framework.

However, when I tried using it with the #:adjust-on-insert/delete argument as 
#true, I got strange behavior with my little toy editor. 

When I start typing something like "(define (f x)", it circles the (f x) like I 
expected. However, when I continue typing, that's when I get the weird 
behavior. For example, if I type a return, the beginning of the circle jumps 
backwards to the "(define", and the end of the circle jumps down with the 
cursor. Then if I press delete after more typing, that circle disappears and is 
replaced by a narrow ellipse that just follows my cursor around. This weird 
behavior goes away when the #:adjust-on-insert/delete argument is #false.

Is there a way to stay on the positions of the open and close parentheses 
without jumping backwards or collapsing and following the cursor? To do that, 
it needs to shift positions when I insert or delete characters, but is there a 
way to tell it to stay where it is if I insert something right after the close 
parenthesis?

#lang racket/base

(require racket/class racket/gui/base framework)

(define frame
  (new frame% [label "circledit"] [width 500] [height 500]))

(define text-editor
  (new (class racket:text%
         (super-new)
         (inherit get-start-position get-backward-seep)
         ;; when an s-expr is closed, this method will be called
         (define/override (balance-parens key-event)
           (super balance-parens key-event)
           ;; and it will circle the s-expr that was just closed
           (define end
             (get-start-position))
           (define start
             (get-backward-sexp end))
           (when start
             (send text-editor highlight-range
                   start end "dark green" #f 'low 'hollow-ellipse 
                   #:adjust-on-insert/delete? #t))))))

(define canvas
  (new editor-canvas% [parent frame] [editor text-editor]))

(send frame show #true)

Alex Knauth

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to