github.com/jmigpin/editor@v1.6.0/util/iout/iorw/rwedit/duplicatelines.go (about) 1 package rwedit 2 3 import "github.com/jmigpin/editor/util/iout" 4 5 func DuplicateLines(ctx *Ctx) error { 6 a, b, newline, err := ctx.CursorSelectionLinesIndexes() 7 if err != nil { 8 return err 9 } 10 11 s0, err := ctx.RW.ReadFastAt(a, b-a) 12 if err != nil { 13 return err 14 } 15 s := iout.CopyBytes(s0) 16 17 c := b 18 if !newline { 19 s = append([]byte{'\n'}, s...) 20 c++ 21 } 22 23 if err := ctx.RW.OverwriteAt(b, 0, s); err != nil { 24 return err 25 } 26 27 // cursor index without the newline 28 d := b + len(s) 29 if newline && len(s) > 0 && s[len(s)-1] == '\n' { 30 d-- 31 } 32 33 ctx.C.SetSelection(c, d) 34 return nil 35 }