Add custom keybindings API to CodeMirror editor#6000
Draft
Jepson2k wants to merge 1 commit intozauberzeug:mainfrom
Draft
Add custom keybindings API to CodeMirror editor#6000Jepson2k wants to merge 1 commit intozauberzeug:mainfrom
Jepson2k wants to merge 1 commit intozauberzeug:mainfrom
Conversation
Surfaces CodeMirror 6's keymap as a Python-facing dict — `keybindings={key: handler}`
in the constructor plus `on_keybinding(key, handler)` and `remove_keybinding(key)` for
runtime updates. Keys follow CM6's keymap syntax (`Mod-s`, `F5`, `Mod-Shift-d`, etc.).
A new `ui.codemirror.binding(callback, ...)` factory wraps callbacks with per-binding
overrides: `prevent_default=False` (notify Python while letting the browser default
fire — e.g. for `Mod-c`) and `mac=`/`linux=`/`win=` for per-platform shortcuts. The
factory keeps constructor and method surfaces shape-identical (both accept either a
bare callable or the wrapped spec).
JS side wires user bindings into a `Compartment`-backed keymap wrapped in
`Prec.high(...)` so they win over `basicSetup`'s defaults. The dispatcher emits a
single `keybinding` event with the matched key; Python looks the handler up by key
and calls it. The `run` callback returns `prevent_default` so opting out cleanly
forwards the event to lower-precedence bindings and the browser's native handler.
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
ui.codemirrorexposes no way to map keystrokes to Python callbacks today. Common in-app editor shortcuts —Mod-sto save,Mod-rto run,F5to refresh, plus things likeMod-cwhere you want to react without overriding the browser's native copy — currently require forking the JS or wiring up DOM-level keyboard listeners that fight CodeMirror's own keymap.Implementation
Adds:
keybindings={key: handler}constructor parameter and matchingeditor.on_keybinding(key, handler)/editor.remove_keybinding(key)for runtime updates. Keys follow CodeMirror's keymap syntax —Mod-s,F5,Mod-Shift-d, etc. (Modis Cmd on macOS, Ctrl elsewhere).ui.codemirror.binding(callback, ...)factory for per-binding overrides:prevent_default=False(notify Python while letting the browser default fire — e.g. forMod-c) and per-platformmac=/linux=/win=shortcut overrides. The factory keeps the constructor and method surfaces shape-identical (both accept either a bare callable or the wrapped spec).JS side wires user bindings into a
Compartment-backedkeymap, wrapped inCM.Prec.high(...)so they win overbasicSetup's defaults (e.g. you can overrideMod-z). The dispatcher emits a singlekeybindingevent with the matched key; Python looks the handler up by key and calls it. Theruncallback returnsprevent_default, so opting out cleanly forwards the event to lower-precedence bindings and the browser's native handling —Mod-cnotifies Python and the browser still copies.Progress