comparison runtime/doc/usr_41.txt @ 22171:d4c7b3e9cd17

Update runtime files. Commit: https://github.com/vim/vim/commit/1c6737b20a5cf71751b180461cea22fc76d8870c Author: Bram Moolenaar <Bram@vim.org> Date: Mon Sep 7 22:18:52 2020 +0200 Update runtime files.
author Bram Moolenaar <Bram@vim.org>
date Mon, 07 Sep 2020 22:30:04 +0200
parents 335365fcbb60
children f22acf6472da
comparison
equal deleted inserted replaced
22170:ac11f9fee87a 22171:d4c7b3e9cd17
325 Grouping is done with parentheses. No surprises here. Example: > 325 Grouping is done with parentheses. No surprises here. Example: >
326 326
327 :echo (10 + 5) * 2 327 :echo (10 + 5) * 2
328 < 30 ~ 328 < 30 ~
329 329
330 Strings can be concatenated with ".". Example: > 330 Strings can be concatenated with ".." (see |expr6|). Example: >
331 331
332 :echo "foo" . "bar" 332 :echo "foo" .. "bar"
333 < foobar ~ 333 < foobar ~
334 334
335 When the ":echo" command gets multiple arguments, it separates them with a 335 When the ":echo" command gets multiple arguments, it separates them with a
336 space. In the example the argument is a single expression, thus no space is 336 space. In the example the argument is a single expression, thus no space is
337 inserted. 337 inserted.
494 So far the commands in the script were executed by Vim directly. The 494 So far the commands in the script were executed by Vim directly. The
495 ":execute" command allows executing the result of an expression. This is a 495 ":execute" command allows executing the result of an expression. This is a
496 very powerful way to build commands and execute them. 496 very powerful way to build commands and execute them.
497 An example is to jump to a tag, which is contained in a variable: > 497 An example is to jump to a tag, which is contained in a variable: >
498 498
499 :execute "tag " . tag_name 499 :execute "tag " .. tag_name
500 500
501 The "." is used to concatenate the string "tag " with the value of variable 501 The ".." is used to concatenate the string "tag " with the value of variable
502 "tag_name". Suppose "tag_name" has the value "get_cmd", then the command that 502 "tag_name". Suppose "tag_name" has the value "get_cmd", then the command that
503 will be executed is: > 503 will be executed is: >
504 504
505 :tag get_cmd 505 :tag get_cmd
506 506
512 512
513 This jumps to the first line and formats all lines with the "=" operator. 513 This jumps to the first line and formats all lines with the "=" operator.
514 To make ":normal" work with an expression, combine ":execute" with it. 514 To make ":normal" work with an expression, combine ":execute" with it.
515 Example: > 515 Example: >
516 516
517 :execute "normal " . normal_commands 517 :execute "normal " .. normal_commands
518 518
519 The variable "normal_commands" must contain the Normal mode commands. 519 The variable "normal_commands" must contain the Normal mode commands.
520 Make sure that the argument for ":normal" is a complete command. Otherwise 520 Make sure that the argument for ":normal" is a complete command. Otherwise
521 Vim will run into the end of the argument and abort the command. For example, 521 Vim will run into the end of the argument and abort the command. For example,
522 if you start Insert mode, you must leave Insert mode as well. This works: > 522 if you start Insert mode, you must leave Insert mode as well. This works: >
529 529
530 If you don't want to execute a string but evaluate it to get its expression 530 If you don't want to execute a string but evaluate it to get its expression
531 value, you can use the eval() function: > 531 value, you can use the eval() function: >
532 532
533 :let optname = "path" 533 :let optname = "path"
534 :let optval = eval('&' . optname) 534 :let optval = eval('&' .. optname)
535 535
536 A "&" character is prepended to "path", thus the argument to eval() is 536 A "&" character is prepended to "path", thus the argument to eval() is
537 "&path". The result will then be the value of the 'path' option. 537 "&path". The result will then be the value of the 'path' option.
538 The same thing can be done with: > 538 The same thing can be done with: >
539 :exe 'let optval = &' . optname 539 :exe 'let optval = &' .. optname
540 540
541 ============================================================================== 541 ==============================================================================
542 *41.6* Using functions 542 *41.6* Using functions
543 543
544 Vim defines many functions and provides a large amount of functionality that 544 Vim defines many functions and provides a large amount of functionality that
1286 : let n = 0 1286 : let n = 0
1287 : while lnum <= a:lastline 1287 : while lnum <= a:lastline
1288 : let n = n + len(split(getline(lnum))) 1288 : let n = n + len(split(getline(lnum)))
1289 : let lnum = lnum + 1 1289 : let lnum = lnum + 1
1290 : endwhile 1290 : endwhile
1291 : echo "found " . n . " words" 1291 : echo "found " .. n .. " words"
1292 :endfunction 1292 :endfunction
1293 1293
1294 You can call this function with: > 1294 You can call this function with: >
1295 1295
1296 :10,30call Count_words() 1296 :10,30call Count_words()
1299 The other way to use a line range is by defining a function without the 1299 The other way to use a line range is by defining a function without the
1300 "range" keyword. The function will be called once for every line in the 1300 "range" keyword. The function will be called once for every line in the
1301 range, with the cursor in that line. Example: > 1301 range, with the cursor in that line. Example: >
1302 1302
1303 :function Number() 1303 :function Number()
1304 : echo "line " . line(".") . " contains: " . getline(".") 1304 : echo "line " .. line(".") .. " contains: " .. getline(".")
1305 :endfunction 1305 :endfunction
1306 1306
1307 If you call this function with: > 1307 If you call this function with: >
1308 1308
1309 :10,15call Number() 1309 :10,15call Number()
1323 so on. The variable "a:0" contains the number of extra arguments. 1323 so on. The variable "a:0" contains the number of extra arguments.
1324 For example: > 1324 For example: >
1325 1325
1326 :function Show(start, ...) 1326 :function Show(start, ...)
1327 : echohl Title 1327 : echohl Title
1328 : echo "start is " . a:start 1328 : echo "start is " .. a:start
1329 : echohl None 1329 : echohl None
1330 : let index = 1 1330 : let index = 1
1331 : while index <= a:0 1331 : while index <= a:0
1332 : echo " Arg " . index . " is " . a:{index} 1332 : echo " Arg " .. index .. " is " .. a:{index}
1333 : let index = index + 1 1333 : let index = index + 1
1334 : endwhile 1334 : endwhile
1335 : echo "" 1335 : echo ""
1336 :endfunction 1336 :endfunction
1337 1337
1735 1735
1736 Another useful mechanism is the ":finally" command: > 1736 Another useful mechanism is the ":finally" command: >
1737 1737
1738 :let tmp = tempname() 1738 :let tmp = tempname()
1739 :try 1739 :try
1740 : exe ".,$write " . tmp 1740 : exe ".,$write " .. tmp
1741 : exe "!filter " . tmp 1741 : exe "!filter " .. tmp
1742 : .,$delete 1742 : .,$delete
1743 : exe "$read " . tmp 1743 : exe "$read " .. tmp
1744 :finally 1744 :finally
1745 : call delete(tmp) 1745 : call delete(tmp)
1746 :endtry 1746 :endtry
1747 1747
1748 This filters the lines from the cursor until the end of the file through the 1748 This filters the lines from the cursor until the end of the file through the
2089 prepending it with "s:". 2089 prepending it with "s:".
2090 2090
2091 We will define a function that adds a new typing correction: > 2091 We will define a function that adds a new typing correction: >
2092 2092
2093 30 function s:Add(from, correct) 2093 30 function s:Add(from, correct)
2094 31 let to = input("type the correction for " . a:from . ": ") 2094 31 let to = input("type the correction for " .. a:from .. ": ")
2095 32 exe ":iabbrev " . a:from . " " . to 2095 32 exe ":iabbrev " .. a:from .. " " .. to
2096 .. 2096 ..
2097 36 endfunction 2097 36 endfunction
2098 2098
2099 Now we can call the function s:Add() from within this script. If another 2099 Now we can call the function s:Add() from within this script. If another
2100 script also defines s:Add(), it will be local to that script and can only 2100 script also defines s:Add(), it will be local to that script and can only
2195 19 let s:count = 4 2195 19 let s:count = 4
2196 .. 2196 ..
2197 30 function s:Add(from, correct) 2197 30 function s:Add(from, correct)
2198 .. 2198 ..
2199 34 let s:count = s:count + 1 2199 34 let s:count = s:count + 1
2200 35 echo s:count . " corrections now" 2200 35 echo s:count .. " corrections now"
2201 36 endfunction 2201 36 endfunction
2202 2202
2203 First s:count is initialized to 4 in the script itself. When later the 2203 First s:count is initialized to 4 in the script itself. When later the
2204 s:Add() function is called, it increments s:count. It doesn't matter from 2204 s:Add() function is called, it increments s:count. It doesn't matter from
2205 where the function was called, since it has been defined in the script, it 2205 where the function was called, since it has been defined in the script, it
2238 26 noremenu <script> Plugin.Add\ Correction <SID>Add 2238 26 noremenu <script> Plugin.Add\ Correction <SID>Add
2239 27 2239 27
2240 28 noremap <SID>Add :call <SID>Add(expand("<cword>"), 1)<CR> 2240 28 noremap <SID>Add :call <SID>Add(expand("<cword>"), 1)<CR>
2241 29 2241 29
2242 30 function s:Add(from, correct) 2242 30 function s:Add(from, correct)
2243 31 let to = input("type the correction for " . a:from . ": ") 2243 31 let to = input("type the correction for " .. a:from .. ": ")
2244 32 exe ":iabbrev " . a:from . " " . to 2244 32 exe ":iabbrev " .. a:from .. " " .. to
2245 33 if a:correct | exe "normal viws\<C-R>\" \b\e" | endif 2245 33 if a:correct | exe "normal viws\<C-R>\" \b\e" | endif
2246 34 let s:count = s:count + 1 2246 34 let s:count = s:count + 1
2247 35 echo s:count . " corrections now" 2247 35 echo s:count .. " corrections now"
2248 36 endfunction 2248 36 endfunction
2249 37 2249 37
2250 38 if !exists(":Correct") 2250 38 if !exists(":Correct")
2251 39 command -nargs=1 Correct :call s:Add(<q-args>, 0) 2251 39 command -nargs=1 Correct :call s:Add(<q-args>, 0)
2252 40 endif 2252 40 endif
2490 When the user does ":setfiletype xyz" the effect of the previous filetype 2490 When the user does ":setfiletype xyz" the effect of the previous filetype
2491 should be undone. Set the b:undo_ftplugin variable to the commands that will 2491 should be undone. Set the b:undo_ftplugin variable to the commands that will
2492 undo the settings in your filetype plugin. Example: > 2492 undo the settings in your filetype plugin. Example: >
2493 2493
2494 let b:undo_ftplugin = "setlocal fo< com< tw< commentstring<" 2494 let b:undo_ftplugin = "setlocal fo< com< tw< commentstring<"
2495 \ . "| unlet b:match_ignorecase b:match_words b:match_skip" 2495 \ .. "| unlet b:match_ignorecase b:match_words b:match_skip"
2496 2496
2497 Using ":setlocal" with "<" after the option name resets the option to its 2497 Using ":setlocal" with "<" after the option name resets the option to its
2498 global value. That is mostly the best way to reset the option value. 2498 global value. That is mostly the best way to reset the option value.
2499 2499
2500 This does require removing the "C" flag from 'cpoptions' to allow line 2500 This does require removing the "C" flag from 'cpoptions' to allow line
2611 if !exists("s:did_load") 2611 if !exists("s:did_load")
2612 command -nargs=* BNRead call BufNetRead(<f-args>) 2612 command -nargs=* BNRead call BufNetRead(<f-args>)
2613 map <F19> :call BufNetWrite('something')<CR> 2613 map <F19> :call BufNetWrite('something')<CR>
2614 2614
2615 let s:did_load = 1 2615 let s:did_load = 1
2616 exe 'au FuncUndefined BufNet* source ' . expand('<sfile>') 2616 exe 'au FuncUndefined BufNet* source ' .. expand('<sfile>')
2617 finish 2617 finish
2618 endif 2618 endif
2619 2619
2620 function BufNetRead(...) 2620 function BufNetRead(...)
2621 echo 'BufNetRead(' . string(a:000) . ')' 2621 echo 'BufNetRead(' .. string(a:000) .. ')'
2622 " read functionality here 2622 " read functionality here
2623 endfunction 2623 endfunction
2624 2624
2625 function BufNetWrite(...) 2625 function BufNetWrite(...)
2626 echo 'BufNetWrite(' . string(a:000) . ')' 2626 echo 'BufNetWrite(' .. string(a:000) .. ')'
2627 " write functionality here 2627 " write functionality here
2628 endfunction 2628 endfunction
2629 2629
2630 When the script is first loaded "s:did_load" is not set. The commands between 2630 When the script is first loaded "s:did_load" is not set. The commands between
2631 the "if" and "endif" will be executed. This ends in a |:finish| command, thus 2631 the "if" and "endif" will be executed. This ends in a |:finish| command, thus