blob: de3e8680b573cc93d8daf3a0260a6fb2aec1ab7d [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);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02002925 if (argv == NULL)
2926 {
2927 PyErr_NoMemory();
2928 dict_unref(selfdict);
2929 list_unref(argslist);
2930 return NULL;
2931 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02002932 curtv = argv;
2933 for (li = argslist->lv_first; li != NULL; li = li->li_next)
2934 copy_tv(&li->li_tv, curtv++);
2935 }
2936 list_unref(argslist);
2937 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002938 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002939
Bram Moolenaar389a1792013-06-23 13:00:44 +02002940 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02002941 {
2942 dict_unref(selfdict);
2943 while (argc--)
2944 clear_tv(&argv[argc]);
2945 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002946 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002947 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002948
Bram Moolenaar8110a092016-04-14 15:56:09 +02002949 self = FunctionNew(subtype, name, argc, argv, selfdict);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002950
Bram Moolenaar389a1792013-06-23 13:00:44 +02002951 PyMem_Free(name);
2952
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002953 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002954}
2955
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002956 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002957FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002958{
Bram Moolenaar8110a092016-04-14 15:56:09 +02002959 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002960 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002961 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002962 for (i = 0; i < self->argc; ++i)
2963 clear_tv(&self->argv[i]);
2964 PyMem_Free(self->argv);
2965 dict_unref(self->self);
2966 if (self->argv || self->self)
2967 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002968
2969 DESTRUCTOR_FINISH(self);
2970}
2971
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002972static char *FunctionAttrs[] = {
Bram Moolenaar8110a092016-04-14 15:56:09 +02002973 "softspace", "args", "self",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002974 NULL
2975};
2976
2977 static PyObject *
2978FunctionDir(PyObject *self)
2979{
2980 return ObjectDir(self, FunctionAttrs);
2981}
2982
Bram Moolenaardb913952012-06-29 12:54:53 +02002983 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002984FunctionAttr(FunctionObject *self, char *name)
2985{
2986 list_T *list;
2987 int i;
2988 if (strcmp(name, "name") == 0)
2989 return PyString_FromString((char *)(self->name));
2990 else if (strcmp(name, "args") == 0)
2991 {
2992 if (self->argv == NULL)
2993 return AlwaysNone(NULL);
2994 list = list_alloc();
2995 for (i = 0; i < self->argc; ++i)
2996 list_append_tv(list, &self->argv[i]);
2997 return NEW_LIST(list);
2998 }
2999 else if (strcmp(name, "self") == 0)
3000 return self->self == NULL
3001 ? AlwaysNone(NULL)
3002 : NEW_DICTIONARY(self->self);
3003 else if (strcmp(name, "__members__") == 0)
3004 return ObjectDir(NULL, FunctionAttrs);
3005 return NULL;
3006}
3007
3008/* Populate partial_T given function object.
3009 *
3010 * "exported" should be set to true when it is needed to construct a partial
3011 * that may be stored in a variable (i.e. may be freed by Vim).
3012 */
3013 static void
3014set_partial(FunctionObject *self, partial_T *pt, int exported)
3015{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003016 int i;
3017
3018 pt->pt_name = self->name;
3019 if (self->argv)
3020 {
3021 pt->pt_argc = self->argc;
3022 if (exported)
3023 {
3024 pt->pt_argv = (typval_T *)alloc_clear(
3025 sizeof(typval_T) * self->argc);
3026 for (i = 0; i < pt->pt_argc; ++i)
3027 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3028 }
3029 else
3030 pt->pt_argv = self->argv;
3031 }
3032 else
3033 {
3034 pt->pt_argc = 0;
3035 pt->pt_argv = NULL;
3036 }
3037 pt->pt_dict = self->self;
3038 if (exported && self->self)
3039 ++pt->pt_dict->dv_refcount;
3040 if (exported)
3041 pt->pt_name = vim_strsave(pt->pt_name);
3042 pt->pt_refcount = 1;
3043}
3044
3045 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003046FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003047{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003048 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003049 typval_T args;
3050 typval_T selfdicttv;
3051 typval_T rettv;
3052 dict_T *selfdict = NULL;
3053 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003054 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003055 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003056 partial_T pt;
3057 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003058
Bram Moolenaar8110a092016-04-14 15:56:09 +02003059 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003060 return NULL;
3061
3062 if (kwargs != NULL)
3063 {
3064 selfdictObject = PyDict_GetItemString(kwargs, "self");
3065 if (selfdictObject != NULL)
3066 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003067 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003068 {
3069 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003070 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003071 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003072 selfdict = selfdicttv.vval.v_dict;
3073 }
3074 }
3075
Bram Moolenaar8110a092016-04-14 15:56:09 +02003076 if (self->argv || self->self)
3077 {
3078 set_partial(self, &pt, FALSE);
3079 pt_ptr = &pt;
3080 }
3081
Bram Moolenaar71700b82013-05-15 17:49:05 +02003082 Py_BEGIN_ALLOW_THREADS
3083 Python_Lock_Vim();
3084
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003085 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003086 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003087
3088 Python_Release_Vim();
3089 Py_END_ALLOW_THREADS
3090
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003091 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003092 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003093 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003094 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003095 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003096 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003097 }
3098 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003099 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003100
Bram Moolenaardb913952012-06-29 12:54:53 +02003101 clear_tv(&args);
3102 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003103 if (selfdict != NULL)
3104 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003105
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003106 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003107}
3108
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003109 static PyObject *
3110FunctionRepr(FunctionObject *self)
3111{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003112 PyObject *ret;
3113 garray_T repr_ga;
3114 int i;
3115 char_u *tofree = NULL;
3116 typval_T tv;
3117 char_u numbuf[NUMBUFLEN];
3118
3119 ga_init2(&repr_ga, (int)sizeof(char), 70);
3120 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3121 if (self->name)
3122 ga_concat(&repr_ga, self->name);
3123 else
3124 ga_concat(&repr_ga, (char_u *)"<NULL>");
3125 ga_append(&repr_ga, '\'');
3126 if (self->argv)
3127 {
3128 ga_concat(&repr_ga, (char_u *)", args=[");
3129 ++emsg_silent;
3130 for (i = 0; i < self->argc; i++)
3131 {
3132 if (i != 0)
3133 ga_concat(&repr_ga, (char_u *)", ");
3134 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3135 get_copyID()));
3136 vim_free(tofree);
3137 }
3138 --emsg_silent;
3139 ga_append(&repr_ga, ']');
3140 }
3141 if (self->self)
3142 {
3143 ga_concat(&repr_ga, (char_u *)", self=");
3144 tv.v_type = VAR_DICT;
3145 tv.vval.v_dict = self->self;
3146 ++emsg_silent;
3147 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3148 --emsg_silent;
3149 vim_free(tofree);
3150 }
3151 ga_append(&repr_ga, '>');
3152 ret = PyString_FromString((char *)repr_ga.ga_data);
3153 ga_clear(&repr_ga);
3154 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003155}
3156
Bram Moolenaardb913952012-06-29 12:54:53 +02003157static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003158 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3159 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003160};
3161
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003162/*
3163 * Options object
3164 */
3165
3166static PyTypeObject OptionsType;
3167
3168typedef int (*checkfun)(void *);
3169
3170typedef struct
3171{
3172 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003173 int opt_type;
3174 void *from;
3175 checkfun Check;
3176 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003177} OptionsObject;
3178
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003179 static int
3180dummy_check(void *arg UNUSED)
3181{
3182 return 0;
3183}
3184
3185 static PyObject *
3186OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3187{
3188 OptionsObject *self;
3189
Bram Moolenaar774267b2013-05-21 20:51:59 +02003190 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003191 if (self == NULL)
3192 return NULL;
3193
3194 self->opt_type = opt_type;
3195 self->from = from;
3196 self->Check = Check;
3197 self->fromObj = fromObj;
3198 if (fromObj)
3199 Py_INCREF(fromObj);
3200
3201 return (PyObject *)(self);
3202}
3203
3204 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003205OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003206{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003207 PyObject_GC_UnTrack((void *)(self));
3208 Py_XDECREF(self->fromObj);
3209 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003210}
3211
3212 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003213OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003214{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003215 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003216 return 0;
3217}
3218
3219 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003220OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003221{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003222 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003223 return 0;
3224}
3225
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003226 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003227OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003228{
3229 char_u *key;
3230 int flags;
3231 long numval;
3232 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003233 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003234
Bram Moolenaard6e39182013-05-21 18:30:34 +02003235 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003236 return NULL;
3237
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003238 if (!(key = StringToChars(keyObject, &todecref)))
3239 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003240
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003241 if (*key == NUL)
3242 {
3243 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003244 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003245 return NULL;
3246 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003247
3248 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003249 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003250
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003251 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003252
3253 if (flags == 0)
3254 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003255 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003256 return NULL;
3257 }
3258
3259 if (flags & SOPT_UNSET)
3260 {
3261 Py_INCREF(Py_None);
3262 return Py_None;
3263 }
3264 else if (flags & SOPT_BOOL)
3265 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003266 PyObject *ret;
3267 ret = numval ? Py_True : Py_False;
3268 Py_INCREF(ret);
3269 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003270 }
3271 else if (flags & SOPT_NUM)
3272 return PyInt_FromLong(numval);
3273 else if (flags & SOPT_STRING)
3274 {
3275 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003276 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003277 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003278 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003279 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003280 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003281 else
3282 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003283 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003284 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003285 return NULL;
3286 }
3287 }
3288 else
3289 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003290 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003291 return NULL;
3292 }
3293}
3294
3295 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003296OptionsContains(OptionsObject *self, PyObject *keyObject)
3297{
3298 char_u *key;
3299 PyObject *todecref;
3300
3301 if (!(key = StringToChars(keyObject, &todecref)))
3302 return -1;
3303
3304 if (*key == NUL)
3305 {
3306 Py_XDECREF(todecref);
3307 return 0;
3308 }
3309
3310 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3311 {
3312 Py_XDECREF(todecref);
3313 return 1;
3314 }
3315 else
3316 {
3317 Py_XDECREF(todecref);
3318 return 0;
3319 }
3320}
3321
3322typedef struct
3323{
3324 void *lastoption;
3325 int opt_type;
3326} optiterinfo_T;
3327
3328 static PyObject *
3329OptionsIterNext(optiterinfo_T **oii)
3330{
3331 char_u *name;
3332
3333 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3334 return PyString_FromString((char *)name);
3335
3336 return NULL;
3337}
3338
3339 static PyObject *
3340OptionsIter(OptionsObject *self)
3341{
3342 optiterinfo_T *oii;
3343
3344 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3345 {
3346 PyErr_NoMemory();
3347 return NULL;
3348 }
3349
3350 oii->opt_type = self->opt_type;
3351 oii->lastoption = NULL;
3352
3353 return IterNew(oii,
3354 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3355 NULL, NULL);
3356}
3357
3358 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003359set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003360{
3361 char_u *errmsg;
3362
3363 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3364 {
3365 if (VimTryEnd())
3366 return FAIL;
3367 PyErr_SetVim((char *)errmsg);
3368 return FAIL;
3369 }
3370 return OK;
3371}
3372
3373 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003374set_option_value_for(
3375 char_u *key,
3376 int numval,
3377 char_u *stringval,
3378 int opt_flags,
3379 int opt_type,
3380 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003381{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003382 win_T *save_curwin = NULL;
3383 tabpage_T *save_curtab = NULL;
3384 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003385 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003386
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003387 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003388 switch (opt_type)
3389 {
3390 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003391 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003392 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003393 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003394 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003395 if (VimTryEnd())
3396 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003397 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003398 return -1;
3399 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003400 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3401 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003402 break;
3403 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003404 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003405 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003406 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003407 break;
3408 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003409 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003410 break;
3411 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003412 if (set_ret == FAIL)
3413 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003414 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003415}
3416
3417 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003418OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003419{
3420 char_u *key;
3421 int flags;
3422 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003423 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003424 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003425
Bram Moolenaard6e39182013-05-21 18:30:34 +02003426 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003427 return -1;
3428
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003429 if (!(key = StringToChars(keyObject, &todecref)))
3430 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003431
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003432 if (*key == NUL)
3433 {
3434 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003435 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003436 return -1;
3437 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003438
3439 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003440 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003441
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003442 if (flags == 0)
3443 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003444 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003445 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003446 return -1;
3447 }
3448
3449 if (valObject == NULL)
3450 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003451 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003452 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003453 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003454 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003455 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003456 return -1;
3457 }
3458 else if (!(flags & SOPT_GLOBAL))
3459 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003460 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003461 N_("unable to unset option %s "
3462 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003463 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003464 return -1;
3465 }
3466 else
3467 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003468 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003469 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003470 return 0;
3471 }
3472 }
3473
Bram Moolenaard6e39182013-05-21 18:30:34 +02003474 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003475
3476 if (flags & SOPT_BOOL)
3477 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003478 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003479
Bram Moolenaarb983f752013-05-15 16:11:50 +02003480 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003481 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003482 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003483 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003484 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003485 }
3486 else if (flags & SOPT_NUM)
3487 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003488 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003489
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003490 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003491 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003492 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003493 return -1;
3494 }
3495
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003496 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003497 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003498 }
3499 else
3500 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003501 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003502 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003503
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003504 if ((val = StringToChars(valObject, &todecref2)))
3505 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003506 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003507 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003508 Py_XDECREF(todecref2);
3509 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003510 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003511 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003512 }
3513
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003514 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003515
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003516 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003517}
3518
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003519static PySequenceMethods OptionsAsSeq = {
3520 0, /* sq_length */
3521 0, /* sq_concat */
3522 0, /* sq_repeat */
3523 0, /* sq_item */
3524 0, /* sq_slice */
3525 0, /* sq_ass_item */
3526 0, /* sq_ass_slice */
3527 (objobjproc) OptionsContains, /* sq_contains */
3528 0, /* sq_inplace_concat */
3529 0, /* sq_inplace_repeat */
3530};
3531
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003532static PyMappingMethods OptionsAsMapping = {
3533 (lenfunc) NULL,
3534 (binaryfunc) OptionsItem,
3535 (objobjargproc) OptionsAssItem,
3536};
3537
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003538/* Tabpage object
3539 */
3540
3541typedef struct
3542{
3543 PyObject_HEAD
3544 tabpage_T *tab;
3545} TabPageObject;
3546
3547static PyObject *WinListNew(TabPageObject *tabObject);
3548
3549static PyTypeObject TabPageType;
3550
3551 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003552CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003553{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003554 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003555 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003556 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003557 return -1;
3558 }
3559
3560 return 0;
3561}
3562
3563 static PyObject *
3564TabPageNew(tabpage_T *tab)
3565{
3566 TabPageObject *self;
3567
3568 if (TAB_PYTHON_REF(tab))
3569 {
3570 self = TAB_PYTHON_REF(tab);
3571 Py_INCREF(self);
3572 }
3573 else
3574 {
3575 self = PyObject_NEW(TabPageObject, &TabPageType);
3576 if (self == NULL)
3577 return NULL;
3578 self->tab = tab;
3579 TAB_PYTHON_REF(tab) = self;
3580 }
3581
3582 return (PyObject *)(self);
3583}
3584
3585 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003586TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003587{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003588 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3589 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003590
3591 DESTRUCTOR_FINISH(self);
3592}
3593
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003594static char *TabPageAttrs[] = {
3595 "windows", "number", "vars", "window", "valid",
3596 NULL
3597};
3598
3599 static PyObject *
3600TabPageDir(PyObject *self)
3601{
3602 return ObjectDir(self, TabPageAttrs);
3603}
3604
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003605 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003606TabPageAttrValid(TabPageObject *self, char *name)
3607{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003608 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003609
3610 if (strcmp(name, "valid") != 0)
3611 return NULL;
3612
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003613 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3614 Py_INCREF(ret);
3615 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003616}
3617
3618 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003619TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003620{
3621 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003622 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003623 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003624 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003625 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003626 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003627 else if (strcmp(name, "window") == 0)
3628 {
3629 /* For current tab window.c does not bother to set or update tp_curwin
3630 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003631 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003632 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003633 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003634 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003635 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003636 else if (strcmp(name, "__members__") == 0)
3637 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003638 return NULL;
3639}
3640
3641 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003642TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003643{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003644 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003645 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003646 else
3647 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003648 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003649
3650 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003651 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3652 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003653 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003654 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003655 }
3656}
3657
3658static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003659 /* name, function, calling, documentation */
3660 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3661 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003662};
3663
3664/*
3665 * Window list object
3666 */
3667
3668static PyTypeObject TabListType;
3669static PySequenceMethods TabListAsSeq;
3670
3671typedef struct
3672{
3673 PyObject_HEAD
3674} TabListObject;
3675
3676 static PyInt
3677TabListLength(PyObject *self UNUSED)
3678{
3679 tabpage_T *tp = first_tabpage;
3680 PyInt n = 0;
3681
3682 while (tp != NULL)
3683 {
3684 ++n;
3685 tp = tp->tp_next;
3686 }
3687
3688 return n;
3689}
3690
3691 static PyObject *
3692TabListItem(PyObject *self UNUSED, PyInt n)
3693{
3694 tabpage_T *tp;
3695
3696 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3697 if (n == 0)
3698 return TabPageNew(tp);
3699
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003700 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003701 return NULL;
3702}
3703
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003704/*
3705 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003706 */
3707
3708typedef struct
3709{
3710 PyObject_HEAD
3711 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003712 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003713} WindowObject;
3714
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003715static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003716
3717 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003718CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003719{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003720 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003721 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003722 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003723 return -1;
3724 }
3725
3726 return 0;
3727}
3728
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003729 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003730WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003731{
3732 /* We need to handle deletion of windows underneath us.
3733 * If we add a "w_python*_ref" field to the win_T structure,
3734 * then we can get at it in win_free() in vim. We then
3735 * need to create only ONE Python object per window - if
3736 * we try to create a second, just INCREF the existing one
3737 * and return it. The (single) Python object referring to
3738 * the window is stored in "w_python*_ref".
3739 * On a win_free() we set the Python object's win_T* field
3740 * to an invalid value. We trap all uses of a window
3741 * object, and reject them if the win_T* field is invalid.
3742 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003743 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003744 * w_python_ref and w_python3_ref fields respectively.
3745 */
3746
3747 WindowObject *self;
3748
3749 if (WIN_PYTHON_REF(win))
3750 {
3751 self = WIN_PYTHON_REF(win);
3752 Py_INCREF(self);
3753 }
3754 else
3755 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003756 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003757 if (self == NULL)
3758 return NULL;
3759 self->win = win;
3760 WIN_PYTHON_REF(win) = self;
3761 }
3762
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003763 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3764
Bram Moolenaar971db462013-05-12 18:44:48 +02003765 return (PyObject *)(self);
3766}
3767
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003768 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003769WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003770{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003771 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003772 if (self->win && self->win != INVALID_WINDOW_VALUE)
3773 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003774 Py_XDECREF(((PyObject *)(self->tabObject)));
3775 PyObject_GC_Del((void *)(self));
3776}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003777
Bram Moolenaar774267b2013-05-21 20:51:59 +02003778 static int
3779WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3780{
3781 Py_VISIT(((PyObject *)(self->tabObject)));
3782 return 0;
3783}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003784
Bram Moolenaar774267b2013-05-21 20:51:59 +02003785 static int
3786WindowClear(WindowObject *self)
3787{
3788 Py_CLEAR(self->tabObject);
3789 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003790}
3791
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003792 static win_T *
3793get_firstwin(TabPageObject *tabObject)
3794{
3795 if (tabObject)
3796 {
3797 if (CheckTabPage(tabObject))
3798 return NULL;
3799 /* For current tab window.c does not bother to set or update tp_firstwin
3800 */
3801 else if (tabObject->tab == curtab)
3802 return firstwin;
3803 else
3804 return tabObject->tab->tp_firstwin;
3805 }
3806 else
3807 return firstwin;
3808}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003809static char *WindowAttrs[] = {
3810 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3811 "tabpage", "valid",
3812 NULL
3813};
3814
3815 static PyObject *
3816WindowDir(PyObject *self)
3817{
3818 return ObjectDir(self, WindowAttrs);
3819}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003820
Bram Moolenaar971db462013-05-12 18:44:48 +02003821 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003822WindowAttrValid(WindowObject *self, char *name)
3823{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003824 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003825
3826 if (strcmp(name, "valid") != 0)
3827 return NULL;
3828
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003829 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3830 Py_INCREF(ret);
3831 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003832}
3833
3834 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003835WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003836{
3837 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003838 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003839 else if (strcmp(name, "cursor") == 0)
3840 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003841 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003842
3843 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3844 }
3845 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003846 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003847#ifdef FEAT_WINDOWS
3848 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003849 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003850 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003851 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003852 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003853 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003854#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003855 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003856 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003857 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003858 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3859 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003860 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003861 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003862 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003863 return NULL;
3864 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003865 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003866 }
3867 else if (strcmp(name, "tabpage") == 0)
3868 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003869 Py_INCREF(self->tabObject);
3870 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003871 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003872 else if (strcmp(name, "__members__") == 0)
3873 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003874 else
3875 return NULL;
3876}
3877
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003878 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003879WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003880{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003881 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003882 return -1;
3883
3884 if (strcmp(name, "buffer") == 0)
3885 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003886 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003887 return -1;
3888 }
3889 else if (strcmp(name, "cursor") == 0)
3890 {
3891 long lnum;
3892 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003893
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003894 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003895 return -1;
3896
Bram Moolenaard6e39182013-05-21 18:30:34 +02003897 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003898 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003899 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003900 return -1;
3901 }
3902
3903 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003904 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003905 return -1;
3906
Bram Moolenaard6e39182013-05-21 18:30:34 +02003907 self->win->w_cursor.lnum = lnum;
3908 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003909#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003910 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003911#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003912 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003913 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003914
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003915 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003916 return 0;
3917 }
3918 else if (strcmp(name, "height") == 0)
3919 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003920 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003921 win_T *savewin;
3922
Bram Moolenaardee2e312013-06-23 16:35:47 +02003923 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003924 return -1;
3925
3926#ifdef FEAT_GUI
3927 need_mouse_correct = TRUE;
3928#endif
3929 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003930 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003931
3932 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003933 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003934 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003935 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003936 return -1;
3937
3938 return 0;
3939 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003940#ifdef FEAT_WINDOWS
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003941 else if (strcmp(name, "width") == 0)
3942 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003943 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003944 win_T *savewin;
3945
Bram Moolenaardee2e312013-06-23 16:35:47 +02003946 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003947 return -1;
3948
3949#ifdef FEAT_GUI
3950 need_mouse_correct = TRUE;
3951#endif
3952 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003953 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003954
3955 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003956 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003957 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003958 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003959 return -1;
3960
3961 return 0;
3962 }
3963#endif
3964 else
3965 {
3966 PyErr_SetString(PyExc_AttributeError, name);
3967 return -1;
3968 }
3969}
3970
3971 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003972WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003973{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003974 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003975 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003976 else
3977 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003978 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003979
Bram Moolenaar6d216452013-05-12 19:00:41 +02003980 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003981 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003982 (self));
3983 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003984 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003985 }
3986}
3987
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003988static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003989 /* name, function, calling, documentation */
3990 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3991 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003992};
3993
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003994/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003995 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003996 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003997
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003998static PyTypeObject WinListType;
3999static PySequenceMethods WinListAsSeq;
4000
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004001typedef struct
4002{
4003 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004004 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004005} WinListObject;
4006
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004007 static PyObject *
4008WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004009{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004010 WinListObject *self;
4011
4012 self = PyObject_NEW(WinListObject, &WinListType);
4013 self->tabObject = tabObject;
4014 Py_INCREF(tabObject);
4015
4016 return (PyObject *)(self);
4017}
4018
4019 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004020WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004021{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004022 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004023
4024 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004025 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004026 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004027 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004028
4029 DESTRUCTOR_FINISH(self);
4030}
4031
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004032 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004033WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004034{
4035 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004036 PyInt n = 0;
4037
Bram Moolenaard6e39182013-05-21 18:30:34 +02004038 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004039 return -1;
4040
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004041 while (w != NULL)
4042 {
4043 ++n;
4044 w = W_NEXT(w);
4045 }
4046
4047 return n;
4048}
4049
4050 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004051WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052{
4053 win_T *w;
4054
Bram Moolenaard6e39182013-05-21 18:30:34 +02004055 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004056 return NULL;
4057
4058 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004059 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004060 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004061
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004062 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004063 return NULL;
4064}
4065
4066/* Convert a Python string into a Vim line.
4067 *
4068 * The result is in allocated memory. All internal nulls are replaced by
4069 * newline characters. It is an error for the string to contain newline
4070 * characters.
4071 *
4072 * On errors, the Python exception data is set, and NULL is returned.
4073 */
4074 static char *
4075StringToLine(PyObject *obj)
4076{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004077 char *str;
4078 char *save;
4079 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004080 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004081 PyInt i;
4082 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004083
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004084 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004086 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4087 || str == NULL)
4088 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004089 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004090 else if (PyUnicode_Check(obj))
4091 {
4092 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4093 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004094
Bram Moolenaardaa27022013-06-24 22:33:30 +02004095 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004096 || str == NULL)
4097 {
4098 Py_DECREF(bytes);
4099 return NULL;
4100 }
4101 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004102 else
4103 {
4104#if PY_MAJOR_VERSION < 3
4105 PyErr_FORMAT(PyExc_TypeError,
4106 N_("expected str() or unicode() instance, but got %s"),
4107 Py_TYPE_NAME(obj));
4108#else
4109 PyErr_FORMAT(PyExc_TypeError,
4110 N_("expected bytes() or str() instance, but got %s"),
4111 Py_TYPE_NAME(obj));
4112#endif
4113 return NULL;
4114 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004115
4116 /*
4117 * Error checking: String must not contain newlines, as we
4118 * are replacing a single line, and we must replace it with
4119 * a single line.
4120 * A trailing newline is removed, so that append(f.readlines()) works.
4121 */
4122 p = memchr(str, '\n', len);
4123 if (p != NULL)
4124 {
4125 if (p == str + len - 1)
4126 --len;
4127 else
4128 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004129 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004130 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004131 return NULL;
4132 }
4133 }
4134
4135 /* Create a copy of the string, with internal nulls replaced by
4136 * newline characters, as is the vim convention.
4137 */
4138 save = (char *)alloc((unsigned)(len+1));
4139 if (save == NULL)
4140 {
4141 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004142 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004143 return NULL;
4144 }
4145
4146 for (i = 0; i < len; ++i)
4147 {
4148 if (str[i] == '\0')
4149 save[i] = '\n';
4150 else
4151 save[i] = str[i];
4152 }
4153
4154 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004155 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004156
4157 return save;
4158}
4159
4160/* Get a line from the specified buffer. The line number is
4161 * in Vim format (1-based). The line is returned as a Python
4162 * string object.
4163 */
4164 static PyObject *
4165GetBufferLine(buf_T *buf, PyInt n)
4166{
4167 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4168}
4169
4170
4171/* Get a list of lines from the specified buffer. The line numbers
4172 * are in Vim format (1-based). The range is from lo up to, but not
4173 * including, hi. The list is returned as a Python list of string objects.
4174 */
4175 static PyObject *
4176GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4177{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004178 PyInt i;
4179 PyInt n = hi - lo;
4180 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004181
4182 if (list == NULL)
4183 return NULL;
4184
4185 for (i = 0; i < n; ++i)
4186 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004187 PyObject *string = LineToString(
4188 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004189
4190 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004191 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004192 {
4193 Py_DECREF(list);
4194 return NULL;
4195 }
4196
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004197 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004198 }
4199
4200 /* The ownership of the Python list is passed to the caller (ie,
4201 * the caller should Py_DECREF() the object when it is finished
4202 * with it).
4203 */
4204
4205 return list;
4206}
4207
4208/*
4209 * Check if deleting lines made the cursor position invalid.
4210 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4211 * deleted).
4212 */
4213 static void
4214py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4215{
4216 if (curwin->w_cursor.lnum >= lo)
4217 {
4218 /* Adjust the cursor position if it's in/after the changed
4219 * lines. */
4220 if (curwin->w_cursor.lnum >= hi)
4221 {
4222 curwin->w_cursor.lnum += extra;
4223 check_cursor_col();
4224 }
4225 else if (extra < 0)
4226 {
4227 curwin->w_cursor.lnum = lo;
4228 check_cursor();
4229 }
4230 else
4231 check_cursor_col();
4232 changed_cline_bef_curs();
4233 }
4234 invalidate_botline();
4235}
4236
Bram Moolenaar19e60942011-06-19 00:27:51 +02004237/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004238 * Find a window that contains "buf" and switch to it.
4239 * If there is no such window, use the current window and change "curbuf".
4240 * Caller must initialize save_curbuf to NULL.
4241 * restore_win_for_buf() MUST be called later!
4242 */
4243 static void
4244switch_to_win_for_buf(
4245 buf_T *buf,
4246 win_T **save_curwinp,
4247 tabpage_T **save_curtabp,
4248 buf_T **save_curbufp)
4249{
4250 win_T *wp;
4251 tabpage_T *tp;
4252
Bram Moolenaarae38d052014-12-17 14:46:09 +01004253 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004254 switch_buffer(save_curbufp, buf);
Bram Moolenaarae38d052014-12-17 14:46:09 +01004255 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4256 {
4257 restore_win(*save_curwinp, *save_curtabp, TRUE);
4258 switch_buffer(save_curbufp, buf);
4259 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004260}
4261
4262 static void
4263restore_win_for_buf(
4264 win_T *save_curwin,
4265 tabpage_T *save_curtab,
4266 buf_T *save_curbuf)
4267{
4268 if (save_curbuf == NULL)
4269 restore_win(save_curwin, save_curtab, TRUE);
4270 else
4271 restore_buffer(save_curbuf);
4272}
4273
4274/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004275 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004276 * in Vim format (1-based). The replacement line is given as
4277 * a Python string object. The object is checked for validity
4278 * and correct format. Errors are returned as a value of FAIL.
4279 * The return value is OK on success.
4280 * If OK is returned and len_change is not NULL, *len_change
4281 * is set to the change in the buffer length.
4282 */
4283 static int
4284SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4285{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004286 buf_T *save_curbuf = NULL;
4287 win_T *save_curwin = NULL;
4288 tabpage_T *save_curtab = NULL;
4289
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004290 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004291 * There are three cases:
4292 * 1. NULL, or None - this is a deletion.
4293 * 2. A string - this is a replacement.
4294 * 3. Anything else - this is an error.
4295 */
4296 if (line == Py_None || line == NULL)
4297 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004298 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004299 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004300
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004301 VimTryStart();
4302
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004303 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004304 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004305 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004306 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004307 else
4308 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004309 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004310 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004311 if (save_curbuf == NULL)
4312 /* Only adjust marks if we managed to switch to a window that
4313 * holds the buffer, otherwise line numbers will be invalid. */
4314 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004315 }
4316
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004317 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004318
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004319 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004320 return FAIL;
4321
4322 if (len_change)
4323 *len_change = -1;
4324
4325 return OK;
4326 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004327 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004328 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004329 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004330
4331 if (save == NULL)
4332 return FAIL;
4333
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004334 VimTryStart();
4335
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004336 /* We do not need to free "save" if ml_replace() consumes it. */
4337 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004338 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004339
4340 if (u_savesub((linenr_T)n) == FAIL)
4341 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004342 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004343 vim_free(save);
4344 }
4345 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4346 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004347 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004348 vim_free(save);
4349 }
4350 else
4351 changed_bytes((linenr_T)n, 0);
4352
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004353 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004354
4355 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004356 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004357 check_cursor_col();
4358
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004359 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004360 return FAIL;
4361
4362 if (len_change)
4363 *len_change = 0;
4364
4365 return OK;
4366 }
4367 else
4368 {
4369 PyErr_BadArgument();
4370 return FAIL;
4371 }
4372}
4373
Bram Moolenaar19e60942011-06-19 00:27:51 +02004374/* Replace a range of lines in the specified buffer. The line numbers are in
4375 * Vim format (1-based). The range is from lo up to, but not including, hi.
4376 * The replacement lines are given as a Python list of string objects. The
4377 * list is checked for validity and correct format. Errors are returned as a
4378 * value of FAIL. The return value is OK on success.
4379 * If OK is returned and len_change is not NULL, *len_change
4380 * is set to the change in the buffer length.
4381 */
4382 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004383SetBufferLineList(
4384 buf_T *buf,
4385 PyInt lo,
4386 PyInt hi,
4387 PyObject *list,
4388 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004389{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004390 buf_T *save_curbuf = NULL;
4391 win_T *save_curwin = NULL;
4392 tabpage_T *save_curtab = NULL;
4393
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004394 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004395 * There are three cases:
4396 * 1. NULL, or None - this is a deletion.
4397 * 2. A list - this is a replacement.
4398 * 3. Anything else - this is an error.
4399 */
4400 if (list == Py_None || list == NULL)
4401 {
4402 PyInt i;
4403 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004404
4405 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004406 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004407 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004408
4409 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004410 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004411 else
4412 {
4413 for (i = 0; i < n; ++i)
4414 {
4415 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4416 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004417 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004418 break;
4419 }
4420 }
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004421 if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL))
4422 /* Using an existing window for the buffer, adjust the cursor
4423 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004424 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004425 if (save_curbuf == NULL)
4426 /* Only adjust marks if we managed to switch to a window that
4427 * holds the buffer, otherwise line numbers will be invalid. */
4428 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004429 }
4430
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004431 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004432
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004433 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004434 return FAIL;
4435
4436 if (len_change)
4437 *len_change = -n;
4438
4439 return OK;
4440 }
4441 else if (PyList_Check(list))
4442 {
4443 PyInt i;
4444 PyInt new_len = PyList_Size(list);
4445 PyInt old_len = hi - lo;
4446 PyInt extra = 0; /* lines added to text, can be negative */
4447 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004448
4449 if (new_len == 0) /* avoid allocating zero bytes */
4450 array = NULL;
4451 else
4452 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004453 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004454 if (array == NULL)
4455 {
4456 PyErr_NoMemory();
4457 return FAIL;
4458 }
4459 }
4460
4461 for (i = 0; i < new_len; ++i)
4462 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004463 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004464
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004465 if (!(line = PyList_GetItem(list, i)) ||
4466 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004467 {
4468 while (i)
4469 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004470 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004471 return FAIL;
4472 }
4473 }
4474
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004475 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004476 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004477
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004478 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004479 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004480
4481 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004482 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004483
4484 /* If the size of the range is reducing (ie, new_len < old_len) we
4485 * need to delete some old_len. We do this at the start, by
4486 * repeatedly deleting line "lo".
4487 */
4488 if (!PyErr_Occurred())
4489 {
4490 for (i = 0; i < old_len - new_len; ++i)
4491 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4492 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004493 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004494 break;
4495 }
4496 extra -= i;
4497 }
4498
4499 /* For as long as possible, replace the existing old_len with the
4500 * new old_len. This is a more efficient operation, as it requires
4501 * less memory allocation and freeing.
4502 */
4503 if (!PyErr_Occurred())
4504 {
4505 for (i = 0; i < old_len && i < new_len; ++i)
4506 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4507 == FAIL)
4508 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004509 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004510 break;
4511 }
4512 }
4513 else
4514 i = 0;
4515
4516 /* Now we may need to insert the remaining new old_len. If we do, we
4517 * must free the strings as we finish with them (we can't pass the
4518 * responsibility to vim in this case).
4519 */
4520 if (!PyErr_Occurred())
4521 {
4522 while (i < new_len)
4523 {
4524 if (ml_append((linenr_T)(lo + i - 1),
4525 (char_u *)array[i], 0, FALSE) == FAIL)
4526 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004527 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004528 break;
4529 }
4530 vim_free(array[i]);
4531 ++i;
4532 ++extra;
4533 }
4534 }
4535
4536 /* Free any left-over old_len, as a result of an error */
4537 while (i < new_len)
4538 {
4539 vim_free(array[i]);
4540 ++i;
4541 }
4542
4543 /* Free the array of old_len. All of its contents have now
4544 * been dealt with (either freed, or the responsibility passed
4545 * to vim.
4546 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004547 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004548
4549 /* Adjust marks. Invalidate any which lie in the
4550 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004551 * Only adjust marks if we managed to switch to a window that holds
4552 * the buffer, otherwise line numbers will be invalid. */
4553 if (save_curbuf == NULL)
4554 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004555 (long)MAXLNUM, (long)extra);
4556 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4557
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004558 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004559 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4560
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004561 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004562 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004563
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004564 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004565 return FAIL;
4566
4567 if (len_change)
4568 *len_change = new_len - old_len;
4569
4570 return OK;
4571 }
4572 else
4573 {
4574 PyErr_BadArgument();
4575 return FAIL;
4576 }
4577}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004578
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004579/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004580 * The line number is in Vim format (1-based). The lines to be inserted are
4581 * given as a Python list of string objects or as a single string. The lines
4582 * to be added are checked for validity and correct format. Errors are
4583 * returned as a value of FAIL. The return value is OK on success.
4584 * If OK is returned and len_change is not NULL, *len_change
4585 * is set to the change in the buffer length.
4586 */
4587 static int
4588InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4589{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004590 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004591 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004592 tabpage_T *save_curtab = NULL;
4593
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004594 /* First of all, we check the type of the supplied Python object.
4595 * It must be a string or a list, or the call is in error.
4596 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004597 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004598 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004599 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004600
4601 if (str == NULL)
4602 return FAIL;
4603
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004604 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004605 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004606 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004607
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004608 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004609 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004610 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004611 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004612 else if (save_curbuf == NULL)
4613 /* Only adjust marks if we managed to switch to a window that
4614 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004615 appended_lines_mark((linenr_T)n, 1L);
4616
4617 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004618 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004619 update_screen(VALID);
4620
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004621 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004622 return FAIL;
4623
4624 if (len_change)
4625 *len_change = 1;
4626
4627 return OK;
4628 }
4629 else if (PyList_Check(lines))
4630 {
4631 PyInt i;
4632 PyInt size = PyList_Size(lines);
4633 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004634
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004635 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004636 if (array == NULL)
4637 {
4638 PyErr_NoMemory();
4639 return FAIL;
4640 }
4641
4642 for (i = 0; i < size; ++i)
4643 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004644 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004645
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004646 if (!(line = PyList_GetItem(lines, i)) ||
4647 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004648 {
4649 while (i)
4650 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004651 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004652 return FAIL;
4653 }
4654 }
4655
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004656 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004657 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004658 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004659
4660 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004661 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004662 else
4663 {
4664 for (i = 0; i < size; ++i)
4665 {
4666 if (ml_append((linenr_T)(n + i),
4667 (char_u *)array[i], 0, FALSE) == FAIL)
4668 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004669 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004670
4671 /* Free the rest of the lines */
4672 while (i < size)
4673 vim_free(array[i++]);
4674
4675 break;
4676 }
4677 vim_free(array[i]);
4678 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004679 if (i > 0 && save_curbuf == NULL)
4680 /* Only adjust marks if we managed to switch to a window that
4681 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004682 appended_lines_mark((linenr_T)n, (long)i);
4683 }
4684
4685 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004686 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004687 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004688 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004689
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004690 update_screen(VALID);
4691
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004692 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004693 return FAIL;
4694
4695 if (len_change)
4696 *len_change = size;
4697
4698 return OK;
4699 }
4700 else
4701 {
4702 PyErr_BadArgument();
4703 return FAIL;
4704 }
4705}
4706
4707/*
4708 * Common routines for buffers and line ranges
4709 * -------------------------------------------
4710 */
4711
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004712typedef struct
4713{
4714 PyObject_HEAD
4715 buf_T *buf;
4716} BufferObject;
4717
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004718 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004719CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004720{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004721 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004722 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004723 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004724 return -1;
4725 }
4726
4727 return 0;
4728}
4729
4730 static PyObject *
4731RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4732{
4733 if (CheckBuffer(self))
4734 return NULL;
4735
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004736 if (end == -1)
4737 end = self->buf->b_ml.ml_line_count;
4738
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004739 if (n < 0)
4740 n += end - start + 1;
4741
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004742 if (n < 0 || n > end - start)
4743 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004744 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004745 return NULL;
4746 }
4747
4748 return GetBufferLine(self->buf, n+start);
4749}
4750
4751 static PyObject *
4752RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4753{
4754 PyInt size;
4755
4756 if (CheckBuffer(self))
4757 return NULL;
4758
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004759 if (end == -1)
4760 end = self->buf->b_ml.ml_line_count;
4761
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004762 size = end - start + 1;
4763
4764 if (lo < 0)
4765 lo = 0;
4766 else if (lo > size)
4767 lo = size;
4768 if (hi < 0)
4769 hi = 0;
4770 if (hi < lo)
4771 hi = lo;
4772 else if (hi > size)
4773 hi = size;
4774
4775 return GetBufferLineList(self->buf, lo+start, hi+start);
4776}
4777
4778 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004779RBAsItem(
4780 BufferObject *self,
4781 PyInt n,
4782 PyObject *valObject,
4783 PyInt start,
4784 PyInt end,
4785 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786{
4787 PyInt len_change;
4788
4789 if (CheckBuffer(self))
4790 return -1;
4791
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004792 if (end == -1)
4793 end = self->buf->b_ml.ml_line_count;
4794
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004795 if (n < 0)
4796 n += end - start + 1;
4797
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004798 if (n < 0 || n > end - start)
4799 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004800 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004801 return -1;
4802 }
4803
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004804 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004805 return -1;
4806
4807 if (new_end)
4808 *new_end = end + len_change;
4809
4810 return 0;
4811}
4812
Bram Moolenaar19e60942011-06-19 00:27:51 +02004813 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004814RBAsSlice(
4815 BufferObject *self,
4816 PyInt lo,
4817 PyInt hi,
4818 PyObject *valObject,
4819 PyInt start,
4820 PyInt end,
4821 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004822{
4823 PyInt size;
4824 PyInt len_change;
4825
4826 /* Self must be a valid buffer */
4827 if (CheckBuffer(self))
4828 return -1;
4829
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004830 if (end == -1)
4831 end = self->buf->b_ml.ml_line_count;
4832
Bram Moolenaar19e60942011-06-19 00:27:51 +02004833 /* Sort out the slice range */
4834 size = end - start + 1;
4835
4836 if (lo < 0)
4837 lo = 0;
4838 else if (lo > size)
4839 lo = size;
4840 if (hi < 0)
4841 hi = 0;
4842 if (hi < lo)
4843 hi = lo;
4844 else if (hi > size)
4845 hi = size;
4846
4847 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004848 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004849 return -1;
4850
4851 if (new_end)
4852 *new_end = end + len_change;
4853
4854 return 0;
4855}
4856
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004857
4858 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004859RBAppend(
4860 BufferObject *self,
4861 PyObject *args,
4862 PyInt start,
4863 PyInt end,
4864 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004865{
4866 PyObject *lines;
4867 PyInt len_change;
4868 PyInt max;
4869 PyInt n;
4870
4871 if (CheckBuffer(self))
4872 return NULL;
4873
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004874 if (end == -1)
4875 end = self->buf->b_ml.ml_line_count;
4876
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004877 max = n = end - start + 1;
4878
4879 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4880 return NULL;
4881
4882 if (n < 0 || n > max)
4883 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004884 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004885 return NULL;
4886 }
4887
4888 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4889 return NULL;
4890
4891 if (new_end)
4892 *new_end = end + len_change;
4893
4894 Py_INCREF(Py_None);
4895 return Py_None;
4896}
4897
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004898/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004899 */
4900
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004901static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004902static PySequenceMethods RangeAsSeq;
4903static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004904
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004905typedef struct
4906{
4907 PyObject_HEAD
4908 BufferObject *buf;
4909 PyInt start;
4910 PyInt end;
4911} RangeObject;
4912
4913 static PyObject *
4914RangeNew(buf_T *buf, PyInt start, PyInt end)
4915{
4916 BufferObject *bufr;
4917 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004918 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004919 if (self == NULL)
4920 return NULL;
4921
4922 bufr = (BufferObject *)BufferNew(buf);
4923 if (bufr == NULL)
4924 {
4925 Py_DECREF(self);
4926 return NULL;
4927 }
4928 Py_INCREF(bufr);
4929
4930 self->buf = bufr;
4931 self->start = start;
4932 self->end = end;
4933
4934 return (PyObject *)(self);
4935}
4936
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004937 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004938RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004939{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004940 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004941 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004942 PyObject_GC_Del((void *)(self));
4943}
4944
4945 static int
4946RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4947{
4948 Py_VISIT(((PyObject *)(self->buf)));
4949 return 0;
4950}
4951
4952 static int
4953RangeClear(RangeObject *self)
4954{
4955 Py_CLEAR(self->buf);
4956 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004957}
4958
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004959 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004960RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004961{
4962 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004963 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004964 return -1; /* ??? */
4965
Bram Moolenaard6e39182013-05-21 18:30:34 +02004966 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004967}
4968
4969 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004970RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004971{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004972 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004973}
4974
4975 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004976RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004977{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004978 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004979}
4980
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004981static char *RangeAttrs[] = {
4982 "start", "end",
4983 NULL
4984};
4985
4986 static PyObject *
4987RangeDir(PyObject *self)
4988{
4989 return ObjectDir(self, RangeAttrs);
4990}
4991
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004992 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004993RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004994{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004995 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004996}
4997
4998 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004999RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005000{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005001 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005002 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5003 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005004 else
5005 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005006 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005007
5008 if (name == NULL)
5009 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005010
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005011 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005012 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005013 }
5014}
5015
5016static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005017 /* name, function, calling, documentation */
5018 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005019 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5020 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005021};
5022
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005023static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005024static PySequenceMethods BufferAsSeq;
5025static PyMappingMethods BufferAsMapping;
5026
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005027 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005028BufferNew(buf_T *buf)
5029{
5030 /* We need to handle deletion of buffers underneath us.
5031 * If we add a "b_python*_ref" field to the buf_T structure,
5032 * then we can get at it in buf_freeall() in vim. We then
5033 * need to create only ONE Python object per buffer - if
5034 * we try to create a second, just INCREF the existing one
5035 * and return it. The (single) Python object referring to
5036 * the buffer is stored in "b_python*_ref".
5037 * Question: what to do on a buf_freeall(). We'll probably
5038 * have to either delete the Python object (DECREF it to
5039 * zero - a bad idea, as it leaves dangling refs!) or
5040 * set the buf_T * value to an invalid value (-1?), which
5041 * means we need checks in all access functions... Bah.
5042 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005043 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005044 * b_python_ref and b_python3_ref fields respectively.
5045 */
5046
5047 BufferObject *self;
5048
5049 if (BUF_PYTHON_REF(buf) != NULL)
5050 {
5051 self = BUF_PYTHON_REF(buf);
5052 Py_INCREF(self);
5053 }
5054 else
5055 {
5056 self = PyObject_NEW(BufferObject, &BufferType);
5057 if (self == NULL)
5058 return NULL;
5059 self->buf = buf;
5060 BUF_PYTHON_REF(buf) = self;
5061 }
5062
5063 return (PyObject *)(self);
5064}
5065
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005066 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005067BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005068{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005069 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5070 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005071
5072 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005073}
5074
Bram Moolenaar971db462013-05-12 18:44:48 +02005075 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005076BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005077{
5078 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005079 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005080 return -1; /* ??? */
5081
Bram Moolenaard6e39182013-05-21 18:30:34 +02005082 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005083}
5084
5085 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005086BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005087{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005088 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005089}
5090
5091 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005092BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005093{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005094 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005095}
5096
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005097static char *BufferAttrs[] = {
5098 "name", "number", "vars", "options", "valid",
5099 NULL
5100};
5101
5102 static PyObject *
5103BufferDir(PyObject *self)
5104{
5105 return ObjectDir(self, BufferAttrs);
5106}
5107
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005108 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005109BufferAttrValid(BufferObject *self, char *name)
5110{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005111 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005112
5113 if (strcmp(name, "valid") != 0)
5114 return NULL;
5115
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005116 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5117 Py_INCREF(ret);
5118 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005119}
5120
5121 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005122BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005123{
5124 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005125 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005126 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005127 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005128 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005129 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005130 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005131 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005132 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5133 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005134 else if (strcmp(name, "__members__") == 0)
5135 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005136 else
5137 return NULL;
5138}
5139
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005140 static int
5141BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5142{
5143 if (CheckBuffer(self))
5144 return -1;
5145
5146 if (strcmp(name, "name") == 0)
5147 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005148 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005149 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005150 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005151 PyObject *todecref;
5152
5153 if (!(val = StringToChars(valObject, &todecref)))
5154 return -1;
5155
5156 VimTryStart();
5157 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5158 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005159 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005160 aucmd_restbuf(&aco);
5161 Py_XDECREF(todecref);
5162 if (VimTryEnd())
5163 return -1;
5164
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005165 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005166 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005167 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005168 return -1;
5169 }
5170 return 0;
5171 }
5172 else
5173 {
5174 PyErr_SetString(PyExc_AttributeError, name);
5175 return -1;
5176 }
5177}
5178
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005179 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005180BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005181{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005182 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005183}
5184
5185 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005186BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005187{
5188 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005189 char_u *pmark;
5190 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02005191 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005192 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005193
Bram Moolenaard6e39182013-05-21 18:30:34 +02005194 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005195 return NULL;
5196
Bram Moolenaar389a1792013-06-23 13:00:44 +02005197 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005198 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005199
Bram Moolenaar389a1792013-06-23 13:00:44 +02005200 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005201 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005202 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005203 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005204 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005205 return NULL;
5206 }
5207
5208 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005209
5210 Py_XDECREF(todecref);
5211
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005212 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005213 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005214 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02005215 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005216 if (VimTryEnd())
5217 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005218
5219 if (posp == NULL)
5220 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005221 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005222 return NULL;
5223 }
5224
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005225 if (posp->lnum <= 0)
5226 {
5227 /* Or raise an error? */
5228 Py_INCREF(Py_None);
5229 return Py_None;
5230 }
5231
5232 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5233}
5234
5235 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005236BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005237{
5238 PyInt start;
5239 PyInt end;
5240
Bram Moolenaard6e39182013-05-21 18:30:34 +02005241 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005242 return NULL;
5243
5244 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5245 return NULL;
5246
Bram Moolenaard6e39182013-05-21 18:30:34 +02005247 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005248}
5249
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005250 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005251BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005252{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005253 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005254 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005255 else
5256 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005257 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005258
5259 if (name == NULL)
5260 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005261
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005262 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005263 }
5264}
5265
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005266static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005267 /* name, function, calling, documentation */
5268 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005269 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005270 {"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 +02005271 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5272 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005273};
5274
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005275/*
5276 * Buffer list object - Implementation
5277 */
5278
5279static PyTypeObject BufMapType;
5280
5281typedef struct
5282{
5283 PyObject_HEAD
5284} BufMapObject;
5285
5286 static PyInt
5287BufMapLength(PyObject *self UNUSED)
5288{
5289 buf_T *b = firstbuf;
5290 PyInt n = 0;
5291
5292 while (b)
5293 {
5294 ++n;
5295 b = b->b_next;
5296 }
5297
5298 return n;
5299}
5300
5301 static PyObject *
5302BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5303{
5304 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005305 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005306
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005307 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005308 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005309
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005310 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005311
5312 if (b)
5313 return BufferNew(b);
5314 else
5315 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005316 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005317 return NULL;
5318 }
5319}
5320
5321 static void
5322BufMapIterDestruct(PyObject *buffer)
5323{
5324 /* Iteration was stopped before all buffers were processed */
5325 if (buffer)
5326 {
5327 Py_DECREF(buffer);
5328 }
5329}
5330
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005331 static int
5332BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5333{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005334 if (buffer)
5335 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005336 return 0;
5337}
5338
5339 static int
5340BufMapIterClear(PyObject **buffer)
5341{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005342 if (*buffer)
5343 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005344 return 0;
5345}
5346
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005347 static PyObject *
5348BufMapIterNext(PyObject **buffer)
5349{
5350 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005351 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005352
5353 if (!*buffer)
5354 return NULL;
5355
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005356 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005357
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005358 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005359 {
5360 *buffer = NULL;
5361 return NULL;
5362 }
5363
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005364 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005365 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005366 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005367 return NULL;
5368 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005369 /* Do not increment reference: we no longer hold it (decref), but whoever
5370 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005371 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005372}
5373
5374 static PyObject *
5375BufMapIter(PyObject *self UNUSED)
5376{
5377 PyObject *buffer;
5378
5379 buffer = BufferNew(firstbuf);
5380 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005381 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5382 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005383}
5384
5385static PyMappingMethods BufMapAsMapping = {
5386 (lenfunc) BufMapLength,
5387 (binaryfunc) BufMapItem,
5388 (objobjargproc) 0,
5389};
5390
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005391/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005392 */
5393
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005394static char *CurrentAttrs[] = {
5395 "buffer", "window", "line", "range", "tabpage",
5396 NULL
5397};
5398
5399 static PyObject *
5400CurrentDir(PyObject *self)
5401{
5402 return ObjectDir(self, CurrentAttrs);
5403}
5404
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005405 static PyObject *
5406CurrentGetattr(PyObject *self UNUSED, char *name)
5407{
5408 if (strcmp(name, "buffer") == 0)
5409 return (PyObject *)BufferNew(curbuf);
5410 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005411 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005412 else if (strcmp(name, "tabpage") == 0)
5413 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005414 else if (strcmp(name, "line") == 0)
5415 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5416 else if (strcmp(name, "range") == 0)
5417 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005418 else if (strcmp(name, "__members__") == 0)
5419 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005420 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005421#if PY_MAJOR_VERSION < 3
5422 return Py_FindMethod(WindowMethods, self, name);
5423#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005424 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005425#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005426}
5427
5428 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005429CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005430{
5431 if (strcmp(name, "line") == 0)
5432 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005433 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5434 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005435 return -1;
5436
5437 return 0;
5438 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005439 else if (strcmp(name, "buffer") == 0)
5440 {
5441 int count;
5442
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005443 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005444 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005445 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005446 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005447 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005448 return -1;
5449 }
5450
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005451 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005452 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005453 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005454
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005455 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005456 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5457 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005458 if (VimTryEnd())
5459 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005460 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005461 return -1;
5462 }
5463
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005464 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005465 }
5466 else if (strcmp(name, "window") == 0)
5467 {
5468 int count;
5469
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005470 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005471 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005472 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005473 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005474 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005475 return -1;
5476 }
5477
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005478 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005479 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005480 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005481
5482 if (!count)
5483 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005484 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005485 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005486 return -1;
5487 }
5488
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005489 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005490 win_goto(((WindowObject *)(valObject))->win);
5491 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005492 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005493 if (VimTryEnd())
5494 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005495 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005496 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005497 return -1;
5498 }
5499
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005500 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005501 }
5502 else if (strcmp(name, "tabpage") == 0)
5503 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005504 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005505 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005506 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005507 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005508 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005509 return -1;
5510 }
5511
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005512 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005513 return -1;
5514
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005515 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005516 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5517 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005518 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005519 if (VimTryEnd())
5520 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005521 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005522 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005523 return -1;
5524 }
5525
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005526 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005527 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005528 else
5529 {
5530 PyErr_SetString(PyExc_AttributeError, name);
5531 return -1;
5532 }
5533}
5534
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005535static struct PyMethodDef CurrentMethods[] = {
5536 /* name, function, calling, documentation */
5537 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5538 { NULL, NULL, 0, NULL}
5539};
5540
Bram Moolenaardb913952012-06-29 12:54:53 +02005541 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005542init_range_cmd(exarg_T *eap)
5543{
5544 RangeStart = eap->line1;
5545 RangeEnd = eap->line2;
5546}
5547
5548 static void
5549init_range_eval(typval_T *rettv UNUSED)
5550{
5551 RangeStart = (PyInt) curwin->w_cursor.lnum;
5552 RangeEnd = RangeStart;
5553}
5554
5555 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005556run_cmd(const char *cmd, void *arg UNUSED
5557#ifdef PY_CAN_RECURSE
5558 , PyGILState_STATE *pygilstate UNUSED
5559#endif
5560 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005561{
Bram Moolenaar41009372013-07-01 22:03:04 +02005562 PyObject *run_ret;
5563 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5564 if (run_ret != NULL)
5565 {
5566 Py_DECREF(run_ret);
5567 }
5568 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5569 {
5570 EMSG2(_(e_py_systemexit), "python");
5571 PyErr_Clear();
5572 }
5573 else
5574 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005575}
5576
5577static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5578static int code_hdr_len = 30;
5579
5580 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005581run_do(const char *cmd, void *arg UNUSED
5582#ifdef PY_CAN_RECURSE
5583 , PyGILState_STATE *pygilstate
5584#endif
5585 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005586{
5587 PyInt lnum;
5588 size_t len;
5589 char *code;
5590 int status;
5591 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005592 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005593
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005594 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005595 {
5596 EMSG(_("cannot save undo information"));
5597 return;
5598 }
5599
5600 len = code_hdr_len + STRLEN(cmd);
5601 code = PyMem_New(char, len + 1);
5602 memcpy(code, code_hdr, code_hdr_len);
5603 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005604 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5605 status = -1;
5606 if (run_ret != NULL)
5607 {
5608 status = 0;
5609 Py_DECREF(run_ret);
5610 }
5611 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5612 {
5613 PyMem_Free(code);
5614 EMSG2(_(e_py_systemexit), "python");
5615 PyErr_Clear();
5616 return;
5617 }
5618 else
5619 PyErr_PrintEx(1);
5620
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005621 PyMem_Free(code);
5622
5623 if (status)
5624 {
5625 EMSG(_("failed to run the code"));
5626 return;
5627 }
5628
5629 status = 0;
5630 pymain = PyImport_AddModule("__main__");
5631 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005632#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005633 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005634#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005635
5636 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5637 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005638 PyObject *line;
5639 PyObject *linenr;
5640 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005641
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005642#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005643 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005644#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005645 if (!(line = GetBufferLine(curbuf, lnum)))
5646 goto err;
5647 if (!(linenr = PyInt_FromLong((long) lnum)))
5648 {
5649 Py_DECREF(line);
5650 goto err;
5651 }
5652 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5653 Py_DECREF(line);
5654 Py_DECREF(linenr);
5655 if (!ret)
5656 goto err;
5657
5658 if (ret != Py_None)
5659 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5660 goto err;
5661
5662 Py_XDECREF(ret);
5663 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005664#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005665 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005666#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005667 }
5668 goto out;
5669err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005670#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005671 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005672#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005673 PyErr_PrintEx(0);
5674 PythonIO_Flush();
5675 status = 1;
5676out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005677#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005678 if (!status)
5679 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005680#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005681 Py_DECREF(pyfunc);
5682 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5683 if (status)
5684 return;
5685 check_cursor();
5686 update_curbuf(NOT_VALID);
5687}
5688
5689 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005690run_eval(const char *cmd, typval_T *rettv
5691#ifdef PY_CAN_RECURSE
5692 , PyGILState_STATE *pygilstate UNUSED
5693#endif
5694 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005695{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005696 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005697
Bram Moolenaar41009372013-07-01 22:03:04 +02005698 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005699 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005700 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005701 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005702 {
5703 EMSG2(_(e_py_systemexit), "python");
5704 PyErr_Clear();
5705 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005706 else
5707 {
5708 if (PyErr_Occurred() && !msg_silent)
5709 PyErr_PrintEx(0);
5710 EMSG(_("E858: Eval did not return a valid python object"));
5711 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005712 }
5713 else
5714 {
Bram Moolenaar77324fc2016-01-17 22:37:03 +01005715 if (run_ret != Py_None && ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005716 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005717 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005718 }
5719 PyErr_Clear();
5720}
5721
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005722 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005723set_ref_in_py(const int copyID)
5724{
5725 pylinkedlist_T *cur;
5726 dict_T *dd;
5727 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005728 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005729 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005730 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005731
5732 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005733 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005734 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005735 {
5736 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5737 if (dd->dv_copyID != copyID)
5738 {
5739 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005740 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005741 }
5742 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005743 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005744
5745 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005746 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005747 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005748 {
5749 ll = ((ListObject *) (cur->pll_obj))->list;
5750 if (ll->lv_copyID != copyID)
5751 {
5752 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005753 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005754 }
5755 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005756 }
5757
Bram Moolenaar8110a092016-04-14 15:56:09 +02005758 if (lastfunc != NULL)
5759 {
5760 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5761 {
5762 func = (FunctionObject *) cur->pll_obj;
5763 if (func->self != NULL && func->self->dv_copyID != copyID)
5764 {
5765 func->self->dv_copyID = copyID;
5766 abort = abort || set_ref_in_ht(
5767 &func->self->dv_hashtab, copyID, NULL);
5768 }
5769 if (func->argc)
5770 for (i = 0; !abort && i < func->argc; ++i)
5771 abort = abort
5772 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5773 }
5774 }
5775
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005776 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005777}
5778
5779 static int
5780set_string_copy(char_u *str, typval_T *tv)
5781{
5782 tv->vval.v_string = vim_strsave(str);
5783 if (tv->vval.v_string == NULL)
5784 {
5785 PyErr_NoMemory();
5786 return -1;
5787 }
5788 return 0;
5789}
5790
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005791 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005792pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005793{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005794 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005795 char_u *key;
5796 dictitem_T *di;
5797 PyObject *keyObject;
5798 PyObject *valObject;
5799 Py_ssize_t iter = 0;
5800
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005801 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005802 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005803
5804 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005805 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005806
5807 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5808 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005809 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005810
Bram Moolenaara03e6312013-05-29 22:49:26 +02005811 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005812 {
5813 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005814 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005815 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005816
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005817 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005818 {
5819 dict_unref(dict);
5820 return -1;
5821 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005822
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005823 if (*key == NUL)
5824 {
5825 dict_unref(dict);
5826 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005827 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005828 return -1;
5829 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005830
5831 di = dictitem_alloc(key);
5832
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005833 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005834
5835 if (di == NULL)
5836 {
5837 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005838 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005839 return -1;
5840 }
5841 di->di_tv.v_lock = 0;
5842
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005843 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005844 {
5845 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005846 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005847 return -1;
5848 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005849
5850 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005851 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005852 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005853 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005854 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005855 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005856 return -1;
5857 }
5858 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005859
5860 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005861 return 0;
5862}
5863
5864 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005865pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005866{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005867 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005868 char_u *key;
5869 dictitem_T *di;
5870 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005871 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005872 PyObject *keyObject;
5873 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005874
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005875 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005876 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005877
5878 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005879 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005880
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005881 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005882 {
5883 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005884 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005885 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005886
5887 if (!(iterator = PyObject_GetIter(list)))
5888 {
5889 dict_unref(dict);
5890 Py_DECREF(list);
5891 return -1;
5892 }
5893 Py_DECREF(list);
5894
5895 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005896 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005897 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005898
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005899 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005900 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005901 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005902 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005903 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005904 return -1;
5905 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005906
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005907 if (*key == NUL)
5908 {
5909 Py_DECREF(keyObject);
5910 Py_DECREF(iterator);
5911 Py_XDECREF(todecref);
5912 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005913 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005914 return -1;
5915 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005916
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005917 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005918 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005919 Py_DECREF(keyObject);
5920 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005921 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005922 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005923 return -1;
5924 }
5925
5926 di = dictitem_alloc(key);
5927
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005928 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005929 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005930
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005931 if (di == NULL)
5932 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005933 Py_DECREF(iterator);
5934 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005935 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005936 PyErr_NoMemory();
5937 return -1;
5938 }
5939 di->di_tv.v_lock = 0;
5940
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005941 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005942 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005943 Py_DECREF(iterator);
5944 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005945 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005946 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005947 return -1;
5948 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005949
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005950 Py_DECREF(valObject);
5951
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005952 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005953 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005954 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005955 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005956 dictitem_free(di);
5957 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005958 return -1;
5959 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005960 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005961 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005962 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005963 return 0;
5964}
5965
5966 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005967pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005968{
5969 list_T *l;
5970
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005971 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005972 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005973
5974 tv->v_type = VAR_LIST;
5975 tv->vval.v_list = l;
5976
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005977 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005978 {
5979 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005980 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005981 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005982
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005983 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005984 return 0;
5985}
5986
Bram Moolenaardb913952012-06-29 12:54:53 +02005987typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5988
5989 static int
5990convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005991 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005992{
5993 PyObject *capsule;
5994 char hexBuf[sizeof(void *) * 2 + 3];
5995
5996 sprintf(hexBuf, "%p", obj);
5997
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005998# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005999 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006000# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006001 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006002# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006003 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006004 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006005# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006006 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006007# else
6008 capsule = PyCObject_FromVoidPtr(tv, NULL);
6009# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006010 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6011 {
6012 Py_DECREF(capsule);
6013 tv->v_type = VAR_UNKNOWN;
6014 return -1;
6015 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006016
6017 Py_DECREF(capsule);
6018
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006019 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006020 {
6021 tv->v_type = VAR_UNKNOWN;
6022 return -1;
6023 }
6024 /* As we are not using copy_tv which increments reference count we must
6025 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006026 if (tv->v_type == VAR_DICT)
6027 ++tv->vval.v_dict->dv_refcount;
6028 else if (tv->v_type == VAR_LIST)
6029 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006030 }
6031 else
6032 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006033 typval_T *v;
6034
6035# ifdef PY_USE_CAPSULE
6036 v = PyCapsule_GetPointer(capsule, NULL);
6037# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006038 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006039# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006040 copy_tv(v, tv);
6041 }
6042 return 0;
6043}
6044
6045 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006046ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6047{
6048 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006049 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006050
6051 if (!(lookup_dict = PyDict_New()))
6052 return -1;
6053
6054 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6055 {
6056 tv->v_type = VAR_DICT;
6057 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6058 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006059 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006060 }
6061 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006062 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006063 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006064 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006065 else
6066 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006067 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006068 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006069 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006070 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006071 }
6072 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006073 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006074}
6075
6076 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006077ConvertFromPySequence(PyObject *obj, typval_T *tv)
6078{
6079 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006080 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006081
6082 if (!(lookup_dict = PyDict_New()))
6083 return -1;
6084
6085 if (PyType_IsSubtype(obj->ob_type, &ListType))
6086 {
6087 tv->v_type = VAR_LIST;
6088 tv->vval.v_list = (((ListObject *)(obj))->list);
6089 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006090 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006091 }
6092 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006093 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006094 else
6095 {
6096 PyErr_FORMAT(PyExc_TypeError,
6097 N_("unable to convert %s to vim list"),
6098 Py_TYPE_NAME(obj));
6099 ret = -1;
6100 }
6101 Py_DECREF(lookup_dict);
6102 return ret;
6103}
6104
6105 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006106ConvertFromPyObject(PyObject *obj, typval_T *tv)
6107{
6108 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006109 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006110
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006111 if (!(lookup_dict = PyDict_New()))
6112 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006113 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006114 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006115 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006116}
6117
6118 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006119_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006120{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006121 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006122 {
6123 tv->v_type = VAR_DICT;
6124 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6125 ++tv->vval.v_dict->dv_refcount;
6126 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006127 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006128 {
6129 tv->v_type = VAR_LIST;
6130 tv->vval.v_list = (((ListObject *)(obj))->list);
6131 ++tv->vval.v_list->lv_refcount;
6132 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006133 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006134 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006135 FunctionObject *func = (FunctionObject *) obj;
6136 if (func->self != NULL || func->argv != NULL)
6137 {
6138 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6139 set_partial(func, pt, TRUE);
6140 tv->vval.v_partial = pt;
6141 tv->v_type = VAR_PARTIAL;
6142 }
6143 else
6144 {
6145 if (set_string_copy(func->name, tv) == -1)
6146 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006147
Bram Moolenaar8110a092016-04-14 15:56:09 +02006148 tv->v_type = VAR_FUNC;
6149 }
6150 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006151 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006152 else if (PyBytes_Check(obj))
6153 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006154 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006155
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006156 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006157 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006158 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006159 return -1;
6160
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006161 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006162 return -1;
6163
6164 tv->v_type = VAR_STRING;
6165 }
6166 else if (PyUnicode_Check(obj))
6167 {
6168 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006169 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006170
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006171 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006172 if (bytes == NULL)
6173 return -1;
6174
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006175 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006176 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006177 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006178 return -1;
6179
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006180 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006181 {
6182 Py_XDECREF(bytes);
6183 return -1;
6184 }
6185 Py_XDECREF(bytes);
6186
6187 tv->v_type = VAR_STRING;
6188 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006189#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006190 else if (PyInt_Check(obj))
6191 {
6192 tv->v_type = VAR_NUMBER;
6193 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006194 if (PyErr_Occurred())
6195 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006196 }
6197#endif
6198 else if (PyLong_Check(obj))
6199 {
6200 tv->v_type = VAR_NUMBER;
6201 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006202 if (PyErr_Occurred())
6203 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006204 }
6205 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006206 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006207#ifdef FEAT_FLOAT
6208 else if (PyFloat_Check(obj))
6209 {
6210 tv->v_type = VAR_FLOAT;
6211 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6212 }
6213#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006214 else if (PyObject_HasAttrString(obj, "keys"))
6215 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006216 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006217 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006218 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006219 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006220 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006221 else if (PyNumber_Check(obj))
6222 {
6223 PyObject *num;
6224
6225 if (!(num = PyNumber_Long(obj)))
6226 return -1;
6227
6228 tv->v_type = VAR_NUMBER;
6229 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6230
6231 Py_DECREF(num);
6232 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006233 else
6234 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006235 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006236 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006237 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006238 return -1;
6239 }
6240 return 0;
6241}
6242
6243 static PyObject *
6244ConvertToPyObject(typval_T *tv)
6245{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006246 typval_T *argv;
6247 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006248 if (tv == NULL)
6249 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006250 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006251 return NULL;
6252 }
6253 switch (tv->v_type)
6254 {
6255 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006256 return PyBytes_FromString(tv->vval.v_string == NULL
6257 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006258 case VAR_NUMBER:
6259 return PyLong_FromLong((long) tv->vval.v_number);
6260#ifdef FEAT_FLOAT
6261 case VAR_FLOAT:
6262 return PyFloat_FromDouble((double) tv->vval.v_float);
6263#endif
6264 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006265 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006266 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006267 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006268 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006269 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006270 ? (char_u *)"" : tv->vval.v_string,
6271 0, NULL, NULL);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006272 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006273 if (tv->vval.v_partial->pt_argc)
6274 {
6275 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6276 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6277 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6278 }
6279 else
6280 argv = NULL;
6281 if (tv->vval.v_partial->pt_dict != NULL)
6282 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006283 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006284 ? (char_u *)"" : tv->vval.v_partial->pt_name,
6285 tv->vval.v_partial->pt_argc, argv,
6286 tv->vval.v_partial->pt_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006287 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006288 case VAR_CHANNEL:
6289 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006290 Py_INCREF(Py_None);
6291 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006292 case VAR_SPECIAL:
6293 switch (tv->vval.v_number)
6294 {
6295 case VVAL_FALSE: return AlwaysFalse(NULL);
6296 case VVAL_TRUE: return AlwaysTrue(NULL);
6297 case VVAL_NONE:
6298 case VVAL_NULL: return AlwaysNone(NULL);
6299 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006300 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006301 return NULL;
6302 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006303 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006304}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006305
6306typedef struct
6307{
6308 PyObject_HEAD
6309} CurrentObject;
6310static PyTypeObject CurrentType;
6311
6312 static void
6313init_structs(void)
6314{
6315 vim_memset(&OutputType, 0, sizeof(OutputType));
6316 OutputType.tp_name = "vim.message";
6317 OutputType.tp_basicsize = sizeof(OutputObject);
6318 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6319 OutputType.tp_doc = "vim message object";
6320 OutputType.tp_methods = OutputMethods;
6321#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006322 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6323 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006324 OutputType.tp_alloc = call_PyType_GenericAlloc;
6325 OutputType.tp_new = call_PyType_GenericNew;
6326 OutputType.tp_free = call_PyObject_Free;
6327#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006328 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6329 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006330#endif
6331
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006332 vim_memset(&IterType, 0, sizeof(IterType));
6333 IterType.tp_name = "vim.iter";
6334 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006335 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006336 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006337 IterType.tp_iter = (getiterfunc)IterIter;
6338 IterType.tp_iternext = (iternextfunc)IterNext;
6339 IterType.tp_dealloc = (destructor)IterDestructor;
6340 IterType.tp_traverse = (traverseproc)IterTraverse;
6341 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006342
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006343 vim_memset(&BufferType, 0, sizeof(BufferType));
6344 BufferType.tp_name = "vim.buffer";
6345 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006346 BufferType.tp_dealloc = (destructor)BufferDestructor;
6347 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006348 BufferType.tp_as_sequence = &BufferAsSeq;
6349 BufferType.tp_as_mapping = &BufferAsMapping;
6350 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6351 BufferType.tp_doc = "vim buffer object";
6352 BufferType.tp_methods = BufferMethods;
6353#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006354 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006355 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006356 BufferType.tp_alloc = call_PyType_GenericAlloc;
6357 BufferType.tp_new = call_PyType_GenericNew;
6358 BufferType.tp_free = call_PyObject_Free;
6359#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006360 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006361 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006362#endif
6363
6364 vim_memset(&WindowType, 0, sizeof(WindowType));
6365 WindowType.tp_name = "vim.window";
6366 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006367 WindowType.tp_dealloc = (destructor)WindowDestructor;
6368 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006369 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006370 WindowType.tp_doc = "vim Window object";
6371 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006372 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6373 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006374#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006375 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6376 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006377 WindowType.tp_alloc = call_PyType_GenericAlloc;
6378 WindowType.tp_new = call_PyType_GenericNew;
6379 WindowType.tp_free = call_PyObject_Free;
6380#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006381 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6382 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006383#endif
6384
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006385 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6386 TabPageType.tp_name = "vim.tabpage";
6387 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006388 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6389 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006390 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6391 TabPageType.tp_doc = "vim tab page object";
6392 TabPageType.tp_methods = TabPageMethods;
6393#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006394 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006395 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6396 TabPageType.tp_new = call_PyType_GenericNew;
6397 TabPageType.tp_free = call_PyObject_Free;
6398#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006399 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006400#endif
6401
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006402 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6403 BufMapType.tp_name = "vim.bufferlist";
6404 BufMapType.tp_basicsize = sizeof(BufMapObject);
6405 BufMapType.tp_as_mapping = &BufMapAsMapping;
6406 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006407 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006408 BufferType.tp_doc = "vim buffer list";
6409
6410 vim_memset(&WinListType, 0, sizeof(WinListType));
6411 WinListType.tp_name = "vim.windowlist";
6412 WinListType.tp_basicsize = sizeof(WinListType);
6413 WinListType.tp_as_sequence = &WinListAsSeq;
6414 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6415 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006416 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006417
6418 vim_memset(&TabListType, 0, sizeof(TabListType));
6419 TabListType.tp_name = "vim.tabpagelist";
6420 TabListType.tp_basicsize = sizeof(TabListType);
6421 TabListType.tp_as_sequence = &TabListAsSeq;
6422 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6423 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006424
6425 vim_memset(&RangeType, 0, sizeof(RangeType));
6426 RangeType.tp_name = "vim.range";
6427 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006428 RangeType.tp_dealloc = (destructor)RangeDestructor;
6429 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006430 RangeType.tp_as_sequence = &RangeAsSeq;
6431 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006432 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006433 RangeType.tp_doc = "vim Range object";
6434 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006435 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6436 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006437#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006438 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006439 RangeType.tp_alloc = call_PyType_GenericAlloc;
6440 RangeType.tp_new = call_PyType_GenericNew;
6441 RangeType.tp_free = call_PyObject_Free;
6442#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006443 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006444#endif
6445
6446 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6447 CurrentType.tp_name = "vim.currentdata";
6448 CurrentType.tp_basicsize = sizeof(CurrentObject);
6449 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6450 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006451 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006452#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006453 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6454 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006455#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006456 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6457 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006458#endif
6459
6460 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6461 DictionaryType.tp_name = "vim.dictionary";
6462 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006463 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006464 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006465 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006466 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006467 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6468 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006469 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6470 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6471 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006472#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006473 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6474 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006475#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006476 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6477 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006478#endif
6479
6480 vim_memset(&ListType, 0, sizeof(ListType));
6481 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006482 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006483 ListType.tp_basicsize = sizeof(ListObject);
6484 ListType.tp_as_sequence = &ListAsSeq;
6485 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006486 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006487 ListType.tp_doc = "list pushing modifications to vim structure";
6488 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006489 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006490 ListType.tp_new = (newfunc)ListConstructor;
6491 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006492#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006493 ListType.tp_getattro = (getattrofunc)ListGetattro;
6494 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006495#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006496 ListType.tp_getattr = (getattrfunc)ListGetattr;
6497 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006498#endif
6499
6500 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006501 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006502 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006503 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6504 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006505 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006506 FunctionType.tp_doc = "object that calls vim function";
6507 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006508 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006509 FunctionType.tp_new = (newfunc)FunctionConstructor;
6510 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006511#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006512 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006513#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006514 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006515#endif
6516
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006517 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6518 OptionsType.tp_name = "vim.options";
6519 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006520 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006521 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006522 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006523 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006524 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006525 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6526 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6527 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006528
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006529 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6530 LoaderType.tp_name = "vim.Loader";
6531 LoaderType.tp_basicsize = sizeof(LoaderObject);
6532 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6533 LoaderType.tp_doc = "vim message object";
6534 LoaderType.tp_methods = LoaderMethods;
6535 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6536
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006537#if PY_MAJOR_VERSION >= 3
6538 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6539 vimmodule.m_name = "vim";
6540 vimmodule.m_doc = "Vim Python interface\n";
6541 vimmodule.m_size = -1;
6542 vimmodule.m_methods = VimMethods;
6543#endif
6544}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006545
6546#define PYTYPE_READY(type) \
6547 if (PyType_Ready(&type)) \
6548 return -1;
6549
6550 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006551init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006552{
6553 PYTYPE_READY(IterType);
6554 PYTYPE_READY(BufferType);
6555 PYTYPE_READY(RangeType);
6556 PYTYPE_READY(WindowType);
6557 PYTYPE_READY(TabPageType);
6558 PYTYPE_READY(BufMapType);
6559 PYTYPE_READY(WinListType);
6560 PYTYPE_READY(TabListType);
6561 PYTYPE_READY(CurrentType);
6562 PYTYPE_READY(DictionaryType);
6563 PYTYPE_READY(ListType);
6564 PYTYPE_READY(FunctionType);
6565 PYTYPE_READY(OptionsType);
6566 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006567 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006568 return 0;
6569}
6570
6571 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006572init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006573{
6574 PyObject *path;
6575 PyObject *path_hook;
6576 PyObject *path_hooks;
6577
6578 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6579 return -1;
6580
6581 if (!(path_hooks = PySys_GetObject("path_hooks")))
6582 {
6583 PyErr_Clear();
6584 path_hooks = PyList_New(1);
6585 PyList_SET_ITEM(path_hooks, 0, path_hook);
6586 if (PySys_SetObject("path_hooks", path_hooks))
6587 {
6588 Py_DECREF(path_hooks);
6589 return -1;
6590 }
6591 Py_DECREF(path_hooks);
6592 }
6593 else if (PyList_Check(path_hooks))
6594 {
6595 if (PyList_Append(path_hooks, path_hook))
6596 {
6597 Py_DECREF(path_hook);
6598 return -1;
6599 }
6600 Py_DECREF(path_hook);
6601 }
6602 else
6603 {
6604 VimTryStart();
6605 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6606 "You should now do the following:\n"
6607 "- append vim.path_hook to sys.path_hooks\n"
6608 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6609 VimTryEnd(); /* Discard the error */
6610 Py_DECREF(path_hook);
6611 return 0;
6612 }
6613
6614 if (!(path = PySys_GetObject("path")))
6615 {
6616 PyErr_Clear();
6617 path = PyList_New(1);
6618 Py_INCREF(vim_special_path_object);
6619 PyList_SET_ITEM(path, 0, vim_special_path_object);
6620 if (PySys_SetObject("path", path))
6621 {
6622 Py_DECREF(path);
6623 return -1;
6624 }
6625 Py_DECREF(path);
6626 }
6627 else if (PyList_Check(path))
6628 {
6629 if (PyList_Append(path, vim_special_path_object))
6630 return -1;
6631 }
6632 else
6633 {
6634 VimTryStart();
6635 EMSG(_("Failed to set path: sys.path is not a list\n"
6636 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6637 VimTryEnd(); /* Discard the error */
6638 }
6639
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006640 return 0;
6641}
6642
6643static BufMapObject TheBufferMap =
6644{
6645 PyObject_HEAD_INIT(&BufMapType)
6646};
6647
6648static WinListObject TheWindowList =
6649{
6650 PyObject_HEAD_INIT(&WinListType)
6651 NULL
6652};
6653
6654static CurrentObject TheCurrent =
6655{
6656 PyObject_HEAD_INIT(&CurrentType)
6657};
6658
6659static TabListObject TheTabPageList =
6660{
6661 PyObject_HEAD_INIT(&TabListType)
6662};
6663
6664static struct numeric_constant {
6665 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006666 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006667} numeric_constants[] = {
6668 {"VAR_LOCKED", VAR_LOCKED},
6669 {"VAR_FIXED", VAR_FIXED},
6670 {"VAR_SCOPE", VAR_SCOPE},
6671 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6672};
6673
6674static struct object_constant {
6675 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006676 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006677} object_constants[] = {
6678 {"buffers", (PyObject *)(void *)&TheBufferMap},
6679 {"windows", (PyObject *)(void *)&TheWindowList},
6680 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6681 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006682
6683 {"Buffer", (PyObject *)&BufferType},
6684 {"Range", (PyObject *)&RangeType},
6685 {"Window", (PyObject *)&WindowType},
6686 {"TabPage", (PyObject *)&TabPageType},
6687 {"Dictionary", (PyObject *)&DictionaryType},
6688 {"List", (PyObject *)&ListType},
6689 {"Function", (PyObject *)&FunctionType},
6690 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006691 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006692};
6693
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006694#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006695 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006696 return -1;
6697
6698#define ADD_CHECKED_OBJECT(m, name, obj) \
6699 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006700 PyObject *valObject = obj; \
6701 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006702 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006703 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006704 }
6705
6706 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006707populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006708{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006709 int i;
6710 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006711 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006712 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006713
6714 for (i = 0; i < (int)(sizeof(numeric_constants)
6715 / sizeof(struct numeric_constant));
6716 ++i)
6717 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006718 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006719
6720 for (i = 0; i < (int)(sizeof(object_constants)
6721 / sizeof(struct object_constant));
6722 ++i)
6723 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006724 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006725
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006726 valObject = object_constants[i].valObject;
6727 Py_INCREF(valObject);
6728 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006729 }
6730
6731 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6732 return -1;
6733 ADD_OBJECT(m, "error", VimError);
6734
Bram Moolenaara9922d62013-05-30 13:01:18 +02006735 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6736 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006737 ADD_CHECKED_OBJECT(m, "options",
6738 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006739
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006740 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006741 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006742 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006743
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006744 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006745 return -1;
6746 ADD_OBJECT(m, "_getcwd", py_getcwd)
6747
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006748 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006749 return -1;
6750 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006751 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006752 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006753 if (PyObject_SetAttrString(other_module, "chdir", attr))
6754 {
6755 Py_DECREF(attr);
6756 return -1;
6757 }
6758 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006759
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006760 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006761 {
6762 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006763 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006764 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006765 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6766 {
6767 Py_DECREF(attr);
6768 return -1;
6769 }
6770 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006771 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006772 else
6773 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006774
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006775 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6776 return -1;
6777
6778 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6779
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006780 if (!(imp = PyImport_ImportModule("imp")))
6781 return -1;
6782
6783 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6784 {
6785 Py_DECREF(imp);
6786 return -1;
6787 }
6788
6789 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6790 {
6791 Py_DECREF(py_find_module);
6792 Py_DECREF(imp);
6793 return -1;
6794 }
6795
6796 Py_DECREF(imp);
6797
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006798 ADD_OBJECT(m, "_find_module", py_find_module);
6799 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006800
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006801 return 0;
6802}