~martanne/devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
3 2

[PATCH 1/2] vis: refactor selections_match_next

Evan Gates
Details
Message ID
<20200924214830.24617-1-evan.gates@gmail.com>
DKIM signature
pass
Download raw message
Patch: +24 -33
A lot of code from selections_match_next was duplicated in
selections_match_next_literal.  Add text_literal_find_next (and prev)
that mirrors text_object_word_find_next so they can be swapped out in
a single function.
---
 main.c | 57 ++++++++++++++++++++++++---------------------------------
 1 file changed, 24 insertions(+), 33 deletions(-)

diff --git a/main.c b/main.c
index eb855ae..17a8fd9 100644
--- a/main.c
+++ b/main.c
@@ -1337,35 +1337,22 @@ static const Selection *selection_new_primary(View *view, Filerange *r) {
	return s;
}

static const char *selections_match_next_literal(Vis *vis, const char *keys, const Arg *arg) {
	Text *txt = vis_text(vis);
	View *view = vis_view(vis);
	Selection *s = view_selections_primary_get(view);
	Filerange sel = view_selections_get(s);
	size_t len = text_range_size(&sel);
	if (!len)
		return keys;

	char *buf = text_bytes_alloc0(txt, sel.start, len);
	if (!buf)
		return keys;

	size_t start = text_find_next(txt, sel.end, buf);
	Filerange match = text_range_new(start, start+len);
	if (start != sel.end && selection_new_primary(view, &match))
		goto out;

	sel = view_selections_get(view_selections(view));
	start = text_find_prev(txt, sel.start, buf);
	if (start == sel.start)
		goto out;

	match = text_range_new(start, start+len);
	selection_new_primary(view, &match);

out:
	free(buf);
	return keys;
static Filerange text_literal_find(
	Text *txt, size_t pos, const char *search,
	size_t (*text_find)(Text *, size_t, const char *)
) {
	size_t start = text_find(txt, pos, search);
	if (start == pos)
		return text_range_empty();

	size_t len = strlen(search);
	return text_range_new(start, start+len);
}
static Filerange text_literal_find_next(Text *txt, size_t pos, const char *search) {
	return text_literal_find(txt, pos, search, text_find_next);
}
static Filerange text_literal_find_prev(Text *txt, size_t pos, const char *search) {
	return text_literal_find(txt, pos, search, text_find_prev);
}

static const char *selections_match_next(Vis *vis, const char *keys, const Arg *arg) {
@@ -1383,19 +1370,23 @@ static const char *selections_match_next(Vis *vis, const char *keys, const Arg *
		match_word = text_range_equal(&sel, &word);
	}

	if (!match_word)
		return selections_match_next_literal(vis, keys, arg);
	Filerange (*find_next)(Text *, size_t, const char *) = text_object_word_find_next;
	Filerange (*find_prev)(Text *, size_t, const char *) = text_object_word_find_prev;
	if (!match_word) {
		find_next = text_literal_find_next;
		find_prev = text_literal_find_prev;
	}

	char *buf = text_bytes_alloc0(txt, sel.start, text_range_size(&sel));
	if (!buf)
		return keys;

	Filerange word = text_object_word_find_next(txt, sel.end, buf);
	Filerange word = find_next(txt, sel.end, buf);
	if (text_range_valid(&word) && selection_new_primary(view, &word))
		goto out;

	sel = view_selections_get(view_selections(view));
	word = text_object_word_find_prev(txt, sel.start, buf);
	word = find_prev(txt, sel.start, buf);
	if (!text_range_valid(&word))
		goto out;
	selection_new_primary(view, &word);
-- 
2.28.0

[PATCH 2/2] vis: add vis-selection-new-match-all

Evan Gates
Details
Message ID
<20200924214830.24617-2-evan.gates@gmail.com>
In-Reply-To
<20200924214830.24617-1-evan.gates@gmail.com> (view parent)
DKIM signature
pass
Download raw message
Patch: +30 -9
Add new vis-selection-new-match-all command, default keybinding <C-a>
in visual mode.  Refactor selections_next_match to find all matches if
arg.b is true.  This does not affect existing configs as arg.b defaults
to false.
---
 config.def.h |  1 +
 main.c       | 35 ++++++++++++++++++++++++++---------
 man/vis.1    |  3 +++
 3 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/config.def.h b/config.def.h
index 6a4555e..61b97b3 100644
--- a/config.def.h
+++ b/config.def.h
@@ -243,6 +243,7 @@ static const KeyBinding bindings_visual[] = {
	{ "+",                  ACTION(SELECTIONS_ROTATE_RIGHT)             },
	{ "<",                  ALIAS("<vis-operator-shift-left>gv")        },
	{ ">",                  ALIAS("<vis-operator-shift-right>gv")       },
	{ "<C-a>",              ACTION(SELECTIONS_NEW_MATCH_ALL)            },
	{ "<C-b>",              ALIAS("<PageUp>")                           },
	{ "<C-c>",              ACTION(SELECTIONS_REMOVE_COLUMN)            },
	{ "<C-d>",              ACTION(SELECTIONS_NEXT)                     },
diff --git a/main.c b/main.c
index 17a8fd9..1d095f5 100644
--- a/main.c
+++ b/main.c
@@ -260,6 +260,7 @@ enum {
	VIS_ACTION_SELECTIONS_NEW_LINE_BELOW_LAST,
	VIS_ACTION_SELECTIONS_NEW_LINES_BEGIN,
	VIS_ACTION_SELECTIONS_NEW_LINES_END,
	VIS_ACTION_SELECTIONS_NEW_MATCH_ALL,
	VIS_ACTION_SELECTIONS_NEW_MATCH_NEXT,
	VIS_ACTION_SELECTIONS_NEW_MATCH_SKIP,
	VIS_ACTION_SELECTIONS_ALIGN,
@@ -917,6 +918,11 @@ static const KeyAction vis_action[] = {
		VIS_HELP("Create a new selection at the end of every line covered by selection")
		operator, { .i = VIS_OP_CURSOR_EOL }
	},
	[VIS_ACTION_SELECTIONS_NEW_MATCH_ALL] = {
		"vis-selection-new-match-all",
		VIS_HELP("Select all regions matching the current selection")
		selections_match_next, { .b = true }
	},
	[VIS_ACTION_SELECTIONS_NEW_MATCH_NEXT] = {
		"vis-selection-new-match-next",
		VIS_HELP("Select the next region matching the current selection")
@@ -1325,7 +1331,7 @@ static const char *selections_clear(Vis *vis, const char *keys, const Arg *arg)
	return keys;
}

static const Selection *selection_new_primary(View *view, Filerange *r) {
static Selection *selection_new_primary(View *view, Filerange *r) {
	Text *txt = view_text(view);
	size_t pos = text_char_prev(txt, r->end);
	Selection *s = view_selections_new(view, pos);
@@ -1381,15 +1387,26 @@ static const char *selections_match_next(Vis *vis, const char *keys, const Arg *
	if (!buf)
		return keys;

	Filerange word = find_next(txt, sel.end, buf);
	if (text_range_valid(&word) && selection_new_primary(view, &word))
		goto out;
	bool match_all = arg->b;

	for (;;) {
		sel = view_selections_get(s);
		Filerange word = find_next(txt, sel.end, buf);
		if (!text_range_valid(&word) || !(s = selection_new_primary(view, &word)))
			break;
		if (!match_all)
			goto out;
	}

	sel = view_selections_get(view_selections(view));
	word = find_prev(txt, sel.start, buf);
	if (!text_range_valid(&word))
		goto out;
	selection_new_primary(view, &word);
	for (;;) {
		sel = view_selections_get(view_selections(view));
		Filerange word = find_prev(txt, sel.start, buf);
		if (!text_range_valid(&word))
			break;
		selection_new_primary(view, &word);
		if (!match_all)
			break;
	}

out:
	free(buf);
diff --git a/man/vis.1 b/man/vis.1
index 10ad5bb..4082997 100644
--- a/man/vis.1
+++ b/man/vis.1
@@ -1097,6 +1097,9 @@ left align selections by inserting spaces
.It Aq Ic S-Tab
right align selections by inserting spaces
.
.It Aq Ic C-a
create new selections everywhere matching current word or selection
.
.It Aq Ic C-n
create new selection and select next word matching current selection
.
-- 
2.28.0
Details
Message ID
<20200925062423.GA77810@thinkpad.localdomain>
In-Reply-To
<20200924214830.24617-1-evan.gates@gmail.com> (view parent)
DKIM signature
missing
Download raw message
On Thu, Sep 24, 2020 at 02:48:29PM -0700, Evan Gates wrote:
> A lot of code from selections_match_next was duplicated in
> selections_match_next_literal.  Add text_literal_find_next (and prev)
> that mirrors text_object_word_find_next so they can be swapped out in
> a single function.

The new utility functions should be placed in text-objects.[ch] as
text_object_find_{next,prev}, they might be useful eslsewhere. A separate
patch introducing them would be nice, otherwise this looks OK.

Marc

Re: [PATCH 2/2] vis: add vis-selection-new-match-all

Details
Message ID
<20200925063656.GB77810@thinkpad.localdomain>
In-Reply-To
<20200924214830.24617-2-evan.gates@gmail.com> (view parent)
DKIM signature
missing
Download raw message
On Thu, Sep 24, 2020 at 02:48:30PM -0700, Evan Gates wrote:
> Add new vis-selection-new-match-all command, default keybinding <C-a>
> in visual mode.  Refactor selections_next_match to find all matches if
> arg.b is true.  This does not affect existing configs as arg.b defaults
> to false.

I think this should keep the primary selection, i.e. the viewport should
not change just select all the other matches.

What do you think?

Marc
Reply to thread Export thread (mbox)