search.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* search.c, Ait, BSD 3-Clause, Kevin Bloom, 2023-2025,
  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_CTRL_I &&
  64. c != TB_KEY_TAB &&
  65. c != TB_KEY_ESC)
  66. continue;
  67. switch(c) {
  68. case TB_KEY_ENTER: /* return */
  69. if(found != o_point) {
  70. shift_pmark(TRUE, o_point);
  71. shift_pmark(TRUE, curbp->b_point);
  72. found_point = -1;
  73. return;
  74. }
  75. found = search_forward(curbp, curbp->b_point, searchtext);
  76. display_search_result(found, FWD_SEARCH, prompt, searchtext, &failed_search);
  77. curbp->b_pcol = curbp->b_col;
  78. break;
  79. case TB_KEY_ESC: /* esc */
  80. case TB_KEY_CTRL_G: /* ctrl-g */
  81. found_point = -1;
  82. curbp->b_point = o_point;
  83. curbp->b_page = o_page;
  84. clrtoeol(0, MSGLINE);
  85. return;
  86. case TB_KEY_CTRL_S: /* ctrl-s, do the search */
  87. if(searchtext[0] == '\0') {
  88. strcpy(searchtext, prev_search);
  89. }
  90. cpos = strlen(searchtext);
  91. found = search_forward(curbp, curbp->b_point, searchtext);
  92. if(found == -1 && failed_search)
  93. found = search_forward(curbp, 0, searchtext);
  94. display_search_result(found, FWD_SEARCH, prompt, searchtext, &failed_search);
  95. dir = FWD_SEARCH;
  96. break;
  97. case TB_KEY_CTRL_R: /* ctrl-r, do the search */
  98. if(searchtext[0] == '\0') {
  99. strcpy(searchtext, prev_search);
  100. }
  101. cpos = strlen(searchtext);
  102. found = search_backwards(curbp, curbp->b_point-1, searchtext);
  103. if(found == -1 && failed_search)
  104. found = search_backwards(curbp, pos(curbp, curbp->b_ebuf), searchtext);
  105. display_search_result(found, REV_SEARCH, prompt, searchtext, &failed_search);
  106. dir = REV_SEARCH;
  107. break;
  108. case TB_KEY_BACKSPACE2: /* del, erase */
  109. case TB_KEY_BACKSPACE: /* backspace */
  110. if (cpos == 0)
  111. continue;
  112. searchtext[--cpos] = '\0';
  113. tb_set_cursor(start_col + cpos, MSGLINE);
  114. display_prompt_and_response(prompt, searchtext);
  115. break;
  116. default:
  117. if (cpos < STRBUF_L - 1) {
  118. for(int i = strlen(searchtext); i > cpos; i--) {
  119. searchtext[i] = searchtext[i - 1];
  120. }
  121. searchtext[cpos] = c;
  122. tb_set_cursor(start_col, MSGLINE);
  123. addstr(searchtext);
  124. cpos++;
  125. tb_set_cursor(start_col + cpos, MSGLINE);
  126. if(cpos == strlen(searchtext)) {
  127. if(dir == FWD_SEARCH)
  128. found = search_forward(curbp, o_point, searchtext);
  129. else
  130. found = search_backwards(curbp, o_point, searchtext);
  131. display_search_result(found, dir, prompt, searchtext, &failed_search);
  132. }
  133. }
  134. break;
  135. }
  136. }
  137. }
  138. void display_search_result(point_t found, int dir, char *prompt, char *search, int *failed_search)
  139. {
  140. int i = curwp->w_rows / 2, found_end = FALSE, shift = 0;
  141. point_t new_page = 0;
  142. if (found != -1 ) {
  143. search_dir = dir;
  144. // tb_clear();
  145. found_point = found;
  146. curbp->b_point = dir == 2 ? found + 1 : found;
  147. update_display();
  148. msg("%s%s",prompt, search);
  149. new_page = curbp->b_page;
  150. shift = curwp->w_row - i;
  151. if(dir == FWD_SEARCH) {
  152. for(int k = shift; k > 0; k--) {
  153. found_end = FALSE;
  154. while(found_end == FALSE) {
  155. if(*ptr(curbp, new_page) == '\n')
  156. found_end = TRUE;
  157. new_page++;
  158. }
  159. }
  160. } else {
  161. for(int k = shift; k < 0; k++) {
  162. new_page = lnstart(curbp,new_page - 1);
  163. }
  164. }
  165. curbp->b_page = lnstart(curbp, new_page);
  166. update_display();
  167. *failed_search = FALSE;
  168. } else {
  169. found_point = -1;
  170. msg("Failing %s%s",prompt, search);
  171. dispmsg();
  172. *failed_search = TRUE;
  173. }
  174. }
  175. point_t search_forward(buffer_t *bp, point_t start_p, char *stext)
  176. {
  177. point_t end_p = pos(bp, bp->b_ebuf);
  178. point_t p,pp;
  179. char* s;
  180. char_t * cur;
  181. int case_sense = FALSE;
  182. if (0 == strlen(stext))
  183. return start_p;
  184. for (p=start_p; p < end_p; p++) {
  185. for (s=stext, pp=p; *s !='\0' && pp < end_p; s++, pp++) {
  186. cur = ptr(bp, pp);
  187. if(isupper(*s)) {
  188. case_sense = TRUE;
  189. if(*s != *cur)
  190. break;
  191. }
  192. else if (*s != *cur && case_sense)
  193. break;
  194. else if(*s != tolower(*cur))
  195. break;
  196. }
  197. if (*s == '\0')
  198. return pp;
  199. }
  200. return -1;
  201. }
  202. point_t search_backwards(buffer_t *bp, point_t start_p, char *stext)
  203. {
  204. point_t p,pp;
  205. char* s;
  206. char_t * cur;
  207. int case_sense = FALSE;
  208. if (0 == strlen(stext))
  209. return start_p;
  210. for (p=start_p; p >= 0; p--) {
  211. for (s=stext, pp=p; *s != '\0' && pp > -1; s++, pp++) {
  212. cur = ptr(bp, pp);
  213. if(isupper(*s)) {
  214. case_sense = TRUE;
  215. if(*s != *cur)
  216. break;
  217. }
  218. else if (*s != *cur && case_sense)
  219. break;
  220. else if(*s != tolower(*cur))
  221. break;
  222. }
  223. if (*s == '\0') {
  224. if (p > 0)
  225. p--;
  226. return p;
  227. }
  228. }
  229. return -1;
  230. }