complete.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. continue;
  22. if(!ev.mod)
  23. k = ev.ch;
  24. else
  25. k = ev.key;
  26. if (k < 32 &&
  27. k != TB_KEY_CTRL_G &&
  28. k != TB_KEY_BACKSPACE &&
  29. k != TB_KEY_BACKSPACE2 &&
  30. k != TB_KEY_ENTER &&
  31. k != TB_KEY_CTRL_R &&
  32. k != TB_KEY_CTRL_S &&
  33. k != TB_KEY_ESC &&
  34. k != TB_KEY_CTRL_I &&
  35. k != TB_KEY_TAB)
  36. continue;
  37. switch(k) {
  38. case TB_KEY_CTRL_J: /* cr, lf */
  39. case TB_KEY_ENTER:
  40. if (cpos > 0 && NULL != strchr(buf, '~')) goto do_tab;
  41. case TB_KEY_CTRL_G: /* ctrl-g, abort */
  42. if (fp != NULL) fclose(fp);
  43. tb_set_cursor(0, MSGLINE);
  44. clrtoeol("", MSGLINE);
  45. return (k != 0x07 && cpos > 0);
  46. case TB_KEY_CTRL_U: /* C-u kill */
  47. cpos = 0;
  48. buf[0] = '\0';
  49. break;
  50. do_tab:
  51. case TB_KEY_TAB: /* TAB, complete file name */
  52. /* scan backwards for a wild card and set */
  53. if(cpos != strlen(buf))
  54. continue;
  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. if (c != ' ') rewind(fp);
  83. didtry = 1;
  84. tb_set_cursor(start_col, MSGLINE);
  85. clrtoeol(prompt, MSGLINE);
  86. addstr(buf);
  87. break;
  88. default:
  89. if (cpos < nbuf - 1) {
  90. for(int i = strlen(buf); i > cpos; i--) {
  91. buf[i] = buf[i - 1];
  92. }
  93. buf[cpos] = k;
  94. buf[strlen(buf)] = '\0';
  95. tb_set_cursor(start_col, MSGLINE);
  96. addstr(buf);
  97. cpos++;
  98. tb_set_cursor(start_col + cpos, MSGLINE);
  99. }
  100. break;
  101. }
  102. }
  103. }
  104. buffer_t * getbuffername(char *prompt, char *buf, int nbuf, int *ret)
  105. {
  106. int cpos = 0; /* current character position in string */
  107. int k = 0, c, i, didtry = 0;
  108. int start_col = strlen(prompt);
  109. struct tb_event ev;
  110. buffer_t *bp = NULL;
  111. char tabbuffer[NAME_MAX];
  112. buf[0] ='\0';
  113. cpos = strlen(buf);
  114. display_prompt_and_response(prompt, buf);
  115. for (;;) {
  116. didtry = (k == 0x09) ? didtry + 1 : didtry; /* Was last command tab-completion? */
  117. tb_present();
  118. if(tb_poll_event(&ev) != TB_OK) return 0;
  119. if(msgline_editor(ev, prompt, buf, &cpos))
  120. continue;
  121. if(!ev.mod)
  122. k = ev.ch;
  123. else
  124. k = ev.key;
  125. switch(k) {
  126. case TB_KEY_CTRL_J: /* cr, lf */
  127. case TB_KEY_ENTER:
  128. *ret = 1;
  129. return bp == NULL ? curbp->b_next : bp;
  130. case TB_KEY_CTRL_G: /* ctrl-g, abort */
  131. tb_set_cursor(0, MSGLINE);
  132. clrtoeol("", MSGLINE);
  133. return NULL;
  134. case TB_KEY_BACKSPACE2: /* del, erase */
  135. case TB_KEY_BACKSPACE: /* backspace */
  136. if (cpos == 0) continue;
  137. buf[--cpos] = '\0';
  138. tb_set_cursor(start_col + cpos, MSGLINE);
  139. addch(' ');
  140. tb_set_cursor(start_col + cpos, MSGLINE);
  141. break;
  142. case TB_KEY_CTRL_U: /* C-u kill */
  143. cpos = 0;
  144. buf[0] = '\0';
  145. break;
  146. case TB_KEY_TAB: /* TAB, complete file name */
  147. if(cpos != strlen(buf))
  148. continue;
  149. if(didtry == 0)
  150. strncpy(tabbuffer, buf, NAME_MAX);
  151. for (c = 0, bp = bheadp; bp != NULL; bp = bp->b_next) {
  152. int match = FALSE;
  153. if(tabbuffer[0] == '\0') {
  154. if (didtry == c) {
  155. strncpy(buf, bp->b_bname, NAME_MAX);
  156. break;
  157. }
  158. c++;
  159. }
  160. for(i = 0; i < strlen(tabbuffer); i++)
  161. if(tabbuffer[i] == bp->b_bname[i]) {
  162. match = TRUE;
  163. } else {
  164. match = FALSE;
  165. break;
  166. }
  167. if(match) {
  168. if(didtry == c) {
  169. strncpy(buf, bp->b_bname, NAME_MAX);
  170. break;
  171. }
  172. c++;
  173. }
  174. }
  175. cpos = 0;
  176. cpos += strlen(buf);
  177. buf[cpos] = '\0';
  178. tb_set_cursor(start_col, MSGLINE);
  179. clrtoeol(prompt, MSGLINE);
  180. addstr(buf);
  181. break;
  182. default:
  183. if (cpos < nbuf - 1) {
  184. for(int i = strlen(buf); i > cpos; i--) {
  185. buf[i] = buf[i - 1];
  186. }
  187. buf[cpos] = k;
  188. tb_set_cursor(start_col, MSGLINE);
  189. addstr(buf);
  190. cpos++;
  191. tb_set_cursor(start_col + cpos, MSGLINE);
  192. }
  193. break;
  194. }
  195. }
  196. }