blob: e717646d0544e84131c732655c6c3c56844fc8e7 [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaar78cf3f02014-01-10 18:16:07 +010016#ifdef __BORLANDC__
17/* Disable Warning W8060: Possibly incorrect assignment in function ... */
18# pragma warn -8060
19#endif
20
Bram Moolenaar41009372013-07-01 22:03:04 +020021static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
22
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020023#if PY_VERSION_HEX < 0x02050000
24typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
25#endif
26
Bram Moolenaar91805fc2011-06-26 04:01:44 +020027#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020028# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020029#else
30# define ENC_OPT "latin1"
31#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020032#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020033
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020034static const char *vim_special_path = "_vim_path_";
35
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020036#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020038#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010039#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
40#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
41#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020042
43#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
44 ? "(NULL)" \
45 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020046
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020047#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 N_("empty keys are not allowed"))
49#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
50#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
51#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
52#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
53#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
54#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020055#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020056 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020057#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020058 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020059 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020061#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
62#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020063#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020065typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020066typedef void (*runner)(const char *, void *
67#ifdef PY_CAN_RECURSE
68 , PyGILState_STATE *
69#endif
70 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020071
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020072static int ConvertFromPyObject(PyObject *, typval_T *);
73static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020074static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +020075static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020076static PyObject *WindowNew(win_T *, tabpage_T *);
77static PyObject *BufferNew (buf_T *);
78static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020079
80static PyInt RangeStart;
81static PyInt RangeEnd;
82
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020083static PyObject *globals;
84
Bram Moolenaarf4258302013-06-02 18:20:17 +020085static PyObject *py_chdir;
86static PyObject *py_fchdir;
87static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020088static PyObject *vim_module;
89static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020090
Bram Moolenaar81c40c52013-06-12 14:41:04 +020091static PyObject *py_find_module;
92static PyObject *py_load_module;
93
94static PyObject *VimError;
95
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020096/*
97 * obtain a lock on the Vim data structures
98 */
99 static void
100Python_Lock_Vim(void)
101{
102}
103
104/*
105 * release a lock on the Vim data structures
106 */
107 static void
108Python_Release_Vim(void)
109{
110}
111
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200112/*
113 * The "todecref" argument holds a pointer to PyObject * that must be
114 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
115 * was needed to generate returned value is object.
116 *
117 * Use Py_XDECREF to decrement reference count.
118 */
119 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200120StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200121{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200122 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200123
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200124 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125 {
126
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200127 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
128 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200129 return NULL;
130
131 *todecref = NULL;
132 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200133 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200134 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200135 PyObject *bytes;
136
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200137 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200138 return NULL;
139
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200140 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
141 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200142 {
143 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200144 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200145 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200146
147 *todecref = bytes;
148 }
149 else
150 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200151#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200152 PyErr_FORMAT(PyExc_TypeError,
153 N_("expected str() or unicode() instance, but got %s"),
154 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200155#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200156 PyErr_FORMAT(PyExc_TypeError,
157 N_("expected bytes() or str() instance, but got %s"),
158 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200159#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200160 return NULL;
161 }
162
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200163 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200164}
165
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200166#define NUMBER_LONG 1
167#define NUMBER_INT 2
168#define NUMBER_NATURAL 4
169#define NUMBER_UNSIGNED 8
170
171 static int
172NumberToLong(PyObject *obj, long *result, int flags)
173{
174#if PY_MAJOR_VERSION < 3
175 if (PyInt_Check(obj))
176 {
177 *result = PyInt_AsLong(obj);
178 if (PyErr_Occurred())
179 return -1;
180 }
181 else
182#endif
183 if (PyLong_Check(obj))
184 {
185 *result = PyLong_AsLong(obj);
186 if (PyErr_Occurred())
187 return -1;
188 }
189 else if (PyNumber_Check(obj))
190 {
191 PyObject *num;
192
193 if (!(num = PyNumber_Long(obj)))
194 return -1;
195
196 *result = PyLong_AsLong(num);
197
198 Py_DECREF(num);
199
200 if (PyErr_Occurred())
201 return -1;
202 }
203 else
204 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200205#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200206 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200207 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200208 "coercing to long(), but got %s"),
209 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200210#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200211 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200212 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200213 "but got %s"),
214 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200215#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200216 return -1;
217 }
218
219 if (flags & NUMBER_INT)
220 {
221 if (*result > INT_MAX)
222 {
223 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200224 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200225 return -1;
226 }
227 else if (*result < INT_MIN)
228 {
229 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200230 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200231 return -1;
232 }
233 }
234
235 if (flags & NUMBER_NATURAL)
236 {
237 if (*result <= 0)
238 {
239 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100240 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200241 return -1;
242 }
243 }
244 else if (flags & NUMBER_UNSIGNED)
245 {
246 if (*result < 0)
247 {
248 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200249 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200250 return -1;
251 }
252 }
253
254 return 0;
255}
256
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200257 static int
258add_string(PyObject *list, char *s)
259{
260 PyObject *string;
261
262 if (!(string = PyString_FromString(s)))
263 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200264
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200265 if (PyList_Append(list, string))
266 {
267 Py_DECREF(string);
268 return -1;
269 }
270
271 Py_DECREF(string);
272 return 0;
273}
274
275 static PyObject *
276ObjectDir(PyObject *self, char **attributes)
277{
278 PyMethodDef *method;
279 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200280 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200281
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200282 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200283 return NULL;
284
285 if (self)
286 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200287 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200288 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200289 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200290 return NULL;
291 }
292
293 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200294 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200295 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200296 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200297 return NULL;
298 }
299
300#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200301 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200302 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200303 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200304 return NULL;
305 }
306#endif
307
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200308 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200309}
310
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200311/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200312 */
313
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200314/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200315typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200316
317static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200318
319typedef struct
320{
321 PyObject_HEAD
322 long softspace;
323 long error;
324} OutputObject;
325
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200326static char *OutputAttrs[] = {
327 "softspace",
328 NULL
329};
330
331 static PyObject *
332OutputDir(PyObject *self)
333{
334 return ObjectDir(self, OutputAttrs);
335}
336
Bram Moolenaar77045652012-09-21 13:46:06 +0200337 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200338OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200339{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200340 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200341 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200342 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200343 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200344 return -1;
345 }
346
347 if (strcmp(name, "softspace") == 0)
348 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200349 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200351 return 0;
352 }
353
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200354 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200355 return -1;
356}
357
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200358/* Buffer IO, we write one whole line at a time. */
359static garray_T io_ga = {0, 0, 1, 80, NULL};
360static writefn old_fn = NULL;
361
362 static void
363PythonIO_Flush(void)
364{
365 if (old_fn != NULL && io_ga.ga_len > 0)
366 {
367 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
368 old_fn((char_u *)io_ga.ga_data);
369 }
370 io_ga.ga_len = 0;
371}
372
373 static void
374writer(writefn fn, char_u *str, PyInt n)
375{
376 char_u *ptr;
377
378 /* Flush when switching output function. */
379 if (fn != old_fn)
380 PythonIO_Flush();
381 old_fn = fn;
382
383 /* Write each NL separated line. Text after the last NL is kept for
384 * writing later. */
385 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
386 {
387 PyInt len = ptr - str;
388
389 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
390 break;
391
392 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
393 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
394 fn((char_u *)io_ga.ga_data);
395 str = ptr + 1;
396 n -= len + 1;
397 io_ga.ga_len = 0;
398 }
399
400 /* Put the remaining text into io_ga for later printing. */
401 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
402 {
403 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
404 io_ga.ga_len += (int)n;
405 }
406}
407
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200408 static int
409write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200411 Py_ssize_t len = 0;
412 char *str = NULL;
413 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200414
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200415 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
416 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417
418 Py_BEGIN_ALLOW_THREADS
419 Python_Lock_Vim();
420 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
421 Python_Release_Vim();
422 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200423 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200424
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200425 return 0;
426}
427
428 static PyObject *
429OutputWrite(OutputObject *self, PyObject *string)
430{
431 if (write_output(self, string))
432 return NULL;
433
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200434 Py_INCREF(Py_None);
435 return Py_None;
436}
437
438 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200439OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200441 PyObject *iterator;
442 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200444 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200447 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200448 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200449 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200450 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200451 Py_DECREF(iterator);
452 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200453 return NULL;
454 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200455 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200456 }
457
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200458 Py_DECREF(iterator);
459
460 /* Iterator may have finished due to an exception */
461 if (PyErr_Occurred())
462 return NULL;
463
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200464 Py_INCREF(Py_None);
465 return Py_None;
466}
467
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100468 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100469AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100470{
471 /* do nothing */
472 Py_INCREF(Py_None);
473 return Py_None;
474}
475
Bram Moolenaard4247472015-11-02 13:28:59 +0100476 static PyObject *
477AlwaysFalse(PyObject *self UNUSED)
478{
479 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100480 PyObject *ret = Py_False;
481 Py_INCREF(ret);
482 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100483}
484
485 static PyObject *
486AlwaysTrue(PyObject *self UNUSED)
487{
488 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100489 PyObject *ret = Py_True;
490 Py_INCREF(ret);
491 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100492}
493
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200494/***************/
495
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200496static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200497 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200498 {"write", (PyCFunction)OutputWrite, METH_O, ""},
499 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100500 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
501 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
502 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
503 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
504 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
505 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaar6d4431e2016-04-21 20:00:56 +0200506 {"closed", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200507 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200508 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200509};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200510
511static OutputObject Output =
512{
513 PyObject_HEAD_INIT(&OutputType)
514 0,
515 0
516};
517
518static OutputObject Error =
519{
520 PyObject_HEAD_INIT(&OutputType)
521 0,
522 1
523};
524
525 static int
526PythonIO_Init_io(void)
527{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200528 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
529 return -1;
530 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
531 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200532
533 if (PyErr_Occurred())
534 {
535 EMSG(_("E264: Python: Error initialising I/O objects"));
536 return -1;
537 }
538
539 return 0;
540}
541
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200542typedef struct
543{
544 PyObject_HEAD
545 PyObject *module;
546} LoaderObject;
547static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200548
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200549 static void
550LoaderDestructor(LoaderObject *self)
551{
552 Py_DECREF(self->module);
553 DESTRUCTOR_FINISH(self);
554}
555
556 static PyObject *
557LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
558{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200559 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200560
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200561 Py_INCREF(ret);
562 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200563}
564
565static struct PyMethodDef LoaderMethods[] = {
566 /* name, function, calling, doc */
567 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
568 { NULL, NULL, 0, NULL}
569};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200570
571/* Check to see whether a Vim error has been reported, or a keyboard
572 * interrupt has been detected.
573 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200574
575 static void
576VimTryStart(void)
577{
578 ++trylevel;
579}
580
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200581 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200582VimTryEnd(void)
583{
584 --trylevel;
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100585 /* Without this it stops processing all subsequent Vim script commands and
586 * generates strange error messages if I e.g. try calling Test() in a cycle
587 */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200588 did_emsg = FALSE;
589 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200590 if (got_int)
591 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100592 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100593 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100594 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200595 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200596 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200597 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100598 else if (msg_list != NULL && *msg_list != NULL)
599 {
600 int should_free;
601 char_u *msg;
602
603 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
604
605 if (msg == NULL)
606 {
607 PyErr_NoMemory();
608 return -1;
609 }
610
611 PyErr_SetVim((char *) msg);
612
613 free_global_msglist();
614
615 if (should_free)
616 vim_free(msg);
617
618 return -1;
619 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200620 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200621 return (PyErr_Occurred() ? -1 : 0);
622 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200623 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200624 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100625 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200626 return -1;
627 }
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100628 /* Finally transform Vim script exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200629 else
630 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200631 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200632 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200633 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200634 }
635}
636
637 static int
638VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200639{
640 if (got_int)
641 {
642 PyErr_SetNone(PyExc_KeyboardInterrupt);
643 return 1;
644 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200645 return 0;
646}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200647
648/* Vim module - Implementation
649 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200650
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200651 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200652VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200653{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200654 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200655 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200656 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200657
Bram Moolenaar389a1792013-06-23 13:00:44 +0200658 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200659 return NULL;
660
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200661 Py_BEGIN_ALLOW_THREADS
662 Python_Lock_Vim();
663
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200664 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200665 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200666 update_screen(VALID);
667
668 Python_Release_Vim();
669 Py_END_ALLOW_THREADS
670
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200671 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200672 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200673 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200674 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200675
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200676 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200677 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200678 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200679}
680
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200681/*
682 * Function to translate a typval_T into a PyObject; this will recursively
683 * translate lists/dictionaries into their Python equivalents.
684 *
685 * The depth parameter is to avoid infinite recursion, set it to 1 when
686 * you call VimToPython.
687 */
688 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200689VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200690{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200691 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200692 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200693 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200694
695 /* Avoid infinite recursion */
696 if (depth > 100)
697 {
698 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200699 ret = Py_None;
700 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200701 }
702
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200703 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200704 * then and we can use it again. */
705 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
706 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
707 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200708 sprintf(ptrBuf, "%p",
709 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
710 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200711
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200713 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200714 Py_INCREF(ret);
715 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200716 }
717 }
718
719 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200720 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200721 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200722 else if (our_tv->v_type == VAR_NUMBER)
723 {
724 char buf[NUMBUFLEN];
725
726 /* For backwards compatibility numbers are stored as strings. */
727 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200728 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200729 }
730# ifdef FEAT_FLOAT
731 else if (our_tv->v_type == VAR_FLOAT)
732 {
733 char buf[NUMBUFLEN];
734
735 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200736 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200737 }
738# endif
739 else if (our_tv->v_type == VAR_LIST)
740 {
741 list_T *list = our_tv->vval.v_list;
742 listitem_T *curr;
743
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200744 if (list == NULL)
745 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200746
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200747 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200748 return NULL;
749
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200750 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200751 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200752 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200753 return NULL;
754 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200755
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200756 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
757 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200758 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200759 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200760 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200761 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200762 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200763 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200764 {
765 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200766 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200767 return NULL;
768 }
769 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200770 }
771 }
772 else if (our_tv->v_type == VAR_DICT)
773 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200774
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100775 hashtab_T *ht;
776 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200777 hashitem_T *hi;
778 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100779
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200780 if (our_tv->vval.v_dict == NULL)
781 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100782 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200783
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200784 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200785 return NULL;
786
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200787 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200788 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200789 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200790 return NULL;
791 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200792
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100793 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200794 for (hi = ht->ht_array; todo > 0; ++hi)
795 {
796 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200797 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200798 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200799
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200800 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200801 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200802 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200803 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200804 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200806 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200807 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200808 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200809 Py_DECREF(newObj);
810 return NULL;
811 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200812 }
813 }
814 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100815 else if (our_tv->v_type == VAR_SPECIAL)
816 {
817 if (our_tv->vval.v_number == VVAL_FALSE)
818 {
819 ret = Py_False;
820 Py_INCREF(ret);
821 }
822 else if (our_tv->vval.v_number == VVAL_TRUE)
823 {
824 ret = Py_True;
825 Py_INCREF(ret);
826 }
827 else
828 {
829 Py_INCREF(Py_None);
830 ret = Py_None;
831 }
832 return ret;
833 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200834 else
835 {
836 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200837 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200838 }
839
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200840 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200841}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200842
843 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200844VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200845{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200846 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200847 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200848 PyObject *string;
849 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200850 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200851 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200852
Bram Moolenaar389a1792013-06-23 13:00:44 +0200853 if (!PyArg_ParseTuple(args, "O", &string))
854 return NULL;
855
856 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200857 return NULL;
858
859 Py_BEGIN_ALLOW_THREADS
860 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200861 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200862 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200863 Python_Release_Vim();
864 Py_END_ALLOW_THREADS
865
Bram Moolenaar389a1792013-06-23 13:00:44 +0200866 Py_XDECREF(todecref);
867
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200868 if (VimTryEnd())
869 return NULL;
870
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200871 if (our_tv == NULL)
872 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200873 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200874 return NULL;
875 }
876
877 /* Convert the Vim type into a Python type. Create a dictionary that's
878 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200879 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200880 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200881 else
882 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200883 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200884 Py_DECREF(lookup_dict);
885 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200886
887
888 Py_BEGIN_ALLOW_THREADS
889 Python_Lock_Vim();
890 free_tv(our_tv);
891 Python_Release_Vim();
892 Py_END_ALLOW_THREADS
893
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200894 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200895}
896
Bram Moolenaardb913952012-06-29 12:54:53 +0200897static PyObject *ConvertToPyObject(typval_T *);
898
899 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200900VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200901{
Bram Moolenaardb913952012-06-29 12:54:53 +0200902 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200903 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200904 char_u *expr;
905 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200906
Bram Moolenaar389a1792013-06-23 13:00:44 +0200907 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200908 return NULL;
909
910 Py_BEGIN_ALLOW_THREADS
911 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200912 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200913 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200914 Python_Release_Vim();
915 Py_END_ALLOW_THREADS
916
Bram Moolenaar389a1792013-06-23 13:00:44 +0200917 Py_XDECREF(todecref);
918
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200919 if (VimTryEnd())
920 return NULL;
921
Bram Moolenaardb913952012-06-29 12:54:53 +0200922 if (our_tv == NULL)
923 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200924 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200925 return NULL;
926 }
927
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200928 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200929 Py_BEGIN_ALLOW_THREADS
930 Python_Lock_Vim();
931 free_tv(our_tv);
932 Python_Release_Vim();
933 Py_END_ALLOW_THREADS
934
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200935 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200936}
937
938 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200939VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200940{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200941 char_u *str;
942 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200943 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200944
Bram Moolenaar389a1792013-06-23 13:00:44 +0200945 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200946 return NULL;
947
Bram Moolenaara54bf402012-12-05 16:30:07 +0100948#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200949 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100950#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200951 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100952#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200953
954 Py_XDECREF(todecref);
955
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200956 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200957}
958
Bram Moolenaarf4258302013-06-02 18:20:17 +0200959 static PyObject *
960_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
961{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200962 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200963 PyObject *newwd;
964 PyObject *todecref;
965 char_u *new_dir;
966
Bram Moolenaard4209d22013-06-05 20:34:15 +0200967 if (_chdir == NULL)
968 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200969 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200970 return NULL;
971
972 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
973 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200974 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200975 return NULL;
976 }
977
978 if (!(new_dir = StringToChars(newwd, &todecref)))
979 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200980 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200981 Py_DECREF(newwd);
982 return NULL;
983 }
984
985 VimTryStart();
986
987 if (vim_chdir(new_dir))
988 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200989 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200990 Py_DECREF(newwd);
991 Py_XDECREF(todecref);
992
993 if (VimTryEnd())
994 return NULL;
995
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200996 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200997 return NULL;
998 }
999
1000 Py_DECREF(newwd);
1001 Py_XDECREF(todecref);
1002
1003 post_chdir(FALSE);
1004
1005 if (VimTryEnd())
1006 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001007 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001008 return NULL;
1009 }
1010
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001011 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001012}
1013
1014 static PyObject *
1015VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1016{
1017 return _VimChdir(py_chdir, args, kwargs);
1018}
1019
1020 static PyObject *
1021VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1022{
1023 return _VimChdir(py_fchdir, args, kwargs);
1024}
1025
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001026typedef struct {
1027 PyObject *callable;
1028 PyObject *result;
1029} map_rtp_data;
1030
1031 static void
1032map_rtp_callback(char_u *path, void *_data)
1033{
1034 void **data = (void **) _data;
1035 PyObject *pathObject;
1036 map_rtp_data *mr_data = *((map_rtp_data **) data);
1037
Bram Moolenaar41009372013-07-01 22:03:04 +02001038 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001039 {
1040 *data = NULL;
1041 return;
1042 }
1043
1044 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1045 pathObject, NULL);
1046
1047 Py_DECREF(pathObject);
1048
1049 if (!mr_data->result || mr_data->result != Py_None)
1050 *data = NULL;
1051 else
1052 {
1053 Py_DECREF(mr_data->result);
1054 mr_data->result = NULL;
1055 }
1056}
1057
1058 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001059VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001060{
1061 map_rtp_data data;
1062
Bram Moolenaar389a1792013-06-23 13:00:44 +02001063 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001064 data.result = NULL;
1065
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001066 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001067
1068 if (data.result == NULL)
1069 {
1070 if (PyErr_Occurred())
1071 return NULL;
1072 else
1073 {
1074 Py_INCREF(Py_None);
1075 return Py_None;
1076 }
1077 }
1078 return data.result;
1079}
1080
1081/*
1082 * _vim_runtimepath_ special path implementation.
1083 */
1084
1085 static void
1086map_finder_callback(char_u *path, void *_data)
1087{
1088 void **data = (void **) _data;
1089 PyObject *list = *((PyObject **) data);
1090 PyObject *pathObject1, *pathObject2;
1091 char *pathbuf;
1092 size_t pathlen;
1093
1094 pathlen = STRLEN(path);
1095
1096#if PY_MAJOR_VERSION < 3
1097# define PY_MAIN_DIR_STRING "python2"
1098#else
1099# define PY_MAIN_DIR_STRING "python3"
1100#endif
1101#define PY_ALTERNATE_DIR_STRING "pythonx"
1102
1103#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1104 if (!(pathbuf = PyMem_New(char,
1105 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1106 {
1107 PyErr_NoMemory();
1108 *data = NULL;
1109 return;
1110 }
1111
1112 mch_memmove(pathbuf, path, pathlen + 1);
1113 add_pathsep((char_u *) pathbuf);
1114
1115 pathlen = STRLEN(pathbuf);
1116 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1117 PYTHONX_STRING_LENGTH + 1);
1118
1119 if (!(pathObject1 = PyString_FromString(pathbuf)))
1120 {
1121 *data = NULL;
1122 PyMem_Free(pathbuf);
1123 return;
1124 }
1125
1126 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1127 PYTHONX_STRING_LENGTH + 1);
1128
1129 if (!(pathObject2 = PyString_FromString(pathbuf)))
1130 {
1131 Py_DECREF(pathObject1);
1132 PyMem_Free(pathbuf);
1133 *data = NULL;
1134 return;
1135 }
1136
1137 PyMem_Free(pathbuf);
1138
1139 if (PyList_Append(list, pathObject1)
1140 || PyList_Append(list, pathObject2))
1141 *data = NULL;
1142
1143 Py_DECREF(pathObject1);
1144 Py_DECREF(pathObject2);
1145}
1146
1147 static PyObject *
1148Vim_GetPaths(PyObject *self UNUSED)
1149{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001150 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001151
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001152 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001153 return NULL;
1154
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001155 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001156
1157 if (PyErr_Occurred())
1158 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001159 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001160 return NULL;
1161 }
1162
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001163 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001164}
1165
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001166 static PyObject *
1167call_load_module(char *name, int len, PyObject *find_module_result)
1168{
1169 PyObject *fd, *pathname, *description;
1170
Bram Moolenaarc476e522013-06-23 13:46:40 +02001171 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001172 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001173 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001174 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001175 Py_TYPE_NAME(find_module_result));
1176 return NULL;
1177 }
1178 if (PyTuple_GET_SIZE(find_module_result) != 3)
1179 {
1180 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001181 N_("expected 3-tuple as imp.find_module() result, but got "
1182 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001183 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001184 return NULL;
1185 }
1186
1187 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1188 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1189 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1190 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001191 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001192 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001193 return NULL;
1194 }
1195
1196 return PyObject_CallFunction(py_load_module,
1197 "s#OOO", name, len, fd, pathname, description);
1198}
1199
1200 static PyObject *
1201find_module(char *fullname, char *tail, PyObject *new_path)
1202{
1203 PyObject *find_module_result;
1204 PyObject *module;
1205 char *dot;
1206
Bram Moolenaar41009372013-07-01 22:03:04 +02001207 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001208 {
1209 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001210 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001211 * first component
1212 */
1213 PyObject *newest_path;
1214 int partlen = (int) (dot - 1 - tail);
1215
1216 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1217 "s#O", tail, partlen, new_path)))
1218 return NULL;
1219
1220 if (!(module = call_load_module(
1221 fullname,
1222 ((int) (tail - fullname)) + partlen,
1223 find_module_result)))
1224 {
1225 Py_DECREF(find_module_result);
1226 return NULL;
1227 }
1228
1229 Py_DECREF(find_module_result);
1230
1231 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1232 {
1233 Py_DECREF(module);
1234 return NULL;
1235 }
1236
1237 Py_DECREF(module);
1238
1239 module = find_module(fullname, dot + 1, newest_path);
1240
1241 Py_DECREF(newest_path);
1242
1243 return module;
1244 }
1245 else
1246 {
1247 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1248 "sO", tail, new_path)))
1249 return NULL;
1250
1251 if (!(module = call_load_module(
1252 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001253 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001254 find_module_result)))
1255 {
1256 Py_DECREF(find_module_result);
1257 return NULL;
1258 }
1259
1260 Py_DECREF(find_module_result);
1261
1262 return module;
1263 }
1264}
1265
1266 static PyObject *
1267FinderFindModule(PyObject *self, PyObject *args)
1268{
1269 char *fullname;
1270 PyObject *module;
1271 PyObject *new_path;
1272 LoaderObject *loader;
1273
1274 if (!PyArg_ParseTuple(args, "s", &fullname))
1275 return NULL;
1276
1277 if (!(new_path = Vim_GetPaths(self)))
1278 return NULL;
1279
1280 module = find_module(fullname, fullname, new_path);
1281
1282 Py_DECREF(new_path);
1283
1284 if (!module)
1285 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001286 if (PyErr_Occurred())
1287 {
1288 if (PyErr_ExceptionMatches(PyExc_ImportError))
1289 PyErr_Clear();
1290 else
1291 return NULL;
1292 }
1293
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001294 Py_INCREF(Py_None);
1295 return Py_None;
1296 }
1297
1298 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1299 {
1300 Py_DECREF(module);
1301 return NULL;
1302 }
1303
1304 loader->module = module;
1305
1306 return (PyObject *) loader;
1307}
1308
1309 static PyObject *
1310VimPathHook(PyObject *self UNUSED, PyObject *args)
1311{
1312 char *path;
1313
1314 if (PyArg_ParseTuple(args, "s", &path)
1315 && STRCMP(path, vim_special_path) == 0)
1316 {
1317 Py_INCREF(vim_module);
1318 return vim_module;
1319 }
1320
1321 PyErr_Clear();
1322 PyErr_SetNone(PyExc_ImportError);
1323 return NULL;
1324}
1325
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001326/*
1327 * Vim module - Definitions
1328 */
1329
1330static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001331 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001332 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001333 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001334 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1335 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001336 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1337 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001338 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001339 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001340 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1341 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1342 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001343};
1344
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001345/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001346 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001347 */
1348
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001349static PyTypeObject IterType;
1350
1351typedef PyObject *(*nextfun)(void **);
1352typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001353typedef int (*traversefun)(void *, visitproc, void *);
1354typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001355
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001356/* Main purpose of this object is removing the need for do python
1357 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1358 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001359
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001360typedef struct
1361{
1362 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001363 void *cur;
1364 nextfun next;
1365 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001366 traversefun traverse;
1367 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001368} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001369
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001370 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001371IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1372 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001373{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001374 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001375
Bram Moolenaar774267b2013-05-21 20:51:59 +02001376 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001377 self->cur = start;
1378 self->next = next;
1379 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001380 self->traverse = traverse;
1381 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001382
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001383 return (PyObject *)(self);
1384}
1385
1386 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001387IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001388{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001389 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001390 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001391 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001392}
1393
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001394 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001395IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001396{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001397 if (self->traverse != NULL)
1398 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001399 else
1400 return 0;
1401}
1402
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001403/* Mac OSX defines clear() somewhere. */
1404#ifdef clear
1405# undef clear
1406#endif
1407
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001408 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001409IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001410{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001411 if (self->clear != NULL)
1412 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001413 else
1414 return 0;
1415}
1416
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001417 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001418IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001419{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001420 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001421}
1422
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001423 static PyObject *
1424IterIter(PyObject *self)
1425{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001426 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001427 return self;
1428}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001429
Bram Moolenaardb913952012-06-29 12:54:53 +02001430typedef struct pylinkedlist_S {
1431 struct pylinkedlist_S *pll_next;
1432 struct pylinkedlist_S *pll_prev;
1433 PyObject *pll_obj;
1434} pylinkedlist_T;
1435
1436static pylinkedlist_T *lastdict = NULL;
1437static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001438static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001439
1440 static void
1441pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1442{
1443 if (ref->pll_prev == NULL)
1444 {
1445 if (ref->pll_next == NULL)
1446 {
1447 *last = NULL;
1448 return;
1449 }
1450 }
1451 else
1452 ref->pll_prev->pll_next = ref->pll_next;
1453
1454 if (ref->pll_next == NULL)
1455 *last = ref->pll_prev;
1456 else
1457 ref->pll_next->pll_prev = ref->pll_prev;
1458}
1459
1460 static void
1461pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1462{
1463 if (*last == NULL)
1464 ref->pll_prev = NULL;
1465 else
1466 {
1467 (*last)->pll_next = ref;
1468 ref->pll_prev = *last;
1469 }
1470 ref->pll_next = NULL;
1471 ref->pll_obj = self;
1472 *last = ref;
1473}
1474
1475static PyTypeObject DictionaryType;
1476
1477typedef struct
1478{
1479 PyObject_HEAD
1480 dict_T *dict;
1481 pylinkedlist_T ref;
1482} DictionaryObject;
1483
Bram Moolenaara9922d62013-05-30 13:01:18 +02001484static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1485
1486#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1487
Bram Moolenaardb913952012-06-29 12:54:53 +02001488 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001489DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001490{
1491 DictionaryObject *self;
1492
Bram Moolenaara9922d62013-05-30 13:01:18 +02001493 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001494 if (self == NULL)
1495 return NULL;
1496 self->dict = dict;
1497 ++dict->dv_refcount;
1498
1499 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1500
1501 return (PyObject *)(self);
1502}
1503
Bram Moolenaara9922d62013-05-30 13:01:18 +02001504 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001505py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001506{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001507 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001508
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001509 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001510 {
1511 PyErr_NoMemory();
1512 return NULL;
1513 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001514 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001515
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001516 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001517}
1518
1519 static PyObject *
1520DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1521{
1522 DictionaryObject *self;
1523 dict_T *dict;
1524
1525 if (!(dict = py_dict_alloc()))
1526 return NULL;
1527
1528 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1529
1530 --dict->dv_refcount;
1531
1532 if (kwargs || PyTuple_Size(args))
1533 {
1534 PyObject *tmp;
1535 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1536 {
1537 Py_DECREF(self);
1538 return NULL;
1539 }
1540
1541 Py_DECREF(tmp);
1542 }
1543
1544 return (PyObject *)(self);
1545}
1546
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001547 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001548DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001549{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001550 pyll_remove(&self->ref, &lastdict);
1551 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001552
1553 DESTRUCTOR_FINISH(self);
1554}
1555
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001556static char *DictionaryAttrs[] = {
1557 "locked", "scope",
1558 NULL
1559};
1560
1561 static PyObject *
1562DictionaryDir(PyObject *self)
1563{
1564 return ObjectDir(self, DictionaryAttrs);
1565}
1566
Bram Moolenaardb913952012-06-29 12:54:53 +02001567 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001568DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001569{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001570 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001571 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001572 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001573 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001574 return -1;
1575 }
1576
1577 if (strcmp(name, "locked") == 0)
1578 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001579 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001580 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001581 PyErr_SET_STRING(PyExc_TypeError,
1582 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001583 return -1;
1584 }
1585 else
1586 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001587 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001588 if (istrue == -1)
1589 return -1;
1590 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001591 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001592 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001593 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001594 }
1595 return 0;
1596 }
1597 else
1598 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001599 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001600 return -1;
1601 }
1602}
1603
1604 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001605DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001606{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001607 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001608}
1609
Bram Moolenaara9922d62013-05-30 13:01:18 +02001610#define DICT_FLAG_HAS_DEFAULT 0x01
1611#define DICT_FLAG_POP 0x02
1612#define DICT_FLAG_NONE_DEFAULT 0x04
1613#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1614#define DICT_FLAG_RETURN_PAIR 0x10
1615
Bram Moolenaardb913952012-06-29 12:54:53 +02001616 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001617_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001618{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001619 PyObject *keyObject;
1620 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001621 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001622 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001623 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001624 dict_T *dict = self->dict;
1625 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001626 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001627
Bram Moolenaara9922d62013-05-30 13:01:18 +02001628 if (flags & DICT_FLAG_HAS_DEFAULT)
1629 {
1630 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1631 return NULL;
1632 }
1633 else
1634 keyObject = args;
1635
1636 if (flags & DICT_FLAG_RETURN_BOOL)
1637 defObject = Py_False;
1638
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001639 if (!(key = StringToChars(keyObject, &todecref)))
1640 return NULL;
1641
1642 if (*key == NUL)
1643 {
1644 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001645 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001646 return NULL;
1647 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001648
Bram Moolenaara9922d62013-05-30 13:01:18 +02001649 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001650
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001651 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001652
Bram Moolenaara9922d62013-05-30 13:01:18 +02001653 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001654 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001655 if (defObject)
1656 {
1657 Py_INCREF(defObject);
1658 return defObject;
1659 }
1660 else
1661 {
1662 PyErr_SetObject(PyExc_KeyError, keyObject);
1663 return NULL;
1664 }
1665 }
1666 else if (flags & DICT_FLAG_RETURN_BOOL)
1667 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001668 ret = Py_True;
1669 Py_INCREF(ret);
1670 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001671 }
1672
1673 di = dict_lookup(hi);
1674
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001675 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001676 return NULL;
1677
1678 if (flags & DICT_FLAG_POP)
1679 {
1680 if (dict->dv_lock)
1681 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001682 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001683 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001684 return NULL;
1685 }
1686
1687 hash_remove(&dict->dv_hashtab, hi);
1688 dictitem_free(di);
1689 }
1690
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001691 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001692}
1693
1694 static PyObject *
1695DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1696{
1697 return _DictionaryItem(self, keyObject, 0);
1698}
1699
1700 static int
1701DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1702{
1703 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001704 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001705
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001706 if (rObj == NULL)
1707 return -1;
1708
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001709 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001710
Bram Moolenaardee2e312013-06-23 16:35:47 +02001711 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001712
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001713 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001714}
1715
1716typedef struct
1717{
1718 hashitem_T *ht_array;
1719 long_u ht_used;
1720 hashtab_T *ht;
1721 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001722 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001723} dictiterinfo_T;
1724
1725 static PyObject *
1726DictionaryIterNext(dictiterinfo_T **dii)
1727{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001728 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001729
1730 if (!(*dii)->todo)
1731 return NULL;
1732
1733 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1734 (*dii)->ht->ht_used != (*dii)->ht_used)
1735 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001736 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001737 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001738 return NULL;
1739 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001740
Bram Moolenaara9922d62013-05-30 13:01:18 +02001741 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1742 ++((*dii)->hi);
1743
1744 --((*dii)->todo);
1745
Bram Moolenaar41009372013-07-01 22:03:04 +02001746 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001747 return NULL;
1748
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001749 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001750}
1751
1752 static PyObject *
1753DictionaryIter(DictionaryObject *self)
1754{
1755 dictiterinfo_T *dii;
1756 hashtab_T *ht;
1757
1758 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1759 {
1760 PyErr_NoMemory();
1761 return NULL;
1762 }
1763
1764 ht = &self->dict->dv_hashtab;
1765 dii->ht_array = ht->ht_array;
1766 dii->ht_used = ht->ht_used;
1767 dii->ht = ht;
1768 dii->hi = dii->ht_array;
1769 dii->todo = dii->ht_used;
1770
1771 return IterNew(dii,
1772 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1773 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001774}
1775
1776 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001777DictionaryAssItem(
1778 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001779{
1780 char_u *key;
1781 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001782 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001783 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001784 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001785
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001786 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001787 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001788 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001789 return -1;
1790 }
1791
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001792 if (!(key = StringToChars(keyObject, &todecref)))
1793 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001794
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001795 if (*key == NUL)
1796 {
1797 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001798 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001799 return -1;
1800 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001801
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001802 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001803
1804 if (valObject == NULL)
1805 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001806 hashitem_T *hi;
1807
Bram Moolenaardb913952012-06-29 12:54:53 +02001808 if (di == NULL)
1809 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001810 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001811 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001812 return -1;
1813 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001814 hi = hash_find(&dict->dv_hashtab, di->di_key);
1815 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001816 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001817 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001818 return 0;
1819 }
1820
1821 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001822 {
1823 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001824 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001825 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001826
1827 if (di == NULL)
1828 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001829 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001830 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001831 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001832 PyErr_NoMemory();
1833 return -1;
1834 }
1835 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001836 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001837
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001838 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001839 {
1840 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001841 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001842 RAISE_KEY_ADD_FAIL(key);
1843 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001844 return -1;
1845 }
1846 }
1847 else
1848 clear_tv(&di->di_tv);
1849
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001850 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001851
1852 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001853 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001854 return 0;
1855}
1856
Bram Moolenaara9922d62013-05-30 13:01:18 +02001857typedef PyObject *(*hi_to_py)(hashitem_T *);
1858
Bram Moolenaardb913952012-06-29 12:54:53 +02001859 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001860DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001861{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001862 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001863 long_u todo = dict->dv_hashtab.ht_used;
1864 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001865 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001866 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001867 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001868
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001869 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001870 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1871 {
1872 if (!HASHITEM_EMPTY(hi))
1873 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001874 if (!(newObj = hiconvert(hi)))
1875 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001876 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001877 return NULL;
1878 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001879 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001880 --todo;
1881 ++i;
1882 }
1883 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001884 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001885}
1886
Bram Moolenaara9922d62013-05-30 13:01:18 +02001887 static PyObject *
1888dict_key(hashitem_T *hi)
1889{
1890 return PyBytes_FromString((char *)(hi->hi_key));
1891}
1892
1893 static PyObject *
1894DictionaryListKeys(DictionaryObject *self)
1895{
1896 return DictionaryListObjects(self, dict_key);
1897}
1898
1899 static PyObject *
1900dict_val(hashitem_T *hi)
1901{
1902 dictitem_T *di;
1903
1904 di = dict_lookup(hi);
1905 return ConvertToPyObject(&di->di_tv);
1906}
1907
1908 static PyObject *
1909DictionaryListValues(DictionaryObject *self)
1910{
1911 return DictionaryListObjects(self, dict_val);
1912}
1913
1914 static PyObject *
1915dict_item(hashitem_T *hi)
1916{
1917 PyObject *keyObject;
1918 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001919 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001920
1921 if (!(keyObject = dict_key(hi)))
1922 return NULL;
1923
1924 if (!(valObject = dict_val(hi)))
1925 {
1926 Py_DECREF(keyObject);
1927 return NULL;
1928 }
1929
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001930 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001931
1932 Py_DECREF(keyObject);
1933 Py_DECREF(valObject);
1934
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001935 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001936}
1937
1938 static PyObject *
1939DictionaryListItems(DictionaryObject *self)
1940{
1941 return DictionaryListObjects(self, dict_item);
1942}
1943
1944 static PyObject *
1945DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1946{
1947 dict_T *dict = self->dict;
1948
1949 if (dict->dv_lock)
1950 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001951 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001952 return NULL;
1953 }
1954
1955 if (kwargs)
1956 {
1957 typval_T tv;
1958
1959 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1960 return NULL;
1961
1962 VimTryStart();
1963 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1964 clear_tv(&tv);
1965 if (VimTryEnd())
1966 return NULL;
1967 }
1968 else
1969 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001970 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001971
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001972 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001973 return NULL;
1974
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001975 if (obj == NULL)
1976 {
1977 Py_INCREF(Py_None);
1978 return Py_None;
1979 }
1980
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001981 if (PyObject_HasAttrString(obj, "keys"))
1982 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001983 else
1984 {
1985 PyObject *iterator;
1986 PyObject *item;
1987
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001988 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001989 return NULL;
1990
1991 while ((item = PyIter_Next(iterator)))
1992 {
1993 PyObject *fast;
1994 PyObject *keyObject;
1995 PyObject *valObject;
1996 PyObject *todecref;
1997 char_u *key;
1998 dictitem_T *di;
1999
2000 if (!(fast = PySequence_Fast(item, "")))
2001 {
2002 Py_DECREF(iterator);
2003 Py_DECREF(item);
2004 return NULL;
2005 }
2006
2007 Py_DECREF(item);
2008
2009 if (PySequence_Fast_GET_SIZE(fast) != 2)
2010 {
2011 Py_DECREF(iterator);
2012 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002013 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002014 N_("expected sequence element of size 2, "
2015 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002016 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002017 return NULL;
2018 }
2019
2020 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2021
2022 if (!(key = StringToChars(keyObject, &todecref)))
2023 {
2024 Py_DECREF(iterator);
2025 Py_DECREF(fast);
2026 return NULL;
2027 }
2028
2029 di = dictitem_alloc(key);
2030
2031 Py_XDECREF(todecref);
2032
2033 if (di == NULL)
2034 {
2035 Py_DECREF(fast);
2036 Py_DECREF(iterator);
2037 PyErr_NoMemory();
2038 return NULL;
2039 }
2040 di->di_tv.v_lock = 0;
2041 di->di_tv.v_type = VAR_UNKNOWN;
2042
2043 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2044
2045 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2046 {
2047 Py_DECREF(iterator);
2048 Py_DECREF(fast);
2049 dictitem_free(di);
2050 return NULL;
2051 }
2052
2053 Py_DECREF(fast);
2054
2055 if (dict_add(dict, di) == FAIL)
2056 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002057 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002058 Py_DECREF(iterator);
2059 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002060 return NULL;
2061 }
2062 }
2063
2064 Py_DECREF(iterator);
2065
2066 /* Iterator may have finished due to an exception */
2067 if (PyErr_Occurred())
2068 return NULL;
2069 }
2070 }
2071 Py_INCREF(Py_None);
2072 return Py_None;
2073}
2074
2075 static PyObject *
2076DictionaryGet(DictionaryObject *self, PyObject *args)
2077{
2078 return _DictionaryItem(self, args,
2079 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2080}
2081
2082 static PyObject *
2083DictionaryPop(DictionaryObject *self, PyObject *args)
2084{
2085 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2086}
2087
2088 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002089DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002090{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002091 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002092 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002093 PyObject *valObject;
2094 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002095
Bram Moolenaarde71b562013-06-02 17:41:54 +02002096 if (self->dict->dv_hashtab.ht_used == 0)
2097 {
2098 PyErr_SetNone(PyExc_KeyError);
2099 return NULL;
2100 }
2101
2102 hi = self->dict->dv_hashtab.ht_array;
2103 while (HASHITEM_EMPTY(hi))
2104 ++hi;
2105
2106 di = dict_lookup(hi);
2107
2108 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002109 return NULL;
2110
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002111 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002112 {
2113 Py_DECREF(valObject);
2114 return NULL;
2115 }
2116
2117 hash_remove(&self->dict->dv_hashtab, hi);
2118 dictitem_free(di);
2119
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002120 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002121}
2122
2123 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002124DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002125{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002126 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2127}
2128
2129static PySequenceMethods DictionaryAsSeq = {
2130 0, /* sq_length */
2131 0, /* sq_concat */
2132 0, /* sq_repeat */
2133 0, /* sq_item */
2134 0, /* sq_slice */
2135 0, /* sq_ass_item */
2136 0, /* sq_ass_slice */
2137 (objobjproc) DictionaryContains, /* sq_contains */
2138 0, /* sq_inplace_concat */
2139 0, /* sq_inplace_repeat */
2140};
2141
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002142static PyMappingMethods DictionaryAsMapping = {
2143 (lenfunc) DictionaryLength,
2144 (binaryfunc) DictionaryItem,
2145 (objobjargproc) DictionaryAssItem,
2146};
2147
Bram Moolenaardb913952012-06-29 12:54:53 +02002148static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002149 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002150 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2151 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2152 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2153 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2154 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002155 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002156 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002157 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2158 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002159};
2160
2161static PyTypeObject ListType;
2162
2163typedef struct
2164{
2165 PyObject_HEAD
2166 list_T *list;
2167 pylinkedlist_T ref;
2168} ListObject;
2169
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002170#define NEW_LIST(list) ListNew(&ListType, list)
2171
Bram Moolenaardb913952012-06-29 12:54:53 +02002172 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002173ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002174{
2175 ListObject *self;
2176
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002177 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002178 if (self == NULL)
2179 return NULL;
2180 self->list = list;
2181 ++list->lv_refcount;
2182
2183 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2184
2185 return (PyObject *)(self);
2186}
2187
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002188 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002189py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002190{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002191 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002192
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002193 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002194 {
2195 PyErr_NoMemory();
2196 return NULL;
2197 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002198 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002199
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002200 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002201}
2202
2203 static int
2204list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2205{
2206 PyObject *iterator;
2207 PyObject *item;
2208 listitem_T *li;
2209
2210 if (!(iterator = PyObject_GetIter(obj)))
2211 return -1;
2212
2213 while ((item = PyIter_Next(iterator)))
2214 {
2215 if (!(li = listitem_alloc()))
2216 {
2217 PyErr_NoMemory();
2218 Py_DECREF(item);
2219 Py_DECREF(iterator);
2220 return -1;
2221 }
2222 li->li_tv.v_lock = 0;
2223 li->li_tv.v_type = VAR_UNKNOWN;
2224
2225 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2226 {
2227 Py_DECREF(item);
2228 Py_DECREF(iterator);
2229 listitem_free(li);
2230 return -1;
2231 }
2232
2233 Py_DECREF(item);
2234
2235 list_append(l, li);
2236 }
2237
2238 Py_DECREF(iterator);
2239
2240 /* Iterator may have finished due to an exception */
2241 if (PyErr_Occurred())
2242 return -1;
2243
2244 return 0;
2245}
2246
2247 static PyObject *
2248ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2249{
2250 list_T *list;
2251 PyObject *obj = NULL;
2252
2253 if (kwargs)
2254 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002255 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002256 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002257 return NULL;
2258 }
2259
2260 if (!PyArg_ParseTuple(args, "|O", &obj))
2261 return NULL;
2262
2263 if (!(list = py_list_alloc()))
2264 return NULL;
2265
2266 if (obj)
2267 {
2268 PyObject *lookup_dict;
2269
2270 if (!(lookup_dict = PyDict_New()))
2271 {
2272 list_unref(list);
2273 return NULL;
2274 }
2275
2276 if (list_py_concat(list, obj, lookup_dict) == -1)
2277 {
2278 Py_DECREF(lookup_dict);
2279 list_unref(list);
2280 return NULL;
2281 }
2282
2283 Py_DECREF(lookup_dict);
2284 }
2285
2286 return ListNew(subtype, list);
2287}
2288
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002289 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002290ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002291{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002292 pyll_remove(&self->ref, &lastlist);
2293 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002294
2295 DESTRUCTOR_FINISH(self);
2296}
2297
Bram Moolenaardb913952012-06-29 12:54:53 +02002298 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002299ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002300{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002301 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002302}
2303
2304 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002305ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002306{
2307 listitem_T *li;
2308
Bram Moolenaard6e39182013-05-21 18:30:34 +02002309 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002310 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002311 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002312 return NULL;
2313 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002314 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002315 if (li == NULL)
2316 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002317 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002318 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002319 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002320 return NULL;
2321 }
2322 return ConvertToPyObject(&li->li_tv);
2323}
2324
Bram Moolenaardb913952012-06-29 12:54:53 +02002325 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002326ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2327 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002328{
2329 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002330 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002331
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002332 if (step == 0)
2333 {
2334 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2335 return NULL;
2336 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002337
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002338 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002339 if (list == NULL)
2340 return NULL;
2341
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002342 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002343 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002344 PyObject *item;
2345
2346 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002347 if (item == NULL)
2348 {
2349 Py_DECREF(list);
2350 return NULL;
2351 }
2352
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002353 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002354 }
2355
2356 return list;
2357}
2358
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002359 static PyObject *
2360ListItem(ListObject *self, PyObject* idx)
2361{
2362#if PY_MAJOR_VERSION < 3
2363 if (PyInt_Check(idx))
2364 {
2365 long _idx = PyInt_AsLong(idx);
2366 return ListIndex(self, _idx);
2367 }
2368 else
2369#endif
2370 if (PyLong_Check(idx))
2371 {
2372 long _idx = PyLong_AsLong(idx);
2373 return ListIndex(self, _idx);
2374 }
2375 else if (PySlice_Check(idx))
2376 {
2377 Py_ssize_t start, stop, step, slicelen;
2378
Bram Moolenaar922a4662014-03-30 16:11:43 +02002379 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002380 &start, &stop, &step, &slicelen) < 0)
2381 return NULL;
2382 return ListSlice(self, start, step, slicelen);
2383 }
2384 else
2385 {
2386 RAISE_INVALID_INDEX_TYPE(idx);
2387 return NULL;
2388 }
2389}
2390
2391 static void
2392list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2393 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2394{
2395 while (numreplaced--)
2396 {
2397 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2398 listitem_remove(l, lis[slicelen + numreplaced]);
2399 }
2400 while (numadded--)
2401 {
2402 listitem_T *next;
2403
2404 next = lastaddedli->li_prev;
2405 listitem_remove(l, lastaddedli);
2406 lastaddedli = next;
2407 }
2408}
2409
2410 static int
2411ListAssSlice(ListObject *self, Py_ssize_t first,
2412 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2413{
2414 PyObject *iterator;
2415 PyObject *item;
2416 listitem_T *li;
2417 listitem_T *lastaddedli = NULL;
2418 listitem_T *next;
2419 typval_T v;
2420 list_T *l = self->list;
2421 PyInt i;
2422 PyInt j;
2423 PyInt numreplaced = 0;
2424 PyInt numadded = 0;
2425 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002426 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002427
2428 size = ListLength(self);
2429
2430 if (l->lv_lock)
2431 {
2432 RAISE_LOCKED_LIST;
2433 return -1;
2434 }
2435
2436 if (step == 0)
2437 {
2438 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2439 return -1;
2440 }
2441
2442 if (step != 1 && slicelen == 0)
2443 {
2444 /* Nothing to do. Only error out if obj has some items. */
2445 int ret = 0;
2446
2447 if (obj == NULL)
2448 return 0;
2449
2450 if (!(iterator = PyObject_GetIter(obj)))
2451 return -1;
2452
2453 if ((item = PyIter_Next(iterator)))
2454 {
2455 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002456 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002457 "to extended slice"), 0);
2458 Py_DECREF(item);
2459 ret = -1;
2460 }
2461 Py_DECREF(iterator);
2462 return ret;
2463 }
2464
2465 if (obj != NULL)
2466 /* XXX May allocate zero bytes. */
2467 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2468 {
2469 PyErr_NoMemory();
2470 return -1;
2471 }
2472
2473 if (first == size)
2474 li = NULL;
2475 else
2476 {
2477 li = list_find(l, (long) first);
2478 if (li == NULL)
2479 {
2480 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2481 (int)first);
2482 if (obj != NULL)
2483 PyMem_Free(lis);
2484 return -1;
2485 }
2486 i = slicelen;
2487 while (i-- && li != NULL)
2488 {
2489 j = step;
2490 next = li;
2491 if (step > 0)
2492 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2493 else
2494 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2495
2496 if (obj == NULL)
2497 listitem_remove(l, li);
2498 else
2499 lis[slicelen - i - 1] = li;
2500
2501 li = next;
2502 }
2503 if (li == NULL && i != -1)
2504 {
2505 PyErr_SET_VIM(N_("internal error: not enough list items"));
2506 if (obj != NULL)
2507 PyMem_Free(lis);
2508 return -1;
2509 }
2510 }
2511
2512 if (obj == NULL)
2513 return 0;
2514
2515 if (!(iterator = PyObject_GetIter(obj)))
2516 {
2517 PyMem_Free(lis);
2518 return -1;
2519 }
2520
2521 i = 0;
2522 while ((item = PyIter_Next(iterator)))
2523 {
2524 if (ConvertFromPyObject(item, &v) == -1)
2525 {
2526 Py_DECREF(iterator);
2527 Py_DECREF(item);
2528 PyMem_Free(lis);
2529 return -1;
2530 }
2531 Py_DECREF(item);
2532 if (list_insert_tv(l, &v, numreplaced < slicelen
2533 ? lis[numreplaced]
2534 : li) == FAIL)
2535 {
2536 clear_tv(&v);
2537 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2538 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2539 PyMem_Free(lis);
2540 return -1;
2541 }
2542 if (numreplaced < slicelen)
2543 {
2544 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002545 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002546 numreplaced++;
2547 }
2548 else
2549 {
2550 if (li)
2551 lastaddedli = li->li_prev;
2552 else
2553 lastaddedli = l->lv_last;
2554 numadded++;
2555 }
2556 clear_tv(&v);
2557 if (step != 1 && i >= slicelen)
2558 {
2559 Py_DECREF(iterator);
2560 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002561 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002562 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002563 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2564 PyMem_Free(lis);
2565 return -1;
2566 }
2567 ++i;
2568 }
2569 Py_DECREF(iterator);
2570
2571 if (step != 1 && i != slicelen)
2572 {
2573 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002574 N_("attempt to assign sequence of size %d to extended slice "
2575 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002576 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2577 PyMem_Free(lis);
2578 return -1;
2579 }
2580
2581 if (PyErr_Occurred())
2582 {
2583 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2584 PyMem_Free(lis);
2585 return -1;
2586 }
2587
2588 for (i = 0; i < numreplaced; i++)
2589 listitem_free(lis[i]);
2590 if (step == 1)
2591 for (i = numreplaced; i < slicelen; i++)
2592 listitem_remove(l, lis[i]);
2593
2594 PyMem_Free(lis);
2595
2596 return 0;
2597}
2598
2599 static int
2600ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2601{
2602 typval_T tv;
2603 list_T *l = self->list;
2604 listitem_T *li;
2605 Py_ssize_t length = ListLength(self);
2606
2607 if (l->lv_lock)
2608 {
2609 RAISE_LOCKED_LIST;
2610 return -1;
2611 }
2612 if (index > length || (index == length && obj == NULL))
2613 {
2614 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2615 return -1;
2616 }
2617
2618 if (obj == NULL)
2619 {
2620 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002621 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002622 clear_tv(&li->li_tv);
2623 vim_free(li);
2624 return 0;
2625 }
2626
2627 if (ConvertFromPyObject(obj, &tv) == -1)
2628 return -1;
2629
2630 if (index == length)
2631 {
2632 if (list_append_tv(l, &tv) == FAIL)
2633 {
2634 clear_tv(&tv);
2635 PyErr_SET_VIM(N_("failed to add item to list"));
2636 return -1;
2637 }
2638 }
2639 else
2640 {
2641 li = list_find(l, (long) index);
2642 clear_tv(&li->li_tv);
2643 copy_tv(&tv, &li->li_tv);
2644 clear_tv(&tv);
2645 }
2646 return 0;
2647}
2648
2649 static Py_ssize_t
2650ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2651{
2652#if PY_MAJOR_VERSION < 3
2653 if (PyInt_Check(idx))
2654 {
2655 long _idx = PyInt_AsLong(idx);
2656 return ListAssIndex(self, _idx, obj);
2657 }
2658 else
2659#endif
2660 if (PyLong_Check(idx))
2661 {
2662 long _idx = PyLong_AsLong(idx);
2663 return ListAssIndex(self, _idx, obj);
2664 }
2665 else if (PySlice_Check(idx))
2666 {
2667 Py_ssize_t start, stop, step, slicelen;
2668
Bram Moolenaar922a4662014-03-30 16:11:43 +02002669 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002670 &start, &stop, &step, &slicelen) < 0)
2671 return -1;
2672 return ListAssSlice(self, start, step, slicelen,
2673 obj);
2674 }
2675 else
2676 {
2677 RAISE_INVALID_INDEX_TYPE(idx);
2678 return -1;
2679 }
2680}
2681
2682 static PyObject *
2683ListConcatInPlace(ListObject *self, PyObject *obj)
2684{
2685 list_T *l = self->list;
2686 PyObject *lookup_dict;
2687
2688 if (l->lv_lock)
2689 {
2690 RAISE_LOCKED_LIST;
2691 return NULL;
2692 }
2693
2694 if (!(lookup_dict = PyDict_New()))
2695 return NULL;
2696
2697 if (list_py_concat(l, obj, lookup_dict) == -1)
2698 {
2699 Py_DECREF(lookup_dict);
2700 return NULL;
2701 }
2702 Py_DECREF(lookup_dict);
2703
2704 Py_INCREF(self);
2705 return (PyObject *)(self);
2706}
2707
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002708typedef struct
2709{
2710 listwatch_T lw;
2711 list_T *list;
2712} listiterinfo_T;
2713
2714 static void
2715ListIterDestruct(listiterinfo_T *lii)
2716{
2717 list_rem_watch(lii->list, &lii->lw);
2718 PyMem_Free(lii);
2719}
2720
2721 static PyObject *
2722ListIterNext(listiterinfo_T **lii)
2723{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002724 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002725
2726 if (!((*lii)->lw.lw_item))
2727 return NULL;
2728
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002729 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002730 return NULL;
2731
2732 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2733
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002734 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002735}
2736
2737 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002738ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002739{
2740 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002741 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002742
2743 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2744 {
2745 PyErr_NoMemory();
2746 return NULL;
2747 }
2748
2749 list_add_watch(l, &lii->lw);
2750 lii->lw.lw_item = l->lv_first;
2751 lii->list = l;
2752
2753 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002754 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2755 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002756}
2757
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002758static char *ListAttrs[] = {
2759 "locked",
2760 NULL
2761};
2762
2763 static PyObject *
2764ListDir(PyObject *self)
2765{
2766 return ObjectDir(self, ListAttrs);
2767}
2768
Bram Moolenaar66b79852012-09-21 14:00:35 +02002769 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002770ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002771{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002772 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002773 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002774 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002775 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002776 return -1;
2777 }
2778
2779 if (strcmp(name, "locked") == 0)
2780 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002781 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002782 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002783 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002784 return -1;
2785 }
2786 else
2787 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002788 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002789 if (istrue == -1)
2790 return -1;
2791 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002792 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002793 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002794 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002795 }
2796 return 0;
2797 }
2798 else
2799 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002800 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002801 return -1;
2802 }
2803}
2804
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002805static PySequenceMethods ListAsSeq = {
2806 (lenfunc) ListLength, /* sq_length, len(x) */
2807 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2808 0, /* RangeRepeat, sq_repeat, x*n */
2809 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2810 0, /* was_sq_slice, x[i:j] */
2811 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2812 0, /* was_sq_ass_slice, x[i:j]=v */
2813 0, /* sq_contains */
2814 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2815 0, /* sq_inplace_repeat */
2816};
2817
2818static PyMappingMethods ListAsMapping = {
2819 /* mp_length */ (lenfunc) ListLength,
2820 /* mp_subscript */ (binaryfunc) ListItem,
2821 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2822};
2823
Bram Moolenaardb913952012-06-29 12:54:53 +02002824static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002825 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2826 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2827 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002828};
2829
2830typedef struct
2831{
2832 PyObject_HEAD
2833 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002834 int argc;
2835 typval_T *argv;
2836 dict_T *self;
2837 pylinkedlist_T ref;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002838 int auto_rebind;
Bram Moolenaardb913952012-06-29 12:54:53 +02002839} FunctionObject;
2840
2841static PyTypeObject FunctionType;
2842
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002843#define NEW_FUNCTION(name, argc, argv, self, pt_auto) \
2844 FunctionNew(&FunctionType, (name), (argc), (argv), (self), (pt_auto))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002845
Bram Moolenaardb913952012-06-29 12:54:53 +02002846 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002847FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002848 dict_T *selfdict, int auto_rebind)
Bram Moolenaardb913952012-06-29 12:54:53 +02002849{
2850 FunctionObject *self;
2851
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002852 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2853
Bram Moolenaardb913952012-06-29 12:54:53 +02002854 if (self == NULL)
2855 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002856
2857 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002858 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002859 if (!translated_function_exists(name))
2860 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002861 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002862 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002863 return NULL;
2864 }
2865 self->name = vim_strsave(name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002866 }
2867 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002868 if ((self->name = get_expanded_name(name,
2869 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2870 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002871 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002872 PyErr_FORMAT(PyExc_ValueError,
2873 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002874 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002875 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002876
Bram Moolenaar2d3d60a2016-08-01 16:27:23 +02002877 func_ref(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002878 self->argc = argc;
2879 self->argv = argv;
2880 self->self = selfdict;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002881 self->auto_rebind = selfdict == NULL ? TRUE : auto_rebind;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002882
2883 if (self->argv || self->self)
2884 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2885
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002886 return (PyObject *)(self);
2887}
2888
2889 static PyObject *
2890FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2891{
2892 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002893 PyObject *selfdictObject;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002894 PyObject *autoRebindObject;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002895 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002896 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002897 typval_T selfdicttv;
2898 typval_T argstv;
2899 list_T *argslist = NULL;
2900 dict_T *selfdict = NULL;
2901 int argc = 0;
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002902 int auto_rebind = TRUE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002903 typval_T *argv = NULL;
2904 typval_T *curtv;
2905 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002906
Bram Moolenaar8110a092016-04-14 15:56:09 +02002907 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002908 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02002909 selfdictObject = PyDict_GetItemString(kwargs, "self");
2910 if (selfdictObject != NULL)
2911 {
2912 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
2913 return NULL;
2914 selfdict = selfdicttv.vval.v_dict;
2915 }
2916 argsObject = PyDict_GetItemString(kwargs, "args");
2917 if (argsObject != NULL)
2918 {
2919 if (ConvertFromPySequence(argsObject, &argstv) == -1)
2920 {
2921 dict_unref(selfdict);
2922 return NULL;
2923 }
2924 argslist = argstv.vval.v_list;
2925
2926 argc = argslist->lv_len;
2927 if (argc != 0)
2928 {
2929 argv = PyMem_New(typval_T, (size_t) argc);
Bram Moolenaarfe4b1862016-04-15 21:47:54 +02002930 if (argv == NULL)
2931 {
2932 PyErr_NoMemory();
2933 dict_unref(selfdict);
2934 list_unref(argslist);
2935 return NULL;
2936 }
Bram Moolenaar8110a092016-04-14 15:56:09 +02002937 curtv = argv;
2938 for (li = argslist->lv_first; li != NULL; li = li->li_next)
2939 copy_tv(&li->li_tv, curtv++);
2940 }
2941 list_unref(argslist);
2942 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002943 if (selfdict != NULL)
2944 {
2945 auto_rebind = FALSE;
2946 autoRebindObject = PyDict_GetItemString(kwargs, "auto_rebind");
2947 if (autoRebindObject != NULL)
2948 {
2949 auto_rebind = PyObject_IsTrue(autoRebindObject);
2950 if (auto_rebind == -1)
2951 {
2952 dict_unref(selfdict);
2953 list_unref(argslist);
2954 return NULL;
2955 }
2956 }
2957 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002958 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002959
Bram Moolenaar389a1792013-06-23 13:00:44 +02002960 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02002961 {
2962 dict_unref(selfdict);
2963 while (argc--)
2964 clear_tv(&argv[argc]);
2965 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002966 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002967 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002968
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002969 self = FunctionNew(subtype, name, argc, argv, selfdict, auto_rebind);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002970
Bram Moolenaar389a1792013-06-23 13:00:44 +02002971 PyMem_Free(name);
2972
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002973 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002974}
2975
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002976 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002977FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002978{
Bram Moolenaar8110a092016-04-14 15:56:09 +02002979 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002980 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002981 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002982 for (i = 0; i < self->argc; ++i)
2983 clear_tv(&self->argv[i]);
2984 PyMem_Free(self->argv);
2985 dict_unref(self->self);
2986 if (self->argv || self->self)
2987 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002988
2989 DESTRUCTOR_FINISH(self);
2990}
2991
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002992static char *FunctionAttrs[] = {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02002993 "softspace", "args", "self", "auto_rebind",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002994 NULL
2995};
2996
2997 static PyObject *
2998FunctionDir(PyObject *self)
2999{
3000 return ObjectDir(self, FunctionAttrs);
3001}
3002
Bram Moolenaardb913952012-06-29 12:54:53 +02003003 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02003004FunctionAttr(FunctionObject *self, char *name)
3005{
3006 list_T *list;
3007 int i;
3008 if (strcmp(name, "name") == 0)
3009 return PyString_FromString((char *)(self->name));
3010 else if (strcmp(name, "args") == 0)
3011 {
Bram Moolenaar9f289532016-08-26 16:39:03 +02003012 if (self->argv == NULL || (list = list_alloc()) == NULL)
Bram Moolenaar8110a092016-04-14 15:56:09 +02003013 return AlwaysNone(NULL);
Bram Moolenaar9f289532016-08-26 16:39:03 +02003014
Bram Moolenaar8110a092016-04-14 15:56:09 +02003015 for (i = 0; i < self->argc; ++i)
3016 list_append_tv(list, &self->argv[i]);
3017 return NEW_LIST(list);
3018 }
3019 else if (strcmp(name, "self") == 0)
3020 return self->self == NULL
3021 ? AlwaysNone(NULL)
3022 : NEW_DICTIONARY(self->self);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003023 else if (strcmp(name, "auto_rebind") == 0)
3024 return self->auto_rebind
3025 ? AlwaysTrue(NULL)
3026 : AlwaysFalse(NULL);
Bram Moolenaar8110a092016-04-14 15:56:09 +02003027 else if (strcmp(name, "__members__") == 0)
3028 return ObjectDir(NULL, FunctionAttrs);
3029 return NULL;
3030}
3031
3032/* Populate partial_T given function object.
3033 *
3034 * "exported" should be set to true when it is needed to construct a partial
3035 * that may be stored in a variable (i.e. may be freed by Vim).
3036 */
3037 static void
3038set_partial(FunctionObject *self, partial_T *pt, int exported)
3039{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003040 int i;
3041
3042 pt->pt_name = self->name;
3043 if (self->argv)
3044 {
3045 pt->pt_argc = self->argc;
3046 if (exported)
3047 {
3048 pt->pt_argv = (typval_T *)alloc_clear(
3049 sizeof(typval_T) * self->argc);
3050 for (i = 0; i < pt->pt_argc; ++i)
3051 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3052 }
3053 else
3054 pt->pt_argv = self->argv;
3055 }
3056 else
3057 {
3058 pt->pt_argc = 0;
3059 pt->pt_argv = NULL;
3060 }
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003061 pt->pt_auto = self->auto_rebind || !exported;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003062 pt->pt_dict = self->self;
3063 if (exported && self->self)
3064 ++pt->pt_dict->dv_refcount;
3065 if (exported)
3066 pt->pt_name = vim_strsave(pt->pt_name);
3067 pt->pt_refcount = 1;
3068}
3069
3070 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003071FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003072{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003073 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003074 typval_T args;
3075 typval_T selfdicttv;
3076 typval_T rettv;
3077 dict_T *selfdict = NULL;
3078 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003079 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003080 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003081 partial_T pt;
3082 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003083
Bram Moolenaar8110a092016-04-14 15:56:09 +02003084 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003085 return NULL;
3086
3087 if (kwargs != NULL)
3088 {
3089 selfdictObject = PyDict_GetItemString(kwargs, "self");
3090 if (selfdictObject != NULL)
3091 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003092 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003093 {
3094 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003095 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003096 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003097 selfdict = selfdicttv.vval.v_dict;
3098 }
3099 }
3100
Bram Moolenaar8110a092016-04-14 15:56:09 +02003101 if (self->argv || self->self)
3102 {
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003103 vim_memset(&pt, 0, sizeof(partial_T));
Bram Moolenaar8110a092016-04-14 15:56:09 +02003104 set_partial(self, &pt, FALSE);
3105 pt_ptr = &pt;
3106 }
3107
Bram Moolenaar71700b82013-05-15 17:49:05 +02003108 Py_BEGIN_ALLOW_THREADS
3109 Python_Lock_Vim();
3110
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003111 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003112 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003113
3114 Python_Release_Vim();
3115 Py_END_ALLOW_THREADS
3116
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003117 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003118 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003119 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003120 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003121 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003122 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003123 }
3124 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003125 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003126
Bram Moolenaardb913952012-06-29 12:54:53 +02003127 clear_tv(&args);
3128 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003129 if (selfdict != NULL)
3130 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003131
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003132 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003133}
3134
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003135 static PyObject *
3136FunctionRepr(FunctionObject *self)
3137{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003138 PyObject *ret;
3139 garray_T repr_ga;
3140 int i;
3141 char_u *tofree = NULL;
3142 typval_T tv;
3143 char_u numbuf[NUMBUFLEN];
3144
3145 ga_init2(&repr_ga, (int)sizeof(char), 70);
3146 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3147 if (self->name)
3148 ga_concat(&repr_ga, self->name);
3149 else
3150 ga_concat(&repr_ga, (char_u *)"<NULL>");
3151 ga_append(&repr_ga, '\'');
3152 if (self->argv)
3153 {
3154 ga_concat(&repr_ga, (char_u *)", args=[");
3155 ++emsg_silent;
3156 for (i = 0; i < self->argc; i++)
3157 {
3158 if (i != 0)
3159 ga_concat(&repr_ga, (char_u *)", ");
3160 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3161 get_copyID()));
3162 vim_free(tofree);
3163 }
3164 --emsg_silent;
3165 ga_append(&repr_ga, ']');
3166 }
3167 if (self->self)
3168 {
3169 ga_concat(&repr_ga, (char_u *)", self=");
3170 tv.v_type = VAR_DICT;
3171 tv.vval.v_dict = self->self;
3172 ++emsg_silent;
3173 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3174 --emsg_silent;
3175 vim_free(tofree);
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02003176 if (self->auto_rebind)
3177 ga_concat(&repr_ga, (char_u *)", auto_rebind=True");
Bram Moolenaar8110a092016-04-14 15:56:09 +02003178 }
3179 ga_append(&repr_ga, '>');
3180 ret = PyString_FromString((char *)repr_ga.ga_data);
3181 ga_clear(&repr_ga);
3182 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003183}
3184
Bram Moolenaardb913952012-06-29 12:54:53 +02003185static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003186 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3187 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003188};
3189
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003190/*
3191 * Options object
3192 */
3193
3194static PyTypeObject OptionsType;
3195
3196typedef int (*checkfun)(void *);
3197
3198typedef struct
3199{
3200 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003201 int opt_type;
3202 void *from;
3203 checkfun Check;
3204 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003205} OptionsObject;
3206
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003207 static int
3208dummy_check(void *arg UNUSED)
3209{
3210 return 0;
3211}
3212
3213 static PyObject *
3214OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3215{
3216 OptionsObject *self;
3217
Bram Moolenaar774267b2013-05-21 20:51:59 +02003218 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003219 if (self == NULL)
3220 return NULL;
3221
3222 self->opt_type = opt_type;
3223 self->from = from;
3224 self->Check = Check;
3225 self->fromObj = fromObj;
3226 if (fromObj)
3227 Py_INCREF(fromObj);
3228
3229 return (PyObject *)(self);
3230}
3231
3232 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003233OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003234{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003235 PyObject_GC_UnTrack((void *)(self));
3236 Py_XDECREF(self->fromObj);
3237 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003238}
3239
3240 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003241OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003242{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003243 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003244 return 0;
3245}
3246
3247 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003248OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003249{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003250 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003251 return 0;
3252}
3253
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003254 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003255OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003256{
3257 char_u *key;
3258 int flags;
3259 long numval;
3260 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003261 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003262
Bram Moolenaard6e39182013-05-21 18:30:34 +02003263 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003264 return NULL;
3265
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003266 if (!(key = StringToChars(keyObject, &todecref)))
3267 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003268
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003269 if (*key == NUL)
3270 {
3271 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003272 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003273 return NULL;
3274 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003275
3276 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003277 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003278
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003279 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003280
3281 if (flags == 0)
3282 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003283 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003284 return NULL;
3285 }
3286
3287 if (flags & SOPT_UNSET)
3288 {
3289 Py_INCREF(Py_None);
3290 return Py_None;
3291 }
3292 else if (flags & SOPT_BOOL)
3293 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003294 PyObject *ret;
3295 ret = numval ? Py_True : Py_False;
3296 Py_INCREF(ret);
3297 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003298 }
3299 else if (flags & SOPT_NUM)
3300 return PyInt_FromLong(numval);
3301 else if (flags & SOPT_STRING)
3302 {
3303 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003304 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003305 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003306 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003307 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003308 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003309 else
3310 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003311 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003312 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003313 return NULL;
3314 }
3315 }
3316 else
3317 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003318 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003319 return NULL;
3320 }
3321}
3322
3323 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003324OptionsContains(OptionsObject *self, PyObject *keyObject)
3325{
3326 char_u *key;
3327 PyObject *todecref;
3328
3329 if (!(key = StringToChars(keyObject, &todecref)))
3330 return -1;
3331
3332 if (*key == NUL)
3333 {
3334 Py_XDECREF(todecref);
3335 return 0;
3336 }
3337
3338 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3339 {
3340 Py_XDECREF(todecref);
3341 return 1;
3342 }
3343 else
3344 {
3345 Py_XDECREF(todecref);
3346 return 0;
3347 }
3348}
3349
3350typedef struct
3351{
3352 void *lastoption;
3353 int opt_type;
3354} optiterinfo_T;
3355
3356 static PyObject *
3357OptionsIterNext(optiterinfo_T **oii)
3358{
3359 char_u *name;
3360
3361 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3362 return PyString_FromString((char *)name);
3363
3364 return NULL;
3365}
3366
3367 static PyObject *
3368OptionsIter(OptionsObject *self)
3369{
3370 optiterinfo_T *oii;
3371
3372 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3373 {
3374 PyErr_NoMemory();
3375 return NULL;
3376 }
3377
3378 oii->opt_type = self->opt_type;
3379 oii->lastoption = NULL;
3380
3381 return IterNew(oii,
3382 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3383 NULL, NULL);
3384}
3385
3386 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003387set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003388{
3389 char_u *errmsg;
3390
3391 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3392 {
3393 if (VimTryEnd())
3394 return FAIL;
3395 PyErr_SetVim((char *)errmsg);
3396 return FAIL;
3397 }
3398 return OK;
3399}
3400
3401 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003402set_option_value_for(
3403 char_u *key,
3404 int numval,
3405 char_u *stringval,
3406 int opt_flags,
3407 int opt_type,
3408 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003409{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003410 win_T *save_curwin = NULL;
3411 tabpage_T *save_curtab = NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003412 bufref_T save_curbuf;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003413 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003414
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003415 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003416 switch (opt_type)
3417 {
3418 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003419 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003420 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003421 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003422 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003423 if (VimTryEnd())
3424 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003425 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003426 return -1;
3427 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003428 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3429 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003430 break;
3431 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003432 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003433 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003434 restore_buffer(&save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003435 break;
3436 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003437 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003438 break;
3439 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003440 if (set_ret == FAIL)
3441 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003442 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003443}
3444
3445 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003446OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003447{
3448 char_u *key;
3449 int flags;
3450 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003451 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003452 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003453
Bram Moolenaard6e39182013-05-21 18:30:34 +02003454 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003455 return -1;
3456
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003457 if (!(key = StringToChars(keyObject, &todecref)))
3458 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003459
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003460 if (*key == NUL)
3461 {
3462 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003463 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003464 return -1;
3465 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003466
3467 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003468 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003469
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003470 if (flags == 0)
3471 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003472 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003473 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003474 return -1;
3475 }
3476
3477 if (valObject == NULL)
3478 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003479 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003480 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003481 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003482 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003483 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003484 return -1;
3485 }
3486 else if (!(flags & SOPT_GLOBAL))
3487 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003488 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003489 N_("unable to unset option %s "
3490 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003491 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003492 return -1;
3493 }
3494 else
3495 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003496 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003497 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003498 return 0;
3499 }
3500 }
3501
Bram Moolenaard6e39182013-05-21 18:30:34 +02003502 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003503
3504 if (flags & SOPT_BOOL)
3505 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003506 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003507
Bram Moolenaarb983f752013-05-15 16:11:50 +02003508 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003509 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003510 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003511 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003512 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003513 }
3514 else if (flags & SOPT_NUM)
3515 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003516 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003517
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003518 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003519 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003520 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003521 return -1;
3522 }
3523
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003524 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003525 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003526 }
3527 else
3528 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003529 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003530 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003531
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003532 if ((val = StringToChars(valObject, &todecref2)))
3533 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003534 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003535 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003536 Py_XDECREF(todecref2);
3537 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003538 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003539 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003540 }
3541
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003542 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003543
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003544 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003545}
3546
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003547static PySequenceMethods OptionsAsSeq = {
3548 0, /* sq_length */
3549 0, /* sq_concat */
3550 0, /* sq_repeat */
3551 0, /* sq_item */
3552 0, /* sq_slice */
3553 0, /* sq_ass_item */
3554 0, /* sq_ass_slice */
3555 (objobjproc) OptionsContains, /* sq_contains */
3556 0, /* sq_inplace_concat */
3557 0, /* sq_inplace_repeat */
3558};
3559
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003560static PyMappingMethods OptionsAsMapping = {
3561 (lenfunc) NULL,
3562 (binaryfunc) OptionsItem,
3563 (objobjargproc) OptionsAssItem,
3564};
3565
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003566/* Tabpage object
3567 */
3568
3569typedef struct
3570{
3571 PyObject_HEAD
3572 tabpage_T *tab;
3573} TabPageObject;
3574
3575static PyObject *WinListNew(TabPageObject *tabObject);
3576
3577static PyTypeObject TabPageType;
3578
3579 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003580CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003581{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003582 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003583 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003584 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003585 return -1;
3586 }
3587
3588 return 0;
3589}
3590
3591 static PyObject *
3592TabPageNew(tabpage_T *tab)
3593{
3594 TabPageObject *self;
3595
3596 if (TAB_PYTHON_REF(tab))
3597 {
3598 self = TAB_PYTHON_REF(tab);
3599 Py_INCREF(self);
3600 }
3601 else
3602 {
3603 self = PyObject_NEW(TabPageObject, &TabPageType);
3604 if (self == NULL)
3605 return NULL;
3606 self->tab = tab;
3607 TAB_PYTHON_REF(tab) = self;
3608 }
3609
3610 return (PyObject *)(self);
3611}
3612
3613 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003614TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003615{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003616 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3617 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003618
3619 DESTRUCTOR_FINISH(self);
3620}
3621
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003622static char *TabPageAttrs[] = {
3623 "windows", "number", "vars", "window", "valid",
3624 NULL
3625};
3626
3627 static PyObject *
3628TabPageDir(PyObject *self)
3629{
3630 return ObjectDir(self, TabPageAttrs);
3631}
3632
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003633 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003634TabPageAttrValid(TabPageObject *self, char *name)
3635{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003636 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003637
3638 if (strcmp(name, "valid") != 0)
3639 return NULL;
3640
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003641 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3642 Py_INCREF(ret);
3643 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003644}
3645
3646 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003647TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003648{
3649 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003650 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003651 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003652 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003653 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003654 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003655 else if (strcmp(name, "window") == 0)
3656 {
3657 /* For current tab window.c does not bother to set or update tp_curwin
3658 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003659 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003660 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003661 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003662 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003663 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003664 else if (strcmp(name, "__members__") == 0)
3665 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003666 return NULL;
3667}
3668
3669 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003670TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003671{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003672 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003673 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003674 else
3675 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003676 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003677
3678 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003679 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3680 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003681 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003682 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003683 }
3684}
3685
3686static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003687 /* name, function, calling, documentation */
3688 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3689 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003690};
3691
3692/*
3693 * Window list object
3694 */
3695
3696static PyTypeObject TabListType;
3697static PySequenceMethods TabListAsSeq;
3698
3699typedef struct
3700{
3701 PyObject_HEAD
3702} TabListObject;
3703
3704 static PyInt
3705TabListLength(PyObject *self UNUSED)
3706{
3707 tabpage_T *tp = first_tabpage;
3708 PyInt n = 0;
3709
3710 while (tp != NULL)
3711 {
3712 ++n;
3713 tp = tp->tp_next;
3714 }
3715
3716 return n;
3717}
3718
3719 static PyObject *
3720TabListItem(PyObject *self UNUSED, PyInt n)
3721{
3722 tabpage_T *tp;
3723
3724 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3725 if (n == 0)
3726 return TabPageNew(tp);
3727
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003728 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003729 return NULL;
3730}
3731
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003732/*
3733 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003734 */
3735
3736typedef struct
3737{
3738 PyObject_HEAD
3739 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003740 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003741} WindowObject;
3742
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003743static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003744
3745 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003746CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003747{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003748 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003749 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003750 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003751 return -1;
3752 }
3753
3754 return 0;
3755}
3756
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003757 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003758WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003759{
3760 /* We need to handle deletion of windows underneath us.
3761 * If we add a "w_python*_ref" field to the win_T structure,
3762 * then we can get at it in win_free() in vim. We then
3763 * need to create only ONE Python object per window - if
3764 * we try to create a second, just INCREF the existing one
3765 * and return it. The (single) Python object referring to
3766 * the window is stored in "w_python*_ref".
3767 * On a win_free() we set the Python object's win_T* field
3768 * to an invalid value. We trap all uses of a window
3769 * object, and reject them if the win_T* field is invalid.
3770 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003771 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003772 * w_python_ref and w_python3_ref fields respectively.
3773 */
3774
3775 WindowObject *self;
3776
3777 if (WIN_PYTHON_REF(win))
3778 {
3779 self = WIN_PYTHON_REF(win);
3780 Py_INCREF(self);
3781 }
3782 else
3783 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003784 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003785 if (self == NULL)
3786 return NULL;
3787 self->win = win;
3788 WIN_PYTHON_REF(win) = self;
3789 }
3790
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003791 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3792
Bram Moolenaar971db462013-05-12 18:44:48 +02003793 return (PyObject *)(self);
3794}
3795
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003796 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003797WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003798{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003799 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003800 if (self->win && self->win != INVALID_WINDOW_VALUE)
3801 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003802 Py_XDECREF(((PyObject *)(self->tabObject)));
3803 PyObject_GC_Del((void *)(self));
3804}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003805
Bram Moolenaar774267b2013-05-21 20:51:59 +02003806 static int
3807WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3808{
3809 Py_VISIT(((PyObject *)(self->tabObject)));
3810 return 0;
3811}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003812
Bram Moolenaar774267b2013-05-21 20:51:59 +02003813 static int
3814WindowClear(WindowObject *self)
3815{
3816 Py_CLEAR(self->tabObject);
3817 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003818}
3819
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003820 static win_T *
3821get_firstwin(TabPageObject *tabObject)
3822{
3823 if (tabObject)
3824 {
3825 if (CheckTabPage(tabObject))
3826 return NULL;
3827 /* For current tab window.c does not bother to set or update tp_firstwin
3828 */
3829 else if (tabObject->tab == curtab)
3830 return firstwin;
3831 else
3832 return tabObject->tab->tp_firstwin;
3833 }
3834 else
3835 return firstwin;
3836}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003837static char *WindowAttrs[] = {
3838 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3839 "tabpage", "valid",
3840 NULL
3841};
3842
3843 static PyObject *
3844WindowDir(PyObject *self)
3845{
3846 return ObjectDir(self, WindowAttrs);
3847}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003848
Bram Moolenaar971db462013-05-12 18:44:48 +02003849 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003850WindowAttrValid(WindowObject *self, char *name)
3851{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003852 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003853
3854 if (strcmp(name, "valid") != 0)
3855 return NULL;
3856
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003857 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3858 Py_INCREF(ret);
3859 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003860}
3861
3862 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003863WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003864{
3865 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003866 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003867 else if (strcmp(name, "cursor") == 0)
3868 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003869 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003870
3871 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3872 }
3873 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003874 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003875 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003876 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003877 else if (strcmp(name, "width") == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02003878 return PyLong_FromLong((long)(self->win->w_width));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003879 else if (strcmp(name, "col") == 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003880 return PyLong_FromLong((long)(self->win->w_wincol));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003881 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003882 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003883 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003884 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3885 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003886 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003887 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003888 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003889 return NULL;
3890 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003891 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003892 }
3893 else if (strcmp(name, "tabpage") == 0)
3894 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003895 Py_INCREF(self->tabObject);
3896 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003897 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003898 else if (strcmp(name, "__members__") == 0)
3899 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003900 else
3901 return NULL;
3902}
3903
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003904 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003905WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003906{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003907 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003908 return -1;
3909
3910 if (strcmp(name, "buffer") == 0)
3911 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003912 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003913 return -1;
3914 }
3915 else if (strcmp(name, "cursor") == 0)
3916 {
3917 long lnum;
3918 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003919
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003920 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003921 return -1;
3922
Bram Moolenaard6e39182013-05-21 18:30:34 +02003923 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003924 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003925 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003926 return -1;
3927 }
3928
3929 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003930 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003931 return -1;
3932
Bram Moolenaard6e39182013-05-21 18:30:34 +02003933 self->win->w_cursor.lnum = lnum;
3934 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003935#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003936 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003937#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003938 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003939 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003940
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003941 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003942 return 0;
3943 }
3944 else if (strcmp(name, "height") == 0)
3945 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003946 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003947 win_T *savewin;
3948
Bram Moolenaardee2e312013-06-23 16:35:47 +02003949 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003950 return -1;
3951
3952#ifdef FEAT_GUI
3953 need_mouse_correct = TRUE;
3954#endif
3955 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003956 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003957
3958 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003959 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003960 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003961 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003962 return -1;
3963
3964 return 0;
3965 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003966 else if (strcmp(name, "width") == 0)
3967 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003968 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003969 win_T *savewin;
3970
Bram Moolenaardee2e312013-06-23 16:35:47 +02003971 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003972 return -1;
3973
3974#ifdef FEAT_GUI
3975 need_mouse_correct = TRUE;
3976#endif
3977 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003978 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003979
3980 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003981 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003982 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003983 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003984 return -1;
3985
3986 return 0;
3987 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003988 else
3989 {
3990 PyErr_SetString(PyExc_AttributeError, name);
3991 return -1;
3992 }
3993}
3994
3995 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003996WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003997{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003998 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003999 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004000 else
4001 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004002 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004003
Bram Moolenaar6d216452013-05-12 19:00:41 +02004004 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004005 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004006 (self));
4007 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004008 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004009 }
4010}
4011
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004012static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004013 /* name, function, calling, documentation */
4014 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
4015 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004016};
4017
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004018/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004019 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004020 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004021
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004022static PyTypeObject WinListType;
4023static PySequenceMethods WinListAsSeq;
4024
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004025typedef struct
4026{
4027 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004028 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004029} WinListObject;
4030
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004031 static PyObject *
4032WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004033{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004034 WinListObject *self;
4035
4036 self = PyObject_NEW(WinListObject, &WinListType);
4037 self->tabObject = tabObject;
4038 Py_INCREF(tabObject);
4039
4040 return (PyObject *)(self);
4041}
4042
4043 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004044WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004045{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004046 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004047
4048 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004049 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004050 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004051 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004052
4053 DESTRUCTOR_FINISH(self);
4054}
4055
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004056 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004057WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004058{
4059 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004060 PyInt n = 0;
4061
Bram Moolenaard6e39182013-05-21 18:30:34 +02004062 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004063 return -1;
4064
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004065 while (w != NULL)
4066 {
4067 ++n;
4068 w = W_NEXT(w);
4069 }
4070
4071 return n;
4072}
4073
4074 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004075WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076{
4077 win_T *w;
4078
Bram Moolenaard6e39182013-05-21 18:30:34 +02004079 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004080 return NULL;
4081
4082 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004083 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004084 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004086 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087 return NULL;
4088}
4089
4090/* Convert a Python string into a Vim line.
4091 *
4092 * The result is in allocated memory. All internal nulls are replaced by
4093 * newline characters. It is an error for the string to contain newline
4094 * characters.
4095 *
4096 * On errors, the Python exception data is set, and NULL is returned.
4097 */
4098 static char *
4099StringToLine(PyObject *obj)
4100{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004101 char *str;
4102 char *save;
4103 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004104 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004105 PyInt i;
4106 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004108 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004110 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4111 || str == NULL)
4112 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004114 else if (PyUnicode_Check(obj))
4115 {
4116 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4117 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004118
Bram Moolenaardaa27022013-06-24 22:33:30 +02004119 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004120 || str == NULL)
4121 {
4122 Py_DECREF(bytes);
4123 return NULL;
4124 }
4125 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004126 else
4127 {
4128#if PY_MAJOR_VERSION < 3
4129 PyErr_FORMAT(PyExc_TypeError,
4130 N_("expected str() or unicode() instance, but got %s"),
4131 Py_TYPE_NAME(obj));
4132#else
4133 PyErr_FORMAT(PyExc_TypeError,
4134 N_("expected bytes() or str() instance, but got %s"),
4135 Py_TYPE_NAME(obj));
4136#endif
4137 return NULL;
4138 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004139
4140 /*
4141 * Error checking: String must not contain newlines, as we
4142 * are replacing a single line, and we must replace it with
4143 * a single line.
4144 * A trailing newline is removed, so that append(f.readlines()) works.
4145 */
4146 p = memchr(str, '\n', len);
4147 if (p != NULL)
4148 {
4149 if (p == str + len - 1)
4150 --len;
4151 else
4152 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004153 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004154 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004155 return NULL;
4156 }
4157 }
4158
4159 /* Create a copy of the string, with internal nulls replaced by
4160 * newline characters, as is the vim convention.
4161 */
4162 save = (char *)alloc((unsigned)(len+1));
4163 if (save == NULL)
4164 {
4165 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004166 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004167 return NULL;
4168 }
4169
4170 for (i = 0; i < len; ++i)
4171 {
4172 if (str[i] == '\0')
4173 save[i] = '\n';
4174 else
4175 save[i] = str[i];
4176 }
4177
4178 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004179 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004180
4181 return save;
4182}
4183
4184/* Get a line from the specified buffer. The line number is
4185 * in Vim format (1-based). The line is returned as a Python
4186 * string object.
4187 */
4188 static PyObject *
4189GetBufferLine(buf_T *buf, PyInt n)
4190{
4191 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4192}
4193
4194
4195/* Get a list of lines from the specified buffer. The line numbers
4196 * are in Vim format (1-based). The range is from lo up to, but not
4197 * including, hi. The list is returned as a Python list of string objects.
4198 */
4199 static PyObject *
4200GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4201{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004202 PyInt i;
4203 PyInt n = hi - lo;
4204 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004205
4206 if (list == NULL)
4207 return NULL;
4208
4209 for (i = 0; i < n; ++i)
4210 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004211 PyObject *string = LineToString(
4212 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004213
4214 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004215 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004216 {
4217 Py_DECREF(list);
4218 return NULL;
4219 }
4220
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004221 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004222 }
4223
4224 /* The ownership of the Python list is passed to the caller (ie,
4225 * the caller should Py_DECREF() the object when it is finished
4226 * with it).
4227 */
4228
4229 return list;
4230}
4231
4232/*
4233 * Check if deleting lines made the cursor position invalid.
4234 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4235 * deleted).
4236 */
4237 static void
4238py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4239{
4240 if (curwin->w_cursor.lnum >= lo)
4241 {
4242 /* Adjust the cursor position if it's in/after the changed
4243 * lines. */
4244 if (curwin->w_cursor.lnum >= hi)
4245 {
4246 curwin->w_cursor.lnum += extra;
4247 check_cursor_col();
4248 }
4249 else if (extra < 0)
4250 {
4251 curwin->w_cursor.lnum = lo;
4252 check_cursor();
4253 }
4254 else
4255 check_cursor_col();
4256 changed_cline_bef_curs();
4257 }
4258 invalidate_botline();
4259}
4260
Bram Moolenaar19e60942011-06-19 00:27:51 +02004261/*
4262 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004263 * in Vim format (1-based). The replacement line is given as
4264 * a Python string object. The object is checked for validity
4265 * and correct format. Errors are returned as a value of FAIL.
4266 * The return value is OK on success.
4267 * If OK is returned and len_change is not NULL, *len_change
4268 * is set to the change in the buffer length.
4269 */
4270 static int
4271SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4272{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004273 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004274 win_T *save_curwin = NULL;
4275 tabpage_T *save_curtab = NULL;
4276
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004277 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004278 * There are three cases:
4279 * 1. NULL, or None - this is a deletion.
4280 * 2. A string - this is a replacement.
4281 * 3. Anything else - this is an error.
4282 */
4283 if (line == Py_None || line == NULL)
4284 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004285 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004286 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004287
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004288 VimTryStart();
4289
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004290 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004291 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004292 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004293 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004294 else
4295 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004296 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004297 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004298 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004299 /* Only adjust marks if we managed to switch to a window that
4300 * holds the buffer, otherwise line numbers will be invalid. */
4301 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004302 }
4303
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004304 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004305
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004306 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004307 return FAIL;
4308
4309 if (len_change)
4310 *len_change = -1;
4311
4312 return OK;
4313 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004314 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004315 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004316 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004317
4318 if (save == NULL)
4319 return FAIL;
4320
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004321 VimTryStart();
4322
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004323 /* We do not need to free "save" if ml_replace() consumes it. */
4324 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004325 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004326
4327 if (u_savesub((linenr_T)n) == FAIL)
4328 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004329 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004330 vim_free(save);
4331 }
4332 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4333 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004334 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004335 vim_free(save);
4336 }
4337 else
4338 changed_bytes((linenr_T)n, 0);
4339
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004340 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004341
4342 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004343 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004344 check_cursor_col();
4345
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004346 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004347 return FAIL;
4348
4349 if (len_change)
4350 *len_change = 0;
4351
4352 return OK;
4353 }
4354 else
4355 {
4356 PyErr_BadArgument();
4357 return FAIL;
4358 }
4359}
4360
Bram Moolenaar19e60942011-06-19 00:27:51 +02004361/* Replace a range of lines in the specified buffer. The line numbers are in
4362 * Vim format (1-based). The range is from lo up to, but not including, hi.
4363 * The replacement lines are given as a Python list of string objects. The
4364 * list is checked for validity and correct format. Errors are returned as a
4365 * value of FAIL. The return value is OK on success.
4366 * If OK is returned and len_change is not NULL, *len_change
4367 * is set to the change in the buffer length.
4368 */
4369 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004370SetBufferLineList(
4371 buf_T *buf,
4372 PyInt lo,
4373 PyInt hi,
4374 PyObject *list,
4375 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004376{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004377 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004378 win_T *save_curwin = NULL;
4379 tabpage_T *save_curtab = NULL;
4380
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004381 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004382 * There are three cases:
4383 * 1. NULL, or None - this is a deletion.
4384 * 2. A list - this is a replacement.
4385 * 3. Anything else - this is an error.
4386 */
4387 if (list == Py_None || list == NULL)
4388 {
4389 PyInt i;
4390 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004391
4392 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004393 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004394 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004395
4396 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004397 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004398 else
4399 {
4400 for (i = 0; i < n; ++i)
4401 {
4402 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4403 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004404 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004405 break;
4406 }
4407 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004408 if (buf == curbuf && (save_curwin != NULL
4409 || save_curbuf.br_buf == NULL))
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004410 /* Using an existing window for the buffer, adjust the cursor
4411 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004412 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004413 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004414 /* Only adjust marks if we managed to switch to a window that
4415 * holds the buffer, otherwise line numbers will be invalid. */
4416 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004417 }
4418
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004419 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004420
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004421 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004422 return FAIL;
4423
4424 if (len_change)
4425 *len_change = -n;
4426
4427 return OK;
4428 }
4429 else if (PyList_Check(list))
4430 {
4431 PyInt i;
4432 PyInt new_len = PyList_Size(list);
4433 PyInt old_len = hi - lo;
4434 PyInt extra = 0; /* lines added to text, can be negative */
4435 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004436
4437 if (new_len == 0) /* avoid allocating zero bytes */
4438 array = NULL;
4439 else
4440 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004441 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004442 if (array == NULL)
4443 {
4444 PyErr_NoMemory();
4445 return FAIL;
4446 }
4447 }
4448
4449 for (i = 0; i < new_len; ++i)
4450 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004451 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004452
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004453 if (!(line = PyList_GetItem(list, i)) ||
4454 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004455 {
4456 while (i)
4457 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004458 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004459 return FAIL;
4460 }
4461 }
4462
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004463 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004464 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004465
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004466 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004467 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004468
4469 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004470 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004471
4472 /* If the size of the range is reducing (ie, new_len < old_len) we
4473 * need to delete some old_len. We do this at the start, by
4474 * repeatedly deleting line "lo".
4475 */
4476 if (!PyErr_Occurred())
4477 {
4478 for (i = 0; i < old_len - new_len; ++i)
4479 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4480 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004481 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004482 break;
4483 }
4484 extra -= i;
4485 }
4486
4487 /* For as long as possible, replace the existing old_len with the
4488 * new old_len. This is a more efficient operation, as it requires
4489 * less memory allocation and freeing.
4490 */
4491 if (!PyErr_Occurred())
4492 {
4493 for (i = 0; i < old_len && i < new_len; ++i)
4494 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4495 == FAIL)
4496 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004497 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004498 break;
4499 }
4500 }
4501 else
4502 i = 0;
4503
4504 /* Now we may need to insert the remaining new old_len. If we do, we
4505 * must free the strings as we finish with them (we can't pass the
4506 * responsibility to vim in this case).
4507 */
4508 if (!PyErr_Occurred())
4509 {
4510 while (i < new_len)
4511 {
4512 if (ml_append((linenr_T)(lo + i - 1),
4513 (char_u *)array[i], 0, FALSE) == FAIL)
4514 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004515 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004516 break;
4517 }
4518 vim_free(array[i]);
4519 ++i;
4520 ++extra;
4521 }
4522 }
4523
4524 /* Free any left-over old_len, as a result of an error */
4525 while (i < new_len)
4526 {
4527 vim_free(array[i]);
4528 ++i;
4529 }
4530
4531 /* Free the array of old_len. All of its contents have now
4532 * been dealt with (either freed, or the responsibility passed
4533 * to vim.
4534 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004535 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004536
4537 /* Adjust marks. Invalidate any which lie in the
4538 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004539 * Only adjust marks if we managed to switch to a window that holds
4540 * the buffer, otherwise line numbers will be invalid. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004541 if (save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004542 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004543 (long)MAXLNUM, (long)extra);
4544 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4545
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004546 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004547 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4548
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004549 /* END of region without "return". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004550 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004551
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004552 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004553 return FAIL;
4554
4555 if (len_change)
4556 *len_change = new_len - old_len;
4557
4558 return OK;
4559 }
4560 else
4561 {
4562 PyErr_BadArgument();
4563 return FAIL;
4564 }
4565}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004566
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004567/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004568 * The line number is in Vim format (1-based). The lines to be inserted are
4569 * given as a Python list of string objects or as a single string. The lines
4570 * to be added are checked for validity and correct format. Errors are
4571 * returned as a value of FAIL. The return value is OK on success.
4572 * If OK is returned and len_change is not NULL, *len_change
4573 * is set to the change in the buffer length.
4574 */
4575 static int
4576InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4577{
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02004578 bufref_T save_curbuf = {NULL, 0, 0};
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004579 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004580 tabpage_T *save_curtab = NULL;
4581
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004582 /* First of all, we check the type of the supplied Python object.
4583 * It must be a string or a list, or the call is in error.
4584 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004585 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004586 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004587 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004588
4589 if (str == NULL)
4590 return FAIL;
4591
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004592 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004593 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004594 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004595
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004596 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004597 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004598 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004599 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004600 else if (save_curbuf.br_buf == NULL)
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004601 /* Only adjust marks if we managed to switch to a window that
4602 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004603 appended_lines_mark((linenr_T)n, 1L);
4604
4605 vim_free(str);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004606 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004607 update_screen(VALID);
4608
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004609 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004610 return FAIL;
4611
4612 if (len_change)
4613 *len_change = 1;
4614
4615 return OK;
4616 }
4617 else if (PyList_Check(lines))
4618 {
4619 PyInt i;
4620 PyInt size = PyList_Size(lines);
4621 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004622
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004623 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004624 if (array == NULL)
4625 {
4626 PyErr_NoMemory();
4627 return FAIL;
4628 }
4629
4630 for (i = 0; i < size; ++i)
4631 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004632 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004633
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004634 if (!(line = PyList_GetItem(lines, i)) ||
4635 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004636 {
4637 while (i)
4638 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004639 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004640 return FAIL;
4641 }
4642 }
4643
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004644 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004645 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004646 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004647
4648 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004649 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004650 else
4651 {
4652 for (i = 0; i < size; ++i)
4653 {
4654 if (ml_append((linenr_T)(n + i),
4655 (char_u *)array[i], 0, FALSE) == FAIL)
4656 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004657 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004658
4659 /* Free the rest of the lines */
4660 while (i < size)
4661 vim_free(array[i++]);
4662
4663 break;
4664 }
4665 vim_free(array[i]);
4666 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004667 if (i > 0 && save_curbuf.br_buf == NULL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004668 /* Only adjust marks if we managed to switch to a window that
4669 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004670 appended_lines_mark((linenr_T)n, (long)i);
4671 }
4672
4673 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004674 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004675 PyMem_Free(array);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004676 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004677
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004678 update_screen(VALID);
4679
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004680 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004681 return FAIL;
4682
4683 if (len_change)
4684 *len_change = size;
4685
4686 return OK;
4687 }
4688 else
4689 {
4690 PyErr_BadArgument();
4691 return FAIL;
4692 }
4693}
4694
4695/*
4696 * Common routines for buffers and line ranges
4697 * -------------------------------------------
4698 */
4699
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004700typedef struct
4701{
4702 PyObject_HEAD
4703 buf_T *buf;
4704} BufferObject;
4705
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004706 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004707CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004708{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004709 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004710 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004711 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004712 return -1;
4713 }
4714
4715 return 0;
4716}
4717
4718 static PyObject *
4719RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4720{
4721 if (CheckBuffer(self))
4722 return NULL;
4723
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004724 if (end == -1)
4725 end = self->buf->b_ml.ml_line_count;
4726
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004727 if (n < 0)
4728 n += end - start + 1;
4729
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004730 if (n < 0 || n > end - start)
4731 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004732 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004733 return NULL;
4734 }
4735
4736 return GetBufferLine(self->buf, n+start);
4737}
4738
4739 static PyObject *
4740RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4741{
4742 PyInt size;
4743
4744 if (CheckBuffer(self))
4745 return NULL;
4746
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004747 if (end == -1)
4748 end = self->buf->b_ml.ml_line_count;
4749
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004750 size = end - start + 1;
4751
4752 if (lo < 0)
4753 lo = 0;
4754 else if (lo > size)
4755 lo = size;
4756 if (hi < 0)
4757 hi = 0;
4758 if (hi < lo)
4759 hi = lo;
4760 else if (hi > size)
4761 hi = size;
4762
4763 return GetBufferLineList(self->buf, lo+start, hi+start);
4764}
4765
4766 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004767RBAsItem(
4768 BufferObject *self,
4769 PyInt n,
4770 PyObject *valObject,
4771 PyInt start,
4772 PyInt end,
4773 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004774{
4775 PyInt len_change;
4776
4777 if (CheckBuffer(self))
4778 return -1;
4779
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004780 if (end == -1)
4781 end = self->buf->b_ml.ml_line_count;
4782
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004783 if (n < 0)
4784 n += end - start + 1;
4785
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004786 if (n < 0 || n > end - start)
4787 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004788 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004789 return -1;
4790 }
4791
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004792 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004793 return -1;
4794
4795 if (new_end)
4796 *new_end = end + len_change;
4797
4798 return 0;
4799}
4800
Bram Moolenaar19e60942011-06-19 00:27:51 +02004801 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004802RBAsSlice(
4803 BufferObject *self,
4804 PyInt lo,
4805 PyInt hi,
4806 PyObject *valObject,
4807 PyInt start,
4808 PyInt end,
4809 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004810{
4811 PyInt size;
4812 PyInt len_change;
4813
4814 /* Self must be a valid buffer */
4815 if (CheckBuffer(self))
4816 return -1;
4817
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004818 if (end == -1)
4819 end = self->buf->b_ml.ml_line_count;
4820
Bram Moolenaar19e60942011-06-19 00:27:51 +02004821 /* Sort out the slice range */
4822 size = end - start + 1;
4823
4824 if (lo < 0)
4825 lo = 0;
4826 else if (lo > size)
4827 lo = size;
4828 if (hi < 0)
4829 hi = 0;
4830 if (hi < lo)
4831 hi = lo;
4832 else if (hi > size)
4833 hi = size;
4834
4835 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004836 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004837 return -1;
4838
4839 if (new_end)
4840 *new_end = end + len_change;
4841
4842 return 0;
4843}
4844
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004845
4846 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004847RBAppend(
4848 BufferObject *self,
4849 PyObject *args,
4850 PyInt start,
4851 PyInt end,
4852 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004853{
4854 PyObject *lines;
4855 PyInt len_change;
4856 PyInt max;
4857 PyInt n;
4858
4859 if (CheckBuffer(self))
4860 return NULL;
4861
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004862 if (end == -1)
4863 end = self->buf->b_ml.ml_line_count;
4864
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004865 max = n = end - start + 1;
4866
4867 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4868 return NULL;
4869
4870 if (n < 0 || n > max)
4871 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004872 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004873 return NULL;
4874 }
4875
4876 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4877 return NULL;
4878
4879 if (new_end)
4880 *new_end = end + len_change;
4881
4882 Py_INCREF(Py_None);
4883 return Py_None;
4884}
4885
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004886/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004887 */
4888
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004889static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004890static PySequenceMethods RangeAsSeq;
4891static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004892
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004893typedef struct
4894{
4895 PyObject_HEAD
4896 BufferObject *buf;
4897 PyInt start;
4898 PyInt end;
4899} RangeObject;
4900
4901 static PyObject *
4902RangeNew(buf_T *buf, PyInt start, PyInt end)
4903{
4904 BufferObject *bufr;
4905 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004906 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004907 if (self == NULL)
4908 return NULL;
4909
4910 bufr = (BufferObject *)BufferNew(buf);
4911 if (bufr == NULL)
4912 {
4913 Py_DECREF(self);
4914 return NULL;
4915 }
4916 Py_INCREF(bufr);
4917
4918 self->buf = bufr;
4919 self->start = start;
4920 self->end = end;
4921
4922 return (PyObject *)(self);
4923}
4924
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004925 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004926RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004927{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004928 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004929 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004930 PyObject_GC_Del((void *)(self));
4931}
4932
4933 static int
4934RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4935{
4936 Py_VISIT(((PyObject *)(self->buf)));
4937 return 0;
4938}
4939
4940 static int
4941RangeClear(RangeObject *self)
4942{
4943 Py_CLEAR(self->buf);
4944 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004945}
4946
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004947 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004948RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004949{
4950 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004951 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004952 return -1; /* ??? */
4953
Bram Moolenaard6e39182013-05-21 18:30:34 +02004954 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004955}
4956
4957 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004958RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004959{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004960 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004961}
4962
4963 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004964RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004965{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004966 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004967}
4968
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004969static char *RangeAttrs[] = {
4970 "start", "end",
4971 NULL
4972};
4973
4974 static PyObject *
4975RangeDir(PyObject *self)
4976{
4977 return ObjectDir(self, RangeAttrs);
4978}
4979
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004980 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004981RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004982{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004983 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004984}
4985
4986 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004987RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004988{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004989 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004990 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4991 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004992 else
4993 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004994 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004995
4996 if (name == NULL)
4997 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004998
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004999 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005000 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005001 }
5002}
5003
5004static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005005 /* name, function, calling, documentation */
5006 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005007 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5008 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005009};
5010
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005011static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005012static PySequenceMethods BufferAsSeq;
5013static PyMappingMethods BufferAsMapping;
5014
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005015 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005016BufferNew(buf_T *buf)
5017{
5018 /* We need to handle deletion of buffers underneath us.
5019 * If we add a "b_python*_ref" field to the buf_T structure,
5020 * then we can get at it in buf_freeall() in vim. We then
5021 * need to create only ONE Python object per buffer - if
5022 * we try to create a second, just INCREF the existing one
5023 * and return it. The (single) Python object referring to
5024 * the buffer is stored in "b_python*_ref".
5025 * Question: what to do on a buf_freeall(). We'll probably
5026 * have to either delete the Python object (DECREF it to
5027 * zero - a bad idea, as it leaves dangling refs!) or
5028 * set the buf_T * value to an invalid value (-1?), which
5029 * means we need checks in all access functions... Bah.
5030 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005031 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005032 * b_python_ref and b_python3_ref fields respectively.
5033 */
5034
5035 BufferObject *self;
5036
5037 if (BUF_PYTHON_REF(buf) != NULL)
5038 {
5039 self = BUF_PYTHON_REF(buf);
5040 Py_INCREF(self);
5041 }
5042 else
5043 {
5044 self = PyObject_NEW(BufferObject, &BufferType);
5045 if (self == NULL)
5046 return NULL;
5047 self->buf = buf;
5048 BUF_PYTHON_REF(buf) = self;
5049 }
5050
5051 return (PyObject *)(self);
5052}
5053
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005054 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005055BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005056{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005057 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5058 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005059
5060 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005061}
5062
Bram Moolenaar971db462013-05-12 18:44:48 +02005063 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005064BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005065{
5066 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005067 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005068 return -1; /* ??? */
5069
Bram Moolenaard6e39182013-05-21 18:30:34 +02005070 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005071}
5072
5073 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005074BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005075{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005076 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005077}
5078
5079 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005080BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005081{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005082 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005083}
5084
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005085static char *BufferAttrs[] = {
5086 "name", "number", "vars", "options", "valid",
5087 NULL
5088};
5089
5090 static PyObject *
5091BufferDir(PyObject *self)
5092{
5093 return ObjectDir(self, BufferAttrs);
5094}
5095
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005096 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005097BufferAttrValid(BufferObject *self, char *name)
5098{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005099 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005100
5101 if (strcmp(name, "valid") != 0)
5102 return NULL;
5103
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005104 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5105 Py_INCREF(ret);
5106 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005107}
5108
5109 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005110BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005111{
5112 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005113 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005114 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005115 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005116 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005117 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005118 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005119 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005120 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5121 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005122 else if (strcmp(name, "__members__") == 0)
5123 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005124 else
5125 return NULL;
5126}
5127
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005128 static int
5129BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5130{
5131 if (CheckBuffer(self))
5132 return -1;
5133
5134 if (strcmp(name, "name") == 0)
5135 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005136 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005137 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005138 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005139 PyObject *todecref;
5140
5141 if (!(val = StringToChars(valObject, &todecref)))
5142 return -1;
5143
5144 VimTryStart();
5145 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5146 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005147 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005148 aucmd_restbuf(&aco);
5149 Py_XDECREF(todecref);
5150 if (VimTryEnd())
5151 return -1;
5152
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005153 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005154 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005155 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005156 return -1;
5157 }
5158 return 0;
5159 }
5160 else
5161 {
5162 PyErr_SetString(PyExc_AttributeError, name);
5163 return -1;
5164 }
5165}
5166
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005167 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005168BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005169{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005170 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005171}
5172
5173 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005174BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005175{
5176 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005177 char_u *pmark;
5178 char_u mark;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005179 bufref_T savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005180 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005181
Bram Moolenaard6e39182013-05-21 18:30:34 +02005182 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005183 return NULL;
5184
Bram Moolenaar389a1792013-06-23 13:00:44 +02005185 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005186 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005187
Bram Moolenaar389a1792013-06-23 13:00:44 +02005188 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005189 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005190 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005191 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005192 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005193 return NULL;
5194 }
5195
5196 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005197
5198 Py_XDECREF(todecref);
5199
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005200 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005201 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005202 posp = getmark(mark, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005203 restore_buffer(&savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005204 if (VimTryEnd())
5205 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005206
5207 if (posp == NULL)
5208 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005209 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005210 return NULL;
5211 }
5212
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005213 if (posp->lnum <= 0)
5214 {
5215 /* Or raise an error? */
5216 Py_INCREF(Py_None);
5217 return Py_None;
5218 }
5219
5220 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5221}
5222
5223 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005224BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005225{
5226 PyInt start;
5227 PyInt end;
5228
Bram Moolenaard6e39182013-05-21 18:30:34 +02005229 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005230 return NULL;
5231
5232 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5233 return NULL;
5234
Bram Moolenaard6e39182013-05-21 18:30:34 +02005235 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005236}
5237
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005238 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005239BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005240{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005241 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005242 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005243 else
5244 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005245 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005246
5247 if (name == NULL)
5248 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005249
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005250 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005251 }
5252}
5253
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005254static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005255 /* name, function, calling, documentation */
5256 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005257 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005258 {"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 +02005259 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5260 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005261};
5262
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005263/*
5264 * Buffer list object - Implementation
5265 */
5266
5267static PyTypeObject BufMapType;
5268
5269typedef struct
5270{
5271 PyObject_HEAD
5272} BufMapObject;
5273
5274 static PyInt
5275BufMapLength(PyObject *self UNUSED)
5276{
5277 buf_T *b = firstbuf;
5278 PyInt n = 0;
5279
5280 while (b)
5281 {
5282 ++n;
5283 b = b->b_next;
5284 }
5285
5286 return n;
5287}
5288
5289 static PyObject *
5290BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5291{
5292 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005293 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005294
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005295 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005296 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005297
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005298 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005299
5300 if (b)
5301 return BufferNew(b);
5302 else
5303 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005304 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005305 return NULL;
5306 }
5307}
5308
5309 static void
5310BufMapIterDestruct(PyObject *buffer)
5311{
5312 /* Iteration was stopped before all buffers were processed */
5313 if (buffer)
5314 {
5315 Py_DECREF(buffer);
5316 }
5317}
5318
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005319 static int
5320BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5321{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005322 if (buffer)
5323 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005324 return 0;
5325}
5326
5327 static int
5328BufMapIterClear(PyObject **buffer)
5329{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005330 if (*buffer)
5331 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005332 return 0;
5333}
5334
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005335 static PyObject *
5336BufMapIterNext(PyObject **buffer)
5337{
5338 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005339 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005340
5341 if (!*buffer)
5342 return NULL;
5343
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005344 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005345
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005346 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005347 {
5348 *buffer = NULL;
5349 return NULL;
5350 }
5351
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005352 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005353 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005354 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005355 return NULL;
5356 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005357 /* Do not increment reference: we no longer hold it (decref), but whoever
5358 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005359 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005360}
5361
5362 static PyObject *
5363BufMapIter(PyObject *self UNUSED)
5364{
5365 PyObject *buffer;
5366
5367 buffer = BufferNew(firstbuf);
5368 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005369 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5370 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005371}
5372
5373static PyMappingMethods BufMapAsMapping = {
5374 (lenfunc) BufMapLength,
5375 (binaryfunc) BufMapItem,
5376 (objobjargproc) 0,
5377};
5378
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005379/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005380 */
5381
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005382static char *CurrentAttrs[] = {
5383 "buffer", "window", "line", "range", "tabpage",
5384 NULL
5385};
5386
5387 static PyObject *
5388CurrentDir(PyObject *self)
5389{
5390 return ObjectDir(self, CurrentAttrs);
5391}
5392
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005393 static PyObject *
5394CurrentGetattr(PyObject *self UNUSED, char *name)
5395{
5396 if (strcmp(name, "buffer") == 0)
5397 return (PyObject *)BufferNew(curbuf);
5398 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005399 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005400 else if (strcmp(name, "tabpage") == 0)
5401 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005402 else if (strcmp(name, "line") == 0)
5403 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5404 else if (strcmp(name, "range") == 0)
5405 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005406 else if (strcmp(name, "__members__") == 0)
5407 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005408 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005409#if PY_MAJOR_VERSION < 3
5410 return Py_FindMethod(WindowMethods, self, name);
5411#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005412 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005413#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005414}
5415
5416 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005417CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005418{
5419 if (strcmp(name, "line") == 0)
5420 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005421 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5422 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005423 return -1;
5424
5425 return 0;
5426 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005427 else if (strcmp(name, "buffer") == 0)
5428 {
5429 int count;
5430
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005431 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005432 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005433 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005434 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005435 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005436 return -1;
5437 }
5438
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005439 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005440 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005441 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005442
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005443 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005444 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5445 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005446 if (VimTryEnd())
5447 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005448 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005449 return -1;
5450 }
5451
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005452 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005453 }
5454 else if (strcmp(name, "window") == 0)
5455 {
5456 int count;
5457
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005458 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005459 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005460 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005461 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005462 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005463 return -1;
5464 }
5465
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005466 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005467 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005468 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005469
5470 if (!count)
5471 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005472 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005473 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005474 return -1;
5475 }
5476
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005477 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005478 win_goto(((WindowObject *)(valObject))->win);
5479 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005480 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005481 if (VimTryEnd())
5482 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005483 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005484 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005485 return -1;
5486 }
5487
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005488 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005489 }
5490 else if (strcmp(name, "tabpage") == 0)
5491 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005492 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005493 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005494 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005495 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005496 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005497 return -1;
5498 }
5499
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005500 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005501 return -1;
5502
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005503 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005504 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5505 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005506 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005507 if (VimTryEnd())
5508 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005509 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005510 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005511 return -1;
5512 }
5513
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005514 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005515 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005516 else
5517 {
5518 PyErr_SetString(PyExc_AttributeError, name);
5519 return -1;
5520 }
5521}
5522
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005523static struct PyMethodDef CurrentMethods[] = {
5524 /* name, function, calling, documentation */
5525 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5526 { NULL, NULL, 0, NULL}
5527};
5528
Bram Moolenaardb913952012-06-29 12:54:53 +02005529 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005530init_range_cmd(exarg_T *eap)
5531{
5532 RangeStart = eap->line1;
5533 RangeEnd = eap->line2;
5534}
5535
5536 static void
5537init_range_eval(typval_T *rettv UNUSED)
5538{
5539 RangeStart = (PyInt) curwin->w_cursor.lnum;
5540 RangeEnd = RangeStart;
5541}
5542
5543 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005544run_cmd(const char *cmd, void *arg UNUSED
5545#ifdef PY_CAN_RECURSE
5546 , PyGILState_STATE *pygilstate UNUSED
5547#endif
5548 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005549{
Bram Moolenaar41009372013-07-01 22:03:04 +02005550 PyObject *run_ret;
5551 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5552 if (run_ret != NULL)
5553 {
5554 Py_DECREF(run_ret);
5555 }
5556 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5557 {
5558 EMSG2(_(e_py_systemexit), "python");
5559 PyErr_Clear();
5560 }
5561 else
5562 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005563}
5564
5565static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5566static int code_hdr_len = 30;
5567
5568 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005569run_do(const char *cmd, void *arg UNUSED
5570#ifdef PY_CAN_RECURSE
5571 , PyGILState_STATE *pygilstate
5572#endif
5573 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005574{
5575 PyInt lnum;
5576 size_t len;
5577 char *code;
5578 int status;
5579 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005580 PyObject *run_ret;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005581 buf_T *was_curbuf = curbuf;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005582
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005583 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005584 {
5585 EMSG(_("cannot save undo information"));
5586 return;
5587 }
5588
5589 len = code_hdr_len + STRLEN(cmd);
5590 code = PyMem_New(char, len + 1);
5591 memcpy(code, code_hdr, code_hdr_len);
5592 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005593 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5594 status = -1;
5595 if (run_ret != NULL)
5596 {
5597 status = 0;
5598 Py_DECREF(run_ret);
5599 }
5600 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5601 {
5602 PyMem_Free(code);
5603 EMSG2(_(e_py_systemexit), "python");
5604 PyErr_Clear();
5605 return;
5606 }
5607 else
5608 PyErr_PrintEx(1);
5609
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005610 PyMem_Free(code);
5611
5612 if (status)
5613 {
5614 EMSG(_("failed to run the code"));
5615 return;
5616 }
5617
5618 status = 0;
5619 pymain = PyImport_AddModule("__main__");
5620 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005621#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005622 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005623#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005624
5625 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5626 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005627 PyObject *line;
5628 PyObject *linenr;
5629 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005630
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005631#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005632 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005633#endif
Bram Moolenaara58883b2017-01-29 21:31:09 +01005634 /* Check the line number, the command my have deleted lines. */
5635 if (lnum > curbuf->b_ml.ml_line_count
5636 || !(line = GetBufferLine(curbuf, lnum)))
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005637 goto err;
5638 if (!(linenr = PyInt_FromLong((long) lnum)))
5639 {
5640 Py_DECREF(line);
5641 goto err;
5642 }
5643 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5644 Py_DECREF(line);
5645 Py_DECREF(linenr);
5646 if (!ret)
5647 goto err;
5648
Bram Moolenaara58883b2017-01-29 21:31:09 +01005649 /* Check that the command didn't switch to another buffer. */
5650 if (curbuf != was_curbuf)
5651 {
5652 Py_XDECREF(ret);
5653 goto err;
5654 }
5655
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005656 if (ret != Py_None)
5657 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Bram Moolenaara58883b2017-01-29 21:31:09 +01005658 {
5659 Py_XDECREF(ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005660 goto err;
Bram Moolenaara58883b2017-01-29 21:31:09 +01005661 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005662
5663 Py_XDECREF(ret);
5664 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005665#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005666 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005667#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005668 }
5669 goto out;
5670err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005671#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005672 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005673#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005674 PyErr_PrintEx(0);
5675 PythonIO_Flush();
5676 status = 1;
5677out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005678#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005679 if (!status)
5680 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005681#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005682 Py_DECREF(pyfunc);
5683 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5684 if (status)
5685 return;
5686 check_cursor();
5687 update_curbuf(NOT_VALID);
5688}
5689
5690 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005691run_eval(const char *cmd, typval_T *rettv
5692#ifdef PY_CAN_RECURSE
5693 , PyGILState_STATE *pygilstate UNUSED
5694#endif
5695 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005696{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005697 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005698
Bram Moolenaar41009372013-07-01 22:03:04 +02005699 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005700 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005701 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005702 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005703 {
5704 EMSG2(_(e_py_systemexit), "python");
5705 PyErr_Clear();
5706 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005707 else
5708 {
5709 if (PyErr_Occurred() && !msg_silent)
5710 PyErr_PrintEx(0);
5711 EMSG(_("E858: Eval did not return a valid python object"));
5712 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005713 }
5714 else
5715 {
Bram Moolenaar77324fc2016-01-17 22:37:03 +01005716 if (run_ret != Py_None && ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005717 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005718 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005719 }
5720 PyErr_Clear();
5721}
5722
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005723 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005724set_ref_in_py(const int copyID)
5725{
5726 pylinkedlist_T *cur;
5727 dict_T *dd;
5728 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005729 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005730 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005731 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005732
5733 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005734 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005735 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005736 {
5737 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5738 if (dd->dv_copyID != copyID)
5739 {
5740 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005741 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005742 }
5743 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005744 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005745
5746 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005747 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005748 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005749 {
5750 ll = ((ListObject *) (cur->pll_obj))->list;
5751 if (ll->lv_copyID != copyID)
5752 {
5753 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005754 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005755 }
5756 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005757 }
5758
Bram Moolenaar8110a092016-04-14 15:56:09 +02005759 if (lastfunc != NULL)
5760 {
5761 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5762 {
5763 func = (FunctionObject *) cur->pll_obj;
5764 if (func->self != NULL && func->self->dv_copyID != copyID)
5765 {
5766 func->self->dv_copyID = copyID;
5767 abort = abort || set_ref_in_ht(
5768 &func->self->dv_hashtab, copyID, NULL);
5769 }
5770 if (func->argc)
5771 for (i = 0; !abort && i < func->argc; ++i)
5772 abort = abort
5773 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5774 }
5775 }
5776
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005777 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005778}
5779
5780 static int
5781set_string_copy(char_u *str, typval_T *tv)
5782{
5783 tv->vval.v_string = vim_strsave(str);
5784 if (tv->vval.v_string == NULL)
5785 {
5786 PyErr_NoMemory();
5787 return -1;
5788 }
5789 return 0;
5790}
5791
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005792 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005793pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005794{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005795 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005796 char_u *key;
5797 dictitem_T *di;
5798 PyObject *keyObject;
5799 PyObject *valObject;
5800 Py_ssize_t iter = 0;
5801
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005802 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005803 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005804
5805 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005806 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005807
5808 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5809 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005810 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005811
Bram Moolenaara03e6312013-05-29 22:49:26 +02005812 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005813 {
5814 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005815 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005816 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005817
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005818 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005819 {
5820 dict_unref(dict);
5821 return -1;
5822 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005823
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005824 if (*key == NUL)
5825 {
5826 dict_unref(dict);
5827 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005828 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005829 return -1;
5830 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005831
5832 di = dictitem_alloc(key);
5833
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005834 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005835
5836 if (di == NULL)
5837 {
5838 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005839 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005840 return -1;
5841 }
5842 di->di_tv.v_lock = 0;
5843
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005844 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005845 {
5846 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005847 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005848 return -1;
5849 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005850
5851 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005852 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005853 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005854 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005855 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005856 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005857 return -1;
5858 }
5859 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005860
5861 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005862 return 0;
5863}
5864
5865 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005866pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005867{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005868 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005869 char_u *key;
5870 dictitem_T *di;
5871 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005872 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005873 PyObject *keyObject;
5874 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005875
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005876 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005877 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005878
5879 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005880 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005881
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005882 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005883 {
5884 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005885 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005886 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005887
5888 if (!(iterator = PyObject_GetIter(list)))
5889 {
5890 dict_unref(dict);
5891 Py_DECREF(list);
5892 return -1;
5893 }
5894 Py_DECREF(list);
5895
5896 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005897 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005898 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005899
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005900 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005901 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005902 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005903 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005904 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005905 return -1;
5906 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005907
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005908 if (*key == NUL)
5909 {
5910 Py_DECREF(keyObject);
5911 Py_DECREF(iterator);
5912 Py_XDECREF(todecref);
5913 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005914 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005915 return -1;
5916 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005917
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005918 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005919 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005920 Py_DECREF(keyObject);
5921 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005922 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005923 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005924 return -1;
5925 }
5926
5927 di = dictitem_alloc(key);
5928
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005929 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005930 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005931
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005932 if (di == NULL)
5933 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005934 Py_DECREF(iterator);
5935 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005936 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005937 PyErr_NoMemory();
5938 return -1;
5939 }
5940 di->di_tv.v_lock = 0;
5941
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005942 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005943 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005944 Py_DECREF(iterator);
5945 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005946 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005947 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005948 return -1;
5949 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005950
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005951 Py_DECREF(valObject);
5952
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005953 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005954 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005955 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005956 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005957 dictitem_free(di);
5958 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005959 return -1;
5960 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005961 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005962 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005963 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005964 return 0;
5965}
5966
5967 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005968pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005969{
5970 list_T *l;
5971
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005972 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005973 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005974
5975 tv->v_type = VAR_LIST;
5976 tv->vval.v_list = l;
5977
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005978 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005979 {
5980 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005981 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005982 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005983
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005984 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005985 return 0;
5986}
5987
Bram Moolenaardb913952012-06-29 12:54:53 +02005988typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5989
5990 static int
5991convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005992 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005993{
5994 PyObject *capsule;
5995 char hexBuf[sizeof(void *) * 2 + 3];
5996
5997 sprintf(hexBuf, "%p", obj);
5998
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005999# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006000 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006001# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006002 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006003# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02006004 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006005 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006006# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02006007 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006008# else
6009 capsule = PyCObject_FromVoidPtr(tv, NULL);
6010# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006011 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6012 {
6013 Py_DECREF(capsule);
6014 tv->v_type = VAR_UNKNOWN;
6015 return -1;
6016 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006017
6018 Py_DECREF(capsule);
6019
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006020 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006021 {
6022 tv->v_type = VAR_UNKNOWN;
6023 return -1;
6024 }
6025 /* As we are not using copy_tv which increments reference count we must
6026 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006027 if (tv->v_type == VAR_DICT)
6028 ++tv->vval.v_dict->dv_refcount;
6029 else if (tv->v_type == VAR_LIST)
6030 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006031 }
6032 else
6033 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006034 typval_T *v;
6035
6036# ifdef PY_USE_CAPSULE
6037 v = PyCapsule_GetPointer(capsule, NULL);
6038# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006039 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006040# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006041 copy_tv(v, tv);
6042 }
6043 return 0;
6044}
6045
6046 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006047ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6048{
6049 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006050 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006051
6052 if (!(lookup_dict = PyDict_New()))
6053 return -1;
6054
6055 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6056 {
6057 tv->v_type = VAR_DICT;
6058 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6059 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006060 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006061 }
6062 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006063 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006064 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006065 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006066 else
6067 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006068 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006069 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006070 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006071 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006072 }
6073 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006074 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006075}
6076
6077 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006078ConvertFromPySequence(PyObject *obj, typval_T *tv)
6079{
6080 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006081 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006082
6083 if (!(lookup_dict = PyDict_New()))
6084 return -1;
6085
6086 if (PyType_IsSubtype(obj->ob_type, &ListType))
6087 {
6088 tv->v_type = VAR_LIST;
6089 tv->vval.v_list = (((ListObject *)(obj))->list);
6090 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006091 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006092 }
6093 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006094 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006095 else
6096 {
6097 PyErr_FORMAT(PyExc_TypeError,
6098 N_("unable to convert %s to vim list"),
6099 Py_TYPE_NAME(obj));
6100 ret = -1;
6101 }
6102 Py_DECREF(lookup_dict);
6103 return ret;
6104}
6105
6106 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006107ConvertFromPyObject(PyObject *obj, typval_T *tv)
6108{
6109 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006110 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006111
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006112 if (!(lookup_dict = PyDict_New()))
6113 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006114 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006115 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006116 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006117}
6118
6119 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006120_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006121{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006122 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006123 {
6124 tv->v_type = VAR_DICT;
6125 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6126 ++tv->vval.v_dict->dv_refcount;
6127 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006128 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006129 {
6130 tv->v_type = VAR_LIST;
6131 tv->vval.v_list = (((ListObject *)(obj))->list);
6132 ++tv->vval.v_list->lv_refcount;
6133 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006134 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006135 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006136 FunctionObject *func = (FunctionObject *) obj;
6137 if (func->self != NULL || func->argv != NULL)
6138 {
6139 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6140 set_partial(func, pt, TRUE);
6141 tv->vval.v_partial = pt;
6142 tv->v_type = VAR_PARTIAL;
6143 }
6144 else
6145 {
6146 if (set_string_copy(func->name, tv) == -1)
6147 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006148
Bram Moolenaar8110a092016-04-14 15:56:09 +02006149 tv->v_type = VAR_FUNC;
6150 }
6151 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006152 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006153 else if (PyBytes_Check(obj))
6154 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006155 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006156
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006157 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006158 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006159 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006160 return -1;
6161
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006162 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006163 return -1;
6164
6165 tv->v_type = VAR_STRING;
6166 }
6167 else if (PyUnicode_Check(obj))
6168 {
6169 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006170 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006171
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006172 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006173 if (bytes == NULL)
6174 return -1;
6175
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006176 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006177 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006178 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006179 return -1;
6180
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006181 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006182 {
6183 Py_XDECREF(bytes);
6184 return -1;
6185 }
6186 Py_XDECREF(bytes);
6187
6188 tv->v_type = VAR_STRING;
6189 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006190#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006191 else if (PyInt_Check(obj))
6192 {
6193 tv->v_type = VAR_NUMBER;
6194 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006195 if (PyErr_Occurred())
6196 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006197 }
6198#endif
6199 else if (PyLong_Check(obj))
6200 {
6201 tv->v_type = VAR_NUMBER;
6202 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006203 if (PyErr_Occurred())
6204 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006205 }
6206 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006207 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006208#ifdef FEAT_FLOAT
6209 else if (PyFloat_Check(obj))
6210 {
6211 tv->v_type = VAR_FLOAT;
6212 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6213 }
6214#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006215 else if (PyObject_HasAttrString(obj, "keys"))
6216 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006217 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006218 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006219 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006220 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006221 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006222 else if (PyNumber_Check(obj))
6223 {
6224 PyObject *num;
6225
6226 if (!(num = PyNumber_Long(obj)))
6227 return -1;
6228
6229 tv->v_type = VAR_NUMBER;
6230 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6231
6232 Py_DECREF(num);
6233 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006234 else
6235 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006236 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006237 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006238 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006239 return -1;
6240 }
6241 return 0;
6242}
6243
6244 static PyObject *
6245ConvertToPyObject(typval_T *tv)
6246{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006247 typval_T *argv;
6248 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006249 if (tv == NULL)
6250 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006251 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006252 return NULL;
6253 }
6254 switch (tv->v_type)
6255 {
6256 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006257 return PyBytes_FromString(tv->vval.v_string == NULL
6258 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 case VAR_NUMBER:
6260 return PyLong_FromLong((long) tv->vval.v_number);
6261#ifdef FEAT_FLOAT
6262 case VAR_FLOAT:
6263 return PyFloat_FromDouble((double) tv->vval.v_float);
6264#endif
6265 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006266 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006267 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006268 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006269 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006270 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006271 ? (char_u *)"" : tv->vval.v_string,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006272 0, NULL, NULL, TRUE);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006273 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006274 if (tv->vval.v_partial->pt_argc)
6275 {
6276 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6277 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6278 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6279 }
6280 else
6281 argv = NULL;
6282 if (tv->vval.v_partial->pt_dict != NULL)
6283 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006284 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006285 ? (char_u *)"" : partial_name(tv->vval.v_partial),
Bram Moolenaar8110a092016-04-14 15:56:09 +02006286 tv->vval.v_partial->pt_argc, argv,
Bram Moolenaar2177f9f2016-05-25 20:39:09 +02006287 tv->vval.v_partial->pt_dict,
6288 tv->vval.v_partial->pt_auto);
Bram Moolenaardb913952012-06-29 12:54:53 +02006289 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006290 case VAR_CHANNEL:
6291 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006292 Py_INCREF(Py_None);
6293 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006294 case VAR_SPECIAL:
6295 switch (tv->vval.v_number)
6296 {
6297 case VVAL_FALSE: return AlwaysFalse(NULL);
6298 case VVAL_TRUE: return AlwaysTrue(NULL);
6299 case VVAL_NONE:
6300 case VVAL_NULL: return AlwaysNone(NULL);
6301 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006302 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006303 return NULL;
6304 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006305 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006306}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006307
6308typedef struct
6309{
6310 PyObject_HEAD
6311} CurrentObject;
6312static PyTypeObject CurrentType;
6313
6314 static void
6315init_structs(void)
6316{
6317 vim_memset(&OutputType, 0, sizeof(OutputType));
6318 OutputType.tp_name = "vim.message";
6319 OutputType.tp_basicsize = sizeof(OutputObject);
6320 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6321 OutputType.tp_doc = "vim message object";
6322 OutputType.tp_methods = OutputMethods;
6323#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006324 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6325 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006326 OutputType.tp_alloc = call_PyType_GenericAlloc;
6327 OutputType.tp_new = call_PyType_GenericNew;
6328 OutputType.tp_free = call_PyObject_Free;
6329#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006330 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6331 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006332#endif
6333
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006334 vim_memset(&IterType, 0, sizeof(IterType));
6335 IterType.tp_name = "vim.iter";
6336 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006337 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006338 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006339 IterType.tp_iter = (getiterfunc)IterIter;
6340 IterType.tp_iternext = (iternextfunc)IterNext;
6341 IterType.tp_dealloc = (destructor)IterDestructor;
6342 IterType.tp_traverse = (traverseproc)IterTraverse;
6343 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006344
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006345 vim_memset(&BufferType, 0, sizeof(BufferType));
6346 BufferType.tp_name = "vim.buffer";
6347 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006348 BufferType.tp_dealloc = (destructor)BufferDestructor;
6349 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006350 BufferType.tp_as_sequence = &BufferAsSeq;
6351 BufferType.tp_as_mapping = &BufferAsMapping;
6352 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6353 BufferType.tp_doc = "vim buffer object";
6354 BufferType.tp_methods = BufferMethods;
6355#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006356 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006357 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006358 BufferType.tp_alloc = call_PyType_GenericAlloc;
6359 BufferType.tp_new = call_PyType_GenericNew;
6360 BufferType.tp_free = call_PyObject_Free;
6361#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006362 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006363 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006364#endif
6365
6366 vim_memset(&WindowType, 0, sizeof(WindowType));
6367 WindowType.tp_name = "vim.window";
6368 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006369 WindowType.tp_dealloc = (destructor)WindowDestructor;
6370 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006371 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006372 WindowType.tp_doc = "vim Window object";
6373 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006374 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6375 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006376#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006377 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6378 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006379 WindowType.tp_alloc = call_PyType_GenericAlloc;
6380 WindowType.tp_new = call_PyType_GenericNew;
6381 WindowType.tp_free = call_PyObject_Free;
6382#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006383 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6384 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006385#endif
6386
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006387 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6388 TabPageType.tp_name = "vim.tabpage";
6389 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006390 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6391 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006392 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6393 TabPageType.tp_doc = "vim tab page object";
6394 TabPageType.tp_methods = TabPageMethods;
6395#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006396 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006397 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6398 TabPageType.tp_new = call_PyType_GenericNew;
6399 TabPageType.tp_free = call_PyObject_Free;
6400#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006401 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006402#endif
6403
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006404 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6405 BufMapType.tp_name = "vim.bufferlist";
6406 BufMapType.tp_basicsize = sizeof(BufMapObject);
6407 BufMapType.tp_as_mapping = &BufMapAsMapping;
6408 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006409 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006410 BufferType.tp_doc = "vim buffer list";
6411
6412 vim_memset(&WinListType, 0, sizeof(WinListType));
6413 WinListType.tp_name = "vim.windowlist";
6414 WinListType.tp_basicsize = sizeof(WinListType);
6415 WinListType.tp_as_sequence = &WinListAsSeq;
6416 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6417 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006418 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006419
6420 vim_memset(&TabListType, 0, sizeof(TabListType));
6421 TabListType.tp_name = "vim.tabpagelist";
6422 TabListType.tp_basicsize = sizeof(TabListType);
6423 TabListType.tp_as_sequence = &TabListAsSeq;
6424 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6425 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006426
6427 vim_memset(&RangeType, 0, sizeof(RangeType));
6428 RangeType.tp_name = "vim.range";
6429 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006430 RangeType.tp_dealloc = (destructor)RangeDestructor;
6431 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006432 RangeType.tp_as_sequence = &RangeAsSeq;
6433 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006434 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006435 RangeType.tp_doc = "vim Range object";
6436 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006437 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6438 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006439#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006440 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006441 RangeType.tp_alloc = call_PyType_GenericAlloc;
6442 RangeType.tp_new = call_PyType_GenericNew;
6443 RangeType.tp_free = call_PyObject_Free;
6444#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006445 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006446#endif
6447
6448 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6449 CurrentType.tp_name = "vim.currentdata";
6450 CurrentType.tp_basicsize = sizeof(CurrentObject);
6451 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6452 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006453 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006454#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006455 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6456 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006457#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006458 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6459 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006460#endif
6461
6462 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6463 DictionaryType.tp_name = "vim.dictionary";
6464 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006465 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006466 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006467 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006468 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006469 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6470 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006471 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6472 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6473 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006474#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006475 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6476 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006477#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006478 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6479 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006480#endif
6481
6482 vim_memset(&ListType, 0, sizeof(ListType));
6483 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006484 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006485 ListType.tp_basicsize = sizeof(ListObject);
6486 ListType.tp_as_sequence = &ListAsSeq;
6487 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006488 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006489 ListType.tp_doc = "list pushing modifications to vim structure";
6490 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006491 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006492 ListType.tp_new = (newfunc)ListConstructor;
6493 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006494#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006495 ListType.tp_getattro = (getattrofunc)ListGetattro;
6496 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006497#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006498 ListType.tp_getattr = (getattrfunc)ListGetattr;
6499 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006500#endif
6501
6502 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006503 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006504 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006505 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6506 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006507 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006508 FunctionType.tp_doc = "object that calls vim function";
6509 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006510 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006511 FunctionType.tp_new = (newfunc)FunctionConstructor;
6512 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006513#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006514 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006515#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006516 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006517#endif
6518
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006519 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6520 OptionsType.tp_name = "vim.options";
6521 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006522 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006523 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006524 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006525 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006526 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006527 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6528 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6529 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006530
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006531 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6532 LoaderType.tp_name = "vim.Loader";
6533 LoaderType.tp_basicsize = sizeof(LoaderObject);
6534 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6535 LoaderType.tp_doc = "vim message object";
6536 LoaderType.tp_methods = LoaderMethods;
6537 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6538
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006539#if PY_MAJOR_VERSION >= 3
6540 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6541 vimmodule.m_name = "vim";
6542 vimmodule.m_doc = "Vim Python interface\n";
6543 vimmodule.m_size = -1;
6544 vimmodule.m_methods = VimMethods;
6545#endif
6546}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006547
6548#define PYTYPE_READY(type) \
6549 if (PyType_Ready(&type)) \
6550 return -1;
6551
6552 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006553init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006554{
6555 PYTYPE_READY(IterType);
6556 PYTYPE_READY(BufferType);
6557 PYTYPE_READY(RangeType);
6558 PYTYPE_READY(WindowType);
6559 PYTYPE_READY(TabPageType);
6560 PYTYPE_READY(BufMapType);
6561 PYTYPE_READY(WinListType);
6562 PYTYPE_READY(TabListType);
6563 PYTYPE_READY(CurrentType);
6564 PYTYPE_READY(DictionaryType);
6565 PYTYPE_READY(ListType);
6566 PYTYPE_READY(FunctionType);
6567 PYTYPE_READY(OptionsType);
6568 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006569 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006570 return 0;
6571}
6572
6573 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006574init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006575{
6576 PyObject *path;
6577 PyObject *path_hook;
6578 PyObject *path_hooks;
6579
6580 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6581 return -1;
6582
6583 if (!(path_hooks = PySys_GetObject("path_hooks")))
6584 {
6585 PyErr_Clear();
6586 path_hooks = PyList_New(1);
6587 PyList_SET_ITEM(path_hooks, 0, path_hook);
6588 if (PySys_SetObject("path_hooks", path_hooks))
6589 {
6590 Py_DECREF(path_hooks);
6591 return -1;
6592 }
6593 Py_DECREF(path_hooks);
6594 }
6595 else if (PyList_Check(path_hooks))
6596 {
6597 if (PyList_Append(path_hooks, path_hook))
6598 {
6599 Py_DECREF(path_hook);
6600 return -1;
6601 }
6602 Py_DECREF(path_hook);
6603 }
6604 else
6605 {
6606 VimTryStart();
6607 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6608 "You should now do the following:\n"
6609 "- append vim.path_hook to sys.path_hooks\n"
6610 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6611 VimTryEnd(); /* Discard the error */
6612 Py_DECREF(path_hook);
6613 return 0;
6614 }
6615
6616 if (!(path = PySys_GetObject("path")))
6617 {
6618 PyErr_Clear();
6619 path = PyList_New(1);
6620 Py_INCREF(vim_special_path_object);
6621 PyList_SET_ITEM(path, 0, vim_special_path_object);
6622 if (PySys_SetObject("path", path))
6623 {
6624 Py_DECREF(path);
6625 return -1;
6626 }
6627 Py_DECREF(path);
6628 }
6629 else if (PyList_Check(path))
6630 {
6631 if (PyList_Append(path, vim_special_path_object))
6632 return -1;
6633 }
6634 else
6635 {
6636 VimTryStart();
6637 EMSG(_("Failed to set path: sys.path is not a list\n"
6638 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6639 VimTryEnd(); /* Discard the error */
6640 }
6641
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006642 return 0;
6643}
6644
6645static BufMapObject TheBufferMap =
6646{
6647 PyObject_HEAD_INIT(&BufMapType)
6648};
6649
6650static WinListObject TheWindowList =
6651{
6652 PyObject_HEAD_INIT(&WinListType)
6653 NULL
6654};
6655
6656static CurrentObject TheCurrent =
6657{
6658 PyObject_HEAD_INIT(&CurrentType)
6659};
6660
6661static TabListObject TheTabPageList =
6662{
6663 PyObject_HEAD_INIT(&TabListType)
6664};
6665
6666static struct numeric_constant {
6667 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006668 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006669} numeric_constants[] = {
6670 {"VAR_LOCKED", VAR_LOCKED},
6671 {"VAR_FIXED", VAR_FIXED},
6672 {"VAR_SCOPE", VAR_SCOPE},
6673 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6674};
6675
6676static struct object_constant {
6677 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006678 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006679} object_constants[] = {
6680 {"buffers", (PyObject *)(void *)&TheBufferMap},
6681 {"windows", (PyObject *)(void *)&TheWindowList},
6682 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6683 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006684
6685 {"Buffer", (PyObject *)&BufferType},
6686 {"Range", (PyObject *)&RangeType},
6687 {"Window", (PyObject *)&WindowType},
6688 {"TabPage", (PyObject *)&TabPageType},
6689 {"Dictionary", (PyObject *)&DictionaryType},
6690 {"List", (PyObject *)&ListType},
6691 {"Function", (PyObject *)&FunctionType},
6692 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006693 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006694};
6695
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006696#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006697 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006698 return -1;
6699
6700#define ADD_CHECKED_OBJECT(m, name, obj) \
6701 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006702 PyObject *valObject = obj; \
6703 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006704 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006705 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006706 }
6707
6708 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006709populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006710{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006711 int i;
6712 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006713 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006714 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006715
6716 for (i = 0; i < (int)(sizeof(numeric_constants)
6717 / sizeof(struct numeric_constant));
6718 ++i)
6719 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006720 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006721
6722 for (i = 0; i < (int)(sizeof(object_constants)
6723 / sizeof(struct object_constant));
6724 ++i)
6725 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006726 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006727
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006728 valObject = object_constants[i].valObject;
6729 Py_INCREF(valObject);
6730 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006731 }
6732
6733 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6734 return -1;
6735 ADD_OBJECT(m, "error", VimError);
6736
Bram Moolenaara9922d62013-05-30 13:01:18 +02006737 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6738 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006739 ADD_CHECKED_OBJECT(m, "options",
6740 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006741
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006742 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006743 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006744 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006745
Bram Moolenaar22081f42016-06-01 20:38:34 +02006746#if PY_MAJOR_VERSION >= 3
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006747 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006748 return -1;
Bram Moolenaar22081f42016-06-01 20:38:34 +02006749#else
6750 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwdu")))
6751 return -1;
6752#endif
Bram Moolenaarf4258302013-06-02 18:20:17 +02006753 ADD_OBJECT(m, "_getcwd", py_getcwd)
6754
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006755 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006756 return -1;
6757 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006758 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006759 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006760 if (PyObject_SetAttrString(other_module, "chdir", attr))
6761 {
6762 Py_DECREF(attr);
6763 return -1;
6764 }
6765 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006766
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006767 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006768 {
6769 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006770 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006771 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006772 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6773 {
6774 Py_DECREF(attr);
6775 return -1;
6776 }
6777 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006778 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006779 else
6780 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006781
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006782 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6783 return -1;
6784
6785 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6786
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006787 if (!(imp = PyImport_ImportModule("imp")))
6788 return -1;
6789
6790 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6791 {
6792 Py_DECREF(imp);
6793 return -1;
6794 }
6795
6796 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6797 {
6798 Py_DECREF(py_find_module);
6799 Py_DECREF(imp);
6800 return -1;
6801 }
6802
6803 Py_DECREF(imp);
6804
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006805 ADD_OBJECT(m, "_find_module", py_find_module);
6806 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006807
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006808 return 0;
6809}