Skip to content

Commit

Permalink
pkg/edit: Press Ctrl-Alt-V to insert the name of the next key.
Browse files Browse the repository at this point in the history
Implemented with an unstable builtin without an accompanying test for now, due
to the ongoing off-branch TUI rewrite.

This fixes #1180.
  • Loading branch information
xiaq committed Dec 30, 2024
1 parent aa76ef4 commit 0b62bf4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions 0.22.0-release-notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Notable new features

- Pressing `Ctrl-Alt-V` in the REPL now enters a mode where the name of the
next key pressed will be inserted into the buffer.

# Notable bugfixes

- The `lower` glob modifier (as in `echo *[lower]`) now correctly matches
Expand Down
28 changes: 26 additions & 2 deletions pkg/edit/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ func insertRaw(app cli.App, tty cli.TTY) {
app.PushAddon(w)
}

func insertKeyName(app cli.App) {
codeArea, ok := focusedCodeArea(app)
if !ok {
return
}
w := modes.NewStub(modes.StubSpec{
Bindings: tk.FuncBindings(func(w tk.Widget, event term.Event) bool {
switch event := event.(type) {
case term.KeyEvent:
codeArea.MutateState(func(s *tk.CodeAreaState) {
s.Buffer.InsertAtDot(ui.Key(event).String())
})
app.PopAddon()
return true
default:
return false
}
}),
Name: " KEY NAME ",
})
app.PushAddon(w)
}

var errMustBeKeyOrString = errors.New("must be key or string")

func toKey(v any) (ui.Key, error) {
Expand Down Expand Up @@ -143,8 +166,9 @@ func wordify(fm *eval.Frame, code string) error {

func initTTYBuiltins(app cli.App, tty cli.TTY, nb eval.NsBuilder) {
nb.AddGoFns(map[string]any{
"insert-raw": func() { insertRaw(app, tty) },
"clear": func() { clear(app, tty) },
"insert-raw": func() { insertRaw(app, tty) },
"-insert-key-name": func() { insertKeyName(app) },
"clear": func() { clear(app, tty) },
})
}

Expand Down
1 change: 1 addition & 0 deletions pkg/edit/init.elv
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ set insert:binding = (binding-table [
&Ctrl-K= $kill-line-right~

&Ctrl-V= $insert-raw~
&Ctrl-Alt-V= $-insert-key-name~

&Alt-,= $lastcmd:start~
&Alt-.= $insert-last-word~
Expand Down

0 comments on commit 0b62bf4

Please sign in to comment.