/* * A special insert used as the undo of delete char (C-d or DEL) * this is where the char is inserted at the point and the cursor * is NOT moved on 1 char. This MUST be a seperate function so that * INSERT + BACKSPACE are matching undo pairs * INSERT_AT + DELETE are matching undo pairs * Note: This function is only ever called by execute_undo to undo a DEL. */ void insert_at() { char_t the_char[2]; /* the inserted char plus a null */ assert(curbp->b_gap <= curbp->b_egap); if (curbp->b_gap == curbp->b_egap && !growgap(curbp, CHUNK)) return; curbp->b_point = movegap(curbp, curbp->b_point); /* overwrite if mid line, not EOL or EOF, CR will insert as normal */ if ((curbp->b_flags & B_OVERWRITE) && *input != '\r' && *(ptr(curbp, curbp->b_point)) != '\n' && curbp->b_point < pos(curbp,curbp->b_ebuf) ) { *(ptr(curbp, curbp->b_point)) = *input; if (curbp->b_point < pos(curbp, curbp->b_ebuf)) ++curbp->b_point; /* FIXME - overwite mode not handled properly for undo yet */ } else { the_char[0] = *input == '\r' ? '\n' : *input; the_char[1] = '\0'; /* null terminate */ *curbp->b_gap++ = the_char[0]; curbp->b_point = pos(curbp, curbp->b_egap); curbp->b_point--; /* move point back to where it was before, should always be safe */ /* the point is set so that and undo will DELETE the char */ add_undo(curbp, UNDO_T_INSAT, curbp->b_point, the_char, NULL); } add_mode(curbp, B_MODIFIED); }
void insert() { assert(curbp->b_gap <= curbp->b_egap); if (curbp->b_gap == curbp->b_egap && !growgap(curbp, CHUNK)) return; curbp->b_point = movegap(curbp, curbp->b_point); /* overwrite if mid line, not EOL or EOF, CR will insert as normal */ if ((curbp->b_flags & B_OVERWRITE) && input != '\r' && *(ptr(curbp, curbp->b_point)) != '\n' && curbp->b_point < pos(curbp,curbp->b_ebuf) ) { *(ptr(curbp, curbp->b_point)) = input; if (curbp->b_point < pos(curbp, curbp->b_ebuf)) ++curbp->b_point; } else { *curbp->b_gap++ = input == '\r' ? '\n' : input; curbp->b_point = pos(curbp, curbp->b_egap); } curbp->b_flags |= B_MODIFIED; }
/*search for a string and replace it with another string */ void query_replace(void) { 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'; if (!getinput("Query replace: ", (char*)searchtext, STRBUF_M)) return; if (!getinput("With: ", (char*)replace, STRBUF_M)) return; slen = strlen(searchtext); rlen = strlen(replace); /* build query replace question string */ sprintf(question, "Replace '%s' with '%s' ? ", searchtext, replace); /* scan through the file, from point */ numsub = 0; while(TRUE) { found = search_forward(searchtext); /* if not found set the point to the last point of replacement, or where we started */ if (found == -1) { curbp->b_point = (l_point == -1 ? o_point : l_point); break; } curbp->b_point = found; /* search_forward places point at end of search, move to start of search */ curbp->b_point -= slen; if (ask == TRUE) { msg(question); clrtoeol(); qprompt: display(curwp, TRUE); c = getch(); switch (c) { case 'y': /* yes, substitute */ break; case 'n': /* no, find next */ curbp->b_point = found; /* set to end of search string */ continue; case '!': /* yes/stop asking, do the lot */ ask = FALSE; break; case 0x1B: /* esc */ flushinp(); /* discard any escape sequence without writing in buffer */ case 'q': /* controlled exit */ return; default: /* help me */ msg("(y)es, (n)o, (!)do the rest, (q)uit"); goto qprompt; } } 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 */ } /* 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 */ numsub++; } msg("%d substitutions", numsub); }