Basic code editor functionality in Racket -


i'm creating program live coding performance, want basic s-expressions code editor (whose contents input evaled racket code in appropriate syntactical context).

since drracket written in racket, expected recreating text editing functionality of code editor painless, , documented, i've found no guidance. have following code far:

(define frame (new frame% [label "simple edit"]                           [width 800]                           [height 800])) (define canvas (new editor-canvas% [parent frame])) (define text (new text%)) (send canvas set-editor text) (send frame show #t)  (define menu-bar (new menu-bar% [parent frame])) (define edit-menu (new menu% [label "edit"] [parent menu-bar])) (define execution-menu (new menu% [label "execution"] [parent menu-bar])) (new menu-item% [label "run"]                 [parent execution-menu]                 [callback (λ (mi e) (update (send text get-text)))]                 [shortcut #\r]                 [shortcut-prefix '(cmd)]) (append-editor-operation-menu-items edit-menu #f)  (define delta (make-object style-delta% 'change-size 14)) (send delta set-face "menlo") (send text change-style delta) 

with have set font , size agreeable one, , copy , paste operations, etc. work. there's lot of unexpected behaviors, such as:

  • pressing modifier+letter key combinations still inserts letter instead of ignoring it.
  • pressing alt+left or cmd+left (mac user) move caret single character instead of word or margin.
  • double-clicking won't select word.

i don't want reinvent wheel, googled hard no avail, tried looking drracket source code (which complex still limited understanding of language), etc. there doesn't seem explanation on using gui toolkit around either (that isn't reference), , pasted above took me deal of trial-and-error, don't forward implementing of basic text editing stuff hand.

if has project source code exemplifies how done, package has resolved, or pointers me in right track, appreciated!

the functionality editing part (not "run" part) provided racket:text% class framework library.

#lang racket/gui (require framework)  (define frame   (new frame% [label "simple editor"] [width 800] [height 800])) (define text-editor   (new racket:text%)) (define canvas   (new editor-canvas% [parent frame] [editor text-editor]))  (send frame show #true) 

this takes care of syntax highlighting, paren-matching, double-click s-expression, , indentation. code in question start @ adding "run" functionality, because callback function can text when it's supposed run. need function can take piece of text , run it. can use make-module-evaluator racket/sandbox.

(require racket/sandbox)  (define (run-text str)   (define repl-ev     (parameterize ([sandbox-output (current-output-port)]                    [sandbox-error-output (current-error-port)])       (make-module-evaluator str)))   (void)) 

then can use run-text in callback function this:

                [callback (λ (mi e) (run-text (send text-editor get-text)))] 

the way it's set up, running module prints results in drracket's interactions window. want own interactions window that, , i'm not sure how that.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo