Mercurial > vim
diff src/if_py_both.h @ 4319:b79f3c3a584c v7.3.909
updated for version 7.3.909
Problem: Duplicate Python code.
Solution: Move more items to if_py_both.h. (ZyX) Also avoid compiler
warnings for missing initializers.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 24 Apr 2013 13:39:15 +0200 |
parents | 4281875776fb |
children | d8d9c591c50f |
line wrap: on
line diff
--- a/src/if_py_both.h +++ b/src/if_py_both.h @@ -542,6 +542,14 @@ typedef struct * Buffer list object - Implementation */ +typedef struct +{ + PyObject_HEAD +} BufListObject; + +static PyTypeObject BufListType; +static PySequenceMethods WinListAsSeq; + static PyInt BufListLength(PyObject *self UNUSED) { @@ -578,6 +586,11 @@ typedef struct win_T *win; } WindowObject; +static struct PyMethodDef WindowMethods[] = { + /* name, function, calling, documentation */ + { NULL, NULL, 0, NULL } +}; + static int ConvertFromPyObject(PyObject *, typval_T *); static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *); @@ -642,6 +655,16 @@ typedef struct pylinkedlist_T ref; } DictionaryObject; +static PyInt DictionaryAssItem(PyObject *, PyObject *, PyObject *); +static PyInt DictionaryLength(PyObject *); +static PyObject *DictionaryItem(PyObject *, PyObject *); + +static PyMappingMethods DictionaryAsMapping = { + (lenfunc) DictionaryLength, + (binaryfunc) DictionaryItem, + (objobjargproc) DictionaryAssItem, +}; + static PyObject * DictionaryNew(dict_T *dict) { @@ -658,6 +681,17 @@ DictionaryNew(dict_T *dict) return (PyObject *)(self); } + static void +DictionaryDestructor(PyObject *self) +{ + DictionaryObject *this = ((DictionaryObject *) (self)); + + pyll_remove(&this->ref, &lastdict); + dict_unref(this->dict); + + DESTRUCTOR_FINISH(self); +} + static int pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookupDict) { @@ -804,9 +838,11 @@ pymap_to_tv(PyObject *obj, typval_T *tv, return 0; } - static PyInt -DictionarySetattr(DictionaryObject *self, char *name, PyObject *val) + static int +DictionarySetattr(PyObject *self, char *name, PyObject *val) { + DictionaryObject *this = (DictionaryObject *)(self); + if (val == NULL) { PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes")); @@ -815,7 +851,7 @@ DictionarySetattr(DictionaryObject *self if (strcmp(name, "locked") == 0) { - if (self->dict->dv_lock == VAR_FIXED) + if (this->dict->dv_lock == VAR_FIXED) { PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed dictionary")); return -1; @@ -829,9 +865,9 @@ DictionarySetattr(DictionaryObject *self } if (val == Py_True) - self->dict->dv_lock = VAR_LOCKED; + this->dict->dv_lock = VAR_LOCKED; else - self->dict->dv_lock = 0; + this->dict->dv_lock = 0; } return 0; } @@ -963,6 +999,8 @@ static struct PyMethodDef DictionaryMeth }; static PyTypeObject ListType; +static PySequenceMethods ListAsSeq; +static PyMappingMethods ListAsMapping; typedef struct { @@ -987,6 +1025,17 @@ ListNew(list_T *list) return (PyObject *)(self); } + static void +ListDestructor(PyObject *self) +{ + ListObject *this = (ListObject *)(self); + + pyll_remove(&this->ref, &lastlist); + list_unref(this->list); + + DESTRUCTOR_FINISH(self); +} + static int list_py_concat(list_T *l, PyObject *obj, PyObject *lookupDict) { @@ -1307,8 +1356,10 @@ ListConcatInPlace(PyObject *self, PyObje } static int -ListSetattr(ListObject *self, char *name, PyObject *val) +ListSetattr(PyObject *self, char *name, PyObject *val) { + ListObject *this = (ListObject *)(self); + if (val == NULL) { PyErr_SetString(PyExc_AttributeError, _("Cannot delete DictionaryObject attributes")); @@ -1317,7 +1368,7 @@ ListSetattr(ListObject *self, char *name if (strcmp(name, "locked") == 0) { - if (self->list->lv_lock == VAR_FIXED) + if (this->list->lv_lock == VAR_FIXED) { PyErr_SetString(PyExc_TypeError, _("Cannot modify fixed list")); return -1; @@ -1331,9 +1382,9 @@ ListSetattr(ListObject *self, char *name } if (val == Py_True) - self->list->lv_lock = VAR_LOCKED; + this->list->lv_lock = VAR_LOCKED; else - self->list->lv_lock = 0; + this->list->lv_lock = 0; } return 0; } @@ -1376,6 +1427,17 @@ FunctionNew(char_u *name) return (PyObject *)(self); } + static void +FunctionDestructor(PyObject *self) +{ + FunctionObject *this = (FunctionObject *) (self); + + func_unref(this->name); + PyMem_Del(this->name); + + DESTRUCTOR_FINISH(self); +} + static PyObject * FunctionCall(PyObject *self, PyObject *argsObject, PyObject *kwargs) { @@ -1451,6 +1513,45 @@ CheckWindow(WindowObject *this) static int WindowSetattr(PyObject *, char *, PyObject *); static PyObject *WindowRepr(PyObject *); +static PyTypeObject WindowType; + + static PyObject * +WindowAttr(WindowObject *this, char *name) +{ + if (strcmp(name, "buffer") == 0) + return (PyObject *)BufferNew(this->win->w_buffer); + else if (strcmp(name, "cursor") == 0) + { + pos_T *pos = &this->win->w_cursor; + + return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); + } + else if (strcmp(name, "height") == 0) + return Py_BuildValue("l", (long)(this->win->w_height)); +#ifdef FEAT_VERTSPLIT + else if (strcmp(name, "width") == 0) + return Py_BuildValue("l", (long)(W_WIDTH(this->win))); +#endif + else if (strcmp(name,"__members__") == 0) + return Py_BuildValue("[sss]", "buffer", "cursor", "height"); + else + return NULL; +} + + static void +WindowDestructor(PyObject *self) +{ + WindowObject *this = (WindowObject *)(self); + + if (this->win && this->win != INVALID_WINDOW_VALUE) +#if PY_MAJOR_VERSION >= 3 + this->win->w_python3_ref = NULL; +#else + this->win->w_python_ref = NULL; +#endif + + DESTRUCTOR_FINISH(self); +} static int WindowSetattr(PyObject *self, char *name, PyObject *val) @@ -1579,6 +1680,15 @@ WindowRepr(PyObject *self) /* * Window list object - Implementation */ + +typedef struct +{ + PyObject_HEAD +} WinListObject; + +static PyTypeObject WinListType; +static PySequenceMethods BufListAsSeq; + static PyInt WinListLength(PyObject *self UNUSED) { @@ -2310,10 +2420,11 @@ RBAppend(BufferObject *self, PyObject *a return Py_None; } - -/* Buffer object - Definitions +/* Range object - Definitions */ +static PyTypeObject RangeType; + typedef struct { PyObject_HEAD @@ -2322,6 +2433,10 @@ typedef struct PyInt end; } RangeObject; +static void RangeDestructor(PyObject *); +static PySequenceMethods RangeAsSeq; +static PyMappingMethods RangeAsMapping; + static PyObject * RangeNew(buf_T *buf, PyInt start, PyInt end) { @@ -2346,6 +2461,46 @@ RangeNew(buf_T *buf, PyInt start, PyInt return (PyObject *)(self); } + static void +RangeDestructor(PyObject *self) +{ + Py_DECREF(((RangeObject *)(self))->buf); + DESTRUCTOR_FINISH(self); +} + +static PyTypeObject BufferType; +static PyObject *BufferRepr(PyObject *); +static PySequenceMethods BufferAsSeq; +static PyMappingMethods BufferAsMapping; + + static void +BufferDestructor(PyObject *self) +{ + BufferObject *this = (BufferObject *)(self); + + if (this->buf && this->buf != INVALID_BUFFER_VALUE) +#if PY_MAJOR_VERSION >= 3 + this->buf->b_python3_ref = NULL; +#else + this->buf->b_python_ref = NULL; +#endif + + DESTRUCTOR_FINISH(self); +} + + static PyObject * +BufferAttr(BufferObject *this, char *name) +{ + if (strcmp(name, "name") == 0) + return Py_BuildValue("s", this->buf->b_ffname); + else if (strcmp(name, "number") == 0) + return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum); + else if (strcmp(name,"__members__") == 0) + return Py_BuildValue("[ss]", "name", "number"); + else + return NULL; +} + static PyObject * BufferAppend(PyObject *self, PyObject *args) { @@ -2409,6 +2564,35 @@ BufferRange(PyObject *self, PyObject *ar return RangeNew(((BufferObject *)(self))->buf, start, end); } + static PyObject * +BufferRepr(PyObject *self) +{ + static char repr[100]; + BufferObject *this = (BufferObject *)(self); + + if (this->buf == INVALID_BUFFER_VALUE) + { + vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); + return PyString_FromString(repr); + } + else + { + char *name = (char *)this->buf->b_fname; + PyInt len; + + if (name == NULL) + name = ""; + len = strlen(name); + + if (len > 35) + name = name + (35 - len); + + vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); + + return PyString_FromString(repr); + } +} + static struct PyMethodDef BufferMethods[] = { /* name, function, calling, documentation */ {"append", BufferAppend, 1, "Append data to Vim buffer" }, @@ -2497,6 +2681,49 @@ static struct PyMethodDef RangeMethods[] { NULL, NULL, 0, NULL } }; +/* Current items object - Implementation + */ + +static PyInt RangeStart; +static PyInt RangeEnd; + + static PyObject * +CurrentGetattr(PyObject *self UNUSED, char *name) +{ + if (strcmp(name, "buffer") == 0) + return (PyObject *)BufferNew(curbuf); + else if (strcmp(name, "window") == 0) + return (PyObject *)WindowNew(curwin); + else if (strcmp(name, "line") == 0) + return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); + else if (strcmp(name, "range") == 0) + return RangeNew(curbuf, RangeStart, RangeEnd); + else if (strcmp(name,"__members__") == 0) + return Py_BuildValue("[ssss]", "buffer", "window", "line", "range"); + else + { + PyErr_SetString(PyExc_AttributeError, name); + return NULL; + } +} + + static int +CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) +{ + if (strcmp(name, "line") == 0) + { + if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) + return -1; + + return 0; + } + else + { + PyErr_SetString(PyExc_AttributeError, name); + return -1; + } +} + static void set_ref_in_py(const int copyID) { @@ -2770,3 +2997,169 @@ ConvertToPyObject(typval_T *tv) return NULL; } } + +typedef struct +{ + PyObject_HEAD +} CurrentObject; +static PyTypeObject CurrentType; + + static void +init_structs(void) +{ + vim_memset(&OutputType, 0, sizeof(OutputType)); + OutputType.tp_name = "vim.message"; + OutputType.tp_basicsize = sizeof(OutputObject); + OutputType.tp_flags = Py_TPFLAGS_DEFAULT; + OutputType.tp_doc = "vim message object"; + OutputType.tp_methods = OutputMethods; +#if PY_MAJOR_VERSION >= 3 + OutputType.tp_getattro = OutputGetattro; + OutputType.tp_setattro = OutputSetattro; + OutputType.tp_alloc = call_PyType_GenericAlloc; + OutputType.tp_new = call_PyType_GenericNew; + OutputType.tp_free = call_PyObject_Free; +#else + OutputType.tp_getattr = OutputGetattr; + OutputType.tp_setattr = OutputSetattr; +#endif + + vim_memset(&BufferType, 0, sizeof(BufferType)); + BufferType.tp_name = "vim.buffer"; + BufferType.tp_basicsize = sizeof(BufferType); + BufferType.tp_dealloc = BufferDestructor; + BufferType.tp_repr = BufferRepr; + BufferType.tp_as_sequence = &BufferAsSeq; + BufferType.tp_as_mapping = &BufferAsMapping; + BufferType.tp_flags = Py_TPFLAGS_DEFAULT; + BufferType.tp_doc = "vim buffer object"; + BufferType.tp_methods = BufferMethods; +#if PY_MAJOR_VERSION >= 3 + BufferType.tp_getattro = BufferGetattro; + BufferType.tp_alloc = call_PyType_GenericAlloc; + BufferType.tp_new = call_PyType_GenericNew; + BufferType.tp_free = call_PyObject_Free; +#else + BufferType.tp_getattr = BufferGetattr; +#endif + + vim_memset(&WindowType, 0, sizeof(WindowType)); + WindowType.tp_name = "vim.window"; + WindowType.tp_basicsize = sizeof(WindowObject); + WindowType.tp_dealloc = WindowDestructor; + WindowType.tp_repr = WindowRepr; + WindowType.tp_flags = Py_TPFLAGS_DEFAULT; + WindowType.tp_doc = "vim Window object"; + WindowType.tp_methods = WindowMethods; +#if PY_MAJOR_VERSION >= 3 + WindowType.tp_getattro = WindowGetattro; + WindowType.tp_setattro = WindowSetattro; + WindowType.tp_alloc = call_PyType_GenericAlloc; + WindowType.tp_new = call_PyType_GenericNew; + WindowType.tp_free = call_PyObject_Free; +#else + WindowType.tp_getattr = WindowGetattr; + WindowType.tp_setattr = WindowSetattr; +#endif + + vim_memset(&BufListType, 0, sizeof(BufListType)); + BufListType.tp_name = "vim.bufferlist"; + BufListType.tp_basicsize = sizeof(BufListObject); + BufListType.tp_as_sequence = &BufListAsSeq; + BufListType.tp_flags = Py_TPFLAGS_DEFAULT; + BufferType.tp_doc = "vim buffer list"; + + vim_memset(&WinListType, 0, sizeof(WinListType)); + WinListType.tp_name = "vim.windowlist"; + WinListType.tp_basicsize = sizeof(WinListType); + WinListType.tp_as_sequence = &WinListAsSeq; + WinListType.tp_flags = Py_TPFLAGS_DEFAULT; + WinListType.tp_doc = "vim window list"; + + vim_memset(&RangeType, 0, sizeof(RangeType)); + RangeType.tp_name = "vim.range"; + RangeType.tp_basicsize = sizeof(RangeObject); + RangeType.tp_dealloc = RangeDestructor; + RangeType.tp_repr = RangeRepr; + RangeType.tp_as_sequence = &RangeAsSeq; + RangeType.tp_as_mapping = &RangeAsMapping; + RangeType.tp_flags = Py_TPFLAGS_DEFAULT; + RangeType.tp_doc = "vim Range object"; + RangeType.tp_methods = RangeMethods; +#if PY_MAJOR_VERSION >= 3 + RangeType.tp_getattro = RangeGetattro; + RangeType.tp_alloc = call_PyType_GenericAlloc; + RangeType.tp_new = call_PyType_GenericNew; + RangeType.tp_free = call_PyObject_Free; +#else + RangeType.tp_getattr = RangeGetattr; +#endif + + vim_memset(&CurrentType, 0, sizeof(CurrentType)); + CurrentType.tp_name = "vim.currentdata"; + CurrentType.tp_basicsize = sizeof(CurrentObject); + CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; + CurrentType.tp_doc = "vim current object"; +#if PY_MAJOR_VERSION >= 3 + CurrentType.tp_getattro = CurrentGetattro; + CurrentType.tp_setattro = CurrentSetattro; +#else + CurrentType.tp_getattr = CurrentGetattr; + CurrentType.tp_setattr = CurrentSetattr; +#endif + + vim_memset(&DictionaryType, 0, sizeof(DictionaryType)); + DictionaryType.tp_name = "vim.dictionary"; + DictionaryType.tp_basicsize = sizeof(DictionaryObject); + DictionaryType.tp_dealloc = DictionaryDestructor; + DictionaryType.tp_as_mapping = &DictionaryAsMapping; + DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT; + DictionaryType.tp_doc = "dictionary pushing modifications to vim structure"; + DictionaryType.tp_methods = DictionaryMethods; +#if PY_MAJOR_VERSION >= 3 + DictionaryType.tp_getattro = DictionaryGetattro; + DictionaryType.tp_setattro = DictionarySetattro; +#else + DictionaryType.tp_getattr = DictionaryGetattr; + DictionaryType.tp_setattr = DictionarySetattr; +#endif + + vim_memset(&ListType, 0, sizeof(ListType)); + ListType.tp_name = "vim.list"; + ListType.tp_dealloc = ListDestructor; + ListType.tp_basicsize = sizeof(ListObject); + ListType.tp_as_sequence = &ListAsSeq; + ListType.tp_as_mapping = &ListAsMapping; + ListType.tp_flags = Py_TPFLAGS_DEFAULT; + ListType.tp_doc = "list pushing modifications to vim structure"; + ListType.tp_methods = ListMethods; +#if PY_MAJOR_VERSION >= 3 + ListType.tp_getattro = ListGetattro; + ListType.tp_setattro = ListSetattro; +#else + ListType.tp_getattr = ListGetattr; + ListType.tp_setattr = ListSetattr; +#endif + + vim_memset(&FunctionType, 0, sizeof(FunctionType)); + FunctionType.tp_name = "vim.list"; + FunctionType.tp_basicsize = sizeof(FunctionObject); + FunctionType.tp_dealloc = FunctionDestructor; + FunctionType.tp_call = FunctionCall; + FunctionType.tp_flags = Py_TPFLAGS_DEFAULT; + FunctionType.tp_doc = "object that calls vim function"; + FunctionType.tp_methods = FunctionMethods; +#if PY_MAJOR_VERSION >= 3 + FunctionType.tp_getattro = FunctionGetattro; +#else + FunctionType.tp_getattr = FunctionGetattr; +#endif + +#if PY_MAJOR_VERSION >= 3 + vim_memset(&vimmodule, 0, sizeof(vimmodule)); + vimmodule.m_name = "vim"; + vimmodule.m_doc = "Vim Python interface\n"; + vimmodule.m_size = -1; + vimmodule.m_methods = VimMethods; +#endif +}