blob: c6a8c44635513c563583d46793c8aeea876c2300 [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaar78cf3f02014-01-10 18:16:07 +010016#ifdef __BORLANDC__
17/* Disable Warning W8060: Possibly incorrect assignment in function ... */
18# pragma warn -8060
19#endif
20
Bram Moolenaar41009372013-07-01 22:03:04 +020021static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
22
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020023#if PY_VERSION_HEX < 0x02050000
24typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
25#endif
26
Bram Moolenaar91805fc2011-06-26 04:01:44 +020027#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020028# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020029#else
30# define ENC_OPT "latin1"
31#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020032#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020033
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020034static const char *vim_special_path = "_vim_path_";
35
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020036#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020038#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010039#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
40#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
41#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020042
43#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
44 ? "(NULL)" \
45 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020046
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020047#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 N_("empty keys are not allowed"))
49#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
50#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
51#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
52#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
53#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
54#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020055#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020056 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020057#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020058 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020059 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020061#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
62#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020063#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020065typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020066typedef void (*runner)(const char *, void *
67#ifdef PY_CAN_RECURSE
68 , PyGILState_STATE *
69#endif
70 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020071
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020072static int ConvertFromPyObject(PyObject *, typval_T *);
73static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020074static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaar8110a092016-04-14 15:56:09 +020075static int ConvertFromPySequence(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020076static PyObject *WindowNew(win_T *, tabpage_T *);
77static PyObject *BufferNew (buf_T *);
78static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020079
80static PyInt RangeStart;
81static PyInt RangeEnd;
82
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020083static PyObject *globals;
84
Bram Moolenaarf4258302013-06-02 18:20:17 +020085static PyObject *py_chdir;
86static PyObject *py_fchdir;
87static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020088static PyObject *vim_module;
89static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020090
Bram Moolenaar81c40c52013-06-12 14:41:04 +020091static PyObject *py_find_module;
92static PyObject *py_load_module;
93
94static PyObject *VimError;
95
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020096/*
97 * obtain a lock on the Vim data structures
98 */
99 static void
100Python_Lock_Vim(void)
101{
102}
103
104/*
105 * release a lock on the Vim data structures
106 */
107 static void
108Python_Release_Vim(void)
109{
110}
111
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200112/*
113 * The "todecref" argument holds a pointer to PyObject * that must be
114 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
115 * was needed to generate returned value is object.
116 *
117 * Use Py_XDECREF to decrement reference count.
118 */
119 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200120StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200121{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200122 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200123
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200124 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200125 {
126
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200127 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
128 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200129 return NULL;
130
131 *todecref = NULL;
132 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200133 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200134 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200135 PyObject *bytes;
136
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200137 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200138 return NULL;
139
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200140 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
141 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200142 {
143 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200144 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200145 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200146
147 *todecref = bytes;
148 }
149 else
150 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200151#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200152 PyErr_FORMAT(PyExc_TypeError,
153 N_("expected str() or unicode() instance, but got %s"),
154 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200155#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200156 PyErr_FORMAT(PyExc_TypeError,
157 N_("expected bytes() or str() instance, but got %s"),
158 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200159#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200160 return NULL;
161 }
162
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200163 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200164}
165
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200166#define NUMBER_LONG 1
167#define NUMBER_INT 2
168#define NUMBER_NATURAL 4
169#define NUMBER_UNSIGNED 8
170
171 static int
172NumberToLong(PyObject *obj, long *result, int flags)
173{
174#if PY_MAJOR_VERSION < 3
175 if (PyInt_Check(obj))
176 {
177 *result = PyInt_AsLong(obj);
178 if (PyErr_Occurred())
179 return -1;
180 }
181 else
182#endif
183 if (PyLong_Check(obj))
184 {
185 *result = PyLong_AsLong(obj);
186 if (PyErr_Occurred())
187 return -1;
188 }
189 else if (PyNumber_Check(obj))
190 {
191 PyObject *num;
192
193 if (!(num = PyNumber_Long(obj)))
194 return -1;
195
196 *result = PyLong_AsLong(num);
197
198 Py_DECREF(num);
199
200 if (PyErr_Occurred())
201 return -1;
202 }
203 else
204 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200205#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200206 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200207 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200208 "coercing to long(), but got %s"),
209 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200210#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200211 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200212 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200213 "but got %s"),
214 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200215#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200216 return -1;
217 }
218
219 if (flags & NUMBER_INT)
220 {
221 if (*result > INT_MAX)
222 {
223 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200224 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200225 return -1;
226 }
227 else if (*result < INT_MIN)
228 {
229 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200230 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200231 return -1;
232 }
233 }
234
235 if (flags & NUMBER_NATURAL)
236 {
237 if (*result <= 0)
238 {
239 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100240 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200241 return -1;
242 }
243 }
244 else if (flags & NUMBER_UNSIGNED)
245 {
246 if (*result < 0)
247 {
248 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200249 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200250 return -1;
251 }
252 }
253
254 return 0;
255}
256
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200257 static int
258add_string(PyObject *list, char *s)
259{
260 PyObject *string;
261
262 if (!(string = PyString_FromString(s)))
263 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200264
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200265 if (PyList_Append(list, string))
266 {
267 Py_DECREF(string);
268 return -1;
269 }
270
271 Py_DECREF(string);
272 return 0;
273}
274
275 static PyObject *
276ObjectDir(PyObject *self, char **attributes)
277{
278 PyMethodDef *method;
279 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200280 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200281
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200282 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200283 return NULL;
284
285 if (self)
286 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200287 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200288 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200289 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200290 return NULL;
291 }
292
293 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200294 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200295 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200296 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200297 return NULL;
298 }
299
300#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200301 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200302 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200303 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200304 return NULL;
305 }
306#endif
307
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200308 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200309}
310
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200311/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200312 */
313
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200314/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200315typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200316
317static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200318
319typedef struct
320{
321 PyObject_HEAD
322 long softspace;
323 long error;
324} OutputObject;
325
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200326static char *OutputAttrs[] = {
327 "softspace",
328 NULL
329};
330
331 static PyObject *
332OutputDir(PyObject *self)
333{
334 return ObjectDir(self, OutputAttrs);
335}
336
Bram Moolenaar77045652012-09-21 13:46:06 +0200337 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200338OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200339{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200340 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200341 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200342 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200343 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200344 return -1;
345 }
346
347 if (strcmp(name, "softspace") == 0)
348 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200349 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200351 return 0;
352 }
353
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200354 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200355 return -1;
356}
357
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200358/* Buffer IO, we write one whole line at a time. */
359static garray_T io_ga = {0, 0, 1, 80, NULL};
360static writefn old_fn = NULL;
361
362 static void
363PythonIO_Flush(void)
364{
365 if (old_fn != NULL && io_ga.ga_len > 0)
366 {
367 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
368 old_fn((char_u *)io_ga.ga_data);
369 }
370 io_ga.ga_len = 0;
371}
372
373 static void
374writer(writefn fn, char_u *str, PyInt n)
375{
376 char_u *ptr;
377
378 /* Flush when switching output function. */
379 if (fn != old_fn)
380 PythonIO_Flush();
381 old_fn = fn;
382
383 /* Write each NL separated line. Text after the last NL is kept for
384 * writing later. */
385 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
386 {
387 PyInt len = ptr - str;
388
389 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
390 break;
391
392 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
393 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
394 fn((char_u *)io_ga.ga_data);
395 str = ptr + 1;
396 n -= len + 1;
397 io_ga.ga_len = 0;
398 }
399
400 /* Put the remaining text into io_ga for later printing. */
401 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
402 {
403 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
404 io_ga.ga_len += (int)n;
405 }
406}
407
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200408 static int
409write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200411 Py_ssize_t len = 0;
412 char *str = NULL;
413 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200414
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200415 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
416 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200417
418 Py_BEGIN_ALLOW_THREADS
419 Python_Lock_Vim();
420 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
421 Python_Release_Vim();
422 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200423 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200424
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200425 return 0;
426}
427
428 static PyObject *
429OutputWrite(OutputObject *self, PyObject *string)
430{
431 if (write_output(self, string))
432 return NULL;
433
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200434 Py_INCREF(Py_None);
435 return Py_None;
436}
437
438 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200439OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200441 PyObject *iterator;
442 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200444 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200447 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200448 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200449 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200450 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200451 Py_DECREF(iterator);
452 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200453 return NULL;
454 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200455 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200456 }
457
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200458 Py_DECREF(iterator);
459
460 /* Iterator may have finished due to an exception */
461 if (PyErr_Occurred())
462 return NULL;
463
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200464 Py_INCREF(Py_None);
465 return Py_None;
466}
467
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100468 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100469AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100470{
471 /* do nothing */
472 Py_INCREF(Py_None);
473 return Py_None;
474}
475
Bram Moolenaard4247472015-11-02 13:28:59 +0100476 static PyObject *
477AlwaysFalse(PyObject *self UNUSED)
478{
479 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100480 PyObject *ret = Py_False;
481 Py_INCREF(ret);
482 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100483}
484
485 static PyObject *
486AlwaysTrue(PyObject *self UNUSED)
487{
488 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100489 PyObject *ret = Py_True;
490 Py_INCREF(ret);
491 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100492}
493
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200494/***************/
495
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200496static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200497 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200498 {"write", (PyCFunction)OutputWrite, METH_O, ""},
499 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100500 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
501 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
502 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
503 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
504 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
505 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200506 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200507 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200508};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200509
510static OutputObject Output =
511{
512 PyObject_HEAD_INIT(&OutputType)
513 0,
514 0
515};
516
517static OutputObject Error =
518{
519 PyObject_HEAD_INIT(&OutputType)
520 0,
521 1
522};
523
524 static int
525PythonIO_Init_io(void)
526{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200527 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
528 return -1;
529 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
530 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200531
532 if (PyErr_Occurred())
533 {
534 EMSG(_("E264: Python: Error initialising I/O objects"));
535 return -1;
536 }
537
538 return 0;
539}
540
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200541typedef struct
542{
543 PyObject_HEAD
544 PyObject *module;
545} LoaderObject;
546static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200547
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200548 static void
549LoaderDestructor(LoaderObject *self)
550{
551 Py_DECREF(self->module);
552 DESTRUCTOR_FINISH(self);
553}
554
555 static PyObject *
556LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
557{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200558 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200559
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200560 Py_INCREF(ret);
561 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200562}
563
564static struct PyMethodDef LoaderMethods[] = {
565 /* name, function, calling, doc */
566 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
567 { NULL, NULL, 0, NULL}
568};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200569
570/* Check to see whether a Vim error has been reported, or a keyboard
571 * interrupt has been detected.
572 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200573
574 static void
575VimTryStart(void)
576{
577 ++trylevel;
578}
579
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200580 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200581VimTryEnd(void)
582{
583 --trylevel;
Bram Moolenaar41009372013-07-01 22:03:04 +0200584 /* Without this it stops processing all subsequent VimL commands and
585 * generates strange error messages if I e.g. try calling Test() in a
586 * cycle */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200587 did_emsg = FALSE;
588 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200589 if (got_int)
590 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100591 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100592 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100593 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200594 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200595 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200596 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100597 else if (msg_list != NULL && *msg_list != NULL)
598 {
599 int should_free;
600 char_u *msg;
601
602 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
603
604 if (msg == NULL)
605 {
606 PyErr_NoMemory();
607 return -1;
608 }
609
610 PyErr_SetVim((char *) msg);
611
612 free_global_msglist();
613
614 if (should_free)
615 vim_free(msg);
616
617 return -1;
618 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200619 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200620 return (PyErr_Occurred() ? -1 : 0);
621 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200622 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200623 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100624 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200625 return -1;
626 }
627 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200628 else
629 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200630 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200631 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200632 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200633 }
634}
635
636 static int
637VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200638{
639 if (got_int)
640 {
641 PyErr_SetNone(PyExc_KeyboardInterrupt);
642 return 1;
643 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200644 return 0;
645}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200646
647/* Vim module - Implementation
648 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200649
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200650 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200651VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200652{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200653 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200654 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200655 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200656
Bram Moolenaar389a1792013-06-23 13:00:44 +0200657 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200658 return NULL;
659
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200660 Py_BEGIN_ALLOW_THREADS
661 Python_Lock_Vim();
662
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200663 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200664 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200665 update_screen(VALID);
666
667 Python_Release_Vim();
668 Py_END_ALLOW_THREADS
669
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200670 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200671 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200672 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200673 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200674
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200675 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200676 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200677 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200678}
679
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200680/*
681 * Function to translate a typval_T into a PyObject; this will recursively
682 * translate lists/dictionaries into their Python equivalents.
683 *
684 * The depth parameter is to avoid infinite recursion, set it to 1 when
685 * you call VimToPython.
686 */
687 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200688VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200689{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200690 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200691 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200692 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693
694 /* Avoid infinite recursion */
695 if (depth > 100)
696 {
697 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200698 ret = Py_None;
699 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200700 }
701
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200702 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200703 * then and we can use it again. */
704 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
705 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
706 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200707 sprintf(ptrBuf, "%p",
708 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
709 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200710
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200711 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200712 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200713 Py_INCREF(ret);
714 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200715 }
716 }
717
718 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200719 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200720 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200721 else if (our_tv->v_type == VAR_NUMBER)
722 {
723 char buf[NUMBUFLEN];
724
725 /* For backwards compatibility numbers are stored as strings. */
726 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200727 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200728 }
729# ifdef FEAT_FLOAT
730 else if (our_tv->v_type == VAR_FLOAT)
731 {
732 char buf[NUMBUFLEN];
733
734 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200735 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200736 }
737# endif
738 else if (our_tv->v_type == VAR_LIST)
739 {
740 list_T *list = our_tv->vval.v_list;
741 listitem_T *curr;
742
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200743 if (list == NULL)
744 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200745
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200746 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200747 return NULL;
748
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200749 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200750 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200751 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200752 return NULL;
753 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200754
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200755 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
756 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200757 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200758 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200759 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200760 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200762 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200763 {
764 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200765 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200766 return NULL;
767 }
768 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200769 }
770 }
771 else if (our_tv->v_type == VAR_DICT)
772 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200773
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100774 hashtab_T *ht;
775 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200776 hashitem_T *hi;
777 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100778
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200779 if (our_tv->vval.v_dict == NULL)
780 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100781 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200782
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200783 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200784 return NULL;
785
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200786 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200787 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200788 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200789 return NULL;
790 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100792 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200793 for (hi = ht->ht_array; todo > 0; ++hi)
794 {
795 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200796 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200797 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200799 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200800 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200801 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200802 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200803 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200804 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200805 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200806 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200807 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200808 Py_DECREF(newObj);
809 return NULL;
810 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200811 }
812 }
813 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100814 else if (our_tv->v_type == VAR_SPECIAL)
815 {
816 if (our_tv->vval.v_number == VVAL_FALSE)
817 {
818 ret = Py_False;
819 Py_INCREF(ret);
820 }
821 else if (our_tv->vval.v_number == VVAL_TRUE)
822 {
823 ret = Py_True;
824 Py_INCREF(ret);
825 }
826 else
827 {
828 Py_INCREF(Py_None);
829 ret = Py_None;
830 }
831 return ret;
832 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200833 else
834 {
835 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200836 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200837 }
838
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200839 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200840}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200841
842 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200843VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200844{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200845 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200846 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200847 PyObject *string;
848 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200849 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200850 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200851
Bram Moolenaar389a1792013-06-23 13:00:44 +0200852 if (!PyArg_ParseTuple(args, "O", &string))
853 return NULL;
854
855 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200856 return NULL;
857
858 Py_BEGIN_ALLOW_THREADS
859 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200860 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200861 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200862 Python_Release_Vim();
863 Py_END_ALLOW_THREADS
864
Bram Moolenaar389a1792013-06-23 13:00:44 +0200865 Py_XDECREF(todecref);
866
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200867 if (VimTryEnd())
868 return NULL;
869
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200870 if (our_tv == NULL)
871 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200872 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200873 return NULL;
874 }
875
876 /* Convert the Vim type into a Python type. Create a dictionary that's
877 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200878 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200879 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200880 else
881 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200882 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200883 Py_DECREF(lookup_dict);
884 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200885
886
887 Py_BEGIN_ALLOW_THREADS
888 Python_Lock_Vim();
889 free_tv(our_tv);
890 Python_Release_Vim();
891 Py_END_ALLOW_THREADS
892
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200893 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200894}
895
Bram Moolenaardb913952012-06-29 12:54:53 +0200896static PyObject *ConvertToPyObject(typval_T *);
897
898 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200899VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200900{
Bram Moolenaardb913952012-06-29 12:54:53 +0200901 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200902 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200903 char_u *expr;
904 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200905
Bram Moolenaar389a1792013-06-23 13:00:44 +0200906 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200907 return NULL;
908
909 Py_BEGIN_ALLOW_THREADS
910 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200911 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200912 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200913 Python_Release_Vim();
914 Py_END_ALLOW_THREADS
915
Bram Moolenaar389a1792013-06-23 13:00:44 +0200916 Py_XDECREF(todecref);
917
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200918 if (VimTryEnd())
919 return NULL;
920
Bram Moolenaardb913952012-06-29 12:54:53 +0200921 if (our_tv == NULL)
922 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200923 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200924 return NULL;
925 }
926
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200927 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200928 Py_BEGIN_ALLOW_THREADS
929 Python_Lock_Vim();
930 free_tv(our_tv);
931 Python_Release_Vim();
932 Py_END_ALLOW_THREADS
933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200934 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200935}
936
937 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200938VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200939{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200940 char_u *str;
941 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200942 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200943
Bram Moolenaar389a1792013-06-23 13:00:44 +0200944 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200945 return NULL;
946
Bram Moolenaara54bf402012-12-05 16:30:07 +0100947#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200948 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100949#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200950 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100951#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200952
953 Py_XDECREF(todecref);
954
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200955 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200956}
957
Bram Moolenaarf4258302013-06-02 18:20:17 +0200958 static PyObject *
959_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
960{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200961 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200962 PyObject *newwd;
963 PyObject *todecref;
964 char_u *new_dir;
965
Bram Moolenaard4209d22013-06-05 20:34:15 +0200966 if (_chdir == NULL)
967 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200968 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200969 return NULL;
970
971 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
972 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200973 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200974 return NULL;
975 }
976
977 if (!(new_dir = StringToChars(newwd, &todecref)))
978 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200979 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200980 Py_DECREF(newwd);
981 return NULL;
982 }
983
984 VimTryStart();
985
986 if (vim_chdir(new_dir))
987 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200988 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200989 Py_DECREF(newwd);
990 Py_XDECREF(todecref);
991
992 if (VimTryEnd())
993 return NULL;
994
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200995 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200996 return NULL;
997 }
998
999 Py_DECREF(newwd);
1000 Py_XDECREF(todecref);
1001
1002 post_chdir(FALSE);
1003
1004 if (VimTryEnd())
1005 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001006 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001007 return NULL;
1008 }
1009
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001010 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001011}
1012
1013 static PyObject *
1014VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1015{
1016 return _VimChdir(py_chdir, args, kwargs);
1017}
1018
1019 static PyObject *
1020VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1021{
1022 return _VimChdir(py_fchdir, args, kwargs);
1023}
1024
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001025typedef struct {
1026 PyObject *callable;
1027 PyObject *result;
1028} map_rtp_data;
1029
1030 static void
1031map_rtp_callback(char_u *path, void *_data)
1032{
1033 void **data = (void **) _data;
1034 PyObject *pathObject;
1035 map_rtp_data *mr_data = *((map_rtp_data **) data);
1036
Bram Moolenaar41009372013-07-01 22:03:04 +02001037 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001038 {
1039 *data = NULL;
1040 return;
1041 }
1042
1043 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1044 pathObject, NULL);
1045
1046 Py_DECREF(pathObject);
1047
1048 if (!mr_data->result || mr_data->result != Py_None)
1049 *data = NULL;
1050 else
1051 {
1052 Py_DECREF(mr_data->result);
1053 mr_data->result = NULL;
1054 }
1055}
1056
1057 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001058VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001059{
1060 map_rtp_data data;
1061
Bram Moolenaar389a1792013-06-23 13:00:44 +02001062 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001063 data.result = NULL;
1064
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001065 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001066
1067 if (data.result == NULL)
1068 {
1069 if (PyErr_Occurred())
1070 return NULL;
1071 else
1072 {
1073 Py_INCREF(Py_None);
1074 return Py_None;
1075 }
1076 }
1077 return data.result;
1078}
1079
1080/*
1081 * _vim_runtimepath_ special path implementation.
1082 */
1083
1084 static void
1085map_finder_callback(char_u *path, void *_data)
1086{
1087 void **data = (void **) _data;
1088 PyObject *list = *((PyObject **) data);
1089 PyObject *pathObject1, *pathObject2;
1090 char *pathbuf;
1091 size_t pathlen;
1092
1093 pathlen = STRLEN(path);
1094
1095#if PY_MAJOR_VERSION < 3
1096# define PY_MAIN_DIR_STRING "python2"
1097#else
1098# define PY_MAIN_DIR_STRING "python3"
1099#endif
1100#define PY_ALTERNATE_DIR_STRING "pythonx"
1101
1102#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1103 if (!(pathbuf = PyMem_New(char,
1104 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1105 {
1106 PyErr_NoMemory();
1107 *data = NULL;
1108 return;
1109 }
1110
1111 mch_memmove(pathbuf, path, pathlen + 1);
1112 add_pathsep((char_u *) pathbuf);
1113
1114 pathlen = STRLEN(pathbuf);
1115 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1116 PYTHONX_STRING_LENGTH + 1);
1117
1118 if (!(pathObject1 = PyString_FromString(pathbuf)))
1119 {
1120 *data = NULL;
1121 PyMem_Free(pathbuf);
1122 return;
1123 }
1124
1125 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1126 PYTHONX_STRING_LENGTH + 1);
1127
1128 if (!(pathObject2 = PyString_FromString(pathbuf)))
1129 {
1130 Py_DECREF(pathObject1);
1131 PyMem_Free(pathbuf);
1132 *data = NULL;
1133 return;
1134 }
1135
1136 PyMem_Free(pathbuf);
1137
1138 if (PyList_Append(list, pathObject1)
1139 || PyList_Append(list, pathObject2))
1140 *data = NULL;
1141
1142 Py_DECREF(pathObject1);
1143 Py_DECREF(pathObject2);
1144}
1145
1146 static PyObject *
1147Vim_GetPaths(PyObject *self UNUSED)
1148{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001149 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001150
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001151 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001152 return NULL;
1153
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001154 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001155
1156 if (PyErr_Occurred())
1157 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001158 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001159 return NULL;
1160 }
1161
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001162 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001163}
1164
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001165 static PyObject *
1166call_load_module(char *name, int len, PyObject *find_module_result)
1167{
1168 PyObject *fd, *pathname, *description;
1169
Bram Moolenaarc476e522013-06-23 13:46:40 +02001170 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001171 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001172 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001173 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001174 Py_TYPE_NAME(find_module_result));
1175 return NULL;
1176 }
1177 if (PyTuple_GET_SIZE(find_module_result) != 3)
1178 {
1179 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001180 N_("expected 3-tuple as imp.find_module() result, but got "
1181 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001182 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001183 return NULL;
1184 }
1185
1186 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1187 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1188 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1189 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001190 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001191 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001192 return NULL;
1193 }
1194
1195 return PyObject_CallFunction(py_load_module,
1196 "s#OOO", name, len, fd, pathname, description);
1197}
1198
1199 static PyObject *
1200find_module(char *fullname, char *tail, PyObject *new_path)
1201{
1202 PyObject *find_module_result;
1203 PyObject *module;
1204 char *dot;
1205
Bram Moolenaar41009372013-07-01 22:03:04 +02001206 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001207 {
1208 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001209 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001210 * first component
1211 */
1212 PyObject *newest_path;
1213 int partlen = (int) (dot - 1 - tail);
1214
1215 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1216 "s#O", tail, partlen, new_path)))
1217 return NULL;
1218
1219 if (!(module = call_load_module(
1220 fullname,
1221 ((int) (tail - fullname)) + partlen,
1222 find_module_result)))
1223 {
1224 Py_DECREF(find_module_result);
1225 return NULL;
1226 }
1227
1228 Py_DECREF(find_module_result);
1229
1230 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1231 {
1232 Py_DECREF(module);
1233 return NULL;
1234 }
1235
1236 Py_DECREF(module);
1237
1238 module = find_module(fullname, dot + 1, newest_path);
1239
1240 Py_DECREF(newest_path);
1241
1242 return module;
1243 }
1244 else
1245 {
1246 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1247 "sO", tail, new_path)))
1248 return NULL;
1249
1250 if (!(module = call_load_module(
1251 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001252 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001253 find_module_result)))
1254 {
1255 Py_DECREF(find_module_result);
1256 return NULL;
1257 }
1258
1259 Py_DECREF(find_module_result);
1260
1261 return module;
1262 }
1263}
1264
1265 static PyObject *
1266FinderFindModule(PyObject *self, PyObject *args)
1267{
1268 char *fullname;
1269 PyObject *module;
1270 PyObject *new_path;
1271 LoaderObject *loader;
1272
1273 if (!PyArg_ParseTuple(args, "s", &fullname))
1274 return NULL;
1275
1276 if (!(new_path = Vim_GetPaths(self)))
1277 return NULL;
1278
1279 module = find_module(fullname, fullname, new_path);
1280
1281 Py_DECREF(new_path);
1282
1283 if (!module)
1284 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001285 if (PyErr_Occurred())
1286 {
1287 if (PyErr_ExceptionMatches(PyExc_ImportError))
1288 PyErr_Clear();
1289 else
1290 return NULL;
1291 }
1292
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001293 Py_INCREF(Py_None);
1294 return Py_None;
1295 }
1296
1297 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1298 {
1299 Py_DECREF(module);
1300 return NULL;
1301 }
1302
1303 loader->module = module;
1304
1305 return (PyObject *) loader;
1306}
1307
1308 static PyObject *
1309VimPathHook(PyObject *self UNUSED, PyObject *args)
1310{
1311 char *path;
1312
1313 if (PyArg_ParseTuple(args, "s", &path)
1314 && STRCMP(path, vim_special_path) == 0)
1315 {
1316 Py_INCREF(vim_module);
1317 return vim_module;
1318 }
1319
1320 PyErr_Clear();
1321 PyErr_SetNone(PyExc_ImportError);
1322 return NULL;
1323}
1324
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001325/*
1326 * Vim module - Definitions
1327 */
1328
1329static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001330 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001331 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001332 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001333 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1334 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001335 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1336 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001337 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001338 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001339 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1340 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1341 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001342};
1343
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001344/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001345 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001346 */
1347
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001348static PyTypeObject IterType;
1349
1350typedef PyObject *(*nextfun)(void **);
1351typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001352typedef int (*traversefun)(void *, visitproc, void *);
1353typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001354
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001355/* Main purpose of this object is removing the need for do python
1356 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1357 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001358
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001359typedef struct
1360{
1361 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001362 void *cur;
1363 nextfun next;
1364 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001365 traversefun traverse;
1366 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001367} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001368
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001369 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001370IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1371 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001372{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001373 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001374
Bram Moolenaar774267b2013-05-21 20:51:59 +02001375 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001376 self->cur = start;
1377 self->next = next;
1378 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001379 self->traverse = traverse;
1380 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001381
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001382 return (PyObject *)(self);
1383}
1384
1385 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001386IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001387{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001388 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001389 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001390 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001391}
1392
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001393 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001394IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001395{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001396 if (self->traverse != NULL)
1397 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001398 else
1399 return 0;
1400}
1401
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001402/* Mac OSX defines clear() somewhere. */
1403#ifdef clear
1404# undef clear
1405#endif
1406
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001407 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001408IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001409{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001410 if (self->clear != NULL)
1411 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001412 else
1413 return 0;
1414}
1415
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001416 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001417IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001418{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001419 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001420}
1421
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001422 static PyObject *
1423IterIter(PyObject *self)
1424{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001425 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001426 return self;
1427}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001428
Bram Moolenaardb913952012-06-29 12:54:53 +02001429typedef struct pylinkedlist_S {
1430 struct pylinkedlist_S *pll_next;
1431 struct pylinkedlist_S *pll_prev;
1432 PyObject *pll_obj;
1433} pylinkedlist_T;
1434
1435static pylinkedlist_T *lastdict = NULL;
1436static pylinkedlist_T *lastlist = NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02001437static pylinkedlist_T *lastfunc = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02001438
1439 static void
1440pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1441{
1442 if (ref->pll_prev == NULL)
1443 {
1444 if (ref->pll_next == NULL)
1445 {
1446 *last = NULL;
1447 return;
1448 }
1449 }
1450 else
1451 ref->pll_prev->pll_next = ref->pll_next;
1452
1453 if (ref->pll_next == NULL)
1454 *last = ref->pll_prev;
1455 else
1456 ref->pll_next->pll_prev = ref->pll_prev;
1457}
1458
1459 static void
1460pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1461{
1462 if (*last == NULL)
1463 ref->pll_prev = NULL;
1464 else
1465 {
1466 (*last)->pll_next = ref;
1467 ref->pll_prev = *last;
1468 }
1469 ref->pll_next = NULL;
1470 ref->pll_obj = self;
1471 *last = ref;
1472}
1473
1474static PyTypeObject DictionaryType;
1475
1476typedef struct
1477{
1478 PyObject_HEAD
1479 dict_T *dict;
1480 pylinkedlist_T ref;
1481} DictionaryObject;
1482
Bram Moolenaara9922d62013-05-30 13:01:18 +02001483static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1484
1485#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1486
Bram Moolenaardb913952012-06-29 12:54:53 +02001487 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001488DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001489{
1490 DictionaryObject *self;
1491
Bram Moolenaara9922d62013-05-30 13:01:18 +02001492 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001493 if (self == NULL)
1494 return NULL;
1495 self->dict = dict;
1496 ++dict->dv_refcount;
1497
1498 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1499
1500 return (PyObject *)(self);
1501}
1502
Bram Moolenaara9922d62013-05-30 13:01:18 +02001503 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001504py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001505{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001506 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001507
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001508 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001509 {
1510 PyErr_NoMemory();
1511 return NULL;
1512 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001513 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001514
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001515 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001516}
1517
1518 static PyObject *
1519DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1520{
1521 DictionaryObject *self;
1522 dict_T *dict;
1523
1524 if (!(dict = py_dict_alloc()))
1525 return NULL;
1526
1527 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1528
1529 --dict->dv_refcount;
1530
1531 if (kwargs || PyTuple_Size(args))
1532 {
1533 PyObject *tmp;
1534 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1535 {
1536 Py_DECREF(self);
1537 return NULL;
1538 }
1539
1540 Py_DECREF(tmp);
1541 }
1542
1543 return (PyObject *)(self);
1544}
1545
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001546 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001547DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001548{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001549 pyll_remove(&self->ref, &lastdict);
1550 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001551
1552 DESTRUCTOR_FINISH(self);
1553}
1554
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001555static char *DictionaryAttrs[] = {
1556 "locked", "scope",
1557 NULL
1558};
1559
1560 static PyObject *
1561DictionaryDir(PyObject *self)
1562{
1563 return ObjectDir(self, DictionaryAttrs);
1564}
1565
Bram Moolenaardb913952012-06-29 12:54:53 +02001566 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001567DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001568{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001569 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001570 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001571 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001572 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001573 return -1;
1574 }
1575
1576 if (strcmp(name, "locked") == 0)
1577 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001578 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001579 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001580 PyErr_SET_STRING(PyExc_TypeError,
1581 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001582 return -1;
1583 }
1584 else
1585 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001586 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001587 if (istrue == -1)
1588 return -1;
1589 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001590 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001591 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001592 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001593 }
1594 return 0;
1595 }
1596 else
1597 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001598 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001599 return -1;
1600 }
1601}
1602
1603 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001604DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001605{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001606 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001607}
1608
Bram Moolenaara9922d62013-05-30 13:01:18 +02001609#define DICT_FLAG_HAS_DEFAULT 0x01
1610#define DICT_FLAG_POP 0x02
1611#define DICT_FLAG_NONE_DEFAULT 0x04
1612#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1613#define DICT_FLAG_RETURN_PAIR 0x10
1614
Bram Moolenaardb913952012-06-29 12:54:53 +02001615 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001616_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001617{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001618 PyObject *keyObject;
1619 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001620 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001621 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001622 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001623 dict_T *dict = self->dict;
1624 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001625 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001626
Bram Moolenaara9922d62013-05-30 13:01:18 +02001627 if (flags & DICT_FLAG_HAS_DEFAULT)
1628 {
1629 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1630 return NULL;
1631 }
1632 else
1633 keyObject = args;
1634
1635 if (flags & DICT_FLAG_RETURN_BOOL)
1636 defObject = Py_False;
1637
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001638 if (!(key = StringToChars(keyObject, &todecref)))
1639 return NULL;
1640
1641 if (*key == NUL)
1642 {
1643 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001644 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001645 return NULL;
1646 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001647
Bram Moolenaara9922d62013-05-30 13:01:18 +02001648 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001649
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001650 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001651
Bram Moolenaara9922d62013-05-30 13:01:18 +02001652 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001653 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001654 if (defObject)
1655 {
1656 Py_INCREF(defObject);
1657 return defObject;
1658 }
1659 else
1660 {
1661 PyErr_SetObject(PyExc_KeyError, keyObject);
1662 return NULL;
1663 }
1664 }
1665 else if (flags & DICT_FLAG_RETURN_BOOL)
1666 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001667 ret = Py_True;
1668 Py_INCREF(ret);
1669 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001670 }
1671
1672 di = dict_lookup(hi);
1673
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001674 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001675 return NULL;
1676
1677 if (flags & DICT_FLAG_POP)
1678 {
1679 if (dict->dv_lock)
1680 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001681 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001682 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001683 return NULL;
1684 }
1685
1686 hash_remove(&dict->dv_hashtab, hi);
1687 dictitem_free(di);
1688 }
1689
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001690 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001691}
1692
1693 static PyObject *
1694DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1695{
1696 return _DictionaryItem(self, keyObject, 0);
1697}
1698
1699 static int
1700DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1701{
1702 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001703 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001704
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001705 if (rObj == NULL)
1706 return -1;
1707
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001708 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001709
Bram Moolenaardee2e312013-06-23 16:35:47 +02001710 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001711
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001712 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001713}
1714
1715typedef struct
1716{
1717 hashitem_T *ht_array;
1718 long_u ht_used;
1719 hashtab_T *ht;
1720 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001721 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001722} dictiterinfo_T;
1723
1724 static PyObject *
1725DictionaryIterNext(dictiterinfo_T **dii)
1726{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001727 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001728
1729 if (!(*dii)->todo)
1730 return NULL;
1731
1732 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1733 (*dii)->ht->ht_used != (*dii)->ht_used)
1734 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001735 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001736 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001737 return NULL;
1738 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001739
Bram Moolenaara9922d62013-05-30 13:01:18 +02001740 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1741 ++((*dii)->hi);
1742
1743 --((*dii)->todo);
1744
Bram Moolenaar41009372013-07-01 22:03:04 +02001745 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001746 return NULL;
1747
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001748 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001749}
1750
1751 static PyObject *
1752DictionaryIter(DictionaryObject *self)
1753{
1754 dictiterinfo_T *dii;
1755 hashtab_T *ht;
1756
1757 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1758 {
1759 PyErr_NoMemory();
1760 return NULL;
1761 }
1762
1763 ht = &self->dict->dv_hashtab;
1764 dii->ht_array = ht->ht_array;
1765 dii->ht_used = ht->ht_used;
1766 dii->ht = ht;
1767 dii->hi = dii->ht_array;
1768 dii->todo = dii->ht_used;
1769
1770 return IterNew(dii,
1771 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1772 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001773}
1774
1775 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001776DictionaryAssItem(
1777 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001778{
1779 char_u *key;
1780 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001781 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001782 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001783 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001784
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001785 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001786 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001787 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001788 return -1;
1789 }
1790
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001791 if (!(key = StringToChars(keyObject, &todecref)))
1792 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001793
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001794 if (*key == NUL)
1795 {
1796 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001797 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001798 return -1;
1799 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001800
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001801 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001802
1803 if (valObject == NULL)
1804 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001805 hashitem_T *hi;
1806
Bram Moolenaardb913952012-06-29 12:54:53 +02001807 if (di == NULL)
1808 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001809 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001810 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001811 return -1;
1812 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001813 hi = hash_find(&dict->dv_hashtab, di->di_key);
1814 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001815 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001816 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001817 return 0;
1818 }
1819
1820 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001821 {
1822 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001823 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001824 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001825
1826 if (di == NULL)
1827 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001828 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001829 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001830 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001831 PyErr_NoMemory();
1832 return -1;
1833 }
1834 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001835 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001836
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001837 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001838 {
1839 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001840 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001841 RAISE_KEY_ADD_FAIL(key);
1842 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001843 return -1;
1844 }
1845 }
1846 else
1847 clear_tv(&di->di_tv);
1848
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001849 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001850
1851 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001852 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001853 return 0;
1854}
1855
Bram Moolenaara9922d62013-05-30 13:01:18 +02001856typedef PyObject *(*hi_to_py)(hashitem_T *);
1857
Bram Moolenaardb913952012-06-29 12:54:53 +02001858 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001859DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001860{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001861 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001862 long_u todo = dict->dv_hashtab.ht_used;
1863 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001864 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001865 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001866 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001867
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001868 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001869 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1870 {
1871 if (!HASHITEM_EMPTY(hi))
1872 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001873 if (!(newObj = hiconvert(hi)))
1874 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001875 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001876 return NULL;
1877 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001878 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001879 --todo;
1880 ++i;
1881 }
1882 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001883 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001884}
1885
Bram Moolenaara9922d62013-05-30 13:01:18 +02001886 static PyObject *
1887dict_key(hashitem_T *hi)
1888{
1889 return PyBytes_FromString((char *)(hi->hi_key));
1890}
1891
1892 static PyObject *
1893DictionaryListKeys(DictionaryObject *self)
1894{
1895 return DictionaryListObjects(self, dict_key);
1896}
1897
1898 static PyObject *
1899dict_val(hashitem_T *hi)
1900{
1901 dictitem_T *di;
1902
1903 di = dict_lookup(hi);
1904 return ConvertToPyObject(&di->di_tv);
1905}
1906
1907 static PyObject *
1908DictionaryListValues(DictionaryObject *self)
1909{
1910 return DictionaryListObjects(self, dict_val);
1911}
1912
1913 static PyObject *
1914dict_item(hashitem_T *hi)
1915{
1916 PyObject *keyObject;
1917 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001918 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001919
1920 if (!(keyObject = dict_key(hi)))
1921 return NULL;
1922
1923 if (!(valObject = dict_val(hi)))
1924 {
1925 Py_DECREF(keyObject);
1926 return NULL;
1927 }
1928
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001929 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001930
1931 Py_DECREF(keyObject);
1932 Py_DECREF(valObject);
1933
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001934 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001935}
1936
1937 static PyObject *
1938DictionaryListItems(DictionaryObject *self)
1939{
1940 return DictionaryListObjects(self, dict_item);
1941}
1942
1943 static PyObject *
1944DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1945{
1946 dict_T *dict = self->dict;
1947
1948 if (dict->dv_lock)
1949 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001950 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001951 return NULL;
1952 }
1953
1954 if (kwargs)
1955 {
1956 typval_T tv;
1957
1958 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1959 return NULL;
1960
1961 VimTryStart();
1962 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1963 clear_tv(&tv);
1964 if (VimTryEnd())
1965 return NULL;
1966 }
1967 else
1968 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001969 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001970
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001971 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001972 return NULL;
1973
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001974 if (obj == NULL)
1975 {
1976 Py_INCREF(Py_None);
1977 return Py_None;
1978 }
1979
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001980 if (PyObject_HasAttrString(obj, "keys"))
1981 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001982 else
1983 {
1984 PyObject *iterator;
1985 PyObject *item;
1986
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001987 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001988 return NULL;
1989
1990 while ((item = PyIter_Next(iterator)))
1991 {
1992 PyObject *fast;
1993 PyObject *keyObject;
1994 PyObject *valObject;
1995 PyObject *todecref;
1996 char_u *key;
1997 dictitem_T *di;
1998
1999 if (!(fast = PySequence_Fast(item, "")))
2000 {
2001 Py_DECREF(iterator);
2002 Py_DECREF(item);
2003 return NULL;
2004 }
2005
2006 Py_DECREF(item);
2007
2008 if (PySequence_Fast_GET_SIZE(fast) != 2)
2009 {
2010 Py_DECREF(iterator);
2011 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002012 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002013 N_("expected sequence element of size 2, "
2014 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002015 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002016 return NULL;
2017 }
2018
2019 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2020
2021 if (!(key = StringToChars(keyObject, &todecref)))
2022 {
2023 Py_DECREF(iterator);
2024 Py_DECREF(fast);
2025 return NULL;
2026 }
2027
2028 di = dictitem_alloc(key);
2029
2030 Py_XDECREF(todecref);
2031
2032 if (di == NULL)
2033 {
2034 Py_DECREF(fast);
2035 Py_DECREF(iterator);
2036 PyErr_NoMemory();
2037 return NULL;
2038 }
2039 di->di_tv.v_lock = 0;
2040 di->di_tv.v_type = VAR_UNKNOWN;
2041
2042 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2043
2044 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2045 {
2046 Py_DECREF(iterator);
2047 Py_DECREF(fast);
2048 dictitem_free(di);
2049 return NULL;
2050 }
2051
2052 Py_DECREF(fast);
2053
2054 if (dict_add(dict, di) == FAIL)
2055 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002056 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002057 Py_DECREF(iterator);
2058 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002059 return NULL;
2060 }
2061 }
2062
2063 Py_DECREF(iterator);
2064
2065 /* Iterator may have finished due to an exception */
2066 if (PyErr_Occurred())
2067 return NULL;
2068 }
2069 }
2070 Py_INCREF(Py_None);
2071 return Py_None;
2072}
2073
2074 static PyObject *
2075DictionaryGet(DictionaryObject *self, PyObject *args)
2076{
2077 return _DictionaryItem(self, args,
2078 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2079}
2080
2081 static PyObject *
2082DictionaryPop(DictionaryObject *self, PyObject *args)
2083{
2084 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2085}
2086
2087 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002088DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002089{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002090 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002091 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002092 PyObject *valObject;
2093 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002094
Bram Moolenaarde71b562013-06-02 17:41:54 +02002095 if (self->dict->dv_hashtab.ht_used == 0)
2096 {
2097 PyErr_SetNone(PyExc_KeyError);
2098 return NULL;
2099 }
2100
2101 hi = self->dict->dv_hashtab.ht_array;
2102 while (HASHITEM_EMPTY(hi))
2103 ++hi;
2104
2105 di = dict_lookup(hi);
2106
2107 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002108 return NULL;
2109
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002110 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002111 {
2112 Py_DECREF(valObject);
2113 return NULL;
2114 }
2115
2116 hash_remove(&self->dict->dv_hashtab, hi);
2117 dictitem_free(di);
2118
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002119 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002120}
2121
2122 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002123DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002124{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002125 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2126}
2127
2128static PySequenceMethods DictionaryAsSeq = {
2129 0, /* sq_length */
2130 0, /* sq_concat */
2131 0, /* sq_repeat */
2132 0, /* sq_item */
2133 0, /* sq_slice */
2134 0, /* sq_ass_item */
2135 0, /* sq_ass_slice */
2136 (objobjproc) DictionaryContains, /* sq_contains */
2137 0, /* sq_inplace_concat */
2138 0, /* sq_inplace_repeat */
2139};
2140
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002141static PyMappingMethods DictionaryAsMapping = {
2142 (lenfunc) DictionaryLength,
2143 (binaryfunc) DictionaryItem,
2144 (objobjargproc) DictionaryAssItem,
2145};
2146
Bram Moolenaardb913952012-06-29 12:54:53 +02002147static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002148 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002149 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2150 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2151 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2152 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2153 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002154 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002155 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002156 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2157 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002158};
2159
2160static PyTypeObject ListType;
2161
2162typedef struct
2163{
2164 PyObject_HEAD
2165 list_T *list;
2166 pylinkedlist_T ref;
2167} ListObject;
2168
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002169#define NEW_LIST(list) ListNew(&ListType, list)
2170
Bram Moolenaardb913952012-06-29 12:54:53 +02002171 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002172ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002173{
2174 ListObject *self;
2175
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002176 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002177 if (self == NULL)
2178 return NULL;
2179 self->list = list;
2180 ++list->lv_refcount;
2181
2182 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2183
2184 return (PyObject *)(self);
2185}
2186
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002187 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002188py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002189{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002190 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002191
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002192 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002193 {
2194 PyErr_NoMemory();
2195 return NULL;
2196 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002197 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002198
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002199 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002200}
2201
2202 static int
2203list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2204{
2205 PyObject *iterator;
2206 PyObject *item;
2207 listitem_T *li;
2208
2209 if (!(iterator = PyObject_GetIter(obj)))
2210 return -1;
2211
2212 while ((item = PyIter_Next(iterator)))
2213 {
2214 if (!(li = listitem_alloc()))
2215 {
2216 PyErr_NoMemory();
2217 Py_DECREF(item);
2218 Py_DECREF(iterator);
2219 return -1;
2220 }
2221 li->li_tv.v_lock = 0;
2222 li->li_tv.v_type = VAR_UNKNOWN;
2223
2224 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2225 {
2226 Py_DECREF(item);
2227 Py_DECREF(iterator);
2228 listitem_free(li);
2229 return -1;
2230 }
2231
2232 Py_DECREF(item);
2233
2234 list_append(l, li);
2235 }
2236
2237 Py_DECREF(iterator);
2238
2239 /* Iterator may have finished due to an exception */
2240 if (PyErr_Occurred())
2241 return -1;
2242
2243 return 0;
2244}
2245
2246 static PyObject *
2247ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2248{
2249 list_T *list;
2250 PyObject *obj = NULL;
2251
2252 if (kwargs)
2253 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002254 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002255 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002256 return NULL;
2257 }
2258
2259 if (!PyArg_ParseTuple(args, "|O", &obj))
2260 return NULL;
2261
2262 if (!(list = py_list_alloc()))
2263 return NULL;
2264
2265 if (obj)
2266 {
2267 PyObject *lookup_dict;
2268
2269 if (!(lookup_dict = PyDict_New()))
2270 {
2271 list_unref(list);
2272 return NULL;
2273 }
2274
2275 if (list_py_concat(list, obj, lookup_dict) == -1)
2276 {
2277 Py_DECREF(lookup_dict);
2278 list_unref(list);
2279 return NULL;
2280 }
2281
2282 Py_DECREF(lookup_dict);
2283 }
2284
2285 return ListNew(subtype, list);
2286}
2287
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002288 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002289ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002290{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002291 pyll_remove(&self->ref, &lastlist);
2292 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002293
2294 DESTRUCTOR_FINISH(self);
2295}
2296
Bram Moolenaardb913952012-06-29 12:54:53 +02002297 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002298ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002299{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002300 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002301}
2302
2303 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002304ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002305{
2306 listitem_T *li;
2307
Bram Moolenaard6e39182013-05-21 18:30:34 +02002308 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002309 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002310 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002311 return NULL;
2312 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002313 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002314 if (li == NULL)
2315 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002316 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002317 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002318 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002319 return NULL;
2320 }
2321 return ConvertToPyObject(&li->li_tv);
2322}
2323
Bram Moolenaardb913952012-06-29 12:54:53 +02002324 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002325ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2326 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002327{
2328 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002329 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002330
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002331 if (step == 0)
2332 {
2333 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2334 return NULL;
2335 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002336
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002337 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002338 if (list == NULL)
2339 return NULL;
2340
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002341 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002342 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002343 PyObject *item;
2344
2345 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002346 if (item == NULL)
2347 {
2348 Py_DECREF(list);
2349 return NULL;
2350 }
2351
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002352 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002353 }
2354
2355 return list;
2356}
2357
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002358 static PyObject *
2359ListItem(ListObject *self, PyObject* idx)
2360{
2361#if PY_MAJOR_VERSION < 3
2362 if (PyInt_Check(idx))
2363 {
2364 long _idx = PyInt_AsLong(idx);
2365 return ListIndex(self, _idx);
2366 }
2367 else
2368#endif
2369 if (PyLong_Check(idx))
2370 {
2371 long _idx = PyLong_AsLong(idx);
2372 return ListIndex(self, _idx);
2373 }
2374 else if (PySlice_Check(idx))
2375 {
2376 Py_ssize_t start, stop, step, slicelen;
2377
Bram Moolenaar922a4662014-03-30 16:11:43 +02002378 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002379 &start, &stop, &step, &slicelen) < 0)
2380 return NULL;
2381 return ListSlice(self, start, step, slicelen);
2382 }
2383 else
2384 {
2385 RAISE_INVALID_INDEX_TYPE(idx);
2386 return NULL;
2387 }
2388}
2389
2390 static void
2391list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2392 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2393{
2394 while (numreplaced--)
2395 {
2396 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2397 listitem_remove(l, lis[slicelen + numreplaced]);
2398 }
2399 while (numadded--)
2400 {
2401 listitem_T *next;
2402
2403 next = lastaddedli->li_prev;
2404 listitem_remove(l, lastaddedli);
2405 lastaddedli = next;
2406 }
2407}
2408
2409 static int
2410ListAssSlice(ListObject *self, Py_ssize_t first,
2411 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2412{
2413 PyObject *iterator;
2414 PyObject *item;
2415 listitem_T *li;
2416 listitem_T *lastaddedli = NULL;
2417 listitem_T *next;
2418 typval_T v;
2419 list_T *l = self->list;
2420 PyInt i;
2421 PyInt j;
2422 PyInt numreplaced = 0;
2423 PyInt numadded = 0;
2424 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002425 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002426
2427 size = ListLength(self);
2428
2429 if (l->lv_lock)
2430 {
2431 RAISE_LOCKED_LIST;
2432 return -1;
2433 }
2434
2435 if (step == 0)
2436 {
2437 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2438 return -1;
2439 }
2440
2441 if (step != 1 && slicelen == 0)
2442 {
2443 /* Nothing to do. Only error out if obj has some items. */
2444 int ret = 0;
2445
2446 if (obj == NULL)
2447 return 0;
2448
2449 if (!(iterator = PyObject_GetIter(obj)))
2450 return -1;
2451
2452 if ((item = PyIter_Next(iterator)))
2453 {
2454 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002455 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002456 "to extended slice"), 0);
2457 Py_DECREF(item);
2458 ret = -1;
2459 }
2460 Py_DECREF(iterator);
2461 return ret;
2462 }
2463
2464 if (obj != NULL)
2465 /* XXX May allocate zero bytes. */
2466 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2467 {
2468 PyErr_NoMemory();
2469 return -1;
2470 }
2471
2472 if (first == size)
2473 li = NULL;
2474 else
2475 {
2476 li = list_find(l, (long) first);
2477 if (li == NULL)
2478 {
2479 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2480 (int)first);
2481 if (obj != NULL)
2482 PyMem_Free(lis);
2483 return -1;
2484 }
2485 i = slicelen;
2486 while (i-- && li != NULL)
2487 {
2488 j = step;
2489 next = li;
2490 if (step > 0)
2491 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2492 else
2493 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2494
2495 if (obj == NULL)
2496 listitem_remove(l, li);
2497 else
2498 lis[slicelen - i - 1] = li;
2499
2500 li = next;
2501 }
2502 if (li == NULL && i != -1)
2503 {
2504 PyErr_SET_VIM(N_("internal error: not enough list items"));
2505 if (obj != NULL)
2506 PyMem_Free(lis);
2507 return -1;
2508 }
2509 }
2510
2511 if (obj == NULL)
2512 return 0;
2513
2514 if (!(iterator = PyObject_GetIter(obj)))
2515 {
2516 PyMem_Free(lis);
2517 return -1;
2518 }
2519
2520 i = 0;
2521 while ((item = PyIter_Next(iterator)))
2522 {
2523 if (ConvertFromPyObject(item, &v) == -1)
2524 {
2525 Py_DECREF(iterator);
2526 Py_DECREF(item);
2527 PyMem_Free(lis);
2528 return -1;
2529 }
2530 Py_DECREF(item);
2531 if (list_insert_tv(l, &v, numreplaced < slicelen
2532 ? lis[numreplaced]
2533 : li) == FAIL)
2534 {
2535 clear_tv(&v);
2536 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2537 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2538 PyMem_Free(lis);
2539 return -1;
2540 }
2541 if (numreplaced < slicelen)
2542 {
2543 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002544 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002545 numreplaced++;
2546 }
2547 else
2548 {
2549 if (li)
2550 lastaddedli = li->li_prev;
2551 else
2552 lastaddedli = l->lv_last;
2553 numadded++;
2554 }
2555 clear_tv(&v);
2556 if (step != 1 && i >= slicelen)
2557 {
2558 Py_DECREF(iterator);
2559 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002560 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002561 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002562 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2563 PyMem_Free(lis);
2564 return -1;
2565 }
2566 ++i;
2567 }
2568 Py_DECREF(iterator);
2569
2570 if (step != 1 && i != slicelen)
2571 {
2572 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002573 N_("attempt to assign sequence of size %d to extended slice "
2574 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002575 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2576 PyMem_Free(lis);
2577 return -1;
2578 }
2579
2580 if (PyErr_Occurred())
2581 {
2582 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2583 PyMem_Free(lis);
2584 return -1;
2585 }
2586
2587 for (i = 0; i < numreplaced; i++)
2588 listitem_free(lis[i]);
2589 if (step == 1)
2590 for (i = numreplaced; i < slicelen; i++)
2591 listitem_remove(l, lis[i]);
2592
2593 PyMem_Free(lis);
2594
2595 return 0;
2596}
2597
2598 static int
2599ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2600{
2601 typval_T tv;
2602 list_T *l = self->list;
2603 listitem_T *li;
2604 Py_ssize_t length = ListLength(self);
2605
2606 if (l->lv_lock)
2607 {
2608 RAISE_LOCKED_LIST;
2609 return -1;
2610 }
2611 if (index > length || (index == length && obj == NULL))
2612 {
2613 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2614 return -1;
2615 }
2616
2617 if (obj == NULL)
2618 {
2619 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002620 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002621 clear_tv(&li->li_tv);
2622 vim_free(li);
2623 return 0;
2624 }
2625
2626 if (ConvertFromPyObject(obj, &tv) == -1)
2627 return -1;
2628
2629 if (index == length)
2630 {
2631 if (list_append_tv(l, &tv) == FAIL)
2632 {
2633 clear_tv(&tv);
2634 PyErr_SET_VIM(N_("failed to add item to list"));
2635 return -1;
2636 }
2637 }
2638 else
2639 {
2640 li = list_find(l, (long) index);
2641 clear_tv(&li->li_tv);
2642 copy_tv(&tv, &li->li_tv);
2643 clear_tv(&tv);
2644 }
2645 return 0;
2646}
2647
2648 static Py_ssize_t
2649ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2650{
2651#if PY_MAJOR_VERSION < 3
2652 if (PyInt_Check(idx))
2653 {
2654 long _idx = PyInt_AsLong(idx);
2655 return ListAssIndex(self, _idx, obj);
2656 }
2657 else
2658#endif
2659 if (PyLong_Check(idx))
2660 {
2661 long _idx = PyLong_AsLong(idx);
2662 return ListAssIndex(self, _idx, obj);
2663 }
2664 else if (PySlice_Check(idx))
2665 {
2666 Py_ssize_t start, stop, step, slicelen;
2667
Bram Moolenaar922a4662014-03-30 16:11:43 +02002668 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002669 &start, &stop, &step, &slicelen) < 0)
2670 return -1;
2671 return ListAssSlice(self, start, step, slicelen,
2672 obj);
2673 }
2674 else
2675 {
2676 RAISE_INVALID_INDEX_TYPE(idx);
2677 return -1;
2678 }
2679}
2680
2681 static PyObject *
2682ListConcatInPlace(ListObject *self, PyObject *obj)
2683{
2684 list_T *l = self->list;
2685 PyObject *lookup_dict;
2686
2687 if (l->lv_lock)
2688 {
2689 RAISE_LOCKED_LIST;
2690 return NULL;
2691 }
2692
2693 if (!(lookup_dict = PyDict_New()))
2694 return NULL;
2695
2696 if (list_py_concat(l, obj, lookup_dict) == -1)
2697 {
2698 Py_DECREF(lookup_dict);
2699 return NULL;
2700 }
2701 Py_DECREF(lookup_dict);
2702
2703 Py_INCREF(self);
2704 return (PyObject *)(self);
2705}
2706
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002707typedef struct
2708{
2709 listwatch_T lw;
2710 list_T *list;
2711} listiterinfo_T;
2712
2713 static void
2714ListIterDestruct(listiterinfo_T *lii)
2715{
2716 list_rem_watch(lii->list, &lii->lw);
2717 PyMem_Free(lii);
2718}
2719
2720 static PyObject *
2721ListIterNext(listiterinfo_T **lii)
2722{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002723 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002724
2725 if (!((*lii)->lw.lw_item))
2726 return NULL;
2727
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002728 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002729 return NULL;
2730
2731 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2732
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002733 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002734}
2735
2736 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002737ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002738{
2739 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002740 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002741
2742 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2743 {
2744 PyErr_NoMemory();
2745 return NULL;
2746 }
2747
2748 list_add_watch(l, &lii->lw);
2749 lii->lw.lw_item = l->lv_first;
2750 lii->list = l;
2751
2752 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002753 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2754 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002755}
2756
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002757static char *ListAttrs[] = {
2758 "locked",
2759 NULL
2760};
2761
2762 static PyObject *
2763ListDir(PyObject *self)
2764{
2765 return ObjectDir(self, ListAttrs);
2766}
2767
Bram Moolenaar66b79852012-09-21 14:00:35 +02002768 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002769ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002770{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002771 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002772 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002773 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002774 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002775 return -1;
2776 }
2777
2778 if (strcmp(name, "locked") == 0)
2779 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002780 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002781 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002782 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002783 return -1;
2784 }
2785 else
2786 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002787 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002788 if (istrue == -1)
2789 return -1;
2790 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002791 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002792 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002793 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002794 }
2795 return 0;
2796 }
2797 else
2798 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002799 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002800 return -1;
2801 }
2802}
2803
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002804static PySequenceMethods ListAsSeq = {
2805 (lenfunc) ListLength, /* sq_length, len(x) */
2806 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2807 0, /* RangeRepeat, sq_repeat, x*n */
2808 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2809 0, /* was_sq_slice, x[i:j] */
2810 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2811 0, /* was_sq_ass_slice, x[i:j]=v */
2812 0, /* sq_contains */
2813 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2814 0, /* sq_inplace_repeat */
2815};
2816
2817static PyMappingMethods ListAsMapping = {
2818 /* mp_length */ (lenfunc) ListLength,
2819 /* mp_subscript */ (binaryfunc) ListItem,
2820 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2821};
2822
Bram Moolenaardb913952012-06-29 12:54:53 +02002823static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002824 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2825 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2826 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002827};
2828
2829typedef struct
2830{
2831 PyObject_HEAD
2832 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002833 int argc;
2834 typval_T *argv;
2835 dict_T *self;
2836 pylinkedlist_T ref;
Bram Moolenaardb913952012-06-29 12:54:53 +02002837} FunctionObject;
2838
2839static PyTypeObject FunctionType;
2840
Bram Moolenaar8110a092016-04-14 15:56:09 +02002841#define NEW_FUNCTION(name, argc, argv, self) \
2842 FunctionNew(&FunctionType, name, argc, argv, self)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002843
Bram Moolenaardb913952012-06-29 12:54:53 +02002844 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002845FunctionNew(PyTypeObject *subtype, char_u *name, int argc, typval_T *argv,
2846 dict_T *selfdict)
Bram Moolenaardb913952012-06-29 12:54:53 +02002847{
2848 FunctionObject *self;
2849
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002850 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2851
Bram Moolenaardb913952012-06-29 12:54:53 +02002852 if (self == NULL)
2853 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002854
2855 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002856 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002857 if (!translated_function_exists(name))
2858 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002859 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002860 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002861 return NULL;
2862 }
2863 self->name = vim_strsave(name);
2864 func_ref(self->name);
2865 }
2866 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002867 if ((self->name = get_expanded_name(name,
2868 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2869 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002870 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002871 PyErr_FORMAT(PyExc_ValueError,
2872 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002873 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002874 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002875
Bram Moolenaar8110a092016-04-14 15:56:09 +02002876 self->argc = argc;
2877 self->argv = argv;
2878 self->self = selfdict;
2879
2880 if (self->argv || self->self)
2881 pyll_add((PyObject *)(self), &self->ref, &lastfunc);
2882
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002883 return (PyObject *)(self);
2884}
2885
2886 static PyObject *
2887FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2888{
2889 PyObject *self;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002890 PyObject *selfdictObject;
2891 PyObject *argsObject = NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002892 char_u *name;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002893 typval_T selfdicttv;
2894 typval_T argstv;
2895 list_T *argslist = NULL;
2896 dict_T *selfdict = NULL;
2897 int argc = 0;
2898 typval_T *argv = NULL;
2899 typval_T *curtv;
2900 listitem_T *li;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002901
Bram Moolenaar8110a092016-04-14 15:56:09 +02002902 if (kwargs != NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002903 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02002904 selfdictObject = PyDict_GetItemString(kwargs, "self");
2905 if (selfdictObject != NULL)
2906 {
2907 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
2908 return NULL;
2909 selfdict = selfdicttv.vval.v_dict;
2910 }
2911 argsObject = PyDict_GetItemString(kwargs, "args");
2912 if (argsObject != NULL)
2913 {
2914 if (ConvertFromPySequence(argsObject, &argstv) == -1)
2915 {
2916 dict_unref(selfdict);
2917 return NULL;
2918 }
2919 argslist = argstv.vval.v_list;
2920
2921 argc = argslist->lv_len;
2922 if (argc != 0)
2923 {
2924 argv = PyMem_New(typval_T, (size_t) argc);
2925 curtv = argv;
2926 for (li = argslist->lv_first; li != NULL; li = li->li_next)
2927 copy_tv(&li->li_tv, curtv++);
2928 }
2929 list_unref(argslist);
2930 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002931 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002932
Bram Moolenaar389a1792013-06-23 13:00:44 +02002933 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar8110a092016-04-14 15:56:09 +02002934 {
2935 dict_unref(selfdict);
2936 while (argc--)
2937 clear_tv(&argv[argc]);
2938 PyMem_Free(argv);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002939 return NULL;
Bram Moolenaar8110a092016-04-14 15:56:09 +02002940 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002941
Bram Moolenaar8110a092016-04-14 15:56:09 +02002942 self = FunctionNew(subtype, name, argc, argv, selfdict);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002943
Bram Moolenaar389a1792013-06-23 13:00:44 +02002944 PyMem_Free(name);
2945
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002946 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002947}
2948
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002949 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002950FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002951{
Bram Moolenaar8110a092016-04-14 15:56:09 +02002952 int i;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002953 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002954 vim_free(self->name);
Bram Moolenaar8110a092016-04-14 15:56:09 +02002955 for (i = 0; i < self->argc; ++i)
2956 clear_tv(&self->argv[i]);
2957 PyMem_Free(self->argv);
2958 dict_unref(self->self);
2959 if (self->argv || self->self)
2960 pyll_remove(&self->ref, &lastfunc);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002961
2962 DESTRUCTOR_FINISH(self);
2963}
2964
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002965static char *FunctionAttrs[] = {
Bram Moolenaar8110a092016-04-14 15:56:09 +02002966 "softspace", "args", "self",
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002967 NULL
2968};
2969
2970 static PyObject *
2971FunctionDir(PyObject *self)
2972{
2973 return ObjectDir(self, FunctionAttrs);
2974}
2975
Bram Moolenaardb913952012-06-29 12:54:53 +02002976 static PyObject *
Bram Moolenaar8110a092016-04-14 15:56:09 +02002977FunctionAttr(FunctionObject *self, char *name)
2978{
2979 list_T *list;
2980 int i;
2981 if (strcmp(name, "name") == 0)
2982 return PyString_FromString((char *)(self->name));
2983 else if (strcmp(name, "args") == 0)
2984 {
2985 if (self->argv == NULL)
2986 return AlwaysNone(NULL);
2987 list = list_alloc();
2988 for (i = 0; i < self->argc; ++i)
2989 list_append_tv(list, &self->argv[i]);
2990 return NEW_LIST(list);
2991 }
2992 else if (strcmp(name, "self") == 0)
2993 return self->self == NULL
2994 ? AlwaysNone(NULL)
2995 : NEW_DICTIONARY(self->self);
2996 else if (strcmp(name, "__members__") == 0)
2997 return ObjectDir(NULL, FunctionAttrs);
2998 return NULL;
2999}
3000
3001/* Populate partial_T given function object.
3002 *
3003 * "exported" should be set to true when it is needed to construct a partial
3004 * that may be stored in a variable (i.e. may be freed by Vim).
3005 */
3006 static void
3007set_partial(FunctionObject *self, partial_T *pt, int exported)
3008{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003009 int i;
3010
3011 pt->pt_name = self->name;
3012 if (self->argv)
3013 {
3014 pt->pt_argc = self->argc;
3015 if (exported)
3016 {
3017 pt->pt_argv = (typval_T *)alloc_clear(
3018 sizeof(typval_T) * self->argc);
3019 for (i = 0; i < pt->pt_argc; ++i)
3020 copy_tv(&self->argv[i], &pt->pt_argv[i]);
3021 }
3022 else
3023 pt->pt_argv = self->argv;
3024 }
3025 else
3026 {
3027 pt->pt_argc = 0;
3028 pt->pt_argv = NULL;
3029 }
3030 pt->pt_dict = self->self;
3031 if (exported && self->self)
3032 ++pt->pt_dict->dv_refcount;
3033 if (exported)
3034 pt->pt_name = vim_strsave(pt->pt_name);
3035 pt->pt_refcount = 1;
3036}
3037
3038 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003039FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02003040{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003041 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02003042 typval_T args;
3043 typval_T selfdicttv;
3044 typval_T rettv;
3045 dict_T *selfdict = NULL;
3046 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003047 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003048 int error;
Bram Moolenaar8110a092016-04-14 15:56:09 +02003049 partial_T pt;
3050 partial_T *pt_ptr = NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02003051
Bram Moolenaar8110a092016-04-14 15:56:09 +02003052 if (ConvertFromPySequence(argsObject, &args) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02003053 return NULL;
3054
3055 if (kwargs != NULL)
3056 {
3057 selfdictObject = PyDict_GetItemString(kwargs, "self");
3058 if (selfdictObject != NULL)
3059 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02003060 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003061 {
3062 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02003063 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003064 }
Bram Moolenaardb913952012-06-29 12:54:53 +02003065 selfdict = selfdicttv.vval.v_dict;
3066 }
3067 }
3068
Bram Moolenaar8110a092016-04-14 15:56:09 +02003069 if (self->argv || self->self)
3070 {
3071 set_partial(self, &pt, FALSE);
3072 pt_ptr = &pt;
3073 }
3074
Bram Moolenaar71700b82013-05-15 17:49:05 +02003075 Py_BEGIN_ALLOW_THREADS
3076 Python_Lock_Vim();
3077
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003078 VimTryStart();
Bram Moolenaar8110a092016-04-14 15:56:09 +02003079 error = func_call(name, &args, pt_ptr, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02003080
3081 Python_Release_Vim();
3082 Py_END_ALLOW_THREADS
3083
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003084 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003085 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003086 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02003087 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003088 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003089 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02003090 }
3091 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003092 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003093
Bram Moolenaardb913952012-06-29 12:54:53 +02003094 clear_tv(&args);
3095 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003096 if (selfdict != NULL)
3097 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02003098
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003099 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02003100}
3101
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003102 static PyObject *
3103FunctionRepr(FunctionObject *self)
3104{
Bram Moolenaar8110a092016-04-14 15:56:09 +02003105 PyObject *ret;
3106 garray_T repr_ga;
3107 int i;
3108 char_u *tofree = NULL;
3109 typval_T tv;
3110 char_u numbuf[NUMBUFLEN];
3111
3112 ga_init2(&repr_ga, (int)sizeof(char), 70);
3113 ga_concat(&repr_ga, (char_u *)"<vim.Function '");
3114 if (self->name)
3115 ga_concat(&repr_ga, self->name);
3116 else
3117 ga_concat(&repr_ga, (char_u *)"<NULL>");
3118 ga_append(&repr_ga, '\'');
3119 if (self->argv)
3120 {
3121 ga_concat(&repr_ga, (char_u *)", args=[");
3122 ++emsg_silent;
3123 for (i = 0; i < self->argc; i++)
3124 {
3125 if (i != 0)
3126 ga_concat(&repr_ga, (char_u *)", ");
3127 ga_concat(&repr_ga, tv2string(&self->argv[i], &tofree, numbuf,
3128 get_copyID()));
3129 vim_free(tofree);
3130 }
3131 --emsg_silent;
3132 ga_append(&repr_ga, ']');
3133 }
3134 if (self->self)
3135 {
3136 ga_concat(&repr_ga, (char_u *)", self=");
3137 tv.v_type = VAR_DICT;
3138 tv.vval.v_dict = self->self;
3139 ++emsg_silent;
3140 ga_concat(&repr_ga, tv2string(&tv, &tofree, numbuf, get_copyID()));
3141 --emsg_silent;
3142 vim_free(tofree);
3143 }
3144 ga_append(&repr_ga, '>');
3145 ret = PyString_FromString((char *)repr_ga.ga_data);
3146 ga_clear(&repr_ga);
3147 return ret;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02003148}
3149
Bram Moolenaardb913952012-06-29 12:54:53 +02003150static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003151 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
3152 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02003153};
3154
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003155/*
3156 * Options object
3157 */
3158
3159static PyTypeObject OptionsType;
3160
3161typedef int (*checkfun)(void *);
3162
3163typedef struct
3164{
3165 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003166 int opt_type;
3167 void *from;
3168 checkfun Check;
3169 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003170} OptionsObject;
3171
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003172 static int
3173dummy_check(void *arg UNUSED)
3174{
3175 return 0;
3176}
3177
3178 static PyObject *
3179OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3180{
3181 OptionsObject *self;
3182
Bram Moolenaar774267b2013-05-21 20:51:59 +02003183 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003184 if (self == NULL)
3185 return NULL;
3186
3187 self->opt_type = opt_type;
3188 self->from = from;
3189 self->Check = Check;
3190 self->fromObj = fromObj;
3191 if (fromObj)
3192 Py_INCREF(fromObj);
3193
3194 return (PyObject *)(self);
3195}
3196
3197 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003198OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003199{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003200 PyObject_GC_UnTrack((void *)(self));
3201 Py_XDECREF(self->fromObj);
3202 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003203}
3204
3205 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003206OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003207{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003208 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003209 return 0;
3210}
3211
3212 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003213OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003214{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003215 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003216 return 0;
3217}
3218
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003219 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003220OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003221{
3222 char_u *key;
3223 int flags;
3224 long numval;
3225 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003226 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003227
Bram Moolenaard6e39182013-05-21 18:30:34 +02003228 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003229 return NULL;
3230
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003231 if (!(key = StringToChars(keyObject, &todecref)))
3232 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003233
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003234 if (*key == NUL)
3235 {
3236 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003237 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003238 return NULL;
3239 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003240
3241 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003242 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003243
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003244 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003245
3246 if (flags == 0)
3247 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003248 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003249 return NULL;
3250 }
3251
3252 if (flags & SOPT_UNSET)
3253 {
3254 Py_INCREF(Py_None);
3255 return Py_None;
3256 }
3257 else if (flags & SOPT_BOOL)
3258 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003259 PyObject *ret;
3260 ret = numval ? Py_True : Py_False;
3261 Py_INCREF(ret);
3262 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003263 }
3264 else if (flags & SOPT_NUM)
3265 return PyInt_FromLong(numval);
3266 else if (flags & SOPT_STRING)
3267 {
3268 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003269 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003270 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003271 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003272 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003273 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003274 else
3275 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003276 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003277 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003278 return NULL;
3279 }
3280 }
3281 else
3282 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003283 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003284 return NULL;
3285 }
3286}
3287
3288 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003289OptionsContains(OptionsObject *self, PyObject *keyObject)
3290{
3291 char_u *key;
3292 PyObject *todecref;
3293
3294 if (!(key = StringToChars(keyObject, &todecref)))
3295 return -1;
3296
3297 if (*key == NUL)
3298 {
3299 Py_XDECREF(todecref);
3300 return 0;
3301 }
3302
3303 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3304 {
3305 Py_XDECREF(todecref);
3306 return 1;
3307 }
3308 else
3309 {
3310 Py_XDECREF(todecref);
3311 return 0;
3312 }
3313}
3314
3315typedef struct
3316{
3317 void *lastoption;
3318 int opt_type;
3319} optiterinfo_T;
3320
3321 static PyObject *
3322OptionsIterNext(optiterinfo_T **oii)
3323{
3324 char_u *name;
3325
3326 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3327 return PyString_FromString((char *)name);
3328
3329 return NULL;
3330}
3331
3332 static PyObject *
3333OptionsIter(OptionsObject *self)
3334{
3335 optiterinfo_T *oii;
3336
3337 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3338 {
3339 PyErr_NoMemory();
3340 return NULL;
3341 }
3342
3343 oii->opt_type = self->opt_type;
3344 oii->lastoption = NULL;
3345
3346 return IterNew(oii,
3347 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3348 NULL, NULL);
3349}
3350
3351 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003352set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003353{
3354 char_u *errmsg;
3355
3356 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3357 {
3358 if (VimTryEnd())
3359 return FAIL;
3360 PyErr_SetVim((char *)errmsg);
3361 return FAIL;
3362 }
3363 return OK;
3364}
3365
3366 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003367set_option_value_for(
3368 char_u *key,
3369 int numval,
3370 char_u *stringval,
3371 int opt_flags,
3372 int opt_type,
3373 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003374{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003375 win_T *save_curwin = NULL;
3376 tabpage_T *save_curtab = NULL;
3377 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003378 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003379
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003380 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003381 switch (opt_type)
3382 {
3383 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003384 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003385 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003386 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003387 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003388 if (VimTryEnd())
3389 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003390 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003391 return -1;
3392 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003393 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3394 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003395 break;
3396 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003397 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003398 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003399 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003400 break;
3401 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003402 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003403 break;
3404 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003405 if (set_ret == FAIL)
3406 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003407 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003408}
3409
3410 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003411OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003412{
3413 char_u *key;
3414 int flags;
3415 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003416 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003417 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003418
Bram Moolenaard6e39182013-05-21 18:30:34 +02003419 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003420 return -1;
3421
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003422 if (!(key = StringToChars(keyObject, &todecref)))
3423 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003424
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003425 if (*key == NUL)
3426 {
3427 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003428 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003429 return -1;
3430 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003431
3432 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003433 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003434
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003435 if (flags == 0)
3436 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003437 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003438 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003439 return -1;
3440 }
3441
3442 if (valObject == NULL)
3443 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003444 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003445 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003446 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003447 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003448 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003449 return -1;
3450 }
3451 else if (!(flags & SOPT_GLOBAL))
3452 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003453 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003454 N_("unable to unset option %s "
3455 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003456 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003457 return -1;
3458 }
3459 else
3460 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003461 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003462 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003463 return 0;
3464 }
3465 }
3466
Bram Moolenaard6e39182013-05-21 18:30:34 +02003467 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003468
3469 if (flags & SOPT_BOOL)
3470 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003471 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003472
Bram Moolenaarb983f752013-05-15 16:11:50 +02003473 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003474 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003475 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003476 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003477 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003478 }
3479 else if (flags & SOPT_NUM)
3480 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003481 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003482
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003483 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003484 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003485 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003486 return -1;
3487 }
3488
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003489 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003490 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003491 }
3492 else
3493 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003494 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003495 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003496
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003497 if ((val = StringToChars(valObject, &todecref2)))
3498 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003499 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003500 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003501 Py_XDECREF(todecref2);
3502 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003503 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003504 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003505 }
3506
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003507 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003508
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003509 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003510}
3511
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003512static PySequenceMethods OptionsAsSeq = {
3513 0, /* sq_length */
3514 0, /* sq_concat */
3515 0, /* sq_repeat */
3516 0, /* sq_item */
3517 0, /* sq_slice */
3518 0, /* sq_ass_item */
3519 0, /* sq_ass_slice */
3520 (objobjproc) OptionsContains, /* sq_contains */
3521 0, /* sq_inplace_concat */
3522 0, /* sq_inplace_repeat */
3523};
3524
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003525static PyMappingMethods OptionsAsMapping = {
3526 (lenfunc) NULL,
3527 (binaryfunc) OptionsItem,
3528 (objobjargproc) OptionsAssItem,
3529};
3530
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003531/* Tabpage object
3532 */
3533
3534typedef struct
3535{
3536 PyObject_HEAD
3537 tabpage_T *tab;
3538} TabPageObject;
3539
3540static PyObject *WinListNew(TabPageObject *tabObject);
3541
3542static PyTypeObject TabPageType;
3543
3544 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003545CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003546{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003547 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003548 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003549 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003550 return -1;
3551 }
3552
3553 return 0;
3554}
3555
3556 static PyObject *
3557TabPageNew(tabpage_T *tab)
3558{
3559 TabPageObject *self;
3560
3561 if (TAB_PYTHON_REF(tab))
3562 {
3563 self = TAB_PYTHON_REF(tab);
3564 Py_INCREF(self);
3565 }
3566 else
3567 {
3568 self = PyObject_NEW(TabPageObject, &TabPageType);
3569 if (self == NULL)
3570 return NULL;
3571 self->tab = tab;
3572 TAB_PYTHON_REF(tab) = self;
3573 }
3574
3575 return (PyObject *)(self);
3576}
3577
3578 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003579TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003580{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003581 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3582 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003583
3584 DESTRUCTOR_FINISH(self);
3585}
3586
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003587static char *TabPageAttrs[] = {
3588 "windows", "number", "vars", "window", "valid",
3589 NULL
3590};
3591
3592 static PyObject *
3593TabPageDir(PyObject *self)
3594{
3595 return ObjectDir(self, TabPageAttrs);
3596}
3597
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003598 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003599TabPageAttrValid(TabPageObject *self, char *name)
3600{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003601 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003602
3603 if (strcmp(name, "valid") != 0)
3604 return NULL;
3605
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003606 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3607 Py_INCREF(ret);
3608 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003609}
3610
3611 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003612TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003613{
3614 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003615 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003616 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003617 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003618 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003619 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003620 else if (strcmp(name, "window") == 0)
3621 {
3622 /* For current tab window.c does not bother to set or update tp_curwin
3623 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003624 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003625 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003626 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003627 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003628 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003629 else if (strcmp(name, "__members__") == 0)
3630 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003631 return NULL;
3632}
3633
3634 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003635TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003636{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003637 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003638 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003639 else
3640 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003641 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003642
3643 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003644 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3645 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003646 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003647 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003648 }
3649}
3650
3651static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003652 /* name, function, calling, documentation */
3653 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3654 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003655};
3656
3657/*
3658 * Window list object
3659 */
3660
3661static PyTypeObject TabListType;
3662static PySequenceMethods TabListAsSeq;
3663
3664typedef struct
3665{
3666 PyObject_HEAD
3667} TabListObject;
3668
3669 static PyInt
3670TabListLength(PyObject *self UNUSED)
3671{
3672 tabpage_T *tp = first_tabpage;
3673 PyInt n = 0;
3674
3675 while (tp != NULL)
3676 {
3677 ++n;
3678 tp = tp->tp_next;
3679 }
3680
3681 return n;
3682}
3683
3684 static PyObject *
3685TabListItem(PyObject *self UNUSED, PyInt n)
3686{
3687 tabpage_T *tp;
3688
3689 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3690 if (n == 0)
3691 return TabPageNew(tp);
3692
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003693 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003694 return NULL;
3695}
3696
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003697/*
3698 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003699 */
3700
3701typedef struct
3702{
3703 PyObject_HEAD
3704 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003705 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003706} WindowObject;
3707
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003708static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003709
3710 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003711CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003712{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003713 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003714 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003715 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003716 return -1;
3717 }
3718
3719 return 0;
3720}
3721
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003722 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003723WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003724{
3725 /* We need to handle deletion of windows underneath us.
3726 * If we add a "w_python*_ref" field to the win_T structure,
3727 * then we can get at it in win_free() in vim. We then
3728 * need to create only ONE Python object per window - if
3729 * we try to create a second, just INCREF the existing one
3730 * and return it. The (single) Python object referring to
3731 * the window is stored in "w_python*_ref".
3732 * On a win_free() we set the Python object's win_T* field
3733 * to an invalid value. We trap all uses of a window
3734 * object, and reject them if the win_T* field is invalid.
3735 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003736 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003737 * w_python_ref and w_python3_ref fields respectively.
3738 */
3739
3740 WindowObject *self;
3741
3742 if (WIN_PYTHON_REF(win))
3743 {
3744 self = WIN_PYTHON_REF(win);
3745 Py_INCREF(self);
3746 }
3747 else
3748 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003749 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003750 if (self == NULL)
3751 return NULL;
3752 self->win = win;
3753 WIN_PYTHON_REF(win) = self;
3754 }
3755
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003756 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3757
Bram Moolenaar971db462013-05-12 18:44:48 +02003758 return (PyObject *)(self);
3759}
3760
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003761 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003762WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003763{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003764 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003765 if (self->win && self->win != INVALID_WINDOW_VALUE)
3766 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003767 Py_XDECREF(((PyObject *)(self->tabObject)));
3768 PyObject_GC_Del((void *)(self));
3769}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003770
Bram Moolenaar774267b2013-05-21 20:51:59 +02003771 static int
3772WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3773{
3774 Py_VISIT(((PyObject *)(self->tabObject)));
3775 return 0;
3776}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003777
Bram Moolenaar774267b2013-05-21 20:51:59 +02003778 static int
3779WindowClear(WindowObject *self)
3780{
3781 Py_CLEAR(self->tabObject);
3782 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003783}
3784
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003785 static win_T *
3786get_firstwin(TabPageObject *tabObject)
3787{
3788 if (tabObject)
3789 {
3790 if (CheckTabPage(tabObject))
3791 return NULL;
3792 /* For current tab window.c does not bother to set or update tp_firstwin
3793 */
3794 else if (tabObject->tab == curtab)
3795 return firstwin;
3796 else
3797 return tabObject->tab->tp_firstwin;
3798 }
3799 else
3800 return firstwin;
3801}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003802static char *WindowAttrs[] = {
3803 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3804 "tabpage", "valid",
3805 NULL
3806};
3807
3808 static PyObject *
3809WindowDir(PyObject *self)
3810{
3811 return ObjectDir(self, WindowAttrs);
3812}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003813
Bram Moolenaar971db462013-05-12 18:44:48 +02003814 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003815WindowAttrValid(WindowObject *self, char *name)
3816{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003817 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003818
3819 if (strcmp(name, "valid") != 0)
3820 return NULL;
3821
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003822 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3823 Py_INCREF(ret);
3824 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003825}
3826
3827 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003828WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003829{
3830 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003831 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003832 else if (strcmp(name, "cursor") == 0)
3833 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003834 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003835
3836 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3837 }
3838 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003839 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003840#ifdef FEAT_WINDOWS
3841 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003842 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003843 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003844 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003845 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003846 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003847#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003848 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003849 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003850 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003851 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3852 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003853 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003854 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003855 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003856 return NULL;
3857 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003858 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003859 }
3860 else if (strcmp(name, "tabpage") == 0)
3861 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003862 Py_INCREF(self->tabObject);
3863 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003864 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003865 else if (strcmp(name, "__members__") == 0)
3866 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003867 else
3868 return NULL;
3869}
3870
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003871 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003872WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003873{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003874 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003875 return -1;
3876
3877 if (strcmp(name, "buffer") == 0)
3878 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003879 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003880 return -1;
3881 }
3882 else if (strcmp(name, "cursor") == 0)
3883 {
3884 long lnum;
3885 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003886
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003887 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003888 return -1;
3889
Bram Moolenaard6e39182013-05-21 18:30:34 +02003890 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003891 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003892 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003893 return -1;
3894 }
3895
3896 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003897 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003898 return -1;
3899
Bram Moolenaard6e39182013-05-21 18:30:34 +02003900 self->win->w_cursor.lnum = lnum;
3901 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003902#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003903 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003904#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003905 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003906 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003907
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003908 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003909 return 0;
3910 }
3911 else if (strcmp(name, "height") == 0)
3912 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003913 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003914 win_T *savewin;
3915
Bram Moolenaardee2e312013-06-23 16:35:47 +02003916 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003917 return -1;
3918
3919#ifdef FEAT_GUI
3920 need_mouse_correct = TRUE;
3921#endif
3922 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003923 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003924
3925 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003926 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003927 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003928 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003929 return -1;
3930
3931 return 0;
3932 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003933#ifdef FEAT_WINDOWS
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003934 else if (strcmp(name, "width") == 0)
3935 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003936 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003937 win_T *savewin;
3938
Bram Moolenaardee2e312013-06-23 16:35:47 +02003939 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003940 return -1;
3941
3942#ifdef FEAT_GUI
3943 need_mouse_correct = TRUE;
3944#endif
3945 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003946 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003947
3948 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003949 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003950 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003951 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003952 return -1;
3953
3954 return 0;
3955 }
3956#endif
3957 else
3958 {
3959 PyErr_SetString(PyExc_AttributeError, name);
3960 return -1;
3961 }
3962}
3963
3964 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003965WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003966{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003967 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003968 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003969 else
3970 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003971 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003972
Bram Moolenaar6d216452013-05-12 19:00:41 +02003973 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003974 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003975 (self));
3976 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003977 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003978 }
3979}
3980
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003981static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003982 /* name, function, calling, documentation */
3983 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3984 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003985};
3986
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003987/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003988 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003989 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003990
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003991static PyTypeObject WinListType;
3992static PySequenceMethods WinListAsSeq;
3993
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003994typedef struct
3995{
3996 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003997 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003998} WinListObject;
3999
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004000 static PyObject *
4001WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004002{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004003 WinListObject *self;
4004
4005 self = PyObject_NEW(WinListObject, &WinListType);
4006 self->tabObject = tabObject;
4007 Py_INCREF(tabObject);
4008
4009 return (PyObject *)(self);
4010}
4011
4012 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004013WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004014{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004015 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004016
4017 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02004018 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004019 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02004020 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004021
4022 DESTRUCTOR_FINISH(self);
4023}
4024
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004025 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004026WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004027{
4028 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004029 PyInt n = 0;
4030
Bram Moolenaard6e39182013-05-21 18:30:34 +02004031 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004032 return -1;
4033
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004034 while (w != NULL)
4035 {
4036 ++n;
4037 w = W_NEXT(w);
4038 }
4039
4040 return n;
4041}
4042
4043 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004044WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004045{
4046 win_T *w;
4047
Bram Moolenaard6e39182013-05-21 18:30:34 +02004048 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02004049 return NULL;
4050
4051 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004052 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004053 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004055 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004056 return NULL;
4057}
4058
4059/* Convert a Python string into a Vim line.
4060 *
4061 * The result is in allocated memory. All internal nulls are replaced by
4062 * newline characters. It is an error for the string to contain newline
4063 * characters.
4064 *
4065 * On errors, the Python exception data is set, and NULL is returned.
4066 */
4067 static char *
4068StringToLine(PyObject *obj)
4069{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004070 char *str;
4071 char *save;
4072 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02004073 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004074 PyInt i;
4075 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004077 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004078 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004079 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
4080 || str == NULL)
4081 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004082 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004083 else if (PyUnicode_Check(obj))
4084 {
4085 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
4086 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087
Bram Moolenaardaa27022013-06-24 22:33:30 +02004088 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004089 || str == NULL)
4090 {
4091 Py_DECREF(bytes);
4092 return NULL;
4093 }
4094 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02004095 else
4096 {
4097#if PY_MAJOR_VERSION < 3
4098 PyErr_FORMAT(PyExc_TypeError,
4099 N_("expected str() or unicode() instance, but got %s"),
4100 Py_TYPE_NAME(obj));
4101#else
4102 PyErr_FORMAT(PyExc_TypeError,
4103 N_("expected bytes() or str() instance, but got %s"),
4104 Py_TYPE_NAME(obj));
4105#endif
4106 return NULL;
4107 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004108
4109 /*
4110 * Error checking: String must not contain newlines, as we
4111 * are replacing a single line, and we must replace it with
4112 * a single line.
4113 * A trailing newline is removed, so that append(f.readlines()) works.
4114 */
4115 p = memchr(str, '\n', len);
4116 if (p != NULL)
4117 {
4118 if (p == str + len - 1)
4119 --len;
4120 else
4121 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004122 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004123 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004124 return NULL;
4125 }
4126 }
4127
4128 /* Create a copy of the string, with internal nulls replaced by
4129 * newline characters, as is the vim convention.
4130 */
4131 save = (char *)alloc((unsigned)(len+1));
4132 if (save == NULL)
4133 {
4134 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004135 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136 return NULL;
4137 }
4138
4139 for (i = 0; i < len; ++i)
4140 {
4141 if (str[i] == '\0')
4142 save[i] = '\n';
4143 else
4144 save[i] = str[i];
4145 }
4146
4147 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004148 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004149
4150 return save;
4151}
4152
4153/* Get a line from the specified buffer. The line number is
4154 * in Vim format (1-based). The line is returned as a Python
4155 * string object.
4156 */
4157 static PyObject *
4158GetBufferLine(buf_T *buf, PyInt n)
4159{
4160 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
4161}
4162
4163
4164/* Get a list of lines from the specified buffer. The line numbers
4165 * are in Vim format (1-based). The range is from lo up to, but not
4166 * including, hi. The list is returned as a Python list of string objects.
4167 */
4168 static PyObject *
4169GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4170{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004171 PyInt i;
4172 PyInt n = hi - lo;
4173 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004174
4175 if (list == NULL)
4176 return NULL;
4177
4178 for (i = 0; i < n; ++i)
4179 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004180 PyObject *string = LineToString(
4181 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004182
4183 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004184 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004185 {
4186 Py_DECREF(list);
4187 return NULL;
4188 }
4189
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004190 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004191 }
4192
4193 /* The ownership of the Python list is passed to the caller (ie,
4194 * the caller should Py_DECREF() the object when it is finished
4195 * with it).
4196 */
4197
4198 return list;
4199}
4200
4201/*
4202 * Check if deleting lines made the cursor position invalid.
4203 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4204 * deleted).
4205 */
4206 static void
4207py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4208{
4209 if (curwin->w_cursor.lnum >= lo)
4210 {
4211 /* Adjust the cursor position if it's in/after the changed
4212 * lines. */
4213 if (curwin->w_cursor.lnum >= hi)
4214 {
4215 curwin->w_cursor.lnum += extra;
4216 check_cursor_col();
4217 }
4218 else if (extra < 0)
4219 {
4220 curwin->w_cursor.lnum = lo;
4221 check_cursor();
4222 }
4223 else
4224 check_cursor_col();
4225 changed_cline_bef_curs();
4226 }
4227 invalidate_botline();
4228}
4229
Bram Moolenaar19e60942011-06-19 00:27:51 +02004230/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004231 * Find a window that contains "buf" and switch to it.
4232 * If there is no such window, use the current window and change "curbuf".
4233 * Caller must initialize save_curbuf to NULL.
4234 * restore_win_for_buf() MUST be called later!
4235 */
4236 static void
4237switch_to_win_for_buf(
4238 buf_T *buf,
4239 win_T **save_curwinp,
4240 tabpage_T **save_curtabp,
4241 buf_T **save_curbufp)
4242{
4243 win_T *wp;
4244 tabpage_T *tp;
4245
Bram Moolenaarae38d052014-12-17 14:46:09 +01004246 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004247 switch_buffer(save_curbufp, buf);
Bram Moolenaarae38d052014-12-17 14:46:09 +01004248 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4249 {
4250 restore_win(*save_curwinp, *save_curtabp, TRUE);
4251 switch_buffer(save_curbufp, buf);
4252 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004253}
4254
4255 static void
4256restore_win_for_buf(
4257 win_T *save_curwin,
4258 tabpage_T *save_curtab,
4259 buf_T *save_curbuf)
4260{
4261 if (save_curbuf == NULL)
4262 restore_win(save_curwin, save_curtab, TRUE);
4263 else
4264 restore_buffer(save_curbuf);
4265}
4266
4267/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004268 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004269 * in Vim format (1-based). The replacement line is given as
4270 * a Python string object. The object is checked for validity
4271 * and correct format. Errors are returned as a value of FAIL.
4272 * The return value is OK on success.
4273 * If OK is returned and len_change is not NULL, *len_change
4274 * is set to the change in the buffer length.
4275 */
4276 static int
4277SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4278{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004279 buf_T *save_curbuf = NULL;
4280 win_T *save_curwin = NULL;
4281 tabpage_T *save_curtab = NULL;
4282
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004283 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004284 * There are three cases:
4285 * 1. NULL, or None - this is a deletion.
4286 * 2. A string - this is a replacement.
4287 * 3. Anything else - this is an error.
4288 */
4289 if (line == Py_None || line == NULL)
4290 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004291 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004292 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004293
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004294 VimTryStart();
4295
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004296 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004297 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004298 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004299 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004300 else
4301 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004302 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004303 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004304 if (save_curbuf == NULL)
4305 /* Only adjust marks if we managed to switch to a window that
4306 * holds the buffer, otherwise line numbers will be invalid. */
4307 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004308 }
4309
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004310 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004311
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004312 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004313 return FAIL;
4314
4315 if (len_change)
4316 *len_change = -1;
4317
4318 return OK;
4319 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004320 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004321 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004322 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004323
4324 if (save == NULL)
4325 return FAIL;
4326
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004327 VimTryStart();
4328
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004329 /* We do not need to free "save" if ml_replace() consumes it. */
4330 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004331 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004332
4333 if (u_savesub((linenr_T)n) == FAIL)
4334 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004335 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004336 vim_free(save);
4337 }
4338 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4339 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004340 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004341 vim_free(save);
4342 }
4343 else
4344 changed_bytes((linenr_T)n, 0);
4345
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004346 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004347
4348 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004349 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004350 check_cursor_col();
4351
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004352 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004353 return FAIL;
4354
4355 if (len_change)
4356 *len_change = 0;
4357
4358 return OK;
4359 }
4360 else
4361 {
4362 PyErr_BadArgument();
4363 return FAIL;
4364 }
4365}
4366
Bram Moolenaar19e60942011-06-19 00:27:51 +02004367/* Replace a range of lines in the specified buffer. The line numbers are in
4368 * Vim format (1-based). The range is from lo up to, but not including, hi.
4369 * The replacement lines are given as a Python list of string objects. The
4370 * list is checked for validity and correct format. Errors are returned as a
4371 * value of FAIL. The return value is OK on success.
4372 * If OK is returned and len_change is not NULL, *len_change
4373 * is set to the change in the buffer length.
4374 */
4375 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004376SetBufferLineList(
4377 buf_T *buf,
4378 PyInt lo,
4379 PyInt hi,
4380 PyObject *list,
4381 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004382{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004383 buf_T *save_curbuf = NULL;
4384 win_T *save_curwin = NULL;
4385 tabpage_T *save_curtab = NULL;
4386
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004387 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004388 * There are three cases:
4389 * 1. NULL, or None - this is a deletion.
4390 * 2. A list - this is a replacement.
4391 * 3. Anything else - this is an error.
4392 */
4393 if (list == Py_None || list == NULL)
4394 {
4395 PyInt i;
4396 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004397
4398 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004399 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004400 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004401
4402 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004403 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004404 else
4405 {
4406 for (i = 0; i < n; ++i)
4407 {
4408 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4409 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004410 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004411 break;
4412 }
4413 }
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004414 if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL))
4415 /* Using an existing window for the buffer, adjust the cursor
4416 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004417 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004418 if (save_curbuf == NULL)
4419 /* Only adjust marks if we managed to switch to a window that
4420 * holds the buffer, otherwise line numbers will be invalid. */
4421 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004422 }
4423
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004424 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004425
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004426 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004427 return FAIL;
4428
4429 if (len_change)
4430 *len_change = -n;
4431
4432 return OK;
4433 }
4434 else if (PyList_Check(list))
4435 {
4436 PyInt i;
4437 PyInt new_len = PyList_Size(list);
4438 PyInt old_len = hi - lo;
4439 PyInt extra = 0; /* lines added to text, can be negative */
4440 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004441
4442 if (new_len == 0) /* avoid allocating zero bytes */
4443 array = NULL;
4444 else
4445 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004446 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004447 if (array == NULL)
4448 {
4449 PyErr_NoMemory();
4450 return FAIL;
4451 }
4452 }
4453
4454 for (i = 0; i < new_len; ++i)
4455 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004456 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004457
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004458 if (!(line = PyList_GetItem(list, i)) ||
4459 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004460 {
4461 while (i)
4462 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004463 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004464 return FAIL;
4465 }
4466 }
4467
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004468 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004469 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004470
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004471 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004472 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004473
4474 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004475 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004476
4477 /* If the size of the range is reducing (ie, new_len < old_len) we
4478 * need to delete some old_len. We do this at the start, by
4479 * repeatedly deleting line "lo".
4480 */
4481 if (!PyErr_Occurred())
4482 {
4483 for (i = 0; i < old_len - new_len; ++i)
4484 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4485 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004486 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004487 break;
4488 }
4489 extra -= i;
4490 }
4491
4492 /* For as long as possible, replace the existing old_len with the
4493 * new old_len. This is a more efficient operation, as it requires
4494 * less memory allocation and freeing.
4495 */
4496 if (!PyErr_Occurred())
4497 {
4498 for (i = 0; i < old_len && i < new_len; ++i)
4499 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4500 == FAIL)
4501 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004502 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004503 break;
4504 }
4505 }
4506 else
4507 i = 0;
4508
4509 /* Now we may need to insert the remaining new old_len. If we do, we
4510 * must free the strings as we finish with them (we can't pass the
4511 * responsibility to vim in this case).
4512 */
4513 if (!PyErr_Occurred())
4514 {
4515 while (i < new_len)
4516 {
4517 if (ml_append((linenr_T)(lo + i - 1),
4518 (char_u *)array[i], 0, FALSE) == FAIL)
4519 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004520 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004521 break;
4522 }
4523 vim_free(array[i]);
4524 ++i;
4525 ++extra;
4526 }
4527 }
4528
4529 /* Free any left-over old_len, as a result of an error */
4530 while (i < new_len)
4531 {
4532 vim_free(array[i]);
4533 ++i;
4534 }
4535
4536 /* Free the array of old_len. All of its contents have now
4537 * been dealt with (either freed, or the responsibility passed
4538 * to vim.
4539 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004540 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004541
4542 /* Adjust marks. Invalidate any which lie in the
4543 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004544 * Only adjust marks if we managed to switch to a window that holds
4545 * the buffer, otherwise line numbers will be invalid. */
4546 if (save_curbuf == NULL)
4547 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004548 (long)MAXLNUM, (long)extra);
4549 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4550
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004551 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004552 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4553
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004554 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004555 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004556
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004557 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004558 return FAIL;
4559
4560 if (len_change)
4561 *len_change = new_len - old_len;
4562
4563 return OK;
4564 }
4565 else
4566 {
4567 PyErr_BadArgument();
4568 return FAIL;
4569 }
4570}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004571
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004572/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004573 * The line number is in Vim format (1-based). The lines to be inserted are
4574 * given as a Python list of string objects or as a single string. The lines
4575 * to be added are checked for validity and correct format. Errors are
4576 * returned as a value of FAIL. The return value is OK on success.
4577 * If OK is returned and len_change is not NULL, *len_change
4578 * is set to the change in the buffer length.
4579 */
4580 static int
4581InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4582{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004583 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004584 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004585 tabpage_T *save_curtab = NULL;
4586
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004587 /* First of all, we check the type of the supplied Python object.
4588 * It must be a string or a list, or the call is in error.
4589 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004590 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004591 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004592 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004593
4594 if (str == NULL)
4595 return FAIL;
4596
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004597 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004598 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004599 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004600
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004601 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004602 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004603 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004604 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004605 else if (save_curbuf == NULL)
4606 /* Only adjust marks if we managed to switch to a window that
4607 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004608 appended_lines_mark((linenr_T)n, 1L);
4609
4610 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004611 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004612 update_screen(VALID);
4613
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004614 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004615 return FAIL;
4616
4617 if (len_change)
4618 *len_change = 1;
4619
4620 return OK;
4621 }
4622 else if (PyList_Check(lines))
4623 {
4624 PyInt i;
4625 PyInt size = PyList_Size(lines);
4626 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004627
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004628 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004629 if (array == NULL)
4630 {
4631 PyErr_NoMemory();
4632 return FAIL;
4633 }
4634
4635 for (i = 0; i < size; ++i)
4636 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004637 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004638
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004639 if (!(line = PyList_GetItem(lines, i)) ||
4640 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004641 {
4642 while (i)
4643 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004644 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004645 return FAIL;
4646 }
4647 }
4648
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004649 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004650 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004651 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004652
4653 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004654 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004655 else
4656 {
4657 for (i = 0; i < size; ++i)
4658 {
4659 if (ml_append((linenr_T)(n + i),
4660 (char_u *)array[i], 0, FALSE) == FAIL)
4661 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004662 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004663
4664 /* Free the rest of the lines */
4665 while (i < size)
4666 vim_free(array[i++]);
4667
4668 break;
4669 }
4670 vim_free(array[i]);
4671 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004672 if (i > 0 && save_curbuf == NULL)
4673 /* Only adjust marks if we managed to switch to a window that
4674 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004675 appended_lines_mark((linenr_T)n, (long)i);
4676 }
4677
4678 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004679 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004680 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004681 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004682
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004683 update_screen(VALID);
4684
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004685 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004686 return FAIL;
4687
4688 if (len_change)
4689 *len_change = size;
4690
4691 return OK;
4692 }
4693 else
4694 {
4695 PyErr_BadArgument();
4696 return FAIL;
4697 }
4698}
4699
4700/*
4701 * Common routines for buffers and line ranges
4702 * -------------------------------------------
4703 */
4704
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004705typedef struct
4706{
4707 PyObject_HEAD
4708 buf_T *buf;
4709} BufferObject;
4710
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004711 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004712CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004713{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004714 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004715 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004716 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004717 return -1;
4718 }
4719
4720 return 0;
4721}
4722
4723 static PyObject *
4724RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4725{
4726 if (CheckBuffer(self))
4727 return NULL;
4728
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004729 if (end == -1)
4730 end = self->buf->b_ml.ml_line_count;
4731
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004732 if (n < 0)
4733 n += end - start + 1;
4734
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004735 if (n < 0 || n > end - start)
4736 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004737 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004738 return NULL;
4739 }
4740
4741 return GetBufferLine(self->buf, n+start);
4742}
4743
4744 static PyObject *
4745RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4746{
4747 PyInt size;
4748
4749 if (CheckBuffer(self))
4750 return NULL;
4751
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004752 if (end == -1)
4753 end = self->buf->b_ml.ml_line_count;
4754
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004755 size = end - start + 1;
4756
4757 if (lo < 0)
4758 lo = 0;
4759 else if (lo > size)
4760 lo = size;
4761 if (hi < 0)
4762 hi = 0;
4763 if (hi < lo)
4764 hi = lo;
4765 else if (hi > size)
4766 hi = size;
4767
4768 return GetBufferLineList(self->buf, lo+start, hi+start);
4769}
4770
4771 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004772RBAsItem(
4773 BufferObject *self,
4774 PyInt n,
4775 PyObject *valObject,
4776 PyInt start,
4777 PyInt end,
4778 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004779{
4780 PyInt len_change;
4781
4782 if (CheckBuffer(self))
4783 return -1;
4784
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004785 if (end == -1)
4786 end = self->buf->b_ml.ml_line_count;
4787
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004788 if (n < 0)
4789 n += end - start + 1;
4790
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004791 if (n < 0 || n > end - start)
4792 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004793 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004794 return -1;
4795 }
4796
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004797 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004798 return -1;
4799
4800 if (new_end)
4801 *new_end = end + len_change;
4802
4803 return 0;
4804}
4805
Bram Moolenaar19e60942011-06-19 00:27:51 +02004806 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004807RBAsSlice(
4808 BufferObject *self,
4809 PyInt lo,
4810 PyInt hi,
4811 PyObject *valObject,
4812 PyInt start,
4813 PyInt end,
4814 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004815{
4816 PyInt size;
4817 PyInt len_change;
4818
4819 /* Self must be a valid buffer */
4820 if (CheckBuffer(self))
4821 return -1;
4822
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004823 if (end == -1)
4824 end = self->buf->b_ml.ml_line_count;
4825
Bram Moolenaar19e60942011-06-19 00:27:51 +02004826 /* Sort out the slice range */
4827 size = end - start + 1;
4828
4829 if (lo < 0)
4830 lo = 0;
4831 else if (lo > size)
4832 lo = size;
4833 if (hi < 0)
4834 hi = 0;
4835 if (hi < lo)
4836 hi = lo;
4837 else if (hi > size)
4838 hi = size;
4839
4840 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004841 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004842 return -1;
4843
4844 if (new_end)
4845 *new_end = end + len_change;
4846
4847 return 0;
4848}
4849
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004850
4851 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004852RBAppend(
4853 BufferObject *self,
4854 PyObject *args,
4855 PyInt start,
4856 PyInt end,
4857 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004858{
4859 PyObject *lines;
4860 PyInt len_change;
4861 PyInt max;
4862 PyInt n;
4863
4864 if (CheckBuffer(self))
4865 return NULL;
4866
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004867 if (end == -1)
4868 end = self->buf->b_ml.ml_line_count;
4869
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004870 max = n = end - start + 1;
4871
4872 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4873 return NULL;
4874
4875 if (n < 0 || n > max)
4876 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004877 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004878 return NULL;
4879 }
4880
4881 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4882 return NULL;
4883
4884 if (new_end)
4885 *new_end = end + len_change;
4886
4887 Py_INCREF(Py_None);
4888 return Py_None;
4889}
4890
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004891/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004892 */
4893
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004894static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004895static PySequenceMethods RangeAsSeq;
4896static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004897
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004898typedef struct
4899{
4900 PyObject_HEAD
4901 BufferObject *buf;
4902 PyInt start;
4903 PyInt end;
4904} RangeObject;
4905
4906 static PyObject *
4907RangeNew(buf_T *buf, PyInt start, PyInt end)
4908{
4909 BufferObject *bufr;
4910 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004911 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004912 if (self == NULL)
4913 return NULL;
4914
4915 bufr = (BufferObject *)BufferNew(buf);
4916 if (bufr == NULL)
4917 {
4918 Py_DECREF(self);
4919 return NULL;
4920 }
4921 Py_INCREF(bufr);
4922
4923 self->buf = bufr;
4924 self->start = start;
4925 self->end = end;
4926
4927 return (PyObject *)(self);
4928}
4929
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004930 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004931RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004932{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004933 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004934 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004935 PyObject_GC_Del((void *)(self));
4936}
4937
4938 static int
4939RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4940{
4941 Py_VISIT(((PyObject *)(self->buf)));
4942 return 0;
4943}
4944
4945 static int
4946RangeClear(RangeObject *self)
4947{
4948 Py_CLEAR(self->buf);
4949 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004950}
4951
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004952 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004953RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004954{
4955 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004956 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004957 return -1; /* ??? */
4958
Bram Moolenaard6e39182013-05-21 18:30:34 +02004959 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004960}
4961
4962 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004963RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004964{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004965 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004966}
4967
4968 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004969RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004970{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004971 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004972}
4973
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004974static char *RangeAttrs[] = {
4975 "start", "end",
4976 NULL
4977};
4978
4979 static PyObject *
4980RangeDir(PyObject *self)
4981{
4982 return ObjectDir(self, RangeAttrs);
4983}
4984
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004985 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004986RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004987{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004988 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004989}
4990
4991 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004992RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004993{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004994 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004995 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4996 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004997 else
4998 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004999 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005000
5001 if (name == NULL)
5002 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005003
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005004 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02005005 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005006 }
5007}
5008
5009static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005010 /* name, function, calling, documentation */
5011 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005012 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
5013 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005014};
5015
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005016static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005017static PySequenceMethods BufferAsSeq;
5018static PyMappingMethods BufferAsMapping;
5019
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005020 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02005021BufferNew(buf_T *buf)
5022{
5023 /* We need to handle deletion of buffers underneath us.
5024 * If we add a "b_python*_ref" field to the buf_T structure,
5025 * then we can get at it in buf_freeall() in vim. We then
5026 * need to create only ONE Python object per buffer - if
5027 * we try to create a second, just INCREF the existing one
5028 * and return it. The (single) Python object referring to
5029 * the buffer is stored in "b_python*_ref".
5030 * Question: what to do on a buf_freeall(). We'll probably
5031 * have to either delete the Python object (DECREF it to
5032 * zero - a bad idea, as it leaves dangling refs!) or
5033 * set the buf_T * value to an invalid value (-1?), which
5034 * means we need checks in all access functions... Bah.
5035 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005036 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02005037 * b_python_ref and b_python3_ref fields respectively.
5038 */
5039
5040 BufferObject *self;
5041
5042 if (BUF_PYTHON_REF(buf) != NULL)
5043 {
5044 self = BUF_PYTHON_REF(buf);
5045 Py_INCREF(self);
5046 }
5047 else
5048 {
5049 self = PyObject_NEW(BufferObject, &BufferType);
5050 if (self == NULL)
5051 return NULL;
5052 self->buf = buf;
5053 BUF_PYTHON_REF(buf) = self;
5054 }
5055
5056 return (PyObject *)(self);
5057}
5058
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005059 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02005060BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005061{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005062 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
5063 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005064
5065 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005066}
5067
Bram Moolenaar971db462013-05-12 18:44:48 +02005068 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02005069BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02005070{
5071 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02005072 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02005073 return -1; /* ??? */
5074
Bram Moolenaard6e39182013-05-21 18:30:34 +02005075 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02005076}
5077
5078 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005079BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02005080{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005081 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005082}
5083
5084 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005085BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02005086{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005087 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02005088}
5089
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005090static char *BufferAttrs[] = {
5091 "name", "number", "vars", "options", "valid",
5092 NULL
5093};
5094
5095 static PyObject *
5096BufferDir(PyObject *self)
5097{
5098 return ObjectDir(self, BufferAttrs);
5099}
5100
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005101 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005102BufferAttrValid(BufferObject *self, char *name)
5103{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005104 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005105
5106 if (strcmp(name, "valid") != 0)
5107 return NULL;
5108
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005109 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
5110 Py_INCREF(ret);
5111 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02005112}
5113
5114 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005115BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005116{
5117 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02005118 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02005119 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005120 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005121 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005122 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02005123 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005124 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02005125 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
5126 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005127 else if (strcmp(name, "__members__") == 0)
5128 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005129 else
5130 return NULL;
5131}
5132
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005133 static int
5134BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
5135{
5136 if (CheckBuffer(self))
5137 return -1;
5138
5139 if (strcmp(name, "name") == 0)
5140 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005141 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005142 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005143 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005144 PyObject *todecref;
5145
5146 if (!(val = StringToChars(valObject, &todecref)))
5147 return -1;
5148
5149 VimTryStart();
5150 /* Using aucmd_*: autocommands will be executed by rename_buffer */
5151 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005152 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005153 aucmd_restbuf(&aco);
5154 Py_XDECREF(todecref);
5155 if (VimTryEnd())
5156 return -1;
5157
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005158 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005159 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005160 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005161 return -1;
5162 }
5163 return 0;
5164 }
5165 else
5166 {
5167 PyErr_SetString(PyExc_AttributeError, name);
5168 return -1;
5169 }
5170}
5171
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005172 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005173BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005174{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005175 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005176}
5177
5178 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005179BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005180{
5181 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005182 char_u *pmark;
5183 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02005184 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005185 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005186
Bram Moolenaard6e39182013-05-21 18:30:34 +02005187 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005188 return NULL;
5189
Bram Moolenaar389a1792013-06-23 13:00:44 +02005190 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005191 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005192
Bram Moolenaar389a1792013-06-23 13:00:44 +02005193 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005194 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005195 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005196 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005197 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005198 return NULL;
5199 }
5200
5201 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005202
5203 Py_XDECREF(todecref);
5204
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005205 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005206 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005207 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02005208 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005209 if (VimTryEnd())
5210 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005211
5212 if (posp == NULL)
5213 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005214 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005215 return NULL;
5216 }
5217
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005218 if (posp->lnum <= 0)
5219 {
5220 /* Or raise an error? */
5221 Py_INCREF(Py_None);
5222 return Py_None;
5223 }
5224
5225 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5226}
5227
5228 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005229BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005230{
5231 PyInt start;
5232 PyInt end;
5233
Bram Moolenaard6e39182013-05-21 18:30:34 +02005234 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005235 return NULL;
5236
5237 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5238 return NULL;
5239
Bram Moolenaard6e39182013-05-21 18:30:34 +02005240 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005241}
5242
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005243 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005244BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005245{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005246 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005247 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005248 else
5249 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005250 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005251
5252 if (name == NULL)
5253 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005254
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005255 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005256 }
5257}
5258
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005259static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005260 /* name, function, calling, documentation */
5261 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005262 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005263 {"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 +02005264 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5265 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005266};
5267
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005268/*
5269 * Buffer list object - Implementation
5270 */
5271
5272static PyTypeObject BufMapType;
5273
5274typedef struct
5275{
5276 PyObject_HEAD
5277} BufMapObject;
5278
5279 static PyInt
5280BufMapLength(PyObject *self UNUSED)
5281{
5282 buf_T *b = firstbuf;
5283 PyInt n = 0;
5284
5285 while (b)
5286 {
5287 ++n;
5288 b = b->b_next;
5289 }
5290
5291 return n;
5292}
5293
5294 static PyObject *
5295BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5296{
5297 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005298 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005299
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005300 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005301 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005302
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005303 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005304
5305 if (b)
5306 return BufferNew(b);
5307 else
5308 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005309 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005310 return NULL;
5311 }
5312}
5313
5314 static void
5315BufMapIterDestruct(PyObject *buffer)
5316{
5317 /* Iteration was stopped before all buffers were processed */
5318 if (buffer)
5319 {
5320 Py_DECREF(buffer);
5321 }
5322}
5323
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005324 static int
5325BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5326{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005327 if (buffer)
5328 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005329 return 0;
5330}
5331
5332 static int
5333BufMapIterClear(PyObject **buffer)
5334{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005335 if (*buffer)
5336 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005337 return 0;
5338}
5339
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005340 static PyObject *
5341BufMapIterNext(PyObject **buffer)
5342{
5343 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005344 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005345
5346 if (!*buffer)
5347 return NULL;
5348
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005349 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005350
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005351 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005352 {
5353 *buffer = NULL;
5354 return NULL;
5355 }
5356
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005357 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005358 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005359 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005360 return NULL;
5361 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005362 /* Do not increment reference: we no longer hold it (decref), but whoever
5363 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005364 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005365}
5366
5367 static PyObject *
5368BufMapIter(PyObject *self UNUSED)
5369{
5370 PyObject *buffer;
5371
5372 buffer = BufferNew(firstbuf);
5373 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005374 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5375 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005376}
5377
5378static PyMappingMethods BufMapAsMapping = {
5379 (lenfunc) BufMapLength,
5380 (binaryfunc) BufMapItem,
5381 (objobjargproc) 0,
5382};
5383
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005384/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005385 */
5386
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005387static char *CurrentAttrs[] = {
5388 "buffer", "window", "line", "range", "tabpage",
5389 NULL
5390};
5391
5392 static PyObject *
5393CurrentDir(PyObject *self)
5394{
5395 return ObjectDir(self, CurrentAttrs);
5396}
5397
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005398 static PyObject *
5399CurrentGetattr(PyObject *self UNUSED, char *name)
5400{
5401 if (strcmp(name, "buffer") == 0)
5402 return (PyObject *)BufferNew(curbuf);
5403 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005404 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005405 else if (strcmp(name, "tabpage") == 0)
5406 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005407 else if (strcmp(name, "line") == 0)
5408 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5409 else if (strcmp(name, "range") == 0)
5410 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005411 else if (strcmp(name, "__members__") == 0)
5412 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005413 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005414#if PY_MAJOR_VERSION < 3
5415 return Py_FindMethod(WindowMethods, self, name);
5416#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005417 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005418#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005419}
5420
5421 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005422CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005423{
5424 if (strcmp(name, "line") == 0)
5425 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005426 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5427 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005428 return -1;
5429
5430 return 0;
5431 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005432 else if (strcmp(name, "buffer") == 0)
5433 {
5434 int count;
5435
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005436 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005437 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005438 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005439 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005440 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005441 return -1;
5442 }
5443
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005444 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005445 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005446 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005447
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005448 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005449 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5450 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005451 if (VimTryEnd())
5452 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005453 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005454 return -1;
5455 }
5456
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005457 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005458 }
5459 else if (strcmp(name, "window") == 0)
5460 {
5461 int count;
5462
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005463 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005464 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005465 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005466 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005467 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005468 return -1;
5469 }
5470
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005471 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005472 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005473 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005474
5475 if (!count)
5476 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005477 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005478 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005479 return -1;
5480 }
5481
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005482 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005483 win_goto(((WindowObject *)(valObject))->win);
5484 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005485 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005486 if (VimTryEnd())
5487 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005488 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005489 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005490 return -1;
5491 }
5492
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005493 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005494 }
5495 else if (strcmp(name, "tabpage") == 0)
5496 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005497 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005498 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005499 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005500 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005501 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005502 return -1;
5503 }
5504
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005505 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005506 return -1;
5507
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005508 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005509 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5510 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005511 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005512 if (VimTryEnd())
5513 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005514 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005515 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005516 return -1;
5517 }
5518
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005519 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005520 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005521 else
5522 {
5523 PyErr_SetString(PyExc_AttributeError, name);
5524 return -1;
5525 }
5526}
5527
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005528static struct PyMethodDef CurrentMethods[] = {
5529 /* name, function, calling, documentation */
5530 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5531 { NULL, NULL, 0, NULL}
5532};
5533
Bram Moolenaardb913952012-06-29 12:54:53 +02005534 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005535init_range_cmd(exarg_T *eap)
5536{
5537 RangeStart = eap->line1;
5538 RangeEnd = eap->line2;
5539}
5540
5541 static void
5542init_range_eval(typval_T *rettv UNUSED)
5543{
5544 RangeStart = (PyInt) curwin->w_cursor.lnum;
5545 RangeEnd = RangeStart;
5546}
5547
5548 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005549run_cmd(const char *cmd, void *arg UNUSED
5550#ifdef PY_CAN_RECURSE
5551 , PyGILState_STATE *pygilstate UNUSED
5552#endif
5553 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005554{
Bram Moolenaar41009372013-07-01 22:03:04 +02005555 PyObject *run_ret;
5556 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5557 if (run_ret != NULL)
5558 {
5559 Py_DECREF(run_ret);
5560 }
5561 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5562 {
5563 EMSG2(_(e_py_systemexit), "python");
5564 PyErr_Clear();
5565 }
5566 else
5567 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005568}
5569
5570static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5571static int code_hdr_len = 30;
5572
5573 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005574run_do(const char *cmd, void *arg UNUSED
5575#ifdef PY_CAN_RECURSE
5576 , PyGILState_STATE *pygilstate
5577#endif
5578 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005579{
5580 PyInt lnum;
5581 size_t len;
5582 char *code;
5583 int status;
5584 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005585 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005586
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005587 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005588 {
5589 EMSG(_("cannot save undo information"));
5590 return;
5591 }
5592
5593 len = code_hdr_len + STRLEN(cmd);
5594 code = PyMem_New(char, len + 1);
5595 memcpy(code, code_hdr, code_hdr_len);
5596 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005597 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5598 status = -1;
5599 if (run_ret != NULL)
5600 {
5601 status = 0;
5602 Py_DECREF(run_ret);
5603 }
5604 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5605 {
5606 PyMem_Free(code);
5607 EMSG2(_(e_py_systemexit), "python");
5608 PyErr_Clear();
5609 return;
5610 }
5611 else
5612 PyErr_PrintEx(1);
5613
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005614 PyMem_Free(code);
5615
5616 if (status)
5617 {
5618 EMSG(_("failed to run the code"));
5619 return;
5620 }
5621
5622 status = 0;
5623 pymain = PyImport_AddModule("__main__");
5624 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005625#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005626 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005627#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005628
5629 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5630 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005631 PyObject *line;
5632 PyObject *linenr;
5633 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005634
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005635#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005636 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005637#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005638 if (!(line = GetBufferLine(curbuf, lnum)))
5639 goto err;
5640 if (!(linenr = PyInt_FromLong((long) lnum)))
5641 {
5642 Py_DECREF(line);
5643 goto err;
5644 }
5645 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5646 Py_DECREF(line);
5647 Py_DECREF(linenr);
5648 if (!ret)
5649 goto err;
5650
5651 if (ret != Py_None)
5652 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5653 goto err;
5654
5655 Py_XDECREF(ret);
5656 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005657#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005658 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005659#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005660 }
5661 goto out;
5662err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005663#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005664 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005665#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005666 PyErr_PrintEx(0);
5667 PythonIO_Flush();
5668 status = 1;
5669out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005670#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005671 if (!status)
5672 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005673#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005674 Py_DECREF(pyfunc);
5675 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5676 if (status)
5677 return;
5678 check_cursor();
5679 update_curbuf(NOT_VALID);
5680}
5681
5682 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005683run_eval(const char *cmd, typval_T *rettv
5684#ifdef PY_CAN_RECURSE
5685 , PyGILState_STATE *pygilstate UNUSED
5686#endif
5687 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005688{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005689 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005690
Bram Moolenaar41009372013-07-01 22:03:04 +02005691 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005692 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005693 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005694 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005695 {
5696 EMSG2(_(e_py_systemexit), "python");
5697 PyErr_Clear();
5698 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005699 else
5700 {
5701 if (PyErr_Occurred() && !msg_silent)
5702 PyErr_PrintEx(0);
5703 EMSG(_("E858: Eval did not return a valid python object"));
5704 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005705 }
5706 else
5707 {
Bram Moolenaar77324fc2016-01-17 22:37:03 +01005708 if (run_ret != Py_None && ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005709 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005710 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005711 }
5712 PyErr_Clear();
5713}
5714
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005715 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005716set_ref_in_py(const int copyID)
5717{
5718 pylinkedlist_T *cur;
5719 dict_T *dd;
5720 list_T *ll;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005721 int i;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005722 int abort = FALSE;
Bram Moolenaar8110a092016-04-14 15:56:09 +02005723 FunctionObject *func;
Bram Moolenaardb913952012-06-29 12:54:53 +02005724
5725 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005726 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005727 for (cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005728 {
5729 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5730 if (dd->dv_copyID != copyID)
5731 {
5732 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005733 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005734 }
5735 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005736 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005737
5738 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005739 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02005740 for (cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005741 {
5742 ll = ((ListObject *) (cur->pll_obj))->list;
5743 if (ll->lv_copyID != copyID)
5744 {
5745 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005746 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005747 }
5748 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005749 }
5750
Bram Moolenaar8110a092016-04-14 15:56:09 +02005751 if (lastfunc != NULL)
5752 {
5753 for (cur = lastfunc ; !abort && cur != NULL ; cur = cur->pll_prev)
5754 {
5755 func = (FunctionObject *) cur->pll_obj;
5756 if (func->self != NULL && func->self->dv_copyID != copyID)
5757 {
5758 func->self->dv_copyID = copyID;
5759 abort = abort || set_ref_in_ht(
5760 &func->self->dv_hashtab, copyID, NULL);
5761 }
5762 if (func->argc)
5763 for (i = 0; !abort && i < func->argc; ++i)
5764 abort = abort
5765 || set_ref_in_item(&func->argv[i], copyID, NULL, NULL);
5766 }
5767 }
5768
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005769 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005770}
5771
5772 static int
5773set_string_copy(char_u *str, typval_T *tv)
5774{
5775 tv->vval.v_string = vim_strsave(str);
5776 if (tv->vval.v_string == NULL)
5777 {
5778 PyErr_NoMemory();
5779 return -1;
5780 }
5781 return 0;
5782}
5783
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005784 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005785pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005786{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005787 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005788 char_u *key;
5789 dictitem_T *di;
5790 PyObject *keyObject;
5791 PyObject *valObject;
5792 Py_ssize_t iter = 0;
5793
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005794 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005795 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005796
5797 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005798 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005799
5800 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5801 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005802 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005803
Bram Moolenaara03e6312013-05-29 22:49:26 +02005804 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005805 {
5806 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005807 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005808 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005809
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005810 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005811 {
5812 dict_unref(dict);
5813 return -1;
5814 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005815
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005816 if (*key == NUL)
5817 {
5818 dict_unref(dict);
5819 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005820 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005821 return -1;
5822 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005823
5824 di = dictitem_alloc(key);
5825
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005826 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005827
5828 if (di == NULL)
5829 {
5830 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005831 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005832 return -1;
5833 }
5834 di->di_tv.v_lock = 0;
5835
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005836 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005837 {
5838 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005839 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005840 return -1;
5841 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005842
5843 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005844 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005845 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005846 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005847 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005848 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005849 return -1;
5850 }
5851 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005852
5853 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005854 return 0;
5855}
5856
5857 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005858pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005859{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005860 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005861 char_u *key;
5862 dictitem_T *di;
5863 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005864 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005865 PyObject *keyObject;
5866 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005867
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005868 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005869 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005870
5871 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005872 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005873
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005874 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005875 {
5876 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005877 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005878 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005879
5880 if (!(iterator = PyObject_GetIter(list)))
5881 {
5882 dict_unref(dict);
5883 Py_DECREF(list);
5884 return -1;
5885 }
5886 Py_DECREF(list);
5887
5888 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005889 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005890 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005891
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005892 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005893 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005894 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005895 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005896 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005897 return -1;
5898 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005899
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005900 if (*key == NUL)
5901 {
5902 Py_DECREF(keyObject);
5903 Py_DECREF(iterator);
5904 Py_XDECREF(todecref);
5905 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005906 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005907 return -1;
5908 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005909
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005910 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005911 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005912 Py_DECREF(keyObject);
5913 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005914 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005915 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005916 return -1;
5917 }
5918
5919 di = dictitem_alloc(key);
5920
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005921 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005922 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005923
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005924 if (di == NULL)
5925 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005926 Py_DECREF(iterator);
5927 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005928 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005929 PyErr_NoMemory();
5930 return -1;
5931 }
5932 di->di_tv.v_lock = 0;
5933
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005934 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005935 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005936 Py_DECREF(iterator);
5937 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005938 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005939 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005940 return -1;
5941 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005942
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005943 Py_DECREF(valObject);
5944
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005945 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005946 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005947 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005948 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005949 dictitem_free(di);
5950 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005951 return -1;
5952 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005953 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005954 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005955 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005956 return 0;
5957}
5958
5959 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005960pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005961{
5962 list_T *l;
5963
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005964 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005965 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005966
5967 tv->v_type = VAR_LIST;
5968 tv->vval.v_list = l;
5969
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005970 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005971 {
5972 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005973 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005974 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005975
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005976 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005977 return 0;
5978}
5979
Bram Moolenaardb913952012-06-29 12:54:53 +02005980typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5981
5982 static int
5983convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005984 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005985{
5986 PyObject *capsule;
5987 char hexBuf[sizeof(void *) * 2 + 3];
5988
5989 sprintf(hexBuf, "%p", obj);
5990
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005991# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005992 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005993# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005994 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005995# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005996 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005997 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005998# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005999 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02006000# else
6001 capsule = PyCObject_FromVoidPtr(tv, NULL);
6002# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02006003 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
6004 {
6005 Py_DECREF(capsule);
6006 tv->v_type = VAR_UNKNOWN;
6007 return -1;
6008 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006009
6010 Py_DECREF(capsule);
6011
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006012 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006013 {
6014 tv->v_type = VAR_UNKNOWN;
6015 return -1;
6016 }
6017 /* As we are not using copy_tv which increments reference count we must
6018 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01006019 if (tv->v_type == VAR_DICT)
6020 ++tv->vval.v_dict->dv_refcount;
6021 else if (tv->v_type == VAR_LIST)
6022 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02006023 }
6024 else
6025 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006026 typval_T *v;
6027
6028# ifdef PY_USE_CAPSULE
6029 v = PyCapsule_GetPointer(capsule, NULL);
6030# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02006031 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02006032# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02006033 copy_tv(v, tv);
6034 }
6035 return 0;
6036}
6037
6038 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02006039ConvertFromPyMapping(PyObject *obj, typval_T *tv)
6040{
6041 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006042 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006043
6044 if (!(lookup_dict = PyDict_New()))
6045 return -1;
6046
6047 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
6048 {
6049 tv->v_type = VAR_DICT;
6050 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6051 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006052 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006053 }
6054 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006055 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006056 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006057 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02006058 else
6059 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006060 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006061 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006062 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006063 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006064 }
6065 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006066 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006067}
6068
6069 static int
Bram Moolenaar8110a092016-04-14 15:56:09 +02006070ConvertFromPySequence(PyObject *obj, typval_T *tv)
6071{
6072 PyObject *lookup_dict;
Bram Moolenaar66210042016-04-15 20:40:41 +02006073 int ret;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006074
6075 if (!(lookup_dict = PyDict_New()))
6076 return -1;
6077
6078 if (PyType_IsSubtype(obj->ob_type, &ListType))
6079 {
6080 tv->v_type = VAR_LIST;
6081 tv->vval.v_list = (((ListObject *)(obj))->list);
6082 ++tv->vval.v_list->lv_refcount;
Bram Moolenaar66210042016-04-15 20:40:41 +02006083 ret = 0;
Bram Moolenaar8110a092016-04-14 15:56:09 +02006084 }
6085 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaar66210042016-04-15 20:40:41 +02006086 ret = convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaar8110a092016-04-14 15:56:09 +02006087 else
6088 {
6089 PyErr_FORMAT(PyExc_TypeError,
6090 N_("unable to convert %s to vim list"),
6091 Py_TYPE_NAME(obj));
6092 ret = -1;
6093 }
6094 Py_DECREF(lookup_dict);
6095 return ret;
6096}
6097
6098 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02006099ConvertFromPyObject(PyObject *obj, typval_T *tv)
6100{
6101 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006102 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006103
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006104 if (!(lookup_dict = PyDict_New()))
6105 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006106 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006107 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006108 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02006109}
6110
6111 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006112_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02006113{
Bram Moolenaara9922d62013-05-30 13:01:18 +02006114 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006115 {
6116 tv->v_type = VAR_DICT;
6117 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
6118 ++tv->vval.v_dict->dv_refcount;
6119 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006120 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006121 {
6122 tv->v_type = VAR_LIST;
6123 tv->vval.v_list = (((ListObject *)(obj))->list);
6124 ++tv->vval.v_list->lv_refcount;
6125 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02006126 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02006127 {
Bram Moolenaar8110a092016-04-14 15:56:09 +02006128 FunctionObject *func = (FunctionObject *) obj;
6129 if (func->self != NULL || func->argv != NULL)
6130 {
6131 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
6132 set_partial(func, pt, TRUE);
6133 tv->vval.v_partial = pt;
6134 tv->v_type = VAR_PARTIAL;
6135 }
6136 else
6137 {
6138 if (set_string_copy(func->name, tv) == -1)
6139 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006140
Bram Moolenaar8110a092016-04-14 15:56:09 +02006141 tv->v_type = VAR_FUNC;
6142 }
6143 func_ref(func->name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006144 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006145 else if (PyBytes_Check(obj))
6146 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006147 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006148
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006149 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006150 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006151 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006152 return -1;
6153
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006154 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02006155 return -1;
6156
6157 tv->v_type = VAR_STRING;
6158 }
6159 else if (PyUnicode_Check(obj))
6160 {
6161 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006162 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02006163
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02006164 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02006165 if (bytes == NULL)
6166 return -1;
6167
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006168 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02006169 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006170 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02006171 return -1;
6172
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006173 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02006174 {
6175 Py_XDECREF(bytes);
6176 return -1;
6177 }
6178 Py_XDECREF(bytes);
6179
6180 tv->v_type = VAR_STRING;
6181 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02006182#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02006183 else if (PyInt_Check(obj))
6184 {
6185 tv->v_type = VAR_NUMBER;
6186 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006187 if (PyErr_Occurred())
6188 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006189 }
6190#endif
6191 else if (PyLong_Check(obj))
6192 {
6193 tv->v_type = VAR_NUMBER;
6194 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006195 if (PyErr_Occurred())
6196 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02006197 }
6198 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006199 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006200#ifdef FEAT_FLOAT
6201 else if (PyFloat_Check(obj))
6202 {
6203 tv->v_type = VAR_FLOAT;
6204 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
6205 }
6206#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02006207 else if (PyObject_HasAttrString(obj, "keys"))
6208 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006209 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006210 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006211 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006212 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02006213 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02006214 else if (PyNumber_Check(obj))
6215 {
6216 PyObject *num;
6217
6218 if (!(num = PyNumber_Long(obj)))
6219 return -1;
6220
6221 tv->v_type = VAR_NUMBER;
6222 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
6223
6224 Py_DECREF(num);
6225 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006226 else
6227 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006228 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006229 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006230 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006231 return -1;
6232 }
6233 return 0;
6234}
6235
6236 static PyObject *
6237ConvertToPyObject(typval_T *tv)
6238{
Bram Moolenaar8110a092016-04-14 15:56:09 +02006239 typval_T *argv;
6240 int i;
Bram Moolenaardb913952012-06-29 12:54:53 +02006241 if (tv == NULL)
6242 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006243 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006244 return NULL;
6245 }
6246 switch (tv->v_type)
6247 {
6248 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006249 return PyBytes_FromString(tv->vval.v_string == NULL
6250 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006251 case VAR_NUMBER:
6252 return PyLong_FromLong((long) tv->vval.v_number);
6253#ifdef FEAT_FLOAT
6254 case VAR_FLOAT:
6255 return PyFloat_FromDouble((double) tv->vval.v_float);
6256#endif
6257 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006258 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006260 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006261 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006262 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006263 ? (char_u *)"" : tv->vval.v_string,
6264 0, NULL, NULL);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006265 case VAR_PARTIAL:
Bram Moolenaar8110a092016-04-14 15:56:09 +02006266 if (tv->vval.v_partial->pt_argc)
6267 {
6268 argv = PyMem_New(typval_T, (size_t)tv->vval.v_partial->pt_argc);
6269 for (i = 0; i < tv->vval.v_partial->pt_argc; i++)
6270 copy_tv(&tv->vval.v_partial->pt_argv[i], &argv[i]);
6271 }
6272 else
6273 argv = NULL;
6274 if (tv->vval.v_partial->pt_dict != NULL)
6275 tv->vval.v_partial->pt_dict->dv_refcount++;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006276 return NEW_FUNCTION(tv->vval.v_partial == NULL
Bram Moolenaar8110a092016-04-14 15:56:09 +02006277 ? (char_u *)"" : tv->vval.v_partial->pt_name,
6278 tv->vval.v_partial->pt_argc, argv,
6279 tv->vval.v_partial->pt_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006280 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006281 case VAR_CHANNEL:
6282 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006283 Py_INCREF(Py_None);
6284 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006285 case VAR_SPECIAL:
6286 switch (tv->vval.v_number)
6287 {
6288 case VVAL_FALSE: return AlwaysFalse(NULL);
6289 case VVAL_TRUE: return AlwaysTrue(NULL);
6290 case VVAL_NONE:
6291 case VVAL_NULL: return AlwaysNone(NULL);
6292 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006293 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006294 return NULL;
6295 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006296 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006297}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006298
6299typedef struct
6300{
6301 PyObject_HEAD
6302} CurrentObject;
6303static PyTypeObject CurrentType;
6304
6305 static void
6306init_structs(void)
6307{
6308 vim_memset(&OutputType, 0, sizeof(OutputType));
6309 OutputType.tp_name = "vim.message";
6310 OutputType.tp_basicsize = sizeof(OutputObject);
6311 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6312 OutputType.tp_doc = "vim message object";
6313 OutputType.tp_methods = OutputMethods;
6314#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006315 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6316 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006317 OutputType.tp_alloc = call_PyType_GenericAlloc;
6318 OutputType.tp_new = call_PyType_GenericNew;
6319 OutputType.tp_free = call_PyObject_Free;
6320#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006321 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6322 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006323#endif
6324
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006325 vim_memset(&IterType, 0, sizeof(IterType));
6326 IterType.tp_name = "vim.iter";
6327 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006328 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006329 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006330 IterType.tp_iter = (getiterfunc)IterIter;
6331 IterType.tp_iternext = (iternextfunc)IterNext;
6332 IterType.tp_dealloc = (destructor)IterDestructor;
6333 IterType.tp_traverse = (traverseproc)IterTraverse;
6334 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006335
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006336 vim_memset(&BufferType, 0, sizeof(BufferType));
6337 BufferType.tp_name = "vim.buffer";
6338 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006339 BufferType.tp_dealloc = (destructor)BufferDestructor;
6340 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006341 BufferType.tp_as_sequence = &BufferAsSeq;
6342 BufferType.tp_as_mapping = &BufferAsMapping;
6343 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6344 BufferType.tp_doc = "vim buffer object";
6345 BufferType.tp_methods = BufferMethods;
6346#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006347 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006348 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006349 BufferType.tp_alloc = call_PyType_GenericAlloc;
6350 BufferType.tp_new = call_PyType_GenericNew;
6351 BufferType.tp_free = call_PyObject_Free;
6352#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006353 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006354 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006355#endif
6356
6357 vim_memset(&WindowType, 0, sizeof(WindowType));
6358 WindowType.tp_name = "vim.window";
6359 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006360 WindowType.tp_dealloc = (destructor)WindowDestructor;
6361 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006362 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006363 WindowType.tp_doc = "vim Window object";
6364 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006365 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6366 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006367#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006368 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6369 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006370 WindowType.tp_alloc = call_PyType_GenericAlloc;
6371 WindowType.tp_new = call_PyType_GenericNew;
6372 WindowType.tp_free = call_PyObject_Free;
6373#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006374 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6375 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006376#endif
6377
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006378 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6379 TabPageType.tp_name = "vim.tabpage";
6380 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006381 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6382 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006383 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6384 TabPageType.tp_doc = "vim tab page object";
6385 TabPageType.tp_methods = TabPageMethods;
6386#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006387 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006388 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6389 TabPageType.tp_new = call_PyType_GenericNew;
6390 TabPageType.tp_free = call_PyObject_Free;
6391#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006392 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006393#endif
6394
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006395 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6396 BufMapType.tp_name = "vim.bufferlist";
6397 BufMapType.tp_basicsize = sizeof(BufMapObject);
6398 BufMapType.tp_as_mapping = &BufMapAsMapping;
6399 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006400 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006401 BufferType.tp_doc = "vim buffer list";
6402
6403 vim_memset(&WinListType, 0, sizeof(WinListType));
6404 WinListType.tp_name = "vim.windowlist";
6405 WinListType.tp_basicsize = sizeof(WinListType);
6406 WinListType.tp_as_sequence = &WinListAsSeq;
6407 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6408 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006409 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006410
6411 vim_memset(&TabListType, 0, sizeof(TabListType));
6412 TabListType.tp_name = "vim.tabpagelist";
6413 TabListType.tp_basicsize = sizeof(TabListType);
6414 TabListType.tp_as_sequence = &TabListAsSeq;
6415 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6416 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006417
6418 vim_memset(&RangeType, 0, sizeof(RangeType));
6419 RangeType.tp_name = "vim.range";
6420 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006421 RangeType.tp_dealloc = (destructor)RangeDestructor;
6422 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006423 RangeType.tp_as_sequence = &RangeAsSeq;
6424 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006425 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006426 RangeType.tp_doc = "vim Range object";
6427 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006428 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6429 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006430#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006431 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006432 RangeType.tp_alloc = call_PyType_GenericAlloc;
6433 RangeType.tp_new = call_PyType_GenericNew;
6434 RangeType.tp_free = call_PyObject_Free;
6435#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006436 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006437#endif
6438
6439 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6440 CurrentType.tp_name = "vim.currentdata";
6441 CurrentType.tp_basicsize = sizeof(CurrentObject);
6442 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6443 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006444 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006445#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006446 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6447 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006448#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006449 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6450 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006451#endif
6452
6453 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6454 DictionaryType.tp_name = "vim.dictionary";
6455 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006456 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006457 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006458 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006459 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006460 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6461 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006462 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6463 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6464 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006465#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006466 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6467 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006468#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006469 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6470 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006471#endif
6472
6473 vim_memset(&ListType, 0, sizeof(ListType));
6474 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006475 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006476 ListType.tp_basicsize = sizeof(ListObject);
6477 ListType.tp_as_sequence = &ListAsSeq;
6478 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006479 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006480 ListType.tp_doc = "list pushing modifications to vim structure";
6481 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006482 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006483 ListType.tp_new = (newfunc)ListConstructor;
6484 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006485#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006486 ListType.tp_getattro = (getattrofunc)ListGetattro;
6487 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006488#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006489 ListType.tp_getattr = (getattrfunc)ListGetattr;
6490 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006491#endif
6492
6493 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006494 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006495 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006496 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6497 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006498 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006499 FunctionType.tp_doc = "object that calls vim function";
6500 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006501 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006502 FunctionType.tp_new = (newfunc)FunctionConstructor;
6503 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006504#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006505 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006506#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006507 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006508#endif
6509
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006510 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6511 OptionsType.tp_name = "vim.options";
6512 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006513 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006514 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006515 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006516 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006517 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006518 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6519 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6520 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006521
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006522 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6523 LoaderType.tp_name = "vim.Loader";
6524 LoaderType.tp_basicsize = sizeof(LoaderObject);
6525 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6526 LoaderType.tp_doc = "vim message object";
6527 LoaderType.tp_methods = LoaderMethods;
6528 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6529
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006530#if PY_MAJOR_VERSION >= 3
6531 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6532 vimmodule.m_name = "vim";
6533 vimmodule.m_doc = "Vim Python interface\n";
6534 vimmodule.m_size = -1;
6535 vimmodule.m_methods = VimMethods;
6536#endif
6537}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006538
6539#define PYTYPE_READY(type) \
6540 if (PyType_Ready(&type)) \
6541 return -1;
6542
6543 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006544init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006545{
6546 PYTYPE_READY(IterType);
6547 PYTYPE_READY(BufferType);
6548 PYTYPE_READY(RangeType);
6549 PYTYPE_READY(WindowType);
6550 PYTYPE_READY(TabPageType);
6551 PYTYPE_READY(BufMapType);
6552 PYTYPE_READY(WinListType);
6553 PYTYPE_READY(TabListType);
6554 PYTYPE_READY(CurrentType);
6555 PYTYPE_READY(DictionaryType);
6556 PYTYPE_READY(ListType);
6557 PYTYPE_READY(FunctionType);
6558 PYTYPE_READY(OptionsType);
6559 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006560 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006561 return 0;
6562}
6563
6564 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006565init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006566{
6567 PyObject *path;
6568 PyObject *path_hook;
6569 PyObject *path_hooks;
6570
6571 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6572 return -1;
6573
6574 if (!(path_hooks = PySys_GetObject("path_hooks")))
6575 {
6576 PyErr_Clear();
6577 path_hooks = PyList_New(1);
6578 PyList_SET_ITEM(path_hooks, 0, path_hook);
6579 if (PySys_SetObject("path_hooks", path_hooks))
6580 {
6581 Py_DECREF(path_hooks);
6582 return -1;
6583 }
6584 Py_DECREF(path_hooks);
6585 }
6586 else if (PyList_Check(path_hooks))
6587 {
6588 if (PyList_Append(path_hooks, path_hook))
6589 {
6590 Py_DECREF(path_hook);
6591 return -1;
6592 }
6593 Py_DECREF(path_hook);
6594 }
6595 else
6596 {
6597 VimTryStart();
6598 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6599 "You should now do the following:\n"
6600 "- append vim.path_hook to sys.path_hooks\n"
6601 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6602 VimTryEnd(); /* Discard the error */
6603 Py_DECREF(path_hook);
6604 return 0;
6605 }
6606
6607 if (!(path = PySys_GetObject("path")))
6608 {
6609 PyErr_Clear();
6610 path = PyList_New(1);
6611 Py_INCREF(vim_special_path_object);
6612 PyList_SET_ITEM(path, 0, vim_special_path_object);
6613 if (PySys_SetObject("path", path))
6614 {
6615 Py_DECREF(path);
6616 return -1;
6617 }
6618 Py_DECREF(path);
6619 }
6620 else if (PyList_Check(path))
6621 {
6622 if (PyList_Append(path, vim_special_path_object))
6623 return -1;
6624 }
6625 else
6626 {
6627 VimTryStart();
6628 EMSG(_("Failed to set path: sys.path is not a list\n"
6629 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6630 VimTryEnd(); /* Discard the error */
6631 }
6632
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006633 return 0;
6634}
6635
6636static BufMapObject TheBufferMap =
6637{
6638 PyObject_HEAD_INIT(&BufMapType)
6639};
6640
6641static WinListObject TheWindowList =
6642{
6643 PyObject_HEAD_INIT(&WinListType)
6644 NULL
6645};
6646
6647static CurrentObject TheCurrent =
6648{
6649 PyObject_HEAD_INIT(&CurrentType)
6650};
6651
6652static TabListObject TheTabPageList =
6653{
6654 PyObject_HEAD_INIT(&TabListType)
6655};
6656
6657static struct numeric_constant {
6658 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006659 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006660} numeric_constants[] = {
6661 {"VAR_LOCKED", VAR_LOCKED},
6662 {"VAR_FIXED", VAR_FIXED},
6663 {"VAR_SCOPE", VAR_SCOPE},
6664 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6665};
6666
6667static struct object_constant {
6668 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006669 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006670} object_constants[] = {
6671 {"buffers", (PyObject *)(void *)&TheBufferMap},
6672 {"windows", (PyObject *)(void *)&TheWindowList},
6673 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6674 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006675
6676 {"Buffer", (PyObject *)&BufferType},
6677 {"Range", (PyObject *)&RangeType},
6678 {"Window", (PyObject *)&WindowType},
6679 {"TabPage", (PyObject *)&TabPageType},
6680 {"Dictionary", (PyObject *)&DictionaryType},
6681 {"List", (PyObject *)&ListType},
6682 {"Function", (PyObject *)&FunctionType},
6683 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006684 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006685};
6686
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006687#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006688 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006689 return -1;
6690
6691#define ADD_CHECKED_OBJECT(m, name, obj) \
6692 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006693 PyObject *valObject = obj; \
6694 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006695 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006696 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006697 }
6698
6699 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006700populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006701{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006702 int i;
6703 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006704 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006705 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006706
6707 for (i = 0; i < (int)(sizeof(numeric_constants)
6708 / sizeof(struct numeric_constant));
6709 ++i)
6710 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006711 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006712
6713 for (i = 0; i < (int)(sizeof(object_constants)
6714 / sizeof(struct object_constant));
6715 ++i)
6716 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006717 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006718
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006719 valObject = object_constants[i].valObject;
6720 Py_INCREF(valObject);
6721 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006722 }
6723
6724 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6725 return -1;
6726 ADD_OBJECT(m, "error", VimError);
6727
Bram Moolenaara9922d62013-05-30 13:01:18 +02006728 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6729 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006730 ADD_CHECKED_OBJECT(m, "options",
6731 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006732
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006733 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006734 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006735 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006736
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006737 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006738 return -1;
6739 ADD_OBJECT(m, "_getcwd", py_getcwd)
6740
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006741 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006742 return -1;
6743 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006744 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006745 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006746 if (PyObject_SetAttrString(other_module, "chdir", attr))
6747 {
6748 Py_DECREF(attr);
6749 return -1;
6750 }
6751 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006752
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006753 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006754 {
6755 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006756 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006757 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006758 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6759 {
6760 Py_DECREF(attr);
6761 return -1;
6762 }
6763 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006764 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006765 else
6766 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006767
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006768 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6769 return -1;
6770
6771 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6772
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006773 if (!(imp = PyImport_ImportModule("imp")))
6774 return -1;
6775
6776 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6777 {
6778 Py_DECREF(imp);
6779 return -1;
6780 }
6781
6782 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6783 {
6784 Py_DECREF(py_find_module);
6785 Py_DECREF(imp);
6786 return -1;
6787 }
6788
6789 Py_DECREF(imp);
6790
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006791 ADD_OBJECT(m, "_find_module", py_find_module);
6792 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006793
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006794 return 0;
6795}