Mercurial > vim
comparison src/if_python3.c @ 2399:76f0c4918f5c vim73
Move some common code from if_python.c and if_python3.c to if_py_both.h.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sat, 24 Jul 2010 23:51:45 +0200 |
parents | aeea25941392 |
children | eb4718cb2a76 |
comparison
equal
deleted
inserted
replaced
2398:0c8219a26bc9 | 2399:76f0c4918f5c |
---|---|
65 #endif | 65 #endif |
66 #undef main /* Defined in python.h - aargh */ | 66 #undef main /* Defined in python.h - aargh */ |
67 #undef HAVE_FCNTL_H /* Clash with os_win32.h */ | 67 #undef HAVE_FCNTL_H /* Clash with os_win32.h */ |
68 | 68 |
69 static void init_structs(void); | 69 static void init_structs(void); |
70 | |
71 #define PyInt Py_ssize_t | |
70 | 72 |
71 #if defined(DYNAMIC_PYTHON3) | 73 #if defined(DYNAMIC_PYTHON3) |
72 | 74 |
73 #ifndef WIN3264 | 75 #ifndef WIN3264 |
74 #include <dlfcn.h> | 76 #include <dlfcn.h> |
302 }; | 304 }; |
303 | 305 |
304 /* | 306 /* |
305 * Free python.dll | 307 * Free python.dll |
306 */ | 308 */ |
307 static void end_dynamic_python3(void) | 309 static void |
310 end_dynamic_python3(void) | |
308 { | 311 { |
309 if (hinstPy3 != 0) | 312 if (hinstPy3 != 0) |
310 { | 313 { |
311 close_dll(hinstPy3); | 314 close_dll(hinstPy3); |
312 hinstPy3 = 0; | 315 hinstPy3 = 0; |
316 /* | 319 /* |
317 * Load library and get all pointers. | 320 * Load library and get all pointers. |
318 * Parameter 'libname' provides name of DLL. | 321 * Parameter 'libname' provides name of DLL. |
319 * Return OK or FAIL. | 322 * Return OK or FAIL. |
320 */ | 323 */ |
321 static int py3_runtime_link_init(char *libname, int verbose) | 324 static int |
325 py3_runtime_link_init(char *libname, int verbose) | |
322 { | 326 { |
323 int i; | 327 int i; |
324 void *ucs_from_string, *ucs_from_string_and_size; | 328 void *ucs_from_string, *ucs_from_string_and_size; |
325 | 329 |
326 #if defined(UNIX) && defined(FEAT_PYTHON) | 330 #if defined(UNIX) && defined(FEAT_PYTHON) |
388 | 392 |
389 /* | 393 /* |
390 * If python is enabled (there is installed python on Windows system) return | 394 * If python is enabled (there is installed python on Windows system) return |
391 * TRUE, else FALSE. | 395 * TRUE, else FALSE. |
392 */ | 396 */ |
393 int python3_enabled(int verbose) | 397 int |
398 python3_enabled(int verbose) | |
394 { | 399 { |
395 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK; | 400 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK; |
396 } | 401 } |
397 | 402 |
398 /* Load the standard Python exceptions - don't import the symbols from the | 403 /* Load the standard Python exceptions - don't import the symbols from the |
399 * DLL, as this can cause errors (importing data symbols is not reliable). | 404 * DLL, as this can cause errors (importing data symbols is not reliable). |
400 */ | 405 */ |
401 static void get_py3_exceptions __ARGS((void)); | 406 static void get_py3_exceptions __ARGS((void)); |
402 | 407 |
403 static void get_py3_exceptions() | 408 static void |
409 get_py3_exceptions() | |
404 { | 410 { |
405 PyObject *exmod = PyImport_ImportModule("builtins"); | 411 PyObject *exmod = PyImport_ImportModule("builtins"); |
406 PyObject *exdict = PyModule_GetDict(exmod); | 412 PyObject *exdict = PyModule_GetDict(exmod); |
407 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); | 413 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); |
408 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); | 414 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); |
416 Py_XINCREF(p3imp_PyExc_ValueError); | 422 Py_XINCREF(p3imp_PyExc_ValueError); |
417 Py_XDECREF(exmod); | 423 Py_XDECREF(exmod); |
418 } | 424 } |
419 #endif /* DYNAMIC_PYTHON3 */ | 425 #endif /* DYNAMIC_PYTHON3 */ |
420 | 426 |
421 static void call_PyObject_Free(void *p) | 427 /* |
428 * Include the code shared with if_python.c | |
429 */ | |
430 #include "if_py_both.h" | |
431 | |
432 static void | |
433 call_PyObject_Free(void *p) | |
422 { | 434 { |
423 #ifdef Py_DEBUG | 435 #ifdef Py_DEBUG |
424 _PyObject_DebugFree(p); | 436 _PyObject_DebugFree(p); |
425 #else | 437 #else |
426 PyObject_Free(p); | 438 PyObject_Free(p); |
427 #endif | 439 #endif |
428 } | 440 } |
429 static PyObject* call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) | 441 |
442 static PyObject * | |
443 call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
430 { | 444 { |
431 return PyType_GenericNew(type,args,kwds); | 445 return PyType_GenericNew(type,args,kwds); |
432 } | 446 } |
433 static PyObject* call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) | 447 |
448 static PyObject * | |
449 call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) | |
434 { | 450 { |
435 return PyType_GenericAlloc(type,nitems); | 451 return PyType_GenericAlloc(type,nitems); |
436 } | 452 } |
437 | 453 |
438 /****************************************************** | 454 /****************************************************** |
458 static PyObject *GetBufferLineList(buf_T *buf, Py_ssize_t lo, Py_ssize_t hi); | 474 static PyObject *GetBufferLineList(buf_T *buf, Py_ssize_t lo, Py_ssize_t hi); |
459 | 475 |
460 static PyObject *LineToString(const char *); | 476 static PyObject *LineToString(const char *); |
461 static char *StringToLine(PyObject *); | 477 static char *StringToLine(PyObject *); |
462 | 478 |
463 static int VimErrorCheck(void); | |
464 | |
465 #define PyErr_SetVim(str) PyErr_SetString(VimError, str) | 479 #define PyErr_SetVim(str) PyErr_SetString(VimError, str) |
466 | 480 |
467 /****************************************************** | 481 /****************************************************** |
468 * 1. Python interpreter main program. | 482 * 1. Python interpreter main program. |
469 */ | 483 */ |
471 static int py3initialised = 0; | 485 static int py3initialised = 0; |
472 | 486 |
473 | 487 |
474 static PyGILState_STATE pygilstate = PyGILState_UNLOCKED; | 488 static PyGILState_STATE pygilstate = PyGILState_UNLOCKED; |
475 | 489 |
476 /* | 490 void |
477 * obtain a lock on the Vim data structures | 491 python3_end() |
478 */ | |
479 static void Python_Lock_Vim(void) | |
480 { | |
481 } | |
482 | |
483 /* | |
484 * release a lock on the Vim data structures | |
485 */ | |
486 static void Python_Release_Vim(void) | |
487 { | |
488 } | |
489 | |
490 void python3_end() | |
491 { | 492 { |
492 static int recurse = 0; | 493 static int recurse = 0; |
493 | 494 |
494 /* If a crash occurs while doing this, don't try again. */ | 495 /* If a crash occurs while doing this, don't try again. */ |
495 if (recurse != 0) | 496 if (recurse != 0) |
522 { | 523 { |
523 return (hinstPy3 != 0); | 524 return (hinstPy3 != 0); |
524 } | 525 } |
525 #endif | 526 #endif |
526 | 527 |
527 static int Python3_Init(void) | 528 static int |
529 Python3_Init(void) | |
528 { | 530 { |
529 if (!py3initialised) | 531 if (!py3initialised) |
530 { | 532 { |
531 #ifdef DYNAMIC_PYTHON3 | 533 #ifdef DYNAMIC_PYTHON3 |
532 if (!python3_enabled(TRUE)) | 534 if (!python3_enabled(TRUE)) |
586 } | 588 } |
587 | 589 |
588 /* | 590 /* |
589 * External interface | 591 * External interface |
590 */ | 592 */ |
591 static void DoPy3Command(exarg_T *eap, const char *cmd) | 593 static void |
594 DoPy3Command(exarg_T *eap, const char *cmd) | |
592 { | 595 { |
593 #if defined(MACOS) && !defined(MACOS_X_UNIX) | 596 #if defined(MACOS) && !defined(MACOS_X_UNIX) |
594 GrafPtr oldPort; | 597 GrafPtr oldPort; |
595 #endif | 598 #endif |
596 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) | 599 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
648 } | 651 } |
649 | 652 |
650 /* | 653 /* |
651 * ":py3" | 654 * ":py3" |
652 */ | 655 */ |
653 void ex_py3(exarg_T *eap) | 656 void |
657 ex_py3(exarg_T *eap) | |
654 { | 658 { |
655 char_u *script; | 659 char_u *script; |
656 | 660 |
657 script = script_get(eap, eap->arg); | 661 script = script_get(eap, eap->arg); |
658 if (!eap->skip) | 662 if (!eap->skip) |
729 */ | 733 */ |
730 | 734 |
731 static PyObject *OutputGetattro(PyObject *, PyObject *); | 735 static PyObject *OutputGetattro(PyObject *, PyObject *); |
732 static int OutputSetattro(PyObject *, PyObject *, PyObject *); | 736 static int OutputSetattro(PyObject *, PyObject *, PyObject *); |
733 | 737 |
734 static PyObject *OutputWrite(PyObject *, PyObject *); | |
735 static PyObject *OutputWritelines(PyObject *, PyObject *); | |
736 | |
737 typedef void (*writefn)(char_u *); | |
738 static void writer(writefn fn, char_u *str, Py_ssize_t n); | |
739 | |
740 /* Output object definition | |
741 */ | |
742 | |
743 typedef struct | |
744 { | |
745 PyObject_HEAD | |
746 long softspace; | |
747 long error; | |
748 } OutputObject; | |
749 | |
750 static struct PyMethodDef OutputMethods[] = { | |
751 /* name, function, calling, documentation */ | |
752 {"write", OutputWrite, 1, "" }, | |
753 {"writelines", OutputWritelines, 1, "" }, | |
754 { NULL, NULL, 0, NULL } | |
755 }; | |
756 | |
757 static PyTypeObject OutputType; | |
758 | |
759 /*************/ | 738 /*************/ |
760 | 739 |
761 static PyObject * OutputGetattro(PyObject *self, PyObject *nameobj) | 740 static PyObject * |
741 OutputGetattro(PyObject *self, PyObject *nameobj) | |
762 { | 742 { |
763 char *name = ""; | 743 char *name = ""; |
764 if (PyUnicode_Check(nameobj)) | 744 if (PyUnicode_Check(nameobj)) |
765 name = _PyUnicode_AsString(nameobj); | 745 name = _PyUnicode_AsString(nameobj); |
766 | 746 |
768 return PyLong_FromLong(((OutputObject *)(self))->softspace); | 748 return PyLong_FromLong(((OutputObject *)(self))->softspace); |
769 | 749 |
770 return PyObject_GenericGetAttr(self, nameobj); | 750 return PyObject_GenericGetAttr(self, nameobj); |
771 } | 751 } |
772 | 752 |
773 static int OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) | 753 static int |
754 OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) | |
774 { | 755 { |
775 char *name = ""; | 756 char *name = ""; |
776 if (PyUnicode_Check(nameobj)) | 757 if (PyUnicode_Check(nameobj)) |
777 name = _PyUnicode_AsString(nameobj); | 758 name = _PyUnicode_AsString(nameobj); |
778 | 759 |
794 | 775 |
795 PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); | 776 PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); |
796 return -1; | 777 return -1; |
797 } | 778 } |
798 | 779 |
799 /*************/ | |
800 | |
801 static PyObject * OutputWrite(PyObject *self, PyObject *args) | |
802 { | |
803 int len; | |
804 char *str; | |
805 int error = ((OutputObject *)(self))->error; | |
806 | |
807 if (!PyArg_ParseTuple(args, "s#", &str, &len)) | |
808 return NULL; | |
809 | |
810 Py_BEGIN_ALLOW_THREADS | |
811 Python_Lock_Vim(); | |
812 writer((writefn)(error ? emsg : msg), (char_u *)str, len); | |
813 Python_Release_Vim(); | |
814 Py_END_ALLOW_THREADS | |
815 | |
816 Py_INCREF(Py_None); | |
817 return Py_None; | |
818 } | |
819 | |
820 static PyObject * OutputWritelines(PyObject *self, PyObject *args) | |
821 { | |
822 Py_ssize_t n; | |
823 Py_ssize_t i; | |
824 PyObject *list; | |
825 int error = ((OutputObject *)(self))->error; | |
826 | |
827 if (!PyArg_ParseTuple(args, "O", &list)) | |
828 return NULL; | |
829 Py_INCREF(list); | |
830 | |
831 if (!PyList_Check(list)) { | |
832 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); | |
833 Py_DECREF(list); | |
834 return NULL; | |
835 } | |
836 | |
837 n = PyList_Size(list); | |
838 | |
839 for (i = 0; i < n; ++i) | |
840 { | |
841 PyObject *line = PyList_GetItem(list, i); | |
842 char *str; | |
843 Py_ssize_t len; | |
844 | |
845 if (!PyArg_Parse(line, "s#", &str, &len)) { | |
846 PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings")); | |
847 Py_DECREF(list); | |
848 return NULL; | |
849 } | |
850 | |
851 Py_BEGIN_ALLOW_THREADS | |
852 Python_Lock_Vim(); | |
853 writer((writefn)(error ? emsg : msg), (char_u *)str, len); | |
854 Python_Release_Vim(); | |
855 Py_END_ALLOW_THREADS | |
856 } | |
857 | |
858 Py_DECREF(list); | |
859 Py_INCREF(Py_None); | |
860 return Py_None; | |
861 } | |
862 | |
863 /* Output buffer management | |
864 */ | |
865 | |
866 static char_u *buffer = NULL; | |
867 static Py_ssize_t buffer_len = 0; | |
868 static Py_ssize_t buffer_size = 0; | |
869 | |
870 static writefn old_fn = NULL; | |
871 | |
872 static void buffer_ensure(Py_ssize_t n) | |
873 { | |
874 Py_ssize_t new_size; | |
875 char_u *new_buffer; | |
876 | |
877 if (n < buffer_size) | |
878 return; | |
879 | |
880 new_size = buffer_size; | |
881 while (new_size < n) | |
882 new_size += 80; | |
883 | |
884 if (new_size != buffer_size) | |
885 { | |
886 new_buffer = alloc((unsigned)new_size); | |
887 if (new_buffer == NULL) | |
888 return; | |
889 | |
890 if (buffer) | |
891 { | |
892 memcpy(new_buffer, buffer, buffer_len); | |
893 vim_free(buffer); | |
894 } | |
895 | |
896 buffer = new_buffer; | |
897 buffer_size = new_size; | |
898 } | |
899 } | |
900 | |
901 static void PythonIO_Flush(void) | |
902 { | |
903 if (old_fn && buffer_len) | |
904 { | |
905 buffer[buffer_len] = 0; | |
906 old_fn(buffer); | |
907 } | |
908 | |
909 buffer_len = 0; | |
910 } | |
911 | |
912 static void writer(writefn fn, char_u *str, Py_ssize_t n) | |
913 { | |
914 char_u *ptr; | |
915 | |
916 if (fn != old_fn && old_fn != NULL) | |
917 PythonIO_Flush(); | |
918 | |
919 old_fn = fn; | |
920 | |
921 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL) | |
922 { | |
923 Py_ssize_t len = ptr - str; | |
924 | |
925 buffer_ensure(buffer_len + len + 1); | |
926 | |
927 memcpy(buffer + buffer_len, str, len); | |
928 buffer_len += len; | |
929 buffer[buffer_len] = 0; | |
930 fn(buffer); | |
931 str = ptr + 1; | |
932 n -= len + 1; | |
933 buffer_len = 0; | |
934 } | |
935 | |
936 /* Put the remaining text into the buffer for later printing */ | |
937 buffer_ensure(buffer_len + n + 1); | |
938 memcpy(buffer + buffer_len, str, n); | |
939 buffer_len += n; | |
940 } | |
941 | |
942 /***************/ | 780 /***************/ |
943 | 781 |
944 static OutputObject Output = | 782 static int |
945 { | 783 PythonIO_Init(void) |
946 PyObject_HEAD_INIT(&OutputType) | |
947 0, | |
948 0 | |
949 }; | |
950 | |
951 static OutputObject Error = | |
952 { | |
953 PyObject_HEAD_INIT(&OutputType) | |
954 0, | |
955 1 | |
956 }; | |
957 | |
958 static int PythonIO_Init(void) | |
959 { | 784 { |
960 PyType_Ready(&OutputType); | 785 PyType_Ready(&OutputType); |
961 | 786 return PythonIO_Init_io(); |
962 PySys_SetObject("stdout", (PyObject *)(void *)&Output); | 787 } |
963 PySys_SetObject("stderr", (PyObject *)(void *)&Error); | 788 |
964 | 789 static void |
965 if (PyErr_Occurred()) | 790 PythonIO_Fini(void) |
966 { | |
967 EMSG(_("E264: Python: Error initialising I/O objects")); | |
968 return -1; | |
969 } | |
970 | |
971 return 0; | |
972 } | |
973 static void PythonIO_Fini(void) | |
974 { | 791 { |
975 PySys_SetObject("stdout", NULL); | 792 PySys_SetObject("stdout", NULL); |
976 PySys_SetObject("stderr", NULL); | 793 PySys_SetObject("stderr", NULL); |
977 } | 794 } |
978 | 795 |
981 */ | 798 */ |
982 | 799 |
983 /* Vim module - Implementation functions | 800 /* Vim module - Implementation functions |
984 * ------------------------------------- | 801 * ------------------------------------- |
985 */ | 802 */ |
986 | |
987 static PyObject *VimError; | |
988 | 803 |
989 static PyObject *VimCommand(PyObject *, PyObject *); | 804 static PyObject *VimCommand(PyObject *, PyObject *); |
990 static PyObject *VimEval(PyObject *, PyObject *); | 805 static PyObject *VimEval(PyObject *, PyObject *); |
991 | 806 |
992 /* Window type - Implementation functions | 807 /* Window type - Implementation functions |
1094 /* Vim module - Definitions | 909 /* Vim module - Definitions |
1095 */ | 910 */ |
1096 | 911 |
1097 static struct PyMethodDef VimMethods[] = { | 912 static struct PyMethodDef VimMethods[] = { |
1098 /* name, function, calling, documentation */ | 913 /* name, function, calling, documentation */ |
1099 {"command", VimCommand, 1, "Execute a Vim ex-mode command" }, | 914 {"command", VimCommand, 1, "Execute a Vim ex-mode command" }, |
1100 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" }, | 915 {"eval", VimEval, 1, "Evaluate an expression using Vim evaluator" }, |
1101 { NULL, NULL, 0, NULL } | 916 { NULL, NULL, 0, NULL } |
1102 }; | 917 }; |
1103 | 918 |
1104 /* Vim module - Implementation | 919 /* Vim module - Implementation |
1105 */ | 920 */ |
1106 /*ARGSUSED*/ | 921 static PyObject * |
1107 static PyObject * VimCommand(PyObject *self UNUSED, PyObject *args) | 922 VimCommand(PyObject *self UNUSED, PyObject *args) |
1108 { | 923 { |
1109 char *cmd; | 924 char *cmd; |
1110 PyObject *result; | 925 PyObject *result; |
1111 | 926 |
1112 if (!PyArg_ParseTuple(args, "s", &cmd)) | 927 if (!PyArg_ParseTuple(args, "s", &cmd)) |
1138 * translate lists/dictionaries into their Python equivalents. | 953 * translate lists/dictionaries into their Python equivalents. |
1139 * | 954 * |
1140 * The depth parameter is to avoid infinite recursion, set it to 1 when | 955 * The depth parameter is to avoid infinite recursion, set it to 1 when |
1141 * you call VimToPython. | 956 * you call VimToPython. |
1142 */ | 957 */ |
1143 static PyObject * VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) | 958 static PyObject * |
1144 { | 959 VimToPython(typval_T *our_tv, int depth, PyObject *lookupDict) |
1145 PyObject *result; | 960 { |
1146 PyObject *newObj; | 961 PyObject *result; |
962 PyObject *newObj; | |
1147 char ptrBuf[NUMBUFLEN]; | 963 char ptrBuf[NUMBUFLEN]; |
1148 | 964 |
1149 /* Avoid infinite recursion */ | 965 /* Avoid infinite recursion */ |
1150 if (depth > 100) | 966 if (depth > 100) |
1151 { | 967 { |
1214 { | 1030 { |
1215 result = PyDict_New(); | 1031 result = PyDict_New(); |
1216 | 1032 |
1217 if (our_tv->vval.v_dict != NULL) | 1033 if (our_tv->vval.v_dict != NULL) |
1218 { | 1034 { |
1219 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab; | 1035 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab; |
1220 long_u t = ht->ht_used; | 1036 long_u todo = ht->ht_used; |
1221 hashitem_T *hi; | 1037 hashitem_T *hi; |
1222 dictitem_T *di; | 1038 dictitem_T *di; |
1223 | 1039 |
1224 PyDict_SetItemString(lookupDict, ptrBuf, result); | 1040 PyDict_SetItemString(lookupDict, ptrBuf, result); |
1225 | 1041 |
1226 for (hi = ht->ht_array; t > 0; ++hi) | 1042 for (hi = ht->ht_array; todo > 0; ++hi) |
1227 { | 1043 { |
1228 if (!HASHITEM_EMPTY(hi)) | 1044 if (!HASHITEM_EMPTY(hi)) |
1229 { | 1045 { |
1230 --t; | 1046 --todo; |
1231 | 1047 |
1232 di = dict_lookup(hi); | 1048 di = dict_lookup(hi); |
1233 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict); | 1049 newObj = VimToPython(&di->di_tv, depth + 1, lookupDict); |
1234 PyDict_SetItemString(result, (char *)hi->hi_key, newObj); | 1050 PyDict_SetItemString(result, (char *)hi->hi_key, newObj); |
1235 Py_DECREF(newObj); | 1051 Py_DECREF(newObj); |
1245 | 1061 |
1246 return result; | 1062 return result; |
1247 } | 1063 } |
1248 #endif | 1064 #endif |
1249 | 1065 |
1250 /*ARGSUSED*/ | 1066 static PyObject * |
1251 static PyObject * VimEval(PyObject *self UNUSED, PyObject *args) | 1067 VimEval(PyObject *self UNUSED, PyObject *args) |
1252 { | 1068 { |
1253 #ifdef FEAT_EVAL | 1069 #ifdef FEAT_EVAL |
1254 char *expr; | 1070 char *expr; |
1255 typval_T *our_tv; | 1071 typval_T *our_tv; |
1256 PyObject *result; | 1072 PyObject *result; |
1257 PyObject *lookup_dict; | 1073 PyObject *lookup_dict; |
1258 | 1074 |
1259 if (!PyArg_ParseTuple(args, "s", &expr)) | 1075 if (!PyArg_ParseTuple(args, "s", &expr)) |
1260 return NULL; | 1076 return NULL; |
1261 | 1077 |
1294 | 1110 |
1295 /* Common routines for buffers and line ranges | 1111 /* Common routines for buffers and line ranges |
1296 * ------------------------------------------- | 1112 * ------------------------------------------- |
1297 */ | 1113 */ |
1298 | 1114 |
1299 static int CheckBuffer(BufferObject *this) | 1115 static int |
1116 CheckBuffer(BufferObject *this) | |
1300 { | 1117 { |
1301 if (this->buf == INVALID_BUFFER_VALUE) | 1118 if (this->buf == INVALID_BUFFER_VALUE) |
1302 { | 1119 { |
1303 PyErr_SetVim(_("attempt to refer to deleted buffer")); | 1120 PyErr_SetVim(_("attempt to refer to deleted buffer")); |
1304 return -1; | 1121 return -1; |
1305 } | 1122 } |
1306 | 1123 |
1307 return 0; | 1124 return 0; |
1308 } | 1125 } |
1309 | 1126 |
1310 static PyObject * RBItem(BufferObject *self, Py_ssize_t n, Py_ssize_t start, Py_ssize_t end) | 1127 static PyObject * |
1128 RBItem(BufferObject *self, Py_ssize_t n, Py_ssize_t start, Py_ssize_t end) | |
1311 { | 1129 { |
1312 if (CheckBuffer(self)) | 1130 if (CheckBuffer(self)) |
1313 return NULL; | 1131 return NULL; |
1314 | 1132 |
1315 if (n < 0 || n > end - start) | 1133 if (n < 0 || n > end - start) |
1319 } | 1137 } |
1320 | 1138 |
1321 return GetBufferLine(self->buf, n+start); | 1139 return GetBufferLine(self->buf, n+start); |
1322 } | 1140 } |
1323 | 1141 |
1324 static Py_ssize_t RBAsItem(BufferObject *self, Py_ssize_t n, PyObject *val, Py_ssize_t start, Py_ssize_t end, Py_ssize_t *new_end) | 1142 static PyObject * |
1325 { | 1143 RBSlice(BufferObject *self, Py_ssize_t lo, Py_ssize_t hi, Py_ssize_t start, Py_ssize_t end) |
1326 Py_ssize_t len_change; | |
1327 | |
1328 if (CheckBuffer(self)) | |
1329 return -1; | |
1330 | |
1331 if (n < 0 || n > end - start) | |
1332 { | |
1333 PyErr_SetString(PyExc_IndexError, _("line number out of range")); | |
1334 return -1; | |
1335 } | |
1336 | |
1337 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) | |
1338 return -1; | |
1339 | |
1340 if (new_end) | |
1341 *new_end = end + len_change; | |
1342 | |
1343 return 0; | |
1344 } | |
1345 | |
1346 static PyObject * RBSlice(BufferObject *self, Py_ssize_t lo, Py_ssize_t hi, Py_ssize_t start, Py_ssize_t end) | |
1347 { | 1144 { |
1348 Py_ssize_t size; | 1145 Py_ssize_t size; |
1349 | 1146 |
1350 if (CheckBuffer(self)) | 1147 if (CheckBuffer(self)) |
1351 return NULL; | 1148 return NULL; |
1364 hi = size; | 1161 hi = size; |
1365 | 1162 |
1366 return GetBufferLineList(self->buf, lo+start, hi+start); | 1163 return GetBufferLineList(self->buf, lo+start, hi+start); |
1367 } | 1164 } |
1368 | 1165 |
1369 static PyObject * RBAppend(BufferObject *self, PyObject *args, Py_ssize_t start, Py_ssize_t end, Py_ssize_t *new_end) | 1166 static Py_ssize_t |
1167 RBAsItem(BufferObject *self, Py_ssize_t n, PyObject *val, Py_ssize_t start, Py_ssize_t end, Py_ssize_t *new_end) | |
1168 { | |
1169 Py_ssize_t len_change; | |
1170 | |
1171 if (CheckBuffer(self)) | |
1172 return -1; | |
1173 | |
1174 if (n < 0 || n > end - start) | |
1175 { | |
1176 PyErr_SetString(PyExc_IndexError, _("line number out of range")); | |
1177 return -1; | |
1178 } | |
1179 | |
1180 if (SetBufferLine(self->buf, n+start, val, &len_change) == FAIL) | |
1181 return -1; | |
1182 | |
1183 if (new_end) | |
1184 *new_end = end + len_change; | |
1185 | |
1186 return 0; | |
1187 } | |
1188 | |
1189 static PyObject * | |
1190 RBAppend(BufferObject *self, PyObject *args, Py_ssize_t start, Py_ssize_t end, Py_ssize_t *new_end) | |
1370 { | 1191 { |
1371 PyObject *lines; | 1192 PyObject *lines; |
1372 Py_ssize_t len_change; | 1193 Py_ssize_t len_change; |
1373 Py_ssize_t max; | 1194 Py_ssize_t max; |
1374 Py_ssize_t n; | 1195 Py_ssize_t n; |
1395 | 1216 |
1396 Py_INCREF(Py_None); | 1217 Py_INCREF(Py_None); |
1397 return Py_None; | 1218 return Py_None; |
1398 } | 1219 } |
1399 | 1220 |
1221 | |
1222 /* Buffer object - Definitions | |
1223 */ | |
1400 | 1224 |
1401 static struct PyMethodDef BufferMethods[] = { | 1225 static struct PyMethodDef BufferMethods[] = { |
1402 /* name, function, calling, documentation */ | 1226 /* name, function, calling, documentation */ |
1403 {"append", BufferAppend, 1, "Append data to Vim buffer" }, | 1227 {"append", BufferAppend, 1, "Append data to Vim buffer" }, |
1404 {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" }, | 1228 {"mark", BufferMark, 1, "Return (row,col) representing position of named mark" }, |
1429 /* Buffer object - Definitions | 1253 /* Buffer object - Definitions |
1430 */ | 1254 */ |
1431 | 1255 |
1432 static PyTypeObject BufferType; | 1256 static PyTypeObject BufferType; |
1433 | 1257 |
1434 static PyObject * BufferNew(buf_T *buf) | 1258 static PyObject * |
1259 BufferNew(buf_T *buf) | |
1435 { | 1260 { |
1436 /* We need to handle deletion of buffers underneath us. | 1261 /* We need to handle deletion of buffers underneath us. |
1437 * If we add a "b_python3_ref" field to the buf_T structure, | 1262 * If we add a "b_python3_ref" field to the buf_T structure, |
1438 * then we can get at it in buf_freeall() in vim. We then | 1263 * then we can get at it in buf_freeall() in vim. We then |
1439 * need to create only ONE Python object per buffer - if | 1264 * need to create only ONE Python object per buffer - if |
1464 } | 1289 } |
1465 | 1290 |
1466 return (PyObject *)(self); | 1291 return (PyObject *)(self); |
1467 } | 1292 } |
1468 | 1293 |
1469 static void BufferDestructor(PyObject *self) | 1294 static void |
1295 BufferDestructor(PyObject *self) | |
1470 { | 1296 { |
1471 BufferObject *this = (BufferObject *)(self); | 1297 BufferObject *this = (BufferObject *)(self); |
1472 | 1298 |
1473 if (this->buf && this->buf != INVALID_BUFFER_VALUE) | 1299 if (this->buf && this->buf != INVALID_BUFFER_VALUE) |
1474 this->buf->b_python3_ref = NULL; | 1300 this->buf->b_python3_ref = NULL; |
1475 } | 1301 } |
1476 | 1302 |
1477 static PyObject * BufferGetattro(PyObject *self, PyObject*nameobj) | 1303 static PyObject * |
1304 BufferGetattro(PyObject *self, PyObject*nameobj) | |
1478 { | 1305 { |
1479 BufferObject *this = (BufferObject *)(self); | 1306 BufferObject *this = (BufferObject *)(self); |
1480 | 1307 |
1481 char *name = ""; | 1308 char *name = ""; |
1482 if (PyUnicode_Check(nameobj)) | 1309 if (PyUnicode_Check(nameobj)) |
1493 return Py_BuildValue("[ss]", "name", "number"); | 1320 return Py_BuildValue("[ss]", "name", "number"); |
1494 else | 1321 else |
1495 return PyObject_GenericGetAttr(self, nameobj); | 1322 return PyObject_GenericGetAttr(self, nameobj); |
1496 } | 1323 } |
1497 | 1324 |
1498 static PyObject * BufferRepr(PyObject *self) | 1325 static PyObject * |
1326 BufferRepr(PyObject *self) | |
1499 { | 1327 { |
1500 static char repr[100]; | 1328 static char repr[100]; |
1501 BufferObject *this = (BufferObject *)(self); | 1329 BufferObject *this = (BufferObject *)(self); |
1502 | 1330 |
1503 if (this->buf == INVALID_BUFFER_VALUE) | 1331 if (this->buf == INVALID_BUFFER_VALUE) |
1523 } | 1351 } |
1524 } | 1352 } |
1525 | 1353 |
1526 /******************/ | 1354 /******************/ |
1527 | 1355 |
1528 static Py_ssize_t BufferLength(PyObject *self) | 1356 static Py_ssize_t |
1357 BufferLength(PyObject *self) | |
1529 { | 1358 { |
1530 if (CheckBuffer((BufferObject *)(self))) | 1359 if (CheckBuffer((BufferObject *)(self))) |
1531 return -1; | 1360 return -1; |
1532 | 1361 |
1533 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count); | 1362 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count); |
1534 } | 1363 } |
1535 | 1364 |
1536 static PyObject * BufferItem(PyObject *self, Py_ssize_t n) | 1365 static PyObject * |
1366 BufferItem(PyObject *self, Py_ssize_t n) | |
1537 { | 1367 { |
1538 return RBItem((BufferObject *)(self), n, 1, | 1368 return RBItem((BufferObject *)(self), n, 1, |
1539 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); | 1369 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); |
1540 } | 1370 } |
1541 | 1371 |
1542 static Py_ssize_t BufferAsItem(PyObject *self, Py_ssize_t n, PyObject *val) | 1372 static PyObject * |
1373 BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi) | |
1374 { | |
1375 return RBSlice((BufferObject *)(self), lo, hi, 1, | |
1376 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); | |
1377 } | |
1378 | |
1379 static Py_ssize_t | |
1380 BufferAsItem(PyObject *self, Py_ssize_t n, PyObject *val) | |
1543 { | 1381 { |
1544 return RBAsItem((BufferObject *)(self), n, val, 1, | 1382 return RBAsItem((BufferObject *)(self), n, val, 1, |
1545 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, | 1383 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
1546 NULL); | 1384 NULL); |
1547 } | 1385 } |
1548 | 1386 |
1549 static PyObject * BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi) | 1387 |
1550 { | 1388 static PyObject * |
1551 return RBSlice((BufferObject *)(self), lo, hi, 1, | 1389 BufferSubscript(PyObject *self, PyObject* idx) |
1552 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); | |
1553 } | |
1554 | |
1555 | |
1556 static PyObject* BufferSubscript(PyObject *self, PyObject* idx) | |
1557 { | 1390 { |
1558 if (PyLong_Check(idx)) { | 1391 if (PyLong_Check(idx)) { |
1559 long _idx = PyLong_AsLong(idx); | 1392 long _idx = PyLong_AsLong(idx); |
1560 return BufferItem(self,_idx); | 1393 return BufferItem(self,_idx); |
1561 } else if (PySlice_Check(idx)) { | 1394 } else if (PySlice_Check(idx)) { |
1572 PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); | 1405 PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); |
1573 return NULL; | 1406 return NULL; |
1574 } | 1407 } |
1575 } | 1408 } |
1576 | 1409 |
1577 static PyObject * BufferAppend(PyObject *self, PyObject *args) | 1410 static PyObject * |
1411 BufferAppend(PyObject *self, PyObject *args) | |
1578 { | 1412 { |
1579 return RBAppend((BufferObject *)(self), args, 1, | 1413 return RBAppend((BufferObject *)(self), args, 1, |
1580 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, | 1414 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
1581 NULL); | 1415 NULL); |
1582 } | 1416 } |
1583 | 1417 |
1584 static PyObject * BufferMark(PyObject *self, PyObject *args) | 1418 static PyObject * |
1419 BufferMark(PyObject *self, PyObject *args) | |
1585 { | 1420 { |
1586 pos_T *posp; | 1421 pos_T *posp; |
1587 char *pmark;//test | 1422 char *pmark;//test |
1588 char mark; | 1423 char mark; |
1589 buf_T *curbuf_save; | 1424 buf_T *curbuf_save; |
1618 } | 1453 } |
1619 | 1454 |
1620 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col)); | 1455 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col)); |
1621 } | 1456 } |
1622 | 1457 |
1623 static PyObject * BufferRange(PyObject *self, PyObject *args) | 1458 static PyObject * |
1459 BufferRange(PyObject *self, PyObject *args) | |
1624 { | 1460 { |
1625 Py_ssize_t start; | 1461 Py_ssize_t start; |
1626 Py_ssize_t end; | 1462 Py_ssize_t end; |
1627 | 1463 |
1628 if (CheckBuffer((BufferObject *)(self))) | 1464 if (CheckBuffer((BufferObject *)(self))) |
1665 static PyTypeObject RangeType; | 1501 static PyTypeObject RangeType; |
1666 | 1502 |
1667 /* Line range object - Implementation | 1503 /* Line range object - Implementation |
1668 */ | 1504 */ |
1669 | 1505 |
1670 static PyObject * RangeNew(buf_T *buf, Py_ssize_t start, Py_ssize_t end) | 1506 static PyObject * |
1507 RangeNew(buf_T *buf, Py_ssize_t start, Py_ssize_t end) | |
1671 { | 1508 { |
1672 BufferObject *bufr; | 1509 BufferObject *bufr; |
1673 RangeObject *self; | 1510 RangeObject *self; |
1674 self = PyObject_NEW(RangeObject, &RangeType); | 1511 self = PyObject_NEW(RangeObject, &RangeType); |
1675 if (self == NULL) | 1512 if (self == NULL) |
1688 self->end = end; | 1525 self->end = end; |
1689 | 1526 |
1690 return (PyObject *)(self); | 1527 return (PyObject *)(self); |
1691 } | 1528 } |
1692 | 1529 |
1693 static void RangeDestructor(PyObject *self) | 1530 static void |
1531 RangeDestructor(PyObject *self) | |
1694 { | 1532 { |
1695 Py_DECREF(((RangeObject *)(self))->buf); | 1533 Py_DECREF(((RangeObject *)(self))->buf); |
1696 } | 1534 } |
1697 | 1535 |
1698 static PyObject * RangeGetattro(PyObject *self, PyObject *nameobj) | 1536 static PyObject * |
1537 RangeGetattro(PyObject *self, PyObject *nameobj) | |
1699 { | 1538 { |
1700 char *name = ""; | 1539 char *name = ""; |
1701 if (PyUnicode_Check(nameobj)) | 1540 if (PyUnicode_Check(nameobj)) |
1702 name = _PyUnicode_AsString(nameobj); | 1541 name = _PyUnicode_AsString(nameobj); |
1703 | 1542 |
1707 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1); | 1546 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1); |
1708 else | 1547 else |
1709 return PyObject_GenericGetAttr(self, nameobj); | 1548 return PyObject_GenericGetAttr(self, nameobj); |
1710 } | 1549 } |
1711 | 1550 |
1712 static PyObject * RangeRepr(PyObject *self) | 1551 static PyObject * |
1552 RangeRepr(PyObject *self) | |
1713 { | 1553 { |
1714 static char repr[100]; | 1554 static char repr[100]; |
1715 RangeObject *this = (RangeObject *)(self); | 1555 RangeObject *this = (RangeObject *)(self); |
1716 | 1556 |
1717 if (this->buf->buf == INVALID_BUFFER_VALUE) | 1557 if (this->buf->buf == INVALID_BUFFER_VALUE) |
1740 } | 1580 } |
1741 } | 1581 } |
1742 | 1582 |
1743 /****************/ | 1583 /****************/ |
1744 | 1584 |
1745 static Py_ssize_t RangeLength(PyObject *self) | 1585 static Py_ssize_t |
1586 RangeLength(PyObject *self) | |
1746 { | 1587 { |
1747 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | 1588 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
1748 if (CheckBuffer(((RangeObject *)(self))->buf)) | 1589 if (CheckBuffer(((RangeObject *)(self))->buf)) |
1749 return -1; /* ??? */ | 1590 return -1; /* ??? */ |
1750 | 1591 |
1751 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1); | 1592 return (((RangeObject *)(self))->end - ((RangeObject *)(self))->start + 1); |
1752 } | 1593 } |
1753 | 1594 |
1754 static PyObject * RangeItem(PyObject *self, Py_ssize_t n) | 1595 static PyObject * |
1596 RangeItem(PyObject *self, Py_ssize_t n) | |
1755 { | 1597 { |
1756 return RBItem(((RangeObject *)(self))->buf, n, | 1598 return RBItem(((RangeObject *)(self))->buf, n, |
1757 ((RangeObject *)(self))->start, | 1599 ((RangeObject *)(self))->start, |
1758 ((RangeObject *)(self))->end); | 1600 ((RangeObject *)(self))->end); |
1759 } | 1601 } |
1760 | 1602 |
1761 static Py_ssize_t RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val) | 1603 static Py_ssize_t |
1604 RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val) | |
1762 { | 1605 { |
1763 return RBAsItem(((RangeObject *)(self))->buf, n, val, | 1606 return RBAsItem(((RangeObject *)(self))->buf, n, val, |
1764 ((RangeObject *)(self))->start, | 1607 ((RangeObject *)(self))->start, |
1765 ((RangeObject *)(self))->end, | 1608 ((RangeObject *)(self))->end, |
1766 &((RangeObject *)(self))->end); | 1609 &((RangeObject *)(self))->end); |
1767 } | 1610 } |
1768 | 1611 |
1769 static PyObject * RangeSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi) | 1612 static PyObject * |
1613 RangeSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi) | |
1770 { | 1614 { |
1771 return RBSlice(((RangeObject *)(self))->buf, lo, hi, | 1615 return RBSlice(((RangeObject *)(self))->buf, lo, hi, |
1772 ((RangeObject *)(self))->start, | 1616 ((RangeObject *)(self))->start, |
1773 ((RangeObject *)(self))->end); | 1617 ((RangeObject *)(self))->end); |
1774 } | 1618 } |
1775 | 1619 |
1776 static PyObject* RangeSubscript(PyObject *self, PyObject* idx) | 1620 static PyObject * |
1621 RangeSubscript(PyObject *self, PyObject* idx) | |
1777 { | 1622 { |
1778 if (PyLong_Check(idx)) { | 1623 if (PyLong_Check(idx)) { |
1779 long _idx = PyLong_AsLong(idx); | 1624 long _idx = PyLong_AsLong(idx); |
1780 return RangeItem(self,_idx); | 1625 return RangeItem(self,_idx); |
1781 } else if (PySlice_Check(idx)) { | 1626 } else if (PySlice_Check(idx)) { |
1792 PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); | 1637 PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); |
1793 return NULL; | 1638 return NULL; |
1794 } | 1639 } |
1795 } | 1640 } |
1796 | 1641 |
1797 static PyObject * RangeAppend(PyObject *self, PyObject *args) | 1642 static PyObject * |
1643 RangeAppend(PyObject *self, PyObject *args) | |
1798 { | 1644 { |
1799 return RBAppend(((RangeObject *)(self))->buf, args, | 1645 return RBAppend(((RangeObject *)(self))->buf, args, |
1800 ((RangeObject *)(self))->start, | 1646 ((RangeObject *)(self))->start, |
1801 ((RangeObject *)(self))->end, | 1647 ((RangeObject *)(self))->end, |
1802 &((RangeObject *)(self))->end); | 1648 &((RangeObject *)(self))->end); |
1827 static PyTypeObject BufListType; | 1673 static PyTypeObject BufListType; |
1828 | 1674 |
1829 /* Buffer list object - Implementation | 1675 /* Buffer list object - Implementation |
1830 */ | 1676 */ |
1831 | 1677 |
1832 /*ARGSUSED*/ | 1678 static Py_ssize_t |
1833 static Py_ssize_t BufListLength(PyObject *self UNUSED) | 1679 BufListLength(PyObject *self UNUSED) |
1834 { | 1680 { |
1835 buf_T *b = firstbuf; | 1681 buf_T *b = firstbuf; |
1836 Py_ssize_t n = 0; | 1682 Py_ssize_t n = 0; |
1837 | 1683 |
1838 while (b) | 1684 while (b) |
1842 } | 1688 } |
1843 | 1689 |
1844 return n; | 1690 return n; |
1845 } | 1691 } |
1846 | 1692 |
1847 /*ARGSUSED*/ | 1693 static PyObject * |
1848 static PyObject * BufListItem(PyObject *self UNUSED, Py_ssize_t n) | 1694 BufListItem(PyObject *self UNUSED, Py_ssize_t n) |
1849 { | 1695 { |
1850 buf_T *b; | 1696 buf_T *b; |
1851 | 1697 |
1852 for (b = firstbuf; b; b = b->b_next, --n) | 1698 for (b = firstbuf; b; b = b->b_next, --n) |
1853 { | 1699 { |
1870 static PyTypeObject WindowType; | 1716 static PyTypeObject WindowType; |
1871 | 1717 |
1872 /* Window object - Implementation | 1718 /* Window object - Implementation |
1873 */ | 1719 */ |
1874 | 1720 |
1875 static PyObject * WindowNew(win_T *win) | 1721 static PyObject * |
1722 WindowNew(win_T *win) | |
1876 { | 1723 { |
1877 /* We need to handle deletion of windows underneath us. | 1724 /* We need to handle deletion of windows underneath us. |
1878 * If we add a "w_python3_ref" field to the win_T structure, | 1725 * If we add a "w_python3_ref" field to the win_T structure, |
1879 * then we can get at it in win_free() in vim. We then | 1726 * then we can get at it in win_free() in vim. We then |
1880 * need to create only ONE Python object per window - if | 1727 * need to create only ONE Python object per window - if |
1903 } | 1750 } |
1904 | 1751 |
1905 return (PyObject *)(self); | 1752 return (PyObject *)(self); |
1906 } | 1753 } |
1907 | 1754 |
1908 static void WindowDestructor(PyObject *self) | 1755 static void |
1756 WindowDestructor(PyObject *self) | |
1909 { | 1757 { |
1910 WindowObject *this = (WindowObject *)(self); | 1758 WindowObject *this = (WindowObject *)(self); |
1911 | 1759 |
1912 if (this->win && this->win != INVALID_WINDOW_VALUE) | 1760 if (this->win && this->win != INVALID_WINDOW_VALUE) |
1913 this->win->w_python3_ref = NULL; | 1761 this->win->w_python3_ref = NULL; |
1914 } | 1762 } |
1915 | 1763 |
1916 static int CheckWindow(WindowObject *this) | 1764 static int |
1765 CheckWindow(WindowObject *this) | |
1917 { | 1766 { |
1918 if (this->win == INVALID_WINDOW_VALUE) | 1767 if (this->win == INVALID_WINDOW_VALUE) |
1919 { | 1768 { |
1920 PyErr_SetVim(_("attempt to refer to deleted window")); | 1769 PyErr_SetVim(_("attempt to refer to deleted window")); |
1921 return -1; | 1770 return -1; |
1922 } | 1771 } |
1923 | 1772 |
1924 return 0; | 1773 return 0; |
1925 } | 1774 } |
1926 | 1775 |
1927 static PyObject * WindowGetattro(PyObject *self, PyObject *nameobj) | 1776 static PyObject * |
1777 WindowGetattro(PyObject *self, PyObject *nameobj) | |
1928 { | 1778 { |
1929 WindowObject *this = (WindowObject *)(self); | 1779 WindowObject *this = (WindowObject *)(self); |
1930 | 1780 |
1931 char *name = ""; | 1781 char *name = ""; |
1932 if (PyUnicode_Check(nameobj)) | 1782 if (PyUnicode_Check(nameobj)) |
1954 return Py_BuildValue("[sss]", "buffer", "cursor", "height"); | 1804 return Py_BuildValue("[sss]", "buffer", "cursor", "height"); |
1955 else | 1805 else |
1956 return PyObject_GenericGetAttr(self, nameobj); | 1806 return PyObject_GenericGetAttr(self, nameobj); |
1957 } | 1807 } |
1958 | 1808 |
1959 static int WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val) | 1809 static int |
1810 WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val) | |
1960 { | 1811 { |
1961 WindowObject *this = (WindowObject *)(self); | 1812 WindowObject *this = (WindowObject *)(self); |
1962 | 1813 |
1963 char *name = ""; | 1814 char *name = ""; |
1964 if (PyUnicode_Check(nameobj)) | 1815 if (PyUnicode_Check(nameobj)) |
1999 | 1850 |
2000 return 0; | 1851 return 0; |
2001 } | 1852 } |
2002 else if (strcmp(name, "height") == 0) | 1853 else if (strcmp(name, "height") == 0) |
2003 { | 1854 { |
2004 int height; | 1855 int height; |
2005 win_T *savewin; | 1856 win_T *savewin; |
2006 | 1857 |
2007 if (!PyArg_Parse(val, "i", &height)) | 1858 if (!PyArg_Parse(val, "i", &height)) |
2008 return -1; | 1859 return -1; |
2009 | 1860 |
2010 #ifdef FEAT_GUI | 1861 #ifdef FEAT_GUI |
2022 return 0; | 1873 return 0; |
2023 } | 1874 } |
2024 #ifdef FEAT_VERTSPLIT | 1875 #ifdef FEAT_VERTSPLIT |
2025 else if (strcmp(name, "width") == 0) | 1876 else if (strcmp(name, "width") == 0) |
2026 { | 1877 { |
2027 int width; | 1878 int width; |
2028 win_T *savewin; | 1879 win_T *savewin; |
2029 | 1880 |
2030 if (!PyArg_Parse(val, "i", &width)) | 1881 if (!PyArg_Parse(val, "i", &width)) |
2031 return -1; | 1882 return -1; |
2032 | 1883 |
2033 #ifdef FEAT_GUI | 1884 #ifdef FEAT_GUI |
2050 PyErr_SetString(PyExc_AttributeError, name); | 1901 PyErr_SetString(PyExc_AttributeError, name); |
2051 return -1; | 1902 return -1; |
2052 } | 1903 } |
2053 } | 1904 } |
2054 | 1905 |
2055 static PyObject * WindowRepr(PyObject *self) | 1906 static PyObject * |
1907 WindowRepr(PyObject *self) | |
2056 { | 1908 { |
2057 static char repr[100]; | 1909 static char repr[100]; |
2058 WindowObject *this = (WindowObject *)(self); | 1910 WindowObject *this = (WindowObject *)(self); |
2059 | 1911 |
2060 if (this->win == INVALID_WINDOW_VALUE) | 1912 if (this->win == INVALID_WINDOW_VALUE) |
2062 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self)); | 1914 vim_snprintf(repr, 100, _("<window object (deleted) at %p>"), (self)); |
2063 return PyUnicode_FromString(repr); | 1915 return PyUnicode_FromString(repr); |
2064 } | 1916 } |
2065 else | 1917 else |
2066 { | 1918 { |
2067 int i = 0; | 1919 int i = 0; |
2068 win_T *w; | 1920 win_T *w; |
2069 | 1921 |
2070 for (w = firstwin; w != NULL && w != this->win; w = W_NEXT(w)) | 1922 for (w = firstwin; w != NULL && w != this->win; w = W_NEXT(w)) |
2071 ++i; | 1923 ++i; |
2072 | 1924 |
2073 if (w == NULL) | 1925 if (w == NULL) |
2104 | 1956 |
2105 static PyTypeObject WinListType; | 1957 static PyTypeObject WinListType; |
2106 | 1958 |
2107 /* Window list object - Implementation | 1959 /* Window list object - Implementation |
2108 */ | 1960 */ |
2109 /*ARGSUSED*/ | 1961 static Py_ssize_t |
2110 static Py_ssize_t WinListLength(PyObject *self UNUSED) | 1962 WinListLength(PyObject *self UNUSED) |
2111 { | 1963 { |
2112 win_T *w = firstwin; | 1964 win_T *w = firstwin; |
2113 Py_ssize_t n = 0; | 1965 Py_ssize_t n = 0; |
2114 | 1966 |
2115 while (w != NULL) | 1967 while (w != NULL) |
2119 } | 1971 } |
2120 | 1972 |
2121 return n; | 1973 return n; |
2122 } | 1974 } |
2123 | 1975 |
2124 /*ARGSUSED*/ | 1976 static PyObject * |
2125 static PyObject * WinListItem(PyObject *self UNUSED, Py_ssize_t n) | 1977 WinListItem(PyObject *self UNUSED, Py_ssize_t n) |
2126 { | 1978 { |
2127 win_T *w; | 1979 win_T *w; |
2128 | 1980 |
2129 for (w = firstwin; w != NULL; w = W_NEXT(w), --n) | 1981 for (w = firstwin; w != NULL; w = W_NEXT(w), --n) |
2130 if (n == 0) | 1982 if (n == 0) |
2145 | 1997 |
2146 static PyTypeObject CurrentType; | 1998 static PyTypeObject CurrentType; |
2147 | 1999 |
2148 /* Current items object - Implementation | 2000 /* Current items object - Implementation |
2149 */ | 2001 */ |
2150 /*ARGSUSED*/ | 2002 static PyObject * |
2151 static PyObject * CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj) | 2003 CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj) |
2152 { | 2004 { |
2153 char *name = ""; | 2005 char *name = ""; |
2154 if (PyUnicode_Check(nameobj)) | 2006 if (PyUnicode_Check(nameobj)) |
2155 name = _PyUnicode_AsString(nameobj); | 2007 name = _PyUnicode_AsString(nameobj); |
2156 | 2008 |
2169 PyErr_SetString(PyExc_AttributeError, name); | 2021 PyErr_SetString(PyExc_AttributeError, name); |
2170 return NULL; | 2022 return NULL; |
2171 } | 2023 } |
2172 } | 2024 } |
2173 | 2025 |
2174 /*ARGSUSED*/ | 2026 static int |
2175 static int CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value) | 2027 CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value) |
2176 { | 2028 { |
2177 char *name = ""; | 2029 char *name = ""; |
2178 if (PyUnicode_Check(nameobj)) | 2030 if (PyUnicode_Check(nameobj)) |
2179 name = _PyUnicode_AsString(nameobj); | 2031 name = _PyUnicode_AsString(nameobj); |
2180 | 2032 |
2276 | 2128 |
2277 /************************************************************************* | 2129 /************************************************************************* |
2278 * 4. Utility functions for handling the interface between Vim and Python. | 2130 * 4. Utility functions for handling the interface between Vim and Python. |
2279 */ | 2131 */ |
2280 | 2132 |
2133 /* Get a line from the specified buffer. The line number is | |
2134 * in Vim format (1-based). The line is returned as a Python | |
2135 * string object. | |
2136 */ | |
2137 static PyObject * | |
2138 GetBufferLine(buf_T *buf, Py_ssize_t n) | |
2139 { | |
2140 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE)); | |
2141 } | |
2142 | |
2281 | 2143 |
2282 /* Get a list of lines from the specified buffer. The line numbers | 2144 /* Get a list of lines from the specified buffer. The line numbers |
2283 * are in Vim format (1-based). The range is from lo up to, but not | 2145 * are in Vim format (1-based). The range is from lo up to, but not |
2284 * including, hi. The list is returned as a Python list of string objects. | 2146 * including, hi. The list is returned as a Python list of string objects. |
2285 */ | 2147 */ |
2286 static PyObject * GetBufferLineList(buf_T *buf, Py_ssize_t lo, Py_ssize_t hi) | 2148 static PyObject * |
2149 GetBufferLineList(buf_T *buf, Py_ssize_t lo, Py_ssize_t hi) | |
2287 { | 2150 { |
2288 Py_ssize_t i; | 2151 Py_ssize_t i; |
2289 Py_ssize_t n = hi - lo; | 2152 Py_ssize_t n = hi - lo; |
2290 PyObject *list = PyList_New(n); | 2153 PyObject *list = PyList_New(n); |
2291 | 2154 |
2318 */ | 2181 */ |
2319 | 2182 |
2320 return list; | 2183 return list; |
2321 } | 2184 } |
2322 | 2185 |
2323 /* Get a line from the specified buffer. The line number is | |
2324 * in Vim format (1-based). The line is returned as a Python | |
2325 * string object. | |
2326 */ | |
2327 static PyObject * GetBufferLine(buf_T *buf, Py_ssize_t n) | |
2328 { | |
2329 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE)); | |
2330 } | |
2331 | |
2332 /* | 2186 /* |
2333 * Check if deleting lines made the cursor position invalid. | 2187 * Check if deleting lines made the cursor position invalid. |
2334 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if | 2188 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if |
2335 * deleted). | 2189 * deleted). |
2336 */ | 2190 */ |
2337 static void py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) | 2191 static void |
2192 py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) | |
2338 { | 2193 { |
2339 if (curwin->w_cursor.lnum >= lo) | 2194 if (curwin->w_cursor.lnum >= lo) |
2340 { | 2195 { |
2341 /* Adjust the cursor position if it's in/after the changed | 2196 /* Adjust the cursor position if it's in/after the changed |
2342 * lines. */ | 2197 * lines. */ |
2363 * and correct format. Errors are returned as a value of FAIL. | 2218 * and correct format. Errors are returned as a value of FAIL. |
2364 * The return value is OK on success. | 2219 * The return value is OK on success. |
2365 * If OK is returned and len_change is not NULL, *len_change | 2220 * If OK is returned and len_change is not NULL, *len_change |
2366 * is set to the change in the buffer length. | 2221 * is set to the change in the buffer length. |
2367 */ | 2222 */ |
2368 static int SetBufferLine(buf_T *buf, Py_ssize_t n, PyObject *line, Py_ssize_t *len_change) | 2223 static int |
2224 SetBufferLine(buf_T *buf, Py_ssize_t n, PyObject *line, Py_ssize_t *len_change) | |
2369 { | 2225 { |
2370 /* First of all, we check the thpe of the supplied Python object. | 2226 /* First of all, we check the thpe of the supplied Python object. |
2371 * There are three cases: | 2227 * There are three cases: |
2372 * 1. NULL, or None - this is a deletion. | 2228 * 1. NULL, or None - this is a deletion. |
2373 * 2. A string - this is a replacement. | 2229 * 2. A string - this is a replacement. |
2453 * to be added are checked for validity and correct format. Errors are | 2309 * to be added are checked for validity and correct format. Errors are |
2454 * returned as a value of FAIL. The return value is OK on success. | 2310 * returned as a value of FAIL. The return value is OK on success. |
2455 * If OK is returned and len_change is not NULL, *len_change | 2311 * If OK is returned and len_change is not NULL, *len_change |
2456 * is set to the change in the buffer length. | 2312 * is set to the change in the buffer length. |
2457 */ | 2313 */ |
2458 static int InsertBufferLines(buf_T *buf, Py_ssize_t n, PyObject *lines, Py_ssize_t *len_change) | 2314 static int |
2315 InsertBufferLines(buf_T *buf, Py_ssize_t n, PyObject *lines, Py_ssize_t *len_change) | |
2459 { | 2316 { |
2460 /* First of all, we check the type of the supplied Python object. | 2317 /* First of all, we check the type of the supplied Python object. |
2461 * It must be a string or a list, or the call is in error. | 2318 * It must be a string or a list, or the call is in error. |
2462 */ | 2319 */ |
2463 if (PyUnicode_Check(lines)) | 2320 if (PyUnicode_Check(lines)) |
2574 /* Convert a Vim line into a Python string. | 2431 /* Convert a Vim line into a Python string. |
2575 * All internal newlines are replaced by null characters. | 2432 * All internal newlines are replaced by null characters. |
2576 * | 2433 * |
2577 * On errors, the Python exception data is set, and NULL is returned. | 2434 * On errors, the Python exception data is set, and NULL is returned. |
2578 */ | 2435 */ |
2579 static PyObject * LineToString(const char *str) | 2436 static PyObject * |
2437 LineToString(const char *str) | |
2580 { | 2438 { |
2581 PyObject *result; | 2439 PyObject *result; |
2582 Py_ssize_t len = strlen(str); | 2440 Py_ssize_t len = strlen(str); |
2583 char *tmp,*p; | 2441 char *tmp,*p; |
2584 | 2442 |
2614 * newline characters. It is an error for the string to contain newline | 2472 * newline characters. It is an error for the string to contain newline |
2615 * characters. | 2473 * characters. |
2616 * | 2474 * |
2617 * On errors, the Python exception data is set, and NULL is returned. | 2475 * On errors, the Python exception data is set, and NULL is returned. |
2618 */ | 2476 */ |
2619 static char * StringToLine(PyObject *obj) | 2477 static char * |
2478 StringToLine(PyObject *obj) | |
2620 { | 2479 { |
2621 const char *str; | 2480 const char *str; |
2622 char *save; | 2481 char *save; |
2623 Py_ssize_t len; | 2482 Py_ssize_t len; |
2624 Py_ssize_t i; | 2483 Py_ssize_t i; |
2672 save[i] = '\0'; | 2531 save[i] = '\0'; |
2673 | 2532 |
2674 return save; | 2533 return save; |
2675 } | 2534 } |
2676 | 2535 |
2677 /* Check to see whether a Vim error has been reported, or a keyboard | 2536 static void |
2678 * interrupt has been detected. | 2537 init_structs(void) |
2679 */ | |
2680 static int VimErrorCheck(void) | |
2681 { | |
2682 if (got_int) | |
2683 { | |
2684 PyErr_SetNone(PyExc_KeyboardInterrupt); | |
2685 return 1; | |
2686 } | |
2687 else if (did_emsg && !PyErr_Occurred()) | |
2688 { | |
2689 PyErr_SetNone(VimError); | |
2690 return 1; | |
2691 } | |
2692 | |
2693 return 0; | |
2694 } | |
2695 | |
2696 static void init_structs(void) | |
2697 { | 2538 { |
2698 vim_memset(&OutputType, 0, sizeof(OutputType)); | 2539 vim_memset(&OutputType, 0, sizeof(OutputType)); |
2699 OutputType.tp_name = "vim.message"; | 2540 OutputType.tp_name = "vim.message"; |
2700 OutputType.tp_basicsize = sizeof(OutputObject); | 2541 OutputType.tp_basicsize = sizeof(OutputObject); |
2701 OutputType.tp_getattro = OutputGetattro; | 2542 OutputType.tp_getattro = OutputGetattro; |