Skip to content

Instantly share code, notes, and snippets.

@erichschroeter
Created October 9, 2012 20:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erichschroeter/3861386 to your computer and use it in GitHub Desktop.
Save erichschroeter/3861386 to your computer and use it in GitHub Desktop.
dmenu filename tab completion
--- dmenu.c 2012-10-09 15:30:08.605004976 -0500
+++ dmenu.c 2012-10-09 15:46:59.318016781 -0500
@@ -5,6 +5,7 @@
#include <string.h>
#include <strings.h>
#include <unistd.h>
+#include <wordexp.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
@@ -32,6 +32,7 @@
static void insert(const char *str, ssize_t n);
static void keypress(XKeyEvent *ev);
static void match(void);
+static void matchfile(char *filestart);
static size_t nextrune(int inc);
static void paste(void);
static void readstdin(void);
@@ -378,6 +378,11 @@
}
break;
case XK_Tab:
+ if( strchr(text, ' ')!=NULL ) {
+ matchfile( strchr(text, ' ')+1 );
+ cursor = strlen(text);
+ break;
+ }
if(!sel)
return;
strncpy(text, sel->text, sizeof text);
@@ -467,6 +467,34 @@
}
void
+matchfile(char *filestart) {
+ wordexp_t exp;
+ int i, j, k, p=strlen(filestart);
+ filestart[ p+1 ] = 0;
+ filestart[ p ] = '*';
+
+ wordexp(filestart, &exp, 0);
+ if( exp.we_wordc > 0 ) {
+ for(j=0,i=0; exp.we_wordv[0][i]!=0; i++,j++) {
+ if( exp.we_wordv[0][i]==' ' ) filestart[j++]='\\';
+ filestart[j]=exp.we_wordv[0][i];
+ }
+ filestart[j]=0;
+
+ for(k=1; k<exp.we_wordc; k++) /* comment this block for first-completion */
+ for(j=0, i=0; exp.we_wordv[k][i]; i++,j++) {
+ if( filestart[j]=='\\' ) j++;
+ if( filestart[j]!=exp.we_wordv[k][i] ) {
+ filestart[j]=0;
+ break;
+ }
+ }
+ } else {
+ filestart[ p ] = 0;
+ }
+ wordfree(&exp);
+}
+void
readstdin(void) {
char buf[sizeof text], *p, *maxstr = NULL;
size_t i, max = 0, size = 0;
@ezyang
Copy link

ezyang commented Jun 29, 2016

This patch is a bit buggy. It doesn't update the cursor position correctly, and stuffs in wildcards when it can't find anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment