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