annotate src/if_python.c @ 2258:772bfca06c18 vim73

Fix crash when using ":grep".
author Bram Moolenaar <bram@vim.org>
date Mon, 07 Jun 2010 23:07:44 +0200
parents 1bac28a53fae
children ccda151dde4e
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 /* vi:set ts=8 sts=4 sw=4:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 * Python extensions by Paul Moore.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 * Changes for Unix by David Leonard.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 * This consists of four parts:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 * 1. Python interpreter main program
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 * 2. Python output stream: writes output via [e]msg().
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 * 3. Implementation of the Vim module for Python
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 * 4. Utility functions for handling the interface between Vim and Python.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 #include "vim.h"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 #include <limits.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 /* Python.h defines _POSIX_THREADS itself (if needed) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 #ifdef _POSIX_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 # undef _POSIX_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28
2250
1bac28a53fae Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents: 2100
diff changeset
29 #if defined(_WIN32) && defined(HAVE_FCNTL_H)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 # undef HAVE_FCNTL_H
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 #ifdef _DEBUG
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 # undef _DEBUG
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 #ifdef HAVE_STDARG_H
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 # undef HAVE_STDARG_H /* Python's config.h defines it as well. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 #endif
1991
5c6a3e894d43 updated for version 7.2-288
vimboss
parents: 1971
diff changeset
40 #ifdef _POSIX_C_SOURCE
5c6a3e894d43 updated for version 7.2-288
vimboss
parents: 1971
diff changeset
41 # undef _POSIX_C_SOURCE /* pyconfig.h defines it as well. */
5c6a3e894d43 updated for version 7.2-288
vimboss
parents: 1971
diff changeset
42 #endif
5c6a3e894d43 updated for version 7.2-288
vimboss
parents: 1971
diff changeset
43 #ifdef _XOPEN_SOURCE
5c6a3e894d43 updated for version 7.2-288
vimboss
parents: 1971
diff changeset
44 # undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */
5c6a3e894d43 updated for version 7.2-288
vimboss
parents: 1971
diff changeset
45 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
47 #define PY_SSIZE_T_CLEAN
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
48
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 #include <Python.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 #if defined(MACOS) && !defined(MACOS_X_UNIX)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 # include "macglue.h"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 # include <CodeFragments.h>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 #undef main /* Defined in python.h - aargh */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 #undef HAVE_FCNTL_H /* Clash with os_win32.h */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 #if !defined(FEAT_PYTHON) && defined(PROTO)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 /* Use this to be able to generate prototypes without python being used. */
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
59 # define PyObject Py_ssize_t
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
60 # define PyThreadState Py_ssize_t
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
61 # define PyTypeObject Py_ssize_t
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
62 struct PyMethodDef { Py_ssize_t a; };
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
63 # define PySequenceMethods Py_ssize_t
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
66 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
67 # define PyInt Py_ssize_t
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
68 # define PyInquiry lenfunc
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
69 # define PyIntArgFunc ssizeargfunc
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
70 # define PyIntIntArgFunc ssizessizeargfunc
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
71 # define PyIntObjArgProc ssizeobjargproc
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
72 # define PyIntIntObjArgProc ssizessizeobjargproc
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
73 # define Py_ssize_t_fmt "n"
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
74 #else
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
75 # define PyInt int
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
76 # define PyInquiry inquiry
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
77 # define PyIntArgFunc intargfunc
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
78 # define PyIntIntArgFunc intintargfunc
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
79 # define PyIntObjArgProc intobjargproc
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
80 # define PyIntIntObjArgProc intintobjargproc
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
81 # define Py_ssize_t_fmt "i"
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
82 #endif
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
83
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 /* Parser flags */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 #define single_input 256
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 #define file_input 257
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 #define eval_input 258
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 /* Python 2.3: can invoke ":python" recursively. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 # define PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 #if defined(DYNAMIC_PYTHON) || defined(PROTO)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 # ifndef DYNAMIC_PYTHON
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
96 # define HINSTANCE long_u /* for generating prototypes */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
99 /* This makes if_python.c compile without warnings against Python 2.5
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
100 * on Win32 and Win64. */
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
101 #undef PyRun_SimpleString
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
102 #undef PyArg_Parse
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
103 #undef PyArg_ParseTuple
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
104 #undef Py_BuildValue
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
105 #undef Py_InitModule4
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
106 #undef Py_InitModule4_64
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
107
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 * Wrapper defines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111 # define PyArg_Parse dll_PyArg_Parse
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112 # define PyArg_ParseTuple dll_PyArg_ParseTuple
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 # define PyDict_SetItemString dll_PyDict_SetItemString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 # define PyErr_BadArgument dll_PyErr_BadArgument
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 # define PyErr_Clear dll_PyErr_Clear
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 # define PyErr_NoMemory dll_PyErr_NoMemory
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 # define PyErr_Occurred dll_PyErr_Occurred
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 # define PyErr_SetNone dll_PyErr_SetNone
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 # define PyErr_SetString dll_PyErr_SetString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 # define PyEval_InitThreads dll_PyEval_InitThreads
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 # define PyEval_RestoreThread dll_PyEval_RestoreThread
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 # define PyEval_SaveThread dll_PyEval_SaveThread
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 # ifdef PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 # define PyGILState_Ensure dll_PyGILState_Ensure
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 # define PyGILState_Release dll_PyGILState_Release
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 # define PyInt_AsLong dll_PyInt_AsLong
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 # define PyInt_FromLong dll_PyInt_FromLong
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 # define PyInt_Type (*dll_PyInt_Type)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130 # define PyList_GetItem dll_PyList_GetItem
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
131 # define PyList_Append dll_PyList_Append
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 # define PyList_New dll_PyList_New
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 # define PyList_SetItem dll_PyList_SetItem
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 # define PyList_Size dll_PyList_Size
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 # define PyList_Type (*dll_PyList_Type)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136 # define PyImport_ImportModule dll_PyImport_ImportModule
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
137 # define PyDict_New dll_PyDict_New
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138 # define PyDict_GetItemString dll_PyDict_GetItemString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 # define PyModule_GetDict dll_PyModule_GetDict
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140 # define PyRun_SimpleString dll_PyRun_SimpleString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141 # define PyString_AsString dll_PyString_AsString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 # define PyString_FromString dll_PyString_FromString
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143 # define PyString_FromStringAndSize dll_PyString_FromStringAndSize
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 # define PyString_Size dll_PyString_Size
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145 # define PyString_Type (*dll_PyString_Type)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 # define PySys_SetObject dll_PySys_SetObject
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147 # define PySys_SetArgv dll_PySys_SetArgv
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148 # define PyType_Type (*dll_PyType_Type)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149 # define Py_BuildValue dll_Py_BuildValue
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 # define Py_FindMethod dll_Py_FindMethod
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151 # define Py_InitModule4 dll_Py_InitModule4
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 # define Py_Initialize dll_Py_Initialize
242
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
153 # define Py_Finalize dll_Py_Finalize
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
154 # define Py_IsInitialized dll_Py_IsInitialized
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155 # define _PyObject_New dll__PyObject_New
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156 # define _Py_NoneStruct (*dll__Py_NoneStruct)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157 # define PyObject_Init dll__PyObject_Init
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 # define PyType_IsSubtype dll_PyType_IsSubtype
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
160 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
162 # define PyObject_Malloc dll_PyObject_Malloc
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
163 # define PyObject_Free dll_PyObject_Free
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
164 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
165
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
166 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
167 * Pointers for dynamic link
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
168 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
169 static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
170 static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
171 static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
172 static int(*dll_PyErr_BadArgument)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
173 static void(*dll_PyErr_Clear)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
174 static PyObject*(*dll_PyErr_NoMemory)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
175 static PyObject*(*dll_PyErr_Occurred)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
176 static void(*dll_PyErr_SetNone)(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
177 static void(*dll_PyErr_SetString)(PyObject *, const char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
178 static void(*dll_PyEval_InitThreads)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
179 static void(*dll_PyEval_RestoreThread)(PyThreadState *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
180 static PyThreadState*(*dll_PyEval_SaveThread)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
181 # ifdef PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
182 static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
183 static void (*dll_PyGILState_Release)(PyGILState_STATE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
184 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
185 static long(*dll_PyInt_AsLong)(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
186 static PyObject*(*dll_PyInt_FromLong)(long);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
187 static PyTypeObject* dll_PyInt_Type;
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
188 static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
189 static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
190 static PyObject*(*dll_PyList_New)(PyInt size);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
191 static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
192 static PyInt(*dll_PyList_Size)(PyObject *);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
193 static PyTypeObject* dll_PyList_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
194 static PyObject*(*dll_PyImport_ImportModule)(const char *);
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
195 static PyObject*(*dll_PyDict_New)(void);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
196 static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
197 static PyObject*(*dll_PyModule_GetDict)(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
198 static int(*dll_PyRun_SimpleString)(char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
199 static char*(*dll_PyString_AsString)(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
200 static PyObject*(*dll_PyString_FromString)(const char *);
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
201 static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
202 static PyInt(*dll_PyString_Size)(PyObject *);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
203 static PyTypeObject* dll_PyString_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
204 static int(*dll_PySys_SetObject)(char *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
205 static int(*dll_PySys_SetArgv)(int, char **);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
206 static PyTypeObject* dll_PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
207 static PyObject*(*dll_Py_BuildValue)(char *, ...);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
208 static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
209 static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
210 static void(*dll_Py_Initialize)(void);
242
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
211 static void(*dll_Py_Finalize)(void);
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
212 static int(*dll_Py_IsInitialized)(void);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
213 static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
214 static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
215 static PyObject* dll__Py_NoneStruct;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
216 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
217 static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
218 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
219 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
220 static void* (*dll_PyObject_Malloc)(size_t);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
221 static void (*dll_PyObject_Free)(void*);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
222 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
223
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
224 static HINSTANCE hinstPython = 0; /* Instance of python.dll */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
225
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
226 /* Imported exception objects */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
227 static PyObject *imp_PyExc_AttributeError;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
228 static PyObject *imp_PyExc_IndexError;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
229 static PyObject *imp_PyExc_KeyboardInterrupt;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
230 static PyObject *imp_PyExc_TypeError;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
231 static PyObject *imp_PyExc_ValueError;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
232
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
233 # define PyExc_AttributeError imp_PyExc_AttributeError
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
234 # define PyExc_IndexError imp_PyExc_IndexError
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
235 # define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
236 # define PyExc_TypeError imp_PyExc_TypeError
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
237 # define PyExc_ValueError imp_PyExc_ValueError
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
238
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
239 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
240 * Table of name to function pointer of python.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
241 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
242 # define PYTHON_PROC FARPROC
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
243 static struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
244 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
245 char *name;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
246 PYTHON_PROC *ptr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
247 } python_funcname_table[] =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
248 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
249 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
250 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
251 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
252 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
253 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
254 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
255 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
256 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
257 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
258 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
259 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
260 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
261 # ifdef PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
262 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
263 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
264 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
265 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
266 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
267 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
268 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
269 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
270 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
271 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
272 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
273 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
274 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
275 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
637
a420bba0d851 updated for version 7.0184
vimboss
parents: 634
diff changeset
276 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
277 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
278 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
279 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
280 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
281 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
282 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
283 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
284 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
285 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
286 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
287 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
288 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
289 # if (PY_VERSION_HEX >= 0x02050000) && SIZEOF_SIZE_T != SIZEOF_INT
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
290 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
291 # else
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
292 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
293 # endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
294 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
242
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
295 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
296 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
297 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
298 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
299 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
300 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
301 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
302 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
303 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
304 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
305 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
306 # endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
307 {"", NULL},
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
308 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
309
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
310 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
311 * Free python.dll
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
312 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
313 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
314 end_dynamic_python(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
315 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
316 if (hinstPython)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
317 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
318 FreeLibrary(hinstPython);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
319 hinstPython = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
320 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
321 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
322
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
323 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
324 * Load library and get all pointers.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
325 * Parameter 'libname' provides name of DLL.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
326 * Return OK or FAIL.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
327 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
328 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
329 python_runtime_link_init(char *libname, int verbose)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
330 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
331 int i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
332
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
333 if (hinstPython)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
334 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
335 hinstPython = LoadLibrary(libname);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
336 if (!hinstPython)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
337 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
338 if (verbose)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
339 EMSG2(_(e_loadlib), libname);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
340 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
341 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
342
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
343 for (i = 0; python_funcname_table[i].ptr; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
344 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
345 if ((*python_funcname_table[i].ptr = GetProcAddress(hinstPython,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
346 python_funcname_table[i].name)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
347 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
348 FreeLibrary(hinstPython);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
349 hinstPython = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
350 if (verbose)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
351 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
352 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
353 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
354 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
355 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
356 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
357
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
358 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
359 * If python is enabled (there is installed python on Windows system) return
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
360 * TRUE, else FALSE.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
361 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
362 int
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
363 python_enabled(int verbose)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
364 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
365 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
366 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
367
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
368 /* Load the standard Python exceptions - don't import the symbols from the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
369 * DLL, as this can cause errors (importing data symbols is not reliable).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
370 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
371 static void get_exceptions __ARGS((void));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
372
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
373 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
374 get_exceptions()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
375 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
376 PyObject *exmod = PyImport_ImportModule("exceptions");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
377 PyObject *exdict = PyModule_GetDict(exmod);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
378 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
379 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
380 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
381 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
382 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
383 Py_XINCREF(imp_PyExc_AttributeError);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
384 Py_XINCREF(imp_PyExc_IndexError);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
385 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
386 Py_XINCREF(imp_PyExc_TypeError);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
387 Py_XINCREF(imp_PyExc_ValueError);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
388 Py_XDECREF(exmod);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
389 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
390 #endif /* DYNAMIC_PYTHON */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
391
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
392 /******************************************************
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
393 * Internal function prototypes.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
394 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
395
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
396 static void DoPythonCommand(exarg_T *, const char *);
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
397 static PyInt RangeStart;
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
398 static PyInt RangeEnd;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
399
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
400 static void PythonIO_Flush(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
401 static int PythonIO_Init(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
402 static int PythonMod_Init(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
403
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
404 /* Utility functions for the vim/python interface
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
405 * ----------------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
406 */
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
407 static PyObject *GetBufferLine(buf_T *, PyInt);
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
408 static PyObject *GetBufferLineList(buf_T *, PyInt, PyInt);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
409
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
410 static int SetBufferLine(buf_T *, PyInt, PyObject *, PyInt *);
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
411 static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
412 static int InsertBufferLines(buf_T *, PyInt, PyObject *, PyInt *);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
413
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
414 static PyObject *LineToString(const char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
415 static char *StringToLine(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
416
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
417 static int VimErrorCheck(void);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
418
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
419 #define PyErr_SetVim(str) PyErr_SetString(VimError, str)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
420
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
421 /******************************************************
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
422 * 1. Python interpreter main program.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
423 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
424
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
425 static int initialised = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
426
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
427 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
428 typedef PyObject PyThreadState;
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
429 #endif
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
430
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
431 #ifdef PY_CAN_RECURSE
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
432 static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
433 #else
36
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
434 static PyThreadState *saved_python_thread = NULL;
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
435 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
436
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
437 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
438 * Suspend a thread of the Python interpreter, other threads are allowed to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
439 * run.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
440 */
36
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
441 static void
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
442 Python_SaveThread(void)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
443 {
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
444 #ifdef PY_CAN_RECURSE
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
445 PyGILState_Release(pygilstate);
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
446 #else
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
447 saved_python_thread = PyEval_SaveThread();
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
448 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
449 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
450
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
451 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
452 * Restore a thread of the Python interpreter, waits for other threads to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
453 * block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
454 */
36
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
455 static void
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
456 Python_RestoreThread(void)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
457 {
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
458 #ifdef PY_CAN_RECURSE
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
459 pygilstate = PyGILState_Ensure();
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
460 #else
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
461 PyEval_RestoreThread(saved_python_thread);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
462 saved_python_thread = NULL;
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
463 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
464 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
465
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
466 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
467 * obtain a lock on the Vim data structures
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
468 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
469 static void Python_Lock_Vim(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
470 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
471 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
472
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
473 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
474 * release a lock on the Vim data structures
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
475 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
476 static void Python_Release_Vim(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
477 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
478 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
479
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
480 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
481 python_end()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
482 {
557
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
483 static int recurse = 0;
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
484
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
485 /* If a crash occurs while doing this, don't try again. */
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
486 if (recurse != 0)
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
487 return;
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
488
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
489 ++recurse;
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
490
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
491 #ifdef DYNAMIC_PYTHON
242
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
492 if (hinstPython && Py_IsInitialized())
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
493 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
494 Python_RestoreThread(); /* enter python */
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
495 Py_Finalize();
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
496 }
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
497 end_dynamic_python();
242
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
498 #else
a711f7a6852d updated for version 7.0067
vimboss
parents: 135
diff changeset
499 if (Py_IsInitialized())
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
500 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
501 Python_RestoreThread(); /* enter python */
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
502 Py_Finalize();
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
503 }
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
504 #endif
557
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
505
862863033fdd updated for version 7.0158
vimboss
parents: 502
diff changeset
506 --recurse;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
507 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
508
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
509 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
510 Python_Init(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
511 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
512 if (!initialised)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
513 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
514 #ifdef DYNAMIC_PYTHON
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
515 if (!python_enabled(TRUE))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
516 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
517 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
518 goto fail;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
519 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
520 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
521
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
522 #if !defined(MACOS) || defined(MACOS_X_UNIX)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
523 Py_Initialize();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
524 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
525 PyMac_Initialize();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
526 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
527 /* initialise threads */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
528 PyEval_InitThreads();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
529
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
530 #ifdef DYNAMIC_PYTHON
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
531 get_exceptions();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
532 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
533
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
534 if (PythonIO_Init())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
535 goto fail;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
536
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
537 if (PythonMod_Init())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
538 goto fail;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
539
1748
06317850ad3a updated for version 7.2-045
vimboss
parents: 1681
diff changeset
540 /* Remove the element from sys.path that was added because of our
06317850ad3a updated for version 7.2-045
vimboss
parents: 1681
diff changeset
541 * argv[0] value in PythonMod_Init(). Previously we used an empty
06317850ad3a updated for version 7.2-045
vimboss
parents: 1681
diff changeset
542 * string, but dependinding on the OS we then get an empty entry or
06317850ad3a updated for version 7.2-045
vimboss
parents: 1681
diff changeset
543 * the current directory in sys.path. */
06317850ad3a updated for version 7.2-045
vimboss
parents: 1681
diff changeset
544 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
06317850ad3a updated for version 7.2-045
vimboss
parents: 1681
diff changeset
545
36
125e80798a85 updated for version 7.0021
vimboss
parents: 20
diff changeset
546 /* the first python thread is vim's, release the lock */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
547 Python_SaveThread();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
548
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
549 initialised = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
550 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
551
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
552 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
553
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
554 fail:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
555 /* We call PythonIO_Flush() here to print any Python errors.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
556 * This is OK, as it is possible to call this function even
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
557 * if PythonIO_Init() has not completed successfully (it will
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
558 * not do anything in this case).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
559 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
560 PythonIO_Flush();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
561 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
562 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
563
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
564 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
565 * External interface
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
566 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
567 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
568 DoPythonCommand(exarg_T *eap, const char *cmd)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
569 {
323
03b3684919e3 updated for version 7.0084
vimboss
parents: 274
diff changeset
570 #ifndef PY_CAN_RECURSE
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
571 static int recursive = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
572 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
573 #if defined(MACOS) && !defined(MACOS_X_UNIX)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
574 GrafPtr oldPort;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
575 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
576 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
577 char *saved_locale;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
578 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
579
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
580 #ifndef PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
581 if (recursive)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
582 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
583 EMSG(_("E659: Cannot invoke Python recursively"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
584 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
585 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
586 ++recursive;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
587 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
588
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
589 #if defined(MACOS) && !defined(MACOS_X_UNIX)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
590 GetPort(&oldPort);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
591 /* Check if the Python library is available */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
592 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
593 goto theend;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
594 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
595 if (Python_Init())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
596 goto theend;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
597
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
598 RangeStart = eap->line1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
599 RangeEnd = eap->line2;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
600 Python_Release_Vim(); /* leave vim */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
601
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
602 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
603 /* Python only works properly when the LC_NUMERIC locale is "C". */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
604 saved_locale = setlocale(LC_NUMERIC, NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
605 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
606 saved_locale = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
607 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
608 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
609 /* Need to make a copy, value may change when setting new locale. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
610 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
611 (void)setlocale(LC_NUMERIC, "C");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
612 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
613 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
614
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
615 Python_RestoreThread(); /* enter python */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
616
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
617 PyRun_SimpleString((char *)(cmd));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
618
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
619 Python_SaveThread(); /* leave python */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
620
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
621 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
622 if (saved_locale != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
623 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
624 (void)setlocale(LC_NUMERIC, saved_locale);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
625 vim_free(saved_locale);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
626 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
627 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
628
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
629 Python_Lock_Vim(); /* enter vim */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
630 PythonIO_Flush();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
631 #if defined(MACOS) && !defined(MACOS_X_UNIX)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
632 SetPort(oldPort);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
633 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
634
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
635 theend:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
636 #ifndef PY_CAN_RECURSE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
637 --recursive;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
638 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
639 return; /* keeps lint happy */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
640 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
641
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
642 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
643 * ":python"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
644 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
645 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
646 ex_python(exarg_T *eap)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
647 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
648 char_u *script;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
649
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
650 script = script_get(eap, eap->arg);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
651 if (!eap->skip)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
652 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
653 if (script == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
654 DoPythonCommand(eap, (char *)eap->arg);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
655 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
656 DoPythonCommand(eap, (char *)script);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
657 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
658 vim_free(script);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
659 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
660
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
661 #define BUFFER_SIZE 1024
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
662
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
663 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
664 * ":pyfile"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
665 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
666 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
667 ex_pyfile(exarg_T *eap)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
668 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
669 static char buffer[BUFFER_SIZE];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
670 const char *file = (char *)eap->arg;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
671 char *p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
672
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
673 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
674 * stdio file pointer, but Vim and the Python DLL are compiled with
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
675 * different options under Windows, meaning that stdio pointers aren't
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
676 * compatible between the two. Yuk.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
677 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
678 * Put the string "execfile('file')" into buffer. But, we need to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
679 * escape any backslashes or single quotes in the file name, so that
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
680 * Python won't mangle the file name.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
681 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
682 strcpy(buffer, "execfile('");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
683 p = buffer + 10; /* size of "execfile('" */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
684
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
685 while (*file && p < buffer + (BUFFER_SIZE - 3))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
686 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
687 if (*file == '\\' || *file == '\'')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
688 *p++ = '\\';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
689 *p++ = *file++;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
690 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
691
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
692 /* If we didn't finish the file name, we hit a buffer overflow */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
693 if (*file != '\0')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
694 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
695
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
696 /* Put in the terminating "')" and a null */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
697 *p++ = '\'';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
698 *p++ = ')';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
699 *p++ = '\0';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
700
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
701 /* Execute the file */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
702 DoPythonCommand(eap, buffer);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
703 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
704
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
705 /******************************************************
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
706 * 2. Python output stream: writes output via [e]msg().
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
707 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
708
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
709 /* Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
710 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
711
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
712 static PyObject *OutputGetattr(PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
713 static int OutputSetattr(PyObject *, char *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
714
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
715 static PyObject *OutputWrite(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
716 static PyObject *OutputWritelines(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
717
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
718 typedef void (*writefn)(char_u *);
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
719 static void writer(writefn fn, char_u *str, PyInt n);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
720
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
721 /* Output object definition
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
722 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
723
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
724 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
725 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
726 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
727 long softspace;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
728 long error;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
729 } OutputObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
730
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
731 static struct PyMethodDef OutputMethods[] = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
732 /* name, function, calling, documentation */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
733 {"write", OutputWrite, 1, "" },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
734 {"writelines", OutputWritelines, 1, "" },
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
735 { NULL, NULL, 0, NULL }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
736 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
737
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
738 static PyTypeObject OutputType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
739 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
740 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
741 "message",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
742 sizeof(OutputObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
743 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
744
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
745 (destructor) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
746 (printfunc) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
747 (getattrfunc) OutputGetattr,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
748 (setattrfunc) OutputSetattr,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
749 (cmpfunc) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
750 (reprfunc) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
751
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
752 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
753 0, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
754 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
755
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
756 (hashfunc) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
757 (ternaryfunc) 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
758 (reprfunc) 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
759 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
760
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
761 /*************/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
762
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
763 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
764 OutputGetattr(PyObject *self, char *name)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
765 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
766 if (strcmp(name, "softspace") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
767 return PyInt_FromLong(((OutputObject *)(self))->softspace);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
768
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
769 return Py_FindMethod(OutputMethods, self, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
770 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
771
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
772 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
773 OutputSetattr(PyObject *self, char *name, PyObject *val)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
774 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
775 if (val == NULL) {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
776 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
777 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
778 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
779
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
780 if (strcmp(name, "softspace") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
781 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
782 if (!PyInt_Check(val)) {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
783 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
784 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
785 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
786
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
787 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
788 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
789 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
790
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
791 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
792 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
793 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
794
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
795 /*************/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
796
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
797 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
798 OutputWrite(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
799 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
800 int len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
801 char *str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
802 int error = ((OutputObject *)(self))->error;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
803
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
804 if (!PyArg_ParseTuple(args, "s#", &str, &len))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
805 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
806
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
807 Py_BEGIN_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
808 Python_Lock_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
809 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
810 Python_Release_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
811 Py_END_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
812
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
813 Py_INCREF(Py_None);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
814 return Py_None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
815 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
816
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
817 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
818 OutputWritelines(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
819 {
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
820 PyInt n;
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
821 PyInt i;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
822 PyObject *list;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
823 int error = ((OutputObject *)(self))->error;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
824
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
825 if (!PyArg_ParseTuple(args, "O", &list))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
826 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
827 Py_INCREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
828
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
829 if (!PyList_Check(list)) {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
830 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
831 Py_DECREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
832 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
833 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
834
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
835 n = PyList_Size(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
836
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
837 for (i = 0; i < n; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
838 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
839 PyObject *line = PyList_GetItem(list, i);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
840 char *str;
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
841 PyInt len;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
842
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
843 if (!PyArg_Parse(line, "s#", &str, &len)) {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
844 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
845 Py_DECREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
846 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
847 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
848
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
849 Py_BEGIN_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
850 Python_Lock_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
851 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
852 Python_Release_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
853 Py_END_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
854 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
855
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
856 Py_DECREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
857 Py_INCREF(Py_None);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
858 return Py_None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
859 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
860
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
861 /* Output buffer management
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
862 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
863
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
864 static char_u *buffer = NULL;
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
865 static PyInt buffer_len = 0;
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
866 static PyInt buffer_size = 0;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
867
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
868 static writefn old_fn = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
869
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
870 static void
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
871 buffer_ensure(PyInt n)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
872 {
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
873 PyInt new_size;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
874 char_u *new_buffer;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
875
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
876 if (n < buffer_size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
877 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
878
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
879 new_size = buffer_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
880 while (new_size < n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
881 new_size += 80;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
882
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
883 if (new_size != buffer_size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
884 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
885 new_buffer = alloc((unsigned)new_size);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
886 if (new_buffer == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
887 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
888
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
889 if (buffer)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
890 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
891 memcpy(new_buffer, buffer, buffer_len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
892 vim_free(buffer);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
893 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
894
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
895 buffer = new_buffer;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
896 buffer_size = new_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
897 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
898 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
899
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
900 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
901 PythonIO_Flush(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
902 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
903 if (old_fn && buffer_len)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
904 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
905 buffer[buffer_len] = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
906 old_fn(buffer);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
907 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
908
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
909 buffer_len = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
910 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
911
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
912 static void
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
913 writer(writefn fn, char_u *str, PyInt n)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
914 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
915 char_u *ptr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
916
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
917 if (fn != old_fn && old_fn != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
918 PythonIO_Flush();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
919
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
920 old_fn = fn;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
921
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
922 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
923 {
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
924 PyInt len = ptr - str;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
925
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
926 buffer_ensure(buffer_len + len + 1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
927
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
928 memcpy(buffer + buffer_len, str, len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
929 buffer_len += len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
930 buffer[buffer_len] = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
931 fn(buffer);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
932 str = ptr + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
933 n -= len + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
934 buffer_len = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
935 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
936
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
937 /* Put the remaining text into the buffer for later printing */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
938 buffer_ensure(buffer_len + n + 1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
939 memcpy(buffer + buffer_len, str, n);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
940 buffer_len += n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
941 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
942
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
943 /***************/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
944
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
945 static OutputObject Output =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
946 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
947 PyObject_HEAD_INIT(&OutputType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
948 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
949 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
950 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
951
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
952 static OutputObject Error =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
953 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
954 PyObject_HEAD_INIT(&OutputType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
955 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
956 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
957 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
958
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
959 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
960 PythonIO_Init(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
961 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
962 /* Fixups... */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
963 OutputType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
964
135
b5fc81a825a1 updated for version 7.0044
vimboss
parents: 36
diff changeset
965 PySys_SetObject("stdout", (PyObject *)(void *)&Output);
b5fc81a825a1 updated for version 7.0044
vimboss
parents: 36
diff changeset
966 PySys_SetObject("stderr", (PyObject *)(void *)&Error);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
967
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
968 if (PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
969 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
970 EMSG(_("E264: Python: Error initialising I/O objects"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
971 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
972 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
973
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
974 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
975 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
976
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
977 /******************************************************
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
978 * 3. Implementation of the Vim module for Python
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
979 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
980
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
981 /* Vim module - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
982 * -------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
983 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
984
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
985 static PyObject *VimError;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
986
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
987 static PyObject *VimCommand(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
988 static PyObject *VimEval(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
989
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
990 /* Window type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
991 * --------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
992 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
993
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
994 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
995 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
996 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
997 win_T *win;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
998 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
999 WindowObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1000
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1001 #define INVALID_WINDOW_VALUE ((win_T *)(-1))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1002
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1003 #define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1004
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1005 static PyObject *WindowNew(win_T *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1006
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1007 static void WindowDestructor(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1008 static PyObject *WindowGetattr(PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1009 static int WindowSetattr(PyObject *, char *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1010 static PyObject *WindowRepr(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1011
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1012 /* Buffer type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1013 * --------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1014 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1015
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1016 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1017 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1018 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1019 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1020 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1021 BufferObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1022
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1023 #define INVALID_BUFFER_VALUE ((buf_T *)(-1))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1024
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1025 #define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1026
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1027 static PyObject *BufferNew (buf_T *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1028
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1029 static void BufferDestructor(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1030 static PyObject *BufferGetattr(PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1031 static PyObject *BufferRepr(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1032
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1033 static PyInt BufferLength(PyObject *);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1034 static PyObject *BufferItem(PyObject *, PyInt);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1035 static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1036 static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1037 static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1038
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1039 static PyObject *BufferAppend(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1040 static PyObject *BufferMark(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1041 static PyObject *BufferRange(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1042
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1043 /* Line range type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1044 * --------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1045 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1046
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1047 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1048 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1049 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1050 BufferObject *buf;
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1051 PyInt start;
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1052 PyInt end;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1053 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1054 RangeObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1055
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1056 #define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1057
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1058 static PyObject *RangeNew(buf_T *, PyInt, PyInt);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1059
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1060 static void RangeDestructor(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1061 static PyObject *RangeGetattr(PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1062 static PyObject *RangeRepr(PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1063
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1064 static PyInt RangeLength(PyObject *);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1065 static PyObject *RangeItem(PyObject *, PyInt);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1066 static PyObject *RangeSlice(PyObject *, PyInt, PyInt);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1067 static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1068 static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1069
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1070 static PyObject *RangeAppend(PyObject *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1071
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1072 /* Window list type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1073 * -------------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1074 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1075
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1076 static PyInt WinListLength(PyObject *);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1077 static PyObject *WinListItem(PyObject *, PyInt);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1078
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1079 /* Buffer list type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1080 * -------------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1081 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1082
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1083 static PyInt BufListLength(PyObject *);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1084 static PyObject *BufListItem(PyObject *, PyInt);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1085
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1086 /* Current objects type - Implementation functions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1087 * -----------------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1088 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1089
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1090 static PyObject *CurrentGetattr(PyObject *, char *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1091 static int CurrentSetattr(PyObject *, char *, PyObject *);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1092
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1093 /* Vim module - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1094 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1095
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1096 static struct PyMethodDef VimMethods[] = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1097 /* name, function, calling, documentation */
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1098 {"command", VimCommand, 1, "Execute a Vim ex-mode command" },
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1099 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" },
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1100 { NULL, NULL, 0, NULL }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1101 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1102
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1103 /* Vim module - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1104 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1105 static PyObject *
1887
fc89a4f98a8b updated for version 7.2-184
vimboss
parents: 1786
diff changeset
1106 VimCommand(PyObject *self UNUSED, PyObject *args)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1107 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1108 char *cmd;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1109 PyObject *result;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1110
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1111 if (!PyArg_ParseTuple(args, "s", &cmd))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1112 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1113
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1114 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1115
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1116 Py_BEGIN_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1117 Python_Lock_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1118
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1119 do_cmdline_cmd((char_u *)cmd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1120 update_screen(VALID);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1121
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1122 Python_Release_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1123 Py_END_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1124
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1125 if (VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1126 result = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1127 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1128 result = Py_None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1129
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1130 Py_XINCREF(result);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1131 return result;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1132 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1133
1681
fe4ebbec775a updated for version 7.2b-014
vimboss
parents: 1668
diff changeset
1134 #ifdef FEAT_EVAL
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1135 /*
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1136 * Function to translate a typval_T into a PyObject; this will recursively
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1137 * translate lists/dictionaries into their Python equivalents.
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1138 *
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1139 * The depth parameter is to avoid infinite recursion, set it to 1 when
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1140 * you call VimToPython.
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1141 */
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1142 static PyObject *
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1143 VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1144 {
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1145 PyObject *result;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1146 PyObject *newObj;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1147 char ptrBuf[NUMBUFLEN];
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1148
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1149 /* Avoid infinite recursion */
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1150 if (depth > 100)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1151 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1152 Py_INCREF(Py_None);
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1153 result = Py_None;
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1154 return result;
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1155 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1156
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1157 /* Check if we run into a recursive loop. The item must be in lookupDict
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1158 * then and we can use it again. */
1786
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1159 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1160 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1161 {
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1162 sprintf(ptrBuf, PRINTF_DECIMAL_LONG_U,
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1163 our_tv->v_type == VAR_LIST ? (long_u)our_tv->vval.v_list
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1164 : (long_u)our_tv->vval.v_dict);
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1165 result = PyDict_GetItemString(lookupDict, ptrBuf);
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1166 if (result != NULL)
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1167 {
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1168 Py_INCREF(result);
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1169 return result;
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1170 }
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1171 }
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1172
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1173 if (our_tv->v_type == VAR_STRING)
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1174 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1175 result = Py_BuildValue("s", our_tv->vval.v_string);
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1176 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1177 else if (our_tv->v_type == VAR_NUMBER)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1178 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1179 char buf[NUMBUFLEN];
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1180
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1181 /* For backwards compatibility numbers are stored as strings. */
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1182 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1183 result = Py_BuildValue("s", buf);
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1184 }
1681
fe4ebbec775a updated for version 7.2b-014
vimboss
parents: 1668
diff changeset
1185 # ifdef FEAT_FLOAT
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1186 else if (our_tv->v_type == VAR_FLOAT)
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1187 {
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1188 char buf[NUMBUFLEN];
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1189
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1190 sprintf(buf, "%f", our_tv->vval.v_float);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1191 result = Py_BuildValue("s", buf);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1192 }
1681
fe4ebbec775a updated for version 7.2b-014
vimboss
parents: 1668
diff changeset
1193 # endif
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1194 else if (our_tv->v_type == VAR_LIST)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1195 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1196 list_T *list = our_tv->vval.v_list;
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1197 listitem_T *curr;
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1198
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1199 result = PyList_New(0);
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1200
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1201 if (list != NULL)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1202 {
1786
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1203 PyDict_SetItemString(lookupDict, ptrBuf, result);
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1204
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1205 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1206 {
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1207 newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1208 PyList_Append(result, newObj);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1209 Py_DECREF(newObj);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1210 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1211 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1212 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1213 else if (our_tv->v_type == VAR_DICT)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1214 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1215 result = PyDict_New();
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1216
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1217 if (our_tv->vval.v_dict != NULL)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1218 {
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1219 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1220 long_u todo = ht->ht_used;
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1221 hashitem_T *hi;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1222 dictitem_T *di;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1223
1786
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1224 PyDict_SetItemString(lookupDict, ptrBuf, result);
b2f4319efde1 updated for version 7.2-084
vimboss
parents: 1748
diff changeset
1225
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1226 for (hi = ht->ht_array; todo > 0; ++hi)
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1227 {
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1228 if (!HASHITEM_EMPTY(hi))
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1229 {
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1230 --todo;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1231
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1232 di = dict_lookup(hi);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1233 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1234 PyDict_SetItemString(result, (char *)hi->hi_key, newObj);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1235 Py_DECREF(newObj);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1236 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1237 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1238 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1239 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1240 else
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1241 {
856
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1242 Py_INCREF(Py_None);
8cd729851562 updated for version 7.0g
vimboss
parents: 671
diff changeset
1243 result = Py_None;
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1244 }
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1245
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1246 return result;
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1247 }
1681
fe4ebbec775a updated for version 7.2b-014
vimboss
parents: 1668
diff changeset
1248 #endif
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1249
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1250 static PyObject *
1887
fc89a4f98a8b updated for version 7.2-184
vimboss
parents: 1786
diff changeset
1251 VimEval(PyObject *self UNUSED, PyObject *args)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1252 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1253 #ifdef FEAT_EVAL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1254 char *expr;
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1255 typval_T *our_tv;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1256 PyObject *result;
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1257 PyObject *lookup_dict;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1258
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1259 if (!PyArg_ParseTuple(args, "s", &expr))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1260 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1261
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1262 Py_BEGIN_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1263 Python_Lock_Vim();
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1264 our_tv = eval_expr((char_u *)expr, NULL);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1265
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1266 Python_Release_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1267 Py_END_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1268
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1269 if (our_tv == NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1270 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1271 PyErr_SetVim(_("invalid expression"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1272 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1273 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1274
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1275 /* Convert the Vim type into a Python type. Create a dictionary that's
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1276 * used to check for recursive loops. */
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1277 lookup_dict = PyDict_New();
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1278 result = VimToPython(our_tv, 1, lookup_dict);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1279 Py_DECREF(lookup_dict);
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1280
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1281
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1282 Py_BEGIN_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1283 Python_Lock_Vim();
634
1c586ee8dd45 updated for version 7.0183
vimboss
parents: 557
diff changeset
1284 free_tv(our_tv);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1285 Python_Release_Vim();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1286 Py_END_ALLOW_THREADS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1287
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1288 return result;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1289 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1290 PyErr_SetVim(_("expressions disabled at compile time"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1291 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1292 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1293 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1294
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1295 /* Common routines for buffers and line ranges
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1296 * -------------------------------------------
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1297 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1298 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1299 CheckBuffer(BufferObject *this)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1300 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1301 if (this->buf == INVALID_BUFFER_VALUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1302 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1303 PyErr_SetVim(_("attempt to refer to deleted buffer"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1304 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1305 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1306
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1307 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1308 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1309
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1310 static PyObject *
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1311 RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1312 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1313 if (CheckBuffer(self))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1314 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1315
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1316 if (n < 0 || n > end - start)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1317 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1318 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1319 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1320 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1321
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1322 return GetBufferLine(self->buf, n+start);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1323 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1324
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1325 static PyObject *
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1326 RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1327 {
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1328 PyInt size;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1329
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1330 if (CheckBuffer(self))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1331 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1332
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1333 size = end - start + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1334
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1335 if (lo < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1336 lo = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1337 else if (lo > size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1338 lo = size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1339 if (hi < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1340 hi = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1341 if (hi < lo)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1342 hi = lo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1343 else if (hi > size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1344 hi = size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1345
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1346 return GetBufferLineList(self->buf, lo+start, hi+start);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1347 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1348
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1349 static PyInt
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1350 RBAssItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1351 {
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1352 PyInt len_change;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1353
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1354 if (CheckBuffer(self))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1355 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1356
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1357 if (n < 0 || n > end - start)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1358 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1359 PyErr_SetString(PyExc_IndexError, _("line number out of range"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1360 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1361 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1362
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1363 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1364 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1365
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1366 if (new_end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1367 *new_end = end + len_change;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1368
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1369 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1370 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1371
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1372 static PyInt
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1373 RBAssSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1374 {
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1375 PyInt size;
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1376 PyInt len_change;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1377
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1378 /* Self must be a valid buffer */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1379 if (CheckBuffer(self))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1380 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1381
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1382 /* Sort out the slice range */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1383 size = end - start + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1384
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1385 if (lo < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1386 lo = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1387 else if (lo > size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1388 lo = size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1389 if (hi < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1390 hi = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1391 if (hi < lo)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1392 hi = lo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1393 else if (hi > size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1394 hi = size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1395
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1396 if (SetBufferLineList(self->buf, lo+start, hi+start, val, &len_change) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1397 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1398
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1399 if (new_end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1400 *new_end = end + len_change;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1401
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1402 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1403 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1404
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1405 static PyObject *
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1406 RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1407 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1408 PyObject *lines;
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1409 PyInt len_change;
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1410 PyInt max;
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1411 PyInt n;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1412
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1413 if (CheckBuffer(self))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1414 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1415
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1416 max = n = end - start + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1417
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1418 if (!PyArg_ParseTuple(args, "O|" Py_ssize_t_fmt, &lines, &n))
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1419 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1420
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1421 if (n < 0 || n > max)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1422 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1423 PyErr_SetString(PyExc_ValueError, _("line number out of range"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1424 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1425 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1426
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1427 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1428 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1429
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1430 if (new_end)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1431 *new_end = end + len_change;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1432
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1433 Py_INCREF(Py_None);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1434 return Py_None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1435 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1436
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1437
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1438 /* Buffer object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1439 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1440
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1441 static struct PyMethodDef BufferMethods[] = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1442 /* name, function, calling, documentation */
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1443 {"append", BufferAppend, 1, "Append data to Vim buffer" },
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1444 {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" },
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1445 {"range", BufferRange, 1, "Return a range object which represents the part of the given buffer between line numbers s and e" },
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1446 { NULL, NULL, 0, NULL }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1447 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1448
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1449 static PySequenceMethods BufferAsSeq = {
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1450 (PyInquiry) BufferLength, /* sq_length, len(x) */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1451 (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1452 (PyIntArgFunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1453 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1454 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1455 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1456 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1457 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1458
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1459 static PyTypeObject BufferType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1460 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1461 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1462 "buffer",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1463 sizeof(BufferObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1464 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1465
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1466 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1467 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1468 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1469 (setattrfunc) 0, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1470 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1471 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1472
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1473 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1474 &BufferAsSeq, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1475 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1476
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1477 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1478 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1479 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1480 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1481
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1482 /* Buffer object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1483 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1484
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1485 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1486 BufferNew(buf_T *buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1487 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1488 /* We need to handle deletion of buffers underneath us.
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1489 * If we add a "b_python_ref" field to the buf_T structure,
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1490 * then we can get at it in buf_freeall() in vim. We then
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1491 * need to create only ONE Python object per buffer - if
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1492 * we try to create a second, just INCREF the existing one
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1493 * and return it. The (single) Python object referring to
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1494 * the buffer is stored in "b_python_ref".
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1495 * Question: what to do on a buf_freeall(). We'll probably
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1496 * have to either delete the Python object (DECREF it to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1497 * zero - a bad idea, as it leaves dangling refs!) or
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1498 * set the buf_T * value to an invalid value (-1?), which
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1499 * means we need checks in all access functions... Bah.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1500 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1501
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1502 BufferObject *self;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1503
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1504 if (buf->b_python_ref != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1505 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1506 self = buf->b_python_ref;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1507 Py_INCREF(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1508 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1509 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1510 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1511 self = PyObject_NEW(BufferObject, &BufferType);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1512 if (self == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1513 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1514 self->buf = buf;
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1515 buf->b_python_ref = self;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1516 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1517
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1518 return (PyObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1519 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1520
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1521 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1522 BufferDestructor(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1523 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1524 BufferObject *this = (BufferObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1525
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1526 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1527 this->buf->b_python_ref = NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1528
986
8fd2e00c44ae updated for version 7.0-112
vimboss
parents: 856
diff changeset
1529 Py_DECREF(self);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1530 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1531
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1532 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1533 BufferGetattr(PyObject *self, char *name)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1534 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1535 BufferObject *this = (BufferObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1536
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1537 if (CheckBuffer(this))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1538 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1539
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1540 if (strcmp(name, "name") == 0)
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1541 return Py_BuildValue("s", this->buf->b_ffname);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1542 else if (strcmp(name, "number") == 0)
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1543 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1544 else if (strcmp(name,"__members__") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1545 return Py_BuildValue("[ss]", "name", "number");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1546 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1547 return Py_FindMethod(BufferMethods, self, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1548 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1549
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1550 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1551 BufferRepr(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1552 {
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
1553 static char repr[100];
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1554 BufferObject *this = (BufferObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1555
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1556 if (this->buf == INVALID_BUFFER_VALUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1557 {
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1558 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1559 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1560 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1561 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1562 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1563 char *name = (char *)this->buf->b_fname;
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1564 PyInt len;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1565
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1566 if (name == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1567 name = "";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1568 len = strlen(name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1569
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1570 if (len > 35)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1571 name = name + (35 - len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1572
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
1573 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1574
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1575 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1576 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1577 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1578
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1579 /******************/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1580
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1581 static PyInt
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1582 BufferLength(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1583 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1584 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1585 if (CheckBuffer((BufferObject *)(self)))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1586 return -1; /* ??? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1587
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1588 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1589 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1590
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1591 static PyObject *
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1592 BufferItem(PyObject *self, PyInt n)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1593 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1594 return RBItem((BufferObject *)(self), n, 1,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1595 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1596 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1597
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1598 static PyObject *
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1599 BufferSlice(PyObject *self, PyInt lo, PyInt hi)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1600 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1601 return RBSlice((BufferObject *)(self), lo, hi, 1,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1602 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1603 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1604
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1605 static PyInt
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1606 BufferAssItem(PyObject *self, PyInt n, PyObject *val)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1607 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1608 return RBAssItem((BufferObject *)(self), n, val, 1,
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1609 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1610 NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1611 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1612
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1613 static PyInt
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1614 BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1615 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1616 return RBAssSlice((BufferObject *)(self), lo, hi, val, 1,
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1617 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1618 NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1619 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1620
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1621 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1622 BufferAppend(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1623 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1624 return RBAppend((BufferObject *)(self), args, 1,
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1625 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1626 NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1627 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1628
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1629 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1630 BufferMark(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1631 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1632 pos_T *posp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1633 char mark;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1634 buf_T *curbuf_save;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1635
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1636 if (CheckBuffer((BufferObject *)(self)))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1637 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1638
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1639 if (!PyArg_ParseTuple(args, "c", &mark))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1640 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1641
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1642 curbuf_save = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1643 curbuf = ((BufferObject *)(self))->buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1644 posp = getmark(mark, FALSE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1645 curbuf = curbuf_save;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1646
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1647 if (posp == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1648 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1649 PyErr_SetVim(_("invalid mark name"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1650 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1651 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1652
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1653 /* Ckeck for keyboard interrupt */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1654 if (VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1655 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1656
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1657 if (posp->lnum <= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1658 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1659 /* Or raise an error? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1660 Py_INCREF(Py_None);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1661 return Py_None;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1662 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1663
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1664 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1665 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1666
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1667 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1668 BufferRange(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1669 {
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1670 PyInt start;
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1671 PyInt end;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1672
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1673 if (CheckBuffer((BufferObject *)(self)))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1674 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1675
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1676 if (!PyArg_ParseTuple(args, Py_ssize_t_fmt Py_ssize_t_fmt, &start, &end))
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1677 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1678
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1679 return RangeNew(((BufferObject *)(self))->buf, start, end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1680 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1681
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1682 /* Line range object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1683 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1684
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1685 static struct PyMethodDef RangeMethods[] = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1686 /* name, function, calling, documentation */
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1687 {"append", RangeAppend, 1, "Append data to the Vim range" },
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1688 { NULL, NULL, 0, NULL }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1689 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1690
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1691 static PySequenceMethods RangeAsSeq = {
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1692 (PyInquiry) RangeLength, /* sq_length, len(x) */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1693 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1694 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1695 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1696 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1697 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1698 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1699 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1700
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1701 static PyTypeObject RangeType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1702 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1703 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1704 "range",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1705 sizeof(RangeObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1706 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1707
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1708 (destructor) RangeDestructor, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1709 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1710 (getattrfunc) RangeGetattr, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1711 (setattrfunc) 0, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1712 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1713 (reprfunc) RangeRepr, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1714
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1715 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1716 &RangeAsSeq, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1717 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1718
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1719 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1720 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1721 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1722 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1723
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1724 /* Line range object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1725 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1726
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1727 static PyObject *
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1728 RangeNew(buf_T *buf, PyInt start, PyInt end)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1729 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1730 BufferObject *bufr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1731 RangeObject *self;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1732 self = PyObject_NEW(RangeObject, &RangeType);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1733 if (self == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1734 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1735
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1736 bufr = (BufferObject *)BufferNew(buf);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1737 if (bufr == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1738 {
986
8fd2e00c44ae updated for version 7.0-112
vimboss
parents: 856
diff changeset
1739 Py_DECREF(self);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1740 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1741 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1742 Py_INCREF(bufr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1743
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1744 self->buf = bufr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1745 self->start = start;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1746 self->end = end;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1747
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1748 return (PyObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1749 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1750
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1751 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1752 RangeDestructor(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1753 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1754 Py_DECREF(((RangeObject *)(self))->buf);
986
8fd2e00c44ae updated for version 7.0-112
vimboss
parents: 856
diff changeset
1755 Py_DECREF(self);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1756 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1757
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1758 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1759 RangeGetattr(PyObject *self, char *name)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1760 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1761 if (strcmp(name, "start") == 0)
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1762 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1763 else if (strcmp(name, "end") == 0)
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1764 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1765 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1766 return Py_FindMethod(RangeMethods, self, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1767 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1768
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1769 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1770 RangeRepr(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1771 {
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
1772 static char repr[100];
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1773 RangeObject *this = (RangeObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1774
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1775 if (this->buf->buf == INVALID_BUFFER_VALUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1776 {
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1777 vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>",
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1778 (self));
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1779 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1780 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1781 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1782 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1783 char *name = (char *)this->buf->buf->b_fname;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1784 int len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1785
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1786 if (name == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1787 name = "";
1668
0b796e045c42 updated for version 7.2b-000
vimboss
parents: 1607
diff changeset
1788 len = (int)strlen(name);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1789
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1790 if (len > 45)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1791 name = name + (45 - len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1792
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
1793 vim_snprintf(repr, 100, "<range %s%s (%d:%d)>",
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1794 len > 45 ? "..." : "", name,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1795 this->start, this->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1796
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1797 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1798 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1799 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1800
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1801 /****************/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1802
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1803 static PyInt
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1804 RangeLength(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1805 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1806 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1807 if (CheckBuffer(((RangeObject *)(self))->buf))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1808 return -1; /* ??? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1809
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1810 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1811 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1812
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1813 static PyObject *
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1814 RangeItem(PyObject *self, PyInt n)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1815 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1816 return RBItem(((RangeObject *)(self))->buf, n,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1817 ((RangeObject *)(self))->start,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1818 ((RangeObject *)(self))->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1819 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1820
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1821 static PyObject *
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1822 RangeSlice(PyObject *self, PyInt lo, PyInt hi)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1823 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1824 return RBSlice(((RangeObject *)(self))->buf, lo, hi,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1825 ((RangeObject *)(self))->start,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1826 ((RangeObject *)(self))->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1827 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1828
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1829 static PyInt
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1830 RangeAssItem(PyObject *self, PyInt n, PyObject *val)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1831 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1832 return RBAssItem(((RangeObject *)(self))->buf, n, val,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1833 ((RangeObject *)(self))->start,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1834 ((RangeObject *)(self))->end,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1835 &((RangeObject *)(self))->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1836 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1837
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1838 static PyInt
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1839 RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1840 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1841 return RBAssSlice(((RangeObject *)(self))->buf, lo, hi, val,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1842 ((RangeObject *)(self))->start,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1843 ((RangeObject *)(self))->end,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1844 &((RangeObject *)(self))->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1845 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1846
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1847 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1848 RangeAppend(PyObject *self, PyObject *args)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1849 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1850 return RBAppend(((RangeObject *)(self))->buf, args,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1851 ((RangeObject *)(self))->start,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1852 ((RangeObject *)(self))->end,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1853 &((RangeObject *)(self))->end);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1854 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1855
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1856 /* Buffer list object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1857 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1858
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1859 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1860 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1861 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1862 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1863 BufListObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1864
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1865 static PySequenceMethods BufListAsSeq = {
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1866 (PyInquiry) BufListLength, /* sq_length, len(x) */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1867 (binaryfunc) 0, /* sq_concat, x+y */
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1868 (PyIntArgFunc) 0, /* sq_repeat, x*n */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1869 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1870 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1871 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1872 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1873 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1874
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1875 static PyTypeObject BufListType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1876 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1877 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1878 "buffer list",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1879 sizeof(BufListObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1880 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1881
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1882 (destructor) 0, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1883 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1884 (getattrfunc) 0, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1885 (setattrfunc) 0, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1886 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1887 (reprfunc) 0, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1888
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1889 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1890 &BufListAsSeq, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1891 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1892
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1893 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1894 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1895 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1896 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1897
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1898 /* Buffer list object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1899 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1900
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
1901 static PyInt
1887
fc89a4f98a8b updated for version 7.2-184
vimboss
parents: 1786
diff changeset
1902 BufListLength(PyObject *self UNUSED)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1903 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1904 buf_T *b = firstbuf;
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
1905 PyInt n = 0;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1906
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1907 while (b)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1908 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1909 ++n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1910 b = b->b_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1911 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1912
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1913 return n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1914 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1915
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1916 static PyObject *
1887
fc89a4f98a8b updated for version 7.2-184
vimboss
parents: 1786
diff changeset
1917 BufListItem(PyObject *self UNUSED, PyInt n)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1918 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1919 buf_T *b;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1920
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1921 for (b = firstbuf; b; b = b->b_next, --n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1922 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1923 if (n == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1924 return BufferNew(b);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1925 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1926
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1927 PyErr_SetString(PyExc_IndexError, _("no such buffer"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1928 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1929 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1930
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1931 /* Window object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1932 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1933
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1934 static struct PyMethodDef WindowMethods[] = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1935 /* name, function, calling, documentation */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1936 { NULL, NULL, 0, NULL }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1937 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1938
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1939 static PyTypeObject WindowType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1940 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1941 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1942 "window",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1943 sizeof(WindowObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1944 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1945
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1946 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1947 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1948 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1949 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1950 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1951 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1952
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1953 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1954 0, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1955 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1956
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1957 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1958 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1959 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1960 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1961
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1962 /* Window object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1963 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1964
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1965 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1966 WindowNew(win_T *win)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1967 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1968 /* We need to handle deletion of windows underneath us.
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1969 * If we add a "w_python_ref" field to the win_T structure,
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1970 * then we can get at it in win_free() in vim. We then
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1971 * need to create only ONE Python object per window - if
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1972 * we try to create a second, just INCREF the existing one
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1973 * and return it. The (single) Python object referring to
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1974 * the window is stored in "w_python_ref".
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1975 * On a win_free() we set the Python object's win_T* field
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1976 * to an invalid value. We trap all uses of a window
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1977 * object, and reject them if the win_T* field is invalid.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1978 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1979
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1980 WindowObject *self;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1981
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1982 if (win->w_python_ref)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1983 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1984 self = win->w_python_ref;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1985 Py_INCREF(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1986 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1987 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1988 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1989 self = PyObject_NEW(WindowObject, &WindowType);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1990 if (self == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1991 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1992 self->win = win;
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
1993 win->w_python_ref = self;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1994 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1995
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1996 return (PyObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1997 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1998
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1999 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2000 WindowDestructor(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2001 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2002 WindowObject *this = (WindowObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2003
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2004 if (this->win && this->win != INVALID_WINDOW_VALUE)
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2005 this->win->w_python_ref = NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2006
986
8fd2e00c44ae updated for version 7.0-112
vimboss
parents: 856
diff changeset
2007 Py_DECREF(self);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2008 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2009
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2010 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2011 CheckWindow(WindowObject *this)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2012 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2013 if (this->win == INVALID_WINDOW_VALUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2014 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2015 PyErr_SetVim(_("attempt to refer to deleted window"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2016 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2017 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2018
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2019 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2020 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2021
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2022 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2023 WindowGetattr(PyObject *self, char *name)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2024 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2025 WindowObject *this = (WindowObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2026
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2027 if (CheckWindow(this))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2028 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2029
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2030 if (strcmp(name, "buffer") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2031 return (PyObject *)BufferNew(this->win->w_buffer);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2032 else if (strcmp(name, "cursor") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2033 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2034 pos_T *pos = &this->win->w_cursor;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2035
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2036 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2037 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2038 else if (strcmp(name, "height") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2039 return Py_BuildValue("l", (long)(this->win->w_height));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2040 #ifdef FEAT_VERTSPLIT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2041 else if (strcmp(name, "width") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2042 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2043 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2044 else if (strcmp(name,"__members__") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2045 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2046 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2047 return Py_FindMethod(WindowMethods, self, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2048 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2049
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2050 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2051 WindowSetattr(PyObject *self, char *name, PyObject *val)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2052 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2053 WindowObject *this = (WindowObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2054
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2055 if (CheckWindow(this))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2056 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2057
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2058 if (strcmp(name, "buffer") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2059 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2060 PyErr_SetString(PyExc_TypeError, _("readonly attribute"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2061 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2062 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2063 else if (strcmp(name, "cursor") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2064 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2065 long lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2066 long col;
1971
11c6bda3b53a updated for version 7.2-268
vimboss
parents: 1929
diff changeset
2067 long len;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2068
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2069 if (!PyArg_Parse(val, "(ll)", &lnum, &col))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2070 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2071
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2072 if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2073 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2074 PyErr_SetVim(_("cursor position outside buffer"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2075 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2076 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2077
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2078 /* Check for keyboard interrupts */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2079 if (VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2080 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2081
1971
11c6bda3b53a updated for version 7.2-268
vimboss
parents: 1929
diff changeset
2082 /* When column is out of range silently correct it. */
2100
7d121c69f540 updated for version 7.2.383
Bram Moolenaar <bram@zimbu.org>
parents: 1991
diff changeset
2083 len = (long)STRLEN(ml_get_buf(this->win->w_buffer, lnum, FALSE));
1971
11c6bda3b53a updated for version 7.2-268
vimboss
parents: 1929
diff changeset
2084 if (col > len)
11c6bda3b53a updated for version 7.2-268
vimboss
parents: 1929
diff changeset
2085 col = len;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2086
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2087 this->win->w_cursor.lnum = lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2088 this->win->w_cursor.col = col;
1971
11c6bda3b53a updated for version 7.2-268
vimboss
parents: 1929
diff changeset
2089 #ifdef FEAT_VIRTUALEDIT
11c6bda3b53a updated for version 7.2-268
vimboss
parents: 1929
diff changeset
2090 this->win->w_cursor.coladd = 0;
11c6bda3b53a updated for version 7.2-268
vimboss
parents: 1929
diff changeset
2091 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2092 update_screen(VALID);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2093
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2094 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2095 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2096 else if (strcmp(name, "height") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2097 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2098 int height;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2099 win_T *savewin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2100
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2101 if (!PyArg_Parse(val, "i", &height))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2102 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2103
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2104 #ifdef FEAT_GUI
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2105 need_mouse_correct = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2106 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2107 savewin = curwin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2108 curwin = this->win;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2109 win_setheight(height);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2110 curwin = savewin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2111
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2112 /* Check for keyboard interrupts */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2113 if (VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2114 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2115
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2116 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2117 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2118 #ifdef FEAT_VERTSPLIT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2119 else if (strcmp(name, "width") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2120 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2121 int width;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2122 win_T *savewin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2123
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2124 if (!PyArg_Parse(val, "i", &width))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2125 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2126
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2127 #ifdef FEAT_GUI
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2128 need_mouse_correct = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2129 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2130 savewin = curwin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2131 curwin = this->win;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2132 win_setwidth(width);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2133 curwin = savewin;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2134
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2135 /* Check for keyboard interrupts */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2136 if (VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2137 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2138
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2139 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2140 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2141 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2142 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2143 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2144 PyErr_SetString(PyExc_AttributeError, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2145 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2146 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2147 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2148
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2149 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2150 WindowRepr(PyObject *self)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2151 {
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
2152 static char repr[100];
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2153 WindowObject *this = (WindowObject *)(self);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2154
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2155 if (this->win == INVALID_WINDOW_VALUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2156 {
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2157 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self));
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2158 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2159 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2160 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2161 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2162 int i = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2163 win_T *w;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2164
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2165 for (w = firstwin; w != NULL && w != this->win; w = W_NEXT(w))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2166 ++i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2167
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2168 if (w == NULL)
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2169 vim_snprintf(repr, 100, _("<window object (unknown) at %p>"),
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2170 (self));
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2171 else
274
8fa8d7964cf1 updated for version 7.0073
vimboss
parents: 242
diff changeset
2172 vim_snprintf(repr, 100, _("<window %d>"), i);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2173
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2174 return PyString_FromString(repr);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2175 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2176 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2177
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2178 /* Window list object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2179 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2180
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2181 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2182 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2183 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2184 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2185 WinListObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2186
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2187 static PySequenceMethods WinListAsSeq = {
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2188 (PyInquiry) WinListLength, /* sq_length, len(x) */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2189 (binaryfunc) 0, /* sq_concat, x+y */
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2190 (PyIntArgFunc) 0, /* sq_repeat, x*n */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2191 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2192 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2193 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2194 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2195 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2196
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2197 static PyTypeObject WinListType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2198 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2199 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2200 "window list",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2201 sizeof(WinListObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2202 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2203
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2204 (destructor) 0, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2205 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2206 (getattrfunc) 0, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2207 (setattrfunc) 0, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2208 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2209 (reprfunc) 0, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2210
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2211 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2212 &WinListAsSeq, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2213 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2214
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2215 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2216 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2217 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2218 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2219
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2220 /* Window list object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2221 */
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2222 static PyInt
1887
fc89a4f98a8b updated for version 7.2-184
vimboss
parents: 1786
diff changeset
2223 WinListLength(PyObject *self UNUSED)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2224 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2225 win_T *w = firstwin;
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2226 PyInt n = 0;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2227
671
83a006f81bac updated for version 7.0199
vimboss
parents: 637
diff changeset
2228 while (w != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2229 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2230 ++n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2231 w = W_NEXT(w);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2232 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2233
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2234 return n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2235 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2236
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2237 static PyObject *
1887
fc89a4f98a8b updated for version 7.2-184
vimboss
parents: 1786
diff changeset
2238 WinListItem(PyObject *self UNUSED, PyInt n)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2239 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2240 win_T *w;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2241
671
83a006f81bac updated for version 7.0199
vimboss
parents: 637
diff changeset
2242 for (w = firstwin; w != NULL; w = W_NEXT(w), --n)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2243 if (n == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2244 return WindowNew(w);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2245
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2246 PyErr_SetString(PyExc_IndexError, _("no such window"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2247 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2248 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2249
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2250 /* Current items object - Definitions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2251 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2252
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2253 typedef struct
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2254 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2255 PyObject_HEAD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2256 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2257 CurrentObject;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2258
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2259 static PyTypeObject CurrentType = {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2260 PyObject_HEAD_INIT(0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2261 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2262 "current data",
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2263 sizeof(CurrentObject),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2264 0,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2265
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2266 (destructor) 0, /* tp_dealloc, refcount==0 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2267 (printfunc) 0, /* tp_print, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2268 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2269 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2270 (cmpfunc) 0, /* tp_compare, x>y */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2271 (reprfunc) 0, /* tp_repr, `x`, print x */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2272
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2273 0, /* as number */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2274 0, /* as sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2275 0, /* as mapping */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2276
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2277 (hashfunc) 0, /* tp_hash, dict(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2278 (ternaryfunc) 0, /* tp_call, x() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2279 (reprfunc) 0, /* tp_str, str(x) */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2280 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2281
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2282 /* Current items object - Implementation
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2283 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2284 static PyObject *
1887
fc89a4f98a8b updated for version 7.2-184
vimboss
parents: 1786
diff changeset
2285 CurrentGetattr(PyObject *self UNUSED, char *name)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2286 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2287 if (strcmp(name, "buffer") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2288 return (PyObject *)BufferNew(curbuf);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2289 else if (strcmp(name, "window") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2290 return (PyObject *)WindowNew(curwin);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2291 else if (strcmp(name, "line") == 0)
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2292 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2293 else if (strcmp(name, "range") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2294 return RangeNew(curbuf, RangeStart, RangeEnd);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2295 else if (strcmp(name,"__members__") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2296 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2297 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2298 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2299 PyErr_SetString(PyExc_AttributeError, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2300 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2301 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2302 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2303
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2304 static int
1887
fc89a4f98a8b updated for version 7.2-184
vimboss
parents: 1786
diff changeset
2305 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2306 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2307 if (strcmp(name, "line") == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2308 {
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2309 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2310 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2311
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2312 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2313 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2314 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2315 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2316 PyErr_SetString(PyExc_AttributeError, name);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2317 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2318 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2319 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2320
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2321 /* External interface
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2322 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2323
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2324 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2325 python_buffer_free(buf_T *buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2326 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2327 if (buf->b_python_ref != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2328 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2329 BufferObject *bp = buf->b_python_ref;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2330 bp->buf = INVALID_BUFFER_VALUE;
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2331 buf->b_python_ref = NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2332 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2333 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2334
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2335 #if defined(FEAT_WINDOWS) || defined(PROTO)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2336 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2337 python_window_free(win_T *win)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2338 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2339 if (win->w_python_ref != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2340 {
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2341 WindowObject *wp = win->w_python_ref;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2342 wp->win = INVALID_WINDOW_VALUE;
502
52e76e2b5b65 updated for version 7.0140
vimboss
parents: 456
diff changeset
2343 win->w_python_ref = NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2344 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2345 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2346 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2347
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2348 static BufListObject TheBufferList =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2349 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2350 PyObject_HEAD_INIT(&BufListType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2351 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2352
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2353 static WinListObject TheWindowList =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2354 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2355 PyObject_HEAD_INIT(&WinListType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2356 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2357
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2358 static CurrentObject TheCurrent =
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2359 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2360 PyObject_HEAD_INIT(&CurrentType)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2361 };
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2362
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2363 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2364 PythonMod_Init(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2365 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2366 PyObject *mod;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2367 PyObject *dict;
1748
06317850ad3a updated for version 7.2-045
vimboss
parents: 1681
diff changeset
2368 /* The special value is removed from sys.path in Python_Init(). */
06317850ad3a updated for version 7.2-045
vimboss
parents: 1681
diff changeset
2369 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2370
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2371 /* Fixups... */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2372 BufferType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2373 RangeType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2374 WindowType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2375 BufListType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2376 WinListType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2377 CurrentType.ob_type = &PyType_Type;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2378
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2379 /* Set sys.argv[] to avoid a crash in warn(). */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2380 PySys_SetArgv(1, argv);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2381
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2382 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2383 dict = PyModule_GetDict(mod);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2384
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2385 VimError = Py_BuildValue("s", "vim.error");
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2386
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2387 PyDict_SetItemString(dict, "error", VimError);
135
b5fc81a825a1 updated for version 7.0044
vimboss
parents: 36
diff changeset
2388 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
b5fc81a825a1 updated for version 7.0044
vimboss
parents: 36
diff changeset
2389 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
b5fc81a825a1 updated for version 7.0044
vimboss
parents: 36
diff changeset
2390 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2391
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2392 if (PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2393 return -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2394
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2395 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2396 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2397
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2398 /*************************************************************************
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2399 * 4. Utility functions for handling the interface between Vim and Python.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2400 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2401
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2402 /* Get a line from the specified buffer. The line number is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2403 * in Vim format (1-based). The line is returned as a Python
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2404 * string object.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2405 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2406 static PyObject *
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2407 GetBufferLine(buf_T *buf, PyInt n)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2408 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2409 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2410 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2411
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2412 /* Get a list of lines from the specified buffer. The line numbers
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2413 * are in Vim format (1-based). The range is from lo up to, but not
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2414 * including, hi. The list is returned as a Python list of string objects.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2415 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2416 static PyObject *
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2417 GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2418 {
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2419 PyInt i;
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2420 PyInt n = hi - lo;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2421 PyObject *list = PyList_New(n);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2422
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2423 if (list == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2424 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2425
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2426 for (i = 0; i < n; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2427 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2428 PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2429
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2430 /* Error check - was the Python string creation OK? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2431 if (str == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2432 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2433 Py_DECREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2434 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2435 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2436
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2437 /* Set the list item */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2438 if (PyList_SetItem(list, i, str))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2439 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2440 Py_DECREF(str);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2441 Py_DECREF(list);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2442 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2443 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2444 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2445
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2446 /* The ownership of the Python list is passed to the caller (ie,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2447 * the caller should Py_DECREF() the object when it is finished
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2448 * with it).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2449 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2450
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2451 return list;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2452 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2453
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2454 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2455 * Check if deleting lines made the cursor position invalid.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2456 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2457 * deleted).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2458 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2459 static void
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2460 py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2461 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2462 if (curwin->w_cursor.lnum >= lo)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2463 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2464 /* Adjust the cursor position if it's in/after the changed
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2465 * lines. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2466 if (curwin->w_cursor.lnum >= hi)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2467 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2468 curwin->w_cursor.lnum += extra;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2469 check_cursor_col();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2470 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2471 else if (extra < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2472 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2473 curwin->w_cursor.lnum = lo;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2474 check_cursor();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2475 }
1083
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2476 else
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2477 check_cursor_col();
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2478 changed_cline_bef_curs();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2479 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2480 invalidate_botline();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2481 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2482
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2483 /* Replace a line in the specified buffer. The line number is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2484 * in Vim format (1-based). The replacement line is given as
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2485 * a Python string object. The object is checked for validity
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2486 * and correct format. Errors are returned as a value of FAIL.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2487 * The return value is OK on success.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2488 * If OK is returned and len_change is not NULL, *len_change
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2489 * is set to the change in the buffer length.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2490 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2491 static int
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2492 SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2493 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2494 /* First of all, we check the thpe of the supplied Python object.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2495 * There are three cases:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2496 * 1. NULL, or None - this is a deletion.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2497 * 2. A string - this is a replacement.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2498 * 3. Anything else - this is an error.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2499 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2500 if (line == Py_None || line == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2501 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2502 buf_T *savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2503
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2504 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2505 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2506
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2507 if (u_savedel((linenr_T)n, 1L) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2508 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2509 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2510 PyErr_SetVim(_("cannot delete line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2511 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2512 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2513 if (buf == curwin->w_buffer)
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2514 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
1929
9230c45fdde6 updated for version 7.2-226
vimboss
parents: 1887
diff changeset
2515 deleted_lines_mark((linenr_T)n, 1L);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2516 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2517
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2518 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2519
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2520 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2521 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2522
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2523 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2524 *len_change = -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2525
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2526 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2527 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2528 else if (PyString_Check(line))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2529 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2530 char *save = StringToLine(line);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2531 buf_T *savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2532
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2533 if (save == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2534 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2535
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2536 /* We do not need to free "save" if ml_replace() consumes it. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2537 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2538 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2539
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2540 if (u_savesub((linenr_T)n) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2541 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2542 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2543 vim_free(save);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2544 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2545 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2546 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2547 PyErr_SetVim(_("cannot replace line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2548 vim_free(save);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2549 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2550 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2551 changed_bytes((linenr_T)n, 0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2552
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2553 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2554
1083
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2555 /* Check that the cursor is not beyond the end of the line now. */
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2556 if (buf == curwin->w_buffer)
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2557 check_cursor_col();
1d12208efb37 updated for version 7.0-209
vimboss
parents: 986
diff changeset
2558
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2559 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2560 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2561
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2562 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2563 *len_change = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2564
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2565 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2566 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2567 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2568 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2569 PyErr_BadArgument();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2570 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2571 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2572 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2573
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2574 /* Replace a range of lines in the specified buffer. The line numbers are in
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2575 * Vim format (1-based). The range is from lo up to, but not including, hi.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2576 * The replacement lines are given as a Python list of string objects. The
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2577 * list is checked for validity and correct format. Errors are returned as a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2578 * value of FAIL. The return value is OK on success.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2579 * If OK is returned and len_change is not NULL, *len_change
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2580 * is set to the change in the buffer length.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2581 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2582 static int
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2583 SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2584 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2585 /* First of all, we check the thpe of the supplied Python object.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2586 * There are three cases:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2587 * 1. NULL, or None - this is a deletion.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2588 * 2. A list - this is a replacement.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2589 * 3. Anything else - this is an error.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2590 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2591 if (list == Py_None || list == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2592 {
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2593 PyInt i;
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2594 PyInt n = (int)(hi - lo);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2595 buf_T *savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2596
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2597 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2598 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2599
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2600 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2601 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2602 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2603 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2604 for (i = 0; i < n; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2605 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2606 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2607 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2608 PyErr_SetVim(_("cannot delete line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2609 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2610 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2611 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2612 if (buf == curwin->w_buffer)
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2613 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
1929
9230c45fdde6 updated for version 7.2-226
vimboss
parents: 1887
diff changeset
2614 deleted_lines_mark((linenr_T)lo, (long)i);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2615 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2616
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2617 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2618
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2619 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2620 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2621
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2622 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2623 *len_change = -n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2624
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2625 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2626 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2627 else if (PyList_Check(list))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2628 {
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2629 PyInt i;
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2630 PyInt new_len = PyList_Size(list);
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2631 PyInt old_len = hi - lo;
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2632 PyInt extra = 0; /* lines added to text, can be negative */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2633 char **array;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2634 buf_T *savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2635
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2636 if (new_len == 0) /* avoid allocating zero bytes */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2637 array = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2638 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2639 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2640 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2641 if (array == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2642 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2643 PyErr_NoMemory();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2644 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2645 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2646 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2647
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2648 for (i = 0; i < new_len; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2649 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2650 PyObject *line = PyList_GetItem(list, i);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2651
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2652 array[i] = StringToLine(line);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2653 if (array[i] == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2654 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2655 while (i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2656 vim_free(array[--i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2657 vim_free(array);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2658 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2659 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2660 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2661
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2662 savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2663
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2664 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2665 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2666
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2667 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2668 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2669
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2670 /* If the size of the range is reducing (ie, new_len < old_len) we
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2671 * need to delete some old_len. We do this at the start, by
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2672 * repeatedly deleting line "lo".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2673 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2674 if (!PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2675 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2676 for (i = 0; i < old_len - new_len; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2677 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2678 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2679 PyErr_SetVim(_("cannot delete line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2680 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2681 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2682 extra -= i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2683 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2684
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2685 /* For as long as possible, replace the existing old_len with the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2686 * new old_len. This is a more efficient operation, as it requires
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2687 * less memory allocation and freeing.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2688 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2689 if (!PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2690 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2691 for (i = 0; i < old_len && i < new_len; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2692 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2693 == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2694 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2695 PyErr_SetVim(_("cannot replace line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2696 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2697 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2698 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2699 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2700 i = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2701
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2702 /* Now we may need to insert the remaining new old_len. If we do, we
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2703 * must free the strings as we finish with them (we can't pass the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2704 * responsibility to vim in this case).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2705 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2706 if (!PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2707 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2708 while (i < new_len)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2709 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2710 if (ml_append((linenr_T)(lo + i - 1),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2711 (char_u *)array[i], 0, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2712 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2713 PyErr_SetVim(_("cannot insert line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2714 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2715 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2716 vim_free(array[i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2717 ++i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2718 ++extra;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2719 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2720 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2721
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2722 /* Free any left-over old_len, as a result of an error */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2723 while (i < new_len)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2724 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2725 vim_free(array[i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2726 ++i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2727 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2728
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2729 /* Free the array of old_len. All of its contents have now
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2730 * been dealt with (either freed, or the responsibility passed
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2731 * to vim.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2732 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2733 vim_free(array);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2734
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2735 /* Adjust marks. Invalidate any which lie in the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2736 * changed range, and move any in the remainder of the buffer.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2737 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2738 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2739 (long)MAXLNUM, (long)extra);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2740 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2741
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2742 if (buf == curwin->w_buffer)
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2743 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2744
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2745 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2746
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2747 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2748 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2749
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2750 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2751 *len_change = new_len - old_len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2752
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2753 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2754 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2755 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2756 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2757 PyErr_BadArgument();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2758 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2759 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2760 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2761
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2762 /* Insert a number of lines into the specified buffer after the specifed line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2763 * The line number is in Vim format (1-based). The lines to be inserted are
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2764 * given as a Python list of string objects or as a single string. The lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2765 * to be added are checked for validity and correct format. Errors are
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2766 * returned as a value of FAIL. The return value is OK on success.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2767 * If OK is returned and len_change is not NULL, *len_change
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2768 * is set to the change in the buffer length.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2769 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2770 static int
1607
4f1130893a1c updated for version 7.1-320
vimboss
parents: 1594
diff changeset
2771 InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2772 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2773 /* First of all, we check the type of the supplied Python object.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2774 * It must be a string or a list, or the call is in error.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2775 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2776 if (PyString_Check(lines))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2777 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2778 char *str = StringToLine(lines);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2779 buf_T *savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2780
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2781 if (str == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2782 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2783
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2784 savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2785
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2786 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2787 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2788
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2789 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2790 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2791 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2792 PyErr_SetVim(_("cannot insert line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2793 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2794 appended_lines_mark((linenr_T)n, 1L);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2795
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2796 vim_free(str);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2797 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2798 update_screen(VALID);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2799
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2800 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2801 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2802
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2803 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2804 *len_change = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2805
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2806 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2807 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2808 else if (PyList_Check(lines))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2809 {
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2810 PyInt i;
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2811 PyInt size = PyList_Size(lines);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2812 char **array;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2813 buf_T *savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2814
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2815 array = (char **)alloc((unsigned)(size * sizeof(char *)));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2816 if (array == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2817 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2818 PyErr_NoMemory();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2819 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2820 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2821
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2822 for (i = 0; i < size; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2823 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2824 PyObject *line = PyList_GetItem(lines, i);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2825 array[i] = StringToLine(line);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2826
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2827 if (array[i] == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2828 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2829 while (i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2830 vim_free(array[--i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2831 vim_free(array);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2832 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2833 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2834 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2835
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2836 savebuf = curbuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2837
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2838 PyErr_Clear();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2839 curbuf = buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2840
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2841 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2842 PyErr_SetVim(_("cannot save undo information"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2843 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2844 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2845 for (i = 0; i < size; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2846 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2847 if (ml_append((linenr_T)(n + i),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2848 (char_u *)array[i], 0, FALSE) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2849 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2850 PyErr_SetVim(_("cannot insert line"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2851
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2852 /* Free the rest of the lines */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2853 while (i < size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2854 vim_free(array[i++]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2855
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2856 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2857 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2858 vim_free(array[i]);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2859 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2860 if (i > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2861 appended_lines_mark((linenr_T)n, (long)i);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2862 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2863
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2864 /* Free the array of lines. All of its contents have now
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2865 * been freed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2866 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2867 vim_free(array);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2868
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2869 curbuf = savebuf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2870 update_screen(VALID);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2871
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2872 if (PyErr_Occurred() || VimErrorCheck())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2873 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2874
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2875 if (len_change)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2876 *len_change = size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2877
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2878 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2879 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2880 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2881 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2882 PyErr_BadArgument();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2883 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2884 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2885 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2886
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2887 /* Convert a Vim line into a Python string.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2888 * All internal newlines are replaced by null characters.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2889 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2890 * On errors, the Python exception data is set, and NULL is returned.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2891 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2892 static PyObject *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2893 LineToString(const char *str)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2894 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2895 PyObject *result;
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2896 PyInt len = strlen(str);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2897 char *p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2898
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2899 /* Allocate an Python string object, with uninitialised contents. We
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2900 * must do it this way, so that we can modify the string in place
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2901 * later. See the Python source, Objects/stringobject.c for details.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2902 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2903 result = PyString_FromStringAndSize(NULL, len);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2904 if (result == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2905 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2906
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2907 p = PyString_AsString(result);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2908
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2909 while (*str)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2910 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2911 if (*str == '\n')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2912 *p = '\0';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2913 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2914 *p = *str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2915
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2916 ++p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2917 ++str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2918 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2919
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2920 return result;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2921 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2922
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2923 /* Convert a Python string into a Vim line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2924 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2925 * The result is in allocated memory. All internal nulls are replaced by
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2926 * newline characters. It is an error for the string to contain newline
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2927 * characters.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2928 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2929 * On errors, the Python exception data is set, and NULL is returned.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2930 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2931 static char *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2932 StringToLine(PyObject *obj)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2933 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2934 const char *str;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2935 char *save;
1594
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2936 PyInt len;
23aeaff96141 updated for version 7.1-307
vimboss
parents: 1083
diff changeset
2937 PyInt i;
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2938 char *p;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2939
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2940 if (obj == NULL || !PyString_Check(obj))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2941 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2942 PyErr_BadArgument();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2943 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2944 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2945
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2946 str = PyString_AsString(obj);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2947 len = PyString_Size(obj);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2948
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2949 /*
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2950 * Error checking: String must not contain newlines, as we
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2951 * are replacing a single line, and we must replace it with
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2952 * a single line.
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2953 * A trailing newline is removed, so that append(f.readlines()) works.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2954 */
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2955 p = memchr(str, '\n', len);
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2956 if (p != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2957 {
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2958 if (p == str + len - 1)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2959 --len;
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2960 else
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2961 {
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2962 PyErr_SetVim(_("string cannot contain newlines"));
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2963 return NULL;
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
2964 }
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2965 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2966
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2967 /* Create a copy of the string, with internal nulls replaced by
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2968 * newline characters, as is the vim convention.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2969 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2970 save = (char *)alloc((unsigned)(len+1));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2971 if (save == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2972 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2973 PyErr_NoMemory();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2974 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2975 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2976
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2977 for (i = 0; i < len; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2978 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2979 if (str[i] == '\0')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2980 save[i] = '\n';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2981 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2982 save[i] = str[i];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2983 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2984
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2985 save[i] = '\0';
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2986
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2987 return save;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2988 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2989
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2990 /* Check to see whether a Vim error has been reported, or a keyboard
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2991 * interrupt has been detected.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2992 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2993 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2994 VimErrorCheck(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2995 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2996 if (got_int)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2997 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2998 PyErr_SetNone(PyExc_KeyboardInterrupt);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2999 return 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3000 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3001 else if (did_emsg && !PyErr_Occurred())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3002 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3003 PyErr_SetNone(VimError);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3004 return 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3005 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3006
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3007 return 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3008 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3009
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3010
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3011 /* Don't generate a prototype for the next function, it generates an error on
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3012 * newer Python versions. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3013 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3014
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3015 char *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3016 Py_GetProgramName(void)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3017 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3018 return "vim";
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3019 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3020 #endif /* Python 1.4 */