blob: 302be7ece640b55780eb941599da0340bd1a87cc [file] [log] [blame]
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001/* 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/*
21 * Roland Puntaier 2009/sept/16:
22 * Adaptations to support both python3.x and python2.x
23 */
24
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +010025/* uncomment this if used with the debug version of python */
26/* #define Py_DEBUG */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020027
28#include "vim.h"
29
30#include <limits.h>
31
32/* Python.h defines _POSIX_THREADS itself (if needed) */
33#ifdef _POSIX_THREADS
34# undef _POSIX_THREADS
35#endif
36
Bram Moolenaard68554d2010-07-25 13:43:20 +020037#if defined(_WIN32) && defined(HAVE_FCNTL_H)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020038# undef HAVE_FCNTL_H
39#endif
40
41#ifdef _DEBUG
42# undef _DEBUG
43#endif
44
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020045#ifdef F_BLANK
46# undef F_BLANK
47#endif
48
Bram Moolenaar6df6f472010-07-18 18:04:50 +020049#ifdef HAVE_STDARG_H
50# undef HAVE_STDARG_H /* Python's config.h defines it as well. */
51#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020052#ifdef _POSIX_C_SOURCE /* defined in feature.h */
53# undef _POSIX_C_SOURCE
54#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020055#ifdef _XOPEN_SOURCE
56# undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */
57#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020058
59#include <Python.h>
60#if defined(MACOS) && !defined(MACOS_X_UNIX)
61# include "macglue.h"
62# include <CodeFragments.h>
63#endif
64#undef main /* Defined in python.h - aargh */
65#undef HAVE_FCNTL_H /* Clash with os_win32.h */
66
Bram Moolenaare8cdcef2012-09-12 20:21:43 +020067#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
68# define PY_SSIZE_T_CLEAN
69#endif
70
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020071static void init_structs(void);
72
Bram Moolenaar3d64a312011-07-15 15:54:44 +020073/* The "surrogateescape" error handler is new in Python 3.1 */
74#if PY_VERSION_HEX >= 0x030100f0
75# define CODEC_ERROR_HANDLER "surrogateescape"
76#else
77# define CODEC_ERROR_HANDLER NULL
78#endif
79
Bram Moolenaar2afa3232012-06-29 16:28:28 +020080/* Python 3 does not support CObjects, always use Capsules */
81#define PY_USE_CAPSULE
82
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020083#define PyInt Py_ssize_t
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020084#define PyString_Check(obj) PyUnicode_Check(obj)
Bram Moolenaardb913952012-06-29 12:54:53 +020085#define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER)
Bram Moolenaar19e60942011-06-19 00:27:51 +020086#define PyString_FreeBytes(obj) Py_XDECREF(bytes)
87#define PyString_AsString(obj) PyBytes_AsString(obj)
88#define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020089#define PyString_FromString(repr) PyUnicode_FromString(repr)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +020090#define PyString_AsStringAndSize(obj, buffer, len) PyBytes_AsStringAndSize(obj, buffer, len)
Bram Moolenaar77045652012-09-21 13:46:06 +020091#define PyInt_Check(obj) PyLong_Check(obj)
92#define PyInt_FromLong(i) PyLong_FromLong(i)
93#define PyInt_AsLong(obj) PyLong_AsLong(obj)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020094
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +010095#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020096
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020097# ifndef WIN3264
98# include <dlfcn.h>
99# define FARPROC void*
100# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100101# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200102# define load_dll(n) dlopen((n), RTLD_LAZY)
103# else
104# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
105# endif
106# define close_dll dlclose
107# define symbol_from_dll dlsym
108# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200109# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200110# define close_dll FreeLibrary
111# define symbol_from_dll GetProcAddress
112# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200113/*
114 * Wrapper defines
115 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200116# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200117# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200118# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200119# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200120# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200121# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200122# define PyDict_SetItemString py3_PyDict_SetItemString
123# define PyErr_BadArgument py3_PyErr_BadArgument
124# define PyErr_Clear py3_PyErr_Clear
125# define PyErr_NoMemory py3_PyErr_NoMemory
126# define PyErr_Occurred py3_PyErr_Occurred
127# define PyErr_SetNone py3_PyErr_SetNone
128# define PyErr_SetString py3_PyErr_SetString
129# define PyEval_InitThreads py3_PyEval_InitThreads
130# define PyEval_RestoreThread py3_PyEval_RestoreThread
131# define PyEval_SaveThread py3_PyEval_SaveThread
132# define PyGILState_Ensure py3_PyGILState_Ensure
133# define PyGILState_Release py3_PyGILState_Release
134# define PyLong_AsLong py3_PyLong_AsLong
135# define PyLong_FromLong py3_PyLong_FromLong
136# define PyList_GetItem py3_PyList_GetItem
137# define PyList_Append py3_PyList_Append
138# define PyList_New py3_PyList_New
139# define PyList_SetItem py3_PyList_SetItem
140# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200141# define PySequence_Check py3_PySequence_Check
142# define PySequence_Size py3_PySequence_Size
143# define PySequence_GetItem py3_PySequence_GetItem
144# define PyTuple_Size py3_PyTuple_Size
145# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200146# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
147# define PyImport_ImportModule py3_PyImport_ImportModule
Bram Moolenaardb913952012-06-29 12:54:53 +0200148# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200149# define PyObject_Init py3__PyObject_Init
150# define PyDict_New py3_PyDict_New
151# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200152# define PyDict_Next py3_PyDict_Next
153# define PyMapping_Check py3_PyMapping_Check
154# define PyMapping_Items py3_PyMapping_Items
155# define PyIter_Next py3_PyIter_Next
156# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200157# define PyModule_GetDict py3_PyModule_GetDict
158#undef PyRun_SimpleString
159# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200160#undef PyRun_String
161# define PyRun_String py3_PyRun_String
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200162# define PySys_SetObject py3_PySys_SetObject
163# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200164# define PyType_Ready py3_PyType_Ready
165#undef Py_BuildValue
166# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100167# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200168# define Py_Initialize py3_Py_Initialize
169# define Py_Finalize py3_Py_Finalize
170# define Py_IsInitialized py3_Py_IsInitialized
171# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200172# define _Py_FalseStruct (*py3__Py_FalseStruct)
173# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200174# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200175# define PyModule_AddObject py3_PyModule_AddObject
176# define PyImport_AppendInittab py3_PyImport_AppendInittab
177# define _PyUnicode_AsString py3__PyUnicode_AsString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200178# undef PyUnicode_AsEncodedString
179# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
180# undef PyBytes_AsString
181# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200182# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
Bram Moolenaardb913952012-06-29 12:54:53 +0200183# undef PyBytes_FromString
184# define PyBytes_FromString py3_PyBytes_FromString
185# define PyFloat_FromDouble py3_PyFloat_FromDouble
186# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200187# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200188# define PyType_Type (*py3_PyType_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200189# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200190# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200191# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200192# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200193# ifdef Py_DEBUG
194# define _Py_NegativeRefcount py3__Py_NegativeRefcount
195# define _Py_RefTotal (*py3__Py_RefTotal)
196# define _Py_Dealloc py3__Py_Dealloc
197# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
198# define _PyObject_DebugFree py3__PyObject_DebugFree
199# else
200# define PyObject_Malloc py3_PyObject_Malloc
201# define PyObject_Free py3_PyObject_Free
202# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200203# define PyType_GenericAlloc py3_PyType_GenericAlloc
204# define PyType_GenericNew py3_PyType_GenericNew
205# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200206# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200207# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200208# undef PyUnicode_Decode
209# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200210# define PyType_IsSubtype py3_PyType_IsSubtype
211# define PyCapsule_New py3_PyCapsule_New
212# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200213
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200214# ifdef Py_DEBUG
215# undef PyObject_NEW
216# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200217( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200218 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200219# endif
220
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200221/*
222 * Pointers for dynamic link
223 */
224static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100225static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200226static void (*py3_Py_Initialize)(void);
227static PyObject* (*py3_PyList_New)(Py_ssize_t size);
228static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
229static void (*py3_PyGILState_Release)(PyGILState_STATE);
230static int (*py3_PySys_SetObject)(char *, PyObject *);
231static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
232static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200233static int (*py3_PySequence_Check)(PyObject *);
234static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
235static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
236static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
237static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
238static int (*py3_PyMapping_Check)(PyObject *);
239static PyObject* (*py3_PyMapping_Items)(PyObject *);
Bram Moolenaar314ed4b2011-09-14 18:59:39 +0200240static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200241 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200242static PyObject* (*py3_PyErr_NoMemory)(void);
243static void (*py3_Py_Finalize)(void);
244static void (*py3_PyErr_SetString)(PyObject *, const char *);
245static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200246static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200247static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
248static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200249static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200250static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200251static PyObject* (*py3_PyErr_Occurred)(void);
252static PyObject* (*py3_PyModule_GetDict)(PyObject *);
253static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
254static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200255static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200256static PyObject* (*py3_PyLong_FromLong)(long);
257static PyObject* (*py3_PyDict_New)(void);
Bram Moolenaardb913952012-06-29 12:54:53 +0200258static PyObject* (*py3_PyIter_Next)(PyObject *);
259static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200260static PyObject* (*py3_Py_BuildValue)(char *, ...);
261static int (*py3_PyType_Ready)(PyTypeObject *type);
262static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
263static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200264static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
265 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200266static long (*py3_PyLong_AsLong)(PyObject *);
267static void (*py3_PyErr_SetNone)(PyObject *);
268static void (*py3_PyEval_InitThreads)(void);
269static void(*py3_PyEval_RestoreThread)(PyThreadState *);
270static PyThreadState*(*py3_PyEval_SaveThread)(void);
271static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
272static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200273static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200274static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200275static int (*py3_Py_IsInitialized)(void);
276static void (*py3_PyErr_Clear)(void);
277static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200278static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200279static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200280static PyObject* py3__Py_FalseStruct;
281static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200282static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
283static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
284static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200285static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
286static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200287static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200288static PyObject* (*py3_PyBytes_FromString)(char *str);
289static PyObject* (*py3_PyFloat_FromDouble)(double num);
290static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200291static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
292static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
293static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
294static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200295static PyTypeObject* py3_PyType_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200296static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200297static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200298static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200299static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200300static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
301static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200302# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200303 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
304 static Py_ssize_t* py3__Py_RefTotal;
305 static void (*py3__Py_Dealloc)(PyObject *obj);
306 static void (*py3__PyObject_DebugFree)(void*);
307 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200308# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200309 static void (*py3_PyObject_Free)(void*);
310 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200311# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200312static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200313
314static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
315
316/* Imported exception objects */
317static PyObject *p3imp_PyExc_AttributeError;
318static PyObject *p3imp_PyExc_IndexError;
319static PyObject *p3imp_PyExc_KeyboardInterrupt;
320static PyObject *p3imp_PyExc_TypeError;
321static PyObject *p3imp_PyExc_ValueError;
322
323# define PyExc_AttributeError p3imp_PyExc_AttributeError
324# define PyExc_IndexError p3imp_PyExc_IndexError
325# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
326# define PyExc_TypeError p3imp_PyExc_TypeError
327# define PyExc_ValueError p3imp_PyExc_ValueError
328
329/*
330 * Table of name to function pointer of python.
331 */
332# define PYTHON_PROC FARPROC
333static struct
334{
335 char *name;
336 PYTHON_PROC *ptr;
337} py3_funcname_table[] =
338{
339 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100340 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200341 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200342#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200343 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200344 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
345#else
346 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
347 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
348#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200349 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200350 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200351 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
352 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
353 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
354 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
355 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
356 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200357 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
358 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
359 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
360 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
361 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200362 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
363 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
364 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
365 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
366 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200367 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200368 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
369 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200370 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200371 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200372 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
373 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
374 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
375 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200376 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
377 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
378 {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
379 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
380 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200381 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
382 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200383 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
384 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
385 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
386 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
387 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
388 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
389 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
390 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200391 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200392 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200393 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200394 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
395 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200396 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
397 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
398 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
399 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
400 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200401 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200402 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200403 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
404 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
405 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200406 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
407 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
408 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
409 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200410 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200411 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200412 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200413 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200414 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200415# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200416 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
417 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
418 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
419 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
420 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200421# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200422 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
423 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200424# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200425 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
426 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
427 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200428 {"", NULL},
429};
430
431/*
432 * Free python.dll
433 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200434 static void
435end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200436{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200437 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200438 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200439 close_dll(hinstPy3);
440 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200441 }
442}
443
444/*
445 * Load library and get all pointers.
446 * Parameter 'libname' provides name of DLL.
447 * Return OK or FAIL.
448 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200449 static int
450py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200451{
452 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200453 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200454
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100455# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200456 /* Can't have Python and Python3 loaded at the same time.
457 * It cause a crash, because RTLD_GLOBAL is needed for
458 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200459 if (python_loaded())
460 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200461 if (verbose)
462 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200463 return FAIL;
464 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200465# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200466
467 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200468 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200469 hinstPy3 = load_dll(libname);
470
471 if (!hinstPy3)
472 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200473 if (verbose)
474 EMSG2(_(e_loadlib), libname);
475 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200476 }
477
478 for (i = 0; py3_funcname_table[i].ptr; ++i)
479 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200480 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
481 py3_funcname_table[i].name)) == NULL)
482 {
483 close_dll(hinstPy3);
484 hinstPy3 = 0;
485 if (verbose)
486 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
487 return FAIL;
488 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200489 }
490
Bram Moolenaar69154f22010-07-18 21:42:34 +0200491 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
492 * will be present in the library. */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200493 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200494 ucs_decode = symbol_from_dll(hinstPy3,
495 "PyUnicodeUCS2_Decode");
496 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
497 "PyUnicodeUCS2_AsEncodedString");
498 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200499 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200500 ucs_from_string = symbol_from_dll(hinstPy3,
501 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200502 ucs_decode = symbol_from_dll(hinstPy3,
503 "PyUnicodeUCS4_Decode");
504 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
505 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200506 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200507 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200508 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200509 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200510 py3_PyUnicode_Decode = ucs_decode;
511 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200512 }
513 else
514 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200515 close_dll(hinstPy3);
516 hinstPy3 = 0;
517 if (verbose)
518 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
519 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200520 }
521
522 return OK;
523}
524
525/*
526 * If python is enabled (there is installed python on Windows system) return
527 * TRUE, else FALSE.
528 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200529 int
530python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200531{
532 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
533}
534
535/* Load the standard Python exceptions - don't import the symbols from the
536 * DLL, as this can cause errors (importing data symbols is not reliable).
537 */
538static void get_py3_exceptions __ARGS((void));
539
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200540 static void
541get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200542{
543 PyObject *exmod = PyImport_ImportModule("builtins");
544 PyObject *exdict = PyModule_GetDict(exmod);
545 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
546 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
547 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
548 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
549 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
550 Py_XINCREF(p3imp_PyExc_AttributeError);
551 Py_XINCREF(p3imp_PyExc_IndexError);
552 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
553 Py_XINCREF(p3imp_PyExc_TypeError);
554 Py_XINCREF(p3imp_PyExc_ValueError);
555 Py_XDECREF(exmod);
556}
557#endif /* DYNAMIC_PYTHON3 */
558
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200559static PyObject *BufferNew (buf_T *);
560static PyObject *WindowNew(win_T *);
561static PyObject *LineToString(const char *);
Bram Moolenaar7f85d292012-02-04 20:17:26 +0100562static PyObject *BufferDir(PyObject *, PyObject *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200563
564static PyTypeObject RangeType;
565
Bram Moolenaardb913952012-06-29 12:54:53 +0200566static int py3initialised = 0;
567
568#define PYINITIALISED py3initialised
569
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200570#define DICTKEY_DECL PyObject *bytes = NULL;
571
Bram Moolenaardb913952012-06-29 12:54:53 +0200572#define DICTKEY_GET(err) \
573 if (PyBytes_Check(keyObject)) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200574 { \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200575 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200576 return err; \
577 } \
Bram Moolenaardb913952012-06-29 12:54:53 +0200578 else if (PyUnicode_Check(keyObject)) \
579 { \
580 bytes = PyString_AsBytes(keyObject); \
581 if (bytes == NULL) \
582 return err; \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200583 if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
Bram Moolenaardb913952012-06-29 12:54:53 +0200584 return err; \
585 } \
586 else \
587 { \
588 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
589 return err; \
590 }
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200591
Bram Moolenaardb913952012-06-29 12:54:53 +0200592#define DICTKEY_UNREF \
593 if (bytes != NULL) \
594 Py_XDECREF(bytes);
595
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200596/*
597 * Include the code shared with if_python.c
598 */
599#include "if_py_both.h"
600
Bram Moolenaar77045652012-09-21 13:46:06 +0200601#define GET_ATTR_STRING(name, nameobj) \
602 char *name = ""; \
603 if(PyUnicode_Check(nameobj)) \
604 name = _PyUnicode_AsString(nameobj)
605
Bram Moolenaardb913952012-06-29 12:54:53 +0200606#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
607
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200608 static void
609call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200610{
611#ifdef Py_DEBUG
612 _PyObject_DebugFree(p);
613#else
614 PyObject_Free(p);
615#endif
616}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200617
618 static PyObject *
619call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200620{
621 return PyType_GenericNew(type,args,kwds);
622}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200623
624 static PyObject *
625call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200626{
627 return PyType_GenericAlloc(type,nitems);
628}
629
630/******************************************************
631 * Internal function prototypes.
632 */
633
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200634static Py_ssize_t RangeStart;
635static Py_ssize_t RangeEnd;
636
Bram Moolenaardb913952012-06-29 12:54:53 +0200637static PyObject *globals;
638
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200639static int PythonIO_Init(void);
640static void PythonIO_Fini(void);
Bram Moolenaar69154f22010-07-18 21:42:34 +0200641PyMODINIT_FUNC Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200642
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200643/******************************************************
644 * 1. Python interpreter main program.
645 */
646
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200647static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
648
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200649 void
650python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200651{
652 static int recurse = 0;
653
654 /* If a crash occurs while doing this, don't try again. */
655 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200656 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200657
658 ++recurse;
659
660#ifdef DYNAMIC_PYTHON3
661 if (hinstPy3)
662#endif
663 if (Py_IsInitialized())
664 {
665 // acquire lock before finalizing
666 pygilstate = PyGILState_Ensure();
667
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200668 PythonIO_Fini();
669 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200670 }
671
672#ifdef DYNAMIC_PYTHON3
673 end_dynamic_python3();
674#endif
675
676 --recurse;
677}
678
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200679#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
680 int
681python3_loaded()
682{
683 return (hinstPy3 != 0);
684}
685#endif
686
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200687 static int
688Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200689{
690 if (!py3initialised)
691 {
692#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200693 if (!python3_enabled(TRUE))
694 {
695 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
696 goto fail;
697 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200698#endif
699
700 init_structs();
701
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100702
703#ifdef PYTHON3_HOME
704 Py_SetPythonHome(PYTHON3_HOME);
705#endif
706
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200707#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200708 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200709#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200710 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200711#endif
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200712 /* initialise threads, must be after Py_Initialize() */
713 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200714
715#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200716 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200717#endif
718
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200719 if (PythonIO_Init())
720 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200721
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200722 PyImport_AppendInittab("vim", Py3Init_vim);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200723
Bram Moolenaardb913952012-06-29 12:54:53 +0200724 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
725
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200726 /* Remove the element from sys.path that was added because of our
727 * argv[0] value in Py3Init_vim(). Previously we used an empty
728 * string, but dependinding on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200729 * the current directory in sys.path.
730 * Only after vim has been imported, the element does exist in
731 * sys.path.
732 */
733 PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200734
735 // lock is created and acquired in PyEval_InitThreads() and thread
736 // state is created in Py_Initialize()
737 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
738 // (python must have threads enabled!)
739 // so the following does both: unlock GIL and save thread state in TLS
740 // without deleting thread state
741 PyGILState_Release(pygilstate);
742
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200743 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200744 }
745
746 return 0;
747
748fail:
749 /* We call PythonIO_Flush() here to print any Python errors.
750 * This is OK, as it is possible to call this function even
751 * if PythonIO_Init() has not completed successfully (it will
752 * not do anything in this case).
753 */
754 PythonIO_Flush();
755 return -1;
756}
757
758/*
759 * External interface
760 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200761 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200762DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200763{
764#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200765 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200766#endif
767#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200768 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200769#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200770 PyObject *cmdstr;
771 PyObject *cmdbytes;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200772
773#if defined(MACOS) && !defined(MACOS_X_UNIX)
774 GetPort(&oldPort);
775 /* Check if the Python library is available */
776 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200777 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200778#endif
779 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200780 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200781
Bram Moolenaardb913952012-06-29 12:54:53 +0200782 if (rettv == NULL)
783 {
784 RangeStart = eap->line1;
785 RangeEnd = eap->line2;
786 }
787 else
788 {
789 RangeStart = (PyInt) curwin->w_cursor.lnum;
790 RangeEnd = RangeStart;
791 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200792 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200793
794#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
795 /* Python only works properly when the LC_NUMERIC locale is "C". */
796 saved_locale = setlocale(LC_NUMERIC, NULL);
797 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200798 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200799 else
800 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200801 /* Need to make a copy, value may change when setting new locale. */
802 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
803 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200804 }
805#endif
806
807 pygilstate = PyGILState_Ensure();
808
Bram Moolenaar19e60942011-06-19 00:27:51 +0200809 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
810 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200811 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
812 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
813 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200814 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200815 if (rettv == NULL)
816 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
817 else
818 {
819 PyObject *r;
820
821 r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
822 globals, globals);
823 if (r == NULL)
824 EMSG(_("E860: Eval did not return a valid python 3 object"));
825 else
826 {
827 if (ConvertFromPyObject(r, rettv) == -1)
828 EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
829 Py_DECREF(r);
830 }
831 PyErr_Clear();
832 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200833 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200834
835 PyGILState_Release(pygilstate);
836
837#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
838 if (saved_locale != NULL)
839 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200840 (void)setlocale(LC_NUMERIC, saved_locale);
841 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200842 }
843#endif
844
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200845 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200846 PythonIO_Flush();
847#if defined(MACOS) && !defined(MACOS_X_UNIX)
848 SetPort(oldPort);
849#endif
850
851theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200852 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200853}
854
855/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200856 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200857 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200858 void
859ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200860{
861 char_u *script;
862
863 script = script_get(eap, eap->arg);
864 if (!eap->skip)
865 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200866 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200867 DoPy3Command(eap, (char *)eap->arg, NULL);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200868 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200869 DoPy3Command(eap, (char *)script, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200870 }
871 vim_free(script);
872}
873
874#define BUFFER_SIZE 2048
875
876/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200877 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200878 */
879 void
880ex_py3file(exarg_T *eap)
881{
882 static char buffer[BUFFER_SIZE];
883 const char *file;
884 char *p;
885 int i;
886
887 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
888 * stdio file pointer, but Vim and the Python DLL are compiled with
889 * different options under Windows, meaning that stdio pointers aren't
890 * compatible between the two. Yuk.
891 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200892 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
893 *
894 * Using bytes so that Python can detect the source encoding as it normally
895 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200896 *
897 * We need to escape any backslashes or single quotes in the file name, so that
898 * Python won't mangle the file name.
899 */
900
901 strcpy(buffer, "exec(compile(open('");
902 p = buffer + 19; /* size of "exec(compile(open('" */
903
904 for (i=0; i<2; ++i)
905 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200906 file = (char *)eap->arg;
907 while (*file && p < buffer + (BUFFER_SIZE - 3))
908 {
909 if (*file == '\\' || *file == '\'')
910 *p++ = '\\';
911 *p++ = *file++;
912 }
913 /* If we didn't finish the file name, we hit a buffer overflow */
914 if (*file != '\0')
915 return;
916 if (i==0)
917 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200918 strcpy(p,"','rb').read(),'");
919 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200920 }
921 else
922 {
923 strcpy(p,"','exec'))");
924 p += 10;
925 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200926 }
927
928
929 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200930 DoPy3Command(eap, buffer, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200931}
932
933/******************************************************
934 * 2. Python output stream: writes output via [e]msg().
935 */
936
937/* Implementation functions
938 */
939
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200940 static PyObject *
941OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200942{
Bram Moolenaar77045652012-09-21 13:46:06 +0200943 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200944
945 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200946 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200947
948 return PyObject_GenericGetAttr(self, nameobj);
949}
950
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200951 static int
952OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200953{
Bram Moolenaar77045652012-09-21 13:46:06 +0200954 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200955
Bram Moolenaar77045652012-09-21 13:46:06 +0200956 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200957}
958
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200959/***************/
960
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200961 static int
962PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200963{
964 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200965 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200966}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200967
968 static void
969PythonIO_Fini(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200970{
971 PySys_SetObject("stdout", NULL);
972 PySys_SetObject("stderr", NULL);
973}
974
975/******************************************************
976 * 3. Implementation of the Vim module for Python
977 */
978
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200979/* Window type - Implementation functions
980 * --------------------------------------
981 */
982
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200983#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
984
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200985/* Buffer type - Implementation functions
986 * --------------------------------------
987 */
988
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200989#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
990
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200991static Py_ssize_t BufferLength(PyObject *);
992static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +0200993static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
994static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200995
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200996
997/* Line range type - Implementation functions
998 * --------------------------------------
999 */
1000
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001001#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1002
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001003static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001004static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001005static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001006
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001007/* Current objects type - Implementation functions
1008 * -----------------------------------------------
1009 */
1010
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001011static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001012 (lenfunc) BufferLength, /* sq_length, len(x) */
1013 (binaryfunc) 0, /* sq_concat, x+y */
1014 (ssizeargfunc) 0, /* sq_repeat, x*n */
1015 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1016 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001017 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001018 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001019 0, /* sq_contains */
1020 0, /* sq_inplace_concat */
1021 0, /* sq_inplace_repeat */
1022};
1023
1024PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001025 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001026 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001027 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001028};
1029
1030
1031/* Buffer object - Definitions
1032 */
1033
1034static PyTypeObject BufferType;
1035
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001036 static PyObject *
1037BufferNew(buf_T *buf)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001038{
1039 /* We need to handle deletion of buffers underneath us.
1040 * If we add a "b_python3_ref" field to the buf_T structure,
1041 * then we can get at it in buf_freeall() in vim. We then
1042 * need to create only ONE Python object per buffer - if
1043 * we try to create a second, just INCREF the existing one
1044 * and return it. The (single) Python object referring to
1045 * the buffer is stored in "b_python3_ref".
1046 * Question: what to do on a buf_freeall(). We'll probably
1047 * have to either delete the Python object (DECREF it to
1048 * zero - a bad idea, as it leaves dangling refs!) or
1049 * set the buf_T * value to an invalid value (-1?), which
1050 * means we need checks in all access functions... Bah.
1051 */
1052
1053 BufferObject *self;
1054
1055 if (buf->b_python3_ref != NULL)
1056 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001057 self = buf->b_python3_ref;
1058 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001059 }
1060 else
1061 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001062 self = PyObject_NEW(BufferObject, &BufferType);
1063 buf->b_python3_ref = self;
1064 if (self == NULL)
1065 return NULL;
1066 self->buf = buf;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001067 }
1068
1069 return (PyObject *)(self);
1070}
1071
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001072 static void
1073BufferDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001074{
1075 BufferObject *this = (BufferObject *)(self);
1076
1077 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001078 this->buf->b_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001079
1080 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001081}
1082
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001083 static PyObject *
1084BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001085{
1086 BufferObject *this = (BufferObject *)(self);
1087
Bram Moolenaar77045652012-09-21 13:46:06 +02001088 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001089
1090 if (CheckBuffer(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001091 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001092
1093 if (strcmp(name, "name") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001094 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001095 else if (strcmp(name, "number") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001096 return Py_BuildValue("n", this->buf->b_fnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001097 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001098 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001099}
1100
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001101 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001102BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1103{
1104 return Py_BuildValue("[sssss]", "name", "number",
1105 "append", "mark", "range");
1106}
1107
1108 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001109BufferRepr(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001110{
1111 static char repr[100];
1112 BufferObject *this = (BufferObject *)(self);
1113
1114 if (this->buf == INVALID_BUFFER_VALUE)
1115 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001116 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
1117 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001118 }
1119 else
1120 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001121 char *name = (char *)this->buf->b_fname;
1122 Py_ssize_t len;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001123
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001124 if (name == NULL)
1125 name = "";
1126 len = strlen(name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001127
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001128 if (len > 35)
1129 name = name + (35 - len);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001130
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001131 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001132
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001133 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001134 }
1135}
1136
1137/******************/
1138
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001139 static Py_ssize_t
1140BufferLength(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001141{
1142 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001143 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001144
1145 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1146}
1147
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001148 static PyObject *
1149BufferItem(PyObject *self, Py_ssize_t n)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001150{
1151 return RBItem((BufferObject *)(self), n, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001152 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001153}
1154
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001155 static PyObject *
1156BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1157{
1158 return RBSlice((BufferObject *)(self), lo, hi, 1,
1159 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1160}
1161
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001162 static PyObject *
1163BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001164{
Bram Moolenaardb913952012-06-29 12:54:53 +02001165 if (PyLong_Check(idx))
1166 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001167 long _idx = PyLong_AsLong(idx);
1168 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001169 } else if (PySlice_Check(idx))
1170 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001171 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001172
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001173 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001174 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1175 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001176 &step, &slicelen) < 0)
1177 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001178 return NULL;
1179 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001180 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001181 }
1182 else
1183 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001184 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1185 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001186 }
1187}
1188
Bram Moolenaar19e60942011-06-19 00:27:51 +02001189 static Py_ssize_t
1190BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1191{
Bram Moolenaardb913952012-06-29 12:54:53 +02001192 if (PyLong_Check(idx))
1193 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001194 long n = PyLong_AsLong(idx);
1195 return RBAsItem((BufferObject *)(self), n, val, 1,
1196 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1197 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001198 } else if (PySlice_Check(idx))
1199 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001200 Py_ssize_t start, stop, step, slicelen;
1201
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001202 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001203 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1204 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001205 &step, &slicelen) < 0)
1206 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001207 return -1;
1208 }
1209 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1210 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1211 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001212 }
1213 else
1214 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001215 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1216 return -1;
1217 }
1218}
1219
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001220static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001221 (lenfunc) RangeLength, /* sq_length, len(x) */
1222 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1223 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1224 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1225 0, /* was_sq_slice, x[i:j] */
1226 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1227 0, /* sq_ass_slice, x[i:j]=v */
1228 0, /* sq_contains */
1229 0, /* sq_inplace_concat */
1230 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001231};
1232
1233PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001234 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001235 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001236 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001237};
1238
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001239/* Line range object - Implementation
1240 */
1241
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001242 static void
1243RangeDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001244{
1245 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001246 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001247}
1248
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001249 static PyObject *
1250RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001251{
Bram Moolenaar77045652012-09-21 13:46:06 +02001252 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001253
1254 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001255 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001256 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001257 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001258 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001259 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001260}
1261
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001262/****************/
1263
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001264 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001265RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001266{
1267 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001268 ((RangeObject *)(self))->start,
1269 ((RangeObject *)(self))->end,
1270 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001271}
1272
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001273 static Py_ssize_t
1274RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1275{
1276 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1277 ((RangeObject *)(self))->start,
1278 ((RangeObject *)(self))->end,
1279 &((RangeObject *)(self))->end);
1280}
1281
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001282 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001283RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001284{
Bram Moolenaardb913952012-06-29 12:54:53 +02001285 if (PyLong_Check(idx))
1286 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001287 long _idx = PyLong_AsLong(idx);
1288 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001289 } else if (PySlice_Check(idx))
1290 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001291 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001292
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001293 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001294 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1295 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001296 &step, &slicelen) < 0)
1297 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001298 return NULL;
1299 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001300 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001301 }
1302 else
1303 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001304 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1305 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001306 }
1307}
1308
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001309 static Py_ssize_t
1310RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1311{
Bram Moolenaardb913952012-06-29 12:54:53 +02001312 if (PyLong_Check(idx))
1313 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001314 long n = PyLong_AsLong(idx);
1315 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001316 } else if (PySlice_Check(idx))
1317 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001318 Py_ssize_t start, stop, step, slicelen;
1319
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001320 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001321 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1322 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001323 &step, &slicelen) < 0)
1324 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001325 return -1;
1326 }
1327 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001328 }
1329 else
1330 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001331 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1332 return -1;
1333 }
1334}
1335
1336
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001337/* Buffer list object - Definitions
1338 */
1339
1340typedef struct
1341{
1342 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001343} BufListObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001344
1345static PySequenceMethods BufListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001346 (lenfunc) BufListLength, /* sq_length, len(x) */
1347 (binaryfunc) 0, /* sq_concat, x+y */
1348 (ssizeargfunc) 0, /* sq_repeat, x*n */
1349 (ssizeargfunc) BufListItem, /* sq_item, x[i] */
1350 0, /* was_sq_slice, x[i:j] */
1351 (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */
1352 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001353 0, /* sq_contains */
1354 0, /* sq_inplace_concat */
1355 0, /* sq_inplace_repeat */
1356};
1357
1358static PyTypeObject BufListType;
1359
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001360/* Window object - Definitions
1361 */
1362
1363static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001364 /* name, function, calling, documentation */
1365 { NULL, NULL, 0, NULL }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001366};
1367
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001368static PyTypeObject WindowType;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001369
1370/* Window object - Implementation
1371 */
1372
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001373 static PyObject *
1374WindowNew(win_T *win)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001375{
1376 /* We need to handle deletion of windows underneath us.
1377 * If we add a "w_python3_ref" field to the win_T structure,
1378 * then we can get at it in win_free() in vim. We then
1379 * need to create only ONE Python object per window - if
1380 * we try to create a second, just INCREF the existing one
1381 * and return it. The (single) Python object referring to
1382 * the window is stored in "w_python3_ref".
1383 * On a win_free() we set the Python object's win_T* field
1384 * to an invalid value. We trap all uses of a window
1385 * object, and reject them if the win_T* field is invalid.
1386 */
1387
1388 WindowObject *self;
1389
1390 if (win->w_python3_ref)
1391 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001392 self = win->w_python3_ref;
1393 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001394 }
1395 else
1396 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001397 self = PyObject_NEW(WindowObject, &WindowType);
1398 if (self == NULL)
1399 return NULL;
1400 self->win = win;
1401 win->w_python3_ref = self;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001402 }
1403
1404 return (PyObject *)(self);
1405}
1406
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001407 static void
1408WindowDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001409{
1410 WindowObject *this = (WindowObject *)(self);
1411
1412 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001413 this->win->w_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001414
1415 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001416}
1417
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001418 static PyObject *
1419WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001420{
1421 WindowObject *this = (WindowObject *)(self);
1422
Bram Moolenaar77045652012-09-21 13:46:06 +02001423 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001424
1425 if (CheckWindow(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001426 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001427
1428 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001429 return (PyObject *)BufferNew(this->win->w_buffer);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001430 else if (strcmp(name, "cursor") == 0)
1431 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001432 pos_T *pos = &this->win->w_cursor;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001433
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001434 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001435 }
1436 else if (strcmp(name, "height") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001437 return Py_BuildValue("l", (long)(this->win->w_height));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001438#ifdef FEAT_VERTSPLIT
1439 else if (strcmp(name, "width") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001440 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001441#endif
1442 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001443 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001444 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001445 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001446}
1447
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001448 static int
1449WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001450{
Bram Moolenaar77045652012-09-21 13:46:06 +02001451 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001452
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001453 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001454}
1455
1456/* Window list object - Definitions
1457 */
1458
1459typedef struct
1460{
1461 PyObject_HEAD
1462}
1463WinListObject;
1464
1465static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001466 (lenfunc) WinListLength, /* sq_length, len(x) */
1467 (binaryfunc) 0, /* sq_concat, x+y */
1468 (ssizeargfunc) 0, /* sq_repeat, x*n */
1469 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1470 0, /* sq_slice, x[i:j] */
1471 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1472 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001473 0, /* sq_contains */
1474 0, /* sq_inplace_concat */
1475 0, /* sq_inplace_repeat */
1476};
1477
1478static PyTypeObject WinListType;
1479
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001480/* Current items object - Definitions
1481 */
1482
1483typedef struct
1484{
1485 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001486} CurrentObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001487
1488static PyTypeObject CurrentType;
1489
1490/* Current items object - Implementation
1491 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001492 static PyObject *
1493CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001494{
Bram Moolenaar77045652012-09-21 13:46:06 +02001495 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001496
1497 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001498 return (PyObject *)BufferNew(curbuf);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001499 else if (strcmp(name, "window") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001500 return (PyObject *)WindowNew(curwin);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001501 else if (strcmp(name, "line") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001502 return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001503 else if (strcmp(name, "range") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001504 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001505 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001506 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001507 else
1508 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001509 PyErr_SetString(PyExc_AttributeError, name);
1510 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001511 }
1512}
1513
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001514 static int
1515CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001516{
1517 char *name = "";
1518 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001519 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001520
1521 if (strcmp(name, "line") == 0)
1522 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001523 if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1524 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001525
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001526 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001527 }
1528 else
1529 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001530 PyErr_SetString(PyExc_AttributeError, name);
1531 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001532 }
1533}
1534
Bram Moolenaardb913952012-06-29 12:54:53 +02001535/* Dictionary object - Definitions
1536 */
1537
1538static PyInt DictionaryLength(PyObject *);
1539
1540static PyMappingMethods DictionaryAsMapping = {
1541 /* mp_length */ (lenfunc) DictionaryLength,
1542 /* mp_subscript */ (binaryfunc) DictionaryItem,
1543 /* mp_ass_subscript */ (objobjargproc) DictionaryAssItem,
1544};
1545
Bram Moolenaar66b79852012-09-21 14:00:35 +02001546 static PyObject *
1547DictionaryGetattro(PyObject *self, PyObject *nameobj)
1548{
1549 DictionaryObject *this = ((DictionaryObject *) (self));
1550
1551 GET_ATTR_STRING(name, nameobj);
1552
1553 if (strcmp(name, "locked") == 0)
1554 return PyLong_FromLong(this->dict->dv_lock);
1555 else if (strcmp(name, "scope") == 0)
1556 return PyLong_FromLong(this->dict->dv_scope);
1557
1558 return PyObject_GenericGetAttr(self, nameobj);
1559}
1560
1561 static int
1562DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1563{
1564 GET_ATTR_STRING(name, nameobj);
1565 return DictionarySetattr((DictionaryObject *) self, name, val);
1566}
1567
Bram Moolenaardb913952012-06-29 12:54:53 +02001568static PyTypeObject DictionaryType;
1569
1570 static void
1571DictionaryDestructor(PyObject *self)
1572{
1573 DictionaryObject *this = (DictionaryObject *)(self);
1574
1575 pyll_remove(&this->ref, &lastdict);
1576 dict_unref(this->dict);
1577
1578 Py_TYPE(self)->tp_free((PyObject*)self);
1579}
1580
1581/* List object - Definitions
1582 */
1583
1584static PyInt ListLength(PyObject *);
1585static PyObject *ListItem(PyObject *, Py_ssize_t);
1586
1587static PySequenceMethods ListAsSeq = {
1588 (lenfunc) ListLength, /* sq_length, len(x) */
1589 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1590 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1591 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1592 (void *) 0, /* was_sq_slice, x[i:j] */
1593 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1594 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1595 0, /* sq_contains */
1596 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1597 0, /* sq_inplace_repeat */
1598};
1599
1600static PyObject *ListSubscript(PyObject *, PyObject *);
1601static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1602
1603static PyMappingMethods ListAsMapping = {
1604 /* mp_length */ (lenfunc) ListLength,
1605 /* mp_subscript */ (binaryfunc) ListSubscript,
1606 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1607};
1608
1609static PyTypeObject ListType;
1610
1611 static PyObject *
1612ListSubscript(PyObject *self, PyObject* idxObject)
1613{
1614 if (PyLong_Check(idxObject))
1615 {
1616 long idx = PyLong_AsLong(idxObject);
1617 return ListItem(self, idx);
1618 }
1619 else if (PySlice_Check(idxObject))
1620 {
1621 Py_ssize_t start, stop, step, slicelen;
1622
1623 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1624 &step, &slicelen) < 0)
1625 return NULL;
1626 return ListSlice(self, start, stop);
1627 }
1628 else
1629 {
1630 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1631 return NULL;
1632 }
1633}
1634
1635 static Py_ssize_t
1636ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1637{
1638 if (PyLong_Check(idxObject))
1639 {
1640 long idx = PyLong_AsLong(idxObject);
1641 return ListAssItem(self, idx, obj);
1642 }
1643 else if (PySlice_Check(idxObject))
1644 {
1645 Py_ssize_t start, stop, step, slicelen;
1646
1647 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1648 &step, &slicelen) < 0)
1649 return -1;
1650 return ListAssSlice(self, start, stop, obj);
1651 }
1652 else
1653 {
1654 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1655 return -1;
1656 }
1657}
1658
Bram Moolenaar66b79852012-09-21 14:00:35 +02001659 static PyObject *
1660ListGetattro(PyObject *self, PyObject *nameobj)
1661{
1662 GET_ATTR_STRING(name, nameobj);
1663
1664 if (strcmp(name, "locked") == 0)
1665 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1666
1667 return PyObject_GenericGetAttr(self, nameobj);
1668}
1669
1670 static int
1671ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1672{
1673 GET_ATTR_STRING(name, nameobj);
1674 return ListSetattr((ListObject *) self, name, val);
1675}
1676
Bram Moolenaardb913952012-06-29 12:54:53 +02001677 static void
1678ListDestructor(PyObject *self)
1679{
1680 ListObject *this = (ListObject *)(self);
1681
1682 pyll_remove(&this->ref, &lastlist);
1683 list_unref(this->list);
1684
1685 Py_TYPE(self)->tp_free((PyObject*)self);
1686}
1687
1688/* Function object - Definitions
1689 */
1690
1691 static void
1692FunctionDestructor(PyObject *self)
1693{
1694 FunctionObject *this = (FunctionObject *) (self);
1695
1696 func_unref(this->name);
1697 PyMem_Del(this->name);
1698
1699 Py_TYPE(self)->tp_free((PyObject*)self);
1700}
1701
1702 static PyObject *
1703FunctionGetattro(PyObject *self, PyObject *nameobj)
1704{
1705 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001706
1707 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001708
1709 if (strcmp(name, "name") == 0)
1710 return PyUnicode_FromString((char *)(this->name));
1711
1712 return PyObject_GenericGetAttr(self, nameobj);
1713}
1714
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001715/* External interface
1716 */
1717
1718 void
1719python3_buffer_free(buf_T *buf)
1720{
1721 if (buf->b_python3_ref != NULL)
1722 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001723 BufferObject *bp = buf->b_python3_ref;
1724 bp->buf = INVALID_BUFFER_VALUE;
1725 buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001726 }
1727}
1728
1729#if defined(FEAT_WINDOWS) || defined(PROTO)
1730 void
1731python3_window_free(win_T *win)
1732{
1733 if (win->w_python3_ref != NULL)
1734 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001735 WindowObject *wp = win->w_python3_ref;
1736 wp->win = INVALID_WINDOW_VALUE;
1737 win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001738 }
1739}
1740#endif
1741
1742static BufListObject TheBufferList =
1743{
1744 PyObject_HEAD_INIT(&BufListType)
1745};
1746
1747static WinListObject TheWindowList =
1748{
1749 PyObject_HEAD_INIT(&WinListType)
1750};
1751
1752static CurrentObject TheCurrent =
1753{
1754 PyObject_HEAD_INIT(&CurrentType)
1755};
1756
1757PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1758
1759static struct PyModuleDef vimmodule;
1760
Bram Moolenaar69154f22010-07-18 21:42:34 +02001761#ifndef PROTO
1762PyMODINIT_FUNC Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001763{
1764 PyObject *mod;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001765 PyObject *tmp;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001766 /* The special value is removed from sys.path in Python3_Init(). */
1767 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1768
1769 PyType_Ready(&BufferType);
1770 PyType_Ready(&RangeType);
1771 PyType_Ready(&WindowType);
1772 PyType_Ready(&BufListType);
1773 PyType_Ready(&WinListType);
1774 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001775 PyType_Ready(&DictionaryType);
1776 PyType_Ready(&ListType);
1777 PyType_Ready(&FunctionType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001778
1779 /* Set sys.argv[] to avoid a crash in warn(). */
1780 PySys_SetArgv(1, argv);
1781
1782 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001783 if (mod == NULL)
1784 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001785
Bram Moolenaar19e60942011-06-19 00:27:51 +02001786 VimError = PyErr_NewException("vim.error", NULL, NULL);
1787 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001788
1789 PyModule_AddObject(mod, "error", VimError);
1790 Py_INCREF((PyObject *)(void *)&TheBufferList);
1791 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1792 Py_INCREF((PyObject *)(void *)&TheCurrent);
1793 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1794 Py_INCREF((PyObject *)(void *)&TheWindowList);
1795 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1796
Bram Moolenaar66b79852012-09-21 14:00:35 +02001797#define ADD_INT_CONSTANT(name, value) \
1798 tmp = PyLong_FromLong(value); \
1799 Py_INCREF(tmp); \
1800 PyModule_AddObject(mod, name, tmp)
1801
1802 ADD_INT_CONSTANT("VAR_LOCKED", VAR_LOCKED);
1803 ADD_INT_CONSTANT("VAR_FIXED", VAR_FIXED);
1804 ADD_INT_CONSTANT("VAR_SCOPE", VAR_SCOPE);
1805 ADD_INT_CONSTANT("VAR_DEF_SCOPE", VAR_DEF_SCOPE);
1806
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001807 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001808 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001809
1810 return mod;
1811}
Bram Moolenaar69154f22010-07-18 21:42:34 +02001812#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001813
1814/*************************************************************************
1815 * 4. Utility functions for handling the interface between Vim and Python.
1816 */
1817
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001818/* Convert a Vim line into a Python string.
1819 * All internal newlines are replaced by null characters.
1820 *
1821 * On errors, the Python exception data is set, and NULL is returned.
1822 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001823 static PyObject *
1824LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001825{
1826 PyObject *result;
1827 Py_ssize_t len = strlen(str);
1828 char *tmp,*p;
1829
1830 tmp = (char *)alloc((unsigned)(len+1));
1831 p = tmp;
1832 if (p == NULL)
1833 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001834 PyErr_NoMemory();
1835 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001836 }
1837
1838 while (*str)
1839 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001840 if (*str == '\n')
1841 *p = '\0';
1842 else
1843 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001844
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001845 ++p;
1846 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001847 }
1848 *p = '\0';
1849
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001850 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001851
1852 vim_free(tmp);
1853 return result;
1854}
1855
Bram Moolenaardb913952012-06-29 12:54:53 +02001856 void
1857do_py3eval (char_u *str, typval_T *rettv)
1858{
1859 DoPy3Command(NULL, (char *) str, rettv);
1860 switch(rettv->v_type)
1861 {
1862 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1863 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1864 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001865 case VAR_UNKNOWN:
1866 rettv->v_type = VAR_NUMBER;
1867 rettv->vval.v_number = 0;
1868 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001869 }
1870}
1871
1872 void
1873set_ref_in_python3 (int copyID)
1874{
1875 set_ref_in_py(copyID);
1876}
1877
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001878 static void
1879init_structs(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001880{
1881 vim_memset(&OutputType, 0, sizeof(OutputType));
1882 OutputType.tp_name = "vim.message";
1883 OutputType.tp_basicsize = sizeof(OutputObject);
1884 OutputType.tp_getattro = OutputGetattro;
1885 OutputType.tp_setattro = OutputSetattro;
1886 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1887 OutputType.tp_doc = "vim message object";
1888 OutputType.tp_methods = OutputMethods;
1889 OutputType.tp_alloc = call_PyType_GenericAlloc;
1890 OutputType.tp_new = call_PyType_GenericNew;
1891 OutputType.tp_free = call_PyObject_Free;
1892
1893 vim_memset(&BufferType, 0, sizeof(BufferType));
1894 BufferType.tp_name = "vim.buffer";
1895 BufferType.tp_basicsize = sizeof(BufferType);
1896 BufferType.tp_dealloc = BufferDestructor;
1897 BufferType.tp_repr = BufferRepr;
1898 BufferType.tp_as_sequence = &BufferAsSeq;
1899 BufferType.tp_as_mapping = &BufferAsMapping;
1900 BufferType.tp_getattro = BufferGetattro;
1901 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1902 BufferType.tp_doc = "vim buffer object";
1903 BufferType.tp_methods = BufferMethods;
1904 BufferType.tp_alloc = call_PyType_GenericAlloc;
1905 BufferType.tp_new = call_PyType_GenericNew;
1906 BufferType.tp_free = call_PyObject_Free;
1907
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001908 vim_memset(&WindowType, 0, sizeof(WindowType));
1909 WindowType.tp_name = "vim.window";
1910 WindowType.tp_basicsize = sizeof(WindowObject);
1911 WindowType.tp_dealloc = WindowDestructor;
1912 WindowType.tp_repr = WindowRepr;
1913 WindowType.tp_getattro = WindowGetattro;
1914 WindowType.tp_setattro = WindowSetattro;
1915 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
1916 WindowType.tp_doc = "vim Window object";
1917 WindowType.tp_methods = WindowMethods;
1918 WindowType.tp_alloc = call_PyType_GenericAlloc;
1919 WindowType.tp_new = call_PyType_GenericNew;
1920 WindowType.tp_free = call_PyObject_Free;
1921
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001922 vim_memset(&BufListType, 0, sizeof(BufListType));
1923 BufListType.tp_name = "vim.bufferlist";
1924 BufListType.tp_basicsize = sizeof(BufListObject);
1925 BufListType.tp_as_sequence = &BufListAsSeq;
1926 BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1927 BufferType.tp_doc = "vim buffer list";
1928
1929 vim_memset(&WinListType, 0, sizeof(WinListType));
1930 WinListType.tp_name = "vim.windowlist";
1931 WinListType.tp_basicsize = sizeof(WinListType);
1932 WinListType.tp_as_sequence = &WinListAsSeq;
1933 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1934 WinListType.tp_doc = "vim window list";
1935
1936 vim_memset(&RangeType, 0, sizeof(RangeType));
1937 RangeType.tp_name = "vim.range";
1938 RangeType.tp_basicsize = sizeof(RangeObject);
1939 RangeType.tp_dealloc = RangeDestructor;
1940 RangeType.tp_repr = RangeRepr;
1941 RangeType.tp_as_sequence = &RangeAsSeq;
1942 RangeType.tp_as_mapping = &RangeAsMapping;
1943 RangeType.tp_getattro = RangeGetattro;
1944 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1945 RangeType.tp_doc = "vim Range object";
1946 RangeType.tp_methods = RangeMethods;
1947 RangeType.tp_alloc = call_PyType_GenericAlloc;
1948 RangeType.tp_new = call_PyType_GenericNew;
1949 RangeType.tp_free = call_PyObject_Free;
1950
1951 vim_memset(&CurrentType, 0, sizeof(CurrentType));
1952 CurrentType.tp_name = "vim.currentdata";
1953 CurrentType.tp_basicsize = sizeof(CurrentObject);
1954 CurrentType.tp_getattro = CurrentGetattro;
1955 CurrentType.tp_setattro = CurrentSetattro;
1956 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1957 CurrentType.tp_doc = "vim current object";
1958
Bram Moolenaardb913952012-06-29 12:54:53 +02001959 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
1960 DictionaryType.tp_name = "vim.dictionary";
1961 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001962 DictionaryType.tp_getattro = DictionaryGetattro;
1963 DictionaryType.tp_setattro = DictionarySetattro;
Bram Moolenaardb913952012-06-29 12:54:53 +02001964 DictionaryType.tp_dealloc = DictionaryDestructor;
1965 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
1966 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
1967 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
1968 DictionaryType.tp_methods = DictionaryMethods;
1969
1970 vim_memset(&ListType, 0, sizeof(ListType));
1971 ListType.tp_name = "vim.list";
1972 ListType.tp_dealloc = ListDestructor;
1973 ListType.tp_basicsize = sizeof(ListObject);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001974 ListType.tp_getattro = ListGetattro;
1975 ListType.tp_setattro = ListSetattro;
Bram Moolenaardb913952012-06-29 12:54:53 +02001976 ListType.tp_as_sequence = &ListAsSeq;
1977 ListType.tp_as_mapping = &ListAsMapping;
1978 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
1979 ListType.tp_doc = "list pushing modifications to vim structure";
1980 ListType.tp_methods = ListMethods;
1981
1982 vim_memset(&FunctionType, 0, sizeof(FunctionType));
1983 FunctionType.tp_name = "vim.list";
1984 FunctionType.tp_basicsize = sizeof(FunctionObject);
1985 FunctionType.tp_getattro = FunctionGetattro;
1986 FunctionType.tp_dealloc = FunctionDestructor;
1987 FunctionType.tp_call = FunctionCall;
1988 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
1989 FunctionType.tp_doc = "object that calls vim function";
1990 FunctionType.tp_methods = FunctionMethods;
1991
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001992 vim_memset(&vimmodule, 0, sizeof(vimmodule));
1993 vimmodule.m_name = "vim";
1994 vimmodule.m_doc = vim_module_doc;
1995 vimmodule.m_size = -1;
1996 vimmodule.m_methods = VimMethods;
1997}