annotate runtime/doc/if_pyth.txt @ 18:293621502c4d v7.0010

updated for version 7.0010
author vimboss
date Sun, 18 Jul 2004 21:34:53 +0000
parents 3fc0f57ecb91
children 4ac1dce8dd5e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 *if_pyth.txt* For Vim version 7.0aa. Last change: 2004 Feb 28
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4 VIM REFERENCE MANUAL by Paul Moore
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 The Python Interface to Vim *python* *Python*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 1. Commands |python-commands|
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 2. The vim module |python-vim|
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 3. Buffer objects |python-buffer|
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 4. Range objects |python-range|
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 5. Window objects |python-window|
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 {Vi does not have any of these commands}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 The Python interface is available only when Vim was compiled with the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 |+python| feature.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 1. Commands *python-commands*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 *:python* *:py* *E205* *E263* *E264*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 :[range]py[thon] {stmt}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 Execute Python statement {stmt}.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 :[range]py[thon] << {endmarker}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 {script}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 {endmarker}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 Execute Python script {script}.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 Note: This command doesn't work when the Python
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32 feature wasn't compiled in. To avoid errors, see
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 |script-here|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 {endmarker} must NOT be preceded by any white space. If {endmarker} is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 omitted from after the "<<", a dot '.' must be used after {script}, like
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 for the |:append| and |:insert| commands.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 This form of the |:python| command is mainly useful for including python code
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 in Vim scripts.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 Example: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 function! IcecreamInitialize()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 python << EOF
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 class StrawberryIcecream:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 def __call__(self):
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 print 'EAT ME'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 EOF
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 endfunction
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 <
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 Note: Python is very sensitive to the indenting. Also make sure the "class"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 line and "EOF" do not have any indent.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 *:pyfile* *:pyf*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 :[range]pyf[ile] {file}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 Execute the Python script in {file}. The whole
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 argument is used as a single file name. {not in Vi}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 Both of these commands do essentially the same thing - they execute a piece of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 Python code, with the "current range" |python-range| set to the given line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 range.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 In the case of :python, the code to execute is in the command-line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 In the case of :pyfile, the code to execute is the contents of the given file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 Python commands cannot be used in the |sandbox|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 To pass arguments you need to set sys.argv[] explicitly. Example: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 :python import sys
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 :python sys.argv = ["foo", "bar"]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 :pyfile myscript.py
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 Here are some examples *python-examples* >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 :python from vim import *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 :python from string import upper
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 :python current.line = upper(current.line)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 :python print "Hello"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 :python str = current.buffer[42]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 (Note that changes - like the imports - persist from one command to the next,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 just like in the Python interpreter.)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 2. The vim module *python-vim*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 Python code gets all of its access to vim (with one exception - see
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 |python-output| below) via the "vim" module. The vim module implements two
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 methods, three constants, and one error object. You need to import the vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 module before using it: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 :python import vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93 Overview >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 print "Hello" # displays a message
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 vim.command(cmd) # execute an ex command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96 w = vim.windows[n] # gets window "n"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 cw = vim.current.window # gets the current window
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 b = vim.buffers[n] # gets buffer "n"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 cb = vim.current.buffer # gets the current buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100 w.height = lines # sets the window height
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 w.cursor = (row, col) # sets the window cursor position
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102 pos = w.cursor # gets a tuple (row, col)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103 name = b.name # gets the buffer file name
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104 line = b[n] # gets a line from the buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 lines = b[n:m] # gets a list of lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 num = len(b) # gets the number of lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 b[n] = str # sets a line in the buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 b[n:m] = [str1, str2, str3] # sets a number of lines at once
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 del b[n] # deletes a line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 del b[n:m] # deletes a number of lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 Methods of the "vim" module
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 vim.command(str) *python-command*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 Executes the vim (ex-mode) command str. Returns None.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 Examples: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 vim.command("set tw=72")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 vim.command("%s/aaa/bbb/g")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 < The following definition executes Normal mode commands: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 def normal(str):
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 vim.command("normal "+str)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 # Note the use of single quotes to delimit a string containing
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 # double quotes
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 normal('"a2dd"aP')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 < *E659*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 The ":python" command cannot be used recursively with Python 2.2 and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 older. This only works with Python 2.3 and later: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 :python vim.command("python print 'Hello again Python'")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131 vim.eval(str) *python-eval*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 Evaluates the expression str using the vim internal expression
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 evaluator (see |expression|). Returns the expression result as a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 string.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 Examples: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136 text_width = vim.eval("&tw")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137 str = vim.eval("12+12") # NB result is a string! Use
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138 # string.atoi() to convert to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 # a number.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141 Error object of the "vim" module
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143 vim.error *python-error*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 Upon encountering a Vim error, Python raises an exception of type
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145 vim.error.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 Example: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147 try:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148 vim.command("put a")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149 except vim.error:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 # nothing in register a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 Constants of the "vim" module
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
154 Note that these are not actually constants - you could reassign them.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155 But this is silly, as you would then lose access to the vim objects
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156 to which the variables referred.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158 vim.buffers *python-buffers*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 A sequence object providing access to the list of vim buffers. The
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
160 object supports the following operations: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161 b = vim.buffers[i] # Indexing (read-only)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
162 b in vim.buffers # Membership test
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
163 n = len(vim.buffers) # Number of elements
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
164 for b in vim.buffers: # Sequential access
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
165 <
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
166 vim.windows *python-windows*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
167 A sequence object providing access to the list of vim windows. The
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
168 object supports the following operations: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
169 w = vim.windows[i] # Indexing (read-only)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
170 w in vim.windows # Membership test
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
171 n = len(vim.windows) # Number of elements
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
172 for w in vim.windows: # Sequential access
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
173 <
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
174 vim.current *python-current*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
175 An object providing access (via specific attributes) to various
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
176 "current" objects available in vim:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
177 vim.current.line The current line (RW) String
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
178 vim.current.buffer The current buffer (RO) Buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
179 vim.current.window The current window (RO) Window
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
180 vim.current.range The current line range (RO) Range
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
181
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
182 The last case deserves a little explanation. When the :python or
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
183 :pyfile command specifies a range, this range of lines becomes the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
184 "current range". A range is a bit like a buffer, but with all access
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
185 restricted to a subset of lines. See |python-range| for more details.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
186
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
187
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
188 Output from Python *python-output*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
189 Vim displays all Python code output in the Vim message area. Normal
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
190 output appears as information messages, and error output appears as
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
191 error messages.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
192
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
193 In implementation terms, this means that all output to sys.stdout
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
194 (including the output from print statements) appears as information
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
195 messages, and all output to sys.stderr (including error tracebacks)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
196 appears as error messages.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
197
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
198 *python-input*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
199 Input (via sys.stdin, including input() and raw_input()) is not
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
200 supported, and may cause the program to crash. This should probably be
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
201 fixed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
202
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
203 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
204 3. Buffer objects *python-buffer*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
205
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
206 Buffer objects represent vim buffers. You can obtain them in a number of ways:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
207 - via vim.current.buffer (|python-current|)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
208 - from indexing vim.buffers (|python-buffers|)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
209 - from the "buffer" attribute of a window (|python-window|)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
210
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
211 Buffer objects have one read-only attribute - name - the full file name for
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
212 the buffer. They also have three methods (append, mark, and range; see below).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
213
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
214 You can also treat buffer objects as sequence objects. In this context, they
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
215 act as if they were lists (yes, they are mutable) of strings, with each
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
216 element being a line of the buffer. All of the usual sequence operations,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
217 including indexing, index assignment, slicing and slice assignment, work as
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
218 you would expect. Note that the result of indexing (slicing) a buffer is a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
219 string (list of strings). This has one unusual consequence - b[:] is different
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
220 from b. In particular, "b[:] = None" deletes the whole of the buffer, whereas
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
221 "b = None" merely updates the variable b, with no effect on the buffer.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
222
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
223 Buffer indexes start at zero, as is normal in Python. This differs from vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
224 line numbers, which start from 1. This is particularly relevant when dealing
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
225 with marks (see below) which use vim line numbers.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
226
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
227 The buffer object methods are:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
228 b.append(str) Append a line to the buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
229 b.append(list) Append a list of lines to the buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
230 Note that the option of supplying a list of strings to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
231 the append method differs from the equivalent method
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
232 for Python's built-in list objects.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
233 b.mark(name) Return a tuple (row,col) representing the position
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
234 of the named mark (can also get the []"<> marks)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
235 b.range(s,e) Return a range object (see |python-range|) which
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
236 represents the part of the given buffer between line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
237 numbers s and e |inclusive|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
238
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
239 Examples (assume b is the current buffer) >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
240 print b.name # write the buffer file name
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
241 b[0] = "hello!!!" # replace the top line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
242 b[:] = None # delete the whole buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
243 del b[:] # delete the whole buffer (same as above)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
244 b[0:0] = [ "a line" ] # add a line at the top
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
245 del b[2] # delete a line (the third)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
246 b.append("bottom") # add a line at the bottom
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
247 n = len(b) # number of lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
248 (row,col) = b.mark('a') # named mark
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
249 r = b.range(1,5) # a sub-range of the buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
250
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
251 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
252 4. Range objects *python-range*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
253
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
254 Range objects represent a part of a vim buffer. You can obtain them in a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
255 number of ways:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
256 - via vim.current.range (|python-current|)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
257 - from a buffer's range() method (|python-buffer|)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
258
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
259 A range object is almost identical in operation to a buffer object. However,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
260 all operations are restricted to the lines within the range (this line range
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
261 can, of course, change as a result of slice assignments, line deletions, or
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
262 the range.append() method).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
263
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
264 The range object attributes are:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
265 r.start Index of first line into the buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
266 r.end Index of last line into the buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
267
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
268 The range object methods are:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
269 r.append(str) Append a line to the range
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
270 r.append(list) Append a list of lines to the range
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
271 Note that the option of supplying a list of strings to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
272 the append method differs from the equivalent method
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
273 for Python's built-in list objects.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
274
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
275 Example (assume r is the current range):
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
276 # Send all lines in a range to the default printer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
277 vim.command("%d,%dhardcopy!" % (r.start+1,r.end+1))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
278
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
279 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
280 5. Window objects *python-window*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
281
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
282 Window objects represent vim windows. You can obtain them in a number of ways:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
283 - via vim.current.window (|python-current|)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
284 - from indexing vim.windows (|python-windows|)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
285
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
286 You can manipulate window objects only through their attributes. They have no
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
287 methods, and no sequence or other interface.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
288
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
289 Window attributes are:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
290 buffer (read-only) The buffer displayed in this window
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
291 cursor (read-write) The current cursor position in the window
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
292 This is a tuple, (row,col).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
293 height (read-write) The window height, in rows
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
294 width (read-write) The window width, in columns
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
295 The height attribute is writable only if the screen is split horizontally.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
296 The width attribute is writable only if the screen is split vertically.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
297
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
298 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
299 vim:tw=78:ts=8:ft=help:norl: