blob: 706794cc5fc96c1a269f72714404585c90921659 [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 {
1202 Py_INCREF(Py_None);
1203 return Py_None;
1204 }
1205
1206 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1207 {
1208 Py_DECREF(module);
1209 return NULL;
1210 }
1211
1212 loader->module = module;
1213
1214 return (PyObject *) loader;
1215}
1216
1217 static PyObject *
1218VimPathHook(PyObject *self UNUSED, PyObject *args)
1219{
1220 char *path;
1221
1222 if (PyArg_ParseTuple(args, "s", &path)
1223 && STRCMP(path, vim_special_path) == 0)
1224 {
1225 Py_INCREF(vim_module);
1226 return vim_module;
1227 }
1228
1229 PyErr_Clear();
1230 PyErr_SetNone(PyExc_ImportError);
1231 return NULL;
1232}
1233
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001234/*
1235 * Vim module - Definitions
1236 */
1237
1238static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001239 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001240 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001241 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001242 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1243 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001244 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1245 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001246 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001247 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001248 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1249 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1250 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001251};
1252
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001253/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001254 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001255 */
1256
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001257static PyTypeObject IterType;
1258
1259typedef PyObject *(*nextfun)(void **);
1260typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001261typedef int (*traversefun)(void *, visitproc, void *);
1262typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001263
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001264/* Main purpose of this object is removing the need for do python
1265 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1266 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001267
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001268typedef struct
1269{
1270 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001271 void *cur;
1272 nextfun next;
1273 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001274 traversefun traverse;
1275 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001276} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001277
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001278 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001279IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1280 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001281{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001282 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001283
Bram Moolenaar774267b2013-05-21 20:51:59 +02001284 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001285 self->cur = start;
1286 self->next = next;
1287 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001288 self->traverse = traverse;
1289 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001290
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001291 return (PyObject *)(self);
1292}
1293
1294 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001295IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001296{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001297 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001298 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001299 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001300}
1301
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001302 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001303IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001304{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001305 if (self->traverse != NULL)
1306 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001307 else
1308 return 0;
1309}
1310
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001311/* Mac OSX defines clear() somewhere. */
1312#ifdef clear
1313# undef clear
1314#endif
1315
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001316 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001317IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001318{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001319 if (self->clear != NULL)
1320 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001321 else
1322 return 0;
1323}
1324
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001325 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001326IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001327{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001328 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001329}
1330
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001331 static PyObject *
1332IterIter(PyObject *self)
1333{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001334 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001335 return self;
1336}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001337
Bram Moolenaardb913952012-06-29 12:54:53 +02001338typedef struct pylinkedlist_S {
1339 struct pylinkedlist_S *pll_next;
1340 struct pylinkedlist_S *pll_prev;
1341 PyObject *pll_obj;
1342} pylinkedlist_T;
1343
1344static pylinkedlist_T *lastdict = NULL;
1345static pylinkedlist_T *lastlist = NULL;
1346
1347 static void
1348pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1349{
1350 if (ref->pll_prev == NULL)
1351 {
1352 if (ref->pll_next == NULL)
1353 {
1354 *last = NULL;
1355 return;
1356 }
1357 }
1358 else
1359 ref->pll_prev->pll_next = ref->pll_next;
1360
1361 if (ref->pll_next == NULL)
1362 *last = ref->pll_prev;
1363 else
1364 ref->pll_next->pll_prev = ref->pll_prev;
1365}
1366
1367 static void
1368pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1369{
1370 if (*last == NULL)
1371 ref->pll_prev = NULL;
1372 else
1373 {
1374 (*last)->pll_next = ref;
1375 ref->pll_prev = *last;
1376 }
1377 ref->pll_next = NULL;
1378 ref->pll_obj = self;
1379 *last = ref;
1380}
1381
1382static PyTypeObject DictionaryType;
1383
1384typedef struct
1385{
1386 PyObject_HEAD
1387 dict_T *dict;
1388 pylinkedlist_T ref;
1389} DictionaryObject;
1390
Bram Moolenaara9922d62013-05-30 13:01:18 +02001391static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1392
1393#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1394
Bram Moolenaardb913952012-06-29 12:54:53 +02001395 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001396DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001397{
1398 DictionaryObject *self;
1399
Bram Moolenaara9922d62013-05-30 13:01:18 +02001400 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001401 if (self == NULL)
1402 return NULL;
1403 self->dict = dict;
1404 ++dict->dv_refcount;
1405
1406 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1407
1408 return (PyObject *)(self);
1409}
1410
Bram Moolenaara9922d62013-05-30 13:01:18 +02001411 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001412py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001413{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001414 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001415
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001416 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001417 {
1418 PyErr_NoMemory();
1419 return NULL;
1420 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001421 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001422
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001423 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001424}
1425
1426 static PyObject *
1427DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1428{
1429 DictionaryObject *self;
1430 dict_T *dict;
1431
1432 if (!(dict = py_dict_alloc()))
1433 return NULL;
1434
1435 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1436
1437 --dict->dv_refcount;
1438
1439 if (kwargs || PyTuple_Size(args))
1440 {
1441 PyObject *tmp;
1442 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1443 {
1444 Py_DECREF(self);
1445 return NULL;
1446 }
1447
1448 Py_DECREF(tmp);
1449 }
1450
1451 return (PyObject *)(self);
1452}
1453
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001454 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001455DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001456{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001457 pyll_remove(&self->ref, &lastdict);
1458 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001459
1460 DESTRUCTOR_FINISH(self);
1461}
1462
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001463static char *DictionaryAttrs[] = {
1464 "locked", "scope",
1465 NULL
1466};
1467
1468 static PyObject *
1469DictionaryDir(PyObject *self)
1470{
1471 return ObjectDir(self, DictionaryAttrs);
1472}
1473
Bram Moolenaardb913952012-06-29 12:54:53 +02001474 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001475DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001476{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001477 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001478 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001479 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001480 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001481 return -1;
1482 }
1483
1484 if (strcmp(name, "locked") == 0)
1485 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001486 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001487 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001488 PyErr_SET_STRING(PyExc_TypeError,
1489 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001490 return -1;
1491 }
1492 else
1493 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001494 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001495 if (istrue == -1)
1496 return -1;
1497 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001498 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001499 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001500 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001501 }
1502 return 0;
1503 }
1504 else
1505 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001506 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001507 return -1;
1508 }
1509}
1510
1511 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001512DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001513{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001514 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001515}
1516
Bram Moolenaara9922d62013-05-30 13:01:18 +02001517#define DICT_FLAG_HAS_DEFAULT 0x01
1518#define DICT_FLAG_POP 0x02
1519#define DICT_FLAG_NONE_DEFAULT 0x04
1520#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1521#define DICT_FLAG_RETURN_PAIR 0x10
1522
Bram Moolenaardb913952012-06-29 12:54:53 +02001523 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001524_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001525{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001526 PyObject *keyObject;
1527 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001528 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001529 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001530 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001531 dict_T *dict = self->dict;
1532 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001533 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001534
Bram Moolenaara9922d62013-05-30 13:01:18 +02001535 if (flags & DICT_FLAG_HAS_DEFAULT)
1536 {
1537 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1538 return NULL;
1539 }
1540 else
1541 keyObject = args;
1542
1543 if (flags & DICT_FLAG_RETURN_BOOL)
1544 defObject = Py_False;
1545
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001546 if (!(key = StringToChars(keyObject, &todecref)))
1547 return NULL;
1548
1549 if (*key == NUL)
1550 {
1551 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001552 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001553 return NULL;
1554 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001555
Bram Moolenaara9922d62013-05-30 13:01:18 +02001556 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001557
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001558 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001559
Bram Moolenaara9922d62013-05-30 13:01:18 +02001560 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001561 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001562 if (defObject)
1563 {
1564 Py_INCREF(defObject);
1565 return defObject;
1566 }
1567 else
1568 {
1569 PyErr_SetObject(PyExc_KeyError, keyObject);
1570 return NULL;
1571 }
1572 }
1573 else if (flags & DICT_FLAG_RETURN_BOOL)
1574 {
1575 Py_INCREF(Py_True);
1576 return Py_True;
1577 }
1578
1579 di = dict_lookup(hi);
1580
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001581 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001582 return NULL;
1583
1584 if (flags & DICT_FLAG_POP)
1585 {
1586 if (dict->dv_lock)
1587 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001588 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001589 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001590 return NULL;
1591 }
1592
1593 hash_remove(&dict->dv_hashtab, hi);
1594 dictitem_free(di);
1595 }
1596
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001597 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001598}
1599
1600 static PyObject *
1601DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1602{
1603 return _DictionaryItem(self, keyObject, 0);
1604}
1605
1606 static int
1607DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1608{
1609 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001610 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001611
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001612 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001613
1614 Py_DECREF(Py_True);
1615
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001616 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001617}
1618
1619typedef struct
1620{
1621 hashitem_T *ht_array;
1622 long_u ht_used;
1623 hashtab_T *ht;
1624 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001625 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001626} dictiterinfo_T;
1627
1628 static PyObject *
1629DictionaryIterNext(dictiterinfo_T **dii)
1630{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001631 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001632
1633 if (!(*dii)->todo)
1634 return NULL;
1635
1636 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1637 (*dii)->ht->ht_used != (*dii)->ht_used)
1638 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001639 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001640 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001641 return NULL;
1642 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001643
Bram Moolenaara9922d62013-05-30 13:01:18 +02001644 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1645 ++((*dii)->hi);
1646
1647 --((*dii)->todo);
1648
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001649 if (!(ret = PyBytes_FromString((char *) (*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001650 return NULL;
1651
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001652 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001653}
1654
1655 static PyObject *
1656DictionaryIter(DictionaryObject *self)
1657{
1658 dictiterinfo_T *dii;
1659 hashtab_T *ht;
1660
1661 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1662 {
1663 PyErr_NoMemory();
1664 return NULL;
1665 }
1666
1667 ht = &self->dict->dv_hashtab;
1668 dii->ht_array = ht->ht_array;
1669 dii->ht_used = ht->ht_used;
1670 dii->ht = ht;
1671 dii->hi = dii->ht_array;
1672 dii->todo = dii->ht_used;
1673
1674 return IterNew(dii,
1675 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1676 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001677}
1678
1679 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001680DictionaryAssItem(
1681 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001682{
1683 char_u *key;
1684 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001685 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001686 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001687 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001688
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001689 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001690 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001691 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001692 return -1;
1693 }
1694
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001695 if (!(key = StringToChars(keyObject, &todecref)))
1696 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001697
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001698 if (*key == NUL)
1699 {
1700 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001701 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001702 return -1;
1703 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001704
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001705 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001706
1707 if (valObject == NULL)
1708 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001709 hashitem_T *hi;
1710
Bram Moolenaardb913952012-06-29 12:54:53 +02001711 if (di == NULL)
1712 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001713 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001714 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001715 return -1;
1716 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001717 hi = hash_find(&dict->dv_hashtab, di->di_key);
1718 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001719 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001720 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001721 return 0;
1722 }
1723
1724 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001725 {
1726 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001727 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001728 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001729
1730 if (di == NULL)
1731 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001732 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001733 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001734 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001735 PyErr_NoMemory();
1736 return -1;
1737 }
1738 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001739 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001740
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001741 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001742 {
1743 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001744 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001745 RAISE_KEY_ADD_FAIL(key);
1746 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001747 return -1;
1748 }
1749 }
1750 else
1751 clear_tv(&di->di_tv);
1752
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001753 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001754
1755 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001756 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001757 return 0;
1758}
1759
Bram Moolenaara9922d62013-05-30 13:01:18 +02001760typedef PyObject *(*hi_to_py)(hashitem_T *);
1761
Bram Moolenaardb913952012-06-29 12:54:53 +02001762 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001763DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001764{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001765 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001766 long_u todo = dict->dv_hashtab.ht_used;
1767 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001768 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001769 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001770 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001771
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001772 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001773 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1774 {
1775 if (!HASHITEM_EMPTY(hi))
1776 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001777 if (!(newObj = hiconvert(hi)))
1778 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001779 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001780 return NULL;
1781 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001782 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001783 --todo;
1784 ++i;
1785 }
1786 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001787 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001788}
1789
Bram Moolenaara9922d62013-05-30 13:01:18 +02001790 static PyObject *
1791dict_key(hashitem_T *hi)
1792{
1793 return PyBytes_FromString((char *)(hi->hi_key));
1794}
1795
1796 static PyObject *
1797DictionaryListKeys(DictionaryObject *self)
1798{
1799 return DictionaryListObjects(self, dict_key);
1800}
1801
1802 static PyObject *
1803dict_val(hashitem_T *hi)
1804{
1805 dictitem_T *di;
1806
1807 di = dict_lookup(hi);
1808 return ConvertToPyObject(&di->di_tv);
1809}
1810
1811 static PyObject *
1812DictionaryListValues(DictionaryObject *self)
1813{
1814 return DictionaryListObjects(self, dict_val);
1815}
1816
1817 static PyObject *
1818dict_item(hashitem_T *hi)
1819{
1820 PyObject *keyObject;
1821 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001822 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001823
1824 if (!(keyObject = dict_key(hi)))
1825 return NULL;
1826
1827 if (!(valObject = dict_val(hi)))
1828 {
1829 Py_DECREF(keyObject);
1830 return NULL;
1831 }
1832
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001833 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001834
1835 Py_DECREF(keyObject);
1836 Py_DECREF(valObject);
1837
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001838 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001839}
1840
1841 static PyObject *
1842DictionaryListItems(DictionaryObject *self)
1843{
1844 return DictionaryListObjects(self, dict_item);
1845}
1846
1847 static PyObject *
1848DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1849{
1850 dict_T *dict = self->dict;
1851
1852 if (dict->dv_lock)
1853 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001854 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001855 return NULL;
1856 }
1857
1858 if (kwargs)
1859 {
1860 typval_T tv;
1861
1862 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1863 return NULL;
1864
1865 VimTryStart();
1866 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1867 clear_tv(&tv);
1868 if (VimTryEnd())
1869 return NULL;
1870 }
1871 else
1872 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001873 PyObject *obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001874
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001875 if (!PyArg_ParseTuple(args, "O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001876 return NULL;
1877
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001878 if (PyObject_HasAttrString(obj, "keys"))
1879 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001880 else
1881 {
1882 PyObject *iterator;
1883 PyObject *item;
1884
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001885 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001886 return NULL;
1887
1888 while ((item = PyIter_Next(iterator)))
1889 {
1890 PyObject *fast;
1891 PyObject *keyObject;
1892 PyObject *valObject;
1893 PyObject *todecref;
1894 char_u *key;
1895 dictitem_T *di;
1896
1897 if (!(fast = PySequence_Fast(item, "")))
1898 {
1899 Py_DECREF(iterator);
1900 Py_DECREF(item);
1901 return NULL;
1902 }
1903
1904 Py_DECREF(item);
1905
1906 if (PySequence_Fast_GET_SIZE(fast) != 2)
1907 {
1908 Py_DECREF(iterator);
1909 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001910 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001911 N_("expected sequence element of size 2, "
1912 "but got sequence of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001913 PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001914 return NULL;
1915 }
1916
1917 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1918
1919 if (!(key = StringToChars(keyObject, &todecref)))
1920 {
1921 Py_DECREF(iterator);
1922 Py_DECREF(fast);
1923 return NULL;
1924 }
1925
1926 di = dictitem_alloc(key);
1927
1928 Py_XDECREF(todecref);
1929
1930 if (di == NULL)
1931 {
1932 Py_DECREF(fast);
1933 Py_DECREF(iterator);
1934 PyErr_NoMemory();
1935 return NULL;
1936 }
1937 di->di_tv.v_lock = 0;
1938 di->di_tv.v_type = VAR_UNKNOWN;
1939
1940 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1941
1942 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1943 {
1944 Py_DECREF(iterator);
1945 Py_DECREF(fast);
1946 dictitem_free(di);
1947 return NULL;
1948 }
1949
1950 Py_DECREF(fast);
1951
1952 if (dict_add(dict, di) == FAIL)
1953 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001954 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001955 Py_DECREF(iterator);
1956 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001957 return NULL;
1958 }
1959 }
1960
1961 Py_DECREF(iterator);
1962
1963 /* Iterator may have finished due to an exception */
1964 if (PyErr_Occurred())
1965 return NULL;
1966 }
1967 }
1968 Py_INCREF(Py_None);
1969 return Py_None;
1970}
1971
1972 static PyObject *
1973DictionaryGet(DictionaryObject *self, PyObject *args)
1974{
1975 return _DictionaryItem(self, args,
1976 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
1977}
1978
1979 static PyObject *
1980DictionaryPop(DictionaryObject *self, PyObject *args)
1981{
1982 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
1983}
1984
1985 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02001986DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001987{
Bram Moolenaarde71b562013-06-02 17:41:54 +02001988 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001989 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02001990 PyObject *valObject;
1991 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001992
Bram Moolenaarde71b562013-06-02 17:41:54 +02001993 if (self->dict->dv_hashtab.ht_used == 0)
1994 {
1995 PyErr_SetNone(PyExc_KeyError);
1996 return NULL;
1997 }
1998
1999 hi = self->dict->dv_hashtab.ht_array;
2000 while (HASHITEM_EMPTY(hi))
2001 ++hi;
2002
2003 di = dict_lookup(hi);
2004
2005 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002006 return NULL;
2007
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002008 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002009 {
2010 Py_DECREF(valObject);
2011 return NULL;
2012 }
2013
2014 hash_remove(&self->dict->dv_hashtab, hi);
2015 dictitem_free(di);
2016
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002017 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002018}
2019
2020 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002021DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002022{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002023 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2024}
2025
2026static PySequenceMethods DictionaryAsSeq = {
2027 0, /* sq_length */
2028 0, /* sq_concat */
2029 0, /* sq_repeat */
2030 0, /* sq_item */
2031 0, /* sq_slice */
2032 0, /* sq_ass_item */
2033 0, /* sq_ass_slice */
2034 (objobjproc) DictionaryContains, /* sq_contains */
2035 0, /* sq_inplace_concat */
2036 0, /* sq_inplace_repeat */
2037};
2038
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002039static PyMappingMethods DictionaryAsMapping = {
2040 (lenfunc) DictionaryLength,
2041 (binaryfunc) DictionaryItem,
2042 (objobjargproc) DictionaryAssItem,
2043};
2044
Bram Moolenaardb913952012-06-29 12:54:53 +02002045static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002046 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002047 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2048 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2049 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2050 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2051 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002052 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002053 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002054 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2055 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002056};
2057
2058static PyTypeObject ListType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002059static PySequenceMethods ListAsSeq;
2060static PyMappingMethods ListAsMapping;
Bram Moolenaardb913952012-06-29 12:54:53 +02002061
2062typedef struct
2063{
2064 PyObject_HEAD
2065 list_T *list;
2066 pylinkedlist_T ref;
2067} ListObject;
2068
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002069#define NEW_LIST(list) ListNew(&ListType, list)
2070
Bram Moolenaardb913952012-06-29 12:54:53 +02002071 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002072ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002073{
2074 ListObject *self;
2075
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002076 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002077 if (self == NULL)
2078 return NULL;
2079 self->list = list;
2080 ++list->lv_refcount;
2081
2082 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2083
2084 return (PyObject *)(self);
2085}
2086
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002087 static list_T *
2088py_list_alloc()
2089{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002090 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002091
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002092 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002093 {
2094 PyErr_NoMemory();
2095 return NULL;
2096 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002097 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002098
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002099 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002100}
2101
2102 static int
2103list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2104{
2105 PyObject *iterator;
2106 PyObject *item;
2107 listitem_T *li;
2108
2109 if (!(iterator = PyObject_GetIter(obj)))
2110 return -1;
2111
2112 while ((item = PyIter_Next(iterator)))
2113 {
2114 if (!(li = listitem_alloc()))
2115 {
2116 PyErr_NoMemory();
2117 Py_DECREF(item);
2118 Py_DECREF(iterator);
2119 return -1;
2120 }
2121 li->li_tv.v_lock = 0;
2122 li->li_tv.v_type = VAR_UNKNOWN;
2123
2124 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2125 {
2126 Py_DECREF(item);
2127 Py_DECREF(iterator);
2128 listitem_free(li);
2129 return -1;
2130 }
2131
2132 Py_DECREF(item);
2133
2134 list_append(l, li);
2135 }
2136
2137 Py_DECREF(iterator);
2138
2139 /* Iterator may have finished due to an exception */
2140 if (PyErr_Occurred())
2141 return -1;
2142
2143 return 0;
2144}
2145
2146 static PyObject *
2147ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2148{
2149 list_T *list;
2150 PyObject *obj = NULL;
2151
2152 if (kwargs)
2153 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002154 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002155 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002156 return NULL;
2157 }
2158
2159 if (!PyArg_ParseTuple(args, "|O", &obj))
2160 return NULL;
2161
2162 if (!(list = py_list_alloc()))
2163 return NULL;
2164
2165 if (obj)
2166 {
2167 PyObject *lookup_dict;
2168
2169 if (!(lookup_dict = PyDict_New()))
2170 {
2171 list_unref(list);
2172 return NULL;
2173 }
2174
2175 if (list_py_concat(list, obj, lookup_dict) == -1)
2176 {
2177 Py_DECREF(lookup_dict);
2178 list_unref(list);
2179 return NULL;
2180 }
2181
2182 Py_DECREF(lookup_dict);
2183 }
2184
2185 return ListNew(subtype, list);
2186}
2187
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002188 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002189ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002190{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002191 pyll_remove(&self->ref, &lastlist);
2192 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002193
2194 DESTRUCTOR_FINISH(self);
2195}
2196
Bram Moolenaardb913952012-06-29 12:54:53 +02002197 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002198ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002199{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002200 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002201}
2202
2203 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002204ListItem(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002205{
2206 listitem_T *li;
2207
Bram Moolenaard6e39182013-05-21 18:30:34 +02002208 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002209 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002210 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002211 return NULL;
2212 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002213 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002214 if (li == NULL)
2215 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002216 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002217 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002218 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002219 return NULL;
2220 }
2221 return ConvertToPyObject(&li->li_tv);
2222}
2223
2224#define PROC_RANGE \
2225 if (last < 0) {\
2226 if (last < -size) \
2227 last = 0; \
2228 else \
2229 last += size; \
2230 } \
2231 if (first < 0) \
2232 first = 0; \
2233 if (first > size) \
2234 first = size; \
2235 if (last > size) \
2236 last = size;
2237
2238 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002239ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last)
Bram Moolenaardb913952012-06-29 12:54:53 +02002240{
2241 PyInt i;
2242 PyInt size = ListLength(self);
2243 PyInt n;
2244 PyObject *list;
2245 int reversed = 0;
2246
2247 PROC_RANGE
2248 if (first >= last)
2249 first = last;
2250
2251 n = last-first;
2252 list = PyList_New(n);
2253 if (list == NULL)
2254 return NULL;
2255
2256 for (i = 0; i < n; ++i)
2257 {
Bram Moolenaar24b11fb2013-04-05 19:32:36 +02002258 PyObject *item = ListItem(self, first + i);
Bram Moolenaardb913952012-06-29 12:54:53 +02002259 if (item == NULL)
2260 {
2261 Py_DECREF(list);
2262 return NULL;
2263 }
2264
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02002265 PyList_SET_ITEM(list, ((reversed)?(n-i-1):(i)), item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002266 }
2267
2268 return list;
2269}
2270
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002271typedef struct
2272{
2273 listwatch_T lw;
2274 list_T *list;
2275} listiterinfo_T;
2276
2277 static void
2278ListIterDestruct(listiterinfo_T *lii)
2279{
2280 list_rem_watch(lii->list, &lii->lw);
2281 PyMem_Free(lii);
2282}
2283
2284 static PyObject *
2285ListIterNext(listiterinfo_T **lii)
2286{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002287 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002288
2289 if (!((*lii)->lw.lw_item))
2290 return NULL;
2291
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002292 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002293 return NULL;
2294
2295 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2296
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002297 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002298}
2299
2300 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002301ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002302{
2303 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002304 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002305
2306 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2307 {
2308 PyErr_NoMemory();
2309 return NULL;
2310 }
2311
2312 list_add_watch(l, &lii->lw);
2313 lii->lw.lw_item = l->lv_first;
2314 lii->list = l;
2315
2316 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002317 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2318 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002319}
2320
Bram Moolenaardb913952012-06-29 12:54:53 +02002321 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002322ListAssItem(ListObject *self, Py_ssize_t index, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002323{
2324 typval_T tv;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002325 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002326 listitem_T *li;
2327 Py_ssize_t length = ListLength(self);
2328
2329 if (l->lv_lock)
2330 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002331 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002332 return -1;
2333 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002334 if (index > length || (index == length && obj == NULL))
Bram Moolenaardb913952012-06-29 12:54:53 +02002335 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002336 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002337 return -1;
2338 }
2339
2340 if (obj == NULL)
2341 {
2342 li = list_find(l, (long) index);
2343 list_remove(l, li, li);
2344 clear_tv(&li->li_tv);
2345 vim_free(li);
2346 return 0;
2347 }
2348
2349 if (ConvertFromPyObject(obj, &tv) == -1)
2350 return -1;
2351
2352 if (index == length)
2353 {
2354 if (list_append_tv(l, &tv) == FAIL)
2355 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002356 clear_tv(&tv);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002357 PyErr_SET_VIM(N_("failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002358 return -1;
2359 }
2360 }
2361 else
2362 {
2363 li = list_find(l, (long) index);
2364 clear_tv(&li->li_tv);
2365 copy_tv(&tv, &li->li_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002366 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002367 }
2368 return 0;
2369}
2370
2371 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002372ListAssSlice(ListObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002373{
2374 PyInt size = ListLength(self);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002375 PyObject *iterator;
2376 PyObject *item;
Bram Moolenaardb913952012-06-29 12:54:53 +02002377 listitem_T *li;
2378 listitem_T *next;
2379 typval_T v;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002380 list_T *l = self->list;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002381 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002382
2383 if (l->lv_lock)
2384 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002385 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002386 return -1;
2387 }
2388
2389 PROC_RANGE
2390
2391 if (first == size)
2392 li = NULL;
2393 else
2394 {
2395 li = list_find(l, (long) first);
2396 if (li == NULL)
2397 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002398 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2399 (int)first);
Bram Moolenaardb913952012-06-29 12:54:53 +02002400 return -1;
2401 }
2402 if (last > first)
2403 {
2404 i = last - first;
2405 while (i-- && li != NULL)
2406 {
2407 next = li->li_next;
2408 listitem_remove(l, li);
2409 li = next;
2410 }
2411 }
2412 }
2413
2414 if (obj == NULL)
2415 return 0;
2416
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002417 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002418 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02002419
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002420 while ((item = PyIter_Next(iterator)))
Bram Moolenaardb913952012-06-29 12:54:53 +02002421 {
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002422 if (ConvertFromPyObject(item, &v) == -1)
2423 {
2424 Py_DECREF(iterator);
2425 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002426 return -1;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002427 }
2428 Py_DECREF(item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002429 if (list_insert_tv(l, &v, li) == FAIL)
2430 {
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002431 clear_tv(&v);
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002432 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002433 return -1;
2434 }
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002435 clear_tv(&v);
Bram Moolenaardb913952012-06-29 12:54:53 +02002436 }
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002437 Py_DECREF(iterator);
Bram Moolenaardb913952012-06-29 12:54:53 +02002438 return 0;
2439}
2440
2441 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002442ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002443{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002444 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002445 PyObject *lookup_dict;
2446
2447 if (l->lv_lock)
2448 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002449 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002450 return NULL;
2451 }
2452
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002453 if (!(lookup_dict = PyDict_New()))
2454 return NULL;
2455
Bram Moolenaardb913952012-06-29 12:54:53 +02002456 if (list_py_concat(l, obj, lookup_dict) == -1)
2457 {
2458 Py_DECREF(lookup_dict);
2459 return NULL;
2460 }
2461 Py_DECREF(lookup_dict);
2462
2463 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002464 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002465}
2466
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002467static char *ListAttrs[] = {
2468 "locked",
2469 NULL
2470};
2471
2472 static PyObject *
2473ListDir(PyObject *self)
2474{
2475 return ObjectDir(self, ListAttrs);
2476}
2477
Bram Moolenaar66b79852012-09-21 14:00:35 +02002478 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002479ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002480{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002481 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002482 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002483 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002484 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002485 return -1;
2486 }
2487
2488 if (strcmp(name, "locked") == 0)
2489 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002490 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002491 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002492 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002493 return -1;
2494 }
2495 else
2496 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002497 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002498 if (istrue == -1)
2499 return -1;
2500 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002501 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002502 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002503 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002504 }
2505 return 0;
2506 }
2507 else
2508 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002509 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002510 return -1;
2511 }
2512}
2513
Bram Moolenaardb913952012-06-29 12:54:53 +02002514static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002515 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2516 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2517 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002518};
2519
2520typedef struct
2521{
2522 PyObject_HEAD
2523 char_u *name;
2524} FunctionObject;
2525
2526static PyTypeObject FunctionType;
2527
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002528#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2529
Bram Moolenaardb913952012-06-29 12:54:53 +02002530 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002531FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002532{
2533 FunctionObject *self;
2534
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002535 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2536
Bram Moolenaardb913952012-06-29 12:54:53 +02002537 if (self == NULL)
2538 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002539
2540 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002541 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002542 if (!translated_function_exists(name))
2543 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002544 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002545 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002546 return NULL;
2547 }
2548 self->name = vim_strsave(name);
2549 func_ref(self->name);
2550 }
2551 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002552 if ((self->name = get_expanded_name(name,
2553 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2554 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002555 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002556 PyErr_FORMAT(PyExc_ValueError,
2557 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002558 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002559 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002560
2561 return (PyObject *)(self);
2562}
2563
2564 static PyObject *
2565FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2566{
2567 PyObject *self;
2568 char_u *name;
2569
2570 if (kwargs)
2571 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002572 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002573 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002574 return NULL;
2575 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002576
Bram Moolenaar389a1792013-06-23 13:00:44 +02002577 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002578 return NULL;
2579
2580 self = FunctionNew(subtype, name);
2581
Bram Moolenaar389a1792013-06-23 13:00:44 +02002582 PyMem_Free(name);
2583
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002584 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002585}
2586
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002587 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002588FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002589{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002590 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002591 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002592
2593 DESTRUCTOR_FINISH(self);
2594}
2595
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002596static char *FunctionAttrs[] = {
2597 "softspace",
2598 NULL
2599};
2600
2601 static PyObject *
2602FunctionDir(PyObject *self)
2603{
2604 return ObjectDir(self, FunctionAttrs);
2605}
2606
Bram Moolenaardb913952012-06-29 12:54:53 +02002607 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002608FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002609{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002610 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002611 typval_T args;
2612 typval_T selfdicttv;
2613 typval_T rettv;
2614 dict_T *selfdict = NULL;
2615 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002616 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002617 int error;
2618
2619 if (ConvertFromPyObject(argsObject, &args) == -1)
2620 return NULL;
2621
2622 if (kwargs != NULL)
2623 {
2624 selfdictObject = PyDict_GetItemString(kwargs, "self");
2625 if (selfdictObject != NULL)
2626 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002627 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002628 {
2629 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002630 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002631 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002632 selfdict = selfdicttv.vval.v_dict;
2633 }
2634 }
2635
Bram Moolenaar71700b82013-05-15 17:49:05 +02002636 Py_BEGIN_ALLOW_THREADS
2637 Python_Lock_Vim();
2638
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002639 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002640 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002641
2642 Python_Release_Vim();
2643 Py_END_ALLOW_THREADS
2644
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002645 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002646 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002647 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002648 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002649 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002650 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002651 }
2652 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002653 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002654
Bram Moolenaardb913952012-06-29 12:54:53 +02002655 clear_tv(&args);
2656 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002657 if (selfdict != NULL)
2658 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002659
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002660 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002661}
2662
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002663 static PyObject *
2664FunctionRepr(FunctionObject *self)
2665{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002666#ifdef Py_TRACE_REFS
2667 /* For unknown reason self->name may be NULL after calling
2668 * Finalize */
2669 return PyString_FromFormat("<vim.Function '%s'>",
2670 (self->name == NULL ? "<NULL>" : (char *) self->name));
2671#else
2672 return PyString_FromFormat("<vim.Function '%s'>", (char *) self->name);
2673#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002674}
2675
Bram Moolenaardb913952012-06-29 12:54:53 +02002676static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002677 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2678 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002679};
2680
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002681/*
2682 * Options object
2683 */
2684
2685static PyTypeObject OptionsType;
2686
2687typedef int (*checkfun)(void *);
2688
2689typedef struct
2690{
2691 PyObject_HEAD
2692 int opt_type;
2693 void *from;
2694 checkfun Check;
2695 PyObject *fromObj;
2696} OptionsObject;
2697
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002698 static int
2699dummy_check(void *arg UNUSED)
2700{
2701 return 0;
2702}
2703
2704 static PyObject *
2705OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2706{
2707 OptionsObject *self;
2708
Bram Moolenaar774267b2013-05-21 20:51:59 +02002709 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002710 if (self == NULL)
2711 return NULL;
2712
2713 self->opt_type = opt_type;
2714 self->from = from;
2715 self->Check = Check;
2716 self->fromObj = fromObj;
2717 if (fromObj)
2718 Py_INCREF(fromObj);
2719
2720 return (PyObject *)(self);
2721}
2722
2723 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002724OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002725{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002726 PyObject_GC_UnTrack((void *)(self));
2727 Py_XDECREF(self->fromObj);
2728 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002729}
2730
2731 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002732OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002733{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002734 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002735 return 0;
2736}
2737
2738 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002739OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002740{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002741 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002742 return 0;
2743}
2744
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002745 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002746OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002747{
2748 char_u *key;
2749 int flags;
2750 long numval;
2751 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002752 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002753
Bram Moolenaard6e39182013-05-21 18:30:34 +02002754 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002755 return NULL;
2756
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002757 if (!(key = StringToChars(keyObject, &todecref)))
2758 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002759
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002760 if (*key == NUL)
2761 {
2762 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002763 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002764 return NULL;
2765 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002766
2767 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002768 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002769
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002770 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002771
2772 if (flags == 0)
2773 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002774 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002775 return NULL;
2776 }
2777
2778 if (flags & SOPT_UNSET)
2779 {
2780 Py_INCREF(Py_None);
2781 return Py_None;
2782 }
2783 else if (flags & SOPT_BOOL)
2784 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002785 PyObject *ret;
2786 ret = numval ? Py_True : Py_False;
2787 Py_INCREF(ret);
2788 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002789 }
2790 else if (flags & SOPT_NUM)
2791 return PyInt_FromLong(numval);
2792 else if (flags & SOPT_STRING)
2793 {
2794 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002795 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002796 PyObject *ret = PyBytes_FromString((char *) stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002797 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002798 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002799 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002800 else
2801 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002802 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002803 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002804 return NULL;
2805 }
2806 }
2807 else
2808 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002809 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002810 return NULL;
2811 }
2812}
2813
2814 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002815set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002816{
2817 char_u *errmsg;
2818
2819 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2820 {
2821 if (VimTryEnd())
2822 return FAIL;
2823 PyErr_SetVim((char *)errmsg);
2824 return FAIL;
2825 }
2826 return OK;
2827}
2828
2829 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002830set_option_value_for(
2831 char_u *key,
2832 int numval,
2833 char_u *stringval,
2834 int opt_flags,
2835 int opt_type,
2836 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002837{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002838 win_T *save_curwin = NULL;
2839 tabpage_T *save_curtab = NULL;
2840 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002841 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002842
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002843 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002844 switch (opt_type)
2845 {
2846 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002847 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002848 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002849 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002850 if (VimTryEnd())
2851 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002852 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002853 return -1;
2854 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002855 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
2856 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002857 break;
2858 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002859 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002860 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002861 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002862 break;
2863 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002864 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002865 break;
2866 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002867 if (set_ret == FAIL)
2868 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002869 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002870}
2871
2872 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002873OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002874{
2875 char_u *key;
2876 int flags;
2877 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002878 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002879 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002880
Bram Moolenaard6e39182013-05-21 18:30:34 +02002881 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002882 return -1;
2883
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002884 if (!(key = StringToChars(keyObject, &todecref)))
2885 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002886
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002887 if (*key == NUL)
2888 {
2889 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002890 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002891 return -1;
2892 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002893
2894 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002895 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002896
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002897 if (flags == 0)
2898 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002899 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002900 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002901 return -1;
2902 }
2903
2904 if (valObject == NULL)
2905 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002906 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002907 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002908 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002909 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002910 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002911 return -1;
2912 }
2913 else if (!(flags & SOPT_GLOBAL))
2914 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002915 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002916 N_("unable to unset option %s "
2917 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002918 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002919 return -1;
2920 }
2921 else
2922 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002923 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002924 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002925 return 0;
2926 }
2927 }
2928
Bram Moolenaard6e39182013-05-21 18:30:34 +02002929 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002930
2931 if (flags & SOPT_BOOL)
2932 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002933 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002934
Bram Moolenaarb983f752013-05-15 16:11:50 +02002935 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002936 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002937 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002938 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002939 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002940 }
2941 else if (flags & SOPT_NUM)
2942 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002943 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002944
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002945 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002946 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002947 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002948 return -1;
2949 }
2950
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002951 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002952 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002953 }
2954 else
2955 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002956 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002957 PyObject *todecref;
2958
2959 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002960 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002961 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002962 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002963 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002964 }
2965
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002966 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002967
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002968 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002969}
2970
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002971static PyMappingMethods OptionsAsMapping = {
2972 (lenfunc) NULL,
2973 (binaryfunc) OptionsItem,
2974 (objobjargproc) OptionsAssItem,
2975};
2976
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002977/* Tabpage object
2978 */
2979
2980typedef struct
2981{
2982 PyObject_HEAD
2983 tabpage_T *tab;
2984} TabPageObject;
2985
2986static PyObject *WinListNew(TabPageObject *tabObject);
2987
2988static PyTypeObject TabPageType;
2989
2990 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002991CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002992{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002993 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002994 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002995 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002996 return -1;
2997 }
2998
2999 return 0;
3000}
3001
3002 static PyObject *
3003TabPageNew(tabpage_T *tab)
3004{
3005 TabPageObject *self;
3006
3007 if (TAB_PYTHON_REF(tab))
3008 {
3009 self = TAB_PYTHON_REF(tab);
3010 Py_INCREF(self);
3011 }
3012 else
3013 {
3014 self = PyObject_NEW(TabPageObject, &TabPageType);
3015 if (self == NULL)
3016 return NULL;
3017 self->tab = tab;
3018 TAB_PYTHON_REF(tab) = self;
3019 }
3020
3021 return (PyObject *)(self);
3022}
3023
3024 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003025TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003026{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003027 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3028 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003029
3030 DESTRUCTOR_FINISH(self);
3031}
3032
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003033static char *TabPageAttrs[] = {
3034 "windows", "number", "vars", "window", "valid",
3035 NULL
3036};
3037
3038 static PyObject *
3039TabPageDir(PyObject *self)
3040{
3041 return ObjectDir(self, TabPageAttrs);
3042}
3043
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003044 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003045TabPageAttrValid(TabPageObject *self, char *name)
3046{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003047 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003048
3049 if (strcmp(name, "valid") != 0)
3050 return NULL;
3051
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003052 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3053 Py_INCREF(ret);
3054 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003055}
3056
3057 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003058TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003059{
3060 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003061 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003062 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003063 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003064 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003065 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003066 else if (strcmp(name, "window") == 0)
3067 {
3068 /* For current tab window.c does not bother to set or update tp_curwin
3069 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003070 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003071 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003072 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003073 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003074 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003075 else if (strcmp(name, "__members__") == 0)
3076 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003077 return NULL;
3078}
3079
3080 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003081TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003082{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003083 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003084 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003085 else
3086 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003087 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003088
3089 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003090 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3091 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003092 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003093 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003094 }
3095}
3096
3097static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003098 /* name, function, calling, documentation */
3099 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3100 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003101};
3102
3103/*
3104 * Window list object
3105 */
3106
3107static PyTypeObject TabListType;
3108static PySequenceMethods TabListAsSeq;
3109
3110typedef struct
3111{
3112 PyObject_HEAD
3113} TabListObject;
3114
3115 static PyInt
3116TabListLength(PyObject *self UNUSED)
3117{
3118 tabpage_T *tp = first_tabpage;
3119 PyInt n = 0;
3120
3121 while (tp != NULL)
3122 {
3123 ++n;
3124 tp = tp->tp_next;
3125 }
3126
3127 return n;
3128}
3129
3130 static PyObject *
3131TabListItem(PyObject *self UNUSED, PyInt n)
3132{
3133 tabpage_T *tp;
3134
3135 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3136 if (n == 0)
3137 return TabPageNew(tp);
3138
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003139 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003140 return NULL;
3141}
3142
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003143/*
3144 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003145 */
3146
3147typedef struct
3148{
3149 PyObject_HEAD
3150 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003151 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003152} WindowObject;
3153
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003154static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003155
3156 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003157CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003158{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003159 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003160 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003161 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003162 return -1;
3163 }
3164
3165 return 0;
3166}
3167
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003168 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003169WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003170{
3171 /* We need to handle deletion of windows underneath us.
3172 * If we add a "w_python*_ref" field to the win_T structure,
3173 * then we can get at it in win_free() in vim. We then
3174 * need to create only ONE Python object per window - if
3175 * we try to create a second, just INCREF the existing one
3176 * and return it. The (single) Python object referring to
3177 * the window is stored in "w_python*_ref".
3178 * On a win_free() we set the Python object's win_T* field
3179 * to an invalid value. We trap all uses of a window
3180 * object, and reject them if the win_T* field is invalid.
3181 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003182 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003183 * w_python_ref and w_python3_ref fields respectively.
3184 */
3185
3186 WindowObject *self;
3187
3188 if (WIN_PYTHON_REF(win))
3189 {
3190 self = WIN_PYTHON_REF(win);
3191 Py_INCREF(self);
3192 }
3193 else
3194 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003195 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003196 if (self == NULL)
3197 return NULL;
3198 self->win = win;
3199 WIN_PYTHON_REF(win) = self;
3200 }
3201
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003202 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3203
Bram Moolenaar971db462013-05-12 18:44:48 +02003204 return (PyObject *)(self);
3205}
3206
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003207 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003208WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003209{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003210 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003211 if (self->win && self->win != INVALID_WINDOW_VALUE)
3212 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003213 Py_XDECREF(((PyObject *)(self->tabObject)));
3214 PyObject_GC_Del((void *)(self));
3215}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003216
Bram Moolenaar774267b2013-05-21 20:51:59 +02003217 static int
3218WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3219{
3220 Py_VISIT(((PyObject *)(self->tabObject)));
3221 return 0;
3222}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003223
Bram Moolenaar774267b2013-05-21 20:51:59 +02003224 static int
3225WindowClear(WindowObject *self)
3226{
3227 Py_CLEAR(self->tabObject);
3228 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003229}
3230
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003231 static win_T *
3232get_firstwin(TabPageObject *tabObject)
3233{
3234 if (tabObject)
3235 {
3236 if (CheckTabPage(tabObject))
3237 return NULL;
3238 /* For current tab window.c does not bother to set or update tp_firstwin
3239 */
3240 else if (tabObject->tab == curtab)
3241 return firstwin;
3242 else
3243 return tabObject->tab->tp_firstwin;
3244 }
3245 else
3246 return firstwin;
3247}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003248static char *WindowAttrs[] = {
3249 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3250 "tabpage", "valid",
3251 NULL
3252};
3253
3254 static PyObject *
3255WindowDir(PyObject *self)
3256{
3257 return ObjectDir(self, WindowAttrs);
3258}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003259
Bram Moolenaar971db462013-05-12 18:44:48 +02003260 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003261WindowAttrValid(WindowObject *self, char *name)
3262{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003263 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003264
3265 if (strcmp(name, "valid") != 0)
3266 return NULL;
3267
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003268 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3269 Py_INCREF(ret);
3270 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003271}
3272
3273 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003274WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003275{
3276 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003277 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003278 else if (strcmp(name, "cursor") == 0)
3279 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003280 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003281
3282 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3283 }
3284 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003285 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003286#ifdef FEAT_WINDOWS
3287 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003288 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003289#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003290#ifdef FEAT_VERTSPLIT
3291 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003292 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003293 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003294 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003295#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003296 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003297 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003298 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003299 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3300 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003301 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003302 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003303 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003304 return NULL;
3305 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003306 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003307 }
3308 else if (strcmp(name, "tabpage") == 0)
3309 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003310 Py_INCREF(self->tabObject);
3311 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003312 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003313 else if (strcmp(name, "__members__") == 0)
3314 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003315 else
3316 return NULL;
3317}
3318
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003319 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003320WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003321{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003322 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003323 return -1;
3324
3325 if (strcmp(name, "buffer") == 0)
3326 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003327 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003328 return -1;
3329 }
3330 else if (strcmp(name, "cursor") == 0)
3331 {
3332 long lnum;
3333 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003334
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003335 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003336 return -1;
3337
Bram Moolenaard6e39182013-05-21 18:30:34 +02003338 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003339 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003340 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003341 return -1;
3342 }
3343
3344 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003345 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003346 return -1;
3347
Bram Moolenaard6e39182013-05-21 18:30:34 +02003348 self->win->w_cursor.lnum = lnum;
3349 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003350#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003351 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003352#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003353 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003354 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003355
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003356 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003357 return 0;
3358 }
3359 else if (strcmp(name, "height") == 0)
3360 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003361 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003362 win_T *savewin;
3363
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003364 if (NumberToLong(valObject, &height, NUMBER_INT))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003365 return -1;
3366
3367#ifdef FEAT_GUI
3368 need_mouse_correct = TRUE;
3369#endif
3370 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003371 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003372
3373 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003374 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003375 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003376 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003377 return -1;
3378
3379 return 0;
3380 }
3381#ifdef FEAT_VERTSPLIT
3382 else if (strcmp(name, "width") == 0)
3383 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003384 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003385 win_T *savewin;
3386
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003387 if (NumberToLong(valObject, &width, NUMBER_INT))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003388 return -1;
3389
3390#ifdef FEAT_GUI
3391 need_mouse_correct = TRUE;
3392#endif
3393 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003394 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003395
3396 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003397 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003398 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003399 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003400 return -1;
3401
3402 return 0;
3403 }
3404#endif
3405 else
3406 {
3407 PyErr_SetString(PyExc_AttributeError, name);
3408 return -1;
3409 }
3410}
3411
3412 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003413WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003414{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003415 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003416 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003417 else
3418 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003419 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003420
Bram Moolenaar6d216452013-05-12 19:00:41 +02003421 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003422 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003423 (self));
3424 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003425 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003426 }
3427}
3428
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003429static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003430 /* name, function, calling, documentation */
3431 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3432 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003433};
3434
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003435/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003436 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003437 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003438
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003439static PyTypeObject WinListType;
3440static PySequenceMethods WinListAsSeq;
3441
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003442typedef struct
3443{
3444 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003445 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003446} WinListObject;
3447
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003448 static PyObject *
3449WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003450{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003451 WinListObject *self;
3452
3453 self = PyObject_NEW(WinListObject, &WinListType);
3454 self->tabObject = tabObject;
3455 Py_INCREF(tabObject);
3456
3457 return (PyObject *)(self);
3458}
3459
3460 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003461WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003462{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003463 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003464
3465 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003466 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003467 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003468 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003469
3470 DESTRUCTOR_FINISH(self);
3471}
3472
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003473 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003474WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003475{
3476 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003477 PyInt n = 0;
3478
Bram Moolenaard6e39182013-05-21 18:30:34 +02003479 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003480 return -1;
3481
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003482 while (w != NULL)
3483 {
3484 ++n;
3485 w = W_NEXT(w);
3486 }
3487
3488 return n;
3489}
3490
3491 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003492WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003493{
3494 win_T *w;
3495
Bram Moolenaard6e39182013-05-21 18:30:34 +02003496 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003497 return NULL;
3498
3499 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003500 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003501 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003502
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003503 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003504 return NULL;
3505}
3506
3507/* Convert a Python string into a Vim line.
3508 *
3509 * The result is in allocated memory. All internal nulls are replaced by
3510 * newline characters. It is an error for the string to contain newline
3511 * characters.
3512 *
3513 * On errors, the Python exception data is set, and NULL is returned.
3514 */
3515 static char *
3516StringToLine(PyObject *obj)
3517{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003518 char *str;
3519 char *save;
3520 PyObject *bytes = NULL;
3521 Py_ssize_t len;
3522 PyInt i;
3523 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003524
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003525 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003526 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003527 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3528 || str == NULL)
3529 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003530 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003531 else if (PyUnicode_Check(obj))
3532 {
3533 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3534 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003535
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003536 if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
3537 || str == NULL)
3538 {
3539 Py_DECREF(bytes);
3540 return NULL;
3541 }
3542 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003543
3544 /*
3545 * Error checking: String must not contain newlines, as we
3546 * are replacing a single line, and we must replace it with
3547 * a single line.
3548 * A trailing newline is removed, so that append(f.readlines()) works.
3549 */
3550 p = memchr(str, '\n', len);
3551 if (p != NULL)
3552 {
3553 if (p == str + len - 1)
3554 --len;
3555 else
3556 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003557 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003558 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003559 return NULL;
3560 }
3561 }
3562
3563 /* Create a copy of the string, with internal nulls replaced by
3564 * newline characters, as is the vim convention.
3565 */
3566 save = (char *)alloc((unsigned)(len+1));
3567 if (save == NULL)
3568 {
3569 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003570 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003571 return NULL;
3572 }
3573
3574 for (i = 0; i < len; ++i)
3575 {
3576 if (str[i] == '\0')
3577 save[i] = '\n';
3578 else
3579 save[i] = str[i];
3580 }
3581
3582 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003583 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003584
3585 return save;
3586}
3587
3588/* Get a line from the specified buffer. The line number is
3589 * in Vim format (1-based). The line is returned as a Python
3590 * string object.
3591 */
3592 static PyObject *
3593GetBufferLine(buf_T *buf, PyInt n)
3594{
3595 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3596}
3597
3598
3599/* Get a list of lines from the specified buffer. The line numbers
3600 * are in Vim format (1-based). The range is from lo up to, but not
3601 * including, hi. The list is returned as a Python list of string objects.
3602 */
3603 static PyObject *
3604GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3605{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003606 PyInt i;
3607 PyInt n = hi - lo;
3608 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003609
3610 if (list == NULL)
3611 return NULL;
3612
3613 for (i = 0; i < n; ++i)
3614 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003615 PyObject *string = LineToString(
3616 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003617
3618 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003619 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003620 {
3621 Py_DECREF(list);
3622 return NULL;
3623 }
3624
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003625 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003626 }
3627
3628 /* The ownership of the Python list is passed to the caller (ie,
3629 * the caller should Py_DECREF() the object when it is finished
3630 * with it).
3631 */
3632
3633 return list;
3634}
3635
3636/*
3637 * Check if deleting lines made the cursor position invalid.
3638 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3639 * deleted).
3640 */
3641 static void
3642py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3643{
3644 if (curwin->w_cursor.lnum >= lo)
3645 {
3646 /* Adjust the cursor position if it's in/after the changed
3647 * lines. */
3648 if (curwin->w_cursor.lnum >= hi)
3649 {
3650 curwin->w_cursor.lnum += extra;
3651 check_cursor_col();
3652 }
3653 else if (extra < 0)
3654 {
3655 curwin->w_cursor.lnum = lo;
3656 check_cursor();
3657 }
3658 else
3659 check_cursor_col();
3660 changed_cline_bef_curs();
3661 }
3662 invalidate_botline();
3663}
3664
Bram Moolenaar19e60942011-06-19 00:27:51 +02003665/*
3666 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003667 * in Vim format (1-based). The replacement line is given as
3668 * a Python string object. The object is checked for validity
3669 * and correct format. Errors are returned as a value of FAIL.
3670 * The return value is OK on success.
3671 * If OK is returned and len_change is not NULL, *len_change
3672 * is set to the change in the buffer length.
3673 */
3674 static int
3675SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3676{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003677 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003678 * There are three cases:
3679 * 1. NULL, or None - this is a deletion.
3680 * 2. A string - this is a replacement.
3681 * 3. Anything else - this is an error.
3682 */
3683 if (line == Py_None || line == NULL)
3684 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003685 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003686
3687 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003688 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003689
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003690 VimTryStart();
3691
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003692 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003693 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003694 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003695 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003696 else
3697 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003698 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003699 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3700 deleted_lines_mark((linenr_T)n, 1L);
3701 }
3702
Bram Moolenaar105bc352013-05-17 16:03:57 +02003703 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003704
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003705 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003706 return FAIL;
3707
3708 if (len_change)
3709 *len_change = -1;
3710
3711 return OK;
3712 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003713 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003714 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003715 char *save = StringToLine(line);
3716 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003717
3718 if (save == NULL)
3719 return FAIL;
3720
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003721 VimTryStart();
3722
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003723 /* We do not need to free "save" if ml_replace() consumes it. */
3724 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003725 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003726
3727 if (u_savesub((linenr_T)n) == FAIL)
3728 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003729 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003730 vim_free(save);
3731 }
3732 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3733 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003734 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003735 vim_free(save);
3736 }
3737 else
3738 changed_bytes((linenr_T)n, 0);
3739
Bram Moolenaar105bc352013-05-17 16:03:57 +02003740 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003741
3742 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003743 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003744 check_cursor_col();
3745
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003746 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003747 return FAIL;
3748
3749 if (len_change)
3750 *len_change = 0;
3751
3752 return OK;
3753 }
3754 else
3755 {
3756 PyErr_BadArgument();
3757 return FAIL;
3758 }
3759}
3760
Bram Moolenaar19e60942011-06-19 00:27:51 +02003761/* Replace a range of lines in the specified buffer. The line numbers are in
3762 * Vim format (1-based). The range is from lo up to, but not including, hi.
3763 * The replacement lines are given as a Python list of string objects. The
3764 * list is checked for validity and correct format. Errors are returned as a
3765 * value of FAIL. The return value is OK on success.
3766 * If OK is returned and len_change is not NULL, *len_change
3767 * is set to the change in the buffer length.
3768 */
3769 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003770SetBufferLineList(
3771 buf_T *buf,
3772 PyInt lo,
3773 PyInt hi,
3774 PyObject *list,
3775 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003776{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003777 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003778 * There are three cases:
3779 * 1. NULL, or None - this is a deletion.
3780 * 2. A list - this is a replacement.
3781 * 3. Anything else - this is an error.
3782 */
3783 if (list == Py_None || list == NULL)
3784 {
3785 PyInt i;
3786 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003787 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003788
3789 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003790 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003791 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003792
3793 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003794 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003795 else
3796 {
3797 for (i = 0; i < n; ++i)
3798 {
3799 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3800 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003801 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003802 break;
3803 }
3804 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003805 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003806 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3807 deleted_lines_mark((linenr_T)lo, (long)i);
3808 }
3809
Bram Moolenaar105bc352013-05-17 16:03:57 +02003810 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003811
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003812 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003813 return FAIL;
3814
3815 if (len_change)
3816 *len_change = -n;
3817
3818 return OK;
3819 }
3820 else if (PyList_Check(list))
3821 {
3822 PyInt i;
3823 PyInt new_len = PyList_Size(list);
3824 PyInt old_len = hi - lo;
3825 PyInt extra = 0; /* lines added to text, can be negative */
3826 char **array;
3827 buf_T *savebuf;
3828
3829 if (new_len == 0) /* avoid allocating zero bytes */
3830 array = NULL;
3831 else
3832 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003833 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003834 if (array == NULL)
3835 {
3836 PyErr_NoMemory();
3837 return FAIL;
3838 }
3839 }
3840
3841 for (i = 0; i < new_len; ++i)
3842 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003843 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003844
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003845 if (!(line = PyList_GetItem(list, i)) ||
3846 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003847 {
3848 while (i)
3849 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003850 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003851 return FAIL;
3852 }
3853 }
3854
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003855 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003856 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003857
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003858 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003859 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003860
3861 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003862 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003863
3864 /* If the size of the range is reducing (ie, new_len < old_len) we
3865 * need to delete some old_len. We do this at the start, by
3866 * repeatedly deleting line "lo".
3867 */
3868 if (!PyErr_Occurred())
3869 {
3870 for (i = 0; i < old_len - new_len; ++i)
3871 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3872 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003873 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003874 break;
3875 }
3876 extra -= i;
3877 }
3878
3879 /* For as long as possible, replace the existing old_len with the
3880 * new old_len. This is a more efficient operation, as it requires
3881 * less memory allocation and freeing.
3882 */
3883 if (!PyErr_Occurred())
3884 {
3885 for (i = 0; i < old_len && i < new_len; ++i)
3886 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3887 == FAIL)
3888 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003889 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003890 break;
3891 }
3892 }
3893 else
3894 i = 0;
3895
3896 /* Now we may need to insert the remaining new old_len. If we do, we
3897 * must free the strings as we finish with them (we can't pass the
3898 * responsibility to vim in this case).
3899 */
3900 if (!PyErr_Occurred())
3901 {
3902 while (i < new_len)
3903 {
3904 if (ml_append((linenr_T)(lo + i - 1),
3905 (char_u *)array[i], 0, FALSE) == FAIL)
3906 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003907 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003908 break;
3909 }
3910 vim_free(array[i]);
3911 ++i;
3912 ++extra;
3913 }
3914 }
3915
3916 /* Free any left-over old_len, as a result of an error */
3917 while (i < new_len)
3918 {
3919 vim_free(array[i]);
3920 ++i;
3921 }
3922
3923 /* Free the array of old_len. All of its contents have now
3924 * been dealt with (either freed, or the responsibility passed
3925 * to vim.
3926 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003927 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003928
3929 /* Adjust marks. Invalidate any which lie in the
3930 * changed range, and move any in the remainder of the buffer.
3931 */
3932 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3933 (long)MAXLNUM, (long)extra);
3934 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3935
Bram Moolenaar105bc352013-05-17 16:03:57 +02003936 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003937 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3938
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003939 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003940 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003941
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003942 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003943 return FAIL;
3944
3945 if (len_change)
3946 *len_change = new_len - old_len;
3947
3948 return OK;
3949 }
3950 else
3951 {
3952 PyErr_BadArgument();
3953 return FAIL;
3954 }
3955}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003956
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003957/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003958 * The line number is in Vim format (1-based). The lines to be inserted are
3959 * given as a Python list of string objects or as a single string. The lines
3960 * to be added are checked for validity and correct format. Errors are
3961 * returned as a value of FAIL. The return value is OK on success.
3962 * If OK is returned and len_change is not NULL, *len_change
3963 * is set to the change in the buffer length.
3964 */
3965 static int
3966InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3967{
3968 /* First of all, we check the type of the supplied Python object.
3969 * It must be a string or a list, or the call is in error.
3970 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003971 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003972 {
3973 char *str = StringToLine(lines);
3974 buf_T *savebuf;
3975
3976 if (str == NULL)
3977 return FAIL;
3978
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003979 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003980 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003981 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003982
3983 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003984 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003985 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003986 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003987 else
3988 appended_lines_mark((linenr_T)n, 1L);
3989
3990 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003991 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003992 update_screen(VALID);
3993
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003994 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003995 return FAIL;
3996
3997 if (len_change)
3998 *len_change = 1;
3999
4000 return OK;
4001 }
4002 else if (PyList_Check(lines))
4003 {
4004 PyInt i;
4005 PyInt size = PyList_Size(lines);
4006 char **array;
4007 buf_T *savebuf;
4008
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004009 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004010 if (array == NULL)
4011 {
4012 PyErr_NoMemory();
4013 return FAIL;
4014 }
4015
4016 for (i = 0; i < size; ++i)
4017 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004018 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004019
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004020 if (!(line = PyList_GetItem(lines, i)) ||
4021 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004022 {
4023 while (i)
4024 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004025 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004026 return FAIL;
4027 }
4028 }
4029
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004030 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004031 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004032 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004033
4034 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004035 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004036 else
4037 {
4038 for (i = 0; i < size; ++i)
4039 {
4040 if (ml_append((linenr_T)(n + i),
4041 (char_u *)array[i], 0, FALSE) == FAIL)
4042 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004043 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004044
4045 /* Free the rest of the lines */
4046 while (i < size)
4047 vim_free(array[i++]);
4048
4049 break;
4050 }
4051 vim_free(array[i]);
4052 }
4053 if (i > 0)
4054 appended_lines_mark((linenr_T)n, (long)i);
4055 }
4056
4057 /* Free the array of lines. All of its contents have now
4058 * been freed.
4059 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004060 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004061
Bram Moolenaar105bc352013-05-17 16:03:57 +02004062 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004063 update_screen(VALID);
4064
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004065 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004066 return FAIL;
4067
4068 if (len_change)
4069 *len_change = size;
4070
4071 return OK;
4072 }
4073 else
4074 {
4075 PyErr_BadArgument();
4076 return FAIL;
4077 }
4078}
4079
4080/*
4081 * Common routines for buffers and line ranges
4082 * -------------------------------------------
4083 */
4084
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004085typedef struct
4086{
4087 PyObject_HEAD
4088 buf_T *buf;
4089} BufferObject;
4090
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004092CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004094 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004095 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004096 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004097 return -1;
4098 }
4099
4100 return 0;
4101}
4102
4103 static PyObject *
4104RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4105{
4106 if (CheckBuffer(self))
4107 return NULL;
4108
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004109 if (end == -1)
4110 end = self->buf->b_ml.ml_line_count;
4111
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004112 if (n < 0)
4113 n += end - start + 1;
4114
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004115 if (n < 0 || n > end - start)
4116 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004117 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004118 return NULL;
4119 }
4120
4121 return GetBufferLine(self->buf, n+start);
4122}
4123
4124 static PyObject *
4125RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4126{
4127 PyInt size;
4128
4129 if (CheckBuffer(self))
4130 return NULL;
4131
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004132 if (end == -1)
4133 end = self->buf->b_ml.ml_line_count;
4134
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004135 size = end - start + 1;
4136
4137 if (lo < 0)
4138 lo = 0;
4139 else if (lo > size)
4140 lo = size;
4141 if (hi < 0)
4142 hi = 0;
4143 if (hi < lo)
4144 hi = lo;
4145 else if (hi > size)
4146 hi = size;
4147
4148 return GetBufferLineList(self->buf, lo+start, hi+start);
4149}
4150
4151 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004152RBAsItem(
4153 BufferObject *self,
4154 PyInt n,
4155 PyObject *valObject,
4156 PyInt start,
4157 PyInt end,
4158 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004159{
4160 PyInt len_change;
4161
4162 if (CheckBuffer(self))
4163 return -1;
4164
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004165 if (end == -1)
4166 end = self->buf->b_ml.ml_line_count;
4167
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004168 if (n < 0)
4169 n += end - start + 1;
4170
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004171 if (n < 0 || n > end - start)
4172 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004173 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004174 return -1;
4175 }
4176
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004177 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004178 return -1;
4179
4180 if (new_end)
4181 *new_end = end + len_change;
4182
4183 return 0;
4184}
4185
Bram Moolenaar19e60942011-06-19 00:27:51 +02004186 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004187RBAsSlice(
4188 BufferObject *self,
4189 PyInt lo,
4190 PyInt hi,
4191 PyObject *valObject,
4192 PyInt start,
4193 PyInt end,
4194 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004195{
4196 PyInt size;
4197 PyInt len_change;
4198
4199 /* Self must be a valid buffer */
4200 if (CheckBuffer(self))
4201 return -1;
4202
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004203 if (end == -1)
4204 end = self->buf->b_ml.ml_line_count;
4205
Bram Moolenaar19e60942011-06-19 00:27:51 +02004206 /* Sort out the slice range */
4207 size = end - start + 1;
4208
4209 if (lo < 0)
4210 lo = 0;
4211 else if (lo > size)
4212 lo = size;
4213 if (hi < 0)
4214 hi = 0;
4215 if (hi < lo)
4216 hi = lo;
4217 else if (hi > size)
4218 hi = size;
4219
4220 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004221 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004222 return -1;
4223
4224 if (new_end)
4225 *new_end = end + len_change;
4226
4227 return 0;
4228}
4229
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004230
4231 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004232RBAppend(
4233 BufferObject *self,
4234 PyObject *args,
4235 PyInt start,
4236 PyInt end,
4237 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004238{
4239 PyObject *lines;
4240 PyInt len_change;
4241 PyInt max;
4242 PyInt n;
4243
4244 if (CheckBuffer(self))
4245 return NULL;
4246
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004247 if (end == -1)
4248 end = self->buf->b_ml.ml_line_count;
4249
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004250 max = n = end - start + 1;
4251
4252 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4253 return NULL;
4254
4255 if (n < 0 || n > max)
4256 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004257 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004258 return NULL;
4259 }
4260
4261 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4262 return NULL;
4263
4264 if (new_end)
4265 *new_end = end + len_change;
4266
4267 Py_INCREF(Py_None);
4268 return Py_None;
4269}
4270
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004271/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004272 */
4273
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004274static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004275static PySequenceMethods RangeAsSeq;
4276static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004277
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004278typedef struct
4279{
4280 PyObject_HEAD
4281 BufferObject *buf;
4282 PyInt start;
4283 PyInt end;
4284} RangeObject;
4285
4286 static PyObject *
4287RangeNew(buf_T *buf, PyInt start, PyInt end)
4288{
4289 BufferObject *bufr;
4290 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004291 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004292 if (self == NULL)
4293 return NULL;
4294
4295 bufr = (BufferObject *)BufferNew(buf);
4296 if (bufr == NULL)
4297 {
4298 Py_DECREF(self);
4299 return NULL;
4300 }
4301 Py_INCREF(bufr);
4302
4303 self->buf = bufr;
4304 self->start = start;
4305 self->end = end;
4306
4307 return (PyObject *)(self);
4308}
4309
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004310 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004311RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004312{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004313 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004314 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004315 PyObject_GC_Del((void *)(self));
4316}
4317
4318 static int
4319RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4320{
4321 Py_VISIT(((PyObject *)(self->buf)));
4322 return 0;
4323}
4324
4325 static int
4326RangeClear(RangeObject *self)
4327{
4328 Py_CLEAR(self->buf);
4329 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004330}
4331
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004332 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004333RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004334{
4335 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004336 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004337 return -1; /* ??? */
4338
Bram Moolenaard6e39182013-05-21 18:30:34 +02004339 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004340}
4341
4342 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004343RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004344{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004345 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004346}
4347
4348 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004349RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004350{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004351 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004352}
4353
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004354static char *RangeAttrs[] = {
4355 "start", "end",
4356 NULL
4357};
4358
4359 static PyObject *
4360RangeDir(PyObject *self)
4361{
4362 return ObjectDir(self, RangeAttrs);
4363}
4364
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004365 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004366RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004367{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004368 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004369}
4370
4371 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004372RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004373{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004374 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004375 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4376 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004377 else
4378 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004379 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004380
4381 if (name == NULL)
4382 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004383
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004384 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004385 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004386 }
4387}
4388
4389static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004390 /* name, function, calling, documentation */
4391 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004392 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4393 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004394};
4395
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004396static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004397static PySequenceMethods BufferAsSeq;
4398static PyMappingMethods BufferAsMapping;
4399
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004400 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004401BufferNew(buf_T *buf)
4402{
4403 /* We need to handle deletion of buffers underneath us.
4404 * If we add a "b_python*_ref" field to the buf_T structure,
4405 * then we can get at it in buf_freeall() in vim. We then
4406 * need to create only ONE Python object per buffer - if
4407 * we try to create a second, just INCREF the existing one
4408 * and return it. The (single) Python object referring to
4409 * the buffer is stored in "b_python*_ref".
4410 * Question: what to do on a buf_freeall(). We'll probably
4411 * have to either delete the Python object (DECREF it to
4412 * zero - a bad idea, as it leaves dangling refs!) or
4413 * set the buf_T * value to an invalid value (-1?), which
4414 * means we need checks in all access functions... Bah.
4415 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004416 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004417 * b_python_ref and b_python3_ref fields respectively.
4418 */
4419
4420 BufferObject *self;
4421
4422 if (BUF_PYTHON_REF(buf) != NULL)
4423 {
4424 self = BUF_PYTHON_REF(buf);
4425 Py_INCREF(self);
4426 }
4427 else
4428 {
4429 self = PyObject_NEW(BufferObject, &BufferType);
4430 if (self == NULL)
4431 return NULL;
4432 self->buf = buf;
4433 BUF_PYTHON_REF(buf) = self;
4434 }
4435
4436 return (PyObject *)(self);
4437}
4438
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004439 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004440BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004441{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004442 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4443 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004444
4445 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004446}
4447
Bram Moolenaar971db462013-05-12 18:44:48 +02004448 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004449BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004450{
4451 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004452 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004453 return -1; /* ??? */
4454
Bram Moolenaard6e39182013-05-21 18:30:34 +02004455 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004456}
4457
4458 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004459BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004460{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004461 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004462}
4463
4464 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004465BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004466{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004467 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004468}
4469
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004470static char *BufferAttrs[] = {
4471 "name", "number", "vars", "options", "valid",
4472 NULL
4473};
4474
4475 static PyObject *
4476BufferDir(PyObject *self)
4477{
4478 return ObjectDir(self, BufferAttrs);
4479}
4480
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004481 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004482BufferAttrValid(BufferObject *self, char *name)
4483{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004484 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004485
4486 if (strcmp(name, "valid") != 0)
4487 return NULL;
4488
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004489 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4490 Py_INCREF(ret);
4491 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004492}
4493
4494 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004495BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004496{
4497 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004498 return PyString_FromString((self->buf->b_ffname == NULL
4499 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004500 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004501 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004502 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004503 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004504 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004505 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4506 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004507 else if (strcmp(name, "__members__") == 0)
4508 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004509 else
4510 return NULL;
4511}
4512
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004513 static int
4514BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4515{
4516 if (CheckBuffer(self))
4517 return -1;
4518
4519 if (strcmp(name, "name") == 0)
4520 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004521 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004522 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004523 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004524 PyObject *todecref;
4525
4526 if (!(val = StringToChars(valObject, &todecref)))
4527 return -1;
4528
4529 VimTryStart();
4530 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4531 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004532 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004533 aucmd_restbuf(&aco);
4534 Py_XDECREF(todecref);
4535 if (VimTryEnd())
4536 return -1;
4537
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004538 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004539 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004540 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004541 return -1;
4542 }
4543 return 0;
4544 }
4545 else
4546 {
4547 PyErr_SetString(PyExc_AttributeError, name);
4548 return -1;
4549 }
4550}
4551
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004552 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004553BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004554{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004555 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004556}
4557
4558 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004559BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004560{
4561 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004562 char_u *pmark;
4563 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004564 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004565 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004566
Bram Moolenaard6e39182013-05-21 18:30:34 +02004567 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004568 return NULL;
4569
Bram Moolenaar389a1792013-06-23 13:00:44 +02004570 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004571 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004572
Bram Moolenaar389a1792013-06-23 13:00:44 +02004573 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004574 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004575 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004576 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004577 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004578 return NULL;
4579 }
4580
4581 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004582
4583 Py_XDECREF(todecref);
4584
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004585 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004586 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004587 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004588 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004589 if (VimTryEnd())
4590 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004591
4592 if (posp == NULL)
4593 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004594 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004595 return NULL;
4596 }
4597
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004598 if (posp->lnum <= 0)
4599 {
4600 /* Or raise an error? */
4601 Py_INCREF(Py_None);
4602 return Py_None;
4603 }
4604
4605 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4606}
4607
4608 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004609BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004610{
4611 PyInt start;
4612 PyInt end;
4613
Bram Moolenaard6e39182013-05-21 18:30:34 +02004614 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004615 return NULL;
4616
4617 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4618 return NULL;
4619
Bram Moolenaard6e39182013-05-21 18:30:34 +02004620 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004621}
4622
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004623 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004624BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004625{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004626 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004627 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004628 else
4629 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004630 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004631
4632 if (name == NULL)
4633 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004634
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004635 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004636 }
4637}
4638
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004639static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004640 /* name, function, calling, documentation */
4641 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004642 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004643 {"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 +02004644 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4645 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004646};
4647
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004648/*
4649 * Buffer list object - Implementation
4650 */
4651
4652static PyTypeObject BufMapType;
4653
4654typedef struct
4655{
4656 PyObject_HEAD
4657} BufMapObject;
4658
4659 static PyInt
4660BufMapLength(PyObject *self UNUSED)
4661{
4662 buf_T *b = firstbuf;
4663 PyInt n = 0;
4664
4665 while (b)
4666 {
4667 ++n;
4668 b = b->b_next;
4669 }
4670
4671 return n;
4672}
4673
4674 static PyObject *
4675BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4676{
4677 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004678 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004679
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004680 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004681 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004682
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004683 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004684
4685 if (b)
4686 return BufferNew(b);
4687 else
4688 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004689 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004690 return NULL;
4691 }
4692}
4693
4694 static void
4695BufMapIterDestruct(PyObject *buffer)
4696{
4697 /* Iteration was stopped before all buffers were processed */
4698 if (buffer)
4699 {
4700 Py_DECREF(buffer);
4701 }
4702}
4703
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004704 static int
4705BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4706{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004707 if (buffer)
4708 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004709 return 0;
4710}
4711
4712 static int
4713BufMapIterClear(PyObject **buffer)
4714{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004715 if (*buffer)
4716 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004717 return 0;
4718}
4719
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004720 static PyObject *
4721BufMapIterNext(PyObject **buffer)
4722{
4723 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004724 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004725
4726 if (!*buffer)
4727 return NULL;
4728
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004729 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004730
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004731 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004732 {
4733 *buffer = NULL;
4734 return NULL;
4735 }
4736
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004737 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004738 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004739 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004740 return NULL;
4741 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004742 /* Do not increment reference: we no longer hold it (decref), but whoever
4743 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004744 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004745}
4746
4747 static PyObject *
4748BufMapIter(PyObject *self UNUSED)
4749{
4750 PyObject *buffer;
4751
4752 buffer = BufferNew(firstbuf);
4753 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004754 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4755 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004756}
4757
4758static PyMappingMethods BufMapAsMapping = {
4759 (lenfunc) BufMapLength,
4760 (binaryfunc) BufMapItem,
4761 (objobjargproc) 0,
4762};
4763
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004764/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004765 */
4766
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004767static char *CurrentAttrs[] = {
4768 "buffer", "window", "line", "range", "tabpage",
4769 NULL
4770};
4771
4772 static PyObject *
4773CurrentDir(PyObject *self)
4774{
4775 return ObjectDir(self, CurrentAttrs);
4776}
4777
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004778 static PyObject *
4779CurrentGetattr(PyObject *self UNUSED, char *name)
4780{
4781 if (strcmp(name, "buffer") == 0)
4782 return (PyObject *)BufferNew(curbuf);
4783 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004784 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004785 else if (strcmp(name, "tabpage") == 0)
4786 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004787 else if (strcmp(name, "line") == 0)
4788 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4789 else if (strcmp(name, "range") == 0)
4790 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004791 else if (strcmp(name, "__members__") == 0)
4792 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004793 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004794#if PY_MAJOR_VERSION < 3
4795 return Py_FindMethod(WindowMethods, self, name);
4796#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004797 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004798#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004799}
4800
4801 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004802CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004803{
4804 if (strcmp(name, "line") == 0)
4805 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004806 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4807 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004808 return -1;
4809
4810 return 0;
4811 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004812 else if (strcmp(name, "buffer") == 0)
4813 {
4814 int count;
4815
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004816 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004817 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004818 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004819 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004820 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004821 return -1;
4822 }
4823
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004824 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004825 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004826 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004827
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004828 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004829 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4830 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004831 if (VimTryEnd())
4832 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004833 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004834 return -1;
4835 }
4836
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004837 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004838 }
4839 else if (strcmp(name, "window") == 0)
4840 {
4841 int count;
4842
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004843 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004844 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004845 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004846 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004847 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004848 return -1;
4849 }
4850
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004851 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004852 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004853 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004854
4855 if (!count)
4856 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004857 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004858 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004859 return -1;
4860 }
4861
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004862 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004863 win_goto(((WindowObject *)(valObject))->win);
4864 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004865 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004866 if (VimTryEnd())
4867 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004868 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004869 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004870 return -1;
4871 }
4872
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004873 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004874 }
4875 else if (strcmp(name, "tabpage") == 0)
4876 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004877 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004878 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004879 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004880 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004881 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004882 return -1;
4883 }
4884
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004885 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004886 return -1;
4887
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004888 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004889 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
4890 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02004891 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004892 if (VimTryEnd())
4893 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004894 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004895 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004896 return -1;
4897 }
4898
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004899 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004900 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004901 else
4902 {
4903 PyErr_SetString(PyExc_AttributeError, name);
4904 return -1;
4905 }
4906}
4907
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004908static struct PyMethodDef CurrentMethods[] = {
4909 /* name, function, calling, documentation */
4910 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4911 { NULL, NULL, 0, NULL}
4912};
4913
Bram Moolenaardb913952012-06-29 12:54:53 +02004914 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004915init_range_cmd(exarg_T *eap)
4916{
4917 RangeStart = eap->line1;
4918 RangeEnd = eap->line2;
4919}
4920
4921 static void
4922init_range_eval(typval_T *rettv UNUSED)
4923{
4924 RangeStart = (PyInt) curwin->w_cursor.lnum;
4925 RangeEnd = RangeStart;
4926}
4927
4928 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004929run_cmd(const char *cmd, void *arg UNUSED
4930#ifdef PY_CAN_RECURSE
4931 , PyGILState_STATE *pygilstate UNUSED
4932#endif
4933 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004934{
4935 PyRun_SimpleString((char *) cmd);
4936}
4937
4938static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4939static int code_hdr_len = 30;
4940
4941 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004942run_do(const char *cmd, void *arg UNUSED
4943#ifdef PY_CAN_RECURSE
4944 , PyGILState_STATE *pygilstate
4945#endif
4946 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004947{
4948 PyInt lnum;
4949 size_t len;
4950 char *code;
4951 int status;
4952 PyObject *pyfunc, *pymain;
4953
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004954 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004955 {
4956 EMSG(_("cannot save undo information"));
4957 return;
4958 }
4959
4960 len = code_hdr_len + STRLEN(cmd);
4961 code = PyMem_New(char, len + 1);
4962 memcpy(code, code_hdr, code_hdr_len);
4963 STRCPY(code + code_hdr_len, cmd);
4964 status = PyRun_SimpleString(code);
4965 PyMem_Free(code);
4966
4967 if (status)
4968 {
4969 EMSG(_("failed to run the code"));
4970 return;
4971 }
4972
4973 status = 0;
4974 pymain = PyImport_AddModule("__main__");
4975 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004976#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004977 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004978#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004979
4980 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4981 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004982 PyObject *line;
4983 PyObject *linenr;
4984 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004985
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004986#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004987 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004988#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004989 if (!(line = GetBufferLine(curbuf, lnum)))
4990 goto err;
4991 if (!(linenr = PyInt_FromLong((long) lnum)))
4992 {
4993 Py_DECREF(line);
4994 goto err;
4995 }
4996 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
4997 Py_DECREF(line);
4998 Py_DECREF(linenr);
4999 if (!ret)
5000 goto err;
5001
5002 if (ret != Py_None)
5003 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5004 goto err;
5005
5006 Py_XDECREF(ret);
5007 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005008#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005009 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005010#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005011 }
5012 goto out;
5013err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005014#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005015 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005016#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005017 PyErr_PrintEx(0);
5018 PythonIO_Flush();
5019 status = 1;
5020out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005021#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005022 if (!status)
5023 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005024#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005025 Py_DECREF(pyfunc);
5026 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5027 if (status)
5028 return;
5029 check_cursor();
5030 update_curbuf(NOT_VALID);
5031}
5032
5033 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005034run_eval(const char *cmd, typval_T *rettv
5035#ifdef PY_CAN_RECURSE
5036 , PyGILState_STATE *pygilstate UNUSED
5037#endif
5038 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005039{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005040 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005041
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005042 run_ret = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
5043 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005044 {
5045 if (PyErr_Occurred() && !msg_silent)
5046 PyErr_PrintEx(0);
5047 EMSG(_("E858: Eval did not return a valid python object"));
5048 }
5049 else
5050 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005051 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005052 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005053 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005054 }
5055 PyErr_Clear();
5056}
5057
5058 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005059set_ref_in_py(const int copyID)
5060{
5061 pylinkedlist_T *cur;
5062 dict_T *dd;
5063 list_T *ll;
5064
5065 if (lastdict != NULL)
5066 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5067 {
5068 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5069 if (dd->dv_copyID != copyID)
5070 {
5071 dd->dv_copyID = copyID;
5072 set_ref_in_ht(&dd->dv_hashtab, copyID);
5073 }
5074 }
5075
5076 if (lastlist != NULL)
5077 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5078 {
5079 ll = ((ListObject *) (cur->pll_obj))->list;
5080 if (ll->lv_copyID != copyID)
5081 {
5082 ll->lv_copyID = copyID;
5083 set_ref_in_list(ll, copyID);
5084 }
5085 }
5086}
5087
5088 static int
5089set_string_copy(char_u *str, typval_T *tv)
5090{
5091 tv->vval.v_string = vim_strsave(str);
5092 if (tv->vval.v_string == NULL)
5093 {
5094 PyErr_NoMemory();
5095 return -1;
5096 }
5097 return 0;
5098}
5099
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005100 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005101pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005102{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005103 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005104 char_u *key;
5105 dictitem_T *di;
5106 PyObject *keyObject;
5107 PyObject *valObject;
5108 Py_ssize_t iter = 0;
5109
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005110 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005111 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005112
5113 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005114 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005115
5116 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5117 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005118 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005119
Bram Moolenaara03e6312013-05-29 22:49:26 +02005120 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005121 {
5122 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005123 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005124 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005125
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005126 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005127 {
5128 dict_unref(dict);
5129 return -1;
5130 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005131
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005132 if (*key == NUL)
5133 {
5134 dict_unref(dict);
5135 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005136 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005137 return -1;
5138 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005139
5140 di = dictitem_alloc(key);
5141
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005142 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005143
5144 if (di == NULL)
5145 {
5146 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005147 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005148 return -1;
5149 }
5150 di->di_tv.v_lock = 0;
5151
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005152 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005153 {
5154 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005155 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005156 return -1;
5157 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005158
5159 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005160 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005161 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005162 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005163 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005164 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005165 return -1;
5166 }
5167 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005168
5169 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005170 return 0;
5171}
5172
5173 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005174pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005175{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005176 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005177 char_u *key;
5178 dictitem_T *di;
5179 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005180 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005181 PyObject *keyObject;
5182 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005183
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005184 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005185 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005186
5187 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005188 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005189
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005190 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005191 {
5192 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005193 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005194 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005195
5196 if (!(iterator = PyObject_GetIter(list)))
5197 {
5198 dict_unref(dict);
5199 Py_DECREF(list);
5200 return -1;
5201 }
5202 Py_DECREF(list);
5203
5204 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005205 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005206 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005207
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005208 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005209 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005210 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005211 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005212 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005213 return -1;
5214 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005215
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005216 if (*key == NUL)
5217 {
5218 Py_DECREF(keyObject);
5219 Py_DECREF(iterator);
5220 Py_XDECREF(todecref);
5221 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005222 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005223 return -1;
5224 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005225
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005226 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005227 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005228 Py_DECREF(keyObject);
5229 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005230 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005231 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005232 return -1;
5233 }
5234
5235 di = dictitem_alloc(key);
5236
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005237 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005238 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005239
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005240 if (di == NULL)
5241 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005242 Py_DECREF(iterator);
5243 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005244 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005245 PyErr_NoMemory();
5246 return -1;
5247 }
5248 di->di_tv.v_lock = 0;
5249
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005250 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005251 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005252 Py_DECREF(iterator);
5253 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005254 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005255 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005256 return -1;
5257 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005258
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005259 Py_DECREF(valObject);
5260
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005261 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005262 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005263 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005264 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005265 dictitem_free(di);
5266 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005267 return -1;
5268 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005269 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005270 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005271 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005272 return 0;
5273}
5274
5275 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005276pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005277{
5278 list_T *l;
5279
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005280 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005281 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005282
5283 tv->v_type = VAR_LIST;
5284 tv->vval.v_list = l;
5285
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005286 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005287 {
5288 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005289 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005290 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005291
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005292 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005293 return 0;
5294}
5295
Bram Moolenaardb913952012-06-29 12:54:53 +02005296typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5297
5298 static int
5299convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005300 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005301{
5302 PyObject *capsule;
5303 char hexBuf[sizeof(void *) * 2 + 3];
5304
5305 sprintf(hexBuf, "%p", obj);
5306
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005307# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005308 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005309# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005310 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005311# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005312 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005313 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005314# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005315 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005316# else
5317 capsule = PyCObject_FromVoidPtr(tv, NULL);
5318# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005319 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5320 {
5321 Py_DECREF(capsule);
5322 tv->v_type = VAR_UNKNOWN;
5323 return -1;
5324 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005325
5326 Py_DECREF(capsule);
5327
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005328 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005329 {
5330 tv->v_type = VAR_UNKNOWN;
5331 return -1;
5332 }
5333 /* As we are not using copy_tv which increments reference count we must
5334 * do it ourself. */
5335 switch(tv->v_type)
5336 {
5337 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5338 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5339 }
5340 }
5341 else
5342 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005343 typval_T *v;
5344
5345# ifdef PY_USE_CAPSULE
5346 v = PyCapsule_GetPointer(capsule, NULL);
5347# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005348 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005349# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005350 copy_tv(v, tv);
5351 }
5352 return 0;
5353}
5354
5355 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005356ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5357{
5358 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005359 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005360
5361 if (!(lookup_dict = PyDict_New()))
5362 return -1;
5363
5364 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5365 {
5366 tv->v_type = VAR_DICT;
5367 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5368 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005369 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005370 }
5371 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005372 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005373 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005374 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005375 else
5376 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005377 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005378 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005379 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005380 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005381 }
5382 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005383 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005384}
5385
5386 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005387ConvertFromPyObject(PyObject *obj, typval_T *tv)
5388{
5389 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005390 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005391
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005392 if (!(lookup_dict = PyDict_New()))
5393 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005394 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005395 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005396 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005397}
5398
5399 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005400_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005401{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005402 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005403 {
5404 tv->v_type = VAR_DICT;
5405 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5406 ++tv->vval.v_dict->dv_refcount;
5407 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005408 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005409 {
5410 tv->v_type = VAR_LIST;
5411 tv->vval.v_list = (((ListObject *)(obj))->list);
5412 ++tv->vval.v_list->lv_refcount;
5413 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005414 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005415 {
5416 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5417 return -1;
5418
5419 tv->v_type = VAR_FUNC;
5420 func_ref(tv->vval.v_string);
5421 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005422 else if (PyBytes_Check(obj))
5423 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005424 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005425
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005426 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005427 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005428 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005429 return -1;
5430
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005431 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005432 return -1;
5433
5434 tv->v_type = VAR_STRING;
5435 }
5436 else if (PyUnicode_Check(obj))
5437 {
5438 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005439 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005440
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005441 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005442 if (bytes == NULL)
5443 return -1;
5444
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005445 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005446 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005447 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005448 return -1;
5449
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005450 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005451 {
5452 Py_XDECREF(bytes);
5453 return -1;
5454 }
5455 Py_XDECREF(bytes);
5456
5457 tv->v_type = VAR_STRING;
5458 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005459#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005460 else if (PyInt_Check(obj))
5461 {
5462 tv->v_type = VAR_NUMBER;
5463 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005464 if (PyErr_Occurred())
5465 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005466 }
5467#endif
5468 else if (PyLong_Check(obj))
5469 {
5470 tv->v_type = VAR_NUMBER;
5471 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005472 if (PyErr_Occurred())
5473 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005474 }
5475 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005476 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005477#ifdef FEAT_FLOAT
5478 else if (PyFloat_Check(obj))
5479 {
5480 tv->v_type = VAR_FLOAT;
5481 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5482 }
5483#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005484 else if (PyObject_HasAttrString(obj, "keys"))
5485 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005486 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005487 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005488 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005489 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005490 else if (PyNumber_Check(obj))
5491 {
5492 PyObject *num;
5493
5494 if (!(num = PyNumber_Long(obj)))
5495 return -1;
5496
5497 tv->v_type = VAR_NUMBER;
5498 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5499
5500 Py_DECREF(num);
5501 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005502 else
5503 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005504 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005505 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005506 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005507 return -1;
5508 }
5509 return 0;
5510}
5511
5512 static PyObject *
5513ConvertToPyObject(typval_T *tv)
5514{
5515 if (tv == NULL)
5516 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005517 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005518 return NULL;
5519 }
5520 switch (tv->v_type)
5521 {
5522 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005523 return PyBytes_FromString(tv->vval.v_string == NULL
5524 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005525 case VAR_NUMBER:
5526 return PyLong_FromLong((long) tv->vval.v_number);
5527#ifdef FEAT_FLOAT
5528 case VAR_FLOAT:
5529 return PyFloat_FromDouble((double) tv->vval.v_float);
5530#endif
5531 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005532 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005533 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005534 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005535 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005536 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005537 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005538 case VAR_UNKNOWN:
5539 Py_INCREF(Py_None);
5540 return Py_None;
5541 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005542 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005543 return NULL;
5544 }
5545}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005546
5547typedef struct
5548{
5549 PyObject_HEAD
5550} CurrentObject;
5551static PyTypeObject CurrentType;
5552
5553 static void
5554init_structs(void)
5555{
5556 vim_memset(&OutputType, 0, sizeof(OutputType));
5557 OutputType.tp_name = "vim.message";
5558 OutputType.tp_basicsize = sizeof(OutputObject);
5559 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5560 OutputType.tp_doc = "vim message object";
5561 OutputType.tp_methods = OutputMethods;
5562#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005563 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5564 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005565 OutputType.tp_alloc = call_PyType_GenericAlloc;
5566 OutputType.tp_new = call_PyType_GenericNew;
5567 OutputType.tp_free = call_PyObject_Free;
5568#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005569 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5570 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005571#endif
5572
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005573 vim_memset(&IterType, 0, sizeof(IterType));
5574 IterType.tp_name = "vim.iter";
5575 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005576 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005577 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005578 IterType.tp_iter = (getiterfunc)IterIter;
5579 IterType.tp_iternext = (iternextfunc)IterNext;
5580 IterType.tp_dealloc = (destructor)IterDestructor;
5581 IterType.tp_traverse = (traverseproc)IterTraverse;
5582 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005583
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005584 vim_memset(&BufferType, 0, sizeof(BufferType));
5585 BufferType.tp_name = "vim.buffer";
5586 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005587 BufferType.tp_dealloc = (destructor)BufferDestructor;
5588 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005589 BufferType.tp_as_sequence = &BufferAsSeq;
5590 BufferType.tp_as_mapping = &BufferAsMapping;
5591 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5592 BufferType.tp_doc = "vim buffer object";
5593 BufferType.tp_methods = BufferMethods;
5594#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005595 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005596 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005597 BufferType.tp_alloc = call_PyType_GenericAlloc;
5598 BufferType.tp_new = call_PyType_GenericNew;
5599 BufferType.tp_free = call_PyObject_Free;
5600#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005601 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005602 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005603#endif
5604
5605 vim_memset(&WindowType, 0, sizeof(WindowType));
5606 WindowType.tp_name = "vim.window";
5607 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005608 WindowType.tp_dealloc = (destructor)WindowDestructor;
5609 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005610 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005611 WindowType.tp_doc = "vim Window object";
5612 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005613 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5614 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005615#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005616 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5617 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005618 WindowType.tp_alloc = call_PyType_GenericAlloc;
5619 WindowType.tp_new = call_PyType_GenericNew;
5620 WindowType.tp_free = call_PyObject_Free;
5621#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005622 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5623 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005624#endif
5625
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005626 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5627 TabPageType.tp_name = "vim.tabpage";
5628 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005629 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5630 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005631 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5632 TabPageType.tp_doc = "vim tab page object";
5633 TabPageType.tp_methods = TabPageMethods;
5634#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005635 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005636 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5637 TabPageType.tp_new = call_PyType_GenericNew;
5638 TabPageType.tp_free = call_PyObject_Free;
5639#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005640 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005641#endif
5642
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005643 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5644 BufMapType.tp_name = "vim.bufferlist";
5645 BufMapType.tp_basicsize = sizeof(BufMapObject);
5646 BufMapType.tp_as_mapping = &BufMapAsMapping;
5647 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005648 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005649 BufferType.tp_doc = "vim buffer list";
5650
5651 vim_memset(&WinListType, 0, sizeof(WinListType));
5652 WinListType.tp_name = "vim.windowlist";
5653 WinListType.tp_basicsize = sizeof(WinListType);
5654 WinListType.tp_as_sequence = &WinListAsSeq;
5655 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5656 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005657 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005658
5659 vim_memset(&TabListType, 0, sizeof(TabListType));
5660 TabListType.tp_name = "vim.tabpagelist";
5661 TabListType.tp_basicsize = sizeof(TabListType);
5662 TabListType.tp_as_sequence = &TabListAsSeq;
5663 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5664 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005665
5666 vim_memset(&RangeType, 0, sizeof(RangeType));
5667 RangeType.tp_name = "vim.range";
5668 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005669 RangeType.tp_dealloc = (destructor)RangeDestructor;
5670 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005671 RangeType.tp_as_sequence = &RangeAsSeq;
5672 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005673 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005674 RangeType.tp_doc = "vim Range object";
5675 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005676 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5677 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005678#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005679 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005680 RangeType.tp_alloc = call_PyType_GenericAlloc;
5681 RangeType.tp_new = call_PyType_GenericNew;
5682 RangeType.tp_free = call_PyObject_Free;
5683#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005684 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005685#endif
5686
5687 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5688 CurrentType.tp_name = "vim.currentdata";
5689 CurrentType.tp_basicsize = sizeof(CurrentObject);
5690 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5691 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005692 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005693#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005694 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5695 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005696#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005697 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5698 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005699#endif
5700
5701 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5702 DictionaryType.tp_name = "vim.dictionary";
5703 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005704 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005705 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005706 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005707 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005708 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5709 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005710 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5711 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5712 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005713#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005714 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5715 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005716#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005717 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5718 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005719#endif
5720
5721 vim_memset(&ListType, 0, sizeof(ListType));
5722 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005723 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005724 ListType.tp_basicsize = sizeof(ListObject);
5725 ListType.tp_as_sequence = &ListAsSeq;
5726 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005727 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005728 ListType.tp_doc = "list pushing modifications to vim structure";
5729 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005730 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005731 ListType.tp_new = (newfunc)ListConstructor;
5732 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005733#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005734 ListType.tp_getattro = (getattrofunc)ListGetattro;
5735 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005736#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005737 ListType.tp_getattr = (getattrfunc)ListGetattr;
5738 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005739#endif
5740
5741 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005742 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005743 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005744 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5745 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005746 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005747 FunctionType.tp_doc = "object that calls vim function";
5748 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005749 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005750 FunctionType.tp_new = (newfunc)FunctionConstructor;
5751 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005752#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005753 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005754#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005755 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005756#endif
5757
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005758 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5759 OptionsType.tp_name = "vim.options";
5760 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005761 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005762 OptionsType.tp_doc = "object for manipulating options";
5763 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005764 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5765 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5766 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005767
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005768 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5769 LoaderType.tp_name = "vim.Loader";
5770 LoaderType.tp_basicsize = sizeof(LoaderObject);
5771 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5772 LoaderType.tp_doc = "vim message object";
5773 LoaderType.tp_methods = LoaderMethods;
5774 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5775
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005776#if PY_MAJOR_VERSION >= 3
5777 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5778 vimmodule.m_name = "vim";
5779 vimmodule.m_doc = "Vim Python interface\n";
5780 vimmodule.m_size = -1;
5781 vimmodule.m_methods = VimMethods;
5782#endif
5783}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005784
5785#define PYTYPE_READY(type) \
5786 if (PyType_Ready(&type)) \
5787 return -1;
5788
5789 static int
5790init_types()
5791{
5792 PYTYPE_READY(IterType);
5793 PYTYPE_READY(BufferType);
5794 PYTYPE_READY(RangeType);
5795 PYTYPE_READY(WindowType);
5796 PYTYPE_READY(TabPageType);
5797 PYTYPE_READY(BufMapType);
5798 PYTYPE_READY(WinListType);
5799 PYTYPE_READY(TabListType);
5800 PYTYPE_READY(CurrentType);
5801 PYTYPE_READY(DictionaryType);
5802 PYTYPE_READY(ListType);
5803 PYTYPE_READY(FunctionType);
5804 PYTYPE_READY(OptionsType);
5805 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005806 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005807 return 0;
5808}
5809
5810 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005811init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005812{
5813 PyObject *path;
5814 PyObject *path_hook;
5815 PyObject *path_hooks;
5816
5817 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5818 return -1;
5819
5820 if (!(path_hooks = PySys_GetObject("path_hooks")))
5821 {
5822 PyErr_Clear();
5823 path_hooks = PyList_New(1);
5824 PyList_SET_ITEM(path_hooks, 0, path_hook);
5825 if (PySys_SetObject("path_hooks", path_hooks))
5826 {
5827 Py_DECREF(path_hooks);
5828 return -1;
5829 }
5830 Py_DECREF(path_hooks);
5831 }
5832 else if (PyList_Check(path_hooks))
5833 {
5834 if (PyList_Append(path_hooks, path_hook))
5835 {
5836 Py_DECREF(path_hook);
5837 return -1;
5838 }
5839 Py_DECREF(path_hook);
5840 }
5841 else
5842 {
5843 VimTryStart();
5844 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5845 "You should now do the following:\n"
5846 "- append vim.path_hook to sys.path_hooks\n"
5847 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5848 VimTryEnd(); /* Discard the error */
5849 Py_DECREF(path_hook);
5850 return 0;
5851 }
5852
5853 if (!(path = PySys_GetObject("path")))
5854 {
5855 PyErr_Clear();
5856 path = PyList_New(1);
5857 Py_INCREF(vim_special_path_object);
5858 PyList_SET_ITEM(path, 0, vim_special_path_object);
5859 if (PySys_SetObject("path", path))
5860 {
5861 Py_DECREF(path);
5862 return -1;
5863 }
5864 Py_DECREF(path);
5865 }
5866 else if (PyList_Check(path))
5867 {
5868 if (PyList_Append(path, vim_special_path_object))
5869 return -1;
5870 }
5871 else
5872 {
5873 VimTryStart();
5874 EMSG(_("Failed to set path: sys.path is not a list\n"
5875 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5876 VimTryEnd(); /* Discard the error */
5877 }
5878
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005879 return 0;
5880}
5881
5882static BufMapObject TheBufferMap =
5883{
5884 PyObject_HEAD_INIT(&BufMapType)
5885};
5886
5887static WinListObject TheWindowList =
5888{
5889 PyObject_HEAD_INIT(&WinListType)
5890 NULL
5891};
5892
5893static CurrentObject TheCurrent =
5894{
5895 PyObject_HEAD_INIT(&CurrentType)
5896};
5897
5898static TabListObject TheTabPageList =
5899{
5900 PyObject_HEAD_INIT(&TabListType)
5901};
5902
5903static struct numeric_constant {
5904 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005905 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005906} numeric_constants[] = {
5907 {"VAR_LOCKED", VAR_LOCKED},
5908 {"VAR_FIXED", VAR_FIXED},
5909 {"VAR_SCOPE", VAR_SCOPE},
5910 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5911};
5912
5913static struct object_constant {
5914 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005915 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005916} object_constants[] = {
5917 {"buffers", (PyObject *)(void *)&TheBufferMap},
5918 {"windows", (PyObject *)(void *)&TheWindowList},
5919 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5920 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005921
5922 {"Buffer", (PyObject *)&BufferType},
5923 {"Range", (PyObject *)&RangeType},
5924 {"Window", (PyObject *)&WindowType},
5925 {"TabPage", (PyObject *)&TabPageType},
5926 {"Dictionary", (PyObject *)&DictionaryType},
5927 {"List", (PyObject *)&ListType},
5928 {"Function", (PyObject *)&FunctionType},
5929 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005930 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005931};
5932
5933typedef int (*object_adder)(PyObject *, const char *, PyObject *);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005934typedef PyObject *(*attr_getter)(PyObject *, const char *);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005935
5936#define ADD_OBJECT(m, name, obj) \
5937 if (add_object(m, name, obj)) \
5938 return -1;
5939
5940#define ADD_CHECKED_OBJECT(m, name, obj) \
5941 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005942 PyObject *valObject = obj; \
5943 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005944 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005945 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005946 }
5947
5948 static int
Bram Moolenaarf4258302013-06-02 18:20:17 +02005949populate_module(PyObject *m, object_adder add_object, attr_getter get_attr)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005950{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005951 int i;
5952 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005953 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005954 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005955
5956 for (i = 0; i < (int)(sizeof(numeric_constants)
5957 / sizeof(struct numeric_constant));
5958 ++i)
5959 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005960 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005961
5962 for (i = 0; i < (int)(sizeof(object_constants)
5963 / sizeof(struct object_constant));
5964 ++i)
5965 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005966 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005967
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005968 valObject = object_constants[i].valObject;
5969 Py_INCREF(valObject);
5970 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005971 }
5972
5973 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5974 return -1;
5975 ADD_OBJECT(m, "error", VimError);
5976
Bram Moolenaara9922d62013-05-30 13:01:18 +02005977 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5978 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005979 ADD_CHECKED_OBJECT(m, "options",
5980 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005981
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005982 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005983 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005984 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005985
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005986 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005987 return -1;
5988 ADD_OBJECT(m, "_getcwd", py_getcwd)
5989
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005990 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005991 return -1;
5992 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005993 if (!(attr = get_attr(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005994 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005995 if (PyObject_SetAttrString(other_module, "chdir", attr))
5996 {
5997 Py_DECREF(attr);
5998 return -1;
5999 }
6000 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006001
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006002 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006003 {
6004 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006005 if (!(attr = get_attr(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006006 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006007 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6008 {
6009 Py_DECREF(attr);
6010 return -1;
6011 }
6012 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006013 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006014 else
6015 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006016
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006017 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6018 return -1;
6019
6020 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6021
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006022 if (!(imp = PyImport_ImportModule("imp")))
6023 return -1;
6024
6025 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6026 {
6027 Py_DECREF(imp);
6028 return -1;
6029 }
6030
6031 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6032 {
6033 Py_DECREF(py_find_module);
6034 Py_DECREF(imp);
6035 return -1;
6036 }
6037
6038 Py_DECREF(imp);
6039
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006040 ADD_OBJECT(m, "_find_module", py_find_module);
6041 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006042
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006043 return 0;
6044}