summaryrefslogtreecommitdiff
path: root/shed_complete.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2023-04-02 15:38:44 -0400
committerJakob Kaivo <jkk@ung.org>2023-04-02 15:38:44 -0400
commitf8fe4917d052187e7e184ab769055b6cf85986c0 (patch)
treee6ed491683dde30dcac4756521f92bd7d1ce867f /shed_complete.c
parentf4ec5ce5e758361a91929487950747bc4ae178fc (diff)
begin working on completion
Diffstat (limited to 'shed_complete.c')
-rw-r--r--shed_complete.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/shed_complete.c b/shed_complete.c
new file mode 100644
index 0000000..145e0a7
--- /dev/null
+++ b/shed_complete.c
@@ -0,0 +1,52 @@
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <wordexp.h>
+#include "shed.h"
+
+static void shed_reprompt(struct shed *e)
+{
+ write(STDOUT_FILENO, e->prompt, strlen(e->prompt));
+ size_t pos = e->cur->pos;
+ e->cur->pos = 0;
+ e->count = (int)pos + 1;
+ shed_move_column(e);
+ e->count = 0;
+}
+
+int shed_complete_wordexp(struct shed *e)
+{
+ char *start = e->cur->buf + e->cur->pos;
+ while (!isblank(*start) && start != e->cur->buf) {
+ start--;
+ }
+ if (isblank(*start)) {
+ start++;
+ }
+
+ char *word = calloc(1, strlen(start) + 2);
+ strcpy(word, start);
+ char *end = word;
+ while (!isblank(*end) && *end != '\0') {
+ end++;
+ }
+ *end = '\0';
+
+ if (strpbrk(word, "*?[") == NULL) {
+ strcat(word, "*");
+ }
+
+ wordexp_t we = { 0 };
+ wordexp(word, &we, WRDE_NOCMD);
+ for (size_t i = 0; i < we.we_wordc; i++) {
+ write(STDOUT_FILENO, i == 0 ? "\n" : "\t", 1);
+ write(STDOUT_FILENO, we.we_wordv[i], strlen(we.we_wordv[i]));
+ }
+ write(STDOUT_FILENO, "\n", 2);
+ wordfree(&we);
+
+ free(word);
+ shed_reprompt(e);
+ return 1;
+}