DrRacket REPL: Make ctrl-p an alias for ctrl-up

In the DrRacket REPL (interaction window), I can use ctrl-up (up = arrow up) to recall one of the previous inputs. However, recently I've become used to use ctrl-p in Bash to recall previous inputs, because that's easier to type if you touch type. (It's not necessary to reach for the arrow key block.)

The problem is that if I habitually press ctrl-p in the DrRacket interaction window, I get a print dialog, which is somewhat annoying. Since I practically never need printing in DrRacket, I'd like to change the ctrl-p keybinding to be equivalent to ctrl-up. Is this possible, and if yes, how?

I found the "Keybindings" option in the "Edit" dropdown menu and the documentation on DrRacket 3.3 Keyboard Shortcuts , but I don't understand how to apply the information there to my problem.

Quickly: It appears the keymap function name is put-previous-sexp.
To call this method, you need to find the interaction's keymap, then use the keymap API to call by name. I may be able to dig a little more tomorrow.

1 Like

That would be great, but it doesn't have to be right tomorrow if that's difficult for you. :slight_smile:

Quickly again, here's a quickscript that does the job:

#lang racket/base

(require quickscript
         racket/class
         racket/gui/event)

;; Scratchpad to try scripts.

;; Returns a replacement string for the selected string `selection`
;; ("" if no text is selected), or `#f` to leave the selection as is.
(define-script find-keymap
  #:label "keymap"
  #:menu-path ("Scratch")
  (λ (selection #:interactions ints)
    (define km (send ints get-keymap))
    (send km call-function "put-previous-sexp" ints (new event%) #t)
    #f))

I guess you probably want just a keybinding. Unfortunately I was not able to overload the predefined Ctrl-p. (More and more I believe the whole keymap business needs to be rethought, but that's quite some work.)

Thanks for the script.

I've never worked with quickscripts before. It seems I was able to activate it, as I get an entry Scratch/keymap in DrRacket's Script menu. Selecting keymap actually recalls the history, but ...

yes, that's the idea. :slight_smile: If I have to select a menu item to go through the history, I might as well use ctrl-up as before.

Ok, that it (probably) doesn't work is also useful information (in a way).

I haven't tried yet, but maybe via a keybinding it's possible to overload ctl-p.

You should be able to use a custom keybindings file to rebind Ctrl-P via the method described on the shortcuts page. Here's an example file which worked for me:

#lang s-exp framework/keybinding-lang

(define (rebind key command)
  (keybinding
   key
   (λ (ed evt)
     (send (send ed get-keymap) call-function
           command ed evt #t))))

(rebind "c:p" "put-previous-sexp")

The rebind definition above is the same as the one given on the documentation page.

It does take a bit of trial and error to work out what's going on when customising keys in this way... I suspect the documentation and / or DrRacket could be improved to make it easier to achieve quick customisations like this.

2 Likes

Haven't tested on Windows, but does esc-p work out of the box?

[Esc followed by p -- not esc and p simultaneously.]

[Esc] followed by [p] works for me on Windows and macOS, and is documented/expected behaviour: 1.6 The Interactions Window

keybindings has the following for put-previous-sexp
On windows

  • m:~g:p
  • esc;p
  • c:up

on macOS

  • esc;p
  • c:up

where for me c is the control key on macOS and Windows, and m is the Alt key to the left of the spacebar on my windows laptop.

In the above notation;

  • : indicates the subsequent is pressed while the former is held down
  • ; indicates a sequence of key presses

Does anyone know what keying behaviour ~ indicates?

Fun fact I did not realise - the keybindings window changes what it shows you depending if you have the cursor in definitions or interactions.

Interactions left


definitions right

The page 3.3 Keyboard Shortcuts uses a different notation for when 'enable keybindings in menus' is unticked:

Capitalised 'C' for control, 'M' for meta, ESC, etc.
I believe

  • - indicates the subsequent is pressed while the former is held down
  • (space) indicates a sequence of key presses

The keybindings window changes - no 'Print Definitions' keybinding when definitions is selected?

Sorry for taking you with me on this journey down the keybinding rabbit hole.

Stephen :beetle:

1 Like