comparison runtime/doc/eval.txt @ 87:014ba200db86

updated for version 7.0035
author vimboss
date Sat, 08 Jan 2005 21:45:39 +0000
parents fc244e0a6e7e
children 9d4f762cc1d9
comparison
equal deleted inserted replaced
86:8173ec1e9f1f 87:014ba200db86
12 done, the features in this document are not available. See |+eval| and the 12 done, the features in this document are not available. See |+eval| and the
13 last chapter below. 13 last chapter below.
14 14
15 1. Variables |variables| 15 1. Variables |variables|
16 1.1 Variable types 16 1.1 Variable types
17 1.2 Function reference |Funcref| 17 1.2 Function references |Funcref|
18 1.3 List |List| 18 1.3 Lists |List|
19 1.4 More about variables |more-variables| 19 1.4 More about variables |more-variables|
20 2. Expression syntax |expression-syntax| 20 2. Expression syntax |expression-syntax|
21 3. Internal variable |internal-variables| 21 3. Internal variable |internal-variables|
22 4. Builtin Functions |functions| 22 4. Builtin Functions |functions|
23 5. Defining functions |user-functions| 23 5. Defining functions |user-functions|
78 *E706* 78 *E706*
79 You will get an error if you try to change the type of a variable. You need 79 You will get an error if you try to change the type of a variable. You need
80 to |:unlet| it first to avoid this error. String and Number are considered 80 to |:unlet| it first to avoid this error. String and Number are considered
81 equivalent though. > 81 equivalent though. >
82 :let l = "string" 82 :let l = "string"
83 :let l = 44 83 :let l = 44 " changes type from String to Number
84 :let l = [1, 2, 3] " error! 84 :let l = [1, 2, 3] " error!
85 85
86 86
87 1.2 Function reference ~ 87 1.2 Function references ~
88 *Funcref* *E695* *E703* 88 *Funcref* *E695* *E703*
89 A Funcref variable is obtained with the |function()| function. It can be used 89 A Funcref variable is obtained with the |function()| function. It can be used
90 in an expression to invoke the function it refers to by using it in the place 90 in an expression to invoke the function it refers to by using it in the place
91 of a function name, before the parenthesis around the arguments. Example: > 91 of a function name, before the parenthesis around the arguments. Example: >
92 92
95 < 95 <
96 *E704* *E705* *E707* 96 *E704* *E705* *E707*
97 A Funcref variable must start with a capital, "s:", "w:" or "b:". You cannot 97 A Funcref variable must start with a capital, "s:", "w:" or "b:". You cannot
98 have both a Funcref variable and a function with the same name. 98 have both a Funcref variable and a function with the same name.
99 99
100 Note that a Funcref cannot be used with |:call|, because its argument is not 100 Note that a Funcref cannot be used with the |:call| command, because its
101 an expression. 101 argument is not an expression.
102 102
103 The name of the referenced function can be obtained with |string()|. > 103 The name of the referenced function can be obtained with |string()|. >
104 :echo "The function is " . string(Myfunc) 104 :echo "The function is " . string(Myfunc)
105 105
106 You can use |call()| to invoke a Funcref and use a list variable for the 106 You can use |call()| to invoke a Funcref and use a list variable for the
107 arguments: > 107 arguments: >
108 :let r = call(Myfunc, mylist) 108 :let r = call(Myfunc, mylist)
109 109
110 110
111 1.3 List ~ 111 1.3 Lists ~
112 *List* *E686* 112 *List* *E686*
113 A List is an ordered sequence of items. An item can be of any type. Items 113 A List is an ordered sequence of items. An item can be of any type. Items
114 can be accessed by their index number. Items can be added and removed at any 114 can be accessed by their index number. Items can be added and removed at any
115 position in the sequence. 115 position in the sequence.
116 116
134 An item in the List can be accessed by putting the index in square brackets 134 An item in the List can be accessed by putting the index in square brackets
135 after the List. Indexes are zero-based, thus the first item has index zero. > 135 after the List. Indexes are zero-based, thus the first item has index zero. >
136 :let item = mylist[0] " get the first item: 1 136 :let item = mylist[0] " get the first item: 1
137 :let item = mylist[2] " get the third item: 3 137 :let item = mylist[2] " get the third item: 3
138 138
139 When the item is a list again this can be repeated: > 139 When the resulting item is a list this can be repeated: >
140 :let item = nestlist[0][1] " get the first list, second item: 12 140 :let item = nestlist[0][1] " get the first list, second item: 12
141 < 141 <
142 A negative index is counted from the end. Index -1 refers to the last item in 142 A negative index is counted from the end. Index -1 refers to the last item in
143 the List, -2 to the last but one item, etc. > 143 the List, -2 to the last but one item, etc. >
144 :let last = mylist[-1] " get the last item: "four" 144 :let last = mylist[-1] " get the last item: "four"
145 145
146 To avoid an error for an invalid index use the |get()| function. When an item 146 To avoid an error for an invalid index use the |get()| function. When an item
147 is not available it returns zero, unless you specify a default value: > 147 is not available it returns zero or the default value you specify: >
148 :echo get(mylist, idx) 148 :echo get(mylist, idx)
149 :echo get(mylist, idx, "NONE") 149 :echo get(mylist, idx, "NONE")
150 150
151 151
152 List concatenation ~ 152 List concatenation ~
183 :echo bb 183 :echo bb
184 [1, 2, 3, 4] 184 [1, 2, 3, 4]
185 185
186 Making a copy of a list is done with the |copy()| function. Using [:] also 186 Making a copy of a list is done with the |copy()| function. Using [:] also
187 works, as explained above. This creates a shallow copy of the list: Changing 187 works, as explained above. This creates a shallow copy of the list: Changing
188 a list in the list will also change the copied list: > 188 a list item in the list will also change the item in the copied list: >
189 :let aa = [[1, 'a'], 2, 3] 189 :let aa = [[1, 'a'], 2, 3]
190 :let bb = copy(aa) 190 :let bb = copy(aa)
191 :let aa = aa + [4] 191 :let aa = aa + [4]
192 :let aa[0][1] = 'aaa' 192 :let aa[0][1] = 'aaa'
193 :echo aa 193 :echo aa
194 [[1, aaa], 2, 3, 4] 194 [[1, aaa], 2, 3, 4]
195 :echo bb 195 :echo bb
196 [[1, aaa], 2, 3] 196 [[1, aaa], 2, 3]
197 197
198 To make a completely independent list use |deepcopy()|. This also copies the 198 To make a completely independent list use |deepcopy()|. This also makes a
199 values in the list, recursively. 199 copy of the values in the list, recursively.
200 200
201 The operator "is" can be used to check if two variables refer to the same 201 The operator "is" can be used to check if two variables refer to the same
202 list. "isnot" does the opposite. In contrast "==" compares if two lists have 202 list. "isnot" does the opposite. In contrast "==" compares if two lists have
203 the same value. 203 the same value. >
204 :let alist = [1, 2, 3]
205 :let blist = [1, 2, 3]
206 :echo alist is blist
207 0
208 :echo alist == blist
209 1
204 210
205 211
206 List unpack ~ 212 List unpack ~
207 213
208 To unpack the items in a list to individual variables, put the variables in 214 To unpack the items in a list to individual variables, put the variables in
223 empty list then. 229 empty list then.
224 230
225 231
226 List modification ~ 232 List modification ~
227 *list-modification* 233 *list-modification*
228 To change a specific item of a list use |:let|: > 234 To change a specific item of a list use |:let| this way: >
229 :let list[4] = "four" 235 :let list[4] = "four"
230 :let listlist[0][3] = item 236 :let listlist[0][3] = item
237
238 To change part of a list you can specify the first and last item to be
239 modified. The value must mach the range of replaced items: >
240 :let list[3:5] = [3, 4, 5]
231 241
232 Adding and removing items from a list is done with functions. Here are a few 242 Adding and removing items from a list is done with functions. Here are a few
233 examples: > 243 examples: >
234 :call insert(list, 'a') " prepend item 'a' 244 :call insert(list, 'a') " prepend item 'a'
235 :call insert(list, 'a', 3) " insert item 'a' before list[3] 245 :call insert(list, 'a', 3) " insert item 'a' before list[3]
237 :call add(list, [1, 2]) " append List as one new item 247 :call add(list, [1, 2]) " append List as one new item
238 :call extend(list, [1, 2]) " extend the list with two more items 248 :call extend(list, [1, 2]) " extend the list with two more items
239 :let i = remove(list, 3) " remove item 3 249 :let i = remove(list, 3) " remove item 3
240 :let l = remove(list, 3, -1) " remove items 3 to last item 250 :let l = remove(list, 3, -1) " remove items 3 to last item
241 251
252 Changing the oder of items in a list: >
253 :call sort(list) " sort a list alphabetically
254 :call reverse(list) " reverse the order of items
255
242 256
243 For loop ~ 257 For loop ~
244 258
245 The |:for| loop executes commands for each item in a list. Example: > 259 The |:for| loop executes commands for each item in a list. A variable is set
260 to each item in the list in sequence. Example: >
246 :for i in mylist 261 :for i in mylist
247 : call Doit(i) 262 : call Doit(i)
248 :endfor 263 :endfor
249 264
250 This works like: > 265 This works like: >
254 : :call Doit(i) 269 : :call Doit(i)
255 : let index = index + 1 270 : let index = index + 1
256 :endwhile 271 :endwhile
257 272
258 Note that all items in the list should be of the same type, otherwise this 273 Note that all items in the list should be of the same type, otherwise this
259 results in an error. To avoid this |:unlet| the variable at the end of the 274 results in an error |E706|. To avoid this |:unlet| the variable at the end of
260 loop. 275 the loop.
261 276
262 Just like the |:let| command, |:for| also accepts a list of variables. This 277 Just like the |:let| command, |:for| also accepts a list of variables. This
263 requires the argument to be a list of lists. > 278 requires the argument to be a list of lists. >
264 :for [lnum, col] in [[1, 3], [2, 8], [3, 0]] 279 :for [lnum, col] in [[1, 3], [2, 8], [3, 0]]
265 : call Doit(lnum, col) 280 : call Doit(lnum, col)
278 293
279 294
280 List functions ~ 295 List functions ~
281 296
282 Functions that are useful with a List: > 297 Functions that are useful with a List: >
283 :let r = call(funcname, list) " invoke a function with argument list 298 :let r = call(funcname, list) " call a function with an argument list
284 :if empty(list) " check if list is empty 299 :if empty(list) " check if list is empty
285 :let l = len(list) " number of items in a list 300 :let l = len(list) " number of items in a list
286 :let xs = count(list, 'x') " count occurrences of a value 301 :let big = max(list) " maximum value in a list
287 :let i = index(list, 'x') " find a value 302 :let small = min(list) " minumum value in a list
303 :let xs = count(list, 'x') " count nr of times 'x' appears in list
304 :let i = index(list, 'x') " index of first 'x' in list
288 :let lines = getline(1, 10) " get ten text lines from buffer 305 :let lines = getline(1, 10) " get ten text lines from buffer
289 :call append('$', lines) " append text lines in buffer 306 :call append('$', lines) " append text lines in buffer
290 :let list = str2list("a b c") " create list from items in a string 307 :let list = str2list("a b c") " create list from items in a string
291 :let s = string() " String representation of a list 308 :let s = string() " String representation of a list
292 309
735 An internal variable name can be made up of letters, digits and '_'. But it 752 An internal variable name can be made up of letters, digits and '_'. But it
736 cannot start with a digit. It's also possible to use curly braces, see 753 cannot start with a digit. It's also possible to use curly braces, see
737 |curly-braces-names|. 754 |curly-braces-names|.
738 755
739 An internal variable is created with the ":let" command |:let|. 756 An internal variable is created with the ":let" command |:let|.
740 An internal variable is destroyed with the ":unlet" command |:unlet|. 757 An internal variable is explicitly destroyed with the ":unlet" command
741 Using a name that isn't an internal variable, or an internal variable that has 758 |:unlet|.
742 been destroyed, results in an error. 759 Using a name that is not an internal variable or refers to a variable that has
760 been destroyed results in an error.
743 761
744 There are several name spaces for variables. Which one is to be used is 762 There are several name spaces for variables. Which one is to be used is
745 specified by what is prepended: 763 specified by what is prepended:
746 764
747 (nothing) In a function: local to a function; otherwise: global 765 (nothing) In a function: local to a function; otherwise: global
1206 Number position where {pat} matches in {expr} 1224 Number position where {pat} matches in {expr}
1207 matchend( {expr}, {pat}[, {start}[, {count}]]) 1225 matchend( {expr}, {pat}[, {start}[, {count}]])
1208 Number position where {pat} ends in {expr} 1226 Number position where {pat} ends in {expr}
1209 matchstr( {expr}, {pat}[, {start}[, {count}]]) 1227 matchstr( {expr}, {pat}[, {start}[, {count}]])
1210 String {count}'th match of {pat} in {expr} 1228 String {count}'th match of {pat} in {expr}
1229 max({list}) Number maximum value of items in {list}
1230 min({list}) Number minumum value of items in {list}
1211 mode() String current editing mode 1231 mode() String current editing mode
1212 nextnonblank( {lnum}) Number line nr of non-blank line >= {lnum} 1232 nextnonblank( {lnum}) Number line nr of non-blank line >= {lnum}
1213 nr2char( {expr}) String single char with ASCII value {expr} 1233 nr2char( {expr}) String single char with ASCII value {expr}
1214 prevnonblank( {lnum}) Number line nr of non-blank line <= {lnum} 1234 prevnonblank( {lnum}) Number line nr of non-blank line <= {lnum}
1215 remote_expr( {server}, {string} [, {idvar}]) 1235 remote_expr( {server}, {string} [, {idvar}])
2344 When {ic} is given and it is non-zero, ignore case. Otherwise 2364 When {ic} is given and it is non-zero, ignore case. Otherwise
2345 case must match. 2365 case must match.
2346 -1 is returned when {expr} is not found in {list}. 2366 -1 is returned when {expr} is not found in {list}.
2347 Example: > 2367 Example: >
2348 :let idx = index(words, "the") 2368 :let idx = index(words, "the")
2369 :if index(numbers, 123) >= 0
2349 2370
2350 2371
2351 input({prompt} [, {text}]) *input()* 2372 input({prompt} [, {text}]) *input()*
2352 The result is a String, which is whatever the user typed on 2373 The result is a String, which is whatever the user typed on
2353 the command-line. The parameter is either a prompt string, or 2374 the command-line. The parameter is either a prompt string, or
2644 :echo matchstr("testing", "ing", 2) 2665 :echo matchstr("testing", "ing", 2)
2645 < results in "ing". > 2666 < results in "ing". >
2646 :echo matchstr("testing", "ing", 5) 2667 :echo matchstr("testing", "ing", 5)
2647 < result is "". 2668 < result is "".
2648 2669
2670 *max()*
2671 max({list}) Return the maximum value of all items in {list}.
2672 If {list} is not a list or one of the items in {list} cannot
2673 be used as a Number this results in an error.
2674 An empty List results in zero.
2675
2676 *min()*
2677 min({list}) Return the minumum value of all items in {list}.
2678 If {list} is not a list or one of the items in {list} cannot
2679 be used as a Number this results in an error.
2680 An empty List results in zero.
2681
2649 *mode()* 2682 *mode()*
2650 mode() Return a string that indicates the current mode: 2683 mode() Return a string that indicates the current mode:
2651 n Normal 2684 n Normal
2652 v Visual by character 2685 v Visual by character
2653 V Visual by line 2686 V Visual by line
2787 repeat({expr}, {count}) *repeat()* 2820 repeat({expr}, {count}) *repeat()*
2788 Repeat {expr} {count} times and return the concatenated 2821 Repeat {expr} {count} times and return the concatenated
2789 result. Example: > 2822 result. Example: >
2790 :let seperator = repeat('-', 80) 2823 :let seperator = repeat('-', 80)
2791 < When {count} is zero or negative the result is empty. 2824 < When {count} is zero or negative the result is empty.
2792 When {expr} is a list the result is {expr} concatenated 2825 When {expr} is a List the result is {expr} concatenated
2793 {count} times. Example: > 2826 {count} times. Example: >
2794 :let longlist = repeat(['a', 'b'], 3) 2827 :let longlist = repeat(['a', 'b'], 3)
2795 < Results in ['a', 'b', 'a', 'b', 'a', 'b']. 2828 < Results in ['a', 'b', 'a', 'b', 'a', 'b'].
2796 2829
2797 2830
3295 echo tr("hello there", "ht", "HT") 3328 echo tr("hello there", "ht", "HT")
3296 < returns "Hello THere" > 3329 < returns "Hello THere" >
3297 echo tr("<blob>", "<>", "{}") 3330 echo tr("<blob>", "<>", "{}")
3298 < returns "{blob}" 3331 < returns "{blob}"
3299 3332
3300 type({expr}) *type()* 3333 *type()*
3301 The result is a Number: 3334 type({expr}) The result is a Number, depending on the type of {expr}:
3302 0 if {expr} has the type Number 3335 Number: 0
3303 1 if {expr} has the type String 3336 String: 1
3337 Funcref: 2
3338 List: 3
3339 To avoid the magic numbers it can be used this way: >
3340 :if type(myvar) == type(0)
3341 :if type(myvar) == type("")
3342 :if type(myvar) == type(function("tr"))
3343 :if type(myvar) == type([])
3304 3344
3305 virtcol({expr}) *virtcol()* 3345 virtcol({expr}) *virtcol()*
3306 The result is a Number, which is the screen column of the file 3346 The result is a Number, which is the screen column of the file
3307 position given with {expr}. That is, the last screen position 3347 position given with {expr}. That is, the last screen position
3308 occupied by the character at that position, when the screen 3348 occupied by the character at that position, when the screen
3839 {expr1}. {var-name} must refer to a list and {idx} 3879 {expr1}. {var-name} must refer to a list and {idx}
3840 must be a valid index in that list. For nested list 3880 must be a valid index in that list. For nested list
3841 the index can be repeated. 3881 the index can be repeated.
3842 This cannot be used to add an item to a list. 3882 This cannot be used to add an item to a list.
3843 3883
3884 :let {var-name}[{idx1}:{idx2}] = {expr1} *E708* *E709* *E710* *E711*
3885 Set a sequence of items in a List to the result of the
3886 expression {expr1}, which must be a list with the
3887 correct number of items.
3888 {idx1} can be omitted, zero is used instead.
3889 {idx2} can be omitted, meaning the end of the list.
3890 When the selected range of items is partly past the
3891 end of the list, items will be added.
3892
3844 :let ${env-name} = {expr1} *:let-environment* *:let-$* 3893 :let ${env-name} = {expr1} *:let-environment* *:let-$*
3845 Set environment variable {env-name} to the result of 3894 Set environment variable {env-name} to the result of
3846 the expression {expr1}. The type is always String. 3895 the expression {expr1}. The type is always String.
3847 3896
3848 :let @{reg-name} = {expr1} *:let-register* *:let-@* 3897 :let @{reg-name} = {expr1} *:let-register* *:let-@*
3983 it will not be found. Thus the following example 4032 it will not be found. Thus the following example
3984 works (an inefficient way to make a list empty): > 4033 works (an inefficient way to make a list empty): >
3985 :for item in mylist 4034 :for item in mylist
3986 :call remove(mylist, 0) 4035 :call remove(mylist, 0)
3987 :endfor 4036 :endfor
3988 < Note that the type of each list item should be 4037 < Note that reordering the list (e.g., with sort() or
4038 reverse()) may have unexpected effects.
4039 Note that the type of each list item should be
3989 identical to avoid errors for the type of {var} 4040 identical to avoid errors for the type of {var}
3990 changing. Unlet the variable at the end of the loop 4041 changing. Unlet the variable at the end of the loop
3991 to allow multiple item types. 4042 to allow multiple item types.
3992 4043
3993 :for {var} in {string} 4044 :for {var} in {string}