addons.mdx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ---
  2. title: Built-in Addons
  3. description: Shipped universal and OpenTUI-specific keymap addons
  4. order: 6
  5. navTitle: Built-in Addons
  6. ---
  7. # Built-in keymap addons
  8. `@opentui/keymap` keeps the engine bare. The built-in addon packages install parser stages, field compilers, diagnostics, disambiguation and host-specific helpers on top of the public registration APIs.
  9. If you want to build your own addon, see [Custom Addons](/docs/keymap/custom-addons).
  10. Every addon returns a disposer.
  11. ```ts
  12. import * as addons from "@opentui/keymap/addons"
  13. import * as opentuiAddons from "@opentui/keymap/addons/opentui"
  14. ```
  15. ## Universal addons
  16. ### Defaults
  17. | Export | Description |
  18. | ------------------------------------- | ----------------------------------------------------- |
  19. | `defaultBindingParser` | Shared parser for string key syntax |
  20. | `defaultEventMatchResolver` | Shared event matcher for canonical normalized strokes |
  21. | `registerDefaultBindingParser()` | Appends `defaultBindingParser` |
  22. | `registerDefaultEventMatchResolver()` | Appends `defaultEventMatchResolver` |
  23. | `registerDefaultKeys()` | Installs both default stages together |
  24. The shared default parser accepts single-stroke named keys, modifier chords, literal punctuation, `" "` for space, `"+"` as a literal plus key, `<token>` aliases, and `{pattern}` runtime captures. It parses concatenated multi-stroke sequences such as `dd`, `<leader>s`, or `{count}j`.
  25. Recognized modifier prefixes (case-insensitive, joined with `+`):
  26. | Modifier | Aliases |
  27. | -------- | --------------- |
  28. | `ctrl` | `control` |
  29. | `shift` | - |
  30. | `meta` | `alt`, `option` |
  31. | `super` | - |
  32. | `hyper` | - |
  33. ### Field addons
  34. | Export | Adds |
  35. | ---------------------------- | ---------------------------------------------------------------------------------- |
  36. | `registerBindingOverrides()` | Layer-local `bindingOverrides` field for replacing bindings by string command name |
  37. | `registerEnabledFields()` | Layer and command `enabled` fields |
  38. | `registerMetadataFields()` | Binding `desc` / `group` and command `desc` / `title` / `category` metadata |
  39. | `registerAliasesField()` | Layer-local `aliases` field for remapping single-key binding names |
  40. `registerBindingOverrides()` expects `bindingOverrides` to already be a `Binding[]` array. It rewrites that layer's binding array once at registration time. Matching string commands are replaced by the override binding and unmatched bindings stay in place.
  41. `registerEnabledFields()` accepts `boolean`, `() => boolean`, or `ReactiveMatcher` values. It applies to layers and commands, not bindings. It controls activation only; it does not publish attrs.
  42. `registerMetadataFields()` validates and trims metadata fields before exposing them in projections. Binding `desc` / `group` become binding metadata. Command `desc` / `title` / `category` become command metadata in `commandAttrs`.
  43. Layer metadata is app-specific. Register layer fields directly with `registerLayerFields()` and call `ctx.attr(...)` when graph/debug visualizations need layer labels or grouping metadata.
  44. `registerAliasesField()` adds bindings instead of replacing the originals and aliases stay local to the layer that declared them.
  45. ### Syntax and sequence addons
  46. | Export | Description |
  47. | ---------------------------------------- | ----------------------------------------------------------------------- |
  48. | `registerCommaBindings()` | Expands `x, y` into multiple bindings |
  49. | `registerEmacsBindings()` | Parses Emacs-style spaced chords such as `ctrl+x ctrl+s` |
  50. | `registerLeader()` | Defines a token such as `leader` that `<leader>` syntax expands through |
  51. | `registerModBindings()` | Adds platform-aware `mod+...` bindings using host metadata |
  52. | `registerTimedLeader()` | Defines a leader token and clears it after a timeout |
  53. | `registerBackspacePopsPendingSequence()` | Lets Backspace step back through the current pending sequence |
  54. | `registerEscapeClearsPendingSequence()` | Lets Escape cancel the current pending sequence |
  55. | `registerNeovimDisambiguation()` | Timeout-based exact-vs-prefix disambiguation |
  56. Options:
  57. | API | Options |
  58. | ---------------------------------------- | ------------------------------------------------------------------------------------ |
  59. | `registerLeader()` | `LeaderOptions`: `trigger`, optional semantic `name` (default `"leader"`) |
  60. | `registerTimedLeader()` | `TimedLeaderOptions`: `LeaderOptions` plus `timeoutMs`, `onArm`, `onDisarm` |
  61. | `registerBackspacePopsPendingSequence()` | `BackspacePopsPendingSequenceOptions`: `preventDefault` (default `true`), `priority` |
  62. | `registerEscapeClearsPendingSequence()` | `EscapeClearsPendingSequenceOptions`: `preventDefault` (default `true`), `priority` |
  63. | `registerNeovimDisambiguation()` | `NeovimDisambiguationOptions`: `timeoutMs` (default `300`) |
  64. Register disambiguation addons before registering same-layer exact/prefix bindings such as `g` and `gg`.
  65. Leader `trigger` accepts a key string, key object, binding-like `{ key }`, or a single binding lookup result from `createBindingLookup().get(...)`.
  66. ### Diagnostics
  67. | Export | Description |
  68. | ------------------------------------- | ----------------------------------------------------------------- |
  69. | `registerDeadBindingWarnings()` | Warns when a binding has no command and no reachable continuation |
  70. | `registerUnresolvedCommandWarnings()` | Warns when a string command cannot be resolved |
  71. ## Ex commands
  72. `registerExCommands()` installs ex-command field compilers, a command transformer and a resolver for `:name ...args` strings. Register ex commands through normal keymap layers by using a colon-prefixed command name or `namespace: "excommands"`.
  73. ```ts
  74. import { registerExCommands } from "@opentui/keymap/addons"
  75. registerExCommands(keymap)
  76. keymap.registerLayer({
  77. commands: [
  78. {
  79. name: "write",
  80. namespace: "excommands",
  81. aliases: ["w"],
  82. nargs: "1",
  83. desc: "Write file",
  84. run({ payload }) {
  85. // payload.raw === ":write session.log"
  86. // payload.args === ["session.log"]
  87. },
  88. },
  89. ],
  90. })
  91. ```
  92. `ExCommand` fields:
  93. | Field | Type | Required | Description |
  94. | ------------ | --------------------------------------------------------------------------- | -------- | ---------------------------------------------------------------- |
  95. | `name` | `string` | yes | Command name. `:name` and `name` both normalize to `:name` |
  96. | `aliases` | `string[]` | no | Additional names |
  97. | `nargs` | `"0"`, `"1"`, `"?"`, `"*"`, `"+"` | no | Argument-count validation |
  98. | `run` | `(ctx: CommandContext<TTarget, TEvent, ExCommandPayload>) => CommandResult` | yes | `ctx.payload` contains the trimmed `raw` input and parsed `args` |
  99. | extra fields | `unknown` | no | Added as top-level fields on the registered command |
  100. - Registered ex commands default `namespace` to `excommands`.
  101. - Aliases produce additional registered commands, for example `:write` and `:w`.
  102. - Extra fields are preserved on `getCommands()` / `getCommandEntries()` results and compiled through command-field addons.
  103. - `runCommand(":write file.txt")` and `dispatchCommand(":write file.txt")` both go through the installed resolver.
  104. ## OpenTUI addons
  105. `@opentui/keymap/addons/opentui` re-exports every universal addon and adds the OpenTUI-specific helpers below.
  106. ### `registerBaseLayoutFallback()`
  107. Adds an event match resolver that falls back to `KeyEvent.baseCode` so bindings can ignore active keyboard layout changes when Kitty base-layout reporting is available. Direct stroke matches win before base-layout fallback matches, even across active layers.
  108. ### Edit buffer helpers
  109. | Export | Description |
  110. | ------------------------------------- | ---------------------------------------------------------------------------------------- |
  111. | `createTextareaBindings()` | Returns generated textarea bindings with overrides prepended |
  112. | `registerEditBufferCommands()` | Registers the shared edit-buffer command set against `renderer.currentFocusedEditor` |
  113. | `registerTextareaMappingSuspension()` | Suspends focused `TextareaRenderable` mapped shortcuts while keeping plain typing intact |
  114. | `registerManagedTextareaLayer()` | High-level textarea integration: commands, suspension, default bindings and overrides |
  115. `registerManagedTextareaLayer()` accepts a global `Layer` shape plus optional `bindings`, but it intentionally excludes `target` and `targetMode`. The helper is global and follows `renderer.currentFocusedEditor`.
  116. `EditBufferCommandOptions`:
  117. | Field | Type | Description |
  118. | ------------------ | ------------------------------------------------ | ------------------------------------------------------------------------------- |
  119. | `category` | `string` | Override generated command category, defaulting to `Text Editing` |
  120. | `group` | `string` | Override generated binding group, defaulting to `Text Editing` |
  121. | `includeFineGroup` | `boolean` | Include hard-coded binding `fineGroup` fields for apps that register that field |
  122. | `commandNames` | `Partial<Record<EditBufferCommandName, string>>` | Override generated command names |
  123. | `descriptions` | `Partial<Record<EditBufferCommandName, string>>` | Override generated command descriptions |
  124. `registerEditBufferCommands()` and `registerTextareaMappingSuspension()` are reference-counted per keymap, so multiple integrations can safely share them.
  125. ### `EditBufferCommandName`
  126. | Action | Default command | Default description |
  127. | ------------------------- | ------------------------------- | --------------------------- |
  128. | `move-left` | `input.move.left` | Cursor left |
  129. | `move-right` | `input.move.right` | Cursor right |
  130. | `move-up` | `input.move.up` | Cursor up |
  131. | `move-down` | `input.move.down` | Cursor down |
  132. | `select-left` | `input.select.left` | Select left |
  133. | `select-right` | `input.select.right` | Select right |
  134. | `select-up` | `input.select.up` | Select up |
  135. | `select-down` | `input.select.down` | Select down |
  136. | `line-home` | `input.line.home` | Line start |
  137. | `line-end` | `input.line.end` | Line end |
  138. | `select-line-home` | `input.select.line.home` | Select to line start |
  139. | `select-line-end` | `input.select.line.end` | Select to line end |
  140. | `visual-line-home` | `input.visual.line.home` | Visual line start |
  141. | `visual-line-end` | `input.visual.line.end` | Visual line end |
  142. | `select-visual-line-home` | `input.select.visual.line.home` | Select to visual line start |
  143. | `select-visual-line-end` | `input.select.visual.line.end` | Select to visual line end |
  144. | `buffer-home` | `input.buffer.home` | Buffer start |
  145. | `buffer-end` | `input.buffer.end` | Buffer end |
  146. | `select-buffer-home` | `input.select.buffer.home` | Select to buffer start |
  147. | `select-buffer-end` | `input.select.buffer.end` | Select to buffer end |
  148. | `delete-line` | `input.delete.line` | Delete line |
  149. | `delete-to-line-end` | `input.delete.to.line.end` | Delete to line end |
  150. | `delete-to-line-start` | `input.delete.to.line.start` | Delete to line start |
  151. | `backspace` | `input.backspace` | Delete backward |
  152. | `delete` | `input.delete` | Delete forward |
  153. | `newline` | `input.newline` | New line |
  154. | `undo` | `input.undo` | Undo |
  155. | `redo` | `input.redo` | Redo |
  156. | `word-forward` | `input.word.forward` | Next word |
  157. | `word-backward` | `input.word.backward` | Previous word |
  158. | `select-word-forward` | `input.select.word.forward` | Select next word |
  159. | `select-word-backward` | `input.select.word.backward` | Select previous word |
  160. | `delete-word-forward` | `input.delete.word.forward` | Delete next word |
  161. | `delete-word-backward` | `input.delete.word.backward` | Delete previous word |
  162. | `select-all` | `input.select.all` | Select all |
  163. | `submit` | `input.submit` | Submit |
  164. The generated textarea bindings come from the shared `defaultTextareaKeyBindings` in `@opentui/core`. `createTextareaBindings()` prepends overrides before those defaults so custom bindings win by order.
  165. See [Custom Addons](/docs/keymap/custom-addons) for the public addon-authoring APIs, lifecycle rules, callback contexts and extension-point quirks.