search.c 6.2 KB

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