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