blob: a46b42adda25b60d4ea01089c0ecec0029c22b66 [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 Moolenaarcabf80f2013-05-17 16:18:33 +020075static PyObject *WindowNew(win_T *, tabpage_T *);
76static PyObject *BufferNew (buf_T *);
77static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020078
79static PyInt RangeStart;
80static PyInt RangeEnd;
81
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020082static PyObject *globals;
83
Bram Moolenaarf4258302013-06-02 18:20:17 +020084static PyObject *py_chdir;
85static PyObject *py_fchdir;
86static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020087static PyObject *vim_module;
88static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020089
Bram Moolenaar81c40c52013-06-12 14:41:04 +020090static PyObject *py_find_module;
91static PyObject *py_load_module;
92
93static PyObject *VimError;
94
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020095/*
96 * obtain a lock on the Vim data structures
97 */
98 static void
99Python_Lock_Vim(void)
100{
101}
102
103/*
104 * release a lock on the Vim data structures
105 */
106 static void
107Python_Release_Vim(void)
108{
109}
110
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200111/*
112 * The "todecref" argument holds a pointer to PyObject * that must be
113 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
114 * was needed to generate returned value is object.
115 *
116 * Use Py_XDECREF to decrement reference count.
117 */
118 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200119StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200120{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200121 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200122
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200123 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200124 {
125
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200126 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
127 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200128 return NULL;
129
130 *todecref = NULL;
131 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200132 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200133 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200134 PyObject *bytes;
135
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200136 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200137 return NULL;
138
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200139 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
140 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200141 {
142 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200143 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200144 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200145
146 *todecref = bytes;
147 }
148 else
149 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200150#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200151 PyErr_FORMAT(PyExc_TypeError,
152 N_("expected str() or unicode() instance, but got %s"),
153 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200154#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200155 PyErr_FORMAT(PyExc_TypeError,
156 N_("expected bytes() or str() instance, but got %s"),
157 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200158#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200159 return NULL;
160 }
161
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200162 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200163}
164
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200165#define NUMBER_LONG 1
166#define NUMBER_INT 2
167#define NUMBER_NATURAL 4
168#define NUMBER_UNSIGNED 8
169
170 static int
171NumberToLong(PyObject *obj, long *result, int flags)
172{
173#if PY_MAJOR_VERSION < 3
174 if (PyInt_Check(obj))
175 {
176 *result = PyInt_AsLong(obj);
177 if (PyErr_Occurred())
178 return -1;
179 }
180 else
181#endif
182 if (PyLong_Check(obj))
183 {
184 *result = PyLong_AsLong(obj);
185 if (PyErr_Occurred())
186 return -1;
187 }
188 else if (PyNumber_Check(obj))
189 {
190 PyObject *num;
191
192 if (!(num = PyNumber_Long(obj)))
193 return -1;
194
195 *result = PyLong_AsLong(num);
196
197 Py_DECREF(num);
198
199 if (PyErr_Occurred())
200 return -1;
201 }
202 else
203 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200204#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200205 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200206 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200207 "coercing to long(), but got %s"),
208 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200209#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200210 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200211 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200212 "but got %s"),
213 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200214#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200215 return -1;
216 }
217
218 if (flags & NUMBER_INT)
219 {
220 if (*result > INT_MAX)
221 {
222 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200223 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200224 return -1;
225 }
226 else if (*result < INT_MIN)
227 {
228 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200229 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200230 return -1;
231 }
232 }
233
234 if (flags & NUMBER_NATURAL)
235 {
236 if (*result <= 0)
237 {
238 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100239 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200240 return -1;
241 }
242 }
243 else if (flags & NUMBER_UNSIGNED)
244 {
245 if (*result < 0)
246 {
247 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200248 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200249 return -1;
250 }
251 }
252
253 return 0;
254}
255
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200256 static int
257add_string(PyObject *list, char *s)
258{
259 PyObject *string;
260
261 if (!(string = PyString_FromString(s)))
262 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200263
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200264 if (PyList_Append(list, string))
265 {
266 Py_DECREF(string);
267 return -1;
268 }
269
270 Py_DECREF(string);
271 return 0;
272}
273
274 static PyObject *
275ObjectDir(PyObject *self, char **attributes)
276{
277 PyMethodDef *method;
278 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200279 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200280
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200281 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200282 return NULL;
283
284 if (self)
285 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200286 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200287 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200288 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200289 return NULL;
290 }
291
292 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200293 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200294 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200295 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200296 return NULL;
297 }
298
299#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200300 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200301 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200302 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200303 return NULL;
304 }
305#endif
306
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200307 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200308}
309
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200310/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200311 */
312
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200313/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200314typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200315
316static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200317
318typedef struct
319{
320 PyObject_HEAD
321 long softspace;
322 long error;
323} OutputObject;
324
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200325static char *OutputAttrs[] = {
326 "softspace",
327 NULL
328};
329
330 static PyObject *
331OutputDir(PyObject *self)
332{
333 return ObjectDir(self, OutputAttrs);
334}
335
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200337OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200339 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200340 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200341 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200342 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200343 return -1;
344 }
345
346 if (strcmp(name, "softspace") == 0)
347 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200348 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200349 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return 0;
351 }
352
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200353 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200354 return -1;
355}
356
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200357/* Buffer IO, we write one whole line at a time. */
358static garray_T io_ga = {0, 0, 1, 80, NULL};
359static writefn old_fn = NULL;
360
361 static void
362PythonIO_Flush(void)
363{
364 if (old_fn != NULL && io_ga.ga_len > 0)
365 {
366 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
367 old_fn((char_u *)io_ga.ga_data);
368 }
369 io_ga.ga_len = 0;
370}
371
372 static void
373writer(writefn fn, char_u *str, PyInt n)
374{
375 char_u *ptr;
376
377 /* Flush when switching output function. */
378 if (fn != old_fn)
379 PythonIO_Flush();
380 old_fn = fn;
381
382 /* Write each NL separated line. Text after the last NL is kept for
383 * writing later. */
384 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
385 {
386 PyInt len = ptr - str;
387
388 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
389 break;
390
391 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
392 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
393 fn((char_u *)io_ga.ga_data);
394 str = ptr + 1;
395 n -= len + 1;
396 io_ga.ga_len = 0;
397 }
398
399 /* Put the remaining text into io_ga for later printing. */
400 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
401 {
402 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
403 io_ga.ga_len += (int)n;
404 }
405}
406
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200407 static int
408write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200409{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200410 Py_ssize_t len = 0;
411 char *str = NULL;
412 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200413
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200414 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
415 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200416
417 Py_BEGIN_ALLOW_THREADS
418 Python_Lock_Vim();
419 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
420 Python_Release_Vim();
421 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200422 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200423
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200424 return 0;
425}
426
427 static PyObject *
428OutputWrite(OutputObject *self, PyObject *string)
429{
430 if (write_output(self, string))
431 return NULL;
432
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200433 Py_INCREF(Py_None);
434 return Py_None;
435}
436
437 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200438OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200439{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200440 PyObject *iterator;
441 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200443 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200446 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200448 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200449 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200450 Py_DECREF(iterator);
451 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200452 return NULL;
453 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200454 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 }
456
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200457 Py_DECREF(iterator);
458
459 /* Iterator may have finished due to an exception */
460 if (PyErr_Occurred())
461 return NULL;
462
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200463 Py_INCREF(Py_None);
464 return Py_None;
465}
466
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100467 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200468OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100469{
470 /* do nothing */
471 Py_INCREF(Py_None);
472 return Py_None;
473}
474
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200475/***************/
476
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200477static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200478 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200479 {"write", (PyCFunction)OutputWrite, METH_O, ""},
480 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200481 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200482 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200483 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200484};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200485
486static OutputObject Output =
487{
488 PyObject_HEAD_INIT(&OutputType)
489 0,
490 0
491};
492
493static OutputObject Error =
494{
495 PyObject_HEAD_INIT(&OutputType)
496 0,
497 1
498};
499
500 static int
501PythonIO_Init_io(void)
502{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200503 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
504 return -1;
505 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
506 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200507
508 if (PyErr_Occurred())
509 {
510 EMSG(_("E264: Python: Error initialising I/O objects"));
511 return -1;
512 }
513
514 return 0;
515}
516
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200517typedef struct
518{
519 PyObject_HEAD
520 PyObject *module;
521} LoaderObject;
522static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200523
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200524 static void
525LoaderDestructor(LoaderObject *self)
526{
527 Py_DECREF(self->module);
528 DESTRUCTOR_FINISH(self);
529}
530
531 static PyObject *
532LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
533{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200534 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200535
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200536 Py_INCREF(ret);
537 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200538}
539
540static struct PyMethodDef LoaderMethods[] = {
541 /* name, function, calling, doc */
542 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
543 { NULL, NULL, 0, NULL}
544};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200545
546/* Check to see whether a Vim error has been reported, or a keyboard
547 * interrupt has been detected.
548 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200549
550 static void
551VimTryStart(void)
552{
553 ++trylevel;
554}
555
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200556 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200557VimTryEnd(void)
558{
559 --trylevel;
Bram Moolenaar41009372013-07-01 22:03:04 +0200560 /* Without this it stops processing all subsequent VimL commands and
561 * generates strange error messages if I e.g. try calling Test() in a
562 * cycle */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200563 did_emsg = FALSE;
564 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200565 if (got_int)
566 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100567 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100568 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100569 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200570 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200571 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200572 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100573 else if (msg_list != NULL && *msg_list != NULL)
574 {
575 int should_free;
576 char_u *msg;
577
578 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
579
580 if (msg == NULL)
581 {
582 PyErr_NoMemory();
583 return -1;
584 }
585
586 PyErr_SetVim((char *) msg);
587
588 free_global_msglist();
589
590 if (should_free)
591 vim_free(msg);
592
593 return -1;
594 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200595 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200596 return (PyErr_Occurred() ? -1 : 0);
597 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200598 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200599 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100600 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200601 return -1;
602 }
603 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200604 else
605 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200606 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200607 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200608 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200609 }
610}
611
612 static int
613VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200614{
615 if (got_int)
616 {
617 PyErr_SetNone(PyExc_KeyboardInterrupt);
618 return 1;
619 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200620 return 0;
621}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200622
623/* Vim module - Implementation
624 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200625
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200626 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200627VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200628{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200629 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200630 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200631 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200632
Bram Moolenaar389a1792013-06-23 13:00:44 +0200633 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200634 return NULL;
635
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200636 Py_BEGIN_ALLOW_THREADS
637 Python_Lock_Vim();
638
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200639 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200640 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200641 update_screen(VALID);
642
643 Python_Release_Vim();
644 Py_END_ALLOW_THREADS
645
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200646 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200647 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200648 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200649 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200650
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200651 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200652 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200653 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200654}
655
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200656/*
657 * Function to translate a typval_T into a PyObject; this will recursively
658 * translate lists/dictionaries into their Python equivalents.
659 *
660 * The depth parameter is to avoid infinite recursion, set it to 1 when
661 * you call VimToPython.
662 */
663 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200664VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200665{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200666 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200667 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200668 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200669
670 /* Avoid infinite recursion */
671 if (depth > 100)
672 {
673 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200674 ret = Py_None;
675 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200676 }
677
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200678 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200679 * then and we can use it again. */
680 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
681 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
682 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200683 sprintf(ptrBuf, "%p",
684 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
685 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200686
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200687 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200688 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200689 Py_INCREF(ret);
690 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200691 }
692 }
693
694 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200695 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200696 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200697 else if (our_tv->v_type == VAR_NUMBER)
698 {
699 char buf[NUMBUFLEN];
700
701 /* For backwards compatibility numbers are stored as strings. */
702 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200703 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200704 }
705# ifdef FEAT_FLOAT
706 else if (our_tv->v_type == VAR_FLOAT)
707 {
708 char buf[NUMBUFLEN];
709
710 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200711 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200712 }
713# endif
714 else if (our_tv->v_type == VAR_LIST)
715 {
716 list_T *list = our_tv->vval.v_list;
717 listitem_T *curr;
718
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200719 if (list == NULL)
720 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200721
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200722 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200723 return NULL;
724
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200725 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200728 return NULL;
729 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200731 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
732 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200733 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200734 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200735 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200736 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200737 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200738 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200739 {
740 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200741 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200742 return NULL;
743 }
744 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200745 }
746 }
747 else if (our_tv->v_type == VAR_DICT)
748 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200749
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200750 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
751 long_u todo = ht->ht_used;
752 hashitem_T *hi;
753 dictitem_T *di;
754 if (our_tv->vval.v_dict == NULL)
755 return NULL;
756
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200757 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200758 return NULL;
759
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200760 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200762 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200763 return NULL;
764 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200766 for (hi = ht->ht_array; todo > 0; ++hi)
767 {
768 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200769 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200770 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200772 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200773 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200774 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200775 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200776 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200777 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200778 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200779 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200780 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200781 Py_DECREF(newObj);
782 return NULL;
783 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200784 }
785 }
786 }
787 else
788 {
789 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200790 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791 }
792
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200793 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795
796 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200797VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200799 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200800 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200801 PyObject *string;
802 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200803 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200804 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805
Bram Moolenaar389a1792013-06-23 13:00:44 +0200806 if (!PyArg_ParseTuple(args, "O", &string))
807 return NULL;
808
809 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810 return NULL;
811
812 Py_BEGIN_ALLOW_THREADS
813 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200814 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200815 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200816 Python_Release_Vim();
817 Py_END_ALLOW_THREADS
818
Bram Moolenaar389a1792013-06-23 13:00:44 +0200819 Py_XDECREF(todecref);
820
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200821 if (VimTryEnd())
822 return NULL;
823
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200824 if (our_tv == NULL)
825 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200826 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200827 return NULL;
828 }
829
830 /* Convert the Vim type into a Python type. Create a dictionary that's
831 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200832 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200833 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200834 else
835 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200836 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200837 Py_DECREF(lookup_dict);
838 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200839
840
841 Py_BEGIN_ALLOW_THREADS
842 Python_Lock_Vim();
843 free_tv(our_tv);
844 Python_Release_Vim();
845 Py_END_ALLOW_THREADS
846
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200847 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200848}
849
Bram Moolenaardb913952012-06-29 12:54:53 +0200850static PyObject *ConvertToPyObject(typval_T *);
851
852 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200853VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200854{
Bram Moolenaardb913952012-06-29 12:54:53 +0200855 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200856 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200857 char_u *expr;
858 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200859
Bram Moolenaar389a1792013-06-23 13:00:44 +0200860 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200861 return NULL;
862
863 Py_BEGIN_ALLOW_THREADS
864 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200865 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200866 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200867 Python_Release_Vim();
868 Py_END_ALLOW_THREADS
869
Bram Moolenaar389a1792013-06-23 13:00:44 +0200870 Py_XDECREF(todecref);
871
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200872 if (VimTryEnd())
873 return NULL;
874
Bram Moolenaardb913952012-06-29 12:54:53 +0200875 if (our_tv == NULL)
876 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200877 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200878 return NULL;
879 }
880
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200881 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200882 Py_BEGIN_ALLOW_THREADS
883 Python_Lock_Vim();
884 free_tv(our_tv);
885 Python_Release_Vim();
886 Py_END_ALLOW_THREADS
887
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200888 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200889}
890
891 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200892VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200893{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200894 char_u *str;
895 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200896 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200897
Bram Moolenaar389a1792013-06-23 13:00:44 +0200898 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200899 return NULL;
900
Bram Moolenaara54bf402012-12-05 16:30:07 +0100901#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200902 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100903#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200904 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100905#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200906
907 Py_XDECREF(todecref);
908
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200909 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200910}
911
Bram Moolenaarf4258302013-06-02 18:20:17 +0200912 static PyObject *
913_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
914{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200915 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200916 PyObject *newwd;
917 PyObject *todecref;
918 char_u *new_dir;
919
Bram Moolenaard4209d22013-06-05 20:34:15 +0200920 if (_chdir == NULL)
921 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200922 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200923 return NULL;
924
925 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
926 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200927 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200928 return NULL;
929 }
930
931 if (!(new_dir = StringToChars(newwd, &todecref)))
932 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200933 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200934 Py_DECREF(newwd);
935 return NULL;
936 }
937
938 VimTryStart();
939
940 if (vim_chdir(new_dir))
941 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200942 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200943 Py_DECREF(newwd);
944 Py_XDECREF(todecref);
945
946 if (VimTryEnd())
947 return NULL;
948
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200949 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200950 return NULL;
951 }
952
953 Py_DECREF(newwd);
954 Py_XDECREF(todecref);
955
956 post_chdir(FALSE);
957
958 if (VimTryEnd())
959 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200960 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200961 return NULL;
962 }
963
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200964 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200965}
966
967 static PyObject *
968VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
969{
970 return _VimChdir(py_chdir, args, kwargs);
971}
972
973 static PyObject *
974VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
975{
976 return _VimChdir(py_fchdir, args, kwargs);
977}
978
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200979typedef struct {
980 PyObject *callable;
981 PyObject *result;
982} map_rtp_data;
983
984 static void
985map_rtp_callback(char_u *path, void *_data)
986{
987 void **data = (void **) _data;
988 PyObject *pathObject;
989 map_rtp_data *mr_data = *((map_rtp_data **) data);
990
Bram Moolenaar41009372013-07-01 22:03:04 +0200991 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200992 {
993 *data = NULL;
994 return;
995 }
996
997 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
998 pathObject, NULL);
999
1000 Py_DECREF(pathObject);
1001
1002 if (!mr_data->result || mr_data->result != Py_None)
1003 *data = NULL;
1004 else
1005 {
1006 Py_DECREF(mr_data->result);
1007 mr_data->result = NULL;
1008 }
1009}
1010
1011 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001012VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001013{
1014 map_rtp_data data;
1015
Bram Moolenaar389a1792013-06-23 13:00:44 +02001016 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001017 data.result = NULL;
1018
1019 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
1020
1021 if (data.result == NULL)
1022 {
1023 if (PyErr_Occurred())
1024 return NULL;
1025 else
1026 {
1027 Py_INCREF(Py_None);
1028 return Py_None;
1029 }
1030 }
1031 return data.result;
1032}
1033
1034/*
1035 * _vim_runtimepath_ special path implementation.
1036 */
1037
1038 static void
1039map_finder_callback(char_u *path, void *_data)
1040{
1041 void **data = (void **) _data;
1042 PyObject *list = *((PyObject **) data);
1043 PyObject *pathObject1, *pathObject2;
1044 char *pathbuf;
1045 size_t pathlen;
1046
1047 pathlen = STRLEN(path);
1048
1049#if PY_MAJOR_VERSION < 3
1050# define PY_MAIN_DIR_STRING "python2"
1051#else
1052# define PY_MAIN_DIR_STRING "python3"
1053#endif
1054#define PY_ALTERNATE_DIR_STRING "pythonx"
1055
1056#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1057 if (!(pathbuf = PyMem_New(char,
1058 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1059 {
1060 PyErr_NoMemory();
1061 *data = NULL;
1062 return;
1063 }
1064
1065 mch_memmove(pathbuf, path, pathlen + 1);
1066 add_pathsep((char_u *) pathbuf);
1067
1068 pathlen = STRLEN(pathbuf);
1069 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1070 PYTHONX_STRING_LENGTH + 1);
1071
1072 if (!(pathObject1 = PyString_FromString(pathbuf)))
1073 {
1074 *data = NULL;
1075 PyMem_Free(pathbuf);
1076 return;
1077 }
1078
1079 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1080 PYTHONX_STRING_LENGTH + 1);
1081
1082 if (!(pathObject2 = PyString_FromString(pathbuf)))
1083 {
1084 Py_DECREF(pathObject1);
1085 PyMem_Free(pathbuf);
1086 *data = NULL;
1087 return;
1088 }
1089
1090 PyMem_Free(pathbuf);
1091
1092 if (PyList_Append(list, pathObject1)
1093 || PyList_Append(list, pathObject2))
1094 *data = NULL;
1095
1096 Py_DECREF(pathObject1);
1097 Py_DECREF(pathObject2);
1098}
1099
1100 static PyObject *
1101Vim_GetPaths(PyObject *self UNUSED)
1102{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001103 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001104
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001105 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001106 return NULL;
1107
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001108 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001109
1110 if (PyErr_Occurred())
1111 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001112 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001113 return NULL;
1114 }
1115
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001116 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001117}
1118
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001119 static PyObject *
1120call_load_module(char *name, int len, PyObject *find_module_result)
1121{
1122 PyObject *fd, *pathname, *description;
1123
Bram Moolenaarc476e522013-06-23 13:46:40 +02001124 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001125 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001126 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001127 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001128 Py_TYPE_NAME(find_module_result));
1129 return NULL;
1130 }
1131 if (PyTuple_GET_SIZE(find_module_result) != 3)
1132 {
1133 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001134 N_("expected 3-tuple as imp.find_module() result, but got "
1135 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001136 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001137 return NULL;
1138 }
1139
1140 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1141 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1142 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1143 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001144 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001145 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001146 return NULL;
1147 }
1148
1149 return PyObject_CallFunction(py_load_module,
1150 "s#OOO", name, len, fd, pathname, description);
1151}
1152
1153 static PyObject *
1154find_module(char *fullname, char *tail, PyObject *new_path)
1155{
1156 PyObject *find_module_result;
1157 PyObject *module;
1158 char *dot;
1159
Bram Moolenaar41009372013-07-01 22:03:04 +02001160 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001161 {
1162 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001163 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001164 * first component
1165 */
1166 PyObject *newest_path;
1167 int partlen = (int) (dot - 1 - tail);
1168
1169 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1170 "s#O", tail, partlen, new_path)))
1171 return NULL;
1172
1173 if (!(module = call_load_module(
1174 fullname,
1175 ((int) (tail - fullname)) + partlen,
1176 find_module_result)))
1177 {
1178 Py_DECREF(find_module_result);
1179 return NULL;
1180 }
1181
1182 Py_DECREF(find_module_result);
1183
1184 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1185 {
1186 Py_DECREF(module);
1187 return NULL;
1188 }
1189
1190 Py_DECREF(module);
1191
1192 module = find_module(fullname, dot + 1, newest_path);
1193
1194 Py_DECREF(newest_path);
1195
1196 return module;
1197 }
1198 else
1199 {
1200 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1201 "sO", tail, new_path)))
1202 return NULL;
1203
1204 if (!(module = call_load_module(
1205 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001206 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001207 find_module_result)))
1208 {
1209 Py_DECREF(find_module_result);
1210 return NULL;
1211 }
1212
1213 Py_DECREF(find_module_result);
1214
1215 return module;
1216 }
1217}
1218
1219 static PyObject *
1220FinderFindModule(PyObject *self, PyObject *args)
1221{
1222 char *fullname;
1223 PyObject *module;
1224 PyObject *new_path;
1225 LoaderObject *loader;
1226
1227 if (!PyArg_ParseTuple(args, "s", &fullname))
1228 return NULL;
1229
1230 if (!(new_path = Vim_GetPaths(self)))
1231 return NULL;
1232
1233 module = find_module(fullname, fullname, new_path);
1234
1235 Py_DECREF(new_path);
1236
1237 if (!module)
1238 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001239 if (PyErr_Occurred())
1240 {
1241 if (PyErr_ExceptionMatches(PyExc_ImportError))
1242 PyErr_Clear();
1243 else
1244 return NULL;
1245 }
1246
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001247 Py_INCREF(Py_None);
1248 return Py_None;
1249 }
1250
1251 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1252 {
1253 Py_DECREF(module);
1254 return NULL;
1255 }
1256
1257 loader->module = module;
1258
1259 return (PyObject *) loader;
1260}
1261
1262 static PyObject *
1263VimPathHook(PyObject *self UNUSED, PyObject *args)
1264{
1265 char *path;
1266
1267 if (PyArg_ParseTuple(args, "s", &path)
1268 && STRCMP(path, vim_special_path) == 0)
1269 {
1270 Py_INCREF(vim_module);
1271 return vim_module;
1272 }
1273
1274 PyErr_Clear();
1275 PyErr_SetNone(PyExc_ImportError);
1276 return NULL;
1277}
1278
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001279/*
1280 * Vim module - Definitions
1281 */
1282
1283static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001284 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001285 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001286 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001287 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1288 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001289 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1290 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001291 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001292 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001293 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1294 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1295 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001296};
1297
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001298/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001299 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001300 */
1301
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001302static PyTypeObject IterType;
1303
1304typedef PyObject *(*nextfun)(void **);
1305typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001306typedef int (*traversefun)(void *, visitproc, void *);
1307typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001308
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001309/* Main purpose of this object is removing the need for do python
1310 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1311 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001312
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001313typedef struct
1314{
1315 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001316 void *cur;
1317 nextfun next;
1318 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001319 traversefun traverse;
1320 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001321} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001322
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001323 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001324IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1325 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001326{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001327 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001328
Bram Moolenaar774267b2013-05-21 20:51:59 +02001329 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001330 self->cur = start;
1331 self->next = next;
1332 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001333 self->traverse = traverse;
1334 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001335
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001336 return (PyObject *)(self);
1337}
1338
1339 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001340IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001341{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001342 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001343 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001344 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001345}
1346
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001347 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001348IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001349{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001350 if (self->traverse != NULL)
1351 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001352 else
1353 return 0;
1354}
1355
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001356/* Mac OSX defines clear() somewhere. */
1357#ifdef clear
1358# undef clear
1359#endif
1360
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001361 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001362IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001363{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001364 if (self->clear != NULL)
1365 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001366 else
1367 return 0;
1368}
1369
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001370 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001371IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001372{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001373 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001374}
1375
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001376 static PyObject *
1377IterIter(PyObject *self)
1378{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001379 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001380 return self;
1381}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001382
Bram Moolenaardb913952012-06-29 12:54:53 +02001383typedef struct pylinkedlist_S {
1384 struct pylinkedlist_S *pll_next;
1385 struct pylinkedlist_S *pll_prev;
1386 PyObject *pll_obj;
1387} pylinkedlist_T;
1388
1389static pylinkedlist_T *lastdict = NULL;
1390static pylinkedlist_T *lastlist = NULL;
1391
1392 static void
1393pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1394{
1395 if (ref->pll_prev == NULL)
1396 {
1397 if (ref->pll_next == NULL)
1398 {
1399 *last = NULL;
1400 return;
1401 }
1402 }
1403 else
1404 ref->pll_prev->pll_next = ref->pll_next;
1405
1406 if (ref->pll_next == NULL)
1407 *last = ref->pll_prev;
1408 else
1409 ref->pll_next->pll_prev = ref->pll_prev;
1410}
1411
1412 static void
1413pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1414{
1415 if (*last == NULL)
1416 ref->pll_prev = NULL;
1417 else
1418 {
1419 (*last)->pll_next = ref;
1420 ref->pll_prev = *last;
1421 }
1422 ref->pll_next = NULL;
1423 ref->pll_obj = self;
1424 *last = ref;
1425}
1426
1427static PyTypeObject DictionaryType;
1428
1429typedef struct
1430{
1431 PyObject_HEAD
1432 dict_T *dict;
1433 pylinkedlist_T ref;
1434} DictionaryObject;
1435
Bram Moolenaara9922d62013-05-30 13:01:18 +02001436static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1437
1438#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1439
Bram Moolenaardb913952012-06-29 12:54:53 +02001440 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001441DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001442{
1443 DictionaryObject *self;
1444
Bram Moolenaara9922d62013-05-30 13:01:18 +02001445 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001446 if (self == NULL)
1447 return NULL;
1448 self->dict = dict;
1449 ++dict->dv_refcount;
1450
1451 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1452
1453 return (PyObject *)(self);
1454}
1455
Bram Moolenaara9922d62013-05-30 13:01:18 +02001456 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001457py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001458{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001459 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001460
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001461 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001462 {
1463 PyErr_NoMemory();
1464 return NULL;
1465 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001466 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001467
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001468 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001469}
1470
1471 static PyObject *
1472DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1473{
1474 DictionaryObject *self;
1475 dict_T *dict;
1476
1477 if (!(dict = py_dict_alloc()))
1478 return NULL;
1479
1480 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1481
1482 --dict->dv_refcount;
1483
1484 if (kwargs || PyTuple_Size(args))
1485 {
1486 PyObject *tmp;
1487 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1488 {
1489 Py_DECREF(self);
1490 return NULL;
1491 }
1492
1493 Py_DECREF(tmp);
1494 }
1495
1496 return (PyObject *)(self);
1497}
1498
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001499 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001500DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001501{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001502 pyll_remove(&self->ref, &lastdict);
1503 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001504
1505 DESTRUCTOR_FINISH(self);
1506}
1507
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001508static char *DictionaryAttrs[] = {
1509 "locked", "scope",
1510 NULL
1511};
1512
1513 static PyObject *
1514DictionaryDir(PyObject *self)
1515{
1516 return ObjectDir(self, DictionaryAttrs);
1517}
1518
Bram Moolenaardb913952012-06-29 12:54:53 +02001519 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001520DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001521{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001522 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001523 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001524 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001525 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001526 return -1;
1527 }
1528
1529 if (strcmp(name, "locked") == 0)
1530 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001531 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001532 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001533 PyErr_SET_STRING(PyExc_TypeError,
1534 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001535 return -1;
1536 }
1537 else
1538 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001539 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001540 if (istrue == -1)
1541 return -1;
1542 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001543 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001544 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001545 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001546 }
1547 return 0;
1548 }
1549 else
1550 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001551 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001552 return -1;
1553 }
1554}
1555
1556 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001557DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001558{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001559 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001560}
1561
Bram Moolenaara9922d62013-05-30 13:01:18 +02001562#define DICT_FLAG_HAS_DEFAULT 0x01
1563#define DICT_FLAG_POP 0x02
1564#define DICT_FLAG_NONE_DEFAULT 0x04
1565#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1566#define DICT_FLAG_RETURN_PAIR 0x10
1567
Bram Moolenaardb913952012-06-29 12:54:53 +02001568 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001569_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001570{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001571 PyObject *keyObject;
1572 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001573 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001574 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001575 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001576 dict_T *dict = self->dict;
1577 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001578 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001579
Bram Moolenaara9922d62013-05-30 13:01:18 +02001580 if (flags & DICT_FLAG_HAS_DEFAULT)
1581 {
1582 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1583 return NULL;
1584 }
1585 else
1586 keyObject = args;
1587
1588 if (flags & DICT_FLAG_RETURN_BOOL)
1589 defObject = Py_False;
1590
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001591 if (!(key = StringToChars(keyObject, &todecref)))
1592 return NULL;
1593
1594 if (*key == NUL)
1595 {
1596 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001597 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001598 return NULL;
1599 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001600
Bram Moolenaara9922d62013-05-30 13:01:18 +02001601 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001602
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001603 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001604
Bram Moolenaara9922d62013-05-30 13:01:18 +02001605 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001606 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001607 if (defObject)
1608 {
1609 Py_INCREF(defObject);
1610 return defObject;
1611 }
1612 else
1613 {
1614 PyErr_SetObject(PyExc_KeyError, keyObject);
1615 return NULL;
1616 }
1617 }
1618 else if (flags & DICT_FLAG_RETURN_BOOL)
1619 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001620 ret = Py_True;
1621 Py_INCREF(ret);
1622 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001623 }
1624
1625 di = dict_lookup(hi);
1626
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001627 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001628 return NULL;
1629
1630 if (flags & DICT_FLAG_POP)
1631 {
1632 if (dict->dv_lock)
1633 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001634 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001635 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001636 return NULL;
1637 }
1638
1639 hash_remove(&dict->dv_hashtab, hi);
1640 dictitem_free(di);
1641 }
1642
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001643 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001644}
1645
1646 static PyObject *
1647DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1648{
1649 return _DictionaryItem(self, keyObject, 0);
1650}
1651
1652 static int
1653DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1654{
1655 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001656 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001657
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001658 if (rObj == NULL)
1659 return -1;
1660
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001661 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001662
Bram Moolenaardee2e312013-06-23 16:35:47 +02001663 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001664
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001665 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001666}
1667
1668typedef struct
1669{
1670 hashitem_T *ht_array;
1671 long_u ht_used;
1672 hashtab_T *ht;
1673 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001674 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001675} dictiterinfo_T;
1676
1677 static PyObject *
1678DictionaryIterNext(dictiterinfo_T **dii)
1679{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001680 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001681
1682 if (!(*dii)->todo)
1683 return NULL;
1684
1685 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1686 (*dii)->ht->ht_used != (*dii)->ht_used)
1687 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001688 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001689 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001690 return NULL;
1691 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001692
Bram Moolenaara9922d62013-05-30 13:01:18 +02001693 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1694 ++((*dii)->hi);
1695
1696 --((*dii)->todo);
1697
Bram Moolenaar41009372013-07-01 22:03:04 +02001698 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001699 return NULL;
1700
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001701 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001702}
1703
1704 static PyObject *
1705DictionaryIter(DictionaryObject *self)
1706{
1707 dictiterinfo_T *dii;
1708 hashtab_T *ht;
1709
1710 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1711 {
1712 PyErr_NoMemory();
1713 return NULL;
1714 }
1715
1716 ht = &self->dict->dv_hashtab;
1717 dii->ht_array = ht->ht_array;
1718 dii->ht_used = ht->ht_used;
1719 dii->ht = ht;
1720 dii->hi = dii->ht_array;
1721 dii->todo = dii->ht_used;
1722
1723 return IterNew(dii,
1724 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1725 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001726}
1727
1728 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001729DictionaryAssItem(
1730 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001731{
1732 char_u *key;
1733 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001734 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001735 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001736 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001737
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001738 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001739 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001740 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001741 return -1;
1742 }
1743
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001744 if (!(key = StringToChars(keyObject, &todecref)))
1745 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001746
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001747 if (*key == NUL)
1748 {
1749 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001750 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001751 return -1;
1752 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001753
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001754 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001755
1756 if (valObject == NULL)
1757 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001758 hashitem_T *hi;
1759
Bram Moolenaardb913952012-06-29 12:54:53 +02001760 if (di == NULL)
1761 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001762 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001763 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001764 return -1;
1765 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001766 hi = hash_find(&dict->dv_hashtab, di->di_key);
1767 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001768 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001769 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001770 return 0;
1771 }
1772
1773 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001774 {
1775 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001776 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001777 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001778
1779 if (di == NULL)
1780 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001781 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001782 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001783 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001784 PyErr_NoMemory();
1785 return -1;
1786 }
1787 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001788 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001789
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001790 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001791 {
1792 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001793 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001794 RAISE_KEY_ADD_FAIL(key);
1795 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001796 return -1;
1797 }
1798 }
1799 else
1800 clear_tv(&di->di_tv);
1801
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001802 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001803
1804 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001805 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001806 return 0;
1807}
1808
Bram Moolenaara9922d62013-05-30 13:01:18 +02001809typedef PyObject *(*hi_to_py)(hashitem_T *);
1810
Bram Moolenaardb913952012-06-29 12:54:53 +02001811 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001812DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001813{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001814 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001815 long_u todo = dict->dv_hashtab.ht_used;
1816 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001817 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001818 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001819 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001820
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001821 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001822 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1823 {
1824 if (!HASHITEM_EMPTY(hi))
1825 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001826 if (!(newObj = hiconvert(hi)))
1827 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001828 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001829 return NULL;
1830 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001831 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001832 --todo;
1833 ++i;
1834 }
1835 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001836 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001837}
1838
Bram Moolenaara9922d62013-05-30 13:01:18 +02001839 static PyObject *
1840dict_key(hashitem_T *hi)
1841{
1842 return PyBytes_FromString((char *)(hi->hi_key));
1843}
1844
1845 static PyObject *
1846DictionaryListKeys(DictionaryObject *self)
1847{
1848 return DictionaryListObjects(self, dict_key);
1849}
1850
1851 static PyObject *
1852dict_val(hashitem_T *hi)
1853{
1854 dictitem_T *di;
1855
1856 di = dict_lookup(hi);
1857 return ConvertToPyObject(&di->di_tv);
1858}
1859
1860 static PyObject *
1861DictionaryListValues(DictionaryObject *self)
1862{
1863 return DictionaryListObjects(self, dict_val);
1864}
1865
1866 static PyObject *
1867dict_item(hashitem_T *hi)
1868{
1869 PyObject *keyObject;
1870 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001871 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001872
1873 if (!(keyObject = dict_key(hi)))
1874 return NULL;
1875
1876 if (!(valObject = dict_val(hi)))
1877 {
1878 Py_DECREF(keyObject);
1879 return NULL;
1880 }
1881
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001882 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001883
1884 Py_DECREF(keyObject);
1885 Py_DECREF(valObject);
1886
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001887 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001888}
1889
1890 static PyObject *
1891DictionaryListItems(DictionaryObject *self)
1892{
1893 return DictionaryListObjects(self, dict_item);
1894}
1895
1896 static PyObject *
1897DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1898{
1899 dict_T *dict = self->dict;
1900
1901 if (dict->dv_lock)
1902 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001903 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001904 return NULL;
1905 }
1906
1907 if (kwargs)
1908 {
1909 typval_T tv;
1910
1911 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1912 return NULL;
1913
1914 VimTryStart();
1915 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1916 clear_tv(&tv);
1917 if (VimTryEnd())
1918 return NULL;
1919 }
1920 else
1921 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001922 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001923
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001924 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001925 return NULL;
1926
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001927 if (obj == NULL)
1928 {
1929 Py_INCREF(Py_None);
1930 return Py_None;
1931 }
1932
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001933 if (PyObject_HasAttrString(obj, "keys"))
1934 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001935 else
1936 {
1937 PyObject *iterator;
1938 PyObject *item;
1939
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001940 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001941 return NULL;
1942
1943 while ((item = PyIter_Next(iterator)))
1944 {
1945 PyObject *fast;
1946 PyObject *keyObject;
1947 PyObject *valObject;
1948 PyObject *todecref;
1949 char_u *key;
1950 dictitem_T *di;
1951
1952 if (!(fast = PySequence_Fast(item, "")))
1953 {
1954 Py_DECREF(iterator);
1955 Py_DECREF(item);
1956 return NULL;
1957 }
1958
1959 Py_DECREF(item);
1960
1961 if (PySequence_Fast_GET_SIZE(fast) != 2)
1962 {
1963 Py_DECREF(iterator);
1964 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001965 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001966 N_("expected sequence element of size 2, "
1967 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001968 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001969 return NULL;
1970 }
1971
1972 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1973
1974 if (!(key = StringToChars(keyObject, &todecref)))
1975 {
1976 Py_DECREF(iterator);
1977 Py_DECREF(fast);
1978 return NULL;
1979 }
1980
1981 di = dictitem_alloc(key);
1982
1983 Py_XDECREF(todecref);
1984
1985 if (di == NULL)
1986 {
1987 Py_DECREF(fast);
1988 Py_DECREF(iterator);
1989 PyErr_NoMemory();
1990 return NULL;
1991 }
1992 di->di_tv.v_lock = 0;
1993 di->di_tv.v_type = VAR_UNKNOWN;
1994
1995 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1996
1997 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1998 {
1999 Py_DECREF(iterator);
2000 Py_DECREF(fast);
2001 dictitem_free(di);
2002 return NULL;
2003 }
2004
2005 Py_DECREF(fast);
2006
2007 if (dict_add(dict, di) == FAIL)
2008 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002009 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002010 Py_DECREF(iterator);
2011 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002012 return NULL;
2013 }
2014 }
2015
2016 Py_DECREF(iterator);
2017
2018 /* Iterator may have finished due to an exception */
2019 if (PyErr_Occurred())
2020 return NULL;
2021 }
2022 }
2023 Py_INCREF(Py_None);
2024 return Py_None;
2025}
2026
2027 static PyObject *
2028DictionaryGet(DictionaryObject *self, PyObject *args)
2029{
2030 return _DictionaryItem(self, args,
2031 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2032}
2033
2034 static PyObject *
2035DictionaryPop(DictionaryObject *self, PyObject *args)
2036{
2037 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2038}
2039
2040 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002041DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002042{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002043 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002044 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002045 PyObject *valObject;
2046 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002047
Bram Moolenaarde71b562013-06-02 17:41:54 +02002048 if (self->dict->dv_hashtab.ht_used == 0)
2049 {
2050 PyErr_SetNone(PyExc_KeyError);
2051 return NULL;
2052 }
2053
2054 hi = self->dict->dv_hashtab.ht_array;
2055 while (HASHITEM_EMPTY(hi))
2056 ++hi;
2057
2058 di = dict_lookup(hi);
2059
2060 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002061 return NULL;
2062
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002063 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002064 {
2065 Py_DECREF(valObject);
2066 return NULL;
2067 }
2068
2069 hash_remove(&self->dict->dv_hashtab, hi);
2070 dictitem_free(di);
2071
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002072 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002073}
2074
2075 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002076DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002077{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002078 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2079}
2080
2081static PySequenceMethods DictionaryAsSeq = {
2082 0, /* sq_length */
2083 0, /* sq_concat */
2084 0, /* sq_repeat */
2085 0, /* sq_item */
2086 0, /* sq_slice */
2087 0, /* sq_ass_item */
2088 0, /* sq_ass_slice */
2089 (objobjproc) DictionaryContains, /* sq_contains */
2090 0, /* sq_inplace_concat */
2091 0, /* sq_inplace_repeat */
2092};
2093
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002094static PyMappingMethods DictionaryAsMapping = {
2095 (lenfunc) DictionaryLength,
2096 (binaryfunc) DictionaryItem,
2097 (objobjargproc) DictionaryAssItem,
2098};
2099
Bram Moolenaardb913952012-06-29 12:54:53 +02002100static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002101 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002102 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2103 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2104 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2105 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2106 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002107 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002108 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002109 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2110 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002111};
2112
2113static PyTypeObject ListType;
2114
2115typedef struct
2116{
2117 PyObject_HEAD
2118 list_T *list;
2119 pylinkedlist_T ref;
2120} ListObject;
2121
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002122#define NEW_LIST(list) ListNew(&ListType, list)
2123
Bram Moolenaardb913952012-06-29 12:54:53 +02002124 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002125ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002126{
2127 ListObject *self;
2128
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002129 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002130 if (self == NULL)
2131 return NULL;
2132 self->list = list;
2133 ++list->lv_refcount;
2134
2135 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2136
2137 return (PyObject *)(self);
2138}
2139
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002140 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002141py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002142{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002143 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002144
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002145 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002146 {
2147 PyErr_NoMemory();
2148 return NULL;
2149 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002150 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002151
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002152 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002153}
2154
2155 static int
2156list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2157{
2158 PyObject *iterator;
2159 PyObject *item;
2160 listitem_T *li;
2161
2162 if (!(iterator = PyObject_GetIter(obj)))
2163 return -1;
2164
2165 while ((item = PyIter_Next(iterator)))
2166 {
2167 if (!(li = listitem_alloc()))
2168 {
2169 PyErr_NoMemory();
2170 Py_DECREF(item);
2171 Py_DECREF(iterator);
2172 return -1;
2173 }
2174 li->li_tv.v_lock = 0;
2175 li->li_tv.v_type = VAR_UNKNOWN;
2176
2177 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2178 {
2179 Py_DECREF(item);
2180 Py_DECREF(iterator);
2181 listitem_free(li);
2182 return -1;
2183 }
2184
2185 Py_DECREF(item);
2186
2187 list_append(l, li);
2188 }
2189
2190 Py_DECREF(iterator);
2191
2192 /* Iterator may have finished due to an exception */
2193 if (PyErr_Occurred())
2194 return -1;
2195
2196 return 0;
2197}
2198
2199 static PyObject *
2200ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2201{
2202 list_T *list;
2203 PyObject *obj = NULL;
2204
2205 if (kwargs)
2206 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002207 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002208 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002209 return NULL;
2210 }
2211
2212 if (!PyArg_ParseTuple(args, "|O", &obj))
2213 return NULL;
2214
2215 if (!(list = py_list_alloc()))
2216 return NULL;
2217
2218 if (obj)
2219 {
2220 PyObject *lookup_dict;
2221
2222 if (!(lookup_dict = PyDict_New()))
2223 {
2224 list_unref(list);
2225 return NULL;
2226 }
2227
2228 if (list_py_concat(list, obj, lookup_dict) == -1)
2229 {
2230 Py_DECREF(lookup_dict);
2231 list_unref(list);
2232 return NULL;
2233 }
2234
2235 Py_DECREF(lookup_dict);
2236 }
2237
2238 return ListNew(subtype, list);
2239}
2240
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002241 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002242ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002243{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002244 pyll_remove(&self->ref, &lastlist);
2245 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002246
2247 DESTRUCTOR_FINISH(self);
2248}
2249
Bram Moolenaardb913952012-06-29 12:54:53 +02002250 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002251ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002252{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002253 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002254}
2255
2256 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002257ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002258{
2259 listitem_T *li;
2260
Bram Moolenaard6e39182013-05-21 18:30:34 +02002261 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002262 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002263 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002264 return NULL;
2265 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002266 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002267 if (li == NULL)
2268 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002269 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002270 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002271 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002272 return NULL;
2273 }
2274 return ConvertToPyObject(&li->li_tv);
2275}
2276
Bram Moolenaardb913952012-06-29 12:54:53 +02002277 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002278ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2279 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002280{
2281 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002282 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002283
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002284 if (step == 0)
2285 {
2286 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2287 return NULL;
2288 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002289
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002290 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002291 if (list == NULL)
2292 return NULL;
2293
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002294 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002295 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002296 PyObject *item;
2297
2298 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002299 if (item == NULL)
2300 {
2301 Py_DECREF(list);
2302 return NULL;
2303 }
2304
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002305 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002306 }
2307
2308 return list;
2309}
2310
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002311 static PyObject *
2312ListItem(ListObject *self, PyObject* idx)
2313{
2314#if PY_MAJOR_VERSION < 3
2315 if (PyInt_Check(idx))
2316 {
2317 long _idx = PyInt_AsLong(idx);
2318 return ListIndex(self, _idx);
2319 }
2320 else
2321#endif
2322 if (PyLong_Check(idx))
2323 {
2324 long _idx = PyLong_AsLong(idx);
2325 return ListIndex(self, _idx);
2326 }
2327 else if (PySlice_Check(idx))
2328 {
2329 Py_ssize_t start, stop, step, slicelen;
2330
Bram Moolenaar922a4662014-03-30 16:11:43 +02002331 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002332 &start, &stop, &step, &slicelen) < 0)
2333 return NULL;
2334 return ListSlice(self, start, step, slicelen);
2335 }
2336 else
2337 {
2338 RAISE_INVALID_INDEX_TYPE(idx);
2339 return NULL;
2340 }
2341}
2342
2343 static void
2344list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2345 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2346{
2347 while (numreplaced--)
2348 {
2349 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2350 listitem_remove(l, lis[slicelen + numreplaced]);
2351 }
2352 while (numadded--)
2353 {
2354 listitem_T *next;
2355
2356 next = lastaddedli->li_prev;
2357 listitem_remove(l, lastaddedli);
2358 lastaddedli = next;
2359 }
2360}
2361
2362 static int
2363ListAssSlice(ListObject *self, Py_ssize_t first,
2364 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2365{
2366 PyObject *iterator;
2367 PyObject *item;
2368 listitem_T *li;
2369 listitem_T *lastaddedli = NULL;
2370 listitem_T *next;
2371 typval_T v;
2372 list_T *l = self->list;
2373 PyInt i;
2374 PyInt j;
2375 PyInt numreplaced = 0;
2376 PyInt numadded = 0;
2377 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002378 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002379
2380 size = ListLength(self);
2381
2382 if (l->lv_lock)
2383 {
2384 RAISE_LOCKED_LIST;
2385 return -1;
2386 }
2387
2388 if (step == 0)
2389 {
2390 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2391 return -1;
2392 }
2393
2394 if (step != 1 && slicelen == 0)
2395 {
2396 /* Nothing to do. Only error out if obj has some items. */
2397 int ret = 0;
2398
2399 if (obj == NULL)
2400 return 0;
2401
2402 if (!(iterator = PyObject_GetIter(obj)))
2403 return -1;
2404
2405 if ((item = PyIter_Next(iterator)))
2406 {
2407 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002408 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002409 "to extended slice"), 0);
2410 Py_DECREF(item);
2411 ret = -1;
2412 }
2413 Py_DECREF(iterator);
2414 return ret;
2415 }
2416
2417 if (obj != NULL)
2418 /* XXX May allocate zero bytes. */
2419 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2420 {
2421 PyErr_NoMemory();
2422 return -1;
2423 }
2424
2425 if (first == size)
2426 li = NULL;
2427 else
2428 {
2429 li = list_find(l, (long) first);
2430 if (li == NULL)
2431 {
2432 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2433 (int)first);
2434 if (obj != NULL)
2435 PyMem_Free(lis);
2436 return -1;
2437 }
2438 i = slicelen;
2439 while (i-- && li != NULL)
2440 {
2441 j = step;
2442 next = li;
2443 if (step > 0)
2444 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2445 else
2446 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2447
2448 if (obj == NULL)
2449 listitem_remove(l, li);
2450 else
2451 lis[slicelen - i - 1] = li;
2452
2453 li = next;
2454 }
2455 if (li == NULL && i != -1)
2456 {
2457 PyErr_SET_VIM(N_("internal error: not enough list items"));
2458 if (obj != NULL)
2459 PyMem_Free(lis);
2460 return -1;
2461 }
2462 }
2463
2464 if (obj == NULL)
2465 return 0;
2466
2467 if (!(iterator = PyObject_GetIter(obj)))
2468 {
2469 PyMem_Free(lis);
2470 return -1;
2471 }
2472
2473 i = 0;
2474 while ((item = PyIter_Next(iterator)))
2475 {
2476 if (ConvertFromPyObject(item, &v) == -1)
2477 {
2478 Py_DECREF(iterator);
2479 Py_DECREF(item);
2480 PyMem_Free(lis);
2481 return -1;
2482 }
2483 Py_DECREF(item);
2484 if (list_insert_tv(l, &v, numreplaced < slicelen
2485 ? lis[numreplaced]
2486 : li) == FAIL)
2487 {
2488 clear_tv(&v);
2489 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2490 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2491 PyMem_Free(lis);
2492 return -1;
2493 }
2494 if (numreplaced < slicelen)
2495 {
2496 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002497 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002498 numreplaced++;
2499 }
2500 else
2501 {
2502 if (li)
2503 lastaddedli = li->li_prev;
2504 else
2505 lastaddedli = l->lv_last;
2506 numadded++;
2507 }
2508 clear_tv(&v);
2509 if (step != 1 && i >= slicelen)
2510 {
2511 Py_DECREF(iterator);
2512 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002513 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002514 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002515 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2516 PyMem_Free(lis);
2517 return -1;
2518 }
2519 ++i;
2520 }
2521 Py_DECREF(iterator);
2522
2523 if (step != 1 && i != slicelen)
2524 {
2525 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002526 N_("attempt to assign sequence of size %d to extended slice "
2527 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002528 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2529 PyMem_Free(lis);
2530 return -1;
2531 }
2532
2533 if (PyErr_Occurred())
2534 {
2535 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2536 PyMem_Free(lis);
2537 return -1;
2538 }
2539
2540 for (i = 0; i < numreplaced; i++)
2541 listitem_free(lis[i]);
2542 if (step == 1)
2543 for (i = numreplaced; i < slicelen; i++)
2544 listitem_remove(l, lis[i]);
2545
2546 PyMem_Free(lis);
2547
2548 return 0;
2549}
2550
2551 static int
2552ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2553{
2554 typval_T tv;
2555 list_T *l = self->list;
2556 listitem_T *li;
2557 Py_ssize_t length = ListLength(self);
2558
2559 if (l->lv_lock)
2560 {
2561 RAISE_LOCKED_LIST;
2562 return -1;
2563 }
2564 if (index > length || (index == length && obj == NULL))
2565 {
2566 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2567 return -1;
2568 }
2569
2570 if (obj == NULL)
2571 {
2572 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002573 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002574 clear_tv(&li->li_tv);
2575 vim_free(li);
2576 return 0;
2577 }
2578
2579 if (ConvertFromPyObject(obj, &tv) == -1)
2580 return -1;
2581
2582 if (index == length)
2583 {
2584 if (list_append_tv(l, &tv) == FAIL)
2585 {
2586 clear_tv(&tv);
2587 PyErr_SET_VIM(N_("failed to add item to list"));
2588 return -1;
2589 }
2590 }
2591 else
2592 {
2593 li = list_find(l, (long) index);
2594 clear_tv(&li->li_tv);
2595 copy_tv(&tv, &li->li_tv);
2596 clear_tv(&tv);
2597 }
2598 return 0;
2599}
2600
2601 static Py_ssize_t
2602ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2603{
2604#if PY_MAJOR_VERSION < 3
2605 if (PyInt_Check(idx))
2606 {
2607 long _idx = PyInt_AsLong(idx);
2608 return ListAssIndex(self, _idx, obj);
2609 }
2610 else
2611#endif
2612 if (PyLong_Check(idx))
2613 {
2614 long _idx = PyLong_AsLong(idx);
2615 return ListAssIndex(self, _idx, obj);
2616 }
2617 else if (PySlice_Check(idx))
2618 {
2619 Py_ssize_t start, stop, step, slicelen;
2620
Bram Moolenaar922a4662014-03-30 16:11:43 +02002621 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002622 &start, &stop, &step, &slicelen) < 0)
2623 return -1;
2624 return ListAssSlice(self, start, step, slicelen,
2625 obj);
2626 }
2627 else
2628 {
2629 RAISE_INVALID_INDEX_TYPE(idx);
2630 return -1;
2631 }
2632}
2633
2634 static PyObject *
2635ListConcatInPlace(ListObject *self, PyObject *obj)
2636{
2637 list_T *l = self->list;
2638 PyObject *lookup_dict;
2639
2640 if (l->lv_lock)
2641 {
2642 RAISE_LOCKED_LIST;
2643 return NULL;
2644 }
2645
2646 if (!(lookup_dict = PyDict_New()))
2647 return NULL;
2648
2649 if (list_py_concat(l, obj, lookup_dict) == -1)
2650 {
2651 Py_DECREF(lookup_dict);
2652 return NULL;
2653 }
2654 Py_DECREF(lookup_dict);
2655
2656 Py_INCREF(self);
2657 return (PyObject *)(self);
2658}
2659
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002660typedef struct
2661{
2662 listwatch_T lw;
2663 list_T *list;
2664} listiterinfo_T;
2665
2666 static void
2667ListIterDestruct(listiterinfo_T *lii)
2668{
2669 list_rem_watch(lii->list, &lii->lw);
2670 PyMem_Free(lii);
2671}
2672
2673 static PyObject *
2674ListIterNext(listiterinfo_T **lii)
2675{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002676 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002677
2678 if (!((*lii)->lw.lw_item))
2679 return NULL;
2680
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002681 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002682 return NULL;
2683
2684 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2685
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002686 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002687}
2688
2689 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002690ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002691{
2692 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002693 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002694
2695 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2696 {
2697 PyErr_NoMemory();
2698 return NULL;
2699 }
2700
2701 list_add_watch(l, &lii->lw);
2702 lii->lw.lw_item = l->lv_first;
2703 lii->list = l;
2704
2705 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002706 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2707 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002708}
2709
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002710static char *ListAttrs[] = {
2711 "locked",
2712 NULL
2713};
2714
2715 static PyObject *
2716ListDir(PyObject *self)
2717{
2718 return ObjectDir(self, ListAttrs);
2719}
2720
Bram Moolenaar66b79852012-09-21 14:00:35 +02002721 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002722ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002723{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002724 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002725 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002726 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002727 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002728 return -1;
2729 }
2730
2731 if (strcmp(name, "locked") == 0)
2732 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002733 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002734 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002735 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002736 return -1;
2737 }
2738 else
2739 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002740 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002741 if (istrue == -1)
2742 return -1;
2743 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002744 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002745 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002746 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002747 }
2748 return 0;
2749 }
2750 else
2751 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002752 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002753 return -1;
2754 }
2755}
2756
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002757static PySequenceMethods ListAsSeq = {
2758 (lenfunc) ListLength, /* sq_length, len(x) */
2759 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2760 0, /* RangeRepeat, sq_repeat, x*n */
2761 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2762 0, /* was_sq_slice, x[i:j] */
2763 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2764 0, /* was_sq_ass_slice, x[i:j]=v */
2765 0, /* sq_contains */
2766 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2767 0, /* sq_inplace_repeat */
2768};
2769
2770static PyMappingMethods ListAsMapping = {
2771 /* mp_length */ (lenfunc) ListLength,
2772 /* mp_subscript */ (binaryfunc) ListItem,
2773 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2774};
2775
Bram Moolenaardb913952012-06-29 12:54:53 +02002776static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002777 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2778 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2779 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002780};
2781
2782typedef struct
2783{
2784 PyObject_HEAD
2785 char_u *name;
2786} FunctionObject;
2787
2788static PyTypeObject FunctionType;
2789
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002790#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2791
Bram Moolenaardb913952012-06-29 12:54:53 +02002792 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002793FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002794{
2795 FunctionObject *self;
2796
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002797 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2798
Bram Moolenaardb913952012-06-29 12:54:53 +02002799 if (self == NULL)
2800 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002801
2802 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002803 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002804 if (!translated_function_exists(name))
2805 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002806 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002807 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002808 return NULL;
2809 }
2810 self->name = vim_strsave(name);
2811 func_ref(self->name);
2812 }
2813 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002814 if ((self->name = get_expanded_name(name,
2815 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2816 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002817 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002818 PyErr_FORMAT(PyExc_ValueError,
2819 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002820 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002821 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002822
2823 return (PyObject *)(self);
2824}
2825
2826 static PyObject *
2827FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2828{
2829 PyObject *self;
2830 char_u *name;
2831
2832 if (kwargs)
2833 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002834 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002835 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002836 return NULL;
2837 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002838
Bram Moolenaar389a1792013-06-23 13:00:44 +02002839 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002840 return NULL;
2841
2842 self = FunctionNew(subtype, name);
2843
Bram Moolenaar389a1792013-06-23 13:00:44 +02002844 PyMem_Free(name);
2845
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002846 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002847}
2848
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002849 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002850FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002851{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002852 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002853 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002854
2855 DESTRUCTOR_FINISH(self);
2856}
2857
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002858static char *FunctionAttrs[] = {
2859 "softspace",
2860 NULL
2861};
2862
2863 static PyObject *
2864FunctionDir(PyObject *self)
2865{
2866 return ObjectDir(self, FunctionAttrs);
2867}
2868
Bram Moolenaardb913952012-06-29 12:54:53 +02002869 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002870FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002871{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002872 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002873 typval_T args;
2874 typval_T selfdicttv;
2875 typval_T rettv;
2876 dict_T *selfdict = NULL;
2877 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002878 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002879 int error;
2880
2881 if (ConvertFromPyObject(argsObject, &args) == -1)
2882 return NULL;
2883
2884 if (kwargs != NULL)
2885 {
2886 selfdictObject = PyDict_GetItemString(kwargs, "self");
2887 if (selfdictObject != NULL)
2888 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002889 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002890 {
2891 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002892 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002893 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002894 selfdict = selfdicttv.vval.v_dict;
2895 }
2896 }
2897
Bram Moolenaar71700b82013-05-15 17:49:05 +02002898 Py_BEGIN_ALLOW_THREADS
2899 Python_Lock_Vim();
2900
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002901 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002902 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002903
2904 Python_Release_Vim();
2905 Py_END_ALLOW_THREADS
2906
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002907 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002908 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002909 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002910 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002911 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002912 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002913 }
2914 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002915 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002916
Bram Moolenaardb913952012-06-29 12:54:53 +02002917 clear_tv(&args);
2918 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002919 if (selfdict != NULL)
2920 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002921
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002922 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002923}
2924
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002925 static PyObject *
2926FunctionRepr(FunctionObject *self)
2927{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002928#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002929 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002930 * Finalize */
2931 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002932 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002933#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002934 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002935#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002936}
2937
Bram Moolenaardb913952012-06-29 12:54:53 +02002938static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002939 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2940 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002941};
2942
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002943/*
2944 * Options object
2945 */
2946
2947static PyTypeObject OptionsType;
2948
2949typedef int (*checkfun)(void *);
2950
2951typedef struct
2952{
2953 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01002954 int opt_type;
2955 void *from;
2956 checkfun Check;
2957 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002958} OptionsObject;
2959
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002960 static int
2961dummy_check(void *arg UNUSED)
2962{
2963 return 0;
2964}
2965
2966 static PyObject *
2967OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2968{
2969 OptionsObject *self;
2970
Bram Moolenaar774267b2013-05-21 20:51:59 +02002971 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002972 if (self == NULL)
2973 return NULL;
2974
2975 self->opt_type = opt_type;
2976 self->from = from;
2977 self->Check = Check;
2978 self->fromObj = fromObj;
2979 if (fromObj)
2980 Py_INCREF(fromObj);
2981
2982 return (PyObject *)(self);
2983}
2984
2985 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002986OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002987{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002988 PyObject_GC_UnTrack((void *)(self));
2989 Py_XDECREF(self->fromObj);
2990 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002991}
2992
2993 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002994OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002995{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002996 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002997 return 0;
2998}
2999
3000 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003001OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003002{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003003 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003004 return 0;
3005}
3006
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003007 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003008OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003009{
3010 char_u *key;
3011 int flags;
3012 long numval;
3013 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003014 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003015
Bram Moolenaard6e39182013-05-21 18:30:34 +02003016 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003017 return NULL;
3018
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003019 if (!(key = StringToChars(keyObject, &todecref)))
3020 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003021
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003022 if (*key == NUL)
3023 {
3024 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003025 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003026 return NULL;
3027 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003028
3029 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003030 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003031
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003032 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003033
3034 if (flags == 0)
3035 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003036 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003037 return NULL;
3038 }
3039
3040 if (flags & SOPT_UNSET)
3041 {
3042 Py_INCREF(Py_None);
3043 return Py_None;
3044 }
3045 else if (flags & SOPT_BOOL)
3046 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003047 PyObject *ret;
3048 ret = numval ? Py_True : Py_False;
3049 Py_INCREF(ret);
3050 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003051 }
3052 else if (flags & SOPT_NUM)
3053 return PyInt_FromLong(numval);
3054 else if (flags & SOPT_STRING)
3055 {
3056 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003057 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003058 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003059 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003060 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003061 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003062 else
3063 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003064 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003065 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003066 return NULL;
3067 }
3068 }
3069 else
3070 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003071 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003072 return NULL;
3073 }
3074}
3075
3076 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003077OptionsContains(OptionsObject *self, PyObject *keyObject)
3078{
3079 char_u *key;
3080 PyObject *todecref;
3081
3082 if (!(key = StringToChars(keyObject, &todecref)))
3083 return -1;
3084
3085 if (*key == NUL)
3086 {
3087 Py_XDECREF(todecref);
3088 return 0;
3089 }
3090
3091 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3092 {
3093 Py_XDECREF(todecref);
3094 return 1;
3095 }
3096 else
3097 {
3098 Py_XDECREF(todecref);
3099 return 0;
3100 }
3101}
3102
3103typedef struct
3104{
3105 void *lastoption;
3106 int opt_type;
3107} optiterinfo_T;
3108
3109 static PyObject *
3110OptionsIterNext(optiterinfo_T **oii)
3111{
3112 char_u *name;
3113
3114 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3115 return PyString_FromString((char *)name);
3116
3117 return NULL;
3118}
3119
3120 static PyObject *
3121OptionsIter(OptionsObject *self)
3122{
3123 optiterinfo_T *oii;
3124
3125 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3126 {
3127 PyErr_NoMemory();
3128 return NULL;
3129 }
3130
3131 oii->opt_type = self->opt_type;
3132 oii->lastoption = NULL;
3133
3134 return IterNew(oii,
3135 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3136 NULL, NULL);
3137}
3138
3139 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003140set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003141{
3142 char_u *errmsg;
3143
3144 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3145 {
3146 if (VimTryEnd())
3147 return FAIL;
3148 PyErr_SetVim((char *)errmsg);
3149 return FAIL;
3150 }
3151 return OK;
3152}
3153
3154 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003155set_option_value_for(
3156 char_u *key,
3157 int numval,
3158 char_u *stringval,
3159 int opt_flags,
3160 int opt_type,
3161 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003162{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003163 win_T *save_curwin = NULL;
3164 tabpage_T *save_curtab = NULL;
3165 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003166 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003167
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003168 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003169 switch (opt_type)
3170 {
3171 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003172 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003173 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003174 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003175 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003176 if (VimTryEnd())
3177 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003178 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003179 return -1;
3180 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003181 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3182 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003183 break;
3184 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003185 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003186 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003187 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003188 break;
3189 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003190 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003191 break;
3192 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003193 if (set_ret == FAIL)
3194 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003195 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003196}
3197
3198 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003199OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003200{
3201 char_u *key;
3202 int flags;
3203 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003204 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003205 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003206
Bram Moolenaard6e39182013-05-21 18:30:34 +02003207 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003208 return -1;
3209
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003210 if (!(key = StringToChars(keyObject, &todecref)))
3211 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003212
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003213 if (*key == NUL)
3214 {
3215 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003216 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003217 return -1;
3218 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003219
3220 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003221 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003222
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003223 if (flags == 0)
3224 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003225 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003226 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003227 return -1;
3228 }
3229
3230 if (valObject == NULL)
3231 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003232 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003233 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003234 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003235 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003236 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003237 return -1;
3238 }
3239 else if (!(flags & SOPT_GLOBAL))
3240 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003241 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003242 N_("unable to unset option %s "
3243 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003244 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003245 return -1;
3246 }
3247 else
3248 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003249 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003250 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003251 return 0;
3252 }
3253 }
3254
Bram Moolenaard6e39182013-05-21 18:30:34 +02003255 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003256
3257 if (flags & SOPT_BOOL)
3258 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003259 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003260
Bram Moolenaarb983f752013-05-15 16:11:50 +02003261 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003262 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003263 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003264 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003265 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003266 }
3267 else if (flags & SOPT_NUM)
3268 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003269 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003270
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003271 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003272 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003273 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003274 return -1;
3275 }
3276
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003277 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003278 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003279 }
3280 else
3281 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003282 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003283 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003284
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003285 if ((val = StringToChars(valObject, &todecref2)))
3286 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003287 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003288 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003289 Py_XDECREF(todecref2);
3290 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003291 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003292 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003293 }
3294
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003295 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003296
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003297 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003298}
3299
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003300static PySequenceMethods OptionsAsSeq = {
3301 0, /* sq_length */
3302 0, /* sq_concat */
3303 0, /* sq_repeat */
3304 0, /* sq_item */
3305 0, /* sq_slice */
3306 0, /* sq_ass_item */
3307 0, /* sq_ass_slice */
3308 (objobjproc) OptionsContains, /* sq_contains */
3309 0, /* sq_inplace_concat */
3310 0, /* sq_inplace_repeat */
3311};
3312
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003313static PyMappingMethods OptionsAsMapping = {
3314 (lenfunc) NULL,
3315 (binaryfunc) OptionsItem,
3316 (objobjargproc) OptionsAssItem,
3317};
3318
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003319/* Tabpage object
3320 */
3321
3322typedef struct
3323{
3324 PyObject_HEAD
3325 tabpage_T *tab;
3326} TabPageObject;
3327
3328static PyObject *WinListNew(TabPageObject *tabObject);
3329
3330static PyTypeObject TabPageType;
3331
3332 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003333CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003334{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003335 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003336 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003337 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003338 return -1;
3339 }
3340
3341 return 0;
3342}
3343
3344 static PyObject *
3345TabPageNew(tabpage_T *tab)
3346{
3347 TabPageObject *self;
3348
3349 if (TAB_PYTHON_REF(tab))
3350 {
3351 self = TAB_PYTHON_REF(tab);
3352 Py_INCREF(self);
3353 }
3354 else
3355 {
3356 self = PyObject_NEW(TabPageObject, &TabPageType);
3357 if (self == NULL)
3358 return NULL;
3359 self->tab = tab;
3360 TAB_PYTHON_REF(tab) = self;
3361 }
3362
3363 return (PyObject *)(self);
3364}
3365
3366 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003367TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003368{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003369 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3370 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003371
3372 DESTRUCTOR_FINISH(self);
3373}
3374
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003375static char *TabPageAttrs[] = {
3376 "windows", "number", "vars", "window", "valid",
3377 NULL
3378};
3379
3380 static PyObject *
3381TabPageDir(PyObject *self)
3382{
3383 return ObjectDir(self, TabPageAttrs);
3384}
3385
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003386 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003387TabPageAttrValid(TabPageObject *self, char *name)
3388{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003389 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003390
3391 if (strcmp(name, "valid") != 0)
3392 return NULL;
3393
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003394 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3395 Py_INCREF(ret);
3396 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003397}
3398
3399 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003400TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003401{
3402 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003403 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003404 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003405 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003406 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003407 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003408 else if (strcmp(name, "window") == 0)
3409 {
3410 /* For current tab window.c does not bother to set or update tp_curwin
3411 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003412 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003413 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003414 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003415 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003416 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003417 else if (strcmp(name, "__members__") == 0)
3418 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003419 return NULL;
3420}
3421
3422 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003423TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003424{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003425 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003426 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003427 else
3428 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003429 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003430
3431 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003432 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3433 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003434 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003435 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003436 }
3437}
3438
3439static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003440 /* name, function, calling, documentation */
3441 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3442 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003443};
3444
3445/*
3446 * Window list object
3447 */
3448
3449static PyTypeObject TabListType;
3450static PySequenceMethods TabListAsSeq;
3451
3452typedef struct
3453{
3454 PyObject_HEAD
3455} TabListObject;
3456
3457 static PyInt
3458TabListLength(PyObject *self UNUSED)
3459{
3460 tabpage_T *tp = first_tabpage;
3461 PyInt n = 0;
3462
3463 while (tp != NULL)
3464 {
3465 ++n;
3466 tp = tp->tp_next;
3467 }
3468
3469 return n;
3470}
3471
3472 static PyObject *
3473TabListItem(PyObject *self UNUSED, PyInt n)
3474{
3475 tabpage_T *tp;
3476
3477 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3478 if (n == 0)
3479 return TabPageNew(tp);
3480
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003481 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003482 return NULL;
3483}
3484
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003485/*
3486 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003487 */
3488
3489typedef struct
3490{
3491 PyObject_HEAD
3492 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003493 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003494} WindowObject;
3495
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003496static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003497
3498 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003499CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003500{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003501 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003502 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003503 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003504 return -1;
3505 }
3506
3507 return 0;
3508}
3509
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003510 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003511WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003512{
3513 /* We need to handle deletion of windows underneath us.
3514 * If we add a "w_python*_ref" field to the win_T structure,
3515 * then we can get at it in win_free() in vim. We then
3516 * need to create only ONE Python object per window - if
3517 * we try to create a second, just INCREF the existing one
3518 * and return it. The (single) Python object referring to
3519 * the window is stored in "w_python*_ref".
3520 * On a win_free() we set the Python object's win_T* field
3521 * to an invalid value. We trap all uses of a window
3522 * object, and reject them if the win_T* field is invalid.
3523 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003524 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003525 * w_python_ref and w_python3_ref fields respectively.
3526 */
3527
3528 WindowObject *self;
3529
3530 if (WIN_PYTHON_REF(win))
3531 {
3532 self = WIN_PYTHON_REF(win);
3533 Py_INCREF(self);
3534 }
3535 else
3536 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003537 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003538 if (self == NULL)
3539 return NULL;
3540 self->win = win;
3541 WIN_PYTHON_REF(win) = self;
3542 }
3543
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003544 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3545
Bram Moolenaar971db462013-05-12 18:44:48 +02003546 return (PyObject *)(self);
3547}
3548
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003549 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003550WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003551{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003552 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003553 if (self->win && self->win != INVALID_WINDOW_VALUE)
3554 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003555 Py_XDECREF(((PyObject *)(self->tabObject)));
3556 PyObject_GC_Del((void *)(self));
3557}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003558
Bram Moolenaar774267b2013-05-21 20:51:59 +02003559 static int
3560WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3561{
3562 Py_VISIT(((PyObject *)(self->tabObject)));
3563 return 0;
3564}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003565
Bram Moolenaar774267b2013-05-21 20:51:59 +02003566 static int
3567WindowClear(WindowObject *self)
3568{
3569 Py_CLEAR(self->tabObject);
3570 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003571}
3572
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003573 static win_T *
3574get_firstwin(TabPageObject *tabObject)
3575{
3576 if (tabObject)
3577 {
3578 if (CheckTabPage(tabObject))
3579 return NULL;
3580 /* For current tab window.c does not bother to set or update tp_firstwin
3581 */
3582 else if (tabObject->tab == curtab)
3583 return firstwin;
3584 else
3585 return tabObject->tab->tp_firstwin;
3586 }
3587 else
3588 return firstwin;
3589}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003590static char *WindowAttrs[] = {
3591 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3592 "tabpage", "valid",
3593 NULL
3594};
3595
3596 static PyObject *
3597WindowDir(PyObject *self)
3598{
3599 return ObjectDir(self, WindowAttrs);
3600}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003601
Bram Moolenaar971db462013-05-12 18:44:48 +02003602 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003603WindowAttrValid(WindowObject *self, char *name)
3604{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003605 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003606
3607 if (strcmp(name, "valid") != 0)
3608 return NULL;
3609
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003610 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3611 Py_INCREF(ret);
3612 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003613}
3614
3615 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003616WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003617{
3618 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003619 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003620 else if (strcmp(name, "cursor") == 0)
3621 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003622 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003623
3624 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3625 }
3626 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003627 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003628#ifdef FEAT_WINDOWS
3629 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003630 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003631#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003632#ifdef FEAT_VERTSPLIT
3633 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003634 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003635 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003636 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003637#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003638 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003639 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003640 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003641 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3642 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003643 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003644 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003645 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003646 return NULL;
3647 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003648 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003649 }
3650 else if (strcmp(name, "tabpage") == 0)
3651 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003652 Py_INCREF(self->tabObject);
3653 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003654 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003655 else if (strcmp(name, "__members__") == 0)
3656 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003657 else
3658 return NULL;
3659}
3660
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003661 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003662WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003663{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003664 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003665 return -1;
3666
3667 if (strcmp(name, "buffer") == 0)
3668 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003669 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003670 return -1;
3671 }
3672 else if (strcmp(name, "cursor") == 0)
3673 {
3674 long lnum;
3675 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003676
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003677 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003678 return -1;
3679
Bram Moolenaard6e39182013-05-21 18:30:34 +02003680 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003681 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003682 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003683 return -1;
3684 }
3685
3686 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003687 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003688 return -1;
3689
Bram Moolenaard6e39182013-05-21 18:30:34 +02003690 self->win->w_cursor.lnum = lnum;
3691 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003692#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003693 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003694#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003695 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003696 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003697
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003698 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003699 return 0;
3700 }
3701 else if (strcmp(name, "height") == 0)
3702 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003703 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003704 win_T *savewin;
3705
Bram Moolenaardee2e312013-06-23 16:35:47 +02003706 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003707 return -1;
3708
3709#ifdef FEAT_GUI
3710 need_mouse_correct = TRUE;
3711#endif
3712 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003713 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003714
3715 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003716 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003717 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003718 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003719 return -1;
3720
3721 return 0;
3722 }
3723#ifdef FEAT_VERTSPLIT
3724 else if (strcmp(name, "width") == 0)
3725 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003726 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003727 win_T *savewin;
3728
Bram Moolenaardee2e312013-06-23 16:35:47 +02003729 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003730 return -1;
3731
3732#ifdef FEAT_GUI
3733 need_mouse_correct = TRUE;
3734#endif
3735 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003736 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003737
3738 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003739 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003740 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003741 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003742 return -1;
3743
3744 return 0;
3745 }
3746#endif
3747 else
3748 {
3749 PyErr_SetString(PyExc_AttributeError, name);
3750 return -1;
3751 }
3752}
3753
3754 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003755WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003756{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003757 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003758 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003759 else
3760 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003761 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003762
Bram Moolenaar6d216452013-05-12 19:00:41 +02003763 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003764 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003765 (self));
3766 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003767 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003768 }
3769}
3770
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003771static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003772 /* name, function, calling, documentation */
3773 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3774 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003775};
3776
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003777/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003778 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003779 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003780
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003781static PyTypeObject WinListType;
3782static PySequenceMethods WinListAsSeq;
3783
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003784typedef struct
3785{
3786 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003787 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003788} WinListObject;
3789
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003790 static PyObject *
3791WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003792{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003793 WinListObject *self;
3794
3795 self = PyObject_NEW(WinListObject, &WinListType);
3796 self->tabObject = tabObject;
3797 Py_INCREF(tabObject);
3798
3799 return (PyObject *)(self);
3800}
3801
3802 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003803WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003804{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003805 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003806
3807 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003808 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003809 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003810 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003811
3812 DESTRUCTOR_FINISH(self);
3813}
3814
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003815 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003816WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003817{
3818 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003819 PyInt n = 0;
3820
Bram Moolenaard6e39182013-05-21 18:30:34 +02003821 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003822 return -1;
3823
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003824 while (w != NULL)
3825 {
3826 ++n;
3827 w = W_NEXT(w);
3828 }
3829
3830 return n;
3831}
3832
3833 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003834WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003835{
3836 win_T *w;
3837
Bram Moolenaard6e39182013-05-21 18:30:34 +02003838 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003839 return NULL;
3840
3841 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003842 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003843 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003844
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003845 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003846 return NULL;
3847}
3848
3849/* Convert a Python string into a Vim line.
3850 *
3851 * The result is in allocated memory. All internal nulls are replaced by
3852 * newline characters. It is an error for the string to contain newline
3853 * characters.
3854 *
3855 * On errors, the Python exception data is set, and NULL is returned.
3856 */
3857 static char *
3858StringToLine(PyObject *obj)
3859{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003860 char *str;
3861 char *save;
3862 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003863 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003864 PyInt i;
3865 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003866
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003867 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003868 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003869 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3870 || str == NULL)
3871 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003872 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003873 else if (PyUnicode_Check(obj))
3874 {
3875 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3876 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003877
Bram Moolenaardaa27022013-06-24 22:33:30 +02003878 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003879 || str == NULL)
3880 {
3881 Py_DECREF(bytes);
3882 return NULL;
3883 }
3884 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003885 else
3886 {
3887#if PY_MAJOR_VERSION < 3
3888 PyErr_FORMAT(PyExc_TypeError,
3889 N_("expected str() or unicode() instance, but got %s"),
3890 Py_TYPE_NAME(obj));
3891#else
3892 PyErr_FORMAT(PyExc_TypeError,
3893 N_("expected bytes() or str() instance, but got %s"),
3894 Py_TYPE_NAME(obj));
3895#endif
3896 return NULL;
3897 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003898
3899 /*
3900 * Error checking: String must not contain newlines, as we
3901 * are replacing a single line, and we must replace it with
3902 * a single line.
3903 * A trailing newline is removed, so that append(f.readlines()) works.
3904 */
3905 p = memchr(str, '\n', len);
3906 if (p != NULL)
3907 {
3908 if (p == str + len - 1)
3909 --len;
3910 else
3911 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003912 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003913 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003914 return NULL;
3915 }
3916 }
3917
3918 /* Create a copy of the string, with internal nulls replaced by
3919 * newline characters, as is the vim convention.
3920 */
3921 save = (char *)alloc((unsigned)(len+1));
3922 if (save == NULL)
3923 {
3924 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003925 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003926 return NULL;
3927 }
3928
3929 for (i = 0; i < len; ++i)
3930 {
3931 if (str[i] == '\0')
3932 save[i] = '\n';
3933 else
3934 save[i] = str[i];
3935 }
3936
3937 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003938 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003939
3940 return save;
3941}
3942
3943/* Get a line from the specified buffer. The line number is
3944 * in Vim format (1-based). The line is returned as a Python
3945 * string object.
3946 */
3947 static PyObject *
3948GetBufferLine(buf_T *buf, PyInt n)
3949{
3950 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3951}
3952
3953
3954/* Get a list of lines from the specified buffer. The line numbers
3955 * are in Vim format (1-based). The range is from lo up to, but not
3956 * including, hi. The list is returned as a Python list of string objects.
3957 */
3958 static PyObject *
3959GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3960{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003961 PyInt i;
3962 PyInt n = hi - lo;
3963 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003964
3965 if (list == NULL)
3966 return NULL;
3967
3968 for (i = 0; i < n; ++i)
3969 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003970 PyObject *string = LineToString(
3971 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003972
3973 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003974 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003975 {
3976 Py_DECREF(list);
3977 return NULL;
3978 }
3979
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003980 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003981 }
3982
3983 /* The ownership of the Python list is passed to the caller (ie,
3984 * the caller should Py_DECREF() the object when it is finished
3985 * with it).
3986 */
3987
3988 return list;
3989}
3990
3991/*
3992 * Check if deleting lines made the cursor position invalid.
3993 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3994 * deleted).
3995 */
3996 static void
3997py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3998{
3999 if (curwin->w_cursor.lnum >= lo)
4000 {
4001 /* Adjust the cursor position if it's in/after the changed
4002 * lines. */
4003 if (curwin->w_cursor.lnum >= hi)
4004 {
4005 curwin->w_cursor.lnum += extra;
4006 check_cursor_col();
4007 }
4008 else if (extra < 0)
4009 {
4010 curwin->w_cursor.lnum = lo;
4011 check_cursor();
4012 }
4013 else
4014 check_cursor_col();
4015 changed_cline_bef_curs();
4016 }
4017 invalidate_botline();
4018}
4019
Bram Moolenaar19e60942011-06-19 00:27:51 +02004020/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004021 * Find a window that contains "buf" and switch to it.
4022 * If there is no such window, use the current window and change "curbuf".
4023 * Caller must initialize save_curbuf to NULL.
4024 * restore_win_for_buf() MUST be called later!
4025 */
4026 static void
4027switch_to_win_for_buf(
4028 buf_T *buf,
4029 win_T **save_curwinp,
4030 tabpage_T **save_curtabp,
4031 buf_T **save_curbufp)
4032{
4033 win_T *wp;
4034 tabpage_T *tp;
4035
Bram Moolenaarae38d052014-12-17 14:46:09 +01004036 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004037 switch_buffer(save_curbufp, buf);
Bram Moolenaarae38d052014-12-17 14:46:09 +01004038 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4039 {
4040 restore_win(*save_curwinp, *save_curtabp, TRUE);
4041 switch_buffer(save_curbufp, buf);
4042 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004043}
4044
4045 static void
4046restore_win_for_buf(
4047 win_T *save_curwin,
4048 tabpage_T *save_curtab,
4049 buf_T *save_curbuf)
4050{
4051 if (save_curbuf == NULL)
4052 restore_win(save_curwin, save_curtab, TRUE);
4053 else
4054 restore_buffer(save_curbuf);
4055}
4056
4057/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004058 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004059 * in Vim format (1-based). The replacement line is given as
4060 * a Python string object. The object is checked for validity
4061 * and correct format. Errors are returned as a value of FAIL.
4062 * The return value is OK on success.
4063 * If OK is returned and len_change is not NULL, *len_change
4064 * is set to the change in the buffer length.
4065 */
4066 static int
4067SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4068{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004069 buf_T *save_curbuf = NULL;
4070 win_T *save_curwin = NULL;
4071 tabpage_T *save_curtab = NULL;
4072
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004073 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004074 * There are three cases:
4075 * 1. NULL, or None - this is a deletion.
4076 * 2. A string - this is a replacement.
4077 * 3. Anything else - this is an error.
4078 */
4079 if (line == Py_None || line == NULL)
4080 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004081 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004082 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004083
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004084 VimTryStart();
4085
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004086 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004087 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004089 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004090 else
4091 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004092 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004094 if (save_curbuf == NULL)
4095 /* Only adjust marks if we managed to switch to a window that
4096 * holds the buffer, otherwise line numbers will be invalid. */
4097 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004098 }
4099
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004100 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004101
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004102 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004103 return FAIL;
4104
4105 if (len_change)
4106 *len_change = -1;
4107
4108 return OK;
4109 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004110 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004111 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004112 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113
4114 if (save == NULL)
4115 return FAIL;
4116
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004117 VimTryStart();
4118
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004119 /* We do not need to free "save" if ml_replace() consumes it. */
4120 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004121 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004122
4123 if (u_savesub((linenr_T)n) == FAIL)
4124 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004125 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004126 vim_free(save);
4127 }
4128 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4129 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004130 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004131 vim_free(save);
4132 }
4133 else
4134 changed_bytes((linenr_T)n, 0);
4135
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004136 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004137
4138 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004139 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004140 check_cursor_col();
4141
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004142 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004143 return FAIL;
4144
4145 if (len_change)
4146 *len_change = 0;
4147
4148 return OK;
4149 }
4150 else
4151 {
4152 PyErr_BadArgument();
4153 return FAIL;
4154 }
4155}
4156
Bram Moolenaar19e60942011-06-19 00:27:51 +02004157/* Replace a range of lines in the specified buffer. The line numbers are in
4158 * Vim format (1-based). The range is from lo up to, but not including, hi.
4159 * The replacement lines are given as a Python list of string objects. The
4160 * list is checked for validity and correct format. Errors are returned as a
4161 * value of FAIL. The return value is OK on success.
4162 * If OK is returned and len_change is not NULL, *len_change
4163 * is set to the change in the buffer length.
4164 */
4165 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004166SetBufferLineList(
4167 buf_T *buf,
4168 PyInt lo,
4169 PyInt hi,
4170 PyObject *list,
4171 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004172{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004173 buf_T *save_curbuf = NULL;
4174 win_T *save_curwin = NULL;
4175 tabpage_T *save_curtab = NULL;
4176
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004177 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004178 * There are three cases:
4179 * 1. NULL, or None - this is a deletion.
4180 * 2. A list - this is a replacement.
4181 * 3. Anything else - this is an error.
4182 */
4183 if (list == Py_None || list == NULL)
4184 {
4185 PyInt i;
4186 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004187
4188 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004189 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004190 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004191
4192 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004193 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004194 else
4195 {
4196 for (i = 0; i < n; ++i)
4197 {
4198 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4199 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004200 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004201 break;
4202 }
4203 }
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004204 if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL))
4205 /* Using an existing window for the buffer, adjust the cursor
4206 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004207 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004208 if (save_curbuf == NULL)
4209 /* Only adjust marks if we managed to switch to a window that
4210 * holds the buffer, otherwise line numbers will be invalid. */
4211 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004212 }
4213
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004214 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004215
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004216 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004217 return FAIL;
4218
4219 if (len_change)
4220 *len_change = -n;
4221
4222 return OK;
4223 }
4224 else if (PyList_Check(list))
4225 {
4226 PyInt i;
4227 PyInt new_len = PyList_Size(list);
4228 PyInt old_len = hi - lo;
4229 PyInt extra = 0; /* lines added to text, can be negative */
4230 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004231
4232 if (new_len == 0) /* avoid allocating zero bytes */
4233 array = NULL;
4234 else
4235 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004236 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004237 if (array == NULL)
4238 {
4239 PyErr_NoMemory();
4240 return FAIL;
4241 }
4242 }
4243
4244 for (i = 0; i < new_len; ++i)
4245 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004246 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004247
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004248 if (!(line = PyList_GetItem(list, i)) ||
4249 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004250 {
4251 while (i)
4252 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004253 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004254 return FAIL;
4255 }
4256 }
4257
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004258 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004259 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004260
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004261 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004262 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004263
4264 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004265 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004266
4267 /* If the size of the range is reducing (ie, new_len < old_len) we
4268 * need to delete some old_len. We do this at the start, by
4269 * repeatedly deleting line "lo".
4270 */
4271 if (!PyErr_Occurred())
4272 {
4273 for (i = 0; i < old_len - new_len; ++i)
4274 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4275 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004276 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004277 break;
4278 }
4279 extra -= i;
4280 }
4281
4282 /* For as long as possible, replace the existing old_len with the
4283 * new old_len. This is a more efficient operation, as it requires
4284 * less memory allocation and freeing.
4285 */
4286 if (!PyErr_Occurred())
4287 {
4288 for (i = 0; i < old_len && i < new_len; ++i)
4289 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4290 == FAIL)
4291 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004292 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004293 break;
4294 }
4295 }
4296 else
4297 i = 0;
4298
4299 /* Now we may need to insert the remaining new old_len. If we do, we
4300 * must free the strings as we finish with them (we can't pass the
4301 * responsibility to vim in this case).
4302 */
4303 if (!PyErr_Occurred())
4304 {
4305 while (i < new_len)
4306 {
4307 if (ml_append((linenr_T)(lo + i - 1),
4308 (char_u *)array[i], 0, FALSE) == FAIL)
4309 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004310 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004311 break;
4312 }
4313 vim_free(array[i]);
4314 ++i;
4315 ++extra;
4316 }
4317 }
4318
4319 /* Free any left-over old_len, as a result of an error */
4320 while (i < new_len)
4321 {
4322 vim_free(array[i]);
4323 ++i;
4324 }
4325
4326 /* Free the array of old_len. All of its contents have now
4327 * been dealt with (either freed, or the responsibility passed
4328 * to vim.
4329 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004330 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004331
4332 /* Adjust marks. Invalidate any which lie in the
4333 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004334 * Only adjust marks if we managed to switch to a window that holds
4335 * the buffer, otherwise line numbers will be invalid. */
4336 if (save_curbuf == NULL)
4337 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004338 (long)MAXLNUM, (long)extra);
4339 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4340
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004341 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004342 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4343
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004344 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004345 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004346
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004347 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004348 return FAIL;
4349
4350 if (len_change)
4351 *len_change = new_len - old_len;
4352
4353 return OK;
4354 }
4355 else
4356 {
4357 PyErr_BadArgument();
4358 return FAIL;
4359 }
4360}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004361
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004362/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004363 * The line number is in Vim format (1-based). The lines to be inserted are
4364 * given as a Python list of string objects or as a single string. The lines
4365 * to be added are checked for validity and correct format. Errors are
4366 * returned as a value of FAIL. The return value is OK on success.
4367 * If OK is returned and len_change is not NULL, *len_change
4368 * is set to the change in the buffer length.
4369 */
4370 static int
4371InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4372{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004373 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004374 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004375 tabpage_T *save_curtab = NULL;
4376
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004377 /* First of all, we check the type of the supplied Python object.
4378 * It must be a string or a list, or the call is in error.
4379 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004380 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004381 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004382 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004383
4384 if (str == NULL)
4385 return FAIL;
4386
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004387 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004388 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004389 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004391 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004392 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004393 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004394 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004395 else if (save_curbuf == NULL)
4396 /* Only adjust marks if we managed to switch to a window that
4397 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004398 appended_lines_mark((linenr_T)n, 1L);
4399
4400 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004401 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004402 update_screen(VALID);
4403
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004404 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004405 return FAIL;
4406
4407 if (len_change)
4408 *len_change = 1;
4409
4410 return OK;
4411 }
4412 else if (PyList_Check(lines))
4413 {
4414 PyInt i;
4415 PyInt size = PyList_Size(lines);
4416 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004417
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004418 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004419 if (array == NULL)
4420 {
4421 PyErr_NoMemory();
4422 return FAIL;
4423 }
4424
4425 for (i = 0; i < size; ++i)
4426 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004427 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004428
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004429 if (!(line = PyList_GetItem(lines, i)) ||
4430 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004431 {
4432 while (i)
4433 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004434 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004435 return FAIL;
4436 }
4437 }
4438
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004439 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004440 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004441 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004442
4443 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004444 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004445 else
4446 {
4447 for (i = 0; i < size; ++i)
4448 {
4449 if (ml_append((linenr_T)(n + i),
4450 (char_u *)array[i], 0, FALSE) == FAIL)
4451 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004452 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004453
4454 /* Free the rest of the lines */
4455 while (i < size)
4456 vim_free(array[i++]);
4457
4458 break;
4459 }
4460 vim_free(array[i]);
4461 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004462 if (i > 0 && save_curbuf == NULL)
4463 /* Only adjust marks if we managed to switch to a window that
4464 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004465 appended_lines_mark((linenr_T)n, (long)i);
4466 }
4467
4468 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004469 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004470 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004471 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004472
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004473 update_screen(VALID);
4474
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004475 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004476 return FAIL;
4477
4478 if (len_change)
4479 *len_change = size;
4480
4481 return OK;
4482 }
4483 else
4484 {
4485 PyErr_BadArgument();
4486 return FAIL;
4487 }
4488}
4489
4490/*
4491 * Common routines for buffers and line ranges
4492 * -------------------------------------------
4493 */
4494
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004495typedef struct
4496{
4497 PyObject_HEAD
4498 buf_T *buf;
4499} BufferObject;
4500
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004501 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004502CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004503{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004504 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004505 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004506 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004507 return -1;
4508 }
4509
4510 return 0;
4511}
4512
4513 static PyObject *
4514RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4515{
4516 if (CheckBuffer(self))
4517 return NULL;
4518
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004519 if (end == -1)
4520 end = self->buf->b_ml.ml_line_count;
4521
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004522 if (n < 0)
4523 n += end - start + 1;
4524
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004525 if (n < 0 || n > end - start)
4526 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004527 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004528 return NULL;
4529 }
4530
4531 return GetBufferLine(self->buf, n+start);
4532}
4533
4534 static PyObject *
4535RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4536{
4537 PyInt size;
4538
4539 if (CheckBuffer(self))
4540 return NULL;
4541
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004542 if (end == -1)
4543 end = self->buf->b_ml.ml_line_count;
4544
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004545 size = end - start + 1;
4546
4547 if (lo < 0)
4548 lo = 0;
4549 else if (lo > size)
4550 lo = size;
4551 if (hi < 0)
4552 hi = 0;
4553 if (hi < lo)
4554 hi = lo;
4555 else if (hi > size)
4556 hi = size;
4557
4558 return GetBufferLineList(self->buf, lo+start, hi+start);
4559}
4560
4561 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004562RBAsItem(
4563 BufferObject *self,
4564 PyInt n,
4565 PyObject *valObject,
4566 PyInt start,
4567 PyInt end,
4568 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004569{
4570 PyInt len_change;
4571
4572 if (CheckBuffer(self))
4573 return -1;
4574
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004575 if (end == -1)
4576 end = self->buf->b_ml.ml_line_count;
4577
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004578 if (n < 0)
4579 n += end - start + 1;
4580
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004581 if (n < 0 || n > end - start)
4582 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004583 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004584 return -1;
4585 }
4586
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004587 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004588 return -1;
4589
4590 if (new_end)
4591 *new_end = end + len_change;
4592
4593 return 0;
4594}
4595
Bram Moolenaar19e60942011-06-19 00:27:51 +02004596 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004597RBAsSlice(
4598 BufferObject *self,
4599 PyInt lo,
4600 PyInt hi,
4601 PyObject *valObject,
4602 PyInt start,
4603 PyInt end,
4604 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004605{
4606 PyInt size;
4607 PyInt len_change;
4608
4609 /* Self must be a valid buffer */
4610 if (CheckBuffer(self))
4611 return -1;
4612
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004613 if (end == -1)
4614 end = self->buf->b_ml.ml_line_count;
4615
Bram Moolenaar19e60942011-06-19 00:27:51 +02004616 /* Sort out the slice range */
4617 size = end - start + 1;
4618
4619 if (lo < 0)
4620 lo = 0;
4621 else if (lo > size)
4622 lo = size;
4623 if (hi < 0)
4624 hi = 0;
4625 if (hi < lo)
4626 hi = lo;
4627 else if (hi > size)
4628 hi = size;
4629
4630 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004631 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004632 return -1;
4633
4634 if (new_end)
4635 *new_end = end + len_change;
4636
4637 return 0;
4638}
4639
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004640
4641 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004642RBAppend(
4643 BufferObject *self,
4644 PyObject *args,
4645 PyInt start,
4646 PyInt end,
4647 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004648{
4649 PyObject *lines;
4650 PyInt len_change;
4651 PyInt max;
4652 PyInt n;
4653
4654 if (CheckBuffer(self))
4655 return NULL;
4656
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004657 if (end == -1)
4658 end = self->buf->b_ml.ml_line_count;
4659
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004660 max = n = end - start + 1;
4661
4662 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4663 return NULL;
4664
4665 if (n < 0 || n > max)
4666 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004667 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004668 return NULL;
4669 }
4670
4671 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4672 return NULL;
4673
4674 if (new_end)
4675 *new_end = end + len_change;
4676
4677 Py_INCREF(Py_None);
4678 return Py_None;
4679}
4680
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004681/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004682 */
4683
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004684static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004685static PySequenceMethods RangeAsSeq;
4686static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004687
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004688typedef struct
4689{
4690 PyObject_HEAD
4691 BufferObject *buf;
4692 PyInt start;
4693 PyInt end;
4694} RangeObject;
4695
4696 static PyObject *
4697RangeNew(buf_T *buf, PyInt start, PyInt end)
4698{
4699 BufferObject *bufr;
4700 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004701 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004702 if (self == NULL)
4703 return NULL;
4704
4705 bufr = (BufferObject *)BufferNew(buf);
4706 if (bufr == NULL)
4707 {
4708 Py_DECREF(self);
4709 return NULL;
4710 }
4711 Py_INCREF(bufr);
4712
4713 self->buf = bufr;
4714 self->start = start;
4715 self->end = end;
4716
4717 return (PyObject *)(self);
4718}
4719
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004720 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004721RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004722{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004723 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004724 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004725 PyObject_GC_Del((void *)(self));
4726}
4727
4728 static int
4729RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4730{
4731 Py_VISIT(((PyObject *)(self->buf)));
4732 return 0;
4733}
4734
4735 static int
4736RangeClear(RangeObject *self)
4737{
4738 Py_CLEAR(self->buf);
4739 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004740}
4741
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004742 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004743RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004744{
4745 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004746 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004747 return -1; /* ??? */
4748
Bram Moolenaard6e39182013-05-21 18:30:34 +02004749 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004750}
4751
4752 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004753RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004754{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004755 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004756}
4757
4758 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004759RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004760{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004761 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004762}
4763
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004764static char *RangeAttrs[] = {
4765 "start", "end",
4766 NULL
4767};
4768
4769 static PyObject *
4770RangeDir(PyObject *self)
4771{
4772 return ObjectDir(self, RangeAttrs);
4773}
4774
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004775 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004776RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004777{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004778 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004779}
4780
4781 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004782RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004783{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004784 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004785 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4786 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004787 else
4788 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004789 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004790
4791 if (name == NULL)
4792 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004793
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004794 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004795 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004796 }
4797}
4798
4799static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004800 /* name, function, calling, documentation */
4801 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004802 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4803 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004804};
4805
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004806static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004807static PySequenceMethods BufferAsSeq;
4808static PyMappingMethods BufferAsMapping;
4809
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004810 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004811BufferNew(buf_T *buf)
4812{
4813 /* We need to handle deletion of buffers underneath us.
4814 * If we add a "b_python*_ref" field to the buf_T structure,
4815 * then we can get at it in buf_freeall() in vim. We then
4816 * need to create only ONE Python object per buffer - if
4817 * we try to create a second, just INCREF the existing one
4818 * and return it. The (single) Python object referring to
4819 * the buffer is stored in "b_python*_ref".
4820 * Question: what to do on a buf_freeall(). We'll probably
4821 * have to either delete the Python object (DECREF it to
4822 * zero - a bad idea, as it leaves dangling refs!) or
4823 * set the buf_T * value to an invalid value (-1?), which
4824 * means we need checks in all access functions... Bah.
4825 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004826 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004827 * b_python_ref and b_python3_ref fields respectively.
4828 */
4829
4830 BufferObject *self;
4831
4832 if (BUF_PYTHON_REF(buf) != NULL)
4833 {
4834 self = BUF_PYTHON_REF(buf);
4835 Py_INCREF(self);
4836 }
4837 else
4838 {
4839 self = PyObject_NEW(BufferObject, &BufferType);
4840 if (self == NULL)
4841 return NULL;
4842 self->buf = buf;
4843 BUF_PYTHON_REF(buf) = self;
4844 }
4845
4846 return (PyObject *)(self);
4847}
4848
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004849 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004850BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004851{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004852 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4853 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004854
4855 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004856}
4857
Bram Moolenaar971db462013-05-12 18:44:48 +02004858 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004859BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004860{
4861 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004862 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004863 return -1; /* ??? */
4864
Bram Moolenaard6e39182013-05-21 18:30:34 +02004865 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004866}
4867
4868 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004869BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004870{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004871 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004872}
4873
4874 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004875BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004876{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004877 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004878}
4879
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004880static char *BufferAttrs[] = {
4881 "name", "number", "vars", "options", "valid",
4882 NULL
4883};
4884
4885 static PyObject *
4886BufferDir(PyObject *self)
4887{
4888 return ObjectDir(self, BufferAttrs);
4889}
4890
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004891 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004892BufferAttrValid(BufferObject *self, char *name)
4893{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004894 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004895
4896 if (strcmp(name, "valid") != 0)
4897 return NULL;
4898
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004899 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4900 Py_INCREF(ret);
4901 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004902}
4903
4904 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004905BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004906{
4907 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004908 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004909 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004910 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004911 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004912 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004913 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004914 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004915 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4916 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004917 else if (strcmp(name, "__members__") == 0)
4918 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004919 else
4920 return NULL;
4921}
4922
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004923 static int
4924BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4925{
4926 if (CheckBuffer(self))
4927 return -1;
4928
4929 if (strcmp(name, "name") == 0)
4930 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004931 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004932 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004933 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004934 PyObject *todecref;
4935
4936 if (!(val = StringToChars(valObject, &todecref)))
4937 return -1;
4938
4939 VimTryStart();
4940 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4941 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004942 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004943 aucmd_restbuf(&aco);
4944 Py_XDECREF(todecref);
4945 if (VimTryEnd())
4946 return -1;
4947
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004948 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004949 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004950 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004951 return -1;
4952 }
4953 return 0;
4954 }
4955 else
4956 {
4957 PyErr_SetString(PyExc_AttributeError, name);
4958 return -1;
4959 }
4960}
4961
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004962 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004963BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004964{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004965 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004966}
4967
4968 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004969BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004970{
4971 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004972 char_u *pmark;
4973 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004974 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004975 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004976
Bram Moolenaard6e39182013-05-21 18:30:34 +02004977 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004978 return NULL;
4979
Bram Moolenaar389a1792013-06-23 13:00:44 +02004980 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004981 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004982
Bram Moolenaar389a1792013-06-23 13:00:44 +02004983 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004984 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004985 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004986 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004987 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004988 return NULL;
4989 }
4990
4991 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004992
4993 Py_XDECREF(todecref);
4994
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004995 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004996 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004997 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004998 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004999 if (VimTryEnd())
5000 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005001
5002 if (posp == NULL)
5003 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005004 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005005 return NULL;
5006 }
5007
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005008 if (posp->lnum <= 0)
5009 {
5010 /* Or raise an error? */
5011 Py_INCREF(Py_None);
5012 return Py_None;
5013 }
5014
5015 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5016}
5017
5018 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005019BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005020{
5021 PyInt start;
5022 PyInt end;
5023
Bram Moolenaard6e39182013-05-21 18:30:34 +02005024 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005025 return NULL;
5026
5027 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5028 return NULL;
5029
Bram Moolenaard6e39182013-05-21 18:30:34 +02005030 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005031}
5032
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005033 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005034BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005035{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005036 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005037 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005038 else
5039 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005040 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005041
5042 if (name == NULL)
5043 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005044
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005045 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005046 }
5047}
5048
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005049static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005050 /* name, function, calling, documentation */
5051 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005052 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005053 {"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 +02005054 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5055 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005056};
5057
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005058/*
5059 * Buffer list object - Implementation
5060 */
5061
5062static PyTypeObject BufMapType;
5063
5064typedef struct
5065{
5066 PyObject_HEAD
5067} BufMapObject;
5068
5069 static PyInt
5070BufMapLength(PyObject *self UNUSED)
5071{
5072 buf_T *b = firstbuf;
5073 PyInt n = 0;
5074
5075 while (b)
5076 {
5077 ++n;
5078 b = b->b_next;
5079 }
5080
5081 return n;
5082}
5083
5084 static PyObject *
5085BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5086{
5087 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005088 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005089
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005090 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005091 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005092
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005093 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005094
5095 if (b)
5096 return BufferNew(b);
5097 else
5098 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005099 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005100 return NULL;
5101 }
5102}
5103
5104 static void
5105BufMapIterDestruct(PyObject *buffer)
5106{
5107 /* Iteration was stopped before all buffers were processed */
5108 if (buffer)
5109 {
5110 Py_DECREF(buffer);
5111 }
5112}
5113
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005114 static int
5115BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5116{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005117 if (buffer)
5118 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005119 return 0;
5120}
5121
5122 static int
5123BufMapIterClear(PyObject **buffer)
5124{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005125 if (*buffer)
5126 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005127 return 0;
5128}
5129
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005130 static PyObject *
5131BufMapIterNext(PyObject **buffer)
5132{
5133 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005134 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005135
5136 if (!*buffer)
5137 return NULL;
5138
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005139 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005140
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005141 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005142 {
5143 *buffer = NULL;
5144 return NULL;
5145 }
5146
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005147 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005148 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005149 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005150 return NULL;
5151 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005152 /* Do not increment reference: we no longer hold it (decref), but whoever
5153 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005154 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005155}
5156
5157 static PyObject *
5158BufMapIter(PyObject *self UNUSED)
5159{
5160 PyObject *buffer;
5161
5162 buffer = BufferNew(firstbuf);
5163 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005164 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5165 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005166}
5167
5168static PyMappingMethods BufMapAsMapping = {
5169 (lenfunc) BufMapLength,
5170 (binaryfunc) BufMapItem,
5171 (objobjargproc) 0,
5172};
5173
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005174/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005175 */
5176
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005177static char *CurrentAttrs[] = {
5178 "buffer", "window", "line", "range", "tabpage",
5179 NULL
5180};
5181
5182 static PyObject *
5183CurrentDir(PyObject *self)
5184{
5185 return ObjectDir(self, CurrentAttrs);
5186}
5187
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005188 static PyObject *
5189CurrentGetattr(PyObject *self UNUSED, char *name)
5190{
5191 if (strcmp(name, "buffer") == 0)
5192 return (PyObject *)BufferNew(curbuf);
5193 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005194 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005195 else if (strcmp(name, "tabpage") == 0)
5196 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005197 else if (strcmp(name, "line") == 0)
5198 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5199 else if (strcmp(name, "range") == 0)
5200 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005201 else if (strcmp(name, "__members__") == 0)
5202 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005203 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005204#if PY_MAJOR_VERSION < 3
5205 return Py_FindMethod(WindowMethods, self, name);
5206#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005207 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005208#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005209}
5210
5211 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005212CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005213{
5214 if (strcmp(name, "line") == 0)
5215 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005216 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5217 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005218 return -1;
5219
5220 return 0;
5221 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005222 else if (strcmp(name, "buffer") == 0)
5223 {
5224 int count;
5225
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005226 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005227 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005228 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005229 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005230 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005231 return -1;
5232 }
5233
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005234 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005235 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005236 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005237
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005238 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005239 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5240 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005241 if (VimTryEnd())
5242 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005243 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005244 return -1;
5245 }
5246
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005247 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005248 }
5249 else if (strcmp(name, "window") == 0)
5250 {
5251 int count;
5252
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005253 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005254 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005255 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005256 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005257 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005258 return -1;
5259 }
5260
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005261 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005262 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005263 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005264
5265 if (!count)
5266 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005267 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005268 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005269 return -1;
5270 }
5271
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005272 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005273 win_goto(((WindowObject *)(valObject))->win);
5274 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005275 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005276 if (VimTryEnd())
5277 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005278 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005279 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005280 return -1;
5281 }
5282
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005283 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005284 }
5285 else if (strcmp(name, "tabpage") == 0)
5286 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005287 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005288 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005289 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005290 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005291 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005292 return -1;
5293 }
5294
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005295 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005296 return -1;
5297
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005298 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005299 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5300 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005301 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005302 if (VimTryEnd())
5303 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005304 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005305 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005306 return -1;
5307 }
5308
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005309 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005310 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005311 else
5312 {
5313 PyErr_SetString(PyExc_AttributeError, name);
5314 return -1;
5315 }
5316}
5317
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005318static struct PyMethodDef CurrentMethods[] = {
5319 /* name, function, calling, documentation */
5320 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5321 { NULL, NULL, 0, NULL}
5322};
5323
Bram Moolenaardb913952012-06-29 12:54:53 +02005324 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005325init_range_cmd(exarg_T *eap)
5326{
5327 RangeStart = eap->line1;
5328 RangeEnd = eap->line2;
5329}
5330
5331 static void
5332init_range_eval(typval_T *rettv UNUSED)
5333{
5334 RangeStart = (PyInt) curwin->w_cursor.lnum;
5335 RangeEnd = RangeStart;
5336}
5337
5338 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005339run_cmd(const char *cmd, void *arg UNUSED
5340#ifdef PY_CAN_RECURSE
5341 , PyGILState_STATE *pygilstate UNUSED
5342#endif
5343 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005344{
Bram Moolenaar41009372013-07-01 22:03:04 +02005345 PyObject *run_ret;
5346 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5347 if (run_ret != NULL)
5348 {
5349 Py_DECREF(run_ret);
5350 }
5351 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5352 {
5353 EMSG2(_(e_py_systemexit), "python");
5354 PyErr_Clear();
5355 }
5356 else
5357 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005358}
5359
5360static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5361static int code_hdr_len = 30;
5362
5363 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005364run_do(const char *cmd, void *arg UNUSED
5365#ifdef PY_CAN_RECURSE
5366 , PyGILState_STATE *pygilstate
5367#endif
5368 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005369{
5370 PyInt lnum;
5371 size_t len;
5372 char *code;
5373 int status;
5374 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005375 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005376
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005377 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005378 {
5379 EMSG(_("cannot save undo information"));
5380 return;
5381 }
5382
5383 len = code_hdr_len + STRLEN(cmd);
5384 code = PyMem_New(char, len + 1);
5385 memcpy(code, code_hdr, code_hdr_len);
5386 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005387 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5388 status = -1;
5389 if (run_ret != NULL)
5390 {
5391 status = 0;
5392 Py_DECREF(run_ret);
5393 }
5394 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5395 {
5396 PyMem_Free(code);
5397 EMSG2(_(e_py_systemexit), "python");
5398 PyErr_Clear();
5399 return;
5400 }
5401 else
5402 PyErr_PrintEx(1);
5403
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005404 PyMem_Free(code);
5405
5406 if (status)
5407 {
5408 EMSG(_("failed to run the code"));
5409 return;
5410 }
5411
5412 status = 0;
5413 pymain = PyImport_AddModule("__main__");
5414 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005415#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005416 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005417#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005418
5419 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5420 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005421 PyObject *line;
5422 PyObject *linenr;
5423 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005424
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005425#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005426 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005427#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005428 if (!(line = GetBufferLine(curbuf, lnum)))
5429 goto err;
5430 if (!(linenr = PyInt_FromLong((long) lnum)))
5431 {
5432 Py_DECREF(line);
5433 goto err;
5434 }
5435 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5436 Py_DECREF(line);
5437 Py_DECREF(linenr);
5438 if (!ret)
5439 goto err;
5440
5441 if (ret != Py_None)
5442 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5443 goto err;
5444
5445 Py_XDECREF(ret);
5446 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005447#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005448 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005449#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005450 }
5451 goto out;
5452err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005453#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005454 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005455#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005456 PyErr_PrintEx(0);
5457 PythonIO_Flush();
5458 status = 1;
5459out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005460#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005461 if (!status)
5462 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005463#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005464 Py_DECREF(pyfunc);
5465 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5466 if (status)
5467 return;
5468 check_cursor();
5469 update_curbuf(NOT_VALID);
5470}
5471
5472 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005473run_eval(const char *cmd, typval_T *rettv
5474#ifdef PY_CAN_RECURSE
5475 , PyGILState_STATE *pygilstate UNUSED
5476#endif
5477 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005478{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005479 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005480
Bram Moolenaar41009372013-07-01 22:03:04 +02005481 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005482 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005483 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005484 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005485 {
5486 EMSG2(_(e_py_systemexit), "python");
5487 PyErr_Clear();
5488 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005489 else
5490 {
5491 if (PyErr_Occurred() && !msg_silent)
5492 PyErr_PrintEx(0);
5493 EMSG(_("E858: Eval did not return a valid python object"));
5494 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005495 }
5496 else
5497 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005498 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005499 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005500 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005501 }
5502 PyErr_Clear();
5503}
5504
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005505 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005506set_ref_in_py(const int copyID)
5507{
5508 pylinkedlist_T *cur;
5509 dict_T *dd;
5510 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005511 int abort = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +02005512
5513 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005514 {
5515 for(cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005516 {
5517 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5518 if (dd->dv_copyID != copyID)
5519 {
5520 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005521 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005522 }
5523 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005524 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005525
5526 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005527 {
5528 for(cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005529 {
5530 ll = ((ListObject *) (cur->pll_obj))->list;
5531 if (ll->lv_copyID != copyID)
5532 {
5533 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005534 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005535 }
5536 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005537 }
5538
5539 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005540}
5541
5542 static int
5543set_string_copy(char_u *str, typval_T *tv)
5544{
5545 tv->vval.v_string = vim_strsave(str);
5546 if (tv->vval.v_string == NULL)
5547 {
5548 PyErr_NoMemory();
5549 return -1;
5550 }
5551 return 0;
5552}
5553
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005554 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005555pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005556{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005557 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005558 char_u *key;
5559 dictitem_T *di;
5560 PyObject *keyObject;
5561 PyObject *valObject;
5562 Py_ssize_t iter = 0;
5563
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005564 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005565 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005566
5567 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005568 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005569
5570 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5571 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005572 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005573
Bram Moolenaara03e6312013-05-29 22:49:26 +02005574 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005575 {
5576 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005577 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005578 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005579
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005580 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005581 {
5582 dict_unref(dict);
5583 return -1;
5584 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005585
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005586 if (*key == NUL)
5587 {
5588 dict_unref(dict);
5589 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005590 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005591 return -1;
5592 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005593
5594 di = dictitem_alloc(key);
5595
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005596 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005597
5598 if (di == NULL)
5599 {
5600 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005601 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005602 return -1;
5603 }
5604 di->di_tv.v_lock = 0;
5605
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005606 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005607 {
5608 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005609 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005610 return -1;
5611 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005612
5613 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005614 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005615 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005616 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005617 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005618 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005619 return -1;
5620 }
5621 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005622
5623 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005624 return 0;
5625}
5626
5627 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005628pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005629{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005630 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005631 char_u *key;
5632 dictitem_T *di;
5633 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005634 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005635 PyObject *keyObject;
5636 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005637
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005638 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005639 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005640
5641 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005642 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005643
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005644 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005645 {
5646 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005647 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005648 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005649
5650 if (!(iterator = PyObject_GetIter(list)))
5651 {
5652 dict_unref(dict);
5653 Py_DECREF(list);
5654 return -1;
5655 }
5656 Py_DECREF(list);
5657
5658 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005659 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005660 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005661
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005662 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005663 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005664 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005665 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005666 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005667 return -1;
5668 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005669
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005670 if (*key == NUL)
5671 {
5672 Py_DECREF(keyObject);
5673 Py_DECREF(iterator);
5674 Py_XDECREF(todecref);
5675 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005676 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005677 return -1;
5678 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005679
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005680 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005681 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005682 Py_DECREF(keyObject);
5683 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005684 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005685 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005686 return -1;
5687 }
5688
5689 di = dictitem_alloc(key);
5690
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005691 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005692 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005693
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005694 if (di == NULL)
5695 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005696 Py_DECREF(iterator);
5697 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005698 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005699 PyErr_NoMemory();
5700 return -1;
5701 }
5702 di->di_tv.v_lock = 0;
5703
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005704 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005705 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005706 Py_DECREF(iterator);
5707 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005708 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005709 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005710 return -1;
5711 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005712
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005713 Py_DECREF(valObject);
5714
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005715 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005716 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005717 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005718 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005719 dictitem_free(di);
5720 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005721 return -1;
5722 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005723 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005724 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005725 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005726 return 0;
5727}
5728
5729 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005730pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005731{
5732 list_T *l;
5733
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005734 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005735 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005736
5737 tv->v_type = VAR_LIST;
5738 tv->vval.v_list = l;
5739
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005740 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005741 {
5742 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005743 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005744 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005745
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005746 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005747 return 0;
5748}
5749
Bram Moolenaardb913952012-06-29 12:54:53 +02005750typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5751
5752 static int
5753convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005754 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005755{
5756 PyObject *capsule;
5757 char hexBuf[sizeof(void *) * 2 + 3];
5758
5759 sprintf(hexBuf, "%p", obj);
5760
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005761# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005762 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005763# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005764 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005765# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005766 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005767 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005768# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005769 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005770# else
5771 capsule = PyCObject_FromVoidPtr(tv, NULL);
5772# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005773 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5774 {
5775 Py_DECREF(capsule);
5776 tv->v_type = VAR_UNKNOWN;
5777 return -1;
5778 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005779
5780 Py_DECREF(capsule);
5781
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005782 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005783 {
5784 tv->v_type = VAR_UNKNOWN;
5785 return -1;
5786 }
5787 /* As we are not using copy_tv which increments reference count we must
5788 * do it ourself. */
5789 switch(tv->v_type)
5790 {
5791 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5792 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5793 }
5794 }
5795 else
5796 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005797 typval_T *v;
5798
5799# ifdef PY_USE_CAPSULE
5800 v = PyCapsule_GetPointer(capsule, NULL);
5801# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005802 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005803# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005804 copy_tv(v, tv);
5805 }
5806 return 0;
5807}
5808
5809 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005810ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5811{
5812 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005813 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005814
5815 if (!(lookup_dict = PyDict_New()))
5816 return -1;
5817
5818 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5819 {
5820 tv->v_type = VAR_DICT;
5821 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5822 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005823 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005824 }
5825 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005826 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005827 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005828 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005829 else
5830 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005831 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005832 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005833 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005834 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005835 }
5836 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005837 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005838}
5839
5840 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005841ConvertFromPyObject(PyObject *obj, typval_T *tv)
5842{
5843 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005844 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005845
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005846 if (!(lookup_dict = PyDict_New()))
5847 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005848 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005849 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005850 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005851}
5852
5853 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005854_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005855{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005856 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005857 {
5858 tv->v_type = VAR_DICT;
5859 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5860 ++tv->vval.v_dict->dv_refcount;
5861 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005862 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005863 {
5864 tv->v_type = VAR_LIST;
5865 tv->vval.v_list = (((ListObject *)(obj))->list);
5866 ++tv->vval.v_list->lv_refcount;
5867 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005868 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005869 {
5870 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5871 return -1;
5872
5873 tv->v_type = VAR_FUNC;
5874 func_ref(tv->vval.v_string);
5875 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005876 else if (PyBytes_Check(obj))
5877 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005878 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005879
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005880 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005881 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005882 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005883 return -1;
5884
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005885 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005886 return -1;
5887
5888 tv->v_type = VAR_STRING;
5889 }
5890 else if (PyUnicode_Check(obj))
5891 {
5892 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005893 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005894
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005895 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005896 if (bytes == NULL)
5897 return -1;
5898
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005899 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005900 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005901 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005902 return -1;
5903
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005904 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005905 {
5906 Py_XDECREF(bytes);
5907 return -1;
5908 }
5909 Py_XDECREF(bytes);
5910
5911 tv->v_type = VAR_STRING;
5912 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005913#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005914 else if (PyInt_Check(obj))
5915 {
5916 tv->v_type = VAR_NUMBER;
5917 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005918 if (PyErr_Occurred())
5919 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005920 }
5921#endif
5922 else if (PyLong_Check(obj))
5923 {
5924 tv->v_type = VAR_NUMBER;
5925 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005926 if (PyErr_Occurred())
5927 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005928 }
5929 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005930 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005931#ifdef FEAT_FLOAT
5932 else if (PyFloat_Check(obj))
5933 {
5934 tv->v_type = VAR_FLOAT;
5935 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5936 }
5937#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005938 else if (PyObject_HasAttrString(obj, "keys"))
5939 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005940 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005941 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005942 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005943 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005944 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005945 else if (PyNumber_Check(obj))
5946 {
5947 PyObject *num;
5948
5949 if (!(num = PyNumber_Long(obj)))
5950 return -1;
5951
5952 tv->v_type = VAR_NUMBER;
5953 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5954
5955 Py_DECREF(num);
5956 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005957 else
5958 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005959 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005960 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005961 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005962 return -1;
5963 }
5964 return 0;
5965}
5966
5967 static PyObject *
5968ConvertToPyObject(typval_T *tv)
5969{
5970 if (tv == NULL)
5971 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005972 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005973 return NULL;
5974 }
5975 switch (tv->v_type)
5976 {
5977 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005978 return PyBytes_FromString(tv->vval.v_string == NULL
5979 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005980 case VAR_NUMBER:
5981 return PyLong_FromLong((long) tv->vval.v_number);
5982#ifdef FEAT_FLOAT
5983 case VAR_FLOAT:
5984 return PyFloat_FromDouble((double) tv->vval.v_float);
5985#endif
5986 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005987 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005988 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005989 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005990 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005991 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005992 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005993 case VAR_UNKNOWN:
5994 Py_INCREF(Py_None);
5995 return Py_None;
5996 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005997 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005998 return NULL;
5999 }
6000}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006001
6002typedef struct
6003{
6004 PyObject_HEAD
6005} CurrentObject;
6006static PyTypeObject CurrentType;
6007
6008 static void
6009init_structs(void)
6010{
6011 vim_memset(&OutputType, 0, sizeof(OutputType));
6012 OutputType.tp_name = "vim.message";
6013 OutputType.tp_basicsize = sizeof(OutputObject);
6014 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6015 OutputType.tp_doc = "vim message object";
6016 OutputType.tp_methods = OutputMethods;
6017#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006018 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6019 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006020 OutputType.tp_alloc = call_PyType_GenericAlloc;
6021 OutputType.tp_new = call_PyType_GenericNew;
6022 OutputType.tp_free = call_PyObject_Free;
6023#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006024 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6025 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006026#endif
6027
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006028 vim_memset(&IterType, 0, sizeof(IterType));
6029 IterType.tp_name = "vim.iter";
6030 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006031 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006032 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006033 IterType.tp_iter = (getiterfunc)IterIter;
6034 IterType.tp_iternext = (iternextfunc)IterNext;
6035 IterType.tp_dealloc = (destructor)IterDestructor;
6036 IterType.tp_traverse = (traverseproc)IterTraverse;
6037 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006038
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006039 vim_memset(&BufferType, 0, sizeof(BufferType));
6040 BufferType.tp_name = "vim.buffer";
6041 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006042 BufferType.tp_dealloc = (destructor)BufferDestructor;
6043 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006044 BufferType.tp_as_sequence = &BufferAsSeq;
6045 BufferType.tp_as_mapping = &BufferAsMapping;
6046 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6047 BufferType.tp_doc = "vim buffer object";
6048 BufferType.tp_methods = BufferMethods;
6049#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006050 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006051 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006052 BufferType.tp_alloc = call_PyType_GenericAlloc;
6053 BufferType.tp_new = call_PyType_GenericNew;
6054 BufferType.tp_free = call_PyObject_Free;
6055#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006056 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006057 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006058#endif
6059
6060 vim_memset(&WindowType, 0, sizeof(WindowType));
6061 WindowType.tp_name = "vim.window";
6062 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006063 WindowType.tp_dealloc = (destructor)WindowDestructor;
6064 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006065 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006066 WindowType.tp_doc = "vim Window object";
6067 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006068 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6069 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006070#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006071 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6072 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006073 WindowType.tp_alloc = call_PyType_GenericAlloc;
6074 WindowType.tp_new = call_PyType_GenericNew;
6075 WindowType.tp_free = call_PyObject_Free;
6076#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006077 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6078 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006079#endif
6080
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006081 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6082 TabPageType.tp_name = "vim.tabpage";
6083 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006084 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6085 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006086 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6087 TabPageType.tp_doc = "vim tab page object";
6088 TabPageType.tp_methods = TabPageMethods;
6089#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006090 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006091 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6092 TabPageType.tp_new = call_PyType_GenericNew;
6093 TabPageType.tp_free = call_PyObject_Free;
6094#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006095 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006096#endif
6097
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006098 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6099 BufMapType.tp_name = "vim.bufferlist";
6100 BufMapType.tp_basicsize = sizeof(BufMapObject);
6101 BufMapType.tp_as_mapping = &BufMapAsMapping;
6102 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006103 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006104 BufferType.tp_doc = "vim buffer list";
6105
6106 vim_memset(&WinListType, 0, sizeof(WinListType));
6107 WinListType.tp_name = "vim.windowlist";
6108 WinListType.tp_basicsize = sizeof(WinListType);
6109 WinListType.tp_as_sequence = &WinListAsSeq;
6110 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6111 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006112 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006113
6114 vim_memset(&TabListType, 0, sizeof(TabListType));
6115 TabListType.tp_name = "vim.tabpagelist";
6116 TabListType.tp_basicsize = sizeof(TabListType);
6117 TabListType.tp_as_sequence = &TabListAsSeq;
6118 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6119 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006120
6121 vim_memset(&RangeType, 0, sizeof(RangeType));
6122 RangeType.tp_name = "vim.range";
6123 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006124 RangeType.tp_dealloc = (destructor)RangeDestructor;
6125 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006126 RangeType.tp_as_sequence = &RangeAsSeq;
6127 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006128 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006129 RangeType.tp_doc = "vim Range object";
6130 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006131 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6132 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006133#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006134 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006135 RangeType.tp_alloc = call_PyType_GenericAlloc;
6136 RangeType.tp_new = call_PyType_GenericNew;
6137 RangeType.tp_free = call_PyObject_Free;
6138#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006139 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006140#endif
6141
6142 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6143 CurrentType.tp_name = "vim.currentdata";
6144 CurrentType.tp_basicsize = sizeof(CurrentObject);
6145 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6146 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006147 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006148#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006149 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6150 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006151#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006152 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6153 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006154#endif
6155
6156 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6157 DictionaryType.tp_name = "vim.dictionary";
6158 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006159 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006160 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006161 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006162 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006163 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6164 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006165 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6166 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6167 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006168#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006169 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6170 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006171#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006172 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6173 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006174#endif
6175
6176 vim_memset(&ListType, 0, sizeof(ListType));
6177 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006178 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006179 ListType.tp_basicsize = sizeof(ListObject);
6180 ListType.tp_as_sequence = &ListAsSeq;
6181 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006182 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006183 ListType.tp_doc = "list pushing modifications to vim structure";
6184 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006185 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006186 ListType.tp_new = (newfunc)ListConstructor;
6187 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006188#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006189 ListType.tp_getattro = (getattrofunc)ListGetattro;
6190 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006191#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006192 ListType.tp_getattr = (getattrfunc)ListGetattr;
6193 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006194#endif
6195
6196 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006197 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006198 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006199 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6200 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006201 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006202 FunctionType.tp_doc = "object that calls vim function";
6203 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006204 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006205 FunctionType.tp_new = (newfunc)FunctionConstructor;
6206 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006207#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006208 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006209#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006210 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006211#endif
6212
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006213 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6214 OptionsType.tp_name = "vim.options";
6215 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006216 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006217 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006218 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006219 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006220 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006221 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6222 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6223 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006224
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006225 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6226 LoaderType.tp_name = "vim.Loader";
6227 LoaderType.tp_basicsize = sizeof(LoaderObject);
6228 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6229 LoaderType.tp_doc = "vim message object";
6230 LoaderType.tp_methods = LoaderMethods;
6231 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6232
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006233#if PY_MAJOR_VERSION >= 3
6234 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6235 vimmodule.m_name = "vim";
6236 vimmodule.m_doc = "Vim Python interface\n";
6237 vimmodule.m_size = -1;
6238 vimmodule.m_methods = VimMethods;
6239#endif
6240}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006241
6242#define PYTYPE_READY(type) \
6243 if (PyType_Ready(&type)) \
6244 return -1;
6245
6246 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006247init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006248{
6249 PYTYPE_READY(IterType);
6250 PYTYPE_READY(BufferType);
6251 PYTYPE_READY(RangeType);
6252 PYTYPE_READY(WindowType);
6253 PYTYPE_READY(TabPageType);
6254 PYTYPE_READY(BufMapType);
6255 PYTYPE_READY(WinListType);
6256 PYTYPE_READY(TabListType);
6257 PYTYPE_READY(CurrentType);
6258 PYTYPE_READY(DictionaryType);
6259 PYTYPE_READY(ListType);
6260 PYTYPE_READY(FunctionType);
6261 PYTYPE_READY(OptionsType);
6262 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006263 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006264 return 0;
6265}
6266
6267 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006268init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006269{
6270 PyObject *path;
6271 PyObject *path_hook;
6272 PyObject *path_hooks;
6273
6274 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6275 return -1;
6276
6277 if (!(path_hooks = PySys_GetObject("path_hooks")))
6278 {
6279 PyErr_Clear();
6280 path_hooks = PyList_New(1);
6281 PyList_SET_ITEM(path_hooks, 0, path_hook);
6282 if (PySys_SetObject("path_hooks", path_hooks))
6283 {
6284 Py_DECREF(path_hooks);
6285 return -1;
6286 }
6287 Py_DECREF(path_hooks);
6288 }
6289 else if (PyList_Check(path_hooks))
6290 {
6291 if (PyList_Append(path_hooks, path_hook))
6292 {
6293 Py_DECREF(path_hook);
6294 return -1;
6295 }
6296 Py_DECREF(path_hook);
6297 }
6298 else
6299 {
6300 VimTryStart();
6301 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6302 "You should now do the following:\n"
6303 "- append vim.path_hook to sys.path_hooks\n"
6304 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6305 VimTryEnd(); /* Discard the error */
6306 Py_DECREF(path_hook);
6307 return 0;
6308 }
6309
6310 if (!(path = PySys_GetObject("path")))
6311 {
6312 PyErr_Clear();
6313 path = PyList_New(1);
6314 Py_INCREF(vim_special_path_object);
6315 PyList_SET_ITEM(path, 0, vim_special_path_object);
6316 if (PySys_SetObject("path", path))
6317 {
6318 Py_DECREF(path);
6319 return -1;
6320 }
6321 Py_DECREF(path);
6322 }
6323 else if (PyList_Check(path))
6324 {
6325 if (PyList_Append(path, vim_special_path_object))
6326 return -1;
6327 }
6328 else
6329 {
6330 VimTryStart();
6331 EMSG(_("Failed to set path: sys.path is not a list\n"
6332 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6333 VimTryEnd(); /* Discard the error */
6334 }
6335
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006336 return 0;
6337}
6338
6339static BufMapObject TheBufferMap =
6340{
6341 PyObject_HEAD_INIT(&BufMapType)
6342};
6343
6344static WinListObject TheWindowList =
6345{
6346 PyObject_HEAD_INIT(&WinListType)
6347 NULL
6348};
6349
6350static CurrentObject TheCurrent =
6351{
6352 PyObject_HEAD_INIT(&CurrentType)
6353};
6354
6355static TabListObject TheTabPageList =
6356{
6357 PyObject_HEAD_INIT(&TabListType)
6358};
6359
6360static struct numeric_constant {
6361 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006362 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006363} numeric_constants[] = {
6364 {"VAR_LOCKED", VAR_LOCKED},
6365 {"VAR_FIXED", VAR_FIXED},
6366 {"VAR_SCOPE", VAR_SCOPE},
6367 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6368};
6369
6370static struct object_constant {
6371 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006372 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006373} object_constants[] = {
6374 {"buffers", (PyObject *)(void *)&TheBufferMap},
6375 {"windows", (PyObject *)(void *)&TheWindowList},
6376 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6377 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006378
6379 {"Buffer", (PyObject *)&BufferType},
6380 {"Range", (PyObject *)&RangeType},
6381 {"Window", (PyObject *)&WindowType},
6382 {"TabPage", (PyObject *)&TabPageType},
6383 {"Dictionary", (PyObject *)&DictionaryType},
6384 {"List", (PyObject *)&ListType},
6385 {"Function", (PyObject *)&FunctionType},
6386 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006387 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006388};
6389
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006390#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006391 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006392 return -1;
6393
6394#define ADD_CHECKED_OBJECT(m, name, obj) \
6395 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006396 PyObject *valObject = obj; \
6397 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006398 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006399 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006400 }
6401
6402 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006403populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006404{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006405 int i;
6406 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006407 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006408 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006409
6410 for (i = 0; i < (int)(sizeof(numeric_constants)
6411 / sizeof(struct numeric_constant));
6412 ++i)
6413 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006414 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006415
6416 for (i = 0; i < (int)(sizeof(object_constants)
6417 / sizeof(struct object_constant));
6418 ++i)
6419 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006420 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006421
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006422 valObject = object_constants[i].valObject;
6423 Py_INCREF(valObject);
6424 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006425 }
6426
6427 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6428 return -1;
6429 ADD_OBJECT(m, "error", VimError);
6430
Bram Moolenaara9922d62013-05-30 13:01:18 +02006431 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6432 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006433 ADD_CHECKED_OBJECT(m, "options",
6434 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006435
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006436 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006437 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006438 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006439
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006440 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006441 return -1;
6442 ADD_OBJECT(m, "_getcwd", py_getcwd)
6443
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006444 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006445 return -1;
6446 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006447 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006448 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006449 if (PyObject_SetAttrString(other_module, "chdir", attr))
6450 {
6451 Py_DECREF(attr);
6452 return -1;
6453 }
6454 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006455
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006456 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006457 {
6458 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006459 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006460 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006461 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6462 {
6463 Py_DECREF(attr);
6464 return -1;
6465 }
6466 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006467 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006468 else
6469 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006470
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006471 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6472 return -1;
6473
6474 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6475
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006476 if (!(imp = PyImport_ImportModule("imp")))
6477 return -1;
6478
6479 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6480 {
6481 Py_DECREF(imp);
6482 return -1;
6483 }
6484
6485 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6486 {
6487 Py_DECREF(py_find_module);
6488 Py_DECREF(imp);
6489 return -1;
6490 }
6491
6492 Py_DECREF(imp);
6493
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006494 ADD_OBJECT(m, "_find_module", py_find_module);
6495 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006496
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006497 return 0;
6498}