search.c 5.8 KB

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