&(Rule){ LAUNCH, "man $M2 \"$M1\" | col -b | tide -", NULL },
&(Rule){ COMPLETE, NULL, NULL }
},
- (Rule*[]){ /* If it's an existing directory, open it with system default */
+ (Rule*[]){ /* If it's an existing directory, open it tide */
&(Rule){ ISDIR, "$data", NULL },
- &(Rule){ LAUNCH, "cd $data && ls -ap | tide -", NULL },
+ &(Rule){ LAUNCH, "edit \"$data\"", NULL },
&(Rule){ COMPLETE, NULL, NULL }
},
(Rule*[]){ /* look for files in /usr/include */
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
+#include <dirent.h>
+#include <vec.h>
#include "config.h"
size_t gapbuf_end(GapBuf* buf)
}
}
+static int cmpnames(const void* a, const void* b)
+{
+ char *sa = *((char**)a), *sb = *((char**)b);
+ return strcmp(sa, sb);
+}
+
+static void loaddir(GapBuf* buf, char* path, int fd)
+{
+ Sel sel = { 0 };
+ vec_t entries;
+ vec_init(&entries, sizeof(char*));
+ DIR* dir = fdopendir(fd);
+ struct dirent* entry = NULL;
+ struct stat attrs = {0};
+ while ( (entry = readdir(dir)) )
+ {
+ char* epath = strmcat(path, "/", entry->d_name, 0);
+ memset(&attrs, 0, sizeof(attrs));
+ stat(epath, &attrs);
+ char* ename = strmcat(entry->d_name, (S_ISDIR(attrs.st_mode) ? "/" : 0), 0);
+ vec_push_back(&entries, &ename);
+ free(epath);
+ }
+ vec_sort(&entries, cmpnames);
+ for (int i = 0; i < vec_size(&entries); i++)
+ {
+ char* name = *((char**)vec_at(&entries, i));
+ for (int i = 0; name[i]; i++)
+ {
+ gapbuf_putb(buf, name[i], &sel);
+ }
+ gapbuf_putb(buf, '\n', &sel);
+ free(name);
+ }
+ free(entries.elem_buffer);
+ chdir(path);
+}
+
void gapbuf_init(GapBuf* buf)
{
require(buf != NULL);
buf->bufsize = pagealign(sb.st_size);
buf->bufstart = malloc(buf->bufsize);
buf->bufend = buf->bufstart + buf->bufsize;
- buf->gapstart = buf->bufstart + sb.st_size;
+ buf->gapstart = buf->bufstart;
buf->gapend = buf->bufend;
- (void)readfd(fd, buf->bufstart, sb.st_size);
+
+ if (S_ISDIR(sb.st_mode))
+ {
+ loaddir(buf, path, fd);
+ }
+ else
+ {
+ buf->gapstart += sb.st_size;
+ (void)readfd(fd, buf->bufstart, sb.st_size);
+ }
}
if (fd > 0)
{