aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Ennen <mike.ennen@gmail.com>2016-11-24 23:13:30 -0700
committerMichael Ennen <mike.ennen@gmail.com>2016-12-06 15:52:48 -0700
commit6fea2dfd26cb902d7b2f52f53ccca9befef2b442 (patch)
treea12933cbd0a18015e69221df92e7d9294ceaeb44
parent61150294967f2522cb1774e29e49a3386d3bcee5 (diff)
downloadrneovim-6fea2dfd26cb902d7b2f52f53ccca9befef2b442.tar.gz
rneovim-6fea2dfd26cb902d7b2f52f53ccca9befef2b442.tar.bz2
rneovim-6fea2dfd26cb902d7b2f52f53ccca9befef2b442.zip
vim-patch:7.4.1685
Problem: There is no easy way to get all the information about a match. Solution: Add matchstrpos(). (Ozaki Kiichi) https://github.com/vim/vim/commit/7fed5c18f8577b75404b80d8b9a9907b1bbd27e4
-rw-r--r--runtime/doc/eval.txt20
-rw-r--r--runtime/doc/usr_41.txt1
-rw-r--r--src/nvim/eval.c37
-rw-r--r--src/nvim/eval.lua1
-rw-r--r--src/nvim/testdir/test_alot.vim1
-rw-r--r--src/nvim/testdir/test_matchstrpos.vim13
-rw-r--r--src/nvim/version.c2
7 files changed, 70 insertions, 5 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index cca53db531..53f14c7d47 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -2032,6 +2032,8 @@ matchlist({expr}, {pat}[, {start}[, {count}]])
List match and submatches of {pat} in {expr}
matchstr({expr}, {pat}[, {start}[, {count}]])
String {count}'th match of {pat} in {expr}
+matchstrpos( {expr}, {pat}[, {start}[, {count}]])
+ List {count}'th match of {pat} in {expr}
max({list}) Number maximum value of items in {list}
min({list}) Number minimum value of items in {list}
mkdir({name} [, {path} [, {prot}]])
@@ -5019,6 +5021,24 @@ matchstr({expr}, {pat}[, {start}[, {count}]]) *matchstr()*
When {expr} is a |List| then the matching item is returned.
The type isn't changed, it's not necessarily a String.
+matchstrpos({expr}, {pat}[, {start}[, {count}]]) *matchstrpos()*
+ Same as |matchstr()|, but return the matched string, the start
+ position and the end position of the match. Example: >
+ :echo matchstrpos("testing", "ing")
+< results in ["ing", 4, 7].
+ When there is no match ["", -1, -1] is returned.
+ The {start}, if given, has the same meaning as for |match()|. >
+ :echo matchstrpos("testing", "ing", 2)
+< results in ["ing", 4, 7]. >
+ :echo matchstrpos("testing", "ing", 5)
+< result is ["", -1, -1].
+ When {expr} is a |List| then the matching item, the index
+ of first item where {pat} matches, the start position and the
+ end position of the match are returned. >
+ :echo matchstrpos([1, '__x'], '\a')
+< result is ["x", 1, 2, 3].
+ The type isn't changed, it's not necessarily a String.
+
*max()*
max({list}) Return the maximum value of all items in {list}.
If {list} is not a list or one of the items in {list} cannot
diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt
index 4d3ad49f1f..2896611274 100644
--- a/runtime/doc/usr_41.txt
+++ b/runtime/doc/usr_41.txt
@@ -592,6 +592,7 @@ String manipulation: *string-functions*
match() position where a pattern matches in a string
matchend() position where a pattern match ends in a string
matchstr() match of a pattern in a string
+ matchstrpos() match and postions of a pattern in a string
matchlist() like matchstr() and also return submatches
stridx() first index of a short string in a long string
strridx() last index of a short string in a long string
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 8e8d36b442..ba5864fe3b 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -12211,9 +12211,16 @@ static void find_some_match(typval_T *argvars, typval_T *rettv, int type)
p_cpo = (char_u *)"";
rettv->vval.v_number = -1;
- if (type == 3) {
- /* return empty list when there are no matches */
+ if (type == 3 || type == 4) {
+ // type 3: return empty list when there are no matches.
+ // type 4: return ["", -1, -1, -1]
rettv_list_alloc(rettv);
+ if (type == 4) {
+ list_append_string(rettv->vval.v_list, (char_u *)"", 0);
+ list_append_number(rettv->vval.v_list, (varnumber_T)-1);
+ list_append_number(rettv->vval.v_list, (varnumber_T)-1);
+ list_append_number(rettv->vval.v_list, (varnumber_T)-1);
+ }
} else if (type == 2) {
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
@@ -12276,7 +12283,7 @@ static void find_some_match(typval_T *argvars, typval_T *rettv, int type)
break;
}
xfree(tofree);
- tofree = str = (char_u *) encode_tv2echo(&li->li_tv, NULL);
+ tofree = expr = str = (char_u *)encode_tv2echo(&li->li_tv, NULL);
if (str == NULL) {
break;
}
@@ -12304,7 +12311,19 @@ static void find_some_match(typval_T *argvars, typval_T *rettv, int type)
}
if (match) {
- if (type == 3) {
+ if (type == 4) {
+ listitem_T *li1 = rettv->vval.v_list->lv_first;
+ listitem_T *li2 = li1->li_next;
+ listitem_T *li3 = li2->li_next;
+ listitem_T *li4 = li3->li_next;
+ int rd = (int)(regmatch.endp[0] - regmatch.startp[0]);
+ li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0], rd);
+ li3->li_tv.vval.v_number = (varnumber_T)(regmatch.startp[0] - expr);
+ li4->li_tv.vval.v_number = (varnumber_T)(regmatch.endp[0] - expr);
+ if (l != NULL) {
+ li2->li_tv.vval.v_number = (varnumber_T)idx;
+ }
+ } else if (type == 3) {
int i;
/* return list with matched string and submatches */
@@ -12339,6 +12358,11 @@ static void find_some_match(typval_T *argvars, typval_T *rettv, int type)
vim_regfree(regmatch.regprog);
}
+ if (type == 4 && l == NULL) {
+ // matchstrpos() without a list: drop the second item
+ listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first->li_next);
+ }
+
theend:
xfree(tofree);
p_cpo = save_cpo;
@@ -12511,6 +12535,11 @@ static void f_matchstr(typval_T *argvars, typval_T *rettv, FunPtr fptr)
find_some_match(argvars, rettv, 2);
}
+/// "matchstrpos()" function
+static void f_matchstrpos(typval_T *argvars, typval_T *rettv, FunPtr fptr)
+{
+ find_some_match(argvars, rettv, 4);
+}
static void max_min(typval_T *argvars, typval_T *rettv, int domax)
{
diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua
index bea25b36f3..61a5438d8c 100644
--- a/src/nvim/eval.lua
+++ b/src/nvim/eval.lua
@@ -198,6 +198,7 @@ return {
matchend={args={2, 4}},
matchlist={args={2, 4}},
matchstr={args={2, 4}},
+ matchstrpos={args={2,4}},
max={args=1},
min={args=1},
mkdir={args={1, 3}},
diff --git a/src/nvim/testdir/test_alot.vim b/src/nvim/testdir/test_alot.vim
index c9d7b332e4..58ed57499f 100644
--- a/src/nvim/testdir/test_alot.vim
+++ b/src/nvim/testdir/test_alot.vim
@@ -9,6 +9,7 @@ source test_expr.vim
source test_expr_utf8.vim
source test_feedkeys.vim
source test_goto.vim
+source test_matchstrpos.vim
source test_menu.vim
source test_messages.vim
source test_options.vim
diff --git a/src/nvim/testdir/test_matchstrpos.vim b/src/nvim/testdir/test_matchstrpos.vim
new file mode 100644
index 0000000000..e14765b26b
--- /dev/null
+++ b/src/nvim/testdir/test_matchstrpos.vim
@@ -0,0 +1,13 @@
+" Test matchstrpos
+
+func Test_matchstrpos()
+ call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
+
+ call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
+
+ call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
+
+ call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
+
+ call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
+endfunc
diff --git a/src/nvim/version.c b/src/nvim/version.c
index aea5143bb8..ea2646d23e 100644
--- a/src/nvim/version.c
+++ b/src/nvim/version.c
@@ -759,7 +759,7 @@ static int included_patches[] = {
// 1688 NA
// 1687 NA
1686,
- // 1685,
+ 1685,
// 1684 NA
// 1683 NA
1682,