1
0
forked from aniani/vim

updated for version 7.3.945

Problem:    Python: List of buffers is not very useful.
Solution:   Make vim.buffers a map. No iterator yet. (ZyX)
This commit is contained in:
Bram Moolenaar
2013-05-15 13:38:47 +02:00
parent 29607acff6
commit dfa38d4e45
7 changed files with 88 additions and 101 deletions

View File

@@ -209,12 +209,11 @@ Constants of the "vim" module
to which the variables referred. to which the variables referred.
vim.buffers *python-buffers* vim.buffers *python-buffers*
A sequence object providing access to the list of vim buffers. The A mapping object providing access to the list of vim buffers. The
object supports the following operations: > object supports the following operations: >
:py b = vim.buffers[i] # Indexing (read-only) :py b = vim.buffers[i] # Indexing (read-only)
:py b in vim.buffers # Membership test :py b in vim.buffers # Membership test
:py n = len(vim.buffers) # Number of elements :py n = len(vim.buffers) # Number of elements
:py for b in vim.buffers: # Sequential access
< <
vim.windows *python-windows* vim.windows *python-windows*
A sequence object providing access to the list of vim windows. The A sequence object providing access to the list of vim windows. The

View File

@@ -534,16 +534,15 @@ static struct PyMethodDef VimMethods[] = {
* Buffer list object - Implementation * Buffer list object - Implementation
*/ */
static PyTypeObject BufListType; static PyTypeObject BufMapType;
static PySequenceMethods BufListAsSeq;
typedef struct typedef struct
{ {
PyObject_HEAD PyObject_HEAD
} BufListObject; } BufMapObject;
static PyInt static PyInt
BufListLength(PyObject *self UNUSED) BufMapLength(PyObject *self UNUSED)
{ {
buf_T *b = firstbuf; buf_T *b = firstbuf;
PyInt n = 0; PyInt n = 0;
@@ -558,20 +557,41 @@ BufListLength(PyObject *self UNUSED)
} }
static PyObject * static PyObject *
BufListItem(PyObject *self UNUSED, PyInt n) BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
{ {
buf_T *b; buf_T *b;
int bnr;
for (b = firstbuf; b; b = b->b_next, --n) #if PY_MAJOR_VERSION < 3
if (PyInt_Check(keyObject))
bnr = PyInt_AsLong(keyObject);
else
#endif
if (PyLong_Check(keyObject))
bnr = PyLong_AsLong(keyObject);
else
{ {
if (n == 0) PyErr_SetString(PyExc_ValueError, _("key must be integer"));
return BufferNew(b); return NULL;
} }
PyErr_SetString(PyExc_IndexError, _("no such buffer")); b = buflist_findnr(bnr);
if (b)
return BufferNew(b);
else
{
PyErr_SetString(PyExc_KeyError, _("no such buffer"));
return NULL; return NULL;
}
} }
static PyMappingMethods BufMapAsMapping = {
(lenfunc) BufMapLength,
(binaryfunc) BufMapItem,
(objobjargproc) 0,
};
typedef struct pylinkedlist_S { typedef struct pylinkedlist_S {
struct pylinkedlist_S *pll_next; struct pylinkedlist_S *pll_next;
struct pylinkedlist_S *pll_prev; struct pylinkedlist_S *pll_prev;
@@ -3401,11 +3421,11 @@ init_structs(void)
WindowType.tp_setattr = WindowSetattr; WindowType.tp_setattr = WindowSetattr;
#endif #endif
vim_memset(&BufListType, 0, sizeof(BufListType)); vim_memset(&BufMapType, 0, sizeof(BufMapType));
BufListType.tp_name = "vim.bufferlist"; BufMapType.tp_name = "vim.bufferlist";
BufListType.tp_basicsize = sizeof(BufListObject); BufMapType.tp_basicsize = sizeof(BufMapObject);
BufListType.tp_as_sequence = &BufListAsSeq; BufMapType.tp_as_mapping = &BufMapAsMapping;
BufListType.tp_flags = Py_TPFLAGS_DEFAULT; BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
BufferType.tp_doc = "vim buffer list"; BufferType.tp_doc = "vim buffer list";
vim_memset(&WinListType, 0, sizeof(WinListType)); vim_memset(&WinListType, 0, sizeof(WinListType));

View File

@@ -1131,24 +1131,6 @@ RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
&((RangeObject *)(self))->end); &((RangeObject *)(self))->end);
} }
/* Buffer list object - Definitions
*/
static PySequenceMethods BufListAsSeq = {
(PyInquiry) BufListLength, /* sq_length, len(x) */
(binaryfunc) 0, /* sq_concat, x+y */
(PyIntArgFunc) 0, /* sq_repeat, x*n */
(PyIntArgFunc) BufListItem, /* sq_item, x[i] */
(PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
(PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
(PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
(objobjproc) 0,
#if PY_MAJOR_VERSION >= 2
(binaryfunc) 0,
0,
#endif
};
/* Window object - Implementation /* Window object - Implementation
*/ */
@@ -1212,9 +1194,9 @@ python_window_free(win_T *win)
} }
#endif #endif
static BufListObject TheBufferList = static BufMapObject TheBufferMap =
{ {
PyObject_HEAD_INIT(&BufListType) PyObject_HEAD_INIT(&BufMapType)
}; };
static WinListObject TheWindowList = static WinListObject TheWindowList =
@@ -1240,7 +1222,7 @@ PythonMod_Init(void)
PyType_Ready(&BufferType); PyType_Ready(&BufferType);
PyType_Ready(&RangeType); PyType_Ready(&RangeType);
PyType_Ready(&WindowType); PyType_Ready(&WindowType);
PyType_Ready(&BufListType); PyType_Ready(&BufMapType);
PyType_Ready(&WinListType); PyType_Ready(&WinListType);
PyType_Ready(&CurrentType); PyType_Ready(&CurrentType);
PyType_Ready(&OptionsType); PyType_Ready(&OptionsType);
@@ -1254,7 +1236,7 @@ PythonMod_Init(void)
VimError = Py_BuildValue("s", "vim.error"); VimError = Py_BuildValue("s", "vim.error");
PyDict_SetItemString(dict, "error", VimError); PyDict_SetItemString(dict, "error", VimError);
PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList); PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferMap);
PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent); PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList); PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
tmp = DictionaryNew(&globvardict); tmp = DictionaryNew(&globvardict);

View File

@@ -1272,22 +1272,6 @@ RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
} }
} }
/* Buffer list object - Definitions
*/
static PySequenceMethods BufListAsSeq = {
(lenfunc) BufListLength, /* sq_length, len(x) */
(binaryfunc) 0, /* sq_concat, x+y */
(ssizeargfunc) 0, /* sq_repeat, x*n */
(ssizeargfunc) BufListItem, /* sq_item, x[i] */
0, /* was_sq_slice, x[i:j] */
(ssizeobjargproc) 0, /* sq_as_item, x[i]=v */
0, /* sq_ass_slice, x[i:j]=v */
0, /* sq_contains */
0, /* sq_inplace_concat */
0, /* sq_inplace_repeat */
};
/* Window object - Implementation /* Window object - Implementation
*/ */
@@ -1512,9 +1496,9 @@ python3_window_free(win_T *win)
} }
#endif #endif
static BufListObject TheBufferList = static BufMapObject TheBufferMap =
{ {
PyObject_HEAD_INIT(&BufListType) PyObject_HEAD_INIT(&BufMapType)
}; };
static WinListObject TheWindowList = static WinListObject TheWindowList =
@@ -1538,7 +1522,7 @@ Py3Init_vim(void)
PyType_Ready(&BufferType); PyType_Ready(&BufferType);
PyType_Ready(&RangeType); PyType_Ready(&RangeType);
PyType_Ready(&WindowType); PyType_Ready(&WindowType);
PyType_Ready(&BufListType); PyType_Ready(&BufMapType);
PyType_Ready(&WinListType); PyType_Ready(&WinListType);
PyType_Ready(&CurrentType); PyType_Ready(&CurrentType);
PyType_Ready(&DictionaryType); PyType_Ready(&DictionaryType);
@@ -1557,8 +1541,8 @@ Py3Init_vim(void)
Py_INCREF(VimError); Py_INCREF(VimError);
PyModule_AddObject(mod, "error", VimError); PyModule_AddObject(mod, "error", VimError);
Py_INCREF((PyObject *)(void *)&TheBufferList); Py_INCREF((PyObject *)(void *)&TheBufferMap);
PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList); PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferMap);
Py_INCREF((PyObject *)(void *)&TheCurrent); Py_INCREF((PyObject *)(void *)&TheCurrent);
PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent); PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
Py_INCREF((PyObject *)(void *)&TheWindowList); Py_INCREF((PyObject *)(void *)&TheWindowList);

View File

@@ -226,13 +226,13 @@ bar
p/bopts1: False p/bopts1: False
inv: 2! ValueError inv: 2! ValueError
G: 0 G: 0
W: 1:1 2:1 3:0 4:0 W: 1:0 2:1 3:0 4:1
B: 1:1 2:1 3:0 4:0 B: 1:0 2:1 3:0 4:1
del wopts3! KeyError del wopts3! KeyError
del bopts3! ValueError del bopts3! ValueError
G: 0 G: 0
W: 1:1 2:1 3:0 4:0 W: 1:0 2:1 3:0 4:1
B: 1:1 2:1 3:0 4:0 B: 1:0 2:1 3:0 4:1
>>> iminsert >>> iminsert
p/gopts1! KeyError p/gopts1! KeyError
inv: 3! KeyError inv: 3! KeyError
@@ -244,13 +244,13 @@ bar
wopts3! KeyError wopts3! KeyError
p/bopts1: 2 p/bopts1: 2
G: 1 G: 1
W: 1:2 2:1 3:0 4:2 W: 1:0 2:2 3:2 4:1
B: 1:2 2:1 3:0 4:2 B: 1:0 2:2 3:2 4:1
del wopts3! KeyError del wopts3! KeyError
del bopts3! ValueError del bopts3! ValueError
G: 1 G: 1
W: 1:2 2:1 3:0 4:2 W: 1:0 2:2 3:2 4:1
B: 1:2 2:1 3:0 4:2 B: 1:0 2:2 3:2 4:1
>>> omnifunc >>> omnifunc
p/gopts1! KeyError p/gopts1! KeyError
inv: 1! KeyError inv: 1! KeyError
@@ -263,13 +263,13 @@ bar
p/bopts1: '' p/bopts1: ''
inv: 1! ValueError inv: 1! ValueError
G: '' G: ''
W: 1:'B' 2:'C' 3:'A' 4:'' W: 1:'A' 2:'B' 3:'' 4:'C'
B: 1:'B' 2:'C' 3:'A' 4:'' B: 1:'A' 2:'B' 3:'' 4:'C'
del wopts3! KeyError del wopts3! KeyError
del bopts3! ValueError del bopts3! ValueError
G: '' G: ''
W: 1:'B' 2:'C' 3:'A' 4:'' W: 1:'A' 2:'B' 3:'' 4:'C'
B: 1:'B' 2:'C' 3:'A' 4:'' B: 1:'A' 2:'B' 3:'' 4:'C'
>>> preserveindent >>> preserveindent
p/gopts1! KeyError p/gopts1! KeyError
inv: 2! KeyError inv: 2! KeyError
@@ -282,13 +282,13 @@ bar
p/bopts1: False p/bopts1: False
inv: 2! ValueError inv: 2! ValueError
G: 0 G: 0
W: 1:1 2:1 3:0 4:0 W: 1:0 2:1 3:0 4:1
B: 1:1 2:1 3:0 4:0 B: 1:0 2:1 3:0 4:1
del wopts3! KeyError del wopts3! KeyError
del bopts3! ValueError del bopts3! ValueError
G: 0 G: 0
W: 1:1 2:1 3:0 4:0 W: 1:0 2:1 3:0 4:1
B: 1:1 2:1 3:0 4:0 B: 1:0 2:1 3:0 4:1
>>> path >>> path
p/gopts1: '.,/usr/include,,' p/gopts1: '.,/usr/include,,'
inv: 0! ValueError inv: 0! ValueError
@@ -300,12 +300,12 @@ bar
p/bopts1: None p/bopts1: None
inv: 0! ValueError inv: 0! ValueError
G: '.,,' G: '.,,'
W: 1:',,' 2:'.' 3:'.,,' 4:'.,,' W: 1:'.,,' 2:',,' 3:'.,,' 4:'.'
B: 1:',,' 2:'.' 3:'.,,' 4:'.,,' B: 1:'.,,' 2:',,' 3:'.,,' 4:'.'
del wopts3! KeyError del wopts3! KeyError
G: '.,,' G: '.,,'
W: 1:',,' 2:'.,,' 3:'.,,' 4:'.,,' W: 1:'.,,' 2:',,' 3:'.,,' 4:'.,,'
B: 1:',,' 2:'.,,' 3:'.,,' 4:'.,,' B: 1:'.,,' 2:',,' 3:'.,,' 4:'.,,'
First line First line
First line First line
def def

View File

@@ -215,13 +215,13 @@ bar
p/bopts1: False p/bopts1: False
inv: 2! ValueError inv: 2! ValueError
G: 0 G: 0
W: 1:1 2:1 3:0 4:0 W: 1:0 2:1 3:0 4:1
B: 1:1 2:1 3:0 4:0 B: 1:0 2:1 3:0 4:1
del wopts3! KeyError del wopts3! KeyError
del bopts3! ValueError del bopts3! ValueError
G: 0 G: 0
W: 1:1 2:1 3:0 4:0 W: 1:0 2:1 3:0 4:1
B: 1:1 2:1 3:0 4:0 B: 1:0 2:1 3:0 4:1
>>> iminsert >>> iminsert
p/gopts1! KeyError p/gopts1! KeyError
inv: 3! KeyError inv: 3! KeyError
@@ -233,13 +233,13 @@ bar
wopts3! KeyError wopts3! KeyError
p/bopts1: 2 p/bopts1: 2
G: 1 G: 1
W: 1:2 2:1 3:0 4:2 W: 1:0 2:2 3:2 4:1
B: 1:2 2:1 3:0 4:2 B: 1:0 2:2 3:2 4:1
del wopts3! KeyError del wopts3! KeyError
del bopts3! ValueError del bopts3! ValueError
G: 1 G: 1
W: 1:2 2:1 3:0 4:2 W: 1:0 2:2 3:2 4:1
B: 1:2 2:1 3:0 4:2 B: 1:0 2:2 3:2 4:1
>>> omnifunc >>> omnifunc
p/gopts1! KeyError p/gopts1! KeyError
inv: 1! KeyError inv: 1! KeyError
@@ -252,13 +252,13 @@ bar
p/bopts1: b'' p/bopts1: b''
inv: 1! ValueError inv: 1! ValueError
G: '' G: ''
W: 1:'B' 2:'C' 3:'A' 4:'' W: 1:'A' 2:'B' 3:'' 4:'C'
B: 1:'B' 2:'C' 3:'A' 4:'' B: 1:'A' 2:'B' 3:'' 4:'C'
del wopts3! KeyError del wopts3! KeyError
del bopts3! ValueError del bopts3! ValueError
G: '' G: ''
W: 1:'B' 2:'C' 3:'A' 4:'' W: 1:'A' 2:'B' 3:'' 4:'C'
B: 1:'B' 2:'C' 3:'A' 4:'' B: 1:'A' 2:'B' 3:'' 4:'C'
>>> preserveindent >>> preserveindent
p/gopts1! KeyError p/gopts1! KeyError
inv: 2! KeyError inv: 2! KeyError
@@ -271,13 +271,13 @@ bar
p/bopts1: False p/bopts1: False
inv: 2! ValueError inv: 2! ValueError
G: 0 G: 0
W: 1:1 2:1 3:0 4:0 W: 1:0 2:1 3:0 4:1
B: 1:1 2:1 3:0 4:0 B: 1:0 2:1 3:0 4:1
del wopts3! KeyError del wopts3! KeyError
del bopts3! ValueError del bopts3! ValueError
G: 0 G: 0
W: 1:1 2:1 3:0 4:0 W: 1:0 2:1 3:0 4:1
B: 1:1 2:1 3:0 4:0 B: 1:0 2:1 3:0 4:1
>>> path >>> path
p/gopts1: b'.,/usr/include,,' p/gopts1: b'.,/usr/include,,'
inv: 0! ValueError inv: 0! ValueError
@@ -289,12 +289,12 @@ bar
p/bopts1: None p/bopts1: None
inv: 0! ValueError inv: 0! ValueError
G: '.,,' G: '.,,'
W: 1:',,' 2:'.' 3:'.,,' 4:'.,,' W: 1:'.,,' 2:',,' 3:'.,,' 4:'.'
B: 1:',,' 2:'.' 3:'.,,' 4:'.,,' B: 1:'.,,' 2:',,' 3:'.,,' 4:'.'
del wopts3! KeyError del wopts3! KeyError
G: '.,,' G: '.,,'
W: 1:',,' 2:'.,,' 3:'.,,' 4:'.,,' W: 1:'.,,' 2:',,' 3:'.,,' 4:'.,,'
B: 1:',,' 2:'.,,' 3:'.,,' 4:'.,,' B: 1:'.,,' 2:',,' 3:'.,,' 4:'.,,'
First line First line
First line First line
def def

View File

@@ -728,6 +728,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
945,
/**/ /**/
944, 944,
/**/ /**/