replace.c 4.0 KB

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