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"
18 DEFINE_ALLOCATOR(realloc_unsigned_ints
, unsigned int, 32)
21 grep_text(struct view
*view
, const char *text
[])
26 for (i
= 0; text
[i
]; i
++)
27 if (*text
[i
] && !regexec(view
->regex
, text
[i
], 1, &pmatch
, 0))
33 find_matches(struct view
*view
)
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
]))
43 if (!realloc_unsigned_ints(&view
->matched_line
, view
->matched_lines
, 1))
46 view
->matched_line
[view
->matched_lines
++] = lineno
;
53 setup_and_find_next(struct view
*view
, enum request request
)
56 int regex_flags
= opt_ignore_case
? REG_ICASE
: 0;
62 view
->regex
= calloc(1, sizeof(*view
->regex
));
67 regex_err
= regcomp(view
->regex
, view
->env
->search
, REG_EXTENDED
| regex_flags
);
69 char buf
[SIZEOF_STR
] = "unknown error";
71 regerror(regex_err
, view
->regex
, buf
, sizeof(buf
));
72 report("Search failed: %s", buf
);
76 string_copy(view
->grep
, view
->env
->search
);
80 find_next(view
, request
);
84 find_next(struct view
*view
, enum request request
)
90 if (!*view
->env
->search
)
91 report("No previous search");
93 setup_and_find_next(view
, request
);
103 case REQ_SEARCH_BACK
:
112 if (!view
->matched_lines
&& !find_matches(view
)) {
113 report("Allocation failure");
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
)
126 if (direction
< 0 && lineno
>= view
->pos
.lineno
)
129 select_view_line(view
, lineno
);
130 report("Line %zu matches '%s' (%zu of %zu)", lineno
+ 1, view
->grep
, i
+ 1, view
->matched_lines
);
134 report("No match found for '%s'", view
->grep
);
138 reset_search(struct view
*view
)
140 free(view
->matched_line
);
141 view
->matched_line
= NULL
;
142 view
->matched_lines
= 0;
146 search_view(struct view
*view
, enum request request
)
148 setup_and_find_next(view
, request
);
151 /* vim: set ts=8 sw=8 noexpandtab: */