Add yank actions

This commit is contained in:
Kovid Goyal 2022-10-30 08:31:49 +05:30
parent 1f115870bb
commit 22f7145e34
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 52 additions and 2 deletions

View File

@ -51,7 +51,6 @@ func (self *Readline) all_text() string {
} }
func (self *Readline) add_text(text string) { func (self *Readline) add_text(text string) {
defer func() { self.last_action = ActionAddText }()
new_lines := make([]string, 0, len(self.lines)+4) new_lines := make([]string, 0, len(self.lines)+4)
new_lines = append(new_lines, self.lines[:self.cursor.Y]...) new_lines = append(new_lines, self.lines[:self.cursor.Y]...)
var lines_after []string var lines_after []string
@ -430,6 +429,40 @@ func (self *Readline) kill_previous_space_delimited_word(amt uint, traverse_line
return num_killed return num_killed
} }
func (self *Readline) ensure_position_in_bounds(pos *Position) *Position {
pos.Y = utils.Max(0, utils.Min(pos.Y, len(self.lines)-1))
line := self.lines[pos.Y]
pos.X = utils.Max(0, utils.Min(pos.X, len(line)))
return pos
}
func (self *Readline) yank(repeat_count uint, pop bool) bool {
if pop && self.last_action != ActionYank && self.last_action != ActionPopYank {
return false
}
text := ""
if pop {
text = self.kill_ring.pop_yank()
} else {
text = self.kill_ring.yank()
}
if text == "" {
return false
}
before := self.cursor
if pop {
self.ensure_position_in_bounds(&self.last_yank_extent.start)
self.ensure_position_in_bounds(&self.last_yank_extent.end)
self.erase_between(self.last_yank_extent.start, self.last_yank_extent.end)
self.cursor = self.last_yank_extent.start
before = self.cursor
}
self.add_text(text)
self.last_yank_extent.start = before
self.last_yank_extent.end = self.cursor
return true
}
func (self *Readline) perform_action(ac Action, repeat_count uint) error { func (self *Readline) perform_action(ac Action, repeat_count uint) error {
defer func() { self.last_action = ac }() defer func() { self.last_action = ac }()
switch ac { switch ac {
@ -531,6 +564,14 @@ func (self *Readline) perform_action(ac Action, repeat_count uint) error {
if self.kill_previous_space_delimited_word(repeat_count, true) > 0 { if self.kill_previous_space_delimited_word(repeat_count, true) > 0 {
return nil return nil
} }
case ActionYank:
if self.yank(repeat_count, false) {
return nil
}
case ActionPopYank:
if self.yank(repeat_count, true) {
return nil
}
} }
return ErrCouldNotPerformAction return ErrCouldNotPerformAction
} }

View File

@ -66,6 +66,9 @@ const (
ActionKillPreviousWord ActionKillPreviousWord
ActionKillPreviousSpaceDelimitedWord ActionKillPreviousSpaceDelimitedWord
ActionEndKillActions ActionEndKillActions
ActionYank
ActionPopYank
) )
type kill_ring struct { type kill_ring struct {
@ -120,6 +123,9 @@ type Readline struct {
lines []string lines []string
// The cursor position in the text // The cursor position in the text
cursor Position cursor Position
last_yank_extent struct {
start, end Position
}
bracketed_paste_buffer strings.Builder bracketed_paste_buffer strings.Builder
last_action Action last_action Action
} }
@ -216,6 +222,7 @@ func (self *Readline) OnText(text string, from_key_event bool, in_bracketed_past
self.bracketed_paste_buffer.Reset() self.bracketed_paste_buffer.Reset()
} }
self.add_text(text) self.add_text(text)
self.last_action = ActionAddText
return nil return nil
} }

View File

@ -46,6 +46,8 @@ var default_shortcuts = map[string]Action{
"alt+d": ActionKillNextWord, "alt+d": ActionKillNextWord,
"alt+backspace": ActionKillPreviousWord, "alt+backspace": ActionKillPreviousWord,
"ctrl+w": ActionKillPreviousSpaceDelimitedWord, "ctrl+w": ActionKillPreviousSpaceDelimitedWord,
"ctrl+y": ActionYank,
"alt+y": ActionPopYank,
} }
func action_for_key_event(event *loop.KeyEvent, shortcuts map[string]Action) Action { func action_for_key_event(event *loop.KeyEvent, shortcuts map[string]Action) Action {