patch 7.4.984
Problem: searchpos() always starts searching in the first column, which is
not what some people expect. (Brett Stahlman)
Solution: Add the 'z' flag: start at the specified column.
diff --git a/src/eval.c b/src/eval.c
index 2668f3d..a2d2939 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -16471,6 +16471,7 @@
#define SP_START 0x10 /* accept match at start position */
#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
#define SP_END 0x40 /* leave cursor at end of match */
+#define SP_COLUMN 0x80 /* start at cursor column */
static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
@@ -16512,6 +16513,7 @@
case 'p': mask = SP_SUBPAT; break;
case 'r': mask = SP_REPEAT; break;
case 's': mask = SP_SETPCMARK; break;
+ case 'z': mask = SP_COLUMN; break;
}
if (mask == 0)
{
@@ -16530,7 +16532,7 @@
}
/*
- * Shared by search() and searchpos() functions
+ * Shared by search() and searchpos() functions.
*/
static int
search_cmn(argvars, match_pos, flagsp)
@@ -16562,6 +16564,8 @@
options |= SEARCH_START;
if (flags & SP_END)
options |= SEARCH_END;
+ if (flags & SP_COLUMN)
+ options |= SEARCH_COL;
/* Optional arguments: line number to stop searching and timeout. */
if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
diff --git a/src/search.c b/src/search.c
index 74398e3..1145558 100644
--- a/src/search.c
+++ b/src/search.c
@@ -578,6 +578,7 @@
* if (options & SEARCH_KEEP) keep previous search pattern
* if (options & SEARCH_FOLD) match only once in a closed fold
* if (options & SEARCH_PEEK) check for typed char, cancel search
+ * if (options & SEARCH_COL) start at pos->col instead of zero
*
* Return FAIL (zero) for failure, non-zero for success.
* When FEAT_EVAL is defined, returns the index of the first matching
@@ -599,6 +600,7 @@
{
int found;
linenr_T lnum; /* no init to shut up Apollo cc */
+ colnr_T col;
regmmatch_T regmatch;
char_u *ptr;
colnr_T matchcol;
@@ -711,12 +713,14 @@
/*
* Look for a match somewhere in line "lnum".
*/
+ col = at_first_line && (options & SEARCH_COL) ? pos->col
+ : (colnr_T)0;
nmatched = vim_regexec_multi(®match, win, buf,
- lnum, (colnr_T)0,
+ lnum, col,
#ifdef FEAT_RELTIME
- tm
+ tm
#else
- NULL
+ NULL
#endif
);
/* Abort searching on an error (e.g., out of stack). */
@@ -1098,6 +1102,7 @@
/*
* Return the number of the first subpat that matched.
+ * Return zero if none of them matched.
*/
static int
first_submatch(rp)
diff --git a/src/testdir/test_alot.vim b/src/testdir/test_alot.vim
index 21a5241..f15a2dc 100644
--- a/src/testdir/test_alot.vim
+++ b/src/testdir/test_alot.vim
@@ -2,5 +2,6 @@
" This makes testing go faster, since Vim doesn't need to restart.
source test_lispwords.vim
+source test_searchpos.vim
source test_sort.vim
source test_undolevels.vim
diff --git a/src/testdir/test_searchpos.vim b/src/testdir/test_searchpos.vim
new file mode 100644
index 0000000..4a1e024
--- /dev/null
+++ b/src/testdir/test_searchpos.vim
@@ -0,0 +1,28 @@
+" Tests for searchpos()
+
+func Test_searchpos()
+ new one
+ 0put ='1a3'
+ 1put ='123xyz'
+ call cursor(1, 1)
+ call assert_equal([1, 1, 2], searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW'))
+ call cursor(1, 2)
+ call assert_equal([2, 1, 1], searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW'))
+ set cpo-=c
+ call cursor(1, 2)
+ call assert_equal([1, 2, 2], searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW'))
+ call cursor(1, 3)
+ call assert_equal([1, 3, 1], searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW'))
+
+ " Now with \zs, first match is in column 0, "a" is matched.
+ call cursor(1. 3)
+ call assert_equal([2, 4, 2], searchpos('\%(\([a-z]\)\|\_.\)\{-}\zsxyz', 'pcW'))
+ " With z flag start at cursor column, don't see the "a".
+ call cursor(1. 3)
+ call assert_equal([2, 4, 1], searchpos('\%(\([a-z]\)\|\_.\)\{-}\zsxyz', 'pcWz'))
+
+ set cpo+=c
+ " close the window
+ q!
+
+endfunc
diff --git a/src/version.c b/src/version.c
index 67f8d77..5a5a59c 100644
--- a/src/version.c
+++ b/src/version.c
@@ -742,6 +742,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 984,
+/**/
983,
/**/
982,
diff --git a/src/vim.h b/src/vim.h
index 729c45a..11b9119 100644
--- a/src/vim.h
+++ b/src/vim.h
@@ -930,6 +930,7 @@
#define SEARCH_MARK 0x200 /* set previous context mark */
#define SEARCH_KEEP 0x400 /* keep previous search pattern */
#define SEARCH_PEEK 0x800 /* peek for typed char, cancel search */
+#define SEARCH_COL 0x1000 /* start at specified column instead of zero */
/* Values for find_ident_under_cursor() */
#define FIND_IDENT 1 /* find identifier (word) */