Skip to content

Commit

Permalink
Also let ctrl-d work in block mode
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Aug 23, 2024
1 parent 8cc4dff commit 4b6ecce
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
6 changes: 3 additions & 3 deletions v2/backspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (e *Editor) Backspace(c *vt100.Canvas, bookmark *Position) {
e.pos.Up()
e.TrimRight(e.DataY())
e.End(c)
e.Delete()
e.Delete(c)
}
} else if (e.EmptyLine() || e.AtStartOfTheLine()) && e.indentation.Spaces && e.indentation.WSLen(e.LeadingWhitespace()) >= e.indentation.PerTab {
// Delete several spaces
Expand All @@ -34,7 +34,7 @@ func (e *Editor) Backspace(c *vt100.Canvas, bookmark *Position) {
// Type a blank
e.SetRune(' ')
e.WriteRune(c)
e.Delete()
e.Delete(c)
}
} else {
// Move back
Expand All @@ -44,7 +44,7 @@ func (e *Editor) Backspace(c *vt100.Canvas, bookmark *Position) {
e.WriteRune(c)
if !e.AtOrAfterEndOfLine() {
// Delete the blank
e.Delete()
e.Delete(c)
// scroll left instead of moving the cursor left, if possible
e.pos.mut.Lock()
if e.pos.offsetX > 0 {
Expand Down
66 changes: 38 additions & 28 deletions v2/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,37 +663,47 @@ func (e *Editor) DeleteCurrentLineMoveBookmark(bookmark *Position) {
}

// Delete will delete a character at the given position
func (e *Editor) Delete() {
y := int(e.DataY())
lineLen := len(e.lines[y])
if _, ok := e.lines[y]; !ok || lineLen == 0 || (lineLen == 1 && unicode.IsSpace(e.lines[y][0])) {
// All keys in the map that are > y should be shifted -1.
// This also overwrites e.lines[y].
e.DeleteLine(LineIndex(y))
e.changed = true
return
}
x, err := e.DataX()
if err != nil || x > len(e.lines[y])-1 {
// on the last index, just use every element but x
e.lines[y] = e.lines[y][:x]
// check if the next line exists
if _, ok := e.lines[y+1]; ok {
// then add the contents of the next line, if available
nextLine, ok := e.lines[y+1]
if ok && len(nextLine) > 0 {
e.lines[y] = append(e.lines[y], nextLine...)
// then delete the next line
e.DeleteLine(LineIndex(y + 1))
func (e *Editor) Delete(c *vt100.Canvas) {

deleteThisRune := func() bool {
y := int(e.DataY())
lineLen := len(e.lines[y])
if _, ok := e.lines[y]; !ok || lineLen == 0 || (lineLen == 1 && unicode.IsSpace(e.lines[y][0])) {
// All keys in the map that are > y should be shifted -1.
// This also overwrites e.lines[y].
e.DeleteLine(LineIndex(y))
e.changed = true
return true // continue
}
x, err := e.DataX()
if err != nil || x > len(e.lines[y])-1 {
// on the last index, just use every element but x
e.lines[y] = e.lines[y][:x]
// check if the next line exists
if _, ok := e.lines[y+1]; ok {
// then add the contents of the next line, if available
nextLine, ok := e.lines[y+1]
if ok && len(nextLine) > 0 && !e.blockMode {
e.lines[y] = append(e.lines[y], nextLine...)
// then delete the next line
e.DeleteLine(LineIndex(y + 1))
}
}
e.changed = true
return true // continue
}
e.changed = true
return
// Delete just this character
e.lines[y] = append(e.lines[y][:x], e.lines[y][x+1:]...)
return true // continue
}
// Delete just this character
e.lines[y] = append(e.lines[y][:x], e.lines[y][x+1:]...)
e.changed = true

if e.blockMode {
e.ForEachLineInBlock(c, deleteThisRune)
} else {
deleteThisRune()
}

e.changed = true
// Make sure no lines are nil
e.MakeConsistent()
}
Expand Down Expand Up @@ -2963,6 +2973,6 @@ func (e *Editor) JoinLineWithNext(c *vt100.Canvas, bookmark *Position) bool {
e.InsertRune(c, ' ')
e.WriteRune(c)
e.Next(c)
e.Delete()
e.Delete(c)
return true
}
2 changes: 1 addition & 1 deletion v2/keyloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,7 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
status.SetMessage("Empty")
status.Show(c, e)
} else {
e.Delete()
e.Delete(c)
e.redraw = true
}
e.redrawCursor = true
Expand Down

0 comments on commit 4b6ecce

Please sign in to comment.