search.c 6.1 KB

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