github.com/jmigpin/editor@v1.6.0/util/iout/iorw/rwedit/startendofline.go (about)

     1  package rwedit
     2  
     3  import (
     4  	"unicode"
     5  
     6  	"github.com/jmigpin/editor/util/iout/iorw"
     7  )
     8  
     9  func StartOfLine(ctx *Ctx, sel bool) error {
    10  	ci := ctx.C.Index()
    11  
    12  	rd := ctx.LocalReader(ci)
    13  	i, err := iorw.LineStartIndex(rd, ci)
    14  	if err != nil {
    15  		return err
    16  	}
    17  
    18  	// stop at first non blank rune from the left
    19  	n := ci - i
    20  	for j := 0; j < n; j++ {
    21  		ru, _, err := iorw.ReadRuneAt(ctx.RW, i+j)
    22  		if err != nil {
    23  			return err
    24  		}
    25  		if !unicode.IsSpace(ru) {
    26  			i += j
    27  			break
    28  		}
    29  	}
    30  
    31  	ctx.C.UpdateSelection(sel, i)
    32  	return nil
    33  }
    34  
    35  func EndOfLine(ctx *Ctx, sel bool) error {
    36  	rd := ctx.LocalReader(ctx.C.Index())
    37  	le, newline, err := iorw.LineEndIndex(rd, ctx.C.Index())
    38  	if err != nil {
    39  		return err
    40  	}
    41  	if newline {
    42  		le--
    43  	}
    44  	ctx.C.UpdateSelection(sel, le)
    45  	return nil
    46  }