blob: 9315324c46d08ada17744bc96e08ec2a9193c9b5 [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 Moolenaar4315f262014-01-31 14:54:04 +0100567 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100568 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100569 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200570 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200571 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200572 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100573 else if (msg_list != NULL && *msg_list != NULL)
574 {
575 int should_free;
576 char_u *msg;
577
578 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
579
580 if (msg == NULL)
581 {
582 PyErr_NoMemory();
583 return -1;
584 }
585
586 PyErr_SetVim((char *) msg);
587
588 free_global_msglist();
589
590 if (should_free)
591 vim_free(msg);
592
593 return -1;
594 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200595 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200596 return (PyErr_Occurred() ? -1 : 0);
597 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200598 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200599 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100600 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200601 return -1;
602 }
603 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200604 else
605 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200606 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200607 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200608 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200609 }
610}
611
612 static int
613VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200614{
615 if (got_int)
616 {
617 PyErr_SetNone(PyExc_KeyboardInterrupt);
618 return 1;
619 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200620 return 0;
621}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200622
623/* Vim module - Implementation
624 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200625
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200626 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200627VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200628{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200629 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200630 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200631 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200632
Bram Moolenaar389a1792013-06-23 13:00:44 +0200633 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200634 return NULL;
635
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200636 Py_BEGIN_ALLOW_THREADS
637 Python_Lock_Vim();
638
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200639 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200640 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200641 update_screen(VALID);
642
643 Python_Release_Vim();
644 Py_END_ALLOW_THREADS
645
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200646 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200647 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200648 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200649 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200650
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200651 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200652 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200653 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200654}
655
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200656/*
657 * Function to translate a typval_T into a PyObject; this will recursively
658 * translate lists/dictionaries into their Python equivalents.
659 *
660 * The depth parameter is to avoid infinite recursion, set it to 1 when
661 * you call VimToPython.
662 */
663 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200664VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200665{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200666 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200667 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200668 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200669
670 /* Avoid infinite recursion */
671 if (depth > 100)
672 {
673 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200674 ret = Py_None;
675 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200676 }
677
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200678 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200679 * then and we can use it again. */
680 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
681 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
682 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200683 sprintf(ptrBuf, "%p",
684 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
685 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200686
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200687 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200688 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200689 Py_INCREF(ret);
690 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200691 }
692 }
693
694 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200695 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200696 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200697 else if (our_tv->v_type == VAR_NUMBER)
698 {
699 char buf[NUMBUFLEN];
700
701 /* For backwards compatibility numbers are stored as strings. */
702 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200703 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200704 }
705# ifdef FEAT_FLOAT
706 else if (our_tv->v_type == VAR_FLOAT)
707 {
708 char buf[NUMBUFLEN];
709
710 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200711 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200712 }
713# endif
714 else if (our_tv->v_type == VAR_LIST)
715 {
716 list_T *list = our_tv->vval.v_list;
717 listitem_T *curr;
718
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200719 if (list == NULL)
720 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200721
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200722 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200723 return NULL;
724
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200725 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200726 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200727 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200728 return NULL;
729 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200730
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200731 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
732 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200733 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200734 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200735 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200736 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200737 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200738 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200739 {
740 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200741 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200742 return NULL;
743 }
744 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200745 }
746 }
747 else if (our_tv->v_type == VAR_DICT)
748 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200749
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200750 hashtab_T *ht = &our_tv->vval.v_dict->dv_hashtab;
751 long_u todo = ht->ht_used;
752 hashitem_T *hi;
753 dictitem_T *di;
754 if (our_tv->vval.v_dict == NULL)
755 return NULL;
756
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200757 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200758 return NULL;
759
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200760 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200761 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200762 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200763 return NULL;
764 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200765
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200766 for (hi = ht->ht_array; todo > 0; ++hi)
767 {
768 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200769 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200770 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200771
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200772 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200773 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200774 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200775 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200776 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200777 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200778 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200779 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200780 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200781 Py_DECREF(newObj);
782 return NULL;
783 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200784 }
785 }
786 }
787 else
788 {
789 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200790 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200791 }
792
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200793 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200794}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795
796 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200797VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200798{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200799 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200800 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200801 PyObject *string;
802 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200803 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200804 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200805
Bram Moolenaar389a1792013-06-23 13:00:44 +0200806 if (!PyArg_ParseTuple(args, "O", &string))
807 return NULL;
808
809 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810 return NULL;
811
812 Py_BEGIN_ALLOW_THREADS
813 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200814 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200815 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200816 Python_Release_Vim();
817 Py_END_ALLOW_THREADS
818
Bram Moolenaar389a1792013-06-23 13:00:44 +0200819 Py_XDECREF(todecref);
820
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200821 if (VimTryEnd())
822 return NULL;
823
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200824 if (our_tv == NULL)
825 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200826 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200827 return NULL;
828 }
829
830 /* Convert the Vim type into a Python type. Create a dictionary that's
831 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200832 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200833 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200834 else
835 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200836 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200837 Py_DECREF(lookup_dict);
838 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200839
840
841 Py_BEGIN_ALLOW_THREADS
842 Python_Lock_Vim();
843 free_tv(our_tv);
844 Python_Release_Vim();
845 Py_END_ALLOW_THREADS
846
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200847 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200848}
849
Bram Moolenaardb913952012-06-29 12:54:53 +0200850static PyObject *ConvertToPyObject(typval_T *);
851
852 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200853VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200854{
Bram Moolenaardb913952012-06-29 12:54:53 +0200855 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200856 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200857 char_u *expr;
858 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200859
Bram Moolenaar389a1792013-06-23 13:00:44 +0200860 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200861 return NULL;
862
863 Py_BEGIN_ALLOW_THREADS
864 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200865 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200866 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200867 Python_Release_Vim();
868 Py_END_ALLOW_THREADS
869
Bram Moolenaar389a1792013-06-23 13:00:44 +0200870 Py_XDECREF(todecref);
871
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200872 if (VimTryEnd())
873 return NULL;
874
Bram Moolenaardb913952012-06-29 12:54:53 +0200875 if (our_tv == NULL)
876 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200877 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200878 return NULL;
879 }
880
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200881 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200882 Py_BEGIN_ALLOW_THREADS
883 Python_Lock_Vim();
884 free_tv(our_tv);
885 Python_Release_Vim();
886 Py_END_ALLOW_THREADS
887
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200888 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200889}
890
891 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200892VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200893{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200894 char_u *str;
895 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200896 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200897
Bram Moolenaar389a1792013-06-23 13:00:44 +0200898 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200899 return NULL;
900
Bram Moolenaara54bf402012-12-05 16:30:07 +0100901#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200902 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100903#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200904 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100905#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200906
907 Py_XDECREF(todecref);
908
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200909 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200910}
911
Bram Moolenaarf4258302013-06-02 18:20:17 +0200912 static PyObject *
913_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
914{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200915 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200916 PyObject *newwd;
917 PyObject *todecref;
918 char_u *new_dir;
919
Bram Moolenaard4209d22013-06-05 20:34:15 +0200920 if (_chdir == NULL)
921 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200922 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200923 return NULL;
924
925 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
926 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200927 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200928 return NULL;
929 }
930
931 if (!(new_dir = StringToChars(newwd, &todecref)))
932 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200933 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200934 Py_DECREF(newwd);
935 return NULL;
936 }
937
938 VimTryStart();
939
940 if (vim_chdir(new_dir))
941 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200942 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200943 Py_DECREF(newwd);
944 Py_XDECREF(todecref);
945
946 if (VimTryEnd())
947 return NULL;
948
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200949 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200950 return NULL;
951 }
952
953 Py_DECREF(newwd);
954 Py_XDECREF(todecref);
955
956 post_chdir(FALSE);
957
958 if (VimTryEnd())
959 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200960 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200961 return NULL;
962 }
963
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200964 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200965}
966
967 static PyObject *
968VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
969{
970 return _VimChdir(py_chdir, args, kwargs);
971}
972
973 static PyObject *
974VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
975{
976 return _VimChdir(py_fchdir, args, kwargs);
977}
978
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200979typedef struct {
980 PyObject *callable;
981 PyObject *result;
982} map_rtp_data;
983
984 static void
985map_rtp_callback(char_u *path, void *_data)
986{
987 void **data = (void **) _data;
988 PyObject *pathObject;
989 map_rtp_data *mr_data = *((map_rtp_data **) data);
990
Bram Moolenaar41009372013-07-01 22:03:04 +0200991 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +0200992 {
993 *data = NULL;
994 return;
995 }
996
997 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
998 pathObject, NULL);
999
1000 Py_DECREF(pathObject);
1001
1002 if (!mr_data->result || mr_data->result != Py_None)
1003 *data = NULL;
1004 else
1005 {
1006 Py_DECREF(mr_data->result);
1007 mr_data->result = NULL;
1008 }
1009}
1010
1011 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001012VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001013{
1014 map_rtp_data data;
1015
Bram Moolenaar389a1792013-06-23 13:00:44 +02001016 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001017 data.result = NULL;
1018
1019 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
1020
1021 if (data.result == NULL)
1022 {
1023 if (PyErr_Occurred())
1024 return NULL;
1025 else
1026 {
1027 Py_INCREF(Py_None);
1028 return Py_None;
1029 }
1030 }
1031 return data.result;
1032}
1033
1034/*
1035 * _vim_runtimepath_ special path implementation.
1036 */
1037
1038 static void
1039map_finder_callback(char_u *path, void *_data)
1040{
1041 void **data = (void **) _data;
1042 PyObject *list = *((PyObject **) data);
1043 PyObject *pathObject1, *pathObject2;
1044 char *pathbuf;
1045 size_t pathlen;
1046
1047 pathlen = STRLEN(path);
1048
1049#if PY_MAJOR_VERSION < 3
1050# define PY_MAIN_DIR_STRING "python2"
1051#else
1052# define PY_MAIN_DIR_STRING "python3"
1053#endif
1054#define PY_ALTERNATE_DIR_STRING "pythonx"
1055
1056#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1057 if (!(pathbuf = PyMem_New(char,
1058 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1059 {
1060 PyErr_NoMemory();
1061 *data = NULL;
1062 return;
1063 }
1064
1065 mch_memmove(pathbuf, path, pathlen + 1);
1066 add_pathsep((char_u *) pathbuf);
1067
1068 pathlen = STRLEN(pathbuf);
1069 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1070 PYTHONX_STRING_LENGTH + 1);
1071
1072 if (!(pathObject1 = PyString_FromString(pathbuf)))
1073 {
1074 *data = NULL;
1075 PyMem_Free(pathbuf);
1076 return;
1077 }
1078
1079 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1080 PYTHONX_STRING_LENGTH + 1);
1081
1082 if (!(pathObject2 = PyString_FromString(pathbuf)))
1083 {
1084 Py_DECREF(pathObject1);
1085 PyMem_Free(pathbuf);
1086 *data = NULL;
1087 return;
1088 }
1089
1090 PyMem_Free(pathbuf);
1091
1092 if (PyList_Append(list, pathObject1)
1093 || PyList_Append(list, pathObject2))
1094 *data = NULL;
1095
1096 Py_DECREF(pathObject1);
1097 Py_DECREF(pathObject2);
1098}
1099
1100 static PyObject *
1101Vim_GetPaths(PyObject *self UNUSED)
1102{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001103 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001104
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001105 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001106 return NULL;
1107
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001108 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001109
1110 if (PyErr_Occurred())
1111 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001112 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001113 return NULL;
1114 }
1115
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001116 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001117}
1118
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001119 static PyObject *
1120call_load_module(char *name, int len, PyObject *find_module_result)
1121{
1122 PyObject *fd, *pathname, *description;
1123
Bram Moolenaarc476e522013-06-23 13:46:40 +02001124 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001125 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001126 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001127 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001128 Py_TYPE_NAME(find_module_result));
1129 return NULL;
1130 }
1131 if (PyTuple_GET_SIZE(find_module_result) != 3)
1132 {
1133 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001134 N_("expected 3-tuple as imp.find_module() result, but got "
1135 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001136 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001137 return NULL;
1138 }
1139
1140 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1141 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1142 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1143 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001144 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001145 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001146 return NULL;
1147 }
1148
1149 return PyObject_CallFunction(py_load_module,
1150 "s#OOO", name, len, fd, pathname, description);
1151}
1152
1153 static PyObject *
1154find_module(char *fullname, char *tail, PyObject *new_path)
1155{
1156 PyObject *find_module_result;
1157 PyObject *module;
1158 char *dot;
1159
Bram Moolenaar41009372013-07-01 22:03:04 +02001160 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001161 {
1162 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001163 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001164 * first component
1165 */
1166 PyObject *newest_path;
1167 int partlen = (int) (dot - 1 - tail);
1168
1169 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1170 "s#O", tail, partlen, new_path)))
1171 return NULL;
1172
1173 if (!(module = call_load_module(
1174 fullname,
1175 ((int) (tail - fullname)) + partlen,
1176 find_module_result)))
1177 {
1178 Py_DECREF(find_module_result);
1179 return NULL;
1180 }
1181
1182 Py_DECREF(find_module_result);
1183
1184 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1185 {
1186 Py_DECREF(module);
1187 return NULL;
1188 }
1189
1190 Py_DECREF(module);
1191
1192 module = find_module(fullname, dot + 1, newest_path);
1193
1194 Py_DECREF(newest_path);
1195
1196 return module;
1197 }
1198 else
1199 {
1200 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1201 "sO", tail, new_path)))
1202 return NULL;
1203
1204 if (!(module = call_load_module(
1205 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001206 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001207 find_module_result)))
1208 {
1209 Py_DECREF(find_module_result);
1210 return NULL;
1211 }
1212
1213 Py_DECREF(find_module_result);
1214
1215 return module;
1216 }
1217}
1218
1219 static PyObject *
1220FinderFindModule(PyObject *self, PyObject *args)
1221{
1222 char *fullname;
1223 PyObject *module;
1224 PyObject *new_path;
1225 LoaderObject *loader;
1226
1227 if (!PyArg_ParseTuple(args, "s", &fullname))
1228 return NULL;
1229
1230 if (!(new_path = Vim_GetPaths(self)))
1231 return NULL;
1232
1233 module = find_module(fullname, fullname, new_path);
1234
1235 Py_DECREF(new_path);
1236
1237 if (!module)
1238 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001239 if (PyErr_Occurred())
1240 {
1241 if (PyErr_ExceptionMatches(PyExc_ImportError))
1242 PyErr_Clear();
1243 else
1244 return NULL;
1245 }
1246
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001247 Py_INCREF(Py_None);
1248 return Py_None;
1249 }
1250
1251 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1252 {
1253 Py_DECREF(module);
1254 return NULL;
1255 }
1256
1257 loader->module = module;
1258
1259 return (PyObject *) loader;
1260}
1261
1262 static PyObject *
1263VimPathHook(PyObject *self UNUSED, PyObject *args)
1264{
1265 char *path;
1266
1267 if (PyArg_ParseTuple(args, "s", &path)
1268 && STRCMP(path, vim_special_path) == 0)
1269 {
1270 Py_INCREF(vim_module);
1271 return vim_module;
1272 }
1273
1274 PyErr_Clear();
1275 PyErr_SetNone(PyExc_ImportError);
1276 return NULL;
1277}
1278
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001279/*
1280 * Vim module - Definitions
1281 */
1282
1283static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001284 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001285 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001286 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001287 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1288 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001289 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1290 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001291 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001292 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001293 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1294 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1295 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001296};
1297
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001298/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001299 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001300 */
1301
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001302static PyTypeObject IterType;
1303
1304typedef PyObject *(*nextfun)(void **);
1305typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001306typedef int (*traversefun)(void *, visitproc, void *);
1307typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001308
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001309/* Main purpose of this object is removing the need for do python
1310 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1311 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001312
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001313typedef struct
1314{
1315 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001316 void *cur;
1317 nextfun next;
1318 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001319 traversefun traverse;
1320 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001321} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001322
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001323 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001324IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1325 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001326{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001327 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001328
Bram Moolenaar774267b2013-05-21 20:51:59 +02001329 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001330 self->cur = start;
1331 self->next = next;
1332 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001333 self->traverse = traverse;
1334 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001335
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001336 return (PyObject *)(self);
1337}
1338
1339 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001340IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001341{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001342 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001343 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001344 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001345}
1346
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001347 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001348IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001349{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001350 if (self->traverse != NULL)
1351 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001352 else
1353 return 0;
1354}
1355
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001356/* Mac OSX defines clear() somewhere. */
1357#ifdef clear
1358# undef clear
1359#endif
1360
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001361 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001362IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001363{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001364 if (self->clear != NULL)
1365 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001366 else
1367 return 0;
1368}
1369
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001370 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001371IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001372{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001373 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001374}
1375
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001376 static PyObject *
1377IterIter(PyObject *self)
1378{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001379 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001380 return self;
1381}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001382
Bram Moolenaardb913952012-06-29 12:54:53 +02001383typedef struct pylinkedlist_S {
1384 struct pylinkedlist_S *pll_next;
1385 struct pylinkedlist_S *pll_prev;
1386 PyObject *pll_obj;
1387} pylinkedlist_T;
1388
1389static pylinkedlist_T *lastdict = NULL;
1390static pylinkedlist_T *lastlist = NULL;
1391
1392 static void
1393pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1394{
1395 if (ref->pll_prev == NULL)
1396 {
1397 if (ref->pll_next == NULL)
1398 {
1399 *last = NULL;
1400 return;
1401 }
1402 }
1403 else
1404 ref->pll_prev->pll_next = ref->pll_next;
1405
1406 if (ref->pll_next == NULL)
1407 *last = ref->pll_prev;
1408 else
1409 ref->pll_next->pll_prev = ref->pll_prev;
1410}
1411
1412 static void
1413pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1414{
1415 if (*last == NULL)
1416 ref->pll_prev = NULL;
1417 else
1418 {
1419 (*last)->pll_next = ref;
1420 ref->pll_prev = *last;
1421 }
1422 ref->pll_next = NULL;
1423 ref->pll_obj = self;
1424 *last = ref;
1425}
1426
1427static PyTypeObject DictionaryType;
1428
1429typedef struct
1430{
1431 PyObject_HEAD
1432 dict_T *dict;
1433 pylinkedlist_T ref;
1434} DictionaryObject;
1435
Bram Moolenaara9922d62013-05-30 13:01:18 +02001436static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1437
1438#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1439
Bram Moolenaardb913952012-06-29 12:54:53 +02001440 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001441DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001442{
1443 DictionaryObject *self;
1444
Bram Moolenaara9922d62013-05-30 13:01:18 +02001445 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001446 if (self == NULL)
1447 return NULL;
1448 self->dict = dict;
1449 ++dict->dv_refcount;
1450
1451 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1452
1453 return (PyObject *)(self);
1454}
1455
Bram Moolenaara9922d62013-05-30 13:01:18 +02001456 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001457py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001458{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001459 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001460
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001461 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001462 {
1463 PyErr_NoMemory();
1464 return NULL;
1465 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001466 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001467
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001468 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001469}
1470
1471 static PyObject *
1472DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1473{
1474 DictionaryObject *self;
1475 dict_T *dict;
1476
1477 if (!(dict = py_dict_alloc()))
1478 return NULL;
1479
1480 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1481
1482 --dict->dv_refcount;
1483
1484 if (kwargs || PyTuple_Size(args))
1485 {
1486 PyObject *tmp;
1487 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1488 {
1489 Py_DECREF(self);
1490 return NULL;
1491 }
1492
1493 Py_DECREF(tmp);
1494 }
1495
1496 return (PyObject *)(self);
1497}
1498
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001499 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001500DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001501{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001502 pyll_remove(&self->ref, &lastdict);
1503 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001504
1505 DESTRUCTOR_FINISH(self);
1506}
1507
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001508static char *DictionaryAttrs[] = {
1509 "locked", "scope",
1510 NULL
1511};
1512
1513 static PyObject *
1514DictionaryDir(PyObject *self)
1515{
1516 return ObjectDir(self, DictionaryAttrs);
1517}
1518
Bram Moolenaardb913952012-06-29 12:54:53 +02001519 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001520DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001521{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001522 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001523 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001524 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001525 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001526 return -1;
1527 }
1528
1529 if (strcmp(name, "locked") == 0)
1530 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001531 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001532 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001533 PyErr_SET_STRING(PyExc_TypeError,
1534 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001535 return -1;
1536 }
1537 else
1538 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001539 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001540 if (istrue == -1)
1541 return -1;
1542 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001543 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001544 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001545 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001546 }
1547 return 0;
1548 }
1549 else
1550 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001551 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001552 return -1;
1553 }
1554}
1555
1556 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001557DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001558{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001559 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001560}
1561
Bram Moolenaara9922d62013-05-30 13:01:18 +02001562#define DICT_FLAG_HAS_DEFAULT 0x01
1563#define DICT_FLAG_POP 0x02
1564#define DICT_FLAG_NONE_DEFAULT 0x04
1565#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1566#define DICT_FLAG_RETURN_PAIR 0x10
1567
Bram Moolenaardb913952012-06-29 12:54:53 +02001568 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001569_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001570{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001571 PyObject *keyObject;
1572 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001573 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001574 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001575 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001576 dict_T *dict = self->dict;
1577 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001578 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001579
Bram Moolenaara9922d62013-05-30 13:01:18 +02001580 if (flags & DICT_FLAG_HAS_DEFAULT)
1581 {
1582 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1583 return NULL;
1584 }
1585 else
1586 keyObject = args;
1587
1588 if (flags & DICT_FLAG_RETURN_BOOL)
1589 defObject = Py_False;
1590
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001591 if (!(key = StringToChars(keyObject, &todecref)))
1592 return NULL;
1593
1594 if (*key == NUL)
1595 {
1596 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001597 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001598 return NULL;
1599 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001600
Bram Moolenaara9922d62013-05-30 13:01:18 +02001601 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001602
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001603 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001604
Bram Moolenaara9922d62013-05-30 13:01:18 +02001605 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001606 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001607 if (defObject)
1608 {
1609 Py_INCREF(defObject);
1610 return defObject;
1611 }
1612 else
1613 {
1614 PyErr_SetObject(PyExc_KeyError, keyObject);
1615 return NULL;
1616 }
1617 }
1618 else if (flags & DICT_FLAG_RETURN_BOOL)
1619 {
1620 Py_INCREF(Py_True);
1621 return Py_True;
1622 }
1623
1624 di = dict_lookup(hi);
1625
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001626 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001627 return NULL;
1628
1629 if (flags & DICT_FLAG_POP)
1630 {
1631 if (dict->dv_lock)
1632 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001633 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001634 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001635 return NULL;
1636 }
1637
1638 hash_remove(&dict->dv_hashtab, hi);
1639 dictitem_free(di);
1640 }
1641
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001642 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001643}
1644
1645 static PyObject *
1646DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1647{
1648 return _DictionaryItem(self, keyObject, 0);
1649}
1650
1651 static int
1652DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1653{
1654 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001655 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001656
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001657 if (rObj == NULL)
1658 return -1;
1659
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001660 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001661
Bram Moolenaardee2e312013-06-23 16:35:47 +02001662 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001663
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001664 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001665}
1666
1667typedef struct
1668{
1669 hashitem_T *ht_array;
1670 long_u ht_used;
1671 hashtab_T *ht;
1672 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001673 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001674} dictiterinfo_T;
1675
1676 static PyObject *
1677DictionaryIterNext(dictiterinfo_T **dii)
1678{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001679 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001680
1681 if (!(*dii)->todo)
1682 return NULL;
1683
1684 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1685 (*dii)->ht->ht_used != (*dii)->ht_used)
1686 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001687 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001688 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001689 return NULL;
1690 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001691
Bram Moolenaara9922d62013-05-30 13:01:18 +02001692 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1693 ++((*dii)->hi);
1694
1695 --((*dii)->todo);
1696
Bram Moolenaar41009372013-07-01 22:03:04 +02001697 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001698 return NULL;
1699
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001700 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001701}
1702
1703 static PyObject *
1704DictionaryIter(DictionaryObject *self)
1705{
1706 dictiterinfo_T *dii;
1707 hashtab_T *ht;
1708
1709 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1710 {
1711 PyErr_NoMemory();
1712 return NULL;
1713 }
1714
1715 ht = &self->dict->dv_hashtab;
1716 dii->ht_array = ht->ht_array;
1717 dii->ht_used = ht->ht_used;
1718 dii->ht = ht;
1719 dii->hi = dii->ht_array;
1720 dii->todo = dii->ht_used;
1721
1722 return IterNew(dii,
1723 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1724 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001725}
1726
1727 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001728DictionaryAssItem(
1729 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001730{
1731 char_u *key;
1732 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001733 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001734 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001735 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001736
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001737 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001738 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001739 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001740 return -1;
1741 }
1742
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001743 if (!(key = StringToChars(keyObject, &todecref)))
1744 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001745
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001746 if (*key == NUL)
1747 {
1748 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001749 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001750 return -1;
1751 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001752
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001753 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001754
1755 if (valObject == NULL)
1756 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001757 hashitem_T *hi;
1758
Bram Moolenaardb913952012-06-29 12:54:53 +02001759 if (di == NULL)
1760 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001761 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001762 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001763 return -1;
1764 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001765 hi = hash_find(&dict->dv_hashtab, di->di_key);
1766 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001767 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001768 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001769 return 0;
1770 }
1771
1772 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001773 {
1774 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001775 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001776 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001777
1778 if (di == NULL)
1779 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001780 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001781 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001782 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001783 PyErr_NoMemory();
1784 return -1;
1785 }
1786 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001787 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001788
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001789 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001790 {
1791 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001792 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001793 RAISE_KEY_ADD_FAIL(key);
1794 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001795 return -1;
1796 }
1797 }
1798 else
1799 clear_tv(&di->di_tv);
1800
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001801 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001802
1803 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001804 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001805 return 0;
1806}
1807
Bram Moolenaara9922d62013-05-30 13:01:18 +02001808typedef PyObject *(*hi_to_py)(hashitem_T *);
1809
Bram Moolenaardb913952012-06-29 12:54:53 +02001810 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001811DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001812{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001813 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001814 long_u todo = dict->dv_hashtab.ht_used;
1815 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001816 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001817 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001818 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001819
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001820 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001821 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1822 {
1823 if (!HASHITEM_EMPTY(hi))
1824 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001825 if (!(newObj = hiconvert(hi)))
1826 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001827 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001828 return NULL;
1829 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001830 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001831 --todo;
1832 ++i;
1833 }
1834 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001835 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001836}
1837
Bram Moolenaara9922d62013-05-30 13:01:18 +02001838 static PyObject *
1839dict_key(hashitem_T *hi)
1840{
1841 return PyBytes_FromString((char *)(hi->hi_key));
1842}
1843
1844 static PyObject *
1845DictionaryListKeys(DictionaryObject *self)
1846{
1847 return DictionaryListObjects(self, dict_key);
1848}
1849
1850 static PyObject *
1851dict_val(hashitem_T *hi)
1852{
1853 dictitem_T *di;
1854
1855 di = dict_lookup(hi);
1856 return ConvertToPyObject(&di->di_tv);
1857}
1858
1859 static PyObject *
1860DictionaryListValues(DictionaryObject *self)
1861{
1862 return DictionaryListObjects(self, dict_val);
1863}
1864
1865 static PyObject *
1866dict_item(hashitem_T *hi)
1867{
1868 PyObject *keyObject;
1869 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001870 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001871
1872 if (!(keyObject = dict_key(hi)))
1873 return NULL;
1874
1875 if (!(valObject = dict_val(hi)))
1876 {
1877 Py_DECREF(keyObject);
1878 return NULL;
1879 }
1880
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001881 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001882
1883 Py_DECREF(keyObject);
1884 Py_DECREF(valObject);
1885
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001886 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001887}
1888
1889 static PyObject *
1890DictionaryListItems(DictionaryObject *self)
1891{
1892 return DictionaryListObjects(self, dict_item);
1893}
1894
1895 static PyObject *
1896DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1897{
1898 dict_T *dict = self->dict;
1899
1900 if (dict->dv_lock)
1901 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001902 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001903 return NULL;
1904 }
1905
1906 if (kwargs)
1907 {
1908 typval_T tv;
1909
1910 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1911 return NULL;
1912
1913 VimTryStart();
1914 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1915 clear_tv(&tv);
1916 if (VimTryEnd())
1917 return NULL;
1918 }
1919 else
1920 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001921 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001922
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001923 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001924 return NULL;
1925
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001926 if (obj == NULL)
1927 {
1928 Py_INCREF(Py_None);
1929 return Py_None;
1930 }
1931
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001932 if (PyObject_HasAttrString(obj, "keys"))
1933 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001934 else
1935 {
1936 PyObject *iterator;
1937 PyObject *item;
1938
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001939 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001940 return NULL;
1941
1942 while ((item = PyIter_Next(iterator)))
1943 {
1944 PyObject *fast;
1945 PyObject *keyObject;
1946 PyObject *valObject;
1947 PyObject *todecref;
1948 char_u *key;
1949 dictitem_T *di;
1950
1951 if (!(fast = PySequence_Fast(item, "")))
1952 {
1953 Py_DECREF(iterator);
1954 Py_DECREF(item);
1955 return NULL;
1956 }
1957
1958 Py_DECREF(item);
1959
1960 if (PySequence_Fast_GET_SIZE(fast) != 2)
1961 {
1962 Py_DECREF(iterator);
1963 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001964 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001965 N_("expected sequence element of size 2, "
1966 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001967 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001968 return NULL;
1969 }
1970
1971 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1972
1973 if (!(key = StringToChars(keyObject, &todecref)))
1974 {
1975 Py_DECREF(iterator);
1976 Py_DECREF(fast);
1977 return NULL;
1978 }
1979
1980 di = dictitem_alloc(key);
1981
1982 Py_XDECREF(todecref);
1983
1984 if (di == NULL)
1985 {
1986 Py_DECREF(fast);
1987 Py_DECREF(iterator);
1988 PyErr_NoMemory();
1989 return NULL;
1990 }
1991 di->di_tv.v_lock = 0;
1992 di->di_tv.v_type = VAR_UNKNOWN;
1993
1994 valObject = PySequence_Fast_GET_ITEM(fast, 1);
1995
1996 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
1997 {
1998 Py_DECREF(iterator);
1999 Py_DECREF(fast);
2000 dictitem_free(di);
2001 return NULL;
2002 }
2003
2004 Py_DECREF(fast);
2005
2006 if (dict_add(dict, di) == FAIL)
2007 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002008 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002009 Py_DECREF(iterator);
2010 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002011 return NULL;
2012 }
2013 }
2014
2015 Py_DECREF(iterator);
2016
2017 /* Iterator may have finished due to an exception */
2018 if (PyErr_Occurred())
2019 return NULL;
2020 }
2021 }
2022 Py_INCREF(Py_None);
2023 return Py_None;
2024}
2025
2026 static PyObject *
2027DictionaryGet(DictionaryObject *self, PyObject *args)
2028{
2029 return _DictionaryItem(self, args,
2030 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2031}
2032
2033 static PyObject *
2034DictionaryPop(DictionaryObject *self, PyObject *args)
2035{
2036 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2037}
2038
2039 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002040DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002041{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002042 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002043 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002044 PyObject *valObject;
2045 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002046
Bram Moolenaarde71b562013-06-02 17:41:54 +02002047 if (self->dict->dv_hashtab.ht_used == 0)
2048 {
2049 PyErr_SetNone(PyExc_KeyError);
2050 return NULL;
2051 }
2052
2053 hi = self->dict->dv_hashtab.ht_array;
2054 while (HASHITEM_EMPTY(hi))
2055 ++hi;
2056
2057 di = dict_lookup(hi);
2058
2059 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002060 return NULL;
2061
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002062 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002063 {
2064 Py_DECREF(valObject);
2065 return NULL;
2066 }
2067
2068 hash_remove(&self->dict->dv_hashtab, hi);
2069 dictitem_free(di);
2070
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002071 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002072}
2073
2074 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002075DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002076{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002077 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2078}
2079
2080static PySequenceMethods DictionaryAsSeq = {
2081 0, /* sq_length */
2082 0, /* sq_concat */
2083 0, /* sq_repeat */
2084 0, /* sq_item */
2085 0, /* sq_slice */
2086 0, /* sq_ass_item */
2087 0, /* sq_ass_slice */
2088 (objobjproc) DictionaryContains, /* sq_contains */
2089 0, /* sq_inplace_concat */
2090 0, /* sq_inplace_repeat */
2091};
2092
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002093static PyMappingMethods DictionaryAsMapping = {
2094 (lenfunc) DictionaryLength,
2095 (binaryfunc) DictionaryItem,
2096 (objobjargproc) DictionaryAssItem,
2097};
2098
Bram Moolenaardb913952012-06-29 12:54:53 +02002099static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002100 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002101 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2102 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2103 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2104 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2105 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002106 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002107 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002108 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2109 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002110};
2111
2112static PyTypeObject ListType;
2113
2114typedef struct
2115{
2116 PyObject_HEAD
2117 list_T *list;
2118 pylinkedlist_T ref;
2119} ListObject;
2120
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002121#define NEW_LIST(list) ListNew(&ListType, list)
2122
Bram Moolenaardb913952012-06-29 12:54:53 +02002123 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002124ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002125{
2126 ListObject *self;
2127
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002128 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002129 if (self == NULL)
2130 return NULL;
2131 self->list = list;
2132 ++list->lv_refcount;
2133
2134 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2135
2136 return (PyObject *)(self);
2137}
2138
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002139 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002140py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002141{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002142 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002143
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002144 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002145 {
2146 PyErr_NoMemory();
2147 return NULL;
2148 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002149 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002150
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002151 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002152}
2153
2154 static int
2155list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2156{
2157 PyObject *iterator;
2158 PyObject *item;
2159 listitem_T *li;
2160
2161 if (!(iterator = PyObject_GetIter(obj)))
2162 return -1;
2163
2164 while ((item = PyIter_Next(iterator)))
2165 {
2166 if (!(li = listitem_alloc()))
2167 {
2168 PyErr_NoMemory();
2169 Py_DECREF(item);
2170 Py_DECREF(iterator);
2171 return -1;
2172 }
2173 li->li_tv.v_lock = 0;
2174 li->li_tv.v_type = VAR_UNKNOWN;
2175
2176 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2177 {
2178 Py_DECREF(item);
2179 Py_DECREF(iterator);
2180 listitem_free(li);
2181 return -1;
2182 }
2183
2184 Py_DECREF(item);
2185
2186 list_append(l, li);
2187 }
2188
2189 Py_DECREF(iterator);
2190
2191 /* Iterator may have finished due to an exception */
2192 if (PyErr_Occurred())
2193 return -1;
2194
2195 return 0;
2196}
2197
2198 static PyObject *
2199ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2200{
2201 list_T *list;
2202 PyObject *obj = NULL;
2203
2204 if (kwargs)
2205 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002206 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002207 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002208 return NULL;
2209 }
2210
2211 if (!PyArg_ParseTuple(args, "|O", &obj))
2212 return NULL;
2213
2214 if (!(list = py_list_alloc()))
2215 return NULL;
2216
2217 if (obj)
2218 {
2219 PyObject *lookup_dict;
2220
2221 if (!(lookup_dict = PyDict_New()))
2222 {
2223 list_unref(list);
2224 return NULL;
2225 }
2226
2227 if (list_py_concat(list, obj, lookup_dict) == -1)
2228 {
2229 Py_DECREF(lookup_dict);
2230 list_unref(list);
2231 return NULL;
2232 }
2233
2234 Py_DECREF(lookup_dict);
2235 }
2236
2237 return ListNew(subtype, list);
2238}
2239
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002240 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002241ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002242{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002243 pyll_remove(&self->ref, &lastlist);
2244 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002245
2246 DESTRUCTOR_FINISH(self);
2247}
2248
Bram Moolenaardb913952012-06-29 12:54:53 +02002249 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002250ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002251{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002252 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002253}
2254
2255 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002256ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002257{
2258 listitem_T *li;
2259
Bram Moolenaard6e39182013-05-21 18:30:34 +02002260 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002261 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002262 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002263 return NULL;
2264 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002265 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002266 if (li == NULL)
2267 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002268 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002269 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002270 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002271 return NULL;
2272 }
2273 return ConvertToPyObject(&li->li_tv);
2274}
2275
Bram Moolenaardb913952012-06-29 12:54:53 +02002276 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002277ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2278 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002279{
2280 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002281 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002282
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002283 if (step == 0)
2284 {
2285 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2286 return NULL;
2287 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002288
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002289 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002290 if (list == NULL)
2291 return NULL;
2292
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002293 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002294 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002295 PyObject *item;
2296
2297 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002298 if (item == NULL)
2299 {
2300 Py_DECREF(list);
2301 return NULL;
2302 }
2303
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002304 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002305 }
2306
2307 return list;
2308}
2309
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002310 static PyObject *
2311ListItem(ListObject *self, PyObject* idx)
2312{
2313#if PY_MAJOR_VERSION < 3
2314 if (PyInt_Check(idx))
2315 {
2316 long _idx = PyInt_AsLong(idx);
2317 return ListIndex(self, _idx);
2318 }
2319 else
2320#endif
2321 if (PyLong_Check(idx))
2322 {
2323 long _idx = PyLong_AsLong(idx);
2324 return ListIndex(self, _idx);
2325 }
2326 else if (PySlice_Check(idx))
2327 {
2328 Py_ssize_t start, stop, step, slicelen;
2329
Bram Moolenaar5395e7a2014-01-14 19:35:56 +01002330 if (PySlice_GetIndicesEx((PySliceObject *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002331 &start, &stop, &step, &slicelen) < 0)
2332 return NULL;
2333 return ListSlice(self, start, step, slicelen);
2334 }
2335 else
2336 {
2337 RAISE_INVALID_INDEX_TYPE(idx);
2338 return NULL;
2339 }
2340}
2341
2342 static void
2343list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2344 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2345{
2346 while (numreplaced--)
2347 {
2348 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2349 listitem_remove(l, lis[slicelen + numreplaced]);
2350 }
2351 while (numadded--)
2352 {
2353 listitem_T *next;
2354
2355 next = lastaddedli->li_prev;
2356 listitem_remove(l, lastaddedli);
2357 lastaddedli = next;
2358 }
2359}
2360
2361 static int
2362ListAssSlice(ListObject *self, Py_ssize_t first,
2363 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2364{
2365 PyObject *iterator;
2366 PyObject *item;
2367 listitem_T *li;
2368 listitem_T *lastaddedli = NULL;
2369 listitem_T *next;
2370 typval_T v;
2371 list_T *l = self->list;
2372 PyInt i;
2373 PyInt j;
2374 PyInt numreplaced = 0;
2375 PyInt numadded = 0;
2376 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002377 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002378
2379 size = ListLength(self);
2380
2381 if (l->lv_lock)
2382 {
2383 RAISE_LOCKED_LIST;
2384 return -1;
2385 }
2386
2387 if (step == 0)
2388 {
2389 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2390 return -1;
2391 }
2392
2393 if (step != 1 && slicelen == 0)
2394 {
2395 /* Nothing to do. Only error out if obj has some items. */
2396 int ret = 0;
2397
2398 if (obj == NULL)
2399 return 0;
2400
2401 if (!(iterator = PyObject_GetIter(obj)))
2402 return -1;
2403
2404 if ((item = PyIter_Next(iterator)))
2405 {
2406 PyErr_FORMAT(PyExc_ValueError,
2407 N_("attempt to assign sequence of size greater then %d "
2408 "to extended slice"), 0);
2409 Py_DECREF(item);
2410 ret = -1;
2411 }
2412 Py_DECREF(iterator);
2413 return ret;
2414 }
2415
2416 if (obj != NULL)
2417 /* XXX May allocate zero bytes. */
2418 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2419 {
2420 PyErr_NoMemory();
2421 return -1;
2422 }
2423
2424 if (first == size)
2425 li = NULL;
2426 else
2427 {
2428 li = list_find(l, (long) first);
2429 if (li == NULL)
2430 {
2431 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2432 (int)first);
2433 if (obj != NULL)
2434 PyMem_Free(lis);
2435 return -1;
2436 }
2437 i = slicelen;
2438 while (i-- && li != NULL)
2439 {
2440 j = step;
2441 next = li;
2442 if (step > 0)
2443 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2444 else
2445 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2446
2447 if (obj == NULL)
2448 listitem_remove(l, li);
2449 else
2450 lis[slicelen - i - 1] = li;
2451
2452 li = next;
2453 }
2454 if (li == NULL && i != -1)
2455 {
2456 PyErr_SET_VIM(N_("internal error: not enough list items"));
2457 if (obj != NULL)
2458 PyMem_Free(lis);
2459 return -1;
2460 }
2461 }
2462
2463 if (obj == NULL)
2464 return 0;
2465
2466 if (!(iterator = PyObject_GetIter(obj)))
2467 {
2468 PyMem_Free(lis);
2469 return -1;
2470 }
2471
2472 i = 0;
2473 while ((item = PyIter_Next(iterator)))
2474 {
2475 if (ConvertFromPyObject(item, &v) == -1)
2476 {
2477 Py_DECREF(iterator);
2478 Py_DECREF(item);
2479 PyMem_Free(lis);
2480 return -1;
2481 }
2482 Py_DECREF(item);
2483 if (list_insert_tv(l, &v, numreplaced < slicelen
2484 ? lis[numreplaced]
2485 : li) == FAIL)
2486 {
2487 clear_tv(&v);
2488 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2489 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2490 PyMem_Free(lis);
2491 return -1;
2492 }
2493 if (numreplaced < slicelen)
2494 {
2495 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
2496 list_remove(l, lis[numreplaced], lis[numreplaced]);
2497 numreplaced++;
2498 }
2499 else
2500 {
2501 if (li)
2502 lastaddedli = li->li_prev;
2503 else
2504 lastaddedli = l->lv_last;
2505 numadded++;
2506 }
2507 clear_tv(&v);
2508 if (step != 1 && i >= slicelen)
2509 {
2510 Py_DECREF(iterator);
2511 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar3b522612014-02-11 16:00:35 +01002512 N_("attempt to assign sequence of size greater then %ld "
2513 "to extended slice"), (long)slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002514 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2515 PyMem_Free(lis);
2516 return -1;
2517 }
2518 ++i;
2519 }
2520 Py_DECREF(iterator);
2521
2522 if (step != 1 && i != slicelen)
2523 {
2524 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar3b522612014-02-11 16:00:35 +01002525 N_("attempt to assign sequence of size %ld to extended slice "
2526 "of size %ld"), (long)i, (long)slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002527 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2528 PyMem_Free(lis);
2529 return -1;
2530 }
2531
2532 if (PyErr_Occurred())
2533 {
2534 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2535 PyMem_Free(lis);
2536 return -1;
2537 }
2538
2539 for (i = 0; i < numreplaced; i++)
2540 listitem_free(lis[i]);
2541 if (step == 1)
2542 for (i = numreplaced; i < slicelen; i++)
2543 listitem_remove(l, lis[i]);
2544
2545 PyMem_Free(lis);
2546
2547 return 0;
2548}
2549
2550 static int
2551ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2552{
2553 typval_T tv;
2554 list_T *l = self->list;
2555 listitem_T *li;
2556 Py_ssize_t length = ListLength(self);
2557
2558 if (l->lv_lock)
2559 {
2560 RAISE_LOCKED_LIST;
2561 return -1;
2562 }
2563 if (index > length || (index == length && obj == NULL))
2564 {
2565 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2566 return -1;
2567 }
2568
2569 if (obj == NULL)
2570 {
2571 li = list_find(l, (long) index);
2572 list_remove(l, li, li);
2573 clear_tv(&li->li_tv);
2574 vim_free(li);
2575 return 0;
2576 }
2577
2578 if (ConvertFromPyObject(obj, &tv) == -1)
2579 return -1;
2580
2581 if (index == length)
2582 {
2583 if (list_append_tv(l, &tv) == FAIL)
2584 {
2585 clear_tv(&tv);
2586 PyErr_SET_VIM(N_("failed to add item to list"));
2587 return -1;
2588 }
2589 }
2590 else
2591 {
2592 li = list_find(l, (long) index);
2593 clear_tv(&li->li_tv);
2594 copy_tv(&tv, &li->li_tv);
2595 clear_tv(&tv);
2596 }
2597 return 0;
2598}
2599
2600 static Py_ssize_t
2601ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2602{
2603#if PY_MAJOR_VERSION < 3
2604 if (PyInt_Check(idx))
2605 {
2606 long _idx = PyInt_AsLong(idx);
2607 return ListAssIndex(self, _idx, obj);
2608 }
2609 else
2610#endif
2611 if (PyLong_Check(idx))
2612 {
2613 long _idx = PyLong_AsLong(idx);
2614 return ListAssIndex(self, _idx, obj);
2615 }
2616 else if (PySlice_Check(idx))
2617 {
2618 Py_ssize_t start, stop, step, slicelen;
2619
Bram Moolenaar5395e7a2014-01-14 19:35:56 +01002620 if (PySlice_GetIndicesEx((PySliceObject *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002621 &start, &stop, &step, &slicelen) < 0)
2622 return -1;
2623 return ListAssSlice(self, start, step, slicelen,
2624 obj);
2625 }
2626 else
2627 {
2628 RAISE_INVALID_INDEX_TYPE(idx);
2629 return -1;
2630 }
2631}
2632
2633 static PyObject *
2634ListConcatInPlace(ListObject *self, PyObject *obj)
2635{
2636 list_T *l = self->list;
2637 PyObject *lookup_dict;
2638
2639 if (l->lv_lock)
2640 {
2641 RAISE_LOCKED_LIST;
2642 return NULL;
2643 }
2644
2645 if (!(lookup_dict = PyDict_New()))
2646 return NULL;
2647
2648 if (list_py_concat(l, obj, lookup_dict) == -1)
2649 {
2650 Py_DECREF(lookup_dict);
2651 return NULL;
2652 }
2653 Py_DECREF(lookup_dict);
2654
2655 Py_INCREF(self);
2656 return (PyObject *)(self);
2657}
2658
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002659typedef struct
2660{
2661 listwatch_T lw;
2662 list_T *list;
2663} listiterinfo_T;
2664
2665 static void
2666ListIterDestruct(listiterinfo_T *lii)
2667{
2668 list_rem_watch(lii->list, &lii->lw);
2669 PyMem_Free(lii);
2670}
2671
2672 static PyObject *
2673ListIterNext(listiterinfo_T **lii)
2674{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002675 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002676
2677 if (!((*lii)->lw.lw_item))
2678 return NULL;
2679
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002680 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002681 return NULL;
2682
2683 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2684
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002685 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002686}
2687
2688 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002689ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002690{
2691 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002692 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002693
2694 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2695 {
2696 PyErr_NoMemory();
2697 return NULL;
2698 }
2699
2700 list_add_watch(l, &lii->lw);
2701 lii->lw.lw_item = l->lv_first;
2702 lii->list = l;
2703
2704 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002705 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2706 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002707}
2708
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002709static char *ListAttrs[] = {
2710 "locked",
2711 NULL
2712};
2713
2714 static PyObject *
2715ListDir(PyObject *self)
2716{
2717 return ObjectDir(self, ListAttrs);
2718}
2719
Bram Moolenaar66b79852012-09-21 14:00:35 +02002720 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002721ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002722{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002723 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002724 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002725 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002726 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002727 return -1;
2728 }
2729
2730 if (strcmp(name, "locked") == 0)
2731 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002732 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002733 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002734 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002735 return -1;
2736 }
2737 else
2738 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002739 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002740 if (istrue == -1)
2741 return -1;
2742 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002743 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002744 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002745 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002746 }
2747 return 0;
2748 }
2749 else
2750 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002751 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002752 return -1;
2753 }
2754}
2755
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002756static PySequenceMethods ListAsSeq = {
2757 (lenfunc) ListLength, /* sq_length, len(x) */
2758 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2759 0, /* RangeRepeat, sq_repeat, x*n */
2760 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2761 0, /* was_sq_slice, x[i:j] */
2762 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2763 0, /* was_sq_ass_slice, x[i:j]=v */
2764 0, /* sq_contains */
2765 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2766 0, /* sq_inplace_repeat */
2767};
2768
2769static PyMappingMethods ListAsMapping = {
2770 /* mp_length */ (lenfunc) ListLength,
2771 /* mp_subscript */ (binaryfunc) ListItem,
2772 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2773};
2774
Bram Moolenaardb913952012-06-29 12:54:53 +02002775static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002776 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2777 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2778 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002779};
2780
2781typedef struct
2782{
2783 PyObject_HEAD
2784 char_u *name;
2785} FunctionObject;
2786
2787static PyTypeObject FunctionType;
2788
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002789#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2790
Bram Moolenaardb913952012-06-29 12:54:53 +02002791 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002792FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002793{
2794 FunctionObject *self;
2795
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002796 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2797
Bram Moolenaardb913952012-06-29 12:54:53 +02002798 if (self == NULL)
2799 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002800
2801 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002802 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002803 if (!translated_function_exists(name))
2804 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002805 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002806 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002807 return NULL;
2808 }
2809 self->name = vim_strsave(name);
2810 func_ref(self->name);
2811 }
2812 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002813 if ((self->name = get_expanded_name(name,
2814 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2815 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002816 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002817 PyErr_FORMAT(PyExc_ValueError,
2818 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002819 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002820 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002821
2822 return (PyObject *)(self);
2823}
2824
2825 static PyObject *
2826FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2827{
2828 PyObject *self;
2829 char_u *name;
2830
2831 if (kwargs)
2832 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002833 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002834 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002835 return NULL;
2836 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002837
Bram Moolenaar389a1792013-06-23 13:00:44 +02002838 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002839 return NULL;
2840
2841 self = FunctionNew(subtype, name);
2842
Bram Moolenaar389a1792013-06-23 13:00:44 +02002843 PyMem_Free(name);
2844
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002845 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002846}
2847
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002848 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002849FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002850{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002851 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002852 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002853
2854 DESTRUCTOR_FINISH(self);
2855}
2856
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002857static char *FunctionAttrs[] = {
2858 "softspace",
2859 NULL
2860};
2861
2862 static PyObject *
2863FunctionDir(PyObject *self)
2864{
2865 return ObjectDir(self, FunctionAttrs);
2866}
2867
Bram Moolenaardb913952012-06-29 12:54:53 +02002868 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002869FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002870{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002871 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002872 typval_T args;
2873 typval_T selfdicttv;
2874 typval_T rettv;
2875 dict_T *selfdict = NULL;
2876 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002877 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002878 int error;
2879
2880 if (ConvertFromPyObject(argsObject, &args) == -1)
2881 return NULL;
2882
2883 if (kwargs != NULL)
2884 {
2885 selfdictObject = PyDict_GetItemString(kwargs, "self");
2886 if (selfdictObject != NULL)
2887 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002888 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002889 {
2890 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002891 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002892 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002893 selfdict = selfdicttv.vval.v_dict;
2894 }
2895 }
2896
Bram Moolenaar71700b82013-05-15 17:49:05 +02002897 Py_BEGIN_ALLOW_THREADS
2898 Python_Lock_Vim();
2899
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002900 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002901 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002902
2903 Python_Release_Vim();
2904 Py_END_ALLOW_THREADS
2905
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002906 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002907 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002908 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002909 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002910 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002911 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002912 }
2913 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002914 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002915
Bram Moolenaardb913952012-06-29 12:54:53 +02002916 clear_tv(&args);
2917 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002918 if (selfdict != NULL)
2919 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002920
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002921 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002922}
2923
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002924 static PyObject *
2925FunctionRepr(FunctionObject *self)
2926{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002927#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002928 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002929 * Finalize */
2930 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002931 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002932#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002933 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002934#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002935}
2936
Bram Moolenaardb913952012-06-29 12:54:53 +02002937static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002938 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2939 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002940};
2941
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002942/*
2943 * Options object
2944 */
2945
2946static PyTypeObject OptionsType;
2947
2948typedef int (*checkfun)(void *);
2949
2950typedef struct
2951{
2952 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01002953 int opt_type;
2954 void *from;
2955 checkfun Check;
2956 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002957} OptionsObject;
2958
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002959 static int
2960dummy_check(void *arg UNUSED)
2961{
2962 return 0;
2963}
2964
2965 static PyObject *
2966OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2967{
2968 OptionsObject *self;
2969
Bram Moolenaar774267b2013-05-21 20:51:59 +02002970 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002971 if (self == NULL)
2972 return NULL;
2973
2974 self->opt_type = opt_type;
2975 self->from = from;
2976 self->Check = Check;
2977 self->fromObj = fromObj;
2978 if (fromObj)
2979 Py_INCREF(fromObj);
2980
2981 return (PyObject *)(self);
2982}
2983
2984 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002985OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002986{
Bram Moolenaar774267b2013-05-21 20:51:59 +02002987 PyObject_GC_UnTrack((void *)(self));
2988 Py_XDECREF(self->fromObj);
2989 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002990}
2991
2992 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02002993OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002994{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002995 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002996 return 0;
2997}
2998
2999 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003000OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003001{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003002 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003003 return 0;
3004}
3005
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003006 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003007OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003008{
3009 char_u *key;
3010 int flags;
3011 long numval;
3012 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003013 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003014
Bram Moolenaard6e39182013-05-21 18:30:34 +02003015 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003016 return NULL;
3017
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003018 if (!(key = StringToChars(keyObject, &todecref)))
3019 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003020
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003021 if (*key == NUL)
3022 {
3023 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003024 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003025 return NULL;
3026 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003027
3028 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003029 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003030
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003031 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003032
3033 if (flags == 0)
3034 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003035 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003036 return NULL;
3037 }
3038
3039 if (flags & SOPT_UNSET)
3040 {
3041 Py_INCREF(Py_None);
3042 return Py_None;
3043 }
3044 else if (flags & SOPT_BOOL)
3045 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003046 PyObject *ret;
3047 ret = numval ? Py_True : Py_False;
3048 Py_INCREF(ret);
3049 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003050 }
3051 else if (flags & SOPT_NUM)
3052 return PyInt_FromLong(numval);
3053 else if (flags & SOPT_STRING)
3054 {
3055 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003056 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003057 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003058 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003059 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003060 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003061 else
3062 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003063 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003064 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003065 return NULL;
3066 }
3067 }
3068 else
3069 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003070 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003071 return NULL;
3072 }
3073}
3074
3075 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003076OptionsContains(OptionsObject *self, PyObject *keyObject)
3077{
3078 char_u *key;
3079 PyObject *todecref;
3080
3081 if (!(key = StringToChars(keyObject, &todecref)))
3082 return -1;
3083
3084 if (*key == NUL)
3085 {
3086 Py_XDECREF(todecref);
3087 return 0;
3088 }
3089
3090 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3091 {
3092 Py_XDECREF(todecref);
3093 return 1;
3094 }
3095 else
3096 {
3097 Py_XDECREF(todecref);
3098 return 0;
3099 }
3100}
3101
3102typedef struct
3103{
3104 void *lastoption;
3105 int opt_type;
3106} optiterinfo_T;
3107
3108 static PyObject *
3109OptionsIterNext(optiterinfo_T **oii)
3110{
3111 char_u *name;
3112
3113 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3114 return PyString_FromString((char *)name);
3115
3116 return NULL;
3117}
3118
3119 static PyObject *
3120OptionsIter(OptionsObject *self)
3121{
3122 optiterinfo_T *oii;
3123
3124 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3125 {
3126 PyErr_NoMemory();
3127 return NULL;
3128 }
3129
3130 oii->opt_type = self->opt_type;
3131 oii->lastoption = NULL;
3132
3133 return IterNew(oii,
3134 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3135 NULL, NULL);
3136}
3137
3138 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003139set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003140{
3141 char_u *errmsg;
3142
3143 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3144 {
3145 if (VimTryEnd())
3146 return FAIL;
3147 PyErr_SetVim((char *)errmsg);
3148 return FAIL;
3149 }
3150 return OK;
3151}
3152
3153 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003154set_option_value_for(
3155 char_u *key,
3156 int numval,
3157 char_u *stringval,
3158 int opt_flags,
3159 int opt_type,
3160 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003161{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003162 win_T *save_curwin = NULL;
3163 tabpage_T *save_curtab = NULL;
3164 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003165 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003166
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003167 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003168 switch (opt_type)
3169 {
3170 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003171 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003172 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003173 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003174 if (VimTryEnd())
3175 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003176 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003177 return -1;
3178 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003179 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3180 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003181 break;
3182 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003183 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003184 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003185 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003186 break;
3187 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003188 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003189 break;
3190 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003191 if (set_ret == FAIL)
3192 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003193 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003194}
3195
3196 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003197OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003198{
3199 char_u *key;
3200 int flags;
3201 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003202 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003203 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003204
Bram Moolenaard6e39182013-05-21 18:30:34 +02003205 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003206 return -1;
3207
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003208 if (!(key = StringToChars(keyObject, &todecref)))
3209 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003210
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003211 if (*key == NUL)
3212 {
3213 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003214 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003215 return -1;
3216 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003217
3218 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003219 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003220
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003221 if (flags == 0)
3222 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003223 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003224 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003225 return -1;
3226 }
3227
3228 if (valObject == NULL)
3229 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003230 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003231 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003232 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003233 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003234 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003235 return -1;
3236 }
3237 else if (!(flags & SOPT_GLOBAL))
3238 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003239 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003240 N_("unable to unset option %s "
3241 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003242 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003243 return -1;
3244 }
3245 else
3246 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003247 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003248 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003249 return 0;
3250 }
3251 }
3252
Bram Moolenaard6e39182013-05-21 18:30:34 +02003253 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003254
3255 if (flags & SOPT_BOOL)
3256 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003257 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003258
Bram Moolenaarb983f752013-05-15 16:11:50 +02003259 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003260 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003261 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003262 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003263 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003264 }
3265 else if (flags & SOPT_NUM)
3266 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003267 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003268
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003269 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003270 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003271 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003272 return -1;
3273 }
3274
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003275 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003276 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003277 }
3278 else
3279 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003280 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003281 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003282
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003283 if ((val = StringToChars(valObject, &todecref2)))
3284 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003285 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003286 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003287 Py_XDECREF(todecref2);
3288 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003289 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003290 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003291 }
3292
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003293 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003294
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003295 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003296}
3297
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003298static PySequenceMethods OptionsAsSeq = {
3299 0, /* sq_length */
3300 0, /* sq_concat */
3301 0, /* sq_repeat */
3302 0, /* sq_item */
3303 0, /* sq_slice */
3304 0, /* sq_ass_item */
3305 0, /* sq_ass_slice */
3306 (objobjproc) OptionsContains, /* sq_contains */
3307 0, /* sq_inplace_concat */
3308 0, /* sq_inplace_repeat */
3309};
3310
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003311static PyMappingMethods OptionsAsMapping = {
3312 (lenfunc) NULL,
3313 (binaryfunc) OptionsItem,
3314 (objobjargproc) OptionsAssItem,
3315};
3316
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003317/* Tabpage object
3318 */
3319
3320typedef struct
3321{
3322 PyObject_HEAD
3323 tabpage_T *tab;
3324} TabPageObject;
3325
3326static PyObject *WinListNew(TabPageObject *tabObject);
3327
3328static PyTypeObject TabPageType;
3329
3330 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003331CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003332{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003333 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003334 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003335 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003336 return -1;
3337 }
3338
3339 return 0;
3340}
3341
3342 static PyObject *
3343TabPageNew(tabpage_T *tab)
3344{
3345 TabPageObject *self;
3346
3347 if (TAB_PYTHON_REF(tab))
3348 {
3349 self = TAB_PYTHON_REF(tab);
3350 Py_INCREF(self);
3351 }
3352 else
3353 {
3354 self = PyObject_NEW(TabPageObject, &TabPageType);
3355 if (self == NULL)
3356 return NULL;
3357 self->tab = tab;
3358 TAB_PYTHON_REF(tab) = self;
3359 }
3360
3361 return (PyObject *)(self);
3362}
3363
3364 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003365TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003366{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003367 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3368 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003369
3370 DESTRUCTOR_FINISH(self);
3371}
3372
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003373static char *TabPageAttrs[] = {
3374 "windows", "number", "vars", "window", "valid",
3375 NULL
3376};
3377
3378 static PyObject *
3379TabPageDir(PyObject *self)
3380{
3381 return ObjectDir(self, TabPageAttrs);
3382}
3383
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003384 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003385TabPageAttrValid(TabPageObject *self, char *name)
3386{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003387 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003388
3389 if (strcmp(name, "valid") != 0)
3390 return NULL;
3391
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003392 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3393 Py_INCREF(ret);
3394 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003395}
3396
3397 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003398TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003399{
3400 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003401 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003402 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003403 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003404 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003405 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003406 else if (strcmp(name, "window") == 0)
3407 {
3408 /* For current tab window.c does not bother to set or update tp_curwin
3409 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003410 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003411 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003412 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003413 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003414 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003415 else if (strcmp(name, "__members__") == 0)
3416 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003417 return NULL;
3418}
3419
3420 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003421TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003422{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003423 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003424 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003425 else
3426 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003427 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003428
3429 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003430 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3431 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003432 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003433 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003434 }
3435}
3436
3437static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003438 /* name, function, calling, documentation */
3439 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3440 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003441};
3442
3443/*
3444 * Window list object
3445 */
3446
3447static PyTypeObject TabListType;
3448static PySequenceMethods TabListAsSeq;
3449
3450typedef struct
3451{
3452 PyObject_HEAD
3453} TabListObject;
3454
3455 static PyInt
3456TabListLength(PyObject *self UNUSED)
3457{
3458 tabpage_T *tp = first_tabpage;
3459 PyInt n = 0;
3460
3461 while (tp != NULL)
3462 {
3463 ++n;
3464 tp = tp->tp_next;
3465 }
3466
3467 return n;
3468}
3469
3470 static PyObject *
3471TabListItem(PyObject *self UNUSED, PyInt n)
3472{
3473 tabpage_T *tp;
3474
3475 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3476 if (n == 0)
3477 return TabPageNew(tp);
3478
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003479 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003480 return NULL;
3481}
3482
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003483/*
3484 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003485 */
3486
3487typedef struct
3488{
3489 PyObject_HEAD
3490 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003491 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003492} WindowObject;
3493
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003494static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003495
3496 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003497CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003498{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003499 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003500 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003501 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003502 return -1;
3503 }
3504
3505 return 0;
3506}
3507
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003508 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003509WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003510{
3511 /* We need to handle deletion of windows underneath us.
3512 * If we add a "w_python*_ref" field to the win_T structure,
3513 * then we can get at it in win_free() in vim. We then
3514 * need to create only ONE Python object per window - if
3515 * we try to create a second, just INCREF the existing one
3516 * and return it. The (single) Python object referring to
3517 * the window is stored in "w_python*_ref".
3518 * On a win_free() we set the Python object's win_T* field
3519 * to an invalid value. We trap all uses of a window
3520 * object, and reject them if the win_T* field is invalid.
3521 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003522 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003523 * w_python_ref and w_python3_ref fields respectively.
3524 */
3525
3526 WindowObject *self;
3527
3528 if (WIN_PYTHON_REF(win))
3529 {
3530 self = WIN_PYTHON_REF(win);
3531 Py_INCREF(self);
3532 }
3533 else
3534 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003535 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003536 if (self == NULL)
3537 return NULL;
3538 self->win = win;
3539 WIN_PYTHON_REF(win) = self;
3540 }
3541
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003542 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3543
Bram Moolenaar971db462013-05-12 18:44:48 +02003544 return (PyObject *)(self);
3545}
3546
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003547 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003548WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003549{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003550 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003551 if (self->win && self->win != INVALID_WINDOW_VALUE)
3552 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003553 Py_XDECREF(((PyObject *)(self->tabObject)));
3554 PyObject_GC_Del((void *)(self));
3555}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003556
Bram Moolenaar774267b2013-05-21 20:51:59 +02003557 static int
3558WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3559{
3560 Py_VISIT(((PyObject *)(self->tabObject)));
3561 return 0;
3562}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003563
Bram Moolenaar774267b2013-05-21 20:51:59 +02003564 static int
3565WindowClear(WindowObject *self)
3566{
3567 Py_CLEAR(self->tabObject);
3568 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003569}
3570
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003571 static win_T *
3572get_firstwin(TabPageObject *tabObject)
3573{
3574 if (tabObject)
3575 {
3576 if (CheckTabPage(tabObject))
3577 return NULL;
3578 /* For current tab window.c does not bother to set or update tp_firstwin
3579 */
3580 else if (tabObject->tab == curtab)
3581 return firstwin;
3582 else
3583 return tabObject->tab->tp_firstwin;
3584 }
3585 else
3586 return firstwin;
3587}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003588static char *WindowAttrs[] = {
3589 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3590 "tabpage", "valid",
3591 NULL
3592};
3593
3594 static PyObject *
3595WindowDir(PyObject *self)
3596{
3597 return ObjectDir(self, WindowAttrs);
3598}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003599
Bram Moolenaar971db462013-05-12 18:44:48 +02003600 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003601WindowAttrValid(WindowObject *self, char *name)
3602{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003603 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003604
3605 if (strcmp(name, "valid") != 0)
3606 return NULL;
3607
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003608 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3609 Py_INCREF(ret);
3610 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003611}
3612
3613 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003614WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003615{
3616 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003617 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003618 else if (strcmp(name, "cursor") == 0)
3619 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003620 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003621
3622 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3623 }
3624 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003625 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003626#ifdef FEAT_WINDOWS
3627 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003628 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003629#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003630#ifdef FEAT_VERTSPLIT
3631 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003632 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003633 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003634 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003635#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003636 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003637 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003638 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003639 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3640 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003641 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003642 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003643 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003644 return NULL;
3645 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003646 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003647 }
3648 else if (strcmp(name, "tabpage") == 0)
3649 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003650 Py_INCREF(self->tabObject);
3651 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003652 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003653 else if (strcmp(name, "__members__") == 0)
3654 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003655 else
3656 return NULL;
3657}
3658
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003659 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003660WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003661{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003662 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003663 return -1;
3664
3665 if (strcmp(name, "buffer") == 0)
3666 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003667 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003668 return -1;
3669 }
3670 else if (strcmp(name, "cursor") == 0)
3671 {
3672 long lnum;
3673 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003674
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003675 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003676 return -1;
3677
Bram Moolenaard6e39182013-05-21 18:30:34 +02003678 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003679 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003680 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003681 return -1;
3682 }
3683
3684 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003685 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003686 return -1;
3687
Bram Moolenaard6e39182013-05-21 18:30:34 +02003688 self->win->w_cursor.lnum = lnum;
3689 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003690#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003691 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003692#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003693 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003694 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003695
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003696 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003697 return 0;
3698 }
3699 else if (strcmp(name, "height") == 0)
3700 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003701 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003702 win_T *savewin;
3703
Bram Moolenaardee2e312013-06-23 16:35:47 +02003704 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003705 return -1;
3706
3707#ifdef FEAT_GUI
3708 need_mouse_correct = TRUE;
3709#endif
3710 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003711 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003712
3713 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003714 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003715 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003716 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003717 return -1;
3718
3719 return 0;
3720 }
3721#ifdef FEAT_VERTSPLIT
3722 else if (strcmp(name, "width") == 0)
3723 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003724 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003725 win_T *savewin;
3726
Bram Moolenaardee2e312013-06-23 16:35:47 +02003727 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003728 return -1;
3729
3730#ifdef FEAT_GUI
3731 need_mouse_correct = TRUE;
3732#endif
3733 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003734 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003735
3736 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003737 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003738 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003739 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003740 return -1;
3741
3742 return 0;
3743 }
3744#endif
3745 else
3746 {
3747 PyErr_SetString(PyExc_AttributeError, name);
3748 return -1;
3749 }
3750}
3751
3752 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003753WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003754{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003755 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003756 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003757 else
3758 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003759 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003760
Bram Moolenaar6d216452013-05-12 19:00:41 +02003761 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003762 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003763 (self));
3764 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003765 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003766 }
3767}
3768
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003769static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003770 /* name, function, calling, documentation */
3771 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3772 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003773};
3774
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003775/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003776 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003777 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003778
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003779static PyTypeObject WinListType;
3780static PySequenceMethods WinListAsSeq;
3781
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003782typedef struct
3783{
3784 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003785 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003786} WinListObject;
3787
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003788 static PyObject *
3789WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003790{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003791 WinListObject *self;
3792
3793 self = PyObject_NEW(WinListObject, &WinListType);
3794 self->tabObject = tabObject;
3795 Py_INCREF(tabObject);
3796
3797 return (PyObject *)(self);
3798}
3799
3800 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003801WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003802{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003803 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003804
3805 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003806 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003807 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003808 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003809
3810 DESTRUCTOR_FINISH(self);
3811}
3812
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003813 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003814WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003815{
3816 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003817 PyInt n = 0;
3818
Bram Moolenaard6e39182013-05-21 18:30:34 +02003819 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003820 return -1;
3821
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003822 while (w != NULL)
3823 {
3824 ++n;
3825 w = W_NEXT(w);
3826 }
3827
3828 return n;
3829}
3830
3831 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003832WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003833{
3834 win_T *w;
3835
Bram Moolenaard6e39182013-05-21 18:30:34 +02003836 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003837 return NULL;
3838
3839 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003840 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003841 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003842
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003843 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003844 return NULL;
3845}
3846
3847/* Convert a Python string into a Vim line.
3848 *
3849 * The result is in allocated memory. All internal nulls are replaced by
3850 * newline characters. It is an error for the string to contain newline
3851 * characters.
3852 *
3853 * On errors, the Python exception data is set, and NULL is returned.
3854 */
3855 static char *
3856StringToLine(PyObject *obj)
3857{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003858 char *str;
3859 char *save;
3860 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003861 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003862 PyInt i;
3863 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003864
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003865 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003866 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003867 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3868 || str == NULL)
3869 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003870 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003871 else if (PyUnicode_Check(obj))
3872 {
3873 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3874 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003875
Bram Moolenaardaa27022013-06-24 22:33:30 +02003876 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003877 || str == NULL)
3878 {
3879 Py_DECREF(bytes);
3880 return NULL;
3881 }
3882 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003883 else
3884 {
3885#if PY_MAJOR_VERSION < 3
3886 PyErr_FORMAT(PyExc_TypeError,
3887 N_("expected str() or unicode() instance, but got %s"),
3888 Py_TYPE_NAME(obj));
3889#else
3890 PyErr_FORMAT(PyExc_TypeError,
3891 N_("expected bytes() or str() instance, but got %s"),
3892 Py_TYPE_NAME(obj));
3893#endif
3894 return NULL;
3895 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003896
3897 /*
3898 * Error checking: String must not contain newlines, as we
3899 * are replacing a single line, and we must replace it with
3900 * a single line.
3901 * A trailing newline is removed, so that append(f.readlines()) works.
3902 */
3903 p = memchr(str, '\n', len);
3904 if (p != NULL)
3905 {
3906 if (p == str + len - 1)
3907 --len;
3908 else
3909 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003910 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003911 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003912 return NULL;
3913 }
3914 }
3915
3916 /* Create a copy of the string, with internal nulls replaced by
3917 * newline characters, as is the vim convention.
3918 */
3919 save = (char *)alloc((unsigned)(len+1));
3920 if (save == NULL)
3921 {
3922 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003923 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003924 return NULL;
3925 }
3926
3927 for (i = 0; i < len; ++i)
3928 {
3929 if (str[i] == '\0')
3930 save[i] = '\n';
3931 else
3932 save[i] = str[i];
3933 }
3934
3935 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003936 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003937
3938 return save;
3939}
3940
3941/* Get a line from the specified buffer. The line number is
3942 * in Vim format (1-based). The line is returned as a Python
3943 * string object.
3944 */
3945 static PyObject *
3946GetBufferLine(buf_T *buf, PyInt n)
3947{
3948 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3949}
3950
3951
3952/* Get a list of lines from the specified buffer. The line numbers
3953 * are in Vim format (1-based). The range is from lo up to, but not
3954 * including, hi. The list is returned as a Python list of string objects.
3955 */
3956 static PyObject *
3957GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3958{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003959 PyInt i;
3960 PyInt n = hi - lo;
3961 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003962
3963 if (list == NULL)
3964 return NULL;
3965
3966 for (i = 0; i < n; ++i)
3967 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003968 PyObject *string = LineToString(
3969 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003970
3971 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003972 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003973 {
3974 Py_DECREF(list);
3975 return NULL;
3976 }
3977
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003978 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003979 }
3980
3981 /* The ownership of the Python list is passed to the caller (ie,
3982 * the caller should Py_DECREF() the object when it is finished
3983 * with it).
3984 */
3985
3986 return list;
3987}
3988
3989/*
3990 * Check if deleting lines made the cursor position invalid.
3991 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
3992 * deleted).
3993 */
3994 static void
3995py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
3996{
3997 if (curwin->w_cursor.lnum >= lo)
3998 {
3999 /* Adjust the cursor position if it's in/after the changed
4000 * lines. */
4001 if (curwin->w_cursor.lnum >= hi)
4002 {
4003 curwin->w_cursor.lnum += extra;
4004 check_cursor_col();
4005 }
4006 else if (extra < 0)
4007 {
4008 curwin->w_cursor.lnum = lo;
4009 check_cursor();
4010 }
4011 else
4012 check_cursor_col();
4013 changed_cline_bef_curs();
4014 }
4015 invalidate_botline();
4016}
4017
Bram Moolenaar19e60942011-06-19 00:27:51 +02004018/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004019 * Find a window that contains "buf" and switch to it.
4020 * If there is no such window, use the current window and change "curbuf".
4021 * Caller must initialize save_curbuf to NULL.
4022 * restore_win_for_buf() MUST be called later!
4023 */
4024 static void
4025switch_to_win_for_buf(
4026 buf_T *buf,
4027 win_T **save_curwinp,
4028 tabpage_T **save_curtabp,
4029 buf_T **save_curbufp)
4030{
4031 win_T *wp;
4032 tabpage_T *tp;
4033
4034 if (find_win_for_buf(buf, &wp, &tp) == FAIL
4035 || switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4036 switch_buffer(save_curbufp, buf);
4037}
4038
4039 static void
4040restore_win_for_buf(
4041 win_T *save_curwin,
4042 tabpage_T *save_curtab,
4043 buf_T *save_curbuf)
4044{
4045 if (save_curbuf == NULL)
4046 restore_win(save_curwin, save_curtab, TRUE);
4047 else
4048 restore_buffer(save_curbuf);
4049}
4050
4051/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004052 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004053 * in Vim format (1-based). The replacement line is given as
4054 * a Python string object. The object is checked for validity
4055 * and correct format. Errors are returned as a value of FAIL.
4056 * The return value is OK on success.
4057 * If OK is returned and len_change is not NULL, *len_change
4058 * is set to the change in the buffer length.
4059 */
4060 static int
4061SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4062{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004063 buf_T *save_curbuf = NULL;
4064 win_T *save_curwin = NULL;
4065 tabpage_T *save_curtab = NULL;
4066
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004067 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004068 * There are three cases:
4069 * 1. NULL, or None - this is a deletion.
4070 * 2. A string - this is a replacement.
4071 * 3. Anything else - this is an error.
4072 */
4073 if (line == Py_None || line == NULL)
4074 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004075 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004076 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004077
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004078 VimTryStart();
4079
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004080 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004081 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004082 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004083 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004084 else
4085 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004086 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004087 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004088 if (save_curbuf == NULL)
4089 /* Only adjust marks if we managed to switch to a window that
4090 * holds the buffer, otherwise line numbers will be invalid. */
4091 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004092 }
4093
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004094 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004095
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004096 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004097 return FAIL;
4098
4099 if (len_change)
4100 *len_change = -1;
4101
4102 return OK;
4103 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004104 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004105 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004106 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107
4108 if (save == NULL)
4109 return FAIL;
4110
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004111 VimTryStart();
4112
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004113 /* We do not need to free "save" if ml_replace() consumes it. */
4114 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004115 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116
4117 if (u_savesub((linenr_T)n) == FAIL)
4118 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004119 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004120 vim_free(save);
4121 }
4122 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4123 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004124 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004125 vim_free(save);
4126 }
4127 else
4128 changed_bytes((linenr_T)n, 0);
4129
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004130 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004131
4132 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004133 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004134 check_cursor_col();
4135
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004136 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004137 return FAIL;
4138
4139 if (len_change)
4140 *len_change = 0;
4141
4142 return OK;
4143 }
4144 else
4145 {
4146 PyErr_BadArgument();
4147 return FAIL;
4148 }
4149}
4150
Bram Moolenaar19e60942011-06-19 00:27:51 +02004151/* Replace a range of lines in the specified buffer. The line numbers are in
4152 * Vim format (1-based). The range is from lo up to, but not including, hi.
4153 * The replacement lines are given as a Python list of string objects. The
4154 * list is checked for validity and correct format. Errors are returned as a
4155 * value of FAIL. The return value is OK on success.
4156 * If OK is returned and len_change is not NULL, *len_change
4157 * is set to the change in the buffer length.
4158 */
4159 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004160SetBufferLineList(
4161 buf_T *buf,
4162 PyInt lo,
4163 PyInt hi,
4164 PyObject *list,
4165 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004166{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004167 buf_T *save_curbuf = NULL;
4168 win_T *save_curwin = NULL;
4169 tabpage_T *save_curtab = NULL;
4170
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004171 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004172 * There are three cases:
4173 * 1. NULL, or None - this is a deletion.
4174 * 2. A list - this is a replacement.
4175 * 3. Anything else - this is an error.
4176 */
4177 if (list == Py_None || list == NULL)
4178 {
4179 PyInt i;
4180 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004181
4182 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004183 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004184 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004185
4186 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004187 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004188 else
4189 {
4190 for (i = 0; i < n; ++i)
4191 {
4192 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4193 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004194 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004195 break;
4196 }
4197 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004198 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004199 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004200 if (save_curbuf == NULL)
4201 /* Only adjust marks if we managed to switch to a window that
4202 * holds the buffer, otherwise line numbers will be invalid. */
4203 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004204 }
4205
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004206 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004207
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004208 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004209 return FAIL;
4210
4211 if (len_change)
4212 *len_change = -n;
4213
4214 return OK;
4215 }
4216 else if (PyList_Check(list))
4217 {
4218 PyInt i;
4219 PyInt new_len = PyList_Size(list);
4220 PyInt old_len = hi - lo;
4221 PyInt extra = 0; /* lines added to text, can be negative */
4222 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004223
4224 if (new_len == 0) /* avoid allocating zero bytes */
4225 array = NULL;
4226 else
4227 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004228 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004229 if (array == NULL)
4230 {
4231 PyErr_NoMemory();
4232 return FAIL;
4233 }
4234 }
4235
4236 for (i = 0; i < new_len; ++i)
4237 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004238 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004239
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004240 if (!(line = PyList_GetItem(list, i)) ||
4241 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004242 {
4243 while (i)
4244 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004245 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004246 return FAIL;
4247 }
4248 }
4249
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004250 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004251 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004252
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004253 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004254 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004255
4256 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004257 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004258
4259 /* If the size of the range is reducing (ie, new_len < old_len) we
4260 * need to delete some old_len. We do this at the start, by
4261 * repeatedly deleting line "lo".
4262 */
4263 if (!PyErr_Occurred())
4264 {
4265 for (i = 0; i < old_len - new_len; ++i)
4266 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4267 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004268 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004269 break;
4270 }
4271 extra -= i;
4272 }
4273
4274 /* For as long as possible, replace the existing old_len with the
4275 * new old_len. This is a more efficient operation, as it requires
4276 * less memory allocation and freeing.
4277 */
4278 if (!PyErr_Occurred())
4279 {
4280 for (i = 0; i < old_len && i < new_len; ++i)
4281 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4282 == FAIL)
4283 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004284 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004285 break;
4286 }
4287 }
4288 else
4289 i = 0;
4290
4291 /* Now we may need to insert the remaining new old_len. If we do, we
4292 * must free the strings as we finish with them (we can't pass the
4293 * responsibility to vim in this case).
4294 */
4295 if (!PyErr_Occurred())
4296 {
4297 while (i < new_len)
4298 {
4299 if (ml_append((linenr_T)(lo + i - 1),
4300 (char_u *)array[i], 0, FALSE) == FAIL)
4301 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004302 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004303 break;
4304 }
4305 vim_free(array[i]);
4306 ++i;
4307 ++extra;
4308 }
4309 }
4310
4311 /* Free any left-over old_len, as a result of an error */
4312 while (i < new_len)
4313 {
4314 vim_free(array[i]);
4315 ++i;
4316 }
4317
4318 /* Free the array of old_len. All of its contents have now
4319 * been dealt with (either freed, or the responsibility passed
4320 * to vim.
4321 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004322 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004323
4324 /* Adjust marks. Invalidate any which lie in the
4325 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004326 * Only adjust marks if we managed to switch to a window that holds
4327 * the buffer, otherwise line numbers will be invalid. */
4328 if (save_curbuf == NULL)
4329 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004330 (long)MAXLNUM, (long)extra);
4331 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4332
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004333 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004334 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4335
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004336 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004337 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004338
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004339 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004340 return FAIL;
4341
4342 if (len_change)
4343 *len_change = new_len - old_len;
4344
4345 return OK;
4346 }
4347 else
4348 {
4349 PyErr_BadArgument();
4350 return FAIL;
4351 }
4352}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004353
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004354/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004355 * The line number is in Vim format (1-based). The lines to be inserted are
4356 * given as a Python list of string objects or as a single string. The lines
4357 * to be added are checked for validity and correct format. Errors are
4358 * returned as a value of FAIL. The return value is OK on success.
4359 * If OK is returned and len_change is not NULL, *len_change
4360 * is set to the change in the buffer length.
4361 */
4362 static int
4363InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4364{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004365 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004366 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004367 tabpage_T *save_curtab = NULL;
4368
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004369 /* First of all, we check the type of the supplied Python object.
4370 * It must be a string or a list, or the call is in error.
4371 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004372 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004373 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004374 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004375
4376 if (str == NULL)
4377 return FAIL;
4378
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004379 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004380 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004381 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004382
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004383 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004384 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004385 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004386 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004387 else if (save_curbuf == NULL)
4388 /* Only adjust marks if we managed to switch to a window that
4389 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004390 appended_lines_mark((linenr_T)n, 1L);
4391
4392 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004393 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004394 update_screen(VALID);
4395
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004396 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004397 return FAIL;
4398
4399 if (len_change)
4400 *len_change = 1;
4401
4402 return OK;
4403 }
4404 else if (PyList_Check(lines))
4405 {
4406 PyInt i;
4407 PyInt size = PyList_Size(lines);
4408 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004409
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004410 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004411 if (array == NULL)
4412 {
4413 PyErr_NoMemory();
4414 return FAIL;
4415 }
4416
4417 for (i = 0; i < size; ++i)
4418 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004419 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004420
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004421 if (!(line = PyList_GetItem(lines, i)) ||
4422 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004423 {
4424 while (i)
4425 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004426 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004427 return FAIL;
4428 }
4429 }
4430
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004431 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004432 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004433 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004434
4435 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004436 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004437 else
4438 {
4439 for (i = 0; i < size; ++i)
4440 {
4441 if (ml_append((linenr_T)(n + i),
4442 (char_u *)array[i], 0, FALSE) == FAIL)
4443 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004444 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004445
4446 /* Free the rest of the lines */
4447 while (i < size)
4448 vim_free(array[i++]);
4449
4450 break;
4451 }
4452 vim_free(array[i]);
4453 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004454 if (i > 0 && save_curbuf == NULL)
4455 /* Only adjust marks if we managed to switch to a window that
4456 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004457 appended_lines_mark((linenr_T)n, (long)i);
4458 }
4459
4460 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004461 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004462 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004463 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004464
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004465 update_screen(VALID);
4466
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004467 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004468 return FAIL;
4469
4470 if (len_change)
4471 *len_change = size;
4472
4473 return OK;
4474 }
4475 else
4476 {
4477 PyErr_BadArgument();
4478 return FAIL;
4479 }
4480}
4481
4482/*
4483 * Common routines for buffers and line ranges
4484 * -------------------------------------------
4485 */
4486
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004487typedef struct
4488{
4489 PyObject_HEAD
4490 buf_T *buf;
4491} BufferObject;
4492
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004493 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004494CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004495{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004496 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004497 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004498 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004499 return -1;
4500 }
4501
4502 return 0;
4503}
4504
4505 static PyObject *
4506RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4507{
4508 if (CheckBuffer(self))
4509 return NULL;
4510
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004511 if (end == -1)
4512 end = self->buf->b_ml.ml_line_count;
4513
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004514 if (n < 0)
4515 n += end - start + 1;
4516
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004517 if (n < 0 || n > end - start)
4518 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004519 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004520 return NULL;
4521 }
4522
4523 return GetBufferLine(self->buf, n+start);
4524}
4525
4526 static PyObject *
4527RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4528{
4529 PyInt size;
4530
4531 if (CheckBuffer(self))
4532 return NULL;
4533
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004534 if (end == -1)
4535 end = self->buf->b_ml.ml_line_count;
4536
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004537 size = end - start + 1;
4538
4539 if (lo < 0)
4540 lo = 0;
4541 else if (lo > size)
4542 lo = size;
4543 if (hi < 0)
4544 hi = 0;
4545 if (hi < lo)
4546 hi = lo;
4547 else if (hi > size)
4548 hi = size;
4549
4550 return GetBufferLineList(self->buf, lo+start, hi+start);
4551}
4552
4553 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004554RBAsItem(
4555 BufferObject *self,
4556 PyInt n,
4557 PyObject *valObject,
4558 PyInt start,
4559 PyInt end,
4560 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004561{
4562 PyInt len_change;
4563
4564 if (CheckBuffer(self))
4565 return -1;
4566
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004567 if (end == -1)
4568 end = self->buf->b_ml.ml_line_count;
4569
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004570 if (n < 0)
4571 n += end - start + 1;
4572
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004573 if (n < 0 || n > end - start)
4574 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004575 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004576 return -1;
4577 }
4578
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004579 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004580 return -1;
4581
4582 if (new_end)
4583 *new_end = end + len_change;
4584
4585 return 0;
4586}
4587
Bram Moolenaar19e60942011-06-19 00:27:51 +02004588 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004589RBAsSlice(
4590 BufferObject *self,
4591 PyInt lo,
4592 PyInt hi,
4593 PyObject *valObject,
4594 PyInt start,
4595 PyInt end,
4596 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004597{
4598 PyInt size;
4599 PyInt len_change;
4600
4601 /* Self must be a valid buffer */
4602 if (CheckBuffer(self))
4603 return -1;
4604
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004605 if (end == -1)
4606 end = self->buf->b_ml.ml_line_count;
4607
Bram Moolenaar19e60942011-06-19 00:27:51 +02004608 /* Sort out the slice range */
4609 size = end - start + 1;
4610
4611 if (lo < 0)
4612 lo = 0;
4613 else if (lo > size)
4614 lo = size;
4615 if (hi < 0)
4616 hi = 0;
4617 if (hi < lo)
4618 hi = lo;
4619 else if (hi > size)
4620 hi = size;
4621
4622 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004623 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004624 return -1;
4625
4626 if (new_end)
4627 *new_end = end + len_change;
4628
4629 return 0;
4630}
4631
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004632
4633 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004634RBAppend(
4635 BufferObject *self,
4636 PyObject *args,
4637 PyInt start,
4638 PyInt end,
4639 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004640{
4641 PyObject *lines;
4642 PyInt len_change;
4643 PyInt max;
4644 PyInt n;
4645
4646 if (CheckBuffer(self))
4647 return NULL;
4648
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004649 if (end == -1)
4650 end = self->buf->b_ml.ml_line_count;
4651
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004652 max = n = end - start + 1;
4653
4654 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4655 return NULL;
4656
4657 if (n < 0 || n > max)
4658 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004659 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004660 return NULL;
4661 }
4662
4663 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4664 return NULL;
4665
4666 if (new_end)
4667 *new_end = end + len_change;
4668
4669 Py_INCREF(Py_None);
4670 return Py_None;
4671}
4672
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004673/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004674 */
4675
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004676static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004677static PySequenceMethods RangeAsSeq;
4678static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004679
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004680typedef struct
4681{
4682 PyObject_HEAD
4683 BufferObject *buf;
4684 PyInt start;
4685 PyInt end;
4686} RangeObject;
4687
4688 static PyObject *
4689RangeNew(buf_T *buf, PyInt start, PyInt end)
4690{
4691 BufferObject *bufr;
4692 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004693 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004694 if (self == NULL)
4695 return NULL;
4696
4697 bufr = (BufferObject *)BufferNew(buf);
4698 if (bufr == NULL)
4699 {
4700 Py_DECREF(self);
4701 return NULL;
4702 }
4703 Py_INCREF(bufr);
4704
4705 self->buf = bufr;
4706 self->start = start;
4707 self->end = end;
4708
4709 return (PyObject *)(self);
4710}
4711
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004712 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004713RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004714{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004715 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004716 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004717 PyObject_GC_Del((void *)(self));
4718}
4719
4720 static int
4721RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4722{
4723 Py_VISIT(((PyObject *)(self->buf)));
4724 return 0;
4725}
4726
4727 static int
4728RangeClear(RangeObject *self)
4729{
4730 Py_CLEAR(self->buf);
4731 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004732}
4733
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004734 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004735RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004736{
4737 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004738 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004739 return -1; /* ??? */
4740
Bram Moolenaard6e39182013-05-21 18:30:34 +02004741 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004742}
4743
4744 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004745RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004746{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004747 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004748}
4749
4750 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004751RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004752{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004753 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004754}
4755
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004756static char *RangeAttrs[] = {
4757 "start", "end",
4758 NULL
4759};
4760
4761 static PyObject *
4762RangeDir(PyObject *self)
4763{
4764 return ObjectDir(self, RangeAttrs);
4765}
4766
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004767 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004768RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004769{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004770 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004771}
4772
4773 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004774RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004775{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004776 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004777 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4778 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004779 else
4780 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004781 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004782
4783 if (name == NULL)
4784 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004785
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004786 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004787 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004788 }
4789}
4790
4791static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004792 /* name, function, calling, documentation */
4793 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004794 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4795 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004796};
4797
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004798static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004799static PySequenceMethods BufferAsSeq;
4800static PyMappingMethods BufferAsMapping;
4801
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004802 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004803BufferNew(buf_T *buf)
4804{
4805 /* We need to handle deletion of buffers underneath us.
4806 * If we add a "b_python*_ref" field to the buf_T structure,
4807 * then we can get at it in buf_freeall() in vim. We then
4808 * need to create only ONE Python object per buffer - if
4809 * we try to create a second, just INCREF the existing one
4810 * and return it. The (single) Python object referring to
4811 * the buffer is stored in "b_python*_ref".
4812 * Question: what to do on a buf_freeall(). We'll probably
4813 * have to either delete the Python object (DECREF it to
4814 * zero - a bad idea, as it leaves dangling refs!) or
4815 * set the buf_T * value to an invalid value (-1?), which
4816 * means we need checks in all access functions... Bah.
4817 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004818 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004819 * b_python_ref and b_python3_ref fields respectively.
4820 */
4821
4822 BufferObject *self;
4823
4824 if (BUF_PYTHON_REF(buf) != NULL)
4825 {
4826 self = BUF_PYTHON_REF(buf);
4827 Py_INCREF(self);
4828 }
4829 else
4830 {
4831 self = PyObject_NEW(BufferObject, &BufferType);
4832 if (self == NULL)
4833 return NULL;
4834 self->buf = buf;
4835 BUF_PYTHON_REF(buf) = self;
4836 }
4837
4838 return (PyObject *)(self);
4839}
4840
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004841 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004842BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004843{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004844 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4845 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004846
4847 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004848}
4849
Bram Moolenaar971db462013-05-12 18:44:48 +02004850 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004851BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004852{
4853 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004854 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004855 return -1; /* ??? */
4856
Bram Moolenaard6e39182013-05-21 18:30:34 +02004857 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004858}
4859
4860 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004861BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004862{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004863 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004864}
4865
4866 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004867BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004868{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004869 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004870}
4871
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004872static char *BufferAttrs[] = {
4873 "name", "number", "vars", "options", "valid",
4874 NULL
4875};
4876
4877 static PyObject *
4878BufferDir(PyObject *self)
4879{
4880 return ObjectDir(self, BufferAttrs);
4881}
4882
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004883 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004884BufferAttrValid(BufferObject *self, char *name)
4885{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004886 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004887
4888 if (strcmp(name, "valid") != 0)
4889 return NULL;
4890
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004891 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4892 Py_INCREF(ret);
4893 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004894}
4895
4896 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004897BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004898{
4899 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004900 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004901 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004902 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004903 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004904 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004905 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004906 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004907 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4908 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004909 else if (strcmp(name, "__members__") == 0)
4910 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004911 else
4912 return NULL;
4913}
4914
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004915 static int
4916BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4917{
4918 if (CheckBuffer(self))
4919 return -1;
4920
4921 if (strcmp(name, "name") == 0)
4922 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004923 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004924 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004925 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004926 PyObject *todecref;
4927
4928 if (!(val = StringToChars(valObject, &todecref)))
4929 return -1;
4930
4931 VimTryStart();
4932 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4933 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004934 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004935 aucmd_restbuf(&aco);
4936 Py_XDECREF(todecref);
4937 if (VimTryEnd())
4938 return -1;
4939
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004940 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004941 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004942 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004943 return -1;
4944 }
4945 return 0;
4946 }
4947 else
4948 {
4949 PyErr_SetString(PyExc_AttributeError, name);
4950 return -1;
4951 }
4952}
4953
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004954 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004955BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004956{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004957 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004958}
4959
4960 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004961BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004962{
4963 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004964 char_u *pmark;
4965 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02004966 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004967 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004968
Bram Moolenaard6e39182013-05-21 18:30:34 +02004969 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004970 return NULL;
4971
Bram Moolenaar389a1792013-06-23 13:00:44 +02004972 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004973 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004974
Bram Moolenaar389a1792013-06-23 13:00:44 +02004975 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004976 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02004977 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004978 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02004979 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004980 return NULL;
4981 }
4982
4983 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004984
4985 Py_XDECREF(todecref);
4986
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004987 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02004988 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004989 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02004990 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004991 if (VimTryEnd())
4992 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004993
4994 if (posp == NULL)
4995 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004996 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004997 return NULL;
4998 }
4999
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005000 if (posp->lnum <= 0)
5001 {
5002 /* Or raise an error? */
5003 Py_INCREF(Py_None);
5004 return Py_None;
5005 }
5006
5007 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5008}
5009
5010 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005011BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005012{
5013 PyInt start;
5014 PyInt end;
5015
Bram Moolenaard6e39182013-05-21 18:30:34 +02005016 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005017 return NULL;
5018
5019 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5020 return NULL;
5021
Bram Moolenaard6e39182013-05-21 18:30:34 +02005022 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005023}
5024
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005025 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005026BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005027{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005028 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005029 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005030 else
5031 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005032 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005033
5034 if (name == NULL)
5035 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005036
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005037 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005038 }
5039}
5040
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005041static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005042 /* name, function, calling, documentation */
5043 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005044 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005045 {"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 +02005046 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5047 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005048};
5049
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005050/*
5051 * Buffer list object - Implementation
5052 */
5053
5054static PyTypeObject BufMapType;
5055
5056typedef struct
5057{
5058 PyObject_HEAD
5059} BufMapObject;
5060
5061 static PyInt
5062BufMapLength(PyObject *self UNUSED)
5063{
5064 buf_T *b = firstbuf;
5065 PyInt n = 0;
5066
5067 while (b)
5068 {
5069 ++n;
5070 b = b->b_next;
5071 }
5072
5073 return n;
5074}
5075
5076 static PyObject *
5077BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5078{
5079 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005080 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005081
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005082 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005083 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005084
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005085 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005086
5087 if (b)
5088 return BufferNew(b);
5089 else
5090 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005091 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005092 return NULL;
5093 }
5094}
5095
5096 static void
5097BufMapIterDestruct(PyObject *buffer)
5098{
5099 /* Iteration was stopped before all buffers were processed */
5100 if (buffer)
5101 {
5102 Py_DECREF(buffer);
5103 }
5104}
5105
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005106 static int
5107BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5108{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005109 if (buffer)
5110 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005111 return 0;
5112}
5113
5114 static int
5115BufMapIterClear(PyObject **buffer)
5116{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005117 if (*buffer)
5118 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005119 return 0;
5120}
5121
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005122 static PyObject *
5123BufMapIterNext(PyObject **buffer)
5124{
5125 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005126 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005127
5128 if (!*buffer)
5129 return NULL;
5130
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005131 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005132
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005133 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005134 {
5135 *buffer = NULL;
5136 return NULL;
5137 }
5138
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005139 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005140 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005141 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005142 return NULL;
5143 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005144 /* Do not increment reference: we no longer hold it (decref), but whoever
5145 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005146 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005147}
5148
5149 static PyObject *
5150BufMapIter(PyObject *self UNUSED)
5151{
5152 PyObject *buffer;
5153
5154 buffer = BufferNew(firstbuf);
5155 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005156 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5157 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005158}
5159
5160static PyMappingMethods BufMapAsMapping = {
5161 (lenfunc) BufMapLength,
5162 (binaryfunc) BufMapItem,
5163 (objobjargproc) 0,
5164};
5165
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005166/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005167 */
5168
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005169static char *CurrentAttrs[] = {
5170 "buffer", "window", "line", "range", "tabpage",
5171 NULL
5172};
5173
5174 static PyObject *
5175CurrentDir(PyObject *self)
5176{
5177 return ObjectDir(self, CurrentAttrs);
5178}
5179
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005180 static PyObject *
5181CurrentGetattr(PyObject *self UNUSED, char *name)
5182{
5183 if (strcmp(name, "buffer") == 0)
5184 return (PyObject *)BufferNew(curbuf);
5185 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005186 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005187 else if (strcmp(name, "tabpage") == 0)
5188 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005189 else if (strcmp(name, "line") == 0)
5190 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5191 else if (strcmp(name, "range") == 0)
5192 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005193 else if (strcmp(name, "__members__") == 0)
5194 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005195 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005196#if PY_MAJOR_VERSION < 3
5197 return Py_FindMethod(WindowMethods, self, name);
5198#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005199 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005200#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005201}
5202
5203 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005204CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005205{
5206 if (strcmp(name, "line") == 0)
5207 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005208 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5209 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005210 return -1;
5211
5212 return 0;
5213 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005214 else if (strcmp(name, "buffer") == 0)
5215 {
5216 int count;
5217
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005218 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005219 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005220 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005221 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005222 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005223 return -1;
5224 }
5225
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005226 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005227 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005228 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005229
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005230 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005231 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5232 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005233 if (VimTryEnd())
5234 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005235 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005236 return -1;
5237 }
5238
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005239 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005240 }
5241 else if (strcmp(name, "window") == 0)
5242 {
5243 int count;
5244
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005245 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005246 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005247 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005248 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005249 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005250 return -1;
5251 }
5252
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005253 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005254 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005255 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005256
5257 if (!count)
5258 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005259 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005260 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005261 return -1;
5262 }
5263
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005264 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005265 win_goto(((WindowObject *)(valObject))->win);
5266 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005267 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005268 if (VimTryEnd())
5269 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005270 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005271 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005272 return -1;
5273 }
5274
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005275 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005276 }
5277 else if (strcmp(name, "tabpage") == 0)
5278 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005279 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005280 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005281 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005282 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005283 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005284 return -1;
5285 }
5286
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005287 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005288 return -1;
5289
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005290 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005291 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5292 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005293 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005294 if (VimTryEnd())
5295 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005296 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005297 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005298 return -1;
5299 }
5300
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005301 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005302 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005303 else
5304 {
5305 PyErr_SetString(PyExc_AttributeError, name);
5306 return -1;
5307 }
5308}
5309
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005310static struct PyMethodDef CurrentMethods[] = {
5311 /* name, function, calling, documentation */
5312 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5313 { NULL, NULL, 0, NULL}
5314};
5315
Bram Moolenaardb913952012-06-29 12:54:53 +02005316 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005317init_range_cmd(exarg_T *eap)
5318{
5319 RangeStart = eap->line1;
5320 RangeEnd = eap->line2;
5321}
5322
5323 static void
5324init_range_eval(typval_T *rettv UNUSED)
5325{
5326 RangeStart = (PyInt) curwin->w_cursor.lnum;
5327 RangeEnd = RangeStart;
5328}
5329
5330 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005331run_cmd(const char *cmd, void *arg UNUSED
5332#ifdef PY_CAN_RECURSE
5333 , PyGILState_STATE *pygilstate UNUSED
5334#endif
5335 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005336{
Bram Moolenaar41009372013-07-01 22:03:04 +02005337 PyObject *run_ret;
5338 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5339 if (run_ret != NULL)
5340 {
5341 Py_DECREF(run_ret);
5342 }
5343 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5344 {
5345 EMSG2(_(e_py_systemexit), "python");
5346 PyErr_Clear();
5347 }
5348 else
5349 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005350}
5351
5352static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5353static int code_hdr_len = 30;
5354
5355 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005356run_do(const char *cmd, void *arg UNUSED
5357#ifdef PY_CAN_RECURSE
5358 , PyGILState_STATE *pygilstate
5359#endif
5360 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005361{
5362 PyInt lnum;
5363 size_t len;
5364 char *code;
5365 int status;
5366 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005367 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005368
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005369 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005370 {
5371 EMSG(_("cannot save undo information"));
5372 return;
5373 }
5374
5375 len = code_hdr_len + STRLEN(cmd);
5376 code = PyMem_New(char, len + 1);
5377 memcpy(code, code_hdr, code_hdr_len);
5378 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005379 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5380 status = -1;
5381 if (run_ret != NULL)
5382 {
5383 status = 0;
5384 Py_DECREF(run_ret);
5385 }
5386 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5387 {
5388 PyMem_Free(code);
5389 EMSG2(_(e_py_systemexit), "python");
5390 PyErr_Clear();
5391 return;
5392 }
5393 else
5394 PyErr_PrintEx(1);
5395
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005396 PyMem_Free(code);
5397
5398 if (status)
5399 {
5400 EMSG(_("failed to run the code"));
5401 return;
5402 }
5403
5404 status = 0;
5405 pymain = PyImport_AddModule("__main__");
5406 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005407#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005408 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005409#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005410
5411 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5412 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005413 PyObject *line;
5414 PyObject *linenr;
5415 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005416
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005417#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005418 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005419#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005420 if (!(line = GetBufferLine(curbuf, lnum)))
5421 goto err;
5422 if (!(linenr = PyInt_FromLong((long) lnum)))
5423 {
5424 Py_DECREF(line);
5425 goto err;
5426 }
5427 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5428 Py_DECREF(line);
5429 Py_DECREF(linenr);
5430 if (!ret)
5431 goto err;
5432
5433 if (ret != Py_None)
5434 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5435 goto err;
5436
5437 Py_XDECREF(ret);
5438 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005439#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005440 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005441#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005442 }
5443 goto out;
5444err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005445#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005446 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005447#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005448 PyErr_PrintEx(0);
5449 PythonIO_Flush();
5450 status = 1;
5451out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005452#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005453 if (!status)
5454 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005455#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005456 Py_DECREF(pyfunc);
5457 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5458 if (status)
5459 return;
5460 check_cursor();
5461 update_curbuf(NOT_VALID);
5462}
5463
5464 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005465run_eval(const char *cmd, typval_T *rettv
5466#ifdef PY_CAN_RECURSE
5467 , PyGILState_STATE *pygilstate UNUSED
5468#endif
5469 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005470{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005471 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005472
Bram Moolenaar41009372013-07-01 22:03:04 +02005473 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005474 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005475 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005476 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005477 {
5478 EMSG2(_(e_py_systemexit), "python");
5479 PyErr_Clear();
5480 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005481 else
5482 {
5483 if (PyErr_Occurred() && !msg_silent)
5484 PyErr_PrintEx(0);
5485 EMSG(_("E858: Eval did not return a valid python object"));
5486 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005487 }
5488 else
5489 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005490 if (ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005491 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005492 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005493 }
5494 PyErr_Clear();
5495}
5496
5497 static void
Bram Moolenaardb913952012-06-29 12:54:53 +02005498set_ref_in_py(const int copyID)
5499{
5500 pylinkedlist_T *cur;
5501 dict_T *dd;
5502 list_T *ll;
5503
5504 if (lastdict != NULL)
5505 for(cur = lastdict ; cur != NULL ; cur = cur->pll_prev)
5506 {
5507 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5508 if (dd->dv_copyID != copyID)
5509 {
5510 dd->dv_copyID = copyID;
5511 set_ref_in_ht(&dd->dv_hashtab, copyID);
5512 }
5513 }
5514
5515 if (lastlist != NULL)
5516 for(cur = lastlist ; cur != NULL ; cur = cur->pll_prev)
5517 {
5518 ll = ((ListObject *) (cur->pll_obj))->list;
5519 if (ll->lv_copyID != copyID)
5520 {
5521 ll->lv_copyID = copyID;
5522 set_ref_in_list(ll, copyID);
5523 }
5524 }
5525}
5526
5527 static int
5528set_string_copy(char_u *str, typval_T *tv)
5529{
5530 tv->vval.v_string = vim_strsave(str);
5531 if (tv->vval.v_string == NULL)
5532 {
5533 PyErr_NoMemory();
5534 return -1;
5535 }
5536 return 0;
5537}
5538
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005539 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005540pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005541{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005542 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005543 char_u *key;
5544 dictitem_T *di;
5545 PyObject *keyObject;
5546 PyObject *valObject;
5547 Py_ssize_t iter = 0;
5548
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005549 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005550 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005551
5552 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005553 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005554
5555 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5556 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005557 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005558
Bram Moolenaara03e6312013-05-29 22:49:26 +02005559 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005560 {
5561 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005562 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005563 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005564
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005565 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005566 {
5567 dict_unref(dict);
5568 return -1;
5569 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005570
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005571 if (*key == NUL)
5572 {
5573 dict_unref(dict);
5574 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005575 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005576 return -1;
5577 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005578
5579 di = dictitem_alloc(key);
5580
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005581 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005582
5583 if (di == NULL)
5584 {
5585 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005586 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005587 return -1;
5588 }
5589 di->di_tv.v_lock = 0;
5590
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005591 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005592 {
5593 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005594 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005595 return -1;
5596 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005597
5598 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005599 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005600 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005601 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005602 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005603 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005604 return -1;
5605 }
5606 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005607
5608 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005609 return 0;
5610}
5611
5612 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005613pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005614{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005615 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005616 char_u *key;
5617 dictitem_T *di;
5618 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005619 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005620 PyObject *keyObject;
5621 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005622
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005623 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005624 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005625
5626 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005627 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005628
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005629 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005630 {
5631 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005632 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005633 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005634
5635 if (!(iterator = PyObject_GetIter(list)))
5636 {
5637 dict_unref(dict);
5638 Py_DECREF(list);
5639 return -1;
5640 }
5641 Py_DECREF(list);
5642
5643 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005644 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005645 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005646
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005647 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005648 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005649 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005650 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005651 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005652 return -1;
5653 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005654
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005655 if (*key == NUL)
5656 {
5657 Py_DECREF(keyObject);
5658 Py_DECREF(iterator);
5659 Py_XDECREF(todecref);
5660 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005661 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005662 return -1;
5663 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005664
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005665 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005666 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005667 Py_DECREF(keyObject);
5668 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005669 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005670 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005671 return -1;
5672 }
5673
5674 di = dictitem_alloc(key);
5675
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005676 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005677 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005678
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005679 if (di == NULL)
5680 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005681 Py_DECREF(iterator);
5682 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005683 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005684 PyErr_NoMemory();
5685 return -1;
5686 }
5687 di->di_tv.v_lock = 0;
5688
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005689 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005690 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005691 Py_DECREF(iterator);
5692 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005693 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005694 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005695 return -1;
5696 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005697
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005698 Py_DECREF(valObject);
5699
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005700 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005701 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005702 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005703 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005704 dictitem_free(di);
5705 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005706 return -1;
5707 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005708 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005709 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005710 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005711 return 0;
5712}
5713
5714 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005715pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005716{
5717 list_T *l;
5718
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005719 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005720 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005721
5722 tv->v_type = VAR_LIST;
5723 tv->vval.v_list = l;
5724
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005725 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005726 {
5727 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005728 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005729 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005730
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005731 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005732 return 0;
5733}
5734
Bram Moolenaardb913952012-06-29 12:54:53 +02005735typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5736
5737 static int
5738convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005739 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005740{
5741 PyObject *capsule;
5742 char hexBuf[sizeof(void *) * 2 + 3];
5743
5744 sprintf(hexBuf, "%p", obj);
5745
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005746# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005747 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005748# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005749 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005750# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005751 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005752 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005753# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005754 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005755# else
5756 capsule = PyCObject_FromVoidPtr(tv, NULL);
5757# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005758 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5759 {
5760 Py_DECREF(capsule);
5761 tv->v_type = VAR_UNKNOWN;
5762 return -1;
5763 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005764
5765 Py_DECREF(capsule);
5766
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005767 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005768 {
5769 tv->v_type = VAR_UNKNOWN;
5770 return -1;
5771 }
5772 /* As we are not using copy_tv which increments reference count we must
5773 * do it ourself. */
5774 switch(tv->v_type)
5775 {
5776 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5777 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5778 }
5779 }
5780 else
5781 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005782 typval_T *v;
5783
5784# ifdef PY_USE_CAPSULE
5785 v = PyCapsule_GetPointer(capsule, NULL);
5786# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005787 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005788# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005789 copy_tv(v, tv);
5790 }
5791 return 0;
5792}
5793
5794 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005795ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5796{
5797 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005798 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005799
5800 if (!(lookup_dict = PyDict_New()))
5801 return -1;
5802
5803 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5804 {
5805 tv->v_type = VAR_DICT;
5806 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5807 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005808 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005809 }
5810 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005811 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005812 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005813 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005814 else
5815 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005816 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005817 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005818 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005819 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005820 }
5821 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005822 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005823}
5824
5825 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005826ConvertFromPyObject(PyObject *obj, typval_T *tv)
5827{
5828 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005829 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005830
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005831 if (!(lookup_dict = PyDict_New()))
5832 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005833 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005834 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005835 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005836}
5837
5838 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005839_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005840{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005841 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005842 {
5843 tv->v_type = VAR_DICT;
5844 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5845 ++tv->vval.v_dict->dv_refcount;
5846 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005847 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005848 {
5849 tv->v_type = VAR_LIST;
5850 tv->vval.v_list = (((ListObject *)(obj))->list);
5851 ++tv->vval.v_list->lv_refcount;
5852 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005853 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005854 {
5855 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5856 return -1;
5857
5858 tv->v_type = VAR_FUNC;
5859 func_ref(tv->vval.v_string);
5860 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005861 else if (PyBytes_Check(obj))
5862 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005863 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005864
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005865 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005866 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005867 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005868 return -1;
5869
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005870 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005871 return -1;
5872
5873 tv->v_type = VAR_STRING;
5874 }
5875 else if (PyUnicode_Check(obj))
5876 {
5877 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005878 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005879
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005880 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005881 if (bytes == NULL)
5882 return -1;
5883
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005884 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005885 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005886 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005887 return -1;
5888
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005889 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005890 {
5891 Py_XDECREF(bytes);
5892 return -1;
5893 }
5894 Py_XDECREF(bytes);
5895
5896 tv->v_type = VAR_STRING;
5897 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005898#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005899 else if (PyInt_Check(obj))
5900 {
5901 tv->v_type = VAR_NUMBER;
5902 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005903 if (PyErr_Occurred())
5904 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005905 }
5906#endif
5907 else if (PyLong_Check(obj))
5908 {
5909 tv->v_type = VAR_NUMBER;
5910 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005911 if (PyErr_Occurred())
5912 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005913 }
5914 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005915 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005916#ifdef FEAT_FLOAT
5917 else if (PyFloat_Check(obj))
5918 {
5919 tv->v_type = VAR_FLOAT;
5920 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5921 }
5922#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005923 else if (PyObject_HasAttrString(obj, "keys"))
5924 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005925 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005926 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005927 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005928 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005929 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005930 else if (PyNumber_Check(obj))
5931 {
5932 PyObject *num;
5933
5934 if (!(num = PyNumber_Long(obj)))
5935 return -1;
5936
5937 tv->v_type = VAR_NUMBER;
5938 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5939
5940 Py_DECREF(num);
5941 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005942 else
5943 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005944 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005945 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005946 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005947 return -1;
5948 }
5949 return 0;
5950}
5951
5952 static PyObject *
5953ConvertToPyObject(typval_T *tv)
5954{
5955 if (tv == NULL)
5956 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005957 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005958 return NULL;
5959 }
5960 switch (tv->v_type)
5961 {
5962 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005963 return PyBytes_FromString(tv->vval.v_string == NULL
5964 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005965 case VAR_NUMBER:
5966 return PyLong_FromLong((long) tv->vval.v_number);
5967#ifdef FEAT_FLOAT
5968 case VAR_FLOAT:
5969 return PyFloat_FromDouble((double) tv->vval.v_float);
5970#endif
5971 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005972 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02005973 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02005974 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005975 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02005976 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02005977 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02005978 case VAR_UNKNOWN:
5979 Py_INCREF(Py_None);
5980 return Py_None;
5981 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005982 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005983 return NULL;
5984 }
5985}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005986
5987typedef struct
5988{
5989 PyObject_HEAD
5990} CurrentObject;
5991static PyTypeObject CurrentType;
5992
5993 static void
5994init_structs(void)
5995{
5996 vim_memset(&OutputType, 0, sizeof(OutputType));
5997 OutputType.tp_name = "vim.message";
5998 OutputType.tp_basicsize = sizeof(OutputObject);
5999 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6000 OutputType.tp_doc = "vim message object";
6001 OutputType.tp_methods = OutputMethods;
6002#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006003 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6004 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006005 OutputType.tp_alloc = call_PyType_GenericAlloc;
6006 OutputType.tp_new = call_PyType_GenericNew;
6007 OutputType.tp_free = call_PyObject_Free;
6008#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006009 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6010 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006011#endif
6012
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006013 vim_memset(&IterType, 0, sizeof(IterType));
6014 IterType.tp_name = "vim.iter";
6015 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006016 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006017 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006018 IterType.tp_iter = (getiterfunc)IterIter;
6019 IterType.tp_iternext = (iternextfunc)IterNext;
6020 IterType.tp_dealloc = (destructor)IterDestructor;
6021 IterType.tp_traverse = (traverseproc)IterTraverse;
6022 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006023
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006024 vim_memset(&BufferType, 0, sizeof(BufferType));
6025 BufferType.tp_name = "vim.buffer";
6026 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006027 BufferType.tp_dealloc = (destructor)BufferDestructor;
6028 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006029 BufferType.tp_as_sequence = &BufferAsSeq;
6030 BufferType.tp_as_mapping = &BufferAsMapping;
6031 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6032 BufferType.tp_doc = "vim buffer object";
6033 BufferType.tp_methods = BufferMethods;
6034#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006035 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006036 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006037 BufferType.tp_alloc = call_PyType_GenericAlloc;
6038 BufferType.tp_new = call_PyType_GenericNew;
6039 BufferType.tp_free = call_PyObject_Free;
6040#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006041 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006042 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006043#endif
6044
6045 vim_memset(&WindowType, 0, sizeof(WindowType));
6046 WindowType.tp_name = "vim.window";
6047 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006048 WindowType.tp_dealloc = (destructor)WindowDestructor;
6049 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006050 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006051 WindowType.tp_doc = "vim Window object";
6052 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006053 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6054 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006055#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006056 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6057 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006058 WindowType.tp_alloc = call_PyType_GenericAlloc;
6059 WindowType.tp_new = call_PyType_GenericNew;
6060 WindowType.tp_free = call_PyObject_Free;
6061#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006062 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6063 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006064#endif
6065
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006066 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6067 TabPageType.tp_name = "vim.tabpage";
6068 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006069 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6070 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006071 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6072 TabPageType.tp_doc = "vim tab page object";
6073 TabPageType.tp_methods = TabPageMethods;
6074#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006075 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006076 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6077 TabPageType.tp_new = call_PyType_GenericNew;
6078 TabPageType.tp_free = call_PyObject_Free;
6079#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006080 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006081#endif
6082
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006083 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6084 BufMapType.tp_name = "vim.bufferlist";
6085 BufMapType.tp_basicsize = sizeof(BufMapObject);
6086 BufMapType.tp_as_mapping = &BufMapAsMapping;
6087 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006088 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006089 BufferType.tp_doc = "vim buffer list";
6090
6091 vim_memset(&WinListType, 0, sizeof(WinListType));
6092 WinListType.tp_name = "vim.windowlist";
6093 WinListType.tp_basicsize = sizeof(WinListType);
6094 WinListType.tp_as_sequence = &WinListAsSeq;
6095 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6096 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006097 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006098
6099 vim_memset(&TabListType, 0, sizeof(TabListType));
6100 TabListType.tp_name = "vim.tabpagelist";
6101 TabListType.tp_basicsize = sizeof(TabListType);
6102 TabListType.tp_as_sequence = &TabListAsSeq;
6103 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6104 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006105
6106 vim_memset(&RangeType, 0, sizeof(RangeType));
6107 RangeType.tp_name = "vim.range";
6108 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006109 RangeType.tp_dealloc = (destructor)RangeDestructor;
6110 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006111 RangeType.tp_as_sequence = &RangeAsSeq;
6112 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006113 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006114 RangeType.tp_doc = "vim Range object";
6115 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006116 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6117 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006118#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006119 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006120 RangeType.tp_alloc = call_PyType_GenericAlloc;
6121 RangeType.tp_new = call_PyType_GenericNew;
6122 RangeType.tp_free = call_PyObject_Free;
6123#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006124 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006125#endif
6126
6127 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6128 CurrentType.tp_name = "vim.currentdata";
6129 CurrentType.tp_basicsize = sizeof(CurrentObject);
6130 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6131 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006132 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006133#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006134 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6135 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006136#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006137 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6138 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006139#endif
6140
6141 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6142 DictionaryType.tp_name = "vim.dictionary";
6143 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006144 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006145 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006146 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006147 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006148 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6149 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006150 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6151 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6152 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006153#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006154 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6155 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006156#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006157 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6158 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006159#endif
6160
6161 vim_memset(&ListType, 0, sizeof(ListType));
6162 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006163 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006164 ListType.tp_basicsize = sizeof(ListObject);
6165 ListType.tp_as_sequence = &ListAsSeq;
6166 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006167 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006168 ListType.tp_doc = "list pushing modifications to vim structure";
6169 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006170 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006171 ListType.tp_new = (newfunc)ListConstructor;
6172 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006173#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006174 ListType.tp_getattro = (getattrofunc)ListGetattro;
6175 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006176#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006177 ListType.tp_getattr = (getattrfunc)ListGetattr;
6178 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006179#endif
6180
6181 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006182 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006183 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006184 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6185 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006186 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006187 FunctionType.tp_doc = "object that calls vim function";
6188 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006189 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006190 FunctionType.tp_new = (newfunc)FunctionConstructor;
6191 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006192#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006193 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006194#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006195 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006196#endif
6197
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006198 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6199 OptionsType.tp_name = "vim.options";
6200 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006201 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006202 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006203 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006204 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006205 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006206 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6207 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6208 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006209
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006210 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6211 LoaderType.tp_name = "vim.Loader";
6212 LoaderType.tp_basicsize = sizeof(LoaderObject);
6213 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6214 LoaderType.tp_doc = "vim message object";
6215 LoaderType.tp_methods = LoaderMethods;
6216 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6217
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006218#if PY_MAJOR_VERSION >= 3
6219 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6220 vimmodule.m_name = "vim";
6221 vimmodule.m_doc = "Vim Python interface\n";
6222 vimmodule.m_size = -1;
6223 vimmodule.m_methods = VimMethods;
6224#endif
6225}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006226
6227#define PYTYPE_READY(type) \
6228 if (PyType_Ready(&type)) \
6229 return -1;
6230
6231 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006232init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006233{
6234 PYTYPE_READY(IterType);
6235 PYTYPE_READY(BufferType);
6236 PYTYPE_READY(RangeType);
6237 PYTYPE_READY(WindowType);
6238 PYTYPE_READY(TabPageType);
6239 PYTYPE_READY(BufMapType);
6240 PYTYPE_READY(WinListType);
6241 PYTYPE_READY(TabListType);
6242 PYTYPE_READY(CurrentType);
6243 PYTYPE_READY(DictionaryType);
6244 PYTYPE_READY(ListType);
6245 PYTYPE_READY(FunctionType);
6246 PYTYPE_READY(OptionsType);
6247 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006248 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006249 return 0;
6250}
6251
6252 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006253init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006254{
6255 PyObject *path;
6256 PyObject *path_hook;
6257 PyObject *path_hooks;
6258
6259 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6260 return -1;
6261
6262 if (!(path_hooks = PySys_GetObject("path_hooks")))
6263 {
6264 PyErr_Clear();
6265 path_hooks = PyList_New(1);
6266 PyList_SET_ITEM(path_hooks, 0, path_hook);
6267 if (PySys_SetObject("path_hooks", path_hooks))
6268 {
6269 Py_DECREF(path_hooks);
6270 return -1;
6271 }
6272 Py_DECREF(path_hooks);
6273 }
6274 else if (PyList_Check(path_hooks))
6275 {
6276 if (PyList_Append(path_hooks, path_hook))
6277 {
6278 Py_DECREF(path_hook);
6279 return -1;
6280 }
6281 Py_DECREF(path_hook);
6282 }
6283 else
6284 {
6285 VimTryStart();
6286 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6287 "You should now do the following:\n"
6288 "- append vim.path_hook to sys.path_hooks\n"
6289 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6290 VimTryEnd(); /* Discard the error */
6291 Py_DECREF(path_hook);
6292 return 0;
6293 }
6294
6295 if (!(path = PySys_GetObject("path")))
6296 {
6297 PyErr_Clear();
6298 path = PyList_New(1);
6299 Py_INCREF(vim_special_path_object);
6300 PyList_SET_ITEM(path, 0, vim_special_path_object);
6301 if (PySys_SetObject("path", path))
6302 {
6303 Py_DECREF(path);
6304 return -1;
6305 }
6306 Py_DECREF(path);
6307 }
6308 else if (PyList_Check(path))
6309 {
6310 if (PyList_Append(path, vim_special_path_object))
6311 return -1;
6312 }
6313 else
6314 {
6315 VimTryStart();
6316 EMSG(_("Failed to set path: sys.path is not a list\n"
6317 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6318 VimTryEnd(); /* Discard the error */
6319 }
6320
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006321 return 0;
6322}
6323
6324static BufMapObject TheBufferMap =
6325{
6326 PyObject_HEAD_INIT(&BufMapType)
6327};
6328
6329static WinListObject TheWindowList =
6330{
6331 PyObject_HEAD_INIT(&WinListType)
6332 NULL
6333};
6334
6335static CurrentObject TheCurrent =
6336{
6337 PyObject_HEAD_INIT(&CurrentType)
6338};
6339
6340static TabListObject TheTabPageList =
6341{
6342 PyObject_HEAD_INIT(&TabListType)
6343};
6344
6345static struct numeric_constant {
6346 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006347 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006348} numeric_constants[] = {
6349 {"VAR_LOCKED", VAR_LOCKED},
6350 {"VAR_FIXED", VAR_FIXED},
6351 {"VAR_SCOPE", VAR_SCOPE},
6352 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6353};
6354
6355static struct object_constant {
6356 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006357 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006358} object_constants[] = {
6359 {"buffers", (PyObject *)(void *)&TheBufferMap},
6360 {"windows", (PyObject *)(void *)&TheWindowList},
6361 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6362 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006363
6364 {"Buffer", (PyObject *)&BufferType},
6365 {"Range", (PyObject *)&RangeType},
6366 {"Window", (PyObject *)&WindowType},
6367 {"TabPage", (PyObject *)&TabPageType},
6368 {"Dictionary", (PyObject *)&DictionaryType},
6369 {"List", (PyObject *)&ListType},
6370 {"Function", (PyObject *)&FunctionType},
6371 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006372 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006373};
6374
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006375#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006376 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006377 return -1;
6378
6379#define ADD_CHECKED_OBJECT(m, name, obj) \
6380 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006381 PyObject *valObject = obj; \
6382 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006383 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006384 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006385 }
6386
6387 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006388populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006389{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006390 int i;
6391 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006392 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006393 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006394
6395 for (i = 0; i < (int)(sizeof(numeric_constants)
6396 / sizeof(struct numeric_constant));
6397 ++i)
6398 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006399 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006400
6401 for (i = 0; i < (int)(sizeof(object_constants)
6402 / sizeof(struct object_constant));
6403 ++i)
6404 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006405 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006406
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006407 valObject = object_constants[i].valObject;
6408 Py_INCREF(valObject);
6409 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006410 }
6411
6412 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6413 return -1;
6414 ADD_OBJECT(m, "error", VimError);
6415
Bram Moolenaara9922d62013-05-30 13:01:18 +02006416 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6417 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006418 ADD_CHECKED_OBJECT(m, "options",
6419 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006420
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006421 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006422 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006423 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006424
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006425 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006426 return -1;
6427 ADD_OBJECT(m, "_getcwd", py_getcwd)
6428
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006429 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006430 return -1;
6431 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006432 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006433 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006434 if (PyObject_SetAttrString(other_module, "chdir", attr))
6435 {
6436 Py_DECREF(attr);
6437 return -1;
6438 }
6439 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006440
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006441 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006442 {
6443 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006444 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006445 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006446 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6447 {
6448 Py_DECREF(attr);
6449 return -1;
6450 }
6451 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006452 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006453 else
6454 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006455
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006456 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6457 return -1;
6458
6459 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6460
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006461 if (!(imp = PyImport_ImportModule("imp")))
6462 return -1;
6463
6464 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6465 {
6466 Py_DECREF(imp);
6467 return -1;
6468 }
6469
6470 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6471 {
6472 Py_DECREF(py_find_module);
6473 Py_DECREF(imp);
6474 return -1;
6475 }
6476
6477 Py_DECREF(imp);
6478
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006479 ADD_OBJECT(m, "_find_module", py_find_module);
6480 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006481
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006482 return 0;
6483}