complete.c 5.1 KB

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