summaryrefslogtreecommitdiffstats
path: root/src/if_py_both.h
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2014-01-14 16:55:00 +0100
committerBram Moolenaar <Bram@vim.org>2014-01-14 16:55:00 +0100
commit1028f4d75ee04261f1338620c22f388a05098bb0 (patch)
tree46c911fb9aaca60b7caad845dcb8dce1915eaf06 /src/if_py_both.h
parent063a46ba77c3251f0b5245e872dcbad003c71024 (diff)
updated for version 7.4.152v7.4.152
Problem: Python: Cannot iterate over options. Solution: Add options iterator. (ZyX)
Diffstat (limited to 'src/if_py_both.h')
-rw-r--r--src/if_py_both.h86
1 files changed, 82 insertions, 4 deletions
diff --git a/src/if_py_both.h b/src/if_py_both.h
index 6e3939abe5..01cbe418c8 100644
--- a/src/if_py_both.h
+++ b/src/if_py_both.h
@@ -2949,10 +2949,10 @@ typedef int (*checkfun)(void *);
typedef struct
{
PyObject_HEAD
- int opt_type;
- void *from;
- checkfun Check;
- PyObject *fromObj;
+ int opt_type;
+ void *from;
+ checkfun Check;
+ PyObject *fromObj;
} OptionsObject;
static int
@@ -3072,6 +3072,69 @@ OptionsItem(OptionsObject *self, PyObject *keyObject)
}
static int
+OptionsContains(OptionsObject *self, PyObject *keyObject)
+{
+ char_u *key;
+ PyObject *todecref;
+
+ if (!(key = StringToChars(keyObject, &todecref)))
+ return -1;
+
+ if (*key == NUL)
+ {
+ Py_XDECREF(todecref);
+ return 0;
+ }
+
+ if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
+ {
+ Py_XDECREF(todecref);
+ return 1;
+ }
+ else
+ {
+ Py_XDECREF(todecref);
+ return 0;
+ }
+}
+
+typedef struct
+{
+ void *lastoption;
+ int opt_type;
+} optiterinfo_T;
+
+ static PyObject *
+OptionsIterNext(optiterinfo_T **oii)
+{
+ char_u *name;
+
+ if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
+ return PyString_FromString((char *)name);
+
+ return NULL;
+}
+
+ static PyObject *
+OptionsIter(OptionsObject *self)
+{
+ optiterinfo_T *oii;
+
+ if (!(oii = PyMem_New(optiterinfo_T, 1)))
+ {
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ oii->opt_type = self->opt_type;
+ oii->lastoption = NULL;
+
+ return IterNew(oii,
+ (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
+ NULL, NULL);
+}
+
+ static int
set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
{
char_u *errmsg;
@@ -3231,6 +3294,19 @@ OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
return ret;
}
+static PySequenceMethods OptionsAsSeq = {
+ 0, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc) OptionsContains, /* sq_contains */
+ 0, /* sq_inplace_concat */
+ 0, /* sq_inplace_repeat */
+};
+
static PyMappingMethods OptionsAsMapping = {
(lenfunc) NULL,
(binaryfunc) OptionsItem,
@@ -6121,8 +6197,10 @@ init_structs(void)
vim_memset(&OptionsType, 0, sizeof(OptionsType));
OptionsType.tp_name = "vim.options";
OptionsType.tp_basicsize = sizeof(OptionsObject);
+ OptionsType.tp_as_sequence = &OptionsAsSeq;
OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
OptionsType.tp_doc = "object for manipulating options";
+ OptionsType.tp_iter = (getiterfunc)OptionsIter;
OptionsType.tp_as_mapping = &OptionsAsMapping;
OptionsType.tp_dealloc = (destructor)OptionsDestructor;
OptionsType.tp_traverse = (traverseproc)OptionsTraverse;