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