diff options
author | Anselm R Garbe <anselm@garbe.us> | 2009-11-28 12:28:15 +0000 |
---|---|---|
committer | Anselm R Garbe <anselm@garbe.us> | 2009-11-28 12:28:15 +0000 |
commit | 569a1f925a7cf351e6ec3e38529f46a1d9becf3f (patch) | |
tree | 38e61a5eda145b5449636143ebbbc1ea13c34465 | |
parent | 0f4d3ffdb5759070682b241c6fcc864bbd329037 (diff) |
applied cursor, vertical and paste patches for upcoming 4.1 dmenu version (due to the fact that surf is using dmenu as well)
-rw-r--r-- | LICENSE | 5 | ||||
-rw-r--r-- | config.mk | 2 | ||||
-rw-r--r-- | dmenu.1 | 7 | ||||
-rw-r--r-- | dmenu.c | 163 |
4 files changed, 149 insertions, 28 deletions
@@ -1,6 +1,9 @@ | |||
1 | MIT/X Consortium License | 1 | MIT/X Consortium License |
2 | 2 | ||
3 | © 2006-2008 Anselm R. Garbe <garbeam at gmail dot com> | 3 | © 2006-2009 Anselm R. Garbe <anselm@garbe.us> |
4 | © 2009 Gottox <gottox@s01.de> | ||
5 | © 2009 Markus Schnalke <meillo@marmaro.de> | ||
6 | © 2009 Evan Gates <evan.gates@gmail.com> | ||
4 | © 2006-2008 Sander van Dijk <a dot h dot vandijk at gmail dot com> | 7 | © 2006-2008 Sander van Dijk <a dot h dot vandijk at gmail dot com> |
5 | © 2006-2007 Michał Janeczek <janeczek at gmail dot com> | 8 | © 2006-2007 Michał Janeczek <janeczek at gmail dot com> |
6 | 9 | ||
@@ -1,5 +1,5 @@ | |||
1 | # dmenu version | 1 | # dmenu version |
2 | VERSION = 4.0 | 2 | VERSION = 4.1 |
3 | 3 | ||
4 | # Customize below to fit your system | 4 | # Customize below to fit your system |
5 | 5 | ||
@@ -5,6 +5,7 @@ dmenu \- dynamic menu | |||
5 | .B dmenu | 5 | .B dmenu |
6 | .RB [ \-i ] | 6 | .RB [ \-i ] |
7 | .RB [ \-b ] | 7 | .RB [ \-b ] |
8 | .RB [ \-l " <lines>"] | ||
8 | .RB [ \-fn " <font>"] | 9 | .RB [ \-fn " <font>"] |
9 | .RB [ \-nb " <color>"] | 10 | .RB [ \-nb " <color>"] |
10 | .RB [ \-nf " <color>"] | 11 | .RB [ \-nf " <color>"] |
@@ -26,6 +27,10 @@ makes dmenu match menu entries case insensitively. | |||
26 | .B \-b | 27 | .B \-b |
27 | defines that dmenu appears at the bottom. | 28 | defines that dmenu appears at the bottom. |
28 | .TP | 29 | .TP |
30 | .B \-l <lines> | ||
31 | activates vertical list mode. | ||
32 | The given number of lines will be displayed. Window height will get adjusted. | ||
33 | .TP | ||
29 | .B \-fn <font> | 34 | .B \-fn <font> |
30 | defines the font. | 35 | defines the font. |
31 | .TP | 36 | .TP |
@@ -57,7 +62,7 @@ dmenu is completely controlled by the keyboard. The following keys are recognize | |||
57 | Appends the character to the text in the input field. This works as a filter: | 62 | Appends the character to the text in the input field. This works as a filter: |
58 | only items containing this text will be displayed. | 63 | only items containing this text will be displayed. |
59 | .TP | 64 | .TP |
60 | .B Left/Right (Mod1\-h/Mod1\-l) | 65 | .B Left/Right (Up/Down) (Mod1\-h/Mod1\-l) |
61 | Select the previous/next item. | 66 | Select the previous/next item. |
62 | .TP | 67 | .TP |
63 | .B PageUp/PageDown (Mod1\-k/Mod1\-j) | 68 | .B PageUp/PageDown (Mod1\-k/Mod1\-j) |
@@ -18,6 +18,7 @@ | |||
18 | #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask)) | 18 | #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask)) |
19 | #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH)) | 19 | #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH)) |
20 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) | 20 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
21 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) | ||
21 | 22 | ||
22 | /* enums */ | 23 | /* enums */ |
23 | enum { ColFG, ColBG, ColLast }; | 24 | enum { ColFG, ColBG, ColLast }; |
@@ -47,10 +48,12 @@ struct Item { | |||
47 | 48 | ||
48 | /* forward declarations */ | 49 | /* forward declarations */ |
49 | static void appenditem(Item *i, Item **list, Item **last); | 50 | static void appenditem(Item *i, Item **list, Item **last); |
50 | static void calcoffsets(void); | 51 | static void calcoffsetsh(void); |
52 | static void calcoffsetsv(void); | ||
51 | static char *cistrstr(const char *s, const char *sub); | 53 | static char *cistrstr(const char *s, const char *sub); |
52 | static void cleanup(void); | 54 | static void cleanup(void); |
53 | static void drawmenu(void); | 55 | static void drawmenuh(void); |
56 | static void drawmenuv(void); | ||
54 | static void drawtext(const char *text, unsigned long col[ColLast]); | 57 | static void drawtext(const char *text, unsigned long col[ColLast]); |
55 | static void eprint(const char *errstr, ...); | 58 | static void eprint(const char *errstr, ...); |
56 | static unsigned long getcolor(const char *colstr); | 59 | static unsigned long getcolor(const char *colstr); |
@@ -73,6 +76,7 @@ static char text[4096]; | |||
73 | static int cmdw = 0; | 76 | static int cmdw = 0; |
74 | static int promptw = 0; | 77 | static int promptw = 0; |
75 | static int ret = 0; | 78 | static int ret = 0; |
79 | static int cursor = 0; | ||
76 | static int screen; | 80 | static int screen; |
77 | static unsigned int mw, mh; | 81 | static unsigned int mw, mh; |
78 | static unsigned int numlockmask = 0; | 82 | static unsigned int numlockmask = 0; |
@@ -88,6 +92,10 @@ static Item *curr = NULL; | |||
88 | static Window root, win; | 92 | static Window root, win; |
89 | static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp; | 93 | static int (*fstrncmp)(const char *, const char *, size_t n) = strncmp; |
90 | static char *(*fstrstr)(const char *, const char *) = strstr; | 94 | static char *(*fstrstr)(const char *, const char *) = strstr; |
95 | static Bool vlist = False; | ||
96 | static unsigned int lines = 5; | ||
97 | static void (*calcoffsets)(void) = calcoffsetsh; | ||
98 | static void (*drawmenu)(void) = drawmenuh; | ||
91 | 99 | ||
92 | void | 100 | void |
93 | appenditem(Item *i, Item **list, Item **last) { | 101 | appenditem(Item *i, Item **list, Item **last) { |
@@ -101,7 +109,7 @@ appenditem(Item *i, Item **list, Item **last) { | |||
101 | } | 109 | } |
102 | 110 | ||
103 | void | 111 | void |
104 | calcoffsets(void) { | 112 | calcoffsetsh(void) { |
105 | int tw; | 113 | int tw; |
106 | unsigned int w; | 114 | unsigned int w; |
107 | 115 | ||
@@ -127,6 +135,26 @@ calcoffsets(void) { | |||
127 | } | 135 | } |
128 | } | 136 | } |
129 | 137 | ||
138 | void | ||
139 | calcoffsetsv(void) { | ||
140 | static unsigned int w; | ||
141 | |||
142 | if(!curr) | ||
143 | return; | ||
144 | w = (dc.font.height + 2) * (lines + 1); | ||
145 | for(next = curr; next; next=next->right) { | ||
146 | w -= dc.font.height + 2; | ||
147 | if(w <= 0) | ||
148 | break; | ||
149 | } | ||
150 | w = (dc.font.height + 2) * (lines + 1); | ||
151 | for(prev = curr; prev && prev->left; prev=prev->left) { | ||
152 | w -= dc.font.height + 2; | ||
153 | if(w <= 0) | ||
154 | break; | ||
155 | } | ||
156 | } | ||
157 | |||
130 | char * | 158 | char * |
131 | cistrstr(const char *s, const char *sub) { | 159 | cistrstr(const char *s, const char *sub) { |
132 | int c, csub; | 160 | int c, csub; |
@@ -171,7 +199,17 @@ cleanup(void) { | |||
171 | } | 199 | } |
172 | 200 | ||
173 | void | 201 | void |
174 | drawmenu(void) { | 202 | drawcursor(void) { |
203 | XRectangle r = { dc.x, dc.y + 2, 1, dc.h - 4 }; | ||
204 | |||
205 | r.x += textnw(text, cursor) + dc.font.height / 2; | ||
206 | |||
207 | XSetForeground(dpy, dc.gc, dc.norm[ColFG]); | ||
208 | XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); | ||
209 | } | ||
210 | |||
211 | void | ||
212 | drawmenuh(void) { | ||
175 | Item *i; | 213 | Item *i; |
176 | 214 | ||
177 | dc.x = 0; | 215 | dc.x = 0; |
@@ -190,6 +228,7 @@ drawmenu(void) { | |||
190 | if(cmdw && item) | 228 | if(cmdw && item) |
191 | dc.w = cmdw; | 229 | dc.w = cmdw; |
192 | drawtext(text[0] ? text : NULL, dc.norm); | 230 | drawtext(text[0] ? text : NULL, dc.norm); |
231 | drawcursor(); | ||
193 | dc.x += cmdw; | 232 | dc.x += cmdw; |
194 | if(curr) { | 233 | if(curr) { |
195 | dc.w = spaceitem; | 234 | dc.w = spaceitem; |
@@ -212,6 +251,39 @@ drawmenu(void) { | |||
212 | } | 251 | } |
213 | 252 | ||
214 | void | 253 | void |
254 | drawmenuv(void) { | ||
255 | Item *i; | ||
256 | |||
257 | dc.x = 0; | ||
258 | dc.y = 0; | ||
259 | dc.w = mw; | ||
260 | dc.h = mh; | ||
261 | drawtext(NULL, dc.norm); | ||
262 | /* print prompt? */ | ||
263 | if(promptw) { | ||
264 | dc.w = promptw; | ||
265 | drawtext(prompt, dc.sel); | ||
266 | } | ||
267 | dc.x += promptw; | ||
268 | dc.w = mw - promptw; | ||
269 | /* print command */ | ||
270 | drawtext(text[0] ? text : NULL, dc.norm); | ||
271 | if(curr) { | ||
272 | dc.x = 0; | ||
273 | dc.w = mw; | ||
274 | dc.y += dc.font.height + 2; | ||
275 | /* determine maximum items */ | ||
276 | for(i = curr; i != next; i=i->right) { | ||
277 | drawtext(i->text, (sel == i) ? dc.sel : dc.norm); | ||
278 | dc.y += dc.font.height + 2; | ||
279 | } | ||
280 | drawtext(NULL, dc.norm); | ||
281 | } | ||
282 | XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0); | ||
283 | XFlush(dpy); | ||
284 | } | ||
285 | |||
286 | void | ||
215 | drawtext(const char *text, unsigned long col[ColLast]) { | 287 | drawtext(const char *text, unsigned long col[ColLast]) { |
216 | char buf[256]; | 288 | char buf[256]; |
217 | int i, x, y, h, len, olen; | 289 | int i, x, y, h, len, olen; |
@@ -222,8 +294,8 @@ drawtext(const char *text, unsigned long col[ColLast]) { | |||
222 | if(!text) | 294 | if(!text) |
223 | return; | 295 | return; |
224 | olen = strlen(text); | 296 | olen = strlen(text); |
225 | h = dc.font.ascent + dc.font.descent; | 297 | h = dc.font.height; |
226 | y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; | 298 | y = dc.y + ((h+2) / 2) - (h / 2) + dc.font.ascent; |
227 | x = dc.x + (h / 2); | 299 | x = dc.x + (h / 2); |
228 | /* shorten text if necessary */ | 300 | /* shorten text if necessary */ |
229 | for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--); | 301 | for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--); |
@@ -353,7 +425,7 @@ kpress(XKeyEvent * e) { | |||
353 | text[0] = 0; | 425 | text[0] = 0; |
354 | match(text); | 426 | match(text); |
355 | drawmenu(); | 427 | drawmenu(); |
356 | return; | 428 | break; |
357 | case XK_w: | 429 | case XK_w: |
358 | case XK_W: | 430 | case XK_W: |
359 | if(len) { | 431 | if(len) { |
@@ -365,7 +437,7 @@ kpress(XKeyEvent * e) { | |||
365 | match(text); | 437 | match(text); |
366 | drawmenu(); | 438 | drawmenu(); |
367 | } | 439 | } |
368 | return; | 440 | break; |
369 | } | 441 | } |
370 | } | 442 | } |
371 | if(CLEANMASK(e->state) & Mod1Mask) { | 443 | if(CLEANMASK(e->state) & Mod1Mask) { |
@@ -389,19 +461,39 @@ kpress(XKeyEvent * e) { | |||
389 | case XK_G: | 461 | case XK_G: |
390 | ksym = XK_End; | 462 | ksym = XK_End; |
391 | break; | 463 | break; |
464 | case XK_p: | ||
465 | { | ||
466 | FILE *fp; | ||
467 | char *c; | ||
468 | if(!(fp = (FILE*)popen("sselp", "r"))) | ||
469 | fprintf(stderr, "dmenu: Could not popen sselp\n"); | ||
470 | c = fgets(text + len, sizeof(text) - len, fp); | ||
471 | pclose(fp); | ||
472 | if(c == NULL) | ||
473 | return; | ||
474 | } | ||
475 | len = strlen(text); | ||
476 | if(len && text[len-1] == '\n') | ||
477 | text[--len] = '\0'; | ||
478 | match(text); | ||
479 | drawmenu(); | ||
480 | return; | ||
392 | } | 481 | } |
393 | } | 482 | } |
394 | switch(ksym) { | 483 | switch(ksym) { |
395 | default: | 484 | default: |
396 | if(num && !iscntrl((int) buf[0])) { | 485 | if(num && !iscntrl((int) buf[0])) { |
397 | buf[num] = 0; | 486 | buf[num] = 0; |
398 | strncpy(text + len, buf, sizeof text - len); | 487 | memmove(text + cursor + num, text + cursor, sizeof text - cursor); |
488 | strncpy(text + cursor, buf, sizeof text - cursor); | ||
489 | cursor+=num; | ||
399 | match(text); | 490 | match(text); |
400 | } | 491 | } |
401 | break; | 492 | break; |
402 | case XK_BackSpace: | 493 | case XK_BackSpace: |
403 | if(len) { | 494 | if(cursor > 0) { |
404 | text[--len] = 0; | 495 | memmove(text + cursor + -1, text + cursor, sizeof text - cursor); |
496 | cursor--; | ||
405 | match(text); | 497 | match(text); |
406 | } | 498 | } |
407 | break; | 499 | break; |
@@ -426,13 +518,18 @@ kpress(XKeyEvent * e) { | |||
426 | calcoffsets(); | 518 | calcoffsets(); |
427 | break; | 519 | break; |
428 | case XK_Left: | 520 | case XK_Left: |
429 | if(!(sel && sel->left)) | 521 | case XK_Up: |
430 | return; | 522 | if(sel && sel->left){ |
431 | sel=sel->left; | 523 | sel=sel->left; |
432 | if(sel->right == curr) { | 524 | if(sel->right == curr) { |
433 | curr = prev; | 525 | curr = prev; |
434 | calcoffsets(); | 526 | calcoffsets(); |
527 | } | ||
435 | } | 528 | } |
529 | else if(cursor > 0) | ||
530 | cursor--; | ||
531 | else | ||
532 | return; | ||
436 | break; | 533 | break; |
437 | case XK_Next: | 534 | case XK_Next: |
438 | if(!next) | 535 | if(!next) |
@@ -457,21 +554,30 @@ kpress(XKeyEvent * e) { | |||
457 | running = False; | 554 | running = False; |
458 | break; | 555 | break; |
459 | case XK_Right: | 556 | case XK_Right: |
460 | if(!(sel && sel->right)) | 557 | case XK_Down: |
461 | return; | 558 | if(cursor < len) |
462 | sel=sel->right; | 559 | cursor++; |
463 | if(sel == next) { | 560 | else if(sel && sel->right) { |
464 | curr = next; | 561 | sel=sel->right; |
465 | calcoffsets(); | 562 | if(sel == next) { |
563 | curr = next; | ||
564 | calcoffsets(); | ||
565 | } | ||
466 | } | 566 | } |
567 | else | ||
568 | return; | ||
467 | break; | 569 | break; |
468 | case XK_Tab: | 570 | case XK_Tab: |
469 | if(!sel) | 571 | if(!sel) |
470 | return; | 572 | return; |
471 | strncpy(text, sel->text, sizeof text); | 573 | strncpy(text, sel->text, sizeof text); |
574 | cursor = strlen(text); | ||
472 | match(text); | 575 | match(text); |
473 | break; | 576 | break; |
474 | } | 577 | } |
578 | len = strlen(text); | ||
579 | cursor = MIN(cursor, len); | ||
580 | cursor = MAX(cursor, 0); | ||
475 | drawmenu(); | 581 | drawmenu(); |
476 | } | 582 | } |
477 | 583 | ||
@@ -598,6 +704,7 @@ setup(Bool topbar) { | |||
598 | 704 | ||
599 | /* menu window geometry */ | 705 | /* menu window geometry */ |
600 | mh = dc.font.height + 2; | 706 | mh = dc.font.height + 2; |
707 | mh = vlist ? mh * (lines+1) : mh; | ||
601 | #if XINERAMA | 708 | #if XINERAMA |
602 | if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) { | 709 | if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) { |
603 | i = 0; | 710 | i = 0; |
@@ -676,6 +783,12 @@ main(int argc, char *argv[]) { | |||
676 | } | 783 | } |
677 | else if(!strcmp(argv[i], "-b")) | 784 | else if(!strcmp(argv[i], "-b")) |
678 | topbar = False; | 785 | topbar = False; |
786 | else if(!strcmp(argv[i], "-l")) { | ||
787 | vlist = True; | ||
788 | calcoffsets = calcoffsetsv; | ||
789 | drawmenu = drawmenuv; | ||
790 | if(++i < argc) lines += atoi(argv[i]); | ||
791 | } | ||
679 | else if(!strcmp(argv[i], "-fn")) { | 792 | else if(!strcmp(argv[i], "-fn")) { |
680 | if(++i < argc) font = argv[i]; | 793 | if(++i < argc) font = argv[i]; |
681 | } | 794 | } |
@@ -695,9 +808,9 @@ main(int argc, char *argv[]) { | |||
695 | if(++i < argc) selfgcolor = argv[i]; | 808 | if(++i < argc) selfgcolor = argv[i]; |
696 | } | 809 | } |
697 | else if(!strcmp(argv[i], "-v")) | 810 | else if(!strcmp(argv[i], "-v")) |
698 | eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n"); | 811 | eprint("dmenu-"VERSION", © 2006-2009 dmenu engineers, see LICENSE for details\n"); |
699 | else | 812 | else |
700 | eprint("usage: dmenu [-i] [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n" | 813 | eprint("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>] [-nf <color>]\n" |
701 | " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n"); | 814 | " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n"); |
702 | if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) | 815 | if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) |
703 | fprintf(stderr, "warning: no locale support\n"); | 816 | fprintf(stderr, "warning: no locale support\n"); |