blob: e8a5f5de9cc8eced86a2c1e1e27f16d8d686bac3 [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 Moolenaard4247472015-11-02 13:28:59 +0100468AlwaysNone(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 Moolenaard4247472015-11-02 13:28:59 +0100475 static PyObject *
476AlwaysFalse(PyObject *self UNUSED)
477{
478 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100479 PyObject *ret = Py_False;
480 Py_INCREF(ret);
481 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100482}
483
484 static PyObject *
485AlwaysTrue(PyObject *self UNUSED)
486{
487 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100488 PyObject *ret = Py_True;
489 Py_INCREF(ret);
490 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100491}
492
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200493/***************/
494
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200495static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200496 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200497 {"write", (PyCFunction)OutputWrite, METH_O, ""},
498 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100499 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
500 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
501 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
502 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
503 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
504 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200505 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200506 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200507};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200508
509static OutputObject Output =
510{
511 PyObject_HEAD_INIT(&OutputType)
512 0,
513 0
514};
515
516static OutputObject Error =
517{
518 PyObject_HEAD_INIT(&OutputType)
519 0,
520 1
521};
522
523 static int
524PythonIO_Init_io(void)
525{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200526 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
527 return -1;
528 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
529 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200530
531 if (PyErr_Occurred())
532 {
533 EMSG(_("E264: Python: Error initialising I/O objects"));
534 return -1;
535 }
536
537 return 0;
538}
539
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200540typedef struct
541{
542 PyObject_HEAD
543 PyObject *module;
544} LoaderObject;
545static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200546
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200547 static void
548LoaderDestructor(LoaderObject *self)
549{
550 Py_DECREF(self->module);
551 DESTRUCTOR_FINISH(self);
552}
553
554 static PyObject *
555LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
556{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200557 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200558
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200559 Py_INCREF(ret);
560 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200561}
562
563static struct PyMethodDef LoaderMethods[] = {
564 /* name, function, calling, doc */
565 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
566 { NULL, NULL, 0, NULL}
567};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200568
569/* Check to see whether a Vim error has been reported, or a keyboard
570 * interrupt has been detected.
571 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200572
573 static void
574VimTryStart(void)
575{
576 ++trylevel;
577}
578
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200579 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200580VimTryEnd(void)
581{
582 --trylevel;
Bram Moolenaar41009372013-07-01 22:03:04 +0200583 /* Without this it stops processing all subsequent VimL commands and
584 * generates strange error messages if I e.g. try calling Test() in a
585 * cycle */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200586 did_emsg = FALSE;
587 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200588 if (got_int)
589 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100590 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100591 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100592 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200593 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200594 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200595 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100596 else if (msg_list != NULL && *msg_list != NULL)
597 {
598 int should_free;
599 char_u *msg;
600
601 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
602
603 if (msg == NULL)
604 {
605 PyErr_NoMemory();
606 return -1;
607 }
608
609 PyErr_SetVim((char *) msg);
610
611 free_global_msglist();
612
613 if (should_free)
614 vim_free(msg);
615
616 return -1;
617 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200618 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200619 return (PyErr_Occurred() ? -1 : 0);
620 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200621 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200622 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100623 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200624 return -1;
625 }
626 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200627 else
628 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200629 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200630 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200631 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200632 }
633}
634
635 static int
636VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200637{
638 if (got_int)
639 {
640 PyErr_SetNone(PyExc_KeyboardInterrupt);
641 return 1;
642 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200643 return 0;
644}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200645
646/* Vim module - Implementation
647 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200648
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200649 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200650VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200651{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200652 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200653 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200654 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200655
Bram Moolenaar389a1792013-06-23 13:00:44 +0200656 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200657 return NULL;
658
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200659 Py_BEGIN_ALLOW_THREADS
660 Python_Lock_Vim();
661
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200662 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200663 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200664 update_screen(VALID);
665
666 Python_Release_Vim();
667 Py_END_ALLOW_THREADS
668
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200669 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200670 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200671 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200672 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200673
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200674 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200675 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200676 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200677}
678
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200679/*
680 * Function to translate a typval_T into a PyObject; this will recursively
681 * translate lists/dictionaries into their Python equivalents.
682 *
683 * The depth parameter is to avoid infinite recursion, set it to 1 when
684 * you call VimToPython.
685 */
686 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200687VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200688{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200689 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200690 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200691 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200692
693 /* Avoid infinite recursion */
694 if (depth > 100)
695 {
696 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200697 ret = Py_None;
698 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200699 }
700
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200701 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200702 * then and we can use it again. */
703 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
704 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
705 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200706 sprintf(ptrBuf, "%p",
707 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
708 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200709
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200710 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200711 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 Py_INCREF(ret);
713 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200714 }
715 }
716
717 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200718 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200719 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200720 else if (our_tv->v_type == VAR_NUMBER)
721 {
722 char buf[NUMBUFLEN];
723
724 /* For backwards compatibility numbers are stored as strings. */
725 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200726 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200727 }
728# ifdef FEAT_FLOAT
729 else if (our_tv->v_type == VAR_FLOAT)
730 {
731 char buf[NUMBUFLEN];
732
733 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200734 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200735 }
736# endif
737 else if (our_tv->v_type == VAR_LIST)
738 {
739 list_T *list = our_tv->vval.v_list;
740 listitem_T *curr;
741
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200742 if (list == NULL)
743 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200744
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200745 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200746 return NULL;
747
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200748 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200749 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200750 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200751 return NULL;
752 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200753
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200754 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
755 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200756 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200757 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200758 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200759 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200760 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200761 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200762 {
763 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200764 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200765 return NULL;
766 }
767 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200768 }
769 }
770 else if (our_tv->v_type == VAR_DICT)
771 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200772
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100773 hashtab_T *ht;
774 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200775 hashitem_T *hi;
776 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100777
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200778 if (our_tv->vval.v_dict == NULL)
779 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100780 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200781
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200782 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200783 return NULL;
784
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200785 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200786 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200787 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200788 return NULL;
789 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200790
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100791 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200792 for (hi = ht->ht_array; todo > 0; ++hi)
793 {
794 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200796 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200797
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200798 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200799 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200800 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200801 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200802 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200803 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200804 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200805 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200806 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200807 Py_DECREF(newObj);
808 return NULL;
809 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810 }
811 }
812 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100813 else if (our_tv->v_type == VAR_SPECIAL)
814 {
815 if (our_tv->vval.v_number == VVAL_FALSE)
816 {
817 ret = Py_False;
818 Py_INCREF(ret);
819 }
820 else if (our_tv->vval.v_number == VVAL_TRUE)
821 {
822 ret = Py_True;
823 Py_INCREF(ret);
824 }
825 else
826 {
827 Py_INCREF(Py_None);
828 ret = Py_None;
829 }
830 return ret;
831 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200832 else
833 {
834 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200835 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200836 }
837
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200838 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200839}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200840
841 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200842VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200843{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200844 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200845 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200846 PyObject *string;
847 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200848 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200849 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200850
Bram Moolenaar389a1792013-06-23 13:00:44 +0200851 if (!PyArg_ParseTuple(args, "O", &string))
852 return NULL;
853
854 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200855 return NULL;
856
857 Py_BEGIN_ALLOW_THREADS
858 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200859 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200860 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200861 Python_Release_Vim();
862 Py_END_ALLOW_THREADS
863
Bram Moolenaar389a1792013-06-23 13:00:44 +0200864 Py_XDECREF(todecref);
865
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200866 if (VimTryEnd())
867 return NULL;
868
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200869 if (our_tv == NULL)
870 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200871 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200872 return NULL;
873 }
874
875 /* Convert the Vim type into a Python type. Create a dictionary that's
876 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200877 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200878 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200879 else
880 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200881 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200882 Py_DECREF(lookup_dict);
883 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200884
885
886 Py_BEGIN_ALLOW_THREADS
887 Python_Lock_Vim();
888 free_tv(our_tv);
889 Python_Release_Vim();
890 Py_END_ALLOW_THREADS
891
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200892 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200893}
894
Bram Moolenaardb913952012-06-29 12:54:53 +0200895static PyObject *ConvertToPyObject(typval_T *);
896
897 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200898VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200899{
Bram Moolenaardb913952012-06-29 12:54:53 +0200900 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200901 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200902 char_u *expr;
903 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200904
Bram Moolenaar389a1792013-06-23 13:00:44 +0200905 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200906 return NULL;
907
908 Py_BEGIN_ALLOW_THREADS
909 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200910 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200911 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200912 Python_Release_Vim();
913 Py_END_ALLOW_THREADS
914
Bram Moolenaar389a1792013-06-23 13:00:44 +0200915 Py_XDECREF(todecref);
916
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200917 if (VimTryEnd())
918 return NULL;
919
Bram Moolenaardb913952012-06-29 12:54:53 +0200920 if (our_tv == NULL)
921 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200922 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200923 return NULL;
924 }
925
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200926 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200927 Py_BEGIN_ALLOW_THREADS
928 Python_Lock_Vim();
929 free_tv(our_tv);
930 Python_Release_Vim();
931 Py_END_ALLOW_THREADS
932
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200933 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200934}
935
936 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200937VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200938{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200939 char_u *str;
940 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200941 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200942
Bram Moolenaar389a1792013-06-23 13:00:44 +0200943 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200944 return NULL;
945
Bram Moolenaara54bf402012-12-05 16:30:07 +0100946#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200947 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100948#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200949 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100950#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200951
952 Py_XDECREF(todecref);
953
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200954 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200955}
956
Bram Moolenaarf4258302013-06-02 18:20:17 +0200957 static PyObject *
958_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
959{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200960 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200961 PyObject *newwd;
962 PyObject *todecref;
963 char_u *new_dir;
964
Bram Moolenaard4209d22013-06-05 20:34:15 +0200965 if (_chdir == NULL)
966 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200967 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200968 return NULL;
969
970 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
971 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200972 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200973 return NULL;
974 }
975
976 if (!(new_dir = StringToChars(newwd, &todecref)))
977 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200978 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200979 Py_DECREF(newwd);
980 return NULL;
981 }
982
983 VimTryStart();
984
985 if (vim_chdir(new_dir))
986 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200987 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200988 Py_DECREF(newwd);
989 Py_XDECREF(todecref);
990
991 if (VimTryEnd())
992 return NULL;
993
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200994 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200995 return NULL;
996 }
997
998 Py_DECREF(newwd);
999 Py_XDECREF(todecref);
1000
1001 post_chdir(FALSE);
1002
1003 if (VimTryEnd())
1004 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001005 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001006 return NULL;
1007 }
1008
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001009 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001010}
1011
1012 static PyObject *
1013VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1014{
1015 return _VimChdir(py_chdir, args, kwargs);
1016}
1017
1018 static PyObject *
1019VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1020{
1021 return _VimChdir(py_fchdir, args, kwargs);
1022}
1023
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001024typedef struct {
1025 PyObject *callable;
1026 PyObject *result;
1027} map_rtp_data;
1028
1029 static void
1030map_rtp_callback(char_u *path, void *_data)
1031{
1032 void **data = (void **) _data;
1033 PyObject *pathObject;
1034 map_rtp_data *mr_data = *((map_rtp_data **) data);
1035
Bram Moolenaar41009372013-07-01 22:03:04 +02001036 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001037 {
1038 *data = NULL;
1039 return;
1040 }
1041
1042 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1043 pathObject, NULL);
1044
1045 Py_DECREF(pathObject);
1046
1047 if (!mr_data->result || mr_data->result != Py_None)
1048 *data = NULL;
1049 else
1050 {
1051 Py_DECREF(mr_data->result);
1052 mr_data->result = NULL;
1053 }
1054}
1055
1056 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001057VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001058{
1059 map_rtp_data data;
1060
Bram Moolenaar389a1792013-06-23 13:00:44 +02001061 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001062 data.result = NULL;
1063
1064 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
1065
1066 if (data.result == NULL)
1067 {
1068 if (PyErr_Occurred())
1069 return NULL;
1070 else
1071 {
1072 Py_INCREF(Py_None);
1073 return Py_None;
1074 }
1075 }
1076 return data.result;
1077}
1078
1079/*
1080 * _vim_runtimepath_ special path implementation.
1081 */
1082
1083 static void
1084map_finder_callback(char_u *path, void *_data)
1085{
1086 void **data = (void **) _data;
1087 PyObject *list = *((PyObject **) data);
1088 PyObject *pathObject1, *pathObject2;
1089 char *pathbuf;
1090 size_t pathlen;
1091
1092 pathlen = STRLEN(path);
1093
1094#if PY_MAJOR_VERSION < 3
1095# define PY_MAIN_DIR_STRING "python2"
1096#else
1097# define PY_MAIN_DIR_STRING "python3"
1098#endif
1099#define PY_ALTERNATE_DIR_STRING "pythonx"
1100
1101#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1102 if (!(pathbuf = PyMem_New(char,
1103 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1104 {
1105 PyErr_NoMemory();
1106 *data = NULL;
1107 return;
1108 }
1109
1110 mch_memmove(pathbuf, path, pathlen + 1);
1111 add_pathsep((char_u *) pathbuf);
1112
1113 pathlen = STRLEN(pathbuf);
1114 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1115 PYTHONX_STRING_LENGTH + 1);
1116
1117 if (!(pathObject1 = PyString_FromString(pathbuf)))
1118 {
1119 *data = NULL;
1120 PyMem_Free(pathbuf);
1121 return;
1122 }
1123
1124 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1125 PYTHONX_STRING_LENGTH + 1);
1126
1127 if (!(pathObject2 = PyString_FromString(pathbuf)))
1128 {
1129 Py_DECREF(pathObject1);
1130 PyMem_Free(pathbuf);
1131 *data = NULL;
1132 return;
1133 }
1134
1135 PyMem_Free(pathbuf);
1136
1137 if (PyList_Append(list, pathObject1)
1138 || PyList_Append(list, pathObject2))
1139 *data = NULL;
1140
1141 Py_DECREF(pathObject1);
1142 Py_DECREF(pathObject2);
1143}
1144
1145 static PyObject *
1146Vim_GetPaths(PyObject *self UNUSED)
1147{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001148 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001149
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001150 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001151 return NULL;
1152
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001153 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001154
1155 if (PyErr_Occurred())
1156 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001157 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001158 return NULL;
1159 }
1160
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001161 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001162}
1163
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001164 static PyObject *
1165call_load_module(char *name, int len, PyObject *find_module_result)
1166{
1167 PyObject *fd, *pathname, *description;
1168
Bram Moolenaarc476e522013-06-23 13:46:40 +02001169 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001170 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001171 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001172 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001173 Py_TYPE_NAME(find_module_result));
1174 return NULL;
1175 }
1176 if (PyTuple_GET_SIZE(find_module_result) != 3)
1177 {
1178 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001179 N_("expected 3-tuple as imp.find_module() result, but got "
1180 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001181 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001182 return NULL;
1183 }
1184
1185 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1186 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1187 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1188 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001189 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001190 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001191 return NULL;
1192 }
1193
1194 return PyObject_CallFunction(py_load_module,
1195 "s#OOO", name, len, fd, pathname, description);
1196}
1197
1198 static PyObject *
1199find_module(char *fullname, char *tail, PyObject *new_path)
1200{
1201 PyObject *find_module_result;
1202 PyObject *module;
1203 char *dot;
1204
Bram Moolenaar41009372013-07-01 22:03:04 +02001205 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001206 {
1207 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001208 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001209 * first component
1210 */
1211 PyObject *newest_path;
1212 int partlen = (int) (dot - 1 - tail);
1213
1214 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1215 "s#O", tail, partlen, new_path)))
1216 return NULL;
1217
1218 if (!(module = call_load_module(
1219 fullname,
1220 ((int) (tail - fullname)) + partlen,
1221 find_module_result)))
1222 {
1223 Py_DECREF(find_module_result);
1224 return NULL;
1225 }
1226
1227 Py_DECREF(find_module_result);
1228
1229 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1230 {
1231 Py_DECREF(module);
1232 return NULL;
1233 }
1234
1235 Py_DECREF(module);
1236
1237 module = find_module(fullname, dot + 1, newest_path);
1238
1239 Py_DECREF(newest_path);
1240
1241 return module;
1242 }
1243 else
1244 {
1245 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1246 "sO", tail, new_path)))
1247 return NULL;
1248
1249 if (!(module = call_load_module(
1250 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001251 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001252 find_module_result)))
1253 {
1254 Py_DECREF(find_module_result);
1255 return NULL;
1256 }
1257
1258 Py_DECREF(find_module_result);
1259
1260 return module;
1261 }
1262}
1263
1264 static PyObject *
1265FinderFindModule(PyObject *self, PyObject *args)
1266{
1267 char *fullname;
1268 PyObject *module;
1269 PyObject *new_path;
1270 LoaderObject *loader;
1271
1272 if (!PyArg_ParseTuple(args, "s", &fullname))
1273 return NULL;
1274
1275 if (!(new_path = Vim_GetPaths(self)))
1276 return NULL;
1277
1278 module = find_module(fullname, fullname, new_path);
1279
1280 Py_DECREF(new_path);
1281
1282 if (!module)
1283 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001284 if (PyErr_Occurred())
1285 {
1286 if (PyErr_ExceptionMatches(PyExc_ImportError))
1287 PyErr_Clear();
1288 else
1289 return NULL;
1290 }
1291
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001292 Py_INCREF(Py_None);
1293 return Py_None;
1294 }
1295
1296 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1297 {
1298 Py_DECREF(module);
1299 return NULL;
1300 }
1301
1302 loader->module = module;
1303
1304 return (PyObject *) loader;
1305}
1306
1307 static PyObject *
1308VimPathHook(PyObject *self UNUSED, PyObject *args)
1309{
1310 char *path;
1311
1312 if (PyArg_ParseTuple(args, "s", &path)
1313 && STRCMP(path, vim_special_path) == 0)
1314 {
1315 Py_INCREF(vim_module);
1316 return vim_module;
1317 }
1318
1319 PyErr_Clear();
1320 PyErr_SetNone(PyExc_ImportError);
1321 return NULL;
1322}
1323
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001324/*
1325 * Vim module - Definitions
1326 */
1327
1328static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001329 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001330 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001331 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001332 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1333 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001334 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1335 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001336 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001337 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001338 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1339 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1340 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001341};
1342
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001343/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001344 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001345 */
1346
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001347static PyTypeObject IterType;
1348
1349typedef PyObject *(*nextfun)(void **);
1350typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001351typedef int (*traversefun)(void *, visitproc, void *);
1352typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001353
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001354/* Main purpose of this object is removing the need for do python
1355 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1356 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001357
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001358typedef struct
1359{
1360 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001361 void *cur;
1362 nextfun next;
1363 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001364 traversefun traverse;
1365 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001366} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001367
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001368 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001369IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1370 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001371{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001372 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001373
Bram Moolenaar774267b2013-05-21 20:51:59 +02001374 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001375 self->cur = start;
1376 self->next = next;
1377 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001378 self->traverse = traverse;
1379 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001380
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001381 return (PyObject *)(self);
1382}
1383
1384 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001385IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001386{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001387 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001388 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001389 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001390}
1391
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001392 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001393IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001394{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001395 if (self->traverse != NULL)
1396 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001397 else
1398 return 0;
1399}
1400
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001401/* Mac OSX defines clear() somewhere. */
1402#ifdef clear
1403# undef clear
1404#endif
1405
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001406 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001407IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001408{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001409 if (self->clear != NULL)
1410 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001411 else
1412 return 0;
1413}
1414
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001415 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001416IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001417{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001418 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001419}
1420
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001421 static PyObject *
1422IterIter(PyObject *self)
1423{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001424 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001425 return self;
1426}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001427
Bram Moolenaardb913952012-06-29 12:54:53 +02001428typedef struct pylinkedlist_S {
1429 struct pylinkedlist_S *pll_next;
1430 struct pylinkedlist_S *pll_prev;
1431 PyObject *pll_obj;
1432} pylinkedlist_T;
1433
1434static pylinkedlist_T *lastdict = NULL;
1435static pylinkedlist_T *lastlist = NULL;
1436
1437 static void
1438pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1439{
1440 if (ref->pll_prev == NULL)
1441 {
1442 if (ref->pll_next == NULL)
1443 {
1444 *last = NULL;
1445 return;
1446 }
1447 }
1448 else
1449 ref->pll_prev->pll_next = ref->pll_next;
1450
1451 if (ref->pll_next == NULL)
1452 *last = ref->pll_prev;
1453 else
1454 ref->pll_next->pll_prev = ref->pll_prev;
1455}
1456
1457 static void
1458pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1459{
1460 if (*last == NULL)
1461 ref->pll_prev = NULL;
1462 else
1463 {
1464 (*last)->pll_next = ref;
1465 ref->pll_prev = *last;
1466 }
1467 ref->pll_next = NULL;
1468 ref->pll_obj = self;
1469 *last = ref;
1470}
1471
1472static PyTypeObject DictionaryType;
1473
1474typedef struct
1475{
1476 PyObject_HEAD
1477 dict_T *dict;
1478 pylinkedlist_T ref;
1479} DictionaryObject;
1480
Bram Moolenaara9922d62013-05-30 13:01:18 +02001481static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1482
1483#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1484
Bram Moolenaardb913952012-06-29 12:54:53 +02001485 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001486DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001487{
1488 DictionaryObject *self;
1489
Bram Moolenaara9922d62013-05-30 13:01:18 +02001490 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001491 if (self == NULL)
1492 return NULL;
1493 self->dict = dict;
1494 ++dict->dv_refcount;
1495
1496 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1497
1498 return (PyObject *)(self);
1499}
1500
Bram Moolenaara9922d62013-05-30 13:01:18 +02001501 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001502py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001503{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001504 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001505
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001506 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001507 {
1508 PyErr_NoMemory();
1509 return NULL;
1510 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001511 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001512
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001513 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001514}
1515
1516 static PyObject *
1517DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1518{
1519 DictionaryObject *self;
1520 dict_T *dict;
1521
1522 if (!(dict = py_dict_alloc()))
1523 return NULL;
1524
1525 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1526
1527 --dict->dv_refcount;
1528
1529 if (kwargs || PyTuple_Size(args))
1530 {
1531 PyObject *tmp;
1532 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1533 {
1534 Py_DECREF(self);
1535 return NULL;
1536 }
1537
1538 Py_DECREF(tmp);
1539 }
1540
1541 return (PyObject *)(self);
1542}
1543
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001544 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001545DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001546{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001547 pyll_remove(&self->ref, &lastdict);
1548 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001549
1550 DESTRUCTOR_FINISH(self);
1551}
1552
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001553static char *DictionaryAttrs[] = {
1554 "locked", "scope",
1555 NULL
1556};
1557
1558 static PyObject *
1559DictionaryDir(PyObject *self)
1560{
1561 return ObjectDir(self, DictionaryAttrs);
1562}
1563
Bram Moolenaardb913952012-06-29 12:54:53 +02001564 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001565DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001566{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001567 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001568 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001569 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001570 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001571 return -1;
1572 }
1573
1574 if (strcmp(name, "locked") == 0)
1575 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001576 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001577 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001578 PyErr_SET_STRING(PyExc_TypeError,
1579 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001580 return -1;
1581 }
1582 else
1583 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001584 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001585 if (istrue == -1)
1586 return -1;
1587 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001588 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001589 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001590 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001591 }
1592 return 0;
1593 }
1594 else
1595 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001596 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001597 return -1;
1598 }
1599}
1600
1601 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001602DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001603{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001604 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001605}
1606
Bram Moolenaara9922d62013-05-30 13:01:18 +02001607#define DICT_FLAG_HAS_DEFAULT 0x01
1608#define DICT_FLAG_POP 0x02
1609#define DICT_FLAG_NONE_DEFAULT 0x04
1610#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1611#define DICT_FLAG_RETURN_PAIR 0x10
1612
Bram Moolenaardb913952012-06-29 12:54:53 +02001613 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001614_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001615{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001616 PyObject *keyObject;
1617 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001618 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001619 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001620 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001621 dict_T *dict = self->dict;
1622 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001623 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001624
Bram Moolenaara9922d62013-05-30 13:01:18 +02001625 if (flags & DICT_FLAG_HAS_DEFAULT)
1626 {
1627 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1628 return NULL;
1629 }
1630 else
1631 keyObject = args;
1632
1633 if (flags & DICT_FLAG_RETURN_BOOL)
1634 defObject = Py_False;
1635
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001636 if (!(key = StringToChars(keyObject, &todecref)))
1637 return NULL;
1638
1639 if (*key == NUL)
1640 {
1641 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001642 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001643 return NULL;
1644 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001645
Bram Moolenaara9922d62013-05-30 13:01:18 +02001646 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001647
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001648 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001649
Bram Moolenaara9922d62013-05-30 13:01:18 +02001650 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001651 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001652 if (defObject)
1653 {
1654 Py_INCREF(defObject);
1655 return defObject;
1656 }
1657 else
1658 {
1659 PyErr_SetObject(PyExc_KeyError, keyObject);
1660 return NULL;
1661 }
1662 }
1663 else if (flags & DICT_FLAG_RETURN_BOOL)
1664 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001665 ret = Py_True;
1666 Py_INCREF(ret);
1667 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001668 }
1669
1670 di = dict_lookup(hi);
1671
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001672 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001673 return NULL;
1674
1675 if (flags & DICT_FLAG_POP)
1676 {
1677 if (dict->dv_lock)
1678 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001679 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001680 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001681 return NULL;
1682 }
1683
1684 hash_remove(&dict->dv_hashtab, hi);
1685 dictitem_free(di);
1686 }
1687
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001688 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001689}
1690
1691 static PyObject *
1692DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1693{
1694 return _DictionaryItem(self, keyObject, 0);
1695}
1696
1697 static int
1698DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1699{
1700 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001701 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001702
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001703 if (rObj == NULL)
1704 return -1;
1705
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001706 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001707
Bram Moolenaardee2e312013-06-23 16:35:47 +02001708 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001709
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001710 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001711}
1712
1713typedef struct
1714{
1715 hashitem_T *ht_array;
1716 long_u ht_used;
1717 hashtab_T *ht;
1718 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001719 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001720} dictiterinfo_T;
1721
1722 static PyObject *
1723DictionaryIterNext(dictiterinfo_T **dii)
1724{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001725 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001726
1727 if (!(*dii)->todo)
1728 return NULL;
1729
1730 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1731 (*dii)->ht->ht_used != (*dii)->ht_used)
1732 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001733 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001734 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001735 return NULL;
1736 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001737
Bram Moolenaara9922d62013-05-30 13:01:18 +02001738 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1739 ++((*dii)->hi);
1740
1741 --((*dii)->todo);
1742
Bram Moolenaar41009372013-07-01 22:03:04 +02001743 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001744 return NULL;
1745
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001746 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001747}
1748
1749 static PyObject *
1750DictionaryIter(DictionaryObject *self)
1751{
1752 dictiterinfo_T *dii;
1753 hashtab_T *ht;
1754
1755 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1756 {
1757 PyErr_NoMemory();
1758 return NULL;
1759 }
1760
1761 ht = &self->dict->dv_hashtab;
1762 dii->ht_array = ht->ht_array;
1763 dii->ht_used = ht->ht_used;
1764 dii->ht = ht;
1765 dii->hi = dii->ht_array;
1766 dii->todo = dii->ht_used;
1767
1768 return IterNew(dii,
1769 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1770 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001771}
1772
1773 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001774DictionaryAssItem(
1775 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001776{
1777 char_u *key;
1778 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001779 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001780 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001781 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001782
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001783 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001784 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001785 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001786 return -1;
1787 }
1788
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001789 if (!(key = StringToChars(keyObject, &todecref)))
1790 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001791
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001792 if (*key == NUL)
1793 {
1794 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001795 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001796 return -1;
1797 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001798
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001799 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001800
1801 if (valObject == NULL)
1802 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001803 hashitem_T *hi;
1804
Bram Moolenaardb913952012-06-29 12:54:53 +02001805 if (di == NULL)
1806 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001807 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001808 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001809 return -1;
1810 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001811 hi = hash_find(&dict->dv_hashtab, di->di_key);
1812 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001813 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001814 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001815 return 0;
1816 }
1817
1818 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001819 {
1820 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001821 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001822 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001823
1824 if (di == NULL)
1825 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001826 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001827 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001828 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001829 PyErr_NoMemory();
1830 return -1;
1831 }
1832 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001833 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001834
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001835 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001836 {
1837 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001838 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001839 RAISE_KEY_ADD_FAIL(key);
1840 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001841 return -1;
1842 }
1843 }
1844 else
1845 clear_tv(&di->di_tv);
1846
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001847 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001848
1849 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001850 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001851 return 0;
1852}
1853
Bram Moolenaara9922d62013-05-30 13:01:18 +02001854typedef PyObject *(*hi_to_py)(hashitem_T *);
1855
Bram Moolenaardb913952012-06-29 12:54:53 +02001856 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001857DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001858{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001859 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001860 long_u todo = dict->dv_hashtab.ht_used;
1861 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001862 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001863 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001864 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001865
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001866 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001867 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1868 {
1869 if (!HASHITEM_EMPTY(hi))
1870 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001871 if (!(newObj = hiconvert(hi)))
1872 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001873 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001874 return NULL;
1875 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001876 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001877 --todo;
1878 ++i;
1879 }
1880 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001881 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001882}
1883
Bram Moolenaara9922d62013-05-30 13:01:18 +02001884 static PyObject *
1885dict_key(hashitem_T *hi)
1886{
1887 return PyBytes_FromString((char *)(hi->hi_key));
1888}
1889
1890 static PyObject *
1891DictionaryListKeys(DictionaryObject *self)
1892{
1893 return DictionaryListObjects(self, dict_key);
1894}
1895
1896 static PyObject *
1897dict_val(hashitem_T *hi)
1898{
1899 dictitem_T *di;
1900
1901 di = dict_lookup(hi);
1902 return ConvertToPyObject(&di->di_tv);
1903}
1904
1905 static PyObject *
1906DictionaryListValues(DictionaryObject *self)
1907{
1908 return DictionaryListObjects(self, dict_val);
1909}
1910
1911 static PyObject *
1912dict_item(hashitem_T *hi)
1913{
1914 PyObject *keyObject;
1915 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001916 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001917
1918 if (!(keyObject = dict_key(hi)))
1919 return NULL;
1920
1921 if (!(valObject = dict_val(hi)))
1922 {
1923 Py_DECREF(keyObject);
1924 return NULL;
1925 }
1926
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001927 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001928
1929 Py_DECREF(keyObject);
1930 Py_DECREF(valObject);
1931
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001932 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001933}
1934
1935 static PyObject *
1936DictionaryListItems(DictionaryObject *self)
1937{
1938 return DictionaryListObjects(self, dict_item);
1939}
1940
1941 static PyObject *
1942DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1943{
1944 dict_T *dict = self->dict;
1945
1946 if (dict->dv_lock)
1947 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001948 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001949 return NULL;
1950 }
1951
1952 if (kwargs)
1953 {
1954 typval_T tv;
1955
1956 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1957 return NULL;
1958
1959 VimTryStart();
1960 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1961 clear_tv(&tv);
1962 if (VimTryEnd())
1963 return NULL;
1964 }
1965 else
1966 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001967 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001968
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001969 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001970 return NULL;
1971
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001972 if (obj == NULL)
1973 {
1974 Py_INCREF(Py_None);
1975 return Py_None;
1976 }
1977
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001978 if (PyObject_HasAttrString(obj, "keys"))
1979 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001980 else
1981 {
1982 PyObject *iterator;
1983 PyObject *item;
1984
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001985 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001986 return NULL;
1987
1988 while ((item = PyIter_Next(iterator)))
1989 {
1990 PyObject *fast;
1991 PyObject *keyObject;
1992 PyObject *valObject;
1993 PyObject *todecref;
1994 char_u *key;
1995 dictitem_T *di;
1996
1997 if (!(fast = PySequence_Fast(item, "")))
1998 {
1999 Py_DECREF(iterator);
2000 Py_DECREF(item);
2001 return NULL;
2002 }
2003
2004 Py_DECREF(item);
2005
2006 if (PySequence_Fast_GET_SIZE(fast) != 2)
2007 {
2008 Py_DECREF(iterator);
2009 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002010 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002011 N_("expected sequence element of size 2, "
2012 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002013 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002014 return NULL;
2015 }
2016
2017 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2018
2019 if (!(key = StringToChars(keyObject, &todecref)))
2020 {
2021 Py_DECREF(iterator);
2022 Py_DECREF(fast);
2023 return NULL;
2024 }
2025
2026 di = dictitem_alloc(key);
2027
2028 Py_XDECREF(todecref);
2029
2030 if (di == NULL)
2031 {
2032 Py_DECREF(fast);
2033 Py_DECREF(iterator);
2034 PyErr_NoMemory();
2035 return NULL;
2036 }
2037 di->di_tv.v_lock = 0;
2038 di->di_tv.v_type = VAR_UNKNOWN;
2039
2040 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2041
2042 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2043 {
2044 Py_DECREF(iterator);
2045 Py_DECREF(fast);
2046 dictitem_free(di);
2047 return NULL;
2048 }
2049
2050 Py_DECREF(fast);
2051
2052 if (dict_add(dict, di) == FAIL)
2053 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002054 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002055 Py_DECREF(iterator);
2056 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002057 return NULL;
2058 }
2059 }
2060
2061 Py_DECREF(iterator);
2062
2063 /* Iterator may have finished due to an exception */
2064 if (PyErr_Occurred())
2065 return NULL;
2066 }
2067 }
2068 Py_INCREF(Py_None);
2069 return Py_None;
2070}
2071
2072 static PyObject *
2073DictionaryGet(DictionaryObject *self, PyObject *args)
2074{
2075 return _DictionaryItem(self, args,
2076 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2077}
2078
2079 static PyObject *
2080DictionaryPop(DictionaryObject *self, PyObject *args)
2081{
2082 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2083}
2084
2085 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002086DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002087{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002088 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002089 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002090 PyObject *valObject;
2091 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002092
Bram Moolenaarde71b562013-06-02 17:41:54 +02002093 if (self->dict->dv_hashtab.ht_used == 0)
2094 {
2095 PyErr_SetNone(PyExc_KeyError);
2096 return NULL;
2097 }
2098
2099 hi = self->dict->dv_hashtab.ht_array;
2100 while (HASHITEM_EMPTY(hi))
2101 ++hi;
2102
2103 di = dict_lookup(hi);
2104
2105 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002106 return NULL;
2107
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002108 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002109 {
2110 Py_DECREF(valObject);
2111 return NULL;
2112 }
2113
2114 hash_remove(&self->dict->dv_hashtab, hi);
2115 dictitem_free(di);
2116
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002117 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002118}
2119
2120 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002121DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002122{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002123 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2124}
2125
2126static PySequenceMethods DictionaryAsSeq = {
2127 0, /* sq_length */
2128 0, /* sq_concat */
2129 0, /* sq_repeat */
2130 0, /* sq_item */
2131 0, /* sq_slice */
2132 0, /* sq_ass_item */
2133 0, /* sq_ass_slice */
2134 (objobjproc) DictionaryContains, /* sq_contains */
2135 0, /* sq_inplace_concat */
2136 0, /* sq_inplace_repeat */
2137};
2138
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002139static PyMappingMethods DictionaryAsMapping = {
2140 (lenfunc) DictionaryLength,
2141 (binaryfunc) DictionaryItem,
2142 (objobjargproc) DictionaryAssItem,
2143};
2144
Bram Moolenaardb913952012-06-29 12:54:53 +02002145static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002146 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002147 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2148 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2149 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2150 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2151 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002152 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002153 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002154 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2155 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002156};
2157
2158static PyTypeObject ListType;
2159
2160typedef struct
2161{
2162 PyObject_HEAD
2163 list_T *list;
2164 pylinkedlist_T ref;
2165} ListObject;
2166
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002167#define NEW_LIST(list) ListNew(&ListType, list)
2168
Bram Moolenaardb913952012-06-29 12:54:53 +02002169 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002170ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002171{
2172 ListObject *self;
2173
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002174 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002175 if (self == NULL)
2176 return NULL;
2177 self->list = list;
2178 ++list->lv_refcount;
2179
2180 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2181
2182 return (PyObject *)(self);
2183}
2184
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002185 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002186py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002187{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002188 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002189
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002190 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002191 {
2192 PyErr_NoMemory();
2193 return NULL;
2194 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002195 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002196
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002197 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002198}
2199
2200 static int
2201list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2202{
2203 PyObject *iterator;
2204 PyObject *item;
2205 listitem_T *li;
2206
2207 if (!(iterator = PyObject_GetIter(obj)))
2208 return -1;
2209
2210 while ((item = PyIter_Next(iterator)))
2211 {
2212 if (!(li = listitem_alloc()))
2213 {
2214 PyErr_NoMemory();
2215 Py_DECREF(item);
2216 Py_DECREF(iterator);
2217 return -1;
2218 }
2219 li->li_tv.v_lock = 0;
2220 li->li_tv.v_type = VAR_UNKNOWN;
2221
2222 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2223 {
2224 Py_DECREF(item);
2225 Py_DECREF(iterator);
2226 listitem_free(li);
2227 return -1;
2228 }
2229
2230 Py_DECREF(item);
2231
2232 list_append(l, li);
2233 }
2234
2235 Py_DECREF(iterator);
2236
2237 /* Iterator may have finished due to an exception */
2238 if (PyErr_Occurred())
2239 return -1;
2240
2241 return 0;
2242}
2243
2244 static PyObject *
2245ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2246{
2247 list_T *list;
2248 PyObject *obj = NULL;
2249
2250 if (kwargs)
2251 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002252 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002253 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002254 return NULL;
2255 }
2256
2257 if (!PyArg_ParseTuple(args, "|O", &obj))
2258 return NULL;
2259
2260 if (!(list = py_list_alloc()))
2261 return NULL;
2262
2263 if (obj)
2264 {
2265 PyObject *lookup_dict;
2266
2267 if (!(lookup_dict = PyDict_New()))
2268 {
2269 list_unref(list);
2270 return NULL;
2271 }
2272
2273 if (list_py_concat(list, obj, lookup_dict) == -1)
2274 {
2275 Py_DECREF(lookup_dict);
2276 list_unref(list);
2277 return NULL;
2278 }
2279
2280 Py_DECREF(lookup_dict);
2281 }
2282
2283 return ListNew(subtype, list);
2284}
2285
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002286 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002287ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002288{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002289 pyll_remove(&self->ref, &lastlist);
2290 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002291
2292 DESTRUCTOR_FINISH(self);
2293}
2294
Bram Moolenaardb913952012-06-29 12:54:53 +02002295 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002296ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002297{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002298 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002299}
2300
2301 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002302ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002303{
2304 listitem_T *li;
2305
Bram Moolenaard6e39182013-05-21 18:30:34 +02002306 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002307 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002308 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002309 return NULL;
2310 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002311 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002312 if (li == NULL)
2313 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002314 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002315 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002316 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002317 return NULL;
2318 }
2319 return ConvertToPyObject(&li->li_tv);
2320}
2321
Bram Moolenaardb913952012-06-29 12:54:53 +02002322 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002323ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2324 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002325{
2326 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002327 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002328
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002329 if (step == 0)
2330 {
2331 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2332 return NULL;
2333 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002334
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002335 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002336 if (list == NULL)
2337 return NULL;
2338
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002339 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002340 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002341 PyObject *item;
2342
2343 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002344 if (item == NULL)
2345 {
2346 Py_DECREF(list);
2347 return NULL;
2348 }
2349
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002350 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002351 }
2352
2353 return list;
2354}
2355
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002356 static PyObject *
2357ListItem(ListObject *self, PyObject* idx)
2358{
2359#if PY_MAJOR_VERSION < 3
2360 if (PyInt_Check(idx))
2361 {
2362 long _idx = PyInt_AsLong(idx);
2363 return ListIndex(self, _idx);
2364 }
2365 else
2366#endif
2367 if (PyLong_Check(idx))
2368 {
2369 long _idx = PyLong_AsLong(idx);
2370 return ListIndex(self, _idx);
2371 }
2372 else if (PySlice_Check(idx))
2373 {
2374 Py_ssize_t start, stop, step, slicelen;
2375
Bram Moolenaar922a4662014-03-30 16:11:43 +02002376 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002377 &start, &stop, &step, &slicelen) < 0)
2378 return NULL;
2379 return ListSlice(self, start, step, slicelen);
2380 }
2381 else
2382 {
2383 RAISE_INVALID_INDEX_TYPE(idx);
2384 return NULL;
2385 }
2386}
2387
2388 static void
2389list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2390 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2391{
2392 while (numreplaced--)
2393 {
2394 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2395 listitem_remove(l, lis[slicelen + numreplaced]);
2396 }
2397 while (numadded--)
2398 {
2399 listitem_T *next;
2400
2401 next = lastaddedli->li_prev;
2402 listitem_remove(l, lastaddedli);
2403 lastaddedli = next;
2404 }
2405}
2406
2407 static int
2408ListAssSlice(ListObject *self, Py_ssize_t first,
2409 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2410{
2411 PyObject *iterator;
2412 PyObject *item;
2413 listitem_T *li;
2414 listitem_T *lastaddedli = NULL;
2415 listitem_T *next;
2416 typval_T v;
2417 list_T *l = self->list;
2418 PyInt i;
2419 PyInt j;
2420 PyInt numreplaced = 0;
2421 PyInt numadded = 0;
2422 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002423 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002424
2425 size = ListLength(self);
2426
2427 if (l->lv_lock)
2428 {
2429 RAISE_LOCKED_LIST;
2430 return -1;
2431 }
2432
2433 if (step == 0)
2434 {
2435 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2436 return -1;
2437 }
2438
2439 if (step != 1 && slicelen == 0)
2440 {
2441 /* Nothing to do. Only error out if obj has some items. */
2442 int ret = 0;
2443
2444 if (obj == NULL)
2445 return 0;
2446
2447 if (!(iterator = PyObject_GetIter(obj)))
2448 return -1;
2449
2450 if ((item = PyIter_Next(iterator)))
2451 {
2452 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002453 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002454 "to extended slice"), 0);
2455 Py_DECREF(item);
2456 ret = -1;
2457 }
2458 Py_DECREF(iterator);
2459 return ret;
2460 }
2461
2462 if (obj != NULL)
2463 /* XXX May allocate zero bytes. */
2464 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2465 {
2466 PyErr_NoMemory();
2467 return -1;
2468 }
2469
2470 if (first == size)
2471 li = NULL;
2472 else
2473 {
2474 li = list_find(l, (long) first);
2475 if (li == NULL)
2476 {
2477 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2478 (int)first);
2479 if (obj != NULL)
2480 PyMem_Free(lis);
2481 return -1;
2482 }
2483 i = slicelen;
2484 while (i-- && li != NULL)
2485 {
2486 j = step;
2487 next = li;
2488 if (step > 0)
2489 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2490 else
2491 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2492
2493 if (obj == NULL)
2494 listitem_remove(l, li);
2495 else
2496 lis[slicelen - i - 1] = li;
2497
2498 li = next;
2499 }
2500 if (li == NULL && i != -1)
2501 {
2502 PyErr_SET_VIM(N_("internal error: not enough list items"));
2503 if (obj != NULL)
2504 PyMem_Free(lis);
2505 return -1;
2506 }
2507 }
2508
2509 if (obj == NULL)
2510 return 0;
2511
2512 if (!(iterator = PyObject_GetIter(obj)))
2513 {
2514 PyMem_Free(lis);
2515 return -1;
2516 }
2517
2518 i = 0;
2519 while ((item = PyIter_Next(iterator)))
2520 {
2521 if (ConvertFromPyObject(item, &v) == -1)
2522 {
2523 Py_DECREF(iterator);
2524 Py_DECREF(item);
2525 PyMem_Free(lis);
2526 return -1;
2527 }
2528 Py_DECREF(item);
2529 if (list_insert_tv(l, &v, numreplaced < slicelen
2530 ? lis[numreplaced]
2531 : li) == FAIL)
2532 {
2533 clear_tv(&v);
2534 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2535 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2536 PyMem_Free(lis);
2537 return -1;
2538 }
2539 if (numreplaced < slicelen)
2540 {
2541 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002542 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002543 numreplaced++;
2544 }
2545 else
2546 {
2547 if (li)
2548 lastaddedli = li->li_prev;
2549 else
2550 lastaddedli = l->lv_last;
2551 numadded++;
2552 }
2553 clear_tv(&v);
2554 if (step != 1 && i >= slicelen)
2555 {
2556 Py_DECREF(iterator);
2557 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002558 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002559 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002560 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2561 PyMem_Free(lis);
2562 return -1;
2563 }
2564 ++i;
2565 }
2566 Py_DECREF(iterator);
2567
2568 if (step != 1 && i != slicelen)
2569 {
2570 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002571 N_("attempt to assign sequence of size %d to extended slice "
2572 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002573 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2574 PyMem_Free(lis);
2575 return -1;
2576 }
2577
2578 if (PyErr_Occurred())
2579 {
2580 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2581 PyMem_Free(lis);
2582 return -1;
2583 }
2584
2585 for (i = 0; i < numreplaced; i++)
2586 listitem_free(lis[i]);
2587 if (step == 1)
2588 for (i = numreplaced; i < slicelen; i++)
2589 listitem_remove(l, lis[i]);
2590
2591 PyMem_Free(lis);
2592
2593 return 0;
2594}
2595
2596 static int
2597ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2598{
2599 typval_T tv;
2600 list_T *l = self->list;
2601 listitem_T *li;
2602 Py_ssize_t length = ListLength(self);
2603
2604 if (l->lv_lock)
2605 {
2606 RAISE_LOCKED_LIST;
2607 return -1;
2608 }
2609 if (index > length || (index == length && obj == NULL))
2610 {
2611 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2612 return -1;
2613 }
2614
2615 if (obj == NULL)
2616 {
2617 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002618 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002619 clear_tv(&li->li_tv);
2620 vim_free(li);
2621 return 0;
2622 }
2623
2624 if (ConvertFromPyObject(obj, &tv) == -1)
2625 return -1;
2626
2627 if (index == length)
2628 {
2629 if (list_append_tv(l, &tv) == FAIL)
2630 {
2631 clear_tv(&tv);
2632 PyErr_SET_VIM(N_("failed to add item to list"));
2633 return -1;
2634 }
2635 }
2636 else
2637 {
2638 li = list_find(l, (long) index);
2639 clear_tv(&li->li_tv);
2640 copy_tv(&tv, &li->li_tv);
2641 clear_tv(&tv);
2642 }
2643 return 0;
2644}
2645
2646 static Py_ssize_t
2647ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2648{
2649#if PY_MAJOR_VERSION < 3
2650 if (PyInt_Check(idx))
2651 {
2652 long _idx = PyInt_AsLong(idx);
2653 return ListAssIndex(self, _idx, obj);
2654 }
2655 else
2656#endif
2657 if (PyLong_Check(idx))
2658 {
2659 long _idx = PyLong_AsLong(idx);
2660 return ListAssIndex(self, _idx, obj);
2661 }
2662 else if (PySlice_Check(idx))
2663 {
2664 Py_ssize_t start, stop, step, slicelen;
2665
Bram Moolenaar922a4662014-03-30 16:11:43 +02002666 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002667 &start, &stop, &step, &slicelen) < 0)
2668 return -1;
2669 return ListAssSlice(self, start, step, slicelen,
2670 obj);
2671 }
2672 else
2673 {
2674 RAISE_INVALID_INDEX_TYPE(idx);
2675 return -1;
2676 }
2677}
2678
2679 static PyObject *
2680ListConcatInPlace(ListObject *self, PyObject *obj)
2681{
2682 list_T *l = self->list;
2683 PyObject *lookup_dict;
2684
2685 if (l->lv_lock)
2686 {
2687 RAISE_LOCKED_LIST;
2688 return NULL;
2689 }
2690
2691 if (!(lookup_dict = PyDict_New()))
2692 return NULL;
2693
2694 if (list_py_concat(l, obj, lookup_dict) == -1)
2695 {
2696 Py_DECREF(lookup_dict);
2697 return NULL;
2698 }
2699 Py_DECREF(lookup_dict);
2700
2701 Py_INCREF(self);
2702 return (PyObject *)(self);
2703}
2704
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002705typedef struct
2706{
2707 listwatch_T lw;
2708 list_T *list;
2709} listiterinfo_T;
2710
2711 static void
2712ListIterDestruct(listiterinfo_T *lii)
2713{
2714 list_rem_watch(lii->list, &lii->lw);
2715 PyMem_Free(lii);
2716}
2717
2718 static PyObject *
2719ListIterNext(listiterinfo_T **lii)
2720{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002721 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002722
2723 if (!((*lii)->lw.lw_item))
2724 return NULL;
2725
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002726 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002727 return NULL;
2728
2729 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2730
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002731 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002732}
2733
2734 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002735ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002736{
2737 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002738 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002739
2740 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2741 {
2742 PyErr_NoMemory();
2743 return NULL;
2744 }
2745
2746 list_add_watch(l, &lii->lw);
2747 lii->lw.lw_item = l->lv_first;
2748 lii->list = l;
2749
2750 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002751 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2752 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002753}
2754
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002755static char *ListAttrs[] = {
2756 "locked",
2757 NULL
2758};
2759
2760 static PyObject *
2761ListDir(PyObject *self)
2762{
2763 return ObjectDir(self, ListAttrs);
2764}
2765
Bram Moolenaar66b79852012-09-21 14:00:35 +02002766 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002767ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002768{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002769 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002770 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002771 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002772 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002773 return -1;
2774 }
2775
2776 if (strcmp(name, "locked") == 0)
2777 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002778 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002779 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002780 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002781 return -1;
2782 }
2783 else
2784 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002785 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002786 if (istrue == -1)
2787 return -1;
2788 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002789 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002790 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002791 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002792 }
2793 return 0;
2794 }
2795 else
2796 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002797 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002798 return -1;
2799 }
2800}
2801
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002802static PySequenceMethods ListAsSeq = {
2803 (lenfunc) ListLength, /* sq_length, len(x) */
2804 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2805 0, /* RangeRepeat, sq_repeat, x*n */
2806 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2807 0, /* was_sq_slice, x[i:j] */
2808 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2809 0, /* was_sq_ass_slice, x[i:j]=v */
2810 0, /* sq_contains */
2811 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2812 0, /* sq_inplace_repeat */
2813};
2814
2815static PyMappingMethods ListAsMapping = {
2816 /* mp_length */ (lenfunc) ListLength,
2817 /* mp_subscript */ (binaryfunc) ListItem,
2818 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2819};
2820
Bram Moolenaardb913952012-06-29 12:54:53 +02002821static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002822 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2823 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2824 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002825};
2826
2827typedef struct
2828{
2829 PyObject_HEAD
2830 char_u *name;
2831} FunctionObject;
2832
2833static PyTypeObject FunctionType;
2834
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002835#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2836
Bram Moolenaardb913952012-06-29 12:54:53 +02002837 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002838FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002839{
2840 FunctionObject *self;
2841
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002842 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2843
Bram Moolenaardb913952012-06-29 12:54:53 +02002844 if (self == NULL)
2845 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002846
2847 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002848 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002849 if (!translated_function_exists(name))
2850 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002851 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002852 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002853 return NULL;
2854 }
2855 self->name = vim_strsave(name);
2856 func_ref(self->name);
2857 }
2858 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002859 if ((self->name = get_expanded_name(name,
2860 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2861 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002862 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002863 PyErr_FORMAT(PyExc_ValueError,
2864 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002865 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002866 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002867
2868 return (PyObject *)(self);
2869}
2870
2871 static PyObject *
2872FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2873{
2874 PyObject *self;
2875 char_u *name;
2876
2877 if (kwargs)
2878 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002879 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002880 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002881 return NULL;
2882 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002883
Bram Moolenaar389a1792013-06-23 13:00:44 +02002884 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002885 return NULL;
2886
2887 self = FunctionNew(subtype, name);
2888
Bram Moolenaar389a1792013-06-23 13:00:44 +02002889 PyMem_Free(name);
2890
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002891 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002892}
2893
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002894 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002895FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002896{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002897 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002898 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002899
2900 DESTRUCTOR_FINISH(self);
2901}
2902
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002903static char *FunctionAttrs[] = {
2904 "softspace",
2905 NULL
2906};
2907
2908 static PyObject *
2909FunctionDir(PyObject *self)
2910{
2911 return ObjectDir(self, FunctionAttrs);
2912}
2913
Bram Moolenaardb913952012-06-29 12:54:53 +02002914 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002915FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002916{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002917 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002918 typval_T args;
2919 typval_T selfdicttv;
2920 typval_T rettv;
2921 dict_T *selfdict = NULL;
2922 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002923 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002924 int error;
2925
2926 if (ConvertFromPyObject(argsObject, &args) == -1)
2927 return NULL;
2928
2929 if (kwargs != NULL)
2930 {
2931 selfdictObject = PyDict_GetItemString(kwargs, "self");
2932 if (selfdictObject != NULL)
2933 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002934 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002935 {
2936 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002937 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002938 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002939 selfdict = selfdicttv.vval.v_dict;
2940 }
2941 }
2942
Bram Moolenaar71700b82013-05-15 17:49:05 +02002943 Py_BEGIN_ALLOW_THREADS
2944 Python_Lock_Vim();
2945
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002946 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002947 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002948
2949 Python_Release_Vim();
2950 Py_END_ALLOW_THREADS
2951
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002952 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002953 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002954 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002955 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002956 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002957 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002958 }
2959 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002960 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002961
Bram Moolenaardb913952012-06-29 12:54:53 +02002962 clear_tv(&args);
2963 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002964 if (selfdict != NULL)
2965 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002966
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002967 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002968}
2969
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002970 static PyObject *
2971FunctionRepr(FunctionObject *self)
2972{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002973#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002974 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002975 * Finalize */
2976 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002977 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002978#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002979 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002980#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002981}
2982
Bram Moolenaardb913952012-06-29 12:54:53 +02002983static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002984 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2985 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002986};
2987
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002988/*
2989 * Options object
2990 */
2991
2992static PyTypeObject OptionsType;
2993
2994typedef int (*checkfun)(void *);
2995
2996typedef struct
2997{
2998 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01002999 int opt_type;
3000 void *from;
3001 checkfun Check;
3002 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003003} OptionsObject;
3004
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003005 static int
3006dummy_check(void *arg UNUSED)
3007{
3008 return 0;
3009}
3010
3011 static PyObject *
3012OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3013{
3014 OptionsObject *self;
3015
Bram Moolenaar774267b2013-05-21 20:51:59 +02003016 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003017 if (self == NULL)
3018 return NULL;
3019
3020 self->opt_type = opt_type;
3021 self->from = from;
3022 self->Check = Check;
3023 self->fromObj = fromObj;
3024 if (fromObj)
3025 Py_INCREF(fromObj);
3026
3027 return (PyObject *)(self);
3028}
3029
3030 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003031OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003032{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003033 PyObject_GC_UnTrack((void *)(self));
3034 Py_XDECREF(self->fromObj);
3035 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003036}
3037
3038 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003039OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003040{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003041 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003042 return 0;
3043}
3044
3045 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003046OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003047{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003048 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003049 return 0;
3050}
3051
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003052 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003053OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003054{
3055 char_u *key;
3056 int flags;
3057 long numval;
3058 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003059 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003060
Bram Moolenaard6e39182013-05-21 18:30:34 +02003061 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003062 return NULL;
3063
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003064 if (!(key = StringToChars(keyObject, &todecref)))
3065 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003066
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003067 if (*key == NUL)
3068 {
3069 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003070 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003071 return NULL;
3072 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003073
3074 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003075 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003076
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003077 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003078
3079 if (flags == 0)
3080 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003081 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003082 return NULL;
3083 }
3084
3085 if (flags & SOPT_UNSET)
3086 {
3087 Py_INCREF(Py_None);
3088 return Py_None;
3089 }
3090 else if (flags & SOPT_BOOL)
3091 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003092 PyObject *ret;
3093 ret = numval ? Py_True : Py_False;
3094 Py_INCREF(ret);
3095 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003096 }
3097 else if (flags & SOPT_NUM)
3098 return PyInt_FromLong(numval);
3099 else if (flags & SOPT_STRING)
3100 {
3101 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003102 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003103 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003104 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003105 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003106 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003107 else
3108 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003109 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003110 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003111 return NULL;
3112 }
3113 }
3114 else
3115 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003116 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003117 return NULL;
3118 }
3119}
3120
3121 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003122OptionsContains(OptionsObject *self, PyObject *keyObject)
3123{
3124 char_u *key;
3125 PyObject *todecref;
3126
3127 if (!(key = StringToChars(keyObject, &todecref)))
3128 return -1;
3129
3130 if (*key == NUL)
3131 {
3132 Py_XDECREF(todecref);
3133 return 0;
3134 }
3135
3136 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3137 {
3138 Py_XDECREF(todecref);
3139 return 1;
3140 }
3141 else
3142 {
3143 Py_XDECREF(todecref);
3144 return 0;
3145 }
3146}
3147
3148typedef struct
3149{
3150 void *lastoption;
3151 int opt_type;
3152} optiterinfo_T;
3153
3154 static PyObject *
3155OptionsIterNext(optiterinfo_T **oii)
3156{
3157 char_u *name;
3158
3159 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3160 return PyString_FromString((char *)name);
3161
3162 return NULL;
3163}
3164
3165 static PyObject *
3166OptionsIter(OptionsObject *self)
3167{
3168 optiterinfo_T *oii;
3169
3170 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3171 {
3172 PyErr_NoMemory();
3173 return NULL;
3174 }
3175
3176 oii->opt_type = self->opt_type;
3177 oii->lastoption = NULL;
3178
3179 return IterNew(oii,
3180 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3181 NULL, NULL);
3182}
3183
3184 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003185set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003186{
3187 char_u *errmsg;
3188
3189 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3190 {
3191 if (VimTryEnd())
3192 return FAIL;
3193 PyErr_SetVim((char *)errmsg);
3194 return FAIL;
3195 }
3196 return OK;
3197}
3198
3199 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003200set_option_value_for(
3201 char_u *key,
3202 int numval,
3203 char_u *stringval,
3204 int opt_flags,
3205 int opt_type,
3206 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003207{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003208 win_T *save_curwin = NULL;
3209 tabpage_T *save_curtab = NULL;
3210 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003211 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003212
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003213 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003214 switch (opt_type)
3215 {
3216 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003217 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003218 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003219 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003220 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003221 if (VimTryEnd())
3222 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003223 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003224 return -1;
3225 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003226 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3227 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003228 break;
3229 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003230 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003231 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003232 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003233 break;
3234 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003235 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003236 break;
3237 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003238 if (set_ret == FAIL)
3239 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003240 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003241}
3242
3243 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003244OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003245{
3246 char_u *key;
3247 int flags;
3248 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003249 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003250 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003251
Bram Moolenaard6e39182013-05-21 18:30:34 +02003252 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003253 return -1;
3254
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003255 if (!(key = StringToChars(keyObject, &todecref)))
3256 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003257
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003258 if (*key == NUL)
3259 {
3260 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003261 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003262 return -1;
3263 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003264
3265 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003266 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003267
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003268 if (flags == 0)
3269 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003270 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003271 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003272 return -1;
3273 }
3274
3275 if (valObject == NULL)
3276 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003277 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003278 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003279 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003280 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003281 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003282 return -1;
3283 }
3284 else if (!(flags & SOPT_GLOBAL))
3285 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003286 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003287 N_("unable to unset option %s "
3288 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003289 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003290 return -1;
3291 }
3292 else
3293 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003294 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003295 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003296 return 0;
3297 }
3298 }
3299
Bram Moolenaard6e39182013-05-21 18:30:34 +02003300 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003301
3302 if (flags & SOPT_BOOL)
3303 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003304 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003305
Bram Moolenaarb983f752013-05-15 16:11:50 +02003306 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003307 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003308 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003309 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003310 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003311 }
3312 else if (flags & SOPT_NUM)
3313 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003314 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003315
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003316 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003317 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003318 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003319 return -1;
3320 }
3321
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003322 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003323 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003324 }
3325 else
3326 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003327 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003328 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003329
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003330 if ((val = StringToChars(valObject, &todecref2)))
3331 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003332 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003333 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003334 Py_XDECREF(todecref2);
3335 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003336 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003337 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003338 }
3339
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003340 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003341
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003342 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003343}
3344
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003345static PySequenceMethods OptionsAsSeq = {
3346 0, /* sq_length */
3347 0, /* sq_concat */
3348 0, /* sq_repeat */
3349 0, /* sq_item */
3350 0, /* sq_slice */
3351 0, /* sq_ass_item */
3352 0, /* sq_ass_slice */
3353 (objobjproc) OptionsContains, /* sq_contains */
3354 0, /* sq_inplace_concat */
3355 0, /* sq_inplace_repeat */
3356};
3357
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003358static PyMappingMethods OptionsAsMapping = {
3359 (lenfunc) NULL,
3360 (binaryfunc) OptionsItem,
3361 (objobjargproc) OptionsAssItem,
3362};
3363
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003364/* Tabpage object
3365 */
3366
3367typedef struct
3368{
3369 PyObject_HEAD
3370 tabpage_T *tab;
3371} TabPageObject;
3372
3373static PyObject *WinListNew(TabPageObject *tabObject);
3374
3375static PyTypeObject TabPageType;
3376
3377 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003378CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003379{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003380 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003381 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003382 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003383 return -1;
3384 }
3385
3386 return 0;
3387}
3388
3389 static PyObject *
3390TabPageNew(tabpage_T *tab)
3391{
3392 TabPageObject *self;
3393
3394 if (TAB_PYTHON_REF(tab))
3395 {
3396 self = TAB_PYTHON_REF(tab);
3397 Py_INCREF(self);
3398 }
3399 else
3400 {
3401 self = PyObject_NEW(TabPageObject, &TabPageType);
3402 if (self == NULL)
3403 return NULL;
3404 self->tab = tab;
3405 TAB_PYTHON_REF(tab) = self;
3406 }
3407
3408 return (PyObject *)(self);
3409}
3410
3411 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003412TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003413{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003414 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3415 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003416
3417 DESTRUCTOR_FINISH(self);
3418}
3419
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003420static char *TabPageAttrs[] = {
3421 "windows", "number", "vars", "window", "valid",
3422 NULL
3423};
3424
3425 static PyObject *
3426TabPageDir(PyObject *self)
3427{
3428 return ObjectDir(self, TabPageAttrs);
3429}
3430
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003431 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003432TabPageAttrValid(TabPageObject *self, char *name)
3433{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003434 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003435
3436 if (strcmp(name, "valid") != 0)
3437 return NULL;
3438
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003439 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3440 Py_INCREF(ret);
3441 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003442}
3443
3444 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003445TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003446{
3447 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003448 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003449 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003450 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003451 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003452 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003453 else if (strcmp(name, "window") == 0)
3454 {
3455 /* For current tab window.c does not bother to set or update tp_curwin
3456 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003457 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003458 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003459 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003460 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003461 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003462 else if (strcmp(name, "__members__") == 0)
3463 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003464 return NULL;
3465}
3466
3467 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003468TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003469{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003470 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003471 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003472 else
3473 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003474 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003475
3476 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003477 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3478 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003479 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003480 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003481 }
3482}
3483
3484static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003485 /* name, function, calling, documentation */
3486 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3487 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003488};
3489
3490/*
3491 * Window list object
3492 */
3493
3494static PyTypeObject TabListType;
3495static PySequenceMethods TabListAsSeq;
3496
3497typedef struct
3498{
3499 PyObject_HEAD
3500} TabListObject;
3501
3502 static PyInt
3503TabListLength(PyObject *self UNUSED)
3504{
3505 tabpage_T *tp = first_tabpage;
3506 PyInt n = 0;
3507
3508 while (tp != NULL)
3509 {
3510 ++n;
3511 tp = tp->tp_next;
3512 }
3513
3514 return n;
3515}
3516
3517 static PyObject *
3518TabListItem(PyObject *self UNUSED, PyInt n)
3519{
3520 tabpage_T *tp;
3521
3522 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3523 if (n == 0)
3524 return TabPageNew(tp);
3525
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003526 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003527 return NULL;
3528}
3529
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003530/*
3531 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003532 */
3533
3534typedef struct
3535{
3536 PyObject_HEAD
3537 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003538 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003539} WindowObject;
3540
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003541static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003542
3543 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003544CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003545{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003546 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003547 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003548 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003549 return -1;
3550 }
3551
3552 return 0;
3553}
3554
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003555 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003556WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003557{
3558 /* We need to handle deletion of windows underneath us.
3559 * If we add a "w_python*_ref" field to the win_T structure,
3560 * then we can get at it in win_free() in vim. We then
3561 * need to create only ONE Python object per window - if
3562 * we try to create a second, just INCREF the existing one
3563 * and return it. The (single) Python object referring to
3564 * the window is stored in "w_python*_ref".
3565 * On a win_free() we set the Python object's win_T* field
3566 * to an invalid value. We trap all uses of a window
3567 * object, and reject them if the win_T* field is invalid.
3568 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003569 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003570 * w_python_ref and w_python3_ref fields respectively.
3571 */
3572
3573 WindowObject *self;
3574
3575 if (WIN_PYTHON_REF(win))
3576 {
3577 self = WIN_PYTHON_REF(win);
3578 Py_INCREF(self);
3579 }
3580 else
3581 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003582 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003583 if (self == NULL)
3584 return NULL;
3585 self->win = win;
3586 WIN_PYTHON_REF(win) = self;
3587 }
3588
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003589 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3590
Bram Moolenaar971db462013-05-12 18:44:48 +02003591 return (PyObject *)(self);
3592}
3593
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003594 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003595WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003596{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003597 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003598 if (self->win && self->win != INVALID_WINDOW_VALUE)
3599 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003600 Py_XDECREF(((PyObject *)(self->tabObject)));
3601 PyObject_GC_Del((void *)(self));
3602}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003603
Bram Moolenaar774267b2013-05-21 20:51:59 +02003604 static int
3605WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3606{
3607 Py_VISIT(((PyObject *)(self->tabObject)));
3608 return 0;
3609}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003610
Bram Moolenaar774267b2013-05-21 20:51:59 +02003611 static int
3612WindowClear(WindowObject *self)
3613{
3614 Py_CLEAR(self->tabObject);
3615 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003616}
3617
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003618 static win_T *
3619get_firstwin(TabPageObject *tabObject)
3620{
3621 if (tabObject)
3622 {
3623 if (CheckTabPage(tabObject))
3624 return NULL;
3625 /* For current tab window.c does not bother to set or update tp_firstwin
3626 */
3627 else if (tabObject->tab == curtab)
3628 return firstwin;
3629 else
3630 return tabObject->tab->tp_firstwin;
3631 }
3632 else
3633 return firstwin;
3634}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003635static char *WindowAttrs[] = {
3636 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3637 "tabpage", "valid",
3638 NULL
3639};
3640
3641 static PyObject *
3642WindowDir(PyObject *self)
3643{
3644 return ObjectDir(self, WindowAttrs);
3645}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003646
Bram Moolenaar971db462013-05-12 18:44:48 +02003647 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003648WindowAttrValid(WindowObject *self, char *name)
3649{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003650 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003651
3652 if (strcmp(name, "valid") != 0)
3653 return NULL;
3654
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003655 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3656 Py_INCREF(ret);
3657 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003658}
3659
3660 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003661WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003662{
3663 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003664 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003665 else if (strcmp(name, "cursor") == 0)
3666 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003667 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003668
3669 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3670 }
3671 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003672 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003673#ifdef FEAT_WINDOWS
3674 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003675 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003676#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003677#ifdef FEAT_VERTSPLIT
3678 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003679 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003680 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003681 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003682#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003683 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003684 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003685 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003686 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3687 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003688 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003689 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003690 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003691 return NULL;
3692 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003693 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003694 }
3695 else if (strcmp(name, "tabpage") == 0)
3696 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003697 Py_INCREF(self->tabObject);
3698 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003699 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003700 else if (strcmp(name, "__members__") == 0)
3701 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003702 else
3703 return NULL;
3704}
3705
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003706 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003707WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003708{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003709 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003710 return -1;
3711
3712 if (strcmp(name, "buffer") == 0)
3713 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003714 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003715 return -1;
3716 }
3717 else if (strcmp(name, "cursor") == 0)
3718 {
3719 long lnum;
3720 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003721
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003722 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003723 return -1;
3724
Bram Moolenaard6e39182013-05-21 18:30:34 +02003725 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003726 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003727 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003728 return -1;
3729 }
3730
3731 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003732 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003733 return -1;
3734
Bram Moolenaard6e39182013-05-21 18:30:34 +02003735 self->win->w_cursor.lnum = lnum;
3736 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003737#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003738 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003739#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003740 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003741 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003742
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003743 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003744 return 0;
3745 }
3746 else if (strcmp(name, "height") == 0)
3747 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003748 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003749 win_T *savewin;
3750
Bram Moolenaardee2e312013-06-23 16:35:47 +02003751 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003752 return -1;
3753
3754#ifdef FEAT_GUI
3755 need_mouse_correct = TRUE;
3756#endif
3757 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003758 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003759
3760 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003761 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003762 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003763 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003764 return -1;
3765
3766 return 0;
3767 }
3768#ifdef FEAT_VERTSPLIT
3769 else if (strcmp(name, "width") == 0)
3770 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003771 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003772 win_T *savewin;
3773
Bram Moolenaardee2e312013-06-23 16:35:47 +02003774 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003775 return -1;
3776
3777#ifdef FEAT_GUI
3778 need_mouse_correct = TRUE;
3779#endif
3780 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003781 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003782
3783 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003784 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003785 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003786 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003787 return -1;
3788
3789 return 0;
3790 }
3791#endif
3792 else
3793 {
3794 PyErr_SetString(PyExc_AttributeError, name);
3795 return -1;
3796 }
3797}
3798
3799 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003800WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003801{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003802 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003803 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003804 else
3805 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003806 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003807
Bram Moolenaar6d216452013-05-12 19:00:41 +02003808 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003809 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003810 (self));
3811 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003812 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003813 }
3814}
3815
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003816static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003817 /* name, function, calling, documentation */
3818 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3819 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003820};
3821
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003822/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003823 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003824 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003825
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003826static PyTypeObject WinListType;
3827static PySequenceMethods WinListAsSeq;
3828
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003829typedef struct
3830{
3831 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003832 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003833} WinListObject;
3834
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003835 static PyObject *
3836WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003837{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003838 WinListObject *self;
3839
3840 self = PyObject_NEW(WinListObject, &WinListType);
3841 self->tabObject = tabObject;
3842 Py_INCREF(tabObject);
3843
3844 return (PyObject *)(self);
3845}
3846
3847 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003848WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003849{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003850 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003851
3852 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003853 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003854 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003855 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003856
3857 DESTRUCTOR_FINISH(self);
3858}
3859
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003860 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003861WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003862{
3863 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003864 PyInt n = 0;
3865
Bram Moolenaard6e39182013-05-21 18:30:34 +02003866 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003867 return -1;
3868
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003869 while (w != NULL)
3870 {
3871 ++n;
3872 w = W_NEXT(w);
3873 }
3874
3875 return n;
3876}
3877
3878 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003879WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003880{
3881 win_T *w;
3882
Bram Moolenaard6e39182013-05-21 18:30:34 +02003883 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003884 return NULL;
3885
3886 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003887 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003888 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003889
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003890 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003891 return NULL;
3892}
3893
3894/* Convert a Python string into a Vim line.
3895 *
3896 * The result is in allocated memory. All internal nulls are replaced by
3897 * newline characters. It is an error for the string to contain newline
3898 * characters.
3899 *
3900 * On errors, the Python exception data is set, and NULL is returned.
3901 */
3902 static char *
3903StringToLine(PyObject *obj)
3904{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003905 char *str;
3906 char *save;
3907 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003908 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003909 PyInt i;
3910 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003911
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003912 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003913 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003914 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3915 || str == NULL)
3916 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003917 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003918 else if (PyUnicode_Check(obj))
3919 {
3920 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3921 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003922
Bram Moolenaardaa27022013-06-24 22:33:30 +02003923 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003924 || str == NULL)
3925 {
3926 Py_DECREF(bytes);
3927 return NULL;
3928 }
3929 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003930 else
3931 {
3932#if PY_MAJOR_VERSION < 3
3933 PyErr_FORMAT(PyExc_TypeError,
3934 N_("expected str() or unicode() instance, but got %s"),
3935 Py_TYPE_NAME(obj));
3936#else
3937 PyErr_FORMAT(PyExc_TypeError,
3938 N_("expected bytes() or str() instance, but got %s"),
3939 Py_TYPE_NAME(obj));
3940#endif
3941 return NULL;
3942 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003943
3944 /*
3945 * Error checking: String must not contain newlines, as we
3946 * are replacing a single line, and we must replace it with
3947 * a single line.
3948 * A trailing newline is removed, so that append(f.readlines()) works.
3949 */
3950 p = memchr(str, '\n', len);
3951 if (p != NULL)
3952 {
3953 if (p == str + len - 1)
3954 --len;
3955 else
3956 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003957 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003958 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003959 return NULL;
3960 }
3961 }
3962
3963 /* Create a copy of the string, with internal nulls replaced by
3964 * newline characters, as is the vim convention.
3965 */
3966 save = (char *)alloc((unsigned)(len+1));
3967 if (save == NULL)
3968 {
3969 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003970 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003971 return NULL;
3972 }
3973
3974 for (i = 0; i < len; ++i)
3975 {
3976 if (str[i] == '\0')
3977 save[i] = '\n';
3978 else
3979 save[i] = str[i];
3980 }
3981
3982 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003983 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003984
3985 return save;
3986}
3987
3988/* Get a line from the specified buffer. The line number is
3989 * in Vim format (1-based). The line is returned as a Python
3990 * string object.
3991 */
3992 static PyObject *
3993GetBufferLine(buf_T *buf, PyInt n)
3994{
3995 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3996}
3997
3998
3999/* Get a list of lines from the specified buffer. The line numbers
4000 * are in Vim format (1-based). The range is from lo up to, but not
4001 * including, hi. The list is returned as a Python list of string objects.
4002 */
4003 static PyObject *
4004GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4005{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004006 PyInt i;
4007 PyInt n = hi - lo;
4008 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004009
4010 if (list == NULL)
4011 return NULL;
4012
4013 for (i = 0; i < n; ++i)
4014 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004015 PyObject *string = LineToString(
4016 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004017
4018 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004019 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004020 {
4021 Py_DECREF(list);
4022 return NULL;
4023 }
4024
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004025 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004026 }
4027
4028 /* The ownership of the Python list is passed to the caller (ie,
4029 * the caller should Py_DECREF() the object when it is finished
4030 * with it).
4031 */
4032
4033 return list;
4034}
4035
4036/*
4037 * Check if deleting lines made the cursor position invalid.
4038 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4039 * deleted).
4040 */
4041 static void
4042py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4043{
4044 if (curwin->w_cursor.lnum >= lo)
4045 {
4046 /* Adjust the cursor position if it's in/after the changed
4047 * lines. */
4048 if (curwin->w_cursor.lnum >= hi)
4049 {
4050 curwin->w_cursor.lnum += extra;
4051 check_cursor_col();
4052 }
4053 else if (extra < 0)
4054 {
4055 curwin->w_cursor.lnum = lo;
4056 check_cursor();
4057 }
4058 else
4059 check_cursor_col();
4060 changed_cline_bef_curs();
4061 }
4062 invalidate_botline();
4063}
4064
Bram Moolenaar19e60942011-06-19 00:27:51 +02004065/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004066 * Find a window that contains "buf" and switch to it.
4067 * If there is no such window, use the current window and change "curbuf".
4068 * Caller must initialize save_curbuf to NULL.
4069 * restore_win_for_buf() MUST be called later!
4070 */
4071 static void
4072switch_to_win_for_buf(
4073 buf_T *buf,
4074 win_T **save_curwinp,
4075 tabpage_T **save_curtabp,
4076 buf_T **save_curbufp)
4077{
4078 win_T *wp;
4079 tabpage_T *tp;
4080
Bram Moolenaarae38d052014-12-17 14:46:09 +01004081 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004082 switch_buffer(save_curbufp, buf);
Bram Moolenaarae38d052014-12-17 14:46:09 +01004083 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4084 {
4085 restore_win(*save_curwinp, *save_curtabp, TRUE);
4086 switch_buffer(save_curbufp, buf);
4087 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004088}
4089
4090 static void
4091restore_win_for_buf(
4092 win_T *save_curwin,
4093 tabpage_T *save_curtab,
4094 buf_T *save_curbuf)
4095{
4096 if (save_curbuf == NULL)
4097 restore_win(save_curwin, save_curtab, TRUE);
4098 else
4099 restore_buffer(save_curbuf);
4100}
4101
4102/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004103 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004104 * in Vim format (1-based). The replacement line is given as
4105 * a Python string object. The object is checked for validity
4106 * and correct format. Errors are returned as a value of FAIL.
4107 * The return value is OK on success.
4108 * If OK is returned and len_change is not NULL, *len_change
4109 * is set to the change in the buffer length.
4110 */
4111 static int
4112SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4113{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004114 buf_T *save_curbuf = NULL;
4115 win_T *save_curwin = NULL;
4116 tabpage_T *save_curtab = NULL;
4117
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004118 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004119 * There are three cases:
4120 * 1. NULL, or None - this is a deletion.
4121 * 2. A string - this is a replacement.
4122 * 3. Anything else - this is an error.
4123 */
4124 if (line == Py_None || line == NULL)
4125 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004126 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004127 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004128
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004129 VimTryStart();
4130
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004131 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004132 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004133 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004134 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004135 else
4136 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004137 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004138 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004139 if (save_curbuf == NULL)
4140 /* Only adjust marks if we managed to switch to a window that
4141 * holds the buffer, otherwise line numbers will be invalid. */
4142 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004143 }
4144
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004145 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004146
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004147 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004148 return FAIL;
4149
4150 if (len_change)
4151 *len_change = -1;
4152
4153 return OK;
4154 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004155 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004156 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004157 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004158
4159 if (save == NULL)
4160 return FAIL;
4161
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004162 VimTryStart();
4163
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004164 /* We do not need to free "save" if ml_replace() consumes it. */
4165 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004166 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004167
4168 if (u_savesub((linenr_T)n) == FAIL)
4169 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004170 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004171 vim_free(save);
4172 }
4173 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4174 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004175 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004176 vim_free(save);
4177 }
4178 else
4179 changed_bytes((linenr_T)n, 0);
4180
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004181 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004182
4183 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004184 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004185 check_cursor_col();
4186
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004187 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004188 return FAIL;
4189
4190 if (len_change)
4191 *len_change = 0;
4192
4193 return OK;
4194 }
4195 else
4196 {
4197 PyErr_BadArgument();
4198 return FAIL;
4199 }
4200}
4201
Bram Moolenaar19e60942011-06-19 00:27:51 +02004202/* Replace a range of lines in the specified buffer. The line numbers are in
4203 * Vim format (1-based). The range is from lo up to, but not including, hi.
4204 * The replacement lines are given as a Python list of string objects. The
4205 * list is checked for validity and correct format. Errors are returned as a
4206 * value of FAIL. The return value is OK on success.
4207 * If OK is returned and len_change is not NULL, *len_change
4208 * is set to the change in the buffer length.
4209 */
4210 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004211SetBufferLineList(
4212 buf_T *buf,
4213 PyInt lo,
4214 PyInt hi,
4215 PyObject *list,
4216 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004217{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004218 buf_T *save_curbuf = NULL;
4219 win_T *save_curwin = NULL;
4220 tabpage_T *save_curtab = NULL;
4221
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004222 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004223 * There are three cases:
4224 * 1. NULL, or None - this is a deletion.
4225 * 2. A list - this is a replacement.
4226 * 3. Anything else - this is an error.
4227 */
4228 if (list == Py_None || list == NULL)
4229 {
4230 PyInt i;
4231 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004232
4233 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004234 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004235 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004236
4237 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004238 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004239 else
4240 {
4241 for (i = 0; i < n; ++i)
4242 {
4243 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4244 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004245 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004246 break;
4247 }
4248 }
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004249 if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL))
4250 /* Using an existing window for the buffer, adjust the cursor
4251 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004252 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004253 if (save_curbuf == NULL)
4254 /* Only adjust marks if we managed to switch to a window that
4255 * holds the buffer, otherwise line numbers will be invalid. */
4256 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004257 }
4258
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004259 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004260
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004261 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004262 return FAIL;
4263
4264 if (len_change)
4265 *len_change = -n;
4266
4267 return OK;
4268 }
4269 else if (PyList_Check(list))
4270 {
4271 PyInt i;
4272 PyInt new_len = PyList_Size(list);
4273 PyInt old_len = hi - lo;
4274 PyInt extra = 0; /* lines added to text, can be negative */
4275 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004276
4277 if (new_len == 0) /* avoid allocating zero bytes */
4278 array = NULL;
4279 else
4280 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004281 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004282 if (array == NULL)
4283 {
4284 PyErr_NoMemory();
4285 return FAIL;
4286 }
4287 }
4288
4289 for (i = 0; i < new_len; ++i)
4290 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004291 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004292
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004293 if (!(line = PyList_GetItem(list, i)) ||
4294 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004295 {
4296 while (i)
4297 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004298 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004299 return FAIL;
4300 }
4301 }
4302
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004303 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004304 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004305
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004306 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004307 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004308
4309 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004310 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004311
4312 /* If the size of the range is reducing (ie, new_len < old_len) we
4313 * need to delete some old_len. We do this at the start, by
4314 * repeatedly deleting line "lo".
4315 */
4316 if (!PyErr_Occurred())
4317 {
4318 for (i = 0; i < old_len - new_len; ++i)
4319 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4320 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004321 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004322 break;
4323 }
4324 extra -= i;
4325 }
4326
4327 /* For as long as possible, replace the existing old_len with the
4328 * new old_len. This is a more efficient operation, as it requires
4329 * less memory allocation and freeing.
4330 */
4331 if (!PyErr_Occurred())
4332 {
4333 for (i = 0; i < old_len && i < new_len; ++i)
4334 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4335 == FAIL)
4336 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004337 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004338 break;
4339 }
4340 }
4341 else
4342 i = 0;
4343
4344 /* Now we may need to insert the remaining new old_len. If we do, we
4345 * must free the strings as we finish with them (we can't pass the
4346 * responsibility to vim in this case).
4347 */
4348 if (!PyErr_Occurred())
4349 {
4350 while (i < new_len)
4351 {
4352 if (ml_append((linenr_T)(lo + i - 1),
4353 (char_u *)array[i], 0, FALSE) == FAIL)
4354 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004355 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004356 break;
4357 }
4358 vim_free(array[i]);
4359 ++i;
4360 ++extra;
4361 }
4362 }
4363
4364 /* Free any left-over old_len, as a result of an error */
4365 while (i < new_len)
4366 {
4367 vim_free(array[i]);
4368 ++i;
4369 }
4370
4371 /* Free the array of old_len. All of its contents have now
4372 * been dealt with (either freed, or the responsibility passed
4373 * to vim.
4374 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004375 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004376
4377 /* Adjust marks. Invalidate any which lie in the
4378 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004379 * Only adjust marks if we managed to switch to a window that holds
4380 * the buffer, otherwise line numbers will be invalid. */
4381 if (save_curbuf == NULL)
4382 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004383 (long)MAXLNUM, (long)extra);
4384 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4385
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004386 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004387 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4388
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004389 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004390 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004391
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004392 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004393 return FAIL;
4394
4395 if (len_change)
4396 *len_change = new_len - old_len;
4397
4398 return OK;
4399 }
4400 else
4401 {
4402 PyErr_BadArgument();
4403 return FAIL;
4404 }
4405}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004406
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004407/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004408 * The line number is in Vim format (1-based). The lines to be inserted are
4409 * given as a Python list of string objects or as a single string. The lines
4410 * to be added are checked for validity and correct format. Errors are
4411 * returned as a value of FAIL. The return value is OK on success.
4412 * If OK is returned and len_change is not NULL, *len_change
4413 * is set to the change in the buffer length.
4414 */
4415 static int
4416InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4417{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004418 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004419 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004420 tabpage_T *save_curtab = NULL;
4421
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004422 /* First of all, we check the type of the supplied Python object.
4423 * It must be a string or a list, or the call is in error.
4424 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004425 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004426 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004427 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004428
4429 if (str == NULL)
4430 return FAIL;
4431
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004432 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004433 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004434 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004435
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004436 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004437 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004438 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004439 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004440 else if (save_curbuf == NULL)
4441 /* Only adjust marks if we managed to switch to a window that
4442 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004443 appended_lines_mark((linenr_T)n, 1L);
4444
4445 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004446 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004447 update_screen(VALID);
4448
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004449 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004450 return FAIL;
4451
4452 if (len_change)
4453 *len_change = 1;
4454
4455 return OK;
4456 }
4457 else if (PyList_Check(lines))
4458 {
4459 PyInt i;
4460 PyInt size = PyList_Size(lines);
4461 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004462
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004463 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004464 if (array == NULL)
4465 {
4466 PyErr_NoMemory();
4467 return FAIL;
4468 }
4469
4470 for (i = 0; i < size; ++i)
4471 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004472 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004473
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004474 if (!(line = PyList_GetItem(lines, i)) ||
4475 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004476 {
4477 while (i)
4478 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004479 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004480 return FAIL;
4481 }
4482 }
4483
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004484 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004485 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004486 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004487
4488 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004489 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004490 else
4491 {
4492 for (i = 0; i < size; ++i)
4493 {
4494 if (ml_append((linenr_T)(n + i),
4495 (char_u *)array[i], 0, FALSE) == FAIL)
4496 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004497 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004498
4499 /* Free the rest of the lines */
4500 while (i < size)
4501 vim_free(array[i++]);
4502
4503 break;
4504 }
4505 vim_free(array[i]);
4506 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004507 if (i > 0 && save_curbuf == NULL)
4508 /* Only adjust marks if we managed to switch to a window that
4509 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004510 appended_lines_mark((linenr_T)n, (long)i);
4511 }
4512
4513 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004514 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004515 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004516 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004517
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004518 update_screen(VALID);
4519
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004520 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004521 return FAIL;
4522
4523 if (len_change)
4524 *len_change = size;
4525
4526 return OK;
4527 }
4528 else
4529 {
4530 PyErr_BadArgument();
4531 return FAIL;
4532 }
4533}
4534
4535/*
4536 * Common routines for buffers and line ranges
4537 * -------------------------------------------
4538 */
4539
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004540typedef struct
4541{
4542 PyObject_HEAD
4543 buf_T *buf;
4544} BufferObject;
4545
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004546 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004547CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004548{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004549 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004550 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004551 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004552 return -1;
4553 }
4554
4555 return 0;
4556}
4557
4558 static PyObject *
4559RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4560{
4561 if (CheckBuffer(self))
4562 return NULL;
4563
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004564 if (end == -1)
4565 end = self->buf->b_ml.ml_line_count;
4566
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004567 if (n < 0)
4568 n += end - start + 1;
4569
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004570 if (n < 0 || n > end - start)
4571 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004572 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004573 return NULL;
4574 }
4575
4576 return GetBufferLine(self->buf, n+start);
4577}
4578
4579 static PyObject *
4580RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4581{
4582 PyInt size;
4583
4584 if (CheckBuffer(self))
4585 return NULL;
4586
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004587 if (end == -1)
4588 end = self->buf->b_ml.ml_line_count;
4589
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004590 size = end - start + 1;
4591
4592 if (lo < 0)
4593 lo = 0;
4594 else if (lo > size)
4595 lo = size;
4596 if (hi < 0)
4597 hi = 0;
4598 if (hi < lo)
4599 hi = lo;
4600 else if (hi > size)
4601 hi = size;
4602
4603 return GetBufferLineList(self->buf, lo+start, hi+start);
4604}
4605
4606 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004607RBAsItem(
4608 BufferObject *self,
4609 PyInt n,
4610 PyObject *valObject,
4611 PyInt start,
4612 PyInt end,
4613 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004614{
4615 PyInt len_change;
4616
4617 if (CheckBuffer(self))
4618 return -1;
4619
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004620 if (end == -1)
4621 end = self->buf->b_ml.ml_line_count;
4622
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004623 if (n < 0)
4624 n += end - start + 1;
4625
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004626 if (n < 0 || n > end - start)
4627 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004628 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004629 return -1;
4630 }
4631
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004632 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004633 return -1;
4634
4635 if (new_end)
4636 *new_end = end + len_change;
4637
4638 return 0;
4639}
4640
Bram Moolenaar19e60942011-06-19 00:27:51 +02004641 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004642RBAsSlice(
4643 BufferObject *self,
4644 PyInt lo,
4645 PyInt hi,
4646 PyObject *valObject,
4647 PyInt start,
4648 PyInt end,
4649 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004650{
4651 PyInt size;
4652 PyInt len_change;
4653
4654 /* Self must be a valid buffer */
4655 if (CheckBuffer(self))
4656 return -1;
4657
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004658 if (end == -1)
4659 end = self->buf->b_ml.ml_line_count;
4660
Bram Moolenaar19e60942011-06-19 00:27:51 +02004661 /* Sort out the slice range */
4662 size = end - start + 1;
4663
4664 if (lo < 0)
4665 lo = 0;
4666 else if (lo > size)
4667 lo = size;
4668 if (hi < 0)
4669 hi = 0;
4670 if (hi < lo)
4671 hi = lo;
4672 else if (hi > size)
4673 hi = size;
4674
4675 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004676 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004677 return -1;
4678
4679 if (new_end)
4680 *new_end = end + len_change;
4681
4682 return 0;
4683}
4684
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004685
4686 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004687RBAppend(
4688 BufferObject *self,
4689 PyObject *args,
4690 PyInt start,
4691 PyInt end,
4692 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004693{
4694 PyObject *lines;
4695 PyInt len_change;
4696 PyInt max;
4697 PyInt n;
4698
4699 if (CheckBuffer(self))
4700 return NULL;
4701
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004702 if (end == -1)
4703 end = self->buf->b_ml.ml_line_count;
4704
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004705 max = n = end - start + 1;
4706
4707 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4708 return NULL;
4709
4710 if (n < 0 || n > max)
4711 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004712 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004713 return NULL;
4714 }
4715
4716 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4717 return NULL;
4718
4719 if (new_end)
4720 *new_end = end + len_change;
4721
4722 Py_INCREF(Py_None);
4723 return Py_None;
4724}
4725
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004726/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004727 */
4728
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004729static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004730static PySequenceMethods RangeAsSeq;
4731static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004732
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004733typedef struct
4734{
4735 PyObject_HEAD
4736 BufferObject *buf;
4737 PyInt start;
4738 PyInt end;
4739} RangeObject;
4740
4741 static PyObject *
4742RangeNew(buf_T *buf, PyInt start, PyInt end)
4743{
4744 BufferObject *bufr;
4745 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004746 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004747 if (self == NULL)
4748 return NULL;
4749
4750 bufr = (BufferObject *)BufferNew(buf);
4751 if (bufr == NULL)
4752 {
4753 Py_DECREF(self);
4754 return NULL;
4755 }
4756 Py_INCREF(bufr);
4757
4758 self->buf = bufr;
4759 self->start = start;
4760 self->end = end;
4761
4762 return (PyObject *)(self);
4763}
4764
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004765 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004766RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004767{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004768 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004769 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004770 PyObject_GC_Del((void *)(self));
4771}
4772
4773 static int
4774RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4775{
4776 Py_VISIT(((PyObject *)(self->buf)));
4777 return 0;
4778}
4779
4780 static int
4781RangeClear(RangeObject *self)
4782{
4783 Py_CLEAR(self->buf);
4784 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004785}
4786
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004787 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004788RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004789{
4790 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004791 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004792 return -1; /* ??? */
4793
Bram Moolenaard6e39182013-05-21 18:30:34 +02004794 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004795}
4796
4797 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004798RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004799{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004800 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004801}
4802
4803 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004804RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004805{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004806 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004807}
4808
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004809static char *RangeAttrs[] = {
4810 "start", "end",
4811 NULL
4812};
4813
4814 static PyObject *
4815RangeDir(PyObject *self)
4816{
4817 return ObjectDir(self, RangeAttrs);
4818}
4819
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004820 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004821RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004822{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004823 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004824}
4825
4826 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004827RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004828{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004829 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004830 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4831 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004832 else
4833 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004834 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004835
4836 if (name == NULL)
4837 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004838
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004839 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004840 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004841 }
4842}
4843
4844static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004845 /* name, function, calling, documentation */
4846 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004847 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4848 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004849};
4850
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004851static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004852static PySequenceMethods BufferAsSeq;
4853static PyMappingMethods BufferAsMapping;
4854
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004855 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004856BufferNew(buf_T *buf)
4857{
4858 /* We need to handle deletion of buffers underneath us.
4859 * If we add a "b_python*_ref" field to the buf_T structure,
4860 * then we can get at it in buf_freeall() in vim. We then
4861 * need to create only ONE Python object per buffer - if
4862 * we try to create a second, just INCREF the existing one
4863 * and return it. The (single) Python object referring to
4864 * the buffer is stored in "b_python*_ref".
4865 * Question: what to do on a buf_freeall(). We'll probably
4866 * have to either delete the Python object (DECREF it to
4867 * zero - a bad idea, as it leaves dangling refs!) or
4868 * set the buf_T * value to an invalid value (-1?), which
4869 * means we need checks in all access functions... Bah.
4870 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004871 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004872 * b_python_ref and b_python3_ref fields respectively.
4873 */
4874
4875 BufferObject *self;
4876
4877 if (BUF_PYTHON_REF(buf) != NULL)
4878 {
4879 self = BUF_PYTHON_REF(buf);
4880 Py_INCREF(self);
4881 }
4882 else
4883 {
4884 self = PyObject_NEW(BufferObject, &BufferType);
4885 if (self == NULL)
4886 return NULL;
4887 self->buf = buf;
4888 BUF_PYTHON_REF(buf) = self;
4889 }
4890
4891 return (PyObject *)(self);
4892}
4893
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004894 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004895BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004896{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004897 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4898 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004899
4900 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004901}
4902
Bram Moolenaar971db462013-05-12 18:44:48 +02004903 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004904BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004905{
4906 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004907 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004908 return -1; /* ??? */
4909
Bram Moolenaard6e39182013-05-21 18:30:34 +02004910 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004911}
4912
4913 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004914BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004915{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004916 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004917}
4918
4919 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004920BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004921{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004922 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004923}
4924
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004925static char *BufferAttrs[] = {
4926 "name", "number", "vars", "options", "valid",
4927 NULL
4928};
4929
4930 static PyObject *
4931BufferDir(PyObject *self)
4932{
4933 return ObjectDir(self, BufferAttrs);
4934}
4935
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004936 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004937BufferAttrValid(BufferObject *self, char *name)
4938{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004939 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004940
4941 if (strcmp(name, "valid") != 0)
4942 return NULL;
4943
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004944 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4945 Py_INCREF(ret);
4946 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004947}
4948
4949 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004950BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004951{
4952 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004953 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004954 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004955 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004956 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004957 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004958 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004959 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004960 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4961 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004962 else if (strcmp(name, "__members__") == 0)
4963 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004964 else
4965 return NULL;
4966}
4967
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004968 static int
4969BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4970{
4971 if (CheckBuffer(self))
4972 return -1;
4973
4974 if (strcmp(name, "name") == 0)
4975 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004976 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004977 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004978 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004979 PyObject *todecref;
4980
4981 if (!(val = StringToChars(valObject, &todecref)))
4982 return -1;
4983
4984 VimTryStart();
4985 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4986 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004987 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004988 aucmd_restbuf(&aco);
4989 Py_XDECREF(todecref);
4990 if (VimTryEnd())
4991 return -1;
4992
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004993 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004994 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004995 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004996 return -1;
4997 }
4998 return 0;
4999 }
5000 else
5001 {
5002 PyErr_SetString(PyExc_AttributeError, name);
5003 return -1;
5004 }
5005}
5006
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005007 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005008BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005009{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005010 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005011}
5012
5013 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005014BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005015{
5016 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005017 char_u *pmark;
5018 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02005019 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005020 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005021
Bram Moolenaard6e39182013-05-21 18:30:34 +02005022 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005023 return NULL;
5024
Bram Moolenaar389a1792013-06-23 13:00:44 +02005025 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005026 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005027
Bram Moolenaar389a1792013-06-23 13:00:44 +02005028 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005029 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005030 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005031 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005032 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005033 return NULL;
5034 }
5035
5036 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005037
5038 Py_XDECREF(todecref);
5039
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005040 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005041 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005042 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02005043 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005044 if (VimTryEnd())
5045 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005046
5047 if (posp == NULL)
5048 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005049 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005050 return NULL;
5051 }
5052
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005053 if (posp->lnum <= 0)
5054 {
5055 /* Or raise an error? */
5056 Py_INCREF(Py_None);
5057 return Py_None;
5058 }
5059
5060 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5061}
5062
5063 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005064BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005065{
5066 PyInt start;
5067 PyInt end;
5068
Bram Moolenaard6e39182013-05-21 18:30:34 +02005069 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005070 return NULL;
5071
5072 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5073 return NULL;
5074
Bram Moolenaard6e39182013-05-21 18:30:34 +02005075 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005076}
5077
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005078 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005079BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005080{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005081 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005082 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005083 else
5084 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005085 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005086
5087 if (name == NULL)
5088 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005089
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005090 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005091 }
5092}
5093
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005094static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005095 /* name, function, calling, documentation */
5096 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005097 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005098 {"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 +02005099 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5100 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005101};
5102
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005103/*
5104 * Buffer list object - Implementation
5105 */
5106
5107static PyTypeObject BufMapType;
5108
5109typedef struct
5110{
5111 PyObject_HEAD
5112} BufMapObject;
5113
5114 static PyInt
5115BufMapLength(PyObject *self UNUSED)
5116{
5117 buf_T *b = firstbuf;
5118 PyInt n = 0;
5119
5120 while (b)
5121 {
5122 ++n;
5123 b = b->b_next;
5124 }
5125
5126 return n;
5127}
5128
5129 static PyObject *
5130BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5131{
5132 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005133 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005134
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005135 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005136 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005137
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005138 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005139
5140 if (b)
5141 return BufferNew(b);
5142 else
5143 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005144 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005145 return NULL;
5146 }
5147}
5148
5149 static void
5150BufMapIterDestruct(PyObject *buffer)
5151{
5152 /* Iteration was stopped before all buffers were processed */
5153 if (buffer)
5154 {
5155 Py_DECREF(buffer);
5156 }
5157}
5158
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005159 static int
5160BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5161{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005162 if (buffer)
5163 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005164 return 0;
5165}
5166
5167 static int
5168BufMapIterClear(PyObject **buffer)
5169{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005170 if (*buffer)
5171 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005172 return 0;
5173}
5174
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005175 static PyObject *
5176BufMapIterNext(PyObject **buffer)
5177{
5178 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005179 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005180
5181 if (!*buffer)
5182 return NULL;
5183
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005184 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005185
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005186 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005187 {
5188 *buffer = NULL;
5189 return NULL;
5190 }
5191
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005192 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005193 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005194 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005195 return NULL;
5196 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005197 /* Do not increment reference: we no longer hold it (decref), but whoever
5198 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005199 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005200}
5201
5202 static PyObject *
5203BufMapIter(PyObject *self UNUSED)
5204{
5205 PyObject *buffer;
5206
5207 buffer = BufferNew(firstbuf);
5208 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005209 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5210 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005211}
5212
5213static PyMappingMethods BufMapAsMapping = {
5214 (lenfunc) BufMapLength,
5215 (binaryfunc) BufMapItem,
5216 (objobjargproc) 0,
5217};
5218
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005219/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005220 */
5221
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005222static char *CurrentAttrs[] = {
5223 "buffer", "window", "line", "range", "tabpage",
5224 NULL
5225};
5226
5227 static PyObject *
5228CurrentDir(PyObject *self)
5229{
5230 return ObjectDir(self, CurrentAttrs);
5231}
5232
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005233 static PyObject *
5234CurrentGetattr(PyObject *self UNUSED, char *name)
5235{
5236 if (strcmp(name, "buffer") == 0)
5237 return (PyObject *)BufferNew(curbuf);
5238 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005239 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005240 else if (strcmp(name, "tabpage") == 0)
5241 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005242 else if (strcmp(name, "line") == 0)
5243 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5244 else if (strcmp(name, "range") == 0)
5245 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005246 else if (strcmp(name, "__members__") == 0)
5247 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005248 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005249#if PY_MAJOR_VERSION < 3
5250 return Py_FindMethod(WindowMethods, self, name);
5251#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005252 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005253#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005254}
5255
5256 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005257CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005258{
5259 if (strcmp(name, "line") == 0)
5260 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005261 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5262 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005263 return -1;
5264
5265 return 0;
5266 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005267 else if (strcmp(name, "buffer") == 0)
5268 {
5269 int count;
5270
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005271 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005272 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005273 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005274 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005275 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005276 return -1;
5277 }
5278
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005279 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005280 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005281 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005282
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005283 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005284 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5285 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005286 if (VimTryEnd())
5287 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005288 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005289 return -1;
5290 }
5291
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005292 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005293 }
5294 else if (strcmp(name, "window") == 0)
5295 {
5296 int count;
5297
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005298 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005299 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005300 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005301 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005302 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005303 return -1;
5304 }
5305
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005306 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005307 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005308 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005309
5310 if (!count)
5311 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005312 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005313 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005314 return -1;
5315 }
5316
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005317 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005318 win_goto(((WindowObject *)(valObject))->win);
5319 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005320 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005321 if (VimTryEnd())
5322 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005323 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005324 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005325 return -1;
5326 }
5327
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005328 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005329 }
5330 else if (strcmp(name, "tabpage") == 0)
5331 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005332 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005333 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005334 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005335 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005336 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005337 return -1;
5338 }
5339
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005340 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005341 return -1;
5342
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005343 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005344 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5345 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005346 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005347 if (VimTryEnd())
5348 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005349 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005350 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005351 return -1;
5352 }
5353
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005354 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005355 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005356 else
5357 {
5358 PyErr_SetString(PyExc_AttributeError, name);
5359 return -1;
5360 }
5361}
5362
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005363static struct PyMethodDef CurrentMethods[] = {
5364 /* name, function, calling, documentation */
5365 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5366 { NULL, NULL, 0, NULL}
5367};
5368
Bram Moolenaardb913952012-06-29 12:54:53 +02005369 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005370init_range_cmd(exarg_T *eap)
5371{
5372 RangeStart = eap->line1;
5373 RangeEnd = eap->line2;
5374}
5375
5376 static void
5377init_range_eval(typval_T *rettv UNUSED)
5378{
5379 RangeStart = (PyInt) curwin->w_cursor.lnum;
5380 RangeEnd = RangeStart;
5381}
5382
5383 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005384run_cmd(const char *cmd, void *arg UNUSED
5385#ifdef PY_CAN_RECURSE
5386 , PyGILState_STATE *pygilstate UNUSED
5387#endif
5388 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005389{
Bram Moolenaar41009372013-07-01 22:03:04 +02005390 PyObject *run_ret;
5391 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5392 if (run_ret != NULL)
5393 {
5394 Py_DECREF(run_ret);
5395 }
5396 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5397 {
5398 EMSG2(_(e_py_systemexit), "python");
5399 PyErr_Clear();
5400 }
5401 else
5402 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005403}
5404
5405static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5406static int code_hdr_len = 30;
5407
5408 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005409run_do(const char *cmd, void *arg UNUSED
5410#ifdef PY_CAN_RECURSE
5411 , PyGILState_STATE *pygilstate
5412#endif
5413 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005414{
5415 PyInt lnum;
5416 size_t len;
5417 char *code;
5418 int status;
5419 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005420 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005421
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005422 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005423 {
5424 EMSG(_("cannot save undo information"));
5425 return;
5426 }
5427
5428 len = code_hdr_len + STRLEN(cmd);
5429 code = PyMem_New(char, len + 1);
5430 memcpy(code, code_hdr, code_hdr_len);
5431 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005432 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5433 status = -1;
5434 if (run_ret != NULL)
5435 {
5436 status = 0;
5437 Py_DECREF(run_ret);
5438 }
5439 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5440 {
5441 PyMem_Free(code);
5442 EMSG2(_(e_py_systemexit), "python");
5443 PyErr_Clear();
5444 return;
5445 }
5446 else
5447 PyErr_PrintEx(1);
5448
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005449 PyMem_Free(code);
5450
5451 if (status)
5452 {
5453 EMSG(_("failed to run the code"));
5454 return;
5455 }
5456
5457 status = 0;
5458 pymain = PyImport_AddModule("__main__");
5459 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005460#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005461 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005462#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005463
5464 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5465 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005466 PyObject *line;
5467 PyObject *linenr;
5468 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005469
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005470#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005471 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005472#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005473 if (!(line = GetBufferLine(curbuf, lnum)))
5474 goto err;
5475 if (!(linenr = PyInt_FromLong((long) lnum)))
5476 {
5477 Py_DECREF(line);
5478 goto err;
5479 }
5480 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5481 Py_DECREF(line);
5482 Py_DECREF(linenr);
5483 if (!ret)
5484 goto err;
5485
5486 if (ret != Py_None)
5487 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5488 goto err;
5489
5490 Py_XDECREF(ret);
5491 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005492#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005493 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005494#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005495 }
5496 goto out;
5497err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005498#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005499 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005500#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005501 PyErr_PrintEx(0);
5502 PythonIO_Flush();
5503 status = 1;
5504out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005505#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005506 if (!status)
5507 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005508#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005509 Py_DECREF(pyfunc);
5510 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5511 if (status)
5512 return;
5513 check_cursor();
5514 update_curbuf(NOT_VALID);
5515}
5516
5517 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005518run_eval(const char *cmd, typval_T *rettv
5519#ifdef PY_CAN_RECURSE
5520 , PyGILState_STATE *pygilstate UNUSED
5521#endif
5522 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005523{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005524 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005525
Bram Moolenaar41009372013-07-01 22:03:04 +02005526 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005527 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005528 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005529 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005530 {
5531 EMSG2(_(e_py_systemexit), "python");
5532 PyErr_Clear();
5533 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005534 else
5535 {
5536 if (PyErr_Occurred() && !msg_silent)
5537 PyErr_PrintEx(0);
5538 EMSG(_("E858: Eval did not return a valid python object"));
5539 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005540 }
5541 else
5542 {
Bram Moolenaar77324fc2016-01-17 22:37:03 +01005543 if (run_ret != Py_None && ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005544 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005545 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005546 }
5547 PyErr_Clear();
5548}
5549
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005550 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005551set_ref_in_py(const int copyID)
5552{
5553 pylinkedlist_T *cur;
5554 dict_T *dd;
5555 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005556 int abort = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +02005557
5558 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005559 {
5560 for(cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005561 {
5562 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5563 if (dd->dv_copyID != copyID)
5564 {
5565 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005566 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005567 }
5568 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005569 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005570
5571 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005572 {
5573 for(cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005574 {
5575 ll = ((ListObject *) (cur->pll_obj))->list;
5576 if (ll->lv_copyID != copyID)
5577 {
5578 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005579 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005580 }
5581 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005582 }
5583
5584 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005585}
5586
5587 static int
5588set_string_copy(char_u *str, typval_T *tv)
5589{
5590 tv->vval.v_string = vim_strsave(str);
5591 if (tv->vval.v_string == NULL)
5592 {
5593 PyErr_NoMemory();
5594 return -1;
5595 }
5596 return 0;
5597}
5598
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005599 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005600pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005601{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005602 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005603 char_u *key;
5604 dictitem_T *di;
5605 PyObject *keyObject;
5606 PyObject *valObject;
5607 Py_ssize_t iter = 0;
5608
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005609 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005610 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005611
5612 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005613 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005614
5615 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5616 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005617 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005618
Bram Moolenaara03e6312013-05-29 22:49:26 +02005619 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005620 {
5621 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005622 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005623 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005624
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005625 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005626 {
5627 dict_unref(dict);
5628 return -1;
5629 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005630
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005631 if (*key == NUL)
5632 {
5633 dict_unref(dict);
5634 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005635 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005636 return -1;
5637 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005638
5639 di = dictitem_alloc(key);
5640
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005641 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005642
5643 if (di == NULL)
5644 {
5645 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005646 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005647 return -1;
5648 }
5649 di->di_tv.v_lock = 0;
5650
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005651 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005652 {
5653 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005654 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005655 return -1;
5656 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005657
5658 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005659 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005660 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005661 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005662 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005663 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005664 return -1;
5665 }
5666 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005667
5668 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005669 return 0;
5670}
5671
5672 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005673pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005674{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005675 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005676 char_u *key;
5677 dictitem_T *di;
5678 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005679 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005680 PyObject *keyObject;
5681 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005682
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005683 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005684 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005685
5686 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005687 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005688
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005689 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005690 {
5691 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005692 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005693 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005694
5695 if (!(iterator = PyObject_GetIter(list)))
5696 {
5697 dict_unref(dict);
5698 Py_DECREF(list);
5699 return -1;
5700 }
5701 Py_DECREF(list);
5702
5703 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005704 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005705 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005706
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005707 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005708 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005709 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005710 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005711 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005712 return -1;
5713 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005714
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005715 if (*key == NUL)
5716 {
5717 Py_DECREF(keyObject);
5718 Py_DECREF(iterator);
5719 Py_XDECREF(todecref);
5720 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005721 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005722 return -1;
5723 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005724
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005725 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005726 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005727 Py_DECREF(keyObject);
5728 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005729 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005730 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005731 return -1;
5732 }
5733
5734 di = dictitem_alloc(key);
5735
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005736 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005737 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005738
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005739 if (di == NULL)
5740 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005741 Py_DECREF(iterator);
5742 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005743 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005744 PyErr_NoMemory();
5745 return -1;
5746 }
5747 di->di_tv.v_lock = 0;
5748
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005749 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005750 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005751 Py_DECREF(iterator);
5752 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005753 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005754 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005755 return -1;
5756 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005757
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005758 Py_DECREF(valObject);
5759
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005760 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005761 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005762 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005763 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005764 dictitem_free(di);
5765 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005766 return -1;
5767 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005768 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005769 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005770 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005771 return 0;
5772}
5773
5774 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005775pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005776{
5777 list_T *l;
5778
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005779 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005780 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005781
5782 tv->v_type = VAR_LIST;
5783 tv->vval.v_list = l;
5784
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005785 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005786 {
5787 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005788 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005789 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005790
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005791 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005792 return 0;
5793}
5794
Bram Moolenaardb913952012-06-29 12:54:53 +02005795typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5796
5797 static int
5798convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005799 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005800{
5801 PyObject *capsule;
5802 char hexBuf[sizeof(void *) * 2 + 3];
5803
5804 sprintf(hexBuf, "%p", obj);
5805
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005806# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005807 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005808# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005809 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005810# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005811 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005812 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005813# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005814 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005815# else
5816 capsule = PyCObject_FromVoidPtr(tv, NULL);
5817# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005818 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5819 {
5820 Py_DECREF(capsule);
5821 tv->v_type = VAR_UNKNOWN;
5822 return -1;
5823 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005824
5825 Py_DECREF(capsule);
5826
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005827 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005828 {
5829 tv->v_type = VAR_UNKNOWN;
5830 return -1;
5831 }
5832 /* As we are not using copy_tv which increments reference count we must
5833 * do it ourself. */
5834 switch(tv->v_type)
5835 {
5836 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5837 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5838 }
5839 }
5840 else
5841 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005842 typval_T *v;
5843
5844# ifdef PY_USE_CAPSULE
5845 v = PyCapsule_GetPointer(capsule, NULL);
5846# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005847 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005848# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005849 copy_tv(v, tv);
5850 }
5851 return 0;
5852}
5853
5854 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005855ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5856{
5857 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005858 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005859
5860 if (!(lookup_dict = PyDict_New()))
5861 return -1;
5862
5863 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5864 {
5865 tv->v_type = VAR_DICT;
5866 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5867 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005868 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005869 }
5870 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005871 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005872 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005873 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005874 else
5875 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005876 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005877 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005878 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005879 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005880 }
5881 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005882 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005883}
5884
5885 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005886ConvertFromPyObject(PyObject *obj, typval_T *tv)
5887{
5888 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005889 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005890
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005891 if (!(lookup_dict = PyDict_New()))
5892 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005893 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005894 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005895 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005896}
5897
5898 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005899_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005900{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005901 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005902 {
5903 tv->v_type = VAR_DICT;
5904 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5905 ++tv->vval.v_dict->dv_refcount;
5906 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005907 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005908 {
5909 tv->v_type = VAR_LIST;
5910 tv->vval.v_list = (((ListObject *)(obj))->list);
5911 ++tv->vval.v_list->lv_refcount;
5912 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005913 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005914 {
5915 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5916 return -1;
5917
5918 tv->v_type = VAR_FUNC;
5919 func_ref(tv->vval.v_string);
5920 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005921 else if (PyBytes_Check(obj))
5922 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005923 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005924
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005925 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005926 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005927 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005928 return -1;
5929
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005930 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005931 return -1;
5932
5933 tv->v_type = VAR_STRING;
5934 }
5935 else if (PyUnicode_Check(obj))
5936 {
5937 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005938 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005939
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005940 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005941 if (bytes == NULL)
5942 return -1;
5943
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005944 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005945 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005946 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005947 return -1;
5948
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005949 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005950 {
5951 Py_XDECREF(bytes);
5952 return -1;
5953 }
5954 Py_XDECREF(bytes);
5955
5956 tv->v_type = VAR_STRING;
5957 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005958#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005959 else if (PyInt_Check(obj))
5960 {
5961 tv->v_type = VAR_NUMBER;
5962 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005963 if (PyErr_Occurred())
5964 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005965 }
5966#endif
5967 else if (PyLong_Check(obj))
5968 {
5969 tv->v_type = VAR_NUMBER;
5970 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005971 if (PyErr_Occurred())
5972 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005973 }
5974 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005975 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005976#ifdef FEAT_FLOAT
5977 else if (PyFloat_Check(obj))
5978 {
5979 tv->v_type = VAR_FLOAT;
5980 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5981 }
5982#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005983 else if (PyObject_HasAttrString(obj, "keys"))
5984 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005985 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005986 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005987 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005988 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005989 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005990 else if (PyNumber_Check(obj))
5991 {
5992 PyObject *num;
5993
5994 if (!(num = PyNumber_Long(obj)))
5995 return -1;
5996
5997 tv->v_type = VAR_NUMBER;
5998 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5999
6000 Py_DECREF(num);
6001 }
Bram Moolenaardb913952012-06-29 12:54:53 +02006002 else
6003 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006004 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006005 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006006 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006007 return -1;
6008 }
6009 return 0;
6010}
6011
6012 static PyObject *
6013ConvertToPyObject(typval_T *tv)
6014{
6015 if (tv == NULL)
6016 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006017 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006018 return NULL;
6019 }
6020 switch (tv->v_type)
6021 {
6022 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006023 return PyBytes_FromString(tv->vval.v_string == NULL
6024 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006025 case VAR_NUMBER:
6026 return PyLong_FromLong((long) tv->vval.v_number);
6027#ifdef FEAT_FLOAT
6028 case VAR_FLOAT:
6029 return PyFloat_FromDouble((double) tv->vval.v_float);
6030#endif
6031 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006032 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006033 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006034 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006035 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006036 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006037 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006038 case VAR_UNKNOWN:
6039 Py_INCREF(Py_None);
6040 return Py_None;
6041 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006042 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006043 return NULL;
6044 }
6045}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006046
6047typedef struct
6048{
6049 PyObject_HEAD
6050} CurrentObject;
6051static PyTypeObject CurrentType;
6052
6053 static void
6054init_structs(void)
6055{
6056 vim_memset(&OutputType, 0, sizeof(OutputType));
6057 OutputType.tp_name = "vim.message";
6058 OutputType.tp_basicsize = sizeof(OutputObject);
6059 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6060 OutputType.tp_doc = "vim message object";
6061 OutputType.tp_methods = OutputMethods;
6062#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006063 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6064 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006065 OutputType.tp_alloc = call_PyType_GenericAlloc;
6066 OutputType.tp_new = call_PyType_GenericNew;
6067 OutputType.tp_free = call_PyObject_Free;
6068#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006069 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6070 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006071#endif
6072
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006073 vim_memset(&IterType, 0, sizeof(IterType));
6074 IterType.tp_name = "vim.iter";
6075 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006076 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006077 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006078 IterType.tp_iter = (getiterfunc)IterIter;
6079 IterType.tp_iternext = (iternextfunc)IterNext;
6080 IterType.tp_dealloc = (destructor)IterDestructor;
6081 IterType.tp_traverse = (traverseproc)IterTraverse;
6082 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006083
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006084 vim_memset(&BufferType, 0, sizeof(BufferType));
6085 BufferType.tp_name = "vim.buffer";
6086 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006087 BufferType.tp_dealloc = (destructor)BufferDestructor;
6088 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006089 BufferType.tp_as_sequence = &BufferAsSeq;
6090 BufferType.tp_as_mapping = &BufferAsMapping;
6091 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6092 BufferType.tp_doc = "vim buffer object";
6093 BufferType.tp_methods = BufferMethods;
6094#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006095 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006096 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006097 BufferType.tp_alloc = call_PyType_GenericAlloc;
6098 BufferType.tp_new = call_PyType_GenericNew;
6099 BufferType.tp_free = call_PyObject_Free;
6100#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006101 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006102 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006103#endif
6104
6105 vim_memset(&WindowType, 0, sizeof(WindowType));
6106 WindowType.tp_name = "vim.window";
6107 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006108 WindowType.tp_dealloc = (destructor)WindowDestructor;
6109 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006110 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006111 WindowType.tp_doc = "vim Window object";
6112 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006113 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6114 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006115#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006116 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6117 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006118 WindowType.tp_alloc = call_PyType_GenericAlloc;
6119 WindowType.tp_new = call_PyType_GenericNew;
6120 WindowType.tp_free = call_PyObject_Free;
6121#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006122 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6123 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006124#endif
6125
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006126 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6127 TabPageType.tp_name = "vim.tabpage";
6128 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006129 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6130 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006131 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6132 TabPageType.tp_doc = "vim tab page object";
6133 TabPageType.tp_methods = TabPageMethods;
6134#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006135 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006136 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6137 TabPageType.tp_new = call_PyType_GenericNew;
6138 TabPageType.tp_free = call_PyObject_Free;
6139#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006140 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006141#endif
6142
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006143 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6144 BufMapType.tp_name = "vim.bufferlist";
6145 BufMapType.tp_basicsize = sizeof(BufMapObject);
6146 BufMapType.tp_as_mapping = &BufMapAsMapping;
6147 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006148 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006149 BufferType.tp_doc = "vim buffer list";
6150
6151 vim_memset(&WinListType, 0, sizeof(WinListType));
6152 WinListType.tp_name = "vim.windowlist";
6153 WinListType.tp_basicsize = sizeof(WinListType);
6154 WinListType.tp_as_sequence = &WinListAsSeq;
6155 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6156 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006157 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006158
6159 vim_memset(&TabListType, 0, sizeof(TabListType));
6160 TabListType.tp_name = "vim.tabpagelist";
6161 TabListType.tp_basicsize = sizeof(TabListType);
6162 TabListType.tp_as_sequence = &TabListAsSeq;
6163 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6164 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006165
6166 vim_memset(&RangeType, 0, sizeof(RangeType));
6167 RangeType.tp_name = "vim.range";
6168 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006169 RangeType.tp_dealloc = (destructor)RangeDestructor;
6170 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006171 RangeType.tp_as_sequence = &RangeAsSeq;
6172 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006173 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006174 RangeType.tp_doc = "vim Range object";
6175 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006176 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6177 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006178#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006179 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006180 RangeType.tp_alloc = call_PyType_GenericAlloc;
6181 RangeType.tp_new = call_PyType_GenericNew;
6182 RangeType.tp_free = call_PyObject_Free;
6183#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006184 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006185#endif
6186
6187 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6188 CurrentType.tp_name = "vim.currentdata";
6189 CurrentType.tp_basicsize = sizeof(CurrentObject);
6190 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6191 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006192 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006193#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006194 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6195 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006196#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006197 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6198 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006199#endif
6200
6201 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6202 DictionaryType.tp_name = "vim.dictionary";
6203 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006204 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006205 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006206 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006207 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006208 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6209 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006210 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6211 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6212 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006213#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006214 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6215 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006216#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006217 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6218 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006219#endif
6220
6221 vim_memset(&ListType, 0, sizeof(ListType));
6222 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006223 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006224 ListType.tp_basicsize = sizeof(ListObject);
6225 ListType.tp_as_sequence = &ListAsSeq;
6226 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006227 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006228 ListType.tp_doc = "list pushing modifications to vim structure";
6229 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006230 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006231 ListType.tp_new = (newfunc)ListConstructor;
6232 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006233#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006234 ListType.tp_getattro = (getattrofunc)ListGetattro;
6235 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006236#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006237 ListType.tp_getattr = (getattrfunc)ListGetattr;
6238 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006239#endif
6240
6241 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006242 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006243 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006244 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6245 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006246 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006247 FunctionType.tp_doc = "object that calls vim function";
6248 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006249 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006250 FunctionType.tp_new = (newfunc)FunctionConstructor;
6251 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006252#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006253 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006254#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006255 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006256#endif
6257
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006258 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6259 OptionsType.tp_name = "vim.options";
6260 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006261 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006262 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006263 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006264 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006265 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006266 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6267 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6268 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006269
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006270 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6271 LoaderType.tp_name = "vim.Loader";
6272 LoaderType.tp_basicsize = sizeof(LoaderObject);
6273 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6274 LoaderType.tp_doc = "vim message object";
6275 LoaderType.tp_methods = LoaderMethods;
6276 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6277
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006278#if PY_MAJOR_VERSION >= 3
6279 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6280 vimmodule.m_name = "vim";
6281 vimmodule.m_doc = "Vim Python interface\n";
6282 vimmodule.m_size = -1;
6283 vimmodule.m_methods = VimMethods;
6284#endif
6285}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006286
6287#define PYTYPE_READY(type) \
6288 if (PyType_Ready(&type)) \
6289 return -1;
6290
6291 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006292init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006293{
6294 PYTYPE_READY(IterType);
6295 PYTYPE_READY(BufferType);
6296 PYTYPE_READY(RangeType);
6297 PYTYPE_READY(WindowType);
6298 PYTYPE_READY(TabPageType);
6299 PYTYPE_READY(BufMapType);
6300 PYTYPE_READY(WinListType);
6301 PYTYPE_READY(TabListType);
6302 PYTYPE_READY(CurrentType);
6303 PYTYPE_READY(DictionaryType);
6304 PYTYPE_READY(ListType);
6305 PYTYPE_READY(FunctionType);
6306 PYTYPE_READY(OptionsType);
6307 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006308 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006309 return 0;
6310}
6311
6312 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006313init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006314{
6315 PyObject *path;
6316 PyObject *path_hook;
6317 PyObject *path_hooks;
6318
6319 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6320 return -1;
6321
6322 if (!(path_hooks = PySys_GetObject("path_hooks")))
6323 {
6324 PyErr_Clear();
6325 path_hooks = PyList_New(1);
6326 PyList_SET_ITEM(path_hooks, 0, path_hook);
6327 if (PySys_SetObject("path_hooks", path_hooks))
6328 {
6329 Py_DECREF(path_hooks);
6330 return -1;
6331 }
6332 Py_DECREF(path_hooks);
6333 }
6334 else if (PyList_Check(path_hooks))
6335 {
6336 if (PyList_Append(path_hooks, path_hook))
6337 {
6338 Py_DECREF(path_hook);
6339 return -1;
6340 }
6341 Py_DECREF(path_hook);
6342 }
6343 else
6344 {
6345 VimTryStart();
6346 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6347 "You should now do the following:\n"
6348 "- append vim.path_hook to sys.path_hooks\n"
6349 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6350 VimTryEnd(); /* Discard the error */
6351 Py_DECREF(path_hook);
6352 return 0;
6353 }
6354
6355 if (!(path = PySys_GetObject("path")))
6356 {
6357 PyErr_Clear();
6358 path = PyList_New(1);
6359 Py_INCREF(vim_special_path_object);
6360 PyList_SET_ITEM(path, 0, vim_special_path_object);
6361 if (PySys_SetObject("path", path))
6362 {
6363 Py_DECREF(path);
6364 return -1;
6365 }
6366 Py_DECREF(path);
6367 }
6368 else if (PyList_Check(path))
6369 {
6370 if (PyList_Append(path, vim_special_path_object))
6371 return -1;
6372 }
6373 else
6374 {
6375 VimTryStart();
6376 EMSG(_("Failed to set path: sys.path is not a list\n"
6377 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6378 VimTryEnd(); /* Discard the error */
6379 }
6380
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006381 return 0;
6382}
6383
6384static BufMapObject TheBufferMap =
6385{
6386 PyObject_HEAD_INIT(&BufMapType)
6387};
6388
6389static WinListObject TheWindowList =
6390{
6391 PyObject_HEAD_INIT(&WinListType)
6392 NULL
6393};
6394
6395static CurrentObject TheCurrent =
6396{
6397 PyObject_HEAD_INIT(&CurrentType)
6398};
6399
6400static TabListObject TheTabPageList =
6401{
6402 PyObject_HEAD_INIT(&TabListType)
6403};
6404
6405static struct numeric_constant {
6406 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006407 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006408} numeric_constants[] = {
6409 {"VAR_LOCKED", VAR_LOCKED},
6410 {"VAR_FIXED", VAR_FIXED},
6411 {"VAR_SCOPE", VAR_SCOPE},
6412 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6413};
6414
6415static struct object_constant {
6416 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006417 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006418} object_constants[] = {
6419 {"buffers", (PyObject *)(void *)&TheBufferMap},
6420 {"windows", (PyObject *)(void *)&TheWindowList},
6421 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6422 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006423
6424 {"Buffer", (PyObject *)&BufferType},
6425 {"Range", (PyObject *)&RangeType},
6426 {"Window", (PyObject *)&WindowType},
6427 {"TabPage", (PyObject *)&TabPageType},
6428 {"Dictionary", (PyObject *)&DictionaryType},
6429 {"List", (PyObject *)&ListType},
6430 {"Function", (PyObject *)&FunctionType},
6431 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006432 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006433};
6434
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006435#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006436 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006437 return -1;
6438
6439#define ADD_CHECKED_OBJECT(m, name, obj) \
6440 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006441 PyObject *valObject = obj; \
6442 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006443 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006444 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006445 }
6446
6447 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006448populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006449{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006450 int i;
6451 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006452 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006453 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006454
6455 for (i = 0; i < (int)(sizeof(numeric_constants)
6456 / sizeof(struct numeric_constant));
6457 ++i)
6458 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006459 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006460
6461 for (i = 0; i < (int)(sizeof(object_constants)
6462 / sizeof(struct object_constant));
6463 ++i)
6464 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006465 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006466
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006467 valObject = object_constants[i].valObject;
6468 Py_INCREF(valObject);
6469 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006470 }
6471
6472 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6473 return -1;
6474 ADD_OBJECT(m, "error", VimError);
6475
Bram Moolenaara9922d62013-05-30 13:01:18 +02006476 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6477 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006478 ADD_CHECKED_OBJECT(m, "options",
6479 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006480
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006481 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006482 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006483 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006484
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006485 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006486 return -1;
6487 ADD_OBJECT(m, "_getcwd", py_getcwd)
6488
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006489 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006490 return -1;
6491 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006492 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006493 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006494 if (PyObject_SetAttrString(other_module, "chdir", attr))
6495 {
6496 Py_DECREF(attr);
6497 return -1;
6498 }
6499 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006500
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006501 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006502 {
6503 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006504 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006505 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006506 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6507 {
6508 Py_DECREF(attr);
6509 return -1;
6510 }
6511 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006512 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006513 else
6514 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006515
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006516 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6517 return -1;
6518
6519 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6520
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006521 if (!(imp = PyImport_ImportModule("imp")))
6522 return -1;
6523
6524 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6525 {
6526 Py_DECREF(imp);
6527 return -1;
6528 }
6529
6530 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6531 {
6532 Py_DECREF(py_find_module);
6533 Py_DECREF(imp);
6534 return -1;
6535 }
6536
6537 Py_DECREF(imp);
6538
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006539 ADD_OBJECT(m, "_find_module", py_find_module);
6540 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006541
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006542 return 0;
6543}