From 331b960e84c4bf7638342ea4e27fb02bb2f84d4c Mon Sep 17 00:00:00 2001
From: Gravity <alternativegravitational@protonmail.com>
Date: Sat, 20 Dec 2025 20:10:45 +0800
Subject: [PATCH] spawnorfocus

---
 config.def.h |  8 ++++++--
 dwl.c        | 27 +++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/config.def.h b/config.def.h
index 95c2afa..5b37429 100644
--- a/config.def.h
+++ b/config.def.h
@@ -119,14 +119,18 @@ static const enum libinput_config_tap_button_map button_map = LIBINPUT_CONFIG_TA
 #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } }
 
 /* commands */
-static const char *termcmd[] = { "foot", NULL };
+/* The third element is used by the 'spawnorfocus' function to match a title/app_id.
+ * If the third element is 'NULL', match on the first element instead */
+static const char *termcmd[] = { "foot", NULL, NULL };
+static const char *browsercmd[] = { "firefox", NULL, "Mozilla Firefox" };
 static const char *menucmd[] = { "wmenu-run", NULL };
 
 static const Key keys[] = {
 	/* Note that Shift changes certain key codes: c -> C, 2 -> at, etc. */
 	/* modifier                  key                 function        argument */
 	{ MODKEY,                    XKB_KEY_p,          spawn,          {.v = menucmd} },
-	{ MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_Return,     spawn,          {.v = termcmd} },
+	{ MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_Return,     spawnorfocus,   {.v = termcmd} },
+	{ MODKEY,                    XKB_KEY_b,          spawnorfocus,   {.v = browsercmd} },
 	{ MODKEY,                    XKB_KEY_j,          focusstack,     {.i = +1} },
 	{ MODKEY,                    XKB_KEY_k,          focusstack,     {.i = -1} },
 	{ MODKEY,                    XKB_KEY_i,          incnmaster,     {.i = +1} },
diff --git a/dwl.c b/dwl.c
index 12f441e..f902f3b 100644
--- a/dwl.c
+++ b/dwl.c
@@ -8,6 +8,7 @@
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/wait.h>
 #include <time.h>
 #include <unistd.h>
@@ -330,6 +331,7 @@ static void setpsel(struct wl_listener *listener, void *data);
 static void setsel(struct wl_listener *listener, void *data);
 static void setup(void);
 static void spawn(const Arg *arg);
+static void spawnorfocus(const Arg *arg);
 static void startdrag(struct wl_listener *listener, void *data);
 static void tag(const Arg *arg);
 static void tagmon(const Arg *arg);
@@ -2676,6 +2678,31 @@ spawn(const Arg *arg)
 	}
 }
 
+void
+spawnorfocus(const Arg *arg)
+{
+	char *needle;
+	Client *c;
+	int i = 0;
+
+	while (((char **)arg->v)[i++]);
+	needle = ((char **)arg->v)[i + 1] ? ((char **)arg->v)[i + 1] : ((char **)arg->v)[0];
+
+	wl_list_for_each(c, &clients, link)
+		if (strstr(client_get_title(c), needle)
+				|| strstr(client_get_appid(c), needle)) {
+			selmon = c->mon;
+			if (!(c->tags & selmon->tagset[selmon->seltags]))
+				selmon->tagset[selmon->seltags] = c->tags;
+
+			focusclient(c, 1);
+			arrange(selmon);
+			return;
+		}
+
+	spawn(arg);
+}
+
 void
 startdrag(struct wl_listener *listener, void *data)
 {
-- 
2.51.2

