aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/eval.lua
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2023-08-17 13:38:41 +0800
committerGitHub <noreply@github.com>2023-08-17 13:38:41 +0800
commit7f51829cf75e0d3ca546cab0e70a4c089eac40ec (patch)
treef907b7af2bbe5ec6bb19823d0a182f974062a406 /src/nvim/eval.lua
parent8861f2af72481b1759a6935afa6dce2aae512359 (diff)
parentacac1e19d364c90ded6ded8c874f886d9ebbc393 (diff)
downloadrneovim-7f51829cf75e0d3ca546cab0e70a4c089eac40ec.tar.gz
rneovim-7f51829cf75e0d3ca546cab0e70a4c089eac40ec.tar.bz2
rneovim-7f51829cf75e0d3ca546cab0e70a4c089eac40ec.zip
Merge pull request #24748 from zeertzjq/vim-8.2.3818
vim-patch:8.2.{3818,3822,3908,3982}: filter()/map() on a string
Diffstat (limited to 'src/nvim/eval.lua')
-rw-r--r--src/nvim/eval.lua47
1 files changed, 27 insertions, 20 deletions
diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua
index 97b10a46c0..4dbd63d131 100644
--- a/src/nvim/eval.lua
+++ b/src/nvim/eval.lua
@@ -2539,10 +2539,11 @@ M.funcs = {
args = 2,
base = 1,
desc = [=[
- {expr1} must be a |List|, |Blob|, or a |Dictionary|.
+ {expr1} must be a |List|, |String|, |Blob| or |Dictionary|.
For each item in {expr1} evaluate {expr2} and when the result
- is zero remove the item from the |List| or |Dictionary|. For a
- |Blob| each byte is removed.
+ is zero or false remove the item from the |List| or
+ |Dictionary|. Similarly for each byte in a |Blob| and each
+ character in a |String|.
{expr2} must be a |string| or |Funcref|.
@@ -2578,14 +2579,16 @@ M.funcs = {
<If you do not use "val" you can leave it out: >vim
call filter(myList, {idx -> idx % 2 == 1})
<
- The operation is done in-place. If you want a |List| or
- |Dictionary| to remain unmodified make a copy first: >vim
+ For a |List| and a |Dictionary| the operation is done
+ in-place. If you want it to remain unmodified make a copy
+ first: >vim
let l = filter(copy(mylist), 'v:val =~ "KEEP"')
- <Returns {expr1}, the |List|, |Blob| or |Dictionary| that was
- filtered. When an error is encountered while evaluating
- {expr2} no further items in {expr1} are processed. When
- {expr2} is a Funcref errors inside a function are ignored,
+ <Returns {expr1}, the |List| or |Dictionary| that was filtered,
+ or a new |Blob| or |String|.
+ When an error is encountered while evaluating {expr2} no
+ further items in {expr1} are processed.
+ When {expr2} is a Funcref errors inside a function are ignored,
unless it was defined with the "abort" flag.
]=],
@@ -6050,15 +6053,18 @@ M.funcs = {
args = 2,
base = 1,
desc = [=[
- {expr1} must be a |List|, |Blob| or |Dictionary|.
- Replace each item in {expr1} with the result of evaluating
- {expr2}. For a |Blob| each byte is replaced.
+ {expr1} must be a |List|, |String|, |Blob| or |Dictionary|.
+ When {expr1} is a |List|| or |Dictionary|, replace each
+ item in {expr1} with the result of evaluating {expr2}.
+ For a |Blob| each byte is replaced.
+ For a |String|, each character, including composing
+ characters, is replaced.
If the item type changes you may want to use |mapnew()| to
create a new List or Dictionary.
- {expr2} must be a |string| or |Funcref|.
+ {expr2} must be a |String| or |Funcref|.
- If {expr2} is a |string|, inside {expr2} |v:val| has the value
+ If {expr2} is a |String|, inside {expr2} |v:val| has the value
of the current item. For a |Dictionary| |v:key| has the key
of the current item and for a |List| |v:key| has the index of
the current item. For a |Blob| |v:key| has the index of the
@@ -6088,14 +6094,15 @@ M.funcs = {
<If you do not use "key" you can use a short name: >vim
call map(myDict, {_, val -> 'item: ' .. val})
<
- The operation is done in-place. If you want a |List| or
- |Dictionary| to remain unmodified make a copy first: >vim
+ The operation is done in-place for a |List| and |Dictionary|.
+ If you want it to remain unmodified make a copy first: >vim
let tlist = map(copy(mylist), ' v:val .. "\t"')
- <Returns {expr1}, the |List|, |Blob| or |Dictionary| that was
- filtered. When an error is encountered while evaluating
- {expr2} no further items in {expr1} are processed. When
- {expr2} is a Funcref errors inside a function are ignored,
+ <Returns {expr1}, the |List| or |Dictionary| that was filtered,
+ or a new |Blob| or |String|.
+ When an error is encountered while evaluating {expr2} no
+ further items in {expr1} are processed.
+ When {expr2} is a Funcref errors inside a function are ignored,
unless it was defined with the "abort" flag.
]=],