123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- /* replace.c, Ait Emacs, Kevin Bloom, BSD 3-Clause, 2023 */
- #include <string.h>
- #include "header.h"
- #include "termbox.h"
- /*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;
- struct tb_event ev;
- undoset();
- memset(searchtext, 0, STRBUF_M);
- memset(replace, 0, STRBUF_M);
- if (!getinput("Query replace: ", (char*)searchtext, STRBUF_M, F_CLEAR, FALSE))
- return;
- if (!getinput("With: ", (char*)replace, STRBUF_M, F_CLEAR, TRUE))
- 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(curbp, curbp->b_point, 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;
- search_dir = 1;
- found_point = found;
- if (ask == TRUE) {
- msg(question);
- clrtoeol(question, MSGLINE);
- qprompt:
- update_display(TRUE);
- if(tb_poll_event(&ev) != TB_OK) return;
- if(!ev.mod)
- c = ev.ch;
- else
- c = ev.key;
- 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;
- found_point = -1;
- break;
- case TB_KEY_CTRL_G:
- case TB_KEY_ESC: /* esc */
- found_point = -1;
- case 'q': /* controlled exit */
- found_point = -1;
- 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++;
- }
- found_point = -1;
- msg("%d substitutions", numsub);
- }
|