search.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* search.c, Ait, BSD 3-Clause, Kevin Bloom, 2023,
  2. Derived from: Atto January 2017
  3. Derived from: Anthony's Editor January 93
  4. */
  5. #include "header.h"
  6. #include "termbox.h"
  7. #define FWD_SEARCH 1
  8. #define REV_SEARCH 2
  9. void search_fwd()
  10. {
  11. search(FWD_SEARCH);
  12. }
  13. void search_rev()
  14. {
  15. search(REV_SEARCH);
  16. }
  17. void search(int initdir)
  18. {
  19. char *prompt = "Search: ";
  20. int cpos = 0;
  21. int c, dir = initdir;
  22. point_t o_point = curbp->b_point;
  23. point_t found = o_point;
  24. int start_col = strlen(prompt);
  25. struct tb_event ev;
  26. char prev_search[STRBUF_M];
  27. strcpy(prev_search, searchtext);
  28. // searchtext[0] = '\0';
  29. memset(searchtext, 0, STRBUF_M);
  30. display_prompt_and_response(prompt, searchtext);
  31. cpos = strlen(searchtext);
  32. for (;;) {
  33. tb_present();
  34. if(tb_poll_event(&ev) != TB_OK) return;
  35. if(msgline_editor(ev, prompt, searchtext, &cpos))
  36. continue;
  37. if(!ev.mod)
  38. c = ev.ch;
  39. else
  40. c = ev.key;
  41. /* ignore control keys other than return, C-g, CR, C-s, C-R, ESC */
  42. if (c < 32 &&
  43. c != TB_KEY_CTRL_G &&
  44. c != TB_KEY_ENTER &&
  45. c != TB_KEY_CTRL_R &&
  46. c != TB_KEY_CTRL_S &&
  47. c != TB_KEY_ESC)
  48. continue;
  49. switch(c) {
  50. case TB_KEY_ENTER: /* return */
  51. if(found != o_point) {
  52. found_point = -1;
  53. return;
  54. }
  55. found = search_forward(curbp, curbp->b_point, searchtext);
  56. display_search_result(found, FWD_SEARCH, prompt, searchtext);
  57. break;
  58. case TB_KEY_ESC: /* esc */
  59. case TB_KEY_CTRL_G: /* ctrl-g */
  60. found_point = -1;
  61. curbp->b_point = o_point;
  62. return;
  63. case TB_KEY_CTRL_S: /* ctrl-s, do the search */
  64. if(searchtext[0] == '\0') {
  65. strcpy(searchtext, prev_search);
  66. cpos = strlen(searchtext);
  67. }
  68. found = search_forward(curbp, curbp->b_point, searchtext);
  69. display_search_result(found, FWD_SEARCH, prompt, searchtext);
  70. dir = FWD_SEARCH;
  71. break;
  72. case TB_KEY_CTRL_R: /* ctrl-r, do the search */
  73. if(searchtext[0] == '\0') {
  74. strcpy(searchtext, prev_search);
  75. cpos = strlen(searchtext);
  76. }
  77. found = search_backwards(curbp, curbp->b_point-1, searchtext);
  78. display_search_result(found, REV_SEARCH, prompt, searchtext);
  79. dir = REV_SEARCH;
  80. break;
  81. case TB_KEY_BACKSPACE2: /* del, erase */
  82. case TB_KEY_BACKSPACE: /* backspace */
  83. if (cpos == 0)
  84. continue;
  85. searchtext[--cpos] = '\0';
  86. tb_set_cursor(start_col + cpos, MSGLINE);
  87. display_prompt_and_response(prompt, searchtext);
  88. break;
  89. default:
  90. if (cpos < STRBUF_M - 1) {
  91. for(int i = strlen(searchtext); searchtext[i] != '\0'; i--) {
  92. searchtext[i] = searchtext[i - 1];
  93. }
  94. searchtext[cpos] = c;
  95. tb_set_cursor(start_col, MSGLINE);
  96. addstr(searchtext);
  97. cpos++;
  98. tb_set_cursor(start_col + cpos, MSGLINE);
  99. if(dir == FWD_SEARCH)
  100. found = search_forward(curbp, curbp->b_point-strlen(searchtext), searchtext);
  101. else
  102. found = search_backwards(curbp, curbp->b_point, searchtext);
  103. display_search_result(found, dir, prompt, searchtext);
  104. }
  105. break;
  106. }
  107. }
  108. }
  109. void display_search_result(point_t found, int dir, char *prompt, char *search)
  110. {
  111. int i = curwp->w_rows / 2, found_end = FALSE, shift = 0;
  112. point_t new_page = 0;
  113. if (found != -1 ) {
  114. search_dir = dir;
  115. tb_clear();
  116. found_point = found;
  117. curbp->b_point = dir == 2 ? found + 1 : found;
  118. update_display(TRUE);
  119. msg("%s%s",prompt, search);
  120. new_page = curbp->b_page;
  121. shift = curwp->w_row - i;
  122. if(dir == FWD_SEARCH) {
  123. for(int k = shift; k > 0; k--) {
  124. found_end = FALSE;
  125. while(found_end == FALSE) {
  126. if(*ptr(curbp, new_page) == '\n')
  127. found_end = TRUE;
  128. new_page++;
  129. }
  130. }
  131. } else {
  132. for(int k = shift; k < 0; k++) {
  133. new_page = lnstart(curbp,new_page - 1);
  134. }
  135. }
  136. curbp->b_page = lnstart(curbp, new_page);
  137. update_display(TRUE);
  138. } else {
  139. found_point = -1;
  140. msg("Failing %s%s",prompt, search);
  141. dispmsg();
  142. curbp->b_point = (dir == FWD_SEARCH ? 0 : pos(curbp, curbp->b_ebuf));
  143. }
  144. }
  145. point_t search_forward(buffer_t *bp, point_t start_p, char *stext)
  146. {
  147. point_t end_p = pos(bp, bp->b_ebuf);
  148. point_t p,pp;
  149. char* s;
  150. char_t * cur;
  151. int case_sense = FALSE;
  152. if (0 == strlen(stext))
  153. return start_p;
  154. for (p=start_p; p < end_p; p++) {
  155. for (s=stext, pp=p; *s !='\0' && pp < end_p; s++, pp++) {
  156. cur = ptr(bp, pp);
  157. if(isupper(*s)) {
  158. case_sense = TRUE;
  159. if(*s != *cur)
  160. break;
  161. }
  162. else if (*s != *cur && case_sense)
  163. break;
  164. else if(*s != tolower(*cur))
  165. break;
  166. }
  167. if (*s == '\0')
  168. return pp;
  169. }
  170. return -1;
  171. }
  172. point_t search_backwards(buffer_t *bp, point_t start_p, char *stext)
  173. {
  174. point_t p,pp;
  175. char* s;
  176. char_t * cur;
  177. int case_sense = FALSE;
  178. if (0 == strlen(stext))
  179. return start_p;
  180. for (p=start_p; p >= 0; p--) {
  181. for (s=stext, pp=p; *s != '\0' && pp > -1; s++, pp++) {
  182. cur = ptr(bp, pp);
  183. if(isupper(*s)) {
  184. case_sense = TRUE;
  185. if(*s != *cur)
  186. break;
  187. }
  188. else if (*s != *cur && case_sense)
  189. break;
  190. else if(*s != tolower(*cur))
  191. break;
  192. }
  193. if (*s == '\0') {
  194. if (p > 0)
  195. p--;
  196. return p;
  197. }
  198. }
  199. return -1;
  200. }