Bram Moolenaar | b1c9198 | 2018-05-17 17:04:55 +0200 | [diff] [blame] | 1 | *if_pyth.txt* For Vim version 8.1. Last change: 2018 Jan 30 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Paul Moore |
| 5 | |
| 6 | |
| 7 | The Python Interface to Vim *python* *Python* |
| 8 | |
Bram Moolenaar | 30b6581 | 2012-07-12 22:01:11 +0200 | [diff] [blame] | 9 | 1. Commands |python-commands| |
| 10 | 2. The vim module |python-vim| |
| 11 | 3. Buffer objects |python-buffer| |
| 12 | 4. Range objects |python-range| |
| 13 | 5. Window objects |python-window| |
Bram Moolenaar | cac867a | 2013-05-21 19:50:34 +0200 | [diff] [blame] | 14 | 6. Tab page objects |python-tabpage| |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 15 | 7. vim.bindeval objects |python-bindeval-objects| |
| 16 | 8. pyeval(), py3eval() Vim functions |python-pyeval| |
| 17 | 9. Dynamic loading |python-dynamic| |
| 18 | 10. Python 3 |python3| |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 19 | 11. Python X |python_x| |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 20 | 12. Building with Python support |python-building| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 21 | |
| 22 | {Vi does not have any of these commands} |
| 23 | |
Bram Moolenaar | 368373e | 2010-07-19 20:46:22 +0200 | [diff] [blame] | 24 | The Python 2.x interface is available only when Vim was compiled with the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 25 | |+python| feature. |
Bram Moolenaar | 368373e | 2010-07-19 20:46:22 +0200 | [diff] [blame] | 26 | The Python 3 interface is available only when Vim was compiled with the |
| 27 | |+python3| feature. |
Bram Moolenaar | 9ba7e17 | 2013-07-17 22:37:26 +0200 | [diff] [blame] | 28 | Both can be available at the same time, but read |python-2-and-3|. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | |
| 30 | ============================================================================== |
| 31 | 1. Commands *python-commands* |
| 32 | |
Bram Moolenaar | dbc2802 | 2014-07-26 13:40:44 +0200 | [diff] [blame] | 33 | *:python* *:py* *E263* *E264* *E887* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 34 | :[range]py[thon] {stmt} |
Bram Moolenaar | 9b45125 | 2012-08-15 17:43:31 +0200 | [diff] [blame] | 35 | Execute Python statement {stmt}. A simple check if |
| 36 | the `:python` command is working: > |
| 37 | :python print "Hello" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | |
| 39 | :[range]py[thon] << {endmarker} |
| 40 | {script} |
| 41 | {endmarker} |
| 42 | Execute Python script {script}. |
| 43 | Note: This command doesn't work when the Python |
| 44 | feature wasn't compiled in. To avoid errors, see |
| 45 | |script-here|. |
| 46 | |
| 47 | {endmarker} must NOT be preceded by any white space. If {endmarker} is |
| 48 | omitted from after the "<<", a dot '.' must be used after {script}, like |
| 49 | for the |:append| and |:insert| commands. |
| 50 | This form of the |:python| command is mainly useful for including python code |
| 51 | in Vim scripts. |
| 52 | |
| 53 | Example: > |
| 54 | function! IcecreamInitialize() |
| 55 | python << EOF |
| 56 | class StrawberryIcecream: |
| 57 | def __call__(self): |
| 58 | print 'EAT ME' |
| 59 | EOF |
| 60 | endfunction |
Bram Moolenaar | 64d8e25 | 2016-09-06 22:12:34 +0200 | [diff] [blame] | 61 | |
| 62 | To see what version of Python you have: > |
Bram Moolenaar | 64d8e25 | 2016-09-06 22:12:34 +0200 | [diff] [blame] | 63 | :python print(sys.version) |
| 64 | |
Bram Moolenaar | 95bafa2 | 2018-10-02 13:26:25 +0200 | [diff] [blame] | 65 | There is no need to import sys, it's done by default. |
| 66 | |
Bram Moolenaar | a3e6bc9 | 2013-01-30 14:18:00 +0100 | [diff] [blame] | 67 | Note: Python is very sensitive to the indenting. Make sure the "class" line |
| 68 | and "EOF" do not have any indent. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | |
Bram Moolenaar | d620aa9 | 2013-05-17 16:40:06 +0200 | [diff] [blame] | 70 | *:pydo* |
| 71 | :[range]pydo {body} Execute Python function "def _vim_pydo(line, linenr): |
| 72 | {body}" for each line in the [range], with the |
| 73 | function arguments being set to the text of each line |
| 74 | in turn, without a trailing <EOL>, and the current |
| 75 | line number. The function should return a string or |
| 76 | None. If a string is returned, it becomes the text of |
| 77 | the line in the current turn. The default for [range] |
| 78 | is the whole file: "1,$". |
| 79 | {not in Vi} |
| 80 | |
| 81 | Examples: |
| 82 | > |
| 83 | :pydo return "%s\t%d" % (line[::-1], len(line)) |
| 84 | :pydo if line: return "%4d: %s" % (linenr, line) |
| 85 | < |
Bram Moolenaar | 20aac6c | 2018-09-02 21:07:30 +0200 | [diff] [blame] | 86 | One can use `:pydo` in possible conjunction with `:py` to filter a range using |
| 87 | python. For example: > |
| 88 | |
| 89 | :py3 << EOF |
| 90 | needle = vim.eval('@a') |
| 91 | replacement = vim.eval('@b') |
| 92 | |
| 93 | def py_vim_string_replace(str): |
| 94 | return str.replace(needle, replacement) |
| 95 | EOF |
| 96 | :'<,'>py3do return py_vim_string_replace(line) |
| 97 | < |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 98 | *:pyfile* *:pyf* |
| 99 | :[range]pyf[ile] {file} |
| 100 | Execute the Python script in {file}. The whole |
| 101 | argument is used as a single file name. {not in Vi} |
| 102 | |
| 103 | Both of these commands do essentially the same thing - they execute a piece of |
| 104 | Python code, with the "current range" |python-range| set to the given line |
| 105 | range. |
| 106 | |
| 107 | In the case of :python, the code to execute is in the command-line. |
| 108 | In the case of :pyfile, the code to execute is the contents of the given file. |
| 109 | |
| 110 | Python commands cannot be used in the |sandbox|. |
| 111 | |
| 112 | To pass arguments you need to set sys.argv[] explicitly. Example: > |
| 113 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 114 | :python sys.argv = ["foo", "bar"] |
| 115 | :pyfile myscript.py |
| 116 | |
| 117 | Here are some examples *python-examples* > |
| 118 | |
| 119 | :python from vim import * |
| 120 | :python from string import upper |
| 121 | :python current.line = upper(current.line) |
| 122 | :python print "Hello" |
| 123 | :python str = current.buffer[42] |
| 124 | |
| 125 | (Note that changes - like the imports - persist from one command to the next, |
| 126 | just like in the Python interpreter.) |
| 127 | |
| 128 | ============================================================================== |
| 129 | 2. The vim module *python-vim* |
| 130 | |
| 131 | Python code gets all of its access to vim (with one exception - see |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 132 | |python-output| below) via the "vim" module. The vim module implements two |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 133 | methods, three constants, and one error object. You need to import the vim |
| 134 | module before using it: > |
| 135 | :python import vim |
| 136 | |
| 137 | Overview > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 138 | :py print "Hello" # displays a message |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 139 | :py vim.command(cmd) # execute an Ex command |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 140 | :py w = vim.windows[n] # gets window "n" |
| 141 | :py cw = vim.current.window # gets the current window |
| 142 | :py b = vim.buffers[n] # gets buffer "n" |
| 143 | :py cb = vim.current.buffer # gets the current buffer |
| 144 | :py w.height = lines # sets the window height |
| 145 | :py w.cursor = (row, col) # sets the window cursor position |
| 146 | :py pos = w.cursor # gets a tuple (row, col) |
| 147 | :py name = b.name # gets the buffer file name |
| 148 | :py line = b[n] # gets a line from the buffer |
| 149 | :py lines = b[n:m] # gets a list of lines |
| 150 | :py num = len(b) # gets the number of lines |
| 151 | :py b[n] = str # sets a line in the buffer |
| 152 | :py b[n:m] = [str1, str2, str3] # sets a number of lines at once |
| 153 | :py del b[n] # deletes a line |
| 154 | :py del b[n:m] # deletes a number of lines |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | Methods of the "vim" module |
| 158 | |
| 159 | vim.command(str) *python-command* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 160 | Executes the vim (ex-mode) command str. Returns None. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 161 | Examples: > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 162 | :py vim.command("set tw=72") |
| 163 | :py vim.command("%s/aaa/bbb/g") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 164 | < The following definition executes Normal mode commands: > |
| 165 | def normal(str): |
| 166 | vim.command("normal "+str) |
| 167 | # Note the use of single quotes to delimit a string containing |
| 168 | # double quotes |
| 169 | normal('"a2dd"aP') |
| 170 | < *E659* |
| 171 | The ":python" command cannot be used recursively with Python 2.2 and |
| 172 | older. This only works with Python 2.3 and later: > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 173 | :py vim.command("python print 'Hello again Python'") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 174 | |
| 175 | vim.eval(str) *python-eval* |
| 176 | Evaluates the expression str using the vim internal expression |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 177 | evaluator (see |expression|). Returns the expression result as: |
| 178 | - a string if the Vim expression evaluates to a string or number |
| 179 | - a list if the Vim expression evaluates to a Vim list |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 180 | - a dictionary if the Vim expression evaluates to a Vim dictionary |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 181 | Dictionaries and lists are recursively expanded. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 182 | Examples: > |
Bram Moolenaar | fc65cab | 2018-08-28 22:58:02 +0200 | [diff] [blame] | 183 | :" value of the 'textwidth' option |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 184 | :py text_width = vim.eval("&tw") |
Bram Moolenaar | fc65cab | 2018-08-28 22:58:02 +0200 | [diff] [blame] | 185 | : |
| 186 | :" contents of the 'a' register |
| 187 | :py a_reg = vim.eval("@a") |
| 188 | : |
| 189 | :" Result is a string! Use string.atoi() to convert to a number. |
| 190 | :py str = vim.eval("12+12") |
| 191 | : |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 192 | :py tagList = vim.eval('taglist("eval_expr")') |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 193 | < The latter will return a python list of python dicts, for instance: |
Bram Moolenaar | 214641f | 2017-03-05 17:04:09 +0100 | [diff] [blame] | 194 | [{'cmd': '/^eval_expr(arg, nextcmd)$/', 'static': 0, 'name': ~ |
| 195 | 'eval_expr', 'kind': 'f', 'filename': './src/eval.c'}] ~ |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 196 | |
Bram Moolenaar | 30b6581 | 2012-07-12 22:01:11 +0200 | [diff] [blame] | 197 | vim.bindeval(str) *python-bindeval* |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 198 | Like |python-eval|, but returns special objects described in |
| 199 | |python-bindeval-objects|. These python objects let you modify (|List| |
Bram Moolenaar | de71b56 | 2013-06-02 17:41:54 +0200 | [diff] [blame] | 200 | or |Dictionary|) or call (|Funcref|) vim objects. |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 201 | |
Bram Moolenaar | bc41196 | 2013-06-02 17:46:40 +0200 | [diff] [blame] | 202 | vim.strwidth(str) *python-strwidth* |
| 203 | Like |strwidth()|: returns number of display cells str occupies, tab |
| 204 | is counted as one cell. |
| 205 | |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 206 | vim.foreach_rtp(callable) *python-foreach_rtp* |
| 207 | Call the given callable for each path in 'runtimepath' until either |
| 208 | callable returns something but None, the exception is raised or there |
| 209 | are no longer paths. If stopped in case callable returned non-None, |
| 210 | vim.foreach_rtp function returns the value returned by callable. |
| 211 | |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 212 | vim.chdir(*args, **kwargs) *python-chdir* |
| 213 | vim.fchdir(*args, **kwargs) *python-fchdir* |
| 214 | Run os.chdir or os.fchdir, then all appropriate vim stuff. |
| 215 | Note: you should not use these functions directly, use os.chdir and |
| 216 | os.fchdir instead. Behavior of vim.fchdir is undefined in case |
| 217 | os.fchdir does not exist. |
| 218 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 219 | Error object of the "vim" module |
| 220 | |
| 221 | vim.error *python-error* |
| 222 | Upon encountering a Vim error, Python raises an exception of type |
| 223 | vim.error. |
| 224 | Example: > |
| 225 | try: |
| 226 | vim.command("put a") |
| 227 | except vim.error: |
| 228 | # nothing in register a |
| 229 | |
| 230 | Constants of the "vim" module |
| 231 | |
| 232 | Note that these are not actually constants - you could reassign them. |
| 233 | But this is silly, as you would then lose access to the vim objects |
| 234 | to which the variables referred. |
| 235 | |
| 236 | vim.buffers *python-buffers* |
Bram Moolenaar | dfa38d4 | 2013-05-15 13:38:47 +0200 | [diff] [blame] | 237 | A mapping object providing access to the list of vim buffers. The |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 238 | object supports the following operations: > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 239 | :py b = vim.buffers[i] # Indexing (read-only) |
| 240 | :py b in vim.buffers # Membership test |
| 241 | :py n = len(vim.buffers) # Number of elements |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 242 | :py for b in vim.buffers: # Iterating over buffer list |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 243 | < |
| 244 | vim.windows *python-windows* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 245 | A sequence object providing access to the list of vim windows. The |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 246 | object supports the following operations: > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 247 | :py w = vim.windows[i] # Indexing (read-only) |
| 248 | :py w in vim.windows # Membership test |
| 249 | :py n = len(vim.windows) # Number of elements |
| 250 | :py for w in vim.windows: # Sequential access |
Bram Moolenaar | de71b56 | 2013-06-02 17:41:54 +0200 | [diff] [blame] | 251 | < Note: vim.windows object always accesses current tab page. |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 252 | |python-tabpage|.windows objects are bound to parent |python-tabpage| |
| 253 | object and always use windows from that tab page (or throw vim.error |
| 254 | in case tab page was deleted). You can keep a reference to both |
| 255 | without keeping a reference to vim module object or |python-tabpage|, |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 256 | they will not lose their properties in this case. |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 257 | |
| 258 | vim.tabpages *python-tabpages* |
| 259 | A sequence object providing access to the list of vim tab pages. The |
| 260 | object supports the following operations: > |
| 261 | :py t = vim.tabpages[i] # Indexing (read-only) |
| 262 | :py t in vim.tabpages # Membership test |
| 263 | :py n = len(vim.tabpages) # Number of elements |
| 264 | :py for t in vim.tabpages: # Sequential access |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 265 | < |
| 266 | vim.current *python-current* |
| 267 | An object providing access (via specific attributes) to various |
| 268 | "current" objects available in vim: |
| 269 | vim.current.line The current line (RW) String |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 270 | vim.current.buffer The current buffer (RW) Buffer |
| 271 | vim.current.window The current window (RW) Window |
| 272 | vim.current.tabpage The current tab page (RW) TabPage |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 273 | vim.current.range The current line range (RO) Range |
| 274 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 275 | The last case deserves a little explanation. When the :python or |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 276 | :pyfile command specifies a range, this range of lines becomes the |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 277 | "current range". A range is a bit like a buffer, but with all access |
| 278 | restricted to a subset of lines. See |python-range| for more details. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 279 | |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 280 | Note: When assigning to vim.current.{buffer,window,tabpage} it expects |
| 281 | valid |python-buffer|, |python-window| or |python-tabpage| objects |
| 282 | respectively. Assigning triggers normal (with |autocommand|s) |
| 283 | switching to given buffer, window or tab page. It is the only way to |
| 284 | switch UI objects in python: you can't assign to |
| 285 | |python-tabpage|.window attribute. To switch without triggering |
| 286 | autocommands use > |
| 287 | py << EOF |
| 288 | saved_eventignore = vim.options['eventignore'] |
| 289 | vim.options['eventignore'] = 'all' |
| 290 | try: |
| 291 | vim.current.buffer = vim.buffers[2] # Switch to buffer 2 |
| 292 | finally: |
| 293 | vim.options['eventignore'] = saved_eventignore |
| 294 | EOF |
| 295 | < |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 296 | vim.vars *python-vars* |
| 297 | vim.vvars *python-vvars* |
| 298 | Dictionary-like objects holding dictionaries with global (|g:|) and |
| 299 | vim (|v:|) variables respectively. Identical to `vim.bindeval("g:")`, |
| 300 | but faster. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 301 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 302 | vim.options *python-options* |
| 303 | Object partly supporting mapping protocol (supports setting and |
| 304 | getting items) providing a read-write access to global options. |
| 305 | Note: unlike |:set| this provides access only to global options. You |
| 306 | cannot use this object to obtain or set local options' values or |
| 307 | access local-only options in any fashion. Raises KeyError if no global |
| 308 | option with such name exists (i.e. does not raise KeyError for |
| 309 | |global-local| options and global only options, but does for window- |
| 310 | and buffer-local ones). Use |python-buffer| objects to access to |
| 311 | buffer-local options and |python-window| objects to access to |
| 312 | window-local options. |
| 313 | |
Bram Moolenaar | cac867a | 2013-05-21 19:50:34 +0200 | [diff] [blame] | 314 | Type of this object is available via "Options" attribute of vim |
| 315 | module. |
| 316 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 317 | Output from Python *python-output* |
| 318 | Vim displays all Python code output in the Vim message area. Normal |
| 319 | output appears as information messages, and error output appears as |
| 320 | error messages. |
| 321 | |
| 322 | In implementation terms, this means that all output to sys.stdout |
| 323 | (including the output from print statements) appears as information |
| 324 | messages, and all output to sys.stderr (including error tracebacks) |
| 325 | appears as error messages. |
| 326 | |
| 327 | *python-input* |
| 328 | Input (via sys.stdin, including input() and raw_input()) is not |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 329 | supported, and may cause the program to crash. This should probably be |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 330 | fixed. |
| 331 | |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 332 | *python2-directory* *python3-directory* *pythonx-directory* |
| 333 | Python 'runtimepath' handling *python-special-path* |
| 334 | |
| 335 | In python vim.VIM_SPECIAL_PATH special directory is used as a replacement for |
| 336 | the list of paths found in 'runtimepath': with this directory in sys.path and |
| 337 | vim.path_hooks in sys.path_hooks python will try to load module from |
| 338 | {rtp}/python2 (or python3) and {rtp}/pythonx (for both python versions) for |
| 339 | each {rtp} found in 'runtimepath'. |
| 340 | |
Bram Moolenaar | 81c40c5 | 2013-06-12 14:41:04 +0200 | [diff] [blame] | 341 | Implementation is similar to the following, but written in C: > |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 342 | |
Bram Moolenaar | 9f3685a | 2013-06-12 14:20:36 +0200 | [diff] [blame] | 343 | from imp import find_module, load_module |
| 344 | import vim |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 345 | import sys |
| 346 | |
Bram Moolenaar | 9f3685a | 2013-06-12 14:20:36 +0200 | [diff] [blame] | 347 | class VimModuleLoader(object): |
| 348 | def __init__(self, module): |
| 349 | self.module = module |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 350 | |
Bram Moolenaar | 9f3685a | 2013-06-12 14:20:36 +0200 | [diff] [blame] | 351 | def load_module(self, fullname, path=None): |
| 352 | return self.module |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 353 | |
Bram Moolenaar | 9f3685a | 2013-06-12 14:20:36 +0200 | [diff] [blame] | 354 | def _find_module(fullname, oldtail, path): |
| 355 | idx = oldtail.find('.') |
| 356 | if idx > 0: |
| 357 | name = oldtail[:idx] |
| 358 | tail = oldtail[idx+1:] |
| 359 | fmr = find_module(name, path) |
| 360 | module = load_module(fullname[:-len(oldtail)] + name, *fmr) |
| 361 | return _find_module(fullname, tail, module.__path__) |
| 362 | else: |
| 363 | fmr = find_module(fullname, path) |
| 364 | return load_module(fullname, *fmr) |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 365 | |
Bram Moolenaar | 9f3685a | 2013-06-12 14:20:36 +0200 | [diff] [blame] | 366 | # It uses vim module itself in place of VimPathFinder class: it does not |
| 367 | # matter for python which object has find_module function attached to as |
| 368 | # an attribute. |
| 369 | class VimPathFinder(object): |
Bram Moolenaar | 81c40c5 | 2013-06-12 14:41:04 +0200 | [diff] [blame] | 370 | @classmethod |
Bram Moolenaar | 9f3685a | 2013-06-12 14:20:36 +0200 | [diff] [blame] | 371 | def find_module(cls, fullname, path=None): |
| 372 | try: |
| 373 | return VimModuleLoader(_find_module(fullname, fullname, path or vim._get_paths())) |
| 374 | except ImportError: |
| 375 | return None |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 376 | |
Bram Moolenaar | 81c40c5 | 2013-06-12 14:41:04 +0200 | [diff] [blame] | 377 | @classmethod |
Bram Moolenaar | 9f3685a | 2013-06-12 14:20:36 +0200 | [diff] [blame] | 378 | def load_module(cls, fullname, path=None): |
| 379 | return _find_module(fullname, fullname, path or vim._get_paths()) |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 380 | |
Bram Moolenaar | 9f3685a | 2013-06-12 14:20:36 +0200 | [diff] [blame] | 381 | def hook(path): |
| 382 | if path == vim.VIM_SPECIAL_PATH: |
| 383 | return VimPathFinder |
| 384 | else: |
| 385 | raise ImportError |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 386 | |
Bram Moolenaar | 9f3685a | 2013-06-12 14:20:36 +0200 | [diff] [blame] | 387 | sys.path_hooks.append(hook) |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 388 | |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 389 | vim.VIM_SPECIAL_PATH *python-VIM_SPECIAL_PATH* |
| 390 | String constant used in conjunction with vim path hook. If path hook |
| 391 | installed by vim is requested to handle anything but path equal to |
| 392 | vim.VIM_SPECIAL_PATH constant it raises ImportError. In the only other |
| 393 | case it uses special loader. |
| 394 | |
| 395 | Note: you must not use value of this constant directly, always use |
| 396 | vim.VIM_SPECIAL_PATH object. |
| 397 | |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 398 | vim.find_module(...) *python-find_module* |
| 399 | vim.path_hook(path) *python-path_hook* |
| 400 | Methods or objects used to implement path loading as described above. |
| 401 | You should not be using any of these directly except for vim.path_hook |
| 402 | in case you need to do something with sys.meta_path. It is not |
| 403 | guaranteed that any of the objects will exist in the future vim |
Bram Moolenaar | 81c40c5 | 2013-06-12 14:41:04 +0200 | [diff] [blame] | 404 | versions. |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 405 | |
| 406 | vim._get_paths *python-_get_paths* |
| 407 | Methods returning a list of paths which will be searched for by path |
| 408 | hook. You should not rely on this method being present in future |
| 409 | versions, but can use it for debugging. |
| 410 | |
| 411 | It returns a list of {rtp}/python2 (or {rtp}/python3) and |
| 412 | {rtp}/pythonx directories for each {rtp} in 'runtimepath'. |
| 413 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 414 | ============================================================================== |
| 415 | 3. Buffer objects *python-buffer* |
| 416 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 417 | Buffer objects represent vim buffers. You can obtain them in a number of ways: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 418 | - via vim.current.buffer (|python-current|) |
| 419 | - from indexing vim.buffers (|python-buffers|) |
| 420 | - from the "buffer" attribute of a window (|python-window|) |
| 421 | |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 422 | Buffer objects have two read-only attributes - name - the full file name for |
| 423 | the buffer, and number - the buffer number. They also have three methods |
| 424 | (append, mark, and range; see below). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 425 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 426 | You can also treat buffer objects as sequence objects. In this context, they |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 427 | act as if they were lists (yes, they are mutable) of strings, with each |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 428 | element being a line of the buffer. All of the usual sequence operations, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 429 | including indexing, index assignment, slicing and slice assignment, work as |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 430 | you would expect. Note that the result of indexing (slicing) a buffer is a |
| 431 | string (list of strings). This has one unusual consequence - b[:] is different |
| 432 | from b. In particular, "b[:] = None" deletes the whole of the buffer, whereas |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 433 | "b = None" merely updates the variable b, with no effect on the buffer. |
| 434 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 435 | Buffer indexes start at zero, as is normal in Python. This differs from vim |
| 436 | line numbers, which start from 1. This is particularly relevant when dealing |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 437 | with marks (see below) which use vim line numbers. |
| 438 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 439 | The buffer object attributes are: |
| 440 | b.vars Dictionary-like object used to access |
| 441 | |buffer-variable|s. |
| 442 | b.options Mapping object (supports item getting, setting and |
| 443 | deleting) that provides access to buffer-local options |
| 444 | and buffer-local values of |global-local| options. Use |
| 445 | |python-window|.options if option is window-local, |
| 446 | this object will raise KeyError. If option is |
| 447 | |global-local| and local value is missing getting it |
| 448 | will return None. |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 449 | b.name String, RW. Contains buffer name (full path). |
| 450 | Note: when assigning to b.name |BufFilePre| and |
| 451 | |BufFilePost| autocommands are launched. |
| 452 | b.number Buffer number. Can be used as |python-buffers| key. |
| 453 | Read-only. |
Bram Moolenaar | 203d04d | 2013-06-06 21:36:40 +0200 | [diff] [blame] | 454 | b.valid True or False. Buffer object becomes invalid when |
Bram Moolenaar | bc41196 | 2013-06-02 17:46:40 +0200 | [diff] [blame] | 455 | corresponding buffer is wiped out. |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 456 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 457 | The buffer object methods are: |
| 458 | b.append(str) Append a line to the buffer |
Bram Moolenaar | 2c3b1d9 | 2010-07-24 16:58:02 +0200 | [diff] [blame] | 459 | b.append(str, nr) Idem, below line "nr" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 460 | b.append(list) Append a list of lines to the buffer |
| 461 | Note that the option of supplying a list of strings to |
| 462 | the append method differs from the equivalent method |
| 463 | for Python's built-in list objects. |
Bram Moolenaar | 2c3b1d9 | 2010-07-24 16:58:02 +0200 | [diff] [blame] | 464 | b.append(list, nr) Idem, below line "nr" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 465 | b.mark(name) Return a tuple (row,col) representing the position |
| 466 | of the named mark (can also get the []"<> marks) |
| 467 | b.range(s,e) Return a range object (see |python-range|) which |
| 468 | represents the part of the given buffer between line |
| 469 | numbers s and e |inclusive|. |
| 470 | |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 471 | Note that when adding a line it must not contain a line break character '\n'. |
| 472 | A trailing '\n' is allowed and ignored, so that you can do: > |
| 473 | :py b.append(f.readlines()) |
| 474 | |
Bram Moolenaar | cac867a | 2013-05-21 19:50:34 +0200 | [diff] [blame] | 475 | Buffer object type is available using "Buffer" attribute of vim module. |
| 476 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 477 | Examples (assume b is the current buffer) > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 478 | :py print b.name # write the buffer file name |
| 479 | :py b[0] = "hello!!!" # replace the top line |
| 480 | :py b[:] = None # delete the whole buffer |
| 481 | :py del b[:] # delete the whole buffer |
| 482 | :py b[0:0] = [ "a line" ] # add a line at the top |
| 483 | :py del b[2] # delete a line (the third) |
| 484 | :py b.append("bottom") # add a line at the bottom |
| 485 | :py n = len(b) # number of lines |
| 486 | :py (row,col) = b.mark('a') # named mark |
| 487 | :py r = b.range(1,5) # a sub-range of the buffer |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 488 | :py b.vars["foo"] = "bar" # assign b:foo variable |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 489 | :py b.options["ff"] = "dos" # set fileformat |
| 490 | :py del b.options["ar"] # same as :set autoread< |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 491 | |
| 492 | ============================================================================== |
| 493 | 4. Range objects *python-range* |
| 494 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 495 | Range objects represent a part of a vim buffer. You can obtain them in a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 496 | number of ways: |
| 497 | - via vim.current.range (|python-current|) |
| 498 | - from a buffer's range() method (|python-buffer|) |
| 499 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 500 | A range object is almost identical in operation to a buffer object. However, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 501 | all operations are restricted to the lines within the range (this line range |
| 502 | can, of course, change as a result of slice assignments, line deletions, or |
| 503 | the range.append() method). |
| 504 | |
| 505 | The range object attributes are: |
| 506 | r.start Index of first line into the buffer |
| 507 | r.end Index of last line into the buffer |
| 508 | |
| 509 | The range object methods are: |
| 510 | r.append(str) Append a line to the range |
Bram Moolenaar | 2c3b1d9 | 2010-07-24 16:58:02 +0200 | [diff] [blame] | 511 | r.append(str, nr) Idem, after line "nr" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 512 | r.append(list) Append a list of lines to the range |
| 513 | Note that the option of supplying a list of strings to |
| 514 | the append method differs from the equivalent method |
| 515 | for Python's built-in list objects. |
Bram Moolenaar | 2c3b1d9 | 2010-07-24 16:58:02 +0200 | [diff] [blame] | 516 | r.append(list, nr) Idem, after line "nr" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 517 | |
Bram Moolenaar | cac867a | 2013-05-21 19:50:34 +0200 | [diff] [blame] | 518 | Range object type is available using "Range" attribute of vim module. |
| 519 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 520 | Example (assume r is the current range): |
| 521 | # Send all lines in a range to the default printer |
| 522 | vim.command("%d,%dhardcopy!" % (r.start+1,r.end+1)) |
| 523 | |
| 524 | ============================================================================== |
| 525 | 5. Window objects *python-window* |
| 526 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 527 | Window objects represent vim windows. You can obtain them in a number of ways: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 528 | - via vim.current.window (|python-current|) |
| 529 | - from indexing vim.windows (|python-windows|) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 530 | - from indexing "windows" attribute of a tab page (|python-tabpage|) |
| 531 | - from the "window" attribute of a tab page (|python-tabpage|) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 532 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 533 | You can manipulate window objects only through their attributes. They have no |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 534 | methods, and no sequence or other interface. |
| 535 | |
| 536 | Window attributes are: |
| 537 | buffer (read-only) The buffer displayed in this window |
| 538 | cursor (read-write) The current cursor position in the window |
| 539 | This is a tuple, (row,col). |
| 540 | height (read-write) The window height, in rows |
| 541 | width (read-write) The window width, in columns |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 542 | vars (read-only) The window |w:| variables. Attribute is |
| 543 | unassignable, but you can change window |
| 544 | variables this way |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 545 | options (read-only) The window-local options. Attribute is |
| 546 | unassignable, but you can change window |
| 547 | options this way. Provides access only to |
| 548 | window-local options, for buffer-local use |
| 549 | |python-buffer| and for global ones use |
| 550 | |python-options|. If option is |global-local| |
| 551 | and local value is missing getting it will |
| 552 | return None. |
Bram Moolenaar | 6d21645 | 2013-05-12 19:00:41 +0200 | [diff] [blame] | 553 | number (read-only) Window number. The first window has number 1. |
| 554 | This is zero in case it cannot be determined |
| 555 | (e.g. when the window object belongs to other |
| 556 | tab page). |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 557 | row, col (read-only) On-screen window position in display cells. |
Bram Moolenaar | 4e5dfb5 | 2013-05-12 19:30:31 +0200 | [diff] [blame] | 558 | First position is zero. |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 559 | tabpage (read-only) Window tab page. |
Bram Moolenaar | 203d04d | 2013-06-06 21:36:40 +0200 | [diff] [blame] | 560 | valid (read-write) True or False. Window object becomes invalid |
Bram Moolenaar | bc41196 | 2013-06-02 17:46:40 +0200 | [diff] [blame] | 561 | when corresponding window is closed. |
Bram Moolenaar | 4e5dfb5 | 2013-05-12 19:30:31 +0200 | [diff] [blame] | 562 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 563 | The height attribute is writable only if the screen is split horizontally. |
| 564 | The width attribute is writable only if the screen is split vertically. |
| 565 | |
Bram Moolenaar | cac867a | 2013-05-21 19:50:34 +0200 | [diff] [blame] | 566 | Window object type is available using "Window" attribute of vim module. |
| 567 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 568 | ============================================================================== |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 569 | 6. Tab page objects *python-tabpage* |
| 570 | |
| 571 | Tab page objects represent vim tab pages. You can obtain them in a number of |
| 572 | ways: |
| 573 | - via vim.current.tabpage (|python-current|) |
| 574 | - from indexing vim.tabpages (|python-tabpages|) |
| 575 | |
| 576 | You can use this object to access tab page windows. They have no methods and |
| 577 | no sequence or other interfaces. |
| 578 | |
| 579 | Tab page attributes are: |
| 580 | number The tab page number like the one returned by |
| 581 | |tabpagenr()|. |
| 582 | windows Like |python-windows|, but for current tab page. |
| 583 | vars The tab page |t:| variables. |
| 584 | window Current tabpage window. |
Bram Moolenaar | 203d04d | 2013-06-06 21:36:40 +0200 | [diff] [blame] | 585 | valid True or False. Tab page object becomes invalid when |
Bram Moolenaar | bc41196 | 2013-06-02 17:46:40 +0200 | [diff] [blame] | 586 | corresponding tab page is closed. |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 587 | |
Bram Moolenaar | cac867a | 2013-05-21 19:50:34 +0200 | [diff] [blame] | 588 | TabPage object type is available using "TabPage" attribute of vim module. |
| 589 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 590 | ============================================================================== |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 591 | 7. vim.bindeval objects *python-bindeval-objects* |
| 592 | |
| 593 | vim.Dictionary object *python-Dictionary* |
| 594 | Dictionary-like object providing access to vim |Dictionary| type. |
| 595 | Attributes: |
| 596 | Attribute Description ~ |
| 597 | locked One of *python-.locked* |
| 598 | Value Description ~ |
| 599 | zero Variable is not locked |
| 600 | vim.VAR_LOCKED Variable is locked, but can be unlocked |
| 601 | vim.VAR_FIXED Variable is locked and can't be unlocked |
| 602 | Read-write. You can unlock locked variable by assigning |
| 603 | `True` or `False` to this attribute. No recursive locking |
| 604 | is supported. |
| 605 | scope One of |
| 606 | Value Description ~ |
| 607 | zero Dictionary is not a scope one |
| 608 | vim.VAR_DEF_SCOPE |g:| or |l:| dictionary |
| 609 | vim.VAR_SCOPE Other scope dictionary, |
| 610 | see |internal-variables| |
Bram Moolenaar | 305b2fd | 2013-05-30 13:32:30 +0200 | [diff] [blame] | 611 | Methods (note: methods do not support keyword arguments): |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 612 | Method Description ~ |
| 613 | keys() Returns a list with dictionary keys. |
| 614 | values() Returns a list with dictionary values. |
| 615 | items() Returns a list of 2-tuples with dictionary contents. |
Bram Moolenaar | 305b2fd | 2013-05-30 13:32:30 +0200 | [diff] [blame] | 616 | update(iterable), update(dictionary), update(**kwargs) |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 617 | Adds keys to dictionary. |
Bram Moolenaar | 305b2fd | 2013-05-30 13:32:30 +0200 | [diff] [blame] | 618 | get(key[, default=None]) |
| 619 | Obtain key from dictionary, returning the default if it is |
| 620 | not present. |
| 621 | pop(key[, default]) |
| 622 | Remove specified key from dictionary and return |
| 623 | corresponding value. If key is not found and default is |
| 624 | given returns the default, otherwise raises KeyError. |
Bram Moolenaar | de71b56 | 2013-06-02 17:41:54 +0200 | [diff] [blame] | 625 | popitem() |
| 626 | Remove random key from dictionary and return (key, value) |
| 627 | pair. |
Bram Moolenaar | 305b2fd | 2013-05-30 13:32:30 +0200 | [diff] [blame] | 628 | has_key(key) |
| 629 | Check whether dictionary contains specified key, similar |
| 630 | to `key in dict`. |
| 631 | |
| 632 | __new__(), __new__(iterable), __new__(dictionary), __new__(update) |
| 633 | You can use `vim.Dictionary()` to create new vim |
| 634 | dictionaries. `d=vim.Dictionary(arg)` is the same as |
| 635 | `d=vim.bindeval('{}');d.update(arg)`. Without arguments |
| 636 | constructs empty dictionary. |
| 637 | |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 638 | Examples: > |
Bram Moolenaar | 305b2fd | 2013-05-30 13:32:30 +0200 | [diff] [blame] | 639 | d = vim.Dictionary(food="bar") # Constructor |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 640 | d['a'] = 'b' # Item assignment |
| 641 | print d['a'] # getting item |
| 642 | d.update({'c': 'd'}) # .update(dictionary) |
| 643 | d.update(e='f') # .update(**kwargs) |
| 644 | d.update((('g', 'h'), ('i', 'j'))) # .update(iterable) |
| 645 | for key in d.keys(): # .keys() |
| 646 | for val in d.values(): # .values() |
| 647 | for key, val in d.items(): # .items() |
| 648 | print isinstance(d, vim.Dictionary) # True |
| 649 | for key in d: # Iteration over keys |
Bram Moolenaar | 305b2fd | 2013-05-30 13:32:30 +0200 | [diff] [blame] | 650 | class Dict(vim.Dictionary): # Subclassing |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 651 | < |
| 652 | Note: when iterating over keys you should not modify dictionary. |
| 653 | |
| 654 | vim.List object *python-List* |
| 655 | Sequence-like object providing access to vim |List| type. |
| 656 | Supports `.locked` attribute, see |python-.locked|. Also supports the |
| 657 | following methods: |
| 658 | Method Description ~ |
| 659 | extend(item) Add items to the list. |
Bram Moolenaar | 305b2fd | 2013-05-30 13:32:30 +0200 | [diff] [blame] | 660 | |
| 661 | __new__(), __new__(iterable) |
| 662 | You can use `vim.List()` to create new vim lists. |
| 663 | `l=vim.List(iterable)` is the same as |
| 664 | `l=vim.bindeval('[]');l.extend(iterable)`. Without |
| 665 | arguments constructs empty list. |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 666 | Examples: > |
Bram Moolenaar | 305b2fd | 2013-05-30 13:32:30 +0200 | [diff] [blame] | 667 | l = vim.List("abc") # Constructor, result: ['a', 'b', 'c'] |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 668 | l.extend(['abc', 'def']) # .extend() method |
| 669 | print l[1:] # slicing |
| 670 | l[:0] = ['ghi', 'jkl'] # slice assignment |
| 671 | print l[0] # getting item |
| 672 | l[0] = 'mno' # assignment |
| 673 | for i in l: # iteration |
| 674 | print isinstance(l, vim.List) # True |
Bram Moolenaar | 305b2fd | 2013-05-30 13:32:30 +0200 | [diff] [blame] | 675 | class List(vim.List): # Subclassing |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 676 | |
| 677 | vim.Function object *python-Function* |
Bram Moolenaar | 8110a09 | 2016-04-14 15:56:09 +0200 | [diff] [blame] | 678 | Function-like object, acting like vim |Funcref| object. Accepts special |
| 679 | keyword argument `self`, see |Dictionary-function|. You can also use |
| 680 | `vim.Function(name)` constructor, it is the same as |
| 681 | `vim.bindeval('function(%s)'%json.dumps(name))`. |
| 682 | |
| 683 | Attributes (read-only): |
Bram Moolenaar | 2177f9f | 2016-05-25 20:39:09 +0200 | [diff] [blame] | 684 | Attribute Description ~ |
| 685 | name Function name. |
| 686 | args `None` or a |python-List| object with arguments. Note |
| 687 | that this is a copy of the arguments list, constructed |
| 688 | each time you request this attribute. Modifications made |
| 689 | to the list will be ignored (but not to the containers |
| 690 | inside argument list: this is like |copy()| and not |
| 691 | |deepcopy()|). |
| 692 | self `None` or a |python-Dictionary| object with self |
| 693 | dictionary. Note that explicit `self` keyword used when |
| 694 | calling resulting object overrides this attribute. |
| 695 | auto_rebind Boolean. True if partial created from this Python object |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 696 | and stored in the Vim script dictionary should be |
| 697 | automatically rebound to the dictionary it is stored in |
| 698 | when this dictionary is indexed. Exposes Vim internal |
| 699 | difference between `dict.func` (auto_rebind=True) and |
| 700 | `function(dict.func,dict)` (auto_rebind=False). This |
Bram Moolenaar | 2177f9f | 2016-05-25 20:39:09 +0200 | [diff] [blame] | 701 | attribute makes no sense if `self` attribute is `None`. |
Bram Moolenaar | 8110a09 | 2016-04-14 15:56:09 +0200 | [diff] [blame] | 702 | |
Bram Moolenaar | 2177f9f | 2016-05-25 20:39:09 +0200 | [diff] [blame] | 703 | Constructor additionally accepts `args`, `self` and `auto_rebind` |
| 704 | keywords. If `args` and/or `self` argument is given then it constructs |
| 705 | a partial, see |function()|. `auto_rebind` is only used when `self` |
| 706 | argument is given, otherwise it is assumed to be `True` regardless of |
| 707 | whether it was given or not. If `self` is given then it defaults to |
| 708 | `False`. |
Bram Moolenaar | 305b2fd | 2013-05-30 13:32:30 +0200 | [diff] [blame] | 709 | |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 710 | Examples: > |
Bram Moolenaar | 305b2fd | 2013-05-30 13:32:30 +0200 | [diff] [blame] | 711 | f = vim.Function('tr') # Constructor |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 712 | print f('abc', 'a', 'b') # Calls tr('abc', 'a', 'b') |
| 713 | vim.command(''' |
| 714 | function DictFun() dict |
| 715 | return self |
| 716 | endfunction |
| 717 | ''') |
| 718 | f = vim.bindeval('function("DictFun")') |
| 719 | print f(self={}) # Like call('DictFun', [], {}) |
| 720 | print isinstance(f, vim.Function) # True |
| 721 | |
Bram Moolenaar | 8110a09 | 2016-04-14 15:56:09 +0200 | [diff] [blame] | 722 | p = vim.Function('DictFun', self={}) |
| 723 | print f() |
| 724 | p = vim.Function('tr', args=['abc', 'a']) |
| 725 | print f('b') |
| 726 | |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 727 | ============================================================================== |
| 728 | 8. pyeval() and py3eval() Vim functions *python-pyeval* |
Bram Moolenaar | 30b6581 | 2012-07-12 22:01:11 +0200 | [diff] [blame] | 729 | |
| 730 | To facilitate bi-directional interface, you can use |pyeval()| and |py3eval()| |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 731 | functions to evaluate Python expressions and pass their values to Vim script. |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 732 | |pyxeval()| is also available. |
Bram Moolenaar | 30b6581 | 2012-07-12 22:01:11 +0200 | [diff] [blame] | 733 | |
Bram Moolenaar | de32309 | 2017-11-09 19:56:08 +0100 | [diff] [blame] | 734 | The Python value "None" is converted to v:none. |
| 735 | |
Bram Moolenaar | 30b6581 | 2012-07-12 22:01:11 +0200 | [diff] [blame] | 736 | ============================================================================== |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 737 | 9. Dynamic loading *python-dynamic* |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 738 | |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 739 | On MS-Windows and Unix the Python library can be loaded dynamically. The |
| 740 | |:version| output then includes |+python/dyn| or |+python3/dyn|. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 741 | |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 742 | This means that Vim will search for the Python DLL or shared library file only |
| 743 | when needed. When you don't use the Python interface you don't need it, thus |
| 744 | you can use Vim without this file. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 745 | |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 746 | |
| 747 | MS-Windows ~ |
| 748 | |
| 749 | To use the Python interface the Python DLL must be in your search path. In a |
| 750 | console window type "path" to see what directories are used. The 'pythondll' |
| 751 | or 'pythonthreedll' option can be also used to specify the Python DLL. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 752 | |
Bram Moolenaar | 3df0173 | 2017-02-17 22:47:16 +0100 | [diff] [blame] | 753 | The name of the DLL should match the Python version Vim was compiled with. |
| 754 | Currently the name for Python 2 is "python27.dll", that is for Python 2.7. |
Bram Moolenaar | 59eb016 | 2017-12-10 18:17:44 +0100 | [diff] [blame] | 755 | That is the default value for 'pythondll'. For Python 3 it is python36.dll |
| 756 | (Python 3.6). To know for sure edit "gvim.exe" and search for |
Bram Moolenaar | 3df0173 | 2017-02-17 22:47:16 +0100 | [diff] [blame] | 757 | "python\d*.dll\c". |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 758 | |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 759 | |
| 760 | Unix ~ |
| 761 | |
| 762 | The 'pythondll' or 'pythonthreedll' option can be used to specify the Python |
| 763 | shared library file instead of DYNAMIC_PYTHON_DLL or DYNAMIC_PYTHON3_DLL file |
| 764 | what were specified at compile time. The version of the shared library must |
| 765 | match the Python 2.x or Python 3 version Vim was compiled with. |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 766 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 767 | ============================================================================== |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 768 | 10. Python 3 *python3* |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 769 | |
Bram Moolenaar | bfc8b97 | 2010-08-13 22:05:54 +0200 | [diff] [blame] | 770 | *:py3* *:python3* |
Bram Moolenaar | d620aa9 | 2013-05-17 16:40:06 +0200 | [diff] [blame] | 771 | The `:py3` and `:python3` commands work similar to `:python`. A simple check |
Bram Moolenaar | fa13eef | 2013-02-06 17:34:04 +0100 | [diff] [blame] | 772 | if the `:py3` command is working: > |
Bram Moolenaar | 9b45125 | 2012-08-15 17:43:31 +0200 | [diff] [blame] | 773 | :py3 print("Hello") |
Bram Moolenaar | 50ba526 | 2016-09-22 22:33:02 +0200 | [diff] [blame] | 774 | |
Bram Moolenaar | 64d8e25 | 2016-09-06 22:12:34 +0200 | [diff] [blame] | 775 | To see what version of Python you have: > |
| 776 | :py3 import sys |
| 777 | :py3 print(sys.version) |
Bram Moolenaar | 50ba526 | 2016-09-22 22:33:02 +0200 | [diff] [blame] | 778 | < *:py3file* |
Bram Moolenaar | d620aa9 | 2013-05-17 16:40:06 +0200 | [diff] [blame] | 779 | The `:py3file` command works similar to `:pyfile`. |
Bram Moolenaar | aa3b15d | 2016-04-21 08:53:19 +0200 | [diff] [blame] | 780 | *:py3do* |
Bram Moolenaar | d620aa9 | 2013-05-17 16:40:06 +0200 | [diff] [blame] | 781 | The `:py3do` command works similar to `:pydo`. |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 782 | |
Bram Moolenaar | 30b6581 | 2012-07-12 22:01:11 +0200 | [diff] [blame] | 783 | |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 784 | Vim can be built in four ways (:version output): |
Bram Moolenaar | bfc8b97 | 2010-08-13 22:05:54 +0200 | [diff] [blame] | 785 | 1. No Python support (-python, -python3) |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 786 | 2. Python 2 support only (+python or +python/dyn, -python3) |
| 787 | 3. Python 3 support only (-python, +python3 or +python3/dyn) |
| 788 | 4. Python 2 and 3 support (+python/dyn, +python3/dyn) |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 789 | |
Bram Moolenaar | 9ba7e17 | 2013-07-17 22:37:26 +0200 | [diff] [blame] | 790 | Some more details on the special case 4: *python-2-and-3* |
Bram Moolenaar | ede981a | 2010-08-11 23:37:32 +0200 | [diff] [blame] | 791 | |
Bram Moolenaar | bfc8b97 | 2010-08-13 22:05:54 +0200 | [diff] [blame] | 792 | When Python 2 and Python 3 are both supported they must be loaded dynamically. |
| 793 | |
| 794 | When doing this on Linux/Unix systems and importing global symbols, this leads |
| 795 | to a crash when the second Python version is used. So either global symbols |
| 796 | are loaded but only one Python version is activated, or no global symbols are |
Bram Moolenaar | 483c5d8 | 2010-10-20 18:45:33 +0200 | [diff] [blame] | 797 | loaded. The latter makes Python's "import" fail on libraries that expect the |
Bram Moolenaar | bfc8b97 | 2010-08-13 22:05:54 +0200 | [diff] [blame] | 798 | symbols to be provided by Vim. |
| 799 | *E836* *E837* |
| 800 | Vim's configuration script makes a guess for all libraries based on one |
| 801 | standard Python library (termios). If importing this library succeeds for |
| 802 | both Python versions, then both will be made available in Vim at the same |
| 803 | time. If not, only the version first used in a session will be enabled. |
| 804 | When trying to use the other one you will get the E836 or E837 error message. |
| 805 | |
| 806 | Here Vim's behavior depends on the system in which it was configured. In a |
| 807 | system where both versions of Python were configured with --enable-shared, |
| 808 | both versions of Python will be activated at the same time. There will still |
| 809 | be problems with other third party libraries that were not linked to |
| 810 | libPython. |
| 811 | |
| 812 | To work around such problems there are these options: |
| 813 | 1. The problematic library is recompiled to link to the according |
| 814 | libpython.so. |
| 815 | 2. Vim is recompiled for only one Python version. |
| 816 | 3. You undefine PY_NO_RTLD_GLOBAL in auto/config.h after configuration. This |
| 817 | may crash Vim though. |
| 818 | |
Bram Moolenaar | 4100937 | 2013-07-01 22:03:04 +0200 | [diff] [blame] | 819 | *E880* |
| 820 | Raising SystemExit exception in python isn't endorsed way to quit vim, use: > |
| 821 | :py vim.command("qall!") |
| 822 | < |
| 823 | |
Bram Moolenaar | 446beb4 | 2011-05-10 17:18:44 +0200 | [diff] [blame] | 824 | *has-python* |
| 825 | You can test what Python version is available with: > |
| 826 | if has('python') |
Bram Moolenaar | 5302d9e | 2011-09-14 17:55:08 +0200 | [diff] [blame] | 827 | echo 'there is Python 2.x' |
Bram Moolenaar | 40962ec | 2018-01-28 22:47:25 +0100 | [diff] [blame] | 828 | endif |
| 829 | if has('python3') |
Bram Moolenaar | 446beb4 | 2011-05-10 17:18:44 +0200 | [diff] [blame] | 830 | echo 'there is Python 3.x' |
| 831 | endif |
| 832 | |
| 833 | Note however, that when Python 2 and 3 are both available and loaded |
| 834 | dynamically, these has() calls will try to load them. If only one can be |
| 835 | loaded at a time, just checking if Python 2 or 3 are available will prevent |
| 836 | the other one from being available. |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 837 | |
Bram Moolenaar | 40962ec | 2018-01-28 22:47:25 +0100 | [diff] [blame] | 838 | To avoid loading the dynamic library, only check if Vim was compiled with |
| 839 | python support: > |
| 840 | if has('python_compiled') |
| 841 | echo 'compiled with Python 2.x support' |
Bram Moolenaar | 7254067 | 2018-02-09 22:00:53 +0100 | [diff] [blame] | 842 | if has('python_dynamic') |
| 843 | echo 'Python 2.x dynamically loaded' |
Bram Moolenaar | 40962ec | 2018-01-28 22:47:25 +0100 | [diff] [blame] | 844 | endif |
| 845 | endif |
| 846 | if has('python3_compiled') |
| 847 | echo 'compiled with Python 3.x support' |
Bram Moolenaar | 7254067 | 2018-02-09 22:00:53 +0100 | [diff] [blame] | 848 | if has('python3_dynamic') |
| 849 | echo 'Python 3.x dynamically loaded' |
Bram Moolenaar | 40962ec | 2018-01-28 22:47:25 +0100 | [diff] [blame] | 850 | endif |
| 851 | endif |
| 852 | |
| 853 | This also tells you whether Python is dynamically loaded, which will fail if |
| 854 | the runtime library cannot be found. |
| 855 | |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 856 | ============================================================================== |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 857 | 11. Python X *python_x* *pythonx* |
| 858 | |
| 859 | Because most python code can be written so that it works with python 2.6+ and |
Bram Moolenaar | 214641f | 2017-03-05 17:04:09 +0100 | [diff] [blame] | 860 | python 3 the pyx* functions and commands have been written. They work exactly |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 861 | the same as the Python 2 and 3 variants, but select the Python version using |
| 862 | the 'pyxversion' setting. |
| 863 | |
| 864 | You should set 'pyxversion' in your |.vimrc| to prefer Python 2 or Python 3 |
| 865 | for Python commands. If you change this setting at runtime you may risk that |
| 866 | state of plugins (such as initialization) may be lost. |
| 867 | |
| 868 | If you want to use a module, you can put it in the {rtp}/pythonx directory. |
| 869 | See |pythonx-directory|. |
| 870 | |
| 871 | *:pyx* *:pythonx* |
| 872 | The `:pyx` and `:pythonx` commands work similar to `:python`. A simple check |
| 873 | if the `:pyx` command is working: > |
| 874 | :pyx print("Hello") |
| 875 | |
| 876 | To see what version of Python is being used: > |
| 877 | :pyx import sys |
| 878 | :pyx print(sys.version) |
| 879 | < |
| 880 | *:pyxfile* *python_x-special-comments* |
| 881 | The `:pyxfile` command works similar to `:pyfile`. However you can add one of |
| 882 | these comments to force Vim using `:pyfile` or `:py3file`: > |
| 883 | #!/any string/python2 " Shebang. Must be the first line of the file. |
| 884 | #!/any string/python3 " Shebang. Must be the first line of the file. |
| 885 | # requires python 2.x " Maximum lines depend on 'modelines'. |
| 886 | # requires python 3.x " Maximum lines depend on 'modelines'. |
| 887 | Unlike normal modelines, the bottom of the file is not checked. |
| 888 | If none of them are found, the 'pyxversion' setting is used. |
| 889 | *W20* *W21* |
| 890 | If Vim does not support the selected Python version a silent message will be |
| 891 | printed. Use `:messages` to read them. |
| 892 | |
| 893 | *:pyxdo* |
| 894 | The `:pyxdo` command works similar to `:pydo`. |
| 895 | |
| 896 | *has-pythonx* |
| 897 | You can test if pyx* commands are available with: > |
| 898 | if has('pythonx') |
| 899 | echo 'pyx* commands are available. (Python ' . &pyx . ')' |
| 900 | endif |
| 901 | |
| 902 | When compiled with only one of |+python| or |+python3|, the has() returns 1. |
| 903 | When compiled with both |+python| and |+python3|, the test depends on the |
| 904 | 'pyxversion' setting. If 'pyxversion' is 0, it tests Python 3 first, and if |
| 905 | it is not available then Python 2. If 'pyxversion' is 2 or 3, it tests only |
| 906 | Python 2 or 3 respectively. |
| 907 | |
Bram Moolenaar | 214641f | 2017-03-05 17:04:09 +0100 | [diff] [blame] | 908 | Note that for `has('pythonx')` to work it may try to dynamically load Python 3 |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 909 | or 2. This may have side effects, especially when Vim can only load one of |
| 910 | the two. |
| 911 | |
| 912 | If a user prefers Python 2 and want to fallback to Python 3, he needs to set |
| 913 | 'pyxversion' explicitly in his |.vimrc|. E.g.: > |
| 914 | if has('python') |
| 915 | set pyx=2 |
| 916 | elseif has('python3') |
| 917 | set pyx=3 |
| 918 | endif |
| 919 | |
| 920 | ============================================================================== |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 921 | 12. Building with Python support *python-building* |
| 922 | |
| 923 | A few hints for building with Python 2 or 3 support. |
| 924 | |
| 925 | UNIX |
| 926 | |
| 927 | See src/Makefile for how to enable including the Python interface. |
| 928 | |
| 929 | On Ubuntu you will want to install these packages for Python 2: |
| 930 | python |
| 931 | python-dev |
| 932 | For Python 3: |
| 933 | python3 |
Bram Moolenaar | 1ccd8ff | 2017-08-11 19:50:37 +0200 | [diff] [blame] | 934 | python3-dev |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 935 | For Python 3.6: |
| 936 | python3.6 |
Bram Moolenaar | 1ccd8ff | 2017-08-11 19:50:37 +0200 | [diff] [blame] | 937 | python3.6-dev |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 938 | |
| 939 | If you have more than one version of Python 3, you need to link python3 to the |
| 940 | one you prefer, before running configure. |
| 941 | |
| 942 | ============================================================================== |
Bram Moolenaar | 91f84f6 | 2018-07-29 15:07:52 +0200 | [diff] [blame] | 943 | vim:tw=78:ts=8:noet:ft=help:norl: |