[wiki] [sites] wiki updated

From: <hg_AT_suckless.org>
Date: Sat, 28 Aug 2010 17:26:24 +0000 (UTC)

changeset: 619:3bf24375f0eb
tag: tip
user: Rob Pilling <my name at gmail dot com>
date: Sat Aug 28 18:37:54 2010 +0100
files: tools.suckless.org/dmenu/patches/dmenu-tip-history.diff
description:
Updated dmenu history patch


diff -r 6e4cc3ecd57c -r 3bf24375f0eb tools.suckless.org/dmenu/patches/dmenu-tip-history.diff
--- a/tools.suckless.org/dmenu/patches/dmenu-tip-history.diff Sat Aug 28 22:48:09 2010 +1000
+++ b/tools.suckless.org/dmenu/patches/dmenu-tip-history.diff Sat Aug 28 18:37:54 2010 +0100
_AT_@ -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
-_AT_@ -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
+_AT_@ -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
-_AT_@ -20,6 +20,8 @@
- #define MIN(a, b) ((a) < (b) ? (a) : (b))
- #define MAX(a, b) ((a) > (b) ? (a) : (b))
+ .P
+ .BR dmenu_run " ..."
+_AT_@ -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
+_AT_@ -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 };
-
-_AT_@ -97,6 +99,52 @@
- static void (*calcoffsets)(void) = calcoffsetsh;
- static void (*drawmenu)(void) = drawmenuh;
+ typedef struct Item Item;
+ struct Item {
+ char *text;
+_AT_@ -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);
+_AT_@ -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))
-_AT_@ -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);
+ appenditem(Item *item, Item **list, Item **last) {
+ if(!*last)
+_AT_@ -296,6 +327,7 @@
+ case XK_KP_Enter:
+ fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
+ fflush(stdout);
++ writehistory( (sel == NULL) ? text : sel->text);
+ exit(EXIT_SUCCESS);
+ case XK_Right:
+ if(cursor < len) {
+_AT_@ -385,9 +417,10 @@
  }
-_AT_@ -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;
-_AT_@ -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;
+ 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')))
+_AT_@ -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));
++
++ strncpy(hist[hcnt++], buf, (strlen(buf) <= 1024) ? strlen(buf): 1024 );
 + }
++ fclose(f);
 + }
-+ len=0; max=0;
-+
-+
- while(fgets(buf, sizeof buf, stdin)) {
- len = strlen(buf);
- if (buf[len - 1] == '\n')
-_AT_@ -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");
+ }
+
+ void
+_AT_@ -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);
+ }
+
+_AT_@ -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();
+
Received on Sat Aug 28 2010 - 19:26:24 CEST

This archive was generated by hypermail 2.3.0 : Thu Sep 13 2012 - 19:31:27 CEST