/* @file completion; see atfile.h. */
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
#include "tui/atfile.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include "tui/fuzzy.h"
typedef struct {
char *path; /* relative to the walk's base */
const char *name;
int is_dir;
int depth;
int rank;
double fuzzy;
} atfile_entry;
static int is_delim(char c)
{
return c == ' ' || c == '\t' || c == '\n';
}
int atfile_token(const char *text, size_t cursor, size_t *start, char *query, size_t qcap, int *quoted)
{
size_t ls = cursor, quotes = 0, qpos = 0, s;
while (ls > 0 && text[ls - 1] != '\n')
ls--;
for (size_t i = ls; i < cursor; i++)
if (text[i] == '"')
quotes++, qpos = i;
if (quotes % 2 == 1 && qpos > ls && text[qpos - 1] == '@' && (qpos - 1 == ls || is_delim(text[qpos - 2]))) {
*start = qpos - 1, *quoted = 1;
(void)snprintf(query, qcap, "%.*s", (int)(cursor - qpos - 1), text + qpos + 1);
return 1;
}
s = cursor;
while (s > ls && !is_delim(text[s - 1]))
s--;
while (s < cursor && strchr("([{<`'\"", text[s]))
s++; /* opening wrappers before the path, as Pi strips them */
if (s >= cursor || text[s] != '@')
return 0;
*start = s, *quoted = 0;
(void)snprintf(query, qcap, "%.*s", (int)(cursor - s - 1), text + s + 1);
return 1;
}
/* The lister's entries, with each one's name (its last path element). */
static size_t walk(const char *base, atfile_lister list, void *ctx, atfile_entry **out)
{
atfile_found *f = NULL;
atfile_entry *v;
size_t n = list ? list(ctx, base, &f) : 0;
*out = NULL;
if (!n || !(v = calloc(n, sizeof *v))) {
for (size_t i = 0; i < n; i++)
free(f[i].path);
free(f);
return 0;
}
for (size_t i = 0; i < n; i++) {
const char *sl = strrchr(f[i].path, '/');
v[i].path = f[i].path;
v[i].is_dir = f[i].is_dir;
v[i].depth = f[i].depth;
v[i].name = sl ? sl + 1 : f[i].path;
}
free(f);
*out = v;
return n;
}
static int cmp(const void *pa, const void *pb)
{
const atfile_entry *a = pa, *b = pb;
if (a->rank != b->rank)
return b->rank - a->rank;
if (a->fuzzy != b->fuzzy)
return a->fuzzy < b->fuzzy ? -1 : 1;
if (a->depth != b->depth)
return a->depth - b->depth;
if (strlen(a->path) != strlen(b->path))
return strlen(a->path) < strlen(b->path) ? -1 : 1;
return strcmp(a->path, b->path);
}
/* Case-insensitive substring test (ASCII); strcasestr is not standard C. */
static int contains_nocase(const char *hay, const char *needle)
{
size_t n = strlen(needle);
for (; *hay; hay++)
if (strncasecmp(hay, needle, n) == 0)
return 1;
return n == 0;
}
/* Pi's scoreEntry on the name; the extra 20 is a fuzzy-only match. */
static int rank_of(const char *name, const char *q, int is_dir)
{
int s;
if (!*q)
return 1;
if (strcasecmp(name, q) == 0)
s = 100;
else if (strncasecmp(name, q, strlen(q)) == 0)
s = 80;
else if (contains_nocase(name, q))
s = 50;
else
s = 20;
return is_dir ? s + 10 : s;
}
size_t atfile_candidates(const char *root, const char *query, int quoted, atfile_item *out, size_t cap,
atfile_lister list, void *ctx)
{
char base[4096], display[4096] = "", q[512];
const char *slash = strrchr(query, '/');
atfile_entry *v = NULL;
size_t n, m = 0, k = 0;
struct stat st;
if (slash) {
(void)snprintf(display, sizeof display, "%.*s", (int)(slash - query + 1), query);
(void)snprintf(q, sizeof q, "%s", slash + 1);
if (display[0] == '/')
(void)snprintf(base, sizeof base, "%s", display);
else if (display[0] == '~' && display[1] == '/' && getenv("HOME"))
(void)snprintf(base, sizeof base, "%s/%s", getenv("HOME"), display + 2);
else
(void)snprintf(base, sizeof base, "%s/%s", root, display);
if (stat(base, &st) != 0 || !S_ISDIR(st.st_mode))
return 0;
for (size_t l = strlen(base); l > 1 && base[l - 1] == '/'; l--)
base[l - 1] = 0;
} else {
(void)snprintf(base, sizeof base, "%s", root);
(void)snprintf(q, sizeof q, "%s", query);
}
n = walk(base, list, ctx, &v);
for (size_t i = 0; i < n; i++) {
double sc = 0;
if (*q && !tui_fuzzy_match(q, v[i].name, &sc)) {
free(v[i].path);
continue;
}
v[i].rank = rank_of(v[i].name, q, v[i].is_dir);
v[i].fuzzy = sc;
v[m++] = v[i];
}
if (m > 1) { /* glibc: qsort's array may not be NULL, even empty */
qsort(v, m, sizeof *v, cmp);
}
for (size_t i = 0; i < m; i++) {
if (k < cap) {
char path[4096];
(void)snprintf(path, sizeof path, "%s%s%s", display, v[i].path, v[i].is_dir ? "/" : "");
atfile_item *it = &out[k++];
it->is_dir = v[i].is_dir;
if (quoted || strpbrk(path, " \t"))
(void)snprintf(it->value, sizeof it->value, "@\"%s\"", path);
else
(void)snprintf(it->value, sizeof it->value, "@%s", path);
(void)snprintf(it->label, sizeof it->label, "%s%s", v[i].name, v[i].is_dir ? "/" : "");
if (strcmp(path, it->label) != 0)
(void)snprintf(it->label + strlen(it->label), sizeof it->label - strlen(it->label), " %s", path);
}
free(v[i].path);
}
free(v);
return k;
}