blob: 6e3939abe569f4b634d990a7d1e6a1df5939fa73 [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 Moolenaar6f1404f2013-06-23 16:04:08 +0200239 N_("number must be greater then zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200240 return -1;
241 }
242 }
243 else if (flags & NUMBER_UNSIGNED)
244 {
245 if (*result < 0)
246 {
247 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200248 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200249 return -1;
250 }
251 }
252
253 return 0;
254}
255
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200256 static int
257add_string(PyObject *list, char *s)
258{
259 PyObject *string;
260
261 if (!(string = PyString_FromString(s)))
262 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200263
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200264 if (PyList_Append(list, string))
265 {
266 Py_DECREF(string);
267 return -1;
268 }
269
270 Py_DECREF(string);
271 return 0;
272}
273
274 static PyObject *
275ObjectDir(PyObject *self, char **attributes)
276{
277 PyMethodDef *method;
278 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200279 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200280
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200281 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200282 return NULL;
283
284 if (self)
285 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200286 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200287 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200288 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200289 return NULL;
290 }
291
292 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200293 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200294 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200295 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200296 return NULL;
297 }
298
299#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200300 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200301 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200302 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200303 return NULL;
304 }
305#endif
306
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200307 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200308}
309
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200310/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200311 */
312
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200313/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200314typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200315
316static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200317
318typedef struct
319{
320 PyObject_HEAD
321 long softspace;
322 long error;
323} OutputObject;
324
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200325static char *OutputAttrs[] = {
326 "softspace",
327 NULL
328};
329
330 static PyObject *
331OutputDir(PyObject *self)
332{
333 return ObjectDir(self, OutputAttrs);
334}
335
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200337OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200339 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200340 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200341 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200342 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200343 return -1;
344 }
345
346 if (strcmp(name, "softspace") == 0)
347 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200348 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200349 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return 0;
351 }
352
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200353 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200354 return -1;
355}
356
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200357/* Buffer IO, we write one whole line at a time. */
358static garray_T io_ga = {0, 0, 1, 80, NULL};
359static writefn old_fn = NULL;
360
361 static void
362PythonIO_Flush(void)
363{
364 if (old_fn != NULL && io_ga.ga_len > 0)
365 {
366 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
367 old_fn((char_u *)io_ga.ga_data);
368 }
369 io_ga.ga_len = 0;
370}
371
372 static void
373writer(writefn fn, char_u *str, PyInt n)
374{
375 char_u *ptr;
376
377 /* Flush when switching output function. */
378 if (fn != old_fn)
379 PythonIO_Flush();
380 old_fn = fn;
381
382 /* Write each NL separated line. Text after the last NL is kept for
383 * writing later. */
384 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
385 {
386 PyInt len = ptr - str;
387
388 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
389 break;
390
391 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
392 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
393 fn((char_u *)io_ga.ga_data);
394 str = ptr + 1;
395 n -= len + 1;
396 io_ga.ga_len = 0;
397 }
398
399 /* Put the remaining text into io_ga for later printing. */
400 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
401 {
402 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
403 io_ga.ga_len += (int)n;
404 }
405}
406
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200407 static int
408write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200409{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200410 Py_ssize_t len = 0;
411 char *str = NULL;
412 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200413
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200414 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
415 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200416
417 Py_BEGIN_ALLOW_THREADS
418 Python_Lock_Vim();
419 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
420 Python_Release_Vim();
421 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200422 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200423
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200424 return 0;
425}
426
427 static PyObject *
428OutputWrite(OutputObject *self, PyObject *string)
429{
430 if (write_output(self, string))
431 return NULL;
432
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200433 Py_INCREF(Py_None);
434 return Py_None;
435}
436
437 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200438OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200439{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200440 PyObject *iterator;
441 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200443 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200446 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200448 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200449 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200450 Py_DECREF(iterator);
451 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200452 return NULL;
453 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200454 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 }
456
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200457 Py_DECREF(iterator);
458
459 /* Iterator may have finished due to an exception */
460 if (PyErr_Occurred())
461 return NULL;
462
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200463 Py_INCREF(Py_None);
464 return Py_None;
465}
466
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100467 static PyObject *
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200468OutputFlush(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100469{
470 /* do nothing */
471 Py_INCREF(Py_None);
472 return Py_None;
473}
474
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200475/***************/
476
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200477static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200478 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200479 {"write", (PyCFunction)OutputWrite, METH_O, ""},
480 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200481 {"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200482 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200483 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200484};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200485
486static OutputObject Output =
487{
488 PyObject_HEAD_INIT(&OutputType)
489 0,
490 0
491};
492
493static OutputObject Error =
494{
495 PyObject_HEAD_INIT(&OutputType)
496 0,
497 1
498};
499
500 static int
501PythonIO_Init_io(void)
502{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200503 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
504 return -1;
505 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
506 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200507
508 if (PyErr_Occurred())
509 {
510 EMSG(_("E264: Python: Error initialising I/O objects"));
511 return -1;
512 }
513
514 return 0;
515}
516
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200517typedef struct
518{
519 PyObject_HEAD
520 PyObject *module;
521} LoaderObject;
522static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200523
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200524 static void
525LoaderDestructor(LoaderObject *self)
526{
527 Py_DECREF(self->module);
528 DESTRUCTOR_FINISH(self);
529}
530
531 static PyObject *
532LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
533{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200534 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200535
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200536 Py_INCREF(ret);
537 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200538}
539
540static struct PyMethodDef LoaderMethods[] = {
541 /* name, function, calling, doc */
542 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
543 { NULL, NULL, 0, NULL}
544};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200545
546/* Check to see whether a Vim error has been reported, or a keyboard
547 * interrupt has been detected.
548 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200549
550 static void
551VimTryStart(void)
552{
553 ++trylevel;
554}
555
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200556 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200557VimTryEnd(void)
558{
559 --trylevel;
Bram Moolenaar41009372013-07-01 22:03:04 +0200560 /* Without this it stops processing all subsequent VimL commands and
561 * generates strange error messages if I e.g. try calling Test() in a
562 * cycle */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200563 did_emsg = FALSE;
564 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200565 if (got_int)
566 {
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100567 if (current_exception != NULL)
568 discard_current_exception();
569 else
570 need_rethrow = did_throw = FALSE;
571 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200572 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200573 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200574 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100575 else if (msg_list != NULL && *msg_list != NULL)
576 {
577 int should_free;
578 char_u *msg;
579
580 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
581
582 if (msg == NULL)
583 {
584 PyErr_NoMemory();
585 return -1;
586 }
587
588 PyErr_SetVim((char *) msg);
589
590 free_global_msglist();
591
592 if (should_free)
593 vim_free(msg);
594
595 return -1;
596 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200597 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200598 return (PyErr_Occurred() ? -1 : 0);
599 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200600 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200601 {
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100602 if (current_exception != NULL)
603 discard_current_exception();
604 else
605 need_rethrow = did_throw = FALSE;
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200606 return -1;
607 }
608 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200609 else
610 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200611 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200612 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200613 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200614 }
615}
616
617 static int
618VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200619{
620 if (got_int)
621 {
622 PyErr_SetNone(PyExc_KeyboardInterrupt);
623 return 1;
624 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200625 return 0;
626}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200627
628/* Vim module - Implementation
629 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200630
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200631 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200632VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200633{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200634 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200635 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200636 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200637
Bram Moolenaar389a1792013-06-23 13:00:44 +0200638 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200639 return NULL;
640
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200641 Py_BEGIN_ALLOW_THREADS
642 Python_Lock_Vim();
643
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200644 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200645 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200646 update_screen(VALID);
647
648 Python_Release_Vim();
649 Py_END_ALLOW_THREADS
650
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200651 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200652 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200653 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200654 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200655
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200656 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200657 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200658 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200659}
660
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200661/*
662 * Function to translate a typval_T into a PyObject; this will recursively
663 * translate lists/dictionaries into their Python equivalents.
664 *
665 * The depth parameter is to avoid infinite recursion, set it to 1 when
666 * you call VimToPython.
667 */
668 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200669VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200670{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200671 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200672 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200673 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200674
675 /* Avoid infinite recursion */
676 if (depth > 100)
677 {
678 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200679 ret = Py_None;
680 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200681 }
682
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200683 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200684 * then and we can use it again. */
685 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
686 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
687 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200688 sprintf(ptrBuf, "%p",
689 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
690 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200691
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200692 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200693 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200694 Py_INCREF(ret);
695 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200696 }
697 }
698
699 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200700 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200701 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200702 else if (our_tv->v_type == VAR_NUMBER)
703 {
704 char buf[NUMBUFLEN];
705
706 /* For backwards compatibility numbers are stored as strings. */
707 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200708 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200709 }
710# ifdef FEAT_FLOAT
711 else if (our_tv->v_type == VAR_FLOAT)
712 {
713 char buf[NUMBUFLEN];
714
715 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200716 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200717 }
718# endif
719 else if (our_tv->v_type == VAR_LIST)
720 {
721 list_T *list = our_tv->vval.v_list;
722 listitem_T *curr;
723
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200724 if (list == NULL)
725 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200728 return NULL;
729
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200730 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200731 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200732 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200733 return NULL;
734 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200735
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200736 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
737 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200738 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200739 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200740 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200741 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200742 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200743 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200744 {
745 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200746 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200747 return NULL;
748 }
749 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200750 }
751 }
752 else if (our_tv->v_type == VAR_DICT)
753 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200754
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200755 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
756 long_u todo = ht->ht_used;
757 hashitem_T *hi;
758 dictitem_T *di;
759 if (our_tv->vval.v_dict == NULL)
760 return NULL;
761
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200762 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200763 return NULL;
764
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200765 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200766 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200767 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200768 return NULL;
769 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200770
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200771 for (hi = ht->ht_array; todo > 0; ++hi)
772 {
773 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200774 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200775 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200776
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200777 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200778 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200779 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200780 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200781 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200782 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200783 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200784 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200785 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200786 Py_DECREF(newObj);
787 return NULL;
788 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200789 }
790 }
791 }
792 else
793 {
794 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200795 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200796 }
797
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200798 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200799}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200800
801 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200802VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200803{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200804 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200806 PyObject *string;
807 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200808 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200809 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810
Bram Moolenaar389a1792013-06-23 13:00:44 +0200811 if (!PyArg_ParseTuple(args, "O", &string))
812 return NULL;
813
814 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200815 return NULL;
816
817 Py_BEGIN_ALLOW_THREADS
818 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200819 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200820 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200821 Python_Release_Vim();
822 Py_END_ALLOW_THREADS
823
Bram Moolenaar389a1792013-06-23 13:00:44 +0200824 Py_XDECREF(todecref);
825
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200826 if (VimTryEnd())
827 return NULL;
828
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200829 if (our_tv == NULL)
830 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200831 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200832 return NULL;
833 }
834
835 /* Convert the Vim type into a Python type. Create a dictionary that's
836 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200837 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200838 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200839 else
840 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200841 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200842 Py_DECREF(lookup_dict);
843 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200844
845
846 Py_BEGIN_ALLOW_THREADS
847 Python_Lock_Vim();
848 free_tv(our_tv);
849 Python_Release_Vim();
850 Py_END_ALLOW_THREADS
851
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200852 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200853}
854
Bram Moolenaardb913952012-06-29 12:54:53 +0200855static PyObject *ConvertToPyObject(typval_T *);
856
857 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200858VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200859{
Bram Moolenaardb913952012-06-29 12:54:53 +0200860 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200861 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200862 char_u *expr;
863 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200864
Bram Moolenaar389a1792013-06-23 13:00:44 +0200865 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200866 return NULL;
867
868 Py_BEGIN_ALLOW_THREADS
869 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200870 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200871 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200872 Python_Release_Vim();
873 Py_END_ALLOW_THREADS
874
Bram Moolenaar389a1792013-06-23 13:00:44 +0200875 Py_XDECREF(todecref);
876
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200877 if (VimTryEnd())
878 return NULL;
879
Bram Moolenaardb913952012-06-29 12:54:53 +0200880 if (our_tv == NULL)
881 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200882 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200883 return NULL;
884 }
885
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200886 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200887 Py_BEGIN_ALLOW_THREADS
888 Python_Lock_Vim();
889 free_tv(our_tv);
890 Python_Release_Vim();
891 Py_END_ALLOW_THREADS
892
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200893 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200894}
895
896 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200897VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200898{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200899 char_u *str;
900 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200901 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200902
Bram Moolenaar389a1792013-06-23 13:00:44 +0200903 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200904 return NULL;
905
Bram Moolenaara54bf402012-12-05 16:30:07 +0100906#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200907 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100908#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200909 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100910#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200911
912 Py_XDECREF(todecref);
913
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200914 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200915}
916
Bram Moolenaarf4258302013-06-02 18:20:17 +0200917 static PyObject *
918_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
919{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200920 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200921 PyObject *newwd;
922 PyObject *todecref;
923 char_u *new_dir;
924
Bram Moolenaard4209d22013-06-05 20:34:15 +0200925 if (_chdir == NULL)
926 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200927 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200928 return NULL;
929
930 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
931 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200932 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200933 return NULL;
934 }
935
936 if (!(new_dir = StringToChars(newwd, &todecref)))
937 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200938 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200939 Py_DECREF(newwd);
940 return NULL;
941 }
942
943 VimTryStart();
944
945 if (vim_chdir(new_dir))
946 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200947 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200948 Py_DECREF(newwd);
949 Py_XDECREF(todecref);
950
951 if (VimTryEnd())
952 return NULL;
953
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200954 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200955 return NULL;
956 }
957
958 Py_DECREF(newwd);
959 Py_XDECREF(todecref);
960
961 post_chdir(FALSE);
962
963 if (VimTryEnd())
964 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200965 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200966 return NULL;
967 }
968
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200969 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200970}
971
972 static PyObject *
973VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
974{
975 return _VimChdir(py_chdir, args, kwargs);
976}
977
978 static PyObject *
979VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
980{
981 return _VimChdir(py_fchdir, args, kwargs);
982}
983
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200984typedef struct {
985 PyObject *callable;
986 PyObject *result;
987} map_rtp_data;
988
989 static void
990map_rtp_callback(char_u *path, void *_data)
991{
992 void **data = (void **) _data;
993 PyObject *pathObject;
994 map_rtp_data *mr_data = *((map_rtp_data **) data);
995
Bram Moolenaar41009372013-07-01 22:03:04 +0200996 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200997 {
998 *data = NULL;
999 return;
1000 }
1001
1002 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1003 pathObject, NULL);
1004
1005 Py_DECREF(pathObject);
1006
1007 if (!mr_data->result || mr_data->result != Py_None)
1008 *data = NULL;
1009 else
1010 {
1011 Py_DECREF(mr_data->result);
1012 mr_data->result = NULL;
1013 }
1014}
1015
1016 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001017VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001018{
1019 map_rtp_data data;
1020
Bram Moolenaar389a1792013-06-23 13:00:44 +02001021 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001022 data.result = NULL;
1023
1024 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
1025
1026 if (data.result == NULL)
1027 {
1028 if (PyErr_Occurred())
1029 return NULL;
1030 else
1031 {
1032 Py_INCREF(Py_None);
1033 return Py_None;
1034 }
1035 }
1036 return data.result;
1037}
1038
1039/*
1040 * _vim_runtimepath_ special path implementation.
1041 */
1042
1043 static void
1044map_finder_callback(char_u *path, void *_data)
1045{
1046 void **data = (void **) _data;
1047 PyObject *list = *((PyObject **) data);
1048 PyObject *pathObject1, *pathObject2;
1049 char *pathbuf;
1050 size_t pathlen;
1051
1052 pathlen = STRLEN(path);
1053
1054#if PY_MAJOR_VERSION < 3
1055# define PY_MAIN_DIR_STRING "python2"
1056#else
1057# define PY_MAIN_DIR_STRING "python3"
1058#endif
1059#define PY_ALTERNATE_DIR_STRING "pythonx"
1060
1061#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1062 if (!(pathbuf = PyMem_New(char,
1063 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1064 {
1065 PyErr_NoMemory();
1066 *data = NULL;
1067 return;
1068 }
1069
1070 mch_memmove(pathbuf, path, pathlen + 1);
1071 add_pathsep((char_u *) pathbuf);
1072
1073 pathlen = STRLEN(pathbuf);
1074 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1075 PYTHONX_STRING_LENGTH + 1);
1076
1077 if (!(pathObject1 = PyString_FromString(pathbuf)))
1078 {
1079 *data = NULL;
1080 PyMem_Free(pathbuf);
1081 return;
1082 }
1083
1084 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1085 PYTHONX_STRING_LENGTH + 1);
1086
1087 if (!(pathObject2 = PyString_FromString(pathbuf)))
1088 {
1089 Py_DECREF(pathObject1);
1090 PyMem_Free(pathbuf);
1091 *data = NULL;
1092 return;
1093 }
1094
1095 PyMem_Free(pathbuf);
1096
1097 if (PyList_Append(list, pathObject1)
1098 || PyList_Append(list, pathObject2))
1099 *data = NULL;
1100
1101 Py_DECREF(pathObject1);
1102 Py_DECREF(pathObject2);
1103}
1104
1105 static PyObject *
1106Vim_GetPaths(PyObject *self UNUSED)
1107{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001108 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001109
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001110 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001111 return NULL;
1112
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001113 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001114
1115 if (PyErr_Occurred())
1116 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001117 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001118 return NULL;
1119 }
1120
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001121 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001122}
1123
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001124 static PyObject *
1125call_load_module(char *name, int len, PyObject *find_module_result)
1126{
1127 PyObject *fd, *pathname, *description;
1128
Bram Moolenaarc476e522013-06-23 13:46:40 +02001129 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001130 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001131 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001132 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001133 Py_TYPE_NAME(find_module_result));
1134 return NULL;
1135 }
1136 if (PyTuple_GET_SIZE(find_module_result) != 3)
1137 {
1138 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001139 N_("expected 3-tuple as imp.find_module() result, but got "
1140 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001141 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001142 return NULL;
1143 }
1144
1145 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1146 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1147 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1148 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001149 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001150 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001151 return NULL;
1152 }
1153
1154 return PyObject_CallFunction(py_load_module,
1155 "s#OOO", name, len, fd, pathname, description);
1156}
1157
1158 static PyObject *
1159find_module(char *fullname, char *tail, PyObject *new_path)
1160{
1161 PyObject *find_module_result;
1162 PyObject *module;
1163 char *dot;
1164
Bram Moolenaar41009372013-07-01 22:03:04 +02001165 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001166 {
1167 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001168 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001169 * first component
1170 */
1171 PyObject *newest_path;
1172 int partlen = (int) (dot - 1 - tail);
1173
1174 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1175 "s#O", tail, partlen, new_path)))
1176 return NULL;
1177
1178 if (!(module = call_load_module(
1179 fullname,
1180 ((int) (tail - fullname)) + partlen,
1181 find_module_result)))
1182 {
1183 Py_DECREF(find_module_result);
1184 return NULL;
1185 }
1186
1187 Py_DECREF(find_module_result);
1188
1189 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1190 {
1191 Py_DECREF(module);
1192 return NULL;
1193 }
1194
1195 Py_DECREF(module);
1196
1197 module = find_module(fullname, dot + 1, newest_path);
1198
1199 Py_DECREF(newest_path);
1200
1201 return module;
1202 }
1203 else
1204 {
1205 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1206 "sO", tail, new_path)))
1207 return NULL;
1208
1209 if (!(module = call_load_module(
1210 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001211 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001212 find_module_result)))
1213 {
1214 Py_DECREF(find_module_result);
1215 return NULL;
1216 }
1217
1218 Py_DECREF(find_module_result);
1219
1220 return module;
1221 }
1222}
1223
1224 static PyObject *
1225FinderFindModule(PyObject *self, PyObject *args)
1226{
1227 char *fullname;
1228 PyObject *module;
1229 PyObject *new_path;
1230 LoaderObject *loader;
1231
1232 if (!PyArg_ParseTuple(args, "s", &fullname))
1233 return NULL;
1234
1235 if (!(new_path = Vim_GetPaths(self)))
1236 return NULL;
1237
1238 module = find_module(fullname, fullname, new_path);
1239
1240 Py_DECREF(new_path);
1241
1242 if (!module)
1243 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001244 if (PyErr_Occurred())
1245 {
1246 if (PyErr_ExceptionMatches(PyExc_ImportError))
1247 PyErr_Clear();
1248 else
1249 return NULL;
1250 }
1251
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001252 Py_INCREF(Py_None);
1253 return Py_None;
1254 }
1255
1256 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1257 {
1258 Py_DECREF(module);
1259 return NULL;
1260 }
1261
1262 loader->module = module;
1263
1264 return (PyObject *) loader;
1265}
1266
1267 static PyObject *
1268VimPathHook(PyObject *self UNUSED, PyObject *args)
1269{
1270 char *path;
1271
1272 if (PyArg_ParseTuple(args, "s", &path)
1273 && STRCMP(path, vim_special_path) == 0)
1274 {
1275 Py_INCREF(vim_module);
1276 return vim_module;
1277 }
1278
1279 PyErr_Clear();
1280 PyErr_SetNone(PyExc_ImportError);
1281 return NULL;
1282}
1283
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001284/*
1285 * Vim module - Definitions
1286 */
1287
1288static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001289 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001290 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001291 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001292 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1293 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001294 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1295 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001296 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001297 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001298 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1299 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1300 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001301};
1302
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001303/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001304 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001305 */
1306
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001307static PyTypeObject IterType;
1308
1309typedef PyObject *(*nextfun)(void **);
1310typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001311typedef int (*traversefun)(void *, visitproc, void *);
1312typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001313
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001314/* Main purpose of this object is removing the need for do python
1315 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1316 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001317
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001318typedef struct
1319{
1320 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001321 void *cur;
1322 nextfun next;
1323 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001324 traversefun traverse;
1325 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001326} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001327
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001328 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001329IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1330 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001331{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001332 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001333
Bram Moolenaar774267b2013-05-21 20:51:59 +02001334 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001335 self->cur = start;
1336 self->next = next;
1337 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001338 self->traverse = traverse;
1339 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001340
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001341 return (PyObject *)(self);
1342}
1343
1344 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001345IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001346{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001347 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001348 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001349 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001350}
1351
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001352 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001353IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001354{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001355 if (self->traverse != NULL)
1356 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001357 else
1358 return 0;
1359}
1360
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001361/* Mac OSX defines clear() somewhere. */
1362#ifdef clear
1363# undef clear
1364#endif
1365
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001366 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001367IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001368{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001369 if (self->clear != NULL)
1370 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001371 else
1372 return 0;
1373}
1374
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001375 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001376IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001377{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001378 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001379}
1380
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001381 static PyObject *
1382IterIter(PyObject *self)
1383{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001384 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001385 return self;
1386}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001387
Bram Moolenaardb913952012-06-29 12:54:53 +02001388typedef struct pylinkedlist_S {
1389 struct pylinkedlist_S *pll_next;
1390 struct pylinkedlist_S *pll_prev;
1391 PyObject *pll_obj;
1392} pylinkedlist_T;
1393
1394static pylinkedlist_T *lastdict = NULL;
1395static pylinkedlist_T *lastlist = NULL;
1396
1397 static void
1398pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1399{
1400 if (ref->pll_prev == NULL)
1401 {
1402 if (ref->pll_next == NULL)
1403 {
1404 *last = NULL;
1405 return;
1406 }
1407 }
1408 else
1409 ref->pll_prev->pll_next = ref->pll_next;
1410
1411 if (ref->pll_next == NULL)
1412 *last = ref->pll_prev;
1413 else
1414 ref->pll_next->pll_prev = ref->pll_prev;
1415}
1416
1417 static void
1418pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1419{
1420 if (*last == NULL)
1421 ref->pll_prev = NULL;
1422 else
1423 {
1424 (*last)->pll_next = ref;
1425 ref->pll_prev = *last;
1426 }
1427 ref->pll_next = NULL;
1428 ref->pll_obj = self;
1429 *last = ref;
1430}
1431
1432static PyTypeObject DictionaryType;
1433
1434typedef struct
1435{
1436 PyObject_HEAD
1437 dict_T *dict;
1438 pylinkedlist_T ref;
1439} DictionaryObject;
1440
Bram Moolenaara9922d62013-05-30 13:01:18 +02001441static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1442
1443#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1444
Bram Moolenaardb913952012-06-29 12:54:53 +02001445 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001446DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001447{
1448 DictionaryObject *self;
1449
Bram Moolenaara9922d62013-05-30 13:01:18 +02001450 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001451 if (self == NULL)
1452 return NULL;
1453 self->dict = dict;
1454 ++dict->dv_refcount;
1455
1456 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1457
1458 return (PyObject *)(self);
1459}
1460
Bram Moolenaara9922d62013-05-30 13:01:18 +02001461 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001462py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001463{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001464 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001465
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001466 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001467 {
1468 PyErr_NoMemory();
1469 return NULL;
1470 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001471 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001472
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001473 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001474}
1475
1476 static PyObject *
1477DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1478{
1479 DictionaryObject *self;
1480 dict_T *dict;
1481
1482 if (!(dict = py_dict_alloc()))
1483 return NULL;
1484
1485 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1486
1487 --dict->dv_refcount;
1488
1489 if (kwargs || PyTuple_Size(args))
1490 {
1491 PyObject *tmp;
1492 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1493 {
1494 Py_DECREF(self);
1495 return NULL;
1496 }
1497
1498 Py_DECREF(tmp);
1499 }
1500
1501 return (PyObject *)(self);
1502}
1503
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001504 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001505DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001506{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001507 pyll_remove(&self->ref, &lastdict);
1508 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001509
1510 DESTRUCTOR_FINISH(self);
1511}
1512
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001513static char *DictionaryAttrs[] = {
1514 "locked", "scope",
1515 NULL
1516};
1517
1518 static PyObject *
1519DictionaryDir(PyObject *self)
1520{
1521 return ObjectDir(self, DictionaryAttrs);
1522}
1523
Bram Moolenaardb913952012-06-29 12:54:53 +02001524 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001525DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001526{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001527 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001528 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001529 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001530 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001531 return -1;
1532 }
1533
1534 if (strcmp(name, "locked") == 0)
1535 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001536 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001537 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001538 PyErr_SET_STRING(PyExc_TypeError,
1539 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001540 return -1;
1541 }
1542 else
1543 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001544 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001545 if (istrue == -1)
1546 return -1;
1547 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001548 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001549 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001550 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001551 }
1552 return 0;
1553 }
1554 else
1555 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001556 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001557 return -1;
1558 }
1559}
1560
1561 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001562DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001563{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001564 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001565}
1566
Bram Moolenaara9922d62013-05-30 13:01:18 +02001567#define DICT_FLAG_HAS_DEFAULT 0x01
1568#define DICT_FLAG_POP 0x02
1569#define DICT_FLAG_NONE_DEFAULT 0x04
1570#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1571#define DICT_FLAG_RETURN_PAIR 0x10
1572
Bram Moolenaardb913952012-06-29 12:54:53 +02001573 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001574_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001575{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001576 PyObject *keyObject;
1577 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001578 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001579 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001580 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001581 dict_T *dict = self->dict;
1582 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001583 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001584
Bram Moolenaara9922d62013-05-30 13:01:18 +02001585 if (flags & DICT_FLAG_HAS_DEFAULT)
1586 {
1587 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1588 return NULL;
1589 }
1590 else
1591 keyObject = args;
1592
1593 if (flags & DICT_FLAG_RETURN_BOOL)
1594 defObject = Py_False;
1595
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001596 if (!(key = StringToChars(keyObject, &todecref)))
1597 return NULL;
1598
1599 if (*key == NUL)
1600 {
1601 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001602 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001603 return NULL;
1604 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001605
Bram Moolenaara9922d62013-05-30 13:01:18 +02001606 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001607
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001608 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001609
Bram Moolenaara9922d62013-05-30 13:01:18 +02001610 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001611 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001612 if (defObject)
1613 {
1614 Py_INCREF(defObject);
1615 return defObject;
1616 }
1617 else
1618 {
1619 PyErr_SetObject(PyExc_KeyError, keyObject);
1620 return NULL;
1621 }
1622 }
1623 else if (flags & DICT_FLAG_RETURN_BOOL)
1624 {
1625 Py_INCREF(Py_True);
1626 return Py_True;
1627 }
1628
1629 di = dict_lookup(hi);
1630
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001631 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001632 return NULL;
1633
1634 if (flags & DICT_FLAG_POP)
1635 {
1636 if (dict->dv_lock)
1637 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001638 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001639 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001640 return NULL;
1641 }
1642
1643 hash_remove(&dict->dv_hashtab, hi);
1644 dictitem_free(di);
1645 }
1646
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001647 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001648}
1649
1650 static PyObject *
1651DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1652{
1653 return _DictionaryItem(self, keyObject, 0);
1654}
1655
1656 static int
1657DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1658{
1659 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001660 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001661
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001662 if (rObj == NULL)
1663 return -1;
1664
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001665 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001666
Bram Moolenaardee2e312013-06-23 16:35:47 +02001667 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001668
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001669 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001670}
1671
1672typedef struct
1673{
1674 hashitem_T *ht_array;
1675 long_u ht_used;
1676 hashtab_T *ht;
1677 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001678 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001679} dictiterinfo_T;
1680
1681 static PyObject *
1682DictionaryIterNext(dictiterinfo_T **dii)
1683{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001684 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001685
1686 if (!(*dii)->todo)
1687 return NULL;
1688
1689 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1690 (*dii)->ht->ht_used != (*dii)->ht_used)
1691 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001692 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001693 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001694 return NULL;
1695 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001696
Bram Moolenaara9922d62013-05-30 13:01:18 +02001697 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1698 ++((*dii)->hi);
1699
1700 --((*dii)->todo);
1701
Bram Moolenaar41009372013-07-01 22:03:04 +02001702 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001703 return NULL;
1704
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001705 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001706}
1707
1708 static PyObject *
1709DictionaryIter(DictionaryObject *self)
1710{
1711 dictiterinfo_T *dii;
1712 hashtab_T *ht;
1713
1714 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1715 {
1716 PyErr_NoMemory();
1717 return NULL;
1718 }
1719
1720 ht = &self->dict->dv_hashtab;
1721 dii->ht_array = ht->ht_array;
1722 dii->ht_used = ht->ht_used;
1723 dii->ht = ht;
1724 dii->hi = dii->ht_array;
1725 dii->todo = dii->ht_used;
1726
1727 return IterNew(dii,
1728 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1729 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001730}
1731
1732 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001733DictionaryAssItem(
1734 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001735{
1736 char_u *key;
1737 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001738 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001739 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001740 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001741
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001742 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001743 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001744 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001745 return -1;
1746 }
1747
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001748 if (!(key = StringToChars(keyObject, &todecref)))
1749 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001750
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001751 if (*key == NUL)
1752 {
1753 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001754 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001755 return -1;
1756 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001757
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001758 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001759
1760 if (valObject == NULL)
1761 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001762 hashitem_T *hi;
1763
Bram Moolenaardb913952012-06-29 12:54:53 +02001764 if (di == NULL)
1765 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001766 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001767 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001768 return -1;
1769 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001770 hi = hash_find(&dict->dv_hashtab, di->di_key);
1771 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001772 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001773 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001774 return 0;
1775 }
1776
1777 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001778 {
1779 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001780 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001781 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001782
1783 if (di == NULL)
1784 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001785 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001786 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001787 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001788 PyErr_NoMemory();
1789 return -1;
1790 }
1791 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001792 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001793
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001794 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001795 {
1796 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001797 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001798 RAISE_KEY_ADD_FAIL(key);
1799 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001800 return -1;
1801 }
1802 }
1803 else
1804 clear_tv(&di->di_tv);
1805
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001806 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001807
1808 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001809 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001810 return 0;
1811}
1812
Bram Moolenaara9922d62013-05-30 13:01:18 +02001813typedef PyObject *(*hi_to_py)(hashitem_T *);
1814
Bram Moolenaardb913952012-06-29 12:54:53 +02001815 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001816DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001817{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001818 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001819 long_u todo = dict->dv_hashtab.ht_used;
1820 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001821 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001822 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001823 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001824
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001825 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001826 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1827 {
1828 if (!HASHITEM_EMPTY(hi))
1829 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001830 if (!(newObj = hiconvert(hi)))
1831 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001832 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001833 return NULL;
1834 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001835 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001836 --todo;
1837 ++i;
1838 }
1839 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001840 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001841}
1842
Bram Moolenaara9922d62013-05-30 13:01:18 +02001843 static PyObject *
1844dict_key(hashitem_T *hi)
1845{
1846 return PyBytes_FromString((char *)(hi->hi_key));
1847}
1848
1849 static PyObject *
1850DictionaryListKeys(DictionaryObject *self)
1851{
1852 return DictionaryListObjects(self, dict_key);
1853}
1854
1855 static PyObject *
1856dict_val(hashitem_T *hi)
1857{
1858 dictitem_T *di;
1859
1860 di = dict_lookup(hi);
1861 return ConvertToPyObject(&di->di_tv);
1862}
1863
1864 static PyObject *
1865DictionaryListValues(DictionaryObject *self)
1866{
1867 return DictionaryListObjects(self, dict_val);
1868}
1869
1870 static PyObject *
1871dict_item(hashitem_T *hi)
1872{
1873 PyObject *keyObject;
1874 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001875 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001876
1877 if (!(keyObject = dict_key(hi)))
1878 return NULL;
1879
1880 if (!(valObject = dict_val(hi)))
1881 {
1882 Py_DECREF(keyObject);
1883 return NULL;
1884 }
1885
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001886 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001887
1888 Py_DECREF(keyObject);
1889 Py_DECREF(valObject);
1890
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001891 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001892}
1893
1894 static PyObject *
1895DictionaryListItems(DictionaryObject *self)
1896{
1897 return DictionaryListObjects(self, dict_item);
1898}
1899
1900 static PyObject *
1901DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1902{
1903 dict_T *dict = self->dict;
1904
1905 if (dict->dv_lock)
1906 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001907 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001908 return NULL;
1909 }
1910
1911 if (kwargs)
1912 {
1913 typval_T tv;
1914
1915 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1916 return NULL;
1917
1918 VimTryStart();
1919 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1920 clear_tv(&tv);
1921 if (VimTryEnd())
1922 return NULL;
1923 }
1924 else
1925 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001926 PyObject *obj;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001927
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001928 if (!PyArg_ParseTuple(args, "O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001929 return NULL;
1930
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001931 if (PyObject_HasAttrString(obj, "keys"))
1932 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001933 else
1934 {
1935 PyObject *iterator;
1936 PyObject *item;
1937
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001938 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001939 return NULL;
1940
1941 while ((item = PyIter_Next(iterator)))
1942 {
1943 PyObject *fast;
1944 PyObject *keyObject;
1945 PyObject *valObject;
1946 PyObject *todecref;
1947 char_u *key;
1948 dictitem_T *di;
1949
1950 if (!(fast = PySequence_Fast(item, "")))
1951 {
1952 Py_DECREF(iterator);
1953 Py_DECREF(item);
1954 return NULL;
1955 }
1956
1957 Py_DECREF(item);
1958
1959 if (PySequence_Fast_GET_SIZE(fast) != 2)
1960 {
1961 Py_DECREF(iterator);
1962 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001963 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001964 N_("expected sequence element of size 2, "
1965 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001966 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001967 return NULL;
1968 }
1969
1970 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1971
1972 if (!(key = StringToChars(keyObject, &todecref)))
1973 {
1974 Py_DECREF(iterator);
1975 Py_DECREF(fast);
1976 return NULL;
1977 }
1978
1979 di = dictitem_alloc(key);
1980
1981 Py_XDECREF(todecref);
1982
1983 if (di == NULL)
1984 {
1985 Py_DECREF(fast);
1986 Py_DECREF(iterator);
1987 PyErr_NoMemory();
1988 return NULL;
1989 }
1990 di->di_tv.v_lock = 0;
1991 di->di_tv.v_type = VAR_UNKNOWN;
1992
1993 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1994
1995 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1996 {
1997 Py_DECREF(iterator);
1998 Py_DECREF(fast);
1999 dictitem_free(di);
2000 return NULL;
2001 }
2002
2003 Py_DECREF(fast);
2004
2005 if (dict_add(dict, di) == FAIL)
2006 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002007 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002008 Py_DECREF(iterator);
2009 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002010 return NULL;
2011 }
2012 }
2013
2014 Py_DECREF(iterator);
2015
2016 /* Iterator may have finished due to an exception */
2017 if (PyErr_Occurred())
2018 return NULL;
2019 }
2020 }
2021 Py_INCREF(Py_None);
2022 return Py_None;
2023}
2024
2025 static PyObject *
2026DictionaryGet(DictionaryObject *self, PyObject *args)
2027{
2028 return _DictionaryItem(self, args,
2029 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2030}
2031
2032 static PyObject *
2033DictionaryPop(DictionaryObject *self, PyObject *args)
2034{
2035 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2036}
2037
2038 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002039DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002040{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002041 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002042 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002043 PyObject *valObject;
2044 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002045
Bram Moolenaarde71b562013-06-02 17:41:54 +02002046 if (self->dict->dv_hashtab.ht_used == 0)
2047 {
2048 PyErr_SetNone(PyExc_KeyError);
2049 return NULL;
2050 }
2051
2052 hi = self->dict->dv_hashtab.ht_array;
2053 while (HASHITEM_EMPTY(hi))
2054 ++hi;
2055
2056 di = dict_lookup(hi);
2057
2058 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002059 return NULL;
2060
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002061 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002062 {
2063 Py_DECREF(valObject);
2064 return NULL;
2065 }
2066
2067 hash_remove(&self->dict->dv_hashtab, hi);
2068 dictitem_free(di);
2069
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002070 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002071}
2072
2073 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002074DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002075{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002076 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2077}
2078
2079static PySequenceMethods DictionaryAsSeq = {
2080 0, /* sq_length */
2081 0, /* sq_concat */
2082 0, /* sq_repeat */
2083 0, /* sq_item */
2084 0, /* sq_slice */
2085 0, /* sq_ass_item */
2086 0, /* sq_ass_slice */
2087 (objobjproc) DictionaryContains, /* sq_contains */
2088 0, /* sq_inplace_concat */
2089 0, /* sq_inplace_repeat */
2090};
2091
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002092static PyMappingMethods DictionaryAsMapping = {
2093 (lenfunc) DictionaryLength,
2094 (binaryfunc) DictionaryItem,
2095 (objobjargproc) DictionaryAssItem,
2096};
2097
Bram Moolenaardb913952012-06-29 12:54:53 +02002098static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002099 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002100 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2101 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2102 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2103 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2104 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002105 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002106 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002107 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2108 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002109};
2110
2111static PyTypeObject ListType;
2112
2113typedef struct
2114{
2115 PyObject_HEAD
2116 list_T *list;
2117 pylinkedlist_T ref;
2118} ListObject;
2119
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002120#define NEW_LIST(list) ListNew(&ListType, list)
2121
Bram Moolenaardb913952012-06-29 12:54:53 +02002122 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002123ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002124{
2125 ListObject *self;
2126
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002127 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002128 if (self == NULL)
2129 return NULL;
2130 self->list = list;
2131 ++list->lv_refcount;
2132
2133 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2134
2135 return (PyObject *)(self);
2136}
2137
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002138 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002139py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002140{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002141 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002142
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002143 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002144 {
2145 PyErr_NoMemory();
2146 return NULL;
2147 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002148 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002149
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002150 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002151}
2152
2153 static int
2154list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2155{
2156 PyObject *iterator;
2157 PyObject *item;
2158 listitem_T *li;
2159
2160 if (!(iterator = PyObject_GetIter(obj)))
2161 return -1;
2162
2163 while ((item = PyIter_Next(iterator)))
2164 {
2165 if (!(li = listitem_alloc()))
2166 {
2167 PyErr_NoMemory();
2168 Py_DECREF(item);
2169 Py_DECREF(iterator);
2170 return -1;
2171 }
2172 li->li_tv.v_lock = 0;
2173 li->li_tv.v_type = VAR_UNKNOWN;
2174
2175 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2176 {
2177 Py_DECREF(item);
2178 Py_DECREF(iterator);
2179 listitem_free(li);
2180 return -1;
2181 }
2182
2183 Py_DECREF(item);
2184
2185 list_append(l, li);
2186 }
2187
2188 Py_DECREF(iterator);
2189
2190 /* Iterator may have finished due to an exception */
2191 if (PyErr_Occurred())
2192 return -1;
2193
2194 return 0;
2195}
2196
2197 static PyObject *
2198ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2199{
2200 list_T *list;
2201 PyObject *obj = NULL;
2202
2203 if (kwargs)
2204 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002205 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002206 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002207 return NULL;
2208 }
2209
2210 if (!PyArg_ParseTuple(args, "|O", &obj))
2211 return NULL;
2212
2213 if (!(list = py_list_alloc()))
2214 return NULL;
2215
2216 if (obj)
2217 {
2218 PyObject *lookup_dict;
2219
2220 if (!(lookup_dict = PyDict_New()))
2221 {
2222 list_unref(list);
2223 return NULL;
2224 }
2225
2226 if (list_py_concat(list, obj, lookup_dict) == -1)
2227 {
2228 Py_DECREF(lookup_dict);
2229 list_unref(list);
2230 return NULL;
2231 }
2232
2233 Py_DECREF(lookup_dict);
2234 }
2235
2236 return ListNew(subtype, list);
2237}
2238
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002239 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002240ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002241{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002242 pyll_remove(&self->ref, &lastlist);
2243 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002244
2245 DESTRUCTOR_FINISH(self);
2246}
2247
Bram Moolenaardb913952012-06-29 12:54:53 +02002248 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002249ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002250{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002251 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002252}
2253
2254 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002255ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002256{
2257 listitem_T *li;
2258
Bram Moolenaard6e39182013-05-21 18:30:34 +02002259 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002260 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002261 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002262 return NULL;
2263 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002264 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002265 if (li == NULL)
2266 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002267 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002268 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002269 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002270 return NULL;
2271 }
2272 return ConvertToPyObject(&li->li_tv);
2273}
2274
Bram Moolenaardb913952012-06-29 12:54:53 +02002275 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002276ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2277 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002278{
2279 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002280 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002281
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002282 if (step == 0)
2283 {
2284 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2285 return NULL;
2286 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002287
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002288 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002289 if (list == NULL)
2290 return NULL;
2291
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002292 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002293 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002294 PyObject *item;
2295
2296 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002297 if (item == NULL)
2298 {
2299 Py_DECREF(list);
2300 return NULL;
2301 }
2302
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002303 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002304 }
2305
2306 return list;
2307}
2308
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002309 static PyObject *
2310ListItem(ListObject *self, PyObject* idx)
2311{
2312#if PY_MAJOR_VERSION < 3
2313 if (PyInt_Check(idx))
2314 {
2315 long _idx = PyInt_AsLong(idx);
2316 return ListIndex(self, _idx);
2317 }
2318 else
2319#endif
2320 if (PyLong_Check(idx))
2321 {
2322 long _idx = PyLong_AsLong(idx);
2323 return ListIndex(self, _idx);
2324 }
2325 else if (PySlice_Check(idx))
2326 {
2327 Py_ssize_t start, stop, step, slicelen;
2328
2329 if (PySlice_GetIndicesEx(idx, ListLength(self),
2330 &start, &stop, &step, &slicelen) < 0)
2331 return NULL;
2332 return ListSlice(self, start, step, slicelen);
2333 }
2334 else
2335 {
2336 RAISE_INVALID_INDEX_TYPE(idx);
2337 return NULL;
2338 }
2339}
2340
2341 static void
2342list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2343 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2344{
2345 while (numreplaced--)
2346 {
2347 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2348 listitem_remove(l, lis[slicelen + numreplaced]);
2349 }
2350 while (numadded--)
2351 {
2352 listitem_T *next;
2353
2354 next = lastaddedli->li_prev;
2355 listitem_remove(l, lastaddedli);
2356 lastaddedli = next;
2357 }
2358}
2359
2360 static int
2361ListAssSlice(ListObject *self, Py_ssize_t first,
2362 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2363{
2364 PyObject *iterator;
2365 PyObject *item;
2366 listitem_T *li;
2367 listitem_T *lastaddedli = NULL;
2368 listitem_T *next;
2369 typval_T v;
2370 list_T *l = self->list;
2371 PyInt i;
2372 PyInt j;
2373 PyInt numreplaced = 0;
2374 PyInt numadded = 0;
2375 PyInt size;
2376 listitem_T **lis;
2377
2378 size = ListLength(self);
2379
2380 if (l->lv_lock)
2381 {
2382 RAISE_LOCKED_LIST;
2383 return -1;
2384 }
2385
2386 if (step == 0)
2387 {
2388 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2389 return -1;
2390 }
2391
2392 if (step != 1 && slicelen == 0)
2393 {
2394 /* Nothing to do. Only error out if obj has some items. */
2395 int ret = 0;
2396
2397 if (obj == NULL)
2398 return 0;
2399
2400 if (!(iterator = PyObject_GetIter(obj)))
2401 return -1;
2402
2403 if ((item = PyIter_Next(iterator)))
2404 {
2405 PyErr_FORMAT(PyExc_ValueError,
2406 N_("attempt to assign sequence of size greater then %d "
2407 "to extended slice"), 0);
2408 Py_DECREF(item);
2409 ret = -1;
2410 }
2411 Py_DECREF(iterator);
2412 return ret;
2413 }
2414
2415 if (obj != NULL)
2416 /* XXX May allocate zero bytes. */
2417 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2418 {
2419 PyErr_NoMemory();
2420 return -1;
2421 }
2422
2423 if (first == size)
2424 li = NULL;
2425 else
2426 {
2427 li = list_find(l, (long) first);
2428 if (li == NULL)
2429 {
2430 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2431 (int)first);
2432 if (obj != NULL)
2433 PyMem_Free(lis);
2434 return -1;
2435 }
2436 i = slicelen;
2437 while (i-- && li != NULL)
2438 {
2439 j = step;
2440 next = li;
2441 if (step > 0)
2442 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2443 else
2444 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2445
2446 if (obj == NULL)
2447 listitem_remove(l, li);
2448 else
2449 lis[slicelen - i - 1] = li;
2450
2451 li = next;
2452 }
2453 if (li == NULL && i != -1)
2454 {
2455 PyErr_SET_VIM(N_("internal error: not enough list items"));
2456 if (obj != NULL)
2457 PyMem_Free(lis);
2458 return -1;
2459 }
2460 }
2461
2462 if (obj == NULL)
2463 return 0;
2464
2465 if (!(iterator = PyObject_GetIter(obj)))
2466 {
2467 PyMem_Free(lis);
2468 return -1;
2469 }
2470
2471 i = 0;
2472 while ((item = PyIter_Next(iterator)))
2473 {
2474 if (ConvertFromPyObject(item, &v) == -1)
2475 {
2476 Py_DECREF(iterator);
2477 Py_DECREF(item);
2478 PyMem_Free(lis);
2479 return -1;
2480 }
2481 Py_DECREF(item);
2482 if (list_insert_tv(l, &v, numreplaced < slicelen
2483 ? lis[numreplaced]
2484 : li) == FAIL)
2485 {
2486 clear_tv(&v);
2487 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2488 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2489 PyMem_Free(lis);
2490 return -1;
2491 }
2492 if (numreplaced < slicelen)
2493 {
2494 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
2495 list_remove(l, lis[numreplaced], lis[numreplaced]);
2496 numreplaced++;
2497 }
2498 else
2499 {
2500 if (li)
2501 lastaddedli = li->li_prev;
2502 else
2503 lastaddedli = l->lv_last;
2504 numadded++;
2505 }
2506 clear_tv(&v);
2507 if (step != 1 && i >= slicelen)
2508 {
2509 Py_DECREF(iterator);
2510 PyErr_FORMAT(PyExc_ValueError,
2511 N_("attempt to assign sequence of size greater then %d "
2512 "to extended slice"), slicelen);
2513 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2514 PyMem_Free(lis);
2515 return -1;
2516 }
2517 ++i;
2518 }
2519 Py_DECREF(iterator);
2520
2521 if (step != 1 && i != slicelen)
2522 {
2523 PyErr_FORMAT2(PyExc_ValueError,
2524 N_("attempt to assign sequence of size %d to extended slice "
2525 "of size %d"), i, slicelen);
2526 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2527 PyMem_Free(lis);
2528 return -1;
2529 }
2530
2531 if (PyErr_Occurred())
2532 {
2533 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2534 PyMem_Free(lis);
2535 return -1;
2536 }
2537
2538 for (i = 0; i < numreplaced; i++)
2539 listitem_free(lis[i]);
2540 if (step == 1)
2541 for (i = numreplaced; i < slicelen; i++)
2542 listitem_remove(l, lis[i]);
2543
2544 PyMem_Free(lis);
2545
2546 return 0;
2547}
2548
2549 static int
2550ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2551{
2552 typval_T tv;
2553 list_T *l = self->list;
2554 listitem_T *li;
2555 Py_ssize_t length = ListLength(self);
2556
2557 if (l->lv_lock)
2558 {
2559 RAISE_LOCKED_LIST;
2560 return -1;
2561 }
2562 if (index > length || (index == length && obj == NULL))
2563 {
2564 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2565 return -1;
2566 }
2567
2568 if (obj == NULL)
2569 {
2570 li = list_find(l, (long) index);
2571 list_remove(l, li, li);
2572 clear_tv(&li->li_tv);
2573 vim_free(li);
2574 return 0;
2575 }
2576
2577 if (ConvertFromPyObject(obj, &tv) == -1)
2578 return -1;
2579
2580 if (index == length)
2581 {
2582 if (list_append_tv(l, &tv) == FAIL)
2583 {
2584 clear_tv(&tv);
2585 PyErr_SET_VIM(N_("failed to add item to list"));
2586 return -1;
2587 }
2588 }
2589 else
2590 {
2591 li = list_find(l, (long) index);
2592 clear_tv(&li->li_tv);
2593 copy_tv(&tv, &li->li_tv);
2594 clear_tv(&tv);
2595 }
2596 return 0;
2597}
2598
2599 static Py_ssize_t
2600ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2601{
2602#if PY_MAJOR_VERSION < 3
2603 if (PyInt_Check(idx))
2604 {
2605 long _idx = PyInt_AsLong(idx);
2606 return ListAssIndex(self, _idx, obj);
2607 }
2608 else
2609#endif
2610 if (PyLong_Check(idx))
2611 {
2612 long _idx = PyLong_AsLong(idx);
2613 return ListAssIndex(self, _idx, obj);
2614 }
2615 else if (PySlice_Check(idx))
2616 {
2617 Py_ssize_t start, stop, step, slicelen;
2618
2619 if (PySlice_GetIndicesEx(idx, ListLength(self),
2620 &start, &stop, &step, &slicelen) < 0)
2621 return -1;
2622 return ListAssSlice(self, start, step, slicelen,
2623 obj);
2624 }
2625 else
2626 {
2627 RAISE_INVALID_INDEX_TYPE(idx);
2628 return -1;
2629 }
2630}
2631
2632 static PyObject *
2633ListConcatInPlace(ListObject *self, PyObject *obj)
2634{
2635 list_T *l = self->list;
2636 PyObject *lookup_dict;
2637
2638 if (l->lv_lock)
2639 {
2640 RAISE_LOCKED_LIST;
2641 return NULL;
2642 }
2643
2644 if (!(lookup_dict = PyDict_New()))
2645 return NULL;
2646
2647 if (list_py_concat(l, obj, lookup_dict) == -1)
2648 {
2649 Py_DECREF(lookup_dict);
2650 return NULL;
2651 }
2652 Py_DECREF(lookup_dict);
2653
2654 Py_INCREF(self);
2655 return (PyObject *)(self);
2656}
2657
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002658typedef struct
2659{
2660 listwatch_T lw;
2661 list_T *list;
2662} listiterinfo_T;
2663
2664 static void
2665ListIterDestruct(listiterinfo_T *lii)
2666{
2667 list_rem_watch(lii->list, &lii->lw);
2668 PyMem_Free(lii);
2669}
2670
2671 static PyObject *
2672ListIterNext(listiterinfo_T **lii)
2673{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002674 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002675
2676 if (!((*lii)->lw.lw_item))
2677 return NULL;
2678
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002679 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002680 return NULL;
2681
2682 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2683
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002684 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002685}
2686
2687 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002688ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002689{
2690 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002691 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002692
2693 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2694 {
2695 PyErr_NoMemory();
2696 return NULL;
2697 }
2698
2699 list_add_watch(l, &lii->lw);
2700 lii->lw.lw_item = l->lv_first;
2701 lii->list = l;
2702
2703 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002704 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2705 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002706}
2707
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002708static char *ListAttrs[] = {
2709 "locked",
2710 NULL
2711};
2712
2713 static PyObject *
2714ListDir(PyObject *self)
2715{
2716 return ObjectDir(self, ListAttrs);
2717}
2718
Bram Moolenaar66b79852012-09-21 14:00:35 +02002719 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002720ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002721{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002722 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002723 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002724 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002725 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002726 return -1;
2727 }
2728
2729 if (strcmp(name, "locked") == 0)
2730 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002731 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002732 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002733 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002734 return -1;
2735 }
2736 else
2737 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002738 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002739 if (istrue == -1)
2740 return -1;
2741 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002742 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002743 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002744 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002745 }
2746 return 0;
2747 }
2748 else
2749 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002750 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002751 return -1;
2752 }
2753}
2754
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002755static PySequenceMethods ListAsSeq = {
2756 (lenfunc) ListLength, /* sq_length, len(x) */
2757 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2758 0, /* RangeRepeat, sq_repeat, x*n */
2759 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2760 0, /* was_sq_slice, x[i:j] */
2761 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2762 0, /* was_sq_ass_slice, x[i:j]=v */
2763 0, /* sq_contains */
2764 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2765 0, /* sq_inplace_repeat */
2766};
2767
2768static PyMappingMethods ListAsMapping = {
2769 /* mp_length */ (lenfunc) ListLength,
2770 /* mp_subscript */ (binaryfunc) ListItem,
2771 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2772};
2773
Bram Moolenaardb913952012-06-29 12:54:53 +02002774static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002775 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2776 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2777 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002778};
2779
2780typedef struct
2781{
2782 PyObject_HEAD
2783 char_u *name;
2784} FunctionObject;
2785
2786static PyTypeObject FunctionType;
2787
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002788#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2789
Bram Moolenaardb913952012-06-29 12:54:53 +02002790 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002791FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002792{
2793 FunctionObject *self;
2794
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002795 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2796
Bram Moolenaardb913952012-06-29 12:54:53 +02002797 if (self == NULL)
2798 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002799
2800 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002801 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002802 if (!translated_function_exists(name))
2803 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002804 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002805 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002806 return NULL;
2807 }
2808 self->name = vim_strsave(name);
2809 func_ref(self->name);
2810 }
2811 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002812 if ((self->name = get_expanded_name(name,
2813 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2814 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002815 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002816 PyErr_FORMAT(PyExc_ValueError,
2817 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002818 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002819 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002820
2821 return (PyObject *)(self);
2822}
2823
2824 static PyObject *
2825FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2826{
2827 PyObject *self;
2828 char_u *name;
2829
2830 if (kwargs)
2831 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002832 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002833 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002834 return NULL;
2835 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002836
Bram Moolenaar389a1792013-06-23 13:00:44 +02002837 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002838 return NULL;
2839
2840 self = FunctionNew(subtype, name);
2841
Bram Moolenaar389a1792013-06-23 13:00:44 +02002842 PyMem_Free(name);
2843
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002844 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002845}
2846
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002847 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002848FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002849{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002850 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002851 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002852
2853 DESTRUCTOR_FINISH(self);
2854}
2855
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002856static char *FunctionAttrs[] = {
2857 "softspace",
2858 NULL
2859};
2860
2861 static PyObject *
2862FunctionDir(PyObject *self)
2863{
2864 return ObjectDir(self, FunctionAttrs);
2865}
2866
Bram Moolenaardb913952012-06-29 12:54:53 +02002867 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002868FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002869{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002870 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002871 typval_T args;
2872 typval_T selfdicttv;
2873 typval_T rettv;
2874 dict_T *selfdict = NULL;
2875 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002876 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002877 int error;
2878
2879 if (ConvertFromPyObject(argsObject, &args) == -1)
2880 return NULL;
2881
2882 if (kwargs != NULL)
2883 {
2884 selfdictObject = PyDict_GetItemString(kwargs, "self");
2885 if (selfdictObject != NULL)
2886 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002887 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002888 {
2889 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002890 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002891 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002892 selfdict = selfdicttv.vval.v_dict;
2893 }
2894 }
2895
Bram Moolenaar71700b82013-05-15 17:49:05 +02002896 Py_BEGIN_ALLOW_THREADS
2897 Python_Lock_Vim();
2898
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002899 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002900 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002901
2902 Python_Release_Vim();
2903 Py_END_ALLOW_THREADS
2904
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002905 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002906 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002907 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002908 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002909 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002910 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002911 }
2912 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002913 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002914
Bram Moolenaardb913952012-06-29 12:54:53 +02002915 clear_tv(&args);
2916 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002917 if (selfdict != NULL)
2918 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002919
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002920 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002921}
2922
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002923 static PyObject *
2924FunctionRepr(FunctionObject *self)
2925{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002926#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002927 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002928 * Finalize */
2929 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002930 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002931#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002932 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002933#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002934}
2935
Bram Moolenaardb913952012-06-29 12:54:53 +02002936static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002937 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2938 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002939};
2940
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002941/*
2942 * Options object
2943 */
2944
2945static PyTypeObject OptionsType;
2946
2947typedef int (*checkfun)(void *);
2948
2949typedef struct
2950{
2951 PyObject_HEAD
2952 int opt_type;
2953 void *from;
2954 checkfun Check;
2955 PyObject *fromObj;
2956} OptionsObject;
2957
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002958 static int
2959dummy_check(void *arg UNUSED)
2960{
2961 return 0;
2962}
2963
2964 static PyObject *
2965OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2966{
2967 OptionsObject *self;
2968
Bram Moolenaar774267b2013-05-21 20:51:59 +02002969 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002970 if (self == NULL)
2971 return NULL;
2972
2973 self->opt_type = opt_type;
2974 self->from = from;
2975 self->Check = Check;
2976 self->fromObj = fromObj;
2977 if (fromObj)
2978 Py_INCREF(fromObj);
2979
2980 return (PyObject *)(self);
2981}
2982
2983 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002984OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002985{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002986 PyObject_GC_UnTrack((void *)(self));
2987 Py_XDECREF(self->fromObj);
2988 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002989}
2990
2991 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002992OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002993{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002994 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002995 return 0;
2996}
2997
2998 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002999OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003000{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003001 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003002 return 0;
3003}
3004
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003005 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003006OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003007{
3008 char_u *key;
3009 int flags;
3010 long numval;
3011 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003012 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003013
Bram Moolenaard6e39182013-05-21 18:30:34 +02003014 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003015 return NULL;
3016
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003017 if (!(key = StringToChars(keyObject, &todecref)))
3018 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003019
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003020 if (*key == NUL)
3021 {
3022 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003023 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003024 return NULL;
3025 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003026
3027 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003028 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003029
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003030 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003031
3032 if (flags == 0)
3033 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003034 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003035 return NULL;
3036 }
3037
3038 if (flags & SOPT_UNSET)
3039 {
3040 Py_INCREF(Py_None);
3041 return Py_None;
3042 }
3043 else if (flags & SOPT_BOOL)
3044 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003045 PyObject *ret;
3046 ret = numval ? Py_True : Py_False;
3047 Py_INCREF(ret);
3048 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003049 }
3050 else if (flags & SOPT_NUM)
3051 return PyInt_FromLong(numval);
3052 else if (flags & SOPT_STRING)
3053 {
3054 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003055 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003056 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003057 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003058 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003059 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003060 else
3061 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003062 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003063 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003064 return NULL;
3065 }
3066 }
3067 else
3068 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003069 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003070 return NULL;
3071 }
3072}
3073
3074 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003075set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003076{
3077 char_u *errmsg;
3078
3079 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3080 {
3081 if (VimTryEnd())
3082 return FAIL;
3083 PyErr_SetVim((char *)errmsg);
3084 return FAIL;
3085 }
3086 return OK;
3087}
3088
3089 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003090set_option_value_for(
3091 char_u *key,
3092 int numval,
3093 char_u *stringval,
3094 int opt_flags,
3095 int opt_type,
3096 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003097{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003098 win_T *save_curwin = NULL;
3099 tabpage_T *save_curtab = NULL;
3100 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003101 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003102
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003103 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003104 switch (opt_type)
3105 {
3106 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003107 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003108 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003109 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003110 if (VimTryEnd())
3111 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003112 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003113 return -1;
3114 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003115 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3116 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003117 break;
3118 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003119 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003120 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003121 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003122 break;
3123 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003124 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003125 break;
3126 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003127 if (set_ret == FAIL)
3128 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003129 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003130}
3131
3132 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003133OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003134{
3135 char_u *key;
3136 int flags;
3137 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003138 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003139 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003140
Bram Moolenaard6e39182013-05-21 18:30:34 +02003141 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003142 return -1;
3143
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003144 if (!(key = StringToChars(keyObject, &todecref)))
3145 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003146
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003147 if (*key == NUL)
3148 {
3149 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003150 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003151 return -1;
3152 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003153
3154 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003155 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003156
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003157 if (flags == 0)
3158 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003159 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003160 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003161 return -1;
3162 }
3163
3164 if (valObject == NULL)
3165 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003166 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003167 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003168 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003169 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003170 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003171 return -1;
3172 }
3173 else if (!(flags & SOPT_GLOBAL))
3174 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003175 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003176 N_("unable to unset option %s "
3177 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003178 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003179 return -1;
3180 }
3181 else
3182 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003183 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003184 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003185 return 0;
3186 }
3187 }
3188
Bram Moolenaard6e39182013-05-21 18:30:34 +02003189 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003190
3191 if (flags & SOPT_BOOL)
3192 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003193 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003194
Bram Moolenaarb983f752013-05-15 16:11:50 +02003195 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003196 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003197 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003198 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003199 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003200 }
3201 else if (flags & SOPT_NUM)
3202 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003203 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003204
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003205 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003206 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003207 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003208 return -1;
3209 }
3210
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003211 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003212 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003213 }
3214 else
3215 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003216 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003217 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003218
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003219 if ((val = StringToChars(valObject, &todecref2)))
3220 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003221 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003222 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003223 Py_XDECREF(todecref2);
3224 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003225 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003226 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003227 }
3228
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003229 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003230
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003231 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003232}
3233
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003234static PyMappingMethods OptionsAsMapping = {
3235 (lenfunc) NULL,
3236 (binaryfunc) OptionsItem,
3237 (objobjargproc) OptionsAssItem,
3238};
3239
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003240/* Tabpage object
3241 */
3242
3243typedef struct
3244{
3245 PyObject_HEAD
3246 tabpage_T *tab;
3247} TabPageObject;
3248
3249static PyObject *WinListNew(TabPageObject *tabObject);
3250
3251static PyTypeObject TabPageType;
3252
3253 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003254CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003255{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003256 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003257 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003258 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003259 return -1;
3260 }
3261
3262 return 0;
3263}
3264
3265 static PyObject *
3266TabPageNew(tabpage_T *tab)
3267{
3268 TabPageObject *self;
3269
3270 if (TAB_PYTHON_REF(tab))
3271 {
3272 self = TAB_PYTHON_REF(tab);
3273 Py_INCREF(self);
3274 }
3275 else
3276 {
3277 self = PyObject_NEW(TabPageObject, &TabPageType);
3278 if (self == NULL)
3279 return NULL;
3280 self->tab = tab;
3281 TAB_PYTHON_REF(tab) = self;
3282 }
3283
3284 return (PyObject *)(self);
3285}
3286
3287 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003288TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003289{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003290 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3291 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003292
3293 DESTRUCTOR_FINISH(self);
3294}
3295
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003296static char *TabPageAttrs[] = {
3297 "windows", "number", "vars", "window", "valid",
3298 NULL
3299};
3300
3301 static PyObject *
3302TabPageDir(PyObject *self)
3303{
3304 return ObjectDir(self, TabPageAttrs);
3305}
3306
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003307 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003308TabPageAttrValid(TabPageObject *self, char *name)
3309{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003310 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003311
3312 if (strcmp(name, "valid") != 0)
3313 return NULL;
3314
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003315 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3316 Py_INCREF(ret);
3317 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003318}
3319
3320 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003321TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003322{
3323 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003324 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003325 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003326 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003327 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003328 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003329 else if (strcmp(name, "window") == 0)
3330 {
3331 /* For current tab window.c does not bother to set or update tp_curwin
3332 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003333 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003334 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003335 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003336 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003337 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003338 else if (strcmp(name, "__members__") == 0)
3339 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003340 return NULL;
3341}
3342
3343 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003344TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003345{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003346 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003347 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003348 else
3349 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003350 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003351
3352 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003353 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3354 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003355 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003356 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003357 }
3358}
3359
3360static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003361 /* name, function, calling, documentation */
3362 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3363 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003364};
3365
3366/*
3367 * Window list object
3368 */
3369
3370static PyTypeObject TabListType;
3371static PySequenceMethods TabListAsSeq;
3372
3373typedef struct
3374{
3375 PyObject_HEAD
3376} TabListObject;
3377
3378 static PyInt
3379TabListLength(PyObject *self UNUSED)
3380{
3381 tabpage_T *tp = first_tabpage;
3382 PyInt n = 0;
3383
3384 while (tp != NULL)
3385 {
3386 ++n;
3387 tp = tp->tp_next;
3388 }
3389
3390 return n;
3391}
3392
3393 static PyObject *
3394TabListItem(PyObject *self UNUSED, PyInt n)
3395{
3396 tabpage_T *tp;
3397
3398 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3399 if (n == 0)
3400 return TabPageNew(tp);
3401
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003402 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003403 return NULL;
3404}
3405
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003406/*
3407 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003408 */
3409
3410typedef struct
3411{
3412 PyObject_HEAD
3413 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003414 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003415} WindowObject;
3416
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003417static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003418
3419 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003420CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003421{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003422 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003423 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003424 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003425 return -1;
3426 }
3427
3428 return 0;
3429}
3430
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003431 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003432WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003433{
3434 /* We need to handle deletion of windows underneath us.
3435 * If we add a "w_python*_ref" field to the win_T structure,
3436 * then we can get at it in win_free() in vim. We then
3437 * need to create only ONE Python object per window - if
3438 * we try to create a second, just INCREF the existing one
3439 * and return it. The (single) Python object referring to
3440 * the window is stored in "w_python*_ref".
3441 * On a win_free() we set the Python object's win_T* field
3442 * to an invalid value. We trap all uses of a window
3443 * object, and reject them if the win_T* field is invalid.
3444 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003445 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003446 * w_python_ref and w_python3_ref fields respectively.
3447 */
3448
3449 WindowObject *self;
3450
3451 if (WIN_PYTHON_REF(win))
3452 {
3453 self = WIN_PYTHON_REF(win);
3454 Py_INCREF(self);
3455 }
3456 else
3457 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003458 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003459 if (self == NULL)
3460 return NULL;
3461 self->win = win;
3462 WIN_PYTHON_REF(win) = self;
3463 }
3464
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003465 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3466
Bram Moolenaar971db462013-05-12 18:44:48 +02003467 return (PyObject *)(self);
3468}
3469
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003470 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003471WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003472{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003473 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003474 if (self->win && self->win != INVALID_WINDOW_VALUE)
3475 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003476 Py_XDECREF(((PyObject *)(self->tabObject)));
3477 PyObject_GC_Del((void *)(self));
3478}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003479
Bram Moolenaar774267b2013-05-21 20:51:59 +02003480 static int
3481WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3482{
3483 Py_VISIT(((PyObject *)(self->tabObject)));
3484 return 0;
3485}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003486
Bram Moolenaar774267b2013-05-21 20:51:59 +02003487 static int
3488WindowClear(WindowObject *self)
3489{
3490 Py_CLEAR(self->tabObject);
3491 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003492}
3493
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003494 static win_T *
3495get_firstwin(TabPageObject *tabObject)
3496{
3497 if (tabObject)
3498 {
3499 if (CheckTabPage(tabObject))
3500 return NULL;
3501 /* For current tab window.c does not bother to set or update tp_firstwin
3502 */
3503 else if (tabObject->tab == curtab)
3504 return firstwin;
3505 else
3506 return tabObject->tab->tp_firstwin;
3507 }
3508 else
3509 return firstwin;
3510}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003511static char *WindowAttrs[] = {
3512 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3513 "tabpage", "valid",
3514 NULL
3515};
3516
3517 static PyObject *
3518WindowDir(PyObject *self)
3519{
3520 return ObjectDir(self, WindowAttrs);
3521}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003522
Bram Moolenaar971db462013-05-12 18:44:48 +02003523 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003524WindowAttrValid(WindowObject *self, char *name)
3525{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003526 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003527
3528 if (strcmp(name, "valid") != 0)
3529 return NULL;
3530
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003531 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3532 Py_INCREF(ret);
3533 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003534}
3535
3536 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003537WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003538{
3539 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003540 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003541 else if (strcmp(name, "cursor") == 0)
3542 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003543 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003544
3545 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3546 }
3547 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003548 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003549#ifdef FEAT_WINDOWS
3550 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003551 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003552#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003553#ifdef FEAT_VERTSPLIT
3554 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003555 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003556 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003557 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003558#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003559 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003560 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003561 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003562 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3563 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003564 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003565 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003566 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003567 return NULL;
3568 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003569 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003570 }
3571 else if (strcmp(name, "tabpage") == 0)
3572 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003573 Py_INCREF(self->tabObject);
3574 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003575 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003576 else if (strcmp(name, "__members__") == 0)
3577 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003578 else
3579 return NULL;
3580}
3581
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003582 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003583WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003584{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003585 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003586 return -1;
3587
3588 if (strcmp(name, "buffer") == 0)
3589 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003590 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003591 return -1;
3592 }
3593 else if (strcmp(name, "cursor") == 0)
3594 {
3595 long lnum;
3596 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003597
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003598 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003599 return -1;
3600
Bram Moolenaard6e39182013-05-21 18:30:34 +02003601 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003602 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003603 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003604 return -1;
3605 }
3606
3607 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003608 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003609 return -1;
3610
Bram Moolenaard6e39182013-05-21 18:30:34 +02003611 self->win->w_cursor.lnum = lnum;
3612 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003613#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003614 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003615#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003616 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003617 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003618
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003619 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003620 return 0;
3621 }
3622 else if (strcmp(name, "height") == 0)
3623 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003624 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003625 win_T *savewin;
3626
Bram Moolenaardee2e312013-06-23 16:35:47 +02003627 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003628 return -1;
3629
3630#ifdef FEAT_GUI
3631 need_mouse_correct = TRUE;
3632#endif
3633 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003634 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003635
3636 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003637 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003638 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003639 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003640 return -1;
3641
3642 return 0;
3643 }
3644#ifdef FEAT_VERTSPLIT
3645 else if (strcmp(name, "width") == 0)
3646 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003647 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003648 win_T *savewin;
3649
Bram Moolenaardee2e312013-06-23 16:35:47 +02003650 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003651 return -1;
3652
3653#ifdef FEAT_GUI
3654 need_mouse_correct = TRUE;
3655#endif
3656 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003657 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003658
3659 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003660 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003661 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003662 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003663 return -1;
3664
3665 return 0;
3666 }
3667#endif
3668 else
3669 {
3670 PyErr_SetString(PyExc_AttributeError, name);
3671 return -1;
3672 }
3673}
3674
3675 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003676WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003677{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003678 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003679 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003680 else
3681 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003682 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003683
Bram Moolenaar6d216452013-05-12 19:00:41 +02003684 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003685 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003686 (self));
3687 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003688 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003689 }
3690}
3691
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003692static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003693 /* name, function, calling, documentation */
3694 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3695 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003696};
3697
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003698/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003699 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003700 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003701
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003702static PyTypeObject WinListType;
3703static PySequenceMethods WinListAsSeq;
3704
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003705typedef struct
3706{
3707 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003708 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003709} WinListObject;
3710
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003711 static PyObject *
3712WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003713{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003714 WinListObject *self;
3715
3716 self = PyObject_NEW(WinListObject, &WinListType);
3717 self->tabObject = tabObject;
3718 Py_INCREF(tabObject);
3719
3720 return (PyObject *)(self);
3721}
3722
3723 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003724WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003725{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003726 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003727
3728 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003729 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003730 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003731 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003732
3733 DESTRUCTOR_FINISH(self);
3734}
3735
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003736 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003737WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003738{
3739 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003740 PyInt n = 0;
3741
Bram Moolenaard6e39182013-05-21 18:30:34 +02003742 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003743 return -1;
3744
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003745 while (w != NULL)
3746 {
3747 ++n;
3748 w = W_NEXT(w);
3749 }
3750
3751 return n;
3752}
3753
3754 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003755WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003756{
3757 win_T *w;
3758
Bram Moolenaard6e39182013-05-21 18:30:34 +02003759 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003760 return NULL;
3761
3762 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003763 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003764 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003765
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003766 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003767 return NULL;
3768}
3769
3770/* Convert a Python string into a Vim line.
3771 *
3772 * The result is in allocated memory. All internal nulls are replaced by
3773 * newline characters. It is an error for the string to contain newline
3774 * characters.
3775 *
3776 * On errors, the Python exception data is set, and NULL is returned.
3777 */
3778 static char *
3779StringToLine(PyObject *obj)
3780{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003781 char *str;
3782 char *save;
3783 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003784 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003785 PyInt i;
3786 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003787
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003788 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003789 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003790 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3791 || str == NULL)
3792 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003793 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003794 else if (PyUnicode_Check(obj))
3795 {
3796 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3797 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003798
Bram Moolenaardaa27022013-06-24 22:33:30 +02003799 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003800 || str == NULL)
3801 {
3802 Py_DECREF(bytes);
3803 return NULL;
3804 }
3805 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003806 else
3807 {
3808#if PY_MAJOR_VERSION < 3
3809 PyErr_FORMAT(PyExc_TypeError,
3810 N_("expected str() or unicode() instance, but got %s"),
3811 Py_TYPE_NAME(obj));
3812#else
3813 PyErr_FORMAT(PyExc_TypeError,
3814 N_("expected bytes() or str() instance, but got %s"),
3815 Py_TYPE_NAME(obj));
3816#endif
3817 return NULL;
3818 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003819
3820 /*
3821 * Error checking: String must not contain newlines, as we
3822 * are replacing a single line, and we must replace it with
3823 * a single line.
3824 * A trailing newline is removed, so that append(f.readlines()) works.
3825 */
3826 p = memchr(str, '\n', len);
3827 if (p != NULL)
3828 {
3829 if (p == str + len - 1)
3830 --len;
3831 else
3832 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003833 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003834 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003835 return NULL;
3836 }
3837 }
3838
3839 /* Create a copy of the string, with internal nulls replaced by
3840 * newline characters, as is the vim convention.
3841 */
3842 save = (char *)alloc((unsigned)(len+1));
3843 if (save == NULL)
3844 {
3845 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003846 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003847 return NULL;
3848 }
3849
3850 for (i = 0; i < len; ++i)
3851 {
3852 if (str[i] == '\0')
3853 save[i] = '\n';
3854 else
3855 save[i] = str[i];
3856 }
3857
3858 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003859 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003860
3861 return save;
3862}
3863
3864/* Get a line from the specified buffer. The line number is
3865 * in Vim format (1-based). The line is returned as a Python
3866 * string object.
3867 */
3868 static PyObject *
3869GetBufferLine(buf_T *buf, PyInt n)
3870{
3871 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3872}
3873
3874
3875/* Get a list of lines from the specified buffer. The line numbers
3876 * are in Vim format (1-based). The range is from lo up to, but not
3877 * including, hi. The list is returned as a Python list of string objects.
3878 */
3879 static PyObject *
3880GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3881{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003882 PyInt i;
3883 PyInt n = hi - lo;
3884 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003885
3886 if (list == NULL)
3887 return NULL;
3888
3889 for (i = 0; i < n; ++i)
3890 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003891 PyObject *string = LineToString(
3892 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003893
3894 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003895 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003896 {
3897 Py_DECREF(list);
3898 return NULL;
3899 }
3900
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003901 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003902 }
3903
3904 /* The ownership of the Python list is passed to the caller (ie,
3905 * the caller should Py_DECREF() the object when it is finished
3906 * with it).
3907 */
3908
3909 return list;
3910}
3911
3912/*
3913 * Check if deleting lines made the cursor position invalid.
3914 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3915 * deleted).
3916 */
3917 static void
3918py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3919{
3920 if (curwin->w_cursor.lnum >= lo)
3921 {
3922 /* Adjust the cursor position if it's in/after the changed
3923 * lines. */
3924 if (curwin->w_cursor.lnum >= hi)
3925 {
3926 curwin->w_cursor.lnum += extra;
3927 check_cursor_col();
3928 }
3929 else if (extra < 0)
3930 {
3931 curwin->w_cursor.lnum = lo;
3932 check_cursor();
3933 }
3934 else
3935 check_cursor_col();
3936 changed_cline_bef_curs();
3937 }
3938 invalidate_botline();
3939}
3940
Bram Moolenaar19e60942011-06-19 00:27:51 +02003941/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003942 * Find a window that contains "buf" and switch to it.
3943 * If there is no such window, use the current window and change "curbuf".
3944 * Caller must initialize save_curbuf to NULL.
3945 * restore_win_for_buf() MUST be called later!
3946 */
3947 static void
3948switch_to_win_for_buf(
3949 buf_T *buf,
3950 win_T **save_curwinp,
3951 tabpage_T **save_curtabp,
3952 buf_T **save_curbufp)
3953{
3954 win_T *wp;
3955 tabpage_T *tp;
3956
3957 if (find_win_for_buf(buf, &wp, &tp) == FAIL
3958 || switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
3959 switch_buffer(save_curbufp, buf);
3960}
3961
3962 static void
3963restore_win_for_buf(
3964 win_T *save_curwin,
3965 tabpage_T *save_curtab,
3966 buf_T *save_curbuf)
3967{
3968 if (save_curbuf == NULL)
3969 restore_win(save_curwin, save_curtab, TRUE);
3970 else
3971 restore_buffer(save_curbuf);
3972}
3973
3974/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02003975 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003976 * in Vim format (1-based). The replacement line is given as
3977 * a Python string object. The object is checked for validity
3978 * and correct format. Errors are returned as a value of FAIL.
3979 * The return value is OK on success.
3980 * If OK is returned and len_change is not NULL, *len_change
3981 * is set to the change in the buffer length.
3982 */
3983 static int
3984SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
3985{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003986 buf_T *save_curbuf = NULL;
3987 win_T *save_curwin = NULL;
3988 tabpage_T *save_curtab = NULL;
3989
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003990 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003991 * There are three cases:
3992 * 1. NULL, or None - this is a deletion.
3993 * 2. A string - this is a replacement.
3994 * 3. Anything else - this is an error.
3995 */
3996 if (line == Py_None || line == NULL)
3997 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003998 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02003999 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004000
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004001 VimTryStart();
4002
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004003 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004004 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004005 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004006 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004007 else
4008 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004009 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004010 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004011 if (save_curbuf == NULL)
4012 /* Only adjust marks if we managed to switch to a window that
4013 * holds the buffer, otherwise line numbers will be invalid. */
4014 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004015 }
4016
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004017 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004018
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004019 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004020 return FAIL;
4021
4022 if (len_change)
4023 *len_change = -1;
4024
4025 return OK;
4026 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004027 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004028 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004029 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004030
4031 if (save == NULL)
4032 return FAIL;
4033
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004034 VimTryStart();
4035
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004036 /* We do not need to free "save" if ml_replace() consumes it. */
4037 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004038 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004039
4040 if (u_savesub((linenr_T)n) == FAIL)
4041 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004042 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004043 vim_free(save);
4044 }
4045 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4046 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004047 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004048 vim_free(save);
4049 }
4050 else
4051 changed_bytes((linenr_T)n, 0);
4052
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004053 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004054
4055 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004056 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004057 check_cursor_col();
4058
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004059 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004060 return FAIL;
4061
4062 if (len_change)
4063 *len_change = 0;
4064
4065 return OK;
4066 }
4067 else
4068 {
4069 PyErr_BadArgument();
4070 return FAIL;
4071 }
4072}
4073
Bram Moolenaar19e60942011-06-19 00:27:51 +02004074/* Replace a range of lines in the specified buffer. The line numbers are in
4075 * Vim format (1-based). The range is from lo up to, but not including, hi.
4076 * The replacement lines are given as a Python list of string objects. The
4077 * list is checked for validity and correct format. Errors are returned as a
4078 * value of FAIL. The return value is OK on success.
4079 * If OK is returned and len_change is not NULL, *len_change
4080 * is set to the change in the buffer length.
4081 */
4082 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004083SetBufferLineList(
4084 buf_T *buf,
4085 PyInt lo,
4086 PyInt hi,
4087 PyObject *list,
4088 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004089{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004090 buf_T *save_curbuf = NULL;
4091 win_T *save_curwin = NULL;
4092 tabpage_T *save_curtab = NULL;
4093
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004094 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004095 * There are three cases:
4096 * 1. NULL, or None - this is a deletion.
4097 * 2. A list - this is a replacement.
4098 * 3. Anything else - this is an error.
4099 */
4100 if (list == Py_None || list == NULL)
4101 {
4102 PyInt i;
4103 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004104
4105 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004106 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004107 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004108
4109 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004110 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004111 else
4112 {
4113 for (i = 0; i < n; ++i)
4114 {
4115 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4116 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004117 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004118 break;
4119 }
4120 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004121 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004122 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004123 if (save_curbuf == NULL)
4124 /* Only adjust marks if we managed to switch to a window that
4125 * holds the buffer, otherwise line numbers will be invalid. */
4126 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004127 }
4128
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004129 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004130
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004131 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004132 return FAIL;
4133
4134 if (len_change)
4135 *len_change = -n;
4136
4137 return OK;
4138 }
4139 else if (PyList_Check(list))
4140 {
4141 PyInt i;
4142 PyInt new_len = PyList_Size(list);
4143 PyInt old_len = hi - lo;
4144 PyInt extra = 0; /* lines added to text, can be negative */
4145 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004146
4147 if (new_len == 0) /* avoid allocating zero bytes */
4148 array = NULL;
4149 else
4150 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004151 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004152 if (array == NULL)
4153 {
4154 PyErr_NoMemory();
4155 return FAIL;
4156 }
4157 }
4158
4159 for (i = 0; i < new_len; ++i)
4160 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004161 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004162
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004163 if (!(line = PyList_GetItem(list, i)) ||
4164 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004165 {
4166 while (i)
4167 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004168 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004169 return FAIL;
4170 }
4171 }
4172
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004173 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004174 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004175
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004176 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004177 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004178
4179 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004180 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004181
4182 /* If the size of the range is reducing (ie, new_len < old_len) we
4183 * need to delete some old_len. We do this at the start, by
4184 * repeatedly deleting line "lo".
4185 */
4186 if (!PyErr_Occurred())
4187 {
4188 for (i = 0; i < old_len - new_len; ++i)
4189 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4190 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004191 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004192 break;
4193 }
4194 extra -= i;
4195 }
4196
4197 /* For as long as possible, replace the existing old_len with the
4198 * new old_len. This is a more efficient operation, as it requires
4199 * less memory allocation and freeing.
4200 */
4201 if (!PyErr_Occurred())
4202 {
4203 for (i = 0; i < old_len && i < new_len; ++i)
4204 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4205 == FAIL)
4206 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004207 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004208 break;
4209 }
4210 }
4211 else
4212 i = 0;
4213
4214 /* Now we may need to insert the remaining new old_len. If we do, we
4215 * must free the strings as we finish with them (we can't pass the
4216 * responsibility to vim in this case).
4217 */
4218 if (!PyErr_Occurred())
4219 {
4220 while (i < new_len)
4221 {
4222 if (ml_append((linenr_T)(lo + i - 1),
4223 (char_u *)array[i], 0, FALSE) == FAIL)
4224 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004225 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004226 break;
4227 }
4228 vim_free(array[i]);
4229 ++i;
4230 ++extra;
4231 }
4232 }
4233
4234 /* Free any left-over old_len, as a result of an error */
4235 while (i < new_len)
4236 {
4237 vim_free(array[i]);
4238 ++i;
4239 }
4240
4241 /* Free the array of old_len. All of its contents have now
4242 * been dealt with (either freed, or the responsibility passed
4243 * to vim.
4244 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004245 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004246
4247 /* Adjust marks. Invalidate any which lie in the
4248 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004249 * Only adjust marks if we managed to switch to a window that holds
4250 * the buffer, otherwise line numbers will be invalid. */
4251 if (save_curbuf == NULL)
4252 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004253 (long)MAXLNUM, (long)extra);
4254 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4255
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004256 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004257 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4258
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004259 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004260 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004261
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004262 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004263 return FAIL;
4264
4265 if (len_change)
4266 *len_change = new_len - old_len;
4267
4268 return OK;
4269 }
4270 else
4271 {
4272 PyErr_BadArgument();
4273 return FAIL;
4274 }
4275}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004276
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004277/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004278 * The line number is in Vim format (1-based). The lines to be inserted are
4279 * given as a Python list of string objects or as a single string. The lines
4280 * to be added are checked for validity and correct format. Errors are
4281 * returned as a value of FAIL. The return value is OK on success.
4282 * If OK is returned and len_change is not NULL, *len_change
4283 * is set to the change in the buffer length.
4284 */
4285 static int
4286InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4287{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004288 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004289 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004290 tabpage_T *save_curtab = NULL;
4291
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004292 /* First of all, we check the type of the supplied Python object.
4293 * It must be a string or a list, or the call is in error.
4294 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004295 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004296 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004297 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004298
4299 if (str == NULL)
4300 return FAIL;
4301
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004302 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004303 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004304 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004305
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004306 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004307 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004308 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004309 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004310 else if (save_curbuf == NULL)
4311 /* Only adjust marks if we managed to switch to a window that
4312 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004313 appended_lines_mark((linenr_T)n, 1L);
4314
4315 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004316 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004317 update_screen(VALID);
4318
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004319 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004320 return FAIL;
4321
4322 if (len_change)
4323 *len_change = 1;
4324
4325 return OK;
4326 }
4327 else if (PyList_Check(lines))
4328 {
4329 PyInt i;
4330 PyInt size = PyList_Size(lines);
4331 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004332
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004333 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004334 if (array == NULL)
4335 {
4336 PyErr_NoMemory();
4337 return FAIL;
4338 }
4339
4340 for (i = 0; i < size; ++i)
4341 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004342 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004343
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004344 if (!(line = PyList_GetItem(lines, i)) ||
4345 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004346 {
4347 while (i)
4348 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004349 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004350 return FAIL;
4351 }
4352 }
4353
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004354 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004355 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004356 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004357
4358 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004359 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004360 else
4361 {
4362 for (i = 0; i < size; ++i)
4363 {
4364 if (ml_append((linenr_T)(n + i),
4365 (char_u *)array[i], 0, FALSE) == FAIL)
4366 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004367 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004368
4369 /* Free the rest of the lines */
4370 while (i < size)
4371 vim_free(array[i++]);
4372
4373 break;
4374 }
4375 vim_free(array[i]);
4376 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004377 if (i > 0 && save_curbuf == NULL)
4378 /* Only adjust marks if we managed to switch to a window that
4379 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004380 appended_lines_mark((linenr_T)n, (long)i);
4381 }
4382
4383 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004384 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004385 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004386 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004387
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004388 update_screen(VALID);
4389
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004390 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004391 return FAIL;
4392
4393 if (len_change)
4394 *len_change = size;
4395
4396 return OK;
4397 }
4398 else
4399 {
4400 PyErr_BadArgument();
4401 return FAIL;
4402 }
4403}
4404
4405/*
4406 * Common routines for buffers and line ranges
4407 * -------------------------------------------
4408 */
4409
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004410typedef struct
4411{
4412 PyObject_HEAD
4413 buf_T *buf;
4414} BufferObject;
4415
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004416 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004417CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004418{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004419 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004420 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004421 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004422 return -1;
4423 }
4424
4425 return 0;
4426}
4427
4428 static PyObject *
4429RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4430{
4431 if (CheckBuffer(self))
4432 return NULL;
4433
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004434 if (end == -1)
4435 end = self->buf->b_ml.ml_line_count;
4436
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004437 if (n < 0)
4438 n += end - start + 1;
4439
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004440 if (n < 0 || n > end - start)
4441 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004442 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004443 return NULL;
4444 }
4445
4446 return GetBufferLine(self->buf, n+start);
4447}
4448
4449 static PyObject *
4450RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4451{
4452 PyInt size;
4453
4454 if (CheckBuffer(self))
4455 return NULL;
4456
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004457 if (end == -1)
4458 end = self->buf->b_ml.ml_line_count;
4459
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004460 size = end - start + 1;
4461
4462 if (lo < 0)
4463 lo = 0;
4464 else if (lo > size)
4465 lo = size;
4466 if (hi < 0)
4467 hi = 0;
4468 if (hi < lo)
4469 hi = lo;
4470 else if (hi > size)
4471 hi = size;
4472
4473 return GetBufferLineList(self->buf, lo+start, hi+start);
4474}
4475
4476 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004477RBAsItem(
4478 BufferObject *self,
4479 PyInt n,
4480 PyObject *valObject,
4481 PyInt start,
4482 PyInt end,
4483 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004484{
4485 PyInt len_change;
4486
4487 if (CheckBuffer(self))
4488 return -1;
4489
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004490 if (end == -1)
4491 end = self->buf->b_ml.ml_line_count;
4492
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004493 if (n < 0)
4494 n += end - start + 1;
4495
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004496 if (n < 0 || n > end - start)
4497 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004498 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004499 return -1;
4500 }
4501
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004502 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004503 return -1;
4504
4505 if (new_end)
4506 *new_end = end + len_change;
4507
4508 return 0;
4509}
4510
Bram Moolenaar19e60942011-06-19 00:27:51 +02004511 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004512RBAsSlice(
4513 BufferObject *self,
4514 PyInt lo,
4515 PyInt hi,
4516 PyObject *valObject,
4517 PyInt start,
4518 PyInt end,
4519 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004520{
4521 PyInt size;
4522 PyInt len_change;
4523
4524 /* Self must be a valid buffer */
4525 if (CheckBuffer(self))
4526 return -1;
4527
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004528 if (end == -1)
4529 end = self->buf->b_ml.ml_line_count;
4530
Bram Moolenaar19e60942011-06-19 00:27:51 +02004531 /* Sort out the slice range */
4532 size = end - start + 1;
4533
4534 if (lo < 0)
4535 lo = 0;
4536 else if (lo > size)
4537 lo = size;
4538 if (hi < 0)
4539 hi = 0;
4540 if (hi < lo)
4541 hi = lo;
4542 else if (hi > size)
4543 hi = size;
4544
4545 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004546 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004547 return -1;
4548
4549 if (new_end)
4550 *new_end = end + len_change;
4551
4552 return 0;
4553}
4554
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004555
4556 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004557RBAppend(
4558 BufferObject *self,
4559 PyObject *args,
4560 PyInt start,
4561 PyInt end,
4562 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004563{
4564 PyObject *lines;
4565 PyInt len_change;
4566 PyInt max;
4567 PyInt n;
4568
4569 if (CheckBuffer(self))
4570 return NULL;
4571
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004572 if (end == -1)
4573 end = self->buf->b_ml.ml_line_count;
4574
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004575 max = n = end - start + 1;
4576
4577 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4578 return NULL;
4579
4580 if (n < 0 || n > max)
4581 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004582 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004583 return NULL;
4584 }
4585
4586 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4587 return NULL;
4588
4589 if (new_end)
4590 *new_end = end + len_change;
4591
4592 Py_INCREF(Py_None);
4593 return Py_None;
4594}
4595
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004596/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004597 */
4598
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004599static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004600static PySequenceMethods RangeAsSeq;
4601static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004602
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004603typedef struct
4604{
4605 PyObject_HEAD
4606 BufferObject *buf;
4607 PyInt start;
4608 PyInt end;
4609} RangeObject;
4610
4611 static PyObject *
4612RangeNew(buf_T *buf, PyInt start, PyInt end)
4613{
4614 BufferObject *bufr;
4615 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004616 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004617 if (self == NULL)
4618 return NULL;
4619
4620 bufr = (BufferObject *)BufferNew(buf);
4621 if (bufr == NULL)
4622 {
4623 Py_DECREF(self);
4624 return NULL;
4625 }
4626 Py_INCREF(bufr);
4627
4628 self->buf = bufr;
4629 self->start = start;
4630 self->end = end;
4631
4632 return (PyObject *)(self);
4633}
4634
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004635 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004636RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004637{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004638 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004639 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004640 PyObject_GC_Del((void *)(self));
4641}
4642
4643 static int
4644RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4645{
4646 Py_VISIT(((PyObject *)(self->buf)));
4647 return 0;
4648}
4649
4650 static int
4651RangeClear(RangeObject *self)
4652{
4653 Py_CLEAR(self->buf);
4654 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004655}
4656
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004657 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004658RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004659{
4660 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004661 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004662 return -1; /* ??? */
4663
Bram Moolenaard6e39182013-05-21 18:30:34 +02004664 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004665}
4666
4667 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004668RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004669{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004670 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004671}
4672
4673 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004674RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004675{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004676 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004677}
4678
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004679static char *RangeAttrs[] = {
4680 "start", "end",
4681 NULL
4682};
4683
4684 static PyObject *
4685RangeDir(PyObject *self)
4686{
4687 return ObjectDir(self, RangeAttrs);
4688}
4689
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004690 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004691RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004692{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004693 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004694}
4695
4696 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004697RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004698{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004699 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004700 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4701 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004702 else
4703 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004704 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004705
4706 if (name == NULL)
4707 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004708
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004709 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004710 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004711 }
4712}
4713
4714static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004715 /* name, function, calling, documentation */
4716 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004717 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4718 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004719};
4720
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004721static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004722static PySequenceMethods BufferAsSeq;
4723static PyMappingMethods BufferAsMapping;
4724
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004725 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004726BufferNew(buf_T *buf)
4727{
4728 /* We need to handle deletion of buffers underneath us.
4729 * If we add a "b_python*_ref" field to the buf_T structure,
4730 * then we can get at it in buf_freeall() in vim. We then
4731 * need to create only ONE Python object per buffer - if
4732 * we try to create a second, just INCREF the existing one
4733 * and return it. The (single) Python object referring to
4734 * the buffer is stored in "b_python*_ref".
4735 * Question: what to do on a buf_freeall(). We'll probably
4736 * have to either delete the Python object (DECREF it to
4737 * zero - a bad idea, as it leaves dangling refs!) or
4738 * set the buf_T * value to an invalid value (-1?), which
4739 * means we need checks in all access functions... Bah.
4740 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004741 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004742 * b_python_ref and b_python3_ref fields respectively.
4743 */
4744
4745 BufferObject *self;
4746
4747 if (BUF_PYTHON_REF(buf) != NULL)
4748 {
4749 self = BUF_PYTHON_REF(buf);
4750 Py_INCREF(self);
4751 }
4752 else
4753 {
4754 self = PyObject_NEW(BufferObject, &BufferType);
4755 if (self == NULL)
4756 return NULL;
4757 self->buf = buf;
4758 BUF_PYTHON_REF(buf) = self;
4759 }
4760
4761 return (PyObject *)(self);
4762}
4763
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004764 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004765BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004766{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004767 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4768 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004769
4770 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004771}
4772
Bram Moolenaar971db462013-05-12 18:44:48 +02004773 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004774BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004775{
4776 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004777 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004778 return -1; /* ??? */
4779
Bram Moolenaard6e39182013-05-21 18:30:34 +02004780 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004781}
4782
4783 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004784BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004785{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004786 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004787}
4788
4789 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004790BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004791{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004792 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004793}
4794
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004795static char *BufferAttrs[] = {
4796 "name", "number", "vars", "options", "valid",
4797 NULL
4798};
4799
4800 static PyObject *
4801BufferDir(PyObject *self)
4802{
4803 return ObjectDir(self, BufferAttrs);
4804}
4805
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004806 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004807BufferAttrValid(BufferObject *self, char *name)
4808{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004809 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004810
4811 if (strcmp(name, "valid") != 0)
4812 return NULL;
4813
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004814 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4815 Py_INCREF(ret);
4816 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004817}
4818
4819 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004820BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004821{
4822 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004823 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004824 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004825 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004826 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004827 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004828 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004829 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004830 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4831 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004832 else if (strcmp(name, "__members__") == 0)
4833 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004834 else
4835 return NULL;
4836}
4837
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004838 static int
4839BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4840{
4841 if (CheckBuffer(self))
4842 return -1;
4843
4844 if (strcmp(name, "name") == 0)
4845 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004846 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004847 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004848 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004849 PyObject *todecref;
4850
4851 if (!(val = StringToChars(valObject, &todecref)))
4852 return -1;
4853
4854 VimTryStart();
4855 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4856 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004857 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004858 aucmd_restbuf(&aco);
4859 Py_XDECREF(todecref);
4860 if (VimTryEnd())
4861 return -1;
4862
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004863 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004864 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004865 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004866 return -1;
4867 }
4868 return 0;
4869 }
4870 else
4871 {
4872 PyErr_SetString(PyExc_AttributeError, name);
4873 return -1;
4874 }
4875}
4876
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004877 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004878BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004879{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004880 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004881}
4882
4883 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004884BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004885{
4886 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004887 char_u *pmark;
4888 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004889 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004890 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004891
Bram Moolenaard6e39182013-05-21 18:30:34 +02004892 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004893 return NULL;
4894
Bram Moolenaar389a1792013-06-23 13:00:44 +02004895 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004896 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004897
Bram Moolenaar389a1792013-06-23 13:00:44 +02004898 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004899 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004900 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004901 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004902 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004903 return NULL;
4904 }
4905
4906 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004907
4908 Py_XDECREF(todecref);
4909
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004910 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004911 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004912 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004913 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004914 if (VimTryEnd())
4915 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004916
4917 if (posp == NULL)
4918 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004919 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004920 return NULL;
4921 }
4922
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004923 if (posp->lnum <= 0)
4924 {
4925 /* Or raise an error? */
4926 Py_INCREF(Py_None);
4927 return Py_None;
4928 }
4929
4930 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
4931}
4932
4933 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004934BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004935{
4936 PyInt start;
4937 PyInt end;
4938
Bram Moolenaard6e39182013-05-21 18:30:34 +02004939 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004940 return NULL;
4941
4942 if (!PyArg_ParseTuple(args, "nn", &start, &end))
4943 return NULL;
4944
Bram Moolenaard6e39182013-05-21 18:30:34 +02004945 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004946}
4947
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004948 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004949BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004950{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004951 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004952 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004953 else
4954 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004955 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004956
4957 if (name == NULL)
4958 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004959
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004960 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004961 }
4962}
4963
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004964static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004965 /* name, function, calling, documentation */
4966 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02004967 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004968 {"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 +02004969 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
4970 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004971};
4972
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02004973/*
4974 * Buffer list object - Implementation
4975 */
4976
4977static PyTypeObject BufMapType;
4978
4979typedef struct
4980{
4981 PyObject_HEAD
4982} BufMapObject;
4983
4984 static PyInt
4985BufMapLength(PyObject *self UNUSED)
4986{
4987 buf_T *b = firstbuf;
4988 PyInt n = 0;
4989
4990 while (b)
4991 {
4992 ++n;
4993 b = b->b_next;
4994 }
4995
4996 return n;
4997}
4998
4999 static PyObject *
5000BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5001{
5002 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005003 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005004
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005005 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005006 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005007
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005008 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005009
5010 if (b)
5011 return BufferNew(b);
5012 else
5013 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005014 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005015 return NULL;
5016 }
5017}
5018
5019 static void
5020BufMapIterDestruct(PyObject *buffer)
5021{
5022 /* Iteration was stopped before all buffers were processed */
5023 if (buffer)
5024 {
5025 Py_DECREF(buffer);
5026 }
5027}
5028
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005029 static int
5030BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5031{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005032 if (buffer)
5033 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005034 return 0;
5035}
5036
5037 static int
5038BufMapIterClear(PyObject **buffer)
5039{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005040 if (*buffer)
5041 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005042 return 0;
5043}
5044
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005045 static PyObject *
5046BufMapIterNext(PyObject **buffer)
5047{
5048 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005049 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005050
5051 if (!*buffer)
5052 return NULL;
5053
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005054 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005055
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005056 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005057 {
5058 *buffer = NULL;
5059 return NULL;
5060 }
5061
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005062 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005063 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005064 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005065 return NULL;
5066 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005067 /* Do not increment reference: we no longer hold it (decref), but whoever
5068 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005069 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005070}
5071
5072 static PyObject *
5073BufMapIter(PyObject *self UNUSED)
5074{
5075 PyObject *buffer;
5076
5077 buffer = BufferNew(firstbuf);
5078 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005079 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5080 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005081}
5082
5083static PyMappingMethods BufMapAsMapping = {
5084 (lenfunc) BufMapLength,
5085 (binaryfunc) BufMapItem,
5086 (objobjargproc) 0,
5087};
5088
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005089/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005090 */
5091
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005092static char *CurrentAttrs[] = {
5093 "buffer", "window", "line", "range", "tabpage",
5094 NULL
5095};
5096
5097 static PyObject *
5098CurrentDir(PyObject *self)
5099{
5100 return ObjectDir(self, CurrentAttrs);
5101}
5102
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005103 static PyObject *
5104CurrentGetattr(PyObject *self UNUSED, char *name)
5105{
5106 if (strcmp(name, "buffer") == 0)
5107 return (PyObject *)BufferNew(curbuf);
5108 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005109 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005110 else if (strcmp(name, "tabpage") == 0)
5111 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005112 else if (strcmp(name, "line") == 0)
5113 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5114 else if (strcmp(name, "range") == 0)
5115 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005116 else if (strcmp(name, "__members__") == 0)
5117 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005118 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005119#if PY_MAJOR_VERSION < 3
5120 return Py_FindMethod(WindowMethods, self, name);
5121#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005122 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005123#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005124}
5125
5126 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005127CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005128{
5129 if (strcmp(name, "line") == 0)
5130 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005131 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5132 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005133 return -1;
5134
5135 return 0;
5136 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005137 else if (strcmp(name, "buffer") == 0)
5138 {
5139 int count;
5140
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005141 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005142 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005143 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005144 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005145 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005146 return -1;
5147 }
5148
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005149 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005150 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005151 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005152
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005153 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005154 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5155 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005156 if (VimTryEnd())
5157 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005158 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005159 return -1;
5160 }
5161
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005162 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005163 }
5164 else if (strcmp(name, "window") == 0)
5165 {
5166 int count;
5167
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005168 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005169 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005170 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005171 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005172 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005173 return -1;
5174 }
5175
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005176 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005177 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005178 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005179
5180 if (!count)
5181 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005182 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005183 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005184 return -1;
5185 }
5186
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005187 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005188 win_goto(((WindowObject *)(valObject))->win);
5189 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005190 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005191 if (VimTryEnd())
5192 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005193 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005194 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005195 return -1;
5196 }
5197
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005198 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005199 }
5200 else if (strcmp(name, "tabpage") == 0)
5201 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005202 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005203 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005204 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005205 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005206 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005207 return -1;
5208 }
5209
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005210 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005211 return -1;
5212
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005213 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005214 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5215 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005216 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005217 if (VimTryEnd())
5218 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005219 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005220 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005221 return -1;
5222 }
5223
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005224 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005225 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005226 else
5227 {
5228 PyErr_SetString(PyExc_AttributeError, name);
5229 return -1;
5230 }
5231}
5232
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005233static struct PyMethodDef CurrentMethods[] = {
5234 /* name, function, calling, documentation */
5235 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5236 { NULL, NULL, 0, NULL}
5237};
5238
Bram Moolenaardb913952012-06-29 12:54:53 +02005239 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005240init_range_cmd(exarg_T *eap)
5241{
5242 RangeStart = eap->line1;
5243 RangeEnd = eap->line2;
5244}
5245
5246 static void
5247init_range_eval(typval_T *rettv UNUSED)
5248{
5249 RangeStart = (PyInt) curwin->w_cursor.lnum;
5250 RangeEnd = RangeStart;
5251}
5252
5253 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005254run_cmd(const char *cmd, void *arg UNUSED
5255#ifdef PY_CAN_RECURSE
5256 , PyGILState_STATE *pygilstate UNUSED
5257#endif
5258 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005259{
Bram Moolenaar41009372013-07-01 22:03:04 +02005260 PyObject *run_ret;
5261 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5262 if (run_ret != NULL)
5263 {
5264 Py_DECREF(run_ret);
5265 }
5266 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5267 {
5268 EMSG2(_(e_py_systemexit), "python");
5269 PyErr_Clear();
5270 }
5271 else
5272 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005273}
5274
5275static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5276static int code_hdr_len = 30;
5277
5278 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005279run_do(const char *cmd, void *arg UNUSED
5280#ifdef PY_CAN_RECURSE
5281 , PyGILState_STATE *pygilstate
5282#endif
5283 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005284{
5285 PyInt lnum;
5286 size_t len;
5287 char *code;
5288 int status;
5289 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005290 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005291
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005292 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005293 {
5294 EMSG(_("cannot save undo information"));
5295 return;
5296 }
5297
5298 len = code_hdr_len + STRLEN(cmd);
5299 code = PyMem_New(char, len + 1);
5300 memcpy(code, code_hdr, code_hdr_len);
5301 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005302 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5303 status = -1;
5304 if (run_ret != NULL)
5305 {
5306 status = 0;
5307 Py_DECREF(run_ret);
5308 }
5309 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5310 {
5311 PyMem_Free(code);
5312 EMSG2(_(e_py_systemexit), "python");
5313 PyErr_Clear();
5314 return;
5315 }
5316 else
5317 PyErr_PrintEx(1);
5318
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005319 PyMem_Free(code);
5320
5321 if (status)
5322 {
5323 EMSG(_("failed to run the code"));
5324 return;
5325 }
5326
5327 status = 0;
5328 pymain = PyImport_AddModule("__main__");
5329 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005330#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005331 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005332#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005333
5334 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5335 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005336 PyObject *line;
5337 PyObject *linenr;
5338 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005339
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005340#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005341 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005342#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005343 if (!(line = GetBufferLine(curbuf, lnum)))
5344 goto err;
5345 if (!(linenr = PyInt_FromLong((long) lnum)))
5346 {
5347 Py_DECREF(line);
5348 goto err;
5349 }
5350 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5351 Py_DECREF(line);
5352 Py_DECREF(linenr);
5353 if (!ret)
5354 goto err;
5355
5356 if (ret != Py_None)
5357 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5358 goto err;
5359
5360 Py_XDECREF(ret);
5361 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005362#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005363 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005364#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005365 }
5366 goto out;
5367err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005368#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005369 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005370#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005371 PyErr_PrintEx(0);
5372 PythonIO_Flush();
5373 status = 1;
5374out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005375#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005376 if (!status)
5377 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005378#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005379 Py_DECREF(pyfunc);
5380 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5381 if (status)
5382 return;
5383 check_cursor();
5384 update_curbuf(NOT_VALID);
5385}
5386
5387 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005388run_eval(const char *cmd, typval_T *rettv
5389#ifdef PY_CAN_RECURSE
5390 , PyGILState_STATE *pygilstate UNUSED
5391#endif
5392 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005393{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005394 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005395
Bram Moolenaar41009372013-07-01 22:03:04 +02005396 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005397 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005398 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005399 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005400 {
5401 EMSG2(_(e_py_systemexit), "python");
5402 PyErr_Clear();
5403 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005404 else
5405 {
5406 if (PyErr_Occurred() && !msg_silent)
5407 PyErr_PrintEx(0);
5408 EMSG(_("E858: Eval did not return a valid python object"));
5409 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005410 }
5411 else
5412 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005413 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005414 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005415 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005416 }
5417 PyErr_Clear();
5418}
5419
5420 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005421set_ref_in_py(const int copyID)
5422{
5423 pylinkedlist_T *cur;
5424 dict_T *dd;
5425 list_T *ll;
5426
5427 if (lastdict != NULL)
5428 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5429 {
5430 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5431 if (dd->dv_copyID != copyID)
5432 {
5433 dd->dv_copyID = copyID;
5434 set_ref_in_ht(&dd->dv_hashtab, copyID);
5435 }
5436 }
5437
5438 if (lastlist != NULL)
5439 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5440 {
5441 ll = ((ListObject *) (cur->pll_obj))->list;
5442 if (ll->lv_copyID != copyID)
5443 {
5444 ll->lv_copyID = copyID;
5445 set_ref_in_list(ll, copyID);
5446 }
5447 }
5448}
5449
5450 static int
5451set_string_copy(char_u *str, typval_T *tv)
5452{
5453 tv->vval.v_string = vim_strsave(str);
5454 if (tv->vval.v_string == NULL)
5455 {
5456 PyErr_NoMemory();
5457 return -1;
5458 }
5459 return 0;
5460}
5461
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005462 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005463pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005464{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005465 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005466 char_u *key;
5467 dictitem_T *di;
5468 PyObject *keyObject;
5469 PyObject *valObject;
5470 Py_ssize_t iter = 0;
5471
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005472 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005473 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005474
5475 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005476 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005477
5478 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5479 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005480 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005481
Bram Moolenaara03e6312013-05-29 22:49:26 +02005482 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005483 {
5484 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005485 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005486 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005487
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005488 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005489 {
5490 dict_unref(dict);
5491 return -1;
5492 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005493
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005494 if (*key == NUL)
5495 {
5496 dict_unref(dict);
5497 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005498 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005499 return -1;
5500 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005501
5502 di = dictitem_alloc(key);
5503
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005504 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005505
5506 if (di == NULL)
5507 {
5508 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005509 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005510 return -1;
5511 }
5512 di->di_tv.v_lock = 0;
5513
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005514 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005515 {
5516 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005517 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005518 return -1;
5519 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005520
5521 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005522 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005523 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005524 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005525 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005526 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005527 return -1;
5528 }
5529 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005530
5531 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005532 return 0;
5533}
5534
5535 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005536pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005537{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005538 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005539 char_u *key;
5540 dictitem_T *di;
5541 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005542 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005543 PyObject *keyObject;
5544 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005545
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005546 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005547 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005548
5549 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005550 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005551
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005552 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005553 {
5554 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005555 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005556 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005557
5558 if (!(iterator = PyObject_GetIter(list)))
5559 {
5560 dict_unref(dict);
5561 Py_DECREF(list);
5562 return -1;
5563 }
5564 Py_DECREF(list);
5565
5566 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005567 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005568 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005569
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005570 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005571 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005572 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005573 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005574 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005575 return -1;
5576 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005577
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005578 if (*key == NUL)
5579 {
5580 Py_DECREF(keyObject);
5581 Py_DECREF(iterator);
5582 Py_XDECREF(todecref);
5583 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005584 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005585 return -1;
5586 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005587
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005588 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005589 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005590 Py_DECREF(keyObject);
5591 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005592 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005593 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005594 return -1;
5595 }
5596
5597 di = dictitem_alloc(key);
5598
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005599 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005600 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005601
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005602 if (di == NULL)
5603 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005604 Py_DECREF(iterator);
5605 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005606 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005607 PyErr_NoMemory();
5608 return -1;
5609 }
5610 di->di_tv.v_lock = 0;
5611
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005612 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005613 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005614 Py_DECREF(iterator);
5615 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005616 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005617 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005618 return -1;
5619 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005620
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005621 Py_DECREF(valObject);
5622
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005623 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005624 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005625 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005626 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005627 dictitem_free(di);
5628 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005629 return -1;
5630 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005631 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005632 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005633 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005634 return 0;
5635}
5636
5637 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005638pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005639{
5640 list_T *l;
5641
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005642 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005643 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005644
5645 tv->v_type = VAR_LIST;
5646 tv->vval.v_list = l;
5647
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005648 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005649 {
5650 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005651 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005652 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005653
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005654 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005655 return 0;
5656}
5657
Bram Moolenaardb913952012-06-29 12:54:53 +02005658typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5659
5660 static int
5661convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005662 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005663{
5664 PyObject *capsule;
5665 char hexBuf[sizeof(void *) * 2 + 3];
5666
5667 sprintf(hexBuf, "%p", obj);
5668
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005669# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005670 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005671# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005672 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005673# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005674 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005675 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005676# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005677 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005678# else
5679 capsule = PyCObject_FromVoidPtr(tv, NULL);
5680# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005681 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5682 {
5683 Py_DECREF(capsule);
5684 tv->v_type = VAR_UNKNOWN;
5685 return -1;
5686 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005687
5688 Py_DECREF(capsule);
5689
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005690 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005691 {
5692 tv->v_type = VAR_UNKNOWN;
5693 return -1;
5694 }
5695 /* As we are not using copy_tv which increments reference count we must
5696 * do it ourself. */
5697 switch(tv->v_type)
5698 {
5699 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5700 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5701 }
5702 }
5703 else
5704 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005705 typval_T *v;
5706
5707# ifdef PY_USE_CAPSULE
5708 v = PyCapsule_GetPointer(capsule, NULL);
5709# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005710 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005711# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005712 copy_tv(v, tv);
5713 }
5714 return 0;
5715}
5716
5717 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005718ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5719{
5720 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005721 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005722
5723 if (!(lookup_dict = PyDict_New()))
5724 return -1;
5725
5726 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5727 {
5728 tv->v_type = VAR_DICT;
5729 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5730 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005731 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005732 }
5733 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005734 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005735 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005736 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005737 else
5738 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005739 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005740 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005741 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005742 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005743 }
5744 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005745 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005746}
5747
5748 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005749ConvertFromPyObject(PyObject *obj, typval_T *tv)
5750{
5751 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005752 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005753
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005754 if (!(lookup_dict = PyDict_New()))
5755 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005756 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005757 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005758 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005759}
5760
5761 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005762_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005763{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005764 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005765 {
5766 tv->v_type = VAR_DICT;
5767 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5768 ++tv->vval.v_dict->dv_refcount;
5769 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005770 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005771 {
5772 tv->v_type = VAR_LIST;
5773 tv->vval.v_list = (((ListObject *)(obj))->list);
5774 ++tv->vval.v_list->lv_refcount;
5775 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005776 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005777 {
5778 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5779 return -1;
5780
5781 tv->v_type = VAR_FUNC;
5782 func_ref(tv->vval.v_string);
5783 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005784 else if (PyBytes_Check(obj))
5785 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005786 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005787
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005788 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005789 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005790 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005791 return -1;
5792
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005793 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005794 return -1;
5795
5796 tv->v_type = VAR_STRING;
5797 }
5798 else if (PyUnicode_Check(obj))
5799 {
5800 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005801 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005802
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005803 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005804 if (bytes == NULL)
5805 return -1;
5806
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005807 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005808 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005809 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005810 return -1;
5811
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005812 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005813 {
5814 Py_XDECREF(bytes);
5815 return -1;
5816 }
5817 Py_XDECREF(bytes);
5818
5819 tv->v_type = VAR_STRING;
5820 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005821#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005822 else if (PyInt_Check(obj))
5823 {
5824 tv->v_type = VAR_NUMBER;
5825 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005826 if (PyErr_Occurred())
5827 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005828 }
5829#endif
5830 else if (PyLong_Check(obj))
5831 {
5832 tv->v_type = VAR_NUMBER;
5833 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005834 if (PyErr_Occurred())
5835 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005836 }
5837 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005838 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005839#ifdef FEAT_FLOAT
5840 else if (PyFloat_Check(obj))
5841 {
5842 tv->v_type = VAR_FLOAT;
5843 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5844 }
5845#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005846 else if (PyObject_HasAttrString(obj, "keys"))
5847 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005848 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005849 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005850 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005851 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005852 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005853 else if (PyNumber_Check(obj))
5854 {
5855 PyObject *num;
5856
5857 if (!(num = PyNumber_Long(obj)))
5858 return -1;
5859
5860 tv->v_type = VAR_NUMBER;
5861 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5862
5863 Py_DECREF(num);
5864 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005865 else
5866 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005867 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005868 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005869 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005870 return -1;
5871 }
5872 return 0;
5873}
5874
5875 static PyObject *
5876ConvertToPyObject(typval_T *tv)
5877{
5878 if (tv == NULL)
5879 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005880 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005881 return NULL;
5882 }
5883 switch (tv->v_type)
5884 {
5885 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005886 return PyBytes_FromString(tv->vval.v_string == NULL
5887 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005888 case VAR_NUMBER:
5889 return PyLong_FromLong((long) tv->vval.v_number);
5890#ifdef FEAT_FLOAT
5891 case VAR_FLOAT:
5892 return PyFloat_FromDouble((double) tv->vval.v_float);
5893#endif
5894 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005895 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005896 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005897 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005898 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005899 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005900 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005901 case VAR_UNKNOWN:
5902 Py_INCREF(Py_None);
5903 return Py_None;
5904 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005905 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005906 return NULL;
5907 }
5908}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005909
5910typedef struct
5911{
5912 PyObject_HEAD
5913} CurrentObject;
5914static PyTypeObject CurrentType;
5915
5916 static void
5917init_structs(void)
5918{
5919 vim_memset(&OutputType, 0, sizeof(OutputType));
5920 OutputType.tp_name = "vim.message";
5921 OutputType.tp_basicsize = sizeof(OutputObject);
5922 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
5923 OutputType.tp_doc = "vim message object";
5924 OutputType.tp_methods = OutputMethods;
5925#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005926 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
5927 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005928 OutputType.tp_alloc = call_PyType_GenericAlloc;
5929 OutputType.tp_new = call_PyType_GenericNew;
5930 OutputType.tp_free = call_PyObject_Free;
5931#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005932 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
5933 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005934#endif
5935
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005936 vim_memset(&IterType, 0, sizeof(IterType));
5937 IterType.tp_name = "vim.iter";
5938 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02005939 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005940 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02005941 IterType.tp_iter = (getiterfunc)IterIter;
5942 IterType.tp_iternext = (iternextfunc)IterNext;
5943 IterType.tp_dealloc = (destructor)IterDestructor;
5944 IterType.tp_traverse = (traverseproc)IterTraverse;
5945 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005946
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005947 vim_memset(&BufferType, 0, sizeof(BufferType));
5948 BufferType.tp_name = "vim.buffer";
5949 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005950 BufferType.tp_dealloc = (destructor)BufferDestructor;
5951 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005952 BufferType.tp_as_sequence = &BufferAsSeq;
5953 BufferType.tp_as_mapping = &BufferAsMapping;
5954 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
5955 BufferType.tp_doc = "vim buffer object";
5956 BufferType.tp_methods = BufferMethods;
5957#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005958 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005959 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005960 BufferType.tp_alloc = call_PyType_GenericAlloc;
5961 BufferType.tp_new = call_PyType_GenericNew;
5962 BufferType.tp_free = call_PyObject_Free;
5963#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005964 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02005965 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005966#endif
5967
5968 vim_memset(&WindowType, 0, sizeof(WindowType));
5969 WindowType.tp_name = "vim.window";
5970 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005971 WindowType.tp_dealloc = (destructor)WindowDestructor;
5972 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02005973 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005974 WindowType.tp_doc = "vim Window object";
5975 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02005976 WindowType.tp_traverse = (traverseproc)WindowTraverse;
5977 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005978#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005979 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
5980 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005981 WindowType.tp_alloc = call_PyType_GenericAlloc;
5982 WindowType.tp_new = call_PyType_GenericNew;
5983 WindowType.tp_free = call_PyObject_Free;
5984#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02005985 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
5986 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005987#endif
5988
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005989 vim_memset(&TabPageType, 0, sizeof(TabPageType));
5990 TabPageType.tp_name = "vim.tabpage";
5991 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02005992 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
5993 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005994 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
5995 TabPageType.tp_doc = "vim tab page object";
5996 TabPageType.tp_methods = TabPageMethods;
5997#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02005998 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005999 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6000 TabPageType.tp_new = call_PyType_GenericNew;
6001 TabPageType.tp_free = call_PyObject_Free;
6002#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006003 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006004#endif
6005
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006006 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6007 BufMapType.tp_name = "vim.bufferlist";
6008 BufMapType.tp_basicsize = sizeof(BufMapObject);
6009 BufMapType.tp_as_mapping = &BufMapAsMapping;
6010 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006011 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006012 BufferType.tp_doc = "vim buffer list";
6013
6014 vim_memset(&WinListType, 0, sizeof(WinListType));
6015 WinListType.tp_name = "vim.windowlist";
6016 WinListType.tp_basicsize = sizeof(WinListType);
6017 WinListType.tp_as_sequence = &WinListAsSeq;
6018 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6019 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006020 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006021
6022 vim_memset(&TabListType, 0, sizeof(TabListType));
6023 TabListType.tp_name = "vim.tabpagelist";
6024 TabListType.tp_basicsize = sizeof(TabListType);
6025 TabListType.tp_as_sequence = &TabListAsSeq;
6026 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6027 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006028
6029 vim_memset(&RangeType, 0, sizeof(RangeType));
6030 RangeType.tp_name = "vim.range";
6031 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006032 RangeType.tp_dealloc = (destructor)RangeDestructor;
6033 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006034 RangeType.tp_as_sequence = &RangeAsSeq;
6035 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006036 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006037 RangeType.tp_doc = "vim Range object";
6038 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006039 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6040 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006041#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006042 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006043 RangeType.tp_alloc = call_PyType_GenericAlloc;
6044 RangeType.tp_new = call_PyType_GenericNew;
6045 RangeType.tp_free = call_PyObject_Free;
6046#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006047 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006048#endif
6049
6050 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6051 CurrentType.tp_name = "vim.currentdata";
6052 CurrentType.tp_basicsize = sizeof(CurrentObject);
6053 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6054 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006055 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006056#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006057 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6058 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006059#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006060 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6061 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006062#endif
6063
6064 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6065 DictionaryType.tp_name = "vim.dictionary";
6066 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006067 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006068 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006069 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006070 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006071 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6072 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006073 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6074 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6075 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006076#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006077 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6078 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006079#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006080 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6081 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006082#endif
6083
6084 vim_memset(&ListType, 0, sizeof(ListType));
6085 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006086 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006087 ListType.tp_basicsize = sizeof(ListObject);
6088 ListType.tp_as_sequence = &ListAsSeq;
6089 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006090 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006091 ListType.tp_doc = "list pushing modifications to vim structure";
6092 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006093 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006094 ListType.tp_new = (newfunc)ListConstructor;
6095 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006096#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006097 ListType.tp_getattro = (getattrofunc)ListGetattro;
6098 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006099#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006100 ListType.tp_getattr = (getattrfunc)ListGetattr;
6101 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006102#endif
6103
6104 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006105 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006106 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006107 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6108 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006109 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006110 FunctionType.tp_doc = "object that calls vim function";
6111 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006112 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006113 FunctionType.tp_new = (newfunc)FunctionConstructor;
6114 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006115#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006116 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006117#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006118 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006119#endif
6120
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006121 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6122 OptionsType.tp_name = "vim.options";
6123 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006124 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006125 OptionsType.tp_doc = "object for manipulating options";
6126 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006127 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6128 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6129 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006130
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006131 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6132 LoaderType.tp_name = "vim.Loader";
6133 LoaderType.tp_basicsize = sizeof(LoaderObject);
6134 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6135 LoaderType.tp_doc = "vim message object";
6136 LoaderType.tp_methods = LoaderMethods;
6137 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6138
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006139#if PY_MAJOR_VERSION >= 3
6140 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6141 vimmodule.m_name = "vim";
6142 vimmodule.m_doc = "Vim Python interface\n";
6143 vimmodule.m_size = -1;
6144 vimmodule.m_methods = VimMethods;
6145#endif
6146}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006147
6148#define PYTYPE_READY(type) \
6149 if (PyType_Ready(&type)) \
6150 return -1;
6151
6152 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006153init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006154{
6155 PYTYPE_READY(IterType);
6156 PYTYPE_READY(BufferType);
6157 PYTYPE_READY(RangeType);
6158 PYTYPE_READY(WindowType);
6159 PYTYPE_READY(TabPageType);
6160 PYTYPE_READY(BufMapType);
6161 PYTYPE_READY(WinListType);
6162 PYTYPE_READY(TabListType);
6163 PYTYPE_READY(CurrentType);
6164 PYTYPE_READY(DictionaryType);
6165 PYTYPE_READY(ListType);
6166 PYTYPE_READY(FunctionType);
6167 PYTYPE_READY(OptionsType);
6168 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006169 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006170 return 0;
6171}
6172
6173 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006174init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006175{
6176 PyObject *path;
6177 PyObject *path_hook;
6178 PyObject *path_hooks;
6179
6180 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6181 return -1;
6182
6183 if (!(path_hooks = PySys_GetObject("path_hooks")))
6184 {
6185 PyErr_Clear();
6186 path_hooks = PyList_New(1);
6187 PyList_SET_ITEM(path_hooks, 0, path_hook);
6188 if (PySys_SetObject("path_hooks", path_hooks))
6189 {
6190 Py_DECREF(path_hooks);
6191 return -1;
6192 }
6193 Py_DECREF(path_hooks);
6194 }
6195 else if (PyList_Check(path_hooks))
6196 {
6197 if (PyList_Append(path_hooks, path_hook))
6198 {
6199 Py_DECREF(path_hook);
6200 return -1;
6201 }
6202 Py_DECREF(path_hook);
6203 }
6204 else
6205 {
6206 VimTryStart();
6207 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6208 "You should now do the following:\n"
6209 "- append vim.path_hook to sys.path_hooks\n"
6210 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6211 VimTryEnd(); /* Discard the error */
6212 Py_DECREF(path_hook);
6213 return 0;
6214 }
6215
6216 if (!(path = PySys_GetObject("path")))
6217 {
6218 PyErr_Clear();
6219 path = PyList_New(1);
6220 Py_INCREF(vim_special_path_object);
6221 PyList_SET_ITEM(path, 0, vim_special_path_object);
6222 if (PySys_SetObject("path", path))
6223 {
6224 Py_DECREF(path);
6225 return -1;
6226 }
6227 Py_DECREF(path);
6228 }
6229 else if (PyList_Check(path))
6230 {
6231 if (PyList_Append(path, vim_special_path_object))
6232 return -1;
6233 }
6234 else
6235 {
6236 VimTryStart();
6237 EMSG(_("Failed to set path: sys.path is not a list\n"
6238 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6239 VimTryEnd(); /* Discard the error */
6240 }
6241
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006242 return 0;
6243}
6244
6245static BufMapObject TheBufferMap =
6246{
6247 PyObject_HEAD_INIT(&BufMapType)
6248};
6249
6250static WinListObject TheWindowList =
6251{
6252 PyObject_HEAD_INIT(&WinListType)
6253 NULL
6254};
6255
6256static CurrentObject TheCurrent =
6257{
6258 PyObject_HEAD_INIT(&CurrentType)
6259};
6260
6261static TabListObject TheTabPageList =
6262{
6263 PyObject_HEAD_INIT(&TabListType)
6264};
6265
6266static struct numeric_constant {
6267 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006268 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006269} numeric_constants[] = {
6270 {"VAR_LOCKED", VAR_LOCKED},
6271 {"VAR_FIXED", VAR_FIXED},
6272 {"VAR_SCOPE", VAR_SCOPE},
6273 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6274};
6275
6276static struct object_constant {
6277 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006278 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006279} object_constants[] = {
6280 {"buffers", (PyObject *)(void *)&TheBufferMap},
6281 {"windows", (PyObject *)(void *)&TheWindowList},
6282 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6283 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006284
6285 {"Buffer", (PyObject *)&BufferType},
6286 {"Range", (PyObject *)&RangeType},
6287 {"Window", (PyObject *)&WindowType},
6288 {"TabPage", (PyObject *)&TabPageType},
6289 {"Dictionary", (PyObject *)&DictionaryType},
6290 {"List", (PyObject *)&ListType},
6291 {"Function", (PyObject *)&FunctionType},
6292 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006293 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006294};
6295
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006296#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006297 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006298 return -1;
6299
6300#define ADD_CHECKED_OBJECT(m, name, obj) \
6301 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006302 PyObject *valObject = obj; \
6303 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006304 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006305 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006306 }
6307
6308 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006309populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006310{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006311 int i;
6312 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006313 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006314 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006315
6316 for (i = 0; i < (int)(sizeof(numeric_constants)
6317 / sizeof(struct numeric_constant));
6318 ++i)
6319 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006320 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006321
6322 for (i = 0; i < (int)(sizeof(object_constants)
6323 / sizeof(struct object_constant));
6324 ++i)
6325 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006326 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006327
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006328 valObject = object_constants[i].valObject;
6329 Py_INCREF(valObject);
6330 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006331 }
6332
6333 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6334 return -1;
6335 ADD_OBJECT(m, "error", VimError);
6336
Bram Moolenaara9922d62013-05-30 13:01:18 +02006337 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6338 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006339 ADD_CHECKED_OBJECT(m, "options",
6340 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006341
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006342 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006343 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006344 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006345
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006346 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006347 return -1;
6348 ADD_OBJECT(m, "_getcwd", py_getcwd)
6349
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006350 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006351 return -1;
6352 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006353 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006354 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006355 if (PyObject_SetAttrString(other_module, "chdir", attr))
6356 {
6357 Py_DECREF(attr);
6358 return -1;
6359 }
6360 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006361
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006362 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006363 {
6364 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006365 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006366 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006367 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6368 {
6369 Py_DECREF(attr);
6370 return -1;
6371 }
6372 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006373 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006374 else
6375 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006376
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006377 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6378 return -1;
6379
6380 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6381
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006382 if (!(imp = PyImport_ImportModule("imp")))
6383 return -1;
6384
6385 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6386 {
6387 Py_DECREF(imp);
6388 return -1;
6389 }
6390
6391 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6392 {
6393 Py_DECREF(py_find_module);
6394 Py_DECREF(imp);
6395 return -1;
6396 }
6397
6398 Py_DECREF(imp);
6399
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006400 ADD_OBJECT(m, "_find_module", py_find_module);
6401 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006402
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006403 return 0;
6404}