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