complete.c 5.8 KB

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