Mercurial > vim
comparison runtime/doc/eval.txt @ 17620:072efa9ca875 v8.1.1807
patch 8.1.1807: more functions can be used as a method
commit https://github.com/vim/vim/commit/25e42231d3ee27feec2568fa4be2aa2bfba82ae5
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Aug 4 15:04:10 2019 +0200
patch 8.1.1807: more functions can be used as a method
Problem: More functions can be used as a method.
Solution: Add append(), appendbufline(), assert_equal(), etc.
Also add the :eval command.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 04 Aug 2019 15:15:06 +0200 |
parents | e259d11e2900 |
children | 4c7097a980a5 |
comparison
equal
deleted
inserted
replaced
17619:f9976baad617 | 17620:072efa9ca875 |
---|---|
1 *eval.txt* For Vim version 8.1. Last change: 2019 Jul 30 | 1 *eval.txt* For Vim version 8.1. Last change: 2019 Aug 04 |
2 | 2 |
3 | 3 |
4 VIM REFERENCE MANUAL by Bram Moolenaar | 4 VIM REFERENCE MANUAL by Bram Moolenaar |
5 | 5 |
6 | 6 |
838 |expr8| expr9 | 838 |expr8| expr9 |
839 expr8[expr1] byte of a String or item of a |List| | 839 expr8[expr1] byte of a String or item of a |List| |
840 expr8[expr1 : expr1] substring of a String or sublist of a |List| | 840 expr8[expr1 : expr1] substring of a String or sublist of a |List| |
841 expr8.name entry in a |Dictionary| | 841 expr8.name entry in a |Dictionary| |
842 expr8(expr1, ...) function call with |Funcref| variable | 842 expr8(expr1, ...) function call with |Funcref| variable |
843 expr8->name(expr1, ...) |method| call | |
843 | 844 |
844 |expr9| number number constant | 845 |expr9| number number constant |
845 "string" string constant, backslash is special | 846 "string" string constant, backslash is special |
846 'string' string constant, ' is doubled | 847 'string' string constant, ' is doubled |
847 [expr1, ...] |List| | 848 [expr1, ...] |List| |
848 {expr1: expr1, ...} |Dictionary| | 849 {expr1: expr1, ...} |Dictionary| |
850 #{key: expr1, ...} |Dictionary| | |
849 &option option value | 851 &option option value |
850 (expr1) nested expression | 852 (expr1) nested expression |
851 variable internal variable | 853 variable internal variable |
852 va{ria}ble internal variable with curly braces | 854 va{ria}ble internal variable with curly braces |
853 $VAR environment variable | 855 $VAR environment variable |
1109 | 1111 |
1110 expr8 *expr8* | 1112 expr8 *expr8* |
1111 ----- | 1113 ----- |
1112 This expression is either |expr9| or a sequence of the alternatives below, | 1114 This expression is either |expr9| or a sequence of the alternatives below, |
1113 in any order. E.g., these are all possible: | 1115 in any order. E.g., these are all possible: |
1114 expr9[expr1].name | 1116 expr8[expr1].name |
1115 expr9.name[expr1] | 1117 expr8.name[expr1] |
1116 expr9(expr1, ...)[expr1].name | 1118 expr8(expr1, ...)[expr1].name |
1117 expr9->(expr1, ...)[expr1] | 1119 expr8->(expr1, ...)[expr1] |
1118 Evaluation is always from left to right. | 1120 Evaluation is always from left to right. |
1119 | 1121 |
1120 | 1122 |
1121 expr8[expr1] item of String or |List| *expr-[]* *E111* | 1123 expr8[expr1] item of String or |List| *expr-[]* *E111* |
1122 *E909* *subscript* | 1124 *E909* *subscript* |
1215 When expr8 is a |Funcref| type variable, invoke the function it refers to. | 1217 When expr8 is a |Funcref| type variable, invoke the function it refers to. |
1216 | 1218 |
1217 | 1219 |
1218 expr8->name([args]) method call *method* | 1220 expr8->name([args]) method call *method* |
1219 | 1221 |
1220 For global methods this is the same as: > | 1222 For methods that are also available as global functions this is the same as: > |
1221 name(expr8 [, args]) | 1223 name(expr8 [, args]) |
1222 There can also be methods specifically for the type of "expr8". | 1224 There can also be methods specifically for the type of "expr8". |
1225 | |
1226 "->name(" must not contain white space. There can be white space before "->" | |
1227 and after the "(". | |
1228 | |
1229 This allows for chaining, using the type that the method returns: > | |
1230 mylist->filter(filterexpr)->map(mapexpr)->sort()->join() | |
1231 < | |
1223 | 1232 |
1224 *expr9* | 1233 *expr9* |
1225 number | 1234 number |
1226 ------ | 1235 ------ |
1227 number number constant *expr-number* | 1236 number number constant *expr-number* |
2904 Returns 1 for failure ({lnum} out of range or out of memory), | 2913 Returns 1 for failure ({lnum} out of range or out of memory), |
2905 0 for success. Example: > | 2914 0 for success. Example: > |
2906 :let failed = append(line('$'), "# THE END") | 2915 :let failed = append(line('$'), "# THE END") |
2907 :let failed = append(0, ["Chapter 1", "the beginning"]) | 2916 :let failed = append(0, ["Chapter 1", "the beginning"]) |
2908 | 2917 |
2918 < Can also be used as a |method| after a List: > | |
2919 mylist->append(lnum) | |
2920 | |
2921 | |
2909 appendbufline({expr}, {lnum}, {text}) *appendbufline()* | 2922 appendbufline({expr}, {lnum}, {text}) *appendbufline()* |
2910 Like |append()| but append the text in buffer {expr}. | 2923 Like |append()| but append the text in buffer {expr}. |
2911 | 2924 |
2912 For the use of {expr}, see |bufname()|. | 2925 For the use of {expr}, see |bufname()|. |
2913 | 2926 |
2919 | 2932 |
2920 If {expr} is not a valid buffer or {lnum} is not valid, an | 2933 If {expr} is not a valid buffer or {lnum} is not valid, an |
2921 error message is given. Example: > | 2934 error message is given. Example: > |
2922 :let failed = appendbufline(13, 0, "# THE START") | 2935 :let failed = appendbufline(13, 0, "# THE START") |
2923 < | 2936 < |
2924 *argc()* | 2937 Can also be used as a |method| after a List: > |
2925 argc([{winid}]) | 2938 mylist->appendbufline(buf, lnum) |
2939 | |
2940 | |
2941 argc([{winid}]) *argc()* | |
2926 The result is the number of files in the argument list. See | 2942 The result is the number of files in the argument list. See |
2927 |arglist|. | 2943 |arglist|. |
2928 If {winid} is not supplied, the argument list of the current | 2944 If {winid} is not supplied, the argument list of the current |
2929 window is used. | 2945 window is used. |
2930 If {winid} is -1, the global argument list is used. | 2946 If {winid} is -1, the global argument list is used. |
3759 eval({string}) Evaluate {string} and return the result. Especially useful to | 3775 eval({string}) Evaluate {string} and return the result. Especially useful to |
3760 turn the result of |string()| back into the original value. | 3776 turn the result of |string()| back into the original value. |
3761 This works for Numbers, Floats, Strings, Blobs and composites | 3777 This works for Numbers, Floats, Strings, Blobs and composites |
3762 of them. Also works for |Funcref|s that refer to existing | 3778 of them. Also works for |Funcref|s that refer to existing |
3763 functions. | 3779 functions. |
3780 | |
3781 Can also be used as a |method|: > | |
3782 argv->join()->eval() | |
3764 | 3783 |
3765 eventhandler() *eventhandler()* | 3784 eventhandler() *eventhandler()* |
3766 Returns 1 when inside an event handler. That is that Vim got | 3785 Returns 1 when inside an event handler. That is that Vim got |
3767 interrupted while waiting for the user to type a character, | 3786 interrupted while waiting for the user to type a character, |
3768 e.g., when dropping a file on Vim. This means interactive | 3787 e.g., when dropping a file on Vim. This means interactive |
4113 name {file} exists, and can be read. If {file} doesn't exist, | 4132 name {file} exists, and can be read. If {file} doesn't exist, |
4114 or is a directory, the result is |FALSE|. {file} is any | 4133 or is a directory, the result is |FALSE|. {file} is any |
4115 expression, which is used as a String. | 4134 expression, which is used as a String. |
4116 If you don't care about the file being readable you can use | 4135 If you don't care about the file being readable you can use |
4117 |glob()|. | 4136 |glob()|. |
4118 *file_readable()* | 4137 {file} is used as-is, you may want to expand wildcards first: > |
4138 echo filereadable('~/.vimrc') | |
4139 0 | |
4140 echo filereadable(expand('~/.vimrc')) | |
4141 1 | |
4142 < *file_readable()* | |
4119 Obsolete name: file_readable(). | 4143 Obsolete name: file_readable(). |
4120 | 4144 |
4121 | 4145 |
4122 filewritable({file}) *filewritable()* | 4146 filewritable({file}) *filewritable()* |
4123 The result is a Number, which is 1 when a file with the | 4147 The result is a Number, which is 1 when a file with the |
7996 checksum of {string}. | 8020 checksum of {string}. |
7997 {only available when compiled with the |+cryptv| feature} | 8021 {only available when compiled with the |+cryptv| feature} |
7998 | 8022 |
7999 shellescape({string} [, {special}]) *shellescape()* | 8023 shellescape({string} [, {special}]) *shellescape()* |
8000 Escape {string} for use as a shell command argument. | 8024 Escape {string} for use as a shell command argument. |
8001 On MS-Windows and MS-DOS, when 'shellslash' is not set, it | 8025 On MS-Windows, when 'shellslash' is not set, it will enclose |
8002 will enclose {string} in double quotes and double all double | 8026 {string} in double quotes and double all double quotes within |
8003 quotes within {string}. | 8027 {string}. |
8004 Otherwise it will enclose {string} in single quotes and | 8028 Otherwise it will enclose {string} in single quotes and |
8005 replace all "'" with "'\''". | 8029 replace all "'" with "'\''". |
8006 | 8030 |
8007 When the {special} argument is present and it's a non-zero | 8031 When the {special} argument is present and it's a non-zero |
8008 Number or a non-empty String (|non-zero-arg|), then special | 8032 Number or a non-empty String (|non-zero-arg|), then special |
10045 | 10069 |
10046 *E132* | 10070 *E132* |
10047 The recursiveness of user functions is restricted with the |'maxfuncdepth'| | 10071 The recursiveness of user functions is restricted with the |'maxfuncdepth'| |
10048 option. | 10072 option. |
10049 | 10073 |
10074 It is also possible to use `:eval`. It does not support a range, but does | |
10075 allow for method chaining, e.g.: > | |
10076 eval GetList()->Filter()->append('$') | |
10077 | |
10050 | 10078 |
10051 AUTOMATICALLY LOADING FUNCTIONS ~ | 10079 AUTOMATICALLY LOADING FUNCTIONS ~ |
10052 *autoload-functions* | 10080 *autoload-functions* |
10053 When using many or large functions, it's possible to automatically define them | 10081 When using many or large functions, it's possible to automatically define them |
10054 only when they are used. There are two methods: with an autocommand and with | 10082 only when they are used. There are two methods: with an autocommand and with |
10495 | 10523 |
10496 | 10524 |
10497 :unlo[ckvar][!] [depth] {name} ... *:unlockvar* *:unlo* | 10525 :unlo[ckvar][!] [depth] {name} ... *:unlockvar* *:unlo* |
10498 Unlock the internal variable {name}. Does the | 10526 Unlock the internal variable {name}. Does the |
10499 opposite of |:lockvar|. | 10527 opposite of |:lockvar|. |
10528 | |
10529 *:eval* | |
10530 :eval {expr} Evaluate {expr} and discard the result. Example: > | |
10531 :eval Getlist()->Filter()->append('$') | |
10532 | |
10533 < The expression is supposed to have a side effect, | |
10534 since the resulting value is not used. In the example | |
10535 the `append()` call appends the List with text to the | |
10536 buffer. This is similar to `:call` but works with any | |
10537 expression. | |
10538 | |
10539 The command can be shortened to `:ev` or `:eva`, but | |
10540 these are hard to recognize and therefore not to be | |
10541 used. | |
10500 | 10542 |
10501 | 10543 |
10502 :if {expr1} *:if* *:end* *:endif* *:en* *E171* *E579* *E580* | 10544 :if {expr1} *:if* *:end* *:endif* *:en* *E171* *E579* *E580* |
10503 :en[dif] Execute the commands until the next matching ":else" | 10545 :en[dif] Execute the commands until the next matching ":else" |
10504 or ":endif" if {expr1} evaluates to non-zero. | 10546 or ":endif" if {expr1} evaluates to non-zero. |