Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [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 | /* |
| 21 | * Roland Puntaier 2009/sept/16: |
| 22 | * Adaptations to support both python3.x and python2.x |
| 23 | */ |
| 24 | |
| 25 | // uncomment this if used with the debug version of python |
| 26 | // #define Py_DEBUG |
| 27 | |
| 28 | #include "vim.h" |
| 29 | |
| 30 | #include <limits.h> |
| 31 | |
| 32 | /* Python.h defines _POSIX_THREADS itself (if needed) */ |
| 33 | #ifdef _POSIX_THREADS |
| 34 | # undef _POSIX_THREADS |
| 35 | #endif |
| 36 | |
| 37 | #if defined(_WIN32) && defined (HAVE_FCNTL_H) |
| 38 | # undef HAVE_FCNTL_H |
| 39 | #endif |
| 40 | |
| 41 | #ifdef _DEBUG |
| 42 | # undef _DEBUG |
| 43 | #endif |
| 44 | |
| 45 | #ifdef HAVE_STDARG_H |
| 46 | # undef HAVE_STDARG_H /* Python's config.h defines it as well. */ |
| 47 | #endif |
| 48 | |
| 49 | #define PY_SSIZE_T_CLEAN |
| 50 | |
| 51 | #ifdef F_BLANK |
| 52 | # undef F_BLANK |
| 53 | #endif |
| 54 | |
| 55 | #ifdef _POSIX_C_SOURCE /* defined in feature.h */ |
| 56 | # undef _POSIX_C_SOURCE |
| 57 | #endif |
| 58 | |
| 59 | #include <Python.h> |
| 60 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 61 | # include "macglue.h" |
| 62 | # include <CodeFragments.h> |
| 63 | #endif |
| 64 | #undef main /* Defined in python.h - aargh */ |
| 65 | #undef HAVE_FCNTL_H /* Clash with os_win32.h */ |
| 66 | |
| 67 | static void init_structs(void); |
| 68 | |
| 69 | #if defined(DYNAMIC_PYTHON3) |
| 70 | |
| 71 | #ifndef _WIN32 |
| 72 | #include <dlfcn.h> |
| 73 | #define FARPROC void* |
| 74 | #define HINSTANCE void* |
| 75 | #define load_dll(n) dlopen((n),RTLD_LAZY) |
| 76 | #define close_dll dlclose |
| 77 | #define symbol_from_dll dlsym |
| 78 | #else |
| 79 | #define load_dll LoadLibrary |
| 80 | #define close_dll FreeLibrary |
| 81 | #define symbol_from_dll GetProcAddress |
| 82 | #endif |
| 83 | /* |
| 84 | * Wrapper defines |
| 85 | */ |
| 86 | #undef PyArg_Parse |
| 87 | # define PyArg_Parse py3_PyArg_Parse |
| 88 | #undef PyArg_ParseTuple |
| 89 | # define PyArg_ParseTuple py3_PyArg_ParseTuple |
| 90 | # define PyDict_SetItemString py3_PyDict_SetItemString |
| 91 | # define PyErr_BadArgument py3_PyErr_BadArgument |
| 92 | # define PyErr_Clear py3_PyErr_Clear |
| 93 | # define PyErr_NoMemory py3_PyErr_NoMemory |
| 94 | # define PyErr_Occurred py3_PyErr_Occurred |
| 95 | # define PyErr_SetNone py3_PyErr_SetNone |
| 96 | # define PyErr_SetString py3_PyErr_SetString |
| 97 | # define PyEval_InitThreads py3_PyEval_InitThreads |
| 98 | # define PyEval_RestoreThread py3_PyEval_RestoreThread |
| 99 | # define PyEval_SaveThread py3_PyEval_SaveThread |
| 100 | # define PyGILState_Ensure py3_PyGILState_Ensure |
| 101 | # define PyGILState_Release py3_PyGILState_Release |
| 102 | # define PyLong_AsLong py3_PyLong_AsLong |
| 103 | # define PyLong_FromLong py3_PyLong_FromLong |
| 104 | # define PyList_GetItem py3_PyList_GetItem |
| 105 | # define PyList_Append py3_PyList_Append |
| 106 | # define PyList_New py3_PyList_New |
| 107 | # define PyList_SetItem py3_PyList_SetItem |
| 108 | # define PyList_Size py3_PyList_Size |
| 109 | # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx |
| 110 | # define PyImport_ImportModule py3_PyImport_ImportModule |
| 111 | # define PyObject_Init py3__PyObject_Init |
| 112 | # define PyDict_New py3_PyDict_New |
| 113 | # define PyDict_GetItemString py3_PyDict_GetItemString |
| 114 | # define PyModule_GetDict py3_PyModule_GetDict |
| 115 | #undef PyRun_SimpleString |
| 116 | # define PyRun_SimpleString py3_PyRun_SimpleString |
| 117 | # define PySys_SetObject py3_PySys_SetObject |
| 118 | # define PySys_SetArgv py3_PySys_SetArgv |
| 119 | # define PyType_Type (*py3_PyType_Type) |
| 120 | # define PyType_Ready py3_PyType_Ready |
| 121 | #undef Py_BuildValue |
| 122 | # define Py_BuildValue py3_Py_BuildValue |
| 123 | # define Py_Initialize py3_Py_Initialize |
| 124 | # define Py_Finalize py3_Py_Finalize |
| 125 | # define Py_IsInitialized py3_Py_IsInitialized |
| 126 | # define _Py_NoneStruct (*py3__Py_NoneStruct) |
| 127 | # define PyModule_AddObject py3_PyModule_AddObject |
| 128 | # define PyImport_AppendInittab py3_PyImport_AppendInittab |
| 129 | # define _PyUnicode_AsString py3__PyUnicode_AsString |
| 130 | # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr |
| 131 | # define PySlice_Type (*py3_PySlice_Type) |
| 132 | #ifdef Py_DEBUG |
| 133 | # define _Py_NegativeRefcount py3__Py_NegativeRefcount |
| 134 | # define _Py_RefTotal (*py3__Py_RefTotal) |
| 135 | # define _Py_Dealloc py3__Py_Dealloc |
| 136 | # define _PyObject_DebugMalloc py3__PyObject_DebugMalloc |
| 137 | # define _PyObject_DebugFree py3__PyObject_DebugFree |
| 138 | #else |
| 139 | # define PyObject_Malloc py3_PyObject_Malloc |
| 140 | # define PyObject_Free py3_PyObject_Free |
| 141 | #endif |
| 142 | # define PyType_GenericAlloc py3_PyType_GenericAlloc |
| 143 | # define PyType_GenericNew py3_PyType_GenericNew |
| 144 | # define PyModule_Create2 py3_PyModule_Create2 |
| 145 | #undef PyUnicode_FromString |
| 146 | # define PyUnicode_FromString py3_PyUnicode_FromString |
| 147 | #undef PyUnicode_FromStringAndSize |
| 148 | # define PyUnicode_FromStringAndSize py3_PyUnicode_FromStringAndSize |
| 149 | |
| 150 | #ifdef Py_DEBUG |
| 151 | #undef PyObject_NEW |
| 152 | #define PyObject_NEW(type, typeobj) \ |
| 153 | ( (type *) PyObject_Init( \ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 154 | (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) ) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 155 | #endif |
| 156 | /* |
| 157 | * Pointers for dynamic link |
| 158 | */ |
| 159 | static int (*py3_PySys_SetArgv)(int, wchar_t **); |
| 160 | static void (*py3_Py_Initialize)(void); |
| 161 | static PyObject* (*py3_PyList_New)(Py_ssize_t size); |
| 162 | static PyGILState_STATE (*py3_PyGILState_Ensure)(void); |
| 163 | static void (*py3_PyGILState_Release)(PyGILState_STATE); |
| 164 | static int (*py3_PySys_SetObject)(char *, PyObject *); |
| 165 | static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *); |
| 166 | static Py_ssize_t (*py3_PyList_Size)(PyObject *); |
| 167 | static int (*py3_PySlice_GetIndicesEx)(PySliceObject *r, Py_ssize_t length, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 168 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 169 | static PyObject* (*py3_PyErr_NoMemory)(void); |
| 170 | static void (*py3_Py_Finalize)(void); |
| 171 | static void (*py3_PyErr_SetString)(PyObject *, const char *); |
| 172 | static int (*py3_PyRun_SimpleString)(char *); |
| 173 | static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t); |
| 174 | static PyObject* (*py3_PyImport_ImportModule)(const char *); |
| 175 | static int (*py3_PyErr_BadArgument)(void); |
| 176 | static PyTypeObject* py3_PyType_Type; |
| 177 | static PyObject* (*py3_PyErr_Occurred)(void); |
| 178 | static PyObject* (*py3_PyModule_GetDict)(PyObject *); |
| 179 | static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *); |
| 180 | static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *); |
| 181 | static PyObject* (*py3_PyLong_FromLong)(long); |
| 182 | static PyObject* (*py3_PyDict_New)(void); |
| 183 | static PyObject* (*py3_Py_BuildValue)(char *, ...); |
| 184 | static int (*py3_PyType_Ready)(PyTypeObject *type); |
| 185 | static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item); |
| 186 | static PyObject* (*py3_PyUnicode_FromString)(const char *u); |
| 187 | static PyObject* (*py3_PyUnicode_FromStringAndSize)(const char *u, Py_ssize_t size); |
| 188 | static long (*py3_PyLong_AsLong)(PyObject *); |
| 189 | static void (*py3_PyErr_SetNone)(PyObject *); |
| 190 | static void (*py3_PyEval_InitThreads)(void); |
| 191 | static void(*py3_PyEval_RestoreThread)(PyThreadState *); |
| 192 | static PyThreadState*(*py3_PyEval_SaveThread)(void); |
| 193 | static int (*py3_PyArg_Parse)(PyObject *, char *, ...); |
| 194 | static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...); |
| 195 | static int (*py3_Py_IsInitialized)(void); |
| 196 | static void (*py3_PyErr_Clear)(void); |
| 197 | static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *); |
| 198 | static PyObject* py3__Py_NoneStruct; |
| 199 | static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o); |
| 200 | static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void)); |
| 201 | static char* (*py3__PyUnicode_AsString)(PyObject *unicode); |
| 202 | static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name); |
| 203 | static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version); |
| 204 | static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems); |
| 205 | static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 206 | static PyTypeObject* py3_PySlice_Type; |
| 207 | #ifdef Py_DEBUG |
| 208 | static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op); |
| 209 | static Py_ssize_t* py3__Py_RefTotal; |
| 210 | static void (*py3__Py_Dealloc)(PyObject *obj); |
| 211 | static void (*py3__PyObject_DebugFree)(void*); |
| 212 | static void* (*py3__PyObject_DebugMalloc)(size_t); |
| 213 | #else |
| 214 | static void (*py3_PyObject_Free)(void*); |
| 215 | static void* (*py3_PyObject_Malloc)(size_t); |
| 216 | #endif |
| 217 | |
| 218 | static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */ |
| 219 | |
| 220 | /* Imported exception objects */ |
| 221 | static PyObject *p3imp_PyExc_AttributeError; |
| 222 | static PyObject *p3imp_PyExc_IndexError; |
| 223 | static PyObject *p3imp_PyExc_KeyboardInterrupt; |
| 224 | static PyObject *p3imp_PyExc_TypeError; |
| 225 | static PyObject *p3imp_PyExc_ValueError; |
| 226 | |
| 227 | # define PyExc_AttributeError p3imp_PyExc_AttributeError |
| 228 | # define PyExc_IndexError p3imp_PyExc_IndexError |
| 229 | # define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt |
| 230 | # define PyExc_TypeError p3imp_PyExc_TypeError |
| 231 | # define PyExc_ValueError p3imp_PyExc_ValueError |
| 232 | |
| 233 | /* |
| 234 | * Table of name to function pointer of python. |
| 235 | */ |
| 236 | # define PYTHON_PROC FARPROC |
| 237 | static struct |
| 238 | { |
| 239 | char *name; |
| 240 | PYTHON_PROC *ptr; |
| 241 | } py3_funcname_table[] = |
| 242 | { |
| 243 | {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv}, |
| 244 | {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize}, |
| 245 | {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple}, |
| 246 | {"PyList_New", (PYTHON_PROC*)&py3_PyList_New}, |
| 247 | {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure}, |
| 248 | {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release}, |
| 249 | {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject}, |
| 250 | {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append}, |
| 251 | {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size}, |
| 252 | {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx}, |
| 253 | {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory}, |
| 254 | {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize}, |
| 255 | {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString}, |
| 256 | {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString}, |
| 257 | {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem}, |
| 258 | {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule}, |
| 259 | {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument}, |
| 260 | {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type}, |
| 261 | {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred}, |
| 262 | {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict}, |
| 263 | {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem}, |
| 264 | {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString}, |
| 265 | {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong}, |
| 266 | {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New}, |
| 267 | {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue}, |
| 268 | {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready}, |
| 269 | {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString}, |
| 270 | {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong}, |
| 271 | {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone}, |
| 272 | {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads}, |
| 273 | {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread}, |
| 274 | {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread}, |
| 275 | {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse}, |
| 276 | {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple}, |
| 277 | {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized}, |
| 278 | {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct}, |
| 279 | {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear}, |
| 280 | {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init}, |
| 281 | {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject}, |
| 282 | {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab}, |
| 283 | {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString}, |
| 284 | {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr}, |
| 285 | {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2}, |
| 286 | {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc}, |
| 287 | {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew}, |
| 288 | {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type}, |
| 289 | #ifdef Py_DEBUG |
| 290 | {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount}, |
| 291 | {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal}, |
| 292 | {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc}, |
| 293 | {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree}, |
| 294 | {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc}, |
| 295 | #else |
| 296 | {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc}, |
| 297 | {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free}, |
| 298 | #endif |
| 299 | {"", NULL}, |
| 300 | }; |
| 301 | |
| 302 | /* |
| 303 | * Free python.dll |
| 304 | */ |
| 305 | static void end_dynamic_python3(void) |
| 306 | { |
| 307 | if (hinstPy3) |
| 308 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 309 | close_dll(hinstPy3); |
| 310 | hinstPy3 = 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Load library and get all pointers. |
| 316 | * Parameter 'libname' provides name of DLL. |
| 317 | * Return OK or FAIL. |
| 318 | */ |
| 319 | static int py3_runtime_link_init(char *libname, int verbose) |
| 320 | { |
| 321 | int i; |
| 322 | |
| 323 | if (hinstPy3) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 324 | return OK; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 325 | hinstPy3 = load_dll(libname); |
| 326 | |
| 327 | if (!hinstPy3) |
| 328 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 329 | if (verbose) |
| 330 | EMSG2(_(e_loadlib), libname); |
| 331 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | for (i = 0; py3_funcname_table[i].ptr; ++i) |
| 335 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 336 | if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3, |
| 337 | py3_funcname_table[i].name)) == NULL) |
| 338 | { |
| 339 | close_dll(hinstPy3); |
| 340 | hinstPy3 = 0; |
| 341 | if (verbose) |
| 342 | EMSG2(_(e_loadfunc), py3_funcname_table[i].name); |
| 343 | return FAIL; |
| 344 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* load unicode functions separately as only the ucs2 or the ucs4 functions |
| 348 | * will be present in the library |
| 349 | */ |
| 350 | void *ucs_from_string, *ucs_from_string_and_size; |
| 351 | |
| 352 | ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString"); |
| 353 | ucs_from_string_and_size = symbol_from_dll(hinstPy3, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 354 | "PyUnicodeUCS2_FromStringAndSize"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 355 | if (!ucs_from_string || !ucs_from_string_and_size) |
| 356 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 357 | ucs_from_string = symbol_from_dll(hinstPy3, |
| 358 | "PyUnicodeUCS4_FromString"); |
| 359 | ucs_from_string_and_size = symbol_from_dll(hinstPy3, |
| 360 | "PyUnicodeUCS4_FromStringAndSize"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 361 | } |
| 362 | if (ucs_from_string && ucs_from_string_and_size) |
| 363 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 364 | py3_PyUnicode_FromString = ucs_from_string; |
| 365 | py3_PyUnicode_FromStringAndSize = ucs_from_string_and_size; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 366 | } |
| 367 | else |
| 368 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 369 | close_dll(hinstPy3); |
| 370 | hinstPy3 = 0; |
| 371 | if (verbose) |
| 372 | EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*"); |
| 373 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | return OK; |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * If python is enabled (there is installed python on Windows system) return |
| 381 | * TRUE, else FALSE. |
| 382 | */ |
| 383 | int python3_enabled(int verbose) |
| 384 | { |
| 385 | return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK; |
| 386 | } |
| 387 | |
| 388 | /* Load the standard Python exceptions - don't import the symbols from the |
| 389 | * DLL, as this can cause errors (importing data symbols is not reliable). |
| 390 | */ |
| 391 | static void get_py3_exceptions __ARGS((void)); |
| 392 | |
| 393 | static void get_py3_exceptions() |
| 394 | { |
| 395 | PyObject *exmod = PyImport_ImportModule("builtins"); |
| 396 | PyObject *exdict = PyModule_GetDict(exmod); |
| 397 | p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); |
| 398 | p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); |
| 399 | p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt"); |
| 400 | p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError"); |
| 401 | p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError"); |
| 402 | Py_XINCREF(p3imp_PyExc_AttributeError); |
| 403 | Py_XINCREF(p3imp_PyExc_IndexError); |
| 404 | Py_XINCREF(p3imp_PyExc_KeyboardInterrupt); |
| 405 | Py_XINCREF(p3imp_PyExc_TypeError); |
| 406 | Py_XINCREF(p3imp_PyExc_ValueError); |
| 407 | Py_XDECREF(exmod); |
| 408 | } |
| 409 | #endif /* DYNAMIC_PYTHON3 */ |
| 410 | |
| 411 | static void call_PyObject_Free(void *p) |
| 412 | { |
| 413 | #ifdef Py_DEBUG |
| 414 | _PyObject_DebugFree(p); |
| 415 | #else |
| 416 | PyObject_Free(p); |
| 417 | #endif |
| 418 | } |
| 419 | static PyObject* call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 420 | { |
| 421 | return PyType_GenericNew(type,args,kwds); |
| 422 | } |
| 423 | static PyObject* call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
| 424 | { |
| 425 | return PyType_GenericAlloc(type,nitems); |
| 426 | } |
| 427 | |
| 428 | /****************************************************** |
| 429 | * Internal function prototypes. |
| 430 | */ |
| 431 | |
| 432 | static void DoPy3Command(exarg_T *, const char *); |
| 433 | static Py_ssize_t RangeStart; |
| 434 | static Py_ssize_t RangeEnd; |
| 435 | |
| 436 | static void PythonIO_Flush(void); |
| 437 | static int PythonIO_Init(void); |
| 438 | static void PythonIO_Fini(void); |
| 439 | PyMODINIT_FUNC Py3Init_vim(void); |
| 440 | |
| 441 | /* Utility functions for the vim/python interface |
| 442 | * ---------------------------------------------- |
| 443 | */ |
| 444 | static PyObject *GetBufferLine(buf_T *, Py_ssize_t); |
| 445 | |
| 446 | static int SetBufferLine(buf_T *, Py_ssize_t, PyObject *, Py_ssize_t*); |
| 447 | static int InsertBufferLines(buf_T *, Py_ssize_t, PyObject *, Py_ssize_t*); |
| 448 | static PyObject *GetBufferLineList(buf_T *buf, Py_ssize_t lo, Py_ssize_t hi); |
| 449 | |
| 450 | static PyObject *LineToString(const char *); |
| 451 | static char *StringToLine(PyObject *); |
| 452 | |
| 453 | static int VimErrorCheck(void); |
| 454 | |
| 455 | #define PyErr_SetVim(str) PyErr_SetString(VimError, str) |
| 456 | |
| 457 | /****************************************************** |
| 458 | * 1. Python interpreter main program. |
| 459 | */ |
| 460 | |
| 461 | static int py3initialised = 0; |
| 462 | |
| 463 | |
| 464 | static PyGILState_STATE pygilstate = PyGILState_UNLOCKED; |
| 465 | |
| 466 | /* |
| 467 | * obtain a lock on the Vim data structures |
| 468 | */ |
| 469 | static void Python_Lock_Vim(void) |
| 470 | { |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * release a lock on the Vim data structures |
| 475 | */ |
| 476 | static void Python_Release_Vim(void) |
| 477 | { |
| 478 | } |
| 479 | |
| 480 | void python3_end() |
| 481 | { |
| 482 | static int recurse = 0; |
| 483 | |
| 484 | /* If a crash occurs while doing this, don't try again. */ |
| 485 | if (recurse != 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 486 | return; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 487 | |
| 488 | ++recurse; |
| 489 | |
| 490 | #ifdef DYNAMIC_PYTHON3 |
| 491 | if (hinstPy3) |
| 492 | #endif |
| 493 | if (Py_IsInitialized()) |
| 494 | { |
| 495 | // acquire lock before finalizing |
| 496 | pygilstate = PyGILState_Ensure(); |
| 497 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 498 | PythonIO_Fini(); |
| 499 | Py_Finalize(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | #ifdef DYNAMIC_PYTHON3 |
| 503 | end_dynamic_python3(); |
| 504 | #endif |
| 505 | |
| 506 | --recurse; |
| 507 | } |
| 508 | |
| 509 | static int Python3_Init(void) |
| 510 | { |
| 511 | if (!py3initialised) |
| 512 | { |
| 513 | #ifdef DYNAMIC_PYTHON3 |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 514 | if (!python3_enabled(TRUE)) |
| 515 | { |
| 516 | EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); |
| 517 | goto fail; |
| 518 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 519 | #endif |
| 520 | |
| 521 | init_structs(); |
| 522 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 523 | /* initialise threads */ |
| 524 | PyEval_InitThreads(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 525 | |
| 526 | #if !defined(MACOS) || defined(MACOS_X_UNIX) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 527 | Py_Initialize(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 528 | #else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 529 | PyMac_Initialize(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 530 | #endif |
| 531 | |
| 532 | #ifdef DYNAMIC_PYTHON3 |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 533 | get_py3_exceptions(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 534 | #endif |
| 535 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 536 | if (PythonIO_Init()) |
| 537 | goto fail; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 538 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 539 | PyImport_AppendInittab("vim", Py3Init_vim); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 540 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 541 | /* Remove the element from sys.path that was added because of our |
| 542 | * argv[0] value in Py3Init_vim(). Previously we used an empty |
| 543 | * string, but dependinding on the OS we then get an empty entry or |
| 544 | * the current directory in sys.path. */ |
| 545 | PyRun_SimpleString("import sys; sys.path = list(filter(lambda x: x != '/must>not&exist', sys.path))"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 546 | |
| 547 | // lock is created and acquired in PyEval_InitThreads() and thread |
| 548 | // state is created in Py_Initialize() |
| 549 | // there _PyGILState_NoteThreadState() also sets gilcounter to 1 |
| 550 | // (python must have threads enabled!) |
| 551 | // so the following does both: unlock GIL and save thread state in TLS |
| 552 | // without deleting thread state |
| 553 | PyGILState_Release(pygilstate); |
| 554 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 555 | py3initialised = 1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | |
| 560 | fail: |
| 561 | /* We call PythonIO_Flush() here to print any Python errors. |
| 562 | * This is OK, as it is possible to call this function even |
| 563 | * if PythonIO_Init() has not completed successfully (it will |
| 564 | * not do anything in this case). |
| 565 | */ |
| 566 | PythonIO_Flush(); |
| 567 | return -1; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * External interface |
| 572 | */ |
| 573 | static void DoPy3Command(exarg_T *eap, const char *cmd) |
| 574 | { |
| 575 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 576 | GrafPtr oldPort; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 577 | #endif |
| 578 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 579 | char *saved_locale; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 580 | #endif |
| 581 | |
| 582 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 583 | GetPort(&oldPort); |
| 584 | /* Check if the Python library is available */ |
| 585 | if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 586 | goto theend; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 587 | #endif |
| 588 | if (Python3_Init()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 589 | goto theend; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 590 | |
| 591 | RangeStart = eap->line1; |
| 592 | RangeEnd = eap->line2; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 593 | Python_Release_Vim(); /* leave vim */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 594 | |
| 595 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 596 | /* Python only works properly when the LC_NUMERIC locale is "C". */ |
| 597 | saved_locale = setlocale(LC_NUMERIC, NULL); |
| 598 | if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 599 | saved_locale = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 600 | else |
| 601 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 602 | /* Need to make a copy, value may change when setting new locale. */ |
| 603 | saved_locale = (char *)vim_strsave((char_u *)saved_locale); |
| 604 | (void)setlocale(LC_NUMERIC, "C"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 605 | } |
| 606 | #endif |
| 607 | |
| 608 | pygilstate = PyGILState_Ensure(); |
| 609 | |
| 610 | PyRun_SimpleString((char *)(cmd)); |
| 611 | |
| 612 | PyGILState_Release(pygilstate); |
| 613 | |
| 614 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 615 | if (saved_locale != NULL) |
| 616 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 617 | (void)setlocale(LC_NUMERIC, saved_locale); |
| 618 | vim_free(saved_locale); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 619 | } |
| 620 | #endif |
| 621 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 622 | Python_Lock_Vim(); /* enter vim */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 623 | PythonIO_Flush(); |
| 624 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 625 | SetPort(oldPort); |
| 626 | #endif |
| 627 | |
| 628 | theend: |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 629 | return; /* keeps lint happy */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /* |
| 633 | * ":python" |
| 634 | */ |
| 635 | void ex_python3(exarg_T *eap) |
| 636 | { |
| 637 | char_u *script; |
| 638 | |
| 639 | script = script_get(eap, eap->arg); |
| 640 | if (!eap->skip) |
| 641 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 642 | if (script == NULL) |
| 643 | DoPy3Command(eap, (char *)eap->arg); |
| 644 | else |
| 645 | DoPy3Command(eap, (char *)script); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 646 | } |
| 647 | vim_free(script); |
| 648 | } |
| 649 | |
| 650 | #define BUFFER_SIZE 2048 |
| 651 | |
| 652 | /* |
| 653 | * ":pyfile" |
| 654 | */ |
| 655 | void |
| 656 | ex_py3file(exarg_T *eap) |
| 657 | { |
| 658 | static char buffer[BUFFER_SIZE]; |
| 659 | const char *file; |
| 660 | char *p; |
| 661 | int i; |
| 662 | |
| 663 | /* Have to do it like this. PyRun_SimpleFile requires you to pass a |
| 664 | * stdio file pointer, but Vim and the Python DLL are compiled with |
| 665 | * different options under Windows, meaning that stdio pointers aren't |
| 666 | * compatible between the two. Yuk. |
| 667 | * |
| 668 | * construct: exec(compile(open('a_filename').read(), 'a_filename', 'exec')) |
| 669 | * |
| 670 | * We need to escape any backslashes or single quotes in the file name, so that |
| 671 | * Python won't mangle the file name. |
| 672 | */ |
| 673 | |
| 674 | strcpy(buffer, "exec(compile(open('"); |
| 675 | p = buffer + 19; /* size of "exec(compile(open('" */ |
| 676 | |
| 677 | for (i=0; i<2; ++i) |
| 678 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 679 | file = (char *)eap->arg; |
| 680 | while (*file && p < buffer + (BUFFER_SIZE - 3)) |
| 681 | { |
| 682 | if (*file == '\\' || *file == '\'') |
| 683 | *p++ = '\\'; |
| 684 | *p++ = *file++; |
| 685 | } |
| 686 | /* If we didn't finish the file name, we hit a buffer overflow */ |
| 687 | if (*file != '\0') |
| 688 | return; |
| 689 | if (i==0) |
| 690 | { |
| 691 | strcpy(p,"').read(),'"); |
| 692 | p += 11; |
| 693 | } |
| 694 | else |
| 695 | { |
| 696 | strcpy(p,"','exec'))"); |
| 697 | p += 10; |
| 698 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | |
| 702 | /* Execute the file */ |
| 703 | DoPy3Command(eap, buffer); |
| 704 | } |
| 705 | |
| 706 | /****************************************************** |
| 707 | * 2. Python output stream: writes output via [e]msg(). |
| 708 | */ |
| 709 | |
| 710 | /* Implementation functions |
| 711 | */ |
| 712 | |
| 713 | static PyObject *OutputGetattro(PyObject *, PyObject *); |
| 714 | static int OutputSetattro(PyObject *, PyObject *, PyObject *); |
| 715 | |
| 716 | static PyObject *OutputWrite(PyObject *, PyObject *); |
| 717 | static PyObject *OutputWritelines(PyObject *, PyObject *); |
| 718 | |
| 719 | typedef void (*writefn)(char_u *); |
| 720 | static void writer(writefn fn, char_u *str, Py_ssize_t n); |
| 721 | |
| 722 | /* Output object definition |
| 723 | */ |
| 724 | |
| 725 | typedef struct |
| 726 | { |
| 727 | PyObject_HEAD |
| 728 | long softspace; |
| 729 | long error; |
| 730 | } OutputObject; |
| 731 | |
| 732 | static struct PyMethodDef OutputMethods[] = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 733 | /* name, function, calling, documentation */ |
| 734 | {"write", OutputWrite, 1, "" }, |
| 735 | {"writelines", OutputWritelines, 1, "" }, |
| 736 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 737 | }; |
| 738 | |
| 739 | static PyTypeObject OutputType; |
| 740 | |
| 741 | /*************/ |
| 742 | |
| 743 | static PyObject * OutputGetattro(PyObject *self, PyObject *nameobj) |
| 744 | { |
| 745 | char *name = ""; |
| 746 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 747 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 748 | |
| 749 | if (strcmp(name, "softspace") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 750 | return PyLong_FromLong(((OutputObject *)(self))->softspace); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 751 | |
| 752 | return PyObject_GenericGetAttr(self, nameobj); |
| 753 | } |
| 754 | |
| 755 | static int OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
| 756 | { |
| 757 | char *name = ""; |
| 758 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 759 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 760 | |
| 761 | if (val == NULL) { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 762 | PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes")); |
| 763 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | if (strcmp(name, "softspace") == 0) |
| 767 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 768 | if (!PyLong_Check(val)) { |
| 769 | PyErr_SetString(PyExc_TypeError, _("softspace must be an integer")); |
| 770 | return -1; |
| 771 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 772 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 773 | ((OutputObject *)(self))->softspace = PyLong_AsLong(val); |
| 774 | return 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); |
| 778 | return -1; |
| 779 | } |
| 780 | |
| 781 | /*************/ |
| 782 | |
| 783 | static PyObject * OutputWrite(PyObject *self, PyObject *args) |
| 784 | { |
| 785 | int len; |
| 786 | char *str; |
| 787 | int error = ((OutputObject *)(self))->error; |
| 788 | |
| 789 | if (!PyArg_ParseTuple(args, "s#", &str, &len)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 790 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 791 | |
| 792 | Py_BEGIN_ALLOW_THREADS |
| 793 | Python_Lock_Vim(); |
| 794 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 795 | Python_Release_Vim(); |
| 796 | Py_END_ALLOW_THREADS |
| 797 | |
| 798 | Py_INCREF(Py_None); |
| 799 | return Py_None; |
| 800 | } |
| 801 | |
| 802 | static PyObject * OutputWritelines(PyObject *self, PyObject *args) |
| 803 | { |
| 804 | Py_ssize_t n; |
| 805 | Py_ssize_t i; |
| 806 | PyObject *list; |
| 807 | int error = ((OutputObject *)(self))->error; |
| 808 | |
| 809 | if (!PyArg_ParseTuple(args, "O", &list)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 810 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 811 | Py_INCREF(list); |
| 812 | |
| 813 | if (!PyList_Check(list)) { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 814 | PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
| 815 | Py_DECREF(list); |
| 816 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | n = PyList_Size(list); |
| 820 | |
| 821 | for (i = 0; i < n; ++i) |
| 822 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 823 | PyObject *line = PyList_GetItem(list, i); |
| 824 | char *str; |
| 825 | Py_ssize_t len; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 826 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 827 | if (!PyArg_Parse(line, "s#", &str, &len)) { |
| 828 | PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
| 829 | Py_DECREF(list); |
| 830 | return NULL; |
| 831 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 832 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 833 | Py_BEGIN_ALLOW_THREADS |
| 834 | Python_Lock_Vim(); |
| 835 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 836 | Python_Release_Vim(); |
| 837 | Py_END_ALLOW_THREADS |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | Py_DECREF(list); |
| 841 | Py_INCREF(Py_None); |
| 842 | return Py_None; |
| 843 | } |
| 844 | |
| 845 | /* Output buffer management |
| 846 | */ |
| 847 | |
| 848 | static char_u *buffer = NULL; |
| 849 | static Py_ssize_t buffer_len = 0; |
| 850 | static Py_ssize_t buffer_size = 0; |
| 851 | |
| 852 | static writefn old_fn = NULL; |
| 853 | |
| 854 | static void buffer_ensure(Py_ssize_t n) |
| 855 | { |
| 856 | Py_ssize_t new_size; |
| 857 | char_u *new_buffer; |
| 858 | |
| 859 | if (n < buffer_size) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 860 | return; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 861 | |
| 862 | new_size = buffer_size; |
| 863 | while (new_size < n) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 864 | new_size += 80; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 865 | |
| 866 | if (new_size != buffer_size) |
| 867 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 868 | new_buffer = alloc((unsigned)new_size); |
| 869 | if (new_buffer == NULL) |
| 870 | return; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 871 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 872 | if (buffer) |
| 873 | { |
| 874 | memcpy(new_buffer, buffer, buffer_len); |
| 875 | vim_free(buffer); |
| 876 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 877 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 878 | buffer = new_buffer; |
| 879 | buffer_size = new_size; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
| 883 | static void PythonIO_Flush(void) |
| 884 | { |
| 885 | if (old_fn && buffer_len) |
| 886 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 887 | buffer[buffer_len] = 0; |
| 888 | old_fn(buffer); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | buffer_len = 0; |
| 892 | } |
| 893 | |
| 894 | static void writer(writefn fn, char_u *str, Py_ssize_t n) |
| 895 | { |
| 896 | char_u *ptr; |
| 897 | |
| 898 | if (fn != old_fn && old_fn != NULL) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 899 | PythonIO_Flush(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 900 | |
| 901 | old_fn = fn; |
| 902 | |
| 903 | while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) |
| 904 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 905 | Py_ssize_t len = ptr - str; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 906 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 907 | buffer_ensure(buffer_len + len + 1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 908 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 909 | memcpy(buffer + buffer_len, str, len); |
| 910 | buffer_len += len; |
| 911 | buffer[buffer_len] = 0; |
| 912 | fn(buffer); |
| 913 | str = ptr + 1; |
| 914 | n -= len + 1; |
| 915 | buffer_len = 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /* Put the remaining text into the buffer for later printing */ |
| 919 | buffer_ensure(buffer_len + n + 1); |
| 920 | memcpy(buffer + buffer_len, str, n); |
| 921 | buffer_len += n; |
| 922 | } |
| 923 | |
| 924 | /***************/ |
| 925 | |
| 926 | static OutputObject Output = |
| 927 | { |
| 928 | PyObject_HEAD_INIT(&OutputType) |
| 929 | 0, |
| 930 | 0 |
| 931 | }; |
| 932 | |
| 933 | static OutputObject Error = |
| 934 | { |
| 935 | PyObject_HEAD_INIT(&OutputType) |
| 936 | 0, |
| 937 | 1 |
| 938 | }; |
| 939 | |
| 940 | static int PythonIO_Init(void) |
| 941 | { |
| 942 | PyType_Ready(&OutputType); |
| 943 | |
| 944 | PySys_SetObject("stdout", (PyObject *)(void *)&Output); |
| 945 | PySys_SetObject("stderr", (PyObject *)(void *)&Error); |
| 946 | |
| 947 | if (PyErr_Occurred()) |
| 948 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 949 | EMSG(_("E264: Python: Error initialising I/O objects")); |
| 950 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | return 0; |
| 954 | } |
| 955 | static void PythonIO_Fini(void) |
| 956 | { |
| 957 | PySys_SetObject("stdout", NULL); |
| 958 | PySys_SetObject("stderr", NULL); |
| 959 | } |
| 960 | |
| 961 | /****************************************************** |
| 962 | * 3. Implementation of the Vim module for Python |
| 963 | */ |
| 964 | |
| 965 | /* Vim module - Implementation functions |
| 966 | * ------------------------------------- |
| 967 | */ |
| 968 | |
| 969 | static PyObject *VimError; |
| 970 | |
| 971 | static PyObject *VimCommand(PyObject *, PyObject *); |
| 972 | static PyObject *VimEval(PyObject *, PyObject *); |
| 973 | |
| 974 | /* Window type - Implementation functions |
| 975 | * -------------------------------------- |
| 976 | */ |
| 977 | |
| 978 | typedef struct |
| 979 | { |
| 980 | PyObject_HEAD |
| 981 | win_T *win; |
| 982 | } |
| 983 | WindowObject; |
| 984 | |
| 985 | #define INVALID_WINDOW_VALUE ((win_T *)(-1)) |
| 986 | |
| 987 | #define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType) |
| 988 | |
| 989 | static PyObject *WindowNew(win_T *); |
| 990 | |
| 991 | static void WindowDestructor(PyObject *); |
| 992 | static PyObject *WindowGetattro(PyObject *, PyObject *); |
| 993 | static int WindowSetattro(PyObject *, PyObject *, PyObject *); |
| 994 | static PyObject *WindowRepr(PyObject *); |
| 995 | |
| 996 | /* Buffer type - Implementation functions |
| 997 | * -------------------------------------- |
| 998 | */ |
| 999 | |
| 1000 | typedef struct |
| 1001 | { |
| 1002 | PyObject_HEAD |
| 1003 | buf_T *buf; |
| 1004 | } |
| 1005 | BufferObject; |
| 1006 | |
| 1007 | #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) |
| 1008 | |
| 1009 | #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType) |
| 1010 | |
| 1011 | static PyObject *BufferNew (buf_T *); |
| 1012 | |
| 1013 | static void BufferDestructor(PyObject *); |
| 1014 | |
| 1015 | static PyObject *BufferGetattro(PyObject *, PyObject*); |
| 1016 | static PyObject *BufferRepr(PyObject *); |
| 1017 | |
| 1018 | static Py_ssize_t BufferLength(PyObject *); |
| 1019 | static PyObject *BufferItem(PyObject *, Py_ssize_t); |
| 1020 | static Py_ssize_t BufferAsItem(PyObject *, Py_ssize_t, PyObject *); |
| 1021 | static PyObject* BufferSubscript(PyObject *self, PyObject* idx); |
| 1022 | |
| 1023 | static PyObject *BufferAppend(PyObject *, PyObject *); |
| 1024 | static PyObject *BufferMark(PyObject *, PyObject *); |
| 1025 | static PyObject *BufferRange(PyObject *, PyObject *); |
| 1026 | |
| 1027 | /* Line range type - Implementation functions |
| 1028 | * -------------------------------------- |
| 1029 | */ |
| 1030 | |
| 1031 | typedef struct |
| 1032 | { |
| 1033 | PyObject_HEAD |
| 1034 | BufferObject *buf; |
| 1035 | Py_ssize_t start; |
| 1036 | Py_ssize_t end; |
| 1037 | } |
| 1038 | RangeObject; |
| 1039 | |
| 1040 | #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType) |
| 1041 | |
| 1042 | static PyObject *RangeNew(buf_T *, Py_ssize_t, Py_ssize_t); |
| 1043 | |
| 1044 | static void RangeDestructor(PyObject *); |
| 1045 | static PyObject *RangeGetattro(PyObject *, PyObject *); |
| 1046 | static PyObject *RangeRepr(PyObject *); |
| 1047 | static PyObject* RangeSubscript(PyObject *self, PyObject* idx); |
| 1048 | |
| 1049 | static Py_ssize_t RangeLength(PyObject *); |
| 1050 | static PyObject *RangeItem(PyObject *, Py_ssize_t); |
| 1051 | static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *); |
| 1052 | |
| 1053 | static PyObject *RangeAppend(PyObject *, PyObject *); |
| 1054 | |
| 1055 | /* Window list type - Implementation functions |
| 1056 | * ------------------------------------------- |
| 1057 | */ |
| 1058 | |
| 1059 | static Py_ssize_t WinListLength(PyObject *); |
| 1060 | static PyObject *WinListItem(PyObject *, Py_ssize_t); |
| 1061 | |
| 1062 | /* Buffer list type - Implementation functions |
| 1063 | * ------------------------------------------- |
| 1064 | */ |
| 1065 | |
| 1066 | static Py_ssize_t BufListLength(PyObject *); |
| 1067 | static PyObject *BufListItem(PyObject *, Py_ssize_t); |
| 1068 | |
| 1069 | /* Current objects type - Implementation functions |
| 1070 | * ----------------------------------------------- |
| 1071 | */ |
| 1072 | |
| 1073 | static PyObject *CurrentGetattro(PyObject *, PyObject *); |
| 1074 | static int CurrentSetattro(PyObject *, PyObject *, PyObject *); |
| 1075 | |
| 1076 | /* Vim module - Definitions |
| 1077 | */ |
| 1078 | |
| 1079 | static struct PyMethodDef VimMethods[] = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1080 | /* name, function, calling, documentation */ |
| 1081 | {"command", VimCommand, 1, "Execute a Vim ex-mode command" }, |
| 1082 | {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" }, |
| 1083 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1084 | }; |
| 1085 | |
| 1086 | /* Vim module - Implementation |
| 1087 | */ |
| 1088 | /*ARGSUSED*/ |
| 1089 | static PyObject * VimCommand(PyObject *self UNUSED, PyObject *args) |
| 1090 | { |
| 1091 | char *cmd; |
| 1092 | PyObject *result; |
| 1093 | |
| 1094 | if (!PyArg_ParseTuple(args, "s", &cmd)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1095 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1096 | |
| 1097 | PyErr_Clear(); |
| 1098 | |
| 1099 | Py_BEGIN_ALLOW_THREADS |
| 1100 | Python_Lock_Vim(); |
| 1101 | |
| 1102 | do_cmdline_cmd((char_u *)cmd); |
| 1103 | update_screen(VALID); |
| 1104 | |
| 1105 | Python_Release_Vim(); |
| 1106 | Py_END_ALLOW_THREADS |
| 1107 | |
| 1108 | if (VimErrorCheck()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1109 | result = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1110 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1111 | result = Py_None; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1112 | |
| 1113 | Py_XINCREF(result); |
| 1114 | return result; |
| 1115 | } |
| 1116 | |
| 1117 | #ifdef FEAT_EVAL |
| 1118 | /* |
| 1119 | * Function to translate a typval_T into a PyObject; this will recursively |
| 1120 | * translate lists/dictionaries into their Python equivalents. |
| 1121 | * |
| 1122 | * The depth parameter is to avoid infinite recursion, set it to 1 when |
| 1123 | * you call VimToPython. |
| 1124 | */ |
| 1125 | static PyObject * VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) |
| 1126 | { |
| 1127 | PyObject *result; |
| 1128 | PyObject *newObj; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1129 | char ptrBuf[NUMBUFLEN]; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1130 | |
| 1131 | /* Avoid infinite recursion */ |
| 1132 | if (depth > 100) |
| 1133 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1134 | Py_INCREF(Py_None); |
| 1135 | result = Py_None; |
| 1136 | return result; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /* Check if we run into a recursive loop. The item must be in lookupDict |
| 1140 | * then and we can use it again. */ |
| 1141 | if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1142 | || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL)) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1143 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1144 | sprintf(ptrBuf, PRINTF_DECIMAL_LONG_U, |
| 1145 | our_tv->v_type == VAR_LIST ? (long_u)our_tv->vval.v_list |
| 1146 | : (long_u)our_tv->vval.v_dict); |
| 1147 | result = PyDict_GetItemString(lookupDict, ptrBuf); |
| 1148 | if (result != NULL) |
| 1149 | { |
| 1150 | Py_INCREF(result); |
| 1151 | return result; |
| 1152 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | if (our_tv->v_type == VAR_STRING) |
| 1156 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1157 | result = Py_BuildValue("s", our_tv->vval.v_string); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1158 | } |
| 1159 | else if (our_tv->v_type == VAR_NUMBER) |
| 1160 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1161 | char buf[NUMBUFLEN]; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1162 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1163 | /* For backwards compatibility numbers are stored as strings. */ |
| 1164 | sprintf(buf, "%ld", (long)our_tv->vval.v_number); |
| 1165 | result = Py_BuildValue("s", buf); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1166 | } |
| 1167 | # ifdef FEAT_FLOAT |
| 1168 | else if (our_tv->v_type == VAR_FLOAT) |
| 1169 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1170 | char buf[NUMBUFLEN]; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1171 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1172 | sprintf(buf, "%f", our_tv->vval.v_float); |
| 1173 | result = Py_BuildValue("s", buf); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1174 | } |
| 1175 | # endif |
| 1176 | else if (our_tv->v_type == VAR_LIST) |
| 1177 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1178 | list_T *list = our_tv->vval.v_list; |
| 1179 | listitem_T *curr; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1180 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1181 | result = PyList_New(0); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1182 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1183 | if (list != NULL) |
| 1184 | { |
| 1185 | PyDict_SetItemString(lookupDict, ptrBuf, result); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1186 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1187 | for (curr = list->lv_first; curr != NULL; curr = curr->li_next) |
| 1188 | { |
| 1189 | newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict); |
| 1190 | PyList_Append(result, newObj); |
| 1191 | Py_DECREF(newObj); |
| 1192 | } |
| 1193 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1194 | } |
| 1195 | else if (our_tv->v_type == VAR_DICT) |
| 1196 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1197 | result = PyDict_New(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1198 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1199 | if (our_tv->vval.v_dict != NULL) |
| 1200 | { |
| 1201 | hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab; |
| 1202 | long_u t = ht->ht_used; |
| 1203 | hashitem_T *hi; |
| 1204 | dictitem_T *di; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1205 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1206 | PyDict_SetItemString(lookupDict, ptrBuf, result); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1207 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1208 | for (hi = ht->ht_array; t > 0; ++hi) |
| 1209 | { |
| 1210 | if (!HASHITEM_EMPTY(hi)) |
| 1211 | { |
| 1212 | --t; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1213 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1214 | di = dict_lookup(hi); |
| 1215 | newObj = VimToPython(&di->di_tv, depth + 1, lookupDict); |
| 1216 | PyDict_SetItemString(result, (char *)hi->hi_key, newObj); |
| 1217 | Py_DECREF(newObj); |
| 1218 | } |
| 1219 | } |
| 1220 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1221 | } |
| 1222 | else |
| 1223 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1224 | Py_INCREF(Py_None); |
| 1225 | result = Py_None; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | return result; |
| 1229 | } |
| 1230 | #endif |
| 1231 | |
| 1232 | /*ARGSUSED*/ |
| 1233 | static PyObject * VimEval(PyObject *self UNUSED, PyObject *args) |
| 1234 | { |
| 1235 | #ifdef FEAT_EVAL |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1236 | char *expr; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1237 | typval_T *our_tv; |
| 1238 | PyObject *result; |
| 1239 | PyObject *lookup_dict; |
| 1240 | |
| 1241 | if (!PyArg_ParseTuple(args, "s", &expr)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1242 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1243 | |
| 1244 | Py_BEGIN_ALLOW_THREADS |
| 1245 | Python_Lock_Vim(); |
| 1246 | our_tv = eval_expr((char_u *)expr, NULL); |
| 1247 | |
| 1248 | Python_Release_Vim(); |
| 1249 | Py_END_ALLOW_THREADS |
| 1250 | |
| 1251 | if (our_tv == NULL) |
| 1252 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1253 | PyErr_SetVim(_("invalid expression")); |
| 1254 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /* Convert the Vim type into a Python type. Create a dictionary that's |
| 1258 | * used to check for recursive loops. */ |
| 1259 | lookup_dict = PyDict_New(); |
| 1260 | result = VimToPython(our_tv, 1, lookup_dict); |
| 1261 | Py_DECREF(lookup_dict); |
| 1262 | |
| 1263 | |
| 1264 | Py_BEGIN_ALLOW_THREADS |
| 1265 | Python_Lock_Vim(); |
| 1266 | free_tv(our_tv); |
| 1267 | Python_Release_Vim(); |
| 1268 | Py_END_ALLOW_THREADS |
| 1269 | |
| 1270 | return result; |
| 1271 | #else |
| 1272 | PyErr_SetVim(_("expressions disabled at compile time")); |
| 1273 | return NULL; |
| 1274 | #endif |
| 1275 | } |
| 1276 | |
| 1277 | /* Common routines for buffers and line ranges |
| 1278 | * ------------------------------------------- |
| 1279 | */ |
| 1280 | |
| 1281 | static int CheckBuffer(BufferObject *this) |
| 1282 | { |
| 1283 | if (this->buf == INVALID_BUFFER_VALUE) |
| 1284 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1285 | PyErr_SetVim(_("attempt to refer to deleted buffer")); |
| 1286 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | static PyObject * RBItem(BufferObject *self, Py_ssize_t n, Py_ssize_t start, Py_ssize_t end) |
| 1293 | { |
| 1294 | if (CheckBuffer(self)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1295 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1296 | |
| 1297 | if (n < 0 || n > end - start) |
| 1298 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1299 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 1300 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | return GetBufferLine(self->buf, n+start); |
| 1304 | } |
| 1305 | |
| 1306 | static Py_ssize_t RBAsItem(BufferObject *self, Py_ssize_t n, PyObject *val, Py_ssize_t start, Py_ssize_t end, Py_ssize_t *new_end) |
| 1307 | { |
| 1308 | Py_ssize_t len_change; |
| 1309 | |
| 1310 | if (CheckBuffer(self)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1311 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1312 | |
| 1313 | if (n < 0 || n > end - start) |
| 1314 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1315 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 1316 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1320 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1321 | |
| 1322 | if (new_end) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1323 | *new_end = end + len_change; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1324 | |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | static PyObject * RBSlice(BufferObject *self, Py_ssize_t lo, Py_ssize_t hi, Py_ssize_t start, Py_ssize_t end) |
| 1329 | { |
| 1330 | Py_ssize_t size; |
| 1331 | |
| 1332 | if (CheckBuffer(self)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1333 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1334 | |
| 1335 | size = end - start + 1; |
| 1336 | |
| 1337 | if (lo < 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1338 | lo = 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1339 | else if (lo > size) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1340 | lo = size; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1341 | if (hi < 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1342 | hi = 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1343 | if (hi < lo) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1344 | hi = lo; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1345 | else if (hi > size) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1346 | hi = size; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1347 | |
| 1348 | return GetBufferLineList(self->buf, lo+start, hi+start); |
| 1349 | } |
| 1350 | |
| 1351 | static PyObject * RBAppend(BufferObject *self, PyObject *args, Py_ssize_t start, Py_ssize_t end, Py_ssize_t *new_end) |
| 1352 | { |
| 1353 | PyObject *lines; |
| 1354 | Py_ssize_t len_change; |
| 1355 | Py_ssize_t max; |
| 1356 | Py_ssize_t n; |
| 1357 | |
| 1358 | if (CheckBuffer(self)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1359 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1360 | |
| 1361 | max = n = end - start + 1; |
| 1362 | |
| 1363 | if (!PyArg_ParseTuple(args, "O|n" , &lines, &n)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1364 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1365 | |
| 1366 | if (n < 0 || n > max) |
| 1367 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1368 | PyErr_SetString(PyExc_ValueError, _("line number out of range")); |
| 1369 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1373 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1374 | |
| 1375 | if (new_end) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1376 | *new_end = end + len_change; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1377 | |
| 1378 | Py_INCREF(Py_None); |
| 1379 | return Py_None; |
| 1380 | } |
| 1381 | |
| 1382 | |
| 1383 | static struct PyMethodDef BufferMethods[] = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1384 | /* name, function, calling, documentation */ |
| 1385 | {"append", BufferAppend, 1, "Append data to Vim buffer" }, |
| 1386 | {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" }, |
| 1387 | {"range", BufferRange, 1, "Return a range object which represents the part of the given buffer between line numbers s and e" }, |
| 1388 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1389 | }; |
| 1390 | |
| 1391 | static PySequenceMethods BufferAsSeq = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1392 | (lenfunc) BufferLength, /* sq_length, len(x) */ |
| 1393 | (binaryfunc) 0, /* sq_concat, x+y */ |
| 1394 | (ssizeargfunc) 0, /* sq_repeat, x*n */ |
| 1395 | (ssizeargfunc) BufferItem, /* sq_item, x[i] */ |
| 1396 | 0, /* was_sq_slice, x[i:j] */ |
| 1397 | (ssizeobjargproc) BufferAsItem, /* sq_ass_item, x[i]=v */ |
| 1398 | 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1399 | 0, /* sq_contains */ |
| 1400 | 0, /* sq_inplace_concat */ |
| 1401 | 0, /* sq_inplace_repeat */ |
| 1402 | }; |
| 1403 | |
| 1404 | PyMappingMethods BufferAsMapping = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1405 | /* mp_length */ (lenfunc)BufferLength, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1406 | /* mp_subscript */ (binaryfunc)BufferSubscript, |
| 1407 | /* mp_ass_subscript */ (objobjargproc)0, |
| 1408 | }; |
| 1409 | |
| 1410 | |
| 1411 | /* Buffer object - Definitions |
| 1412 | */ |
| 1413 | |
| 1414 | static PyTypeObject BufferType; |
| 1415 | |
| 1416 | static PyObject * BufferNew(buf_T *buf) |
| 1417 | { |
| 1418 | /* We need to handle deletion of buffers underneath us. |
| 1419 | * If we add a "b_python3_ref" field to the buf_T structure, |
| 1420 | * then we can get at it in buf_freeall() in vim. We then |
| 1421 | * need to create only ONE Python object per buffer - if |
| 1422 | * we try to create a second, just INCREF the existing one |
| 1423 | * and return it. The (single) Python object referring to |
| 1424 | * the buffer is stored in "b_python3_ref". |
| 1425 | * Question: what to do on a buf_freeall(). We'll probably |
| 1426 | * have to either delete the Python object (DECREF it to |
| 1427 | * zero - a bad idea, as it leaves dangling refs!) or |
| 1428 | * set the buf_T * value to an invalid value (-1?), which |
| 1429 | * means we need checks in all access functions... Bah. |
| 1430 | */ |
| 1431 | |
| 1432 | BufferObject *self; |
| 1433 | |
| 1434 | if (buf->b_python3_ref != NULL) |
| 1435 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1436 | self = buf->b_python3_ref; |
| 1437 | Py_INCREF(self); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1438 | } |
| 1439 | else |
| 1440 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1441 | self = PyObject_NEW(BufferObject, &BufferType); |
| 1442 | buf->b_python3_ref = self; |
| 1443 | if (self == NULL) |
| 1444 | return NULL; |
| 1445 | self->buf = buf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | return (PyObject *)(self); |
| 1449 | } |
| 1450 | |
| 1451 | static void BufferDestructor(PyObject *self) |
| 1452 | { |
| 1453 | BufferObject *this = (BufferObject *)(self); |
| 1454 | |
| 1455 | if (this->buf && this->buf != INVALID_BUFFER_VALUE) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1456 | this->buf->b_python3_ref = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | static PyObject * BufferGetattro(PyObject *self, PyObject*nameobj) |
| 1460 | { |
| 1461 | BufferObject *this = (BufferObject *)(self); |
| 1462 | |
| 1463 | char *name = ""; |
| 1464 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1465 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1466 | |
| 1467 | if (CheckBuffer(this)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1468 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1469 | |
| 1470 | if (strcmp(name, "name") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1471 | return Py_BuildValue("s", this->buf->b_ffname); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1472 | else if (strcmp(name, "number") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1473 | return Py_BuildValue("n", this->buf->b_fnum); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1474 | else if (strcmp(name,"__members__") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1475 | return Py_BuildValue("[ss]", "name", "number"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1476 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1477 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | static PyObject * BufferRepr(PyObject *self) |
| 1481 | { |
| 1482 | static char repr[100]; |
| 1483 | BufferObject *this = (BufferObject *)(self); |
| 1484 | |
| 1485 | if (this->buf == INVALID_BUFFER_VALUE) |
| 1486 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1487 | vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); |
| 1488 | return PyUnicode_FromString(repr); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1489 | } |
| 1490 | else |
| 1491 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1492 | char *name = (char *)this->buf->b_fname; |
| 1493 | Py_ssize_t len; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1494 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1495 | if (name == NULL) |
| 1496 | name = ""; |
| 1497 | len = strlen(name); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1498 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1499 | if (len > 35) |
| 1500 | name = name + (35 - len); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1501 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1502 | vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1503 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1504 | return PyUnicode_FromString(repr); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | /******************/ |
| 1509 | |
| 1510 | static Py_ssize_t BufferLength(PyObject *self) |
| 1511 | { |
| 1512 | if (CheckBuffer((BufferObject *)(self))) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1513 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1514 | |
| 1515 | return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 1516 | } |
| 1517 | |
| 1518 | static PyObject * BufferItem(PyObject *self, Py_ssize_t n) |
| 1519 | { |
| 1520 | return RBItem((BufferObject *)(self), n, 1, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1521 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | static Py_ssize_t BufferAsItem(PyObject *self, Py_ssize_t n, PyObject *val) |
| 1525 | { |
| 1526 | return RBAsItem((BufferObject *)(self), n, val, 1, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1527 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
| 1528 | NULL); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | static PyObject * BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi) |
| 1532 | { |
| 1533 | return RBSlice((BufferObject *)(self), lo, hi, 1, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1534 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | |
| 1538 | static PyObject* BufferSubscript(PyObject *self, PyObject* idx) |
| 1539 | { |
| 1540 | if (PyLong_Check(idx)) { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1541 | long _idx = PyLong_AsLong(idx); |
| 1542 | return BufferItem(self,_idx); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1543 | } else if (PySlice_Check(idx)) { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1544 | Py_ssize_t start, stop, step, slicelen; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1545 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1546 | if (PySlice_GetIndicesEx((PySliceObject *)idx, |
| 1547 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1, |
| 1548 | &start, &stop, |
| 1549 | &step, &slicelen) < 0) { |
| 1550 | return NULL; |
| 1551 | } |
| 1552 | return BufferSlice(self,start,stop+1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1553 | } else { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1554 | PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); |
| 1555 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | static PyObject * BufferAppend(PyObject *self, PyObject *args) |
| 1560 | { |
| 1561 | return RBAppend((BufferObject *)(self), args, 1, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1562 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
| 1563 | NULL); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | static PyObject * BufferMark(PyObject *self, PyObject *args) |
| 1567 | { |
| 1568 | pos_T *posp; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1569 | char *pmark;//test |
| 1570 | char mark; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1571 | buf_T *curbuf_save; |
| 1572 | |
| 1573 | if (CheckBuffer((BufferObject *)(self))) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1574 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1575 | |
| 1576 | if (!PyArg_ParseTuple(args, "s", &pmark))//test: "c"->"s" |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1577 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1578 | mark = *pmark;//test |
| 1579 | |
| 1580 | curbuf_save = curbuf; |
| 1581 | curbuf = ((BufferObject *)(self))->buf; |
| 1582 | posp = getmark(mark, FALSE); |
| 1583 | curbuf = curbuf_save; |
| 1584 | |
| 1585 | if (posp == NULL) |
| 1586 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1587 | PyErr_SetVim(_("invalid mark name")); |
| 1588 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | /* Ckeck for keyboard interrupt */ |
| 1592 | if (VimErrorCheck()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1593 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1594 | |
| 1595 | if (posp->lnum <= 0) |
| 1596 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1597 | /* Or raise an error? */ |
| 1598 | Py_INCREF(Py_None); |
| 1599 | return Py_None; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col)); |
| 1603 | } |
| 1604 | |
| 1605 | static PyObject * BufferRange(PyObject *self, PyObject *args) |
| 1606 | { |
| 1607 | Py_ssize_t start; |
| 1608 | Py_ssize_t end; |
| 1609 | |
| 1610 | if (CheckBuffer((BufferObject *)(self))) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1611 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1612 | |
| 1613 | if (!PyArg_ParseTuple(args, "nn", &start, &end)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1614 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1615 | |
| 1616 | return RangeNew(((BufferObject *)(self))->buf, start, end); |
| 1617 | } |
| 1618 | |
| 1619 | /* Line range object - Definitions |
| 1620 | */ |
| 1621 | |
| 1622 | static struct PyMethodDef RangeMethods[] = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1623 | /* name, function, calling, documentation */ |
| 1624 | {"append", RangeAppend, 1, "Append data to the Vim range" }, |
| 1625 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1626 | }; |
| 1627 | |
| 1628 | static PySequenceMethods RangeAsSeq = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1629 | (lenfunc) RangeLength, /* sq_length, len(x) */ |
| 1630 | (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */ |
| 1631 | (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */ |
| 1632 | (ssizeargfunc) RangeItem, /* sq_item, x[i] */ |
| 1633 | 0, /* was_sq_slice, x[i:j] */ |
| 1634 | (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */ |
| 1635 | 0, /* sq_ass_slice, x[i:j]=v */ |
| 1636 | 0, /* sq_contains */ |
| 1637 | 0, /* sq_inplace_concat */ |
| 1638 | 0, /* sq_inplace_repeat */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1639 | }; |
| 1640 | |
| 1641 | PyMappingMethods RangeAsMapping = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1642 | /* mp_length */ (lenfunc)RangeLength, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1643 | /* mp_subscript */ (binaryfunc)RangeSubscript, |
| 1644 | /* mp_ass_subscript */ (objobjargproc)0, |
| 1645 | }; |
| 1646 | |
| 1647 | static PyTypeObject RangeType; |
| 1648 | |
| 1649 | /* Line range object - Implementation |
| 1650 | */ |
| 1651 | |
| 1652 | static PyObject * RangeNew(buf_T *buf, Py_ssize_t start, Py_ssize_t end) |
| 1653 | { |
| 1654 | BufferObject *bufr; |
| 1655 | RangeObject *self; |
| 1656 | self = PyObject_NEW(RangeObject, &RangeType); |
| 1657 | if (self == NULL) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1658 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1659 | |
| 1660 | bufr = (BufferObject *)BufferNew(buf); |
| 1661 | if (bufr == NULL) |
| 1662 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1663 | Py_DECREF(self); |
| 1664 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1665 | } |
| 1666 | Py_INCREF(bufr); |
| 1667 | |
| 1668 | self->buf = bufr; |
| 1669 | self->start = start; |
| 1670 | self->end = end; |
| 1671 | |
| 1672 | return (PyObject *)(self); |
| 1673 | } |
| 1674 | |
| 1675 | static void RangeDestructor(PyObject *self) |
| 1676 | { |
| 1677 | Py_DECREF(((RangeObject *)(self))->buf); |
| 1678 | } |
| 1679 | |
| 1680 | static PyObject * RangeGetattro(PyObject *self, PyObject *nameobj) |
| 1681 | { |
| 1682 | char *name = ""; |
| 1683 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1684 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1685 | |
| 1686 | if (strcmp(name, "start") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1687 | return Py_BuildValue("n", ((RangeObject *)(self))->start - 1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1688 | else if (strcmp(name, "end") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1689 | return Py_BuildValue("n", ((RangeObject *)(self))->end - 1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1690 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1691 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | static PyObject * RangeRepr(PyObject *self) |
| 1695 | { |
| 1696 | static char repr[100]; |
| 1697 | RangeObject *this = (RangeObject *)(self); |
| 1698 | |
| 1699 | if (this->buf->buf == INVALID_BUFFER_VALUE) |
| 1700 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1701 | vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>", |
| 1702 | (self)); |
| 1703 | return PyUnicode_FromString(repr); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1704 | } |
| 1705 | else |
| 1706 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1707 | char *name = (char *)this->buf->buf->b_fname; |
| 1708 | int len; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1709 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1710 | if (name == NULL) |
| 1711 | name = ""; |
| 1712 | len = (int)strlen(name); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1713 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1714 | if (len > 45) |
| 1715 | name = name + (45 - len); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1716 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1717 | vim_snprintf(repr, 100, "<range %s%s (%d:%d)>", |
| 1718 | len > 45 ? "..." : "", name, |
| 1719 | this->start, this->end); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1720 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1721 | return PyUnicode_FromString(repr); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | /****************/ |
| 1726 | |
| 1727 | static Py_ssize_t RangeLength(PyObject *self) |
| 1728 | { |
| 1729 | /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
| 1730 | if (CheckBuffer(((RangeObject *)(self))->buf)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1731 | return -1; /* ??? */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1732 | |
| 1733 | return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1); |
| 1734 | } |
| 1735 | |
| 1736 | static PyObject * RangeItem(PyObject *self, Py_ssize_t n) |
| 1737 | { |
| 1738 | return RBItem(((RangeObject *)(self))->buf, n, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1739 | ((RangeObject *)(self))->start, |
| 1740 | ((RangeObject *)(self))->end); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | static Py_ssize_t RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val) |
| 1744 | { |
| 1745 | return RBAsItem(((RangeObject *)(self))->buf, n, val, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1746 | ((RangeObject *)(self))->start, |
| 1747 | ((RangeObject *)(self))->end, |
| 1748 | &((RangeObject *)(self))->end); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | static PyObject * RangeSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi) |
| 1752 | { |
| 1753 | return RBSlice(((RangeObject *)(self))->buf, lo, hi, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1754 | ((RangeObject *)(self))->start, |
| 1755 | ((RangeObject *)(self))->end); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | static PyObject* RangeSubscript(PyObject *self, PyObject* idx) |
| 1759 | { |
| 1760 | if (PyLong_Check(idx)) { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1761 | long _idx = PyLong_AsLong(idx); |
| 1762 | return RangeItem(self,_idx); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1763 | } else if (PySlice_Check(idx)) { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1764 | Py_ssize_t start, stop, step, slicelen; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1765 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1766 | if (PySlice_GetIndicesEx((PySliceObject *)idx, |
| 1767 | ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, |
| 1768 | &start, &stop, |
| 1769 | &step, &slicelen) < 0) { |
| 1770 | return NULL; |
| 1771 | } |
| 1772 | return RangeSlice(self,start,stop+1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1773 | } else { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1774 | PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); |
| 1775 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | static PyObject * RangeAppend(PyObject *self, PyObject *args) |
| 1780 | { |
| 1781 | return RBAppend(((RangeObject *)(self))->buf, args, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1782 | ((RangeObject *)(self))->start, |
| 1783 | ((RangeObject *)(self))->end, |
| 1784 | &((RangeObject *)(self))->end); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | /* Buffer list object - Definitions |
| 1788 | */ |
| 1789 | |
| 1790 | typedef struct |
| 1791 | { |
| 1792 | PyObject_HEAD |
| 1793 | } |
| 1794 | BufListObject; |
| 1795 | |
| 1796 | static PySequenceMethods BufListAsSeq = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1797 | (lenfunc) BufListLength, /* sq_length, len(x) */ |
| 1798 | (binaryfunc) 0, /* sq_concat, x+y */ |
| 1799 | (ssizeargfunc) 0, /* sq_repeat, x*n */ |
| 1800 | (ssizeargfunc) BufListItem, /* sq_item, x[i] */ |
| 1801 | 0, /* was_sq_slice, x[i:j] */ |
| 1802 | (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */ |
| 1803 | 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1804 | 0, /* sq_contains */ |
| 1805 | 0, /* sq_inplace_concat */ |
| 1806 | 0, /* sq_inplace_repeat */ |
| 1807 | }; |
| 1808 | |
| 1809 | static PyTypeObject BufListType; |
| 1810 | |
| 1811 | /* Buffer list object - Implementation |
| 1812 | */ |
| 1813 | |
| 1814 | /*ARGSUSED*/ |
| 1815 | static Py_ssize_t BufListLength(PyObject *self UNUSED) |
| 1816 | { |
| 1817 | buf_T *b = firstbuf; |
| 1818 | Py_ssize_t n = 0; |
| 1819 | |
| 1820 | while (b) |
| 1821 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1822 | ++n; |
| 1823 | b = b->b_next; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | return n; |
| 1827 | } |
| 1828 | |
| 1829 | /*ARGSUSED*/ |
| 1830 | static PyObject * BufListItem(PyObject *self UNUSED, Py_ssize_t n) |
| 1831 | { |
| 1832 | buf_T *b; |
| 1833 | |
| 1834 | for (b = firstbuf; b; b = b->b_next, --n) |
| 1835 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1836 | if (n == 0) |
| 1837 | return BufferNew(b); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | PyErr_SetString(PyExc_IndexError, _("no such buffer")); |
| 1841 | return NULL; |
| 1842 | } |
| 1843 | |
| 1844 | /* Window object - Definitions |
| 1845 | */ |
| 1846 | |
| 1847 | static struct PyMethodDef WindowMethods[] = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1848 | /* name, function, calling, documentation */ |
| 1849 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1850 | }; |
| 1851 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1852 | static PyTypeObject WindowType; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1853 | |
| 1854 | /* Window object - Implementation |
| 1855 | */ |
| 1856 | |
| 1857 | static PyObject * WindowNew(win_T *win) |
| 1858 | { |
| 1859 | /* We need to handle deletion of windows underneath us. |
| 1860 | * If we add a "w_python3_ref" field to the win_T structure, |
| 1861 | * then we can get at it in win_free() in vim. We then |
| 1862 | * need to create only ONE Python object per window - if |
| 1863 | * we try to create a second, just INCREF the existing one |
| 1864 | * and return it. The (single) Python object referring to |
| 1865 | * the window is stored in "w_python3_ref". |
| 1866 | * On a win_free() we set the Python object's win_T* field |
| 1867 | * to an invalid value. We trap all uses of a window |
| 1868 | * object, and reject them if the win_T* field is invalid. |
| 1869 | */ |
| 1870 | |
| 1871 | WindowObject *self; |
| 1872 | |
| 1873 | if (win->w_python3_ref) |
| 1874 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1875 | self = win->w_python3_ref; |
| 1876 | Py_INCREF(self); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1877 | } |
| 1878 | else |
| 1879 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1880 | self = PyObject_NEW(WindowObject, &WindowType); |
| 1881 | if (self == NULL) |
| 1882 | return NULL; |
| 1883 | self->win = win; |
| 1884 | win->w_python3_ref = self; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | return (PyObject *)(self); |
| 1888 | } |
| 1889 | |
| 1890 | static void WindowDestructor(PyObject *self) |
| 1891 | { |
| 1892 | WindowObject *this = (WindowObject *)(self); |
| 1893 | |
| 1894 | if (this->win && this->win != INVALID_WINDOW_VALUE) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1895 | this->win->w_python3_ref = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | static int CheckWindow(WindowObject *this) |
| 1899 | { |
| 1900 | if (this->win == INVALID_WINDOW_VALUE) |
| 1901 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1902 | PyErr_SetVim(_("attempt to refer to deleted window")); |
| 1903 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | return 0; |
| 1907 | } |
| 1908 | |
| 1909 | static PyObject * WindowGetattro(PyObject *self, PyObject *nameobj) |
| 1910 | { |
| 1911 | WindowObject *this = (WindowObject *)(self); |
| 1912 | |
| 1913 | char *name = ""; |
| 1914 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1915 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1916 | |
| 1917 | |
| 1918 | if (CheckWindow(this)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1919 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1920 | |
| 1921 | if (strcmp(name, "buffer") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1922 | return (PyObject *)BufferNew(this->win->w_buffer); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1923 | else if (strcmp(name, "cursor") == 0) |
| 1924 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1925 | pos_T *pos = &this->win->w_cursor; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1926 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1927 | return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1928 | } |
| 1929 | else if (strcmp(name, "height") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1930 | return Py_BuildValue("l", (long)(this->win->w_height)); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1931 | #ifdef FEAT_VERTSPLIT |
| 1932 | else if (strcmp(name, "width") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1933 | return Py_BuildValue("l", (long)(W_WIDTH(this->win))); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1934 | #endif |
| 1935 | else if (strcmp(name,"__members__") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1936 | return Py_BuildValue("[sss]", "buffer", "cursor", "height"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1937 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1938 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | static int WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
| 1942 | { |
| 1943 | WindowObject *this = (WindowObject *)(self); |
| 1944 | |
| 1945 | char *name = ""; |
| 1946 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1947 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1948 | |
| 1949 | |
| 1950 | if (CheckWindow(this)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1951 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1952 | |
| 1953 | if (strcmp(name, "buffer") == 0) |
| 1954 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1955 | PyErr_SetString(PyExc_TypeError, _("readonly attribute")); |
| 1956 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1957 | } |
| 1958 | else if (strcmp(name, "cursor") == 0) |
| 1959 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1960 | long lnum; |
| 1961 | long col; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1962 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1963 | if (!PyArg_Parse(val, "(ll)", &lnum, &col)) |
| 1964 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1965 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1966 | if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count) |
| 1967 | { |
| 1968 | PyErr_SetVim(_("cursor position outside buffer")); |
| 1969 | return -1; |
| 1970 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1971 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1972 | /* Check for keyboard interrupts */ |
| 1973 | if (VimErrorCheck()) |
| 1974 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1975 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1976 | /* NO CHECK ON COLUMN - SEEMS NOT TO MATTER */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1977 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1978 | this->win->w_cursor.lnum = lnum; |
| 1979 | this->win->w_cursor.col = col; |
| 1980 | update_screen(VALID); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1981 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1982 | return 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1983 | } |
| 1984 | else if (strcmp(name, "height") == 0) |
| 1985 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1986 | int height; |
| 1987 | win_T *savewin; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1988 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1989 | if (!PyArg_Parse(val, "i", &height)) |
| 1990 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1991 | |
| 1992 | #ifdef FEAT_GUI |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1993 | need_mouse_correct = TRUE; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1994 | #endif |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1995 | savewin = curwin; |
| 1996 | curwin = this->win; |
| 1997 | win_setheight(height); |
| 1998 | curwin = savewin; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1999 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2000 | /* Check for keyboard interrupts */ |
| 2001 | if (VimErrorCheck()) |
| 2002 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2003 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2004 | return 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2005 | } |
| 2006 | #ifdef FEAT_VERTSPLIT |
| 2007 | else if (strcmp(name, "width") == 0) |
| 2008 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2009 | int width; |
| 2010 | win_T *savewin; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2011 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2012 | if (!PyArg_Parse(val, "i", &width)) |
| 2013 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2014 | |
| 2015 | #ifdef FEAT_GUI |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2016 | need_mouse_correct = TRUE; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2017 | #endif |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2018 | savewin = curwin; |
| 2019 | curwin = this->win; |
| 2020 | win_setwidth(width); |
| 2021 | curwin = savewin; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2022 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2023 | /* Check for keyboard interrupts */ |
| 2024 | if (VimErrorCheck()) |
| 2025 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2026 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2027 | return 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2028 | } |
| 2029 | #endif |
| 2030 | else |
| 2031 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2032 | PyErr_SetString(PyExc_AttributeError, name); |
| 2033 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | static PyObject * WindowRepr(PyObject *self) |
| 2038 | { |
| 2039 | static char repr[100]; |
| 2040 | WindowObject *this = (WindowObject *)(self); |
| 2041 | |
| 2042 | if (this->win == INVALID_WINDOW_VALUE) |
| 2043 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2044 | vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self)); |
| 2045 | return PyUnicode_FromString(repr); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2046 | } |
| 2047 | else |
| 2048 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2049 | int i = 0; |
| 2050 | win_T *w; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2051 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2052 | for (w = firstwin; w != NULL && w != this->win; w = W_NEXT(w)) |
| 2053 | ++i; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2054 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2055 | if (w == NULL) |
| 2056 | vim_snprintf(repr, 100, _("<window object (unknown) at %p>"), |
| 2057 | (self)); |
| 2058 | else |
| 2059 | vim_snprintf(repr, 100, _("<window %d>"), i); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2060 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2061 | return PyUnicode_FromString(repr); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | /* Window list object - Definitions |
| 2066 | */ |
| 2067 | |
| 2068 | typedef struct |
| 2069 | { |
| 2070 | PyObject_HEAD |
| 2071 | } |
| 2072 | WinListObject; |
| 2073 | |
| 2074 | static PySequenceMethods WinListAsSeq = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2075 | (lenfunc) WinListLength, /* sq_length, len(x) */ |
| 2076 | (binaryfunc) 0, /* sq_concat, x+y */ |
| 2077 | (ssizeargfunc) 0, /* sq_repeat, x*n */ |
| 2078 | (ssizeargfunc) WinListItem, /* sq_item, x[i] */ |
| 2079 | 0, /* sq_slice, x[i:j] */ |
| 2080 | (ssizeobjargproc)0, /* sq_as_item, x[i]=v */ |
| 2081 | 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2082 | 0, /* sq_contains */ |
| 2083 | 0, /* sq_inplace_concat */ |
| 2084 | 0, /* sq_inplace_repeat */ |
| 2085 | }; |
| 2086 | |
| 2087 | static PyTypeObject WinListType; |
| 2088 | |
| 2089 | /* Window list object - Implementation |
| 2090 | */ |
| 2091 | /*ARGSUSED*/ |
| 2092 | static Py_ssize_t WinListLength(PyObject *self UNUSED) |
| 2093 | { |
| 2094 | win_T *w = firstwin; |
| 2095 | Py_ssize_t n = 0; |
| 2096 | |
| 2097 | while (w != NULL) |
| 2098 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2099 | ++n; |
| 2100 | w = W_NEXT(w); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | return n; |
| 2104 | } |
| 2105 | |
| 2106 | /*ARGSUSED*/ |
| 2107 | static PyObject * WinListItem(PyObject *self UNUSED, Py_ssize_t n) |
| 2108 | { |
| 2109 | win_T *w; |
| 2110 | |
| 2111 | for (w = firstwin; w != NULL; w = W_NEXT(w), --n) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2112 | if (n == 0) |
| 2113 | return WindowNew(w); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2114 | |
| 2115 | PyErr_SetString(PyExc_IndexError, _("no such window")); |
| 2116 | return NULL; |
| 2117 | } |
| 2118 | |
| 2119 | /* Current items object - Definitions |
| 2120 | */ |
| 2121 | |
| 2122 | typedef struct |
| 2123 | { |
| 2124 | PyObject_HEAD |
| 2125 | } |
| 2126 | CurrentObject; |
| 2127 | |
| 2128 | static PyTypeObject CurrentType; |
| 2129 | |
| 2130 | /* Current items object - Implementation |
| 2131 | */ |
| 2132 | /*ARGSUSED*/ |
| 2133 | static PyObject * CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj) |
| 2134 | { |
| 2135 | char *name = ""; |
| 2136 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2137 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2138 | |
| 2139 | if (strcmp(name, "buffer") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2140 | return (PyObject *)BufferNew(curbuf); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2141 | else if (strcmp(name, "window") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2142 | return (PyObject *)WindowNew(curwin); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2143 | else if (strcmp(name, "line") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2144 | return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2145 | else if (strcmp(name, "range") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2146 | return RangeNew(curbuf, RangeStart, RangeEnd); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2147 | else if (strcmp(name,"__members__") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2148 | return Py_BuildValue("[ssss]", "buffer", "window", "line", "range"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2149 | else |
| 2150 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2151 | PyErr_SetString(PyExc_AttributeError, name); |
| 2152 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | /*ARGSUSED*/ |
| 2157 | static int CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value) |
| 2158 | { |
| 2159 | char *name = ""; |
| 2160 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2161 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2162 | |
| 2163 | if (strcmp(name, "line") == 0) |
| 2164 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2165 | if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL) |
| 2166 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2167 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2168 | return 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2169 | } |
| 2170 | else |
| 2171 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2172 | PyErr_SetString(PyExc_AttributeError, name); |
| 2173 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | /* External interface |
| 2178 | */ |
| 2179 | |
| 2180 | void |
| 2181 | python3_buffer_free(buf_T *buf) |
| 2182 | { |
| 2183 | if (buf->b_python3_ref != NULL) |
| 2184 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2185 | BufferObject *bp = buf->b_python3_ref; |
| 2186 | bp->buf = INVALID_BUFFER_VALUE; |
| 2187 | buf->b_python3_ref = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | #if defined(FEAT_WINDOWS) || defined(PROTO) |
| 2192 | void |
| 2193 | python3_window_free(win_T *win) |
| 2194 | { |
| 2195 | if (win->w_python3_ref != NULL) |
| 2196 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2197 | WindowObject *wp = win->w_python3_ref; |
| 2198 | wp->win = INVALID_WINDOW_VALUE; |
| 2199 | win->w_python3_ref = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | #endif |
| 2203 | |
| 2204 | static BufListObject TheBufferList = |
| 2205 | { |
| 2206 | PyObject_HEAD_INIT(&BufListType) |
| 2207 | }; |
| 2208 | |
| 2209 | static WinListObject TheWindowList = |
| 2210 | { |
| 2211 | PyObject_HEAD_INIT(&WinListType) |
| 2212 | }; |
| 2213 | |
| 2214 | static CurrentObject TheCurrent = |
| 2215 | { |
| 2216 | PyObject_HEAD_INIT(&CurrentType) |
| 2217 | }; |
| 2218 | |
| 2219 | PyDoc_STRVAR(vim_module_doc,"vim python interface\n"); |
| 2220 | |
| 2221 | static struct PyModuleDef vimmodule; |
| 2222 | |
| 2223 | PyMODINIT_FUNC Py3Init_vim(void) |
| 2224 | { |
| 2225 | PyObject *mod; |
| 2226 | /* The special value is removed from sys.path in Python3_Init(). */ |
| 2227 | static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL}; |
| 2228 | |
| 2229 | PyType_Ready(&BufferType); |
| 2230 | PyType_Ready(&RangeType); |
| 2231 | PyType_Ready(&WindowType); |
| 2232 | PyType_Ready(&BufListType); |
| 2233 | PyType_Ready(&WinListType); |
| 2234 | PyType_Ready(&CurrentType); |
| 2235 | |
| 2236 | /* Set sys.argv[] to avoid a crash in warn(). */ |
| 2237 | PySys_SetArgv(1, argv); |
| 2238 | |
| 2239 | mod = PyModule_Create(&vimmodule); |
| 2240 | |
| 2241 | VimError = Py_BuildValue("s", "vim.error"); |
| 2242 | |
| 2243 | PyModule_AddObject(mod, "error", VimError); |
| 2244 | Py_INCREF((PyObject *)(void *)&TheBufferList); |
| 2245 | PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList); |
| 2246 | Py_INCREF((PyObject *)(void *)&TheCurrent); |
| 2247 | PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent); |
| 2248 | Py_INCREF((PyObject *)(void *)&TheWindowList); |
| 2249 | PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList); |
| 2250 | |
| 2251 | if (PyErr_Occurred()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2252 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2253 | |
| 2254 | return mod; |
| 2255 | } |
| 2256 | |
| 2257 | /************************************************************************* |
| 2258 | * 4. Utility functions for handling the interface between Vim and Python. |
| 2259 | */ |
| 2260 | |
| 2261 | |
| 2262 | /* Get a list of lines from the specified buffer. The line numbers |
| 2263 | * are in Vim format (1-based). The range is from lo up to, but not |
| 2264 | * including, hi. The list is returned as a Python list of string objects. |
| 2265 | */ |
| 2266 | static PyObject * GetBufferLineList(buf_T *buf, Py_ssize_t lo, Py_ssize_t hi) |
| 2267 | { |
| 2268 | Py_ssize_t i; |
| 2269 | Py_ssize_t n = hi - lo; |
| 2270 | PyObject *list = PyList_New(n); |
| 2271 | |
| 2272 | if (list == NULL) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2273 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2274 | |
| 2275 | for (i = 0; i < n; ++i) |
| 2276 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2277 | PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE)); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2278 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2279 | /* Error check - was the Python string creation OK? */ |
| 2280 | if (str == NULL) |
| 2281 | { |
| 2282 | Py_DECREF(list); |
| 2283 | return NULL; |
| 2284 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2285 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2286 | /* Set the list item */ |
| 2287 | if (PyList_SetItem(list, i, str)) |
| 2288 | { |
| 2289 | Py_DECREF(str); |
| 2290 | Py_DECREF(list); |
| 2291 | return NULL; |
| 2292 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | /* The ownership of the Python list is passed to the caller (ie, |
| 2296 | * the caller should Py_DECREF() the object when it is finished |
| 2297 | * with it). |
| 2298 | */ |
| 2299 | |
| 2300 | return list; |
| 2301 | } |
| 2302 | |
| 2303 | /* Get a line from the specified buffer. The line number is |
| 2304 | * in Vim format (1-based). The line is returned as a Python |
| 2305 | * string object. |
| 2306 | */ |
| 2307 | static PyObject * GetBufferLine(buf_T *buf, Py_ssize_t n) |
| 2308 | { |
| 2309 | return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE)); |
| 2310 | } |
| 2311 | |
| 2312 | /* |
| 2313 | * Check if deleting lines made the cursor position invalid. |
| 2314 | * Changed the lines from "lo" to "hi" and added "extra" lines (negative if |
| 2315 | * deleted). |
| 2316 | */ |
| 2317 | static void py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) |
| 2318 | { |
| 2319 | if (curwin->w_cursor.lnum >= lo) |
| 2320 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2321 | /* Adjust the cursor position if it's in/after the changed |
| 2322 | * lines. */ |
| 2323 | if (curwin->w_cursor.lnum >= hi) |
| 2324 | { |
| 2325 | curwin->w_cursor.lnum += extra; |
| 2326 | check_cursor_col(); |
| 2327 | } |
| 2328 | else if (extra < 0) |
| 2329 | { |
| 2330 | curwin->w_cursor.lnum = lo; |
| 2331 | check_cursor(); |
| 2332 | } |
| 2333 | else |
| 2334 | check_cursor_col(); |
| 2335 | changed_cline_bef_curs(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2336 | } |
| 2337 | invalidate_botline(); |
| 2338 | } |
| 2339 | |
| 2340 | /* Replace a line in the specified buffer. The line number is |
| 2341 | * in Vim format (1-based). The replacement line is given as |
| 2342 | * a Python string object. The object is checked for validity |
| 2343 | * and correct format. Errors are returned as a value of FAIL. |
| 2344 | * The return value is OK on success. |
| 2345 | * If OK is returned and len_change is not NULL, *len_change |
| 2346 | * is set to the change in the buffer length. |
| 2347 | */ |
| 2348 | static int SetBufferLine(buf_T *buf, Py_ssize_t n, PyObject *line, Py_ssize_t *len_change) |
| 2349 | { |
| 2350 | /* First of all, we check the thpe of the supplied Python object. |
| 2351 | * There are three cases: |
| 2352 | * 1. NULL, or None - this is a deletion. |
| 2353 | * 2. A string - this is a replacement. |
| 2354 | * 3. Anything else - this is an error. |
| 2355 | */ |
| 2356 | if (line == Py_None || line == NULL) |
| 2357 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2358 | buf_T *savebuf = curbuf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2359 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2360 | PyErr_Clear(); |
| 2361 | curbuf = buf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2362 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2363 | if (u_savedel((linenr_T)n, 1L) == FAIL) |
| 2364 | PyErr_SetVim(_("cannot save undo information")); |
| 2365 | else if (ml_delete((linenr_T)n, FALSE) == FAIL) |
| 2366 | PyErr_SetVim(_("cannot delete line")); |
| 2367 | else |
| 2368 | { |
| 2369 | deleted_lines_mark((linenr_T)n, 1L); |
| 2370 | if (buf == curwin->w_buffer) |
| 2371 | py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); |
| 2372 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2373 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2374 | curbuf = savebuf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2375 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2376 | if (PyErr_Occurred() || VimErrorCheck()) |
| 2377 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2378 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2379 | if (len_change) |
| 2380 | *len_change = -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2381 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2382 | return OK; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2383 | } |
| 2384 | else if (PyUnicode_Check(line)) |
| 2385 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2386 | char *save = StringToLine(line); |
| 2387 | buf_T *savebuf = curbuf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2388 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2389 | if (save == NULL) |
| 2390 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2391 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2392 | /* We do not need to free "save" if ml_replace() consumes it. */ |
| 2393 | PyErr_Clear(); |
| 2394 | curbuf = buf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2395 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2396 | if (u_savesub((linenr_T)n) == FAIL) |
| 2397 | { |
| 2398 | PyErr_SetVim(_("cannot save undo information")); |
| 2399 | vim_free(save); |
| 2400 | } |
| 2401 | else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL) |
| 2402 | { |
| 2403 | PyErr_SetVim(_("cannot replace line")); |
| 2404 | vim_free(save); |
| 2405 | } |
| 2406 | else |
| 2407 | changed_bytes((linenr_T)n, 0); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2408 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2409 | curbuf = savebuf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2410 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2411 | /* Check that the cursor is not beyond the end of the line now. */ |
| 2412 | if (buf == curwin->w_buffer) |
| 2413 | check_cursor_col(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2414 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2415 | if (PyErr_Occurred() || VimErrorCheck()) |
| 2416 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2417 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2418 | if (len_change) |
| 2419 | *len_change = 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2420 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2421 | return OK; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2422 | } |
| 2423 | else |
| 2424 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2425 | PyErr_BadArgument(); |
| 2426 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | /* Insert a number of lines into the specified buffer after the specifed line. |
| 2431 | * The line number is in Vim format (1-based). The lines to be inserted are |
| 2432 | * given as a Python list of string objects or as a single string. The lines |
| 2433 | * to be added are checked for validity and correct format. Errors are |
| 2434 | * returned as a value of FAIL. The return value is OK on success. |
| 2435 | * If OK is returned and len_change is not NULL, *len_change |
| 2436 | * is set to the change in the buffer length. |
| 2437 | */ |
| 2438 | static int InsertBufferLines(buf_T *buf, Py_ssize_t n, PyObject *lines, Py_ssize_t *len_change) |
| 2439 | { |
| 2440 | /* First of all, we check the type of the supplied Python object. |
| 2441 | * It must be a string or a list, or the call is in error. |
| 2442 | */ |
| 2443 | if (PyUnicode_Check(lines)) |
| 2444 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2445 | char *str = StringToLine(lines); |
| 2446 | buf_T *savebuf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2447 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2448 | if (str == NULL) |
| 2449 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2450 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2451 | savebuf = curbuf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2452 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2453 | PyErr_Clear(); |
| 2454 | curbuf = buf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2455 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2456 | if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL) |
| 2457 | PyErr_SetVim(_("cannot save undo information")); |
| 2458 | else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) |
| 2459 | PyErr_SetVim(_("cannot insert line")); |
| 2460 | else |
| 2461 | appended_lines_mark((linenr_T)n, 1L); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2462 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2463 | vim_free(str); |
| 2464 | curbuf = savebuf; |
| 2465 | update_screen(VALID); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2466 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2467 | if (PyErr_Occurred() || VimErrorCheck()) |
| 2468 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2469 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2470 | if (len_change) |
| 2471 | *len_change = 1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2472 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2473 | return OK; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2474 | } |
| 2475 | else if (PyList_Check(lines)) |
| 2476 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2477 | Py_ssize_t i; |
| 2478 | Py_ssize_t size = PyList_Size(lines); |
| 2479 | char **array; |
| 2480 | buf_T *savebuf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2481 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2482 | array = (char **)alloc((unsigned)(size * sizeof(char *))); |
| 2483 | if (array == NULL) |
| 2484 | { |
| 2485 | PyErr_NoMemory(); |
| 2486 | return FAIL; |
| 2487 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2488 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2489 | for (i = 0; i < size; ++i) |
| 2490 | { |
| 2491 | PyObject *line = PyList_GetItem(lines, i); |
| 2492 | array[i] = StringToLine(line); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2493 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2494 | if (array[i] == NULL) |
| 2495 | { |
| 2496 | while (i) |
| 2497 | vim_free(array[--i]); |
| 2498 | vim_free(array); |
| 2499 | return FAIL; |
| 2500 | } |
| 2501 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2502 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2503 | savebuf = curbuf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2504 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2505 | PyErr_Clear(); |
| 2506 | curbuf = buf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2507 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2508 | if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) |
| 2509 | PyErr_SetVim(_("cannot save undo information")); |
| 2510 | else |
| 2511 | { |
| 2512 | for (i = 0; i < size; ++i) |
| 2513 | { |
| 2514 | if (ml_append((linenr_T)(n + i), |
| 2515 | (char_u *)array[i], 0, FALSE) == FAIL) |
| 2516 | { |
| 2517 | PyErr_SetVim(_("cannot insert line")); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2518 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2519 | /* Free the rest of the lines */ |
| 2520 | while (i < size) |
| 2521 | vim_free(array[i++]); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2522 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2523 | break; |
| 2524 | } |
| 2525 | vim_free(array[i]); |
| 2526 | } |
| 2527 | if (i > 0) |
| 2528 | appended_lines_mark((linenr_T)n, (long)i); |
| 2529 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2530 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2531 | /* Free the array of lines. All of its contents have now |
| 2532 | * been freed. |
| 2533 | */ |
| 2534 | vim_free(array); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2535 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2536 | curbuf = savebuf; |
| 2537 | update_screen(VALID); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2538 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2539 | if (PyErr_Occurred() || VimErrorCheck()) |
| 2540 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2541 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2542 | if (len_change) |
| 2543 | *len_change = size; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2544 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2545 | return OK; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2546 | } |
| 2547 | else |
| 2548 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2549 | PyErr_BadArgument(); |
| 2550 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2551 | } |
| 2552 | } |
| 2553 | |
| 2554 | /* Convert a Vim line into a Python string. |
| 2555 | * All internal newlines are replaced by null characters. |
| 2556 | * |
| 2557 | * On errors, the Python exception data is set, and NULL is returned. |
| 2558 | */ |
| 2559 | static PyObject * LineToString(const char *str) |
| 2560 | { |
| 2561 | PyObject *result; |
| 2562 | Py_ssize_t len = strlen(str); |
| 2563 | char *tmp,*p; |
| 2564 | |
| 2565 | tmp = (char *)alloc((unsigned)(len+1)); |
| 2566 | p = tmp; |
| 2567 | if (p == NULL) |
| 2568 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2569 | PyErr_NoMemory(); |
| 2570 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | while (*str) |
| 2574 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2575 | if (*str == '\n') |
| 2576 | *p = '\0'; |
| 2577 | else |
| 2578 | *p = *str; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2579 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2580 | ++p; |
| 2581 | ++str; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2582 | } |
| 2583 | *p = '\0'; |
| 2584 | |
| 2585 | result = PyUnicode_FromStringAndSize(tmp, len); |
| 2586 | |
| 2587 | vim_free(tmp); |
| 2588 | return result; |
| 2589 | } |
| 2590 | |
| 2591 | /* Convert a Python string into a Vim line. |
| 2592 | * |
| 2593 | * The result is in allocated memory. All internal nulls are replaced by |
| 2594 | * newline characters. It is an error for the string to contain newline |
| 2595 | * characters. |
| 2596 | * |
| 2597 | * On errors, the Python exception data is set, and NULL is returned. |
| 2598 | */ |
| 2599 | static char * StringToLine(PyObject *obj) |
| 2600 | { |
| 2601 | const char *str; |
| 2602 | char *save; |
| 2603 | Py_ssize_t len; |
| 2604 | Py_ssize_t i; |
| 2605 | char *p; |
| 2606 | |
| 2607 | if (obj == NULL || !PyUnicode_Check(obj)) |
| 2608 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2609 | PyErr_BadArgument(); |
| 2610 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | str = _PyUnicode_AsString(obj); |
| 2614 | len = PyUnicode_GET_SIZE(obj); |
| 2615 | |
| 2616 | /* |
| 2617 | * Error checking: String must not contain newlines, as we |
| 2618 | * are replacing a single line, and we must replace it with |
| 2619 | * a single line. |
| 2620 | * A trailing newline is removed, so that append(f.readlines()) works. |
| 2621 | */ |
| 2622 | p = memchr(str, '\n', len); |
| 2623 | if (p != NULL) |
| 2624 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2625 | if (p == str + len - 1) |
| 2626 | --len; |
| 2627 | else |
| 2628 | { |
| 2629 | PyErr_SetVim(_("string cannot contain newlines")); |
| 2630 | return NULL; |
| 2631 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | /* Create a copy of the string, with internal nulls replaced by |
| 2635 | * newline characters, as is the vim convention. |
| 2636 | */ |
| 2637 | save = (char *)alloc((unsigned)(len+1)); |
| 2638 | if (save == NULL) |
| 2639 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2640 | PyErr_NoMemory(); |
| 2641 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | for (i = 0; i < len; ++i) |
| 2645 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2646 | if (str[i] == '\0') |
| 2647 | save[i] = '\n'; |
| 2648 | else |
| 2649 | save[i] = str[i]; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | save[i] = '\0'; |
| 2653 | |
| 2654 | return save; |
| 2655 | } |
| 2656 | |
| 2657 | /* Check to see whether a Vim error has been reported, or a keyboard |
| 2658 | * interrupt has been detected. |
| 2659 | */ |
| 2660 | static int VimErrorCheck(void) |
| 2661 | { |
| 2662 | if (got_int) |
| 2663 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2664 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 2665 | return 1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2666 | } |
| 2667 | else if (did_emsg && !PyErr_Occurred()) |
| 2668 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2669 | PyErr_SetNone(VimError); |
| 2670 | return 1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | return 0; |
| 2674 | } |
| 2675 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2676 | static void init_structs(void) |
| 2677 | { |
| 2678 | vim_memset(&OutputType, 0, sizeof(OutputType)); |
| 2679 | OutputType.tp_name = "vim.message"; |
| 2680 | OutputType.tp_basicsize = sizeof(OutputObject); |
| 2681 | OutputType.tp_getattro = OutputGetattro; |
| 2682 | OutputType.tp_setattro = OutputSetattro; |
| 2683 | OutputType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 2684 | OutputType.tp_doc = "vim message object"; |
| 2685 | OutputType.tp_methods = OutputMethods; |
| 2686 | OutputType.tp_alloc = call_PyType_GenericAlloc; |
| 2687 | OutputType.tp_new = call_PyType_GenericNew; |
| 2688 | OutputType.tp_free = call_PyObject_Free; |
| 2689 | |
| 2690 | vim_memset(&BufferType, 0, sizeof(BufferType)); |
| 2691 | BufferType.tp_name = "vim.buffer"; |
| 2692 | BufferType.tp_basicsize = sizeof(BufferType); |
| 2693 | BufferType.tp_dealloc = BufferDestructor; |
| 2694 | BufferType.tp_repr = BufferRepr; |
| 2695 | BufferType.tp_as_sequence = &BufferAsSeq; |
| 2696 | BufferType.tp_as_mapping = &BufferAsMapping; |
| 2697 | BufferType.tp_getattro = BufferGetattro; |
| 2698 | BufferType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 2699 | BufferType.tp_doc = "vim buffer object"; |
| 2700 | BufferType.tp_methods = BufferMethods; |
| 2701 | BufferType.tp_alloc = call_PyType_GenericAlloc; |
| 2702 | BufferType.tp_new = call_PyType_GenericNew; |
| 2703 | BufferType.tp_free = call_PyObject_Free; |
| 2704 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2705 | vim_memset(&WindowType, 0, sizeof(WindowType)); |
| 2706 | WindowType.tp_name = "vim.window"; |
| 2707 | WindowType.tp_basicsize = sizeof(WindowObject); |
| 2708 | WindowType.tp_dealloc = WindowDestructor; |
| 2709 | WindowType.tp_repr = WindowRepr; |
| 2710 | WindowType.tp_getattro = WindowGetattro; |
| 2711 | WindowType.tp_setattro = WindowSetattro; |
| 2712 | WindowType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 2713 | WindowType.tp_doc = "vim Window object"; |
| 2714 | WindowType.tp_methods = WindowMethods; |
| 2715 | WindowType.tp_alloc = call_PyType_GenericAlloc; |
| 2716 | WindowType.tp_new = call_PyType_GenericNew; |
| 2717 | WindowType.tp_free = call_PyObject_Free; |
| 2718 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2719 | vim_memset(&BufListType, 0, sizeof(BufListType)); |
| 2720 | BufListType.tp_name = "vim.bufferlist"; |
| 2721 | BufListType.tp_basicsize = sizeof(BufListObject); |
| 2722 | BufListType.tp_as_sequence = &BufListAsSeq; |
| 2723 | BufListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 2724 | BufferType.tp_doc = "vim buffer list"; |
| 2725 | |
| 2726 | vim_memset(&WinListType, 0, sizeof(WinListType)); |
| 2727 | WinListType.tp_name = "vim.windowlist"; |
| 2728 | WinListType.tp_basicsize = sizeof(WinListType); |
| 2729 | WinListType.tp_as_sequence = &WinListAsSeq; |
| 2730 | WinListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 2731 | WinListType.tp_doc = "vim window list"; |
| 2732 | |
| 2733 | vim_memset(&RangeType, 0, sizeof(RangeType)); |
| 2734 | RangeType.tp_name = "vim.range"; |
| 2735 | RangeType.tp_basicsize = sizeof(RangeObject); |
| 2736 | RangeType.tp_dealloc = RangeDestructor; |
| 2737 | RangeType.tp_repr = RangeRepr; |
| 2738 | RangeType.tp_as_sequence = &RangeAsSeq; |
| 2739 | RangeType.tp_as_mapping = &RangeAsMapping; |
| 2740 | RangeType.tp_getattro = RangeGetattro; |
| 2741 | RangeType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 2742 | RangeType.tp_doc = "vim Range object"; |
| 2743 | RangeType.tp_methods = RangeMethods; |
| 2744 | RangeType.tp_alloc = call_PyType_GenericAlloc; |
| 2745 | RangeType.tp_new = call_PyType_GenericNew; |
| 2746 | RangeType.tp_free = call_PyObject_Free; |
| 2747 | |
| 2748 | vim_memset(&CurrentType, 0, sizeof(CurrentType)); |
| 2749 | CurrentType.tp_name = "vim.currentdata"; |
| 2750 | CurrentType.tp_basicsize = sizeof(CurrentObject); |
| 2751 | CurrentType.tp_getattro = CurrentGetattro; |
| 2752 | CurrentType.tp_setattro = CurrentSetattro; |
| 2753 | CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 2754 | CurrentType.tp_doc = "vim current object"; |
| 2755 | |
| 2756 | vim_memset(&vimmodule, 0, sizeof(vimmodule)); |
| 2757 | vimmodule.m_name = "vim"; |
| 2758 | vimmodule.m_doc = vim_module_doc; |
| 2759 | vimmodule.m_size = -1; |
| 2760 | vimmodule.m_methods = VimMethods; |
| 2761 | } |