Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 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 | /* |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 10 | * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay |
| 11 | * Pavlov. |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 12 | * |
| 13 | * Common code for if_python.c and if_python3.c. |
| 14 | */ |
| 15 | |
Bram Moolenaar | c1a995d | 2012-08-08 16:05:07 +0200 | [diff] [blame] | 16 | #if PY_VERSION_HEX < 0x02050000 |
| 17 | typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */ |
| 18 | #endif |
| 19 | |
Bram Moolenaar | 91805fc | 2011-06-26 04:01:44 +0200 | [diff] [blame] | 20 | #ifdef FEAT_MBYTE |
| 21 | # define ENC_OPT p_enc |
| 22 | #else |
| 23 | # define ENC_OPT "latin1" |
| 24 | #endif |
Bram Moolenaar | d620aa9 | 2013-05-17 16:40:06 +0200 | [diff] [blame] | 25 | #define DOPY_FUNC "_vim_pydo" |
Bram Moolenaar | 91805fc | 2011-06-26 04:01:44 +0200 | [diff] [blame] | 26 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 27 | #define PyErr_SetVim(str) PyErr_SetString(VimError, str) |
| 28 | |
| 29 | #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) |
| 30 | #define INVALID_WINDOW_VALUE ((win_T *)(-1)) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 31 | #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1)) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 32 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 33 | #define DICTKEY_DECL \ |
| 34 | PyObject *dictkey_todecref; |
| 35 | #define DICTKEY_GET(err) \ |
| 36 | if (!(key = StringToChars(keyObject, &dictkey_todecref))) \ |
| 37 | return err; |
| 38 | #define DICTKEY_UNREF \ |
| 39 | Py_XDECREF(dictkey_todecref); |
| 40 | |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 41 | typedef void (*rangeinitializer)(void *); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 42 | typedef void (*runner)(const char *, void * |
| 43 | #ifdef PY_CAN_RECURSE |
| 44 | , PyGILState_STATE * |
| 45 | #endif |
| 46 | ); |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 47 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 48 | static int ConvertFromPyObject(PyObject *, typval_T *); |
| 49 | static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *); |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 50 | static PyObject *WindowNew(win_T *, tabpage_T *); |
| 51 | static PyObject *BufferNew (buf_T *); |
| 52 | static PyObject *LineToString(const char *); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 53 | |
| 54 | static PyInt RangeStart; |
| 55 | static PyInt RangeEnd; |
| 56 | |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 57 | static PyObject *globals; |
| 58 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 59 | /* |
| 60 | * obtain a lock on the Vim data structures |
| 61 | */ |
| 62 | static void |
| 63 | Python_Lock_Vim(void) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * release a lock on the Vim data structures |
| 69 | */ |
| 70 | static void |
| 71 | Python_Release_Vim(void) |
| 72 | { |
| 73 | } |
| 74 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 75 | /* |
| 76 | * The "todecref" argument holds a pointer to PyObject * that must be |
| 77 | * DECREF'ed after returned char_u * is no longer needed or NULL if all what |
| 78 | * was needed to generate returned value is object. |
| 79 | * |
| 80 | * Use Py_XDECREF to decrement reference count. |
| 81 | */ |
| 82 | static char_u * |
| 83 | StringToChars(PyObject *object, PyObject **todecref) |
| 84 | { |
| 85 | char_u *p; |
| 86 | PyObject *bytes = NULL; |
| 87 | |
| 88 | if (PyBytes_Check(object)) |
| 89 | { |
| 90 | |
| 91 | if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1) |
| 92 | return NULL; |
| 93 | if (p == NULL) |
| 94 | return NULL; |
| 95 | |
| 96 | *todecref = NULL; |
| 97 | } |
| 98 | else if (PyUnicode_Check(object)) |
| 99 | { |
| 100 | bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL); |
| 101 | if (bytes == NULL) |
| 102 | return NULL; |
| 103 | |
| 104 | if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1) |
| 105 | return NULL; |
| 106 | if (p == NULL) |
| 107 | return NULL; |
| 108 | |
| 109 | *todecref = bytes; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | PyErr_SetString(PyExc_TypeError, _("object must be string")); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | return (char_u *) p; |
| 118 | } |
| 119 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 120 | /* Output buffer management |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 121 | */ |
| 122 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 123 | /* Function to write a line, points to either msg() or emsg(). */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 124 | typedef void (*writefn)(char_u *); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 125 | |
| 126 | static PyTypeObject OutputType; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 127 | |
| 128 | typedef struct |
| 129 | { |
| 130 | PyObject_HEAD |
| 131 | long softspace; |
| 132 | long error; |
| 133 | } OutputObject; |
| 134 | |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 135 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 136 | OutputSetattr(OutputObject *self, char *name, PyObject *val) |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 137 | { |
| 138 | if (val == NULL) |
| 139 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 140 | PyErr_SetString(PyExc_AttributeError, |
| 141 | _("can't delete OutputObject attributes")); |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | if (strcmp(name, "softspace") == 0) |
| 146 | { |
| 147 | if (!PyInt_Check(val)) |
| 148 | { |
| 149 | PyErr_SetString(PyExc_TypeError, _("softspace must be an integer")); |
| 150 | return -1; |
| 151 | } |
| 152 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 153 | self->softspace = PyInt_AsLong(val); |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); |
| 158 | return -1; |
| 159 | } |
| 160 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 161 | /* Buffer IO, we write one whole line at a time. */ |
| 162 | static garray_T io_ga = {0, 0, 1, 80, NULL}; |
| 163 | static writefn old_fn = NULL; |
| 164 | |
| 165 | static void |
| 166 | PythonIO_Flush(void) |
| 167 | { |
| 168 | if (old_fn != NULL && io_ga.ga_len > 0) |
| 169 | { |
| 170 | ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL; |
| 171 | old_fn((char_u *)io_ga.ga_data); |
| 172 | } |
| 173 | io_ga.ga_len = 0; |
| 174 | } |
| 175 | |
| 176 | static void |
| 177 | writer(writefn fn, char_u *str, PyInt n) |
| 178 | { |
| 179 | char_u *ptr; |
| 180 | |
| 181 | /* Flush when switching output function. */ |
| 182 | if (fn != old_fn) |
| 183 | PythonIO_Flush(); |
| 184 | old_fn = fn; |
| 185 | |
| 186 | /* Write each NL separated line. Text after the last NL is kept for |
| 187 | * writing later. */ |
| 188 | while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) |
| 189 | { |
| 190 | PyInt len = ptr - str; |
| 191 | |
| 192 | if (ga_grow(&io_ga, (int)(len + 1)) == FAIL) |
| 193 | break; |
| 194 | |
| 195 | mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len); |
| 196 | ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL; |
| 197 | fn((char_u *)io_ga.ga_data); |
| 198 | str = ptr + 1; |
| 199 | n -= len + 1; |
| 200 | io_ga.ga_len = 0; |
| 201 | } |
| 202 | |
| 203 | /* Put the remaining text into io_ga for later printing. */ |
| 204 | if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK) |
| 205 | { |
| 206 | mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n); |
| 207 | io_ga.ga_len += (int)n; |
| 208 | } |
| 209 | } |
| 210 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 211 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 212 | OutputWrite(OutputObject *self, PyObject *args) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 213 | { |
Bram Moolenaar | e8cdcef | 2012-09-12 20:21:43 +0200 | [diff] [blame] | 214 | Py_ssize_t len = 0; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 215 | char *str = NULL; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 216 | int error = self->error; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 217 | |
Bram Moolenaar | 2756480 | 2011-09-07 19:30:21 +0200 | [diff] [blame] | 218 | if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len)) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 219 | return NULL; |
| 220 | |
| 221 | Py_BEGIN_ALLOW_THREADS |
| 222 | Python_Lock_Vim(); |
| 223 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 224 | Python_Release_Vim(); |
| 225 | Py_END_ALLOW_THREADS |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 226 | PyMem_Free(str); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 227 | |
| 228 | Py_INCREF(Py_None); |
| 229 | return Py_None; |
| 230 | } |
| 231 | |
| 232 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 233 | OutputWritelines(OutputObject *self, PyObject *args) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 234 | { |
| 235 | PyInt n; |
| 236 | PyInt i; |
| 237 | PyObject *list; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 238 | int error = self->error; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 239 | |
| 240 | if (!PyArg_ParseTuple(args, "O", &list)) |
| 241 | return NULL; |
| 242 | Py_INCREF(list); |
| 243 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 244 | if (!PyList_Check(list)) |
| 245 | { |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 246 | PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
| 247 | Py_DECREF(list); |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | n = PyList_Size(list); |
| 252 | |
| 253 | for (i = 0; i < n; ++i) |
| 254 | { |
| 255 | PyObject *line = PyList_GetItem(list, i); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 256 | char *str = NULL; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 257 | PyInt len; |
| 258 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 259 | if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len)) |
| 260 | { |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 261 | PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
| 262 | Py_DECREF(list); |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | Py_BEGIN_ALLOW_THREADS |
| 267 | Python_Lock_Vim(); |
| 268 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 269 | Python_Release_Vim(); |
| 270 | Py_END_ALLOW_THREADS |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 271 | PyMem_Free(str); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | Py_DECREF(list); |
| 275 | Py_INCREF(Py_None); |
| 276 | return Py_None; |
| 277 | } |
| 278 | |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 279 | static PyObject * |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 280 | OutputFlush(PyObject *self UNUSED) |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 281 | { |
| 282 | /* do nothing */ |
| 283 | Py_INCREF(Py_None); |
| 284 | return Py_None; |
| 285 | } |
| 286 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 287 | /***************/ |
| 288 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 289 | static struct PyMethodDef OutputMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 290 | /* name, function, calling, doc */ |
| 291 | {"write", (PyCFunction)OutputWrite, METH_VARARGS, ""}, |
| 292 | {"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""}, |
| 293 | {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""}, |
| 294 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 295 | }; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 296 | |
| 297 | static OutputObject Output = |
| 298 | { |
| 299 | PyObject_HEAD_INIT(&OutputType) |
| 300 | 0, |
| 301 | 0 |
| 302 | }; |
| 303 | |
| 304 | static OutputObject Error = |
| 305 | { |
| 306 | PyObject_HEAD_INIT(&OutputType) |
| 307 | 0, |
| 308 | 1 |
| 309 | }; |
| 310 | |
| 311 | static int |
| 312 | PythonIO_Init_io(void) |
| 313 | { |
| 314 | PySys_SetObject("stdout", (PyObject *)(void *)&Output); |
| 315 | PySys_SetObject("stderr", (PyObject *)(void *)&Error); |
| 316 | |
| 317 | if (PyErr_Occurred()) |
| 318 | { |
| 319 | EMSG(_("E264: Python: Error initialising I/O objects")); |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | static PyObject *VimError; |
| 328 | |
| 329 | /* Check to see whether a Vim error has been reported, or a keyboard |
| 330 | * interrupt has been detected. |
| 331 | */ |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 332 | |
| 333 | static void |
| 334 | VimTryStart(void) |
| 335 | { |
| 336 | ++trylevel; |
| 337 | } |
| 338 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 339 | static int |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 340 | VimTryEnd(void) |
| 341 | { |
| 342 | --trylevel; |
| 343 | if (got_int) |
| 344 | { |
| 345 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 346 | return 1; |
| 347 | } |
| 348 | else if (!did_throw) |
| 349 | return 0; |
| 350 | else if (PyErr_Occurred()) |
| 351 | return 1; |
| 352 | else |
| 353 | { |
| 354 | PyErr_SetVim((char *) current_exception->value); |
| 355 | discard_current_exception(); |
| 356 | return 1; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | static int |
| 361 | VimCheckInterrupt(void) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 362 | { |
| 363 | if (got_int) |
| 364 | { |
| 365 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 366 | return 1; |
| 367 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 368 | return 0; |
| 369 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 370 | |
| 371 | /* Vim module - Implementation |
| 372 | */ |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 373 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 374 | static PyObject * |
| 375 | VimCommand(PyObject *self UNUSED, PyObject *args) |
| 376 | { |
| 377 | char *cmd; |
| 378 | PyObject *result; |
| 379 | |
| 380 | if (!PyArg_ParseTuple(args, "s", &cmd)) |
| 381 | return NULL; |
| 382 | |
| 383 | PyErr_Clear(); |
| 384 | |
| 385 | Py_BEGIN_ALLOW_THREADS |
| 386 | Python_Lock_Vim(); |
| 387 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 388 | VimTryStart(); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 389 | do_cmdline_cmd((char_u *)cmd); |
| 390 | update_screen(VALID); |
| 391 | |
| 392 | Python_Release_Vim(); |
| 393 | Py_END_ALLOW_THREADS |
| 394 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 395 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 396 | result = NULL; |
| 397 | else |
| 398 | result = Py_None; |
| 399 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 400 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 401 | Py_XINCREF(result); |
| 402 | return result; |
| 403 | } |
| 404 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 405 | /* |
| 406 | * Function to translate a typval_T into a PyObject; this will recursively |
| 407 | * translate lists/dictionaries into their Python equivalents. |
| 408 | * |
| 409 | * The depth parameter is to avoid infinite recursion, set it to 1 when |
| 410 | * you call VimToPython. |
| 411 | */ |
| 412 | static PyObject * |
| 413 | VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) |
| 414 | { |
| 415 | PyObject *result; |
| 416 | PyObject *newObj; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 417 | char ptrBuf[sizeof(void *) * 2 + 3]; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 418 | |
| 419 | /* Avoid infinite recursion */ |
| 420 | if (depth > 100) |
| 421 | { |
| 422 | Py_INCREF(Py_None); |
| 423 | result = Py_None; |
| 424 | return result; |
| 425 | } |
| 426 | |
| 427 | /* Check if we run into a recursive loop. The item must be in lookupDict |
| 428 | * then and we can use it again. */ |
| 429 | if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) |
| 430 | || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL)) |
| 431 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 432 | sprintf(ptrBuf, "%p", |
| 433 | our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list |
| 434 | : (void *)our_tv->vval.v_dict); |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 435 | |
| 436 | if ((result = PyDict_GetItemString(lookupDict, ptrBuf))) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 437 | { |
| 438 | Py_INCREF(result); |
| 439 | return result; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | if (our_tv->v_type == VAR_STRING) |
| 444 | { |
Bram Moolenaar | d1f13fd | 2012-10-05 21:30:07 +0200 | [diff] [blame] | 445 | result = Py_BuildValue("s", our_tv->vval.v_string == NULL |
| 446 | ? "" : (char *)our_tv->vval.v_string); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 447 | } |
| 448 | else if (our_tv->v_type == VAR_NUMBER) |
| 449 | { |
| 450 | char buf[NUMBUFLEN]; |
| 451 | |
| 452 | /* For backwards compatibility numbers are stored as strings. */ |
| 453 | sprintf(buf, "%ld", (long)our_tv->vval.v_number); |
| 454 | result = Py_BuildValue("s", buf); |
| 455 | } |
| 456 | # ifdef FEAT_FLOAT |
| 457 | else if (our_tv->v_type == VAR_FLOAT) |
| 458 | { |
| 459 | char buf[NUMBUFLEN]; |
| 460 | |
| 461 | sprintf(buf, "%f", our_tv->vval.v_float); |
| 462 | result = Py_BuildValue("s", buf); |
| 463 | } |
| 464 | # endif |
| 465 | else if (our_tv->v_type == VAR_LIST) |
| 466 | { |
| 467 | list_T *list = our_tv->vval.v_list; |
| 468 | listitem_T *curr; |
| 469 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 470 | if (list == NULL) |
| 471 | return NULL; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 472 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 473 | if (!(result = PyList_New(0))) |
| 474 | return NULL; |
| 475 | |
| 476 | if (PyDict_SetItemString(lookupDict, ptrBuf, result)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 477 | { |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 478 | Py_DECREF(result); |
| 479 | return NULL; |
| 480 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 481 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 482 | for (curr = list->lv_first; curr != NULL; curr = curr->li_next) |
| 483 | { |
| 484 | if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict))) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 485 | { |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 486 | Py_DECREF(result); |
| 487 | return NULL; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 488 | } |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 489 | if (PyList_Append(result, newObj)) |
| 490 | { |
| 491 | Py_DECREF(newObj); |
| 492 | Py_DECREF(result); |
| 493 | return NULL; |
| 494 | } |
| 495 | Py_DECREF(newObj); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | else if (our_tv->v_type == VAR_DICT) |
| 499 | { |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 500 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 501 | hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab; |
| 502 | long_u todo = ht->ht_used; |
| 503 | hashitem_T *hi; |
| 504 | dictitem_T *di; |
| 505 | if (our_tv->vval.v_dict == NULL) |
| 506 | return NULL; |
| 507 | |
| 508 | if (!(result = PyDict_New())) |
| 509 | return NULL; |
| 510 | |
| 511 | if (PyDict_SetItemString(lookupDict, ptrBuf, result)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 512 | { |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 513 | Py_DECREF(result); |
| 514 | return NULL; |
| 515 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 516 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 517 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 518 | { |
| 519 | if (!HASHITEM_EMPTY(hi)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 520 | { |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 521 | --todo; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 522 | |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 523 | di = dict_lookup(hi); |
| 524 | if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookupDict))) |
| 525 | { |
| 526 | Py_DECREF(result); |
| 527 | return NULL; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 528 | } |
Bram Moolenaar | 21642ed | 2013-05-29 22:20:01 +0200 | [diff] [blame^] | 529 | if (PyDict_SetItemString(result, (char *)hi->hi_key, newObj)) |
| 530 | { |
| 531 | Py_DECREF(result); |
| 532 | Py_DECREF(newObj); |
| 533 | return NULL; |
| 534 | } |
| 535 | Py_DECREF(newObj); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | Py_INCREF(Py_None); |
| 542 | result = Py_None; |
| 543 | } |
| 544 | |
| 545 | return result; |
| 546 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 547 | |
| 548 | static PyObject * |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 549 | VimEval(PyObject *self UNUSED, PyObject *args) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 550 | { |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 551 | char *expr; |
| 552 | typval_T *our_tv; |
| 553 | PyObject *result; |
| 554 | PyObject *lookup_dict; |
| 555 | |
| 556 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 557 | return NULL; |
| 558 | |
| 559 | Py_BEGIN_ALLOW_THREADS |
| 560 | Python_Lock_Vim(); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 561 | VimTryStart(); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 562 | our_tv = eval_expr((char_u *)expr, NULL); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 563 | Python_Release_Vim(); |
| 564 | Py_END_ALLOW_THREADS |
| 565 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 566 | if (VimTryEnd()) |
| 567 | return NULL; |
| 568 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 569 | if (our_tv == NULL) |
| 570 | { |
| 571 | PyErr_SetVim(_("invalid expression")); |
| 572 | return NULL; |
| 573 | } |
| 574 | |
| 575 | /* Convert the Vim type into a Python type. Create a dictionary that's |
| 576 | * used to check for recursive loops. */ |
| 577 | lookup_dict = PyDict_New(); |
| 578 | result = VimToPython(our_tv, 1, lookup_dict); |
| 579 | Py_DECREF(lookup_dict); |
| 580 | |
| 581 | |
| 582 | Py_BEGIN_ALLOW_THREADS |
| 583 | Python_Lock_Vim(); |
| 584 | free_tv(our_tv); |
| 585 | Python_Release_Vim(); |
| 586 | Py_END_ALLOW_THREADS |
| 587 | |
| 588 | return result; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 589 | } |
| 590 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 591 | static PyObject *ConvertToPyObject(typval_T *); |
| 592 | |
| 593 | static PyObject * |
Bram Moolenaar | 9e74e30 | 2013-05-17 21:20:17 +0200 | [diff] [blame] | 594 | VimEvalPy(PyObject *self UNUSED, PyObject *args) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 595 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 596 | char *expr; |
| 597 | typval_T *our_tv; |
| 598 | PyObject *result; |
| 599 | |
| 600 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 601 | return NULL; |
| 602 | |
| 603 | Py_BEGIN_ALLOW_THREADS |
| 604 | Python_Lock_Vim(); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 605 | VimTryStart(); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 606 | our_tv = eval_expr((char_u *)expr, NULL); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 607 | Python_Release_Vim(); |
| 608 | Py_END_ALLOW_THREADS |
| 609 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 610 | if (VimTryEnd()) |
| 611 | return NULL; |
| 612 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 613 | if (our_tv == NULL) |
| 614 | { |
| 615 | PyErr_SetVim(_("invalid expression")); |
| 616 | return NULL; |
| 617 | } |
| 618 | |
| 619 | result = ConvertToPyObject(our_tv); |
| 620 | Py_BEGIN_ALLOW_THREADS |
| 621 | Python_Lock_Vim(); |
| 622 | free_tv(our_tv); |
| 623 | Python_Release_Vim(); |
| 624 | Py_END_ALLOW_THREADS |
| 625 | |
| 626 | return result; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | static PyObject * |
| 630 | VimStrwidth(PyObject *self UNUSED, PyObject *args) |
| 631 | { |
| 632 | char *expr; |
| 633 | |
| 634 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 635 | return NULL; |
| 636 | |
Bram Moolenaar | a54bf40 | 2012-12-05 16:30:07 +0100 | [diff] [blame] | 637 | return PyLong_FromLong( |
| 638 | #ifdef FEAT_MBYTE |
| 639 | mb_string2cells((char_u *)expr, (int)STRLEN(expr)) |
| 640 | #else |
| 641 | STRLEN(expr) |
| 642 | #endif |
| 643 | ); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 644 | } |
| 645 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 646 | /* |
| 647 | * Vim module - Definitions |
| 648 | */ |
| 649 | |
| 650 | static struct PyMethodDef VimMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 651 | /* name, function, calling, documentation */ |
| 652 | {"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" }, |
| 653 | {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" }, |
| 654 | {"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"}, |
| 655 | {"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"}, |
| 656 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 657 | }; |
| 658 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 659 | /* |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 660 | * Generic iterator object |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 661 | */ |
| 662 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 663 | static PyTypeObject IterType; |
| 664 | |
| 665 | typedef PyObject *(*nextfun)(void **); |
| 666 | typedef void (*destructorfun)(void *); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 667 | typedef int (*traversefun)(void *, visitproc, void *); |
| 668 | typedef int (*clearfun)(void **); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 669 | |
Bram Moolenaar | 9e74e30 | 2013-05-17 21:20:17 +0200 | [diff] [blame] | 670 | /* Main purpose of this object is removing the need for do python |
| 671 | * initialization (i.e. PyType_Ready and setting type attributes) for a big |
| 672 | * bunch of objects. */ |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 673 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 674 | typedef struct |
| 675 | { |
| 676 | PyObject_HEAD |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 677 | void *cur; |
| 678 | nextfun next; |
| 679 | destructorfun destruct; |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 680 | traversefun traverse; |
| 681 | clearfun clear; |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 682 | } IterObject; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 683 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 684 | static PyObject * |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 685 | IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse, |
| 686 | clearfun clear) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 687 | { |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 688 | IterObject *self; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 689 | |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 690 | self = PyObject_GC_New(IterObject, &IterType); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 691 | self->cur = start; |
| 692 | self->next = next; |
| 693 | self->destruct = destruct; |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 694 | self->traverse = traverse; |
| 695 | self->clear = clear; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 696 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 697 | return (PyObject *)(self); |
| 698 | } |
| 699 | |
| 700 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 701 | IterDestructor(IterObject *self) |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 702 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 703 | PyObject_GC_UnTrack((void *)(self)); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 704 | self->destruct(self->cur); |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 705 | PyObject_GC_Del((void *)(self)); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 706 | } |
| 707 | |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 708 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 709 | IterTraverse(IterObject *self, visitproc visit, void *arg) |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 710 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 711 | if (self->traverse != NULL) |
| 712 | return self->traverse(self->cur, visit, arg); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 713 | else |
| 714 | return 0; |
| 715 | } |
| 716 | |
Bram Moolenaar | 9e74e30 | 2013-05-17 21:20:17 +0200 | [diff] [blame] | 717 | /* Mac OSX defines clear() somewhere. */ |
| 718 | #ifdef clear |
| 719 | # undef clear |
| 720 | #endif |
| 721 | |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 722 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 723 | IterClear(IterObject *self) |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 724 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 725 | if (self->clear != NULL) |
| 726 | return self->clear(&self->cur); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 727 | else |
| 728 | return 0; |
| 729 | } |
| 730 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 731 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 732 | IterNext(IterObject *self) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 733 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 734 | return self->next(&self->cur); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 737 | static PyObject * |
| 738 | IterIter(PyObject *self) |
| 739 | { |
| 740 | return self; |
| 741 | } |
Bram Moolenaar | dfa38d4 | 2013-05-15 13:38:47 +0200 | [diff] [blame] | 742 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 743 | typedef struct pylinkedlist_S { |
| 744 | struct pylinkedlist_S *pll_next; |
| 745 | struct pylinkedlist_S *pll_prev; |
| 746 | PyObject *pll_obj; |
| 747 | } pylinkedlist_T; |
| 748 | |
| 749 | static pylinkedlist_T *lastdict = NULL; |
| 750 | static pylinkedlist_T *lastlist = NULL; |
| 751 | |
| 752 | static void |
| 753 | pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last) |
| 754 | { |
| 755 | if (ref->pll_prev == NULL) |
| 756 | { |
| 757 | if (ref->pll_next == NULL) |
| 758 | { |
| 759 | *last = NULL; |
| 760 | return; |
| 761 | } |
| 762 | } |
| 763 | else |
| 764 | ref->pll_prev->pll_next = ref->pll_next; |
| 765 | |
| 766 | if (ref->pll_next == NULL) |
| 767 | *last = ref->pll_prev; |
| 768 | else |
| 769 | ref->pll_next->pll_prev = ref->pll_prev; |
| 770 | } |
| 771 | |
| 772 | static void |
| 773 | pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last) |
| 774 | { |
| 775 | if (*last == NULL) |
| 776 | ref->pll_prev = NULL; |
| 777 | else |
| 778 | { |
| 779 | (*last)->pll_next = ref; |
| 780 | ref->pll_prev = *last; |
| 781 | } |
| 782 | ref->pll_next = NULL; |
| 783 | ref->pll_obj = self; |
| 784 | *last = ref; |
| 785 | } |
| 786 | |
| 787 | static PyTypeObject DictionaryType; |
| 788 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 789 | #define DICTKEY_GET_NOTEMPTY(err) \ |
| 790 | DICTKEY_GET(err) \ |
| 791 | if (*key == NUL) \ |
| 792 | { \ |
| 793 | PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \ |
| 794 | return err; \ |
| 795 | } |
| 796 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 797 | typedef struct |
| 798 | { |
| 799 | PyObject_HEAD |
| 800 | dict_T *dict; |
| 801 | pylinkedlist_T ref; |
| 802 | } DictionaryObject; |
| 803 | |
| 804 | static PyObject * |
| 805 | DictionaryNew(dict_T *dict) |
| 806 | { |
| 807 | DictionaryObject *self; |
| 808 | |
| 809 | self = PyObject_NEW(DictionaryObject, &DictionaryType); |
| 810 | if (self == NULL) |
| 811 | return NULL; |
| 812 | self->dict = dict; |
| 813 | ++dict->dv_refcount; |
| 814 | |
| 815 | pyll_add((PyObject *)(self), &self->ref, &lastdict); |
| 816 | |
| 817 | return (PyObject *)(self); |
| 818 | } |
| 819 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 820 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 821 | DictionaryDestructor(DictionaryObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 822 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 823 | pyll_remove(&self->ref, &lastdict); |
| 824 | dict_unref(self->dict); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 825 | |
| 826 | DESTRUCTOR_FINISH(self); |
| 827 | } |
| 828 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 829 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 830 | DictionarySetattr(DictionaryObject *self, char *name, PyObject *val) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 831 | { |
| 832 | if (val == NULL) |
| 833 | { |
| 834 | PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes")); |
| 835 | return -1; |
| 836 | } |
| 837 | |
| 838 | if (strcmp(name, "locked") == 0) |
| 839 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 840 | if (self->dict->dv_lock == VAR_FIXED) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 841 | { |
| 842 | PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary")); |
| 843 | return -1; |
| 844 | } |
| 845 | else |
| 846 | { |
Bram Moolenaar | b983f75 | 2013-05-15 16:11:50 +0200 | [diff] [blame] | 847 | int istrue = PyObject_IsTrue(val); |
| 848 | if (istrue == -1) |
| 849 | return -1; |
| 850 | else if (istrue) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 851 | self->dict->dv_lock = VAR_LOCKED; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 852 | else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 853 | self->dict->dv_lock = 0; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 854 | } |
| 855 | return 0; |
| 856 | } |
| 857 | else |
| 858 | { |
| 859 | PyErr_SetString(PyExc_AttributeError, _("Cannot set this attribute")); |
| 860 | return -1; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 865 | DictionaryLength(DictionaryObject *self) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 866 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 867 | return ((PyInt) (self->dict->dv_hashtab.ht_used)); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 871 | DictionaryItem(DictionaryObject *self, PyObject *keyObject) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 872 | { |
| 873 | char_u *key; |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 874 | dictitem_T *di; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 875 | DICTKEY_DECL |
| 876 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 877 | DICTKEY_GET_NOTEMPTY(NULL) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 878 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 879 | di = dict_find(self->dict, key, -1); |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 880 | |
Bram Moolenaar | 696c211 | 2012-09-21 13:43:14 +0200 | [diff] [blame] | 881 | DICTKEY_UNREF |
| 882 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 883 | if (di == NULL) |
| 884 | { |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 885 | PyErr_SetObject(PyExc_KeyError, keyObject); |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 886 | return NULL; |
| 887 | } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 888 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 889 | return ConvertToPyObject(&di->di_tv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 893 | DictionaryAssItem(DictionaryObject *self, PyObject *keyObject, PyObject *valObject) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 894 | { |
| 895 | char_u *key; |
| 896 | typval_T tv; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 897 | dict_T *d = self->dict; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 898 | dictitem_T *di; |
| 899 | DICTKEY_DECL |
| 900 | |
| 901 | if (d->dv_lock) |
| 902 | { |
| 903 | PyErr_SetVim(_("dict is locked")); |
| 904 | return -1; |
| 905 | } |
| 906 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 907 | DICTKEY_GET_NOTEMPTY(-1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 908 | |
| 909 | di = dict_find(d, key, -1); |
| 910 | |
| 911 | if (valObject == NULL) |
| 912 | { |
Bram Moolenaar | f27839c | 2012-06-29 16:19:50 +0200 | [diff] [blame] | 913 | hashitem_T *hi; |
| 914 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 915 | if (di == NULL) |
| 916 | { |
Bram Moolenaar | 696c211 | 2012-09-21 13:43:14 +0200 | [diff] [blame] | 917 | DICTKEY_UNREF |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 918 | PyErr_SetObject(PyExc_KeyError, keyObject); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 919 | return -1; |
| 920 | } |
Bram Moolenaar | f27839c | 2012-06-29 16:19:50 +0200 | [diff] [blame] | 921 | hi = hash_find(&d->dv_hashtab, di->di_key); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 922 | hash_remove(&d->dv_hashtab, hi); |
| 923 | dictitem_free(di); |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | if (ConvertFromPyObject(valObject, &tv) == -1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 928 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 929 | |
| 930 | if (di == NULL) |
| 931 | { |
| 932 | di = dictitem_alloc(key); |
| 933 | if (di == NULL) |
| 934 | { |
| 935 | PyErr_NoMemory(); |
| 936 | return -1; |
| 937 | } |
| 938 | di->di_tv.v_lock = 0; |
| 939 | |
| 940 | if (dict_add(d, di) == FAIL) |
| 941 | { |
Bram Moolenaar | 696c211 | 2012-09-21 13:43:14 +0200 | [diff] [blame] | 942 | DICTKEY_UNREF |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 943 | vim_free(di); |
| 944 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 945 | return -1; |
| 946 | } |
| 947 | } |
| 948 | else |
| 949 | clear_tv(&di->di_tv); |
| 950 | |
| 951 | DICTKEY_UNREF |
| 952 | |
| 953 | copy_tv(&tv, &di->di_tv); |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 954 | clear_tv(&tv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 959 | DictionaryListKeys(DictionaryObject *self) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 960 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 961 | dict_T *dict = self->dict; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 962 | long_u todo = dict->dv_hashtab.ht_used; |
| 963 | Py_ssize_t i = 0; |
| 964 | PyObject *r; |
| 965 | hashitem_T *hi; |
| 966 | |
| 967 | r = PyList_New(todo); |
| 968 | for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi) |
| 969 | { |
| 970 | if (!HASHITEM_EMPTY(hi)) |
| 971 | { |
| 972 | PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key))); |
| 973 | --todo; |
| 974 | ++i; |
| 975 | } |
| 976 | } |
| 977 | return r; |
| 978 | } |
| 979 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 980 | static PyMappingMethods DictionaryAsMapping = { |
| 981 | (lenfunc) DictionaryLength, |
| 982 | (binaryfunc) DictionaryItem, |
| 983 | (objobjargproc) DictionaryAssItem, |
| 984 | }; |
| 985 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 986 | static struct PyMethodDef DictionaryMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 987 | {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""}, |
| 988 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 989 | }; |
| 990 | |
| 991 | static PyTypeObject ListType; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 992 | static PySequenceMethods ListAsSeq; |
| 993 | static PyMappingMethods ListAsMapping; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 994 | |
| 995 | typedef struct |
| 996 | { |
| 997 | PyObject_HEAD |
| 998 | list_T *list; |
| 999 | pylinkedlist_T ref; |
| 1000 | } ListObject; |
| 1001 | |
| 1002 | static PyObject * |
| 1003 | ListNew(list_T *list) |
| 1004 | { |
| 1005 | ListObject *self; |
| 1006 | |
| 1007 | self = PyObject_NEW(ListObject, &ListType); |
| 1008 | if (self == NULL) |
| 1009 | return NULL; |
| 1010 | self->list = list; |
| 1011 | ++list->lv_refcount; |
| 1012 | |
| 1013 | pyll_add((PyObject *)(self), &self->ref, &lastlist); |
| 1014 | |
| 1015 | return (PyObject *)(self); |
| 1016 | } |
| 1017 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1018 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1019 | ListDestructor(ListObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1020 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1021 | pyll_remove(&self->ref, &lastlist); |
| 1022 | list_unref(self->list); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1023 | |
| 1024 | DESTRUCTOR_FINISH(self); |
| 1025 | } |
| 1026 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1027 | static int |
| 1028 | list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict) |
| 1029 | { |
| 1030 | Py_ssize_t i; |
| 1031 | Py_ssize_t lsize = PySequence_Size(obj); |
| 1032 | PyObject *litem; |
| 1033 | listitem_T *li; |
| 1034 | |
| 1035 | for(i=0; i<lsize; i++) |
| 1036 | { |
| 1037 | li = listitem_alloc(); |
| 1038 | if (li == NULL) |
| 1039 | { |
| 1040 | PyErr_NoMemory(); |
| 1041 | return -1; |
| 1042 | } |
| 1043 | li->li_tv.v_lock = 0; |
| 1044 | |
| 1045 | litem = PySequence_GetItem(obj, i); |
| 1046 | if (litem == NULL) |
| 1047 | return -1; |
| 1048 | if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1) |
| 1049 | return -1; |
| 1050 | |
| 1051 | list_append(l, li); |
| 1052 | } |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1056 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1057 | ListLength(ListObject *self) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1058 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1059 | return ((PyInt) (self->list->lv_len)); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1063 | ListItem(ListObject *self, Py_ssize_t index) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1064 | { |
| 1065 | listitem_T *li; |
| 1066 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1067 | if (index >= ListLength(self)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1068 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1069 | PyErr_SetString(PyExc_IndexError, _("list index out of range")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1070 | return NULL; |
| 1071 | } |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1072 | li = list_find(self->list, (long) index); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1073 | if (li == NULL) |
| 1074 | { |
| 1075 | PyErr_SetVim(_("internal error: failed to get vim list item")); |
| 1076 | return NULL; |
| 1077 | } |
| 1078 | return ConvertToPyObject(&li->li_tv); |
| 1079 | } |
| 1080 | |
| 1081 | #define PROC_RANGE \ |
| 1082 | if (last < 0) {\ |
| 1083 | if (last < -size) \ |
| 1084 | last = 0; \ |
| 1085 | else \ |
| 1086 | last += size; \ |
| 1087 | } \ |
| 1088 | if (first < 0) \ |
| 1089 | first = 0; \ |
| 1090 | if (first > size) \ |
| 1091 | first = size; \ |
| 1092 | if (last > size) \ |
| 1093 | last = size; |
| 1094 | |
| 1095 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1096 | ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1097 | { |
| 1098 | PyInt i; |
| 1099 | PyInt size = ListLength(self); |
| 1100 | PyInt n; |
| 1101 | PyObject *list; |
| 1102 | int reversed = 0; |
| 1103 | |
| 1104 | PROC_RANGE |
| 1105 | if (first >= last) |
| 1106 | first = last; |
| 1107 | |
| 1108 | n = last-first; |
| 1109 | list = PyList_New(n); |
| 1110 | if (list == NULL) |
| 1111 | return NULL; |
| 1112 | |
| 1113 | for (i = 0; i < n; ++i) |
| 1114 | { |
Bram Moolenaar | 24b11fb | 2013-04-05 19:32:36 +0200 | [diff] [blame] | 1115 | PyObject *item = ListItem(self, first + i); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1116 | if (item == NULL) |
| 1117 | { |
| 1118 | Py_DECREF(list); |
| 1119 | return NULL; |
| 1120 | } |
| 1121 | |
| 1122 | if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item))) |
| 1123 | { |
| 1124 | Py_DECREF(item); |
| 1125 | Py_DECREF(list); |
| 1126 | return NULL; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | return list; |
| 1131 | } |
| 1132 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 1133 | typedef struct |
| 1134 | { |
| 1135 | listwatch_T lw; |
| 1136 | list_T *list; |
| 1137 | } listiterinfo_T; |
| 1138 | |
| 1139 | static void |
| 1140 | ListIterDestruct(listiterinfo_T *lii) |
| 1141 | { |
| 1142 | list_rem_watch(lii->list, &lii->lw); |
| 1143 | PyMem_Free(lii); |
| 1144 | } |
| 1145 | |
| 1146 | static PyObject * |
| 1147 | ListIterNext(listiterinfo_T **lii) |
| 1148 | { |
| 1149 | PyObject *r; |
| 1150 | |
| 1151 | if (!((*lii)->lw.lw_item)) |
| 1152 | return NULL; |
| 1153 | |
| 1154 | if (!(r = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv)))) |
| 1155 | return NULL; |
| 1156 | |
| 1157 | (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next; |
| 1158 | |
| 1159 | return r; |
| 1160 | } |
| 1161 | |
| 1162 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1163 | ListIter(ListObject *self) |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 1164 | { |
| 1165 | listiterinfo_T *lii; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1166 | list_T *l = self->list; |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 1167 | |
| 1168 | if (!(lii = PyMem_New(listiterinfo_T, 1))) |
| 1169 | { |
| 1170 | PyErr_NoMemory(); |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | |
| 1174 | list_add_watch(l, &lii->lw); |
| 1175 | lii->lw.lw_item = l->lv_first; |
| 1176 | lii->list = l; |
| 1177 | |
| 1178 | return IterNew(lii, |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1179 | (destructorfun) ListIterDestruct, (nextfun) ListIterNext, |
| 1180 | NULL, NULL); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 1181 | } |
| 1182 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1183 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1184 | ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1185 | { |
| 1186 | typval_T tv; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1187 | list_T *l = self->list; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1188 | listitem_T *li; |
| 1189 | Py_ssize_t length = ListLength(self); |
| 1190 | |
| 1191 | if (l->lv_lock) |
| 1192 | { |
| 1193 | PyErr_SetVim(_("list is locked")); |
| 1194 | return -1; |
| 1195 | } |
| 1196 | if (index>length || (index==length && obj==NULL)) |
| 1197 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1198 | PyErr_SetString(PyExc_IndexError, _("list index out of range")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1199 | return -1; |
| 1200 | } |
| 1201 | |
| 1202 | if (obj == NULL) |
| 1203 | { |
| 1204 | li = list_find(l, (long) index); |
| 1205 | list_remove(l, li, li); |
| 1206 | clear_tv(&li->li_tv); |
| 1207 | vim_free(li); |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | if (ConvertFromPyObject(obj, &tv) == -1) |
| 1212 | return -1; |
| 1213 | |
| 1214 | if (index == length) |
| 1215 | { |
| 1216 | if (list_append_tv(l, &tv) == FAIL) |
| 1217 | { |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1218 | clear_tv(&tv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1219 | PyErr_SetVim(_("Failed to add item to list")); |
| 1220 | return -1; |
| 1221 | } |
| 1222 | } |
| 1223 | else |
| 1224 | { |
| 1225 | li = list_find(l, (long) index); |
| 1226 | clear_tv(&li->li_tv); |
| 1227 | copy_tv(&tv, &li->li_tv); |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1228 | clear_tv(&tv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1229 | } |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1234 | ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1235 | { |
| 1236 | PyInt size = ListLength(self); |
| 1237 | Py_ssize_t i; |
| 1238 | Py_ssize_t lsize; |
| 1239 | PyObject *litem; |
| 1240 | listitem_T *li; |
| 1241 | listitem_T *next; |
| 1242 | typval_T v; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1243 | list_T *l = self->list; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1244 | |
| 1245 | if (l->lv_lock) |
| 1246 | { |
| 1247 | PyErr_SetVim(_("list is locked")); |
| 1248 | return -1; |
| 1249 | } |
| 1250 | |
| 1251 | PROC_RANGE |
| 1252 | |
| 1253 | if (first == size) |
| 1254 | li = NULL; |
| 1255 | else |
| 1256 | { |
| 1257 | li = list_find(l, (long) first); |
| 1258 | if (li == NULL) |
| 1259 | { |
| 1260 | PyErr_SetVim(_("internal error: no vim list item")); |
| 1261 | return -1; |
| 1262 | } |
| 1263 | if (last > first) |
| 1264 | { |
| 1265 | i = last - first; |
| 1266 | while (i-- && li != NULL) |
| 1267 | { |
| 1268 | next = li->li_next; |
| 1269 | listitem_remove(l, li); |
| 1270 | li = next; |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | if (obj == NULL) |
| 1276 | return 0; |
| 1277 | |
| 1278 | if (!PyList_Check(obj)) |
| 1279 | { |
| 1280 | PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice")); |
| 1281 | return -1; |
| 1282 | } |
| 1283 | |
| 1284 | lsize = PyList_Size(obj); |
| 1285 | |
| 1286 | for(i=0; i<lsize; i++) |
| 1287 | { |
| 1288 | litem = PyList_GetItem(obj, i); |
| 1289 | if (litem == NULL) |
| 1290 | return -1; |
| 1291 | if (ConvertFromPyObject(litem, &v) == -1) |
| 1292 | return -1; |
| 1293 | if (list_insert_tv(l, &v, li) == FAIL) |
| 1294 | { |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1295 | clear_tv(&v); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1296 | PyErr_SetVim(_("internal error: failed to add item to list")); |
| 1297 | return -1; |
| 1298 | } |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1299 | clear_tv(&v); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1300 | } |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1305 | ListConcatInPlace(ListObject *self, PyObject *obj) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1306 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1307 | list_T *l = self->list; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1308 | PyObject *lookup_dict; |
| 1309 | |
| 1310 | if (l->lv_lock) |
| 1311 | { |
| 1312 | PyErr_SetVim(_("list is locked")); |
| 1313 | return NULL; |
| 1314 | } |
| 1315 | |
| 1316 | if (!PySequence_Check(obj)) |
| 1317 | { |
| 1318 | PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists")); |
| 1319 | return NULL; |
| 1320 | } |
| 1321 | |
| 1322 | lookup_dict = PyDict_New(); |
| 1323 | if (list_py_concat(l, obj, lookup_dict) == -1) |
| 1324 | { |
| 1325 | Py_DECREF(lookup_dict); |
| 1326 | return NULL; |
| 1327 | } |
| 1328 | Py_DECREF(lookup_dict); |
| 1329 | |
| 1330 | Py_INCREF(self); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1331 | return (PyObject *)(self); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1332 | } |
| 1333 | |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1334 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1335 | ListSetattr(ListObject *self, char *name, PyObject *val) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1336 | { |
| 1337 | if (val == NULL) |
| 1338 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1339 | PyErr_SetString(PyExc_AttributeError, |
| 1340 | _("cannot delete vim.dictionary attributes")); |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1341 | return -1; |
| 1342 | } |
| 1343 | |
| 1344 | if (strcmp(name, "locked") == 0) |
| 1345 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1346 | if (self->list->lv_lock == VAR_FIXED) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1347 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1348 | PyErr_SetString(PyExc_TypeError, _("cannot modify fixed list")); |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1349 | return -1; |
| 1350 | } |
| 1351 | else |
| 1352 | { |
Bram Moolenaar | b983f75 | 2013-05-15 16:11:50 +0200 | [diff] [blame] | 1353 | int istrue = PyObject_IsTrue(val); |
| 1354 | if (istrue == -1) |
| 1355 | return -1; |
| 1356 | else if (istrue) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1357 | self->list->lv_lock = VAR_LOCKED; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1358 | else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1359 | self->list->lv_lock = 0; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1360 | } |
| 1361 | return 0; |
| 1362 | } |
| 1363 | else |
| 1364 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1365 | PyErr_SetString(PyExc_AttributeError, _("cannot set this attribute")); |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1366 | return -1; |
| 1367 | } |
| 1368 | } |
| 1369 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1370 | static struct PyMethodDef ListMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 1371 | {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""}, |
| 1372 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1373 | }; |
| 1374 | |
| 1375 | typedef struct |
| 1376 | { |
| 1377 | PyObject_HEAD |
| 1378 | char_u *name; |
| 1379 | } FunctionObject; |
| 1380 | |
| 1381 | static PyTypeObject FunctionType; |
| 1382 | |
| 1383 | static PyObject * |
| 1384 | FunctionNew(char_u *name) |
| 1385 | { |
| 1386 | FunctionObject *self; |
| 1387 | |
| 1388 | self = PyObject_NEW(FunctionObject, &FunctionType); |
| 1389 | if (self == NULL) |
| 1390 | return NULL; |
| 1391 | self->name = PyMem_New(char_u, STRLEN(name) + 1); |
| 1392 | if (self->name == NULL) |
| 1393 | { |
| 1394 | PyErr_NoMemory(); |
| 1395 | return NULL; |
| 1396 | } |
| 1397 | STRCPY(self->name, name); |
| 1398 | func_ref(name); |
| 1399 | return (PyObject *)(self); |
| 1400 | } |
| 1401 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1402 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1403 | FunctionDestructor(FunctionObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1404 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1405 | func_unref(self->name); |
| 1406 | PyMem_Free(self->name); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1407 | |
| 1408 | DESTRUCTOR_FINISH(self); |
| 1409 | } |
| 1410 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1411 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1412 | FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1413 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1414 | char_u *name = self->name; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1415 | typval_T args; |
| 1416 | typval_T selfdicttv; |
| 1417 | typval_T rettv; |
| 1418 | dict_T *selfdict = NULL; |
| 1419 | PyObject *selfdictObject; |
| 1420 | PyObject *result; |
| 1421 | int error; |
| 1422 | |
| 1423 | if (ConvertFromPyObject(argsObject, &args) == -1) |
| 1424 | return NULL; |
| 1425 | |
| 1426 | if (kwargs != NULL) |
| 1427 | { |
| 1428 | selfdictObject = PyDict_GetItemString(kwargs, "self"); |
| 1429 | if (selfdictObject != NULL) |
| 1430 | { |
Bram Moolenaar | 9581b5f | 2012-07-25 15:36:04 +0200 | [diff] [blame] | 1431 | if (!PyMapping_Check(selfdictObject)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1432 | { |
Bram Moolenaar | 9581b5f | 2012-07-25 15:36:04 +0200 | [diff] [blame] | 1433 | PyErr_SetString(PyExc_TypeError, |
| 1434 | _("'self' argument must be a dictionary")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1435 | clear_tv(&args); |
| 1436 | return NULL; |
| 1437 | } |
| 1438 | if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1) |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1439 | { |
| 1440 | clear_tv(&args); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1441 | return NULL; |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1442 | } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1443 | selfdict = selfdicttv.vval.v_dict; |
| 1444 | } |
| 1445 | } |
| 1446 | |
Bram Moolenaar | 71700b8 | 2013-05-15 17:49:05 +0200 | [diff] [blame] | 1447 | Py_BEGIN_ALLOW_THREADS |
| 1448 | Python_Lock_Vim(); |
| 1449 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 1450 | VimTryStart(); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1451 | error = func_call(name, &args, selfdict, &rettv); |
Bram Moolenaar | 71700b8 | 2013-05-15 17:49:05 +0200 | [diff] [blame] | 1452 | |
| 1453 | Python_Release_Vim(); |
| 1454 | Py_END_ALLOW_THREADS |
| 1455 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 1456 | if (VimTryEnd()) |
| 1457 | result = NULL; |
| 1458 | else if (error != OK) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1459 | { |
| 1460 | result = NULL; |
| 1461 | PyErr_SetVim(_("failed to run function")); |
| 1462 | } |
| 1463 | else |
| 1464 | result = ConvertToPyObject(&rettv); |
| 1465 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1466 | clear_tv(&args); |
| 1467 | clear_tv(&rettv); |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1468 | if (selfdict != NULL) |
| 1469 | clear_tv(&selfdicttv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1470 | |
| 1471 | return result; |
| 1472 | } |
| 1473 | |
| 1474 | static struct PyMethodDef FunctionMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 1475 | {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""}, |
| 1476 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1477 | }; |
| 1478 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1479 | /* |
| 1480 | * Options object |
| 1481 | */ |
| 1482 | |
| 1483 | static PyTypeObject OptionsType; |
| 1484 | |
| 1485 | typedef int (*checkfun)(void *); |
| 1486 | |
| 1487 | typedef struct |
| 1488 | { |
| 1489 | PyObject_HEAD |
| 1490 | int opt_type; |
| 1491 | void *from; |
| 1492 | checkfun Check; |
| 1493 | PyObject *fromObj; |
| 1494 | } OptionsObject; |
| 1495 | |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1496 | static int |
| 1497 | dummy_check(void *arg UNUSED) |
| 1498 | { |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | static PyObject * |
| 1503 | OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj) |
| 1504 | { |
| 1505 | OptionsObject *self; |
| 1506 | |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 1507 | self = PyObject_GC_New(OptionsObject, &OptionsType); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1508 | if (self == NULL) |
| 1509 | return NULL; |
| 1510 | |
| 1511 | self->opt_type = opt_type; |
| 1512 | self->from = from; |
| 1513 | self->Check = Check; |
| 1514 | self->fromObj = fromObj; |
| 1515 | if (fromObj) |
| 1516 | Py_INCREF(fromObj); |
| 1517 | |
| 1518 | return (PyObject *)(self); |
| 1519 | } |
| 1520 | |
| 1521 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1522 | OptionsDestructor(OptionsObject *self) |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1523 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 1524 | PyObject_GC_UnTrack((void *)(self)); |
| 1525 | Py_XDECREF(self->fromObj); |
| 1526 | PyObject_GC_Del((void *)(self)); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1530 | OptionsTraverse(OptionsObject *self, visitproc visit, void *arg) |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1531 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1532 | Py_VISIT(self->fromObj); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1533 | return 0; |
| 1534 | } |
| 1535 | |
| 1536 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1537 | OptionsClear(OptionsObject *self) |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1538 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1539 | Py_CLEAR(self->fromObj); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 1540 | return 0; |
| 1541 | } |
| 1542 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1543 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1544 | OptionsItem(OptionsObject *self, PyObject *keyObject) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1545 | { |
| 1546 | char_u *key; |
| 1547 | int flags; |
| 1548 | long numval; |
| 1549 | char_u *stringval; |
Bram Moolenaar | 161fb5e | 2013-05-06 06:26:15 +0200 | [diff] [blame] | 1550 | DICTKEY_DECL |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1551 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1552 | if (self->Check(self->from)) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1553 | return NULL; |
| 1554 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1555 | DICTKEY_GET_NOTEMPTY(NULL) |
| 1556 | |
| 1557 | flags = get_option_value_strict(key, &numval, &stringval, |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1558 | self->opt_type, self->from); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1559 | |
| 1560 | DICTKEY_UNREF |
| 1561 | |
| 1562 | if (flags == 0) |
| 1563 | { |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 1564 | PyErr_SetObject(PyExc_KeyError, keyObject); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1565 | return NULL; |
| 1566 | } |
| 1567 | |
| 1568 | if (flags & SOPT_UNSET) |
| 1569 | { |
| 1570 | Py_INCREF(Py_None); |
| 1571 | return Py_None; |
| 1572 | } |
| 1573 | else if (flags & SOPT_BOOL) |
| 1574 | { |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1575 | PyObject *r; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1576 | r = numval ? Py_True : Py_False; |
| 1577 | Py_INCREF(r); |
| 1578 | return r; |
| 1579 | } |
| 1580 | else if (flags & SOPT_NUM) |
| 1581 | return PyInt_FromLong(numval); |
| 1582 | else if (flags & SOPT_STRING) |
| 1583 | { |
| 1584 | if (stringval) |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1585 | { |
| 1586 | PyObject *r = PyBytes_FromString((char *) stringval); |
| 1587 | vim_free(stringval); |
| 1588 | return r; |
| 1589 | } |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1590 | else |
| 1591 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1592 | PyErr_SetString(PyExc_RuntimeError, |
| 1593 | _("unable to get option value")); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1594 | return NULL; |
| 1595 | } |
| 1596 | } |
| 1597 | else |
| 1598 | { |
| 1599 | PyErr_SetVim("Internal error: unknown option type. Should not happen"); |
| 1600 | return NULL; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | static int |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1605 | set_option_value_err(key, numval, stringval, opt_flags) |
| 1606 | char_u *key; |
| 1607 | int numval; |
| 1608 | char_u *stringval; |
| 1609 | int opt_flags; |
| 1610 | { |
| 1611 | char_u *errmsg; |
| 1612 | |
| 1613 | if ((errmsg = set_option_value(key, numval, stringval, opt_flags))) |
| 1614 | { |
| 1615 | if (VimTryEnd()) |
| 1616 | return FAIL; |
| 1617 | PyErr_SetVim((char *)errmsg); |
| 1618 | return FAIL; |
| 1619 | } |
| 1620 | return OK; |
| 1621 | } |
| 1622 | |
| 1623 | static int |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1624 | set_option_value_for(key, numval, stringval, opt_flags, opt_type, from) |
| 1625 | char_u *key; |
| 1626 | int numval; |
| 1627 | char_u *stringval; |
| 1628 | int opt_flags; |
| 1629 | int opt_type; |
| 1630 | void *from; |
| 1631 | { |
Bram Moolenaar | 0b9aecc | 2013-05-21 22:13:41 +0200 | [diff] [blame] | 1632 | win_T *save_curwin = NULL; |
| 1633 | tabpage_T *save_curtab = NULL; |
| 1634 | buf_T *save_curbuf = NULL; |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1635 | int r = 0; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1636 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 1637 | VimTryStart(); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1638 | switch (opt_type) |
| 1639 | { |
| 1640 | case SREQ_WIN: |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 1641 | if (switch_win(&save_curwin, &save_curtab, (win_T *)from, |
| 1642 | win_find_tabpage((win_T *)from)) == FAIL) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1643 | { |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 1644 | if (VimTryEnd()) |
| 1645 | return -1; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1646 | PyErr_SetVim("Problem while switching windows."); |
| 1647 | return -1; |
| 1648 | } |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1649 | r = set_option_value_err(key, numval, stringval, opt_flags); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1650 | restore_win(save_curwin, save_curtab); |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1651 | if (r == FAIL) |
| 1652 | return -1; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1653 | break; |
| 1654 | case SREQ_BUF: |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 1655 | switch_buffer(&save_curbuf, (buf_T *)from); |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1656 | r = set_option_value_err(key, numval, stringval, opt_flags); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 1657 | restore_buffer(save_curbuf); |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1658 | if (r == FAIL) |
| 1659 | return -1; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1660 | break; |
| 1661 | case SREQ_GLOBAL: |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1662 | r = set_option_value_err(key, numval, stringval, opt_flags); |
| 1663 | if (r == FAIL) |
| 1664 | return -1; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1665 | break; |
| 1666 | } |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 1667 | return VimTryEnd(); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1668 | } |
| 1669 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 1670 | static void * |
| 1671 | py_memsave(void *p, size_t len) |
| 1672 | { |
| 1673 | void *r; |
| 1674 | if (!(r = PyMem_Malloc(len))) |
| 1675 | return NULL; |
| 1676 | mch_memmove(r, p, len); |
| 1677 | return r; |
| 1678 | } |
| 1679 | |
| 1680 | #define PY_STRSAVE(s) ((char_u *) py_memsave(s, STRLEN(s) + 1)) |
| 1681 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1682 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1683 | OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1684 | { |
| 1685 | char_u *key; |
| 1686 | int flags; |
| 1687 | int opt_flags; |
| 1688 | int r = 0; |
Bram Moolenaar | 161fb5e | 2013-05-06 06:26:15 +0200 | [diff] [blame] | 1689 | DICTKEY_DECL |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1690 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1691 | if (self->Check(self->from)) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1692 | return -1; |
| 1693 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1694 | DICTKEY_GET_NOTEMPTY(-1) |
| 1695 | |
| 1696 | flags = get_option_value_strict(key, NULL, NULL, |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1697 | self->opt_type, self->from); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1698 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1699 | if (flags == 0) |
| 1700 | { |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 1701 | PyErr_SetObject(PyExc_KeyError, keyObject); |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1702 | DICTKEY_UNREF |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1703 | return -1; |
| 1704 | } |
| 1705 | |
| 1706 | if (valObject == NULL) |
| 1707 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1708 | if (self->opt_type == SREQ_GLOBAL) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1709 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1710 | PyErr_SetString(PyExc_ValueError, |
| 1711 | _("unable to unset global option")); |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1712 | DICTKEY_UNREF |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1713 | return -1; |
| 1714 | } |
| 1715 | else if (!(flags & SOPT_GLOBAL)) |
| 1716 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1717 | PyErr_SetString(PyExc_ValueError, _("unable to unset option " |
| 1718 | "without global value")); |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1719 | DICTKEY_UNREF |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1720 | return -1; |
| 1721 | } |
| 1722 | else |
| 1723 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1724 | unset_global_local_option(key, self->from); |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1725 | DICTKEY_UNREF |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1726 | return 0; |
| 1727 | } |
| 1728 | } |
| 1729 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1730 | opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1731 | |
| 1732 | if (flags & SOPT_BOOL) |
| 1733 | { |
Bram Moolenaar | b983f75 | 2013-05-15 16:11:50 +0200 | [diff] [blame] | 1734 | int istrue = PyObject_IsTrue(valObject); |
Bram Moolenaar | c96ebe7 | 2013-05-21 22:38:18 +0200 | [diff] [blame] | 1735 | |
Bram Moolenaar | b983f75 | 2013-05-15 16:11:50 +0200 | [diff] [blame] | 1736 | if (istrue == -1) |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1737 | r = -1; |
| 1738 | else |
| 1739 | r = set_option_value_for(key, istrue, NULL, |
| 1740 | opt_flags, self->opt_type, self->from); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1741 | } |
| 1742 | else if (flags & SOPT_NUM) |
| 1743 | { |
| 1744 | int val; |
| 1745 | |
| 1746 | #if PY_MAJOR_VERSION < 3 |
| 1747 | if (PyInt_Check(valObject)) |
| 1748 | val = PyInt_AsLong(valObject); |
| 1749 | else |
| 1750 | #endif |
| 1751 | if (PyLong_Check(valObject)) |
| 1752 | val = PyLong_AsLong(valObject); |
| 1753 | else |
| 1754 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1755 | PyErr_SetString(PyExc_TypeError, _("object must be integer")); |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1756 | DICTKEY_UNREF |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1757 | return -1; |
| 1758 | } |
| 1759 | |
| 1760 | r = set_option_value_for(key, val, NULL, opt_flags, |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1761 | self->opt_type, self->from); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1762 | } |
| 1763 | else |
| 1764 | { |
| 1765 | char_u *val; |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 1766 | PyObject *todecref; |
| 1767 | |
| 1768 | if ((val = StringToChars(valObject, &todecref))) |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1769 | { |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 1770 | r = set_option_value_for(key, 0, val, opt_flags, |
| 1771 | self->opt_type, self->from); |
| 1772 | Py_XDECREF(todecref); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1773 | } |
| 1774 | else |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 1775 | r = -1; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1776 | } |
| 1777 | |
Bram Moolenaar | 1bc2428 | 2013-05-29 21:37:35 +0200 | [diff] [blame] | 1778 | DICTKEY_UNREF |
| 1779 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1780 | return r; |
| 1781 | } |
| 1782 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1783 | static PyMappingMethods OptionsAsMapping = { |
| 1784 | (lenfunc) NULL, |
| 1785 | (binaryfunc) OptionsItem, |
| 1786 | (objobjargproc) OptionsAssItem, |
| 1787 | }; |
| 1788 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1789 | /* Tabpage object |
| 1790 | */ |
| 1791 | |
| 1792 | typedef struct |
| 1793 | { |
| 1794 | PyObject_HEAD |
| 1795 | tabpage_T *tab; |
| 1796 | } TabPageObject; |
| 1797 | |
| 1798 | static PyObject *WinListNew(TabPageObject *tabObject); |
| 1799 | |
| 1800 | static PyTypeObject TabPageType; |
| 1801 | |
| 1802 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1803 | CheckTabPage(TabPageObject *self) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1804 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1805 | if (self->tab == INVALID_TABPAGE_VALUE) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1806 | { |
| 1807 | PyErr_SetVim(_("attempt to refer to deleted tab page")); |
| 1808 | return -1; |
| 1809 | } |
| 1810 | |
| 1811 | return 0; |
| 1812 | } |
| 1813 | |
| 1814 | static PyObject * |
| 1815 | TabPageNew(tabpage_T *tab) |
| 1816 | { |
| 1817 | TabPageObject *self; |
| 1818 | |
| 1819 | if (TAB_PYTHON_REF(tab)) |
| 1820 | { |
| 1821 | self = TAB_PYTHON_REF(tab); |
| 1822 | Py_INCREF(self); |
| 1823 | } |
| 1824 | else |
| 1825 | { |
| 1826 | self = PyObject_NEW(TabPageObject, &TabPageType); |
| 1827 | if (self == NULL) |
| 1828 | return NULL; |
| 1829 | self->tab = tab; |
| 1830 | TAB_PYTHON_REF(tab) = self; |
| 1831 | } |
| 1832 | |
| 1833 | return (PyObject *)(self); |
| 1834 | } |
| 1835 | |
| 1836 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1837 | TabPageDestructor(TabPageObject *self) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1838 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1839 | if (self->tab && self->tab != INVALID_TABPAGE_VALUE) |
| 1840 | TAB_PYTHON_REF(self->tab) = NULL; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1841 | |
| 1842 | DESTRUCTOR_FINISH(self); |
| 1843 | } |
| 1844 | |
| 1845 | static PyObject * |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 1846 | TabPageAttrValid(TabPageObject *self, char *name) |
| 1847 | { |
| 1848 | PyObject *r; |
| 1849 | |
| 1850 | if (strcmp(name, "valid") != 0) |
| 1851 | return NULL; |
| 1852 | |
| 1853 | r = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True); |
| 1854 | Py_INCREF(r); |
| 1855 | return r; |
| 1856 | } |
| 1857 | |
| 1858 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1859 | TabPageAttr(TabPageObject *self, char *name) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1860 | { |
| 1861 | if (strcmp(name, "windows") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1862 | return WinListNew(self); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1863 | else if (strcmp(name, "number") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1864 | return PyLong_FromLong((long) get_tab_number(self->tab)); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1865 | else if (strcmp(name, "vars") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1866 | return DictionaryNew(self->tab->tp_vars); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1867 | else if (strcmp(name, "window") == 0) |
| 1868 | { |
| 1869 | /* For current tab window.c does not bother to set or update tp_curwin |
| 1870 | */ |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1871 | if (self->tab == curtab) |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 1872 | return WindowNew(curwin, curtab); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1873 | else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1874 | return WindowNew(self->tab->tp_curwin, self->tab); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1875 | } |
| 1876 | return NULL; |
| 1877 | } |
| 1878 | |
| 1879 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1880 | TabPageRepr(TabPageObject *self) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1881 | { |
| 1882 | static char repr[100]; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1883 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1884 | if (self->tab == INVALID_TABPAGE_VALUE) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1885 | { |
| 1886 | vim_snprintf(repr, 100, _("<tabpage object (deleted) at %p>"), (self)); |
| 1887 | return PyString_FromString(repr); |
| 1888 | } |
| 1889 | else |
| 1890 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1891 | int t = get_tab_number(self->tab); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1892 | |
| 1893 | if (t == 0) |
| 1894 | vim_snprintf(repr, 100, _("<tabpage object (unknown) at %p>"), |
| 1895 | (self)); |
| 1896 | else |
| 1897 | vim_snprintf(repr, 100, _("<tabpage %d>"), t - 1); |
| 1898 | |
| 1899 | return PyString_FromString(repr); |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | static struct PyMethodDef TabPageMethods[] = { |
| 1904 | /* name, function, calling, documentation */ |
| 1905 | { NULL, NULL, 0, NULL } |
| 1906 | }; |
| 1907 | |
| 1908 | /* |
| 1909 | * Window list object |
| 1910 | */ |
| 1911 | |
| 1912 | static PyTypeObject TabListType; |
| 1913 | static PySequenceMethods TabListAsSeq; |
| 1914 | |
| 1915 | typedef struct |
| 1916 | { |
| 1917 | PyObject_HEAD |
| 1918 | } TabListObject; |
| 1919 | |
| 1920 | static PyInt |
| 1921 | TabListLength(PyObject *self UNUSED) |
| 1922 | { |
| 1923 | tabpage_T *tp = first_tabpage; |
| 1924 | PyInt n = 0; |
| 1925 | |
| 1926 | while (tp != NULL) |
| 1927 | { |
| 1928 | ++n; |
| 1929 | tp = tp->tp_next; |
| 1930 | } |
| 1931 | |
| 1932 | return n; |
| 1933 | } |
| 1934 | |
| 1935 | static PyObject * |
| 1936 | TabListItem(PyObject *self UNUSED, PyInt n) |
| 1937 | { |
| 1938 | tabpage_T *tp; |
| 1939 | |
| 1940 | for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n) |
| 1941 | if (n == 0) |
| 1942 | return TabPageNew(tp); |
| 1943 | |
| 1944 | PyErr_SetString(PyExc_IndexError, _("no such tab page")); |
| 1945 | return NULL; |
| 1946 | } |
| 1947 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 1948 | /* Window object |
| 1949 | */ |
| 1950 | |
| 1951 | typedef struct |
| 1952 | { |
| 1953 | PyObject_HEAD |
| 1954 | win_T *win; |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 1955 | TabPageObject *tabObject; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 1956 | } WindowObject; |
| 1957 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 1958 | static PyTypeObject WindowType; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1959 | |
| 1960 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1961 | CheckWindow(WindowObject *self) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1962 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1963 | if (self->win == INVALID_WINDOW_VALUE) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1964 | { |
| 1965 | PyErr_SetVim(_("attempt to refer to deleted window")); |
| 1966 | return -1; |
| 1967 | } |
| 1968 | |
| 1969 | return 0; |
| 1970 | } |
| 1971 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1972 | static PyObject * |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 1973 | WindowNew(win_T *win, tabpage_T *tab) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 1974 | { |
| 1975 | /* We need to handle deletion of windows underneath us. |
| 1976 | * If we add a "w_python*_ref" field to the win_T structure, |
| 1977 | * then we can get at it in win_free() in vim. We then |
| 1978 | * need to create only ONE Python object per window - if |
| 1979 | * we try to create a second, just INCREF the existing one |
| 1980 | * and return it. The (single) Python object referring to |
| 1981 | * the window is stored in "w_python*_ref". |
| 1982 | * On a win_free() we set the Python object's win_T* field |
| 1983 | * to an invalid value. We trap all uses of a window |
| 1984 | * object, and reject them if the win_T* field is invalid. |
| 1985 | * |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 1986 | * Python2 and Python3 get different fields and different objects: |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 1987 | * w_python_ref and w_python3_ref fields respectively. |
| 1988 | */ |
| 1989 | |
| 1990 | WindowObject *self; |
| 1991 | |
| 1992 | if (WIN_PYTHON_REF(win)) |
| 1993 | { |
| 1994 | self = WIN_PYTHON_REF(win); |
| 1995 | Py_INCREF(self); |
| 1996 | } |
| 1997 | else |
| 1998 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 1999 | self = PyObject_GC_New(WindowObject, &WindowType); |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 2000 | if (self == NULL) |
| 2001 | return NULL; |
| 2002 | self->win = win; |
| 2003 | WIN_PYTHON_REF(win) = self; |
| 2004 | } |
| 2005 | |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2006 | self->tabObject = ((TabPageObject *)(TabPageNew(tab))); |
| 2007 | |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 2008 | return (PyObject *)(self); |
| 2009 | } |
| 2010 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2011 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2012 | WindowDestructor(WindowObject *self) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2013 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 2014 | PyObject_GC_UnTrack((void *)(self)); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2015 | if (self->win && self->win != INVALID_WINDOW_VALUE) |
| 2016 | WIN_PYTHON_REF(self->win) = NULL; |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 2017 | Py_XDECREF(((PyObject *)(self->tabObject))); |
| 2018 | PyObject_GC_Del((void *)(self)); |
| 2019 | } |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2020 | |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 2021 | static int |
| 2022 | WindowTraverse(WindowObject *self, visitproc visit, void *arg) |
| 2023 | { |
| 2024 | Py_VISIT(((PyObject *)(self->tabObject))); |
| 2025 | return 0; |
| 2026 | } |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2027 | |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 2028 | static int |
| 2029 | WindowClear(WindowObject *self) |
| 2030 | { |
| 2031 | Py_CLEAR(self->tabObject); |
| 2032 | return 0; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2033 | } |
| 2034 | |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2035 | static win_T * |
| 2036 | get_firstwin(TabPageObject *tabObject) |
| 2037 | { |
| 2038 | if (tabObject) |
| 2039 | { |
| 2040 | if (CheckTabPage(tabObject)) |
| 2041 | return NULL; |
| 2042 | /* For current tab window.c does not bother to set or update tp_firstwin |
| 2043 | */ |
| 2044 | else if (tabObject->tab == curtab) |
| 2045 | return firstwin; |
| 2046 | else |
| 2047 | return tabObject->tab->tp_firstwin; |
| 2048 | } |
| 2049 | else |
| 2050 | return firstwin; |
| 2051 | } |
| 2052 | |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 2053 | static PyObject * |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 2054 | WindowAttrValid(WindowObject *self, char *name) |
| 2055 | { |
| 2056 | PyObject *r; |
| 2057 | |
| 2058 | if (strcmp(name, "valid") != 0) |
| 2059 | return NULL; |
| 2060 | |
| 2061 | r = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True); |
| 2062 | Py_INCREF(r); |
| 2063 | return r; |
| 2064 | } |
| 2065 | |
| 2066 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2067 | WindowAttr(WindowObject *self, char *name) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2068 | { |
| 2069 | if (strcmp(name, "buffer") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2070 | return (PyObject *)BufferNew(self->win->w_buffer); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2071 | else if (strcmp(name, "cursor") == 0) |
| 2072 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2073 | pos_T *pos = &self->win->w_cursor; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2074 | |
| 2075 | return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); |
| 2076 | } |
| 2077 | else if (strcmp(name, "height") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2078 | return PyLong_FromLong((long)(self->win->w_height)); |
Bram Moolenaar | 4e5dfb5 | 2013-05-12 19:30:31 +0200 | [diff] [blame] | 2079 | #ifdef FEAT_WINDOWS |
| 2080 | else if (strcmp(name, "row") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2081 | return PyLong_FromLong((long)(self->win->w_winrow)); |
Bram Moolenaar | 4e5dfb5 | 2013-05-12 19:30:31 +0200 | [diff] [blame] | 2082 | #endif |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2083 | #ifdef FEAT_VERTSPLIT |
| 2084 | else if (strcmp(name, "width") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2085 | return PyLong_FromLong((long)(W_WIDTH(self->win))); |
Bram Moolenaar | 4e5dfb5 | 2013-05-12 19:30:31 +0200 | [diff] [blame] | 2086 | else if (strcmp(name, "col") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2087 | return PyLong_FromLong((long)(W_WINCOL(self->win))); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2088 | #endif |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 2089 | else if (strcmp(name, "vars") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2090 | return DictionaryNew(self->win->w_vars); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 2091 | else if (strcmp(name, "options") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2092 | return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow, |
| 2093 | (PyObject *) self); |
Bram Moolenaar | 6d21645 | 2013-05-12 19:00:41 +0200 | [diff] [blame] | 2094 | else if (strcmp(name, "number") == 0) |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2095 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2096 | if (CheckTabPage(self->tabObject)) |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2097 | return NULL; |
| 2098 | return PyLong_FromLong((long) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2099 | get_win_number(self->win, get_firstwin(self->tabObject))); |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2100 | } |
| 2101 | else if (strcmp(name, "tabpage") == 0) |
| 2102 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2103 | Py_INCREF(self->tabObject); |
| 2104 | return (PyObject *)(self->tabObject); |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 2105 | } |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2106 | else if (strcmp(name,"__members__") == 0) |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 2107 | return Py_BuildValue("[ssssssssss]", "buffer", "cursor", "height", |
| 2108 | "vars", "options", "number", "row", "col", "tabpage", "valid"); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2109 | else |
| 2110 | return NULL; |
| 2111 | } |
| 2112 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2113 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2114 | WindowSetattr(WindowObject *self, char *name, PyObject *val) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2115 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2116 | if (CheckWindow(self)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2117 | return -1; |
| 2118 | |
| 2119 | if (strcmp(name, "buffer") == 0) |
| 2120 | { |
| 2121 | PyErr_SetString(PyExc_TypeError, _("readonly attribute")); |
| 2122 | return -1; |
| 2123 | } |
| 2124 | else if (strcmp(name, "cursor") == 0) |
| 2125 | { |
| 2126 | long lnum; |
| 2127 | long col; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2128 | |
| 2129 | if (!PyArg_Parse(val, "(ll)", &lnum, &col)) |
| 2130 | return -1; |
| 2131 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2132 | if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2133 | { |
| 2134 | PyErr_SetVim(_("cursor position outside buffer")); |
| 2135 | return -1; |
| 2136 | } |
| 2137 | |
| 2138 | /* Check for keyboard interrupts */ |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2139 | if (VimCheckInterrupt()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2140 | return -1; |
| 2141 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2142 | self->win->w_cursor.lnum = lnum; |
| 2143 | self->win->w_cursor.col = col; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2144 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2145 | self->win->w_cursor.coladd = 0; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2146 | #endif |
Bram Moolenaar | 03a807a | 2011-07-07 15:08:58 +0200 | [diff] [blame] | 2147 | /* When column is out of range silently correct it. */ |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2148 | check_cursor_col_win(self->win); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2149 | |
Bram Moolenaar | 03a807a | 2011-07-07 15:08:58 +0200 | [diff] [blame] | 2150 | update_screen(VALID); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2151 | return 0; |
| 2152 | } |
| 2153 | else if (strcmp(name, "height") == 0) |
| 2154 | { |
| 2155 | int height; |
| 2156 | win_T *savewin; |
| 2157 | |
| 2158 | if (!PyArg_Parse(val, "i", &height)) |
| 2159 | return -1; |
| 2160 | |
| 2161 | #ifdef FEAT_GUI |
| 2162 | need_mouse_correct = TRUE; |
| 2163 | #endif |
| 2164 | savewin = curwin; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2165 | curwin = self->win; |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2166 | |
| 2167 | VimTryStart(); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2168 | win_setheight(height); |
| 2169 | curwin = savewin; |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2170 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2171 | return -1; |
| 2172 | |
| 2173 | return 0; |
| 2174 | } |
| 2175 | #ifdef FEAT_VERTSPLIT |
| 2176 | else if (strcmp(name, "width") == 0) |
| 2177 | { |
| 2178 | int width; |
| 2179 | win_T *savewin; |
| 2180 | |
| 2181 | if (!PyArg_Parse(val, "i", &width)) |
| 2182 | return -1; |
| 2183 | |
| 2184 | #ifdef FEAT_GUI |
| 2185 | need_mouse_correct = TRUE; |
| 2186 | #endif |
| 2187 | savewin = curwin; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2188 | curwin = self->win; |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2189 | |
| 2190 | VimTryStart(); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2191 | win_setwidth(width); |
| 2192 | curwin = savewin; |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2193 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2194 | return -1; |
| 2195 | |
| 2196 | return 0; |
| 2197 | } |
| 2198 | #endif |
| 2199 | else |
| 2200 | { |
| 2201 | PyErr_SetString(PyExc_AttributeError, name); |
| 2202 | return -1; |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2207 | WindowRepr(WindowObject *self) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2208 | { |
| 2209 | static char repr[100]; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2210 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2211 | if (self->win == INVALID_WINDOW_VALUE) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2212 | { |
| 2213 | vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self)); |
| 2214 | return PyString_FromString(repr); |
| 2215 | } |
| 2216 | else |
| 2217 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2218 | int w = get_win_number(self->win, firstwin); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2219 | |
Bram Moolenaar | 6d21645 | 2013-05-12 19:00:41 +0200 | [diff] [blame] | 2220 | if (w == 0) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2221 | vim_snprintf(repr, 100, _("<window object (unknown) at %p>"), |
| 2222 | (self)); |
| 2223 | else |
Bram Moolenaar | 6d21645 | 2013-05-12 19:00:41 +0200 | [diff] [blame] | 2224 | vim_snprintf(repr, 100, _("<window %d>"), w - 1); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2225 | |
| 2226 | return PyString_FromString(repr); |
| 2227 | } |
| 2228 | } |
| 2229 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2230 | static struct PyMethodDef WindowMethods[] = { |
| 2231 | /* name, function, calling, documentation */ |
| 2232 | { NULL, NULL, 0, NULL } |
| 2233 | }; |
| 2234 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2235 | /* |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2236 | * Window list object |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2237 | */ |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2238 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2239 | static PyTypeObject WinListType; |
| 2240 | static PySequenceMethods WinListAsSeq; |
| 2241 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2242 | typedef struct |
| 2243 | { |
| 2244 | PyObject_HEAD |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2245 | TabPageObject *tabObject; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 2246 | } WinListObject; |
| 2247 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2248 | static PyObject * |
| 2249 | WinListNew(TabPageObject *tabObject) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2250 | { |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2251 | WinListObject *self; |
| 2252 | |
| 2253 | self = PyObject_NEW(WinListObject, &WinListType); |
| 2254 | self->tabObject = tabObject; |
| 2255 | Py_INCREF(tabObject); |
| 2256 | |
| 2257 | return (PyObject *)(self); |
| 2258 | } |
| 2259 | |
| 2260 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2261 | WinListDestructor(WinListObject *self) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2262 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2263 | TabPageObject *tabObject = self->tabObject; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2264 | |
| 2265 | if (tabObject) |
Bram Moolenaar | 425154d | 2013-05-24 18:58:43 +0200 | [diff] [blame] | 2266 | { |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2267 | Py_DECREF((PyObject *)(tabObject)); |
Bram Moolenaar | 425154d | 2013-05-24 18:58:43 +0200 | [diff] [blame] | 2268 | } |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2269 | |
| 2270 | DESTRUCTOR_FINISH(self); |
| 2271 | } |
| 2272 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2273 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2274 | WinListLength(WinListObject *self) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2275 | { |
| 2276 | win_T *w; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2277 | PyInt n = 0; |
| 2278 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2279 | if (!(w = get_firstwin(self->tabObject))) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2280 | return -1; |
| 2281 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2282 | while (w != NULL) |
| 2283 | { |
| 2284 | ++n; |
| 2285 | w = W_NEXT(w); |
| 2286 | } |
| 2287 | |
| 2288 | return n; |
| 2289 | } |
| 2290 | |
| 2291 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2292 | WinListItem(WinListObject *self, PyInt n) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2293 | { |
| 2294 | win_T *w; |
| 2295 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2296 | if (!(w = get_firstwin(self->tabObject))) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2297 | return NULL; |
| 2298 | |
| 2299 | for (; w != NULL; w = W_NEXT(w), --n) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2300 | if (n == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2301 | return WindowNew(w, self->tabObject? self->tabObject->tab: curtab); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2302 | |
| 2303 | PyErr_SetString(PyExc_IndexError, _("no such window")); |
| 2304 | return NULL; |
| 2305 | } |
| 2306 | |
| 2307 | /* Convert a Python string into a Vim line. |
| 2308 | * |
| 2309 | * The result is in allocated memory. All internal nulls are replaced by |
| 2310 | * newline characters. It is an error for the string to contain newline |
| 2311 | * characters. |
| 2312 | * |
| 2313 | * On errors, the Python exception data is set, and NULL is returned. |
| 2314 | */ |
| 2315 | static char * |
| 2316 | StringToLine(PyObject *obj) |
| 2317 | { |
| 2318 | const char *str; |
| 2319 | char *save; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2320 | PyObject *bytes; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2321 | PyInt len; |
| 2322 | PyInt i; |
| 2323 | char *p; |
| 2324 | |
| 2325 | if (obj == NULL || !PyString_Check(obj)) |
| 2326 | { |
| 2327 | PyErr_BadArgument(); |
| 2328 | return NULL; |
| 2329 | } |
| 2330 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2331 | bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */ |
| 2332 | str = PyString_AsString(bytes); |
| 2333 | len = PyString_Size(bytes); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2334 | |
| 2335 | /* |
| 2336 | * Error checking: String must not contain newlines, as we |
| 2337 | * are replacing a single line, and we must replace it with |
| 2338 | * a single line. |
| 2339 | * A trailing newline is removed, so that append(f.readlines()) works. |
| 2340 | */ |
| 2341 | p = memchr(str, '\n', len); |
| 2342 | if (p != NULL) |
| 2343 | { |
| 2344 | if (p == str + len - 1) |
| 2345 | --len; |
| 2346 | else |
| 2347 | { |
| 2348 | PyErr_SetVim(_("string cannot contain newlines")); |
| 2349 | return NULL; |
| 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | /* Create a copy of the string, with internal nulls replaced by |
| 2354 | * newline characters, as is the vim convention. |
| 2355 | */ |
| 2356 | save = (char *)alloc((unsigned)(len+1)); |
| 2357 | if (save == NULL) |
| 2358 | { |
| 2359 | PyErr_NoMemory(); |
| 2360 | return NULL; |
| 2361 | } |
| 2362 | |
| 2363 | for (i = 0; i < len; ++i) |
| 2364 | { |
| 2365 | if (str[i] == '\0') |
| 2366 | save[i] = '\n'; |
| 2367 | else |
| 2368 | save[i] = str[i]; |
| 2369 | } |
| 2370 | |
| 2371 | save[i] = '\0'; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2372 | PyString_FreeBytes(bytes); /* Python 2 does nothing here */ |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2373 | |
| 2374 | return save; |
| 2375 | } |
| 2376 | |
| 2377 | /* Get a line from the specified buffer. The line number is |
| 2378 | * in Vim format (1-based). The line is returned as a Python |
| 2379 | * string object. |
| 2380 | */ |
| 2381 | static PyObject * |
| 2382 | GetBufferLine(buf_T *buf, PyInt n) |
| 2383 | { |
| 2384 | return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE)); |
| 2385 | } |
| 2386 | |
| 2387 | |
| 2388 | /* Get a list of lines from the specified buffer. The line numbers |
| 2389 | * are in Vim format (1-based). The range is from lo up to, but not |
| 2390 | * including, hi. The list is returned as a Python list of string objects. |
| 2391 | */ |
| 2392 | static PyObject * |
| 2393 | GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi) |
| 2394 | { |
| 2395 | PyInt i; |
| 2396 | PyInt n = hi - lo; |
| 2397 | PyObject *list = PyList_New(n); |
| 2398 | |
| 2399 | if (list == NULL) |
| 2400 | return NULL; |
| 2401 | |
| 2402 | for (i = 0; i < n; ++i) |
| 2403 | { |
| 2404 | PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE)); |
| 2405 | |
| 2406 | /* Error check - was the Python string creation OK? */ |
| 2407 | if (str == NULL) |
| 2408 | { |
| 2409 | Py_DECREF(list); |
| 2410 | return NULL; |
| 2411 | } |
| 2412 | |
| 2413 | /* Set the list item */ |
| 2414 | if (PyList_SetItem(list, i, str)) |
| 2415 | { |
| 2416 | Py_DECREF(str); |
| 2417 | Py_DECREF(list); |
| 2418 | return NULL; |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | /* The ownership of the Python list is passed to the caller (ie, |
| 2423 | * the caller should Py_DECREF() the object when it is finished |
| 2424 | * with it). |
| 2425 | */ |
| 2426 | |
| 2427 | return list; |
| 2428 | } |
| 2429 | |
| 2430 | /* |
| 2431 | * Check if deleting lines made the cursor position invalid. |
| 2432 | * Changed the lines from "lo" to "hi" and added "extra" lines (negative if |
| 2433 | * deleted). |
| 2434 | */ |
| 2435 | static void |
| 2436 | py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) |
| 2437 | { |
| 2438 | if (curwin->w_cursor.lnum >= lo) |
| 2439 | { |
| 2440 | /* Adjust the cursor position if it's in/after the changed |
| 2441 | * lines. */ |
| 2442 | if (curwin->w_cursor.lnum >= hi) |
| 2443 | { |
| 2444 | curwin->w_cursor.lnum += extra; |
| 2445 | check_cursor_col(); |
| 2446 | } |
| 2447 | else if (extra < 0) |
| 2448 | { |
| 2449 | curwin->w_cursor.lnum = lo; |
| 2450 | check_cursor(); |
| 2451 | } |
| 2452 | else |
| 2453 | check_cursor_col(); |
| 2454 | changed_cline_bef_curs(); |
| 2455 | } |
| 2456 | invalidate_botline(); |
| 2457 | } |
| 2458 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2459 | /* |
| 2460 | * Replace a line in the specified buffer. The line number is |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2461 | * in Vim format (1-based). The replacement line is given as |
| 2462 | * a Python string object. The object is checked for validity |
| 2463 | * and correct format. Errors are returned as a value of FAIL. |
| 2464 | * The return value is OK on success. |
| 2465 | * If OK is returned and len_change is not NULL, *len_change |
| 2466 | * is set to the change in the buffer length. |
| 2467 | */ |
| 2468 | static int |
| 2469 | SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change) |
| 2470 | { |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2471 | /* First of all, we check the type of the supplied Python object. |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2472 | * There are three cases: |
| 2473 | * 1. NULL, or None - this is a deletion. |
| 2474 | * 2. A string - this is a replacement. |
| 2475 | * 3. Anything else - this is an error. |
| 2476 | */ |
| 2477 | if (line == Py_None || line == NULL) |
| 2478 | { |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2479 | buf_T *savebuf; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2480 | |
| 2481 | PyErr_Clear(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2482 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2483 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2484 | VimTryStart(); |
| 2485 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2486 | if (u_savedel((linenr_T)n, 1L) == FAIL) |
| 2487 | PyErr_SetVim(_("cannot save undo information")); |
| 2488 | else if (ml_delete((linenr_T)n, FALSE) == FAIL) |
| 2489 | PyErr_SetVim(_("cannot delete line")); |
| 2490 | else |
| 2491 | { |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2492 | if (buf == savebuf) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2493 | py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); |
| 2494 | deleted_lines_mark((linenr_T)n, 1L); |
| 2495 | } |
| 2496 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2497 | restore_buffer(savebuf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2498 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2499 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2500 | return FAIL; |
| 2501 | |
| 2502 | if (len_change) |
| 2503 | *len_change = -1; |
| 2504 | |
| 2505 | return OK; |
| 2506 | } |
| 2507 | else if (PyString_Check(line)) |
| 2508 | { |
| 2509 | char *save = StringToLine(line); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2510 | buf_T *savebuf; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2511 | |
| 2512 | if (save == NULL) |
| 2513 | return FAIL; |
| 2514 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2515 | VimTryStart(); |
| 2516 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2517 | /* We do not need to free "save" if ml_replace() consumes it. */ |
| 2518 | PyErr_Clear(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2519 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2520 | |
| 2521 | if (u_savesub((linenr_T)n) == FAIL) |
| 2522 | { |
| 2523 | PyErr_SetVim(_("cannot save undo information")); |
| 2524 | vim_free(save); |
| 2525 | } |
| 2526 | else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL) |
| 2527 | { |
| 2528 | PyErr_SetVim(_("cannot replace line")); |
| 2529 | vim_free(save); |
| 2530 | } |
| 2531 | else |
| 2532 | changed_bytes((linenr_T)n, 0); |
| 2533 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2534 | restore_buffer(savebuf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2535 | |
| 2536 | /* Check that the cursor is not beyond the end of the line now. */ |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2537 | if (buf == savebuf) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2538 | check_cursor_col(); |
| 2539 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2540 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2541 | return FAIL; |
| 2542 | |
| 2543 | if (len_change) |
| 2544 | *len_change = 0; |
| 2545 | |
| 2546 | return OK; |
| 2547 | } |
| 2548 | else |
| 2549 | { |
| 2550 | PyErr_BadArgument(); |
| 2551 | return FAIL; |
| 2552 | } |
| 2553 | } |
| 2554 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2555 | /* Replace a range of lines in the specified buffer. The line numbers are in |
| 2556 | * Vim format (1-based). The range is from lo up to, but not including, hi. |
| 2557 | * The replacement lines are given as a Python list of string objects. The |
| 2558 | * list is checked for validity and correct format. Errors are returned as a |
| 2559 | * value of FAIL. The return value is OK on success. |
| 2560 | * If OK is returned and len_change is not NULL, *len_change |
| 2561 | * is set to the change in the buffer length. |
| 2562 | */ |
| 2563 | static int |
| 2564 | SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change) |
| 2565 | { |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2566 | /* First of all, we check the type of the supplied Python object. |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2567 | * There are three cases: |
| 2568 | * 1. NULL, or None - this is a deletion. |
| 2569 | * 2. A list - this is a replacement. |
| 2570 | * 3. Anything else - this is an error. |
| 2571 | */ |
| 2572 | if (list == Py_None || list == NULL) |
| 2573 | { |
| 2574 | PyInt i; |
| 2575 | PyInt n = (int)(hi - lo); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2576 | buf_T *savebuf; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2577 | |
| 2578 | PyErr_Clear(); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2579 | VimTryStart(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2580 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2581 | |
| 2582 | if (u_savedel((linenr_T)lo, (long)n) == FAIL) |
| 2583 | PyErr_SetVim(_("cannot save undo information")); |
| 2584 | else |
| 2585 | { |
| 2586 | for (i = 0; i < n; ++i) |
| 2587 | { |
| 2588 | if (ml_delete((linenr_T)lo, FALSE) == FAIL) |
| 2589 | { |
| 2590 | PyErr_SetVim(_("cannot delete line")); |
| 2591 | break; |
| 2592 | } |
| 2593 | } |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2594 | if (buf == savebuf) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2595 | py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); |
| 2596 | deleted_lines_mark((linenr_T)lo, (long)i); |
| 2597 | } |
| 2598 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2599 | restore_buffer(savebuf); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2600 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2601 | if (VimTryEnd()) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2602 | return FAIL; |
| 2603 | |
| 2604 | if (len_change) |
| 2605 | *len_change = -n; |
| 2606 | |
| 2607 | return OK; |
| 2608 | } |
| 2609 | else if (PyList_Check(list)) |
| 2610 | { |
| 2611 | PyInt i; |
| 2612 | PyInt new_len = PyList_Size(list); |
| 2613 | PyInt old_len = hi - lo; |
| 2614 | PyInt extra = 0; /* lines added to text, can be negative */ |
| 2615 | char **array; |
| 2616 | buf_T *savebuf; |
| 2617 | |
| 2618 | if (new_len == 0) /* avoid allocating zero bytes */ |
| 2619 | array = NULL; |
| 2620 | else |
| 2621 | { |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2622 | array = PyMem_New(char *, new_len); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2623 | if (array == NULL) |
| 2624 | { |
| 2625 | PyErr_NoMemory(); |
| 2626 | return FAIL; |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | for (i = 0; i < new_len; ++i) |
| 2631 | { |
| 2632 | PyObject *line = PyList_GetItem(list, i); |
| 2633 | |
| 2634 | array[i] = StringToLine(line); |
| 2635 | if (array[i] == NULL) |
| 2636 | { |
| 2637 | while (i) |
| 2638 | vim_free(array[--i]); |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2639 | PyMem_Free(array); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2640 | return FAIL; |
| 2641 | } |
| 2642 | } |
| 2643 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2644 | VimTryStart(); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2645 | PyErr_Clear(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2646 | |
| 2647 | // START of region without "return". Must call restore_buffer()! |
| 2648 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2649 | |
| 2650 | if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL) |
| 2651 | PyErr_SetVim(_("cannot save undo information")); |
| 2652 | |
| 2653 | /* If the size of the range is reducing (ie, new_len < old_len) we |
| 2654 | * need to delete some old_len. We do this at the start, by |
| 2655 | * repeatedly deleting line "lo". |
| 2656 | */ |
| 2657 | if (!PyErr_Occurred()) |
| 2658 | { |
| 2659 | for (i = 0; i < old_len - new_len; ++i) |
| 2660 | if (ml_delete((linenr_T)lo, FALSE) == FAIL) |
| 2661 | { |
| 2662 | PyErr_SetVim(_("cannot delete line")); |
| 2663 | break; |
| 2664 | } |
| 2665 | extra -= i; |
| 2666 | } |
| 2667 | |
| 2668 | /* For as long as possible, replace the existing old_len with the |
| 2669 | * new old_len. This is a more efficient operation, as it requires |
| 2670 | * less memory allocation and freeing. |
| 2671 | */ |
| 2672 | if (!PyErr_Occurred()) |
| 2673 | { |
| 2674 | for (i = 0; i < old_len && i < new_len; ++i) |
| 2675 | if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE) |
| 2676 | == FAIL) |
| 2677 | { |
| 2678 | PyErr_SetVim(_("cannot replace line")); |
| 2679 | break; |
| 2680 | } |
| 2681 | } |
| 2682 | else |
| 2683 | i = 0; |
| 2684 | |
| 2685 | /* Now we may need to insert the remaining new old_len. If we do, we |
| 2686 | * must free the strings as we finish with them (we can't pass the |
| 2687 | * responsibility to vim in this case). |
| 2688 | */ |
| 2689 | if (!PyErr_Occurred()) |
| 2690 | { |
| 2691 | while (i < new_len) |
| 2692 | { |
| 2693 | if (ml_append((linenr_T)(lo + i - 1), |
| 2694 | (char_u *)array[i], 0, FALSE) == FAIL) |
| 2695 | { |
| 2696 | PyErr_SetVim(_("cannot insert line")); |
| 2697 | break; |
| 2698 | } |
| 2699 | vim_free(array[i]); |
| 2700 | ++i; |
| 2701 | ++extra; |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | /* Free any left-over old_len, as a result of an error */ |
| 2706 | while (i < new_len) |
| 2707 | { |
| 2708 | vim_free(array[i]); |
| 2709 | ++i; |
| 2710 | } |
| 2711 | |
| 2712 | /* Free the array of old_len. All of its contents have now |
| 2713 | * been dealt with (either freed, or the responsibility passed |
| 2714 | * to vim. |
| 2715 | */ |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2716 | PyMem_Free(array); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2717 | |
| 2718 | /* Adjust marks. Invalidate any which lie in the |
| 2719 | * changed range, and move any in the remainder of the buffer. |
| 2720 | */ |
| 2721 | mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), |
| 2722 | (long)MAXLNUM, (long)extra); |
| 2723 | changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); |
| 2724 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2725 | if (buf == savebuf) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2726 | py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); |
| 2727 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2728 | // END of region without "return". |
| 2729 | restore_buffer(savebuf); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2730 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2731 | if (VimTryEnd()) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2732 | return FAIL; |
| 2733 | |
| 2734 | if (len_change) |
| 2735 | *len_change = new_len - old_len; |
| 2736 | |
| 2737 | return OK; |
| 2738 | } |
| 2739 | else |
| 2740 | { |
| 2741 | PyErr_BadArgument(); |
| 2742 | return FAIL; |
| 2743 | } |
| 2744 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2745 | |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 2746 | /* Insert a number of lines into the specified buffer after the specified line. |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2747 | * The line number is in Vim format (1-based). The lines to be inserted are |
| 2748 | * given as a Python list of string objects or as a single string. The lines |
| 2749 | * to be added are checked for validity and correct format. Errors are |
| 2750 | * returned as a value of FAIL. The return value is OK on success. |
| 2751 | * If OK is returned and len_change is not NULL, *len_change |
| 2752 | * is set to the change in the buffer length. |
| 2753 | */ |
| 2754 | static int |
| 2755 | InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) |
| 2756 | { |
| 2757 | /* First of all, we check the type of the supplied Python object. |
| 2758 | * It must be a string or a list, or the call is in error. |
| 2759 | */ |
| 2760 | if (PyString_Check(lines)) |
| 2761 | { |
| 2762 | char *str = StringToLine(lines); |
| 2763 | buf_T *savebuf; |
| 2764 | |
| 2765 | if (str == NULL) |
| 2766 | return FAIL; |
| 2767 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2768 | PyErr_Clear(); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2769 | VimTryStart(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2770 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2771 | |
| 2772 | if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL) |
| 2773 | PyErr_SetVim(_("cannot save undo information")); |
| 2774 | else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) |
| 2775 | PyErr_SetVim(_("cannot insert line")); |
| 2776 | else |
| 2777 | appended_lines_mark((linenr_T)n, 1L); |
| 2778 | |
| 2779 | vim_free(str); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2780 | restore_buffer(savebuf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2781 | update_screen(VALID); |
| 2782 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2783 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2784 | return FAIL; |
| 2785 | |
| 2786 | if (len_change) |
| 2787 | *len_change = 1; |
| 2788 | |
| 2789 | return OK; |
| 2790 | } |
| 2791 | else if (PyList_Check(lines)) |
| 2792 | { |
| 2793 | PyInt i; |
| 2794 | PyInt size = PyList_Size(lines); |
| 2795 | char **array; |
| 2796 | buf_T *savebuf; |
| 2797 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2798 | array = PyMem_New(char *, size); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2799 | if (array == NULL) |
| 2800 | { |
| 2801 | PyErr_NoMemory(); |
| 2802 | return FAIL; |
| 2803 | } |
| 2804 | |
| 2805 | for (i = 0; i < size; ++i) |
| 2806 | { |
| 2807 | PyObject *line = PyList_GetItem(lines, i); |
| 2808 | array[i] = StringToLine(line); |
| 2809 | |
| 2810 | if (array[i] == NULL) |
| 2811 | { |
| 2812 | while (i) |
| 2813 | vim_free(array[--i]); |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2814 | PyMem_Free(array); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2815 | return FAIL; |
| 2816 | } |
| 2817 | } |
| 2818 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2819 | PyErr_Clear(); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2820 | VimTryStart(); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2821 | switch_buffer(&savebuf, buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2822 | |
| 2823 | if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) |
| 2824 | PyErr_SetVim(_("cannot save undo information")); |
| 2825 | else |
| 2826 | { |
| 2827 | for (i = 0; i < size; ++i) |
| 2828 | { |
| 2829 | if (ml_append((linenr_T)(n + i), |
| 2830 | (char_u *)array[i], 0, FALSE) == FAIL) |
| 2831 | { |
| 2832 | PyErr_SetVim(_("cannot insert line")); |
| 2833 | |
| 2834 | /* Free the rest of the lines */ |
| 2835 | while (i < size) |
| 2836 | vim_free(array[i++]); |
| 2837 | |
| 2838 | break; |
| 2839 | } |
| 2840 | vim_free(array[i]); |
| 2841 | } |
| 2842 | if (i > 0) |
| 2843 | appended_lines_mark((linenr_T)n, (long)i); |
| 2844 | } |
| 2845 | |
| 2846 | /* Free the array of lines. All of its contents have now |
| 2847 | * been freed. |
| 2848 | */ |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 2849 | PyMem_Free(array); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2850 | |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 2851 | restore_buffer(savebuf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2852 | update_screen(VALID); |
| 2853 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 2854 | if (VimTryEnd()) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2855 | return FAIL; |
| 2856 | |
| 2857 | if (len_change) |
| 2858 | *len_change = size; |
| 2859 | |
| 2860 | return OK; |
| 2861 | } |
| 2862 | else |
| 2863 | { |
| 2864 | PyErr_BadArgument(); |
| 2865 | return FAIL; |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | /* |
| 2870 | * Common routines for buffers and line ranges |
| 2871 | * ------------------------------------------- |
| 2872 | */ |
| 2873 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 2874 | typedef struct |
| 2875 | { |
| 2876 | PyObject_HEAD |
| 2877 | buf_T *buf; |
| 2878 | } BufferObject; |
| 2879 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2880 | static int |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2881 | CheckBuffer(BufferObject *self) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2882 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 2883 | if (self->buf == INVALID_BUFFER_VALUE) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2884 | { |
| 2885 | PyErr_SetVim(_("attempt to refer to deleted buffer")); |
| 2886 | return -1; |
| 2887 | } |
| 2888 | |
| 2889 | return 0; |
| 2890 | } |
| 2891 | |
| 2892 | static PyObject * |
| 2893 | RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end) |
| 2894 | { |
| 2895 | if (CheckBuffer(self)) |
| 2896 | return NULL; |
| 2897 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 2898 | if (end == -1) |
| 2899 | end = self->buf->b_ml.ml_line_count; |
| 2900 | |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 2901 | if (n < 0) |
| 2902 | n += end - start + 1; |
| 2903 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2904 | if (n < 0 || n > end - start) |
| 2905 | { |
| 2906 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 2907 | return NULL; |
| 2908 | } |
| 2909 | |
| 2910 | return GetBufferLine(self->buf, n+start); |
| 2911 | } |
| 2912 | |
| 2913 | static PyObject * |
| 2914 | RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end) |
| 2915 | { |
| 2916 | PyInt size; |
| 2917 | |
| 2918 | if (CheckBuffer(self)) |
| 2919 | return NULL; |
| 2920 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 2921 | if (end == -1) |
| 2922 | end = self->buf->b_ml.ml_line_count; |
| 2923 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2924 | size = end - start + 1; |
| 2925 | |
| 2926 | if (lo < 0) |
| 2927 | lo = 0; |
| 2928 | else if (lo > size) |
| 2929 | lo = size; |
| 2930 | if (hi < 0) |
| 2931 | hi = 0; |
| 2932 | if (hi < lo) |
| 2933 | hi = lo; |
| 2934 | else if (hi > size) |
| 2935 | hi = size; |
| 2936 | |
| 2937 | return GetBufferLineList(self->buf, lo+start, hi+start); |
| 2938 | } |
| 2939 | |
| 2940 | static PyInt |
| 2941 | RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end) |
| 2942 | { |
| 2943 | PyInt len_change; |
| 2944 | |
| 2945 | if (CheckBuffer(self)) |
| 2946 | return -1; |
| 2947 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 2948 | if (end == -1) |
| 2949 | end = self->buf->b_ml.ml_line_count; |
| 2950 | |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 2951 | if (n < 0) |
| 2952 | n += end - start + 1; |
| 2953 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2954 | if (n < 0 || n > end - start) |
| 2955 | { |
| 2956 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 2957 | return -1; |
| 2958 | } |
| 2959 | |
| 2960 | if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) |
| 2961 | return -1; |
| 2962 | |
| 2963 | if (new_end) |
| 2964 | *new_end = end + len_change; |
| 2965 | |
| 2966 | return 0; |
| 2967 | } |
| 2968 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2969 | static PyInt |
| 2970 | RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end) |
| 2971 | { |
| 2972 | PyInt size; |
| 2973 | PyInt len_change; |
| 2974 | |
| 2975 | /* Self must be a valid buffer */ |
| 2976 | if (CheckBuffer(self)) |
| 2977 | return -1; |
| 2978 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 2979 | if (end == -1) |
| 2980 | end = self->buf->b_ml.ml_line_count; |
| 2981 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2982 | /* Sort out the slice range */ |
| 2983 | size = end - start + 1; |
| 2984 | |
| 2985 | if (lo < 0) |
| 2986 | lo = 0; |
| 2987 | else if (lo > size) |
| 2988 | lo = size; |
| 2989 | if (hi < 0) |
| 2990 | hi = 0; |
| 2991 | if (hi < lo) |
| 2992 | hi = lo; |
| 2993 | else if (hi > size) |
| 2994 | hi = size; |
| 2995 | |
| 2996 | if (SetBufferLineList(self->buf, lo + start, hi + start, |
| 2997 | val, &len_change) == FAIL) |
| 2998 | return -1; |
| 2999 | |
| 3000 | if (new_end) |
| 3001 | *new_end = end + len_change; |
| 3002 | |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3006 | |
| 3007 | static PyObject * |
| 3008 | RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end) |
| 3009 | { |
| 3010 | PyObject *lines; |
| 3011 | PyInt len_change; |
| 3012 | PyInt max; |
| 3013 | PyInt n; |
| 3014 | |
| 3015 | if (CheckBuffer(self)) |
| 3016 | return NULL; |
| 3017 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 3018 | if (end == -1) |
| 3019 | end = self->buf->b_ml.ml_line_count; |
| 3020 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3021 | max = n = end - start + 1; |
| 3022 | |
| 3023 | if (!PyArg_ParseTuple(args, "O|n", &lines, &n)) |
| 3024 | return NULL; |
| 3025 | |
| 3026 | if (n < 0 || n > max) |
| 3027 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 3028 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3029 | return NULL; |
| 3030 | } |
| 3031 | |
| 3032 | if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL) |
| 3033 | return NULL; |
| 3034 | |
| 3035 | if (new_end) |
| 3036 | *new_end = end + len_change; |
| 3037 | |
| 3038 | Py_INCREF(Py_None); |
| 3039 | return Py_None; |
| 3040 | } |
| 3041 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3042 | /* Range object |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3043 | */ |
| 3044 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3045 | static PyTypeObject RangeType; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3046 | static PySequenceMethods RangeAsSeq; |
| 3047 | static PyMappingMethods RangeAsMapping; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3048 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3049 | typedef struct |
| 3050 | { |
| 3051 | PyObject_HEAD |
| 3052 | BufferObject *buf; |
| 3053 | PyInt start; |
| 3054 | PyInt end; |
| 3055 | } RangeObject; |
| 3056 | |
| 3057 | static PyObject * |
| 3058 | RangeNew(buf_T *buf, PyInt start, PyInt end) |
| 3059 | { |
| 3060 | BufferObject *bufr; |
| 3061 | RangeObject *self; |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 3062 | self = PyObject_GC_New(RangeObject, &RangeType); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3063 | if (self == NULL) |
| 3064 | return NULL; |
| 3065 | |
| 3066 | bufr = (BufferObject *)BufferNew(buf); |
| 3067 | if (bufr == NULL) |
| 3068 | { |
| 3069 | Py_DECREF(self); |
| 3070 | return NULL; |
| 3071 | } |
| 3072 | Py_INCREF(bufr); |
| 3073 | |
| 3074 | self->buf = bufr; |
| 3075 | self->start = start; |
| 3076 | self->end = end; |
| 3077 | |
| 3078 | return (PyObject *)(self); |
| 3079 | } |
| 3080 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3081 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3082 | RangeDestructor(RangeObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3083 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 3084 | PyObject_GC_UnTrack((void *)(self)); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3085 | Py_XDECREF(self->buf); |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 3086 | PyObject_GC_Del((void *)(self)); |
| 3087 | } |
| 3088 | |
| 3089 | static int |
| 3090 | RangeTraverse(RangeObject *self, visitproc visit, void *arg) |
| 3091 | { |
| 3092 | Py_VISIT(((PyObject *)(self->buf))); |
| 3093 | return 0; |
| 3094 | } |
| 3095 | |
| 3096 | static int |
| 3097 | RangeClear(RangeObject *self) |
| 3098 | { |
| 3099 | Py_CLEAR(self->buf); |
| 3100 | return 0; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3101 | } |
| 3102 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3103 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3104 | RangeLength(RangeObject *self) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3105 | { |
| 3106 | /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3107 | if (CheckBuffer(self->buf)) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3108 | return -1; /* ??? */ |
| 3109 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3110 | return (self->end - self->start + 1); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3114 | RangeItem(RangeObject *self, PyInt n) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3115 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3116 | return RBItem(self->buf, n, self->start, self->end); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3120 | RangeSlice(RangeObject *self, PyInt lo, PyInt hi) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3121 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3122 | return RBSlice(self->buf, lo, hi, self->start, self->end); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3126 | RangeAppend(RangeObject *self, PyObject *args) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3127 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3128 | return RBAppend(self->buf, args, self->start, self->end, &self->end); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3132 | RangeRepr(RangeObject *self) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3133 | { |
| 3134 | static char repr[100]; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3135 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3136 | if (self->buf->buf == INVALID_BUFFER_VALUE) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3137 | { |
| 3138 | vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>", |
| 3139 | (self)); |
| 3140 | return PyString_FromString(repr); |
| 3141 | } |
| 3142 | else |
| 3143 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3144 | char *name = (char *)self->buf->buf->b_fname; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3145 | int len; |
| 3146 | |
| 3147 | if (name == NULL) |
| 3148 | name = ""; |
| 3149 | len = (int)strlen(name); |
| 3150 | |
| 3151 | if (len > 45) |
| 3152 | name = name + (45 - len); |
| 3153 | |
| 3154 | vim_snprintf(repr, 100, "<range %s%s (%d:%d)>", |
| 3155 | len > 45 ? "..." : "", name, |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3156 | self->start, self->end); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3157 | |
| 3158 | return PyString_FromString(repr); |
| 3159 | } |
| 3160 | } |
| 3161 | |
| 3162 | static struct PyMethodDef RangeMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 3163 | /* name, function, calling, documentation */ |
| 3164 | {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" }, |
| 3165 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3166 | }; |
| 3167 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3168 | static PyTypeObject BufferType; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3169 | static PySequenceMethods BufferAsSeq; |
| 3170 | static PyMappingMethods BufferAsMapping; |
| 3171 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3172 | static PyObject * |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3173 | BufferNew(buf_T *buf) |
| 3174 | { |
| 3175 | /* We need to handle deletion of buffers underneath us. |
| 3176 | * If we add a "b_python*_ref" field to the buf_T structure, |
| 3177 | * then we can get at it in buf_freeall() in vim. We then |
| 3178 | * need to create only ONE Python object per buffer - if |
| 3179 | * we try to create a second, just INCREF the existing one |
| 3180 | * and return it. The (single) Python object referring to |
| 3181 | * the buffer is stored in "b_python*_ref". |
| 3182 | * Question: what to do on a buf_freeall(). We'll probably |
| 3183 | * have to either delete the Python object (DECREF it to |
| 3184 | * zero - a bad idea, as it leaves dangling refs!) or |
| 3185 | * set the buf_T * value to an invalid value (-1?), which |
| 3186 | * means we need checks in all access functions... Bah. |
| 3187 | * |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3188 | * Python2 and Python3 get different fields and different objects: |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3189 | * b_python_ref and b_python3_ref fields respectively. |
| 3190 | */ |
| 3191 | |
| 3192 | BufferObject *self; |
| 3193 | |
| 3194 | if (BUF_PYTHON_REF(buf) != NULL) |
| 3195 | { |
| 3196 | self = BUF_PYTHON_REF(buf); |
| 3197 | Py_INCREF(self); |
| 3198 | } |
| 3199 | else |
| 3200 | { |
| 3201 | self = PyObject_NEW(BufferObject, &BufferType); |
| 3202 | if (self == NULL) |
| 3203 | return NULL; |
| 3204 | self->buf = buf; |
| 3205 | BUF_PYTHON_REF(buf) = self; |
| 3206 | } |
| 3207 | |
| 3208 | return (PyObject *)(self); |
| 3209 | } |
| 3210 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3211 | static void |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3212 | BufferDestructor(BufferObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3213 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3214 | if (self->buf && self->buf != INVALID_BUFFER_VALUE) |
| 3215 | BUF_PYTHON_REF(self->buf) = NULL; |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3216 | |
| 3217 | DESTRUCTOR_FINISH(self); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3218 | } |
| 3219 | |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3220 | static PyInt |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3221 | BufferLength(BufferObject *self) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3222 | { |
| 3223 | /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3224 | if (CheckBuffer(self)) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3225 | return -1; /* ??? */ |
| 3226 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3227 | return (PyInt)(self->buf->b_ml.ml_line_count); |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3231 | BufferItem(BufferObject *self, PyInt n) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3232 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3233 | return RBItem(self, n, 1, -1); |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3237 | BufferSlice(BufferObject *self, PyInt lo, PyInt hi) |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3238 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3239 | return RBSlice(self, lo, hi, 1, -1); |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 3240 | } |
| 3241 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3242 | static PyObject * |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 3243 | BufferAttrValid(BufferObject *self, char *name) |
| 3244 | { |
| 3245 | PyObject *r; |
| 3246 | |
| 3247 | if (strcmp(name, "valid") != 0) |
| 3248 | return NULL; |
| 3249 | |
| 3250 | r = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True); |
| 3251 | Py_INCREF(r); |
| 3252 | return r; |
| 3253 | } |
| 3254 | |
| 3255 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3256 | BufferAttr(BufferObject *self, char *name) |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3257 | { |
| 3258 | if (strcmp(name, "name") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3259 | return Py_BuildValue("s", self->buf->b_ffname); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3260 | else if (strcmp(name, "number") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3261 | return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3262 | else if (strcmp(name, "vars") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3263 | return DictionaryNew(self->buf->b_vars); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3264 | else if (strcmp(name, "options") == 0) |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3265 | return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer, |
| 3266 | (PyObject *) self); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3267 | else if (strcmp(name,"__members__") == 0) |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 3268 | return Py_BuildValue("[sssss]", "name", "number", "vars", "options", |
| 3269 | "valid"); |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3270 | else |
| 3271 | return NULL; |
| 3272 | } |
| 3273 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 3274 | static int |
| 3275 | BufferSetattr(BufferObject *self, char *name, PyObject *valObject) |
| 3276 | { |
| 3277 | if (CheckBuffer(self)) |
| 3278 | return -1; |
| 3279 | |
| 3280 | if (strcmp(name, "name") == 0) |
| 3281 | { |
| 3282 | char_u *val; |
| 3283 | aco_save_T aco; |
| 3284 | int r; |
| 3285 | PyObject *todecref; |
| 3286 | |
| 3287 | if (!(val = StringToChars(valObject, &todecref))) |
| 3288 | return -1; |
| 3289 | |
| 3290 | VimTryStart(); |
| 3291 | /* Using aucmd_*: autocommands will be executed by rename_buffer */ |
| 3292 | aucmd_prepbuf(&aco, self->buf); |
| 3293 | r = rename_buffer(val); |
| 3294 | aucmd_restbuf(&aco); |
| 3295 | Py_XDECREF(todecref); |
| 3296 | if (VimTryEnd()) |
| 3297 | return -1; |
| 3298 | |
| 3299 | if (r == FAIL) |
| 3300 | { |
| 3301 | PyErr_SetVim(_("failed to rename buffer")); |
| 3302 | return -1; |
| 3303 | } |
| 3304 | return 0; |
| 3305 | } |
| 3306 | else |
| 3307 | { |
| 3308 | PyErr_SetString(PyExc_AttributeError, name); |
| 3309 | return -1; |
| 3310 | } |
| 3311 | } |
| 3312 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3313 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3314 | BufferAppend(BufferObject *self, PyObject *args) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3315 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3316 | return RBAppend(self, args, 1, -1, NULL); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3320 | BufferMark(BufferObject *self, PyObject *args) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3321 | { |
| 3322 | pos_T *posp; |
| 3323 | char *pmark; |
| 3324 | char mark; |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 3325 | buf_T *savebuf; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3326 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3327 | if (CheckBuffer(self)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3328 | return NULL; |
| 3329 | |
| 3330 | if (!PyArg_ParseTuple(args, "s", &pmark)) |
| 3331 | return NULL; |
| 3332 | mark = *pmark; |
| 3333 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3334 | VimTryStart(); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3335 | switch_buffer(&savebuf, self->buf); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3336 | posp = getmark(mark, FALSE); |
Bram Moolenaar | 105bc35 | 2013-05-17 16:03:57 +0200 | [diff] [blame] | 3337 | restore_buffer(savebuf); |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3338 | if (VimTryEnd()) |
| 3339 | return NULL; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3340 | |
| 3341 | if (posp == NULL) |
| 3342 | { |
| 3343 | PyErr_SetVim(_("invalid mark name")); |
| 3344 | return NULL; |
| 3345 | } |
| 3346 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3347 | if (posp->lnum <= 0) |
| 3348 | { |
| 3349 | /* Or raise an error? */ |
| 3350 | Py_INCREF(Py_None); |
| 3351 | return Py_None; |
| 3352 | } |
| 3353 | |
| 3354 | return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col)); |
| 3355 | } |
| 3356 | |
| 3357 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3358 | BufferRange(BufferObject *self, PyObject *args) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3359 | { |
| 3360 | PyInt start; |
| 3361 | PyInt end; |
| 3362 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3363 | if (CheckBuffer(self)) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3364 | return NULL; |
| 3365 | |
| 3366 | if (!PyArg_ParseTuple(args, "nn", &start, &end)) |
| 3367 | return NULL; |
| 3368 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3369 | return RangeNew(self->buf, start, end); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3370 | } |
| 3371 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3372 | static PyObject * |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3373 | BufferRepr(BufferObject *self) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3374 | { |
| 3375 | static char repr[100]; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3376 | |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3377 | if (self->buf == INVALID_BUFFER_VALUE) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3378 | { |
| 3379 | vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); |
| 3380 | return PyString_FromString(repr); |
| 3381 | } |
| 3382 | else |
| 3383 | { |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 3384 | char *name = (char *)self->buf->b_fname; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3385 | PyInt len; |
| 3386 | |
| 3387 | if (name == NULL) |
| 3388 | name = ""; |
| 3389 | len = strlen(name); |
| 3390 | |
| 3391 | if (len > 35) |
| 3392 | name = name + (35 - len); |
| 3393 | |
| 3394 | vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); |
| 3395 | |
| 3396 | return PyString_FromString(repr); |
| 3397 | } |
| 3398 | } |
| 3399 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3400 | static struct PyMethodDef BufferMethods[] = { |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 3401 | /* name, function, calling, documentation */ |
| 3402 | {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" }, |
| 3403 | {"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" }, |
| 3404 | {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" }, |
Bram Moolenaar | 7f85d29 | 2012-02-04 20:17:26 +0100 | [diff] [blame] | 3405 | #if PY_VERSION_HEX >= 0x03000000 |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 3406 | {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, "List buffer attributes" }, |
Bram Moolenaar | 7f85d29 | 2012-02-04 20:17:26 +0100 | [diff] [blame] | 3407 | #endif |
Bram Moolenaar | 182dc4f | 2013-05-21 19:01:55 +0200 | [diff] [blame] | 3408 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3409 | }; |
| 3410 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3411 | /* |
| 3412 | * Buffer list object - Implementation |
| 3413 | */ |
| 3414 | |
| 3415 | static PyTypeObject BufMapType; |
| 3416 | |
| 3417 | typedef struct |
| 3418 | { |
| 3419 | PyObject_HEAD |
| 3420 | } BufMapObject; |
| 3421 | |
| 3422 | static PyInt |
| 3423 | BufMapLength(PyObject *self UNUSED) |
| 3424 | { |
| 3425 | buf_T *b = firstbuf; |
| 3426 | PyInt n = 0; |
| 3427 | |
| 3428 | while (b) |
| 3429 | { |
| 3430 | ++n; |
| 3431 | b = b->b_next; |
| 3432 | } |
| 3433 | |
| 3434 | return n; |
| 3435 | } |
| 3436 | |
| 3437 | static PyObject * |
| 3438 | BufMapItem(PyObject *self UNUSED, PyObject *keyObject) |
| 3439 | { |
| 3440 | buf_T *b; |
| 3441 | int bnr; |
| 3442 | |
| 3443 | #if PY_MAJOR_VERSION < 3 |
| 3444 | if (PyInt_Check(keyObject)) |
| 3445 | bnr = PyInt_AsLong(keyObject); |
| 3446 | else |
| 3447 | #endif |
| 3448 | if (PyLong_Check(keyObject)) |
| 3449 | bnr = PyLong_AsLong(keyObject); |
| 3450 | else |
| 3451 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 3452 | PyErr_SetString(PyExc_TypeError, _("key must be integer")); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3453 | return NULL; |
| 3454 | } |
| 3455 | |
| 3456 | b = buflist_findnr(bnr); |
| 3457 | |
| 3458 | if (b) |
| 3459 | return BufferNew(b); |
| 3460 | else |
| 3461 | { |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 3462 | PyErr_SetObject(PyExc_KeyError, keyObject); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3463 | return NULL; |
| 3464 | } |
| 3465 | } |
| 3466 | |
| 3467 | static void |
| 3468 | BufMapIterDestruct(PyObject *buffer) |
| 3469 | { |
| 3470 | /* Iteration was stopped before all buffers were processed */ |
| 3471 | if (buffer) |
| 3472 | { |
| 3473 | Py_DECREF(buffer); |
| 3474 | } |
| 3475 | } |
| 3476 | |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 3477 | static int |
| 3478 | BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg) |
| 3479 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 3480 | if (buffer) |
| 3481 | Py_VISIT(buffer); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 3482 | return 0; |
| 3483 | } |
| 3484 | |
| 3485 | static int |
| 3486 | BufMapIterClear(PyObject **buffer) |
| 3487 | { |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 3488 | if (*buffer) |
| 3489 | Py_CLEAR(*buffer); |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 3490 | return 0; |
| 3491 | } |
| 3492 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3493 | static PyObject * |
| 3494 | BufMapIterNext(PyObject **buffer) |
| 3495 | { |
| 3496 | PyObject *next; |
| 3497 | PyObject *r; |
| 3498 | |
| 3499 | if (!*buffer) |
| 3500 | return NULL; |
| 3501 | |
| 3502 | r = *buffer; |
| 3503 | |
| 3504 | if (CheckBuffer((BufferObject *)(r))) |
| 3505 | { |
| 3506 | *buffer = NULL; |
| 3507 | return NULL; |
| 3508 | } |
| 3509 | |
| 3510 | if (!((BufferObject *)(r))->buf->b_next) |
| 3511 | next = NULL; |
| 3512 | else if (!(next = BufferNew(((BufferObject *)(r))->buf->b_next))) |
| 3513 | return NULL; |
| 3514 | *buffer = next; |
Bram Moolenaar | 9e74e30 | 2013-05-17 21:20:17 +0200 | [diff] [blame] | 3515 | /* Do not increment reference: we no longer hold it (decref), but whoever |
| 3516 | * on other side will hold (incref). Decref+incref = nothing. */ |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3517 | return r; |
| 3518 | } |
| 3519 | |
| 3520 | static PyObject * |
| 3521 | BufMapIter(PyObject *self UNUSED) |
| 3522 | { |
| 3523 | PyObject *buffer; |
| 3524 | |
| 3525 | buffer = BufferNew(firstbuf); |
| 3526 | return IterNew(buffer, |
Bram Moolenaar | cfef5ff | 2013-05-17 16:24:32 +0200 | [diff] [blame] | 3527 | (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext, |
| 3528 | (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | static PyMappingMethods BufMapAsMapping = { |
| 3532 | (lenfunc) BufMapLength, |
| 3533 | (binaryfunc) BufMapItem, |
| 3534 | (objobjargproc) 0, |
| 3535 | }; |
| 3536 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3537 | /* Current items object |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 3538 | */ |
| 3539 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3540 | static PyObject * |
| 3541 | CurrentGetattr(PyObject *self UNUSED, char *name) |
| 3542 | { |
| 3543 | if (strcmp(name, "buffer") == 0) |
| 3544 | return (PyObject *)BufferNew(curbuf); |
| 3545 | else if (strcmp(name, "window") == 0) |
Bram Moolenaar | cabf80f | 2013-05-17 16:18:33 +0200 | [diff] [blame] | 3546 | return (PyObject *)WindowNew(curwin, curtab); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 3547 | else if (strcmp(name, "tabpage") == 0) |
| 3548 | return (PyObject *)TabPageNew(curtab); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3549 | else if (strcmp(name, "line") == 0) |
| 3550 | return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); |
| 3551 | else if (strcmp(name, "range") == 0) |
| 3552 | return RangeNew(curbuf, RangeStart, RangeEnd); |
| 3553 | else if (strcmp(name,"__members__") == 0) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 3554 | return Py_BuildValue("[sssss]", "buffer", "window", "line", "range", |
| 3555 | "tabpage"); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3556 | else |
| 3557 | { |
| 3558 | PyErr_SetString(PyExc_AttributeError, name); |
| 3559 | return NULL; |
| 3560 | } |
| 3561 | } |
| 3562 | |
| 3563 | static int |
| 3564 | CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) |
| 3565 | { |
| 3566 | if (strcmp(name, "line") == 0) |
| 3567 | { |
| 3568 | if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) |
| 3569 | return -1; |
| 3570 | |
| 3571 | return 0; |
| 3572 | } |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3573 | else if (strcmp(name, "buffer") == 0) |
| 3574 | { |
| 3575 | int count; |
| 3576 | |
| 3577 | if (value->ob_type != &BufferType) |
| 3578 | { |
| 3579 | PyErr_SetString(PyExc_TypeError, _("expected vim.buffer object")); |
| 3580 | return -1; |
| 3581 | } |
| 3582 | |
| 3583 | if (CheckBuffer((BufferObject *)(value))) |
| 3584 | return -1; |
| 3585 | count = ((BufferObject *)(value))->buf->b_fnum; |
| 3586 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3587 | VimTryStart(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3588 | if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL) |
| 3589 | { |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3590 | if (VimTryEnd()) |
| 3591 | return -1; |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3592 | PyErr_SetVim(_("failed to switch to given buffer")); |
| 3593 | return -1; |
| 3594 | } |
| 3595 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3596 | return VimTryEnd(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3597 | } |
| 3598 | else if (strcmp(name, "window") == 0) |
| 3599 | { |
| 3600 | int count; |
| 3601 | |
| 3602 | if (value->ob_type != &WindowType) |
| 3603 | { |
| 3604 | PyErr_SetString(PyExc_TypeError, _("expected vim.window object")); |
| 3605 | return -1; |
| 3606 | } |
| 3607 | |
| 3608 | if (CheckWindow((WindowObject *)(value))) |
| 3609 | return -1; |
| 3610 | count = get_win_number(((WindowObject *)(value))->win, firstwin); |
| 3611 | |
| 3612 | if (!count) |
| 3613 | { |
| 3614 | PyErr_SetString(PyExc_ValueError, |
| 3615 | _("failed to find window in the current tab page")); |
| 3616 | return -1; |
| 3617 | } |
| 3618 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3619 | VimTryStart(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3620 | win_goto(((WindowObject *)(value))->win); |
| 3621 | if (((WindowObject *)(value))->win != curwin) |
| 3622 | { |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3623 | if (VimTryEnd()) |
| 3624 | return -1; |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3625 | PyErr_SetString(PyExc_RuntimeError, |
| 3626 | _("did not switch to the specified window")); |
| 3627 | return -1; |
| 3628 | } |
| 3629 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3630 | return VimTryEnd(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3631 | } |
| 3632 | else if (strcmp(name, "tabpage") == 0) |
| 3633 | { |
| 3634 | if (value->ob_type != &TabPageType) |
| 3635 | { |
| 3636 | PyErr_SetString(PyExc_TypeError, _("expected vim.tabpage object")); |
| 3637 | return -1; |
| 3638 | } |
| 3639 | |
| 3640 | if (CheckTabPage((TabPageObject *)(value))) |
| 3641 | return -1; |
| 3642 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3643 | VimTryStart(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3644 | goto_tabpage_tp(((TabPageObject *)(value))->tab, TRUE, TRUE); |
| 3645 | if (((TabPageObject *)(value))->tab != curtab) |
| 3646 | { |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3647 | if (VimTryEnd()) |
| 3648 | return -1; |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3649 | PyErr_SetString(PyExc_RuntimeError, |
| 3650 | _("did not switch to the specified tab page")); |
| 3651 | return -1; |
| 3652 | } |
| 3653 | |
Bram Moolenaar | a7b64ce | 2013-05-21 20:40:40 +0200 | [diff] [blame] | 3654 | return VimTryEnd(); |
Bram Moolenaar | e761459 | 2013-05-15 15:51:08 +0200 | [diff] [blame] | 3655 | } |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 3656 | else |
| 3657 | { |
| 3658 | PyErr_SetString(PyExc_AttributeError, name); |
| 3659 | return -1; |
| 3660 | } |
| 3661 | } |
| 3662 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3663 | static void |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3664 | init_range_cmd(exarg_T *eap) |
| 3665 | { |
| 3666 | RangeStart = eap->line1; |
| 3667 | RangeEnd = eap->line2; |
| 3668 | } |
| 3669 | |
| 3670 | static void |
| 3671 | init_range_eval(typval_T *rettv UNUSED) |
| 3672 | { |
| 3673 | RangeStart = (PyInt) curwin->w_cursor.lnum; |
| 3674 | RangeEnd = RangeStart; |
| 3675 | } |
| 3676 | |
| 3677 | static void |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3678 | run_cmd(const char *cmd, void *arg UNUSED |
| 3679 | #ifdef PY_CAN_RECURSE |
| 3680 | , PyGILState_STATE *pygilstate UNUSED |
| 3681 | #endif |
| 3682 | ) |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3683 | { |
| 3684 | PyRun_SimpleString((char *) cmd); |
| 3685 | } |
| 3686 | |
| 3687 | static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n "; |
| 3688 | static int code_hdr_len = 30; |
| 3689 | |
| 3690 | static void |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3691 | run_do(const char *cmd, void *arg UNUSED |
| 3692 | #ifdef PY_CAN_RECURSE |
| 3693 | , PyGILState_STATE *pygilstate |
| 3694 | #endif |
| 3695 | ) |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3696 | { |
| 3697 | PyInt lnum; |
| 3698 | size_t len; |
| 3699 | char *code; |
| 3700 | int status; |
| 3701 | PyObject *pyfunc, *pymain; |
| 3702 | |
Bram Moolenaar | 4ac6676 | 2013-05-28 22:31:46 +0200 | [diff] [blame] | 3703 | if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK) |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3704 | { |
| 3705 | EMSG(_("cannot save undo information")); |
| 3706 | return; |
| 3707 | } |
| 3708 | |
| 3709 | len = code_hdr_len + STRLEN(cmd); |
| 3710 | code = PyMem_New(char, len + 1); |
| 3711 | memcpy(code, code_hdr, code_hdr_len); |
| 3712 | STRCPY(code + code_hdr_len, cmd); |
| 3713 | status = PyRun_SimpleString(code); |
| 3714 | PyMem_Free(code); |
| 3715 | |
| 3716 | if (status) |
| 3717 | { |
| 3718 | EMSG(_("failed to run the code")); |
| 3719 | return; |
| 3720 | } |
| 3721 | |
| 3722 | status = 0; |
| 3723 | pymain = PyImport_AddModule("__main__"); |
| 3724 | pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3725 | #ifdef PY_CAN_RECURSE |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3726 | PyGILState_Release(*pygilstate); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3727 | #endif |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3728 | |
| 3729 | for (lnum = RangeStart; lnum <= RangeEnd; ++lnum) |
| 3730 | { |
| 3731 | PyObject *line, *linenr, *ret; |
| 3732 | |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3733 | #ifdef PY_CAN_RECURSE |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3734 | *pygilstate = PyGILState_Ensure(); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3735 | #endif |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3736 | if (!(line = GetBufferLine(curbuf, lnum))) |
| 3737 | goto err; |
| 3738 | if (!(linenr = PyInt_FromLong((long) lnum))) |
| 3739 | { |
| 3740 | Py_DECREF(line); |
| 3741 | goto err; |
| 3742 | } |
| 3743 | ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL); |
| 3744 | Py_DECREF(line); |
| 3745 | Py_DECREF(linenr); |
| 3746 | if (!ret) |
| 3747 | goto err; |
| 3748 | |
| 3749 | if (ret != Py_None) |
| 3750 | if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL) |
| 3751 | goto err; |
| 3752 | |
| 3753 | Py_XDECREF(ret); |
| 3754 | PythonIO_Flush(); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3755 | #ifdef PY_CAN_RECURSE |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3756 | PyGILState_Release(*pygilstate); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3757 | #endif |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3758 | } |
| 3759 | goto out; |
| 3760 | err: |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3761 | #ifdef PY_CAN_RECURSE |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3762 | *pygilstate = PyGILState_Ensure(); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3763 | #endif |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3764 | PyErr_PrintEx(0); |
| 3765 | PythonIO_Flush(); |
| 3766 | status = 1; |
| 3767 | out: |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3768 | #ifdef PY_CAN_RECURSE |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3769 | if (!status) |
| 3770 | *pygilstate = PyGILState_Ensure(); |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3771 | #endif |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3772 | Py_DECREF(pyfunc); |
| 3773 | PyObject_SetAttrString(pymain, DOPY_FUNC, NULL); |
| 3774 | if (status) |
| 3775 | return; |
| 3776 | check_cursor(); |
| 3777 | update_curbuf(NOT_VALID); |
| 3778 | } |
| 3779 | |
| 3780 | static void |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 3781 | run_eval(const char *cmd, typval_T *rettv |
| 3782 | #ifdef PY_CAN_RECURSE |
| 3783 | , PyGILState_STATE *pygilstate UNUSED |
| 3784 | #endif |
| 3785 | ) |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 3786 | { |
| 3787 | PyObject *r; |
| 3788 | |
| 3789 | r = PyRun_String((char *) cmd, Py_eval_input, globals, globals); |
| 3790 | if (r == NULL) |
| 3791 | { |
| 3792 | if (PyErr_Occurred() && !msg_silent) |
| 3793 | PyErr_PrintEx(0); |
| 3794 | EMSG(_("E858: Eval did not return a valid python object")); |
| 3795 | } |
| 3796 | else |
| 3797 | { |
| 3798 | if (ConvertFromPyObject(r, rettv) == -1) |
| 3799 | EMSG(_("E859: Failed to convert returned python object to vim value")); |
| 3800 | Py_DECREF(r); |
| 3801 | } |
| 3802 | PyErr_Clear(); |
| 3803 | } |
| 3804 | |
| 3805 | static void |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 3806 | set_ref_in_py(const int copyID) |
| 3807 | { |
| 3808 | pylinkedlist_T *cur; |
| 3809 | dict_T *dd; |
| 3810 | list_T *ll; |
| 3811 | |
| 3812 | if (lastdict != NULL) |
| 3813 | for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev) |
| 3814 | { |
| 3815 | dd = ((DictionaryObject *) (cur->pll_obj))->dict; |
| 3816 | if (dd->dv_copyID != copyID) |
| 3817 | { |
| 3818 | dd->dv_copyID = copyID; |
| 3819 | set_ref_in_ht(&dd->dv_hashtab, copyID); |
| 3820 | } |
| 3821 | } |
| 3822 | |
| 3823 | if (lastlist != NULL) |
| 3824 | for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev) |
| 3825 | { |
| 3826 | ll = ((ListObject *) (cur->pll_obj))->list; |
| 3827 | if (ll->lv_copyID != copyID) |
| 3828 | { |
| 3829 | ll->lv_copyID = copyID; |
| 3830 | set_ref_in_list(ll, copyID); |
| 3831 | } |
| 3832 | } |
| 3833 | } |
| 3834 | |
| 3835 | static int |
| 3836 | set_string_copy(char_u *str, typval_T *tv) |
| 3837 | { |
| 3838 | tv->vval.v_string = vim_strsave(str); |
| 3839 | if (tv->vval.v_string == NULL) |
| 3840 | { |
| 3841 | PyErr_NoMemory(); |
| 3842 | return -1; |
| 3843 | } |
| 3844 | return 0; |
| 3845 | } |
| 3846 | |
Bram Moolenaar | 3d0c52d | 2013-05-12 19:45:35 +0200 | [diff] [blame] | 3847 | static int |
| 3848 | pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 3849 | { |
| 3850 | dict_T *d; |
| 3851 | char_u *key; |
| 3852 | dictitem_T *di; |
| 3853 | PyObject *keyObject; |
| 3854 | PyObject *valObject; |
| 3855 | Py_ssize_t iter = 0; |
| 3856 | |
| 3857 | d = dict_alloc(); |
| 3858 | if (d == NULL) |
| 3859 | { |
| 3860 | PyErr_NoMemory(); |
| 3861 | return -1; |
| 3862 | } |
| 3863 | |
| 3864 | tv->v_type = VAR_DICT; |
| 3865 | tv->vval.v_dict = d; |
| 3866 | |
| 3867 | while (PyDict_Next(obj, &iter, &keyObject, &valObject)) |
| 3868 | { |
| 3869 | DICTKEY_DECL |
| 3870 | |
| 3871 | if (keyObject == NULL) |
| 3872 | return -1; |
| 3873 | if (valObject == NULL) |
| 3874 | return -1; |
| 3875 | |
| 3876 | DICTKEY_GET_NOTEMPTY(-1) |
| 3877 | |
| 3878 | di = dictitem_alloc(key); |
| 3879 | |
| 3880 | DICTKEY_UNREF |
| 3881 | |
| 3882 | if (di == NULL) |
| 3883 | { |
| 3884 | PyErr_NoMemory(); |
| 3885 | return -1; |
| 3886 | } |
| 3887 | di->di_tv.v_lock = 0; |
| 3888 | |
| 3889 | if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) |
| 3890 | { |
| 3891 | vim_free(di); |
| 3892 | return -1; |
| 3893 | } |
| 3894 | if (dict_add(d, di) == FAIL) |
| 3895 | { |
| 3896 | vim_free(di); |
| 3897 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 3898 | return -1; |
| 3899 | } |
| 3900 | } |
| 3901 | return 0; |
| 3902 | } |
| 3903 | |
| 3904 | static int |
| 3905 | pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 3906 | { |
| 3907 | dict_T *d; |
| 3908 | char_u *key; |
| 3909 | dictitem_T *di; |
| 3910 | PyObject *list; |
| 3911 | PyObject *litem; |
| 3912 | PyObject *keyObject; |
| 3913 | PyObject *valObject; |
| 3914 | Py_ssize_t lsize; |
| 3915 | |
| 3916 | d = dict_alloc(); |
| 3917 | if (d == NULL) |
| 3918 | { |
| 3919 | PyErr_NoMemory(); |
| 3920 | return -1; |
| 3921 | } |
| 3922 | |
| 3923 | tv->v_type = VAR_DICT; |
| 3924 | tv->vval.v_dict = d; |
| 3925 | |
| 3926 | list = PyMapping_Items(obj); |
| 3927 | if (list == NULL) |
| 3928 | return -1; |
| 3929 | lsize = PyList_Size(list); |
| 3930 | while (lsize--) |
| 3931 | { |
| 3932 | DICTKEY_DECL |
| 3933 | |
| 3934 | litem = PyList_GetItem(list, lsize); |
| 3935 | if (litem == NULL) |
| 3936 | { |
| 3937 | Py_DECREF(list); |
| 3938 | return -1; |
| 3939 | } |
| 3940 | |
| 3941 | keyObject = PyTuple_GetItem(litem, 0); |
| 3942 | if (keyObject == NULL) |
| 3943 | { |
| 3944 | Py_DECREF(list); |
| 3945 | Py_DECREF(litem); |
| 3946 | return -1; |
| 3947 | } |
| 3948 | |
| 3949 | DICTKEY_GET_NOTEMPTY(-1) |
| 3950 | |
| 3951 | valObject = PyTuple_GetItem(litem, 1); |
| 3952 | if (valObject == NULL) |
| 3953 | { |
| 3954 | Py_DECREF(list); |
| 3955 | Py_DECREF(litem); |
| 3956 | return -1; |
| 3957 | } |
| 3958 | |
| 3959 | di = dictitem_alloc(key); |
| 3960 | |
| 3961 | DICTKEY_UNREF |
| 3962 | |
| 3963 | if (di == NULL) |
| 3964 | { |
| 3965 | Py_DECREF(list); |
| 3966 | Py_DECREF(litem); |
| 3967 | PyErr_NoMemory(); |
| 3968 | return -1; |
| 3969 | } |
| 3970 | di->di_tv.v_lock = 0; |
| 3971 | |
| 3972 | if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) |
| 3973 | { |
| 3974 | vim_free(di); |
| 3975 | Py_DECREF(list); |
| 3976 | Py_DECREF(litem); |
| 3977 | return -1; |
| 3978 | } |
| 3979 | if (dict_add(d, di) == FAIL) |
| 3980 | { |
| 3981 | vim_free(di); |
| 3982 | Py_DECREF(list); |
| 3983 | Py_DECREF(litem); |
| 3984 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 3985 | return -1; |
| 3986 | } |
| 3987 | Py_DECREF(litem); |
| 3988 | } |
| 3989 | Py_DECREF(list); |
| 3990 | return 0; |
| 3991 | } |
| 3992 | |
| 3993 | static int |
| 3994 | pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 3995 | { |
| 3996 | list_T *l; |
| 3997 | |
| 3998 | l = list_alloc(); |
| 3999 | if (l == NULL) |
| 4000 | { |
| 4001 | PyErr_NoMemory(); |
| 4002 | return -1; |
| 4003 | } |
| 4004 | |
| 4005 | tv->v_type = VAR_LIST; |
| 4006 | tv->vval.v_list = l; |
| 4007 | |
| 4008 | if (list_py_concat(l, obj, lookupDict) == -1) |
| 4009 | return -1; |
| 4010 | |
| 4011 | return 0; |
| 4012 | } |
| 4013 | |
| 4014 | static int |
| 4015 | pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 4016 | { |
| 4017 | PyObject *iterator = PyObject_GetIter(obj); |
| 4018 | PyObject *item; |
| 4019 | list_T *l; |
| 4020 | listitem_T *li; |
| 4021 | |
| 4022 | l = list_alloc(); |
| 4023 | |
| 4024 | if (l == NULL) |
| 4025 | { |
| 4026 | PyErr_NoMemory(); |
| 4027 | return -1; |
| 4028 | } |
| 4029 | |
| 4030 | tv->vval.v_list = l; |
| 4031 | tv->v_type = VAR_LIST; |
| 4032 | |
| 4033 | |
| 4034 | if (iterator == NULL) |
| 4035 | return -1; |
| 4036 | |
| 4037 | while ((item = PyIter_Next(obj))) |
| 4038 | { |
| 4039 | li = listitem_alloc(); |
| 4040 | if (li == NULL) |
| 4041 | { |
| 4042 | PyErr_NoMemory(); |
| 4043 | return -1; |
| 4044 | } |
| 4045 | li->li_tv.v_lock = 0; |
| 4046 | |
| 4047 | if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1) |
| 4048 | return -1; |
| 4049 | |
| 4050 | list_append(l, li); |
| 4051 | |
| 4052 | Py_DECREF(item); |
| 4053 | } |
| 4054 | |
| 4055 | Py_DECREF(iterator); |
| 4056 | return 0; |
| 4057 | } |
| 4058 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4059 | typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *); |
| 4060 | |
| 4061 | static int |
| 4062 | convert_dl(PyObject *obj, typval_T *tv, |
| 4063 | pytotvfunc py_to_tv, PyObject *lookupDict) |
| 4064 | { |
| 4065 | PyObject *capsule; |
| 4066 | char hexBuf[sizeof(void *) * 2 + 3]; |
| 4067 | |
| 4068 | sprintf(hexBuf, "%p", obj); |
| 4069 | |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4070 | # ifdef PY_USE_CAPSULE |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4071 | capsule = PyDict_GetItemString(lookupDict, hexBuf); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4072 | # else |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 4073 | capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4074 | # endif |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 4075 | if (capsule == NULL) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4076 | { |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4077 | # ifdef PY_USE_CAPSULE |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4078 | capsule = PyCapsule_New(tv, NULL, NULL); |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 4079 | # else |
| 4080 | capsule = PyCObject_FromVoidPtr(tv, NULL); |
| 4081 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4082 | PyDict_SetItemString(lookupDict, hexBuf, capsule); |
| 4083 | Py_DECREF(capsule); |
| 4084 | if (py_to_tv(obj, tv, lookupDict) == -1) |
| 4085 | { |
| 4086 | tv->v_type = VAR_UNKNOWN; |
| 4087 | return -1; |
| 4088 | } |
| 4089 | /* As we are not using copy_tv which increments reference count we must |
| 4090 | * do it ourself. */ |
| 4091 | switch(tv->v_type) |
| 4092 | { |
| 4093 | case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break; |
| 4094 | case VAR_LIST: ++tv->vval.v_list->lv_refcount; break; |
| 4095 | } |
| 4096 | } |
| 4097 | else |
| 4098 | { |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4099 | typval_T *v; |
| 4100 | |
| 4101 | # ifdef PY_USE_CAPSULE |
| 4102 | v = PyCapsule_GetPointer(capsule, NULL); |
| 4103 | # else |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 4104 | v = PyCObject_AsVoidPtr(capsule); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 4105 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4106 | copy_tv(v, tv); |
| 4107 | } |
| 4108 | return 0; |
| 4109 | } |
| 4110 | |
| 4111 | static int |
| 4112 | ConvertFromPyObject(PyObject *obj, typval_T *tv) |
| 4113 | { |
| 4114 | PyObject *lookup_dict; |
| 4115 | int r; |
| 4116 | |
| 4117 | lookup_dict = PyDict_New(); |
| 4118 | r = _ConvertFromPyObject(obj, tv, lookup_dict); |
| 4119 | Py_DECREF(lookup_dict); |
| 4120 | return r; |
| 4121 | } |
| 4122 | |
| 4123 | static int |
| 4124 | _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 4125 | { |
| 4126 | if (obj->ob_type == &DictionaryType) |
| 4127 | { |
| 4128 | tv->v_type = VAR_DICT; |
| 4129 | tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); |
| 4130 | ++tv->vval.v_dict->dv_refcount; |
| 4131 | } |
| 4132 | else if (obj->ob_type == &ListType) |
| 4133 | { |
| 4134 | tv->v_type = VAR_LIST; |
| 4135 | tv->vval.v_list = (((ListObject *)(obj))->list); |
| 4136 | ++tv->vval.v_list->lv_refcount; |
| 4137 | } |
| 4138 | else if (obj->ob_type == &FunctionType) |
| 4139 | { |
| 4140 | if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1) |
| 4141 | return -1; |
| 4142 | |
| 4143 | tv->v_type = VAR_FUNC; |
| 4144 | func_ref(tv->vval.v_string); |
| 4145 | } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4146 | else if (PyBytes_Check(obj)) |
| 4147 | { |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 4148 | char_u *result; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4149 | |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 4150 | if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1) |
| 4151 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4152 | if (result == NULL) |
| 4153 | return -1; |
| 4154 | |
| 4155 | if (set_string_copy(result, tv) == -1) |
| 4156 | return -1; |
| 4157 | |
| 4158 | tv->v_type = VAR_STRING; |
| 4159 | } |
| 4160 | else if (PyUnicode_Check(obj)) |
| 4161 | { |
| 4162 | PyObject *bytes; |
| 4163 | char_u *result; |
| 4164 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4165 | bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); |
| 4166 | if (bytes == NULL) |
| 4167 | return -1; |
| 4168 | |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 4169 | if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) |
| 4170 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4171 | if (result == NULL) |
| 4172 | return -1; |
| 4173 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 4174 | if (set_string_copy(result, tv)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4175 | { |
| 4176 | Py_XDECREF(bytes); |
| 4177 | return -1; |
| 4178 | } |
| 4179 | Py_XDECREF(bytes); |
| 4180 | |
| 4181 | tv->v_type = VAR_STRING; |
| 4182 | } |
Bram Moolenaar | 335e0b6 | 2013-04-24 13:47:45 +0200 | [diff] [blame] | 4183 | #if PY_MAJOR_VERSION < 3 |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4184 | else if (PyInt_Check(obj)) |
| 4185 | { |
| 4186 | tv->v_type = VAR_NUMBER; |
| 4187 | tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj); |
| 4188 | } |
| 4189 | #endif |
| 4190 | else if (PyLong_Check(obj)) |
| 4191 | { |
| 4192 | tv->v_type = VAR_NUMBER; |
| 4193 | tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj); |
| 4194 | } |
| 4195 | else if (PyDict_Check(obj)) |
| 4196 | return convert_dl(obj, tv, pydict_to_tv, lookupDict); |
| 4197 | #ifdef FEAT_FLOAT |
| 4198 | else if (PyFloat_Check(obj)) |
| 4199 | { |
| 4200 | tv->v_type = VAR_FLOAT; |
| 4201 | tv->vval.v_float = (float_T) PyFloat_AsDouble(obj); |
| 4202 | } |
| 4203 | #endif |
| 4204 | else if (PyIter_Check(obj)) |
| 4205 | return convert_dl(obj, tv, pyiter_to_tv, lookupDict); |
| 4206 | else if (PySequence_Check(obj)) |
| 4207 | return convert_dl(obj, tv, pyseq_to_tv, lookupDict); |
| 4208 | else if (PyMapping_Check(obj)) |
| 4209 | return convert_dl(obj, tv, pymap_to_tv, lookupDict); |
| 4210 | else |
| 4211 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 4212 | PyErr_SetString(PyExc_TypeError, |
| 4213 | _("unable to convert to vim structure")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4214 | return -1; |
| 4215 | } |
| 4216 | return 0; |
| 4217 | } |
| 4218 | |
| 4219 | static PyObject * |
| 4220 | ConvertToPyObject(typval_T *tv) |
| 4221 | { |
| 4222 | if (tv == NULL) |
| 4223 | { |
| 4224 | PyErr_SetVim(_("NULL reference passed")); |
| 4225 | return NULL; |
| 4226 | } |
| 4227 | switch (tv->v_type) |
| 4228 | { |
| 4229 | case VAR_STRING: |
Bram Moolenaar | d1f13fd | 2012-10-05 21:30:07 +0200 | [diff] [blame] | 4230 | return PyBytes_FromString(tv->vval.v_string == NULL |
| 4231 | ? "" : (char *)tv->vval.v_string); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4232 | case VAR_NUMBER: |
| 4233 | return PyLong_FromLong((long) tv->vval.v_number); |
| 4234 | #ifdef FEAT_FLOAT |
| 4235 | case VAR_FLOAT: |
| 4236 | return PyFloat_FromDouble((double) tv->vval.v_float); |
| 4237 | #endif |
| 4238 | case VAR_LIST: |
| 4239 | return ListNew(tv->vval.v_list); |
| 4240 | case VAR_DICT: |
| 4241 | return DictionaryNew(tv->vval.v_dict); |
| 4242 | case VAR_FUNC: |
Bram Moolenaar | d1f13fd | 2012-10-05 21:30:07 +0200 | [diff] [blame] | 4243 | return FunctionNew(tv->vval.v_string == NULL |
| 4244 | ? (char_u *)"" : tv->vval.v_string); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 4245 | case VAR_UNKNOWN: |
| 4246 | Py_INCREF(Py_None); |
| 4247 | return Py_None; |
| 4248 | default: |
| 4249 | PyErr_SetVim(_("internal error: invalid value type")); |
| 4250 | return NULL; |
| 4251 | } |
| 4252 | } |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4253 | |
| 4254 | typedef struct |
| 4255 | { |
| 4256 | PyObject_HEAD |
| 4257 | } CurrentObject; |
| 4258 | static PyTypeObject CurrentType; |
| 4259 | |
| 4260 | static void |
| 4261 | init_structs(void) |
| 4262 | { |
| 4263 | vim_memset(&OutputType, 0, sizeof(OutputType)); |
| 4264 | OutputType.tp_name = "vim.message"; |
| 4265 | OutputType.tp_basicsize = sizeof(OutputObject); |
| 4266 | OutputType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4267 | OutputType.tp_doc = "vim message object"; |
| 4268 | OutputType.tp_methods = OutputMethods; |
| 4269 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4270 | OutputType.tp_getattro = (getattrofunc)OutputGetattro; |
| 4271 | OutputType.tp_setattro = (setattrofunc)OutputSetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4272 | OutputType.tp_alloc = call_PyType_GenericAlloc; |
| 4273 | OutputType.tp_new = call_PyType_GenericNew; |
| 4274 | OutputType.tp_free = call_PyObject_Free; |
| 4275 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4276 | OutputType.tp_getattr = (getattrfunc)OutputGetattr; |
| 4277 | OutputType.tp_setattr = (setattrfunc)OutputSetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4278 | #endif |
| 4279 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 4280 | vim_memset(&IterType, 0, sizeof(IterType)); |
| 4281 | IterType.tp_name = "vim.iter"; |
| 4282 | IterType.tp_basicsize = sizeof(IterObject); |
| 4283 | IterType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4284 | IterType.tp_doc = "generic iterator object"; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4285 | IterType.tp_iter = (getiterfunc)IterIter; |
| 4286 | IterType.tp_iternext = (iternextfunc)IterNext; |
| 4287 | IterType.tp_dealloc = (destructor)IterDestructor; |
| 4288 | IterType.tp_traverse = (traverseproc)IterTraverse; |
| 4289 | IterType.tp_clear = (inquiry)IterClear; |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 4290 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4291 | vim_memset(&BufferType, 0, sizeof(BufferType)); |
| 4292 | BufferType.tp_name = "vim.buffer"; |
| 4293 | BufferType.tp_basicsize = sizeof(BufferType); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4294 | BufferType.tp_dealloc = (destructor)BufferDestructor; |
| 4295 | BufferType.tp_repr = (reprfunc)BufferRepr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4296 | BufferType.tp_as_sequence = &BufferAsSeq; |
| 4297 | BufferType.tp_as_mapping = &BufferAsMapping; |
| 4298 | BufferType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4299 | BufferType.tp_doc = "vim buffer object"; |
| 4300 | BufferType.tp_methods = BufferMethods; |
| 4301 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4302 | BufferType.tp_getattro = (getattrofunc)BufferGetattro; |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 4303 | BufferType.tp_setattro = (setattrofunc)BufferSetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4304 | BufferType.tp_alloc = call_PyType_GenericAlloc; |
| 4305 | BufferType.tp_new = call_PyType_GenericNew; |
| 4306 | BufferType.tp_free = call_PyObject_Free; |
| 4307 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4308 | BufferType.tp_getattr = (getattrfunc)BufferGetattr; |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 4309 | BufferType.tp_setattr = (setattrfunc)BufferSetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4310 | #endif |
| 4311 | |
| 4312 | vim_memset(&WindowType, 0, sizeof(WindowType)); |
| 4313 | WindowType.tp_name = "vim.window"; |
| 4314 | WindowType.tp_basicsize = sizeof(WindowObject); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4315 | WindowType.tp_dealloc = (destructor)WindowDestructor; |
| 4316 | WindowType.tp_repr = (reprfunc)WindowRepr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4317 | WindowType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4318 | WindowType.tp_doc = "vim Window object"; |
| 4319 | WindowType.tp_methods = WindowMethods; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4320 | WindowType.tp_traverse = (traverseproc)WindowTraverse; |
| 4321 | WindowType.tp_clear = (inquiry)WindowClear; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4322 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4323 | WindowType.tp_getattro = (getattrofunc)WindowGetattro; |
| 4324 | WindowType.tp_setattro = (setattrofunc)WindowSetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4325 | WindowType.tp_alloc = call_PyType_GenericAlloc; |
| 4326 | WindowType.tp_new = call_PyType_GenericNew; |
| 4327 | WindowType.tp_free = call_PyObject_Free; |
| 4328 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4329 | WindowType.tp_getattr = (getattrfunc)WindowGetattr; |
| 4330 | WindowType.tp_setattr = (setattrfunc)WindowSetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4331 | #endif |
| 4332 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 4333 | vim_memset(&TabPageType, 0, sizeof(TabPageType)); |
| 4334 | TabPageType.tp_name = "vim.tabpage"; |
| 4335 | TabPageType.tp_basicsize = sizeof(TabPageObject); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4336 | TabPageType.tp_dealloc = (destructor)TabPageDestructor; |
| 4337 | TabPageType.tp_repr = (reprfunc)TabPageRepr; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 4338 | TabPageType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4339 | TabPageType.tp_doc = "vim tab page object"; |
| 4340 | TabPageType.tp_methods = TabPageMethods; |
| 4341 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4342 | TabPageType.tp_getattro = (getattrofunc)TabPageGetattro; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 4343 | TabPageType.tp_alloc = call_PyType_GenericAlloc; |
| 4344 | TabPageType.tp_new = call_PyType_GenericNew; |
| 4345 | TabPageType.tp_free = call_PyObject_Free; |
| 4346 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4347 | TabPageType.tp_getattr = (getattrfunc)TabPageGetattr; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 4348 | #endif |
| 4349 | |
Bram Moolenaar | dfa38d4 | 2013-05-15 13:38:47 +0200 | [diff] [blame] | 4350 | vim_memset(&BufMapType, 0, sizeof(BufMapType)); |
| 4351 | BufMapType.tp_name = "vim.bufferlist"; |
| 4352 | BufMapType.tp_basicsize = sizeof(BufMapObject); |
| 4353 | BufMapType.tp_as_mapping = &BufMapAsMapping; |
| 4354 | BufMapType.tp_flags = Py_TPFLAGS_DEFAULT; |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 4355 | BufMapType.tp_iter = BufMapIter; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4356 | BufferType.tp_doc = "vim buffer list"; |
| 4357 | |
| 4358 | vim_memset(&WinListType, 0, sizeof(WinListType)); |
| 4359 | WinListType.tp_name = "vim.windowlist"; |
| 4360 | WinListType.tp_basicsize = sizeof(WinListType); |
| 4361 | WinListType.tp_as_sequence = &WinListAsSeq; |
| 4362 | WinListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4363 | WinListType.tp_doc = "vim window list"; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4364 | WinListType.tp_dealloc = (destructor)WinListDestructor; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 4365 | |
| 4366 | vim_memset(&TabListType, 0, sizeof(TabListType)); |
| 4367 | TabListType.tp_name = "vim.tabpagelist"; |
| 4368 | TabListType.tp_basicsize = sizeof(TabListType); |
| 4369 | TabListType.tp_as_sequence = &TabListAsSeq; |
| 4370 | TabListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4371 | TabListType.tp_doc = "vim tab page list"; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4372 | |
| 4373 | vim_memset(&RangeType, 0, sizeof(RangeType)); |
| 4374 | RangeType.tp_name = "vim.range"; |
| 4375 | RangeType.tp_basicsize = sizeof(RangeObject); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4376 | RangeType.tp_dealloc = (destructor)RangeDestructor; |
| 4377 | RangeType.tp_repr = (reprfunc)RangeRepr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4378 | RangeType.tp_as_sequence = &RangeAsSeq; |
| 4379 | RangeType.tp_as_mapping = &RangeAsMapping; |
| 4380 | RangeType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4381 | RangeType.tp_doc = "vim Range object"; |
| 4382 | RangeType.tp_methods = RangeMethods; |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 4383 | RangeType.tp_traverse = (traverseproc)RangeTraverse; |
| 4384 | RangeType.tp_clear = (inquiry)RangeClear; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4385 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4386 | RangeType.tp_getattro = (getattrofunc)RangeGetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4387 | RangeType.tp_alloc = call_PyType_GenericAlloc; |
| 4388 | RangeType.tp_new = call_PyType_GenericNew; |
| 4389 | RangeType.tp_free = call_PyObject_Free; |
| 4390 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4391 | RangeType.tp_getattr = (getattrfunc)RangeGetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4392 | #endif |
| 4393 | |
| 4394 | vim_memset(&CurrentType, 0, sizeof(CurrentType)); |
| 4395 | CurrentType.tp_name = "vim.currentdata"; |
| 4396 | CurrentType.tp_basicsize = sizeof(CurrentObject); |
| 4397 | CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4398 | CurrentType.tp_doc = "vim current object"; |
| 4399 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4400 | CurrentType.tp_getattro = (getattrofunc)CurrentGetattro; |
| 4401 | CurrentType.tp_setattro = (setattrofunc)CurrentSetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4402 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4403 | CurrentType.tp_getattr = (getattrfunc)CurrentGetattr; |
| 4404 | CurrentType.tp_setattr = (setattrfunc)CurrentSetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4405 | #endif |
| 4406 | |
| 4407 | vim_memset(&DictionaryType, 0, sizeof(DictionaryType)); |
| 4408 | DictionaryType.tp_name = "vim.dictionary"; |
| 4409 | DictionaryType.tp_basicsize = sizeof(DictionaryObject); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4410 | DictionaryType.tp_dealloc = (destructor)DictionaryDestructor; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4411 | DictionaryType.tp_as_mapping = &DictionaryAsMapping; |
| 4412 | DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4413 | DictionaryType.tp_doc = "dictionary pushing modifications to vim structure"; |
| 4414 | DictionaryType.tp_methods = DictionaryMethods; |
| 4415 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4416 | DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro; |
| 4417 | DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4418 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4419 | DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr; |
| 4420 | DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4421 | #endif |
| 4422 | |
| 4423 | vim_memset(&ListType, 0, sizeof(ListType)); |
| 4424 | ListType.tp_name = "vim.list"; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4425 | ListType.tp_dealloc = (destructor)ListDestructor; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4426 | ListType.tp_basicsize = sizeof(ListObject); |
| 4427 | ListType.tp_as_sequence = &ListAsSeq; |
| 4428 | ListType.tp_as_mapping = &ListAsMapping; |
| 4429 | ListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4430 | ListType.tp_doc = "list pushing modifications to vim structure"; |
| 4431 | ListType.tp_methods = ListMethods; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4432 | ListType.tp_iter = (getiterfunc)ListIter; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4433 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4434 | ListType.tp_getattro = (getattrofunc)ListGetattro; |
| 4435 | ListType.tp_setattro = (setattrofunc)ListSetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4436 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4437 | ListType.tp_getattr = (getattrfunc)ListGetattr; |
| 4438 | ListType.tp_setattr = (setattrfunc)ListSetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4439 | #endif |
| 4440 | |
| 4441 | vim_memset(&FunctionType, 0, sizeof(FunctionType)); |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 4442 | FunctionType.tp_name = "vim.function"; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4443 | FunctionType.tp_basicsize = sizeof(FunctionObject); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4444 | FunctionType.tp_dealloc = (destructor)FunctionDestructor; |
| 4445 | FunctionType.tp_call = (ternaryfunc)FunctionCall; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4446 | FunctionType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4447 | FunctionType.tp_doc = "object that calls vim function"; |
| 4448 | FunctionType.tp_methods = FunctionMethods; |
| 4449 | #if PY_MAJOR_VERSION >= 3 |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4450 | FunctionType.tp_getattro = (getattrofunc)FunctionGetattro; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4451 | #else |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4452 | FunctionType.tp_getattr = (getattrfunc)FunctionGetattr; |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4453 | #endif |
| 4454 | |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 4455 | vim_memset(&OptionsType, 0, sizeof(OptionsType)); |
| 4456 | OptionsType.tp_name = "vim.options"; |
| 4457 | OptionsType.tp_basicsize = sizeof(OptionsObject); |
| 4458 | OptionsType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 4459 | OptionsType.tp_doc = "object for manipulating options"; |
| 4460 | OptionsType.tp_as_mapping = &OptionsAsMapping; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 4461 | OptionsType.tp_dealloc = (destructor)OptionsDestructor; |
| 4462 | OptionsType.tp_traverse = (traverseproc)OptionsTraverse; |
| 4463 | OptionsType.tp_clear = (inquiry)OptionsClear; |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 4464 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 4465 | #if PY_MAJOR_VERSION >= 3 |
| 4466 | vim_memset(&vimmodule, 0, sizeof(vimmodule)); |
| 4467 | vimmodule.m_name = "vim"; |
| 4468 | vimmodule.m_doc = "Vim Python interface\n"; |
| 4469 | vimmodule.m_size = -1; |
| 4470 | vimmodule.m_methods = VimMethods; |
| 4471 | #endif |
| 4472 | } |
Bram Moolenaar | 1dc2878 | 2013-05-21 19:11:01 +0200 | [diff] [blame] | 4473 | |
| 4474 | #define PYTYPE_READY(type) \ |
| 4475 | if (PyType_Ready(&type)) \ |
| 4476 | return -1; |
| 4477 | |
| 4478 | static int |
| 4479 | init_types() |
| 4480 | { |
| 4481 | PYTYPE_READY(IterType); |
| 4482 | PYTYPE_READY(BufferType); |
| 4483 | PYTYPE_READY(RangeType); |
| 4484 | PYTYPE_READY(WindowType); |
| 4485 | PYTYPE_READY(TabPageType); |
| 4486 | PYTYPE_READY(BufMapType); |
| 4487 | PYTYPE_READY(WinListType); |
| 4488 | PYTYPE_READY(TabListType); |
| 4489 | PYTYPE_READY(CurrentType); |
| 4490 | PYTYPE_READY(DictionaryType); |
| 4491 | PYTYPE_READY(ListType); |
| 4492 | PYTYPE_READY(FunctionType); |
| 4493 | PYTYPE_READY(OptionsType); |
| 4494 | PYTYPE_READY(OutputType); |
| 4495 | return 0; |
| 4496 | } |
| 4497 | |
| 4498 | static BufMapObject TheBufferMap = |
| 4499 | { |
| 4500 | PyObject_HEAD_INIT(&BufMapType) |
| 4501 | }; |
| 4502 | |
| 4503 | static WinListObject TheWindowList = |
| 4504 | { |
| 4505 | PyObject_HEAD_INIT(&WinListType) |
| 4506 | NULL |
| 4507 | }; |
| 4508 | |
| 4509 | static CurrentObject TheCurrent = |
| 4510 | { |
| 4511 | PyObject_HEAD_INIT(&CurrentType) |
| 4512 | }; |
| 4513 | |
| 4514 | static TabListObject TheTabPageList = |
| 4515 | { |
| 4516 | PyObject_HEAD_INIT(&TabListType) |
| 4517 | }; |
| 4518 | |
| 4519 | static struct numeric_constant { |
| 4520 | char *name; |
| 4521 | int value; |
| 4522 | } numeric_constants[] = { |
| 4523 | {"VAR_LOCKED", VAR_LOCKED}, |
| 4524 | {"VAR_FIXED", VAR_FIXED}, |
| 4525 | {"VAR_SCOPE", VAR_SCOPE}, |
| 4526 | {"VAR_DEF_SCOPE", VAR_DEF_SCOPE}, |
| 4527 | }; |
| 4528 | |
| 4529 | static struct object_constant { |
| 4530 | char *name; |
| 4531 | PyObject *value; |
| 4532 | } object_constants[] = { |
| 4533 | {"buffers", (PyObject *)(void *)&TheBufferMap}, |
| 4534 | {"windows", (PyObject *)(void *)&TheWindowList}, |
| 4535 | {"tabpages", (PyObject *)(void *)&TheTabPageList}, |
| 4536 | {"current", (PyObject *)(void *)&TheCurrent}, |
Bram Moolenaar | cac867a | 2013-05-21 19:50:34 +0200 | [diff] [blame] | 4537 | |
| 4538 | {"Buffer", (PyObject *)&BufferType}, |
| 4539 | {"Range", (PyObject *)&RangeType}, |
| 4540 | {"Window", (PyObject *)&WindowType}, |
| 4541 | {"TabPage", (PyObject *)&TabPageType}, |
| 4542 | {"Dictionary", (PyObject *)&DictionaryType}, |
| 4543 | {"List", (PyObject *)&ListType}, |
| 4544 | {"Function", (PyObject *)&FunctionType}, |
| 4545 | {"Options", (PyObject *)&OptionsType}, |
Bram Moolenaar | 1dc2878 | 2013-05-21 19:11:01 +0200 | [diff] [blame] | 4546 | }; |
| 4547 | |
| 4548 | typedef int (*object_adder)(PyObject *, const char *, PyObject *); |
| 4549 | |
| 4550 | #define ADD_OBJECT(m, name, obj) \ |
| 4551 | if (add_object(m, name, obj)) \ |
| 4552 | return -1; |
| 4553 | |
| 4554 | #define ADD_CHECKED_OBJECT(m, name, obj) \ |
| 4555 | { \ |
| 4556 | PyObject *value = obj; \ |
| 4557 | if (!value) \ |
| 4558 | return -1; \ |
| 4559 | ADD_OBJECT(m, name, value); \ |
| 4560 | } |
| 4561 | |
| 4562 | static int |
| 4563 | populate_module(PyObject *m, object_adder add_object) |
| 4564 | { |
| 4565 | int i; |
| 4566 | |
| 4567 | for (i = 0; i < (int)(sizeof(numeric_constants) |
| 4568 | / sizeof(struct numeric_constant)); |
| 4569 | ++i) |
| 4570 | ADD_CHECKED_OBJECT(m, numeric_constants[i].name, |
| 4571 | PyInt_FromLong(numeric_constants[i].value)); |
| 4572 | |
| 4573 | for (i = 0; i < (int)(sizeof(object_constants) |
| 4574 | / sizeof(struct object_constant)); |
| 4575 | ++i) |
| 4576 | { |
| 4577 | PyObject *value; |
| 4578 | |
| 4579 | value = object_constants[i].value; |
| 4580 | Py_INCREF(value); |
| 4581 | ADD_OBJECT(m, object_constants[i].name, value); |
| 4582 | } |
| 4583 | |
| 4584 | if (!(VimError = PyErr_NewException("vim.error", NULL, NULL))) |
| 4585 | return -1; |
| 4586 | ADD_OBJECT(m, "error", VimError); |
| 4587 | |
| 4588 | ADD_CHECKED_OBJECT(m, "vars", DictionaryNew(&globvardict)); |
| 4589 | ADD_CHECKED_OBJECT(m, "vvars", DictionaryNew(&vimvardict)); |
| 4590 | ADD_CHECKED_OBJECT(m, "options", |
| 4591 | OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL)); |
| 4592 | return 0; |
| 4593 | } |