Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | /* |
| 10 | * Python extensions by Paul Moore. |
| 11 | * Changes for Unix by David Leonard. |
| 12 | * |
| 13 | * This consists of four parts: |
| 14 | * 1. Python interpreter main program |
| 15 | * 2. Python output stream: writes output via [e]msg(). |
| 16 | * 3. Implementation of the Vim module for Python |
| 17 | * 4. Utility functions for handling the interface between Vim and Python. |
| 18 | */ |
| 19 | |
| 20 | #include "vim.h" |
| 21 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | #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 Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 29 | #if defined(_WIN32) && defined(HAVE_FCNTL_H) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 30 | # 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 Moolenaar | be2c9ae | 2009-11-11 14:06:59 +0000 | [diff] [blame] | 40 | #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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 47 | #define PY_SSIZE_T_CLEAN |
| 48 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 49 | #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 Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 57 | static void init_structs(void); |
| 58 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 59 | /* No-op conversion functions, use with care! */ |
| 60 | #define PyString_AsBytes(obj) (obj) |
| 61 | #define PyString_FreeBytes(obj) |
| 62 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 63 | #if !defined(FEAT_PYTHON) && defined(PROTO) |
| 64 | /* Use this to be able to generate prototypes without python being used. */ |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 65 | # define PyObject Py_ssize_t |
| 66 | # define PyThreadState Py_ssize_t |
| 67 | # define PyTypeObject Py_ssize_t |
| 68 | struct PyMethodDef { Py_ssize_t a; }; |
| 69 | # define PySequenceMethods Py_ssize_t |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | #endif |
| 71 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 72 | #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 |
| 73 | # define PyInt Py_ssize_t |
| 74 | # define PyInquiry lenfunc |
| 75 | # define PyIntArgFunc ssizeargfunc |
| 76 | # define PyIntIntArgFunc ssizessizeargfunc |
| 77 | # define PyIntObjArgProc ssizeobjargproc |
| 78 | # define PyIntIntObjArgProc ssizessizeobjargproc |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 79 | # define Py_ssize_t_fmt "n" |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 80 | #else |
| 81 | # define PyInt int |
| 82 | # define PyInquiry inquiry |
| 83 | # define PyIntArgFunc intargfunc |
| 84 | # define PyIntIntArgFunc intintargfunc |
| 85 | # define PyIntObjArgProc intobjargproc |
| 86 | # define PyIntIntObjArgProc intintobjargproc |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 87 | # define Py_ssize_t_fmt "i" |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 88 | #endif |
| 89 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 90 | /* Parser flags */ |
| 91 | #define single_input 256 |
| 92 | #define file_input 257 |
| 93 | #define eval_input 258 |
| 94 | |
| 95 | #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0 |
| 96 | /* Python 2.3: can invoke ":python" recursively. */ |
| 97 | # define PY_CAN_RECURSE |
| 98 | #endif |
| 99 | |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 100 | # if defined(DYNAMIC_PYTHON) || defined(PROTO) |
| 101 | # ifndef DYNAMIC_PYTHON |
| 102 | # define HINSTANCE long_u /* for generating prototypes */ |
| 103 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 104 | |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 105 | # ifndef WIN3264 |
| 106 | # include <dlfcn.h> |
| 107 | # define FARPROC void* |
| 108 | # define HINSTANCE void* |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 109 | # if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL) |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 110 | # define load_dll(n) dlopen((n), RTLD_LAZY) |
| 111 | # else |
| 112 | # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) |
| 113 | # endif |
| 114 | # define close_dll dlclose |
| 115 | # define symbol_from_dll dlsym |
| 116 | # else |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 117 | # define load_dll vimLoadLib |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 118 | # define close_dll FreeLibrary |
| 119 | # define symbol_from_dll GetProcAddress |
| 120 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 121 | |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 122 | /* This makes if_python.c compile without warnings against Python 2.5 |
| 123 | * on Win32 and Win64. */ |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 124 | # undef PyRun_SimpleString |
| 125 | # undef PyArg_Parse |
| 126 | # undef PyArg_ParseTuple |
| 127 | # undef Py_BuildValue |
| 128 | # undef Py_InitModule4 |
| 129 | # undef Py_InitModule4_64 |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 130 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | /* |
| 132 | * Wrapper defines |
| 133 | */ |
| 134 | # define PyArg_Parse dll_PyArg_Parse |
| 135 | # define PyArg_ParseTuple dll_PyArg_ParseTuple |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 136 | # define PyMem_Free dll_PyMem_Free |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 137 | # define PyDict_SetItemString dll_PyDict_SetItemString |
| 138 | # define PyErr_BadArgument dll_PyErr_BadArgument |
| 139 | # define PyErr_Clear dll_PyErr_Clear |
| 140 | # define PyErr_NoMemory dll_PyErr_NoMemory |
| 141 | # define PyErr_Occurred dll_PyErr_Occurred |
| 142 | # define PyErr_SetNone dll_PyErr_SetNone |
| 143 | # define PyErr_SetString dll_PyErr_SetString |
| 144 | # define PyEval_InitThreads dll_PyEval_InitThreads |
| 145 | # define PyEval_RestoreThread dll_PyEval_RestoreThread |
| 146 | # define PyEval_SaveThread dll_PyEval_SaveThread |
| 147 | # ifdef PY_CAN_RECURSE |
| 148 | # define PyGILState_Ensure dll_PyGILState_Ensure |
| 149 | # define PyGILState_Release dll_PyGILState_Release |
| 150 | # endif |
| 151 | # define PyInt_AsLong dll_PyInt_AsLong |
| 152 | # define PyInt_FromLong dll_PyInt_FromLong |
| 153 | # define PyInt_Type (*dll_PyInt_Type) |
| 154 | # define PyList_GetItem dll_PyList_GetItem |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 155 | # define PyList_Append dll_PyList_Append |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 156 | # define PyList_New dll_PyList_New |
| 157 | # define PyList_SetItem dll_PyList_SetItem |
| 158 | # define PyList_Size dll_PyList_Size |
| 159 | # define PyList_Type (*dll_PyList_Type) |
| 160 | # define PyImport_ImportModule dll_PyImport_ImportModule |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 161 | # define PyDict_New dll_PyDict_New |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 162 | # define PyDict_GetItemString dll_PyDict_GetItemString |
| 163 | # define PyModule_GetDict dll_PyModule_GetDict |
| 164 | # define PyRun_SimpleString dll_PyRun_SimpleString |
| 165 | # define PyString_AsString dll_PyString_AsString |
| 166 | # define PyString_FromString dll_PyString_FromString |
| 167 | # define PyString_FromStringAndSize dll_PyString_FromStringAndSize |
| 168 | # define PyString_Size dll_PyString_Size |
| 169 | # define PyString_Type (*dll_PyString_Type) |
| 170 | # define PySys_SetObject dll_PySys_SetObject |
| 171 | # define PySys_SetArgv dll_PySys_SetArgv |
| 172 | # define PyType_Type (*dll_PyType_Type) |
Bram Moolenaar | 30fec7b | 2011-03-26 18:32:05 +0100 | [diff] [blame] | 173 | # define PyType_Ready (*dll_PyType_Ready) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 174 | # define Py_BuildValue dll_Py_BuildValue |
| 175 | # define Py_FindMethod dll_Py_FindMethod |
| 176 | # define Py_InitModule4 dll_Py_InitModule4 |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 177 | # define Py_SetPythonHome dll_Py_SetPythonHome |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 178 | # define Py_Initialize dll_Py_Initialize |
Bram Moolenaar | 0e21a3f | 2005-04-17 20:28:32 +0000 | [diff] [blame] | 179 | # define Py_Finalize dll_Py_Finalize |
| 180 | # define Py_IsInitialized dll_Py_IsInitialized |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 181 | # define _PyObject_New dll__PyObject_New |
| 182 | # define _Py_NoneStruct (*dll__Py_NoneStruct) |
| 183 | # define PyObject_Init dll__PyObject_Init |
| 184 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 |
| 185 | # define PyType_IsSubtype dll_PyType_IsSubtype |
| 186 | # endif |
| 187 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 |
| 188 | # define PyObject_Malloc dll_PyObject_Malloc |
| 189 | # define PyObject_Free dll_PyObject_Free |
| 190 | # endif |
| 191 | |
| 192 | /* |
| 193 | * Pointers for dynamic link |
| 194 | */ |
| 195 | static int(*dll_PyArg_Parse)(PyObject *, char *, ...); |
| 196 | static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 197 | static int(*dll_PyMem_Free)(void *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 198 | static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item); |
| 199 | static int(*dll_PyErr_BadArgument)(void); |
| 200 | static void(*dll_PyErr_Clear)(void); |
| 201 | static PyObject*(*dll_PyErr_NoMemory)(void); |
| 202 | static PyObject*(*dll_PyErr_Occurred)(void); |
| 203 | static void(*dll_PyErr_SetNone)(PyObject *); |
| 204 | static void(*dll_PyErr_SetString)(PyObject *, const char *); |
| 205 | static void(*dll_PyEval_InitThreads)(void); |
| 206 | static void(*dll_PyEval_RestoreThread)(PyThreadState *); |
| 207 | static PyThreadState*(*dll_PyEval_SaveThread)(void); |
| 208 | # ifdef PY_CAN_RECURSE |
| 209 | static PyGILState_STATE (*dll_PyGILState_Ensure)(void); |
| 210 | static void (*dll_PyGILState_Release)(PyGILState_STATE); |
| 211 | #endif |
| 212 | static long(*dll_PyInt_AsLong)(PyObject *); |
| 213 | static PyObject*(*dll_PyInt_FromLong)(long); |
| 214 | static PyTypeObject* dll_PyInt_Type; |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 215 | static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt); |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 216 | static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *); |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 217 | static PyObject*(*dll_PyList_New)(PyInt size); |
| 218 | static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *); |
| 219 | static PyInt(*dll_PyList_Size)(PyObject *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 220 | static PyTypeObject* dll_PyList_Type; |
| 221 | static PyObject*(*dll_PyImport_ImportModule)(const char *); |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 222 | static PyObject*(*dll_PyDict_New)(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 223 | static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *); |
| 224 | static PyObject*(*dll_PyModule_GetDict)(PyObject *); |
| 225 | static int(*dll_PyRun_SimpleString)(char *); |
| 226 | static char*(*dll_PyString_AsString)(PyObject *); |
| 227 | static PyObject*(*dll_PyString_FromString)(const char *); |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 228 | static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt); |
| 229 | static PyInt(*dll_PyString_Size)(PyObject *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 230 | static PyTypeObject* dll_PyString_Type; |
| 231 | static int(*dll_PySys_SetObject)(char *, PyObject *); |
| 232 | static int(*dll_PySys_SetArgv)(int, char **); |
| 233 | static PyTypeObject* dll_PyType_Type; |
Bram Moolenaar | 30fec7b | 2011-03-26 18:32:05 +0100 | [diff] [blame] | 234 | static int (*dll_PyType_Ready)(PyTypeObject *type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | static PyObject*(*dll_Py_BuildValue)(char *, ...); |
| 236 | static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *); |
| 237 | static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int); |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 238 | static void(*dll_Py_SetPythonHome)(char *home); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 239 | static void(*dll_Py_Initialize)(void); |
Bram Moolenaar | 0e21a3f | 2005-04-17 20:28:32 +0000 | [diff] [blame] | 240 | static void(*dll_Py_Finalize)(void); |
| 241 | static int(*dll_Py_IsInitialized)(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 242 | static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *); |
| 243 | static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *); |
| 244 | static PyObject* dll__Py_NoneStruct; |
| 245 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 |
| 246 | static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *); |
| 247 | # endif |
| 248 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 |
| 249 | static void* (*dll_PyObject_Malloc)(size_t); |
| 250 | static void (*dll_PyObject_Free)(void*); |
| 251 | # endif |
| 252 | |
| 253 | static HINSTANCE hinstPython = 0; /* Instance of python.dll */ |
| 254 | |
| 255 | /* Imported exception objects */ |
| 256 | static PyObject *imp_PyExc_AttributeError; |
| 257 | static PyObject *imp_PyExc_IndexError; |
| 258 | static PyObject *imp_PyExc_KeyboardInterrupt; |
| 259 | static PyObject *imp_PyExc_TypeError; |
| 260 | static PyObject *imp_PyExc_ValueError; |
| 261 | |
| 262 | # define PyExc_AttributeError imp_PyExc_AttributeError |
| 263 | # define PyExc_IndexError imp_PyExc_IndexError |
| 264 | # define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt |
| 265 | # define PyExc_TypeError imp_PyExc_TypeError |
| 266 | # define PyExc_ValueError imp_PyExc_ValueError |
| 267 | |
| 268 | /* |
| 269 | * Table of name to function pointer of python. |
| 270 | */ |
| 271 | # define PYTHON_PROC FARPROC |
| 272 | static struct |
| 273 | { |
| 274 | char *name; |
| 275 | PYTHON_PROC *ptr; |
| 276 | } python_funcname_table[] = |
| 277 | { |
| 278 | {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse}, |
| 279 | {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple}, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 280 | {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 281 | {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString}, |
| 282 | {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument}, |
| 283 | {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear}, |
| 284 | {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory}, |
| 285 | {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred}, |
| 286 | {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone}, |
| 287 | {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString}, |
| 288 | {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads}, |
| 289 | {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread}, |
| 290 | {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread}, |
| 291 | # ifdef PY_CAN_RECURSE |
| 292 | {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure}, |
| 293 | {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release}, |
| 294 | # endif |
| 295 | {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong}, |
| 296 | {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong}, |
| 297 | {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type}, |
| 298 | {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem}, |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 299 | {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 300 | {"PyList_New", (PYTHON_PROC*)&dll_PyList_New}, |
| 301 | {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem}, |
| 302 | {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size}, |
| 303 | {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type}, |
| 304 | {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule}, |
| 305 | {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString}, |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 306 | {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 307 | {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict}, |
| 308 | {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString}, |
| 309 | {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString}, |
| 310 | {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString}, |
| 311 | {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize}, |
| 312 | {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size}, |
| 313 | {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type}, |
| 314 | {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject}, |
| 315 | {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv}, |
| 316 | {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type}, |
Bram Moolenaar | 30fec7b | 2011-03-26 18:32:05 +0100 | [diff] [blame] | 317 | {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 318 | {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue}, |
| 319 | {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod}, |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 320 | # if (PY_VERSION_HEX >= 0x02050000) && SIZEOF_SIZE_T != SIZEOF_INT |
| 321 | {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4}, |
| 322 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 323 | {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4}, |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 324 | # endif |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 325 | {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 326 | {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize}, |
Bram Moolenaar | 0e21a3f | 2005-04-17 20:28:32 +0000 | [diff] [blame] | 327 | {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize}, |
| 328 | {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 329 | {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New}, |
| 330 | {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init}, |
| 331 | {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct}, |
| 332 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 |
| 333 | {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype}, |
| 334 | # endif |
| 335 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 |
| 336 | {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc}, |
| 337 | {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free}, |
| 338 | # endif |
| 339 | {"", NULL}, |
| 340 | }; |
| 341 | |
| 342 | /* |
| 343 | * Free python.dll |
| 344 | */ |
| 345 | static void |
| 346 | end_dynamic_python(void) |
| 347 | { |
| 348 | if (hinstPython) |
| 349 | { |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 350 | close_dll(hinstPython); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 351 | hinstPython = 0; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Load library and get all pointers. |
| 357 | * Parameter 'libname' provides name of DLL. |
| 358 | * Return OK or FAIL. |
| 359 | */ |
| 360 | static int |
| 361 | python_runtime_link_init(char *libname, int verbose) |
| 362 | { |
| 363 | int i; |
| 364 | |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 365 | #if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3) |
Bram Moolenaar | b744b2f | 2010-08-13 16:22:57 +0200 | [diff] [blame] | 366 | /* Can't have Python and Python3 loaded at the same time. |
| 367 | * It cause a crash, because RTLD_GLOBAL is needed for |
| 368 | * standard C extension libraries of one or both python versions. */ |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 369 | if (python3_loaded()) |
| 370 | { |
Bram Moolenaar | 9dc93ae | 2011-08-28 16:00:19 +0200 | [diff] [blame] | 371 | if (verbose) |
| 372 | EMSG(_("E836: This Vim cannot execute :python after using :py3")); |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 373 | return FAIL; |
| 374 | } |
| 375 | #endif |
| 376 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 377 | if (hinstPython) |
| 378 | return OK; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 379 | hinstPython = load_dll(libname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 380 | if (!hinstPython) |
| 381 | { |
| 382 | if (verbose) |
| 383 | EMSG2(_(e_loadlib), libname); |
| 384 | return FAIL; |
| 385 | } |
| 386 | |
| 387 | for (i = 0; python_funcname_table[i].ptr; ++i) |
| 388 | { |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 389 | if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 390 | python_funcname_table[i].name)) == NULL) |
| 391 | { |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 392 | close_dll(hinstPython); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 393 | hinstPython = 0; |
| 394 | if (verbose) |
| 395 | EMSG2(_(e_loadfunc), python_funcname_table[i].name); |
| 396 | return FAIL; |
| 397 | } |
| 398 | } |
| 399 | return OK; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * If python is enabled (there is installed python on Windows system) return |
| 404 | * TRUE, else FALSE. |
| 405 | */ |
| 406 | int |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 407 | python_enabled(int verbose) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 408 | { |
| 409 | return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK; |
| 410 | } |
| 411 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 412 | /* |
| 413 | * Load the standard Python exceptions - don't import the symbols from the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 414 | * DLL, as this can cause errors (importing data symbols is not reliable). |
| 415 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 416 | static void |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 417 | get_exceptions(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 418 | { |
| 419 | PyObject *exmod = PyImport_ImportModule("exceptions"); |
| 420 | PyObject *exdict = PyModule_GetDict(exmod); |
| 421 | imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); |
| 422 | imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); |
| 423 | imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt"); |
| 424 | imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError"); |
| 425 | imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError"); |
| 426 | Py_XINCREF(imp_PyExc_AttributeError); |
| 427 | Py_XINCREF(imp_PyExc_IndexError); |
| 428 | Py_XINCREF(imp_PyExc_KeyboardInterrupt); |
| 429 | Py_XINCREF(imp_PyExc_TypeError); |
| 430 | Py_XINCREF(imp_PyExc_ValueError); |
| 431 | Py_XDECREF(exmod); |
| 432 | } |
| 433 | #endif /* DYNAMIC_PYTHON */ |
| 434 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 435 | static PyObject *BufferNew (buf_T *); |
| 436 | static PyObject *WindowNew(win_T *); |
| 437 | static PyObject *LineToString(const char *); |
| 438 | |
| 439 | static PyTypeObject RangeType; |
| 440 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 441 | /* |
| 442 | * Include the code shared with if_python3.c |
| 443 | */ |
| 444 | #include "if_py_both.h" |
| 445 | |
| 446 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 447 | /****************************************************** |
| 448 | * Internal function prototypes. |
| 449 | */ |
| 450 | |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 451 | static PyInt RangeStart; |
| 452 | static PyInt RangeEnd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 453 | |
| 454 | static void PythonIO_Flush(void); |
| 455 | static int PythonIO_Init(void); |
| 456 | static int PythonMod_Init(void); |
| 457 | |
| 458 | /* Utility functions for the vim/python interface |
| 459 | * ---------------------------------------------- |
| 460 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 461 | |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 462 | static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 463 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 464 | |
| 465 | /****************************************************** |
| 466 | * 1. Python interpreter main program. |
| 467 | */ |
| 468 | |
| 469 | static int initialised = 0; |
| 470 | |
| 471 | #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ |
| 472 | typedef PyObject PyThreadState; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 473 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 474 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 475 | #ifdef PY_CAN_RECURSE |
| 476 | static PyGILState_STATE pygilstate = PyGILState_UNLOCKED; |
| 477 | #else |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 478 | static PyThreadState *saved_python_thread = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 479 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * Suspend a thread of the Python interpreter, other threads are allowed to |
| 483 | * run. |
| 484 | */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 485 | static void |
| 486 | Python_SaveThread(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 487 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 488 | #ifdef PY_CAN_RECURSE |
| 489 | PyGILState_Release(pygilstate); |
| 490 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 491 | saved_python_thread = PyEval_SaveThread(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 492 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Restore a thread of the Python interpreter, waits for other threads to |
| 497 | * block. |
| 498 | */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 499 | static void |
| 500 | Python_RestoreThread(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 501 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 502 | #ifdef PY_CAN_RECURSE |
| 503 | pygilstate = PyGILState_Ensure(); |
| 504 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 505 | PyEval_RestoreThread(saved_python_thread); |
| 506 | saved_python_thread = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 507 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 508 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 509 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 510 | void |
| 511 | python_end() |
| 512 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 513 | static int recurse = 0; |
| 514 | |
| 515 | /* If a crash occurs while doing this, don't try again. */ |
| 516 | if (recurse != 0) |
| 517 | return; |
| 518 | |
| 519 | ++recurse; |
| 520 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 521 | #ifdef DYNAMIC_PYTHON |
Bram Moolenaar | 0e21a3f | 2005-04-17 20:28:32 +0000 | [diff] [blame] | 522 | if (hinstPython && Py_IsInitialized()) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 523 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 524 | Python_RestoreThread(); /* enter python */ |
| 525 | Py_Finalize(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 526 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 527 | end_dynamic_python(); |
Bram Moolenaar | 0e21a3f | 2005-04-17 20:28:32 +0000 | [diff] [blame] | 528 | #else |
| 529 | if (Py_IsInitialized()) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 530 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 531 | Python_RestoreThread(); /* enter python */ |
| 532 | Py_Finalize(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 533 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 534 | #endif |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 535 | |
| 536 | --recurse; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 539 | #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO) |
| 540 | int |
| 541 | python_loaded() |
| 542 | { |
| 543 | return (hinstPython != 0); |
| 544 | } |
| 545 | #endif |
| 546 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 547 | static int |
| 548 | Python_Init(void) |
| 549 | { |
| 550 | if (!initialised) |
| 551 | { |
| 552 | #ifdef DYNAMIC_PYTHON |
| 553 | if (!python_enabled(TRUE)) |
| 554 | { |
| 555 | EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); |
| 556 | goto fail; |
| 557 | } |
| 558 | #endif |
| 559 | |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 560 | #ifdef PYTHON_HOME |
| 561 | Py_SetPythonHome(PYTHON_HOME); |
| 562 | #endif |
| 563 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 564 | init_structs(); |
| 565 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 566 | #if !defined(MACOS) || defined(MACOS_X_UNIX) |
| 567 | Py_Initialize(); |
| 568 | #else |
| 569 | PyMac_Initialize(); |
| 570 | #endif |
| 571 | /* initialise threads */ |
| 572 | PyEval_InitThreads(); |
| 573 | |
| 574 | #ifdef DYNAMIC_PYTHON |
| 575 | get_exceptions(); |
| 576 | #endif |
| 577 | |
| 578 | if (PythonIO_Init()) |
| 579 | goto fail; |
| 580 | |
| 581 | if (PythonMod_Init()) |
| 582 | goto fail; |
| 583 | |
Bram Moolenaar | 9774ecc | 2008-11-20 10:04:53 +0000 | [diff] [blame] | 584 | /* Remove the element from sys.path that was added because of our |
| 585 | * argv[0] value in PythonMod_Init(). Previously we used an empty |
| 586 | * string, but dependinding on the OS we then get an empty entry or |
| 587 | * the current directory in sys.path. */ |
| 588 | PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)"); |
| 589 | |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 590 | /* the first python thread is vim's, release the lock */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 591 | Python_SaveThread(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 592 | |
| 593 | initialised = 1; |
| 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | |
| 598 | fail: |
| 599 | /* We call PythonIO_Flush() here to print any Python errors. |
| 600 | * This is OK, as it is possible to call this function even |
| 601 | * if PythonIO_Init() has not completed successfully (it will |
| 602 | * not do anything in this case). |
| 603 | */ |
| 604 | PythonIO_Flush(); |
| 605 | return -1; |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * External interface |
| 610 | */ |
| 611 | static void |
| 612 | DoPythonCommand(exarg_T *eap, const char *cmd) |
| 613 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 614 | #ifndef PY_CAN_RECURSE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 615 | static int recursive = 0; |
| 616 | #endif |
| 617 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 618 | GrafPtr oldPort; |
| 619 | #endif |
| 620 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 621 | char *saved_locale; |
| 622 | #endif |
| 623 | |
| 624 | #ifndef PY_CAN_RECURSE |
| 625 | if (recursive) |
| 626 | { |
| 627 | EMSG(_("E659: Cannot invoke Python recursively")); |
| 628 | return; |
| 629 | } |
| 630 | ++recursive; |
| 631 | #endif |
| 632 | |
| 633 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 634 | GetPort(&oldPort); |
| 635 | /* Check if the Python library is available */ |
| 636 | if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) |
| 637 | goto theend; |
| 638 | #endif |
| 639 | if (Python_Init()) |
| 640 | goto theend; |
| 641 | |
| 642 | RangeStart = eap->line1; |
| 643 | RangeEnd = eap->line2; |
| 644 | Python_Release_Vim(); /* leave vim */ |
| 645 | |
| 646 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 647 | /* Python only works properly when the LC_NUMERIC locale is "C". */ |
| 648 | saved_locale = setlocale(LC_NUMERIC, NULL); |
| 649 | if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) |
| 650 | saved_locale = NULL; |
| 651 | else |
| 652 | { |
| 653 | /* Need to make a copy, value may change when setting new locale. */ |
| 654 | saved_locale = (char *)vim_strsave((char_u *)saved_locale); |
| 655 | (void)setlocale(LC_NUMERIC, "C"); |
| 656 | } |
| 657 | #endif |
| 658 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 659 | Python_RestoreThread(); /* enter python */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 660 | |
| 661 | PyRun_SimpleString((char *)(cmd)); |
| 662 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 663 | Python_SaveThread(); /* leave python */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 664 | |
| 665 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 666 | if (saved_locale != NULL) |
| 667 | { |
| 668 | (void)setlocale(LC_NUMERIC, saved_locale); |
| 669 | vim_free(saved_locale); |
| 670 | } |
| 671 | #endif |
| 672 | |
| 673 | Python_Lock_Vim(); /* enter vim */ |
| 674 | PythonIO_Flush(); |
| 675 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 676 | SetPort(oldPort); |
| 677 | #endif |
| 678 | |
| 679 | theend: |
| 680 | #ifndef PY_CAN_RECURSE |
| 681 | --recursive; |
| 682 | #endif |
| 683 | return; /* keeps lint happy */ |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * ":python" |
| 688 | */ |
| 689 | void |
| 690 | ex_python(exarg_T *eap) |
| 691 | { |
| 692 | char_u *script; |
| 693 | |
| 694 | script = script_get(eap, eap->arg); |
| 695 | if (!eap->skip) |
| 696 | { |
| 697 | if (script == NULL) |
| 698 | DoPythonCommand(eap, (char *)eap->arg); |
| 699 | else |
| 700 | DoPythonCommand(eap, (char *)script); |
| 701 | } |
| 702 | vim_free(script); |
| 703 | } |
| 704 | |
| 705 | #define BUFFER_SIZE 1024 |
| 706 | |
| 707 | /* |
| 708 | * ":pyfile" |
| 709 | */ |
| 710 | void |
| 711 | ex_pyfile(exarg_T *eap) |
| 712 | { |
| 713 | static char buffer[BUFFER_SIZE]; |
| 714 | const char *file = (char *)eap->arg; |
| 715 | char *p; |
| 716 | |
| 717 | /* Have to do it like this. PyRun_SimpleFile requires you to pass a |
| 718 | * stdio file pointer, but Vim and the Python DLL are compiled with |
| 719 | * different options under Windows, meaning that stdio pointers aren't |
| 720 | * compatible between the two. Yuk. |
| 721 | * |
| 722 | * Put the string "execfile('file')" into buffer. But, we need to |
| 723 | * escape any backslashes or single quotes in the file name, so that |
| 724 | * Python won't mangle the file name. |
| 725 | */ |
| 726 | strcpy(buffer, "execfile('"); |
| 727 | p = buffer + 10; /* size of "execfile('" */ |
| 728 | |
| 729 | while (*file && p < buffer + (BUFFER_SIZE - 3)) |
| 730 | { |
| 731 | if (*file == '\\' || *file == '\'') |
| 732 | *p++ = '\\'; |
| 733 | *p++ = *file++; |
| 734 | } |
| 735 | |
| 736 | /* If we didn't finish the file name, we hit a buffer overflow */ |
| 737 | if (*file != '\0') |
| 738 | return; |
| 739 | |
| 740 | /* Put in the terminating "')" and a null */ |
| 741 | *p++ = '\''; |
| 742 | *p++ = ')'; |
| 743 | *p++ = '\0'; |
| 744 | |
| 745 | /* Execute the file */ |
| 746 | DoPythonCommand(eap, buffer); |
| 747 | } |
| 748 | |
| 749 | /****************************************************** |
| 750 | * 2. Python output stream: writes output via [e]msg(). |
| 751 | */ |
| 752 | |
| 753 | /* Implementation functions |
| 754 | */ |
| 755 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 756 | static PyObject * |
| 757 | OutputGetattr(PyObject *self, char *name) |
| 758 | { |
| 759 | if (strcmp(name, "softspace") == 0) |
| 760 | return PyInt_FromLong(((OutputObject *)(self))->softspace); |
| 761 | |
| 762 | return Py_FindMethod(OutputMethods, self, name); |
| 763 | } |
| 764 | |
| 765 | static int |
| 766 | OutputSetattr(PyObject *self, char *name, PyObject *val) |
| 767 | { |
| 768 | if (val == NULL) { |
| 769 | PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes")); |
| 770 | return -1; |
| 771 | } |
| 772 | |
| 773 | if (strcmp(name, "softspace") == 0) |
| 774 | { |
| 775 | if (!PyInt_Check(val)) { |
| 776 | PyErr_SetString(PyExc_TypeError, _("softspace must be an integer")); |
| 777 | return -1; |
| 778 | } |
| 779 | |
| 780 | ((OutputObject *)(self))->softspace = PyInt_AsLong(val); |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); |
| 785 | return -1; |
| 786 | } |
| 787 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 788 | /***************/ |
| 789 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 790 | static int |
| 791 | PythonIO_Init(void) |
| 792 | { |
| 793 | /* Fixups... */ |
Bram Moolenaar | 21377c8 | 2011-03-26 13:56:48 +0100 | [diff] [blame] | 794 | PyType_Ready(&OutputType); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 795 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 796 | return PythonIO_Init_io(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /****************************************************** |
| 800 | * 3. Implementation of the Vim module for Python |
| 801 | */ |
| 802 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 803 | /* Window type - Implementation functions |
| 804 | * -------------------------------------- |
| 805 | */ |
| 806 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 807 | #define WindowType_Check(obj) ((obj)->ob_type == &WindowType) |
| 808 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 809 | static void WindowDestructor(PyObject *); |
| 810 | static PyObject *WindowGetattr(PyObject *, char *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 811 | |
| 812 | /* Buffer type - Implementation functions |
| 813 | * -------------------------------------- |
| 814 | */ |
| 815 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 816 | #define BufferType_Check(obj) ((obj)->ob_type == &BufferType) |
| 817 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 818 | static void BufferDestructor(PyObject *); |
| 819 | static PyObject *BufferGetattr(PyObject *, char *); |
| 820 | static PyObject *BufferRepr(PyObject *); |
| 821 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 822 | static PyInt BufferLength(PyObject *); |
| 823 | static PyObject *BufferItem(PyObject *, PyInt); |
| 824 | static PyObject *BufferSlice(PyObject *, PyInt, PyInt); |
| 825 | static PyInt BufferAssItem(PyObject *, PyInt, PyObject *); |
| 826 | static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 827 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 828 | /* Line range type - Implementation functions |
| 829 | * -------------------------------------- |
| 830 | */ |
| 831 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 832 | #define RangeType_Check(obj) ((obj)->ob_type == &RangeType) |
| 833 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 834 | static PyInt RangeAssItem(PyObject *, PyInt, PyObject *); |
| 835 | static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 836 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 837 | /* Current objects type - Implementation functions |
| 838 | * ----------------------------------------------- |
| 839 | */ |
| 840 | |
| 841 | static PyObject *CurrentGetattr(PyObject *, char *); |
| 842 | static int CurrentSetattr(PyObject *, char *, PyObject *); |
| 843 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 844 | static PySequenceMethods BufferAsSeq = { |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 845 | (PyInquiry) BufferLength, /* sq_length, len(x) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 846 | (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */ |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 847 | (PyIntArgFunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */ |
| 848 | (PyIntArgFunc) BufferItem, /* sq_item, x[i] */ |
| 849 | (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */ |
| 850 | (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */ |
| 851 | (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 852 | }; |
| 853 | |
| 854 | static PyTypeObject BufferType = { |
| 855 | PyObject_HEAD_INIT(0) |
| 856 | 0, |
| 857 | "buffer", |
| 858 | sizeof(BufferObject), |
| 859 | 0, |
| 860 | |
| 861 | (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */ |
| 862 | (printfunc) 0, /* tp_print, print x */ |
| 863 | (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */ |
| 864 | (setattrfunc) 0, /* tp_setattr, x.attr=v */ |
| 865 | (cmpfunc) 0, /* tp_compare, x>y */ |
| 866 | (reprfunc) BufferRepr, /* tp_repr, `x`, print x */ |
| 867 | |
| 868 | 0, /* as number */ |
| 869 | &BufferAsSeq, /* as sequence */ |
| 870 | 0, /* as mapping */ |
| 871 | |
| 872 | (hashfunc) 0, /* tp_hash, dict(x) */ |
| 873 | (ternaryfunc) 0, /* tp_call, x() */ |
| 874 | (reprfunc) 0, /* tp_str, str(x) */ |
| 875 | }; |
| 876 | |
| 877 | /* Buffer object - Implementation |
| 878 | */ |
| 879 | |
| 880 | static PyObject * |
| 881 | BufferNew(buf_T *buf) |
| 882 | { |
| 883 | /* We need to handle deletion of buffers underneath us. |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 884 | * If we add a "b_python_ref" field to the buf_T structure, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 885 | * then we can get at it in buf_freeall() in vim. We then |
| 886 | * need to create only ONE Python object per buffer - if |
| 887 | * we try to create a second, just INCREF the existing one |
| 888 | * and return it. The (single) Python object referring to |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 889 | * the buffer is stored in "b_python_ref". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 890 | * Question: what to do on a buf_freeall(). We'll probably |
| 891 | * have to either delete the Python object (DECREF it to |
| 892 | * zero - a bad idea, as it leaves dangling refs!) or |
| 893 | * set the buf_T * value to an invalid value (-1?), which |
| 894 | * means we need checks in all access functions... Bah. |
| 895 | */ |
| 896 | |
| 897 | BufferObject *self; |
| 898 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 899 | if (buf->b_python_ref != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 900 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 901 | self = buf->b_python_ref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 902 | Py_INCREF(self); |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | self = PyObject_NEW(BufferObject, &BufferType); |
| 907 | if (self == NULL) |
| 908 | return NULL; |
| 909 | self->buf = buf; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 910 | buf->b_python_ref = self; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | return (PyObject *)(self); |
| 914 | } |
| 915 | |
| 916 | static void |
| 917 | BufferDestructor(PyObject *self) |
| 918 | { |
| 919 | BufferObject *this = (BufferObject *)(self); |
| 920 | |
| 921 | if (this->buf && this->buf != INVALID_BUFFER_VALUE) |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 922 | this->buf->b_python_ref = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 923 | |
Bram Moolenaar | 658ada6 | 2006-10-03 13:02:36 +0000 | [diff] [blame] | 924 | Py_DECREF(self); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | static PyObject * |
| 928 | BufferGetattr(PyObject *self, char *name) |
| 929 | { |
| 930 | BufferObject *this = (BufferObject *)(self); |
| 931 | |
| 932 | if (CheckBuffer(this)) |
| 933 | return NULL; |
| 934 | |
| 935 | if (strcmp(name, "name") == 0) |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 936 | return Py_BuildValue("s", this->buf->b_ffname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 937 | else if (strcmp(name, "number") == 0) |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 938 | return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 939 | else if (strcmp(name,"__members__") == 0) |
| 940 | return Py_BuildValue("[ss]", "name", "number"); |
| 941 | else |
| 942 | return Py_FindMethod(BufferMethods, self, name); |
| 943 | } |
| 944 | |
| 945 | static PyObject * |
| 946 | BufferRepr(PyObject *self) |
| 947 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 948 | static char repr[100]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 949 | BufferObject *this = (BufferObject *)(self); |
| 950 | |
| 951 | if (this->buf == INVALID_BUFFER_VALUE) |
| 952 | { |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 953 | vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 954 | return PyString_FromString(repr); |
| 955 | } |
| 956 | else |
| 957 | { |
| 958 | char *name = (char *)this->buf->b_fname; |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 959 | PyInt len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 960 | |
| 961 | if (name == NULL) |
| 962 | name = ""; |
| 963 | len = strlen(name); |
| 964 | |
| 965 | if (len > 35) |
| 966 | name = name + (35 - len); |
| 967 | |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 968 | vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 969 | |
| 970 | return PyString_FromString(repr); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | /******************/ |
| 975 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 976 | static PyInt |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 977 | BufferLength(PyObject *self) |
| 978 | { |
| 979 | /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
| 980 | if (CheckBuffer((BufferObject *)(self))) |
| 981 | return -1; /* ??? */ |
| 982 | |
| 983 | return (((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 984 | } |
| 985 | |
| 986 | static PyObject * |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 987 | BufferItem(PyObject *self, PyInt n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 988 | { |
| 989 | return RBItem((BufferObject *)(self), n, 1, |
| 990 | (int)((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 991 | } |
| 992 | |
| 993 | static PyObject * |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 994 | BufferSlice(PyObject *self, PyInt lo, PyInt hi) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 995 | { |
| 996 | return RBSlice((BufferObject *)(self), lo, hi, 1, |
| 997 | (int)((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 998 | } |
| 999 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1000 | static PyInt |
| 1001 | BufferAssItem(PyObject *self, PyInt n, PyObject *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1002 | { |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1003 | return RBAsItem((BufferObject *)(self), n, val, 1, |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1004 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1005 | NULL); |
| 1006 | } |
| 1007 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1008 | static PyInt |
| 1009 | BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1010 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1011 | return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1012 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1013 | NULL); |
| 1014 | } |
| 1015 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1016 | static PySequenceMethods RangeAsSeq = { |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1017 | (PyInquiry) RangeLength, /* sq_length, len(x) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1018 | (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */ |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1019 | (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */ |
| 1020 | (PyIntArgFunc) RangeItem, /* sq_item, x[i] */ |
| 1021 | (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */ |
| 1022 | (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */ |
| 1023 | (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1024 | }; |
| 1025 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1026 | /* Line range object - Implementation |
| 1027 | */ |
| 1028 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1029 | static void |
| 1030 | RangeDestructor(PyObject *self) |
| 1031 | { |
| 1032 | Py_DECREF(((RangeObject *)(self))->buf); |
Bram Moolenaar | 658ada6 | 2006-10-03 13:02:36 +0000 | [diff] [blame] | 1033 | Py_DECREF(self); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | static PyObject * |
| 1037 | RangeGetattr(PyObject *self, char *name) |
| 1038 | { |
| 1039 | if (strcmp(name, "start") == 0) |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1040 | return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1041 | else if (strcmp(name, "end") == 0) |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1042 | return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1043 | else |
| 1044 | return Py_FindMethod(RangeMethods, self, name); |
| 1045 | } |
| 1046 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1047 | /****************/ |
| 1048 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1049 | static PyInt |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1050 | RangeAssItem(PyObject *self, PyInt n, PyObject *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1051 | { |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1052 | return RBAsItem(((RangeObject *)(self))->buf, n, val, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1053 | ((RangeObject *)(self))->start, |
| 1054 | ((RangeObject *)(self))->end, |
| 1055 | &((RangeObject *)(self))->end); |
| 1056 | } |
| 1057 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1058 | static PyInt |
| 1059 | RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1060 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1061 | return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1062 | ((RangeObject *)(self))->start, |
| 1063 | ((RangeObject *)(self))->end, |
| 1064 | &((RangeObject *)(self))->end); |
| 1065 | } |
| 1066 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1067 | /* Buffer list object - Definitions |
| 1068 | */ |
| 1069 | |
| 1070 | typedef struct |
| 1071 | { |
| 1072 | PyObject_HEAD |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1073 | } BufListObject; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1074 | |
| 1075 | static PySequenceMethods BufListAsSeq = { |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1076 | (PyInquiry) BufListLength, /* sq_length, len(x) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1077 | (binaryfunc) 0, /* sq_concat, x+y */ |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1078 | (PyIntArgFunc) 0, /* sq_repeat, x*n */ |
| 1079 | (PyIntArgFunc) BufListItem, /* sq_item, x[i] */ |
| 1080 | (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */ |
| 1081 | (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */ |
| 1082 | (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1083 | }; |
| 1084 | |
| 1085 | static PyTypeObject BufListType = { |
| 1086 | PyObject_HEAD_INIT(0) |
| 1087 | 0, |
| 1088 | "buffer list", |
| 1089 | sizeof(BufListObject), |
| 1090 | 0, |
| 1091 | |
| 1092 | (destructor) 0, /* tp_dealloc, refcount==0 */ |
| 1093 | (printfunc) 0, /* tp_print, print x */ |
| 1094 | (getattrfunc) 0, /* tp_getattr, x.attr */ |
| 1095 | (setattrfunc) 0, /* tp_setattr, x.attr=v */ |
| 1096 | (cmpfunc) 0, /* tp_compare, x>y */ |
| 1097 | (reprfunc) 0, /* tp_repr, `x`, print x */ |
| 1098 | |
| 1099 | 0, /* as number */ |
| 1100 | &BufListAsSeq, /* as sequence */ |
| 1101 | 0, /* as mapping */ |
| 1102 | |
| 1103 | (hashfunc) 0, /* tp_hash, dict(x) */ |
| 1104 | (ternaryfunc) 0, /* tp_call, x() */ |
| 1105 | (reprfunc) 0, /* tp_str, str(x) */ |
| 1106 | }; |
| 1107 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1108 | /* Window object - Definitions |
| 1109 | */ |
| 1110 | |
| 1111 | static struct PyMethodDef WindowMethods[] = { |
| 1112 | /* name, function, calling, documentation */ |
| 1113 | { NULL, NULL, 0, NULL } |
| 1114 | }; |
| 1115 | |
| 1116 | static PyTypeObject WindowType = { |
| 1117 | PyObject_HEAD_INIT(0) |
| 1118 | 0, |
| 1119 | "window", |
| 1120 | sizeof(WindowObject), |
| 1121 | 0, |
| 1122 | |
| 1123 | (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */ |
| 1124 | (printfunc) 0, /* tp_print, print x */ |
| 1125 | (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */ |
| 1126 | (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */ |
| 1127 | (cmpfunc) 0, /* tp_compare, x>y */ |
| 1128 | (reprfunc) WindowRepr, /* tp_repr, `x`, print x */ |
| 1129 | |
| 1130 | 0, /* as number */ |
| 1131 | 0, /* as sequence */ |
| 1132 | 0, /* as mapping */ |
| 1133 | |
| 1134 | (hashfunc) 0, /* tp_hash, dict(x) */ |
| 1135 | (ternaryfunc) 0, /* tp_call, x() */ |
| 1136 | (reprfunc) 0, /* tp_str, str(x) */ |
| 1137 | }; |
| 1138 | |
| 1139 | /* Window object - Implementation |
| 1140 | */ |
| 1141 | |
| 1142 | static PyObject * |
| 1143 | WindowNew(win_T *win) |
| 1144 | { |
| 1145 | /* We need to handle deletion of windows underneath us. |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1146 | * If we add a "w_python_ref" field to the win_T structure, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1147 | * then we can get at it in win_free() in vim. We then |
| 1148 | * need to create only ONE Python object per window - if |
| 1149 | * we try to create a second, just INCREF the existing one |
| 1150 | * and return it. The (single) Python object referring to |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1151 | * the window is stored in "w_python_ref". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1152 | * On a win_free() we set the Python object's win_T* field |
| 1153 | * to an invalid value. We trap all uses of a window |
| 1154 | * object, and reject them if the win_T* field is invalid. |
| 1155 | */ |
| 1156 | |
| 1157 | WindowObject *self; |
| 1158 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1159 | if (win->w_python_ref) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1160 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1161 | self = win->w_python_ref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1162 | Py_INCREF(self); |
| 1163 | } |
| 1164 | else |
| 1165 | { |
| 1166 | self = PyObject_NEW(WindowObject, &WindowType); |
| 1167 | if (self == NULL) |
| 1168 | return NULL; |
| 1169 | self->win = win; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1170 | win->w_python_ref = self; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | return (PyObject *)(self); |
| 1174 | } |
| 1175 | |
| 1176 | static void |
| 1177 | WindowDestructor(PyObject *self) |
| 1178 | { |
| 1179 | WindowObject *this = (WindowObject *)(self); |
| 1180 | |
| 1181 | if (this->win && this->win != INVALID_WINDOW_VALUE) |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1182 | this->win->w_python_ref = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1183 | |
Bram Moolenaar | 658ada6 | 2006-10-03 13:02:36 +0000 | [diff] [blame] | 1184 | Py_DECREF(self); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1187 | static PyObject * |
| 1188 | WindowGetattr(PyObject *self, char *name) |
| 1189 | { |
| 1190 | WindowObject *this = (WindowObject *)(self); |
| 1191 | |
| 1192 | if (CheckWindow(this)) |
| 1193 | return NULL; |
| 1194 | |
| 1195 | if (strcmp(name, "buffer") == 0) |
| 1196 | return (PyObject *)BufferNew(this->win->w_buffer); |
| 1197 | else if (strcmp(name, "cursor") == 0) |
| 1198 | { |
| 1199 | pos_T *pos = &this->win->w_cursor; |
| 1200 | |
| 1201 | return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); |
| 1202 | } |
| 1203 | else if (strcmp(name, "height") == 0) |
| 1204 | return Py_BuildValue("l", (long)(this->win->w_height)); |
| 1205 | #ifdef FEAT_VERTSPLIT |
| 1206 | else if (strcmp(name, "width") == 0) |
| 1207 | return Py_BuildValue("l", (long)(W_WIDTH(this->win))); |
| 1208 | #endif |
| 1209 | else if (strcmp(name,"__members__") == 0) |
| 1210 | return Py_BuildValue("[sss]", "buffer", "cursor", "height"); |
| 1211 | else |
| 1212 | return Py_FindMethod(WindowMethods, self, name); |
| 1213 | } |
| 1214 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1215 | /* Window list object - Definitions |
| 1216 | */ |
| 1217 | |
| 1218 | typedef struct |
| 1219 | { |
| 1220 | PyObject_HEAD |
| 1221 | } |
| 1222 | WinListObject; |
| 1223 | |
| 1224 | static PySequenceMethods WinListAsSeq = { |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1225 | (PyInquiry) WinListLength, /* sq_length, len(x) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1226 | (binaryfunc) 0, /* sq_concat, x+y */ |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1227 | (PyIntArgFunc) 0, /* sq_repeat, x*n */ |
| 1228 | (PyIntArgFunc) WinListItem, /* sq_item, x[i] */ |
| 1229 | (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */ |
| 1230 | (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */ |
| 1231 | (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1232 | }; |
| 1233 | |
| 1234 | static PyTypeObject WinListType = { |
| 1235 | PyObject_HEAD_INIT(0) |
| 1236 | 0, |
| 1237 | "window list", |
| 1238 | sizeof(WinListObject), |
| 1239 | 0, |
| 1240 | |
| 1241 | (destructor) 0, /* tp_dealloc, refcount==0 */ |
| 1242 | (printfunc) 0, /* tp_print, print x */ |
| 1243 | (getattrfunc) 0, /* tp_getattr, x.attr */ |
| 1244 | (setattrfunc) 0, /* tp_setattr, x.attr=v */ |
| 1245 | (cmpfunc) 0, /* tp_compare, x>y */ |
| 1246 | (reprfunc) 0, /* tp_repr, `x`, print x */ |
| 1247 | |
| 1248 | 0, /* as number */ |
| 1249 | &WinListAsSeq, /* as sequence */ |
| 1250 | 0, /* as mapping */ |
| 1251 | |
| 1252 | (hashfunc) 0, /* tp_hash, dict(x) */ |
| 1253 | (ternaryfunc) 0, /* tp_call, x() */ |
| 1254 | (reprfunc) 0, /* tp_str, str(x) */ |
| 1255 | }; |
| 1256 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1257 | /* Current items object - Definitions |
| 1258 | */ |
| 1259 | |
| 1260 | typedef struct |
| 1261 | { |
| 1262 | PyObject_HEAD |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1263 | } CurrentObject; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1264 | |
| 1265 | static PyTypeObject CurrentType = { |
| 1266 | PyObject_HEAD_INIT(0) |
| 1267 | 0, |
| 1268 | "current data", |
| 1269 | sizeof(CurrentObject), |
| 1270 | 0, |
| 1271 | |
| 1272 | (destructor) 0, /* tp_dealloc, refcount==0 */ |
| 1273 | (printfunc) 0, /* tp_print, print x */ |
| 1274 | (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */ |
| 1275 | (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */ |
| 1276 | (cmpfunc) 0, /* tp_compare, x>y */ |
| 1277 | (reprfunc) 0, /* tp_repr, `x`, print x */ |
| 1278 | |
| 1279 | 0, /* as number */ |
| 1280 | 0, /* as sequence */ |
| 1281 | 0, /* as mapping */ |
| 1282 | |
| 1283 | (hashfunc) 0, /* tp_hash, dict(x) */ |
| 1284 | (ternaryfunc) 0, /* tp_call, x() */ |
| 1285 | (reprfunc) 0, /* tp_str, str(x) */ |
| 1286 | }; |
| 1287 | |
| 1288 | /* Current items object - Implementation |
| 1289 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1290 | static PyObject * |
Bram Moolenaar | 4bdbbf7 | 2009-05-21 21:27:43 +0000 | [diff] [blame] | 1291 | CurrentGetattr(PyObject *self UNUSED, char *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1292 | { |
| 1293 | if (strcmp(name, "buffer") == 0) |
| 1294 | return (PyObject *)BufferNew(curbuf); |
| 1295 | else if (strcmp(name, "window") == 0) |
| 1296 | return (PyObject *)WindowNew(curwin); |
| 1297 | else if (strcmp(name, "line") == 0) |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1298 | return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1299 | else if (strcmp(name, "range") == 0) |
| 1300 | return RangeNew(curbuf, RangeStart, RangeEnd); |
| 1301 | else if (strcmp(name,"__members__") == 0) |
| 1302 | return Py_BuildValue("[ssss]", "buffer", "window", "line", "range"); |
| 1303 | else |
| 1304 | { |
| 1305 | PyErr_SetString(PyExc_AttributeError, name); |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | } |
| 1309 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1310 | static int |
Bram Moolenaar | 4bdbbf7 | 2009-05-21 21:27:43 +0000 | [diff] [blame] | 1311 | CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1312 | { |
| 1313 | if (strcmp(name, "line") == 0) |
| 1314 | { |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1315 | if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1316 | return -1; |
| 1317 | |
| 1318 | return 0; |
| 1319 | } |
| 1320 | else |
| 1321 | { |
| 1322 | PyErr_SetString(PyExc_AttributeError, name); |
| 1323 | return -1; |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | /* External interface |
| 1328 | */ |
| 1329 | |
| 1330 | void |
| 1331 | python_buffer_free(buf_T *buf) |
| 1332 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1333 | if (buf->b_python_ref != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1334 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1335 | BufferObject *bp = buf->b_python_ref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1336 | bp->buf = INVALID_BUFFER_VALUE; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1337 | buf->b_python_ref = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | #if defined(FEAT_WINDOWS) || defined(PROTO) |
| 1342 | void |
| 1343 | python_window_free(win_T *win) |
| 1344 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1345 | if (win->w_python_ref != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1346 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1347 | WindowObject *wp = win->w_python_ref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1348 | wp->win = INVALID_WINDOW_VALUE; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1349 | win->w_python_ref = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | #endif |
| 1353 | |
| 1354 | static BufListObject TheBufferList = |
| 1355 | { |
| 1356 | PyObject_HEAD_INIT(&BufListType) |
| 1357 | }; |
| 1358 | |
| 1359 | static WinListObject TheWindowList = |
| 1360 | { |
| 1361 | PyObject_HEAD_INIT(&WinListType) |
| 1362 | }; |
| 1363 | |
| 1364 | static CurrentObject TheCurrent = |
| 1365 | { |
| 1366 | PyObject_HEAD_INIT(&CurrentType) |
| 1367 | }; |
| 1368 | |
| 1369 | static int |
| 1370 | PythonMod_Init(void) |
| 1371 | { |
| 1372 | PyObject *mod; |
| 1373 | PyObject *dict; |
Bram Moolenaar | 9774ecc | 2008-11-20 10:04:53 +0000 | [diff] [blame] | 1374 | /* The special value is removed from sys.path in Python_Init(). */ |
| 1375 | static char *(argv[2]) = {"/must>not&exist/foo", NULL}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1376 | |
| 1377 | /* Fixups... */ |
Bram Moolenaar | 21377c8 | 2011-03-26 13:56:48 +0100 | [diff] [blame] | 1378 | PyType_Ready(&BufferType); |
| 1379 | PyType_Ready(&RangeType); |
| 1380 | PyType_Ready(&WindowType); |
| 1381 | PyType_Ready(&BufListType); |
| 1382 | PyType_Ready(&WinListType); |
| 1383 | PyType_Ready(&CurrentType); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1384 | |
| 1385 | /* Set sys.argv[] to avoid a crash in warn(). */ |
| 1386 | PySys_SetArgv(1, argv); |
| 1387 | |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1388 | mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1389 | dict = PyModule_GetDict(mod); |
| 1390 | |
| 1391 | VimError = Py_BuildValue("s", "vim.error"); |
| 1392 | |
| 1393 | PyDict_SetItemString(dict, "error", VimError); |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 1394 | PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList); |
| 1395 | PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent); |
| 1396 | PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1397 | |
| 1398 | if (PyErr_Occurred()) |
| 1399 | return -1; |
| 1400 | |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
| 1404 | /************************************************************************* |
| 1405 | * 4. Utility functions for handling the interface between Vim and Python. |
| 1406 | */ |
| 1407 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1408 | /* Convert a Vim line into a Python string. |
| 1409 | * All internal newlines are replaced by null characters. |
| 1410 | * |
| 1411 | * On errors, the Python exception data is set, and NULL is returned. |
| 1412 | */ |
| 1413 | static PyObject * |
| 1414 | LineToString(const char *str) |
| 1415 | { |
| 1416 | PyObject *result; |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1417 | PyInt len = strlen(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1418 | char *p; |
| 1419 | |
| 1420 | /* Allocate an Python string object, with uninitialised contents. We |
| 1421 | * must do it this way, so that we can modify the string in place |
| 1422 | * later. See the Python source, Objects/stringobject.c for details. |
| 1423 | */ |
| 1424 | result = PyString_FromStringAndSize(NULL, len); |
| 1425 | if (result == NULL) |
| 1426 | return NULL; |
| 1427 | |
| 1428 | p = PyString_AsString(result); |
| 1429 | |
| 1430 | while (*str) |
| 1431 | { |
| 1432 | if (*str == '\n') |
| 1433 | *p = '\0'; |
| 1434 | else |
| 1435 | *p = *str; |
| 1436 | |
| 1437 | ++p; |
| 1438 | ++str; |
| 1439 | } |
| 1440 | |
| 1441 | return result; |
| 1442 | } |
| 1443 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1444 | |
| 1445 | /* Don't generate a prototype for the next function, it generates an error on |
| 1446 | * newer Python versions. */ |
| 1447 | #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO) |
| 1448 | |
| 1449 | char * |
| 1450 | Py_GetProgramName(void) |
| 1451 | { |
| 1452 | return "vim"; |
| 1453 | } |
| 1454 | #endif /* Python 1.4 */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1455 | |
| 1456 | static void |
| 1457 | init_structs(void) |
| 1458 | { |
| 1459 | vim_memset(&OutputType, 0, sizeof(OutputType)); |
| 1460 | OutputType.tp_name = "message"; |
| 1461 | OutputType.tp_basicsize = sizeof(OutputObject); |
| 1462 | OutputType.tp_getattr = OutputGetattr; |
| 1463 | OutputType.tp_setattr = OutputSetattr; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1464 | |
| 1465 | vim_memset(&RangeType, 0, sizeof(RangeType)); |
| 1466 | RangeType.tp_name = "range"; |
| 1467 | RangeType.tp_basicsize = sizeof(RangeObject); |
| 1468 | RangeType.tp_dealloc = RangeDestructor; |
| 1469 | RangeType.tp_getattr = RangeGetattr; |
| 1470 | RangeType.tp_repr = RangeRepr; |
| 1471 | RangeType.tp_as_sequence = &RangeAsSeq; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1472 | } |