Move search_view code to internal helper
[tig.git] / src / search.c
blobd667f04dfdbd44e1b06ebd9701ddc949be152e1c
1 /* Copyright (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include "tig/search.h"
15 #include "tig/display.h"
16 #include "tig/draw.h"
18 DEFINE_ALLOCATOR(realloc_unsigned_ints, unsigned int, 32)
20 bool
21 grep_text(struct view *view, const char *text[])
23 regmatch_t pmatch;
24 size_t i;
26 for (i = 0; text[i]; i++)
27 if (*text[i] && !regexec(view->regex, text[i], 1, &pmatch, 0))
28 return true;
29 return false;
32 static bool
33 find_matches(struct view *view)
35 size_t lineno;
37 /* Note, lineno is unsigned long so will wrap around in which case it
38 * will become bigger than view->lines. */
39 for (lineno = 0; lineno < view->lines; lineno++) {
40 if (!view->ops->grep(view, &view->line[lineno]))
41 continue;
43 if (!realloc_unsigned_ints(&view->matched_line, view->matched_lines, 1))
44 return false;
46 view->matched_line[view->matched_lines++] = lineno;
49 return true;
52 static void
53 setup_and_find_next(struct view *view, enum request request)
55 int regex_err;
56 int regex_flags = opt_ignore_case ? REG_ICASE : 0;
58 if (view->regex) {
59 regfree(view->regex);
60 *view->grep = 0;
61 } else {
62 view->regex = calloc(1, sizeof(*view->regex));
63 if (!view->regex)
64 return;
67 regex_err = regcomp(view->regex, view->env->search, REG_EXTENDED | regex_flags);
68 if (regex_err != 0) {
69 char buf[SIZEOF_STR] = "unknown error";
71 regerror(regex_err, view->regex, buf, sizeof(buf));
72 report("Search failed: %s", buf);
73 return;
76 string_copy(view->grep, view->env->search);
78 reset_search(view);
80 find_next(view, request);
83 void
84 find_next(struct view *view, enum request request)
86 int direction;
87 size_t i;
89 if (!*view->grep) {
90 if (!*view->env->search)
91 report("No previous search");
92 else
93 setup_and_find_next(view, request);
94 return;
97 switch (request) {
98 case REQ_SEARCH:
99 case REQ_FIND_NEXT:
100 direction = 1;
101 break;
103 case REQ_SEARCH_BACK:
104 case REQ_FIND_PREV:
105 direction = -1;
106 break;
108 default:
109 return;
112 if (!view->matched_lines && !find_matches(view)) {
113 report("Allocation failure");
114 return;
117 /* Note, `i` is unsigned and will wrap around in which case it
118 * will become bigger than view->matched_lines. */
119 i = direction > 0 ? 0 : view->matched_lines - 1;
120 for (; i < view->matched_lines; i += direction) {
121 size_t lineno = view->matched_line[i];
123 if (direction > 0 && lineno <= view->pos.lineno)
124 continue;
126 if (direction < 0 && lineno >= view->pos.lineno)
127 continue;
129 select_view_line(view, lineno);
130 report("Line %zu matches '%s' (%zu of %zu)", lineno + 1, view->grep, i + 1, view->matched_lines);
131 return;
134 report("No match found for '%s'", view->grep);
137 void
138 reset_search(struct view *view)
140 free(view->matched_line);
141 view->matched_line = NULL;
142 view->matched_lines = 0;
145 void
146 search_view(struct view *view, enum request request)
148 setup_and_find_next(view, request);
151 /* vim: set ts=8 sw=8 noexpandtab: */