Skip to content

Commit

Permalink
replace works
Browse files Browse the repository at this point in the history
  • Loading branch information
xt0fer committed Nov 26, 2018
1 parent 13ec4be commit ef3fb66
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 50 deletions.
2 changes: 1 addition & 1 deletion keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var keymap = []keymapt{
{"esc f forward-word ", "\x1B\x66", (*Editor).wright},
{"esc g gotoline ", "\x1B\x67", (*Editor).gotoline},
{"esc k kill-region ", "\x1B\x6B", (*Editor).cut},
// {"esc r query-replace ", "\x1B\x72", (*Editor).query_replace},
{"esc r query-replace ", "\x1B\x72", (*Editor).queryReplace},
{"esc v backward-page ", "\x1B\x76", (*Editor).pgup},
{"esc w copy-region ", "\x1B\x77", (*Editor).copy},
{"esc @ set-mark ", "\x1B\x40", (*Editor).iblock}, /* esc-@ */
Expand Down
69 changes: 21 additions & 48 deletions replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,24 @@ import "fmt"

/*search for a string and replace it with another string */
func (e *Editor) queryReplace() {
// point_t o_point = curbp->b_point;
// point_t l_point = -1;
// point_t found;
// char question[STRBUF_L];
// int slen, rlen; /* length of search and replace strings */
// int numsub = 0; /* number of substitutions */
// int ask = TRUE;
// int c;

// searchtext[0] = '\0';
// replace[0] = '\0';

e.Searchtext = e.getInput("Query replace: ")
if len(e.Searchtext) < 1 {
return
}

e.Replace = e.getInput("With: ")
slen := len(e.Searchtext)
rlen := len(e.Replace)
//rlen := len(e.Replace)
bp := e.CurrentBuffer
opoint := bp.Point()
lpoint := -1
ask := true
/* build query replace question string */
question := fmt.Sprintf("Replace '%s' with '%s' ? ", e.Searchtext, e.Replace)

/* scan through the file, from point */
numsub := 0
outer:
for {
found := bp.searchForward(bp.Point(), e.Searchtext)

/* if not found set the point to the last point of replacement, or where we started */
if found == -1 {
if lpoint == -1 {
Expand All @@ -45,66 +31,53 @@ func (e *Editor) queryReplace() {
}
break
}

bp.SetPoint(found)
/* search_forward places point at end of search, move to start of search */
//curbp->b_point -= slen
for k := 0; k < slen; k++ {
bp.PointPrevious()
}
e.Display(e.CurrentWindow, true)

if ask == true {
e.msg(question)
answer := e.getInput(question)

inner:
for {
e.Display(e.CurrentWindow, true)
//c = getch();
resp := e.getInput(question)
c := resp[1]
resp := []rune(answer)
c := ' '
if len(resp) > 0 {
c = resp[0]
}
switch c {
case 'y': /* yes, substitute */
break
break inner

case 'n': /* no, find next */
bp.SetPoint(found) /* set to end of search string */
continue
//continue inner

case '!': /* yes/stop asking, do the lot */
ask = false
break
break inner

case 0x1B: /* esc */
//case 0x1B: /* esc */
//flushinp() /* discard any escape sequence without writing in buffer */
case 'q': /* controlled exit */
return
break outer

default: /* help me */
e.msg("(y)es, (n)o, (!)do the rest, (q)uit")
continue
answer = e.getInput("(y)es, (n)o, (!)do the rest, (q)uit: ")
//continue inner
}
}
}

if rlen > slen {
// movegap(curbp, found);
// /*check enough space in gap left */
// if (rlen - slen < curbp->b_egap - curbp->b_gap)
// growgap(curbp, rlen - slen);
// /* shrink gap right by r - s */
// curbp->b_gap = curbp->b_gap + (rlen - slen);
// } else if (slen > rlen) {
// movegap(curbp, found);
// /* stretch gap left by s - r, no need to worry about space */
// curbp->b_gap = curbp->b_gap - (slen - rlen);
// } else {
// /* if rlen = slen, we just overwrite the chars, no need to move gap */
for k := 0; k < slen; k++ {
bp.Delete()
}

/* now just overwrite the chars at point in the buffer */
// l_point = curbp->b_point;
// memcpy(ptr(curbp, curbp->b_point), replace, rlen * sizeof (char_t));
// curbp->b_flags |= B_MODIFIED;
// curbp->b_point = found - (slen - rlen); /* end of replcement */
bp.Insert(e.Replace)
lpoint = bp.Point()
numsub++
}

Expand Down
2 changes: 1 addition & 1 deletion search.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (e *Editor) rsearch() {
func (e *Editor) displaySearchResult(found int, dir int, prompt string, search string) {
if found != -1 {
e.CurrentBuffer.SetPoint(found)
e.msg("%s%s", prompt, search)
e.msg("%s/%s", prompt, search)
e.Display(e.CurrentWindow, true)
} else {
e.msg("Failing %s%s", prompt, search)
Expand Down

0 comments on commit ef3fb66

Please sign in to comment.