complete.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* complete.c, Ait Emacs, Kevin Bloom, BSD 3-Clause, 2023-2025 */
  2. #include <sys/stat.h>
  3. #include "header.h"
  4. #include "termbox.h"
  5. /* basic filename completion, based on code in uemacs/PK */
  6. int getfilename(char *prompt, char *buf, int nbuf)
  7. {
  8. static char temp_file[15] = TEMPFILE;
  9. int cpos = 0; /* current character position in string */
  10. int k = 0, c, fd, didtry, iswild = 0, start_col = strlen(prompt);
  11. int isdir = TRUE;
  12. struct tb_event ev;
  13. char sys_command[255];
  14. FILE *fp = NULL;
  15. struct stat s;
  16. // buf[0] ='\0';
  17. cpos = strlen(buf);
  18. display_prompt_and_response(prompt, buf);
  19. for (;;) {
  20. didtry = (k == 0x09); /* Was last command tab-completion? */
  21. tb_present();
  22. if(tb_poll_event(&ev) != TB_OK) return 0;
  23. if(msgline_editor(ev, prompt, buf, nbuf, &cpos)) {
  24. k = 0;
  25. continue;
  26. }
  27. if(!ev.mod)
  28. k = ev.ch;
  29. else
  30. k = ev.key;
  31. if (k < 32 &&
  32. k != TB_KEY_CTRL_G &&
  33. k != TB_KEY_BACKSPACE &&
  34. k != TB_KEY_BACKSPACE2 &&
  35. k != TB_KEY_ENTER &&
  36. k != TB_KEY_CTRL_R &&
  37. k != TB_KEY_CTRL_S &&
  38. k != TB_KEY_ESC &&
  39. k != TB_KEY_CTRL_I &&
  40. k != TB_KEY_TAB)
  41. continue;
  42. switch(k) {
  43. case TB_KEY_CTRL_J: /* cr, lf */
  44. case TB_KEY_ENTER: {
  45. if(isdir) {
  46. didtry = 0;
  47. break;
  48. }
  49. /* backup files end with ~, therefore, don't tab if the ~ is at
  50. the end
  51. */
  52. char *idx = strchr(buf, '~');
  53. if (cpos > 0 && (idx != NULL && idx != &buf[strlen(buf)-1]))
  54. goto do_tab;
  55. }
  56. case TB_KEY_CTRL_G: /* ctrl-g, abort */
  57. if (fp != NULL) fclose(fp);
  58. tb_set_cursor(0, MSGLINE);
  59. clrtoeol(0, MSGLINE);
  60. return (k != 0x07 && cpos > 0);
  61. case TB_KEY_CTRL_U: /* C-u kill */
  62. cpos = 0;
  63. buf[0] = '\0';
  64. break;
  65. do_tab:
  66. case TB_KEY_TAB: /* TAB, complete file name */
  67. /* scan backwards for a wild card and set */
  68. iswild=0;
  69. while (cpos > 0) {
  70. cpos--;
  71. if (buf[cpos] == '*' || buf[cpos] == '?')
  72. iswild = 1;
  73. }
  74. /* first time retrieval */
  75. if (! didtry) {
  76. if (fp != NULL) fclose(fp);
  77. strncpy(temp_file, TEMPFILE, 14);
  78. if (-1 == (fd = mkstemp(temp_file)))
  79. fatal("%s: Failed to create temp file\n");
  80. strcpy(sys_command, "printf \"%s\\n\" ");
  81. strcat(sys_command, buf);
  82. if (!iswild) strcat(sys_command, "*");
  83. strcat(sys_command, " >");
  84. strcat(sys_command, temp_file);
  85. strcat(sys_command, " 2>&1");
  86. (void) ! system(sys_command); /* stop compiler unused result warning */
  87. fp = fdopen(fd, "r");
  88. unlink(temp_file);
  89. }
  90. /* copy next filename into buf */
  91. while ((c = getc(fp)) != EOF && c != '\n')
  92. if (cpos < nbuf - 1 && c != '*')
  93. buf[cpos++] = c;
  94. buf[cpos] = '\0';
  95. if(stat(buf, &s) == 0 && s.st_mode & S_IFDIR) {
  96. buf[cpos++] = '/';
  97. isdir = TRUE;
  98. } else {
  99. isdir = FALSE;
  100. }
  101. buf[cpos] = '\0';
  102. for(int i = cpos+1; buf[i] != '\0'; i++)
  103. buf[i] = 0;
  104. if (c != '\n' || c == -1) rewind(fp);
  105. if(c == -1) goto do_tab;
  106. didtry = 1;
  107. tb_set_cursor(start_col, MSGLINE);
  108. clrtoeol(start_col, MSGLINE);
  109. addstr(buf);
  110. break;
  111. default:
  112. if (cpos < nbuf - 1) {
  113. for(int i = strlen(buf); i > cpos; i--) {
  114. buf[i] = buf[i - 1];
  115. }
  116. buf[cpos] = k;
  117. buf[strlen(buf)] = '\0';
  118. tb_set_cursor(start_col, MSGLINE);
  119. addstr(buf);
  120. cpos++;
  121. tb_set_cursor(start_col + cpos, MSGLINE);
  122. isdir = k == '/';
  123. }
  124. break;
  125. }
  126. }
  127. }
  128. buffer_t * getbuffername(char *prompt, char *buf, int nbuf, int *ret)
  129. {
  130. int cpos = 0; /* current character position in string */
  131. int k = 0, c, didtry = 0;
  132. int start_col = strlen(prompt);
  133. int total_buffers = count_buffers();
  134. int filtered_buffers = total_buffers;
  135. struct tb_event ev;
  136. buffer_t *bp = NULL, *bbp = NULL;
  137. char tabbuffer[PATH_MAX];
  138. buf[0] ='\0';
  139. cpos = strlen(buf);
  140. display_prompt_and_response(prompt, buf);
  141. for (;;) {
  142. /* Since the number of valid buffer name options changes based on
  143. what is input, we have to recalculate the valid total number of
  144. buffers before we continue.
  145. */
  146. if(tabbuffer[0] == '\0')
  147. filtered_buffers = total_buffers;
  148. else {
  149. filtered_buffers = 0;
  150. for (bbp = bheadp; bbp != NULL; bbp = bbp->b_next) {
  151. if(strncmp(tabbuffer, bbp->b_bname, strlen(tabbuffer)) == 0)
  152. filtered_buffers++;
  153. }
  154. }
  155. didtry = (k == 0x09) ? didtry + 1 : 0; /* Was last command tab-completion? */
  156. if(k == 0x09 && didtry > filtered_buffers) {
  157. didtry = 1;
  158. }
  159. tb_present();
  160. if(tb_poll_event(&ev) != TB_OK) return 0;
  161. if(msgline_editor(ev, prompt, buf, nbuf, &cpos)) {
  162. k = 0;
  163. continue;
  164. }
  165. if(!ev.mod)
  166. k = ev.ch;
  167. else
  168. k = ev.key;
  169. switch(k) {
  170. case TB_KEY_CTRL_U: {
  171. char *list, *command, *data;
  172. FILE *pf;
  173. if(switch_command == NULL) {
  174. break;
  175. }
  176. list = malloc(total_buffers*sizeof(char *)*(STRBUF_L+1));
  177. memset(list, 0, total_buffers*sizeof(char *)*(STRBUF_L+1));
  178. bp = bheadp;
  179. strcpy(list, bp->b_bname);
  180. strcat(list, " ");
  181. bp = bp->b_next;
  182. for (; bp != NULL; bp = bp->b_next) {
  183. strcat(list, bp->b_bname);
  184. strcat(list, " ");
  185. }
  186. asprintf(&command, "printf \"%%s\\n\" %s | %s", list, switch_command);
  187. tb_shutdown();
  188. pf = popen(command,"r");
  189. if(pf == NULL){
  190. msg("Could not open pipe for output.");
  191. break;
  192. }
  193. data = malloc(sizeof(char *) * STRBUF_L);
  194. fgets(data, sizeof(char *) * STRBUF_L, pf);
  195. tb_init();
  196. LINES = tb_height();
  197. COLS = tb_width();
  198. MSGLINE = LINES-1;
  199. tb_set_input_mode(TB_INPUT_ALT);
  200. redraw();
  201. if(data[0] == -1 || data[0] == 0) {
  202. free(list);
  203. free(command);
  204. free(data);
  205. break;
  206. }
  207. for (bp = bheadp; bp != NULL; bp = bp->b_next) {
  208. /* data has a trailing \n character that we don't want to compare */
  209. if(strncmp(data, bp->b_bname, strlen(data) - 1) == 0) {
  210. free(list);
  211. free(command);
  212. free(data);
  213. *ret = 1;
  214. return bp;
  215. }
  216. }
  217. addstr(buf);
  218. break;
  219. }
  220. case TB_KEY_CTRL_J: /* cr, lf */
  221. case TB_KEY_ENTER:
  222. *ret = 1;
  223. return bp;
  224. case TB_KEY_CTRL_G: /* ctrl-g, abort */
  225. tb_set_cursor(0, MSGLINE);
  226. clrtoeol(0, MSGLINE);
  227. return NULL;
  228. case TB_KEY_BACKSPACE2: /* del, erase */
  229. case TB_KEY_BACKSPACE: /* backspace */
  230. if (cpos == 0) continue;
  231. buf[--cpos] = '\0';
  232. tb_set_cursor(start_col + cpos, MSGLINE);
  233. addch(' ');
  234. tb_set_cursor(start_col + cpos, MSGLINE);
  235. break;
  236. /* case TB_KEY_CTRL_U:
  237. cpos = 0;
  238. buf[0] = '\0';
  239. break;
  240. */
  241. case TB_KEY_TAB: /* TAB, complete file name */
  242. if(didtry == 0) {
  243. strncpy(tabbuffer, buf, PATH_MAX);
  244. didtry++;
  245. }
  246. for(c = 1, bp = bheadp; bp != NULL; bp = bp->b_next) {
  247. if(tabbuffer[0] == '\0') {
  248. if (didtry == c) {
  249. strncpy(buf, bp->b_bname, PATH_MAX);
  250. break;
  251. }
  252. c++;
  253. continue;
  254. }
  255. if(strncmp(tabbuffer, bp->b_bname, strlen(tabbuffer)) == 0) {
  256. if(didtry == c) {
  257. strncpy(buf, bp->b_bname, PATH_MAX);
  258. break;
  259. }
  260. c++;
  261. }
  262. }
  263. cpos = 0;
  264. cpos += strlen(buf);
  265. buf[cpos] = '\0';
  266. for(int i = cpos+1; buf[i] != '\0'; i++)
  267. buf[i] = 0;
  268. tb_set_cursor(start_col, MSGLINE);
  269. clrtoeol(start_col, MSGLINE);
  270. addstr(buf);
  271. break;
  272. default:
  273. if (cpos < nbuf - 1) {
  274. for(int i = strlen(buf); i > cpos; i--) {
  275. buf[i] = buf[i - 1];
  276. }
  277. buf[cpos] = k;
  278. tb_set_cursor(start_col, MSGLINE);
  279. addstr(buf);
  280. cpos++;
  281. tb_set_cursor(start_col + cpos, MSGLINE);
  282. }
  283. break;
  284. }
  285. }
  286. }