Skip to content

Instantly share code, notes, and snippets.

@crvdgc
Last active August 23, 2023 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crvdgc/6ced0f829f2f58efbc02fcee6acd27fc to your computer and use it in GitHub Desktop.
Save crvdgc/6ced0f829f2f58efbc02fcee6acd27fc to your computer and use it in GitHub Desktop.
This keybinding file binds `F4` to execute `Indent all`, `Run`, and keep the focus in the editor. It basically follows [the example](https://docs.racket-lang.org/drracket/Keyboard_Shortcuts.html#%28part._defining-shortcuts%29) in the reference. I find it useful for trying out examples in *The Little Prover*.
#lang s-exp framework/keybinding-lang
(require drracket/tool-lib)
(require racket/gui/base)
(module test racket/base)
(define (call-menu menu-item)
(λ (ed evt)
(define canvas (send ed get-canvas))
(when canvas
(define menu-bar (find-menu-bar canvas))
(when menu-bar
(define item (find-item menu-bar menu-item))
(when item
(define menu-evt
(new control-event%
[event-type 'menu]
[time-stamp
(send evt get-time-stamp)]))
(send item command menu-evt))))))
(define/contract (find-menu-bar c)
(-> (is-a?/c area<%>) (or/c #f (is-a?/c menu-bar%)))
(let loop ([c c])
(cond
[(is-a? c frame%) (send c get-menu-bar)]
[(is-a? c area<%>) (loop (send c get-parent))]
[else #f])))
(define/contract (find-item menu-bar label)
(-> (is-a?/c menu-bar%)
string?
(or/c (is-a?/c selectable-menu-item<%>) #f))
(let loop ([o menu-bar])
(cond
[(is-a? o selectable-menu-item<%>)
(and (equal? (send o get-plain-label) label)
o)]
[(is-a? o menu-item-container<%>)
(for/or ([i (in-list (send o get-items))])
(loop i))]
[else #f])))
(keybinding "f4"
(λ (ed evt)
((call-menu "Reindent All") ed evt)
((call-menu "Run") ed evt)
(send (send ed get-canvas) focus)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment