blob: c7c67a81ead36ef6378fdbe6245a0e3c37fd3f97 [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
96#if defined(DYNAMIC_PYTHON) || defined(PROTO)
97# ifndef DYNAMIC_PYTHON
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000098# define HINSTANCE long_u /* for generating prototypes */
Bram Moolenaar071d4272004-06-13 20:20:40 +000099# endif
100
Bram Moolenaarfa5d1e62010-07-22 21:44:13 +0200101#ifndef WIN3264
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200102# include <dlfcn.h>
103# define FARPROC void*
104# define HINSTANCE void*
Bram Moolenaarfa5d1e62010-07-22 21:44:13 +0200105# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200106# define close_dll dlclose
107# define symbol_from_dll dlsym
108#else
109# define load_dll LoadLibrary
110# define close_dll FreeLibrary
111# define symbol_from_dll GetProcAddress
112#endif
113
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000114/* This makes if_python.c compile without warnings against Python 2.5
115 * on Win32 and Win64. */
116#undef PyRun_SimpleString
117#undef PyArg_Parse
118#undef PyArg_ParseTuple
119#undef Py_BuildValue
120#undef Py_InitModule4
121#undef Py_InitModule4_64
122
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123/*
124 * Wrapper defines
125 */
126# define PyArg_Parse dll_PyArg_Parse
127# define PyArg_ParseTuple dll_PyArg_ParseTuple
128# define PyDict_SetItemString dll_PyDict_SetItemString
129# define PyErr_BadArgument dll_PyErr_BadArgument
130# define PyErr_Clear dll_PyErr_Clear
131# define PyErr_NoMemory dll_PyErr_NoMemory
132# define PyErr_Occurred dll_PyErr_Occurred
133# define PyErr_SetNone dll_PyErr_SetNone
134# define PyErr_SetString dll_PyErr_SetString
135# define PyEval_InitThreads dll_PyEval_InitThreads
136# define PyEval_RestoreThread dll_PyEval_RestoreThread
137# define PyEval_SaveThread dll_PyEval_SaveThread
138# ifdef PY_CAN_RECURSE
139# define PyGILState_Ensure dll_PyGILState_Ensure
140# define PyGILState_Release dll_PyGILState_Release
141# endif
142# define PyInt_AsLong dll_PyInt_AsLong
143# define PyInt_FromLong dll_PyInt_FromLong
144# define PyInt_Type (*dll_PyInt_Type)
145# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000146# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define PyList_New dll_PyList_New
148# define PyList_SetItem dll_PyList_SetItem
149# define PyList_Size dll_PyList_Size
150# define PyList_Type (*dll_PyList_Type)
151# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000152# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define PyDict_GetItemString dll_PyDict_GetItemString
154# define PyModule_GetDict dll_PyModule_GetDict
155# define PyRun_SimpleString dll_PyRun_SimpleString
156# define PyString_AsString dll_PyString_AsString
157# define PyString_FromString dll_PyString_FromString
158# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
159# define PyString_Size dll_PyString_Size
160# define PyString_Type (*dll_PyString_Type)
161# define PySys_SetObject dll_PySys_SetObject
162# define PySys_SetArgv dll_PySys_SetArgv
163# define PyType_Type (*dll_PyType_Type)
164# define Py_BuildValue dll_Py_BuildValue
165# define Py_FindMethod dll_Py_FindMethod
166# define Py_InitModule4 dll_Py_InitModule4
167# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000168# define Py_Finalize dll_Py_Finalize
169# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170# define _PyObject_New dll__PyObject_New
171# define _Py_NoneStruct (*dll__Py_NoneStruct)
172# define PyObject_Init dll__PyObject_Init
173# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
174# define PyType_IsSubtype dll_PyType_IsSubtype
175# endif
176# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
177# define PyObject_Malloc dll_PyObject_Malloc
178# define PyObject_Free dll_PyObject_Free
179# endif
180
181/*
182 * Pointers for dynamic link
183 */
184static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
185static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
186static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
187static int(*dll_PyErr_BadArgument)(void);
188static void(*dll_PyErr_Clear)(void);
189static PyObject*(*dll_PyErr_NoMemory)(void);
190static PyObject*(*dll_PyErr_Occurred)(void);
191static void(*dll_PyErr_SetNone)(PyObject *);
192static void(*dll_PyErr_SetString)(PyObject *, const char *);
193static void(*dll_PyEval_InitThreads)(void);
194static void(*dll_PyEval_RestoreThread)(PyThreadState *);
195static PyThreadState*(*dll_PyEval_SaveThread)(void);
196# ifdef PY_CAN_RECURSE
197static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
198static void (*dll_PyGILState_Release)(PyGILState_STATE);
199#endif
200static long(*dll_PyInt_AsLong)(PyObject *);
201static PyObject*(*dll_PyInt_FromLong)(long);
202static PyTypeObject* dll_PyInt_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000203static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000204static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000205static PyObject*(*dll_PyList_New)(PyInt size);
206static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
207static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208static PyTypeObject* dll_PyList_Type;
209static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000210static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
212static PyObject*(*dll_PyModule_GetDict)(PyObject *);
213static int(*dll_PyRun_SimpleString)(char *);
214static char*(*dll_PyString_AsString)(PyObject *);
215static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000216static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
217static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218static PyTypeObject* dll_PyString_Type;
219static int(*dll_PySys_SetObject)(char *, PyObject *);
220static int(*dll_PySys_SetArgv)(int, char **);
221static PyTypeObject* dll_PyType_Type;
222static PyObject*(*dll_Py_BuildValue)(char *, ...);
223static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
224static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
225static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000226static void(*dll_Py_Finalize)(void);
227static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
229static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
230static PyObject* dll__Py_NoneStruct;
231# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
232static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
233# endif
234# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
235static void* (*dll_PyObject_Malloc)(size_t);
236static void (*dll_PyObject_Free)(void*);
237# endif
238
239static HINSTANCE hinstPython = 0; /* Instance of python.dll */
240
241/* Imported exception objects */
242static PyObject *imp_PyExc_AttributeError;
243static PyObject *imp_PyExc_IndexError;
244static PyObject *imp_PyExc_KeyboardInterrupt;
245static PyObject *imp_PyExc_TypeError;
246static PyObject *imp_PyExc_ValueError;
247
248# define PyExc_AttributeError imp_PyExc_AttributeError
249# define PyExc_IndexError imp_PyExc_IndexError
250# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
251# define PyExc_TypeError imp_PyExc_TypeError
252# define PyExc_ValueError imp_PyExc_ValueError
253
254/*
255 * Table of name to function pointer of python.
256 */
257# define PYTHON_PROC FARPROC
258static struct
259{
260 char *name;
261 PYTHON_PROC *ptr;
262} python_funcname_table[] =
263{
264 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
265 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
266 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
267 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
268 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
269 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
270 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
271 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
272 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
273 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
274 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
275 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
276# ifdef PY_CAN_RECURSE
277 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
278 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
279# endif
280 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
281 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
282 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
283 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000284 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
286 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
287 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
288 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
289 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
290 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000291 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
293 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
294 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
295 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
296 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
297 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
298 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
299 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
300 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
301 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
302 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
303 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000304# if (PY_VERSION_HEX >= 0x02050000) && SIZEOF_SIZE_T != SIZEOF_INT
305 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
306# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000310 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
311 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
313 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
314 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
315# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
316 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
317# endif
318# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
319 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
320 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
321# endif
322 {"", NULL},
323};
324
325/*
326 * Free python.dll
327 */
328 static void
329end_dynamic_python(void)
330{
331 if (hinstPython)
332 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200333 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 hinstPython = 0;
335 }
336}
337
338/*
339 * Load library and get all pointers.
340 * Parameter 'libname' provides name of DLL.
341 * Return OK or FAIL.
342 */
343 static int
344python_runtime_link_init(char *libname, int verbose)
345{
346 int i;
347
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200348#if defined(UNIX) && defined(FEAT_PYTHON3)
349 /* Can't have Python and Python3 loaded at the same time, it may cause a
350 * crash. */
351 if (python3_loaded())
352 {
353 EMSG(_("E999: Python: Cannot use :py and :py3 in one session"));
354 return FAIL;
355 }
356#endif
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 if (hinstPython)
359 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200360 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 if (!hinstPython)
362 {
363 if (verbose)
364 EMSG2(_(e_loadlib), libname);
365 return FAIL;
366 }
367
368 for (i = 0; python_funcname_table[i].ptr; ++i)
369 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200370 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 python_funcname_table[i].name)) == NULL)
372 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200373 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 hinstPython = 0;
375 if (verbose)
376 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
377 return FAIL;
378 }
379 }
380 return OK;
381}
382
383/*
384 * If python is enabled (there is installed python on Windows system) return
385 * TRUE, else FALSE.
386 */
387 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000388python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
390 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
391}
392
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200393/*
394 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 * DLL, as this can cause errors (importing data symbols is not reliable).
396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200398get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
400 PyObject *exmod = PyImport_ImportModule("exceptions");
401 PyObject *exdict = PyModule_GetDict(exmod);
402 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
403 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
404 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
405 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
406 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
407 Py_XINCREF(imp_PyExc_AttributeError);
408 Py_XINCREF(imp_PyExc_IndexError);
409 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
410 Py_XINCREF(imp_PyExc_TypeError);
411 Py_XINCREF(imp_PyExc_ValueError);
412 Py_XDECREF(exmod);
413}
414#endif /* DYNAMIC_PYTHON */
415
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200416static PyObject *BufferNew (buf_T *);
417static PyObject *WindowNew(win_T *);
418static PyObject *LineToString(const char *);
419
420static PyTypeObject RangeType;
421
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200422/*
423 * Include the code shared with if_python3.c
424 */
425#include "if_py_both.h"
426
427
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428/******************************************************
429 * Internal function prototypes.
430 */
431
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000432static PyInt RangeStart;
433static PyInt RangeEnd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434
435static void PythonIO_Flush(void);
436static int PythonIO_Init(void);
437static int PythonMod_Init(void);
438
439/* Utility functions for the vim/python interface
440 * ----------------------------------------------
441 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000443static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445
446/******************************************************
447 * 1. Python interpreter main program.
448 */
449
450static int initialised = 0;
451
452#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
453typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000456#ifdef PY_CAN_RECURSE
457static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
458#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000459static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461
462/*
463 * Suspend a thread of the Python interpreter, other threads are allowed to
464 * run.
465 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000466 static void
467Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000469#ifdef PY_CAN_RECURSE
470 PyGILState_Release(pygilstate);
471#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474}
475
476/*
477 * Restore a thread of the Python interpreter, waits for other threads to
478 * block.
479 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000480 static void
481Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000483#ifdef PY_CAN_RECURSE
484 pygilstate = PyGILState_Ensure();
485#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 PyEval_RestoreThread(saved_python_thread);
487 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000489}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 void
492python_end()
493{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000494 static int recurse = 0;
495
496 /* If a crash occurs while doing this, don't try again. */
497 if (recurse != 0)
498 return;
499
500 ++recurse;
501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000503 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000504 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000505 Python_RestoreThread(); /* enter python */
506 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000509#else
510 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000511 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000512 Python_RestoreThread(); /* enter python */
513 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000516
517 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518}
519
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200520#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
521 int
522python_loaded()
523{
524 return (hinstPython != 0);
525}
526#endif
527
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 static int
529Python_Init(void)
530{
531 if (!initialised)
532 {
533#ifdef DYNAMIC_PYTHON
534 if (!python_enabled(TRUE))
535 {
536 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
537 goto fail;
538 }
539#endif
540
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200541 init_structs();
542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543#if !defined(MACOS) || defined(MACOS_X_UNIX)
544 Py_Initialize();
545#else
546 PyMac_Initialize();
547#endif
548 /* initialise threads */
549 PyEval_InitThreads();
550
551#ifdef DYNAMIC_PYTHON
552 get_exceptions();
553#endif
554
555 if (PythonIO_Init())
556 goto fail;
557
558 if (PythonMod_Init())
559 goto fail;
560
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000561 /* Remove the element from sys.path that was added because of our
562 * argv[0] value in PythonMod_Init(). Previously we used an empty
563 * string, but dependinding on the OS we then get an empty entry or
564 * the current directory in sys.path. */
565 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
566
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000567 /* the first python thread is vim's, release the lock */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 Python_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569
570 initialised = 1;
571 }
572
573 return 0;
574
575fail:
576 /* We call PythonIO_Flush() here to print any Python errors.
577 * This is OK, as it is possible to call this function even
578 * if PythonIO_Init() has not completed successfully (it will
579 * not do anything in this case).
580 */
581 PythonIO_Flush();
582 return -1;
583}
584
585/*
586 * External interface
587 */
588 static void
589DoPythonCommand(exarg_T *eap, const char *cmd)
590{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000591#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 static int recursive = 0;
593#endif
594#if defined(MACOS) && !defined(MACOS_X_UNIX)
595 GrafPtr oldPort;
596#endif
597#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
598 char *saved_locale;
599#endif
600
601#ifndef PY_CAN_RECURSE
602 if (recursive)
603 {
604 EMSG(_("E659: Cannot invoke Python recursively"));
605 return;
606 }
607 ++recursive;
608#endif
609
610#if defined(MACOS) && !defined(MACOS_X_UNIX)
611 GetPort(&oldPort);
612 /* Check if the Python library is available */
613 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
614 goto theend;
615#endif
616 if (Python_Init())
617 goto theend;
618
619 RangeStart = eap->line1;
620 RangeEnd = eap->line2;
621 Python_Release_Vim(); /* leave vim */
622
623#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
624 /* Python only works properly when the LC_NUMERIC locale is "C". */
625 saved_locale = setlocale(LC_NUMERIC, NULL);
626 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
627 saved_locale = NULL;
628 else
629 {
630 /* Need to make a copy, value may change when setting new locale. */
631 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
632 (void)setlocale(LC_NUMERIC, "C");
633 }
634#endif
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
638 PyRun_SimpleString((char *)(cmd));
639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
642#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
643 if (saved_locale != NULL)
644 {
645 (void)setlocale(LC_NUMERIC, saved_locale);
646 vim_free(saved_locale);
647 }
648#endif
649
650 Python_Lock_Vim(); /* enter vim */
651 PythonIO_Flush();
652#if defined(MACOS) && !defined(MACOS_X_UNIX)
653 SetPort(oldPort);
654#endif
655
656theend:
657#ifndef PY_CAN_RECURSE
658 --recursive;
659#endif
660 return; /* keeps lint happy */
661}
662
663/*
664 * ":python"
665 */
666 void
667ex_python(exarg_T *eap)
668{
669 char_u *script;
670
671 script = script_get(eap, eap->arg);
672 if (!eap->skip)
673 {
674 if (script == NULL)
675 DoPythonCommand(eap, (char *)eap->arg);
676 else
677 DoPythonCommand(eap, (char *)script);
678 }
679 vim_free(script);
680}
681
682#define BUFFER_SIZE 1024
683
684/*
685 * ":pyfile"
686 */
687 void
688ex_pyfile(exarg_T *eap)
689{
690 static char buffer[BUFFER_SIZE];
691 const char *file = (char *)eap->arg;
692 char *p;
693
694 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
695 * stdio file pointer, but Vim and the Python DLL are compiled with
696 * different options under Windows, meaning that stdio pointers aren't
697 * compatible between the two. Yuk.
698 *
699 * Put the string "execfile('file')" into buffer. But, we need to
700 * escape any backslashes or single quotes in the file name, so that
701 * Python won't mangle the file name.
702 */
703 strcpy(buffer, "execfile('");
704 p = buffer + 10; /* size of "execfile('" */
705
706 while (*file && p < buffer + (BUFFER_SIZE - 3))
707 {
708 if (*file == '\\' || *file == '\'')
709 *p++ = '\\';
710 *p++ = *file++;
711 }
712
713 /* If we didn't finish the file name, we hit a buffer overflow */
714 if (*file != '\0')
715 return;
716
717 /* Put in the terminating "')" and a null */
718 *p++ = '\'';
719 *p++ = ')';
720 *p++ = '\0';
721
722 /* Execute the file */
723 DoPythonCommand(eap, buffer);
724}
725
726/******************************************************
727 * 2. Python output stream: writes output via [e]msg().
728 */
729
730/* Implementation functions
731 */
732
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 static PyObject *
734OutputGetattr(PyObject *self, char *name)
735{
736 if (strcmp(name, "softspace") == 0)
737 return PyInt_FromLong(((OutputObject *)(self))->softspace);
738
739 return Py_FindMethod(OutputMethods, self, name);
740}
741
742 static int
743OutputSetattr(PyObject *self, char *name, PyObject *val)
744{
745 if (val == NULL) {
746 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
747 return -1;
748 }
749
750 if (strcmp(name, "softspace") == 0)
751 {
752 if (!PyInt_Check(val)) {
753 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
754 return -1;
755 }
756
757 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
758 return 0;
759 }
760
761 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
762 return -1;
763}
764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765/***************/
766
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 static int
768PythonIO_Init(void)
769{
770 /* Fixups... */
771 OutputType.ob_type = &PyType_Type;
772
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200773 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774}
775
776/******************************************************
777 * 3. Implementation of the Vim module for Python
778 */
779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780/* Window type - Implementation functions
781 * --------------------------------------
782 */
783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786static void WindowDestructor(PyObject *);
787static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789/* Buffer type - Implementation functions
790 * --------------------------------------
791 */
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795static void BufferDestructor(PyObject *);
796static PyObject *BufferGetattr(PyObject *, char *);
797static PyObject *BufferRepr(PyObject *);
798
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000799static PyInt BufferLength(PyObject *);
800static PyObject *BufferItem(PyObject *, PyInt);
801static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
802static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
803static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805/* Line range type - Implementation functions
806 * --------------------------------------
807 */
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
810
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000811static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
812static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814/* Current objects type - Implementation functions
815 * -----------------------------------------------
816 */
817
818static PyObject *CurrentGetattr(PyObject *, char *);
819static int CurrentSetattr(PyObject *, char *, PyObject *);
820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821/* Common routines for buffers and line ranges
822 * -------------------------------------------
823 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200824
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000825 static PyInt
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000826RBAssSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000828 PyInt size;
829 PyInt len_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831 /* Self must be a valid buffer */
832 if (CheckBuffer(self))
833 return -1;
834
835 /* Sort out the slice range */
836 size = end - start + 1;
837
838 if (lo < 0)
839 lo = 0;
840 else if (lo > size)
841 lo = size;
842 if (hi < 0)
843 hi = 0;
844 if (hi < lo)
845 hi = lo;
846 else if (hi > size)
847 hi = size;
848
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200849 if (SetBufferLineList(self->buf, lo + start, hi + start,
850 val, &len_change) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 return -1;
852
853 if (new_end)
854 *new_end = end + len_change;
855
856 return 0;
857}
858
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000860 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000862 (PyIntArgFunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */
863 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
864 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
865 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
866 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867};
868
869static PyTypeObject BufferType = {
870 PyObject_HEAD_INIT(0)
871 0,
872 "buffer",
873 sizeof(BufferObject),
874 0,
875
876 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
877 (printfunc) 0, /* tp_print, print x */
878 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
879 (setattrfunc) 0, /* tp_setattr, x.attr=v */
880 (cmpfunc) 0, /* tp_compare, x>y */
881 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
882
883 0, /* as number */
884 &BufferAsSeq, /* as sequence */
885 0, /* as mapping */
886
887 (hashfunc) 0, /* tp_hash, dict(x) */
888 (ternaryfunc) 0, /* tp_call, x() */
889 (reprfunc) 0, /* tp_str, str(x) */
890};
891
892/* Buffer object - Implementation
893 */
894
895 static PyObject *
896BufferNew(buf_T *buf)
897{
898 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +0000899 * If we add a "b_python_ref" field to the buf_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 * then we can get at it in buf_freeall() in vim. We then
901 * need to create only ONE Python object per buffer - if
902 * we try to create a second, just INCREF the existing one
903 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +0000904 * the buffer is stored in "b_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 * Question: what to do on a buf_freeall(). We'll probably
906 * have to either delete the Python object (DECREF it to
907 * zero - a bad idea, as it leaves dangling refs!) or
908 * set the buf_T * value to an invalid value (-1?), which
909 * means we need checks in all access functions... Bah.
910 */
911
912 BufferObject *self;
913
Bram Moolenaare344bea2005-09-01 20:46:49 +0000914 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000916 self = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 Py_INCREF(self);
918 }
919 else
920 {
921 self = PyObject_NEW(BufferObject, &BufferType);
922 if (self == NULL)
923 return NULL;
924 self->buf = buf;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000925 buf->b_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
927
928 return (PyObject *)(self);
929}
930
931 static void
932BufferDestructor(PyObject *self)
933{
934 BufferObject *this = (BufferObject *)(self);
935
936 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000937 this->buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
Bram Moolenaar658ada62006-10-03 13:02:36 +0000939 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940}
941
942 static PyObject *
943BufferGetattr(PyObject *self, char *name)
944{
945 BufferObject *this = (BufferObject *)(self);
946
947 if (CheckBuffer(this))
948 return NULL;
949
950 if (strcmp(name, "name") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000951 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 else if (strcmp(name, "number") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000953 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 else if (strcmp(name,"__members__") == 0)
955 return Py_BuildValue("[ss]", "name", "number");
956 else
957 return Py_FindMethod(BufferMethods, self, name);
958}
959
960 static PyObject *
961BufferRepr(PyObject *self)
962{
Bram Moolenaar555b2802005-05-19 21:08:39 +0000963 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 BufferObject *this = (BufferObject *)(self);
965
966 if (this->buf == INVALID_BUFFER_VALUE)
967 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000968 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 return PyString_FromString(repr);
970 }
971 else
972 {
973 char *name = (char *)this->buf->b_fname;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000974 PyInt len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976 if (name == NULL)
977 name = "";
978 len = strlen(name);
979
980 if (len > 35)
981 name = name + (35 - len);
982
Bram Moolenaar555b2802005-05-19 21:08:39 +0000983 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 return PyString_FromString(repr);
986 }
987}
988
989/******************/
990
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000991 static PyInt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992BufferLength(PyObject *self)
993{
994 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
995 if (CheckBuffer((BufferObject *)(self)))
996 return -1; /* ??? */
997
998 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
999}
1000
1001 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001002BufferItem(PyObject *self, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
1004 return RBItem((BufferObject *)(self), n, 1,
1005 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1006}
1007
1008 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001009BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010{
1011 return RBSlice((BufferObject *)(self), lo, hi, 1,
1012 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1013}
1014
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001015 static PyInt
1016BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001018 return RBAsItem((BufferObject *)(self), n, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001019 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 NULL);
1021}
1022
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001023 static PyInt
1024BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
1026 return RBAssSlice((BufferObject *)(self), lo, hi, 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 Moolenaar071d4272004-06-13 20:20:40 +00001031static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001032 (PyInquiry) RangeLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001034 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1035 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1036 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1037 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1038 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039};
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041/* Line range object - Implementation
1042 */
1043
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 static void
1045RangeDestructor(PyObject *self)
1046{
1047 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar658ada62006-10-03 13:02:36 +00001048 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
1050
1051 static PyObject *
1052RangeGetattr(PyObject *self, char *name)
1053{
1054 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001055 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001057 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 else
1059 return Py_FindMethod(RangeMethods, self, name);
1060}
1061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062/****************/
1063
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001064 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001065RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001067 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 ((RangeObject *)(self))->start,
1069 ((RangeObject *)(self))->end,
1070 &((RangeObject *)(self))->end);
1071}
1072
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001073 static PyInt
1074RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
1076 return RBAssSlice(((RangeObject *)(self))->buf, lo, hi, val,
1077 ((RangeObject *)(self))->start,
1078 ((RangeObject *)(self))->end,
1079 &((RangeObject *)(self))->end);
1080}
1081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082/* Buffer list object - Definitions
1083 */
1084
1085typedef struct
1086{
1087 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001088} BufListObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
1090static PySequenceMethods BufListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001091 (PyInquiry) BufListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001093 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1094 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */
1095 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1096 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1097 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098};
1099
1100static PyTypeObject BufListType = {
1101 PyObject_HEAD_INIT(0)
1102 0,
1103 "buffer list",
1104 sizeof(BufListObject),
1105 0,
1106
1107 (destructor) 0, /* tp_dealloc, refcount==0 */
1108 (printfunc) 0, /* tp_print, print x */
1109 (getattrfunc) 0, /* tp_getattr, x.attr */
1110 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1111 (cmpfunc) 0, /* tp_compare, x>y */
1112 (reprfunc) 0, /* tp_repr, `x`, print x */
1113
1114 0, /* as number */
1115 &BufListAsSeq, /* as sequence */
1116 0, /* as mapping */
1117
1118 (hashfunc) 0, /* tp_hash, dict(x) */
1119 (ternaryfunc) 0, /* tp_call, x() */
1120 (reprfunc) 0, /* tp_str, str(x) */
1121};
1122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123/* Window object - Definitions
1124 */
1125
1126static struct PyMethodDef WindowMethods[] = {
1127 /* name, function, calling, documentation */
1128 { NULL, NULL, 0, NULL }
1129};
1130
1131static PyTypeObject WindowType = {
1132 PyObject_HEAD_INIT(0)
1133 0,
1134 "window",
1135 sizeof(WindowObject),
1136 0,
1137
1138 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
1139 (printfunc) 0, /* tp_print, print x */
1140 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
1141 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
1142 (cmpfunc) 0, /* tp_compare, x>y */
1143 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
1144
1145 0, /* as number */
1146 0, /* as sequence */
1147 0, /* as mapping */
1148
1149 (hashfunc) 0, /* tp_hash, dict(x) */
1150 (ternaryfunc) 0, /* tp_call, x() */
1151 (reprfunc) 0, /* tp_str, str(x) */
1152};
1153
1154/* Window object - Implementation
1155 */
1156
1157 static PyObject *
1158WindowNew(win_T *win)
1159{
1160 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001161 * If we add a "w_python_ref" field to the win_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 * then we can get at it in win_free() in vim. We then
1163 * need to create only ONE Python object per window - if
1164 * we try to create a second, just INCREF the existing one
1165 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001166 * the window is stored in "w_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 * On a win_free() we set the Python object's win_T* field
1168 * to an invalid value. We trap all uses of a window
1169 * object, and reject them if the win_T* field is invalid.
1170 */
1171
1172 WindowObject *self;
1173
Bram Moolenaare344bea2005-09-01 20:46:49 +00001174 if (win->w_python_ref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001176 self = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 Py_INCREF(self);
1178 }
1179 else
1180 {
1181 self = PyObject_NEW(WindowObject, &WindowType);
1182 if (self == NULL)
1183 return NULL;
1184 self->win = win;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001185 win->w_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187
1188 return (PyObject *)(self);
1189}
1190
1191 static void
1192WindowDestructor(PyObject *self)
1193{
1194 WindowObject *this = (WindowObject *)(self);
1195
1196 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001197 this->win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198
Bram Moolenaar658ada62006-10-03 13:02:36 +00001199 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200}
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 static PyObject *
1203WindowGetattr(PyObject *self, char *name)
1204{
1205 WindowObject *this = (WindowObject *)(self);
1206
1207 if (CheckWindow(this))
1208 return NULL;
1209
1210 if (strcmp(name, "buffer") == 0)
1211 return (PyObject *)BufferNew(this->win->w_buffer);
1212 else if (strcmp(name, "cursor") == 0)
1213 {
1214 pos_T *pos = &this->win->w_cursor;
1215
1216 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1217 }
1218 else if (strcmp(name, "height") == 0)
1219 return Py_BuildValue("l", (long)(this->win->w_height));
1220#ifdef FEAT_VERTSPLIT
1221 else if (strcmp(name, "width") == 0)
1222 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1223#endif
1224 else if (strcmp(name,"__members__") == 0)
1225 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1226 else
1227 return Py_FindMethod(WindowMethods, self, name);
1228}
1229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230/* Window list object - Definitions
1231 */
1232
1233typedef struct
1234{
1235 PyObject_HEAD
1236}
1237WinListObject;
1238
1239static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001240 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001242 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1243 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1244 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1245 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1246 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247};
1248
1249static PyTypeObject WinListType = {
1250 PyObject_HEAD_INIT(0)
1251 0,
1252 "window list",
1253 sizeof(WinListObject),
1254 0,
1255
1256 (destructor) 0, /* tp_dealloc, refcount==0 */
1257 (printfunc) 0, /* tp_print, print x */
1258 (getattrfunc) 0, /* tp_getattr, x.attr */
1259 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1260 (cmpfunc) 0, /* tp_compare, x>y */
1261 (reprfunc) 0, /* tp_repr, `x`, print x */
1262
1263 0, /* as number */
1264 &WinListAsSeq, /* as sequence */
1265 0, /* as mapping */
1266
1267 (hashfunc) 0, /* tp_hash, dict(x) */
1268 (ternaryfunc) 0, /* tp_call, x() */
1269 (reprfunc) 0, /* tp_str, str(x) */
1270};
1271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272/* Current items object - Definitions
1273 */
1274
1275typedef struct
1276{
1277 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001278} CurrentObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
1280static PyTypeObject CurrentType = {
1281 PyObject_HEAD_INIT(0)
1282 0,
1283 "current data",
1284 sizeof(CurrentObject),
1285 0,
1286
1287 (destructor) 0, /* tp_dealloc, refcount==0 */
1288 (printfunc) 0, /* tp_print, print x */
1289 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
1290 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
1291 (cmpfunc) 0, /* tp_compare, x>y */
1292 (reprfunc) 0, /* tp_repr, `x`, print x */
1293
1294 0, /* as number */
1295 0, /* as sequence */
1296 0, /* as mapping */
1297
1298 (hashfunc) 0, /* tp_hash, dict(x) */
1299 (ternaryfunc) 0, /* tp_call, x() */
1300 (reprfunc) 0, /* tp_str, str(x) */
1301};
1302
1303/* Current items object - Implementation
1304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001306CurrentGetattr(PyObject *self UNUSED, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 if (strcmp(name, "buffer") == 0)
1309 return (PyObject *)BufferNew(curbuf);
1310 else if (strcmp(name, "window") == 0)
1311 return (PyObject *)WindowNew(curwin);
1312 else if (strcmp(name, "line") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001313 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 else if (strcmp(name, "range") == 0)
1315 return RangeNew(curbuf, RangeStart, RangeEnd);
1316 else if (strcmp(name,"__members__") == 0)
1317 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1318 else
1319 {
1320 PyErr_SetString(PyExc_AttributeError, name);
1321 return NULL;
1322 }
1323}
1324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 static int
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001326CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
1328 if (strcmp(name, "line") == 0)
1329 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001330 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 return -1;
1332
1333 return 0;
1334 }
1335 else
1336 {
1337 PyErr_SetString(PyExc_AttributeError, name);
1338 return -1;
1339 }
1340}
1341
1342/* External interface
1343 */
1344
1345 void
1346python_buffer_free(buf_T *buf)
1347{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001348 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001350 BufferObject *bp = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001352 buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354}
1355
1356#if defined(FEAT_WINDOWS) || defined(PROTO)
1357 void
1358python_window_free(win_T *win)
1359{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001360 if (win->w_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001362 WindowObject *wp = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001364 win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 }
1366}
1367#endif
1368
1369static BufListObject TheBufferList =
1370{
1371 PyObject_HEAD_INIT(&BufListType)
1372};
1373
1374static WinListObject TheWindowList =
1375{
1376 PyObject_HEAD_INIT(&WinListType)
1377};
1378
1379static CurrentObject TheCurrent =
1380{
1381 PyObject_HEAD_INIT(&CurrentType)
1382};
1383
1384 static int
1385PythonMod_Init(void)
1386{
1387 PyObject *mod;
1388 PyObject *dict;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001389 /* The special value is removed from sys.path in Python_Init(). */
1390 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392 /* Fixups... */
1393 BufferType.ob_type = &PyType_Type;
1394 RangeType.ob_type = &PyType_Type;
1395 WindowType.ob_type = &PyType_Type;
1396 BufListType.ob_type = &PyType_Type;
1397 WinListType.ob_type = &PyType_Type;
1398 CurrentType.ob_type = &PyType_Type;
1399
1400 /* Set sys.argv[] to avoid a crash in warn(). */
1401 PySys_SetArgv(1, argv);
1402
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001403 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 dict = PyModule_GetDict(mod);
1405
1406 VimError = Py_BuildValue("s", "vim.error");
1407
1408 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001409 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
1410 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1411 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
1413 if (PyErr_Occurred())
1414 return -1;
1415
1416 return 0;
1417}
1418
1419/*************************************************************************
1420 * 4. Utility functions for handling the interface between Vim and Python.
1421 */
1422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423/* Replace a range of lines in the specified buffer. The line numbers are in
1424 * Vim format (1-based). The range is from lo up to, but not including, hi.
1425 * The replacement lines are given as a Python list of string objects. The
1426 * list is checked for validity and correct format. Errors are returned as a
1427 * value of FAIL. The return value is OK on success.
1428 * If OK is returned and len_change is not NULL, *len_change
1429 * is set to the change in the buffer length.
1430 */
1431 static int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001432SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 /* First of all, we check the thpe of the supplied Python object.
1435 * There are three cases:
1436 * 1. NULL, or None - this is a deletion.
1437 * 2. A list - this is a replacement.
1438 * 3. Anything else - this is an error.
1439 */
1440 if (list == Py_None || list == NULL)
1441 {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001442 PyInt i;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001443 PyInt n = (int)(hi - lo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 buf_T *savebuf = curbuf;
1445
1446 PyErr_Clear();
1447 curbuf = buf;
1448
1449 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
1450 PyErr_SetVim(_("cannot save undo information"));
1451 else
1452 {
1453 for (i = 0; i < n; ++i)
1454 {
1455 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1456 {
1457 PyErr_SetVim(_("cannot delete line"));
1458 break;
1459 }
1460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 if (buf == curwin->w_buffer)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001462 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00001463 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465
1466 curbuf = savebuf;
1467
1468 if (PyErr_Occurred() || VimErrorCheck())
1469 return FAIL;
1470
1471 if (len_change)
1472 *len_change = -n;
1473
1474 return OK;
1475 }
1476 else if (PyList_Check(list))
1477 {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001478 PyInt i;
1479 PyInt new_len = PyList_Size(list);
1480 PyInt old_len = hi - lo;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001481 PyInt extra = 0; /* lines added to text, can be negative */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 char **array;
1483 buf_T *savebuf;
1484
1485 if (new_len == 0) /* avoid allocating zero bytes */
1486 array = NULL;
1487 else
1488 {
1489 array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
1490 if (array == NULL)
1491 {
1492 PyErr_NoMemory();
1493 return FAIL;
1494 }
1495 }
1496
1497 for (i = 0; i < new_len; ++i)
1498 {
1499 PyObject *line = PyList_GetItem(list, i);
1500
1501 array[i] = StringToLine(line);
1502 if (array[i] == NULL)
1503 {
1504 while (i)
1505 vim_free(array[--i]);
1506 vim_free(array);
1507 return FAIL;
1508 }
1509 }
1510
1511 savebuf = curbuf;
1512
1513 PyErr_Clear();
1514 curbuf = buf;
1515
1516 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
1517 PyErr_SetVim(_("cannot save undo information"));
1518
1519 /* If the size of the range is reducing (ie, new_len < old_len) we
1520 * need to delete some old_len. We do this at the start, by
1521 * repeatedly deleting line "lo".
1522 */
1523 if (!PyErr_Occurred())
1524 {
1525 for (i = 0; i < old_len - new_len; ++i)
1526 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
1527 {
1528 PyErr_SetVim(_("cannot delete line"));
1529 break;
1530 }
1531 extra -= i;
1532 }
1533
1534 /* For as long as possible, replace the existing old_len with the
1535 * new old_len. This is a more efficient operation, as it requires
1536 * less memory allocation and freeing.
1537 */
1538 if (!PyErr_Occurred())
1539 {
1540 for (i = 0; i < old_len && i < new_len; ++i)
1541 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
1542 == FAIL)
1543 {
1544 PyErr_SetVim(_("cannot replace line"));
1545 break;
1546 }
1547 }
1548 else
1549 i = 0;
1550
1551 /* Now we may need to insert the remaining new old_len. If we do, we
1552 * must free the strings as we finish with them (we can't pass the
1553 * responsibility to vim in this case).
1554 */
1555 if (!PyErr_Occurred())
1556 {
1557 while (i < new_len)
1558 {
1559 if (ml_append((linenr_T)(lo + i - 1),
1560 (char_u *)array[i], 0, FALSE) == FAIL)
1561 {
1562 PyErr_SetVim(_("cannot insert line"));
1563 break;
1564 }
1565 vim_free(array[i]);
1566 ++i;
1567 ++extra;
1568 }
1569 }
1570
1571 /* Free any left-over old_len, as a result of an error */
1572 while (i < new_len)
1573 {
1574 vim_free(array[i]);
1575 ++i;
1576 }
1577
1578 /* Free the array of old_len. All of its contents have now
1579 * been dealt with (either freed, or the responsibility passed
1580 * to vim.
1581 */
1582 vim_free(array);
1583
1584 /* Adjust marks. Invalidate any which lie in the
1585 * changed range, and move any in the remainder of the buffer.
1586 */
1587 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
1588 (long)MAXLNUM, (long)extra);
1589 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
1590
1591 if (buf == curwin->w_buffer)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001592 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 curbuf = savebuf;
1595
1596 if (PyErr_Occurred() || VimErrorCheck())
1597 return FAIL;
1598
1599 if (len_change)
1600 *len_change = new_len - old_len;
1601
1602 return OK;
1603 }
1604 else
1605 {
1606 PyErr_BadArgument();
1607 return FAIL;
1608 }
1609}
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611/* Convert a Vim line into a Python string.
1612 * All internal newlines are replaced by null characters.
1613 *
1614 * On errors, the Python exception data is set, and NULL is returned.
1615 */
1616 static PyObject *
1617LineToString(const char *str)
1618{
1619 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001620 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 char *p;
1622
1623 /* Allocate an Python string object, with uninitialised contents. We
1624 * must do it this way, so that we can modify the string in place
1625 * later. See the Python source, Objects/stringobject.c for details.
1626 */
1627 result = PyString_FromStringAndSize(NULL, len);
1628 if (result == NULL)
1629 return NULL;
1630
1631 p = PyString_AsString(result);
1632
1633 while (*str)
1634 {
1635 if (*str == '\n')
1636 *p = '\0';
1637 else
1638 *p = *str;
1639
1640 ++p;
1641 ++str;
1642 }
1643
1644 return result;
1645}
1646
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648/* Don't generate a prototype for the next function, it generates an error on
1649 * newer Python versions. */
1650#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1651
1652 char *
1653Py_GetProgramName(void)
1654{
1655 return "vim";
1656}
1657#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001658
1659 static void
1660init_structs(void)
1661{
1662 vim_memset(&OutputType, 0, sizeof(OutputType));
1663 OutputType.tp_name = "message";
1664 OutputType.tp_basicsize = sizeof(OutputObject);
1665 OutputType.tp_getattr = OutputGetattr;
1666 OutputType.tp_setattr = OutputSetattr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001667
1668 vim_memset(&RangeType, 0, sizeof(RangeType));
1669 RangeType.tp_name = "range";
1670 RangeType.tp_basicsize = sizeof(RangeObject);
1671 RangeType.tp_dealloc = RangeDestructor;
1672 RangeType.tp_getattr = RangeGetattr;
1673 RangeType.tp_repr = RangeRepr;
1674 RangeType.tp_as_sequence = &RangeAsSeq;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001675}