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