blob: 4f977c38c20be58756ed7ad2202d4e356c83af6b [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020016#if PY_VERSION_HEX < 0x02050000
17typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
18#endif
19
Bram Moolenaar91805fc2011-06-26 04:01:44 +020020#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020021# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020022#else
23# define ENC_OPT "latin1"
24#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020025#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020026
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020027static const char *vim_special_path = "_vim_path_";
28
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020029#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020030#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020031#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaarc476e522013-06-23 13:46:40 +020032#define PyErr_FORMAT(exc, str, tail) PyErr_Format(exc, _(str), tail)
33#define PyErr_VIM_FORMAT(str, tail) PyErr_FORMAT(VimError, str, tail)
34
35#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
36 ? "(NULL)" \
37 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020038
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020039#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020040 N_("empty keys are not allowed"))
41#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
42#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
43#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
44#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
45#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
46#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020047#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020049#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020050 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020051 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020052
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020053#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
54#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020055#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020056
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020057typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020058typedef void (*runner)(const char *, void *
59#ifdef PY_CAN_RECURSE
60 , PyGILState_STATE *
61#endif
62 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020063
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064static int ConvertFromPyObject(PyObject *, typval_T *);
65static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020066static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020067static PyObject *WindowNew(win_T *, tabpage_T *);
68static PyObject *BufferNew (buf_T *);
69static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020070
71static PyInt RangeStart;
72static PyInt RangeEnd;
73
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020074static PyObject *globals;
75
Bram Moolenaarf4258302013-06-02 18:20:17 +020076static PyObject *py_chdir;
77static PyObject *py_fchdir;
78static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020079static PyObject *vim_module;
80static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020081
Bram Moolenaar81c40c52013-06-12 14:41:04 +020082static PyObject *py_find_module;
83static PyObject *py_load_module;
84
85static PyObject *VimError;
86
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020087/*
88 * obtain a lock on the Vim data structures
89 */
90 static void
91Python_Lock_Vim(void)
92{
93}
94
95/*
96 * release a lock on the Vim data structures
97 */
98 static void
99Python_Release_Vim(void)
100{
101}
102
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200103/*
104 * The "todecref" argument holds a pointer to PyObject * that must be
105 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
106 * was needed to generate returned value is object.
107 *
108 * Use Py_XDECREF to decrement reference count.
109 */
110 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200111StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200112{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200113 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200114
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200115 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200116 {
117
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200118 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
119 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200120 return NULL;
121
122 *todecref = NULL;
123 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200124 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200126 PyObject *bytes;
127
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200128 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200129 return NULL;
130
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200131 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
132 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200133 {
134 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200135 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200136 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200137
138 *todecref = bytes;
139 }
140 else
141 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200142 PyErr_FORMAT(PyExc_TypeError,
143#if PY_MAJOR_VERSION < 3
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200144 N_("expected str() or unicode() instance, but got %s")
Bram Moolenaarc476e522013-06-23 13:46:40 +0200145#else
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200146 N_("expected bytes() or str() instance, but got %s")
Bram Moolenaarc476e522013-06-23 13:46:40 +0200147#endif
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200148 , Py_TYPE_NAME(obj));
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200149 return NULL;
150 }
151
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200152 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200153}
154
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200155#define NUMBER_LONG 1
156#define NUMBER_INT 2
157#define NUMBER_NATURAL 4
158#define NUMBER_UNSIGNED 8
159
160 static int
161NumberToLong(PyObject *obj, long *result, int flags)
162{
163#if PY_MAJOR_VERSION < 3
164 if (PyInt_Check(obj))
165 {
166 *result = PyInt_AsLong(obj);
167 if (PyErr_Occurred())
168 return -1;
169 }
170 else
171#endif
172 if (PyLong_Check(obj))
173 {
174 *result = PyLong_AsLong(obj);
175 if (PyErr_Occurred())
176 return -1;
177 }
178 else if (PyNumber_Check(obj))
179 {
180 PyObject *num;
181
182 if (!(num = PyNumber_Long(obj)))
183 return -1;
184
185 *result = PyLong_AsLong(num);
186
187 Py_DECREF(num);
188
189 if (PyErr_Occurred())
190 return -1;
191 }
192 else
193 {
194 PyErr_FORMAT(PyExc_TypeError,
195#if PY_MAJOR_VERSION < 3
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200196 N_("expected int(), long() or something supporting "
197 "coercing to long(), but got %s")
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200198#else
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200199 N_("expected int() or something supporting coercing to int(), "
200 "but got %s")
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200201#endif
202 , Py_TYPE_NAME(obj));
203 return -1;
204 }
205
206 if (flags & NUMBER_INT)
207 {
208 if (*result > INT_MAX)
209 {
210 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200211 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200212 return -1;
213 }
214 else if (*result < INT_MIN)
215 {
216 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200217 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200218 return -1;
219 }
220 }
221
222 if (flags & NUMBER_NATURAL)
223 {
224 if (*result <= 0)
225 {
226 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200227 N_("number must be greater then zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200228 return -1;
229 }
230 }
231 else if (flags & NUMBER_UNSIGNED)
232 {
233 if (*result < 0)
234 {
235 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200236 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200237 return -1;
238 }
239 }
240
241 return 0;
242}
243
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200244 static int
245add_string(PyObject *list, char *s)
246{
247 PyObject *string;
248
249 if (!(string = PyString_FromString(s)))
250 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200251
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200252 if (PyList_Append(list, string))
253 {
254 Py_DECREF(string);
255 return -1;
256 }
257
258 Py_DECREF(string);
259 return 0;
260}
261
262 static PyObject *
263ObjectDir(PyObject *self, char **attributes)
264{
265 PyMethodDef *method;
266 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200267 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200268
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200269 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200270 return NULL;
271
272 if (self)
273 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200274 if (add_string(ret, (char *) method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200275 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200276 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200277 return NULL;
278 }
279
280 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200281 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200282 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200283 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200284 return NULL;
285 }
286
287#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200288 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200289 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200290 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200291 return NULL;
292 }
293#endif
294
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200295 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200296}
297
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200298/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200299 */
300
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200301/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200302typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200303
304static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200305
306typedef struct
307{
308 PyObject_HEAD
309 long softspace;
310 long error;
311} OutputObject;
312
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200313static char *OutputAttrs[] = {
314 "softspace",
315 NULL
316};
317
318 static PyObject *
319OutputDir(PyObject *self)
320{
321 return ObjectDir(self, OutputAttrs);
322}
323
Bram Moolenaar77045652012-09-21 13:46:06 +0200324 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200325OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200326{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200327 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200328 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200329 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200330 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200331 return -1;
332 }
333
334 if (strcmp(name, "softspace") == 0)
335 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200336 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200337 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200338 return 0;
339 }
340
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200341 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200342 return -1;
343}
344
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200345/* Buffer IO, we write one whole line at a time. */
346static garray_T io_ga = {0, 0, 1, 80, NULL};
347static writefn old_fn = NULL;
348
349 static void
350PythonIO_Flush(void)
351{
352 if (old_fn != NULL && io_ga.ga_len > 0)
353 {
354 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
355 old_fn((char_u *)io_ga.ga_data);
356 }
357 io_ga.ga_len = 0;
358}
359
360 static void
361writer(writefn fn, char_u *str, PyInt n)
362{
363 char_u *ptr;
364
365 /* Flush when switching output function. */
366 if (fn != old_fn)
367 PythonIO_Flush();
368 old_fn = fn;
369
370 /* Write each NL separated line. Text after the last NL is kept for
371 * writing later. */
372 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
373 {
374 PyInt len = ptr - str;
375
376 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
377 break;
378
379 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
380 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
381 fn((char_u *)io_ga.ga_data);
382 str = ptr + 1;
383 n -= len + 1;
384 io_ga.ga_len = 0;
385 }
386
387 /* Put the remaining text into io_ga for later printing. */
388 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
389 {
390 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
391 io_ga.ga_len += (int)n;
392 }
393}
394
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200395 static int
396write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200397{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200398 Py_ssize_t len = 0;
399 char *str = NULL;
400 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200401
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200402 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
403 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200404
405 Py_BEGIN_ALLOW_THREADS
406 Python_Lock_Vim();
407 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
408 Python_Release_Vim();
409 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200410 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200411
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200412 return 0;
413}
414
415 static PyObject *
416OutputWrite(OutputObject *self, PyObject *string)
417{
418 if (write_output(self, string))
419 return NULL;
420
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200421 Py_INCREF(Py_None);
422 return Py_None;
423}
424
425 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200426OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200427{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200428 PyObject *iterator;
429 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200430
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200431 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200432 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200433
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200434 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200435 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200436 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200437 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200438 Py_DECREF(iterator);
439 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440 return NULL;
441 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200442 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443 }
444
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200445 Py_DECREF(iterator);
446
447 /* Iterator may have finished due to an exception */
448 if (PyErr_Occurred())
449 return NULL;
450
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200451 Py_INCREF(Py_None);
452 return Py_None;
453}
454
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100455 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200456OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100457{
458 /* do nothing */
459 Py_INCREF(Py_None);
460 return Py_None;
461}
462
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200463/***************/
464
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200465static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200466 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200467 {"write", (PyCFunction)OutputWrite, METH_O, ""},
468 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200469 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200470 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200471 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200472};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200473
474static OutputObject Output =
475{
476 PyObject_HEAD_INIT(&OutputType)
477 0,
478 0
479};
480
481static OutputObject Error =
482{
483 PyObject_HEAD_INIT(&OutputType)
484 0,
485 1
486};
487
488 static int
489PythonIO_Init_io(void)
490{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200491 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
492 return -1;
493 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
494 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200495
496 if (PyErr_Occurred())
497 {
498 EMSG(_("E264: Python: Error initialising I/O objects"));
499 return -1;
500 }
501
502 return 0;
503}
504
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200505typedef struct
506{
507 PyObject_HEAD
508 PyObject *module;
509} LoaderObject;
510static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200511
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200512 static void
513LoaderDestructor(LoaderObject *self)
514{
515 Py_DECREF(self->module);
516 DESTRUCTOR_FINISH(self);
517}
518
519 static PyObject *
520LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
521{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200522 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200523
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200524 Py_INCREF(ret);
525 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200526}
527
528static struct PyMethodDef LoaderMethods[] = {
529 /* name, function, calling, doc */
530 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
531 { NULL, NULL, 0, NULL}
532};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200533
534/* Check to see whether a Vim error has been reported, or a keyboard
535 * interrupt has been detected.
536 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200537
538 static void
539VimTryStart(void)
540{
541 ++trylevel;
542}
543
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200544 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200545VimTryEnd(void)
546{
547 --trylevel;
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200548 /* Without this it stops processing all subsequent VimL commands and
549 * generates strange error messages if I e.g. try calling Test() in a cycle */
550 did_emsg = FALSE;
551 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200552 if (got_int)
553 {
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200554 did_throw = got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200555 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200556 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200557 }
558 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200559 return (PyErr_Occurred() ? -1 : 0);
560 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200561 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200562 {
563 did_throw = FALSE;
564 return -1;
565 }
566 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200567 else
568 {
569 PyErr_SetVim((char *) current_exception->value);
570 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200571 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200572 }
573}
574
575 static int
576VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200577{
578 if (got_int)
579 {
580 PyErr_SetNone(PyExc_KeyboardInterrupt);
581 return 1;
582 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200583 return 0;
584}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200585
586/* Vim module - Implementation
587 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200588
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200589 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200590VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200591{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200592 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200593 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200594 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200595
Bram Moolenaar389a1792013-06-23 13:00:44 +0200596 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200597 return NULL;
598
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200599 Py_BEGIN_ALLOW_THREADS
600 Python_Lock_Vim();
601
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200602 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200603 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200604 update_screen(VALID);
605
606 Python_Release_Vim();
607 Py_END_ALLOW_THREADS
608
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200609 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200610 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200611 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200612 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200613
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200614 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200615 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200616 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200617}
618
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200619/*
620 * Function to translate a typval_T into a PyObject; this will recursively
621 * translate lists/dictionaries into their Python equivalents.
622 *
623 * The depth parameter is to avoid infinite recursion, set it to 1 when
624 * you call VimToPython.
625 */
626 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200627VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200628{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200629 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200630 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200631 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200632
633 /* Avoid infinite recursion */
634 if (depth > 100)
635 {
636 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200637 ret = Py_None;
638 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200639 }
640
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200641 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200642 * then and we can use it again. */
643 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
644 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
645 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200646 sprintf(ptrBuf, "%p",
647 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
648 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200649
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200650 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200651 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200652 Py_INCREF(ret);
653 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200654 }
655 }
656
657 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200658 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200659 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200660 else if (our_tv->v_type == VAR_NUMBER)
661 {
662 char buf[NUMBUFLEN];
663
664 /* For backwards compatibility numbers are stored as strings. */
665 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200666 ret = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200667 }
668# ifdef FEAT_FLOAT
669 else if (our_tv->v_type == VAR_FLOAT)
670 {
671 char buf[NUMBUFLEN];
672
673 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200674 ret = PyString_FromString((char *) buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200675 }
676# endif
677 else if (our_tv->v_type == VAR_LIST)
678 {
679 list_T *list = our_tv->vval.v_list;
680 listitem_T *curr;
681
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200682 if (list == NULL)
683 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200684
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200685 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200686 return NULL;
687
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200688 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200690 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200691 return NULL;
692 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200694 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
695 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200696 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200697 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200698 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200699 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200700 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200701 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200702 {
703 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200704 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200705 return NULL;
706 }
707 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200708 }
709 }
710 else if (our_tv->v_type == VAR_DICT)
711 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200712
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200713 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
714 long_u todo = ht->ht_used;
715 hashitem_T *hi;
716 dictitem_T *di;
717 if (our_tv->vval.v_dict == NULL)
718 return NULL;
719
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200720 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200721 return NULL;
722
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200723 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200724 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200725 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200726 return NULL;
727 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200729 for (hi = ht->ht_array; todo > 0; ++hi)
730 {
731 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200732 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200733 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200734
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200735 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200736 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200737 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200738 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200739 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200740 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200741 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200742 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200743 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200744 Py_DECREF(newObj);
745 return NULL;
746 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200747 }
748 }
749 }
750 else
751 {
752 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200753 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200754 }
755
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200756 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200757}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200758
759 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200760VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200762 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200763 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200764 PyObject *string;
765 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200766 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200767 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200768
Bram Moolenaar389a1792013-06-23 13:00:44 +0200769 if (!PyArg_ParseTuple(args, "O", &string))
770 return NULL;
771
772 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773 return NULL;
774
775 Py_BEGIN_ALLOW_THREADS
776 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200777 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200778 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200779 Python_Release_Vim();
780 Py_END_ALLOW_THREADS
781
Bram Moolenaar389a1792013-06-23 13:00:44 +0200782 Py_XDECREF(todecref);
783
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200784 if (VimTryEnd())
785 return NULL;
786
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787 if (our_tv == NULL)
788 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200789 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200790 return NULL;
791 }
792
793 /* Convert the Vim type into a Python type. Create a dictionary that's
794 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200795 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200796 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200797 else
798 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200799 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200800 Py_DECREF(lookup_dict);
801 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200802
803
804 Py_BEGIN_ALLOW_THREADS
805 Python_Lock_Vim();
806 free_tv(our_tv);
807 Python_Release_Vim();
808 Py_END_ALLOW_THREADS
809
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200810 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200811}
812
Bram Moolenaardb913952012-06-29 12:54:53 +0200813static PyObject *ConvertToPyObject(typval_T *);
814
815 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200816VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200817{
Bram Moolenaardb913952012-06-29 12:54:53 +0200818 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200819 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200820 char_u *expr;
821 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200822
Bram Moolenaar389a1792013-06-23 13:00:44 +0200823 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200824 return NULL;
825
826 Py_BEGIN_ALLOW_THREADS
827 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200828 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200829 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200830 Python_Release_Vim();
831 Py_END_ALLOW_THREADS
832
Bram Moolenaar389a1792013-06-23 13:00:44 +0200833 Py_XDECREF(todecref);
834
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200835 if (VimTryEnd())
836 return NULL;
837
Bram Moolenaardb913952012-06-29 12:54:53 +0200838 if (our_tv == NULL)
839 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200840 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200841 return NULL;
842 }
843
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200844 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200845 Py_BEGIN_ALLOW_THREADS
846 Python_Lock_Vim();
847 free_tv(our_tv);
848 Python_Release_Vim();
849 Py_END_ALLOW_THREADS
850
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200851 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200852}
853
854 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200855VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200856{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200857 char_u *str;
858 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200859 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200860
Bram Moolenaar389a1792013-06-23 13:00:44 +0200861 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200862 return NULL;
863
Bram Moolenaara54bf402012-12-05 16:30:07 +0100864#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200865 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100866#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200867 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100868#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200869
870 Py_XDECREF(todecref);
871
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200872 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200873}
874
Bram Moolenaarf4258302013-06-02 18:20:17 +0200875 static PyObject *
876_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
877{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200878 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200879 PyObject *newwd;
880 PyObject *todecref;
881 char_u *new_dir;
882
Bram Moolenaard4209d22013-06-05 20:34:15 +0200883 if (_chdir == NULL)
884 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200885 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200886 return NULL;
887
888 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
889 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200890 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200891 return NULL;
892 }
893
894 if (!(new_dir = StringToChars(newwd, &todecref)))
895 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200896 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200897 Py_DECREF(newwd);
898 return NULL;
899 }
900
901 VimTryStart();
902
903 if (vim_chdir(new_dir))
904 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200905 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200906 Py_DECREF(newwd);
907 Py_XDECREF(todecref);
908
909 if (VimTryEnd())
910 return NULL;
911
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200912 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200913 return NULL;
914 }
915
916 Py_DECREF(newwd);
917 Py_XDECREF(todecref);
918
919 post_chdir(FALSE);
920
921 if (VimTryEnd())
922 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200923 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200924 return NULL;
925 }
926
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200927 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200928}
929
930 static PyObject *
931VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
932{
933 return _VimChdir(py_chdir, args, kwargs);
934}
935
936 static PyObject *
937VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
938{
939 return _VimChdir(py_fchdir, args, kwargs);
940}
941
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200942typedef struct {
943 PyObject *callable;
944 PyObject *result;
945} map_rtp_data;
946
947 static void
948map_rtp_callback(char_u *path, void *_data)
949{
950 void **data = (void **) _data;
951 PyObject *pathObject;
952 map_rtp_data *mr_data = *((map_rtp_data **) data);
953
954 if (!(pathObject = PyString_FromString((char *) path)))
955 {
956 *data = NULL;
957 return;
958 }
959
960 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
961 pathObject, NULL);
962
963 Py_DECREF(pathObject);
964
965 if (!mr_data->result || mr_data->result != Py_None)
966 *data = NULL;
967 else
968 {
969 Py_DECREF(mr_data->result);
970 mr_data->result = NULL;
971 }
972}
973
974 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200975VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200976{
977 map_rtp_data data;
978
Bram Moolenaar389a1792013-06-23 13:00:44 +0200979 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200980 data.result = NULL;
981
982 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
983
984 if (data.result == NULL)
985 {
986 if (PyErr_Occurred())
987 return NULL;
988 else
989 {
990 Py_INCREF(Py_None);
991 return Py_None;
992 }
993 }
994 return data.result;
995}
996
997/*
998 * _vim_runtimepath_ special path implementation.
999 */
1000
1001 static void
1002map_finder_callback(char_u *path, void *_data)
1003{
1004 void **data = (void **) _data;
1005 PyObject *list = *((PyObject **) data);
1006 PyObject *pathObject1, *pathObject2;
1007 char *pathbuf;
1008 size_t pathlen;
1009
1010 pathlen = STRLEN(path);
1011
1012#if PY_MAJOR_VERSION < 3
1013# define PY_MAIN_DIR_STRING "python2"
1014#else
1015# define PY_MAIN_DIR_STRING "python3"
1016#endif
1017#define PY_ALTERNATE_DIR_STRING "pythonx"
1018
1019#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1020 if (!(pathbuf = PyMem_New(char,
1021 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1022 {
1023 PyErr_NoMemory();
1024 *data = NULL;
1025 return;
1026 }
1027
1028 mch_memmove(pathbuf, path, pathlen + 1);
1029 add_pathsep((char_u *) pathbuf);
1030
1031 pathlen = STRLEN(pathbuf);
1032 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1033 PYTHONX_STRING_LENGTH + 1);
1034
1035 if (!(pathObject1 = PyString_FromString(pathbuf)))
1036 {
1037 *data = NULL;
1038 PyMem_Free(pathbuf);
1039 return;
1040 }
1041
1042 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1043 PYTHONX_STRING_LENGTH + 1);
1044
1045 if (!(pathObject2 = PyString_FromString(pathbuf)))
1046 {
1047 Py_DECREF(pathObject1);
1048 PyMem_Free(pathbuf);
1049 *data = NULL;
1050 return;
1051 }
1052
1053 PyMem_Free(pathbuf);
1054
1055 if (PyList_Append(list, pathObject1)
1056 || PyList_Append(list, pathObject2))
1057 *data = NULL;
1058
1059 Py_DECREF(pathObject1);
1060 Py_DECREF(pathObject2);
1061}
1062
1063 static PyObject *
1064Vim_GetPaths(PyObject *self UNUSED)
1065{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001066 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001067
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001068 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001069 return NULL;
1070
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001071 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001072
1073 if (PyErr_Occurred())
1074 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001075 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001076 return NULL;
1077 }
1078
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001079 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001080}
1081
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001082 static PyObject *
1083call_load_module(char *name, int len, PyObject *find_module_result)
1084{
1085 PyObject *fd, *pathname, *description;
1086
Bram Moolenaarc476e522013-06-23 13:46:40 +02001087 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001088 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001089 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001090 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001091 Py_TYPE_NAME(find_module_result));
1092 return NULL;
1093 }
1094 if (PyTuple_GET_SIZE(find_module_result) != 3)
1095 {
1096 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001097 N_("expected 3-tuple as imp.find_module() result, but got "
1098 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001099 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001100 return NULL;
1101 }
1102
1103 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1104 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1105 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1106 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001107 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001108 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001109 return NULL;
1110 }
1111
1112 return PyObject_CallFunction(py_load_module,
1113 "s#OOO", name, len, fd, pathname, description);
1114}
1115
1116 static PyObject *
1117find_module(char *fullname, char *tail, PyObject *new_path)
1118{
1119 PyObject *find_module_result;
1120 PyObject *module;
1121 char *dot;
1122
1123 if ((dot = (char *) vim_strchr((char_u *) tail, '.')))
1124 {
1125 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001126 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001127 * first component
1128 */
1129 PyObject *newest_path;
1130 int partlen = (int) (dot - 1 - tail);
1131
1132 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1133 "s#O", tail, partlen, new_path)))
1134 return NULL;
1135
1136 if (!(module = call_load_module(
1137 fullname,
1138 ((int) (tail - fullname)) + partlen,
1139 find_module_result)))
1140 {
1141 Py_DECREF(find_module_result);
1142 return NULL;
1143 }
1144
1145 Py_DECREF(find_module_result);
1146
1147 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1148 {
1149 Py_DECREF(module);
1150 return NULL;
1151 }
1152
1153 Py_DECREF(module);
1154
1155 module = find_module(fullname, dot + 1, newest_path);
1156
1157 Py_DECREF(newest_path);
1158
1159 return module;
1160 }
1161 else
1162 {
1163 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1164 "sO", tail, new_path)))
1165 return NULL;
1166
1167 if (!(module = call_load_module(
1168 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001169 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001170 find_module_result)))
1171 {
1172 Py_DECREF(find_module_result);
1173 return NULL;
1174 }
1175
1176 Py_DECREF(find_module_result);
1177
1178 return module;
1179 }
1180}
1181
1182 static PyObject *
1183FinderFindModule(PyObject *self, PyObject *args)
1184{
1185 char *fullname;
1186 PyObject *module;
1187 PyObject *new_path;
1188 LoaderObject *loader;
1189
1190 if (!PyArg_ParseTuple(args, "s", &fullname))
1191 return NULL;
1192
1193 if (!(new_path = Vim_GetPaths(self)))
1194 return NULL;
1195
1196 module = find_module(fullname, fullname, new_path);
1197
1198 Py_DECREF(new_path);
1199
1200 if (!module)
1201 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001202 if (PyErr_Occurred())
1203 {
1204 if (PyErr_ExceptionMatches(PyExc_ImportError))
1205 PyErr_Clear();
1206 else
1207 return NULL;
1208 }
1209
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001210 Py_INCREF(Py_None);
1211 return Py_None;
1212 }
1213
1214 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1215 {
1216 Py_DECREF(module);
1217 return NULL;
1218 }
1219
1220 loader->module = module;
1221
1222 return (PyObject *) loader;
1223}
1224
1225 static PyObject *
1226VimPathHook(PyObject *self UNUSED, PyObject *args)
1227{
1228 char *path;
1229
1230 if (PyArg_ParseTuple(args, "s", &path)
1231 && STRCMP(path, vim_special_path) == 0)
1232 {
1233 Py_INCREF(vim_module);
1234 return vim_module;
1235 }
1236
1237 PyErr_Clear();
1238 PyErr_SetNone(PyExc_ImportError);
1239 return NULL;
1240}
1241
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001242/*
1243 * Vim module - Definitions
1244 */
1245
1246static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001247 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001248 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001249 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001250 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1251 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001252 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1253 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001254 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001255 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001256 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1257 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1258 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001259};
1260
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001261/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001262 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001263 */
1264
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001265static PyTypeObject IterType;
1266
1267typedef PyObject *(*nextfun)(void **);
1268typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001269typedef int (*traversefun)(void *, visitproc, void *);
1270typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001271
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001272/* Main purpose of this object is removing the need for do python
1273 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1274 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001275
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001276typedef struct
1277{
1278 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001279 void *cur;
1280 nextfun next;
1281 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001282 traversefun traverse;
1283 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001284} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001285
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001286 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001287IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1288 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001289{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001290 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001291
Bram Moolenaar774267b2013-05-21 20:51:59 +02001292 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001293 self->cur = start;
1294 self->next = next;
1295 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001296 self->traverse = traverse;
1297 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001298
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001299 return (PyObject *)(self);
1300}
1301
1302 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001303IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001304{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001305 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001306 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001307 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001308}
1309
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001310 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001311IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001312{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001313 if (self->traverse != NULL)
1314 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001315 else
1316 return 0;
1317}
1318
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001319/* Mac OSX defines clear() somewhere. */
1320#ifdef clear
1321# undef clear
1322#endif
1323
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001324 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001325IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001326{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001327 if (self->clear != NULL)
1328 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001329 else
1330 return 0;
1331}
1332
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001333 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001334IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001335{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001336 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001337}
1338
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001339 static PyObject *
1340IterIter(PyObject *self)
1341{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001342 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001343 return self;
1344}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001345
Bram Moolenaardb913952012-06-29 12:54:53 +02001346typedef struct pylinkedlist_S {
1347 struct pylinkedlist_S *pll_next;
1348 struct pylinkedlist_S *pll_prev;
1349 PyObject *pll_obj;
1350} pylinkedlist_T;
1351
1352static pylinkedlist_T *lastdict = NULL;
1353static pylinkedlist_T *lastlist = NULL;
1354
1355 static void
1356pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1357{
1358 if (ref->pll_prev == NULL)
1359 {
1360 if (ref->pll_next == NULL)
1361 {
1362 *last = NULL;
1363 return;
1364 }
1365 }
1366 else
1367 ref->pll_prev->pll_next = ref->pll_next;
1368
1369 if (ref->pll_next == NULL)
1370 *last = ref->pll_prev;
1371 else
1372 ref->pll_next->pll_prev = ref->pll_prev;
1373}
1374
1375 static void
1376pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1377{
1378 if (*last == NULL)
1379 ref->pll_prev = NULL;
1380 else
1381 {
1382 (*last)->pll_next = ref;
1383 ref->pll_prev = *last;
1384 }
1385 ref->pll_next = NULL;
1386 ref->pll_obj = self;
1387 *last = ref;
1388}
1389
1390static PyTypeObject DictionaryType;
1391
1392typedef struct
1393{
1394 PyObject_HEAD
1395 dict_T *dict;
1396 pylinkedlist_T ref;
1397} DictionaryObject;
1398
Bram Moolenaara9922d62013-05-30 13:01:18 +02001399static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1400
1401#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1402
Bram Moolenaardb913952012-06-29 12:54:53 +02001403 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001404DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001405{
1406 DictionaryObject *self;
1407
Bram Moolenaara9922d62013-05-30 13:01:18 +02001408 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001409 if (self == NULL)
1410 return NULL;
1411 self->dict = dict;
1412 ++dict->dv_refcount;
1413
1414 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1415
1416 return (PyObject *)(self);
1417}
1418
Bram Moolenaara9922d62013-05-30 13:01:18 +02001419 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001420py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001421{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001422 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001423
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001424 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001425 {
1426 PyErr_NoMemory();
1427 return NULL;
1428 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001429 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001430
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001431 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001432}
1433
1434 static PyObject *
1435DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1436{
1437 DictionaryObject *self;
1438 dict_T *dict;
1439
1440 if (!(dict = py_dict_alloc()))
1441 return NULL;
1442
1443 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1444
1445 --dict->dv_refcount;
1446
1447 if (kwargs || PyTuple_Size(args))
1448 {
1449 PyObject *tmp;
1450 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1451 {
1452 Py_DECREF(self);
1453 return NULL;
1454 }
1455
1456 Py_DECREF(tmp);
1457 }
1458
1459 return (PyObject *)(self);
1460}
1461
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001462 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001463DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001464{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001465 pyll_remove(&self->ref, &lastdict);
1466 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001467
1468 DESTRUCTOR_FINISH(self);
1469}
1470
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001471static char *DictionaryAttrs[] = {
1472 "locked", "scope",
1473 NULL
1474};
1475
1476 static PyObject *
1477DictionaryDir(PyObject *self)
1478{
1479 return ObjectDir(self, DictionaryAttrs);
1480}
1481
Bram Moolenaardb913952012-06-29 12:54:53 +02001482 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001483DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001484{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001485 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001486 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001487 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001488 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001489 return -1;
1490 }
1491
1492 if (strcmp(name, "locked") == 0)
1493 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001494 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001495 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001496 PyErr_SET_STRING(PyExc_TypeError,
1497 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001498 return -1;
1499 }
1500 else
1501 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001502 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001503 if (istrue == -1)
1504 return -1;
1505 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001506 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001507 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001508 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001509 }
1510 return 0;
1511 }
1512 else
1513 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001514 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001515 return -1;
1516 }
1517}
1518
1519 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001520DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001521{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001522 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001523}
1524
Bram Moolenaara9922d62013-05-30 13:01:18 +02001525#define DICT_FLAG_HAS_DEFAULT 0x01
1526#define DICT_FLAG_POP 0x02
1527#define DICT_FLAG_NONE_DEFAULT 0x04
1528#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1529#define DICT_FLAG_RETURN_PAIR 0x10
1530
Bram Moolenaardb913952012-06-29 12:54:53 +02001531 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001532_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001533{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001534 PyObject *keyObject;
1535 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001536 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001537 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001538 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001539 dict_T *dict = self->dict;
1540 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001541 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001542
Bram Moolenaara9922d62013-05-30 13:01:18 +02001543 if (flags & DICT_FLAG_HAS_DEFAULT)
1544 {
1545 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1546 return NULL;
1547 }
1548 else
1549 keyObject = args;
1550
1551 if (flags & DICT_FLAG_RETURN_BOOL)
1552 defObject = Py_False;
1553
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001554 if (!(key = StringToChars(keyObject, &todecref)))
1555 return NULL;
1556
1557 if (*key == NUL)
1558 {
1559 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001560 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001561 return NULL;
1562 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001563
Bram Moolenaara9922d62013-05-30 13:01:18 +02001564 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001565
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001566 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001567
Bram Moolenaara9922d62013-05-30 13:01:18 +02001568 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001569 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001570 if (defObject)
1571 {
1572 Py_INCREF(defObject);
1573 return defObject;
1574 }
1575 else
1576 {
1577 PyErr_SetObject(PyExc_KeyError, keyObject);
1578 return NULL;
1579 }
1580 }
1581 else if (flags & DICT_FLAG_RETURN_BOOL)
1582 {
1583 Py_INCREF(Py_True);
1584 return Py_True;
1585 }
1586
1587 di = dict_lookup(hi);
1588
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001589 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001590 return NULL;
1591
1592 if (flags & DICT_FLAG_POP)
1593 {
1594 if (dict->dv_lock)
1595 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001596 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001597 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001598 return NULL;
1599 }
1600
1601 hash_remove(&dict->dv_hashtab, hi);
1602 dictitem_free(di);
1603 }
1604
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001605 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001606}
1607
1608 static PyObject *
1609DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1610{
1611 return _DictionaryItem(self, keyObject, 0);
1612}
1613
1614 static int
1615DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1616{
1617 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001618 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001619
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001620 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001621
Bram Moolenaardee2e312013-06-23 16:35:47 +02001622 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001623
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001624 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001625}
1626
1627typedef struct
1628{
1629 hashitem_T *ht_array;
1630 long_u ht_used;
1631 hashtab_T *ht;
1632 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001633 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001634} dictiterinfo_T;
1635
1636 static PyObject *
1637DictionaryIterNext(dictiterinfo_T **dii)
1638{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001639 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001640
1641 if (!(*dii)->todo)
1642 return NULL;
1643
1644 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1645 (*dii)->ht->ht_used != (*dii)->ht_used)
1646 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001647 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001648 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001649 return NULL;
1650 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001651
Bram Moolenaara9922d62013-05-30 13:01:18 +02001652 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1653 ++((*dii)->hi);
1654
1655 --((*dii)->todo);
1656
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001657 if (!(ret = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001658 return NULL;
1659
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001660 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001661}
1662
1663 static PyObject *
1664DictionaryIter(DictionaryObject *self)
1665{
1666 dictiterinfo_T *dii;
1667 hashtab_T *ht;
1668
1669 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1670 {
1671 PyErr_NoMemory();
1672 return NULL;
1673 }
1674
1675 ht = &self->dict->dv_hashtab;
1676 dii->ht_array = ht->ht_array;
1677 dii->ht_used = ht->ht_used;
1678 dii->ht = ht;
1679 dii->hi = dii->ht_array;
1680 dii->todo = dii->ht_used;
1681
1682 return IterNew(dii,
1683 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1684 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001685}
1686
1687 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001688DictionaryAssItem(
1689 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001690{
1691 char_u *key;
1692 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001693 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001694 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001695 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001696
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001697 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001698 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001699 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001700 return -1;
1701 }
1702
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001703 if (!(key = StringToChars(keyObject, &todecref)))
1704 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001705
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001706 if (*key == NUL)
1707 {
1708 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001709 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001710 return -1;
1711 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001712
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001713 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001714
1715 if (valObject == NULL)
1716 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001717 hashitem_T *hi;
1718
Bram Moolenaardb913952012-06-29 12:54:53 +02001719 if (di == NULL)
1720 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001721 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001722 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001723 return -1;
1724 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001725 hi = hash_find(&dict->dv_hashtab, di->di_key);
1726 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001727 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001728 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001729 return 0;
1730 }
1731
1732 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001733 {
1734 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001735 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001736 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001737
1738 if (di == NULL)
1739 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001740 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001741 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001742 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001743 PyErr_NoMemory();
1744 return -1;
1745 }
1746 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001747 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001748
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001749 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001750 {
1751 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001752 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001753 RAISE_KEY_ADD_FAIL(key);
1754 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001755 return -1;
1756 }
1757 }
1758 else
1759 clear_tv(&di->di_tv);
1760
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001761 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001762
1763 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001764 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001765 return 0;
1766}
1767
Bram Moolenaara9922d62013-05-30 13:01:18 +02001768typedef PyObject *(*hi_to_py)(hashitem_T *);
1769
Bram Moolenaardb913952012-06-29 12:54:53 +02001770 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001771DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001772{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001773 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001774 long_u todo = dict->dv_hashtab.ht_used;
1775 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001776 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001777 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001778 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001779
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001780 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001781 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1782 {
1783 if (!HASHITEM_EMPTY(hi))
1784 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001785 if (!(newObj = hiconvert(hi)))
1786 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001787 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001788 return NULL;
1789 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001790 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001791 --todo;
1792 ++i;
1793 }
1794 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001795 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001796}
1797
Bram Moolenaara9922d62013-05-30 13:01:18 +02001798 static PyObject *
1799dict_key(hashitem_T *hi)
1800{
1801 return PyBytes_FromString((char *)(hi->hi_key));
1802}
1803
1804 static PyObject *
1805DictionaryListKeys(DictionaryObject *self)
1806{
1807 return DictionaryListObjects(self, dict_key);
1808}
1809
1810 static PyObject *
1811dict_val(hashitem_T *hi)
1812{
1813 dictitem_T *di;
1814
1815 di = dict_lookup(hi);
1816 return ConvertToPyObject(&di->di_tv);
1817}
1818
1819 static PyObject *
1820DictionaryListValues(DictionaryObject *self)
1821{
1822 return DictionaryListObjects(self, dict_val);
1823}
1824
1825 static PyObject *
1826dict_item(hashitem_T *hi)
1827{
1828 PyObject *keyObject;
1829 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001830 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001831
1832 if (!(keyObject = dict_key(hi)))
1833 return NULL;
1834
1835 if (!(valObject = dict_val(hi)))
1836 {
1837 Py_DECREF(keyObject);
1838 return NULL;
1839 }
1840
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001841 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001842
1843 Py_DECREF(keyObject);
1844 Py_DECREF(valObject);
1845
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001846 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001847}
1848
1849 static PyObject *
1850DictionaryListItems(DictionaryObject *self)
1851{
1852 return DictionaryListObjects(self, dict_item);
1853}
1854
1855 static PyObject *
1856DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1857{
1858 dict_T *dict = self->dict;
1859
1860 if (dict->dv_lock)
1861 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001862 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001863 return NULL;
1864 }
1865
1866 if (kwargs)
1867 {
1868 typval_T tv;
1869
1870 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1871 return NULL;
1872
1873 VimTryStart();
1874 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1875 clear_tv(&tv);
1876 if (VimTryEnd())
1877 return NULL;
1878 }
1879 else
1880 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001881 PyObject *obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001882
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001883 if (!PyArg_ParseTuple(args, "O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001884 return NULL;
1885
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001886 if (PyObject_HasAttrString(obj, "keys"))
1887 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001888 else
1889 {
1890 PyObject *iterator;
1891 PyObject *item;
1892
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001893 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001894 return NULL;
1895
1896 while ((item = PyIter_Next(iterator)))
1897 {
1898 PyObject *fast;
1899 PyObject *keyObject;
1900 PyObject *valObject;
1901 PyObject *todecref;
1902 char_u *key;
1903 dictitem_T *di;
1904
1905 if (!(fast = PySequence_Fast(item, "")))
1906 {
1907 Py_DECREF(iterator);
1908 Py_DECREF(item);
1909 return NULL;
1910 }
1911
1912 Py_DECREF(item);
1913
1914 if (PySequence_Fast_GET_SIZE(fast) != 2)
1915 {
1916 Py_DECREF(iterator);
1917 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001918 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001919 N_("expected sequence element of size 2, "
1920 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001921 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001922 return NULL;
1923 }
1924
1925 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1926
1927 if (!(key = StringToChars(keyObject, &todecref)))
1928 {
1929 Py_DECREF(iterator);
1930 Py_DECREF(fast);
1931 return NULL;
1932 }
1933
1934 di = dictitem_alloc(key);
1935
1936 Py_XDECREF(todecref);
1937
1938 if (di == NULL)
1939 {
1940 Py_DECREF(fast);
1941 Py_DECREF(iterator);
1942 PyErr_NoMemory();
1943 return NULL;
1944 }
1945 di->di_tv.v_lock = 0;
1946 di->di_tv.v_type = VAR_UNKNOWN;
1947
1948 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1949
1950 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1951 {
1952 Py_DECREF(iterator);
1953 Py_DECREF(fast);
1954 dictitem_free(di);
1955 return NULL;
1956 }
1957
1958 Py_DECREF(fast);
1959
1960 if (dict_add(dict, di) == FAIL)
1961 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001962 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001963 Py_DECREF(iterator);
1964 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001965 return NULL;
1966 }
1967 }
1968
1969 Py_DECREF(iterator);
1970
1971 /* Iterator may have finished due to an exception */
1972 if (PyErr_Occurred())
1973 return NULL;
1974 }
1975 }
1976 Py_INCREF(Py_None);
1977 return Py_None;
1978}
1979
1980 static PyObject *
1981DictionaryGet(DictionaryObject *self, PyObject *args)
1982{
1983 return _DictionaryItem(self, args,
1984 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1985}
1986
1987 static PyObject *
1988DictionaryPop(DictionaryObject *self, PyObject *args)
1989{
1990 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1991}
1992
1993 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001994DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001995{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001996 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001997 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02001998 PyObject *valObject;
1999 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002000
Bram Moolenaarde71b562013-06-02 17:41:54 +02002001 if (self->dict->dv_hashtab.ht_used == 0)
2002 {
2003 PyErr_SetNone(PyExc_KeyError);
2004 return NULL;
2005 }
2006
2007 hi = self->dict->dv_hashtab.ht_array;
2008 while (HASHITEM_EMPTY(hi))
2009 ++hi;
2010
2011 di = dict_lookup(hi);
2012
2013 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002014 return NULL;
2015
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002016 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002017 {
2018 Py_DECREF(valObject);
2019 return NULL;
2020 }
2021
2022 hash_remove(&self->dict->dv_hashtab, hi);
2023 dictitem_free(di);
2024
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002025 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002026}
2027
2028 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002029DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002030{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002031 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2032}
2033
2034static PySequenceMethods DictionaryAsSeq = {
2035 0, /* sq_length */
2036 0, /* sq_concat */
2037 0, /* sq_repeat */
2038 0, /* sq_item */
2039 0, /* sq_slice */
2040 0, /* sq_ass_item */
2041 0, /* sq_ass_slice */
2042 (objobjproc) DictionaryContains, /* sq_contains */
2043 0, /* sq_inplace_concat */
2044 0, /* sq_inplace_repeat */
2045};
2046
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002047static PyMappingMethods DictionaryAsMapping = {
2048 (lenfunc) DictionaryLength,
2049 (binaryfunc) DictionaryItem,
2050 (objobjargproc) DictionaryAssItem,
2051};
2052
Bram Moolenaardb913952012-06-29 12:54:53 +02002053static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002054 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002055 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2056 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2057 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2058 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2059 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002060 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002061 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002062 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2063 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002064};
2065
2066static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002067static PySequenceMethods ListAsSeq;
2068static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02002069
2070typedef struct
2071{
2072 PyObject_HEAD
2073 list_T *list;
2074 pylinkedlist_T ref;
2075} ListObject;
2076
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002077#define NEW_LIST(list) ListNew(&ListType, list)
2078
Bram Moolenaardb913952012-06-29 12:54:53 +02002079 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002080ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002081{
2082 ListObject *self;
2083
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002084 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002085 if (self == NULL)
2086 return NULL;
2087 self->list = list;
2088 ++list->lv_refcount;
2089
2090 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2091
2092 return (PyObject *)(self);
2093}
2094
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002095 static list_T *
2096py_list_alloc()
2097{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002098 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002099
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002100 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002101 {
2102 PyErr_NoMemory();
2103 return NULL;
2104 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002105 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002106
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002107 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002108}
2109
2110 static int
2111list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2112{
2113 PyObject *iterator;
2114 PyObject *item;
2115 listitem_T *li;
2116
2117 if (!(iterator = PyObject_GetIter(obj)))
2118 return -1;
2119
2120 while ((item = PyIter_Next(iterator)))
2121 {
2122 if (!(li = listitem_alloc()))
2123 {
2124 PyErr_NoMemory();
2125 Py_DECREF(item);
2126 Py_DECREF(iterator);
2127 return -1;
2128 }
2129 li->li_tv.v_lock = 0;
2130 li->li_tv.v_type = VAR_UNKNOWN;
2131
2132 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2133 {
2134 Py_DECREF(item);
2135 Py_DECREF(iterator);
2136 listitem_free(li);
2137 return -1;
2138 }
2139
2140 Py_DECREF(item);
2141
2142 list_append(l, li);
2143 }
2144
2145 Py_DECREF(iterator);
2146
2147 /* Iterator may have finished due to an exception */
2148 if (PyErr_Occurred())
2149 return -1;
2150
2151 return 0;
2152}
2153
2154 static PyObject *
2155ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2156{
2157 list_T *list;
2158 PyObject *obj = NULL;
2159
2160 if (kwargs)
2161 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002162 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002163 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002164 return NULL;
2165 }
2166
2167 if (!PyArg_ParseTuple(args, "|O", &obj))
2168 return NULL;
2169
2170 if (!(list = py_list_alloc()))
2171 return NULL;
2172
2173 if (obj)
2174 {
2175 PyObject *lookup_dict;
2176
2177 if (!(lookup_dict = PyDict_New()))
2178 {
2179 list_unref(list);
2180 return NULL;
2181 }
2182
2183 if (list_py_concat(list, obj, lookup_dict) == -1)
2184 {
2185 Py_DECREF(lookup_dict);
2186 list_unref(list);
2187 return NULL;
2188 }
2189
2190 Py_DECREF(lookup_dict);
2191 }
2192
2193 return ListNew(subtype, list);
2194}
2195
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002196 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002197ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002198{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002199 pyll_remove(&self->ref, &lastlist);
2200 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002201
2202 DESTRUCTOR_FINISH(self);
2203}
2204
Bram Moolenaardb913952012-06-29 12:54:53 +02002205 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002206ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002207{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002208 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002209}
2210
2211 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002212ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002213{
2214 listitem_T *li;
2215
Bram Moolenaard6e39182013-05-21 18:30:34 +02002216 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002217 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002218 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002219 return NULL;
2220 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002221 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002222 if (li == NULL)
2223 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002224 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002225 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002226 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002227 return NULL;
2228 }
2229 return ConvertToPyObject(&li->li_tv);
2230}
2231
2232#define PROC_RANGE \
2233 if (last < 0) {\
2234 if (last < -size) \
2235 last = 0; \
2236 else \
2237 last += size; \
2238 } \
2239 if (first < 0) \
2240 first = 0; \
2241 if (first > size) \
2242 first = size; \
2243 if (last > size) \
2244 last = size;
2245
2246 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002247ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02002248{
2249 PyInt i;
2250 PyInt size = ListLength(self);
2251 PyInt n;
2252 PyObject *list;
2253 int reversed = 0;
2254
2255 PROC_RANGE
2256 if (first >= last)
2257 first = last;
2258
2259 n = last-first;
2260 list = PyList_New(n);
2261 if (list == NULL)
2262 return NULL;
2263
2264 for (i = 0; i < n; ++i)
2265 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02002266 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02002267 if (item == NULL)
2268 {
2269 Py_DECREF(list);
2270 return NULL;
2271 }
2272
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02002273 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002274 }
2275
2276 return list;
2277}
2278
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002279typedef struct
2280{
2281 listwatch_T lw;
2282 list_T *list;
2283} listiterinfo_T;
2284
2285 static void
2286ListIterDestruct(listiterinfo_T *lii)
2287{
2288 list_rem_watch(lii->list, &lii->lw);
2289 PyMem_Free(lii);
2290}
2291
2292 static PyObject *
2293ListIterNext(listiterinfo_T **lii)
2294{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002295 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002296
2297 if (!((*lii)->lw.lw_item))
2298 return NULL;
2299
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002300 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002301 return NULL;
2302
2303 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2304
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002305 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002306}
2307
2308 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002309ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002310{
2311 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002312 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002313
2314 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2315 {
2316 PyErr_NoMemory();
2317 return NULL;
2318 }
2319
2320 list_add_watch(l, &lii->lw);
2321 lii->lw.lw_item = l->lv_first;
2322 lii->list = l;
2323
2324 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002325 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2326 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002327}
2328
Bram Moolenaardb913952012-06-29 12:54:53 +02002329 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002330ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002331{
2332 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002333 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002334 listitem_T *li;
2335 Py_ssize_t length = ListLength(self);
2336
2337 if (l->lv_lock)
2338 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002339 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002340 return -1;
2341 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002342 if (index > length || (index == length && obj == NULL))
Bram Moolenaardb913952012-06-29 12:54:53 +02002343 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002344 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002345 return -1;
2346 }
2347
2348 if (obj == NULL)
2349 {
2350 li = list_find(l, (long) index);
2351 list_remove(l, li, li);
2352 clear_tv(&li->li_tv);
2353 vim_free(li);
2354 return 0;
2355 }
2356
2357 if (ConvertFromPyObject(obj, &tv) == -1)
2358 return -1;
2359
2360 if (index == length)
2361 {
2362 if (list_append_tv(l, &tv) == FAIL)
2363 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002364 clear_tv(&tv);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002365 PyErr_SET_VIM(N_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002366 return -1;
2367 }
2368 }
2369 else
2370 {
2371 li = list_find(l, (long) index);
2372 clear_tv(&li->li_tv);
2373 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002374 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002375 }
2376 return 0;
2377}
2378
2379 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002380ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002381{
2382 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002383 PyObject *iterator;
2384 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002385 listitem_T *li;
2386 listitem_T *next;
2387 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002388 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002389 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002390
2391 if (l->lv_lock)
2392 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002393 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002394 return -1;
2395 }
2396
2397 PROC_RANGE
2398
2399 if (first == size)
2400 li = NULL;
2401 else
2402 {
2403 li = list_find(l, (long) first);
2404 if (li == NULL)
2405 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002406 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2407 (int)first);
Bram Moolenaardb913952012-06-29 12:54:53 +02002408 return -1;
2409 }
2410 if (last > first)
2411 {
2412 i = last - first;
2413 while (i-- && li != NULL)
2414 {
2415 next = li->li_next;
2416 listitem_remove(l, li);
2417 li = next;
2418 }
2419 }
2420 }
2421
2422 if (obj == NULL)
2423 return 0;
2424
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002425 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002426 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002427
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002428 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002429 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002430 if (ConvertFromPyObject(item, &v) == -1)
2431 {
2432 Py_DECREF(iterator);
2433 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002434 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002435 }
2436 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002437 if (list_insert_tv(l, &v, li) == FAIL)
2438 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002439 clear_tv(&v);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002440 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002441 return -1;
2442 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002443 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002444 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002445 Py_DECREF(iterator);
Bram Moolenaardee2e312013-06-23 16:35:47 +02002446
2447 if (PyErr_Occurred())
2448 return -1;
2449
Bram Moolenaardb913952012-06-29 12:54:53 +02002450 return 0;
2451}
2452
2453 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002454ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002455{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002456 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002457 PyObject *lookup_dict;
2458
2459 if (l->lv_lock)
2460 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002461 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002462 return NULL;
2463 }
2464
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002465 if (!(lookup_dict = PyDict_New()))
2466 return NULL;
2467
Bram Moolenaardb913952012-06-29 12:54:53 +02002468 if (list_py_concat(l, obj, lookup_dict) == -1)
2469 {
2470 Py_DECREF(lookup_dict);
2471 return NULL;
2472 }
2473 Py_DECREF(lookup_dict);
2474
2475 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002476 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002477}
2478
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002479static char *ListAttrs[] = {
2480 "locked",
2481 NULL
2482};
2483
2484 static PyObject *
2485ListDir(PyObject *self)
2486{
2487 return ObjectDir(self, ListAttrs);
2488}
2489
Bram Moolenaar66b79852012-09-21 14:00:35 +02002490 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002491ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002492{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002493 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002494 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002495 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002496 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002497 return -1;
2498 }
2499
2500 if (strcmp(name, "locked") == 0)
2501 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002502 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002503 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002504 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002505 return -1;
2506 }
2507 else
2508 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002509 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002510 if (istrue == -1)
2511 return -1;
2512 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002513 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002514 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002515 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002516 }
2517 return 0;
2518 }
2519 else
2520 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002521 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002522 return -1;
2523 }
2524}
2525
Bram Moolenaardb913952012-06-29 12:54:53 +02002526static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002527 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2528 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2529 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002530};
2531
2532typedef struct
2533{
2534 PyObject_HEAD
2535 char_u *name;
2536} FunctionObject;
2537
2538static PyTypeObject FunctionType;
2539
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002540#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2541
Bram Moolenaardb913952012-06-29 12:54:53 +02002542 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002543FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002544{
2545 FunctionObject *self;
2546
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002547 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2548
Bram Moolenaardb913952012-06-29 12:54:53 +02002549 if (self == NULL)
2550 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002551
2552 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002553 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002554 if (!translated_function_exists(name))
2555 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002556 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002557 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002558 return NULL;
2559 }
2560 self->name = vim_strsave(name);
2561 func_ref(self->name);
2562 }
2563 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002564 if ((self->name = get_expanded_name(name,
2565 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2566 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002567 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002568 PyErr_FORMAT(PyExc_ValueError,
2569 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002570 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002571 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002572
2573 return (PyObject *)(self);
2574}
2575
2576 static PyObject *
2577FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2578{
2579 PyObject *self;
2580 char_u *name;
2581
2582 if (kwargs)
2583 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002584 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002585 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002586 return NULL;
2587 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002588
Bram Moolenaar389a1792013-06-23 13:00:44 +02002589 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002590 return NULL;
2591
2592 self = FunctionNew(subtype, name);
2593
Bram Moolenaar389a1792013-06-23 13:00:44 +02002594 PyMem_Free(name);
2595
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002596 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002597}
2598
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002599 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002600FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002601{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002602 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002603 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002604
2605 DESTRUCTOR_FINISH(self);
2606}
2607
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002608static char *FunctionAttrs[] = {
2609 "softspace",
2610 NULL
2611};
2612
2613 static PyObject *
2614FunctionDir(PyObject *self)
2615{
2616 return ObjectDir(self, FunctionAttrs);
2617}
2618
Bram Moolenaardb913952012-06-29 12:54:53 +02002619 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002620FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002621{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002622 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002623 typval_T args;
2624 typval_T selfdicttv;
2625 typval_T rettv;
2626 dict_T *selfdict = NULL;
2627 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002628 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002629 int error;
2630
2631 if (ConvertFromPyObject(argsObject, &args) == -1)
2632 return NULL;
2633
2634 if (kwargs != NULL)
2635 {
2636 selfdictObject = PyDict_GetItemString(kwargs, "self");
2637 if (selfdictObject != NULL)
2638 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002639 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002640 {
2641 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002642 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002643 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002644 selfdict = selfdicttv.vval.v_dict;
2645 }
2646 }
2647
Bram Moolenaar71700b82013-05-15 17:49:05 +02002648 Py_BEGIN_ALLOW_THREADS
2649 Python_Lock_Vim();
2650
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002651 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002652 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002653
2654 Python_Release_Vim();
2655 Py_END_ALLOW_THREADS
2656
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002657 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002658 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002659 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002660 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002661 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002662 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002663 }
2664 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002665 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002666
Bram Moolenaardb913952012-06-29 12:54:53 +02002667 clear_tv(&args);
2668 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002669 if (selfdict != NULL)
2670 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002671
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002672 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002673}
2674
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002675 static PyObject *
2676FunctionRepr(FunctionObject *self)
2677{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002678#ifdef Py_TRACE_REFS
2679 /* For unknown reason self->name may be NULL after calling
2680 * Finalize */
2681 return PyString_FromFormat("<vim.Function '%s'>",
2682 (self->name == NULL ? "<NULL>" : (char *) self->name));
2683#else
2684 return PyString_FromFormat("<vim.Function '%s'>", (char *) self->name);
2685#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002686}
2687
Bram Moolenaardb913952012-06-29 12:54:53 +02002688static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002689 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2690 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002691};
2692
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002693/*
2694 * Options object
2695 */
2696
2697static PyTypeObject OptionsType;
2698
2699typedef int (*checkfun)(void *);
2700
2701typedef struct
2702{
2703 PyObject_HEAD
2704 int opt_type;
2705 void *from;
2706 checkfun Check;
2707 PyObject *fromObj;
2708} OptionsObject;
2709
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002710 static int
2711dummy_check(void *arg UNUSED)
2712{
2713 return 0;
2714}
2715
2716 static PyObject *
2717OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2718{
2719 OptionsObject *self;
2720
Bram Moolenaar774267b2013-05-21 20:51:59 +02002721 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002722 if (self == NULL)
2723 return NULL;
2724
2725 self->opt_type = opt_type;
2726 self->from = from;
2727 self->Check = Check;
2728 self->fromObj = fromObj;
2729 if (fromObj)
2730 Py_INCREF(fromObj);
2731
2732 return (PyObject *)(self);
2733}
2734
2735 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002736OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002737{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002738 PyObject_GC_UnTrack((void *)(self));
2739 Py_XDECREF(self->fromObj);
2740 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002741}
2742
2743 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002744OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002745{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002746 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002747 return 0;
2748}
2749
2750 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002751OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002752{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002753 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002754 return 0;
2755}
2756
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002757 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002758OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002759{
2760 char_u *key;
2761 int flags;
2762 long numval;
2763 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002764 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002765
Bram Moolenaard6e39182013-05-21 18:30:34 +02002766 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002767 return NULL;
2768
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002769 if (!(key = StringToChars(keyObject, &todecref)))
2770 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002771
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002772 if (*key == NUL)
2773 {
2774 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002775 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002776 return NULL;
2777 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002778
2779 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002780 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002781
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002782 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002783
2784 if (flags == 0)
2785 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002786 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002787 return NULL;
2788 }
2789
2790 if (flags & SOPT_UNSET)
2791 {
2792 Py_INCREF(Py_None);
2793 return Py_None;
2794 }
2795 else if (flags & SOPT_BOOL)
2796 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002797 PyObject *ret;
2798 ret = numval ? Py_True : Py_False;
2799 Py_INCREF(ret);
2800 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002801 }
2802 else if (flags & SOPT_NUM)
2803 return PyInt_FromLong(numval);
2804 else if (flags & SOPT_STRING)
2805 {
2806 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002807 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002808 PyObject *ret = PyBytes_FromString((char *) stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002809 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002810 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002811 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002812 else
2813 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002814 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002815 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002816 return NULL;
2817 }
2818 }
2819 else
2820 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002821 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002822 return NULL;
2823 }
2824}
2825
2826 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002827set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002828{
2829 char_u *errmsg;
2830
2831 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2832 {
2833 if (VimTryEnd())
2834 return FAIL;
2835 PyErr_SetVim((char *)errmsg);
2836 return FAIL;
2837 }
2838 return OK;
2839}
2840
2841 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002842set_option_value_for(
2843 char_u *key,
2844 int numval,
2845 char_u *stringval,
2846 int opt_flags,
2847 int opt_type,
2848 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002849{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002850 win_T *save_curwin = NULL;
2851 tabpage_T *save_curtab = NULL;
2852 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002853 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002854
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002855 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002856 switch (opt_type)
2857 {
2858 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002859 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002860 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002861 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002862 if (VimTryEnd())
2863 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002864 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002865 return -1;
2866 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002867 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
2868 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002869 break;
2870 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002871 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002872 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002873 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002874 break;
2875 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002876 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002877 break;
2878 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002879 if (set_ret == FAIL)
2880 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002881 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002882}
2883
2884 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002885OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002886{
2887 char_u *key;
2888 int flags;
2889 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002890 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002891 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002892
Bram Moolenaard6e39182013-05-21 18:30:34 +02002893 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002894 return -1;
2895
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002896 if (!(key = StringToChars(keyObject, &todecref)))
2897 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002898
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002899 if (*key == NUL)
2900 {
2901 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002902 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002903 return -1;
2904 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002905
2906 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002907 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002908
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002909 if (flags == 0)
2910 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002911 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002912 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002913 return -1;
2914 }
2915
2916 if (valObject == NULL)
2917 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002918 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002919 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002920 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002921 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002922 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002923 return -1;
2924 }
2925 else if (!(flags & SOPT_GLOBAL))
2926 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002927 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002928 N_("unable to unset option %s "
2929 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002930 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002931 return -1;
2932 }
2933 else
2934 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002935 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002936 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002937 return 0;
2938 }
2939 }
2940
Bram Moolenaard6e39182013-05-21 18:30:34 +02002941 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002942
2943 if (flags & SOPT_BOOL)
2944 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002945 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002946
Bram Moolenaarb983f752013-05-15 16:11:50 +02002947 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002948 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002949 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002950 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002951 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002952 }
2953 else if (flags & SOPT_NUM)
2954 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002955 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002956
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002957 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002958 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002959 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002960 return -1;
2961 }
2962
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002963 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002964 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002965 }
2966 else
2967 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002968 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002969 PyObject *todecref;
2970
2971 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002972 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002973 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002974 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002975 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002976 }
2977
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002978 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002979
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002980 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002981}
2982
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002983static PyMappingMethods OptionsAsMapping = {
2984 (lenfunc) NULL,
2985 (binaryfunc) OptionsItem,
2986 (objobjargproc) OptionsAssItem,
2987};
2988
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002989/* Tabpage object
2990 */
2991
2992typedef struct
2993{
2994 PyObject_HEAD
2995 tabpage_T *tab;
2996} TabPageObject;
2997
2998static PyObject *WinListNew(TabPageObject *tabObject);
2999
3000static PyTypeObject TabPageType;
3001
3002 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003003CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003004{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003005 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003006 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003007 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003008 return -1;
3009 }
3010
3011 return 0;
3012}
3013
3014 static PyObject *
3015TabPageNew(tabpage_T *tab)
3016{
3017 TabPageObject *self;
3018
3019 if (TAB_PYTHON_REF(tab))
3020 {
3021 self = TAB_PYTHON_REF(tab);
3022 Py_INCREF(self);
3023 }
3024 else
3025 {
3026 self = PyObject_NEW(TabPageObject, &TabPageType);
3027 if (self == NULL)
3028 return NULL;
3029 self->tab = tab;
3030 TAB_PYTHON_REF(tab) = self;
3031 }
3032
3033 return (PyObject *)(self);
3034}
3035
3036 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003037TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003038{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003039 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3040 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003041
3042 DESTRUCTOR_FINISH(self);
3043}
3044
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003045static char *TabPageAttrs[] = {
3046 "windows", "number", "vars", "window", "valid",
3047 NULL
3048};
3049
3050 static PyObject *
3051TabPageDir(PyObject *self)
3052{
3053 return ObjectDir(self, TabPageAttrs);
3054}
3055
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003056 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003057TabPageAttrValid(TabPageObject *self, char *name)
3058{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003059 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003060
3061 if (strcmp(name, "valid") != 0)
3062 return NULL;
3063
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003064 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3065 Py_INCREF(ret);
3066 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003067}
3068
3069 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003070TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003071{
3072 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003073 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003074 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003075 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003076 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003077 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003078 else if (strcmp(name, "window") == 0)
3079 {
3080 /* For current tab window.c does not bother to set or update tp_curwin
3081 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003082 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003083 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003084 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003085 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003086 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003087 else if (strcmp(name, "__members__") == 0)
3088 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003089 return NULL;
3090}
3091
3092 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003093TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003094{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003095 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003096 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003097 else
3098 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003099 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003100
3101 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003102 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3103 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003104 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003105 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003106 }
3107}
3108
3109static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003110 /* name, function, calling, documentation */
3111 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3112 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003113};
3114
3115/*
3116 * Window list object
3117 */
3118
3119static PyTypeObject TabListType;
3120static PySequenceMethods TabListAsSeq;
3121
3122typedef struct
3123{
3124 PyObject_HEAD
3125} TabListObject;
3126
3127 static PyInt
3128TabListLength(PyObject *self UNUSED)
3129{
3130 tabpage_T *tp = first_tabpage;
3131 PyInt n = 0;
3132
3133 while (tp != NULL)
3134 {
3135 ++n;
3136 tp = tp->tp_next;
3137 }
3138
3139 return n;
3140}
3141
3142 static PyObject *
3143TabListItem(PyObject *self UNUSED, PyInt n)
3144{
3145 tabpage_T *tp;
3146
3147 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3148 if (n == 0)
3149 return TabPageNew(tp);
3150
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003151 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003152 return NULL;
3153}
3154
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003155/*
3156 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003157 */
3158
3159typedef struct
3160{
3161 PyObject_HEAD
3162 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003163 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003164} WindowObject;
3165
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003166static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003167
3168 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003169CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003170{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003171 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003172 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003173 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003174 return -1;
3175 }
3176
3177 return 0;
3178}
3179
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003180 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003181WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003182{
3183 /* We need to handle deletion of windows underneath us.
3184 * If we add a "w_python*_ref" field to the win_T structure,
3185 * then we can get at it in win_free() in vim. We then
3186 * need to create only ONE Python object per window - if
3187 * we try to create a second, just INCREF the existing one
3188 * and return it. The (single) Python object referring to
3189 * the window is stored in "w_python*_ref".
3190 * On a win_free() we set the Python object's win_T* field
3191 * to an invalid value. We trap all uses of a window
3192 * object, and reject them if the win_T* field is invalid.
3193 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003194 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003195 * w_python_ref and w_python3_ref fields respectively.
3196 */
3197
3198 WindowObject *self;
3199
3200 if (WIN_PYTHON_REF(win))
3201 {
3202 self = WIN_PYTHON_REF(win);
3203 Py_INCREF(self);
3204 }
3205 else
3206 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003207 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003208 if (self == NULL)
3209 return NULL;
3210 self->win = win;
3211 WIN_PYTHON_REF(win) = self;
3212 }
3213
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003214 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3215
Bram Moolenaar971db462013-05-12 18:44:48 +02003216 return (PyObject *)(self);
3217}
3218
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003219 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003220WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003221{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003222 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003223 if (self->win && self->win != INVALID_WINDOW_VALUE)
3224 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003225 Py_XDECREF(((PyObject *)(self->tabObject)));
3226 PyObject_GC_Del((void *)(self));
3227}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003228
Bram Moolenaar774267b2013-05-21 20:51:59 +02003229 static int
3230WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3231{
3232 Py_VISIT(((PyObject *)(self->tabObject)));
3233 return 0;
3234}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003235
Bram Moolenaar774267b2013-05-21 20:51:59 +02003236 static int
3237WindowClear(WindowObject *self)
3238{
3239 Py_CLEAR(self->tabObject);
3240 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003241}
3242
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003243 static win_T *
3244get_firstwin(TabPageObject *tabObject)
3245{
3246 if (tabObject)
3247 {
3248 if (CheckTabPage(tabObject))
3249 return NULL;
3250 /* For current tab window.c does not bother to set or update tp_firstwin
3251 */
3252 else if (tabObject->tab == curtab)
3253 return firstwin;
3254 else
3255 return tabObject->tab->tp_firstwin;
3256 }
3257 else
3258 return firstwin;
3259}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003260static char *WindowAttrs[] = {
3261 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3262 "tabpage", "valid",
3263 NULL
3264};
3265
3266 static PyObject *
3267WindowDir(PyObject *self)
3268{
3269 return ObjectDir(self, WindowAttrs);
3270}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003271
Bram Moolenaar971db462013-05-12 18:44:48 +02003272 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003273WindowAttrValid(WindowObject *self, char *name)
3274{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003275 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003276
3277 if (strcmp(name, "valid") != 0)
3278 return NULL;
3279
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003280 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3281 Py_INCREF(ret);
3282 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003283}
3284
3285 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003286WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003287{
3288 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003289 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003290 else if (strcmp(name, "cursor") == 0)
3291 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003292 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003293
3294 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3295 }
3296 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003297 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003298#ifdef FEAT_WINDOWS
3299 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003300 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003301#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003302#ifdef FEAT_VERTSPLIT
3303 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003304 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003305 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003306 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003307#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003308 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003309 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003310 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003311 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3312 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003313 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003314 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003315 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003316 return NULL;
3317 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003318 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003319 }
3320 else if (strcmp(name, "tabpage") == 0)
3321 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003322 Py_INCREF(self->tabObject);
3323 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003324 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003325 else if (strcmp(name, "__members__") == 0)
3326 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003327 else
3328 return NULL;
3329}
3330
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003331 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003332WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003333{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003334 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003335 return -1;
3336
3337 if (strcmp(name, "buffer") == 0)
3338 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003339 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003340 return -1;
3341 }
3342 else if (strcmp(name, "cursor") == 0)
3343 {
3344 long lnum;
3345 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003346
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003347 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003348 return -1;
3349
Bram Moolenaard6e39182013-05-21 18:30:34 +02003350 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003351 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003352 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003353 return -1;
3354 }
3355
3356 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003357 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003358 return -1;
3359
Bram Moolenaard6e39182013-05-21 18:30:34 +02003360 self->win->w_cursor.lnum = lnum;
3361 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003362#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003363 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003364#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003365 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003367
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003368 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003369 return 0;
3370 }
3371 else if (strcmp(name, "height") == 0)
3372 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003373 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003374 win_T *savewin;
3375
Bram Moolenaardee2e312013-06-23 16:35:47 +02003376 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003377 return -1;
3378
3379#ifdef FEAT_GUI
3380 need_mouse_correct = TRUE;
3381#endif
3382 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003383 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003384
3385 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003386 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003387 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003388 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003389 return -1;
3390
3391 return 0;
3392 }
3393#ifdef FEAT_VERTSPLIT
3394 else if (strcmp(name, "width") == 0)
3395 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003396 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003397 win_T *savewin;
3398
Bram Moolenaardee2e312013-06-23 16:35:47 +02003399 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003400 return -1;
3401
3402#ifdef FEAT_GUI
3403 need_mouse_correct = TRUE;
3404#endif
3405 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003406 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003407
3408 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003409 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003410 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003411 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003412 return -1;
3413
3414 return 0;
3415 }
3416#endif
3417 else
3418 {
3419 PyErr_SetString(PyExc_AttributeError, name);
3420 return -1;
3421 }
3422}
3423
3424 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003425WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003426{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003427 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003428 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003429 else
3430 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003431 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003432
Bram Moolenaar6d216452013-05-12 19:00:41 +02003433 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003434 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003435 (self));
3436 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003437 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003438 }
3439}
3440
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003441static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003442 /* name, function, calling, documentation */
3443 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3444 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003445};
3446
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003447/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003448 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003449 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003450
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003451static PyTypeObject WinListType;
3452static PySequenceMethods WinListAsSeq;
3453
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003454typedef struct
3455{
3456 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003457 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003458} WinListObject;
3459
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003460 static PyObject *
3461WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003462{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003463 WinListObject *self;
3464
3465 self = PyObject_NEW(WinListObject, &WinListType);
3466 self->tabObject = tabObject;
3467 Py_INCREF(tabObject);
3468
3469 return (PyObject *)(self);
3470}
3471
3472 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003473WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003474{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003475 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003476
3477 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003478 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003479 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003480 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003481
3482 DESTRUCTOR_FINISH(self);
3483}
3484
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003485 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003486WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003487{
3488 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003489 PyInt n = 0;
3490
Bram Moolenaard6e39182013-05-21 18:30:34 +02003491 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003492 return -1;
3493
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003494 while (w != NULL)
3495 {
3496 ++n;
3497 w = W_NEXT(w);
3498 }
3499
3500 return n;
3501}
3502
3503 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003504WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003505{
3506 win_T *w;
3507
Bram Moolenaard6e39182013-05-21 18:30:34 +02003508 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003509 return NULL;
3510
3511 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003512 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003513 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003514
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003515 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003516 return NULL;
3517}
3518
3519/* Convert a Python string into a Vim line.
3520 *
3521 * The result is in allocated memory. All internal nulls are replaced by
3522 * newline characters. It is an error for the string to contain newline
3523 * characters.
3524 *
3525 * On errors, the Python exception data is set, and NULL is returned.
3526 */
3527 static char *
3528StringToLine(PyObject *obj)
3529{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003530 char *str;
3531 char *save;
3532 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003533 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003534 PyInt i;
3535 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003536
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003537 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003538 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003539 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3540 || str == NULL)
3541 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003542 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003543 else if (PyUnicode_Check(obj))
3544 {
3545 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3546 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003547
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003548 if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
3549 || str == NULL)
3550 {
3551 Py_DECREF(bytes);
3552 return NULL;
3553 }
3554 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003555
3556 /*
3557 * Error checking: String must not contain newlines, as we
3558 * are replacing a single line, and we must replace it with
3559 * a single line.
3560 * A trailing newline is removed, so that append(f.readlines()) works.
3561 */
3562 p = memchr(str, '\n', len);
3563 if (p != NULL)
3564 {
3565 if (p == str + len - 1)
3566 --len;
3567 else
3568 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003569 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003570 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003571 return NULL;
3572 }
3573 }
3574
3575 /* Create a copy of the string, with internal nulls replaced by
3576 * newline characters, as is the vim convention.
3577 */
3578 save = (char *)alloc((unsigned)(len+1));
3579 if (save == NULL)
3580 {
3581 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003582 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003583 return NULL;
3584 }
3585
3586 for (i = 0; i < len; ++i)
3587 {
3588 if (str[i] == '\0')
3589 save[i] = '\n';
3590 else
3591 save[i] = str[i];
3592 }
3593
3594 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003595 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003596
3597 return save;
3598}
3599
3600/* Get a line from the specified buffer. The line number is
3601 * in Vim format (1-based). The line is returned as a Python
3602 * string object.
3603 */
3604 static PyObject *
3605GetBufferLine(buf_T *buf, PyInt n)
3606{
3607 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3608}
3609
3610
3611/* Get a list of lines from the specified buffer. The line numbers
3612 * are in Vim format (1-based). The range is from lo up to, but not
3613 * including, hi. The list is returned as a Python list of string objects.
3614 */
3615 static PyObject *
3616GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3617{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003618 PyInt i;
3619 PyInt n = hi - lo;
3620 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003621
3622 if (list == NULL)
3623 return NULL;
3624
3625 for (i = 0; i < n; ++i)
3626 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003627 PyObject *string = LineToString(
3628 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003629
3630 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003631 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003632 {
3633 Py_DECREF(list);
3634 return NULL;
3635 }
3636
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003637 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003638 }
3639
3640 /* The ownership of the Python list is passed to the caller (ie,
3641 * the caller should Py_DECREF() the object when it is finished
3642 * with it).
3643 */
3644
3645 return list;
3646}
3647
3648/*
3649 * Check if deleting lines made the cursor position invalid.
3650 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3651 * deleted).
3652 */
3653 static void
3654py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3655{
3656 if (curwin->w_cursor.lnum >= lo)
3657 {
3658 /* Adjust the cursor position if it's in/after the changed
3659 * lines. */
3660 if (curwin->w_cursor.lnum >= hi)
3661 {
3662 curwin->w_cursor.lnum += extra;
3663 check_cursor_col();
3664 }
3665 else if (extra < 0)
3666 {
3667 curwin->w_cursor.lnum = lo;
3668 check_cursor();
3669 }
3670 else
3671 check_cursor_col();
3672 changed_cline_bef_curs();
3673 }
3674 invalidate_botline();
3675}
3676
Bram Moolenaar19e60942011-06-19 00:27:51 +02003677/*
3678 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003679 * in Vim format (1-based). The replacement line is given as
3680 * a Python string object. The object is checked for validity
3681 * and correct format. Errors are returned as a value of FAIL.
3682 * The return value is OK on success.
3683 * If OK is returned and len_change is not NULL, *len_change
3684 * is set to the change in the buffer length.
3685 */
3686 static int
3687SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3688{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003689 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003690 * There are three cases:
3691 * 1. NULL, or None - this is a deletion.
3692 * 2. A string - this is a replacement.
3693 * 3. Anything else - this is an error.
3694 */
3695 if (line == Py_None || line == NULL)
3696 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003697 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003698
3699 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003700 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003701
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003702 VimTryStart();
3703
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003704 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003705 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003706 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003707 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003708 else
3709 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003710 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003711 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3712 deleted_lines_mark((linenr_T)n, 1L);
3713 }
3714
Bram Moolenaar105bc352013-05-17 16:03:57 +02003715 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003716
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003717 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003718 return FAIL;
3719
3720 if (len_change)
3721 *len_change = -1;
3722
3723 return OK;
3724 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003725 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003726 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003727 char *save = StringToLine(line);
3728 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003729
3730 if (save == NULL)
3731 return FAIL;
3732
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003733 VimTryStart();
3734
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003735 /* We do not need to free "save" if ml_replace() consumes it. */
3736 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003737 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003738
3739 if (u_savesub((linenr_T)n) == FAIL)
3740 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003741 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003742 vim_free(save);
3743 }
3744 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3745 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003746 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003747 vim_free(save);
3748 }
3749 else
3750 changed_bytes((linenr_T)n, 0);
3751
Bram Moolenaar105bc352013-05-17 16:03:57 +02003752 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003753
3754 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003755 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003756 check_cursor_col();
3757
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003758 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003759 return FAIL;
3760
3761 if (len_change)
3762 *len_change = 0;
3763
3764 return OK;
3765 }
3766 else
3767 {
3768 PyErr_BadArgument();
3769 return FAIL;
3770 }
3771}
3772
Bram Moolenaar19e60942011-06-19 00:27:51 +02003773/* Replace a range of lines in the specified buffer. The line numbers are in
3774 * Vim format (1-based). The range is from lo up to, but not including, hi.
3775 * The replacement lines are given as a Python list of string objects. The
3776 * list is checked for validity and correct format. Errors are returned as a
3777 * value of FAIL. The return value is OK on success.
3778 * If OK is returned and len_change is not NULL, *len_change
3779 * is set to the change in the buffer length.
3780 */
3781 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003782SetBufferLineList(
3783 buf_T *buf,
3784 PyInt lo,
3785 PyInt hi,
3786 PyObject *list,
3787 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003788{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003789 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003790 * There are three cases:
3791 * 1. NULL, or None - this is a deletion.
3792 * 2. A list - this is a replacement.
3793 * 3. Anything else - this is an error.
3794 */
3795 if (list == Py_None || list == NULL)
3796 {
3797 PyInt i;
3798 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003799 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003800
3801 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003802 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003803 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003804
3805 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003806 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003807 else
3808 {
3809 for (i = 0; i < n; ++i)
3810 {
3811 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3812 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003813 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003814 break;
3815 }
3816 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003817 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003818 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3819 deleted_lines_mark((linenr_T)lo, (long)i);
3820 }
3821
Bram Moolenaar105bc352013-05-17 16:03:57 +02003822 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003823
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003824 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003825 return FAIL;
3826
3827 if (len_change)
3828 *len_change = -n;
3829
3830 return OK;
3831 }
3832 else if (PyList_Check(list))
3833 {
3834 PyInt i;
3835 PyInt new_len = PyList_Size(list);
3836 PyInt old_len = hi - lo;
3837 PyInt extra = 0; /* lines added to text, can be negative */
3838 char **array;
3839 buf_T *savebuf;
3840
3841 if (new_len == 0) /* avoid allocating zero bytes */
3842 array = NULL;
3843 else
3844 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003845 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003846 if (array == NULL)
3847 {
3848 PyErr_NoMemory();
3849 return FAIL;
3850 }
3851 }
3852
3853 for (i = 0; i < new_len; ++i)
3854 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003855 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003856
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003857 if (!(line = PyList_GetItem(list, i)) ||
3858 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003859 {
3860 while (i)
3861 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003862 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003863 return FAIL;
3864 }
3865 }
3866
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003867 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003868 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003869
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003870 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003871 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003872
3873 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003874 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003875
3876 /* If the size of the range is reducing (ie, new_len < old_len) we
3877 * need to delete some old_len. We do this at the start, by
3878 * repeatedly deleting line "lo".
3879 */
3880 if (!PyErr_Occurred())
3881 {
3882 for (i = 0; i < old_len - new_len; ++i)
3883 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3884 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003885 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003886 break;
3887 }
3888 extra -= i;
3889 }
3890
3891 /* For as long as possible, replace the existing old_len with the
3892 * new old_len. This is a more efficient operation, as it requires
3893 * less memory allocation and freeing.
3894 */
3895 if (!PyErr_Occurred())
3896 {
3897 for (i = 0; i < old_len && i < new_len; ++i)
3898 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3899 == FAIL)
3900 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003901 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003902 break;
3903 }
3904 }
3905 else
3906 i = 0;
3907
3908 /* Now we may need to insert the remaining new old_len. If we do, we
3909 * must free the strings as we finish with them (we can't pass the
3910 * responsibility to vim in this case).
3911 */
3912 if (!PyErr_Occurred())
3913 {
3914 while (i < new_len)
3915 {
3916 if (ml_append((linenr_T)(lo + i - 1),
3917 (char_u *)array[i], 0, FALSE) == FAIL)
3918 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003919 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003920 break;
3921 }
3922 vim_free(array[i]);
3923 ++i;
3924 ++extra;
3925 }
3926 }
3927
3928 /* Free any left-over old_len, as a result of an error */
3929 while (i < new_len)
3930 {
3931 vim_free(array[i]);
3932 ++i;
3933 }
3934
3935 /* Free the array of old_len. All of its contents have now
3936 * been dealt with (either freed, or the responsibility passed
3937 * to vim.
3938 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003939 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003940
3941 /* Adjust marks. Invalidate any which lie in the
3942 * changed range, and move any in the remainder of the buffer.
3943 */
3944 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3945 (long)MAXLNUM, (long)extra);
3946 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3947
Bram Moolenaar105bc352013-05-17 16:03:57 +02003948 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003949 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3950
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003951 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003952 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003953
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003954 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003955 return FAIL;
3956
3957 if (len_change)
3958 *len_change = new_len - old_len;
3959
3960 return OK;
3961 }
3962 else
3963 {
3964 PyErr_BadArgument();
3965 return FAIL;
3966 }
3967}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003968
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003969/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003970 * The line number is in Vim format (1-based). The lines to be inserted are
3971 * given as a Python list of string objects or as a single string. The lines
3972 * to be added are checked for validity and correct format. Errors are
3973 * returned as a value of FAIL. The return value is OK on success.
3974 * If OK is returned and len_change is not NULL, *len_change
3975 * is set to the change in the buffer length.
3976 */
3977 static int
3978InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3979{
3980 /* First of all, we check the type of the supplied Python object.
3981 * It must be a string or a list, or the call is in error.
3982 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003983 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003984 {
3985 char *str = StringToLine(lines);
3986 buf_T *savebuf;
3987
3988 if (str == NULL)
3989 return FAIL;
3990
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003991 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003992 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003993 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003994
3995 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003996 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003997 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003998 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003999 else
4000 appended_lines_mark((linenr_T)n, 1L);
4001
4002 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004003 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004004 update_screen(VALID);
4005
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004006 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004007 return FAIL;
4008
4009 if (len_change)
4010 *len_change = 1;
4011
4012 return OK;
4013 }
4014 else if (PyList_Check(lines))
4015 {
4016 PyInt i;
4017 PyInt size = PyList_Size(lines);
4018 char **array;
4019 buf_T *savebuf;
4020
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004021 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004022 if (array == NULL)
4023 {
4024 PyErr_NoMemory();
4025 return FAIL;
4026 }
4027
4028 for (i = 0; i < size; ++i)
4029 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004030 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004031
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004032 if (!(line = PyList_GetItem(lines, i)) ||
4033 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004034 {
4035 while (i)
4036 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004037 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004038 return FAIL;
4039 }
4040 }
4041
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004042 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004043 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004044 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004045
4046 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004047 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004048 else
4049 {
4050 for (i = 0; i < size; ++i)
4051 {
4052 if (ml_append((linenr_T)(n + i),
4053 (char_u *)array[i], 0, FALSE) == FAIL)
4054 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004055 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004056
4057 /* Free the rest of the lines */
4058 while (i < size)
4059 vim_free(array[i++]);
4060
4061 break;
4062 }
4063 vim_free(array[i]);
4064 }
4065 if (i > 0)
4066 appended_lines_mark((linenr_T)n, (long)i);
4067 }
4068
4069 /* Free the array of lines. All of its contents have now
4070 * been freed.
4071 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004072 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004073
Bram Moolenaar105bc352013-05-17 16:03:57 +02004074 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004075 update_screen(VALID);
4076
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004077 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004078 return FAIL;
4079
4080 if (len_change)
4081 *len_change = size;
4082
4083 return OK;
4084 }
4085 else
4086 {
4087 PyErr_BadArgument();
4088 return FAIL;
4089 }
4090}
4091
4092/*
4093 * Common routines for buffers and line ranges
4094 * -------------------------------------------
4095 */
4096
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004097typedef struct
4098{
4099 PyObject_HEAD
4100 buf_T *buf;
4101} BufferObject;
4102
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004103 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004104CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004105{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004106 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004108 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109 return -1;
4110 }
4111
4112 return 0;
4113}
4114
4115 static PyObject *
4116RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4117{
4118 if (CheckBuffer(self))
4119 return NULL;
4120
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004121 if (end == -1)
4122 end = self->buf->b_ml.ml_line_count;
4123
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004124 if (n < 0)
4125 n += end - start + 1;
4126
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004127 if (n < 0 || n > end - start)
4128 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004129 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004130 return NULL;
4131 }
4132
4133 return GetBufferLine(self->buf, n+start);
4134}
4135
4136 static PyObject *
4137RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4138{
4139 PyInt size;
4140
4141 if (CheckBuffer(self))
4142 return NULL;
4143
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004144 if (end == -1)
4145 end = self->buf->b_ml.ml_line_count;
4146
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004147 size = end - start + 1;
4148
4149 if (lo < 0)
4150 lo = 0;
4151 else if (lo > size)
4152 lo = size;
4153 if (hi < 0)
4154 hi = 0;
4155 if (hi < lo)
4156 hi = lo;
4157 else if (hi > size)
4158 hi = size;
4159
4160 return GetBufferLineList(self->buf, lo+start, hi+start);
4161}
4162
4163 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004164RBAsItem(
4165 BufferObject *self,
4166 PyInt n,
4167 PyObject *valObject,
4168 PyInt start,
4169 PyInt end,
4170 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004171{
4172 PyInt len_change;
4173
4174 if (CheckBuffer(self))
4175 return -1;
4176
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004177 if (end == -1)
4178 end = self->buf->b_ml.ml_line_count;
4179
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004180 if (n < 0)
4181 n += end - start + 1;
4182
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004183 if (n < 0 || n > end - start)
4184 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004185 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004186 return -1;
4187 }
4188
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004189 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004190 return -1;
4191
4192 if (new_end)
4193 *new_end = end + len_change;
4194
4195 return 0;
4196}
4197
Bram Moolenaar19e60942011-06-19 00:27:51 +02004198 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004199RBAsSlice(
4200 BufferObject *self,
4201 PyInt lo,
4202 PyInt hi,
4203 PyObject *valObject,
4204 PyInt start,
4205 PyInt end,
4206 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004207{
4208 PyInt size;
4209 PyInt len_change;
4210
4211 /* Self must be a valid buffer */
4212 if (CheckBuffer(self))
4213 return -1;
4214
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004215 if (end == -1)
4216 end = self->buf->b_ml.ml_line_count;
4217
Bram Moolenaar19e60942011-06-19 00:27:51 +02004218 /* Sort out the slice range */
4219 size = end - start + 1;
4220
4221 if (lo < 0)
4222 lo = 0;
4223 else if (lo > size)
4224 lo = size;
4225 if (hi < 0)
4226 hi = 0;
4227 if (hi < lo)
4228 hi = lo;
4229 else if (hi > size)
4230 hi = size;
4231
4232 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004233 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004234 return -1;
4235
4236 if (new_end)
4237 *new_end = end + len_change;
4238
4239 return 0;
4240}
4241
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004242
4243 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004244RBAppend(
4245 BufferObject *self,
4246 PyObject *args,
4247 PyInt start,
4248 PyInt end,
4249 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004250{
4251 PyObject *lines;
4252 PyInt len_change;
4253 PyInt max;
4254 PyInt n;
4255
4256 if (CheckBuffer(self))
4257 return NULL;
4258
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004259 if (end == -1)
4260 end = self->buf->b_ml.ml_line_count;
4261
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004262 max = n = end - start + 1;
4263
4264 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4265 return NULL;
4266
4267 if (n < 0 || n > max)
4268 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004269 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004270 return NULL;
4271 }
4272
4273 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4274 return NULL;
4275
4276 if (new_end)
4277 *new_end = end + len_change;
4278
4279 Py_INCREF(Py_None);
4280 return Py_None;
4281}
4282
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004283/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004284 */
4285
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004286static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004287static PySequenceMethods RangeAsSeq;
4288static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004289
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004290typedef struct
4291{
4292 PyObject_HEAD
4293 BufferObject *buf;
4294 PyInt start;
4295 PyInt end;
4296} RangeObject;
4297
4298 static PyObject *
4299RangeNew(buf_T *buf, PyInt start, PyInt end)
4300{
4301 BufferObject *bufr;
4302 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004303 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004304 if (self == NULL)
4305 return NULL;
4306
4307 bufr = (BufferObject *)BufferNew(buf);
4308 if (bufr == NULL)
4309 {
4310 Py_DECREF(self);
4311 return NULL;
4312 }
4313 Py_INCREF(bufr);
4314
4315 self->buf = bufr;
4316 self->start = start;
4317 self->end = end;
4318
4319 return (PyObject *)(self);
4320}
4321
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004322 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004323RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004324{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004325 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004326 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004327 PyObject_GC_Del((void *)(self));
4328}
4329
4330 static int
4331RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4332{
4333 Py_VISIT(((PyObject *)(self->buf)));
4334 return 0;
4335}
4336
4337 static int
4338RangeClear(RangeObject *self)
4339{
4340 Py_CLEAR(self->buf);
4341 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004342}
4343
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004344 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004345RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004346{
4347 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004348 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004349 return -1; /* ??? */
4350
Bram Moolenaard6e39182013-05-21 18:30:34 +02004351 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004352}
4353
4354 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004355RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004356{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004357 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004358}
4359
4360 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004361RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004362{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004363 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004364}
4365
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004366static char *RangeAttrs[] = {
4367 "start", "end",
4368 NULL
4369};
4370
4371 static PyObject *
4372RangeDir(PyObject *self)
4373{
4374 return ObjectDir(self, RangeAttrs);
4375}
4376
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004377 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004378RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004379{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004380 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004381}
4382
4383 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004384RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004385{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004386 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004387 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4388 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004389 else
4390 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004391 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004392
4393 if (name == NULL)
4394 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004395
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004396 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004397 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004398 }
4399}
4400
4401static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004402 /* name, function, calling, documentation */
4403 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004404 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4405 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004406};
4407
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004408static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004409static PySequenceMethods BufferAsSeq;
4410static PyMappingMethods BufferAsMapping;
4411
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004412 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004413BufferNew(buf_T *buf)
4414{
4415 /* We need to handle deletion of buffers underneath us.
4416 * If we add a "b_python*_ref" field to the buf_T structure,
4417 * then we can get at it in buf_freeall() in vim. We then
4418 * need to create only ONE Python object per buffer - if
4419 * we try to create a second, just INCREF the existing one
4420 * and return it. The (single) Python object referring to
4421 * the buffer is stored in "b_python*_ref".
4422 * Question: what to do on a buf_freeall(). We'll probably
4423 * have to either delete the Python object (DECREF it to
4424 * zero - a bad idea, as it leaves dangling refs!) or
4425 * set the buf_T * value to an invalid value (-1?), which
4426 * means we need checks in all access functions... Bah.
4427 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004428 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004429 * b_python_ref and b_python3_ref fields respectively.
4430 */
4431
4432 BufferObject *self;
4433
4434 if (BUF_PYTHON_REF(buf) != NULL)
4435 {
4436 self = BUF_PYTHON_REF(buf);
4437 Py_INCREF(self);
4438 }
4439 else
4440 {
4441 self = PyObject_NEW(BufferObject, &BufferType);
4442 if (self == NULL)
4443 return NULL;
4444 self->buf = buf;
4445 BUF_PYTHON_REF(buf) = self;
4446 }
4447
4448 return (PyObject *)(self);
4449}
4450
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004451 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004452BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004453{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004454 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4455 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004456
4457 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004458}
4459
Bram Moolenaar971db462013-05-12 18:44:48 +02004460 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004461BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004462{
4463 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004464 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004465 return -1; /* ??? */
4466
Bram Moolenaard6e39182013-05-21 18:30:34 +02004467 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004468}
4469
4470 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004471BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004472{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004473 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004474}
4475
4476 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004477BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004478{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004479 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004480}
4481
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004482static char *BufferAttrs[] = {
4483 "name", "number", "vars", "options", "valid",
4484 NULL
4485};
4486
4487 static PyObject *
4488BufferDir(PyObject *self)
4489{
4490 return ObjectDir(self, BufferAttrs);
4491}
4492
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004493 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004494BufferAttrValid(BufferObject *self, char *name)
4495{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004496 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004497
4498 if (strcmp(name, "valid") != 0)
4499 return NULL;
4500
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004501 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4502 Py_INCREF(ret);
4503 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004504}
4505
4506 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004507BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004508{
4509 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004510 return PyString_FromString((self->buf->b_ffname == NULL
4511 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004512 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004513 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004514 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004515 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004516 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004517 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4518 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004519 else if (strcmp(name, "__members__") == 0)
4520 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004521 else
4522 return NULL;
4523}
4524
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004525 static int
4526BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4527{
4528 if (CheckBuffer(self))
4529 return -1;
4530
4531 if (strcmp(name, "name") == 0)
4532 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004533 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004534 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004535 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004536 PyObject *todecref;
4537
4538 if (!(val = StringToChars(valObject, &todecref)))
4539 return -1;
4540
4541 VimTryStart();
4542 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4543 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004544 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004545 aucmd_restbuf(&aco);
4546 Py_XDECREF(todecref);
4547 if (VimTryEnd())
4548 return -1;
4549
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004550 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004551 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004552 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004553 return -1;
4554 }
4555 return 0;
4556 }
4557 else
4558 {
4559 PyErr_SetString(PyExc_AttributeError, name);
4560 return -1;
4561 }
4562}
4563
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004564 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004565BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004566{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004567 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004568}
4569
4570 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004571BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004572{
4573 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004574 char_u *pmark;
4575 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004576 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004577 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004578
Bram Moolenaard6e39182013-05-21 18:30:34 +02004579 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004580 return NULL;
4581
Bram Moolenaar389a1792013-06-23 13:00:44 +02004582 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004583 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004584
Bram Moolenaar389a1792013-06-23 13:00:44 +02004585 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004586 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004587 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004588 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004589 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004590 return NULL;
4591 }
4592
4593 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004594
4595 Py_XDECREF(todecref);
4596
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004597 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004598 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004599 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004600 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004601 if (VimTryEnd())
4602 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004603
4604 if (posp == NULL)
4605 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004606 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004607 return NULL;
4608 }
4609
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004610 if (posp->lnum <= 0)
4611 {
4612 /* Or raise an error? */
4613 Py_INCREF(Py_None);
4614 return Py_None;
4615 }
4616
4617 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4618}
4619
4620 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004621BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004622{
4623 PyInt start;
4624 PyInt end;
4625
Bram Moolenaard6e39182013-05-21 18:30:34 +02004626 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004627 return NULL;
4628
4629 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4630 return NULL;
4631
Bram Moolenaard6e39182013-05-21 18:30:34 +02004632 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004633}
4634
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004635 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004636BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004637{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004638 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004639 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004640 else
4641 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004642 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004643
4644 if (name == NULL)
4645 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004646
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004647 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004648 }
4649}
4650
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004651static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004652 /* name, function, calling, documentation */
4653 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004654 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004655 {"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004656 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4657 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004658};
4659
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004660/*
4661 * Buffer list object - Implementation
4662 */
4663
4664static PyTypeObject BufMapType;
4665
4666typedef struct
4667{
4668 PyObject_HEAD
4669} BufMapObject;
4670
4671 static PyInt
4672BufMapLength(PyObject *self UNUSED)
4673{
4674 buf_T *b = firstbuf;
4675 PyInt n = 0;
4676
4677 while (b)
4678 {
4679 ++n;
4680 b = b->b_next;
4681 }
4682
4683 return n;
4684}
4685
4686 static PyObject *
4687BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4688{
4689 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004690 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004691
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004692 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004693 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004694
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004695 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004696
4697 if (b)
4698 return BufferNew(b);
4699 else
4700 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004701 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004702 return NULL;
4703 }
4704}
4705
4706 static void
4707BufMapIterDestruct(PyObject *buffer)
4708{
4709 /* Iteration was stopped before all buffers were processed */
4710 if (buffer)
4711 {
4712 Py_DECREF(buffer);
4713 }
4714}
4715
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004716 static int
4717BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4718{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004719 if (buffer)
4720 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004721 return 0;
4722}
4723
4724 static int
4725BufMapIterClear(PyObject **buffer)
4726{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004727 if (*buffer)
4728 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004729 return 0;
4730}
4731
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004732 static PyObject *
4733BufMapIterNext(PyObject **buffer)
4734{
4735 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004736 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004737
4738 if (!*buffer)
4739 return NULL;
4740
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004741 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004742
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004743 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004744 {
4745 *buffer = NULL;
4746 return NULL;
4747 }
4748
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004749 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004750 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004751 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004752 return NULL;
4753 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004754 /* Do not increment reference: we no longer hold it (decref), but whoever
4755 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004756 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004757}
4758
4759 static PyObject *
4760BufMapIter(PyObject *self UNUSED)
4761{
4762 PyObject *buffer;
4763
4764 buffer = BufferNew(firstbuf);
4765 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004766 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4767 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004768}
4769
4770static PyMappingMethods BufMapAsMapping = {
4771 (lenfunc) BufMapLength,
4772 (binaryfunc) BufMapItem,
4773 (objobjargproc) 0,
4774};
4775
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004776/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004777 */
4778
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004779static char *CurrentAttrs[] = {
4780 "buffer", "window", "line", "range", "tabpage",
4781 NULL
4782};
4783
4784 static PyObject *
4785CurrentDir(PyObject *self)
4786{
4787 return ObjectDir(self, CurrentAttrs);
4788}
4789
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004790 static PyObject *
4791CurrentGetattr(PyObject *self UNUSED, char *name)
4792{
4793 if (strcmp(name, "buffer") == 0)
4794 return (PyObject *)BufferNew(curbuf);
4795 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004796 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004797 else if (strcmp(name, "tabpage") == 0)
4798 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004799 else if (strcmp(name, "line") == 0)
4800 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4801 else if (strcmp(name, "range") == 0)
4802 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004803 else if (strcmp(name, "__members__") == 0)
4804 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004805 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004806#if PY_MAJOR_VERSION < 3
4807 return Py_FindMethod(WindowMethods, self, name);
4808#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004809 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004810#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004811}
4812
4813 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004814CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004815{
4816 if (strcmp(name, "line") == 0)
4817 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004818 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4819 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004820 return -1;
4821
4822 return 0;
4823 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004824 else if (strcmp(name, "buffer") == 0)
4825 {
4826 int count;
4827
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004828 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004829 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004830 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004831 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004832 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004833 return -1;
4834 }
4835
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004836 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004837 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004838 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004839
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004840 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004841 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4842 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004843 if (VimTryEnd())
4844 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004845 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004846 return -1;
4847 }
4848
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004849 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004850 }
4851 else if (strcmp(name, "window") == 0)
4852 {
4853 int count;
4854
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004855 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004856 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004857 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004858 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004859 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004860 return -1;
4861 }
4862
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004863 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004864 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004865 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004866
4867 if (!count)
4868 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004869 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004870 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004871 return -1;
4872 }
4873
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004874 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004875 win_goto(((WindowObject *)(valObject))->win);
4876 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004877 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004878 if (VimTryEnd())
4879 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004880 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004881 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004882 return -1;
4883 }
4884
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004885 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004886 }
4887 else if (strcmp(name, "tabpage") == 0)
4888 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004889 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004890 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004891 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004892 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004893 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004894 return -1;
4895 }
4896
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004897 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004898 return -1;
4899
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004900 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004901 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
4902 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02004903 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004904 if (VimTryEnd())
4905 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004906 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004907 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004908 return -1;
4909 }
4910
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004911 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004912 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004913 else
4914 {
4915 PyErr_SetString(PyExc_AttributeError, name);
4916 return -1;
4917 }
4918}
4919
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004920static struct PyMethodDef CurrentMethods[] = {
4921 /* name, function, calling, documentation */
4922 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4923 { NULL, NULL, 0, NULL}
4924};
4925
Bram Moolenaardb913952012-06-29 12:54:53 +02004926 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004927init_range_cmd(exarg_T *eap)
4928{
4929 RangeStart = eap->line1;
4930 RangeEnd = eap->line2;
4931}
4932
4933 static void
4934init_range_eval(typval_T *rettv UNUSED)
4935{
4936 RangeStart = (PyInt) curwin->w_cursor.lnum;
4937 RangeEnd = RangeStart;
4938}
4939
4940 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004941run_cmd(const char *cmd, void *arg UNUSED
4942#ifdef PY_CAN_RECURSE
4943 , PyGILState_STATE *pygilstate UNUSED
4944#endif
4945 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004946{
4947 PyRun_SimpleString((char *) cmd);
4948}
4949
4950static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4951static int code_hdr_len = 30;
4952
4953 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004954run_do(const char *cmd, void *arg UNUSED
4955#ifdef PY_CAN_RECURSE
4956 , PyGILState_STATE *pygilstate
4957#endif
4958 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004959{
4960 PyInt lnum;
4961 size_t len;
4962 char *code;
4963 int status;
4964 PyObject *pyfunc, *pymain;
4965
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004966 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004967 {
4968 EMSG(_("cannot save undo information"));
4969 return;
4970 }
4971
4972 len = code_hdr_len + STRLEN(cmd);
4973 code = PyMem_New(char, len + 1);
4974 memcpy(code, code_hdr, code_hdr_len);
4975 STRCPY(code + code_hdr_len, cmd);
4976 status = PyRun_SimpleString(code);
4977 PyMem_Free(code);
4978
4979 if (status)
4980 {
4981 EMSG(_("failed to run the code"));
4982 return;
4983 }
4984
4985 status = 0;
4986 pymain = PyImport_AddModule("__main__");
4987 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004988#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004989 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004990#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004991
4992 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4993 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004994 PyObject *line;
4995 PyObject *linenr;
4996 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004997
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004998#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004999 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005000#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005001 if (!(line = GetBufferLine(curbuf, lnum)))
5002 goto err;
5003 if (!(linenr = PyInt_FromLong((long) lnum)))
5004 {
5005 Py_DECREF(line);
5006 goto err;
5007 }
5008 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5009 Py_DECREF(line);
5010 Py_DECREF(linenr);
5011 if (!ret)
5012 goto err;
5013
5014 if (ret != Py_None)
5015 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5016 goto err;
5017
5018 Py_XDECREF(ret);
5019 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005020#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005021 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005022#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005023 }
5024 goto out;
5025err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005026#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005027 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005028#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005029 PyErr_PrintEx(0);
5030 PythonIO_Flush();
5031 status = 1;
5032out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005033#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005034 if (!status)
5035 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005036#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005037 Py_DECREF(pyfunc);
5038 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5039 if (status)
5040 return;
5041 check_cursor();
5042 update_curbuf(NOT_VALID);
5043}
5044
5045 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005046run_eval(const char *cmd, typval_T *rettv
5047#ifdef PY_CAN_RECURSE
5048 , PyGILState_STATE *pygilstate UNUSED
5049#endif
5050 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005051{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005052 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005053
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005054 run_ret = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
5055 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005056 {
5057 if (PyErr_Occurred() && !msg_silent)
5058 PyErr_PrintEx(0);
5059 EMSG(_("E858: Eval did not return a valid python object"));
5060 }
5061 else
5062 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005063 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005064 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005065 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005066 }
5067 PyErr_Clear();
5068}
5069
5070 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005071set_ref_in_py(const int copyID)
5072{
5073 pylinkedlist_T *cur;
5074 dict_T *dd;
5075 list_T *ll;
5076
5077 if (lastdict != NULL)
5078 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5079 {
5080 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5081 if (dd->dv_copyID != copyID)
5082 {
5083 dd->dv_copyID = copyID;
5084 set_ref_in_ht(&dd->dv_hashtab, copyID);
5085 }
5086 }
5087
5088 if (lastlist != NULL)
5089 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5090 {
5091 ll = ((ListObject *) (cur->pll_obj))->list;
5092 if (ll->lv_copyID != copyID)
5093 {
5094 ll->lv_copyID = copyID;
5095 set_ref_in_list(ll, copyID);
5096 }
5097 }
5098}
5099
5100 static int
5101set_string_copy(char_u *str, typval_T *tv)
5102{
5103 tv->vval.v_string = vim_strsave(str);
5104 if (tv->vval.v_string == NULL)
5105 {
5106 PyErr_NoMemory();
5107 return -1;
5108 }
5109 return 0;
5110}
5111
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005112 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005113pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005114{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005115 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005116 char_u *key;
5117 dictitem_T *di;
5118 PyObject *keyObject;
5119 PyObject *valObject;
5120 Py_ssize_t iter = 0;
5121
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005122 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005123 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005124
5125 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005126 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005127
5128 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5129 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005130 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005131
Bram Moolenaara03e6312013-05-29 22:49:26 +02005132 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005133 {
5134 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005135 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005136 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005137
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005138 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005139 {
5140 dict_unref(dict);
5141 return -1;
5142 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005143
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005144 if (*key == NUL)
5145 {
5146 dict_unref(dict);
5147 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005148 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005149 return -1;
5150 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005151
5152 di = dictitem_alloc(key);
5153
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005154 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005155
5156 if (di == NULL)
5157 {
5158 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005159 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005160 return -1;
5161 }
5162 di->di_tv.v_lock = 0;
5163
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005164 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005165 {
5166 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005167 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005168 return -1;
5169 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005170
5171 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005172 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005173 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005174 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005175 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005176 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005177 return -1;
5178 }
5179 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005180
5181 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005182 return 0;
5183}
5184
5185 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005186pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005187{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005188 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005189 char_u *key;
5190 dictitem_T *di;
5191 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005192 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005193 PyObject *keyObject;
5194 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005195
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005196 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005197 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005198
5199 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005200 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005201
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005202 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005203 {
5204 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005205 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005206 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005207
5208 if (!(iterator = PyObject_GetIter(list)))
5209 {
5210 dict_unref(dict);
5211 Py_DECREF(list);
5212 return -1;
5213 }
5214 Py_DECREF(list);
5215
5216 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005217 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005218 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005219
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005220 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005221 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005222 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005223 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005224 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005225 return -1;
5226 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005227
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005228 if (*key == NUL)
5229 {
5230 Py_DECREF(keyObject);
5231 Py_DECREF(iterator);
5232 Py_XDECREF(todecref);
5233 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005234 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005235 return -1;
5236 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005237
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005238 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005239 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005240 Py_DECREF(keyObject);
5241 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005242 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005243 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005244 return -1;
5245 }
5246
5247 di = dictitem_alloc(key);
5248
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005249 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005250 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005251
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005252 if (di == NULL)
5253 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005254 Py_DECREF(iterator);
5255 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005256 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005257 PyErr_NoMemory();
5258 return -1;
5259 }
5260 di->di_tv.v_lock = 0;
5261
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005262 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005263 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005264 Py_DECREF(iterator);
5265 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005266 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005267 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005268 return -1;
5269 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005270
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005271 Py_DECREF(valObject);
5272
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005273 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005274 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005275 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005276 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005277 dictitem_free(di);
5278 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005279 return -1;
5280 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005281 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005282 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005283 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005284 return 0;
5285}
5286
5287 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005288pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005289{
5290 list_T *l;
5291
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005292 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005293 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005294
5295 tv->v_type = VAR_LIST;
5296 tv->vval.v_list = l;
5297
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005298 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005299 {
5300 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005301 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005302 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005303
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005304 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005305 return 0;
5306}
5307
Bram Moolenaardb913952012-06-29 12:54:53 +02005308typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5309
5310 static int
5311convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005312 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005313{
5314 PyObject *capsule;
5315 char hexBuf[sizeof(void *) * 2 + 3];
5316
5317 sprintf(hexBuf, "%p", obj);
5318
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005319# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005320 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005321# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005322 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005323# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005324 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005325 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005326# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005327 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005328# else
5329 capsule = PyCObject_FromVoidPtr(tv, NULL);
5330# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005331 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5332 {
5333 Py_DECREF(capsule);
5334 tv->v_type = VAR_UNKNOWN;
5335 return -1;
5336 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005337
5338 Py_DECREF(capsule);
5339
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005340 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005341 {
5342 tv->v_type = VAR_UNKNOWN;
5343 return -1;
5344 }
5345 /* As we are not using copy_tv which increments reference count we must
5346 * do it ourself. */
5347 switch(tv->v_type)
5348 {
5349 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5350 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5351 }
5352 }
5353 else
5354 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005355 typval_T *v;
5356
5357# ifdef PY_USE_CAPSULE
5358 v = PyCapsule_GetPointer(capsule, NULL);
5359# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005360 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005361# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005362 copy_tv(v, tv);
5363 }
5364 return 0;
5365}
5366
5367 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005368ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5369{
5370 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005371 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005372
5373 if (!(lookup_dict = PyDict_New()))
5374 return -1;
5375
5376 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5377 {
5378 tv->v_type = VAR_DICT;
5379 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5380 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005381 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005382 }
5383 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005384 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005385 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005386 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005387 else
5388 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005389 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005390 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005391 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005392 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005393 }
5394 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005395 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005396}
5397
5398 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005399ConvertFromPyObject(PyObject *obj, typval_T *tv)
5400{
5401 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005402 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005403
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005404 if (!(lookup_dict = PyDict_New()))
5405 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005406 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005407 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005408 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005409}
5410
5411 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005412_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005413{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005414 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005415 {
5416 tv->v_type = VAR_DICT;
5417 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5418 ++tv->vval.v_dict->dv_refcount;
5419 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005420 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005421 {
5422 tv->v_type = VAR_LIST;
5423 tv->vval.v_list = (((ListObject *)(obj))->list);
5424 ++tv->vval.v_list->lv_refcount;
5425 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005426 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005427 {
5428 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5429 return -1;
5430
5431 tv->v_type = VAR_FUNC;
5432 func_ref(tv->vval.v_string);
5433 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005434 else if (PyBytes_Check(obj))
5435 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005436 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005437
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005438 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005439 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005440 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005441 return -1;
5442
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005443 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005444 return -1;
5445
5446 tv->v_type = VAR_STRING;
5447 }
5448 else if (PyUnicode_Check(obj))
5449 {
5450 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005451 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005452
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005453 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005454 if (bytes == NULL)
5455 return -1;
5456
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005457 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005458 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005459 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005460 return -1;
5461
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005462 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005463 {
5464 Py_XDECREF(bytes);
5465 return -1;
5466 }
5467 Py_XDECREF(bytes);
5468
5469 tv->v_type = VAR_STRING;
5470 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005471#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005472 else if (PyInt_Check(obj))
5473 {
5474 tv->v_type = VAR_NUMBER;
5475 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005476 if (PyErr_Occurred())
5477 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005478 }
5479#endif
5480 else if (PyLong_Check(obj))
5481 {
5482 tv->v_type = VAR_NUMBER;
5483 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005484 if (PyErr_Occurred())
5485 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005486 }
5487 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005488 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005489#ifdef FEAT_FLOAT
5490 else if (PyFloat_Check(obj))
5491 {
5492 tv->v_type = VAR_FLOAT;
5493 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5494 }
5495#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005496 else if (PyObject_HasAttrString(obj, "keys"))
5497 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005498 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005499 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005500 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005501 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005502 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005503 else if (PyNumber_Check(obj))
5504 {
5505 PyObject *num;
5506
5507 if (!(num = PyNumber_Long(obj)))
5508 return -1;
5509
5510 tv->v_type = VAR_NUMBER;
5511 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5512
5513 Py_DECREF(num);
5514 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005515 else
5516 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005517 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005518 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005519 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005520 return -1;
5521 }
5522 return 0;
5523}
5524
5525 static PyObject *
5526ConvertToPyObject(typval_T *tv)
5527{
5528 if (tv == NULL)
5529 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005530 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005531 return NULL;
5532 }
5533 switch (tv->v_type)
5534 {
5535 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005536 return PyBytes_FromString(tv->vval.v_string == NULL
5537 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005538 case VAR_NUMBER:
5539 return PyLong_FromLong((long) tv->vval.v_number);
5540#ifdef FEAT_FLOAT
5541 case VAR_FLOAT:
5542 return PyFloat_FromDouble((double) tv->vval.v_float);
5543#endif
5544 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005545 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005546 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005547 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005548 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005549 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005550 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005551 case VAR_UNKNOWN:
5552 Py_INCREF(Py_None);
5553 return Py_None;
5554 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005555 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005556 return NULL;
5557 }
5558}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005559
5560typedef struct
5561{
5562 PyObject_HEAD
5563} CurrentObject;
5564static PyTypeObject CurrentType;
5565
5566 static void
5567init_structs(void)
5568{
5569 vim_memset(&OutputType, 0, sizeof(OutputType));
5570 OutputType.tp_name = "vim.message";
5571 OutputType.tp_basicsize = sizeof(OutputObject);
5572 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5573 OutputType.tp_doc = "vim message object";
5574 OutputType.tp_methods = OutputMethods;
5575#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005576 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5577 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005578 OutputType.tp_alloc = call_PyType_GenericAlloc;
5579 OutputType.tp_new = call_PyType_GenericNew;
5580 OutputType.tp_free = call_PyObject_Free;
5581#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005582 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5583 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005584#endif
5585
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005586 vim_memset(&IterType, 0, sizeof(IterType));
5587 IterType.tp_name = "vim.iter";
5588 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005589 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005590 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005591 IterType.tp_iter = (getiterfunc)IterIter;
5592 IterType.tp_iternext = (iternextfunc)IterNext;
5593 IterType.tp_dealloc = (destructor)IterDestructor;
5594 IterType.tp_traverse = (traverseproc)IterTraverse;
5595 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005596
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005597 vim_memset(&BufferType, 0, sizeof(BufferType));
5598 BufferType.tp_name = "vim.buffer";
5599 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005600 BufferType.tp_dealloc = (destructor)BufferDestructor;
5601 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005602 BufferType.tp_as_sequence = &BufferAsSeq;
5603 BufferType.tp_as_mapping = &BufferAsMapping;
5604 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5605 BufferType.tp_doc = "vim buffer object";
5606 BufferType.tp_methods = BufferMethods;
5607#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005608 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005609 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005610 BufferType.tp_alloc = call_PyType_GenericAlloc;
5611 BufferType.tp_new = call_PyType_GenericNew;
5612 BufferType.tp_free = call_PyObject_Free;
5613#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005614 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005615 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005616#endif
5617
5618 vim_memset(&WindowType, 0, sizeof(WindowType));
5619 WindowType.tp_name = "vim.window";
5620 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005621 WindowType.tp_dealloc = (destructor)WindowDestructor;
5622 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005623 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005624 WindowType.tp_doc = "vim Window object";
5625 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005626 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5627 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005628#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005629 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5630 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005631 WindowType.tp_alloc = call_PyType_GenericAlloc;
5632 WindowType.tp_new = call_PyType_GenericNew;
5633 WindowType.tp_free = call_PyObject_Free;
5634#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005635 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5636 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005637#endif
5638
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005639 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5640 TabPageType.tp_name = "vim.tabpage";
5641 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005642 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5643 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005644 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5645 TabPageType.tp_doc = "vim tab page object";
5646 TabPageType.tp_methods = TabPageMethods;
5647#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005648 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005649 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5650 TabPageType.tp_new = call_PyType_GenericNew;
5651 TabPageType.tp_free = call_PyObject_Free;
5652#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005653 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005654#endif
5655
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005656 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5657 BufMapType.tp_name = "vim.bufferlist";
5658 BufMapType.tp_basicsize = sizeof(BufMapObject);
5659 BufMapType.tp_as_mapping = &BufMapAsMapping;
5660 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005661 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005662 BufferType.tp_doc = "vim buffer list";
5663
5664 vim_memset(&WinListType, 0, sizeof(WinListType));
5665 WinListType.tp_name = "vim.windowlist";
5666 WinListType.tp_basicsize = sizeof(WinListType);
5667 WinListType.tp_as_sequence = &WinListAsSeq;
5668 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5669 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005670 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005671
5672 vim_memset(&TabListType, 0, sizeof(TabListType));
5673 TabListType.tp_name = "vim.tabpagelist";
5674 TabListType.tp_basicsize = sizeof(TabListType);
5675 TabListType.tp_as_sequence = &TabListAsSeq;
5676 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5677 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005678
5679 vim_memset(&RangeType, 0, sizeof(RangeType));
5680 RangeType.tp_name = "vim.range";
5681 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005682 RangeType.tp_dealloc = (destructor)RangeDestructor;
5683 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005684 RangeType.tp_as_sequence = &RangeAsSeq;
5685 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005686 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005687 RangeType.tp_doc = "vim Range object";
5688 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005689 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5690 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005691#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005692 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005693 RangeType.tp_alloc = call_PyType_GenericAlloc;
5694 RangeType.tp_new = call_PyType_GenericNew;
5695 RangeType.tp_free = call_PyObject_Free;
5696#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005697 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005698#endif
5699
5700 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5701 CurrentType.tp_name = "vim.currentdata";
5702 CurrentType.tp_basicsize = sizeof(CurrentObject);
5703 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5704 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005705 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005706#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005707 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5708 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005709#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005710 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5711 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005712#endif
5713
5714 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5715 DictionaryType.tp_name = "vim.dictionary";
5716 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005717 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005718 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005719 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005720 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005721 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5722 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005723 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5724 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5725 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005726#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005727 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5728 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005729#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005730 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5731 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005732#endif
5733
5734 vim_memset(&ListType, 0, sizeof(ListType));
5735 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005736 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005737 ListType.tp_basicsize = sizeof(ListObject);
5738 ListType.tp_as_sequence = &ListAsSeq;
5739 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005740 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005741 ListType.tp_doc = "list pushing modifications to vim structure";
5742 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005743 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005744 ListType.tp_new = (newfunc)ListConstructor;
5745 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005746#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005747 ListType.tp_getattro = (getattrofunc)ListGetattro;
5748 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005749#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005750 ListType.tp_getattr = (getattrfunc)ListGetattr;
5751 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005752#endif
5753
5754 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005755 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005756 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005757 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5758 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005759 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005760 FunctionType.tp_doc = "object that calls vim function";
5761 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005762 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005763 FunctionType.tp_new = (newfunc)FunctionConstructor;
5764 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005765#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005766 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005767#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005768 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005769#endif
5770
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005771 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5772 OptionsType.tp_name = "vim.options";
5773 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005774 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005775 OptionsType.tp_doc = "object for manipulating options";
5776 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005777 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5778 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5779 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005780
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005781 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5782 LoaderType.tp_name = "vim.Loader";
5783 LoaderType.tp_basicsize = sizeof(LoaderObject);
5784 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5785 LoaderType.tp_doc = "vim message object";
5786 LoaderType.tp_methods = LoaderMethods;
5787 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5788
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005789#if PY_MAJOR_VERSION >= 3
5790 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5791 vimmodule.m_name = "vim";
5792 vimmodule.m_doc = "Vim Python interface\n";
5793 vimmodule.m_size = -1;
5794 vimmodule.m_methods = VimMethods;
5795#endif
5796}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005797
5798#define PYTYPE_READY(type) \
5799 if (PyType_Ready(&type)) \
5800 return -1;
5801
5802 static int
5803init_types()
5804{
5805 PYTYPE_READY(IterType);
5806 PYTYPE_READY(BufferType);
5807 PYTYPE_READY(RangeType);
5808 PYTYPE_READY(WindowType);
5809 PYTYPE_READY(TabPageType);
5810 PYTYPE_READY(BufMapType);
5811 PYTYPE_READY(WinListType);
5812 PYTYPE_READY(TabListType);
5813 PYTYPE_READY(CurrentType);
5814 PYTYPE_READY(DictionaryType);
5815 PYTYPE_READY(ListType);
5816 PYTYPE_READY(FunctionType);
5817 PYTYPE_READY(OptionsType);
5818 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005819 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005820 return 0;
5821}
5822
5823 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005824init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005825{
5826 PyObject *path;
5827 PyObject *path_hook;
5828 PyObject *path_hooks;
5829
5830 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5831 return -1;
5832
5833 if (!(path_hooks = PySys_GetObject("path_hooks")))
5834 {
5835 PyErr_Clear();
5836 path_hooks = PyList_New(1);
5837 PyList_SET_ITEM(path_hooks, 0, path_hook);
5838 if (PySys_SetObject("path_hooks", path_hooks))
5839 {
5840 Py_DECREF(path_hooks);
5841 return -1;
5842 }
5843 Py_DECREF(path_hooks);
5844 }
5845 else if (PyList_Check(path_hooks))
5846 {
5847 if (PyList_Append(path_hooks, path_hook))
5848 {
5849 Py_DECREF(path_hook);
5850 return -1;
5851 }
5852 Py_DECREF(path_hook);
5853 }
5854 else
5855 {
5856 VimTryStart();
5857 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5858 "You should now do the following:\n"
5859 "- append vim.path_hook to sys.path_hooks\n"
5860 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5861 VimTryEnd(); /* Discard the error */
5862 Py_DECREF(path_hook);
5863 return 0;
5864 }
5865
5866 if (!(path = PySys_GetObject("path")))
5867 {
5868 PyErr_Clear();
5869 path = PyList_New(1);
5870 Py_INCREF(vim_special_path_object);
5871 PyList_SET_ITEM(path, 0, vim_special_path_object);
5872 if (PySys_SetObject("path", path))
5873 {
5874 Py_DECREF(path);
5875 return -1;
5876 }
5877 Py_DECREF(path);
5878 }
5879 else if (PyList_Check(path))
5880 {
5881 if (PyList_Append(path, vim_special_path_object))
5882 return -1;
5883 }
5884 else
5885 {
5886 VimTryStart();
5887 EMSG(_("Failed to set path: sys.path is not a list\n"
5888 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5889 VimTryEnd(); /* Discard the error */
5890 }
5891
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005892 return 0;
5893}
5894
5895static BufMapObject TheBufferMap =
5896{
5897 PyObject_HEAD_INIT(&BufMapType)
5898};
5899
5900static WinListObject TheWindowList =
5901{
5902 PyObject_HEAD_INIT(&WinListType)
5903 NULL
5904};
5905
5906static CurrentObject TheCurrent =
5907{
5908 PyObject_HEAD_INIT(&CurrentType)
5909};
5910
5911static TabListObject TheTabPageList =
5912{
5913 PyObject_HEAD_INIT(&TabListType)
5914};
5915
5916static struct numeric_constant {
5917 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005918 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005919} numeric_constants[] = {
5920 {"VAR_LOCKED", VAR_LOCKED},
5921 {"VAR_FIXED", VAR_FIXED},
5922 {"VAR_SCOPE", VAR_SCOPE},
5923 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5924};
5925
5926static struct object_constant {
5927 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005928 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005929} object_constants[] = {
5930 {"buffers", (PyObject *)(void *)&TheBufferMap},
5931 {"windows", (PyObject *)(void *)&TheWindowList},
5932 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5933 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005934
5935 {"Buffer", (PyObject *)&BufferType},
5936 {"Range", (PyObject *)&RangeType},
5937 {"Window", (PyObject *)&WindowType},
5938 {"TabPage", (PyObject *)&TabPageType},
5939 {"Dictionary", (PyObject *)&DictionaryType},
5940 {"List", (PyObject *)&ListType},
5941 {"Function", (PyObject *)&FunctionType},
5942 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005943 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005944};
5945
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005946#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02005947 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005948 return -1;
5949
5950#define ADD_CHECKED_OBJECT(m, name, obj) \
5951 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005952 PyObject *valObject = obj; \
5953 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005954 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005955 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005956 }
5957
5958 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02005959populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005960{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005961 int i;
5962 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005963 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005964 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005965
5966 for (i = 0; i < (int)(sizeof(numeric_constants)
5967 / sizeof(struct numeric_constant));
5968 ++i)
5969 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005970 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005971
5972 for (i = 0; i < (int)(sizeof(object_constants)
5973 / sizeof(struct object_constant));
5974 ++i)
5975 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005976 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005977
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005978 valObject = object_constants[i].valObject;
5979 Py_INCREF(valObject);
5980 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005981 }
5982
5983 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5984 return -1;
5985 ADD_OBJECT(m, "error", VimError);
5986
Bram Moolenaara9922d62013-05-30 13:01:18 +02005987 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5988 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005989 ADD_CHECKED_OBJECT(m, "options",
5990 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005991
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005992 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005993 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005994 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005995
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005996 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005997 return -1;
5998 ADD_OBJECT(m, "_getcwd", py_getcwd)
5999
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006000 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006001 return -1;
6002 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006003 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006004 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006005 if (PyObject_SetAttrString(other_module, "chdir", attr))
6006 {
6007 Py_DECREF(attr);
6008 return -1;
6009 }
6010 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006011
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006012 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006013 {
6014 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006015 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006016 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006017 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6018 {
6019 Py_DECREF(attr);
6020 return -1;
6021 }
6022 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006023 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006024 else
6025 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006026
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006027 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6028 return -1;
6029
6030 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6031
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006032 if (!(imp = PyImport_ImportModule("imp")))
6033 return -1;
6034
6035 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6036 {
6037 Py_DECREF(imp);
6038 return -1;
6039 }
6040
6041 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6042 {
6043 Py_DECREF(py_find_module);
6044 Py_DECREF(imp);
6045 return -1;
6046 }
6047
6048 Py_DECREF(imp);
6049
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006050 ADD_OBJECT(m, "_find_module", py_find_module);
6051 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006052
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006053 return 0;
6054}