blob: 5044afbeff30df747480a94037f0425c99f8396f [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 Moolenaara7b64ce2013-05-21 20:40:40 +02003175 if (VimTryEnd())
3176 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003177 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003178 return -1;
3179 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003180 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3181 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003182 break;
3183 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003184 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003185 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003186 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003187 break;
3188 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003189 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003190 break;
3191 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003192 if (set_ret == FAIL)
3193 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003194 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003195}
3196
3197 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003198OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003199{
3200 char_u *key;
3201 int flags;
3202 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003203 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003204 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003205
Bram Moolenaard6e39182013-05-21 18:30:34 +02003206 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003207 return -1;
3208
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003209 if (!(key = StringToChars(keyObject, &todecref)))
3210 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003211
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003212 if (*key == NUL)
3213 {
3214 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003215 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003216 return -1;
3217 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003218
3219 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003220 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003221
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003222 if (flags == 0)
3223 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003224 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003225 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003226 return -1;
3227 }
3228
3229 if (valObject == NULL)
3230 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003231 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003232 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003233 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003234 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003235 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003236 return -1;
3237 }
3238 else if (!(flags & SOPT_GLOBAL))
3239 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003240 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003241 N_("unable to unset option %s "
3242 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003243 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003244 return -1;
3245 }
3246 else
3247 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003248 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003249 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003250 return 0;
3251 }
3252 }
3253
Bram Moolenaard6e39182013-05-21 18:30:34 +02003254 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003255
3256 if (flags & SOPT_BOOL)
3257 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003258 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003259
Bram Moolenaarb983f752013-05-15 16:11:50 +02003260 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003261 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003262 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003263 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003264 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003265 }
3266 else if (flags & SOPT_NUM)
3267 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003268 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003269
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003270 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003271 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003272 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003273 return -1;
3274 }
3275
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003276 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003277 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003278 }
3279 else
3280 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003281 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003282 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003283
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003284 if ((val = StringToChars(valObject, &todecref2)))
3285 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003286 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003287 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003288 Py_XDECREF(todecref2);
3289 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003290 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003291 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003292 }
3293
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003294 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003295
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003296 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003297}
3298
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003299static PySequenceMethods OptionsAsSeq = {
3300 0, /* sq_length */
3301 0, /* sq_concat */
3302 0, /* sq_repeat */
3303 0, /* sq_item */
3304 0, /* sq_slice */
3305 0, /* sq_ass_item */
3306 0, /* sq_ass_slice */
3307 (objobjproc) OptionsContains, /* sq_contains */
3308 0, /* sq_inplace_concat */
3309 0, /* sq_inplace_repeat */
3310};
3311
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003312static PyMappingMethods OptionsAsMapping = {
3313 (lenfunc) NULL,
3314 (binaryfunc) OptionsItem,
3315 (objobjargproc) OptionsAssItem,
3316};
3317
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003318/* Tabpage object
3319 */
3320
3321typedef struct
3322{
3323 PyObject_HEAD
3324 tabpage_T *tab;
3325} TabPageObject;
3326
3327static PyObject *WinListNew(TabPageObject *tabObject);
3328
3329static PyTypeObject TabPageType;
3330
3331 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003332CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003333{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003334 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003335 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003336 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003337 return -1;
3338 }
3339
3340 return 0;
3341}
3342
3343 static PyObject *
3344TabPageNew(tabpage_T *tab)
3345{
3346 TabPageObject *self;
3347
3348 if (TAB_PYTHON_REF(tab))
3349 {
3350 self = TAB_PYTHON_REF(tab);
3351 Py_INCREF(self);
3352 }
3353 else
3354 {
3355 self = PyObject_NEW(TabPageObject, &TabPageType);
3356 if (self == NULL)
3357 return NULL;
3358 self->tab = tab;
3359 TAB_PYTHON_REF(tab) = self;
3360 }
3361
3362 return (PyObject *)(self);
3363}
3364
3365 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003366TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003367{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003368 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3369 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003370
3371 DESTRUCTOR_FINISH(self);
3372}
3373
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003374static char *TabPageAttrs[] = {
3375 "windows", "number", "vars", "window", "valid",
3376 NULL
3377};
3378
3379 static PyObject *
3380TabPageDir(PyObject *self)
3381{
3382 return ObjectDir(self, TabPageAttrs);
3383}
3384
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003385 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003386TabPageAttrValid(TabPageObject *self, char *name)
3387{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003388 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003389
3390 if (strcmp(name, "valid") != 0)
3391 return NULL;
3392
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003393 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3394 Py_INCREF(ret);
3395 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003396}
3397
3398 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003399TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003400{
3401 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003402 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003403 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003404 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003405 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003406 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003407 else if (strcmp(name, "window") == 0)
3408 {
3409 /* For current tab window.c does not bother to set or update tp_curwin
3410 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003411 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003412 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003413 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003414 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003415 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003416 else if (strcmp(name, "__members__") == 0)
3417 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003418 return NULL;
3419}
3420
3421 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003422TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003423{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003424 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003425 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003426 else
3427 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003428 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003429
3430 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003431 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3432 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003433 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003434 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003435 }
3436}
3437
3438static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003439 /* name, function, calling, documentation */
3440 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3441 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003442};
3443
3444/*
3445 * Window list object
3446 */
3447
3448static PyTypeObject TabListType;
3449static PySequenceMethods TabListAsSeq;
3450
3451typedef struct
3452{
3453 PyObject_HEAD
3454} TabListObject;
3455
3456 static PyInt
3457TabListLength(PyObject *self UNUSED)
3458{
3459 tabpage_T *tp = first_tabpage;
3460 PyInt n = 0;
3461
3462 while (tp != NULL)
3463 {
3464 ++n;
3465 tp = tp->tp_next;
3466 }
3467
3468 return n;
3469}
3470
3471 static PyObject *
3472TabListItem(PyObject *self UNUSED, PyInt n)
3473{
3474 tabpage_T *tp;
3475
3476 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3477 if (n == 0)
3478 return TabPageNew(tp);
3479
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003480 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003481 return NULL;
3482}
3483
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003484/*
3485 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003486 */
3487
3488typedef struct
3489{
3490 PyObject_HEAD
3491 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003492 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003493} WindowObject;
3494
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003495static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003496
3497 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003498CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003499{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003500 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003501 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003502 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003503 return -1;
3504 }
3505
3506 return 0;
3507}
3508
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003509 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003510WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003511{
3512 /* We need to handle deletion of windows underneath us.
3513 * If we add a "w_python*_ref" field to the win_T structure,
3514 * then we can get at it in win_free() in vim. We then
3515 * need to create only ONE Python object per window - if
3516 * we try to create a second, just INCREF the existing one
3517 * and return it. The (single) Python object referring to
3518 * the window is stored in "w_python*_ref".
3519 * On a win_free() we set the Python object's win_T* field
3520 * to an invalid value. We trap all uses of a window
3521 * object, and reject them if the win_T* field is invalid.
3522 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003523 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003524 * w_python_ref and w_python3_ref fields respectively.
3525 */
3526
3527 WindowObject *self;
3528
3529 if (WIN_PYTHON_REF(win))
3530 {
3531 self = WIN_PYTHON_REF(win);
3532 Py_INCREF(self);
3533 }
3534 else
3535 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003536 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003537 if (self == NULL)
3538 return NULL;
3539 self->win = win;
3540 WIN_PYTHON_REF(win) = self;
3541 }
3542
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003543 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3544
Bram Moolenaar971db462013-05-12 18:44:48 +02003545 return (PyObject *)(self);
3546}
3547
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003548 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003549WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003550{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003551 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003552 if (self->win && self->win != INVALID_WINDOW_VALUE)
3553 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003554 Py_XDECREF(((PyObject *)(self->tabObject)));
3555 PyObject_GC_Del((void *)(self));
3556}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003557
Bram Moolenaar774267b2013-05-21 20:51:59 +02003558 static int
3559WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3560{
3561 Py_VISIT(((PyObject *)(self->tabObject)));
3562 return 0;
3563}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003564
Bram Moolenaar774267b2013-05-21 20:51:59 +02003565 static int
3566WindowClear(WindowObject *self)
3567{
3568 Py_CLEAR(self->tabObject);
3569 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003570}
3571
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003572 static win_T *
3573get_firstwin(TabPageObject *tabObject)
3574{
3575 if (tabObject)
3576 {
3577 if (CheckTabPage(tabObject))
3578 return NULL;
3579 /* For current tab window.c does not bother to set or update tp_firstwin
3580 */
3581 else if (tabObject->tab == curtab)
3582 return firstwin;
3583 else
3584 return tabObject->tab->tp_firstwin;
3585 }
3586 else
3587 return firstwin;
3588}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003589static char *WindowAttrs[] = {
3590 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3591 "tabpage", "valid",
3592 NULL
3593};
3594
3595 static PyObject *
3596WindowDir(PyObject *self)
3597{
3598 return ObjectDir(self, WindowAttrs);
3599}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003600
Bram Moolenaar971db462013-05-12 18:44:48 +02003601 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003602WindowAttrValid(WindowObject *self, char *name)
3603{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003604 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003605
3606 if (strcmp(name, "valid") != 0)
3607 return NULL;
3608
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003609 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3610 Py_INCREF(ret);
3611 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003612}
3613
3614 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003615WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003616{
3617 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003618 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003619 else if (strcmp(name, "cursor") == 0)
3620 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003621 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003622
3623 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3624 }
3625 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003626 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003627#ifdef FEAT_WINDOWS
3628 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003629 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003630#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003631#ifdef FEAT_VERTSPLIT
3632 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003633 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003634 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003635 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003636#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003637 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003638 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003639 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003640 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3641 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003642 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003643 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003644 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003645 return NULL;
3646 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003647 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003648 }
3649 else if (strcmp(name, "tabpage") == 0)
3650 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003651 Py_INCREF(self->tabObject);
3652 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003653 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003654 else if (strcmp(name, "__members__") == 0)
3655 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003656 else
3657 return NULL;
3658}
3659
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003660 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003661WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003662{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003663 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003664 return -1;
3665
3666 if (strcmp(name, "buffer") == 0)
3667 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003668 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003669 return -1;
3670 }
3671 else if (strcmp(name, "cursor") == 0)
3672 {
3673 long lnum;
3674 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003675
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003676 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003677 return -1;
3678
Bram Moolenaard6e39182013-05-21 18:30:34 +02003679 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003680 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003681 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003682 return -1;
3683 }
3684
3685 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003686 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003687 return -1;
3688
Bram Moolenaard6e39182013-05-21 18:30:34 +02003689 self->win->w_cursor.lnum = lnum;
3690 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003691#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003692 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003693#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003694 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003695 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003696
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003697 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003698 return 0;
3699 }
3700 else if (strcmp(name, "height") == 0)
3701 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003702 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003703 win_T *savewin;
3704
Bram Moolenaardee2e312013-06-23 16:35:47 +02003705 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003706 return -1;
3707
3708#ifdef FEAT_GUI
3709 need_mouse_correct = TRUE;
3710#endif
3711 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003712 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003713
3714 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003715 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003716 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003717 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003718 return -1;
3719
3720 return 0;
3721 }
3722#ifdef FEAT_VERTSPLIT
3723 else if (strcmp(name, "width") == 0)
3724 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003725 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003726 win_T *savewin;
3727
Bram Moolenaardee2e312013-06-23 16:35:47 +02003728 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003729 return -1;
3730
3731#ifdef FEAT_GUI
3732 need_mouse_correct = TRUE;
3733#endif
3734 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003735 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003736
3737 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003738 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003739 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003740 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003741 return -1;
3742
3743 return 0;
3744 }
3745#endif
3746 else
3747 {
3748 PyErr_SetString(PyExc_AttributeError, name);
3749 return -1;
3750 }
3751}
3752
3753 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003754WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003755{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003756 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003757 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003758 else
3759 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003760 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003761
Bram Moolenaar6d216452013-05-12 19:00:41 +02003762 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003763 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003764 (self));
3765 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003766 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003767 }
3768}
3769
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003770static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003771 /* name, function, calling, documentation */
3772 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3773 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003774};
3775
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003776/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003777 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003778 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003779
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003780static PyTypeObject WinListType;
3781static PySequenceMethods WinListAsSeq;
3782
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003783typedef struct
3784{
3785 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003786 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003787} WinListObject;
3788
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003789 static PyObject *
3790WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003791{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003792 WinListObject *self;
3793
3794 self = PyObject_NEW(WinListObject, &WinListType);
3795 self->tabObject = tabObject;
3796 Py_INCREF(tabObject);
3797
3798 return (PyObject *)(self);
3799}
3800
3801 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003802WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003803{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003804 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003805
3806 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003807 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003808 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003809 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003810
3811 DESTRUCTOR_FINISH(self);
3812}
3813
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003814 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003815WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003816{
3817 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003818 PyInt n = 0;
3819
Bram Moolenaard6e39182013-05-21 18:30:34 +02003820 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003821 return -1;
3822
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003823 while (w != NULL)
3824 {
3825 ++n;
3826 w = W_NEXT(w);
3827 }
3828
3829 return n;
3830}
3831
3832 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003833WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003834{
3835 win_T *w;
3836
Bram Moolenaard6e39182013-05-21 18:30:34 +02003837 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003838 return NULL;
3839
3840 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003841 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003842 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003843
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003844 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003845 return NULL;
3846}
3847
3848/* Convert a Python string into a Vim line.
3849 *
3850 * The result is in allocated memory. All internal nulls are replaced by
3851 * newline characters. It is an error for the string to contain newline
3852 * characters.
3853 *
3854 * On errors, the Python exception data is set, and NULL is returned.
3855 */
3856 static char *
3857StringToLine(PyObject *obj)
3858{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003859 char *str;
3860 char *save;
3861 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003862 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003863 PyInt i;
3864 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003865
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003866 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003867 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003868 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3869 || str == NULL)
3870 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003871 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003872 else if (PyUnicode_Check(obj))
3873 {
3874 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3875 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003876
Bram Moolenaardaa27022013-06-24 22:33:30 +02003877 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003878 || str == NULL)
3879 {
3880 Py_DECREF(bytes);
3881 return NULL;
3882 }
3883 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003884 else
3885 {
3886#if PY_MAJOR_VERSION < 3
3887 PyErr_FORMAT(PyExc_TypeError,
3888 N_("expected str() or unicode() instance, but got %s"),
3889 Py_TYPE_NAME(obj));
3890#else
3891 PyErr_FORMAT(PyExc_TypeError,
3892 N_("expected bytes() or str() instance, but got %s"),
3893 Py_TYPE_NAME(obj));
3894#endif
3895 return NULL;
3896 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003897
3898 /*
3899 * Error checking: String must not contain newlines, as we
3900 * are replacing a single line, and we must replace it with
3901 * a single line.
3902 * A trailing newline is removed, so that append(f.readlines()) works.
3903 */
3904 p = memchr(str, '\n', len);
3905 if (p != NULL)
3906 {
3907 if (p == str + len - 1)
3908 --len;
3909 else
3910 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003911 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003912 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003913 return NULL;
3914 }
3915 }
3916
3917 /* Create a copy of the string, with internal nulls replaced by
3918 * newline characters, as is the vim convention.
3919 */
3920 save = (char *)alloc((unsigned)(len+1));
3921 if (save == NULL)
3922 {
3923 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003924 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003925 return NULL;
3926 }
3927
3928 for (i = 0; i < len; ++i)
3929 {
3930 if (str[i] == '\0')
3931 save[i] = '\n';
3932 else
3933 save[i] = str[i];
3934 }
3935
3936 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003937 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003938
3939 return save;
3940}
3941
3942/* Get a line from the specified buffer. The line number is
3943 * in Vim format (1-based). The line is returned as a Python
3944 * string object.
3945 */
3946 static PyObject *
3947GetBufferLine(buf_T *buf, PyInt n)
3948{
3949 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3950}
3951
3952
3953/* Get a list of lines from the specified buffer. The line numbers
3954 * are in Vim format (1-based). The range is from lo up to, but not
3955 * including, hi. The list is returned as a Python list of string objects.
3956 */
3957 static PyObject *
3958GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3959{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003960 PyInt i;
3961 PyInt n = hi - lo;
3962 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003963
3964 if (list == NULL)
3965 return NULL;
3966
3967 for (i = 0; i < n; ++i)
3968 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003969 PyObject *string = LineToString(
3970 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003971
3972 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003973 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003974 {
3975 Py_DECREF(list);
3976 return NULL;
3977 }
3978
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003979 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003980 }
3981
3982 /* The ownership of the Python list is passed to the caller (ie,
3983 * the caller should Py_DECREF() the object when it is finished
3984 * with it).
3985 */
3986
3987 return list;
3988}
3989
3990/*
3991 * Check if deleting lines made the cursor position invalid.
3992 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3993 * deleted).
3994 */
3995 static void
3996py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3997{
3998 if (curwin->w_cursor.lnum >= lo)
3999 {
4000 /* Adjust the cursor position if it's in/after the changed
4001 * lines. */
4002 if (curwin->w_cursor.lnum >= hi)
4003 {
4004 curwin->w_cursor.lnum += extra;
4005 check_cursor_col();
4006 }
4007 else if (extra < 0)
4008 {
4009 curwin->w_cursor.lnum = lo;
4010 check_cursor();
4011 }
4012 else
4013 check_cursor_col();
4014 changed_cline_bef_curs();
4015 }
4016 invalidate_botline();
4017}
4018
Bram Moolenaar19e60942011-06-19 00:27:51 +02004019/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004020 * Find a window that contains "buf" and switch to it.
4021 * If there is no such window, use the current window and change "curbuf".
4022 * Caller must initialize save_curbuf to NULL.
4023 * restore_win_for_buf() MUST be called later!
4024 */
4025 static void
4026switch_to_win_for_buf(
4027 buf_T *buf,
4028 win_T **save_curwinp,
4029 tabpage_T **save_curtabp,
4030 buf_T **save_curbufp)
4031{
4032 win_T *wp;
4033 tabpage_T *tp;
4034
4035 if (find_win_for_buf(buf, &wp, &tp) == FAIL
4036 || switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4037 switch_buffer(save_curbufp, buf);
4038}
4039
4040 static void
4041restore_win_for_buf(
4042 win_T *save_curwin,
4043 tabpage_T *save_curtab,
4044 buf_T *save_curbuf)
4045{
4046 if (save_curbuf == NULL)
4047 restore_win(save_curwin, save_curtab, TRUE);
4048 else
4049 restore_buffer(save_curbuf);
4050}
4051
4052/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004053 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054 * in Vim format (1-based). The replacement line is given as
4055 * a Python string object. The object is checked for validity
4056 * and correct format. Errors are returned as a value of FAIL.
4057 * The return value is OK on success.
4058 * If OK is returned and len_change is not NULL, *len_change
4059 * is set to the change in the buffer length.
4060 */
4061 static int
4062SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4063{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004064 buf_T *save_curbuf = NULL;
4065 win_T *save_curwin = NULL;
4066 tabpage_T *save_curtab = NULL;
4067
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004068 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004069 * There are three cases:
4070 * 1. NULL, or None - this is a deletion.
4071 * 2. A string - this is a replacement.
4072 * 3. Anything else - this is an error.
4073 */
4074 if (line == Py_None || line == NULL)
4075 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004076 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004077 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004078
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004079 VimTryStart();
4080
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004081 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004082 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004083 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004084 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085 else
4086 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004087 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004088 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004089 if (save_curbuf == NULL)
4090 /* Only adjust marks if we managed to switch to a window that
4091 * holds the buffer, otherwise line numbers will be invalid. */
4092 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004093 }
4094
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004095 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004096
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004097 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004098 return FAIL;
4099
4100 if (len_change)
4101 *len_change = -1;
4102
4103 return OK;
4104 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004105 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004106 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004107 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004108
4109 if (save == NULL)
4110 return FAIL;
4111
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004112 VimTryStart();
4113
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 /* We do not need to free "save" if ml_replace() consumes it. */
4115 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004116 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004117
4118 if (u_savesub((linenr_T)n) == FAIL)
4119 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004120 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004121 vim_free(save);
4122 }
4123 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4124 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004125 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004126 vim_free(save);
4127 }
4128 else
4129 changed_bytes((linenr_T)n, 0);
4130
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004131 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004132
4133 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004134 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004135 check_cursor_col();
4136
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004137 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004138 return FAIL;
4139
4140 if (len_change)
4141 *len_change = 0;
4142
4143 return OK;
4144 }
4145 else
4146 {
4147 PyErr_BadArgument();
4148 return FAIL;
4149 }
4150}
4151
Bram Moolenaar19e60942011-06-19 00:27:51 +02004152/* Replace a range of lines in the specified buffer. The line numbers are in
4153 * Vim format (1-based). The range is from lo up to, but not including, hi.
4154 * The replacement lines are given as a Python list of string objects. The
4155 * list is checked for validity and correct format. Errors are returned as a
4156 * value of FAIL. The return value is OK on success.
4157 * If OK is returned and len_change is not NULL, *len_change
4158 * is set to the change in the buffer length.
4159 */
4160 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004161SetBufferLineList(
4162 buf_T *buf,
4163 PyInt lo,
4164 PyInt hi,
4165 PyObject *list,
4166 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004167{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004168 buf_T *save_curbuf = NULL;
4169 win_T *save_curwin = NULL;
4170 tabpage_T *save_curtab = NULL;
4171
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004172 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004173 * There are three cases:
4174 * 1. NULL, or None - this is a deletion.
4175 * 2. A list - this is a replacement.
4176 * 3. Anything else - this is an error.
4177 */
4178 if (list == Py_None || list == NULL)
4179 {
4180 PyInt i;
4181 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004182
4183 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004184 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004185 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004186
4187 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004188 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004189 else
4190 {
4191 for (i = 0; i < n; ++i)
4192 {
4193 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4194 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004195 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004196 break;
4197 }
4198 }
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004199 if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL))
4200 /* Using an existing window for the buffer, adjust the cursor
4201 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004202 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004203 if (save_curbuf == NULL)
4204 /* Only adjust marks if we managed to switch to a window that
4205 * holds the buffer, otherwise line numbers will be invalid. */
4206 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004207 }
4208
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004209 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004210
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004211 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004212 return FAIL;
4213
4214 if (len_change)
4215 *len_change = -n;
4216
4217 return OK;
4218 }
4219 else if (PyList_Check(list))
4220 {
4221 PyInt i;
4222 PyInt new_len = PyList_Size(list);
4223 PyInt old_len = hi - lo;
4224 PyInt extra = 0; /* lines added to text, can be negative */
4225 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004226
4227 if (new_len == 0) /* avoid allocating zero bytes */
4228 array = NULL;
4229 else
4230 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004231 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004232 if (array == NULL)
4233 {
4234 PyErr_NoMemory();
4235 return FAIL;
4236 }
4237 }
4238
4239 for (i = 0; i < new_len; ++i)
4240 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004241 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004242
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004243 if (!(line = PyList_GetItem(list, i)) ||
4244 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004245 {
4246 while (i)
4247 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004248 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004249 return FAIL;
4250 }
4251 }
4252
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004253 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004254 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004255
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004256 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004257 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004258
4259 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004260 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004261
4262 /* If the size of the range is reducing (ie, new_len < old_len) we
4263 * need to delete some old_len. We do this at the start, by
4264 * repeatedly deleting line "lo".
4265 */
4266 if (!PyErr_Occurred())
4267 {
4268 for (i = 0; i < old_len - new_len; ++i)
4269 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4270 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004271 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004272 break;
4273 }
4274 extra -= i;
4275 }
4276
4277 /* For as long as possible, replace the existing old_len with the
4278 * new old_len. This is a more efficient operation, as it requires
4279 * less memory allocation and freeing.
4280 */
4281 if (!PyErr_Occurred())
4282 {
4283 for (i = 0; i < old_len && i < new_len; ++i)
4284 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4285 == FAIL)
4286 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004287 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004288 break;
4289 }
4290 }
4291 else
4292 i = 0;
4293
4294 /* Now we may need to insert the remaining new old_len. If we do, we
4295 * must free the strings as we finish with them (we can't pass the
4296 * responsibility to vim in this case).
4297 */
4298 if (!PyErr_Occurred())
4299 {
4300 while (i < new_len)
4301 {
4302 if (ml_append((linenr_T)(lo + i - 1),
4303 (char_u *)array[i], 0, FALSE) == FAIL)
4304 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004305 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004306 break;
4307 }
4308 vim_free(array[i]);
4309 ++i;
4310 ++extra;
4311 }
4312 }
4313
4314 /* Free any left-over old_len, as a result of an error */
4315 while (i < new_len)
4316 {
4317 vim_free(array[i]);
4318 ++i;
4319 }
4320
4321 /* Free the array of old_len. All of its contents have now
4322 * been dealt with (either freed, or the responsibility passed
4323 * to vim.
4324 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004325 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004326
4327 /* Adjust marks. Invalidate any which lie in the
4328 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004329 * Only adjust marks if we managed to switch to a window that holds
4330 * the buffer, otherwise line numbers will be invalid. */
4331 if (save_curbuf == NULL)
4332 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004333 (long)MAXLNUM, (long)extra);
4334 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4335
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004336 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004337 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4338
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004339 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004340 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004341
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004342 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004343 return FAIL;
4344
4345 if (len_change)
4346 *len_change = new_len - old_len;
4347
4348 return OK;
4349 }
4350 else
4351 {
4352 PyErr_BadArgument();
4353 return FAIL;
4354 }
4355}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004356
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004357/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004358 * The line number is in Vim format (1-based). The lines to be inserted are
4359 * given as a Python list of string objects or as a single string. The lines
4360 * to be added are checked for validity and correct format. Errors are
4361 * returned as a value of FAIL. The return value is OK on success.
4362 * If OK is returned and len_change is not NULL, *len_change
4363 * is set to the change in the buffer length.
4364 */
4365 static int
4366InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4367{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004368 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004369 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004370 tabpage_T *save_curtab = NULL;
4371
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004372 /* First of all, we check the type of the supplied Python object.
4373 * It must be a string or a list, or the call is in error.
4374 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004375 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004376 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004377 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004378
4379 if (str == NULL)
4380 return FAIL;
4381
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004382 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004383 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004384 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004385
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004386 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004387 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004388 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004389 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004390 else if (save_curbuf == NULL)
4391 /* Only adjust marks if we managed to switch to a window that
4392 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004393 appended_lines_mark((linenr_T)n, 1L);
4394
4395 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004396 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004397 update_screen(VALID);
4398
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004399 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004400 return FAIL;
4401
4402 if (len_change)
4403 *len_change = 1;
4404
4405 return OK;
4406 }
4407 else if (PyList_Check(lines))
4408 {
4409 PyInt i;
4410 PyInt size = PyList_Size(lines);
4411 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004412
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004413 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004414 if (array == NULL)
4415 {
4416 PyErr_NoMemory();
4417 return FAIL;
4418 }
4419
4420 for (i = 0; i < size; ++i)
4421 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004422 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004423
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004424 if (!(line = PyList_GetItem(lines, i)) ||
4425 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004426 {
4427 while (i)
4428 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004429 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004430 return FAIL;
4431 }
4432 }
4433
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004434 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004435 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004436 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004437
4438 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004439 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004440 else
4441 {
4442 for (i = 0; i < size; ++i)
4443 {
4444 if (ml_append((linenr_T)(n + i),
4445 (char_u *)array[i], 0, FALSE) == FAIL)
4446 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004447 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004448
4449 /* Free the rest of the lines */
4450 while (i < size)
4451 vim_free(array[i++]);
4452
4453 break;
4454 }
4455 vim_free(array[i]);
4456 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004457 if (i > 0 && save_curbuf == NULL)
4458 /* Only adjust marks if we managed to switch to a window that
4459 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004460 appended_lines_mark((linenr_T)n, (long)i);
4461 }
4462
4463 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004464 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004465 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004466 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004467
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004468 update_screen(VALID);
4469
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004470 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004471 return FAIL;
4472
4473 if (len_change)
4474 *len_change = size;
4475
4476 return OK;
4477 }
4478 else
4479 {
4480 PyErr_BadArgument();
4481 return FAIL;
4482 }
4483}
4484
4485/*
4486 * Common routines for buffers and line ranges
4487 * -------------------------------------------
4488 */
4489
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004490typedef struct
4491{
4492 PyObject_HEAD
4493 buf_T *buf;
4494} BufferObject;
4495
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004496 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004497CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004498{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004499 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004500 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004501 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004502 return -1;
4503 }
4504
4505 return 0;
4506}
4507
4508 static PyObject *
4509RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4510{
4511 if (CheckBuffer(self))
4512 return NULL;
4513
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004514 if (end == -1)
4515 end = self->buf->b_ml.ml_line_count;
4516
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004517 if (n < 0)
4518 n += end - start + 1;
4519
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004520 if (n < 0 || n > end - start)
4521 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004522 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004523 return NULL;
4524 }
4525
4526 return GetBufferLine(self->buf, n+start);
4527}
4528
4529 static PyObject *
4530RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4531{
4532 PyInt size;
4533
4534 if (CheckBuffer(self))
4535 return NULL;
4536
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004537 if (end == -1)
4538 end = self->buf->b_ml.ml_line_count;
4539
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004540 size = end - start + 1;
4541
4542 if (lo < 0)
4543 lo = 0;
4544 else if (lo > size)
4545 lo = size;
4546 if (hi < 0)
4547 hi = 0;
4548 if (hi < lo)
4549 hi = lo;
4550 else if (hi > size)
4551 hi = size;
4552
4553 return GetBufferLineList(self->buf, lo+start, hi+start);
4554}
4555
4556 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004557RBAsItem(
4558 BufferObject *self,
4559 PyInt n,
4560 PyObject *valObject,
4561 PyInt start,
4562 PyInt end,
4563 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004564{
4565 PyInt len_change;
4566
4567 if (CheckBuffer(self))
4568 return -1;
4569
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004570 if (end == -1)
4571 end = self->buf->b_ml.ml_line_count;
4572
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004573 if (n < 0)
4574 n += end - start + 1;
4575
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004576 if (n < 0 || n > end - start)
4577 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004578 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004579 return -1;
4580 }
4581
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004582 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004583 return -1;
4584
4585 if (new_end)
4586 *new_end = end + len_change;
4587
4588 return 0;
4589}
4590
Bram Moolenaar19e60942011-06-19 00:27:51 +02004591 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004592RBAsSlice(
4593 BufferObject *self,
4594 PyInt lo,
4595 PyInt hi,
4596 PyObject *valObject,
4597 PyInt start,
4598 PyInt end,
4599 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004600{
4601 PyInt size;
4602 PyInt len_change;
4603
4604 /* Self must be a valid buffer */
4605 if (CheckBuffer(self))
4606 return -1;
4607
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004608 if (end == -1)
4609 end = self->buf->b_ml.ml_line_count;
4610
Bram Moolenaar19e60942011-06-19 00:27:51 +02004611 /* Sort out the slice range */
4612 size = end - start + 1;
4613
4614 if (lo < 0)
4615 lo = 0;
4616 else if (lo > size)
4617 lo = size;
4618 if (hi < 0)
4619 hi = 0;
4620 if (hi < lo)
4621 hi = lo;
4622 else if (hi > size)
4623 hi = size;
4624
4625 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004626 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004627 return -1;
4628
4629 if (new_end)
4630 *new_end = end + len_change;
4631
4632 return 0;
4633}
4634
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004635
4636 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004637RBAppend(
4638 BufferObject *self,
4639 PyObject *args,
4640 PyInt start,
4641 PyInt end,
4642 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004643{
4644 PyObject *lines;
4645 PyInt len_change;
4646 PyInt max;
4647 PyInt n;
4648
4649 if (CheckBuffer(self))
4650 return NULL;
4651
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004652 if (end == -1)
4653 end = self->buf->b_ml.ml_line_count;
4654
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004655 max = n = end - start + 1;
4656
4657 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4658 return NULL;
4659
4660 if (n < 0 || n > max)
4661 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004662 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004663 return NULL;
4664 }
4665
4666 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4667 return NULL;
4668
4669 if (new_end)
4670 *new_end = end + len_change;
4671
4672 Py_INCREF(Py_None);
4673 return Py_None;
4674}
4675
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004676/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004677 */
4678
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004679static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004680static PySequenceMethods RangeAsSeq;
4681static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004682
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004683typedef struct
4684{
4685 PyObject_HEAD
4686 BufferObject *buf;
4687 PyInt start;
4688 PyInt end;
4689} RangeObject;
4690
4691 static PyObject *
4692RangeNew(buf_T *buf, PyInt start, PyInt end)
4693{
4694 BufferObject *bufr;
4695 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004696 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004697 if (self == NULL)
4698 return NULL;
4699
4700 bufr = (BufferObject *)BufferNew(buf);
4701 if (bufr == NULL)
4702 {
4703 Py_DECREF(self);
4704 return NULL;
4705 }
4706 Py_INCREF(bufr);
4707
4708 self->buf = bufr;
4709 self->start = start;
4710 self->end = end;
4711
4712 return (PyObject *)(self);
4713}
4714
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004715 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004716RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004717{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004718 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004719 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004720 PyObject_GC_Del((void *)(self));
4721}
4722
4723 static int
4724RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4725{
4726 Py_VISIT(((PyObject *)(self->buf)));
4727 return 0;
4728}
4729
4730 static int
4731RangeClear(RangeObject *self)
4732{
4733 Py_CLEAR(self->buf);
4734 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004735}
4736
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004737 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004738RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004739{
4740 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004741 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004742 return -1; /* ??? */
4743
Bram Moolenaard6e39182013-05-21 18:30:34 +02004744 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004745}
4746
4747 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004748RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004749{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004750 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004751}
4752
4753 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004754RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004755{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004756 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004757}
4758
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004759static char *RangeAttrs[] = {
4760 "start", "end",
4761 NULL
4762};
4763
4764 static PyObject *
4765RangeDir(PyObject *self)
4766{
4767 return ObjectDir(self, RangeAttrs);
4768}
4769
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004770 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004771RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004772{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004773 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004774}
4775
4776 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004777RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004778{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004779 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004780 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4781 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004782 else
4783 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004784 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004785
4786 if (name == NULL)
4787 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004788
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004789 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004790 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004791 }
4792}
4793
4794static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004795 /* name, function, calling, documentation */
4796 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004797 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4798 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004799};
4800
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004801static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004802static PySequenceMethods BufferAsSeq;
4803static PyMappingMethods BufferAsMapping;
4804
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004805 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004806BufferNew(buf_T *buf)
4807{
4808 /* We need to handle deletion of buffers underneath us.
4809 * If we add a "b_python*_ref" field to the buf_T structure,
4810 * then we can get at it in buf_freeall() in vim. We then
4811 * need to create only ONE Python object per buffer - if
4812 * we try to create a second, just INCREF the existing one
4813 * and return it. The (single) Python object referring to
4814 * the buffer is stored in "b_python*_ref".
4815 * Question: what to do on a buf_freeall(). We'll probably
4816 * have to either delete the Python object (DECREF it to
4817 * zero - a bad idea, as it leaves dangling refs!) or
4818 * set the buf_T * value to an invalid value (-1?), which
4819 * means we need checks in all access functions... Bah.
4820 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004821 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004822 * b_python_ref and b_python3_ref fields respectively.
4823 */
4824
4825 BufferObject *self;
4826
4827 if (BUF_PYTHON_REF(buf) != NULL)
4828 {
4829 self = BUF_PYTHON_REF(buf);
4830 Py_INCREF(self);
4831 }
4832 else
4833 {
4834 self = PyObject_NEW(BufferObject, &BufferType);
4835 if (self == NULL)
4836 return NULL;
4837 self->buf = buf;
4838 BUF_PYTHON_REF(buf) = self;
4839 }
4840
4841 return (PyObject *)(self);
4842}
4843
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004844 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004845BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004846{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004847 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4848 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004849
4850 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004851}
4852
Bram Moolenaar971db462013-05-12 18:44:48 +02004853 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004854BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004855{
4856 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004857 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004858 return -1; /* ??? */
4859
Bram Moolenaard6e39182013-05-21 18:30:34 +02004860 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004861}
4862
4863 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004864BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004865{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004866 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004867}
4868
4869 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004870BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004871{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004872 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004873}
4874
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004875static char *BufferAttrs[] = {
4876 "name", "number", "vars", "options", "valid",
4877 NULL
4878};
4879
4880 static PyObject *
4881BufferDir(PyObject *self)
4882{
4883 return ObjectDir(self, BufferAttrs);
4884}
4885
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004886 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004887BufferAttrValid(BufferObject *self, char *name)
4888{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004889 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004890
4891 if (strcmp(name, "valid") != 0)
4892 return NULL;
4893
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004894 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4895 Py_INCREF(ret);
4896 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004897}
4898
4899 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004900BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004901{
4902 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004903 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004904 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004905 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004906 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004907 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004908 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004909 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004910 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4911 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004912 else if (strcmp(name, "__members__") == 0)
4913 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004914 else
4915 return NULL;
4916}
4917
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004918 static int
4919BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4920{
4921 if (CheckBuffer(self))
4922 return -1;
4923
4924 if (strcmp(name, "name") == 0)
4925 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004926 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004927 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004928 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004929 PyObject *todecref;
4930
4931 if (!(val = StringToChars(valObject, &todecref)))
4932 return -1;
4933
4934 VimTryStart();
4935 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4936 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004937 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004938 aucmd_restbuf(&aco);
4939 Py_XDECREF(todecref);
4940 if (VimTryEnd())
4941 return -1;
4942
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004943 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004944 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004945 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004946 return -1;
4947 }
4948 return 0;
4949 }
4950 else
4951 {
4952 PyErr_SetString(PyExc_AttributeError, name);
4953 return -1;
4954 }
4955}
4956
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004957 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004958BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004959{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004960 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004961}
4962
4963 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004964BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004965{
4966 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004967 char_u *pmark;
4968 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004969 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004970 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004971
Bram Moolenaard6e39182013-05-21 18:30:34 +02004972 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004973 return NULL;
4974
Bram Moolenaar389a1792013-06-23 13:00:44 +02004975 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004976 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004977
Bram Moolenaar389a1792013-06-23 13:00:44 +02004978 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004979 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004980 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004981 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004982 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004983 return NULL;
4984 }
4985
4986 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004987
4988 Py_XDECREF(todecref);
4989
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004990 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004991 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004992 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004993 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004994 if (VimTryEnd())
4995 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004996
4997 if (posp == NULL)
4998 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004999 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005000 return NULL;
5001 }
5002
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005003 if (posp->lnum <= 0)
5004 {
5005 /* Or raise an error? */
5006 Py_INCREF(Py_None);
5007 return Py_None;
5008 }
5009
5010 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5011}
5012
5013 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005014BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005015{
5016 PyInt start;
5017 PyInt end;
5018
Bram Moolenaard6e39182013-05-21 18:30:34 +02005019 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005020 return NULL;
5021
5022 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5023 return NULL;
5024
Bram Moolenaard6e39182013-05-21 18:30:34 +02005025 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005026}
5027
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005028 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005029BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005030{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005031 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005032 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005033 else
5034 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005035 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005036
5037 if (name == NULL)
5038 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005039
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005040 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005041 }
5042}
5043
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005044static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005045 /* name, function, calling, documentation */
5046 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005047 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005048 {"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 +02005049 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5050 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005051};
5052
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005053/*
5054 * Buffer list object - Implementation
5055 */
5056
5057static PyTypeObject BufMapType;
5058
5059typedef struct
5060{
5061 PyObject_HEAD
5062} BufMapObject;
5063
5064 static PyInt
5065BufMapLength(PyObject *self UNUSED)
5066{
5067 buf_T *b = firstbuf;
5068 PyInt n = 0;
5069
5070 while (b)
5071 {
5072 ++n;
5073 b = b->b_next;
5074 }
5075
5076 return n;
5077}
5078
5079 static PyObject *
5080BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5081{
5082 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005083 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005084
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005085 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005086 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005087
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005088 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005089
5090 if (b)
5091 return BufferNew(b);
5092 else
5093 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005094 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005095 return NULL;
5096 }
5097}
5098
5099 static void
5100BufMapIterDestruct(PyObject *buffer)
5101{
5102 /* Iteration was stopped before all buffers were processed */
5103 if (buffer)
5104 {
5105 Py_DECREF(buffer);
5106 }
5107}
5108
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005109 static int
5110BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5111{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005112 if (buffer)
5113 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005114 return 0;
5115}
5116
5117 static int
5118BufMapIterClear(PyObject **buffer)
5119{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005120 if (*buffer)
5121 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005122 return 0;
5123}
5124
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005125 static PyObject *
5126BufMapIterNext(PyObject **buffer)
5127{
5128 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005129 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005130
5131 if (!*buffer)
5132 return NULL;
5133
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005134 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005135
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005136 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005137 {
5138 *buffer = NULL;
5139 return NULL;
5140 }
5141
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005142 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005143 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005144 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005145 return NULL;
5146 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005147 /* Do not increment reference: we no longer hold it (decref), but whoever
5148 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005149 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005150}
5151
5152 static PyObject *
5153BufMapIter(PyObject *self UNUSED)
5154{
5155 PyObject *buffer;
5156
5157 buffer = BufferNew(firstbuf);
5158 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005159 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5160 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005161}
5162
5163static PyMappingMethods BufMapAsMapping = {
5164 (lenfunc) BufMapLength,
5165 (binaryfunc) BufMapItem,
5166 (objobjargproc) 0,
5167};
5168
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005169/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005170 */
5171
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005172static char *CurrentAttrs[] = {
5173 "buffer", "window", "line", "range", "tabpage",
5174 NULL
5175};
5176
5177 static PyObject *
5178CurrentDir(PyObject *self)
5179{
5180 return ObjectDir(self, CurrentAttrs);
5181}
5182
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005183 static PyObject *
5184CurrentGetattr(PyObject *self UNUSED, char *name)
5185{
5186 if (strcmp(name, "buffer") == 0)
5187 return (PyObject *)BufferNew(curbuf);
5188 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005189 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005190 else if (strcmp(name, "tabpage") == 0)
5191 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005192 else if (strcmp(name, "line") == 0)
5193 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5194 else if (strcmp(name, "range") == 0)
5195 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005196 else if (strcmp(name, "__members__") == 0)
5197 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005198 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005199#if PY_MAJOR_VERSION < 3
5200 return Py_FindMethod(WindowMethods, self, name);
5201#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005202 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005203#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005204}
5205
5206 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005207CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005208{
5209 if (strcmp(name, "line") == 0)
5210 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005211 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5212 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005213 return -1;
5214
5215 return 0;
5216 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005217 else if (strcmp(name, "buffer") == 0)
5218 {
5219 int count;
5220
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005221 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005222 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005223 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005224 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005225 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005226 return -1;
5227 }
5228
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005229 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005230 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005231 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005232
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005233 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005234 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5235 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005236 if (VimTryEnd())
5237 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005238 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005239 return -1;
5240 }
5241
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005242 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005243 }
5244 else if (strcmp(name, "window") == 0)
5245 {
5246 int count;
5247
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005248 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005249 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005250 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005251 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005252 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005253 return -1;
5254 }
5255
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005256 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005257 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005258 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005259
5260 if (!count)
5261 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005262 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005263 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005264 return -1;
5265 }
5266
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005267 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005268 win_goto(((WindowObject *)(valObject))->win);
5269 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005270 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005271 if (VimTryEnd())
5272 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005273 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005274 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005275 return -1;
5276 }
5277
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005278 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005279 }
5280 else if (strcmp(name, "tabpage") == 0)
5281 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005282 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005283 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005284 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005285 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005286 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005287 return -1;
5288 }
5289
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005290 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005291 return -1;
5292
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005293 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005294 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5295 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005296 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005297 if (VimTryEnd())
5298 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005299 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005300 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005301 return -1;
5302 }
5303
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005304 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005305 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005306 else
5307 {
5308 PyErr_SetString(PyExc_AttributeError, name);
5309 return -1;
5310 }
5311}
5312
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005313static struct PyMethodDef CurrentMethods[] = {
5314 /* name, function, calling, documentation */
5315 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5316 { NULL, NULL, 0, NULL}
5317};
5318
Bram Moolenaardb913952012-06-29 12:54:53 +02005319 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005320init_range_cmd(exarg_T *eap)
5321{
5322 RangeStart = eap->line1;
5323 RangeEnd = eap->line2;
5324}
5325
5326 static void
5327init_range_eval(typval_T *rettv UNUSED)
5328{
5329 RangeStart = (PyInt) curwin->w_cursor.lnum;
5330 RangeEnd = RangeStart;
5331}
5332
5333 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005334run_cmd(const char *cmd, void *arg UNUSED
5335#ifdef PY_CAN_RECURSE
5336 , PyGILState_STATE *pygilstate UNUSED
5337#endif
5338 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005339{
Bram Moolenaar41009372013-07-01 22:03:04 +02005340 PyObject *run_ret;
5341 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5342 if (run_ret != NULL)
5343 {
5344 Py_DECREF(run_ret);
5345 }
5346 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5347 {
5348 EMSG2(_(e_py_systemexit), "python");
5349 PyErr_Clear();
5350 }
5351 else
5352 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005353}
5354
5355static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5356static int code_hdr_len = 30;
5357
5358 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005359run_do(const char *cmd, void *arg UNUSED
5360#ifdef PY_CAN_RECURSE
5361 , PyGILState_STATE *pygilstate
5362#endif
5363 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005364{
5365 PyInt lnum;
5366 size_t len;
5367 char *code;
5368 int status;
5369 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005370 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005371
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005372 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005373 {
5374 EMSG(_("cannot save undo information"));
5375 return;
5376 }
5377
5378 len = code_hdr_len + STRLEN(cmd);
5379 code = PyMem_New(char, len + 1);
5380 memcpy(code, code_hdr, code_hdr_len);
5381 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005382 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5383 status = -1;
5384 if (run_ret != NULL)
5385 {
5386 status = 0;
5387 Py_DECREF(run_ret);
5388 }
5389 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5390 {
5391 PyMem_Free(code);
5392 EMSG2(_(e_py_systemexit), "python");
5393 PyErr_Clear();
5394 return;
5395 }
5396 else
5397 PyErr_PrintEx(1);
5398
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005399 PyMem_Free(code);
5400
5401 if (status)
5402 {
5403 EMSG(_("failed to run the code"));
5404 return;
5405 }
5406
5407 status = 0;
5408 pymain = PyImport_AddModule("__main__");
5409 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005410#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005411 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005412#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005413
5414 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5415 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005416 PyObject *line;
5417 PyObject *linenr;
5418 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005419
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005420#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005421 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005422#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005423 if (!(line = GetBufferLine(curbuf, lnum)))
5424 goto err;
5425 if (!(linenr = PyInt_FromLong((long) lnum)))
5426 {
5427 Py_DECREF(line);
5428 goto err;
5429 }
5430 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5431 Py_DECREF(line);
5432 Py_DECREF(linenr);
5433 if (!ret)
5434 goto err;
5435
5436 if (ret != Py_None)
5437 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5438 goto err;
5439
5440 Py_XDECREF(ret);
5441 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005442#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005443 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005444#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005445 }
5446 goto out;
5447err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005448#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005449 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005450#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005451 PyErr_PrintEx(0);
5452 PythonIO_Flush();
5453 status = 1;
5454out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005455#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005456 if (!status)
5457 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005458#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005459 Py_DECREF(pyfunc);
5460 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5461 if (status)
5462 return;
5463 check_cursor();
5464 update_curbuf(NOT_VALID);
5465}
5466
5467 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005468run_eval(const char *cmd, typval_T *rettv
5469#ifdef PY_CAN_RECURSE
5470 , PyGILState_STATE *pygilstate UNUSED
5471#endif
5472 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005473{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005474 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005475
Bram Moolenaar41009372013-07-01 22:03:04 +02005476 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005477 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005478 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005479 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005480 {
5481 EMSG2(_(e_py_systemexit), "python");
5482 PyErr_Clear();
5483 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005484 else
5485 {
5486 if (PyErr_Occurred() && !msg_silent)
5487 PyErr_PrintEx(0);
5488 EMSG(_("E858: Eval did not return a valid python object"));
5489 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005490 }
5491 else
5492 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005493 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005494 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005495 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005496 }
5497 PyErr_Clear();
5498}
5499
5500 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005501set_ref_in_py(const int copyID)
5502{
5503 pylinkedlist_T *cur;
5504 dict_T *dd;
5505 list_T *ll;
5506
5507 if (lastdict != NULL)
5508 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5509 {
5510 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5511 if (dd->dv_copyID != copyID)
5512 {
5513 dd->dv_copyID = copyID;
5514 set_ref_in_ht(&dd->dv_hashtab, copyID);
5515 }
5516 }
5517
5518 if (lastlist != NULL)
5519 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5520 {
5521 ll = ((ListObject *) (cur->pll_obj))->list;
5522 if (ll->lv_copyID != copyID)
5523 {
5524 ll->lv_copyID = copyID;
5525 set_ref_in_list(ll, copyID);
5526 }
5527 }
5528}
5529
5530 static int
5531set_string_copy(char_u *str, typval_T *tv)
5532{
5533 tv->vval.v_string = vim_strsave(str);
5534 if (tv->vval.v_string == NULL)
5535 {
5536 PyErr_NoMemory();
5537 return -1;
5538 }
5539 return 0;
5540}
5541
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005542 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005543pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005544{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005545 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005546 char_u *key;
5547 dictitem_T *di;
5548 PyObject *keyObject;
5549 PyObject *valObject;
5550 Py_ssize_t iter = 0;
5551
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005552 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005553 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005554
5555 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005556 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005557
5558 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5559 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005560 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005561
Bram Moolenaara03e6312013-05-29 22:49:26 +02005562 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005563 {
5564 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005565 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005566 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005567
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005568 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005569 {
5570 dict_unref(dict);
5571 return -1;
5572 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005573
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005574 if (*key == NUL)
5575 {
5576 dict_unref(dict);
5577 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005578 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005579 return -1;
5580 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005581
5582 di = dictitem_alloc(key);
5583
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005584 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005585
5586 if (di == NULL)
5587 {
5588 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005589 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005590 return -1;
5591 }
5592 di->di_tv.v_lock = 0;
5593
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005594 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005595 {
5596 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005597 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005598 return -1;
5599 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005600
5601 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005602 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005603 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005604 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005605 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005606 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005607 return -1;
5608 }
5609 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005610
5611 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005612 return 0;
5613}
5614
5615 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005616pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005617{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005618 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005619 char_u *key;
5620 dictitem_T *di;
5621 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005622 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005623 PyObject *keyObject;
5624 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005625
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005626 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005627 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005628
5629 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005630 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005631
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005632 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005633 {
5634 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005635 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005636 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005637
5638 if (!(iterator = PyObject_GetIter(list)))
5639 {
5640 dict_unref(dict);
5641 Py_DECREF(list);
5642 return -1;
5643 }
5644 Py_DECREF(list);
5645
5646 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005647 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005648 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005649
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005650 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005651 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005652 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005653 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005654 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005655 return -1;
5656 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005657
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005658 if (*key == NUL)
5659 {
5660 Py_DECREF(keyObject);
5661 Py_DECREF(iterator);
5662 Py_XDECREF(todecref);
5663 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005664 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005665 return -1;
5666 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005667
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005668 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005669 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005670 Py_DECREF(keyObject);
5671 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005672 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005673 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005674 return -1;
5675 }
5676
5677 di = dictitem_alloc(key);
5678
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005679 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005680 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005681
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005682 if (di == NULL)
5683 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005684 Py_DECREF(iterator);
5685 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005686 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005687 PyErr_NoMemory();
5688 return -1;
5689 }
5690 di->di_tv.v_lock = 0;
5691
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005692 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005693 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005694 Py_DECREF(iterator);
5695 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005696 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005697 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005698 return -1;
5699 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005700
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005701 Py_DECREF(valObject);
5702
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005703 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005704 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005705 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005706 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005707 dictitem_free(di);
5708 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005709 return -1;
5710 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005711 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005712 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005713 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005714 return 0;
5715}
5716
5717 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005718pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005719{
5720 list_T *l;
5721
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005722 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005723 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005724
5725 tv->v_type = VAR_LIST;
5726 tv->vval.v_list = l;
5727
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005728 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005729 {
5730 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005731 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005732 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005733
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005734 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005735 return 0;
5736}
5737
Bram Moolenaardb913952012-06-29 12:54:53 +02005738typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5739
5740 static int
5741convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005742 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005743{
5744 PyObject *capsule;
5745 char hexBuf[sizeof(void *) * 2 + 3];
5746
5747 sprintf(hexBuf, "%p", obj);
5748
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005749# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005750 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005751# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005752 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005753# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005754 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005755 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005756# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005757 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005758# else
5759 capsule = PyCObject_FromVoidPtr(tv, NULL);
5760# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005761 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5762 {
5763 Py_DECREF(capsule);
5764 tv->v_type = VAR_UNKNOWN;
5765 return -1;
5766 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005767
5768 Py_DECREF(capsule);
5769
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005770 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005771 {
5772 tv->v_type = VAR_UNKNOWN;
5773 return -1;
5774 }
5775 /* As we are not using copy_tv which increments reference count we must
5776 * do it ourself. */
5777 switch(tv->v_type)
5778 {
5779 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5780 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5781 }
5782 }
5783 else
5784 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005785 typval_T *v;
5786
5787# ifdef PY_USE_CAPSULE
5788 v = PyCapsule_GetPointer(capsule, NULL);
5789# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005790 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005791# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005792 copy_tv(v, tv);
5793 }
5794 return 0;
5795}
5796
5797 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005798ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5799{
5800 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005801 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005802
5803 if (!(lookup_dict = PyDict_New()))
5804 return -1;
5805
5806 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5807 {
5808 tv->v_type = VAR_DICT;
5809 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5810 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005811 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005812 }
5813 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005814 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005815 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005816 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005817 else
5818 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005819 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005820 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005821 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005822 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005823 }
5824 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005825 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005826}
5827
5828 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005829ConvertFromPyObject(PyObject *obj, typval_T *tv)
5830{
5831 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005832 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005833
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005834 if (!(lookup_dict = PyDict_New()))
5835 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005836 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005837 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005838 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005839}
5840
5841 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005842_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005843{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005844 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005845 {
5846 tv->v_type = VAR_DICT;
5847 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5848 ++tv->vval.v_dict->dv_refcount;
5849 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005850 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005851 {
5852 tv->v_type = VAR_LIST;
5853 tv->vval.v_list = (((ListObject *)(obj))->list);
5854 ++tv->vval.v_list->lv_refcount;
5855 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005856 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005857 {
5858 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5859 return -1;
5860
5861 tv->v_type = VAR_FUNC;
5862 func_ref(tv->vval.v_string);
5863 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005864 else if (PyBytes_Check(obj))
5865 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005866 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005867
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005868 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005869 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005870 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005871 return -1;
5872
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005873 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005874 return -1;
5875
5876 tv->v_type = VAR_STRING;
5877 }
5878 else if (PyUnicode_Check(obj))
5879 {
5880 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005881 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005882
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005883 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005884 if (bytes == NULL)
5885 return -1;
5886
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005887 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005888 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005889 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005890 return -1;
5891
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005892 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005893 {
5894 Py_XDECREF(bytes);
5895 return -1;
5896 }
5897 Py_XDECREF(bytes);
5898
5899 tv->v_type = VAR_STRING;
5900 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005901#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005902 else if (PyInt_Check(obj))
5903 {
5904 tv->v_type = VAR_NUMBER;
5905 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005906 if (PyErr_Occurred())
5907 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005908 }
5909#endif
5910 else if (PyLong_Check(obj))
5911 {
5912 tv->v_type = VAR_NUMBER;
5913 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005914 if (PyErr_Occurred())
5915 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005916 }
5917 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005918 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005919#ifdef FEAT_FLOAT
5920 else if (PyFloat_Check(obj))
5921 {
5922 tv->v_type = VAR_FLOAT;
5923 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5924 }
5925#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005926 else if (PyObject_HasAttrString(obj, "keys"))
5927 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005928 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005929 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005930 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005931 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005932 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005933 else if (PyNumber_Check(obj))
5934 {
5935 PyObject *num;
5936
5937 if (!(num = PyNumber_Long(obj)))
5938 return -1;
5939
5940 tv->v_type = VAR_NUMBER;
5941 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5942
5943 Py_DECREF(num);
5944 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005945 else
5946 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005947 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005948 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005949 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005950 return -1;
5951 }
5952 return 0;
5953}
5954
5955 static PyObject *
5956ConvertToPyObject(typval_T *tv)
5957{
5958 if (tv == NULL)
5959 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005960 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005961 return NULL;
5962 }
5963 switch (tv->v_type)
5964 {
5965 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005966 return PyBytes_FromString(tv->vval.v_string == NULL
5967 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005968 case VAR_NUMBER:
5969 return PyLong_FromLong((long) tv->vval.v_number);
5970#ifdef FEAT_FLOAT
5971 case VAR_FLOAT:
5972 return PyFloat_FromDouble((double) tv->vval.v_float);
5973#endif
5974 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005975 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005976 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005977 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005978 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005979 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005980 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005981 case VAR_UNKNOWN:
5982 Py_INCREF(Py_None);
5983 return Py_None;
5984 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005985 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005986 return NULL;
5987 }
5988}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005989
5990typedef struct
5991{
5992 PyObject_HEAD
5993} CurrentObject;
5994static PyTypeObject CurrentType;
5995
5996 static void
5997init_structs(void)
5998{
5999 vim_memset(&OutputType, 0, sizeof(OutputType));
6000 OutputType.tp_name = "vim.message";
6001 OutputType.tp_basicsize = sizeof(OutputObject);
6002 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6003 OutputType.tp_doc = "vim message object";
6004 OutputType.tp_methods = OutputMethods;
6005#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006006 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6007 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006008 OutputType.tp_alloc = call_PyType_GenericAlloc;
6009 OutputType.tp_new = call_PyType_GenericNew;
6010 OutputType.tp_free = call_PyObject_Free;
6011#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006012 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6013 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006014#endif
6015
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006016 vim_memset(&IterType, 0, sizeof(IterType));
6017 IterType.tp_name = "vim.iter";
6018 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006019 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006020 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006021 IterType.tp_iter = (getiterfunc)IterIter;
6022 IterType.tp_iternext = (iternextfunc)IterNext;
6023 IterType.tp_dealloc = (destructor)IterDestructor;
6024 IterType.tp_traverse = (traverseproc)IterTraverse;
6025 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006026
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006027 vim_memset(&BufferType, 0, sizeof(BufferType));
6028 BufferType.tp_name = "vim.buffer";
6029 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006030 BufferType.tp_dealloc = (destructor)BufferDestructor;
6031 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006032 BufferType.tp_as_sequence = &BufferAsSeq;
6033 BufferType.tp_as_mapping = &BufferAsMapping;
6034 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6035 BufferType.tp_doc = "vim buffer object";
6036 BufferType.tp_methods = BufferMethods;
6037#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006038 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006039 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006040 BufferType.tp_alloc = call_PyType_GenericAlloc;
6041 BufferType.tp_new = call_PyType_GenericNew;
6042 BufferType.tp_free = call_PyObject_Free;
6043#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006044 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006045 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006046#endif
6047
6048 vim_memset(&WindowType, 0, sizeof(WindowType));
6049 WindowType.tp_name = "vim.window";
6050 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006051 WindowType.tp_dealloc = (destructor)WindowDestructor;
6052 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006053 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006054 WindowType.tp_doc = "vim Window object";
6055 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006056 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6057 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006058#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006059 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6060 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006061 WindowType.tp_alloc = call_PyType_GenericAlloc;
6062 WindowType.tp_new = call_PyType_GenericNew;
6063 WindowType.tp_free = call_PyObject_Free;
6064#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006065 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6066 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006067#endif
6068
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006069 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6070 TabPageType.tp_name = "vim.tabpage";
6071 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006072 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6073 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006074 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6075 TabPageType.tp_doc = "vim tab page object";
6076 TabPageType.tp_methods = TabPageMethods;
6077#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006078 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006079 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6080 TabPageType.tp_new = call_PyType_GenericNew;
6081 TabPageType.tp_free = call_PyObject_Free;
6082#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006083 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006084#endif
6085
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006086 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6087 BufMapType.tp_name = "vim.bufferlist";
6088 BufMapType.tp_basicsize = sizeof(BufMapObject);
6089 BufMapType.tp_as_mapping = &BufMapAsMapping;
6090 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006091 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006092 BufferType.tp_doc = "vim buffer list";
6093
6094 vim_memset(&WinListType, 0, sizeof(WinListType));
6095 WinListType.tp_name = "vim.windowlist";
6096 WinListType.tp_basicsize = sizeof(WinListType);
6097 WinListType.tp_as_sequence = &WinListAsSeq;
6098 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6099 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006100 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006101
6102 vim_memset(&TabListType, 0, sizeof(TabListType));
6103 TabListType.tp_name = "vim.tabpagelist";
6104 TabListType.tp_basicsize = sizeof(TabListType);
6105 TabListType.tp_as_sequence = &TabListAsSeq;
6106 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6107 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006108
6109 vim_memset(&RangeType, 0, sizeof(RangeType));
6110 RangeType.tp_name = "vim.range";
6111 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006112 RangeType.tp_dealloc = (destructor)RangeDestructor;
6113 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006114 RangeType.tp_as_sequence = &RangeAsSeq;
6115 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006116 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006117 RangeType.tp_doc = "vim Range object";
6118 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006119 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6120 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006121#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006122 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006123 RangeType.tp_alloc = call_PyType_GenericAlloc;
6124 RangeType.tp_new = call_PyType_GenericNew;
6125 RangeType.tp_free = call_PyObject_Free;
6126#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006127 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006128#endif
6129
6130 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6131 CurrentType.tp_name = "vim.currentdata";
6132 CurrentType.tp_basicsize = sizeof(CurrentObject);
6133 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6134 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006135 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006136#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006137 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6138 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006139#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006140 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6141 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006142#endif
6143
6144 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6145 DictionaryType.tp_name = "vim.dictionary";
6146 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006147 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006148 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006149 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006150 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006151 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6152 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006153 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6154 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6155 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006156#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006157 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6158 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006159#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006160 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6161 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006162#endif
6163
6164 vim_memset(&ListType, 0, sizeof(ListType));
6165 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006166 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006167 ListType.tp_basicsize = sizeof(ListObject);
6168 ListType.tp_as_sequence = &ListAsSeq;
6169 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006170 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006171 ListType.tp_doc = "list pushing modifications to vim structure";
6172 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006173 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006174 ListType.tp_new = (newfunc)ListConstructor;
6175 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006176#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006177 ListType.tp_getattro = (getattrofunc)ListGetattro;
6178 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006179#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006180 ListType.tp_getattr = (getattrfunc)ListGetattr;
6181 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006182#endif
6183
6184 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006185 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006186 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006187 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6188 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006189 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006190 FunctionType.tp_doc = "object that calls vim function";
6191 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006192 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006193 FunctionType.tp_new = (newfunc)FunctionConstructor;
6194 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006195#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006196 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006197#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006198 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006199#endif
6200
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006201 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6202 OptionsType.tp_name = "vim.options";
6203 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006204 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006205 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006206 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006207 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006208 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006209 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6210 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6211 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006212
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006213 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6214 LoaderType.tp_name = "vim.Loader";
6215 LoaderType.tp_basicsize = sizeof(LoaderObject);
6216 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6217 LoaderType.tp_doc = "vim message object";
6218 LoaderType.tp_methods = LoaderMethods;
6219 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6220
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006221#if PY_MAJOR_VERSION >= 3
6222 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6223 vimmodule.m_name = "vim";
6224 vimmodule.m_doc = "Vim Python interface\n";
6225 vimmodule.m_size = -1;
6226 vimmodule.m_methods = VimMethods;
6227#endif
6228}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006229
6230#define PYTYPE_READY(type) \
6231 if (PyType_Ready(&type)) \
6232 return -1;
6233
6234 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006235init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006236{
6237 PYTYPE_READY(IterType);
6238 PYTYPE_READY(BufferType);
6239 PYTYPE_READY(RangeType);
6240 PYTYPE_READY(WindowType);
6241 PYTYPE_READY(TabPageType);
6242 PYTYPE_READY(BufMapType);
6243 PYTYPE_READY(WinListType);
6244 PYTYPE_READY(TabListType);
6245 PYTYPE_READY(CurrentType);
6246 PYTYPE_READY(DictionaryType);
6247 PYTYPE_READY(ListType);
6248 PYTYPE_READY(FunctionType);
6249 PYTYPE_READY(OptionsType);
6250 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006251 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006252 return 0;
6253}
6254
6255 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006256init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006257{
6258 PyObject *path;
6259 PyObject *path_hook;
6260 PyObject *path_hooks;
6261
6262 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6263 return -1;
6264
6265 if (!(path_hooks = PySys_GetObject("path_hooks")))
6266 {
6267 PyErr_Clear();
6268 path_hooks = PyList_New(1);
6269 PyList_SET_ITEM(path_hooks, 0, path_hook);
6270 if (PySys_SetObject("path_hooks", path_hooks))
6271 {
6272 Py_DECREF(path_hooks);
6273 return -1;
6274 }
6275 Py_DECREF(path_hooks);
6276 }
6277 else if (PyList_Check(path_hooks))
6278 {
6279 if (PyList_Append(path_hooks, path_hook))
6280 {
6281 Py_DECREF(path_hook);
6282 return -1;
6283 }
6284 Py_DECREF(path_hook);
6285 }
6286 else
6287 {
6288 VimTryStart();
6289 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6290 "You should now do the following:\n"
6291 "- append vim.path_hook to sys.path_hooks\n"
6292 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6293 VimTryEnd(); /* Discard the error */
6294 Py_DECREF(path_hook);
6295 return 0;
6296 }
6297
6298 if (!(path = PySys_GetObject("path")))
6299 {
6300 PyErr_Clear();
6301 path = PyList_New(1);
6302 Py_INCREF(vim_special_path_object);
6303 PyList_SET_ITEM(path, 0, vim_special_path_object);
6304 if (PySys_SetObject("path", path))
6305 {
6306 Py_DECREF(path);
6307 return -1;
6308 }
6309 Py_DECREF(path);
6310 }
6311 else if (PyList_Check(path))
6312 {
6313 if (PyList_Append(path, vim_special_path_object))
6314 return -1;
6315 }
6316 else
6317 {
6318 VimTryStart();
6319 EMSG(_("Failed to set path: sys.path is not a list\n"
6320 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6321 VimTryEnd(); /* Discard the error */
6322 }
6323
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006324 return 0;
6325}
6326
6327static BufMapObject TheBufferMap =
6328{
6329 PyObject_HEAD_INIT(&BufMapType)
6330};
6331
6332static WinListObject TheWindowList =
6333{
6334 PyObject_HEAD_INIT(&WinListType)
6335 NULL
6336};
6337
6338static CurrentObject TheCurrent =
6339{
6340 PyObject_HEAD_INIT(&CurrentType)
6341};
6342
6343static TabListObject TheTabPageList =
6344{
6345 PyObject_HEAD_INIT(&TabListType)
6346};
6347
6348static struct numeric_constant {
6349 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006350 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006351} numeric_constants[] = {
6352 {"VAR_LOCKED", VAR_LOCKED},
6353 {"VAR_FIXED", VAR_FIXED},
6354 {"VAR_SCOPE", VAR_SCOPE},
6355 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6356};
6357
6358static struct object_constant {
6359 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006360 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006361} object_constants[] = {
6362 {"buffers", (PyObject *)(void *)&TheBufferMap},
6363 {"windows", (PyObject *)(void *)&TheWindowList},
6364 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6365 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006366
6367 {"Buffer", (PyObject *)&BufferType},
6368 {"Range", (PyObject *)&RangeType},
6369 {"Window", (PyObject *)&WindowType},
6370 {"TabPage", (PyObject *)&TabPageType},
6371 {"Dictionary", (PyObject *)&DictionaryType},
6372 {"List", (PyObject *)&ListType},
6373 {"Function", (PyObject *)&FunctionType},
6374 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006375 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006376};
6377
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006378#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006379 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006380 return -1;
6381
6382#define ADD_CHECKED_OBJECT(m, name, obj) \
6383 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006384 PyObject *valObject = obj; \
6385 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006386 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006387 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006388 }
6389
6390 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006391populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006392{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006393 int i;
6394 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006395 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006396 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006397
6398 for (i = 0; i < (int)(sizeof(numeric_constants)
6399 / sizeof(struct numeric_constant));
6400 ++i)
6401 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006402 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006403
6404 for (i = 0; i < (int)(sizeof(object_constants)
6405 / sizeof(struct object_constant));
6406 ++i)
6407 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006408 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006409
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006410 valObject = object_constants[i].valObject;
6411 Py_INCREF(valObject);
6412 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006413 }
6414
6415 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6416 return -1;
6417 ADD_OBJECT(m, "error", VimError);
6418
Bram Moolenaara9922d62013-05-30 13:01:18 +02006419 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6420 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006421 ADD_CHECKED_OBJECT(m, "options",
6422 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006423
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006424 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006425 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006426 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006427
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006428 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006429 return -1;
6430 ADD_OBJECT(m, "_getcwd", py_getcwd)
6431
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006432 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006433 return -1;
6434 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006435 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006436 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006437 if (PyObject_SetAttrString(other_module, "chdir", attr))
6438 {
6439 Py_DECREF(attr);
6440 return -1;
6441 }
6442 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006443
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006444 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006445 {
6446 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006447 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006448 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006449 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6450 {
6451 Py_DECREF(attr);
6452 return -1;
6453 }
6454 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006455 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006456 else
6457 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006458
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006459 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6460 return -1;
6461
6462 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6463
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006464 if (!(imp = PyImport_ImportModule("imp")))
6465 return -1;
6466
6467 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6468 {
6469 Py_DECREF(imp);
6470 return -1;
6471 }
6472
6473 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6474 {
6475 Py_DECREF(py_find_module);
6476 Py_DECREF(imp);
6477 return -1;
6478 }
6479
6480 Py_DECREF(imp);
6481
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006482 ADD_OBJECT(m, "_find_module", py_find_module);
6483 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006484
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006485 return 0;
6486}