From 7a585c9992c1ee706520426704f32b74818f3f99 Mon Sep 17 00:00:00 2001 From: Spenser Truex Date: Fri, 18 Sep 2020 05:57:23 -0700 Subject: [dmenu][PATCH] Vim-like vertical and horizontal keybinding swap To: suckless Suckless Hackers Old: Alt-h,l moves one element Alt-j,k moves one page New: Alt-h,l moves left/right or pages on a vertical listing Alt-j,k moves up/down on a vertical listing or pages a horizontal one --- config.def.h | 5 ++++- dmenu.c | 37 +++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/config.def.h b/config.def.h index 1edb647..0d7748d 100644 --- a/config.def.h +++ b/config.def.h @@ -13,8 +13,11 @@ static const char *colors[SchemeLast][2] = { [SchemeSel] = { "#eeeeee", "#005577" }, [SchemeOut] = { "#000000", "#00ffff" }, }; -/* -l option; if nonzero, dmenu uses vertical list with given number of lines */ +/* -l option sets both of these to true */ +/* if nonzero, dmenu uses given number of lines */ static unsigned int lines = 0; +/* If nonzero, show vertical instead of a horizontal listing */ +static unsigned int vertical = 0; /* * Characters not considered part of a word while deleting words diff --git a/dmenu.c b/dmenu.c index 65f25ce..cb28913 100644 --- a/dmenu.c +++ b/dmenu.c @@ -76,16 +76,16 @@ calcoffsets(void) { int i, n; - if (lines > 0) + if (vertical) n = lines * bh; else n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">")); /* calculate which items will begin the next page and previous page */ for (i = 0, next = curr; next; next = next->right) - if ((i += (lines > 0) ? bh : MIN(TEXTW(next->text), n)) > n) + if ((i += vertical ? bh : MIN(TEXTW(next->text), n)) > n) break; for (i = 0, prev = curr; prev && prev->left; prev = prev->left) - if ((i += (lines > 0) ? bh : MIN(TEXTW(prev->left->text), n)) > n) + if ((i += vertical ? bh : MIN(TEXTW(prev->left->text), n)) > n) break; } @@ -141,7 +141,7 @@ drawmenu(void) x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0); } /* draw input field */ - w = (lines > 0 || !matches) ? mw - x : inputw; + w = (vertical || !matches) ? mw - x : inputw; drw_setscheme(drw, scheme[SchemeNorm]); drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0); @@ -151,7 +151,7 @@ drawmenu(void) drw_rect(drw, x + curpos, 2, 2, bh - 4, 1, 0); } - if (lines > 0) { + if (vertical) { /* draw vertical list */ for (item = curr; item != next; item = item->right) drawitem(item, x, y += bh, mw - x); @@ -384,10 +384,18 @@ keypress(XKeyEvent *ev) goto draw; case XK_g: ksym = XK_Home; break; case XK_G: ksym = XK_End; break; - case XK_h: ksym = XK_Up; break; - case XK_j: ksym = XK_Next; break; - case XK_k: ksym = XK_Prior; break; - case XK_l: ksym = XK_Down; break; + case XK_j: + vertical ? ksym = XK_Down : (ksym = XK_Next); + break; + case XK_k: + vertical ? ksym = XK_Up : (ksym = XK_Prior); + break; + case XK_h: + vertical ? ksym = XK_Prior : (ksym = XK_Up); + break; + case XK_l: + vertical ? ksym = XK_Next : (ksym = XK_Down); + break; default: return; } @@ -437,11 +445,11 @@ insert: calcoffsets(); break; case XK_Left: - if (cursor > 0 && (!sel || !sel->left || lines > 0)) { + if (cursor > 0 && (!sel || !sel->left || vertical)) { cursor = nextrune(-1); break; } - if (lines > 0) + if (vertical) return; /* fallthrough */ case XK_Up: @@ -477,7 +485,7 @@ insert: cursor = nextrune(+1); break; } - if (lines > 0) + if (vertical) return; /* fallthrough */ case XK_Down: @@ -715,9 +723,10 @@ main(int argc, char *argv[]) } else if (i + 1 == argc) usage(); /* these options take one argument */ - else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */ + else if (!strcmp(argv[i], "-l")){ /* number of lines in vertical list */ lines = atoi(argv[++i]); - else if (!strcmp(argv[i], "-m")) + vertical = 1; + } else if (!strcmp(argv[i], "-m")) mon = atoi(argv[++i]); else if (!strcmp(argv[i], "-p")) /* adds prompt to left of input field */ prompt = argv[++i]; -- 2.30.0