blob: 8a28f8bd4e721908cc27b8efc6e5af56c7238130 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#include <limits.h>
23
24/* Python.h defines _POSIX_THREADS itself (if needed) */
25#ifdef _POSIX_THREADS
26# undef _POSIX_THREADS
27#endif
28
Bram Moolenaar860cae12010-06-05 23:22:07 +020029#if defined(_WIN32) && defined(HAVE_FCNTL_H)
Bram Moolenaar071d4272004-06-13 20:20:40 +000030# 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
Bram Moolenaarbe2c9ae2009-11-11 14:06:59 +000040#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
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
Bram Moolenaar2c45e942008-06-04 11:35:26 +000047#define PY_SSIZE_T_CLEAN
48
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#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
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020057static void init_structs(void);
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#if !defined(FEAT_PYTHON) && defined(PROTO)
60/* Use this to be able to generate prototypes without python being used. */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000061# define PyObject Py_ssize_t
62# define PyThreadState Py_ssize_t
63# define PyTypeObject Py_ssize_t
64struct PyMethodDef { Py_ssize_t a; };
65# define PySequenceMethods Py_ssize_t
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#endif
67
Bram Moolenaar2c45e942008-06-04 11:35:26 +000068#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
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000075# define Py_ssize_t_fmt "n"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000076#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
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000083# define Py_ssize_t_fmt "i"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000084#endif
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/* 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
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020096# if defined(DYNAMIC_PYTHON) || defined(PROTO)
97# ifndef DYNAMIC_PYTHON
98# define HINSTANCE long_u /* for generating prototypes */
99# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200101# ifndef WIN3264
102# include <dlfcn.h>
103# define FARPROC void*
104# define HINSTANCE void*
105# ifdef FEAT_PYTHON3
106 /* Don't use RTLD_GLOBAL, it may cause a crash if both :python and :py3 are
107 * used. But without it importing may fail, e.g., for termios. */
108# define load_dll(n) dlopen((n), RTLD_LAZY)
109# else
110# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
111# endif
112# define close_dll dlclose
113# define symbol_from_dll dlsym
114# else
115# define load_dll LoadLibrary
116# define close_dll FreeLibrary
117# define symbol_from_dll GetProcAddress
118# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200119
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000120/* This makes if_python.c compile without warnings against Python 2.5
121 * on Win32 and Win64. */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200122# undef PyRun_SimpleString
123# undef PyArg_Parse
124# undef PyArg_ParseTuple
125# undef Py_BuildValue
126# undef Py_InitModule4
127# undef Py_InitModule4_64
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129/*
130 * Wrapper defines
131 */
132# define PyArg_Parse dll_PyArg_Parse
133# define PyArg_ParseTuple dll_PyArg_ParseTuple
134# define PyDict_SetItemString dll_PyDict_SetItemString
135# define PyErr_BadArgument dll_PyErr_BadArgument
136# define PyErr_Clear dll_PyErr_Clear
137# define PyErr_NoMemory dll_PyErr_NoMemory
138# define PyErr_Occurred dll_PyErr_Occurred
139# define PyErr_SetNone dll_PyErr_SetNone
140# define PyErr_SetString dll_PyErr_SetString
141# define PyEval_InitThreads dll_PyEval_InitThreads
142# define PyEval_RestoreThread dll_PyEval_RestoreThread
143# define PyEval_SaveThread dll_PyEval_SaveThread
144# ifdef PY_CAN_RECURSE
145# define PyGILState_Ensure dll_PyGILState_Ensure
146# define PyGILState_Release dll_PyGILState_Release
147# endif
148# define PyInt_AsLong dll_PyInt_AsLong
149# define PyInt_FromLong dll_PyInt_FromLong
150# define PyInt_Type (*dll_PyInt_Type)
151# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000152# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define PyList_New dll_PyList_New
154# define PyList_SetItem dll_PyList_SetItem
155# define PyList_Size dll_PyList_Size
156# define PyList_Type (*dll_PyList_Type)
157# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000158# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159# define PyDict_GetItemString dll_PyDict_GetItemString
160# define PyModule_GetDict dll_PyModule_GetDict
161# define PyRun_SimpleString dll_PyRun_SimpleString
162# define PyString_AsString dll_PyString_AsString
163# define PyString_FromString dll_PyString_FromString
164# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
165# define PyString_Size dll_PyString_Size
166# define PyString_Type (*dll_PyString_Type)
167# define PySys_SetObject dll_PySys_SetObject
168# define PySys_SetArgv dll_PySys_SetArgv
169# define PyType_Type (*dll_PyType_Type)
170# define Py_BuildValue dll_Py_BuildValue
171# define Py_FindMethod dll_Py_FindMethod
172# define Py_InitModule4 dll_Py_InitModule4
173# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000174# define Py_Finalize dll_Py_Finalize
175# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176# define _PyObject_New dll__PyObject_New
177# define _Py_NoneStruct (*dll__Py_NoneStruct)
178# define PyObject_Init dll__PyObject_Init
179# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
180# define PyType_IsSubtype dll_PyType_IsSubtype
181# endif
182# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
183# define PyObject_Malloc dll_PyObject_Malloc
184# define PyObject_Free dll_PyObject_Free
185# endif
186
187/*
188 * Pointers for dynamic link
189 */
190static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
191static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
192static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
193static int(*dll_PyErr_BadArgument)(void);
194static void(*dll_PyErr_Clear)(void);
195static PyObject*(*dll_PyErr_NoMemory)(void);
196static PyObject*(*dll_PyErr_Occurred)(void);
197static void(*dll_PyErr_SetNone)(PyObject *);
198static void(*dll_PyErr_SetString)(PyObject *, const char *);
199static void(*dll_PyEval_InitThreads)(void);
200static void(*dll_PyEval_RestoreThread)(PyThreadState *);
201static PyThreadState*(*dll_PyEval_SaveThread)(void);
202# ifdef PY_CAN_RECURSE
203static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
204static void (*dll_PyGILState_Release)(PyGILState_STATE);
205#endif
206static long(*dll_PyInt_AsLong)(PyObject *);
207static PyObject*(*dll_PyInt_FromLong)(long);
208static PyTypeObject* dll_PyInt_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000209static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000210static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000211static PyObject*(*dll_PyList_New)(PyInt size);
212static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
213static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214static PyTypeObject* dll_PyList_Type;
215static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000216static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
218static PyObject*(*dll_PyModule_GetDict)(PyObject *);
219static int(*dll_PyRun_SimpleString)(char *);
220static char*(*dll_PyString_AsString)(PyObject *);
221static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000222static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
223static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224static PyTypeObject* dll_PyString_Type;
225static int(*dll_PySys_SetObject)(char *, PyObject *);
226static int(*dll_PySys_SetArgv)(int, char **);
227static PyTypeObject* dll_PyType_Type;
228static PyObject*(*dll_Py_BuildValue)(char *, ...);
229static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
230static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
231static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000232static void(*dll_Py_Finalize)(void);
233static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
235static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
236static PyObject* dll__Py_NoneStruct;
237# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
238static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
239# endif
240# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
241static void* (*dll_PyObject_Malloc)(size_t);
242static void (*dll_PyObject_Free)(void*);
243# endif
244
245static HINSTANCE hinstPython = 0; /* Instance of python.dll */
246
247/* Imported exception objects */
248static PyObject *imp_PyExc_AttributeError;
249static PyObject *imp_PyExc_IndexError;
250static PyObject *imp_PyExc_KeyboardInterrupt;
251static PyObject *imp_PyExc_TypeError;
252static PyObject *imp_PyExc_ValueError;
253
254# define PyExc_AttributeError imp_PyExc_AttributeError
255# define PyExc_IndexError imp_PyExc_IndexError
256# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
257# define PyExc_TypeError imp_PyExc_TypeError
258# define PyExc_ValueError imp_PyExc_ValueError
259
260/*
261 * Table of name to function pointer of python.
262 */
263# define PYTHON_PROC FARPROC
264static struct
265{
266 char *name;
267 PYTHON_PROC *ptr;
268} python_funcname_table[] =
269{
270 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
271 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
272 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
273 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
274 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
275 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
276 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
277 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
278 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
279 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
280 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
281 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
282# ifdef PY_CAN_RECURSE
283 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
284 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
285# endif
286 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
287 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
288 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
289 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000290 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
292 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
293 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
294 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
295 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
296 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000297 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
299 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
300 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
301 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
302 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
303 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
304 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
305 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
306 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
307 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
308 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
309 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000310# if (PY_VERSION_HEX >= 0x02050000) && SIZEOF_SIZE_T != SIZEOF_INT
311 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
312# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000316 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
317 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
319 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
320 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
321# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
322 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
323# endif
324# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
325 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
326 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
327# endif
328 {"", NULL},
329};
330
331/*
332 * Free python.dll
333 */
334 static void
335end_dynamic_python(void)
336{
337 if (hinstPython)
338 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200339 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 hinstPython = 0;
341 }
342}
343
344/*
345 * Load library and get all pointers.
346 * Parameter 'libname' provides name of DLL.
347 * Return OK or FAIL.
348 */
349 static int
350python_runtime_link_init(char *libname, int verbose)
351{
352 int i;
353
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200354#if 0 /* this should be OK now that we don't use RTLD_GLOBAL */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200355#if defined(UNIX) && defined(FEAT_PYTHON3)
356 /* Can't have Python and Python3 loaded at the same time, it may cause a
357 * crash. */
358 if (python3_loaded())
359 {
360 EMSG(_("E999: Python: Cannot use :py and :py3 in one session"));
361 return FAIL;
362 }
363#endif
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200364#endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 if (hinstPython)
367 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200368 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 if (!hinstPython)
370 {
371 if (verbose)
372 EMSG2(_(e_loadlib), libname);
373 return FAIL;
374 }
375
376 for (i = 0; python_funcname_table[i].ptr; ++i)
377 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200378 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 python_funcname_table[i].name)) == NULL)
380 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200381 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 hinstPython = 0;
383 if (verbose)
384 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
385 return FAIL;
386 }
387 }
388 return OK;
389}
390
391/*
392 * If python is enabled (there is installed python on Windows system) return
393 * TRUE, else FALSE.
394 */
395 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000396python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
398 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
399}
400
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200401/*
402 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 * DLL, as this can cause errors (importing data symbols is not reliable).
404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200406get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407{
408 PyObject *exmod = PyImport_ImportModule("exceptions");
409 PyObject *exdict = PyModule_GetDict(exmod);
410 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
411 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
412 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
413 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
414 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
415 Py_XINCREF(imp_PyExc_AttributeError);
416 Py_XINCREF(imp_PyExc_IndexError);
417 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
418 Py_XINCREF(imp_PyExc_TypeError);
419 Py_XINCREF(imp_PyExc_ValueError);
420 Py_XDECREF(exmod);
421}
422#endif /* DYNAMIC_PYTHON */
423
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200424static PyObject *BufferNew (buf_T *);
425static PyObject *WindowNew(win_T *);
426static PyObject *LineToString(const char *);
427
428static PyTypeObject RangeType;
429
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200430/*
431 * Include the code shared with if_python3.c
432 */
433#include "if_py_both.h"
434
435
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436/******************************************************
437 * Internal function prototypes.
438 */
439
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000440static PyInt RangeStart;
441static PyInt RangeEnd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
443static void PythonIO_Flush(void);
444static int PythonIO_Init(void);
445static int PythonMod_Init(void);
446
447/* Utility functions for the vim/python interface
448 * ----------------------------------------------
449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000451static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453
454/******************************************************
455 * 1. Python interpreter main program.
456 */
457
458static int initialised = 0;
459
460#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
461typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000464#ifdef PY_CAN_RECURSE
465static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
466#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000467static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469
470/*
471 * Suspend a thread of the Python interpreter, other threads are allowed to
472 * run.
473 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000474 static void
475Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000477#ifdef PY_CAN_RECURSE
478 PyGILState_Release(pygilstate);
479#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482}
483
484/*
485 * Restore a thread of the Python interpreter, waits for other threads to
486 * block.
487 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000488 static void
489Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000491#ifdef PY_CAN_RECURSE
492 pygilstate = PyGILState_Ensure();
493#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 PyEval_RestoreThread(saved_python_thread);
495 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000497}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 void
500python_end()
501{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000502 static int recurse = 0;
503
504 /* If a crash occurs while doing this, don't try again. */
505 if (recurse != 0)
506 return;
507
508 ++recurse;
509
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000511 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000512 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000513 Python_RestoreThread(); /* enter python */
514 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000517#else
518 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000519 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000520 Python_RestoreThread(); /* enter python */
521 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000524
525 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526}
527
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200528#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
529 int
530python_loaded()
531{
532 return (hinstPython != 0);
533}
534#endif
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 static int
537Python_Init(void)
538{
539 if (!initialised)
540 {
541#ifdef DYNAMIC_PYTHON
542 if (!python_enabled(TRUE))
543 {
544 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
545 goto fail;
546 }
547#endif
548
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200549 init_structs();
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#if !defined(MACOS) || defined(MACOS_X_UNIX)
552 Py_Initialize();
553#else
554 PyMac_Initialize();
555#endif
556 /* initialise threads */
557 PyEval_InitThreads();
558
559#ifdef DYNAMIC_PYTHON
560 get_exceptions();
561#endif
562
563 if (PythonIO_Init())
564 goto fail;
565
566 if (PythonMod_Init())
567 goto fail;
568
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000569 /* Remove the element from sys.path that was added because of our
570 * argv[0] value in PythonMod_Init(). Previously we used an empty
571 * string, but dependinding on the OS we then get an empty entry or
572 * the current directory in sys.path. */
573 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
574
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000575 /* the first python thread is vim's, release the lock */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 Python_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
578 initialised = 1;
579 }
580
581 return 0;
582
583fail:
584 /* We call PythonIO_Flush() here to print any Python errors.
585 * This is OK, as it is possible to call this function even
586 * if PythonIO_Init() has not completed successfully (it will
587 * not do anything in this case).
588 */
589 PythonIO_Flush();
590 return -1;
591}
592
593/*
594 * External interface
595 */
596 static void
597DoPythonCommand(exarg_T *eap, const char *cmd)
598{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000599#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 static int recursive = 0;
601#endif
602#if defined(MACOS) && !defined(MACOS_X_UNIX)
603 GrafPtr oldPort;
604#endif
605#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
606 char *saved_locale;
607#endif
608
609#ifndef PY_CAN_RECURSE
610 if (recursive)
611 {
612 EMSG(_("E659: Cannot invoke Python recursively"));
613 return;
614 }
615 ++recursive;
616#endif
617
618#if defined(MACOS) && !defined(MACOS_X_UNIX)
619 GetPort(&oldPort);
620 /* Check if the Python library is available */
621 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
622 goto theend;
623#endif
624 if (Python_Init())
625 goto theend;
626
627 RangeStart = eap->line1;
628 RangeEnd = eap->line2;
629 Python_Release_Vim(); /* leave vim */
630
631#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
632 /* Python only works properly when the LC_NUMERIC locale is "C". */
633 saved_locale = setlocale(LC_NUMERIC, NULL);
634 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
635 saved_locale = NULL;
636 else
637 {
638 /* Need to make a copy, value may change when setting new locale. */
639 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
640 (void)setlocale(LC_NUMERIC, "C");
641 }
642#endif
643
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 PyRun_SimpleString((char *)(cmd));
647
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
651 if (saved_locale != NULL)
652 {
653 (void)setlocale(LC_NUMERIC, saved_locale);
654 vim_free(saved_locale);
655 }
656#endif
657
658 Python_Lock_Vim(); /* enter vim */
659 PythonIO_Flush();
660#if defined(MACOS) && !defined(MACOS_X_UNIX)
661 SetPort(oldPort);
662#endif
663
664theend:
665#ifndef PY_CAN_RECURSE
666 --recursive;
667#endif
668 return; /* keeps lint happy */
669}
670
671/*
672 * ":python"
673 */
674 void
675ex_python(exarg_T *eap)
676{
677 char_u *script;
678
679 script = script_get(eap, eap->arg);
680 if (!eap->skip)
681 {
682 if (script == NULL)
683 DoPythonCommand(eap, (char *)eap->arg);
684 else
685 DoPythonCommand(eap, (char *)script);
686 }
687 vim_free(script);
688}
689
690#define BUFFER_SIZE 1024
691
692/*
693 * ":pyfile"
694 */
695 void
696ex_pyfile(exarg_T *eap)
697{
698 static char buffer[BUFFER_SIZE];
699 const char *file = (char *)eap->arg;
700 char *p;
701
702 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
703 * stdio file pointer, but Vim and the Python DLL are compiled with
704 * different options under Windows, meaning that stdio pointers aren't
705 * compatible between the two. Yuk.
706 *
707 * Put the string "execfile('file')" into buffer. But, we need to
708 * escape any backslashes or single quotes in the file name, so that
709 * Python won't mangle the file name.
710 */
711 strcpy(buffer, "execfile('");
712 p = buffer + 10; /* size of "execfile('" */
713
714 while (*file && p < buffer + (BUFFER_SIZE - 3))
715 {
716 if (*file == '\\' || *file == '\'')
717 *p++ = '\\';
718 *p++ = *file++;
719 }
720
721 /* If we didn't finish the file name, we hit a buffer overflow */
722 if (*file != '\0')
723 return;
724
725 /* Put in the terminating "')" and a null */
726 *p++ = '\'';
727 *p++ = ')';
728 *p++ = '\0';
729
730 /* Execute the file */
731 DoPythonCommand(eap, buffer);
732}
733
734/******************************************************
735 * 2. Python output stream: writes output via [e]msg().
736 */
737
738/* Implementation functions
739 */
740
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 static PyObject *
742OutputGetattr(PyObject *self, char *name)
743{
744 if (strcmp(name, "softspace") == 0)
745 return PyInt_FromLong(((OutputObject *)(self))->softspace);
746
747 return Py_FindMethod(OutputMethods, self, name);
748}
749
750 static int
751OutputSetattr(PyObject *self, char *name, PyObject *val)
752{
753 if (val == NULL) {
754 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
755 return -1;
756 }
757
758 if (strcmp(name, "softspace") == 0)
759 {
760 if (!PyInt_Check(val)) {
761 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
762 return -1;
763 }
764
765 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
766 return 0;
767 }
768
769 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
770 return -1;
771}
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773/***************/
774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 static int
776PythonIO_Init(void)
777{
778 /* Fixups... */
779 OutputType.ob_type = &PyType_Type;
780
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200781 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782}
783
784/******************************************************
785 * 3. Implementation of the Vim module for Python
786 */
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788/* Window type - Implementation functions
789 * --------------------------------------
790 */
791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794static void WindowDestructor(PyObject *);
795static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
797/* Buffer type - Implementation functions
798 * --------------------------------------
799 */
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803static void BufferDestructor(PyObject *);
804static PyObject *BufferGetattr(PyObject *, char *);
805static PyObject *BufferRepr(PyObject *);
806
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000807static PyInt BufferLength(PyObject *);
808static PyObject *BufferItem(PyObject *, PyInt);
809static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
810static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
811static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813/* Line range type - Implementation functions
814 * --------------------------------------
815 */
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
818
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000819static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
820static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822/* Current objects type - Implementation functions
823 * -----------------------------------------------
824 */
825
826static PyObject *CurrentGetattr(PyObject *, char *);
827static int CurrentSetattr(PyObject *, char *, PyObject *);
828
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829/* Common routines for buffers and line ranges
830 * -------------------------------------------
831 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200832
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000833 static PyInt
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000834RBAssSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000836 PyInt size;
837 PyInt len_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
839 /* Self must be a valid buffer */
840 if (CheckBuffer(self))
841 return -1;
842
843 /* Sort out the slice range */
844 size = end - start + 1;
845
846 if (lo < 0)
847 lo = 0;
848 else if (lo > size)
849 lo = size;
850 if (hi < 0)
851 hi = 0;
852 if (hi < lo)
853 hi = lo;
854 else if (hi > size)
855 hi = size;
856
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200857 if (SetBufferLineList(self->buf, lo + start, hi + start,
858 val, &len_change) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 return -1;
860
861 if (new_end)
862 *new_end = end + len_change;
863
864 return 0;
865}
866
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000868 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000870 (PyIntArgFunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */
871 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
872 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
873 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
874 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875};
876
877static PyTypeObject BufferType = {
878 PyObject_HEAD_INIT(0)
879 0,
880 "buffer",
881 sizeof(BufferObject),
882 0,
883
884 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
885 (printfunc) 0, /* tp_print, print x */
886 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
887 (setattrfunc) 0, /* tp_setattr, x.attr=v */
888 (cmpfunc) 0, /* tp_compare, x>y */
889 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
890
891 0, /* as number */
892 &BufferAsSeq, /* as sequence */
893 0, /* as mapping */
894
895 (hashfunc) 0, /* tp_hash, dict(x) */
896 (ternaryfunc) 0, /* tp_call, x() */
897 (reprfunc) 0, /* tp_str, str(x) */
898};
899
900/* Buffer object - Implementation
901 */
902
903 static PyObject *
904BufferNew(buf_T *buf)
905{
906 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +0000907 * If we add a "b_python_ref" field to the buf_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 * then we can get at it in buf_freeall() in vim. We then
909 * need to create only ONE Python object per buffer - if
910 * we try to create a second, just INCREF the existing one
911 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +0000912 * the buffer is stored in "b_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * Question: what to do on a buf_freeall(). We'll probably
914 * have to either delete the Python object (DECREF it to
915 * zero - a bad idea, as it leaves dangling refs!) or
916 * set the buf_T * value to an invalid value (-1?), which
917 * means we need checks in all access functions... Bah.
918 */
919
920 BufferObject *self;
921
Bram Moolenaare344bea2005-09-01 20:46:49 +0000922 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000924 self = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 Py_INCREF(self);
926 }
927 else
928 {
929 self = PyObject_NEW(BufferObject, &BufferType);
930 if (self == NULL)
931 return NULL;
932 self->buf = buf;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000933 buf->b_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935
936 return (PyObject *)(self);
937}
938
939 static void
940BufferDestructor(PyObject *self)
941{
942 BufferObject *this = (BufferObject *)(self);
943
944 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000945 this->buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
Bram Moolenaar658ada62006-10-03 13:02:36 +0000947 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
950 static PyObject *
951BufferGetattr(PyObject *self, char *name)
952{
953 BufferObject *this = (BufferObject *)(self);
954
955 if (CheckBuffer(this))
956 return NULL;
957
958 if (strcmp(name, "name") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000959 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 else if (strcmp(name, "number") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000961 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 else if (strcmp(name,"__members__") == 0)
963 return Py_BuildValue("[ss]", "name", "number");
964 else
965 return Py_FindMethod(BufferMethods, self, name);
966}
967
968 static PyObject *
969BufferRepr(PyObject *self)
970{
Bram Moolenaar555b2802005-05-19 21:08:39 +0000971 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 BufferObject *this = (BufferObject *)(self);
973
974 if (this->buf == INVALID_BUFFER_VALUE)
975 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000976 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 return PyString_FromString(repr);
978 }
979 else
980 {
981 char *name = (char *)this->buf->b_fname;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000982 PyInt len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 if (name == NULL)
985 name = "";
986 len = strlen(name);
987
988 if (len > 35)
989 name = name + (35 - len);
990
Bram Moolenaar555b2802005-05-19 21:08:39 +0000991 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
993 return PyString_FromString(repr);
994 }
995}
996
997/******************/
998
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000999 static PyInt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000BufferLength(PyObject *self)
1001{
1002 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1003 if (CheckBuffer((BufferObject *)(self)))
1004 return -1; /* ??? */
1005
1006 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
1007}
1008
1009 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001010BufferItem(PyObject *self, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
1012 return RBItem((BufferObject *)(self), n, 1,
1013 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1014}
1015
1016 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001017BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018{
1019 return RBSlice((BufferObject *)(self), lo, hi, 1,
1020 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1021}
1022
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001023 static PyInt
1024BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001026 return RBAsItem((BufferObject *)(self), n, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001027 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 NULL);
1029}
1030
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001031 static PyInt
1032BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
1034 return RBAssSlice((BufferObject *)(self), lo, hi, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001035 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 NULL);
1037}
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001040 (PyInquiry) RangeLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001042 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1043 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1044 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1045 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1046 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047};
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049/* Line range object - Implementation
1050 */
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 static void
1053RangeDestructor(PyObject *self)
1054{
1055 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar658ada62006-10-03 13:02:36 +00001056 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057}
1058
1059 static PyObject *
1060RangeGetattr(PyObject *self, char *name)
1061{
1062 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001063 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001065 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 else
1067 return Py_FindMethod(RangeMethods, self, name);
1068}
1069
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070/****************/
1071
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001072 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001073RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001075 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 ((RangeObject *)(self))->start,
1077 ((RangeObject *)(self))->end,
1078 &((RangeObject *)(self))->end);
1079}
1080
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001081 static PyInt
1082RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084 return RBAssSlice(((RangeObject *)(self))->buf, lo, hi, val,
1085 ((RangeObject *)(self))->start,
1086 ((RangeObject *)(self))->end,
1087 &((RangeObject *)(self))->end);
1088}
1089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090/* Buffer list object - Definitions
1091 */
1092
1093typedef struct
1094{
1095 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001096} BufListObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
1098static PySequenceMethods BufListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001099 (PyInquiry) BufListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001101 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1102 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */
1103 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1104 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1105 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106};
1107
1108static PyTypeObject BufListType = {
1109 PyObject_HEAD_INIT(0)
1110 0,
1111 "buffer list",
1112 sizeof(BufListObject),
1113 0,
1114
1115 (destructor) 0, /* tp_dealloc, refcount==0 */
1116 (printfunc) 0, /* tp_print, print x */
1117 (getattrfunc) 0, /* tp_getattr, x.attr */
1118 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1119 (cmpfunc) 0, /* tp_compare, x>y */
1120 (reprfunc) 0, /* tp_repr, `x`, print x */
1121
1122 0, /* as number */
1123 &BufListAsSeq, /* as sequence */
1124 0, /* as mapping */
1125
1126 (hashfunc) 0, /* tp_hash, dict(x) */
1127 (ternaryfunc) 0, /* tp_call, x() */
1128 (reprfunc) 0, /* tp_str, str(x) */
1129};
1130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131/* Window object - Definitions
1132 */
1133
1134static struct PyMethodDef WindowMethods[] = {
1135 /* name, function, calling, documentation */
1136 { NULL, NULL, 0, NULL }
1137};
1138
1139static PyTypeObject WindowType = {
1140 PyObject_HEAD_INIT(0)
1141 0,
1142 "window",
1143 sizeof(WindowObject),
1144 0,
1145
1146 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
1147 (printfunc) 0, /* tp_print, print x */
1148 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
1149 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
1150 (cmpfunc) 0, /* tp_compare, x>y */
1151 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
1152
1153 0, /* as number */
1154 0, /* as sequence */
1155 0, /* as mapping */
1156
1157 (hashfunc) 0, /* tp_hash, dict(x) */
1158 (ternaryfunc) 0, /* tp_call, x() */
1159 (reprfunc) 0, /* tp_str, str(x) */
1160};
1161
1162/* Window object - Implementation
1163 */
1164
1165 static PyObject *
1166WindowNew(win_T *win)
1167{
1168 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001169 * If we add a "w_python_ref" field to the win_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 * then we can get at it in win_free() in vim. We then
1171 * need to create only ONE Python object per window - if
1172 * we try to create a second, just INCREF the existing one
1173 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001174 * the window is stored in "w_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 * On a win_free() we set the Python object's win_T* field
1176 * to an invalid value. We trap all uses of a window
1177 * object, and reject them if the win_T* field is invalid.
1178 */
1179
1180 WindowObject *self;
1181
Bram Moolenaare344bea2005-09-01 20:46:49 +00001182 if (win->w_python_ref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001184 self = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 Py_INCREF(self);
1186 }
1187 else
1188 {
1189 self = PyObject_NEW(WindowObject, &WindowType);
1190 if (self == NULL)
1191 return NULL;
1192 self->win = win;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001193 win->w_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 }
1195
1196 return (PyObject *)(self);
1197}
1198
1199 static void
1200WindowDestructor(PyObject *self)
1201{
1202 WindowObject *this = (WindowObject *)(self);
1203
1204 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001205 this->win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
Bram Moolenaar658ada62006-10-03 13:02:36 +00001207 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208}
1209
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 static PyObject *
1211WindowGetattr(PyObject *self, char *name)
1212{
1213 WindowObject *this = (WindowObject *)(self);
1214
1215 if (CheckWindow(this))
1216 return NULL;
1217
1218 if (strcmp(name, "buffer") == 0)
1219 return (PyObject *)BufferNew(this->win->w_buffer);
1220 else if (strcmp(name, "cursor") == 0)
1221 {
1222 pos_T *pos = &this->win->w_cursor;
1223
1224 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1225 }
1226 else if (strcmp(name, "height") == 0)
1227 return Py_BuildValue("l", (long)(this->win->w_height));
1228#ifdef FEAT_VERTSPLIT
1229 else if (strcmp(name, "width") == 0)
1230 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1231#endif
1232 else if (strcmp(name,"__members__") == 0)
1233 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1234 else
1235 return Py_FindMethod(WindowMethods, self, name);
1236}
1237
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238/* Window list object - Definitions
1239 */
1240
1241typedef struct
1242{
1243 PyObject_HEAD
1244}
1245WinListObject;
1246
1247static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001248 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001250 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1251 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1252 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1253 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1254 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255};
1256
1257static PyTypeObject WinListType = {
1258 PyObject_HEAD_INIT(0)
1259 0,
1260 "window list",
1261 sizeof(WinListObject),
1262 0,
1263
1264 (destructor) 0, /* tp_dealloc, refcount==0 */
1265 (printfunc) 0, /* tp_print, print x */
1266 (getattrfunc) 0, /* tp_getattr, x.attr */
1267 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1268 (cmpfunc) 0, /* tp_compare, x>y */
1269 (reprfunc) 0, /* tp_repr, `x`, print x */
1270
1271 0, /* as number */
1272 &WinListAsSeq, /* as sequence */
1273 0, /* as mapping */
1274
1275 (hashfunc) 0, /* tp_hash, dict(x) */
1276 (ternaryfunc) 0, /* tp_call, x() */
1277 (reprfunc) 0, /* tp_str, str(x) */
1278};
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280/* Current items object - Definitions
1281 */
1282
1283typedef struct
1284{
1285 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001286} CurrentObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288static PyTypeObject CurrentType = {
1289 PyObject_HEAD_INIT(0)
1290 0,
1291 "current data",
1292 sizeof(CurrentObject),
1293 0,
1294
1295 (destructor) 0, /* tp_dealloc, refcount==0 */
1296 (printfunc) 0, /* tp_print, print x */
1297 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
1298 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
1299 (cmpfunc) 0, /* tp_compare, x>y */
1300 (reprfunc) 0, /* tp_repr, `x`, print x */
1301
1302 0, /* as number */
1303 0, /* as sequence */
1304 0, /* as mapping */
1305
1306 (hashfunc) 0, /* tp_hash, dict(x) */
1307 (ternaryfunc) 0, /* tp_call, x() */
1308 (reprfunc) 0, /* tp_str, str(x) */
1309};
1310
1311/* Current items object - Implementation
1312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001314CurrentGetattr(PyObject *self UNUSED, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315{
1316 if (strcmp(name, "buffer") == 0)
1317 return (PyObject *)BufferNew(curbuf);
1318 else if (strcmp(name, "window") == 0)
1319 return (PyObject *)WindowNew(curwin);
1320 else if (strcmp(name, "line") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001321 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 else if (strcmp(name, "range") == 0)
1323 return RangeNew(curbuf, RangeStart, RangeEnd);
1324 else if (strcmp(name,"__members__") == 0)
1325 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1326 else
1327 {
1328 PyErr_SetString(PyExc_AttributeError, name);
1329 return NULL;
1330 }
1331}
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 static int
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001334CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336 if (strcmp(name, "line") == 0)
1337 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001338 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 return -1;
1340
1341 return 0;
1342 }
1343 else
1344 {
1345 PyErr_SetString(PyExc_AttributeError, name);
1346 return -1;
1347 }
1348}
1349
1350/* External interface
1351 */
1352
1353 void
1354python_buffer_free(buf_T *buf)
1355{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001356 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001358 BufferObject *bp = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001360 buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
1362}
1363
1364#if defined(FEAT_WINDOWS) || defined(PROTO)
1365 void
1366python_window_free(win_T *win)
1367{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001368 if (win->w_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001370 WindowObject *wp = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001372 win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374}
1375#endif
1376
1377static BufListObject TheBufferList =
1378{
1379 PyObject_HEAD_INIT(&BufListType)
1380};
1381
1382static WinListObject TheWindowList =
1383{
1384 PyObject_HEAD_INIT(&WinListType)
1385};
1386
1387static CurrentObject TheCurrent =
1388{
1389 PyObject_HEAD_INIT(&CurrentType)
1390};
1391
1392 static int
1393PythonMod_Init(void)
1394{
1395 PyObject *mod;
1396 PyObject *dict;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001397 /* The special value is removed from sys.path in Python_Init(). */
1398 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
1400 /* Fixups... */
1401 BufferType.ob_type = &PyType_Type;
1402 RangeType.ob_type = &PyType_Type;
1403 WindowType.ob_type = &PyType_Type;
1404 BufListType.ob_type = &PyType_Type;
1405 WinListType.ob_type = &PyType_Type;
1406 CurrentType.ob_type = &PyType_Type;
1407
1408 /* Set sys.argv[] to avoid a crash in warn(). */
1409 PySys_SetArgv(1, argv);
1410
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001411 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 dict = PyModule_GetDict(mod);
1413
1414 VimError = Py_BuildValue("s", "vim.error");
1415
1416 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001417 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
1418 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1419 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421 if (PyErr_Occurred())
1422 return -1;
1423
1424 return 0;
1425}
1426
1427/*************************************************************************
1428 * 4. Utility functions for handling the interface between Vim and Python.
1429 */
1430
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431/* Replace a range of lines in the specified buffer. The line numbers are in
1432 * Vim format (1-based). The range is from lo up to, but not including, hi.
1433 * The replacement lines are given as a Python list of string objects. The
1434 * list is checked for validity and correct format. Errors are returned as a
1435 * value of FAIL. The return value is OK on success.
1436 * If OK is returned and len_change is not NULL, *len_change
1437 * is set to the change in the buffer length.
1438 */
1439 static int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001440SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 /* First of all, we check the thpe of the supplied Python object.
1443 * There are three cases:
1444 * 1. NULL, or None - this is a deletion.
1445 * 2. A list - this is a replacement.
1446 * 3. Anything else - this is an error.
1447 */
1448 if (list == Py_None || list == NULL)
1449 {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001450 PyInt i;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001451 PyInt n = (int)(hi - lo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 buf_T *savebuf = curbuf;
1453
1454 PyErr_Clear();
1455 curbuf = buf;
1456
1457 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
1458 PyErr_SetVim(_("cannot save undo information"));
1459 else
1460 {
1461 for (i = 0; i < n; ++i)
1462 {
1463 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1464 {
1465 PyErr_SetVim(_("cannot delete line"));
1466 break;
1467 }
1468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (buf == curwin->w_buffer)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001470 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001471 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473
1474 curbuf = savebuf;
1475
1476 if (PyErr_Occurred() || VimErrorCheck())
1477 return FAIL;
1478
1479 if (len_change)
1480 *len_change = -n;
1481
1482 return OK;
1483 }
1484 else if (PyList_Check(list))
1485 {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001486 PyInt i;
1487 PyInt new_len = PyList_Size(list);
1488 PyInt old_len = hi - lo;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001489 PyInt extra = 0; /* lines added to text, can be negative */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 char **array;
1491 buf_T *savebuf;
1492
1493 if (new_len == 0) /* avoid allocating zero bytes */
1494 array = NULL;
1495 else
1496 {
1497 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
1498 if (array == NULL)
1499 {
1500 PyErr_NoMemory();
1501 return FAIL;
1502 }
1503 }
1504
1505 for (i = 0; i < new_len; ++i)
1506 {
1507 PyObject *line = PyList_GetItem(list, i);
1508
1509 array[i] = StringToLine(line);
1510 if (array[i] == NULL)
1511 {
1512 while (i)
1513 vim_free(array[--i]);
1514 vim_free(array);
1515 return FAIL;
1516 }
1517 }
1518
1519 savebuf = curbuf;
1520
1521 PyErr_Clear();
1522 curbuf = buf;
1523
1524 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
1525 PyErr_SetVim(_("cannot save undo information"));
1526
1527 /* If the size of the range is reducing (ie, new_len < old_len) we
1528 * need to delete some old_len. We do this at the start, by
1529 * repeatedly deleting line "lo".
1530 */
1531 if (!PyErr_Occurred())
1532 {
1533 for (i = 0; i < old_len - new_len; ++i)
1534 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1535 {
1536 PyErr_SetVim(_("cannot delete line"));
1537 break;
1538 }
1539 extra -= i;
1540 }
1541
1542 /* For as long as possible, replace the existing old_len with the
1543 * new old_len. This is a more efficient operation, as it requires
1544 * less memory allocation and freeing.
1545 */
1546 if (!PyErr_Occurred())
1547 {
1548 for (i = 0; i < old_len && i < new_len; ++i)
1549 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
1550 == FAIL)
1551 {
1552 PyErr_SetVim(_("cannot replace line"));
1553 break;
1554 }
1555 }
1556 else
1557 i = 0;
1558
1559 /* Now we may need to insert the remaining new old_len. If we do, we
1560 * must free the strings as we finish with them (we can't pass the
1561 * responsibility to vim in this case).
1562 */
1563 if (!PyErr_Occurred())
1564 {
1565 while (i < new_len)
1566 {
1567 if (ml_append((linenr_T)(lo + i - 1),
1568 (char_u *)array[i], 0, FALSE) == FAIL)
1569 {
1570 PyErr_SetVim(_("cannot insert line"));
1571 break;
1572 }
1573 vim_free(array[i]);
1574 ++i;
1575 ++extra;
1576 }
1577 }
1578
1579 /* Free any left-over old_len, as a result of an error */
1580 while (i < new_len)
1581 {
1582 vim_free(array[i]);
1583 ++i;
1584 }
1585
1586 /* Free the array of old_len. All of its contents have now
1587 * been dealt with (either freed, or the responsibility passed
1588 * to vim.
1589 */
1590 vim_free(array);
1591
1592 /* Adjust marks. Invalidate any which lie in the
1593 * changed range, and move any in the remainder of the buffer.
1594 */
1595 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
1596 (long)MAXLNUM, (long)extra);
1597 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
1598
1599 if (buf == curwin->w_buffer)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001600 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
1602 curbuf = savebuf;
1603
1604 if (PyErr_Occurred() || VimErrorCheck())
1605 return FAIL;
1606
1607 if (len_change)
1608 *len_change = new_len - old_len;
1609
1610 return OK;
1611 }
1612 else
1613 {
1614 PyErr_BadArgument();
1615 return FAIL;
1616 }
1617}
1618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619/* Convert a Vim line into a Python string.
1620 * All internal newlines are replaced by null characters.
1621 *
1622 * On errors, the Python exception data is set, and NULL is returned.
1623 */
1624 static PyObject *
1625LineToString(const char *str)
1626{
1627 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001628 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 char *p;
1630
1631 /* Allocate an Python string object, with uninitialised contents. We
1632 * must do it this way, so that we can modify the string in place
1633 * later. See the Python source, Objects/stringobject.c for details.
1634 */
1635 result = PyString_FromStringAndSize(NULL, len);
1636 if (result == NULL)
1637 return NULL;
1638
1639 p = PyString_AsString(result);
1640
1641 while (*str)
1642 {
1643 if (*str == '\n')
1644 *p = '\0';
1645 else
1646 *p = *str;
1647
1648 ++p;
1649 ++str;
1650 }
1651
1652 return result;
1653}
1654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
1656/* Don't generate a prototype for the next function, it generates an error on
1657 * newer Python versions. */
1658#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1659
1660 char *
1661Py_GetProgramName(void)
1662{
1663 return "vim";
1664}
1665#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001666
1667 static void
1668init_structs(void)
1669{
1670 vim_memset(&OutputType, 0, sizeof(OutputType));
1671 OutputType.tp_name = "message";
1672 OutputType.tp_basicsize = sizeof(OutputObject);
1673 OutputType.tp_getattr = OutputGetattr;
1674 OutputType.tp_setattr = OutputSetattr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001675
1676 vim_memset(&RangeType, 0, sizeof(RangeType));
1677 RangeType.tp_name = "range";
1678 RangeType.tp_basicsize = sizeof(RangeObject);
1679 RangeType.tp_dealloc = RangeDestructor;
1680 RangeType.tp_getattr = RangeGetattr;
1681 RangeType.tp_repr = RangeRepr;
1682 RangeType.tp_as_sequence = &RangeAsSeq;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001683}