Hi folks,

Here's a history patch patched to dmenu tip.  This is pretty useful in
conjunction with surf.  I point the surf "^G" to:

dmenu -hist /home/peterjh/.dmenu.history -b -l 10 < ~/.surf/history

(Of course, it only makes sense in conjunct with the surf history patch as 
well.)

Honestly, it is the best thing ever.

If I could figure out how to update the suckless.org wiki, I'd post the
patch up there too.  Anyone know how?

Peter


--
sic dicit magister P.
http://individual.utoronto.ca/peterjh/
diff -r 3c3a635d3de6 dmenu.1
--- a/dmenu.1   Tue Dec 15 09:52:52 2009 -0500
+++ b/dmenu.1   Tue Dec 15 09:53:41 2009 -0500
@@ -12,6 +12,7 @@
 .RB [ \-p " <prompt>"]
 .RB [ \-sb " <color>"]
 .RB [ \-sf " <color>"]
+.RB [ \-hist " <filename>"]
 .RB [ \-v ]
 .SH DESCRIPTION
 .SS Overview
diff -r 3c3a635d3de6 dmenu.c
--- a/dmenu.c   Tue Dec 15 09:52:52 2009 -0500
+++ b/dmenu.c   Tue Dec 15 09:53:41 2009 -0500
@@ -20,6 +20,8 @@
 #define MIN(a, b)               ((a) < (b) ? (a) : (b))
 #define MAX(a, b)               ((a) > (b) ? (a) : (b))
 
+#define HIST_SIZE 20
+
 /* enums */
 enum { ColFG, ColBG, ColLast };
 
@@ -97,6 +99,52 @@
 static void (*calcoffsets)(void) = calcoffsetsh;
 static void (*drawmenu)(void) = drawmenuh;
 
+static char hist[HIST_SIZE][1024];
+static char *histfile = NULL;
+static int hcnt = 0;
+
+static int
+writehistory(char *command) {
+   int i = 0, j = hcnt;
+   FILE *f;
+
+   if(!histfile || strlen(command) <= 0)
+      return 0;
+
+   if( (f = fopen(histfile, "w")) ) {
+      fputs(command, f);
+         fputc('\n', f);
+      for(; i<HIST_SIZE && i<j; i++) {
+         if(strcmp(command, hist[i]) != 0) {
+            fputs(hist[i], f);
+            fputc('\n', f);
+         }
+      }
+      fclose(f);
+      return 1;
+   }
+
+   return 0;
+}
+
+static int
+readhistory (void) {
+   char buf[1024];
+   FILE *f;
+
+
+   if(!histfile)
+      return 0;
+
+   if( (f = fopen(histfile, "r+")) ) {
+      while(fgets(buf, sizeof buf, f) && (hcnt < HIST_SIZE))  
+         strncpy(hist[hcnt++], buf, (strlen(buf) <= 1024) ? strlen(buf): 1024 
);
+      fclose(f);
+   }
+
+   return hcnt;
+}
+
 void
 appenditem(Item *i, Item **list, Item **last) {
        if(!(*last))
@@ -245,7 +293,7 @@
                dc.x = mw - spaceitem;
                dc.w = spaceitem;
                drawtext(next ? ">" : NULL, dc.norm);
-       }
+       } 
        XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
        XFlush(dpy);
 }
@@ -551,6 +599,7 @@
                        fprintf(stdout, "%s", sel->text);
                else if(*text)
                        fprintf(stdout, "%s", text);
+               writehistory( (sel == NULL) ? text : sel->text);
                fflush(stdout);
                running = False;
                break;
@@ -628,8 +677,34 @@
        char *p, buf[1024];
        unsigned int len = 0, max = 0;
        Item *i, *new;
+       int k;
+       
+       i = 0;
 
-       i = 0;
+       if( readhistory() )  {
+               for(k=0; k<hcnt; k++) {
+                       len = strlen(hist[k]);
+                       if (hist[k][len - 1] == '\n')
+                               hist[k][len - 1] = 0;
+                       p = strdup(hist[k]);
+                       if(max < len) {
+                               maxname = p;
+                               max = len;
+                       }
+                       if(!(new = (Item *)malloc(sizeof(Item))))
+                               eprint("fatal: could not malloc() %u bytes\n", 
sizeof(Item));
+                       new->next = new->left = new->right = NULL;
+                       new->text = p;
+                       if(!i)
+                               allitems = new;
+                       else 
+                               i->next = new;
+                       i = new;
+               }
+       }
+       len=0; max=0;
+
+
        while(fgets(buf, sizeof buf, stdin)) {
                len = strlen(buf);
                if (buf[len - 1] == '\n')
@@ -808,11 +883,14 @@
                else if(!strcmp(argv[i], "-sf")) {
                        if(++i < argc) selfgcolor = argv[i];
                }
+               else if(!strcmp(argv[i], "-hist")) {
+                       if(++i < argc) histfile = argv[i];
+               }
                else if(!strcmp(argv[i], "-v"))
                        eprint("dmenu-"VERSION", © 2006-2009 dmenu engineers, 
see LICENSE for details\n");
                else
                        eprint("usage: dmenu [-i] [-b] [-l <lines>] [-fn 
<font>] [-nb <color>] [-nf <color>]\n"
-                              "             [-p <prompt>] [-sb <color>] [-sf 
<color>] [-v]\n");
+                              "             [-p <prompt>] [-hist <file> ] [-sb 
<color>] [-sf <color>] [-v]\n");
        if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
                fprintf(stderr, "warning: no locale support\n");
        if(!(dpy = XOpenDisplay(NULL)))

Reply via email to