1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/* Copyright 2007, 2008 Peter Klausler. See COPYING for license. */
#include "all.h"
/*
* Incremental search mode
*/
struct mode_search {
command command;
struct mode *previous;
Byte_t *pattern;
size_t bytes, alloc, last_bytes;
Boolean_t backward;
position_t start, mark;
regex_t *regex;
Boolean_t regex_ready;
};
static Boolean_t match_char(Unicode_t x, Unicode_t y)
{
if (!IS_UNICODE(x) || !IS_UNICODE(y))
return FALSE;
if (x >= 'a' && x <= 'z')
x += 'A' - 'a';
if (y >= 'a' && y <= 'z')
y += 'A' - 'a';
return x == y;
}
static size_t match_pattern(struct view *view, position_t offset)
{
struct mode_search *mode = (struct mode_search *) view->mode;
size_t j;
for (j = 0; j < mode->bytes; j++)
if (!match_char(mode->pattern[j],
view_byte(view, offset++)))
return 0;
return mode->bytes;
}
static int match_regex(struct view *view, position_t*offset, Boolean_t advance)
{
int j, err;
char *raw;
size_t bytes = view_raw(view, &raw, *offset, ~0);
unsigned flags = 0;
regmatch_t match[10];
struct mode_search *mode = (struct mode_search *) view->mode;
if (view_char_prior(view, *offset, NULL) != '\n')
flags |= REG_NOTBOL;
err = regexec(mode->regex, raw, 10, match, flags);
if (err && err != REG_NOMATCH)
window_beep(view);
if (err)
return 0;
if (!advance && match[0].rm_so)
return 0;
if (match[0].rm_so >= bytes)
return 0;
if (match[0].rm_eo > bytes)
match[0].rm_eo = bytes;
for (j = 1; j < 10; j++) {
if (match[j].rm_so < 0 ||
match[j].rm_so >= bytes)
continue;
if (match[j].rm_eo > bytes)
match[j].rm_eo = bytes;
clip_init(j);
clip(j, view, *offset + match[j].rm_so,
match[j].rm_eo - match[j].rm_so, 0);
}
*offset += match[0].rm_so;
return match[0].rm_eo - match[0].rm_so;
}
static int scan_forward(struct view *view, size_t *length,
position_t offset, position_t max_offset)
{
struct mode_search *mode = (struct mode_search *) view->mode;
if (offset + mode->bytes > view->bytes)
return -1;
if (max_offset + mode->bytes > view->bytes)
max_offset = view->bytes - mode->bytes;
if (mode->regex) {
if ((*length = match_regex(view, &offset, 1)) &&
offset < max_offset)
return offset;
} else
for (; offset < max_offset; offset++)
if ((*length = match_pattern(view, offset)))
return offset;
return -1;
}
static int scan_backward(struct view *view, size_t *length,
position_t offset, position_t min_offset)
{
struct mode_search *mode = (struct mode_search *) view->mode;
if (min_offset + mode->bytes > view->bytes)
return -1;
if (offset + mode->bytes > view->bytes)
offset = view->bytes - mode->bytes;
if (mode->regex) {
for (; offset+1 > min_offset; offset--)
if ((*length = match_regex(view, &offset, 0)))
return offset;
} else
for (; offset+1 > min_offset; offset--)
if ((*length = match_pattern(view, offset)))
return offset;
return -1;
}
static Boolean_t search(struct view *view, int backward, int new)
{
struct mode_search *mode = (struct mode_search *) view->mode;
position_t cursor;
size_t length;
int at;
if (!mode->bytes) {
locus_set(view, CURSOR, mode->start);
locus_set(view, MARK, UNSET);
return TRUE;
}
if (mode->regex) {
int err;
if (new && mode->regex_ready) {
regfree(mode->regex);
mode->regex_ready = FALSE;
}
if (!mode->regex_ready) {
mode->pattern[mode->bytes] = '\0';
status("regular expression: %s", mode->pattern);
err = regcomp(mode->regex, (char *) mode->pattern,
REG_EXTENDED | REG_ICASE | REG_NEWLINE);
if (err)
return FALSE;
mode->regex_ready = TRUE;
}
}
cursor = locus_get(view, CURSOR);
if (backward) {
at = scan_backward(view, &length, cursor - !new, 0);
if (at < 0)
at = scan_backward(view, &length,
view->bytes, cursor+1);
} else {
at = scan_forward(view, &length,
cursor + !new, view->bytes);
if (at < 0)
at = scan_forward(view, &length, 0, cursor-1);
}
if (at < 0) {
window_beep(view);
macros_abort();
return 0;
}
/* A hit! */
locus_set(view, CURSOR, at);
locus_set(view, MARK, at + length);
mode->last_bytes = mode->bytes;
return TRUE;
}
static void command_handler(struct view *view, Unicode_t ch)
{
struct mode_search *mode = (struct mode_search *) view->mode;
static char cmdchar[][2] = {
{ CONTROL('H'), CONTROL('G') },
{ CONTROL('T'), CONTROL('H') },
{ CONTROL('V'), CONTROL('U') }
};
static char *last_search;
/* Backspace removes the last character from the search target and
* returns to the previous hit
*/
if (ch == 0x7f /*BCK*/)
if (!mode->bytes) {
window_beep(view);
goto done;
} else {
mode->bytes--;
search(view, !mode->backward, 1);
return;
}
/* Non-control characters are appended to the search target and
* we proceed to the next hit if the current position does not
* match the extended target.
*/
if (ch >= ' ' || ch == '\t') {
size_t new;
if (mode->bytes + 8 > mode->alloc) {
mode->alloc = mode->bytes + 64;
mode->pattern = reallocate(mode->pattern, mode->alloc);
}
mode->bytes += new = unicode_utf8((char *) mode->pattern +
mode->bytes, ch);
mode->pattern[mode->bytes] = '\0';
if (!search(view, mode->backward, new) &&
!mode->regex)
mode->bytes -= new;
return;
}
/* ^H moves to a hit that is earlier in the text.
* ^T and ^/ (^A, ^_) proceed to a later hit.
*/
if (mode->last_bytes &&
(ch == cmdchar[0][is_asdfg] ||
ch == cmdchar[1][is_asdfg] ||
ch == CONTROL('A') ||
ch == CONTROL('_'))) {
mode->bytes = mode->last_bytes;
search(view, mode->backward = (ch == cmdchar[0][is_asdfg]), 0);
return;
}
/* Hitting ^H/^T or ^/ (^A, ^_) with an empty search pattern causes
* the last successful search target to be reused.
*/
if ((ch == cmdchar[0][is_asdfg] ||
ch == cmdchar[1][is_asdfg] ||
ch == CONTROL('A') ||
ch == CONTROL('_')) &&
!mode->bytes && last_search) {
mode->bytes = strlen(last_search);
mode->alloc = mode->bytes + 8;
mode->pattern = reallocate(mode->pattern, mode->alloc);
memcpy(mode->pattern, last_search, mode->bytes+1);
if (ch == cmdchar[0][is_asdfg])
mode->backward = 1;
else if (ch == cmdchar[1][is_asdfg])
mode->backward = 0;
search(view, mode->backward, 0);
return;
}
/* Search is done */
done: if (mode->bytes) {
last_search = reallocate(last_search, mode->bytes+1);
memcpy(last_search, mode->pattern, mode->bytes);
last_search[mode->bytes] = '\0';
}
view->mode = mode->previous;
status_hide();
if (ch == '\r' || ch == CONTROL('A') || ch == CONTROL('_'))
;
else if (ch == cmdchar[2][is_asdfg]) {
/* restore mark */
if (mode->mark != UNSET && !mode->backward) {
position_t endmark = locus_get(view, MARK);
if (endmark != UNSET)
locus_set(view, CURSOR, endmark);
}
locus_set(view, MARK, mode->mark);
} else if (ch != 0x7f /*BCK*/)
view->mode->command(view, ch);
/* Release search mode resources */
RELEASE(mode->pattern);
if (mode->regex_ready)
regfree(mode->regex);
RELEASE(mode->regex);
RELEASE(mode);
}
void mode_search(struct view *view, Boolean_t regex)
{
struct mode_search *mode = allocate0(sizeof *mode);
mode->previous = view->mode;
mode->command = command_handler;
mode->start = locus_get(view, CURSOR);
mode->mark = locus_get(view, MARK);
if (regex)
mode->regex = allocate0(sizeof *mode->regex);
view->mode = (struct mode *) mode;
}
|