blob: ee882600fc64d2bc0e4e26d95b30e8ecfa6c0879 [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020010 * Python extensions by Paul Moore, David Leonard, Roland Puntaier, Nikolay
11 * Pavlov.
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020012 *
13 * Common code for if_python.c and if_python3.c.
14 */
15
Bram Moolenaar78cf3f02014-01-10 18:16:07 +010016#ifdef __BORLANDC__
17/* Disable Warning W8060: Possibly incorrect assignment in function ... */
18# pragma warn -8060
19#endif
20
Bram Moolenaar41009372013-07-01 22:03:04 +020021static char_u e_py_systemexit[] = "E880: Can't handle SystemExit of %s exception in vim";
22
Bram Moolenaarc1a995d2012-08-08 16:05:07 +020023#if PY_VERSION_HEX < 0x02050000
24typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
25#endif
26
Bram Moolenaar91805fc2011-06-26 04:01:44 +020027#ifdef FEAT_MBYTE
Bram Moolenaar808c2bc2013-06-23 13:11:18 +020028# define ENC_OPT ((char *)p_enc)
Bram Moolenaar91805fc2011-06-26 04:01:44 +020029#else
30# define ENC_OPT "latin1"
31#endif
Bram Moolenaard620aa92013-05-17 16:40:06 +020032#define DOPY_FUNC "_vim_pydo"
Bram Moolenaar91805fc2011-06-26 04:01:44 +020033
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020034static const char *vim_special_path = "_vim_path_";
35
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020036#define PyErr_SET_STRING(exc, str) PyErr_SetString(exc, _(str))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020037#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020038#define PyErr_SET_VIM(str) PyErr_SET_STRING(VimError, str)
Bram Moolenaar063a46b2014-01-14 16:36:51 +010039#define PyErr_FORMAT(exc, str, arg) PyErr_Format(exc, _(str), arg)
40#define PyErr_FORMAT2(exc, str, arg1, arg2) PyErr_Format(exc, _(str), arg1,arg2)
41#define PyErr_VIM_FORMAT(str, arg) PyErr_FORMAT(VimError, str, arg)
Bram Moolenaarc476e522013-06-23 13:46:40 +020042
43#define Py_TYPE_NAME(obj) (obj->ob_type->tp_name == NULL \
44 ? "(NULL)" \
45 : obj->ob_type->tp_name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020046
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +020047#define RAISE_NO_EMPTY_KEYS PyErr_SET_STRING(PyExc_ValueError, \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020048 N_("empty keys are not allowed"))
49#define RAISE_LOCKED_DICTIONARY PyErr_SET_VIM(N_("dictionary is locked"))
50#define RAISE_LOCKED_LIST PyErr_SET_VIM(N_("list is locked"))
51#define RAISE_UNDO_FAIL PyErr_SET_VIM(N_("cannot save undo information"))
52#define RAISE_DELETE_LINE_FAIL PyErr_SET_VIM(N_("cannot delete line"))
53#define RAISE_INSERT_LINE_FAIL PyErr_SET_VIM(N_("cannot insert line"))
54#define RAISE_REPLACE_LINE_FAIL PyErr_SET_VIM(N_("cannot replace line"))
Bram Moolenaarc476e522013-06-23 13:46:40 +020055#define RAISE_KEY_ADD_FAIL(key) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020056 PyErr_VIM_FORMAT(N_("failed to add key '%s' to dictionary"), key)
Bram Moolenaarc476e522013-06-23 13:46:40 +020057#define RAISE_INVALID_INDEX_TYPE(idx) \
Bram Moolenaar6f1404f2013-06-23 16:04:08 +020058 PyErr_FORMAT(PyExc_TypeError, N_("index must be int or slice, not %s"), \
Bram Moolenaarc476e522013-06-23 13:46:40 +020059 Py_TYPE_NAME(idx));
Bram Moolenaar35eacd72013-05-30 22:06:33 +020060
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020061#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
62#define INVALID_WINDOW_VALUE ((win_T *)(-1))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +020063#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020064
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020065typedef void (*rangeinitializer)(void *);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +020066typedef void (*runner)(const char *, void *
67#ifdef PY_CAN_RECURSE
68 , PyGILState_STATE *
69#endif
70 );
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020071
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020072static int ConvertFromPyObject(PyObject *, typval_T *);
73static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Bram Moolenaara9922d62013-05-30 13:01:18 +020074static int ConvertFromPyMapping(PyObject *, typval_T *);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +020075static PyObject *WindowNew(win_T *, tabpage_T *);
76static PyObject *BufferNew (buf_T *);
77static PyObject *LineToString(const char *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +020078
79static PyInt RangeStart;
80static PyInt RangeEnd;
81
Bram Moolenaarb52f4c02013-05-21 18:19:38 +020082static PyObject *globals;
83
Bram Moolenaarf4258302013-06-02 18:20:17 +020084static PyObject *py_chdir;
85static PyObject *py_fchdir;
86static PyObject *py_getcwd;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +020087static PyObject *vim_module;
88static PyObject *vim_special_path_object;
Bram Moolenaarf4258302013-06-02 18:20:17 +020089
Bram Moolenaar81c40c52013-06-12 14:41:04 +020090static PyObject *py_find_module;
91static PyObject *py_load_module;
92
93static PyObject *VimError;
94
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020095/*
96 * obtain a lock on the Vim data structures
97 */
98 static void
99Python_Lock_Vim(void)
100{
101}
102
103/*
104 * release a lock on the Vim data structures
105 */
106 static void
107Python_Release_Vim(void)
108{
109}
110
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200111/*
112 * The "todecref" argument holds a pointer to PyObject * that must be
113 * DECREF'ed after returned char_u * is no longer needed or NULL if all what
114 * was needed to generate returned value is object.
115 *
116 * Use Py_XDECREF to decrement reference count.
117 */
118 static char_u *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200119StringToChars(PyObject *obj, PyObject **todecref)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200120{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200121 char_u *str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200122
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200123 if (PyBytes_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200124 {
125
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200126 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1
127 || str == NULL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200128 return NULL;
129
130 *todecref = NULL;
131 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200132 else if (PyUnicode_Check(obj))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200133 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200134 PyObject *bytes;
135
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200136 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200137 return NULL;
138
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200139 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1
140 || str == NULL)
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200141 {
142 Py_DECREF(bytes);
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200143 return NULL;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200144 }
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200145
146 *todecref = bytes;
147 }
148 else
149 {
Bram Moolenaarc476e522013-06-23 13:46:40 +0200150#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200151 PyErr_FORMAT(PyExc_TypeError,
152 N_("expected str() or unicode() instance, but got %s"),
153 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200154#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200155 PyErr_FORMAT(PyExc_TypeError,
156 N_("expected bytes() or str() instance, but got %s"),
157 Py_TYPE_NAME(obj));
Bram Moolenaarc476e522013-06-23 13:46:40 +0200158#endif
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200159 return NULL;
160 }
161
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200162 return (char_u *) str;
Bram Moolenaare9ba5162013-05-29 22:02:22 +0200163}
164
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200165#define NUMBER_LONG 1
166#define NUMBER_INT 2
167#define NUMBER_NATURAL 4
168#define NUMBER_UNSIGNED 8
169
170 static int
171NumberToLong(PyObject *obj, long *result, int flags)
172{
173#if PY_MAJOR_VERSION < 3
174 if (PyInt_Check(obj))
175 {
176 *result = PyInt_AsLong(obj);
177 if (PyErr_Occurred())
178 return -1;
179 }
180 else
181#endif
182 if (PyLong_Check(obj))
183 {
184 *result = PyLong_AsLong(obj);
185 if (PyErr_Occurred())
186 return -1;
187 }
188 else if (PyNumber_Check(obj))
189 {
190 PyObject *num;
191
192 if (!(num = PyNumber_Long(obj)))
193 return -1;
194
195 *result = PyLong_AsLong(num);
196
197 Py_DECREF(num);
198
199 if (PyErr_Occurred())
200 return -1;
201 }
202 else
203 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200204#if PY_MAJOR_VERSION < 3
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200205 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200206 N_("expected int(), long() or something supporting "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200207 "coercing to long(), but got %s"),
208 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200209#else
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200210 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200211 N_("expected int() or something supporting coercing to int(), "
Bram Moolenaarc1c3d682013-06-24 21:21:58 +0200212 "but got %s"),
213 Py_TYPE_NAME(obj));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200214#endif
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200215 return -1;
216 }
217
218 if (flags & NUMBER_INT)
219 {
220 if (*result > INT_MAX)
221 {
222 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200223 N_("value is too large to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200224 return -1;
225 }
226 else if (*result < INT_MIN)
227 {
228 PyErr_SET_STRING(PyExc_OverflowError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200229 N_("value is too small to fit into C int type"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200230 return -1;
231 }
232 }
233
234 if (flags & NUMBER_NATURAL)
235 {
236 if (*result <= 0)
237 {
238 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +0100239 N_("number must be greater than zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200240 return -1;
241 }
242 }
243 else if (flags & NUMBER_UNSIGNED)
244 {
245 if (*result < 0)
246 {
247 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200248 N_("number must be greater or equal to zero"));
Bram Moolenaar141be8a2013-06-23 14:16:57 +0200249 return -1;
250 }
251 }
252
253 return 0;
254}
255
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200256 static int
257add_string(PyObject *list, char *s)
258{
259 PyObject *string;
260
261 if (!(string = PyString_FromString(s)))
262 return -1;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +0200263
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200264 if (PyList_Append(list, string))
265 {
266 Py_DECREF(string);
267 return -1;
268 }
269
270 Py_DECREF(string);
271 return 0;
272}
273
274 static PyObject *
275ObjectDir(PyObject *self, char **attributes)
276{
277 PyMethodDef *method;
278 char **attr;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200279 PyObject *ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200280
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200281 if (!(ret = PyList_New(0)))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200282 return NULL;
283
284 if (self)
285 for (method = self->ob_type->tp_methods ; method->ml_name != NULL ; ++method)
Bram Moolenaar41009372013-07-01 22:03:04 +0200286 if (add_string(ret, (char *)method->ml_name))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200287 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200288 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200289 return NULL;
290 }
291
292 for (attr = attributes ; *attr ; ++attr)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200293 if (add_string(ret, *attr))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200294 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200295 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200296 return NULL;
297 }
298
299#if PY_MAJOR_VERSION < 3
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200300 if (add_string(ret, "__members__"))
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200301 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200302 Py_DECREF(ret);
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200303 return NULL;
304 }
305#endif
306
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200307 return ret;
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200308}
309
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200310/* Output buffer management
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200311 */
312
Bram Moolenaar2eea1982010-09-21 16:49:37 +0200313/* Function to write a line, points to either msg() or emsg(). */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200314typedef void (*writefn)(char_u *);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200315
316static PyTypeObject OutputType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200317
318typedef struct
319{
320 PyObject_HEAD
321 long softspace;
322 long error;
323} OutputObject;
324
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200325static char *OutputAttrs[] = {
326 "softspace",
327 NULL
328};
329
330 static PyObject *
331OutputDir(PyObject *self)
332{
333 return ObjectDir(self, OutputAttrs);
334}
335
Bram Moolenaar77045652012-09-21 13:46:06 +0200336 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200337OutputSetattr(OutputObject *self, char *name, PyObject *valObject)
Bram Moolenaar77045652012-09-21 13:46:06 +0200338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200339 if (valObject == NULL)
Bram Moolenaar77045652012-09-21 13:46:06 +0200340 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +0200341 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200342 N_("can't delete OutputObject attributes"));
Bram Moolenaar77045652012-09-21 13:46:06 +0200343 return -1;
344 }
345
346 if (strcmp(name, "softspace") == 0)
347 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200348 if (NumberToLong(valObject, &(self->softspace), NUMBER_UNSIGNED))
Bram Moolenaar77045652012-09-21 13:46:06 +0200349 return -1;
Bram Moolenaar77045652012-09-21 13:46:06 +0200350 return 0;
351 }
352
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200353 PyErr_FORMAT(PyExc_AttributeError, N_("invalid attribute: %s"), name);
Bram Moolenaar77045652012-09-21 13:46:06 +0200354 return -1;
355}
356
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200357/* Buffer IO, we write one whole line at a time. */
358static garray_T io_ga = {0, 0, 1, 80, NULL};
359static writefn old_fn = NULL;
360
361 static void
362PythonIO_Flush(void)
363{
364 if (old_fn != NULL && io_ga.ga_len > 0)
365 {
366 ((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
367 old_fn((char_u *)io_ga.ga_data);
368 }
369 io_ga.ga_len = 0;
370}
371
372 static void
373writer(writefn fn, char_u *str, PyInt n)
374{
375 char_u *ptr;
376
377 /* Flush when switching output function. */
378 if (fn != old_fn)
379 PythonIO_Flush();
380 old_fn = fn;
381
382 /* Write each NL separated line. Text after the last NL is kept for
383 * writing later. */
384 while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
385 {
386 PyInt len = ptr - str;
387
388 if (ga_grow(&io_ga, (int)(len + 1)) == FAIL)
389 break;
390
391 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
392 ((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
393 fn((char_u *)io_ga.ga_data);
394 str = ptr + 1;
395 n -= len + 1;
396 io_ga.ga_len = 0;
397 }
398
399 /* Put the remaining text into io_ga for later printing. */
400 if (n > 0 && ga_grow(&io_ga, (int)(n + 1)) == OK)
401 {
402 mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
403 io_ga.ga_len += (int)n;
404 }
405}
406
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200407 static int
408write_output(OutputObject *self, PyObject *string)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200409{
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200410 Py_ssize_t len = 0;
411 char *str = NULL;
412 int error = self->error;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200413
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200414 if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
415 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200416
417 Py_BEGIN_ALLOW_THREADS
418 Python_Lock_Vim();
419 writer((writefn)(error ? emsg : msg), (char_u *)str, len);
420 Python_Release_Vim();
421 Py_END_ALLOW_THREADS
Bram Moolenaar19e60942011-06-19 00:27:51 +0200422 PyMem_Free(str);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200423
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200424 return 0;
425}
426
427 static PyObject *
428OutputWrite(OutputObject *self, PyObject *string)
429{
430 if (write_output(self, string))
431 return NULL;
432
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200433 Py_INCREF(Py_None);
434 return Py_None;
435}
436
437 static PyObject *
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200438OutputWritelines(OutputObject *self, PyObject *seq)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200439{
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200440 PyObject *iterator;
441 PyObject *item;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200442
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200443 if (!(iterator = PyObject_GetIter(seq)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200444 return NULL;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200445
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200446 while ((item = PyIter_Next(iterator)))
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 {
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200448 if (write_output(self, item))
Bram Moolenaardb913952012-06-29 12:54:53 +0200449 {
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200450 Py_DECREF(iterator);
451 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200452 return NULL;
453 }
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200454 Py_DECREF(item);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200455 }
456
Bram Moolenaar01a7a722013-05-30 12:26:58 +0200457 Py_DECREF(iterator);
458
459 /* Iterator may have finished due to an exception */
460 if (PyErr_Occurred())
461 return NULL;
462
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200463 Py_INCREF(Py_None);
464 return Py_None;
465}
466
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100467 static PyObject *
Bram Moolenaard4247472015-11-02 13:28:59 +0100468AlwaysNone(PyObject *self UNUSED)
Bram Moolenaara29a37d2011-03-22 15:47:44 +0100469{
470 /* do nothing */
471 Py_INCREF(Py_None);
472 return Py_None;
473}
474
Bram Moolenaard4247472015-11-02 13:28:59 +0100475 static PyObject *
476AlwaysFalse(PyObject *self UNUSED)
477{
478 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100479 PyObject *ret = Py_False;
480 Py_INCREF(ret);
481 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100482}
483
484 static PyObject *
485AlwaysTrue(PyObject *self UNUSED)
486{
487 /* do nothing */
Bram Moolenaare7427f42015-11-10 13:24:20 +0100488 PyObject *ret = Py_True;
489 Py_INCREF(ret);
490 return ret;
Bram Moolenaard4247472015-11-02 13:28:59 +0100491}
492
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200493/***************/
494
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200495static struct PyMethodDef OutputMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200496 /* name, function, calling, doc */
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +0200497 {"write", (PyCFunction)OutputWrite, METH_O, ""},
498 {"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
Bram Moolenaard4247472015-11-02 13:28:59 +0100499 {"flush", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
500 {"close", (PyCFunction)AlwaysNone, METH_NOARGS, ""},
501 {"isatty", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
502 {"readable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
503 {"seekable", (PyCFunction)AlwaysFalse, METH_NOARGS, ""},
504 {"writable", (PyCFunction)AlwaysTrue, METH_NOARGS, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +0200505 {"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
Bram Moolenaar182dc4f2013-05-21 19:01:55 +0200506 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200507};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200508
509static OutputObject Output =
510{
511 PyObject_HEAD_INIT(&OutputType)
512 0,
513 0
514};
515
516static OutputObject Error =
517{
518 PyObject_HEAD_INIT(&OutputType)
519 0,
520 1
521};
522
523 static int
524PythonIO_Init_io(void)
525{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +0200526 if (PySys_SetObject("stdout", (PyObject *)(void *)&Output))
527 return -1;
528 if (PySys_SetObject("stderr", (PyObject *)(void *)&Error))
529 return -1;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200530
531 if (PyErr_Occurred())
532 {
533 EMSG(_("E264: Python: Error initialising I/O objects"));
534 return -1;
535 }
536
537 return 0;
538}
539
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200540typedef struct
541{
542 PyObject_HEAD
543 PyObject *module;
544} LoaderObject;
545static PyTypeObject LoaderType;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200546
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200547 static void
548LoaderDestructor(LoaderObject *self)
549{
550 Py_DECREF(self->module);
551 DESTRUCTOR_FINISH(self);
552}
553
554 static PyObject *
555LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
556{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200557 PyObject *ret = self->module;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200558
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200559 Py_INCREF(ret);
560 return ret;
Bram Moolenaar81c40c52013-06-12 14:41:04 +0200561}
562
563static struct PyMethodDef LoaderMethods[] = {
564 /* name, function, calling, doc */
565 {"load_module", (PyCFunction)LoaderLoadModule, METH_VARARGS, ""},
566 { NULL, NULL, 0, NULL}
567};
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200568
569/* Check to see whether a Vim error has been reported, or a keyboard
570 * interrupt has been detected.
571 */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200572
573 static void
574VimTryStart(void)
575{
576 ++trylevel;
577}
578
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200579 static int
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200580VimTryEnd(void)
581{
582 --trylevel;
Bram Moolenaar41009372013-07-01 22:03:04 +0200583 /* Without this it stops processing all subsequent VimL commands and
584 * generates strange error messages if I e.g. try calling Test() in a
585 * cycle */
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200586 did_emsg = FALSE;
587 /* Keyboard interrupt should be preferred over anything else */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200588 if (got_int)
589 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100590 if (did_throw)
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100591 discard_current_exception();
Bram Moolenaard6b8a522013-11-11 01:05:48 +0100592 got_int = FALSE;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200593 PyErr_SetNone(PyExc_KeyboardInterrupt);
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200594 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200595 }
Bram Moolenaar9fee7d42013-11-28 17:04:43 +0100596 else if (msg_list != NULL && *msg_list != NULL)
597 {
598 int should_free;
599 char_u *msg;
600
601 msg = get_exception_string(*msg_list, ET_ERROR, NULL, &should_free);
602
603 if (msg == NULL)
604 {
605 PyErr_NoMemory();
606 return -1;
607 }
608
609 PyErr_SetVim((char *) msg);
610
611 free_global_msglist();
612
613 if (should_free)
614 vim_free(msg);
615
616 return -1;
617 }
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200618 else if (!did_throw)
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200619 return (PyErr_Occurred() ? -1 : 0);
620 /* Python exception is preferred over vim one; unlikely to occur though */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200621 else if (PyErr_Occurred())
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200622 {
Bram Moolenaar4315f262014-01-31 14:54:04 +0100623 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200624 return -1;
625 }
626 /* Finally transform VimL exception to python one */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200627 else
628 {
Bram Moolenaar41009372013-07-01 22:03:04 +0200629 PyErr_SetVim((char *)current_exception->value);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200630 discard_current_exception();
Bram Moolenaar841fbd22013-06-23 14:37:07 +0200631 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200632 }
633}
634
635 static int
636VimCheckInterrupt(void)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200637{
638 if (got_int)
639 {
640 PyErr_SetNone(PyExc_KeyboardInterrupt);
641 return 1;
642 }
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200643 return 0;
644}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200645
646/* Vim module - Implementation
647 */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +0200648
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200649 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200650VimCommand(PyObject *self UNUSED, PyObject *string)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200651{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200652 char_u *cmd;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200653 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200654 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200655
Bram Moolenaar389a1792013-06-23 13:00:44 +0200656 if (!(cmd = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200657 return NULL;
658
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200659 Py_BEGIN_ALLOW_THREADS
660 Python_Lock_Vim();
661
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200662 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200663 do_cmdline_cmd(cmd);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200664 update_screen(VALID);
665
666 Python_Release_Vim();
667 Py_END_ALLOW_THREADS
668
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200669 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200670 ret = NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200671 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200672 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200673
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200674 Py_XINCREF(ret);
Bram Moolenaar389a1792013-06-23 13:00:44 +0200675 Py_XDECREF(todecref);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200676 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200677}
678
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200679/*
680 * Function to translate a typval_T into a PyObject; this will recursively
681 * translate lists/dictionaries into their Python equivalents.
682 *
683 * The depth parameter is to avoid infinite recursion, set it to 1 when
684 * you call VimToPython.
685 */
686 static PyObject *
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200687VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200688{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200689 PyObject *ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200690 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +0200691 char ptrBuf[sizeof(void *) * 2 + 3];
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200692
693 /* Avoid infinite recursion */
694 if (depth > 100)
695 {
696 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200697 ret = Py_None;
698 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200699 }
700
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200701 /* Check if we run into a recursive loop. The item must be in lookup_dict
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200702 * then and we can use it again. */
703 if ((our_tv->v_type == VAR_LIST && our_tv->vval.v_list != NULL)
704 || (our_tv->v_type == VAR_DICT && our_tv->vval.v_dict != NULL))
705 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200706 sprintf(ptrBuf, "%p",
707 our_tv->v_type == VAR_LIST ? (void *)our_tv->vval.v_list
708 : (void *)our_tv->vval.v_dict);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200709
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200710 if ((ret = PyDict_GetItemString(lookup_dict, ptrBuf)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200711 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200712 Py_INCREF(ret);
713 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200714 }
715 }
716
717 if (our_tv->v_type == VAR_STRING)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200718 ret = PyString_FromString(our_tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +0200719 ? "" : (char *)our_tv->vval.v_string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200720 else if (our_tv->v_type == VAR_NUMBER)
721 {
722 char buf[NUMBUFLEN];
723
724 /* For backwards compatibility numbers are stored as strings. */
725 sprintf(buf, "%ld", (long)our_tv->vval.v_number);
Bram Moolenaar41009372013-07-01 22:03:04 +0200726 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200727 }
728# ifdef FEAT_FLOAT
729 else if (our_tv->v_type == VAR_FLOAT)
730 {
731 char buf[NUMBUFLEN];
732
733 sprintf(buf, "%f", our_tv->vval.v_float);
Bram Moolenaar41009372013-07-01 22:03:04 +0200734 ret = PyString_FromString((char *)buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200735 }
736# endif
737 else if (our_tv->v_type == VAR_LIST)
738 {
739 list_T *list = our_tv->vval.v_list;
740 listitem_T *curr;
741
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200742 if (list == NULL)
743 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200744
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200745 if (!(ret = PyList_New(0)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200746 return NULL;
747
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200748 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200749 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200750 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200751 return NULL;
752 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200753
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200754 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
755 {
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200756 if (!(newObj = VimToPython(&curr->li_tv, depth + 1, lookup_dict)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200757 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200758 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200759 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200760 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200761 if (PyList_Append(ret, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200762 {
763 Py_DECREF(newObj);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200764 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200765 return NULL;
766 }
767 Py_DECREF(newObj);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200768 }
769 }
770 else if (our_tv->v_type == VAR_DICT)
771 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200772
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100773 hashtab_T *ht;
774 long_u todo;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200775 hashitem_T *hi;
776 dictitem_T *di;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100777
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200778 if (our_tv->vval.v_dict == NULL)
779 return NULL;
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100780 ht = &our_tv->vval.v_dict->dv_hashtab;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200781
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200782 if (!(ret = PyDict_New()))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200783 return NULL;
784
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200785 if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200786 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200787 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200788 return NULL;
789 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200790
Bram Moolenaar24a6ff82015-02-10 18:41:58 +0100791 todo = ht->ht_used;
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200792 for (hi = ht->ht_array; todo > 0; ++hi)
793 {
794 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200795 {
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200796 --todo;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200797
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200798 di = dict_lookup(hi);
Bram Moolenaarb38caae2013-05-29 22:39:52 +0200799 if (!(newObj = VimToPython(&di->di_tv, depth + 1, lookup_dict)))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200800 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200801 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200802 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200803 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200804 if (PyDict_SetItemString(ret, (char *)hi->hi_key, newObj))
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200805 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200806 Py_DECREF(ret);
Bram Moolenaar21642ed2013-05-29 22:20:01 +0200807 Py_DECREF(newObj);
808 return NULL;
809 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200810 }
811 }
812 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100813 else if (our_tv->v_type == VAR_SPECIAL)
814 {
815 if (our_tv->vval.v_number == VVAL_FALSE)
816 {
817 ret = Py_False;
818 Py_INCREF(ret);
819 }
820 else if (our_tv->vval.v_number == VVAL_TRUE)
821 {
822 ret = Py_True;
823 Py_INCREF(ret);
824 }
825 else
826 {
827 Py_INCREF(Py_None);
828 ret = Py_None;
829 }
830 return ret;
831 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200832 else
833 {
834 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200835 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200836 }
837
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200838 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200839}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200840
841 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200842VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200843{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200844 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200845 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200846 PyObject *string;
847 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200848 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200849 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200850
Bram Moolenaar389a1792013-06-23 13:00:44 +0200851 if (!PyArg_ParseTuple(args, "O", &string))
852 return NULL;
853
854 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200855 return NULL;
856
857 Py_BEGIN_ALLOW_THREADS
858 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200859 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200860 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200861 Python_Release_Vim();
862 Py_END_ALLOW_THREADS
863
Bram Moolenaar389a1792013-06-23 13:00:44 +0200864 Py_XDECREF(todecref);
865
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200866 if (VimTryEnd())
867 return NULL;
868
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200869 if (our_tv == NULL)
870 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200871 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200872 return NULL;
873 }
874
875 /* Convert the Vim type into a Python type. Create a dictionary that's
876 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200877 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200878 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200879 else
880 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200881 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200882 Py_DECREF(lookup_dict);
883 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200884
885
886 Py_BEGIN_ALLOW_THREADS
887 Python_Lock_Vim();
888 free_tv(our_tv);
889 Python_Release_Vim();
890 Py_END_ALLOW_THREADS
891
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200892 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200893}
894
Bram Moolenaardb913952012-06-29 12:54:53 +0200895static PyObject *ConvertToPyObject(typval_T *);
896
897 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200898VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200899{
Bram Moolenaardb913952012-06-29 12:54:53 +0200900 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200901 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200902 char_u *expr;
903 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200904
Bram Moolenaar389a1792013-06-23 13:00:44 +0200905 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200906 return NULL;
907
908 Py_BEGIN_ALLOW_THREADS
909 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200910 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200911 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200912 Python_Release_Vim();
913 Py_END_ALLOW_THREADS
914
Bram Moolenaar389a1792013-06-23 13:00:44 +0200915 Py_XDECREF(todecref);
916
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200917 if (VimTryEnd())
918 return NULL;
919
Bram Moolenaardb913952012-06-29 12:54:53 +0200920 if (our_tv == NULL)
921 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200922 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200923 return NULL;
924 }
925
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200926 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200927 Py_BEGIN_ALLOW_THREADS
928 Python_Lock_Vim();
929 free_tv(our_tv);
930 Python_Release_Vim();
931 Py_END_ALLOW_THREADS
932
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200933 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200934}
935
936 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200937VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200938{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200939 char_u *str;
940 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200941 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200942
Bram Moolenaar389a1792013-06-23 13:00:44 +0200943 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200944 return NULL;
945
Bram Moolenaara54bf402012-12-05 16:30:07 +0100946#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200947 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100948#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200949 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100950#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200951
952 Py_XDECREF(todecref);
953
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200954 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200955}
956
Bram Moolenaarf4258302013-06-02 18:20:17 +0200957 static PyObject *
958_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
959{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200960 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200961 PyObject *newwd;
962 PyObject *todecref;
963 char_u *new_dir;
964
Bram Moolenaard4209d22013-06-05 20:34:15 +0200965 if (_chdir == NULL)
966 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200967 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200968 return NULL;
969
970 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
971 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200972 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200973 return NULL;
974 }
975
976 if (!(new_dir = StringToChars(newwd, &todecref)))
977 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200978 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200979 Py_DECREF(newwd);
980 return NULL;
981 }
982
983 VimTryStart();
984
985 if (vim_chdir(new_dir))
986 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200987 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200988 Py_DECREF(newwd);
989 Py_XDECREF(todecref);
990
991 if (VimTryEnd())
992 return NULL;
993
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200994 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200995 return NULL;
996 }
997
998 Py_DECREF(newwd);
999 Py_XDECREF(todecref);
1000
1001 post_chdir(FALSE);
1002
1003 if (VimTryEnd())
1004 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001005 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +02001006 return NULL;
1007 }
1008
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001009 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +02001010}
1011
1012 static PyObject *
1013VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1014{
1015 return _VimChdir(py_chdir, args, kwargs);
1016}
1017
1018 static PyObject *
1019VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1020{
1021 return _VimChdir(py_fchdir, args, kwargs);
1022}
1023
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001024typedef struct {
1025 PyObject *callable;
1026 PyObject *result;
1027} map_rtp_data;
1028
1029 static void
1030map_rtp_callback(char_u *path, void *_data)
1031{
1032 void **data = (void **) _data;
1033 PyObject *pathObject;
1034 map_rtp_data *mr_data = *((map_rtp_data **) data);
1035
Bram Moolenaar41009372013-07-01 22:03:04 +02001036 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001037 {
1038 *data = NULL;
1039 return;
1040 }
1041
1042 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1043 pathObject, NULL);
1044
1045 Py_DECREF(pathObject);
1046
1047 if (!mr_data->result || mr_data->result != Py_None)
1048 *data = NULL;
1049 else
1050 {
1051 Py_DECREF(mr_data->result);
1052 mr_data->result = NULL;
1053 }
1054}
1055
1056 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001057VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001058{
1059 map_rtp_data data;
1060
Bram Moolenaar389a1792013-06-23 13:00:44 +02001061 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001062 data.result = NULL;
1063
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001064 do_in_runtimepath(NULL, 0, &map_rtp_callback, &data);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001065
1066 if (data.result == NULL)
1067 {
1068 if (PyErr_Occurred())
1069 return NULL;
1070 else
1071 {
1072 Py_INCREF(Py_None);
1073 return Py_None;
1074 }
1075 }
1076 return data.result;
1077}
1078
1079/*
1080 * _vim_runtimepath_ special path implementation.
1081 */
1082
1083 static void
1084map_finder_callback(char_u *path, void *_data)
1085{
1086 void **data = (void **) _data;
1087 PyObject *list = *((PyObject **) data);
1088 PyObject *pathObject1, *pathObject2;
1089 char *pathbuf;
1090 size_t pathlen;
1091
1092 pathlen = STRLEN(path);
1093
1094#if PY_MAJOR_VERSION < 3
1095# define PY_MAIN_DIR_STRING "python2"
1096#else
1097# define PY_MAIN_DIR_STRING "python3"
1098#endif
1099#define PY_ALTERNATE_DIR_STRING "pythonx"
1100
1101#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1102 if (!(pathbuf = PyMem_New(char,
1103 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1104 {
1105 PyErr_NoMemory();
1106 *data = NULL;
1107 return;
1108 }
1109
1110 mch_memmove(pathbuf, path, pathlen + 1);
1111 add_pathsep((char_u *) pathbuf);
1112
1113 pathlen = STRLEN(pathbuf);
1114 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1115 PYTHONX_STRING_LENGTH + 1);
1116
1117 if (!(pathObject1 = PyString_FromString(pathbuf)))
1118 {
1119 *data = NULL;
1120 PyMem_Free(pathbuf);
1121 return;
1122 }
1123
1124 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1125 PYTHONX_STRING_LENGTH + 1);
1126
1127 if (!(pathObject2 = PyString_FromString(pathbuf)))
1128 {
1129 Py_DECREF(pathObject1);
1130 PyMem_Free(pathbuf);
1131 *data = NULL;
1132 return;
1133 }
1134
1135 PyMem_Free(pathbuf);
1136
1137 if (PyList_Append(list, pathObject1)
1138 || PyList_Append(list, pathObject2))
1139 *data = NULL;
1140
1141 Py_DECREF(pathObject1);
1142 Py_DECREF(pathObject2);
1143}
1144
1145 static PyObject *
1146Vim_GetPaths(PyObject *self UNUSED)
1147{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001148 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001149
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001150 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001151 return NULL;
1152
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001153 do_in_runtimepath(NULL, 0, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001154
1155 if (PyErr_Occurred())
1156 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001157 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001158 return NULL;
1159 }
1160
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001161 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001162}
1163
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001164 static PyObject *
1165call_load_module(char *name, int len, PyObject *find_module_result)
1166{
1167 PyObject *fd, *pathname, *description;
1168
Bram Moolenaarc476e522013-06-23 13:46:40 +02001169 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001170 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001171 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001172 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001173 Py_TYPE_NAME(find_module_result));
1174 return NULL;
1175 }
1176 if (PyTuple_GET_SIZE(find_module_result) != 3)
1177 {
1178 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001179 N_("expected 3-tuple as imp.find_module() result, but got "
1180 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001181 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001182 return NULL;
1183 }
1184
1185 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1186 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1187 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1188 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001189 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001190 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001191 return NULL;
1192 }
1193
1194 return PyObject_CallFunction(py_load_module,
1195 "s#OOO", name, len, fd, pathname, description);
1196}
1197
1198 static PyObject *
1199find_module(char *fullname, char *tail, PyObject *new_path)
1200{
1201 PyObject *find_module_result;
1202 PyObject *module;
1203 char *dot;
1204
Bram Moolenaar41009372013-07-01 22:03:04 +02001205 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001206 {
1207 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001208 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001209 * first component
1210 */
1211 PyObject *newest_path;
1212 int partlen = (int) (dot - 1 - tail);
1213
1214 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1215 "s#O", tail, partlen, new_path)))
1216 return NULL;
1217
1218 if (!(module = call_load_module(
1219 fullname,
1220 ((int) (tail - fullname)) + partlen,
1221 find_module_result)))
1222 {
1223 Py_DECREF(find_module_result);
1224 return NULL;
1225 }
1226
1227 Py_DECREF(find_module_result);
1228
1229 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1230 {
1231 Py_DECREF(module);
1232 return NULL;
1233 }
1234
1235 Py_DECREF(module);
1236
1237 module = find_module(fullname, dot + 1, newest_path);
1238
1239 Py_DECREF(newest_path);
1240
1241 return module;
1242 }
1243 else
1244 {
1245 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1246 "sO", tail, new_path)))
1247 return NULL;
1248
1249 if (!(module = call_load_module(
1250 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001251 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001252 find_module_result)))
1253 {
1254 Py_DECREF(find_module_result);
1255 return NULL;
1256 }
1257
1258 Py_DECREF(find_module_result);
1259
1260 return module;
1261 }
1262}
1263
1264 static PyObject *
1265FinderFindModule(PyObject *self, PyObject *args)
1266{
1267 char *fullname;
1268 PyObject *module;
1269 PyObject *new_path;
1270 LoaderObject *loader;
1271
1272 if (!PyArg_ParseTuple(args, "s", &fullname))
1273 return NULL;
1274
1275 if (!(new_path = Vim_GetPaths(self)))
1276 return NULL;
1277
1278 module = find_module(fullname, fullname, new_path);
1279
1280 Py_DECREF(new_path);
1281
1282 if (!module)
1283 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001284 if (PyErr_Occurred())
1285 {
1286 if (PyErr_ExceptionMatches(PyExc_ImportError))
1287 PyErr_Clear();
1288 else
1289 return NULL;
1290 }
1291
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001292 Py_INCREF(Py_None);
1293 return Py_None;
1294 }
1295
1296 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1297 {
1298 Py_DECREF(module);
1299 return NULL;
1300 }
1301
1302 loader->module = module;
1303
1304 return (PyObject *) loader;
1305}
1306
1307 static PyObject *
1308VimPathHook(PyObject *self UNUSED, PyObject *args)
1309{
1310 char *path;
1311
1312 if (PyArg_ParseTuple(args, "s", &path)
1313 && STRCMP(path, vim_special_path) == 0)
1314 {
1315 Py_INCREF(vim_module);
1316 return vim_module;
1317 }
1318
1319 PyErr_Clear();
1320 PyErr_SetNone(PyExc_ImportError);
1321 return NULL;
1322}
1323
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001324/*
1325 * Vim module - Definitions
1326 */
1327
1328static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001329 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001330 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001331 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001332 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1333 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001334 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1335 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001336 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001337 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001338 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1339 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1340 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001341};
1342
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001343/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001344 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001345 */
1346
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001347static PyTypeObject IterType;
1348
1349typedef PyObject *(*nextfun)(void **);
1350typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001351typedef int (*traversefun)(void *, visitproc, void *);
1352typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001353
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001354/* Main purpose of this object is removing the need for do python
1355 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1356 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001357
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001358typedef struct
1359{
1360 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001361 void *cur;
1362 nextfun next;
1363 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001364 traversefun traverse;
1365 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001366} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001367
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001368 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001369IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1370 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001371{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001372 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001373
Bram Moolenaar774267b2013-05-21 20:51:59 +02001374 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001375 self->cur = start;
1376 self->next = next;
1377 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001378 self->traverse = traverse;
1379 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001380
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001381 return (PyObject *)(self);
1382}
1383
1384 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001385IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001386{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001387 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001388 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001389 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001390}
1391
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001392 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001393IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001394{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001395 if (self->traverse != NULL)
1396 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001397 else
1398 return 0;
1399}
1400
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001401/* Mac OSX defines clear() somewhere. */
1402#ifdef clear
1403# undef clear
1404#endif
1405
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001406 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001407IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001408{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001409 if (self->clear != NULL)
1410 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001411 else
1412 return 0;
1413}
1414
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001415 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001416IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001417{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001418 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001419}
1420
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001421 static PyObject *
1422IterIter(PyObject *self)
1423{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001424 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001425 return self;
1426}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001427
Bram Moolenaardb913952012-06-29 12:54:53 +02001428typedef struct pylinkedlist_S {
1429 struct pylinkedlist_S *pll_next;
1430 struct pylinkedlist_S *pll_prev;
1431 PyObject *pll_obj;
1432} pylinkedlist_T;
1433
1434static pylinkedlist_T *lastdict = NULL;
1435static pylinkedlist_T *lastlist = NULL;
1436
1437 static void
1438pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1439{
1440 if (ref->pll_prev == NULL)
1441 {
1442 if (ref->pll_next == NULL)
1443 {
1444 *last = NULL;
1445 return;
1446 }
1447 }
1448 else
1449 ref->pll_prev->pll_next = ref->pll_next;
1450
1451 if (ref->pll_next == NULL)
1452 *last = ref->pll_prev;
1453 else
1454 ref->pll_next->pll_prev = ref->pll_prev;
1455}
1456
1457 static void
1458pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1459{
1460 if (*last == NULL)
1461 ref->pll_prev = NULL;
1462 else
1463 {
1464 (*last)->pll_next = ref;
1465 ref->pll_prev = *last;
1466 }
1467 ref->pll_next = NULL;
1468 ref->pll_obj = self;
1469 *last = ref;
1470}
1471
1472static PyTypeObject DictionaryType;
1473
1474typedef struct
1475{
1476 PyObject_HEAD
1477 dict_T *dict;
1478 pylinkedlist_T ref;
1479} DictionaryObject;
1480
Bram Moolenaara9922d62013-05-30 13:01:18 +02001481static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1482
1483#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1484
Bram Moolenaardb913952012-06-29 12:54:53 +02001485 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001486DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001487{
1488 DictionaryObject *self;
1489
Bram Moolenaara9922d62013-05-30 13:01:18 +02001490 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001491 if (self == NULL)
1492 return NULL;
1493 self->dict = dict;
1494 ++dict->dv_refcount;
1495
1496 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1497
1498 return (PyObject *)(self);
1499}
1500
Bram Moolenaara9922d62013-05-30 13:01:18 +02001501 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001502py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001503{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001504 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001505
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001506 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001507 {
1508 PyErr_NoMemory();
1509 return NULL;
1510 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001511 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001512
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001513 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001514}
1515
1516 static PyObject *
1517DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1518{
1519 DictionaryObject *self;
1520 dict_T *dict;
1521
1522 if (!(dict = py_dict_alloc()))
1523 return NULL;
1524
1525 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1526
1527 --dict->dv_refcount;
1528
1529 if (kwargs || PyTuple_Size(args))
1530 {
1531 PyObject *tmp;
1532 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1533 {
1534 Py_DECREF(self);
1535 return NULL;
1536 }
1537
1538 Py_DECREF(tmp);
1539 }
1540
1541 return (PyObject *)(self);
1542}
1543
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001544 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001545DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001546{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001547 pyll_remove(&self->ref, &lastdict);
1548 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001549
1550 DESTRUCTOR_FINISH(self);
1551}
1552
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001553static char *DictionaryAttrs[] = {
1554 "locked", "scope",
1555 NULL
1556};
1557
1558 static PyObject *
1559DictionaryDir(PyObject *self)
1560{
1561 return ObjectDir(self, DictionaryAttrs);
1562}
1563
Bram Moolenaardb913952012-06-29 12:54:53 +02001564 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001565DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001566{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001567 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001568 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001569 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001570 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001571 return -1;
1572 }
1573
1574 if (strcmp(name, "locked") == 0)
1575 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001576 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001577 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001578 PyErr_SET_STRING(PyExc_TypeError,
1579 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001580 return -1;
1581 }
1582 else
1583 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001584 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001585 if (istrue == -1)
1586 return -1;
1587 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001588 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001589 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001590 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001591 }
1592 return 0;
1593 }
1594 else
1595 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001596 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001597 return -1;
1598 }
1599}
1600
1601 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001602DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001603{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001604 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001605}
1606
Bram Moolenaara9922d62013-05-30 13:01:18 +02001607#define DICT_FLAG_HAS_DEFAULT 0x01
1608#define DICT_FLAG_POP 0x02
1609#define DICT_FLAG_NONE_DEFAULT 0x04
1610#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1611#define DICT_FLAG_RETURN_PAIR 0x10
1612
Bram Moolenaardb913952012-06-29 12:54:53 +02001613 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001614_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001615{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001616 PyObject *keyObject;
1617 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001618 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001619 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001620 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001621 dict_T *dict = self->dict;
1622 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001623 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001624
Bram Moolenaara9922d62013-05-30 13:01:18 +02001625 if (flags & DICT_FLAG_HAS_DEFAULT)
1626 {
1627 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1628 return NULL;
1629 }
1630 else
1631 keyObject = args;
1632
1633 if (flags & DICT_FLAG_RETURN_BOOL)
1634 defObject = Py_False;
1635
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001636 if (!(key = StringToChars(keyObject, &todecref)))
1637 return NULL;
1638
1639 if (*key == NUL)
1640 {
1641 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001642 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001643 return NULL;
1644 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001645
Bram Moolenaara9922d62013-05-30 13:01:18 +02001646 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001647
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001648 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001649
Bram Moolenaara9922d62013-05-30 13:01:18 +02001650 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001651 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001652 if (defObject)
1653 {
1654 Py_INCREF(defObject);
1655 return defObject;
1656 }
1657 else
1658 {
1659 PyErr_SetObject(PyExc_KeyError, keyObject);
1660 return NULL;
1661 }
1662 }
1663 else if (flags & DICT_FLAG_RETURN_BOOL)
1664 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001665 ret = Py_True;
1666 Py_INCREF(ret);
1667 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001668 }
1669
1670 di = dict_lookup(hi);
1671
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001672 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001673 return NULL;
1674
1675 if (flags & DICT_FLAG_POP)
1676 {
1677 if (dict->dv_lock)
1678 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001679 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001680 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001681 return NULL;
1682 }
1683
1684 hash_remove(&dict->dv_hashtab, hi);
1685 dictitem_free(di);
1686 }
1687
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001688 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001689}
1690
1691 static PyObject *
1692DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1693{
1694 return _DictionaryItem(self, keyObject, 0);
1695}
1696
1697 static int
1698DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1699{
1700 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001701 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001702
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001703 if (rObj == NULL)
1704 return -1;
1705
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001706 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001707
Bram Moolenaardee2e312013-06-23 16:35:47 +02001708 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001709
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001710 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001711}
1712
1713typedef struct
1714{
1715 hashitem_T *ht_array;
1716 long_u ht_used;
1717 hashtab_T *ht;
1718 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001719 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001720} dictiterinfo_T;
1721
1722 static PyObject *
1723DictionaryIterNext(dictiterinfo_T **dii)
1724{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001725 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001726
1727 if (!(*dii)->todo)
1728 return NULL;
1729
1730 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1731 (*dii)->ht->ht_used != (*dii)->ht_used)
1732 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001733 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001734 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001735 return NULL;
1736 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001737
Bram Moolenaara9922d62013-05-30 13:01:18 +02001738 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1739 ++((*dii)->hi);
1740
1741 --((*dii)->todo);
1742
Bram Moolenaar41009372013-07-01 22:03:04 +02001743 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001744 return NULL;
1745
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001746 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001747}
1748
1749 static PyObject *
1750DictionaryIter(DictionaryObject *self)
1751{
1752 dictiterinfo_T *dii;
1753 hashtab_T *ht;
1754
1755 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1756 {
1757 PyErr_NoMemory();
1758 return NULL;
1759 }
1760
1761 ht = &self->dict->dv_hashtab;
1762 dii->ht_array = ht->ht_array;
1763 dii->ht_used = ht->ht_used;
1764 dii->ht = ht;
1765 dii->hi = dii->ht_array;
1766 dii->todo = dii->ht_used;
1767
1768 return IterNew(dii,
1769 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1770 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001771}
1772
1773 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001774DictionaryAssItem(
1775 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001776{
1777 char_u *key;
1778 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001779 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001780 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001781 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001782
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001783 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001784 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001785 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001786 return -1;
1787 }
1788
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001789 if (!(key = StringToChars(keyObject, &todecref)))
1790 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001791
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001792 if (*key == NUL)
1793 {
1794 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001795 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001796 return -1;
1797 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001798
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001799 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001800
1801 if (valObject == NULL)
1802 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001803 hashitem_T *hi;
1804
Bram Moolenaardb913952012-06-29 12:54:53 +02001805 if (di == NULL)
1806 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001807 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001808 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001809 return -1;
1810 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001811 hi = hash_find(&dict->dv_hashtab, di->di_key);
1812 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001813 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001814 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001815 return 0;
1816 }
1817
1818 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001819 {
1820 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001821 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001822 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001823
1824 if (di == NULL)
1825 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001826 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001827 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001828 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001829 PyErr_NoMemory();
1830 return -1;
1831 }
1832 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001833 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001834
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001835 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001836 {
1837 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001838 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001839 RAISE_KEY_ADD_FAIL(key);
1840 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001841 return -1;
1842 }
1843 }
1844 else
1845 clear_tv(&di->di_tv);
1846
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001847 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001848
1849 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001850 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001851 return 0;
1852}
1853
Bram Moolenaara9922d62013-05-30 13:01:18 +02001854typedef PyObject *(*hi_to_py)(hashitem_T *);
1855
Bram Moolenaardb913952012-06-29 12:54:53 +02001856 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001857DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001858{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001859 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001860 long_u todo = dict->dv_hashtab.ht_used;
1861 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001862 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001863 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001864 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001865
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001866 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001867 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1868 {
1869 if (!HASHITEM_EMPTY(hi))
1870 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001871 if (!(newObj = hiconvert(hi)))
1872 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001873 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001874 return NULL;
1875 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001876 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001877 --todo;
1878 ++i;
1879 }
1880 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001881 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001882}
1883
Bram Moolenaara9922d62013-05-30 13:01:18 +02001884 static PyObject *
1885dict_key(hashitem_T *hi)
1886{
1887 return PyBytes_FromString((char *)(hi->hi_key));
1888}
1889
1890 static PyObject *
1891DictionaryListKeys(DictionaryObject *self)
1892{
1893 return DictionaryListObjects(self, dict_key);
1894}
1895
1896 static PyObject *
1897dict_val(hashitem_T *hi)
1898{
1899 dictitem_T *di;
1900
1901 di = dict_lookup(hi);
1902 return ConvertToPyObject(&di->di_tv);
1903}
1904
1905 static PyObject *
1906DictionaryListValues(DictionaryObject *self)
1907{
1908 return DictionaryListObjects(self, dict_val);
1909}
1910
1911 static PyObject *
1912dict_item(hashitem_T *hi)
1913{
1914 PyObject *keyObject;
1915 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001916 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001917
1918 if (!(keyObject = dict_key(hi)))
1919 return NULL;
1920
1921 if (!(valObject = dict_val(hi)))
1922 {
1923 Py_DECREF(keyObject);
1924 return NULL;
1925 }
1926
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001927 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001928
1929 Py_DECREF(keyObject);
1930 Py_DECREF(valObject);
1931
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001932 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001933}
1934
1935 static PyObject *
1936DictionaryListItems(DictionaryObject *self)
1937{
1938 return DictionaryListObjects(self, dict_item);
1939}
1940
1941 static PyObject *
1942DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1943{
1944 dict_T *dict = self->dict;
1945
1946 if (dict->dv_lock)
1947 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001948 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001949 return NULL;
1950 }
1951
1952 if (kwargs)
1953 {
1954 typval_T tv;
1955
1956 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1957 return NULL;
1958
1959 VimTryStart();
1960 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1961 clear_tv(&tv);
1962 if (VimTryEnd())
1963 return NULL;
1964 }
1965 else
1966 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001967 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001968
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001969 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001970 return NULL;
1971
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001972 if (obj == NULL)
1973 {
1974 Py_INCREF(Py_None);
1975 return Py_None;
1976 }
1977
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001978 if (PyObject_HasAttrString(obj, "keys"))
1979 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001980 else
1981 {
1982 PyObject *iterator;
1983 PyObject *item;
1984
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001985 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001986 return NULL;
1987
1988 while ((item = PyIter_Next(iterator)))
1989 {
1990 PyObject *fast;
1991 PyObject *keyObject;
1992 PyObject *valObject;
1993 PyObject *todecref;
1994 char_u *key;
1995 dictitem_T *di;
1996
1997 if (!(fast = PySequence_Fast(item, "")))
1998 {
1999 Py_DECREF(iterator);
2000 Py_DECREF(item);
2001 return NULL;
2002 }
2003
2004 Py_DECREF(item);
2005
2006 if (PySequence_Fast_GET_SIZE(fast) != 2)
2007 {
2008 Py_DECREF(iterator);
2009 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02002010 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002011 N_("expected sequence element of size 2, "
2012 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02002013 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02002014 return NULL;
2015 }
2016
2017 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
2018
2019 if (!(key = StringToChars(keyObject, &todecref)))
2020 {
2021 Py_DECREF(iterator);
2022 Py_DECREF(fast);
2023 return NULL;
2024 }
2025
2026 di = dictitem_alloc(key);
2027
2028 Py_XDECREF(todecref);
2029
2030 if (di == NULL)
2031 {
2032 Py_DECREF(fast);
2033 Py_DECREF(iterator);
2034 PyErr_NoMemory();
2035 return NULL;
2036 }
2037 di->di_tv.v_lock = 0;
2038 di->di_tv.v_type = VAR_UNKNOWN;
2039
2040 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2041
2042 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2043 {
2044 Py_DECREF(iterator);
2045 Py_DECREF(fast);
2046 dictitem_free(di);
2047 return NULL;
2048 }
2049
2050 Py_DECREF(fast);
2051
2052 if (dict_add(dict, di) == FAIL)
2053 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002054 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002055 Py_DECREF(iterator);
2056 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002057 return NULL;
2058 }
2059 }
2060
2061 Py_DECREF(iterator);
2062
2063 /* Iterator may have finished due to an exception */
2064 if (PyErr_Occurred())
2065 return NULL;
2066 }
2067 }
2068 Py_INCREF(Py_None);
2069 return Py_None;
2070}
2071
2072 static PyObject *
2073DictionaryGet(DictionaryObject *self, PyObject *args)
2074{
2075 return _DictionaryItem(self, args,
2076 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2077}
2078
2079 static PyObject *
2080DictionaryPop(DictionaryObject *self, PyObject *args)
2081{
2082 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2083}
2084
2085 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002086DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002087{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002088 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002089 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002090 PyObject *valObject;
2091 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002092
Bram Moolenaarde71b562013-06-02 17:41:54 +02002093 if (self->dict->dv_hashtab.ht_used == 0)
2094 {
2095 PyErr_SetNone(PyExc_KeyError);
2096 return NULL;
2097 }
2098
2099 hi = self->dict->dv_hashtab.ht_array;
2100 while (HASHITEM_EMPTY(hi))
2101 ++hi;
2102
2103 di = dict_lookup(hi);
2104
2105 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002106 return NULL;
2107
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002108 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002109 {
2110 Py_DECREF(valObject);
2111 return NULL;
2112 }
2113
2114 hash_remove(&self->dict->dv_hashtab, hi);
2115 dictitem_free(di);
2116
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002117 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002118}
2119
2120 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002121DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002122{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002123 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2124}
2125
2126static PySequenceMethods DictionaryAsSeq = {
2127 0, /* sq_length */
2128 0, /* sq_concat */
2129 0, /* sq_repeat */
2130 0, /* sq_item */
2131 0, /* sq_slice */
2132 0, /* sq_ass_item */
2133 0, /* sq_ass_slice */
2134 (objobjproc) DictionaryContains, /* sq_contains */
2135 0, /* sq_inplace_concat */
2136 0, /* sq_inplace_repeat */
2137};
2138
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002139static PyMappingMethods DictionaryAsMapping = {
2140 (lenfunc) DictionaryLength,
2141 (binaryfunc) DictionaryItem,
2142 (objobjargproc) DictionaryAssItem,
2143};
2144
Bram Moolenaardb913952012-06-29 12:54:53 +02002145static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002146 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002147 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2148 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2149 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2150 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2151 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002152 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002153 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002154 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2155 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002156};
2157
2158static PyTypeObject ListType;
2159
2160typedef struct
2161{
2162 PyObject_HEAD
2163 list_T *list;
2164 pylinkedlist_T ref;
2165} ListObject;
2166
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002167#define NEW_LIST(list) ListNew(&ListType, list)
2168
Bram Moolenaardb913952012-06-29 12:54:53 +02002169 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002170ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002171{
2172 ListObject *self;
2173
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002174 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002175 if (self == NULL)
2176 return NULL;
2177 self->list = list;
2178 ++list->lv_refcount;
2179
2180 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2181
2182 return (PyObject *)(self);
2183}
2184
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002185 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002186py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002187{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002188 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002189
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002190 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002191 {
2192 PyErr_NoMemory();
2193 return NULL;
2194 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002195 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002196
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002197 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002198}
2199
2200 static int
2201list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2202{
2203 PyObject *iterator;
2204 PyObject *item;
2205 listitem_T *li;
2206
2207 if (!(iterator = PyObject_GetIter(obj)))
2208 return -1;
2209
2210 while ((item = PyIter_Next(iterator)))
2211 {
2212 if (!(li = listitem_alloc()))
2213 {
2214 PyErr_NoMemory();
2215 Py_DECREF(item);
2216 Py_DECREF(iterator);
2217 return -1;
2218 }
2219 li->li_tv.v_lock = 0;
2220 li->li_tv.v_type = VAR_UNKNOWN;
2221
2222 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2223 {
2224 Py_DECREF(item);
2225 Py_DECREF(iterator);
2226 listitem_free(li);
2227 return -1;
2228 }
2229
2230 Py_DECREF(item);
2231
2232 list_append(l, li);
2233 }
2234
2235 Py_DECREF(iterator);
2236
2237 /* Iterator may have finished due to an exception */
2238 if (PyErr_Occurred())
2239 return -1;
2240
2241 return 0;
2242}
2243
2244 static PyObject *
2245ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2246{
2247 list_T *list;
2248 PyObject *obj = NULL;
2249
2250 if (kwargs)
2251 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002252 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002253 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002254 return NULL;
2255 }
2256
2257 if (!PyArg_ParseTuple(args, "|O", &obj))
2258 return NULL;
2259
2260 if (!(list = py_list_alloc()))
2261 return NULL;
2262
2263 if (obj)
2264 {
2265 PyObject *lookup_dict;
2266
2267 if (!(lookup_dict = PyDict_New()))
2268 {
2269 list_unref(list);
2270 return NULL;
2271 }
2272
2273 if (list_py_concat(list, obj, lookup_dict) == -1)
2274 {
2275 Py_DECREF(lookup_dict);
2276 list_unref(list);
2277 return NULL;
2278 }
2279
2280 Py_DECREF(lookup_dict);
2281 }
2282
2283 return ListNew(subtype, list);
2284}
2285
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002286 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002287ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002288{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002289 pyll_remove(&self->ref, &lastlist);
2290 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002291
2292 DESTRUCTOR_FINISH(self);
2293}
2294
Bram Moolenaardb913952012-06-29 12:54:53 +02002295 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002296ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002297{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002298 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002299}
2300
2301 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002302ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002303{
2304 listitem_T *li;
2305
Bram Moolenaard6e39182013-05-21 18:30:34 +02002306 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002307 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002308 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002309 return NULL;
2310 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002311 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002312 if (li == NULL)
2313 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002314 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002315 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002316 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002317 return NULL;
2318 }
2319 return ConvertToPyObject(&li->li_tv);
2320}
2321
Bram Moolenaardb913952012-06-29 12:54:53 +02002322 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002323ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2324 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002325{
2326 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002327 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002328
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002329 if (step == 0)
2330 {
2331 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2332 return NULL;
2333 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002334
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002335 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002336 if (list == NULL)
2337 return NULL;
2338
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002339 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002340 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002341 PyObject *item;
2342
2343 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002344 if (item == NULL)
2345 {
2346 Py_DECREF(list);
2347 return NULL;
2348 }
2349
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002350 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002351 }
2352
2353 return list;
2354}
2355
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002356 static PyObject *
2357ListItem(ListObject *self, PyObject* idx)
2358{
2359#if PY_MAJOR_VERSION < 3
2360 if (PyInt_Check(idx))
2361 {
2362 long _idx = PyInt_AsLong(idx);
2363 return ListIndex(self, _idx);
2364 }
2365 else
2366#endif
2367 if (PyLong_Check(idx))
2368 {
2369 long _idx = PyLong_AsLong(idx);
2370 return ListIndex(self, _idx);
2371 }
2372 else if (PySlice_Check(idx))
2373 {
2374 Py_ssize_t start, stop, step, slicelen;
2375
Bram Moolenaar922a4662014-03-30 16:11:43 +02002376 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002377 &start, &stop, &step, &slicelen) < 0)
2378 return NULL;
2379 return ListSlice(self, start, step, slicelen);
2380 }
2381 else
2382 {
2383 RAISE_INVALID_INDEX_TYPE(idx);
2384 return NULL;
2385 }
2386}
2387
2388 static void
2389list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2390 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2391{
2392 while (numreplaced--)
2393 {
2394 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2395 listitem_remove(l, lis[slicelen + numreplaced]);
2396 }
2397 while (numadded--)
2398 {
2399 listitem_T *next;
2400
2401 next = lastaddedli->li_prev;
2402 listitem_remove(l, lastaddedli);
2403 lastaddedli = next;
2404 }
2405}
2406
2407 static int
2408ListAssSlice(ListObject *self, Py_ssize_t first,
2409 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2410{
2411 PyObject *iterator;
2412 PyObject *item;
2413 listitem_T *li;
2414 listitem_T *lastaddedli = NULL;
2415 listitem_T *next;
2416 typval_T v;
2417 list_T *l = self->list;
2418 PyInt i;
2419 PyInt j;
2420 PyInt numreplaced = 0;
2421 PyInt numadded = 0;
2422 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002423 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002424
2425 size = ListLength(self);
2426
2427 if (l->lv_lock)
2428 {
2429 RAISE_LOCKED_LIST;
2430 return -1;
2431 }
2432
2433 if (step == 0)
2434 {
2435 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2436 return -1;
2437 }
2438
2439 if (step != 1 && slicelen == 0)
2440 {
2441 /* Nothing to do. Only error out if obj has some items. */
2442 int ret = 0;
2443
2444 if (obj == NULL)
2445 return 0;
2446
2447 if (!(iterator = PyObject_GetIter(obj)))
2448 return -1;
2449
2450 if ((item = PyIter_Next(iterator)))
2451 {
2452 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002453 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002454 "to extended slice"), 0);
2455 Py_DECREF(item);
2456 ret = -1;
2457 }
2458 Py_DECREF(iterator);
2459 return ret;
2460 }
2461
2462 if (obj != NULL)
2463 /* XXX May allocate zero bytes. */
2464 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2465 {
2466 PyErr_NoMemory();
2467 return -1;
2468 }
2469
2470 if (first == size)
2471 li = NULL;
2472 else
2473 {
2474 li = list_find(l, (long) first);
2475 if (li == NULL)
2476 {
2477 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2478 (int)first);
2479 if (obj != NULL)
2480 PyMem_Free(lis);
2481 return -1;
2482 }
2483 i = slicelen;
2484 while (i-- && li != NULL)
2485 {
2486 j = step;
2487 next = li;
2488 if (step > 0)
2489 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2490 else
2491 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2492
2493 if (obj == NULL)
2494 listitem_remove(l, li);
2495 else
2496 lis[slicelen - i - 1] = li;
2497
2498 li = next;
2499 }
2500 if (li == NULL && i != -1)
2501 {
2502 PyErr_SET_VIM(N_("internal error: not enough list items"));
2503 if (obj != NULL)
2504 PyMem_Free(lis);
2505 return -1;
2506 }
2507 }
2508
2509 if (obj == NULL)
2510 return 0;
2511
2512 if (!(iterator = PyObject_GetIter(obj)))
2513 {
2514 PyMem_Free(lis);
2515 return -1;
2516 }
2517
2518 i = 0;
2519 while ((item = PyIter_Next(iterator)))
2520 {
2521 if (ConvertFromPyObject(item, &v) == -1)
2522 {
2523 Py_DECREF(iterator);
2524 Py_DECREF(item);
2525 PyMem_Free(lis);
2526 return -1;
2527 }
2528 Py_DECREF(item);
2529 if (list_insert_tv(l, &v, numreplaced < slicelen
2530 ? lis[numreplaced]
2531 : li) == FAIL)
2532 {
2533 clear_tv(&v);
2534 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2535 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2536 PyMem_Free(lis);
2537 return -1;
2538 }
2539 if (numreplaced < slicelen)
2540 {
2541 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002542 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002543 numreplaced++;
2544 }
2545 else
2546 {
2547 if (li)
2548 lastaddedli = li->li_prev;
2549 else
2550 lastaddedli = l->lv_last;
2551 numadded++;
2552 }
2553 clear_tv(&v);
2554 if (step != 1 && i >= slicelen)
2555 {
2556 Py_DECREF(iterator);
2557 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002558 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002559 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002560 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2561 PyMem_Free(lis);
2562 return -1;
2563 }
2564 ++i;
2565 }
2566 Py_DECREF(iterator);
2567
2568 if (step != 1 && i != slicelen)
2569 {
2570 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002571 N_("attempt to assign sequence of size %d to extended slice "
2572 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002573 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2574 PyMem_Free(lis);
2575 return -1;
2576 }
2577
2578 if (PyErr_Occurred())
2579 {
2580 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2581 PyMem_Free(lis);
2582 return -1;
2583 }
2584
2585 for (i = 0; i < numreplaced; i++)
2586 listitem_free(lis[i]);
2587 if (step == 1)
2588 for (i = numreplaced; i < slicelen; i++)
2589 listitem_remove(l, lis[i]);
2590
2591 PyMem_Free(lis);
2592
2593 return 0;
2594}
2595
2596 static int
2597ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2598{
2599 typval_T tv;
2600 list_T *l = self->list;
2601 listitem_T *li;
2602 Py_ssize_t length = ListLength(self);
2603
2604 if (l->lv_lock)
2605 {
2606 RAISE_LOCKED_LIST;
2607 return -1;
2608 }
2609 if (index > length || (index == length && obj == NULL))
2610 {
2611 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2612 return -1;
2613 }
2614
2615 if (obj == NULL)
2616 {
2617 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002618 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002619 clear_tv(&li->li_tv);
2620 vim_free(li);
2621 return 0;
2622 }
2623
2624 if (ConvertFromPyObject(obj, &tv) == -1)
2625 return -1;
2626
2627 if (index == length)
2628 {
2629 if (list_append_tv(l, &tv) == FAIL)
2630 {
2631 clear_tv(&tv);
2632 PyErr_SET_VIM(N_("failed to add item to list"));
2633 return -1;
2634 }
2635 }
2636 else
2637 {
2638 li = list_find(l, (long) index);
2639 clear_tv(&li->li_tv);
2640 copy_tv(&tv, &li->li_tv);
2641 clear_tv(&tv);
2642 }
2643 return 0;
2644}
2645
2646 static Py_ssize_t
2647ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2648{
2649#if PY_MAJOR_VERSION < 3
2650 if (PyInt_Check(idx))
2651 {
2652 long _idx = PyInt_AsLong(idx);
2653 return ListAssIndex(self, _idx, obj);
2654 }
2655 else
2656#endif
2657 if (PyLong_Check(idx))
2658 {
2659 long _idx = PyLong_AsLong(idx);
2660 return ListAssIndex(self, _idx, obj);
2661 }
2662 else if (PySlice_Check(idx))
2663 {
2664 Py_ssize_t start, stop, step, slicelen;
2665
Bram Moolenaar922a4662014-03-30 16:11:43 +02002666 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002667 &start, &stop, &step, &slicelen) < 0)
2668 return -1;
2669 return ListAssSlice(self, start, step, slicelen,
2670 obj);
2671 }
2672 else
2673 {
2674 RAISE_INVALID_INDEX_TYPE(idx);
2675 return -1;
2676 }
2677}
2678
2679 static PyObject *
2680ListConcatInPlace(ListObject *self, PyObject *obj)
2681{
2682 list_T *l = self->list;
2683 PyObject *lookup_dict;
2684
2685 if (l->lv_lock)
2686 {
2687 RAISE_LOCKED_LIST;
2688 return NULL;
2689 }
2690
2691 if (!(lookup_dict = PyDict_New()))
2692 return NULL;
2693
2694 if (list_py_concat(l, obj, lookup_dict) == -1)
2695 {
2696 Py_DECREF(lookup_dict);
2697 return NULL;
2698 }
2699 Py_DECREF(lookup_dict);
2700
2701 Py_INCREF(self);
2702 return (PyObject *)(self);
2703}
2704
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002705typedef struct
2706{
2707 listwatch_T lw;
2708 list_T *list;
2709} listiterinfo_T;
2710
2711 static void
2712ListIterDestruct(listiterinfo_T *lii)
2713{
2714 list_rem_watch(lii->list, &lii->lw);
2715 PyMem_Free(lii);
2716}
2717
2718 static PyObject *
2719ListIterNext(listiterinfo_T **lii)
2720{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002721 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002722
2723 if (!((*lii)->lw.lw_item))
2724 return NULL;
2725
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002726 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002727 return NULL;
2728
2729 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2730
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002731 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002732}
2733
2734 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002735ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002736{
2737 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002738 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002739
2740 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2741 {
2742 PyErr_NoMemory();
2743 return NULL;
2744 }
2745
2746 list_add_watch(l, &lii->lw);
2747 lii->lw.lw_item = l->lv_first;
2748 lii->list = l;
2749
2750 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002751 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2752 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002753}
2754
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002755static char *ListAttrs[] = {
2756 "locked",
2757 NULL
2758};
2759
2760 static PyObject *
2761ListDir(PyObject *self)
2762{
2763 return ObjectDir(self, ListAttrs);
2764}
2765
Bram Moolenaar66b79852012-09-21 14:00:35 +02002766 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002767ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002768{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002769 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002770 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002771 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002772 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002773 return -1;
2774 }
2775
2776 if (strcmp(name, "locked") == 0)
2777 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002778 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002779 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002780 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002781 return -1;
2782 }
2783 else
2784 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002785 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002786 if (istrue == -1)
2787 return -1;
2788 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002789 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002790 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002791 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002792 }
2793 return 0;
2794 }
2795 else
2796 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002797 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002798 return -1;
2799 }
2800}
2801
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002802static PySequenceMethods ListAsSeq = {
2803 (lenfunc) ListLength, /* sq_length, len(x) */
2804 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2805 0, /* RangeRepeat, sq_repeat, x*n */
2806 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2807 0, /* was_sq_slice, x[i:j] */
2808 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2809 0, /* was_sq_ass_slice, x[i:j]=v */
2810 0, /* sq_contains */
2811 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2812 0, /* sq_inplace_repeat */
2813};
2814
2815static PyMappingMethods ListAsMapping = {
2816 /* mp_length */ (lenfunc) ListLength,
2817 /* mp_subscript */ (binaryfunc) ListItem,
2818 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2819};
2820
Bram Moolenaardb913952012-06-29 12:54:53 +02002821static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002822 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2823 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2824 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002825};
2826
2827typedef struct
2828{
2829 PyObject_HEAD
2830 char_u *name;
2831} FunctionObject;
2832
2833static PyTypeObject FunctionType;
2834
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002835#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2836
Bram Moolenaardb913952012-06-29 12:54:53 +02002837 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002838FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002839{
2840 FunctionObject *self;
2841
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002842 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2843
Bram Moolenaardb913952012-06-29 12:54:53 +02002844 if (self == NULL)
2845 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002846
2847 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002848 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002849 if (!translated_function_exists(name))
2850 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002851 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002852 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002853 return NULL;
2854 }
2855 self->name = vim_strsave(name);
2856 func_ref(self->name);
2857 }
2858 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002859 if ((self->name = get_expanded_name(name,
2860 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2861 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002862 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002863 PyErr_FORMAT(PyExc_ValueError,
2864 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002865 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002866 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002867
2868 return (PyObject *)(self);
2869}
2870
2871 static PyObject *
2872FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2873{
2874 PyObject *self;
2875 char_u *name;
2876
2877 if (kwargs)
2878 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002879 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002880 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002881 return NULL;
2882 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002883
Bram Moolenaar389a1792013-06-23 13:00:44 +02002884 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002885 return NULL;
2886
2887 self = FunctionNew(subtype, name);
2888
Bram Moolenaar389a1792013-06-23 13:00:44 +02002889 PyMem_Free(name);
2890
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002891 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002892}
2893
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002894 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002895FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002896{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002897 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002898 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002899
2900 DESTRUCTOR_FINISH(self);
2901}
2902
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002903static char *FunctionAttrs[] = {
2904 "softspace",
2905 NULL
2906};
2907
2908 static PyObject *
2909FunctionDir(PyObject *self)
2910{
2911 return ObjectDir(self, FunctionAttrs);
2912}
2913
Bram Moolenaardb913952012-06-29 12:54:53 +02002914 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002915FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002916{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002917 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002918 typval_T args;
2919 typval_T selfdicttv;
2920 typval_T rettv;
2921 dict_T *selfdict = NULL;
2922 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002923 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002924 int error;
2925
2926 if (ConvertFromPyObject(argsObject, &args) == -1)
2927 return NULL;
2928
2929 if (kwargs != NULL)
2930 {
2931 selfdictObject = PyDict_GetItemString(kwargs, "self");
2932 if (selfdictObject != NULL)
2933 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002934 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002935 {
2936 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002937 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002938 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002939 selfdict = selfdicttv.vval.v_dict;
2940 }
2941 }
2942
Bram Moolenaar71700b82013-05-15 17:49:05 +02002943 Py_BEGIN_ALLOW_THREADS
2944 Python_Lock_Vim();
2945
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002946 VimTryStart();
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002947 error = func_call(name, &args, NULL, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002948
2949 Python_Release_Vim();
2950 Py_END_ALLOW_THREADS
2951
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002952 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002953 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002954 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002955 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002956 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002957 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002958 }
2959 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002960 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002961
Bram Moolenaardb913952012-06-29 12:54:53 +02002962 clear_tv(&args);
2963 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002964 if (selfdict != NULL)
2965 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002966
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002967 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002968}
2969
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002970 static PyObject *
2971FunctionRepr(FunctionObject *self)
2972{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002973#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002974 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002975 * Finalize */
2976 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002977 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002978#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002979 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002980#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002981}
2982
Bram Moolenaardb913952012-06-29 12:54:53 +02002983static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002984 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2985 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002986};
2987
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002988/*
2989 * Options object
2990 */
2991
2992static PyTypeObject OptionsType;
2993
2994typedef int (*checkfun)(void *);
2995
2996typedef struct
2997{
2998 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01002999 int opt_type;
3000 void *from;
3001 checkfun Check;
3002 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003003} OptionsObject;
3004
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003005 static int
3006dummy_check(void *arg UNUSED)
3007{
3008 return 0;
3009}
3010
3011 static PyObject *
3012OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
3013{
3014 OptionsObject *self;
3015
Bram Moolenaar774267b2013-05-21 20:51:59 +02003016 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003017 if (self == NULL)
3018 return NULL;
3019
3020 self->opt_type = opt_type;
3021 self->from = from;
3022 self->Check = Check;
3023 self->fromObj = fromObj;
3024 if (fromObj)
3025 Py_INCREF(fromObj);
3026
3027 return (PyObject *)(self);
3028}
3029
3030 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003031OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003032{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003033 PyObject_GC_UnTrack((void *)(self));
3034 Py_XDECREF(self->fromObj);
3035 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003036}
3037
3038 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003039OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003040{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003041 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003042 return 0;
3043}
3044
3045 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003046OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003047{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003048 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003049 return 0;
3050}
3051
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003052 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003053OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003054{
3055 char_u *key;
3056 int flags;
3057 long numval;
3058 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003059 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003060
Bram Moolenaard6e39182013-05-21 18:30:34 +02003061 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003062 return NULL;
3063
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003064 if (!(key = StringToChars(keyObject, &todecref)))
3065 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003066
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003067 if (*key == NUL)
3068 {
3069 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003070 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003071 return NULL;
3072 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003073
3074 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003075 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003076
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003077 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003078
3079 if (flags == 0)
3080 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003081 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003082 return NULL;
3083 }
3084
3085 if (flags & SOPT_UNSET)
3086 {
3087 Py_INCREF(Py_None);
3088 return Py_None;
3089 }
3090 else if (flags & SOPT_BOOL)
3091 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003092 PyObject *ret;
3093 ret = numval ? Py_True : Py_False;
3094 Py_INCREF(ret);
3095 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003096 }
3097 else if (flags & SOPT_NUM)
3098 return PyInt_FromLong(numval);
3099 else if (flags & SOPT_STRING)
3100 {
3101 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003102 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003103 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003104 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003105 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003106 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003107 else
3108 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003109 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003110 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003111 return NULL;
3112 }
3113 }
3114 else
3115 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003116 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003117 return NULL;
3118 }
3119}
3120
3121 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003122OptionsContains(OptionsObject *self, PyObject *keyObject)
3123{
3124 char_u *key;
3125 PyObject *todecref;
3126
3127 if (!(key = StringToChars(keyObject, &todecref)))
3128 return -1;
3129
3130 if (*key == NUL)
3131 {
3132 Py_XDECREF(todecref);
3133 return 0;
3134 }
3135
3136 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3137 {
3138 Py_XDECREF(todecref);
3139 return 1;
3140 }
3141 else
3142 {
3143 Py_XDECREF(todecref);
3144 return 0;
3145 }
3146}
3147
3148typedef struct
3149{
3150 void *lastoption;
3151 int opt_type;
3152} optiterinfo_T;
3153
3154 static PyObject *
3155OptionsIterNext(optiterinfo_T **oii)
3156{
3157 char_u *name;
3158
3159 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3160 return PyString_FromString((char *)name);
3161
3162 return NULL;
3163}
3164
3165 static PyObject *
3166OptionsIter(OptionsObject *self)
3167{
3168 optiterinfo_T *oii;
3169
3170 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3171 {
3172 PyErr_NoMemory();
3173 return NULL;
3174 }
3175
3176 oii->opt_type = self->opt_type;
3177 oii->lastoption = NULL;
3178
3179 return IterNew(oii,
3180 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3181 NULL, NULL);
3182}
3183
3184 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003185set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003186{
3187 char_u *errmsg;
3188
3189 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3190 {
3191 if (VimTryEnd())
3192 return FAIL;
3193 PyErr_SetVim((char *)errmsg);
3194 return FAIL;
3195 }
3196 return OK;
3197}
3198
3199 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003200set_option_value_for(
3201 char_u *key,
3202 int numval,
3203 char_u *stringval,
3204 int opt_flags,
3205 int opt_type,
3206 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003207{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003208 win_T *save_curwin = NULL;
3209 tabpage_T *save_curtab = NULL;
3210 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003211 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003212
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003213 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003214 switch (opt_type)
3215 {
3216 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003217 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003218 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003219 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003220 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003221 if (VimTryEnd())
3222 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003223 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003224 return -1;
3225 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003226 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3227 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003228 break;
3229 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003230 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003231 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003232 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003233 break;
3234 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003235 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003236 break;
3237 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003238 if (set_ret == FAIL)
3239 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003240 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003241}
3242
3243 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003244OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003245{
3246 char_u *key;
3247 int flags;
3248 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003249 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003250 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003251
Bram Moolenaard6e39182013-05-21 18:30:34 +02003252 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003253 return -1;
3254
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003255 if (!(key = StringToChars(keyObject, &todecref)))
3256 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003257
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003258 if (*key == NUL)
3259 {
3260 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003261 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003262 return -1;
3263 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003264
3265 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003266 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003267
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003268 if (flags == 0)
3269 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003270 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003271 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003272 return -1;
3273 }
3274
3275 if (valObject == NULL)
3276 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003277 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003278 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003279 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003280 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003281 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003282 return -1;
3283 }
3284 else if (!(flags & SOPT_GLOBAL))
3285 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003286 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003287 N_("unable to unset option %s "
3288 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003289 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003290 return -1;
3291 }
3292 else
3293 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003294 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003295 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003296 return 0;
3297 }
3298 }
3299
Bram Moolenaard6e39182013-05-21 18:30:34 +02003300 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003301
3302 if (flags & SOPT_BOOL)
3303 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003304 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003305
Bram Moolenaarb983f752013-05-15 16:11:50 +02003306 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003307 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003308 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003309 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003310 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003311 }
3312 else if (flags & SOPT_NUM)
3313 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003314 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003315
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003316 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003317 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003318 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003319 return -1;
3320 }
3321
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003322 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003323 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003324 }
3325 else
3326 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003327 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003328 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003329
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003330 if ((val = StringToChars(valObject, &todecref2)))
3331 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003332 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003333 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003334 Py_XDECREF(todecref2);
3335 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003336 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003337 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003338 }
3339
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003340 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003341
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003342 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003343}
3344
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003345static PySequenceMethods OptionsAsSeq = {
3346 0, /* sq_length */
3347 0, /* sq_concat */
3348 0, /* sq_repeat */
3349 0, /* sq_item */
3350 0, /* sq_slice */
3351 0, /* sq_ass_item */
3352 0, /* sq_ass_slice */
3353 (objobjproc) OptionsContains, /* sq_contains */
3354 0, /* sq_inplace_concat */
3355 0, /* sq_inplace_repeat */
3356};
3357
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003358static PyMappingMethods OptionsAsMapping = {
3359 (lenfunc) NULL,
3360 (binaryfunc) OptionsItem,
3361 (objobjargproc) OptionsAssItem,
3362};
3363
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003364/* Tabpage object
3365 */
3366
3367typedef struct
3368{
3369 PyObject_HEAD
3370 tabpage_T *tab;
3371} TabPageObject;
3372
3373static PyObject *WinListNew(TabPageObject *tabObject);
3374
3375static PyTypeObject TabPageType;
3376
3377 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003378CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003379{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003380 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003381 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003382 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003383 return -1;
3384 }
3385
3386 return 0;
3387}
3388
3389 static PyObject *
3390TabPageNew(tabpage_T *tab)
3391{
3392 TabPageObject *self;
3393
3394 if (TAB_PYTHON_REF(tab))
3395 {
3396 self = TAB_PYTHON_REF(tab);
3397 Py_INCREF(self);
3398 }
3399 else
3400 {
3401 self = PyObject_NEW(TabPageObject, &TabPageType);
3402 if (self == NULL)
3403 return NULL;
3404 self->tab = tab;
3405 TAB_PYTHON_REF(tab) = self;
3406 }
3407
3408 return (PyObject *)(self);
3409}
3410
3411 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003412TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003413{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003414 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3415 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003416
3417 DESTRUCTOR_FINISH(self);
3418}
3419
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003420static char *TabPageAttrs[] = {
3421 "windows", "number", "vars", "window", "valid",
3422 NULL
3423};
3424
3425 static PyObject *
3426TabPageDir(PyObject *self)
3427{
3428 return ObjectDir(self, TabPageAttrs);
3429}
3430
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003431 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003432TabPageAttrValid(TabPageObject *self, char *name)
3433{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003434 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003435
3436 if (strcmp(name, "valid") != 0)
3437 return NULL;
3438
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003439 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3440 Py_INCREF(ret);
3441 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003442}
3443
3444 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003445TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003446{
3447 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003448 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003449 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003450 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003451 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003452 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003453 else if (strcmp(name, "window") == 0)
3454 {
3455 /* For current tab window.c does not bother to set or update tp_curwin
3456 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003457 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003458 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003459 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003460 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003461 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003462 else if (strcmp(name, "__members__") == 0)
3463 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003464 return NULL;
3465}
3466
3467 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003468TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003469{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003470 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003471 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003472 else
3473 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003474 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003475
3476 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003477 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3478 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003479 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003480 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003481 }
3482}
3483
3484static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003485 /* name, function, calling, documentation */
3486 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3487 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003488};
3489
3490/*
3491 * Window list object
3492 */
3493
3494static PyTypeObject TabListType;
3495static PySequenceMethods TabListAsSeq;
3496
3497typedef struct
3498{
3499 PyObject_HEAD
3500} TabListObject;
3501
3502 static PyInt
3503TabListLength(PyObject *self UNUSED)
3504{
3505 tabpage_T *tp = first_tabpage;
3506 PyInt n = 0;
3507
3508 while (tp != NULL)
3509 {
3510 ++n;
3511 tp = tp->tp_next;
3512 }
3513
3514 return n;
3515}
3516
3517 static PyObject *
3518TabListItem(PyObject *self UNUSED, PyInt n)
3519{
3520 tabpage_T *tp;
3521
3522 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3523 if (n == 0)
3524 return TabPageNew(tp);
3525
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003526 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003527 return NULL;
3528}
3529
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003530/*
3531 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003532 */
3533
3534typedef struct
3535{
3536 PyObject_HEAD
3537 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003538 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003539} WindowObject;
3540
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003541static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003542
3543 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003544CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003545{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003546 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003547 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003548 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003549 return -1;
3550 }
3551
3552 return 0;
3553}
3554
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003555 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003556WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003557{
3558 /* We need to handle deletion of windows underneath us.
3559 * If we add a "w_python*_ref" field to the win_T structure,
3560 * then we can get at it in win_free() in vim. We then
3561 * need to create only ONE Python object per window - if
3562 * we try to create a second, just INCREF the existing one
3563 * and return it. The (single) Python object referring to
3564 * the window is stored in "w_python*_ref".
3565 * On a win_free() we set the Python object's win_T* field
3566 * to an invalid value. We trap all uses of a window
3567 * object, and reject them if the win_T* field is invalid.
3568 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003569 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003570 * w_python_ref and w_python3_ref fields respectively.
3571 */
3572
3573 WindowObject *self;
3574
3575 if (WIN_PYTHON_REF(win))
3576 {
3577 self = WIN_PYTHON_REF(win);
3578 Py_INCREF(self);
3579 }
3580 else
3581 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003582 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003583 if (self == NULL)
3584 return NULL;
3585 self->win = win;
3586 WIN_PYTHON_REF(win) = self;
3587 }
3588
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003589 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3590
Bram Moolenaar971db462013-05-12 18:44:48 +02003591 return (PyObject *)(self);
3592}
3593
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003594 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003595WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003596{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003597 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003598 if (self->win && self->win != INVALID_WINDOW_VALUE)
3599 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003600 Py_XDECREF(((PyObject *)(self->tabObject)));
3601 PyObject_GC_Del((void *)(self));
3602}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003603
Bram Moolenaar774267b2013-05-21 20:51:59 +02003604 static int
3605WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3606{
3607 Py_VISIT(((PyObject *)(self->tabObject)));
3608 return 0;
3609}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003610
Bram Moolenaar774267b2013-05-21 20:51:59 +02003611 static int
3612WindowClear(WindowObject *self)
3613{
3614 Py_CLEAR(self->tabObject);
3615 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003616}
3617
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003618 static win_T *
3619get_firstwin(TabPageObject *tabObject)
3620{
3621 if (tabObject)
3622 {
3623 if (CheckTabPage(tabObject))
3624 return NULL;
3625 /* For current tab window.c does not bother to set or update tp_firstwin
3626 */
3627 else if (tabObject->tab == curtab)
3628 return firstwin;
3629 else
3630 return tabObject->tab->tp_firstwin;
3631 }
3632 else
3633 return firstwin;
3634}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003635static char *WindowAttrs[] = {
3636 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3637 "tabpage", "valid",
3638 NULL
3639};
3640
3641 static PyObject *
3642WindowDir(PyObject *self)
3643{
3644 return ObjectDir(self, WindowAttrs);
3645}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003646
Bram Moolenaar971db462013-05-12 18:44:48 +02003647 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003648WindowAttrValid(WindowObject *self, char *name)
3649{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003650 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003651
3652 if (strcmp(name, "valid") != 0)
3653 return NULL;
3654
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003655 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3656 Py_INCREF(ret);
3657 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003658}
3659
3660 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003661WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003662{
3663 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003664 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003665 else if (strcmp(name, "cursor") == 0)
3666 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003667 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003668
3669 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3670 }
3671 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003672 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003673#ifdef FEAT_WINDOWS
3674 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003675 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003676 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003677 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003678 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003679 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003680#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003681 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003682 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003683 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003684 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3685 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003686 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003687 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003688 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003689 return NULL;
3690 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003691 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003692 }
3693 else if (strcmp(name, "tabpage") == 0)
3694 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003695 Py_INCREF(self->tabObject);
3696 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003697 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003698 else if (strcmp(name, "__members__") == 0)
3699 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003700 else
3701 return NULL;
3702}
3703
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003704 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003705WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003706{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003707 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003708 return -1;
3709
3710 if (strcmp(name, "buffer") == 0)
3711 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003712 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003713 return -1;
3714 }
3715 else if (strcmp(name, "cursor") == 0)
3716 {
3717 long lnum;
3718 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003719
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003720 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003721 return -1;
3722
Bram Moolenaard6e39182013-05-21 18:30:34 +02003723 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003724 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003725 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003726 return -1;
3727 }
3728
3729 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003730 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003731 return -1;
3732
Bram Moolenaard6e39182013-05-21 18:30:34 +02003733 self->win->w_cursor.lnum = lnum;
3734 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003735#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003736 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003737#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003738 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003739 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003740
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003741 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003742 return 0;
3743 }
3744 else if (strcmp(name, "height") == 0)
3745 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003746 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003747 win_T *savewin;
3748
Bram Moolenaardee2e312013-06-23 16:35:47 +02003749 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003750 return -1;
3751
3752#ifdef FEAT_GUI
3753 need_mouse_correct = TRUE;
3754#endif
3755 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003756 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003757
3758 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003759 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003760 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003761 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003762 return -1;
3763
3764 return 0;
3765 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003766#ifdef FEAT_WINDOWS
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003767 else if (strcmp(name, "width") == 0)
3768 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003769 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003770 win_T *savewin;
3771
Bram Moolenaardee2e312013-06-23 16:35:47 +02003772 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003773 return -1;
3774
3775#ifdef FEAT_GUI
3776 need_mouse_correct = TRUE;
3777#endif
3778 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003779 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003780
3781 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003782 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003783 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003784 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003785 return -1;
3786
3787 return 0;
3788 }
3789#endif
3790 else
3791 {
3792 PyErr_SetString(PyExc_AttributeError, name);
3793 return -1;
3794 }
3795}
3796
3797 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003798WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003799{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003800 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003801 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003802 else
3803 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003804 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003805
Bram Moolenaar6d216452013-05-12 19:00:41 +02003806 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003807 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003808 (self));
3809 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003810 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003811 }
3812}
3813
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003814static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003815 /* name, function, calling, documentation */
3816 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3817 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003818};
3819
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003820/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003821 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003822 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003823
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003824static PyTypeObject WinListType;
3825static PySequenceMethods WinListAsSeq;
3826
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003827typedef struct
3828{
3829 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003830 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003831} WinListObject;
3832
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003833 static PyObject *
3834WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003835{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003836 WinListObject *self;
3837
3838 self = PyObject_NEW(WinListObject, &WinListType);
3839 self->tabObject = tabObject;
3840 Py_INCREF(tabObject);
3841
3842 return (PyObject *)(self);
3843}
3844
3845 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003846WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003847{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003848 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003849
3850 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003851 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003852 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003853 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003854
3855 DESTRUCTOR_FINISH(self);
3856}
3857
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003858 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003859WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003860{
3861 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003862 PyInt n = 0;
3863
Bram Moolenaard6e39182013-05-21 18:30:34 +02003864 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003865 return -1;
3866
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003867 while (w != NULL)
3868 {
3869 ++n;
3870 w = W_NEXT(w);
3871 }
3872
3873 return n;
3874}
3875
3876 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003877WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003878{
3879 win_T *w;
3880
Bram Moolenaard6e39182013-05-21 18:30:34 +02003881 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003882 return NULL;
3883
3884 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003885 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003886 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003887
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003888 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003889 return NULL;
3890}
3891
3892/* Convert a Python string into a Vim line.
3893 *
3894 * The result is in allocated memory. All internal nulls are replaced by
3895 * newline characters. It is an error for the string to contain newline
3896 * characters.
3897 *
3898 * On errors, the Python exception data is set, and NULL is returned.
3899 */
3900 static char *
3901StringToLine(PyObject *obj)
3902{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003903 char *str;
3904 char *save;
3905 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003906 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003907 PyInt i;
3908 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003909
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003910 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003911 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003912 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3913 || str == NULL)
3914 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003915 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003916 else if (PyUnicode_Check(obj))
3917 {
3918 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3919 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003920
Bram Moolenaardaa27022013-06-24 22:33:30 +02003921 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003922 || str == NULL)
3923 {
3924 Py_DECREF(bytes);
3925 return NULL;
3926 }
3927 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003928 else
3929 {
3930#if PY_MAJOR_VERSION < 3
3931 PyErr_FORMAT(PyExc_TypeError,
3932 N_("expected str() or unicode() instance, but got %s"),
3933 Py_TYPE_NAME(obj));
3934#else
3935 PyErr_FORMAT(PyExc_TypeError,
3936 N_("expected bytes() or str() instance, but got %s"),
3937 Py_TYPE_NAME(obj));
3938#endif
3939 return NULL;
3940 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003941
3942 /*
3943 * Error checking: String must not contain newlines, as we
3944 * are replacing a single line, and we must replace it with
3945 * a single line.
3946 * A trailing newline is removed, so that append(f.readlines()) works.
3947 */
3948 p = memchr(str, '\n', len);
3949 if (p != NULL)
3950 {
3951 if (p == str + len - 1)
3952 --len;
3953 else
3954 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003955 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003956 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003957 return NULL;
3958 }
3959 }
3960
3961 /* Create a copy of the string, with internal nulls replaced by
3962 * newline characters, as is the vim convention.
3963 */
3964 save = (char *)alloc((unsigned)(len+1));
3965 if (save == NULL)
3966 {
3967 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003968 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003969 return NULL;
3970 }
3971
3972 for (i = 0; i < len; ++i)
3973 {
3974 if (str[i] == '\0')
3975 save[i] = '\n';
3976 else
3977 save[i] = str[i];
3978 }
3979
3980 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003981 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003982
3983 return save;
3984}
3985
3986/* Get a line from the specified buffer. The line number is
3987 * in Vim format (1-based). The line is returned as a Python
3988 * string object.
3989 */
3990 static PyObject *
3991GetBufferLine(buf_T *buf, PyInt n)
3992{
3993 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3994}
3995
3996
3997/* Get a list of lines from the specified buffer. The line numbers
3998 * are in Vim format (1-based). The range is from lo up to, but not
3999 * including, hi. The list is returned as a Python list of string objects.
4000 */
4001 static PyObject *
4002GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
4003{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004004 PyInt i;
4005 PyInt n = hi - lo;
4006 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004007
4008 if (list == NULL)
4009 return NULL;
4010
4011 for (i = 0; i < n; ++i)
4012 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004013 PyObject *string = LineToString(
4014 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004015
4016 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004017 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004018 {
4019 Py_DECREF(list);
4020 return NULL;
4021 }
4022
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004023 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004024 }
4025
4026 /* The ownership of the Python list is passed to the caller (ie,
4027 * the caller should Py_DECREF() the object when it is finished
4028 * with it).
4029 */
4030
4031 return list;
4032}
4033
4034/*
4035 * Check if deleting lines made the cursor position invalid.
4036 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4037 * deleted).
4038 */
4039 static void
4040py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4041{
4042 if (curwin->w_cursor.lnum >= lo)
4043 {
4044 /* Adjust the cursor position if it's in/after the changed
4045 * lines. */
4046 if (curwin->w_cursor.lnum >= hi)
4047 {
4048 curwin->w_cursor.lnum += extra;
4049 check_cursor_col();
4050 }
4051 else if (extra < 0)
4052 {
4053 curwin->w_cursor.lnum = lo;
4054 check_cursor();
4055 }
4056 else
4057 check_cursor_col();
4058 changed_cline_bef_curs();
4059 }
4060 invalidate_botline();
4061}
4062
Bram Moolenaar19e60942011-06-19 00:27:51 +02004063/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004064 * Find a window that contains "buf" and switch to it.
4065 * If there is no such window, use the current window and change "curbuf".
4066 * Caller must initialize save_curbuf to NULL.
4067 * restore_win_for_buf() MUST be called later!
4068 */
4069 static void
4070switch_to_win_for_buf(
4071 buf_T *buf,
4072 win_T **save_curwinp,
4073 tabpage_T **save_curtabp,
4074 buf_T **save_curbufp)
4075{
4076 win_T *wp;
4077 tabpage_T *tp;
4078
Bram Moolenaarae38d052014-12-17 14:46:09 +01004079 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004080 switch_buffer(save_curbufp, buf);
Bram Moolenaarae38d052014-12-17 14:46:09 +01004081 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4082 {
4083 restore_win(*save_curwinp, *save_curtabp, TRUE);
4084 switch_buffer(save_curbufp, buf);
4085 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004086}
4087
4088 static void
4089restore_win_for_buf(
4090 win_T *save_curwin,
4091 tabpage_T *save_curtab,
4092 buf_T *save_curbuf)
4093{
4094 if (save_curbuf == NULL)
4095 restore_win(save_curwin, save_curtab, TRUE);
4096 else
4097 restore_buffer(save_curbuf);
4098}
4099
4100/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004101 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004102 * in Vim format (1-based). The replacement line is given as
4103 * a Python string object. The object is checked for validity
4104 * and correct format. Errors are returned as a value of FAIL.
4105 * The return value is OK on success.
4106 * If OK is returned and len_change is not NULL, *len_change
4107 * is set to the change in the buffer length.
4108 */
4109 static int
4110SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4111{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004112 buf_T *save_curbuf = NULL;
4113 win_T *save_curwin = NULL;
4114 tabpage_T *save_curtab = NULL;
4115
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004116 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004117 * There are three cases:
4118 * 1. NULL, or None - this is a deletion.
4119 * 2. A string - this is a replacement.
4120 * 3. Anything else - this is an error.
4121 */
4122 if (line == Py_None || line == NULL)
4123 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004124 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004125 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004126
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004127 VimTryStart();
4128
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004129 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004130 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004131 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004132 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004133 else
4134 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004135 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004136 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004137 if (save_curbuf == NULL)
4138 /* Only adjust marks if we managed to switch to a window that
4139 * holds the buffer, otherwise line numbers will be invalid. */
4140 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004141 }
4142
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004143 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004144
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004145 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004146 return FAIL;
4147
4148 if (len_change)
4149 *len_change = -1;
4150
4151 return OK;
4152 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004153 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004154 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004155 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004156
4157 if (save == NULL)
4158 return FAIL;
4159
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004160 VimTryStart();
4161
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004162 /* We do not need to free "save" if ml_replace() consumes it. */
4163 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004164 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004165
4166 if (u_savesub((linenr_T)n) == FAIL)
4167 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004168 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004169 vim_free(save);
4170 }
4171 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4172 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004173 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004174 vim_free(save);
4175 }
4176 else
4177 changed_bytes((linenr_T)n, 0);
4178
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004179 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004180
4181 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004182 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004183 check_cursor_col();
4184
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004185 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004186 return FAIL;
4187
4188 if (len_change)
4189 *len_change = 0;
4190
4191 return OK;
4192 }
4193 else
4194 {
4195 PyErr_BadArgument();
4196 return FAIL;
4197 }
4198}
4199
Bram Moolenaar19e60942011-06-19 00:27:51 +02004200/* Replace a range of lines in the specified buffer. The line numbers are in
4201 * Vim format (1-based). The range is from lo up to, but not including, hi.
4202 * The replacement lines are given as a Python list of string objects. The
4203 * list is checked for validity and correct format. Errors are returned as a
4204 * value of FAIL. The return value is OK on success.
4205 * If OK is returned and len_change is not NULL, *len_change
4206 * is set to the change in the buffer length.
4207 */
4208 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004209SetBufferLineList(
4210 buf_T *buf,
4211 PyInt lo,
4212 PyInt hi,
4213 PyObject *list,
4214 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004215{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004216 buf_T *save_curbuf = NULL;
4217 win_T *save_curwin = NULL;
4218 tabpage_T *save_curtab = NULL;
4219
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004220 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004221 * There are three cases:
4222 * 1. NULL, or None - this is a deletion.
4223 * 2. A list - this is a replacement.
4224 * 3. Anything else - this is an error.
4225 */
4226 if (list == Py_None || list == NULL)
4227 {
4228 PyInt i;
4229 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004230
4231 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004232 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004233 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004234
4235 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004236 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004237 else
4238 {
4239 for (i = 0; i < n; ++i)
4240 {
4241 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4242 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004243 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004244 break;
4245 }
4246 }
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004247 if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL))
4248 /* Using an existing window for the buffer, adjust the cursor
4249 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004250 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004251 if (save_curbuf == NULL)
4252 /* Only adjust marks if we managed to switch to a window that
4253 * holds the buffer, otherwise line numbers will be invalid. */
4254 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004255 }
4256
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004257 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004258
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004259 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004260 return FAIL;
4261
4262 if (len_change)
4263 *len_change = -n;
4264
4265 return OK;
4266 }
4267 else if (PyList_Check(list))
4268 {
4269 PyInt i;
4270 PyInt new_len = PyList_Size(list);
4271 PyInt old_len = hi - lo;
4272 PyInt extra = 0; /* lines added to text, can be negative */
4273 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004274
4275 if (new_len == 0) /* avoid allocating zero bytes */
4276 array = NULL;
4277 else
4278 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004279 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004280 if (array == NULL)
4281 {
4282 PyErr_NoMemory();
4283 return FAIL;
4284 }
4285 }
4286
4287 for (i = 0; i < new_len; ++i)
4288 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004289 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004290
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004291 if (!(line = PyList_GetItem(list, i)) ||
4292 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004293 {
4294 while (i)
4295 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004296 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004297 return FAIL;
4298 }
4299 }
4300
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004301 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004302 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004303
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004304 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004305 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004306
4307 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004308 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004309
4310 /* If the size of the range is reducing (ie, new_len < old_len) we
4311 * need to delete some old_len. We do this at the start, by
4312 * repeatedly deleting line "lo".
4313 */
4314 if (!PyErr_Occurred())
4315 {
4316 for (i = 0; i < old_len - new_len; ++i)
4317 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4318 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004319 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004320 break;
4321 }
4322 extra -= i;
4323 }
4324
4325 /* For as long as possible, replace the existing old_len with the
4326 * new old_len. This is a more efficient operation, as it requires
4327 * less memory allocation and freeing.
4328 */
4329 if (!PyErr_Occurred())
4330 {
4331 for (i = 0; i < old_len && i < new_len; ++i)
4332 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4333 == FAIL)
4334 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004335 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004336 break;
4337 }
4338 }
4339 else
4340 i = 0;
4341
4342 /* Now we may need to insert the remaining new old_len. If we do, we
4343 * must free the strings as we finish with them (we can't pass the
4344 * responsibility to vim in this case).
4345 */
4346 if (!PyErr_Occurred())
4347 {
4348 while (i < new_len)
4349 {
4350 if (ml_append((linenr_T)(lo + i - 1),
4351 (char_u *)array[i], 0, FALSE) == FAIL)
4352 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004353 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004354 break;
4355 }
4356 vim_free(array[i]);
4357 ++i;
4358 ++extra;
4359 }
4360 }
4361
4362 /* Free any left-over old_len, as a result of an error */
4363 while (i < new_len)
4364 {
4365 vim_free(array[i]);
4366 ++i;
4367 }
4368
4369 /* Free the array of old_len. All of its contents have now
4370 * been dealt with (either freed, or the responsibility passed
4371 * to vim.
4372 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004373 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004374
4375 /* Adjust marks. Invalidate any which lie in the
4376 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004377 * Only adjust marks if we managed to switch to a window that holds
4378 * the buffer, otherwise line numbers will be invalid. */
4379 if (save_curbuf == NULL)
4380 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004381 (long)MAXLNUM, (long)extra);
4382 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4383
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004384 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004385 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4386
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004387 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004388 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004389
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004390 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004391 return FAIL;
4392
4393 if (len_change)
4394 *len_change = new_len - old_len;
4395
4396 return OK;
4397 }
4398 else
4399 {
4400 PyErr_BadArgument();
4401 return FAIL;
4402 }
4403}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004404
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004405/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004406 * The line number is in Vim format (1-based). The lines to be inserted are
4407 * given as a Python list of string objects or as a single string. The lines
4408 * to be added are checked for validity and correct format. Errors are
4409 * returned as a value of FAIL. The return value is OK on success.
4410 * If OK is returned and len_change is not NULL, *len_change
4411 * is set to the change in the buffer length.
4412 */
4413 static int
4414InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4415{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004416 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004417 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004418 tabpage_T *save_curtab = NULL;
4419
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004420 /* First of all, we check the type of the supplied Python object.
4421 * It must be a string or a list, or the call is in error.
4422 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004423 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004424 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004425 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004426
4427 if (str == NULL)
4428 return FAIL;
4429
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004430 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004431 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004432 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004433
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004434 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004435 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004436 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004437 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004438 else if (save_curbuf == NULL)
4439 /* Only adjust marks if we managed to switch to a window that
4440 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004441 appended_lines_mark((linenr_T)n, 1L);
4442
4443 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004444 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004445 update_screen(VALID);
4446
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004447 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004448 return FAIL;
4449
4450 if (len_change)
4451 *len_change = 1;
4452
4453 return OK;
4454 }
4455 else if (PyList_Check(lines))
4456 {
4457 PyInt i;
4458 PyInt size = PyList_Size(lines);
4459 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004460
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004461 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004462 if (array == NULL)
4463 {
4464 PyErr_NoMemory();
4465 return FAIL;
4466 }
4467
4468 for (i = 0; i < size; ++i)
4469 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004470 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004471
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004472 if (!(line = PyList_GetItem(lines, i)) ||
4473 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004474 {
4475 while (i)
4476 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004477 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004478 return FAIL;
4479 }
4480 }
4481
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004482 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004483 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004484 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004485
4486 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004487 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004488 else
4489 {
4490 for (i = 0; i < size; ++i)
4491 {
4492 if (ml_append((linenr_T)(n + i),
4493 (char_u *)array[i], 0, FALSE) == FAIL)
4494 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004495 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004496
4497 /* Free the rest of the lines */
4498 while (i < size)
4499 vim_free(array[i++]);
4500
4501 break;
4502 }
4503 vim_free(array[i]);
4504 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004505 if (i > 0 && save_curbuf == NULL)
4506 /* Only adjust marks if we managed to switch to a window that
4507 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004508 appended_lines_mark((linenr_T)n, (long)i);
4509 }
4510
4511 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004512 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004513 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004514 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004515
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004516 update_screen(VALID);
4517
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004518 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004519 return FAIL;
4520
4521 if (len_change)
4522 *len_change = size;
4523
4524 return OK;
4525 }
4526 else
4527 {
4528 PyErr_BadArgument();
4529 return FAIL;
4530 }
4531}
4532
4533/*
4534 * Common routines for buffers and line ranges
4535 * -------------------------------------------
4536 */
4537
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004538typedef struct
4539{
4540 PyObject_HEAD
4541 buf_T *buf;
4542} BufferObject;
4543
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004544 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004545CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004546{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004547 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004548 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004549 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004550 return -1;
4551 }
4552
4553 return 0;
4554}
4555
4556 static PyObject *
4557RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4558{
4559 if (CheckBuffer(self))
4560 return NULL;
4561
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004562 if (end == -1)
4563 end = self->buf->b_ml.ml_line_count;
4564
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004565 if (n < 0)
4566 n += end - start + 1;
4567
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004568 if (n < 0 || n > end - start)
4569 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004570 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004571 return NULL;
4572 }
4573
4574 return GetBufferLine(self->buf, n+start);
4575}
4576
4577 static PyObject *
4578RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4579{
4580 PyInt size;
4581
4582 if (CheckBuffer(self))
4583 return NULL;
4584
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004585 if (end == -1)
4586 end = self->buf->b_ml.ml_line_count;
4587
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004588 size = end - start + 1;
4589
4590 if (lo < 0)
4591 lo = 0;
4592 else if (lo > size)
4593 lo = size;
4594 if (hi < 0)
4595 hi = 0;
4596 if (hi < lo)
4597 hi = lo;
4598 else if (hi > size)
4599 hi = size;
4600
4601 return GetBufferLineList(self->buf, lo+start, hi+start);
4602}
4603
4604 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004605RBAsItem(
4606 BufferObject *self,
4607 PyInt n,
4608 PyObject *valObject,
4609 PyInt start,
4610 PyInt end,
4611 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004612{
4613 PyInt len_change;
4614
4615 if (CheckBuffer(self))
4616 return -1;
4617
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004618 if (end == -1)
4619 end = self->buf->b_ml.ml_line_count;
4620
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004621 if (n < 0)
4622 n += end - start + 1;
4623
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004624 if (n < 0 || n > end - start)
4625 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004626 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004627 return -1;
4628 }
4629
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004630 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004631 return -1;
4632
4633 if (new_end)
4634 *new_end = end + len_change;
4635
4636 return 0;
4637}
4638
Bram Moolenaar19e60942011-06-19 00:27:51 +02004639 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004640RBAsSlice(
4641 BufferObject *self,
4642 PyInt lo,
4643 PyInt hi,
4644 PyObject *valObject,
4645 PyInt start,
4646 PyInt end,
4647 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004648{
4649 PyInt size;
4650 PyInt len_change;
4651
4652 /* Self must be a valid buffer */
4653 if (CheckBuffer(self))
4654 return -1;
4655
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004656 if (end == -1)
4657 end = self->buf->b_ml.ml_line_count;
4658
Bram Moolenaar19e60942011-06-19 00:27:51 +02004659 /* Sort out the slice range */
4660 size = end - start + 1;
4661
4662 if (lo < 0)
4663 lo = 0;
4664 else if (lo > size)
4665 lo = size;
4666 if (hi < 0)
4667 hi = 0;
4668 if (hi < lo)
4669 hi = lo;
4670 else if (hi > size)
4671 hi = size;
4672
4673 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004674 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004675 return -1;
4676
4677 if (new_end)
4678 *new_end = end + len_change;
4679
4680 return 0;
4681}
4682
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004683
4684 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004685RBAppend(
4686 BufferObject *self,
4687 PyObject *args,
4688 PyInt start,
4689 PyInt end,
4690 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004691{
4692 PyObject *lines;
4693 PyInt len_change;
4694 PyInt max;
4695 PyInt n;
4696
4697 if (CheckBuffer(self))
4698 return NULL;
4699
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004700 if (end == -1)
4701 end = self->buf->b_ml.ml_line_count;
4702
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004703 max = n = end - start + 1;
4704
4705 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4706 return NULL;
4707
4708 if (n < 0 || n > max)
4709 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004710 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004711 return NULL;
4712 }
4713
4714 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4715 return NULL;
4716
4717 if (new_end)
4718 *new_end = end + len_change;
4719
4720 Py_INCREF(Py_None);
4721 return Py_None;
4722}
4723
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004724/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004725 */
4726
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004727static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004728static PySequenceMethods RangeAsSeq;
4729static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004730
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004731typedef struct
4732{
4733 PyObject_HEAD
4734 BufferObject *buf;
4735 PyInt start;
4736 PyInt end;
4737} RangeObject;
4738
4739 static PyObject *
4740RangeNew(buf_T *buf, PyInt start, PyInt end)
4741{
4742 BufferObject *bufr;
4743 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004744 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004745 if (self == NULL)
4746 return NULL;
4747
4748 bufr = (BufferObject *)BufferNew(buf);
4749 if (bufr == NULL)
4750 {
4751 Py_DECREF(self);
4752 return NULL;
4753 }
4754 Py_INCREF(bufr);
4755
4756 self->buf = bufr;
4757 self->start = start;
4758 self->end = end;
4759
4760 return (PyObject *)(self);
4761}
4762
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004763 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004764RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004765{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004766 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004767 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004768 PyObject_GC_Del((void *)(self));
4769}
4770
4771 static int
4772RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4773{
4774 Py_VISIT(((PyObject *)(self->buf)));
4775 return 0;
4776}
4777
4778 static int
4779RangeClear(RangeObject *self)
4780{
4781 Py_CLEAR(self->buf);
4782 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004783}
4784
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004785 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004786RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004787{
4788 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004789 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004790 return -1; /* ??? */
4791
Bram Moolenaard6e39182013-05-21 18:30:34 +02004792 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004793}
4794
4795 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004796RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004797{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004798 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004799}
4800
4801 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004802RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004803{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004804 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004805}
4806
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004807static char *RangeAttrs[] = {
4808 "start", "end",
4809 NULL
4810};
4811
4812 static PyObject *
4813RangeDir(PyObject *self)
4814{
4815 return ObjectDir(self, RangeAttrs);
4816}
4817
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004818 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004819RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004820{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004821 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004822}
4823
4824 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004825RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004826{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004827 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004828 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4829 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004830 else
4831 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004832 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004833
4834 if (name == NULL)
4835 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004836
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004837 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004838 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004839 }
4840}
4841
4842static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004843 /* name, function, calling, documentation */
4844 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004845 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4846 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004847};
4848
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004849static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004850static PySequenceMethods BufferAsSeq;
4851static PyMappingMethods BufferAsMapping;
4852
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004853 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004854BufferNew(buf_T *buf)
4855{
4856 /* We need to handle deletion of buffers underneath us.
4857 * If we add a "b_python*_ref" field to the buf_T structure,
4858 * then we can get at it in buf_freeall() in vim. We then
4859 * need to create only ONE Python object per buffer - if
4860 * we try to create a second, just INCREF the existing one
4861 * and return it. The (single) Python object referring to
4862 * the buffer is stored in "b_python*_ref".
4863 * Question: what to do on a buf_freeall(). We'll probably
4864 * have to either delete the Python object (DECREF it to
4865 * zero - a bad idea, as it leaves dangling refs!) or
4866 * set the buf_T * value to an invalid value (-1?), which
4867 * means we need checks in all access functions... Bah.
4868 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004869 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004870 * b_python_ref and b_python3_ref fields respectively.
4871 */
4872
4873 BufferObject *self;
4874
4875 if (BUF_PYTHON_REF(buf) != NULL)
4876 {
4877 self = BUF_PYTHON_REF(buf);
4878 Py_INCREF(self);
4879 }
4880 else
4881 {
4882 self = PyObject_NEW(BufferObject, &BufferType);
4883 if (self == NULL)
4884 return NULL;
4885 self->buf = buf;
4886 BUF_PYTHON_REF(buf) = self;
4887 }
4888
4889 return (PyObject *)(self);
4890}
4891
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004892 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004893BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004894{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004895 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4896 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004897
4898 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004899}
4900
Bram Moolenaar971db462013-05-12 18:44:48 +02004901 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004902BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004903{
4904 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004905 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004906 return -1; /* ??? */
4907
Bram Moolenaard6e39182013-05-21 18:30:34 +02004908 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004909}
4910
4911 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004912BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004913{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004914 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004915}
4916
4917 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004918BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004919{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004920 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004921}
4922
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004923static char *BufferAttrs[] = {
4924 "name", "number", "vars", "options", "valid",
4925 NULL
4926};
4927
4928 static PyObject *
4929BufferDir(PyObject *self)
4930{
4931 return ObjectDir(self, BufferAttrs);
4932}
4933
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004934 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004935BufferAttrValid(BufferObject *self, char *name)
4936{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004937 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004938
4939 if (strcmp(name, "valid") != 0)
4940 return NULL;
4941
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004942 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4943 Py_INCREF(ret);
4944 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004945}
4946
4947 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004948BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004949{
4950 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004951 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004952 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004953 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004954 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004955 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004956 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004957 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004958 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4959 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004960 else if (strcmp(name, "__members__") == 0)
4961 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004962 else
4963 return NULL;
4964}
4965
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004966 static int
4967BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4968{
4969 if (CheckBuffer(self))
4970 return -1;
4971
4972 if (strcmp(name, "name") == 0)
4973 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004974 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004975 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004976 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004977 PyObject *todecref;
4978
4979 if (!(val = StringToChars(valObject, &todecref)))
4980 return -1;
4981
4982 VimTryStart();
4983 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4984 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004985 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004986 aucmd_restbuf(&aco);
4987 Py_XDECREF(todecref);
4988 if (VimTryEnd())
4989 return -1;
4990
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004991 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004992 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004993 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004994 return -1;
4995 }
4996 return 0;
4997 }
4998 else
4999 {
5000 PyErr_SetString(PyExc_AttributeError, name);
5001 return -1;
5002 }
5003}
5004
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005005 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005006BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005007{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005008 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005009}
5010
5011 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02005012BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005013{
5014 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005015 char_u *pmark;
5016 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02005017 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005018 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005019
Bram Moolenaard6e39182013-05-21 18:30:34 +02005020 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005021 return NULL;
5022
Bram Moolenaar389a1792013-06-23 13:00:44 +02005023 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005024 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005025
Bram Moolenaar389a1792013-06-23 13:00:44 +02005026 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005027 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005028 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005029 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005030 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005031 return NULL;
5032 }
5033
5034 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005035
5036 Py_XDECREF(todecref);
5037
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005038 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005039 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005040 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02005041 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005042 if (VimTryEnd())
5043 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005044
5045 if (posp == NULL)
5046 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005047 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005048 return NULL;
5049 }
5050
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005051 if (posp->lnum <= 0)
5052 {
5053 /* Or raise an error? */
5054 Py_INCREF(Py_None);
5055 return Py_None;
5056 }
5057
5058 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5059}
5060
5061 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005062BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005063{
5064 PyInt start;
5065 PyInt end;
5066
Bram Moolenaard6e39182013-05-21 18:30:34 +02005067 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005068 return NULL;
5069
5070 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5071 return NULL;
5072
Bram Moolenaard6e39182013-05-21 18:30:34 +02005073 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005074}
5075
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005076 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005077BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005078{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005079 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005080 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005081 else
5082 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005083 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005084
5085 if (name == NULL)
5086 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005087
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005088 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005089 }
5090}
5091
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005092static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005093 /* name, function, calling, documentation */
5094 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005095 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005096 {"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 +02005097 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5098 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005099};
5100
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005101/*
5102 * Buffer list object - Implementation
5103 */
5104
5105static PyTypeObject BufMapType;
5106
5107typedef struct
5108{
5109 PyObject_HEAD
5110} BufMapObject;
5111
5112 static PyInt
5113BufMapLength(PyObject *self UNUSED)
5114{
5115 buf_T *b = firstbuf;
5116 PyInt n = 0;
5117
5118 while (b)
5119 {
5120 ++n;
5121 b = b->b_next;
5122 }
5123
5124 return n;
5125}
5126
5127 static PyObject *
5128BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5129{
5130 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005131 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005132
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005133 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005134 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005135
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005136 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005137
5138 if (b)
5139 return BufferNew(b);
5140 else
5141 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005142 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005143 return NULL;
5144 }
5145}
5146
5147 static void
5148BufMapIterDestruct(PyObject *buffer)
5149{
5150 /* Iteration was stopped before all buffers were processed */
5151 if (buffer)
5152 {
5153 Py_DECREF(buffer);
5154 }
5155}
5156
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005157 static int
5158BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5159{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005160 if (buffer)
5161 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005162 return 0;
5163}
5164
5165 static int
5166BufMapIterClear(PyObject **buffer)
5167{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005168 if (*buffer)
5169 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005170 return 0;
5171}
5172
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005173 static PyObject *
5174BufMapIterNext(PyObject **buffer)
5175{
5176 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005177 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005178
5179 if (!*buffer)
5180 return NULL;
5181
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005182 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005183
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005184 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005185 {
5186 *buffer = NULL;
5187 return NULL;
5188 }
5189
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005190 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005191 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005192 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005193 return NULL;
5194 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005195 /* Do not increment reference: we no longer hold it (decref), but whoever
5196 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005197 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005198}
5199
5200 static PyObject *
5201BufMapIter(PyObject *self UNUSED)
5202{
5203 PyObject *buffer;
5204
5205 buffer = BufferNew(firstbuf);
5206 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005207 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5208 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005209}
5210
5211static PyMappingMethods BufMapAsMapping = {
5212 (lenfunc) BufMapLength,
5213 (binaryfunc) BufMapItem,
5214 (objobjargproc) 0,
5215};
5216
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005217/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005218 */
5219
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005220static char *CurrentAttrs[] = {
5221 "buffer", "window", "line", "range", "tabpage",
5222 NULL
5223};
5224
5225 static PyObject *
5226CurrentDir(PyObject *self)
5227{
5228 return ObjectDir(self, CurrentAttrs);
5229}
5230
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005231 static PyObject *
5232CurrentGetattr(PyObject *self UNUSED, char *name)
5233{
5234 if (strcmp(name, "buffer") == 0)
5235 return (PyObject *)BufferNew(curbuf);
5236 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005237 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005238 else if (strcmp(name, "tabpage") == 0)
5239 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005240 else if (strcmp(name, "line") == 0)
5241 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5242 else if (strcmp(name, "range") == 0)
5243 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005244 else if (strcmp(name, "__members__") == 0)
5245 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005246 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005247#if PY_MAJOR_VERSION < 3
5248 return Py_FindMethod(WindowMethods, self, name);
5249#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005250 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005251#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005252}
5253
5254 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005255CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005256{
5257 if (strcmp(name, "line") == 0)
5258 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005259 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5260 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005261 return -1;
5262
5263 return 0;
5264 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005265 else if (strcmp(name, "buffer") == 0)
5266 {
5267 int count;
5268
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005269 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005270 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005271 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005272 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005273 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005274 return -1;
5275 }
5276
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005277 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005278 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005279 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005280
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005281 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005282 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5283 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005284 if (VimTryEnd())
5285 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005286 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005287 return -1;
5288 }
5289
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005290 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005291 }
5292 else if (strcmp(name, "window") == 0)
5293 {
5294 int count;
5295
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005296 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005297 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005298 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005299 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005300 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005301 return -1;
5302 }
5303
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005304 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005305 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005306 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005307
5308 if (!count)
5309 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005310 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005311 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005312 return -1;
5313 }
5314
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005315 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005316 win_goto(((WindowObject *)(valObject))->win);
5317 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005318 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005319 if (VimTryEnd())
5320 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005321 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005322 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005323 return -1;
5324 }
5325
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005326 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005327 }
5328 else if (strcmp(name, "tabpage") == 0)
5329 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005330 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005331 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005332 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005333 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005334 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005335 return -1;
5336 }
5337
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005338 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005339 return -1;
5340
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005341 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005342 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5343 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005344 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005345 if (VimTryEnd())
5346 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005347 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005348 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005349 return -1;
5350 }
5351
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005352 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005353 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005354 else
5355 {
5356 PyErr_SetString(PyExc_AttributeError, name);
5357 return -1;
5358 }
5359}
5360
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005361static struct PyMethodDef CurrentMethods[] = {
5362 /* name, function, calling, documentation */
5363 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5364 { NULL, NULL, 0, NULL}
5365};
5366
Bram Moolenaardb913952012-06-29 12:54:53 +02005367 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005368init_range_cmd(exarg_T *eap)
5369{
5370 RangeStart = eap->line1;
5371 RangeEnd = eap->line2;
5372}
5373
5374 static void
5375init_range_eval(typval_T *rettv UNUSED)
5376{
5377 RangeStart = (PyInt) curwin->w_cursor.lnum;
5378 RangeEnd = RangeStart;
5379}
5380
5381 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005382run_cmd(const char *cmd, void *arg UNUSED
5383#ifdef PY_CAN_RECURSE
5384 , PyGILState_STATE *pygilstate UNUSED
5385#endif
5386 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005387{
Bram Moolenaar41009372013-07-01 22:03:04 +02005388 PyObject *run_ret;
5389 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5390 if (run_ret != NULL)
5391 {
5392 Py_DECREF(run_ret);
5393 }
5394 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5395 {
5396 EMSG2(_(e_py_systemexit), "python");
5397 PyErr_Clear();
5398 }
5399 else
5400 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005401}
5402
5403static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5404static int code_hdr_len = 30;
5405
5406 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005407run_do(const char *cmd, void *arg UNUSED
5408#ifdef PY_CAN_RECURSE
5409 , PyGILState_STATE *pygilstate
5410#endif
5411 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005412{
5413 PyInt lnum;
5414 size_t len;
5415 char *code;
5416 int status;
5417 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005418 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005419
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005420 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005421 {
5422 EMSG(_("cannot save undo information"));
5423 return;
5424 }
5425
5426 len = code_hdr_len + STRLEN(cmd);
5427 code = PyMem_New(char, len + 1);
5428 memcpy(code, code_hdr, code_hdr_len);
5429 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005430 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5431 status = -1;
5432 if (run_ret != NULL)
5433 {
5434 status = 0;
5435 Py_DECREF(run_ret);
5436 }
5437 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5438 {
5439 PyMem_Free(code);
5440 EMSG2(_(e_py_systemexit), "python");
5441 PyErr_Clear();
5442 return;
5443 }
5444 else
5445 PyErr_PrintEx(1);
5446
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005447 PyMem_Free(code);
5448
5449 if (status)
5450 {
5451 EMSG(_("failed to run the code"));
5452 return;
5453 }
5454
5455 status = 0;
5456 pymain = PyImport_AddModule("__main__");
5457 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005458#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005459 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005460#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005461
5462 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5463 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005464 PyObject *line;
5465 PyObject *linenr;
5466 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005467
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005468#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005469 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005470#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005471 if (!(line = GetBufferLine(curbuf, lnum)))
5472 goto err;
5473 if (!(linenr = PyInt_FromLong((long) lnum)))
5474 {
5475 Py_DECREF(line);
5476 goto err;
5477 }
5478 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5479 Py_DECREF(line);
5480 Py_DECREF(linenr);
5481 if (!ret)
5482 goto err;
5483
5484 if (ret != Py_None)
5485 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5486 goto err;
5487
5488 Py_XDECREF(ret);
5489 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005490#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005491 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005492#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005493 }
5494 goto out;
5495err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005496#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005497 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005498#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005499 PyErr_PrintEx(0);
5500 PythonIO_Flush();
5501 status = 1;
5502out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005503#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005504 if (!status)
5505 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005506#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005507 Py_DECREF(pyfunc);
5508 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5509 if (status)
5510 return;
5511 check_cursor();
5512 update_curbuf(NOT_VALID);
5513}
5514
5515 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005516run_eval(const char *cmd, typval_T *rettv
5517#ifdef PY_CAN_RECURSE
5518 , PyGILState_STATE *pygilstate UNUSED
5519#endif
5520 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005521{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005522 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005523
Bram Moolenaar41009372013-07-01 22:03:04 +02005524 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005525 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005526 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005527 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005528 {
5529 EMSG2(_(e_py_systemexit), "python");
5530 PyErr_Clear();
5531 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005532 else
5533 {
5534 if (PyErr_Occurred() && !msg_silent)
5535 PyErr_PrintEx(0);
5536 EMSG(_("E858: Eval did not return a valid python object"));
5537 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005538 }
5539 else
5540 {
Bram Moolenaar77324fc2016-01-17 22:37:03 +01005541 if (run_ret != Py_None && ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005542 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005543 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005544 }
5545 PyErr_Clear();
5546}
5547
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005548 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005549set_ref_in_py(const int copyID)
5550{
5551 pylinkedlist_T *cur;
5552 dict_T *dd;
5553 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005554 int abort = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +02005555
5556 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005557 {
5558 for(cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005559 {
5560 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5561 if (dd->dv_copyID != copyID)
5562 {
5563 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005564 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005565 }
5566 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005567 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005568
5569 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005570 {
5571 for(cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005572 {
5573 ll = ((ListObject *) (cur->pll_obj))->list;
5574 if (ll->lv_copyID != copyID)
5575 {
5576 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005577 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005578 }
5579 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005580 }
5581
5582 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005583}
5584
5585 static int
5586set_string_copy(char_u *str, typval_T *tv)
5587{
5588 tv->vval.v_string = vim_strsave(str);
5589 if (tv->vval.v_string == NULL)
5590 {
5591 PyErr_NoMemory();
5592 return -1;
5593 }
5594 return 0;
5595}
5596
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005597 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005598pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005599{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005600 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005601 char_u *key;
5602 dictitem_T *di;
5603 PyObject *keyObject;
5604 PyObject *valObject;
5605 Py_ssize_t iter = 0;
5606
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005607 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005608 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005609
5610 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005611 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005612
5613 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5614 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005615 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005616
Bram Moolenaara03e6312013-05-29 22:49:26 +02005617 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005618 {
5619 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005620 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005621 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005622
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005623 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005624 {
5625 dict_unref(dict);
5626 return -1;
5627 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005628
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005629 if (*key == NUL)
5630 {
5631 dict_unref(dict);
5632 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005633 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005634 return -1;
5635 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005636
5637 di = dictitem_alloc(key);
5638
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005639 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005640
5641 if (di == NULL)
5642 {
5643 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005644 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005645 return -1;
5646 }
5647 di->di_tv.v_lock = 0;
5648
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005649 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005650 {
5651 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005652 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005653 return -1;
5654 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005655
5656 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005657 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005658 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005659 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005660 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005661 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005662 return -1;
5663 }
5664 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005665
5666 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005667 return 0;
5668}
5669
5670 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005671pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005672{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005673 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005674 char_u *key;
5675 dictitem_T *di;
5676 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005677 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005678 PyObject *keyObject;
5679 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005680
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005681 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005682 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005683
5684 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005685 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005686
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005687 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005688 {
5689 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005690 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005691 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005692
5693 if (!(iterator = PyObject_GetIter(list)))
5694 {
5695 dict_unref(dict);
5696 Py_DECREF(list);
5697 return -1;
5698 }
5699 Py_DECREF(list);
5700
5701 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005702 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005703 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005704
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005705 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005706 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005707 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005708 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005709 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005710 return -1;
5711 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005712
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005713 if (*key == NUL)
5714 {
5715 Py_DECREF(keyObject);
5716 Py_DECREF(iterator);
5717 Py_XDECREF(todecref);
5718 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005719 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005720 return -1;
5721 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005722
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005723 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005724 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005725 Py_DECREF(keyObject);
5726 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005727 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005728 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005729 return -1;
5730 }
5731
5732 di = dictitem_alloc(key);
5733
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005734 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005735 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005736
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005737 if (di == NULL)
5738 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005739 Py_DECREF(iterator);
5740 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005741 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005742 PyErr_NoMemory();
5743 return -1;
5744 }
5745 di->di_tv.v_lock = 0;
5746
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005747 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005748 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005749 Py_DECREF(iterator);
5750 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005751 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005752 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005753 return -1;
5754 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005755
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005756 Py_DECREF(valObject);
5757
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005758 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005759 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005760 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005761 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005762 dictitem_free(di);
5763 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005764 return -1;
5765 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005766 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005767 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005768 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005769 return 0;
5770}
5771
5772 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005773pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005774{
5775 list_T *l;
5776
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005777 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005778 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005779
5780 tv->v_type = VAR_LIST;
5781 tv->vval.v_list = l;
5782
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005783 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005784 {
5785 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005786 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005787 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005788
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005789 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005790 return 0;
5791}
5792
Bram Moolenaardb913952012-06-29 12:54:53 +02005793typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5794
5795 static int
5796convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005797 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005798{
5799 PyObject *capsule;
5800 char hexBuf[sizeof(void *) * 2 + 3];
5801
5802 sprintf(hexBuf, "%p", obj);
5803
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005804# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005805 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005806# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005807 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005808# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005809 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005810 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005811# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005812 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005813# else
5814 capsule = PyCObject_FromVoidPtr(tv, NULL);
5815# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005816 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5817 {
5818 Py_DECREF(capsule);
5819 tv->v_type = VAR_UNKNOWN;
5820 return -1;
5821 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005822
5823 Py_DECREF(capsule);
5824
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005825 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005826 {
5827 tv->v_type = VAR_UNKNOWN;
5828 return -1;
5829 }
5830 /* As we are not using copy_tv which increments reference count we must
5831 * do it ourself. */
Bram Moolenaar81e7a9c2016-02-06 19:57:20 +01005832 if (tv->v_type == VAR_DICT)
5833 ++tv->vval.v_dict->dv_refcount;
5834 else if (tv->v_type == VAR_LIST)
5835 ++tv->vval.v_list->lv_refcount;
Bram Moolenaardb913952012-06-29 12:54:53 +02005836 }
5837 else
5838 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005839 typval_T *v;
5840
5841# ifdef PY_USE_CAPSULE
5842 v = PyCapsule_GetPointer(capsule, NULL);
5843# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005844 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005845# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005846 copy_tv(v, tv);
5847 }
5848 return 0;
5849}
5850
5851 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005852ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5853{
5854 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005855 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005856
5857 if (!(lookup_dict = PyDict_New()))
5858 return -1;
5859
5860 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5861 {
5862 tv->v_type = VAR_DICT;
5863 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5864 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005865 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005866 }
5867 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005868 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005869 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005870 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005871 else
5872 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005873 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005874 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005875 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005876 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005877 }
5878 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005879 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005880}
5881
5882 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005883ConvertFromPyObject(PyObject *obj, typval_T *tv)
5884{
5885 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005886 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005887
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005888 if (!(lookup_dict = PyDict_New()))
5889 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005890 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005891 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005892 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005893}
5894
5895 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005896_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005897{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005898 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005899 {
5900 tv->v_type = VAR_DICT;
5901 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5902 ++tv->vval.v_dict->dv_refcount;
5903 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005904 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005905 {
5906 tv->v_type = VAR_LIST;
5907 tv->vval.v_list = (((ListObject *)(obj))->list);
5908 ++tv->vval.v_list->lv_refcount;
5909 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005910 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005911 {
5912 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5913 return -1;
5914
5915 tv->v_type = VAR_FUNC;
5916 func_ref(tv->vval.v_string);
5917 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005918 else if (PyBytes_Check(obj))
5919 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005920 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005921
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005922 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005923 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005924 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005925 return -1;
5926
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005927 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005928 return -1;
5929
5930 tv->v_type = VAR_STRING;
5931 }
5932 else if (PyUnicode_Check(obj))
5933 {
5934 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005935 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005936
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005937 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005938 if (bytes == NULL)
5939 return -1;
5940
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005941 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005942 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005943 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005944 return -1;
5945
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005946 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005947 {
5948 Py_XDECREF(bytes);
5949 return -1;
5950 }
5951 Py_XDECREF(bytes);
5952
5953 tv->v_type = VAR_STRING;
5954 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005955#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005956 else if (PyInt_Check(obj))
5957 {
5958 tv->v_type = VAR_NUMBER;
5959 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005960 if (PyErr_Occurred())
5961 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005962 }
5963#endif
5964 else if (PyLong_Check(obj))
5965 {
5966 tv->v_type = VAR_NUMBER;
5967 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005968 if (PyErr_Occurred())
5969 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005970 }
5971 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005972 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005973#ifdef FEAT_FLOAT
5974 else if (PyFloat_Check(obj))
5975 {
5976 tv->v_type = VAR_FLOAT;
5977 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5978 }
5979#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005980 else if (PyObject_HasAttrString(obj, "keys"))
5981 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005982 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005983 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005984 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005985 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005986 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005987 else if (PyNumber_Check(obj))
5988 {
5989 PyObject *num;
5990
5991 if (!(num = PyNumber_Long(obj)))
5992 return -1;
5993
5994 tv->v_type = VAR_NUMBER;
5995 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5996
5997 Py_DECREF(num);
5998 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005999 else
6000 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02006001 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006002 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02006003 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02006004 return -1;
6005 }
6006 return 0;
6007}
6008
6009 static PyObject *
6010ConvertToPyObject(typval_T *tv)
6011{
6012 if (tv == NULL)
6013 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006014 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006015 return NULL;
6016 }
6017 switch (tv->v_type)
6018 {
6019 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006020 return PyBytes_FromString(tv->vval.v_string == NULL
6021 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006022 case VAR_NUMBER:
6023 return PyLong_FromLong((long) tv->vval.v_number);
6024#ifdef FEAT_FLOAT
6025 case VAR_FLOAT:
6026 return PyFloat_FromDouble((double) tv->vval.v_float);
6027#endif
6028 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006029 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006030 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006031 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006032 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006033 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006034 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaar4c908612016-03-24 21:58:12 +01006035 case VAR_PARTIAL:
6036 return NEW_FUNCTION(tv->vval.v_partial == NULL
6037 ? (char_u *)"" : tv->vval.v_partial->pt_name);
Bram Moolenaardb913952012-06-29 12:54:53 +02006038 case VAR_UNKNOWN:
Bram Moolenaar4c908612016-03-24 21:58:12 +01006039 case VAR_CHANNEL:
6040 case VAR_JOB:
Bram Moolenaardb913952012-06-29 12:54:53 +02006041 Py_INCREF(Py_None);
6042 return Py_None;
Bram Moolenaar4c908612016-03-24 21:58:12 +01006043 case VAR_SPECIAL:
6044 switch (tv->vval.v_number)
6045 {
6046 case VVAL_FALSE: return AlwaysFalse(NULL);
6047 case VVAL_TRUE: return AlwaysTrue(NULL);
6048 case VVAL_NONE:
6049 case VVAL_NULL: return AlwaysNone(NULL);
6050 }
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006051 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006052 return NULL;
6053 }
Bram Moolenaar4c908612016-03-24 21:58:12 +01006054 return NULL;
Bram Moolenaardb913952012-06-29 12:54:53 +02006055}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006056
6057typedef struct
6058{
6059 PyObject_HEAD
6060} CurrentObject;
6061static PyTypeObject CurrentType;
6062
6063 static void
6064init_structs(void)
6065{
6066 vim_memset(&OutputType, 0, sizeof(OutputType));
6067 OutputType.tp_name = "vim.message";
6068 OutputType.tp_basicsize = sizeof(OutputObject);
6069 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6070 OutputType.tp_doc = "vim message object";
6071 OutputType.tp_methods = OutputMethods;
6072#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006073 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6074 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006075 OutputType.tp_alloc = call_PyType_GenericAlloc;
6076 OutputType.tp_new = call_PyType_GenericNew;
6077 OutputType.tp_free = call_PyObject_Free;
6078#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006079 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6080 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006081#endif
6082
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006083 vim_memset(&IterType, 0, sizeof(IterType));
6084 IterType.tp_name = "vim.iter";
6085 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006086 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006087 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006088 IterType.tp_iter = (getiterfunc)IterIter;
6089 IterType.tp_iternext = (iternextfunc)IterNext;
6090 IterType.tp_dealloc = (destructor)IterDestructor;
6091 IterType.tp_traverse = (traverseproc)IterTraverse;
6092 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006093
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006094 vim_memset(&BufferType, 0, sizeof(BufferType));
6095 BufferType.tp_name = "vim.buffer";
6096 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006097 BufferType.tp_dealloc = (destructor)BufferDestructor;
6098 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006099 BufferType.tp_as_sequence = &BufferAsSeq;
6100 BufferType.tp_as_mapping = &BufferAsMapping;
6101 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6102 BufferType.tp_doc = "vim buffer object";
6103 BufferType.tp_methods = BufferMethods;
6104#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006105 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006106 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006107 BufferType.tp_alloc = call_PyType_GenericAlloc;
6108 BufferType.tp_new = call_PyType_GenericNew;
6109 BufferType.tp_free = call_PyObject_Free;
6110#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006111 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006112 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006113#endif
6114
6115 vim_memset(&WindowType, 0, sizeof(WindowType));
6116 WindowType.tp_name = "vim.window";
6117 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006118 WindowType.tp_dealloc = (destructor)WindowDestructor;
6119 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006120 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006121 WindowType.tp_doc = "vim Window object";
6122 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006123 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6124 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006125#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006126 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6127 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006128 WindowType.tp_alloc = call_PyType_GenericAlloc;
6129 WindowType.tp_new = call_PyType_GenericNew;
6130 WindowType.tp_free = call_PyObject_Free;
6131#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006132 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6133 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006134#endif
6135
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006136 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6137 TabPageType.tp_name = "vim.tabpage";
6138 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006139 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6140 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006141 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6142 TabPageType.tp_doc = "vim tab page object";
6143 TabPageType.tp_methods = TabPageMethods;
6144#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006145 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006146 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6147 TabPageType.tp_new = call_PyType_GenericNew;
6148 TabPageType.tp_free = call_PyObject_Free;
6149#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006150 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006151#endif
6152
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006153 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6154 BufMapType.tp_name = "vim.bufferlist";
6155 BufMapType.tp_basicsize = sizeof(BufMapObject);
6156 BufMapType.tp_as_mapping = &BufMapAsMapping;
6157 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006158 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006159 BufferType.tp_doc = "vim buffer list";
6160
6161 vim_memset(&WinListType, 0, sizeof(WinListType));
6162 WinListType.tp_name = "vim.windowlist";
6163 WinListType.tp_basicsize = sizeof(WinListType);
6164 WinListType.tp_as_sequence = &WinListAsSeq;
6165 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6166 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006167 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006168
6169 vim_memset(&TabListType, 0, sizeof(TabListType));
6170 TabListType.tp_name = "vim.tabpagelist";
6171 TabListType.tp_basicsize = sizeof(TabListType);
6172 TabListType.tp_as_sequence = &TabListAsSeq;
6173 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6174 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006175
6176 vim_memset(&RangeType, 0, sizeof(RangeType));
6177 RangeType.tp_name = "vim.range";
6178 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006179 RangeType.tp_dealloc = (destructor)RangeDestructor;
6180 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006181 RangeType.tp_as_sequence = &RangeAsSeq;
6182 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006183 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006184 RangeType.tp_doc = "vim Range object";
6185 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006186 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6187 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006188#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006189 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006190 RangeType.tp_alloc = call_PyType_GenericAlloc;
6191 RangeType.tp_new = call_PyType_GenericNew;
6192 RangeType.tp_free = call_PyObject_Free;
6193#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006194 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006195#endif
6196
6197 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6198 CurrentType.tp_name = "vim.currentdata";
6199 CurrentType.tp_basicsize = sizeof(CurrentObject);
6200 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6201 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006202 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006203#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006204 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6205 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006206#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006207 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6208 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006209#endif
6210
6211 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6212 DictionaryType.tp_name = "vim.dictionary";
6213 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006214 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006215 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006216 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006217 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006218 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6219 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006220 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6221 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6222 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006223#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006224 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6225 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006226#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006227 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6228 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006229#endif
6230
6231 vim_memset(&ListType, 0, sizeof(ListType));
6232 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006233 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006234 ListType.tp_basicsize = sizeof(ListObject);
6235 ListType.tp_as_sequence = &ListAsSeq;
6236 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006237 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006238 ListType.tp_doc = "list pushing modifications to vim structure";
6239 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006240 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006241 ListType.tp_new = (newfunc)ListConstructor;
6242 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006243#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006244 ListType.tp_getattro = (getattrofunc)ListGetattro;
6245 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006246#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006247 ListType.tp_getattr = (getattrfunc)ListGetattr;
6248 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006249#endif
6250
6251 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006252 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006253 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006254 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6255 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006256 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006257 FunctionType.tp_doc = "object that calls vim function";
6258 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006259 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006260 FunctionType.tp_new = (newfunc)FunctionConstructor;
6261 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006262#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006263 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006264#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006265 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006266#endif
6267
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006268 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6269 OptionsType.tp_name = "vim.options";
6270 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006271 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006272 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006273 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006274 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006275 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006276 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6277 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6278 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006279
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006280 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6281 LoaderType.tp_name = "vim.Loader";
6282 LoaderType.tp_basicsize = sizeof(LoaderObject);
6283 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6284 LoaderType.tp_doc = "vim message object";
6285 LoaderType.tp_methods = LoaderMethods;
6286 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6287
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006288#if PY_MAJOR_VERSION >= 3
6289 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6290 vimmodule.m_name = "vim";
6291 vimmodule.m_doc = "Vim Python interface\n";
6292 vimmodule.m_size = -1;
6293 vimmodule.m_methods = VimMethods;
6294#endif
6295}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006296
6297#define PYTYPE_READY(type) \
6298 if (PyType_Ready(&type)) \
6299 return -1;
6300
6301 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006302init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006303{
6304 PYTYPE_READY(IterType);
6305 PYTYPE_READY(BufferType);
6306 PYTYPE_READY(RangeType);
6307 PYTYPE_READY(WindowType);
6308 PYTYPE_READY(TabPageType);
6309 PYTYPE_READY(BufMapType);
6310 PYTYPE_READY(WinListType);
6311 PYTYPE_READY(TabListType);
6312 PYTYPE_READY(CurrentType);
6313 PYTYPE_READY(DictionaryType);
6314 PYTYPE_READY(ListType);
6315 PYTYPE_READY(FunctionType);
6316 PYTYPE_READY(OptionsType);
6317 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006318 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006319 return 0;
6320}
6321
6322 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006323init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006324{
6325 PyObject *path;
6326 PyObject *path_hook;
6327 PyObject *path_hooks;
6328
6329 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6330 return -1;
6331
6332 if (!(path_hooks = PySys_GetObject("path_hooks")))
6333 {
6334 PyErr_Clear();
6335 path_hooks = PyList_New(1);
6336 PyList_SET_ITEM(path_hooks, 0, path_hook);
6337 if (PySys_SetObject("path_hooks", path_hooks))
6338 {
6339 Py_DECREF(path_hooks);
6340 return -1;
6341 }
6342 Py_DECREF(path_hooks);
6343 }
6344 else if (PyList_Check(path_hooks))
6345 {
6346 if (PyList_Append(path_hooks, path_hook))
6347 {
6348 Py_DECREF(path_hook);
6349 return -1;
6350 }
6351 Py_DECREF(path_hook);
6352 }
6353 else
6354 {
6355 VimTryStart();
6356 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6357 "You should now do the following:\n"
6358 "- append vim.path_hook to sys.path_hooks\n"
6359 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6360 VimTryEnd(); /* Discard the error */
6361 Py_DECREF(path_hook);
6362 return 0;
6363 }
6364
6365 if (!(path = PySys_GetObject("path")))
6366 {
6367 PyErr_Clear();
6368 path = PyList_New(1);
6369 Py_INCREF(vim_special_path_object);
6370 PyList_SET_ITEM(path, 0, vim_special_path_object);
6371 if (PySys_SetObject("path", path))
6372 {
6373 Py_DECREF(path);
6374 return -1;
6375 }
6376 Py_DECREF(path);
6377 }
6378 else if (PyList_Check(path))
6379 {
6380 if (PyList_Append(path, vim_special_path_object))
6381 return -1;
6382 }
6383 else
6384 {
6385 VimTryStart();
6386 EMSG(_("Failed to set path: sys.path is not a list\n"
6387 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6388 VimTryEnd(); /* Discard the error */
6389 }
6390
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006391 return 0;
6392}
6393
6394static BufMapObject TheBufferMap =
6395{
6396 PyObject_HEAD_INIT(&BufMapType)
6397};
6398
6399static WinListObject TheWindowList =
6400{
6401 PyObject_HEAD_INIT(&WinListType)
6402 NULL
6403};
6404
6405static CurrentObject TheCurrent =
6406{
6407 PyObject_HEAD_INIT(&CurrentType)
6408};
6409
6410static TabListObject TheTabPageList =
6411{
6412 PyObject_HEAD_INIT(&TabListType)
6413};
6414
6415static struct numeric_constant {
6416 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006417 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006418} numeric_constants[] = {
6419 {"VAR_LOCKED", VAR_LOCKED},
6420 {"VAR_FIXED", VAR_FIXED},
6421 {"VAR_SCOPE", VAR_SCOPE},
6422 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6423};
6424
6425static struct object_constant {
6426 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006427 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006428} object_constants[] = {
6429 {"buffers", (PyObject *)(void *)&TheBufferMap},
6430 {"windows", (PyObject *)(void *)&TheWindowList},
6431 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6432 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006433
6434 {"Buffer", (PyObject *)&BufferType},
6435 {"Range", (PyObject *)&RangeType},
6436 {"Window", (PyObject *)&WindowType},
6437 {"TabPage", (PyObject *)&TabPageType},
6438 {"Dictionary", (PyObject *)&DictionaryType},
6439 {"List", (PyObject *)&ListType},
6440 {"Function", (PyObject *)&FunctionType},
6441 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006442 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006443};
6444
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006445#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006446 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006447 return -1;
6448
6449#define ADD_CHECKED_OBJECT(m, name, obj) \
6450 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006451 PyObject *valObject = obj; \
6452 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006453 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006454 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006455 }
6456
6457 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006458populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006459{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006460 int i;
6461 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006462 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006463 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006464
6465 for (i = 0; i < (int)(sizeof(numeric_constants)
6466 / sizeof(struct numeric_constant));
6467 ++i)
6468 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006469 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006470
6471 for (i = 0; i < (int)(sizeof(object_constants)
6472 / sizeof(struct object_constant));
6473 ++i)
6474 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006475 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006476
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006477 valObject = object_constants[i].valObject;
6478 Py_INCREF(valObject);
6479 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006480 }
6481
6482 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6483 return -1;
6484 ADD_OBJECT(m, "error", VimError);
6485
Bram Moolenaara9922d62013-05-30 13:01:18 +02006486 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6487 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006488 ADD_CHECKED_OBJECT(m, "options",
6489 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006490
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006491 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006492 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006493 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006494
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006495 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006496 return -1;
6497 ADD_OBJECT(m, "_getcwd", py_getcwd)
6498
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006499 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006500 return -1;
6501 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006502 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006503 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006504 if (PyObject_SetAttrString(other_module, "chdir", attr))
6505 {
6506 Py_DECREF(attr);
6507 return -1;
6508 }
6509 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006510
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006511 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006512 {
6513 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006514 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006515 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006516 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6517 {
6518 Py_DECREF(attr);
6519 return -1;
6520 }
6521 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006522 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006523 else
6524 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006525
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006526 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6527 return -1;
6528
6529 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6530
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006531 if (!(imp = PyImport_ImportModule("imp")))
6532 return -1;
6533
6534 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6535 {
6536 Py_DECREF(imp);
6537 return -1;
6538 }
6539
6540 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6541 {
6542 Py_DECREF(py_find_module);
6543 Py_DECREF(imp);
6544 return -1;
6545 }
6546
6547 Py_DECREF(imp);
6548
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006549 ADD_OBJECT(m, "_find_module", py_find_module);
6550 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006551
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006552 return 0;
6553}