blob: 8f60550f934c170b3a96ea3a8ff01bcc3fdd801f [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
Bram Moolenaardee2e312013-06-23 16:35:47 +02001614 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001615
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 Moolenaardee2e312013-06-23 16:35:47 +02001913 (int) 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 Moolenaardee2e312013-06-23 16:35:47 +02002438
2439 if (PyErr_Occurred())
2440 return -1;
2441
Bram Moolenaardb913952012-06-29 12:54:53 +02002442 return 0;
2443}
2444
2445 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002446ListConcatInPlace(ListObject *self, PyObject *obj)
Bram Moolenaardb913952012-06-29 12:54:53 +02002447{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002448 list_T *l = self->list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002449 PyObject *lookup_dict;
2450
2451 if (l->lv_lock)
2452 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002453 RAISE_LOCKED_LIST;
Bram Moolenaardb913952012-06-29 12:54:53 +02002454 return NULL;
2455 }
2456
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02002457 if (!(lookup_dict = PyDict_New()))
2458 return NULL;
2459
Bram Moolenaardb913952012-06-29 12:54:53 +02002460 if (list_py_concat(l, obj, lookup_dict) == -1)
2461 {
2462 Py_DECREF(lookup_dict);
2463 return NULL;
2464 }
2465 Py_DECREF(lookup_dict);
2466
2467 Py_INCREF(self);
Bram Moolenaard6e39182013-05-21 18:30:34 +02002468 return (PyObject *)(self);
Bram Moolenaardb913952012-06-29 12:54:53 +02002469}
2470
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002471static char *ListAttrs[] = {
2472 "locked",
2473 NULL
2474};
2475
2476 static PyObject *
2477ListDir(PyObject *self)
2478{
2479 return ObjectDir(self, ListAttrs);
2480}
2481
Bram Moolenaar66b79852012-09-21 14:00:35 +02002482 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002483ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002484{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002485 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002486 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002487 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002488 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002489 return -1;
2490 }
2491
2492 if (strcmp(name, "locked") == 0)
2493 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002494 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002495 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002496 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002497 return -1;
2498 }
2499 else
2500 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002501 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002502 if (istrue == -1)
2503 return -1;
2504 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002505 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002506 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002507 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002508 }
2509 return 0;
2510 }
2511 else
2512 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002513 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002514 return -1;
2515 }
2516}
2517
Bram Moolenaardb913952012-06-29 12:54:53 +02002518static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002519 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2520 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2521 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002522};
2523
2524typedef struct
2525{
2526 PyObject_HEAD
2527 char_u *name;
2528} FunctionObject;
2529
2530static PyTypeObject FunctionType;
2531
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002532#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2533
Bram Moolenaardb913952012-06-29 12:54:53 +02002534 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002535FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002536{
2537 FunctionObject *self;
2538
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002539 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2540
Bram Moolenaardb913952012-06-29 12:54:53 +02002541 if (self == NULL)
2542 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002543
2544 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002545 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002546 if (!translated_function_exists(name))
2547 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002548 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002549 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002550 return NULL;
2551 }
2552 self->name = vim_strsave(name);
2553 func_ref(self->name);
2554 }
2555 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002556 if ((self->name = get_expanded_name(name,
2557 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2558 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002559 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002560 PyErr_FORMAT(PyExc_ValueError,
2561 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002562 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002563 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002564
2565 return (PyObject *)(self);
2566}
2567
2568 static PyObject *
2569FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2570{
2571 PyObject *self;
2572 char_u *name;
2573
2574 if (kwargs)
2575 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002576 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002577 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002578 return NULL;
2579 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002580
Bram Moolenaar389a1792013-06-23 13:00:44 +02002581 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002582 return NULL;
2583
2584 self = FunctionNew(subtype, name);
2585
Bram Moolenaar389a1792013-06-23 13:00:44 +02002586 PyMem_Free(name);
2587
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002588 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002589}
2590
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002591 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002592FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002593{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002594 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002595 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002596
2597 DESTRUCTOR_FINISH(self);
2598}
2599
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002600static char *FunctionAttrs[] = {
2601 "softspace",
2602 NULL
2603};
2604
2605 static PyObject *
2606FunctionDir(PyObject *self)
2607{
2608 return ObjectDir(self, FunctionAttrs);
2609}
2610
Bram Moolenaardb913952012-06-29 12:54:53 +02002611 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002612FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002613{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002614 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002615 typval_T args;
2616 typval_T selfdicttv;
2617 typval_T rettv;
2618 dict_T *selfdict = NULL;
2619 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002620 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002621 int error;
2622
2623 if (ConvertFromPyObject(argsObject, &args) == -1)
2624 return NULL;
2625
2626 if (kwargs != NULL)
2627 {
2628 selfdictObject = PyDict_GetItemString(kwargs, "self");
2629 if (selfdictObject != NULL)
2630 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002631 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002632 {
2633 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002634 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002635 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002636 selfdict = selfdicttv.vval.v_dict;
2637 }
2638 }
2639
Bram Moolenaar71700b82013-05-15 17:49:05 +02002640 Py_BEGIN_ALLOW_THREADS
2641 Python_Lock_Vim();
2642
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002643 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002644 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002645
2646 Python_Release_Vim();
2647 Py_END_ALLOW_THREADS
2648
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002649 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002650 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002651 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002652 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002653 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002654 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002655 }
2656 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002657 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002658
Bram Moolenaardb913952012-06-29 12:54:53 +02002659 clear_tv(&args);
2660 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002661 if (selfdict != NULL)
2662 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002663
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002664 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002665}
2666
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002667 static PyObject *
2668FunctionRepr(FunctionObject *self)
2669{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002670#ifdef Py_TRACE_REFS
2671 /* For unknown reason self->name may be NULL after calling
2672 * Finalize */
2673 return PyString_FromFormat("<vim.Function '%s'>",
2674 (self->name == NULL ? "<NULL>" : (char *) self->name));
2675#else
2676 return PyString_FromFormat("<vim.Function '%s'>", (char *) self->name);
2677#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002678}
2679
Bram Moolenaardb913952012-06-29 12:54:53 +02002680static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002681 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2682 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002683};
2684
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002685/*
2686 * Options object
2687 */
2688
2689static PyTypeObject OptionsType;
2690
2691typedef int (*checkfun)(void *);
2692
2693typedef struct
2694{
2695 PyObject_HEAD
2696 int opt_type;
2697 void *from;
2698 checkfun Check;
2699 PyObject *fromObj;
2700} OptionsObject;
2701
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002702 static int
2703dummy_check(void *arg UNUSED)
2704{
2705 return 0;
2706}
2707
2708 static PyObject *
2709OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2710{
2711 OptionsObject *self;
2712
Bram Moolenaar774267b2013-05-21 20:51:59 +02002713 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002714 if (self == NULL)
2715 return NULL;
2716
2717 self->opt_type = opt_type;
2718 self->from = from;
2719 self->Check = Check;
2720 self->fromObj = fromObj;
2721 if (fromObj)
2722 Py_INCREF(fromObj);
2723
2724 return (PyObject *)(self);
2725}
2726
2727 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002728OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002729{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002730 PyObject_GC_UnTrack((void *)(self));
2731 Py_XDECREF(self->fromObj);
2732 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002733}
2734
2735 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002736OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002737{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002738 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002739 return 0;
2740}
2741
2742 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002743OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002744{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002745 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002746 return 0;
2747}
2748
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002749 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002750OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002751{
2752 char_u *key;
2753 int flags;
2754 long numval;
2755 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002756 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002757
Bram Moolenaard6e39182013-05-21 18:30:34 +02002758 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002759 return NULL;
2760
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002761 if (!(key = StringToChars(keyObject, &todecref)))
2762 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002763
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002764 if (*key == NUL)
2765 {
2766 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002767 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002768 return NULL;
2769 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002770
2771 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002772 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002773
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002774 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002775
2776 if (flags == 0)
2777 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002778 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002779 return NULL;
2780 }
2781
2782 if (flags & SOPT_UNSET)
2783 {
2784 Py_INCREF(Py_None);
2785 return Py_None;
2786 }
2787 else if (flags & SOPT_BOOL)
2788 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002789 PyObject *ret;
2790 ret = numval ? Py_True : Py_False;
2791 Py_INCREF(ret);
2792 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002793 }
2794 else if (flags & SOPT_NUM)
2795 return PyInt_FromLong(numval);
2796 else if (flags & SOPT_STRING)
2797 {
2798 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002799 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002800 PyObject *ret = PyBytes_FromString((char *) stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002801 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002802 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002803 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002804 else
2805 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002806 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002807 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002808 return NULL;
2809 }
2810 }
2811 else
2812 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002813 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002814 return NULL;
2815 }
2816}
2817
2818 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002819set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002820{
2821 char_u *errmsg;
2822
2823 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
2824 {
2825 if (VimTryEnd())
2826 return FAIL;
2827 PyErr_SetVim((char *)errmsg);
2828 return FAIL;
2829 }
2830 return OK;
2831}
2832
2833 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02002834set_option_value_for(
2835 char_u *key,
2836 int numval,
2837 char_u *stringval,
2838 int opt_flags,
2839 int opt_type,
2840 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002841{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002842 win_T *save_curwin = NULL;
2843 tabpage_T *save_curtab = NULL;
2844 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002845 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002846
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002847 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002848 switch (opt_type)
2849 {
2850 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002851 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02002852 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002853 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002854 if (VimTryEnd())
2855 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002856 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002857 return -1;
2858 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002859 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
2860 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002861 break;
2862 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02002863 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002864 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02002865 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002866 break;
2867 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002868 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002869 break;
2870 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002871 if (set_ret == FAIL)
2872 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002873 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002874}
2875
2876 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002877OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002878{
2879 char_u *key;
2880 int flags;
2881 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002882 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002883 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002884
Bram Moolenaard6e39182013-05-21 18:30:34 +02002885 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002886 return -1;
2887
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002888 if (!(key = StringToChars(keyObject, &todecref)))
2889 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002890
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002891 if (*key == NUL)
2892 {
2893 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02002894 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002895 return -1;
2896 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002897
2898 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002899 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002900
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002901 if (flags == 0)
2902 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02002903 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002904 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002905 return -1;
2906 }
2907
2908 if (valObject == NULL)
2909 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002910 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002911 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002912 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002913 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002914 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002915 return -1;
2916 }
2917 else if (!(flags & SOPT_GLOBAL))
2918 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002919 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002920 N_("unable to unset option %s "
2921 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002922 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002923 return -1;
2924 }
2925 else
2926 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002927 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002928 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002929 return 0;
2930 }
2931 }
2932
Bram Moolenaard6e39182013-05-21 18:30:34 +02002933 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002934
2935 if (flags & SOPT_BOOL)
2936 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02002937 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002938
Bram Moolenaarb983f752013-05-15 16:11:50 +02002939 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002940 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002941 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002942 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002943 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002944 }
2945 else if (flags & SOPT_NUM)
2946 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002947 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002948
Bram Moolenaar141be8a2013-06-23 14:16:57 +02002949 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002950 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002951 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002952 return -1;
2953 }
2954
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002955 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02002956 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002957 }
2958 else
2959 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002960 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002961 PyObject *todecref;
2962
2963 if ((val = StringToChars(valObject, &todecref)))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002964 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02002965 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002966 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002967 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002968 }
2969
Bram Moolenaar35eacd72013-05-30 22:06:33 +02002970 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02002971
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002972 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002973}
2974
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002975static PyMappingMethods OptionsAsMapping = {
2976 (lenfunc) NULL,
2977 (binaryfunc) OptionsItem,
2978 (objobjargproc) OptionsAssItem,
2979};
2980
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002981/* Tabpage object
2982 */
2983
2984typedef struct
2985{
2986 PyObject_HEAD
2987 tabpage_T *tab;
2988} TabPageObject;
2989
2990static PyObject *WinListNew(TabPageObject *tabObject);
2991
2992static PyTypeObject TabPageType;
2993
2994 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002995CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002996{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002997 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02002998 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002999 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003000 return -1;
3001 }
3002
3003 return 0;
3004}
3005
3006 static PyObject *
3007TabPageNew(tabpage_T *tab)
3008{
3009 TabPageObject *self;
3010
3011 if (TAB_PYTHON_REF(tab))
3012 {
3013 self = TAB_PYTHON_REF(tab);
3014 Py_INCREF(self);
3015 }
3016 else
3017 {
3018 self = PyObject_NEW(TabPageObject, &TabPageType);
3019 if (self == NULL)
3020 return NULL;
3021 self->tab = tab;
3022 TAB_PYTHON_REF(tab) = self;
3023 }
3024
3025 return (PyObject *)(self);
3026}
3027
3028 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003029TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003030{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003031 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3032 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003033
3034 DESTRUCTOR_FINISH(self);
3035}
3036
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003037static char *TabPageAttrs[] = {
3038 "windows", "number", "vars", "window", "valid",
3039 NULL
3040};
3041
3042 static PyObject *
3043TabPageDir(PyObject *self)
3044{
3045 return ObjectDir(self, TabPageAttrs);
3046}
3047
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003048 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003049TabPageAttrValid(TabPageObject *self, char *name)
3050{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003051 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003052
3053 if (strcmp(name, "valid") != 0)
3054 return NULL;
3055
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003056 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3057 Py_INCREF(ret);
3058 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003059}
3060
3061 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003062TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003063{
3064 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003065 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003066 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003067 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003068 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003069 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003070 else if (strcmp(name, "window") == 0)
3071 {
3072 /* For current tab window.c does not bother to set or update tp_curwin
3073 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003074 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003075 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003076 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003077 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003078 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003079 else if (strcmp(name, "__members__") == 0)
3080 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003081 return NULL;
3082}
3083
3084 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003085TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003086{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003087 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003088 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003089 else
3090 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003091 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003092
3093 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003094 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3095 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003096 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003097 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003098 }
3099}
3100
3101static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003102 /* name, function, calling, documentation */
3103 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3104 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003105};
3106
3107/*
3108 * Window list object
3109 */
3110
3111static PyTypeObject TabListType;
3112static PySequenceMethods TabListAsSeq;
3113
3114typedef struct
3115{
3116 PyObject_HEAD
3117} TabListObject;
3118
3119 static PyInt
3120TabListLength(PyObject *self UNUSED)
3121{
3122 tabpage_T *tp = first_tabpage;
3123 PyInt n = 0;
3124
3125 while (tp != NULL)
3126 {
3127 ++n;
3128 tp = tp->tp_next;
3129 }
3130
3131 return n;
3132}
3133
3134 static PyObject *
3135TabListItem(PyObject *self UNUSED, PyInt n)
3136{
3137 tabpage_T *tp;
3138
3139 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3140 if (n == 0)
3141 return TabPageNew(tp);
3142
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003143 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003144 return NULL;
3145}
3146
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003147/*
3148 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003149 */
3150
3151typedef struct
3152{
3153 PyObject_HEAD
3154 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003155 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003156} WindowObject;
3157
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003158static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003159
3160 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003161CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003162{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003163 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003164 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003165 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003166 return -1;
3167 }
3168
3169 return 0;
3170}
3171
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003172 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003173WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003174{
3175 /* We need to handle deletion of windows underneath us.
3176 * If we add a "w_python*_ref" field to the win_T structure,
3177 * then we can get at it in win_free() in vim. We then
3178 * need to create only ONE Python object per window - if
3179 * we try to create a second, just INCREF the existing one
3180 * and return it. The (single) Python object referring to
3181 * the window is stored in "w_python*_ref".
3182 * On a win_free() we set the Python object's win_T* field
3183 * to an invalid value. We trap all uses of a window
3184 * object, and reject them if the win_T* field is invalid.
3185 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003186 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003187 * w_python_ref and w_python3_ref fields respectively.
3188 */
3189
3190 WindowObject *self;
3191
3192 if (WIN_PYTHON_REF(win))
3193 {
3194 self = WIN_PYTHON_REF(win);
3195 Py_INCREF(self);
3196 }
3197 else
3198 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003199 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003200 if (self == NULL)
3201 return NULL;
3202 self->win = win;
3203 WIN_PYTHON_REF(win) = self;
3204 }
3205
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003206 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3207
Bram Moolenaar971db462013-05-12 18:44:48 +02003208 return (PyObject *)(self);
3209}
3210
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003211 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003212WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003213{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003214 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003215 if (self->win && self->win != INVALID_WINDOW_VALUE)
3216 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003217 Py_XDECREF(((PyObject *)(self->tabObject)));
3218 PyObject_GC_Del((void *)(self));
3219}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003220
Bram Moolenaar774267b2013-05-21 20:51:59 +02003221 static int
3222WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3223{
3224 Py_VISIT(((PyObject *)(self->tabObject)));
3225 return 0;
3226}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003227
Bram Moolenaar774267b2013-05-21 20:51:59 +02003228 static int
3229WindowClear(WindowObject *self)
3230{
3231 Py_CLEAR(self->tabObject);
3232 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003233}
3234
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003235 static win_T *
3236get_firstwin(TabPageObject *tabObject)
3237{
3238 if (tabObject)
3239 {
3240 if (CheckTabPage(tabObject))
3241 return NULL;
3242 /* For current tab window.c does not bother to set or update tp_firstwin
3243 */
3244 else if (tabObject->tab == curtab)
3245 return firstwin;
3246 else
3247 return tabObject->tab->tp_firstwin;
3248 }
3249 else
3250 return firstwin;
3251}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003252static char *WindowAttrs[] = {
3253 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3254 "tabpage", "valid",
3255 NULL
3256};
3257
3258 static PyObject *
3259WindowDir(PyObject *self)
3260{
3261 return ObjectDir(self, WindowAttrs);
3262}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003263
Bram Moolenaar971db462013-05-12 18:44:48 +02003264 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003265WindowAttrValid(WindowObject *self, char *name)
3266{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003267 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003268
3269 if (strcmp(name, "valid") != 0)
3270 return NULL;
3271
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003272 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3273 Py_INCREF(ret);
3274 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003275}
3276
3277 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003278WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003279{
3280 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003281 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003282 else if (strcmp(name, "cursor") == 0)
3283 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003284 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003285
3286 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3287 }
3288 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003289 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003290#ifdef FEAT_WINDOWS
3291 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003292 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003293#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003294#ifdef FEAT_VERTSPLIT
3295 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003296 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003297 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003298 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003299#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003300 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003301 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003302 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003303 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3304 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003305 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003306 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003307 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003308 return NULL;
3309 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003310 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003311 }
3312 else if (strcmp(name, "tabpage") == 0)
3313 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003314 Py_INCREF(self->tabObject);
3315 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003316 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003317 else if (strcmp(name, "__members__") == 0)
3318 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003319 else
3320 return NULL;
3321}
3322
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003323 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003324WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003325{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003326 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003327 return -1;
3328
3329 if (strcmp(name, "buffer") == 0)
3330 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003331 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003332 return -1;
3333 }
3334 else if (strcmp(name, "cursor") == 0)
3335 {
3336 long lnum;
3337 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003338
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003339 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003340 return -1;
3341
Bram Moolenaard6e39182013-05-21 18:30:34 +02003342 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003343 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003344 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003345 return -1;
3346 }
3347
3348 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003349 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003350 return -1;
3351
Bram Moolenaard6e39182013-05-21 18:30:34 +02003352 self->win->w_cursor.lnum = lnum;
3353 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003354#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003355 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003356#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003357 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003358 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003359
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003360 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003361 return 0;
3362 }
3363 else if (strcmp(name, "height") == 0)
3364 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003365 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003366 win_T *savewin;
3367
Bram Moolenaardee2e312013-06-23 16:35:47 +02003368 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003369 return -1;
3370
3371#ifdef FEAT_GUI
3372 need_mouse_correct = TRUE;
3373#endif
3374 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003375 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003376
3377 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003378 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003379 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003380 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003381 return -1;
3382
3383 return 0;
3384 }
3385#ifdef FEAT_VERTSPLIT
3386 else if (strcmp(name, "width") == 0)
3387 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003388 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003389 win_T *savewin;
3390
Bram Moolenaardee2e312013-06-23 16:35:47 +02003391 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003392 return -1;
3393
3394#ifdef FEAT_GUI
3395 need_mouse_correct = TRUE;
3396#endif
3397 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003398 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003399
3400 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003401 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003402 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003403 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003404 return -1;
3405
3406 return 0;
3407 }
3408#endif
3409 else
3410 {
3411 PyErr_SetString(PyExc_AttributeError, name);
3412 return -1;
3413 }
3414}
3415
3416 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003417WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003418{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003419 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003420 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003421 else
3422 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003423 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003424
Bram Moolenaar6d216452013-05-12 19:00:41 +02003425 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003426 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003427 (self));
3428 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003429 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003430 }
3431}
3432
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003433static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003434 /* name, function, calling, documentation */
3435 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3436 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003437};
3438
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003439/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003440 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003441 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003442
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003443static PyTypeObject WinListType;
3444static PySequenceMethods WinListAsSeq;
3445
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003446typedef struct
3447{
3448 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003449 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003450} WinListObject;
3451
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003452 static PyObject *
3453WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003454{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003455 WinListObject *self;
3456
3457 self = PyObject_NEW(WinListObject, &WinListType);
3458 self->tabObject = tabObject;
3459 Py_INCREF(tabObject);
3460
3461 return (PyObject *)(self);
3462}
3463
3464 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003465WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003466{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003467 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003468
3469 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003470 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003471 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003472 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003473
3474 DESTRUCTOR_FINISH(self);
3475}
3476
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003477 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003478WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003479{
3480 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003481 PyInt n = 0;
3482
Bram Moolenaard6e39182013-05-21 18:30:34 +02003483 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003484 return -1;
3485
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003486 while (w != NULL)
3487 {
3488 ++n;
3489 w = W_NEXT(w);
3490 }
3491
3492 return n;
3493}
3494
3495 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003496WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003497{
3498 win_T *w;
3499
Bram Moolenaard6e39182013-05-21 18:30:34 +02003500 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003501 return NULL;
3502
3503 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003504 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003505 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003506
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003507 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003508 return NULL;
3509}
3510
3511/* Convert a Python string into a Vim line.
3512 *
3513 * The result is in allocated memory. All internal nulls are replaced by
3514 * newline characters. It is an error for the string to contain newline
3515 * characters.
3516 *
3517 * On errors, the Python exception data is set, and NULL is returned.
3518 */
3519 static char *
3520StringToLine(PyObject *obj)
3521{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003522 char *str;
3523 char *save;
3524 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003525 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003526 PyInt i;
3527 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003528
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003529 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003530 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003531 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3532 || str == NULL)
3533 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003534 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003535 else if (PyUnicode_Check(obj))
3536 {
3537 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3538 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003539
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003540 if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
3541 || str == NULL)
3542 {
3543 Py_DECREF(bytes);
3544 return NULL;
3545 }
3546 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003547
3548 /*
3549 * Error checking: String must not contain newlines, as we
3550 * are replacing a single line, and we must replace it with
3551 * a single line.
3552 * A trailing newline is removed, so that append(f.readlines()) works.
3553 */
3554 p = memchr(str, '\n', len);
3555 if (p != NULL)
3556 {
3557 if (p == str + len - 1)
3558 --len;
3559 else
3560 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003561 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003562 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003563 return NULL;
3564 }
3565 }
3566
3567 /* Create a copy of the string, with internal nulls replaced by
3568 * newline characters, as is the vim convention.
3569 */
3570 save = (char *)alloc((unsigned)(len+1));
3571 if (save == NULL)
3572 {
3573 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003574 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003575 return NULL;
3576 }
3577
3578 for (i = 0; i < len; ++i)
3579 {
3580 if (str[i] == '\0')
3581 save[i] = '\n';
3582 else
3583 save[i] = str[i];
3584 }
3585
3586 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003587 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003588
3589 return save;
3590}
3591
3592/* Get a line from the specified buffer. The line number is
3593 * in Vim format (1-based). The line is returned as a Python
3594 * string object.
3595 */
3596 static PyObject *
3597GetBufferLine(buf_T *buf, PyInt n)
3598{
3599 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3600}
3601
3602
3603/* Get a list of lines from the specified buffer. The line numbers
3604 * are in Vim format (1-based). The range is from lo up to, but not
3605 * including, hi. The list is returned as a Python list of string objects.
3606 */
3607 static PyObject *
3608GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3609{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003610 PyInt i;
3611 PyInt n = hi - lo;
3612 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003613
3614 if (list == NULL)
3615 return NULL;
3616
3617 for (i = 0; i < n; ++i)
3618 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003619 PyObject *string = LineToString(
3620 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003621
3622 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003623 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003624 {
3625 Py_DECREF(list);
3626 return NULL;
3627 }
3628
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003629 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003630 }
3631
3632 /* The ownership of the Python list is passed to the caller (ie,
3633 * the caller should Py_DECREF() the object when it is finished
3634 * with it).
3635 */
3636
3637 return list;
3638}
3639
3640/*
3641 * Check if deleting lines made the cursor position invalid.
3642 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3643 * deleted).
3644 */
3645 static void
3646py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3647{
3648 if (curwin->w_cursor.lnum >= lo)
3649 {
3650 /* Adjust the cursor position if it's in/after the changed
3651 * lines. */
3652 if (curwin->w_cursor.lnum >= hi)
3653 {
3654 curwin->w_cursor.lnum += extra;
3655 check_cursor_col();
3656 }
3657 else if (extra < 0)
3658 {
3659 curwin->w_cursor.lnum = lo;
3660 check_cursor();
3661 }
3662 else
3663 check_cursor_col();
3664 changed_cline_bef_curs();
3665 }
3666 invalidate_botline();
3667}
3668
Bram Moolenaar19e60942011-06-19 00:27:51 +02003669/*
3670 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003671 * in Vim format (1-based). The replacement line is given as
3672 * a Python string object. The object is checked for validity
3673 * and correct format. Errors are returned as a value of FAIL.
3674 * The return value is OK on success.
3675 * If OK is returned and len_change is not NULL, *len_change
3676 * is set to the change in the buffer length.
3677 */
3678 static int
3679SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3680{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003681 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003682 * There are three cases:
3683 * 1. NULL, or None - this is a deletion.
3684 * 2. A string - this is a replacement.
3685 * 3. Anything else - this is an error.
3686 */
3687 if (line == Py_None || line == NULL)
3688 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003689 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003690
3691 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003692 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003693
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003694 VimTryStart();
3695
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003696 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003697 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003698 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003699 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003700 else
3701 {
Bram Moolenaar105bc352013-05-17 16:03:57 +02003702 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003703 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
3704 deleted_lines_mark((linenr_T)n, 1L);
3705 }
3706
Bram Moolenaar105bc352013-05-17 16:03:57 +02003707 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003708
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003709 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003710 return FAIL;
3711
3712 if (len_change)
3713 *len_change = -1;
3714
3715 return OK;
3716 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003717 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003718 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003719 char *save = StringToLine(line);
3720 buf_T *savebuf;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003721
3722 if (save == NULL)
3723 return FAIL;
3724
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003725 VimTryStart();
3726
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003727 /* We do not need to free "save" if ml_replace() consumes it. */
3728 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003729 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003730
3731 if (u_savesub((linenr_T)n) == FAIL)
3732 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003733 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003734 vim_free(save);
3735 }
3736 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
3737 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003738 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003739 vim_free(save);
3740 }
3741 else
3742 changed_bytes((linenr_T)n, 0);
3743
Bram Moolenaar105bc352013-05-17 16:03:57 +02003744 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003745
3746 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003747 if (buf == savebuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003748 check_cursor_col();
3749
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003750 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003751 return FAIL;
3752
3753 if (len_change)
3754 *len_change = 0;
3755
3756 return OK;
3757 }
3758 else
3759 {
3760 PyErr_BadArgument();
3761 return FAIL;
3762 }
3763}
3764
Bram Moolenaar19e60942011-06-19 00:27:51 +02003765/* Replace a range of lines in the specified buffer. The line numbers are in
3766 * Vim format (1-based). The range is from lo up to, but not including, hi.
3767 * The replacement lines are given as a Python list of string objects. The
3768 * list is checked for validity and correct format. Errors are returned as a
3769 * value of FAIL. The return value is OK on success.
3770 * If OK is returned and len_change is not NULL, *len_change
3771 * is set to the change in the buffer length.
3772 */
3773 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003774SetBufferLineList(
3775 buf_T *buf,
3776 PyInt lo,
3777 PyInt hi,
3778 PyObject *list,
3779 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003780{
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003781 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02003782 * There are three cases:
3783 * 1. NULL, or None - this is a deletion.
3784 * 2. A list - this is a replacement.
3785 * 3. Anything else - this is an error.
3786 */
3787 if (list == Py_None || list == NULL)
3788 {
3789 PyInt i;
3790 PyInt n = (int)(hi - lo);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003791 buf_T *savebuf;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003792
3793 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003794 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003795 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003796
3797 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003798 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003799 else
3800 {
3801 for (i = 0; i < n; ++i)
3802 {
3803 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3804 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003805 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003806 break;
3807 }
3808 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02003809 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003810 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
3811 deleted_lines_mark((linenr_T)lo, (long)i);
3812 }
3813
Bram Moolenaar105bc352013-05-17 16:03:57 +02003814 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003815
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003816 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003817 return FAIL;
3818
3819 if (len_change)
3820 *len_change = -n;
3821
3822 return OK;
3823 }
3824 else if (PyList_Check(list))
3825 {
3826 PyInt i;
3827 PyInt new_len = PyList_Size(list);
3828 PyInt old_len = hi - lo;
3829 PyInt extra = 0; /* lines added to text, can be negative */
3830 char **array;
3831 buf_T *savebuf;
3832
3833 if (new_len == 0) /* avoid allocating zero bytes */
3834 array = NULL;
3835 else
3836 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003837 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003838 if (array == NULL)
3839 {
3840 PyErr_NoMemory();
3841 return FAIL;
3842 }
3843 }
3844
3845 for (i = 0; i < new_len; ++i)
3846 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003847 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003848
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003849 if (!(line = PyList_GetItem(list, i)) ||
3850 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02003851 {
3852 while (i)
3853 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003854 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003855 return FAIL;
3856 }
3857 }
3858
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003859 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02003860 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003861
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003862 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003863 switch_buffer(&savebuf, buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003864
3865 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003866 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003867
3868 /* If the size of the range is reducing (ie, new_len < old_len) we
3869 * need to delete some old_len. We do this at the start, by
3870 * repeatedly deleting line "lo".
3871 */
3872 if (!PyErr_Occurred())
3873 {
3874 for (i = 0; i < old_len - new_len; ++i)
3875 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
3876 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003877 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003878 break;
3879 }
3880 extra -= i;
3881 }
3882
3883 /* For as long as possible, replace the existing old_len with the
3884 * new old_len. This is a more efficient operation, as it requires
3885 * less memory allocation and freeing.
3886 */
3887 if (!PyErr_Occurred())
3888 {
3889 for (i = 0; i < old_len && i < new_len; ++i)
3890 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
3891 == FAIL)
3892 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003893 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003894 break;
3895 }
3896 }
3897 else
3898 i = 0;
3899
3900 /* Now we may need to insert the remaining new old_len. If we do, we
3901 * must free the strings as we finish with them (we can't pass the
3902 * responsibility to vim in this case).
3903 */
3904 if (!PyErr_Occurred())
3905 {
3906 while (i < new_len)
3907 {
3908 if (ml_append((linenr_T)(lo + i - 1),
3909 (char_u *)array[i], 0, FALSE) == FAIL)
3910 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003911 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02003912 break;
3913 }
3914 vim_free(array[i]);
3915 ++i;
3916 ++extra;
3917 }
3918 }
3919
3920 /* Free any left-over old_len, as a result of an error */
3921 while (i < new_len)
3922 {
3923 vim_free(array[i]);
3924 ++i;
3925 }
3926
3927 /* Free the array of old_len. All of its contents have now
3928 * been dealt with (either freed, or the responsibility passed
3929 * to vim.
3930 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003931 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003932
3933 /* Adjust marks. Invalidate any which lie in the
3934 * changed range, and move any in the remainder of the buffer.
3935 */
3936 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
3937 (long)MAXLNUM, (long)extra);
3938 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
3939
Bram Moolenaar105bc352013-05-17 16:03:57 +02003940 if (buf == savebuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02003941 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
3942
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02003943 /* END of region without "return". */
Bram Moolenaar105bc352013-05-17 16:03:57 +02003944 restore_buffer(savebuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02003945
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003946 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02003947 return FAIL;
3948
3949 if (len_change)
3950 *len_change = new_len - old_len;
3951
3952 return OK;
3953 }
3954 else
3955 {
3956 PyErr_BadArgument();
3957 return FAIL;
3958 }
3959}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003960
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003961/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003962 * The line number is in Vim format (1-based). The lines to be inserted are
3963 * given as a Python list of string objects or as a single string. The lines
3964 * to be added are checked for validity and correct format. Errors are
3965 * returned as a value of FAIL. The return value is OK on success.
3966 * If OK is returned and len_change is not NULL, *len_change
3967 * is set to the change in the buffer length.
3968 */
3969 static int
3970InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
3971{
3972 /* First of all, we check the type of the supplied Python object.
3973 * It must be a string or a list, or the call is in error.
3974 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003975 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003976 {
3977 char *str = StringToLine(lines);
3978 buf_T *savebuf;
3979
3980 if (str == NULL)
3981 return FAIL;
3982
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003983 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003984 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02003985 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003986
3987 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02003988 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003989 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003990 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003991 else
3992 appended_lines_mark((linenr_T)n, 1L);
3993
3994 vim_free(str);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003995 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003996 update_screen(VALID);
3997
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003998 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003999 return FAIL;
4000
4001 if (len_change)
4002 *len_change = 1;
4003
4004 return OK;
4005 }
4006 else if (PyList_Check(lines))
4007 {
4008 PyInt i;
4009 PyInt size = PyList_Size(lines);
4010 char **array;
4011 buf_T *savebuf;
4012
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004013 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004014 if (array == NULL)
4015 {
4016 PyErr_NoMemory();
4017 return FAIL;
4018 }
4019
4020 for (i = 0; i < size; ++i)
4021 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004022 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004023
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004024 if (!(line = PyList_GetItem(lines, i)) ||
4025 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004026 {
4027 while (i)
4028 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004029 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004030 return FAIL;
4031 }
4032 }
4033
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004034 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004035 VimTryStart();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004036 switch_buffer(&savebuf, buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004037
4038 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004039 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004040 else
4041 {
4042 for (i = 0; i < size; ++i)
4043 {
4044 if (ml_append((linenr_T)(n + i),
4045 (char_u *)array[i], 0, FALSE) == FAIL)
4046 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004047 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004048
4049 /* Free the rest of the lines */
4050 while (i < size)
4051 vim_free(array[i++]);
4052
4053 break;
4054 }
4055 vim_free(array[i]);
4056 }
4057 if (i > 0)
4058 appended_lines_mark((linenr_T)n, (long)i);
4059 }
4060
4061 /* Free the array of lines. All of its contents have now
4062 * been freed.
4063 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004064 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004065
Bram Moolenaar105bc352013-05-17 16:03:57 +02004066 restore_buffer(savebuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004067 update_screen(VALID);
4068
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004069 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004070 return FAIL;
4071
4072 if (len_change)
4073 *len_change = size;
4074
4075 return OK;
4076 }
4077 else
4078 {
4079 PyErr_BadArgument();
4080 return FAIL;
4081 }
4082}
4083
4084/*
4085 * Common routines for buffers and line ranges
4086 * -------------------------------------------
4087 */
4088
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004089typedef struct
4090{
4091 PyObject_HEAD
4092 buf_T *buf;
4093} BufferObject;
4094
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004095 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004096CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004097{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004098 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004099 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004100 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101 return -1;
4102 }
4103
4104 return 0;
4105}
4106
4107 static PyObject *
4108RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4109{
4110 if (CheckBuffer(self))
4111 return NULL;
4112
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004113 if (end == -1)
4114 end = self->buf->b_ml.ml_line_count;
4115
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004116 if (n < 0)
4117 n += end - start + 1;
4118
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004119 if (n < 0 || n > end - start)
4120 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004121 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004122 return NULL;
4123 }
4124
4125 return GetBufferLine(self->buf, n+start);
4126}
4127
4128 static PyObject *
4129RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4130{
4131 PyInt size;
4132
4133 if (CheckBuffer(self))
4134 return NULL;
4135
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004136 if (end == -1)
4137 end = self->buf->b_ml.ml_line_count;
4138
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004139 size = end - start + 1;
4140
4141 if (lo < 0)
4142 lo = 0;
4143 else if (lo > size)
4144 lo = size;
4145 if (hi < 0)
4146 hi = 0;
4147 if (hi < lo)
4148 hi = lo;
4149 else if (hi > size)
4150 hi = size;
4151
4152 return GetBufferLineList(self->buf, lo+start, hi+start);
4153}
4154
4155 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004156RBAsItem(
4157 BufferObject *self,
4158 PyInt n,
4159 PyObject *valObject,
4160 PyInt start,
4161 PyInt end,
4162 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004163{
4164 PyInt len_change;
4165
4166 if (CheckBuffer(self))
4167 return -1;
4168
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004169 if (end == -1)
4170 end = self->buf->b_ml.ml_line_count;
4171
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004172 if (n < 0)
4173 n += end - start + 1;
4174
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004175 if (n < 0 || n > end - start)
4176 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004177 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004178 return -1;
4179 }
4180
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004181 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004182 return -1;
4183
4184 if (new_end)
4185 *new_end = end + len_change;
4186
4187 return 0;
4188}
4189
Bram Moolenaar19e60942011-06-19 00:27:51 +02004190 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004191RBAsSlice(
4192 BufferObject *self,
4193 PyInt lo,
4194 PyInt hi,
4195 PyObject *valObject,
4196 PyInt start,
4197 PyInt end,
4198 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004199{
4200 PyInt size;
4201 PyInt len_change;
4202
4203 /* Self must be a valid buffer */
4204 if (CheckBuffer(self))
4205 return -1;
4206
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004207 if (end == -1)
4208 end = self->buf->b_ml.ml_line_count;
4209
Bram Moolenaar19e60942011-06-19 00:27:51 +02004210 /* Sort out the slice range */
4211 size = end - start + 1;
4212
4213 if (lo < 0)
4214 lo = 0;
4215 else if (lo > size)
4216 lo = size;
4217 if (hi < 0)
4218 hi = 0;
4219 if (hi < lo)
4220 hi = lo;
4221 else if (hi > size)
4222 hi = size;
4223
4224 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004225 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004226 return -1;
4227
4228 if (new_end)
4229 *new_end = end + len_change;
4230
4231 return 0;
4232}
4233
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004234
4235 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004236RBAppend(
4237 BufferObject *self,
4238 PyObject *args,
4239 PyInt start,
4240 PyInt end,
4241 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004242{
4243 PyObject *lines;
4244 PyInt len_change;
4245 PyInt max;
4246 PyInt n;
4247
4248 if (CheckBuffer(self))
4249 return NULL;
4250
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004251 if (end == -1)
4252 end = self->buf->b_ml.ml_line_count;
4253
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004254 max = n = end - start + 1;
4255
4256 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4257 return NULL;
4258
4259 if (n < 0 || n > max)
4260 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004261 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004262 return NULL;
4263 }
4264
4265 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4266 return NULL;
4267
4268 if (new_end)
4269 *new_end = end + len_change;
4270
4271 Py_INCREF(Py_None);
4272 return Py_None;
4273}
4274
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004275/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004276 */
4277
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004278static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004279static PySequenceMethods RangeAsSeq;
4280static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004281
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004282typedef struct
4283{
4284 PyObject_HEAD
4285 BufferObject *buf;
4286 PyInt start;
4287 PyInt end;
4288} RangeObject;
4289
4290 static PyObject *
4291RangeNew(buf_T *buf, PyInt start, PyInt end)
4292{
4293 BufferObject *bufr;
4294 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004295 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004296 if (self == NULL)
4297 return NULL;
4298
4299 bufr = (BufferObject *)BufferNew(buf);
4300 if (bufr == NULL)
4301 {
4302 Py_DECREF(self);
4303 return NULL;
4304 }
4305 Py_INCREF(bufr);
4306
4307 self->buf = bufr;
4308 self->start = start;
4309 self->end = end;
4310
4311 return (PyObject *)(self);
4312}
4313
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004314 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004315RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004316{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004317 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004318 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004319 PyObject_GC_Del((void *)(self));
4320}
4321
4322 static int
4323RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4324{
4325 Py_VISIT(((PyObject *)(self->buf)));
4326 return 0;
4327}
4328
4329 static int
4330RangeClear(RangeObject *self)
4331{
4332 Py_CLEAR(self->buf);
4333 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004334}
4335
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004336 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004337RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004338{
4339 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004340 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004341 return -1; /* ??? */
4342
Bram Moolenaard6e39182013-05-21 18:30:34 +02004343 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004344}
4345
4346 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004347RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004348{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004349 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004350}
4351
4352 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004353RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004354{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004355 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004356}
4357
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004358static char *RangeAttrs[] = {
4359 "start", "end",
4360 NULL
4361};
4362
4363 static PyObject *
4364RangeDir(PyObject *self)
4365{
4366 return ObjectDir(self, RangeAttrs);
4367}
4368
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004369 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004370RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004371{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004372 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004373}
4374
4375 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004376RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004377{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004378 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004379 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4380 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004381 else
4382 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004383 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004384
4385 if (name == NULL)
4386 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004387
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004388 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004389 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004390 }
4391}
4392
4393static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004394 /* name, function, calling, documentation */
4395 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004396 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4397 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004398};
4399
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004400static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004401static PySequenceMethods BufferAsSeq;
4402static PyMappingMethods BufferAsMapping;
4403
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004404 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004405BufferNew(buf_T *buf)
4406{
4407 /* We need to handle deletion of buffers underneath us.
4408 * If we add a "b_python*_ref" field to the buf_T structure,
4409 * then we can get at it in buf_freeall() in vim. We then
4410 * need to create only ONE Python object per buffer - if
4411 * we try to create a second, just INCREF the existing one
4412 * and return it. The (single) Python object referring to
4413 * the buffer is stored in "b_python*_ref".
4414 * Question: what to do on a buf_freeall(). We'll probably
4415 * have to either delete the Python object (DECREF it to
4416 * zero - a bad idea, as it leaves dangling refs!) or
4417 * set the buf_T * value to an invalid value (-1?), which
4418 * means we need checks in all access functions... Bah.
4419 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004420 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004421 * b_python_ref and b_python3_ref fields respectively.
4422 */
4423
4424 BufferObject *self;
4425
4426 if (BUF_PYTHON_REF(buf) != NULL)
4427 {
4428 self = BUF_PYTHON_REF(buf);
4429 Py_INCREF(self);
4430 }
4431 else
4432 {
4433 self = PyObject_NEW(BufferObject, &BufferType);
4434 if (self == NULL)
4435 return NULL;
4436 self->buf = buf;
4437 BUF_PYTHON_REF(buf) = self;
4438 }
4439
4440 return (PyObject *)(self);
4441}
4442
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004443 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004444BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004445{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004446 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4447 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004448
4449 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004450}
4451
Bram Moolenaar971db462013-05-12 18:44:48 +02004452 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004453BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004454{
4455 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004456 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004457 return -1; /* ??? */
4458
Bram Moolenaard6e39182013-05-21 18:30:34 +02004459 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004460}
4461
4462 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004463BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004464{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004465 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004466}
4467
4468 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004469BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004470{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004471 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004472}
4473
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004474static char *BufferAttrs[] = {
4475 "name", "number", "vars", "options", "valid",
4476 NULL
4477};
4478
4479 static PyObject *
4480BufferDir(PyObject *self)
4481{
4482 return ObjectDir(self, BufferAttrs);
4483}
4484
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004485 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004486BufferAttrValid(BufferObject *self, char *name)
4487{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004488 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004489
4490 if (strcmp(name, "valid") != 0)
4491 return NULL;
4492
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004493 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4494 Py_INCREF(ret);
4495 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004496}
4497
4498 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004499BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004500{
4501 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004502 return PyString_FromString((self->buf->b_ffname == NULL
4503 ? "" : (char *) self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004504 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004505 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004506 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004507 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004508 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004509 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4510 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004511 else if (strcmp(name, "__members__") == 0)
4512 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004513 else
4514 return NULL;
4515}
4516
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004517 static int
4518BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4519{
4520 if (CheckBuffer(self))
4521 return -1;
4522
4523 if (strcmp(name, "name") == 0)
4524 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004525 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004526 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004527 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004528 PyObject *todecref;
4529
4530 if (!(val = StringToChars(valObject, &todecref)))
4531 return -1;
4532
4533 VimTryStart();
4534 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4535 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004536 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004537 aucmd_restbuf(&aco);
4538 Py_XDECREF(todecref);
4539 if (VimTryEnd())
4540 return -1;
4541
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004542 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004543 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004544 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004545 return -1;
4546 }
4547 return 0;
4548 }
4549 else
4550 {
4551 PyErr_SetString(PyExc_AttributeError, name);
4552 return -1;
4553 }
4554}
4555
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004556 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004557BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004558{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004559 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004560}
4561
4562 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004563BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004564{
4565 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004566 char_u *pmark;
4567 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004568 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004569 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004570
Bram Moolenaard6e39182013-05-21 18:30:34 +02004571 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004572 return NULL;
4573
Bram Moolenaar389a1792013-06-23 13:00:44 +02004574 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004575 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004576
Bram Moolenaar389a1792013-06-23 13:00:44 +02004577 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004578 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004579 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004580 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004581 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004582 return NULL;
4583 }
4584
4585 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004586
4587 Py_XDECREF(todecref);
4588
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004589 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004590 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004591 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004592 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004593 if (VimTryEnd())
4594 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004595
4596 if (posp == NULL)
4597 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004598 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004599 return NULL;
4600 }
4601
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004602 if (posp->lnum <= 0)
4603 {
4604 /* Or raise an error? */
4605 Py_INCREF(Py_None);
4606 return Py_None;
4607 }
4608
4609 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4610}
4611
4612 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004613BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004614{
4615 PyInt start;
4616 PyInt end;
4617
Bram Moolenaard6e39182013-05-21 18:30:34 +02004618 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004619 return NULL;
4620
4621 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4622 return NULL;
4623
Bram Moolenaard6e39182013-05-21 18:30:34 +02004624 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004625}
4626
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004627 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004628BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004629{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004630 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004631 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004632 else
4633 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004634 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004635
4636 if (name == NULL)
4637 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004638
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004639 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004640 }
4641}
4642
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004643static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004644 /* name, function, calling, documentation */
4645 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004646 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004647 {"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 +02004648 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4649 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004650};
4651
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004652/*
4653 * Buffer list object - Implementation
4654 */
4655
4656static PyTypeObject BufMapType;
4657
4658typedef struct
4659{
4660 PyObject_HEAD
4661} BufMapObject;
4662
4663 static PyInt
4664BufMapLength(PyObject *self UNUSED)
4665{
4666 buf_T *b = firstbuf;
4667 PyInt n = 0;
4668
4669 while (b)
4670 {
4671 ++n;
4672 b = b->b_next;
4673 }
4674
4675 return n;
4676}
4677
4678 static PyObject *
4679BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
4680{
4681 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004682 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004683
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004684 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004685 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004686
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004687 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004688
4689 if (b)
4690 return BufferNew(b);
4691 else
4692 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02004693 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004694 return NULL;
4695 }
4696}
4697
4698 static void
4699BufMapIterDestruct(PyObject *buffer)
4700{
4701 /* Iteration was stopped before all buffers were processed */
4702 if (buffer)
4703 {
4704 Py_DECREF(buffer);
4705 }
4706}
4707
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004708 static int
4709BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
4710{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004711 if (buffer)
4712 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004713 return 0;
4714}
4715
4716 static int
4717BufMapIterClear(PyObject **buffer)
4718{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004719 if (*buffer)
4720 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004721 return 0;
4722}
4723
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004724 static PyObject *
4725BufMapIterNext(PyObject **buffer)
4726{
4727 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004728 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004729
4730 if (!*buffer)
4731 return NULL;
4732
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004733 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004734
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004735 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004736 {
4737 *buffer = NULL;
4738 return NULL;
4739 }
4740
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004741 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004742 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004743 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004744 return NULL;
4745 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02004746 /* Do not increment reference: we no longer hold it (decref), but whoever
4747 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004748 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004749}
4750
4751 static PyObject *
4752BufMapIter(PyObject *self UNUSED)
4753{
4754 PyObject *buffer;
4755
4756 buffer = BufferNew(firstbuf);
4757 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02004758 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
4759 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004760}
4761
4762static PyMappingMethods BufMapAsMapping = {
4763 (lenfunc) BufMapLength,
4764 (binaryfunc) BufMapItem,
4765 (objobjargproc) 0,
4766};
4767
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004768/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004769 */
4770
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004771static char *CurrentAttrs[] = {
4772 "buffer", "window", "line", "range", "tabpage",
4773 NULL
4774};
4775
4776 static PyObject *
4777CurrentDir(PyObject *self)
4778{
4779 return ObjectDir(self, CurrentAttrs);
4780}
4781
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004782 static PyObject *
4783CurrentGetattr(PyObject *self UNUSED, char *name)
4784{
4785 if (strcmp(name, "buffer") == 0)
4786 return (PyObject *)BufferNew(curbuf);
4787 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004788 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004789 else if (strcmp(name, "tabpage") == 0)
4790 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004791 else if (strcmp(name, "line") == 0)
4792 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
4793 else if (strcmp(name, "range") == 0)
4794 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004795 else if (strcmp(name, "__members__") == 0)
4796 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004797 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004798#if PY_MAJOR_VERSION < 3
4799 return Py_FindMethod(WindowMethods, self, name);
4800#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004801 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004802#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004803}
4804
4805 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004806CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004807{
4808 if (strcmp(name, "line") == 0)
4809 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004810 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
4811 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004812 return -1;
4813
4814 return 0;
4815 }
Bram Moolenaare7614592013-05-15 15:51:08 +02004816 else if (strcmp(name, "buffer") == 0)
4817 {
4818 int count;
4819
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004820 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004821 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004822 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004823 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004824 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004825 return -1;
4826 }
4827
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004828 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004829 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004830 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02004831
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004832 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02004833 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
4834 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004835 if (VimTryEnd())
4836 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004837 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02004838 return -1;
4839 }
4840
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004841 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004842 }
4843 else if (strcmp(name, "window") == 0)
4844 {
4845 int count;
4846
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004847 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004848 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004849 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004850 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004851 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004852 return -1;
4853 }
4854
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004855 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004856 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004857 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02004858
4859 if (!count)
4860 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004861 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004862 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004863 return -1;
4864 }
4865
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004866 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004867 win_goto(((WindowObject *)(valObject))->win);
4868 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02004869 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004870 if (VimTryEnd())
4871 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004872 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004873 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004874 return -1;
4875 }
4876
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004877 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004878 }
4879 else if (strcmp(name, "tabpage") == 0)
4880 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004881 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02004882 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004883 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004884 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004885 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02004886 return -1;
4887 }
4888
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004889 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02004890 return -1;
4891
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004892 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004893 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
4894 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02004895 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004896 if (VimTryEnd())
4897 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004898 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004899 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02004900 return -1;
4901 }
4902
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004903 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02004904 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004905 else
4906 {
4907 PyErr_SetString(PyExc_AttributeError, name);
4908 return -1;
4909 }
4910}
4911
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004912static struct PyMethodDef CurrentMethods[] = {
4913 /* name, function, calling, documentation */
4914 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
4915 { NULL, NULL, 0, NULL}
4916};
4917
Bram Moolenaardb913952012-06-29 12:54:53 +02004918 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004919init_range_cmd(exarg_T *eap)
4920{
4921 RangeStart = eap->line1;
4922 RangeEnd = eap->line2;
4923}
4924
4925 static void
4926init_range_eval(typval_T *rettv UNUSED)
4927{
4928 RangeStart = (PyInt) curwin->w_cursor.lnum;
4929 RangeEnd = RangeStart;
4930}
4931
4932 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004933run_cmd(const char *cmd, void *arg UNUSED
4934#ifdef PY_CAN_RECURSE
4935 , PyGILState_STATE *pygilstate UNUSED
4936#endif
4937 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004938{
4939 PyRun_SimpleString((char *) cmd);
4940}
4941
4942static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
4943static int code_hdr_len = 30;
4944
4945 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004946run_do(const char *cmd, void *arg UNUSED
4947#ifdef PY_CAN_RECURSE
4948 , PyGILState_STATE *pygilstate
4949#endif
4950 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004951{
4952 PyInt lnum;
4953 size_t len;
4954 char *code;
4955 int status;
4956 PyObject *pyfunc, *pymain;
4957
Bram Moolenaar4ac66762013-05-28 22:31:46 +02004958 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004959 {
4960 EMSG(_("cannot save undo information"));
4961 return;
4962 }
4963
4964 len = code_hdr_len + STRLEN(cmd);
4965 code = PyMem_New(char, len + 1);
4966 memcpy(code, code_hdr, code_hdr_len);
4967 STRCPY(code + code_hdr_len, cmd);
4968 status = PyRun_SimpleString(code);
4969 PyMem_Free(code);
4970
4971 if (status)
4972 {
4973 EMSG(_("failed to run the code"));
4974 return;
4975 }
4976
4977 status = 0;
4978 pymain = PyImport_AddModule("__main__");
4979 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004980#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004981 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004982#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004983
4984 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
4985 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004986 PyObject *line;
4987 PyObject *linenr;
4988 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004989
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004990#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004991 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02004992#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02004993 if (!(line = GetBufferLine(curbuf, lnum)))
4994 goto err;
4995 if (!(linenr = PyInt_FromLong((long) lnum)))
4996 {
4997 Py_DECREF(line);
4998 goto err;
4999 }
5000 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5001 Py_DECREF(line);
5002 Py_DECREF(linenr);
5003 if (!ret)
5004 goto err;
5005
5006 if (ret != Py_None)
5007 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5008 goto err;
5009
5010 Py_XDECREF(ret);
5011 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005012#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005013 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005014#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005015 }
5016 goto out;
5017err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005018#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005019 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005020#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005021 PyErr_PrintEx(0);
5022 PythonIO_Flush();
5023 status = 1;
5024out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005025#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005026 if (!status)
5027 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005028#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005029 Py_DECREF(pyfunc);
5030 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5031 if (status)
5032 return;
5033 check_cursor();
5034 update_curbuf(NOT_VALID);
5035}
5036
5037 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005038run_eval(const char *cmd, typval_T *rettv
5039#ifdef PY_CAN_RECURSE
5040 , PyGILState_STATE *pygilstate UNUSED
5041#endif
5042 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005043{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005044 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005045
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005046 run_ret = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
5047 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005048 {
5049 if (PyErr_Occurred() && !msg_silent)
5050 PyErr_PrintEx(0);
5051 EMSG(_("E858: Eval did not return a valid python object"));
5052 }
5053 else
5054 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005055 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005056 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005057 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005058 }
5059 PyErr_Clear();
5060}
5061
5062 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005063set_ref_in_py(const int copyID)
5064{
5065 pylinkedlist_T *cur;
5066 dict_T *dd;
5067 list_T *ll;
5068
5069 if (lastdict != NULL)
5070 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5071 {
5072 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5073 if (dd->dv_copyID != copyID)
5074 {
5075 dd->dv_copyID = copyID;
5076 set_ref_in_ht(&dd->dv_hashtab, copyID);
5077 }
5078 }
5079
5080 if (lastlist != NULL)
5081 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5082 {
5083 ll = ((ListObject *) (cur->pll_obj))->list;
5084 if (ll->lv_copyID != copyID)
5085 {
5086 ll->lv_copyID = copyID;
5087 set_ref_in_list(ll, copyID);
5088 }
5089 }
5090}
5091
5092 static int
5093set_string_copy(char_u *str, typval_T *tv)
5094{
5095 tv->vval.v_string = vim_strsave(str);
5096 if (tv->vval.v_string == NULL)
5097 {
5098 PyErr_NoMemory();
5099 return -1;
5100 }
5101 return 0;
5102}
5103
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005104 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005105pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005106{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005107 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005108 char_u *key;
5109 dictitem_T *di;
5110 PyObject *keyObject;
5111 PyObject *valObject;
5112 Py_ssize_t iter = 0;
5113
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005114 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005115 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005116
5117 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005118 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005119
5120 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5121 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005122 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005123
Bram Moolenaara03e6312013-05-29 22:49:26 +02005124 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005125 {
5126 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005127 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005128 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005129
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005130 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005131 {
5132 dict_unref(dict);
5133 return -1;
5134 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005135
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005136 if (*key == NUL)
5137 {
5138 dict_unref(dict);
5139 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005140 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005141 return -1;
5142 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005143
5144 di = dictitem_alloc(key);
5145
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005146 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005147
5148 if (di == NULL)
5149 {
5150 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005151 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005152 return -1;
5153 }
5154 di->di_tv.v_lock = 0;
5155
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005156 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005157 {
5158 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005159 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005160 return -1;
5161 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005162
5163 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005164 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005165 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005166 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005167 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005168 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005169 return -1;
5170 }
5171 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005172
5173 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005174 return 0;
5175}
5176
5177 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005178pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005179{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005180 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005181 char_u *key;
5182 dictitem_T *di;
5183 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005184 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005185 PyObject *keyObject;
5186 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005187
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005188 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005189 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005190
5191 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005192 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005193
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005194 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005195 {
5196 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005197 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005198 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005199
5200 if (!(iterator = PyObject_GetIter(list)))
5201 {
5202 dict_unref(dict);
5203 Py_DECREF(list);
5204 return -1;
5205 }
5206 Py_DECREF(list);
5207
5208 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005209 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005210 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005211
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005212 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005213 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005214 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005215 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005216 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005217 return -1;
5218 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005219
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005220 if (*key == NUL)
5221 {
5222 Py_DECREF(keyObject);
5223 Py_DECREF(iterator);
5224 Py_XDECREF(todecref);
5225 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005226 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005227 return -1;
5228 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005229
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005230 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005231 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005232 Py_DECREF(keyObject);
5233 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005234 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005235 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005236 return -1;
5237 }
5238
5239 di = dictitem_alloc(key);
5240
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005241 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005242 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005243
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005244 if (di == NULL)
5245 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005246 Py_DECREF(iterator);
5247 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005248 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005249 PyErr_NoMemory();
5250 return -1;
5251 }
5252 di->di_tv.v_lock = 0;
5253
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005254 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005255 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005256 Py_DECREF(iterator);
5257 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005258 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005259 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005260 return -1;
5261 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005262
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005263 Py_DECREF(valObject);
5264
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005265 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005266 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005267 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005268 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005269 dictitem_free(di);
5270 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005271 return -1;
5272 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005273 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005274 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005275 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005276 return 0;
5277}
5278
5279 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005280pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005281{
5282 list_T *l;
5283
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005284 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005285 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005286
5287 tv->v_type = VAR_LIST;
5288 tv->vval.v_list = l;
5289
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005290 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005291 {
5292 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005293 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005294 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005295
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005296 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005297 return 0;
5298}
5299
Bram Moolenaardb913952012-06-29 12:54:53 +02005300typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5301
5302 static int
5303convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005304 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005305{
5306 PyObject *capsule;
5307 char hexBuf[sizeof(void *) * 2 + 3];
5308
5309 sprintf(hexBuf, "%p", obj);
5310
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005311# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005312 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005313# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005314 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005315# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005316 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005317 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005318# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005319 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005320# else
5321 capsule = PyCObject_FromVoidPtr(tv, NULL);
5322# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005323 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5324 {
5325 Py_DECREF(capsule);
5326 tv->v_type = VAR_UNKNOWN;
5327 return -1;
5328 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005329
5330 Py_DECREF(capsule);
5331
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005332 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005333 {
5334 tv->v_type = VAR_UNKNOWN;
5335 return -1;
5336 }
5337 /* As we are not using copy_tv which increments reference count we must
5338 * do it ourself. */
5339 switch(tv->v_type)
5340 {
5341 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5342 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5343 }
5344 }
5345 else
5346 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005347 typval_T *v;
5348
5349# ifdef PY_USE_CAPSULE
5350 v = PyCapsule_GetPointer(capsule, NULL);
5351# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005352 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005353# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005354 copy_tv(v, tv);
5355 }
5356 return 0;
5357}
5358
5359 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005360ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5361{
5362 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005363 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005364
5365 if (!(lookup_dict = PyDict_New()))
5366 return -1;
5367
5368 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5369 {
5370 tv->v_type = VAR_DICT;
5371 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5372 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005373 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005374 }
5375 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005376 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005377 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005378 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005379 else
5380 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005381 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005382 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005383 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005384 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005385 }
5386 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005387 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005388}
5389
5390 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005391ConvertFromPyObject(PyObject *obj, typval_T *tv)
5392{
5393 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005394 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005395
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005396 if (!(lookup_dict = PyDict_New()))
5397 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005398 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005399 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005400 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005401}
5402
5403 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005404_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005405{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005406 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005407 {
5408 tv->v_type = VAR_DICT;
5409 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5410 ++tv->vval.v_dict->dv_refcount;
5411 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005412 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005413 {
5414 tv->v_type = VAR_LIST;
5415 tv->vval.v_list = (((ListObject *)(obj))->list);
5416 ++tv->vval.v_list->lv_refcount;
5417 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005418 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005419 {
5420 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5421 return -1;
5422
5423 tv->v_type = VAR_FUNC;
5424 func_ref(tv->vval.v_string);
5425 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005426 else if (PyBytes_Check(obj))
5427 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005428 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005429
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005430 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005431 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005432 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005433 return -1;
5434
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005435 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005436 return -1;
5437
5438 tv->v_type = VAR_STRING;
5439 }
5440 else if (PyUnicode_Check(obj))
5441 {
5442 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005443 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005444
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005445 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005446 if (bytes == NULL)
5447 return -1;
5448
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005449 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005450 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005451 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005452 return -1;
5453
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005454 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005455 {
5456 Py_XDECREF(bytes);
5457 return -1;
5458 }
5459 Py_XDECREF(bytes);
5460
5461 tv->v_type = VAR_STRING;
5462 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005463#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005464 else if (PyInt_Check(obj))
5465 {
5466 tv->v_type = VAR_NUMBER;
5467 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005468 if (PyErr_Occurred())
5469 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005470 }
5471#endif
5472 else if (PyLong_Check(obj))
5473 {
5474 tv->v_type = VAR_NUMBER;
5475 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005476 if (PyErr_Occurred())
5477 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005478 }
5479 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005480 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005481#ifdef FEAT_FLOAT
5482 else if (PyFloat_Check(obj))
5483 {
5484 tv->v_type = VAR_FLOAT;
5485 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5486 }
5487#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005488 else if (PyObject_HasAttrString(obj, "keys"))
5489 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005490 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005491 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005492 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005493 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005494 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005495 else if (PyNumber_Check(obj))
5496 {
5497 PyObject *num;
5498
5499 if (!(num = PyNumber_Long(obj)))
5500 return -1;
5501
5502 tv->v_type = VAR_NUMBER;
5503 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5504
5505 Py_DECREF(num);
5506 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005507 else
5508 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005509 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005510 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005511 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005512 return -1;
5513 }
5514 return 0;
5515}
5516
5517 static PyObject *
5518ConvertToPyObject(typval_T *tv)
5519{
5520 if (tv == NULL)
5521 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005522 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005523 return NULL;
5524 }
5525 switch (tv->v_type)
5526 {
5527 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005528 return PyBytes_FromString(tv->vval.v_string == NULL
5529 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005530 case VAR_NUMBER:
5531 return PyLong_FromLong((long) tv->vval.v_number);
5532#ifdef FEAT_FLOAT
5533 case VAR_FLOAT:
5534 return PyFloat_FromDouble((double) tv->vval.v_float);
5535#endif
5536 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005537 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005538 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005539 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005540 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005541 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005542 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005543 case VAR_UNKNOWN:
5544 Py_INCREF(Py_None);
5545 return Py_None;
5546 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005547 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005548 return NULL;
5549 }
5550}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005551
5552typedef struct
5553{
5554 PyObject_HEAD
5555} CurrentObject;
5556static PyTypeObject CurrentType;
5557
5558 static void
5559init_structs(void)
5560{
5561 vim_memset(&OutputType, 0, sizeof(OutputType));
5562 OutputType.tp_name = "vim.message";
5563 OutputType.tp_basicsize = sizeof(OutputObject);
5564 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5565 OutputType.tp_doc = "vim message object";
5566 OutputType.tp_methods = OutputMethods;
5567#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005568 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5569 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005570 OutputType.tp_alloc = call_PyType_GenericAlloc;
5571 OutputType.tp_new = call_PyType_GenericNew;
5572 OutputType.tp_free = call_PyObject_Free;
5573#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005574 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5575 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005576#endif
5577
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005578 vim_memset(&IterType, 0, sizeof(IterType));
5579 IterType.tp_name = "vim.iter";
5580 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005581 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005582 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005583 IterType.tp_iter = (getiterfunc)IterIter;
5584 IterType.tp_iternext = (iternextfunc)IterNext;
5585 IterType.tp_dealloc = (destructor)IterDestructor;
5586 IterType.tp_traverse = (traverseproc)IterTraverse;
5587 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005588
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005589 vim_memset(&BufferType, 0, sizeof(BufferType));
5590 BufferType.tp_name = "vim.buffer";
5591 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005592 BufferType.tp_dealloc = (destructor)BufferDestructor;
5593 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005594 BufferType.tp_as_sequence = &BufferAsSeq;
5595 BufferType.tp_as_mapping = &BufferAsMapping;
5596 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5597 BufferType.tp_doc = "vim buffer object";
5598 BufferType.tp_methods = BufferMethods;
5599#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005600 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005601 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005602 BufferType.tp_alloc = call_PyType_GenericAlloc;
5603 BufferType.tp_new = call_PyType_GenericNew;
5604 BufferType.tp_free = call_PyObject_Free;
5605#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005606 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005607 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005608#endif
5609
5610 vim_memset(&WindowType, 0, sizeof(WindowType));
5611 WindowType.tp_name = "vim.window";
5612 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005613 WindowType.tp_dealloc = (destructor)WindowDestructor;
5614 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005615 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005616 WindowType.tp_doc = "vim Window object";
5617 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005618 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5619 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005620#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005621 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5622 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005623 WindowType.tp_alloc = call_PyType_GenericAlloc;
5624 WindowType.tp_new = call_PyType_GenericNew;
5625 WindowType.tp_free = call_PyObject_Free;
5626#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005627 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5628 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005629#endif
5630
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005631 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5632 TabPageType.tp_name = "vim.tabpage";
5633 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005634 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5635 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005636 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5637 TabPageType.tp_doc = "vim tab page object";
5638 TabPageType.tp_methods = TabPageMethods;
5639#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005640 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005641 TabPageType.tp_alloc = call_PyType_GenericAlloc;
5642 TabPageType.tp_new = call_PyType_GenericNew;
5643 TabPageType.tp_free = call_PyObject_Free;
5644#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005645 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005646#endif
5647
Bram Moolenaardfa38d42013-05-15 13:38:47 +02005648 vim_memset(&BufMapType, 0, sizeof(BufMapType));
5649 BufMapType.tp_name = "vim.bufferlist";
5650 BufMapType.tp_basicsize = sizeof(BufMapObject);
5651 BufMapType.tp_as_mapping = &BufMapAsMapping;
5652 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005653 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005654 BufferType.tp_doc = "vim buffer list";
5655
5656 vim_memset(&WinListType, 0, sizeof(WinListType));
5657 WinListType.tp_name = "vim.windowlist";
5658 WinListType.tp_basicsize = sizeof(WinListType);
5659 WinListType.tp_as_sequence = &WinListAsSeq;
5660 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
5661 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005662 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005663
5664 vim_memset(&TabListType, 0, sizeof(TabListType));
5665 TabListType.tp_name = "vim.tabpagelist";
5666 TabListType.tp_basicsize = sizeof(TabListType);
5667 TabListType.tp_as_sequence = &TabListAsSeq;
5668 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
5669 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005670
5671 vim_memset(&RangeType, 0, sizeof(RangeType));
5672 RangeType.tp_name = "vim.range";
5673 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005674 RangeType.tp_dealloc = (destructor)RangeDestructor;
5675 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005676 RangeType.tp_as_sequence = &RangeAsSeq;
5677 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005678 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005679 RangeType.tp_doc = "vim Range object";
5680 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005681 RangeType.tp_traverse = (traverseproc)RangeTraverse;
5682 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005683#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005684 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005685 RangeType.tp_alloc = call_PyType_GenericAlloc;
5686 RangeType.tp_new = call_PyType_GenericNew;
5687 RangeType.tp_free = call_PyObject_Free;
5688#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005689 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005690#endif
5691
5692 vim_memset(&CurrentType, 0, sizeof(CurrentType));
5693 CurrentType.tp_name = "vim.currentdata";
5694 CurrentType.tp_basicsize = sizeof(CurrentObject);
5695 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
5696 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005697 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005698#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005699 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
5700 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005701#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005702 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
5703 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005704#endif
5705
5706 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
5707 DictionaryType.tp_name = "vim.dictionary";
5708 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005709 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005710 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005711 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005712 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005713 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
5714 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005715 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
5716 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
5717 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005718#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005719 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
5720 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005721#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005722 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
5723 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005724#endif
5725
5726 vim_memset(&ListType, 0, sizeof(ListType));
5727 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005728 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005729 ListType.tp_basicsize = sizeof(ListObject);
5730 ListType.tp_as_sequence = &ListAsSeq;
5731 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005732 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005733 ListType.tp_doc = "list pushing modifications to vim structure";
5734 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005735 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005736 ListType.tp_new = (newfunc)ListConstructor;
5737 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005738#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005739 ListType.tp_getattro = (getattrofunc)ListGetattro;
5740 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005741#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005742 ListType.tp_getattr = (getattrfunc)ListGetattr;
5743 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005744#endif
5745
5746 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005747 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005748 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005749 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
5750 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005751 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005752 FunctionType.tp_doc = "object that calls vim function";
5753 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02005754 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005755 FunctionType.tp_new = (newfunc)FunctionConstructor;
5756 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005757#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005758 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005759#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005760 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005761#endif
5762
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005763 vim_memset(&OptionsType, 0, sizeof(OptionsType));
5764 OptionsType.tp_name = "vim.options";
5765 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005766 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005767 OptionsType.tp_doc = "object for manipulating options";
5768 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005769 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
5770 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
5771 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005772
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005773 vim_memset(&LoaderType, 0, sizeof(LoaderType));
5774 LoaderType.tp_name = "vim.Loader";
5775 LoaderType.tp_basicsize = sizeof(LoaderObject);
5776 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
5777 LoaderType.tp_doc = "vim message object";
5778 LoaderType.tp_methods = LoaderMethods;
5779 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
5780
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005781#if PY_MAJOR_VERSION >= 3
5782 vim_memset(&vimmodule, 0, sizeof(vimmodule));
5783 vimmodule.m_name = "vim";
5784 vimmodule.m_doc = "Vim Python interface\n";
5785 vimmodule.m_size = -1;
5786 vimmodule.m_methods = VimMethods;
5787#endif
5788}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005789
5790#define PYTYPE_READY(type) \
5791 if (PyType_Ready(&type)) \
5792 return -1;
5793
5794 static int
5795init_types()
5796{
5797 PYTYPE_READY(IterType);
5798 PYTYPE_READY(BufferType);
5799 PYTYPE_READY(RangeType);
5800 PYTYPE_READY(WindowType);
5801 PYTYPE_READY(TabPageType);
5802 PYTYPE_READY(BufMapType);
5803 PYTYPE_READY(WinListType);
5804 PYTYPE_READY(TabListType);
5805 PYTYPE_READY(CurrentType);
5806 PYTYPE_READY(DictionaryType);
5807 PYTYPE_READY(ListType);
5808 PYTYPE_READY(FunctionType);
5809 PYTYPE_READY(OptionsType);
5810 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02005811 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005812 return 0;
5813}
5814
5815 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02005816init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02005817{
5818 PyObject *path;
5819 PyObject *path_hook;
5820 PyObject *path_hooks;
5821
5822 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
5823 return -1;
5824
5825 if (!(path_hooks = PySys_GetObject("path_hooks")))
5826 {
5827 PyErr_Clear();
5828 path_hooks = PyList_New(1);
5829 PyList_SET_ITEM(path_hooks, 0, path_hook);
5830 if (PySys_SetObject("path_hooks", path_hooks))
5831 {
5832 Py_DECREF(path_hooks);
5833 return -1;
5834 }
5835 Py_DECREF(path_hooks);
5836 }
5837 else if (PyList_Check(path_hooks))
5838 {
5839 if (PyList_Append(path_hooks, path_hook))
5840 {
5841 Py_DECREF(path_hook);
5842 return -1;
5843 }
5844 Py_DECREF(path_hook);
5845 }
5846 else
5847 {
5848 VimTryStart();
5849 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
5850 "You should now do the following:\n"
5851 "- append vim.path_hook to sys.path_hooks\n"
5852 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
5853 VimTryEnd(); /* Discard the error */
5854 Py_DECREF(path_hook);
5855 return 0;
5856 }
5857
5858 if (!(path = PySys_GetObject("path")))
5859 {
5860 PyErr_Clear();
5861 path = PyList_New(1);
5862 Py_INCREF(vim_special_path_object);
5863 PyList_SET_ITEM(path, 0, vim_special_path_object);
5864 if (PySys_SetObject("path", path))
5865 {
5866 Py_DECREF(path);
5867 return -1;
5868 }
5869 Py_DECREF(path);
5870 }
5871 else if (PyList_Check(path))
5872 {
5873 if (PyList_Append(path, vim_special_path_object))
5874 return -1;
5875 }
5876 else
5877 {
5878 VimTryStart();
5879 EMSG(_("Failed to set path: sys.path is not a list\n"
5880 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
5881 VimTryEnd(); /* Discard the error */
5882 }
5883
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005884 return 0;
5885}
5886
5887static BufMapObject TheBufferMap =
5888{
5889 PyObject_HEAD_INIT(&BufMapType)
5890};
5891
5892static WinListObject TheWindowList =
5893{
5894 PyObject_HEAD_INIT(&WinListType)
5895 NULL
5896};
5897
5898static CurrentObject TheCurrent =
5899{
5900 PyObject_HEAD_INIT(&CurrentType)
5901};
5902
5903static TabListObject TheTabPageList =
5904{
5905 PyObject_HEAD_INIT(&TabListType)
5906};
5907
5908static struct numeric_constant {
5909 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005910 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005911} numeric_constants[] = {
5912 {"VAR_LOCKED", VAR_LOCKED},
5913 {"VAR_FIXED", VAR_FIXED},
5914 {"VAR_SCOPE", VAR_SCOPE},
5915 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
5916};
5917
5918static struct object_constant {
5919 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005920 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005921} object_constants[] = {
5922 {"buffers", (PyObject *)(void *)&TheBufferMap},
5923 {"windows", (PyObject *)(void *)&TheWindowList},
5924 {"tabpages", (PyObject *)(void *)&TheTabPageList},
5925 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02005926
5927 {"Buffer", (PyObject *)&BufferType},
5928 {"Range", (PyObject *)&RangeType},
5929 {"Window", (PyObject *)&WindowType},
5930 {"TabPage", (PyObject *)&TabPageType},
5931 {"Dictionary", (PyObject *)&DictionaryType},
5932 {"List", (PyObject *)&ListType},
5933 {"Function", (PyObject *)&FunctionType},
5934 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005935 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005936};
5937
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005938#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02005939 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005940 return -1;
5941
5942#define ADD_CHECKED_OBJECT(m, name, obj) \
5943 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005944 PyObject *valObject = obj; \
5945 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005946 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005947 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005948 }
5949
5950 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02005951populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005952{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005953 int i;
5954 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005955 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02005956 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005957
5958 for (i = 0; i < (int)(sizeof(numeric_constants)
5959 / sizeof(struct numeric_constant));
5960 ++i)
5961 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005962 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005963
5964 for (i = 0; i < (int)(sizeof(object_constants)
5965 / sizeof(struct object_constant));
5966 ++i)
5967 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005968 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005969
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005970 valObject = object_constants[i].valObject;
5971 Py_INCREF(valObject);
5972 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005973 }
5974
5975 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
5976 return -1;
5977 ADD_OBJECT(m, "error", VimError);
5978
Bram Moolenaara9922d62013-05-30 13:01:18 +02005979 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
5980 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02005981 ADD_CHECKED_OBJECT(m, "options",
5982 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02005983
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005984 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005985 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005986 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02005987
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005988 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005989 return -1;
5990 ADD_OBJECT(m, "_getcwd", py_getcwd)
5991
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02005992 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005993 return -1;
5994 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005995 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02005996 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02005997 if (PyObject_SetAttrString(other_module, "chdir", attr))
5998 {
5999 Py_DECREF(attr);
6000 return -1;
6001 }
6002 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006003
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006004 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006005 {
6006 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006007 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006008 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006009 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6010 {
6011 Py_DECREF(attr);
6012 return -1;
6013 }
6014 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006015 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006016 else
6017 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006018
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006019 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6020 return -1;
6021
6022 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6023
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006024 if (!(imp = PyImport_ImportModule("imp")))
6025 return -1;
6026
6027 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6028 {
6029 Py_DECREF(imp);
6030 return -1;
6031 }
6032
6033 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6034 {
6035 Py_DECREF(py_find_module);
6036 Py_DECREF(imp);
6037 return -1;
6038 }
6039
6040 Py_DECREF(imp);
6041
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006042 ADD_OBJECT(m, "_find_module", py_find_module);
6043 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006044
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006045 return 0;
6046}