blob: 1c159260c736715200b23342d7372fe816e6e1db [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 Moolenaar9123c0b2018-12-22 18:59:06 +01002925 self = (FunctionObject *)subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002926 if (self == NULL)
2927 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002928
2929 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002930 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002931 if (!translated_function_exists(name))
2932 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002933 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002934 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002935 return NULL;
2936 }
2937 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002938 }
2939 else
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002940 {
2941 char_u *p;
2942
2943 if ((p = get_expanded_name(name,
2944 vim_strchr(name, AUTOLOAD_CHAR) == NULL)) == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002945 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002946 PyErr_FORMAT(PyExc_ValueError,
2947 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002948 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002949 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002950
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002951 if (p[0] == K_SPECIAL && p[1] == KS_EXTRA && p[2] == (int)KE_SNR)
2952 {
2953 char_u *np;
2954 size_t len = STRLEN(p) + 1;
2955
Bram Moolenaar80dae042018-12-23 13:36:40 +01002956 if ((np = alloc((int)len + 2)) == NULL)
Bram Moolenaar9123c0b2018-12-22 18:59:06 +01002957 {
2958 vim_free(p);
2959 return NULL;
2960 }
2961 mch_memmove(np, "<SNR>", 5);
2962 mch_memmove(np + 5, p + 3, len - 3);
2963 vim_free(p);
2964 self->name = np;
2965 }
2966 else
2967 self->name = p;
2968 }
2969
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002970 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002971 self->argc = argc;
2972 self->argv = argv;
2973 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002974 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002975
2976 if (self->argv || self->self)
2977 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2978
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002979 return (PyObject *)(self);
2980}
2981
2982 static PyObject *
2983FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2984{
2985 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002986 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002987 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002988 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002989 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002990 typval_T selfdicttv;
2991 typval_T argstv;
2992 list_T *argslist = NULL;
2993 dict_T *selfdict = NULL;
2994 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002995 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002996 typval_T *argv = NULL;
2997 typval_T *curtv;
2998 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002999
Bram Moolenaar8110a092016-04-14 15:56:09 +02003000 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003001 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02003002 selfdictObject = PyDict_GetItemString(kwargs, "self");
3003 if (selfdictObject != NULL)
3004 {
3005 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
3006 return NULL;
3007 selfdict = selfdicttv.vval.v_dict;
3008 }
3009 argsObject = PyDict_GetItemString(kwargs, "args");
3010 if (argsObject != NULL)
3011 {
3012 if (ConvertFromPySequence(argsObject, &argstv) == -1)
3013 {
3014 dict_unref(selfdict);
3015 return NULL;
3016 }
3017 argslist = argstv.vval.v_list;
3018
3019 argc = argslist->lv_len;
3020 if (argc != 0)
3021 {
3022 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02003023 if (argv == NULL)
3024 {
3025 PyErr_NoMemory();
3026 dict_unref(selfdict);
3027 list_unref(argslist);
3028 return NULL;
3029 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02003030 curtv = argv;
3031 for (li = argslist->lv_first; li != NULL; li = li->li_next)
3032 copy_tv(&li->li_tv, curtv++);
3033 }
3034 list_unref(argslist);
3035 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003036 if (selfdict != NULL)
3037 {
3038 auto_rebind = FALSE;
3039 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
3040 if (autoRebindObject != NULL)
3041 {
3042 auto_rebind = PyObject_IsTrue(autoRebindObject);
3043 if (auto_rebind == -1)
3044 {
3045 dict_unref(selfdict);
3046 list_unref(argslist);
3047 return NULL;
3048 }
3049 }
3050 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003051 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003052
Bram Moolenaar389a1792013-06-23 13:00:44 +02003053 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02003054 {
3055 dict_unref(selfdict);
3056 while (argc--)
3057 clear_tv(&argv[argc]);
3058 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003059 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003060 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003061
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003062 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003063
Bram Moolenaar389a1792013-06-23 13:00:44 +02003064 PyMem_Free(name);
3065
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003066 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02003067}
3068
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003069 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003070FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003071{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003072 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003073 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02003074 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003075 for (i = 0; i < self->argc; ++i)
3076 clear_tv(&self->argv[i]);
3077 PyMem_Free(self->argv);
3078 dict_unref(self->self);
3079 if (self->argv || self->self)
3080 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003081
3082 DESTRUCTOR_FINISH(self);
3083}
3084
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003085static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003086 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003087 NULL
3088};
3089
3090 static PyObject *
3091FunctionDir(PyObject *self)
3092{
3093 return ObjectDir(self, FunctionAttrs);
3094}
3095
Bram Moolenaardb913952012-06-29 12:54:53 +02003096 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003097FunctionAttr(FunctionObject *self, char *name)
3098{
3099 list_T *list;
3100 int i;
3101 if (strcmp(name, "name") == 0)
3102 return PyString_FromString((char *)(self->name));
3103 else if (strcmp(name, "args") == 0)
3104 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003105 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003106 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003107
Bram Moolenaar8110a092016-04-14 15:56:09 +02003108 for (i = 0; i < self->argc; ++i)
3109 list_append_tv(list, &self->argv[i]);
3110 return NEW_LIST(list);
3111 }
3112 else if (strcmp(name, "self") == 0)
3113 return self->self == NULL
3114 ? AlwaysNone(NULL)
3115 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003116 else if (strcmp(name, "auto_rebind") == 0)
3117 return self->auto_rebind
3118 ? AlwaysTrue(NULL)
3119 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003120 else if (strcmp(name, "__members__") == 0)
3121 return ObjectDir(NULL, FunctionAttrs);
3122 return NULL;
3123}
3124
3125/* Populate partial_T given function object.
3126 *
3127 * "exported" should be set to true when it is needed to construct a partial
3128 * that may be stored in a variable (i.e. may be freed by Vim).
3129 */
3130 static void
3131set_partial(FunctionObject *self, partial_T *pt, int exported)
3132{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003133 int i;
3134
3135 pt->pt_name = self->name;
3136 if (self->argv)
3137 {
3138 pt->pt_argc = self->argc;
3139 if (exported)
3140 {
3141 pt->pt_argv = (typval_T *)alloc_clear(
3142 sizeof(typval_T) * self->argc);
3143 for (i = 0; i < pt->pt_argc; ++i)
3144 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3145 }
3146 else
3147 pt->pt_argv = self->argv;
3148 }
3149 else
3150 {
3151 pt->pt_argc = 0;
3152 pt->pt_argv = NULL;
3153 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003154 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003155 pt->pt_dict = self->self;
3156 if (exported && self->self)
3157 ++pt->pt_dict->dv_refcount;
3158 if (exported)
3159 pt->pt_name = vim_strsave(pt->pt_name);
3160 pt->pt_refcount = 1;
3161}
3162
3163 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003164FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003165{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003166 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003167 typval_T args;
3168 typval_T selfdicttv;
3169 typval_T rettv;
3170 dict_T *selfdict = NULL;
3171 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003172 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003173 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003174 partial_T pt;
3175 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003176
Bram Moolenaar8110a092016-04-14 15:56:09 +02003177 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003178 return NULL;
3179
3180 if (kwargs != NULL)
3181 {
3182 selfdictObject = PyDict_GetItemString(kwargs, "self");
3183 if (selfdictObject != NULL)
3184 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003185 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003186 {
3187 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003188 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003189 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003190 selfdict = selfdicttv.vval.v_dict;
3191 }
3192 }
3193
Bram Moolenaar8110a092016-04-14 15:56:09 +02003194 if (self->argv || self->self)
3195 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003196 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003197 set_partial(self, &pt, FALSE);
3198 pt_ptr = &pt;
3199 }
3200
Bram Moolenaar71700b82013-05-15 17:49:05 +02003201 Py_BEGIN_ALLOW_THREADS
3202 Python_Lock_Vim();
3203
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003204 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003205 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003206
3207 Python_Release_Vim();
3208 Py_END_ALLOW_THREADS
3209
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003210 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003211 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003212 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003213 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003214 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003215 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003216 }
3217 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003218 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003219
Bram Moolenaardb913952012-06-29 12:54:53 +02003220 clear_tv(&args);
3221 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003222 if (selfdict != NULL)
3223 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003224
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003225 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003226}
3227
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003228 static PyObject *
3229FunctionRepr(FunctionObject *self)
3230{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003231 PyObject *ret;
3232 garray_T repr_ga;
3233 int i;
3234 char_u *tofree = NULL;
3235 typval_T tv;
3236 char_u numbuf[NUMBUFLEN];
3237
3238 ga_init2(&repr_ga, (int)sizeof(char), 70);
3239 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3240 if (self->name)
3241 ga_concat(&repr_ga, self->name);
3242 else
3243 ga_concat(&repr_ga, (char_u *)"<NULL>");
3244 ga_append(&repr_ga, '\'');
3245 if (self->argv)
3246 {
3247 ga_concat(&repr_ga, (char_u *)", args=[");
3248 ++emsg_silent;
3249 for (i = 0; i < self->argc; i++)
3250 {
3251 if (i != 0)
3252 ga_concat(&repr_ga, (char_u *)", ");
3253 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3254 get_copyID()));
3255 vim_free(tofree);
3256 }
3257 --emsg_silent;
3258 ga_append(&repr_ga, ']');
3259 }
3260 if (self->self)
3261 {
3262 ga_concat(&repr_ga, (char_u *)", self=");
3263 tv.v_type = VAR_DICT;
3264 tv.vval.v_dict = self->self;
3265 ++emsg_silent;
3266 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3267 --emsg_silent;
3268 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003269 if (self->auto_rebind)
3270 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003271 }
3272 ga_append(&repr_ga, '>');
3273 ret = PyString_FromString((char *)repr_ga.ga_data);
3274 ga_clear(&repr_ga);
3275 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003276}
3277
Bram Moolenaardb913952012-06-29 12:54:53 +02003278static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003279 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3280 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003281};
3282
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003283/*
3284 * Options object
3285 */
3286
3287static PyTypeObject OptionsType;
3288
3289typedef int (*checkfun)(void *);
3290
3291typedef struct
3292{
3293 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003294 int opt_type;
3295 void *from;
3296 checkfun Check;
3297 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003298} OptionsObject;
3299
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003300 static int
3301dummy_check(void *arg UNUSED)
3302{
3303 return 0;
3304}
3305
3306 static PyObject *
3307OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3308{
3309 OptionsObject *self;
3310
Bram Moolenaar774267b2013-05-21 20:51:59 +02003311 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003312 if (self == NULL)
3313 return NULL;
3314
3315 self->opt_type = opt_type;
3316 self->from = from;
3317 self->Check = Check;
3318 self->fromObj = fromObj;
3319 if (fromObj)
3320 Py_INCREF(fromObj);
3321
3322 return (PyObject *)(self);
3323}
3324
3325 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003326OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003327{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003328 PyObject_GC_UnTrack((void *)(self));
3329 Py_XDECREF(self->fromObj);
3330 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003331}
3332
3333 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003334OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003335{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003336 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003337 return 0;
3338}
3339
3340 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003341OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003342{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003343 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003344 return 0;
3345}
3346
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003347 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003348OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003349{
3350 char_u *key;
3351 int flags;
3352 long numval;
3353 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003354 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003355
Bram Moolenaard6e39182013-05-21 18:30:34 +02003356 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003357 return NULL;
3358
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003359 if (!(key = StringToChars(keyObject, &todecref)))
3360 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003361
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003362 if (*key == NUL)
3363 {
3364 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003365 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003366 return NULL;
3367 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003368
3369 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003370 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003371
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003372 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003373
3374 if (flags == 0)
3375 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003376 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003377 return NULL;
3378 }
3379
3380 if (flags & SOPT_UNSET)
3381 {
3382 Py_INCREF(Py_None);
3383 return Py_None;
3384 }
3385 else if (flags & SOPT_BOOL)
3386 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003387 PyObject *ret;
3388 ret = numval ? Py_True : Py_False;
3389 Py_INCREF(ret);
3390 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003391 }
3392 else if (flags & SOPT_NUM)
3393 return PyInt_FromLong(numval);
3394 else if (flags & SOPT_STRING)
3395 {
3396 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003397 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003398 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003399 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003400 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003401 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003402 else
3403 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003404 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003405 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003406 return NULL;
3407 }
3408 }
3409 else
3410 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003411 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003412 return NULL;
3413 }
3414}
3415
3416 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003417OptionsContains(OptionsObject *self, PyObject *keyObject)
3418{
3419 char_u *key;
3420 PyObject *todecref;
3421
3422 if (!(key = StringToChars(keyObject, &todecref)))
3423 return -1;
3424
3425 if (*key == NUL)
3426 {
3427 Py_XDECREF(todecref);
3428 return 0;
3429 }
3430
3431 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3432 {
3433 Py_XDECREF(todecref);
3434 return 1;
3435 }
3436 else
3437 {
3438 Py_XDECREF(todecref);
3439 return 0;
3440 }
3441}
3442
3443typedef struct
3444{
3445 void *lastoption;
3446 int opt_type;
3447} optiterinfo_T;
3448
3449 static PyObject *
3450OptionsIterNext(optiterinfo_T **oii)
3451{
3452 char_u *name;
3453
3454 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3455 return PyString_FromString((char *)name);
3456
3457 return NULL;
3458}
3459
3460 static PyObject *
3461OptionsIter(OptionsObject *self)
3462{
3463 optiterinfo_T *oii;
3464
3465 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3466 {
3467 PyErr_NoMemory();
3468 return NULL;
3469 }
3470
3471 oii->opt_type = self->opt_type;
3472 oii->lastoption = NULL;
3473
3474 return IterNew(oii,
3475 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3476 NULL, NULL);
3477}
3478
3479 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003480set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003481{
3482 char_u *errmsg;
3483
3484 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3485 {
3486 if (VimTryEnd())
3487 return FAIL;
3488 PyErr_SetVim((char *)errmsg);
3489 return FAIL;
3490 }
3491 return OK;
3492}
3493
3494 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003495set_option_value_for(
3496 char_u *key,
3497 int numval,
3498 char_u *stringval,
3499 int opt_flags,
3500 int opt_type,
3501 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003502{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003503 win_T *save_curwin = NULL;
3504 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003505 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003506 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003507
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003508 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003509 switch (opt_type)
3510 {
3511 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003512 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003513 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003514 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003515 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003516 if (VimTryEnd())
3517 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003518 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003519 return -1;
3520 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003521 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3522 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003523 break;
3524 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003525 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003526 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003527 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003528 break;
3529 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003530 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003531 break;
3532 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003533 if (set_ret == FAIL)
3534 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003535 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003536}
3537
3538 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003539OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003540{
3541 char_u *key;
3542 int flags;
3543 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003544 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003545 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003546
Bram Moolenaard6e39182013-05-21 18:30:34 +02003547 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003548 return -1;
3549
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003550 if (!(key = StringToChars(keyObject, &todecref)))
3551 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003552
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003553 if (*key == NUL)
3554 {
3555 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003556 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003557 return -1;
3558 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003559
3560 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003561 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003562
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003563 if (flags == 0)
3564 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003565 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003566 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003567 return -1;
3568 }
3569
3570 if (valObject == NULL)
3571 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003572 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003573 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003574 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003575 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003576 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003577 return -1;
3578 }
3579 else if (!(flags & SOPT_GLOBAL))
3580 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003581 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003582 N_("unable to unset option %s "
3583 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003584 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003585 return -1;
3586 }
3587 else
3588 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003589 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003590 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003591 return 0;
3592 }
3593 }
3594
Bram Moolenaard6e39182013-05-21 18:30:34 +02003595 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003596
3597 if (flags & SOPT_BOOL)
3598 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003599 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003600
Bram Moolenaarb983f752013-05-15 16:11:50 +02003601 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003602 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003603 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003604 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003605 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003606 }
3607 else if (flags & SOPT_NUM)
3608 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003609 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003610
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003611 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003612 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003613 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003614 return -1;
3615 }
3616
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003617 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003618 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003619 }
3620 else
3621 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003622 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003623 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003624
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003625 if ((val = StringToChars(valObject, &todecref2)))
3626 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003627 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003628 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003629 Py_XDECREF(todecref2);
3630 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003631 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003632 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003633 }
3634
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003635 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003636
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003637 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003638}
3639
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003640static PySequenceMethods OptionsAsSeq = {
3641 0, /* sq_length */
3642 0, /* sq_concat */
3643 0, /* sq_repeat */
3644 0, /* sq_item */
3645 0, /* sq_slice */
3646 0, /* sq_ass_item */
3647 0, /* sq_ass_slice */
3648 (objobjproc) OptionsContains, /* sq_contains */
3649 0, /* sq_inplace_concat */
3650 0, /* sq_inplace_repeat */
3651};
3652
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003653static PyMappingMethods OptionsAsMapping = {
3654 (lenfunc) NULL,
3655 (binaryfunc) OptionsItem,
3656 (objobjargproc) OptionsAssItem,
3657};
3658
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003659/* Tabpage object
3660 */
3661
3662typedef struct
3663{
3664 PyObject_HEAD
3665 tabpage_T *tab;
3666} TabPageObject;
3667
3668static PyObject *WinListNew(TabPageObject *tabObject);
3669
3670static PyTypeObject TabPageType;
3671
3672 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003673CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003674{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003675 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003676 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003677 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003678 return -1;
3679 }
3680
3681 return 0;
3682}
3683
3684 static PyObject *
3685TabPageNew(tabpage_T *tab)
3686{
3687 TabPageObject *self;
3688
3689 if (TAB_PYTHON_REF(tab))
3690 {
3691 self = TAB_PYTHON_REF(tab);
3692 Py_INCREF(self);
3693 }
3694 else
3695 {
3696 self = PyObject_NEW(TabPageObject, &TabPageType);
3697 if (self == NULL)
3698 return NULL;
3699 self->tab = tab;
3700 TAB_PYTHON_REF(tab) = self;
3701 }
3702
3703 return (PyObject *)(self);
3704}
3705
3706 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003707TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003708{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003709 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3710 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003711
3712 DESTRUCTOR_FINISH(self);
3713}
3714
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003715static char *TabPageAttrs[] = {
3716 "windows", "number", "vars", "window", "valid",
3717 NULL
3718};
3719
3720 static PyObject *
3721TabPageDir(PyObject *self)
3722{
3723 return ObjectDir(self, TabPageAttrs);
3724}
3725
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003726 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003727TabPageAttrValid(TabPageObject *self, char *name)
3728{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003729 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003730
3731 if (strcmp(name, "valid") != 0)
3732 return NULL;
3733
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003734 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3735 Py_INCREF(ret);
3736 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003737}
3738
3739 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003740TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003741{
3742 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003743 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003744 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003745 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003746 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003747 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003748 else if (strcmp(name, "window") == 0)
3749 {
3750 /* For current tab window.c does not bother to set or update tp_curwin
3751 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003752 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003753 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003754 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003755 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003756 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003757 else if (strcmp(name, "__members__") == 0)
3758 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003759 return NULL;
3760}
3761
3762 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003763TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003764{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003765 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003766 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003767 else
3768 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003769 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003770
3771 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003772 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3773 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003774 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003775 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003776 }
3777}
3778
3779static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003780 /* name, function, calling, documentation */
3781 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3782 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003783};
3784
3785/*
3786 * Window list object
3787 */
3788
3789static PyTypeObject TabListType;
3790static PySequenceMethods TabListAsSeq;
3791
3792typedef struct
3793{
3794 PyObject_HEAD
3795} TabListObject;
3796
3797 static PyInt
3798TabListLength(PyObject *self UNUSED)
3799{
3800 tabpage_T *tp = first_tabpage;
3801 PyInt n = 0;
3802
3803 while (tp != NULL)
3804 {
3805 ++n;
3806 tp = tp->tp_next;
3807 }
3808
3809 return n;
3810}
3811
3812 static PyObject *
3813TabListItem(PyObject *self UNUSED, PyInt n)
3814{
3815 tabpage_T *tp;
3816
3817 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3818 if (n == 0)
3819 return TabPageNew(tp);
3820
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003821 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003822 return NULL;
3823}
3824
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003825/*
3826 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003827 */
3828
3829typedef struct
3830{
3831 PyObject_HEAD
3832 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003833 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003834} WindowObject;
3835
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003836static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003837
3838 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003839CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003840{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003841 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003842 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003843 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003844 return -1;
3845 }
3846
3847 return 0;
3848}
3849
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003850 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003851WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003852{
3853 /* We need to handle deletion of windows underneath us.
3854 * If we add a "w_python*_ref" field to the win_T structure,
3855 * then we can get at it in win_free() in vim. We then
3856 * need to create only ONE Python object per window - if
3857 * we try to create a second, just INCREF the existing one
3858 * and return it. The (single) Python object referring to
3859 * the window is stored in "w_python*_ref".
3860 * On a win_free() we set the Python object's win_T* field
3861 * to an invalid value. We trap all uses of a window
3862 * object, and reject them if the win_T* field is invalid.
3863 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003864 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003865 * w_python_ref and w_python3_ref fields respectively.
3866 */
3867
3868 WindowObject *self;
3869
3870 if (WIN_PYTHON_REF(win))
3871 {
3872 self = WIN_PYTHON_REF(win);
3873 Py_INCREF(self);
3874 }
3875 else
3876 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003877 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003878 if (self == NULL)
3879 return NULL;
3880 self->win = win;
3881 WIN_PYTHON_REF(win) = self;
3882 }
3883
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003884 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3885
Bram Moolenaar971db462013-05-12 18:44:48 +02003886 return (PyObject *)(self);
3887}
3888
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003889 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003890WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003891{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003892 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003893 if (self->win && self->win != INVALID_WINDOW_VALUE)
3894 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003895 Py_XDECREF(((PyObject *)(self->tabObject)));
3896 PyObject_GC_Del((void *)(self));
3897}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003898
Bram Moolenaar774267b2013-05-21 20:51:59 +02003899 static int
3900WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3901{
3902 Py_VISIT(((PyObject *)(self->tabObject)));
3903 return 0;
3904}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003905
Bram Moolenaar774267b2013-05-21 20:51:59 +02003906 static int
3907WindowClear(WindowObject *self)
3908{
3909 Py_CLEAR(self->tabObject);
3910 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003911}
3912
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003913 static win_T *
3914get_firstwin(TabPageObject *tabObject)
3915{
3916 if (tabObject)
3917 {
3918 if (CheckTabPage(tabObject))
3919 return NULL;
3920 /* For current tab window.c does not bother to set or update tp_firstwin
3921 */
3922 else if (tabObject->tab == curtab)
3923 return firstwin;
3924 else
3925 return tabObject->tab->tp_firstwin;
3926 }
3927 else
3928 return firstwin;
3929}
Bram Moolenaare950f992018-06-10 13:55:55 +02003930
3931// Use the same order as in the WindowAttr() function.
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003932static char *WindowAttrs[] = {
Bram Moolenaare950f992018-06-10 13:55:55 +02003933 "buffer",
3934 "cursor",
3935 "height",
3936 "row",
3937 "width",
3938 "col",
3939 "vars",
3940 "options",
3941 "number",
3942 "tabpage",
3943 "valid",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003944 NULL
3945};
3946
3947 static PyObject *
3948WindowDir(PyObject *self)
3949{
3950 return ObjectDir(self, WindowAttrs);
3951}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003952
Bram Moolenaar971db462013-05-12 18:44:48 +02003953 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003954WindowAttrValid(WindowObject *self, char *name)
3955{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003956 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003957
3958 if (strcmp(name, "valid") != 0)
3959 return NULL;
3960
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003961 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3962 Py_INCREF(ret);
3963 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003964}
3965
3966 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003967WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003968{
3969 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003970 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003971 else if (strcmp(name, "cursor") == 0)
3972 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003973 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003974
3975 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3976 }
3977 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003978 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003979 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003980 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003981 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003982 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003983 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003984 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003985 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003986 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003987 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003988 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3989 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003990 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003991 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003992 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003993 return NULL;
3994 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003995 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003996 }
3997 else if (strcmp(name, "tabpage") == 0)
3998 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003999 Py_INCREF(self->tabObject);
4000 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02004001 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004002 else if (strcmp(name, "__members__") == 0)
4003 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004004 else
4005 return NULL;
4006}
4007
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004008 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004009WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004010{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004011 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004012 return -1;
4013
4014 if (strcmp(name, "buffer") == 0)
4015 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004016 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004017 return -1;
4018 }
4019 else if (strcmp(name, "cursor") == 0)
4020 {
4021 long lnum;
4022 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004023
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004024 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004025 return -1;
4026
Bram Moolenaard6e39182013-05-21 18:30:34 +02004027 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004028 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004029 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004030 return -1;
4031 }
4032
4033 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004034 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004035 return -1;
4036
Bram Moolenaard6e39182013-05-21 18:30:34 +02004037 self->win->w_cursor.lnum = lnum;
4038 self->win->w_cursor.col = col;
Bram Moolenaar53901442018-07-25 22:02:36 +02004039 self->win->w_set_curswant = TRUE;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004040#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02004041 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004042#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004043 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004044 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004045
Bram Moolenaar03a807a2011-07-07 15:08:58 +02004046 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004047 return 0;
4048 }
4049 else if (strcmp(name, "height") == 0)
4050 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004051 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052 win_T *savewin;
4053
Bram Moolenaardee2e312013-06-23 16:35:47 +02004054 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004055 return -1;
4056
4057#ifdef FEAT_GUI
4058 need_mouse_correct = TRUE;
4059#endif
4060 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004061 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004062
4063 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004064 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004065 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004066 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004067 return -1;
4068
4069 return 0;
4070 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004071 else if (strcmp(name, "width") == 0)
4072 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004073 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 win_T *savewin;
4075
Bram Moolenaardee2e312013-06-23 16:35:47 +02004076 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004077 return -1;
4078
4079#ifdef FEAT_GUI
4080 need_mouse_correct = TRUE;
4081#endif
4082 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02004083 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004084
4085 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02004086 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004088 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004089 return -1;
4090
4091 return 0;
4092 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093 else
4094 {
4095 PyErr_SetString(PyExc_AttributeError, name);
4096 return -1;
4097 }
4098}
4099
4100 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004101WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004102{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004103 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004104 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004105 else
4106 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004107 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004108
Bram Moolenaar6d216452013-05-12 19:00:41 +02004109 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004110 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004111 (self));
4112 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004113 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 }
4115}
4116
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004117static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004118 /* name, function, calling, documentation */
4119 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4120 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004121};
4122
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004123/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004124 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004125 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004126
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004127static PyTypeObject WinListType;
4128static PySequenceMethods WinListAsSeq;
4129
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004130typedef struct
4131{
4132 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004133 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004134} WinListObject;
4135
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004136 static PyObject *
4137WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004138{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004139 WinListObject *self;
4140
4141 self = PyObject_NEW(WinListObject, &WinListType);
4142 self->tabObject = tabObject;
4143 Py_INCREF(tabObject);
4144
4145 return (PyObject *)(self);
4146}
4147
4148 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004149WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004150{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004151 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004152
4153 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004154 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004155 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004156 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004157
4158 DESTRUCTOR_FINISH(self);
4159}
4160
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004161 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004162WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004163{
4164 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004165 PyInt n = 0;
4166
Bram Moolenaard6e39182013-05-21 18:30:34 +02004167 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004168 return -1;
4169
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004170 while (w != NULL)
4171 {
4172 ++n;
4173 w = W_NEXT(w);
4174 }
4175
4176 return n;
4177}
4178
4179 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004180WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004181{
4182 win_T *w;
4183
Bram Moolenaard6e39182013-05-21 18:30:34 +02004184 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004185 return NULL;
4186
4187 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004188 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004189 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004190
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004191 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004192 return NULL;
4193}
4194
4195/* Convert a Python string into a Vim line.
4196 *
4197 * The result is in allocated memory. All internal nulls are replaced by
4198 * newline characters. It is an error for the string to contain newline
4199 * characters.
4200 *
4201 * On errors, the Python exception data is set, and NULL is returned.
4202 */
4203 static char *
4204StringToLine(PyObject *obj)
4205{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004206 char *str;
4207 char *save;
4208 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004209 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004210 PyInt i;
4211 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004212
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004213 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004214 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004215 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4216 || str == NULL)
4217 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004218 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004219 else if (PyUnicode_Check(obj))
4220 {
4221 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4222 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004223
Bram Moolenaardaa27022013-06-24 22:33:30 +02004224 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004225 || str == NULL)
4226 {
4227 Py_DECREF(bytes);
4228 return NULL;
4229 }
4230 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004231 else
4232 {
4233#if PY_MAJOR_VERSION < 3
4234 PyErr_FORMAT(PyExc_TypeError,
4235 N_("expected str() or unicode() instance, but got %s"),
4236 Py_TYPE_NAME(obj));
4237#else
4238 PyErr_FORMAT(PyExc_TypeError,
4239 N_("expected bytes() or str() instance, but got %s"),
4240 Py_TYPE_NAME(obj));
4241#endif
4242 return NULL;
4243 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004244
4245 /*
4246 * Error checking: String must not contain newlines, as we
4247 * are replacing a single line, and we must replace it with
4248 * a single line.
4249 * A trailing newline is removed, so that append(f.readlines()) works.
4250 */
4251 p = memchr(str, '\n', len);
4252 if (p != NULL)
4253 {
4254 if (p == str + len - 1)
4255 --len;
4256 else
4257 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004258 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004259 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004260 return NULL;
4261 }
4262 }
4263
4264 /* Create a copy of the string, with internal nulls replaced by
4265 * newline characters, as is the vim convention.
4266 */
4267 save = (char *)alloc((unsigned)(len+1));
4268 if (save == NULL)
4269 {
4270 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004271 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004272 return NULL;
4273 }
4274
4275 for (i = 0; i < len; ++i)
4276 {
4277 if (str[i] == '\0')
4278 save[i] = '\n';
4279 else
4280 save[i] = str[i];
4281 }
4282
4283 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004284 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004285
4286 return save;
4287}
4288
4289/* Get a line from the specified buffer. The line number is
4290 * in Vim format (1-based). The line is returned as a Python
4291 * string object.
4292 */
4293 static PyObject *
4294GetBufferLine(buf_T *buf, PyInt n)
4295{
4296 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4297}
4298
4299
4300/* Get a list of lines from the specified buffer. The line numbers
4301 * are in Vim format (1-based). The range is from lo up to, but not
4302 * including, hi. The list is returned as a Python list of string objects.
4303 */
4304 static PyObject *
4305GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4306{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004307 PyInt i;
4308 PyInt n = hi - lo;
4309 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004310
4311 if (list == NULL)
4312 return NULL;
4313
4314 for (i = 0; i < n; ++i)
4315 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004316 PyObject *string = LineToString(
4317 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004318
4319 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004320 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004321 {
4322 Py_DECREF(list);
4323 return NULL;
4324 }
4325
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004326 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004327 }
4328
4329 /* The ownership of the Python list is passed to the caller (ie,
4330 * the caller should Py_DECREF() the object when it is finished
4331 * with it).
4332 */
4333
4334 return list;
4335}
4336
4337/*
4338 * Check if deleting lines made the cursor position invalid.
4339 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4340 * deleted).
4341 */
4342 static void
4343py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4344{
4345 if (curwin->w_cursor.lnum >= lo)
4346 {
4347 /* Adjust the cursor position if it's in/after the changed
4348 * lines. */
4349 if (curwin->w_cursor.lnum >= hi)
4350 {
4351 curwin->w_cursor.lnum += extra;
4352 check_cursor_col();
4353 }
4354 else if (extra < 0)
4355 {
4356 curwin->w_cursor.lnum = lo;
4357 check_cursor();
4358 }
4359 else
4360 check_cursor_col();
4361 changed_cline_bef_curs();
4362 }
4363 invalidate_botline();
4364}
4365
Bram Moolenaar19e60942011-06-19 00:27:51 +02004366/*
4367 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004368 * in Vim format (1-based). The replacement line is given as
4369 * a Python string object. The object is checked for validity
4370 * and correct format. Errors are returned as a value of FAIL.
4371 * The return value is OK on success.
4372 * If OK is returned and len_change is not NULL, *len_change
4373 * is set to the change in the buffer length.
4374 */
4375 static int
4376SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4377{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004378 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004379 win_T *save_curwin = NULL;
4380 tabpage_T *save_curtab = NULL;
4381
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004382 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004383 * There are three cases:
4384 * 1. NULL, or None - this is a deletion.
4385 * 2. A string - this is a replacement.
4386 * 3. Anything else - this is an error.
4387 */
4388 if (line == Py_None || line == NULL)
4389 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004391 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004392
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004393 VimTryStart();
4394
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004395 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004396 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004397 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004398 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004399 else
4400 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004401 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004402 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004403 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004404 /* Only adjust marks if we managed to switch to a window that
4405 * holds the buffer, otherwise line numbers will be invalid. */
4406 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004407 }
4408
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004409 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004410
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004411 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004412 return FAIL;
4413
4414 if (len_change)
4415 *len_change = -1;
4416
4417 return OK;
4418 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004419 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004420 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004421 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004422
4423 if (save == NULL)
4424 return FAIL;
4425
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004426 VimTryStart();
4427
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004428 /* We do not need to free "save" if ml_replace() consumes it. */
4429 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004430 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004431
4432 if (u_savesub((linenr_T)n) == FAIL)
4433 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004434 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004435 vim_free(save);
4436 }
4437 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4438 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004439 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004440 vim_free(save);
4441 }
4442 else
4443 changed_bytes((linenr_T)n, 0);
4444
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004445 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004446
4447 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004448 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004449 check_cursor_col();
4450
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004451 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004452 return FAIL;
4453
4454 if (len_change)
4455 *len_change = 0;
4456
4457 return OK;
4458 }
4459 else
4460 {
4461 PyErr_BadArgument();
4462 return FAIL;
4463 }
4464}
4465
Bram Moolenaar19e60942011-06-19 00:27:51 +02004466/* Replace a range of lines in the specified buffer. The line numbers are in
4467 * Vim format (1-based). The range is from lo up to, but not including, hi.
4468 * The replacement lines are given as a Python list of string objects. The
4469 * list is checked for validity and correct format. Errors are returned as a
4470 * value of FAIL. The return value is OK on success.
4471 * If OK is returned and len_change is not NULL, *len_change
4472 * is set to the change in the buffer length.
4473 */
4474 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004475SetBufferLineList(
4476 buf_T *buf,
4477 PyInt lo,
4478 PyInt hi,
4479 PyObject *list,
4480 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004481{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004482 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004483 win_T *save_curwin = NULL;
4484 tabpage_T *save_curtab = NULL;
4485
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004486 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004487 * There are three cases:
4488 * 1. NULL, or None - this is a deletion.
4489 * 2. A list - this is a replacement.
4490 * 3. Anything else - this is an error.
4491 */
4492 if (list == Py_None || list == NULL)
4493 {
4494 PyInt i;
4495 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004496
4497 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004498 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004499 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004500
4501 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004502 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004503 else
4504 {
4505 for (i = 0; i < n; ++i)
4506 {
4507 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4508 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004509 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004510 break;
4511 }
4512 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004513 if (buf == curbuf && (save_curwin != NULL
4514 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004515 /* Using an existing window for the buffer, adjust the cursor
4516 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004517 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004518 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004519 /* Only adjust marks if we managed to switch to a window that
4520 * holds the buffer, otherwise line numbers will be invalid. */
4521 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004522 }
4523
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004524 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004525
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004526 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004527 return FAIL;
4528
4529 if (len_change)
4530 *len_change = -n;
4531
4532 return OK;
4533 }
4534 else if (PyList_Check(list))
4535 {
4536 PyInt i;
4537 PyInt new_len = PyList_Size(list);
4538 PyInt old_len = hi - lo;
4539 PyInt extra = 0; /* lines added to text, can be negative */
4540 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004541
4542 if (new_len == 0) /* avoid allocating zero bytes */
4543 array = NULL;
4544 else
4545 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004546 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004547 if (array == NULL)
4548 {
4549 PyErr_NoMemory();
4550 return FAIL;
4551 }
4552 }
4553
4554 for (i = 0; i < new_len; ++i)
4555 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004556 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004557
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004558 if (!(line = PyList_GetItem(list, i)) ||
4559 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004560 {
4561 while (i)
4562 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004563 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004564 return FAIL;
4565 }
4566 }
4567
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004568 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004569 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004570
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004571 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004572 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004573
4574 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004575 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004576
4577 /* If the size of the range is reducing (ie, new_len < old_len) we
4578 * need to delete some old_len. We do this at the start, by
4579 * repeatedly deleting line "lo".
4580 */
4581 if (!PyErr_Occurred())
4582 {
4583 for (i = 0; i < old_len - new_len; ++i)
4584 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4585 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004586 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004587 break;
4588 }
4589 extra -= i;
4590 }
4591
4592 /* For as long as possible, replace the existing old_len with the
4593 * new old_len. This is a more efficient operation, as it requires
4594 * less memory allocation and freeing.
4595 */
4596 if (!PyErr_Occurred())
4597 {
4598 for (i = 0; i < old_len && i < new_len; ++i)
4599 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4600 == FAIL)
4601 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004602 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004603 break;
4604 }
4605 }
4606 else
4607 i = 0;
4608
4609 /* Now we may need to insert the remaining new old_len. If we do, we
4610 * must free the strings as we finish with them (we can't pass the
4611 * responsibility to vim in this case).
4612 */
4613 if (!PyErr_Occurred())
4614 {
4615 while (i < new_len)
4616 {
4617 if (ml_append((linenr_T)(lo + i - 1),
4618 (char_u *)array[i], 0, FALSE) == FAIL)
4619 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004620 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004621 break;
4622 }
4623 vim_free(array[i]);
4624 ++i;
4625 ++extra;
4626 }
4627 }
4628
4629 /* Free any left-over old_len, as a result of an error */
4630 while (i < new_len)
4631 {
4632 vim_free(array[i]);
4633 ++i;
4634 }
4635
4636 /* Free the array of old_len. All of its contents have now
4637 * been dealt with (either freed, or the responsibility passed
4638 * to vim.
4639 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004640 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004641
4642 /* Adjust marks. Invalidate any which lie in the
4643 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004644 * Only adjust marks if we managed to switch to a window that holds
4645 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004646 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004647 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004648 (long)MAXLNUM, (long)extra);
4649 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4650
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004651 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004652 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4653
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004654 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004655 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004656
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004657 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004658 return FAIL;
4659
4660 if (len_change)
4661 *len_change = new_len - old_len;
4662
4663 return OK;
4664 }
4665 else
4666 {
4667 PyErr_BadArgument();
4668 return FAIL;
4669 }
4670}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004671
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004672/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004673 * The line number is in Vim format (1-based). The lines to be inserted are
4674 * given as a Python list of string objects or as a single string. The lines
4675 * to be added are checked for validity and correct format. Errors are
4676 * returned as a value of FAIL. The return value is OK on success.
4677 * If OK is returned and len_change is not NULL, *len_change
4678 * is set to the change in the buffer length.
4679 */
4680 static int
4681InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4682{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004683 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004684 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004685 tabpage_T *save_curtab = NULL;
4686
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004687 /* First of all, we check the type of the supplied Python object.
4688 * It must be a string or a list, or the call is in error.
4689 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004690 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004691 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004692 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004693
4694 if (str == NULL)
4695 return FAIL;
4696
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004697 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004698 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004699 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004700
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004701 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004702 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004703 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004704 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004705 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004706 /* Only adjust marks if we managed to switch to a window that
4707 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004708 appended_lines_mark((linenr_T)n, 1L);
4709
4710 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004711 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004712 update_screen(VALID);
4713
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004714 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004715 return FAIL;
4716
4717 if (len_change)
4718 *len_change = 1;
4719
4720 return OK;
4721 }
4722 else if (PyList_Check(lines))
4723 {
4724 PyInt i;
4725 PyInt size = PyList_Size(lines);
4726 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004727
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004728 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004729 if (array == NULL)
4730 {
4731 PyErr_NoMemory();
4732 return FAIL;
4733 }
4734
4735 for (i = 0; i < size; ++i)
4736 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004737 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004738
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004739 if (!(line = PyList_GetItem(lines, i)) ||
4740 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004741 {
4742 while (i)
4743 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004744 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004745 return FAIL;
4746 }
4747 }
4748
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004749 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004750 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004751 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004752
4753 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004754 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004755 else
4756 {
4757 for (i = 0; i < size; ++i)
4758 {
4759 if (ml_append((linenr_T)(n + i),
4760 (char_u *)array[i], 0, FALSE) == FAIL)
4761 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004762 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004763
4764 /* Free the rest of the lines */
4765 while (i < size)
4766 vim_free(array[i++]);
4767
4768 break;
4769 }
4770 vim_free(array[i]);
4771 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004772 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004773 /* Only adjust marks if we managed to switch to a window that
4774 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004775 appended_lines_mark((linenr_T)n, (long)i);
4776 }
4777
4778 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004779 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004780 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004781 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004782
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004783 update_screen(VALID);
4784
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004785 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786 return FAIL;
4787
4788 if (len_change)
4789 *len_change = size;
4790
4791 return OK;
4792 }
4793 else
4794 {
4795 PyErr_BadArgument();
4796 return FAIL;
4797 }
4798}
4799
4800/*
4801 * Common routines for buffers and line ranges
4802 * -------------------------------------------
4803 */
4804
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004805typedef struct
4806{
4807 PyObject_HEAD
4808 buf_T *buf;
4809} BufferObject;
4810
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004811 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004812CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004813{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004814 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004815 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004816 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004817 return -1;
4818 }
4819
4820 return 0;
4821}
4822
4823 static PyObject *
4824RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4825{
4826 if (CheckBuffer(self))
4827 return NULL;
4828
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004829 if (end == -1)
4830 end = self->buf->b_ml.ml_line_count;
4831
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004832 if (n < 0)
4833 n += end - start + 1;
4834
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004835 if (n < 0 || n > end - start)
4836 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004837 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004838 return NULL;
4839 }
4840
4841 return GetBufferLine(self->buf, n+start);
4842}
4843
4844 static PyObject *
4845RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4846{
4847 PyInt size;
4848
4849 if (CheckBuffer(self))
4850 return NULL;
4851
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004852 if (end == -1)
4853 end = self->buf->b_ml.ml_line_count;
4854
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004855 size = end - start + 1;
4856
4857 if (lo < 0)
4858 lo = 0;
4859 else if (lo > size)
4860 lo = size;
4861 if (hi < 0)
4862 hi = 0;
4863 if (hi < lo)
4864 hi = lo;
4865 else if (hi > size)
4866 hi = size;
4867
4868 return GetBufferLineList(self->buf, lo+start, hi+start);
4869}
4870
4871 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004872RBAsItem(
4873 BufferObject *self,
4874 PyInt n,
4875 PyObject *valObject,
4876 PyInt start,
4877 PyInt end,
4878 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004879{
4880 PyInt len_change;
4881
4882 if (CheckBuffer(self))
4883 return -1;
4884
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004885 if (end == -1)
4886 end = self->buf->b_ml.ml_line_count;
4887
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004888 if (n < 0)
4889 n += end - start + 1;
4890
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004891 if (n < 0 || n > end - start)
4892 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004893 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004894 return -1;
4895 }
4896
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004897 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004898 return -1;
4899
4900 if (new_end)
4901 *new_end = end + len_change;
4902
4903 return 0;
4904}
4905
Bram Moolenaar19e60942011-06-19 00:27:51 +02004906 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004907RBAsSlice(
4908 BufferObject *self,
4909 PyInt lo,
4910 PyInt hi,
4911 PyObject *valObject,
4912 PyInt start,
4913 PyInt end,
4914 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004915{
4916 PyInt size;
4917 PyInt len_change;
4918
4919 /* Self must be a valid buffer */
4920 if (CheckBuffer(self))
4921 return -1;
4922
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004923 if (end == -1)
4924 end = self->buf->b_ml.ml_line_count;
4925
Bram Moolenaar19e60942011-06-19 00:27:51 +02004926 /* Sort out the slice range */
4927 size = end - start + 1;
4928
4929 if (lo < 0)
4930 lo = 0;
4931 else if (lo > size)
4932 lo = size;
4933 if (hi < 0)
4934 hi = 0;
4935 if (hi < lo)
4936 hi = lo;
4937 else if (hi > size)
4938 hi = size;
4939
4940 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004941 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004942 return -1;
4943
4944 if (new_end)
4945 *new_end = end + len_change;
4946
4947 return 0;
4948}
4949
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004950
4951 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004952RBAppend(
4953 BufferObject *self,
4954 PyObject *args,
4955 PyInt start,
4956 PyInt end,
4957 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004958{
4959 PyObject *lines;
4960 PyInt len_change;
4961 PyInt max;
4962 PyInt n;
4963
4964 if (CheckBuffer(self))
4965 return NULL;
4966
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004967 if (end == -1)
4968 end = self->buf->b_ml.ml_line_count;
4969
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004970 max = n = end - start + 1;
4971
4972 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4973 return NULL;
4974
4975 if (n < 0 || n > max)
4976 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004977 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004978 return NULL;
4979 }
4980
4981 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4982 return NULL;
4983
4984 if (new_end)
4985 *new_end = end + len_change;
4986
4987 Py_INCREF(Py_None);
4988 return Py_None;
4989}
4990
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004991/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004992 */
4993
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004994static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004995static PySequenceMethods RangeAsSeq;
4996static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004997
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004998typedef struct
4999{
5000 PyObject_HEAD
5001 BufferObject *buf;
5002 PyInt start;
5003 PyInt end;
5004} RangeObject;
5005
5006 static PyObject *
5007RangeNew(buf_T *buf, PyInt start, PyInt end)
5008{
5009 BufferObject *bufr;
5010 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02005011 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005012 if (self == NULL)
5013 return NULL;
5014
5015 bufr = (BufferObject *)BufferNew(buf);
5016 if (bufr == NULL)
5017 {
5018 Py_DECREF(self);
5019 return NULL;
5020 }
5021 Py_INCREF(bufr);
5022
5023 self->buf = bufr;
5024 self->start = start;
5025 self->end = end;
5026
5027 return (PyObject *)(self);
5028}
5029
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005030 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005031RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005032{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005033 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005034 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02005035 PyObject_GC_Del((void *)(self));
5036}
5037
5038 static int
5039RangeTraverse(RangeObject *self, visitproc visit, void *arg)
5040{
5041 Py_VISIT(((PyObject *)(self->buf)));
5042 return 0;
5043}
5044
5045 static int
5046RangeClear(RangeObject *self)
5047{
5048 Py_CLEAR(self->buf);
5049 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005050}
5051
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005052 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005053RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005054{
5055 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005056 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005057 return -1; /* ??? */
5058
Bram Moolenaard6e39182013-05-21 18:30:34 +02005059 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005060}
5061
5062 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005063RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005064{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005065 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005066}
5067
5068 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005069RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005070{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005071 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005072}
5073
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005074static char *RangeAttrs[] = {
5075 "start", "end",
5076 NULL
5077};
5078
5079 static PyObject *
5080RangeDir(PyObject *self)
5081{
5082 return ObjectDir(self, RangeAttrs);
5083}
5084
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005085 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005086RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005087{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005088 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005089}
5090
5091 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005092RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005093{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005094 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005095 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
5096 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005097 else
5098 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02005099 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005100
5101 if (name == NULL)
5102 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005103
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005104 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005105 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005106 }
5107}
5108
5109static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005110 /* name, function, calling, documentation */
5111 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005112 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5113 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005114};
5115
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005116static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005117static PySequenceMethods BufferAsSeq;
5118static PyMappingMethods BufferAsMapping;
5119
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005120 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005121BufferNew(buf_T *buf)
5122{
5123 /* We need to handle deletion of buffers underneath us.
5124 * If we add a "b_python*_ref" field to the buf_T structure,
5125 * then we can get at it in buf_freeall() in vim. We then
5126 * need to create only ONE Python object per buffer - if
5127 * we try to create a second, just INCREF the existing one
5128 * and return it. The (single) Python object referring to
5129 * the buffer is stored in "b_python*_ref".
5130 * Question: what to do on a buf_freeall(). We'll probably
5131 * have to either delete the Python object (DECREF it to
5132 * zero - a bad idea, as it leaves dangling refs!) or
5133 * set the buf_T * value to an invalid value (-1?), which
5134 * means we need checks in all access functions... Bah.
5135 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005136 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005137 * b_python_ref and b_python3_ref fields respectively.
5138 */
5139
5140 BufferObject *self;
5141
5142 if (BUF_PYTHON_REF(buf) != NULL)
5143 {
5144 self = BUF_PYTHON_REF(buf);
5145 Py_INCREF(self);
5146 }
5147 else
5148 {
5149 self = PyObject_NEW(BufferObject, &BufferType);
5150 if (self == NULL)
5151 return NULL;
5152 self->buf = buf;
5153 BUF_PYTHON_REF(buf) = self;
5154 }
5155
5156 return (PyObject *)(self);
5157}
5158
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005159 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005160BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005161{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005162 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5163 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005164
5165 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005166}
5167
Bram Moolenaar971db462013-05-12 18:44:48 +02005168 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005169BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005170{
5171 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005172 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005173 return -1; /* ??? */
5174
Bram Moolenaard6e39182013-05-21 18:30:34 +02005175 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005176}
5177
5178 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005179BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005180{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005181 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005182}
5183
5184 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005185BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005186{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005187 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005188}
5189
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005190static char *BufferAttrs[] = {
5191 "name", "number", "vars", "options", "valid",
5192 NULL
5193};
5194
5195 static PyObject *
5196BufferDir(PyObject *self)
5197{
5198 return ObjectDir(self, BufferAttrs);
5199}
5200
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005201 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005202BufferAttrValid(BufferObject *self, char *name)
5203{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005204 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005205
5206 if (strcmp(name, "valid") != 0)
5207 return NULL;
5208
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005209 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5210 Py_INCREF(ret);
5211 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005212}
5213
5214 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005215BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005216{
5217 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005218 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005219 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005220 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005221 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005222 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005223 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005224 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005225 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5226 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005227 else if (strcmp(name, "__members__") == 0)
5228 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005229 else
5230 return NULL;
5231}
5232
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005233 static int
5234BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5235{
5236 if (CheckBuffer(self))
5237 return -1;
5238
5239 if (strcmp(name, "name") == 0)
5240 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005241 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005242 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005243 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005244 PyObject *todecref;
5245
5246 if (!(val = StringToChars(valObject, &todecref)))
5247 return -1;
5248
5249 VimTryStart();
5250 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5251 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005252 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005253 aucmd_restbuf(&aco);
5254 Py_XDECREF(todecref);
5255 if (VimTryEnd())
5256 return -1;
5257
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005258 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005259 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005260 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005261 return -1;
5262 }
5263 return 0;
5264 }
5265 else
5266 {
5267 PyErr_SetString(PyExc_AttributeError, name);
5268 return -1;
5269 }
5270}
5271
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005272 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005273BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005274{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005275 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005276}
5277
5278 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005279BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005280{
5281 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005282 char_u *pmark;
5283 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005284 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005285 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005286
Bram Moolenaard6e39182013-05-21 18:30:34 +02005287 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005288 return NULL;
5289
Bram Moolenaar389a1792013-06-23 13:00:44 +02005290 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005291 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005292
Bram Moolenaar389a1792013-06-23 13:00:44 +02005293 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005294 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005295 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005296 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005297 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005298 return NULL;
5299 }
5300
5301 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005302
5303 Py_XDECREF(todecref);
5304
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005305 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005306 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005307 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005308 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005309 if (VimTryEnd())
5310 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005311
5312 if (posp == NULL)
5313 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005314 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005315 return NULL;
5316 }
5317
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005318 if (posp->lnum <= 0)
5319 {
5320 /* Or raise an error? */
5321 Py_INCREF(Py_None);
5322 return Py_None;
5323 }
5324
5325 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5326}
5327
5328 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005329BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005330{
5331 PyInt start;
5332 PyInt end;
5333
Bram Moolenaard6e39182013-05-21 18:30:34 +02005334 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005335 return NULL;
5336
5337 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5338 return NULL;
5339
Bram Moolenaard6e39182013-05-21 18:30:34 +02005340 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005341}
5342
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005343 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005344BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005345{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005346 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005347 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005348 else
5349 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005350 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005351
5352 if (name == NULL)
5353 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005354
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005355 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005356 }
5357}
5358
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005359static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005360 /* name, function, calling, documentation */
5361 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005362 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005363 {"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 +02005364 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5365 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005366};
5367
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005368/*
5369 * Buffer list object - Implementation
5370 */
5371
5372static PyTypeObject BufMapType;
5373
5374typedef struct
5375{
5376 PyObject_HEAD
5377} BufMapObject;
5378
5379 static PyInt
5380BufMapLength(PyObject *self UNUSED)
5381{
5382 buf_T *b = firstbuf;
5383 PyInt n = 0;
5384
5385 while (b)
5386 {
5387 ++n;
5388 b = b->b_next;
5389 }
5390
5391 return n;
5392}
5393
5394 static PyObject *
5395BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5396{
5397 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005398 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005399
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005400 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005401 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005402
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005403 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005404
5405 if (b)
5406 return BufferNew(b);
5407 else
5408 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005409 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005410 return NULL;
5411 }
5412}
5413
5414 static void
5415BufMapIterDestruct(PyObject *buffer)
5416{
5417 /* Iteration was stopped before all buffers were processed */
5418 if (buffer)
5419 {
5420 Py_DECREF(buffer);
5421 }
5422}
5423
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005424 static int
5425BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5426{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005427 if (buffer)
5428 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005429 return 0;
5430}
5431
5432 static int
5433BufMapIterClear(PyObject **buffer)
5434{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005435 if (*buffer)
5436 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005437 return 0;
5438}
5439
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005440 static PyObject *
5441BufMapIterNext(PyObject **buffer)
5442{
5443 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005444 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005445
5446 if (!*buffer)
5447 return NULL;
5448
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005449 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005450
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005451 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005452 {
5453 *buffer = NULL;
5454 return NULL;
5455 }
5456
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005457 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005458 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005459 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005460 return NULL;
5461 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005462 /* Do not increment reference: we no longer hold it (decref), but whoever
5463 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005464 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005465}
5466
5467 static PyObject *
5468BufMapIter(PyObject *self UNUSED)
5469{
5470 PyObject *buffer;
5471
5472 buffer = BufferNew(firstbuf);
5473 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005474 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5475 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005476}
5477
5478static PyMappingMethods BufMapAsMapping = {
5479 (lenfunc) BufMapLength,
5480 (binaryfunc) BufMapItem,
5481 (objobjargproc) 0,
5482};
5483
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005484/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005485 */
5486
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005487static char *CurrentAttrs[] = {
5488 "buffer", "window", "line", "range", "tabpage",
5489 NULL
5490};
5491
5492 static PyObject *
5493CurrentDir(PyObject *self)
5494{
5495 return ObjectDir(self, CurrentAttrs);
5496}
5497
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005498 static PyObject *
5499CurrentGetattr(PyObject *self UNUSED, char *name)
5500{
5501 if (strcmp(name, "buffer") == 0)
5502 return (PyObject *)BufferNew(curbuf);
5503 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005504 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005505 else if (strcmp(name, "tabpage") == 0)
5506 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005507 else if (strcmp(name, "line") == 0)
5508 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5509 else if (strcmp(name, "range") == 0)
5510 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005511 else if (strcmp(name, "__members__") == 0)
5512 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005513 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005514#if PY_MAJOR_VERSION < 3
5515 return Py_FindMethod(WindowMethods, self, name);
5516#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005517 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005518#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005519}
5520
5521 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005522CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005523{
5524 if (strcmp(name, "line") == 0)
5525 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005526 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5527 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005528 return -1;
5529
5530 return 0;
5531 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005532 else if (strcmp(name, "buffer") == 0)
5533 {
5534 int count;
5535
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005536 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005537 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005538 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005539 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005540 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005541 return -1;
5542 }
5543
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005544 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005545 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005546 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005547
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005548 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005549 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5550 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005551 if (VimTryEnd())
5552 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005553 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005554 return -1;
5555 }
5556
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005557 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005558 }
5559 else if (strcmp(name, "window") == 0)
5560 {
5561 int count;
5562
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005563 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005564 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005565 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005566 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005567 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005568 return -1;
5569 }
5570
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005571 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005572 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005573 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005574
5575 if (!count)
5576 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005577 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005578 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005579 return -1;
5580 }
5581
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005582 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005583 win_goto(((WindowObject *)(valObject))->win);
5584 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005585 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005586 if (VimTryEnd())
5587 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005588 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005589 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005590 return -1;
5591 }
5592
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005593 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005594 }
5595 else if (strcmp(name, "tabpage") == 0)
5596 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005597 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005598 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005599 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005600 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005601 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005602 return -1;
5603 }
5604
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005605 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005606 return -1;
5607
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005608 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005609 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5610 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005611 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005612 if (VimTryEnd())
5613 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005614 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005615 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005616 return -1;
5617 }
5618
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005619 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005620 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005621 else
5622 {
5623 PyErr_SetString(PyExc_AttributeError, name);
5624 return -1;
5625 }
5626}
5627
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005628static struct PyMethodDef CurrentMethods[] = {
5629 /* name, function, calling, documentation */
5630 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5631 { NULL, NULL, 0, NULL}
5632};
5633
Bram Moolenaardb913952012-06-29 12:54:53 +02005634 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005635init_range_cmd(exarg_T *eap)
5636{
5637 RangeStart = eap->line1;
5638 RangeEnd = eap->line2;
5639}
5640
5641 static void
5642init_range_eval(typval_T *rettv UNUSED)
5643{
5644 RangeStart = (PyInt) curwin->w_cursor.lnum;
5645 RangeEnd = RangeStart;
5646}
5647
5648 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005649run_cmd(const char *cmd, void *arg UNUSED
5650#ifdef PY_CAN_RECURSE
5651 , PyGILState_STATE *pygilstate UNUSED
5652#endif
5653 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005654{
Bram Moolenaar41009372013-07-01 22:03:04 +02005655 PyObject *run_ret;
5656 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5657 if (run_ret != NULL)
5658 {
5659 Py_DECREF(run_ret);
5660 }
5661 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5662 {
5663 EMSG2(_(e_py_systemexit), "python");
5664 PyErr_Clear();
5665 }
5666 else
5667 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005668}
5669
5670static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5671static int code_hdr_len = 30;
5672
5673 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005674run_do(const char *cmd, void *arg UNUSED
5675#ifdef PY_CAN_RECURSE
5676 , PyGILState_STATE *pygilstate
5677#endif
5678 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005679{
5680 PyInt lnum;
5681 size_t len;
5682 char *code;
5683 int status;
5684 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005685 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005686 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005687
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005688 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005689 {
5690 EMSG(_("cannot save undo information"));
5691 return;
5692 }
5693
5694 len = code_hdr_len + STRLEN(cmd);
5695 code = PyMem_New(char, len + 1);
5696 memcpy(code, code_hdr, code_hdr_len);
5697 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005698 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5699 status = -1;
5700 if (run_ret != NULL)
5701 {
5702 status = 0;
5703 Py_DECREF(run_ret);
5704 }
5705 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5706 {
5707 PyMem_Free(code);
5708 EMSG2(_(e_py_systemexit), "python");
5709 PyErr_Clear();
5710 return;
5711 }
5712 else
5713 PyErr_PrintEx(1);
5714
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005715 PyMem_Free(code);
5716
5717 if (status)
5718 {
5719 EMSG(_("failed to run the code"));
5720 return;
5721 }
5722
5723 status = 0;
5724 pymain = PyImport_AddModule("__main__");
5725 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005726#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005727 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005728#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005729
5730 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5731 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005732 PyObject *line;
5733 PyObject *linenr;
5734 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005735
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005736#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005737 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005738#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005739 /* Check the line number, the command my have deleted lines. */
5740 if (lnum > curbuf->b_ml.ml_line_count
5741 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005742 goto err;
5743 if (!(linenr = PyInt_FromLong((long) lnum)))
5744 {
5745 Py_DECREF(line);
5746 goto err;
5747 }
5748 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5749 Py_DECREF(line);
5750 Py_DECREF(linenr);
5751 if (!ret)
5752 goto err;
5753
Bram Moolenaara58883b2017-01-29 21:31:09 +01005754 /* Check that the command didn't switch to another buffer. */
5755 if (curbuf != was_curbuf)
5756 {
5757 Py_XDECREF(ret);
5758 goto err;
5759 }
5760
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005761 if (ret != Py_None)
5762 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005763 {
5764 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005765 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005766 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005767
5768 Py_XDECREF(ret);
5769 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005770#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005771 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005772#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005773 }
5774 goto out;
5775err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005776#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005777 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005778#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005779 PyErr_PrintEx(0);
5780 PythonIO_Flush();
5781 status = 1;
5782out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005783#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005784 if (!status)
5785 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005786#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005787 Py_DECREF(pyfunc);
5788 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5789 if (status)
5790 return;
5791 check_cursor();
5792 update_curbuf(NOT_VALID);
5793}
5794
5795 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005796run_eval(const char *cmd, typval_T *rettv
5797#ifdef PY_CAN_RECURSE
5798 , PyGILState_STATE *pygilstate UNUSED
5799#endif
5800 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005801{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005802 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005803
Bram Moolenaar41009372013-07-01 22:03:04 +02005804 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005805 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005806 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005807 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005808 {
5809 EMSG2(_(e_py_systemexit), "python");
5810 PyErr_Clear();
5811 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005812 else
5813 {
5814 if (PyErr_Occurred() && !msg_silent)
5815 PyErr_PrintEx(0);
5816 EMSG(_("E858: Eval did not return a valid python object"));
5817 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005818 }
5819 else
5820 {
Bram Moolenaarde323092017-11-09 19:56:08 +01005821 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005822 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005823 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005824 }
5825 PyErr_Clear();
5826}
5827
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005828 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005829set_ref_in_py(const int copyID)
5830{
5831 pylinkedlist_T *cur;
5832 dict_T *dd;
5833 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005834 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005835 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005836 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005837
5838 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005839 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005840 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005841 {
5842 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5843 if (dd->dv_copyID != copyID)
5844 {
5845 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005846 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005847 }
5848 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005849 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005850
5851 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005852 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005853 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005854 {
5855 ll = ((ListObject *) (cur->pll_obj))->list;
5856 if (ll->lv_copyID != copyID)
5857 {
5858 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005859 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005860 }
5861 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005862 }
5863
Bram Moolenaar8110a092016-04-14 15:56:09 +02005864 if (lastfunc != NULL)
5865 {
5866 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5867 {
5868 func = (FunctionObject *) cur->pll_obj;
5869 if (func->self != NULL && func->self->dv_copyID != copyID)
5870 {
5871 func->self->dv_copyID = copyID;
5872 abort = abort || set_ref_in_ht(
5873 &func->self->dv_hashtab, copyID, NULL);
5874 }
5875 if (func->argc)
5876 for (i = 0; !abort && i < func->argc; ++i)
5877 abort = abort
5878 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5879 }
5880 }
5881
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005882 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005883}
5884
5885 static int
5886set_string_copy(char_u *str, typval_T *tv)
5887{
5888 tv->vval.v_string = vim_strsave(str);
5889 if (tv->vval.v_string == NULL)
5890 {
5891 PyErr_NoMemory();
5892 return -1;
5893 }
5894 return 0;
5895}
5896
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005897 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005898pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005899{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005900 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005901 char_u *key;
5902 dictitem_T *di;
5903 PyObject *keyObject;
5904 PyObject *valObject;
5905 Py_ssize_t iter = 0;
5906
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005907 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005908 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005909
5910 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005911 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005912
5913 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5914 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005915 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005916
Bram Moolenaara03e6312013-05-29 22:49:26 +02005917 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005918 {
5919 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005920 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005921 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005922
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005923 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005924 {
5925 dict_unref(dict);
5926 return -1;
5927 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005928
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005929 if (*key == NUL)
5930 {
5931 dict_unref(dict);
5932 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005933 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005934 return -1;
5935 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005936
5937 di = dictitem_alloc(key);
5938
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005939 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005940
5941 if (di == NULL)
5942 {
5943 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005944 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005945 return -1;
5946 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005947
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005948 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005949 {
5950 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005951 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005952 return -1;
5953 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005954
5955 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005956 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005957 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005958 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005959 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005960 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005961 return -1;
5962 }
5963 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005964
5965 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966 return 0;
5967}
5968
5969 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005970pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005971{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005972 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005973 char_u *key;
5974 dictitem_T *di;
5975 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005976 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977 PyObject *keyObject;
5978 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005979
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005980 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005981 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005982
5983 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005984 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005985
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005986 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005987 {
5988 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005989 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005990 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005991
5992 if (!(iterator = PyObject_GetIter(list)))
5993 {
5994 dict_unref(dict);
5995 Py_DECREF(list);
5996 return -1;
5997 }
5998 Py_DECREF(list);
5999
6000 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006001 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006002 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006003
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006004 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006005 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006006 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006007 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006008 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006009 return -1;
6010 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02006011
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006012 if (*key == NUL)
6013 {
6014 Py_DECREF(keyObject);
6015 Py_DECREF(iterator);
6016 Py_XDECREF(todecref);
6017 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02006018 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006019 return -1;
6020 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006021
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006022 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006023 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006024 Py_DECREF(keyObject);
6025 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006026 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006027 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006028 return -1;
6029 }
6030
6031 di = dictitem_alloc(key);
6032
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006033 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02006034 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006035
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006036 if (di == NULL)
6037 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006038 Py_DECREF(iterator);
6039 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006040 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006041 PyErr_NoMemory();
6042 return -1;
6043 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006044
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006045 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006046 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006047 Py_DECREF(iterator);
6048 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006049 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006050 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006051 return -1;
6052 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02006053
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006054 Py_DECREF(valObject);
6055
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006056 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006057 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006058 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006059 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006060 dictitem_free(di);
6061 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006062 return -1;
6063 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006064 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006065 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006066 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006067 return 0;
6068}
6069
6070 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006071pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006072{
6073 list_T *l;
6074
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006075 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006076 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006077
6078 tv->v_type = VAR_LIST;
6079 tv->vval.v_list = l;
6080
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006081 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006082 {
6083 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006084 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006085 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006086
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006087 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02006088 return 0;
6089}
6090
Bram Moolenaardb913952012-06-29 12:54:53 +02006091typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
6092
6093 static int
6094convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006095 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006096{
6097 PyObject *capsule;
6098 char hexBuf[sizeof(void *) * 2 + 3];
6099
Bram Moolenaar792f0e32018-02-27 17:27:13 +01006100 sprintf(hexBuf, "%p", (void *)obj);
Bram Moolenaardb913952012-06-29 12:54:53 +02006101
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006102# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006103 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006104# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006105 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006106# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006107 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006108 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006109# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006110 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006111# else
6112 capsule = PyCObject_FromVoidPtr(tv, NULL);
6113# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006114 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6115 {
6116 Py_DECREF(capsule);
6117 tv->v_type = VAR_UNKNOWN;
6118 return -1;
6119 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006120
6121 Py_DECREF(capsule);
6122
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006123 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006124 {
6125 tv->v_type = VAR_UNKNOWN;
6126 return -1;
6127 }
6128 /* As we are not using copy_tv which increments reference count we must
6129 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006130 if (tv->v_type == VAR_DICT)
6131 ++tv->vval.v_dict->dv_refcount;
6132 else if (tv->v_type == VAR_LIST)
6133 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006134 }
6135 else
6136 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006137 typval_T *v;
6138
6139# ifdef PY_USE_CAPSULE
6140 v = PyCapsule_GetPointer(capsule, NULL);
6141# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006142 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006143# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006144 copy_tv(v, tv);
6145 }
6146 return 0;
6147}
6148
6149 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006150ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6151{
6152 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006153 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006154
6155 if (!(lookup_dict = PyDict_New()))
6156 return -1;
6157
6158 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6159 {
6160 tv->v_type = VAR_DICT;
6161 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6162 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006163 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006164 }
6165 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006166 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006167 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006168 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006169 else
6170 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006171 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006172 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006173 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006174 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006175 }
6176 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006177 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006178}
6179
6180 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006181ConvertFromPySequence(PyObject *obj, typval_T *tv)
6182{
6183 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006184 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006185
6186 if (!(lookup_dict = PyDict_New()))
6187 return -1;
6188
6189 if (PyType_IsSubtype(obj->ob_type, &ListType))
6190 {
6191 tv->v_type = VAR_LIST;
6192 tv->vval.v_list = (((ListObject *)(obj))->list);
6193 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006194 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006195 }
6196 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006197 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006198 else
6199 {
6200 PyErr_FORMAT(PyExc_TypeError,
6201 N_("unable to convert %s to vim list"),
6202 Py_TYPE_NAME(obj));
6203 ret = -1;
6204 }
6205 Py_DECREF(lookup_dict);
6206 return ret;
6207}
6208
6209 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006210ConvertFromPyObject(PyObject *obj, typval_T *tv)
6211{
6212 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006213 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006214
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006215 if (!(lookup_dict = PyDict_New()))
6216 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006217 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006218 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006219 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006220}
6221
6222 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006223_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006224{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006225 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006226 {
6227 tv->v_type = VAR_DICT;
6228 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6229 ++tv->vval.v_dict->dv_refcount;
6230 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006231 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006232 {
6233 tv->v_type = VAR_LIST;
6234 tv->vval.v_list = (((ListObject *)(obj))->list);
6235 ++tv->vval.v_list->lv_refcount;
6236 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006237 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006238 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006239 FunctionObject *func = (FunctionObject *) obj;
6240 if (func->self != NULL || func->argv != NULL)
6241 {
6242 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6243 set_partial(func, pt, TRUE);
6244 tv->vval.v_partial = pt;
6245 tv->v_type = VAR_PARTIAL;
6246 }
6247 else
6248 {
6249 if (set_string_copy(func->name, tv) == -1)
6250 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006251
Bram Moolenaar8110a092016-04-14 15:56:09 +02006252 tv->v_type = VAR_FUNC;
6253 }
6254 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006255 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006256 else if (PyBytes_Check(obj))
6257 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006258 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006259
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006260 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006261 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006262 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006263 return -1;
6264
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006265 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006266 return -1;
6267
6268 tv->v_type = VAR_STRING;
6269 }
6270 else if (PyUnicode_Check(obj))
6271 {
6272 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006273 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006274
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006275 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006276 if (bytes == NULL)
6277 return -1;
6278
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006279 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006280 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006281 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006282 return -1;
6283
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006284 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006285 {
6286 Py_XDECREF(bytes);
6287 return -1;
6288 }
6289 Py_XDECREF(bytes);
6290
6291 tv->v_type = VAR_STRING;
6292 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006293#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006294 else if (PyInt_Check(obj))
6295 {
6296 tv->v_type = VAR_NUMBER;
6297 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006298 if (PyErr_Occurred())
6299 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006300 }
6301#endif
6302 else if (PyLong_Check(obj))
6303 {
6304 tv->v_type = VAR_NUMBER;
6305 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006306 if (PyErr_Occurred())
6307 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006308 }
6309 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006310 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006311#ifdef FEAT_FLOAT
6312 else if (PyFloat_Check(obj))
6313 {
6314 tv->v_type = VAR_FLOAT;
6315 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6316 }
6317#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006318 else if (PyObject_HasAttrString(obj, "keys"))
6319 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006320 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006321 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006322 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006323 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006324 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006325 else if (PyNumber_Check(obj))
6326 {
6327 PyObject *num;
6328
6329 if (!(num = PyNumber_Long(obj)))
6330 return -1;
6331
6332 tv->v_type = VAR_NUMBER;
6333 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6334
6335 Py_DECREF(num);
6336 }
Bram Moolenaarde323092017-11-09 19:56:08 +01006337 else if (obj == Py_None)
6338 {
6339 tv->v_type = VAR_SPECIAL;
6340 tv->vval.v_number = VVAL_NONE;
6341 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006342 else
6343 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006344 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006345 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006346 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006347 return -1;
6348 }
6349 return 0;
6350}
6351
6352 static PyObject *
6353ConvertToPyObject(typval_T *tv)
6354{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006355 typval_T *argv;
6356 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006357 if (tv == NULL)
6358 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006359 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006360 return NULL;
6361 }
6362 switch (tv->v_type)
6363 {
6364 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006365 return PyBytes_FromString(tv->vval.v_string == NULL
6366 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006367 case VAR_NUMBER:
6368 return PyLong_FromLong((long) tv->vval.v_number);
6369#ifdef FEAT_FLOAT
6370 case VAR_FLOAT:
6371 return PyFloat_FromDouble((double) tv->vval.v_float);
6372#endif
6373 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006374 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006375 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006376 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006377 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006378 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006379 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006380 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006381 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006382 if (tv->vval.v_partial->pt_argc)
6383 {
6384 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6385 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6386 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6387 }
6388 else
6389 argv = NULL;
6390 if (tv->vval.v_partial->pt_dict != NULL)
6391 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006392 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006393 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006394 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006395 tv->vval.v_partial->pt_dict,
6396 tv->vval.v_partial->pt_auto);
Bram Moolenaardb913952012-06-29 12:54:53 +02006397 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006398 case VAR_CHANNEL:
6399 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006400 Py_INCREF(Py_None);
6401 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006402 case VAR_SPECIAL:
6403 switch (tv->vval.v_number)
6404 {
6405 case VVAL_FALSE: return AlwaysFalse(NULL);
6406 case VVAL_TRUE: return AlwaysTrue(NULL);
6407 case VVAL_NONE:
6408 case VVAL_NULL: return AlwaysNone(NULL);
6409 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006410 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006411 return NULL;
6412 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006413 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006414}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006415
6416typedef struct
6417{
6418 PyObject_HEAD
6419} CurrentObject;
6420static PyTypeObject CurrentType;
6421
6422 static void
6423init_structs(void)
6424{
6425 vim_memset(&OutputType, 0, sizeof(OutputType));
6426 OutputType.tp_name = "vim.message";
6427 OutputType.tp_basicsize = sizeof(OutputObject);
6428 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6429 OutputType.tp_doc = "vim message object";
6430 OutputType.tp_methods = OutputMethods;
6431#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006432 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6433 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006434 OutputType.tp_alloc = call_PyType_GenericAlloc;
6435 OutputType.tp_new = call_PyType_GenericNew;
6436 OutputType.tp_free = call_PyObject_Free;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006437 OutputType.tp_base = &PyStdPrinter_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006438#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006439 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6440 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaard4a8c982018-05-15 22:31:18 +02006441 // Disabled, because this causes a crash in test86
6442 // OutputType.tp_base = &PyFile_Type;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006443#endif
6444
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006445 vim_memset(&IterType, 0, sizeof(IterType));
6446 IterType.tp_name = "vim.iter";
6447 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006448 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006449 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006450 IterType.tp_iter = (getiterfunc)IterIter;
6451 IterType.tp_iternext = (iternextfunc)IterNext;
6452 IterType.tp_dealloc = (destructor)IterDestructor;
6453 IterType.tp_traverse = (traverseproc)IterTraverse;
6454 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006455
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006456 vim_memset(&BufferType, 0, sizeof(BufferType));
6457 BufferType.tp_name = "vim.buffer";
6458 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006459 BufferType.tp_dealloc = (destructor)BufferDestructor;
6460 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006461 BufferType.tp_as_sequence = &BufferAsSeq;
6462 BufferType.tp_as_mapping = &BufferAsMapping;
6463 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6464 BufferType.tp_doc = "vim buffer object";
6465 BufferType.tp_methods = BufferMethods;
6466#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006467 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006468 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006469 BufferType.tp_alloc = call_PyType_GenericAlloc;
6470 BufferType.tp_new = call_PyType_GenericNew;
6471 BufferType.tp_free = call_PyObject_Free;
6472#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006473 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006474 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006475#endif
6476
6477 vim_memset(&WindowType, 0, sizeof(WindowType));
6478 WindowType.tp_name = "vim.window";
6479 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006480 WindowType.tp_dealloc = (destructor)WindowDestructor;
6481 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006482 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006483 WindowType.tp_doc = "vim Window object";
6484 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006485 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6486 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006487#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006488 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6489 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006490 WindowType.tp_alloc = call_PyType_GenericAlloc;
6491 WindowType.tp_new = call_PyType_GenericNew;
6492 WindowType.tp_free = call_PyObject_Free;
6493#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006494 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6495 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006496#endif
6497
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006498 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6499 TabPageType.tp_name = "vim.tabpage";
6500 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006501 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6502 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006503 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6504 TabPageType.tp_doc = "vim tab page object";
6505 TabPageType.tp_methods = TabPageMethods;
6506#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006507 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006508 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6509 TabPageType.tp_new = call_PyType_GenericNew;
6510 TabPageType.tp_free = call_PyObject_Free;
6511#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006512 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006513#endif
6514
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006515 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6516 BufMapType.tp_name = "vim.bufferlist";
6517 BufMapType.tp_basicsize = sizeof(BufMapObject);
6518 BufMapType.tp_as_mapping = &BufMapAsMapping;
6519 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006520 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006521 BufferType.tp_doc = "vim buffer list";
6522
6523 vim_memset(&WinListType, 0, sizeof(WinListType));
6524 WinListType.tp_name = "vim.windowlist";
6525 WinListType.tp_basicsize = sizeof(WinListType);
6526 WinListType.tp_as_sequence = &WinListAsSeq;
6527 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6528 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006529 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006530
6531 vim_memset(&TabListType, 0, sizeof(TabListType));
6532 TabListType.tp_name = "vim.tabpagelist";
6533 TabListType.tp_basicsize = sizeof(TabListType);
6534 TabListType.tp_as_sequence = &TabListAsSeq;
6535 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6536 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006537
6538 vim_memset(&RangeType, 0, sizeof(RangeType));
6539 RangeType.tp_name = "vim.range";
6540 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006541 RangeType.tp_dealloc = (destructor)RangeDestructor;
6542 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006543 RangeType.tp_as_sequence = &RangeAsSeq;
6544 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006545 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006546 RangeType.tp_doc = "vim Range object";
6547 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006548 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6549 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006550#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006551 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006552 RangeType.tp_alloc = call_PyType_GenericAlloc;
6553 RangeType.tp_new = call_PyType_GenericNew;
6554 RangeType.tp_free = call_PyObject_Free;
6555#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006556 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006557#endif
6558
6559 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6560 CurrentType.tp_name = "vim.currentdata";
6561 CurrentType.tp_basicsize = sizeof(CurrentObject);
6562 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6563 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006564 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006565#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006566 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6567 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006568#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006569 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6570 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006571#endif
6572
6573 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6574 DictionaryType.tp_name = "vim.dictionary";
6575 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006576 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006577 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006578 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006579 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006580 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6581 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006582 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6583 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6584 DictionaryType.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 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6587 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006588#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006589 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6590 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006591#endif
6592
6593 vim_memset(&ListType, 0, sizeof(ListType));
6594 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006595 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006596 ListType.tp_basicsize = sizeof(ListObject);
6597 ListType.tp_as_sequence = &ListAsSeq;
6598 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006599 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006600 ListType.tp_doc = "list pushing modifications to vim structure";
6601 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006602 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006603 ListType.tp_new = (newfunc)ListConstructor;
6604 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006605#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006606 ListType.tp_getattro = (getattrofunc)ListGetattro;
6607 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006608#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006609 ListType.tp_getattr = (getattrfunc)ListGetattr;
6610 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006611#endif
6612
6613 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006614 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006615 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006616 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6617 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006618 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006619 FunctionType.tp_doc = "object that calls vim function";
6620 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006621 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006622 FunctionType.tp_new = (newfunc)FunctionConstructor;
6623 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006624#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006625 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006626#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006627 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006628#endif
6629
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006630 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6631 OptionsType.tp_name = "vim.options";
6632 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006633 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006634 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006635 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006636 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006637 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006638 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6639 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6640 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006641
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006642#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006643 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6644 LoaderType.tp_name = "vim.Loader";
6645 LoaderType.tp_basicsize = sizeof(LoaderObject);
6646 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6647 LoaderType.tp_doc = "vim message object";
6648 LoaderType.tp_methods = LoaderMethods;
6649 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006650#endif
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006651
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006652#if PY_MAJOR_VERSION >= 3
6653 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6654 vimmodule.m_name = "vim";
6655 vimmodule.m_doc = "Vim Python interface\n";
6656 vimmodule.m_size = -1;
6657 vimmodule.m_methods = VimMethods;
6658#endif
6659}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006660
6661#define PYTYPE_READY(type) \
6662 if (PyType_Ready(&type)) \
6663 return -1;
6664
6665 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006666init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006667{
6668 PYTYPE_READY(IterType);
6669 PYTYPE_READY(BufferType);
6670 PYTYPE_READY(RangeType);
6671 PYTYPE_READY(WindowType);
6672 PYTYPE_READY(TabPageType);
6673 PYTYPE_READY(BufMapType);
6674 PYTYPE_READY(WinListType);
6675 PYTYPE_READY(TabListType);
6676 PYTYPE_READY(CurrentType);
6677 PYTYPE_READY(DictionaryType);
6678 PYTYPE_READY(ListType);
6679 PYTYPE_READY(FunctionType);
6680 PYTYPE_READY(OptionsType);
6681 PYTYPE_READY(OutputType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006682#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006683 PYTYPE_READY(LoaderType);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006684#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006685 return 0;
6686}
6687
6688 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006689init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006690{
6691 PyObject *path;
6692 PyObject *path_hook;
6693 PyObject *path_hooks;
6694
6695 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6696 return -1;
6697
6698 if (!(path_hooks = PySys_GetObject("path_hooks")))
6699 {
6700 PyErr_Clear();
6701 path_hooks = PyList_New(1);
6702 PyList_SET_ITEM(path_hooks, 0, path_hook);
6703 if (PySys_SetObject("path_hooks", path_hooks))
6704 {
6705 Py_DECREF(path_hooks);
6706 return -1;
6707 }
6708 Py_DECREF(path_hooks);
6709 }
6710 else if (PyList_Check(path_hooks))
6711 {
6712 if (PyList_Append(path_hooks, path_hook))
6713 {
6714 Py_DECREF(path_hook);
6715 return -1;
6716 }
6717 Py_DECREF(path_hook);
6718 }
6719 else
6720 {
6721 VimTryStart();
6722 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6723 "You should now do the following:\n"
6724 "- append vim.path_hook to sys.path_hooks\n"
6725 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6726 VimTryEnd(); /* Discard the error */
6727 Py_DECREF(path_hook);
6728 return 0;
6729 }
6730
6731 if (!(path = PySys_GetObject("path")))
6732 {
6733 PyErr_Clear();
6734 path = PyList_New(1);
6735 Py_INCREF(vim_special_path_object);
6736 PyList_SET_ITEM(path, 0, vim_special_path_object);
6737 if (PySys_SetObject("path", path))
6738 {
6739 Py_DECREF(path);
6740 return -1;
6741 }
6742 Py_DECREF(path);
6743 }
6744 else if (PyList_Check(path))
6745 {
6746 if (PyList_Append(path, vim_special_path_object))
6747 return -1;
6748 }
6749 else
6750 {
6751 VimTryStart();
6752 EMSG(_("Failed to set path: sys.path is not a list\n"
6753 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6754 VimTryEnd(); /* Discard the error */
6755 }
6756
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006757 return 0;
6758}
6759
6760static BufMapObject TheBufferMap =
6761{
6762 PyObject_HEAD_INIT(&BufMapType)
6763};
6764
6765static WinListObject TheWindowList =
6766{
6767 PyObject_HEAD_INIT(&WinListType)
6768 NULL
6769};
6770
6771static CurrentObject TheCurrent =
6772{
6773 PyObject_HEAD_INIT(&CurrentType)
6774};
6775
6776static TabListObject TheTabPageList =
6777{
6778 PyObject_HEAD_INIT(&TabListType)
6779};
6780
6781static struct numeric_constant {
6782 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006783 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006784} numeric_constants[] = {
6785 {"VAR_LOCKED", VAR_LOCKED},
6786 {"VAR_FIXED", VAR_FIXED},
6787 {"VAR_SCOPE", VAR_SCOPE},
6788 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6789};
6790
6791static struct object_constant {
6792 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006793 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006794} object_constants[] = {
6795 {"buffers", (PyObject *)(void *)&TheBufferMap},
6796 {"windows", (PyObject *)(void *)&TheWindowList},
6797 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6798 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006799
6800 {"Buffer", (PyObject *)&BufferType},
6801 {"Range", (PyObject *)&RangeType},
6802 {"Window", (PyObject *)&WindowType},
6803 {"TabPage", (PyObject *)&TabPageType},
6804 {"Dictionary", (PyObject *)&DictionaryType},
6805 {"List", (PyObject *)&ListType},
6806 {"Function", (PyObject *)&FunctionType},
6807 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006808#if PY_VERSION_HEX < 0x030700f0
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006809 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006810#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006811};
6812
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006813#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006814 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006815 return -1;
6816
6817#define ADD_CHECKED_OBJECT(m, name, obj) \
6818 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006819 PyObject *valObject = obj; \
6820 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006821 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006822 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006823 }
6824
6825 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006826populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006827{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006828 int i;
6829 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006830 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006831 PyObject *imp;
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006832#if PY_VERSION_HEX >= 0x030700f0
6833 PyObject *dict;
6834 PyObject *cls;
6835#endif
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006836
6837 for (i = 0; i < (int)(sizeof(numeric_constants)
6838 / sizeof(struct numeric_constant));
6839 ++i)
6840 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006841 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006842
6843 for (i = 0; i < (int)(sizeof(object_constants)
6844 / sizeof(struct object_constant));
6845 ++i)
6846 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006847 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006848
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006849 valObject = object_constants[i].valObject;
6850 Py_INCREF(valObject);
6851 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006852 }
6853
6854 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6855 return -1;
6856 ADD_OBJECT(m, "error", VimError);
6857
Bram Moolenaara9922d62013-05-30 13:01:18 +02006858 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6859 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006860 ADD_CHECKED_OBJECT(m, "options",
6861 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006862
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006863 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006864 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006865 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006866
Bram Moolenaar22081f42016-06-01 20:38:34 +02006867#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006868 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006869 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006870#else
6871 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6872 return -1;
6873#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006874 ADD_OBJECT(m, "_getcwd", py_getcwd)
6875
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006876 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006877 return -1;
6878 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006879 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006880 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006881 if (PyObject_SetAttrString(other_module, "chdir", attr))
6882 {
6883 Py_DECREF(attr);
6884 return -1;
6885 }
6886 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006887
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006888 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006889 {
6890 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006891 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006892 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006893 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6894 {
6895 Py_DECREF(attr);
6896 return -1;
6897 }
6898 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006899 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006900 else
6901 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006902
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006903 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6904 return -1;
6905
6906 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6907
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006908#if PY_VERSION_HEX >= 0x030700f0
6909 if (!(imp = PyImport_ImportModule("importlib.machinery")))
6910 return -1;
6911
6912 dict = PyModule_GetDict(imp);
6913
6914 if (!(cls = PyDict_GetItemString(dict, "PathFinder")))
6915 {
6916 Py_DECREF(imp);
6917 return -1;
6918 }
6919
6920 if (!(py_find_spec = PyObject_GetAttrString(cls, "find_spec")))
6921 {
6922 Py_DECREF(imp);
6923 return -1;
6924 }
6925
6926 Py_DECREF(imp);
6927
6928 ADD_OBJECT(m, "_find_spec", py_find_spec);
6929#else
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006930 if (!(imp = PyImport_ImportModule("imp")))
6931 return -1;
6932
6933 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6934 {
6935 Py_DECREF(imp);
6936 return -1;
6937 }
6938
6939 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6940 {
6941 Py_DECREF(py_find_module);
6942 Py_DECREF(imp);
6943 return -1;
6944 }
6945
6946 Py_DECREF(imp);
6947
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006948 ADD_OBJECT(m, "_find_module", py_find_module);
6949 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaar79a494d2018-07-22 04:30:21 +02006950#endif
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006951
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006952 return 0;
6953}