2007-04-27 21:58:48 +04:00
|
|
|
/*
|
|
|
|
* calmwm - the calm window manager
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004 Marius Aamodt Eriksen <marius@monkey.org>
|
2011-05-11 17:53:51 +04:00
|
|
|
*
|
2008-01-11 19:06:44 +03:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2007-04-27 21:58:48 +04:00
|
|
|
*
|
2011-05-11 17:53:51 +04:00
|
|
|
* $OpenBSD$
|
2007-04-27 21:58:48 +04:00
|
|
|
*/
|
|
|
|
|
2015-01-19 17:54:16 +03:00
|
|
|
#include <sys/types.h>
|
2009-12-15 07:10:42 +03:00
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2009-06-26 16:45:12 +04:00
|
|
|
#include <fnmatch.h>
|
2012-11-09 07:52:02 +04:00
|
|
|
#include <glob.h>
|
2015-01-19 17:54:16 +03:00
|
|
|
#include <limits.h>
|
2012-11-09 07:52:02 +04:00
|
|
|
#include <stdio.h>
|
2009-12-15 07:10:42 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2009-12-15 06:24:36 +03:00
|
|
|
|
2007-04-27 21:58:48 +04:00
|
|
|
#include "calmwm.h"
|
|
|
|
|
2013-04-05 21:36:02 +04:00
|
|
|
#define PATH_ANY 0x0001
|
|
|
|
#define PATH_EXEC 0x0002
|
2012-11-07 18:39:44 +04:00
|
|
|
|
2013-04-05 21:36:02 +04:00
|
|
|
static void search_match_path(struct menu_q *, struct menu_q *,
|
|
|
|
char *, int);
|
2013-05-01 01:11:07 +04:00
|
|
|
static void search_match_path_exec(struct menu_q *, struct menu_q *,
|
|
|
|
char *);
|
2009-05-18 04:23:35 +04:00
|
|
|
static int strsubmatch(char *, char *, int);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
search_match_client(struct menu_q *menuq, struct menu_q *resultq, char *search)
|
|
|
|
{
|
2008-07-11 18:21:28 +04:00
|
|
|
struct winname *wn;
|
|
|
|
struct menu *mi, *tierp[4], *before = NULL;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
TAILQ_INIT(resultq);
|
|
|
|
|
2011-07-25 19:10:24 +04:00
|
|
|
(void)memset(tierp, 0, sizeof(tierp));
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In order of rank:
|
|
|
|
*
|
|
|
|
* 1. Look through labels.
|
|
|
|
* 2. Look at title history, from present to past.
|
|
|
|
* 3. Look at window class name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
TAILQ_FOREACH(mi, menuq, entry) {
|
|
|
|
int tier = -1, t;
|
2015-06-30 21:42:50 +03:00
|
|
|
struct client_ctx *cc = (struct client_ctx *)mi->ctx;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
/* First, try to match on labels. */
|
2009-05-18 04:23:35 +04:00
|
|
|
if (cc->label != NULL && strsubmatch(search, cc->label, 0)) {
|
2007-04-27 21:58:48 +04:00
|
|
|
cc->matchname = cc->label;
|
|
|
|
tier = 0;
|
2008-04-16 00:24:41 +04:00
|
|
|
}
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
/* Then, on window names. */
|
|
|
|
if (tier < 0) {
|
|
|
|
TAILQ_FOREACH_REVERSE(wn, &cc->nameq, winname_q, entry)
|
2009-05-18 04:23:35 +04:00
|
|
|
if (strsubmatch(search, wn->name, 0)) {
|
2008-04-16 00:24:41 +04:00
|
|
|
cc->matchname = wn->name;
|
|
|
|
tier = 2;
|
|
|
|
break;
|
|
|
|
}
|
2007-04-27 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
2010-09-26 00:01:27 +04:00
|
|
|
/* Then if there is a match on the window class name. */
|
2013-12-11 18:09:21 +04:00
|
|
|
if (tier < 0 && strsubmatch(search, cc->ch.res_class, 0)) {
|
|
|
|
cc->matchname = cc->ch.res_class;
|
2007-04-27 21:58:48 +04:00
|
|
|
tier = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tier < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* De-rank a client one tier if it's the current
|
|
|
|
* window. Furthermore, this is denoted by a "!" when
|
|
|
|
* printing the window name in the search menu.
|
|
|
|
*/
|
2011-03-22 13:57:31 +03:00
|
|
|
if (cc == client_current() && tier < nitems(tierp) - 1)
|
2007-04-27 21:58:48 +04:00
|
|
|
tier++;
|
|
|
|
|
2010-09-26 00:01:27 +04:00
|
|
|
/* Clients that are hidden get ranked one up. */
|
2014-09-15 17:00:49 +04:00
|
|
|
if ((cc->flags & CLIENT_HIDDEN) && (tier > 0))
|
2007-04-27 21:58:48 +04:00
|
|
|
tier--;
|
|
|
|
|
2015-06-28 22:50:46 +03:00
|
|
|
if (tier >= nitems(tierp))
|
|
|
|
errx(1, "search_match_client: invalid tier");
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If you have a tierp, insert after it, and make it
|
|
|
|
* the new tierp. If you don't have a tierp, find the
|
|
|
|
* first nonzero tierp above you, insert after it.
|
|
|
|
* Always make your current tierp the newly inserted
|
|
|
|
* entry.
|
|
|
|
*/
|
|
|
|
for (t = tier; t >= 0 && ((before = tierp[t]) == NULL); t--)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (before == NULL)
|
|
|
|
TAILQ_INSERT_HEAD(resultq, mi, resultentry);
|
|
|
|
else
|
|
|
|
TAILQ_INSERT_AFTER(resultq, before, mi, resultentry);
|
|
|
|
|
|
|
|
tierp[tier] = mi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 21:54:12 +03:00
|
|
|
void
|
|
|
|
search_print_cmd(struct menu *mi, int i)
|
|
|
|
{
|
|
|
|
struct cmd *cmd = (struct cmd *)mi->ctx;
|
|
|
|
|
2015-08-21 19:05:55 +03:00
|
|
|
(void)snprintf(mi->print, sizeof(mi->print), "%s", cmd->name);
|
2015-06-30 21:54:12 +03:00
|
|
|
}
|
|
|
|
|
2015-07-12 17:31:47 +03:00
|
|
|
void
|
|
|
|
search_print_group(struct menu *mi, int i)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-04-27 21:58:48 +04:00
|
|
|
void
|
|
|
|
search_print_client(struct menu *mi, int list)
|
|
|
|
{
|
2015-06-30 21:42:50 +03:00
|
|
|
struct client_ctx *cc = (struct client_ctx *)mi->ctx;
|
2008-07-11 18:21:28 +04:00
|
|
|
char flag = ' ';
|
|
|
|
|
2007-04-27 21:58:48 +04:00
|
|
|
if (cc == client_current())
|
|
|
|
flag = '!';
|
|
|
|
else if (cc->flags & CLIENT_HIDDEN)
|
|
|
|
flag = '&';
|
|
|
|
|
2015-07-01 17:36:42 +03:00
|
|
|
if ((list) || (cc->matchname == cc->label))
|
2008-09-22 18:15:03 +04:00
|
|
|
cc->matchname = cc->name;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2015-06-08 18:34:03 +03:00
|
|
|
(void)snprintf(mi->print, sizeof(mi->print), "(%d) %c[%s] %s",
|
2015-07-01 17:36:42 +03:00
|
|
|
(cc->group) ? cc->group->num : 0, flag,
|
|
|
|
(cc->label) ? cc->label : "", cc->matchname);
|
2007-04-27 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
2012-11-07 18:39:44 +04:00
|
|
|
static void
|
|
|
|
search_match_path(struct menu_q *menuq, struct menu_q *resultq, char *search, int flag)
|
|
|
|
{
|
2015-01-19 17:54:16 +03:00
|
|
|
char pattern[PATH_MAX];
|
2014-01-20 22:58:03 +04:00
|
|
|
glob_t g;
|
|
|
|
int i;
|
2012-11-07 18:39:44 +04:00
|
|
|
|
|
|
|
TAILQ_INIT(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++) {
|
|
|
|
if ((flag & PATH_EXEC) && access(g.gl_pathv[i], X_OK))
|
|
|
|
continue;
|
2014-01-20 22:58:03 +04:00
|
|
|
menuq_add(resultq, NULL, "%s", g.gl_pathv[i]);
|
2012-11-07 18:39:44 +04:00
|
|
|
}
|
|
|
|
globfree(&g);
|
|
|
|
}
|
|
|
|
|
2013-05-01 01:11:07 +04:00
|
|
|
static void
|
2012-11-07 18:39:44 +04:00
|
|
|
search_match_path_exec(struct menu_q *menuq, struct menu_q *resultq, char *search)
|
|
|
|
{
|
2014-09-07 23:27:30 +04:00
|
|
|
return(search_match_path(menuq, resultq, search, PATH_EXEC));
|
2012-11-07 18:39:44 +04:00
|
|
|
}
|
|
|
|
|
2013-04-05 21:07:25 +04:00
|
|
|
void
|
2012-11-07 18:39:44 +04:00
|
|
|
search_match_path_any(struct menu_q *menuq, struct menu_q *resultq, char *search)
|
|
|
|
{
|
2014-09-07 23:27:30 +04:00
|
|
|
return(search_match_path(menuq, resultq, search, PATH_ANY));
|
2012-11-07 18:39:44 +04:00
|
|
|
}
|
|
|
|
|
2007-04-27 21:58:48 +04:00
|
|
|
void
|
|
|
|
search_match_text(struct menu_q *menuq, struct menu_q *resultq, char *search)
|
|
|
|
{
|
2008-07-11 18:21:28 +04:00
|
|
|
struct menu *mi;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
TAILQ_INIT(resultq);
|
|
|
|
|
|
|
|
TAILQ_FOREACH(mi, menuq, entry)
|
2009-05-18 04:23:35 +04:00
|
|
|
if (strsubmatch(search, mi->text, 0))
|
2007-06-26 23:34:26 +04:00
|
|
|
TAILQ_INSERT_TAIL(resultq, mi, resultentry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
search_match_exec(struct menu_q *menuq, struct menu_q *resultq, char *search)
|
|
|
|
{
|
2008-09-03 08:39:12 +04:00
|
|
|
struct menu *mi, *mj;
|
2013-04-03 23:28:00 +04:00
|
|
|
int r;
|
2007-06-26 23:34:26 +04:00
|
|
|
|
|
|
|
TAILQ_INIT(resultq);
|
|
|
|
|
2008-09-03 08:39:12 +04:00
|
|
|
TAILQ_FOREACH(mi, menuq, entry) {
|
2009-06-26 16:45:12 +04:00
|
|
|
if (strsubmatch(search, mi->text, 1) == 0 &&
|
|
|
|
fnmatch(search, mi->text, 0) == FNM_NOMATCH)
|
2013-03-10 01:55:56 +04:00
|
|
|
continue;
|
|
|
|
TAILQ_FOREACH(mj, resultq, resultentry) {
|
2013-04-03 23:28:00 +04:00
|
|
|
r = strcasecmp(mi->text, mj->text);
|
|
|
|
if (r < 0)
|
2008-09-03 08:39:12 +04:00
|
|
|
TAILQ_INSERT_BEFORE(mj, mi, resultentry);
|
2013-04-03 23:28:00 +04:00
|
|
|
if (r <= 0)
|
2008-09-03 08:39:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (mj == NULL)
|
2007-04-27 21:58:48 +04:00
|
|
|
TAILQ_INSERT_TAIL(resultq, mi, resultentry);
|
2008-09-03 08:39:12 +04:00
|
|
|
}
|
2007-04-27 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
2012-11-07 18:39:44 +04:00
|
|
|
void
|
|
|
|
search_match_exec_path(struct menu_q *menuq, struct menu_q *resultq, char *search)
|
|
|
|
{
|
|
|
|
search_match_exec(menuq, resultq, search);
|
|
|
|
if (TAILQ_EMPTY(resultq))
|
|
|
|
search_match_path_exec(menuq, resultq, search);
|
|
|
|
}
|
|
|
|
|
2007-04-27 21:58:48 +04:00
|
|
|
static int
|
2009-05-18 04:23:35 +04:00
|
|
|
strsubmatch(char *sub, char *str, int zeroidx)
|
2007-04-27 21:58:48 +04:00
|
|
|
{
|
2014-01-03 19:29:06 +04:00
|
|
|
size_t len, sublen;
|
|
|
|
unsigned int n, flen;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
if (sub == NULL || str == NULL)
|
2014-09-07 23:27:30 +04:00
|
|
|
return(0);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
sublen = strlen(sub);
|
|
|
|
|
|
|
|
if (sublen > len)
|
2014-09-07 23:27:30 +04:00
|
|
|
return(0);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2007-06-26 23:34:26 +04:00
|
|
|
if (!zeroidx)
|
|
|
|
flen = len - sublen;
|
|
|
|
else
|
|
|
|
flen = 0;
|
2008-07-11 18:21:28 +04:00
|
|
|
|
2007-06-26 23:34:26 +04:00
|
|
|
for (n = 0; n <= flen; n++)
|
2007-04-27 21:58:48 +04:00
|
|
|
if (strncasecmp(sub, str + n, sublen) == 0)
|
2014-09-07 23:27:30 +04:00
|
|
|
return(1);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2014-09-07 23:27:30 +04:00
|
|
|
return(0);
|
2007-04-27 21:58:48 +04:00
|
|
|
}
|