blob: d6ae8806665f188796444f8712e67d06171dc610 [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 Moolenaar78cf3f02014-01-10 18:16:07 +010016#ifdef __BORLANDC__
17/* Disable Warning W8060: Possibly incorrect assignment in function ... */
18# pragma warn -8060
19#endif
20
Bram Moolenaar41009372013-07-01 22:03:04 +020021static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
22
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020023#if PY_VERSION_HEX < 0x02050000
24typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
25#endif
26
Bram Moolenaar91805fc2011-06-26 04:01:44 +020027#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020028# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020029#else
30# define ENC_OPT "latin1"
31#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020032#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020033
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020034static const char *vim_special_path = "_vim_path_";
35
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020036#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020038#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010039#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
40#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
41#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020042
43#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
44 ? "(NULL)" \
45 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020046
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020047#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 N_("empty keys are not allowed"))
49#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
50#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
51#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
52#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
53#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
54#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020055#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020056 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020057#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020058 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020059 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020061#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
62#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020063#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020065typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020066typedef void (*runner)(const char *, void *
67#ifdef PY_CAN_RECURSE
68 , PyGILState_STATE *
69#endif
70 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020071
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020072static int ConvertFromPyObject(PyObject *, typval_T *);
73static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020074static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +020075static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020076static PyObject *WindowNew(win_T *, tabpage_T *);
77static PyObject *BufferNew (buf_T *);
78static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020079
80static PyInt RangeStart;
81static PyInt RangeEnd;
82
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020083static PyObject *globals;
84
Bram Moolenaarf4258302013-06-02 18:20:17 +020085static PyObject *py_chdir;
86static PyObject *py_fchdir;
87static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020088static PyObject *vim_module;
89static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020090
Bram Moolenaar81c40c52013-06-12 14:41:04 +020091static PyObject *py_find_module;
92static PyObject *py_load_module;
93
94static PyObject *VimError;
95
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020096/*
97 * obtain a lock on the Vim data structures
98 */
99 static void
100Python_Lock_Vim(void)
101{
102}
103
104/*
105 * release a lock on the Vim data structures
106 */
107 static void
108Python_Release_Vim(void)
109{
110}
111
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200112/*
113 * The "todecref" argument holds a pointer to PyObject * that must be
114 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
115 * was needed to generate returned value is object.
116 *
117 * Use Py_XDECREF to decrement reference count.
118 */
119 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200120StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200121{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200122 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200123
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200124 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125 {
126
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200127 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
128 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200129 return NULL;
130
131 *todecref = NULL;
132 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200133 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200134 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200135 PyObject *bytes;
136
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200137 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200138 return NULL;
139
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200140 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
141 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200142 {
143 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200144 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200145 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200146
147 *todecref = bytes;
148 }
149 else
150 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200151#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200152 PyErr_FORMAT(PyExc_TypeError,
153 N_("expected str() or unicode() instance, but got %s"),
154 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200155#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200156 PyErr_FORMAT(PyExc_TypeError,
157 N_("expected bytes() or str() instance, but got %s"),
158 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200159#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200160 return NULL;
161 }
162
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200163 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200164}
165
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200166#define NUMBER_LONG 1
167#define NUMBER_INT 2
168#define NUMBER_NATURAL 4
169#define NUMBER_UNSIGNED 8
170
171 static int
172NumberToLong(PyObject *obj, long *result, int flags)
173{
174#if PY_MAJOR_VERSION < 3
175 if (PyInt_Check(obj))
176 {
177 *result = PyInt_AsLong(obj);
178 if (PyErr_Occurred())
179 return -1;
180 }
181 else
182#endif
183 if (PyLong_Check(obj))
184 {
185 *result = PyLong_AsLong(obj);
186 if (PyErr_Occurred())
187 return -1;
188 }
189 else if (PyNumber_Check(obj))
190 {
191 PyObject *num;
192
193 if (!(num = PyNumber_Long(obj)))
194 return -1;
195
196 *result = PyLong_AsLong(num);
197
198 Py_DECREF(num);
199
200 if (PyErr_Occurred())
201 return -1;
202 }
203 else
204 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200205#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200206 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200207 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200208 "coercing to long(), but got %s"),
209 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200210#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200211 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200212 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200213 "but got %s"),
214 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200215#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200216 return -1;
217 }
218
219 if (flags & NUMBER_INT)
220 {
221 if (*result > INT_MAX)
222 {
223 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200224 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200225 return -1;
226 }
227 else if (*result < INT_MIN)
228 {
229 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200230 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200231 return -1;
232 }
233 }
234
235 if (flags & NUMBER_NATURAL)
236 {
237 if (*result <= 0)
238 {
239 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100240 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200241 return -1;
242 }
243 }
244 else if (flags & NUMBER_UNSIGNED)
245 {
246 if (*result < 0)
247 {
248 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200249 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200250 return -1;
251 }
252 }
253
254 return 0;
255}
256
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200257 static int
258add_string(PyObject *list, char *s)
259{
260 PyObject *string;
261
262 if (!(string = PyString_FromString(s)))
263 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200264
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200265 if (PyList_Append(list, string))
266 {
267 Py_DECREF(string);
268 return -1;
269 }
270
271 Py_DECREF(string);
272 return 0;
273}
274
275 static PyObject *
276ObjectDir(PyObject *self, char **attributes)
277{
278 PyMethodDef *method;
279 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200280 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200281
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200282 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200283 return NULL;
284
285 if (self)
286 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200287 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200288 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200289 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200290 return NULL;
291 }
292
293 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200294 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200295 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200296 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200297 return NULL;
298 }
299
300#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200301 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200302 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200303 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200304 return NULL;
305 }
306#endif
307
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200308 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200309}
310
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200311/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200312 */
313
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200314/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200315typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200316
317static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200318
319typedef struct
320{
321 PyObject_HEAD
322 long softspace;
323 long error;
324} OutputObject;
325
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200326static char *OutputAttrs[] = {
327 "softspace",
328 NULL
329};
330
331 static PyObject *
332OutputDir(PyObject *self)
333{
334 return ObjectDir(self, OutputAttrs);
335}
336
Bram Moolenaar77045652012-09-21 13:46:06 +0200337 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200338OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200339{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200340 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200341 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200342 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200343 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200344 return -1;
345 }
346
347 if (strcmp(name, "softspace") == 0)
348 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200349 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200351 return 0;
352 }
353
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200354 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200355 return -1;
356}
357
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200358/* Buffer IO, we write one whole line at a time. */
359static garray_T io_ga = {0, 0, 1, 80, NULL};
360static writefn old_fn = NULL;
361
362 static void
363PythonIO_Flush(void)
364{
365 if (old_fn != NULL && io_ga.ga_len > 0)
366 {
367 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
368 old_fn((char_u *)io_ga.ga_data);
369 }
370 io_ga.ga_len = 0;
371}
372
373 static void
374writer(writefn fn, char_u *str, PyInt n)
375{
376 char_u *ptr;
377
378 /* Flush when switching output function. */
379 if (fn != old_fn)
380 PythonIO_Flush();
381 old_fn = fn;
382
383 /* Write each NL separated line. Text after the last NL is kept for
384 * writing later. */
385 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
386 {
387 PyInt len = ptr - str;
388
389 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
390 break;
391
392 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
393 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
394 fn((char_u *)io_ga.ga_data);
395 str = ptr + 1;
396 n -= len + 1;
397 io_ga.ga_len = 0;
398 }
399
400 /* Put the remaining text into io_ga for later printing. */
401 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
402 {
403 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
404 io_ga.ga_len += (int)n;
405 }
406}
407
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200408 static int
409write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200411 Py_ssize_t len = 0;
412 char *str = NULL;
413 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200414
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200415 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
416 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417
418 Py_BEGIN_ALLOW_THREADS
419 Python_Lock_Vim();
420 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
421 Python_Release_Vim();
422 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200423 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200424
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200425 return 0;
426}
427
428 static PyObject *
429OutputWrite(OutputObject *self, PyObject *string)
430{
431 if (write_output(self, string))
432 return NULL;
433
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200434 Py_INCREF(Py_None);
435 return Py_None;
436}
437
438 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200439OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200441 PyObject *iterator;
442 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200444 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200447 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200448 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200449 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200450 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200451 Py_DECREF(iterator);
452 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200453 return NULL;
454 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200455 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200456 }
457
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200458 Py_DECREF(iterator);
459
460 /* Iterator may have finished due to an exception */
461 if (PyErr_Occurred())
462 return NULL;
463
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200464 Py_INCREF(Py_None);
465 return Py_None;
466}
467
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100468 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100469AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100470{
471 /* do nothing */
472 Py_INCREF(Py_None);
473 return Py_None;
474}
475
Bram Moolenaard4247472015-11-02 13:28:59 +0100476 static PyObject *
477AlwaysFalse(PyObject *self UNUSED)
478{
479 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100480 PyObject *ret = Py_False;
481 Py_INCREF(ret);
482 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100483}
484
485 static PyObject *
486AlwaysTrue(PyObject *self UNUSED)
487{
488 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100489 PyObject *ret = Py_True;
490 Py_INCREF(ret);
491 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100492}
493
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200494/***************/
495
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200496static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200497 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200498 {"write", (PyCFunction)OutputWrite, METH_O, ""},
499 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100500 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
501 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
502 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
503 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
504 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
505 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200506 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200507 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200508};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200509
510static OutputObject Output =
511{
512 PyObject_HEAD_INIT(&OutputType)
513 0,
514 0
515};
516
517static OutputObject Error =
518{
519 PyObject_HEAD_INIT(&OutputType)
520 0,
521 1
522};
523
524 static int
525PythonIO_Init_io(void)
526{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200527 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
528 return -1;
529 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
530 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200531
532 if (PyErr_Occurred())
533 {
534 EMSG(_("E264: Python: Error initialising I/O objects"));
535 return -1;
536 }
537
538 return 0;
539}
540
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200541typedef struct
542{
543 PyObject_HEAD
544 PyObject *module;
545} LoaderObject;
546static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200547
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200548 static void
549LoaderDestructor(LoaderObject *self)
550{
551 Py_DECREF(self->module);
552 DESTRUCTOR_FINISH(self);
553}
554
555 static PyObject *
556LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
557{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200558 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200559
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200560 Py_INCREF(ret);
561 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200562}
563
564static struct PyMethodDef LoaderMethods[] = {
565 /* name, function, calling, doc */
566 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
567 { NULL, NULL, 0, NULL}
568};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200569
570/* Check to see whether a Vim error has been reported, or a keyboard
571 * interrupt has been detected.
572 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200573
574 static void
575VimTryStart(void)
576{
577 ++trylevel;
578}
579
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200580 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200581VimTryEnd(void)
582{
583 --trylevel;
Bram Moolenaar41009372013-07-01 22:03:04 +0200584 /* Without this it stops processing all subsequent VimL commands and
585 * generates strange error messages if I e.g. try calling Test() in a
586 * cycle */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200587 did_emsg = FALSE;
588 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200589 if (got_int)
590 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100591 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100592 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100593 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200594 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200595 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200596 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100597 else if (msg_list != NULL && *msg_list != NULL)
598 {
599 int should_free;
600 char_u *msg;
601
602 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
603
604 if (msg == NULL)
605 {
606 PyErr_NoMemory();
607 return -1;
608 }
609
610 PyErr_SetVim((char *) msg);
611
612 free_global_msglist();
613
614 if (should_free)
615 vim_free(msg);
616
617 return -1;
618 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200619 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200620 return (PyErr_Occurred() ? -1 : 0);
621 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200622 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200623 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100624 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200625 return -1;
626 }
627 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200628 else
629 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200630 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200631 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200632 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200633 }
634}
635
636 static int
637VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200638{
639 if (got_int)
640 {
641 PyErr_SetNone(PyExc_KeyboardInterrupt);
642 return 1;
643 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200644 return 0;
645}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200646
647/* Vim module - Implementation
648 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200649
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200650 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200651VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200652{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200653 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200654 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200655 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200656
Bram Moolenaar389a1792013-06-23 13:00:44 +0200657 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200658 return NULL;
659
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200660 Py_BEGIN_ALLOW_THREADS
661 Python_Lock_Vim();
662
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200663 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200664 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200665 update_screen(VALID);
666
667 Python_Release_Vim();
668 Py_END_ALLOW_THREADS
669
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200670 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200671 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200672 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200673 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200674
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200675 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200676 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200677 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200678}
679
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200680/*
681 * Function to translate a typval_T into a PyObject; this will recursively
682 * translate lists/dictionaries into their Python equivalents.
683 *
684 * The depth parameter is to avoid infinite recursion, set it to 1 when
685 * you call VimToPython.
686 */
687 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200688VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200690 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200691 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200692 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693
694 /* Avoid infinite recursion */
695 if (depth > 100)
696 {
697 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200698 ret = Py_None;
699 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200700 }
701
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200702 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200703 * then and we can use it again. */
704 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
705 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
706 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200707 sprintf(ptrBuf, "%p",
708 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
709 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200710
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200711 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200712 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200713 Py_INCREF(ret);
714 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200715 }
716 }
717
718 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200719 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200720 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200721 else if (our_tv->v_type == VAR_NUMBER)
722 {
723 char buf[NUMBUFLEN];
724
725 /* For backwards compatibility numbers are stored as strings. */
726 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200727 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728 }
729# ifdef FEAT_FLOAT
730 else if (our_tv->v_type == VAR_FLOAT)
731 {
732 char buf[NUMBUFLEN];
733
734 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200735 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200736 }
737# endif
738 else if (our_tv->v_type == VAR_LIST)
739 {
740 list_T *list = our_tv->vval.v_list;
741 listitem_T *curr;
742
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200743 if (list == NULL)
744 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200745
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200746 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200747 return NULL;
748
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200749 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200750 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200751 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200752 return NULL;
753 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200754
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200755 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
756 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200757 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200758 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200759 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200760 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200762 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200763 {
764 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200765 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200766 return NULL;
767 }
768 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200769 }
770 }
771 else if (our_tv->v_type == VAR_DICT)
772 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100774 hashtab_T *ht;
775 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200776 hashitem_T *hi;
777 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100778
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200779 if (our_tv->vval.v_dict == NULL)
780 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100781 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200782
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200783 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200784 return NULL;
785
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200786 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200788 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200789 return NULL;
790 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100792 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200793 for (hi = ht->ht_array; todo > 0; ++hi)
794 {
795 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200796 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200797 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200799 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200800 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200801 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200802 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200803 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200804 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200805 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200806 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200807 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200808 Py_DECREF(newObj);
809 return NULL;
810 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200811 }
812 }
813 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100814 else if (our_tv->v_type == VAR_SPECIAL)
815 {
816 if (our_tv->vval.v_number == VVAL_FALSE)
817 {
818 ret = Py_False;
819 Py_INCREF(ret);
820 }
821 else if (our_tv->vval.v_number == VVAL_TRUE)
822 {
823 ret = Py_True;
824 Py_INCREF(ret);
825 }
826 else
827 {
828 Py_INCREF(Py_None);
829 ret = Py_None;
830 }
831 return ret;
832 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200833 else
834 {
835 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200836 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200837 }
838
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200839 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200840}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200841
842 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200843VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200844{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200845 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200846 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200847 PyObject *string;
848 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200849 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200850 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200851
Bram Moolenaar389a1792013-06-23 13:00:44 +0200852 if (!PyArg_ParseTuple(args, "O", &string))
853 return NULL;
854
855 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200856 return NULL;
857
858 Py_BEGIN_ALLOW_THREADS
859 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200860 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200861 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200862 Python_Release_Vim();
863 Py_END_ALLOW_THREADS
864
Bram Moolenaar389a1792013-06-23 13:00:44 +0200865 Py_XDECREF(todecref);
866
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200867 if (VimTryEnd())
868 return NULL;
869
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200870 if (our_tv == NULL)
871 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200872 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200873 return NULL;
874 }
875
876 /* Convert the Vim type into a Python type. Create a dictionary that's
877 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200878 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200879 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200880 else
881 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200882 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200883 Py_DECREF(lookup_dict);
884 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200885
886
887 Py_BEGIN_ALLOW_THREADS
888 Python_Lock_Vim();
889 free_tv(our_tv);
890 Python_Release_Vim();
891 Py_END_ALLOW_THREADS
892
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200893 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200894}
895
Bram Moolenaardb913952012-06-29 12:54:53 +0200896static PyObject *ConvertToPyObject(typval_T *);
897
898 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200899VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200900{
Bram Moolenaardb913952012-06-29 12:54:53 +0200901 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200902 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200903 char_u *expr;
904 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200905
Bram Moolenaar389a1792013-06-23 13:00:44 +0200906 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200907 return NULL;
908
909 Py_BEGIN_ALLOW_THREADS
910 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200911 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200912 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200913 Python_Release_Vim();
914 Py_END_ALLOW_THREADS
915
Bram Moolenaar389a1792013-06-23 13:00:44 +0200916 Py_XDECREF(todecref);
917
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200918 if (VimTryEnd())
919 return NULL;
920
Bram Moolenaardb913952012-06-29 12:54:53 +0200921 if (our_tv == NULL)
922 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200923 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200924 return NULL;
925 }
926
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200927 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200928 Py_BEGIN_ALLOW_THREADS
929 Python_Lock_Vim();
930 free_tv(our_tv);
931 Python_Release_Vim();
932 Py_END_ALLOW_THREADS
933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200934 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200935}
936
937 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200938VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200939{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200940 char_u *str;
941 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200942 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200943
Bram Moolenaar389a1792013-06-23 13:00:44 +0200944 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200945 return NULL;
946
Bram Moolenaara54bf402012-12-05 16:30:07 +0100947#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200948 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100949#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200950 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100951#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200952
953 Py_XDECREF(todecref);
954
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200955 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200956}
957
Bram Moolenaarf4258302013-06-02 18:20:17 +0200958 static PyObject *
959_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
960{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200961 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200962 PyObject *newwd;
963 PyObject *todecref;
964 char_u *new_dir;
965
Bram Moolenaard4209d22013-06-05 20:34:15 +0200966 if (_chdir == NULL)
967 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200968 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200969 return NULL;
970
971 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
972 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200973 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200974 return NULL;
975 }
976
977 if (!(new_dir = StringToChars(newwd, &todecref)))
978 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200979 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200980 Py_DECREF(newwd);
981 return NULL;
982 }
983
984 VimTryStart();
985
986 if (vim_chdir(new_dir))
987 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200988 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200989 Py_DECREF(newwd);
990 Py_XDECREF(todecref);
991
992 if (VimTryEnd())
993 return NULL;
994
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200995 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200996 return NULL;
997 }
998
999 Py_DECREF(newwd);
1000 Py_XDECREF(todecref);
1001
1002 post_chdir(FALSE);
1003
1004 if (VimTryEnd())
1005 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001006 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001007 return NULL;
1008 }
1009
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001010 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001011}
1012
1013 static PyObject *
1014VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1015{
1016 return _VimChdir(py_chdir, args, kwargs);
1017}
1018
1019 static PyObject *
1020VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1021{
1022 return _VimChdir(py_fchdir, args, kwargs);
1023}
1024
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001025typedef struct {
1026 PyObject *callable;
1027 PyObject *result;
1028} map_rtp_data;
1029
1030 static void
1031map_rtp_callback(char_u *path, void *_data)
1032{
1033 void **data = (void **) _data;
1034 PyObject *pathObject;
1035 map_rtp_data *mr_data = *((map_rtp_data **) data);
1036
Bram Moolenaar41009372013-07-01 22:03:04 +02001037 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001038 {
1039 *data = NULL;
1040 return;
1041 }
1042
1043 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1044 pathObject, NULL);
1045
1046 Py_DECREF(pathObject);
1047
1048 if (!mr_data->result || mr_data->result != Py_None)
1049 *data = NULL;
1050 else
1051 {
1052 Py_DECREF(mr_data->result);
1053 mr_data->result = NULL;
1054 }
1055}
1056
1057 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001058VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001059{
1060 map_rtp_data data;
1061
Bram Moolenaar389a1792013-06-23 13:00:44 +02001062 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001063 data.result = NULL;
1064
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001065 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001066
1067 if (data.result == NULL)
1068 {
1069 if (PyErr_Occurred())
1070 return NULL;
1071 else
1072 {
1073 Py_INCREF(Py_None);
1074 return Py_None;
1075 }
1076 }
1077 return data.result;
1078}
1079
1080/*
1081 * _vim_runtimepath_ special path implementation.
1082 */
1083
1084 static void
1085map_finder_callback(char_u *path, void *_data)
1086{
1087 void **data = (void **) _data;
1088 PyObject *list = *((PyObject **) data);
1089 PyObject *pathObject1, *pathObject2;
1090 char *pathbuf;
1091 size_t pathlen;
1092
1093 pathlen = STRLEN(path);
1094
1095#if PY_MAJOR_VERSION < 3
1096# define PY_MAIN_DIR_STRING "python2"
1097#else
1098# define PY_MAIN_DIR_STRING "python3"
1099#endif
1100#define PY_ALTERNATE_DIR_STRING "pythonx"
1101
1102#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1103 if (!(pathbuf = PyMem_New(char,
1104 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1105 {
1106 PyErr_NoMemory();
1107 *data = NULL;
1108 return;
1109 }
1110
1111 mch_memmove(pathbuf, path, pathlen + 1);
1112 add_pathsep((char_u *) pathbuf);
1113
1114 pathlen = STRLEN(pathbuf);
1115 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1116 PYTHONX_STRING_LENGTH + 1);
1117
1118 if (!(pathObject1 = PyString_FromString(pathbuf)))
1119 {
1120 *data = NULL;
1121 PyMem_Free(pathbuf);
1122 return;
1123 }
1124
1125 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1126 PYTHONX_STRING_LENGTH + 1);
1127
1128 if (!(pathObject2 = PyString_FromString(pathbuf)))
1129 {
1130 Py_DECREF(pathObject1);
1131 PyMem_Free(pathbuf);
1132 *data = NULL;
1133 return;
1134 }
1135
1136 PyMem_Free(pathbuf);
1137
1138 if (PyList_Append(list, pathObject1)
1139 || PyList_Append(list, pathObject2))
1140 *data = NULL;
1141
1142 Py_DECREF(pathObject1);
1143 Py_DECREF(pathObject2);
1144}
1145
1146 static PyObject *
1147Vim_GetPaths(PyObject *self UNUSED)
1148{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001149 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001150
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001151 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001152 return NULL;
1153
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001154 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001155
1156 if (PyErr_Occurred())
1157 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001158 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001159 return NULL;
1160 }
1161
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001162 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001163}
1164
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001165 static PyObject *
1166call_load_module(char *name, int len, PyObject *find_module_result)
1167{
1168 PyObject *fd, *pathname, *description;
1169
Bram Moolenaarc476e522013-06-23 13:46:40 +02001170 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001171 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001172 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001173 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001174 Py_TYPE_NAME(find_module_result));
1175 return NULL;
1176 }
1177 if (PyTuple_GET_SIZE(find_module_result) != 3)
1178 {
1179 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001180 N_("expected 3-tuple as imp.find_module() result, but got "
1181 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001182 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001183 return NULL;
1184 }
1185
1186 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1187 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1188 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1189 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001190 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001191 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001192 return NULL;
1193 }
1194
1195 return PyObject_CallFunction(py_load_module,
1196 "s#OOO", name, len, fd, pathname, description);
1197}
1198
1199 static PyObject *
1200find_module(char *fullname, char *tail, PyObject *new_path)
1201{
1202 PyObject *find_module_result;
1203 PyObject *module;
1204 char *dot;
1205
Bram Moolenaar41009372013-07-01 22:03:04 +02001206 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001207 {
1208 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001209 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001210 * first component
1211 */
1212 PyObject *newest_path;
1213 int partlen = (int) (dot - 1 - tail);
1214
1215 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1216 "s#O", tail, partlen, new_path)))
1217 return NULL;
1218
1219 if (!(module = call_load_module(
1220 fullname,
1221 ((int) (tail - fullname)) + partlen,
1222 find_module_result)))
1223 {
1224 Py_DECREF(find_module_result);
1225 return NULL;
1226 }
1227
1228 Py_DECREF(find_module_result);
1229
1230 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1231 {
1232 Py_DECREF(module);
1233 return NULL;
1234 }
1235
1236 Py_DECREF(module);
1237
1238 module = find_module(fullname, dot + 1, newest_path);
1239
1240 Py_DECREF(newest_path);
1241
1242 return module;
1243 }
1244 else
1245 {
1246 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1247 "sO", tail, new_path)))
1248 return NULL;
1249
1250 if (!(module = call_load_module(
1251 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001252 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001253 find_module_result)))
1254 {
1255 Py_DECREF(find_module_result);
1256 return NULL;
1257 }
1258
1259 Py_DECREF(find_module_result);
1260
1261 return module;
1262 }
1263}
1264
1265 static PyObject *
1266FinderFindModule(PyObject *self, PyObject *args)
1267{
1268 char *fullname;
1269 PyObject *module;
1270 PyObject *new_path;
1271 LoaderObject *loader;
1272
1273 if (!PyArg_ParseTuple(args, "s", &fullname))
1274 return NULL;
1275
1276 if (!(new_path = Vim_GetPaths(self)))
1277 return NULL;
1278
1279 module = find_module(fullname, fullname, new_path);
1280
1281 Py_DECREF(new_path);
1282
1283 if (!module)
1284 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001285 if (PyErr_Occurred())
1286 {
1287 if (PyErr_ExceptionMatches(PyExc_ImportError))
1288 PyErr_Clear();
1289 else
1290 return NULL;
1291 }
1292
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001293 Py_INCREF(Py_None);
1294 return Py_None;
1295 }
1296
1297 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1298 {
1299 Py_DECREF(module);
1300 return NULL;
1301 }
1302
1303 loader->module = module;
1304
1305 return (PyObject *) loader;
1306}
1307
1308 static PyObject *
1309VimPathHook(PyObject *self UNUSED, PyObject *args)
1310{
1311 char *path;
1312
1313 if (PyArg_ParseTuple(args, "s", &path)
1314 && STRCMP(path, vim_special_path) == 0)
1315 {
1316 Py_INCREF(vim_module);
1317 return vim_module;
1318 }
1319
1320 PyErr_Clear();
1321 PyErr_SetNone(PyExc_ImportError);
1322 return NULL;
1323}
1324
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001325/*
1326 * Vim module - Definitions
1327 */
1328
1329static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001330 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001331 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001332 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001333 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1334 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001335 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1336 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001337 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001338 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001339 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1340 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1341 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001342};
1343
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001344/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001345 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001346 */
1347
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001348static PyTypeObject IterType;
1349
1350typedef PyObject *(*nextfun)(void **);
1351typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001352typedef int (*traversefun)(void *, visitproc, void *);
1353typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001354
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001355/* Main purpose of this object is removing the need for do python
1356 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1357 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001358
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001359typedef struct
1360{
1361 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001362 void *cur;
1363 nextfun next;
1364 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001365 traversefun traverse;
1366 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001367} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001368
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001369 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001370IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1371 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001372{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001373 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001374
Bram Moolenaar774267b2013-05-21 20:51:59 +02001375 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001376 self->cur = start;
1377 self->next = next;
1378 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001379 self->traverse = traverse;
1380 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001381
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001382 return (PyObject *)(self);
1383}
1384
1385 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001386IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001387{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001388 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001389 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001390 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001391}
1392
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001393 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001394IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001395{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001396 if (self->traverse != NULL)
1397 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001398 else
1399 return 0;
1400}
1401
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001402/* Mac OSX defines clear() somewhere. */
1403#ifdef clear
1404# undef clear
1405#endif
1406
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001407 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001408IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001409{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001410 if (self->clear != NULL)
1411 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001412 else
1413 return 0;
1414}
1415
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001416 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001417IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001418{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001419 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001420}
1421
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001422 static PyObject *
1423IterIter(PyObject *self)
1424{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001425 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001426 return self;
1427}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001428
Bram Moolenaardb913952012-06-29 12:54:53 +02001429typedef struct pylinkedlist_S {
1430 struct pylinkedlist_S *pll_next;
1431 struct pylinkedlist_S *pll_prev;
1432 PyObject *pll_obj;
1433} pylinkedlist_T;
1434
1435static pylinkedlist_T *lastdict = NULL;
1436static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001437static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001438
1439 static void
1440pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1441{
1442 if (ref->pll_prev == NULL)
1443 {
1444 if (ref->pll_next == NULL)
1445 {
1446 *last = NULL;
1447 return;
1448 }
1449 }
1450 else
1451 ref->pll_prev->pll_next = ref->pll_next;
1452
1453 if (ref->pll_next == NULL)
1454 *last = ref->pll_prev;
1455 else
1456 ref->pll_next->pll_prev = ref->pll_prev;
1457}
1458
1459 static void
1460pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1461{
1462 if (*last == NULL)
1463 ref->pll_prev = NULL;
1464 else
1465 {
1466 (*last)->pll_next = ref;
1467 ref->pll_prev = *last;
1468 }
1469 ref->pll_next = NULL;
1470 ref->pll_obj = self;
1471 *last = ref;
1472}
1473
1474static PyTypeObject DictionaryType;
1475
1476typedef struct
1477{
1478 PyObject_HEAD
1479 dict_T *dict;
1480 pylinkedlist_T ref;
1481} DictionaryObject;
1482
Bram Moolenaara9922d62013-05-30 13:01:18 +02001483static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1484
1485#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1486
Bram Moolenaardb913952012-06-29 12:54:53 +02001487 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001488DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001489{
1490 DictionaryObject *self;
1491
Bram Moolenaara9922d62013-05-30 13:01:18 +02001492 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001493 if (self == NULL)
1494 return NULL;
1495 self->dict = dict;
1496 ++dict->dv_refcount;
1497
1498 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1499
1500 return (PyObject *)(self);
1501}
1502
Bram Moolenaara9922d62013-05-30 13:01:18 +02001503 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001504py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001505{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001506 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001507
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001508 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001509 {
1510 PyErr_NoMemory();
1511 return NULL;
1512 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001513 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001514
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001515 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001516}
1517
1518 static PyObject *
1519DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1520{
1521 DictionaryObject *self;
1522 dict_T *dict;
1523
1524 if (!(dict = py_dict_alloc()))
1525 return NULL;
1526
1527 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1528
1529 --dict->dv_refcount;
1530
1531 if (kwargs || PyTuple_Size(args))
1532 {
1533 PyObject *tmp;
1534 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1535 {
1536 Py_DECREF(self);
1537 return NULL;
1538 }
1539
1540 Py_DECREF(tmp);
1541 }
1542
1543 return (PyObject *)(self);
1544}
1545
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001546 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001547DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001548{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001549 pyll_remove(&self->ref, &lastdict);
1550 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001551
1552 DESTRUCTOR_FINISH(self);
1553}
1554
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001555static char *DictionaryAttrs[] = {
1556 "locked", "scope",
1557 NULL
1558};
1559
1560 static PyObject *
1561DictionaryDir(PyObject *self)
1562{
1563 return ObjectDir(self, DictionaryAttrs);
1564}
1565
Bram Moolenaardb913952012-06-29 12:54:53 +02001566 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001567DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001568{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001569 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001570 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001571 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001572 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001573 return -1;
1574 }
1575
1576 if (strcmp(name, "locked") == 0)
1577 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001578 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001579 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001580 PyErr_SET_STRING(PyExc_TypeError,
1581 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001582 return -1;
1583 }
1584 else
1585 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001586 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001587 if (istrue == -1)
1588 return -1;
1589 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001590 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001591 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001592 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001593 }
1594 return 0;
1595 }
1596 else
1597 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001598 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001599 return -1;
1600 }
1601}
1602
1603 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001604DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001605{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001606 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001607}
1608
Bram Moolenaara9922d62013-05-30 13:01:18 +02001609#define DICT_FLAG_HAS_DEFAULT 0x01
1610#define DICT_FLAG_POP 0x02
1611#define DICT_FLAG_NONE_DEFAULT 0x04
1612#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1613#define DICT_FLAG_RETURN_PAIR 0x10
1614
Bram Moolenaardb913952012-06-29 12:54:53 +02001615 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001616_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001617{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001618 PyObject *keyObject;
1619 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001620 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001621 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001622 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001623 dict_T *dict = self->dict;
1624 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001625 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001626
Bram Moolenaara9922d62013-05-30 13:01:18 +02001627 if (flags & DICT_FLAG_HAS_DEFAULT)
1628 {
1629 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1630 return NULL;
1631 }
1632 else
1633 keyObject = args;
1634
1635 if (flags & DICT_FLAG_RETURN_BOOL)
1636 defObject = Py_False;
1637
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001638 if (!(key = StringToChars(keyObject, &todecref)))
1639 return NULL;
1640
1641 if (*key == NUL)
1642 {
1643 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001644 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001645 return NULL;
1646 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001647
Bram Moolenaara9922d62013-05-30 13:01:18 +02001648 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001649
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001650 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001651
Bram Moolenaara9922d62013-05-30 13:01:18 +02001652 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001653 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001654 if (defObject)
1655 {
1656 Py_INCREF(defObject);
1657 return defObject;
1658 }
1659 else
1660 {
1661 PyErr_SetObject(PyExc_KeyError, keyObject);
1662 return NULL;
1663 }
1664 }
1665 else if (flags & DICT_FLAG_RETURN_BOOL)
1666 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001667 ret = Py_True;
1668 Py_INCREF(ret);
1669 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001670 }
1671
1672 di = dict_lookup(hi);
1673
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001674 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001675 return NULL;
1676
1677 if (flags & DICT_FLAG_POP)
1678 {
1679 if (dict->dv_lock)
1680 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001681 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001682 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001683 return NULL;
1684 }
1685
1686 hash_remove(&dict->dv_hashtab, hi);
1687 dictitem_free(di);
1688 }
1689
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001690 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001691}
1692
1693 static PyObject *
1694DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1695{
1696 return _DictionaryItem(self, keyObject, 0);
1697}
1698
1699 static int
1700DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1701{
1702 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001703 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001704
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001705 if (rObj == NULL)
1706 return -1;
1707
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001708 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001709
Bram Moolenaardee2e312013-06-23 16:35:47 +02001710 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001711
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001712 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001713}
1714
1715typedef struct
1716{
1717 hashitem_T *ht_array;
1718 long_u ht_used;
1719 hashtab_T *ht;
1720 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001721 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001722} dictiterinfo_T;
1723
1724 static PyObject *
1725DictionaryIterNext(dictiterinfo_T **dii)
1726{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001727 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001728
1729 if (!(*dii)->todo)
1730 return NULL;
1731
1732 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1733 (*dii)->ht->ht_used != (*dii)->ht_used)
1734 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001735 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001736 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001737 return NULL;
1738 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001739
Bram Moolenaara9922d62013-05-30 13:01:18 +02001740 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1741 ++((*dii)->hi);
1742
1743 --((*dii)->todo);
1744
Bram Moolenaar41009372013-07-01 22:03:04 +02001745 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001746 return NULL;
1747
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001748 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001749}
1750
1751 static PyObject *
1752DictionaryIter(DictionaryObject *self)
1753{
1754 dictiterinfo_T *dii;
1755 hashtab_T *ht;
1756
1757 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1758 {
1759 PyErr_NoMemory();
1760 return NULL;
1761 }
1762
1763 ht = &self->dict->dv_hashtab;
1764 dii->ht_array = ht->ht_array;
1765 dii->ht_used = ht->ht_used;
1766 dii->ht = ht;
1767 dii->hi = dii->ht_array;
1768 dii->todo = dii->ht_used;
1769
1770 return IterNew(dii,
1771 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1772 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001773}
1774
1775 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001776DictionaryAssItem(
1777 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001778{
1779 char_u *key;
1780 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001781 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001782 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001783 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001784
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001785 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001786 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001787 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001788 return -1;
1789 }
1790
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001791 if (!(key = StringToChars(keyObject, &todecref)))
1792 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001793
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001794 if (*key == NUL)
1795 {
1796 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001797 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001798 return -1;
1799 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001800
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001801 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001802
1803 if (valObject == NULL)
1804 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001805 hashitem_T *hi;
1806
Bram Moolenaardb913952012-06-29 12:54:53 +02001807 if (di == NULL)
1808 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001809 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001810 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001811 return -1;
1812 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001813 hi = hash_find(&dict->dv_hashtab, di->di_key);
1814 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001815 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001816 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001817 return 0;
1818 }
1819
1820 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001821 {
1822 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001823 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001824 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001825
1826 if (di == NULL)
1827 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001828 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001829 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001830 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001831 PyErr_NoMemory();
1832 return -1;
1833 }
1834 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001835 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001836
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001837 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001838 {
1839 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001840 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001841 RAISE_KEY_ADD_FAIL(key);
1842 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001843 return -1;
1844 }
1845 }
1846 else
1847 clear_tv(&di->di_tv);
1848
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001849 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001850
1851 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001852 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001853 return 0;
1854}
1855
Bram Moolenaara9922d62013-05-30 13:01:18 +02001856typedef PyObject *(*hi_to_py)(hashitem_T *);
1857
Bram Moolenaardb913952012-06-29 12:54:53 +02001858 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001859DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001860{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001861 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001862 long_u todo = dict->dv_hashtab.ht_used;
1863 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001864 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001865 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001866 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001867
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001868 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001869 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1870 {
1871 if (!HASHITEM_EMPTY(hi))
1872 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001873 if (!(newObj = hiconvert(hi)))
1874 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001875 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001876 return NULL;
1877 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001878 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001879 --todo;
1880 ++i;
1881 }
1882 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001883 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001884}
1885
Bram Moolenaara9922d62013-05-30 13:01:18 +02001886 static PyObject *
1887dict_key(hashitem_T *hi)
1888{
1889 return PyBytes_FromString((char *)(hi->hi_key));
1890}
1891
1892 static PyObject *
1893DictionaryListKeys(DictionaryObject *self)
1894{
1895 return DictionaryListObjects(self, dict_key);
1896}
1897
1898 static PyObject *
1899dict_val(hashitem_T *hi)
1900{
1901 dictitem_T *di;
1902
1903 di = dict_lookup(hi);
1904 return ConvertToPyObject(&di->di_tv);
1905}
1906
1907 static PyObject *
1908DictionaryListValues(DictionaryObject *self)
1909{
1910 return DictionaryListObjects(self, dict_val);
1911}
1912
1913 static PyObject *
1914dict_item(hashitem_T *hi)
1915{
1916 PyObject *keyObject;
1917 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001918 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001919
1920 if (!(keyObject = dict_key(hi)))
1921 return NULL;
1922
1923 if (!(valObject = dict_val(hi)))
1924 {
1925 Py_DECREF(keyObject);
1926 return NULL;
1927 }
1928
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001929 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001930
1931 Py_DECREF(keyObject);
1932 Py_DECREF(valObject);
1933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001934 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001935}
1936
1937 static PyObject *
1938DictionaryListItems(DictionaryObject *self)
1939{
1940 return DictionaryListObjects(self, dict_item);
1941}
1942
1943 static PyObject *
1944DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1945{
1946 dict_T *dict = self->dict;
1947
1948 if (dict->dv_lock)
1949 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001950 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001951 return NULL;
1952 }
1953
1954 if (kwargs)
1955 {
1956 typval_T tv;
1957
1958 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1959 return NULL;
1960
1961 VimTryStart();
1962 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1963 clear_tv(&tv);
1964 if (VimTryEnd())
1965 return NULL;
1966 }
1967 else
1968 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001969 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001970
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001971 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001972 return NULL;
1973
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001974 if (obj == NULL)
1975 {
1976 Py_INCREF(Py_None);
1977 return Py_None;
1978 }
1979
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001980 if (PyObject_HasAttrString(obj, "keys"))
1981 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001982 else
1983 {
1984 PyObject *iterator;
1985 PyObject *item;
1986
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001987 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001988 return NULL;
1989
1990 while ((item = PyIter_Next(iterator)))
1991 {
1992 PyObject *fast;
1993 PyObject *keyObject;
1994 PyObject *valObject;
1995 PyObject *todecref;
1996 char_u *key;
1997 dictitem_T *di;
1998
1999 if (!(fast = PySequence_Fast(item, "")))
2000 {
2001 Py_DECREF(iterator);
2002 Py_DECREF(item);
2003 return NULL;
2004 }
2005
2006 Py_DECREF(item);
2007
2008 if (PySequence_Fast_GET_SIZE(fast) != 2)
2009 {
2010 Py_DECREF(iterator);
2011 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002012 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002013 N_("expected sequence element of size 2, "
2014 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002015 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002016 return NULL;
2017 }
2018
2019 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2020
2021 if (!(key = StringToChars(keyObject, &todecref)))
2022 {
2023 Py_DECREF(iterator);
2024 Py_DECREF(fast);
2025 return NULL;
2026 }
2027
2028 di = dictitem_alloc(key);
2029
2030 Py_XDECREF(todecref);
2031
2032 if (di == NULL)
2033 {
2034 Py_DECREF(fast);
2035 Py_DECREF(iterator);
2036 PyErr_NoMemory();
2037 return NULL;
2038 }
2039 di->di_tv.v_lock = 0;
2040 di->di_tv.v_type = VAR_UNKNOWN;
2041
2042 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2043
2044 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2045 {
2046 Py_DECREF(iterator);
2047 Py_DECREF(fast);
2048 dictitem_free(di);
2049 return NULL;
2050 }
2051
2052 Py_DECREF(fast);
2053
2054 if (dict_add(dict, di) == FAIL)
2055 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002056 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002057 Py_DECREF(iterator);
2058 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002059 return NULL;
2060 }
2061 }
2062
2063 Py_DECREF(iterator);
2064
2065 /* Iterator may have finished due to an exception */
2066 if (PyErr_Occurred())
2067 return NULL;
2068 }
2069 }
2070 Py_INCREF(Py_None);
2071 return Py_None;
2072}
2073
2074 static PyObject *
2075DictionaryGet(DictionaryObject *self, PyObject *args)
2076{
2077 return _DictionaryItem(self, args,
2078 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2079}
2080
2081 static PyObject *
2082DictionaryPop(DictionaryObject *self, PyObject *args)
2083{
2084 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2085}
2086
2087 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002088DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002089{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002090 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002091 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002092 PyObject *valObject;
2093 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002094
Bram Moolenaarde71b562013-06-02 17:41:54 +02002095 if (self->dict->dv_hashtab.ht_used == 0)
2096 {
2097 PyErr_SetNone(PyExc_KeyError);
2098 return NULL;
2099 }
2100
2101 hi = self->dict->dv_hashtab.ht_array;
2102 while (HASHITEM_EMPTY(hi))
2103 ++hi;
2104
2105 di = dict_lookup(hi);
2106
2107 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002108 return NULL;
2109
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002110 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002111 {
2112 Py_DECREF(valObject);
2113 return NULL;
2114 }
2115
2116 hash_remove(&self->dict->dv_hashtab, hi);
2117 dictitem_free(di);
2118
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002119 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002120}
2121
2122 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002123DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002124{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002125 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2126}
2127
2128static PySequenceMethods DictionaryAsSeq = {
2129 0, /* sq_length */
2130 0, /* sq_concat */
2131 0, /* sq_repeat */
2132 0, /* sq_item */
2133 0, /* sq_slice */
2134 0, /* sq_ass_item */
2135 0, /* sq_ass_slice */
2136 (objobjproc) DictionaryContains, /* sq_contains */
2137 0, /* sq_inplace_concat */
2138 0, /* sq_inplace_repeat */
2139};
2140
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002141static PyMappingMethods DictionaryAsMapping = {
2142 (lenfunc) DictionaryLength,
2143 (binaryfunc) DictionaryItem,
2144 (objobjargproc) DictionaryAssItem,
2145};
2146
Bram Moolenaardb913952012-06-29 12:54:53 +02002147static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002148 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002149 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2150 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2151 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2152 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2153 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002154 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002155 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002156 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2157 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002158};
2159
2160static PyTypeObject ListType;
2161
2162typedef struct
2163{
2164 PyObject_HEAD
2165 list_T *list;
2166 pylinkedlist_T ref;
2167} ListObject;
2168
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002169#define NEW_LIST(list) ListNew(&ListType, list)
2170
Bram Moolenaardb913952012-06-29 12:54:53 +02002171 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002172ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002173{
2174 ListObject *self;
2175
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002176 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002177 if (self == NULL)
2178 return NULL;
2179 self->list = list;
2180 ++list->lv_refcount;
2181
2182 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2183
2184 return (PyObject *)(self);
2185}
2186
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002187 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002188py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002189{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002190 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002191
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002192 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002193 {
2194 PyErr_NoMemory();
2195 return NULL;
2196 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002197 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002198
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002199 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002200}
2201
2202 static int
2203list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2204{
2205 PyObject *iterator;
2206 PyObject *item;
2207 listitem_T *li;
2208
2209 if (!(iterator = PyObject_GetIter(obj)))
2210 return -1;
2211
2212 while ((item = PyIter_Next(iterator)))
2213 {
2214 if (!(li = listitem_alloc()))
2215 {
2216 PyErr_NoMemory();
2217 Py_DECREF(item);
2218 Py_DECREF(iterator);
2219 return -1;
2220 }
2221 li->li_tv.v_lock = 0;
2222 li->li_tv.v_type = VAR_UNKNOWN;
2223
2224 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2225 {
2226 Py_DECREF(item);
2227 Py_DECREF(iterator);
2228 listitem_free(li);
2229 return -1;
2230 }
2231
2232 Py_DECREF(item);
2233
2234 list_append(l, li);
2235 }
2236
2237 Py_DECREF(iterator);
2238
2239 /* Iterator may have finished due to an exception */
2240 if (PyErr_Occurred())
2241 return -1;
2242
2243 return 0;
2244}
2245
2246 static PyObject *
2247ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2248{
2249 list_T *list;
2250 PyObject *obj = NULL;
2251
2252 if (kwargs)
2253 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002254 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002255 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002256 return NULL;
2257 }
2258
2259 if (!PyArg_ParseTuple(args, "|O", &obj))
2260 return NULL;
2261
2262 if (!(list = py_list_alloc()))
2263 return NULL;
2264
2265 if (obj)
2266 {
2267 PyObject *lookup_dict;
2268
2269 if (!(lookup_dict = PyDict_New()))
2270 {
2271 list_unref(list);
2272 return NULL;
2273 }
2274
2275 if (list_py_concat(list, obj, lookup_dict) == -1)
2276 {
2277 Py_DECREF(lookup_dict);
2278 list_unref(list);
2279 return NULL;
2280 }
2281
2282 Py_DECREF(lookup_dict);
2283 }
2284
2285 return ListNew(subtype, list);
2286}
2287
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002288 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002289ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002290{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002291 pyll_remove(&self->ref, &lastlist);
2292 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002293
2294 DESTRUCTOR_FINISH(self);
2295}
2296
Bram Moolenaardb913952012-06-29 12:54:53 +02002297 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002298ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002299{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002300 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002301}
2302
2303 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002304ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002305{
2306 listitem_T *li;
2307
Bram Moolenaard6e39182013-05-21 18:30:34 +02002308 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002309 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002310 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002311 return NULL;
2312 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002313 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002314 if (li == NULL)
2315 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002316 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002317 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002318 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002319 return NULL;
2320 }
2321 return ConvertToPyObject(&li->li_tv);
2322}
2323
Bram Moolenaardb913952012-06-29 12:54:53 +02002324 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002325ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2326 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002327{
2328 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002329 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002330
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002331 if (step == 0)
2332 {
2333 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2334 return NULL;
2335 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002336
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002337 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002338 if (list == NULL)
2339 return NULL;
2340
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002341 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002342 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002343 PyObject *item;
2344
2345 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002346 if (item == NULL)
2347 {
2348 Py_DECREF(list);
2349 return NULL;
2350 }
2351
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002352 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002353 }
2354
2355 return list;
2356}
2357
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002358 static PyObject *
2359ListItem(ListObject *self, PyObject* idx)
2360{
2361#if PY_MAJOR_VERSION < 3
2362 if (PyInt_Check(idx))
2363 {
2364 long _idx = PyInt_AsLong(idx);
2365 return ListIndex(self, _idx);
2366 }
2367 else
2368#endif
2369 if (PyLong_Check(idx))
2370 {
2371 long _idx = PyLong_AsLong(idx);
2372 return ListIndex(self, _idx);
2373 }
2374 else if (PySlice_Check(idx))
2375 {
2376 Py_ssize_t start, stop, step, slicelen;
2377
Bram Moolenaar922a4662014-03-30 16:11:43 +02002378 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002379 &start, &stop, &step, &slicelen) < 0)
2380 return NULL;
2381 return ListSlice(self, start, step, slicelen);
2382 }
2383 else
2384 {
2385 RAISE_INVALID_INDEX_TYPE(idx);
2386 return NULL;
2387 }
2388}
2389
2390 static void
2391list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2392 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2393{
2394 while (numreplaced--)
2395 {
2396 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2397 listitem_remove(l, lis[slicelen + numreplaced]);
2398 }
2399 while (numadded--)
2400 {
2401 listitem_T *next;
2402
2403 next = lastaddedli->li_prev;
2404 listitem_remove(l, lastaddedli);
2405 lastaddedli = next;
2406 }
2407}
2408
2409 static int
2410ListAssSlice(ListObject *self, Py_ssize_t first,
2411 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2412{
2413 PyObject *iterator;
2414 PyObject *item;
2415 listitem_T *li;
2416 listitem_T *lastaddedli = NULL;
2417 listitem_T *next;
2418 typval_T v;
2419 list_T *l = self->list;
2420 PyInt i;
2421 PyInt j;
2422 PyInt numreplaced = 0;
2423 PyInt numadded = 0;
2424 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002425 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002426
2427 size = ListLength(self);
2428
2429 if (l->lv_lock)
2430 {
2431 RAISE_LOCKED_LIST;
2432 return -1;
2433 }
2434
2435 if (step == 0)
2436 {
2437 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2438 return -1;
2439 }
2440
2441 if (step != 1 && slicelen == 0)
2442 {
2443 /* Nothing to do. Only error out if obj has some items. */
2444 int ret = 0;
2445
2446 if (obj == NULL)
2447 return 0;
2448
2449 if (!(iterator = PyObject_GetIter(obj)))
2450 return -1;
2451
2452 if ((item = PyIter_Next(iterator)))
2453 {
2454 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002455 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002456 "to extended slice"), 0);
2457 Py_DECREF(item);
2458 ret = -1;
2459 }
2460 Py_DECREF(iterator);
2461 return ret;
2462 }
2463
2464 if (obj != NULL)
2465 /* XXX May allocate zero bytes. */
2466 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2467 {
2468 PyErr_NoMemory();
2469 return -1;
2470 }
2471
2472 if (first == size)
2473 li = NULL;
2474 else
2475 {
2476 li = list_find(l, (long) first);
2477 if (li == NULL)
2478 {
2479 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2480 (int)first);
2481 if (obj != NULL)
2482 PyMem_Free(lis);
2483 return -1;
2484 }
2485 i = slicelen;
2486 while (i-- && li != NULL)
2487 {
2488 j = step;
2489 next = li;
2490 if (step > 0)
2491 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2492 else
2493 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2494
2495 if (obj == NULL)
2496 listitem_remove(l, li);
2497 else
2498 lis[slicelen - i - 1] = li;
2499
2500 li = next;
2501 }
2502 if (li == NULL && i != -1)
2503 {
2504 PyErr_SET_VIM(N_("internal error: not enough list items"));
2505 if (obj != NULL)
2506 PyMem_Free(lis);
2507 return -1;
2508 }
2509 }
2510
2511 if (obj == NULL)
2512 return 0;
2513
2514 if (!(iterator = PyObject_GetIter(obj)))
2515 {
2516 PyMem_Free(lis);
2517 return -1;
2518 }
2519
2520 i = 0;
2521 while ((item = PyIter_Next(iterator)))
2522 {
2523 if (ConvertFromPyObject(item, &v) == -1)
2524 {
2525 Py_DECREF(iterator);
2526 Py_DECREF(item);
2527 PyMem_Free(lis);
2528 return -1;
2529 }
2530 Py_DECREF(item);
2531 if (list_insert_tv(l, &v, numreplaced < slicelen
2532 ? lis[numreplaced]
2533 : li) == FAIL)
2534 {
2535 clear_tv(&v);
2536 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2537 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2538 PyMem_Free(lis);
2539 return -1;
2540 }
2541 if (numreplaced < slicelen)
2542 {
2543 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002544 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002545 numreplaced++;
2546 }
2547 else
2548 {
2549 if (li)
2550 lastaddedli = li->li_prev;
2551 else
2552 lastaddedli = l->lv_last;
2553 numadded++;
2554 }
2555 clear_tv(&v);
2556 if (step != 1 && i >= slicelen)
2557 {
2558 Py_DECREF(iterator);
2559 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002560 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002561 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002562 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2563 PyMem_Free(lis);
2564 return -1;
2565 }
2566 ++i;
2567 }
2568 Py_DECREF(iterator);
2569
2570 if (step != 1 && i != slicelen)
2571 {
2572 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002573 N_("attempt to assign sequence of size %d to extended slice "
2574 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002575 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2576 PyMem_Free(lis);
2577 return -1;
2578 }
2579
2580 if (PyErr_Occurred())
2581 {
2582 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2583 PyMem_Free(lis);
2584 return -1;
2585 }
2586
2587 for (i = 0; i < numreplaced; i++)
2588 listitem_free(lis[i]);
2589 if (step == 1)
2590 for (i = numreplaced; i < slicelen; i++)
2591 listitem_remove(l, lis[i]);
2592
2593 PyMem_Free(lis);
2594
2595 return 0;
2596}
2597
2598 static int
2599ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2600{
2601 typval_T tv;
2602 list_T *l = self->list;
2603 listitem_T *li;
2604 Py_ssize_t length = ListLength(self);
2605
2606 if (l->lv_lock)
2607 {
2608 RAISE_LOCKED_LIST;
2609 return -1;
2610 }
2611 if (index > length || (index == length && obj == NULL))
2612 {
2613 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2614 return -1;
2615 }
2616
2617 if (obj == NULL)
2618 {
2619 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002620 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002621 clear_tv(&li->li_tv);
2622 vim_free(li);
2623 return 0;
2624 }
2625
2626 if (ConvertFromPyObject(obj, &tv) == -1)
2627 return -1;
2628
2629 if (index == length)
2630 {
2631 if (list_append_tv(l, &tv) == FAIL)
2632 {
2633 clear_tv(&tv);
2634 PyErr_SET_VIM(N_("failed to add item to list"));
2635 return -1;
2636 }
2637 }
2638 else
2639 {
2640 li = list_find(l, (long) index);
2641 clear_tv(&li->li_tv);
2642 copy_tv(&tv, &li->li_tv);
2643 clear_tv(&tv);
2644 }
2645 return 0;
2646}
2647
2648 static Py_ssize_t
2649ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2650{
2651#if PY_MAJOR_VERSION < 3
2652 if (PyInt_Check(idx))
2653 {
2654 long _idx = PyInt_AsLong(idx);
2655 return ListAssIndex(self, _idx, obj);
2656 }
2657 else
2658#endif
2659 if (PyLong_Check(idx))
2660 {
2661 long _idx = PyLong_AsLong(idx);
2662 return ListAssIndex(self, _idx, obj);
2663 }
2664 else if (PySlice_Check(idx))
2665 {
2666 Py_ssize_t start, stop, step, slicelen;
2667
Bram Moolenaar922a4662014-03-30 16:11:43 +02002668 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002669 &start, &stop, &step, &slicelen) < 0)
2670 return -1;
2671 return ListAssSlice(self, start, step, slicelen,
2672 obj);
2673 }
2674 else
2675 {
2676 RAISE_INVALID_INDEX_TYPE(idx);
2677 return -1;
2678 }
2679}
2680
2681 static PyObject *
2682ListConcatInPlace(ListObject *self, PyObject *obj)
2683{
2684 list_T *l = self->list;
2685 PyObject *lookup_dict;
2686
2687 if (l->lv_lock)
2688 {
2689 RAISE_LOCKED_LIST;
2690 return NULL;
2691 }
2692
2693 if (!(lookup_dict = PyDict_New()))
2694 return NULL;
2695
2696 if (list_py_concat(l, obj, lookup_dict) == -1)
2697 {
2698 Py_DECREF(lookup_dict);
2699 return NULL;
2700 }
2701 Py_DECREF(lookup_dict);
2702
2703 Py_INCREF(self);
2704 return (PyObject *)(self);
2705}
2706
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002707typedef struct
2708{
2709 listwatch_T lw;
2710 list_T *list;
2711} listiterinfo_T;
2712
2713 static void
2714ListIterDestruct(listiterinfo_T *lii)
2715{
2716 list_rem_watch(lii->list, &lii->lw);
2717 PyMem_Free(lii);
2718}
2719
2720 static PyObject *
2721ListIterNext(listiterinfo_T **lii)
2722{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002723 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002724
2725 if (!((*lii)->lw.lw_item))
2726 return NULL;
2727
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002728 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002729 return NULL;
2730
2731 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2732
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002733 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002734}
2735
2736 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002737ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002738{
2739 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002740 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002741
2742 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2743 {
2744 PyErr_NoMemory();
2745 return NULL;
2746 }
2747
2748 list_add_watch(l, &lii->lw);
2749 lii->lw.lw_item = l->lv_first;
2750 lii->list = l;
2751
2752 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002753 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2754 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002755}
2756
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002757static char *ListAttrs[] = {
2758 "locked",
2759 NULL
2760};
2761
2762 static PyObject *
2763ListDir(PyObject *self)
2764{
2765 return ObjectDir(self, ListAttrs);
2766}
2767
Bram Moolenaar66b79852012-09-21 14:00:35 +02002768 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002769ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002770{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002771 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002772 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002773 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002774 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002775 return -1;
2776 }
2777
2778 if (strcmp(name, "locked") == 0)
2779 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002780 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002781 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002782 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002783 return -1;
2784 }
2785 else
2786 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002787 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002788 if (istrue == -1)
2789 return -1;
2790 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002791 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002792 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002793 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002794 }
2795 return 0;
2796 }
2797 else
2798 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002799 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002800 return -1;
2801 }
2802}
2803
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002804static PySequenceMethods ListAsSeq = {
2805 (lenfunc) ListLength, /* sq_length, len(x) */
2806 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2807 0, /* RangeRepeat, sq_repeat, x*n */
2808 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2809 0, /* was_sq_slice, x[i:j] */
2810 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2811 0, /* was_sq_ass_slice, x[i:j]=v */
2812 0, /* sq_contains */
2813 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2814 0, /* sq_inplace_repeat */
2815};
2816
2817static PyMappingMethods ListAsMapping = {
2818 /* mp_length */ (lenfunc) ListLength,
2819 /* mp_subscript */ (binaryfunc) ListItem,
2820 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2821};
2822
Bram Moolenaardb913952012-06-29 12:54:53 +02002823static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002824 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2825 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2826 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002827};
2828
2829typedef struct
2830{
2831 PyObject_HEAD
2832 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002833 int argc;
2834 typval_T *argv;
2835 dict_T *self;
2836 pylinkedlist_T ref;
Bram Moolenaardb913952012-06-29 12:54:53 +02002837} FunctionObject;
2838
2839static PyTypeObject FunctionType;
2840
Bram Moolenaar8110a092016-04-14 15:56:09 +02002841#define NEW_FUNCTION(name, argc, argv, self) \
2842 FunctionNew(&FunctionType, name, argc, argv, self)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002843
Bram Moolenaardb913952012-06-29 12:54:53 +02002844 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002845FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
2846 dict_T *selfdict)
Bram Moolenaardb913952012-06-29 12:54:53 +02002847{
2848 FunctionObject *self;
2849
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002850 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2851
Bram Moolenaardb913952012-06-29 12:54:53 +02002852 if (self == NULL)
2853 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002854
2855 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002856 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002857 if (!translated_function_exists(name))
2858 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002859 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002860 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002861 return NULL;
2862 }
2863 self->name = vim_strsave(name);
2864 func_ref(self->name);
2865 }
2866 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002867 if ((self->name = get_expanded_name(name,
2868 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2869 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002870 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002871 PyErr_FORMAT(PyExc_ValueError,
2872 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002873 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002874 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002875
Bram Moolenaar8110a092016-04-14 15:56:09 +02002876 self->argc = argc;
2877 self->argv = argv;
2878 self->self = selfdict;
2879
2880 if (self->argv || self->self)
2881 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2882
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002883 return (PyObject *)(self);
2884}
2885
2886 static PyObject *
2887FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2888{
2889 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002890 PyObject *selfdictObject;
2891 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002892 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002893 typval_T selfdicttv;
2894 typval_T argstv;
2895 list_T *argslist = NULL;
2896 dict_T *selfdict = NULL;
2897 int argc = 0;
2898 typval_T *argv = NULL;
2899 typval_T *curtv;
2900 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002901
Bram Moolenaar8110a092016-04-14 15:56:09 +02002902 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002903 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02002904 selfdictObject = PyDict_GetItemString(kwargs, "self");
2905 if (selfdictObject != NULL)
2906 {
2907 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
2908 return NULL;
2909 selfdict = selfdicttv.vval.v_dict;
2910 }
2911 argsObject = PyDict_GetItemString(kwargs, "args");
2912 if (argsObject != NULL)
2913 {
2914 if (ConvertFromPySequence(argsObject, &argstv) == -1)
2915 {
2916 dict_unref(selfdict);
2917 return NULL;
2918 }
2919 argslist = argstv.vval.v_list;
2920
2921 argc = argslist->lv_len;
2922 if (argc != 0)
2923 {
2924 argv = PyMem_New(typval_T, (size_t) argc);
2925 curtv = argv;
2926 for (li = argslist->lv_first; li != NULL; li = li->li_next)
2927 copy_tv(&li->li_tv, curtv++);
2928 }
2929 list_unref(argslist);
2930 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002931 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002932
Bram Moolenaar389a1792013-06-23 13:00:44 +02002933 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02002934 {
2935 dict_unref(selfdict);
2936 while (argc--)
2937 clear_tv(&argv[argc]);
2938 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002939 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002940 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002941
Bram Moolenaar8110a092016-04-14 15:56:09 +02002942 self = FunctionNew(subtype, name, argc, argv, selfdict);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002943
Bram Moolenaar389a1792013-06-23 13:00:44 +02002944 PyMem_Free(name);
2945
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002946 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002947}
2948
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002949 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002950FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002951{
Bram Moolenaar8110a092016-04-14 15:56:09 +02002952 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002953 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002954 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002955 for (i = 0; i < self->argc; ++i)
2956 clear_tv(&self->argv[i]);
2957 PyMem_Free(self->argv);
2958 dict_unref(self->self);
2959 if (self->argv || self->self)
2960 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002961
2962 DESTRUCTOR_FINISH(self);
2963}
2964
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002965static char *FunctionAttrs[] = {
Bram Moolenaar8110a092016-04-14 15:56:09 +02002966 "softspace", "args", "self",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002967 NULL
2968};
2969
2970 static PyObject *
2971FunctionDir(PyObject *self)
2972{
2973 return ObjectDir(self, FunctionAttrs);
2974}
2975
Bram Moolenaardb913952012-06-29 12:54:53 +02002976 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002977FunctionAttr(FunctionObject *self, char *name)
2978{
2979 list_T *list;
2980 int i;
2981 if (strcmp(name, "name") == 0)
2982 return PyString_FromString((char *)(self->name));
2983 else if (strcmp(name, "args") == 0)
2984 {
2985 if (self->argv == NULL)
2986 return AlwaysNone(NULL);
2987 list = list_alloc();
2988 for (i = 0; i < self->argc; ++i)
2989 list_append_tv(list, &self->argv[i]);
2990 return NEW_LIST(list);
2991 }
2992 else if (strcmp(name, "self") == 0)
2993 return self->self == NULL
2994 ? AlwaysNone(NULL)
2995 : NEW_DICTIONARY(self->self);
2996 else if (strcmp(name, "__members__") == 0)
2997 return ObjectDir(NULL, FunctionAttrs);
2998 return NULL;
2999}
3000
3001/* Populate partial_T given function object.
3002 *
3003 * "exported" should be set to true when it is needed to construct a partial
3004 * that may be stored in a variable (i.e. may be freed by Vim).
3005 */
3006 static void
3007set_partial(FunctionObject *self, partial_T *pt, int exported)
3008{
3009 typval_T *curtv;
3010 int i;
3011
3012 pt->pt_name = self->name;
3013 if (self->argv)
3014 {
3015 pt->pt_argc = self->argc;
3016 if (exported)
3017 {
3018 pt->pt_argv = (typval_T *)alloc_clear(
3019 sizeof(typval_T) * self->argc);
3020 for (i = 0; i < pt->pt_argc; ++i)
3021 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3022 }
3023 else
3024 pt->pt_argv = self->argv;
3025 }
3026 else
3027 {
3028 pt->pt_argc = 0;
3029 pt->pt_argv = NULL;
3030 }
3031 pt->pt_dict = self->self;
3032 if (exported && self->self)
3033 ++pt->pt_dict->dv_refcount;
3034 if (exported)
3035 pt->pt_name = vim_strsave(pt->pt_name);
3036 pt->pt_refcount = 1;
3037}
3038
3039 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003040FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003041{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003042 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003043 typval_T args;
3044 typval_T selfdicttv;
3045 typval_T rettv;
3046 dict_T *selfdict = NULL;
3047 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003048 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003049 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003050 partial_T pt;
3051 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003052
Bram Moolenaar8110a092016-04-14 15:56:09 +02003053 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003054 return NULL;
3055
3056 if (kwargs != NULL)
3057 {
3058 selfdictObject = PyDict_GetItemString(kwargs, "self");
3059 if (selfdictObject != NULL)
3060 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003061 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003062 {
3063 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003064 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003065 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003066 selfdict = selfdicttv.vval.v_dict;
3067 }
3068 }
3069
Bram Moolenaar8110a092016-04-14 15:56:09 +02003070 if (self->argv || self->self)
3071 {
3072 set_partial(self, &pt, FALSE);
3073 pt_ptr = &pt;
3074 }
3075
Bram Moolenaar71700b82013-05-15 17:49:05 +02003076 Py_BEGIN_ALLOW_THREADS
3077 Python_Lock_Vim();
3078
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003079 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003080 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003081
3082 Python_Release_Vim();
3083 Py_END_ALLOW_THREADS
3084
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003085 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003086 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003087 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003088 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003089 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003090 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003091 }
3092 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003093 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003094
Bram Moolenaardb913952012-06-29 12:54:53 +02003095 clear_tv(&args);
3096 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003097 if (selfdict != NULL)
3098 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003099
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003100 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003101}
3102
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003103 static PyObject *
3104FunctionRepr(FunctionObject *self)
3105{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003106 PyObject *ret;
3107 garray_T repr_ga;
3108 int i;
3109 char_u *tofree = NULL;
3110 typval_T tv;
3111 char_u numbuf[NUMBUFLEN];
3112
3113 ga_init2(&repr_ga, (int)sizeof(char), 70);
3114 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3115 if (self->name)
3116 ga_concat(&repr_ga, self->name);
3117 else
3118 ga_concat(&repr_ga, (char_u *)"<NULL>");
3119 ga_append(&repr_ga, '\'');
3120 if (self->argv)
3121 {
3122 ga_concat(&repr_ga, (char_u *)", args=[");
3123 ++emsg_silent;
3124 for (i = 0; i < self->argc; i++)
3125 {
3126 if (i != 0)
3127 ga_concat(&repr_ga, (char_u *)", ");
3128 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3129 get_copyID()));
3130 vim_free(tofree);
3131 }
3132 --emsg_silent;
3133 ga_append(&repr_ga, ']');
3134 }
3135 if (self->self)
3136 {
3137 ga_concat(&repr_ga, (char_u *)", self=");
3138 tv.v_type = VAR_DICT;
3139 tv.vval.v_dict = self->self;
3140 ++emsg_silent;
3141 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3142 --emsg_silent;
3143 vim_free(tofree);
3144 }
3145 ga_append(&repr_ga, '>');
3146 ret = PyString_FromString((char *)repr_ga.ga_data);
3147 ga_clear(&repr_ga);
3148 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003149}
3150
Bram Moolenaardb913952012-06-29 12:54:53 +02003151static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003152 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3153 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003154};
3155
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003156/*
3157 * Options object
3158 */
3159
3160static PyTypeObject OptionsType;
3161
3162typedef int (*checkfun)(void *);
3163
3164typedef struct
3165{
3166 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003167 int opt_type;
3168 void *from;
3169 checkfun Check;
3170 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003171} OptionsObject;
3172
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003173 static int
3174dummy_check(void *arg UNUSED)
3175{
3176 return 0;
3177}
3178
3179 static PyObject *
3180OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3181{
3182 OptionsObject *self;
3183
Bram Moolenaar774267b2013-05-21 20:51:59 +02003184 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003185 if (self == NULL)
3186 return NULL;
3187
3188 self->opt_type = opt_type;
3189 self->from = from;
3190 self->Check = Check;
3191 self->fromObj = fromObj;
3192 if (fromObj)
3193 Py_INCREF(fromObj);
3194
3195 return (PyObject *)(self);
3196}
3197
3198 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003199OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003200{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003201 PyObject_GC_UnTrack((void *)(self));
3202 Py_XDECREF(self->fromObj);
3203 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003204}
3205
3206 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003207OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003208{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003209 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003210 return 0;
3211}
3212
3213 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003214OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003215{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003216 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003217 return 0;
3218}
3219
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003220 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003221OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003222{
3223 char_u *key;
3224 int flags;
3225 long numval;
3226 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003227 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003228
Bram Moolenaard6e39182013-05-21 18:30:34 +02003229 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003230 return NULL;
3231
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003232 if (!(key = StringToChars(keyObject, &todecref)))
3233 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003234
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003235 if (*key == NUL)
3236 {
3237 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003238 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003239 return NULL;
3240 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003241
3242 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003243 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003244
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003245 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003246
3247 if (flags == 0)
3248 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003249 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003250 return NULL;
3251 }
3252
3253 if (flags & SOPT_UNSET)
3254 {
3255 Py_INCREF(Py_None);
3256 return Py_None;
3257 }
3258 else if (flags & SOPT_BOOL)
3259 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003260 PyObject *ret;
3261 ret = numval ? Py_True : Py_False;
3262 Py_INCREF(ret);
3263 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003264 }
3265 else if (flags & SOPT_NUM)
3266 return PyInt_FromLong(numval);
3267 else if (flags & SOPT_STRING)
3268 {
3269 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003270 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003271 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003272 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003273 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003274 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003275 else
3276 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003277 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003278 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003279 return NULL;
3280 }
3281 }
3282 else
3283 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003284 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003285 return NULL;
3286 }
3287}
3288
3289 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003290OptionsContains(OptionsObject *self, PyObject *keyObject)
3291{
3292 char_u *key;
3293 PyObject *todecref;
3294
3295 if (!(key = StringToChars(keyObject, &todecref)))
3296 return -1;
3297
3298 if (*key == NUL)
3299 {
3300 Py_XDECREF(todecref);
3301 return 0;
3302 }
3303
3304 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3305 {
3306 Py_XDECREF(todecref);
3307 return 1;
3308 }
3309 else
3310 {
3311 Py_XDECREF(todecref);
3312 return 0;
3313 }
3314}
3315
3316typedef struct
3317{
3318 void *lastoption;
3319 int opt_type;
3320} optiterinfo_T;
3321
3322 static PyObject *
3323OptionsIterNext(optiterinfo_T **oii)
3324{
3325 char_u *name;
3326
3327 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3328 return PyString_FromString((char *)name);
3329
3330 return NULL;
3331}
3332
3333 static PyObject *
3334OptionsIter(OptionsObject *self)
3335{
3336 optiterinfo_T *oii;
3337
3338 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3339 {
3340 PyErr_NoMemory();
3341 return NULL;
3342 }
3343
3344 oii->opt_type = self->opt_type;
3345 oii->lastoption = NULL;
3346
3347 return IterNew(oii,
3348 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3349 NULL, NULL);
3350}
3351
3352 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003353set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003354{
3355 char_u *errmsg;
3356
3357 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3358 {
3359 if (VimTryEnd())
3360 return FAIL;
3361 PyErr_SetVim((char *)errmsg);
3362 return FAIL;
3363 }
3364 return OK;
3365}
3366
3367 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003368set_option_value_for(
3369 char_u *key,
3370 int numval,
3371 char_u *stringval,
3372 int opt_flags,
3373 int opt_type,
3374 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003375{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003376 win_T *save_curwin = NULL;
3377 tabpage_T *save_curtab = NULL;
3378 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003379 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003380
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003381 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003382 switch (opt_type)
3383 {
3384 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003385 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003386 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003387 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003388 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003389 if (VimTryEnd())
3390 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003391 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003392 return -1;
3393 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003394 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3395 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003396 break;
3397 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003398 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003399 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003400 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003401 break;
3402 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003403 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003404 break;
3405 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003406 if (set_ret == FAIL)
3407 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003408 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003409}
3410
3411 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003412OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003413{
3414 char_u *key;
3415 int flags;
3416 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003417 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003418 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003419
Bram Moolenaard6e39182013-05-21 18:30:34 +02003420 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003421 return -1;
3422
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003423 if (!(key = StringToChars(keyObject, &todecref)))
3424 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003425
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003426 if (*key == NUL)
3427 {
3428 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003429 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003430 return -1;
3431 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003432
3433 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003434 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003435
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003436 if (flags == 0)
3437 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003438 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003439 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003440 return -1;
3441 }
3442
3443 if (valObject == NULL)
3444 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003445 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003446 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003447 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003448 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003449 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003450 return -1;
3451 }
3452 else if (!(flags & SOPT_GLOBAL))
3453 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003454 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003455 N_("unable to unset option %s "
3456 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003457 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003458 return -1;
3459 }
3460 else
3461 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003462 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003463 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003464 return 0;
3465 }
3466 }
3467
Bram Moolenaard6e39182013-05-21 18:30:34 +02003468 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003469
3470 if (flags & SOPT_BOOL)
3471 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003472 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003473
Bram Moolenaarb983f752013-05-15 16:11:50 +02003474 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003475 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003476 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003477 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003478 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003479 }
3480 else if (flags & SOPT_NUM)
3481 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003482 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003483
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003484 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003485 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003486 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003487 return -1;
3488 }
3489
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003490 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003491 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003492 }
3493 else
3494 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003495 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003496 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003497
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003498 if ((val = StringToChars(valObject, &todecref2)))
3499 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003500 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003501 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003502 Py_XDECREF(todecref2);
3503 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003504 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003505 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003506 }
3507
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003508 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003509
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003510 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003511}
3512
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003513static PySequenceMethods OptionsAsSeq = {
3514 0, /* sq_length */
3515 0, /* sq_concat */
3516 0, /* sq_repeat */
3517 0, /* sq_item */
3518 0, /* sq_slice */
3519 0, /* sq_ass_item */
3520 0, /* sq_ass_slice */
3521 (objobjproc) OptionsContains, /* sq_contains */
3522 0, /* sq_inplace_concat */
3523 0, /* sq_inplace_repeat */
3524};
3525
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003526static PyMappingMethods OptionsAsMapping = {
3527 (lenfunc) NULL,
3528 (binaryfunc) OptionsItem,
3529 (objobjargproc) OptionsAssItem,
3530};
3531
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003532/* Tabpage object
3533 */
3534
3535typedef struct
3536{
3537 PyObject_HEAD
3538 tabpage_T *tab;
3539} TabPageObject;
3540
3541static PyObject *WinListNew(TabPageObject *tabObject);
3542
3543static PyTypeObject TabPageType;
3544
3545 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003546CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003547{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003548 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003549 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003550 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003551 return -1;
3552 }
3553
3554 return 0;
3555}
3556
3557 static PyObject *
3558TabPageNew(tabpage_T *tab)
3559{
3560 TabPageObject *self;
3561
3562 if (TAB_PYTHON_REF(tab))
3563 {
3564 self = TAB_PYTHON_REF(tab);
3565 Py_INCREF(self);
3566 }
3567 else
3568 {
3569 self = PyObject_NEW(TabPageObject, &TabPageType);
3570 if (self == NULL)
3571 return NULL;
3572 self->tab = tab;
3573 TAB_PYTHON_REF(tab) = self;
3574 }
3575
3576 return (PyObject *)(self);
3577}
3578
3579 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003580TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003581{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003582 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3583 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003584
3585 DESTRUCTOR_FINISH(self);
3586}
3587
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003588static char *TabPageAttrs[] = {
3589 "windows", "number", "vars", "window", "valid",
3590 NULL
3591};
3592
3593 static PyObject *
3594TabPageDir(PyObject *self)
3595{
3596 return ObjectDir(self, TabPageAttrs);
3597}
3598
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003599 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003600TabPageAttrValid(TabPageObject *self, char *name)
3601{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003602 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003603
3604 if (strcmp(name, "valid") != 0)
3605 return NULL;
3606
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003607 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3608 Py_INCREF(ret);
3609 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003610}
3611
3612 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003613TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003614{
3615 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003616 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003617 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003618 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003619 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003620 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003621 else if (strcmp(name, "window") == 0)
3622 {
3623 /* For current tab window.c does not bother to set or update tp_curwin
3624 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003625 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003626 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003627 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003628 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003629 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003630 else if (strcmp(name, "__members__") == 0)
3631 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003632 return NULL;
3633}
3634
3635 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003636TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003637{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003638 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003639 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003640 else
3641 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003642 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003643
3644 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003645 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3646 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003647 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003648 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003649 }
3650}
3651
3652static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003653 /* name, function, calling, documentation */
3654 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3655 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003656};
3657
3658/*
3659 * Window list object
3660 */
3661
3662static PyTypeObject TabListType;
3663static PySequenceMethods TabListAsSeq;
3664
3665typedef struct
3666{
3667 PyObject_HEAD
3668} TabListObject;
3669
3670 static PyInt
3671TabListLength(PyObject *self UNUSED)
3672{
3673 tabpage_T *tp = first_tabpage;
3674 PyInt n = 0;
3675
3676 while (tp != NULL)
3677 {
3678 ++n;
3679 tp = tp->tp_next;
3680 }
3681
3682 return n;
3683}
3684
3685 static PyObject *
3686TabListItem(PyObject *self UNUSED, PyInt n)
3687{
3688 tabpage_T *tp;
3689
3690 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3691 if (n == 0)
3692 return TabPageNew(tp);
3693
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003694 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003695 return NULL;
3696}
3697
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003698/*
3699 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003700 */
3701
3702typedef struct
3703{
3704 PyObject_HEAD
3705 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003706 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003707} WindowObject;
3708
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003709static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003710
3711 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003712CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003713{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003714 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003715 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003716 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003717 return -1;
3718 }
3719
3720 return 0;
3721}
3722
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003723 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003724WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003725{
3726 /* We need to handle deletion of windows underneath us.
3727 * If we add a "w_python*_ref" field to the win_T structure,
3728 * then we can get at it in win_free() in vim. We then
3729 * need to create only ONE Python object per window - if
3730 * we try to create a second, just INCREF the existing one
3731 * and return it. The (single) Python object referring to
3732 * the window is stored in "w_python*_ref".
3733 * On a win_free() we set the Python object's win_T* field
3734 * to an invalid value. We trap all uses of a window
3735 * object, and reject them if the win_T* field is invalid.
3736 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003737 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003738 * w_python_ref and w_python3_ref fields respectively.
3739 */
3740
3741 WindowObject *self;
3742
3743 if (WIN_PYTHON_REF(win))
3744 {
3745 self = WIN_PYTHON_REF(win);
3746 Py_INCREF(self);
3747 }
3748 else
3749 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003750 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003751 if (self == NULL)
3752 return NULL;
3753 self->win = win;
3754 WIN_PYTHON_REF(win) = self;
3755 }
3756
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003757 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3758
Bram Moolenaar971db462013-05-12 18:44:48 +02003759 return (PyObject *)(self);
3760}
3761
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003762 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003763WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003764{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003765 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003766 if (self->win && self->win != INVALID_WINDOW_VALUE)
3767 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003768 Py_XDECREF(((PyObject *)(self->tabObject)));
3769 PyObject_GC_Del((void *)(self));
3770}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003771
Bram Moolenaar774267b2013-05-21 20:51:59 +02003772 static int
3773WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3774{
3775 Py_VISIT(((PyObject *)(self->tabObject)));
3776 return 0;
3777}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003778
Bram Moolenaar774267b2013-05-21 20:51:59 +02003779 static int
3780WindowClear(WindowObject *self)
3781{
3782 Py_CLEAR(self->tabObject);
3783 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003784}
3785
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003786 static win_T *
3787get_firstwin(TabPageObject *tabObject)
3788{
3789 if (tabObject)
3790 {
3791 if (CheckTabPage(tabObject))
3792 return NULL;
3793 /* For current tab window.c does not bother to set or update tp_firstwin
3794 */
3795 else if (tabObject->tab == curtab)
3796 return firstwin;
3797 else
3798 return tabObject->tab->tp_firstwin;
3799 }
3800 else
3801 return firstwin;
3802}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003803static char *WindowAttrs[] = {
3804 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3805 "tabpage", "valid",
3806 NULL
3807};
3808
3809 static PyObject *
3810WindowDir(PyObject *self)
3811{
3812 return ObjectDir(self, WindowAttrs);
3813}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003814
Bram Moolenaar971db462013-05-12 18:44:48 +02003815 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003816WindowAttrValid(WindowObject *self, char *name)
3817{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003818 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003819
3820 if (strcmp(name, "valid") != 0)
3821 return NULL;
3822
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003823 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3824 Py_INCREF(ret);
3825 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003826}
3827
3828 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003829WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003830{
3831 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003832 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003833 else if (strcmp(name, "cursor") == 0)
3834 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003835 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003836
3837 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3838 }
3839 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003840 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003841#ifdef FEAT_WINDOWS
3842 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003843 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003844 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003845 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003846 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003847 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003848#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003849 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003850 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003851 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003852 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3853 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003854 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003855 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003856 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003857 return NULL;
3858 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003859 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003860 }
3861 else if (strcmp(name, "tabpage") == 0)
3862 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003863 Py_INCREF(self->tabObject);
3864 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003865 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003866 else if (strcmp(name, "__members__") == 0)
3867 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003868 else
3869 return NULL;
3870}
3871
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003872 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003873WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003874{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003875 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003876 return -1;
3877
3878 if (strcmp(name, "buffer") == 0)
3879 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003880 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003881 return -1;
3882 }
3883 else if (strcmp(name, "cursor") == 0)
3884 {
3885 long lnum;
3886 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003887
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003888 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003889 return -1;
3890
Bram Moolenaard6e39182013-05-21 18:30:34 +02003891 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003892 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003893 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003894 return -1;
3895 }
3896
3897 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003898 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003899 return -1;
3900
Bram Moolenaard6e39182013-05-21 18:30:34 +02003901 self->win->w_cursor.lnum = lnum;
3902 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003903#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003904 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003905#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003906 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003907 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003908
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003909 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003910 return 0;
3911 }
3912 else if (strcmp(name, "height") == 0)
3913 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003914 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003915 win_T *savewin;
3916
Bram Moolenaardee2e312013-06-23 16:35:47 +02003917 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003918 return -1;
3919
3920#ifdef FEAT_GUI
3921 need_mouse_correct = TRUE;
3922#endif
3923 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003924 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003925
3926 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003927 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003928 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003929 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003930 return -1;
3931
3932 return 0;
3933 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003934#ifdef FEAT_WINDOWS
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003935 else if (strcmp(name, "width") == 0)
3936 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003937 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003938 win_T *savewin;
3939
Bram Moolenaardee2e312013-06-23 16:35:47 +02003940 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003941 return -1;
3942
3943#ifdef FEAT_GUI
3944 need_mouse_correct = TRUE;
3945#endif
3946 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003947 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003948
3949 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003950 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003951 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003952 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003953 return -1;
3954
3955 return 0;
3956 }
3957#endif
3958 else
3959 {
3960 PyErr_SetString(PyExc_AttributeError, name);
3961 return -1;
3962 }
3963}
3964
3965 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003966WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003967{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003968 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003969 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003970 else
3971 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003972 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003973
Bram Moolenaar6d216452013-05-12 19:00:41 +02003974 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003975 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003976 (self));
3977 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003978 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003979 }
3980}
3981
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003982static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003983 /* name, function, calling, documentation */
3984 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3985 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003986};
3987
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003988/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003989 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003990 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003991
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003992static PyTypeObject WinListType;
3993static PySequenceMethods WinListAsSeq;
3994
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003995typedef struct
3996{
3997 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003998 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003999} WinListObject;
4000
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004001 static PyObject *
4002WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004003{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004004 WinListObject *self;
4005
4006 self = PyObject_NEW(WinListObject, &WinListType);
4007 self->tabObject = tabObject;
4008 Py_INCREF(tabObject);
4009
4010 return (PyObject *)(self);
4011}
4012
4013 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004014WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004015{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004016 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004017
4018 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004019 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004020 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004021 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004022
4023 DESTRUCTOR_FINISH(self);
4024}
4025
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004026 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004027WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004028{
4029 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004030 PyInt n = 0;
4031
Bram Moolenaard6e39182013-05-21 18:30:34 +02004032 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004033 return -1;
4034
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035 while (w != NULL)
4036 {
4037 ++n;
4038 w = W_NEXT(w);
4039 }
4040
4041 return n;
4042}
4043
4044 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004045WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004046{
4047 win_T *w;
4048
Bram Moolenaard6e39182013-05-21 18:30:34 +02004049 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004050 return NULL;
4051
4052 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004053 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004054 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004055
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004056 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004057 return NULL;
4058}
4059
4060/* Convert a Python string into a Vim line.
4061 *
4062 * The result is in allocated memory. All internal nulls are replaced by
4063 * newline characters. It is an error for the string to contain newline
4064 * characters.
4065 *
4066 * On errors, the Python exception data is set, and NULL is returned.
4067 */
4068 static char *
4069StringToLine(PyObject *obj)
4070{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004071 char *str;
4072 char *save;
4073 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004074 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004075 PyInt i;
4076 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004077
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004078 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004079 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004080 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4081 || str == NULL)
4082 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004083 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004084 else if (PyUnicode_Check(obj))
4085 {
4086 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4087 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088
Bram Moolenaardaa27022013-06-24 22:33:30 +02004089 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004090 || str == NULL)
4091 {
4092 Py_DECREF(bytes);
4093 return NULL;
4094 }
4095 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004096 else
4097 {
4098#if PY_MAJOR_VERSION < 3
4099 PyErr_FORMAT(PyExc_TypeError,
4100 N_("expected str() or unicode() instance, but got %s"),
4101 Py_TYPE_NAME(obj));
4102#else
4103 PyErr_FORMAT(PyExc_TypeError,
4104 N_("expected bytes() or str() instance, but got %s"),
4105 Py_TYPE_NAME(obj));
4106#endif
4107 return NULL;
4108 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109
4110 /*
4111 * Error checking: String must not contain newlines, as we
4112 * are replacing a single line, and we must replace it with
4113 * a single line.
4114 * A trailing newline is removed, so that append(f.readlines()) works.
4115 */
4116 p = memchr(str, '\n', len);
4117 if (p != NULL)
4118 {
4119 if (p == str + len - 1)
4120 --len;
4121 else
4122 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004123 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004124 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004125 return NULL;
4126 }
4127 }
4128
4129 /* Create a copy of the string, with internal nulls replaced by
4130 * newline characters, as is the vim convention.
4131 */
4132 save = (char *)alloc((unsigned)(len+1));
4133 if (save == NULL)
4134 {
4135 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004136 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004137 return NULL;
4138 }
4139
4140 for (i = 0; i < len; ++i)
4141 {
4142 if (str[i] == '\0')
4143 save[i] = '\n';
4144 else
4145 save[i] = str[i];
4146 }
4147
4148 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004149 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004150
4151 return save;
4152}
4153
4154/* Get a line from the specified buffer. The line number is
4155 * in Vim format (1-based). The line is returned as a Python
4156 * string object.
4157 */
4158 static PyObject *
4159GetBufferLine(buf_T *buf, PyInt n)
4160{
4161 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4162}
4163
4164
4165/* Get a list of lines from the specified buffer. The line numbers
4166 * are in Vim format (1-based). The range is from lo up to, but not
4167 * including, hi. The list is returned as a Python list of string objects.
4168 */
4169 static PyObject *
4170GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4171{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004172 PyInt i;
4173 PyInt n = hi - lo;
4174 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004175
4176 if (list == NULL)
4177 return NULL;
4178
4179 for (i = 0; i < n; ++i)
4180 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004181 PyObject *string = LineToString(
4182 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004183
4184 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004185 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004186 {
4187 Py_DECREF(list);
4188 return NULL;
4189 }
4190
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004191 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004192 }
4193
4194 /* The ownership of the Python list is passed to the caller (ie,
4195 * the caller should Py_DECREF() the object when it is finished
4196 * with it).
4197 */
4198
4199 return list;
4200}
4201
4202/*
4203 * Check if deleting lines made the cursor position invalid.
4204 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4205 * deleted).
4206 */
4207 static void
4208py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4209{
4210 if (curwin->w_cursor.lnum >= lo)
4211 {
4212 /* Adjust the cursor position if it's in/after the changed
4213 * lines. */
4214 if (curwin->w_cursor.lnum >= hi)
4215 {
4216 curwin->w_cursor.lnum += extra;
4217 check_cursor_col();
4218 }
4219 else if (extra < 0)
4220 {
4221 curwin->w_cursor.lnum = lo;
4222 check_cursor();
4223 }
4224 else
4225 check_cursor_col();
4226 changed_cline_bef_curs();
4227 }
4228 invalidate_botline();
4229}
4230
Bram Moolenaar19e60942011-06-19 00:27:51 +02004231/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004232 * Find a window that contains "buf" and switch to it.
4233 * If there is no such window, use the current window and change "curbuf".
4234 * Caller must initialize save_curbuf to NULL.
4235 * restore_win_for_buf() MUST be called later!
4236 */
4237 static void
4238switch_to_win_for_buf(
4239 buf_T *buf,
4240 win_T **save_curwinp,
4241 tabpage_T **save_curtabp,
4242 buf_T **save_curbufp)
4243{
4244 win_T *wp;
4245 tabpage_T *tp;
4246
Bram Moolenaarae38d052014-12-17 14:46:09 +01004247 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004248 switch_buffer(save_curbufp, buf);
Bram Moolenaarae38d052014-12-17 14:46:09 +01004249 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4250 {
4251 restore_win(*save_curwinp, *save_curtabp, TRUE);
4252 switch_buffer(save_curbufp, buf);
4253 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004254}
4255
4256 static void
4257restore_win_for_buf(
4258 win_T *save_curwin,
4259 tabpage_T *save_curtab,
4260 buf_T *save_curbuf)
4261{
4262 if (save_curbuf == NULL)
4263 restore_win(save_curwin, save_curtab, TRUE);
4264 else
4265 restore_buffer(save_curbuf);
4266}
4267
4268/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004269 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004270 * in Vim format (1-based). The replacement line is given as
4271 * a Python string object. The object is checked for validity
4272 * and correct format. Errors are returned as a value of FAIL.
4273 * The return value is OK on success.
4274 * If OK is returned and len_change is not NULL, *len_change
4275 * is set to the change in the buffer length.
4276 */
4277 static int
4278SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4279{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004280 buf_T *save_curbuf = NULL;
4281 win_T *save_curwin = NULL;
4282 tabpage_T *save_curtab = NULL;
4283
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004284 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004285 * There are three cases:
4286 * 1. NULL, or None - this is a deletion.
4287 * 2. A string - this is a replacement.
4288 * 3. Anything else - this is an error.
4289 */
4290 if (line == Py_None || line == NULL)
4291 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004292 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004293 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004294
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004295 VimTryStart();
4296
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004297 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004298 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004299 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004300 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004301 else
4302 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004303 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004304 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004305 if (save_curbuf == NULL)
4306 /* Only adjust marks if we managed to switch to a window that
4307 * holds the buffer, otherwise line numbers will be invalid. */
4308 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004309 }
4310
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004311 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004312
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004313 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004314 return FAIL;
4315
4316 if (len_change)
4317 *len_change = -1;
4318
4319 return OK;
4320 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004321 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004322 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004323 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004324
4325 if (save == NULL)
4326 return FAIL;
4327
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004328 VimTryStart();
4329
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004330 /* We do not need to free "save" if ml_replace() consumes it. */
4331 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004332 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004333
4334 if (u_savesub((linenr_T)n) == FAIL)
4335 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004336 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004337 vim_free(save);
4338 }
4339 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4340 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004341 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004342 vim_free(save);
4343 }
4344 else
4345 changed_bytes((linenr_T)n, 0);
4346
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004347 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004348
4349 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004350 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004351 check_cursor_col();
4352
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004353 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004354 return FAIL;
4355
4356 if (len_change)
4357 *len_change = 0;
4358
4359 return OK;
4360 }
4361 else
4362 {
4363 PyErr_BadArgument();
4364 return FAIL;
4365 }
4366}
4367
Bram Moolenaar19e60942011-06-19 00:27:51 +02004368/* Replace a range of lines in the specified buffer. The line numbers are in
4369 * Vim format (1-based). The range is from lo up to, but not including, hi.
4370 * The replacement lines are given as a Python list of string objects. The
4371 * list is checked for validity and correct format. Errors are returned as a
4372 * value of FAIL. The return value is OK on success.
4373 * If OK is returned and len_change is not NULL, *len_change
4374 * is set to the change in the buffer length.
4375 */
4376 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004377SetBufferLineList(
4378 buf_T *buf,
4379 PyInt lo,
4380 PyInt hi,
4381 PyObject *list,
4382 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004383{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004384 buf_T *save_curbuf = NULL;
4385 win_T *save_curwin = NULL;
4386 tabpage_T *save_curtab = NULL;
4387
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004388 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004389 * There are three cases:
4390 * 1. NULL, or None - this is a deletion.
4391 * 2. A list - this is a replacement.
4392 * 3. Anything else - this is an error.
4393 */
4394 if (list == Py_None || list == NULL)
4395 {
4396 PyInt i;
4397 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004398
4399 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004400 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004401 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004402
4403 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004404 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004405 else
4406 {
4407 for (i = 0; i < n; ++i)
4408 {
4409 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4410 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004411 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004412 break;
4413 }
4414 }
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004415 if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL))
4416 /* Using an existing window for the buffer, adjust the cursor
4417 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004418 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004419 if (save_curbuf == NULL)
4420 /* Only adjust marks if we managed to switch to a window that
4421 * holds the buffer, otherwise line numbers will be invalid. */
4422 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004423 }
4424
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004425 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004426
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004427 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004428 return FAIL;
4429
4430 if (len_change)
4431 *len_change = -n;
4432
4433 return OK;
4434 }
4435 else if (PyList_Check(list))
4436 {
4437 PyInt i;
4438 PyInt new_len = PyList_Size(list);
4439 PyInt old_len = hi - lo;
4440 PyInt extra = 0; /* lines added to text, can be negative */
4441 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004442
4443 if (new_len == 0) /* avoid allocating zero bytes */
4444 array = NULL;
4445 else
4446 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004447 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004448 if (array == NULL)
4449 {
4450 PyErr_NoMemory();
4451 return FAIL;
4452 }
4453 }
4454
4455 for (i = 0; i < new_len; ++i)
4456 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004457 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004458
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004459 if (!(line = PyList_GetItem(list, i)) ||
4460 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004461 {
4462 while (i)
4463 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004464 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004465 return FAIL;
4466 }
4467 }
4468
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004469 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004470 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004471
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004472 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004473 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004474
4475 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004476 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004477
4478 /* If the size of the range is reducing (ie, new_len < old_len) we
4479 * need to delete some old_len. We do this at the start, by
4480 * repeatedly deleting line "lo".
4481 */
4482 if (!PyErr_Occurred())
4483 {
4484 for (i = 0; i < old_len - new_len; ++i)
4485 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4486 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004487 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004488 break;
4489 }
4490 extra -= i;
4491 }
4492
4493 /* For as long as possible, replace the existing old_len with the
4494 * new old_len. This is a more efficient operation, as it requires
4495 * less memory allocation and freeing.
4496 */
4497 if (!PyErr_Occurred())
4498 {
4499 for (i = 0; i < old_len && i < new_len; ++i)
4500 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4501 == FAIL)
4502 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004503 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004504 break;
4505 }
4506 }
4507 else
4508 i = 0;
4509
4510 /* Now we may need to insert the remaining new old_len. If we do, we
4511 * must free the strings as we finish with them (we can't pass the
4512 * responsibility to vim in this case).
4513 */
4514 if (!PyErr_Occurred())
4515 {
4516 while (i < new_len)
4517 {
4518 if (ml_append((linenr_T)(lo + i - 1),
4519 (char_u *)array[i], 0, FALSE) == FAIL)
4520 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004521 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004522 break;
4523 }
4524 vim_free(array[i]);
4525 ++i;
4526 ++extra;
4527 }
4528 }
4529
4530 /* Free any left-over old_len, as a result of an error */
4531 while (i < new_len)
4532 {
4533 vim_free(array[i]);
4534 ++i;
4535 }
4536
4537 /* Free the array of old_len. All of its contents have now
4538 * been dealt with (either freed, or the responsibility passed
4539 * to vim.
4540 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004541 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004542
4543 /* Adjust marks. Invalidate any which lie in the
4544 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004545 * Only adjust marks if we managed to switch to a window that holds
4546 * the buffer, otherwise line numbers will be invalid. */
4547 if (save_curbuf == NULL)
4548 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004549 (long)MAXLNUM, (long)extra);
4550 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4551
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004552 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004553 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4554
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004555 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004556 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004557
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004558 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004559 return FAIL;
4560
4561 if (len_change)
4562 *len_change = new_len - old_len;
4563
4564 return OK;
4565 }
4566 else
4567 {
4568 PyErr_BadArgument();
4569 return FAIL;
4570 }
4571}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004572
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004573/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004574 * The line number is in Vim format (1-based). The lines to be inserted are
4575 * given as a Python list of string objects or as a single string. The lines
4576 * to be added are checked for validity and correct format. Errors are
4577 * returned as a value of FAIL. The return value is OK on success.
4578 * If OK is returned and len_change is not NULL, *len_change
4579 * is set to the change in the buffer length.
4580 */
4581 static int
4582InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4583{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004584 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004585 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004586 tabpage_T *save_curtab = NULL;
4587
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004588 /* First of all, we check the type of the supplied Python object.
4589 * It must be a string or a list, or the call is in error.
4590 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004591 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004592 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004593 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004594
4595 if (str == NULL)
4596 return FAIL;
4597
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004598 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004599 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004600 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004601
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004602 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004603 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004604 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004605 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004606 else if (save_curbuf == NULL)
4607 /* Only adjust marks if we managed to switch to a window that
4608 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004609 appended_lines_mark((linenr_T)n, 1L);
4610
4611 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004612 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004613 update_screen(VALID);
4614
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004615 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004616 return FAIL;
4617
4618 if (len_change)
4619 *len_change = 1;
4620
4621 return OK;
4622 }
4623 else if (PyList_Check(lines))
4624 {
4625 PyInt i;
4626 PyInt size = PyList_Size(lines);
4627 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004628
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004629 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004630 if (array == NULL)
4631 {
4632 PyErr_NoMemory();
4633 return FAIL;
4634 }
4635
4636 for (i = 0; i < size; ++i)
4637 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004638 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004639
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004640 if (!(line = PyList_GetItem(lines, i)) ||
4641 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004642 {
4643 while (i)
4644 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004645 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004646 return FAIL;
4647 }
4648 }
4649
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004650 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004651 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004652 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004653
4654 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004655 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004656 else
4657 {
4658 for (i = 0; i < size; ++i)
4659 {
4660 if (ml_append((linenr_T)(n + i),
4661 (char_u *)array[i], 0, FALSE) == FAIL)
4662 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004663 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004664
4665 /* Free the rest of the lines */
4666 while (i < size)
4667 vim_free(array[i++]);
4668
4669 break;
4670 }
4671 vim_free(array[i]);
4672 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004673 if (i > 0 && save_curbuf == NULL)
4674 /* Only adjust marks if we managed to switch to a window that
4675 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004676 appended_lines_mark((linenr_T)n, (long)i);
4677 }
4678
4679 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004680 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004681 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004682 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004683
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004684 update_screen(VALID);
4685
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004686 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004687 return FAIL;
4688
4689 if (len_change)
4690 *len_change = size;
4691
4692 return OK;
4693 }
4694 else
4695 {
4696 PyErr_BadArgument();
4697 return FAIL;
4698 }
4699}
4700
4701/*
4702 * Common routines for buffers and line ranges
4703 * -------------------------------------------
4704 */
4705
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004706typedef struct
4707{
4708 PyObject_HEAD
4709 buf_T *buf;
4710} BufferObject;
4711
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004712 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004713CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004714{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004715 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004716 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004717 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004718 return -1;
4719 }
4720
4721 return 0;
4722}
4723
4724 static PyObject *
4725RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4726{
4727 if (CheckBuffer(self))
4728 return NULL;
4729
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004730 if (end == -1)
4731 end = self->buf->b_ml.ml_line_count;
4732
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004733 if (n < 0)
4734 n += end - start + 1;
4735
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004736 if (n < 0 || n > end - start)
4737 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004738 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004739 return NULL;
4740 }
4741
4742 return GetBufferLine(self->buf, n+start);
4743}
4744
4745 static PyObject *
4746RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4747{
4748 PyInt size;
4749
4750 if (CheckBuffer(self))
4751 return NULL;
4752
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004753 if (end == -1)
4754 end = self->buf->b_ml.ml_line_count;
4755
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004756 size = end - start + 1;
4757
4758 if (lo < 0)
4759 lo = 0;
4760 else if (lo > size)
4761 lo = size;
4762 if (hi < 0)
4763 hi = 0;
4764 if (hi < lo)
4765 hi = lo;
4766 else if (hi > size)
4767 hi = size;
4768
4769 return GetBufferLineList(self->buf, lo+start, hi+start);
4770}
4771
4772 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004773RBAsItem(
4774 BufferObject *self,
4775 PyInt n,
4776 PyObject *valObject,
4777 PyInt start,
4778 PyInt end,
4779 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004780{
4781 PyInt len_change;
4782
4783 if (CheckBuffer(self))
4784 return -1;
4785
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004786 if (end == -1)
4787 end = self->buf->b_ml.ml_line_count;
4788
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004789 if (n < 0)
4790 n += end - start + 1;
4791
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004792 if (n < 0 || n > end - start)
4793 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004794 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004795 return -1;
4796 }
4797
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004798 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004799 return -1;
4800
4801 if (new_end)
4802 *new_end = end + len_change;
4803
4804 return 0;
4805}
4806
Bram Moolenaar19e60942011-06-19 00:27:51 +02004807 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004808RBAsSlice(
4809 BufferObject *self,
4810 PyInt lo,
4811 PyInt hi,
4812 PyObject *valObject,
4813 PyInt start,
4814 PyInt end,
4815 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004816{
4817 PyInt size;
4818 PyInt len_change;
4819
4820 /* Self must be a valid buffer */
4821 if (CheckBuffer(self))
4822 return -1;
4823
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004824 if (end == -1)
4825 end = self->buf->b_ml.ml_line_count;
4826
Bram Moolenaar19e60942011-06-19 00:27:51 +02004827 /* Sort out the slice range */
4828 size = end - start + 1;
4829
4830 if (lo < 0)
4831 lo = 0;
4832 else if (lo > size)
4833 lo = size;
4834 if (hi < 0)
4835 hi = 0;
4836 if (hi < lo)
4837 hi = lo;
4838 else if (hi > size)
4839 hi = size;
4840
4841 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004842 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004843 return -1;
4844
4845 if (new_end)
4846 *new_end = end + len_change;
4847
4848 return 0;
4849}
4850
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004851
4852 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004853RBAppend(
4854 BufferObject *self,
4855 PyObject *args,
4856 PyInt start,
4857 PyInt end,
4858 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004859{
4860 PyObject *lines;
4861 PyInt len_change;
4862 PyInt max;
4863 PyInt n;
4864
4865 if (CheckBuffer(self))
4866 return NULL;
4867
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004868 if (end == -1)
4869 end = self->buf->b_ml.ml_line_count;
4870
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004871 max = n = end - start + 1;
4872
4873 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4874 return NULL;
4875
4876 if (n < 0 || n > max)
4877 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004878 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004879 return NULL;
4880 }
4881
4882 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4883 return NULL;
4884
4885 if (new_end)
4886 *new_end = end + len_change;
4887
4888 Py_INCREF(Py_None);
4889 return Py_None;
4890}
4891
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004892/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004893 */
4894
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004895static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004896static PySequenceMethods RangeAsSeq;
4897static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004898
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004899typedef struct
4900{
4901 PyObject_HEAD
4902 BufferObject *buf;
4903 PyInt start;
4904 PyInt end;
4905} RangeObject;
4906
4907 static PyObject *
4908RangeNew(buf_T *buf, PyInt start, PyInt end)
4909{
4910 BufferObject *bufr;
4911 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004912 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004913 if (self == NULL)
4914 return NULL;
4915
4916 bufr = (BufferObject *)BufferNew(buf);
4917 if (bufr == NULL)
4918 {
4919 Py_DECREF(self);
4920 return NULL;
4921 }
4922 Py_INCREF(bufr);
4923
4924 self->buf = bufr;
4925 self->start = start;
4926 self->end = end;
4927
4928 return (PyObject *)(self);
4929}
4930
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004931 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004932RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004933{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004934 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004935 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004936 PyObject_GC_Del((void *)(self));
4937}
4938
4939 static int
4940RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4941{
4942 Py_VISIT(((PyObject *)(self->buf)));
4943 return 0;
4944}
4945
4946 static int
4947RangeClear(RangeObject *self)
4948{
4949 Py_CLEAR(self->buf);
4950 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004951}
4952
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004953 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004954RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004955{
4956 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004957 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004958 return -1; /* ??? */
4959
Bram Moolenaard6e39182013-05-21 18:30:34 +02004960 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004961}
4962
4963 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004964RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004965{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004966 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004967}
4968
4969 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004970RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004971{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004972 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004973}
4974
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004975static char *RangeAttrs[] = {
4976 "start", "end",
4977 NULL
4978};
4979
4980 static PyObject *
4981RangeDir(PyObject *self)
4982{
4983 return ObjectDir(self, RangeAttrs);
4984}
4985
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004986 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004987RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004988{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004989 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004990}
4991
4992 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004993RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004994{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004995 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004996 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4997 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004998 else
4999 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005000 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005001
5002 if (name == NULL)
5003 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005004
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005005 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005006 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005007 }
5008}
5009
5010static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005011 /* name, function, calling, documentation */
5012 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005013 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5014 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005015};
5016
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005017static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005018static PySequenceMethods BufferAsSeq;
5019static PyMappingMethods BufferAsMapping;
5020
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005021 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005022BufferNew(buf_T *buf)
5023{
5024 /* We need to handle deletion of buffers underneath us.
5025 * If we add a "b_python*_ref" field to the buf_T structure,
5026 * then we can get at it in buf_freeall() in vim. We then
5027 * need to create only ONE Python object per buffer - if
5028 * we try to create a second, just INCREF the existing one
5029 * and return it. The (single) Python object referring to
5030 * the buffer is stored in "b_python*_ref".
5031 * Question: what to do on a buf_freeall(). We'll probably
5032 * have to either delete the Python object (DECREF it to
5033 * zero - a bad idea, as it leaves dangling refs!) or
5034 * set the buf_T * value to an invalid value (-1?), which
5035 * means we need checks in all access functions... Bah.
5036 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005037 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005038 * b_python_ref and b_python3_ref fields respectively.
5039 */
5040
5041 BufferObject *self;
5042
5043 if (BUF_PYTHON_REF(buf) != NULL)
5044 {
5045 self = BUF_PYTHON_REF(buf);
5046 Py_INCREF(self);
5047 }
5048 else
5049 {
5050 self = PyObject_NEW(BufferObject, &BufferType);
5051 if (self == NULL)
5052 return NULL;
5053 self->buf = buf;
5054 BUF_PYTHON_REF(buf) = self;
5055 }
5056
5057 return (PyObject *)(self);
5058}
5059
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005060 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005061BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005062{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005063 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5064 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005065
5066 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005067}
5068
Bram Moolenaar971db462013-05-12 18:44:48 +02005069 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005070BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005071{
5072 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005073 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005074 return -1; /* ??? */
5075
Bram Moolenaard6e39182013-05-21 18:30:34 +02005076 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005077}
5078
5079 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005080BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005081{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005082 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005083}
5084
5085 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005086BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005087{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005088 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005089}
5090
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005091static char *BufferAttrs[] = {
5092 "name", "number", "vars", "options", "valid",
5093 NULL
5094};
5095
5096 static PyObject *
5097BufferDir(PyObject *self)
5098{
5099 return ObjectDir(self, BufferAttrs);
5100}
5101
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005102 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005103BufferAttrValid(BufferObject *self, char *name)
5104{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005105 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005106
5107 if (strcmp(name, "valid") != 0)
5108 return NULL;
5109
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005110 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5111 Py_INCREF(ret);
5112 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005113}
5114
5115 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005116BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005117{
5118 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005119 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005120 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005121 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005122 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005123 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005124 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005125 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005126 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5127 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005128 else if (strcmp(name, "__members__") == 0)
5129 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005130 else
5131 return NULL;
5132}
5133
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005134 static int
5135BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5136{
5137 if (CheckBuffer(self))
5138 return -1;
5139
5140 if (strcmp(name, "name") == 0)
5141 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005142 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005143 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005144 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005145 PyObject *todecref;
5146
5147 if (!(val = StringToChars(valObject, &todecref)))
5148 return -1;
5149
5150 VimTryStart();
5151 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5152 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005153 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005154 aucmd_restbuf(&aco);
5155 Py_XDECREF(todecref);
5156 if (VimTryEnd())
5157 return -1;
5158
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005159 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005160 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005161 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005162 return -1;
5163 }
5164 return 0;
5165 }
5166 else
5167 {
5168 PyErr_SetString(PyExc_AttributeError, name);
5169 return -1;
5170 }
5171}
5172
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005173 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005174BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005175{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005176 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005177}
5178
5179 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005180BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005181{
5182 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005183 char_u *pmark;
5184 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02005185 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005186 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005187
Bram Moolenaard6e39182013-05-21 18:30:34 +02005188 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005189 return NULL;
5190
Bram Moolenaar389a1792013-06-23 13:00:44 +02005191 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005192 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005193
Bram Moolenaar389a1792013-06-23 13:00:44 +02005194 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005195 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005196 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005197 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005198 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005199 return NULL;
5200 }
5201
5202 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005203
5204 Py_XDECREF(todecref);
5205
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005206 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005207 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005208 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02005209 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005210 if (VimTryEnd())
5211 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005212
5213 if (posp == NULL)
5214 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005215 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005216 return NULL;
5217 }
5218
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005219 if (posp->lnum <= 0)
5220 {
5221 /* Or raise an error? */
5222 Py_INCREF(Py_None);
5223 return Py_None;
5224 }
5225
5226 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5227}
5228
5229 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005230BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005231{
5232 PyInt start;
5233 PyInt end;
5234
Bram Moolenaard6e39182013-05-21 18:30:34 +02005235 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005236 return NULL;
5237
5238 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5239 return NULL;
5240
Bram Moolenaard6e39182013-05-21 18:30:34 +02005241 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005242}
5243
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005244 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005245BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005246{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005247 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005248 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005249 else
5250 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005251 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005252
5253 if (name == NULL)
5254 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005255
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005256 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005257 }
5258}
5259
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005260static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005261 /* name, function, calling, documentation */
5262 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005263 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005264 {"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 +02005265 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5266 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005267};
5268
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005269/*
5270 * Buffer list object - Implementation
5271 */
5272
5273static PyTypeObject BufMapType;
5274
5275typedef struct
5276{
5277 PyObject_HEAD
5278} BufMapObject;
5279
5280 static PyInt
5281BufMapLength(PyObject *self UNUSED)
5282{
5283 buf_T *b = firstbuf;
5284 PyInt n = 0;
5285
5286 while (b)
5287 {
5288 ++n;
5289 b = b->b_next;
5290 }
5291
5292 return n;
5293}
5294
5295 static PyObject *
5296BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5297{
5298 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005299 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005300
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005301 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005302 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005303
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005304 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005305
5306 if (b)
5307 return BufferNew(b);
5308 else
5309 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005310 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005311 return NULL;
5312 }
5313}
5314
5315 static void
5316BufMapIterDestruct(PyObject *buffer)
5317{
5318 /* Iteration was stopped before all buffers were processed */
5319 if (buffer)
5320 {
5321 Py_DECREF(buffer);
5322 }
5323}
5324
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005325 static int
5326BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5327{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005328 if (buffer)
5329 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005330 return 0;
5331}
5332
5333 static int
5334BufMapIterClear(PyObject **buffer)
5335{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005336 if (*buffer)
5337 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005338 return 0;
5339}
5340
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005341 static PyObject *
5342BufMapIterNext(PyObject **buffer)
5343{
5344 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005345 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005346
5347 if (!*buffer)
5348 return NULL;
5349
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005350 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005351
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005352 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005353 {
5354 *buffer = NULL;
5355 return NULL;
5356 }
5357
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005358 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005359 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005360 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005361 return NULL;
5362 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005363 /* Do not increment reference: we no longer hold it (decref), but whoever
5364 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005365 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005366}
5367
5368 static PyObject *
5369BufMapIter(PyObject *self UNUSED)
5370{
5371 PyObject *buffer;
5372
5373 buffer = BufferNew(firstbuf);
5374 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005375 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5376 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005377}
5378
5379static PyMappingMethods BufMapAsMapping = {
5380 (lenfunc) BufMapLength,
5381 (binaryfunc) BufMapItem,
5382 (objobjargproc) 0,
5383};
5384
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005385/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005386 */
5387
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005388static char *CurrentAttrs[] = {
5389 "buffer", "window", "line", "range", "tabpage",
5390 NULL
5391};
5392
5393 static PyObject *
5394CurrentDir(PyObject *self)
5395{
5396 return ObjectDir(self, CurrentAttrs);
5397}
5398
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005399 static PyObject *
5400CurrentGetattr(PyObject *self UNUSED, char *name)
5401{
5402 if (strcmp(name, "buffer") == 0)
5403 return (PyObject *)BufferNew(curbuf);
5404 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005405 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005406 else if (strcmp(name, "tabpage") == 0)
5407 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005408 else if (strcmp(name, "line") == 0)
5409 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5410 else if (strcmp(name, "range") == 0)
5411 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005412 else if (strcmp(name, "__members__") == 0)
5413 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005414 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005415#if PY_MAJOR_VERSION < 3
5416 return Py_FindMethod(WindowMethods, self, name);
5417#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005418 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005419#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005420}
5421
5422 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005423CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005424{
5425 if (strcmp(name, "line") == 0)
5426 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005427 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5428 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005429 return -1;
5430
5431 return 0;
5432 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005433 else if (strcmp(name, "buffer") == 0)
5434 {
5435 int count;
5436
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005437 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005438 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005439 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005440 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005441 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005442 return -1;
5443 }
5444
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005445 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005446 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005447 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005448
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005449 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005450 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5451 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005452 if (VimTryEnd())
5453 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005454 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005455 return -1;
5456 }
5457
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005458 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005459 }
5460 else if (strcmp(name, "window") == 0)
5461 {
5462 int count;
5463
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005464 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005465 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005466 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005467 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005468 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005469 return -1;
5470 }
5471
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005472 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005473 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005474 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005475
5476 if (!count)
5477 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005478 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005479 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005480 return -1;
5481 }
5482
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005483 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005484 win_goto(((WindowObject *)(valObject))->win);
5485 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005486 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005487 if (VimTryEnd())
5488 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005489 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005490 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005491 return -1;
5492 }
5493
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005494 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005495 }
5496 else if (strcmp(name, "tabpage") == 0)
5497 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005498 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005499 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005500 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005501 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005502 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005503 return -1;
5504 }
5505
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005506 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005507 return -1;
5508
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005509 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005510 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5511 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005512 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005513 if (VimTryEnd())
5514 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005515 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005516 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005517 return -1;
5518 }
5519
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005520 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005521 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005522 else
5523 {
5524 PyErr_SetString(PyExc_AttributeError, name);
5525 return -1;
5526 }
5527}
5528
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005529static struct PyMethodDef CurrentMethods[] = {
5530 /* name, function, calling, documentation */
5531 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5532 { NULL, NULL, 0, NULL}
5533};
5534
Bram Moolenaardb913952012-06-29 12:54:53 +02005535 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005536init_range_cmd(exarg_T *eap)
5537{
5538 RangeStart = eap->line1;
5539 RangeEnd = eap->line2;
5540}
5541
5542 static void
5543init_range_eval(typval_T *rettv UNUSED)
5544{
5545 RangeStart = (PyInt) curwin->w_cursor.lnum;
5546 RangeEnd = RangeStart;
5547}
5548
5549 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005550run_cmd(const char *cmd, void *arg UNUSED
5551#ifdef PY_CAN_RECURSE
5552 , PyGILState_STATE *pygilstate UNUSED
5553#endif
5554 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005555{
Bram Moolenaar41009372013-07-01 22:03:04 +02005556 PyObject *run_ret;
5557 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5558 if (run_ret != NULL)
5559 {
5560 Py_DECREF(run_ret);
5561 }
5562 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5563 {
5564 EMSG2(_(e_py_systemexit), "python");
5565 PyErr_Clear();
5566 }
5567 else
5568 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005569}
5570
5571static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5572static int code_hdr_len = 30;
5573
5574 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005575run_do(const char *cmd, void *arg UNUSED
5576#ifdef PY_CAN_RECURSE
5577 , PyGILState_STATE *pygilstate
5578#endif
5579 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005580{
5581 PyInt lnum;
5582 size_t len;
5583 char *code;
5584 int status;
5585 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005586 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005587
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005588 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005589 {
5590 EMSG(_("cannot save undo information"));
5591 return;
5592 }
5593
5594 len = code_hdr_len + STRLEN(cmd);
5595 code = PyMem_New(char, len + 1);
5596 memcpy(code, code_hdr, code_hdr_len);
5597 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005598 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5599 status = -1;
5600 if (run_ret != NULL)
5601 {
5602 status = 0;
5603 Py_DECREF(run_ret);
5604 }
5605 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5606 {
5607 PyMem_Free(code);
5608 EMSG2(_(e_py_systemexit), "python");
5609 PyErr_Clear();
5610 return;
5611 }
5612 else
5613 PyErr_PrintEx(1);
5614
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005615 PyMem_Free(code);
5616
5617 if (status)
5618 {
5619 EMSG(_("failed to run the code"));
5620 return;
5621 }
5622
5623 status = 0;
5624 pymain = PyImport_AddModule("__main__");
5625 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005626#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005627 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005628#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005629
5630 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5631 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005632 PyObject *line;
5633 PyObject *linenr;
5634 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005635
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005636#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005637 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005638#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005639 if (!(line = GetBufferLine(curbuf, lnum)))
5640 goto err;
5641 if (!(linenr = PyInt_FromLong((long) lnum)))
5642 {
5643 Py_DECREF(line);
5644 goto err;
5645 }
5646 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5647 Py_DECREF(line);
5648 Py_DECREF(linenr);
5649 if (!ret)
5650 goto err;
5651
5652 if (ret != Py_None)
5653 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5654 goto err;
5655
5656 Py_XDECREF(ret);
5657 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005658#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005659 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005660#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005661 }
5662 goto out;
5663err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005664#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005665 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005666#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005667 PyErr_PrintEx(0);
5668 PythonIO_Flush();
5669 status = 1;
5670out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005671#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005672 if (!status)
5673 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005674#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005675 Py_DECREF(pyfunc);
5676 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5677 if (status)
5678 return;
5679 check_cursor();
5680 update_curbuf(NOT_VALID);
5681}
5682
5683 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005684run_eval(const char *cmd, typval_T *rettv
5685#ifdef PY_CAN_RECURSE
5686 , PyGILState_STATE *pygilstate UNUSED
5687#endif
5688 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005689{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005690 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005691
Bram Moolenaar41009372013-07-01 22:03:04 +02005692 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005693 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005694 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005695 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005696 {
5697 EMSG2(_(e_py_systemexit), "python");
5698 PyErr_Clear();
5699 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005700 else
5701 {
5702 if (PyErr_Occurred() && !msg_silent)
5703 PyErr_PrintEx(0);
5704 EMSG(_("E858: Eval did not return a valid python object"));
5705 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005706 }
5707 else
5708 {
Bram Moolenaar77324fc2016-01-17 22:37:03 +01005709 if (run_ret != Py_None && ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005710 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005711 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005712 }
5713 PyErr_Clear();
5714}
5715
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005716 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005717set_ref_in_py(const int copyID)
5718{
5719 pylinkedlist_T *cur;
5720 dict_T *dd;
5721 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005722 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005723 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005724 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005725
5726 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005727 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005728 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005729 {
5730 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5731 if (dd->dv_copyID != copyID)
5732 {
5733 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005734 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005735 }
5736 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005737 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005738
5739 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005740 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005741 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005742 {
5743 ll = ((ListObject *) (cur->pll_obj))->list;
5744 if (ll->lv_copyID != copyID)
5745 {
5746 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005747 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005748 }
5749 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005750 }
5751
Bram Moolenaar8110a092016-04-14 15:56:09 +02005752 if (lastfunc != NULL)
5753 {
5754 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5755 {
5756 func = (FunctionObject *) cur->pll_obj;
5757 if (func->self != NULL && func->self->dv_copyID != copyID)
5758 {
5759 func->self->dv_copyID = copyID;
5760 abort = abort || set_ref_in_ht(
5761 &func->self->dv_hashtab, copyID, NULL);
5762 }
5763 if (func->argc)
5764 for (i = 0; !abort && i < func->argc; ++i)
5765 abort = abort
5766 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5767 }
5768 }
5769
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005770 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005771}
5772
5773 static int
5774set_string_copy(char_u *str, typval_T *tv)
5775{
5776 tv->vval.v_string = vim_strsave(str);
5777 if (tv->vval.v_string == NULL)
5778 {
5779 PyErr_NoMemory();
5780 return -1;
5781 }
5782 return 0;
5783}
5784
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005785 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005786pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005787{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005788 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005789 char_u *key;
5790 dictitem_T *di;
5791 PyObject *keyObject;
5792 PyObject *valObject;
5793 Py_ssize_t iter = 0;
5794
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005795 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005796 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005797
5798 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005799 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005800
5801 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5802 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005803 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005804
Bram Moolenaara03e6312013-05-29 22:49:26 +02005805 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005806 {
5807 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005808 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005809 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005810
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005811 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005812 {
5813 dict_unref(dict);
5814 return -1;
5815 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005816
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005817 if (*key == NUL)
5818 {
5819 dict_unref(dict);
5820 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005821 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005822 return -1;
5823 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005824
5825 di = dictitem_alloc(key);
5826
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005827 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005828
5829 if (di == NULL)
5830 {
5831 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005832 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005833 return -1;
5834 }
5835 di->di_tv.v_lock = 0;
5836
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005837 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005838 {
5839 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005840 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005841 return -1;
5842 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005843
5844 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005845 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005846 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005847 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005848 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005849 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005850 return -1;
5851 }
5852 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005853
5854 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005855 return 0;
5856}
5857
5858 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005859pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005860{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005861 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005862 char_u *key;
5863 dictitem_T *di;
5864 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005865 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005866 PyObject *keyObject;
5867 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005868
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005869 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005870 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005871
5872 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005873 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005874
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005875 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005876 {
5877 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005878 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005879 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005880
5881 if (!(iterator = PyObject_GetIter(list)))
5882 {
5883 dict_unref(dict);
5884 Py_DECREF(list);
5885 return -1;
5886 }
5887 Py_DECREF(list);
5888
5889 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005890 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005891 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005892
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005893 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005894 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005895 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005896 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005897 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005898 return -1;
5899 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005900
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005901 if (*key == NUL)
5902 {
5903 Py_DECREF(keyObject);
5904 Py_DECREF(iterator);
5905 Py_XDECREF(todecref);
5906 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005907 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005908 return -1;
5909 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005910
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005911 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005912 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005913 Py_DECREF(keyObject);
5914 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005915 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005916 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005917 return -1;
5918 }
5919
5920 di = dictitem_alloc(key);
5921
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005922 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005923 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005924
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005925 if (di == NULL)
5926 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005927 Py_DECREF(iterator);
5928 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005929 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005930 PyErr_NoMemory();
5931 return -1;
5932 }
5933 di->di_tv.v_lock = 0;
5934
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005935 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005936 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005937 Py_DECREF(iterator);
5938 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005939 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005940 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005941 return -1;
5942 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005943
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005944 Py_DECREF(valObject);
5945
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005946 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005947 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005948 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005949 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005950 dictitem_free(di);
5951 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005952 return -1;
5953 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005954 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005955 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005956 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005957 return 0;
5958}
5959
5960 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005961pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005962{
5963 list_T *l;
5964
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005965 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005967
5968 tv->v_type = VAR_LIST;
5969 tv->vval.v_list = l;
5970
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005971 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005972 {
5973 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005974 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005975 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005976
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005977 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005978 return 0;
5979}
5980
Bram Moolenaardb913952012-06-29 12:54:53 +02005981typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5982
5983 static int
5984convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005985 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005986{
5987 PyObject *capsule;
5988 char hexBuf[sizeof(void *) * 2 + 3];
5989
5990 sprintf(hexBuf, "%p", obj);
5991
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005992# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005993 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005994# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005995 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005996# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005997 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005998 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005999# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006000 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006001# else
6002 capsule = PyCObject_FromVoidPtr(tv, NULL);
6003# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006004 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6005 {
6006 Py_DECREF(capsule);
6007 tv->v_type = VAR_UNKNOWN;
6008 return -1;
6009 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006010
6011 Py_DECREF(capsule);
6012
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006013 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006014 {
6015 tv->v_type = VAR_UNKNOWN;
6016 return -1;
6017 }
6018 /* As we are not using copy_tv which increments reference count we must
6019 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006020 if (tv->v_type == VAR_DICT)
6021 ++tv->vval.v_dict->dv_refcount;
6022 else if (tv->v_type == VAR_LIST)
6023 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006024 }
6025 else
6026 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006027 typval_T *v;
6028
6029# ifdef PY_USE_CAPSULE
6030 v = PyCapsule_GetPointer(capsule, NULL);
6031# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006032 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006033# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006034 copy_tv(v, tv);
6035 }
6036 return 0;
6037}
6038
6039 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006040ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6041{
6042 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006043 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006044
6045 if (!(lookup_dict = PyDict_New()))
6046 return -1;
6047
6048 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6049 {
6050 tv->v_type = VAR_DICT;
6051 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6052 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006053 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006054 }
6055 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006056 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006057 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006058 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006059 else
6060 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006061 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006062 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006063 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006064 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006065 }
6066 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006067 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006068}
6069
6070 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006071ConvertFromPySequence(PyObject *obj, typval_T *tv)
6072{
6073 PyObject *lookup_dict;
6074 int ret;
6075
6076 if (!(lookup_dict = PyDict_New()))
6077 return -1;
6078
6079 if (PyType_IsSubtype(obj->ob_type, &ListType))
6080 {
6081 tv->v_type = VAR_LIST;
6082 tv->vval.v_list = (((ListObject *)(obj))->list);
6083 ++tv->vval.v_list->lv_refcount;
6084 }
6085 else if (PyIter_Check(obj) || PySequence_Check(obj))
6086 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
6087 else
6088 {
6089 PyErr_FORMAT(PyExc_TypeError,
6090 N_("unable to convert %s to vim list"),
6091 Py_TYPE_NAME(obj));
6092 ret = -1;
6093 }
6094 Py_DECREF(lookup_dict);
6095 return ret;
6096}
6097
6098 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006099ConvertFromPyObject(PyObject *obj, typval_T *tv)
6100{
6101 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006102 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006103
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006104 if (!(lookup_dict = PyDict_New()))
6105 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006106 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006107 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006108 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006109}
6110
6111 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006112_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006113{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006114 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006115 {
6116 tv->v_type = VAR_DICT;
6117 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6118 ++tv->vval.v_dict->dv_refcount;
6119 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006120 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006121 {
6122 tv->v_type = VAR_LIST;
6123 tv->vval.v_list = (((ListObject *)(obj))->list);
6124 ++tv->vval.v_list->lv_refcount;
6125 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006126 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006127 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006128 FunctionObject *func = (FunctionObject *) obj;
6129 if (func->self != NULL || func->argv != NULL)
6130 {
6131 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6132 set_partial(func, pt, TRUE);
6133 tv->vval.v_partial = pt;
6134 tv->v_type = VAR_PARTIAL;
6135 }
6136 else
6137 {
6138 if (set_string_copy(func->name, tv) == -1)
6139 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006140
Bram Moolenaar8110a092016-04-14 15:56:09 +02006141 tv->v_type = VAR_FUNC;
6142 }
6143 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006144 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006145 else if (PyBytes_Check(obj))
6146 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006147 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006148
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006149 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006150 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006151 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006152 return -1;
6153
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006154 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006155 return -1;
6156
6157 tv->v_type = VAR_STRING;
6158 }
6159 else if (PyUnicode_Check(obj))
6160 {
6161 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006162 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006163
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006164 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006165 if (bytes == NULL)
6166 return -1;
6167
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006168 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006169 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006170 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006171 return -1;
6172
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006173 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006174 {
6175 Py_XDECREF(bytes);
6176 return -1;
6177 }
6178 Py_XDECREF(bytes);
6179
6180 tv->v_type = VAR_STRING;
6181 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006182#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006183 else if (PyInt_Check(obj))
6184 {
6185 tv->v_type = VAR_NUMBER;
6186 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006187 if (PyErr_Occurred())
6188 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006189 }
6190#endif
6191 else if (PyLong_Check(obj))
6192 {
6193 tv->v_type = VAR_NUMBER;
6194 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006195 if (PyErr_Occurred())
6196 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006197 }
6198 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006199 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006200#ifdef FEAT_FLOAT
6201 else if (PyFloat_Check(obj))
6202 {
6203 tv->v_type = VAR_FLOAT;
6204 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6205 }
6206#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006207 else if (PyObject_HasAttrString(obj, "keys"))
6208 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006209 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006210 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006211 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006212 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006213 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006214 else if (PyNumber_Check(obj))
6215 {
6216 PyObject *num;
6217
6218 if (!(num = PyNumber_Long(obj)))
6219 return -1;
6220
6221 tv->v_type = VAR_NUMBER;
6222 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6223
6224 Py_DECREF(num);
6225 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006226 else
6227 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006228 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006229 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006230 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006231 return -1;
6232 }
6233 return 0;
6234}
6235
6236 static PyObject *
6237ConvertToPyObject(typval_T *tv)
6238{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006239 typval_T *argv;
6240 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006241 if (tv == NULL)
6242 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006243 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006244 return NULL;
6245 }
6246 switch (tv->v_type)
6247 {
6248 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006249 return PyBytes_FromString(tv->vval.v_string == NULL
6250 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006251 case VAR_NUMBER:
6252 return PyLong_FromLong((long) tv->vval.v_number);
6253#ifdef FEAT_FLOAT
6254 case VAR_FLOAT:
6255 return PyFloat_FromDouble((double) tv->vval.v_float);
6256#endif
6257 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006258 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006260 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006261 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006262 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006263 ? (char_u *)"" : tv->vval.v_string,
6264 0, NULL, NULL);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006265 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006266 if (tv->vval.v_partial->pt_argc)
6267 {
6268 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6269 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6270 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6271 }
6272 else
6273 argv = NULL;
6274 if (tv->vval.v_partial->pt_dict != NULL)
6275 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006276 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006277 ? (char_u *)"" : tv->vval.v_partial->pt_name,
6278 tv->vval.v_partial->pt_argc, argv,
6279 tv->vval.v_partial->pt_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006280 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006281 case VAR_CHANNEL:
6282 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006283 Py_INCREF(Py_None);
6284 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006285 case VAR_SPECIAL:
6286 switch (tv->vval.v_number)
6287 {
6288 case VVAL_FALSE: return AlwaysFalse(NULL);
6289 case VVAL_TRUE: return AlwaysTrue(NULL);
6290 case VVAL_NONE:
6291 case VVAL_NULL: return AlwaysNone(NULL);
6292 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006293 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006294 return NULL;
6295 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006296 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006297}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006298
6299typedef struct
6300{
6301 PyObject_HEAD
6302} CurrentObject;
6303static PyTypeObject CurrentType;
6304
6305 static void
6306init_structs(void)
6307{
6308 vim_memset(&OutputType, 0, sizeof(OutputType));
6309 OutputType.tp_name = "vim.message";
6310 OutputType.tp_basicsize = sizeof(OutputObject);
6311 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6312 OutputType.tp_doc = "vim message object";
6313 OutputType.tp_methods = OutputMethods;
6314#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006315 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6316 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006317 OutputType.tp_alloc = call_PyType_GenericAlloc;
6318 OutputType.tp_new = call_PyType_GenericNew;
6319 OutputType.tp_free = call_PyObject_Free;
6320#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006321 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6322 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006323#endif
6324
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006325 vim_memset(&IterType, 0, sizeof(IterType));
6326 IterType.tp_name = "vim.iter";
6327 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006328 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006329 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006330 IterType.tp_iter = (getiterfunc)IterIter;
6331 IterType.tp_iternext = (iternextfunc)IterNext;
6332 IterType.tp_dealloc = (destructor)IterDestructor;
6333 IterType.tp_traverse = (traverseproc)IterTraverse;
6334 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006335
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006336 vim_memset(&BufferType, 0, sizeof(BufferType));
6337 BufferType.tp_name = "vim.buffer";
6338 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006339 BufferType.tp_dealloc = (destructor)BufferDestructor;
6340 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006341 BufferType.tp_as_sequence = &BufferAsSeq;
6342 BufferType.tp_as_mapping = &BufferAsMapping;
6343 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6344 BufferType.tp_doc = "vim buffer object";
6345 BufferType.tp_methods = BufferMethods;
6346#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006347 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006348 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006349 BufferType.tp_alloc = call_PyType_GenericAlloc;
6350 BufferType.tp_new = call_PyType_GenericNew;
6351 BufferType.tp_free = call_PyObject_Free;
6352#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006353 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006354 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006355#endif
6356
6357 vim_memset(&WindowType, 0, sizeof(WindowType));
6358 WindowType.tp_name = "vim.window";
6359 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006360 WindowType.tp_dealloc = (destructor)WindowDestructor;
6361 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006362 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006363 WindowType.tp_doc = "vim Window object";
6364 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006365 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6366 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006367#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006368 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6369 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006370 WindowType.tp_alloc = call_PyType_GenericAlloc;
6371 WindowType.tp_new = call_PyType_GenericNew;
6372 WindowType.tp_free = call_PyObject_Free;
6373#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006374 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6375 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006376#endif
6377
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006378 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6379 TabPageType.tp_name = "vim.tabpage";
6380 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006381 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6382 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006383 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6384 TabPageType.tp_doc = "vim tab page object";
6385 TabPageType.tp_methods = TabPageMethods;
6386#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006387 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006388 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6389 TabPageType.tp_new = call_PyType_GenericNew;
6390 TabPageType.tp_free = call_PyObject_Free;
6391#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006392 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006393#endif
6394
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006395 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6396 BufMapType.tp_name = "vim.bufferlist";
6397 BufMapType.tp_basicsize = sizeof(BufMapObject);
6398 BufMapType.tp_as_mapping = &BufMapAsMapping;
6399 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006400 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006401 BufferType.tp_doc = "vim buffer list";
6402
6403 vim_memset(&WinListType, 0, sizeof(WinListType));
6404 WinListType.tp_name = "vim.windowlist";
6405 WinListType.tp_basicsize = sizeof(WinListType);
6406 WinListType.tp_as_sequence = &WinListAsSeq;
6407 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6408 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006409 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006410
6411 vim_memset(&TabListType, 0, sizeof(TabListType));
6412 TabListType.tp_name = "vim.tabpagelist";
6413 TabListType.tp_basicsize = sizeof(TabListType);
6414 TabListType.tp_as_sequence = &TabListAsSeq;
6415 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6416 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006417
6418 vim_memset(&RangeType, 0, sizeof(RangeType));
6419 RangeType.tp_name = "vim.range";
6420 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006421 RangeType.tp_dealloc = (destructor)RangeDestructor;
6422 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006423 RangeType.tp_as_sequence = &RangeAsSeq;
6424 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006425 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006426 RangeType.tp_doc = "vim Range object";
6427 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006428 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6429 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006430#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006431 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006432 RangeType.tp_alloc = call_PyType_GenericAlloc;
6433 RangeType.tp_new = call_PyType_GenericNew;
6434 RangeType.tp_free = call_PyObject_Free;
6435#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006436 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006437#endif
6438
6439 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6440 CurrentType.tp_name = "vim.currentdata";
6441 CurrentType.tp_basicsize = sizeof(CurrentObject);
6442 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6443 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006444 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006445#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006446 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6447 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006448#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006449 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6450 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006451#endif
6452
6453 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6454 DictionaryType.tp_name = "vim.dictionary";
6455 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006456 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006457 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006458 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006459 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006460 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6461 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006462 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6463 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6464 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006465#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006466 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6467 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006468#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006469 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6470 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006471#endif
6472
6473 vim_memset(&ListType, 0, sizeof(ListType));
6474 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006475 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006476 ListType.tp_basicsize = sizeof(ListObject);
6477 ListType.tp_as_sequence = &ListAsSeq;
6478 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006479 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006480 ListType.tp_doc = "list pushing modifications to vim structure";
6481 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006482 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006483 ListType.tp_new = (newfunc)ListConstructor;
6484 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006485#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006486 ListType.tp_getattro = (getattrofunc)ListGetattro;
6487 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006488#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006489 ListType.tp_getattr = (getattrfunc)ListGetattr;
6490 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006491#endif
6492
6493 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006494 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006495 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006496 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6497 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006498 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006499 FunctionType.tp_doc = "object that calls vim function";
6500 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006501 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006502 FunctionType.tp_new = (newfunc)FunctionConstructor;
6503 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006504#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006505 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006506#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006507 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006508#endif
6509
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006510 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6511 OptionsType.tp_name = "vim.options";
6512 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006513 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006514 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006515 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006516 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006517 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006518 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6519 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6520 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006521
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006522 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6523 LoaderType.tp_name = "vim.Loader";
6524 LoaderType.tp_basicsize = sizeof(LoaderObject);
6525 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6526 LoaderType.tp_doc = "vim message object";
6527 LoaderType.tp_methods = LoaderMethods;
6528 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6529
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006530#if PY_MAJOR_VERSION >= 3
6531 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6532 vimmodule.m_name = "vim";
6533 vimmodule.m_doc = "Vim Python interface\n";
6534 vimmodule.m_size = -1;
6535 vimmodule.m_methods = VimMethods;
6536#endif
6537}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006538
6539#define PYTYPE_READY(type) \
6540 if (PyType_Ready(&type)) \
6541 return -1;
6542
6543 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006544init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006545{
6546 PYTYPE_READY(IterType);
6547 PYTYPE_READY(BufferType);
6548 PYTYPE_READY(RangeType);
6549 PYTYPE_READY(WindowType);
6550 PYTYPE_READY(TabPageType);
6551 PYTYPE_READY(BufMapType);
6552 PYTYPE_READY(WinListType);
6553 PYTYPE_READY(TabListType);
6554 PYTYPE_READY(CurrentType);
6555 PYTYPE_READY(DictionaryType);
6556 PYTYPE_READY(ListType);
6557 PYTYPE_READY(FunctionType);
6558 PYTYPE_READY(OptionsType);
6559 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006560 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006561 return 0;
6562}
6563
6564 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006565init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006566{
6567 PyObject *path;
6568 PyObject *path_hook;
6569 PyObject *path_hooks;
6570
6571 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6572 return -1;
6573
6574 if (!(path_hooks = PySys_GetObject("path_hooks")))
6575 {
6576 PyErr_Clear();
6577 path_hooks = PyList_New(1);
6578 PyList_SET_ITEM(path_hooks, 0, path_hook);
6579 if (PySys_SetObject("path_hooks", path_hooks))
6580 {
6581 Py_DECREF(path_hooks);
6582 return -1;
6583 }
6584 Py_DECREF(path_hooks);
6585 }
6586 else if (PyList_Check(path_hooks))
6587 {
6588 if (PyList_Append(path_hooks, path_hook))
6589 {
6590 Py_DECREF(path_hook);
6591 return -1;
6592 }
6593 Py_DECREF(path_hook);
6594 }
6595 else
6596 {
6597 VimTryStart();
6598 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6599 "You should now do the following:\n"
6600 "- append vim.path_hook to sys.path_hooks\n"
6601 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6602 VimTryEnd(); /* Discard the error */
6603 Py_DECREF(path_hook);
6604 return 0;
6605 }
6606
6607 if (!(path = PySys_GetObject("path")))
6608 {
6609 PyErr_Clear();
6610 path = PyList_New(1);
6611 Py_INCREF(vim_special_path_object);
6612 PyList_SET_ITEM(path, 0, vim_special_path_object);
6613 if (PySys_SetObject("path", path))
6614 {
6615 Py_DECREF(path);
6616 return -1;
6617 }
6618 Py_DECREF(path);
6619 }
6620 else if (PyList_Check(path))
6621 {
6622 if (PyList_Append(path, vim_special_path_object))
6623 return -1;
6624 }
6625 else
6626 {
6627 VimTryStart();
6628 EMSG(_("Failed to set path: sys.path is not a list\n"
6629 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6630 VimTryEnd(); /* Discard the error */
6631 }
6632
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006633 return 0;
6634}
6635
6636static BufMapObject TheBufferMap =
6637{
6638 PyObject_HEAD_INIT(&BufMapType)
6639};
6640
6641static WinListObject TheWindowList =
6642{
6643 PyObject_HEAD_INIT(&WinListType)
6644 NULL
6645};
6646
6647static CurrentObject TheCurrent =
6648{
6649 PyObject_HEAD_INIT(&CurrentType)
6650};
6651
6652static TabListObject TheTabPageList =
6653{
6654 PyObject_HEAD_INIT(&TabListType)
6655};
6656
6657static struct numeric_constant {
6658 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006659 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006660} numeric_constants[] = {
6661 {"VAR_LOCKED", VAR_LOCKED},
6662 {"VAR_FIXED", VAR_FIXED},
6663 {"VAR_SCOPE", VAR_SCOPE},
6664 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6665};
6666
6667static struct object_constant {
6668 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006669 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006670} object_constants[] = {
6671 {"buffers", (PyObject *)(void *)&TheBufferMap},
6672 {"windows", (PyObject *)(void *)&TheWindowList},
6673 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6674 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006675
6676 {"Buffer", (PyObject *)&BufferType},
6677 {"Range", (PyObject *)&RangeType},
6678 {"Window", (PyObject *)&WindowType},
6679 {"TabPage", (PyObject *)&TabPageType},
6680 {"Dictionary", (PyObject *)&DictionaryType},
6681 {"List", (PyObject *)&ListType},
6682 {"Function", (PyObject *)&FunctionType},
6683 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006684 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006685};
6686
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006687#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006688 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006689 return -1;
6690
6691#define ADD_CHECKED_OBJECT(m, name, obj) \
6692 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006693 PyObject *valObject = obj; \
6694 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006695 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006696 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006697 }
6698
6699 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006700populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006701{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006702 int i;
6703 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006704 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006705 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006706
6707 for (i = 0; i < (int)(sizeof(numeric_constants)
6708 / sizeof(struct numeric_constant));
6709 ++i)
6710 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006711 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006712
6713 for (i = 0; i < (int)(sizeof(object_constants)
6714 / sizeof(struct object_constant));
6715 ++i)
6716 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006717 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006718
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006719 valObject = object_constants[i].valObject;
6720 Py_INCREF(valObject);
6721 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006722 }
6723
6724 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6725 return -1;
6726 ADD_OBJECT(m, "error", VimError);
6727
Bram Moolenaara9922d62013-05-30 13:01:18 +02006728 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6729 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006730 ADD_CHECKED_OBJECT(m, "options",
6731 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006732
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006733 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006734 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006735 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006736
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006737 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006738 return -1;
6739 ADD_OBJECT(m, "_getcwd", py_getcwd)
6740
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006741 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006742 return -1;
6743 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006744 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006745 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006746 if (PyObject_SetAttrString(other_module, "chdir", attr))
6747 {
6748 Py_DECREF(attr);
6749 return -1;
6750 }
6751 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006752
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006753 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006754 {
6755 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006756 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006757 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006758 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6759 {
6760 Py_DECREF(attr);
6761 return -1;
6762 }
6763 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006764 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006765 else
6766 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006767
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006768 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6769 return -1;
6770
6771 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6772
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006773 if (!(imp = PyImport_ImportModule("imp")))
6774 return -1;
6775
6776 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6777 {
6778 Py_DECREF(imp);
6779 return -1;
6780 }
6781
6782 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6783 {
6784 Py_DECREF(py_find_module);
6785 Py_DECREF(imp);
6786 return -1;
6787 }
6788
6789 Py_DECREF(imp);
6790
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006791 ADD_OBJECT(m, "_find_module", py_find_module);
6792 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006793
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006794 return 0;
6795}