comparison src/if_py_both.h @ 18753:6e3dc2d630c2 v8.1.2366

patch 8.1.2366: using old C style comments Commit: https://github.com/vim/vim/commit/9bf703d46a79fbffeb829246ea5ce385bddc4166 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Nov 30 19:44:38 2019 +0100 patch 8.1.2366: using old C style comments Problem: Using old C style comments. Solution: Use // comments where appropriate.
author Bram Moolenaar <Bram@vim.org>
date Sat, 30 Nov 2019 19:45:03 +0100
parents 026034963159
children a3d9a5e14c1f
comparison
equal deleted inserted replaced
18752:9ec3c9cb4533 18753:6e3dc2d630c2
14 */ 14 */
15 15
16 static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim"; 16 static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
17 17
18 #if PY_VERSION_HEX < 0x02050000 18 #if PY_VERSION_HEX < 0x02050000
19 typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */ 19 typedef int Py_ssize_t; // Python 2.4 and earlier don't have this type.
20 #endif 20 #endif
21 21
22 #define ENC_OPT ((char *)p_enc) 22 #define ENC_OPT ((char *)p_enc)
23 #define DOPY_FUNC "_vim_pydo" 23 #define DOPY_FUNC "_vim_pydo"
24 24
301 #endif 301 #endif
302 302
303 return ret; 303 return ret;
304 } 304 }
305 305
306 /* Output buffer management 306 // Output buffer management
307 */ 307
308 308 // Function to write a line, points to either msg() or emsg().
309 /* Function to write a line, points to either msg() or emsg(). */
310 typedef void (*writefn)(char_u *); 309 typedef void (*writefn)(char_u *);
311 310
312 static PyTypeObject OutputType; 311 static PyTypeObject OutputType;
313 312
314 typedef struct 313 typedef struct
348 347
349 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name); 348 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
350 return -1; 349 return -1;
351 } 350 }
352 351
353 /* Buffer IO, we write one whole line at a time. */ 352 // Buffer IO, we write one whole line at a time.
354 static garray_T io_ga = {0, 0, 1, 80, NULL}; 353 static garray_T io_ga = {0, 0, 1, 80, NULL};
355 static writefn old_fn = NULL; 354 static writefn old_fn = NULL;
356 355
357 static void 356 static void
358 PythonIO_Flush(void) 357 PythonIO_Flush(void)
368 static void 367 static void
369 writer(writefn fn, char_u *str, PyInt n) 368 writer(writefn fn, char_u *str, PyInt n)
370 { 369 {
371 char_u *ptr; 370 char_u *ptr;
372 371
373 /* Flush when switching output function. */ 372 // Flush when switching output function.
374 if (fn != old_fn) 373 if (fn != old_fn)
375 PythonIO_Flush(); 374 PythonIO_Flush();
376 old_fn = fn; 375 old_fn = fn;
377 376
378 // Write each NL separated line. Text after the last NL is kept for 377 // Write each NL separated line. Text after the last NL is kept for
457 Py_DECREF(item); 456 Py_DECREF(item);
458 } 457 }
459 458
460 Py_DECREF(iterator); 459 Py_DECREF(iterator);
461 460
462 /* Iterator may have finished due to an exception */ 461 // Iterator may have finished due to an exception
463 if (PyErr_Occurred()) 462 if (PyErr_Occurred())
464 return NULL; 463 return NULL;
465 464
466 Py_INCREF(Py_None); 465 Py_INCREF(Py_None);
467 return Py_None; 466 return Py_None;
468 } 467 }
469 468
470 static PyObject * 469 static PyObject *
471 AlwaysNone(PyObject *self UNUSED) 470 AlwaysNone(PyObject *self UNUSED)
472 { 471 {
473 /* do nothing */ 472 // do nothing
474 Py_INCREF(Py_None); 473 Py_INCREF(Py_None);
475 return Py_None; 474 return Py_None;
476 } 475 }
477 476
478 static PyObject * 477 static PyObject *
479 AlwaysFalse(PyObject *self UNUSED) 478 AlwaysFalse(PyObject *self UNUSED)
480 { 479 {
481 /* do nothing */ 480 // do nothing
482 PyObject *ret = Py_False; 481 PyObject *ret = Py_False;
483 Py_INCREF(ret); 482 Py_INCREF(ret);
484 return ret; 483 return ret;
485 } 484 }
486 485
487 static PyObject * 486 static PyObject *
488 AlwaysTrue(PyObject *self UNUSED) 487 AlwaysTrue(PyObject *self UNUSED)
489 { 488 {
490 /* do nothing */ 489 // do nothing
491 PyObject *ret = Py_True; 490 PyObject *ret = Py_True;
492 Py_INCREF(ret); 491 Py_INCREF(ret);
493 return ret; 492 return ret;
494 } 493 }
495 494
496 /***************/ 495 /***************/
497 496
498 static struct PyMethodDef OutputMethods[] = { 497 static struct PyMethodDef OutputMethods[] = {
499 /* name, function, calling, doc */ 498 // name, function, calling, doc
500 {"write", (PyCFunction)OutputWrite, METH_O, ""}, 499 {"write", (PyCFunction)OutputWrite, METH_O, ""},
501 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""}, 500 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
502 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""}, 501 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
503 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""}, 502 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
504 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""}, 503 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
594 Py_INCREF(module); 593 Py_INCREF(module);
595 return module; 594 return module;
596 } 595 }
597 596
598 static struct PyMethodDef LoaderMethods[] = { 597 static struct PyMethodDef LoaderMethods[] = {
599 /* name, function, calling, doc */ 598 // name, function, calling, doc
600 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""}, 599 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
601 { NULL, NULL, 0, NULL} 600 { NULL, NULL, 0, NULL}
602 }; 601 };
603 #endif 602 #endif
604 603
605 /* Check to see whether a Vim error has been reported, or a keyboard 604 /*
605 * Check to see whether a Vim error has been reported, or a keyboard
606 * interrupt has been detected. 606 * interrupt has been detected.
607 */ 607 */
608
609 static void 608 static void
610 VimTryStart(void) 609 VimTryStart(void)
611 { 610 {
612 ++trylevel; 611 ++trylevel;
613 } 612 }
614 613
615 static int 614 static int
616 VimTryEnd(void) 615 VimTryEnd(void)
617 { 616 {
618 --trylevel; 617 --trylevel;
619 /* Without this it stops processing all subsequent Vim script commands and 618 // Without this it stops processing all subsequent Vim script commands and
620 * generates strange error messages if I e.g. try calling Test() in a cycle 619 // generates strange error messages if I e.g. try calling Test() in a cycle
621 */
622 did_emsg = FALSE; 620 did_emsg = FALSE;
623 /* Keyboard interrupt should be preferred over anything else */ 621 // Keyboard interrupt should be preferred over anything else
624 if (got_int) 622 if (got_int)
625 { 623 {
626 if (did_throw) 624 if (did_throw)
627 discard_current_exception(); 625 discard_current_exception();
628 got_int = FALSE; 626 got_int = FALSE;
651 649
652 return -1; 650 return -1;
653 } 651 }
654 else if (!did_throw) 652 else if (!did_throw)
655 return (PyErr_Occurred() ? -1 : 0); 653 return (PyErr_Occurred() ? -1 : 0);
656 /* Python exception is preferred over vim one; unlikely to occur though */ 654 // Python exception is preferred over vim one; unlikely to occur though
657 else if (PyErr_Occurred()) 655 else if (PyErr_Occurred())
658 { 656 {
659 discard_current_exception(); 657 discard_current_exception();
660 return -1; 658 return -1;
661 } 659 }
662 /* Finally transform Vim script exception to python one */ 660 // Finally transform Vim script exception to python one
663 else 661 else
664 { 662 {
665 PyErr_SetVim((char *)current_exception->value); 663 PyErr_SetVim((char *)current_exception->value);
666 discard_current_exception(); 664 discard_current_exception();
667 return -1; 665 return -1;
677 return 1; 675 return 1;
678 } 676 }
679 return 0; 677 return 0;
680 } 678 }
681 679
682 /* Vim module - Implementation 680 // Vim module - Implementation
683 */
684 681
685 static PyObject * 682 static PyObject *
686 VimCommand(PyObject *self UNUSED, PyObject *string) 683 VimCommand(PyObject *self UNUSED, PyObject *string)
687 { 684 {
688 char_u *cmd; 685 char_u *cmd;
724 { 721 {
725 PyObject *ret; 722 PyObject *ret;
726 PyObject *newObj; 723 PyObject *newObj;
727 char ptrBuf[sizeof(void *) * 2 + 3]; 724 char ptrBuf[sizeof(void *) * 2 + 3];
728 725
729 /* Avoid infinite recursion */ 726 // Avoid infinite recursion
730 if (depth > 100) 727 if (depth > 100)
731 { 728 {
732 Py_INCREF(Py_None); 729 Py_INCREF(Py_None);
733 ret = Py_None; 730 ret = Py_None;
734 return ret; 731 return ret;
735 } 732 }
736 733
737 /* Check if we run into a recursive loop. The item must be in lookup_dict 734 // Check if we run into a recursive loop. The item must be in lookup_dict
738 * then and we can use it again. */ 735 // then and we can use it again.
739 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL) 736 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
740 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL)) 737 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
741 { 738 {
742 sprintf(ptrBuf, "%p", 739 sprintf(ptrBuf, "%p",
743 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list 740 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
755 ? "" : (char *)our_tv->vval.v_string); 752 ? "" : (char *)our_tv->vval.v_string);
756 else if (our_tv->v_type == VAR_NUMBER) 753 else if (our_tv->v_type == VAR_NUMBER)
757 { 754 {
758 char buf[NUMBUFLEN]; 755 char buf[NUMBUFLEN];
759 756
760 /* For backwards compatibility numbers are stored as strings. */ 757 // For backwards compatibility numbers are stored as strings.
761 sprintf(buf, "%ld", (long)our_tv->vval.v_number); 758 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
762 ret = PyString_FromString((char *)buf); 759 ret = PyString_FromString((char *)buf);
763 } 760 }
764 #ifdef FEAT_FLOAT 761 #ifdef FEAT_FLOAT
765 else if (our_tv->v_type == VAR_FLOAT) 762 else if (our_tv->v_type == VAR_FLOAT)
910 { 907 {
911 PyErr_SET_VIM(N_("invalid expression")); 908 PyErr_SET_VIM(N_("invalid expression"));
912 return NULL; 909 return NULL;
913 } 910 }
914 911
915 /* Convert the Vim type into a Python type. Create a dictionary that's 912 // Convert the Vim type into a Python type. Create a dictionary that's
916 * used to check for recursive loops. */ 913 // used to check for recursive loops.
917 if (!(lookup_dict = PyDict_New())) 914 if (!(lookup_dict = PyDict_New()))
918 ret = NULL; 915 ret = NULL;
919 else 916 else
920 { 917 {
921 ret = VimToPython(our_tv, 1, lookup_dict); 918 ret = VimToPython(our_tv, 1, lookup_dict);
1132 #else 1129 #else
1133 # define PY_MAIN_DIR_STRING "python3" 1130 # define PY_MAIN_DIR_STRING "python3"
1134 #endif 1131 #endif
1135 #define PY_ALTERNATE_DIR_STRING "pythonx" 1132 #define PY_ALTERNATE_DIR_STRING "pythonx"
1136 1133
1137 #define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */ 1134 #define PYTHONX_STRING_LENGTH 7 // STRLEN("pythonx")
1138 if (!(pathbuf = PyMem_New(char, 1135 if (!(pathbuf = PyMem_New(char,
1139 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1))) 1136 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1140 { 1137 {
1141 PyErr_NoMemory(); 1138 PyErr_NoMemory();
1142 *data = NULL; 1139 *data = NULL;
1401 /* 1398 /*
1402 * Vim module - Definitions 1399 * Vim module - Definitions
1403 */ 1400 */
1404 1401
1405 static struct PyMethodDef VimMethods[] = { 1402 static struct PyMethodDef VimMethods[] = {
1406 /* name, function, calling, documentation */ 1403 // name, function, calling, documentation
1407 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" }, 1404 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
1408 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" }, 1405 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
1409 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"}, 1406 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1410 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"}, 1407 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
1411 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"}, 1408 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1429 typedef PyObject *(*nextfun)(void **); 1426 typedef PyObject *(*nextfun)(void **);
1430 typedef void (*destructorfun)(void *); 1427 typedef void (*destructorfun)(void *);
1431 typedef int (*traversefun)(void *, visitproc, void *); 1428 typedef int (*traversefun)(void *, visitproc, void *);
1432 typedef int (*clearfun)(void **); 1429 typedef int (*clearfun)(void **);
1433 1430
1434 /* Main purpose of this object is removing the need for do python 1431 // Main purpose of this object is removing the need for do python
1435 * initialization (i.e. PyType_Ready and setting type attributes) for a big 1432 // initialization (i.e. PyType_Ready and setting type attributes) for a big
1436 * bunch of objects. */ 1433 // bunch of objects.
1437
1438 typedef struct 1434 typedef struct
1439 { 1435 {
1440 PyObject_HEAD 1436 PyObject_HEAD
1441 void *cur; 1437 void *cur;
1442 nextfun next; 1438 nextfun next;
1476 return self->traverse(self->cur, visit, arg); 1472 return self->traverse(self->cur, visit, arg);
1477 else 1473 else
1478 return 0; 1474 return 0;
1479 } 1475 }
1480 1476
1481 /* Mac OSX defines clear() somewhere. */ 1477 // Mac OSX defines clear() somewhere.
1482 #ifdef clear 1478 #ifdef clear
1483 # undef clear 1479 # undef clear
1484 #endif 1480 #endif
1485 1481
1486 static int 1482 static int
1686 } 1682 }
1687 1683
1688 #define DICT_FLAG_HAS_DEFAULT 0x01 1684 #define DICT_FLAG_HAS_DEFAULT 0x01
1689 #define DICT_FLAG_POP 0x02 1685 #define DICT_FLAG_POP 0x02
1690 #define DICT_FLAG_NONE_DEFAULT 0x04 1686 #define DICT_FLAG_NONE_DEFAULT 0x04
1691 #define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */ 1687 #define DICT_FLAG_RETURN_BOOL 0x08 // Incompatible with DICT_FLAG_POP
1692 #define DICT_FLAG_RETURN_PAIR 0x10 1688 #define DICT_FLAG_RETURN_PAIR 0x10
1693 1689
1694 static PyObject * 1690 static PyObject *
1695 _DictionaryItem(DictionaryObject *self, PyObject *args, int flags) 1691 _DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
1696 { 1692 {
2139 } 2135 }
2140 } 2136 }
2141 2137
2142 Py_DECREF(iterator); 2138 Py_DECREF(iterator);
2143 2139
2144 /* Iterator may have finished due to an exception */ 2140 // Iterator may have finished due to an exception
2145 if (PyErr_Occurred()) 2141 if (PyErr_Occurred())
2146 return NULL; 2142 return NULL;
2147 } 2143 }
2148 } 2144 }
2149 Py_INCREF(Py_None); 2145 Py_INCREF(Py_None);
2203 { 2199 {
2204 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL); 2200 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2205 } 2201 }
2206 2202
2207 static PySequenceMethods DictionaryAsSeq = { 2203 static PySequenceMethods DictionaryAsSeq = {
2208 0, /* sq_length */ 2204 0, // sq_length
2209 0, /* sq_concat */ 2205 0, // sq_concat
2210 0, /* sq_repeat */ 2206 0, // sq_repeat
2211 0, /* sq_item */ 2207 0, // sq_item
2212 0, /* sq_slice */ 2208 0, // sq_slice
2213 0, /* sq_ass_item */ 2209 0, // sq_ass_item
2214 0, /* sq_ass_slice */ 2210 0, // sq_ass_slice
2215 (objobjproc) DictionaryContains, /* sq_contains */ 2211 (objobjproc) DictionaryContains, // sq_contains
2216 0, /* sq_inplace_concat */ 2212 0, // sq_inplace_concat
2217 0, /* sq_inplace_repeat */ 2213 0, // sq_inplace_repeat
2218 }; 2214 };
2219 2215
2220 static PyMappingMethods DictionaryAsMapping = { 2216 static PyMappingMethods DictionaryAsMapping = {
2221 (lenfunc) DictionaryLength, 2217 (lenfunc) DictionaryLength,
2222 (binaryfunc) DictionaryItem, 2218 (binaryfunc) DictionaryItem,
2313 list_append(l, li); 2309 list_append(l, li);
2314 } 2310 }
2315 2311
2316 Py_DECREF(iterator); 2312 Py_DECREF(iterator);
2317 2313
2318 /* Iterator may have finished due to an exception */ 2314 // Iterator may have finished due to an exception
2319 if (PyErr_Occurred()) 2315 if (PyErr_Occurred())
2320 return -1; 2316 return -1;
2321 2317
2322 return 0; 2318 return 0;
2323 } 2319 }
2390 return NULL; 2386 return NULL;
2391 } 2387 }
2392 li = list_find(self->list, (long) index); 2388 li = list_find(self->list, (long) index);
2393 if (li == NULL) 2389 if (li == NULL)
2394 { 2390 {
2395 /* No more suitable format specifications in python-2.3 */ 2391 // No more suitable format specifications in python-2.3
2396 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"), 2392 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
2397 (int) index); 2393 (int) index);
2398 return NULL; 2394 return NULL;
2399 } 2395 }
2400 return ConvertToPyObject(&li->li_tv); 2396 return ConvertToPyObject(&li->li_tv);
2517 return -1; 2513 return -1;
2518 } 2514 }
2519 2515
2520 if (step != 1 && slicelen == 0) 2516 if (step != 1 && slicelen == 0)
2521 { 2517 {
2522 /* Nothing to do. Only error out if obj has some items. */ 2518 // Nothing to do. Only error out if obj has some items.
2523 int ret = 0; 2519 int ret = 0;
2524 2520
2525 if (obj == NULL) 2521 if (obj == NULL)
2526 return 0; 2522 return 0;
2527 2523
2539 Py_DECREF(iterator); 2535 Py_DECREF(iterator);
2540 return ret; 2536 return ret;
2541 } 2537 }
2542 2538
2543 if (obj != NULL) 2539 if (obj != NULL)
2544 /* XXX May allocate zero bytes. */ 2540 // XXX May allocate zero bytes.
2545 if (!(lis = PyMem_New(listitem_T *, slicelen * 2))) 2541 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2546 { 2542 {
2547 PyErr_NoMemory(); 2543 PyErr_NoMemory();
2548 return -1; 2544 return -1;
2549 } 2545 }
2879 return -1; 2875 return -1;
2880 } 2876 }
2881 } 2877 }
2882 2878
2883 static PySequenceMethods ListAsSeq = { 2879 static PySequenceMethods ListAsSeq = {
2884 (lenfunc) ListLength, /* sq_length, len(x) */ 2880 (lenfunc) ListLength, // sq_length, len(x)
2885 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */ 2881 (binaryfunc) 0, // RangeConcat, sq_concat, x+y
2886 0, /* RangeRepeat, sq_repeat, x*n */ 2882 0, // RangeRepeat, sq_repeat, x*n
2887 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */ 2883 (PyIntArgFunc) ListIndex, // sq_item, x[i]
2888 0, /* was_sq_slice, x[i:j] */ 2884 0, // was_sq_slice, x[i:j]
2889 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */ 2885 (PyIntObjArgProc) ListAssIndex, // sq_as_item, x[i]=v
2890 0, /* was_sq_ass_slice, x[i:j]=v */ 2886 0, // was_sq_ass_slice, x[i:j]=v
2891 0, /* sq_contains */ 2887 0, // sq_contains
2892 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */ 2888 (binaryfunc) ListConcatInPlace,// sq_inplace_concat
2893 0, /* sq_inplace_repeat */ 2889 0, // sq_inplace_repeat
2894 }; 2890 };
2895 2891
2896 static PyMappingMethods ListAsMapping = { 2892 static PyMappingMethods ListAsMapping = {
2897 /* mp_length */ (lenfunc) ListLength, 2893 /* mp_length */ (lenfunc) ListLength,
2898 /* mp_subscript */ (binaryfunc) ListItem, 2894 /* mp_subscript */ (binaryfunc) ListItem,
3125 else if (strcmp(name, "__members__") == 0) 3121 else if (strcmp(name, "__members__") == 0)
3126 return ObjectDir(NULL, FunctionAttrs); 3122 return ObjectDir(NULL, FunctionAttrs);
3127 return NULL; 3123 return NULL;
3128 } 3124 }
3129 3125
3130 /* Populate partial_T given function object. 3126 /*
3127 * Populate partial_T given function object.
3131 * 3128 *
3132 * "exported" should be set to true when it is needed to construct a partial 3129 * "exported" should be set to true when it is needed to construct a partial
3133 * that may be stored in a variable (i.e. may be freed by Vim). 3130 * that may be stored in a variable (i.e. may be freed by Vim).
3134 */ 3131 */
3135 static void 3132 static void
3640 3637
3641 return ret; 3638 return ret;
3642 } 3639 }
3643 3640
3644 static PySequenceMethods OptionsAsSeq = { 3641 static PySequenceMethods OptionsAsSeq = {
3645 0, /* sq_length */ 3642 0, // sq_length
3646 0, /* sq_concat */ 3643 0, // sq_concat
3647 0, /* sq_repeat */ 3644 0, // sq_repeat
3648 0, /* sq_item */ 3645 0, // sq_item
3649 0, /* sq_slice */ 3646 0, // sq_slice
3650 0, /* sq_ass_item */ 3647 0, // sq_ass_item
3651 0, /* sq_ass_slice */ 3648 0, // sq_ass_slice
3652 (objobjproc) OptionsContains, /* sq_contains */ 3649 (objobjproc) OptionsContains, // sq_contains
3653 0, /* sq_inplace_concat */ 3650 0, // sq_inplace_concat
3654 0, /* sq_inplace_repeat */ 3651 0, // sq_inplace_repeat
3655 }; 3652 };
3656 3653
3657 static PyMappingMethods OptionsAsMapping = { 3654 static PyMappingMethods OptionsAsMapping = {
3658 (lenfunc) NULL, 3655 (lenfunc) NULL,
3659 (binaryfunc) OptionsItem, 3656 (binaryfunc) OptionsItem,
3660 (objobjargproc) OptionsAssItem, 3657 (objobjargproc) OptionsAssItem,
3661 }; 3658 };
3662 3659
3663 /* Tabpage object 3660 // Tabpage object
3664 */
3665 3661
3666 typedef struct 3662 typedef struct
3667 { 3663 {
3668 PyObject_HEAD 3664 PyObject_HEAD
3669 tabpage_T *tab; 3665 tabpage_T *tab;
3749 return PyLong_FromLong((long) get_tab_number(self->tab)); 3745 return PyLong_FromLong((long) get_tab_number(self->tab));
3750 else if (strcmp(name, "vars") == 0) 3746 else if (strcmp(name, "vars") == 0)
3751 return NEW_DICTIONARY(self->tab->tp_vars); 3747 return NEW_DICTIONARY(self->tab->tp_vars);
3752 else if (strcmp(name, "window") == 0) 3748 else if (strcmp(name, "window") == 0)
3753 { 3749 {
3754 /* For current tab window.c does not bother to set or update tp_curwin 3750 // For current tab window.c does not bother to set or update tp_curwin
3755 */
3756 if (self->tab == curtab) 3751 if (self->tab == curtab)
3757 return WindowNew(curwin, curtab); 3752 return WindowNew(curwin, curtab);
3758 else 3753 else
3759 return WindowNew(self->tab->tp_curwin, self->tab); 3754 return WindowNew(self->tab->tp_curwin, self->tab);
3760 } 3755 }
3779 return PyString_FromFormat("<tabpage %d>", t - 1); 3774 return PyString_FromFormat("<tabpage %d>", t - 1);
3780 } 3775 }
3781 } 3776 }
3782 3777
3783 static struct PyMethodDef TabPageMethods[] = { 3778 static struct PyMethodDef TabPageMethods[] = {
3784 /* name, function, calling, documentation */ 3779 // name, function, calling, documentation
3785 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""}, 3780 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3786 { NULL, NULL, 0, NULL} 3781 { NULL, NULL, 0, NULL}
3787 }; 3782 };
3788 3783
3789 /* 3784 /*
3852 } 3847 }
3853 3848
3854 static PyObject * 3849 static PyObject *
3855 WindowNew(win_T *win, tabpage_T *tab) 3850 WindowNew(win_T *win, tabpage_T *tab)
3856 { 3851 {
3857 /* We need to handle deletion of windows underneath us. 3852 /*
3853 * We need to handle deletion of windows underneath us.
3858 * If we add a "w_python*_ref" field to the win_T structure, 3854 * If we add a "w_python*_ref" field to the win_T structure,
3859 * then we can get at it in win_free() in vim. We then 3855 * then we can get at it in win_free() in vim. We then
3860 * need to create only ONE Python object per window - if 3856 * need to create only ONE Python object per window - if
3861 * we try to create a second, just INCREF the existing one 3857 * we try to create a second, just INCREF the existing one
3862 * and return it. The (single) Python object referring to 3858 * and return it. The (single) Python object referring to
3919 { 3915 {
3920 if (tabObject) 3916 if (tabObject)
3921 { 3917 {
3922 if (CheckTabPage(tabObject)) 3918 if (CheckTabPage(tabObject))
3923 return NULL; 3919 return NULL;
3924 /* For current tab window.c does not bother to set or update tp_firstwin 3920 // For current tab window.c does not bother to set or update tp_firstwin
3925 */
3926 else if (tabObject->tab == curtab) 3921 else if (tabObject->tab == curtab)
3927 return firstwin; 3922 return firstwin;
3928 else 3923 else
3929 return tabObject->tab->tp_firstwin; 3924 return tabObject->tab->tp_firstwin;
3930 } 3925 }
4032 { 4027 {
4033 PyErr_SET_VIM(N_("cursor position outside buffer")); 4028 PyErr_SET_VIM(N_("cursor position outside buffer"));
4034 return -1; 4029 return -1;
4035 } 4030 }
4036 4031
4037 /* Check for keyboard interrupts */ 4032 // Check for keyboard interrupts
4038 if (VimCheckInterrupt()) 4033 if (VimCheckInterrupt())
4039 return -1; 4034 return -1;
4040 4035
4041 self->win->w_cursor.lnum = lnum; 4036 self->win->w_cursor.lnum = lnum;
4042 self->win->w_cursor.col = col; 4037 self->win->w_cursor.col = col;
4043 self->win->w_set_curswant = TRUE; 4038 self->win->w_set_curswant = TRUE;
4044 self->win->w_cursor.coladd = 0; 4039 self->win->w_cursor.coladd = 0;
4045 /* When column is out of range silently correct it. */ 4040 // When column is out of range silently correct it.
4046 check_cursor_col_win(self->win); 4041 check_cursor_col_win(self->win);
4047 4042
4048 update_screen(VALID); 4043 update_screen(VALID);
4049 return 0; 4044 return 0;
4050 } 4045 }
4115 return PyString_FromFormat("<window %d>", w - 1); 4110 return PyString_FromFormat("<window %d>", w - 1);
4116 } 4111 }
4117 } 4112 }
4118 4113
4119 static struct PyMethodDef WindowMethods[] = { 4114 static struct PyMethodDef WindowMethods[] = {
4120 /* name, function, calling, documentation */ 4115 // name, function, calling, documentation
4121 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""}, 4116 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4122 { NULL, NULL, 0, NULL} 4117 { NULL, NULL, 0, NULL}
4123 }; 4118 };
4124 4119
4125 /* 4120 /*
4192 4187
4193 PyErr_SET_STRING(PyExc_IndexError, N_("no such window")); 4188 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
4194 return NULL; 4189 return NULL;
4195 } 4190 }
4196 4191
4197 /* Convert a Python string into a Vim line. 4192 /*
4193 * Convert a Python string into a Vim line.
4198 * 4194 *
4199 * The result is in allocated memory. All internal nulls are replaced by 4195 * The result is in allocated memory. All internal nulls are replaced by
4200 * newline characters. It is an error for the string to contain newline 4196 * newline characters. It is an error for the string to contain newline
4201 * characters. 4197 * characters.
4202 * 4198 *
4261 Py_XDECREF(bytes); 4257 Py_XDECREF(bytes);
4262 return NULL; 4258 return NULL;
4263 } 4259 }
4264 } 4260 }
4265 4261
4266 /* Create a copy of the string, with internal nulls replaced by 4262 /*
4263 * Create a copy of the string, with internal nulls replaced by
4267 * newline characters, as is the vim convention. 4264 * newline characters, as is the vim convention.
4268 */ 4265 */
4269 save = alloc(len+1); 4266 save = alloc(len+1);
4270 if (save == NULL) 4267 if (save == NULL)
4271 { 4268 {
4281 else 4278 else
4282 save[i] = str[i]; 4279 save[i] = str[i];
4283 } 4280 }
4284 4281
4285 save[i] = '\0'; 4282 save[i] = '\0';
4286 Py_XDECREF(bytes); /* Python 2 does nothing here */ 4283 Py_XDECREF(bytes); // Python 2 does nothing here
4287 4284
4288 return save; 4285 return save;
4289 } 4286 }
4290 4287
4291 /* Get a line from the specified buffer. The line number is 4288 /*
4289 * Get a line from the specified buffer. The line number is
4292 * in Vim format (1-based). The line is returned as a Python 4290 * in Vim format (1-based). The line is returned as a Python
4293 * string object. 4291 * string object.
4294 */ 4292 */
4295 static PyObject * 4293 static PyObject *
4296 GetBufferLine(buf_T *buf, PyInt n) 4294 GetBufferLine(buf_T *buf, PyInt n)
4297 { 4295 {
4298 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE)); 4296 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4299 } 4297 }
4300 4298
4301 4299
4302 /* Get a list of lines from the specified buffer. The line numbers 4300 /*
4301 * Get a list of lines from the specified buffer. The line numbers
4303 * are in Vim format (1-based). The range is from lo up to, but not 4302 * are in Vim format (1-based). The range is from lo up to, but not
4304 * including, hi. The list is returned as a Python list of string objects. 4303 * including, hi. The list is returned as a Python list of string objects.
4305 */ 4304 */
4306 static PyObject * 4305 static PyObject *
4307 GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi) 4306 GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4316 for (i = 0; i < n; ++i) 4315 for (i = 0; i < n; ++i)
4317 { 4316 {
4318 PyObject *string = LineToString( 4317 PyObject *string = LineToString(
4319 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE)); 4318 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
4320 4319
4321 /* Error check - was the Python string creation OK? */ 4320 // Error check - was the Python string creation OK?
4322 if (string == NULL) 4321 if (string == NULL)
4323 { 4322 {
4324 Py_DECREF(list); 4323 Py_DECREF(list);
4325 return NULL; 4324 return NULL;
4326 } 4325 }
4327 4326
4328 PyList_SET_ITEM(list, i, string); 4327 PyList_SET_ITEM(list, i, string);
4329 } 4328 }
4330 4329
4331 /* The ownership of the Python list is passed to the caller (ie, 4330 // The ownership of the Python list is passed to the caller (ie,
4332 * the caller should Py_DECREF() the object when it is finished 4331 // the caller should Py_DECREF() the object when it is finished
4333 * with it). 4332 // with it).
4334 */
4335 4333
4336 return list; 4334 return list;
4337 } 4335 }
4338 4336
4339 /* 4337 /*
4344 static void 4342 static void
4345 py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) 4343 py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4346 { 4344 {
4347 if (curwin->w_cursor.lnum >= lo) 4345 if (curwin->w_cursor.lnum >= lo)
4348 { 4346 {
4349 /* Adjust the cursor position if it's in/after the changed 4347 // Adjust the cursor position if it's in/after the changed
4350 * lines. */ 4348 // lines.
4351 if (curwin->w_cursor.lnum >= hi) 4349 if (curwin->w_cursor.lnum >= hi)
4352 { 4350 {
4353 curwin->w_cursor.lnum += extra; 4351 curwin->w_cursor.lnum += extra;
4354 check_cursor_col(); 4352 check_cursor_col();
4355 } 4353 }
4379 { 4377 {
4380 bufref_T save_curbuf = {NULL, 0, 0}; 4378 bufref_T save_curbuf = {NULL, 0, 0};
4381 win_T *save_curwin = NULL; 4379 win_T *save_curwin = NULL;
4382 tabpage_T *save_curtab = NULL; 4380 tabpage_T *save_curtab = NULL;
4383 4381
4384 /* First of all, we check the type of the supplied Python object. 4382 // First of all, we check the type of the supplied Python object.
4385 * There are three cases: 4383 // There are three cases:
4386 * 1. NULL, or None - this is a deletion. 4384 // 1. NULL, or None - this is a deletion.
4387 * 2. A string - this is a replacement. 4385 // 2. A string - this is a replacement.
4388 * 3. Anything else - this is an error. 4386 // 3. Anything else - this is an error.
4389 */
4390 if (line == Py_None || line == NULL) 4387 if (line == Py_None || line == NULL)
4391 { 4388 {
4392 PyErr_Clear(); 4389 PyErr_Clear();
4393 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf); 4390 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
4394 4391
4404 || save_curbuf.br_buf == NULL)) 4401 || save_curbuf.br_buf == NULL))
4405 // Using an existing window for the buffer, adjust the cursor 4402 // Using an existing window for the buffer, adjust the cursor
4406 // position. 4403 // position.
4407 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1); 4404 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
4408 if (save_curbuf.br_buf == NULL) 4405 if (save_curbuf.br_buf == NULL)
4409 /* Only adjust marks if we managed to switch to a window that 4406 // Only adjust marks if we managed to switch to a window that
4410 * holds the buffer, otherwise line numbers will be invalid. */ 4407 // holds the buffer, otherwise line numbers will be invalid.
4411 deleted_lines_mark((linenr_T)n, 1L); 4408 deleted_lines_mark((linenr_T)n, 1L);
4412 } 4409 }
4413 4410
4414 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); 4411 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
4415 4412
4428 if (save == NULL) 4425 if (save == NULL)
4429 return FAIL; 4426 return FAIL;
4430 4427
4431 VimTryStart(); 4428 VimTryStart();
4432 4429
4433 /* We do not need to free "save" if ml_replace() consumes it. */ 4430 // We do not need to free "save" if ml_replace() consumes it.
4434 PyErr_Clear(); 4431 PyErr_Clear();
4435 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf); 4432 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
4436 4433
4437 if (u_savesub((linenr_T)n) == FAIL) 4434 if (u_savesub((linenr_T)n) == FAIL)
4438 { 4435 {
4447 else 4444 else
4448 changed_bytes((linenr_T)n, 0); 4445 changed_bytes((linenr_T)n, 0);
4449 4446
4450 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); 4447 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
4451 4448
4452 /* Check that the cursor is not beyond the end of the line now. */ 4449 // Check that the cursor is not beyond the end of the line now.
4453 if (buf == curbuf) 4450 if (buf == curbuf)
4454 check_cursor_col(); 4451 check_cursor_col();
4455 4452
4456 if (VimTryEnd()) 4453 if (VimTryEnd())
4457 return FAIL; 4454 return FAIL;
4466 PyErr_BadArgument(); 4463 PyErr_BadArgument();
4467 return FAIL; 4464 return FAIL;
4468 } 4465 }
4469 } 4466 }
4470 4467
4471 /* Replace a range of lines in the specified buffer. The line numbers are in 4468 /*
4469 * Replace a range of lines in the specified buffer. The line numbers are in
4472 * Vim format (1-based). The range is from lo up to, but not including, hi. 4470 * Vim format (1-based). The range is from lo up to, but not including, hi.
4473 * The replacement lines are given as a Python list of string objects. The 4471 * The replacement lines are given as a Python list of string objects. The
4474 * list is checked for validity and correct format. Errors are returned as a 4472 * list is checked for validity and correct format. Errors are returned as a
4475 * value of FAIL. The return value is OK on success. 4473 * value of FAIL. The return value is OK on success.
4476 * If OK is returned and len_change is not NULL, *len_change 4474 * If OK is returned and len_change is not NULL, *len_change
4486 { 4484 {
4487 bufref_T save_curbuf = {NULL, 0, 0}; 4485 bufref_T save_curbuf = {NULL, 0, 0};
4488 win_T *save_curwin = NULL; 4486 win_T *save_curwin = NULL;
4489 tabpage_T *save_curtab = NULL; 4487 tabpage_T *save_curtab = NULL;
4490 4488
4491 /* First of all, we check the type of the supplied Python object. 4489 // First of all, we check the type of the supplied Python object.
4492 * There are three cases: 4490 // There are three cases:
4493 * 1. NULL, or None - this is a deletion. 4491 // 1. NULL, or None - this is a deletion.
4494 * 2. A list - this is a replacement. 4492 // 2. A list - this is a replacement.
4495 * 3. Anything else - this is an error. 4493 // 3. Anything else - this is an error.
4496 */
4497 if (list == Py_None || list == NULL) 4494 if (list == Py_None || list == NULL)
4498 { 4495 {
4499 PyInt i; 4496 PyInt i;
4500 PyInt n = (int)(hi - lo); 4497 PyInt n = (int)(hi - lo);
4501 4498
4515 break; 4512 break;
4516 } 4513 }
4517 } 4514 }
4518 if (buf == curbuf && (save_curwin != NULL 4515 if (buf == curbuf && (save_curwin != NULL
4519 || save_curbuf.br_buf == NULL)) 4516 || save_curbuf.br_buf == NULL))
4520 /* Using an existing window for the buffer, adjust the cursor 4517 // Using an existing window for the buffer, adjust the cursor
4521 * position. */ 4518 // position.
4522 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n); 4519 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
4523 if (save_curbuf.br_buf == NULL) 4520 if (save_curbuf.br_buf == NULL)
4524 /* Only adjust marks if we managed to switch to a window that 4521 // Only adjust marks if we managed to switch to a window that
4525 * holds the buffer, otherwise line numbers will be invalid. */ 4522 // holds the buffer, otherwise line numbers will be invalid.
4526 deleted_lines_mark((linenr_T)lo, (long)i); 4523 deleted_lines_mark((linenr_T)lo, (long)i);
4527 } 4524 }
4528 4525
4529 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); 4526 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
4530 4527
4539 else if (PyList_Check(list)) 4536 else if (PyList_Check(list))
4540 { 4537 {
4541 PyInt i; 4538 PyInt i;
4542 PyInt new_len = PyList_Size(list); 4539 PyInt new_len = PyList_Size(list);
4543 PyInt old_len = hi - lo; 4540 PyInt old_len = hi - lo;
4544 PyInt extra = 0; /* lines added to text, can be negative */ 4541 PyInt extra = 0; // lines added to text, can be negative
4545 char **array; 4542 char **array;
4546 4543
4547 if (new_len == 0) /* avoid allocating zero bytes */ 4544 if (new_len == 0) // avoid allocating zero bytes
4548 array = NULL; 4545 array = NULL;
4549 else 4546 else
4550 { 4547 {
4551 array = PyMem_New(char *, new_len); 4548 array = PyMem_New(char *, new_len);
4552 if (array == NULL) 4549 if (array == NULL)
4571 } 4568 }
4572 4569
4573 VimTryStart(); 4570 VimTryStart();
4574 PyErr_Clear(); 4571 PyErr_Clear();
4575 4572
4576 /* START of region without "return". Must call restore_buffer()! */ 4573 // START of region without "return". Must call restore_buffer()!
4577 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf); 4574 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
4578 4575
4579 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL) 4576 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
4580 RAISE_UNDO_FAIL; 4577 RAISE_UNDO_FAIL;
4581 4578
4582 /* If the size of the range is reducing (ie, new_len < old_len) we 4579 // If the size of the range is reducing (ie, new_len < old_len) we
4583 * need to delete some old_len. We do this at the start, by 4580 // need to delete some old_len. We do this at the start, by
4584 * repeatedly deleting line "lo". 4581 // repeatedly deleting line "lo".
4585 */
4586 if (!PyErr_Occurred()) 4582 if (!PyErr_Occurred())
4587 { 4583 {
4588 for (i = 0; i < old_len - new_len; ++i) 4584 for (i = 0; i < old_len - new_len; ++i)
4589 if (ml_delete((linenr_T)lo, FALSE) == FAIL) 4585 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4590 { 4586 {
4592 break; 4588 break;
4593 } 4589 }
4594 extra -= i; 4590 extra -= i;
4595 } 4591 }
4596 4592
4597 /* For as long as possible, replace the existing old_len with the 4593 // For as long as possible, replace the existing old_len with the
4598 * new old_len. This is a more efficient operation, as it requires 4594 // new old_len. This is a more efficient operation, as it requires
4599 * less memory allocation and freeing. 4595 // less memory allocation and freeing.
4600 */
4601 if (!PyErr_Occurred()) 4596 if (!PyErr_Occurred())
4602 { 4597 {
4603 for (i = 0; i < old_len && i < new_len; ++i) 4598 for (i = 0; i < old_len && i < new_len; ++i)
4604 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE) 4599 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4605 == FAIL) 4600 == FAIL)
4609 } 4604 }
4610 } 4605 }
4611 else 4606 else
4612 i = 0; 4607 i = 0;
4613 4608
4614 /* Now we may need to insert the remaining new old_len. If we do, we 4609 // Now we may need to insert the remaining new old_len. If we do, we
4615 * must free the strings as we finish with them (we can't pass the 4610 // must free the strings as we finish with them (we can't pass the
4616 * responsibility to vim in this case). 4611 // responsibility to vim in this case).
4617 */
4618 if (!PyErr_Occurred()) 4612 if (!PyErr_Occurred())
4619 { 4613 {
4620 while (i < new_len) 4614 while (i < new_len)
4621 { 4615 {
4622 if (ml_append((linenr_T)(lo + i - 1), 4616 if (ml_append((linenr_T)(lo + i - 1),
4629 ++i; 4623 ++i;
4630 ++extra; 4624 ++extra;
4631 } 4625 }
4632 } 4626 }
4633 4627
4634 /* Free any left-over old_len, as a result of an error */ 4628 // Free any left-over old_len, as a result of an error
4635 while (i < new_len) 4629 while (i < new_len)
4636 { 4630 {
4637 vim_free(array[i]); 4631 vim_free(array[i]);
4638 ++i; 4632 ++i;
4639 } 4633 }
4640 4634
4641 /* Free the array of old_len. All of its contents have now 4635 // Free the array of old_len. All of its contents have now
4642 * been dealt with (either freed, or the responsibility passed 4636 // been dealt with (either freed, or the responsibility passed
4643 * to vim. 4637 // to vim.
4644 */
4645 PyMem_Free(array); 4638 PyMem_Free(array);
4646 4639
4647 /* Adjust marks. Invalidate any which lie in the 4640 // Adjust marks. Invalidate any which lie in the
4648 * changed range, and move any in the remainder of the buffer. 4641 // changed range, and move any in the remainder of the buffer.
4649 * Only adjust marks if we managed to switch to a window that holds 4642 // Only adjust marks if we managed to switch to a window that holds
4650 * the buffer, otherwise line numbers will be invalid. */ 4643 // the buffer, otherwise line numbers will be invalid.
4651 if (save_curbuf.br_buf == NULL) 4644 if (save_curbuf.br_buf == NULL)
4652 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1), 4645 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
4653 (long)MAXLNUM, (long)extra); 4646 (long)MAXLNUM, (long)extra);
4654 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra); 4647 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4655 4648
4657 || save_curbuf.br_buf == NULL)) 4650 || save_curbuf.br_buf == NULL))
4658 // Using an existing window for the buffer, adjust the cursor 4651 // Using an existing window for the buffer, adjust the cursor
4659 // position. 4652 // position.
4660 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra); 4653 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4661 4654
4662 /* END of region without "return". */ 4655 // END of region without "return".
4663 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); 4656 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
4664 4657
4665 if (VimTryEnd()) 4658 if (VimTryEnd())
4666 return FAIL; 4659 return FAIL;
4667 4660
4675 PyErr_BadArgument(); 4668 PyErr_BadArgument();
4676 return FAIL; 4669 return FAIL;
4677 } 4670 }
4678 } 4671 }
4679 4672
4680 /* Insert a number of lines into the specified buffer after the specified line. 4673 /*
4674 * Insert a number of lines into the specified buffer after the specified line.
4681 * The line number is in Vim format (1-based). The lines to be inserted are 4675 * The line number is in Vim format (1-based). The lines to be inserted are
4682 * given as a Python list of string objects or as a single string. The lines 4676 * given as a Python list of string objects or as a single string. The lines
4683 * to be added are checked for validity and correct format. Errors are 4677 * to be added are checked for validity and correct format. Errors are
4684 * returned as a value of FAIL. The return value is OK on success. 4678 * returned as a value of FAIL. The return value is OK on success.
4685 * If OK is returned and len_change is not NULL, *len_change 4679 * If OK is returned and len_change is not NULL, *len_change
4690 { 4684 {
4691 bufref_T save_curbuf = {NULL, 0, 0}; 4685 bufref_T save_curbuf = {NULL, 0, 0};
4692 win_T *save_curwin = NULL; 4686 win_T *save_curwin = NULL;
4693 tabpage_T *save_curtab = NULL; 4687 tabpage_T *save_curtab = NULL;
4694 4688
4695 /* First of all, we check the type of the supplied Python object. 4689 // First of all, we check the type of the supplied Python object.
4696 * It must be a string or a list, or the call is in error. 4690 // It must be a string or a list, or the call is in error.
4697 */
4698 if (PyBytes_Check(lines) || PyUnicode_Check(lines)) 4691 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
4699 { 4692 {
4700 char *str = StringToLine(lines); 4693 char *str = StringToLine(lines);
4701 4694
4702 if (str == NULL) 4695 if (str == NULL)
4709 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) 4702 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
4710 RAISE_UNDO_FAIL; 4703 RAISE_UNDO_FAIL;
4711 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) 4704 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
4712 RAISE_INSERT_LINE_FAIL; 4705 RAISE_INSERT_LINE_FAIL;
4713 else if (save_curbuf.br_buf == NULL) 4706 else if (save_curbuf.br_buf == NULL)
4714 /* Only adjust marks if we managed to switch to a window that 4707 // Only adjust marks if we managed to switch to a window that
4715 * holds the buffer, otherwise line numbers will be invalid. */ 4708 // holds the buffer, otherwise line numbers will be invalid.
4716 appended_lines_mark((linenr_T)n, 1L); 4709 appended_lines_mark((linenr_T)n, 1L);
4717 4710
4718 vim_free(str); 4711 vim_free(str);
4719 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); 4712 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
4720 update_screen(VALID); 4713 update_screen(VALID);
4767 if (ml_append((linenr_T)(n + i), 4760 if (ml_append((linenr_T)(n + i),
4768 (char_u *)array[i], 0, FALSE) == FAIL) 4761 (char_u *)array[i], 0, FALSE) == FAIL)
4769 { 4762 {
4770 RAISE_INSERT_LINE_FAIL; 4763 RAISE_INSERT_LINE_FAIL;
4771 4764
4772 /* Free the rest of the lines */ 4765 // Free the rest of the lines
4773 while (i < size) 4766 while (i < size)
4774 vim_free(array[i++]); 4767 vim_free(array[i++]);
4775 4768
4776 break; 4769 break;
4777 } 4770 }
4778 vim_free(array[i]); 4771 vim_free(array[i]);
4779 } 4772 }
4780 if (i > 0 && save_curbuf.br_buf == NULL) 4773 if (i > 0 && save_curbuf.br_buf == NULL)
4781 /* Only adjust marks if we managed to switch to a window that 4774 // Only adjust marks if we managed to switch to a window that
4782 * holds the buffer, otherwise line numbers will be invalid. */ 4775 // holds the buffer, otherwise line numbers will be invalid.
4783 appended_lines_mark((linenr_T)n, (long)i); 4776 appended_lines_mark((linenr_T)n, (long)i);
4784 } 4777 }
4785 4778
4786 /* Free the array of lines. All of its contents have now 4779 // Free the array of lines. All of its contents have now
4787 * been freed. */ 4780 // been freed.
4788 PyMem_Free(array); 4781 PyMem_Free(array);
4789 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); 4782 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
4790 4783
4791 update_screen(VALID); 4784 update_screen(VALID);
4792 4785
4922 PyInt *new_end) 4915 PyInt *new_end)
4923 { 4916 {
4924 PyInt size; 4917 PyInt size;
4925 PyInt len_change; 4918 PyInt len_change;
4926 4919
4927 /* Self must be a valid buffer */ 4920 // Self must be a valid buffer
4928 if (CheckBuffer(self)) 4921 if (CheckBuffer(self))
4929 return -1; 4922 return -1;
4930 4923
4931 if (end == -1) 4924 if (end == -1)
4932 end = self->buf->b_ml.ml_line_count; 4925 end = self->buf->b_ml.ml_line_count;
4933 4926
4934 /* Sort out the slice range */ 4927 // Sort out the slice range
4935 size = end - start + 1; 4928 size = end - start + 1;
4936 4929
4937 if (lo < 0) 4930 if (lo < 0)
4938 lo = 0; 4931 lo = 0;
4939 else if (lo > size) 4932 else if (lo > size)
4994 4987
4995 Py_INCREF(Py_None); 4988 Py_INCREF(Py_None);
4996 return Py_None; 4989 return Py_None;
4997 } 4990 }
4998 4991
4999 /* Range object 4992 // Range object
5000 */
5001 4993
5002 static PyTypeObject RangeType; 4994 static PyTypeObject RangeType;
5003 static PySequenceMethods RangeAsSeq; 4995 static PySequenceMethods RangeAsSeq;
5004 static PyMappingMethods RangeAsMapping; 4996 static PyMappingMethods RangeAsMapping;
5005 4997
5058 } 5050 }
5059 5051
5060 static PyInt 5052 static PyInt
5061 RangeLength(RangeObject *self) 5053 RangeLength(RangeObject *self)
5062 { 5054 {
5063 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ 5055 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
5064 if (CheckBuffer(self->buf)) 5056 if (CheckBuffer(self->buf))
5065 return -1; /* ??? */ 5057 return -1; // ???
5066 5058
5067 return (self->end - self->start + 1); 5059 return (self->end - self->start + 1);
5068 } 5060 }
5069 5061
5070 static PyObject * 5062 static PyObject *
5113 name, (int)self->start, (int)self->end); 5105 name, (int)self->start, (int)self->end);
5114 } 5106 }
5115 } 5107 }
5116 5108
5117 static struct PyMethodDef RangeMethods[] = { 5109 static struct PyMethodDef RangeMethods[] = {
5118 /* name, function, calling, documentation */ 5110 // name, function, calling, documentation
5119 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" }, 5111 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
5120 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""}, 5112 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5121 { NULL, NULL, 0, NULL} 5113 { NULL, NULL, 0, NULL}
5122 }; 5114 };
5123 5115
5126 static PyMappingMethods BufferAsMapping; 5118 static PyMappingMethods BufferAsMapping;
5127 5119
5128 static PyObject * 5120 static PyObject *
5129 BufferNew(buf_T *buf) 5121 BufferNew(buf_T *buf)
5130 { 5122 {
5131 /* We need to handle deletion of buffers underneath us. 5123 /*
5124 * We need to handle deletion of buffers underneath us.
5132 * If we add a "b_python*_ref" field to the buf_T structure, 5125 * If we add a "b_python*_ref" field to the buf_T structure,
5133 * then we can get at it in buf_freeall() in vim. We then 5126 * then we can get at it in buf_freeall() in vim. We then
5134 * need to create only ONE Python object per buffer - if 5127 * need to create only ONE Python object per buffer - if
5135 * we try to create a second, just INCREF the existing one 5128 * we try to create a second, just INCREF the existing one
5136 * and return it. The (single) Python object referring to 5129 * and return it. The (single) Python object referring to
5174 } 5167 }
5175 5168
5176 static PyInt 5169 static PyInt
5177 BufferLength(BufferObject *self) 5170 BufferLength(BufferObject *self)
5178 { 5171 {
5179 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ 5172 // HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION?
5180 if (CheckBuffer(self)) 5173 if (CheckBuffer(self))
5181 return -1; /* ??? */ 5174 return -1; // ???
5182 5175
5183 return (PyInt)(self->buf->b_ml.ml_line_count); 5176 return (PyInt)(self->buf->b_ml.ml_line_count);
5184 } 5177 }
5185 5178
5186 static PyObject * 5179 static PyObject *
5253 5246
5254 if (!(val = StringToChars(valObject, &todecref))) 5247 if (!(val = StringToChars(valObject, &todecref)))
5255 return -1; 5248 return -1;
5256 5249
5257 VimTryStart(); 5250 VimTryStart();
5258 /* Using aucmd_*: autocommands will be executed by rename_buffer */ 5251 // Using aucmd_*: autocommands will be executed by rename_buffer
5259 aucmd_prepbuf(&aco, self->buf); 5252 aucmd_prepbuf(&aco, self->buf);
5260 ren_ret = rename_buffer(val); 5253 ren_ret = rename_buffer(val);
5261 aucmd_restbuf(&aco); 5254 aucmd_restbuf(&aco);
5262 Py_XDECREF(todecref); 5255 Py_XDECREF(todecref);
5263 if (VimTryEnd()) 5256 if (VimTryEnd())
5323 return NULL; 5316 return NULL;
5324 } 5317 }
5325 5318
5326 if (posp->lnum <= 0) 5319 if (posp->lnum <= 0)
5327 { 5320 {
5328 /* Or raise an error? */ 5321 // Or raise an error?
5329 Py_INCREF(Py_None); 5322 Py_INCREF(Py_None);
5330 return Py_None; 5323 return Py_None;
5331 } 5324 }
5332 5325
5333 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col)); 5326 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5363 return PyString_FromFormat("<buffer %s>", name); 5356 return PyString_FromFormat("<buffer %s>", name);
5364 } 5357 }
5365 } 5358 }
5366 5359
5367 static struct PyMethodDef BufferMethods[] = { 5360 static struct PyMethodDef BufferMethods[] = {
5368 /* name, function, calling, documentation */ 5361 // name, function, calling, documentation
5369 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" }, 5362 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
5370 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" }, 5363 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
5371 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" }, 5364 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" },
5372 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""}, 5365 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5373 { NULL, NULL, 0, NULL} 5366 { NULL, NULL, 0, NULL}
5420 } 5413 }
5421 5414
5422 static void 5415 static void
5423 BufMapIterDestruct(PyObject *buffer) 5416 BufMapIterDestruct(PyObject *buffer)
5424 { 5417 {
5425 /* Iteration was stopped before all buffers were processed */ 5418 // Iteration was stopped before all buffers were processed
5426 if (buffer) 5419 if (buffer)
5427 { 5420 {
5428 Py_DECREF(buffer); 5421 Py_DECREF(buffer);
5429 } 5422 }
5430 } 5423 }
5465 if (!((BufferObject *)(ret))->buf->b_next) 5458 if (!((BufferObject *)(ret))->buf->b_next)
5466 next = NULL; 5459 next = NULL;
5467 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next))) 5460 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
5468 return NULL; 5461 return NULL;
5469 *buffer = next; 5462 *buffer = next;
5470 /* Do not increment reference: we no longer hold it (decref), but whoever 5463 // Do not increment reference: we no longer hold it (decref), but whoever
5471 * on other side will hold (incref). Decref+incref = nothing. */ 5464 // on other side will hold (incref). Decref+incref = nothing.
5472 return ret; 5465 return ret;
5473 } 5466 }
5474 5467
5475 static PyObject * 5468 static PyObject *
5476 BufMapIter(PyObject *self UNUSED) 5469 BufMapIter(PyObject *self UNUSED)
5487 (lenfunc) BufMapLength, 5480 (lenfunc) BufMapLength,
5488 (binaryfunc) BufMapItem, 5481 (binaryfunc) BufMapItem,
5489 (objobjargproc) 0, 5482 (objobjargproc) 0,
5490 }; 5483 };
5491 5484
5492 /* Current items object 5485 // Current items object
5493 */
5494 5486
5495 static char *CurrentAttrs[] = { 5487 static char *CurrentAttrs[] = {
5496 "buffer", "window", "line", "range", "tabpage", 5488 "buffer", "window", "line", "range", "tabpage",
5497 NULL 5489 NULL
5498 }; 5490 };
5632 return -1; 5624 return -1;
5633 } 5625 }
5634 } 5626 }
5635 5627
5636 static struct PyMethodDef CurrentMethods[] = { 5628 static struct PyMethodDef CurrentMethods[] = {
5637 /* name, function, calling, documentation */ 5629 // name, function, calling, documentation
5638 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""}, 5630 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5639 { NULL, NULL, 0, NULL} 5631 { NULL, NULL, 0, NULL}
5640 }; 5632 };
5641 5633
5642 static void 5634 static void
5742 PyObject *ret; 5734 PyObject *ret;
5743 5735
5744 #ifdef PY_CAN_RECURSE 5736 #ifdef PY_CAN_RECURSE
5745 *pygilstate = PyGILState_Ensure(); 5737 *pygilstate = PyGILState_Ensure();
5746 #endif 5738 #endif
5747 /* Check the line number, the command my have deleted lines. */ 5739 // Check the line number, the command my have deleted lines.
5748 if (lnum > curbuf->b_ml.ml_line_count 5740 if (lnum > curbuf->b_ml.ml_line_count
5749 || !(line = GetBufferLine(curbuf, lnum))) 5741 || !(line = GetBufferLine(curbuf, lnum)))
5750 goto err; 5742 goto err;
5751 if (!(linenr = PyInt_FromLong((long) lnum))) 5743 if (!(linenr = PyInt_FromLong((long) lnum)))
5752 { 5744 {
5757 Py_DECREF(line); 5749 Py_DECREF(line);
5758 Py_DECREF(linenr); 5750 Py_DECREF(linenr);
5759 if (!ret) 5751 if (!ret)
5760 goto err; 5752 goto err;
5761 5753
5762 /* Check that the command didn't switch to another buffer. */ 5754 // Check that the command didn't switch to another buffer.
5763 if (curbuf != was_curbuf) 5755 if (curbuf != was_curbuf)
5764 { 5756 {
5765 Py_XDECREF(ret); 5757 Py_XDECREF(ret);
5766 goto err; 5758 goto err;
5767 } 5759 }
6115 if (py_to_tv(obj, tv, lookup_dict) == -1) 6107 if (py_to_tv(obj, tv, lookup_dict) == -1)
6116 { 6108 {
6117 tv->v_type = VAR_UNKNOWN; 6109 tv->v_type = VAR_UNKNOWN;
6118 return -1; 6110 return -1;
6119 } 6111 }
6120 /* As we are not using copy_tv which increments reference count we must 6112 // As we are not using copy_tv which increments reference count we must
6121 * do it ourself. */ 6113 // do it ourself.
6122 if (tv->v_type == VAR_DICT) 6114 if (tv->v_type == VAR_DICT)
6123 ++tv->vval.v_dict->dv_refcount; 6115 ++tv->vval.v_dict->dv_refcount;
6124 else if (tv->v_type == VAR_LIST) 6116 else if (tv->v_type == VAR_LIST)
6125 ++tv->vval.v_list->lv_refcount; 6117 ++tv->vval.v_list->lv_refcount;
6126 } 6118 }
6308 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj); 6300 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6309 } 6301 }
6310 #endif 6302 #endif
6311 else if (PyObject_HasAttrString(obj, "keys")) 6303 else if (PyObject_HasAttrString(obj, "keys"))
6312 return convert_dl(obj, tv, pymap_to_tv, lookup_dict); 6304 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
6313 /* PyObject_GetIter can create built-in iterator for any sequence object */ 6305 // PyObject_GetIter can create built-in iterator for any sequence object
6314 else if (PyIter_Check(obj) || PySequence_Check(obj)) 6306 else if (PyIter_Check(obj) || PySequence_Check(obj))
6315 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict); 6307 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
6316 else if (PyMapping_Check(obj)) 6308 else if (PyMapping_Check(obj))
6317 return convert_dl(obj, tv, pymap_to_tv, lookup_dict); 6309 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
6318 else if (PyNumber_Check(obj)) 6310 else if (PyNumber_Check(obj))
6718 VimTryStart(); 6710 VimTryStart();
6719 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n" 6711 emsg(_("Failed to set path hook: sys.path_hooks is not a list\n"
6720 "You should now do the following:\n" 6712 "You should now do the following:\n"
6721 "- append vim.path_hook to sys.path_hooks\n" 6713 "- append vim.path_hook to sys.path_hooks\n"
6722 "- append vim.VIM_SPECIAL_PATH to sys.path\n")); 6714 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6723 VimTryEnd(); /* Discard the error */ 6715 VimTryEnd(); // Discard the error
6724 Py_DECREF(path_hook); 6716 Py_DECREF(path_hook);
6725 return 0; 6717 return 0;
6726 } 6718 }
6727 6719
6728 if (!(path = PySys_GetObject("path"))) 6720 if (!(path = PySys_GetObject("path")))
6746 else 6738 else
6747 { 6739 {
6748 VimTryStart(); 6740 VimTryStart();
6749 emsg(_("Failed to set path: sys.path is not a list\n" 6741 emsg(_("Failed to set path: sys.path is not a list\n"
6750 "You should now append vim.VIM_SPECIAL_PATH to sys.path")); 6742 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6751 VimTryEnd(); /* Discard the error */ 6743 VimTryEnd(); // Discard the error
6752 } 6744 }
6753 6745
6754 return 0; 6746 return 0;
6755 } 6747 }
6756 6748