From 757366472b7c70e54e0e145b286ea01090ee5dbc Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 26 Mar 2015 13:13:05 +0000 Subject: [PATCH] Add for ensuring selections are forward (cursor >= anchor) Not very useful interactively, but that feature can make macros much more robust. --- README.asciidoc | 1 + src/normal.cc | 6 ++++++ src/selectors.hh | 13 ++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.asciidoc b/README.asciidoc index e6f9daddb..d197a0ef3 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -195,6 +195,7 @@ Basic Movement * +;+: reduce selections to their cursor * +alt-;+: flip the selections direction + * +alt-:+: ensure selections are in forward direction (cursor after anchor) A word is a sequence of alphanumeric characters or underscore, a WORD is a diff --git a/src/normal.cc b/src/normal.cc index 1d4d1cba4..eec197524 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1373,6 +1373,11 @@ void flip_selections(Context& context, NormalParams) flip_selections(context.selections()); } +void ensure_forward(Context& context, NormalParams) +{ + ensure_forward(context.selections()); +} + static NormalCmdDesc cmds[] = { { 'h', "move left", move }, @@ -1434,6 +1439,7 @@ static NormalCmdDesc cmds[] = { alt(' '), "remove main selection", remove_selection }, { ';', "reduce selections to their cursor", clear_selections }, { alt(';'), "swap selections cursor and anchor", flip_selections }, + { alt(':'), "ensure selection cursor is after anchor", ensure_forward }, { 'w', "select to next word start", repeated<&select>> }, { 'e', "select to next word end", repeated>> }, diff --git a/src/selectors.hh b/src/selectors.hh index cdd89dcb6..e03c479cd 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -28,13 +28,24 @@ inline void flip_selections(SelectionList& selections) { for (auto& sel : selections) { - ByteCoord tmp = sel.anchor(); + const ByteCoord tmp = sel.anchor(); sel.anchor() = sel.cursor(); sel.cursor() = tmp; } selections.check_invariant(); } +inline void ensure_forward(SelectionList& selections) +{ + for (auto& sel : selections) + { + const ByteCoord min = sel.min(), max = sel.max(); + sel.anchor() = min; + sel.cursor() = max; + } + selections.check_invariant(); +} + inline void keep_selection(SelectionList& selections, int index) { if (index < selections.size())