home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vim53os2.zip / vim-5.3 / doc / if_python.txt < prev    next >
Text File  |  1998-08-30  |  10KB  |  242 lines

  1. *if_python.txt* For Vim version 5.3.  Last modification: 1998 May 10
  2.  
  3.           VIM REFERENCE MANUAL    by Paul Moore
  4.  
  5. The Python Interface to Vim                *python*
  6.  
  7. 1. Commands            |python-commands|
  8. 2. The vim module        |python-vim|
  9. 3. Buffer objects        |python-buffer|
  10. 4. Range objects        |python-range|
  11. 5. Window objects        |python-window|
  12.  
  13. {Vi does not have any of these commands}
  14.  
  15. The Python interface only works when Vim was compiled with the |+python|
  16. feature.
  17.  
  18. ==============================================================================
  19. 1. Commands                        *python-commands*
  20.  
  21.                             *:python* *:py*
  22. :[range]py[thon] {stmt}    Execute Python statement {stmt}.  {not in Vi}
  23.  
  24.                             *:pyfile* *:pyf*
  25. :[range]pyf[ile] {file}    Execute the Python script in {file}.  {not in Vi}
  26.  
  27. Both of these commands do essentially the same thing - they execute a piece of
  28. Python code, with the "current range" |python-range| set to the given line
  29. range.
  30.  
  31. In the case of :python, the code to execute is specified on the command-line.
  32. In the case of :pyfile, the code to execute is the contents of the given file.
  33.  
  34. Here are some examples                    *python-examples*
  35.  
  36. >    :python from vim import *
  37. >    :python from string import upper
  38. >    :python current.line = upper(current.line)
  39. >    :python print "Hello"
  40. >    :python str = current.buffer[42]
  41.  
  42. (Note that changes - like the imports - persist from one command to the next,
  43. just like in the Python interpreter).
  44.  
  45. ==============================================================================
  46. 2. The vim module                    *python-vim*
  47.  
  48. Python code gets all of its access to vim (with one exception - see
  49. |python-output| below) via the "vim" module. The vim module implements two
  50. methods, three constants, and one error object.
  51.  
  52. Overview
  53. >    print "Hello"            # displays a message
  54. >    vim.command(cmd)        # execute an ex command
  55. >    w = vim.windows[n]        # gets window "n"
  56. >    cw = vim.current.window    # gets the current window
  57. >    b = vim.buffers[n]        # gets buffer "n"
  58. >    cb = vim.current.buffer    # gets the current buffer
  59. >    w.height = lines        # sets the window height
  60. >    w.cursor = (row, col)        # sets the window cursor position
  61. >    pos = w.cursor            # gets a tuple (row, col)
  62. >    name = b.name            # gets the buffer file name
  63. >    line = b[n]            # gets a line from the buffer
  64. >    lines = b[n:m]            # gets a list of lines
  65. >    num = len(b)            # gets the number of lines
  66. >    b[n] = str            # sets a line in the buffer
  67. >    b[n:m] = [str1, str2, str3]    # sets a number of lines at once
  68. >    del b[n]            # deletes a line
  69. >    del b[n:m]            # deletes a number of lines
  70.  
  71. Methods
  72.     vim.command(str)                *python-command*
  73.     Executes the vim (ex-mode) command str. Returns None.
  74.     Examples:
  75. >        vim.command("set tw=72")
  76. >        vim.command("%s/aaa/bbb/g")
  77.     To execute normal-mode commands, the following definition could be
  78.     used:
  79. >        def normal(str):
  80. >            vim.command("normal "+str)
  81. >        # Note the use of '...' to delimit a string containing double
  82. >        # quotes
  83. >        normal('"a2dd"aP')
  84.  
  85.     vim.expr(str)                    *python-expr*
  86.     Evaluates the expression str using the vim internal expression
  87.     evaluator (see |expression|). Returns the expression result as a
  88.     string.
  89.     Examples:
  90. >        text_width = vim.expr("'tw'")
  91. >        str = vim.expr("12+12")        # NB result is a string! Use
  92. >                        # string.atoi() to convert to
  93. >                        # a number.
  94.  
  95. Error object
  96.     vim.error                    *python-error*
  97.     All vim errors encountered by Python are raised as exceptions of type
  98.     vim.error.
  99.     Example:
  100. >        try:
  101. >            vim.command("put a")
  102. >        except vim.error:
  103. >            # nothing in register a
  104.  
  105. Constants
  106.     Note that these are not actually constants - in theory you could
  107.     reassign them. But this is silly, as you would then lose access to the
  108.     vim objects referred to by the variables.
  109.  
  110.     vim.buffers                    *python-buffers*
  111.     A sequence object providing access to the list of vim buffers. The
  112.     following operations are supported:
  113. >        b = vim.buffers[i]    # Indexing (read-only)
  114. >        b in vim.buffers    # Membership test
  115. >        n = len(vim.buffers)    # Number of elements
  116. >        for b in vim.buffers:    # Sequential access
  117.  
  118.     vim.windows                    *python-windows*
  119.     A sequence object providing access to the list of vim windows. The
  120.     following operations are supported:
  121. >        w = vim.windows[i]    # Indexing (read-only)
  122. >        w in vim.windows    # Membership test
  123. >        n = len(vim.windows)    # Number of elements
  124. >        for w in vim.windows:    # Sequential access
  125.  
  126.     vim.current                    *python-current*
  127.     An object providing access (via specific attributes) to various
  128.     "current" objects available in vim:
  129.         vim.current.line    The current line (RW)        String
  130.         vim.current.buffer    The current buffer (RO)        Buffer
  131.         vim.current.window    The current window (RO)        Window
  132.         vim.current.range    The current line range (RO)    Range
  133.  
  134.     The last case deserves a little explanation. When a range is specified
  135.     in the :python or :pyfile command, this range of lines is treated as
  136.     the "current range". A range is a bit like a buffer, but with all
  137.     access restricted to a subset of lines. See |python-range| for more
  138.     details.
  139.  
  140. Output from Python                    *python-output*
  141.     All output from Python code is displayed in the Vim message area.
  142.     Normal output is displayed as information messages, and error output
  143.     is displayed as error messages.
  144.  
  145.     In implementation terms, this means that all output directed to
  146.     sys.stdout (including the output from print statements) is displayed
  147.     by vim as an information message, and all output directed to
  148.     sys.stderr (including error tracebacks) is displayed by vim as an
  149.     error message.
  150.  
  151.                             *python-input*
  152.     Input (via sys.stdin, including input() and raw_input()) is not
  153.     supported, and may cause the program to crash. This should probably be
  154.     fixed.
  155.  
  156. ==============================================================================
  157. 3. Buffer objects                    *python-buffer*
  158.  
  159. Buffer objects represent vim buffers. They can be obtained in a number of
  160. ways:
  161.     - via vim.current.buffer (|python-current|)
  162.     - from indexing vim.buffers (|python-buffers|)
  163.     - from the "buffer" attribute of a window (|python-window|)
  164.  
  165. Buffer objects have one read-only attribute - name - the full file name for
  166. the buffer. They also have three methods (append, mark, and range) which are
  167. described below.
  168.  
  169. Buffer objects can also be treated as sequence objects. In this context, they
  170. act as if they were lists (yes, they are mutable) of strings, with each
  171. element being a line of the buffer. All of the usual sequence operations,
  172. including indexing, index assignment, slicing and slice assignment, work as
  173. you would expect. Note that the result of indexing (slicing) a buffer is a
  174. string (list of strings). This has one unusual consequence - b[:] is different
  175. from b. In particular, "b[:] = None" deletes the whole of the buffer, whereas
  176. "b = None" merely updates the variable b, with no effect on the buffer.
  177.  
  178. Buffer indexes start at zero, as is normal in Python. This differs from vim
  179. line numbers, which start from 1. This is particularly relevant when dealing
  180. with marks (see below) which use vim line numbers.
  181.  
  182. The buffer object methods are:
  183.     b.append(str)    Append a line to the buffer
  184.     b.append(list)    Append a list of lines to the buffer
  185.             Note that the option of supplying a list of strings to
  186.             the append method differs from the equivalent method
  187.             for Python's built-in list objects.
  188.     b.mark(name)    Return a tuple (row,col) representing the position
  189.             of the named mark (can also get the []"<> marks)
  190.     b.range(s,e)    Return a range object (see |python-range|) which
  191.             represents the part of the given buffer between line
  192.             numbers s and e (inclusive).
  193.  
  194. Examples (assume b is the current buffer)
  195. >    print b.name        # write the buffer file name
  196. >    b[0] = "hello!!!"    # replace the top line
  197. >    b[:] = None        # delete the whole buffer
  198. >    del b[:]        # delete the whole buffer (same as above)
  199. >    b[0:0] = "add a line"    # add a line at the top
  200. >    del b[2]        # delete a line (the third)
  201. >    b.append("bottom")    # add a line at the bottom
  202. >    n = len(b)        # number of lines
  203. >    (row,col) = b.mark('a') # named mark
  204. >    r = b.range(1,5)    # a sub-range of the buffer
  205.  
  206. ==============================================================================
  207. 4. Range objects                    *python-range*
  208.  
  209. Range objects represent a part of a vim buffer. They can be obtained in a
  210. number of ways:
  211.     - via vim.current.range (|python-current|)
  212.     - from a buffer's range() method (|python-buffer|)
  213.  
  214. A range object is almost identical in operation to a buffer object. However,
  215. all operations are restricted to the lines within the range (this line range
  216. can, of course, change as a result of slice assignments, line deletions, or
  217. the range.append() method).
  218.  
  219. Unlike buffers, ranges do not have a "name" attribute, nor do they have mark()
  220. or range() methods. They do have an append() method, however, which adds
  221. line(s) to the end of the range.
  222.  
  223. ==============================================================================
  224. 5. Window objects                    *python-window*
  225.  
  226. Window objects represent vim windows. They can be obtained in a number of
  227. ways:
  228.     - via vim.current.window (|python-current|)
  229.     - from indexing vim.windows (|python-windows|)
  230.  
  231. Window objects can only be manipulated through their attributes. They have no
  232. methods, and no sequence or other interface.
  233.  
  234. Window attributes are
  235.     buffer (read-only)    The buffer displayed in this window
  236.     cursor (read-write)    The current cursor position in the window
  237.                 This is a tuple, (row,col).
  238.     height (read-write)    The window height, in rows
  239.  
  240. ==============================================================================
  241.  vim:tw=78:ts=8:sw=8:
  242.