aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2016-03-26 13:00:10 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2016-03-26 13:00:10 +0100
commit1e24d1d1b8d55005e54948fbd8bff6fb50d3bb8e (patch)
tree22c9ba3ad26243c29f5d6b65465d06a589a3a11b
parent6292114c76c935e424e91e8c19ea104ca4925f6e (diff)
downloadxK-1e24d1d1b8d55005e54948fbd8bff6fb50d3bb8e.tar.gz
xK-1e24d1d1b8d55005e54948fbd8bff6fb50d3bb8e.tar.xz
xK-1e24d1d1b8d55005e54948fbd8bff6fb50d3bb8e.zip
degesch: add partial matching /buffer goto
-rw-r--r--degesch.c43
1 files changed, 38 insertions, 5 deletions
diff --git a/degesch.c b/degesch.c
index 87f4680..01be6dc 100644
--- a/degesch.c
+++ b/degesch.c
@@ -9821,10 +9821,26 @@ try_decode_buffer (struct app_context *ctx, const char *word)
struct buffer *buffer = NULL;
if (xstrtoul (&n, word, 10) && n <= INT_MAX)
buffer = buffer_at_index (ctx, n);
- if (!buffer)
- buffer = buffer_by_name (ctx, word);
- // TODO: partial matches
- return buffer;
+ if (buffer || (buffer = buffer_by_name (ctx, word)))
+ return buffer;
+
+ // Basic case insensitive partial matching -- at most one buffer can match
+ int n_matches = 0;
+ LIST_FOR_EACH (struct buffer, iter, ctx->buffers)
+ {
+ char *string = xstrdup (iter->name);
+ char *pattern = xstrdup_printf ("*%s*", word);
+ for (char *p = string; *p; p++) *p = tolower_ascii (*p);
+ for (char *p = pattern; *p; p++) *p = tolower_ascii (*p);
+ if (!fnmatch (pattern, string, 0))
+ {
+ n_matches++;
+ buffer = iter;
+ }
+ free (string);
+ free (pattern);
+ }
+ return n_matches == 1 ? buffer : NULL;
}
static void
@@ -9860,6 +9876,21 @@ part_channel (struct server *s, const char *channel_name, const char *reason)
channel->left_manually = true;
}
+static bool
+handle_buffer_goto (struct app_context *ctx, struct handler_args *a)
+{
+ if (!*a->arguments)
+ return false;
+
+ const char *which = cut_word (&a->arguments);
+ struct buffer *buffer = try_decode_buffer (ctx, which);
+ if (buffer)
+ buffer_activate (ctx, buffer);
+ else
+ log_global_error (ctx, "#s: #s", "No such buffer", which);
+ return true;
+}
+
static void
handle_buffer_close (struct app_context *ctx, struct handler_args *a)
{
@@ -9922,6 +9953,8 @@ handle_command_buffer (struct handler_args *a)
}
else if (!strcasecmp_ascii (action, "move"))
result = handle_buffer_move (ctx, a);
+ else if (!strcasecmp_ascii (action, "goto"))
+ result = handle_buffer_goto (ctx, a);
else if (!strcasecmp_ascii (action, "close"))
handle_buffer_close (ctx, a);
else
@@ -10847,7 +10880,7 @@ g_command_handlers[] =
"[<message>]",
handle_command_quit, 0 },
{ "buffer", "Manage buffers",
- "<N> | list | clear | move <N> | close [<N> | <name>]",
+ "<N> | list | clear | move <N> | goto <N or name> | close [<N or name>]",
handle_command_buffer, 0 },
{ "set", "Manage configuration",
"[<option>]",