sites

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit fa3642e02851de912abe841b2b479c5195ee935c
parent c4a9dc23b4aec1e979ff70512611454c2708efbf
Author: Rob Pilling <my name at gmail dot com>
Date:   Sat, 28 Aug 2010 18:37:54 +0100

Updated dmenu history patch
Diffstat:
Mtools.suckless.org/dmenu/patches/dmenu-tip-history.diff | 246++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 125 insertions(+), 121 deletions(-)

diff --git a/tools.suckless.org/dmenu/patches/dmenu-tip-history.diff b/tools.suckless.org/dmenu/patches/dmenu-tip-history.diff @@ -1,145 +1,149 @@ -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>"] +diff -r 23bd778df432 dmenu.1 +--- a/dmenu.1 Fri Aug 20 19:57:13 2010 +0100 ++++ b/dmenu.1 Sat Aug 28 18:34:49 2010 +0100 +@@ -19,6 +19,8 @@ + .IR color ] + .RB [ \-sf + .IR color ] ++.RB [ \-hist ++.IR "<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)) + .P + .BR dmenu_run " ..." +@@ -72,6 +74,9 @@ + .BI \-sf " color" + defines the selected foreground color. + .TP ++.BI \-hist " <histfile>" ++the file to use for history ++.TP + .B \-v + prints version information to standard output, then exits. + .SH USAGE +diff -r 23bd778df432 dmenu.c +--- a/dmenu.c Fri Aug 20 19:57:13 2010 +0100 ++++ b/dmenu.c Sat Aug 28 18:34:49 2010 +0100 +@@ -16,6 +16,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; + typedef struct Item Item; + struct Item { + char *text; +@@ -33,7 +35,7 @@ + static void match(void); + static size_t nextrune(int incr); + static void paste(void); +-static void readstdin(void); ++static void readitems(void); + static void run(void); + static void setup(void); + static void usage(void); +@@ -60,8 +62,37 @@ + static Item *prev, *curr, *next; + static Window root, win; +static char hist[HIST_SIZE][1024]; +static char *histfile = NULL; +static int hcnt = 0; + + static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; + +static int +writehistory(char *command) { -+ int i = 0, j = hcnt; -+ FILE *f; ++ int i = 0; ++ FILE *f; + -+ if(!histfile || strlen(command) <= 0) -+ return 0; ++ 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; -+ } ++ if((f = fopen(histfile, "w"))) { ++ fputs(command, f); ++ fputc('\n', f); ++ for(; i < hcnt; i++) { ++ if(strcmp(command, hist[i]) != 0) { ++ fputs(hist[i], f); ++ fputc('\n', f); ++ } ++ } ++ fclose(f); ++ return 1; ++ } + -+ return 0; ++ 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); + appenditem(Item *item, Item **list, Item **last) { + if(!*last) +@@ -296,6 +327,7 @@ + case XK_KP_Enter: + fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout); 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; ++ writehistory( (sel == NULL) ? text : sel->text); + exit(EXIT_SUCCESS); + case XK_Right: + if(cursor < len) { +@@ -385,9 +417,10 @@ + } -- 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; + void +-readstdin(void) { ++readitems(void) { + char buf[sizeof text], *p; + Item *item, **end; ++ FILE *f; + + for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) { + if((p = strchr(buf, '\n'))) +@@ -399,6 +432,22 @@ + item->next = item->left = item->right = NULL; + inputw = MAX(inputw, dc_textw(dc, item->text)); + } + ++ if(histfile && (f = fopen(histfile, "r"))) { ++ for(; fgets(buf, sizeof buf, f); *end = item, end = &item->next) { ++ if((p = strchr(buf, '\n'))) ++ *p = '\0'; ++ if(!(item = malloc(sizeof *item))) ++ eprintf("cannot malloc %u bytes\n", sizeof *item); ++ if(!(item->text = strdup(buf))) ++ eprintf("cannot strdup %u bytes\n", strlen(buf)+1); ++ item->next = item->left = item->right = NULL; ++ inputw = MAX(inputw, dc_textw(dc, item->text)); + - 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]; ++ strncpy(hist[hcnt++], buf, (strlen(buf) <= 1024) ? strlen(buf): 1024 ); + } - else if(!strcmp(argv[i], "-v")) - eprint("dmenu-"VERSION", © 2006-2009 dmenu engineers, see LICENSE for details\n"); ++ fclose(f); ++ } + } + + void +@@ -490,7 +539,7 @@ + void + usage(void) { + fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n" +- " [-nf color] [-sb color] [-sf color] [-v]\n", stderr); ++ " [-nf color] [-sb color] [-sf color] [-hist histfile] [-v]\n", stderr); + exit(EXIT_FAILURE); + } + +@@ -526,12 +575,14 @@ + selbgcolor = argv[++i]; + else if(!strcmp(argv[i], "-sf")) + selfgcolor = argv[++i]; ++ else if(!strcmp(argv[i], "-hist")) ++ histfile = argv[++i]; 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))) + usage(); + + dc = dc_init(); + dc_font(dc, font); +- readstdin(); ++ readitems(); + setup(); + run(); +