summaryrefslogtreecommitdiff
path: root/src/gui/gui-buffer.c
diff options
context:
space:
mode:
authorSebastien Helleu <flashcode@flashtux.org>2009-02-24 10:32:12 +0100
committerSebastien Helleu <flashcode@flashtux.org>2009-02-24 10:32:12 +0100
commit2af58ba271733bfd5a508bd4cf2c0f04ed4e5f74 (patch)
tree5843722549823052a3f8167f3fdf33ba4392d787 /src/gui/gui-buffer.c
parent426ab803cb62e47e1040ea9bcbfae0749c014eb0 (diff)
downloadweechat-2af58ba271733bfd5a508bd4cf2c0f04ed4e5f74.zip
Improved search of buffer with partial name (command "/buffer name")
Diffstat (limited to 'src/gui/gui-buffer.c')
-rw-r--r--src/gui/gui-buffer.c72
1 files changed, 61 insertions, 11 deletions
diff --git a/src/gui/gui-buffer.c b/src/gui/gui-buffer.c
index c5d88b98f..24f480baa 100644
--- a/src/gui/gui-buffer.c
+++ b/src/gui/gui-buffer.c
@@ -1040,16 +1040,26 @@ gui_buffer_search_by_name (const char *plugin, const char *name)
struct t_gui_buffer *
gui_buffer_search_by_partial_name (const char *plugin, const char *name)
{
- struct t_gui_buffer *ptr_buffer, *buffer_partial_match;
- int plugin_match;
+ struct t_gui_buffer *ptr_start_buffer, *ptr_buffer, *buffer_partial_match[3];
+ int plugin_match, length_name;
+ const char *pos;
if (!name || !name[0])
return gui_current_window->buffer;
- buffer_partial_match = NULL;
+ /* 0: mathces beginning of buffer name, 1: in the middle, 2: the end */
+ buffer_partial_match[0] = NULL;
+ buffer_partial_match[1] = NULL;
+ buffer_partial_match[2] = NULL;
- for (ptr_buffer = gui_buffers; ptr_buffer;
- ptr_buffer = ptr_buffer->next_buffer)
+ length_name = strlen (name);
+
+ ptr_buffer = gui_current_window->buffer->next_buffer;
+ if (!ptr_buffer)
+ ptr_buffer = gui_buffers;
+ ptr_start_buffer = ptr_buffer;
+
+ while (ptr_buffer)
{
if (ptr_buffer->name)
{
@@ -1069,16 +1079,56 @@ gui_buffer_search_by_partial_name (const char *plugin, const char *name)
}
if (plugin_match)
{
- if (strcmp (ptr_buffer->name, name) == 0)
- return ptr_buffer;
- if (!buffer_partial_match && strstr (ptr_buffer->name, name))
- buffer_partial_match = ptr_buffer;
+ pos = strstr (ptr_buffer->name, name);
+ if (pos)
+ {
+ if (pos == ptr_buffer->name)
+ {
+ if (!pos[length_name])
+ {
+ /* matches full name, return it immediately */
+ return ptr_buffer;
+ }
+ /* matches beginning of name */
+ if (!buffer_partial_match[0])
+ buffer_partial_match[0] = ptr_buffer;
+ }
+ else
+ {
+ if (pos[length_name])
+ {
+ /* matches middle of buffer name */
+ if (!buffer_partial_match[1])
+ buffer_partial_match[1] = ptr_buffer;
+ }
+ else
+ {
+ /* matches end of buffer name */
+ if (!buffer_partial_match[2])
+ buffer_partial_match[2] = ptr_buffer;
+ }
+ }
+ }
}
}
+ ptr_buffer = ptr_buffer->next_buffer;
+ if (!ptr_buffer)
+ ptr_buffer = gui_buffers;
+ if (ptr_buffer == ptr_start_buffer)
+ break;
}
- /* return buffer partially matching (may be NULL if no buffer was found */
- return buffer_partial_match;
+ /* matches end of name? */
+ if (buffer_partial_match[2])
+ return buffer_partial_match[2];
+
+ /* matches beginning of name? */
+ if (buffer_partial_match[0])
+ return buffer_partial_match[0];
+
+ /* return buffer partially matching in name
+ (may be NULL if no buffer was found) */
+ return buffer_partial_match[1];
}
/*