search.c 6.4 KB

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