search.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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), starting_line = curbp->b_line;
  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. break;
  66. case TB_KEY_ESC: /* esc */
  67. case TB_KEY_CTRL_G: /* ctrl-g */
  68. found_point = -1;
  69. curbp->b_point = o_point;
  70. curbp->b_line = starting_line;
  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. curbp->b_line = starting_line;
  114. if(dir == FWD_SEARCH)
  115. found = search_forward(curbp, o_point, searchtext);
  116. else
  117. found = search_backwards(curbp, o_point, searchtext);
  118. display_search_result(found, dir, prompt, searchtext, &failed_search);
  119. }
  120. }
  121. break;
  122. }
  123. }
  124. }
  125. void display_search_result(point_t found, int dir, char *prompt, char *search, int *failed_search)
  126. {
  127. int i = curwp->w_rows / 2, found_end = FALSE, shift = 0;
  128. point_t new_page = 0;
  129. if (found != -1 ) {
  130. search_dir = dir;
  131. tb_clear();
  132. found_point = found;
  133. curbp->b_point = dir == 2 ? found + 1 : found;
  134. update_display(TRUE);
  135. msg("%s%s",prompt, search);
  136. new_page = curbp->b_page;
  137. shift = curwp->w_row - i;
  138. if(dir == FWD_SEARCH) {
  139. for(int k = shift; k > 0; k--) {
  140. found_end = FALSE;
  141. while(found_end == FALSE) {
  142. if(*ptr(curbp, new_page) == '\n')
  143. found_end = TRUE;
  144. new_page++;
  145. }
  146. }
  147. } else {
  148. for(int k = shift; k < 0; k++) {
  149. new_page = lnstart(curbp,new_page - 1);
  150. }
  151. }
  152. curbp->b_page = lnstart(curbp, new_page);
  153. update_display(TRUE);
  154. *failed_search = FALSE;
  155. } else {
  156. found_point = -1;
  157. msg("Failing %s%s",prompt, search);
  158. dispmsg();
  159. *failed_search = TRUE;
  160. }
  161. }
  162. point_t search_forward(buffer_t *bp, point_t start_p, char *stext)
  163. {
  164. point_t end_p = pos(bp, bp->b_ebuf);
  165. point_t p,pp;
  166. char* s;
  167. char_t * cur;
  168. int case_sense = FALSE;
  169. if (0 == strlen(stext))
  170. return start_p;
  171. for (p=start_p; p < end_p; p++) {
  172. for (s=stext, pp=p; *s !='\0' && pp < end_p; s++, pp++) {
  173. cur = ptr(bp, pp);
  174. if(*cur == '\n')
  175. bp->b_line++;
  176. if(isupper(*s)) {
  177. case_sense = TRUE;
  178. if(*s != *cur)
  179. break;
  180. }
  181. else if (*s != *cur && case_sense)
  182. break;
  183. else if(*s != tolower(*cur))
  184. break;
  185. }
  186. if (*s == '\0')
  187. return pp;
  188. }
  189. return -1;
  190. }
  191. point_t search_backwards(buffer_t *bp, point_t start_p, char *stext)
  192. {
  193. point_t p,pp;
  194. char* s;
  195. char_t * cur;
  196. int case_sense = FALSE;
  197. if (0 == strlen(stext))
  198. return start_p;
  199. for (p=start_p; p >= 0; p--) {
  200. for (s=stext, pp=p; *s != '\0' && pp > -1; s++, pp++) {
  201. cur = ptr(bp, pp);
  202. if(*cur == '\n')
  203. bp->b_line--;
  204. if(isupper(*s)) {
  205. case_sense = TRUE;
  206. if(*s != *cur)
  207. break;
  208. }
  209. else if (*s != *cur && case_sense)
  210. break;
  211. else if(*s != tolower(*cur))
  212. break;
  213. }
  214. if (*s == '\0') {
  215. if (p > 0)
  216. p--;
  217. return p;
  218. }
  219. }
  220. return -1;
  221. }