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 | /* |
| 10 | * Python extensions by Paul Moore, David Leonard, Roland Puntaier. |
| 11 | * |
| 12 | * Common code for if_python.c and if_python3.c. |
| 13 | */ |
| 14 | |
Bram Moolenaar | c1a995d | 2012-08-08 16:05:07 +0200 | [diff] [blame] | 15 | #if PY_VERSION_HEX < 0x02050000 |
| 16 | typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */ |
| 17 | #endif |
| 18 | |
Bram Moolenaar | 91805fc | 2011-06-26 04:01:44 +0200 | [diff] [blame] | 19 | #ifdef FEAT_MBYTE |
| 20 | # define ENC_OPT p_enc |
| 21 | #else |
| 22 | # define ENC_OPT "latin1" |
| 23 | #endif |
| 24 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 25 | /* |
| 26 | * obtain a lock on the Vim data structures |
| 27 | */ |
| 28 | static void |
| 29 | Python_Lock_Vim(void) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * release a lock on the Vim data structures |
| 35 | */ |
| 36 | static void |
| 37 | Python_Release_Vim(void) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | /* Output object definition |
| 42 | */ |
| 43 | |
| 44 | static PyObject *OutputWrite(PyObject *, PyObject *); |
| 45 | static PyObject *OutputWritelines(PyObject *, PyObject *); |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 46 | static PyObject *OutputFlush(PyObject *, PyObject *); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 47 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 48 | /* Function to write a line, points to either msg() or emsg(). */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 49 | typedef void (*writefn)(char_u *); |
| 50 | static void writer(writefn fn, char_u *str, PyInt n); |
| 51 | |
| 52 | typedef struct |
| 53 | { |
| 54 | PyObject_HEAD |
| 55 | long softspace; |
| 56 | long error; |
| 57 | } OutputObject; |
| 58 | |
| 59 | static struct PyMethodDef OutputMethods[] = { |
| 60 | /* name, function, calling, documentation */ |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 61 | {"write", OutputWrite, 1, ""}, |
| 62 | {"writelines", OutputWritelines, 1, ""}, |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 63 | {"flush", OutputFlush, 1, ""}, |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 64 | { NULL, NULL, 0, NULL} |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 67 | #define PyErr_SetVim(str) PyErr_SetString(VimError, str) |
| 68 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 69 | /*************/ |
| 70 | |
| 71 | /* Output buffer management |
| 72 | */ |
| 73 | |
| 74 | static PyObject * |
| 75 | OutputWrite(PyObject *self, PyObject *args) |
| 76 | { |
Bram Moolenaar | ac0ddc1 | 2012-09-05 17:28:21 +0200 | [diff] [blame] | 77 | Py_ssize_t len; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 78 | char *str = NULL; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 79 | int error = ((OutputObject *)(self))->error; |
| 80 | |
Bram Moolenaar | 2756480 | 2011-09-07 19:30:21 +0200 | [diff] [blame] | 81 | if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len)) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 82 | return NULL; |
| 83 | |
| 84 | Py_BEGIN_ALLOW_THREADS |
| 85 | Python_Lock_Vim(); |
| 86 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 87 | Python_Release_Vim(); |
| 88 | Py_END_ALLOW_THREADS |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 89 | PyMem_Free(str); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 90 | |
| 91 | Py_INCREF(Py_None); |
| 92 | return Py_None; |
| 93 | } |
| 94 | |
| 95 | static PyObject * |
| 96 | OutputWritelines(PyObject *self, PyObject *args) |
| 97 | { |
| 98 | PyInt n; |
| 99 | PyInt i; |
| 100 | PyObject *list; |
| 101 | int error = ((OutputObject *)(self))->error; |
| 102 | |
| 103 | if (!PyArg_ParseTuple(args, "O", &list)) |
| 104 | return NULL; |
| 105 | Py_INCREF(list); |
| 106 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 107 | if (!PyList_Check(list)) |
| 108 | { |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 109 | PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
| 110 | Py_DECREF(list); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | n = PyList_Size(list); |
| 115 | |
| 116 | for (i = 0; i < n; ++i) |
| 117 | { |
| 118 | PyObject *line = PyList_GetItem(list, i); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 119 | char *str = NULL; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 120 | PyInt len; |
| 121 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 122 | if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len)) |
| 123 | { |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 124 | PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
| 125 | Py_DECREF(list); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | Py_BEGIN_ALLOW_THREADS |
| 130 | Python_Lock_Vim(); |
| 131 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 132 | Python_Release_Vim(); |
| 133 | Py_END_ALLOW_THREADS |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 134 | PyMem_Free(str); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | Py_DECREF(list); |
| 138 | Py_INCREF(Py_None); |
| 139 | return Py_None; |
| 140 | } |
| 141 | |
Bram Moolenaar | a29a37d | 2011-03-22 15:47:44 +0100 | [diff] [blame] | 142 | static PyObject * |
| 143 | OutputFlush(PyObject *self UNUSED, PyObject *args UNUSED) |
| 144 | { |
| 145 | /* do nothing */ |
| 146 | Py_INCREF(Py_None); |
| 147 | return Py_None; |
| 148 | } |
| 149 | |
| 150 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 151 | /* Buffer IO, we write one whole line at a time. */ |
| 152 | static garray_T io_ga = {0, 0, 1, 80, NULL}; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 153 | static writefn old_fn = NULL; |
| 154 | |
| 155 | static void |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 156 | PythonIO_Flush(void) |
| 157 | { |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 158 | if (old_fn != NULL && io_ga.ga_len > 0) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 159 | { |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 160 | ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL; |
| 161 | old_fn((char_u *)io_ga.ga_data); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 162 | } |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 163 | io_ga.ga_len = 0; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static void |
| 167 | writer(writefn fn, char_u *str, PyInt n) |
| 168 | { |
| 169 | char_u *ptr; |
| 170 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 171 | /* Flush when switching output function. */ |
| 172 | if (fn != old_fn) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 173 | PythonIO_Flush(); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 174 | old_fn = fn; |
| 175 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 176 | /* Write each NL separated line. Text after the last NL is kept for |
| 177 | * writing later. */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 178 | while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) |
| 179 | { |
| 180 | PyInt len = ptr - str; |
| 181 | |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 182 | if (ga_grow(&io_ga, (int)(len + 1)) == FAIL) |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 183 | break; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 184 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 185 | mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len); |
| 186 | ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL; |
| 187 | fn((char_u *)io_ga.ga_data); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 188 | str = ptr + 1; |
| 189 | n -= len + 1; |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 190 | io_ga.ga_len = 0; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 193 | /* Put the remaining text into io_ga for later printing. */ |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 194 | if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK) |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 195 | { |
| 196 | mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n); |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 197 | io_ga.ga_len += (int)n; |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 198 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /***************/ |
| 202 | |
| 203 | static PyTypeObject OutputType; |
| 204 | |
| 205 | static OutputObject Output = |
| 206 | { |
| 207 | PyObject_HEAD_INIT(&OutputType) |
| 208 | 0, |
| 209 | 0 |
| 210 | }; |
| 211 | |
| 212 | static OutputObject Error = |
| 213 | { |
| 214 | PyObject_HEAD_INIT(&OutputType) |
| 215 | 0, |
| 216 | 1 |
| 217 | }; |
| 218 | |
| 219 | static int |
| 220 | PythonIO_Init_io(void) |
| 221 | { |
| 222 | PySys_SetObject("stdout", (PyObject *)(void *)&Output); |
| 223 | PySys_SetObject("stderr", (PyObject *)(void *)&Error); |
| 224 | |
| 225 | if (PyErr_Occurred()) |
| 226 | { |
| 227 | EMSG(_("E264: Python: Error initialising I/O objects")); |
| 228 | return -1; |
| 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static PyObject *VimError; |
| 236 | |
| 237 | /* Check to see whether a Vim error has been reported, or a keyboard |
| 238 | * interrupt has been detected. |
| 239 | */ |
| 240 | static int |
| 241 | VimErrorCheck(void) |
| 242 | { |
| 243 | if (got_int) |
| 244 | { |
| 245 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 246 | return 1; |
| 247 | } |
| 248 | else if (did_emsg && !PyErr_Occurred()) |
| 249 | { |
| 250 | PyErr_SetNone(VimError); |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 256 | |
| 257 | /* Vim module - Implementation |
| 258 | */ |
| 259 | static PyObject * |
| 260 | VimCommand(PyObject *self UNUSED, PyObject *args) |
| 261 | { |
| 262 | char *cmd; |
| 263 | PyObject *result; |
| 264 | |
| 265 | if (!PyArg_ParseTuple(args, "s", &cmd)) |
| 266 | return NULL; |
| 267 | |
| 268 | PyErr_Clear(); |
| 269 | |
| 270 | Py_BEGIN_ALLOW_THREADS |
| 271 | Python_Lock_Vim(); |
| 272 | |
| 273 | do_cmdline_cmd((char_u *)cmd); |
| 274 | update_screen(VALID); |
| 275 | |
| 276 | Python_Release_Vim(); |
| 277 | Py_END_ALLOW_THREADS |
| 278 | |
| 279 | if (VimErrorCheck()) |
| 280 | result = NULL; |
| 281 | else |
| 282 | result = Py_None; |
| 283 | |
| 284 | Py_XINCREF(result); |
| 285 | return result; |
| 286 | } |
| 287 | |
| 288 | #ifdef FEAT_EVAL |
| 289 | /* |
| 290 | * Function to translate a typval_T into a PyObject; this will recursively |
| 291 | * translate lists/dictionaries into their Python equivalents. |
| 292 | * |
| 293 | * The depth parameter is to avoid infinite recursion, set it to 1 when |
| 294 | * you call VimToPython. |
| 295 | */ |
| 296 | static PyObject * |
| 297 | VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) |
| 298 | { |
| 299 | PyObject *result; |
| 300 | PyObject *newObj; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 301 | char ptrBuf[sizeof(void *) * 2 + 3]; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 302 | |
| 303 | /* Avoid infinite recursion */ |
| 304 | if (depth > 100) |
| 305 | { |
| 306 | Py_INCREF(Py_None); |
| 307 | result = Py_None; |
| 308 | return result; |
| 309 | } |
| 310 | |
| 311 | /* Check if we run into a recursive loop. The item must be in lookupDict |
| 312 | * then and we can use it again. */ |
| 313 | if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) |
| 314 | || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL)) |
| 315 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 316 | sprintf(ptrBuf, "%p", |
| 317 | our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list |
| 318 | : (void *)our_tv->vval.v_dict); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 319 | result = PyDict_GetItemString(lookupDict, ptrBuf); |
| 320 | if (result != NULL) |
| 321 | { |
| 322 | Py_INCREF(result); |
| 323 | return result; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (our_tv->v_type == VAR_STRING) |
| 328 | { |
| 329 | result = Py_BuildValue("s", our_tv->vval.v_string); |
| 330 | } |
| 331 | else if (our_tv->v_type == VAR_NUMBER) |
| 332 | { |
| 333 | char buf[NUMBUFLEN]; |
| 334 | |
| 335 | /* For backwards compatibility numbers are stored as strings. */ |
| 336 | sprintf(buf, "%ld", (long)our_tv->vval.v_number); |
| 337 | result = Py_BuildValue("s", buf); |
| 338 | } |
| 339 | # ifdef FEAT_FLOAT |
| 340 | else if (our_tv->v_type == VAR_FLOAT) |
| 341 | { |
| 342 | char buf[NUMBUFLEN]; |
| 343 | |
| 344 | sprintf(buf, "%f", our_tv->vval.v_float); |
| 345 | result = Py_BuildValue("s", buf); |
| 346 | } |
| 347 | # endif |
| 348 | else if (our_tv->v_type == VAR_LIST) |
| 349 | { |
| 350 | list_T *list = our_tv->vval.v_list; |
| 351 | listitem_T *curr; |
| 352 | |
| 353 | result = PyList_New(0); |
| 354 | |
| 355 | if (list != NULL) |
| 356 | { |
| 357 | PyDict_SetItemString(lookupDict, ptrBuf, result); |
| 358 | |
| 359 | for (curr = list->lv_first; curr != NULL; curr = curr->li_next) |
| 360 | { |
| 361 | newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict); |
| 362 | PyList_Append(result, newObj); |
| 363 | Py_DECREF(newObj); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | else if (our_tv->v_type == VAR_DICT) |
| 368 | { |
| 369 | result = PyDict_New(); |
| 370 | |
| 371 | if (our_tv->vval.v_dict != NULL) |
| 372 | { |
| 373 | hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab; |
| 374 | long_u todo = ht->ht_used; |
| 375 | hashitem_T *hi; |
| 376 | dictitem_T *di; |
| 377 | |
| 378 | PyDict_SetItemString(lookupDict, ptrBuf, result); |
| 379 | |
| 380 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 381 | { |
| 382 | if (!HASHITEM_EMPTY(hi)) |
| 383 | { |
| 384 | --todo; |
| 385 | |
| 386 | di = dict_lookup(hi); |
| 387 | newObj = VimToPython(&di->di_tv, depth + 1, lookupDict); |
| 388 | PyDict_SetItemString(result, (char *)hi->hi_key, newObj); |
| 389 | Py_DECREF(newObj); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | Py_INCREF(Py_None); |
| 397 | result = Py_None; |
| 398 | } |
| 399 | |
| 400 | return result; |
| 401 | } |
| 402 | #endif |
| 403 | |
| 404 | static PyObject * |
Bram Moolenaar | 0909215 | 2010-08-08 16:38:42 +0200 | [diff] [blame] | 405 | VimEval(PyObject *self UNUSED, PyObject *args UNUSED) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 406 | { |
| 407 | #ifdef FEAT_EVAL |
| 408 | char *expr; |
| 409 | typval_T *our_tv; |
| 410 | PyObject *result; |
| 411 | PyObject *lookup_dict; |
| 412 | |
| 413 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 414 | return NULL; |
| 415 | |
| 416 | Py_BEGIN_ALLOW_THREADS |
| 417 | Python_Lock_Vim(); |
| 418 | our_tv = eval_expr((char_u *)expr, NULL); |
| 419 | |
| 420 | Python_Release_Vim(); |
| 421 | Py_END_ALLOW_THREADS |
| 422 | |
| 423 | if (our_tv == NULL) |
| 424 | { |
| 425 | PyErr_SetVim(_("invalid expression")); |
| 426 | return NULL; |
| 427 | } |
| 428 | |
| 429 | /* Convert the Vim type into a Python type. Create a dictionary that's |
| 430 | * used to check for recursive loops. */ |
| 431 | lookup_dict = PyDict_New(); |
| 432 | result = VimToPython(our_tv, 1, lookup_dict); |
| 433 | Py_DECREF(lookup_dict); |
| 434 | |
| 435 | |
| 436 | Py_BEGIN_ALLOW_THREADS |
| 437 | Python_Lock_Vim(); |
| 438 | free_tv(our_tv); |
| 439 | Python_Release_Vim(); |
| 440 | Py_END_ALLOW_THREADS |
| 441 | |
| 442 | return result; |
| 443 | #else |
| 444 | PyErr_SetVim(_("expressions disabled at compile time")); |
| 445 | return NULL; |
| 446 | #endif |
| 447 | } |
| 448 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 449 | static PyObject *ConvertToPyObject(typval_T *); |
| 450 | |
| 451 | static PyObject * |
| 452 | VimEvalPy(PyObject *self UNUSED, PyObject *args UNUSED) |
| 453 | { |
| 454 | #ifdef FEAT_EVAL |
| 455 | char *expr; |
| 456 | typval_T *our_tv; |
| 457 | PyObject *result; |
| 458 | |
| 459 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 460 | return NULL; |
| 461 | |
| 462 | Py_BEGIN_ALLOW_THREADS |
| 463 | Python_Lock_Vim(); |
| 464 | our_tv = eval_expr((char_u *)expr, NULL); |
| 465 | |
| 466 | Python_Release_Vim(); |
| 467 | Py_END_ALLOW_THREADS |
| 468 | |
| 469 | if (our_tv == NULL) |
| 470 | { |
| 471 | PyErr_SetVim(_("invalid expression")); |
| 472 | return NULL; |
| 473 | } |
| 474 | |
| 475 | result = ConvertToPyObject(our_tv); |
| 476 | Py_BEGIN_ALLOW_THREADS |
| 477 | Python_Lock_Vim(); |
| 478 | free_tv(our_tv); |
| 479 | Python_Release_Vim(); |
| 480 | Py_END_ALLOW_THREADS |
| 481 | |
| 482 | return result; |
| 483 | #else |
| 484 | PyErr_SetVim(_("expressions disabled at compile time")); |
| 485 | return NULL; |
| 486 | #endif |
| 487 | } |
| 488 | |
| 489 | static PyObject * |
| 490 | VimStrwidth(PyObject *self UNUSED, PyObject *args) |
| 491 | { |
| 492 | char *expr; |
| 493 | |
| 494 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 495 | return NULL; |
| 496 | |
Bram Moolenaar | 3cd3e7a | 2012-06-29 17:52:02 +0200 | [diff] [blame] | 497 | return PyLong_FromLong(mb_string2cells((char_u *)expr, (int)STRLEN(expr))); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 500 | /* |
| 501 | * Vim module - Definitions |
| 502 | */ |
| 503 | |
| 504 | static struct PyMethodDef VimMethods[] = { |
| 505 | /* name, function, calling, documentation */ |
| 506 | {"command", VimCommand, 1, "Execute a Vim ex-mode command" }, |
| 507 | {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" }, |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 508 | {"bindeval", VimEvalPy, 1, "Like eval(), but returns objects attached to vim ones"}, |
| 509 | {"strwidth", VimStrwidth, 1, "Screen string width, counts <Tab> as having width 1"}, |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 510 | { NULL, NULL, 0, NULL } |
| 511 | }; |
| 512 | |
| 513 | typedef struct |
| 514 | { |
| 515 | PyObject_HEAD |
| 516 | buf_T *buf; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 517 | } BufferObject; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 518 | |
| 519 | #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) |
| 520 | |
| 521 | /* |
| 522 | * Buffer list object - Implementation |
| 523 | */ |
| 524 | |
| 525 | static PyInt |
| 526 | BufListLength(PyObject *self UNUSED) |
| 527 | { |
| 528 | buf_T *b = firstbuf; |
| 529 | PyInt n = 0; |
| 530 | |
| 531 | while (b) |
| 532 | { |
| 533 | ++n; |
| 534 | b = b->b_next; |
| 535 | } |
| 536 | |
| 537 | return n; |
| 538 | } |
| 539 | |
| 540 | static PyObject * |
| 541 | BufListItem(PyObject *self UNUSED, PyInt n) |
| 542 | { |
| 543 | buf_T *b; |
| 544 | |
| 545 | for (b = firstbuf; b; b = b->b_next, --n) |
| 546 | { |
| 547 | if (n == 0) |
| 548 | return BufferNew(b); |
| 549 | } |
| 550 | |
| 551 | PyErr_SetString(PyExc_IndexError, _("no such buffer")); |
| 552 | return NULL; |
| 553 | } |
| 554 | |
| 555 | typedef struct |
| 556 | { |
| 557 | PyObject_HEAD |
| 558 | win_T *win; |
| 559 | } WindowObject; |
| 560 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 561 | static int ConvertFromPyObject(PyObject *, typval_T *); |
| 562 | static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *); |
| 563 | |
| 564 | typedef struct pylinkedlist_S { |
| 565 | struct pylinkedlist_S *pll_next; |
| 566 | struct pylinkedlist_S *pll_prev; |
| 567 | PyObject *pll_obj; |
| 568 | } pylinkedlist_T; |
| 569 | |
| 570 | static pylinkedlist_T *lastdict = NULL; |
| 571 | static pylinkedlist_T *lastlist = NULL; |
| 572 | |
| 573 | static void |
| 574 | pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last) |
| 575 | { |
| 576 | if (ref->pll_prev == NULL) |
| 577 | { |
| 578 | if (ref->pll_next == NULL) |
| 579 | { |
| 580 | *last = NULL; |
| 581 | return; |
| 582 | } |
| 583 | } |
| 584 | else |
| 585 | ref->pll_prev->pll_next = ref->pll_next; |
| 586 | |
| 587 | if (ref->pll_next == NULL) |
| 588 | *last = ref->pll_prev; |
| 589 | else |
| 590 | ref->pll_next->pll_prev = ref->pll_prev; |
| 591 | } |
| 592 | |
| 593 | static void |
| 594 | pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last) |
| 595 | { |
| 596 | if (*last == NULL) |
| 597 | ref->pll_prev = NULL; |
| 598 | else |
| 599 | { |
| 600 | (*last)->pll_next = ref; |
| 601 | ref->pll_prev = *last; |
| 602 | } |
| 603 | ref->pll_next = NULL; |
| 604 | ref->pll_obj = self; |
| 605 | *last = ref; |
| 606 | } |
| 607 | |
| 608 | static PyTypeObject DictionaryType; |
| 609 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 610 | #define DICTKEY_GET_NOTEMPTY(err) \ |
| 611 | DICTKEY_GET(err) \ |
| 612 | if (*key == NUL) \ |
| 613 | { \ |
| 614 | PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \ |
| 615 | return err; \ |
| 616 | } |
| 617 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 618 | typedef struct |
| 619 | { |
| 620 | PyObject_HEAD |
| 621 | dict_T *dict; |
| 622 | pylinkedlist_T ref; |
| 623 | } DictionaryObject; |
| 624 | |
| 625 | static PyObject * |
| 626 | DictionaryNew(dict_T *dict) |
| 627 | { |
| 628 | DictionaryObject *self; |
| 629 | |
| 630 | self = PyObject_NEW(DictionaryObject, &DictionaryType); |
| 631 | if (self == NULL) |
| 632 | return NULL; |
| 633 | self->dict = dict; |
| 634 | ++dict->dv_refcount; |
| 635 | |
| 636 | pyll_add((PyObject *)(self), &self->ref, &lastdict); |
| 637 | |
| 638 | return (PyObject *)(self); |
| 639 | } |
| 640 | |
| 641 | static int |
| 642 | pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 643 | { |
| 644 | dict_T *d; |
| 645 | char_u *key; |
| 646 | dictitem_T *di; |
| 647 | PyObject *keyObject; |
| 648 | PyObject *valObject; |
| 649 | Py_ssize_t iter = 0; |
| 650 | |
| 651 | d = dict_alloc(); |
| 652 | if (d == NULL) |
| 653 | { |
| 654 | PyErr_NoMemory(); |
| 655 | return -1; |
| 656 | } |
| 657 | |
| 658 | tv->v_type = VAR_DICT; |
| 659 | tv->vval.v_dict = d; |
| 660 | |
| 661 | while (PyDict_Next(obj, &iter, &keyObject, &valObject)) |
| 662 | { |
| 663 | DICTKEY_DECL |
| 664 | |
| 665 | if (keyObject == NULL) |
| 666 | return -1; |
| 667 | if (valObject == NULL) |
| 668 | return -1; |
| 669 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 670 | DICTKEY_GET_NOTEMPTY(-1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 671 | |
| 672 | di = dictitem_alloc(key); |
| 673 | |
| 674 | DICTKEY_UNREF |
| 675 | |
| 676 | if (di == NULL) |
| 677 | { |
| 678 | PyErr_NoMemory(); |
| 679 | return -1; |
| 680 | } |
| 681 | di->di_tv.v_lock = 0; |
| 682 | |
| 683 | if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) |
| 684 | { |
| 685 | vim_free(di); |
| 686 | return -1; |
| 687 | } |
| 688 | if (dict_add(d, di) == FAIL) |
| 689 | { |
| 690 | vim_free(di); |
| 691 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 692 | return -1; |
| 693 | } |
| 694 | } |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | static int |
| 699 | pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 700 | { |
| 701 | dict_T *d; |
| 702 | char_u *key; |
| 703 | dictitem_T *di; |
| 704 | PyObject *list; |
| 705 | PyObject *litem; |
| 706 | PyObject *keyObject; |
| 707 | PyObject *valObject; |
| 708 | Py_ssize_t lsize; |
| 709 | |
| 710 | d = dict_alloc(); |
| 711 | if (d == NULL) |
| 712 | { |
| 713 | PyErr_NoMemory(); |
| 714 | return -1; |
| 715 | } |
| 716 | |
| 717 | tv->v_type = VAR_DICT; |
| 718 | tv->vval.v_dict = d; |
| 719 | |
| 720 | list = PyMapping_Items(obj); |
| 721 | lsize = PyList_Size(list); |
| 722 | while (lsize--) |
| 723 | { |
| 724 | DICTKEY_DECL |
| 725 | |
| 726 | litem = PyList_GetItem(list, lsize); |
| 727 | if (litem == NULL) |
| 728 | { |
| 729 | Py_DECREF(list); |
| 730 | return -1; |
| 731 | } |
| 732 | |
| 733 | keyObject = PyTuple_GetItem(litem, 0); |
| 734 | if (keyObject == NULL) |
| 735 | { |
| 736 | Py_DECREF(list); |
| 737 | Py_DECREF(litem); |
| 738 | return -1; |
| 739 | } |
| 740 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 741 | DICTKEY_GET_NOTEMPTY(-1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 742 | |
| 743 | valObject = PyTuple_GetItem(litem, 1); |
| 744 | if (valObject == NULL) |
| 745 | { |
| 746 | Py_DECREF(list); |
| 747 | Py_DECREF(litem); |
| 748 | return -1; |
| 749 | } |
| 750 | |
| 751 | di = dictitem_alloc(key); |
| 752 | |
| 753 | DICTKEY_UNREF |
| 754 | |
| 755 | if (di == NULL) |
| 756 | { |
| 757 | Py_DECREF(list); |
| 758 | Py_DECREF(litem); |
| 759 | PyErr_NoMemory(); |
| 760 | return -1; |
| 761 | } |
| 762 | di->di_tv.v_lock = 0; |
| 763 | |
| 764 | if (_ConvertFromPyObject(valObject, &di->di_tv, lookupDict) == -1) |
| 765 | { |
| 766 | vim_free(di); |
| 767 | Py_DECREF(list); |
| 768 | Py_DECREF(litem); |
| 769 | return -1; |
| 770 | } |
| 771 | if (dict_add(d, di) == FAIL) |
| 772 | { |
| 773 | vim_free(di); |
| 774 | Py_DECREF(list); |
| 775 | Py_DECREF(litem); |
| 776 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 777 | return -1; |
| 778 | } |
| 779 | Py_DECREF(litem); |
| 780 | } |
| 781 | Py_DECREF(list); |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | static PyInt |
| 786 | DictionaryLength(PyObject *self) |
| 787 | { |
| 788 | return ((PyInt) ((((DictionaryObject *)(self))->dict->dv_hashtab.ht_used))); |
| 789 | } |
| 790 | |
| 791 | static PyObject * |
| 792 | DictionaryItem(PyObject *self, PyObject *keyObject) |
| 793 | { |
| 794 | char_u *key; |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 795 | dictitem_T *di; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 796 | DICTKEY_DECL |
| 797 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 798 | DICTKEY_GET_NOTEMPTY(NULL) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 799 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 800 | di = dict_find(((DictionaryObject *) (self))->dict, key, -1); |
| 801 | |
| 802 | if (di == NULL) |
| 803 | { |
| 804 | PyErr_SetString(PyExc_IndexError, _("no such key in dictionary")); |
| 805 | return NULL; |
| 806 | } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 807 | |
| 808 | DICTKEY_UNREF |
| 809 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 810 | return ConvertToPyObject(&di->di_tv); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | static PyInt |
| 814 | DictionaryAssItem(PyObject *self, PyObject *keyObject, PyObject *valObject) |
| 815 | { |
| 816 | char_u *key; |
| 817 | typval_T tv; |
| 818 | dict_T *d = ((DictionaryObject *)(self))->dict; |
| 819 | dictitem_T *di; |
| 820 | DICTKEY_DECL |
| 821 | |
| 822 | if (d->dv_lock) |
| 823 | { |
| 824 | PyErr_SetVim(_("dict is locked")); |
| 825 | return -1; |
| 826 | } |
| 827 | |
Bram Moolenaar | 231e1a1 | 2012-09-05 18:45:28 +0200 | [diff] [blame] | 828 | DICTKEY_GET_NOTEMPTY(-1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 829 | |
| 830 | di = dict_find(d, key, -1); |
| 831 | |
| 832 | if (valObject == NULL) |
| 833 | { |
Bram Moolenaar | f27839c | 2012-06-29 16:19:50 +0200 | [diff] [blame] | 834 | hashitem_T *hi; |
| 835 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 836 | if (di == NULL) |
| 837 | { |
| 838 | PyErr_SetString(PyExc_IndexError, _("no such key in dictionary")); |
| 839 | return -1; |
| 840 | } |
Bram Moolenaar | f27839c | 2012-06-29 16:19:50 +0200 | [diff] [blame] | 841 | hi = hash_find(&d->dv_hashtab, di->di_key); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 842 | hash_remove(&d->dv_hashtab, hi); |
| 843 | dictitem_free(di); |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | if (ConvertFromPyObject(valObject, &tv) == -1) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 848 | return -1; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 849 | |
| 850 | if (di == NULL) |
| 851 | { |
| 852 | di = dictitem_alloc(key); |
| 853 | if (di == NULL) |
| 854 | { |
| 855 | PyErr_NoMemory(); |
| 856 | return -1; |
| 857 | } |
| 858 | di->di_tv.v_lock = 0; |
| 859 | |
| 860 | if (dict_add(d, di) == FAIL) |
| 861 | { |
| 862 | vim_free(di); |
| 863 | PyErr_SetVim(_("failed to add key to dictionary")); |
| 864 | return -1; |
| 865 | } |
| 866 | } |
| 867 | else |
| 868 | clear_tv(&di->di_tv); |
| 869 | |
| 870 | DICTKEY_UNREF |
| 871 | |
| 872 | copy_tv(&tv, &di->di_tv); |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | static PyObject * |
| 877 | DictionaryListKeys(PyObject *self) |
| 878 | { |
| 879 | dict_T *dict = ((DictionaryObject *)(self))->dict; |
| 880 | long_u todo = dict->dv_hashtab.ht_used; |
| 881 | Py_ssize_t i = 0; |
| 882 | PyObject *r; |
| 883 | hashitem_T *hi; |
| 884 | |
| 885 | r = PyList_New(todo); |
| 886 | for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi) |
| 887 | { |
| 888 | if (!HASHITEM_EMPTY(hi)) |
| 889 | { |
| 890 | PyList_SetItem(r, i, PyBytes_FromString((char *)(hi->hi_key))); |
| 891 | --todo; |
| 892 | ++i; |
| 893 | } |
| 894 | } |
| 895 | return r; |
| 896 | } |
| 897 | |
| 898 | static struct PyMethodDef DictionaryMethods[] = { |
| 899 | {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""}, |
| 900 | { NULL, NULL, 0, NULL } |
| 901 | }; |
| 902 | |
| 903 | static PyTypeObject ListType; |
| 904 | |
| 905 | typedef struct |
| 906 | { |
| 907 | PyObject_HEAD |
| 908 | list_T *list; |
| 909 | pylinkedlist_T ref; |
| 910 | } ListObject; |
| 911 | |
| 912 | static PyObject * |
| 913 | ListNew(list_T *list) |
| 914 | { |
| 915 | ListObject *self; |
| 916 | |
| 917 | self = PyObject_NEW(ListObject, &ListType); |
| 918 | if (self == NULL) |
| 919 | return NULL; |
| 920 | self->list = list; |
| 921 | ++list->lv_refcount; |
| 922 | |
| 923 | pyll_add((PyObject *)(self), &self->ref, &lastlist); |
| 924 | |
| 925 | return (PyObject *)(self); |
| 926 | } |
| 927 | |
| 928 | static int |
| 929 | list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict) |
| 930 | { |
| 931 | Py_ssize_t i; |
| 932 | Py_ssize_t lsize = PySequence_Size(obj); |
| 933 | PyObject *litem; |
| 934 | listitem_T *li; |
| 935 | |
| 936 | for(i=0; i<lsize; i++) |
| 937 | { |
| 938 | li = listitem_alloc(); |
| 939 | if (li == NULL) |
| 940 | { |
| 941 | PyErr_NoMemory(); |
| 942 | return -1; |
| 943 | } |
| 944 | li->li_tv.v_lock = 0; |
| 945 | |
| 946 | litem = PySequence_GetItem(obj, i); |
| 947 | if (litem == NULL) |
| 948 | return -1; |
| 949 | if (_ConvertFromPyObject(litem, &li->li_tv, lookupDict) == -1) |
| 950 | return -1; |
| 951 | |
| 952 | list_append(l, li); |
| 953 | } |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | static int |
| 958 | pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 959 | { |
| 960 | list_T *l; |
| 961 | |
| 962 | l = list_alloc(); |
| 963 | if (l == NULL) |
| 964 | { |
| 965 | PyErr_NoMemory(); |
| 966 | return -1; |
| 967 | } |
| 968 | |
| 969 | tv->v_type = VAR_LIST; |
| 970 | tv->vval.v_list = l; |
| 971 | |
| 972 | if (list_py_concat(l, obj, lookupDict) == -1) |
| 973 | return -1; |
| 974 | |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | static int |
| 979 | pyiter_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 980 | { |
| 981 | PyObject *iterator = PyObject_GetIter(obj); |
| 982 | PyObject *item; |
| 983 | list_T *l; |
| 984 | listitem_T *li; |
| 985 | |
| 986 | l = list_alloc(); |
| 987 | |
| 988 | if (l == NULL) |
| 989 | { |
| 990 | PyErr_NoMemory(); |
| 991 | return -1; |
| 992 | } |
| 993 | |
| 994 | tv->vval.v_list = l; |
| 995 | tv->v_type = VAR_LIST; |
| 996 | |
| 997 | |
| 998 | if (iterator == NULL) |
| 999 | return -1; |
| 1000 | |
| 1001 | while ((item = PyIter_Next(obj))) |
| 1002 | { |
| 1003 | li = listitem_alloc(); |
| 1004 | if (li == NULL) |
| 1005 | { |
| 1006 | PyErr_NoMemory(); |
| 1007 | return -1; |
| 1008 | } |
| 1009 | li->li_tv.v_lock = 0; |
| 1010 | |
| 1011 | if (_ConvertFromPyObject(item, &li->li_tv, lookupDict) == -1) |
| 1012 | return -1; |
| 1013 | |
| 1014 | list_append(l, li); |
| 1015 | |
| 1016 | Py_DECREF(item); |
| 1017 | } |
| 1018 | |
| 1019 | Py_DECREF(iterator); |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
| 1023 | static PyInt |
| 1024 | ListLength(PyObject *self) |
| 1025 | { |
| 1026 | return ((PyInt) (((ListObject *) (self))->list->lv_len)); |
| 1027 | } |
| 1028 | |
| 1029 | static PyObject * |
| 1030 | ListItem(PyObject *self, Py_ssize_t index) |
| 1031 | { |
| 1032 | listitem_T *li; |
| 1033 | |
| 1034 | if (index>=ListLength(self)) |
| 1035 | { |
| 1036 | PyErr_SetString(PyExc_IndexError, "list index out of range"); |
| 1037 | return NULL; |
| 1038 | } |
| 1039 | li = list_find(((ListObject *) (self))->list, (long) index); |
| 1040 | if (li == NULL) |
| 1041 | { |
| 1042 | PyErr_SetVim(_("internal error: failed to get vim list item")); |
| 1043 | return NULL; |
| 1044 | } |
| 1045 | return ConvertToPyObject(&li->li_tv); |
| 1046 | } |
| 1047 | |
| 1048 | #define PROC_RANGE \ |
| 1049 | if (last < 0) {\ |
| 1050 | if (last < -size) \ |
| 1051 | last = 0; \ |
| 1052 | else \ |
| 1053 | last += size; \ |
| 1054 | } \ |
| 1055 | if (first < 0) \ |
| 1056 | first = 0; \ |
| 1057 | if (first > size) \ |
| 1058 | first = size; \ |
| 1059 | if (last > size) \ |
| 1060 | last = size; |
| 1061 | |
| 1062 | static PyObject * |
| 1063 | ListSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last) |
| 1064 | { |
| 1065 | PyInt i; |
| 1066 | PyInt size = ListLength(self); |
| 1067 | PyInt n; |
| 1068 | PyObject *list; |
| 1069 | int reversed = 0; |
| 1070 | |
| 1071 | PROC_RANGE |
| 1072 | if (first >= last) |
| 1073 | first = last; |
| 1074 | |
| 1075 | n = last-first; |
| 1076 | list = PyList_New(n); |
| 1077 | if (list == NULL) |
| 1078 | return NULL; |
| 1079 | |
| 1080 | for (i = 0; i < n; ++i) |
| 1081 | { |
| 1082 | PyObject *item = ListItem(self, i); |
| 1083 | if (item == NULL) |
| 1084 | { |
| 1085 | Py_DECREF(list); |
| 1086 | return NULL; |
| 1087 | } |
| 1088 | |
| 1089 | if ((PyList_SetItem(list, ((reversed)?(n-i-1):(i)), item))) |
| 1090 | { |
| 1091 | Py_DECREF(item); |
| 1092 | Py_DECREF(list); |
| 1093 | return NULL; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | return list; |
| 1098 | } |
| 1099 | |
| 1100 | static int |
| 1101 | ListAssItem(PyObject *self, Py_ssize_t index, PyObject *obj) |
| 1102 | { |
| 1103 | typval_T tv; |
| 1104 | list_T *l = ((ListObject *) (self))->list; |
| 1105 | listitem_T *li; |
| 1106 | Py_ssize_t length = ListLength(self); |
| 1107 | |
| 1108 | if (l->lv_lock) |
| 1109 | { |
| 1110 | PyErr_SetVim(_("list is locked")); |
| 1111 | return -1; |
| 1112 | } |
| 1113 | if (index>length || (index==length && obj==NULL)) |
| 1114 | { |
| 1115 | PyErr_SetString(PyExc_IndexError, "list index out of range"); |
| 1116 | return -1; |
| 1117 | } |
| 1118 | |
| 1119 | if (obj == NULL) |
| 1120 | { |
| 1121 | li = list_find(l, (long) index); |
| 1122 | list_remove(l, li, li); |
| 1123 | clear_tv(&li->li_tv); |
| 1124 | vim_free(li); |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | if (ConvertFromPyObject(obj, &tv) == -1) |
| 1129 | return -1; |
| 1130 | |
| 1131 | if (index == length) |
| 1132 | { |
| 1133 | if (list_append_tv(l, &tv) == FAIL) |
| 1134 | { |
| 1135 | PyErr_SetVim(_("Failed to add item to list")); |
| 1136 | return -1; |
| 1137 | } |
| 1138 | } |
| 1139 | else |
| 1140 | { |
| 1141 | li = list_find(l, (long) index); |
| 1142 | clear_tv(&li->li_tv); |
| 1143 | copy_tv(&tv, &li->li_tv); |
| 1144 | } |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
| 1148 | static int |
| 1149 | ListAssSlice(PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj) |
| 1150 | { |
| 1151 | PyInt size = ListLength(self); |
| 1152 | Py_ssize_t i; |
| 1153 | Py_ssize_t lsize; |
| 1154 | PyObject *litem; |
| 1155 | listitem_T *li; |
| 1156 | listitem_T *next; |
| 1157 | typval_T v; |
| 1158 | list_T *l = ((ListObject *) (self))->list; |
| 1159 | |
| 1160 | if (l->lv_lock) |
| 1161 | { |
| 1162 | PyErr_SetVim(_("list is locked")); |
| 1163 | return -1; |
| 1164 | } |
| 1165 | |
| 1166 | PROC_RANGE |
| 1167 | |
| 1168 | if (first == size) |
| 1169 | li = NULL; |
| 1170 | else |
| 1171 | { |
| 1172 | li = list_find(l, (long) first); |
| 1173 | if (li == NULL) |
| 1174 | { |
| 1175 | PyErr_SetVim(_("internal error: no vim list item")); |
| 1176 | return -1; |
| 1177 | } |
| 1178 | if (last > first) |
| 1179 | { |
| 1180 | i = last - first; |
| 1181 | while (i-- && li != NULL) |
| 1182 | { |
| 1183 | next = li->li_next; |
| 1184 | listitem_remove(l, li); |
| 1185 | li = next; |
| 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | if (obj == NULL) |
| 1191 | return 0; |
| 1192 | |
| 1193 | if (!PyList_Check(obj)) |
| 1194 | { |
| 1195 | PyErr_SetString(PyExc_TypeError, _("can only assign lists to slice")); |
| 1196 | return -1; |
| 1197 | } |
| 1198 | |
| 1199 | lsize = PyList_Size(obj); |
| 1200 | |
| 1201 | for(i=0; i<lsize; i++) |
| 1202 | { |
| 1203 | litem = PyList_GetItem(obj, i); |
| 1204 | if (litem == NULL) |
| 1205 | return -1; |
| 1206 | if (ConvertFromPyObject(litem, &v) == -1) |
| 1207 | return -1; |
| 1208 | if (list_insert_tv(l, &v, li) == FAIL) |
| 1209 | { |
| 1210 | PyErr_SetVim(_("internal error: failed to add item to list")); |
| 1211 | return -1; |
| 1212 | } |
| 1213 | } |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | static PyObject * |
| 1218 | ListConcatInPlace(PyObject *self, PyObject *obj) |
| 1219 | { |
| 1220 | list_T *l = ((ListObject *) (self))->list; |
| 1221 | PyObject *lookup_dict; |
| 1222 | |
| 1223 | if (l->lv_lock) |
| 1224 | { |
| 1225 | PyErr_SetVim(_("list is locked")); |
| 1226 | return NULL; |
| 1227 | } |
| 1228 | |
| 1229 | if (!PySequence_Check(obj)) |
| 1230 | { |
| 1231 | PyErr_SetString(PyExc_TypeError, _("can only concatenate with lists")); |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
| 1235 | lookup_dict = PyDict_New(); |
| 1236 | if (list_py_concat(l, obj, lookup_dict) == -1) |
| 1237 | { |
| 1238 | Py_DECREF(lookup_dict); |
| 1239 | return NULL; |
| 1240 | } |
| 1241 | Py_DECREF(lookup_dict); |
| 1242 | |
| 1243 | Py_INCREF(self); |
| 1244 | return self; |
| 1245 | } |
| 1246 | |
| 1247 | static struct PyMethodDef ListMethods[] = { |
| 1248 | {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""}, |
| 1249 | { NULL, NULL, 0, NULL } |
| 1250 | }; |
| 1251 | |
| 1252 | typedef struct |
| 1253 | { |
| 1254 | PyObject_HEAD |
| 1255 | char_u *name; |
| 1256 | } FunctionObject; |
| 1257 | |
| 1258 | static PyTypeObject FunctionType; |
| 1259 | |
| 1260 | static PyObject * |
| 1261 | FunctionNew(char_u *name) |
| 1262 | { |
| 1263 | FunctionObject *self; |
| 1264 | |
| 1265 | self = PyObject_NEW(FunctionObject, &FunctionType); |
| 1266 | if (self == NULL) |
| 1267 | return NULL; |
| 1268 | self->name = PyMem_New(char_u, STRLEN(name) + 1); |
| 1269 | if (self->name == NULL) |
| 1270 | { |
| 1271 | PyErr_NoMemory(); |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | STRCPY(self->name, name); |
| 1275 | func_ref(name); |
| 1276 | return (PyObject *)(self); |
| 1277 | } |
| 1278 | |
| 1279 | static PyObject * |
| 1280 | FunctionCall(PyObject *self, PyObject *argsObject, PyObject *kwargs) |
| 1281 | { |
| 1282 | FunctionObject *this = (FunctionObject *)(self); |
| 1283 | char_u *name = this->name; |
| 1284 | typval_T args; |
| 1285 | typval_T selfdicttv; |
| 1286 | typval_T rettv; |
| 1287 | dict_T *selfdict = NULL; |
| 1288 | PyObject *selfdictObject; |
| 1289 | PyObject *result; |
| 1290 | int error; |
| 1291 | |
| 1292 | if (ConvertFromPyObject(argsObject, &args) == -1) |
| 1293 | return NULL; |
| 1294 | |
| 1295 | if (kwargs != NULL) |
| 1296 | { |
| 1297 | selfdictObject = PyDict_GetItemString(kwargs, "self"); |
| 1298 | if (selfdictObject != NULL) |
| 1299 | { |
Bram Moolenaar | 9581b5f | 2012-07-25 15:36:04 +0200 | [diff] [blame] | 1300 | if (!PyMapping_Check(selfdictObject)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1301 | { |
Bram Moolenaar | 9581b5f | 2012-07-25 15:36:04 +0200 | [diff] [blame] | 1302 | PyErr_SetString(PyExc_TypeError, |
| 1303 | _("'self' argument must be a dictionary")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1304 | clear_tv(&args); |
| 1305 | return NULL; |
| 1306 | } |
| 1307 | if (ConvertFromPyObject(selfdictObject, &selfdicttv) == -1) |
| 1308 | return NULL; |
| 1309 | selfdict = selfdicttv.vval.v_dict; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | error = func_call(name, &args, selfdict, &rettv); |
| 1314 | if (error != OK) |
| 1315 | { |
| 1316 | result = NULL; |
| 1317 | PyErr_SetVim(_("failed to run function")); |
| 1318 | } |
| 1319 | else |
| 1320 | result = ConvertToPyObject(&rettv); |
| 1321 | |
| 1322 | /* FIXME Check what should really be cleared. */ |
| 1323 | clear_tv(&args); |
| 1324 | clear_tv(&rettv); |
| 1325 | /* |
| 1326 | * if (selfdict!=NULL) |
| 1327 | * clear_tv(selfdicttv); |
| 1328 | */ |
| 1329 | |
| 1330 | return result; |
| 1331 | } |
| 1332 | |
| 1333 | static struct PyMethodDef FunctionMethods[] = { |
| 1334 | {"__call__", (PyCFunction)FunctionCall, METH_VARARGS|METH_KEYWORDS, ""}, |
| 1335 | { NULL, NULL, 0, NULL } |
| 1336 | }; |
| 1337 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1338 | #define INVALID_WINDOW_VALUE ((win_T *)(-1)) |
| 1339 | |
| 1340 | static int |
| 1341 | CheckWindow(WindowObject *this) |
| 1342 | { |
| 1343 | if (this->win == INVALID_WINDOW_VALUE) |
| 1344 | { |
| 1345 | PyErr_SetVim(_("attempt to refer to deleted window")); |
| 1346 | return -1; |
| 1347 | } |
| 1348 | |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
| 1352 | static int WindowSetattr(PyObject *, char *, PyObject *); |
| 1353 | static PyObject *WindowRepr(PyObject *); |
| 1354 | |
| 1355 | static int |
| 1356 | WindowSetattr(PyObject *self, char *name, PyObject *val) |
| 1357 | { |
| 1358 | WindowObject *this = (WindowObject *)(self); |
| 1359 | |
| 1360 | if (CheckWindow(this)) |
| 1361 | return -1; |
| 1362 | |
| 1363 | if (strcmp(name, "buffer") == 0) |
| 1364 | { |
| 1365 | PyErr_SetString(PyExc_TypeError, _("readonly attribute")); |
| 1366 | return -1; |
| 1367 | } |
| 1368 | else if (strcmp(name, "cursor") == 0) |
| 1369 | { |
| 1370 | long lnum; |
| 1371 | long col; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1372 | |
| 1373 | if (!PyArg_Parse(val, "(ll)", &lnum, &col)) |
| 1374 | return -1; |
| 1375 | |
| 1376 | if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count) |
| 1377 | { |
| 1378 | PyErr_SetVim(_("cursor position outside buffer")); |
| 1379 | return -1; |
| 1380 | } |
| 1381 | |
| 1382 | /* Check for keyboard interrupts */ |
| 1383 | if (VimErrorCheck()) |
| 1384 | return -1; |
| 1385 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1386 | this->win->w_cursor.lnum = lnum; |
| 1387 | this->win->w_cursor.col = col; |
| 1388 | #ifdef FEAT_VIRTUALEDIT |
| 1389 | this->win->w_cursor.coladd = 0; |
| 1390 | #endif |
Bram Moolenaar | 03a807a | 2011-07-07 15:08:58 +0200 | [diff] [blame] | 1391 | /* When column is out of range silently correct it. */ |
| 1392 | check_cursor_col_win(this->win); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1393 | |
Bram Moolenaar | 03a807a | 2011-07-07 15:08:58 +0200 | [diff] [blame] | 1394 | update_screen(VALID); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1395 | return 0; |
| 1396 | } |
| 1397 | else if (strcmp(name, "height") == 0) |
| 1398 | { |
| 1399 | int height; |
| 1400 | win_T *savewin; |
| 1401 | |
| 1402 | if (!PyArg_Parse(val, "i", &height)) |
| 1403 | return -1; |
| 1404 | |
| 1405 | #ifdef FEAT_GUI |
| 1406 | need_mouse_correct = TRUE; |
| 1407 | #endif |
| 1408 | savewin = curwin; |
| 1409 | curwin = this->win; |
| 1410 | win_setheight(height); |
| 1411 | curwin = savewin; |
| 1412 | |
| 1413 | /* Check for keyboard interrupts */ |
| 1414 | if (VimErrorCheck()) |
| 1415 | return -1; |
| 1416 | |
| 1417 | return 0; |
| 1418 | } |
| 1419 | #ifdef FEAT_VERTSPLIT |
| 1420 | else if (strcmp(name, "width") == 0) |
| 1421 | { |
| 1422 | int width; |
| 1423 | win_T *savewin; |
| 1424 | |
| 1425 | if (!PyArg_Parse(val, "i", &width)) |
| 1426 | return -1; |
| 1427 | |
| 1428 | #ifdef FEAT_GUI |
| 1429 | need_mouse_correct = TRUE; |
| 1430 | #endif |
| 1431 | savewin = curwin; |
| 1432 | curwin = this->win; |
| 1433 | win_setwidth(width); |
| 1434 | curwin = savewin; |
| 1435 | |
| 1436 | /* Check for keyboard interrupts */ |
| 1437 | if (VimErrorCheck()) |
| 1438 | return -1; |
| 1439 | |
| 1440 | return 0; |
| 1441 | } |
| 1442 | #endif |
| 1443 | else |
| 1444 | { |
| 1445 | PyErr_SetString(PyExc_AttributeError, name); |
| 1446 | return -1; |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | static PyObject * |
| 1451 | WindowRepr(PyObject *self) |
| 1452 | { |
| 1453 | static char repr[100]; |
| 1454 | WindowObject *this = (WindowObject *)(self); |
| 1455 | |
| 1456 | if (this->win == INVALID_WINDOW_VALUE) |
| 1457 | { |
| 1458 | vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self)); |
| 1459 | return PyString_FromString(repr); |
| 1460 | } |
| 1461 | else |
| 1462 | { |
| 1463 | int i = 0; |
| 1464 | win_T *w; |
| 1465 | |
| 1466 | for (w = firstwin; w != NULL && w != this->win; w = W_NEXT(w)) |
| 1467 | ++i; |
| 1468 | |
| 1469 | if (w == NULL) |
| 1470 | vim_snprintf(repr, 100, _("<window object (unknown) at %p>"), |
| 1471 | (self)); |
| 1472 | else |
| 1473 | vim_snprintf(repr, 100, _("<window %d>"), i); |
| 1474 | |
| 1475 | return PyString_FromString(repr); |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | /* |
| 1480 | * Window list object - Implementation |
| 1481 | */ |
| 1482 | static PyInt |
| 1483 | WinListLength(PyObject *self UNUSED) |
| 1484 | { |
| 1485 | win_T *w = firstwin; |
| 1486 | PyInt n = 0; |
| 1487 | |
| 1488 | while (w != NULL) |
| 1489 | { |
| 1490 | ++n; |
| 1491 | w = W_NEXT(w); |
| 1492 | } |
| 1493 | |
| 1494 | return n; |
| 1495 | } |
| 1496 | |
| 1497 | static PyObject * |
| 1498 | WinListItem(PyObject *self UNUSED, PyInt n) |
| 1499 | { |
| 1500 | win_T *w; |
| 1501 | |
| 1502 | for (w = firstwin; w != NULL; w = W_NEXT(w), --n) |
| 1503 | if (n == 0) |
| 1504 | return WindowNew(w); |
| 1505 | |
| 1506 | PyErr_SetString(PyExc_IndexError, _("no such window")); |
| 1507 | return NULL; |
| 1508 | } |
| 1509 | |
| 1510 | /* Convert a Python string into a Vim line. |
| 1511 | * |
| 1512 | * The result is in allocated memory. All internal nulls are replaced by |
| 1513 | * newline characters. It is an error for the string to contain newline |
| 1514 | * characters. |
| 1515 | * |
| 1516 | * On errors, the Python exception data is set, and NULL is returned. |
| 1517 | */ |
| 1518 | static char * |
| 1519 | StringToLine(PyObject *obj) |
| 1520 | { |
| 1521 | const char *str; |
| 1522 | char *save; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1523 | PyObject *bytes; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1524 | PyInt len; |
| 1525 | PyInt i; |
| 1526 | char *p; |
| 1527 | |
| 1528 | if (obj == NULL || !PyString_Check(obj)) |
| 1529 | { |
| 1530 | PyErr_BadArgument(); |
| 1531 | return NULL; |
| 1532 | } |
| 1533 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1534 | bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */ |
| 1535 | str = PyString_AsString(bytes); |
| 1536 | len = PyString_Size(bytes); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1537 | |
| 1538 | /* |
| 1539 | * Error checking: String must not contain newlines, as we |
| 1540 | * are replacing a single line, and we must replace it with |
| 1541 | * a single line. |
| 1542 | * A trailing newline is removed, so that append(f.readlines()) works. |
| 1543 | */ |
| 1544 | p = memchr(str, '\n', len); |
| 1545 | if (p != NULL) |
| 1546 | { |
| 1547 | if (p == str + len - 1) |
| 1548 | --len; |
| 1549 | else |
| 1550 | { |
| 1551 | PyErr_SetVim(_("string cannot contain newlines")); |
| 1552 | return NULL; |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | /* Create a copy of the string, with internal nulls replaced by |
| 1557 | * newline characters, as is the vim convention. |
| 1558 | */ |
| 1559 | save = (char *)alloc((unsigned)(len+1)); |
| 1560 | if (save == NULL) |
| 1561 | { |
| 1562 | PyErr_NoMemory(); |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | |
| 1566 | for (i = 0; i < len; ++i) |
| 1567 | { |
| 1568 | if (str[i] == '\0') |
| 1569 | save[i] = '\n'; |
| 1570 | else |
| 1571 | save[i] = str[i]; |
| 1572 | } |
| 1573 | |
| 1574 | save[i] = '\0'; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1575 | PyString_FreeBytes(bytes); /* Python 2 does nothing here */ |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1576 | |
| 1577 | return save; |
| 1578 | } |
| 1579 | |
| 1580 | /* Get a line from the specified buffer. The line number is |
| 1581 | * in Vim format (1-based). The line is returned as a Python |
| 1582 | * string object. |
| 1583 | */ |
| 1584 | static PyObject * |
| 1585 | GetBufferLine(buf_T *buf, PyInt n) |
| 1586 | { |
| 1587 | return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE)); |
| 1588 | } |
| 1589 | |
| 1590 | |
| 1591 | /* Get a list of lines from the specified buffer. The line numbers |
| 1592 | * are in Vim format (1-based). The range is from lo up to, but not |
| 1593 | * including, hi. The list is returned as a Python list of string objects. |
| 1594 | */ |
| 1595 | static PyObject * |
| 1596 | GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi) |
| 1597 | { |
| 1598 | PyInt i; |
| 1599 | PyInt n = hi - lo; |
| 1600 | PyObject *list = PyList_New(n); |
| 1601 | |
| 1602 | if (list == NULL) |
| 1603 | return NULL; |
| 1604 | |
| 1605 | for (i = 0; i < n; ++i) |
| 1606 | { |
| 1607 | PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE)); |
| 1608 | |
| 1609 | /* Error check - was the Python string creation OK? */ |
| 1610 | if (str == NULL) |
| 1611 | { |
| 1612 | Py_DECREF(list); |
| 1613 | return NULL; |
| 1614 | } |
| 1615 | |
| 1616 | /* Set the list item */ |
| 1617 | if (PyList_SetItem(list, i, str)) |
| 1618 | { |
| 1619 | Py_DECREF(str); |
| 1620 | Py_DECREF(list); |
| 1621 | return NULL; |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | /* The ownership of the Python list is passed to the caller (ie, |
| 1626 | * the caller should Py_DECREF() the object when it is finished |
| 1627 | * with it). |
| 1628 | */ |
| 1629 | |
| 1630 | return list; |
| 1631 | } |
| 1632 | |
| 1633 | /* |
| 1634 | * Check if deleting lines made the cursor position invalid. |
| 1635 | * Changed the lines from "lo" to "hi" and added "extra" lines (negative if |
| 1636 | * deleted). |
| 1637 | */ |
| 1638 | static void |
| 1639 | py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) |
| 1640 | { |
| 1641 | if (curwin->w_cursor.lnum >= lo) |
| 1642 | { |
| 1643 | /* Adjust the cursor position if it's in/after the changed |
| 1644 | * lines. */ |
| 1645 | if (curwin->w_cursor.lnum >= hi) |
| 1646 | { |
| 1647 | curwin->w_cursor.lnum += extra; |
| 1648 | check_cursor_col(); |
| 1649 | } |
| 1650 | else if (extra < 0) |
| 1651 | { |
| 1652 | curwin->w_cursor.lnum = lo; |
| 1653 | check_cursor(); |
| 1654 | } |
| 1655 | else |
| 1656 | check_cursor_col(); |
| 1657 | changed_cline_bef_curs(); |
| 1658 | } |
| 1659 | invalidate_botline(); |
| 1660 | } |
| 1661 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1662 | /* |
| 1663 | * Replace a line in the specified buffer. The line number is |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1664 | * in Vim format (1-based). The replacement line is given as |
| 1665 | * a Python string object. The object is checked for validity |
| 1666 | * and correct format. Errors are returned as a value of FAIL. |
| 1667 | * The return value is OK on success. |
| 1668 | * If OK is returned and len_change is not NULL, *len_change |
| 1669 | * is set to the change in the buffer length. |
| 1670 | */ |
| 1671 | static int |
| 1672 | SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change) |
| 1673 | { |
| 1674 | /* First of all, we check the thpe of the supplied Python object. |
| 1675 | * There are three cases: |
| 1676 | * 1. NULL, or None - this is a deletion. |
| 1677 | * 2. A string - this is a replacement. |
| 1678 | * 3. Anything else - this is an error. |
| 1679 | */ |
| 1680 | if (line == Py_None || line == NULL) |
| 1681 | { |
| 1682 | buf_T *savebuf = curbuf; |
| 1683 | |
| 1684 | PyErr_Clear(); |
| 1685 | curbuf = buf; |
| 1686 | |
| 1687 | if (u_savedel((linenr_T)n, 1L) == FAIL) |
| 1688 | PyErr_SetVim(_("cannot save undo information")); |
| 1689 | else if (ml_delete((linenr_T)n, FALSE) == FAIL) |
| 1690 | PyErr_SetVim(_("cannot delete line")); |
| 1691 | else |
| 1692 | { |
| 1693 | if (buf == curwin->w_buffer) |
| 1694 | py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); |
| 1695 | deleted_lines_mark((linenr_T)n, 1L); |
| 1696 | } |
| 1697 | |
| 1698 | curbuf = savebuf; |
| 1699 | |
| 1700 | if (PyErr_Occurred() || VimErrorCheck()) |
| 1701 | return FAIL; |
| 1702 | |
| 1703 | if (len_change) |
| 1704 | *len_change = -1; |
| 1705 | |
| 1706 | return OK; |
| 1707 | } |
| 1708 | else if (PyString_Check(line)) |
| 1709 | { |
| 1710 | char *save = StringToLine(line); |
| 1711 | buf_T *savebuf = curbuf; |
| 1712 | |
| 1713 | if (save == NULL) |
| 1714 | return FAIL; |
| 1715 | |
| 1716 | /* We do not need to free "save" if ml_replace() consumes it. */ |
| 1717 | PyErr_Clear(); |
| 1718 | curbuf = buf; |
| 1719 | |
| 1720 | if (u_savesub((linenr_T)n) == FAIL) |
| 1721 | { |
| 1722 | PyErr_SetVim(_("cannot save undo information")); |
| 1723 | vim_free(save); |
| 1724 | } |
| 1725 | else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL) |
| 1726 | { |
| 1727 | PyErr_SetVim(_("cannot replace line")); |
| 1728 | vim_free(save); |
| 1729 | } |
| 1730 | else |
| 1731 | changed_bytes((linenr_T)n, 0); |
| 1732 | |
| 1733 | curbuf = savebuf; |
| 1734 | |
| 1735 | /* Check that the cursor is not beyond the end of the line now. */ |
| 1736 | if (buf == curwin->w_buffer) |
| 1737 | check_cursor_col(); |
| 1738 | |
| 1739 | if (PyErr_Occurred() || VimErrorCheck()) |
| 1740 | return FAIL; |
| 1741 | |
| 1742 | if (len_change) |
| 1743 | *len_change = 0; |
| 1744 | |
| 1745 | return OK; |
| 1746 | } |
| 1747 | else |
| 1748 | { |
| 1749 | PyErr_BadArgument(); |
| 1750 | return FAIL; |
| 1751 | } |
| 1752 | } |
| 1753 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1754 | /* Replace a range of lines in the specified buffer. The line numbers are in |
| 1755 | * Vim format (1-based). The range is from lo up to, but not including, hi. |
| 1756 | * The replacement lines are given as a Python list of string objects. The |
| 1757 | * list is checked for validity and correct format. Errors are returned as a |
| 1758 | * value of FAIL. The return value is OK on success. |
| 1759 | * If OK is returned and len_change is not NULL, *len_change |
| 1760 | * is set to the change in the buffer length. |
| 1761 | */ |
| 1762 | static int |
| 1763 | SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change) |
| 1764 | { |
| 1765 | /* First of all, we check the thpe of the supplied Python object. |
| 1766 | * There are three cases: |
| 1767 | * 1. NULL, or None - this is a deletion. |
| 1768 | * 2. A list - this is a replacement. |
| 1769 | * 3. Anything else - this is an error. |
| 1770 | */ |
| 1771 | if (list == Py_None || list == NULL) |
| 1772 | { |
| 1773 | PyInt i; |
| 1774 | PyInt n = (int)(hi - lo); |
| 1775 | buf_T *savebuf = curbuf; |
| 1776 | |
| 1777 | PyErr_Clear(); |
| 1778 | curbuf = buf; |
| 1779 | |
| 1780 | if (u_savedel((linenr_T)lo, (long)n) == FAIL) |
| 1781 | PyErr_SetVim(_("cannot save undo information")); |
| 1782 | else |
| 1783 | { |
| 1784 | for (i = 0; i < n; ++i) |
| 1785 | { |
| 1786 | if (ml_delete((linenr_T)lo, FALSE) == FAIL) |
| 1787 | { |
| 1788 | PyErr_SetVim(_("cannot delete line")); |
| 1789 | break; |
| 1790 | } |
| 1791 | } |
| 1792 | if (buf == curwin->w_buffer) |
| 1793 | py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); |
| 1794 | deleted_lines_mark((linenr_T)lo, (long)i); |
| 1795 | } |
| 1796 | |
| 1797 | curbuf = savebuf; |
| 1798 | |
| 1799 | if (PyErr_Occurred() || VimErrorCheck()) |
| 1800 | return FAIL; |
| 1801 | |
| 1802 | if (len_change) |
| 1803 | *len_change = -n; |
| 1804 | |
| 1805 | return OK; |
| 1806 | } |
| 1807 | else if (PyList_Check(list)) |
| 1808 | { |
| 1809 | PyInt i; |
| 1810 | PyInt new_len = PyList_Size(list); |
| 1811 | PyInt old_len = hi - lo; |
| 1812 | PyInt extra = 0; /* lines added to text, can be negative */ |
| 1813 | char **array; |
| 1814 | buf_T *savebuf; |
| 1815 | |
| 1816 | if (new_len == 0) /* avoid allocating zero bytes */ |
| 1817 | array = NULL; |
| 1818 | else |
| 1819 | { |
| 1820 | array = (char **)alloc((unsigned)(new_len * sizeof(char *))); |
| 1821 | if (array == NULL) |
| 1822 | { |
| 1823 | PyErr_NoMemory(); |
| 1824 | return FAIL; |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | for (i = 0; i < new_len; ++i) |
| 1829 | { |
| 1830 | PyObject *line = PyList_GetItem(list, i); |
| 1831 | |
| 1832 | array[i] = StringToLine(line); |
| 1833 | if (array[i] == NULL) |
| 1834 | { |
| 1835 | while (i) |
| 1836 | vim_free(array[--i]); |
| 1837 | vim_free(array); |
| 1838 | return FAIL; |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | savebuf = curbuf; |
| 1843 | |
| 1844 | PyErr_Clear(); |
| 1845 | curbuf = buf; |
| 1846 | |
| 1847 | if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL) |
| 1848 | PyErr_SetVim(_("cannot save undo information")); |
| 1849 | |
| 1850 | /* If the size of the range is reducing (ie, new_len < old_len) we |
| 1851 | * need to delete some old_len. We do this at the start, by |
| 1852 | * repeatedly deleting line "lo". |
| 1853 | */ |
| 1854 | if (!PyErr_Occurred()) |
| 1855 | { |
| 1856 | for (i = 0; i < old_len - new_len; ++i) |
| 1857 | if (ml_delete((linenr_T)lo, FALSE) == FAIL) |
| 1858 | { |
| 1859 | PyErr_SetVim(_("cannot delete line")); |
| 1860 | break; |
| 1861 | } |
| 1862 | extra -= i; |
| 1863 | } |
| 1864 | |
| 1865 | /* For as long as possible, replace the existing old_len with the |
| 1866 | * new old_len. This is a more efficient operation, as it requires |
| 1867 | * less memory allocation and freeing. |
| 1868 | */ |
| 1869 | if (!PyErr_Occurred()) |
| 1870 | { |
| 1871 | for (i = 0; i < old_len && i < new_len; ++i) |
| 1872 | if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE) |
| 1873 | == FAIL) |
| 1874 | { |
| 1875 | PyErr_SetVim(_("cannot replace line")); |
| 1876 | break; |
| 1877 | } |
| 1878 | } |
| 1879 | else |
| 1880 | i = 0; |
| 1881 | |
| 1882 | /* Now we may need to insert the remaining new old_len. If we do, we |
| 1883 | * must free the strings as we finish with them (we can't pass the |
| 1884 | * responsibility to vim in this case). |
| 1885 | */ |
| 1886 | if (!PyErr_Occurred()) |
| 1887 | { |
| 1888 | while (i < new_len) |
| 1889 | { |
| 1890 | if (ml_append((linenr_T)(lo + i - 1), |
| 1891 | (char_u *)array[i], 0, FALSE) == FAIL) |
| 1892 | { |
| 1893 | PyErr_SetVim(_("cannot insert line")); |
| 1894 | break; |
| 1895 | } |
| 1896 | vim_free(array[i]); |
| 1897 | ++i; |
| 1898 | ++extra; |
| 1899 | } |
| 1900 | } |
| 1901 | |
| 1902 | /* Free any left-over old_len, as a result of an error */ |
| 1903 | while (i < new_len) |
| 1904 | { |
| 1905 | vim_free(array[i]); |
| 1906 | ++i; |
| 1907 | } |
| 1908 | |
| 1909 | /* Free the array of old_len. All of its contents have now |
| 1910 | * been dealt with (either freed, or the responsibility passed |
| 1911 | * to vim. |
| 1912 | */ |
| 1913 | vim_free(array); |
| 1914 | |
| 1915 | /* Adjust marks. Invalidate any which lie in the |
| 1916 | * changed range, and move any in the remainder of the buffer. |
| 1917 | */ |
| 1918 | mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), |
| 1919 | (long)MAXLNUM, (long)extra); |
| 1920 | changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); |
| 1921 | |
| 1922 | if (buf == curwin->w_buffer) |
| 1923 | py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); |
| 1924 | |
| 1925 | curbuf = savebuf; |
| 1926 | |
| 1927 | if (PyErr_Occurred() || VimErrorCheck()) |
| 1928 | return FAIL; |
| 1929 | |
| 1930 | if (len_change) |
| 1931 | *len_change = new_len - old_len; |
| 1932 | |
| 1933 | return OK; |
| 1934 | } |
| 1935 | else |
| 1936 | { |
| 1937 | PyErr_BadArgument(); |
| 1938 | return FAIL; |
| 1939 | } |
| 1940 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1941 | |
| 1942 | /* Insert a number of lines into the specified buffer after the specifed line. |
| 1943 | * The line number is in Vim format (1-based). The lines to be inserted are |
| 1944 | * given as a Python list of string objects or as a single string. The lines |
| 1945 | * to be added are checked for validity and correct format. Errors are |
| 1946 | * returned as a value of FAIL. The return value is OK on success. |
| 1947 | * If OK is returned and len_change is not NULL, *len_change |
| 1948 | * is set to the change in the buffer length. |
| 1949 | */ |
| 1950 | static int |
| 1951 | InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) |
| 1952 | { |
| 1953 | /* First of all, we check the type of the supplied Python object. |
| 1954 | * It must be a string or a list, or the call is in error. |
| 1955 | */ |
| 1956 | if (PyString_Check(lines)) |
| 1957 | { |
| 1958 | char *str = StringToLine(lines); |
| 1959 | buf_T *savebuf; |
| 1960 | |
| 1961 | if (str == NULL) |
| 1962 | return FAIL; |
| 1963 | |
| 1964 | savebuf = curbuf; |
| 1965 | |
| 1966 | PyErr_Clear(); |
| 1967 | curbuf = buf; |
| 1968 | |
| 1969 | if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL) |
| 1970 | PyErr_SetVim(_("cannot save undo information")); |
| 1971 | else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) |
| 1972 | PyErr_SetVim(_("cannot insert line")); |
| 1973 | else |
| 1974 | appended_lines_mark((linenr_T)n, 1L); |
| 1975 | |
| 1976 | vim_free(str); |
| 1977 | curbuf = savebuf; |
| 1978 | update_screen(VALID); |
| 1979 | |
| 1980 | if (PyErr_Occurred() || VimErrorCheck()) |
| 1981 | return FAIL; |
| 1982 | |
| 1983 | if (len_change) |
| 1984 | *len_change = 1; |
| 1985 | |
| 1986 | return OK; |
| 1987 | } |
| 1988 | else if (PyList_Check(lines)) |
| 1989 | { |
| 1990 | PyInt i; |
| 1991 | PyInt size = PyList_Size(lines); |
| 1992 | char **array; |
| 1993 | buf_T *savebuf; |
| 1994 | |
| 1995 | array = (char **)alloc((unsigned)(size * sizeof(char *))); |
| 1996 | if (array == NULL) |
| 1997 | { |
| 1998 | PyErr_NoMemory(); |
| 1999 | return FAIL; |
| 2000 | } |
| 2001 | |
| 2002 | for (i = 0; i < size; ++i) |
| 2003 | { |
| 2004 | PyObject *line = PyList_GetItem(lines, i); |
| 2005 | array[i] = StringToLine(line); |
| 2006 | |
| 2007 | if (array[i] == NULL) |
| 2008 | { |
| 2009 | while (i) |
| 2010 | vim_free(array[--i]); |
| 2011 | vim_free(array); |
| 2012 | return FAIL; |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | savebuf = curbuf; |
| 2017 | |
| 2018 | PyErr_Clear(); |
| 2019 | curbuf = buf; |
| 2020 | |
| 2021 | if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) |
| 2022 | PyErr_SetVim(_("cannot save undo information")); |
| 2023 | else |
| 2024 | { |
| 2025 | for (i = 0; i < size; ++i) |
| 2026 | { |
| 2027 | if (ml_append((linenr_T)(n + i), |
| 2028 | (char_u *)array[i], 0, FALSE) == FAIL) |
| 2029 | { |
| 2030 | PyErr_SetVim(_("cannot insert line")); |
| 2031 | |
| 2032 | /* Free the rest of the lines */ |
| 2033 | while (i < size) |
| 2034 | vim_free(array[i++]); |
| 2035 | |
| 2036 | break; |
| 2037 | } |
| 2038 | vim_free(array[i]); |
| 2039 | } |
| 2040 | if (i > 0) |
| 2041 | appended_lines_mark((linenr_T)n, (long)i); |
| 2042 | } |
| 2043 | |
| 2044 | /* Free the array of lines. All of its contents have now |
| 2045 | * been freed. |
| 2046 | */ |
| 2047 | vim_free(array); |
| 2048 | |
| 2049 | curbuf = savebuf; |
| 2050 | update_screen(VALID); |
| 2051 | |
| 2052 | if (PyErr_Occurred() || VimErrorCheck()) |
| 2053 | return FAIL; |
| 2054 | |
| 2055 | if (len_change) |
| 2056 | *len_change = size; |
| 2057 | |
| 2058 | return OK; |
| 2059 | } |
| 2060 | else |
| 2061 | { |
| 2062 | PyErr_BadArgument(); |
| 2063 | return FAIL; |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | /* |
| 2068 | * Common routines for buffers and line ranges |
| 2069 | * ------------------------------------------- |
| 2070 | */ |
| 2071 | |
| 2072 | static int |
| 2073 | CheckBuffer(BufferObject *this) |
| 2074 | { |
| 2075 | if (this->buf == INVALID_BUFFER_VALUE) |
| 2076 | { |
| 2077 | PyErr_SetVim(_("attempt to refer to deleted buffer")); |
| 2078 | return -1; |
| 2079 | } |
| 2080 | |
| 2081 | return 0; |
| 2082 | } |
| 2083 | |
| 2084 | static PyObject * |
| 2085 | RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end) |
| 2086 | { |
| 2087 | if (CheckBuffer(self)) |
| 2088 | return NULL; |
| 2089 | |
| 2090 | if (n < 0 || n > end - start) |
| 2091 | { |
| 2092 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 2093 | return NULL; |
| 2094 | } |
| 2095 | |
| 2096 | return GetBufferLine(self->buf, n+start); |
| 2097 | } |
| 2098 | |
| 2099 | static PyObject * |
| 2100 | RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end) |
| 2101 | { |
| 2102 | PyInt size; |
| 2103 | |
| 2104 | if (CheckBuffer(self)) |
| 2105 | return NULL; |
| 2106 | |
| 2107 | size = end - start + 1; |
| 2108 | |
| 2109 | if (lo < 0) |
| 2110 | lo = 0; |
| 2111 | else if (lo > size) |
| 2112 | lo = size; |
| 2113 | if (hi < 0) |
| 2114 | hi = 0; |
| 2115 | if (hi < lo) |
| 2116 | hi = lo; |
| 2117 | else if (hi > size) |
| 2118 | hi = size; |
| 2119 | |
| 2120 | return GetBufferLineList(self->buf, lo+start, hi+start); |
| 2121 | } |
| 2122 | |
| 2123 | static PyInt |
| 2124 | RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end) |
| 2125 | { |
| 2126 | PyInt len_change; |
| 2127 | |
| 2128 | if (CheckBuffer(self)) |
| 2129 | return -1; |
| 2130 | |
| 2131 | if (n < 0 || n > end - start) |
| 2132 | { |
| 2133 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 2134 | return -1; |
| 2135 | } |
| 2136 | |
| 2137 | if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) |
| 2138 | return -1; |
| 2139 | |
| 2140 | if (new_end) |
| 2141 | *new_end = end + len_change; |
| 2142 | |
| 2143 | return 0; |
| 2144 | } |
| 2145 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2146 | static PyInt |
| 2147 | RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end) |
| 2148 | { |
| 2149 | PyInt size; |
| 2150 | PyInt len_change; |
| 2151 | |
| 2152 | /* Self must be a valid buffer */ |
| 2153 | if (CheckBuffer(self)) |
| 2154 | return -1; |
| 2155 | |
| 2156 | /* Sort out the slice range */ |
| 2157 | size = end - start + 1; |
| 2158 | |
| 2159 | if (lo < 0) |
| 2160 | lo = 0; |
| 2161 | else if (lo > size) |
| 2162 | lo = size; |
| 2163 | if (hi < 0) |
| 2164 | hi = 0; |
| 2165 | if (hi < lo) |
| 2166 | hi = lo; |
| 2167 | else if (hi > size) |
| 2168 | hi = size; |
| 2169 | |
| 2170 | if (SetBufferLineList(self->buf, lo + start, hi + start, |
| 2171 | val, &len_change) == FAIL) |
| 2172 | return -1; |
| 2173 | |
| 2174 | if (new_end) |
| 2175 | *new_end = end + len_change; |
| 2176 | |
| 2177 | return 0; |
| 2178 | } |
| 2179 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2180 | |
| 2181 | static PyObject * |
| 2182 | RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end) |
| 2183 | { |
| 2184 | PyObject *lines; |
| 2185 | PyInt len_change; |
| 2186 | PyInt max; |
| 2187 | PyInt n; |
| 2188 | |
| 2189 | if (CheckBuffer(self)) |
| 2190 | return NULL; |
| 2191 | |
| 2192 | max = n = end - start + 1; |
| 2193 | |
| 2194 | if (!PyArg_ParseTuple(args, "O|n", &lines, &n)) |
| 2195 | return NULL; |
| 2196 | |
| 2197 | if (n < 0 || n > max) |
| 2198 | { |
| 2199 | PyErr_SetString(PyExc_ValueError, _("line number out of range")); |
| 2200 | return NULL; |
| 2201 | } |
| 2202 | |
| 2203 | if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL) |
| 2204 | return NULL; |
| 2205 | |
| 2206 | if (new_end) |
| 2207 | *new_end = end + len_change; |
| 2208 | |
| 2209 | Py_INCREF(Py_None); |
| 2210 | return Py_None; |
| 2211 | } |
| 2212 | |
| 2213 | |
| 2214 | /* Buffer object - Definitions |
| 2215 | */ |
| 2216 | |
| 2217 | typedef struct |
| 2218 | { |
| 2219 | PyObject_HEAD |
| 2220 | BufferObject *buf; |
| 2221 | PyInt start; |
| 2222 | PyInt end; |
| 2223 | } RangeObject; |
| 2224 | |
| 2225 | static PyObject * |
| 2226 | RangeNew(buf_T *buf, PyInt start, PyInt end) |
| 2227 | { |
| 2228 | BufferObject *bufr; |
| 2229 | RangeObject *self; |
| 2230 | self = PyObject_NEW(RangeObject, &RangeType); |
| 2231 | if (self == NULL) |
| 2232 | return NULL; |
| 2233 | |
| 2234 | bufr = (BufferObject *)BufferNew(buf); |
| 2235 | if (bufr == NULL) |
| 2236 | { |
| 2237 | Py_DECREF(self); |
| 2238 | return NULL; |
| 2239 | } |
| 2240 | Py_INCREF(bufr); |
| 2241 | |
| 2242 | self->buf = bufr; |
| 2243 | self->start = start; |
| 2244 | self->end = end; |
| 2245 | |
| 2246 | return (PyObject *)(self); |
| 2247 | } |
| 2248 | |
| 2249 | static PyObject * |
| 2250 | BufferAppend(PyObject *self, PyObject *args) |
| 2251 | { |
| 2252 | return RBAppend((BufferObject *)(self), args, 1, |
| 2253 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
| 2254 | NULL); |
| 2255 | } |
| 2256 | |
| 2257 | static PyObject * |
| 2258 | BufferMark(PyObject *self, PyObject *args) |
| 2259 | { |
| 2260 | pos_T *posp; |
| 2261 | char *pmark; |
| 2262 | char mark; |
| 2263 | buf_T *curbuf_save; |
| 2264 | |
| 2265 | if (CheckBuffer((BufferObject *)(self))) |
| 2266 | return NULL; |
| 2267 | |
| 2268 | if (!PyArg_ParseTuple(args, "s", &pmark)) |
| 2269 | return NULL; |
| 2270 | mark = *pmark; |
| 2271 | |
| 2272 | curbuf_save = curbuf; |
| 2273 | curbuf = ((BufferObject *)(self))->buf; |
| 2274 | posp = getmark(mark, FALSE); |
| 2275 | curbuf = curbuf_save; |
| 2276 | |
| 2277 | if (posp == NULL) |
| 2278 | { |
| 2279 | PyErr_SetVim(_("invalid mark name")); |
| 2280 | return NULL; |
| 2281 | } |
| 2282 | |
| 2283 | /* Ckeck for keyboard interrupt */ |
| 2284 | if (VimErrorCheck()) |
| 2285 | return NULL; |
| 2286 | |
| 2287 | if (posp->lnum <= 0) |
| 2288 | { |
| 2289 | /* Or raise an error? */ |
| 2290 | Py_INCREF(Py_None); |
| 2291 | return Py_None; |
| 2292 | } |
| 2293 | |
| 2294 | return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col)); |
| 2295 | } |
| 2296 | |
| 2297 | static PyObject * |
| 2298 | BufferRange(PyObject *self, PyObject *args) |
| 2299 | { |
| 2300 | PyInt start; |
| 2301 | PyInt end; |
| 2302 | |
| 2303 | if (CheckBuffer((BufferObject *)(self))) |
| 2304 | return NULL; |
| 2305 | |
| 2306 | if (!PyArg_ParseTuple(args, "nn", &start, &end)) |
| 2307 | return NULL; |
| 2308 | |
| 2309 | return RangeNew(((BufferObject *)(self))->buf, start, end); |
| 2310 | } |
| 2311 | |
| 2312 | static struct PyMethodDef BufferMethods[] = { |
| 2313 | /* name, function, calling, documentation */ |
| 2314 | {"append", BufferAppend, 1, "Append data to Vim buffer" }, |
| 2315 | {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" }, |
| 2316 | {"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] | 2317 | #if PY_VERSION_HEX >= 0x03000000 |
| 2318 | {"__dir__", BufferDir, 4, "List its attributes" }, |
| 2319 | #endif |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 2320 | { NULL, NULL, 0, NULL } |
| 2321 | }; |
| 2322 | |
| 2323 | static PyObject * |
| 2324 | RangeAppend(PyObject *self, PyObject *args) |
| 2325 | { |
| 2326 | return RBAppend(((RangeObject *)(self))->buf, args, |
| 2327 | ((RangeObject *)(self))->start, |
| 2328 | ((RangeObject *)(self))->end, |
| 2329 | &((RangeObject *)(self))->end); |
| 2330 | } |
| 2331 | |
| 2332 | static PyInt |
| 2333 | RangeLength(PyObject *self) |
| 2334 | { |
| 2335 | /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
| 2336 | if (CheckBuffer(((RangeObject *)(self))->buf)) |
| 2337 | return -1; /* ??? */ |
| 2338 | |
| 2339 | return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1); |
| 2340 | } |
| 2341 | |
| 2342 | static PyObject * |
| 2343 | RangeItem(PyObject *self, PyInt n) |
| 2344 | { |
| 2345 | return RBItem(((RangeObject *)(self))->buf, n, |
| 2346 | ((RangeObject *)(self))->start, |
| 2347 | ((RangeObject *)(self))->end); |
| 2348 | } |
| 2349 | |
| 2350 | static PyObject * |
| 2351 | RangeRepr(PyObject *self) |
| 2352 | { |
| 2353 | static char repr[100]; |
| 2354 | RangeObject *this = (RangeObject *)(self); |
| 2355 | |
| 2356 | if (this->buf->buf == INVALID_BUFFER_VALUE) |
| 2357 | { |
| 2358 | vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>", |
| 2359 | (self)); |
| 2360 | return PyString_FromString(repr); |
| 2361 | } |
| 2362 | else |
| 2363 | { |
| 2364 | char *name = (char *)this->buf->buf->b_fname; |
| 2365 | int len; |
| 2366 | |
| 2367 | if (name == NULL) |
| 2368 | name = ""; |
| 2369 | len = (int)strlen(name); |
| 2370 | |
| 2371 | if (len > 45) |
| 2372 | name = name + (45 - len); |
| 2373 | |
| 2374 | vim_snprintf(repr, 100, "<range %s%s (%d:%d)>", |
| 2375 | len > 45 ? "..." : "", name, |
| 2376 | this->start, this->end); |
| 2377 | |
| 2378 | return PyString_FromString(repr); |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | static PyObject * |
| 2383 | RangeSlice(PyObject *self, PyInt lo, PyInt hi) |
| 2384 | { |
| 2385 | return RBSlice(((RangeObject *)(self))->buf, lo, hi, |
| 2386 | ((RangeObject *)(self))->start, |
| 2387 | ((RangeObject *)(self))->end); |
| 2388 | } |
| 2389 | |
| 2390 | /* |
| 2391 | * Line range object - Definitions |
| 2392 | */ |
| 2393 | |
| 2394 | static struct PyMethodDef RangeMethods[] = { |
| 2395 | /* name, function, calling, documentation */ |
| 2396 | {"append", RangeAppend, 1, "Append data to the Vim range" }, |
| 2397 | { NULL, NULL, 0, NULL } |
| 2398 | }; |
| 2399 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2400 | static void |
| 2401 | set_ref_in_py(const int copyID) |
| 2402 | { |
| 2403 | pylinkedlist_T *cur; |
| 2404 | dict_T *dd; |
| 2405 | list_T *ll; |
| 2406 | |
| 2407 | if (lastdict != NULL) |
| 2408 | for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev) |
| 2409 | { |
| 2410 | dd = ((DictionaryObject *) (cur->pll_obj))->dict; |
| 2411 | if (dd->dv_copyID != copyID) |
| 2412 | { |
| 2413 | dd->dv_copyID = copyID; |
| 2414 | set_ref_in_ht(&dd->dv_hashtab, copyID); |
| 2415 | } |
| 2416 | } |
| 2417 | |
| 2418 | if (lastlist != NULL) |
| 2419 | for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev) |
| 2420 | { |
| 2421 | ll = ((ListObject *) (cur->pll_obj))->list; |
| 2422 | if (ll->lv_copyID != copyID) |
| 2423 | { |
| 2424 | ll->lv_copyID = copyID; |
| 2425 | set_ref_in_list(ll, copyID); |
| 2426 | } |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | static int |
| 2431 | set_string_copy(char_u *str, typval_T *tv) |
| 2432 | { |
| 2433 | tv->vval.v_string = vim_strsave(str); |
| 2434 | if (tv->vval.v_string == NULL) |
| 2435 | { |
| 2436 | PyErr_NoMemory(); |
| 2437 | return -1; |
| 2438 | } |
| 2439 | return 0; |
| 2440 | } |
| 2441 | |
| 2442 | #ifdef FEAT_EVAL |
| 2443 | typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *); |
| 2444 | |
| 2445 | static int |
| 2446 | convert_dl(PyObject *obj, typval_T *tv, |
| 2447 | pytotvfunc py_to_tv, PyObject *lookupDict) |
| 2448 | { |
| 2449 | PyObject *capsule; |
| 2450 | char hexBuf[sizeof(void *) * 2 + 3]; |
| 2451 | |
| 2452 | sprintf(hexBuf, "%p", obj); |
| 2453 | |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 2454 | # ifdef PY_USE_CAPSULE |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2455 | capsule = PyDict_GetItemString(lookupDict, hexBuf); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 2456 | # else |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 2457 | capsule = (PyObject *)PyDict_GetItemString(lookupDict, hexBuf); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 2458 | # endif |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 2459 | if (capsule == NULL) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2460 | { |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 2461 | # ifdef PY_USE_CAPSULE |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2462 | capsule = PyCapsule_New(tv, NULL, NULL); |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 2463 | # else |
| 2464 | capsule = PyCObject_FromVoidPtr(tv, NULL); |
| 2465 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2466 | PyDict_SetItemString(lookupDict, hexBuf, capsule); |
| 2467 | Py_DECREF(capsule); |
| 2468 | if (py_to_tv(obj, tv, lookupDict) == -1) |
| 2469 | { |
| 2470 | tv->v_type = VAR_UNKNOWN; |
| 2471 | return -1; |
| 2472 | } |
| 2473 | /* As we are not using copy_tv which increments reference count we must |
| 2474 | * do it ourself. */ |
| 2475 | switch(tv->v_type) |
| 2476 | { |
| 2477 | case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break; |
| 2478 | case VAR_LIST: ++tv->vval.v_list->lv_refcount; break; |
| 2479 | } |
| 2480 | } |
| 2481 | else |
| 2482 | { |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 2483 | typval_T *v; |
| 2484 | |
| 2485 | # ifdef PY_USE_CAPSULE |
| 2486 | v = PyCapsule_GetPointer(capsule, NULL); |
| 2487 | # else |
Bram Moolenaar | 221d687 | 2012-06-30 13:34:34 +0200 | [diff] [blame] | 2488 | v = PyCObject_AsVoidPtr(capsule); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 2489 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2490 | copy_tv(v, tv); |
| 2491 | } |
| 2492 | return 0; |
| 2493 | } |
| 2494 | |
| 2495 | static int |
| 2496 | ConvertFromPyObject(PyObject *obj, typval_T *tv) |
| 2497 | { |
| 2498 | PyObject *lookup_dict; |
| 2499 | int r; |
| 2500 | |
| 2501 | lookup_dict = PyDict_New(); |
| 2502 | r = _ConvertFromPyObject(obj, tv, lookup_dict); |
| 2503 | Py_DECREF(lookup_dict); |
| 2504 | return r; |
| 2505 | } |
| 2506 | |
| 2507 | static int |
| 2508 | _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookupDict) |
| 2509 | { |
| 2510 | if (obj->ob_type == &DictionaryType) |
| 2511 | { |
| 2512 | tv->v_type = VAR_DICT; |
| 2513 | tv->vval.v_dict = (((DictionaryObject *)(obj))->dict); |
| 2514 | ++tv->vval.v_dict->dv_refcount; |
| 2515 | } |
| 2516 | else if (obj->ob_type == &ListType) |
| 2517 | { |
| 2518 | tv->v_type = VAR_LIST; |
| 2519 | tv->vval.v_list = (((ListObject *)(obj))->list); |
| 2520 | ++tv->vval.v_list->lv_refcount; |
| 2521 | } |
| 2522 | else if (obj->ob_type == &FunctionType) |
| 2523 | { |
| 2524 | if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1) |
| 2525 | return -1; |
| 2526 | |
| 2527 | tv->v_type = VAR_FUNC; |
| 2528 | func_ref(tv->vval.v_string); |
| 2529 | } |
| 2530 | #if PY_MAJOR_VERSION >= 3 |
| 2531 | else if (PyBytes_Check(obj)) |
| 2532 | { |
| 2533 | char_u *result = (char_u *) PyBytes_AsString(obj); |
| 2534 | |
| 2535 | if (result == NULL) |
| 2536 | return -1; |
| 2537 | |
| 2538 | if (set_string_copy(result, tv) == -1) |
| 2539 | return -1; |
| 2540 | |
| 2541 | tv->v_type = VAR_STRING; |
| 2542 | } |
| 2543 | else if (PyUnicode_Check(obj)) |
| 2544 | { |
| 2545 | PyObject *bytes; |
| 2546 | char_u *result; |
| 2547 | |
| 2548 | bytes = PyString_AsBytes(obj); |
| 2549 | if (bytes == NULL) |
| 2550 | return -1; |
| 2551 | |
| 2552 | result = (char_u *) PyBytes_AsString(bytes); |
| 2553 | if (result == NULL) |
| 2554 | return -1; |
| 2555 | |
| 2556 | if (set_string_copy(result, tv) == -1) |
| 2557 | { |
| 2558 | Py_XDECREF(bytes); |
| 2559 | return -1; |
| 2560 | } |
| 2561 | Py_XDECREF(bytes); |
| 2562 | |
| 2563 | tv->v_type = VAR_STRING; |
| 2564 | } |
| 2565 | #else |
| 2566 | else if (PyUnicode_Check(obj)) |
| 2567 | { |
| 2568 | PyObject *bytes; |
| 2569 | char_u *result; |
| 2570 | |
| 2571 | bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); |
| 2572 | if (bytes == NULL) |
| 2573 | return -1; |
| 2574 | |
| 2575 | result=(char_u *) PyString_AsString(bytes); |
| 2576 | if (result == NULL) |
| 2577 | return -1; |
| 2578 | |
| 2579 | if (set_string_copy(result, tv) == -1) |
| 2580 | { |
| 2581 | Py_XDECREF(bytes); |
| 2582 | return -1; |
| 2583 | } |
| 2584 | Py_XDECREF(bytes); |
| 2585 | |
| 2586 | tv->v_type = VAR_STRING; |
| 2587 | } |
| 2588 | else if (PyString_Check(obj)) |
| 2589 | { |
| 2590 | char_u *result = (char_u *) PyString_AsString(obj); |
| 2591 | |
| 2592 | if (result == NULL) |
| 2593 | return -1; |
| 2594 | |
| 2595 | if (set_string_copy(result, tv) == -1) |
| 2596 | return -1; |
| 2597 | |
| 2598 | tv->v_type = VAR_STRING; |
| 2599 | } |
| 2600 | else if (PyInt_Check(obj)) |
| 2601 | { |
| 2602 | tv->v_type = VAR_NUMBER; |
| 2603 | tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj); |
| 2604 | } |
| 2605 | #endif |
| 2606 | else if (PyLong_Check(obj)) |
| 2607 | { |
| 2608 | tv->v_type = VAR_NUMBER; |
| 2609 | tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj); |
| 2610 | } |
| 2611 | else if (PyDict_Check(obj)) |
| 2612 | return convert_dl(obj, tv, pydict_to_tv, lookupDict); |
| 2613 | #ifdef FEAT_FLOAT |
| 2614 | else if (PyFloat_Check(obj)) |
| 2615 | { |
| 2616 | tv->v_type = VAR_FLOAT; |
| 2617 | tv->vval.v_float = (float_T) PyFloat_AsDouble(obj); |
| 2618 | } |
| 2619 | #endif |
| 2620 | else if (PyIter_Check(obj)) |
| 2621 | return convert_dl(obj, tv, pyiter_to_tv, lookupDict); |
| 2622 | else if (PySequence_Check(obj)) |
| 2623 | return convert_dl(obj, tv, pyseq_to_tv, lookupDict); |
| 2624 | else if (PyMapping_Check(obj)) |
| 2625 | return convert_dl(obj, tv, pymap_to_tv, lookupDict); |
| 2626 | else |
| 2627 | { |
| 2628 | PyErr_SetString(PyExc_TypeError, _("unable to convert to vim structure")); |
| 2629 | return -1; |
| 2630 | } |
| 2631 | return 0; |
| 2632 | } |
| 2633 | |
| 2634 | static PyObject * |
| 2635 | ConvertToPyObject(typval_T *tv) |
| 2636 | { |
| 2637 | if (tv == NULL) |
| 2638 | { |
| 2639 | PyErr_SetVim(_("NULL reference passed")); |
| 2640 | return NULL; |
| 2641 | } |
| 2642 | switch (tv->v_type) |
| 2643 | { |
| 2644 | case VAR_STRING: |
| 2645 | return PyBytes_FromString((char *) tv->vval.v_string); |
| 2646 | case VAR_NUMBER: |
| 2647 | return PyLong_FromLong((long) tv->vval.v_number); |
| 2648 | #ifdef FEAT_FLOAT |
| 2649 | case VAR_FLOAT: |
| 2650 | return PyFloat_FromDouble((double) tv->vval.v_float); |
| 2651 | #endif |
| 2652 | case VAR_LIST: |
| 2653 | return ListNew(tv->vval.v_list); |
| 2654 | case VAR_DICT: |
| 2655 | return DictionaryNew(tv->vval.v_dict); |
| 2656 | case VAR_FUNC: |
| 2657 | return FunctionNew(tv->vval.v_string); |
| 2658 | case VAR_UNKNOWN: |
| 2659 | Py_INCREF(Py_None); |
| 2660 | return Py_None; |
| 2661 | default: |
| 2662 | PyErr_SetVim(_("internal error: invalid value type")); |
| 2663 | return NULL; |
| 2664 | } |
| 2665 | } |
| 2666 | #endif |