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