[hackers] [dmenu] efficiency tweaks || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Thu, 14 Jul 2011 21:06:58 +0200 (CEST)

changeset: 428:c0080bf8e22c
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Thu Jul 14 20:03:08 2011 +0100
files: dmenu.c draw.c
description:
efficiency tweaks

diff -r 6e9a233829f2 -r c0080bf8e22c dmenu.c
--- a/dmenu.c Thu Jul 14 20:03:00 2011 +0100
+++ b/dmenu.c Thu Jul 14 20:03:08 2011 +0100
@@ -25,8 +25,8 @@
 
 static void appenditem(Item *item, Item **list, Item **last);
 static void calcoffsets(void);
+static char *cistrstr(const char *s, const char *sub);
 static void drawmenu(void);
-static char *fstrstr(const char *s, const char *sub);
 static void grabkeyboard(void);
 static void insert(const char *str, ssize_t n);
 static void keypress(XKeyEvent *ev);
@@ -60,6 +60,7 @@
 static Window win;
 
 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
+static char *(*fstrstr)(const char *, const char *) = strstr;
 
 int
 main(int argc, char *argv[]) {
@@ -76,8 +77,10 @@
                         topbar = False;
                 else if(!strcmp(argv[i], "-f"))
                         fast = True;
- else if(!strcmp(argv[i], "-i"))
+ else if(!strcmp(argv[i], "-i")) {
                         fstrncmp = strncasecmp;
+ fstrstr = cistrstr;
+ }
                 else if(i+1 == argc)
                         usage();
                 /* double flags */
@@ -112,7 +115,7 @@
         setup();
         run();
 
- return EXIT_FAILURE; /* should not reach */
+ return EXIT_FAILURE; /* unreachable */
 }
 
 void
@@ -121,6 +124,7 @@
                 *list = item;
         else
                 (*last)->right = item;
+
         item->left = *last;
         item->right = NULL;
         *last = item;
@@ -143,6 +147,16 @@
                         break;
 }
 
+char *
+cistrstr(const char *s, const char *sub) {
+ size_t len;
+
+ for(len = strlen(sub); *s; s++)
+ if(!strncasecmp(s, sub, len))
+ return (char *)s;
+ return NULL;
+}
+
 void
 drawmenu(void) {
         int curpos;
@@ -188,16 +202,6 @@
         mapdc(dc, win, mw, mh);
 }
 
-char *
-fstrstr(const char *s, const char *sub) {
- size_t len;
-
- for(len = strlen(sub); *s; s++)
- if(!fstrncmp(s, sub, len))
- return (char *)s;
- return NULL;
-}
-
 void
 grabkeyboard(void) {
         int i;
@@ -233,58 +237,37 @@
 
                 XConvertCase(ksym, &lower, &upper);
                 switch(lower) {
- default:
- return;
- case XK_a:
- ksym = XK_Home;
- break;
- case XK_b:
- ksym = XK_Left;
- break;
- case XK_c:
- ksym = XK_Escape;
- break;
- case XK_d:
- ksym = XK_Delete;
- break;
- case XK_e:
- ksym = XK_End;
- break;
- case XK_f:
- ksym = XK_Right;
- break;
- case XK_h:
- ksym = XK_BackSpace;
- break;
- case XK_i:
- ksym = XK_Tab;
- break;
- case XK_j:
- case XK_m:
- ksym = XK_Return;
- break;
- case XK_k: /* delete right */
+ case XK_a: ksym = XK_Home; break;
+ case XK_b: ksym = XK_Left; break;
+ case XK_c: ksym = XK_Escape; break;
+ case XK_d: ksym = XK_Delete; break;
+ case XK_e: ksym = XK_End; break;
+ case XK_f: ksym = XK_Right; break;
+ case XK_h: ksym = XK_BackSpace; break;
+ case XK_i: ksym = XK_Tab; break;
+ case XK_j: ksym = XK_Return; break;
+ case XK_m: ksym = XK_Return; break;
+ case XK_n: ksym = XK_Up; break;
+ case XK_p: ksym = XK_Down; break;
+
+ case XK_k: /* delete right */
                         text[cursor] = '\0';
                         match(False);
                         break;
- case XK_n:
- ksym = XK_Next;
- break;
- case XK_p:
- ksym = XK_Prior;
- break;
- case XK_u: /* delete left */
+ case XK_u: /* delete left */
                         insert(NULL, 0 - cursor);
                         break;
- case XK_w: /* delete word */
+ case XK_w: /* delete word */
                         while(cursor > 0 && text[nextrune(-1)] == ' ')
                                 insert(NULL, nextrune(-1) - cursor);
                         while(cursor > 0 && text[nextrune(-1)] != ' ')
                                 insert(NULL, nextrune(-1) - cursor);
                         break;
- case XK_y: /* paste selection */
+ case XK_y: /* paste selection */
                         XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
                         return;
+ default:
+ return;
                 }
         }
         switch(ksym) {
@@ -297,8 +280,9 @@
                         return;
                 cursor = nextrune(+1);
         case XK_BackSpace:
- if(cursor > 0)
- insert(NULL, nextrune(-1) - cursor);
+ if(cursor == 0)
+ return;
+ insert(NULL, nextrune(-1) - cursor);
                 break;
         case XK_End:
                 if(text[cursor] != '\0') {
@@ -351,7 +335,7 @@
                 break;
         case XK_Return:
         case XK_KP_Enter:
- fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
+ puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
                 exit(EXIT_SUCCESS);
         case XK_Right:
                 if(text[cursor] != '\0') {
@@ -468,7 +452,7 @@
                 switch(ev.type) {
                 case Expose:
                         if(ev.xexpose.count == 0)
- drawmenu();
+ mapdc(dc, win, mw, mh);
                         break;
                 case KeyPress:
                         keypress(&ev.xkey);
diff -r 6e9a233829f2 -r c0080bf8e22c draw.c
--- a/draw.c Thu Jul 14 20:03:00 2011 +0100
+++ b/draw.c Thu Jul 14 20:03:08 2011 +0100
@@ -96,7 +96,7 @@
         DC *dc;
 
         if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
- fprintf(stderr, "no locale support\n");
+ fputs("no locale support\n", stderr);
         if(!(dc = calloc(1, sizeof *dc)))
                 eprintf("cannot malloc %u bytes:", sizeof *dc);
         if(!(dc->dpy = XOpenDisplay(NULL)))
@@ -153,10 +153,10 @@
         if(dc->canvas)
                 XFreePixmap(dc->dpy, dc->canvas);
 
+ dc->w = w;
+ dc->h = h;
         dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
                                    DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
- dc->w = w;
- dc->h = h;
 }
 
 int
Received on Thu Jul 14 2011 - 21:06:58 CEST

This archive was generated by hypermail 2.2.0 : Thu Jul 14 2011 - 21:12:05 CEST