Apologies for the delay.
Review for those who have forgotten and/or don't care to backread:
With this patch, if the user attempts to use a block textobject (parenthesis,
square brackets, curley brackets, angle brackets) and the cursor is not already
within the object, vim will search for the object in a given direction. For
example, if the cursor is on one of the "l"'s below and the user enters
"ci)OK<esc>"
lll(mmm)rrr
the following will result:
lll(OK)rrr
Note that this should not touch any already valid Vim input; only in the
contexts where block text object commands do *not* already work does this do
anything.
I've been using it quite happily for the last two months or so, but more eyes
and testing would not be a bad idea.
Attached is the patch, in both unified and context format, including a test.
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
diff --git a/runtime/doc/motion.txt b/runtime/doc/motion.txt
index d40d825..63d7486 100644
--- a/runtime/doc/motion.txt
+++ b/runtime/doc/motion.txt
@@ -574,6 +574,10 @@ a[ "a [] block", select [count] '[' ']' blocks. This
goes backwards to the [count] unclosed '[', and finds
the matching ']'. The enclosed text is selected,
including the '[' and ']'.
+ If it fails to find the block under or around the
+ cursor, and '[' was specified, it will search the
+ buffer backwards for the block or, if ']' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
i] *v_i]* *v_i[* *i]* *i[*
@@ -581,6 +585,10 @@ i[ "inner [] block", select [count] '[' ']' blocks. This
goes backwards to the [count] unclosed '[', and finds
the matching ']'. The enclosed text is selected,
excluding the '[' and ']'.
+ If it fails to find the block under or around the
+ cursor, and '[' was specified, it will search the
+ buffer backwards for the block or, if ']' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
a) *v_a)* *a)* *a(*
@@ -589,6 +597,10 @@ ab "a block", select [count] blocks, from "[count] [(" to
the matching ')', including the '(' and ')' (see
|[(|). Does not include white space outside of the
parenthesis.
+ If it fails to find the block under or around the
+ cursor, and '(' was specified, it will search the
+ buffer backwards for the block or, if ')' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
i) *v_i)* *i)* *i(*
@@ -596,18 +608,30 @@ i( *v_ib* *v_i(* *ib*
ib "inner block", select [count] blocks, from "[count] [("
to the matching ')', excluding the '(' and ')' (see
|[(|).
+ If it fails to find the block under or around the
+ cursor, and '(' was specified, it will search the
+ buffer backwards for the block or, if ')' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
a> *v_a>* *v_a<* *a>* *a<*
a< "a <> block", select [count] <> blocks, from the
[count]'th unmatched '<' backwards to the matching
'>', including the '<' and '>'.
+ If it fails to find the block under or around the
+ cursor, and '<' was specified, it will search the
+ buffer backwards for the block or, if '>' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
i> *v_i>* *v_i<* *i>* *i<*
i< "inner <> block", select [count] <> blocks, from
the [count]'th unmatched '<' backwards to the matching
'>', excluding the '<' and '>'.
+ If it fails to find the block under or around the
+ cursor, and '<' was specified, it will search the
+ buffer backwards for the block or, if '>' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
*v_at* *at*
@@ -629,6 +653,10 @@ a{ *v_aB* *v_a{* *aB*
aB "a Block", select [count] Blocks, from "[count] [{" to
the matching '}', including the '{' and '}' (see
|[{|).
+ If it fails to find the block under or around the
+ cursor, and '{' was specified, it will search the
+ buffer backwards for the block or, if '}' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
i} *v_i}* *i}* *i{*
@@ -636,6 +664,10 @@ i{ *v_iB* *v_i{* *iB*
iB "inner Block", select [count] Blocks, from "[count] [{"
to the matching '}', excluding the '{' and '}' (see
|[{|).
+ If it fails to find the block under or around the
+ cursor, and '{' was specified, it will search the
+ buffer backwards for the block or, if '}' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
a" *v_aquote* *aquote*
diff --git a/src/normal.c b/src/normal.c
index 66a5b7a..254a17b 100644
--- a/src/normal.c
+++ b/src/normal.c
@@ -9269,6 +9269,26 @@ nv_object(cap)
mps_save = curbuf->b_p_mps;
curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
+ /* Set search direction */
+ int dir;
+ switch (cap->nchar)
+ {
+ case '(':
+ case '{':
+ case '[':
+ case '<':
+ dir = BACKWARD;
+ break;
+ case ')':
+ case '}':
+ case ']':
+ case '>':
+ dir = FORWARD;
+ break;
+ default:
+ dir = 0;
+ }
+
switch (cap->nchar)
{
case 'w': /* "aw" = a word */
@@ -9280,20 +9300,20 @@ nv_object(cap)
case 'b': /* "ab" = a braces block */
case '(':
case ')':
- flag = current_block(cap->oap, cap->count1, include, '(', ')');
+ flag = current_block(cap->oap, cap->count1, include, '(', ')', dir);
break;
case 'B': /* "aB" = a Brackets block */
case '{':
case '}':
- flag = current_block(cap->oap, cap->count1, include, '{', '}');
+ flag = current_block(cap->oap, cap->count1, include, '{', '}', dir);
break;
case '[': /* "a[" = a [] block */
case ']':
- flag = current_block(cap->oap, cap->count1, include, '[', ']');
+ flag = current_block(cap->oap, cap->count1, include, '[', ']', dir);
break;
case '<': /* "a<" = a <> block */
case '>':
- flag = current_block(cap->oap, cap->count1, include, '<', '>');
+ flag = current_block(cap->oap, cap->count1, include, '<', '>', dir);
break;
case 't': /* "at" = a tag block (xml and html) */
flag = current_tagblock(cap->oap, cap->count1, include);
diff --git a/src/proto/search.pro b/src/proto/search.pro
index f94fb69..ef1f44c 100644
--- a/src/proto/search.pro
+++ b/src/proto/search.pro
@@ -28,7 +28,7 @@ int end_word __ARGS((long count, int bigword, int stop, int empty));
int bckend_word __ARGS((long count, int bigword, int eol));
int current_word __ARGS((oparg_T *oap, long count, int include, int bigword));
int current_sent __ARGS((oparg_T *oap, long count, int include));
-int current_block __ARGS((oparg_T *oap, long count, int include, int what, int other));
+int current_block __ARGS((oparg_T *oap, long count, int include, int what, int other, int dir));
int current_tagblock __ARGS((oparg_T *oap, long count_arg, int include));
int current_par __ARGS((oparg_T *oap, long count, int include, int type));
int current_quote __ARGS((oparg_T *oap, long count, int include, int quotechar));
diff --git a/src/search.c b/src/search.c
index 0341a5e..020bf2d 100644
--- a/src/search.c
+++ b/src/search.c
@@ -3552,12 +3552,13 @@ extend:
* "what" and "other" are two matching parenthesis/brace/etc.
*/
int
-current_block(oap, count, include, what, other)
+current_block(oap, count, include, what, other, dir)
oparg_T *oap;
long count;
int include; /* TRUE == include white space */
int what; /* '(', '{', etc. */
int other; /* ')', '}', etc. */
+ int dir; /* direction to search if not in object */
{
pos_T old_pos;
pos_T *pos = NULL;
@@ -3619,8 +3620,68 @@ current_block(oap, count, include, what, other)
*/
if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
{
- curwin->w_cursor = old_pos;
- return FAIL;
+ /*
+ * Cursor is not in or on the desired object; however, the object may
+ * still be in the buffer. Search for it in the specified direction.
+ */
+
+ /*
+ * Ensure a direction is set; abort otherwise.
+ */
+ if (dir != FORWARD && dir != BACKWARD)
+ {
+ curwin->w_cursor = old_pos;
+ return FAIL;
+ }
+
+ /*
+ * Search in specified direction for appropriate character
+ */
+ while (1)
+ {
+ if (((dir == FORWARD) ? inc_cursor() : dec_cursor()) == -1)
+ {
+ /*
+ * Ran into end of the buffer without finding desired bound;
+ * abort.
+ */
+ curwin->w_cursor = old_pos;
+ return FAIL;
+ }
+ if (((dir == FORWARD) ? what : other) == gchar_cursor())
+ {
+ /*
+ * Found desired character, stop searching
+ */
+ break;
+ }
+ }
+
+ /*
+ * Find matching bound.
+ */
+ if ((pos = findmatch(NULL, ((dir == FORWARD) ? other : what))) == NULL)
+ {
+ /*
+ * Failed to find matching bound, abort.
+ */
+ curwin->w_cursor = old_pos;
+ return FAIL;
+ }
+
+ /*
+ * Found both bounds, so set them accordingly.
+ */
+ if (dir == FORWARD)
+ {
+ start_pos = curwin->w_cursor;
+ end_pos = pos;
+ }
+ else
+ {
+ start_pos = *pos;
+ end_pos = &curwin->w_cursor;
+ }
}
curwin->w_cursor = *end_pos;
diff --git a/src/testdir/test104.in b/src/testdir/test104.in
new file mode 100644
index 0000000..2f37728
--- /dev/null
+++ b/src/testdir/test104.in
@@ -0,0 +1,46 @@
+Test for block text object search
+
+Specifically, tests:
+- Being within the object (so no need for a search)
+- Being left of the object
+- Being right of the object
+- for every type of block supported
+
+STARTTEST
+:so small.vim
+:enew
+:"
+:" Create six lines with an object on each for every block type
+:" - three for left, inside and right
+:" - pairs to use i and a
+:"
+:for blockpair in ["()", "{}", "[]", "<>"]
+:for i in range(0,2) " left, inside, right
+:for j in range(0,1) " i, a
+:execute "normal olll" . blockpair[0] . "mmm" . blockpair[1] . "rrr"
+:endfor
+:endfor
+:endfor
+:"
+:" Apply the ci<object> and ca<object>, searching left, being in and searching right
+:"
+:1
+:for blockpair in ["()", "{}", "[]", "<>"]
+: +
+: execute "normal 0ci" . blockpair[1] . "OK\<esc>"
+: +
+: execute "normal $ci" . blockpair[0] . "OK\<esc>"
+: +
+: execute "normal 6|ci" . blockpair[0] . "OK\<esc>"
+: +
+: execute "normal 0ca" . blockpair[1] . "OK\<esc>"
+: +
+: execute "normal $ca" . blockpair[0] . "OK\<esc>"
+: +
+: execute "normal 6|ca" . blockpair[0] . "OK\<esc>"
+:endfor
+:
+:w! test.out
+:qa!
+ENDTEST
+
diff --git a/src/testdir/test104.ok b/src/testdir/test104.ok
new file mode 100644
index 0000000..2361a27
--- /dev/null
+++ b/src/testdir/test104.ok
@@ -0,0 +1,25 @@
+
+lll(OK)rrr
+lll(OK)rrr
+lll(OK)rrr
+lllOKrrr
+lllOKrrr
+lllOKrrr
+lll{OK}rrr
+lll{OK}rrr
+lll{OK}rrr
+lllOKrrr
+lllOKrrr
+lllOKrrr
+lll[OK]rrr
+lll[OK]rrr
+lll[OK]rrr
+lllOKrrr
+lllOKrrr
+lllOKrrr
+lll<OK>rrr
+lll<OK>rrr
+lll<OK>rrr
+lllOKrrr
+lllOKrrr
+lllOKrrr
*** a/runtime/doc/motion.txt
--- b/runtime/doc/motion.txt
***************
*** 574,579 **** a[ "a [] block", select [count] '[' ']' blocks. This
--- 574,583 ----
goes backwards to the [count] unclosed '[', and finds
the matching ']'. The enclosed text is selected,
including the '[' and ']'.
+ If it fails to find the block under or around the
+ cursor, and '[' was specified, it will search the
+ buffer backwards for the block or, if ']' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
i] *v_i]* *v_i[* *i]* *i[*
***************
*** 581,586 **** i[ "inner [] block", select [count] '[' ']' blocks. This
--- 585,594 ----
goes backwards to the [count] unclosed '[', and finds
the matching ']'. The enclosed text is selected,
excluding the '[' and ']'.
+ If it fails to find the block under or around the
+ cursor, and '[' was specified, it will search the
+ buffer backwards for the block or, if ']' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
a) *v_a)* *a)* *a(*
***************
*** 589,594 **** ab "a block", select [count] blocks, from "[count] [(" to
--- 597,606 ----
the matching ')', including the '(' and ')' (see
|[(|). Does not include white space outside of the
parenthesis.
+ If it fails to find the block under or around the
+ cursor, and '(' was specified, it will search the
+ buffer backwards for the block or, if ')' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
i) *v_i)* *i)* *i(*
***************
*** 596,613 **** i( *v_ib* *v_i(* *ib*
--- 608,637 ----
ib "inner block", select [count] blocks, from "[count] [("
to the matching ')', excluding the '(' and ')' (see
|[(|).
+ If it fails to find the block under or around the
+ cursor, and '(' was specified, it will search the
+ buffer backwards for the block or, if ')' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
a> *v_a>* *v_a<* *a>* *a<*
a< "a <> block", select [count] <> blocks, from the
[count]'th unmatched '<' backwards to the matching
'>', including the '<' and '>'.
+ If it fails to find the block under or around the
+ cursor, and '<' was specified, it will search the
+ buffer backwards for the block or, if '>' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
i> *v_i>* *v_i<* *i>* *i<*
i< "inner <> block", select [count] <> blocks, from
the [count]'th unmatched '<' backwards to the matching
'>', excluding the '<' and '>'.
+ If it fails to find the block under or around the
+ cursor, and '<' was specified, it will search the
+ buffer backwards for the block or, if '>' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
*v_at* *at*
***************
*** 629,634 **** a{ *v_aB* *v_a{* *aB*
--- 653,662 ----
aB "a Block", select [count] Blocks, from "[count] [{" to
the matching '}', including the '{' and '}' (see
|[{|).
+ If it fails to find the block under or around the
+ cursor, and '{' was specified, it will search the
+ buffer backwards for the block or, if '}' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
i} *v_i}* *i}* *i{*
***************
*** 636,641 **** i{ *v_iB* *v_i{* *iB*
--- 664,673 ----
iB "inner Block", select [count] Blocks, from "[count] [{"
to the matching '}', excluding the '{' and '}' (see
|[{|).
+ If it fails to find the block under or around the
+ cursor, and '{' was specified, it will search the
+ buffer backwards for the block or, if '}' was
+ specified, it will search the buffer forwards.
When used in Visual mode it is made characterwise.
a" *v_aquote* *aquote*
*** a/src/normal.c
--- b/src/normal.c
***************
*** 9269,9274 **** nv_object(cap)
--- 9269,9294 ----
mps_save = curbuf->b_p_mps;
curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
+ /* Set search direction */
+ int dir;
+ switch (cap->nchar)
+ {
+ case '(':
+ case '{':
+ case '[':
+ case '<':
+ dir = BACKWARD;
+ break;
+ case ')':
+ case '}':
+ case ']':
+ case '>':
+ dir = FORWARD;
+ break;
+ default:
+ dir = 0;
+ }
+
switch (cap->nchar)
{
case 'w': /* "aw" = a word */
***************
*** 9280,9299 **** nv_object(cap)
case 'b': /* "ab" = a braces block */
case '(':
case ')':
! flag = current_block(cap->oap, cap->count1, include, '(', ')');
break;
case 'B': /* "aB" = a Brackets block */
case '{':
case '}':
! flag = current_block(cap->oap, cap->count1, include, '{', '}');
break;
case '[': /* "a[" = a [] block */
case ']':
! flag = current_block(cap->oap, cap->count1, include, '[', ']');
break;
case '<': /* "a<" = a <> block */
case '>':
! flag = current_block(cap->oap, cap->count1, include, '<', '>');
break;
case 't': /* "at" = a tag block (xml and html) */
flag = current_tagblock(cap->oap, cap->count1, include);
--- 9300,9319 ----
case 'b': /* "ab" = a braces block */
case '(':
case ')':
! flag = current_block(cap->oap, cap->count1, include, '(', ')', dir);
break;
case 'B': /* "aB" = a Brackets block */
case '{':
case '}':
! flag = current_block(cap->oap, cap->count1, include, '{', '}', dir);
break;
case '[': /* "a[" = a [] block */
case ']':
! flag = current_block(cap->oap, cap->count1, include, '[', ']', dir);
break;
case '<': /* "a<" = a <> block */
case '>':
! flag = current_block(cap->oap, cap->count1, include, '<', '>', dir);
break;
case 't': /* "at" = a tag block (xml and html) */
flag = current_tagblock(cap->oap, cap->count1, include);
*** a/src/proto/search.pro
--- b/src/proto/search.pro
***************
*** 28,34 **** int end_word __ARGS((long count, int bigword, int stop, int empty));
int bckend_word __ARGS((long count, int bigword, int eol));
int current_word __ARGS((oparg_T *oap, long count, int include, int bigword));
int current_sent __ARGS((oparg_T *oap, long count, int include));
! int current_block __ARGS((oparg_T *oap, long count, int include, int what, int other));
int current_tagblock __ARGS((oparg_T *oap, long count_arg, int include));
int current_par __ARGS((oparg_T *oap, long count, int include, int type));
int current_quote __ARGS((oparg_T *oap, long count, int include, int quotechar));
--- 28,34 ----
int bckend_word __ARGS((long count, int bigword, int eol));
int current_word __ARGS((oparg_T *oap, long count, int include, int bigword));
int current_sent __ARGS((oparg_T *oap, long count, int include));
! int current_block __ARGS((oparg_T *oap, long count, int include, int what, int other, int dir));
int current_tagblock __ARGS((oparg_T *oap, long count_arg, int include));
int current_par __ARGS((oparg_T *oap, long count, int include, int type));
int current_quote __ARGS((oparg_T *oap, long count, int include, int quotechar));
*** a/src/search.c
--- b/src/search.c
***************
*** 3552,3563 **** extend:
* "what" and "other" are two matching parenthesis/brace/etc.
*/
int
! current_block(oap, count, include, what, other)
oparg_T *oap;
long count;
int include; /* TRUE == include white space */
int what; /* '(', '{', etc. */
int other; /* ')', '}', etc. */
{
pos_T old_pos;
pos_T *pos = NULL;
--- 3552,3564 ----
* "what" and "other" are two matching parenthesis/brace/etc.
*/
int
! current_block(oap, count, include, what, other, dir)
oparg_T *oap;
long count;
int include; /* TRUE == include white space */
int what; /* '(', '{', etc. */
int other; /* ')', '}', etc. */
+ int dir; /* direction to search if not in object */
{
pos_T old_pos;
pos_T *pos = NULL;
***************
*** 3619,3626 **** current_block(oap, count, include, what, other)
*/
if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
{
! curwin->w_cursor = old_pos;
! return FAIL;
}
curwin->w_cursor = *end_pos;
--- 3620,3687 ----
*/
if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
{
! /*
! * Cursor is not in or on the desired object; however, the object may
! * still be in the buffer. Search for it in the specified direction.
! */
!
! /*
! * Ensure a direction is set; abort otherwise.
! */
! if (dir != FORWARD && dir != BACKWARD)
! {
! curwin->w_cursor = old_pos;
! return FAIL;
! }
!
! /*
! * Search in specified direction for appropriate character
! */
! while (1)
! {
! if (((dir == FORWARD) ? inc_cursor() : dec_cursor()) == -1)
! {
! /*
! * Ran into end of the buffer without finding desired bound;
! * abort.
! */
! curwin->w_cursor = old_pos;
! return FAIL;
! }
! if (((dir == FORWARD) ? what : other) == gchar_cursor())
! {
! /*
! * Found desired character, stop searching
! */
! break;
! }
! }
!
! /*
! * Find matching bound.
! */
! if ((pos = findmatch(NULL, ((dir == FORWARD) ? other : what))) == NULL)
! {
! /*
! * Failed to find matching bound, abort.
! */
! curwin->w_cursor = old_pos;
! return FAIL;
! }
!
! /*
! * Found both bounds, so set them accordingly.
! */
! if (dir == FORWARD)
! {
! start_pos = curwin->w_cursor;
! end_pos = pos;
! }
! else
! {
! start_pos = *pos;
! end_pos = &curwin->w_cursor;
! }
}
curwin->w_cursor = *end_pos;
*** /dev/null
--- b/src/testdir/test104.in
***************
*** 0 ****
--- 1,46 ----
+ Test for block text object search
+
+ Specifically, tests:
+ - Being within the object (so no need for a search)
+ - Being left of the object
+ - Being right of the object
+ - for every type of block supported
+
+ STARTTEST
+ :so small.vim
+ :enew
+ :"
+ :" Create six lines with an object on each for every block type
+ :" - three for left, inside and right
+ :" - pairs to use i and a
+ :"
+ :for blockpair in ["()", "{}", "[]", "<>"]
+ :for i in range(0,2) " left, inside, right
+ :for j in range(0,1) " i, a
+ :execute "normal olll" . blockpair[0] . "mmm" . blockpair[1] . "rrr"
+ :endfor
+ :endfor
+ :endfor
+ :"
+ :" Apply the ci<object> and ca<object>, searching left, being in and searching right
+ :"
+ :1
+ :for blockpair in ["()", "{}", "[]", "<>"]
+ : +
+ : execute "normal 0ci" . blockpair[1] . "OK\<esc>"
+ : +
+ : execute "normal $ci" . blockpair[0] . "OK\<esc>"
+ : +
+ : execute "normal 6|ci" . blockpair[0] . "OK\<esc>"
+ : +
+ : execute "normal 0ca" . blockpair[1] . "OK\<esc>"
+ : +
+ : execute "normal $ca" . blockpair[0] . "OK\<esc>"
+ : +
+ : execute "normal 6|ca" . blockpair[0] . "OK\<esc>"
+ :endfor
+ :
+ :w! test.out
+ :qa!
+ ENDTEST
+
*** /dev/null
--- b/src/testdir/test104.ok
***************
*** 0 ****
--- 1,25 ----
+
+ lll(OK)rrr
+ lll(OK)rrr
+ lll(OK)rrr
+ lllOKrrr
+ lllOKrrr
+ lllOKrrr
+ lll{OK}rrr
+ lll{OK}rrr
+ lll{OK}rrr
+ lllOKrrr
+ lllOKrrr
+ lllOKrrr
+ lll[OK]rrr
+ lll[OK]rrr
+ lll[OK]rrr
+ lllOKrrr
+ lllOKrrr
+ lllOKrrr
+ lll<OK>rrr
+ lll<OK>rrr
+ lll<OK>rrr
+ lllOKrrr
+ lllOKrrr
+ lllOKrrr