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