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