search.c 6.2 KB

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