blob: fc493ecaa3109c2435e46d587cae40f61522c50d [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*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100105# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200106# define load_dll(n) dlopen((n), RTLD_LAZY)
107# else
108# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
109# endif
110# define close_dll dlclose
111# define symbol_from_dll dlsym
112# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200113# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200114# define close_dll FreeLibrary
115# define symbol_from_dll GetProcAddress
116# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200117
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000118/* This makes if_python.c compile without warnings against Python 2.5
119 * on Win32 and Win64. */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200120# undef PyRun_SimpleString
121# undef PyArg_Parse
122# undef PyArg_ParseTuple
123# undef Py_BuildValue
124# undef Py_InitModule4
125# undef Py_InitModule4_64
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000126
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127/*
128 * Wrapper defines
129 */
130# define PyArg_Parse dll_PyArg_Parse
131# define PyArg_ParseTuple dll_PyArg_ParseTuple
132# define PyDict_SetItemString dll_PyDict_SetItemString
133# define PyErr_BadArgument dll_PyErr_BadArgument
134# define PyErr_Clear dll_PyErr_Clear
135# define PyErr_NoMemory dll_PyErr_NoMemory
136# define PyErr_Occurred dll_PyErr_Occurred
137# define PyErr_SetNone dll_PyErr_SetNone
138# define PyErr_SetString dll_PyErr_SetString
139# define PyEval_InitThreads dll_PyEval_InitThreads
140# define PyEval_RestoreThread dll_PyEval_RestoreThread
141# define PyEval_SaveThread dll_PyEval_SaveThread
142# ifdef PY_CAN_RECURSE
143# define PyGILState_Ensure dll_PyGILState_Ensure
144# define PyGILState_Release dll_PyGILState_Release
145# endif
146# define PyInt_AsLong dll_PyInt_AsLong
147# define PyInt_FromLong dll_PyInt_FromLong
148# define PyInt_Type (*dll_PyInt_Type)
149# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000150# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define PyList_New dll_PyList_New
152# define PyList_SetItem dll_PyList_SetItem
153# define PyList_Size dll_PyList_Size
154# define PyList_Type (*dll_PyList_Type)
155# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000156# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157# define PyDict_GetItemString dll_PyDict_GetItemString
158# define PyModule_GetDict dll_PyModule_GetDict
159# define PyRun_SimpleString dll_PyRun_SimpleString
160# define PyString_AsString dll_PyString_AsString
161# define PyString_FromString dll_PyString_FromString
162# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
163# define PyString_Size dll_PyString_Size
164# define PyString_Type (*dll_PyString_Type)
165# define PySys_SetObject dll_PySys_SetObject
166# define PySys_SetArgv dll_PySys_SetArgv
167# define PyType_Type (*dll_PyType_Type)
168# define Py_BuildValue dll_Py_BuildValue
169# define Py_FindMethod dll_Py_FindMethod
170# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100171# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000173# define Py_Finalize dll_Py_Finalize
174# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175# define _PyObject_New dll__PyObject_New
176# define _Py_NoneStruct (*dll__Py_NoneStruct)
177# define PyObject_Init dll__PyObject_Init
178# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
179# define PyType_IsSubtype dll_PyType_IsSubtype
180# endif
181# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
182# define PyObject_Malloc dll_PyObject_Malloc
183# define PyObject_Free dll_PyObject_Free
184# endif
185
186/*
187 * Pointers for dynamic link
188 */
189static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
190static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
191static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
192static int(*dll_PyErr_BadArgument)(void);
193static void(*dll_PyErr_Clear)(void);
194static PyObject*(*dll_PyErr_NoMemory)(void);
195static PyObject*(*dll_PyErr_Occurred)(void);
196static void(*dll_PyErr_SetNone)(PyObject *);
197static void(*dll_PyErr_SetString)(PyObject *, const char *);
198static void(*dll_PyEval_InitThreads)(void);
199static void(*dll_PyEval_RestoreThread)(PyThreadState *);
200static PyThreadState*(*dll_PyEval_SaveThread)(void);
201# ifdef PY_CAN_RECURSE
202static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
203static void (*dll_PyGILState_Release)(PyGILState_STATE);
204#endif
205static long(*dll_PyInt_AsLong)(PyObject *);
206static PyObject*(*dll_PyInt_FromLong)(long);
207static PyTypeObject* dll_PyInt_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000208static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000209static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000210static PyObject*(*dll_PyList_New)(PyInt size);
211static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
212static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213static PyTypeObject* dll_PyList_Type;
214static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000215static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
217static PyObject*(*dll_PyModule_GetDict)(PyObject *);
218static int(*dll_PyRun_SimpleString)(char *);
219static char*(*dll_PyString_AsString)(PyObject *);
220static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000221static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
222static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223static PyTypeObject* dll_PyString_Type;
224static int(*dll_PySys_SetObject)(char *, PyObject *);
225static int(*dll_PySys_SetArgv)(int, char **);
226static PyTypeObject* dll_PyType_Type;
227static PyObject*(*dll_Py_BuildValue)(char *, ...);
228static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
229static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100230static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231static 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 Moolenaar644d37b2010-11-16 19:26:02 +0100315 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000317 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
318 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
320 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
321 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
322# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
323 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
324# endif
325# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
326 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
327 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
328# endif
329 {"", NULL},
330};
331
332/*
333 * Free python.dll
334 */
335 static void
336end_dynamic_python(void)
337{
338 if (hinstPython)
339 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200340 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 hinstPython = 0;
342 }
343}
344
345/*
346 * Load library and get all pointers.
347 * Parameter 'libname' provides name of DLL.
348 * Return OK or FAIL.
349 */
350 static int
351python_runtime_link_init(char *libname, int verbose)
352{
353 int i;
354
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100355#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200356 /* Can't have Python and Python3 loaded at the same time.
357 * It cause a crash, because RTLD_GLOBAL is needed for
358 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200359 if (python3_loaded())
360 {
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200361 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200362 return FAIL;
363 }
364#endif
365
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 Moolenaar644d37b2010-11-16 19:26:02 +0100549#ifdef PYTHON_HOME
550 Py_SetPythonHome(PYTHON_HOME);
551#endif
552
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200553 init_structs();
554
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555#if !defined(MACOS) || defined(MACOS_X_UNIX)
556 Py_Initialize();
557#else
558 PyMac_Initialize();
559#endif
560 /* initialise threads */
561 PyEval_InitThreads();
562
563#ifdef DYNAMIC_PYTHON
564 get_exceptions();
565#endif
566
567 if (PythonIO_Init())
568 goto fail;
569
570 if (PythonMod_Init())
571 goto fail;
572
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000573 /* Remove the element from sys.path that was added because of our
574 * argv[0] value in PythonMod_Init(). Previously we used an empty
575 * string, but dependinding on the OS we then get an empty entry or
576 * the current directory in sys.path. */
577 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
578
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000579 /* the first python thread is vim's, release the lock */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 Python_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
582 initialised = 1;
583 }
584
585 return 0;
586
587fail:
588 /* We call PythonIO_Flush() here to print any Python errors.
589 * This is OK, as it is possible to call this function even
590 * if PythonIO_Init() has not completed successfully (it will
591 * not do anything in this case).
592 */
593 PythonIO_Flush();
594 return -1;
595}
596
597/*
598 * External interface
599 */
600 static void
601DoPythonCommand(exarg_T *eap, const char *cmd)
602{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000603#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 static int recursive = 0;
605#endif
606#if defined(MACOS) && !defined(MACOS_X_UNIX)
607 GrafPtr oldPort;
608#endif
609#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
610 char *saved_locale;
611#endif
612
613#ifndef PY_CAN_RECURSE
614 if (recursive)
615 {
616 EMSG(_("E659: Cannot invoke Python recursively"));
617 return;
618 }
619 ++recursive;
620#endif
621
622#if defined(MACOS) && !defined(MACOS_X_UNIX)
623 GetPort(&oldPort);
624 /* Check if the Python library is available */
625 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
626 goto theend;
627#endif
628 if (Python_Init())
629 goto theend;
630
631 RangeStart = eap->line1;
632 RangeEnd = eap->line2;
633 Python_Release_Vim(); /* leave vim */
634
635#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
636 /* Python only works properly when the LC_NUMERIC locale is "C". */
637 saved_locale = setlocale(LC_NUMERIC, NULL);
638 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
639 saved_locale = NULL;
640 else
641 {
642 /* Need to make a copy, value may change when setting new locale. */
643 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
644 (void)setlocale(LC_NUMERIC, "C");
645 }
646#endif
647
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 PyRun_SimpleString((char *)(cmd));
651
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
654#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
655 if (saved_locale != NULL)
656 {
657 (void)setlocale(LC_NUMERIC, saved_locale);
658 vim_free(saved_locale);
659 }
660#endif
661
662 Python_Lock_Vim(); /* enter vim */
663 PythonIO_Flush();
664#if defined(MACOS) && !defined(MACOS_X_UNIX)
665 SetPort(oldPort);
666#endif
667
668theend:
669#ifndef PY_CAN_RECURSE
670 --recursive;
671#endif
672 return; /* keeps lint happy */
673}
674
675/*
676 * ":python"
677 */
678 void
679ex_python(exarg_T *eap)
680{
681 char_u *script;
682
683 script = script_get(eap, eap->arg);
684 if (!eap->skip)
685 {
686 if (script == NULL)
687 DoPythonCommand(eap, (char *)eap->arg);
688 else
689 DoPythonCommand(eap, (char *)script);
690 }
691 vim_free(script);
692}
693
694#define BUFFER_SIZE 1024
695
696/*
697 * ":pyfile"
698 */
699 void
700ex_pyfile(exarg_T *eap)
701{
702 static char buffer[BUFFER_SIZE];
703 const char *file = (char *)eap->arg;
704 char *p;
705
706 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
707 * stdio file pointer, but Vim and the Python DLL are compiled with
708 * different options under Windows, meaning that stdio pointers aren't
709 * compatible between the two. Yuk.
710 *
711 * Put the string "execfile('file')" into buffer. But, we need to
712 * escape any backslashes or single quotes in the file name, so that
713 * Python won't mangle the file name.
714 */
715 strcpy(buffer, "execfile('");
716 p = buffer + 10; /* size of "execfile('" */
717
718 while (*file && p < buffer + (BUFFER_SIZE - 3))
719 {
720 if (*file == '\\' || *file == '\'')
721 *p++ = '\\';
722 *p++ = *file++;
723 }
724
725 /* If we didn't finish the file name, we hit a buffer overflow */
726 if (*file != '\0')
727 return;
728
729 /* Put in the terminating "')" and a null */
730 *p++ = '\'';
731 *p++ = ')';
732 *p++ = '\0';
733
734 /* Execute the file */
735 DoPythonCommand(eap, buffer);
736}
737
738/******************************************************
739 * 2. Python output stream: writes output via [e]msg().
740 */
741
742/* Implementation functions
743 */
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 static PyObject *
746OutputGetattr(PyObject *self, char *name)
747{
748 if (strcmp(name, "softspace") == 0)
749 return PyInt_FromLong(((OutputObject *)(self))->softspace);
750
751 return Py_FindMethod(OutputMethods, self, name);
752}
753
754 static int
755OutputSetattr(PyObject *self, char *name, PyObject *val)
756{
757 if (val == NULL) {
758 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
759 return -1;
760 }
761
762 if (strcmp(name, "softspace") == 0)
763 {
764 if (!PyInt_Check(val)) {
765 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
766 return -1;
767 }
768
769 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
770 return 0;
771 }
772
773 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
774 return -1;
775}
776
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777/***************/
778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 static int
780PythonIO_Init(void)
781{
782 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +0100783 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200785 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787
788/******************************************************
789 * 3. Implementation of the Vim module for Python
790 */
791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792/* Window type - Implementation functions
793 * --------------------------------------
794 */
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798static void WindowDestructor(PyObject *);
799static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801/* Buffer type - Implementation functions
802 * --------------------------------------
803 */
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807static void BufferDestructor(PyObject *);
808static PyObject *BufferGetattr(PyObject *, char *);
809static PyObject *BufferRepr(PyObject *);
810
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000811static PyInt BufferLength(PyObject *);
812static PyObject *BufferItem(PyObject *, PyInt);
813static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
814static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
815static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817/* Line range type - Implementation functions
818 * --------------------------------------
819 */
820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
822
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000823static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
824static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826/* Current objects type - Implementation functions
827 * -----------------------------------------------
828 */
829
830static PyObject *CurrentGetattr(PyObject *, char *);
831static int CurrentSetattr(PyObject *, char *, PyObject *);
832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833/* Common routines for buffers and line ranges
834 * -------------------------------------------
835 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200836
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000837 static PyInt
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000838RBAssSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000840 PyInt size;
841 PyInt len_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
843 /* Self must be a valid buffer */
844 if (CheckBuffer(self))
845 return -1;
846
847 /* Sort out the slice range */
848 size = end - start + 1;
849
850 if (lo < 0)
851 lo = 0;
852 else if (lo > size)
853 lo = size;
854 if (hi < 0)
855 hi = 0;
856 if (hi < lo)
857 hi = lo;
858 else if (hi > size)
859 hi = size;
860
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200861 if (SetBufferLineList(self->buf, lo + start, hi + start,
862 val, &len_change) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 return -1;
864
865 if (new_end)
866 *new_end = end + len_change;
867
868 return 0;
869}
870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000872 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000874 (PyIntArgFunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */
875 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
876 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
877 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
878 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879};
880
881static PyTypeObject BufferType = {
882 PyObject_HEAD_INIT(0)
883 0,
884 "buffer",
885 sizeof(BufferObject),
886 0,
887
888 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
889 (printfunc) 0, /* tp_print, print x */
890 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
891 (setattrfunc) 0, /* tp_setattr, x.attr=v */
892 (cmpfunc) 0, /* tp_compare, x>y */
893 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
894
895 0, /* as number */
896 &BufferAsSeq, /* as sequence */
897 0, /* as mapping */
898
899 (hashfunc) 0, /* tp_hash, dict(x) */
900 (ternaryfunc) 0, /* tp_call, x() */
901 (reprfunc) 0, /* tp_str, str(x) */
902};
903
904/* Buffer object - Implementation
905 */
906
907 static PyObject *
908BufferNew(buf_T *buf)
909{
910 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +0000911 * If we add a "b_python_ref" field to the buf_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 * then we can get at it in buf_freeall() in vim. We then
913 * need to create only ONE Python object per buffer - if
914 * we try to create a second, just INCREF the existing one
915 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +0000916 * the buffer is stored in "b_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 * Question: what to do on a buf_freeall(). We'll probably
918 * have to either delete the Python object (DECREF it to
919 * zero - a bad idea, as it leaves dangling refs!) or
920 * set the buf_T * value to an invalid value (-1?), which
921 * means we need checks in all access functions... Bah.
922 */
923
924 BufferObject *self;
925
Bram Moolenaare344bea2005-09-01 20:46:49 +0000926 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000928 self = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 Py_INCREF(self);
930 }
931 else
932 {
933 self = PyObject_NEW(BufferObject, &BufferType);
934 if (self == NULL)
935 return NULL;
936 self->buf = buf;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000937 buf->b_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
939
940 return (PyObject *)(self);
941}
942
943 static void
944BufferDestructor(PyObject *self)
945{
946 BufferObject *this = (BufferObject *)(self);
947
948 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000949 this->buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
Bram Moolenaar658ada62006-10-03 13:02:36 +0000951 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952}
953
954 static PyObject *
955BufferGetattr(PyObject *self, char *name)
956{
957 BufferObject *this = (BufferObject *)(self);
958
959 if (CheckBuffer(this))
960 return NULL;
961
962 if (strcmp(name, "name") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000963 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 else if (strcmp(name, "number") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000965 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 else if (strcmp(name,"__members__") == 0)
967 return Py_BuildValue("[ss]", "name", "number");
968 else
969 return Py_FindMethod(BufferMethods, self, name);
970}
971
972 static PyObject *
973BufferRepr(PyObject *self)
974{
Bram Moolenaar555b2802005-05-19 21:08:39 +0000975 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 BufferObject *this = (BufferObject *)(self);
977
978 if (this->buf == INVALID_BUFFER_VALUE)
979 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000980 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 return PyString_FromString(repr);
982 }
983 else
984 {
985 char *name = (char *)this->buf->b_fname;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000986 PyInt len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 if (name == NULL)
989 name = "";
990 len = strlen(name);
991
992 if (len > 35)
993 name = name + (35 - len);
994
Bram Moolenaar555b2802005-05-19 21:08:39 +0000995 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
997 return PyString_FromString(repr);
998 }
999}
1000
1001/******************/
1002
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001003 static PyInt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004BufferLength(PyObject *self)
1005{
1006 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1007 if (CheckBuffer((BufferObject *)(self)))
1008 return -1; /* ??? */
1009
1010 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
1011}
1012
1013 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001014BufferItem(PyObject *self, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015{
1016 return RBItem((BufferObject *)(self), n, 1,
1017 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1018}
1019
1020 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001021BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
1023 return RBSlice((BufferObject *)(self), lo, hi, 1,
1024 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1025}
1026
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001027 static PyInt
1028BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001030 return RBAsItem((BufferObject *)(self), n, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001031 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 NULL);
1033}
1034
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001035 static PyInt
1036BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
1038 return RBAssSlice((BufferObject *)(self), lo, hi, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001039 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 NULL);
1041}
1042
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001044 (PyInquiry) RangeLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001046 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1047 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1048 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1049 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1050 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051};
1052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053/* Line range object - Implementation
1054 */
1055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 static void
1057RangeDestructor(PyObject *self)
1058{
1059 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar658ada62006-10-03 13:02:36 +00001060 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061}
1062
1063 static PyObject *
1064RangeGetattr(PyObject *self, char *name)
1065{
1066 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001067 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001069 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 else
1071 return Py_FindMethod(RangeMethods, self, name);
1072}
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074/****************/
1075
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001076 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001077RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001079 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 ((RangeObject *)(self))->start,
1081 ((RangeObject *)(self))->end,
1082 &((RangeObject *)(self))->end);
1083}
1084
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001085 static PyInt
1086RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
1088 return RBAssSlice(((RangeObject *)(self))->buf, lo, hi, val,
1089 ((RangeObject *)(self))->start,
1090 ((RangeObject *)(self))->end,
1091 &((RangeObject *)(self))->end);
1092}
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094/* Buffer list object - Definitions
1095 */
1096
1097typedef struct
1098{
1099 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001100} BufListObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
1102static PySequenceMethods BufListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001103 (PyInquiry) BufListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001105 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1106 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */
1107 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1108 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1109 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110};
1111
1112static PyTypeObject BufListType = {
1113 PyObject_HEAD_INIT(0)
1114 0,
1115 "buffer list",
1116 sizeof(BufListObject),
1117 0,
1118
1119 (destructor) 0, /* tp_dealloc, refcount==0 */
1120 (printfunc) 0, /* tp_print, print x */
1121 (getattrfunc) 0, /* tp_getattr, x.attr */
1122 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1123 (cmpfunc) 0, /* tp_compare, x>y */
1124 (reprfunc) 0, /* tp_repr, `x`, print x */
1125
1126 0, /* as number */
1127 &BufListAsSeq, /* as sequence */
1128 0, /* as mapping */
1129
1130 (hashfunc) 0, /* tp_hash, dict(x) */
1131 (ternaryfunc) 0, /* tp_call, x() */
1132 (reprfunc) 0, /* tp_str, str(x) */
1133};
1134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135/* Window object - Definitions
1136 */
1137
1138static struct PyMethodDef WindowMethods[] = {
1139 /* name, function, calling, documentation */
1140 { NULL, NULL, 0, NULL }
1141};
1142
1143static PyTypeObject WindowType = {
1144 PyObject_HEAD_INIT(0)
1145 0,
1146 "window",
1147 sizeof(WindowObject),
1148 0,
1149
1150 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
1151 (printfunc) 0, /* tp_print, print x */
1152 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
1153 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
1154 (cmpfunc) 0, /* tp_compare, x>y */
1155 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
1156
1157 0, /* as number */
1158 0, /* as sequence */
1159 0, /* as mapping */
1160
1161 (hashfunc) 0, /* tp_hash, dict(x) */
1162 (ternaryfunc) 0, /* tp_call, x() */
1163 (reprfunc) 0, /* tp_str, str(x) */
1164};
1165
1166/* Window object - Implementation
1167 */
1168
1169 static PyObject *
1170WindowNew(win_T *win)
1171{
1172 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001173 * If we add a "w_python_ref" field to the win_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 * then we can get at it in win_free() in vim. We then
1175 * need to create only ONE Python object per window - if
1176 * we try to create a second, just INCREF the existing one
1177 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001178 * the window is stored in "w_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 * On a win_free() we set the Python object's win_T* field
1180 * to an invalid value. We trap all uses of a window
1181 * object, and reject them if the win_T* field is invalid.
1182 */
1183
1184 WindowObject *self;
1185
Bram Moolenaare344bea2005-09-01 20:46:49 +00001186 if (win->w_python_ref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001188 self = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 Py_INCREF(self);
1190 }
1191 else
1192 {
1193 self = PyObject_NEW(WindowObject, &WindowType);
1194 if (self == NULL)
1195 return NULL;
1196 self->win = win;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001197 win->w_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
1199
1200 return (PyObject *)(self);
1201}
1202
1203 static void
1204WindowDestructor(PyObject *self)
1205{
1206 WindowObject *this = (WindowObject *)(self);
1207
1208 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001209 this->win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
Bram Moolenaar658ada62006-10-03 13:02:36 +00001211 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212}
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 static PyObject *
1215WindowGetattr(PyObject *self, char *name)
1216{
1217 WindowObject *this = (WindowObject *)(self);
1218
1219 if (CheckWindow(this))
1220 return NULL;
1221
1222 if (strcmp(name, "buffer") == 0)
1223 return (PyObject *)BufferNew(this->win->w_buffer);
1224 else if (strcmp(name, "cursor") == 0)
1225 {
1226 pos_T *pos = &this->win->w_cursor;
1227
1228 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1229 }
1230 else if (strcmp(name, "height") == 0)
1231 return Py_BuildValue("l", (long)(this->win->w_height));
1232#ifdef FEAT_VERTSPLIT
1233 else if (strcmp(name, "width") == 0)
1234 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1235#endif
1236 else if (strcmp(name,"__members__") == 0)
1237 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1238 else
1239 return Py_FindMethod(WindowMethods, self, name);
1240}
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242/* Window list object - Definitions
1243 */
1244
1245typedef struct
1246{
1247 PyObject_HEAD
1248}
1249WinListObject;
1250
1251static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001252 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001254 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1255 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1256 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1257 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1258 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259};
1260
1261static PyTypeObject WinListType = {
1262 PyObject_HEAD_INIT(0)
1263 0,
1264 "window list",
1265 sizeof(WinListObject),
1266 0,
1267
1268 (destructor) 0, /* tp_dealloc, refcount==0 */
1269 (printfunc) 0, /* tp_print, print x */
1270 (getattrfunc) 0, /* tp_getattr, x.attr */
1271 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1272 (cmpfunc) 0, /* tp_compare, x>y */
1273 (reprfunc) 0, /* tp_repr, `x`, print x */
1274
1275 0, /* as number */
1276 &WinListAsSeq, /* as sequence */
1277 0, /* as mapping */
1278
1279 (hashfunc) 0, /* tp_hash, dict(x) */
1280 (ternaryfunc) 0, /* tp_call, x() */
1281 (reprfunc) 0, /* tp_str, str(x) */
1282};
1283
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284/* Current items object - Definitions
1285 */
1286
1287typedef struct
1288{
1289 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001290} CurrentObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
1292static PyTypeObject CurrentType = {
1293 PyObject_HEAD_INIT(0)
1294 0,
1295 "current data",
1296 sizeof(CurrentObject),
1297 0,
1298
1299 (destructor) 0, /* tp_dealloc, refcount==0 */
1300 (printfunc) 0, /* tp_print, print x */
1301 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
1302 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
1303 (cmpfunc) 0, /* tp_compare, x>y */
1304 (reprfunc) 0, /* tp_repr, `x`, print x */
1305
1306 0, /* as number */
1307 0, /* as sequence */
1308 0, /* as mapping */
1309
1310 (hashfunc) 0, /* tp_hash, dict(x) */
1311 (ternaryfunc) 0, /* tp_call, x() */
1312 (reprfunc) 0, /* tp_str, str(x) */
1313};
1314
1315/* Current items object - Implementation
1316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001318CurrentGetattr(PyObject *self UNUSED, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 if (strcmp(name, "buffer") == 0)
1321 return (PyObject *)BufferNew(curbuf);
1322 else if (strcmp(name, "window") == 0)
1323 return (PyObject *)WindowNew(curwin);
1324 else if (strcmp(name, "line") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001325 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 else if (strcmp(name, "range") == 0)
1327 return RangeNew(curbuf, RangeStart, RangeEnd);
1328 else if (strcmp(name,"__members__") == 0)
1329 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1330 else
1331 {
1332 PyErr_SetString(PyExc_AttributeError, name);
1333 return NULL;
1334 }
1335}
1336
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 static int
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001338CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 if (strcmp(name, "line") == 0)
1341 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001342 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 return -1;
1344
1345 return 0;
1346 }
1347 else
1348 {
1349 PyErr_SetString(PyExc_AttributeError, name);
1350 return -1;
1351 }
1352}
1353
1354/* External interface
1355 */
1356
1357 void
1358python_buffer_free(buf_T *buf)
1359{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001360 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001362 BufferObject *bp = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001364 buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 }
1366}
1367
1368#if defined(FEAT_WINDOWS) || defined(PROTO)
1369 void
1370python_window_free(win_T *win)
1371{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001372 if (win->w_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001374 WindowObject *wp = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001376 win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378}
1379#endif
1380
1381static BufListObject TheBufferList =
1382{
1383 PyObject_HEAD_INIT(&BufListType)
1384};
1385
1386static WinListObject TheWindowList =
1387{
1388 PyObject_HEAD_INIT(&WinListType)
1389};
1390
1391static CurrentObject TheCurrent =
1392{
1393 PyObject_HEAD_INIT(&CurrentType)
1394};
1395
1396 static int
1397PythonMod_Init(void)
1398{
1399 PyObject *mod;
1400 PyObject *dict;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001401 /* The special value is removed from sys.path in Python_Init(). */
1402 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
1404 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +01001405 PyType_Ready(&BufferType);
1406 PyType_Ready(&RangeType);
1407 PyType_Ready(&WindowType);
1408 PyType_Ready(&BufListType);
1409 PyType_Ready(&WinListType);
1410 PyType_Ready(&CurrentType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
1412 /* Set sys.argv[] to avoid a crash in warn(). */
1413 PySys_SetArgv(1, argv);
1414
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001415 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 dict = PyModule_GetDict(mod);
1417
1418 VimError = Py_BuildValue("s", "vim.error");
1419
1420 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001421 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
1422 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1423 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424
1425 if (PyErr_Occurred())
1426 return -1;
1427
1428 return 0;
1429}
1430
1431/*************************************************************************
1432 * 4. Utility functions for handling the interface between Vim and Python.
1433 */
1434
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435/* Replace a range of lines in the specified buffer. The line numbers are in
1436 * Vim format (1-based). The range is from lo up to, but not including, hi.
1437 * The replacement lines are given as a Python list of string objects. The
1438 * list is checked for validity and correct format. Errors are returned as a
1439 * value of FAIL. The return value is OK on success.
1440 * If OK is returned and len_change is not NULL, *len_change
1441 * is set to the change in the buffer length.
1442 */
1443 static int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001444SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 /* First of all, we check the thpe of the supplied Python object.
1447 * There are three cases:
1448 * 1. NULL, or None - this is a deletion.
1449 * 2. A list - this is a replacement.
1450 * 3. Anything else - this is an error.
1451 */
1452 if (list == Py_None || list == NULL)
1453 {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001454 PyInt i;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001455 PyInt n = (int)(hi - lo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 buf_T *savebuf = curbuf;
1457
1458 PyErr_Clear();
1459 curbuf = buf;
1460
1461 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
1462 PyErr_SetVim(_("cannot save undo information"));
1463 else
1464 {
1465 for (i = 0; i < n; ++i)
1466 {
1467 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1468 {
1469 PyErr_SetVim(_("cannot delete line"));
1470 break;
1471 }
1472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (buf == curwin->w_buffer)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001474 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001475 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477
1478 curbuf = savebuf;
1479
1480 if (PyErr_Occurred() || VimErrorCheck())
1481 return FAIL;
1482
1483 if (len_change)
1484 *len_change = -n;
1485
1486 return OK;
1487 }
1488 else if (PyList_Check(list))
1489 {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001490 PyInt i;
1491 PyInt new_len = PyList_Size(list);
1492 PyInt old_len = hi - lo;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001493 PyInt extra = 0; /* lines added to text, can be negative */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 char **array;
1495 buf_T *savebuf;
1496
1497 if (new_len == 0) /* avoid allocating zero bytes */
1498 array = NULL;
1499 else
1500 {
1501 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
1502 if (array == NULL)
1503 {
1504 PyErr_NoMemory();
1505 return FAIL;
1506 }
1507 }
1508
1509 for (i = 0; i < new_len; ++i)
1510 {
1511 PyObject *line = PyList_GetItem(list, i);
1512
1513 array[i] = StringToLine(line);
1514 if (array[i] == NULL)
1515 {
1516 while (i)
1517 vim_free(array[--i]);
1518 vim_free(array);
1519 return FAIL;
1520 }
1521 }
1522
1523 savebuf = curbuf;
1524
1525 PyErr_Clear();
1526 curbuf = buf;
1527
1528 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
1529 PyErr_SetVim(_("cannot save undo information"));
1530
1531 /* If the size of the range is reducing (ie, new_len < old_len) we
1532 * need to delete some old_len. We do this at the start, by
1533 * repeatedly deleting line "lo".
1534 */
1535 if (!PyErr_Occurred())
1536 {
1537 for (i = 0; i < old_len - new_len; ++i)
1538 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1539 {
1540 PyErr_SetVim(_("cannot delete line"));
1541 break;
1542 }
1543 extra -= i;
1544 }
1545
1546 /* For as long as possible, replace the existing old_len with the
1547 * new old_len. This is a more efficient operation, as it requires
1548 * less memory allocation and freeing.
1549 */
1550 if (!PyErr_Occurred())
1551 {
1552 for (i = 0; i < old_len && i < new_len; ++i)
1553 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
1554 == FAIL)
1555 {
1556 PyErr_SetVim(_("cannot replace line"));
1557 break;
1558 }
1559 }
1560 else
1561 i = 0;
1562
1563 /* Now we may need to insert the remaining new old_len. If we do, we
1564 * must free the strings as we finish with them (we can't pass the
1565 * responsibility to vim in this case).
1566 */
1567 if (!PyErr_Occurred())
1568 {
1569 while (i < new_len)
1570 {
1571 if (ml_append((linenr_T)(lo + i - 1),
1572 (char_u *)array[i], 0, FALSE) == FAIL)
1573 {
1574 PyErr_SetVim(_("cannot insert line"));
1575 break;
1576 }
1577 vim_free(array[i]);
1578 ++i;
1579 ++extra;
1580 }
1581 }
1582
1583 /* Free any left-over old_len, as a result of an error */
1584 while (i < new_len)
1585 {
1586 vim_free(array[i]);
1587 ++i;
1588 }
1589
1590 /* Free the array of old_len. All of its contents have now
1591 * been dealt with (either freed, or the responsibility passed
1592 * to vim.
1593 */
1594 vim_free(array);
1595
1596 /* Adjust marks. Invalidate any which lie in the
1597 * changed range, and move any in the remainder of the buffer.
1598 */
1599 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
1600 (long)MAXLNUM, (long)extra);
1601 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
1602
1603 if (buf == curwin->w_buffer)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001604 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
1606 curbuf = savebuf;
1607
1608 if (PyErr_Occurred() || VimErrorCheck())
1609 return FAIL;
1610
1611 if (len_change)
1612 *len_change = new_len - old_len;
1613
1614 return OK;
1615 }
1616 else
1617 {
1618 PyErr_BadArgument();
1619 return FAIL;
1620 }
1621}
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623/* Convert a Vim line into a Python string.
1624 * All internal newlines are replaced by null characters.
1625 *
1626 * On errors, the Python exception data is set, and NULL is returned.
1627 */
1628 static PyObject *
1629LineToString(const char *str)
1630{
1631 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001632 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 char *p;
1634
1635 /* Allocate an Python string object, with uninitialised contents. We
1636 * must do it this way, so that we can modify the string in place
1637 * later. See the Python source, Objects/stringobject.c for details.
1638 */
1639 result = PyString_FromStringAndSize(NULL, len);
1640 if (result == NULL)
1641 return NULL;
1642
1643 p = PyString_AsString(result);
1644
1645 while (*str)
1646 {
1647 if (*str == '\n')
1648 *p = '\0';
1649 else
1650 *p = *str;
1651
1652 ++p;
1653 ++str;
1654 }
1655
1656 return result;
1657}
1658
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660/* Don't generate a prototype for the next function, it generates an error on
1661 * newer Python versions. */
1662#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1663
1664 char *
1665Py_GetProgramName(void)
1666{
1667 return "vim";
1668}
1669#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001670
1671 static void
1672init_structs(void)
1673{
1674 vim_memset(&OutputType, 0, sizeof(OutputType));
1675 OutputType.tp_name = "message";
1676 OutputType.tp_basicsize = sizeof(OutputObject);
1677 OutputType.tp_getattr = OutputGetattr;
1678 OutputType.tp_setattr = OutputSetattr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001679
1680 vim_memset(&RangeType, 0, sizeof(RangeType));
1681 RangeType.tp_name = "range";
1682 RangeType.tp_basicsize = sizeof(RangeObject);
1683 RangeType.tp_dealloc = RangeDestructor;
1684 RangeType.tp_getattr = RangeGetattr;
1685 RangeType.tp_repr = RangeRepr;
1686 RangeType.tp_as_sequence = &RangeAsSeq;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001687}