replace.c 3.6 KB

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