summary refs log tree commit diff
path: root/search.c
diff options
context:
space:
mode:
authorokan <okan>2017-12-07 16:25:33 +0000
committerokan <okan>2017-12-07 16:25:33 +0000
commita0774f4777a4a94ea4bbc867c16cde3167075f80 (patch)
tree95c8ef80196043b3e7241d63b70d6a7f6e2fbe8c /search.c
parentdd5f9512058799be696477ab01e2e877ff4e7430 (diff)
parent0fc9d47fb407a65fd08e3095fface42eb93ac914 (diff)
downloadcwm-a0774f4777a4a94ea4bbc867c16cde3167075f80.tar.gz
cwm-a0774f4777a4a94ea4bbc867c16cde3167075f80.tar.xz
cwm-a0774f4777a4a94ea4bbc867c16cde3167075f80.zip
cvsimport
* refs/heads/master:
  Original idea from Dimitris Papastamos to move windows to corners a while ago; re-proposed by Julien Steinhauser with an updated diff. Apparently this was in the original calmnwm.
  give command and group menus their own match callbacks
  stash dir into conf since it'll be of use
  organize this a bit better
  spacing
Diffstat (limited to 'search.c')
-rw-r--r--search.c188
1 files changed, 105 insertions, 83 deletions
diff --git a/search.c b/search.c
index 7d32291..68c064e 100644
--- a/search.c
+++ b/search.c
@@ -36,9 +36,35 @@
 #define PATH_ANY 	0x0001
 #define PATH_EXEC 	0x0002
 
-static void	search_match_path_type(struct menu_q *, struct menu_q *,
-		    char *, int);
-static int	strsubmatch(char *, char *, int);
+static void	match_path_type(struct menu_q *, char *, int);
+static int	match_substr(char *, char *, int);
+
+static int
+match_substr(char *sub, char *str, int zeroidx)
+{
+	size_t		 len, sublen;
+	unsigned int	 n, flen;
+
+	if (sub == NULL || str == NULL)
+		return(0);
+
+	len = strlen(str);
+	sublen = strlen(sub);
+
+	if (sublen > len)
+		return(0);
+
+	if (zeroidx)
+		flen = 0;
+	else
+		flen = len - sublen;
+
+	for (n = 0; n <= flen; n++)
+		if (strncasecmp(sub, str + n, sublen) == 0)
+			return(1);
+
+	return(0);
+}
 
 void
 search_match_client(struct menu_q *menuq, struct menu_q *resultq, char *search)
@@ -46,29 +72,28 @@ search_match_client(struct menu_q *menuq, struct menu_q *resultq, char *search)
 	struct winname	*wn;
 	struct menu	*mi, *tierp[4], *before = NULL;
 
-	TAILQ_INIT(resultq);
-
 	(void)memset(tierp, 0, sizeof(tierp));
 
+	TAILQ_INIT(resultq);
 	TAILQ_FOREACH(mi, menuq, entry) {
 		int tier = -1, t;
 		struct client_ctx *cc = (struct client_ctx *)mi->ctx;
 
 		/* Match on label. */
-		if (strsubmatch(search, cc->label, 0))
+		if (match_substr(search, cc->label, 0))
 			tier = 0;
 
 		/* Match on window name history, from present to past. */
 		if (tier < 0) {
 			TAILQ_FOREACH_REVERSE(wn, &cc->nameq, name_q, entry)
-				if (strsubmatch(search, wn->name, 0)) {
+				if (match_substr(search, wn->name, 0)) {
 					tier = 2;
 					break;
 				}
 		}
 
 		/* Match on window resource class. */
-		if ((tier < 0) && strsubmatch(search, cc->ch.res_class, 0))
+		if ((tier < 0) && match_substr(search, cc->ch.res_class, 0))
 			tier = 3;
 
 		if (tier < 0)
@@ -105,48 +130,36 @@ search_match_client(struct menu_q *menuq, struct menu_q *resultq, char *search)
 }
 
 void
-search_print_text(struct menu *mi, int listing)
-{
-	(void)snprintf(mi->print, sizeof(mi->print), "%s", mi->text);
-}
-
-void
-search_print_cmd(struct menu *mi, int listing)
-{
-	struct cmd_ctx	*cmd = (struct cmd_ctx *)mi->ctx;
-
-	(void)snprintf(mi->print, sizeof(mi->print), "%s", cmd->name);
-}
-
-void
-search_print_group(struct menu *mi, int listing)
+search_match_cmd(struct menu_q *menuq, struct menu_q *resultq, char *search)
 {
-	struct group_ctx	*gc = (struct group_ctx *)mi->ctx;
+	struct menu	*mi;
 
-	(void)snprintf(mi->print, sizeof(mi->print),
-	    (group_holds_only_hidden(gc)) ? "%d: [%s]" : "%d: %s",
-	    gc->num, gc->name);
+	TAILQ_INIT(resultq);
+	TAILQ_FOREACH(mi, menuq, entry) {
+		struct cmd_ctx *cmd = (struct cmd_ctx *)mi->ctx;
+		if (match_substr(search, cmd->name, 0))
+			TAILQ_INSERT_TAIL(resultq, mi, resultentry);
+	}
 }
 
 void
-search_print_client(struct menu *mi, int listing)
+search_match_group(struct menu_q *menuq, struct menu_q *resultq, char *search)
 {
-	struct client_ctx	*cc = (struct client_ctx *)mi->ctx;
-	char			 flag = ' ';
-
-	if (cc->flags & CLIENT_ACTIVE)
-		flag = '!';
-	else if (cc->flags & CLIENT_HIDDEN)
-		flag = '&';
+	struct menu	*mi;
+	char		*s;
 
-	(void)snprintf(mi->print, sizeof(mi->print), "(%d) %c[%s] %s",
-	    (cc->gc) ? cc->gc->num : 0, flag,
-	    (cc->label) ? cc->label : "", cc->name);
+	TAILQ_INIT(resultq);
+	TAILQ_FOREACH(mi, menuq, entry) {
+		struct group_ctx *gc = (struct group_ctx *)mi->ctx;
+		xasprintf(&s, "%d %s", gc->num, gc->name);
+		if (match_substr(search, s, 0))
+			TAILQ_INSERT_TAIL(resultq, mi, resultentry);
+		free(s);
+	}
 }
 
 static void
-search_match_path_type(struct menu_q *menuq, struct menu_q *resultq,
-    char *search, int flag)
+match_path_type(struct menu_q *resultq, char *search, int flag)
 {
 	struct menu     *mi;
 	char 		 pattern[PATH_MAX];
@@ -155,7 +168,6 @@ search_match_path_type(struct menu_q *menuq, struct menu_q *resultq,
 
 	(void)strlcpy(pattern, search, sizeof(pattern));
 	(void)strlcat(pattern, "*", sizeof(pattern));
-
 	if (glob(pattern, GLOB_MARK, NULL, &g) != 0)
 		return;
 	for (i = 0; i < g.gl_pathc; i++) {
@@ -169,35 +181,14 @@ search_match_path_type(struct menu_q *menuq, struct menu_q *resultq,
 }
 
 void
-search_match_path(struct menu_q *menuq, struct menu_q *resultq, char *search)
-{
-	TAILQ_INIT(resultq);
-
-	search_match_path_type(menuq, resultq, search, PATH_ANY);
-}
-
-void
-search_match_text(struct menu_q *menuq, struct menu_q *resultq, char *search)
-{
-	struct menu	*mi;
-
-	TAILQ_INIT(resultq);
-
-	TAILQ_FOREACH(mi, menuq, entry)
-		if (strsubmatch(search, mi->text, 0))
-			TAILQ_INSERT_TAIL(resultq, mi, resultentry);
-}
-
-void
 search_match_exec(struct menu_q *menuq, struct menu_q *resultq, char *search)
 {
 	struct menu	*mi, *mj;
 	int		 r;
 
 	TAILQ_INIT(resultq);
-
 	TAILQ_FOREACH(mi, menuq, entry) {
-		if (strsubmatch(search, mi->text, 1) == 0 &&
+		if (match_substr(search, mi->text, 1) == 0 &&
 		    fnmatch(search, mi->text, 0) == FNM_NOMATCH)
 			continue;
 		TAILQ_FOREACH(mj, resultq, resultentry) {
@@ -210,34 +201,65 @@ search_match_exec(struct menu_q *menuq, struct menu_q *resultq, char *search)
 		if (mj == NULL)
 			TAILQ_INSERT_TAIL(resultq, mi, resultentry);
 	}
-
 	if (TAILQ_EMPTY(resultq))
-		search_match_path_type(menuq, resultq, search, PATH_EXEC);
+		match_path_type(resultq, search, PATH_EXEC);
 }
 
-static int
-strsubmatch(char *sub, char *str, int zeroidx)
+void
+search_match_path(struct menu_q *menuq, struct menu_q *resultq, char *search)
 {
-	size_t		 len, sublen;
-	unsigned int	 n, flen;
+	TAILQ_INIT(resultq);
+	match_path_type(resultq, search, PATH_ANY);
+}
 
-	if (sub == NULL || str == NULL)
-		return(0);
+void
+search_match_text(struct menu_q *menuq, struct menu_q *resultq, char *search)
+{
+	struct menu	*mi;
 
-	len = strlen(str);
-	sublen = strlen(sub);
+	TAILQ_INIT(resultq);
+	TAILQ_FOREACH(mi, menuq, entry) {
+		if (match_substr(search, mi->text, 0))
+			TAILQ_INSERT_TAIL(resultq, mi, resultentry);
+	}
+}
 
-	if (sublen > len)
-		return(0);
+void
+search_print_client(struct menu *mi, int listing)
+{
+	struct client_ctx	*cc = (struct client_ctx *)mi->ctx;
+	char			 flag = ' ';
 
-	if (!zeroidx)
-		flen = len - sublen;
-	else
-		flen = 0;
+	if (cc->flags & CLIENT_ACTIVE)
+		flag = '!';
+	else if (cc->flags & CLIENT_HIDDEN)
+		flag = '&';
 
-	for (n = 0; n <= flen; n++)
-		if (strncasecmp(sub, str + n, sublen) == 0)
-			return(1);
+	(void)snprintf(mi->print, sizeof(mi->print), "(%d) %c[%s] %s",
+	    (cc->gc) ? cc->gc->num : 0, flag,
+	    (cc->label) ? cc->label : "", cc->name);
+}
 
-	return(0);
+void
+search_print_cmd(struct menu *mi, int listing)
+{
+	struct cmd_ctx	*cmd = (struct cmd_ctx *)mi->ctx;
+
+	(void)snprintf(mi->print, sizeof(mi->print), "%s", cmd->name);
+}
+
+void
+search_print_group(struct menu *mi, int listing)
+{
+	struct group_ctx	*gc = (struct group_ctx *)mi->ctx;
+
+	(void)snprintf(mi->print, sizeof(mi->print),
+	    (group_holds_only_hidden(gc)) ? "%d: [%s]" : "%d: %s",
+	    gc->num, gc->name);
+}
+
+void
+search_print_text(struct menu *mi, int listing)
+{
+	(void)snprintf(mi->print, sizeof(mi->print), "%s", mi->text);
 }