Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 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 | |
| 15 | /* |
| 16 | * obtain a lock on the Vim data structures |
| 17 | */ |
| 18 | static void |
| 19 | Python_Lock_Vim(void) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | /* |
| 24 | * release a lock on the Vim data structures |
| 25 | */ |
| 26 | static void |
| 27 | Python_Release_Vim(void) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | /* Output object definition |
| 32 | */ |
| 33 | |
| 34 | static PyObject *OutputWrite(PyObject *, PyObject *); |
| 35 | static PyObject *OutputWritelines(PyObject *, PyObject *); |
| 36 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 37 | /* Function to write a line, points to either msg() or emsg(). */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 38 | typedef void (*writefn)(char_u *); |
| 39 | static void writer(writefn fn, char_u *str, PyInt n); |
| 40 | |
| 41 | typedef struct |
| 42 | { |
| 43 | PyObject_HEAD |
| 44 | long softspace; |
| 45 | long error; |
| 46 | } OutputObject; |
| 47 | |
| 48 | static struct PyMethodDef OutputMethods[] = { |
| 49 | /* name, function, calling, documentation */ |
| 50 | {"write", OutputWrite, 1, "" }, |
| 51 | {"writelines", OutputWritelines, 1, "" }, |
| 52 | { NULL, NULL, 0, NULL } |
| 53 | }; |
| 54 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 55 | #define PyErr_SetVim(str) PyErr_SetString(VimError, str) |
| 56 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 57 | /*************/ |
| 58 | |
| 59 | /* Output buffer management |
| 60 | */ |
| 61 | |
| 62 | static PyObject * |
| 63 | OutputWrite(PyObject *self, PyObject *args) |
| 64 | { |
| 65 | int len; |
| 66 | char *str; |
| 67 | int error = ((OutputObject *)(self))->error; |
| 68 | |
| 69 | if (!PyArg_ParseTuple(args, "s#", &str, &len)) |
| 70 | return NULL; |
| 71 | |
| 72 | Py_BEGIN_ALLOW_THREADS |
| 73 | Python_Lock_Vim(); |
| 74 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 75 | Python_Release_Vim(); |
| 76 | Py_END_ALLOW_THREADS |
| 77 | |
| 78 | Py_INCREF(Py_None); |
| 79 | return Py_None; |
| 80 | } |
| 81 | |
| 82 | static PyObject * |
| 83 | OutputWritelines(PyObject *self, PyObject *args) |
| 84 | { |
| 85 | PyInt n; |
| 86 | PyInt i; |
| 87 | PyObject *list; |
| 88 | int error = ((OutputObject *)(self))->error; |
| 89 | |
| 90 | if (!PyArg_ParseTuple(args, "O", &list)) |
| 91 | return NULL; |
| 92 | Py_INCREF(list); |
| 93 | |
| 94 | if (!PyList_Check(list)) { |
| 95 | PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
| 96 | Py_DECREF(list); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | n = PyList_Size(list); |
| 101 | |
| 102 | for (i = 0; i < n; ++i) |
| 103 | { |
| 104 | PyObject *line = PyList_GetItem(list, i); |
| 105 | char *str; |
| 106 | PyInt len; |
| 107 | |
| 108 | if (!PyArg_Parse(line, "s#", &str, &len)) { |
| 109 | PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); |
| 110 | Py_DECREF(list); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | Py_BEGIN_ALLOW_THREADS |
| 115 | Python_Lock_Vim(); |
| 116 | writer((writefn)(error ? emsg : msg), (char_u *)str, len); |
| 117 | Python_Release_Vim(); |
| 118 | Py_END_ALLOW_THREADS |
| 119 | } |
| 120 | |
| 121 | Py_DECREF(list); |
| 122 | Py_INCREF(Py_None); |
| 123 | return Py_None; |
| 124 | } |
| 125 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 126 | /* Buffer IO, we write one whole line at a time. */ |
| 127 | static garray_T io_ga = {0, 0, 1, 80, NULL}; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 128 | static writefn old_fn = NULL; |
| 129 | |
| 130 | static void |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 131 | PythonIO_Flush(void) |
| 132 | { |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 133 | if (old_fn != NULL && io_ga.ga_len > 0) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 134 | { |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 135 | ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL; |
| 136 | old_fn((char_u *)io_ga.ga_data); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 137 | } |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 138 | io_ga.ga_len = 0; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static void |
| 142 | writer(writefn fn, char_u *str, PyInt n) |
| 143 | { |
| 144 | char_u *ptr; |
| 145 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 146 | /* Flush when switching output function. */ |
| 147 | if (fn != old_fn) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 148 | PythonIO_Flush(); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 149 | old_fn = fn; |
| 150 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 151 | /* Write each NL separated line. Text after the last NL is kept for |
| 152 | * writing later. */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 153 | while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) |
| 154 | { |
| 155 | PyInt len = ptr - str; |
| 156 | |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 157 | if (ga_grow(&io_ga, (int)(len + 1)) == FAIL) |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 158 | break; |
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 | mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len); |
| 161 | ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL; |
| 162 | fn((char_u *)io_ga.ga_data); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 163 | str = ptr + 1; |
| 164 | n -= len + 1; |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 165 | io_ga.ga_len = 0; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 168 | /* Put the remaining text into io_ga for later printing. */ |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 169 | if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK) |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 170 | { |
| 171 | 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] | 172 | io_ga.ga_len += (int)n; |
Bram Moolenaar | 2eea198 | 2010-09-21 16:49:37 +0200 | [diff] [blame] | 173 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /***************/ |
| 177 | |
| 178 | static PyTypeObject OutputType; |
| 179 | |
| 180 | static OutputObject Output = |
| 181 | { |
| 182 | PyObject_HEAD_INIT(&OutputType) |
| 183 | 0, |
| 184 | 0 |
| 185 | }; |
| 186 | |
| 187 | static OutputObject Error = |
| 188 | { |
| 189 | PyObject_HEAD_INIT(&OutputType) |
| 190 | 0, |
| 191 | 1 |
| 192 | }; |
| 193 | |
| 194 | static int |
| 195 | PythonIO_Init_io(void) |
| 196 | { |
| 197 | PySys_SetObject("stdout", (PyObject *)(void *)&Output); |
| 198 | PySys_SetObject("stderr", (PyObject *)(void *)&Error); |
| 199 | |
| 200 | if (PyErr_Occurred()) |
| 201 | { |
| 202 | EMSG(_("E264: Python: Error initialising I/O objects")); |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | static PyObject *VimError; |
| 211 | |
| 212 | /* Check to see whether a Vim error has been reported, or a keyboard |
| 213 | * interrupt has been detected. |
| 214 | */ |
| 215 | static int |
| 216 | VimErrorCheck(void) |
| 217 | { |
| 218 | if (got_int) |
| 219 | { |
| 220 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 221 | return 1; |
| 222 | } |
| 223 | else if (did_emsg && !PyErr_Occurred()) |
| 224 | { |
| 225 | PyErr_SetNone(VimError); |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 231 | |
| 232 | /* Vim module - Implementation |
| 233 | */ |
| 234 | static PyObject * |
| 235 | VimCommand(PyObject *self UNUSED, PyObject *args) |
| 236 | { |
| 237 | char *cmd; |
| 238 | PyObject *result; |
| 239 | |
| 240 | if (!PyArg_ParseTuple(args, "s", &cmd)) |
| 241 | return NULL; |
| 242 | |
| 243 | PyErr_Clear(); |
| 244 | |
| 245 | Py_BEGIN_ALLOW_THREADS |
| 246 | Python_Lock_Vim(); |
| 247 | |
| 248 | do_cmdline_cmd((char_u *)cmd); |
| 249 | update_screen(VALID); |
| 250 | |
| 251 | Python_Release_Vim(); |
| 252 | Py_END_ALLOW_THREADS |
| 253 | |
| 254 | if (VimErrorCheck()) |
| 255 | result = NULL; |
| 256 | else |
| 257 | result = Py_None; |
| 258 | |
| 259 | Py_XINCREF(result); |
| 260 | return result; |
| 261 | } |
| 262 | |
| 263 | #ifdef FEAT_EVAL |
| 264 | /* |
| 265 | * Function to translate a typval_T into a PyObject; this will recursively |
| 266 | * translate lists/dictionaries into their Python equivalents. |
| 267 | * |
| 268 | * The depth parameter is to avoid infinite recursion, set it to 1 when |
| 269 | * you call VimToPython. |
| 270 | */ |
| 271 | static PyObject * |
| 272 | VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) |
| 273 | { |
| 274 | PyObject *result; |
| 275 | PyObject *newObj; |
| 276 | char ptrBuf[NUMBUFLEN]; |
| 277 | |
| 278 | /* Avoid infinite recursion */ |
| 279 | if (depth > 100) |
| 280 | { |
| 281 | Py_INCREF(Py_None); |
| 282 | result = Py_None; |
| 283 | return result; |
| 284 | } |
| 285 | |
| 286 | /* Check if we run into a recursive loop. The item must be in lookupDict |
| 287 | * then and we can use it again. */ |
| 288 | if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) |
| 289 | || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL)) |
| 290 | { |
| 291 | sprintf(ptrBuf, PRINTF_DECIMAL_LONG_U, |
| 292 | our_tv->v_type == VAR_LIST ? (long_u)our_tv->vval.v_list |
| 293 | : (long_u)our_tv->vval.v_dict); |
| 294 | result = PyDict_GetItemString(lookupDict, ptrBuf); |
| 295 | if (result != NULL) |
| 296 | { |
| 297 | Py_INCREF(result); |
| 298 | return result; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | if (our_tv->v_type == VAR_STRING) |
| 303 | { |
| 304 | result = Py_BuildValue("s", our_tv->vval.v_string); |
| 305 | } |
| 306 | else if (our_tv->v_type == VAR_NUMBER) |
| 307 | { |
| 308 | char buf[NUMBUFLEN]; |
| 309 | |
| 310 | /* For backwards compatibility numbers are stored as strings. */ |
| 311 | sprintf(buf, "%ld", (long)our_tv->vval.v_number); |
| 312 | result = Py_BuildValue("s", buf); |
| 313 | } |
| 314 | # ifdef FEAT_FLOAT |
| 315 | else if (our_tv->v_type == VAR_FLOAT) |
| 316 | { |
| 317 | char buf[NUMBUFLEN]; |
| 318 | |
| 319 | sprintf(buf, "%f", our_tv->vval.v_float); |
| 320 | result = Py_BuildValue("s", buf); |
| 321 | } |
| 322 | # endif |
| 323 | else if (our_tv->v_type == VAR_LIST) |
| 324 | { |
| 325 | list_T *list = our_tv->vval.v_list; |
| 326 | listitem_T *curr; |
| 327 | |
| 328 | result = PyList_New(0); |
| 329 | |
| 330 | if (list != NULL) |
| 331 | { |
| 332 | PyDict_SetItemString(lookupDict, ptrBuf, result); |
| 333 | |
| 334 | for (curr = list->lv_first; curr != NULL; curr = curr->li_next) |
| 335 | { |
| 336 | newObj = VimToPython(&curr->li_tv, depth + 1, lookupDict); |
| 337 | PyList_Append(result, newObj); |
| 338 | Py_DECREF(newObj); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | else if (our_tv->v_type == VAR_DICT) |
| 343 | { |
| 344 | result = PyDict_New(); |
| 345 | |
| 346 | if (our_tv->vval.v_dict != NULL) |
| 347 | { |
| 348 | hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab; |
| 349 | long_u todo = ht->ht_used; |
| 350 | hashitem_T *hi; |
| 351 | dictitem_T *di; |
| 352 | |
| 353 | PyDict_SetItemString(lookupDict, ptrBuf, result); |
| 354 | |
| 355 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 356 | { |
| 357 | if (!HASHITEM_EMPTY(hi)) |
| 358 | { |
| 359 | --todo; |
| 360 | |
| 361 | di = dict_lookup(hi); |
| 362 | newObj = VimToPython(&di->di_tv, depth + 1, lookupDict); |
| 363 | PyDict_SetItemString(result, (char *)hi->hi_key, newObj); |
| 364 | Py_DECREF(newObj); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | Py_INCREF(Py_None); |
| 372 | result = Py_None; |
| 373 | } |
| 374 | |
| 375 | return result; |
| 376 | } |
| 377 | #endif |
| 378 | |
| 379 | static PyObject * |
Bram Moolenaar | 0909215 | 2010-08-08 16:38:42 +0200 | [diff] [blame] | 380 | VimEval(PyObject *self UNUSED, PyObject *args UNUSED) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 381 | { |
| 382 | #ifdef FEAT_EVAL |
| 383 | char *expr; |
| 384 | typval_T *our_tv; |
| 385 | PyObject *result; |
| 386 | PyObject *lookup_dict; |
| 387 | |
| 388 | if (!PyArg_ParseTuple(args, "s", &expr)) |
| 389 | return NULL; |
| 390 | |
| 391 | Py_BEGIN_ALLOW_THREADS |
| 392 | Python_Lock_Vim(); |
| 393 | our_tv = eval_expr((char_u *)expr, NULL); |
| 394 | |
| 395 | Python_Release_Vim(); |
| 396 | Py_END_ALLOW_THREADS |
| 397 | |
| 398 | if (our_tv == NULL) |
| 399 | { |
| 400 | PyErr_SetVim(_("invalid expression")); |
| 401 | return NULL; |
| 402 | } |
| 403 | |
| 404 | /* Convert the Vim type into a Python type. Create a dictionary that's |
| 405 | * used to check for recursive loops. */ |
| 406 | lookup_dict = PyDict_New(); |
| 407 | result = VimToPython(our_tv, 1, lookup_dict); |
| 408 | Py_DECREF(lookup_dict); |
| 409 | |
| 410 | |
| 411 | Py_BEGIN_ALLOW_THREADS |
| 412 | Python_Lock_Vim(); |
| 413 | free_tv(our_tv); |
| 414 | Python_Release_Vim(); |
| 415 | Py_END_ALLOW_THREADS |
| 416 | |
| 417 | return result; |
| 418 | #else |
| 419 | PyErr_SetVim(_("expressions disabled at compile time")); |
| 420 | return NULL; |
| 421 | #endif |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Vim module - Definitions |
| 426 | */ |
| 427 | |
| 428 | static struct PyMethodDef VimMethods[] = { |
| 429 | /* name, function, calling, documentation */ |
| 430 | {"command", VimCommand, 1, "Execute a Vim ex-mode command" }, |
| 431 | {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" }, |
| 432 | { NULL, NULL, 0, NULL } |
| 433 | }; |
| 434 | |
| 435 | typedef struct |
| 436 | { |
| 437 | PyObject_HEAD |
| 438 | buf_T *buf; |
| 439 | } |
| 440 | BufferObject; |
| 441 | |
| 442 | #define INVALID_BUFFER_VALUE ((buf_T *)(-1)) |
| 443 | |
| 444 | /* |
| 445 | * Buffer list object - Implementation |
| 446 | */ |
| 447 | |
| 448 | static PyInt |
| 449 | BufListLength(PyObject *self UNUSED) |
| 450 | { |
| 451 | buf_T *b = firstbuf; |
| 452 | PyInt n = 0; |
| 453 | |
| 454 | while (b) |
| 455 | { |
| 456 | ++n; |
| 457 | b = b->b_next; |
| 458 | } |
| 459 | |
| 460 | return n; |
| 461 | } |
| 462 | |
| 463 | static PyObject * |
| 464 | BufListItem(PyObject *self UNUSED, PyInt n) |
| 465 | { |
| 466 | buf_T *b; |
| 467 | |
| 468 | for (b = firstbuf; b; b = b->b_next, --n) |
| 469 | { |
| 470 | if (n == 0) |
| 471 | return BufferNew(b); |
| 472 | } |
| 473 | |
| 474 | PyErr_SetString(PyExc_IndexError, _("no such buffer")); |
| 475 | return NULL; |
| 476 | } |
| 477 | |
| 478 | typedef struct |
| 479 | { |
| 480 | PyObject_HEAD |
| 481 | win_T *win; |
| 482 | } WindowObject; |
| 483 | |
| 484 | #define INVALID_WINDOW_VALUE ((win_T *)(-1)) |
| 485 | |
| 486 | static int |
| 487 | CheckWindow(WindowObject *this) |
| 488 | { |
| 489 | if (this->win == INVALID_WINDOW_VALUE) |
| 490 | { |
| 491 | PyErr_SetVim(_("attempt to refer to deleted window")); |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static int WindowSetattr(PyObject *, char *, PyObject *); |
| 499 | static PyObject *WindowRepr(PyObject *); |
| 500 | |
| 501 | static int |
| 502 | WindowSetattr(PyObject *self, char *name, PyObject *val) |
| 503 | { |
| 504 | WindowObject *this = (WindowObject *)(self); |
| 505 | |
| 506 | if (CheckWindow(this)) |
| 507 | return -1; |
| 508 | |
| 509 | if (strcmp(name, "buffer") == 0) |
| 510 | { |
| 511 | PyErr_SetString(PyExc_TypeError, _("readonly attribute")); |
| 512 | return -1; |
| 513 | } |
| 514 | else if (strcmp(name, "cursor") == 0) |
| 515 | { |
| 516 | long lnum; |
| 517 | long col; |
| 518 | long len; |
| 519 | |
| 520 | if (!PyArg_Parse(val, "(ll)", &lnum, &col)) |
| 521 | return -1; |
| 522 | |
| 523 | if (lnum <= 0 || lnum > this->win->w_buffer->b_ml.ml_line_count) |
| 524 | { |
| 525 | PyErr_SetVim(_("cursor position outside buffer")); |
| 526 | return -1; |
| 527 | } |
| 528 | |
| 529 | /* Check for keyboard interrupts */ |
| 530 | if (VimErrorCheck()) |
| 531 | return -1; |
| 532 | |
| 533 | /* When column is out of range silently correct it. */ |
| 534 | len = (long)STRLEN(ml_get_buf(this->win->w_buffer, lnum, FALSE)); |
| 535 | if (col > len) |
| 536 | col = len; |
| 537 | |
| 538 | this->win->w_cursor.lnum = lnum; |
| 539 | this->win->w_cursor.col = col; |
| 540 | #ifdef FEAT_VIRTUALEDIT |
| 541 | this->win->w_cursor.coladd = 0; |
| 542 | #endif |
| 543 | update_screen(VALID); |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | else if (strcmp(name, "height") == 0) |
| 548 | { |
| 549 | int height; |
| 550 | win_T *savewin; |
| 551 | |
| 552 | if (!PyArg_Parse(val, "i", &height)) |
| 553 | return -1; |
| 554 | |
| 555 | #ifdef FEAT_GUI |
| 556 | need_mouse_correct = TRUE; |
| 557 | #endif |
| 558 | savewin = curwin; |
| 559 | curwin = this->win; |
| 560 | win_setheight(height); |
| 561 | curwin = savewin; |
| 562 | |
| 563 | /* Check for keyboard interrupts */ |
| 564 | if (VimErrorCheck()) |
| 565 | return -1; |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | #ifdef FEAT_VERTSPLIT |
| 570 | else if (strcmp(name, "width") == 0) |
| 571 | { |
| 572 | int width; |
| 573 | win_T *savewin; |
| 574 | |
| 575 | if (!PyArg_Parse(val, "i", &width)) |
| 576 | return -1; |
| 577 | |
| 578 | #ifdef FEAT_GUI |
| 579 | need_mouse_correct = TRUE; |
| 580 | #endif |
| 581 | savewin = curwin; |
| 582 | curwin = this->win; |
| 583 | win_setwidth(width); |
| 584 | curwin = savewin; |
| 585 | |
| 586 | /* Check for keyboard interrupts */ |
| 587 | if (VimErrorCheck()) |
| 588 | return -1; |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | #endif |
| 593 | else |
| 594 | { |
| 595 | PyErr_SetString(PyExc_AttributeError, name); |
| 596 | return -1; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | static PyObject * |
| 601 | WindowRepr(PyObject *self) |
| 602 | { |
| 603 | static char repr[100]; |
| 604 | WindowObject *this = (WindowObject *)(self); |
| 605 | |
| 606 | if (this->win == INVALID_WINDOW_VALUE) |
| 607 | { |
| 608 | vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self)); |
| 609 | return PyString_FromString(repr); |
| 610 | } |
| 611 | else |
| 612 | { |
| 613 | int i = 0; |
| 614 | win_T *w; |
| 615 | |
| 616 | for (w = firstwin; w != NULL && w != this->win; w = W_NEXT(w)) |
| 617 | ++i; |
| 618 | |
| 619 | if (w == NULL) |
| 620 | vim_snprintf(repr, 100, _("<window object (unknown) at %p>"), |
| 621 | (self)); |
| 622 | else |
| 623 | vim_snprintf(repr, 100, _("<window %d>"), i); |
| 624 | |
| 625 | return PyString_FromString(repr); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Window list object - Implementation |
| 631 | */ |
| 632 | static PyInt |
| 633 | WinListLength(PyObject *self UNUSED) |
| 634 | { |
| 635 | win_T *w = firstwin; |
| 636 | PyInt n = 0; |
| 637 | |
| 638 | while (w != NULL) |
| 639 | { |
| 640 | ++n; |
| 641 | w = W_NEXT(w); |
| 642 | } |
| 643 | |
| 644 | return n; |
| 645 | } |
| 646 | |
| 647 | static PyObject * |
| 648 | WinListItem(PyObject *self UNUSED, PyInt n) |
| 649 | { |
| 650 | win_T *w; |
| 651 | |
| 652 | for (w = firstwin; w != NULL; w = W_NEXT(w), --n) |
| 653 | if (n == 0) |
| 654 | return WindowNew(w); |
| 655 | |
| 656 | PyErr_SetString(PyExc_IndexError, _("no such window")); |
| 657 | return NULL; |
| 658 | } |
| 659 | |
| 660 | /* Convert a Python string into a Vim line. |
| 661 | * |
| 662 | * The result is in allocated memory. All internal nulls are replaced by |
| 663 | * newline characters. It is an error for the string to contain newline |
| 664 | * characters. |
| 665 | * |
| 666 | * On errors, the Python exception data is set, and NULL is returned. |
| 667 | */ |
| 668 | static char * |
| 669 | StringToLine(PyObject *obj) |
| 670 | { |
| 671 | const char *str; |
| 672 | char *save; |
| 673 | PyInt len; |
| 674 | PyInt i; |
| 675 | char *p; |
| 676 | |
| 677 | if (obj == NULL || !PyString_Check(obj)) |
| 678 | { |
| 679 | PyErr_BadArgument(); |
| 680 | return NULL; |
| 681 | } |
| 682 | |
| 683 | str = PyString_AsString(obj); |
| 684 | len = PyString_Size(obj); |
| 685 | |
| 686 | /* |
| 687 | * Error checking: String must not contain newlines, as we |
| 688 | * are replacing a single line, and we must replace it with |
| 689 | * a single line. |
| 690 | * A trailing newline is removed, so that append(f.readlines()) works. |
| 691 | */ |
| 692 | p = memchr(str, '\n', len); |
| 693 | if (p != NULL) |
| 694 | { |
| 695 | if (p == str + len - 1) |
| 696 | --len; |
| 697 | else |
| 698 | { |
| 699 | PyErr_SetVim(_("string cannot contain newlines")); |
| 700 | return NULL; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /* Create a copy of the string, with internal nulls replaced by |
| 705 | * newline characters, as is the vim convention. |
| 706 | */ |
| 707 | save = (char *)alloc((unsigned)(len+1)); |
| 708 | if (save == NULL) |
| 709 | { |
| 710 | PyErr_NoMemory(); |
| 711 | return NULL; |
| 712 | } |
| 713 | |
| 714 | for (i = 0; i < len; ++i) |
| 715 | { |
| 716 | if (str[i] == '\0') |
| 717 | save[i] = '\n'; |
| 718 | else |
| 719 | save[i] = str[i]; |
| 720 | } |
| 721 | |
| 722 | save[i] = '\0'; |
| 723 | |
| 724 | return save; |
| 725 | } |
| 726 | |
| 727 | /* Get a line from the specified buffer. The line number is |
| 728 | * in Vim format (1-based). The line is returned as a Python |
| 729 | * string object. |
| 730 | */ |
| 731 | static PyObject * |
| 732 | GetBufferLine(buf_T *buf, PyInt n) |
| 733 | { |
| 734 | return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE)); |
| 735 | } |
| 736 | |
| 737 | |
| 738 | /* Get a list of lines from the specified buffer. The line numbers |
| 739 | * are in Vim format (1-based). The range is from lo up to, but not |
| 740 | * including, hi. The list is returned as a Python list of string objects. |
| 741 | */ |
| 742 | static PyObject * |
| 743 | GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi) |
| 744 | { |
| 745 | PyInt i; |
| 746 | PyInt n = hi - lo; |
| 747 | PyObject *list = PyList_New(n); |
| 748 | |
| 749 | if (list == NULL) |
| 750 | return NULL; |
| 751 | |
| 752 | for (i = 0; i < n; ++i) |
| 753 | { |
| 754 | PyObject *str = LineToString((char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE)); |
| 755 | |
| 756 | /* Error check - was the Python string creation OK? */ |
| 757 | if (str == NULL) |
| 758 | { |
| 759 | Py_DECREF(list); |
| 760 | return NULL; |
| 761 | } |
| 762 | |
| 763 | /* Set the list item */ |
| 764 | if (PyList_SetItem(list, i, str)) |
| 765 | { |
| 766 | Py_DECREF(str); |
| 767 | Py_DECREF(list); |
| 768 | return NULL; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | /* The ownership of the Python list is passed to the caller (ie, |
| 773 | * the caller should Py_DECREF() the object when it is finished |
| 774 | * with it). |
| 775 | */ |
| 776 | |
| 777 | return list; |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * Check if deleting lines made the cursor position invalid. |
| 782 | * Changed the lines from "lo" to "hi" and added "extra" lines (negative if |
| 783 | * deleted). |
| 784 | */ |
| 785 | static void |
| 786 | py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) |
| 787 | { |
| 788 | if (curwin->w_cursor.lnum >= lo) |
| 789 | { |
| 790 | /* Adjust the cursor position if it's in/after the changed |
| 791 | * lines. */ |
| 792 | if (curwin->w_cursor.lnum >= hi) |
| 793 | { |
| 794 | curwin->w_cursor.lnum += extra; |
| 795 | check_cursor_col(); |
| 796 | } |
| 797 | else if (extra < 0) |
| 798 | { |
| 799 | curwin->w_cursor.lnum = lo; |
| 800 | check_cursor(); |
| 801 | } |
| 802 | else |
| 803 | check_cursor_col(); |
| 804 | changed_cline_bef_curs(); |
| 805 | } |
| 806 | invalidate_botline(); |
| 807 | } |
| 808 | |
| 809 | /* Replace a line in the specified buffer. The line number is |
| 810 | * in Vim format (1-based). The replacement line is given as |
| 811 | * a Python string object. The object is checked for validity |
| 812 | * and correct format. Errors are returned as a value of FAIL. |
| 813 | * The return value is OK on success. |
| 814 | * If OK is returned and len_change is not NULL, *len_change |
| 815 | * is set to the change in the buffer length. |
| 816 | */ |
| 817 | static int |
| 818 | SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change) |
| 819 | { |
| 820 | /* First of all, we check the thpe of the supplied Python object. |
| 821 | * There are three cases: |
| 822 | * 1. NULL, or None - this is a deletion. |
| 823 | * 2. A string - this is a replacement. |
| 824 | * 3. Anything else - this is an error. |
| 825 | */ |
| 826 | if (line == Py_None || line == NULL) |
| 827 | { |
| 828 | buf_T *savebuf = curbuf; |
| 829 | |
| 830 | PyErr_Clear(); |
| 831 | curbuf = buf; |
| 832 | |
| 833 | if (u_savedel((linenr_T)n, 1L) == FAIL) |
| 834 | PyErr_SetVim(_("cannot save undo information")); |
| 835 | else if (ml_delete((linenr_T)n, FALSE) == FAIL) |
| 836 | PyErr_SetVim(_("cannot delete line")); |
| 837 | else |
| 838 | { |
| 839 | if (buf == curwin->w_buffer) |
| 840 | py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); |
| 841 | deleted_lines_mark((linenr_T)n, 1L); |
| 842 | } |
| 843 | |
| 844 | curbuf = savebuf; |
| 845 | |
| 846 | if (PyErr_Occurred() || VimErrorCheck()) |
| 847 | return FAIL; |
| 848 | |
| 849 | if (len_change) |
| 850 | *len_change = -1; |
| 851 | |
| 852 | return OK; |
| 853 | } |
| 854 | else if (PyString_Check(line)) |
| 855 | { |
| 856 | char *save = StringToLine(line); |
| 857 | buf_T *savebuf = curbuf; |
| 858 | |
| 859 | if (save == NULL) |
| 860 | return FAIL; |
| 861 | |
| 862 | /* We do not need to free "save" if ml_replace() consumes it. */ |
| 863 | PyErr_Clear(); |
| 864 | curbuf = buf; |
| 865 | |
| 866 | if (u_savesub((linenr_T)n) == FAIL) |
| 867 | { |
| 868 | PyErr_SetVim(_("cannot save undo information")); |
| 869 | vim_free(save); |
| 870 | } |
| 871 | else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL) |
| 872 | { |
| 873 | PyErr_SetVim(_("cannot replace line")); |
| 874 | vim_free(save); |
| 875 | } |
| 876 | else |
| 877 | changed_bytes((linenr_T)n, 0); |
| 878 | |
| 879 | curbuf = savebuf; |
| 880 | |
| 881 | /* Check that the cursor is not beyond the end of the line now. */ |
| 882 | if (buf == curwin->w_buffer) |
| 883 | check_cursor_col(); |
| 884 | |
| 885 | if (PyErr_Occurred() || VimErrorCheck()) |
| 886 | return FAIL; |
| 887 | |
| 888 | if (len_change) |
| 889 | *len_change = 0; |
| 890 | |
| 891 | return OK; |
| 892 | } |
| 893 | else |
| 894 | { |
| 895 | PyErr_BadArgument(); |
| 896 | return FAIL; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | |
| 901 | /* Insert a number of lines into the specified buffer after the specifed line. |
| 902 | * The line number is in Vim format (1-based). The lines to be inserted are |
| 903 | * given as a Python list of string objects or as a single string. The lines |
| 904 | * to be added are checked for validity and correct format. Errors are |
| 905 | * returned as a value of FAIL. The return value is OK on success. |
| 906 | * If OK is returned and len_change is not NULL, *len_change |
| 907 | * is set to the change in the buffer length. |
| 908 | */ |
| 909 | static int |
| 910 | InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) |
| 911 | { |
| 912 | /* First of all, we check the type of the supplied Python object. |
| 913 | * It must be a string or a list, or the call is in error. |
| 914 | */ |
| 915 | if (PyString_Check(lines)) |
| 916 | { |
| 917 | char *str = StringToLine(lines); |
| 918 | buf_T *savebuf; |
| 919 | |
| 920 | if (str == NULL) |
| 921 | return FAIL; |
| 922 | |
| 923 | savebuf = curbuf; |
| 924 | |
| 925 | PyErr_Clear(); |
| 926 | curbuf = buf; |
| 927 | |
| 928 | if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL) |
| 929 | PyErr_SetVim(_("cannot save undo information")); |
| 930 | else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) |
| 931 | PyErr_SetVim(_("cannot insert line")); |
| 932 | else |
| 933 | appended_lines_mark((linenr_T)n, 1L); |
| 934 | |
| 935 | vim_free(str); |
| 936 | curbuf = savebuf; |
| 937 | update_screen(VALID); |
| 938 | |
| 939 | if (PyErr_Occurred() || VimErrorCheck()) |
| 940 | return FAIL; |
| 941 | |
| 942 | if (len_change) |
| 943 | *len_change = 1; |
| 944 | |
| 945 | return OK; |
| 946 | } |
| 947 | else if (PyList_Check(lines)) |
| 948 | { |
| 949 | PyInt i; |
| 950 | PyInt size = PyList_Size(lines); |
| 951 | char **array; |
| 952 | buf_T *savebuf; |
| 953 | |
| 954 | array = (char **)alloc((unsigned)(size * sizeof(char *))); |
| 955 | if (array == NULL) |
| 956 | { |
| 957 | PyErr_NoMemory(); |
| 958 | return FAIL; |
| 959 | } |
| 960 | |
| 961 | for (i = 0; i < size; ++i) |
| 962 | { |
| 963 | PyObject *line = PyList_GetItem(lines, i); |
| 964 | array[i] = StringToLine(line); |
| 965 | |
| 966 | if (array[i] == NULL) |
| 967 | { |
| 968 | while (i) |
| 969 | vim_free(array[--i]); |
| 970 | vim_free(array); |
| 971 | return FAIL; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | savebuf = curbuf; |
| 976 | |
| 977 | PyErr_Clear(); |
| 978 | curbuf = buf; |
| 979 | |
| 980 | if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) |
| 981 | PyErr_SetVim(_("cannot save undo information")); |
| 982 | else |
| 983 | { |
| 984 | for (i = 0; i < size; ++i) |
| 985 | { |
| 986 | if (ml_append((linenr_T)(n + i), |
| 987 | (char_u *)array[i], 0, FALSE) == FAIL) |
| 988 | { |
| 989 | PyErr_SetVim(_("cannot insert line")); |
| 990 | |
| 991 | /* Free the rest of the lines */ |
| 992 | while (i < size) |
| 993 | vim_free(array[i++]); |
| 994 | |
| 995 | break; |
| 996 | } |
| 997 | vim_free(array[i]); |
| 998 | } |
| 999 | if (i > 0) |
| 1000 | appended_lines_mark((linenr_T)n, (long)i); |
| 1001 | } |
| 1002 | |
| 1003 | /* Free the array of lines. All of its contents have now |
| 1004 | * been freed. |
| 1005 | */ |
| 1006 | vim_free(array); |
| 1007 | |
| 1008 | curbuf = savebuf; |
| 1009 | update_screen(VALID); |
| 1010 | |
| 1011 | if (PyErr_Occurred() || VimErrorCheck()) |
| 1012 | return FAIL; |
| 1013 | |
| 1014 | if (len_change) |
| 1015 | *len_change = size; |
| 1016 | |
| 1017 | return OK; |
| 1018 | } |
| 1019 | else |
| 1020 | { |
| 1021 | PyErr_BadArgument(); |
| 1022 | return FAIL; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * Common routines for buffers and line ranges |
| 1028 | * ------------------------------------------- |
| 1029 | */ |
| 1030 | |
| 1031 | static int |
| 1032 | CheckBuffer(BufferObject *this) |
| 1033 | { |
| 1034 | if (this->buf == INVALID_BUFFER_VALUE) |
| 1035 | { |
| 1036 | PyErr_SetVim(_("attempt to refer to deleted buffer")); |
| 1037 | return -1; |
| 1038 | } |
| 1039 | |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | static PyObject * |
| 1044 | RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end) |
| 1045 | { |
| 1046 | if (CheckBuffer(self)) |
| 1047 | return NULL; |
| 1048 | |
| 1049 | if (n < 0 || n > end - start) |
| 1050 | { |
| 1051 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | |
| 1055 | return GetBufferLine(self->buf, n+start); |
| 1056 | } |
| 1057 | |
| 1058 | static PyObject * |
| 1059 | RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end) |
| 1060 | { |
| 1061 | PyInt size; |
| 1062 | |
| 1063 | if (CheckBuffer(self)) |
| 1064 | return NULL; |
| 1065 | |
| 1066 | size = end - start + 1; |
| 1067 | |
| 1068 | if (lo < 0) |
| 1069 | lo = 0; |
| 1070 | else if (lo > size) |
| 1071 | lo = size; |
| 1072 | if (hi < 0) |
| 1073 | hi = 0; |
| 1074 | if (hi < lo) |
| 1075 | hi = lo; |
| 1076 | else if (hi > size) |
| 1077 | hi = size; |
| 1078 | |
| 1079 | return GetBufferLineList(self->buf, lo+start, hi+start); |
| 1080 | } |
| 1081 | |
| 1082 | static PyInt |
| 1083 | RBAsItem(BufferObject *self, PyInt n, PyObject *val, PyInt start, PyInt end, PyInt *new_end) |
| 1084 | { |
| 1085 | PyInt len_change; |
| 1086 | |
| 1087 | if (CheckBuffer(self)) |
| 1088 | return -1; |
| 1089 | |
| 1090 | if (n < 0 || n > end - start) |
| 1091 | { |
| 1092 | PyErr_SetString(PyExc_IndexError, _("line number out of range")); |
| 1093 | return -1; |
| 1094 | } |
| 1095 | |
| 1096 | if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) |
| 1097 | return -1; |
| 1098 | |
| 1099 | if (new_end) |
| 1100 | *new_end = end + len_change; |
| 1101 | |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | static PyObject * |
| 1107 | RBAppend(BufferObject *self, PyObject *args, PyInt start, PyInt end, PyInt *new_end) |
| 1108 | { |
| 1109 | PyObject *lines; |
| 1110 | PyInt len_change; |
| 1111 | PyInt max; |
| 1112 | PyInt n; |
| 1113 | |
| 1114 | if (CheckBuffer(self)) |
| 1115 | return NULL; |
| 1116 | |
| 1117 | max = n = end - start + 1; |
| 1118 | |
| 1119 | if (!PyArg_ParseTuple(args, "O|n", &lines, &n)) |
| 1120 | return NULL; |
| 1121 | |
| 1122 | if (n < 0 || n > max) |
| 1123 | { |
| 1124 | PyErr_SetString(PyExc_ValueError, _("line number out of range")); |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | |
| 1128 | if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL) |
| 1129 | return NULL; |
| 1130 | |
| 1131 | if (new_end) |
| 1132 | *new_end = end + len_change; |
| 1133 | |
| 1134 | Py_INCREF(Py_None); |
| 1135 | return Py_None; |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | /* Buffer object - Definitions |
| 1140 | */ |
| 1141 | |
| 1142 | typedef struct |
| 1143 | { |
| 1144 | PyObject_HEAD |
| 1145 | BufferObject *buf; |
| 1146 | PyInt start; |
| 1147 | PyInt end; |
| 1148 | } RangeObject; |
| 1149 | |
| 1150 | static PyObject * |
| 1151 | RangeNew(buf_T *buf, PyInt start, PyInt end) |
| 1152 | { |
| 1153 | BufferObject *bufr; |
| 1154 | RangeObject *self; |
| 1155 | self = PyObject_NEW(RangeObject, &RangeType); |
| 1156 | if (self == NULL) |
| 1157 | return NULL; |
| 1158 | |
| 1159 | bufr = (BufferObject *)BufferNew(buf); |
| 1160 | if (bufr == NULL) |
| 1161 | { |
| 1162 | Py_DECREF(self); |
| 1163 | return NULL; |
| 1164 | } |
| 1165 | Py_INCREF(bufr); |
| 1166 | |
| 1167 | self->buf = bufr; |
| 1168 | self->start = start; |
| 1169 | self->end = end; |
| 1170 | |
| 1171 | return (PyObject *)(self); |
| 1172 | } |
| 1173 | |
| 1174 | static PyObject * |
| 1175 | BufferAppend(PyObject *self, PyObject *args) |
| 1176 | { |
| 1177 | return RBAppend((BufferObject *)(self), args, 1, |
| 1178 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
| 1179 | NULL); |
| 1180 | } |
| 1181 | |
| 1182 | static PyObject * |
| 1183 | BufferMark(PyObject *self, PyObject *args) |
| 1184 | { |
| 1185 | pos_T *posp; |
| 1186 | char *pmark; |
| 1187 | char mark; |
| 1188 | buf_T *curbuf_save; |
| 1189 | |
| 1190 | if (CheckBuffer((BufferObject *)(self))) |
| 1191 | return NULL; |
| 1192 | |
| 1193 | if (!PyArg_ParseTuple(args, "s", &pmark)) |
| 1194 | return NULL; |
| 1195 | mark = *pmark; |
| 1196 | |
| 1197 | curbuf_save = curbuf; |
| 1198 | curbuf = ((BufferObject *)(self))->buf; |
| 1199 | posp = getmark(mark, FALSE); |
| 1200 | curbuf = curbuf_save; |
| 1201 | |
| 1202 | if (posp == NULL) |
| 1203 | { |
| 1204 | PyErr_SetVim(_("invalid mark name")); |
| 1205 | return NULL; |
| 1206 | } |
| 1207 | |
| 1208 | /* Ckeck for keyboard interrupt */ |
| 1209 | if (VimErrorCheck()) |
| 1210 | return NULL; |
| 1211 | |
| 1212 | if (posp->lnum <= 0) |
| 1213 | { |
| 1214 | /* Or raise an error? */ |
| 1215 | Py_INCREF(Py_None); |
| 1216 | return Py_None; |
| 1217 | } |
| 1218 | |
| 1219 | return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col)); |
| 1220 | } |
| 1221 | |
| 1222 | static PyObject * |
| 1223 | BufferRange(PyObject *self, PyObject *args) |
| 1224 | { |
| 1225 | PyInt start; |
| 1226 | PyInt end; |
| 1227 | |
| 1228 | if (CheckBuffer((BufferObject *)(self))) |
| 1229 | return NULL; |
| 1230 | |
| 1231 | if (!PyArg_ParseTuple(args, "nn", &start, &end)) |
| 1232 | return NULL; |
| 1233 | |
| 1234 | return RangeNew(((BufferObject *)(self))->buf, start, end); |
| 1235 | } |
| 1236 | |
| 1237 | static struct PyMethodDef BufferMethods[] = { |
| 1238 | /* name, function, calling, documentation */ |
| 1239 | {"append", BufferAppend, 1, "Append data to Vim buffer" }, |
| 1240 | {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" }, |
| 1241 | {"range", BufferRange, 1, "Return a range object which represents the part of the given buffer between line numbers s and e" }, |
| 1242 | { NULL, NULL, 0, NULL } |
| 1243 | }; |
| 1244 | |
| 1245 | static PyObject * |
| 1246 | RangeAppend(PyObject *self, PyObject *args) |
| 1247 | { |
| 1248 | return RBAppend(((RangeObject *)(self))->buf, args, |
| 1249 | ((RangeObject *)(self))->start, |
| 1250 | ((RangeObject *)(self))->end, |
| 1251 | &((RangeObject *)(self))->end); |
| 1252 | } |
| 1253 | |
| 1254 | static PyInt |
| 1255 | RangeLength(PyObject *self) |
| 1256 | { |
| 1257 | /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
| 1258 | if (CheckBuffer(((RangeObject *)(self))->buf)) |
| 1259 | return -1; /* ??? */ |
| 1260 | |
| 1261 | return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1); |
| 1262 | } |
| 1263 | |
| 1264 | static PyObject * |
| 1265 | RangeItem(PyObject *self, PyInt n) |
| 1266 | { |
| 1267 | return RBItem(((RangeObject *)(self))->buf, n, |
| 1268 | ((RangeObject *)(self))->start, |
| 1269 | ((RangeObject *)(self))->end); |
| 1270 | } |
| 1271 | |
| 1272 | static PyObject * |
| 1273 | RangeRepr(PyObject *self) |
| 1274 | { |
| 1275 | static char repr[100]; |
| 1276 | RangeObject *this = (RangeObject *)(self); |
| 1277 | |
| 1278 | if (this->buf->buf == INVALID_BUFFER_VALUE) |
| 1279 | { |
| 1280 | vim_snprintf(repr, 100, "<range object (for deleted buffer) at %p>", |
| 1281 | (self)); |
| 1282 | return PyString_FromString(repr); |
| 1283 | } |
| 1284 | else |
| 1285 | { |
| 1286 | char *name = (char *)this->buf->buf->b_fname; |
| 1287 | int len; |
| 1288 | |
| 1289 | if (name == NULL) |
| 1290 | name = ""; |
| 1291 | len = (int)strlen(name); |
| 1292 | |
| 1293 | if (len > 45) |
| 1294 | name = name + (45 - len); |
| 1295 | |
| 1296 | vim_snprintf(repr, 100, "<range %s%s (%d:%d)>", |
| 1297 | len > 45 ? "..." : "", name, |
| 1298 | this->start, this->end); |
| 1299 | |
| 1300 | return PyString_FromString(repr); |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | static PyObject * |
| 1305 | RangeSlice(PyObject *self, PyInt lo, PyInt hi) |
| 1306 | { |
| 1307 | return RBSlice(((RangeObject *)(self))->buf, lo, hi, |
| 1308 | ((RangeObject *)(self))->start, |
| 1309 | ((RangeObject *)(self))->end); |
| 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * Line range object - Definitions |
| 1314 | */ |
| 1315 | |
| 1316 | static struct PyMethodDef RangeMethods[] = { |
| 1317 | /* name, function, calling, documentation */ |
| 1318 | {"append", RangeAppend, 1, "Append data to the Vim range" }, |
| 1319 | { NULL, NULL, 0, NULL } |
| 1320 | }; |
| 1321 | |