Hi,

small patch against dmenu-1.4 to provide a history.

Define the history file (HIST_FILE) and history size (HIST_SIZE) in
dmenu.h.

An archive with the patch applied can be found here:
http://gotmor.googlepages.com/dmenu-history.tar.bz2

Patch file attached to this message.

Greets, Rob
-- 
Drag me, drop me, treat me like an object!
diff -r 32e4e80d659d dmenu.h
--- a/dmenu.h   Thu Oct 26 12:14:03 2006 +0200
+++ b/dmenu.h   Sat Oct 28 12:12:07 2006 +0200
@@ -11,6 +11,9 @@
 #define SELBGCOLOR             "#666699"
 #define SELFGCOLOR             "#eeeeee"
 #define SPACE          30 /* px */
+
+#define HIST_FILE "/home/robert/.dmenu_history"
+#define HIST_SIZE 20
 
 /* color */
 enum { ColFG, ColBG, ColLast };
diff -r 32e4e80d659d main.c
--- a/main.c    Thu Oct 26 12:14:03 2006 +0200
+++ b/main.c    Sat Oct 28 12:31:57 2006 +0200
@@ -14,6 +14,7 @@
 #include <X11/Xutil.h>
 #include <X11/keysym.h>
 
+
 typedef struct Item Item;
 struct Item {
        Item *next;             /* traverses all items */
@@ -24,6 +25,8 @@ struct Item {
 /* static */
 
 static char text[4096];
+static char hist[HIST_SIZE][1024];
+static int hcnt = 0;
 static int mx, my, mw, mh;
 static int ret = 0;
 static int nitem = 0;
@@ -62,6 +65,45 @@ calcoffsets(void) {
                if(w > mw)
                        break;
        }
+}
+
+
+static int
+writehistory(char *command) {
+    int i = 0, j = hcnt;
+    FILE *f;
+
+    if( (f = fopen(HIST_FILE, "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( (f = fopen(HIST_FILE, "r+")) ) {
+        while(fgets(buf, sizeof buf, f)) { 
+            strncpy(hist[hcnt++], buf, (strlen(buf) <= 1024) ? strlen(buf): 
1024 );
+        }
+    fclose(f);
+    }
+
+    return hcnt;
+
 }
 
 static void
@@ -194,13 +236,17 @@ kpress(XKeyEvent * e) {
                }
                break;
        case XK_Return:
-               if((e->state & ShiftMask) && text)
+               if((e->state & ShiftMask) && text) 
                        fprintf(stdout, "%s", text);
-               else if(sel)
+               else if(sel) 
                        fprintf(stdout, "%s", sel->text);
                else if(text)
                        fprintf(stdout, "%s", text);
-               fflush(stdout);
+
+                (sel == NULL) ? writehistory(text) : writehistory(sel->text);
+
+
+                fflush(stdout);
                running = False;
                break;
        case XK_Escape:
@@ -236,8 +282,33 @@ readstdin(void) {
        char *p, buf[1024];
        unsigned int len = 0, max = 0;
        Item *i, *new;
+        int k;
 
        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 = estrdup(hist[k]);
+               if(max < len) {
+                       maxname = p;
+                       max = len;
+               }
+               new = emalloc(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')
@@ -259,6 +330,7 @@ readstdin(void) {
 
        return maxname;
 }
+
 
 /* extern */
 

Reply via email to