replace.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* replace.c, Ait Emacs, Kevin Bloom, BSD 3-Clause, 2023 */
  2. #include <string.h>
  3. #include "header.h"
  4. #include "termbox.h"
  5. /*search for a string and replace it with another string */
  6. void query_replace(void)
  7. {
  8. point_t o_point = curbp->b_point;
  9. point_t l_point = -1;
  10. point_t found;
  11. char question[STRBUF_L];
  12. int slen, rlen; /* length of search and replace strings */
  13. int numsub = 0; /* number of substitutions */
  14. int ask = TRUE;
  15. int c;
  16. struct tb_event ev;
  17. memset(searchtext, 0, STRBUF_M);
  18. memset(replace, 0, STRBUF_M);
  19. if (!getinput("Query replace: ", (char*)searchtext, STRBUF_M, F_CLEAR))
  20. return;
  21. if (!getinput("With: ", (char*)replace, STRBUF_M, F_CLEAR))
  22. return;
  23. slen = strlen(searchtext);
  24. rlen = strlen(replace);
  25. /* build query replace question string */
  26. sprintf(question, "Replace '%s' with '%s' ? ", searchtext, replace);
  27. /* scan through the file, from point */
  28. numsub = 0;
  29. while(TRUE) {
  30. found = search_forward(curbp, curbp->b_point, searchtext);
  31. /* if not found set the point to the last point of replacement, or where we started */
  32. if (found == -1) {
  33. curbp->b_point = (l_point == -1 ? o_point : l_point);
  34. break;
  35. }
  36. curbp->b_point = found;
  37. /* search_forward places point at end of search, move to start of search */
  38. curbp->b_point -= slen;
  39. search_dir = 1;
  40. found_point = found;
  41. if (ask == TRUE) {
  42. msg(question);
  43. clrtoeol(question, MSGLINE);
  44. qprompt:
  45. update_display(TRUE);
  46. if(tb_poll_event(&ev) != TB_OK) return;
  47. if(!ev.mod)
  48. c = ev.ch;
  49. else
  50. c = ev.key;
  51. switch (c) {
  52. case 'y': /* yes, substitute */
  53. break;
  54. case 'n': /* no, find next */
  55. curbp->b_point = found; /* set to end of search string */
  56. continue;
  57. case '!': /* yes/stop asking, do the lot */
  58. ask = FALSE;
  59. found_point = -1;
  60. break;
  61. case TB_KEY_CTRL_G:
  62. case TB_KEY_ESC: /* esc */
  63. found_point = -1;
  64. case 'q': /* controlled exit */
  65. found_point = -1;
  66. return;
  67. default: /* help me */
  68. msg("(y)es, (n)o, (!)do the rest, (q)uit");
  69. goto qprompt;
  70. }
  71. }
  72. if (rlen > slen) {
  73. movegap(curbp, found);
  74. /*check enough space in gap left */
  75. if (rlen - slen < curbp->b_egap - curbp->b_gap)
  76. growgap(curbp, rlen - slen);
  77. /* shrink gap right by r - s */
  78. curbp->b_gap = curbp->b_gap + (rlen - slen);
  79. } else if (slen > rlen) {
  80. movegap(curbp, found);
  81. /* stretch gap left by s - r, no need to worry about space */
  82. curbp->b_gap = curbp->b_gap - (slen - rlen);
  83. } else {
  84. /* if rlen = slen, we just overwrite the chars, no need to move gap */
  85. }
  86. /* now just overwrite the chars at point in the buffer */
  87. l_point = curbp->b_point;
  88. memcpy(ptr(curbp, curbp->b_point), replace, rlen * sizeof (char_t));
  89. curbp->b_flags |= B_MODIFIED;
  90. curbp->b_point = found - (slen - rlen); /* end of replcement */
  91. numsub++;
  92. }
  93. found_point = -1;
  94. msg("%d substitutions", numsub);
  95. }