Skip to content

Commit

Permalink
vis: overhaul search related code, support "/ register
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Mar 12, 2016
1 parent b9f04d8 commit 2bbcf71
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
2 changes: 2 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,8 @@ static const char *key2register(Vis *vis, const char *keys, enum VisRegister *re
*reg = VIS_REG_ZERO;
else if (keys[0] == '@')
*reg = VIS_MACRO_LAST_RECORDED;
else if (keys[0] == '/')
*reg = VIS_REG_SEARCH;
return keys+1;
}

Expand Down
7 changes: 2 additions & 5 deletions vis-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1177,13 +1177,10 @@ static Filepos parse_pos(Win *win, char **cmd) {
if (!pattern_end)
return EPOS;
*pattern_end++ = '\0';
Regex *regex = text_regex_new();
Regex *regex = vis_regex(win->vis, *cmd);
if (!regex)
return EPOS;
if (!text_regex_compile(regex, *cmd, 0)) {
*cmd = pattern_end;
pos = text_search_forward(txt, view_cursor_get(view), regex);
}
pos = text_search_forward(txt, view_cursor_get(view), regex);
text_regex_free(regex);
break;
case '+':
Expand Down
42 changes: 28 additions & 14 deletions vis-motions.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,56 @@

/** utility functions */

static bool search_word(Vis *vis, Text *txt, size_t pos) {
static Regex *search_word(Vis *vis, Text *txt, size_t pos) {
char expr[512];
Filerange word = text_object_word(txt, pos);
if (!text_range_valid(&word))
return false;
return NULL;
char *buf = text_bytes_alloc0(txt, word.start, text_range_size(&word));
if (!buf)
return false;
return NULL;
snprintf(expr, sizeof(expr), "[[:<:]]%s[[:>:]]", buf);
bool ret = text_regex_compile(vis->search_pattern, expr, REG_EXTENDED) == 0;
if (!ret) {
Regex *regex = vis_regex(vis, expr);
if (!regex) {
snprintf(expr, sizeof(expr), "\\<%s\\>", buf);
ret = text_regex_compile(vis->search_pattern, expr, REG_EXTENDED) == 0;
regex = vis_regex(vis, expr);
}
free(buf);
return ret;
return regex;
}

/** motion implementations */

static size_t search_word_forward(Vis *vis, Text *txt, size_t pos) {
if (search_word(vis, txt, pos))
pos = text_search_forward(txt, pos, vis->search_pattern);
Regex *regex = search_word(vis, txt, pos);
if (regex)
pos = text_search_forward(txt, pos, regex);
text_regex_free(regex);
return pos;
}

static size_t search_word_backward(Vis *vis, Text *txt, size_t pos) {
if (search_word(vis, txt, pos))
pos = text_search_backward(txt, pos, vis->search_pattern);
Regex *regex = search_word(vis, txt, pos);
if (regex)
pos = text_search_backward(txt, pos, regex);
text_regex_free(regex);
return pos;
}

static size_t search_forward(Vis *vis, Text *txt, size_t pos) {
return text_search_forward(txt, pos, vis->search_pattern);
Regex *regex = vis_regex(vis, NULL);
if (regex)
pos = text_search_forward(txt, pos, regex);
text_regex_free(regex);
return pos;
}

static size_t search_backward(Vis *vis, Text *txt, size_t pos) {
return text_search_backward(txt, pos, vis->search_pattern);
Regex *regex = vis_regex(vis, NULL);
if (regex)
pos = text_search_backward(txt, pos, regex);
text_regex_free(regex);
return pos;
}

static size_t mark_goto(Vis *vis, File *file, size_t pos) {
Expand Down Expand Up @@ -245,10 +257,12 @@ bool vis_motion(Vis *vis, enum VisMotion motion, ...) {
case VIS_MOVE_SEARCH_BACKWARD:
{
const char *pattern = va_arg(ap, char*);
if (text_regex_compile(vis->search_pattern, pattern, REG_EXTENDED|REG_NEWLINE)) {
Regex *regex = vis_regex(vis, pattern);
if (!regex) {
vis_cancel(vis);
goto err;
}
text_regex_free(regex);
if (motion == VIS_MOVE_SEARCH_FORWARD)
motion = VIS_MOVE_SEARCH_NEXT;
else
Expand Down
14 changes: 14 additions & 0 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,20 @@ void vis_insert_nl(Vis *vis) {
copy_indent_from_previous_line(vis->win);
}

Regex *vis_regex(Vis *vis, const char *pattern) {
if (!pattern && !(pattern = register_get(vis, &vis->registers[VIS_REG_SEARCH], NULL)))
return NULL;
Regex *regex = text_regex_new();
if (!regex)
return NULL;
if (text_regex_compile(regex, pattern, REG_EXTENDED|REG_NEWLINE) != 0) {
text_regex_free(regex);
return NULL;
}
register_put(vis, &vis->registers[VIS_REG_SEARCH], pattern, strlen(pattern)+1);
return regex;
}

Text *vis_text(Vis *vis) {
return vis->win->file->text;
}
Expand Down
5 changes: 5 additions & 0 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ typedef struct Win Win;

#include "ui.h"
#include "view.h"
#include "text-regex.h"

typedef struct {
void (*vis_start)(Vis*);
Expand Down Expand Up @@ -351,6 +352,7 @@ enum VisRegister {
VIS_REG_PROMPT, /* internal register which shadows DEFAULT in PROMPT mode */
VIS_MACRO_OPERATOR, /* records entered keys after an operator */
VIS_MACRO_REPEAT, /* copy of the above macro once the recording is finished */
VIS_REG_SEARCH,
VIS_REG_INVALID, /* has to be the last 'real' register */
VIS_REG_A, VIS_REG_B, VIS_REG_C, VIS_REG_D, VIS_REG_E,
VIS_REG_F, VIS_REG_G, VIS_REG_H, VIS_REG_I, VIS_REG_J,
Expand Down Expand Up @@ -412,6 +414,9 @@ bool vis_keys_inject(Vis*, const char *pos, const char *input);
* was handled by vis */
bool vis_signal_handler(Vis*, int signum, const siginfo_t *siginfo, const void *context);

/* remember last search pattern, freeing the regex is the callers responsibility */
Regex *vis_regex(Vis*, const char *pattern);

/* TODO: expose proper API to iterate through files etc */
Text *vis_text(Vis*);
View *vis_view(Vis*);
Expand Down

0 comments on commit 2bbcf71

Please sign in to comment.