replace.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, 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;
  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. 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(TRUE);
  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 TB_KEY_CTRL_G:
  79. case TB_KEY_ESC: /* esc */
  80. case TB_KEY_ENTER:
  81. case 'q': /* controlled exit */
  82. found_point = -1;
  83. msg("%d substitutions", numsub);
  84. return;
  85. default: /* help me */
  86. msg("(y)es, (n)o, (!)do the rest, (q)uit");
  87. goto qprompt;
  88. }
  89. }
  90. mark = curbp->b_mark;
  91. curbp->b_mark = curbp->b_point + slen;
  92. undoset(REPLACE, rlen);
  93. curbp->b_mark = mark;
  94. if (rlen > slen) {
  95. movegap(curbp, found);
  96. /*check enough space in gap left */
  97. if (rlen - slen < curbp->b_egap - curbp->b_gap)
  98. growgap(curbp, rlen - slen);
  99. /* shrink gap right by r - s */
  100. curbp->b_gap = curbp->b_gap + (rlen - slen);
  101. } else if (slen > rlen) {
  102. movegap(curbp, found);
  103. /* stretch gap left by s - r, no need to worry about space */
  104. curbp->b_gap = curbp->b_gap - (slen - rlen);
  105. } else {
  106. /* if rlen = slen, we just overwrite the chars, no need to move gap */
  107. }
  108. /* now just overwrite the chars at point in the buffer */
  109. l_point = curbp->b_point;
  110. memcpy(ptr(curbp, curbp->b_point), replace, rlen * sizeof (char_t));
  111. curbp->b_flags |= B_MODIFIED;
  112. curbp->b_point = found - (slen - rlen); /* end of replcement */
  113. numsub++;
  114. }
  115. found_point = -1;
  116. msg("%d substitutions", numsub);
  117. }