blob: cdd7460191ddf2cb1b7e1cb7f9aea970b28039bb [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 Moolenaar79a494d2018-07-22 04:30:21 +020091#if PY_VERSION_HEX >= 0x030700f0
92static PyObject *py_find_spec;
93#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +020094static PyObject *py_find_module;
95static PyObject *py_load_module;
Bram Moolenaar79a494d2018-07-22 04:30:21 +020096#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +020097
98static PyObject *VimError;
99
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200100/*
101 * obtain a lock on the Vim data structures
102 */
103 static void
104Python_Lock_Vim(void)
105{
106}
107
108/*
109 * release a lock on the Vim data structures
110 */
111 static void
112Python_Release_Vim(void)
113{
114}
115
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200116/*
117 * The "todecref" argument holds a pointer to PyObject * that must be
118 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
119 * was needed to generate returned value is object.
120 *
121 * Use Py_XDECREF to decrement reference count.
122 */
123 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200124StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200126 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200127
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200128 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200129 {
130
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200131 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
132 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200133 return NULL;
134
135 *todecref = NULL;
136 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200137 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200138 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200139 PyObject *bytes;
140
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200141 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200142 return NULL;
143
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200144 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
145 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200146 {
147 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200148 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200149 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200150
151 *todecref = bytes;
152 }
153 else
154 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200155#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200156 PyErr_FORMAT(PyExc_TypeError,
157 N_("expected str() or unicode() instance, but got %s"),
158 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200159#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200160 PyErr_FORMAT(PyExc_TypeError,
161 N_("expected bytes() or str() instance, but got %s"),
162 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200163#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200164 return NULL;
165 }
166
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200167 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200168}
169
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200170#define NUMBER_LONG 1
171#define NUMBER_INT 2
172#define NUMBER_NATURAL 4
173#define NUMBER_UNSIGNED 8
174
175 static int
176NumberToLong(PyObject *obj, long *result, int flags)
177{
178#if PY_MAJOR_VERSION < 3
179 if (PyInt_Check(obj))
180 {
181 *result = PyInt_AsLong(obj);
182 if (PyErr_Occurred())
183 return -1;
184 }
185 else
186#endif
187 if (PyLong_Check(obj))
188 {
189 *result = PyLong_AsLong(obj);
190 if (PyErr_Occurred())
191 return -1;
192 }
193 else if (PyNumber_Check(obj))
194 {
195 PyObject *num;
196
197 if (!(num = PyNumber_Long(obj)))
198 return -1;
199
200 *result = PyLong_AsLong(num);
201
202 Py_DECREF(num);
203
204 if (PyErr_Occurred())
205 return -1;
206 }
207 else
208 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200209#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200210 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200211 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200212 "coercing to long(), but got %s"),
213 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200214#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200215 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200216 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200217 "but got %s"),
218 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200219#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200220 return -1;
221 }
222
223 if (flags & NUMBER_INT)
224 {
225 if (*result > INT_MAX)
226 {
227 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200228 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200229 return -1;
230 }
231 else if (*result < INT_MIN)
232 {
233 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200234 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200235 return -1;
236 }
237 }
238
239 if (flags & NUMBER_NATURAL)
240 {
241 if (*result <= 0)
242 {
243 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100244 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200245 return -1;
246 }
247 }
248 else if (flags & NUMBER_UNSIGNED)
249 {
250 if (*result < 0)
251 {
252 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200253 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200254 return -1;
255 }
256 }
257
258 return 0;
259}
260
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200261 static int
262add_string(PyObject *list, char *s)
263{
264 PyObject *string;
265
266 if (!(string = PyString_FromString(s)))
267 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200268
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200269 if (PyList_Append(list, string))
270 {
271 Py_DECREF(string);
272 return -1;
273 }
274
275 Py_DECREF(string);
276 return 0;
277}
278
279 static PyObject *
280ObjectDir(PyObject *self, char **attributes)
281{
282 PyMethodDef *method;
283 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200284 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200285
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200286 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200287 return NULL;
288
289 if (self)
290 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200291 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200292 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200293 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200294 return NULL;
295 }
296
297 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200298 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200299 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200300 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200301 return NULL;
302 }
303
304#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200305 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200306 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200307 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200308 return NULL;
309 }
310#endif
311
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200312 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200313}
314
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200315/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200316 */
317
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200318/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200319typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200320
321static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200322
323typedef struct
324{
325 PyObject_HEAD
326 long softspace;
327 long error;
328} OutputObject;
329
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200330static char *OutputAttrs[] = {
331 "softspace",
332 NULL
333};
334
335 static PyObject *
336OutputDir(PyObject *self)
337{
338 return ObjectDir(self, OutputAttrs);
339}
340
Bram Moolenaar77045652012-09-21 13:46:06 +0200341 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200342OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200343{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200344 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200345 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200346 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200347 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200348 return -1;
349 }
350
351 if (strcmp(name, "softspace") == 0)
352 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200353 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200354 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200355 return 0;
356 }
357
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200358 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200359 return -1;
360}
361
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200362/* Buffer IO, we write one whole line at a time. */
363static garray_T io_ga = {0, 0, 1, 80, NULL};
364static writefn old_fn = NULL;
365
366 static void
367PythonIO_Flush(void)
368{
369 if (old_fn != NULL && io_ga.ga_len > 0)
370 {
371 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
372 old_fn((char_u *)io_ga.ga_data);
373 }
374 io_ga.ga_len = 0;
375}
376
377 static void
378writer(writefn fn, char_u *str, PyInt n)
379{
380 char_u *ptr;
381
382 /* Flush when switching output function. */
383 if (fn != old_fn)
384 PythonIO_Flush();
385 old_fn = fn;
386
387 /* Write each NL separated line. Text after the last NL is kept for
388 * writing later. */
389 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
390 {
391 PyInt len = ptr - str;
392
393 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
394 break;
395
396 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
397 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
398 fn((char_u *)io_ga.ga_data);
399 str = ptr + 1;
400 n -= len + 1;
401 io_ga.ga_len = 0;
402 }
403
404 /* Put the remaining text into io_ga for later printing. */
405 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
406 {
407 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
408 io_ga.ga_len += (int)n;
409 }
410}
411
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200412 static int
413write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200414{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200415 Py_ssize_t len = 0;
416 char *str = NULL;
417 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200418
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200419 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
420 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200421
422 Py_BEGIN_ALLOW_THREADS
423 Python_Lock_Vim();
424 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
425 Python_Release_Vim();
426 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200427 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200428
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200429 return 0;
430}
431
432 static PyObject *
433OutputWrite(OutputObject *self, PyObject *string)
434{
435 if (write_output(self, string))
436 return NULL;
437
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200438 Py_INCREF(Py_None);
439 return Py_None;
440}
441
442 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200443OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200445 PyObject *iterator;
446 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200448 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200449 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200450
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200451 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200452 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200453 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200454 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200455 Py_DECREF(iterator);
456 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200457 return NULL;
458 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200459 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200460 }
461
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200462 Py_DECREF(iterator);
463
464 /* Iterator may have finished due to an exception */
465 if (PyErr_Occurred())
466 return NULL;
467
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200468 Py_INCREF(Py_None);
469 return Py_None;
470}
471
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100472 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100473AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100474{
475 /* do nothing */
476 Py_INCREF(Py_None);
477 return Py_None;
478}
479
Bram Moolenaard4247472015-11-02 13:28:59 +0100480 static PyObject *
481AlwaysFalse(PyObject *self UNUSED)
482{
483 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100484 PyObject *ret = Py_False;
485 Py_INCREF(ret);
486 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100487}
488
489 static PyObject *
490AlwaysTrue(PyObject *self UNUSED)
491{
492 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100493 PyObject *ret = Py_True;
494 Py_INCREF(ret);
495 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100496}
497
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200498/***************/
499
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200500static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200501 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200502 {"write", (PyCFunction)OutputWrite, METH_O, ""},
503 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100504 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
505 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
506 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
507 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
508 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
509 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200510 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200511 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200512 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200513};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200514
515static OutputObject Output =
516{
517 PyObject_HEAD_INIT(&OutputType)
518 0,
519 0
520};
521
522static OutputObject Error =
523{
524 PyObject_HEAD_INIT(&OutputType)
525 0,
526 1
527};
528
529 static int
530PythonIO_Init_io(void)
531{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200532 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
533 return -1;
534 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
535 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200536
537 if (PyErr_Occurred())
538 {
539 EMSG(_("E264: Python: Error initialising I/O objects"));
540 return -1;
541 }
542
543 return 0;
544}
545
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200546#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200547static PyObject *call_load_module(char *name, int len, PyObject *find_module_result);
548
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200549typedef struct
550{
551 PyObject_HEAD
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200552 char *fullname;
553 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200554} LoaderObject;
555static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200556
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200557 static void
558LoaderDestructor(LoaderObject *self)
559{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200560 vim_free(self->fullname);
561 Py_XDECREF(self->result);
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200562 DESTRUCTOR_FINISH(self);
563}
564
565 static PyObject *
566LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
567{
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200568 char *fullname = self->fullname;
569 PyObject *result = self->result;
570 PyObject *module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200571
Bram Moolenaar447bd5a2018-08-07 19:45:27 +0200572 if (!fullname)
573 {
574 module = result ? result : Py_None;
575 Py_INCREF(module);
576 return module;
577 }
578
579 module = call_load_module(fullname, (int)STRLEN(fullname), result);
580
581 self->fullname = NULL;
582 self->result = module;
583
584 vim_free(fullname);
585 Py_DECREF(result);
586
587 if (!module)
588 {
589 if (PyErr_Occurred())
590 return NULL;
591
592 Py_INCREF(Py_None);
593 return Py_None;
594 }
595
596 Py_INCREF(module);
597 return module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200598}
599
600static struct PyMethodDef LoaderMethods[] = {
601 /* name, function, calling, doc */
602 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
603 { NULL, NULL, 0, NULL}
604};
Bram Moolenaar79a494d2018-07-22 04:30:21 +0200605#endif
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200606
607/* Check to see whether a Vim error has been reported, or a keyboard
608 * interrupt has been detected.
609 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200610
611 static void
612VimTryStart(void)
613{
614 ++trylevel;
615}
616
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200617 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200618VimTryEnd(void)
619{
620 --trylevel;
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100621 /* Without this it stops processing all subsequent Vim script commands and
622 * generates strange error messages if I e.g. try calling Test() in a cycle
623 */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200624 did_emsg = FALSE;
625 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200626 if (got_int)
627 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100628 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100629 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100630 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200631 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200632 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200633 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100634 else if (msg_list != NULL && *msg_list != NULL)
635 {
636 int should_free;
637 char_u *msg;
638
639 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
640
641 if (msg == NULL)
642 {
643 PyErr_NoMemory();
644 return -1;
645 }
646
647 PyErr_SetVim((char *) msg);
648
649 free_global_msglist();
650
651 if (should_free)
652 vim_free(msg);
653
654 return -1;
655 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200656 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200657 return (PyErr_Occurred() ? -1 : 0);
658 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200659 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200660 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100661 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200662 return -1;
663 }
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100664 /* Finally transform Vim script exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200665 else
666 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200667 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200668 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200669 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200670 }
671}
672
673 static int
674VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200675{
676 if (got_int)
677 {
678 PyErr_SetNone(PyExc_KeyboardInterrupt);
679 return 1;
680 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200681 return 0;
682}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200683
684/* Vim module - Implementation
685 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200686
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200687 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200688VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200690 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200691 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200692 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693
Bram Moolenaar389a1792013-06-23 13:00:44 +0200694 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200695 return NULL;
696
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200697 Py_BEGIN_ALLOW_THREADS
698 Python_Lock_Vim();
699
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200700 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200701 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200702 update_screen(VALID);
703
704 Python_Release_Vim();
705 Py_END_ALLOW_THREADS
706
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200707 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200708 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200709 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200710 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200711
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200713 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200714 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200715}
716
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200717/*
718 * Function to translate a typval_T into a PyObject; this will recursively
719 * translate lists/dictionaries into their Python equivalents.
720 *
721 * The depth parameter is to avoid infinite recursion, set it to 1 when
722 * you call VimToPython.
723 */
724 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200725VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200729 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730
731 /* Avoid infinite recursion */
732 if (depth > 100)
733 {
734 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200735 ret = Py_None;
736 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200737 }
738
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200739 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200740 * then and we can use it again. */
741 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
742 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
743 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200744 sprintf(ptrBuf, "%p",
745 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
746 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200747
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200748 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200749 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200750 Py_INCREF(ret);
751 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200752 }
753 }
754
755 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200756 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200757 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200758 else if (our_tv->v_type == VAR_NUMBER)
759 {
760 char buf[NUMBUFLEN];
761
762 /* For backwards compatibility numbers are stored as strings. */
763 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200764 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765 }
766# ifdef FEAT_FLOAT
767 else if (our_tv->v_type == VAR_FLOAT)
768 {
769 char buf[NUMBUFLEN];
770
771 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200772 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773 }
774# endif
775 else if (our_tv->v_type == VAR_LIST)
776 {
777 list_T *list = our_tv->vval.v_list;
778 listitem_T *curr;
779
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200780 if (list == NULL)
781 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200782
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200783 if (!(ret = PyList_New(0)))
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 Moolenaar21642ed2013-05-29 22:20:01 +0200792 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
793 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200794 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200796 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200797 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200799 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200800 {
801 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200802 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200803 return NULL;
804 }
805 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200806 }
807 }
808 else if (our_tv->v_type == VAR_DICT)
809 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100811 hashtab_T *ht;
812 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200813 hashitem_T *hi;
814 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100815
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200816 if (our_tv->vval.v_dict == NULL)
817 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100818 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200819
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200820 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200821 return NULL;
822
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200823 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200824 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200825 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200826 return NULL;
827 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200828
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100829 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200830 for (hi = ht->ht_array; todo > 0; ++hi)
831 {
832 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200833 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200834 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200835
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200836 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200837 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200838 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200839 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200840 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200841 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200842 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200843 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200844 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200845 Py_DECREF(newObj);
846 return NULL;
847 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200848 }
849 }
850 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100851 else if (our_tv->v_type == VAR_SPECIAL)
852 {
853 if (our_tv->vval.v_number == VVAL_FALSE)
854 {
855 ret = Py_False;
856 Py_INCREF(ret);
857 }
858 else if (our_tv->vval.v_number == VVAL_TRUE)
859 {
860 ret = Py_True;
861 Py_INCREF(ret);
862 }
863 else
864 {
865 Py_INCREF(Py_None);
866 ret = Py_None;
867 }
868 return ret;
869 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200870 else
871 {
872 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200873 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200874 }
875
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200876 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200877}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200878
879 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200880VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200881{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200882 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200883 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200884 PyObject *string;
885 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200886 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200887 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200888
Bram Moolenaar389a1792013-06-23 13:00:44 +0200889 if (!PyArg_ParseTuple(args, "O", &string))
890 return NULL;
891
892 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200893 return NULL;
894
895 Py_BEGIN_ALLOW_THREADS
896 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200897 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200898 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200899 Python_Release_Vim();
900 Py_END_ALLOW_THREADS
901
Bram Moolenaar389a1792013-06-23 13:00:44 +0200902 Py_XDECREF(todecref);
903
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200904 if (VimTryEnd())
905 return NULL;
906
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200907 if (our_tv == NULL)
908 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200909 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200910 return NULL;
911 }
912
913 /* Convert the Vim type into a Python type. Create a dictionary that's
914 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200915 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200916 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200917 else
918 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200919 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200920 Py_DECREF(lookup_dict);
921 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200922
923
924 Py_BEGIN_ALLOW_THREADS
925 Python_Lock_Vim();
926 free_tv(our_tv);
927 Python_Release_Vim();
928 Py_END_ALLOW_THREADS
929
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200930 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200931}
932
Bram Moolenaardb913952012-06-29 12:54:53 +0200933static PyObject *ConvertToPyObject(typval_T *);
934
935 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200936VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200937{
Bram Moolenaardb913952012-06-29 12:54:53 +0200938 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200939 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200940 char_u *expr;
941 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200942
Bram Moolenaar389a1792013-06-23 13:00:44 +0200943 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200944 return NULL;
945
946 Py_BEGIN_ALLOW_THREADS
947 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200948 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200949 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200950 Python_Release_Vim();
951 Py_END_ALLOW_THREADS
952
Bram Moolenaar389a1792013-06-23 13:00:44 +0200953 Py_XDECREF(todecref);
954
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200955 if (VimTryEnd())
956 return NULL;
957
Bram Moolenaardb913952012-06-29 12:54:53 +0200958 if (our_tv == NULL)
959 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200960 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200961 return NULL;
962 }
963
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200964 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200965 Py_BEGIN_ALLOW_THREADS
966 Python_Lock_Vim();
967 free_tv(our_tv);
968 Python_Release_Vim();
969 Py_END_ALLOW_THREADS
970
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200971 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200972}
973
974 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200975VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200976{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200977 char_u *str;
978 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200979 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200980
Bram Moolenaar389a1792013-06-23 13:00:44 +0200981 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200982 return NULL;
983
Bram Moolenaara54bf402012-12-05 16:30:07 +0100984#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200985 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100986#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200987 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100988#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200989
990 Py_XDECREF(todecref);
991
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200992 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200993}
994
Bram Moolenaarf4258302013-06-02 18:20:17 +0200995 static PyObject *
996_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
997{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200998 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200999 PyObject *newwd;
1000 PyObject *todecref;
1001 char_u *new_dir;
1002
Bram Moolenaard4209d22013-06-05 20:34:15 +02001003 if (_chdir == NULL)
1004 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001005 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +02001006 return NULL;
1007
1008 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
1009 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001010 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001011 return NULL;
1012 }
1013
1014 if (!(new_dir = StringToChars(newwd, &todecref)))
1015 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001016 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001017 Py_DECREF(newwd);
1018 return NULL;
1019 }
1020
1021 VimTryStart();
1022
1023 if (vim_chdir(new_dir))
1024 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001025 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001026 Py_DECREF(newwd);
1027 Py_XDECREF(todecref);
1028
1029 if (VimTryEnd())
1030 return NULL;
1031
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001032 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +02001033 return NULL;
1034 }
1035
1036 Py_DECREF(newwd);
1037 Py_XDECREF(todecref);
1038
1039 post_chdir(FALSE);
1040
1041 if (VimTryEnd())
1042 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001043 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001044 return NULL;
1045 }
1046
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001047 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001048}
1049
1050 static PyObject *
1051VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1052{
1053 return _VimChdir(py_chdir, args, kwargs);
1054}
1055
1056 static PyObject *
1057VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1058{
1059 return _VimChdir(py_fchdir, args, kwargs);
1060}
1061
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001062typedef struct {
1063 PyObject *callable;
1064 PyObject *result;
1065} map_rtp_data;
1066
1067 static void
1068map_rtp_callback(char_u *path, void *_data)
1069{
1070 void **data = (void **) _data;
1071 PyObject *pathObject;
1072 map_rtp_data *mr_data = *((map_rtp_data **) data);
1073
Bram Moolenaar41009372013-07-01 22:03:04 +02001074 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001075 {
1076 *data = NULL;
1077 return;
1078 }
1079
1080 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1081 pathObject, NULL);
1082
1083 Py_DECREF(pathObject);
1084
1085 if (!mr_data->result || mr_data->result != Py_None)
1086 *data = NULL;
1087 else
1088 {
1089 Py_DECREF(mr_data->result);
1090 mr_data->result = NULL;
1091 }
1092}
1093
1094 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001095VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001096{
1097 map_rtp_data data;
1098
Bram Moolenaar389a1792013-06-23 13:00:44 +02001099 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001100 data.result = NULL;
1101
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001102 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001103
1104 if (data.result == NULL)
1105 {
1106 if (PyErr_Occurred())
1107 return NULL;
1108 else
1109 {
1110 Py_INCREF(Py_None);
1111 return Py_None;
1112 }
1113 }
1114 return data.result;
1115}
1116
1117/*
1118 * _vim_runtimepath_ special path implementation.
1119 */
1120
1121 static void
1122map_finder_callback(char_u *path, void *_data)
1123{
1124 void **data = (void **) _data;
1125 PyObject *list = *((PyObject **) data);
1126 PyObject *pathObject1, *pathObject2;
1127 char *pathbuf;
1128 size_t pathlen;
1129
1130 pathlen = STRLEN(path);
1131
1132#if PY_MAJOR_VERSION < 3
1133# define PY_MAIN_DIR_STRING "python2"
1134#else
1135# define PY_MAIN_DIR_STRING "python3"
1136#endif
1137#define PY_ALTERNATE_DIR_STRING "pythonx"
1138
1139#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1140 if (!(pathbuf = PyMem_New(char,
1141 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1142 {
1143 PyErr_NoMemory();
1144 *data = NULL;
1145 return;
1146 }
1147
1148 mch_memmove(pathbuf, path, pathlen + 1);
1149 add_pathsep((char_u *) pathbuf);
1150
1151 pathlen = STRLEN(pathbuf);
1152 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1153 PYTHONX_STRING_LENGTH + 1);
1154
1155 if (!(pathObject1 = PyString_FromString(pathbuf)))
1156 {
1157 *data = NULL;
1158 PyMem_Free(pathbuf);
1159 return;
1160 }
1161
1162 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1163 PYTHONX_STRING_LENGTH + 1);
1164
1165 if (!(pathObject2 = PyString_FromString(pathbuf)))
1166 {
1167 Py_DECREF(pathObject1);
1168 PyMem_Free(pathbuf);
1169 *data = NULL;
1170 return;
1171 }
1172
1173 PyMem_Free(pathbuf);
1174
1175 if (PyList_Append(list, pathObject1)
1176 || PyList_Append(list, pathObject2))
1177 *data = NULL;
1178
1179 Py_DECREF(pathObject1);
1180 Py_DECREF(pathObject2);
1181}
1182
1183 static PyObject *
1184Vim_GetPaths(PyObject *self UNUSED)
1185{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001186 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001187
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001188 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001189 return NULL;
1190
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001191 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001192
1193 if (PyErr_Occurred())
1194 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001195 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001196 return NULL;
1197 }
1198
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001199 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001200}
1201
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001202#if PY_VERSION_HEX >= 0x030700f0
1203 static PyObject *
1204FinderFindSpec(PyObject *self, PyObject *args)
1205{
1206 char *fullname;
1207 PyObject *paths;
1208 PyObject *target = Py_None;
1209 PyObject *spec;
1210
1211 if (!PyArg_ParseTuple(args, "s|O", &fullname, &target))
1212 return NULL;
1213
1214 if (!(paths = Vim_GetPaths(self)))
1215 return NULL;
1216
1217 spec = PyObject_CallFunction(py_find_spec, "sNN", fullname, paths, target);
1218
1219 Py_DECREF(paths);
1220
1221 if (!spec)
1222 {
1223 if (PyErr_Occurred())
1224 return NULL;
1225
1226 Py_INCREF(Py_None);
1227 return Py_None;
1228 }
1229
1230 return spec;
1231}
1232#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001233 static PyObject *
1234call_load_module(char *name, int len, PyObject *find_module_result)
1235{
1236 PyObject *fd, *pathname, *description;
1237
Bram Moolenaarc476e522013-06-23 13:46:40 +02001238 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001239 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001240 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001241 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001242 Py_TYPE_NAME(find_module_result));
1243 return NULL;
1244 }
1245 if (PyTuple_GET_SIZE(find_module_result) != 3)
1246 {
1247 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001248 N_("expected 3-tuple as imp.find_module() result, but got "
1249 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001250 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001251 return NULL;
1252 }
1253
1254 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1255 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1256 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1257 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001258 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001259 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001260 return NULL;
1261 }
1262
1263 return PyObject_CallFunction(py_load_module,
1264 "s#OOO", name, len, fd, pathname, description);
1265}
1266
1267 static PyObject *
1268find_module(char *fullname, char *tail, PyObject *new_path)
1269{
1270 PyObject *find_module_result;
1271 PyObject *module;
1272 char *dot;
1273
Bram Moolenaar41009372013-07-01 22:03:04 +02001274 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001275 {
1276 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001277 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001278 * first component
1279 */
1280 PyObject *newest_path;
1281 int partlen = (int) (dot - 1 - tail);
1282
1283 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1284 "s#O", tail, partlen, new_path)))
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001285 {
1286 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1287 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001288 return NULL;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001289 }
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001290
1291 if (!(module = call_load_module(
1292 fullname,
1293 ((int) (tail - fullname)) + partlen,
1294 find_module_result)))
1295 {
1296 Py_DECREF(find_module_result);
1297 return NULL;
1298 }
1299
1300 Py_DECREF(find_module_result);
1301
1302 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1303 {
1304 Py_DECREF(module);
1305 return NULL;
1306 }
1307
1308 Py_DECREF(module);
1309
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001310 find_module_result = find_module(fullname, dot + 1, newest_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001311
1312 Py_DECREF(newest_path);
1313
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001314 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001315 }
1316 else
1317 {
1318 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1319 "sO", tail, new_path)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001320 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001321 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_ImportError))
1322 PyErr_Clear();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001323 return NULL;
1324 }
1325
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001326 return find_module_result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001327 }
1328}
1329
1330 static PyObject *
1331FinderFindModule(PyObject *self, PyObject *args)
1332{
1333 char *fullname;
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001334 PyObject *result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001335 PyObject *new_path;
1336 LoaderObject *loader;
1337
1338 if (!PyArg_ParseTuple(args, "s", &fullname))
1339 return NULL;
1340
1341 if (!(new_path = Vim_GetPaths(self)))
1342 return NULL;
1343
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001344 result = find_module(fullname, fullname, new_path);
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001345
1346 Py_DECREF(new_path);
1347
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001348 if (!result)
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001349 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001350 if (PyErr_Occurred())
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001351 return NULL;
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001352
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001353 Py_INCREF(Py_None);
1354 return Py_None;
1355 }
1356
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001357 if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001358 {
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001359 Py_DECREF(result);
1360 PyErr_NoMemory();
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001361 return NULL;
1362 }
1363
Bram Moolenaar447bd5a2018-08-07 19:45:27 +02001364 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1365 {
1366 vim_free(fullname);
1367 Py_DECREF(result);
1368 return NULL;
1369 }
1370
1371 loader->fullname = fullname;
1372 loader->result = result;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001373
1374 return (PyObject *) loader;
1375}
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001376#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001377
1378 static PyObject *
1379VimPathHook(PyObject *self UNUSED, PyObject *args)
1380{
1381 char *path;
1382
1383 if (PyArg_ParseTuple(args, "s", &path)
1384 && STRCMP(path, vim_special_path) == 0)
1385 {
1386 Py_INCREF(vim_module);
1387 return vim_module;
1388 }
1389
1390 PyErr_Clear();
1391 PyErr_SetNone(PyExc_ImportError);
1392 return NULL;
1393}
1394
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001395/*
1396 * Vim module - Definitions
1397 */
1398
1399static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001400 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001401 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001402 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001403 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1404 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001405 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1406 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001407 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001408#if PY_VERSION_HEX >= 0x030700f0
1409 {"find_spec", FinderFindSpec, METH_VARARGS, "Internal use only, returns spec object for any input it receives"},
1410#else
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001411 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02001412#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001413 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1414 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1415 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001416};
1417
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001418/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001419 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001420 */
1421
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001422static PyTypeObject IterType;
1423
1424typedef PyObject *(*nextfun)(void **);
1425typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001426typedef int (*traversefun)(void *, visitproc, void *);
1427typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001428
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001429/* Main purpose of this object is removing the need for do python
1430 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1431 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001432
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001433typedef struct
1434{
1435 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001436 void *cur;
1437 nextfun next;
1438 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001439 traversefun traverse;
1440 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001441} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001442
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001443 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001444IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1445 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001446{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001447 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001448
Bram Moolenaar774267b2013-05-21 20:51:59 +02001449 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001450 self->cur = start;
1451 self->next = next;
1452 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001453 self->traverse = traverse;
1454 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001455
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001456 return (PyObject *)(self);
1457}
1458
1459 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001460IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001461{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001462 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001463 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001464 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001465}
1466
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001467 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001468IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001469{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001470 if (self->traverse != NULL)
1471 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001472 else
1473 return 0;
1474}
1475
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001476/* Mac OSX defines clear() somewhere. */
1477#ifdef clear
1478# undef clear
1479#endif
1480
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001481 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001482IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001483{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001484 if (self->clear != NULL)
1485 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001486 else
1487 return 0;
1488}
1489
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001490 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001491IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001492{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001493 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001494}
1495
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001496 static PyObject *
1497IterIter(PyObject *self)
1498{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001499 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001500 return self;
1501}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001502
Bram Moolenaardb913952012-06-29 12:54:53 +02001503typedef struct pylinkedlist_S {
1504 struct pylinkedlist_S *pll_next;
1505 struct pylinkedlist_S *pll_prev;
1506 PyObject *pll_obj;
1507} pylinkedlist_T;
1508
1509static pylinkedlist_T *lastdict = NULL;
1510static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001511static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001512
1513 static void
1514pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1515{
1516 if (ref->pll_prev == NULL)
1517 {
1518 if (ref->pll_next == NULL)
1519 {
1520 *last = NULL;
1521 return;
1522 }
1523 }
1524 else
1525 ref->pll_prev->pll_next = ref->pll_next;
1526
1527 if (ref->pll_next == NULL)
1528 *last = ref->pll_prev;
1529 else
1530 ref->pll_next->pll_prev = ref->pll_prev;
1531}
1532
1533 static void
1534pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1535{
1536 if (*last == NULL)
1537 ref->pll_prev = NULL;
1538 else
1539 {
1540 (*last)->pll_next = ref;
1541 ref->pll_prev = *last;
1542 }
1543 ref->pll_next = NULL;
1544 ref->pll_obj = self;
1545 *last = ref;
1546}
1547
1548static PyTypeObject DictionaryType;
1549
1550typedef struct
1551{
1552 PyObject_HEAD
1553 dict_T *dict;
1554 pylinkedlist_T ref;
1555} DictionaryObject;
1556
Bram Moolenaara9922d62013-05-30 13:01:18 +02001557static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1558
1559#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1560
Bram Moolenaardb913952012-06-29 12:54:53 +02001561 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001562DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001563{
1564 DictionaryObject *self;
1565
Bram Moolenaara9922d62013-05-30 13:01:18 +02001566 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001567 if (self == NULL)
1568 return NULL;
1569 self->dict = dict;
1570 ++dict->dv_refcount;
1571
1572 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1573
1574 return (PyObject *)(self);
1575}
1576
Bram Moolenaara9922d62013-05-30 13:01:18 +02001577 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001578py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001579{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001580 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001581
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001582 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001583 {
1584 PyErr_NoMemory();
1585 return NULL;
1586 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001587 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001588
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001589 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001590}
1591
1592 static PyObject *
1593DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1594{
1595 DictionaryObject *self;
1596 dict_T *dict;
1597
1598 if (!(dict = py_dict_alloc()))
1599 return NULL;
1600
1601 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1602
1603 --dict->dv_refcount;
1604
1605 if (kwargs || PyTuple_Size(args))
1606 {
1607 PyObject *tmp;
1608 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1609 {
1610 Py_DECREF(self);
1611 return NULL;
1612 }
1613
1614 Py_DECREF(tmp);
1615 }
1616
1617 return (PyObject *)(self);
1618}
1619
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001620 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001621DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001622{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001623 pyll_remove(&self->ref, &lastdict);
1624 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001625
1626 DESTRUCTOR_FINISH(self);
1627}
1628
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001629static char *DictionaryAttrs[] = {
1630 "locked", "scope",
1631 NULL
1632};
1633
1634 static PyObject *
1635DictionaryDir(PyObject *self)
1636{
1637 return ObjectDir(self, DictionaryAttrs);
1638}
1639
Bram Moolenaardb913952012-06-29 12:54:53 +02001640 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001641DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001642{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001643 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001644 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001645 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001646 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001647 return -1;
1648 }
1649
1650 if (strcmp(name, "locked") == 0)
1651 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001652 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001653 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001654 PyErr_SET_STRING(PyExc_TypeError,
1655 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001656 return -1;
1657 }
1658 else
1659 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001660 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001661 if (istrue == -1)
1662 return -1;
1663 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001664 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001665 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001666 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001667 }
1668 return 0;
1669 }
1670 else
1671 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001672 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001673 return -1;
1674 }
1675}
1676
1677 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001678DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001679{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001680 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001681}
1682
Bram Moolenaara9922d62013-05-30 13:01:18 +02001683#define DICT_FLAG_HAS_DEFAULT 0x01
1684#define DICT_FLAG_POP 0x02
1685#define DICT_FLAG_NONE_DEFAULT 0x04
1686#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1687#define DICT_FLAG_RETURN_PAIR 0x10
1688
Bram Moolenaardb913952012-06-29 12:54:53 +02001689 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001690_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001691{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001692 PyObject *keyObject;
1693 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001694 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001695 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001696 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001697 dict_T *dict = self->dict;
1698 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001699 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001700
Bram Moolenaara9922d62013-05-30 13:01:18 +02001701 if (flags & DICT_FLAG_HAS_DEFAULT)
1702 {
1703 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1704 return NULL;
1705 }
1706 else
1707 keyObject = args;
1708
1709 if (flags & DICT_FLAG_RETURN_BOOL)
1710 defObject = Py_False;
1711
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001712 if (!(key = StringToChars(keyObject, &todecref)))
1713 return NULL;
1714
1715 if (*key == NUL)
1716 {
1717 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001718 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001719 return NULL;
1720 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001721
Bram Moolenaara9922d62013-05-30 13:01:18 +02001722 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001723
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001724 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001725
Bram Moolenaara9922d62013-05-30 13:01:18 +02001726 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001727 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001728 if (defObject)
1729 {
1730 Py_INCREF(defObject);
1731 return defObject;
1732 }
1733 else
1734 {
1735 PyErr_SetObject(PyExc_KeyError, keyObject);
1736 return NULL;
1737 }
1738 }
1739 else if (flags & DICT_FLAG_RETURN_BOOL)
1740 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001741 ret = Py_True;
1742 Py_INCREF(ret);
1743 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001744 }
1745
1746 di = dict_lookup(hi);
1747
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001748 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001749 return NULL;
1750
1751 if (flags & DICT_FLAG_POP)
1752 {
1753 if (dict->dv_lock)
1754 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001755 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001756 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001757 return NULL;
1758 }
1759
1760 hash_remove(&dict->dv_hashtab, hi);
1761 dictitem_free(di);
1762 }
1763
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001764 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001765}
1766
1767 static PyObject *
1768DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1769{
1770 return _DictionaryItem(self, keyObject, 0);
1771}
1772
1773 static int
1774DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1775{
1776 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001777 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001778
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001779 if (rObj == NULL)
1780 return -1;
1781
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001782 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001783
Bram Moolenaardee2e312013-06-23 16:35:47 +02001784 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001785
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001786 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001787}
1788
1789typedef struct
1790{
1791 hashitem_T *ht_array;
1792 long_u ht_used;
1793 hashtab_T *ht;
1794 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001795 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001796} dictiterinfo_T;
1797
1798 static PyObject *
1799DictionaryIterNext(dictiterinfo_T **dii)
1800{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001801 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001802
1803 if (!(*dii)->todo)
1804 return NULL;
1805
1806 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1807 (*dii)->ht->ht_used != (*dii)->ht_used)
1808 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001809 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001810 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001811 return NULL;
1812 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001813
Bram Moolenaara9922d62013-05-30 13:01:18 +02001814 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1815 ++((*dii)->hi);
1816
1817 --((*dii)->todo);
1818
Bram Moolenaar41009372013-07-01 22:03:04 +02001819 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001820 return NULL;
1821
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001822 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001823}
1824
1825 static PyObject *
1826DictionaryIter(DictionaryObject *self)
1827{
1828 dictiterinfo_T *dii;
1829 hashtab_T *ht;
1830
1831 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1832 {
1833 PyErr_NoMemory();
1834 return NULL;
1835 }
1836
1837 ht = &self->dict->dv_hashtab;
1838 dii->ht_array = ht->ht_array;
1839 dii->ht_used = ht->ht_used;
1840 dii->ht = ht;
1841 dii->hi = dii->ht_array;
1842 dii->todo = dii->ht_used;
1843
1844 return IterNew(dii,
1845 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1846 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001847}
1848
1849 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001850DictionaryAssItem(
1851 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001852{
1853 char_u *key;
1854 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001855 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001856 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001857 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001858
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001859 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001860 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001861 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001862 return -1;
1863 }
1864
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001865 if (!(key = StringToChars(keyObject, &todecref)))
1866 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001867
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001868 if (*key == NUL)
1869 {
1870 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001871 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001872 return -1;
1873 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001874
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001875 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001876
1877 if (valObject == NULL)
1878 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001879 hashitem_T *hi;
1880
Bram Moolenaardb913952012-06-29 12:54:53 +02001881 if (di == NULL)
1882 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001883 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001884 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001885 return -1;
1886 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001887 hi = hash_find(&dict->dv_hashtab, di->di_key);
1888 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001889 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001890 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001891 return 0;
1892 }
1893
1894 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001895 {
1896 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001897 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001898 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001899
1900 if (di == NULL)
1901 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001902 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001903 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001904 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001905 PyErr_NoMemory();
1906 return -1;
1907 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02001908 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001909
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001910 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001911 {
1912 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001913 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001914 RAISE_KEY_ADD_FAIL(key);
1915 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001916 return -1;
1917 }
1918 }
1919 else
1920 clear_tv(&di->di_tv);
1921
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001922 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001923
1924 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001925 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001926 return 0;
1927}
1928
Bram Moolenaara9922d62013-05-30 13:01:18 +02001929typedef PyObject *(*hi_to_py)(hashitem_T *);
1930
Bram Moolenaardb913952012-06-29 12:54:53 +02001931 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001932DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001933{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001934 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001935 long_u todo = dict->dv_hashtab.ht_used;
1936 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001937 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001938 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001939 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001940
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001941 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001942 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1943 {
1944 if (!HASHITEM_EMPTY(hi))
1945 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001946 if (!(newObj = hiconvert(hi)))
1947 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001948 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001949 return NULL;
1950 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001951 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001952 --todo;
1953 ++i;
1954 }
1955 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001956 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001957}
1958
Bram Moolenaara9922d62013-05-30 13:01:18 +02001959 static PyObject *
1960dict_key(hashitem_T *hi)
1961{
1962 return PyBytes_FromString((char *)(hi->hi_key));
1963}
1964
1965 static PyObject *
1966DictionaryListKeys(DictionaryObject *self)
1967{
1968 return DictionaryListObjects(self, dict_key);
1969}
1970
1971 static PyObject *
1972dict_val(hashitem_T *hi)
1973{
1974 dictitem_T *di;
1975
1976 di = dict_lookup(hi);
1977 return ConvertToPyObject(&di->di_tv);
1978}
1979
1980 static PyObject *
1981DictionaryListValues(DictionaryObject *self)
1982{
1983 return DictionaryListObjects(self, dict_val);
1984}
1985
1986 static PyObject *
1987dict_item(hashitem_T *hi)
1988{
1989 PyObject *keyObject;
1990 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001991 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001992
1993 if (!(keyObject = dict_key(hi)))
1994 return NULL;
1995
1996 if (!(valObject = dict_val(hi)))
1997 {
1998 Py_DECREF(keyObject);
1999 return NULL;
2000 }
2001
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002002 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002003
2004 Py_DECREF(keyObject);
2005 Py_DECREF(valObject);
2006
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002007 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002008}
2009
2010 static PyObject *
2011DictionaryListItems(DictionaryObject *self)
2012{
2013 return DictionaryListObjects(self, dict_item);
2014}
2015
2016 static PyObject *
2017DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
2018{
2019 dict_T *dict = self->dict;
2020
2021 if (dict->dv_lock)
2022 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002023 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002024 return NULL;
2025 }
2026
2027 if (kwargs)
2028 {
2029 typval_T tv;
2030
2031 if (ConvertFromPyMapping(kwargs, &tv) == -1)
2032 return NULL;
2033
2034 VimTryStart();
2035 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
2036 clear_tv(&tv);
2037 if (VimTryEnd())
2038 return NULL;
2039 }
2040 else
2041 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002042 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002043
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002044 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002045 return NULL;
2046
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01002047 if (obj == NULL)
2048 {
2049 Py_INCREF(Py_None);
2050 return Py_None;
2051 }
2052
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002053 if (PyObject_HasAttrString(obj, "keys"))
2054 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002055 else
2056 {
2057 PyObject *iterator;
2058 PyObject *item;
2059
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002060 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002061 return NULL;
2062
2063 while ((item = PyIter_Next(iterator)))
2064 {
2065 PyObject *fast;
2066 PyObject *keyObject;
2067 PyObject *valObject;
2068 PyObject *todecref;
2069 char_u *key;
2070 dictitem_T *di;
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002071 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002072
2073 if (!(fast = PySequence_Fast(item, "")))
2074 {
2075 Py_DECREF(iterator);
2076 Py_DECREF(item);
2077 return NULL;
2078 }
2079
2080 Py_DECREF(item);
2081
2082 if (PySequence_Fast_GET_SIZE(fast) != 2)
2083 {
2084 Py_DECREF(iterator);
2085 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002086 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002087 N_("expected sequence element of size 2, "
2088 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002089 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002090 return NULL;
2091 }
2092
2093 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2094
2095 if (!(key = StringToChars(keyObject, &todecref)))
2096 {
2097 Py_DECREF(iterator);
2098 Py_DECREF(fast);
2099 return NULL;
2100 }
2101
2102 di = dictitem_alloc(key);
2103
2104 Py_XDECREF(todecref);
2105
2106 if (di == NULL)
2107 {
2108 Py_DECREF(fast);
2109 Py_DECREF(iterator);
2110 PyErr_NoMemory();
2111 return NULL;
2112 }
Bram Moolenaara9922d62013-05-30 13:01:18 +02002113 di->di_tv.v_type = VAR_UNKNOWN;
2114
2115 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2116
2117 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2118 {
2119 Py_DECREF(iterator);
2120 Py_DECREF(fast);
2121 dictitem_free(di);
2122 return NULL;
2123 }
2124
2125 Py_DECREF(fast);
2126
Bram Moolenaar9ed7d342017-11-09 22:10:33 +01002127 hi = hash_find(&dict->dv_hashtab, di->di_key);
2128 if (!HASHITEM_EMPTY(hi) || dict_add(dict, di) == FAIL)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002129 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002130 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002131 Py_DECREF(iterator);
2132 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002133 return NULL;
2134 }
2135 }
2136
2137 Py_DECREF(iterator);
2138
2139 /* Iterator may have finished due to an exception */
2140 if (PyErr_Occurred())
2141 return NULL;
2142 }
2143 }
2144 Py_INCREF(Py_None);
2145 return Py_None;
2146}
2147
2148 static PyObject *
2149DictionaryGet(DictionaryObject *self, PyObject *args)
2150{
2151 return _DictionaryItem(self, args,
2152 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2153}
2154
2155 static PyObject *
2156DictionaryPop(DictionaryObject *self, PyObject *args)
2157{
2158 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2159}
2160
2161 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002162DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002163{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002164 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002165 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002166 PyObject *valObject;
2167 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002168
Bram Moolenaarde71b562013-06-02 17:41:54 +02002169 if (self->dict->dv_hashtab.ht_used == 0)
2170 {
2171 PyErr_SetNone(PyExc_KeyError);
2172 return NULL;
2173 }
2174
2175 hi = self->dict->dv_hashtab.ht_array;
2176 while (HASHITEM_EMPTY(hi))
2177 ++hi;
2178
2179 di = dict_lookup(hi);
2180
2181 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002182 return NULL;
2183
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002184 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002185 {
2186 Py_DECREF(valObject);
2187 return NULL;
2188 }
2189
2190 hash_remove(&self->dict->dv_hashtab, hi);
2191 dictitem_free(di);
2192
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002193 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002194}
2195
2196 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002197DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002198{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002199 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2200}
2201
2202static PySequenceMethods DictionaryAsSeq = {
2203 0, /* sq_length */
2204 0, /* sq_concat */
2205 0, /* sq_repeat */
2206 0, /* sq_item */
2207 0, /* sq_slice */
2208 0, /* sq_ass_item */
2209 0, /* sq_ass_slice */
2210 (objobjproc) DictionaryContains, /* sq_contains */
2211 0, /* sq_inplace_concat */
2212 0, /* sq_inplace_repeat */
2213};
2214
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002215static PyMappingMethods DictionaryAsMapping = {
2216 (lenfunc) DictionaryLength,
2217 (binaryfunc) DictionaryItem,
2218 (objobjargproc) DictionaryAssItem,
2219};
2220
Bram Moolenaardb913952012-06-29 12:54:53 +02002221static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002222 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002223 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2224 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2225 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2226 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2227 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002228 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002229 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002230 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2231 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002232};
2233
2234static PyTypeObject ListType;
2235
2236typedef struct
2237{
2238 PyObject_HEAD
2239 list_T *list;
2240 pylinkedlist_T ref;
2241} ListObject;
2242
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002243#define NEW_LIST(list) ListNew(&ListType, list)
2244
Bram Moolenaardb913952012-06-29 12:54:53 +02002245 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002246ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002247{
2248 ListObject *self;
2249
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002250 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002251 if (self == NULL)
2252 return NULL;
2253 self->list = list;
2254 ++list->lv_refcount;
2255
2256 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2257
2258 return (PyObject *)(self);
2259}
2260
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002261 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002262py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002263{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002264 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002265
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002266 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002267 {
2268 PyErr_NoMemory();
2269 return NULL;
2270 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002271 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002272
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002273 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002274}
2275
2276 static int
2277list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2278{
2279 PyObject *iterator;
2280 PyObject *item;
2281 listitem_T *li;
2282
2283 if (!(iterator = PyObject_GetIter(obj)))
2284 return -1;
2285
2286 while ((item = PyIter_Next(iterator)))
2287 {
2288 if (!(li = listitem_alloc()))
2289 {
2290 PyErr_NoMemory();
2291 Py_DECREF(item);
2292 Py_DECREF(iterator);
2293 return -1;
2294 }
2295 li->li_tv.v_lock = 0;
2296 li->li_tv.v_type = VAR_UNKNOWN;
2297
2298 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2299 {
2300 Py_DECREF(item);
2301 Py_DECREF(iterator);
2302 listitem_free(li);
2303 return -1;
2304 }
2305
2306 Py_DECREF(item);
2307
2308 list_append(l, li);
2309 }
2310
2311 Py_DECREF(iterator);
2312
2313 /* Iterator may have finished due to an exception */
2314 if (PyErr_Occurred())
2315 return -1;
2316
2317 return 0;
2318}
2319
2320 static PyObject *
2321ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2322{
2323 list_T *list;
2324 PyObject *obj = NULL;
2325
2326 if (kwargs)
2327 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002328 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002329 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002330 return NULL;
2331 }
2332
2333 if (!PyArg_ParseTuple(args, "|O", &obj))
2334 return NULL;
2335
2336 if (!(list = py_list_alloc()))
2337 return NULL;
2338
2339 if (obj)
2340 {
2341 PyObject *lookup_dict;
2342
2343 if (!(lookup_dict = PyDict_New()))
2344 {
2345 list_unref(list);
2346 return NULL;
2347 }
2348
2349 if (list_py_concat(list, obj, lookup_dict) == -1)
2350 {
2351 Py_DECREF(lookup_dict);
2352 list_unref(list);
2353 return NULL;
2354 }
2355
2356 Py_DECREF(lookup_dict);
2357 }
2358
2359 return ListNew(subtype, list);
2360}
2361
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002362 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002363ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002364{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002365 pyll_remove(&self->ref, &lastlist);
2366 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002367
2368 DESTRUCTOR_FINISH(self);
2369}
2370
Bram Moolenaardb913952012-06-29 12:54:53 +02002371 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002372ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002373{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002374 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002375}
2376
2377 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002378ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002379{
2380 listitem_T *li;
2381
Bram Moolenaard6e39182013-05-21 18:30:34 +02002382 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002383 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002384 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002385 return NULL;
2386 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002387 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002388 if (li == NULL)
2389 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002390 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002391 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002392 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002393 return NULL;
2394 }
2395 return ConvertToPyObject(&li->li_tv);
2396}
2397
Bram Moolenaardb913952012-06-29 12:54:53 +02002398 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002399ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2400 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002401{
2402 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002403 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002404
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002405 if (step == 0)
2406 {
2407 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2408 return NULL;
2409 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002410
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002411 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002412 if (list == NULL)
2413 return NULL;
2414
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002415 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002416 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002417 PyObject *item;
2418
2419 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002420 if (item == NULL)
2421 {
2422 Py_DECREF(list);
2423 return NULL;
2424 }
2425
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002426 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002427 }
2428
2429 return list;
2430}
2431
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002432 static PyObject *
2433ListItem(ListObject *self, PyObject* idx)
2434{
2435#if PY_MAJOR_VERSION < 3
2436 if (PyInt_Check(idx))
2437 {
2438 long _idx = PyInt_AsLong(idx);
2439 return ListIndex(self, _idx);
2440 }
2441 else
2442#endif
2443 if (PyLong_Check(idx))
2444 {
2445 long _idx = PyLong_AsLong(idx);
2446 return ListIndex(self, _idx);
2447 }
2448 else if (PySlice_Check(idx))
2449 {
2450 Py_ssize_t start, stop, step, slicelen;
2451
Bram Moolenaar922a4662014-03-30 16:11:43 +02002452 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002453 &start, &stop, &step, &slicelen) < 0)
2454 return NULL;
2455 return ListSlice(self, start, step, slicelen);
2456 }
2457 else
2458 {
2459 RAISE_INVALID_INDEX_TYPE(idx);
2460 return NULL;
2461 }
2462}
2463
2464 static void
2465list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2466 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2467{
2468 while (numreplaced--)
2469 {
2470 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2471 listitem_remove(l, lis[slicelen + numreplaced]);
2472 }
2473 while (numadded--)
2474 {
2475 listitem_T *next;
2476
2477 next = lastaddedli->li_prev;
2478 listitem_remove(l, lastaddedli);
2479 lastaddedli = next;
2480 }
2481}
2482
2483 static int
2484ListAssSlice(ListObject *self, Py_ssize_t first,
2485 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2486{
2487 PyObject *iterator;
2488 PyObject *item;
2489 listitem_T *li;
2490 listitem_T *lastaddedli = NULL;
2491 listitem_T *next;
2492 typval_T v;
2493 list_T *l = self->list;
2494 PyInt i;
2495 PyInt j;
2496 PyInt numreplaced = 0;
2497 PyInt numadded = 0;
2498 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002499 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002500
2501 size = ListLength(self);
2502
2503 if (l->lv_lock)
2504 {
2505 RAISE_LOCKED_LIST;
2506 return -1;
2507 }
2508
2509 if (step == 0)
2510 {
2511 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2512 return -1;
2513 }
2514
2515 if (step != 1 && slicelen == 0)
2516 {
2517 /* Nothing to do. Only error out if obj has some items. */
2518 int ret = 0;
2519
2520 if (obj == NULL)
2521 return 0;
2522
2523 if (!(iterator = PyObject_GetIter(obj)))
2524 return -1;
2525
2526 if ((item = PyIter_Next(iterator)))
2527 {
2528 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002529 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002530 "to extended slice"), 0);
2531 Py_DECREF(item);
2532 ret = -1;
2533 }
2534 Py_DECREF(iterator);
2535 return ret;
2536 }
2537
2538 if (obj != NULL)
2539 /* XXX May allocate zero bytes. */
2540 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2541 {
2542 PyErr_NoMemory();
2543 return -1;
2544 }
2545
2546 if (first == size)
2547 li = NULL;
2548 else
2549 {
2550 li = list_find(l, (long) first);
2551 if (li == NULL)
2552 {
2553 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2554 (int)first);
2555 if (obj != NULL)
2556 PyMem_Free(lis);
2557 return -1;
2558 }
2559 i = slicelen;
2560 while (i-- && li != NULL)
2561 {
2562 j = step;
2563 next = li;
2564 if (step > 0)
2565 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2566 else
2567 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2568
2569 if (obj == NULL)
2570 listitem_remove(l, li);
2571 else
2572 lis[slicelen - i - 1] = li;
2573
2574 li = next;
2575 }
2576 if (li == NULL && i != -1)
2577 {
2578 PyErr_SET_VIM(N_("internal error: not enough list items"));
2579 if (obj != NULL)
2580 PyMem_Free(lis);
2581 return -1;
2582 }
2583 }
2584
2585 if (obj == NULL)
2586 return 0;
2587
2588 if (!(iterator = PyObject_GetIter(obj)))
2589 {
2590 PyMem_Free(lis);
2591 return -1;
2592 }
2593
2594 i = 0;
2595 while ((item = PyIter_Next(iterator)))
2596 {
2597 if (ConvertFromPyObject(item, &v) == -1)
2598 {
2599 Py_DECREF(iterator);
2600 Py_DECREF(item);
2601 PyMem_Free(lis);
2602 return -1;
2603 }
2604 Py_DECREF(item);
2605 if (list_insert_tv(l, &v, numreplaced < slicelen
2606 ? lis[numreplaced]
2607 : li) == FAIL)
2608 {
2609 clear_tv(&v);
2610 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2611 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2612 PyMem_Free(lis);
2613 return -1;
2614 }
2615 if (numreplaced < slicelen)
2616 {
2617 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002618 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002619 numreplaced++;
2620 }
2621 else
2622 {
2623 if (li)
2624 lastaddedli = li->li_prev;
2625 else
2626 lastaddedli = l->lv_last;
2627 numadded++;
2628 }
2629 clear_tv(&v);
2630 if (step != 1 && i >= slicelen)
2631 {
2632 Py_DECREF(iterator);
2633 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002634 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002635 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002636 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2637 PyMem_Free(lis);
2638 return -1;
2639 }
2640 ++i;
2641 }
2642 Py_DECREF(iterator);
2643
2644 if (step != 1 && i != slicelen)
2645 {
2646 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002647 N_("attempt to assign sequence of size %d to extended slice "
2648 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002649 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2650 PyMem_Free(lis);
2651 return -1;
2652 }
2653
2654 if (PyErr_Occurred())
2655 {
2656 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2657 PyMem_Free(lis);
2658 return -1;
2659 }
2660
2661 for (i = 0; i < numreplaced; i++)
2662 listitem_free(lis[i]);
2663 if (step == 1)
2664 for (i = numreplaced; i < slicelen; i++)
2665 listitem_remove(l, lis[i]);
2666
2667 PyMem_Free(lis);
2668
2669 return 0;
2670}
2671
2672 static int
2673ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2674{
2675 typval_T tv;
2676 list_T *l = self->list;
2677 listitem_T *li;
2678 Py_ssize_t length = ListLength(self);
2679
2680 if (l->lv_lock)
2681 {
2682 RAISE_LOCKED_LIST;
2683 return -1;
2684 }
2685 if (index > length || (index == length && obj == NULL))
2686 {
2687 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2688 return -1;
2689 }
2690
2691 if (obj == NULL)
2692 {
2693 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002694 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002695 clear_tv(&li->li_tv);
2696 vim_free(li);
2697 return 0;
2698 }
2699
2700 if (ConvertFromPyObject(obj, &tv) == -1)
2701 return -1;
2702
2703 if (index == length)
2704 {
2705 if (list_append_tv(l, &tv) == FAIL)
2706 {
2707 clear_tv(&tv);
2708 PyErr_SET_VIM(N_("failed to add item to list"));
2709 return -1;
2710 }
2711 }
2712 else
2713 {
2714 li = list_find(l, (long) index);
2715 clear_tv(&li->li_tv);
2716 copy_tv(&tv, &li->li_tv);
2717 clear_tv(&tv);
2718 }
2719 return 0;
2720}
2721
2722 static Py_ssize_t
2723ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2724{
2725#if PY_MAJOR_VERSION < 3
2726 if (PyInt_Check(idx))
2727 {
2728 long _idx = PyInt_AsLong(idx);
2729 return ListAssIndex(self, _idx, obj);
2730 }
2731 else
2732#endif
2733 if (PyLong_Check(idx))
2734 {
2735 long _idx = PyLong_AsLong(idx);
2736 return ListAssIndex(self, _idx, obj);
2737 }
2738 else if (PySlice_Check(idx))
2739 {
2740 Py_ssize_t start, stop, step, slicelen;
2741
Bram Moolenaar922a4662014-03-30 16:11:43 +02002742 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002743 &start, &stop, &step, &slicelen) < 0)
2744 return -1;
2745 return ListAssSlice(self, start, step, slicelen,
2746 obj);
2747 }
2748 else
2749 {
2750 RAISE_INVALID_INDEX_TYPE(idx);
2751 return -1;
2752 }
2753}
2754
2755 static PyObject *
2756ListConcatInPlace(ListObject *self, PyObject *obj)
2757{
2758 list_T *l = self->list;
2759 PyObject *lookup_dict;
2760
2761 if (l->lv_lock)
2762 {
2763 RAISE_LOCKED_LIST;
2764 return NULL;
2765 }
2766
2767 if (!(lookup_dict = PyDict_New()))
2768 return NULL;
2769
2770 if (list_py_concat(l, obj, lookup_dict) == -1)
2771 {
2772 Py_DECREF(lookup_dict);
2773 return NULL;
2774 }
2775 Py_DECREF(lookup_dict);
2776
2777 Py_INCREF(self);
2778 return (PyObject *)(self);
2779}
2780
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002781typedef struct
2782{
2783 listwatch_T lw;
2784 list_T *list;
2785} listiterinfo_T;
2786
2787 static void
2788ListIterDestruct(listiterinfo_T *lii)
2789{
2790 list_rem_watch(lii->list, &lii->lw);
2791 PyMem_Free(lii);
2792}
2793
2794 static PyObject *
2795ListIterNext(listiterinfo_T **lii)
2796{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002797 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002798
2799 if (!((*lii)->lw.lw_item))
2800 return NULL;
2801
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002802 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002803 return NULL;
2804
2805 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2806
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002807 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002808}
2809
2810 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002811ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002812{
2813 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002814 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002815
2816 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2817 {
2818 PyErr_NoMemory();
2819 return NULL;
2820 }
2821
2822 list_add_watch(l, &lii->lw);
2823 lii->lw.lw_item = l->lv_first;
2824 lii->list = l;
2825
2826 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002827 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2828 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002829}
2830
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002831static char *ListAttrs[] = {
2832 "locked",
2833 NULL
2834};
2835
2836 static PyObject *
2837ListDir(PyObject *self)
2838{
2839 return ObjectDir(self, ListAttrs);
2840}
2841
Bram Moolenaar66b79852012-09-21 14:00:35 +02002842 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002843ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002844{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002845 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002846 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002847 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002848 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002849 return -1;
2850 }
2851
2852 if (strcmp(name, "locked") == 0)
2853 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002854 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002855 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002856 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002857 return -1;
2858 }
2859 else
2860 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002861 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002862 if (istrue == -1)
2863 return -1;
2864 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002865 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002866 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002867 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002868 }
2869 return 0;
2870 }
2871 else
2872 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002873 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002874 return -1;
2875 }
2876}
2877
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002878static PySequenceMethods ListAsSeq = {
2879 (lenfunc) ListLength, /* sq_length, len(x) */
2880 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2881 0, /* RangeRepeat, sq_repeat, x*n */
2882 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2883 0, /* was_sq_slice, x[i:j] */
2884 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2885 0, /* was_sq_ass_slice, x[i:j]=v */
2886 0, /* sq_contains */
2887 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2888 0, /* sq_inplace_repeat */
2889};
2890
2891static PyMappingMethods ListAsMapping = {
2892 /* mp_length */ (lenfunc) ListLength,
2893 /* mp_subscript */ (binaryfunc) ListItem,
2894 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2895};
2896
Bram Moolenaardb913952012-06-29 12:54:53 +02002897static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002898 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2899 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2900 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002901};
2902
2903typedef struct
2904{
2905 PyObject_HEAD
2906 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002907 int argc;
2908 typval_T *argv;
2909 dict_T *self;
2910 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002911 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002912} FunctionObject;
2913
2914static PyTypeObject FunctionType;
2915
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002916#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2917 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002918
Bram Moolenaardb913952012-06-29 12:54:53 +02002919 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002920FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002921 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002922{
2923 FunctionObject *self;
2924
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002925 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2926
Bram Moolenaardb913952012-06-29 12:54:53 +02002927 if (self == NULL)
2928 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002929
2930 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002931 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002932 if (!translated_function_exists(name))
2933 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002934 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002935 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002936 return NULL;
2937 }
2938 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002939 }
2940 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002941 if ((self->name = get_expanded_name(name,
2942 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2943 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002944 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002945 PyErr_FORMAT(PyExc_ValueError,
2946 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002947 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002948 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002949
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002950 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002951 self->argc = argc;
2952 self->argv = argv;
2953 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002954 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002955
2956 if (self->argv || self->self)
2957 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2958
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002959 return (PyObject *)(self);
2960}
2961
2962 static PyObject *
2963FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2964{
2965 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002966 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002967 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002968 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002969 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002970 typval_T selfdicttv;
2971 typval_T argstv;
2972 list_T *argslist = NULL;
2973 dict_T *selfdict = NULL;
2974 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002975 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002976 typval_T *argv = NULL;
2977 typval_T *curtv;
2978 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002979
Bram Moolenaar8110a092016-04-14 15:56:09 +02002980 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002981 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02002982 selfdictObject = PyDict_GetItemString(kwargs, "self");
2983 if (selfdictObject != NULL)
2984 {
2985 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
2986 return NULL;
2987 selfdict = selfdicttv.vval.v_dict;
2988 }
2989 argsObject = PyDict_GetItemString(kwargs, "args");
2990 if (argsObject != NULL)
2991 {
2992 if (ConvertFromPySequence(argsObject, &argstv) == -1)
2993 {
2994 dict_unref(selfdict);
2995 return NULL;
2996 }
2997 argslist = argstv.vval.v_list;
2998
2999 argc = argslist->lv_len;
3000 if (argc != 0)
3001 {
3002 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003003 if (argv == NULL)
3004 {
3005 PyErr_NoMemory();
3006 dict_unref(selfdict);
3007 list_unref(argslist);
3008 return NULL;
3009 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003010 curtv = argv;
3011 for (li = argslist->lv_first; li != NULL; li = li->li_next)
3012 copy_tv(&li->li_tv, curtv++);
3013 }
3014 list_unref(argslist);
3015 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003016 if (selfdict != NULL)
3017 {
3018 auto_rebind = FALSE;
3019 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3020 if (autoRebindObject != NULL)
3021 {
3022 auto_rebind = PyObject_IsTrue(autoRebindObject);
3023 if (auto_rebind == -1)
3024 {
3025 dict_unref(selfdict);
3026 list_unref(argslist);
3027 return NULL;
3028 }
3029 }
3030 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003031 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003032
Bram Moolenaar389a1792013-06-23 13:00:44 +02003033 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003034 {
3035 dict_unref(selfdict);
3036 while (argc--)
3037 clear_tv(&argv[argc]);
3038 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003039 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003040 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003041
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003042 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003043
Bram Moolenaar389a1792013-06-23 13:00:44 +02003044 PyMem_Free(name);
3045
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003046 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003047}
3048
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003049 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003050FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003051{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003052 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003053 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003054 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003055 for (i = 0; i < self->argc; ++i)
3056 clear_tv(&self->argv[i]);
3057 PyMem_Free(self->argv);
3058 dict_unref(self->self);
3059 if (self->argv || self->self)
3060 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003061
3062 DESTRUCTOR_FINISH(self);
3063}
3064
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003065static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003066 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003067 NULL
3068};
3069
3070 static PyObject *
3071FunctionDir(PyObject *self)
3072{
3073 return ObjectDir(self, FunctionAttrs);
3074}
3075
Bram Moolenaardb913952012-06-29 12:54:53 +02003076 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003077FunctionAttr(FunctionObject *self, char *name)
3078{
3079 list_T *list;
3080 int i;
3081 if (strcmp(name, "name") == 0)
3082 return PyString_FromString((char *)(self->name));
3083 else if (strcmp(name, "args") == 0)
3084 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003085 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003086 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003087
Bram Moolenaar8110a092016-04-14 15:56:09 +02003088 for (i = 0; i < self->argc; ++i)
3089 list_append_tv(list, &self->argv[i]);
3090 return NEW_LIST(list);
3091 }
3092 else if (strcmp(name, "self") == 0)
3093 return self->self == NULL
3094 ? AlwaysNone(NULL)
3095 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003096 else if (strcmp(name, "auto_rebind") == 0)
3097 return self->auto_rebind
3098 ? AlwaysTrue(NULL)
3099 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003100 else if (strcmp(name, "__members__") == 0)
3101 return ObjectDir(NULL, FunctionAttrs);
3102 return NULL;
3103}
3104
3105/* Populate partial_T given function object.
3106 *
3107 * "exported" should be set to true when it is needed to construct a partial
3108 * that may be stored in a variable (i.e. may be freed by Vim).
3109 */
3110 static void
3111set_partial(FunctionObject *self, partial_T *pt, int exported)
3112{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003113 int i;
3114
3115 pt->pt_name = self->name;
3116 if (self->argv)
3117 {
3118 pt->pt_argc = self->argc;
3119 if (exported)
3120 {
3121 pt->pt_argv = (typval_T *)alloc_clear(
3122 sizeof(typval_T) * self->argc);
3123 for (i = 0; i < pt->pt_argc; ++i)
3124 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3125 }
3126 else
3127 pt->pt_argv = self->argv;
3128 }
3129 else
3130 {
3131 pt->pt_argc = 0;
3132 pt->pt_argv = NULL;
3133 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003134 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003135 pt->pt_dict = self->self;
3136 if (exported && self->self)
3137 ++pt->pt_dict->dv_refcount;
3138 if (exported)
3139 pt->pt_name = vim_strsave(pt->pt_name);
3140 pt->pt_refcount = 1;
3141}
3142
3143 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003144FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003145{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003146 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003147 typval_T args;
3148 typval_T selfdicttv;
3149 typval_T rettv;
3150 dict_T *selfdict = NULL;
3151 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003152 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003153 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003154 partial_T pt;
3155 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003156
Bram Moolenaar8110a092016-04-14 15:56:09 +02003157 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003158 return NULL;
3159
3160 if (kwargs != NULL)
3161 {
3162 selfdictObject = PyDict_GetItemString(kwargs, "self");
3163 if (selfdictObject != NULL)
3164 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003165 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003166 {
3167 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003168 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003169 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003170 selfdict = selfdicttv.vval.v_dict;
3171 }
3172 }
3173
Bram Moolenaar8110a092016-04-14 15:56:09 +02003174 if (self->argv || self->self)
3175 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003176 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003177 set_partial(self, &pt, FALSE);
3178 pt_ptr = &pt;
3179 }
3180
Bram Moolenaar71700b82013-05-15 17:49:05 +02003181 Py_BEGIN_ALLOW_THREADS
3182 Python_Lock_Vim();
3183
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003184 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003185 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003186
3187 Python_Release_Vim();
3188 Py_END_ALLOW_THREADS
3189
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003190 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003191 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003192 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003193 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003194 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003195 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003196 }
3197 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003198 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003199
Bram Moolenaardb913952012-06-29 12:54:53 +02003200 clear_tv(&args);
3201 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003202 if (selfdict != NULL)
3203 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003204
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003205 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003206}
3207
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003208 static PyObject *
3209FunctionRepr(FunctionObject *self)
3210{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003211 PyObject *ret;
3212 garray_T repr_ga;
3213 int i;
3214 char_u *tofree = NULL;
3215 typval_T tv;
3216 char_u numbuf[NUMBUFLEN];
3217
3218 ga_init2(&repr_ga, (int)sizeof(char), 70);
3219 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3220 if (self->name)
3221 ga_concat(&repr_ga, self->name);
3222 else
3223 ga_concat(&repr_ga, (char_u *)"<NULL>");
3224 ga_append(&repr_ga, '\'');
3225 if (self->argv)
3226 {
3227 ga_concat(&repr_ga, (char_u *)", args=[");
3228 ++emsg_silent;
3229 for (i = 0; i < self->argc; i++)
3230 {
3231 if (i != 0)
3232 ga_concat(&repr_ga, (char_u *)", ");
3233 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3234 get_copyID()));
3235 vim_free(tofree);
3236 }
3237 --emsg_silent;
3238 ga_append(&repr_ga, ']');
3239 }
3240 if (self->self)
3241 {
3242 ga_concat(&repr_ga, (char_u *)", self=");
3243 tv.v_type = VAR_DICT;
3244 tv.vval.v_dict = self->self;
3245 ++emsg_silent;
3246 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3247 --emsg_silent;
3248 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003249 if (self->auto_rebind)
3250 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003251 }
3252 ga_append(&repr_ga, '>');
3253 ret = PyString_FromString((char *)repr_ga.ga_data);
3254 ga_clear(&repr_ga);
3255 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003256}
3257
Bram Moolenaardb913952012-06-29 12:54:53 +02003258static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003259 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3260 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003261};
3262
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003263/*
3264 * Options object
3265 */
3266
3267static PyTypeObject OptionsType;
3268
3269typedef int (*checkfun)(void *);
3270
3271typedef struct
3272{
3273 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003274 int opt_type;
3275 void *from;
3276 checkfun Check;
3277 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003278} OptionsObject;
3279
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003280 static int
3281dummy_check(void *arg UNUSED)
3282{
3283 return 0;
3284}
3285
3286 static PyObject *
3287OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3288{
3289 OptionsObject *self;
3290
Bram Moolenaar774267b2013-05-21 20:51:59 +02003291 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003292 if (self == NULL)
3293 return NULL;
3294
3295 self->opt_type = opt_type;
3296 self->from = from;
3297 self->Check = Check;
3298 self->fromObj = fromObj;
3299 if (fromObj)
3300 Py_INCREF(fromObj);
3301
3302 return (PyObject *)(self);
3303}
3304
3305 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003306OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003307{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003308 PyObject_GC_UnTrack((void *)(self));
3309 Py_XDECREF(self->fromObj);
3310 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003311}
3312
3313 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003314OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003315{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003316 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003317 return 0;
3318}
3319
3320 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003321OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003322{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003323 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003324 return 0;
3325}
3326
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003327 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003328OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003329{
3330 char_u *key;
3331 int flags;
3332 long numval;
3333 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003334 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003335
Bram Moolenaard6e39182013-05-21 18:30:34 +02003336 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003337 return NULL;
3338
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003339 if (!(key = StringToChars(keyObject, &todecref)))
3340 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003341
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003342 if (*key == NUL)
3343 {
3344 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003345 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003346 return NULL;
3347 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003348
3349 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003350 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003351
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003352 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003353
3354 if (flags == 0)
3355 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003356 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003357 return NULL;
3358 }
3359
3360 if (flags & SOPT_UNSET)
3361 {
3362 Py_INCREF(Py_None);
3363 return Py_None;
3364 }
3365 else if (flags & SOPT_BOOL)
3366 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003367 PyObject *ret;
3368 ret = numval ? Py_True : Py_False;
3369 Py_INCREF(ret);
3370 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003371 }
3372 else if (flags & SOPT_NUM)
3373 return PyInt_FromLong(numval);
3374 else if (flags & SOPT_STRING)
3375 {
3376 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003377 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003378 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003379 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003380 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003381 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003382 else
3383 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003384 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003385 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003386 return NULL;
3387 }
3388 }
3389 else
3390 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003391 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003392 return NULL;
3393 }
3394}
3395
3396 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003397OptionsContains(OptionsObject *self, PyObject *keyObject)
3398{
3399 char_u *key;
3400 PyObject *todecref;
3401
3402 if (!(key = StringToChars(keyObject, &todecref)))
3403 return -1;
3404
3405 if (*key == NUL)
3406 {
3407 Py_XDECREF(todecref);
3408 return 0;
3409 }
3410
3411 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3412 {
3413 Py_XDECREF(todecref);
3414 return 1;
3415 }
3416 else
3417 {
3418 Py_XDECREF(todecref);
3419 return 0;
3420 }
3421}
3422
3423typedef struct
3424{
3425 void *lastoption;
3426 int opt_type;
3427} optiterinfo_T;
3428
3429 static PyObject *
3430OptionsIterNext(optiterinfo_T **oii)
3431{
3432 char_u *name;
3433
3434 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3435 return PyString_FromString((char *)name);
3436
3437 return NULL;
3438}
3439
3440 static PyObject *
3441OptionsIter(OptionsObject *self)
3442{
3443 optiterinfo_T *oii;
3444
3445 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3446 {
3447 PyErr_NoMemory();
3448 return NULL;
3449 }
3450
3451 oii->opt_type = self->opt_type;
3452 oii->lastoption = NULL;
3453
3454 return IterNew(oii,
3455 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3456 NULL, NULL);
3457}
3458
3459 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003460set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003461{
3462 char_u *errmsg;
3463
3464 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3465 {
3466 if (VimTryEnd())
3467 return FAIL;
3468 PyErr_SetVim((char *)errmsg);
3469 return FAIL;
3470 }
3471 return OK;
3472}
3473
3474 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003475set_option_value_for(
3476 char_u *key,
3477 int numval,
3478 char_u *stringval,
3479 int opt_flags,
3480 int opt_type,
3481 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003482{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003483 win_T *save_curwin = NULL;
3484 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003485 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003486 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003487
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003488 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003489 switch (opt_type)
3490 {
3491 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003492 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003493 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003494 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003495 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003496 if (VimTryEnd())
3497 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003498 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003499 return -1;
3500 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003501 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3502 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003503 break;
3504 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003505 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003506 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003507 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003508 break;
3509 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003510 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003511 break;
3512 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003513 if (set_ret == FAIL)
3514 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003515 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003516}
3517
3518 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003519OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003520{
3521 char_u *key;
3522 int flags;
3523 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003524 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003525 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003526
Bram Moolenaard6e39182013-05-21 18:30:34 +02003527 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003528 return -1;
3529
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003530 if (!(key = StringToChars(keyObject, &todecref)))
3531 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003532
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003533 if (*key == NUL)
3534 {
3535 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003536 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003537 return -1;
3538 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003539
3540 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003541 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003542
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003543 if (flags == 0)
3544 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003545 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003546 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003547 return -1;
3548 }
3549
3550 if (valObject == NULL)
3551 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003552 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003553 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003554 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003555 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003556 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003557 return -1;
3558 }
3559 else if (!(flags & SOPT_GLOBAL))
3560 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003561 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003562 N_("unable to unset option %s "
3563 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003564 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003565 return -1;
3566 }
3567 else
3568 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003569 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003570 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003571 return 0;
3572 }
3573 }
3574
Bram Moolenaard6e39182013-05-21 18:30:34 +02003575 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003576
3577 if (flags & SOPT_BOOL)
3578 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003579 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003580
Bram Moolenaarb983f752013-05-15 16:11:50 +02003581 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003582 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003583 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003584 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003585 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003586 }
3587 else if (flags & SOPT_NUM)
3588 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003589 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003590
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003591 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003592 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003593 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003594 return -1;
3595 }
3596
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003597 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003598 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003599 }
3600 else
3601 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003602 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003603 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003604
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003605 if ((val = StringToChars(valObject, &todecref2)))
3606 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003607 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003608 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003609 Py_XDECREF(todecref2);
3610 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003611 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003612 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003613 }
3614
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003615 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003616
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003617 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003618}
3619
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003620static PySequenceMethods OptionsAsSeq = {
3621 0, /* sq_length */
3622 0, /* sq_concat */
3623 0, /* sq_repeat */
3624 0, /* sq_item */
3625 0, /* sq_slice */
3626 0, /* sq_ass_item */
3627 0, /* sq_ass_slice */
3628 (objobjproc) OptionsContains, /* sq_contains */
3629 0, /* sq_inplace_concat */
3630 0, /* sq_inplace_repeat */
3631};
3632
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003633static PyMappingMethods OptionsAsMapping = {
3634 (lenfunc) NULL,
3635 (binaryfunc) OptionsItem,
3636 (objobjargproc) OptionsAssItem,
3637};
3638
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003639/* Tabpage object
3640 */
3641
3642typedef struct
3643{
3644 PyObject_HEAD
3645 tabpage_T *tab;
3646} TabPageObject;
3647
3648static PyObject *WinListNew(TabPageObject *tabObject);
3649
3650static PyTypeObject TabPageType;
3651
3652 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003653CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003654{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003655 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003656 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003657 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003658 return -1;
3659 }
3660
3661 return 0;
3662}
3663
3664 static PyObject *
3665TabPageNew(tabpage_T *tab)
3666{
3667 TabPageObject *self;
3668
3669 if (TAB_PYTHON_REF(tab))
3670 {
3671 self = TAB_PYTHON_REF(tab);
3672 Py_INCREF(self);
3673 }
3674 else
3675 {
3676 self = PyObject_NEW(TabPageObject, &TabPageType);
3677 if (self == NULL)
3678 return NULL;
3679 self->tab = tab;
3680 TAB_PYTHON_REF(tab) = self;
3681 }
3682
3683 return (PyObject *)(self);
3684}
3685
3686 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003687TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003688{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003689 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3690 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003691
3692 DESTRUCTOR_FINISH(self);
3693}
3694
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003695static char *TabPageAttrs[] = {
3696 "windows", "number", "vars", "window", "valid",
3697 NULL
3698};
3699
3700 static PyObject *
3701TabPageDir(PyObject *self)
3702{
3703 return ObjectDir(self, TabPageAttrs);
3704}
3705
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003706 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003707TabPageAttrValid(TabPageObject *self, char *name)
3708{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003709 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003710
3711 if (strcmp(name, "valid") != 0)
3712 return NULL;
3713
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003714 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3715 Py_INCREF(ret);
3716 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003717}
3718
3719 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003720TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003721{
3722 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003723 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003724 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003725 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003726 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003727 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003728 else if (strcmp(name, "window") == 0)
3729 {
3730 /* For current tab window.c does not bother to set or update tp_curwin
3731 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003732 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003733 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003734 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003735 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003736 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003737 else if (strcmp(name, "__members__") == 0)
3738 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003739 return NULL;
3740}
3741
3742 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003743TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003744{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003745 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003746 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003747 else
3748 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003749 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003750
3751 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003752 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3753 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003754 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003755 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003756 }
3757}
3758
3759static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003760 /* name, function, calling, documentation */
3761 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3762 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003763};
3764
3765/*
3766 * Window list object
3767 */
3768
3769static PyTypeObject TabListType;
3770static PySequenceMethods TabListAsSeq;
3771
3772typedef struct
3773{
3774 PyObject_HEAD
3775} TabListObject;
3776
3777 static PyInt
3778TabListLength(PyObject *self UNUSED)
3779{
3780 tabpage_T *tp = first_tabpage;
3781 PyInt n = 0;
3782
3783 while (tp != NULL)
3784 {
3785 ++n;
3786 tp = tp->tp_next;
3787 }
3788
3789 return n;
3790}
3791
3792 static PyObject *
3793TabListItem(PyObject *self UNUSED, PyInt n)
3794{
3795 tabpage_T *tp;
3796
3797 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3798 if (n == 0)
3799 return TabPageNew(tp);
3800
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003801 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003802 return NULL;
3803}
3804
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003805/*
3806 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003807 */
3808
3809typedef struct
3810{
3811 PyObject_HEAD
3812 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003813 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003814} WindowObject;
3815
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003816static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003817
3818 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003819CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003820{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003821 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003822 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003823 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003824 return -1;
3825 }
3826
3827 return 0;
3828}
3829
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003830 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003831WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003832{
3833 /* We need to handle deletion of windows underneath us.
3834 * If we add a "w_python*_ref" field to the win_T structure,
3835 * then we can get at it in win_free() in vim. We then
3836 * need to create only ONE Python object per window - if
3837 * we try to create a second, just INCREF the existing one
3838 * and return it. The (single) Python object referring to
3839 * the window is stored in "w_python*_ref".
3840 * On a win_free() we set the Python object's win_T* field
3841 * to an invalid value. We trap all uses of a window
3842 * object, and reject them if the win_T* field is invalid.
3843 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003844 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003845 * w_python_ref and w_python3_ref fields respectively.
3846 */
3847
3848 WindowObject *self;
3849
3850 if (WIN_PYTHON_REF(win))
3851 {
3852 self = WIN_PYTHON_REF(win);
3853 Py_INCREF(self);
3854 }
3855 else
3856 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003857 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003858 if (self == NULL)
3859 return NULL;
3860 self->win = win;
3861 WIN_PYTHON_REF(win) = self;
3862 }
3863
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003864 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3865
Bram Moolenaar971db462013-05-12 18:44:48 +02003866 return (PyObject *)(self);
3867}
3868
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003869 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003870WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003871{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003872 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003873 if (self->win && self->win != INVALID_WINDOW_VALUE)
3874 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003875 Py_XDECREF(((PyObject *)(self->tabObject)));
3876 PyObject_GC_Del((void *)(self));
3877}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003878
Bram Moolenaar774267b2013-05-21 20:51:59 +02003879 static int
3880WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3881{
3882 Py_VISIT(((PyObject *)(self->tabObject)));
3883 return 0;
3884}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003885
Bram Moolenaar774267b2013-05-21 20:51:59 +02003886 static int
3887WindowClear(WindowObject *self)
3888{
3889 Py_CLEAR(self->tabObject);
3890 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003891}
3892
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003893 static win_T *
3894get_firstwin(TabPageObject *tabObject)
3895{
3896 if (tabObject)
3897 {
3898 if (CheckTabPage(tabObject))
3899 return NULL;
3900 /* For current tab window.c does not bother to set or update tp_firstwin
3901 */
3902 else if (tabObject->tab == curtab)
3903 return firstwin;
3904 else
3905 return tabObject->tab->tp_firstwin;
3906 }
3907 else
3908 return firstwin;
3909}
Bram Moolenaare950f992018-06-10 13:55:55 +02003910
3911// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003912static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003913 "buffer",
3914 "cursor",
3915 "height",
3916 "row",
3917 "width",
3918 "col",
3919 "vars",
3920 "options",
3921 "number",
3922 "tabpage",
3923 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003924 NULL
3925};
3926
3927 static PyObject *
3928WindowDir(PyObject *self)
3929{
3930 return ObjectDir(self, WindowAttrs);
3931}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003932
Bram Moolenaar971db462013-05-12 18:44:48 +02003933 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003934WindowAttrValid(WindowObject *self, char *name)
3935{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003936 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003937
3938 if (strcmp(name, "valid") != 0)
3939 return NULL;
3940
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003941 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3942 Py_INCREF(ret);
3943 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003944}
3945
3946 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003947WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003948{
3949 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003950 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003951 else if (strcmp(name, "cursor") == 0)
3952 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003953 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003954
3955 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3956 }
3957 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003958 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003959 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003960 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003961 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003962 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003963 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003964 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003965 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003966 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003967 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003968 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3969 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003970 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003971 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003972 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003973 return NULL;
3974 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003975 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003976 }
3977 else if (strcmp(name, "tabpage") == 0)
3978 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003979 Py_INCREF(self->tabObject);
3980 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003981 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003982 else if (strcmp(name, "__members__") == 0)
3983 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003984 else
3985 return NULL;
3986}
3987
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003988 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003989WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003990{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003991 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003992 return -1;
3993
3994 if (strcmp(name, "buffer") == 0)
3995 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003996 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003997 return -1;
3998 }
3999 else if (strcmp(name, "cursor") == 0)
4000 {
4001 long lnum;
4002 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004003
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004004 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004005 return -1;
4006
Bram Moolenaard6e39182013-05-21 18:30:34 +02004007 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004008 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004009 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004010 return -1;
4011 }
4012
4013 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004014 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004015 return -1;
4016
Bram Moolenaard6e39182013-05-21 18:30:34 +02004017 self->win->w_cursor.lnum = lnum;
4018 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004019 self->win->w_set_curswant = TRUE;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004020#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02004021 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004022#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004023 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004024 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004025
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004026 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004027 return 0;
4028 }
4029 else if (strcmp(name, "height") == 0)
4030 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004031 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004032 win_T *savewin;
4033
Bram Moolenaardee2e312013-06-23 16:35:47 +02004034 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035 return -1;
4036
4037#ifdef FEAT_GUI
4038 need_mouse_correct = TRUE;
4039#endif
4040 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004041 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004042
4043 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004044 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004045 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004046 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004047 return -1;
4048
4049 return 0;
4050 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004051 else if (strcmp(name, "width") == 0)
4052 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004053 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 win_T *savewin;
4055
Bram Moolenaardee2e312013-06-23 16:35:47 +02004056 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004057 return -1;
4058
4059#ifdef FEAT_GUI
4060 need_mouse_correct = TRUE;
4061#endif
4062 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004063 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004064
4065 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004066 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004067 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004068 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069 return -1;
4070
4071 return 0;
4072 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004073 else
4074 {
4075 PyErr_SetString(PyExc_AttributeError, name);
4076 return -1;
4077 }
4078}
4079
4080 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004081WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004082{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004083 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004084 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085 else
4086 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004087 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088
Bram Moolenaar6d216452013-05-12 19:00:41 +02004089 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004090 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004091 (self));
4092 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004093 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004094 }
4095}
4096
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004097static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004098 /* name, function, calling, documentation */
4099 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4100 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004101};
4102
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004103/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004104 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004105 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004106
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004107static PyTypeObject WinListType;
4108static PySequenceMethods WinListAsSeq;
4109
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004110typedef struct
4111{
4112 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004113 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004114} WinListObject;
4115
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004116 static PyObject *
4117WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004118{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004119 WinListObject *self;
4120
4121 self = PyObject_NEW(WinListObject, &WinListType);
4122 self->tabObject = tabObject;
4123 Py_INCREF(tabObject);
4124
4125 return (PyObject *)(self);
4126}
4127
4128 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004129WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004130{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004131 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004132
4133 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004134 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004135 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004136 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004137
4138 DESTRUCTOR_FINISH(self);
4139}
4140
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004141 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004142WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004143{
4144 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004145 PyInt n = 0;
4146
Bram Moolenaard6e39182013-05-21 18:30:34 +02004147 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004148 return -1;
4149
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004150 while (w != NULL)
4151 {
4152 ++n;
4153 w = W_NEXT(w);
4154 }
4155
4156 return n;
4157}
4158
4159 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004160WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004161{
4162 win_T *w;
4163
Bram Moolenaard6e39182013-05-21 18:30:34 +02004164 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004165 return NULL;
4166
4167 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004168 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004169 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004170
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004171 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004172 return NULL;
4173}
4174
4175/* Convert a Python string into a Vim line.
4176 *
4177 * The result is in allocated memory. All internal nulls are replaced by
4178 * newline characters. It is an error for the string to contain newline
4179 * characters.
4180 *
4181 * On errors, the Python exception data is set, and NULL is returned.
4182 */
4183 static char *
4184StringToLine(PyObject *obj)
4185{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004186 char *str;
4187 char *save;
4188 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004189 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004190 PyInt i;
4191 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004192
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004193 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004194 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004195 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4196 || str == NULL)
4197 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004198 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004199 else if (PyUnicode_Check(obj))
4200 {
4201 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4202 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004203
Bram Moolenaardaa27022013-06-24 22:33:30 +02004204 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004205 || str == NULL)
4206 {
4207 Py_DECREF(bytes);
4208 return NULL;
4209 }
4210 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004211 else
4212 {
4213#if PY_MAJOR_VERSION < 3
4214 PyErr_FORMAT(PyExc_TypeError,
4215 N_("expected str() or unicode() instance, but got %s"),
4216 Py_TYPE_NAME(obj));
4217#else
4218 PyErr_FORMAT(PyExc_TypeError,
4219 N_("expected bytes() or str() instance, but got %s"),
4220 Py_TYPE_NAME(obj));
4221#endif
4222 return NULL;
4223 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004224
4225 /*
4226 * Error checking: String must not contain newlines, as we
4227 * are replacing a single line, and we must replace it with
4228 * a single line.
4229 * A trailing newline is removed, so that append(f.readlines()) works.
4230 */
4231 p = memchr(str, '\n', len);
4232 if (p != NULL)
4233 {
4234 if (p == str + len - 1)
4235 --len;
4236 else
4237 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004238 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004239 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004240 return NULL;
4241 }
4242 }
4243
4244 /* Create a copy of the string, with internal nulls replaced by
4245 * newline characters, as is the vim convention.
4246 */
4247 save = (char *)alloc((unsigned)(len+1));
4248 if (save == NULL)
4249 {
4250 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004251 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004252 return NULL;
4253 }
4254
4255 for (i = 0; i < len; ++i)
4256 {
4257 if (str[i] == '\0')
4258 save[i] = '\n';
4259 else
4260 save[i] = str[i];
4261 }
4262
4263 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004264 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004265
4266 return save;
4267}
4268
4269/* Get a line from the specified buffer. The line number is
4270 * in Vim format (1-based). The line is returned as a Python
4271 * string object.
4272 */
4273 static PyObject *
4274GetBufferLine(buf_T *buf, PyInt n)
4275{
4276 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4277}
4278
4279
4280/* Get a list of lines from the specified buffer. The line numbers
4281 * are in Vim format (1-based). The range is from lo up to, but not
4282 * including, hi. The list is returned as a Python list of string objects.
4283 */
4284 static PyObject *
4285GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4286{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004287 PyInt i;
4288 PyInt n = hi - lo;
4289 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004290
4291 if (list == NULL)
4292 return NULL;
4293
4294 for (i = 0; i < n; ++i)
4295 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004296 PyObject *string = LineToString(
4297 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004298
4299 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004300 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004301 {
4302 Py_DECREF(list);
4303 return NULL;
4304 }
4305
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004306 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004307 }
4308
4309 /* The ownership of the Python list is passed to the caller (ie,
4310 * the caller should Py_DECREF() the object when it is finished
4311 * with it).
4312 */
4313
4314 return list;
4315}
4316
4317/*
4318 * Check if deleting lines made the cursor position invalid.
4319 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4320 * deleted).
4321 */
4322 static void
4323py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4324{
4325 if (curwin->w_cursor.lnum >= lo)
4326 {
4327 /* Adjust the cursor position if it's in/after the changed
4328 * lines. */
4329 if (curwin->w_cursor.lnum >= hi)
4330 {
4331 curwin->w_cursor.lnum += extra;
4332 check_cursor_col();
4333 }
4334 else if (extra < 0)
4335 {
4336 curwin->w_cursor.lnum = lo;
4337 check_cursor();
4338 }
4339 else
4340 check_cursor_col();
4341 changed_cline_bef_curs();
4342 }
4343 invalidate_botline();
4344}
4345
Bram Moolenaar19e60942011-06-19 00:27:51 +02004346/*
4347 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004348 * in Vim format (1-based). The replacement line is given as
4349 * a Python string object. The object is checked for validity
4350 * and correct format. Errors are returned as a value of FAIL.
4351 * The return value is OK on success.
4352 * If OK is returned and len_change is not NULL, *len_change
4353 * is set to the change in the buffer length.
4354 */
4355 static int
4356SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4357{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004358 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004359 win_T *save_curwin = NULL;
4360 tabpage_T *save_curtab = NULL;
4361
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004362 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004363 * There are three cases:
4364 * 1. NULL, or None - this is a deletion.
4365 * 2. A string - this is a replacement.
4366 * 3. Anything else - this is an error.
4367 */
4368 if (line == Py_None || line == NULL)
4369 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004370 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004371 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004372
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004373 VimTryStart();
4374
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004375 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004376 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004377 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004378 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004379 else
4380 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004381 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004382 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004383 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004384 /* Only adjust marks if we managed to switch to a window that
4385 * holds the buffer, otherwise line numbers will be invalid. */
4386 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004387 }
4388
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004389 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004391 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004392 return FAIL;
4393
4394 if (len_change)
4395 *len_change = -1;
4396
4397 return OK;
4398 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004399 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004400 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004401 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004402
4403 if (save == NULL)
4404 return FAIL;
4405
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004406 VimTryStart();
4407
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004408 /* We do not need to free "save" if ml_replace() consumes it. */
4409 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004410 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004411
4412 if (u_savesub((linenr_T)n) == FAIL)
4413 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004414 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004415 vim_free(save);
4416 }
4417 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4418 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004419 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004420 vim_free(save);
4421 }
4422 else
4423 changed_bytes((linenr_T)n, 0);
4424
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004425 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004426
4427 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004428 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004429 check_cursor_col();
4430
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004431 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432 return FAIL;
4433
4434 if (len_change)
4435 *len_change = 0;
4436
4437 return OK;
4438 }
4439 else
4440 {
4441 PyErr_BadArgument();
4442 return FAIL;
4443 }
4444}
4445
Bram Moolenaar19e60942011-06-19 00:27:51 +02004446/* Replace a range of lines in the specified buffer. The line numbers are in
4447 * Vim format (1-based). The range is from lo up to, but not including, hi.
4448 * The replacement lines are given as a Python list of string objects. The
4449 * list is checked for validity and correct format. Errors are returned as a
4450 * value of FAIL. The return value is OK on success.
4451 * If OK is returned and len_change is not NULL, *len_change
4452 * is set to the change in the buffer length.
4453 */
4454 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004455SetBufferLineList(
4456 buf_T *buf,
4457 PyInt lo,
4458 PyInt hi,
4459 PyObject *list,
4460 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004461{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004462 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004463 win_T *save_curwin = NULL;
4464 tabpage_T *save_curtab = NULL;
4465
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004466 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004467 * There are three cases:
4468 * 1. NULL, or None - this is a deletion.
4469 * 2. A list - this is a replacement.
4470 * 3. Anything else - this is an error.
4471 */
4472 if (list == Py_None || list == NULL)
4473 {
4474 PyInt i;
4475 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004476
4477 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004478 VimTryStart();
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_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004482 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004483 else
4484 {
4485 for (i = 0; i < n; ++i)
4486 {
4487 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4488 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004489 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004490 break;
4491 }
4492 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004493 if (buf == curbuf && (save_curwin != NULL
4494 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004495 /* Using an existing window for the buffer, adjust the cursor
4496 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004497 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004498 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004499 /* Only adjust marks if we managed to switch to a window that
4500 * holds the buffer, otherwise line numbers will be invalid. */
4501 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004502 }
4503
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004504 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004505
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004506 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004507 return FAIL;
4508
4509 if (len_change)
4510 *len_change = -n;
4511
4512 return OK;
4513 }
4514 else if (PyList_Check(list))
4515 {
4516 PyInt i;
4517 PyInt new_len = PyList_Size(list);
4518 PyInt old_len = hi - lo;
4519 PyInt extra = 0; /* lines added to text, can be negative */
4520 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004521
4522 if (new_len == 0) /* avoid allocating zero bytes */
4523 array = NULL;
4524 else
4525 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004526 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004527 if (array == NULL)
4528 {
4529 PyErr_NoMemory();
4530 return FAIL;
4531 }
4532 }
4533
4534 for (i = 0; i < new_len; ++i)
4535 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004536 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004537
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004538 if (!(line = PyList_GetItem(list, i)) ||
4539 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004540 {
4541 while (i)
4542 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004543 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004544 return FAIL;
4545 }
4546 }
4547
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004548 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004549 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004550
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004551 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004552 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004553
4554 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004555 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004556
4557 /* If the size of the range is reducing (ie, new_len < old_len) we
4558 * need to delete some old_len. We do this at the start, by
4559 * repeatedly deleting line "lo".
4560 */
4561 if (!PyErr_Occurred())
4562 {
4563 for (i = 0; i < old_len - new_len; ++i)
4564 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4565 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004566 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004567 break;
4568 }
4569 extra -= i;
4570 }
4571
4572 /* For as long as possible, replace the existing old_len with the
4573 * new old_len. This is a more efficient operation, as it requires
4574 * less memory allocation and freeing.
4575 */
4576 if (!PyErr_Occurred())
4577 {
4578 for (i = 0; i < old_len && i < new_len; ++i)
4579 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4580 == FAIL)
4581 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004582 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004583 break;
4584 }
4585 }
4586 else
4587 i = 0;
4588
4589 /* Now we may need to insert the remaining new old_len. If we do, we
4590 * must free the strings as we finish with them (we can't pass the
4591 * responsibility to vim in this case).
4592 */
4593 if (!PyErr_Occurred())
4594 {
4595 while (i < new_len)
4596 {
4597 if (ml_append((linenr_T)(lo + i - 1),
4598 (char_u *)array[i], 0, FALSE) == FAIL)
4599 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004600 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004601 break;
4602 }
4603 vim_free(array[i]);
4604 ++i;
4605 ++extra;
4606 }
4607 }
4608
4609 /* Free any left-over old_len, as a result of an error */
4610 while (i < new_len)
4611 {
4612 vim_free(array[i]);
4613 ++i;
4614 }
4615
4616 /* Free the array of old_len. All of its contents have now
4617 * been dealt with (either freed, or the responsibility passed
4618 * to vim.
4619 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004620 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004621
4622 /* Adjust marks. Invalidate any which lie in the
4623 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004624 * Only adjust marks if we managed to switch to a window that holds
4625 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004626 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004627 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004628 (long)MAXLNUM, (long)extra);
4629 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4630
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004631 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004632 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4633
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004634 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004635 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004636
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004637 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004638 return FAIL;
4639
4640 if (len_change)
4641 *len_change = new_len - old_len;
4642
4643 return OK;
4644 }
4645 else
4646 {
4647 PyErr_BadArgument();
4648 return FAIL;
4649 }
4650}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004651
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004652/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004653 * The line number is in Vim format (1-based). The lines to be inserted are
4654 * given as a Python list of string objects or as a single string. The lines
4655 * to be added are checked for validity and correct format. Errors are
4656 * returned as a value of FAIL. The return value is OK on success.
4657 * If OK is returned and len_change is not NULL, *len_change
4658 * is set to the change in the buffer length.
4659 */
4660 static int
4661InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4662{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004663 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004664 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004665 tabpage_T *save_curtab = NULL;
4666
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004667 /* First of all, we check the type of the supplied Python object.
4668 * It must be a string or a list, or the call is in error.
4669 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004670 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004671 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004672 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004673
4674 if (str == NULL)
4675 return FAIL;
4676
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004677 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004678 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004679 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004680
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004681 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004682 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004683 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004684 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004685 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004686 /* Only adjust marks if we managed to switch to a window that
4687 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004688 appended_lines_mark((linenr_T)n, 1L);
4689
4690 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004691 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004692 update_screen(VALID);
4693
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004694 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004695 return FAIL;
4696
4697 if (len_change)
4698 *len_change = 1;
4699
4700 return OK;
4701 }
4702 else if (PyList_Check(lines))
4703 {
4704 PyInt i;
4705 PyInt size = PyList_Size(lines);
4706 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004707
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004708 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004709 if (array == NULL)
4710 {
4711 PyErr_NoMemory();
4712 return FAIL;
4713 }
4714
4715 for (i = 0; i < size; ++i)
4716 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004717 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004718
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004719 if (!(line = PyList_GetItem(lines, i)) ||
4720 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004721 {
4722 while (i)
4723 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004724 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004725 return FAIL;
4726 }
4727 }
4728
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004729 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004730 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004731 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004732
4733 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004734 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004735 else
4736 {
4737 for (i = 0; i < size; ++i)
4738 {
4739 if (ml_append((linenr_T)(n + i),
4740 (char_u *)array[i], 0, FALSE) == FAIL)
4741 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004742 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004743
4744 /* Free the rest of the lines */
4745 while (i < size)
4746 vim_free(array[i++]);
4747
4748 break;
4749 }
4750 vim_free(array[i]);
4751 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004752 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004753 /* Only adjust marks if we managed to switch to a window that
4754 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004755 appended_lines_mark((linenr_T)n, (long)i);
4756 }
4757
4758 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004759 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004760 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004761 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004762
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004763 update_screen(VALID);
4764
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004765 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004766 return FAIL;
4767
4768 if (len_change)
4769 *len_change = size;
4770
4771 return OK;
4772 }
4773 else
4774 {
4775 PyErr_BadArgument();
4776 return FAIL;
4777 }
4778}
4779
4780/*
4781 * Common routines for buffers and line ranges
4782 * -------------------------------------------
4783 */
4784
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004785typedef struct
4786{
4787 PyObject_HEAD
4788 buf_T *buf;
4789} BufferObject;
4790
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004791 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004792CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004793{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004794 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004795 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004796 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004797 return -1;
4798 }
4799
4800 return 0;
4801}
4802
4803 static PyObject *
4804RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4805{
4806 if (CheckBuffer(self))
4807 return NULL;
4808
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004809 if (end == -1)
4810 end = self->buf->b_ml.ml_line_count;
4811
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004812 if (n < 0)
4813 n += end - start + 1;
4814
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004815 if (n < 0 || n > end - start)
4816 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004817 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004818 return NULL;
4819 }
4820
4821 return GetBufferLine(self->buf, n+start);
4822}
4823
4824 static PyObject *
4825RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4826{
4827 PyInt size;
4828
4829 if (CheckBuffer(self))
4830 return NULL;
4831
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004832 if (end == -1)
4833 end = self->buf->b_ml.ml_line_count;
4834
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004835 size = end - start + 1;
4836
4837 if (lo < 0)
4838 lo = 0;
4839 else if (lo > size)
4840 lo = size;
4841 if (hi < 0)
4842 hi = 0;
4843 if (hi < lo)
4844 hi = lo;
4845 else if (hi > size)
4846 hi = size;
4847
4848 return GetBufferLineList(self->buf, lo+start, hi+start);
4849}
4850
4851 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004852RBAsItem(
4853 BufferObject *self,
4854 PyInt n,
4855 PyObject *valObject,
4856 PyInt start,
4857 PyInt end,
4858 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004859{
4860 PyInt len_change;
4861
4862 if (CheckBuffer(self))
4863 return -1;
4864
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004865 if (end == -1)
4866 end = self->buf->b_ml.ml_line_count;
4867
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004868 if (n < 0)
4869 n += end - start + 1;
4870
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004871 if (n < 0 || n > end - start)
4872 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004873 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004874 return -1;
4875 }
4876
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004877 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004878 return -1;
4879
4880 if (new_end)
4881 *new_end = end + len_change;
4882
4883 return 0;
4884}
4885
Bram Moolenaar19e60942011-06-19 00:27:51 +02004886 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004887RBAsSlice(
4888 BufferObject *self,
4889 PyInt lo,
4890 PyInt hi,
4891 PyObject *valObject,
4892 PyInt start,
4893 PyInt end,
4894 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004895{
4896 PyInt size;
4897 PyInt len_change;
4898
4899 /* Self must be a valid buffer */
4900 if (CheckBuffer(self))
4901 return -1;
4902
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004903 if (end == -1)
4904 end = self->buf->b_ml.ml_line_count;
4905
Bram Moolenaar19e60942011-06-19 00:27:51 +02004906 /* Sort out the slice range */
4907 size = end - start + 1;
4908
4909 if (lo < 0)
4910 lo = 0;
4911 else if (lo > size)
4912 lo = size;
4913 if (hi < 0)
4914 hi = 0;
4915 if (hi < lo)
4916 hi = lo;
4917 else if (hi > size)
4918 hi = size;
4919
4920 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004921 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004922 return -1;
4923
4924 if (new_end)
4925 *new_end = end + len_change;
4926
4927 return 0;
4928}
4929
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004930
4931 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004932RBAppend(
4933 BufferObject *self,
4934 PyObject *args,
4935 PyInt start,
4936 PyInt end,
4937 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004938{
4939 PyObject *lines;
4940 PyInt len_change;
4941 PyInt max;
4942 PyInt n;
4943
4944 if (CheckBuffer(self))
4945 return NULL;
4946
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004947 if (end == -1)
4948 end = self->buf->b_ml.ml_line_count;
4949
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004950 max = n = end - start + 1;
4951
4952 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4953 return NULL;
4954
4955 if (n < 0 || n > max)
4956 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004957 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004958 return NULL;
4959 }
4960
4961 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4962 return NULL;
4963
4964 if (new_end)
4965 *new_end = end + len_change;
4966
4967 Py_INCREF(Py_None);
4968 return Py_None;
4969}
4970
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004971/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004972 */
4973
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004974static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004975static PySequenceMethods RangeAsSeq;
4976static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004977
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004978typedef struct
4979{
4980 PyObject_HEAD
4981 BufferObject *buf;
4982 PyInt start;
4983 PyInt end;
4984} RangeObject;
4985
4986 static PyObject *
4987RangeNew(buf_T *buf, PyInt start, PyInt end)
4988{
4989 BufferObject *bufr;
4990 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004991 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004992 if (self == NULL)
4993 return NULL;
4994
4995 bufr = (BufferObject *)BufferNew(buf);
4996 if (bufr == NULL)
4997 {
4998 Py_DECREF(self);
4999 return NULL;
5000 }
5001 Py_INCREF(bufr);
5002
5003 self->buf = bufr;
5004 self->start = start;
5005 self->end = end;
5006
5007 return (PyObject *)(self);
5008}
5009
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005010 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005011RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005012{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005013 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005014 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005015 PyObject_GC_Del((void *)(self));
5016}
5017
5018 static int
5019RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5020{
5021 Py_VISIT(((PyObject *)(self->buf)));
5022 return 0;
5023}
5024
5025 static int
5026RangeClear(RangeObject *self)
5027{
5028 Py_CLEAR(self->buf);
5029 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005030}
5031
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005032 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005033RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005034{
5035 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005036 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005037 return -1; /* ??? */
5038
Bram Moolenaard6e39182013-05-21 18:30:34 +02005039 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005040}
5041
5042 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005043RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005044{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005045 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005046}
5047
5048 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005049RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005050{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005051 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005052}
5053
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005054static char *RangeAttrs[] = {
5055 "start", "end",
5056 NULL
5057};
5058
5059 static PyObject *
5060RangeDir(PyObject *self)
5061{
5062 return ObjectDir(self, RangeAttrs);
5063}
5064
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005065 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005066RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005067{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005068 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005069}
5070
5071 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005072RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005073{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005074 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005075 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5076 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005077 else
5078 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005079 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005080
5081 if (name == NULL)
5082 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005083
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005084 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005085 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005086 }
5087}
5088
5089static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005090 /* name, function, calling, documentation */
5091 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005092 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5093 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005094};
5095
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005096static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005097static PySequenceMethods BufferAsSeq;
5098static PyMappingMethods BufferAsMapping;
5099
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005100 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005101BufferNew(buf_T *buf)
5102{
5103 /* We need to handle deletion of buffers underneath us.
5104 * If we add a "b_python*_ref" field to the buf_T structure,
5105 * then we can get at it in buf_freeall() in vim. We then
5106 * need to create only ONE Python object per buffer - if
5107 * we try to create a second, just INCREF the existing one
5108 * and return it. The (single) Python object referring to
5109 * the buffer is stored in "b_python*_ref".
5110 * Question: what to do on a buf_freeall(). We'll probably
5111 * have to either delete the Python object (DECREF it to
5112 * zero - a bad idea, as it leaves dangling refs!) or
5113 * set the buf_T * value to an invalid value (-1?), which
5114 * means we need checks in all access functions... Bah.
5115 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005116 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005117 * b_python_ref and b_python3_ref fields respectively.
5118 */
5119
5120 BufferObject *self;
5121
5122 if (BUF_PYTHON_REF(buf) != NULL)
5123 {
5124 self = BUF_PYTHON_REF(buf);
5125 Py_INCREF(self);
5126 }
5127 else
5128 {
5129 self = PyObject_NEW(BufferObject, &BufferType);
5130 if (self == NULL)
5131 return NULL;
5132 self->buf = buf;
5133 BUF_PYTHON_REF(buf) = self;
5134 }
5135
5136 return (PyObject *)(self);
5137}
5138
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005139 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005140BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005141{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005142 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5143 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005144
5145 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005146}
5147
Bram Moolenaar971db462013-05-12 18:44:48 +02005148 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005149BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005150{
5151 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005152 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005153 return -1; /* ??? */
5154
Bram Moolenaard6e39182013-05-21 18:30:34 +02005155 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005156}
5157
5158 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005159BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005160{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005161 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005162}
5163
5164 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005165BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005166{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005167 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005168}
5169
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005170static char *BufferAttrs[] = {
5171 "name", "number", "vars", "options", "valid",
5172 NULL
5173};
5174
5175 static PyObject *
5176BufferDir(PyObject *self)
5177{
5178 return ObjectDir(self, BufferAttrs);
5179}
5180
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005181 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005182BufferAttrValid(BufferObject *self, char *name)
5183{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005184 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005185
5186 if (strcmp(name, "valid") != 0)
5187 return NULL;
5188
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005189 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5190 Py_INCREF(ret);
5191 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005192}
5193
5194 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005195BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005196{
5197 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005198 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005199 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005200 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005201 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005202 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005203 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005204 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005205 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5206 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005207 else if (strcmp(name, "__members__") == 0)
5208 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005209 else
5210 return NULL;
5211}
5212
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005213 static int
5214BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5215{
5216 if (CheckBuffer(self))
5217 return -1;
5218
5219 if (strcmp(name, "name") == 0)
5220 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005221 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005222 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005223 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005224 PyObject *todecref;
5225
5226 if (!(val = StringToChars(valObject, &todecref)))
5227 return -1;
5228
5229 VimTryStart();
5230 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5231 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005232 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005233 aucmd_restbuf(&aco);
5234 Py_XDECREF(todecref);
5235 if (VimTryEnd())
5236 return -1;
5237
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005238 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005239 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005240 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005241 return -1;
5242 }
5243 return 0;
5244 }
5245 else
5246 {
5247 PyErr_SetString(PyExc_AttributeError, name);
5248 return -1;
5249 }
5250}
5251
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005252 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005253BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005254{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005255 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005256}
5257
5258 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005259BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005260{
5261 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005262 char_u *pmark;
5263 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005264 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005265 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005266
Bram Moolenaard6e39182013-05-21 18:30:34 +02005267 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005268 return NULL;
5269
Bram Moolenaar389a1792013-06-23 13:00:44 +02005270 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005271 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005272
Bram Moolenaar389a1792013-06-23 13:00:44 +02005273 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005274 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005275 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005276 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005277 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005278 return NULL;
5279 }
5280
5281 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005282
5283 Py_XDECREF(todecref);
5284
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005285 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005286 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005287 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005288 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005289 if (VimTryEnd())
5290 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005291
5292 if (posp == NULL)
5293 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005294 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005295 return NULL;
5296 }
5297
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005298 if (posp->lnum <= 0)
5299 {
5300 /* Or raise an error? */
5301 Py_INCREF(Py_None);
5302 return Py_None;
5303 }
5304
5305 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5306}
5307
5308 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005309BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005310{
5311 PyInt start;
5312 PyInt end;
5313
Bram Moolenaard6e39182013-05-21 18:30:34 +02005314 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315 return NULL;
5316
5317 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5318 return NULL;
5319
Bram Moolenaard6e39182013-05-21 18:30:34 +02005320 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005321}
5322
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005323 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005324BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005325{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005326 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005327 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005328 else
5329 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005330 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005331
5332 if (name == NULL)
5333 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005334
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005335 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005336 }
5337}
5338
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005339static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005340 /* name, function, calling, documentation */
5341 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005342 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005343 {"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 +02005344 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5345 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005346};
5347
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005348/*
5349 * Buffer list object - Implementation
5350 */
5351
5352static PyTypeObject BufMapType;
5353
5354typedef struct
5355{
5356 PyObject_HEAD
5357} BufMapObject;
5358
5359 static PyInt
5360BufMapLength(PyObject *self UNUSED)
5361{
5362 buf_T *b = firstbuf;
5363 PyInt n = 0;
5364
5365 while (b)
5366 {
5367 ++n;
5368 b = b->b_next;
5369 }
5370
5371 return n;
5372}
5373
5374 static PyObject *
5375BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5376{
5377 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005378 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005379
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005380 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005381 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005382
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005383 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005384
5385 if (b)
5386 return BufferNew(b);
5387 else
5388 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005389 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005390 return NULL;
5391 }
5392}
5393
5394 static void
5395BufMapIterDestruct(PyObject *buffer)
5396{
5397 /* Iteration was stopped before all buffers were processed */
5398 if (buffer)
5399 {
5400 Py_DECREF(buffer);
5401 }
5402}
5403
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005404 static int
5405BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5406{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005407 if (buffer)
5408 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005409 return 0;
5410}
5411
5412 static int
5413BufMapIterClear(PyObject **buffer)
5414{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005415 if (*buffer)
5416 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005417 return 0;
5418}
5419
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005420 static PyObject *
5421BufMapIterNext(PyObject **buffer)
5422{
5423 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005424 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005425
5426 if (!*buffer)
5427 return NULL;
5428
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005429 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005430
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005431 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005432 {
5433 *buffer = NULL;
5434 return NULL;
5435 }
5436
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005437 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005438 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005439 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005440 return NULL;
5441 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005442 /* Do not increment reference: we no longer hold it (decref), but whoever
5443 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005444 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005445}
5446
5447 static PyObject *
5448BufMapIter(PyObject *self UNUSED)
5449{
5450 PyObject *buffer;
5451
5452 buffer = BufferNew(firstbuf);
5453 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005454 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5455 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005456}
5457
5458static PyMappingMethods BufMapAsMapping = {
5459 (lenfunc) BufMapLength,
5460 (binaryfunc) BufMapItem,
5461 (objobjargproc) 0,
5462};
5463
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005464/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005465 */
5466
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005467static char *CurrentAttrs[] = {
5468 "buffer", "window", "line", "range", "tabpage",
5469 NULL
5470};
5471
5472 static PyObject *
5473CurrentDir(PyObject *self)
5474{
5475 return ObjectDir(self, CurrentAttrs);
5476}
5477
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005478 static PyObject *
5479CurrentGetattr(PyObject *self UNUSED, char *name)
5480{
5481 if (strcmp(name, "buffer") == 0)
5482 return (PyObject *)BufferNew(curbuf);
5483 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005484 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005485 else if (strcmp(name, "tabpage") == 0)
5486 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005487 else if (strcmp(name, "line") == 0)
5488 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5489 else if (strcmp(name, "range") == 0)
5490 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005491 else if (strcmp(name, "__members__") == 0)
5492 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005493 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005494#if PY_MAJOR_VERSION < 3
5495 return Py_FindMethod(WindowMethods, self, name);
5496#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005497 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005498#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005499}
5500
5501 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005502CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005503{
5504 if (strcmp(name, "line") == 0)
5505 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005506 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5507 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005508 return -1;
5509
5510 return 0;
5511 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005512 else if (strcmp(name, "buffer") == 0)
5513 {
5514 int count;
5515
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005516 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005517 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005518 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005519 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005520 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005521 return -1;
5522 }
5523
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005524 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005525 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005526 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005527
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005528 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005529 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5530 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005531 if (VimTryEnd())
5532 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005533 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005534 return -1;
5535 }
5536
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005537 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005538 }
5539 else if (strcmp(name, "window") == 0)
5540 {
5541 int count;
5542
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005543 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005544 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005545 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005546 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005547 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005548 return -1;
5549 }
5550
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005551 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005552 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005553 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005554
5555 if (!count)
5556 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005557 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005558 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005559 return -1;
5560 }
5561
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005562 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005563 win_goto(((WindowObject *)(valObject))->win);
5564 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005565 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005566 if (VimTryEnd())
5567 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005568 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005569 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005570 return -1;
5571 }
5572
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005573 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005574 }
5575 else if (strcmp(name, "tabpage") == 0)
5576 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005577 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005578 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005579 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005580 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005581 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005582 return -1;
5583 }
5584
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005585 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005586 return -1;
5587
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005588 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005589 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5590 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005591 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005592 if (VimTryEnd())
5593 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005594 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005595 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005596 return -1;
5597 }
5598
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005599 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005600 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005601 else
5602 {
5603 PyErr_SetString(PyExc_AttributeError, name);
5604 return -1;
5605 }
5606}
5607
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005608static struct PyMethodDef CurrentMethods[] = {
5609 /* name, function, calling, documentation */
5610 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5611 { NULL, NULL, 0, NULL}
5612};
5613
Bram Moolenaardb913952012-06-29 12:54:53 +02005614 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005615init_range_cmd(exarg_T *eap)
5616{
5617 RangeStart = eap->line1;
5618 RangeEnd = eap->line2;
5619}
5620
5621 static void
5622init_range_eval(typval_T *rettv UNUSED)
5623{
5624 RangeStart = (PyInt) curwin->w_cursor.lnum;
5625 RangeEnd = RangeStart;
5626}
5627
5628 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005629run_cmd(const char *cmd, void *arg UNUSED
5630#ifdef PY_CAN_RECURSE
5631 , PyGILState_STATE *pygilstate UNUSED
5632#endif
5633 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005634{
Bram Moolenaar41009372013-07-01 22:03:04 +02005635 PyObject *run_ret;
5636 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5637 if (run_ret != NULL)
5638 {
5639 Py_DECREF(run_ret);
5640 }
5641 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5642 {
5643 EMSG2(_(e_py_systemexit), "python");
5644 PyErr_Clear();
5645 }
5646 else
5647 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005648}
5649
5650static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5651static int code_hdr_len = 30;
5652
5653 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005654run_do(const char *cmd, void *arg UNUSED
5655#ifdef PY_CAN_RECURSE
5656 , PyGILState_STATE *pygilstate
5657#endif
5658 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005659{
5660 PyInt lnum;
5661 size_t len;
5662 char *code;
5663 int status;
5664 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005665 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005666 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005667
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005668 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005669 {
5670 EMSG(_("cannot save undo information"));
5671 return;
5672 }
5673
5674 len = code_hdr_len + STRLEN(cmd);
5675 code = PyMem_New(char, len + 1);
5676 memcpy(code, code_hdr, code_hdr_len);
5677 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005678 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5679 status = -1;
5680 if (run_ret != NULL)
5681 {
5682 status = 0;
5683 Py_DECREF(run_ret);
5684 }
5685 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5686 {
5687 PyMem_Free(code);
5688 EMSG2(_(e_py_systemexit), "python");
5689 PyErr_Clear();
5690 return;
5691 }
5692 else
5693 PyErr_PrintEx(1);
5694
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005695 PyMem_Free(code);
5696
5697 if (status)
5698 {
5699 EMSG(_("failed to run the code"));
5700 return;
5701 }
5702
5703 status = 0;
5704 pymain = PyImport_AddModule("__main__");
5705 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005706#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005707 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005708#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005709
5710 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5711 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005712 PyObject *line;
5713 PyObject *linenr;
5714 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005715
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005716#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005717 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005718#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005719 /* Check the line number, the command my have deleted lines. */
5720 if (lnum > curbuf->b_ml.ml_line_count
5721 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005722 goto err;
5723 if (!(linenr = PyInt_FromLong((long) lnum)))
5724 {
5725 Py_DECREF(line);
5726 goto err;
5727 }
5728 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5729 Py_DECREF(line);
5730 Py_DECREF(linenr);
5731 if (!ret)
5732 goto err;
5733
Bram Moolenaara58883b2017-01-29 21:31:09 +01005734 /* Check that the command didn't switch to another buffer. */
5735 if (curbuf != was_curbuf)
5736 {
5737 Py_XDECREF(ret);
5738 goto err;
5739 }
5740
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005741 if (ret != Py_None)
5742 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005743 {
5744 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005745 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005746 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005747
5748 Py_XDECREF(ret);
5749 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005750#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005751 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005752#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005753 }
5754 goto out;
5755err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005756#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005757 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005758#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005759 PyErr_PrintEx(0);
5760 PythonIO_Flush();
5761 status = 1;
5762out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005763#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005764 if (!status)
5765 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005766#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005767 Py_DECREF(pyfunc);
5768 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5769 if (status)
5770 return;
5771 check_cursor();
5772 update_curbuf(NOT_VALID);
5773}
5774
5775 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005776run_eval(const char *cmd, typval_T *rettv
5777#ifdef PY_CAN_RECURSE
5778 , PyGILState_STATE *pygilstate UNUSED
5779#endif
5780 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005781{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005782 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005783
Bram Moolenaar41009372013-07-01 22:03:04 +02005784 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005785 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005786 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005787 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005788 {
5789 EMSG2(_(e_py_systemexit), "python");
5790 PyErr_Clear();
5791 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005792 else
5793 {
5794 if (PyErr_Occurred() && !msg_silent)
5795 PyErr_PrintEx(0);
5796 EMSG(_("E858: Eval did not return a valid python object"));
5797 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005798 }
5799 else
5800 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005801 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005802 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005803 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005804 }
5805 PyErr_Clear();
5806}
5807
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005808 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005809set_ref_in_py(const int copyID)
5810{
5811 pylinkedlist_T *cur;
5812 dict_T *dd;
5813 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005814 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005815 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005816 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005817
5818 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005819 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005820 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005821 {
5822 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5823 if (dd->dv_copyID != copyID)
5824 {
5825 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005826 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005827 }
5828 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005829 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005830
5831 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005832 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005833 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005834 {
5835 ll = ((ListObject *) (cur->pll_obj))->list;
5836 if (ll->lv_copyID != copyID)
5837 {
5838 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005839 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005840 }
5841 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005842 }
5843
Bram Moolenaar8110a092016-04-14 15:56:09 +02005844 if (lastfunc != NULL)
5845 {
5846 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5847 {
5848 func = (FunctionObject *) cur->pll_obj;
5849 if (func->self != NULL && func->self->dv_copyID != copyID)
5850 {
5851 func->self->dv_copyID = copyID;
5852 abort = abort || set_ref_in_ht(
5853 &func->self->dv_hashtab, copyID, NULL);
5854 }
5855 if (func->argc)
5856 for (i = 0; !abort && i < func->argc; ++i)
5857 abort = abort
5858 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5859 }
5860 }
5861
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005862 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005863}
5864
5865 static int
5866set_string_copy(char_u *str, typval_T *tv)
5867{
5868 tv->vval.v_string = vim_strsave(str);
5869 if (tv->vval.v_string == NULL)
5870 {
5871 PyErr_NoMemory();
5872 return -1;
5873 }
5874 return 0;
5875}
5876
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005877 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005878pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005879{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005880 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005881 char_u *key;
5882 dictitem_T *di;
5883 PyObject *keyObject;
5884 PyObject *valObject;
5885 Py_ssize_t iter = 0;
5886
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005887 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005888 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005889
5890 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005891 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005892
5893 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5894 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005895 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005896
Bram Moolenaara03e6312013-05-29 22:49:26 +02005897 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005898 {
5899 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005900 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005901 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005902
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005903 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005904 {
5905 dict_unref(dict);
5906 return -1;
5907 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005908
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005909 if (*key == NUL)
5910 {
5911 dict_unref(dict);
5912 Py_XDECREF(todecref);
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
5917 di = dictitem_alloc(key);
5918
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005919 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005920
5921 if (di == NULL)
5922 {
5923 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005924 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005925 return -1;
5926 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005927
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005928 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005929 {
5930 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005931 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005932 return -1;
5933 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005934
5935 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005936 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005937 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005938 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005939 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005940 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005941 return -1;
5942 }
5943 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005944
5945 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005946 return 0;
5947}
5948
5949 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005950pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005951{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005952 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005953 char_u *key;
5954 dictitem_T *di;
5955 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005956 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005957 PyObject *keyObject;
5958 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005959
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005960 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005961 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005962
5963 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005964 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005965
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005966 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005967 {
5968 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005969 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005970 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005971
5972 if (!(iterator = PyObject_GetIter(list)))
5973 {
5974 dict_unref(dict);
5975 Py_DECREF(list);
5976 return -1;
5977 }
5978 Py_DECREF(list);
5979
5980 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005981 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005982 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005983
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005984 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005985 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005986 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005987 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005988 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005989 return -1;
5990 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005991
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005992 if (*key == NUL)
5993 {
5994 Py_DECREF(keyObject);
5995 Py_DECREF(iterator);
5996 Py_XDECREF(todecref);
5997 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005998 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005999 return -1;
6000 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006001
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006002 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006003 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006004 Py_DECREF(keyObject);
6005 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006006 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006007 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006008 return -1;
6009 }
6010
6011 di = dictitem_alloc(key);
6012
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006013 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006014 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006015
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006016 if (di == NULL)
6017 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006018 Py_DECREF(iterator);
6019 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006020 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006021 PyErr_NoMemory();
6022 return -1;
6023 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006024
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006025 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006026 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006027 Py_DECREF(iterator);
6028 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006029 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006030 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006031 return -1;
6032 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006033
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006034 Py_DECREF(valObject);
6035
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006036 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006037 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006038 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006039 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006040 dictitem_free(di);
6041 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006042 return -1;
6043 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006044 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006045 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006046 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006047 return 0;
6048}
6049
6050 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006051pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006052{
6053 list_T *l;
6054
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006055 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006056 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006057
6058 tv->v_type = VAR_LIST;
6059 tv->vval.v_list = l;
6060
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006061 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006062 {
6063 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006064 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006065 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006066
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006067 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006068 return 0;
6069}
6070
Bram Moolenaardb913952012-06-29 12:54:53 +02006071typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6072
6073 static int
6074convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006075 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006076{
6077 PyObject *capsule;
6078 char hexBuf[sizeof(void *) * 2 + 3];
6079
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006080 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006081
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006082# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006083 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006084# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006085 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006086# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006087 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006088 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006089# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006090 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006091# else
6092 capsule = PyCObject_FromVoidPtr(tv, NULL);
6093# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006094 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6095 {
6096 Py_DECREF(capsule);
6097 tv->v_type = VAR_UNKNOWN;
6098 return -1;
6099 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006100
6101 Py_DECREF(capsule);
6102
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006103 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006104 {
6105 tv->v_type = VAR_UNKNOWN;
6106 return -1;
6107 }
6108 /* As we are not using copy_tv which increments reference count we must
6109 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006110 if (tv->v_type == VAR_DICT)
6111 ++tv->vval.v_dict->dv_refcount;
6112 else if (tv->v_type == VAR_LIST)
6113 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006114 }
6115 else
6116 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006117 typval_T *v;
6118
6119# ifdef PY_USE_CAPSULE
6120 v = PyCapsule_GetPointer(capsule, NULL);
6121# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006122 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006123# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006124 copy_tv(v, tv);
6125 }
6126 return 0;
6127}
6128
6129 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006130ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6131{
6132 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006133 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006134
6135 if (!(lookup_dict = PyDict_New()))
6136 return -1;
6137
6138 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6139 {
6140 tv->v_type = VAR_DICT;
6141 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6142 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006143 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006144 }
6145 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006146 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006147 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006148 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006149 else
6150 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006151 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006152 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006153 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006154 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006155 }
6156 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006157 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006158}
6159
6160 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006161ConvertFromPySequence(PyObject *obj, typval_T *tv)
6162{
6163 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006164 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006165
6166 if (!(lookup_dict = PyDict_New()))
6167 return -1;
6168
6169 if (PyType_IsSubtype(obj->ob_type, &ListType))
6170 {
6171 tv->v_type = VAR_LIST;
6172 tv->vval.v_list = (((ListObject *)(obj))->list);
6173 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006174 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006175 }
6176 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006177 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006178 else
6179 {
6180 PyErr_FORMAT(PyExc_TypeError,
6181 N_("unable to convert %s to vim list"),
6182 Py_TYPE_NAME(obj));
6183 ret = -1;
6184 }
6185 Py_DECREF(lookup_dict);
6186 return ret;
6187}
6188
6189 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006190ConvertFromPyObject(PyObject *obj, typval_T *tv)
6191{
6192 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006193 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006194
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006195 if (!(lookup_dict = PyDict_New()))
6196 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006197 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006198 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006199 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006200}
6201
6202 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006203_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006204{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006205 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006206 {
6207 tv->v_type = VAR_DICT;
6208 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6209 ++tv->vval.v_dict->dv_refcount;
6210 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006211 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006212 {
6213 tv->v_type = VAR_LIST;
6214 tv->vval.v_list = (((ListObject *)(obj))->list);
6215 ++tv->vval.v_list->lv_refcount;
6216 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006217 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006218 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006219 FunctionObject *func = (FunctionObject *) obj;
6220 if (func->self != NULL || func->argv != NULL)
6221 {
6222 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6223 set_partial(func, pt, TRUE);
6224 tv->vval.v_partial = pt;
6225 tv->v_type = VAR_PARTIAL;
6226 }
6227 else
6228 {
6229 if (set_string_copy(func->name, tv) == -1)
6230 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006231
Bram Moolenaar8110a092016-04-14 15:56:09 +02006232 tv->v_type = VAR_FUNC;
6233 }
6234 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006235 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006236 else if (PyBytes_Check(obj))
6237 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006238 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006239
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006240 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006241 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006242 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006243 return -1;
6244
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006245 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006246 return -1;
6247
6248 tv->v_type = VAR_STRING;
6249 }
6250 else if (PyUnicode_Check(obj))
6251 {
6252 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006253 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006254
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006255 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006256 if (bytes == NULL)
6257 return -1;
6258
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006259 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006260 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006261 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006262 return -1;
6263
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006264 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006265 {
6266 Py_XDECREF(bytes);
6267 return -1;
6268 }
6269 Py_XDECREF(bytes);
6270
6271 tv->v_type = VAR_STRING;
6272 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006273#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006274 else if (PyInt_Check(obj))
6275 {
6276 tv->v_type = VAR_NUMBER;
6277 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006278 if (PyErr_Occurred())
6279 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006280 }
6281#endif
6282 else if (PyLong_Check(obj))
6283 {
6284 tv->v_type = VAR_NUMBER;
6285 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006286 if (PyErr_Occurred())
6287 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006288 }
6289 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006290 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006291#ifdef FEAT_FLOAT
6292 else if (PyFloat_Check(obj))
6293 {
6294 tv->v_type = VAR_FLOAT;
6295 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6296 }
6297#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006298 else if (PyObject_HasAttrString(obj, "keys"))
6299 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006300 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006301 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006302 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006303 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006304 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006305 else if (PyNumber_Check(obj))
6306 {
6307 PyObject *num;
6308
6309 if (!(num = PyNumber_Long(obj)))
6310 return -1;
6311
6312 tv->v_type = VAR_NUMBER;
6313 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6314
6315 Py_DECREF(num);
6316 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006317 else if (obj == Py_None)
6318 {
6319 tv->v_type = VAR_SPECIAL;
6320 tv->vval.v_number = VVAL_NONE;
6321 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006322 else
6323 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006324 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006325 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006326 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006327 return -1;
6328 }
6329 return 0;
6330}
6331
6332 static PyObject *
6333ConvertToPyObject(typval_T *tv)
6334{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006335 typval_T *argv;
6336 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006337 if (tv == NULL)
6338 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006339 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006340 return NULL;
6341 }
6342 switch (tv->v_type)
6343 {
6344 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006345 return PyBytes_FromString(tv->vval.v_string == NULL
6346 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006347 case VAR_NUMBER:
6348 return PyLong_FromLong((long) tv->vval.v_number);
6349#ifdef FEAT_FLOAT
6350 case VAR_FLOAT:
6351 return PyFloat_FromDouble((double) tv->vval.v_float);
6352#endif
6353 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006354 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006355 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006356 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006357 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006358 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006359 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006360 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006361 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006362 if (tv->vval.v_partial->pt_argc)
6363 {
6364 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6365 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6366 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6367 }
6368 else
6369 argv = NULL;
6370 if (tv->vval.v_partial->pt_dict != NULL)
6371 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006372 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006373 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006374 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006375 tv->vval.v_partial->pt_dict,
6376 tv->vval.v_partial->pt_auto);
Bram Moolenaardb913952012-06-29 12:54:53 +02006377 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006378 case VAR_CHANNEL:
6379 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006380 Py_INCREF(Py_None);
6381 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006382 case VAR_SPECIAL:
6383 switch (tv->vval.v_number)
6384 {
6385 case VVAL_FALSE: return AlwaysFalse(NULL);
6386 case VVAL_TRUE: return AlwaysTrue(NULL);
6387 case VVAL_NONE:
6388 case VVAL_NULL: return AlwaysNone(NULL);
6389 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006390 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006391 return NULL;
6392 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006393 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006394}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006395
6396typedef struct
6397{
6398 PyObject_HEAD
6399} CurrentObject;
6400static PyTypeObject CurrentType;
6401
6402 static void
6403init_structs(void)
6404{
6405 vim_memset(&OutputType, 0, sizeof(OutputType));
6406 OutputType.tp_name = "vim.message";
6407 OutputType.tp_basicsize = sizeof(OutputObject);
6408 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6409 OutputType.tp_doc = "vim message object";
6410 OutputType.tp_methods = OutputMethods;
6411#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006412 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6413 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006414 OutputType.tp_alloc = call_PyType_GenericAlloc;
6415 OutputType.tp_new = call_PyType_GenericNew;
6416 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006417 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006418#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006419 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6420 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006421 // Disabled, because this causes a crash in test86
6422 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006423#endif
6424
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006425 vim_memset(&IterType, 0, sizeof(IterType));
6426 IterType.tp_name = "vim.iter";
6427 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006428 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006429 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006430 IterType.tp_iter = (getiterfunc)IterIter;
6431 IterType.tp_iternext = (iternextfunc)IterNext;
6432 IterType.tp_dealloc = (destructor)IterDestructor;
6433 IterType.tp_traverse = (traverseproc)IterTraverse;
6434 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006435
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006436 vim_memset(&BufferType, 0, sizeof(BufferType));
6437 BufferType.tp_name = "vim.buffer";
6438 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006439 BufferType.tp_dealloc = (destructor)BufferDestructor;
6440 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006441 BufferType.tp_as_sequence = &BufferAsSeq;
6442 BufferType.tp_as_mapping = &BufferAsMapping;
6443 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6444 BufferType.tp_doc = "vim buffer object";
6445 BufferType.tp_methods = BufferMethods;
6446#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006447 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006448 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006449 BufferType.tp_alloc = call_PyType_GenericAlloc;
6450 BufferType.tp_new = call_PyType_GenericNew;
6451 BufferType.tp_free = call_PyObject_Free;
6452#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006453 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006454 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006455#endif
6456
6457 vim_memset(&WindowType, 0, sizeof(WindowType));
6458 WindowType.tp_name = "vim.window";
6459 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006460 WindowType.tp_dealloc = (destructor)WindowDestructor;
6461 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006462 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006463 WindowType.tp_doc = "vim Window object";
6464 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006465 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6466 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006467#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006468 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6469 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006470 WindowType.tp_alloc = call_PyType_GenericAlloc;
6471 WindowType.tp_new = call_PyType_GenericNew;
6472 WindowType.tp_free = call_PyObject_Free;
6473#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006474 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6475 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006476#endif
6477
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006478 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6479 TabPageType.tp_name = "vim.tabpage";
6480 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006481 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6482 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006483 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6484 TabPageType.tp_doc = "vim tab page object";
6485 TabPageType.tp_methods = TabPageMethods;
6486#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006487 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006488 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6489 TabPageType.tp_new = call_PyType_GenericNew;
6490 TabPageType.tp_free = call_PyObject_Free;
6491#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006492 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006493#endif
6494
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006495 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6496 BufMapType.tp_name = "vim.bufferlist";
6497 BufMapType.tp_basicsize = sizeof(BufMapObject);
6498 BufMapType.tp_as_mapping = &BufMapAsMapping;
6499 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006500 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006501 BufferType.tp_doc = "vim buffer list";
6502
6503 vim_memset(&WinListType, 0, sizeof(WinListType));
6504 WinListType.tp_name = "vim.windowlist";
6505 WinListType.tp_basicsize = sizeof(WinListType);
6506 WinListType.tp_as_sequence = &WinListAsSeq;
6507 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6508 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006509 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006510
6511 vim_memset(&TabListType, 0, sizeof(TabListType));
6512 TabListType.tp_name = "vim.tabpagelist";
6513 TabListType.tp_basicsize = sizeof(TabListType);
6514 TabListType.tp_as_sequence = &TabListAsSeq;
6515 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6516 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006517
6518 vim_memset(&RangeType, 0, sizeof(RangeType));
6519 RangeType.tp_name = "vim.range";
6520 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006521 RangeType.tp_dealloc = (destructor)RangeDestructor;
6522 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006523 RangeType.tp_as_sequence = &RangeAsSeq;
6524 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006525 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006526 RangeType.tp_doc = "vim Range object";
6527 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006528 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6529 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006530#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006531 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006532 RangeType.tp_alloc = call_PyType_GenericAlloc;
6533 RangeType.tp_new = call_PyType_GenericNew;
6534 RangeType.tp_free = call_PyObject_Free;
6535#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006536 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006537#endif
6538
6539 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6540 CurrentType.tp_name = "vim.currentdata";
6541 CurrentType.tp_basicsize = sizeof(CurrentObject);
6542 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6543 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006544 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006545#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006546 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6547 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006548#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006549 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6550 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006551#endif
6552
6553 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6554 DictionaryType.tp_name = "vim.dictionary";
6555 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006556 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006557 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006558 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006559 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006560 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6561 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006562 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6563 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6564 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006565#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006566 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6567 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006568#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006569 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6570 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006571#endif
6572
6573 vim_memset(&ListType, 0, sizeof(ListType));
6574 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006575 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006576 ListType.tp_basicsize = sizeof(ListObject);
6577 ListType.tp_as_sequence = &ListAsSeq;
6578 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006579 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006580 ListType.tp_doc = "list pushing modifications to vim structure";
6581 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006582 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006583 ListType.tp_new = (newfunc)ListConstructor;
6584 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006585#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006586 ListType.tp_getattro = (getattrofunc)ListGetattro;
6587 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006588#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006589 ListType.tp_getattr = (getattrfunc)ListGetattr;
6590 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006591#endif
6592
6593 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006594 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006595 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006596 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6597 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006598 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006599 FunctionType.tp_doc = "object that calls vim function";
6600 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006601 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006602 FunctionType.tp_new = (newfunc)FunctionConstructor;
6603 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006604#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006605 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006606#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006607 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006608#endif
6609
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006610 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6611 OptionsType.tp_name = "vim.options";
6612 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006613 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006614 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006615 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006616 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006617 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006618 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6619 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6620 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006621
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006622#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006623 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6624 LoaderType.tp_name = "vim.Loader";
6625 LoaderType.tp_basicsize = sizeof(LoaderObject);
6626 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6627 LoaderType.tp_doc = "vim message object";
6628 LoaderType.tp_methods = LoaderMethods;
6629 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006630#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006631
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006632#if PY_MAJOR_VERSION >= 3
6633 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6634 vimmodule.m_name = "vim";
6635 vimmodule.m_doc = "Vim Python interface\n";
6636 vimmodule.m_size = -1;
6637 vimmodule.m_methods = VimMethods;
6638#endif
6639}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006640
6641#define PYTYPE_READY(type) \
6642 if (PyType_Ready(&type)) \
6643 return -1;
6644
6645 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006646init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006647{
6648 PYTYPE_READY(IterType);
6649 PYTYPE_READY(BufferType);
6650 PYTYPE_READY(RangeType);
6651 PYTYPE_READY(WindowType);
6652 PYTYPE_READY(TabPageType);
6653 PYTYPE_READY(BufMapType);
6654 PYTYPE_READY(WinListType);
6655 PYTYPE_READY(TabListType);
6656 PYTYPE_READY(CurrentType);
6657 PYTYPE_READY(DictionaryType);
6658 PYTYPE_READY(ListType);
6659 PYTYPE_READY(FunctionType);
6660 PYTYPE_READY(OptionsType);
6661 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006662#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006663 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006664#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006665 return 0;
6666}
6667
6668 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006669init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006670{
6671 PyObject *path;
6672 PyObject *path_hook;
6673 PyObject *path_hooks;
6674
6675 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6676 return -1;
6677
6678 if (!(path_hooks = PySys_GetObject("path_hooks")))
6679 {
6680 PyErr_Clear();
6681 path_hooks = PyList_New(1);
6682 PyList_SET_ITEM(path_hooks, 0, path_hook);
6683 if (PySys_SetObject("path_hooks", path_hooks))
6684 {
6685 Py_DECREF(path_hooks);
6686 return -1;
6687 }
6688 Py_DECREF(path_hooks);
6689 }
6690 else if (PyList_Check(path_hooks))
6691 {
6692 if (PyList_Append(path_hooks, path_hook))
6693 {
6694 Py_DECREF(path_hook);
6695 return -1;
6696 }
6697 Py_DECREF(path_hook);
6698 }
6699 else
6700 {
6701 VimTryStart();
6702 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6703 "You should now do the following:\n"
6704 "- append vim.path_hook to sys.path_hooks\n"
6705 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6706 VimTryEnd(); /* Discard the error */
6707 Py_DECREF(path_hook);
6708 return 0;
6709 }
6710
6711 if (!(path = PySys_GetObject("path")))
6712 {
6713 PyErr_Clear();
6714 path = PyList_New(1);
6715 Py_INCREF(vim_special_path_object);
6716 PyList_SET_ITEM(path, 0, vim_special_path_object);
6717 if (PySys_SetObject("path", path))
6718 {
6719 Py_DECREF(path);
6720 return -1;
6721 }
6722 Py_DECREF(path);
6723 }
6724 else if (PyList_Check(path))
6725 {
6726 if (PyList_Append(path, vim_special_path_object))
6727 return -1;
6728 }
6729 else
6730 {
6731 VimTryStart();
6732 EMSG(_("Failed to set path: sys.path is not a list\n"
6733 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6734 VimTryEnd(); /* Discard the error */
6735 }
6736
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006737 return 0;
6738}
6739
6740static BufMapObject TheBufferMap =
6741{
6742 PyObject_HEAD_INIT(&BufMapType)
6743};
6744
6745static WinListObject TheWindowList =
6746{
6747 PyObject_HEAD_INIT(&WinListType)
6748 NULL
6749};
6750
6751static CurrentObject TheCurrent =
6752{
6753 PyObject_HEAD_INIT(&CurrentType)
6754};
6755
6756static TabListObject TheTabPageList =
6757{
6758 PyObject_HEAD_INIT(&TabListType)
6759};
6760
6761static struct numeric_constant {
6762 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006763 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006764} numeric_constants[] = {
6765 {"VAR_LOCKED", VAR_LOCKED},
6766 {"VAR_FIXED", VAR_FIXED},
6767 {"VAR_SCOPE", VAR_SCOPE},
6768 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6769};
6770
6771static struct object_constant {
6772 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006773 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006774} object_constants[] = {
6775 {"buffers", (PyObject *)(void *)&TheBufferMap},
6776 {"windows", (PyObject *)(void *)&TheWindowList},
6777 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6778 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006779
6780 {"Buffer", (PyObject *)&BufferType},
6781 {"Range", (PyObject *)&RangeType},
6782 {"Window", (PyObject *)&WindowType},
6783 {"TabPage", (PyObject *)&TabPageType},
6784 {"Dictionary", (PyObject *)&DictionaryType},
6785 {"List", (PyObject *)&ListType},
6786 {"Function", (PyObject *)&FunctionType},
6787 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006788#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006789 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006790#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006791};
6792
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006793#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006794 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006795 return -1;
6796
6797#define ADD_CHECKED_OBJECT(m, name, obj) \
6798 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006799 PyObject *valObject = obj; \
6800 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006801 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006802 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006803 }
6804
6805 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006806populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006807{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006808 int i;
6809 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006810 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006811 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006812#if PY_VERSION_HEX >= 0x030700f0
6813 PyObject *dict;
6814 PyObject *cls;
6815#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006816
6817 for (i = 0; i < (int)(sizeof(numeric_constants)
6818 / sizeof(struct numeric_constant));
6819 ++i)
6820 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006821 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006822
6823 for (i = 0; i < (int)(sizeof(object_constants)
6824 / sizeof(struct object_constant));
6825 ++i)
6826 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006827 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006828
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006829 valObject = object_constants[i].valObject;
6830 Py_INCREF(valObject);
6831 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006832 }
6833
6834 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6835 return -1;
6836 ADD_OBJECT(m, "error", VimError);
6837
Bram Moolenaara9922d62013-05-30 13:01:18 +02006838 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6839 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006840 ADD_CHECKED_OBJECT(m, "options",
6841 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006842
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006843 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006844 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006845 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006846
Bram Moolenaar22081f42016-06-01 20:38:34 +02006847#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006848 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006849 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006850#else
6851 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6852 return -1;
6853#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006854 ADD_OBJECT(m, "_getcwd", py_getcwd)
6855
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006856 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006857 return -1;
6858 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006859 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006860 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006861 if (PyObject_SetAttrString(other_module, "chdir", attr))
6862 {
6863 Py_DECREF(attr);
6864 return -1;
6865 }
6866 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006867
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006868 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006869 {
6870 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006871 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006872 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006873 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6874 {
6875 Py_DECREF(attr);
6876 return -1;
6877 }
6878 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006879 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006880 else
6881 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006882
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006883 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6884 return -1;
6885
6886 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6887
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006888#if PY_VERSION_HEX >= 0x030700f0
6889 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6890 return -1;
6891
6892 dict = PyModule_GetDict(imp);
6893
6894 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6895 {
6896 Py_DECREF(imp);
6897 return -1;
6898 }
6899
6900 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6901 {
6902 Py_DECREF(imp);
6903 return -1;
6904 }
6905
6906 Py_DECREF(imp);
6907
6908 ADD_OBJECT(m, "_find_spec", py_find_spec);
6909#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006910 if (!(imp = PyImport_ImportModule("imp")))
6911 return -1;
6912
6913 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6914 {
6915 Py_DECREF(imp);
6916 return -1;
6917 }
6918
6919 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6920 {
6921 Py_DECREF(py_find_module);
6922 Py_DECREF(imp);
6923 return -1;
6924 }
6925
6926 Py_DECREF(imp);
6927
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006928 ADD_OBJECT(m, "_find_module", py_find_module);
6929 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006930#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006931
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006932 return 0;
6933}