complete.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* complete.c, Ait Emacs, Kevin Bloom, BSD 3-Clause, 2023 */
  2. #include "header.h"
  3. #include "termbox.h"
  4. /* basic filename completion, based on code in uemacs/PK */
  5. int getfilename(char *prompt, char *buf, int nbuf)
  6. {
  7. static char temp_file[] = TEMPFILE;
  8. int cpos = 0; /* current character position in string */
  9. int k = 0, c, fd, didtry, iswild = 0, start_col = strlen(prompt);
  10. struct tb_event ev;
  11. char sys_command[255];
  12. FILE *fp = NULL;
  13. // buf[0] ='\0';
  14. cpos = strlen(buf);
  15. display_prompt_and_response(prompt, buf);
  16. for (;;) {
  17. didtry = (k == 0x09); /* Was last command tab-completion? */
  18. tb_present();
  19. if(tb_poll_event(&ev) != TB_OK) return 0;
  20. if(msgline_editor(ev, prompt, buf, &cpos)) {
  21. k = 0;
  22. continue;
  23. }
  24. if(!ev.mod)
  25. k = ev.ch;
  26. else
  27. k = ev.key;
  28. if (k < 32 &&
  29. k != TB_KEY_CTRL_G &&
  30. k != TB_KEY_BACKSPACE &&
  31. k != TB_KEY_BACKSPACE2 &&
  32. k != TB_KEY_ENTER &&
  33. k != TB_KEY_CTRL_R &&
  34. k != TB_KEY_CTRL_S &&
  35. k != TB_KEY_ESC &&
  36. k != TB_KEY_CTRL_I &&
  37. k != TB_KEY_TAB)
  38. continue;
  39. switch(k) {
  40. case TB_KEY_CTRL_J: /* cr, lf */
  41. case TB_KEY_ENTER:
  42. if (cpos > 0 && NULL != strchr(buf, '~')) goto do_tab;
  43. case TB_KEY_CTRL_G: /* ctrl-g, abort */
  44. if (fp != NULL) fclose(fp);
  45. tb_set_cursor(0, MSGLINE);
  46. clrtoeol("", MSGLINE);
  47. return (k != 0x07 && cpos > 0);
  48. case TB_KEY_CTRL_U: /* C-u kill */
  49. cpos = 0;
  50. buf[0] = '\0';
  51. break;
  52. do_tab:
  53. case TB_KEY_TAB: /* TAB, complete file name */
  54. /* scan backwards for a wild card and set */
  55. iswild=0;
  56. while (cpos > 0) {
  57. cpos--;
  58. if (buf[cpos] == '*' || buf[cpos] == '?')
  59. iswild = 1;
  60. }
  61. /* first time retrieval */
  62. if (! didtry) {
  63. if (fp != NULL) fclose(fp);
  64. strcpy(temp_file, TEMPFILE);
  65. if (-1 == (fd = mkstemp(temp_file)))
  66. fatal("%s: Failed to create temp file\n");
  67. strcpy(sys_command, "echo ");
  68. strcat(sys_command, buf);
  69. if (!iswild) strcat(sys_command, "*");
  70. strcat(sys_command, " >");
  71. strcat(sys_command, temp_file);
  72. strcat(sys_command, " 2>&1");
  73. (void) ! system(sys_command); /* stop compiler unused result warning */
  74. fp = fdopen(fd, "r");
  75. unlink(temp_file);
  76. }
  77. /* copy next filename into buf */
  78. while ((c = getc(fp)) != EOF && c != '\n' && c != ' ')
  79. if (cpos < nbuf - 1 && c != '*')
  80. buf[cpos++] = c;
  81. buf[cpos] = '\0';
  82. for(int i = cpos+1; buf[i] != '\0'; i++)
  83. buf[i] = 0;
  84. if (c != ' ') rewind(fp);
  85. didtry = 1;
  86. tb_set_cursor(start_col, MSGLINE);
  87. clrtoeol(prompt, MSGLINE);
  88. addstr(buf);
  89. break;
  90. default:
  91. if (cpos < nbuf - 1) {
  92. for(int i = strlen(buf); i > cpos; i--) {
  93. buf[i] = buf[i - 1];
  94. }
  95. buf[cpos] = k;
  96. buf[strlen(buf)] = '\0';
  97. tb_set_cursor(start_col, MSGLINE);
  98. addstr(buf);
  99. cpos++;
  100. tb_set_cursor(start_col + cpos, MSGLINE);
  101. }
  102. break;
  103. }
  104. }
  105. }
  106. buffer_t * getbuffername(char *prompt, char *buf, int nbuf, int *ret)
  107. {
  108. int cpos = 0; /* current character position in string */
  109. int k = 0, c, i, didtry = 0;
  110. int start_col = strlen(prompt);
  111. struct tb_event ev;
  112. buffer_t *bp = NULL;
  113. char tabbuffer[NAME_MAX];
  114. buf[0] ='\0';
  115. cpos = strlen(buf);
  116. display_prompt_and_response(prompt, buf);
  117. for (;;) {
  118. didtry = (k == 0x09) ? didtry + 1 : 0; /* Was last command tab-completion? */
  119. tb_present();
  120. if(tb_poll_event(&ev) != TB_OK) return 0;
  121. if(msgline_editor(ev, prompt, buf, &cpos)) {
  122. k = 0;
  123. continue;
  124. }
  125. if(!ev.mod)
  126. k = ev.ch;
  127. else
  128. k = ev.key;
  129. switch(k) {
  130. case TB_KEY_CTRL_J: /* cr, lf */
  131. case TB_KEY_ENTER:
  132. *ret = 1;
  133. return bp;
  134. case TB_KEY_CTRL_G: /* ctrl-g, abort */
  135. tb_set_cursor(0, MSGLINE);
  136. clrtoeol("", MSGLINE);
  137. return NULL;
  138. case TB_KEY_BACKSPACE2: /* del, erase */
  139. case TB_KEY_BACKSPACE: /* backspace */
  140. if (cpos == 0) continue;
  141. buf[--cpos] = '\0';
  142. tb_set_cursor(start_col + cpos, MSGLINE);
  143. addch(' ');
  144. tb_set_cursor(start_col + cpos, MSGLINE);
  145. break;
  146. case TB_KEY_CTRL_U: /* C-u kill */
  147. cpos = 0;
  148. buf[0] = '\0';
  149. break;
  150. case TB_KEY_TAB: /* TAB, complete file name */
  151. if(didtry == 0)
  152. strncpy(tabbuffer, buf, NAME_MAX);
  153. for (c = 0, bp = bheadp; bp != NULL; bp = bp->b_next) {
  154. int match = FALSE;
  155. if(tabbuffer[0] == '\0') {
  156. if (didtry == c) {
  157. strncpy(buf, bp->b_bname, NAME_MAX);
  158. break;
  159. }
  160. c++;
  161. }
  162. for(i = 0; i < strlen(tabbuffer); i++)
  163. if(tabbuffer[i] == bp->b_bname[i]) {
  164. match = TRUE;
  165. } else {
  166. match = FALSE;
  167. break;
  168. }
  169. if(match) {
  170. if(didtry == c) {
  171. strncpy(buf, bp->b_bname, NAME_MAX);
  172. break;
  173. }
  174. c++;
  175. }
  176. }
  177. cpos = 0;
  178. cpos += strlen(buf);
  179. buf[cpos] = '\0';
  180. for(int i = cpos+1; buf[i] != '\0'; i++)
  181. buf[i] = 0;
  182. tb_set_cursor(start_col, MSGLINE);
  183. clrtoeol(prompt, MSGLINE);
  184. addstr(buf);
  185. break;
  186. default:
  187. if (cpos < nbuf - 1) {
  188. for(int i = strlen(buf); i > cpos; i--) {
  189. buf[i] = buf[i - 1];
  190. }
  191. buf[cpos] = k;
  192. tb_set_cursor(start_col, MSGLINE);
  193. addstr(buf);
  194. cpos++;
  195. tb_set_cursor(start_col + cpos, MSGLINE);
  196. }
  197. break;
  198. }
  199. }
  200. }