dwm-scratchpad-20221102-ba56fe9.diff 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. diff --git a/config.def.h b/config.def.h
  2. index 9efa774..0b8b310 100644
  3. --- a/config.def.h
  4. +++ b/config.def.h
  5. @@ -59,11 +59,14 @@ static const Layout layouts[] = {
  6. static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */
  7. static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL };
  8. static const char *termcmd[] = { "st", NULL };
  9. +static const char scratchpadname[] = "scratchpad";
  10. +static const char *scratchpadcmd[] = { "st", "-t", scratchpadname, "-g", "120x34", NULL };
  11. static const Key keys[] = {
  12. /* modifier key function argument */
  13. { MODKEY, XK_p, spawn, {.v = dmenucmd } },
  14. { MODKEY|ShiftMask, XK_Return, spawn, {.v = termcmd } },
  15. + { MODKEY, XK_grave, togglescratch, {.v = scratchpadcmd } },
  16. { MODKEY, XK_b, togglebar, {0} },
  17. { MODKEY, XK_j, focusstack, {.i = +1 } },
  18. { MODKEY, XK_k, focusstack, {.i = -1 } },
  19. diff --git a/dwm.c b/dwm.c
  20. index 253aba7..4abf8d3 100644
  21. --- a/dwm.c
  22. +++ b/dwm.c
  23. @@ -212,6 +212,7 @@ static void tagmon(const Arg *arg);
  24. static void tile(Monitor *m);
  25. static void togglebar(const Arg *arg);
  26. static void togglefloating(const Arg *arg);
  27. +static void togglescratch(const Arg *arg);
  28. static void toggletag(const Arg *arg);
  29. static void toggleview(const Arg *arg);
  30. static void unfocus(Client *c, int setfocus);
  31. @@ -272,6 +273,8 @@ static Window root, wmcheckwin;
  32. /* configuration, allows nested code to access above variables */
  33. #include "config.h"
  34. +static unsigned int scratchtag = 1 << LENGTH(tags);
  35. +
  36. /* compile-time check if all tags fit into an unsigned int bit array. */
  37. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  38. @@ -1052,6 +1055,14 @@ manage(Window w, XWindowAttributes *wa)
  39. c->y = MAX(c->y, c->mon->wy);
  40. c->bw = borderpx;
  41. + selmon->tagset[selmon->seltags] &= ~scratchtag;
  42. + if (!strcmp(c->name, scratchpadname)) {
  43. + c->mon->tagset[c->mon->seltags] |= c->tags = scratchtag;
  44. + c->isfloating = True;
  45. + c->x = c->mon->wx + (c->mon->ww / 2 - WIDTH(c) / 2);
  46. + c->y = c->mon->wy + (c->mon->wh / 2 - HEIGHT(c) / 2);
  47. + }
  48. +
  49. wc.border_width = c->bw;
  50. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  51. XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
  52. @@ -1641,6 +1652,7 @@ spawn(const Arg *arg)
  53. {
  54. if (arg->v == dmenucmd)
  55. dmenumon[0] = '0' + selmon->num;
  56. + selmon->tagset[selmon->seltags] &= ~scratchtag;
  57. if (fork() == 0) {
  58. if (dpy)
  59. close(ConnectionNumber(dpy));
  60. @@ -1719,6 +1731,28 @@ togglefloating(const Arg *arg)
  61. arrange(selmon);
  62. }
  63. +void
  64. +togglescratch(const Arg *arg)
  65. +{
  66. + Client *c;
  67. + unsigned int found = 0;
  68. +
  69. + for (c = selmon->clients; c && !(found = c->tags & scratchtag); c = c->next);
  70. + if (found) {
  71. + unsigned int newtagset = selmon->tagset[selmon->seltags] ^ scratchtag;
  72. + if (newtagset) {
  73. + selmon->tagset[selmon->seltags] = newtagset;
  74. + focus(NULL);
  75. + arrange(selmon);
  76. + }
  77. + if (ISVISIBLE(c)) {
  78. + focus(c);
  79. + restack(selmon);
  80. + }
  81. + } else
  82. + spawn(arg);
  83. +}
  84. +
  85. void
  86. toggletag(const Arg *arg)
  87. {