From fea6eb3cfc84ede8403c89a3230f5c658a6c7bd1 Mon Sep 17 00:00:00 2001
From: Micah N Gorrell <m@minego.net>
Date: Wed, 27 Mar 2024 13:05:09 -0600
Subject: [PATCH] remembertags

---
 config.def.h |  8 ++++----
 dwl.c        | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 4 deletions(-)

diff --git a/config.def.h b/config.def.h
index 9009517..2312802 100644
--- a/config.def.h
+++ b/config.def.h
@@ -105,10 +105,10 @@ static const enum libinput_config_tap_button_map button_map = LIBINPUT_CONFIG_TA
 #define MODKEY WLR_MODIFIER_ALT
 
 #define TAGKEYS(KEY,SKEY,TAG) \
-	{ MODKEY,                    KEY,            view,            {.ui = 1 << TAG} }, \
-	{ MODKEY|WLR_MODIFIER_CTRL,  KEY,            toggleview,      {.ui = 1 << TAG} }, \
-	{ MODKEY|WLR_MODIFIER_SHIFT, SKEY,           tag,             {.ui = 1 << TAG} }, \
-	{ MODKEY|WLR_MODIFIER_CTRL|WLR_MODIFIER_SHIFT,SKEY,toggletag, {.ui = 1 << TAG} }
+	{ MODKEY,										KEY,	remembertagsview,	{.i = TAG} }, \
+	{ MODKEY|WLR_MODIFIER_CTRL,						KEY,	toggleview,			{.ui = 1 << TAG} }, \
+	{ MODKEY|WLR_MODIFIER_SHIFT,					SKEY,	tag,				{.ui = 1 << TAG} }, \
+	{ MODKEY|WLR_MODIFIER_CTRL|WLR_MODIFIER_SHIFT,	SKEY,	toggletag,			{.ui = 1 << TAG} }
 
 /* helper for spawning shell commands in the pre dwm-5.0 fashion */
 #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } }
diff --git a/dwl.c b/dwl.c
index 5867b0c..31a81aa 100644
--- a/dwl.c
+++ b/dwl.c
@@ -205,6 +205,11 @@ struct Monitor {
 	int gamma_lut_changed;
 	int nmaster;
 	char ltsymbol[16];
+	unsigned int createtag[2]; /* Create windows on the last tag directly selected, not all selected */
+	struct {
+		unsigned int tagset;
+		Client *zoomed;
+	} remembered[31];
 };
 
 typedef struct {
@@ -308,6 +313,7 @@ static void pointerfocus(Client *c, struct wlr_surface *surface,
 		double sx, double sy, uint32_t time);
 static void printstatus(void);
 static void quit(const Arg *arg);
+static void remembertagsview(const Arg *arg);
 static void rendermon(struct wl_listener *listener, void *data);
 static void requestdecorationmode(struct wl_listener *listener, void *data);
 static void requeststartdrag(struct wl_listener *listener, void *data);
@@ -1951,6 +1957,48 @@ quit(const Arg *arg)
 	wl_display_terminate(dpy);
 }
 
+void
+remembertagsview(const Arg *arg) {
+	unsigned newtags = (1 << arg->i) & TAGMASK;
+	int oldtag;
+	int active;
+	unsigned int newcreate;
+
+	if (selmon == NULL) {
+		return;
+	}
+
+	oldtag = selmon->createtag[selmon->seltags];
+	active = (oldtag == arg->i);
+
+	if (oldtag < TAGCOUNT) {
+		selmon->remembered[oldtag].tagset = selmon->tagset[selmon->seltags];
+	}
+
+	selmon->seltags ^= 1;	/*toggle tagset*/
+
+	if (-1 == arg->i) {
+		/* A specific tag was not specified */
+		active = 0;
+		newcreate = selmon->createtag[selmon->seltags];
+	} else {
+		newcreate = arg->i;
+	}
+
+	if (active) {
+		/* Select twice to isolate the tag */
+		selmon->tagset[selmon->seltags] = newtags;
+	} else if (arg->i < TAGCOUNT) {
+		/* Restore whatever was previously on this tag */
+		selmon->tagset[selmon->seltags] = newtags | selmon->remembered[newcreate].tagset;
+	}
+
+	selmon->createtag[selmon->seltags] = newcreate;
+	focusclient(focustop(selmon), 1);
+	arrange(selmon);
+	printstatus();
+}
+
 void
 rendermon(struct wl_listener *listener, void *data)
 {
-- 
2.44.0

