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