blob: e220e3fec7d312e949a6d24d9504c2cc10a4695c [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 }
813 else
814 {
815 Py_INCREF(Py_None);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200816 ret = Py_None;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200817 }
818
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200819 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200820}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200821
822 static PyObject *
Bram Moolenaar774267b2013-05-21 20:51:59 +0200823VimEval(PyObject *self UNUSED, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200824{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200825 char_u *expr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200826 typval_T *our_tv;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200827 PyObject *string;
828 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200829 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200830 PyObject *lookup_dict;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200831
Bram Moolenaar389a1792013-06-23 13:00:44 +0200832 if (!PyArg_ParseTuple(args, "O", &string))
833 return NULL;
834
835 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200836 return NULL;
837
838 Py_BEGIN_ALLOW_THREADS
839 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200840 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200841 our_tv = eval_expr(expr, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200842 Python_Release_Vim();
843 Py_END_ALLOW_THREADS
844
Bram Moolenaar389a1792013-06-23 13:00:44 +0200845 Py_XDECREF(todecref);
846
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200847 if (VimTryEnd())
848 return NULL;
849
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200850 if (our_tv == NULL)
851 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200852 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200853 return NULL;
854 }
855
856 /* Convert the Vim type into a Python type. Create a dictionary that's
857 * used to check for recursive loops. */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200858 if (!(lookup_dict = PyDict_New()))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200859 ret = NULL;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200860 else
861 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200862 ret = VimToPython(our_tv, 1, lookup_dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +0200863 Py_DECREF(lookup_dict);
864 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200865
866
867 Py_BEGIN_ALLOW_THREADS
868 Python_Lock_Vim();
869 free_tv(our_tv);
870 Python_Release_Vim();
871 Py_END_ALLOW_THREADS
872
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200873 return ret;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200874}
875
Bram Moolenaardb913952012-06-29 12:54:53 +0200876static PyObject *ConvertToPyObject(typval_T *);
877
878 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200879VimEvalPy(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200880{
Bram Moolenaardb913952012-06-29 12:54:53 +0200881 typval_T *our_tv;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200882 PyObject *ret;
Bram Moolenaar389a1792013-06-23 13:00:44 +0200883 char_u *expr;
884 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +0200885
Bram Moolenaar389a1792013-06-23 13:00:44 +0200886 if (!(expr = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200887 return NULL;
888
889 Py_BEGIN_ALLOW_THREADS
890 Python_Lock_Vim();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200891 VimTryStart();
Bram Moolenaar389a1792013-06-23 13:00:44 +0200892 our_tv = eval_expr(expr, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +0200893 Python_Release_Vim();
894 Py_END_ALLOW_THREADS
895
Bram Moolenaar389a1792013-06-23 13:00:44 +0200896 Py_XDECREF(todecref);
897
Bram Moolenaara7b64ce2013-05-21 20:40:40 +0200898 if (VimTryEnd())
899 return NULL;
900
Bram Moolenaardb913952012-06-29 12:54:53 +0200901 if (our_tv == NULL)
902 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200903 PyErr_SET_VIM(N_("invalid expression"));
Bram Moolenaardb913952012-06-29 12:54:53 +0200904 return NULL;
905 }
906
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200907 ret = ConvertToPyObject(our_tv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200908 Py_BEGIN_ALLOW_THREADS
909 Python_Lock_Vim();
910 free_tv(our_tv);
911 Python_Release_Vim();
912 Py_END_ALLOW_THREADS
913
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200914 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +0200915}
916
917 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +0200918VimStrwidth(PyObject *self UNUSED, PyObject *string)
Bram Moolenaardb913952012-06-29 12:54:53 +0200919{
Bram Moolenaar389a1792013-06-23 13:00:44 +0200920 char_u *str;
921 PyObject *todecref;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200922 int len;
Bram Moolenaardb913952012-06-29 12:54:53 +0200923
Bram Moolenaar389a1792013-06-23 13:00:44 +0200924 if (!(str = StringToChars(string, &todecref)))
Bram Moolenaardb913952012-06-29 12:54:53 +0200925 return NULL;
926
Bram Moolenaara54bf402012-12-05 16:30:07 +0100927#ifdef FEAT_MBYTE
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200928 len = mb_string2cells(str, (int)STRLEN(str));
Bram Moolenaara54bf402012-12-05 16:30:07 +0100929#else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200930 len = STRLEN(str);
Bram Moolenaara54bf402012-12-05 16:30:07 +0100931#endif
Bram Moolenaar389a1792013-06-23 13:00:44 +0200932
933 Py_XDECREF(todecref);
934
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200935 return PyLong_FromLong(len);
Bram Moolenaardb913952012-06-29 12:54:53 +0200936}
937
Bram Moolenaarf4258302013-06-02 18:20:17 +0200938 static PyObject *
939_VimChdir(PyObject *_chdir, PyObject *args, PyObject *kwargs)
940{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200941 PyObject *ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200942 PyObject *newwd;
943 PyObject *todecref;
944 char_u *new_dir;
945
Bram Moolenaard4209d22013-06-05 20:34:15 +0200946 if (_chdir == NULL)
947 return NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200948 if (!(ret = PyObject_Call(_chdir, args, kwargs)))
Bram Moolenaarf4258302013-06-02 18:20:17 +0200949 return NULL;
950
951 if (!(newwd = PyObject_CallFunctionObjArgs(py_getcwd, NULL)))
952 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200953 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200954 return NULL;
955 }
956
957 if (!(new_dir = StringToChars(newwd, &todecref)))
958 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200959 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200960 Py_DECREF(newwd);
961 return NULL;
962 }
963
964 VimTryStart();
965
966 if (vim_chdir(new_dir))
967 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200968 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200969 Py_DECREF(newwd);
970 Py_XDECREF(todecref);
971
972 if (VimTryEnd())
973 return NULL;
974
Bram Moolenaar6f1404f2013-06-23 16:04:08 +0200975 PyErr_SET_VIM(N_("failed to change directory"));
Bram Moolenaarf4258302013-06-02 18:20:17 +0200976 return NULL;
977 }
978
979 Py_DECREF(newwd);
980 Py_XDECREF(todecref);
981
982 post_chdir(FALSE);
983
984 if (VimTryEnd())
985 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200986 Py_DECREF(ret);
Bram Moolenaarf4258302013-06-02 18:20:17 +0200987 return NULL;
988 }
989
Bram Moolenaarc4b99e02013-06-23 14:30:47 +0200990 return ret;
Bram Moolenaarf4258302013-06-02 18:20:17 +0200991}
992
993 static PyObject *
994VimChdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
995{
996 return _VimChdir(py_chdir, args, kwargs);
997}
998
999 static PyObject *
1000VimFchdir(PyObject *self UNUSED, PyObject *args, PyObject *kwargs)
1001{
1002 return _VimChdir(py_fchdir, args, kwargs);
1003}
1004
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001005typedef struct {
1006 PyObject *callable;
1007 PyObject *result;
1008} map_rtp_data;
1009
1010 static void
1011map_rtp_callback(char_u *path, void *_data)
1012{
1013 void **data = (void **) _data;
1014 PyObject *pathObject;
1015 map_rtp_data *mr_data = *((map_rtp_data **) data);
1016
Bram Moolenaar41009372013-07-01 22:03:04 +02001017 if (!(pathObject = PyString_FromString((char *)path)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001018 {
1019 *data = NULL;
1020 return;
1021 }
1022
1023 mr_data->result = PyObject_CallFunctionObjArgs(mr_data->callable,
1024 pathObject, NULL);
1025
1026 Py_DECREF(pathObject);
1027
1028 if (!mr_data->result || mr_data->result != Py_None)
1029 *data = NULL;
1030 else
1031 {
1032 Py_DECREF(mr_data->result);
1033 mr_data->result = NULL;
1034 }
1035}
1036
1037 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02001038VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001039{
1040 map_rtp_data data;
1041
Bram Moolenaar389a1792013-06-23 13:00:44 +02001042 data.callable = callable;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001043 data.result = NULL;
1044
1045 do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
1046
1047 if (data.result == NULL)
1048 {
1049 if (PyErr_Occurred())
1050 return NULL;
1051 else
1052 {
1053 Py_INCREF(Py_None);
1054 return Py_None;
1055 }
1056 }
1057 return data.result;
1058}
1059
1060/*
1061 * _vim_runtimepath_ special path implementation.
1062 */
1063
1064 static void
1065map_finder_callback(char_u *path, void *_data)
1066{
1067 void **data = (void **) _data;
1068 PyObject *list = *((PyObject **) data);
1069 PyObject *pathObject1, *pathObject2;
1070 char *pathbuf;
1071 size_t pathlen;
1072
1073 pathlen = STRLEN(path);
1074
1075#if PY_MAJOR_VERSION < 3
1076# define PY_MAIN_DIR_STRING "python2"
1077#else
1078# define PY_MAIN_DIR_STRING "python3"
1079#endif
1080#define PY_ALTERNATE_DIR_STRING "pythonx"
1081
1082#define PYTHONX_STRING_LENGTH 7 /* STRLEN("pythonx") */
1083 if (!(pathbuf = PyMem_New(char,
1084 pathlen + STRLEN(PATHSEPSTR) + PYTHONX_STRING_LENGTH + 1)))
1085 {
1086 PyErr_NoMemory();
1087 *data = NULL;
1088 return;
1089 }
1090
1091 mch_memmove(pathbuf, path, pathlen + 1);
1092 add_pathsep((char_u *) pathbuf);
1093
1094 pathlen = STRLEN(pathbuf);
1095 mch_memmove(pathbuf + pathlen, PY_MAIN_DIR_STRING,
1096 PYTHONX_STRING_LENGTH + 1);
1097
1098 if (!(pathObject1 = PyString_FromString(pathbuf)))
1099 {
1100 *data = NULL;
1101 PyMem_Free(pathbuf);
1102 return;
1103 }
1104
1105 mch_memmove(pathbuf + pathlen, PY_ALTERNATE_DIR_STRING,
1106 PYTHONX_STRING_LENGTH + 1);
1107
1108 if (!(pathObject2 = PyString_FromString(pathbuf)))
1109 {
1110 Py_DECREF(pathObject1);
1111 PyMem_Free(pathbuf);
1112 *data = NULL;
1113 return;
1114 }
1115
1116 PyMem_Free(pathbuf);
1117
1118 if (PyList_Append(list, pathObject1)
1119 || PyList_Append(list, pathObject2))
1120 *data = NULL;
1121
1122 Py_DECREF(pathObject1);
1123 Py_DECREF(pathObject2);
1124}
1125
1126 static PyObject *
1127Vim_GetPaths(PyObject *self UNUSED)
1128{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001129 PyObject *ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001130
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001131 if (!(ret = PyList_New(0)))
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001132 return NULL;
1133
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001134 do_in_runtimepath(NULL, FALSE, &map_finder_callback, ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001135
1136 if (PyErr_Occurred())
1137 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001138 Py_DECREF(ret);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001139 return NULL;
1140 }
1141
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001142 return ret;
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001143}
1144
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001145 static PyObject *
1146call_load_module(char *name, int len, PyObject *find_module_result)
1147{
1148 PyObject *fd, *pathname, *description;
1149
Bram Moolenaarc476e522013-06-23 13:46:40 +02001150 if (!PyTuple_Check(find_module_result))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001151 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001152 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001153 N_("expected 3-tuple as imp.find_module() result, but got %s"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001154 Py_TYPE_NAME(find_module_result));
1155 return NULL;
1156 }
1157 if (PyTuple_GET_SIZE(find_module_result) != 3)
1158 {
1159 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001160 N_("expected 3-tuple as imp.find_module() result, but got "
1161 "tuple of size %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02001162 (int) PyTuple_GET_SIZE(find_module_result));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001163 return NULL;
1164 }
1165
1166 if (!(fd = PyTuple_GET_ITEM(find_module_result, 0))
1167 || !(pathname = PyTuple_GET_ITEM(find_module_result, 1))
1168 || !(description = PyTuple_GET_ITEM(find_module_result, 2)))
1169 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001170 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001171 N_("internal error: imp.find_module returned tuple with NULL"));
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001172 return NULL;
1173 }
1174
1175 return PyObject_CallFunction(py_load_module,
1176 "s#OOO", name, len, fd, pathname, description);
1177}
1178
1179 static PyObject *
1180find_module(char *fullname, char *tail, PyObject *new_path)
1181{
1182 PyObject *find_module_result;
1183 PyObject *module;
1184 char *dot;
1185
Bram Moolenaar41009372013-07-01 22:03:04 +02001186 if ((dot = (char *)vim_strchr((char_u *) tail, '.')))
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001187 {
1188 /*
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001189 * There is a dot in the name: call find_module recursively without the
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001190 * first component
1191 */
1192 PyObject *newest_path;
1193 int partlen = (int) (dot - 1 - tail);
1194
1195 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1196 "s#O", tail, partlen, new_path)))
1197 return NULL;
1198
1199 if (!(module = call_load_module(
1200 fullname,
1201 ((int) (tail - fullname)) + partlen,
1202 find_module_result)))
1203 {
1204 Py_DECREF(find_module_result);
1205 return NULL;
1206 }
1207
1208 Py_DECREF(find_module_result);
1209
1210 if (!(newest_path = PyObject_GetAttrString(module, "__path__")))
1211 {
1212 Py_DECREF(module);
1213 return NULL;
1214 }
1215
1216 Py_DECREF(module);
1217
1218 module = find_module(fullname, dot + 1, newest_path);
1219
1220 Py_DECREF(newest_path);
1221
1222 return module;
1223 }
1224 else
1225 {
1226 if (!(find_module_result = PyObject_CallFunction(py_find_module,
1227 "sO", tail, new_path)))
1228 return NULL;
1229
1230 if (!(module = call_load_module(
1231 fullname,
Bram Moolenaaredb07a22013-06-12 18:13:38 +02001232 (int)STRLEN(fullname),
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001233 find_module_result)))
1234 {
1235 Py_DECREF(find_module_result);
1236 return NULL;
1237 }
1238
1239 Py_DECREF(find_module_result);
1240
1241 return module;
1242 }
1243}
1244
1245 static PyObject *
1246FinderFindModule(PyObject *self, PyObject *args)
1247{
1248 char *fullname;
1249 PyObject *module;
1250 PyObject *new_path;
1251 LoaderObject *loader;
1252
1253 if (!PyArg_ParseTuple(args, "s", &fullname))
1254 return NULL;
1255
1256 if (!(new_path = Vim_GetPaths(self)))
1257 return NULL;
1258
1259 module = find_module(fullname, fullname, new_path);
1260
1261 Py_DECREF(new_path);
1262
1263 if (!module)
1264 {
Bram Moolenaar7e85d3d2013-06-23 16:40:39 +02001265 if (PyErr_Occurred())
1266 {
1267 if (PyErr_ExceptionMatches(PyExc_ImportError))
1268 PyErr_Clear();
1269 else
1270 return NULL;
1271 }
1272
Bram Moolenaar81c40c52013-06-12 14:41:04 +02001273 Py_INCREF(Py_None);
1274 return Py_None;
1275 }
1276
1277 if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
1278 {
1279 Py_DECREF(module);
1280 return NULL;
1281 }
1282
1283 loader->module = module;
1284
1285 return (PyObject *) loader;
1286}
1287
1288 static PyObject *
1289VimPathHook(PyObject *self UNUSED, PyObject *args)
1290{
1291 char *path;
1292
1293 if (PyArg_ParseTuple(args, "s", &path)
1294 && STRCMP(path, vim_special_path) == 0)
1295 {
1296 Py_INCREF(vim_module);
1297 return vim_module;
1298 }
1299
1300 PyErr_Clear();
1301 PyErr_SetNone(PyExc_ImportError);
1302 return NULL;
1303}
1304
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001305/*
1306 * Vim module - Definitions
1307 */
1308
1309static struct PyMethodDef VimMethods[] = {
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001310 /* name, function, calling, documentation */
Bram Moolenaar389a1792013-06-23 13:00:44 +02001311 {"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001312 {"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02001313 {"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1314 {"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001315 {"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1316 {"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
Bram Moolenaar389a1792013-06-23 13:00:44 +02001317 {"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001318 {"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02001319 {"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
1320 {"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
1321 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001322};
1323
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001324/*
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001325 * Generic iterator object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001326 */
1327
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001328static PyTypeObject IterType;
1329
1330typedef PyObject *(*nextfun)(void **);
1331typedef void (*destructorfun)(void *);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001332typedef int (*traversefun)(void *, visitproc, void *);
1333typedef int (*clearfun)(void **);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001334
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001335/* Main purpose of this object is removing the need for do python
1336 * initialization (i.e. PyType_Ready and setting type attributes) for a big
1337 * bunch of objects. */
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02001338
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001339typedef struct
1340{
1341 PyObject_HEAD
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001342 void *cur;
1343 nextfun next;
1344 destructorfun destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001345 traversefun traverse;
1346 clearfun clear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001347} IterObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001348
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001349 static PyObject *
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001350IterNew(void *start, destructorfun destruct, nextfun next, traversefun traverse,
1351 clearfun clear)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001352{
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001353 IterObject *self;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001354
Bram Moolenaar774267b2013-05-21 20:51:59 +02001355 self = PyObject_GC_New(IterObject, &IterType);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001356 self->cur = start;
1357 self->next = next;
1358 self->destruct = destruct;
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001359 self->traverse = traverse;
1360 self->clear = clear;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001361
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001362 return (PyObject *)(self);
1363}
1364
1365 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001366IterDestructor(IterObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001367{
Bram Moolenaar774267b2013-05-21 20:51:59 +02001368 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02001369 self->destruct(self->cur);
Bram Moolenaar774267b2013-05-21 20:51:59 +02001370 PyObject_GC_Del((void *)(self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001371}
1372
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001373 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001374IterTraverse(IterObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001375{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001376 if (self->traverse != NULL)
1377 return self->traverse(self->cur, visit, arg);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001378 else
1379 return 0;
1380}
1381
Bram Moolenaar9e74e302013-05-17 21:20:17 +02001382/* Mac OSX defines clear() somewhere. */
1383#ifdef clear
1384# undef clear
1385#endif
1386
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001387 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02001388IterClear(IterObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001389{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001390 if (self->clear != NULL)
1391 return self->clear(&self->cur);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02001392 else
1393 return 0;
1394}
1395
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001396 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02001397IterNext(IterObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001398{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001399 return self->next(&self->cur);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001400}
1401
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001402 static PyObject *
1403IterIter(PyObject *self)
1404{
Bram Moolenaar1bcabe12013-05-29 22:52:32 +02001405 Py_INCREF(self);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001406 return self;
1407}
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001408
Bram Moolenaardb913952012-06-29 12:54:53 +02001409typedef struct pylinkedlist_S {
1410 struct pylinkedlist_S *pll_next;
1411 struct pylinkedlist_S *pll_prev;
1412 PyObject *pll_obj;
1413} pylinkedlist_T;
1414
1415static pylinkedlist_T *lastdict = NULL;
1416static pylinkedlist_T *lastlist = NULL;
1417
1418 static void
1419pyll_remove(pylinkedlist_T *ref, pylinkedlist_T **last)
1420{
1421 if (ref->pll_prev == NULL)
1422 {
1423 if (ref->pll_next == NULL)
1424 {
1425 *last = NULL;
1426 return;
1427 }
1428 }
1429 else
1430 ref->pll_prev->pll_next = ref->pll_next;
1431
1432 if (ref->pll_next == NULL)
1433 *last = ref->pll_prev;
1434 else
1435 ref->pll_next->pll_prev = ref->pll_prev;
1436}
1437
1438 static void
1439pyll_add(PyObject *self, pylinkedlist_T *ref, pylinkedlist_T **last)
1440{
1441 if (*last == NULL)
1442 ref->pll_prev = NULL;
1443 else
1444 {
1445 (*last)->pll_next = ref;
1446 ref->pll_prev = *last;
1447 }
1448 ref->pll_next = NULL;
1449 ref->pll_obj = self;
1450 *last = ref;
1451}
1452
1453static PyTypeObject DictionaryType;
1454
1455typedef struct
1456{
1457 PyObject_HEAD
1458 dict_T *dict;
1459 pylinkedlist_T ref;
1460} DictionaryObject;
1461
Bram Moolenaara9922d62013-05-30 13:01:18 +02001462static PyObject *DictionaryUpdate(DictionaryObject *, PyObject *, PyObject *);
1463
1464#define NEW_DICTIONARY(dict) DictionaryNew(&DictionaryType, dict)
1465
Bram Moolenaardb913952012-06-29 12:54:53 +02001466 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001467DictionaryNew(PyTypeObject *subtype, dict_T *dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02001468{
1469 DictionaryObject *self;
1470
Bram Moolenaara9922d62013-05-30 13:01:18 +02001471 self = (DictionaryObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02001472 if (self == NULL)
1473 return NULL;
1474 self->dict = dict;
1475 ++dict->dv_refcount;
1476
1477 pyll_add((PyObject *)(self), &self->ref, &lastdict);
1478
1479 return (PyObject *)(self);
1480}
1481
Bram Moolenaara9922d62013-05-30 13:01:18 +02001482 static dict_T *
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001483py_dict_alloc(void)
Bram Moolenaara9922d62013-05-30 13:01:18 +02001484{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001485 dict_T *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001486
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001487 if (!(ret = dict_alloc()))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001488 {
1489 PyErr_NoMemory();
1490 return NULL;
1491 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001492 ++ret->dv_refcount;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001493
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001494 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001495}
1496
1497 static PyObject *
1498DictionaryConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
1499{
1500 DictionaryObject *self;
1501 dict_T *dict;
1502
1503 if (!(dict = py_dict_alloc()))
1504 return NULL;
1505
1506 self = (DictionaryObject *) DictionaryNew(subtype, dict);
1507
1508 --dict->dv_refcount;
1509
1510 if (kwargs || PyTuple_Size(args))
1511 {
1512 PyObject *tmp;
1513 if (!(tmp = DictionaryUpdate(self, args, kwargs)))
1514 {
1515 Py_DECREF(self);
1516 return NULL;
1517 }
1518
1519 Py_DECREF(tmp);
1520 }
1521
1522 return (PyObject *)(self);
1523}
1524
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001525 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02001526DictionaryDestructor(DictionaryObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001527{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001528 pyll_remove(&self->ref, &lastdict);
1529 dict_unref(self->dict);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001530
1531 DESTRUCTOR_FINISH(self);
1532}
1533
Bram Moolenaardd8aca62013-05-29 22:36:10 +02001534static char *DictionaryAttrs[] = {
1535 "locked", "scope",
1536 NULL
1537};
1538
1539 static PyObject *
1540DictionaryDir(PyObject *self)
1541{
1542 return ObjectDir(self, DictionaryAttrs);
1543}
1544
Bram Moolenaardb913952012-06-29 12:54:53 +02001545 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001546DictionarySetattr(DictionaryObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001547{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001548 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001549 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001550 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001551 N_("cannot delete vim.Dictionary attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001552 return -1;
1553 }
1554
1555 if (strcmp(name, "locked") == 0)
1556 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02001557 if (self->dict->dv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02001558 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001559 PyErr_SET_STRING(PyExc_TypeError,
1560 N_("cannot modify fixed dictionary"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02001561 return -1;
1562 }
1563 else
1564 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001565 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02001566 if (istrue == -1)
1567 return -1;
1568 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02001569 self->dict->dv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001570 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02001571 self->dict->dv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001572 }
1573 return 0;
1574 }
1575 else
1576 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001577 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001578 return -1;
1579 }
1580}
1581
1582 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02001583DictionaryLength(DictionaryObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02001584{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001585 return ((PyInt) (self->dict->dv_hashtab.ht_used));
Bram Moolenaardb913952012-06-29 12:54:53 +02001586}
1587
Bram Moolenaara9922d62013-05-30 13:01:18 +02001588#define DICT_FLAG_HAS_DEFAULT 0x01
1589#define DICT_FLAG_POP 0x02
1590#define DICT_FLAG_NONE_DEFAULT 0x04
1591#define DICT_FLAG_RETURN_BOOL 0x08 /* Incompatible with DICT_FLAG_POP */
1592#define DICT_FLAG_RETURN_PAIR 0x10
1593
Bram Moolenaardb913952012-06-29 12:54:53 +02001594 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001595_DictionaryItem(DictionaryObject *self, PyObject *args, int flags)
Bram Moolenaardb913952012-06-29 12:54:53 +02001596{
Bram Moolenaara9922d62013-05-30 13:01:18 +02001597 PyObject *keyObject;
1598 PyObject *defObject = ((flags & DICT_FLAG_NONE_DEFAULT)? Py_None : NULL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001599 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001600 char_u *key;
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001601 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001602 dict_T *dict = self->dict;
1603 hashitem_T *hi;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001604 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001605
Bram Moolenaara9922d62013-05-30 13:01:18 +02001606 if (flags & DICT_FLAG_HAS_DEFAULT)
1607 {
1608 if (!PyArg_ParseTuple(args, "O|O", &keyObject, &defObject))
1609 return NULL;
1610 }
1611 else
1612 keyObject = args;
1613
1614 if (flags & DICT_FLAG_RETURN_BOOL)
1615 defObject = Py_False;
1616
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001617 if (!(key = StringToChars(keyObject, &todecref)))
1618 return NULL;
1619
1620 if (*key == NUL)
1621 {
1622 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001623 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001624 return NULL;
1625 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001626
Bram Moolenaara9922d62013-05-30 13:01:18 +02001627 hi = hash_find(&dict->dv_hashtab, key);
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001628
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001629 Py_XDECREF(todecref);
Bram Moolenaar696c2112012-09-21 13:43:14 +02001630
Bram Moolenaara9922d62013-05-30 13:01:18 +02001631 if (HASHITEM_EMPTY(hi))
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001632 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001633 if (defObject)
1634 {
1635 Py_INCREF(defObject);
1636 return defObject;
1637 }
1638 else
1639 {
1640 PyErr_SetObject(PyExc_KeyError, keyObject);
1641 return NULL;
1642 }
1643 }
1644 else if (flags & DICT_FLAG_RETURN_BOOL)
1645 {
Bram Moolenaar0e4eebd2014-02-12 22:08:49 +01001646 ret = Py_True;
1647 Py_INCREF(ret);
1648 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001649 }
1650
1651 di = dict_lookup(hi);
1652
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001653 if (!(ret = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001654 return NULL;
1655
1656 if (flags & DICT_FLAG_POP)
1657 {
1658 if (dict->dv_lock)
1659 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001660 RAISE_LOCKED_DICTIONARY;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001661 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001662 return NULL;
1663 }
1664
1665 hash_remove(&dict->dv_hashtab, hi);
1666 dictitem_free(di);
1667 }
1668
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001669 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001670}
1671
1672 static PyObject *
1673DictionaryItem(DictionaryObject *self, PyObject *keyObject)
1674{
1675 return _DictionaryItem(self, keyObject, 0);
1676}
1677
1678 static int
1679DictionaryContains(DictionaryObject *self, PyObject *keyObject)
1680{
1681 PyObject *rObj = _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001682 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001683
Bram Moolenaarba2d7ff2013-11-04 00:34:53 +01001684 if (rObj == NULL)
1685 return -1;
1686
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001687 ret = (rObj == Py_True);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001688
Bram Moolenaardee2e312013-06-23 16:35:47 +02001689 Py_DECREF(rObj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001690
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001691 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001692}
1693
1694typedef struct
1695{
1696 hashitem_T *ht_array;
1697 long_u ht_used;
1698 hashtab_T *ht;
1699 hashitem_T *hi;
Bram Moolenaar99dc19d2013-05-31 20:49:31 +02001700 long_u todo;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001701} dictiterinfo_T;
1702
1703 static PyObject *
1704DictionaryIterNext(dictiterinfo_T **dii)
1705{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001706 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001707
1708 if (!(*dii)->todo)
1709 return NULL;
1710
1711 if ((*dii)->ht->ht_array != (*dii)->ht_array ||
1712 (*dii)->ht->ht_used != (*dii)->ht_used)
1713 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02001714 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001715 N_("hashtab changed during iteration"));
Bram Moolenaar231e1a12012-09-05 18:45:28 +02001716 return NULL;
1717 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001718
Bram Moolenaara9922d62013-05-30 13:01:18 +02001719 while (((*dii)->todo) && HASHITEM_EMPTY((*dii)->hi))
1720 ++((*dii)->hi);
1721
1722 --((*dii)->todo);
1723
Bram Moolenaar41009372013-07-01 22:03:04 +02001724 if (!(ret = PyBytes_FromString((char *)(*dii)->hi->hi_key)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001725 return NULL;
1726
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001727 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001728}
1729
1730 static PyObject *
1731DictionaryIter(DictionaryObject *self)
1732{
1733 dictiterinfo_T *dii;
1734 hashtab_T *ht;
1735
1736 if (!(dii = PyMem_New(dictiterinfo_T, 1)))
1737 {
1738 PyErr_NoMemory();
1739 return NULL;
1740 }
1741
1742 ht = &self->dict->dv_hashtab;
1743 dii->ht_array = ht->ht_array;
1744 dii->ht_used = ht->ht_used;
1745 dii->ht = ht;
1746 dii->hi = dii->ht_array;
1747 dii->todo = dii->ht_used;
1748
1749 return IterNew(dii,
1750 (destructorfun) PyMem_Free, (nextfun) DictionaryIterNext,
1751 NULL, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001752}
1753
1754 static PyInt
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02001755DictionaryAssItem(
1756 DictionaryObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaardb913952012-06-29 12:54:53 +02001757{
1758 char_u *key;
1759 typval_T tv;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001760 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001761 dictitem_T *di;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001762 PyObject *todecref;
Bram Moolenaardb913952012-06-29 12:54:53 +02001763
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001764 if (dict->dv_lock)
Bram Moolenaardb913952012-06-29 12:54:53 +02001765 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001766 RAISE_LOCKED_DICTIONARY;
Bram Moolenaardb913952012-06-29 12:54:53 +02001767 return -1;
1768 }
1769
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001770 if (!(key = StringToChars(keyObject, &todecref)))
1771 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02001772
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001773 if (*key == NUL)
1774 {
1775 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001776 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001777 return -1;
1778 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001779
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001780 di = dict_find(dict, key, -1);
Bram Moolenaardb913952012-06-29 12:54:53 +02001781
1782 if (valObject == NULL)
1783 {
Bram Moolenaarf27839c2012-06-29 16:19:50 +02001784 hashitem_T *hi;
1785
Bram Moolenaardb913952012-06-29 12:54:53 +02001786 if (di == NULL)
1787 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001788 Py_XDECREF(todecref);
Bram Moolenaar4d188da2013-05-15 15:35:09 +02001789 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaardb913952012-06-29 12:54:53 +02001790 return -1;
1791 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001792 hi = hash_find(&dict->dv_hashtab, di->di_key);
1793 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaardb913952012-06-29 12:54:53 +02001794 dictitem_free(di);
Bram Moolenaar78b59572013-06-02 18:54:21 +02001795 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001796 return 0;
1797 }
1798
1799 if (ConvertFromPyObject(valObject, &tv) == -1)
Bram Moolenaar78b59572013-06-02 18:54:21 +02001800 {
1801 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001802 return -1;
Bram Moolenaar78b59572013-06-02 18:54:21 +02001803 }
Bram Moolenaardb913952012-06-29 12:54:53 +02001804
1805 if (di == NULL)
1806 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001807 if (!(di = dictitem_alloc(key)))
Bram Moolenaardb913952012-06-29 12:54:53 +02001808 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001809 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001810 PyErr_NoMemory();
1811 return -1;
1812 }
1813 di->di_tv.v_lock = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001814 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaardb913952012-06-29 12:54:53 +02001815
Bram Moolenaarb38caae2013-05-29 22:39:52 +02001816 if (dict_add(dict, di) == FAIL)
Bram Moolenaardb913952012-06-29 12:54:53 +02001817 {
1818 vim_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001819 dictitem_free(di);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001820 RAISE_KEY_ADD_FAIL(key);
1821 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001822 return -1;
1823 }
1824 }
1825 else
1826 clear_tv(&di->di_tv);
1827
Bram Moolenaar35eacd72013-05-30 22:06:33 +02001828 Py_XDECREF(todecref);
Bram Moolenaardb913952012-06-29 12:54:53 +02001829
1830 copy_tv(&tv, &di->di_tv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02001831 clear_tv(&tv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001832 return 0;
1833}
1834
Bram Moolenaara9922d62013-05-30 13:01:18 +02001835typedef PyObject *(*hi_to_py)(hashitem_T *);
1836
Bram Moolenaardb913952012-06-29 12:54:53 +02001837 static PyObject *
Bram Moolenaara9922d62013-05-30 13:01:18 +02001838DictionaryListObjects(DictionaryObject *self, hi_to_py hiconvert)
Bram Moolenaardb913952012-06-29 12:54:53 +02001839{
Bram Moolenaard6e39182013-05-21 18:30:34 +02001840 dict_T *dict = self->dict;
Bram Moolenaardb913952012-06-29 12:54:53 +02001841 long_u todo = dict->dv_hashtab.ht_used;
1842 Py_ssize_t i = 0;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001843 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001844 hashitem_T *hi;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001845 PyObject *newObj;
Bram Moolenaardb913952012-06-29 12:54:53 +02001846
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001847 ret = PyList_New(todo);
Bram Moolenaardb913952012-06-29 12:54:53 +02001848 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
1849 {
1850 if (!HASHITEM_EMPTY(hi))
1851 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02001852 if (!(newObj = hiconvert(hi)))
1853 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001854 Py_DECREF(ret);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001855 return NULL;
1856 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001857 PyList_SET_ITEM(ret, i, newObj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001858 --todo;
1859 ++i;
1860 }
1861 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001862 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02001863}
1864
Bram Moolenaara9922d62013-05-30 13:01:18 +02001865 static PyObject *
1866dict_key(hashitem_T *hi)
1867{
1868 return PyBytes_FromString((char *)(hi->hi_key));
1869}
1870
1871 static PyObject *
1872DictionaryListKeys(DictionaryObject *self)
1873{
1874 return DictionaryListObjects(self, dict_key);
1875}
1876
1877 static PyObject *
1878dict_val(hashitem_T *hi)
1879{
1880 dictitem_T *di;
1881
1882 di = dict_lookup(hi);
1883 return ConvertToPyObject(&di->di_tv);
1884}
1885
1886 static PyObject *
1887DictionaryListValues(DictionaryObject *self)
1888{
1889 return DictionaryListObjects(self, dict_val);
1890}
1891
1892 static PyObject *
1893dict_item(hashitem_T *hi)
1894{
1895 PyObject *keyObject;
1896 PyObject *valObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001897 PyObject *ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001898
1899 if (!(keyObject = dict_key(hi)))
1900 return NULL;
1901
1902 if (!(valObject = dict_val(hi)))
1903 {
1904 Py_DECREF(keyObject);
1905 return NULL;
1906 }
1907
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001908 ret = Py_BuildValue("(OO)", keyObject, valObject);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001909
1910 Py_DECREF(keyObject);
1911 Py_DECREF(valObject);
1912
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001913 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001914}
1915
1916 static PyObject *
1917DictionaryListItems(DictionaryObject *self)
1918{
1919 return DictionaryListObjects(self, dict_item);
1920}
1921
1922 static PyObject *
1923DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
1924{
1925 dict_T *dict = self->dict;
1926
1927 if (dict->dv_lock)
1928 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02001929 RAISE_LOCKED_DICTIONARY;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001930 return NULL;
1931 }
1932
1933 if (kwargs)
1934 {
1935 typval_T tv;
1936
1937 if (ConvertFromPyMapping(kwargs, &tv) == -1)
1938 return NULL;
1939
1940 VimTryStart();
1941 dict_extend(self->dict, tv.vval.v_dict, (char_u *) "force");
1942 clear_tv(&tv);
1943 if (VimTryEnd())
1944 return NULL;
1945 }
1946 else
1947 {
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001948 PyObject *obj = NULL;
Bram Moolenaara9922d62013-05-30 13:01:18 +02001949
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001950 if (!PyArg_ParseTuple(args, "|O", &obj))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001951 return NULL;
1952
Bram Moolenaar2d5f38f2014-02-11 18:47:27 +01001953 if (obj == NULL)
1954 {
1955 Py_INCREF(Py_None);
1956 return Py_None;
1957 }
1958
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001959 if (PyObject_HasAttrString(obj, "keys"))
1960 return DictionaryUpdate(self, NULL, obj);
Bram Moolenaara9922d62013-05-30 13:01:18 +02001961 else
1962 {
1963 PyObject *iterator;
1964 PyObject *item;
1965
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001966 if (!(iterator = PyObject_GetIter(obj)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02001967 return NULL;
1968
1969 while ((item = PyIter_Next(iterator)))
1970 {
1971 PyObject *fast;
1972 PyObject *keyObject;
1973 PyObject *valObject;
1974 PyObject *todecref;
1975 char_u *key;
1976 dictitem_T *di;
1977
1978 if (!(fast = PySequence_Fast(item, "")))
1979 {
1980 Py_DECREF(iterator);
1981 Py_DECREF(item);
1982 return NULL;
1983 }
1984
1985 Py_DECREF(item);
1986
1987 if (PySequence_Fast_GET_SIZE(fast) != 2)
1988 {
1989 Py_DECREF(iterator);
1990 Py_DECREF(fast);
Bram Moolenaarc476e522013-06-23 13:46:40 +02001991 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02001992 N_("expected sequence element of size 2, "
1993 "but got sequence of size %d"),
Bram Moolenaardee2e312013-06-23 16:35:47 +02001994 (int) PySequence_Fast_GET_SIZE(fast));
Bram Moolenaara9922d62013-05-30 13:01:18 +02001995 return NULL;
1996 }
1997
1998 keyObject = PySequence_Fast_GET_ITEM(fast, 0);
1999
2000 if (!(key = StringToChars(keyObject, &todecref)))
2001 {
2002 Py_DECREF(iterator);
2003 Py_DECREF(fast);
2004 return NULL;
2005 }
2006
2007 di = dictitem_alloc(key);
2008
2009 Py_XDECREF(todecref);
2010
2011 if (di == NULL)
2012 {
2013 Py_DECREF(fast);
2014 Py_DECREF(iterator);
2015 PyErr_NoMemory();
2016 return NULL;
2017 }
2018 di->di_tv.v_lock = 0;
2019 di->di_tv.v_type = VAR_UNKNOWN;
2020
2021 valObject = PySequence_Fast_GET_ITEM(fast, 1);
2022
2023 if (ConvertFromPyObject(valObject, &di->di_tv) == -1)
2024 {
2025 Py_DECREF(iterator);
2026 Py_DECREF(fast);
2027 dictitem_free(di);
2028 return NULL;
2029 }
2030
2031 Py_DECREF(fast);
2032
2033 if (dict_add(dict, di) == FAIL)
2034 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002035 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002036 Py_DECREF(iterator);
2037 dictitem_free(di);
Bram Moolenaara9922d62013-05-30 13:01:18 +02002038 return NULL;
2039 }
2040 }
2041
2042 Py_DECREF(iterator);
2043
2044 /* Iterator may have finished due to an exception */
2045 if (PyErr_Occurred())
2046 return NULL;
2047 }
2048 }
2049 Py_INCREF(Py_None);
2050 return Py_None;
2051}
2052
2053 static PyObject *
2054DictionaryGet(DictionaryObject *self, PyObject *args)
2055{
2056 return _DictionaryItem(self, args,
2057 DICT_FLAG_HAS_DEFAULT|DICT_FLAG_NONE_DEFAULT);
2058}
2059
2060 static PyObject *
2061DictionaryPop(DictionaryObject *self, PyObject *args)
2062{
2063 return _DictionaryItem(self, args, DICT_FLAG_HAS_DEFAULT|DICT_FLAG_POP);
2064}
2065
2066 static PyObject *
Bram Moolenaarde71b562013-06-02 17:41:54 +02002067DictionaryPopItem(DictionaryObject *self)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002068{
Bram Moolenaarde71b562013-06-02 17:41:54 +02002069 hashitem_T *hi;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002070 PyObject *ret;
Bram Moolenaarde71b562013-06-02 17:41:54 +02002071 PyObject *valObject;
2072 dictitem_T *di;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002073
Bram Moolenaarde71b562013-06-02 17:41:54 +02002074 if (self->dict->dv_hashtab.ht_used == 0)
2075 {
2076 PyErr_SetNone(PyExc_KeyError);
2077 return NULL;
2078 }
2079
2080 hi = self->dict->dv_hashtab.ht_array;
2081 while (HASHITEM_EMPTY(hi))
2082 ++hi;
2083
2084 di = dict_lookup(hi);
2085
2086 if (!(valObject = ConvertToPyObject(&di->di_tv)))
Bram Moolenaara9922d62013-05-30 13:01:18 +02002087 return NULL;
2088
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002089 if (!(ret = Py_BuildValue("(" Py_bytes_fmt "O)", hi->hi_key, valObject)))
Bram Moolenaarde71b562013-06-02 17:41:54 +02002090 {
2091 Py_DECREF(valObject);
2092 return NULL;
2093 }
2094
2095 hash_remove(&self->dict->dv_hashtab, hi);
2096 dictitem_free(di);
2097
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002098 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02002099}
2100
2101 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02002102DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
Bram Moolenaara9922d62013-05-30 13:01:18 +02002103{
Bram Moolenaara9922d62013-05-30 13:01:18 +02002104 return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
2105}
2106
2107static PySequenceMethods DictionaryAsSeq = {
2108 0, /* sq_length */
2109 0, /* sq_concat */
2110 0, /* sq_repeat */
2111 0, /* sq_item */
2112 0, /* sq_slice */
2113 0, /* sq_ass_item */
2114 0, /* sq_ass_slice */
2115 (objobjproc) DictionaryContains, /* sq_contains */
2116 0, /* sq_inplace_concat */
2117 0, /* sq_inplace_repeat */
2118};
2119
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02002120static PyMappingMethods DictionaryAsMapping = {
2121 (lenfunc) DictionaryLength,
2122 (binaryfunc) DictionaryItem,
2123 (objobjargproc) DictionaryAssItem,
2124};
2125
Bram Moolenaardb913952012-06-29 12:54:53 +02002126static struct PyMethodDef DictionaryMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02002127 {"keys", (PyCFunction)DictionaryListKeys, METH_NOARGS, ""},
Bram Moolenaara9922d62013-05-30 13:01:18 +02002128 {"values", (PyCFunction)DictionaryListValues, METH_NOARGS, ""},
2129 {"items", (PyCFunction)DictionaryListItems, METH_NOARGS, ""},
2130 {"update", (PyCFunction)DictionaryUpdate, METH_VARARGS|METH_KEYWORDS, ""},
2131 {"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
2132 {"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
Bram Moolenaarde71b562013-06-02 17:41:54 +02002133 {"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
Bram Moolenaar389a1792013-06-23 13:00:44 +02002134 {"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002135 {"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
2136 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002137};
2138
2139static PyTypeObject ListType;
2140
2141typedef struct
2142{
2143 PyObject_HEAD
2144 list_T *list;
2145 pylinkedlist_T ref;
2146} ListObject;
2147
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002148#define NEW_LIST(list) ListNew(&ListType, list)
2149
Bram Moolenaardb913952012-06-29 12:54:53 +02002150 static PyObject *
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002151ListNew(PyTypeObject *subtype, list_T *list)
Bram Moolenaardb913952012-06-29 12:54:53 +02002152{
2153 ListObject *self;
2154
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002155 self = (ListObject *) subtype->tp_alloc(subtype, 0);
Bram Moolenaardb913952012-06-29 12:54:53 +02002156 if (self == NULL)
2157 return NULL;
2158 self->list = list;
2159 ++list->lv_refcount;
2160
2161 pyll_add((PyObject *)(self), &self->ref, &lastlist);
2162
2163 return (PyObject *)(self);
2164}
2165
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002166 static list_T *
Bram Moolenaarfb97f282013-07-09 17:42:46 +02002167py_list_alloc(void)
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002168{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002169 list_T *ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002170
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002171 if (!(ret = list_alloc()))
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002172 {
2173 PyErr_NoMemory();
2174 return NULL;
2175 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002176 ++ret->lv_refcount;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002177
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002178 return ret;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002179}
2180
2181 static int
2182list_py_concat(list_T *l, PyObject *obj, PyObject *lookup_dict)
2183{
2184 PyObject *iterator;
2185 PyObject *item;
2186 listitem_T *li;
2187
2188 if (!(iterator = PyObject_GetIter(obj)))
2189 return -1;
2190
2191 while ((item = PyIter_Next(iterator)))
2192 {
2193 if (!(li = listitem_alloc()))
2194 {
2195 PyErr_NoMemory();
2196 Py_DECREF(item);
2197 Py_DECREF(iterator);
2198 return -1;
2199 }
2200 li->li_tv.v_lock = 0;
2201 li->li_tv.v_type = VAR_UNKNOWN;
2202
2203 if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
2204 {
2205 Py_DECREF(item);
2206 Py_DECREF(iterator);
2207 listitem_free(li);
2208 return -1;
2209 }
2210
2211 Py_DECREF(item);
2212
2213 list_append(l, li);
2214 }
2215
2216 Py_DECREF(iterator);
2217
2218 /* Iterator may have finished due to an exception */
2219 if (PyErr_Occurred())
2220 return -1;
2221
2222 return 0;
2223}
2224
2225 static PyObject *
2226ListConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2227{
2228 list_T *list;
2229 PyObject *obj = NULL;
2230
2231 if (kwargs)
2232 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002233 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002234 N_("list constructor does not accept keyword arguments"));
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02002235 return NULL;
2236 }
2237
2238 if (!PyArg_ParseTuple(args, "|O", &obj))
2239 return NULL;
2240
2241 if (!(list = py_list_alloc()))
2242 return NULL;
2243
2244 if (obj)
2245 {
2246 PyObject *lookup_dict;
2247
2248 if (!(lookup_dict = PyDict_New()))
2249 {
2250 list_unref(list);
2251 return NULL;
2252 }
2253
2254 if (list_py_concat(list, obj, lookup_dict) == -1)
2255 {
2256 Py_DECREF(lookup_dict);
2257 list_unref(list);
2258 return NULL;
2259 }
2260
2261 Py_DECREF(lookup_dict);
2262 }
2263
2264 return ListNew(subtype, list);
2265}
2266
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002267 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002268ListDestructor(ListObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002269{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002270 pyll_remove(&self->ref, &lastlist);
2271 list_unref(self->list);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002272
2273 DESTRUCTOR_FINISH(self);
2274}
2275
Bram Moolenaardb913952012-06-29 12:54:53 +02002276 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02002277ListLength(ListObject *self)
Bram Moolenaardb913952012-06-29 12:54:53 +02002278{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002279 return ((PyInt) (self->list->lv_len));
Bram Moolenaardb913952012-06-29 12:54:53 +02002280}
2281
2282 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002283ListIndex(ListObject *self, Py_ssize_t index)
Bram Moolenaardb913952012-06-29 12:54:53 +02002284{
2285 listitem_T *li;
2286
Bram Moolenaard6e39182013-05-21 18:30:34 +02002287 if (index >= ListLength(self))
Bram Moolenaardb913952012-06-29 12:54:53 +02002288 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002289 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002290 return NULL;
2291 }
Bram Moolenaard6e39182013-05-21 18:30:34 +02002292 li = list_find(self->list, (long) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002293 if (li == NULL)
2294 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002295 /* No more suitable format specifications in python-2.3 */
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002296 PyErr_VIM_FORMAT(N_("internal error: failed to get vim list item %d"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02002297 (int) index);
Bram Moolenaardb913952012-06-29 12:54:53 +02002298 return NULL;
2299 }
2300 return ConvertToPyObject(&li->li_tv);
2301}
2302
Bram Moolenaardb913952012-06-29 12:54:53 +02002303 static PyObject *
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002304ListSlice(ListObject *self, Py_ssize_t first, Py_ssize_t step,
2305 Py_ssize_t slicelen)
Bram Moolenaardb913952012-06-29 12:54:53 +02002306{
2307 PyInt i;
Bram Moolenaardb913952012-06-29 12:54:53 +02002308 PyObject *list;
Bram Moolenaardb913952012-06-29 12:54:53 +02002309
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002310 if (step == 0)
2311 {
2312 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2313 return NULL;
2314 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002315
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002316 list = PyList_New(slicelen);
Bram Moolenaardb913952012-06-29 12:54:53 +02002317 if (list == NULL)
2318 return NULL;
2319
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002320 for (i = 0; i < slicelen; ++i)
Bram Moolenaardb913952012-06-29 12:54:53 +02002321 {
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002322 PyObject *item;
2323
2324 item = ListIndex(self, first + i*step);
Bram Moolenaardb913952012-06-29 12:54:53 +02002325 if (item == NULL)
2326 {
2327 Py_DECREF(list);
2328 return NULL;
2329 }
2330
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002331 PyList_SET_ITEM(list, i, item);
Bram Moolenaardb913952012-06-29 12:54:53 +02002332 }
2333
2334 return list;
2335}
2336
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002337 static PyObject *
2338ListItem(ListObject *self, PyObject* idx)
2339{
2340#if PY_MAJOR_VERSION < 3
2341 if (PyInt_Check(idx))
2342 {
2343 long _idx = PyInt_AsLong(idx);
2344 return ListIndex(self, _idx);
2345 }
2346 else
2347#endif
2348 if (PyLong_Check(idx))
2349 {
2350 long _idx = PyLong_AsLong(idx);
2351 return ListIndex(self, _idx);
2352 }
2353 else if (PySlice_Check(idx))
2354 {
2355 Py_ssize_t start, stop, step, slicelen;
2356
Bram Moolenaar922a4662014-03-30 16:11:43 +02002357 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002358 &start, &stop, &step, &slicelen) < 0)
2359 return NULL;
2360 return ListSlice(self, start, step, slicelen);
2361 }
2362 else
2363 {
2364 RAISE_INVALID_INDEX_TYPE(idx);
2365 return NULL;
2366 }
2367}
2368
2369 static void
2370list_restore(Py_ssize_t numadded, Py_ssize_t numreplaced, Py_ssize_t slicelen,
2371 list_T *l, listitem_T **lis, listitem_T *lastaddedli)
2372{
2373 while (numreplaced--)
2374 {
2375 list_insert(l, lis[numreplaced], lis[slicelen + numreplaced]);
2376 listitem_remove(l, lis[slicelen + numreplaced]);
2377 }
2378 while (numadded--)
2379 {
2380 listitem_T *next;
2381
2382 next = lastaddedli->li_prev;
2383 listitem_remove(l, lastaddedli);
2384 lastaddedli = next;
2385 }
2386}
2387
2388 static int
2389ListAssSlice(ListObject *self, Py_ssize_t first,
2390 Py_ssize_t step, Py_ssize_t slicelen, PyObject *obj)
2391{
2392 PyObject *iterator;
2393 PyObject *item;
2394 listitem_T *li;
2395 listitem_T *lastaddedli = NULL;
2396 listitem_T *next;
2397 typval_T v;
2398 list_T *l = self->list;
2399 PyInt i;
2400 PyInt j;
2401 PyInt numreplaced = 0;
2402 PyInt numadded = 0;
2403 PyInt size;
Bram Moolenaar3b522612014-02-11 16:00:35 +01002404 listitem_T **lis = NULL;
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002405
2406 size = ListLength(self);
2407
2408 if (l->lv_lock)
2409 {
2410 RAISE_LOCKED_LIST;
2411 return -1;
2412 }
2413
2414 if (step == 0)
2415 {
2416 PyErr_SET_STRING(PyExc_ValueError, N_("slice step cannot be zero"));
2417 return -1;
2418 }
2419
2420 if (step != 1 && slicelen == 0)
2421 {
2422 /* Nothing to do. Only error out if obj has some items. */
2423 int ret = 0;
2424
2425 if (obj == NULL)
2426 return 0;
2427
2428 if (!(iterator = PyObject_GetIter(obj)))
2429 return -1;
2430
2431 if ((item = PyIter_Next(iterator)))
2432 {
2433 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002434 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002435 "to extended slice"), 0);
2436 Py_DECREF(item);
2437 ret = -1;
2438 }
2439 Py_DECREF(iterator);
2440 return ret;
2441 }
2442
2443 if (obj != NULL)
2444 /* XXX May allocate zero bytes. */
2445 if (!(lis = PyMem_New(listitem_T *, slicelen * 2)))
2446 {
2447 PyErr_NoMemory();
2448 return -1;
2449 }
2450
2451 if (first == size)
2452 li = NULL;
2453 else
2454 {
2455 li = list_find(l, (long) first);
2456 if (li == NULL)
2457 {
2458 PyErr_VIM_FORMAT(N_("internal error: no vim list item %d"),
2459 (int)first);
2460 if (obj != NULL)
2461 PyMem_Free(lis);
2462 return -1;
2463 }
2464 i = slicelen;
2465 while (i-- && li != NULL)
2466 {
2467 j = step;
2468 next = li;
2469 if (step > 0)
2470 while (next != NULL && ((next = next->li_next) != NULL) && --j);
2471 else
2472 while (next != NULL && ((next = next->li_prev) != NULL) && ++j);
2473
2474 if (obj == NULL)
2475 listitem_remove(l, li);
2476 else
2477 lis[slicelen - i - 1] = li;
2478
2479 li = next;
2480 }
2481 if (li == NULL && i != -1)
2482 {
2483 PyErr_SET_VIM(N_("internal error: not enough list items"));
2484 if (obj != NULL)
2485 PyMem_Free(lis);
2486 return -1;
2487 }
2488 }
2489
2490 if (obj == NULL)
2491 return 0;
2492
2493 if (!(iterator = PyObject_GetIter(obj)))
2494 {
2495 PyMem_Free(lis);
2496 return -1;
2497 }
2498
2499 i = 0;
2500 while ((item = PyIter_Next(iterator)))
2501 {
2502 if (ConvertFromPyObject(item, &v) == -1)
2503 {
2504 Py_DECREF(iterator);
2505 Py_DECREF(item);
2506 PyMem_Free(lis);
2507 return -1;
2508 }
2509 Py_DECREF(item);
2510 if (list_insert_tv(l, &v, numreplaced < slicelen
2511 ? lis[numreplaced]
2512 : li) == FAIL)
2513 {
2514 clear_tv(&v);
2515 PyErr_SET_VIM(N_("internal error: failed to add item to list"));
2516 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2517 PyMem_Free(lis);
2518 return -1;
2519 }
2520 if (numreplaced < slicelen)
2521 {
2522 lis[slicelen + numreplaced] = lis[numreplaced]->li_prev;
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002523 vimlist_remove(l, lis[numreplaced], lis[numreplaced]);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002524 numreplaced++;
2525 }
2526 else
2527 {
2528 if (li)
2529 lastaddedli = li->li_prev;
2530 else
2531 lastaddedli = l->lv_last;
2532 numadded++;
2533 }
2534 clear_tv(&v);
2535 if (step != 1 && i >= slicelen)
2536 {
2537 Py_DECREF(iterator);
2538 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar4de6a212014-03-08 16:13:44 +01002539 N_("attempt to assign sequence of size greater than %d "
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002540 "to extended slice"), (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002541 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2542 PyMem_Free(lis);
2543 return -1;
2544 }
2545 ++i;
2546 }
2547 Py_DECREF(iterator);
2548
2549 if (step != 1 && i != slicelen)
2550 {
2551 PyErr_FORMAT2(PyExc_ValueError,
Bram Moolenaar403b3cf2014-02-15 15:59:03 +01002552 N_("attempt to assign sequence of size %d to extended slice "
2553 "of size %d"), (int) i, (int) slicelen);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002554 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2555 PyMem_Free(lis);
2556 return -1;
2557 }
2558
2559 if (PyErr_Occurred())
2560 {
2561 list_restore(numadded, numreplaced, slicelen, l, lis, lastaddedli);
2562 PyMem_Free(lis);
2563 return -1;
2564 }
2565
2566 for (i = 0; i < numreplaced; i++)
2567 listitem_free(lis[i]);
2568 if (step == 1)
2569 for (i = numreplaced; i < slicelen; i++)
2570 listitem_remove(l, lis[i]);
2571
2572 PyMem_Free(lis);
2573
2574 return 0;
2575}
2576
2577 static int
2578ListAssIndex(ListObject *self, Py_ssize_t index, PyObject *obj)
2579{
2580 typval_T tv;
2581 list_T *l = self->list;
2582 listitem_T *li;
2583 Py_ssize_t length = ListLength(self);
2584
2585 if (l->lv_lock)
2586 {
2587 RAISE_LOCKED_LIST;
2588 return -1;
2589 }
2590 if (index > length || (index == length && obj == NULL))
2591 {
2592 PyErr_SET_STRING(PyExc_IndexError, N_("list index out of range"));
2593 return -1;
2594 }
2595
2596 if (obj == NULL)
2597 {
2598 li = list_find(l, (long) index);
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02002599 vimlist_remove(l, li, li);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002600 clear_tv(&li->li_tv);
2601 vim_free(li);
2602 return 0;
2603 }
2604
2605 if (ConvertFromPyObject(obj, &tv) == -1)
2606 return -1;
2607
2608 if (index == length)
2609 {
2610 if (list_append_tv(l, &tv) == FAIL)
2611 {
2612 clear_tv(&tv);
2613 PyErr_SET_VIM(N_("failed to add item to list"));
2614 return -1;
2615 }
2616 }
2617 else
2618 {
2619 li = list_find(l, (long) index);
2620 clear_tv(&li->li_tv);
2621 copy_tv(&tv, &li->li_tv);
2622 clear_tv(&tv);
2623 }
2624 return 0;
2625}
2626
2627 static Py_ssize_t
2628ListAssItem(ListObject *self, PyObject *idx, PyObject *obj)
2629{
2630#if PY_MAJOR_VERSION < 3
2631 if (PyInt_Check(idx))
2632 {
2633 long _idx = PyInt_AsLong(idx);
2634 return ListAssIndex(self, _idx, obj);
2635 }
2636 else
2637#endif
2638 if (PyLong_Check(idx))
2639 {
2640 long _idx = PyLong_AsLong(idx);
2641 return ListAssIndex(self, _idx, obj);
2642 }
2643 else if (PySlice_Check(idx))
2644 {
2645 Py_ssize_t start, stop, step, slicelen;
2646
Bram Moolenaar922a4662014-03-30 16:11:43 +02002647 if (PySlice_GetIndicesEx((PySliceObject_T *)idx, ListLength(self),
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002648 &start, &stop, &step, &slicelen) < 0)
2649 return -1;
2650 return ListAssSlice(self, start, step, slicelen,
2651 obj);
2652 }
2653 else
2654 {
2655 RAISE_INVALID_INDEX_TYPE(idx);
2656 return -1;
2657 }
2658}
2659
2660 static PyObject *
2661ListConcatInPlace(ListObject *self, PyObject *obj)
2662{
2663 list_T *l = self->list;
2664 PyObject *lookup_dict;
2665
2666 if (l->lv_lock)
2667 {
2668 RAISE_LOCKED_LIST;
2669 return NULL;
2670 }
2671
2672 if (!(lookup_dict = PyDict_New()))
2673 return NULL;
2674
2675 if (list_py_concat(l, obj, lookup_dict) == -1)
2676 {
2677 Py_DECREF(lookup_dict);
2678 return NULL;
2679 }
2680 Py_DECREF(lookup_dict);
2681
2682 Py_INCREF(self);
2683 return (PyObject *)(self);
2684}
2685
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002686typedef struct
2687{
2688 listwatch_T lw;
2689 list_T *list;
2690} listiterinfo_T;
2691
2692 static void
2693ListIterDestruct(listiterinfo_T *lii)
2694{
2695 list_rem_watch(lii->list, &lii->lw);
2696 PyMem_Free(lii);
2697}
2698
2699 static PyObject *
2700ListIterNext(listiterinfo_T **lii)
2701{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002702 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002703
2704 if (!((*lii)->lw.lw_item))
2705 return NULL;
2706
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002707 if (!(ret = ConvertToPyObject(&((*lii)->lw.lw_item->li_tv))))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002708 return NULL;
2709
2710 (*lii)->lw.lw_item = (*lii)->lw.lw_item->li_next;
2711
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002712 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002713}
2714
2715 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002716ListIter(ListObject *self)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002717{
2718 listiterinfo_T *lii;
Bram Moolenaard6e39182013-05-21 18:30:34 +02002719 list_T *l = self->list;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002720
2721 if (!(lii = PyMem_New(listiterinfo_T, 1)))
2722 {
2723 PyErr_NoMemory();
2724 return NULL;
2725 }
2726
2727 list_add_watch(l, &lii->lw);
2728 lii->lw.lw_item = l->lv_first;
2729 lii->list = l;
2730
2731 return IterNew(lii,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002732 (destructorfun) ListIterDestruct, (nextfun) ListIterNext,
2733 NULL, NULL);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02002734}
2735
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002736static char *ListAttrs[] = {
2737 "locked",
2738 NULL
2739};
2740
2741 static PyObject *
2742ListDir(PyObject *self)
2743{
2744 return ObjectDir(self, ListAttrs);
2745}
2746
Bram Moolenaar66b79852012-09-21 14:00:35 +02002747 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002748ListSetattr(ListObject *self, char *name, PyObject *valObject)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002749{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002750 if (valObject == NULL)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002751 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002752 PyErr_SET_STRING(PyExc_AttributeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002753 N_("cannot delete vim.List attributes"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002754 return -1;
2755 }
2756
2757 if (strcmp(name, "locked") == 0)
2758 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02002759 if (self->list->lv_lock == VAR_FIXED)
Bram Moolenaar66b79852012-09-21 14:00:35 +02002760 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002761 PyErr_SET_STRING(PyExc_TypeError, N_("cannot modify fixed list"));
Bram Moolenaar66b79852012-09-21 14:00:35 +02002762 return -1;
2763 }
2764 else
2765 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002766 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarb983f752013-05-15 16:11:50 +02002767 if (istrue == -1)
2768 return -1;
2769 else if (istrue)
Bram Moolenaard6e39182013-05-21 18:30:34 +02002770 self->list->lv_lock = VAR_LOCKED;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002771 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02002772 self->list->lv_lock = 0;
Bram Moolenaar66b79852012-09-21 14:00:35 +02002773 }
2774 return 0;
2775 }
2776 else
2777 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002778 PyErr_FORMAT(PyExc_AttributeError, N_("cannot set attribute %s"), name);
Bram Moolenaar66b79852012-09-21 14:00:35 +02002779 return -1;
2780 }
2781}
2782
Bram Moolenaar063a46b2014-01-14 16:36:51 +01002783static PySequenceMethods ListAsSeq = {
2784 (lenfunc) ListLength, /* sq_length, len(x) */
2785 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
2786 0, /* RangeRepeat, sq_repeat, x*n */
2787 (PyIntArgFunc) ListIndex, /* sq_item, x[i] */
2788 0, /* was_sq_slice, x[i:j] */
2789 (PyIntObjArgProc) ListAssIndex, /* sq_as_item, x[i]=v */
2790 0, /* was_sq_ass_slice, x[i:j]=v */
2791 0, /* sq_contains */
2792 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
2793 0, /* sq_inplace_repeat */
2794};
2795
2796static PyMappingMethods ListAsMapping = {
2797 /* mp_length */ (lenfunc) ListLength,
2798 /* mp_subscript */ (binaryfunc) ListItem,
2799 /* mp_ass_subscript */ (objobjargproc) ListAssItem,
2800};
2801
Bram Moolenaardb913952012-06-29 12:54:53 +02002802static struct PyMethodDef ListMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002803 {"extend", (PyCFunction)ListConcatInPlace, METH_O, ""},
2804 {"__dir__", (PyCFunction)ListDir, METH_NOARGS, ""},
2805 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002806};
2807
2808typedef struct
2809{
2810 PyObject_HEAD
2811 char_u *name;
2812} FunctionObject;
2813
2814static PyTypeObject FunctionType;
2815
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002816#define NEW_FUNCTION(name) FunctionNew(&FunctionType, name)
2817
Bram Moolenaardb913952012-06-29 12:54:53 +02002818 static PyObject *
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002819FunctionNew(PyTypeObject *subtype, char_u *name)
Bram Moolenaardb913952012-06-29 12:54:53 +02002820{
2821 FunctionObject *self;
2822
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002823 self = (FunctionObject *) subtype->tp_alloc(subtype, 0);
2824
Bram Moolenaardb913952012-06-29 12:54:53 +02002825 if (self == NULL)
2826 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002827
2828 if (isdigit(*name))
Bram Moolenaardb913952012-06-29 12:54:53 +02002829 {
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002830 if (!translated_function_exists(name))
2831 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02002832 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002833 N_("unnamed function %s does not exist"), name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002834 return NULL;
2835 }
2836 self->name = vim_strsave(name);
2837 func_ref(self->name);
2838 }
2839 else
Bram Moolenaar018acca2013-05-30 13:37:28 +02002840 if ((self->name = get_expanded_name(name,
2841 vim_strchr(name, AUTOLOAD_CHAR) == NULL))
2842 == NULL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002843 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002844 PyErr_FORMAT(PyExc_ValueError,
2845 N_("function %s does not exist"), name);
Bram Moolenaar018acca2013-05-30 13:37:28 +02002846 return NULL;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002847 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002848
2849 return (PyObject *)(self);
2850}
2851
2852 static PyObject *
2853FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
2854{
2855 PyObject *self;
2856 char_u *name;
2857
2858 if (kwargs)
2859 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02002860 PyErr_SET_STRING(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002861 N_("function constructor does not accept keyword arguments"));
Bram Moolenaardb913952012-06-29 12:54:53 +02002862 return NULL;
2863 }
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002864
Bram Moolenaar389a1792013-06-23 13:00:44 +02002865 if (!PyArg_ParseTuple(args, "et", "ascii", &name))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002866 return NULL;
2867
2868 self = FunctionNew(subtype, name);
2869
Bram Moolenaar389a1792013-06-23 13:00:44 +02002870 PyMem_Free(name);
2871
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002872 return self;
Bram Moolenaardb913952012-06-29 12:54:53 +02002873}
2874
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002875 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02002876FunctionDestructor(FunctionObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002877{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002878 func_unref(self->name);
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02002879 vim_free(self->name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02002880
2881 DESTRUCTOR_FINISH(self);
2882}
2883
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002884static char *FunctionAttrs[] = {
2885 "softspace",
2886 NULL
2887};
2888
2889 static PyObject *
2890FunctionDir(PyObject *self)
2891{
2892 return ObjectDir(self, FunctionAttrs);
2893}
2894
Bram Moolenaardb913952012-06-29 12:54:53 +02002895 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02002896FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
Bram Moolenaardb913952012-06-29 12:54:53 +02002897{
Bram Moolenaard6e39182013-05-21 18:30:34 +02002898 char_u *name = self->name;
Bram Moolenaardb913952012-06-29 12:54:53 +02002899 typval_T args;
2900 typval_T selfdicttv;
2901 typval_T rettv;
2902 dict_T *selfdict = NULL;
2903 PyObject *selfdictObject;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002904 PyObject *ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002905 int error;
2906
2907 if (ConvertFromPyObject(argsObject, &args) == -1)
2908 return NULL;
2909
2910 if (kwargs != NULL)
2911 {
2912 selfdictObject = PyDict_GetItemString(kwargs, "self");
2913 if (selfdictObject != NULL)
2914 {
Bram Moolenaara9922d62013-05-30 13:01:18 +02002915 if (ConvertFromPyMapping(selfdictObject, &selfdicttv) == -1)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002916 {
2917 clear_tv(&args);
Bram Moolenaardb913952012-06-29 12:54:53 +02002918 return NULL;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002919 }
Bram Moolenaardb913952012-06-29 12:54:53 +02002920 selfdict = selfdicttv.vval.v_dict;
2921 }
2922 }
2923
Bram Moolenaar71700b82013-05-15 17:49:05 +02002924 Py_BEGIN_ALLOW_THREADS
2925 Python_Lock_Vim();
2926
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002927 VimTryStart();
Bram Moolenaardb913952012-06-29 12:54:53 +02002928 error = func_call(name, &args, selfdict, &rettv);
Bram Moolenaar71700b82013-05-15 17:49:05 +02002929
2930 Python_Release_Vim();
2931 Py_END_ALLOW_THREADS
2932
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002933 if (VimTryEnd())
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002934 ret = NULL;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02002935 else if (error != OK)
Bram Moolenaardb913952012-06-29 12:54:53 +02002936 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002937 ret = NULL;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02002938 PyErr_VIM_FORMAT(N_("failed to run function %s"), (char *)name);
Bram Moolenaardb913952012-06-29 12:54:53 +02002939 }
2940 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002941 ret = ConvertToPyObject(&rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002942
Bram Moolenaardb913952012-06-29 12:54:53 +02002943 clear_tv(&args);
2944 clear_tv(&rettv);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02002945 if (selfdict != NULL)
2946 clear_tv(&selfdicttv);
Bram Moolenaardb913952012-06-29 12:54:53 +02002947
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002948 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02002949}
2950
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002951 static PyObject *
2952FunctionRepr(FunctionObject *self)
2953{
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002954#ifdef Py_TRACE_REFS
Bram Moolenaar41009372013-07-01 22:03:04 +02002955 /* For unknown reason self->name may be NULL after calling
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002956 * Finalize */
2957 return PyString_FromFormat("<vim.Function '%s'>",
Bram Moolenaar41009372013-07-01 22:03:04 +02002958 (self->name == NULL ? "<NULL>" : (char *)self->name));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002959#else
Bram Moolenaar41009372013-07-01 22:03:04 +02002960 return PyString_FromFormat("<vim.Function '%s'>", (char *)self->name);
Bram Moolenaar841fbd22013-06-23 14:37:07 +02002961#endif
Bram Moolenaara5b725c2013-05-30 12:43:54 +02002962}
2963
Bram Moolenaardb913952012-06-29 12:54:53 +02002964static struct PyMethodDef FunctionMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02002965 {"__dir__", (PyCFunction)FunctionDir, METH_NOARGS, ""},
2966 { NULL, NULL, 0, NULL}
Bram Moolenaardb913952012-06-29 12:54:53 +02002967};
2968
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002969/*
2970 * Options object
2971 */
2972
2973static PyTypeObject OptionsType;
2974
2975typedef int (*checkfun)(void *);
2976
2977typedef struct
2978{
2979 PyObject_HEAD
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01002980 int opt_type;
2981 void *from;
2982 checkfun Check;
2983 PyObject *fromObj;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02002984} OptionsObject;
2985
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002986 static int
2987dummy_check(void *arg UNUSED)
2988{
2989 return 0;
2990}
2991
2992 static PyObject *
2993OptionsNew(int opt_type, void *from, checkfun Check, PyObject *fromObj)
2994{
2995 OptionsObject *self;
2996
Bram Moolenaar774267b2013-05-21 20:51:59 +02002997 self = PyObject_GC_New(OptionsObject, &OptionsType);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02002998 if (self == NULL)
2999 return NULL;
3000
3001 self->opt_type = opt_type;
3002 self->from = from;
3003 self->Check = Check;
3004 self->fromObj = fromObj;
3005 if (fromObj)
3006 Py_INCREF(fromObj);
3007
3008 return (PyObject *)(self);
3009}
3010
3011 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003012OptionsDestructor(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003013{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003014 PyObject_GC_UnTrack((void *)(self));
3015 Py_XDECREF(self->fromObj);
3016 PyObject_GC_Del((void *)(self));
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003017}
3018
3019 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003020OptionsTraverse(OptionsObject *self, visitproc visit, void *arg)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003021{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003022 Py_VISIT(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003023 return 0;
3024}
3025
3026 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003027OptionsClear(OptionsObject *self)
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003028{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003029 Py_CLEAR(self->fromObj);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02003030 return 0;
3031}
3032
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003033 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003034OptionsItem(OptionsObject *self, PyObject *keyObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003035{
3036 char_u *key;
3037 int flags;
3038 long numval;
3039 char_u *stringval;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003040 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003041
Bram Moolenaard6e39182013-05-21 18:30:34 +02003042 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003043 return NULL;
3044
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003045 if (!(key = StringToChars(keyObject, &todecref)))
3046 return NULL;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003047
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003048 if (*key == NUL)
3049 {
3050 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003051 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003052 return NULL;
3053 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003054
3055 flags = get_option_value_strict(key, &numval, &stringval,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003056 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003057
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003058 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003059
3060 if (flags == 0)
3061 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003062 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003063 return NULL;
3064 }
3065
3066 if (flags & SOPT_UNSET)
3067 {
3068 Py_INCREF(Py_None);
3069 return Py_None;
3070 }
3071 else if (flags & SOPT_BOOL)
3072 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003073 PyObject *ret;
3074 ret = numval ? Py_True : Py_False;
3075 Py_INCREF(ret);
3076 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003077 }
3078 else if (flags & SOPT_NUM)
3079 return PyInt_FromLong(numval);
3080 else if (flags & SOPT_STRING)
3081 {
3082 if (stringval)
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003083 {
Bram Moolenaar41009372013-07-01 22:03:04 +02003084 PyObject *ret = PyBytes_FromString((char *)stringval);
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003085 vim_free(stringval);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003086 return ret;
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003087 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003088 else
3089 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02003090 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003091 N_("unable to get option value"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003092 return NULL;
3093 }
3094 }
3095 else
3096 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003097 PyErr_SET_VIM(N_("internal error: unknown option type"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003098 return NULL;
3099 }
3100}
3101
3102 static int
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003103OptionsContains(OptionsObject *self, PyObject *keyObject)
3104{
3105 char_u *key;
3106 PyObject *todecref;
3107
3108 if (!(key = StringToChars(keyObject, &todecref)))
3109 return -1;
3110
3111 if (*key == NUL)
3112 {
3113 Py_XDECREF(todecref);
3114 return 0;
3115 }
3116
3117 if (get_option_value_strict(key, NULL, NULL, self->opt_type, NULL))
3118 {
3119 Py_XDECREF(todecref);
3120 return 1;
3121 }
3122 else
3123 {
3124 Py_XDECREF(todecref);
3125 return 0;
3126 }
3127}
3128
3129typedef struct
3130{
3131 void *lastoption;
3132 int opt_type;
3133} optiterinfo_T;
3134
3135 static PyObject *
3136OptionsIterNext(optiterinfo_T **oii)
3137{
3138 char_u *name;
3139
3140 if ((name = option_iter_next(&((*oii)->lastoption), (*oii)->opt_type)))
3141 return PyString_FromString((char *)name);
3142
3143 return NULL;
3144}
3145
3146 static PyObject *
3147OptionsIter(OptionsObject *self)
3148{
3149 optiterinfo_T *oii;
3150
3151 if (!(oii = PyMem_New(optiterinfo_T, 1)))
3152 {
3153 PyErr_NoMemory();
3154 return NULL;
3155 }
3156
3157 oii->opt_type = self->opt_type;
3158 oii->lastoption = NULL;
3159
3160 return IterNew(oii,
3161 (destructorfun) PyMem_Free, (nextfun) OptionsIterNext,
3162 NULL, NULL);
3163}
3164
3165 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003166set_option_value_err(char_u *key, int numval, char_u *stringval, int opt_flags)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003167{
3168 char_u *errmsg;
3169
3170 if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
3171 {
3172 if (VimTryEnd())
3173 return FAIL;
3174 PyErr_SetVim((char *)errmsg);
3175 return FAIL;
3176 }
3177 return OK;
3178}
3179
3180 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02003181set_option_value_for(
3182 char_u *key,
3183 int numval,
3184 char_u *stringval,
3185 int opt_flags,
3186 int opt_type,
3187 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003188{
Bram Moolenaar0b9aecc2013-05-21 22:13:41 +02003189 win_T *save_curwin = NULL;
3190 tabpage_T *save_curtab = NULL;
3191 buf_T *save_curbuf = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003192 int set_ret = 0;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003193
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003194 VimTryStart();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003195 switch (opt_type)
3196 {
3197 case SREQ_WIN:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003198 if (switch_win(&save_curwin, &save_curtab, (win_T *)from,
Bram Moolenaard6949742013-06-16 14:18:28 +02003199 win_find_tabpage((win_T *)from), FALSE) == FAIL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003200 {
Bram Moolenaarae38d052014-12-17 14:46:09 +01003201 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003202 if (VimTryEnd())
3203 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003204 PyErr_SET_VIM(N_("problem while switching windows"));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003205 return -1;
3206 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003207 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
3208 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003209 break;
3210 case SREQ_BUF:
Bram Moolenaar105bc352013-05-17 16:03:57 +02003211 switch_buffer(&save_curbuf, (buf_T *)from);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003212 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar105bc352013-05-17 16:03:57 +02003213 restore_buffer(save_curbuf);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003214 break;
3215 case SREQ_GLOBAL:
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003216 set_ret = set_option_value_err(key, numval, stringval, opt_flags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003217 break;
3218 }
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003219 if (set_ret == FAIL)
3220 return -1;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003221 return VimTryEnd();
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003222}
3223
3224 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003225OptionsAssItem(OptionsObject *self, PyObject *keyObject, PyObject *valObject)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003226{
3227 char_u *key;
3228 int flags;
3229 int opt_flags;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003230 int ret = 0;
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003231 PyObject *todecref;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003232
Bram Moolenaard6e39182013-05-21 18:30:34 +02003233 if (self->Check(self->from))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003234 return -1;
3235
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003236 if (!(key = StringToChars(keyObject, &todecref)))
3237 return -1;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003238
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003239 if (*key == NUL)
3240 {
3241 RAISE_NO_EMPTY_KEYS;
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02003242 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003243 return -1;
3244 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003245
3246 flags = get_option_value_strict(key, NULL, NULL,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003247 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003248
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003249 if (flags == 0)
3250 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02003251 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003252 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003253 return -1;
3254 }
3255
3256 if (valObject == NULL)
3257 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003258 if (self->opt_type == SREQ_GLOBAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003259 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003260 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003261 N_("unable to unset global option %s"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003262 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003263 return -1;
3264 }
3265 else if (!(flags & SOPT_GLOBAL))
3266 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02003267 PyErr_FORMAT(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003268 N_("unable to unset option %s "
3269 "which does not have global value"), key);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003270 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003271 return -1;
3272 }
3273 else
3274 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003275 unset_global_local_option(key, self->from);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003276 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003277 return 0;
3278 }
3279 }
3280
Bram Moolenaard6e39182013-05-21 18:30:34 +02003281 opt_flags = (self->opt_type ? OPT_LOCAL : OPT_GLOBAL);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003282
3283 if (flags & SOPT_BOOL)
3284 {
Bram Moolenaarb983f752013-05-15 16:11:50 +02003285 int istrue = PyObject_IsTrue(valObject);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02003286
Bram Moolenaarb983f752013-05-15 16:11:50 +02003287 if (istrue == -1)
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003288 ret = -1;
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003289 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003290 ret = set_option_value_for(key, istrue, NULL,
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003291 opt_flags, self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003292 }
3293 else if (flags & SOPT_NUM)
3294 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003295 long val;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003296
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003297 if (NumberToLong(valObject, &val, NUMBER_INT))
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003298 {
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003299 Py_XDECREF(todecref);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003300 return -1;
3301 }
3302
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003303 ret = set_option_value_for(key, (int) val, NULL, opt_flags,
Bram Moolenaard6e39182013-05-21 18:30:34 +02003304 self->opt_type, self->from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003305 }
3306 else
3307 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003308 char_u *val;
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003309 PyObject *todecref2;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003310
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003311 if ((val = StringToChars(valObject, &todecref2)))
3312 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003313 ret = set_option_value_for(key, 0, val, opt_flags,
Bram Moolenaare9ba5162013-05-29 22:02:22 +02003314 self->opt_type, self->from);
Bram Moolenaarc2401d62013-12-07 14:28:43 +01003315 Py_XDECREF(todecref2);
3316 }
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003317 else
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003318 ret = -1;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003319 }
3320
Bram Moolenaar35eacd72013-05-30 22:06:33 +02003321 Py_XDECREF(todecref);
Bram Moolenaar1bc24282013-05-29 21:37:35 +02003322
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003323 return ret;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003324}
3325
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003326static PySequenceMethods OptionsAsSeq = {
3327 0, /* sq_length */
3328 0, /* sq_concat */
3329 0, /* sq_repeat */
3330 0, /* sq_item */
3331 0, /* sq_slice */
3332 0, /* sq_ass_item */
3333 0, /* sq_ass_slice */
3334 (objobjproc) OptionsContains, /* sq_contains */
3335 0, /* sq_inplace_concat */
3336 0, /* sq_inplace_repeat */
3337};
3338
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003339static PyMappingMethods OptionsAsMapping = {
3340 (lenfunc) NULL,
3341 (binaryfunc) OptionsItem,
3342 (objobjargproc) OptionsAssItem,
3343};
3344
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003345/* Tabpage object
3346 */
3347
3348typedef struct
3349{
3350 PyObject_HEAD
3351 tabpage_T *tab;
3352} TabPageObject;
3353
3354static PyObject *WinListNew(TabPageObject *tabObject);
3355
3356static PyTypeObject TabPageType;
3357
3358 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003359CheckTabPage(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003360{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003361 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003362 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003363 PyErr_SET_VIM(N_("attempt to refer to deleted tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003364 return -1;
3365 }
3366
3367 return 0;
3368}
3369
3370 static PyObject *
3371TabPageNew(tabpage_T *tab)
3372{
3373 TabPageObject *self;
3374
3375 if (TAB_PYTHON_REF(tab))
3376 {
3377 self = TAB_PYTHON_REF(tab);
3378 Py_INCREF(self);
3379 }
3380 else
3381 {
3382 self = PyObject_NEW(TabPageObject, &TabPageType);
3383 if (self == NULL)
3384 return NULL;
3385 self->tab = tab;
3386 TAB_PYTHON_REF(tab) = self;
3387 }
3388
3389 return (PyObject *)(self);
3390}
3391
3392 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003393TabPageDestructor(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003394{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003395 if (self->tab && self->tab != INVALID_TABPAGE_VALUE)
3396 TAB_PYTHON_REF(self->tab) = NULL;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003397
3398 DESTRUCTOR_FINISH(self);
3399}
3400
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003401static char *TabPageAttrs[] = {
3402 "windows", "number", "vars", "window", "valid",
3403 NULL
3404};
3405
3406 static PyObject *
3407TabPageDir(PyObject *self)
3408{
3409 return ObjectDir(self, TabPageAttrs);
3410}
3411
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003412 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003413TabPageAttrValid(TabPageObject *self, char *name)
3414{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003415 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003416
3417 if (strcmp(name, "valid") != 0)
3418 return NULL;
3419
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003420 ret = ((self->tab == INVALID_TABPAGE_VALUE) ? Py_False : Py_True);
3421 Py_INCREF(ret);
3422 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003423}
3424
3425 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003426TabPageAttr(TabPageObject *self, char *name)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003427{
3428 if (strcmp(name, "windows") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003429 return WinListNew(self);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003430 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003431 return PyLong_FromLong((long) get_tab_number(self->tab));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003432 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003433 return NEW_DICTIONARY(self->tab->tp_vars);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003434 else if (strcmp(name, "window") == 0)
3435 {
3436 /* For current tab window.c does not bother to set or update tp_curwin
3437 */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003438 if (self->tab == curtab)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003439 return WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003440 else
Bram Moolenaard6e39182013-05-21 18:30:34 +02003441 return WindowNew(self->tab->tp_curwin, self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003442 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003443 else if (strcmp(name, "__members__") == 0)
3444 return ObjectDir(NULL, TabPageAttrs);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003445 return NULL;
3446}
3447
3448 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003449TabPageRepr(TabPageObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003450{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003451 if (self->tab == INVALID_TABPAGE_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003452 return PyString_FromFormat("<tabpage object (deleted) at %p>", (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003453 else
3454 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003455 int t = get_tab_number(self->tab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003456
3457 if (t == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003458 return PyString_FromFormat("<tabpage object (unknown) at %p>",
3459 (self));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003460 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003461 return PyString_FromFormat("<tabpage %d>", t - 1);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003462 }
3463}
3464
3465static struct PyMethodDef TabPageMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003466 /* name, function, calling, documentation */
3467 {"__dir__", (PyCFunction)TabPageDir, METH_NOARGS, ""},
3468 { NULL, NULL, 0, NULL}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003469};
3470
3471/*
3472 * Window list object
3473 */
3474
3475static PyTypeObject TabListType;
3476static PySequenceMethods TabListAsSeq;
3477
3478typedef struct
3479{
3480 PyObject_HEAD
3481} TabListObject;
3482
3483 static PyInt
3484TabListLength(PyObject *self UNUSED)
3485{
3486 tabpage_T *tp = first_tabpage;
3487 PyInt n = 0;
3488
3489 while (tp != NULL)
3490 {
3491 ++n;
3492 tp = tp->tp_next;
3493 }
3494
3495 return n;
3496}
3497
3498 static PyObject *
3499TabListItem(PyObject *self UNUSED, PyInt n)
3500{
3501 tabpage_T *tp;
3502
3503 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, --n)
3504 if (n == 0)
3505 return TabPageNew(tp);
3506
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003507 PyErr_SET_STRING(PyExc_IndexError, N_("no such tab page"));
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003508 return NULL;
3509}
3510
Bram Moolenaar6c85e7f2013-06-23 12:51:32 +02003511/*
3512 * Window object
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003513 */
3514
3515typedef struct
3516{
3517 PyObject_HEAD
3518 win_T *win;
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003519 TabPageObject *tabObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003520} WindowObject;
3521
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003522static PyTypeObject WindowType;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003523
3524 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02003525CheckWindow(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003526{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003527 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003528 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003529 PyErr_SET_VIM(N_("attempt to refer to deleted window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003530 return -1;
3531 }
3532
3533 return 0;
3534}
3535
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003536 static PyObject *
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003537WindowNew(win_T *win, tabpage_T *tab)
Bram Moolenaar971db462013-05-12 18:44:48 +02003538{
3539 /* We need to handle deletion of windows underneath us.
3540 * If we add a "w_python*_ref" field to the win_T structure,
3541 * then we can get at it in win_free() in vim. We then
3542 * need to create only ONE Python object per window - if
3543 * we try to create a second, just INCREF the existing one
3544 * and return it. The (single) Python object referring to
3545 * the window is stored in "w_python*_ref".
3546 * On a win_free() we set the Python object's win_T* field
3547 * to an invalid value. We trap all uses of a window
3548 * object, and reject them if the win_T* field is invalid.
3549 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003550 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02003551 * w_python_ref and w_python3_ref fields respectively.
3552 */
3553
3554 WindowObject *self;
3555
3556 if (WIN_PYTHON_REF(win))
3557 {
3558 self = WIN_PYTHON_REF(win);
3559 Py_INCREF(self);
3560 }
3561 else
3562 {
Bram Moolenaar774267b2013-05-21 20:51:59 +02003563 self = PyObject_GC_New(WindowObject, &WindowType);
Bram Moolenaar971db462013-05-12 18:44:48 +02003564 if (self == NULL)
3565 return NULL;
3566 self->win = win;
3567 WIN_PYTHON_REF(win) = self;
3568 }
3569
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003570 self->tabObject = ((TabPageObject *)(TabPageNew(tab)));
3571
Bram Moolenaar971db462013-05-12 18:44:48 +02003572 return (PyObject *)(self);
3573}
3574
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003575 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003576WindowDestructor(WindowObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003577{
Bram Moolenaar774267b2013-05-21 20:51:59 +02003578 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaard6e39182013-05-21 18:30:34 +02003579 if (self->win && self->win != INVALID_WINDOW_VALUE)
3580 WIN_PYTHON_REF(self->win) = NULL;
Bram Moolenaar774267b2013-05-21 20:51:59 +02003581 Py_XDECREF(((PyObject *)(self->tabObject)));
3582 PyObject_GC_Del((void *)(self));
3583}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003584
Bram Moolenaar774267b2013-05-21 20:51:59 +02003585 static int
3586WindowTraverse(WindowObject *self, visitproc visit, void *arg)
3587{
3588 Py_VISIT(((PyObject *)(self->tabObject)));
3589 return 0;
3590}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003591
Bram Moolenaar774267b2013-05-21 20:51:59 +02003592 static int
3593WindowClear(WindowObject *self)
3594{
3595 Py_CLEAR(self->tabObject);
3596 return 0;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003597}
3598
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003599 static win_T *
3600get_firstwin(TabPageObject *tabObject)
3601{
3602 if (tabObject)
3603 {
3604 if (CheckTabPage(tabObject))
3605 return NULL;
3606 /* For current tab window.c does not bother to set or update tp_firstwin
3607 */
3608 else if (tabObject->tab == curtab)
3609 return firstwin;
3610 else
3611 return tabObject->tab->tp_firstwin;
3612 }
3613 else
3614 return firstwin;
3615}
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003616static char *WindowAttrs[] = {
3617 "buffer", "cursor", "height", "vars", "options", "number", "row", "col",
3618 "tabpage", "valid",
3619 NULL
3620};
3621
3622 static PyObject *
3623WindowDir(PyObject *self)
3624{
3625 return ObjectDir(self, WindowAttrs);
3626}
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003627
Bram Moolenaar971db462013-05-12 18:44:48 +02003628 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003629WindowAttrValid(WindowObject *self, char *name)
3630{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003631 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003632
3633 if (strcmp(name, "valid") != 0)
3634 return NULL;
3635
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003636 ret = ((self->win == INVALID_WINDOW_VALUE) ? Py_False : Py_True);
3637 Py_INCREF(ret);
3638 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02003639}
3640
3641 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003642WindowAttr(WindowObject *self, char *name)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003643{
3644 if (strcmp(name, "buffer") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003645 return (PyObject *)BufferNew(self->win->w_buffer);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003646 else if (strcmp(name, "cursor") == 0)
3647 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003648 pos_T *pos = &self->win->w_cursor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003649
3650 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
3651 }
3652 else if (strcmp(name, "height") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003653 return PyLong_FromLong((long)(self->win->w_height));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003654#ifdef FEAT_WINDOWS
3655 else if (strcmp(name, "row") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003656 return PyLong_FromLong((long)(self->win->w_winrow));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003657#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003658#ifdef FEAT_VERTSPLIT
3659 else if (strcmp(name, "width") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003660 return PyLong_FromLong((long)(W_WIDTH(self->win)));
Bram Moolenaar4e5dfb52013-05-12 19:30:31 +02003661 else if (strcmp(name, "col") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003662 return PyLong_FromLong((long)(W_WINCOL(self->win)));
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003663#endif
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02003664 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02003665 return NEW_DICTIONARY(self->win->w_vars);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003666 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003667 return OptionsNew(SREQ_WIN, self->win, (checkfun) CheckWindow,
3668 (PyObject *) self);
Bram Moolenaar6d216452013-05-12 19:00:41 +02003669 else if (strcmp(name, "number") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003670 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003671 if (CheckTabPage(self->tabObject))
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003672 return NULL;
3673 return PyLong_FromLong((long)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003674 get_win_number(self->win, get_firstwin(self->tabObject)));
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003675 }
3676 else if (strcmp(name, "tabpage") == 0)
3677 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003678 Py_INCREF(self->tabObject);
3679 return (PyObject *)(self->tabObject);
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02003680 }
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003681 else if (strcmp(name, "__members__") == 0)
3682 return ObjectDir(NULL, WindowAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003683 else
3684 return NULL;
3685}
3686
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003687 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003688WindowSetattr(WindowObject *self, char *name, PyObject *valObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003689{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003690 if (CheckWindow(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003691 return -1;
3692
3693 if (strcmp(name, "buffer") == 0)
3694 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003695 PyErr_SET_STRING(PyExc_TypeError, N_("readonly attribute: buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003696 return -1;
3697 }
3698 else if (strcmp(name, "cursor") == 0)
3699 {
3700 long lnum;
3701 long col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003702
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003703 if (!PyArg_Parse(valObject, "(ll)", &lnum, &col))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003704 return -1;
3705
Bram Moolenaard6e39182013-05-21 18:30:34 +02003706 if (lnum <= 0 || lnum > self->win->w_buffer->b_ml.ml_line_count)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003707 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003708 PyErr_SET_VIM(N_("cursor position outside buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003709 return -1;
3710 }
3711
3712 /* Check for keyboard interrupts */
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003713 if (VimCheckInterrupt())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003714 return -1;
3715
Bram Moolenaard6e39182013-05-21 18:30:34 +02003716 self->win->w_cursor.lnum = lnum;
3717 self->win->w_cursor.col = col;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003718#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard6e39182013-05-21 18:30:34 +02003719 self->win->w_cursor.coladd = 0;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003720#endif
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003721 /* When column is out of range silently correct it. */
Bram Moolenaard6e39182013-05-21 18:30:34 +02003722 check_cursor_col_win(self->win);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003723
Bram Moolenaar03a807a2011-07-07 15:08:58 +02003724 update_screen(VALID);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003725 return 0;
3726 }
3727 else if (strcmp(name, "height") == 0)
3728 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003729 long height;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003730 win_T *savewin;
3731
Bram Moolenaardee2e312013-06-23 16:35:47 +02003732 if (NumberToLong(valObject, &height, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003733 return -1;
3734
3735#ifdef FEAT_GUI
3736 need_mouse_correct = TRUE;
3737#endif
3738 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003739 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003740
3741 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003742 win_setheight((int) height);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003743 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003744 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003745 return -1;
3746
3747 return 0;
3748 }
3749#ifdef FEAT_VERTSPLIT
3750 else if (strcmp(name, "width") == 0)
3751 {
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003752 long width;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003753 win_T *savewin;
3754
Bram Moolenaardee2e312013-06-23 16:35:47 +02003755 if (NumberToLong(valObject, &width, NUMBER_INT|NUMBER_UNSIGNED))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003756 return -1;
3757
3758#ifdef FEAT_GUI
3759 need_mouse_correct = TRUE;
3760#endif
3761 savewin = curwin;
Bram Moolenaard6e39182013-05-21 18:30:34 +02003762 curwin = self->win;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003763
3764 VimTryStart();
Bram Moolenaar141be8a2013-06-23 14:16:57 +02003765 win_setwidth((int) width);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003766 curwin = savewin;
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02003767 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003768 return -1;
3769
3770 return 0;
3771 }
3772#endif
3773 else
3774 {
3775 PyErr_SetString(PyExc_AttributeError, name);
3776 return -1;
3777 }
3778}
3779
3780 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003781WindowRepr(WindowObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003782{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003783 if (self->win == INVALID_WINDOW_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003784 return PyString_FromFormat("<window object (deleted) at %p>", (self));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003785 else
3786 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02003787 int w = get_win_number(self->win, firstwin);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003788
Bram Moolenaar6d216452013-05-12 19:00:41 +02003789 if (w == 0)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003790 return PyString_FromFormat("<window object (unknown) at %p>",
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003791 (self));
3792 else
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02003793 return PyString_FromFormat("<window %d>", w - 1);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003794 }
3795}
3796
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003797static struct PyMethodDef WindowMethods[] = {
Bram Moolenaardd8aca62013-05-29 22:36:10 +02003798 /* name, function, calling, documentation */
3799 {"__dir__", (PyCFunction)WindowDir, METH_NOARGS, ""},
3800 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003801};
3802
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003803/*
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003804 * Window list object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003805 */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003806
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02003807static PyTypeObject WinListType;
3808static PySequenceMethods WinListAsSeq;
3809
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003810typedef struct
3811{
3812 PyObject_HEAD
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003813 TabPageObject *tabObject;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02003814} WinListObject;
3815
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003816 static PyObject *
3817WinListNew(TabPageObject *tabObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003818{
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003819 WinListObject *self;
3820
3821 self = PyObject_NEW(WinListObject, &WinListType);
3822 self->tabObject = tabObject;
3823 Py_INCREF(tabObject);
3824
3825 return (PyObject *)(self);
3826}
3827
3828 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02003829WinListDestructor(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003830{
Bram Moolenaard6e39182013-05-21 18:30:34 +02003831 TabPageObject *tabObject = self->tabObject;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003832
3833 if (tabObject)
Bram Moolenaar425154d2013-05-24 18:58:43 +02003834 {
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003835 Py_DECREF((PyObject *)(tabObject));
Bram Moolenaar425154d2013-05-24 18:58:43 +02003836 }
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003837
3838 DESTRUCTOR_FINISH(self);
3839}
3840
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003841 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02003842WinListLength(WinListObject *self)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003843{
3844 win_T *w;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003845 PyInt n = 0;
3846
Bram Moolenaard6e39182013-05-21 18:30:34 +02003847 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003848 return -1;
3849
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003850 while (w != NULL)
3851 {
3852 ++n;
3853 w = W_NEXT(w);
3854 }
3855
3856 return n;
3857}
3858
3859 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02003860WinListItem(WinListObject *self, PyInt n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003861{
3862 win_T *w;
3863
Bram Moolenaard6e39182013-05-21 18:30:34 +02003864 if (!(w = get_firstwin(self->tabObject)))
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003865 return NULL;
3866
3867 for (; w != NULL; w = W_NEXT(w), --n)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003868 if (n == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02003869 return WindowNew(w, self->tabObject? self->tabObject->tab: curtab);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003870
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003871 PyErr_SET_STRING(PyExc_IndexError, N_("no such window"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003872 return NULL;
3873}
3874
3875/* Convert a Python string into a Vim line.
3876 *
3877 * The result is in allocated memory. All internal nulls are replaced by
3878 * newline characters. It is an error for the string to contain newline
3879 * characters.
3880 *
3881 * On errors, the Python exception data is set, and NULL is returned.
3882 */
3883 static char *
3884StringToLine(PyObject *obj)
3885{
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003886 char *str;
3887 char *save;
3888 PyObject *bytes = NULL;
Bram Moolenaardee2e312013-06-23 16:35:47 +02003889 Py_ssize_t len = 0;
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003890 PyInt i;
3891 char *p;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003892
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003893 if (PyBytes_Check(obj))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003894 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003895 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3896 || str == NULL)
3897 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003898 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003899 else if (PyUnicode_Check(obj))
3900 {
3901 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3902 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003903
Bram Moolenaardaa27022013-06-24 22:33:30 +02003904 if (PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003905 || str == NULL)
3906 {
3907 Py_DECREF(bytes);
3908 return NULL;
3909 }
3910 }
Bram Moolenaardaa27022013-06-24 22:33:30 +02003911 else
3912 {
3913#if PY_MAJOR_VERSION < 3
3914 PyErr_FORMAT(PyExc_TypeError,
3915 N_("expected str() or unicode() instance, but got %s"),
3916 Py_TYPE_NAME(obj));
3917#else
3918 PyErr_FORMAT(PyExc_TypeError,
3919 N_("expected bytes() or str() instance, but got %s"),
3920 Py_TYPE_NAME(obj));
3921#endif
3922 return NULL;
3923 }
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003924
3925 /*
3926 * Error checking: String must not contain newlines, as we
3927 * are replacing a single line, and we must replace it with
3928 * a single line.
3929 * A trailing newline is removed, so that append(f.readlines()) works.
3930 */
3931 p = memchr(str, '\n', len);
3932 if (p != NULL)
3933 {
3934 if (p == str + len - 1)
3935 --len;
3936 else
3937 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02003938 PyErr_SET_VIM(N_("string cannot contain newlines"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003939 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003940 return NULL;
3941 }
3942 }
3943
3944 /* Create a copy of the string, with internal nulls replaced by
3945 * newline characters, as is the vim convention.
3946 */
3947 save = (char *)alloc((unsigned)(len+1));
3948 if (save == NULL)
3949 {
3950 PyErr_NoMemory();
Bram Moolenaar841fbd22013-06-23 14:37:07 +02003951 Py_XDECREF(bytes);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003952 return NULL;
3953 }
3954
3955 for (i = 0; i < len; ++i)
3956 {
3957 if (str[i] == '\0')
3958 save[i] = '\n';
3959 else
3960 save[i] = str[i];
3961 }
3962
3963 save[i] = '\0';
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02003964 Py_XDECREF(bytes); /* Python 2 does nothing here */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003965
3966 return save;
3967}
3968
3969/* Get a line from the specified buffer. The line number is
3970 * in Vim format (1-based). The line is returned as a Python
3971 * string object.
3972 */
3973 static PyObject *
3974GetBufferLine(buf_T *buf, PyInt n)
3975{
3976 return LineToString((char *)ml_get_buf(buf, (linenr_T)n, FALSE));
3977}
3978
3979
3980/* Get a list of lines from the specified buffer. The line numbers
3981 * are in Vim format (1-based). The range is from lo up to, but not
3982 * including, hi. The list is returned as a Python list of string objects.
3983 */
3984 static PyObject *
3985GetBufferLineList(buf_T *buf, PyInt lo, PyInt hi)
3986{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003987 PyInt i;
3988 PyInt n = hi - lo;
3989 PyObject *list = PyList_New(n);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003990
3991 if (list == NULL)
3992 return NULL;
3993
3994 for (i = 0; i < n; ++i)
3995 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003996 PyObject *string = LineToString(
3997 (char *)ml_get_buf(buf, (linenr_T)(lo+i), FALSE));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02003998
3999 /* Error check - was the Python string creation OK? */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004000 if (string == NULL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004001 {
4002 Py_DECREF(list);
4003 return NULL;
4004 }
4005
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004006 PyList_SET_ITEM(list, i, string);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004007 }
4008
4009 /* The ownership of the Python list is passed to the caller (ie,
4010 * the caller should Py_DECREF() the object when it is finished
4011 * with it).
4012 */
4013
4014 return list;
4015}
4016
4017/*
4018 * Check if deleting lines made the cursor position invalid.
4019 * Changed the lines from "lo" to "hi" and added "extra" lines (negative if
4020 * deleted).
4021 */
4022 static void
4023py_fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
4024{
4025 if (curwin->w_cursor.lnum >= lo)
4026 {
4027 /* Adjust the cursor position if it's in/after the changed
4028 * lines. */
4029 if (curwin->w_cursor.lnum >= hi)
4030 {
4031 curwin->w_cursor.lnum += extra;
4032 check_cursor_col();
4033 }
4034 else if (extra < 0)
4035 {
4036 curwin->w_cursor.lnum = lo;
4037 check_cursor();
4038 }
4039 else
4040 check_cursor_col();
4041 changed_cline_bef_curs();
4042 }
4043 invalidate_botline();
4044}
4045
Bram Moolenaar19e60942011-06-19 00:27:51 +02004046/*
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004047 * Find a window that contains "buf" and switch to it.
4048 * If there is no such window, use the current window and change "curbuf".
4049 * Caller must initialize save_curbuf to NULL.
4050 * restore_win_for_buf() MUST be called later!
4051 */
4052 static void
4053switch_to_win_for_buf(
4054 buf_T *buf,
4055 win_T **save_curwinp,
4056 tabpage_T **save_curtabp,
4057 buf_T **save_curbufp)
4058{
4059 win_T *wp;
4060 tabpage_T *tp;
4061
Bram Moolenaarae38d052014-12-17 14:46:09 +01004062 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004063 switch_buffer(save_curbufp, buf);
Bram Moolenaarae38d052014-12-17 14:46:09 +01004064 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
4065 {
4066 restore_win(*save_curwinp, *save_curtabp, TRUE);
4067 switch_buffer(save_curbufp, buf);
4068 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004069}
4070
4071 static void
4072restore_win_for_buf(
4073 win_T *save_curwin,
4074 tabpage_T *save_curtab,
4075 buf_T *save_curbuf)
4076{
4077 if (save_curbuf == NULL)
4078 restore_win(save_curwin, save_curtab, TRUE);
4079 else
4080 restore_buffer(save_curbuf);
4081}
4082
4083/*
Bram Moolenaar19e60942011-06-19 00:27:51 +02004084 * Replace a line in the specified buffer. The line number is
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004085 * in Vim format (1-based). The replacement line is given as
4086 * a Python string object. The object is checked for validity
4087 * and correct format. Errors are returned as a value of FAIL.
4088 * The return value is OK on success.
4089 * If OK is returned and len_change is not NULL, *len_change
4090 * is set to the change in the buffer length.
4091 */
4092 static int
4093SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
4094{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004095 buf_T *save_curbuf = NULL;
4096 win_T *save_curwin = NULL;
4097 tabpage_T *save_curtab = NULL;
4098
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004099 /* First of all, we check the type of the supplied Python object.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004100 * There are three cases:
4101 * 1. NULL, or None - this is a deletion.
4102 * 2. A string - this is a replacement.
4103 * 3. Anything else - this is an error.
4104 */
4105 if (line == Py_None || line == NULL)
4106 {
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004107 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004108 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004109
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004110 VimTryStart();
4111
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004112 if (u_savedel((linenr_T)n, 1L) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004113 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004114 else if (ml_delete((linenr_T)n, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004115 RAISE_DELETE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004116 else
4117 {
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004118 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004119 py_fix_cursor((linenr_T)n, (linenr_T)n + 1, (linenr_T)-1);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004120 if (save_curbuf == NULL)
4121 /* Only adjust marks if we managed to switch to a window that
4122 * holds the buffer, otherwise line numbers will be invalid. */
4123 deleted_lines_mark((linenr_T)n, 1L);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004124 }
4125
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004126 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004127
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004128 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004129 return FAIL;
4130
4131 if (len_change)
4132 *len_change = -1;
4133
4134 return OK;
4135 }
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004136 else if (PyBytes_Check(line) || PyUnicode_Check(line))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004137 {
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004138 char *save = StringToLine(line);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004139
4140 if (save == NULL)
4141 return FAIL;
4142
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004143 VimTryStart();
4144
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004145 /* We do not need to free "save" if ml_replace() consumes it. */
4146 PyErr_Clear();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004147 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004148
4149 if (u_savesub((linenr_T)n) == FAIL)
4150 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02004151 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004152 vim_free(save);
4153 }
4154 else if (ml_replace((linenr_T)n, (char_u *)save, FALSE) == FAIL)
4155 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004156 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004157 vim_free(save);
4158 }
4159 else
4160 changed_bytes((linenr_T)n, 0);
4161
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004162 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004163
4164 /* Check that the cursor is not beyond the end of the line now. */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004165 if (buf == curbuf)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004166 check_cursor_col();
4167
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004168 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004169 return FAIL;
4170
4171 if (len_change)
4172 *len_change = 0;
4173
4174 return OK;
4175 }
4176 else
4177 {
4178 PyErr_BadArgument();
4179 return FAIL;
4180 }
4181}
4182
Bram Moolenaar19e60942011-06-19 00:27:51 +02004183/* Replace a range of lines in the specified buffer. The line numbers are in
4184 * Vim format (1-based). The range is from lo up to, but not including, hi.
4185 * The replacement lines are given as a Python list of string objects. The
4186 * list is checked for validity and correct format. Errors are returned as a
4187 * value of FAIL. The return value is OK on success.
4188 * If OK is returned and len_change is not NULL, *len_change
4189 * is set to the change in the buffer length.
4190 */
4191 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004192SetBufferLineList(
4193 buf_T *buf,
4194 PyInt lo,
4195 PyInt hi,
4196 PyObject *list,
4197 PyInt *len_change)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004198{
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004199 buf_T *save_curbuf = NULL;
4200 win_T *save_curwin = NULL;
4201 tabpage_T *save_curtab = NULL;
4202
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004203 /* First of all, we check the type of the supplied Python object.
Bram Moolenaar19e60942011-06-19 00:27:51 +02004204 * There are three cases:
4205 * 1. NULL, or None - this is a deletion.
4206 * 2. A list - this is a replacement.
4207 * 3. Anything else - this is an error.
4208 */
4209 if (list == Py_None || list == NULL)
4210 {
4211 PyInt i;
4212 PyInt n = (int)(hi - lo);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004213
4214 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004215 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004216 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004217
4218 if (u_savedel((linenr_T)lo, (long)n) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004219 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004220 else
4221 {
4222 for (i = 0; i < n; ++i)
4223 {
4224 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4225 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004226 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004227 break;
4228 }
4229 }
Bram Moolenaard7408fa2014-08-29 13:49:52 +02004230 if (buf == curbuf && (save_curwin != NULL || save_curbuf == NULL))
4231 /* Using an existing window for the buffer, adjust the cursor
4232 * position. */
Bram Moolenaar19e60942011-06-19 00:27:51 +02004233 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004234 if (save_curbuf == NULL)
4235 /* Only adjust marks if we managed to switch to a window that
4236 * holds the buffer, otherwise line numbers will be invalid. */
4237 deleted_lines_mark((linenr_T)lo, (long)i);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004238 }
4239
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004240 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004241
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004242 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004243 return FAIL;
4244
4245 if (len_change)
4246 *len_change = -n;
4247
4248 return OK;
4249 }
4250 else if (PyList_Check(list))
4251 {
4252 PyInt i;
4253 PyInt new_len = PyList_Size(list);
4254 PyInt old_len = hi - lo;
4255 PyInt extra = 0; /* lines added to text, can be negative */
4256 char **array;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004257
4258 if (new_len == 0) /* avoid allocating zero bytes */
4259 array = NULL;
4260 else
4261 {
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004262 array = PyMem_New(char *, new_len);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004263 if (array == NULL)
4264 {
4265 PyErr_NoMemory();
4266 return FAIL;
4267 }
4268 }
4269
4270 for (i = 0; i < new_len; ++i)
4271 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004272 PyObject *line;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004273
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004274 if (!(line = PyList_GetItem(list, i)) ||
4275 !(array[i] = StringToLine(line)))
Bram Moolenaar19e60942011-06-19 00:27:51 +02004276 {
4277 while (i)
4278 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004279 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004280 return FAIL;
4281 }
4282 }
4283
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004284 VimTryStart();
Bram Moolenaar19e60942011-06-19 00:27:51 +02004285 PyErr_Clear();
Bram Moolenaar105bc352013-05-17 16:03:57 +02004286
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004287 /* START of region without "return". Must call restore_buffer()! */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004288 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004289
4290 if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004291 RAISE_UNDO_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004292
4293 /* If the size of the range is reducing (ie, new_len < old_len) we
4294 * need to delete some old_len. We do this at the start, by
4295 * repeatedly deleting line "lo".
4296 */
4297 if (!PyErr_Occurred())
4298 {
4299 for (i = 0; i < old_len - new_len; ++i)
4300 if (ml_delete((linenr_T)lo, FALSE) == FAIL)
4301 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004302 RAISE_DELETE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004303 break;
4304 }
4305 extra -= i;
4306 }
4307
4308 /* For as long as possible, replace the existing old_len with the
4309 * new old_len. This is a more efficient operation, as it requires
4310 * less memory allocation and freeing.
4311 */
4312 if (!PyErr_Occurred())
4313 {
4314 for (i = 0; i < old_len && i < new_len; ++i)
4315 if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
4316 == FAIL)
4317 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004318 RAISE_REPLACE_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004319 break;
4320 }
4321 }
4322 else
4323 i = 0;
4324
4325 /* Now we may need to insert the remaining new old_len. If we do, we
4326 * must free the strings as we finish with them (we can't pass the
4327 * responsibility to vim in this case).
4328 */
4329 if (!PyErr_Occurred())
4330 {
4331 while (i < new_len)
4332 {
4333 if (ml_append((linenr_T)(lo + i - 1),
4334 (char_u *)array[i], 0, FALSE) == FAIL)
4335 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004336 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02004337 break;
4338 }
4339 vim_free(array[i]);
4340 ++i;
4341 ++extra;
4342 }
4343 }
4344
4345 /* Free any left-over old_len, as a result of an error */
4346 while (i < new_len)
4347 {
4348 vim_free(array[i]);
4349 ++i;
4350 }
4351
4352 /* Free the array of old_len. All of its contents have now
4353 * been dealt with (either freed, or the responsibility passed
4354 * to vim.
4355 */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004356 PyMem_Free(array);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004357
4358 /* Adjust marks. Invalidate any which lie in the
4359 * changed range, and move any in the remainder of the buffer.
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004360 * Only adjust marks if we managed to switch to a window that holds
4361 * the buffer, otherwise line numbers will be invalid. */
4362 if (save_curbuf == NULL)
4363 mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
Bram Moolenaar19e60942011-06-19 00:27:51 +02004364 (long)MAXLNUM, (long)extra);
4365 changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
4366
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004367 if (buf == curbuf)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004368 py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
4369
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004370 /* END of region without "return". */
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004371 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02004372
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004373 if (VimTryEnd())
Bram Moolenaar19e60942011-06-19 00:27:51 +02004374 return FAIL;
4375
4376 if (len_change)
4377 *len_change = new_len - old_len;
4378
4379 return OK;
4380 }
4381 else
4382 {
4383 PyErr_BadArgument();
4384 return FAIL;
4385 }
4386}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004387
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004388/* Insert a number of lines into the specified buffer after the specified line.
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004389 * The line number is in Vim format (1-based). The lines to be inserted are
4390 * given as a Python list of string objects or as a single string. The lines
4391 * to be added are checked for validity and correct format. Errors are
4392 * returned as a value of FAIL. The return value is OK on success.
4393 * If OK is returned and len_change is not NULL, *len_change
4394 * is set to the change in the buffer length.
4395 */
4396 static int
4397InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
4398{
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004399 buf_T *save_curbuf = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004400 win_T *save_curwin = NULL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004401 tabpage_T *save_curtab = NULL;
4402
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004403 /* First of all, we check the type of the supplied Python object.
4404 * It must be a string or a list, or the call is in error.
4405 */
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02004406 if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004407 {
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004408 char *str = StringToLine(lines);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004409
4410 if (str == NULL)
4411 return FAIL;
4412
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004413 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004414 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004415 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004416
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004417 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004418 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004419 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL)
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004420 RAISE_INSERT_LINE_FAIL;
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004421 else if (save_curbuf == NULL)
4422 /* Only adjust marks if we managed to switch to a window that
4423 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004424 appended_lines_mark((linenr_T)n, 1L);
4425
4426 vim_free(str);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004427 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004428 update_screen(VALID);
4429
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004430 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004431 return FAIL;
4432
4433 if (len_change)
4434 *len_change = 1;
4435
4436 return OK;
4437 }
4438 else if (PyList_Check(lines))
4439 {
4440 PyInt i;
4441 PyInt size = PyList_Size(lines);
4442 char **array;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004443
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004444 array = PyMem_New(char *, size);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004445 if (array == NULL)
4446 {
4447 PyErr_NoMemory();
4448 return FAIL;
4449 }
4450
4451 for (i = 0; i < size; ++i)
4452 {
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004453 PyObject *line;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004454
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02004455 if (!(line = PyList_GetItem(lines, i)) ||
4456 !(array[i] = StringToLine(line)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004457 {
4458 while (i)
4459 vim_free(array[--i]);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004460 PyMem_Free(array);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004461 return FAIL;
4462 }
4463 }
4464
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004465 PyErr_Clear();
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004466 VimTryStart();
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004467 switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004468
4469 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
Bram Moolenaarc476e522013-06-23 13:46:40 +02004470 RAISE_UNDO_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004471 else
4472 {
4473 for (i = 0; i < size; ++i)
4474 {
4475 if (ml_append((linenr_T)(n + i),
4476 (char_u *)array[i], 0, FALSE) == FAIL)
4477 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004478 RAISE_INSERT_LINE_FAIL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004479
4480 /* Free the rest of the lines */
4481 while (i < size)
4482 vim_free(array[i++]);
4483
4484 break;
4485 }
4486 vim_free(array[i]);
4487 }
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004488 if (i > 0 && save_curbuf == NULL)
4489 /* Only adjust marks if we managed to switch to a window that
4490 * holds the buffer, otherwise line numbers will be invalid. */
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004491 appended_lines_mark((linenr_T)n, (long)i);
4492 }
4493
4494 /* Free the array of lines. All of its contents have now
Bram Moolenaar95064ec2013-07-17 17:15:25 +02004495 * been freed. */
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004496 PyMem_Free(array);
Bram Moolenaaraf003f62013-07-24 17:11:46 +02004497 restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004498
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004499 update_screen(VALID);
4500
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004501 if (VimTryEnd())
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004502 return FAIL;
4503
4504 if (len_change)
4505 *len_change = size;
4506
4507 return OK;
4508 }
4509 else
4510 {
4511 PyErr_BadArgument();
4512 return FAIL;
4513 }
4514}
4515
4516/*
4517 * Common routines for buffers and line ranges
4518 * -------------------------------------------
4519 */
4520
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004521typedef struct
4522{
4523 PyObject_HEAD
4524 buf_T *buf;
4525} BufferObject;
4526
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004527 static int
Bram Moolenaard6e39182013-05-21 18:30:34 +02004528CheckBuffer(BufferObject *self)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004529{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004530 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004531 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004532 PyErr_SET_VIM(N_("attempt to refer to deleted buffer"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004533 return -1;
4534 }
4535
4536 return 0;
4537}
4538
4539 static PyObject *
4540RBItem(BufferObject *self, PyInt n, PyInt start, PyInt end)
4541{
4542 if (CheckBuffer(self))
4543 return NULL;
4544
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004545 if (end == -1)
4546 end = self->buf->b_ml.ml_line_count;
4547
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004548 if (n < 0)
4549 n += end - start + 1;
4550
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004551 if (n < 0 || n > end - start)
4552 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004553 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004554 return NULL;
4555 }
4556
4557 return GetBufferLine(self->buf, n+start);
4558}
4559
4560 static PyObject *
4561RBSlice(BufferObject *self, PyInt lo, PyInt hi, PyInt start, PyInt end)
4562{
4563 PyInt size;
4564
4565 if (CheckBuffer(self))
4566 return NULL;
4567
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004568 if (end == -1)
4569 end = self->buf->b_ml.ml_line_count;
4570
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004571 size = end - start + 1;
4572
4573 if (lo < 0)
4574 lo = 0;
4575 else if (lo > size)
4576 lo = size;
4577 if (hi < 0)
4578 hi = 0;
4579 if (hi < lo)
4580 hi = lo;
4581 else if (hi > size)
4582 hi = size;
4583
4584 return GetBufferLineList(self->buf, lo+start, hi+start);
4585}
4586
4587 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004588RBAsItem(
4589 BufferObject *self,
4590 PyInt n,
4591 PyObject *valObject,
4592 PyInt start,
4593 PyInt end,
4594 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004595{
4596 PyInt len_change;
4597
4598 if (CheckBuffer(self))
4599 return -1;
4600
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004601 if (end == -1)
4602 end = self->buf->b_ml.ml_line_count;
4603
Bram Moolenaarbd80f352013-05-12 21:16:23 +02004604 if (n < 0)
4605 n += end - start + 1;
4606
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004607 if (n < 0 || n > end - start)
4608 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004609 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004610 return -1;
4611 }
4612
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004613 if (SetBufferLine(self->buf, n+start, valObject, &len_change) == FAIL)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004614 return -1;
4615
4616 if (new_end)
4617 *new_end = end + len_change;
4618
4619 return 0;
4620}
4621
Bram Moolenaar19e60942011-06-19 00:27:51 +02004622 static PyInt
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004623RBAsSlice(
4624 BufferObject *self,
4625 PyInt lo,
4626 PyInt hi,
4627 PyObject *valObject,
4628 PyInt start,
4629 PyInt end,
4630 PyInt *new_end)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004631{
4632 PyInt size;
4633 PyInt len_change;
4634
4635 /* Self must be a valid buffer */
4636 if (CheckBuffer(self))
4637 return -1;
4638
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004639 if (end == -1)
4640 end = self->buf->b_ml.ml_line_count;
4641
Bram Moolenaar19e60942011-06-19 00:27:51 +02004642 /* Sort out the slice range */
4643 size = end - start + 1;
4644
4645 if (lo < 0)
4646 lo = 0;
4647 else if (lo > size)
4648 lo = size;
4649 if (hi < 0)
4650 hi = 0;
4651 if (hi < lo)
4652 hi = lo;
4653 else if (hi > size)
4654 hi = size;
4655
4656 if (SetBufferLineList(self->buf, lo + start, hi + start,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004657 valObject, &len_change) == FAIL)
Bram Moolenaar19e60942011-06-19 00:27:51 +02004658 return -1;
4659
4660 if (new_end)
4661 *new_end = end + len_change;
4662
4663 return 0;
4664}
4665
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004666
4667 static PyObject *
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004668RBAppend(
4669 BufferObject *self,
4670 PyObject *args,
4671 PyInt start,
4672 PyInt end,
4673 PyInt *new_end)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004674{
4675 PyObject *lines;
4676 PyInt len_change;
4677 PyInt max;
4678 PyInt n;
4679
4680 if (CheckBuffer(self))
4681 return NULL;
4682
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02004683 if (end == -1)
4684 end = self->buf->b_ml.ml_line_count;
4685
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004686 max = n = end - start + 1;
4687
4688 if (!PyArg_ParseTuple(args, "O|n", &lines, &n))
4689 return NULL;
4690
4691 if (n < 0 || n > max)
4692 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004693 PyErr_SET_STRING(PyExc_IndexError, N_("line number out of range"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004694 return NULL;
4695 }
4696
4697 if (InsertBufferLines(self->buf, n + start - 1, lines, &len_change) == FAIL)
4698 return NULL;
4699
4700 if (new_end)
4701 *new_end = end + len_change;
4702
4703 Py_INCREF(Py_None);
4704 return Py_None;
4705}
4706
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004707/* Range object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004708 */
4709
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004710static PyTypeObject RangeType;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004711static PySequenceMethods RangeAsSeq;
4712static PyMappingMethods RangeAsMapping;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004713
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004714typedef struct
4715{
4716 PyObject_HEAD
4717 BufferObject *buf;
4718 PyInt start;
4719 PyInt end;
4720} RangeObject;
4721
4722 static PyObject *
4723RangeNew(buf_T *buf, PyInt start, PyInt end)
4724{
4725 BufferObject *bufr;
4726 RangeObject *self;
Bram Moolenaar774267b2013-05-21 20:51:59 +02004727 self = PyObject_GC_New(RangeObject, &RangeType);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004728 if (self == NULL)
4729 return NULL;
4730
4731 bufr = (BufferObject *)BufferNew(buf);
4732 if (bufr == NULL)
4733 {
4734 Py_DECREF(self);
4735 return NULL;
4736 }
4737 Py_INCREF(bufr);
4738
4739 self->buf = bufr;
4740 self->start = start;
4741 self->end = end;
4742
4743 return (PyObject *)(self);
4744}
4745
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004746 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004747RangeDestructor(RangeObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004748{
Bram Moolenaar774267b2013-05-21 20:51:59 +02004749 PyObject_GC_UnTrack((void *)(self));
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02004750 Py_XDECREF(self->buf);
Bram Moolenaar774267b2013-05-21 20:51:59 +02004751 PyObject_GC_Del((void *)(self));
4752}
4753
4754 static int
4755RangeTraverse(RangeObject *self, visitproc visit, void *arg)
4756{
4757 Py_VISIT(((PyObject *)(self->buf)));
4758 return 0;
4759}
4760
4761 static int
4762RangeClear(RangeObject *self)
4763{
4764 Py_CLEAR(self->buf);
4765 return 0;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004766}
4767
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004768 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004769RangeLength(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004770{
4771 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004772 if (CheckBuffer(self->buf))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004773 return -1; /* ??? */
4774
Bram Moolenaard6e39182013-05-21 18:30:34 +02004775 return (self->end - self->start + 1);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004776}
4777
4778 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004779RangeItem(RangeObject *self, PyInt n)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004780{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004781 return RBItem(self->buf, n, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004782}
4783
4784 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004785RangeSlice(RangeObject *self, PyInt lo, PyInt hi)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004786{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004787 return RBSlice(self->buf, lo, hi, self->start, self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004788}
4789
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004790static char *RangeAttrs[] = {
4791 "start", "end",
4792 NULL
4793};
4794
4795 static PyObject *
4796RangeDir(PyObject *self)
4797{
4798 return ObjectDir(self, RangeAttrs);
4799}
4800
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004801 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004802RangeAppend(RangeObject *self, PyObject *args)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004803{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004804 return RBAppend(self->buf, args, self->start, self->end, &self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004805}
4806
4807 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004808RangeRepr(RangeObject *self)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004809{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004810 if (self->buf->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004811 return PyString_FromFormat("<range object (for deleted buffer) at %p>",
4812 (self));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004813 else
4814 {
Bram Moolenaard6e39182013-05-21 18:30:34 +02004815 char *name = (char *)self->buf->buf->b_fname;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004816
4817 if (name == NULL)
4818 name = "";
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004819
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02004820 return PyString_FromFormat("<range %s (%d:%d)>",
Bram Moolenaarf62d9422013-05-30 19:01:24 +02004821 name, (int)self->start, (int)self->end);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004822 }
4823}
4824
4825static struct PyMethodDef RangeMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02004826 /* name, function, calling, documentation */
4827 {"append", (PyCFunction)RangeAppend, METH_VARARGS, "Append data to the Vim range" },
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004828 {"__dir__", (PyCFunction)RangeDir, METH_NOARGS, ""},
4829 { NULL, NULL, 0, NULL}
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004830};
4831
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004832static PyTypeObject BufferType;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004833static PySequenceMethods BufferAsSeq;
4834static PyMappingMethods BufferAsMapping;
4835
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004836 static PyObject *
Bram Moolenaar971db462013-05-12 18:44:48 +02004837BufferNew(buf_T *buf)
4838{
4839 /* We need to handle deletion of buffers underneath us.
4840 * If we add a "b_python*_ref" field to the buf_T structure,
4841 * then we can get at it in buf_freeall() in vim. We then
4842 * need to create only ONE Python object per buffer - if
4843 * we try to create a second, just INCREF the existing one
4844 * and return it. The (single) Python object referring to
4845 * the buffer is stored in "b_python*_ref".
4846 * Question: what to do on a buf_freeall(). We'll probably
4847 * have to either delete the Python object (DECREF it to
4848 * zero - a bad idea, as it leaves dangling refs!) or
4849 * set the buf_T * value to an invalid value (-1?), which
4850 * means we need checks in all access functions... Bah.
4851 *
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004852 * Python2 and Python3 get different fields and different objects:
Bram Moolenaar971db462013-05-12 18:44:48 +02004853 * b_python_ref and b_python3_ref fields respectively.
4854 */
4855
4856 BufferObject *self;
4857
4858 if (BUF_PYTHON_REF(buf) != NULL)
4859 {
4860 self = BUF_PYTHON_REF(buf);
4861 Py_INCREF(self);
4862 }
4863 else
4864 {
4865 self = PyObject_NEW(BufferObject, &BufferType);
4866 if (self == NULL)
4867 return NULL;
4868 self->buf = buf;
4869 BUF_PYTHON_REF(buf) = self;
4870 }
4871
4872 return (PyObject *)(self);
4873}
4874
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004875 static void
Bram Moolenaard6e39182013-05-21 18:30:34 +02004876BufferDestructor(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004877{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004878 if (self->buf && self->buf != INVALID_BUFFER_VALUE)
4879 BUF_PYTHON_REF(self->buf) = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004880
4881 DESTRUCTOR_FINISH(self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02004882}
4883
Bram Moolenaar971db462013-05-12 18:44:48 +02004884 static PyInt
Bram Moolenaard6e39182013-05-21 18:30:34 +02004885BufferLength(BufferObject *self)
Bram Moolenaar971db462013-05-12 18:44:48 +02004886{
4887 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Bram Moolenaard6e39182013-05-21 18:30:34 +02004888 if (CheckBuffer(self))
Bram Moolenaar971db462013-05-12 18:44:48 +02004889 return -1; /* ??? */
4890
Bram Moolenaard6e39182013-05-21 18:30:34 +02004891 return (PyInt)(self->buf->b_ml.ml_line_count);
Bram Moolenaar971db462013-05-12 18:44:48 +02004892}
4893
4894 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004895BufferItem(BufferObject *self, PyInt n)
Bram Moolenaar971db462013-05-12 18:44:48 +02004896{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004897 return RBItem(self, n, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004898}
4899
4900 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004901BufferSlice(BufferObject *self, PyInt lo, PyInt hi)
Bram Moolenaar971db462013-05-12 18:44:48 +02004902{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004903 return RBSlice(self, lo, hi, 1, -1);
Bram Moolenaar971db462013-05-12 18:44:48 +02004904}
4905
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004906static char *BufferAttrs[] = {
4907 "name", "number", "vars", "options", "valid",
4908 NULL
4909};
4910
4911 static PyObject *
4912BufferDir(PyObject *self)
4913{
4914 return ObjectDir(self, BufferAttrs);
4915}
4916
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004917 static PyObject *
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004918BufferAttrValid(BufferObject *self, char *name)
4919{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004920 PyObject *ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004921
4922 if (strcmp(name, "valid") != 0)
4923 return NULL;
4924
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004925 ret = ((self->buf == INVALID_BUFFER_VALUE) ? Py_False : Py_True);
4926 Py_INCREF(ret);
4927 return ret;
Bram Moolenaar9e822c02013-05-29 22:15:30 +02004928}
4929
4930 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004931BufferAttr(BufferObject *self, char *name)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004932{
4933 if (strcmp(name, "name") == 0)
Bram Moolenaar432b09c2013-05-29 22:26:18 +02004934 return PyString_FromString((self->buf->b_ffname == NULL
Bram Moolenaar41009372013-07-01 22:03:04 +02004935 ? "" : (char *)self->buf->b_ffname));
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004936 else if (strcmp(name, "number") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004937 return Py_BuildValue(Py_ssize_t_fmt, self->buf->b_fnum);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004938 else if (strcmp(name, "vars") == 0)
Bram Moolenaara9922d62013-05-30 13:01:18 +02004939 return NEW_DICTIONARY(self->buf->b_vars);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004940 else if (strcmp(name, "options") == 0)
Bram Moolenaard6e39182013-05-21 18:30:34 +02004941 return OptionsNew(SREQ_BUF, self->buf, (checkfun) CheckBuffer,
4942 (PyObject *) self);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02004943 else if (strcmp(name, "__members__") == 0)
4944 return ObjectDir(NULL, BufferAttrs);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004945 else
4946 return NULL;
4947}
4948
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004949 static int
4950BufferSetattr(BufferObject *self, char *name, PyObject *valObject)
4951{
4952 if (CheckBuffer(self))
4953 return -1;
4954
4955 if (strcmp(name, "name") == 0)
4956 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004957 char_u *val;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004958 aco_save_T aco;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004959 int ren_ret;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004960 PyObject *todecref;
4961
4962 if (!(val = StringToChars(valObject, &todecref)))
4963 return -1;
4964
4965 VimTryStart();
4966 /* Using aucmd_*: autocommands will be executed by rename_buffer */
4967 aucmd_prepbuf(&aco, self->buf);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004968 ren_ret = rename_buffer(val);
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004969 aucmd_restbuf(&aco);
4970 Py_XDECREF(todecref);
4971 if (VimTryEnd())
4972 return -1;
4973
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02004974 if (ren_ret == FAIL)
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004975 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02004976 PyErr_SET_VIM(N_("failed to rename buffer"));
Bram Moolenaare9ba5162013-05-29 22:02:22 +02004977 return -1;
4978 }
4979 return 0;
4980 }
4981 else
4982 {
4983 PyErr_SetString(PyExc_AttributeError, name);
4984 return -1;
4985 }
4986}
4987
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02004988 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02004989BufferAppend(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004990{
Bram Moolenaard6e39182013-05-21 18:30:34 +02004991 return RBAppend(self, args, 1, -1, NULL);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004992}
4993
4994 static PyObject *
Bram Moolenaar389a1792013-06-23 13:00:44 +02004995BufferMark(BufferObject *self, PyObject *pmarkObject)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02004996{
4997 pos_T *posp;
Bram Moolenaar389a1792013-06-23 13:00:44 +02004998 char_u *pmark;
4999 char_u mark;
Bram Moolenaar105bc352013-05-17 16:03:57 +02005000 buf_T *savebuf;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005001 PyObject *todecref;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005002
Bram Moolenaard6e39182013-05-21 18:30:34 +02005003 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005004 return NULL;
5005
Bram Moolenaar389a1792013-06-23 13:00:44 +02005006 if (!(pmark = StringToChars(pmarkObject, &todecref)))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005007 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005008
Bram Moolenaar389a1792013-06-23 13:00:44 +02005009 if (pmark[0] == '\0' || pmark[1] != '\0')
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005010 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005011 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005012 N_("mark name must be a single character"));
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005013 Py_XDECREF(todecref);
Bram Moolenaar494ff7e2013-05-30 13:17:17 +02005014 return NULL;
5015 }
5016
5017 mark = *pmark;
Bram Moolenaar389a1792013-06-23 13:00:44 +02005018
5019 Py_XDECREF(todecref);
5020
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005021 VimTryStart();
Bram Moolenaard6e39182013-05-21 18:30:34 +02005022 switch_buffer(&savebuf, self->buf);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005023 posp = getmark(mark, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02005024 restore_buffer(savebuf);
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005025 if (VimTryEnd())
5026 return NULL;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005027
5028 if (posp == NULL)
5029 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005030 PyErr_SET_VIM(N_("invalid mark name"));
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005031 return NULL;
5032 }
5033
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005034 if (posp->lnum <= 0)
5035 {
5036 /* Or raise an error? */
5037 Py_INCREF(Py_None);
5038 return Py_None;
5039 }
5040
5041 return Py_BuildValue("(ll)", (long)(posp->lnum), (long)(posp->col));
5042}
5043
5044 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005045BufferRange(BufferObject *self, PyObject *args)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005046{
5047 PyInt start;
5048 PyInt end;
5049
Bram Moolenaard6e39182013-05-21 18:30:34 +02005050 if (CheckBuffer(self))
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005051 return NULL;
5052
5053 if (!PyArg_ParseTuple(args, "nn", &start, &end))
5054 return NULL;
5055
Bram Moolenaard6e39182013-05-21 18:30:34 +02005056 return RangeNew(self->buf, start, end);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005057}
5058
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005059 static PyObject *
Bram Moolenaard6e39182013-05-21 18:30:34 +02005060BufferRepr(BufferObject *self)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005061{
Bram Moolenaard6e39182013-05-21 18:30:34 +02005062 if (self->buf == INVALID_BUFFER_VALUE)
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005063 return PyString_FromFormat("<buffer object (deleted) at %p>", self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005064 else
5065 {
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005066 char *name = (char *)self->buf->b_fname;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005067
5068 if (name == NULL)
5069 name = "";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005070
Bram Moolenaar1a3b5692013-05-30 12:40:39 +02005071 return PyString_FromFormat("<buffer %s>", name);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005072 }
5073}
5074
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005075static struct PyMethodDef BufferMethods[] = {
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005076 /* name, function, calling, documentation */
5077 {"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
Bram Moolenaar389a1792013-06-23 13:00:44 +02005078 {"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
Bram Moolenaar182dc4f2013-05-21 19:01:55 +02005079 {"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 +02005080 {"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
5081 { NULL, NULL, 0, NULL}
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005082};
5083
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005084/*
5085 * Buffer list object - Implementation
5086 */
5087
5088static PyTypeObject BufMapType;
5089
5090typedef struct
5091{
5092 PyObject_HEAD
5093} BufMapObject;
5094
5095 static PyInt
5096BufMapLength(PyObject *self UNUSED)
5097{
5098 buf_T *b = firstbuf;
5099 PyInt n = 0;
5100
5101 while (b)
5102 {
5103 ++n;
5104 b = b->b_next;
5105 }
5106
5107 return n;
5108}
5109
5110 static PyObject *
5111BufMapItem(PyObject *self UNUSED, PyObject *keyObject)
5112{
5113 buf_T *b;
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005114 long bnr;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005115
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005116 if (NumberToLong(keyObject, &bnr, NUMBER_INT|NUMBER_NATURAL))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005117 return NULL;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005118
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005119 b = buflist_findnr((int) bnr);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005120
5121 if (b)
5122 return BufferNew(b);
5123 else
5124 {
Bram Moolenaar4d188da2013-05-15 15:35:09 +02005125 PyErr_SetObject(PyExc_KeyError, keyObject);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005126 return NULL;
5127 }
5128}
5129
5130 static void
5131BufMapIterDestruct(PyObject *buffer)
5132{
5133 /* Iteration was stopped before all buffers were processed */
5134 if (buffer)
5135 {
5136 Py_DECREF(buffer);
5137 }
5138}
5139
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005140 static int
5141BufMapIterTraverse(PyObject *buffer, visitproc visit, void *arg)
5142{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005143 if (buffer)
5144 Py_VISIT(buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005145 return 0;
5146}
5147
5148 static int
5149BufMapIterClear(PyObject **buffer)
5150{
Bram Moolenaar774267b2013-05-21 20:51:59 +02005151 if (*buffer)
5152 Py_CLEAR(*buffer);
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005153 return 0;
5154}
5155
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005156 static PyObject *
5157BufMapIterNext(PyObject **buffer)
5158{
5159 PyObject *next;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005160 PyObject *ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005161
5162 if (!*buffer)
5163 return NULL;
5164
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005165 ret = *buffer;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005166
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005167 if (CheckBuffer((BufferObject *)(ret)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005168 {
5169 *buffer = NULL;
5170 return NULL;
5171 }
5172
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005173 if (!((BufferObject *)(ret))->buf->b_next)
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005174 next = NULL;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005175 else if (!(next = BufferNew(((BufferObject *)(ret))->buf->b_next)))
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005176 return NULL;
5177 *buffer = next;
Bram Moolenaar9e74e302013-05-17 21:20:17 +02005178 /* Do not increment reference: we no longer hold it (decref), but whoever
5179 * on other side will hold (incref). Decref+incref = nothing. */
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005180 return ret;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005181}
5182
5183 static PyObject *
5184BufMapIter(PyObject *self UNUSED)
5185{
5186 PyObject *buffer;
5187
5188 buffer = BufferNew(firstbuf);
5189 return IterNew(buffer,
Bram Moolenaarcfef5ff2013-05-17 16:24:32 +02005190 (destructorfun) BufMapIterDestruct, (nextfun) BufMapIterNext,
5191 (traversefun) BufMapIterTraverse, (clearfun) BufMapIterClear);
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02005192}
5193
5194static PyMappingMethods BufMapAsMapping = {
5195 (lenfunc) BufMapLength,
5196 (binaryfunc) BufMapItem,
5197 (objobjargproc) 0,
5198};
5199
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005200/* Current items object
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02005201 */
5202
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005203static char *CurrentAttrs[] = {
5204 "buffer", "window", "line", "range", "tabpage",
5205 NULL
5206};
5207
5208 static PyObject *
5209CurrentDir(PyObject *self)
5210{
5211 return ObjectDir(self, CurrentAttrs);
5212}
5213
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005214 static PyObject *
5215CurrentGetattr(PyObject *self UNUSED, char *name)
5216{
5217 if (strcmp(name, "buffer") == 0)
5218 return (PyObject *)BufferNew(curbuf);
5219 else if (strcmp(name, "window") == 0)
Bram Moolenaarcabf80f2013-05-17 16:18:33 +02005220 return (PyObject *)WindowNew(curwin, curtab);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02005221 else if (strcmp(name, "tabpage") == 0)
5222 return (PyObject *)TabPageNew(curtab);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005223 else if (strcmp(name, "line") == 0)
5224 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
5225 else if (strcmp(name, "range") == 0)
5226 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005227 else if (strcmp(name, "__members__") == 0)
5228 return ObjectDir(NULL, CurrentAttrs);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005229 else
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005230#if PY_MAJOR_VERSION < 3
5231 return Py_FindMethod(WindowMethods, self, name);
5232#else
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005233 return NULL;
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005234#endif
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005235}
5236
5237 static int
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005238CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *valObject)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005239{
5240 if (strcmp(name, "line") == 0)
5241 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005242 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, valObject,
5243 NULL) == FAIL)
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005244 return -1;
5245
5246 return 0;
5247 }
Bram Moolenaare7614592013-05-15 15:51:08 +02005248 else if (strcmp(name, "buffer") == 0)
5249 {
5250 int count;
5251
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005252 if (valObject->ob_type != &BufferType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005253 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005254 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005255 N_("expected vim.Buffer object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005256 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005257 return -1;
5258 }
5259
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005260 if (CheckBuffer((BufferObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005261 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005262 count = ((BufferObject *)(valObject))->buf->b_fnum;
Bram Moolenaare7614592013-05-15 15:51:08 +02005263
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005264 VimTryStart();
Bram Moolenaare7614592013-05-15 15:51:08 +02005265 if (do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, count, 0) == FAIL)
5266 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005267 if (VimTryEnd())
5268 return -1;
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005269 PyErr_VIM_FORMAT(N_("failed to switch to buffer %d"), count);
Bram Moolenaare7614592013-05-15 15:51:08 +02005270 return -1;
5271 }
5272
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005273 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005274 }
5275 else if (strcmp(name, "window") == 0)
5276 {
5277 int count;
5278
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005279 if (valObject->ob_type != &WindowType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005280 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005281 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005282 N_("expected vim.Window object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005283 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005284 return -1;
5285 }
5286
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005287 if (CheckWindow((WindowObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005288 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005289 count = get_win_number(((WindowObject *)(valObject))->win, firstwin);
Bram Moolenaare7614592013-05-15 15:51:08 +02005290
5291 if (!count)
5292 {
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005293 PyErr_SET_STRING(PyExc_ValueError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005294 N_("failed to find window in the current tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005295 return -1;
5296 }
5297
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005298 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005299 win_goto(((WindowObject *)(valObject))->win);
5300 if (((WindowObject *)(valObject))->win != curwin)
Bram Moolenaare7614592013-05-15 15:51:08 +02005301 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005302 if (VimTryEnd())
5303 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005304 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005305 N_("did not switch to the specified window"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005306 return -1;
5307 }
5308
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005309 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005310 }
5311 else if (strcmp(name, "tabpage") == 0)
5312 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005313 if (valObject->ob_type != &TabPageType)
Bram Moolenaare7614592013-05-15 15:51:08 +02005314 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005315 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005316 N_("expected vim.TabPage object, but got %s"),
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005317 Py_TYPE_NAME(valObject));
Bram Moolenaare7614592013-05-15 15:51:08 +02005318 return -1;
5319 }
5320
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005321 if (CheckTabPage((TabPageObject *)(valObject)))
Bram Moolenaare7614592013-05-15 15:51:08 +02005322 return -1;
5323
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005324 VimTryStart();
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005325 goto_tabpage_tp(((TabPageObject *)(valObject))->tab, TRUE, TRUE);
5326 if (((TabPageObject *)(valObject))->tab != curtab)
Bram Moolenaare7614592013-05-15 15:51:08 +02005327 {
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005328 if (VimTryEnd())
5329 return -1;
Bram Moolenaar0bd80cc2013-06-23 13:28:17 +02005330 PyErr_SET_STRING(PyExc_RuntimeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005331 N_("did not switch to the specified tab page"));
Bram Moolenaare7614592013-05-15 15:51:08 +02005332 return -1;
5333 }
5334
Bram Moolenaara7b64ce2013-05-21 20:40:40 +02005335 return VimTryEnd();
Bram Moolenaare7614592013-05-15 15:51:08 +02005336 }
Bram Moolenaar4d1da492013-04-24 13:39:15 +02005337 else
5338 {
5339 PyErr_SetString(PyExc_AttributeError, name);
5340 return -1;
5341 }
5342}
5343
Bram Moolenaardd8aca62013-05-29 22:36:10 +02005344static struct PyMethodDef CurrentMethods[] = {
5345 /* name, function, calling, documentation */
5346 {"__dir__", (PyCFunction)CurrentDir, METH_NOARGS, ""},
5347 { NULL, NULL, 0, NULL}
5348};
5349
Bram Moolenaardb913952012-06-29 12:54:53 +02005350 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005351init_range_cmd(exarg_T *eap)
5352{
5353 RangeStart = eap->line1;
5354 RangeEnd = eap->line2;
5355}
5356
5357 static void
5358init_range_eval(typval_T *rettv UNUSED)
5359{
5360 RangeStart = (PyInt) curwin->w_cursor.lnum;
5361 RangeEnd = RangeStart;
5362}
5363
5364 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005365run_cmd(const char *cmd, void *arg UNUSED
5366#ifdef PY_CAN_RECURSE
5367 , PyGILState_STATE *pygilstate UNUSED
5368#endif
5369 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005370{
Bram Moolenaar41009372013-07-01 22:03:04 +02005371 PyObject *run_ret;
5372 run_ret = PyRun_String((char *)cmd, Py_file_input, globals, globals);
5373 if (run_ret != NULL)
5374 {
5375 Py_DECREF(run_ret);
5376 }
5377 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5378 {
5379 EMSG2(_(e_py_systemexit), "python");
5380 PyErr_Clear();
5381 }
5382 else
5383 PyErr_PrintEx(1);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005384}
5385
5386static const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
5387static int code_hdr_len = 30;
5388
5389 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005390run_do(const char *cmd, void *arg UNUSED
5391#ifdef PY_CAN_RECURSE
5392 , PyGILState_STATE *pygilstate
5393#endif
5394 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005395{
5396 PyInt lnum;
5397 size_t len;
5398 char *code;
5399 int status;
5400 PyObject *pyfunc, *pymain;
Bram Moolenaar41009372013-07-01 22:03:04 +02005401 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005402
Bram Moolenaar4ac66762013-05-28 22:31:46 +02005403 if (u_save((linenr_T)RangeStart - 1, (linenr_T)RangeEnd + 1) != OK)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005404 {
5405 EMSG(_("cannot save undo information"));
5406 return;
5407 }
5408
5409 len = code_hdr_len + STRLEN(cmd);
5410 code = PyMem_New(char, len + 1);
5411 memcpy(code, code_hdr, code_hdr_len);
5412 STRCPY(code + code_hdr_len, cmd);
Bram Moolenaar41009372013-07-01 22:03:04 +02005413 run_ret = PyRun_String(code, Py_file_input, globals, globals);
5414 status = -1;
5415 if (run_ret != NULL)
5416 {
5417 status = 0;
5418 Py_DECREF(run_ret);
5419 }
5420 else if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
5421 {
5422 PyMem_Free(code);
5423 EMSG2(_(e_py_systemexit), "python");
5424 PyErr_Clear();
5425 return;
5426 }
5427 else
5428 PyErr_PrintEx(1);
5429
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005430 PyMem_Free(code);
5431
5432 if (status)
5433 {
5434 EMSG(_("failed to run the code"));
5435 return;
5436 }
5437
5438 status = 0;
5439 pymain = PyImport_AddModule("__main__");
5440 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005441#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005442 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005443#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005444
5445 for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
5446 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005447 PyObject *line;
5448 PyObject *linenr;
5449 PyObject *ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005450
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005451#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005452 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005453#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005454 if (!(line = GetBufferLine(curbuf, lnum)))
5455 goto err;
5456 if (!(linenr = PyInt_FromLong((long) lnum)))
5457 {
5458 Py_DECREF(line);
5459 goto err;
5460 }
5461 ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
5462 Py_DECREF(line);
5463 Py_DECREF(linenr);
5464 if (!ret)
5465 goto err;
5466
5467 if (ret != Py_None)
5468 if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
5469 goto err;
5470
5471 Py_XDECREF(ret);
5472 PythonIO_Flush();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005473#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005474 PyGILState_Release(*pygilstate);
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005475#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005476 }
5477 goto out;
5478err:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005479#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005480 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005481#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005482 PyErr_PrintEx(0);
5483 PythonIO_Flush();
5484 status = 1;
5485out:
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005486#ifdef PY_CAN_RECURSE
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005487 if (!status)
5488 *pygilstate = PyGILState_Ensure();
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005489#endif
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005490 Py_DECREF(pyfunc);
5491 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
5492 if (status)
5493 return;
5494 check_cursor();
5495 update_curbuf(NOT_VALID);
5496}
5497
5498 static void
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +02005499run_eval(const char *cmd, typval_T *rettv
5500#ifdef PY_CAN_RECURSE
5501 , PyGILState_STATE *pygilstate UNUSED
5502#endif
5503 )
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005504{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005505 PyObject *run_ret;
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005506
Bram Moolenaar41009372013-07-01 22:03:04 +02005507 run_ret = PyRun_String((char *)cmd, Py_eval_input, globals, globals);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005508 if (run_ret == NULL)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005509 {
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005510 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SystemExit))
Bram Moolenaar41009372013-07-01 22:03:04 +02005511 {
5512 EMSG2(_(e_py_systemexit), "python");
5513 PyErr_Clear();
5514 }
Bram Moolenaar91aeaf42013-07-06 13:02:30 +02005515 else
5516 {
5517 if (PyErr_Occurred() && !msg_silent)
5518 PyErr_PrintEx(0);
5519 EMSG(_("E858: Eval did not return a valid python object"));
5520 }
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005521 }
5522 else
5523 {
Bram Moolenaar77324fc2016-01-17 22:37:03 +01005524 if (run_ret != Py_None && ConvertFromPyObject(run_ret, rettv) == -1)
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005525 EMSG(_("E859: Failed to convert returned python object to vim value"));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005526 Py_DECREF(run_ret);
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02005527 }
5528 PyErr_Clear();
5529}
5530
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005531 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005532set_ref_in_py(const int copyID)
5533{
5534 pylinkedlist_T *cur;
5535 dict_T *dd;
5536 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005537 int abort = FALSE;
Bram Moolenaardb913952012-06-29 12:54:53 +02005538
5539 if (lastdict != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005540 {
5541 for(cur = lastdict ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005542 {
5543 dd = ((DictionaryObject *) (cur->pll_obj))->dict;
5544 if (dd->dv_copyID != copyID)
5545 {
5546 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005547 abort = abort || set_ref_in_ht(&dd->dv_hashtab, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005548 }
5549 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005550 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005551
5552 if (lastlist != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005553 {
5554 for(cur = lastlist ; !abort && cur != NULL ; cur = cur->pll_prev)
Bram Moolenaardb913952012-06-29 12:54:53 +02005555 {
5556 ll = ((ListObject *) (cur->pll_obj))->list;
5557 if (ll->lv_copyID != copyID)
5558 {
5559 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005560 abort = abort || set_ref_in_list(ll, copyID, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005561 }
5562 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005563 }
5564
5565 return abort;
Bram Moolenaardb913952012-06-29 12:54:53 +02005566}
5567
5568 static int
5569set_string_copy(char_u *str, typval_T *tv)
5570{
5571 tv->vval.v_string = vim_strsave(str);
5572 if (tv->vval.v_string == NULL)
5573 {
5574 PyErr_NoMemory();
5575 return -1;
5576 }
5577 return 0;
5578}
5579
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005580 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005581pydict_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005582{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005583 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005584 char_u *key;
5585 dictitem_T *di;
5586 PyObject *keyObject;
5587 PyObject *valObject;
5588 Py_ssize_t iter = 0;
5589
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005590 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005591 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005592
5593 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005594 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005595
5596 while (PyDict_Next(obj, &iter, &keyObject, &valObject))
5597 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005598 PyObject *todecref = NULL;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005599
Bram Moolenaara03e6312013-05-29 22:49:26 +02005600 if (keyObject == NULL || valObject == NULL)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005601 {
5602 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005603 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005604 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005605
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005606 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005607 {
5608 dict_unref(dict);
5609 return -1;
5610 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005611
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005612 if (*key == NUL)
5613 {
5614 dict_unref(dict);
5615 Py_XDECREF(todecref);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005616 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005617 return -1;
5618 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005619
5620 di = dictitem_alloc(key);
5621
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005622 Py_XDECREF(todecref);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005623
5624 if (di == NULL)
5625 {
5626 PyErr_NoMemory();
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005627 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005628 return -1;
5629 }
5630 di->di_tv.v_lock = 0;
5631
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005632 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005633 {
5634 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005635 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005636 return -1;
5637 }
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005638
5639 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005640 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005641 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaara03e6312013-05-29 22:49:26 +02005642 clear_tv(&di->di_tv);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005643 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005644 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005645 return -1;
5646 }
5647 }
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005648
5649 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005650 return 0;
5651}
5652
5653 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005654pymap_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005655{
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005656 dict_T *dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005657 char_u *key;
5658 dictitem_T *di;
5659 PyObject *list;
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005660 PyObject *iterator;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005661 PyObject *keyObject;
5662 PyObject *valObject;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005663
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005664 if (!(dict = py_dict_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005665 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005666
5667 tv->v_type = VAR_DICT;
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005668 tv->vval.v_dict = dict;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005669
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005670 if (!(list = PyMapping_Keys(obj)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005671 {
5672 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005673 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005674 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005675
5676 if (!(iterator = PyObject_GetIter(list)))
5677 {
5678 dict_unref(dict);
5679 Py_DECREF(list);
5680 return -1;
5681 }
5682 Py_DECREF(list);
5683
5684 while ((keyObject = PyIter_Next(iterator)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005685 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005686 PyObject *todecref;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005687
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005688 if (!(key = StringToChars(keyObject, &todecref)))
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005689 {
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005690 Py_DECREF(keyObject);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005691 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005692 dict_unref(dict);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005693 return -1;
5694 }
Bram Moolenaar4f2109d2013-06-02 18:07:37 +02005695
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005696 if (*key == NUL)
5697 {
5698 Py_DECREF(keyObject);
5699 Py_DECREF(iterator);
5700 Py_XDECREF(todecref);
5701 dict_unref(dict);
Bram Moolenaar35eacd72013-05-30 22:06:33 +02005702 RAISE_NO_EMPTY_KEYS;
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005703 return -1;
5704 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005705
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005706 if (!(valObject = PyObject_GetItem(obj, keyObject)))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005707 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005708 Py_DECREF(keyObject);
5709 Py_DECREF(iterator);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005710 Py_XDECREF(todecref);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005711 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005712 return -1;
5713 }
5714
5715 di = dictitem_alloc(key);
5716
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005717 Py_DECREF(keyObject);
Bram Moolenaarfc714b32013-05-30 14:52:37 +02005718 Py_XDECREF(todecref);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005719
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005720 if (di == NULL)
5721 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005722 Py_DECREF(iterator);
5723 Py_DECREF(valObject);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005724 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005725 PyErr_NoMemory();
5726 return -1;
5727 }
5728 di->di_tv.v_lock = 0;
5729
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005730 if (_ConvertFromPyObject(valObject, &di->di_tv, lookup_dict) == -1)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005731 {
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005732 Py_DECREF(iterator);
5733 Py_DECREF(valObject);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005734 vim_free(di);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005735 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005736 return -1;
5737 }
Bram Moolenaara03e6312013-05-29 22:49:26 +02005738
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005739 Py_DECREF(valObject);
5740
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005741 if (dict_add(dict, di) == FAIL)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005742 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005743 RAISE_KEY_ADD_FAIL(di->di_key);
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005744 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005745 dictitem_free(di);
5746 dict_unref(dict);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005747 return -1;
5748 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005749 }
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005750 Py_DECREF(iterator);
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005751 --dict->dv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005752 return 0;
5753}
5754
5755 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005756pyseq_to_tv(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005757{
5758 list_T *l;
5759
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005760 if (!(l = py_list_alloc()))
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005761 return -1;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005762
5763 tv->v_type = VAR_LIST;
5764 tv->vval.v_list = l;
5765
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005766 if (list_py_concat(l, obj, lookup_dict) == -1)
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005767 {
5768 list_unref(l);
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005769 return -1;
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005770 }
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005771
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005772 --l->lv_refcount;
Bram Moolenaar3d0c52d2013-05-12 19:45:35 +02005773 return 0;
5774}
5775
Bram Moolenaardb913952012-06-29 12:54:53 +02005776typedef int (*pytotvfunc)(PyObject *, typval_T *, PyObject *);
5777
5778 static int
5779convert_dl(PyObject *obj, typval_T *tv,
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005780 pytotvfunc py_to_tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005781{
5782 PyObject *capsule;
5783 char hexBuf[sizeof(void *) * 2 + 3];
5784
5785 sprintf(hexBuf, "%p", obj);
5786
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005787# ifdef PY_USE_CAPSULE
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005788 capsule = PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005789# else
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005790 capsule = (PyObject *)PyDict_GetItemString(lookup_dict, hexBuf);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005791# endif
Bram Moolenaar221d6872012-06-30 13:34:34 +02005792 if (capsule == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005793 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005794# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +02005795 capsule = PyCapsule_New(tv, NULL, NULL);
Bram Moolenaar221d6872012-06-30 13:34:34 +02005796# else
5797 capsule = PyCObject_FromVoidPtr(tv, NULL);
5798# endif
Bram Moolenaara03e6312013-05-29 22:49:26 +02005799 if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
5800 {
5801 Py_DECREF(capsule);
5802 tv->v_type = VAR_UNKNOWN;
5803 return -1;
5804 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005805
5806 Py_DECREF(capsule);
5807
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005808 if (py_to_tv(obj, tv, lookup_dict) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005809 {
5810 tv->v_type = VAR_UNKNOWN;
5811 return -1;
5812 }
5813 /* As we are not using copy_tv which increments reference count we must
5814 * do it ourself. */
5815 switch(tv->v_type)
5816 {
5817 case VAR_DICT: ++tv->vval.v_dict->dv_refcount; break;
5818 case VAR_LIST: ++tv->vval.v_list->lv_refcount; break;
5819 }
5820 }
5821 else
5822 {
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005823 typval_T *v;
5824
5825# ifdef PY_USE_CAPSULE
5826 v = PyCapsule_GetPointer(capsule, NULL);
5827# else
Bram Moolenaar221d6872012-06-30 13:34:34 +02005828 v = PyCObject_AsVoidPtr(capsule);
Bram Moolenaar2afa3232012-06-29 16:28:28 +02005829# endif
Bram Moolenaardb913952012-06-29 12:54:53 +02005830 copy_tv(v, tv);
5831 }
5832 return 0;
5833}
5834
5835 static int
Bram Moolenaara9922d62013-05-30 13:01:18 +02005836ConvertFromPyMapping(PyObject *obj, typval_T *tv)
5837{
5838 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005839 int ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005840
5841 if (!(lookup_dict = PyDict_New()))
5842 return -1;
5843
5844 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
5845 {
5846 tv->v_type = VAR_DICT;
5847 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5848 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005849 ret = 0;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005850 }
5851 else if (PyDict_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005852 ret = convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005853 else if (PyMapping_Check(obj))
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005854 ret = convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaara9922d62013-05-30 13:01:18 +02005855 else
5856 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005857 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005858 N_("unable to convert %s to vim dictionary"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005859 Py_TYPE_NAME(obj));
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005860 ret = -1;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005861 }
5862 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005863 return ret;
Bram Moolenaara9922d62013-05-30 13:01:18 +02005864}
5865
5866 static int
Bram Moolenaardb913952012-06-29 12:54:53 +02005867ConvertFromPyObject(PyObject *obj, typval_T *tv)
5868{
5869 PyObject *lookup_dict;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005870 int ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005871
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005872 if (!(lookup_dict = PyDict_New()))
5873 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005874 ret = _ConvertFromPyObject(obj, tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005875 Py_DECREF(lookup_dict);
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005876 return ret;
Bram Moolenaardb913952012-06-29 12:54:53 +02005877}
5878
5879 static int
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005880_ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
Bram Moolenaardb913952012-06-29 12:54:53 +02005881{
Bram Moolenaara9922d62013-05-30 13:01:18 +02005882 if (PyType_IsSubtype(obj->ob_type, &DictionaryType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005883 {
5884 tv->v_type = VAR_DICT;
5885 tv->vval.v_dict = (((DictionaryObject *)(obj))->dict);
5886 ++tv->vval.v_dict->dv_refcount;
5887 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005888 else if (PyType_IsSubtype(obj->ob_type, &ListType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005889 {
5890 tv->v_type = VAR_LIST;
5891 tv->vval.v_list = (((ListObject *)(obj))->list);
5892 ++tv->vval.v_list->lv_refcount;
5893 }
Bram Moolenaar841fbd22013-06-23 14:37:07 +02005894 else if (PyType_IsSubtype(obj->ob_type, &FunctionType))
Bram Moolenaardb913952012-06-29 12:54:53 +02005895 {
5896 if (set_string_copy(((FunctionObject *) (obj))->name, tv) == -1)
5897 return -1;
5898
5899 tv->v_type = VAR_FUNC;
5900 func_ref(tv->vval.v_string);
5901 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005902 else if (PyBytes_Check(obj))
5903 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005904 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005905
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005906 if (PyBytes_AsStringAndSize(obj, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005907 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005908 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005909 return -1;
5910
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005911 if (set_string_copy(str, tv) == -1)
Bram Moolenaardb913952012-06-29 12:54:53 +02005912 return -1;
5913
5914 tv->v_type = VAR_STRING;
5915 }
5916 else if (PyUnicode_Check(obj))
5917 {
5918 PyObject *bytes;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005919 char_u *str;
Bram Moolenaardb913952012-06-29 12:54:53 +02005920
Bram Moolenaar808c2bc2013-06-23 13:11:18 +02005921 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02005922 if (bytes == NULL)
5923 return -1;
5924
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005925 if(PyBytes_AsStringAndSize(bytes, (char **) &str, NULL) == -1)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +02005926 return -1;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005927 if (str == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +02005928 return -1;
5929
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02005930 if (set_string_copy(str, tv))
Bram Moolenaardb913952012-06-29 12:54:53 +02005931 {
5932 Py_XDECREF(bytes);
5933 return -1;
5934 }
5935 Py_XDECREF(bytes);
5936
5937 tv->v_type = VAR_STRING;
5938 }
Bram Moolenaar335e0b62013-04-24 13:47:45 +02005939#if PY_MAJOR_VERSION < 3
Bram Moolenaardb913952012-06-29 12:54:53 +02005940 else if (PyInt_Check(obj))
5941 {
5942 tv->v_type = VAR_NUMBER;
5943 tv->vval.v_number = (varnumber_T) PyInt_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005944 if (PyErr_Occurred())
5945 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005946 }
5947#endif
5948 else if (PyLong_Check(obj))
5949 {
5950 tv->v_type = VAR_NUMBER;
5951 tv->vval.v_number = (varnumber_T) PyLong_AsLong(obj);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005952 if (PyErr_Occurred())
5953 return -1;
Bram Moolenaardb913952012-06-29 12:54:53 +02005954 }
5955 else if (PyDict_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005956 return convert_dl(obj, tv, pydict_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005957#ifdef FEAT_FLOAT
5958 else if (PyFloat_Check(obj))
5959 {
5960 tv->v_type = VAR_FLOAT;
5961 tv->vval.v_float = (float_T) PyFloat_AsDouble(obj);
5962 }
5963#endif
Bram Moolenaarbcb40972013-05-30 13:22:13 +02005964 else if (PyObject_HasAttrString(obj, "keys"))
5965 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaardee2e312013-06-23 16:35:47 +02005966 /* PyObject_GetIter can create built-in iterator for any sequence object */
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02005967 else if (PyIter_Check(obj) || PySequence_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005968 return convert_dl(obj, tv, pyseq_to_tv, lookup_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02005969 else if (PyMapping_Check(obj))
Bram Moolenaarb38caae2013-05-29 22:39:52 +02005970 return convert_dl(obj, tv, pymap_to_tv, lookup_dict);
Bram Moolenaar141be8a2013-06-23 14:16:57 +02005971 else if (PyNumber_Check(obj))
5972 {
5973 PyObject *num;
5974
5975 if (!(num = PyNumber_Long(obj)))
5976 return -1;
5977
5978 tv->v_type = VAR_NUMBER;
5979 tv->vval.v_number = (varnumber_T) PyLong_AsLong(num);
5980
5981 Py_DECREF(num);
5982 }
Bram Moolenaardb913952012-06-29 12:54:53 +02005983 else
5984 {
Bram Moolenaarc476e522013-06-23 13:46:40 +02005985 PyErr_FORMAT(PyExc_TypeError,
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005986 N_("unable to convert %s to vim structure"),
Bram Moolenaarc476e522013-06-23 13:46:40 +02005987 Py_TYPE_NAME(obj));
Bram Moolenaardb913952012-06-29 12:54:53 +02005988 return -1;
5989 }
5990 return 0;
5991}
5992
5993 static PyObject *
5994ConvertToPyObject(typval_T *tv)
5995{
5996 if (tv == NULL)
5997 {
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02005998 PyErr_SET_VIM(N_("internal error: NULL reference passed"));
Bram Moolenaardb913952012-06-29 12:54:53 +02005999 return NULL;
6000 }
6001 switch (tv->v_type)
6002 {
6003 case VAR_STRING:
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006004 return PyBytes_FromString(tv->vval.v_string == NULL
6005 ? "" : (char *)tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006006 case VAR_NUMBER:
6007 return PyLong_FromLong((long) tv->vval.v_number);
6008#ifdef FEAT_FLOAT
6009 case VAR_FLOAT:
6010 return PyFloat_FromDouble((double) tv->vval.v_float);
6011#endif
6012 case VAR_LIST:
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006013 return NEW_LIST(tv->vval.v_list);
Bram Moolenaardb913952012-06-29 12:54:53 +02006014 case VAR_DICT:
Bram Moolenaara9922d62013-05-30 13:01:18 +02006015 return NEW_DICTIONARY(tv->vval.v_dict);
Bram Moolenaardb913952012-06-29 12:54:53 +02006016 case VAR_FUNC:
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006017 return NEW_FUNCTION(tv->vval.v_string == NULL
Bram Moolenaard1f13fd2012-10-05 21:30:07 +02006018 ? (char_u *)"" : tv->vval.v_string);
Bram Moolenaardb913952012-06-29 12:54:53 +02006019 case VAR_UNKNOWN:
6020 Py_INCREF(Py_None);
6021 return Py_None;
6022 default:
Bram Moolenaar6f1404f2013-06-23 16:04:08 +02006023 PyErr_SET_VIM(N_("internal error: invalid value type"));
Bram Moolenaardb913952012-06-29 12:54:53 +02006024 return NULL;
6025 }
6026}
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006027
6028typedef struct
6029{
6030 PyObject_HEAD
6031} CurrentObject;
6032static PyTypeObject CurrentType;
6033
6034 static void
6035init_structs(void)
6036{
6037 vim_memset(&OutputType, 0, sizeof(OutputType));
6038 OutputType.tp_name = "vim.message";
6039 OutputType.tp_basicsize = sizeof(OutputObject);
6040 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
6041 OutputType.tp_doc = "vim message object";
6042 OutputType.tp_methods = OutputMethods;
6043#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006044 OutputType.tp_getattro = (getattrofunc)OutputGetattro;
6045 OutputType.tp_setattro = (setattrofunc)OutputSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006046 OutputType.tp_alloc = call_PyType_GenericAlloc;
6047 OutputType.tp_new = call_PyType_GenericNew;
6048 OutputType.tp_free = call_PyObject_Free;
6049#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006050 OutputType.tp_getattr = (getattrfunc)OutputGetattr;
6051 OutputType.tp_setattr = (setattrfunc)OutputSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006052#endif
6053
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006054 vim_memset(&IterType, 0, sizeof(IterType));
6055 IterType.tp_name = "vim.iter";
6056 IterType.tp_basicsize = sizeof(IterObject);
Bram Moolenaar07b88642013-05-29 22:58:32 +02006057 IterType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006058 IterType.tp_doc = "generic iterator object";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006059 IterType.tp_iter = (getiterfunc)IterIter;
6060 IterType.tp_iternext = (iternextfunc)IterNext;
6061 IterType.tp_dealloc = (destructor)IterDestructor;
6062 IterType.tp_traverse = (traverseproc)IterTraverse;
6063 IterType.tp_clear = (inquiry)IterClear;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006064
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006065 vim_memset(&BufferType, 0, sizeof(BufferType));
6066 BufferType.tp_name = "vim.buffer";
6067 BufferType.tp_basicsize = sizeof(BufferType);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006068 BufferType.tp_dealloc = (destructor)BufferDestructor;
6069 BufferType.tp_repr = (reprfunc)BufferRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006070 BufferType.tp_as_sequence = &BufferAsSeq;
6071 BufferType.tp_as_mapping = &BufferAsMapping;
6072 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
6073 BufferType.tp_doc = "vim buffer object";
6074 BufferType.tp_methods = BufferMethods;
6075#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006076 BufferType.tp_getattro = (getattrofunc)BufferGetattro;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006077 BufferType.tp_setattro = (setattrofunc)BufferSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006078 BufferType.tp_alloc = call_PyType_GenericAlloc;
6079 BufferType.tp_new = call_PyType_GenericNew;
6080 BufferType.tp_free = call_PyObject_Free;
6081#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006082 BufferType.tp_getattr = (getattrfunc)BufferGetattr;
Bram Moolenaare9ba5162013-05-29 22:02:22 +02006083 BufferType.tp_setattr = (setattrfunc)BufferSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006084#endif
6085
6086 vim_memset(&WindowType, 0, sizeof(WindowType));
6087 WindowType.tp_name = "vim.window";
6088 WindowType.tp_basicsize = sizeof(WindowObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006089 WindowType.tp_dealloc = (destructor)WindowDestructor;
6090 WindowType.tp_repr = (reprfunc)WindowRepr;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006091 WindowType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006092 WindowType.tp_doc = "vim Window object";
6093 WindowType.tp_methods = WindowMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006094 WindowType.tp_traverse = (traverseproc)WindowTraverse;
6095 WindowType.tp_clear = (inquiry)WindowClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006096#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006097 WindowType.tp_getattro = (getattrofunc)WindowGetattro;
6098 WindowType.tp_setattro = (setattrofunc)WindowSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006099 WindowType.tp_alloc = call_PyType_GenericAlloc;
6100 WindowType.tp_new = call_PyType_GenericNew;
6101 WindowType.tp_free = call_PyObject_Free;
6102#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006103 WindowType.tp_getattr = (getattrfunc)WindowGetattr;
6104 WindowType.tp_setattr = (setattrfunc)WindowSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006105#endif
6106
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006107 vim_memset(&TabPageType, 0, sizeof(TabPageType));
6108 TabPageType.tp_name = "vim.tabpage";
6109 TabPageType.tp_basicsize = sizeof(TabPageObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006110 TabPageType.tp_dealloc = (destructor)TabPageDestructor;
6111 TabPageType.tp_repr = (reprfunc)TabPageRepr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006112 TabPageType.tp_flags = Py_TPFLAGS_DEFAULT;
6113 TabPageType.tp_doc = "vim tab page object";
6114 TabPageType.tp_methods = TabPageMethods;
6115#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006116 TabPageType.tp_getattro = (getattrofunc)TabPageGetattro;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006117 TabPageType.tp_alloc = call_PyType_GenericAlloc;
6118 TabPageType.tp_new = call_PyType_GenericNew;
6119 TabPageType.tp_free = call_PyObject_Free;
6120#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006121 TabPageType.tp_getattr = (getattrfunc)TabPageGetattr;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006122#endif
6123
Bram Moolenaardfa38d42013-05-15 13:38:47 +02006124 vim_memset(&BufMapType, 0, sizeof(BufMapType));
6125 BufMapType.tp_name = "vim.bufferlist";
6126 BufMapType.tp_basicsize = sizeof(BufMapObject);
6127 BufMapType.tp_as_mapping = &BufMapAsMapping;
6128 BufMapType.tp_flags = Py_TPFLAGS_DEFAULT;
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006129 BufMapType.tp_iter = BufMapIter;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006130 BufferType.tp_doc = "vim buffer list";
6131
6132 vim_memset(&WinListType, 0, sizeof(WinListType));
6133 WinListType.tp_name = "vim.windowlist";
6134 WinListType.tp_basicsize = sizeof(WinListType);
6135 WinListType.tp_as_sequence = &WinListAsSeq;
6136 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
6137 WinListType.tp_doc = "vim window list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006138 WinListType.tp_dealloc = (destructor)WinListDestructor;
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006139
6140 vim_memset(&TabListType, 0, sizeof(TabListType));
6141 TabListType.tp_name = "vim.tabpagelist";
6142 TabListType.tp_basicsize = sizeof(TabListType);
6143 TabListType.tp_as_sequence = &TabListAsSeq;
6144 TabListType.tp_flags = Py_TPFLAGS_DEFAULT;
6145 TabListType.tp_doc = "vim tab page list";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006146
6147 vim_memset(&RangeType, 0, sizeof(RangeType));
6148 RangeType.tp_name = "vim.range";
6149 RangeType.tp_basicsize = sizeof(RangeObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006150 RangeType.tp_dealloc = (destructor)RangeDestructor;
6151 RangeType.tp_repr = (reprfunc)RangeRepr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006152 RangeType.tp_as_sequence = &RangeAsSeq;
6153 RangeType.tp_as_mapping = &RangeAsMapping;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006154 RangeType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006155 RangeType.tp_doc = "vim Range object";
6156 RangeType.tp_methods = RangeMethods;
Bram Moolenaar774267b2013-05-21 20:51:59 +02006157 RangeType.tp_traverse = (traverseproc)RangeTraverse;
6158 RangeType.tp_clear = (inquiry)RangeClear;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006159#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006160 RangeType.tp_getattro = (getattrofunc)RangeGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006161 RangeType.tp_alloc = call_PyType_GenericAlloc;
6162 RangeType.tp_new = call_PyType_GenericNew;
6163 RangeType.tp_free = call_PyObject_Free;
6164#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006165 RangeType.tp_getattr = (getattrfunc)RangeGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006166#endif
6167
6168 vim_memset(&CurrentType, 0, sizeof(CurrentType));
6169 CurrentType.tp_name = "vim.currentdata";
6170 CurrentType.tp_basicsize = sizeof(CurrentObject);
6171 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
6172 CurrentType.tp_doc = "vim current object";
Bram Moolenaardd8aca62013-05-29 22:36:10 +02006173 CurrentType.tp_methods = CurrentMethods;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006174#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006175 CurrentType.tp_getattro = (getattrofunc)CurrentGetattro;
6176 CurrentType.tp_setattro = (setattrofunc)CurrentSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006177#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006178 CurrentType.tp_getattr = (getattrfunc)CurrentGetattr;
6179 CurrentType.tp_setattr = (setattrfunc)CurrentSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006180#endif
6181
6182 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
6183 DictionaryType.tp_name = "vim.dictionary";
6184 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006185 DictionaryType.tp_dealloc = (destructor)DictionaryDestructor;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006186 DictionaryType.tp_as_sequence = &DictionaryAsSeq;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006187 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006188 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006189 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
6190 DictionaryType.tp_methods = DictionaryMethods;
Bram Moolenaara9922d62013-05-30 13:01:18 +02006191 DictionaryType.tp_iter = (getiterfunc)DictionaryIter;
6192 DictionaryType.tp_new = (newfunc)DictionaryConstructor;
6193 DictionaryType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006194#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006195 DictionaryType.tp_getattro = (getattrofunc)DictionaryGetattro;
6196 DictionaryType.tp_setattro = (setattrofunc)DictionarySetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006197#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006198 DictionaryType.tp_getattr = (getattrfunc)DictionaryGetattr;
6199 DictionaryType.tp_setattr = (setattrfunc)DictionarySetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006200#endif
6201
6202 vim_memset(&ListType, 0, sizeof(ListType));
6203 ListType.tp_name = "vim.list";
Bram Moolenaard6e39182013-05-21 18:30:34 +02006204 ListType.tp_dealloc = (destructor)ListDestructor;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006205 ListType.tp_basicsize = sizeof(ListObject);
6206 ListType.tp_as_sequence = &ListAsSeq;
6207 ListType.tp_as_mapping = &ListAsMapping;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006208 ListType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006209 ListType.tp_doc = "list pushing modifications to vim structure";
6210 ListType.tp_methods = ListMethods;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006211 ListType.tp_iter = (getiterfunc)ListIter;
Bram Moolenaar78cddbe2013-05-30 13:05:58 +02006212 ListType.tp_new = (newfunc)ListConstructor;
6213 ListType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006214#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006215 ListType.tp_getattro = (getattrofunc)ListGetattro;
6216 ListType.tp_setattro = (setattrofunc)ListSetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006217#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006218 ListType.tp_getattr = (getattrfunc)ListGetattr;
6219 ListType.tp_setattr = (setattrfunc)ListSetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006220#endif
6221
6222 vim_memset(&FunctionType, 0, sizeof(FunctionType));
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02006223 FunctionType.tp_name = "vim.function";
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006224 FunctionType.tp_basicsize = sizeof(FunctionObject);
Bram Moolenaard6e39182013-05-21 18:30:34 +02006225 FunctionType.tp_dealloc = (destructor)FunctionDestructor;
6226 FunctionType.tp_call = (ternaryfunc)FunctionCall;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006227 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006228 FunctionType.tp_doc = "object that calls vim function";
6229 FunctionType.tp_methods = FunctionMethods;
Bram Moolenaara5b725c2013-05-30 12:43:54 +02006230 FunctionType.tp_repr = (reprfunc)FunctionRepr;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +02006231 FunctionType.tp_new = (newfunc)FunctionConstructor;
6232 FunctionType.tp_alloc = (allocfunc)PyType_GenericAlloc;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006233#if PY_MAJOR_VERSION >= 3
Bram Moolenaard6e39182013-05-21 18:30:34 +02006234 FunctionType.tp_getattro = (getattrofunc)FunctionGetattro;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006235#else
Bram Moolenaard6e39182013-05-21 18:30:34 +02006236 FunctionType.tp_getattr = (getattrfunc)FunctionGetattr;
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006237#endif
6238
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006239 vim_memset(&OptionsType, 0, sizeof(OptionsType));
6240 OptionsType.tp_name = "vim.options";
6241 OptionsType.tp_basicsize = sizeof(OptionsObject);
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006242 OptionsType.tp_as_sequence = &OptionsAsSeq;
Bram Moolenaar07b88642013-05-29 22:58:32 +02006243 OptionsType.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006244 OptionsType.tp_doc = "object for manipulating options";
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006245 OptionsType.tp_iter = (getiterfunc)OptionsIter;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006246 OptionsType.tp_as_mapping = &OptionsAsMapping;
Bram Moolenaard6e39182013-05-21 18:30:34 +02006247 OptionsType.tp_dealloc = (destructor)OptionsDestructor;
6248 OptionsType.tp_traverse = (traverseproc)OptionsTraverse;
6249 OptionsType.tp_clear = (inquiry)OptionsClear;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006250
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006251 vim_memset(&LoaderType, 0, sizeof(LoaderType));
6252 LoaderType.tp_name = "vim.Loader";
6253 LoaderType.tp_basicsize = sizeof(LoaderObject);
6254 LoaderType.tp_flags = Py_TPFLAGS_DEFAULT;
6255 LoaderType.tp_doc = "vim message object";
6256 LoaderType.tp_methods = LoaderMethods;
6257 LoaderType.tp_dealloc = (destructor)LoaderDestructor;
6258
Bram Moolenaar4d1da492013-04-24 13:39:15 +02006259#if PY_MAJOR_VERSION >= 3
6260 vim_memset(&vimmodule, 0, sizeof(vimmodule));
6261 vimmodule.m_name = "vim";
6262 vimmodule.m_doc = "Vim Python interface\n";
6263 vimmodule.m_size = -1;
6264 vimmodule.m_methods = VimMethods;
6265#endif
6266}
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006267
6268#define PYTYPE_READY(type) \
6269 if (PyType_Ready(&type)) \
6270 return -1;
6271
6272 static int
Bram Moolenaarfb97f282013-07-09 17:42:46 +02006273init_types(void)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006274{
6275 PYTYPE_READY(IterType);
6276 PYTYPE_READY(BufferType);
6277 PYTYPE_READY(RangeType);
6278 PYTYPE_READY(WindowType);
6279 PYTYPE_READY(TabPageType);
6280 PYTYPE_READY(BufMapType);
6281 PYTYPE_READY(WinListType);
6282 PYTYPE_READY(TabListType);
6283 PYTYPE_READY(CurrentType);
6284 PYTYPE_READY(DictionaryType);
6285 PYTYPE_READY(ListType);
6286 PYTYPE_READY(FunctionType);
6287 PYTYPE_READY(OptionsType);
6288 PYTYPE_READY(OutputType);
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006289 PYTYPE_READY(LoaderType);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006290 return 0;
6291}
6292
6293 static int
Bram Moolenaar5ab9d982013-06-16 14:25:57 +02006294init_sys_path(void)
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006295{
6296 PyObject *path;
6297 PyObject *path_hook;
6298 PyObject *path_hooks;
6299
6300 if (!(path_hook = PyObject_GetAttrString(vim_module, "path_hook")))
6301 return -1;
6302
6303 if (!(path_hooks = PySys_GetObject("path_hooks")))
6304 {
6305 PyErr_Clear();
6306 path_hooks = PyList_New(1);
6307 PyList_SET_ITEM(path_hooks, 0, path_hook);
6308 if (PySys_SetObject("path_hooks", path_hooks))
6309 {
6310 Py_DECREF(path_hooks);
6311 return -1;
6312 }
6313 Py_DECREF(path_hooks);
6314 }
6315 else if (PyList_Check(path_hooks))
6316 {
6317 if (PyList_Append(path_hooks, path_hook))
6318 {
6319 Py_DECREF(path_hook);
6320 return -1;
6321 }
6322 Py_DECREF(path_hook);
6323 }
6324 else
6325 {
6326 VimTryStart();
6327 EMSG(_("Failed to set path hook: sys.path_hooks is not a list\n"
6328 "You should now do the following:\n"
6329 "- append vim.path_hook to sys.path_hooks\n"
6330 "- append vim.VIM_SPECIAL_PATH to sys.path\n"));
6331 VimTryEnd(); /* Discard the error */
6332 Py_DECREF(path_hook);
6333 return 0;
6334 }
6335
6336 if (!(path = PySys_GetObject("path")))
6337 {
6338 PyErr_Clear();
6339 path = PyList_New(1);
6340 Py_INCREF(vim_special_path_object);
6341 PyList_SET_ITEM(path, 0, vim_special_path_object);
6342 if (PySys_SetObject("path", path))
6343 {
6344 Py_DECREF(path);
6345 return -1;
6346 }
6347 Py_DECREF(path);
6348 }
6349 else if (PyList_Check(path))
6350 {
6351 if (PyList_Append(path, vim_special_path_object))
6352 return -1;
6353 }
6354 else
6355 {
6356 VimTryStart();
6357 EMSG(_("Failed to set path: sys.path is not a list\n"
6358 "You should now append vim.VIM_SPECIAL_PATH to sys.path"));
6359 VimTryEnd(); /* Discard the error */
6360 }
6361
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006362 return 0;
6363}
6364
6365static BufMapObject TheBufferMap =
6366{
6367 PyObject_HEAD_INIT(&BufMapType)
6368};
6369
6370static WinListObject TheWindowList =
6371{
6372 PyObject_HEAD_INIT(&WinListType)
6373 NULL
6374};
6375
6376static CurrentObject TheCurrent =
6377{
6378 PyObject_HEAD_INIT(&CurrentType)
6379};
6380
6381static TabListObject TheTabPageList =
6382{
6383 PyObject_HEAD_INIT(&TabListType)
6384};
6385
6386static struct numeric_constant {
6387 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006388 int val;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006389} numeric_constants[] = {
6390 {"VAR_LOCKED", VAR_LOCKED},
6391 {"VAR_FIXED", VAR_FIXED},
6392 {"VAR_SCOPE", VAR_SCOPE},
6393 {"VAR_DEF_SCOPE", VAR_DEF_SCOPE},
6394};
6395
6396static struct object_constant {
6397 char *name;
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006398 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006399} object_constants[] = {
6400 {"buffers", (PyObject *)(void *)&TheBufferMap},
6401 {"windows", (PyObject *)(void *)&TheWindowList},
6402 {"tabpages", (PyObject *)(void *)&TheTabPageList},
6403 {"current", (PyObject *)(void *)&TheCurrent},
Bram Moolenaarcac867a2013-05-21 19:50:34 +02006404
6405 {"Buffer", (PyObject *)&BufferType},
6406 {"Range", (PyObject *)&RangeType},
6407 {"Window", (PyObject *)&WindowType},
6408 {"TabPage", (PyObject *)&TabPageType},
6409 {"Dictionary", (PyObject *)&DictionaryType},
6410 {"List", (PyObject *)&ListType},
6411 {"Function", (PyObject *)&FunctionType},
6412 {"Options", (PyObject *)&OptionsType},
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006413 {"_Loader", (PyObject *)&LoaderType},
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006414};
6415
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006416#define ADD_OBJECT(m, name, obj) \
Bram Moolenaardee2e312013-06-23 16:35:47 +02006417 if (PyModule_AddObject(m, name, obj)) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006418 return -1;
6419
6420#define ADD_CHECKED_OBJECT(m, name, obj) \
6421 { \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006422 PyObject *valObject = obj; \
6423 if (!valObject) \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006424 return -1; \
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006425 ADD_OBJECT(m, name, valObject); \
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006426 }
6427
6428 static int
Bram Moolenaardee2e312013-06-23 16:35:47 +02006429populate_module(PyObject *m)
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006430{
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006431 int i;
6432 PyObject *other_module;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006433 PyObject *attr;
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006434 PyObject *imp;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006435
6436 for (i = 0; i < (int)(sizeof(numeric_constants)
6437 / sizeof(struct numeric_constant));
6438 ++i)
6439 ADD_CHECKED_OBJECT(m, numeric_constants[i].name,
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006440 PyInt_FromLong(numeric_constants[i].val));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006441
6442 for (i = 0; i < (int)(sizeof(object_constants)
6443 / sizeof(struct object_constant));
6444 ++i)
6445 {
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006446 PyObject *valObject;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006447
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02006448 valObject = object_constants[i].valObject;
6449 Py_INCREF(valObject);
6450 ADD_OBJECT(m, object_constants[i].name, valObject);
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006451 }
6452
6453 if (!(VimError = PyErr_NewException("vim.error", NULL, NULL)))
6454 return -1;
6455 ADD_OBJECT(m, "error", VimError);
6456
Bram Moolenaara9922d62013-05-30 13:01:18 +02006457 ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6458 ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006459 ADD_CHECKED_OBJECT(m, "options",
6460 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaarf4258302013-06-02 18:20:17 +02006461
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006462 if (!(other_module = PyImport_ImportModule("os")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006463 return -1;
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006464 ADD_OBJECT(m, "os", other_module);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006465
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006466 if (!(py_getcwd = PyObject_GetAttrString(other_module, "getcwd")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006467 return -1;
6468 ADD_OBJECT(m, "_getcwd", py_getcwd)
6469
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006470 if (!(py_chdir = PyObject_GetAttrString(other_module, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006471 return -1;
6472 ADD_OBJECT(m, "_chdir", py_chdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006473 if (!(attr = PyObject_GetAttrString(m, "chdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006474 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006475 if (PyObject_SetAttrString(other_module, "chdir", attr))
6476 {
6477 Py_DECREF(attr);
6478 return -1;
6479 }
6480 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006481
Bram Moolenaarc1ba10c2013-06-10 20:39:03 +02006482 if ((py_fchdir = PyObject_GetAttrString(other_module, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006483 {
6484 ADD_OBJECT(m, "_fchdir", py_fchdir);
Bram Moolenaardee2e312013-06-23 16:35:47 +02006485 if (!(attr = PyObject_GetAttrString(m, "fchdir")))
Bram Moolenaarf4258302013-06-02 18:20:17 +02006486 return -1;
Bram Moolenaarf9c9b322013-06-10 20:47:36 +02006487 if (PyObject_SetAttrString(other_module, "fchdir", attr))
6488 {
6489 Py_DECREF(attr);
6490 return -1;
6491 }
6492 Py_DECREF(attr);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006493 }
Bram Moolenaare9056b12013-06-03 20:04:48 +02006494 else
6495 PyErr_Clear();
Bram Moolenaarf4258302013-06-02 18:20:17 +02006496
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006497 if (!(vim_special_path_object = PyString_FromString(vim_special_path)))
6498 return -1;
6499
6500 ADD_OBJECT(m, "VIM_SPECIAL_PATH", vim_special_path_object);
6501
Bram Moolenaar81c40c52013-06-12 14:41:04 +02006502 if (!(imp = PyImport_ImportModule("imp")))
6503 return -1;
6504
6505 if (!(py_find_module = PyObject_GetAttrString(imp, "find_module")))
6506 {
6507 Py_DECREF(imp);
6508 return -1;
6509 }
6510
6511 if (!(py_load_module = PyObject_GetAttrString(imp, "load_module")))
6512 {
6513 Py_DECREF(py_find_module);
6514 Py_DECREF(imp);
6515 return -1;
6516 }
6517
6518 Py_DECREF(imp);
6519
Bram Moolenaar9f3685a2013-06-12 14:20:36 +02006520 ADD_OBJECT(m, "_find_module", py_find_module);
6521 ADD_OBJECT(m, "_load_module", py_load_module);
Bram Moolenaarc09a6d62013-06-10 21:27:29 +02006522
Bram Moolenaar1dc28782013-05-21 19:11:01 +02006523 return 0;
6524}