comparison runtime/doc/eval.txt @ 17916:2e53305f2239 v8.1.1954

patch 8.1.1954: more functions can be used as a method Commit: https://github.com/vim/vim/commit/02b31110d31e995326080807716e79e38fe501df Author: Bram Moolenaar <Bram@vim.org> Date: Sat Aug 31 22:16:38 2019 +0200 patch 8.1.1954: more functions can be used as a method Problem: More functions can be used as a method. Solution: Allow more functions to be used as a method.
author Bram Moolenaar <Bram@vim.org>
date Sat, 31 Aug 2019 22:30:03 +0200
parents af3d441845cd
children 0cdb6ac20748
comparison
equal deleted inserted replaced
17915:50ad0353c88a 17916:2e53305f2239
1226 This allows for chaining, passing the value that one method returns to the 1226 This allows for chaining, passing the value that one method returns to the
1227 next method: > 1227 next method: >
1228 mylist->filter(filterexpr)->map(mapexpr)->sort()->join() 1228 mylist->filter(filterexpr)->map(mapexpr)->sort()->join()
1229 < 1229 <
1230 Example of using a lambda: > 1230 Example of using a lambda: >
1231 GetPercentage->{x -> x * 100}()->printf('%d%%') 1231 GetPercentage()->{x -> x * 100}()->printf('%d%%')
1232 < 1232 <
1233 When using -> the |expr7| operators will be applied first, thus: > 1233 When using -> the |expr7| operators will be applied first, thus: >
1234 -1.234->string() 1234 -1.234->string()
1235 Is equivalent to: > 1235 Is equivalent to: >
1236 (-1.234)->string() 1236 (-1.234)->string()
6204 - Object key names do not have to be in quotes. 6204 - Object key names do not have to be in quotes.
6205 - Strings can be in single quotes. 6205 - Strings can be in single quotes.
6206 - Empty items in an array (between two commas) are allowed and 6206 - Empty items in an array (between two commas) are allowed and
6207 result in v:none items. 6207 result in v:none items.
6208 6208
6209 Can also be used as a |method|: >
6210 ReadObject()->js_decode()
6211
6209 js_encode({expr}) *js_encode()* 6212 js_encode({expr}) *js_encode()*
6210 This is similar to |json_encode()| with these differences: 6213 This is similar to |json_encode()| with these differences:
6211 - Object key names are not in quotes. 6214 - Object key names are not in quotes.
6212 - v:none items in an array result in an empty item between 6215 - v:none items in an array result in an empty item between
6213 commas. 6216 commas.
6218 While json_encode() would produce: 6221 While json_encode() would produce:
6219 [1,null,{"one":1},null] ~ 6222 [1,null,{"one":1},null] ~
6220 This encoding is valid for JavaScript. It is more efficient 6223 This encoding is valid for JavaScript. It is more efficient
6221 than JSON, especially when using an array with optional items. 6224 than JSON, especially when using an array with optional items.
6222 6225
6226 Can also be used as a |method|: >
6227 GetObject()->js_encode()
6223 6228
6224 json_decode({string}) *json_decode()* 6229 json_decode({string}) *json_decode()*
6225 This parses a JSON formatted string and returns the equivalent 6230 This parses a JSON formatted string and returns the equivalent
6226 in Vim values. See |json_encode()| for the relation between 6231 in Vim values. See |json_encode()| for the relation between
6227 JSON and Vim values. 6232 JSON and Vim values.
6252 *E938* 6257 *E938*
6253 A duplicate key in an object, valid in rfc7159, is not 6258 A duplicate key in an object, valid in rfc7159, is not
6254 accepted by json_decode() as the result must be a valid Vim 6259 accepted by json_decode() as the result must be a valid Vim
6255 type, e.g. this fails: {"a":"b", "a":"c"} 6260 type, e.g. this fails: {"a":"b", "a":"c"}
6256 6261
6262 Can also be used as a |method|: >
6263 ReadObject()->json_decode()
6257 6264
6258 json_encode({expr}) *json_encode()* 6265 json_encode({expr}) *json_encode()*
6259 Encode {expr} as JSON and return this as a string. 6266 Encode {expr} as JSON and return this as a string.
6260 The encoding is specified in: 6267 The encoding is specified in:
6261 https://tools.ietf.org/html/rfc7159.html 6268 https://tools.ietf.org/html/rfc7159.html
6278 v:null "null" 6285 v:null "null"
6279 Note that NaN and Infinity are passed on as values. This is 6286 Note that NaN and Infinity are passed on as values. This is
6280 missing in the JSON standard, but several implementations do 6287 missing in the JSON standard, but several implementations do
6281 allow it. If not then you will get an error. 6288 allow it. If not then you will get an error.
6282 6289
6290 Can also be used as a |method|: >
6291 GetObject()->json_encode()
6292
6283 keys({dict}) *keys()* 6293 keys({dict}) *keys()*
6284 Return a |List| with all the keys of {dict}. The |List| is in 6294 Return a |List| with all the keys of {dict}. The |List| is in
6285 arbitrary order. Also see |items()| and |values()|. 6295 arbitrary order. Also see |items()| and |values()|.
6286 6296
6287 Can also be used as a |method|: > 6297 Can also be used as a |method|: >
6344 object code must be compiled as position-independent ('PIC'). 6354 object code must be compiled as position-independent ('PIC').
6345 {only in Win32 and some Unix versions, when the |+libcall| 6355 {only in Win32 and some Unix versions, when the |+libcall|
6346 feature is present} 6356 feature is present}
6347 Examples: > 6357 Examples: >
6348 :echo libcall("libc.so", "getenv", "HOME") 6358 :echo libcall("libc.so", "getenv", "HOME")
6359
6360 < Can also be used as a |method|, where the base is passed as
6361 the argument to the called function: >
6362 GetValue()->libcall("libc.so", "getenv")
6349 < 6363 <
6350 *libcallnr()* 6364 *libcallnr()*
6351 libcallnr({libname}, {funcname}, {argument}) 6365 libcallnr({libname}, {funcname}, {argument})
6352 Just like |libcall()|, but used for a function that returns an 6366 Just like |libcall()|, but used for a function that returns an
6353 int instead of a string. 6367 int instead of a string.
6355 feature is present} 6369 feature is present}
6356 Examples: > 6370 Examples: >
6357 :echo libcallnr("/usr/lib/libc.so", "getpid", "") 6371 :echo libcallnr("/usr/lib/libc.so", "getpid", "")
6358 :call libcallnr("libc.so", "printf", "Hello World!\n") 6372 :call libcallnr("libc.so", "printf", "Hello World!\n")
6359 :call libcallnr("libc.so", "sleep", 10) 6373 :call libcallnr("libc.so", "sleep", 10)
6374 <
6375 Can also be used as a |method|, where the base is passed as
6376 the argument to the called function: >
6377 GetValue()->libcallnr("libc.so", "printf")
6360 < 6378 <
6361 *line()* 6379 *line()*
6362 line({expr}) The result is a Number, which is the line number of the file 6380 line({expr}) The result is a Number, which is the line number of the file
6363 position given with {expr}. The accepted positions are: 6381 position given with {expr}. The accepted positions are:
6364 . the cursor position 6382 . the cursor position
6383 line("'" . marker) line number of mark marker 6401 line("'" . marker) line number of mark marker
6384 < 6402 <
6385 To jump to the last known position when opening a file see 6403 To jump to the last known position when opening a file see
6386 |last-position-jump|. 6404 |last-position-jump|.
6387 6405
6406 Can also be used as a |method|: >
6407 GetValue()->line()
6408
6388 line2byte({lnum}) *line2byte()* 6409 line2byte({lnum}) *line2byte()*
6389 Return the byte count from the start of the buffer for line 6410 Return the byte count from the start of the buffer for line
6390 {lnum}. This includes the end-of-line character, depending on 6411 {lnum}. This includes the end-of-line character, depending on
6391 the 'fileformat' option for the current buffer. The first 6412 the 'fileformat' option for the current buffer. The first
6392 line returns 1. 'encoding' matters, 'fileencoding' is ignored. 6413 line returns 1. 'encoding' matters, 'fileencoding' is ignored.
6397 it is the file size plus one. 6418 it is the file size plus one.
6398 When {lnum} is invalid, or the |+byte_offset| feature has been 6419 When {lnum} is invalid, or the |+byte_offset| feature has been
6399 disabled at compile time, -1 is returned. 6420 disabled at compile time, -1 is returned.
6400 Also see |byte2line()|, |go| and |:goto|. 6421 Also see |byte2line()|, |go| and |:goto|.
6401 6422
6423 Can also be used as a |method|: >
6424 GetLnum()->line2byte()
6425
6402 lispindent({lnum}) *lispindent()* 6426 lispindent({lnum}) *lispindent()*
6403 Get the amount of indent for line {lnum} according the lisp 6427 Get the amount of indent for line {lnum} according the lisp
6404 indenting rules, as with 'lisp'. 6428 indenting rules, as with 'lisp'.
6405 The indent is counted in spaces, the value of 'tabstop' is 6429 The indent is counted in spaces, the value of 'tabstop' is
6406 relevant. {lnum} is used just like in |getline()|. 6430 relevant. {lnum} is used just like in |getline()|.
6407 When {lnum} is invalid or Vim was not compiled the 6431 When {lnum} is invalid or Vim was not compiled the
6408 |+lispindent| feature, -1 is returned. 6432 |+lispindent| feature, -1 is returned.
6433
6434 Can also be used as a |method|: >
6435 GetLnum()->lispindent()
6409 6436
6410 list2str({list} [, {utf8}]) *list2str()* 6437 list2str({list} [, {utf8}]) *list2str()*
6411 Convert each number in {list} to a character string can 6438 Convert each number in {list} to a character string can
6412 concatenate them all. Examples: > 6439 concatenate them all. Examples: >
6413 list2str([32]) returns " " 6440 list2str([32]) returns " "
6419 When {utf8} is omitted or zero, the current 'encoding' is used. 6446 When {utf8} is omitted or zero, the current 'encoding' is used.
6420 With {utf8} is 1, always return utf-8 characters. 6447 With {utf8} is 1, always return utf-8 characters.
6421 With utf-8 composing characters work as expected: > 6448 With utf-8 composing characters work as expected: >
6422 list2str([97, 769]) returns "á" 6449 list2str([97, 769]) returns "á"
6423 < 6450 <
6451 Can also be used as a |method|: >
6452 GetList()->list2str()
6453
6424 listener_add({callback} [, {buf}]) *listener_add()* 6454 listener_add({callback} [, {buf}]) *listener_add()*
6425 Add a callback function that will be invoked when changes have 6455 Add a callback function that will be invoked when changes have
6426 been made to buffer {buf}. 6456 been made to buffer {buf}.
6427 {buf} refers to a buffer name or number. For the accepted 6457 {buf} refers to a buffer name or number. For the accepted
6428 values, see |bufname()|. When {buf} is omitted the current 6458 values, see |bufname()|. When {buf} is omitted the current
6488 Use the |BufReadPost| autocmd event to handle the initial text 6518 Use the |BufReadPost| autocmd event to handle the initial text
6489 of a buffer. 6519 of a buffer.
6490 The {callback} is also not invoked when the buffer is 6520 The {callback} is also not invoked when the buffer is
6491 unloaded, use the |BufUnload| autocmd event for that. 6521 unloaded, use the |BufUnload| autocmd event for that.
6492 6522
6523 Can also be used as a |method|, where the base is passed as
6524 the second argument, the buffer: >
6525 GetBuffer()->listener_add(callback)
6526
6493 listener_flush([{buf}]) *listener_flush()* 6527 listener_flush([{buf}]) *listener_flush()*
6494 Invoke listener callbacks for buffer {buf}. If there are no 6528 Invoke listener callbacks for buffer {buf}. If there are no
6495 pending changes then no callbacks are invoked. 6529 pending changes then no callbacks are invoked.
6496 6530
6497 {buf} refers to a buffer name or number. For the accepted 6531 {buf} refers to a buffer name or number. For the accepted
6498 values, see |bufname()|. When {buf} is omitted the current 6532 values, see |bufname()|. When {buf} is omitted the current
6499 buffer is used. 6533 buffer is used.
6534
6535 Can also be used as a |method|: >
6536 GetBuffer()->listener_flush()
6500 6537
6501 listener_remove({id}) *listener_remove()* 6538 listener_remove({id}) *listener_remove()*
6502 Remove a listener previously added with listener_add(). 6539 Remove a listener previously added with listener_add().
6503 Returns zero when {id} could not be found, one when {id} was 6540 Returns zero when {id} could not be found, one when {id} was
6504 removed. 6541 removed.
6542
6543 Can also be used as a |method|: >
6544 GetListenerId()->listener_remove()
6505 6545
6506 localtime() *localtime()* 6546 localtime() *localtime()*
6507 Return the current time, measured as seconds since 1st Jan 6547 Return the current time, measured as seconds since 1st Jan
6508 1970. See also |strftime()| and |getftime()|. 6548 1970. See also |strftime()| and |getftime()|.
6509 6549
6548 with |+float| and to numbers otherwise. 6588 with |+float| and to numbers otherwise.
6549 Dictionaries and lists obtained by vim.eval() are returned 6589 Dictionaries and lists obtained by vim.eval() are returned
6550 as-is. 6590 as-is.
6551 Other objects are returned as zero without any errors. 6591 Other objects are returned as zero without any errors.
6552 See |lua-luaeval| for more details. 6592 See |lua-luaeval| for more details.
6553 {only available when compiled with the |+lua| feature} 6593
6594 Can also be used as a |method|: >
6595 GetExpr()->luaeval()
6596
6597 < {only available when compiled with the |+lua| feature}
6554 6598
6555 map({expr1}, {expr2}) *map()* 6599 map({expr1}, {expr2}) *map()*
6556 {expr1} must be a |List| or a |Dictionary|. 6600 {expr1} must be a |List| or a |Dictionary|.
6557 Replace each item in {expr1} with the result of evaluating 6601 Replace each item in {expr1} with the result of evaluating
6558 {expr2}. {expr2} must be a |string| or |Funcref|. 6602 {expr2}. {expr2} must be a |string| or |Funcref|.