usr_50.txt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. *usr_50.txt* For Vim version 9.0. Last change: 2022 Jun 20
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Advanced Vim script writing
  4. |50.1| Exceptions
  5. |50.2| Function with variable number of arguments
  6. |50.3| Restoring the view
  7. Next chapter: |usr_51.txt| Create a plugin
  8. Previous chapter: |usr_45.txt| Select your language (local)
  9. Table of contents: |usr_toc.txt|
  10. ==============================================================================
  11. *50.1* Exceptions
  12. Let's start with an example: >
  13. try
  14. read ~/templates/pascal.tmpl
  15. catch /E484:/
  16. echo "Sorry, the Pascal template file cannot be found."
  17. endtry
  18. The `read` command will fail if the file does not exist. Instead of
  19. generating an error message, this code catches the error and gives the user a
  20. message with more information.
  21. For the commands in between `try` and `endtry` errors are turned into
  22. exceptions. An exception is a string. In the case of an error the string
  23. contains the error message. And every error message has a number. In this
  24. case, the error we catch contains "E484:". This number is guaranteed to stay
  25. the same (the text may change, e.g., it may be translated).
  26. Besides being able to give a nice error message, Vim will also continue
  27. executing commands after the `:endtry`. Otherwise, once an uncaught error is
  28. encountered, execution of the script/function/mapping will be aborted.
  29. When the `read` command causes another error, the pattern "E484:" will not
  30. match in it. Thus this exception will not be caught and result in the usual
  31. error message and execution is aborted.
  32. You might be tempted to do this: >
  33. try
  34. read ~/templates/pascal.tmpl
  35. catch
  36. echo "Sorry, the Pascal template file cannot be found."
  37. endtry
  38. This means all errors are caught. But then you will not see an error that
  39. would indicate a completely different problem, such as "E21: Cannot make
  40. changes, 'modifiable' is off". Think twice before you catch any error!
  41. Another useful mechanism is the `finally` command: >
  42. var tmp = tempname()
  43. try
  44. exe ":.,$write " .. tmp
  45. exe "!filter " .. tmp
  46. :.,$delete
  47. exe ":$read " .. tmp
  48. finally
  49. delete(tmp)
  50. endtry
  51. This filters the lines from the cursor until the end of the file through the
  52. "filter" command, which takes a file name argument. No matter if the
  53. filtering works, if something goes wrong in between `try` and `finally` or the
  54. user cancels the filtering by pressing CTRL-C, the `delete(tmp)` call is
  55. always executed. This makes sure you don't leave the temporary file behind.
  56. The `finally` does not catch the exception, the error will still abort
  57. further execution.
  58. More information about exception handling can be found in the reference
  59. manual: |exception-handling|.
  60. ==============================================================================
  61. *50.2* Function with variable number of arguments
  62. Vim enables you to define functions that have a variable number of arguments.
  63. The following command, for instance, defines a function that must have 1
  64. argument (start) and can have up to 20 additional arguments: >
  65. def Show(start: string, ...items: list<string>)
  66. The variable "items" will be a list in the function containing the extra
  67. arguments. You can use it like any list, for example: >
  68. def Show(start: string, ...items: list<string>)
  69. echohl Title
  70. echo "start is " .. start
  71. echohl None
  72. for index in range(len(items))
  73. echon $" Arg {index} is {items[index]}"
  74. endfor
  75. echo
  76. enddef
  77. You can call it like this: >
  78. Show('Title', 'one', 'two', 'three')
  79. < start is Title Arg 0 is one Arg 1 is two Arg 2 is three ~
  80. This uses the `echohl` command to specify the highlighting used for the
  81. following `echo` command. `echohl None` stops it again. The `echon` command
  82. works like `echo`, but doesn't output a line break.
  83. If you call it with one argument the "items" list will be empty.
  84. `range(len(items))` returns a list with the indexes, what `for` loops over,
  85. we'll explain that further down.
  86. ==============================================================================
  87. *50.3* Restoring the view
  88. Sometimes you want to jump around, make a change and then go back to the same
  89. position and view. For example to change something in the file header. This
  90. can be done with two functions: >
  91. var view = winsaveview()
  92. # Move around, make changes
  93. winrestview(view)
  94. ==============================================================================
  95. Next chapter: |usr_51.txt| Create a plugin
  96. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: