complete.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* complete.c, Ait Emacs, Kevin Bloom, BSD 3-Clause, 2023-2024 */
  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, "printf \"%s\\n\" ");
  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')
  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 != '\n' || c == -1) rewind(fp);
  85. if(c == -1) goto do_tab;
  86. didtry = 1;
  87. tb_set_cursor(start_col, MSGLINE);
  88. clrtoeol(prompt, MSGLINE);
  89. addstr(buf);
  90. break;
  91. default:
  92. if (cpos < nbuf - 1) {
  93. for(int i = strlen(buf); i > cpos; i--) {
  94. buf[i] = buf[i - 1];
  95. }
  96. buf[cpos] = k;
  97. buf[strlen(buf)] = '\0';
  98. tb_set_cursor(start_col, MSGLINE);
  99. addstr(buf);
  100. cpos++;
  101. tb_set_cursor(start_col + cpos, MSGLINE);
  102. }
  103. break;
  104. }
  105. }
  106. }
  107. buffer_t * getbuffername(char *prompt, char *buf, int nbuf, int *ret)
  108. {
  109. int cpos = 0; /* current character position in string */
  110. int k = 0, c, i, didtry = 0;
  111. int start_col = strlen(prompt);
  112. struct tb_event ev;
  113. buffer_t *bp = NULL;
  114. char tabbuffer[PATH_MAX];
  115. buf[0] ='\0';
  116. cpos = strlen(buf);
  117. display_prompt_and_response(prompt, buf);
  118. for (;;) {
  119. didtry = (k == 0x09) ? didtry + 1 : 0; /* Was last command tab-completion? */
  120. tb_present();
  121. if(tb_poll_event(&ev) != TB_OK) return 0;
  122. if(msgline_editor(ev, prompt, buf, &cpos)) {
  123. k = 0;
  124. continue;
  125. }
  126. if(!ev.mod)
  127. k = ev.ch;
  128. else
  129. k = ev.key;
  130. switch(k) {
  131. case TB_KEY_CTRL_J: /* cr, lf */
  132. case TB_KEY_ENTER:
  133. *ret = 1;
  134. return bp;
  135. case TB_KEY_CTRL_G: /* ctrl-g, abort */
  136. tb_set_cursor(0, MSGLINE);
  137. clrtoeol("", MSGLINE);
  138. return NULL;
  139. case TB_KEY_BACKSPACE2: /* del, erase */
  140. case TB_KEY_BACKSPACE: /* backspace */
  141. if (cpos == 0) continue;
  142. buf[--cpos] = '\0';
  143. tb_set_cursor(start_col + cpos, MSGLINE);
  144. addch(' ');
  145. tb_set_cursor(start_col + cpos, MSGLINE);
  146. break;
  147. case TB_KEY_CTRL_U: /* C-u kill */
  148. cpos = 0;
  149. buf[0] = '\0';
  150. break;
  151. case TB_KEY_TAB: /* TAB, complete file name */
  152. if(didtry == 0)
  153. strncpy(tabbuffer, buf, PATH_MAX);
  154. for (c = 0, bp = bheadp; bp != NULL; bp = bp->b_next) {
  155. int match = FALSE;
  156. if(tabbuffer[0] == '\0') {
  157. if (didtry == c) {
  158. strncpy(buf, bp->b_bname, PATH_MAX);
  159. break;
  160. }
  161. c++;
  162. }
  163. for(i = 0; i < strlen(tabbuffer); i++)
  164. if(tabbuffer[i] == bp->b_bname[i]) {
  165. match = TRUE;
  166. } else {
  167. match = FALSE;
  168. break;
  169. }
  170. if(match) {
  171. if(didtry == c) {
  172. strncpy(buf, bp->b_bname, PATH_MAX);
  173. break;
  174. }
  175. c++;
  176. }
  177. }
  178. cpos = 0;
  179. cpos += strlen(buf);
  180. buf[cpos] = '\0';
  181. for(int i = cpos+1; buf[i] != '\0'; i++)
  182. buf[i] = 0;
  183. tb_set_cursor(start_col, MSGLINE);
  184. clrtoeol(prompt, MSGLINE);
  185. addstr(buf);
  186. break;
  187. default:
  188. if (cpos < nbuf - 1) {
  189. for(int i = strlen(buf); i > cpos; i--) {
  190. buf[i] = buf[i - 1];
  191. }
  192. buf[cpos] = k;
  193. tb_set_cursor(start_col, MSGLINE);
  194. addstr(buf);
  195. cpos++;
  196. tb_set_cursor(start_col + cpos, MSGLINE);
  197. }
  198. break;
  199. }
  200. }
  201. }