complete.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* complete.c, Ait Emacs, Kevin Bloom, BSD 3-Clause, 2023-2024 */
  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[] = 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("", 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. strcpy(temp_file, TEMPFILE);
  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(prompt, 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() - 1;
  134. struct tb_event ev;
  135. buffer_t *bp = NULL;
  136. char tabbuffer[PATH_MAX];
  137. buf[0] ='\0';
  138. cpos = strlen(buf);
  139. display_prompt_and_response(prompt, buf);
  140. for (;;) {
  141. didtry = (k == 0x09) ? didtry + 1 : 0; /* Was last command tab-completion? */
  142. if(k == 0x09 && didtry > total_buffers + 1) {
  143. didtry = 1;
  144. }
  145. tb_present();
  146. if(tb_poll_event(&ev) != TB_OK) return 0;
  147. if(msgline_editor(ev, prompt, buf, nbuf, &cpos)) {
  148. k = 0;
  149. continue;
  150. }
  151. if(!ev.mod)
  152. k = ev.ch;
  153. else
  154. k = ev.key;
  155. switch(k) {
  156. case TB_KEY_CTRL_U: {
  157. char *list, *command, *data;
  158. FILE *pf;
  159. if(switch_command == NULL) {
  160. break;
  161. }
  162. list = calloc(total_buffers*sizeof(char *)*(STRBUF_L+1), '\0');
  163. bp = bheadp;
  164. strcpy(list, bp->b_bname);
  165. strcat(list, " ");
  166. bp = bp->b_next;
  167. for (; bp != NULL; bp = bp->b_next) {
  168. strcat(list, bp->b_bname);
  169. strcat(list, " ");
  170. }
  171. asprintf(&command, "printf \"%%s\\n\" %s | %s", list, switch_command);
  172. tb_shutdown();
  173. pf = popen(command,"r");
  174. if(pf == NULL){
  175. msg("Could not open pipe for output.");
  176. break;
  177. }
  178. data = malloc(sizeof(char *) * STRBUF_L);
  179. fgets(data, sizeof(char *) * STRBUF_L, pf);
  180. tb_init();
  181. LINES = tb_height();
  182. COLS = tb_width();
  183. MSGLINE = LINES-1;
  184. tb_set_input_mode(TB_INPUT_ALT);
  185. redraw();
  186. if(data[0] == -1 || data[0] == 0) {
  187. free(list);
  188. free(command);
  189. free(data);
  190. break;
  191. }
  192. for (bp = bheadp; bp != NULL; bp = bp->b_next) {
  193. /* data has a trailing \n character that we don't want to compare */
  194. if(strncmp(data, bp->b_bname, strlen(data) - 1) == 0) {
  195. free(list);
  196. free(command);
  197. free(data);
  198. *ret = 1;
  199. return bp;
  200. }
  201. }
  202. addstr(buf);
  203. break;
  204. }
  205. case TB_KEY_CTRL_J: /* cr, lf */
  206. case TB_KEY_ENTER:
  207. *ret = 1;
  208. return bp;
  209. case TB_KEY_CTRL_G: /* ctrl-g, abort */
  210. tb_set_cursor(0, MSGLINE);
  211. clrtoeol("", MSGLINE);
  212. return NULL;
  213. case TB_KEY_BACKSPACE2: /* del, erase */
  214. case TB_KEY_BACKSPACE: /* backspace */
  215. if (cpos == 0) continue;
  216. buf[--cpos] = '\0';
  217. tb_set_cursor(start_col + cpos, MSGLINE);
  218. addch(' ');
  219. tb_set_cursor(start_col + cpos, MSGLINE);
  220. break;
  221. /* case TB_KEY_CTRL_U:
  222. cpos = 0;
  223. buf[0] = '\0';
  224. break;
  225. */
  226. case TB_KEY_TAB: /* TAB, complete file name */
  227. if(didtry == 0) {
  228. strncpy(tabbuffer, buf, PATH_MAX);
  229. didtry++;
  230. }
  231. for (c = 1, bp = bheadp; bp != NULL; bp = bp->b_next) {
  232. if(tabbuffer[0] == '\0') {
  233. if (didtry == c) {
  234. strncpy(buf, bp->b_bname, PATH_MAX);
  235. break;
  236. }
  237. c++;
  238. continue;
  239. }
  240. if(strncmp(tabbuffer, bp->b_bname, strlen(tabbuffer)) == 0) {
  241. if(didtry == c) {
  242. strncpy(buf, bp->b_bname, PATH_MAX);
  243. break;
  244. }
  245. c++;
  246. }
  247. }
  248. cpos = 0;
  249. cpos += strlen(buf);
  250. buf[cpos] = '\0';
  251. for(int i = cpos+1; buf[i] != '\0'; i++)
  252. buf[i] = 0;
  253. tb_set_cursor(start_col, MSGLINE);
  254. clrtoeol(prompt, MSGLINE);
  255. addstr(buf);
  256. break;
  257. default:
  258. if (cpos < nbuf - 1) {
  259. for(int i = strlen(buf); i > cpos; i--) {
  260. buf[i] = buf[i - 1];
  261. }
  262. buf[cpos] = k;
  263. tb_set_cursor(start_col, MSGLINE);
  264. addstr(buf);
  265. cpos++;
  266. tb_set_cursor(start_col + cpos, MSGLINE);
  267. }
  268. break;
  269. }
  270. }
  271. }