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