comparison src/if_py_both.h @ 4431:7d81f4e96728 v7.3.964

updated for version 7.3.964 Problem: Python: not so easy to access tab pages. Solution: Add window.tabpage, make window.number work with non-current tab pages. (ZyX)
author Bram Moolenaar <bram@vim.org>
date Fri, 17 May 2013 16:18:33 +0200
parents 7eafa576528e
children 8a3ca4adb5d8
comparison
equal deleted inserted replaced
4430:416fb5d57958 4431:7d81f4e96728
29 #define INVALID_WINDOW_VALUE ((win_T *)(-1)) 29 #define INVALID_WINDOW_VALUE ((win_T *)(-1))
30 #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1)) 30 #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
31 31
32 static int ConvertFromPyObject(PyObject *, typval_T *); 32 static int ConvertFromPyObject(PyObject *, typval_T *);
33 static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *); 33 static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
34 static PyObject *WindowNew(win_T *, tabpage_T *);
35 static PyObject *BufferNew (buf_T *);
36 static PyObject *LineToString(const char *);
34 37
35 static PyInt RangeStart; 38 static PyInt RangeStart;
36 static PyInt RangeEnd; 39 static PyInt RangeEnd;
37 40
38 /* 41 /*
1668 else if (strcmp(name, "window") == 0) 1671 else if (strcmp(name, "window") == 0)
1669 { 1672 {
1670 /* For current tab window.c does not bother to set or update tp_curwin 1673 /* For current tab window.c does not bother to set or update tp_curwin
1671 */ 1674 */
1672 if (this->tab == curtab) 1675 if (this->tab == curtab)
1673 return WindowNew(curwin); 1676 return WindowNew(curwin, curtab);
1674 else 1677 else
1675 return WindowNew(this->tab->tp_curwin); 1678 return WindowNew(this->tab->tp_curwin, this->tab);
1676 } 1679 }
1677 return NULL; 1680 return NULL;
1678 } 1681 }
1679 1682
1680 static PyObject * 1683 static PyObject *
1752 1755
1753 typedef struct 1756 typedef struct
1754 { 1757 {
1755 PyObject_HEAD 1758 PyObject_HEAD
1756 win_T *win; 1759 win_T *win;
1760 TabPageObject *tabObject;
1757 } WindowObject; 1761 } WindowObject;
1758 1762
1759 static PyTypeObject WindowType; 1763 static PyTypeObject WindowType;
1760 1764
1761 static int 1765 static int
1769 1773
1770 return 0; 1774 return 0;
1771 } 1775 }
1772 1776
1773 static PyObject * 1777 static PyObject *
1774 WindowNew(win_T *win) 1778 WindowNew(win_T *win, tabpage_T *tab)
1775 { 1779 {
1776 /* We need to handle deletion of windows underneath us. 1780 /* We need to handle deletion of windows underneath us.
1777 * If we add a "w_python*_ref" field to the win_T structure, 1781 * If we add a "w_python*_ref" field to the win_T structure,
1778 * then we can get at it in win_free() in vim. We then 1782 * then we can get at it in win_free() in vim. We then
1779 * need to create only ONE Python object per window - if 1783 * need to create only ONE Python object per window - if
1802 return NULL; 1806 return NULL;
1803 self->win = win; 1807 self->win = win;
1804 WIN_PYTHON_REF(win) = self; 1808 WIN_PYTHON_REF(win) = self;
1805 } 1809 }
1806 1810
1811 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
1812
1807 return (PyObject *)(self); 1813 return (PyObject *)(self);
1808 } 1814 }
1809 1815
1810 static void 1816 static void
1811 WindowDestructor(PyObject *self) 1817 WindowDestructor(PyObject *self)
1813 WindowObject *this = (WindowObject *)(self); 1819 WindowObject *this = (WindowObject *)(self);
1814 1820
1815 if (this->win && this->win != INVALID_WINDOW_VALUE) 1821 if (this->win && this->win != INVALID_WINDOW_VALUE)
1816 WIN_PYTHON_REF(this->win) = NULL; 1822 WIN_PYTHON_REF(this->win) = NULL;
1817 1823
1824 Py_DECREF(((PyObject *)(this->tabObject)));
1825
1818 DESTRUCTOR_FINISH(self); 1826 DESTRUCTOR_FINISH(self);
1827 }
1828
1829 static win_T *
1830 get_firstwin(TabPageObject *tabObject)
1831 {
1832 if (tabObject)
1833 {
1834 if (CheckTabPage(tabObject))
1835 return NULL;
1836 /* For current tab window.c does not bother to set or update tp_firstwin
1837 */
1838 else if (tabObject->tab == curtab)
1839 return firstwin;
1840 else
1841 return tabObject->tab->tp_firstwin;
1842 }
1843 else
1844 return firstwin;
1819 } 1845 }
1820 1846
1821 static PyObject * 1847 static PyObject *
1822 WindowAttr(WindowObject *this, char *name) 1848 WindowAttr(WindowObject *this, char *name)
1823 { 1849 {
1845 return DictionaryNew(this->win->w_vars); 1871 return DictionaryNew(this->win->w_vars);
1846 else if (strcmp(name, "options") == 0) 1872 else if (strcmp(name, "options") == 0)
1847 return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow, 1873 return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow,
1848 (PyObject *) this); 1874 (PyObject *) this);
1849 else if (strcmp(name, "number") == 0) 1875 else if (strcmp(name, "number") == 0)
1850 return PyLong_FromLong((long) get_win_number(this->win, firstwin)); 1876 {
1877 if (CheckTabPage(this->tabObject))
1878 return NULL;
1879 return PyLong_FromLong((long)
1880 get_win_number(this->win, get_firstwin(this->tabObject)));
1881 }
1882 else if (strcmp(name, "tabpage") == 0)
1883 {
1884 Py_INCREF(this->tabObject);
1885 return (PyObject *)(this->tabObject);
1886 }
1851 else if (strcmp(name,"__members__") == 0) 1887 else if (strcmp(name,"__members__") == 0)
1852 return Py_BuildValue("[ssssssss]", "buffer", "cursor", "height", "vars", 1888 return Py_BuildValue("[sssssssss]", "buffer", "cursor", "height",
1853 "options", "number", "row", "col"); 1889 "vars", "options", "number", "row", "col", "tabpage");
1854 else 1890 else
1855 return NULL; 1891 return NULL;
1856 } 1892 }
1857 1893
1858 static int 1894 static int
2014 Py_DECREF((PyObject *)(tabObject)); 2050 Py_DECREF((PyObject *)(tabObject));
2015 2051
2016 DESTRUCTOR_FINISH(self); 2052 DESTRUCTOR_FINISH(self);
2017 } 2053 }
2018 2054
2019 static win_T *
2020 get_firstwin(WinListObject *this)
2021 {
2022 if (this->tabObject)
2023 {
2024 if (CheckTabPage(this->tabObject))
2025 return NULL;
2026 /* For current tab window.c does not bother to set or update tp_firstwin
2027 */
2028 else if (this->tabObject->tab == curtab)
2029 return firstwin;
2030 else
2031 return this->tabObject->tab->tp_firstwin;
2032 }
2033 else
2034 return firstwin;
2035 }
2036
2037 static PyInt 2055 static PyInt
2038 WinListLength(PyObject *self) 2056 WinListLength(PyObject *self)
2039 { 2057 {
2040 win_T *w; 2058 win_T *w;
2041 PyInt n = 0; 2059 PyInt n = 0;
2042 2060
2043 if (!(w = get_firstwin((WinListObject *)(self)))) 2061 if (!(w = get_firstwin(((WinListObject *)(self))->tabObject)))
2044 return -1; 2062 return -1;
2045 2063
2046 while (w != NULL) 2064 while (w != NULL)
2047 { 2065 {
2048 ++n; 2066 ++n;
2053 } 2071 }
2054 2072
2055 static PyObject * 2073 static PyObject *
2056 WinListItem(PyObject *self, PyInt n) 2074 WinListItem(PyObject *self, PyInt n)
2057 { 2075 {
2076 WinListObject *this = ((WinListObject *)(self));
2058 win_T *w; 2077 win_T *w;
2059 2078
2060 if (!(w = get_firstwin((WinListObject *)(self)))) 2079 if (!(w = get_firstwin(this->tabObject)))
2061 return NULL; 2080 return NULL;
2062 2081
2063 for (; w != NULL; w = W_NEXT(w), --n) 2082 for (; w != NULL; w = W_NEXT(w), --n)
2064 if (n == 0) 2083 if (n == 0)
2065 return WindowNew(w); 2084 return WindowNew(w, this->tabObject? this->tabObject->tab: curtab);
2066 2085
2067 PyErr_SetString(PyExc_IndexError, _("no such window")); 2086 PyErr_SetString(PyExc_IndexError, _("no such window"));
2068 return NULL; 2087 return NULL;
2069 } 2088 }
2070 2089
3225 CurrentGetattr(PyObject *self UNUSED, char *name) 3244 CurrentGetattr(PyObject *self UNUSED, char *name)
3226 { 3245 {
3227 if (strcmp(name, "buffer") == 0) 3246 if (strcmp(name, "buffer") == 0)
3228 return (PyObject *)BufferNew(curbuf); 3247 return (PyObject *)BufferNew(curbuf);
3229 else if (strcmp(name, "window") == 0) 3248 else if (strcmp(name, "window") == 0)
3230 return (PyObject *)WindowNew(curwin); 3249 return (PyObject *)WindowNew(curwin, curtab);
3231 else if (strcmp(name, "tabpage") == 0) 3250 else if (strcmp(name, "tabpage") == 0)
3232 return (PyObject *)TabPageNew(curtab); 3251 return (PyObject *)TabPageNew(curtab);
3233 else if (strcmp(name, "line") == 0) 3252 else if (strcmp(name, "line") == 0)
3234 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); 3253 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
3235 else if (strcmp(name, "range") == 0) 3254 else if (strcmp(name, "range") == 0)