replace.c 3.9 KB

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