cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / ui / vimrepeat.go
vimrepeat.go 2.1K
1package ui23import (4 tea "charm.land/bubbletea/v2"5)67// . repeats the last change made in NORMAL mode, as vim does: the keys8// of the operator (x, dd, cc, s, p, o, i, a, = and the rest marked9// change in vimkeys.go) with its count and register, and when it started10// an entry, the keys typed into it up to the one that finished it. A11// count before . replaces the change's own (3. after dd deletes three12// rows).1314// The binding gets repeatChange here rather than in its table, which15// repeatChange reads through the key handlers.16func init() {17 b := vimNormal["."]18 b.do = repeatChange19 vimNormal["."] = b20}2122// vimChange is a change . can repeat.23type vimChange struct {24 keys []tea.Msg // key presses and pastes, after any count or register25 count int26 reg rune27}2829// vimChanged remembers c as the change . repeats, once any entry it30// started is finished.31func (m *Model) vimChanged(c *vimChange) {32 if m.editing() {33 m.vim.inserting = c34 return35 }36 m.vim.last = c37}3839// vimCapture adds a key or paste to the entry a change started, before40// the model handles it.41func (m *Model) vimCapture(msg tea.Msg) {42 v := &m.vim43 if v.inserting == nil || v.replaying {44 return45 }46 switch msg.(type) {47 case tea.KeyPressMsg, tea.PasteMsg:48 v.inserting.keys = append(v.inserting.keys, msg)49 }50}5152// vimSettle finishes the change taking an entry's keys once the entry53// is accepted or cancelled.54func (m *Model) vimSettle() {55 if v := &m.vim; v.inserting != nil && !m.editing() {56 v.last, v.inserting = v.inserting, nil57 }58}5960// repeatChange is .: it replays the last change's keys, with the count61// typed before . when there is one.62func repeatChange(m *Model, _ int) tea.Cmd {63 v := &m.vim64 c := v.last65 if c == nil {66 m.note = "No change to repeat yet"67 return nil68 }69 count := c.count70 if v.count > 0 {71 count = v.count72 }73 v.reset()74 v.count, v.reg = count, c.reg75 v.replaying = true76 defer func() { v.replaying = false }()77 cmds := make([]tea.Cmd, 0, len(c.keys))78 for _, msg := range c.keys {79 switch msg := msg.(type) {80 case tea.KeyPressMsg:81 cmds = append(cmds, m.handleKey(msg))82 case tea.PasteMsg:83 m.handlePaste(msg.Content)84 }85 }86 return tea.Batch(cmds...)87}