github.com/jmigpin/editor@v1.6.0/core/internalcmds/replace.go (about)

     1  package internalcmds
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/jmigpin/editor/core"
     7  	"github.com/jmigpin/editor/util/iout/iorw/rwedit"
     8  )
     9  
    10  func Replace(args0 *core.InternalCmdArgs) error {
    11  	erow := args0.ERow
    12  	part := args0.Part
    13  
    14  	args := part.Args[1:]
    15  	if len(args) != 2 {
    16  		return fmt.Errorf("expecting 2 arguments")
    17  	}
    18  
    19  	old, new := args[0].UnquotedString(), args[1].UnquotedString()
    20  
    21  	ta := erow.Row.TextArea
    22  	ta.BeginUndoGroup()
    23  	defer ta.EndUndoGroup()
    24  	replaced, err := rwedit.Replace(ta.EditCtx(), old, new)
    25  	if err != nil {
    26  		return err
    27  	}
    28  	if !replaced {
    29  		return fmt.Errorf("string not replaced: %q", old)
    30  	}
    31  	return nil
    32  }