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

     1  package rwedit
     2  
     3  import (
     4  	"github.com/jmigpin/editor/util/iout/iorw"
     5  	"github.com/jmigpin/editor/util/uiutil/event"
     6  )
     7  
     8  func SelectWord(ctx *Ctx) error {
     9  	// index rune
    10  	ci := ctx.C.Index()
    11  	ru, _, err := iorw.ReadRuneAt(ctx.RW, ci)
    12  	if err != nil {
    13  		return err
    14  	}
    15  
    16  	var index int
    17  	var word []byte
    18  	if !iorw.IsWordRune(ru) {
    19  		// select just the index rune
    20  		index = ci
    21  		word = []byte(string(ru))
    22  	} else {
    23  		// select word at index
    24  		rd := ctx.LocalReader(ci)
    25  		w, i, err := iorw.WordAtIndex(rd, ci)
    26  		if err != nil {
    27  			return err
    28  		}
    29  
    30  		index = i
    31  		word = w
    32  	}
    33  
    34  	ctx.C.SetSelection(index, index+len(word))
    35  
    36  	// set primary copy
    37  	if b, ok := ctx.Selection(); ok {
    38  		ctx.Fns.SetClipboardData(event.CIPrimary, string(b))
    39  	}
    40  
    41  	return nil
    42  }