add path autocomplete
Build project / build (push) Has been cancelled

This commit is contained in:
arthur
2025-11-05 15:49:01 +01:00
parent 419e924650
commit 5588b0a8d7
4 changed files with 76 additions and 8 deletions
+1 -1
View File
@@ -20,7 +20,7 @@
// END \x1b[4~ || <esc>[8~ || <esc>[F || <esc>OF // END \x1b[4~ || <esc>[8~ || <esc>[F || <esc>OF
// DELETE \x1b[3~ // DELETE \x1b[3~
char *editorPrompt(char *prompt); char *editorPrompt(char *prompt, char * PlaceHolder, char bPathMode);
char *key_to_string(int key); char *key_to_string(int key);
+1 -1
View File
@@ -125,7 +125,7 @@ Lisp editorMoveCursorPageDown(Lisp args, LispError* e, LispContext ctx) {
} }
Lisp editorOpenFile(Lisp args, LispError *e, LispContext ctx) { Lisp editorOpenFile(Lisp args, LispError *e, LispContext ctx) {
char *filename = editorPrompt("Path : %s"); char *filename = editorPrompt("Open : %s", getenv("PWD"), 1);
if (filename) if (filename)
editorOpen(filename); editorOpen(filename);
+3 -3
View File
@@ -87,7 +87,7 @@ void editorSave() {
char *buf; char *buf;
int fd; int fd;
if (E.filename == NULL) { if (E.filename == NULL) {
E.filename = editorPrompt("Save as: %s (ESC to cancel)"); E.filename = editorPrompt("Save as: %s (ESC to cancel)", "", 1);
if (E.filename == NULL) { if (E.filename == NULL) {
editorSetStatusMessage("Save aborted"); editorSetStatusMessage("Save aborted");
return; return;
@@ -112,10 +112,10 @@ void editorSave() {
} }
void editorFind() { void editorFind() {
char *query = editorPrompt("Search: %s (ESC to cancel)"); char *query = editorPrompt("Search: %s (ESC to cancel)", "", 0);
if (query == NULL) return; if (query == NULL) return;
int i; int i;
for (i = 0; i < E.numrows; i++) { for (i = E.cursor_y + 1; i < E.numrows; i++) {
erow *row = &E.row[i]; erow *row = &E.row[i];
char *match = strstr(row->render, query); char *match = strstr(row->render, query);
if (match) { if (match) {
+70 -2
View File
@@ -3,6 +3,8 @@
#include "../include/output.h" #include "../include/output.h"
#include "../include/define.h" #include "../include/define.h"
#include <ctype.h> #include <ctype.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -11,16 +13,64 @@
extern struct editorConfig E; extern struct editorConfig E;
char * file_completion(const char *path) {
DIR * dir;
struct dirent *entry;
char directory[128];
char predict[128];
size_t predict_len;
// Find dir name
char * last_slash = strrchr(path, '/');
if (last_slash) {
size_t dir_len = last_slash - path + 1; // length of dir_path
strncpy(directory, path, dir_len);
predict_len = strlen(path) - dir_len - 1;
strncpy(predict, last_slash + 1, predict_len);
directory[dir_len] = '\0';
fprintf(stderr, "%s %s\n", directory, predict);
predict[predict_len] = '\0';
} else {
return NULL;
}
dir = opendir(directory);
if (!dir)
return NULL;
while ((entry = readdir(dir)) != NULL) {
if (strncmp(entry->d_name, predict, predict_len) == 0) {
static char full_path[128];
snprintf(full_path, sizeof(full_path), "%s%s", directory, entry->d_name);
struct stat st;
if (stat(full_path, &st) == 0 && S_ISDIR(st.st_mode)) {
strcat(full_path, "/"); // add slash for directories
}
return strdup(full_path);
}
}
// Cleanup when no more entries
closedir(dir);
dir = NULL;
return NULL;
}
/** /**
* \fn char * editorPrompt(struct editorConfig *E, char *prompt) * \fn char * editorPrompt(struct editorConfig *E, char *prompt, char bPathMode)
* \brief Return user input in a prompt when enter is hit. */ * \brief Return user input in a prompt when enter is hit. */
char *editorPrompt(char *prompt) { char *editorPrompt(char *prompt, char * placeHolder, char bPathMode) {
size_t buf_size = 128; size_t buf_size = 128;
char *buf = malloc(buf_size); char *buf = malloc(buf_size);
size_t buf_len = 0; size_t buf_len = 0;
int c = 0; int c = 0;
buf[0] = '\0'; buf[0] = '\0';
strcpy(buf, placeHolder);
buf_len = strlen(placeHolder);
while (1) { while (1) {
editorSetStatusMessage(prompt, buf); editorSetStatusMessage(prompt, buf);
@@ -39,6 +89,24 @@ char *editorPrompt(char *prompt) {
editorSetStatusMessage(""); editorSetStatusMessage("");
return buf; return buf;
} }
} else if (bPathMode && c == '\t') {
char path[128];
char * pwd;
if (buf[0] != '/') {
pwd = getenv("PWD");
fprintf(stderr, "%s\n", pwd);
memcpy(path, pwd, strlen(pwd));
path[strlen(pwd)] = '/';
strncat(path, buf, buf_len);
} else {
strcpy(path, buf);
}
memset(buf, 0, 128);
buf_len = 0;
strcpy(buf, file_completion(path));
buf_len = strlen(buf);
buf[buf_len] = '\0';
} else if (!iscntrl(c) && c < 128) { } else if (!iscntrl(c) && c < 128) {
if (buf_len == buf_size - 1) { if (buf_len == buf_size - 1) {
buf_size *= 2; buf_size *= 2;