annotate runtime/doc/usr_50.txt @ 33738:2172872dfbcd v9.0.2096

patch 9.0.2096: Vim9: confusing usage of private Commit: https://github.com/vim/vim/commit/03042a2753e3e6ac971045a8ce256d709214710e Author: Ernie Rael <errael@raelity.com> Date: Sat Nov 11 08:53:32 2023 +0100 patch 9.0.2096: Vim9: confusing usage of private Problem: Vim9: confusing usage of private Solution: clarify and use protected keyword instead [vim9class] document `_` as protected instead of private fixes #13504 closes: #13520 Signed-off-by: Ernie Rael <errael@raelity.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Sat, 11 Nov 2023 09:00:06 +0100
parents f8116058ca76
children 4635e43f2c6f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29314
f8116058ca76 release version 9.0
Bram Moolenaar <Bram@vim.org>
parents: 29290
diff changeset
1 *usr_50.txt* For Vim version 9.0. Last change: 2022 Jun 20
28862
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 VIM USER MANUAL - by Bram Moolenaar
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 Advanced Vim script writing
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7
29104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
8 |50.1| Exceptions
29290
dc4de65a7fb7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29269
diff changeset
9 |50.2| Function with variable number of arguments
29104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
10 |50.3| Restoring the view
28862
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 Next chapter: |usr_51.txt| Create a plugin
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13 Previous chapter: |usr_45.txt| Select your language (local)
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 Table of contents: |usr_toc.txt|
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 ==============================================================================
29104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
17 *50.1* Exceptions
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
18
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
19 Let's start with an example: >
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
20
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
21 try
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
22 read ~/templates/pascal.tmpl
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
23 catch /E484:/
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
24 echo "Sorry, the Pascal template file cannot be found."
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
25 endtry
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
26
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
27 The `read` command will fail if the file does not exist. Instead of
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
28 generating an error message, this code catches the error and gives the user a
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
29 message with more information.
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
30
29104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
31 For the commands in between `try` and `endtry` errors are turned into
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
32 exceptions. An exception is a string. In the case of an error the string
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
33 contains the error message. And every error message has a number. In this
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
34 case, the error we catch contains "E484:". This number is guaranteed to stay
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
35 the same (the text may change, e.g., it may be translated).
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
36
29104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
37 Besides being able to give a nice error message, Vim will also continue
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
38 executing commands after the `:endtry`. Otherwise, once an uncaught error is
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
39 encountered, execution of the script/function/mapping will be aborted.
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
40
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
41 When the `read` command causes another error, the pattern "E484:" will not
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
42 match in it. Thus this exception will not be caught and result in the usual
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
43 error message and execution is aborted.
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
44
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
45 You might be tempted to do this: >
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
46
29104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
47 try
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
48 read ~/templates/pascal.tmpl
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
49 catch
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
50 echo "Sorry, the Pascal template file cannot be found."
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
51 endtry
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
52
29104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
53 This means all errors are caught. But then you will not see an error that
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
54 would indicate a completely different problem, such as "E21: Cannot make
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
55 changes, 'modifiable' is off". Think twice before you catch any error!
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
56
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
57 Another useful mechanism is the `finally` command: >
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
58
29104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
59 var tmp = tempname()
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
60 try
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
61 exe ":.,$write " .. tmp
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
62 exe "!filter " .. tmp
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
63 :.,$delete
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
64 exe ":$read " .. tmp
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
65 finally
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
66 delete(tmp)
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
67 endtry
28862
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68
29104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
69 This filters the lines from the cursor until the end of the file through the
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
70 "filter" command, which takes a file name argument. No matter if the
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
71 filtering works, if something goes wrong in between `try` and `finally` or the
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
72 user cancels the filtering by pressing CTRL-C, the `delete(tmp)` call is
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
73 always executed. This makes sure you don't leave the temporary file behind.
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
74
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
75 The `finally` does not catch the exception, the error will still abort
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
76 further execution.
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
77
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
78 More information about exception handling can be found in the reference
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
79 manual: |exception-handling|.
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
80
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
81 ==============================================================================
29104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
82 *50.2* Function with variable number of arguments
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
83
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
84 Vim enables you to define functions that have a variable number of arguments.
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
85 The following command, for instance, defines a function that must have 1
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
86 argument (start) and can have up to 20 additional arguments: >
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
87
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
88 def Show(start: string, ...items: list<string>)
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
89
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
90 The variable "items" will be a list in the function containing the extra
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
91 arguments. You can use it like any list, for example: >
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
92
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
93 def Show(start: string, ...items: list<string>)
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
94 echohl Title
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
95 echo "start is " .. start
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
96 echohl None
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
97 for index in range(len(items))
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
98 echon $" Arg {index} is {items[index]}"
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
99 endfor
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
100 echo
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
101 enddef
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
102
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
103 You can call it like this: >
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
105 Show('Title', 'one', 'two', 'three')
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
106 < start is Title Arg 0 is one Arg 1 is two Arg 2 is three ~
29290
dc4de65a7fb7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29269
diff changeset
107
29104
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
108 This uses the `echohl` command to specify the highlighting used for the
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
109 following `echo` command. `echohl None` stops it again. The `echon` command
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
110 works like `echo`, but doesn't output a line break.
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
111
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
112 If you call it with one argument the "items" list will be empty.
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
113 `range(len(items))` returns a list with the indexes, what `for` loops over,
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
114 we'll explain that further down.
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
115
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
116 ==============================================================================
c58baa6d6dda Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
117 *50.3* Restoring the view
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
118
29269
0fdf36de4018 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29104
diff changeset
119 Sometimes you want to jump around, make a change and then go back to the same
0fdf36de4018 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29104
diff changeset
120 position and view. For example to change something in the file header. This
0fdf36de4018 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29104
diff changeset
121 can be done with two functions: >
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
122
29269
0fdf36de4018 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29104
diff changeset
123 var view = winsaveview()
0fdf36de4018 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29104
diff changeset
124 # Move around, make changes
0fdf36de4018 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29104
diff changeset
125 winrestview(view)
28862
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127 ==============================================================================
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 Next chapter: |usr_51.txt| Create a plugin
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
131 Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: